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.

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