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.

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