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.

1995 lines
61KB

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