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.

1694 lines
51KB

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