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.

1766 lines
53KB

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