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.

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