3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/samplefmt.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/avassert.h"
43 #include "libavutil/opt.h"
45 #include "frame_thread_encoder.h"
47 #include "bytestream.h"
57 volatile int ff_avcodec_locked;
58 static int volatile entangled_thread_counter = 0;
59 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
60 static void *codec_mutex;
61 static void *avformat_mutex;
63 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
68 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
70 ptr = av_realloc(ptr, min_size);
71 /* we could set this to the unmodified min_size but this is safer
72 * if the user lost the ptr and uses NULL now
82 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
87 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
89 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
96 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
98 ff_fast_malloc(ptr, size, min_size, 0);
101 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
104 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
109 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
110 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
113 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
116 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
121 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
122 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
125 /* encoder management */
126 static AVCodec *first_avcodec = NULL;
128 AVCodec *av_codec_next(const AVCodec *c)
133 return first_avcodec;
136 static void avcodec_init(void)
138 static int initialized = 0;
140 if (initialized != 0)
145 ff_dsputil_static_init();
148 int av_codec_is_encoder(const AVCodec *codec)
150 return codec && (codec->encode_sub || codec->encode2);
153 int av_codec_is_decoder(const AVCodec *codec)
155 return codec && codec->decode;
158 void avcodec_register(AVCodec *codec)
168 if (codec->init_static_data)
169 codec->init_static_data(codec);
172 unsigned avcodec_get_edge_width(void)
177 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
179 s->coded_width = width;
180 s->coded_height = height;
181 s->width = -((-width ) >> s->lowres);
182 s->height = -((-height) >> s->lowres);
185 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
186 # define STRIDE_ALIGN 16
188 # define STRIDE_ALIGN 8
191 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
192 int linesize_align[AV_NUM_DATA_POINTERS])
198 switch (s->pix_fmt) {
199 case AV_PIX_FMT_YUV420P:
200 case AV_PIX_FMT_YUYV422:
201 case AV_PIX_FMT_UYVY422:
202 case AV_PIX_FMT_YUV422P:
203 case AV_PIX_FMT_YUV440P:
204 case AV_PIX_FMT_YUV444P:
205 case AV_PIX_FMT_GBRP:
206 case AV_PIX_FMT_GRAY8:
207 case AV_PIX_FMT_GRAY16BE:
208 case AV_PIX_FMT_GRAY16LE:
209 case AV_PIX_FMT_YUVJ420P:
210 case AV_PIX_FMT_YUVJ422P:
211 case AV_PIX_FMT_YUVJ440P:
212 case AV_PIX_FMT_YUVJ444P:
213 case AV_PIX_FMT_YUVA420P:
214 case AV_PIX_FMT_YUVA422P:
215 case AV_PIX_FMT_YUVA444P:
216 case AV_PIX_FMT_YUV420P9LE:
217 case AV_PIX_FMT_YUV420P9BE:
218 case AV_PIX_FMT_YUV420P10LE:
219 case AV_PIX_FMT_YUV420P10BE:
220 case AV_PIX_FMT_YUV420P12LE:
221 case AV_PIX_FMT_YUV420P12BE:
222 case AV_PIX_FMT_YUV420P14LE:
223 case AV_PIX_FMT_YUV420P14BE:
224 case AV_PIX_FMT_YUV422P9LE:
225 case AV_PIX_FMT_YUV422P9BE:
226 case AV_PIX_FMT_YUV422P10LE:
227 case AV_PIX_FMT_YUV422P10BE:
228 case AV_PIX_FMT_YUV422P12LE:
229 case AV_PIX_FMT_YUV422P12BE:
230 case AV_PIX_FMT_YUV422P14LE:
231 case AV_PIX_FMT_YUV422P14BE:
232 case AV_PIX_FMT_YUV444P9LE:
233 case AV_PIX_FMT_YUV444P9BE:
234 case AV_PIX_FMT_YUV444P10LE:
235 case AV_PIX_FMT_YUV444P10BE:
236 case AV_PIX_FMT_YUV444P12LE:
237 case AV_PIX_FMT_YUV444P12BE:
238 case AV_PIX_FMT_YUV444P14LE:
239 case AV_PIX_FMT_YUV444P14BE:
240 case AV_PIX_FMT_GBRP9LE:
241 case AV_PIX_FMT_GBRP9BE:
242 case AV_PIX_FMT_GBRP10LE:
243 case AV_PIX_FMT_GBRP10BE:
244 case AV_PIX_FMT_GBRP12LE:
245 case AV_PIX_FMT_GBRP12BE:
246 case AV_PIX_FMT_GBRP14LE:
247 case AV_PIX_FMT_GBRP14BE:
248 w_align = 16; //FIXME assume 16 pixel per macroblock
249 h_align = 16 * 2; // interlaced needs 2 macroblocks height
251 case AV_PIX_FMT_YUV411P:
252 case AV_PIX_FMT_UYYVYY411:
256 case AV_PIX_FMT_YUV410P:
257 if (s->codec_id == AV_CODEC_ID_SVQ1) {
262 case AV_PIX_FMT_RGB555:
263 if (s->codec_id == AV_CODEC_ID_RPZA) {
268 case AV_PIX_FMT_PAL8:
269 case AV_PIX_FMT_BGR8:
270 case AV_PIX_FMT_RGB8:
271 if (s->codec_id == AV_CODEC_ID_SMC ||
272 s->codec_id == AV_CODEC_ID_CINEPAK) {
277 case AV_PIX_FMT_BGR24:
278 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
279 (s->codec_id == AV_CODEC_ID_ZLIB)) {
284 case AV_PIX_FMT_RGB24:
285 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
296 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
297 w_align = FFMAX(w_align, 8);
300 *width = FFALIGN(*width, w_align);
301 *height = FFALIGN(*height, h_align);
302 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
303 // some of the optimized chroma MC reads one line too much
304 // which is also done in mpeg decoders with lowres > 0
307 for (i = 0; i < 4; i++)
308 linesize_align[i] = STRIDE_ALIGN;
311 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
313 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
314 int chroma_shift = desc->log2_chroma_w;
315 int linesize_align[AV_NUM_DATA_POINTERS];
318 avcodec_align_dimensions2(s, width, height, linesize_align);
319 align = FFMAX(linesize_align[0], linesize_align[3]);
320 linesize_align[1] <<= chroma_shift;
321 linesize_align[2] <<= chroma_shift;
322 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
323 *width = FFALIGN(*width, align);
326 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
327 enum AVSampleFormat sample_fmt, const uint8_t *buf,
328 int buf_size, int align)
330 int ch, planar, needed_size, ret = 0;
332 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
333 frame->nb_samples, sample_fmt,
335 if (buf_size < needed_size)
336 return AVERROR(EINVAL);
338 planar = av_sample_fmt_is_planar(sample_fmt);
339 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
340 if (!(frame->extended_data = av_mallocz(nb_channels *
341 sizeof(*frame->extended_data))))
342 return AVERROR(ENOMEM);
344 frame->extended_data = frame->data;
347 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
348 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
349 sample_fmt, align)) < 0) {
350 if (frame->extended_data != frame->data)
351 av_freep(&frame->extended_data);
354 if (frame->extended_data != frame->data) {
355 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
356 frame->data[ch] = frame->extended_data[ch];
362 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
364 FramePool *pool = avctx->internal->pool;
367 switch (avctx->codec_type) {
368 case AVMEDIA_TYPE_VIDEO: {
371 int w = frame->width;
372 int h = frame->height;
373 int tmpsize, unaligned;
375 if (pool->format == frame->format &&
376 pool->width == frame->width && pool->height == frame->height)
379 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
381 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
387 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
388 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
389 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
390 // increase alignment of w for next try (rhs gives the lowest bit set in w)
394 for (i = 0; i < 4; i++)
395 unaligned |= picture.linesize[i] % pool->stride_align[i];
398 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
399 NULL, picture.linesize);
403 for (i = 0; i < 3 && picture.data[i + 1]; i++)
404 size[i] = picture.data[i + 1] - picture.data[i];
405 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
407 for (i = 0; i < 4; i++) {
408 av_buffer_pool_uninit(&pool->pools[i]);
409 pool->linesize[i] = picture.linesize[i];
411 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
412 if (!pool->pools[i]) {
413 ret = AVERROR(ENOMEM);
418 pool->format = frame->format;
419 pool->width = frame->width;
420 pool->height = frame->height;
424 case AVMEDIA_TYPE_AUDIO: {
425 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
426 int planar = av_sample_fmt_is_planar(frame->format);
427 int planes = planar ? ch : 1;
429 if (pool->format == frame->format && pool->planes == planes &&
430 pool->channels == ch && frame->nb_samples == pool->samples)
433 av_buffer_pool_uninit(&pool->pools[0]);
434 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
435 frame->nb_samples, frame->format, 0);
439 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
440 if (!pool->pools[0]) {
441 ret = AVERROR(ENOMEM);
445 pool->format = frame->format;
446 pool->planes = planes;
448 pool->samples = frame->nb_samples;
451 default: av_assert0(0);
455 for (i = 0; i < 4; i++)
456 av_buffer_pool_uninit(&pool->pools[i]);
458 pool->planes = pool->channels = pool->samples = 0;
459 pool->width = pool->height = 0;
463 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
465 FramePool *pool = avctx->internal->pool;
466 int planes = pool->planes;
469 frame->linesize[0] = pool->linesize[0];
471 if (planes > AV_NUM_DATA_POINTERS) {
472 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
473 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
474 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
475 sizeof(*frame->extended_buf));
476 if (!frame->extended_data || !frame->extended_buf) {
477 av_freep(&frame->extended_data);
478 av_freep(&frame->extended_buf);
479 return AVERROR(ENOMEM);
482 frame->extended_data = frame->data;
483 av_assert0(frame->nb_extended_buf == 0);
486 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
487 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
490 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
492 for (i = 0; i < frame->nb_extended_buf; i++) {
493 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
494 if (!frame->extended_buf[i])
496 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
499 if (avctx->debug & FF_DEBUG_BUFFERS)
500 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
504 av_frame_unref(frame);
505 return AVERROR(ENOMEM);
508 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
510 FramePool *pool = s->internal->pool;
511 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
512 int pixel_size = desc->comp[0].step_minus1 + 1;
513 int h_chroma_shift, v_chroma_shift;
516 if (pic->data[0] != NULL) {
517 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
521 memset(pic->data, 0, sizeof(pic->data));
522 pic->extended_data = pic->data;
524 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
526 for (i = 0; i < 4 && pool->pools[i]; i++) {
527 const int h_shift = i == 0 ? 0 : h_chroma_shift;
528 const int v_shift = i == 0 ? 0 : v_chroma_shift;
530 pic->linesize[i] = pool->linesize[i];
532 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
536 // no edge if EDGE EMU or not planar YUV
537 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
538 pic->data[i] = pic->buf[i]->data;
540 pic->data[i] = pic->buf[i]->data +
541 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
542 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
545 for (; i < AV_NUM_DATA_POINTERS; i++) {
547 pic->linesize[i] = 0;
549 if (pic->data[1] && !pic->data[2])
550 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
552 if (s->debug & FF_DEBUG_BUFFERS)
553 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
558 return AVERROR(ENOMEM);
561 void avpriv_color_frame(AVFrame *frame, const int c[4])
563 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
566 av_assert0(desc->flags & PIX_FMT_PLANAR);
568 for (p = 0; p<desc->nb_components; p++) {
569 uint8_t *dst = frame->data[p];
570 int is_chroma = p == 1 || p == 2;
571 int bytes = -((-frame->width) >> (is_chroma ? desc->log2_chroma_w : 0));
572 for (y = 0; y<-((-frame->height) >> (is_chroma ? desc->log2_chroma_h : 0)); y++){
573 if (desc->comp[0].depth_minus1 >= 8) {
574 for (x = 0; x<bytes; x++)
575 ((uint16_t*)dst)[x] = c[p];
577 memset(dst, c[p], bytes);
578 dst += frame->linesize[p];
583 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
587 if ((ret = update_frame_pool(avctx, frame)) < 0)
590 #if FF_API_GET_BUFFER
591 frame->type = FF_BUFFER_TYPE_INTERNAL;
594 switch (avctx->codec_type) {
595 case AVMEDIA_TYPE_VIDEO:
596 return video_get_buffer(avctx, frame);
597 case AVMEDIA_TYPE_AUDIO:
598 return audio_get_buffer(avctx, frame);
604 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
607 frame->pkt_pts = avctx->pkt->pts;
608 av_frame_set_pkt_pos (frame, avctx->pkt->pos);
609 av_frame_set_pkt_duration(frame, avctx->pkt->duration);
610 av_frame_set_pkt_size (frame, avctx->pkt->size);
612 frame->pkt_pts = AV_NOPTS_VALUE;
613 av_frame_set_pkt_pos (frame, -1);
614 av_frame_set_pkt_duration(frame, 0);
615 av_frame_set_pkt_size (frame, -1);
617 frame->reordered_opaque = avctx->reordered_opaque;
619 switch (avctx->codec->type) {
620 case AVMEDIA_TYPE_VIDEO:
622 frame->width = avctx->width;
624 frame->height = avctx->height;
625 if (frame->format < 0)
626 frame->format = avctx->pix_fmt;
627 if (!frame->sample_aspect_ratio.num)
628 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
630 case AVMEDIA_TYPE_AUDIO:
631 if (!frame->sample_rate)
632 frame->sample_rate = avctx->sample_rate;
633 if (frame->format < 0)
634 frame->format = avctx->sample_fmt;
635 if (!frame->channel_layout) {
636 if (avctx->channel_layout) {
637 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
639 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
641 return AVERROR(EINVAL);
644 frame->channel_layout = avctx->channel_layout;
646 if (avctx->channels > FF_SANE_NB_CHANNELS) {
647 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
649 return AVERROR(ENOSYS);
652 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
655 av_frame_set_channels(frame, avctx->channels);
661 #if FF_API_GET_BUFFER
662 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
664 return avcodec_default_get_buffer2(avctx, frame, 0);
667 typedef struct CompatReleaseBufPriv {
668 AVCodecContext avctx;
670 } CompatReleaseBufPriv;
672 static void compat_free_buffer(void *opaque, uint8_t *data)
674 CompatReleaseBufPriv *priv = opaque;
675 if (priv->avctx.release_buffer)
676 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
680 static void compat_release_buffer(void *opaque, uint8_t *data)
682 AVBufferRef *buf = opaque;
683 av_buffer_unref(&buf);
687 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
691 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
692 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
693 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
694 return AVERROR(EINVAL);
697 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
700 #if FF_API_GET_BUFFER
702 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
703 * We wrap each plane in its own AVBuffer. Each of those has a reference to
704 * a dummy AVBuffer as its private data, unreffing it on free.
705 * When all the planes are freed, the dummy buffer's free callback calls
708 if (avctx->get_buffer) {
709 CompatReleaseBufPriv *priv = NULL;
710 AVBufferRef *dummy_buf = NULL;
713 if (flags & AV_GET_BUFFER_FLAG_REF)
714 frame->reference = 1;
716 ret = avctx->get_buffer(avctx, frame);
720 /* return if the buffers are already set up
721 * this would happen e.g. when a custom get_buffer() calls
722 * avcodec_default_get_buffer
727 priv = av_mallocz(sizeof(*priv));
729 ret = AVERROR(ENOMEM);
732 priv->avctx = *avctx;
733 priv->frame = *frame;
735 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
737 ret = AVERROR(ENOMEM);
741 #define WRAP_PLANE(ref_out, data, data_size) \
743 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
745 ret = AVERROR(ENOMEM); \
748 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
751 av_frame_unref(frame); \
752 ret = AVERROR(ENOMEM); \
757 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
758 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
760 planes = av_pix_fmt_count_planes(frame->format);
761 if (!desc || planes <= 0) {
762 ret = AVERROR(EINVAL);
766 for (i = 0; i < planes; i++) {
767 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
768 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
770 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
773 int planar = av_sample_fmt_is_planar(frame->format);
774 planes = planar ? avctx->channels : 1;
776 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
777 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
778 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
779 frame->nb_extended_buf);
780 if (!frame->extended_buf) {
781 ret = AVERROR(ENOMEM);
786 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
787 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
789 for (i = 0; i < frame->nb_extended_buf; i++)
790 WRAP_PLANE(frame->extended_buf[i],
791 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
795 av_buffer_unref(&dummy_buf);
800 avctx->release_buffer(avctx, frame);
802 av_buffer_unref(&dummy_buf);
807 return avctx->get_buffer2(avctx, frame, flags);
810 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
812 int ret = get_buffer_internal(avctx, frame, flags);
814 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
818 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
823 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
825 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
826 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
827 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
828 av_frame_unref(frame);
831 ff_init_buffer_info(avctx, frame);
834 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
836 if (av_frame_is_writable(frame))
839 av_frame_move_ref(&tmp, frame);
841 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
843 av_frame_unref(&tmp);
847 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
848 frame->format, frame->width, frame->height);
850 av_frame_unref(&tmp);
855 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
857 int ret = reget_buffer_internal(avctx, frame);
859 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
863 #if FF_API_GET_BUFFER
864 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
866 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
871 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
877 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
881 for (i = 0; i < count; i++) {
882 int r = func(c, (char *)arg + i * size);
889 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
893 for (i = 0; i < count; i++) {
894 int r = func(c, arg, i, 0);
901 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
903 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
904 return desc->flags & PIX_FMT_HWACCEL;
907 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
909 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
914 void avcodec_get_frame_defaults(AVFrame *frame)
916 #if LIBAVCODEC_VERSION_MAJOR >= 55
917 // extended_data should explicitly be freed when needed, this code is unsafe currently
918 // also this is not compatible to the <55 ABI/API
919 if (frame->extended_data != frame->data && 0)
920 av_freep(&frame->extended_data);
923 memset(frame, 0, sizeof(AVFrame));
927 frame->pkt_pts = AV_NOPTS_VALUE;
928 av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
929 av_frame_set_pkt_duration (frame, 0);
930 av_frame_set_pkt_pos (frame, -1);
931 av_frame_set_pkt_size (frame, -1);
932 frame->key_frame = 1;
933 frame->sample_aspect_ratio = (AVRational) {0, 1 };
934 frame->format = -1; /* unknown */
935 frame->extended_data = frame->data;
938 AVFrame *avcodec_alloc_frame(void)
940 AVFrame *frame = av_malloc(sizeof(AVFrame));
945 frame->extended_data = NULL;
946 avcodec_get_frame_defaults(frame);
951 void avcodec_free_frame(AVFrame **frame)
955 if (!frame || !*frame)
960 if (f->extended_data != f->data)
961 av_freep(&f->extended_data);
966 #define MAKE_ACCESSORS(str, name, type, field) \
967 type av_##name##_get_##field(const str *s) { return s->field; } \
968 void av_##name##_set_##field(str *s, type v) { s->field = v; }
970 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
971 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
973 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
975 memset(sub, 0, sizeof(*sub));
976 sub->pts = AV_NOPTS_VALUE;
979 static int get_bit_rate(AVCodecContext *ctx)
984 switch (ctx->codec_type) {
985 case AVMEDIA_TYPE_VIDEO:
986 case AVMEDIA_TYPE_DATA:
987 case AVMEDIA_TYPE_SUBTITLE:
988 case AVMEDIA_TYPE_ATTACHMENT:
989 bit_rate = ctx->bit_rate;
991 case AVMEDIA_TYPE_AUDIO:
992 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
993 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1002 #if FF_API_AVCODEC_OPEN
1003 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1005 return avcodec_open2(avctx, codec, NULL);
1009 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1013 ff_unlock_avcodec();
1015 ret = avcodec_open2(avctx, codec, options);
1017 ff_lock_avcodec(avctx);
1021 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1024 AVDictionary *tmp = NULL;
1026 if (avcodec_is_open(avctx))
1029 if ((!codec && !avctx->codec)) {
1030 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1031 return AVERROR(EINVAL);
1033 if ((codec && avctx->codec && codec != avctx->codec)) {
1034 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1035 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1036 return AVERROR(EINVAL);
1039 codec = avctx->codec;
1041 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1042 return AVERROR(EINVAL);
1045 av_dict_copy(&tmp, *options, 0);
1047 ret = ff_lock_avcodec(avctx);
1051 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1052 if (!avctx->internal) {
1053 ret = AVERROR(ENOMEM);
1057 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1058 if (!avctx->internal->pool) {
1059 ret = AVERROR(ENOMEM);
1063 if (codec->priv_data_size > 0) {
1064 if (!avctx->priv_data) {
1065 avctx->priv_data = av_mallocz(codec->priv_data_size);
1066 if (!avctx->priv_data) {
1067 ret = AVERROR(ENOMEM);
1070 if (codec->priv_class) {
1071 *(const AVClass **)avctx->priv_data = codec->priv_class;
1072 av_opt_set_defaults(avctx->priv_data);
1075 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1078 avctx->priv_data = NULL;
1080 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1083 // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1084 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1085 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1086 if (avctx->coded_width && avctx->coded_height)
1087 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1088 else if (avctx->width && avctx->height)
1089 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1092 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1093 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1094 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1095 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1096 avcodec_set_dimensions(avctx, 0, 0);
1099 /* if the decoder init function was already called previously,
1100 * free the already allocated subtitle_header before overwriting it */
1101 if (av_codec_is_decoder(codec))
1102 av_freep(&avctx->subtitle_header);
1104 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1105 ret = AVERROR(EINVAL);
1109 avctx->codec = codec;
1110 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1111 avctx->codec_id == AV_CODEC_ID_NONE) {
1112 avctx->codec_type = codec->type;
1113 avctx->codec_id = codec->id;
1115 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1116 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1117 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1118 ret = AVERROR(EINVAL);
1121 avctx->frame_number = 0;
1122 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1124 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1125 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1126 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1128 av_log(NULL, AV_LOG_ERROR,
1129 "The %s '%s' is experimental but experimental codecs are not enabled, "
1130 "add '-strict %d' if you want to use it.\n",
1131 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1132 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1133 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1134 av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1135 codec_string, codec2->name);
1136 ret = AVERROR_EXPERIMENTAL;
1140 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1141 (!avctx->time_base.num || !avctx->time_base.den)) {
1142 avctx->time_base.num = 1;
1143 avctx->time_base.den = avctx->sample_rate;
1147 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1149 if (CONFIG_FRAME_THREAD_ENCODER) {
1150 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1151 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1152 ff_lock_avcodec(avctx);
1157 if (HAVE_THREADS && !avctx->thread_opaque
1158 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1159 ret = ff_thread_init(avctx);
1164 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1165 avctx->thread_count = 1;
1167 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1168 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1169 avctx->codec->max_lowres);
1170 ret = AVERROR(EINVAL);
1174 if (av_codec_is_encoder(avctx->codec)) {
1176 if (avctx->codec->sample_fmts) {
1177 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1178 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1180 if (avctx->channels == 1 &&
1181 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1182 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1183 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1187 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1189 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1190 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1191 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1192 ret = AVERROR(EINVAL);
1196 if (avctx->codec->pix_fmts) {
1197 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1198 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1200 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1201 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1202 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1204 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1205 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1206 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1207 ret = AVERROR(EINVAL);
1211 if (avctx->codec->supported_samplerates) {
1212 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1213 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1215 if (avctx->codec->supported_samplerates[i] == 0) {
1216 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1217 avctx->sample_rate);
1218 ret = AVERROR(EINVAL);
1222 if (avctx->codec->channel_layouts) {
1223 if (!avctx->channel_layout) {
1224 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1226 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1227 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1229 if (avctx->codec->channel_layouts[i] == 0) {
1231 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1232 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1233 ret = AVERROR(EINVAL);
1238 if (avctx->channel_layout && avctx->channels) {
1239 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1240 if (channels != avctx->channels) {
1242 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1243 av_log(avctx, AV_LOG_ERROR,
1244 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1245 buf, channels, avctx->channels);
1246 ret = AVERROR(EINVAL);
1249 } else if (avctx->channel_layout) {
1250 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1252 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1253 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1255 if (avctx->width <= 0 || avctx->height <= 0) {
1256 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1257 ret = AVERROR(EINVAL);
1261 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1262 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1263 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1266 if (!avctx->rc_initial_buffer_occupancy)
1267 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1270 avctx->pts_correction_num_faulty_pts =
1271 avctx->pts_correction_num_faulty_dts = 0;
1272 avctx->pts_correction_last_pts =
1273 avctx->pts_correction_last_dts = INT64_MIN;
1275 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1276 || avctx->internal->frame_thread_encoder)) {
1277 ret = avctx->codec->init(avctx);
1285 if (av_codec_is_decoder(avctx->codec)) {
1286 if (!avctx->bit_rate)
1287 avctx->bit_rate = get_bit_rate(avctx);
1288 /* validate channel layout from the decoder */
1289 if (avctx->channel_layout) {
1290 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1291 if (!avctx->channels)
1292 avctx->channels = channels;
1293 else if (channels != avctx->channels) {
1295 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1296 av_log(avctx, AV_LOG_WARNING,
1297 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1298 "ignoring specified channel layout\n",
1299 buf, channels, avctx->channels);
1300 avctx->channel_layout = 0;
1303 if (avctx->channels && avctx->channels < 0 ||
1304 avctx->channels > FF_SANE_NB_CHANNELS) {
1305 ret = AVERROR(EINVAL);
1308 if (avctx->sub_charenc) {
1309 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1310 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1311 "supported with subtitles codecs\n");
1312 ret = AVERROR(EINVAL);
1314 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1315 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1316 "subtitles character encoding will be ignored\n",
1317 avctx->codec_descriptor->name);
1318 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1320 /* input character encoding is set for a text based subtitle
1321 * codec at this point */
1322 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1323 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1325 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1327 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1328 if (cd == (iconv_t)-1) {
1329 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1330 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1331 ret = AVERROR(errno);
1336 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1337 "conversion needs a libavcodec built with iconv support "
1338 "for this codec\n");
1339 ret = AVERROR(ENOSYS);
1347 ff_unlock_avcodec();
1349 av_dict_free(options);
1356 av_freep(&avctx->priv_data);
1357 if (avctx->internal)
1358 av_freep(&avctx->internal->pool);
1359 av_freep(&avctx->internal);
1360 avctx->codec = NULL;
1364 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1366 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1367 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1368 return AVERROR(EINVAL);
1372 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1373 if (!avpkt->data || avpkt->size < size) {
1374 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1375 avpkt->data = avctx->internal->byte_buffer;
1376 avpkt->size = avctx->internal->byte_buffer_size;
1377 avpkt->destruct = NULL;
1382 AVBufferRef *buf = avpkt->buf;
1383 #if FF_API_DESTRUCT_PACKET
1384 void *destruct = avpkt->destruct;
1387 if (avpkt->size < size) {
1388 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1389 return AVERROR(EINVAL);
1392 av_init_packet(avpkt);
1393 #if FF_API_DESTRUCT_PACKET
1394 avpkt->destruct = destruct;
1400 int ret = av_new_packet(avpkt, size);
1402 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1407 int ff_alloc_packet(AVPacket *avpkt, int size)
1409 return ff_alloc_packet2(NULL, avpkt, size);
1413 * Pad last frame with silence.
1415 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1417 AVFrame *frame = NULL;
1418 uint8_t *buf = NULL;
1421 if (!(frame = avcodec_alloc_frame()))
1422 return AVERROR(ENOMEM);
1425 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1426 s->frame_size, s->sample_fmt, 0)) < 0)
1429 if (!(buf = av_malloc(ret))) {
1430 ret = AVERROR(ENOMEM);
1434 frame->nb_samples = s->frame_size;
1435 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1438 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1439 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1441 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1442 frame->nb_samples - src->nb_samples,
1443 s->channels, s->sample_fmt)) < 0)
1451 if (frame->extended_data != frame->data)
1452 av_freep(&frame->extended_data);
1458 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1460 const AVFrame *frame,
1461 int *got_packet_ptr)
1464 AVFrame *padded_frame = NULL;
1466 AVPacket user_pkt = *avpkt;
1467 int needs_realloc = !user_pkt.data;
1469 *got_packet_ptr = 0;
1471 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1472 av_free_packet(avpkt);
1473 av_init_packet(avpkt);
1477 /* ensure that extended_data is properly set */
1478 if (frame && !frame->extended_data) {
1479 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1480 avctx->channels > AV_NUM_DATA_POINTERS) {
1481 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1482 "with more than %d channels, but extended_data is not set.\n",
1483 AV_NUM_DATA_POINTERS);
1484 return AVERROR(EINVAL);
1486 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1489 tmp.extended_data = tmp.data;
1493 /* check for valid frame size */
1495 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1496 if (frame->nb_samples > avctx->frame_size) {
1497 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1498 return AVERROR(EINVAL);
1500 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1501 if (frame->nb_samples < avctx->frame_size &&
1502 !avctx->internal->last_audio_frame) {
1503 ret = pad_last_frame(avctx, &padded_frame, frame);
1507 frame = padded_frame;
1508 avctx->internal->last_audio_frame = 1;
1511 if (frame->nb_samples != avctx->frame_size) {
1512 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1513 ret = AVERROR(EINVAL);
1519 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1521 if (*got_packet_ptr) {
1522 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1523 if (avpkt->pts == AV_NOPTS_VALUE)
1524 avpkt->pts = frame->pts;
1525 if (!avpkt->duration)
1526 avpkt->duration = ff_samples_to_time_base(avctx,
1529 avpkt->dts = avpkt->pts;
1534 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1536 if (user_pkt.data) {
1537 if (user_pkt.size >= avpkt->size) {
1538 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1540 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1541 avpkt->size = user_pkt.size;
1544 avpkt->buf = user_pkt.buf;
1545 avpkt->data = user_pkt.data;
1546 avpkt->destruct = user_pkt.destruct;
1548 if (av_dup_packet(avpkt) < 0) {
1549 ret = AVERROR(ENOMEM);
1555 if (needs_realloc && avpkt->data) {
1556 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1558 avpkt->data = avpkt->buf->data;
1561 avctx->frame_number++;
1564 if (ret < 0 || !*got_packet_ptr) {
1565 av_free_packet(avpkt);
1566 av_init_packet(avpkt);
1570 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1571 * this needs to be moved to the encoders, but for now we can do it
1572 * here to simplify things */
1573 avpkt->flags |= AV_PKT_FLAG_KEY;
1577 av_freep(&padded_frame->data[0]);
1578 if (padded_frame->extended_data != padded_frame->data)
1579 av_freep(&padded_frame->extended_data);
1580 av_freep(&padded_frame);
1586 #if FF_API_OLD_ENCODE_AUDIO
1587 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1588 uint8_t *buf, int buf_size,
1589 const short *samples)
1592 AVFrame frame0 = { { 0 } };
1594 int ret, samples_size, got_packet;
1596 av_init_packet(&pkt);
1598 pkt.size = buf_size;
1602 avcodec_get_frame_defaults(frame);
1604 if (avctx->frame_size) {
1605 frame->nb_samples = avctx->frame_size;
1607 /* if frame_size is not set, the number of samples must be
1608 * calculated from the buffer size */
1610 if (!av_get_bits_per_sample(avctx->codec_id)) {
1611 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1612 "support this codec\n");
1613 return AVERROR(EINVAL);
1615 nb_samples = (int64_t)buf_size * 8 /
1616 (av_get_bits_per_sample(avctx->codec_id) *
1618 if (nb_samples >= INT_MAX)
1619 return AVERROR(EINVAL);
1620 frame->nb_samples = nb_samples;
1623 /* it is assumed that the samples buffer is large enough based on the
1624 * relevant parameters */
1625 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1627 avctx->sample_fmt, 1);
1628 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1630 (const uint8_t *)samples,
1631 samples_size, 1)) < 0)
1634 /* fabricate frame pts from sample count.
1635 * this is needed because the avcodec_encode_audio() API does not have
1636 * a way for the user to provide pts */
1637 if (avctx->sample_rate && avctx->time_base.num)
1638 frame->pts = ff_samples_to_time_base(avctx,
1639 avctx->internal->sample_count);
1641 frame->pts = AV_NOPTS_VALUE;
1642 avctx->internal->sample_count += frame->nb_samples;
1648 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1649 if (!ret && got_packet && avctx->coded_frame) {
1650 avctx->coded_frame->pts = pkt.pts;
1651 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1653 /* free any side data since we cannot return it */
1654 ff_packet_free_side_data(&pkt);
1656 if (frame && frame->extended_data != frame->data)
1657 av_freep(&frame->extended_data);
1659 return ret ? ret : pkt.size;
1664 #if FF_API_OLD_ENCODE_VIDEO
1665 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1666 const AVFrame *pict)
1669 int ret, got_packet = 0;
1671 if (buf_size < FF_MIN_BUFFER_SIZE) {
1672 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1676 av_init_packet(&pkt);
1678 pkt.size = buf_size;
1680 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1681 if (!ret && got_packet && avctx->coded_frame) {
1682 avctx->coded_frame->pts = pkt.pts;
1683 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1686 /* free any side data since we cannot return it */
1687 if (pkt.side_data_elems > 0) {
1689 for (i = 0; i < pkt.side_data_elems; i++)
1690 av_free(pkt.side_data[i].data);
1691 av_freep(&pkt.side_data);
1692 pkt.side_data_elems = 0;
1695 return ret ? ret : pkt.size;
1700 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1702 const AVFrame *frame,
1703 int *got_packet_ptr)
1706 AVPacket user_pkt = *avpkt;
1707 int needs_realloc = !user_pkt.data;
1709 *got_packet_ptr = 0;
1711 if(CONFIG_FRAME_THREAD_ENCODER &&
1712 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1713 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1715 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1716 avctx->stats_out[0] = '\0';
1718 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1719 av_free_packet(avpkt);
1720 av_init_packet(avpkt);
1725 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1726 return AVERROR(EINVAL);
1728 av_assert0(avctx->codec->encode2);
1730 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1731 av_assert0(ret <= 0);
1733 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1735 if (user_pkt.data) {
1736 if (user_pkt.size >= avpkt->size) {
1737 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1739 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1740 avpkt->size = user_pkt.size;
1743 avpkt->buf = user_pkt.buf;
1744 avpkt->data = user_pkt.data;
1745 avpkt->destruct = user_pkt.destruct;
1747 if (av_dup_packet(avpkt) < 0) {
1748 ret = AVERROR(ENOMEM);
1754 if (!*got_packet_ptr)
1756 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1757 avpkt->pts = avpkt->dts = frame->pts;
1759 if (needs_realloc && avpkt->data) {
1760 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1762 avpkt->data = avpkt->buf->data;
1765 avctx->frame_number++;
1768 if (ret < 0 || !*got_packet_ptr)
1769 av_free_packet(avpkt);
1775 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1776 const AVSubtitle *sub)
1779 if (sub->start_display_time) {
1780 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1784 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1785 avctx->frame_number++;
1790 * Attempt to guess proper monotonic timestamps for decoded video frames
1791 * which might have incorrect times. Input timestamps may wrap around, in
1792 * which case the output will as well.
1794 * @param pts the pts field of the decoded AVPacket, as passed through
1796 * @param dts the dts field of the decoded AVPacket
1797 * @return one of the input values, may be AV_NOPTS_VALUE
1799 static int64_t guess_correct_pts(AVCodecContext *ctx,
1800 int64_t reordered_pts, int64_t dts)
1802 int64_t pts = AV_NOPTS_VALUE;
1804 if (dts != AV_NOPTS_VALUE) {
1805 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1806 ctx->pts_correction_last_dts = dts;
1808 if (reordered_pts != AV_NOPTS_VALUE) {
1809 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1810 ctx->pts_correction_last_pts = reordered_pts;
1812 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1813 && reordered_pts != AV_NOPTS_VALUE)
1814 pts = reordered_pts;
1821 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1824 const uint8_t *data;
1827 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1830 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1831 if (!data || size < 4)
1833 flags = bytestream_get_le32(&data);
1835 if (size < 4) /* Required for any of the changes */
1837 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1838 avctx->channels = bytestream_get_le32(&data);
1841 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1844 avctx->channel_layout = bytestream_get_le64(&data);
1849 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1850 avctx->sample_rate = bytestream_get_le32(&data);
1853 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1856 avctx->width = bytestream_get_le32(&data);
1857 avctx->height = bytestream_get_le32(&data);
1858 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1863 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1866 const uint8_t *side_metadata;
1869 side_metadata = av_packet_get_side_data(avctx->pkt,
1870 AV_PKT_DATA_STRINGS_METADATA, &size);
1873 end = side_metadata + size;
1874 while (side_metadata < end) {
1875 const uint8_t *key = side_metadata;
1876 const uint8_t *val = side_metadata + strlen(key) + 1;
1877 int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
1880 side_metadata = val + strlen(val) + 1;
1886 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1887 int *got_picture_ptr,
1888 const AVPacket *avpkt)
1890 AVCodecInternal *avci = avctx->internal;
1892 // copy to ensure we do not change avpkt
1893 AVPacket tmp = *avpkt;
1895 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1896 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1897 return AVERROR(EINVAL);
1900 *got_picture_ptr = 0;
1901 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1902 return AVERROR(EINVAL);
1904 avcodec_get_frame_defaults(picture);
1906 if (!avctx->refcounted_frames)
1907 av_frame_unref(&avci->to_free);
1909 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1910 int did_split = av_packet_split_side_data(&tmp);
1911 apply_param_change(avctx, &tmp);
1913 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1914 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1917 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1919 picture->pkt_dts = avpkt->dts;
1921 if(!avctx->has_b_frames){
1922 av_frame_set_pkt_pos(picture, avpkt->pos);
1924 //FIXME these should be under if(!avctx->has_b_frames)
1925 /* get_buffer is supposed to set frame parameters */
1926 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1927 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1928 if (!picture->width) picture->width = avctx->width;
1929 if (!picture->height) picture->height = avctx->height;
1930 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1933 add_metadata_from_side_data(avctx, picture);
1935 emms_c(); //needed to avoid an emms_c() call before every return;
1939 ff_packet_free_side_data(&tmp);
1944 if (ret < 0 && picture->data[0])
1945 av_frame_unref(picture);
1947 if (*got_picture_ptr) {
1948 if (!avctx->refcounted_frames) {
1949 avci->to_free = *picture;
1950 avci->to_free.extended_data = avci->to_free.data;
1953 avctx->frame_number++;
1954 av_frame_set_best_effort_timestamp(picture,
1955 guess_correct_pts(avctx,
1962 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1963 * make sure it's set correctly */
1964 picture->extended_data = picture->data;
1969 #if FF_API_OLD_DECODE_AUDIO
1970 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1971 int *frame_size_ptr,
1974 AVFrame frame = { { 0 } };
1975 int ret, got_frame = 0;
1977 if (avctx->get_buffer != avcodec_default_get_buffer) {
1978 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1979 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1980 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1981 "avcodec_decode_audio4()\n");
1982 avctx->get_buffer = avcodec_default_get_buffer;
1983 avctx->release_buffer = avcodec_default_release_buffer;
1986 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1988 if (ret >= 0 && got_frame) {
1990 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1991 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1993 avctx->sample_fmt, 1);
1994 if (*frame_size_ptr < data_size) {
1995 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1996 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1997 return AVERROR(EINVAL);
2000 memcpy(samples, frame.extended_data[0], plane_size);
2002 if (planar && avctx->channels > 1) {
2003 uint8_t *out = ((uint8_t *)samples) + plane_size;
2004 for (ch = 1; ch < avctx->channels; ch++) {
2005 memcpy(out, frame.extended_data[ch], plane_size);
2009 *frame_size_ptr = data_size;
2011 *frame_size_ptr = 0;
2018 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2021 const AVPacket *avpkt)
2023 AVCodecInternal *avci = avctx->internal;
2024 int planar, channels;
2029 if (!avpkt->data && avpkt->size) {
2030 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2031 return AVERROR(EINVAL);
2033 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2034 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2035 return AVERROR(EINVAL);
2038 avcodec_get_frame_defaults(frame);
2040 if (!avctx->refcounted_frames)
2041 av_frame_unref(&avci->to_free);
2043 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2046 // copy to ensure we do not change avpkt
2047 AVPacket tmp = *avpkt;
2048 int did_split = av_packet_split_side_data(&tmp);
2049 apply_param_change(avctx, &tmp);
2052 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2053 if (ret >= 0 && *got_frame_ptr) {
2054 avctx->frame_number++;
2055 frame->pkt_dts = avpkt->dts;
2056 av_frame_set_best_effort_timestamp(frame,
2057 guess_correct_pts(avctx,
2060 if (frame->format == AV_SAMPLE_FMT_NONE)
2061 frame->format = avctx->sample_fmt;
2062 if (!frame->channel_layout)
2063 frame->channel_layout = avctx->channel_layout;
2064 if (!av_frame_get_channels(frame))
2065 av_frame_set_channels(frame, avctx->channels);
2066 if (!frame->sample_rate)
2067 frame->sample_rate = avctx->sample_rate;
2068 if (!avctx->refcounted_frames) {
2069 avci->to_free = *frame;
2070 avci->to_free.extended_data = avci->to_free.data;
2073 add_metadata_from_side_data(avctx, frame);
2075 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2076 if(side && side_size>=10) {
2077 avctx->internal->skip_samples = AV_RL32(side);
2078 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2079 avctx->internal->skip_samples);
2081 if (avctx->internal->skip_samples && *got_frame_ptr) {
2082 if(frame->nb_samples <= avctx->internal->skip_samples){
2084 avctx->internal->skip_samples -= frame->nb_samples;
2085 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2086 avctx->internal->skip_samples);
2088 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2089 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2090 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2091 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2092 (AVRational){1, avctx->sample_rate},
2093 avctx->pkt_timebase);
2094 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2095 frame->pkt_pts += diff_ts;
2096 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2097 frame->pkt_dts += diff_ts;
2098 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2099 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2101 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2103 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2104 avctx->internal->skip_samples, frame->nb_samples);
2105 frame->nb_samples -= avctx->internal->skip_samples;
2106 avctx->internal->skip_samples = 0;
2112 ff_packet_free_side_data(&tmp);
2117 if (ret < 0 && frame->data[0])
2118 av_frame_unref(frame);
2121 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2122 * make sure it's set correctly; assume decoders that actually use
2123 * extended_data are doing it correctly */
2124 if (*got_frame_ptr) {
2125 planar = av_sample_fmt_is_planar(frame->format);
2126 channels = av_frame_get_channels(frame);
2127 if (!(planar && channels > AV_NUM_DATA_POINTERS))
2128 frame->extended_data = frame->data;
2130 frame->extended_data = NULL;
2136 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2137 static int recode_subtitle(AVCodecContext *avctx,
2138 AVPacket *outpkt, const AVPacket *inpkt)
2141 iconv_t cd = (iconv_t)-1;
2148 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
2152 cd = iconv_open("UTF-8", avctx->sub_charenc);
2153 av_assert0(cd != (iconv_t)-1);
2158 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2159 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2160 ret = AVERROR(ENOMEM);
2164 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2167 outpkt->buf = tmp.buf;
2168 outpkt->data = tmp.data;
2169 outpkt->size = tmp.size;
2170 outb = outpkt->data;
2171 outl = outpkt->size;
2173 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2174 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2175 outl >= outpkt->size || inl != 0) {
2176 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2177 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2178 av_free_packet(&tmp);
2179 ret = AVERROR(errno);
2182 outpkt->size -= outl;
2183 outpkt->data[outpkt->size - 1] = '\0';
2186 if (cd != (iconv_t)-1)
2190 av_assert0(!"requesting subtitles recoding without iconv");
2194 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2200 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2201 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2202 return AVERROR(EINVAL);
2206 avcodec_get_subtitle_defaults(sub);
2209 AVPacket pkt_recoded;
2210 AVPacket tmp = *avpkt;
2211 int did_split = av_packet_split_side_data(&tmp);
2212 //apply_param_change(avctx, &tmp);
2215 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2219 avctx->pkt = &pkt_recoded;
2221 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2222 sub->pts = av_rescale_q(avpkt->pts,
2223 avctx->pkt_timebase, AV_TIME_BASE_Q);
2224 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2225 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2226 !!*got_sub_ptr >= !!sub->num_rects);
2227 if (tmp.data != pkt_recoded.data) { // did we recode?
2228 /* prevent from destroying side data from original packet */
2229 pkt_recoded.side_data = NULL;
2230 pkt_recoded.side_data_elems = 0;
2232 av_free_packet(&pkt_recoded);
2234 sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
2239 ff_packet_free_side_data(&tmp);
2245 avctx->frame_number++;
2251 void avsubtitle_free(AVSubtitle *sub)
2255 for (i = 0; i < sub->num_rects; i++) {
2256 av_freep(&sub->rects[i]->pict.data[0]);
2257 av_freep(&sub->rects[i]->pict.data[1]);
2258 av_freep(&sub->rects[i]->pict.data[2]);
2259 av_freep(&sub->rects[i]->pict.data[3]);
2260 av_freep(&sub->rects[i]->text);
2261 av_freep(&sub->rects[i]->ass);
2262 av_freep(&sub->rects[i]);
2265 av_freep(&sub->rects);
2267 memset(sub, 0, sizeof(AVSubtitle));
2270 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2274 ff_unlock_avcodec();
2276 ret = avcodec_close(avctx);
2278 ff_lock_avcodec(NULL);
2282 av_cold int avcodec_close(AVCodecContext *avctx)
2284 int ret = ff_lock_avcodec(avctx);
2288 if (avcodec_is_open(avctx)) {
2289 FramePool *pool = avctx->internal->pool;
2291 if (CONFIG_FRAME_THREAD_ENCODER &&
2292 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2293 ff_unlock_avcodec();
2294 ff_frame_thread_encoder_free(avctx);
2295 ff_lock_avcodec(avctx);
2297 if (HAVE_THREADS && avctx->thread_opaque)
2298 ff_thread_free(avctx);
2299 if (avctx->codec && avctx->codec->close)
2300 avctx->codec->close(avctx);
2301 avctx->coded_frame = NULL;
2302 avctx->internal->byte_buffer_size = 0;
2303 av_freep(&avctx->internal->byte_buffer);
2304 if (!avctx->refcounted_frames)
2305 av_frame_unref(&avctx->internal->to_free);
2306 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2307 av_buffer_pool_uninit(&pool->pools[i]);
2308 av_freep(&avctx->internal->pool);
2309 av_freep(&avctx->internal);
2312 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2313 av_opt_free(avctx->priv_data);
2315 av_freep(&avctx->priv_data);
2316 if (av_codec_is_encoder(avctx->codec))
2317 av_freep(&avctx->extradata);
2318 avctx->codec = NULL;
2319 avctx->active_thread_type = 0;
2321 ff_unlock_avcodec();
2325 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2328 //This is for future deprecatec codec ids, its empty since
2329 //last major bump but will fill up again over time, please don't remove it
2330 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2331 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
2332 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2333 default : return id;
2337 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2339 AVCodec *p, *experimental = NULL;
2341 id= remap_deprecated_codec_id(id);
2343 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2345 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2352 return experimental;
2355 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2357 return find_encdec(id, 1);
2360 AVCodec *avcodec_find_encoder_by_name(const char *name)
2367 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2374 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2376 return find_encdec(id, 0);
2379 AVCodec *avcodec_find_decoder_by_name(const char *name)
2386 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2393 const char *avcodec_get_name(enum AVCodecID id)
2395 const AVCodecDescriptor *cd;
2398 if (id == AV_CODEC_ID_NONE)
2400 cd = avcodec_descriptor_get(id);
2403 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2404 codec = avcodec_find_decoder(id);
2407 codec = avcodec_find_encoder(id);
2410 return "unknown_codec";
2413 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2415 int i, len, ret = 0;
2417 #define TAG_PRINT(x) \
2418 (((x) >= '0' && (x) <= '9') || \
2419 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2420 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2422 for (i = 0; i < 4; i++) {
2423 len = snprintf(buf, buf_size,
2424 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2426 buf_size = buf_size > len ? buf_size - len : 0;
2433 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2435 const char *codec_type;
2436 const char *codec_name;
2437 const char *profile = NULL;
2440 AVRational display_aspect_ratio;
2442 if (!buf || buf_size <= 0)
2444 codec_type = av_get_media_type_string(enc->codec_type);
2445 codec_name = avcodec_get_name(enc->codec_id);
2446 if (enc->profile != FF_PROFILE_UNKNOWN) {
2450 p = encode ? avcodec_find_encoder(enc->codec_id) :
2451 avcodec_find_decoder(enc->codec_id);
2453 profile = av_get_profile_name(p, enc->profile);
2456 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2457 codec_name, enc->mb_decision ? " (hq)" : "");
2458 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2460 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2461 if (enc->codec_tag) {
2463 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2464 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2465 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2468 switch (enc->codec_type) {
2469 case AVMEDIA_TYPE_VIDEO:
2470 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2471 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2473 av_get_pix_fmt_name(enc->pix_fmt));
2474 if (enc->bits_per_raw_sample &&
2475 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2476 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2477 " (%d bpc)", enc->bits_per_raw_sample);
2480 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2482 enc->width, enc->height);
2483 if (enc->sample_aspect_ratio.num) {
2484 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2485 enc->width * enc->sample_aspect_ratio.num,
2486 enc->height * enc->sample_aspect_ratio.den,
2488 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2489 " [SAR %d:%d DAR %d:%d]",
2490 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2491 display_aspect_ratio.num, display_aspect_ratio.den);
2493 if (av_log_get_level() >= AV_LOG_DEBUG) {
2494 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2495 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2497 enc->time_base.num / g, enc->time_base.den / g);
2501 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2502 ", q=%d-%d", enc->qmin, enc->qmax);
2505 case AVMEDIA_TYPE_AUDIO:
2506 if (enc->sample_rate) {
2507 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2508 ", %d Hz", enc->sample_rate);
2510 av_strlcat(buf, ", ", buf_size);
2511 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2512 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2513 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2514 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2517 case AVMEDIA_TYPE_DATA:
2518 if (av_log_get_level() >= AV_LOG_DEBUG) {
2519 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2521 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2523 enc->time_base.num / g, enc->time_base.den / g);
2530 if (enc->flags & CODEC_FLAG_PASS1)
2531 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2533 if (enc->flags & CODEC_FLAG_PASS2)
2534 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2537 bitrate = get_bit_rate(enc);
2539 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2540 ", %d kb/s", bitrate / 1000);
2544 const char *av_get_profile_name(const AVCodec *codec, int profile)
2547 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2550 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2551 if (p->profile == profile)
2557 unsigned avcodec_version(void)
2559 // av_assert0(AV_CODEC_ID_V410==164);
2560 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2561 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2562 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2563 av_assert0(AV_CODEC_ID_SRT==94216);
2564 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2566 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2567 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2568 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2569 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2570 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2571 return LIBAVCODEC_VERSION_INT;
2574 const char *avcodec_configuration(void)
2576 return FFMPEG_CONFIGURATION;
2579 const char *avcodec_license(void)
2581 #define LICENSE_PREFIX "libavcodec license: "
2582 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2585 void avcodec_flush_buffers(AVCodecContext *avctx)
2587 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2588 ff_thread_flush(avctx);
2589 else if (avctx->codec->flush)
2590 avctx->codec->flush(avctx);
2592 avctx->pts_correction_last_pts =
2593 avctx->pts_correction_last_dts = INT64_MIN;
2596 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2599 case AV_CODEC_ID_8SVX_EXP:
2600 case AV_CODEC_ID_8SVX_FIB:
2601 case AV_CODEC_ID_ADPCM_CT:
2602 case AV_CODEC_ID_ADPCM_IMA_APC:
2603 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2604 case AV_CODEC_ID_ADPCM_IMA_OKI:
2605 case AV_CODEC_ID_ADPCM_IMA_WS:
2606 case AV_CODEC_ID_ADPCM_G722:
2607 case AV_CODEC_ID_ADPCM_YAMAHA:
2609 case AV_CODEC_ID_PCM_ALAW:
2610 case AV_CODEC_ID_PCM_MULAW:
2611 case AV_CODEC_ID_PCM_S8:
2612 case AV_CODEC_ID_PCM_S8_PLANAR:
2613 case AV_CODEC_ID_PCM_U8:
2614 case AV_CODEC_ID_PCM_ZORK:
2616 case AV_CODEC_ID_PCM_S16BE:
2617 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2618 case AV_CODEC_ID_PCM_S16LE:
2619 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2620 case AV_CODEC_ID_PCM_U16BE:
2621 case AV_CODEC_ID_PCM_U16LE:
2623 case AV_CODEC_ID_PCM_S24DAUD:
2624 case AV_CODEC_ID_PCM_S24BE:
2625 case AV_CODEC_ID_PCM_S24LE:
2626 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2627 case AV_CODEC_ID_PCM_U24BE:
2628 case AV_CODEC_ID_PCM_U24LE:
2630 case AV_CODEC_ID_PCM_S32BE:
2631 case AV_CODEC_ID_PCM_S32LE:
2632 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2633 case AV_CODEC_ID_PCM_U32BE:
2634 case AV_CODEC_ID_PCM_U32LE:
2635 case AV_CODEC_ID_PCM_F32BE:
2636 case AV_CODEC_ID_PCM_F32LE:
2638 case AV_CODEC_ID_PCM_F64BE:
2639 case AV_CODEC_ID_PCM_F64LE:
2646 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2648 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2649 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2650 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2651 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2652 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2653 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2654 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2655 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2656 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2657 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2658 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2660 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2661 return AV_CODEC_ID_NONE;
2662 if (be < 0 || be > 1)
2664 return map[fmt][be];
2667 int av_get_bits_per_sample(enum AVCodecID codec_id)
2670 case AV_CODEC_ID_ADPCM_SBPRO_2:
2672 case AV_CODEC_ID_ADPCM_SBPRO_3:
2674 case AV_CODEC_ID_ADPCM_SBPRO_4:
2675 case AV_CODEC_ID_ADPCM_IMA_WAV:
2676 case AV_CODEC_ID_ADPCM_IMA_QT:
2677 case AV_CODEC_ID_ADPCM_SWF:
2678 case AV_CODEC_ID_ADPCM_MS:
2681 return av_get_exact_bits_per_sample(codec_id);
2685 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2687 int id, sr, ch, ba, tag, bps;
2689 id = avctx->codec_id;
2690 sr = avctx->sample_rate;
2691 ch = avctx->channels;
2692 ba = avctx->block_align;
2693 tag = avctx->codec_tag;
2694 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2696 /* codecs with an exact constant bits per sample */
2697 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2698 return (frame_bytes * 8LL) / (bps * ch);
2699 bps = avctx->bits_per_coded_sample;
2701 /* codecs with a fixed packet duration */
2703 case AV_CODEC_ID_ADPCM_ADX: return 32;
2704 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2705 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2706 case AV_CODEC_ID_AMR_NB:
2707 case AV_CODEC_ID_EVRC:
2708 case AV_CODEC_ID_GSM:
2709 case AV_CODEC_ID_QCELP:
2710 case AV_CODEC_ID_RA_288: return 160;
2711 case AV_CODEC_ID_AMR_WB:
2712 case AV_CODEC_ID_GSM_MS: return 320;
2713 case AV_CODEC_ID_MP1: return 384;
2714 case AV_CODEC_ID_ATRAC1: return 512;
2715 case AV_CODEC_ID_ATRAC3: return 1024;
2716 case AV_CODEC_ID_MP2:
2717 case AV_CODEC_ID_MUSEPACK7: return 1152;
2718 case AV_CODEC_ID_AC3: return 1536;
2722 /* calc from sample rate */
2723 if (id == AV_CODEC_ID_TTA)
2724 return 256 * sr / 245;
2727 /* calc from sample rate and channels */
2728 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2729 return (480 << (sr / 22050)) / ch;
2734 /* calc from block_align */
2735 if (id == AV_CODEC_ID_SIPR) {
2737 case 20: return 160;
2738 case 19: return 144;
2739 case 29: return 288;
2740 case 37: return 480;
2742 } else if (id == AV_CODEC_ID_ILBC) {
2744 case 38: return 160;
2745 case 50: return 240;
2750 if (frame_bytes > 0) {
2751 /* calc from frame_bytes only */
2752 if (id == AV_CODEC_ID_TRUESPEECH)
2753 return 240 * (frame_bytes / 32);
2754 if (id == AV_CODEC_ID_NELLYMOSER)
2755 return 256 * (frame_bytes / 64);
2756 if (id == AV_CODEC_ID_RA_144)
2757 return 160 * (frame_bytes / 20);
2758 if (id == AV_CODEC_ID_G723_1)
2759 return 240 * (frame_bytes / 24);
2762 /* calc from frame_bytes and bits_per_coded_sample */
2763 if (id == AV_CODEC_ID_ADPCM_G726)
2764 return frame_bytes * 8 / bps;
2768 /* calc from frame_bytes and channels */
2770 case AV_CODEC_ID_ADPCM_AFC:
2771 return frame_bytes / (9 * ch) * 16;
2772 case AV_CODEC_ID_ADPCM_4XM:
2773 case AV_CODEC_ID_ADPCM_IMA_ISS:
2774 return (frame_bytes - 4 * ch) * 2 / ch;
2775 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2776 return (frame_bytes - 4) * 2 / ch;
2777 case AV_CODEC_ID_ADPCM_IMA_AMV:
2778 return (frame_bytes - 8) * 2 / ch;
2779 case AV_CODEC_ID_ADPCM_XA:
2780 return (frame_bytes / 128) * 224 / ch;
2781 case AV_CODEC_ID_INTERPLAY_DPCM:
2782 return (frame_bytes - 6 - ch) / ch;
2783 case AV_CODEC_ID_ROQ_DPCM:
2784 return (frame_bytes - 8) / ch;
2785 case AV_CODEC_ID_XAN_DPCM:
2786 return (frame_bytes - 2 * ch) / ch;
2787 case AV_CODEC_ID_MACE3:
2788 return 3 * frame_bytes / ch;
2789 case AV_CODEC_ID_MACE6:
2790 return 6 * frame_bytes / ch;
2791 case AV_CODEC_ID_PCM_LXF:
2792 return 2 * (frame_bytes / (5 * ch));
2793 case AV_CODEC_ID_IAC:
2794 case AV_CODEC_ID_IMC:
2795 return 4 * frame_bytes / ch;
2799 /* calc from frame_bytes, channels, and codec_tag */
2800 if (id == AV_CODEC_ID_SOL_DPCM) {
2802 return frame_bytes / ch;
2804 return frame_bytes * 2 / ch;
2809 /* calc from frame_bytes, channels, and block_align */
2810 int blocks = frame_bytes / ba;
2811 switch (avctx->codec_id) {
2812 case AV_CODEC_ID_ADPCM_IMA_WAV:
2813 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2814 case AV_CODEC_ID_ADPCM_IMA_DK3:
2815 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2816 case AV_CODEC_ID_ADPCM_IMA_DK4:
2817 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2818 case AV_CODEC_ID_ADPCM_MS:
2819 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2824 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2825 switch (avctx->codec_id) {
2826 case AV_CODEC_ID_PCM_DVD:
2829 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2830 case AV_CODEC_ID_PCM_BLURAY:
2833 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2834 case AV_CODEC_ID_S302M:
2835 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2845 int ff_thread_init(AVCodecContext *s)
2852 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2866 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2869 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2873 #if FF_API_MISSING_SAMPLE
2874 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2876 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2877 "version to the newest one from Git. If the problem still "
2878 "occurs, it means that your file has a feature which has not "
2879 "been implemented.\n", feature);
2881 av_log_ask_for_sample(avc, NULL);
2884 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2886 va_list argument_list;
2888 va_start(argument_list, msg);
2891 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2892 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2893 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2894 "and contact the ffmpeg-devel mailing list.\n");
2896 va_end(argument_list);
2898 #endif /* FF_API_MISSING_SAMPLE */
2900 static AVHWAccel *first_hwaccel = NULL;
2902 void av_register_hwaccel(AVHWAccel *hwaccel)
2904 AVHWAccel **p = &first_hwaccel;
2908 hwaccel->next = NULL;
2911 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2913 return hwaccel ? hwaccel->next : first_hwaccel;
2916 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2918 AVHWAccel *hwaccel = NULL;
2920 while ((hwaccel = av_hwaccel_next(hwaccel)))
2921 if (hwaccel->id == codec_id
2922 && hwaccel->pix_fmt == pix_fmt)
2927 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2929 if (ff_lockmgr_cb) {
2930 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2932 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2938 if (ff_lockmgr_cb) {
2939 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2941 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2947 int ff_lock_avcodec(AVCodecContext *log_ctx)
2949 if (ff_lockmgr_cb) {
2950 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
2953 entangled_thread_counter++;
2954 if (entangled_thread_counter != 1) {
2955 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
2956 ff_avcodec_locked = 1;
2957 ff_unlock_avcodec();
2958 return AVERROR(EINVAL);
2960 av_assert0(!ff_avcodec_locked);
2961 ff_avcodec_locked = 1;
2965 int ff_unlock_avcodec(void)
2967 av_assert0(ff_avcodec_locked);
2968 ff_avcodec_locked = 0;
2969 entangled_thread_counter--;
2970 if (ff_lockmgr_cb) {
2971 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
2977 int avpriv_lock_avformat(void)
2979 if (ff_lockmgr_cb) {
2980 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2986 int avpriv_unlock_avformat(void)
2988 if (ff_lockmgr_cb) {
2989 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2995 unsigned int avpriv_toupper4(unsigned int x)
2997 return av_toupper(x & 0xFF) +
2998 (av_toupper((x >> 8) & 0xFF) << 8) +
2999 (av_toupper((x >> 16) & 0xFF) << 16) +
3000 (av_toupper((x >> 24) & 0xFF) << 24);
3003 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3007 dst->owner = src->owner;
3009 ret = av_frame_ref(dst->f, src->f);
3013 if (src->progress &&
3014 !(dst->progress = av_buffer_ref(src->progress))) {
3015 ff_thread_release_buffer(dst->owner, dst);
3016 return AVERROR(ENOMEM);
3024 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3027 return ff_get_buffer(avctx, f->f, flags);
3030 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3032 av_frame_unref(f->f);
3035 void ff_thread_finish_setup(AVCodecContext *avctx)
3039 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3043 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3047 int ff_thread_can_start_frame(AVCodecContext *avctx)
3054 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3056 AVCodec *c= avcodec_find_decoder(codec_id);
3058 c= avcodec_find_encoder(codec_id);
3062 if (codec_id <= AV_CODEC_ID_NONE)
3063 return AVMEDIA_TYPE_UNKNOWN;
3064 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3065 return AVMEDIA_TYPE_VIDEO;
3066 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3067 return AVMEDIA_TYPE_AUDIO;
3068 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3069 return AVMEDIA_TYPE_SUBTITLE;
3071 return AVMEDIA_TYPE_UNKNOWN;
3074 int avcodec_is_open(AVCodecContext *s)
3076 return !!s->internal;
3079 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3084 ret = av_bprint_finalize(buf, &str);
3087 avctx->extradata = str;
3088 /* Note: the string is NUL terminated (so extradata can be read as a
3089 * string), but the ending character is not accounted in the size (in
3090 * binary formats you are likely not supposed to mux that character). When
3091 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3093 avctx->extradata_size = buf->len;