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.

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