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.

1702 lines
51KB

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