You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2011 lines
62KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "imgconvert.h"
  40. #include "thread.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter=0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if(min_size < *size)
  55. return ptr;
  56. min_size= FFMAX(17*min_size/16 + 32, min_size);
  57. ptr= av_realloc(ptr, min_size);
  58. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  59. min_size= 0;
  60. *size= min_size;
  61. return ptr;
  62. }
  63. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  64. {
  65. void **p = ptr;
  66. if (min_size < *size)
  67. return 0;
  68. min_size= FFMAX(17*min_size/16 + 32, min_size);
  69. av_free(*p);
  70. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  71. if (!*p) min_size = 0;
  72. *size= min_size;
  73. return 1;
  74. }
  75. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  76. {
  77. ff_fast_malloc(ptr, size, min_size, 0);
  78. }
  79. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. uint8_t **p = ptr;
  82. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  83. *p = NULL;
  84. *size = 0;
  85. return;
  86. }
  87. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  88. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(AVCodec *c){
  93. if(c) return c->next;
  94. else return first_avcodec;
  95. }
  96. #if !FF_API_AVCODEC_INIT
  97. static
  98. #endif
  99. void avcodec_init(void)
  100. {
  101. static int initialized = 0;
  102. if (initialized != 0)
  103. return;
  104. initialized = 1;
  105. dsputil_static_init();
  106. }
  107. static av_always_inline int codec_is_encoder(AVCodec *codec)
  108. {
  109. return codec && (codec->encode || codec->encode2);
  110. }
  111. static av_always_inline int codec_is_decoder(AVCodec *codec)
  112. {
  113. return codec && codec->decode;
  114. }
  115. void avcodec_register(AVCodec *codec)
  116. {
  117. AVCodec **p;
  118. avcodec_init();
  119. p = &first_avcodec;
  120. while (*p != NULL) p = &(*p)->next;
  121. *p = codec;
  122. codec->next = NULL;
  123. if (codec->init_static_data)
  124. codec->init_static_data(codec);
  125. }
  126. unsigned avcodec_get_edge_width(void)
  127. {
  128. return EDGE_WIDTH;
  129. }
  130. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  131. s->coded_width = width;
  132. s->coded_height= height;
  133. s->width = -((-width )>>s->lowres);
  134. s->height= -((-height)>>s->lowres);
  135. }
  136. #define INTERNAL_BUFFER_SIZE (32+1)
  137. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  138. int linesize_align[AV_NUM_DATA_POINTERS])
  139. {
  140. int i;
  141. int w_align= 1;
  142. int h_align= 1;
  143. switch(s->pix_fmt){
  144. case PIX_FMT_YUV420P:
  145. case PIX_FMT_YUYV422:
  146. case PIX_FMT_UYVY422:
  147. case PIX_FMT_YUV422P:
  148. case PIX_FMT_YUV440P:
  149. case PIX_FMT_YUV444P:
  150. case PIX_FMT_GBRP:
  151. case PIX_FMT_GRAY8:
  152. case PIX_FMT_GRAY16BE:
  153. case PIX_FMT_GRAY16LE:
  154. case PIX_FMT_YUVJ420P:
  155. case PIX_FMT_YUVJ422P:
  156. case PIX_FMT_YUVJ440P:
  157. case PIX_FMT_YUVJ444P:
  158. case PIX_FMT_YUVA420P:
  159. case PIX_FMT_YUV420P9LE:
  160. case PIX_FMT_YUV420P9BE:
  161. case PIX_FMT_YUV420P10LE:
  162. case PIX_FMT_YUV420P10BE:
  163. case PIX_FMT_YUV422P9LE:
  164. case PIX_FMT_YUV422P9BE:
  165. case PIX_FMT_YUV422P10LE:
  166. case PIX_FMT_YUV422P10BE:
  167. case PIX_FMT_YUV444P9LE:
  168. case PIX_FMT_YUV444P9BE:
  169. case PIX_FMT_YUV444P10LE:
  170. case PIX_FMT_YUV444P10BE:
  171. case PIX_FMT_GBRP9LE:
  172. case PIX_FMT_GBRP9BE:
  173. case PIX_FMT_GBRP10LE:
  174. case PIX_FMT_GBRP10BE:
  175. w_align = 16; //FIXME assume 16 pixel per macroblock
  176. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  177. break;
  178. case PIX_FMT_YUV411P:
  179. case PIX_FMT_UYYVYY411:
  180. w_align=32;
  181. h_align=8;
  182. break;
  183. case PIX_FMT_YUV410P:
  184. if(s->codec_id == CODEC_ID_SVQ1){
  185. w_align=64;
  186. h_align=64;
  187. }
  188. case PIX_FMT_RGB555:
  189. if(s->codec_id == CODEC_ID_RPZA){
  190. w_align=4;
  191. h_align=4;
  192. }
  193. case PIX_FMT_PAL8:
  194. case PIX_FMT_BGR8:
  195. case PIX_FMT_RGB8:
  196. if(s->codec_id == CODEC_ID_SMC){
  197. w_align=4;
  198. h_align=4;
  199. }
  200. break;
  201. case PIX_FMT_BGR24:
  202. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  203. w_align=4;
  204. h_align=4;
  205. }
  206. break;
  207. default:
  208. w_align= 1;
  209. h_align= 1;
  210. break;
  211. }
  212. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  213. w_align= FFMAX(w_align, 8);
  214. }
  215. *width = FFALIGN(*width , w_align);
  216. *height= FFALIGN(*height, h_align);
  217. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  218. *height+=2; // some of the optimized chroma MC reads one line too much
  219. // which is also done in mpeg decoders with lowres > 0
  220. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  221. linesize_align[i] = STRIDE_ALIGN;
  222. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  223. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  224. //picture size unneccessarily in some cases. The solution here is not
  225. //pretty and better ideas are welcome!
  226. #if HAVE_MMX
  227. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  228. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  229. s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
  230. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  231. linesize_align[i] = 16;
  232. }
  233. #endif
  234. }
  235. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  236. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  237. int linesize_align[AV_NUM_DATA_POINTERS];
  238. int align;
  239. avcodec_align_dimensions2(s, width, height, linesize_align);
  240. align = FFMAX(linesize_align[0], linesize_align[3]);
  241. linesize_align[1] <<= chroma_shift;
  242. linesize_align[2] <<= chroma_shift;
  243. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  244. *width=FFALIGN(*width, align);
  245. }
  246. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  247. {
  248. if (s->pkt) {
  249. pic->pkt_pts = s->pkt->pts;
  250. pic->pkt_pos = s->pkt->pos;
  251. } else {
  252. pic->pkt_pts = AV_NOPTS_VALUE;
  253. pic->pkt_pos = -1;
  254. }
  255. pic->reordered_opaque= s->reordered_opaque;
  256. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  257. pic->width = s->width;
  258. pic->height = s->height;
  259. pic->format = s->pix_fmt;
  260. }
  261. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  262. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  263. int buf_size, int align)
  264. {
  265. int ch, planar, needed_size, ret = 0;
  266. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  267. frame->nb_samples, sample_fmt,
  268. align);
  269. if (buf_size < needed_size)
  270. return AVERROR(EINVAL);
  271. planar = av_sample_fmt_is_planar(sample_fmt);
  272. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  273. if (!(frame->extended_data = av_mallocz(nb_channels *
  274. sizeof(*frame->extended_data))))
  275. return AVERROR(ENOMEM);
  276. } else {
  277. frame->extended_data = frame->data;
  278. }
  279. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  280. buf, nb_channels, frame->nb_samples,
  281. sample_fmt, align)) < 0) {
  282. if (frame->extended_data != frame->data)
  283. av_freep(&frame->extended_data);
  284. return ret;
  285. }
  286. if (frame->extended_data != frame->data) {
  287. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  288. frame->data[ch] = frame->extended_data[ch];
  289. }
  290. return ret;
  291. }
  292. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  293. {
  294. AVCodecInternal *avci = avctx->internal;
  295. InternalBuffer *buf;
  296. int buf_size, ret;
  297. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  298. frame->nb_samples, avctx->sample_fmt,
  299. 32);
  300. if (buf_size < 0)
  301. return AVERROR(EINVAL);
  302. /* allocate InternalBuffer if needed */
  303. if (!avci->buffer) {
  304. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  305. if (!avci->buffer)
  306. return AVERROR(ENOMEM);
  307. }
  308. buf = avci->buffer;
  309. /* if there is a previously-used internal buffer, check its size and
  310. channel count to see if we can reuse it */
  311. if (buf->extended_data) {
  312. /* if current buffer is too small, free it */
  313. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  314. av_free(buf->extended_data[0]);
  315. if (buf->extended_data != buf->data)
  316. av_freep(&buf->extended_data);
  317. buf->extended_data = NULL;
  318. buf->data[0] = NULL;
  319. }
  320. /* if number of channels has changed, reset and/or free extended data
  321. pointers but leave data buffer in buf->data[0] for reuse */
  322. if (buf->nb_channels != avctx->channels) {
  323. if (buf->extended_data != buf->data)
  324. av_free(buf->extended_data);
  325. buf->extended_data = NULL;
  326. }
  327. }
  328. /* if there is no previous buffer or the previous buffer cannot be used
  329. as-is, allocate a new buffer and/or rearrange the channel pointers */
  330. if (!buf->extended_data) {
  331. if (!buf->data[0]) {
  332. if (!(buf->data[0] = av_mallocz(buf_size)))
  333. return AVERROR(ENOMEM);
  334. buf->audio_data_size = buf_size;
  335. }
  336. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  337. avctx->sample_fmt, buf->data[0],
  338. buf->audio_data_size, 32)))
  339. return ret;
  340. if (frame->extended_data == frame->data)
  341. buf->extended_data = buf->data;
  342. else
  343. buf->extended_data = frame->extended_data;
  344. memcpy(buf->data, frame->data, sizeof(frame->data));
  345. buf->linesize[0] = frame->linesize[0];
  346. buf->nb_channels = avctx->channels;
  347. } else {
  348. /* copy InternalBuffer info to the AVFrame */
  349. frame->extended_data = buf->extended_data;
  350. frame->linesize[0] = buf->linesize[0];
  351. memcpy(frame->data, buf->data, sizeof(frame->data));
  352. }
  353. frame->type = FF_BUFFER_TYPE_INTERNAL;
  354. if (avctx->pkt) {
  355. frame->pkt_pts = avctx->pkt->pts;
  356. frame->pkt_pos = avctx->pkt->pos;
  357. } else {
  358. frame->pkt_pts = AV_NOPTS_VALUE;
  359. frame->pkt_pos = -1;
  360. }
  361. frame->reordered_opaque = avctx->reordered_opaque;
  362. if (avctx->debug & FF_DEBUG_BUFFERS)
  363. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  364. "internal audio buffer used\n", frame);
  365. return 0;
  366. }
  367. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  368. {
  369. int i;
  370. int w= s->width;
  371. int h= s->height;
  372. InternalBuffer *buf;
  373. AVCodecInternal *avci = s->internal;
  374. if(pic->data[0]!=NULL) {
  375. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  376. return -1;
  377. }
  378. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  379. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  380. return -1;
  381. }
  382. if(av_image_check_size(w, h, 0, s))
  383. return -1;
  384. if (!avci->buffer) {
  385. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  386. sizeof(InternalBuffer));
  387. }
  388. buf = &avci->buffer[avci->buffer_count];
  389. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  390. if(s->active_thread_type&FF_THREAD_FRAME) {
  391. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  392. return -1;
  393. }
  394. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  395. av_freep(&buf->base[i]);
  396. buf->data[i]= NULL;
  397. }
  398. }
  399. if (!buf->base[0]) {
  400. int h_chroma_shift, v_chroma_shift;
  401. int size[4] = {0};
  402. int tmpsize;
  403. int unaligned;
  404. AVPicture picture;
  405. int stride_align[AV_NUM_DATA_POINTERS];
  406. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  407. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  408. avcodec_align_dimensions2(s, &w, &h, stride_align);
  409. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  410. w+= EDGE_WIDTH*2;
  411. h+= EDGE_WIDTH*2;
  412. }
  413. do {
  414. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  415. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  416. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  417. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  418. w += w & ~(w-1);
  419. unaligned = 0;
  420. for (i=0; i<4; i++){
  421. unaligned |= picture.linesize[i] % stride_align[i];
  422. }
  423. } while (unaligned);
  424. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  425. if (tmpsize < 0)
  426. return -1;
  427. for (i=0; i<3 && picture.data[i+1]; i++)
  428. size[i] = picture.data[i+1] - picture.data[i];
  429. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  430. memset(buf->base, 0, sizeof(buf->base));
  431. memset(buf->data, 0, sizeof(buf->data));
  432. for(i=0; i<4 && size[i]; i++){
  433. const int h_shift= i==0 ? 0 : h_chroma_shift;
  434. const int v_shift= i==0 ? 0 : v_chroma_shift;
  435. buf->linesize[i]= picture.linesize[i];
  436. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  437. if(buf->base[i]==NULL) return -1;
  438. memset(buf->base[i], 128, size[i]);
  439. // no edge if EDGE EMU or not planar YUV
  440. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  441. buf->data[i] = buf->base[i];
  442. else
  443. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  444. }
  445. for (; i < AV_NUM_DATA_POINTERS; i++) {
  446. buf->base[i] = buf->data[i] = NULL;
  447. buf->linesize[i] = 0;
  448. }
  449. if(size[1] && !size[2])
  450. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  451. buf->width = s->width;
  452. buf->height = s->height;
  453. buf->pix_fmt= s->pix_fmt;
  454. }
  455. pic->type= FF_BUFFER_TYPE_INTERNAL;
  456. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  457. pic->base[i]= buf->base[i];
  458. pic->data[i]= buf->data[i];
  459. pic->linesize[i]= buf->linesize[i];
  460. }
  461. pic->extended_data = pic->data;
  462. avci->buffer_count++;
  463. if (s->pkt) {
  464. pic->pkt_pts = s->pkt->pts;
  465. pic->pkt_pos = s->pkt->pos;
  466. } else {
  467. pic->pkt_pts = AV_NOPTS_VALUE;
  468. pic->pkt_pos = -1;
  469. }
  470. pic->reordered_opaque= s->reordered_opaque;
  471. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  472. pic->width = s->width;
  473. pic->height = s->height;
  474. pic->format = s->pix_fmt;
  475. if(s->debug&FF_DEBUG_BUFFERS)
  476. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  477. "buffers used\n", pic, avci->buffer_count);
  478. return 0;
  479. }
  480. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  481. {
  482. switch (avctx->codec_type) {
  483. case AVMEDIA_TYPE_VIDEO:
  484. return video_get_buffer(avctx, frame);
  485. case AVMEDIA_TYPE_AUDIO:
  486. return audio_get_buffer(avctx, frame);
  487. default:
  488. return -1;
  489. }
  490. }
  491. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  492. int i;
  493. InternalBuffer *buf, *last;
  494. AVCodecInternal *avci = s->internal;
  495. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  496. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  497. assert(avci->buffer_count);
  498. if (avci->buffer) {
  499. buf = NULL; /* avoids warning */
  500. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  501. buf = &avci->buffer[i];
  502. if (buf->data[0] == pic->data[0])
  503. break;
  504. }
  505. assert(i < avci->buffer_count);
  506. avci->buffer_count--;
  507. last = &avci->buffer[avci->buffer_count];
  508. if (buf != last)
  509. FFSWAP(InternalBuffer, *buf, *last);
  510. }
  511. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  512. pic->data[i]=NULL;
  513. // pic->base[i]=NULL;
  514. }
  515. //printf("R%X\n", pic->opaque);
  516. if(s->debug&FF_DEBUG_BUFFERS)
  517. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  518. "buffers used\n", pic, avci->buffer_count);
  519. }
  520. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  521. AVFrame temp_pic;
  522. int i;
  523. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  524. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  525. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  526. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  527. s->release_buffer(s, pic);
  528. }
  529. ff_init_buffer_info(s, pic);
  530. /* If no picture return a new buffer */
  531. if(pic->data[0] == NULL) {
  532. /* We will copy from buffer, so must be readable */
  533. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  534. return s->get_buffer(s, pic);
  535. }
  536. /* If internal buffer type return the same buffer */
  537. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  538. return 0;
  539. }
  540. /*
  541. * Not internal type and reget_buffer not overridden, emulate cr buffer
  542. */
  543. temp_pic = *pic;
  544. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  545. pic->data[i] = pic->base[i] = NULL;
  546. pic->opaque = NULL;
  547. /* Allocate new frame */
  548. if (s->get_buffer(s, pic))
  549. return -1;
  550. /* Copy image data from old buffer to new buffer */
  551. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  552. s->height);
  553. s->release_buffer(s, &temp_pic); // Release old frame
  554. return 0;
  555. }
  556. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  557. int i;
  558. for(i=0; i<count; i++){
  559. int r= func(c, (char*)arg + i*size);
  560. if(ret) ret[i]= r;
  561. }
  562. return 0;
  563. }
  564. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  565. int i;
  566. for(i=0; i<count; i++){
  567. int r= func(c, arg, i, 0);
  568. if(ret) ret[i]= r;
  569. }
  570. return 0;
  571. }
  572. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  573. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  574. ++fmt;
  575. return fmt[0];
  576. }
  577. void avcodec_get_frame_defaults(AVFrame *pic){
  578. memset(pic, 0, sizeof(AVFrame));
  579. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  580. pic->pkt_pos = -1;
  581. pic->key_frame= 1;
  582. pic->sample_aspect_ratio = (AVRational){0, 1};
  583. pic->format = -1; /* unknown */
  584. }
  585. AVFrame *avcodec_alloc_frame(void){
  586. AVFrame *pic= av_malloc(sizeof(AVFrame));
  587. if(pic==NULL) return NULL;
  588. avcodec_get_frame_defaults(pic);
  589. return pic;
  590. }
  591. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  592. {
  593. memset(sub, 0, sizeof(*sub));
  594. sub->pts = AV_NOPTS_VALUE;
  595. }
  596. #if FF_API_AVCODEC_OPEN
  597. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  598. {
  599. return avcodec_open2(avctx, codec, NULL);
  600. }
  601. #endif
  602. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  603. {
  604. int ret = 0;
  605. AVDictionary *tmp = NULL;
  606. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  607. return AVERROR(EINVAL);
  608. if (options)
  609. av_dict_copy(&tmp, *options, 0);
  610. /* If there is a user-supplied mutex locking routine, call it. */
  611. if (ff_lockmgr_cb) {
  612. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  613. return -1;
  614. }
  615. entangled_thread_counter++;
  616. if(entangled_thread_counter != 1){
  617. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  618. ret = -1;
  619. goto end;
  620. }
  621. if(avctx->codec || !codec) {
  622. ret = AVERROR(EINVAL);
  623. goto end;
  624. }
  625. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  626. if (!avctx->internal) {
  627. ret = AVERROR(ENOMEM);
  628. goto end;
  629. }
  630. if (codec->priv_data_size > 0) {
  631. if(!avctx->priv_data){
  632. avctx->priv_data = av_mallocz(codec->priv_data_size);
  633. if (!avctx->priv_data) {
  634. ret = AVERROR(ENOMEM);
  635. goto end;
  636. }
  637. if (codec->priv_class) {
  638. *(AVClass**)avctx->priv_data= codec->priv_class;
  639. av_opt_set_defaults(avctx->priv_data);
  640. }
  641. }
  642. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  643. goto free_and_end;
  644. } else {
  645. avctx->priv_data = NULL;
  646. }
  647. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  648. goto free_and_end;
  649. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  650. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  651. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
  652. ret = -1;
  653. goto free_and_end;
  654. }
  655. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  656. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  657. if(avctx->coded_width && avctx->coded_height)
  658. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  659. else if(avctx->width && avctx->height)
  660. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  661. }
  662. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  663. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  664. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  665. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  666. avcodec_set_dimensions(avctx, 0, 0);
  667. }
  668. /* if the decoder init function was already called previously,
  669. free the already allocated subtitle_header before overwriting it */
  670. if (codec_is_decoder(codec))
  671. av_freep(&avctx->subtitle_header);
  672. #define SANE_NB_CHANNELS 128U
  673. if (avctx->channels > SANE_NB_CHANNELS) {
  674. ret = AVERROR(EINVAL);
  675. goto free_and_end;
  676. }
  677. avctx->codec = codec;
  678. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  679. avctx->codec_id == CODEC_ID_NONE) {
  680. avctx->codec_type = codec->type;
  681. avctx->codec_id = codec->id;
  682. }
  683. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  684. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  685. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  686. ret = AVERROR(EINVAL);
  687. goto free_and_end;
  688. }
  689. avctx->frame_number = 0;
  690. #if FF_API_ER
  691. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %X\n",
  692. avctx->error_recognition, avctx->err_recognition);
  693. switch(avctx->error_recognition){
  694. case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
  695. break;
  696. case FF_ER_VERY_AGGRESSIVE:
  697. case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
  698. case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
  699. case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
  700. }
  701. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %X\n",
  702. avctx->error_recognition, avctx->err_recognition);
  703. #endif
  704. if (!HAVE_THREADS)
  705. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  706. if (HAVE_THREADS && !avctx->thread_opaque) {
  707. ret = ff_thread_init(avctx);
  708. if (ret < 0) {
  709. goto free_and_end;
  710. }
  711. }
  712. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  713. avctx->thread_count = 1;
  714. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  715. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  716. avctx->codec->max_lowres);
  717. ret = AVERROR(EINVAL);
  718. goto free_and_end;
  719. }
  720. if (codec_is_encoder(avctx->codec)) {
  721. int i;
  722. if (avctx->codec->sample_fmts) {
  723. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  724. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  725. break;
  726. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  727. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  728. ret = AVERROR(EINVAL);
  729. goto free_and_end;
  730. }
  731. }
  732. if (avctx->codec->supported_samplerates) {
  733. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  734. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  735. break;
  736. if (avctx->codec->supported_samplerates[i] == 0) {
  737. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  738. ret = AVERROR(EINVAL);
  739. goto free_and_end;
  740. }
  741. }
  742. if (avctx->codec->channel_layouts) {
  743. if (!avctx->channel_layout) {
  744. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  745. } else {
  746. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  747. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  748. break;
  749. if (avctx->codec->channel_layouts[i] == 0) {
  750. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  751. ret = AVERROR(EINVAL);
  752. goto free_and_end;
  753. }
  754. }
  755. }
  756. if (avctx->channel_layout && avctx->channels) {
  757. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  758. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  759. ret = AVERROR(EINVAL);
  760. goto free_and_end;
  761. }
  762. } else if (avctx->channel_layout) {
  763. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  764. }
  765. }
  766. avctx->pts_correction_num_faulty_pts =
  767. avctx->pts_correction_num_faulty_dts = 0;
  768. avctx->pts_correction_last_pts =
  769. avctx->pts_correction_last_dts = INT64_MIN;
  770. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  771. ret = avctx->codec->init(avctx);
  772. if (ret < 0) {
  773. goto free_and_end;
  774. }
  775. }
  776. ret=0;
  777. end:
  778. entangled_thread_counter--;
  779. /* Release any user-supplied mutex. */
  780. if (ff_lockmgr_cb) {
  781. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  782. }
  783. if (options) {
  784. av_dict_free(options);
  785. *options = tmp;
  786. }
  787. return ret;
  788. free_and_end:
  789. av_dict_free(&tmp);
  790. av_freep(&avctx->priv_data);
  791. av_freep(&avctx->internal);
  792. avctx->codec= NULL;
  793. goto end;
  794. }
  795. int ff_alloc_packet(AVPacket *avpkt, int size)
  796. {
  797. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  798. return AVERROR(EINVAL);
  799. if (avpkt->data) {
  800. uint8_t *pkt_data;
  801. int pkt_size;
  802. if (avpkt->size < size)
  803. return AVERROR(EINVAL);
  804. pkt_data = avpkt->data;
  805. pkt_size = avpkt->size;
  806. av_init_packet(avpkt);
  807. avpkt->data = pkt_data;
  808. avpkt->size = pkt_size;
  809. return 0;
  810. } else {
  811. return av_new_packet(avpkt, size);
  812. }
  813. }
  814. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  815. AVPacket *avpkt,
  816. const AVFrame *frame,
  817. int *got_packet_ptr)
  818. {
  819. int ret;
  820. int user_packet = !!avpkt->data;
  821. int nb_samples;
  822. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  823. av_init_packet(avpkt);
  824. avpkt->size = 0;
  825. return 0;
  826. }
  827. /* check for valid frame size */
  828. if (frame) {
  829. nb_samples = frame->nb_samples;
  830. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  831. if (nb_samples > avctx->frame_size)
  832. return AVERROR(EINVAL);
  833. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  834. if (nb_samples != avctx->frame_size)
  835. return AVERROR(EINVAL);
  836. }
  837. } else {
  838. nb_samples = avctx->frame_size;
  839. }
  840. if (avctx->codec->encode2) {
  841. *got_packet_ptr = 0;
  842. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  843. if (!ret && *got_packet_ptr &&
  844. !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  845. avpkt->pts = frame->pts;
  846. avpkt->duration = av_rescale_q(frame->nb_samples,
  847. (AVRational){ 1, avctx->sample_rate },
  848. avctx->time_base);
  849. }
  850. } else {
  851. /* for compatibility with encoders not supporting encode2(), we need to
  852. allocate a packet buffer if the user has not provided one or check
  853. the size otherwise */
  854. int fs_tmp = 0;
  855. int buf_size = avpkt->size;
  856. if (!user_packet) {
  857. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  858. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  859. if (!frame)
  860. return AVERROR(EINVAL);
  861. buf_size = nb_samples * avctx->channels *
  862. av_get_bits_per_sample(avctx->codec_id) / 8;
  863. } else {
  864. /* this is a guess as to the required size.
  865. if an encoder needs more than this, it should probably
  866. implement encode2() */
  867. buf_size = 2 * avctx->frame_size * avctx->channels *
  868. av_get_bytes_per_sample(avctx->sample_fmt);
  869. buf_size += FF_MIN_BUFFER_SIZE;
  870. }
  871. }
  872. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  873. return ret;
  874. /* Encoders using AVCodec.encode() that support
  875. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  876. the smaller size when encoding the last frame.
  877. This code can be removed once all encoders supporting
  878. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  879. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  880. nb_samples < avctx->frame_size) {
  881. fs_tmp = avctx->frame_size;
  882. avctx->frame_size = nb_samples;
  883. }
  884. /* encode the frame */
  885. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  886. frame ? frame->data[0] : NULL);
  887. if (ret >= 0) {
  888. if (!ret) {
  889. /* no output. if the packet data was allocated by libavcodec,
  890. free it */
  891. if (!user_packet)
  892. av_freep(&avpkt->data);
  893. } else {
  894. if (avctx->coded_frame)
  895. avpkt->pts = avctx->coded_frame->pts;
  896. /* Set duration for final small packet. This can be removed
  897. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  898. encode2() */
  899. if (fs_tmp) {
  900. avpkt->duration = av_rescale_q(avctx->frame_size,
  901. (AVRational){ 1, avctx->sample_rate },
  902. avctx->time_base);
  903. }
  904. }
  905. avpkt->size = ret;
  906. *got_packet_ptr = (ret > 0);
  907. ret = 0;
  908. }
  909. if (fs_tmp)
  910. avctx->frame_size = fs_tmp;
  911. }
  912. if (!ret)
  913. avctx->frame_number++;
  914. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  915. this needs to be moved to the encoders, but for now we can do it
  916. here to simplify things */
  917. avpkt->flags |= AV_PKT_FLAG_KEY;
  918. return ret;
  919. }
  920. #if FF_API_OLD_DECODE_AUDIO
  921. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  922. uint8_t *buf, int buf_size,
  923. const short *samples)
  924. {
  925. AVPacket pkt;
  926. AVFrame frame0;
  927. AVFrame *frame;
  928. int ret, samples_size, got_packet;
  929. av_init_packet(&pkt);
  930. pkt.data = buf;
  931. pkt.size = buf_size;
  932. if (samples) {
  933. frame = &frame0;
  934. avcodec_get_frame_defaults(frame);
  935. if (avctx->frame_size) {
  936. frame->nb_samples = avctx->frame_size;
  937. } else {
  938. /* if frame_size is not set, the number of samples must be
  939. calculated from the buffer size */
  940. int64_t nb_samples;
  941. if (!av_get_bits_per_sample(avctx->codec_id)) {
  942. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  943. "support this codec\n");
  944. return AVERROR(EINVAL);
  945. }
  946. nb_samples = (int64_t)buf_size * 8 /
  947. (av_get_bits_per_sample(avctx->codec_id) *
  948. avctx->channels);
  949. if (nb_samples >= INT_MAX)
  950. return AVERROR(EINVAL);
  951. frame->nb_samples = nb_samples;
  952. }
  953. /* it is assumed that the samples buffer is large enough based on the
  954. relevant parameters */
  955. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  956. frame->nb_samples,
  957. avctx->sample_fmt, 1);
  958. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  959. avctx->sample_fmt,
  960. samples, samples_size, 1)))
  961. return ret;
  962. /* fabricate frame pts from sample count.
  963. this is needed because the avcodec_encode_audio() API does not have
  964. a way for the user to provide pts */
  965. if(avctx->sample_rate && avctx->time_base.num)
  966. frame->pts = av_rescale_q(avctx->internal->sample_count,
  967. (AVRational){ 1, avctx->sample_rate },
  968. avctx->time_base);
  969. else
  970. frame->pts = AV_NOPTS_VALUE;
  971. avctx->internal->sample_count += frame->nb_samples;
  972. } else {
  973. frame = NULL;
  974. }
  975. got_packet = 0;
  976. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  977. if (!ret && got_packet && avctx->coded_frame) {
  978. avctx->coded_frame->pts = pkt.pts;
  979. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  980. }
  981. /* free any side data since we cannot return it */
  982. ff_packet_free_side_data(&pkt);
  983. if (frame && frame->extended_data != frame->data)
  984. av_freep(&frame->extended_data);
  985. return ret ? ret : pkt.size;
  986. }
  987. #endif
  988. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  989. const AVFrame *pict)
  990. {
  991. if(buf_size < FF_MIN_BUFFER_SIZE){
  992. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  993. return -1;
  994. }
  995. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  996. return -1;
  997. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  998. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  999. avctx->frame_number++;
  1000. emms_c(); //needed to avoid an emms_c() call before every return;
  1001. return ret;
  1002. }else
  1003. return 0;
  1004. }
  1005. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1006. const AVSubtitle *sub)
  1007. {
  1008. int ret;
  1009. if(sub->start_display_time) {
  1010. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1011. return -1;
  1012. }
  1013. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  1014. avctx->frame_number++;
  1015. return ret;
  1016. }
  1017. /**
  1018. * Attempt to guess proper monotonic timestamps for decoded video frames
  1019. * which might have incorrect times. Input timestamps may wrap around, in
  1020. * which case the output will as well.
  1021. *
  1022. * @param pts the pts field of the decoded AVPacket, as passed through
  1023. * AVFrame.pkt_pts
  1024. * @param dts the dts field of the decoded AVPacket
  1025. * @return one of the input values, may be AV_NOPTS_VALUE
  1026. */
  1027. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1028. int64_t reordered_pts, int64_t dts)
  1029. {
  1030. int64_t pts = AV_NOPTS_VALUE;
  1031. if (dts != AV_NOPTS_VALUE) {
  1032. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1033. ctx->pts_correction_last_dts = dts;
  1034. }
  1035. if (reordered_pts != AV_NOPTS_VALUE) {
  1036. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1037. ctx->pts_correction_last_pts = reordered_pts;
  1038. }
  1039. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1040. && reordered_pts != AV_NOPTS_VALUE)
  1041. pts = reordered_pts;
  1042. else
  1043. pts = dts;
  1044. return pts;
  1045. }
  1046. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1047. {
  1048. int size = 0;
  1049. const uint8_t *data;
  1050. uint32_t flags;
  1051. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1052. return;
  1053. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1054. if (!data || size < 4)
  1055. return;
  1056. flags = bytestream_get_le32(&data);
  1057. size -= 4;
  1058. if (size < 4) /* Required for any of the changes */
  1059. return;
  1060. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1061. avctx->channels = bytestream_get_le32(&data);
  1062. size -= 4;
  1063. }
  1064. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1065. if (size < 8)
  1066. return;
  1067. avctx->channel_layout = bytestream_get_le64(&data);
  1068. size -= 8;
  1069. }
  1070. if (size < 4)
  1071. return;
  1072. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1073. avctx->sample_rate = bytestream_get_le32(&data);
  1074. size -= 4;
  1075. }
  1076. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1077. if (size < 8)
  1078. return;
  1079. avctx->width = bytestream_get_le32(&data);
  1080. avctx->height = bytestream_get_le32(&data);
  1081. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1082. size -= 8;
  1083. }
  1084. }
  1085. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1086. int *got_picture_ptr,
  1087. const AVPacket *avpkt)
  1088. {
  1089. int ret;
  1090. // copy to ensure we do not change avpkt
  1091. AVPacket tmp = *avpkt;
  1092. *got_picture_ptr= 0;
  1093. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1094. return -1;
  1095. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1096. int did_split = av_packet_split_side_data(&tmp);
  1097. apply_param_change(avctx, &tmp);
  1098. avctx->pkt = &tmp;
  1099. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1100. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1101. &tmp);
  1102. else {
  1103. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1104. &tmp);
  1105. picture->pkt_dts= avpkt->dts;
  1106. if(!avctx->has_b_frames){
  1107. picture->pkt_pos= avpkt->pos;
  1108. }
  1109. //FIXME these should be under if(!avctx->has_b_frames)
  1110. if (!picture->sample_aspect_ratio.num)
  1111. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1112. if (!picture->width)
  1113. picture->width = avctx->width;
  1114. if (!picture->height)
  1115. picture->height = avctx->height;
  1116. if (picture->format == PIX_FMT_NONE)
  1117. picture->format = avctx->pix_fmt;
  1118. }
  1119. emms_c(); //needed to avoid an emms_c() call before every return;
  1120. avctx->pkt = NULL;
  1121. if (did_split)
  1122. ff_packet_free_side_data(&tmp);
  1123. if (*got_picture_ptr){
  1124. avctx->frame_number++;
  1125. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1126. picture->pkt_pts,
  1127. picture->pkt_dts);
  1128. }
  1129. }else
  1130. ret= 0;
  1131. return ret;
  1132. }
  1133. #if FF_API_OLD_DECODE_AUDIO
  1134. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1135. int *frame_size_ptr,
  1136. AVPacket *avpkt)
  1137. {
  1138. AVFrame frame;
  1139. int ret, got_frame = 0;
  1140. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1141. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1142. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1143. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1144. "avcodec_decode_audio4()\n");
  1145. avctx->get_buffer = avcodec_default_get_buffer;
  1146. avctx->release_buffer = avcodec_default_release_buffer;
  1147. }
  1148. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1149. if (ret >= 0 && got_frame) {
  1150. int ch, plane_size;
  1151. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1152. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1153. frame.nb_samples,
  1154. avctx->sample_fmt, 1);
  1155. if (*frame_size_ptr < data_size) {
  1156. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1157. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1158. return AVERROR(EINVAL);
  1159. }
  1160. memcpy(samples, frame.extended_data[0], plane_size);
  1161. if (planar && avctx->channels > 1) {
  1162. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1163. for (ch = 1; ch < avctx->channels; ch++) {
  1164. memcpy(out, frame.extended_data[ch], plane_size);
  1165. out += plane_size;
  1166. }
  1167. }
  1168. *frame_size_ptr = data_size;
  1169. } else {
  1170. *frame_size_ptr = 0;
  1171. }
  1172. return ret;
  1173. }
  1174. #endif
  1175. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1176. AVFrame *frame,
  1177. int *got_frame_ptr,
  1178. AVPacket *avpkt)
  1179. {
  1180. int ret = 0;
  1181. *got_frame_ptr = 0;
  1182. if (!avpkt->data && avpkt->size) {
  1183. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1184. return AVERROR(EINVAL);
  1185. }
  1186. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1187. av_packet_split_side_data(avpkt);
  1188. apply_param_change(avctx, avpkt);
  1189. avctx->pkt = avpkt;
  1190. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1191. if (ret >= 0 && *got_frame_ptr) {
  1192. avctx->frame_number++;
  1193. frame->pkt_dts = avpkt->dts;
  1194. if (frame->format == AV_SAMPLE_FMT_NONE)
  1195. frame->format = avctx->sample_fmt;
  1196. }
  1197. }
  1198. return ret;
  1199. }
  1200. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1201. int *got_sub_ptr,
  1202. AVPacket *avpkt)
  1203. {
  1204. int ret;
  1205. avctx->pkt = avpkt;
  1206. *got_sub_ptr = 0;
  1207. avcodec_get_subtitle_defaults(sub);
  1208. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1209. if (*got_sub_ptr)
  1210. avctx->frame_number++;
  1211. return ret;
  1212. }
  1213. void avsubtitle_free(AVSubtitle *sub)
  1214. {
  1215. int i;
  1216. for (i = 0; i < sub->num_rects; i++)
  1217. {
  1218. av_freep(&sub->rects[i]->pict.data[0]);
  1219. av_freep(&sub->rects[i]->pict.data[1]);
  1220. av_freep(&sub->rects[i]->pict.data[2]);
  1221. av_freep(&sub->rects[i]->pict.data[3]);
  1222. av_freep(&sub->rects[i]->text);
  1223. av_freep(&sub->rects[i]->ass);
  1224. av_freep(&sub->rects[i]);
  1225. }
  1226. av_freep(&sub->rects);
  1227. memset(sub, 0, sizeof(AVSubtitle));
  1228. }
  1229. av_cold int avcodec_close(AVCodecContext *avctx)
  1230. {
  1231. /* If there is a user-supplied mutex locking routine, call it. */
  1232. if (ff_lockmgr_cb) {
  1233. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1234. return -1;
  1235. }
  1236. entangled_thread_counter++;
  1237. if(entangled_thread_counter != 1){
  1238. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1239. entangled_thread_counter--;
  1240. return -1;
  1241. }
  1242. if (HAVE_THREADS && avctx->thread_opaque)
  1243. ff_thread_free(avctx);
  1244. if (avctx->codec && avctx->codec->close)
  1245. avctx->codec->close(avctx);
  1246. avcodec_default_free_buffers(avctx);
  1247. avctx->coded_frame = NULL;
  1248. av_freep(&avctx->internal);
  1249. if (avctx->codec && avctx->codec->priv_class)
  1250. av_opt_free(avctx->priv_data);
  1251. av_opt_free(avctx);
  1252. av_freep(&avctx->priv_data);
  1253. if (codec_is_encoder(avctx->codec))
  1254. av_freep(&avctx->extradata);
  1255. avctx->codec = NULL;
  1256. avctx->active_thread_type = 0;
  1257. entangled_thread_counter--;
  1258. /* Release any user-supplied mutex. */
  1259. if (ff_lockmgr_cb) {
  1260. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1261. }
  1262. return 0;
  1263. }
  1264. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1265. {
  1266. switch(id){
  1267. case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
  1268. case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
  1269. case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1270. default : return id;
  1271. }
  1272. }
  1273. AVCodec *avcodec_find_encoder(enum CodecID id)
  1274. {
  1275. AVCodec *p, *experimental=NULL;
  1276. p = first_avcodec;
  1277. id= remap_deprecated_codec_id(id);
  1278. while (p) {
  1279. if (codec_is_encoder(p) && p->id == id) {
  1280. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1281. experimental = p;
  1282. } else
  1283. return p;
  1284. }
  1285. p = p->next;
  1286. }
  1287. return experimental;
  1288. }
  1289. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1290. {
  1291. AVCodec *p;
  1292. if (!name)
  1293. return NULL;
  1294. p = first_avcodec;
  1295. while (p) {
  1296. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1297. return p;
  1298. p = p->next;
  1299. }
  1300. return NULL;
  1301. }
  1302. AVCodec *avcodec_find_decoder(enum CodecID id)
  1303. {
  1304. AVCodec *p, *experimental=NULL;
  1305. p = first_avcodec;
  1306. id= remap_deprecated_codec_id(id);
  1307. while (p) {
  1308. if (codec_is_decoder(p) && p->id == id) {
  1309. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1310. experimental = p;
  1311. } else
  1312. return p;
  1313. }
  1314. p = p->next;
  1315. }
  1316. return experimental;
  1317. }
  1318. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1319. {
  1320. AVCodec *p;
  1321. if (!name)
  1322. return NULL;
  1323. p = first_avcodec;
  1324. while (p) {
  1325. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1326. return p;
  1327. p = p->next;
  1328. }
  1329. return NULL;
  1330. }
  1331. static int get_bit_rate(AVCodecContext *ctx)
  1332. {
  1333. int bit_rate;
  1334. int bits_per_sample;
  1335. switch(ctx->codec_type) {
  1336. case AVMEDIA_TYPE_VIDEO:
  1337. case AVMEDIA_TYPE_DATA:
  1338. case AVMEDIA_TYPE_SUBTITLE:
  1339. case AVMEDIA_TYPE_ATTACHMENT:
  1340. bit_rate = ctx->bit_rate;
  1341. break;
  1342. case AVMEDIA_TYPE_AUDIO:
  1343. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1344. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1345. break;
  1346. default:
  1347. bit_rate = 0;
  1348. break;
  1349. }
  1350. return bit_rate;
  1351. }
  1352. const char *avcodec_get_name(enum CodecID id)
  1353. {
  1354. AVCodec *codec;
  1355. #if !CONFIG_SMALL
  1356. switch (id) {
  1357. #include "libavcodec/codec_names.h"
  1358. }
  1359. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1360. #endif
  1361. codec = avcodec_find_decoder(id);
  1362. if (codec)
  1363. return codec->name;
  1364. codec = avcodec_find_encoder(id);
  1365. if (codec)
  1366. return codec->name;
  1367. return "unknown_codec";
  1368. }
  1369. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1370. {
  1371. int i, len, ret = 0;
  1372. for (i = 0; i < 4; i++) {
  1373. len = snprintf(buf, buf_size,
  1374. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1375. buf += len;
  1376. buf_size = buf_size > len ? buf_size - len : 0;
  1377. ret += len;
  1378. codec_tag>>=8;
  1379. }
  1380. return ret;
  1381. }
  1382. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1383. {
  1384. const char *codec_type;
  1385. const char *codec_name;
  1386. const char *profile = NULL;
  1387. AVCodec *p;
  1388. int bitrate;
  1389. AVRational display_aspect_ratio;
  1390. if (!buf || buf_size <= 0)
  1391. return;
  1392. codec_type = av_get_media_type_string(enc->codec_type);
  1393. codec_name = avcodec_get_name(enc->codec_id);
  1394. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1395. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1396. avcodec_find_decoder(enc->codec_id);
  1397. if (p)
  1398. profile = av_get_profile_name(p, enc->profile);
  1399. }
  1400. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1401. codec_name, enc->mb_decision ? " (hq)" : "");
  1402. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1403. if (profile)
  1404. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1405. if (enc->codec_tag) {
  1406. char tag_buf[32];
  1407. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1408. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1409. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1410. }
  1411. switch(enc->codec_type) {
  1412. case AVMEDIA_TYPE_VIDEO:
  1413. if (enc->pix_fmt != PIX_FMT_NONE) {
  1414. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1415. ", %s",
  1416. av_get_pix_fmt_name(enc->pix_fmt));
  1417. }
  1418. if (enc->width) {
  1419. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1420. ", %dx%d",
  1421. enc->width, enc->height);
  1422. if (enc->sample_aspect_ratio.num) {
  1423. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1424. enc->width*enc->sample_aspect_ratio.num,
  1425. enc->height*enc->sample_aspect_ratio.den,
  1426. 1024*1024);
  1427. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1428. " [SAR %d:%d DAR %d:%d]",
  1429. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1430. display_aspect_ratio.num, display_aspect_ratio.den);
  1431. }
  1432. if(av_log_get_level() >= AV_LOG_DEBUG){
  1433. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1434. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1435. ", %d/%d",
  1436. enc->time_base.num/g, enc->time_base.den/g);
  1437. }
  1438. }
  1439. if (encode) {
  1440. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1441. ", q=%d-%d", enc->qmin, enc->qmax);
  1442. }
  1443. break;
  1444. case AVMEDIA_TYPE_AUDIO:
  1445. if (enc->sample_rate) {
  1446. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1447. ", %d Hz", enc->sample_rate);
  1448. }
  1449. av_strlcat(buf, ", ", buf_size);
  1450. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1451. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1452. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1453. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1454. }
  1455. break;
  1456. default:
  1457. return;
  1458. }
  1459. if (encode) {
  1460. if (enc->flags & CODEC_FLAG_PASS1)
  1461. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1462. ", pass 1");
  1463. if (enc->flags & CODEC_FLAG_PASS2)
  1464. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1465. ", pass 2");
  1466. }
  1467. bitrate = get_bit_rate(enc);
  1468. if (bitrate != 0) {
  1469. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1470. ", %d kb/s", bitrate / 1000);
  1471. }
  1472. }
  1473. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1474. {
  1475. const AVProfile *p;
  1476. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1477. return NULL;
  1478. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1479. if (p->profile == profile)
  1480. return p->name;
  1481. return NULL;
  1482. }
  1483. unsigned avcodec_version( void )
  1484. {
  1485. av_assert0(CODEC_ID_V410==164);
  1486. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1487. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1488. av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1489. av_assert0(CODEC_ID_SRT==94216);
  1490. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1491. return LIBAVCODEC_VERSION_INT;
  1492. }
  1493. const char *avcodec_configuration(void)
  1494. {
  1495. return FFMPEG_CONFIGURATION;
  1496. }
  1497. const char *avcodec_license(void)
  1498. {
  1499. #define LICENSE_PREFIX "libavcodec license: "
  1500. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1501. }
  1502. void avcodec_flush_buffers(AVCodecContext *avctx)
  1503. {
  1504. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1505. ff_thread_flush(avctx);
  1506. else if(avctx->codec->flush)
  1507. avctx->codec->flush(avctx);
  1508. }
  1509. static void video_free_buffers(AVCodecContext *s)
  1510. {
  1511. AVCodecInternal *avci = s->internal;
  1512. int i, j;
  1513. if (!avci->buffer)
  1514. return;
  1515. if (avci->buffer_count)
  1516. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1517. avci->buffer_count);
  1518. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1519. InternalBuffer *buf = &avci->buffer[i];
  1520. for(j=0; j<4; j++){
  1521. av_freep(&buf->base[j]);
  1522. buf->data[j]= NULL;
  1523. }
  1524. }
  1525. av_freep(&avci->buffer);
  1526. avci->buffer_count=0;
  1527. }
  1528. static void audio_free_buffers(AVCodecContext *avctx)
  1529. {
  1530. AVCodecInternal *avci = avctx->internal;
  1531. InternalBuffer *buf;
  1532. if (!avci->buffer)
  1533. return;
  1534. buf = avci->buffer;
  1535. if (buf->extended_data) {
  1536. av_free(buf->extended_data[0]);
  1537. if (buf->extended_data != buf->data)
  1538. av_freep(&buf->extended_data);
  1539. }
  1540. av_freep(&avci->buffer);
  1541. }
  1542. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1543. {
  1544. switch (avctx->codec_type) {
  1545. case AVMEDIA_TYPE_VIDEO:
  1546. video_free_buffers(avctx);
  1547. break;
  1548. case AVMEDIA_TYPE_AUDIO:
  1549. audio_free_buffers(avctx);
  1550. break;
  1551. default:
  1552. break;
  1553. }
  1554. }
  1555. #if FF_API_OLD_FF_PICT_TYPES
  1556. char av_get_pict_type_char(int pict_type){
  1557. return av_get_picture_type_char(pict_type);
  1558. }
  1559. #endif
  1560. int av_get_bits_per_sample(enum CodecID codec_id){
  1561. switch(codec_id){
  1562. case CODEC_ID_ADPCM_SBPRO_2:
  1563. return 2;
  1564. case CODEC_ID_ADPCM_SBPRO_3:
  1565. return 3;
  1566. case CODEC_ID_ADPCM_SBPRO_4:
  1567. case CODEC_ID_ADPCM_CT:
  1568. case CODEC_ID_ADPCM_IMA_WAV:
  1569. case CODEC_ID_ADPCM_IMA_QT:
  1570. case CODEC_ID_ADPCM_SWF:
  1571. case CODEC_ID_ADPCM_MS:
  1572. case CODEC_ID_ADPCM_YAMAHA:
  1573. case CODEC_ID_ADPCM_G722:
  1574. return 4;
  1575. case CODEC_ID_PCM_ALAW:
  1576. case CODEC_ID_PCM_MULAW:
  1577. case CODEC_ID_PCM_S8:
  1578. case CODEC_ID_PCM_U8:
  1579. case CODEC_ID_PCM_ZORK:
  1580. return 8;
  1581. case CODEC_ID_PCM_S16BE:
  1582. case CODEC_ID_PCM_S16LE:
  1583. case CODEC_ID_PCM_S16LE_PLANAR:
  1584. case CODEC_ID_PCM_U16BE:
  1585. case CODEC_ID_PCM_U16LE:
  1586. return 16;
  1587. case CODEC_ID_PCM_S24DAUD:
  1588. case CODEC_ID_PCM_S24BE:
  1589. case CODEC_ID_PCM_S24LE:
  1590. case CODEC_ID_PCM_U24BE:
  1591. case CODEC_ID_PCM_U24LE:
  1592. return 24;
  1593. case CODEC_ID_PCM_S32BE:
  1594. case CODEC_ID_PCM_S32LE:
  1595. case CODEC_ID_PCM_U32BE:
  1596. case CODEC_ID_PCM_U32LE:
  1597. case CODEC_ID_PCM_F32BE:
  1598. case CODEC_ID_PCM_F32LE:
  1599. return 32;
  1600. case CODEC_ID_PCM_F64BE:
  1601. case CODEC_ID_PCM_F64LE:
  1602. return 64;
  1603. default:
  1604. return 0;
  1605. }
  1606. }
  1607. #if FF_API_OLD_SAMPLE_FMT
  1608. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1609. return av_get_bytes_per_sample(sample_fmt) << 3;
  1610. }
  1611. #endif
  1612. #if !HAVE_THREADS
  1613. int ff_thread_init(AVCodecContext *s){
  1614. return -1;
  1615. }
  1616. #endif
  1617. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1618. {
  1619. unsigned int n = 0;
  1620. while(v >= 0xff) {
  1621. *s++ = 0xff;
  1622. v -= 0xff;
  1623. n++;
  1624. }
  1625. *s = v;
  1626. n++;
  1627. return n;
  1628. }
  1629. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1630. int i;
  1631. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1632. return i;
  1633. }
  1634. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1635. {
  1636. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1637. "version to the newest one from Git. If the problem still "
  1638. "occurs, it means that your file has a feature which has not "
  1639. "been implemented.\n", feature);
  1640. if(want_sample)
  1641. av_log_ask_for_sample(avc, NULL);
  1642. }
  1643. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1644. {
  1645. va_list argument_list;
  1646. va_start(argument_list, msg);
  1647. if (msg)
  1648. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1649. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1650. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1651. "and contact the ffmpeg-devel mailing list.\n");
  1652. va_end(argument_list);
  1653. }
  1654. static AVHWAccel *first_hwaccel = NULL;
  1655. void av_register_hwaccel(AVHWAccel *hwaccel)
  1656. {
  1657. AVHWAccel **p = &first_hwaccel;
  1658. while (*p)
  1659. p = &(*p)->next;
  1660. *p = hwaccel;
  1661. hwaccel->next = NULL;
  1662. }
  1663. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1664. {
  1665. return hwaccel ? hwaccel->next : first_hwaccel;
  1666. }
  1667. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1668. {
  1669. AVHWAccel *hwaccel=NULL;
  1670. while((hwaccel= av_hwaccel_next(hwaccel))){
  1671. if ( hwaccel->id == codec_id
  1672. && hwaccel->pix_fmt == pix_fmt)
  1673. return hwaccel;
  1674. }
  1675. return NULL;
  1676. }
  1677. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1678. {
  1679. if (ff_lockmgr_cb) {
  1680. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1681. return -1;
  1682. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1683. return -1;
  1684. }
  1685. ff_lockmgr_cb = cb;
  1686. if (ff_lockmgr_cb) {
  1687. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1688. return -1;
  1689. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1690. return -1;
  1691. }
  1692. return 0;
  1693. }
  1694. int avpriv_lock_avformat(void)
  1695. {
  1696. if (ff_lockmgr_cb) {
  1697. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1698. return -1;
  1699. }
  1700. return 0;
  1701. }
  1702. int avpriv_unlock_avformat(void)
  1703. {
  1704. if (ff_lockmgr_cb) {
  1705. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1706. return -1;
  1707. }
  1708. return 0;
  1709. }
  1710. unsigned int avpriv_toupper4(unsigned int x)
  1711. {
  1712. return toupper( x &0xFF)
  1713. + (toupper((x>>8 )&0xFF)<<8 )
  1714. + (toupper((x>>16)&0xFF)<<16)
  1715. + (toupper((x>>24)&0xFF)<<24);
  1716. }
  1717. #if !HAVE_THREADS
  1718. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1719. {
  1720. f->owner = avctx;
  1721. ff_init_buffer_info(avctx, f);
  1722. return avctx->get_buffer(avctx, f);
  1723. }
  1724. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1725. {
  1726. f->owner->release_buffer(f->owner, f);
  1727. }
  1728. void ff_thread_finish_setup(AVCodecContext *avctx)
  1729. {
  1730. }
  1731. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1732. {
  1733. }
  1734. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1735. {
  1736. }
  1737. #endif
  1738. #if FF_API_THREAD_INIT
  1739. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1740. {
  1741. s->thread_count = thread_count;
  1742. return ff_thread_init(s);
  1743. }
  1744. #endif
  1745. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1746. {
  1747. AVCodec *c= avcodec_find_decoder(codec_id);
  1748. if(!c)
  1749. c= avcodec_find_encoder(codec_id);
  1750. if(c)
  1751. return c->type;
  1752. if (codec_id <= CODEC_ID_NONE)
  1753. return AVMEDIA_TYPE_UNKNOWN;
  1754. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1755. return AVMEDIA_TYPE_VIDEO;
  1756. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1757. return AVMEDIA_TYPE_AUDIO;
  1758. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1759. return AVMEDIA_TYPE_SUBTITLE;
  1760. return AVMEDIA_TYPE_UNKNOWN;
  1761. }