3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/attributes.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/samplefmt.h"
40 #include "libavutil/dict.h"
43 #include "libavutil/opt.h"
46 #include "bytestream.h"
53 static int volatile entangled_thread_counter = 0;
54 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
55 static void *codec_mutex;
56 static void *avformat_mutex;
58 #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
59 FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
61 return av_fast_realloc(ptr, size, min_size);
64 FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
66 av_fast_malloc(ptr, size, min_size);
70 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
73 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
78 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
80 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
83 /* encoder management */
84 static AVCodec *first_avcodec = NULL;
86 AVCodec *av_codec_next(const AVCodec *c)
94 static av_cold void avcodec_init(void)
96 static int initialized = 0;
103 ff_dsputil_static_init();
106 int av_codec_is_encoder(const AVCodec *codec)
108 return codec && (codec->encode_sub || codec->encode2);
111 int av_codec_is_decoder(const AVCodec *codec)
113 return codec && codec->decode;
116 av_cold void avcodec_register(AVCodec *codec)
126 if (codec->init_static_data)
127 codec->init_static_data(codec);
130 unsigned avcodec_get_edge_width(void)
135 #if FF_API_SET_DIMENSIONS
136 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
138 ff_set_dimensions(s, width, height);
142 int ff_set_dimensions(AVCodecContext *s, int width, int height)
144 int ret = av_image_check_size(width, height, 0, s);
148 s->width = s->coded_width = width;
149 s->height = s->coded_height = height;
154 int ff_side_data_update_matrix_encoding(AVFrame *frame,
155 enum AVMatrixEncoding matrix_encoding)
157 AVFrameSideData *side_data;
158 enum AVMatrixEncoding *data;
160 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
162 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
163 sizeof(enum AVMatrixEncoding));
166 return AVERROR(ENOMEM);
168 data = (enum AVMatrixEncoding*)side_data->data;
169 *data = matrix_encoding;
174 #if HAVE_NEON || ARCH_PPC || HAVE_MMX
175 # define STRIDE_ALIGN 16
177 # define STRIDE_ALIGN 8
180 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
181 int linesize_align[AV_NUM_DATA_POINTERS])
187 switch (s->pix_fmt) {
188 case AV_PIX_FMT_YUV420P:
189 case AV_PIX_FMT_YUYV422:
190 case AV_PIX_FMT_UYVY422:
191 case AV_PIX_FMT_YUV422P:
192 case AV_PIX_FMT_YUV440P:
193 case AV_PIX_FMT_YUV444P:
194 case AV_PIX_FMT_GBRP:
195 case AV_PIX_FMT_GRAY8:
196 case AV_PIX_FMT_GRAY16BE:
197 case AV_PIX_FMT_GRAY16LE:
198 case AV_PIX_FMT_YUVJ420P:
199 case AV_PIX_FMT_YUVJ422P:
200 case AV_PIX_FMT_YUVJ440P:
201 case AV_PIX_FMT_YUVJ444P:
202 case AV_PIX_FMT_YUVA420P:
203 case AV_PIX_FMT_YUVA422P:
204 case AV_PIX_FMT_YUVA444P:
205 case AV_PIX_FMT_YUV420P9LE:
206 case AV_PIX_FMT_YUV420P9BE:
207 case AV_PIX_FMT_YUV420P10LE:
208 case AV_PIX_FMT_YUV420P10BE:
209 case AV_PIX_FMT_YUV422P9LE:
210 case AV_PIX_FMT_YUV422P9BE:
211 case AV_PIX_FMT_YUV422P10LE:
212 case AV_PIX_FMT_YUV422P10BE:
213 case AV_PIX_FMT_YUV444P9LE:
214 case AV_PIX_FMT_YUV444P9BE:
215 case AV_PIX_FMT_YUV444P10LE:
216 case AV_PIX_FMT_YUV444P10BE:
217 case AV_PIX_FMT_GBRP9LE:
218 case AV_PIX_FMT_GBRP9BE:
219 case AV_PIX_FMT_GBRP10LE:
220 case AV_PIX_FMT_GBRP10BE:
221 w_align = 16; //FIXME assume 16 pixel per macroblock
222 h_align = 16 * 2; // interlaced needs 2 macroblocks height
224 case AV_PIX_FMT_YUV411P:
225 case AV_PIX_FMT_UYYVYY411:
229 case AV_PIX_FMT_YUV410P:
230 if (s->codec_id == AV_CODEC_ID_SVQ1) {
234 case AV_PIX_FMT_RGB555:
235 if (s->codec_id == AV_CODEC_ID_RPZA) {
239 case AV_PIX_FMT_PAL8:
240 case AV_PIX_FMT_BGR8:
241 case AV_PIX_FMT_RGB8:
242 if (s->codec_id == AV_CODEC_ID_SMC) {
247 case AV_PIX_FMT_BGR24:
248 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
249 (s->codec_id == AV_CODEC_ID_ZLIB)) {
260 *width = FFALIGN(*width, w_align);
261 *height = FFALIGN(*height, h_align);
262 if (s->codec_id == AV_CODEC_ID_H264)
263 // some of the optimized chroma MC reads one line too much
266 for (i = 0; i < 4; i++)
267 linesize_align[i] = STRIDE_ALIGN;
270 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
272 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
273 int chroma_shift = desc->log2_chroma_w;
274 int linesize_align[AV_NUM_DATA_POINTERS];
277 avcodec_align_dimensions2(s, width, height, linesize_align);
278 align = FFMAX(linesize_align[0], linesize_align[3]);
279 linesize_align[1] <<= chroma_shift;
280 linesize_align[2] <<= chroma_shift;
281 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
282 *width = FFALIGN(*width, align);
285 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
286 enum AVSampleFormat sample_fmt, const uint8_t *buf,
287 int buf_size, int align)
289 int ch, planar, needed_size, ret = 0;
291 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
292 frame->nb_samples, sample_fmt,
294 if (buf_size < needed_size)
295 return AVERROR(EINVAL);
297 planar = av_sample_fmt_is_planar(sample_fmt);
298 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
299 if (!(frame->extended_data = av_mallocz(nb_channels *
300 sizeof(*frame->extended_data))))
301 return AVERROR(ENOMEM);
303 frame->extended_data = frame->data;
306 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
307 buf, nb_channels, frame->nb_samples,
308 sample_fmt, align)) < 0) {
309 if (frame->extended_data != frame->data)
310 av_free(frame->extended_data);
313 if (frame->extended_data != frame->data) {
314 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
315 frame->data[ch] = frame->extended_data[ch];
321 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
323 FramePool *pool = avctx->internal->pool;
326 switch (avctx->codec_type) {
327 case AVMEDIA_TYPE_VIDEO: {
330 int w = frame->width;
331 int h = frame->height;
332 int tmpsize, unaligned;
334 if (pool->format == frame->format &&
335 pool->width == frame->width && pool->height == frame->height)
338 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
340 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
346 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
347 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
348 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
349 // increase alignment of w for next try (rhs gives the lowest bit set in w)
353 for (i = 0; i < 4; i++)
354 unaligned |= picture.linesize[i] % pool->stride_align[i];
357 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
358 NULL, picture.linesize);
362 for (i = 0; i < 3 && picture.data[i + 1]; i++)
363 size[i] = picture.data[i + 1] - picture.data[i];
364 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
366 for (i = 0; i < 4; i++) {
367 av_buffer_pool_uninit(&pool->pools[i]);
368 pool->linesize[i] = picture.linesize[i];
370 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
371 if (!pool->pools[i]) {
372 ret = AVERROR(ENOMEM);
377 pool->format = frame->format;
378 pool->width = frame->width;
379 pool->height = frame->height;
383 case AVMEDIA_TYPE_AUDIO: {
384 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
385 int planar = av_sample_fmt_is_planar(frame->format);
386 int planes = planar ? ch : 1;
388 if (pool->format == frame->format && pool->planes == planes &&
389 pool->channels == ch && frame->nb_samples == pool->samples)
392 av_buffer_pool_uninit(&pool->pools[0]);
393 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
394 frame->nb_samples, frame->format, 0);
398 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
399 if (!pool->pools[0]) {
400 ret = AVERROR(ENOMEM);
404 pool->format = frame->format;
405 pool->planes = planes;
407 pool->samples = frame->nb_samples;
410 default: av_assert0(0);
414 for (i = 0; i < 4; i++)
415 av_buffer_pool_uninit(&pool->pools[i]);
417 pool->planes = pool->channels = pool->samples = 0;
418 pool->width = pool->height = 0;
422 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
424 FramePool *pool = avctx->internal->pool;
425 int planes = pool->planes;
428 frame->linesize[0] = pool->linesize[0];
430 if (planes > AV_NUM_DATA_POINTERS) {
431 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
432 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
433 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
434 sizeof(*frame->extended_buf));
435 if (!frame->extended_data || !frame->extended_buf) {
436 av_freep(&frame->extended_data);
437 av_freep(&frame->extended_buf);
438 return AVERROR(ENOMEM);
441 frame->extended_data = frame->data;
443 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
444 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
447 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
449 for (i = 0; i < frame->nb_extended_buf; i++) {
450 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
451 if (!frame->extended_buf[i])
453 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
456 if (avctx->debug & FF_DEBUG_BUFFERS)
457 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
461 av_frame_unref(frame);
462 return AVERROR(ENOMEM);
465 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
467 FramePool *pool = s->internal->pool;
468 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
469 int pixel_size = desc->comp[0].step_minus1 + 1;
470 int h_chroma_shift, v_chroma_shift;
473 if (pic->data[0] != NULL) {
474 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
478 memset(pic->data, 0, sizeof(pic->data));
479 pic->extended_data = pic->data;
481 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
483 for (i = 0; i < 4 && pool->pools[i]; i++) {
484 const int h_shift = i == 0 ? 0 : h_chroma_shift;
485 const int v_shift = i == 0 ? 0 : v_chroma_shift;
487 pic->linesize[i] = pool->linesize[i];
489 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
493 // no edge if EDGE EMU or not planar YUV
494 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
495 pic->data[i] = pic->buf[i]->data;
497 pic->data[i] = pic->buf[i]->data +
498 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
499 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
502 for (; i < AV_NUM_DATA_POINTERS; i++) {
504 pic->linesize[i] = 0;
506 if (pic->data[1] && !pic->data[2])
507 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
509 if (s->debug & FF_DEBUG_BUFFERS)
510 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
515 return AVERROR(ENOMEM);
518 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
522 if ((ret = update_frame_pool(avctx, frame)) < 0)
525 #if FF_API_GET_BUFFER
526 FF_DISABLE_DEPRECATION_WARNINGS
527 frame->type = FF_BUFFER_TYPE_INTERNAL;
528 FF_ENABLE_DEPRECATION_WARNINGS
531 switch (avctx->codec_type) {
532 case AVMEDIA_TYPE_VIDEO:
533 return video_get_buffer(avctx, frame);
534 case AVMEDIA_TYPE_AUDIO:
535 return audio_get_buffer(avctx, frame);
541 #if FF_API_GET_BUFFER
542 FF_DISABLE_DEPRECATION_WARNINGS
543 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
545 return avcodec_default_get_buffer2(avctx, frame, 0);
548 typedef struct CompatReleaseBufPriv {
549 AVCodecContext avctx;
551 } CompatReleaseBufPriv;
553 static void compat_free_buffer(void *opaque, uint8_t *data)
555 CompatReleaseBufPriv *priv = opaque;
556 if (priv->avctx.release_buffer)
557 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
561 static void compat_release_buffer(void *opaque, uint8_t *data)
563 AVBufferRef *buf = opaque;
564 av_buffer_unref(&buf);
566 FF_ENABLE_DEPRECATION_WARNINGS
569 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
573 switch (avctx->codec_type) {
574 case AVMEDIA_TYPE_VIDEO:
575 frame->width = FFMAX(avctx->width, avctx->coded_width);
576 frame->height = FFMAX(avctx->height, avctx->coded_height);
577 if (frame->format < 0)
578 frame->format = avctx->pix_fmt;
579 if (!frame->sample_aspect_ratio.num)
580 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
582 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
585 case AVMEDIA_TYPE_AUDIO:
586 if (!frame->sample_rate)
587 frame->sample_rate = avctx->sample_rate;
588 if (frame->format < 0)
589 frame->format = avctx->sample_fmt;
590 if (!frame->channel_layout) {
591 if (avctx->channel_layout) {
592 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
594 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
596 return AVERROR(EINVAL);
599 frame->channel_layout = avctx->channel_layout;
601 if (avctx->channels > FF_SANE_NB_CHANNELS) {
602 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
604 return AVERROR(ENOSYS);
607 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
608 if (!frame->channel_layout)
609 frame->channel_layout = (1ULL << avctx->channels) - 1;
613 default: return AVERROR(EINVAL);
616 frame->pkt_pts = avctx->internal->pkt ? avctx->internal->pkt->pts : AV_NOPTS_VALUE;
617 frame->reordered_opaque = avctx->reordered_opaque;
619 #if FF_API_GET_BUFFER
620 FF_DISABLE_DEPRECATION_WARNINGS
622 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
623 * We wrap each plane in its own AVBuffer. Each of those has a reference to
624 * a dummy AVBuffer as its private data, unreffing it on free.
625 * When all the planes are freed, the dummy buffer's free callback calls
628 if (avctx->get_buffer) {
629 CompatReleaseBufPriv *priv = NULL;
630 AVBufferRef *dummy_buf = NULL;
633 if (flags & AV_GET_BUFFER_FLAG_REF)
634 frame->reference = 1;
636 ret = avctx->get_buffer(avctx, frame);
640 /* return if the buffers are already set up
641 * this would happen e.g. when a custom get_buffer() calls
642 * avcodec_default_get_buffer
647 priv = av_mallocz(sizeof(*priv));
649 ret = AVERROR(ENOMEM);
652 priv->avctx = *avctx;
653 priv->frame = *frame;
655 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
657 ret = AVERROR(ENOMEM);
661 #define WRAP_PLANE(ref_out, data, data_size) \
663 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
665 ret = AVERROR(ENOMEM); \
668 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
671 av_frame_unref(frame); \
672 ret = AVERROR(ENOMEM); \
677 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
678 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
680 planes = av_pix_fmt_count_planes(frame->format);
681 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
682 check for allocated buffers: make libavcodec happy */
683 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
685 if (!desc || planes <= 0) {
686 ret = AVERROR(EINVAL);
690 for (i = 0; i < planes; i++) {
691 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
692 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
694 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
697 int planar = av_sample_fmt_is_planar(frame->format);
698 planes = planar ? avctx->channels : 1;
700 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
701 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
702 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
703 frame->nb_extended_buf);
704 if (!frame->extended_buf) {
705 ret = AVERROR(ENOMEM);
710 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
711 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
713 for (i = 0; i < frame->nb_extended_buf; i++)
714 WRAP_PLANE(frame->extended_buf[i],
715 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
719 av_buffer_unref(&dummy_buf);
721 frame->width = avctx->width;
722 frame->height = avctx->height;
727 avctx->release_buffer(avctx, frame);
729 av_buffer_unref(&dummy_buf);
732 FF_ENABLE_DEPRECATION_WARNINGS
735 ret = avctx->get_buffer2(avctx, frame, flags);
737 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
738 frame->width = avctx->width;
739 frame->height = avctx->height;
745 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
750 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
753 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
755 if (av_frame_is_writable(frame))
758 av_frame_move_ref(&tmp, frame);
760 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
762 av_frame_unref(&tmp);
766 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
767 frame->format, frame->width, frame->height);
769 av_frame_unref(&tmp);
774 #if FF_API_GET_BUFFER
775 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
780 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
787 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
791 for (i = 0; i < count; i++) {
792 int r = func(c, (char *)arg + i * size);
799 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
803 for (i = 0; i < count; i++) {
804 int r = func(c, arg, i, 0);
811 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
813 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
814 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
817 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
819 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
824 #if FF_API_AVFRAME_LAVC
825 void avcodec_get_frame_defaults(AVFrame *frame)
827 if (frame->extended_data != frame->data)
828 av_freep(&frame->extended_data);
830 memset(frame, 0, sizeof(AVFrame));
832 frame->pts = AV_NOPTS_VALUE;
833 frame->key_frame = 1;
834 frame->sample_aspect_ratio = (AVRational) {0, 1 };
835 frame->format = -1; /* unknown */
836 frame->extended_data = frame->data;
839 AVFrame *avcodec_alloc_frame(void)
841 AVFrame *frame = av_mallocz(sizeof(AVFrame));
846 avcodec_get_frame_defaults(frame);
851 void avcodec_free_frame(AVFrame **frame)
853 av_frame_free(frame);
857 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
860 AVDictionary *tmp = NULL;
862 if (avcodec_is_open(avctx))
865 if ((!codec && !avctx->codec)) {
866 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
867 return AVERROR(EINVAL);
869 if ((codec && avctx->codec && codec != avctx->codec)) {
870 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
871 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
872 return AVERROR(EINVAL);
875 codec = avctx->codec;
877 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
878 return AVERROR(EINVAL);
881 av_dict_copy(&tmp, *options, 0);
883 /* If there is a user-supplied mutex locking routine, call it. */
885 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
889 entangled_thread_counter++;
890 if (entangled_thread_counter != 1) {
891 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
896 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
897 if (!avctx->internal) {
898 ret = AVERROR(ENOMEM);
902 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
903 if (!avctx->internal->pool) {
904 ret = AVERROR(ENOMEM);
908 avctx->internal->to_free = av_frame_alloc();
909 if (!avctx->internal->to_free) {
910 ret = AVERROR(ENOMEM);
914 if (codec->priv_data_size > 0) {
915 if (!avctx->priv_data) {
916 avctx->priv_data = av_mallocz(codec->priv_data_size);
917 if (!avctx->priv_data) {
918 ret = AVERROR(ENOMEM);
921 if (codec->priv_class) {
922 *(const AVClass **)avctx->priv_data = codec->priv_class;
923 av_opt_set_defaults(avctx->priv_data);
926 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
929 avctx->priv_data = NULL;
931 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
934 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
935 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
936 else if (avctx->width && avctx->height)
937 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
941 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
942 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
943 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
944 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
945 ff_set_dimensions(avctx, 0, 0);
948 /* if the decoder init function was already called previously,
949 * free the already allocated subtitle_header before overwriting it */
950 if (av_codec_is_decoder(codec))
951 av_freep(&avctx->subtitle_header);
953 if (avctx->channels > FF_SANE_NB_CHANNELS) {
954 ret = AVERROR(EINVAL);
958 avctx->codec = codec;
959 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
960 avctx->codec_id == AV_CODEC_ID_NONE) {
961 avctx->codec_type = codec->type;
962 avctx->codec_id = codec->id;
964 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
965 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
966 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
967 ret = AVERROR(EINVAL);
970 avctx->frame_number = 0;
972 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
973 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
974 ret = AVERROR_EXPERIMENTAL;
978 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
979 (!avctx->time_base.num || !avctx->time_base.den)) {
980 avctx->time_base.num = 1;
981 avctx->time_base.den = avctx->sample_rate;
985 ret = ff_thread_init(avctx);
990 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
991 avctx->thread_count = 1;
993 if (av_codec_is_encoder(avctx->codec)) {
995 if (avctx->codec->sample_fmts) {
996 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
997 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
999 if (avctx->channels == 1 &&
1000 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1001 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1002 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1006 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1007 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1008 ret = AVERROR(EINVAL);
1012 if (avctx->codec->pix_fmts) {
1013 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1014 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1016 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1017 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1018 ret = AVERROR(EINVAL);
1022 if (avctx->codec->supported_samplerates) {
1023 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1024 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1026 if (avctx->codec->supported_samplerates[i] == 0) {
1027 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1028 ret = AVERROR(EINVAL);
1032 if (avctx->codec->channel_layouts) {
1033 if (!avctx->channel_layout) {
1034 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1036 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1037 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1039 if (avctx->codec->channel_layouts[i] == 0) {
1040 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1041 ret = AVERROR(EINVAL);
1046 if (avctx->channel_layout && avctx->channels) {
1047 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1048 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1049 ret = AVERROR(EINVAL);
1052 } else if (avctx->channel_layout) {
1053 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1056 if (!avctx->rc_initial_buffer_occupancy)
1057 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1060 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1061 ret = avctx->codec->init(avctx);
1067 if (av_codec_is_decoder(avctx->codec)) {
1068 /* validate channel layout from the decoder */
1069 if (avctx->channel_layout) {
1070 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1071 if (!avctx->channels)
1072 avctx->channels = channels;
1073 else if (channels != avctx->channels) {
1074 av_log(avctx, AV_LOG_WARNING,
1075 "channel layout does not match number of channels\n");
1076 avctx->channel_layout = 0;
1079 if (avctx->channels && avctx->channels < 0 ||
1080 avctx->channels > FF_SANE_NB_CHANNELS) {
1081 ret = AVERROR(EINVAL);
1086 entangled_thread_counter--;
1088 /* Release any user-supplied mutex. */
1090 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1093 av_dict_free(options);
1100 av_freep(&avctx->priv_data);
1101 if (avctx->internal)
1102 av_freep(&avctx->internal->pool);
1103 av_freep(&avctx->internal);
1104 avctx->codec = NULL;
1108 int ff_alloc_packet(AVPacket *avpkt, int size)
1110 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1111 return AVERROR(EINVAL);
1114 AVBufferRef *buf = avpkt->buf;
1115 #if FF_API_DESTRUCT_PACKET
1116 FF_DISABLE_DEPRECATION_WARNINGS
1117 void *destruct = avpkt->destruct;
1118 FF_ENABLE_DEPRECATION_WARNINGS
1121 if (avpkt->size < size)
1122 return AVERROR(EINVAL);
1124 av_init_packet(avpkt);
1125 #if FF_API_DESTRUCT_PACKET
1126 FF_DISABLE_DEPRECATION_WARNINGS
1127 avpkt->destruct = destruct;
1128 FF_ENABLE_DEPRECATION_WARNINGS
1134 return av_new_packet(avpkt, size);
1139 * Pad last frame with silence.
1141 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1143 AVFrame *frame = NULL;
1146 if (!(frame = av_frame_alloc()))
1147 return AVERROR(ENOMEM);
1149 frame->format = src->format;
1150 frame->channel_layout = src->channel_layout;
1151 frame->nb_samples = s->frame_size;
1152 ret = av_frame_get_buffer(frame, 32);
1156 ret = av_frame_copy_props(frame, src);
1160 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1161 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1163 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1164 frame->nb_samples - src->nb_samples,
1165 s->channels, s->sample_fmt)) < 0)
1173 av_frame_free(&frame);
1177 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1179 const AVFrame *frame,
1180 int *got_packet_ptr)
1183 AVFrame *padded_frame = NULL;
1185 int user_packet = !!avpkt->data;
1187 *got_packet_ptr = 0;
1189 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1190 av_free_packet(avpkt);
1191 av_init_packet(avpkt);
1195 /* ensure that extended_data is properly set */
1196 if (frame && !frame->extended_data) {
1197 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1198 avctx->channels > AV_NUM_DATA_POINTERS) {
1199 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1200 "with more than %d channels, but extended_data is not set.\n",
1201 AV_NUM_DATA_POINTERS);
1202 return AVERROR(EINVAL);
1204 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1207 tmp.extended_data = tmp.data;
1211 /* check for valid frame size */
1213 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1214 if (frame->nb_samples > avctx->frame_size)
1215 return AVERROR(EINVAL);
1216 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1217 if (frame->nb_samples < avctx->frame_size &&
1218 !avctx->internal->last_audio_frame) {
1219 ret = pad_last_frame(avctx, &padded_frame, frame);
1223 frame = padded_frame;
1224 avctx->internal->last_audio_frame = 1;
1227 if (frame->nb_samples != avctx->frame_size) {
1228 ret = AVERROR(EINVAL);
1234 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1236 if (*got_packet_ptr) {
1237 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1238 if (avpkt->pts == AV_NOPTS_VALUE)
1239 avpkt->pts = frame->pts;
1240 if (!avpkt->duration)
1241 avpkt->duration = ff_samples_to_time_base(avctx,
1244 avpkt->dts = avpkt->pts;
1249 if (!user_packet && avpkt->size) {
1250 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1252 avpkt->data = avpkt->buf->data;
1255 avctx->frame_number++;
1258 if (ret < 0 || !*got_packet_ptr) {
1259 av_free_packet(avpkt);
1260 av_init_packet(avpkt);
1264 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1265 * this needs to be moved to the encoders, but for now we can do it
1266 * here to simplify things */
1267 avpkt->flags |= AV_PKT_FLAG_KEY;
1270 av_frame_free(&padded_frame);
1275 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1277 const AVFrame *frame,
1278 int *got_packet_ptr)
1281 int user_packet = !!avpkt->data;
1283 *got_packet_ptr = 0;
1285 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1286 av_free_packet(avpkt);
1287 av_init_packet(avpkt);
1292 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1293 return AVERROR(EINVAL);
1295 av_assert0(avctx->codec->encode2);
1297 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1299 if (!*got_packet_ptr)
1301 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1302 avpkt->pts = avpkt->dts = frame->pts;
1304 if (!user_packet && avpkt->size) {
1305 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1307 avpkt->data = avpkt->buf->data;
1310 avctx->frame_number++;
1313 if (ret < 0 || !*got_packet_ptr)
1314 av_free_packet(avpkt);
1320 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1321 const AVSubtitle *sub)
1324 if (sub->start_display_time) {
1325 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1328 if (sub->num_rects == 0 || !sub->rects)
1330 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1331 avctx->frame_number++;
1335 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1338 const uint8_t *data;
1341 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1345 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1346 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1347 "changes, but PARAM_CHANGE side data was sent to it.\n");
1348 return AVERROR(EINVAL);
1354 flags = bytestream_get_le32(&data);
1357 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1360 avctx->channels = bytestream_get_le32(&data);
1363 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1366 avctx->channel_layout = bytestream_get_le64(&data);
1369 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1372 avctx->sample_rate = bytestream_get_le32(&data);
1375 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1378 avctx->width = bytestream_get_le32(&data);
1379 avctx->height = bytestream_get_le32(&data);
1381 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1388 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1389 return AVERROR_INVALIDDATA;
1392 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1396 /* move the original frame to our backup */
1397 av_frame_unref(avci->to_free);
1398 av_frame_move_ref(avci->to_free, frame);
1400 /* now copy everything except the AVBufferRefs back
1401 * note that we make a COPY of the side data, so calling av_frame_free() on
1402 * the caller's frame will work properly */
1403 ret = av_frame_copy_props(frame, avci->to_free);
1407 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1408 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1409 if (avci->to_free->extended_data != avci->to_free->data) {
1410 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1411 int size = planes * sizeof(*frame->extended_data);
1414 av_frame_unref(frame);
1418 frame->extended_data = av_malloc(size);
1419 if (!frame->extended_data) {
1420 av_frame_unref(frame);
1421 return AVERROR(ENOMEM);
1423 memcpy(frame->extended_data, avci->to_free->extended_data,
1426 frame->extended_data = frame->data;
1428 frame->format = avci->to_free->format;
1429 frame->width = avci->to_free->width;
1430 frame->height = avci->to_free->height;
1431 frame->channel_layout = avci->to_free->channel_layout;
1432 frame->nb_samples = avci->to_free->nb_samples;
1437 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1438 int *got_picture_ptr,
1441 AVCodecInternal *avci = avctx->internal;
1444 *got_picture_ptr = 0;
1445 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1448 avctx->internal->pkt = avpkt;
1449 ret = apply_param_change(avctx, avpkt);
1451 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1452 if (avctx->err_recognition & AV_EF_EXPLODE)
1456 av_frame_unref(picture);
1458 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1459 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1460 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1463 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1465 picture->pkt_dts = avpkt->dts;
1466 /* get_buffer is supposed to set frame parameters */
1467 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1468 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1469 picture->width = avctx->width;
1470 picture->height = avctx->height;
1471 picture->format = avctx->pix_fmt;
1475 emms_c(); //needed to avoid an emms_c() call before every return;
1477 if (*got_picture_ptr) {
1478 if (!avctx->refcounted_frames) {
1479 int err = unrefcount_frame(avci, picture);
1484 avctx->frame_number++;
1486 av_frame_unref(picture);
1493 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1498 AVCodecInternal *avci = avctx->internal;
1503 avctx->internal->pkt = avpkt;
1505 if (!avpkt->data && avpkt->size) {
1506 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1507 return AVERROR(EINVAL);
1510 ret = apply_param_change(avctx, avpkt);
1512 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1513 if (avctx->err_recognition & AV_EF_EXPLODE)
1517 av_frame_unref(frame);
1519 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1520 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1521 if (ret >= 0 && *got_frame_ptr) {
1522 avctx->frame_number++;
1523 frame->pkt_dts = avpkt->dts;
1524 if (frame->format == AV_SAMPLE_FMT_NONE)
1525 frame->format = avctx->sample_fmt;
1527 if (!avctx->refcounted_frames) {
1528 int err = unrefcount_frame(avci, frame);
1533 av_frame_unref(frame);
1540 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1546 avctx->internal->pkt = avpkt;
1548 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1550 avctx->frame_number++;
1554 void avsubtitle_free(AVSubtitle *sub)
1558 for (i = 0; i < sub->num_rects; i++) {
1559 av_freep(&sub->rects[i]->pict.data[0]);
1560 av_freep(&sub->rects[i]->pict.data[1]);
1561 av_freep(&sub->rects[i]->pict.data[2]);
1562 av_freep(&sub->rects[i]->pict.data[3]);
1563 av_freep(&sub->rects[i]->text);
1564 av_freep(&sub->rects[i]->ass);
1565 av_freep(&sub->rects[i]);
1568 av_freep(&sub->rects);
1570 memset(sub, 0, sizeof(AVSubtitle));
1573 av_cold int avcodec_close(AVCodecContext *avctx)
1575 /* If there is a user-supplied mutex locking routine, call it. */
1577 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1581 entangled_thread_counter++;
1582 if (entangled_thread_counter != 1) {
1583 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1584 entangled_thread_counter--;
1588 if (avcodec_is_open(avctx)) {
1589 FramePool *pool = avctx->internal->pool;
1591 if (HAVE_THREADS && avctx->internal->thread_ctx)
1592 ff_thread_free(avctx);
1593 if (avctx->codec && avctx->codec->close)
1594 avctx->codec->close(avctx);
1595 avctx->coded_frame = NULL;
1596 av_frame_free(&avctx->internal->to_free);
1597 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1598 av_buffer_pool_uninit(&pool->pools[i]);
1599 av_freep(&avctx->internal->pool);
1600 av_freep(&avctx->internal);
1603 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1604 av_opt_free(avctx->priv_data);
1606 av_freep(&avctx->priv_data);
1607 if (av_codec_is_encoder(avctx->codec))
1608 av_freep(&avctx->extradata);
1609 avctx->codec = NULL;
1610 avctx->active_thread_type = 0;
1611 entangled_thread_counter--;
1613 /* Release any user-supplied mutex. */
1615 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1620 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1622 AVCodec *p, *experimental = NULL;
1625 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1627 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1634 return experimental;
1637 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1639 return find_encdec(id, 1);
1642 AVCodec *avcodec_find_encoder_by_name(const char *name)
1649 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1656 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1658 return find_encdec(id, 0);
1661 AVCodec *avcodec_find_decoder_by_name(const char *name)
1668 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1675 static int get_bit_rate(AVCodecContext *ctx)
1678 int bits_per_sample;
1680 switch (ctx->codec_type) {
1681 case AVMEDIA_TYPE_VIDEO:
1682 case AVMEDIA_TYPE_DATA:
1683 case AVMEDIA_TYPE_SUBTITLE:
1684 case AVMEDIA_TYPE_ATTACHMENT:
1685 bit_rate = ctx->bit_rate;
1687 case AVMEDIA_TYPE_AUDIO:
1688 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1689 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1698 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1700 int i, len, ret = 0;
1702 #define TAG_PRINT(x) \
1703 (((x) >= '0' && (x) <= '9') || \
1704 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1705 ((x) == '.' || (x) == ' '))
1707 for (i = 0; i < 4; i++) {
1708 len = snprintf(buf, buf_size,
1709 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1711 buf_size = buf_size > len ? buf_size - len : 0;
1718 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1720 const char *codec_name;
1721 const char *profile = NULL;
1725 AVRational display_aspect_ratio;
1730 p = avcodec_find_encoder(enc->codec_id);
1732 p = avcodec_find_decoder(enc->codec_id);
1735 codec_name = p->name;
1736 profile = av_get_profile_name(p, enc->profile);
1737 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1738 /* fake mpeg2 transport stream codec (currently not
1740 codec_name = "mpeg2ts";
1741 } else if (enc->codec_name[0] != '\0') {
1742 codec_name = enc->codec_name;
1744 /* output avi tags */
1746 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1747 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1751 switch (enc->codec_type) {
1752 case AVMEDIA_TYPE_VIDEO:
1753 snprintf(buf, buf_size,
1755 codec_name, enc->mb_decision ? " (hq)" : "");
1757 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1759 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1760 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1762 av_get_pix_fmt_name(enc->pix_fmt));
1765 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1767 enc->width, enc->height);
1768 if (enc->sample_aspect_ratio.num) {
1769 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1770 enc->width * enc->sample_aspect_ratio.num,
1771 enc->height * enc->sample_aspect_ratio.den,
1773 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1774 " [PAR %d:%d DAR %d:%d]",
1775 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1776 display_aspect_ratio.num, display_aspect_ratio.den);
1778 if (av_log_get_level() >= AV_LOG_DEBUG) {
1779 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1780 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1782 enc->time_base.num / g, enc->time_base.den / g);
1786 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1787 ", q=%d-%d", enc->qmin, enc->qmax);
1790 case AVMEDIA_TYPE_AUDIO:
1791 snprintf(buf, buf_size,
1795 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1797 if (enc->sample_rate) {
1798 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1799 ", %d Hz", enc->sample_rate);
1801 av_strlcat(buf, ", ", buf_size);
1802 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1803 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1804 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1805 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1808 case AVMEDIA_TYPE_DATA:
1809 snprintf(buf, buf_size, "Data: %s", codec_name);
1811 case AVMEDIA_TYPE_SUBTITLE:
1812 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1814 case AVMEDIA_TYPE_ATTACHMENT:
1815 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1818 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1822 if (enc->flags & CODEC_FLAG_PASS1)
1823 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1825 if (enc->flags & CODEC_FLAG_PASS2)
1826 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1829 bitrate = get_bit_rate(enc);
1831 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1832 ", %d kb/s", bitrate / 1000);
1836 const char *av_get_profile_name(const AVCodec *codec, int profile)
1839 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1842 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1843 if (p->profile == profile)
1849 unsigned avcodec_version(void)
1851 return LIBAVCODEC_VERSION_INT;
1854 const char *avcodec_configuration(void)
1856 return LIBAV_CONFIGURATION;
1859 const char *avcodec_license(void)
1861 #define LICENSE_PREFIX "libavcodec license: "
1862 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1865 void avcodec_flush_buffers(AVCodecContext *avctx)
1867 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1868 ff_thread_flush(avctx);
1869 else if (avctx->codec->flush)
1870 avctx->codec->flush(avctx);
1872 if (!avctx->refcounted_frames)
1873 av_frame_unref(avctx->internal->to_free);
1876 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1879 case AV_CODEC_ID_ADPCM_CT:
1880 case AV_CODEC_ID_ADPCM_IMA_APC:
1881 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1882 case AV_CODEC_ID_ADPCM_IMA_WS:
1883 case AV_CODEC_ID_ADPCM_G722:
1884 case AV_CODEC_ID_ADPCM_YAMAHA:
1886 case AV_CODEC_ID_PCM_ALAW:
1887 case AV_CODEC_ID_PCM_MULAW:
1888 case AV_CODEC_ID_PCM_S8:
1889 case AV_CODEC_ID_PCM_U8:
1890 case AV_CODEC_ID_PCM_ZORK:
1892 case AV_CODEC_ID_PCM_S16BE:
1893 case AV_CODEC_ID_PCM_S16LE:
1894 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1895 case AV_CODEC_ID_PCM_U16BE:
1896 case AV_CODEC_ID_PCM_U16LE:
1898 case AV_CODEC_ID_PCM_S24DAUD:
1899 case AV_CODEC_ID_PCM_S24BE:
1900 case AV_CODEC_ID_PCM_S24LE:
1901 case AV_CODEC_ID_PCM_S24LE_PLANAR:
1902 case AV_CODEC_ID_PCM_U24BE:
1903 case AV_CODEC_ID_PCM_U24LE:
1905 case AV_CODEC_ID_PCM_S32BE:
1906 case AV_CODEC_ID_PCM_S32LE:
1907 case AV_CODEC_ID_PCM_S32LE_PLANAR:
1908 case AV_CODEC_ID_PCM_U32BE:
1909 case AV_CODEC_ID_PCM_U32LE:
1910 case AV_CODEC_ID_PCM_F32BE:
1911 case AV_CODEC_ID_PCM_F32LE:
1913 case AV_CODEC_ID_PCM_F64BE:
1914 case AV_CODEC_ID_PCM_F64LE:
1921 int av_get_bits_per_sample(enum AVCodecID codec_id)
1924 case AV_CODEC_ID_ADPCM_SBPRO_2:
1926 case AV_CODEC_ID_ADPCM_SBPRO_3:
1928 case AV_CODEC_ID_ADPCM_SBPRO_4:
1929 case AV_CODEC_ID_ADPCM_IMA_WAV:
1930 case AV_CODEC_ID_ADPCM_IMA_QT:
1931 case AV_CODEC_ID_ADPCM_SWF:
1932 case AV_CODEC_ID_ADPCM_MS:
1935 return av_get_exact_bits_per_sample(codec_id);
1939 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1941 int id, sr, ch, ba, tag, bps;
1943 id = avctx->codec_id;
1944 sr = avctx->sample_rate;
1945 ch = avctx->channels;
1946 ba = avctx->block_align;
1947 tag = avctx->codec_tag;
1948 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1950 /* codecs with an exact constant bits per sample */
1951 if (bps > 0 && ch > 0 && frame_bytes > 0)
1952 return (frame_bytes * 8) / (bps * ch);
1953 bps = avctx->bits_per_coded_sample;
1955 /* codecs with a fixed packet duration */
1957 case AV_CODEC_ID_ADPCM_ADX: return 32;
1958 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1959 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1960 case AV_CODEC_ID_AMR_NB:
1961 case AV_CODEC_ID_GSM:
1962 case AV_CODEC_ID_QCELP:
1963 case AV_CODEC_ID_RA_144:
1964 case AV_CODEC_ID_RA_288: return 160;
1965 case AV_CODEC_ID_IMC: return 256;
1966 case AV_CODEC_ID_AMR_WB:
1967 case AV_CODEC_ID_GSM_MS: return 320;
1968 case AV_CODEC_ID_MP1: return 384;
1969 case AV_CODEC_ID_ATRAC1: return 512;
1970 case AV_CODEC_ID_ATRAC3: return 1024;
1971 case AV_CODEC_ID_MP2:
1972 case AV_CODEC_ID_MUSEPACK7: return 1152;
1973 case AV_CODEC_ID_AC3: return 1536;
1977 /* calc from sample rate */
1978 if (id == AV_CODEC_ID_TTA)
1979 return 256 * sr / 245;
1982 /* calc from sample rate and channels */
1983 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1984 return (480 << (sr / 22050)) / ch;
1989 /* calc from block_align */
1990 if (id == AV_CODEC_ID_SIPR) {
1992 case 20: return 160;
1993 case 19: return 144;
1994 case 29: return 288;
1995 case 37: return 480;
1997 } else if (id == AV_CODEC_ID_ILBC) {
1999 case 38: return 160;
2000 case 50: return 240;
2005 if (frame_bytes > 0) {
2006 /* calc from frame_bytes only */
2007 if (id == AV_CODEC_ID_TRUESPEECH)
2008 return 240 * (frame_bytes / 32);
2009 if (id == AV_CODEC_ID_NELLYMOSER)
2010 return 256 * (frame_bytes / 64);
2013 /* calc from frame_bytes and bits_per_coded_sample */
2014 if (id == AV_CODEC_ID_ADPCM_G726)
2015 return frame_bytes * 8 / bps;
2019 /* calc from frame_bytes and channels */
2021 case AV_CODEC_ID_ADPCM_4XM:
2022 case AV_CODEC_ID_ADPCM_IMA_ISS:
2023 return (frame_bytes - 4 * ch) * 2 / ch;
2024 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2025 return (frame_bytes - 4) * 2 / ch;
2026 case AV_CODEC_ID_ADPCM_IMA_AMV:
2027 return (frame_bytes - 8) * 2 / ch;
2028 case AV_CODEC_ID_ADPCM_XA:
2029 return (frame_bytes / 128) * 224 / ch;
2030 case AV_CODEC_ID_INTERPLAY_DPCM:
2031 return (frame_bytes - 6 - ch) / ch;
2032 case AV_CODEC_ID_ROQ_DPCM:
2033 return (frame_bytes - 8) / ch;
2034 case AV_CODEC_ID_XAN_DPCM:
2035 return (frame_bytes - 2 * ch) / ch;
2036 case AV_CODEC_ID_MACE3:
2037 return 3 * frame_bytes / ch;
2038 case AV_CODEC_ID_MACE6:
2039 return 6 * frame_bytes / ch;
2040 case AV_CODEC_ID_PCM_LXF:
2041 return 2 * (frame_bytes / (5 * ch));
2045 /* calc from frame_bytes, channels, and codec_tag */
2046 if (id == AV_CODEC_ID_SOL_DPCM) {
2048 return frame_bytes / ch;
2050 return frame_bytes * 2 / ch;
2055 /* calc from frame_bytes, channels, and block_align */
2056 int blocks = frame_bytes / ba;
2057 switch (avctx->codec_id) {
2058 case AV_CODEC_ID_ADPCM_IMA_WAV:
2059 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2060 case AV_CODEC_ID_ADPCM_IMA_DK3:
2061 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2062 case AV_CODEC_ID_ADPCM_IMA_DK4:
2063 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2064 case AV_CODEC_ID_ADPCM_MS:
2065 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2070 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2071 switch (avctx->codec_id) {
2072 case AV_CODEC_ID_PCM_DVD:
2073 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2074 case AV_CODEC_ID_PCM_BLURAY:
2075 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2076 case AV_CODEC_ID_S302M:
2077 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2087 int ff_thread_init(AVCodecContext *s)
2094 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2108 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2111 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2115 #if FF_API_MISSING_SAMPLE
2116 FF_DISABLE_DEPRECATION_WARNINGS
2117 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2119 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2120 "version to the newest one from Git. If the problem still "
2121 "occurs, it means that your file has a feature which has not "
2122 "been implemented.\n", feature);
2124 av_log_ask_for_sample(avc, NULL);
2127 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2129 va_list argument_list;
2131 va_start(argument_list, msg);
2134 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2135 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2136 "of this file to ftp://upload.libav.org/incoming/ "
2137 "and contact the libav-devel mailing list.\n");
2139 va_end(argument_list);
2141 FF_ENABLE_DEPRECATION_WARNINGS
2142 #endif /* FF_API_MISSING_SAMPLE */
2144 static AVHWAccel *first_hwaccel = NULL;
2146 void av_register_hwaccel(AVHWAccel *hwaccel)
2148 AVHWAccel **p = &first_hwaccel;
2152 hwaccel->next = NULL;
2155 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2157 return hwaccel ? hwaccel->next : first_hwaccel;
2160 AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
2162 enum AVCodecID codec_id = avctx->codec->id;
2163 enum AVPixelFormat pix_fmt = avctx->pix_fmt;
2165 AVHWAccel *hwaccel = NULL;
2167 while ((hwaccel = av_hwaccel_next(hwaccel)))
2168 if (hwaccel->id == codec_id
2169 && hwaccel->pix_fmt == pix_fmt)
2174 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2177 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2179 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2186 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2188 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2194 int avpriv_lock_avformat(void)
2197 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2203 int avpriv_unlock_avformat(void)
2206 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2212 unsigned int avpriv_toupper4(unsigned int x)
2214 return av_toupper(x & 0xFF) +
2215 (av_toupper((x >> 8) & 0xFF) << 8) +
2216 (av_toupper((x >> 16) & 0xFF) << 16) +
2217 (av_toupper((x >> 24) & 0xFF) << 24);
2220 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2224 dst->owner = src->owner;
2226 ret = av_frame_ref(dst->f, src->f);
2230 if (src->progress &&
2231 !(dst->progress = av_buffer_ref(src->progress))) {
2232 ff_thread_release_buffer(dst->owner, dst);
2233 return AVERROR(ENOMEM);
2241 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2244 return ff_get_buffer(avctx, f->f, flags);
2247 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2249 av_frame_unref(f->f);
2252 void ff_thread_finish_setup(AVCodecContext *avctx)
2256 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2260 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2266 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2268 if (codec_id <= AV_CODEC_ID_NONE)
2269 return AVMEDIA_TYPE_UNKNOWN;
2270 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2271 return AVMEDIA_TYPE_VIDEO;
2272 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2273 return AVMEDIA_TYPE_AUDIO;
2274 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2275 return AVMEDIA_TYPE_SUBTITLE;
2277 return AVMEDIA_TYPE_UNKNOWN;
2280 int avcodec_is_open(AVCodecContext *s)
2282 return !!s->internal;
2285 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2287 uint32_t * restrict state)
2295 for (i = 0; i < 3; i++) {
2296 uint32_t tmp = *state << 8;
2297 *state = tmp + *(p++);
2298 if (tmp == 0x100 || p == end)
2303 if (p[-1] > 1 ) p += 3;
2304 else if (p[-2] ) p += 2;
2305 else if (p[-3]|(p[-1]-1)) p++;
2312 p = FFMIN(p, end) - 4;
2313 *state = AV_RB32(p);