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.

2366 lines
74KB

  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/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "imgconvert.h"
  40. #include "thread.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter=0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if(min_size < *size)
  55. return ptr;
  56. min_size= FFMAX(17*min_size/16 + 32, min_size);
  57. ptr= av_realloc(ptr, min_size);
  58. 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
  59. min_size= 0;
  60. *size= min_size;
  61. return ptr;
  62. }
  63. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  64. {
  65. void **p = ptr;
  66. if (min_size < *size)
  67. return 0;
  68. min_size= FFMAX(17*min_size/16 + 32, min_size);
  69. av_free(*p);
  70. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  71. if (!*p) min_size = 0;
  72. *size= min_size;
  73. return 1;
  74. }
  75. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  76. {
  77. ff_fast_malloc(ptr, size, min_size, 0);
  78. }
  79. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. uint8_t **p = ptr;
  82. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  83. av_freep(p);
  84. *size = 0;
  85. return;
  86. }
  87. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  88. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(AVCodec *c){
  93. if(c) return c->next;
  94. else return first_avcodec;
  95. }
  96. static void avcodec_init(void)
  97. {
  98. static int initialized = 0;
  99. if (initialized != 0)
  100. return;
  101. initialized = 1;
  102. ff_dsputil_static_init();
  103. }
  104. int av_codec_is_encoder(AVCodec *codec)
  105. {
  106. return codec && (codec->encode || codec->encode2);
  107. }
  108. int av_codec_is_decoder(AVCodec *codec)
  109. {
  110. return codec && codec->decode;
  111. }
  112. void avcodec_register(AVCodec *codec)
  113. {
  114. AVCodec **p;
  115. avcodec_init();
  116. p = &first_avcodec;
  117. while (*p != NULL) p = &(*p)->next;
  118. *p = codec;
  119. codec->next = NULL;
  120. if (codec->init_static_data)
  121. codec->init_static_data(codec);
  122. }
  123. unsigned avcodec_get_edge_width(void)
  124. {
  125. return EDGE_WIDTH;
  126. }
  127. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  128. s->coded_width = width;
  129. s->coded_height= height;
  130. s->width = -((-width )>>s->lowres);
  131. s->height= -((-height)>>s->lowres);
  132. }
  133. #define INTERNAL_BUFFER_SIZE (32+1)
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align= 1;
  139. int h_align= 1;
  140. switch(s->pix_fmt){
  141. case PIX_FMT_YUV420P:
  142. case PIX_FMT_YUYV422:
  143. case PIX_FMT_UYVY422:
  144. case PIX_FMT_YUV422P:
  145. case PIX_FMT_YUV440P:
  146. case PIX_FMT_YUV444P:
  147. case PIX_FMT_GBRP:
  148. case PIX_FMT_GRAY8:
  149. case PIX_FMT_GRAY16BE:
  150. case PIX_FMT_GRAY16LE:
  151. case PIX_FMT_YUVJ420P:
  152. case PIX_FMT_YUVJ422P:
  153. case PIX_FMT_YUVJ440P:
  154. case PIX_FMT_YUVJ444P:
  155. case PIX_FMT_YUVA420P:
  156. case PIX_FMT_YUVA444P:
  157. case PIX_FMT_YUV420P9LE:
  158. case PIX_FMT_YUV420P9BE:
  159. case PIX_FMT_YUV420P10LE:
  160. case PIX_FMT_YUV420P10BE:
  161. case PIX_FMT_YUV422P9LE:
  162. case PIX_FMT_YUV422P9BE:
  163. case PIX_FMT_YUV422P10LE:
  164. case PIX_FMT_YUV422P10BE:
  165. case PIX_FMT_YUV444P9LE:
  166. case PIX_FMT_YUV444P9BE:
  167. case PIX_FMT_YUV444P10LE:
  168. case PIX_FMT_YUV444P10BE:
  169. case PIX_FMT_GBRP9LE:
  170. case PIX_FMT_GBRP9BE:
  171. case PIX_FMT_GBRP10LE:
  172. case PIX_FMT_GBRP10BE:
  173. w_align = 16; //FIXME assume 16 pixel per macroblock
  174. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  175. break;
  176. case PIX_FMT_YUV411P:
  177. case PIX_FMT_UYYVYY411:
  178. w_align=32;
  179. h_align=8;
  180. break;
  181. case PIX_FMT_YUV410P:
  182. if(s->codec_id == CODEC_ID_SVQ1){
  183. w_align=64;
  184. h_align=64;
  185. }
  186. case PIX_FMT_RGB555:
  187. if(s->codec_id == CODEC_ID_RPZA){
  188. w_align=4;
  189. h_align=4;
  190. }
  191. case PIX_FMT_PAL8:
  192. case PIX_FMT_BGR8:
  193. case PIX_FMT_RGB8:
  194. if(s->codec_id == CODEC_ID_SMC){
  195. w_align=4;
  196. h_align=4;
  197. }
  198. break;
  199. case PIX_FMT_BGR24:
  200. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  201. w_align=4;
  202. h_align=4;
  203. }
  204. break;
  205. default:
  206. w_align= 1;
  207. h_align= 1;
  208. break;
  209. }
  210. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  211. w_align= FFMAX(w_align, 8);
  212. }
  213. *width = FFALIGN(*width , w_align);
  214. *height= FFALIGN(*height, h_align);
  215. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  216. *height+=2; // some of the optimized chroma MC reads one line too much
  217. // which is also done in mpeg decoders with lowres > 0
  218. for (i = 0; i < 4; i++)
  219. linesize_align[i] = STRIDE_ALIGN;
  220. }
  221. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  222. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  223. int linesize_align[AV_NUM_DATA_POINTERS];
  224. int align;
  225. avcodec_align_dimensions2(s, width, height, linesize_align);
  226. align = FFMAX(linesize_align[0], linesize_align[3]);
  227. linesize_align[1] <<= chroma_shift;
  228. linesize_align[2] <<= chroma_shift;
  229. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  230. *width=FFALIGN(*width, align);
  231. }
  232. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  233. {
  234. if (s->pkt) {
  235. pic->pkt_pts = s->pkt->pts;
  236. pic->pkt_pos = s->pkt->pos;
  237. } else {
  238. pic->pkt_pts = AV_NOPTS_VALUE;
  239. pic->pkt_pos = -1;
  240. }
  241. pic->reordered_opaque= s->reordered_opaque;
  242. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  243. pic->width = s->width;
  244. pic->height = s->height;
  245. pic->format = s->pix_fmt;
  246. }
  247. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  248. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  249. int buf_size, int align)
  250. {
  251. int ch, planar, needed_size, ret = 0;
  252. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  253. frame->nb_samples, sample_fmt,
  254. align);
  255. if (buf_size < needed_size)
  256. return AVERROR(EINVAL);
  257. planar = av_sample_fmt_is_planar(sample_fmt);
  258. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  259. if (!(frame->extended_data = av_mallocz(nb_channels *
  260. sizeof(*frame->extended_data))))
  261. return AVERROR(ENOMEM);
  262. } else {
  263. frame->extended_data = frame->data;
  264. }
  265. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  266. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  267. sample_fmt, align)) < 0) {
  268. if (frame->extended_data != frame->data)
  269. av_freep(&frame->extended_data);
  270. return ret;
  271. }
  272. if (frame->extended_data != frame->data) {
  273. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  274. frame->data[ch] = frame->extended_data[ch];
  275. }
  276. return ret;
  277. }
  278. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  279. {
  280. AVCodecInternal *avci = avctx->internal;
  281. InternalBuffer *buf;
  282. int buf_size, ret;
  283. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  284. frame->nb_samples, avctx->sample_fmt,
  285. 0);
  286. if (buf_size < 0)
  287. return AVERROR(EINVAL);
  288. /* allocate InternalBuffer if needed */
  289. if (!avci->buffer) {
  290. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  291. if (!avci->buffer)
  292. return AVERROR(ENOMEM);
  293. }
  294. buf = avci->buffer;
  295. /* if there is a previously-used internal buffer, check its size and
  296. channel count to see if we can reuse it */
  297. if (buf->extended_data) {
  298. /* if current buffer is too small, free it */
  299. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  300. av_free(buf->extended_data[0]);
  301. if (buf->extended_data != buf->data)
  302. av_freep(&buf->extended_data);
  303. buf->extended_data = NULL;
  304. buf->data[0] = NULL;
  305. }
  306. /* if number of channels has changed, reset and/or free extended data
  307. pointers but leave data buffer in buf->data[0] for reuse */
  308. if (buf->nb_channels != avctx->channels) {
  309. if (buf->extended_data != buf->data)
  310. av_free(buf->extended_data);
  311. buf->extended_data = NULL;
  312. }
  313. }
  314. /* if there is no previous buffer or the previous buffer cannot be used
  315. as-is, allocate a new buffer and/or rearrange the channel pointers */
  316. if (!buf->extended_data) {
  317. if (!buf->data[0]) {
  318. if (!(buf->data[0] = av_mallocz(buf_size)))
  319. return AVERROR(ENOMEM);
  320. buf->audio_data_size = buf_size;
  321. }
  322. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  323. avctx->sample_fmt, buf->data[0],
  324. buf->audio_data_size, 0)))
  325. return ret;
  326. if (frame->extended_data == frame->data)
  327. buf->extended_data = buf->data;
  328. else
  329. buf->extended_data = frame->extended_data;
  330. memcpy(buf->data, frame->data, sizeof(frame->data));
  331. buf->linesize[0] = frame->linesize[0];
  332. buf->nb_channels = avctx->channels;
  333. } else {
  334. /* copy InternalBuffer info to the AVFrame */
  335. frame->extended_data = buf->extended_data;
  336. frame->linesize[0] = buf->linesize[0];
  337. memcpy(frame->data, buf->data, sizeof(frame->data));
  338. }
  339. frame->type = FF_BUFFER_TYPE_INTERNAL;
  340. if (avctx->pkt) {
  341. frame->pkt_pts = avctx->pkt->pts;
  342. frame->pkt_pos = avctx->pkt->pos;
  343. } else {
  344. frame->pkt_pts = AV_NOPTS_VALUE;
  345. frame->pkt_pos = -1;
  346. }
  347. frame->reordered_opaque = avctx->reordered_opaque;
  348. if (avctx->debug & FF_DEBUG_BUFFERS)
  349. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  350. "internal audio buffer used\n", frame);
  351. return 0;
  352. }
  353. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  354. {
  355. int i;
  356. int w= s->width;
  357. int h= s->height;
  358. InternalBuffer *buf;
  359. AVCodecInternal *avci = s->internal;
  360. if(pic->data[0]!=NULL) {
  361. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  362. return -1;
  363. }
  364. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  365. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  366. return -1;
  367. }
  368. if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
  369. return -1;
  370. if (!avci->buffer) {
  371. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  372. sizeof(InternalBuffer));
  373. }
  374. buf = &avci->buffer[avci->buffer_count];
  375. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  376. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  377. av_freep(&buf->base[i]);
  378. buf->data[i]= NULL;
  379. }
  380. }
  381. if (!buf->base[0]) {
  382. int h_chroma_shift, v_chroma_shift;
  383. int size[4] = {0};
  384. int tmpsize;
  385. int unaligned;
  386. AVPicture picture;
  387. int stride_align[AV_NUM_DATA_POINTERS];
  388. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  389. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  390. avcodec_align_dimensions2(s, &w, &h, stride_align);
  391. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  392. w+= EDGE_WIDTH*2;
  393. h+= EDGE_WIDTH*2;
  394. }
  395. do {
  396. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  397. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  398. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  399. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  400. w += w & ~(w-1);
  401. unaligned = 0;
  402. for (i=0; i<4; i++){
  403. unaligned |= picture.linesize[i] % stride_align[i];
  404. }
  405. } while (unaligned);
  406. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  407. if (tmpsize < 0)
  408. return -1;
  409. for (i=0; i<3 && picture.data[i+1]; i++)
  410. size[i] = picture.data[i+1] - picture.data[i];
  411. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  412. memset(buf->base, 0, sizeof(buf->base));
  413. memset(buf->data, 0, sizeof(buf->data));
  414. for(i=0; i<4 && size[i]; i++){
  415. const int h_shift= i==0 ? 0 : h_chroma_shift;
  416. const int v_shift= i==0 ? 0 : v_chroma_shift;
  417. buf->linesize[i]= picture.linesize[i];
  418. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  419. if(buf->base[i]==NULL) return -1;
  420. memset(buf->base[i], 128, size[i]);
  421. // no edge if EDGE EMU or not planar YUV
  422. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  423. buf->data[i] = buf->base[i];
  424. else
  425. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  426. }
  427. for (; i < AV_NUM_DATA_POINTERS; i++) {
  428. buf->base[i] = buf->data[i] = NULL;
  429. buf->linesize[i] = 0;
  430. }
  431. if(size[1] && !size[2])
  432. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  433. buf->width = s->width;
  434. buf->height = s->height;
  435. buf->pix_fmt= s->pix_fmt;
  436. }
  437. pic->type= FF_BUFFER_TYPE_INTERNAL;
  438. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  439. pic->base[i]= buf->base[i];
  440. pic->data[i]= buf->data[i];
  441. pic->linesize[i]= buf->linesize[i];
  442. }
  443. pic->extended_data = pic->data;
  444. avci->buffer_count++;
  445. pic->width = buf->width;
  446. pic->height = buf->height;
  447. pic->format = buf->pix_fmt;
  448. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  449. if (s->pkt) {
  450. pic->pkt_pts = s->pkt->pts;
  451. pic->pkt_pos = s->pkt->pos;
  452. } else {
  453. pic->pkt_pts = AV_NOPTS_VALUE;
  454. pic->pkt_pos = -1;
  455. }
  456. pic->reordered_opaque= s->reordered_opaque;
  457. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  458. pic->width = s->width;
  459. pic->height = s->height;
  460. pic->format = s->pix_fmt;
  461. if(s->debug&FF_DEBUG_BUFFERS)
  462. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  463. "buffers used\n", pic, avci->buffer_count);
  464. return 0;
  465. }
  466. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  467. {
  468. switch (avctx->codec_type) {
  469. case AVMEDIA_TYPE_VIDEO:
  470. return video_get_buffer(avctx, frame);
  471. case AVMEDIA_TYPE_AUDIO:
  472. return audio_get_buffer(avctx, frame);
  473. default:
  474. return -1;
  475. }
  476. }
  477. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  478. int i;
  479. InternalBuffer *buf, *last;
  480. AVCodecInternal *avci = s->internal;
  481. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  482. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  483. assert(avci->buffer_count);
  484. if (avci->buffer) {
  485. buf = NULL; /* avoids warning */
  486. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  487. buf = &avci->buffer[i];
  488. if (buf->data[0] == pic->data[0])
  489. break;
  490. }
  491. assert(i < avci->buffer_count);
  492. avci->buffer_count--;
  493. last = &avci->buffer[avci->buffer_count];
  494. if (buf != last)
  495. FFSWAP(InternalBuffer, *buf, *last);
  496. }
  497. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  498. pic->data[i]=NULL;
  499. // pic->base[i]=NULL;
  500. }
  501. //printf("R%X\n", pic->opaque);
  502. if(s->debug&FF_DEBUG_BUFFERS)
  503. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  504. "buffers used\n", pic, avci->buffer_count);
  505. }
  506. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  507. AVFrame temp_pic;
  508. int i;
  509. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  510. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  511. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  512. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  513. s->release_buffer(s, pic);
  514. }
  515. ff_init_buffer_info(s, pic);
  516. /* If no picture return a new buffer */
  517. if(pic->data[0] == NULL) {
  518. /* We will copy from buffer, so must be readable */
  519. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  520. return s->get_buffer(s, pic);
  521. }
  522. assert(s->pix_fmt == pic->format);
  523. /* If internal buffer type return the same buffer */
  524. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  525. return 0;
  526. }
  527. /*
  528. * Not internal type and reget_buffer not overridden, emulate cr buffer
  529. */
  530. temp_pic = *pic;
  531. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  532. pic->data[i] = pic->base[i] = NULL;
  533. pic->opaque = NULL;
  534. /* Allocate new frame */
  535. if (s->get_buffer(s, pic))
  536. return -1;
  537. /* Copy image data from old buffer to new buffer */
  538. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  539. s->height);
  540. s->release_buffer(s, &temp_pic); // Release old frame
  541. return 0;
  542. }
  543. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  544. int i;
  545. for(i=0; i<count; i++){
  546. int r= func(c, (char*)arg + i*size);
  547. if(ret) ret[i]= r;
  548. }
  549. return 0;
  550. }
  551. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  552. int i;
  553. for(i=0; i<count; i++){
  554. int r= func(c, arg, i, 0);
  555. if(ret) ret[i]= r;
  556. }
  557. return 0;
  558. }
  559. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  560. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  561. ++fmt;
  562. return fmt[0];
  563. }
  564. void avcodec_get_frame_defaults(AVFrame *pic){
  565. memset(pic, 0, sizeof(AVFrame));
  566. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  567. pic->pkt_pos = -1;
  568. pic->key_frame= 1;
  569. pic->sample_aspect_ratio = (AVRational){0, 1};
  570. pic->format = -1; /* unknown */
  571. }
  572. AVFrame *avcodec_alloc_frame(void){
  573. AVFrame *pic= av_malloc(sizeof(AVFrame));
  574. if(pic==NULL) return NULL;
  575. avcodec_get_frame_defaults(pic);
  576. return pic;
  577. }
  578. #define MAKE_ACCESSORS(str, name, type, field) \
  579. type av_##name##_get_##field(const str *s) { return s->field; } \
  580. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  581. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  582. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  583. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  584. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  585. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  586. {
  587. memset(sub, 0, sizeof(*sub));
  588. sub->pts = AV_NOPTS_VALUE;
  589. }
  590. static int get_bit_rate(AVCodecContext *ctx)
  591. {
  592. int bit_rate;
  593. int bits_per_sample;
  594. switch(ctx->codec_type) {
  595. case AVMEDIA_TYPE_VIDEO:
  596. case AVMEDIA_TYPE_DATA:
  597. case AVMEDIA_TYPE_SUBTITLE:
  598. case AVMEDIA_TYPE_ATTACHMENT:
  599. bit_rate = ctx->bit_rate;
  600. break;
  601. case AVMEDIA_TYPE_AUDIO:
  602. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  603. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  604. break;
  605. default:
  606. bit_rate = 0;
  607. break;
  608. }
  609. return bit_rate;
  610. }
  611. #if FF_API_AVCODEC_OPEN
  612. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  613. {
  614. return avcodec_open2(avctx, codec, NULL);
  615. }
  616. #endif
  617. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  618. {
  619. int ret = 0;
  620. AVDictionary *tmp = NULL;
  621. if (avcodec_is_open(avctx))
  622. return 0;
  623. if ((!codec && !avctx->codec)) {
  624. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  625. return AVERROR(EINVAL);
  626. }
  627. if ((codec && avctx->codec && codec != avctx->codec)) {
  628. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  629. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  630. return AVERROR(EINVAL);
  631. }
  632. if (!codec)
  633. codec = avctx->codec;
  634. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  635. return AVERROR(EINVAL);
  636. if (options)
  637. av_dict_copy(&tmp, *options, 0);
  638. /* If there is a user-supplied mutex locking routine, call it. */
  639. if (ff_lockmgr_cb) {
  640. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  641. return -1;
  642. }
  643. entangled_thread_counter++;
  644. if(entangled_thread_counter != 1){
  645. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  646. ret = -1;
  647. goto end;
  648. }
  649. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  650. if (!avctx->internal) {
  651. ret = AVERROR(ENOMEM);
  652. goto end;
  653. }
  654. if (codec->priv_data_size > 0) {
  655. if(!avctx->priv_data){
  656. avctx->priv_data = av_mallocz(codec->priv_data_size);
  657. if (!avctx->priv_data) {
  658. ret = AVERROR(ENOMEM);
  659. goto end;
  660. }
  661. if (codec->priv_class) {
  662. *(const AVClass**)avctx->priv_data= codec->priv_class;
  663. av_opt_set_defaults(avctx->priv_data);
  664. }
  665. }
  666. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  667. goto free_and_end;
  668. } else {
  669. avctx->priv_data = NULL;
  670. }
  671. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  672. goto free_and_end;
  673. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  674. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  675. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
  676. ret = -1;
  677. goto free_and_end;
  678. }
  679. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  680. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  681. if(avctx->coded_width && avctx->coded_height)
  682. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  683. else if(avctx->width && avctx->height)
  684. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  685. }
  686. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  687. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  688. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  689. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  690. avcodec_set_dimensions(avctx, 0, 0);
  691. }
  692. /* if the decoder init function was already called previously,
  693. free the already allocated subtitle_header before overwriting it */
  694. if (av_codec_is_decoder(codec))
  695. av_freep(&avctx->subtitle_header);
  696. #define SANE_NB_CHANNELS 128U
  697. if (avctx->channels > SANE_NB_CHANNELS) {
  698. ret = AVERROR(EINVAL);
  699. goto free_and_end;
  700. }
  701. avctx->codec = codec;
  702. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  703. avctx->codec_id == CODEC_ID_NONE) {
  704. avctx->codec_type = codec->type;
  705. avctx->codec_id = codec->id;
  706. }
  707. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  708. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  709. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  710. ret = AVERROR(EINVAL);
  711. goto free_and_end;
  712. }
  713. avctx->frame_number = 0;
  714. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  715. (!avctx->time_base.num || !avctx->time_base.den)) {
  716. avctx->time_base.num = 1;
  717. avctx->time_base.den = avctx->sample_rate;
  718. }
  719. if (!HAVE_THREADS)
  720. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  721. if (HAVE_THREADS && !avctx->thread_opaque) {
  722. ret = ff_thread_init(avctx);
  723. if (ret < 0) {
  724. goto free_and_end;
  725. }
  726. }
  727. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  728. avctx->thread_count = 1;
  729. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  730. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  731. avctx->codec->max_lowres);
  732. ret = AVERROR(EINVAL);
  733. goto free_and_end;
  734. }
  735. if (av_codec_is_encoder(avctx->codec)) {
  736. int i;
  737. if (avctx->codec->sample_fmts) {
  738. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  739. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  740. break;
  741. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  742. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  743. ret = AVERROR(EINVAL);
  744. goto free_and_end;
  745. }
  746. }
  747. if (avctx->codec->pix_fmts) {
  748. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  749. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  750. break;
  751. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
  752. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  753. ret = AVERROR(EINVAL);
  754. goto free_and_end;
  755. }
  756. }
  757. if (avctx->codec->supported_samplerates) {
  758. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  759. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  760. break;
  761. if (avctx->codec->supported_samplerates[i] == 0) {
  762. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  763. ret = AVERROR(EINVAL);
  764. goto free_and_end;
  765. }
  766. }
  767. if (avctx->codec->channel_layouts) {
  768. if (!avctx->channel_layout) {
  769. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  770. } else {
  771. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  772. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  773. break;
  774. if (avctx->codec->channel_layouts[i] == 0) {
  775. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  776. ret = AVERROR(EINVAL);
  777. goto free_and_end;
  778. }
  779. }
  780. }
  781. if (avctx->channel_layout && avctx->channels) {
  782. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  783. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  784. ret = AVERROR(EINVAL);
  785. goto free_and_end;
  786. }
  787. } else if (avctx->channel_layout) {
  788. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  789. }
  790. }
  791. avctx->pts_correction_num_faulty_pts =
  792. avctx->pts_correction_num_faulty_dts = 0;
  793. avctx->pts_correction_last_pts =
  794. avctx->pts_correction_last_dts = INT64_MIN;
  795. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  796. ret = avctx->codec->init(avctx);
  797. if (ret < 0) {
  798. goto free_and_end;
  799. }
  800. }
  801. ret=0;
  802. if (av_codec_is_decoder(avctx->codec)) {
  803. if (!avctx->bit_rate)
  804. avctx->bit_rate = get_bit_rate(avctx);
  805. /* validate channel layout from the decoder */
  806. if (avctx->channel_layout &&
  807. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  808. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  809. avctx->channel_layout = 0;
  810. }
  811. }
  812. end:
  813. entangled_thread_counter--;
  814. /* Release any user-supplied mutex. */
  815. if (ff_lockmgr_cb) {
  816. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  817. }
  818. if (options) {
  819. av_dict_free(options);
  820. *options = tmp;
  821. }
  822. return ret;
  823. free_and_end:
  824. av_dict_free(&tmp);
  825. av_freep(&avctx->priv_data);
  826. av_freep(&avctx->internal);
  827. avctx->codec= NULL;
  828. goto end;
  829. }
  830. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  831. {
  832. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  833. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  834. return AVERROR(EINVAL);
  835. }
  836. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  837. if (!avpkt->data || avpkt->size < size) {
  838. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  839. avpkt->data = avctx->internal->byte_buffer;
  840. avpkt->size = avctx->internal->byte_buffer_size;
  841. avpkt->destruct = NULL;
  842. }
  843. if (avpkt->data) {
  844. void *destruct = avpkt->destruct;
  845. if (avpkt->size < size) {
  846. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  847. return AVERROR(EINVAL);
  848. }
  849. av_init_packet(avpkt);
  850. avpkt->destruct = destruct;
  851. avpkt->size = size;
  852. return 0;
  853. } else {
  854. int ret = av_new_packet(avpkt, size);
  855. if (ret < 0)
  856. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  857. return ret;
  858. }
  859. }
  860. int ff_alloc_packet(AVPacket *avpkt, int size)
  861. {
  862. return ff_alloc_packet2(NULL, avpkt, size);
  863. }
  864. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  865. AVPacket *avpkt,
  866. const AVFrame *frame,
  867. int *got_packet_ptr)
  868. {
  869. int ret;
  870. AVPacket user_pkt = *avpkt;
  871. int nb_samples;
  872. int needs_realloc = !user_pkt.data;
  873. *got_packet_ptr = 0;
  874. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  875. av_free_packet(avpkt);
  876. av_init_packet(avpkt);
  877. avpkt->size = 0;
  878. return 0;
  879. }
  880. /* check for valid frame size */
  881. if (frame) {
  882. nb_samples = frame->nb_samples;
  883. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  884. if (nb_samples > avctx->frame_size)
  885. return AVERROR(EINVAL);
  886. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  887. if (nb_samples != avctx->frame_size)
  888. return AVERROR(EINVAL);
  889. }
  890. } else {
  891. nb_samples = avctx->frame_size;
  892. }
  893. if (avctx->codec->encode2) {
  894. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  895. if (!ret && *got_packet_ptr) {
  896. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  897. if (avpkt->pts == AV_NOPTS_VALUE)
  898. avpkt->pts = frame->pts;
  899. if (!avpkt->duration)
  900. avpkt->duration = ff_samples_to_time_base(avctx,
  901. frame->nb_samples);
  902. }
  903. avpkt->dts = avpkt->pts;
  904. } else {
  905. avpkt->size = 0;
  906. }
  907. } else {
  908. /* for compatibility with encoders not supporting encode2(), we need to
  909. allocate a packet buffer if the user has not provided one or check
  910. the size otherwise */
  911. int fs_tmp = 0;
  912. int buf_size = avpkt->size;
  913. if (!user_pkt.data) {
  914. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  915. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  916. if (!frame)
  917. return AVERROR(EINVAL);
  918. buf_size = nb_samples * avctx->channels *
  919. av_get_bits_per_sample(avctx->codec_id) / 8;
  920. } else {
  921. /* this is a guess as to the required size.
  922. if an encoder needs more than this, it should probably
  923. implement encode2() */
  924. buf_size = 2 * avctx->frame_size * avctx->channels *
  925. av_get_bytes_per_sample(avctx->sample_fmt);
  926. buf_size += 2*FF_MIN_BUFFER_SIZE;
  927. }
  928. }
  929. if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)))
  930. return ret;
  931. /* Encoders using AVCodec.encode() that support
  932. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  933. the smaller size when encoding the last frame.
  934. This code can be removed once all encoders supporting
  935. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  936. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  937. nb_samples < avctx->frame_size) {
  938. fs_tmp = avctx->frame_size;
  939. avctx->frame_size = nb_samples;
  940. }
  941. /* encode the frame */
  942. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  943. frame ? frame->data[0] : NULL);
  944. if (ret >= 0) {
  945. if (!ret) {
  946. /* no output. if the packet data was allocated by libavcodec,
  947. free it */
  948. if (!user_pkt.data && avpkt->data != avctx->internal->byte_buffer)
  949. av_freep(&avpkt->data);
  950. } else {
  951. if (avctx->coded_frame)
  952. avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
  953. /* Set duration for final small packet. This can be removed
  954. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  955. encode2() */
  956. if (fs_tmp) {
  957. avpkt->duration = ff_samples_to_time_base(avctx,
  958. avctx->frame_size);
  959. }
  960. }
  961. avpkt->size = ret;
  962. *got_packet_ptr = (ret > 0);
  963. ret = 0;
  964. }
  965. if (fs_tmp)
  966. avctx->frame_size = fs_tmp;
  967. }
  968. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  969. needs_realloc = 0;
  970. if (user_pkt.data) {
  971. if (user_pkt.size >= avpkt->size) {
  972. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  973. } else {
  974. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  975. avpkt->size = user_pkt.size;
  976. ret = -1;
  977. }
  978. avpkt->data = user_pkt.data;
  979. avpkt->destruct = user_pkt.destruct;
  980. } else {
  981. if (av_dup_packet(avpkt) < 0) {
  982. ret = AVERROR(ENOMEM);
  983. }
  984. }
  985. }
  986. if (!ret) {
  987. if (needs_realloc && avpkt->data) {
  988. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  989. if (new_data)
  990. avpkt->data = new_data;
  991. }
  992. avctx->frame_number++;
  993. }
  994. if (ret < 0 || !*got_packet_ptr)
  995. av_free_packet(avpkt);
  996. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  997. this needs to be moved to the encoders, but for now we can do it
  998. here to simplify things */
  999. avpkt->flags |= AV_PKT_FLAG_KEY;
  1000. return ret;
  1001. }
  1002. #if FF_API_OLD_DECODE_AUDIO
  1003. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1004. uint8_t *buf, int buf_size,
  1005. const short *samples)
  1006. {
  1007. AVPacket pkt;
  1008. AVFrame frame0;
  1009. AVFrame *frame;
  1010. int ret, samples_size, got_packet;
  1011. av_init_packet(&pkt);
  1012. pkt.data = buf;
  1013. pkt.size = buf_size;
  1014. if (samples) {
  1015. frame = &frame0;
  1016. avcodec_get_frame_defaults(frame);
  1017. if (avctx->frame_size) {
  1018. frame->nb_samples = avctx->frame_size;
  1019. } else {
  1020. /* if frame_size is not set, the number of samples must be
  1021. calculated from the buffer size */
  1022. int64_t nb_samples;
  1023. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1024. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1025. "support this codec\n");
  1026. return AVERROR(EINVAL);
  1027. }
  1028. nb_samples = (int64_t)buf_size * 8 /
  1029. (av_get_bits_per_sample(avctx->codec_id) *
  1030. avctx->channels);
  1031. if (nb_samples >= INT_MAX)
  1032. return AVERROR(EINVAL);
  1033. frame->nb_samples = nb_samples;
  1034. }
  1035. /* it is assumed that the samples buffer is large enough based on the
  1036. relevant parameters */
  1037. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1038. frame->nb_samples,
  1039. avctx->sample_fmt, 1);
  1040. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1041. avctx->sample_fmt,
  1042. (const uint8_t *)samples, samples_size, 1)))
  1043. return ret;
  1044. /* fabricate frame pts from sample count.
  1045. this is needed because the avcodec_encode_audio() API does not have
  1046. a way for the user to provide pts */
  1047. if(avctx->sample_rate && avctx->time_base.num)
  1048. frame->pts = ff_samples_to_time_base(avctx,
  1049. avctx->internal->sample_count);
  1050. else
  1051. frame->pts = AV_NOPTS_VALUE;
  1052. avctx->internal->sample_count += frame->nb_samples;
  1053. } else {
  1054. frame = NULL;
  1055. }
  1056. got_packet = 0;
  1057. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1058. if (!ret && got_packet && avctx->coded_frame) {
  1059. avctx->coded_frame->pts = pkt.pts;
  1060. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1061. }
  1062. /* free any side data since we cannot return it */
  1063. ff_packet_free_side_data(&pkt);
  1064. if (frame && frame->extended_data != frame->data)
  1065. av_freep(&frame->extended_data);
  1066. return ret ? ret : pkt.size;
  1067. }
  1068. #endif
  1069. #if FF_API_OLD_ENCODE_VIDEO
  1070. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1071. const AVFrame *pict)
  1072. {
  1073. AVPacket pkt;
  1074. int ret, got_packet = 0;
  1075. if(buf_size < FF_MIN_BUFFER_SIZE){
  1076. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1077. return -1;
  1078. }
  1079. av_init_packet(&pkt);
  1080. pkt.data = buf;
  1081. pkt.size = buf_size;
  1082. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1083. if (!ret && got_packet && avctx->coded_frame) {
  1084. avctx->coded_frame->pts = pkt.pts;
  1085. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1086. }
  1087. /* free any side data since we cannot return it */
  1088. if (pkt.side_data_elems > 0) {
  1089. int i;
  1090. for (i = 0; i < pkt.side_data_elems; i++)
  1091. av_free(pkt.side_data[i].data);
  1092. av_freep(&pkt.side_data);
  1093. pkt.side_data_elems = 0;
  1094. }
  1095. return ret ? ret : pkt.size;
  1096. }
  1097. #endif
  1098. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1099. AVPacket *avpkt,
  1100. const AVFrame *frame,
  1101. int *got_packet_ptr)
  1102. {
  1103. int ret;
  1104. AVPacket user_pkt = *avpkt;
  1105. int needs_realloc = !user_pkt.data;
  1106. *got_packet_ptr = 0;
  1107. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1108. av_free_packet(avpkt);
  1109. av_init_packet(avpkt);
  1110. avpkt->size = 0;
  1111. return 0;
  1112. }
  1113. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1114. return AVERROR(EINVAL);
  1115. av_assert0(avctx->codec->encode2);
  1116. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1117. av_assert0(ret <= 0);
  1118. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1119. needs_realloc = 0;
  1120. if (user_pkt.data) {
  1121. if (user_pkt.size >= avpkt->size) {
  1122. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1123. } else {
  1124. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1125. avpkt->size = user_pkt.size;
  1126. ret = -1;
  1127. }
  1128. avpkt->data = user_pkt.data;
  1129. avpkt->destruct = user_pkt.destruct;
  1130. } else {
  1131. if (av_dup_packet(avpkt) < 0) {
  1132. ret = AVERROR(ENOMEM);
  1133. }
  1134. }
  1135. }
  1136. if (!ret) {
  1137. if (!*got_packet_ptr)
  1138. avpkt->size = 0;
  1139. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1140. avpkt->pts = avpkt->dts = frame->pts;
  1141. if (needs_realloc && avpkt->data &&
  1142. avpkt->destruct == av_destruct_packet) {
  1143. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1144. if (new_data)
  1145. avpkt->data = new_data;
  1146. }
  1147. avctx->frame_number++;
  1148. }
  1149. if (ret < 0 || !*got_packet_ptr)
  1150. av_free_packet(avpkt);
  1151. emms_c();
  1152. return ret;
  1153. }
  1154. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1155. const AVSubtitle *sub)
  1156. {
  1157. int ret;
  1158. if(sub->start_display_time) {
  1159. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1160. return -1;
  1161. }
  1162. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1163. avctx->frame_number++;
  1164. return ret;
  1165. }
  1166. /**
  1167. * Attempt to guess proper monotonic timestamps for decoded video frames
  1168. * which might have incorrect times. Input timestamps may wrap around, in
  1169. * which case the output will as well.
  1170. *
  1171. * @param pts the pts field of the decoded AVPacket, as passed through
  1172. * AVFrame.pkt_pts
  1173. * @param dts the dts field of the decoded AVPacket
  1174. * @return one of the input values, may be AV_NOPTS_VALUE
  1175. */
  1176. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1177. int64_t reordered_pts, int64_t dts)
  1178. {
  1179. int64_t pts = AV_NOPTS_VALUE;
  1180. if (dts != AV_NOPTS_VALUE) {
  1181. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1182. ctx->pts_correction_last_dts = dts;
  1183. }
  1184. if (reordered_pts != AV_NOPTS_VALUE) {
  1185. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1186. ctx->pts_correction_last_pts = reordered_pts;
  1187. }
  1188. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1189. && reordered_pts != AV_NOPTS_VALUE)
  1190. pts = reordered_pts;
  1191. else
  1192. pts = dts;
  1193. return pts;
  1194. }
  1195. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1196. {
  1197. int size = 0;
  1198. const uint8_t *data;
  1199. uint32_t flags;
  1200. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1201. return;
  1202. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1203. if (!data || size < 4)
  1204. return;
  1205. flags = bytestream_get_le32(&data);
  1206. size -= 4;
  1207. if (size < 4) /* Required for any of the changes */
  1208. return;
  1209. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1210. avctx->channels = bytestream_get_le32(&data);
  1211. size -= 4;
  1212. }
  1213. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1214. if (size < 8)
  1215. return;
  1216. avctx->channel_layout = bytestream_get_le64(&data);
  1217. size -= 8;
  1218. }
  1219. if (size < 4)
  1220. return;
  1221. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1222. avctx->sample_rate = bytestream_get_le32(&data);
  1223. size -= 4;
  1224. }
  1225. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1226. if (size < 8)
  1227. return;
  1228. avctx->width = bytestream_get_le32(&data);
  1229. avctx->height = bytestream_get_le32(&data);
  1230. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1231. size -= 8;
  1232. }
  1233. }
  1234. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1235. int *got_picture_ptr,
  1236. const AVPacket *avpkt)
  1237. {
  1238. int ret;
  1239. // copy to ensure we do not change avpkt
  1240. AVPacket tmp = *avpkt;
  1241. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1242. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1243. return AVERROR(EINVAL);
  1244. }
  1245. *got_picture_ptr= 0;
  1246. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1247. return -1;
  1248. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1249. int did_split = av_packet_split_side_data(&tmp);
  1250. apply_param_change(avctx, &tmp);
  1251. avctx->pkt = &tmp;
  1252. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1253. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1254. &tmp);
  1255. else {
  1256. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1257. &tmp);
  1258. picture->pkt_dts= avpkt->dts;
  1259. if(!avctx->has_b_frames){
  1260. picture->pkt_pos= avpkt->pos;
  1261. }
  1262. //FIXME these should be under if(!avctx->has_b_frames)
  1263. if (!picture->sample_aspect_ratio.num)
  1264. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1265. if (!picture->width)
  1266. picture->width = avctx->width;
  1267. if (!picture->height)
  1268. picture->height = avctx->height;
  1269. if (picture->format == PIX_FMT_NONE)
  1270. picture->format = avctx->pix_fmt;
  1271. }
  1272. emms_c(); //needed to avoid an emms_c() call before every return;
  1273. avctx->pkt = NULL;
  1274. if (did_split)
  1275. ff_packet_free_side_data(&tmp);
  1276. if (*got_picture_ptr){
  1277. avctx->frame_number++;
  1278. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1279. picture->pkt_pts,
  1280. picture->pkt_dts);
  1281. }
  1282. }else
  1283. ret= 0;
  1284. return ret;
  1285. }
  1286. #if FF_API_OLD_DECODE_AUDIO
  1287. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1288. int *frame_size_ptr,
  1289. AVPacket *avpkt)
  1290. {
  1291. AVFrame frame;
  1292. int ret, got_frame = 0;
  1293. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1294. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1295. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1296. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1297. "avcodec_decode_audio4()\n");
  1298. avctx->get_buffer = avcodec_default_get_buffer;
  1299. avctx->release_buffer = avcodec_default_release_buffer;
  1300. }
  1301. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1302. if (ret >= 0 && got_frame) {
  1303. int ch, plane_size;
  1304. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1305. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1306. frame.nb_samples,
  1307. avctx->sample_fmt, 1);
  1308. if (*frame_size_ptr < data_size) {
  1309. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1310. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1311. return AVERROR(EINVAL);
  1312. }
  1313. memcpy(samples, frame.extended_data[0], plane_size);
  1314. if (planar && avctx->channels > 1) {
  1315. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1316. for (ch = 1; ch < avctx->channels; ch++) {
  1317. memcpy(out, frame.extended_data[ch], plane_size);
  1318. out += plane_size;
  1319. }
  1320. }
  1321. *frame_size_ptr = data_size;
  1322. } else {
  1323. *frame_size_ptr = 0;
  1324. }
  1325. return ret;
  1326. }
  1327. #endif
  1328. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1329. AVFrame *frame,
  1330. int *got_frame_ptr,
  1331. const AVPacket *avpkt)
  1332. {
  1333. int ret = 0;
  1334. *got_frame_ptr = 0;
  1335. if (!avpkt->data && avpkt->size) {
  1336. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1337. return AVERROR(EINVAL);
  1338. }
  1339. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1340. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1341. return AVERROR(EINVAL);
  1342. }
  1343. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1344. // copy to ensure we do not change avpkt
  1345. AVPacket tmp = *avpkt;
  1346. int did_split = av_packet_split_side_data(&tmp);
  1347. apply_param_change(avctx, &tmp);
  1348. avctx->pkt = &tmp;
  1349. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1350. if (ret >= 0 && *got_frame_ptr) {
  1351. avctx->frame_number++;
  1352. frame->pkt_dts = avpkt->dts;
  1353. if (frame->format == AV_SAMPLE_FMT_NONE)
  1354. frame->format = avctx->sample_fmt;
  1355. if (!frame->channel_layout)
  1356. frame->channel_layout = avctx->channel_layout;
  1357. if (!frame->sample_rate)
  1358. frame->sample_rate = avctx->sample_rate;
  1359. }
  1360. avctx->pkt = NULL;
  1361. if (did_split)
  1362. ff_packet_free_side_data(&tmp);
  1363. }
  1364. return ret;
  1365. }
  1366. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1367. int *got_sub_ptr,
  1368. AVPacket *avpkt)
  1369. {
  1370. int ret;
  1371. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1372. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1373. return AVERROR(EINVAL);
  1374. }
  1375. avctx->pkt = avpkt;
  1376. *got_sub_ptr = 0;
  1377. avcodec_get_subtitle_defaults(sub);
  1378. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1379. if (*got_sub_ptr)
  1380. avctx->frame_number++;
  1381. return ret;
  1382. }
  1383. void avsubtitle_free(AVSubtitle *sub)
  1384. {
  1385. int i;
  1386. for (i = 0; i < sub->num_rects; i++)
  1387. {
  1388. av_freep(&sub->rects[i]->pict.data[0]);
  1389. av_freep(&sub->rects[i]->pict.data[1]);
  1390. av_freep(&sub->rects[i]->pict.data[2]);
  1391. av_freep(&sub->rects[i]->pict.data[3]);
  1392. av_freep(&sub->rects[i]->text);
  1393. av_freep(&sub->rects[i]->ass);
  1394. av_freep(&sub->rects[i]);
  1395. }
  1396. av_freep(&sub->rects);
  1397. memset(sub, 0, sizeof(AVSubtitle));
  1398. }
  1399. av_cold int avcodec_close(AVCodecContext *avctx)
  1400. {
  1401. /* If there is a user-supplied mutex locking routine, call it. */
  1402. if (ff_lockmgr_cb) {
  1403. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1404. return -1;
  1405. }
  1406. entangled_thread_counter++;
  1407. if(entangled_thread_counter != 1){
  1408. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1409. entangled_thread_counter--;
  1410. return -1;
  1411. }
  1412. if (avcodec_is_open(avctx)) {
  1413. if (HAVE_THREADS && avctx->thread_opaque)
  1414. ff_thread_free(avctx);
  1415. if (avctx->codec && avctx->codec->close)
  1416. avctx->codec->close(avctx);
  1417. avcodec_default_free_buffers(avctx);
  1418. avctx->coded_frame = NULL;
  1419. avctx->internal->byte_buffer_size = 0;
  1420. av_freep(&avctx->internal->byte_buffer);
  1421. av_freep(&avctx->internal);
  1422. }
  1423. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1424. av_opt_free(avctx->priv_data);
  1425. av_opt_free(avctx);
  1426. av_freep(&avctx->priv_data);
  1427. if (av_codec_is_encoder(avctx->codec))
  1428. av_freep(&avctx->extradata);
  1429. avctx->codec = NULL;
  1430. avctx->active_thread_type = 0;
  1431. entangled_thread_counter--;
  1432. /* Release any user-supplied mutex. */
  1433. if (ff_lockmgr_cb) {
  1434. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1435. }
  1436. return 0;
  1437. }
  1438. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1439. {
  1440. switch(id){
  1441. //This is for future deprecatec codec ids, its empty since
  1442. //last major bump but will fill up again over time, please dont remove it
  1443. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1444. default : return id;
  1445. }
  1446. }
  1447. AVCodec *avcodec_find_encoder(enum CodecID id)
  1448. {
  1449. AVCodec *p, *experimental=NULL;
  1450. p = first_avcodec;
  1451. id= remap_deprecated_codec_id(id);
  1452. while (p) {
  1453. if (av_codec_is_encoder(p) && p->id == id) {
  1454. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1455. experimental = p;
  1456. } else
  1457. return p;
  1458. }
  1459. p = p->next;
  1460. }
  1461. return experimental;
  1462. }
  1463. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1464. {
  1465. AVCodec *p;
  1466. if (!name)
  1467. return NULL;
  1468. p = first_avcodec;
  1469. while (p) {
  1470. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1471. return p;
  1472. p = p->next;
  1473. }
  1474. return NULL;
  1475. }
  1476. AVCodec *avcodec_find_decoder(enum CodecID id)
  1477. {
  1478. AVCodec *p, *experimental=NULL;
  1479. p = first_avcodec;
  1480. id= remap_deprecated_codec_id(id);
  1481. while (p) {
  1482. if (av_codec_is_decoder(p) && p->id == id) {
  1483. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1484. experimental = p;
  1485. } else
  1486. return p;
  1487. }
  1488. p = p->next;
  1489. }
  1490. return experimental;
  1491. }
  1492. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1493. {
  1494. AVCodec *p;
  1495. if (!name)
  1496. return NULL;
  1497. p = first_avcodec;
  1498. while (p) {
  1499. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1500. return p;
  1501. p = p->next;
  1502. }
  1503. return NULL;
  1504. }
  1505. const char *avcodec_get_name(enum CodecID id)
  1506. {
  1507. AVCodec *codec;
  1508. #if !CONFIG_SMALL
  1509. switch (id) {
  1510. #include "libavcodec/codec_names.h"
  1511. }
  1512. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1513. #endif
  1514. codec = avcodec_find_decoder(id);
  1515. if (codec)
  1516. return codec->name;
  1517. codec = avcodec_find_encoder(id);
  1518. if (codec)
  1519. return codec->name;
  1520. return "unknown_codec";
  1521. }
  1522. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1523. {
  1524. int i, len, ret = 0;
  1525. for (i = 0; i < 4; i++) {
  1526. len = snprintf(buf, buf_size,
  1527. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1528. buf += len;
  1529. buf_size = buf_size > len ? buf_size - len : 0;
  1530. ret += len;
  1531. codec_tag>>=8;
  1532. }
  1533. return ret;
  1534. }
  1535. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1536. {
  1537. const char *codec_type;
  1538. const char *codec_name;
  1539. const char *profile = NULL;
  1540. AVCodec *p;
  1541. int bitrate;
  1542. AVRational display_aspect_ratio;
  1543. if (!buf || buf_size <= 0)
  1544. return;
  1545. codec_type = av_get_media_type_string(enc->codec_type);
  1546. codec_name = avcodec_get_name(enc->codec_id);
  1547. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1548. if (enc->codec)
  1549. p = enc->codec;
  1550. else
  1551. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1552. avcodec_find_decoder(enc->codec_id);
  1553. if (p)
  1554. profile = av_get_profile_name(p, enc->profile);
  1555. }
  1556. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1557. codec_name, enc->mb_decision ? " (hq)" : "");
  1558. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1559. if (profile)
  1560. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1561. if (enc->codec_tag) {
  1562. char tag_buf[32];
  1563. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1564. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1565. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1566. }
  1567. switch(enc->codec_type) {
  1568. case AVMEDIA_TYPE_VIDEO:
  1569. if (enc->pix_fmt != PIX_FMT_NONE) {
  1570. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1571. ", %s",
  1572. av_get_pix_fmt_name(enc->pix_fmt));
  1573. }
  1574. if (enc->width) {
  1575. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1576. ", %dx%d",
  1577. enc->width, enc->height);
  1578. if (enc->sample_aspect_ratio.num) {
  1579. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1580. enc->width*enc->sample_aspect_ratio.num,
  1581. enc->height*enc->sample_aspect_ratio.den,
  1582. 1024*1024);
  1583. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1584. " [SAR %d:%d DAR %d:%d]",
  1585. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1586. display_aspect_ratio.num, display_aspect_ratio.den);
  1587. }
  1588. if(av_log_get_level() >= AV_LOG_DEBUG){
  1589. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1590. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1591. ", %d/%d",
  1592. enc->time_base.num/g, enc->time_base.den/g);
  1593. }
  1594. }
  1595. if (encode) {
  1596. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1597. ", q=%d-%d", enc->qmin, enc->qmax);
  1598. }
  1599. break;
  1600. case AVMEDIA_TYPE_AUDIO:
  1601. if (enc->sample_rate) {
  1602. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1603. ", %d Hz", enc->sample_rate);
  1604. }
  1605. av_strlcat(buf, ", ", buf_size);
  1606. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1607. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1608. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1609. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1610. }
  1611. break;
  1612. default:
  1613. return;
  1614. }
  1615. if (encode) {
  1616. if (enc->flags & CODEC_FLAG_PASS1)
  1617. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1618. ", pass 1");
  1619. if (enc->flags & CODEC_FLAG_PASS2)
  1620. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1621. ", pass 2");
  1622. }
  1623. bitrate = get_bit_rate(enc);
  1624. if (bitrate != 0) {
  1625. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1626. ", %d kb/s", bitrate / 1000);
  1627. }
  1628. }
  1629. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1630. {
  1631. const AVProfile *p;
  1632. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1633. return NULL;
  1634. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1635. if (p->profile == profile)
  1636. return p->name;
  1637. return NULL;
  1638. }
  1639. unsigned avcodec_version( void )
  1640. {
  1641. // av_assert0(CODEC_ID_V410==164);
  1642. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1643. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1644. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1645. av_assert0(CODEC_ID_SRT==94216);
  1646. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1647. return LIBAVCODEC_VERSION_INT;
  1648. }
  1649. const char *avcodec_configuration(void)
  1650. {
  1651. return FFMPEG_CONFIGURATION;
  1652. }
  1653. const char *avcodec_license(void)
  1654. {
  1655. #define LICENSE_PREFIX "libavcodec license: "
  1656. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1657. }
  1658. void avcodec_flush_buffers(AVCodecContext *avctx)
  1659. {
  1660. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1661. ff_thread_flush(avctx);
  1662. else if(avctx->codec->flush)
  1663. avctx->codec->flush(avctx);
  1664. avctx->pts_correction_last_pts =
  1665. avctx->pts_correction_last_dts = INT64_MIN;
  1666. }
  1667. static void video_free_buffers(AVCodecContext *s)
  1668. {
  1669. AVCodecInternal *avci = s->internal;
  1670. int i, j;
  1671. if (!avci->buffer)
  1672. return;
  1673. if (avci->buffer_count)
  1674. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1675. avci->buffer_count);
  1676. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1677. InternalBuffer *buf = &avci->buffer[i];
  1678. for(j=0; j<4; j++){
  1679. av_freep(&buf->base[j]);
  1680. buf->data[j]= NULL;
  1681. }
  1682. }
  1683. av_freep(&avci->buffer);
  1684. avci->buffer_count=0;
  1685. }
  1686. static void audio_free_buffers(AVCodecContext *avctx)
  1687. {
  1688. AVCodecInternal *avci = avctx->internal;
  1689. InternalBuffer *buf;
  1690. if (!avci->buffer)
  1691. return;
  1692. buf = avci->buffer;
  1693. if (buf->extended_data) {
  1694. av_free(buf->extended_data[0]);
  1695. if (buf->extended_data != buf->data)
  1696. av_freep(&buf->extended_data);
  1697. }
  1698. av_freep(&avci->buffer);
  1699. }
  1700. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1701. {
  1702. switch (avctx->codec_type) {
  1703. case AVMEDIA_TYPE_VIDEO:
  1704. video_free_buffers(avctx);
  1705. break;
  1706. case AVMEDIA_TYPE_AUDIO:
  1707. audio_free_buffers(avctx);
  1708. break;
  1709. default:
  1710. break;
  1711. }
  1712. }
  1713. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1714. {
  1715. switch(codec_id){
  1716. case CODEC_ID_ADPCM_CT:
  1717. case CODEC_ID_ADPCM_IMA_APC:
  1718. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1719. case CODEC_ID_ADPCM_IMA_WS:
  1720. case CODEC_ID_ADPCM_G722:
  1721. case CODEC_ID_ADPCM_YAMAHA:
  1722. return 4;
  1723. case CODEC_ID_PCM_ALAW:
  1724. case CODEC_ID_PCM_MULAW:
  1725. case CODEC_ID_PCM_S8:
  1726. case CODEC_ID_PCM_U8:
  1727. case CODEC_ID_PCM_ZORK:
  1728. return 8;
  1729. case CODEC_ID_PCM_S16BE:
  1730. case CODEC_ID_PCM_S16LE:
  1731. case CODEC_ID_PCM_S16LE_PLANAR:
  1732. case CODEC_ID_PCM_U16BE:
  1733. case CODEC_ID_PCM_U16LE:
  1734. return 16;
  1735. case CODEC_ID_PCM_S24DAUD:
  1736. case CODEC_ID_PCM_S24BE:
  1737. case CODEC_ID_PCM_S24LE:
  1738. case CODEC_ID_PCM_U24BE:
  1739. case CODEC_ID_PCM_U24LE:
  1740. return 24;
  1741. case CODEC_ID_PCM_S32BE:
  1742. case CODEC_ID_PCM_S32LE:
  1743. case CODEC_ID_PCM_U32BE:
  1744. case CODEC_ID_PCM_U32LE:
  1745. case CODEC_ID_PCM_F32BE:
  1746. case CODEC_ID_PCM_F32LE:
  1747. return 32;
  1748. case CODEC_ID_PCM_F64BE:
  1749. case CODEC_ID_PCM_F64LE:
  1750. return 64;
  1751. default:
  1752. return 0;
  1753. }
  1754. }
  1755. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1756. {
  1757. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1758. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1759. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1760. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1761. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1762. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1763. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1764. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1765. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1766. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1767. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1768. };
  1769. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1770. return CODEC_ID_NONE;
  1771. if (be < 0 || be > 1)
  1772. be = AV_NE(1, 0);
  1773. return map[fmt][be];
  1774. }
  1775. int av_get_bits_per_sample(enum CodecID codec_id)
  1776. {
  1777. switch (codec_id) {
  1778. case CODEC_ID_ADPCM_SBPRO_2:
  1779. return 2;
  1780. case CODEC_ID_ADPCM_SBPRO_3:
  1781. return 3;
  1782. case CODEC_ID_ADPCM_SBPRO_4:
  1783. case CODEC_ID_ADPCM_IMA_WAV:
  1784. case CODEC_ID_ADPCM_IMA_QT:
  1785. case CODEC_ID_ADPCM_SWF:
  1786. case CODEC_ID_ADPCM_MS:
  1787. return 4;
  1788. default:
  1789. return av_get_exact_bits_per_sample(codec_id);
  1790. }
  1791. }
  1792. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1793. {
  1794. int id, sr, ch, ba, tag, bps;
  1795. id = avctx->codec_id;
  1796. sr = avctx->sample_rate;
  1797. ch = avctx->channels;
  1798. ba = avctx->block_align;
  1799. tag = avctx->codec_tag;
  1800. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1801. /* codecs with an exact constant bits per sample */
  1802. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1803. return (frame_bytes * 8) / (bps * ch);
  1804. bps = avctx->bits_per_coded_sample;
  1805. /* codecs with a fixed packet duration */
  1806. switch (id) {
  1807. case CODEC_ID_ADPCM_ADX: return 32;
  1808. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1809. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1810. case CODEC_ID_AMR_NB:
  1811. case CODEC_ID_GSM:
  1812. case CODEC_ID_QCELP:
  1813. case CODEC_ID_RA_144:
  1814. case CODEC_ID_RA_288: return 160;
  1815. case CODEC_ID_IMC: return 256;
  1816. case CODEC_ID_AMR_WB:
  1817. case CODEC_ID_GSM_MS: return 320;
  1818. case CODEC_ID_MP1: return 384;
  1819. case CODEC_ID_ATRAC1: return 512;
  1820. case CODEC_ID_ATRAC3: return 1024;
  1821. case CODEC_ID_MP2:
  1822. case CODEC_ID_MUSEPACK7: return 1152;
  1823. case CODEC_ID_AC3: return 1536;
  1824. }
  1825. if (sr > 0) {
  1826. /* calc from sample rate */
  1827. if (id == CODEC_ID_TTA)
  1828. return 256 * sr / 245;
  1829. if (ch > 0) {
  1830. /* calc from sample rate and channels */
  1831. if (id == CODEC_ID_BINKAUDIO_DCT)
  1832. return (480 << (sr / 22050)) / ch;
  1833. }
  1834. }
  1835. if (ba > 0) {
  1836. /* calc from block_align */
  1837. if (id == CODEC_ID_SIPR) {
  1838. switch (ba) {
  1839. case 20: return 160;
  1840. case 19: return 144;
  1841. case 29: return 288;
  1842. case 37: return 480;
  1843. }
  1844. }
  1845. }
  1846. if (frame_bytes > 0) {
  1847. /* calc from frame_bytes only */
  1848. if (id == CODEC_ID_TRUESPEECH)
  1849. return 240 * (frame_bytes / 32);
  1850. if (id == CODEC_ID_NELLYMOSER)
  1851. return 256 * (frame_bytes / 64);
  1852. if (bps > 0) {
  1853. /* calc from frame_bytes and bits_per_coded_sample */
  1854. if (id == CODEC_ID_ADPCM_G726)
  1855. return frame_bytes * 8 / bps;
  1856. }
  1857. if (ch > 0) {
  1858. /* calc from frame_bytes and channels */
  1859. switch (id) {
  1860. case CODEC_ID_ADPCM_4XM:
  1861. case CODEC_ID_ADPCM_IMA_ISS:
  1862. return (frame_bytes - 4 * ch) * 2 / ch;
  1863. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1864. return (frame_bytes - 4) * 2 / ch;
  1865. case CODEC_ID_ADPCM_IMA_AMV:
  1866. return (frame_bytes - 8) * 2 / ch;
  1867. case CODEC_ID_ADPCM_XA:
  1868. return (frame_bytes / 128) * 224 / ch;
  1869. case CODEC_ID_INTERPLAY_DPCM:
  1870. return (frame_bytes - 6 - ch) / ch;
  1871. case CODEC_ID_ROQ_DPCM:
  1872. return (frame_bytes - 8) / ch;
  1873. case CODEC_ID_XAN_DPCM:
  1874. return (frame_bytes - 2 * ch) / ch;
  1875. case CODEC_ID_MACE3:
  1876. return 3 * frame_bytes / ch;
  1877. case CODEC_ID_MACE6:
  1878. return 6 * frame_bytes / ch;
  1879. case CODEC_ID_PCM_LXF:
  1880. return 2 * (frame_bytes / (5 * ch));
  1881. }
  1882. if (tag) {
  1883. /* calc from frame_bytes, channels, and codec_tag */
  1884. if (id == CODEC_ID_SOL_DPCM) {
  1885. if (tag == 3)
  1886. return frame_bytes / ch;
  1887. else
  1888. return frame_bytes * 2 / ch;
  1889. }
  1890. }
  1891. if (ba > 0) {
  1892. /* calc from frame_bytes, channels, and block_align */
  1893. int blocks = frame_bytes / ba;
  1894. switch (avctx->codec_id) {
  1895. case CODEC_ID_ADPCM_IMA_WAV:
  1896. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1897. case CODEC_ID_ADPCM_IMA_DK3:
  1898. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1899. case CODEC_ID_ADPCM_IMA_DK4:
  1900. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1901. case CODEC_ID_ADPCM_MS:
  1902. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1903. }
  1904. }
  1905. if (bps > 0) {
  1906. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1907. switch (avctx->codec_id) {
  1908. case CODEC_ID_PCM_DVD:
  1909. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1910. case CODEC_ID_PCM_BLURAY:
  1911. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1912. case CODEC_ID_S302M:
  1913. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. return 0;
  1919. }
  1920. #if !HAVE_THREADS
  1921. int ff_thread_init(AVCodecContext *s){
  1922. return -1;
  1923. }
  1924. #endif
  1925. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1926. {
  1927. unsigned int n = 0;
  1928. while(v >= 0xff) {
  1929. *s++ = 0xff;
  1930. v -= 0xff;
  1931. n++;
  1932. }
  1933. *s = v;
  1934. n++;
  1935. return n;
  1936. }
  1937. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1938. int i;
  1939. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1940. return i;
  1941. }
  1942. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1943. {
  1944. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1945. "version to the newest one from Git. If the problem still "
  1946. "occurs, it means that your file has a feature which has not "
  1947. "been implemented.\n", feature);
  1948. if(want_sample)
  1949. av_log_ask_for_sample(avc, NULL);
  1950. }
  1951. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1952. {
  1953. va_list argument_list;
  1954. va_start(argument_list, msg);
  1955. if (msg)
  1956. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1957. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1958. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1959. "and contact the ffmpeg-devel mailing list.\n");
  1960. va_end(argument_list);
  1961. }
  1962. static AVHWAccel *first_hwaccel = NULL;
  1963. void av_register_hwaccel(AVHWAccel *hwaccel)
  1964. {
  1965. AVHWAccel **p = &first_hwaccel;
  1966. while (*p)
  1967. p = &(*p)->next;
  1968. *p = hwaccel;
  1969. hwaccel->next = NULL;
  1970. }
  1971. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1972. {
  1973. return hwaccel ? hwaccel->next : first_hwaccel;
  1974. }
  1975. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1976. {
  1977. AVHWAccel *hwaccel=NULL;
  1978. while((hwaccel= av_hwaccel_next(hwaccel))){
  1979. if ( hwaccel->id == codec_id
  1980. && hwaccel->pix_fmt == pix_fmt)
  1981. return hwaccel;
  1982. }
  1983. return NULL;
  1984. }
  1985. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1986. {
  1987. if (ff_lockmgr_cb) {
  1988. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1989. return -1;
  1990. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1991. return -1;
  1992. }
  1993. ff_lockmgr_cb = cb;
  1994. if (ff_lockmgr_cb) {
  1995. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1996. return -1;
  1997. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1998. return -1;
  1999. }
  2000. return 0;
  2001. }
  2002. int avpriv_lock_avformat(void)
  2003. {
  2004. if (ff_lockmgr_cb) {
  2005. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2006. return -1;
  2007. }
  2008. return 0;
  2009. }
  2010. int avpriv_unlock_avformat(void)
  2011. {
  2012. if (ff_lockmgr_cb) {
  2013. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2014. return -1;
  2015. }
  2016. return 0;
  2017. }
  2018. unsigned int avpriv_toupper4(unsigned int x)
  2019. {
  2020. return toupper( x &0xFF)
  2021. + (toupper((x>>8 )&0xFF)<<8 )
  2022. + (toupper((x>>16)&0xFF)<<16)
  2023. + (toupper((x>>24)&0xFF)<<24);
  2024. }
  2025. #if !HAVE_THREADS
  2026. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2027. {
  2028. f->owner = avctx;
  2029. ff_init_buffer_info(avctx, f);
  2030. return avctx->get_buffer(avctx, f);
  2031. }
  2032. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2033. {
  2034. f->owner->release_buffer(f->owner, f);
  2035. }
  2036. void ff_thread_finish_setup(AVCodecContext *avctx)
  2037. {
  2038. }
  2039. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2040. {
  2041. }
  2042. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2043. {
  2044. }
  2045. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2046. {
  2047. return 1;
  2048. }
  2049. #endif
  2050. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2051. {
  2052. AVCodec *c= avcodec_find_decoder(codec_id);
  2053. if(!c)
  2054. c= avcodec_find_encoder(codec_id);
  2055. if(c)
  2056. return c->type;
  2057. if (codec_id <= CODEC_ID_NONE)
  2058. return AVMEDIA_TYPE_UNKNOWN;
  2059. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2060. return AVMEDIA_TYPE_VIDEO;
  2061. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2062. return AVMEDIA_TYPE_AUDIO;
  2063. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2064. return AVMEDIA_TYPE_SUBTITLE;
  2065. return AVMEDIA_TYPE_UNKNOWN;
  2066. }
  2067. int avcodec_is_open(AVCodecContext *s)
  2068. {
  2069. return !!s->internal;
  2070. }