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/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/crc.h"
33 #include "libavutil/frame.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/samplefmt.h"
38 #include "libavutil/dict.h"
41 #include "libavutil/opt.h"
44 #include "bytestream.h"
51 static int volatile entangled_thread_counter = 0;
52 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
53 static void *codec_mutex;
54 static void *avformat_mutex;
56 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
61 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
63 ptr = av_realloc(ptr, min_size);
64 /* we could set this to the unmodified min_size but this is safer
65 * if the user lost the ptr and uses NULL now
75 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
80 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
82 *p = av_malloc(min_size);
88 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
91 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
96 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
98 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
101 /* encoder management */
102 static AVCodec *first_avcodec = NULL;
104 AVCodec *av_codec_next(const AVCodec *c)
109 return first_avcodec;
112 static void avcodec_init(void)
114 static int initialized = 0;
116 if (initialized != 0)
121 ff_dsputil_static_init();
124 int av_codec_is_encoder(const AVCodec *codec)
126 return codec && (codec->encode_sub || codec->encode2);
129 int av_codec_is_decoder(const AVCodec *codec)
131 return codec && codec->decode;
134 void avcodec_register(AVCodec *codec)
144 if (codec->init_static_data)
145 codec->init_static_data(codec);
148 unsigned avcodec_get_edge_width(void)
153 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
155 s->coded_width = width;
156 s->coded_height = height;
161 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
162 # define STRIDE_ALIGN 16
164 # define STRIDE_ALIGN 8
167 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
168 int linesize_align[AV_NUM_DATA_POINTERS])
174 switch (s->pix_fmt) {
175 case AV_PIX_FMT_YUV420P:
176 case AV_PIX_FMT_YUYV422:
177 case AV_PIX_FMT_UYVY422:
178 case AV_PIX_FMT_YUV422P:
179 case AV_PIX_FMT_YUV440P:
180 case AV_PIX_FMT_YUV444P:
181 case AV_PIX_FMT_GBRP:
182 case AV_PIX_FMT_GRAY8:
183 case AV_PIX_FMT_GRAY16BE:
184 case AV_PIX_FMT_GRAY16LE:
185 case AV_PIX_FMT_YUVJ420P:
186 case AV_PIX_FMT_YUVJ422P:
187 case AV_PIX_FMT_YUVJ440P:
188 case AV_PIX_FMT_YUVJ444P:
189 case AV_PIX_FMT_YUVA420P:
190 case AV_PIX_FMT_YUVA422P:
191 case AV_PIX_FMT_YUVA444P:
192 case AV_PIX_FMT_YUV420P9LE:
193 case AV_PIX_FMT_YUV420P9BE:
194 case AV_PIX_FMT_YUV420P10LE:
195 case AV_PIX_FMT_YUV420P10BE:
196 case AV_PIX_FMT_YUV422P9LE:
197 case AV_PIX_FMT_YUV422P9BE:
198 case AV_PIX_FMT_YUV422P10LE:
199 case AV_PIX_FMT_YUV422P10BE:
200 case AV_PIX_FMT_YUV444P9LE:
201 case AV_PIX_FMT_YUV444P9BE:
202 case AV_PIX_FMT_YUV444P10LE:
203 case AV_PIX_FMT_YUV444P10BE:
204 case AV_PIX_FMT_GBRP9LE:
205 case AV_PIX_FMT_GBRP9BE:
206 case AV_PIX_FMT_GBRP10LE:
207 case AV_PIX_FMT_GBRP10BE:
208 w_align = 16; //FIXME assume 16 pixel per macroblock
209 h_align = 16 * 2; // interlaced needs 2 macroblocks height
211 case AV_PIX_FMT_YUV411P:
212 case AV_PIX_FMT_UYYVYY411:
216 case AV_PIX_FMT_YUV410P:
217 if (s->codec_id == AV_CODEC_ID_SVQ1) {
221 case AV_PIX_FMT_RGB555:
222 if (s->codec_id == AV_CODEC_ID_RPZA) {
226 case AV_PIX_FMT_PAL8:
227 case AV_PIX_FMT_BGR8:
228 case AV_PIX_FMT_RGB8:
229 if (s->codec_id == AV_CODEC_ID_SMC) {
234 case AV_PIX_FMT_BGR24:
235 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
236 (s->codec_id == AV_CODEC_ID_ZLIB)) {
247 *width = FFALIGN(*width, w_align);
248 *height = FFALIGN(*height, h_align);
249 if (s->codec_id == AV_CODEC_ID_H264)
250 // some of the optimized chroma MC reads one line too much
253 for (i = 0; i < 4; i++)
254 linesize_align[i] = STRIDE_ALIGN;
257 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
259 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
260 int chroma_shift = desc->log2_chroma_w;
261 int linesize_align[AV_NUM_DATA_POINTERS];
264 avcodec_align_dimensions2(s, width, height, linesize_align);
265 align = FFMAX(linesize_align[0], linesize_align[3]);
266 linesize_align[1] <<= chroma_shift;
267 linesize_align[2] <<= chroma_shift;
268 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
269 *width = FFALIGN(*width, align);
272 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
273 enum AVSampleFormat sample_fmt, const uint8_t *buf,
274 int buf_size, int align)
276 int ch, planar, needed_size, ret = 0;
278 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
279 frame->nb_samples, sample_fmt,
281 if (buf_size < needed_size)
282 return AVERROR(EINVAL);
284 planar = av_sample_fmt_is_planar(sample_fmt);
285 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
286 if (!(frame->extended_data = av_mallocz(nb_channels *
287 sizeof(*frame->extended_data))))
288 return AVERROR(ENOMEM);
290 frame->extended_data = frame->data;
293 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
294 buf, nb_channels, frame->nb_samples,
295 sample_fmt, align)) < 0) {
296 if (frame->extended_data != frame->data)
297 av_free(frame->extended_data);
300 if (frame->extended_data != frame->data) {
301 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
302 frame->data[ch] = frame->extended_data[ch];
308 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
310 FramePool *pool = avctx->internal->pool;
313 switch (avctx->codec_type) {
314 case AVMEDIA_TYPE_VIDEO: {
317 int w = frame->width;
318 int h = frame->height;
319 int tmpsize, unaligned;
321 if (pool->format == frame->format &&
322 pool->width == frame->width && pool->height == frame->height)
325 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
327 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
333 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
334 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
335 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
336 // increase alignment of w for next try (rhs gives the lowest bit set in w)
340 for (i = 0; i < 4; i++)
341 unaligned |= picture.linesize[i] % pool->stride_align[i];
344 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
345 NULL, picture.linesize);
349 for (i = 0; i < 3 && picture.data[i + 1]; i++)
350 size[i] = picture.data[i + 1] - picture.data[i];
351 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
353 for (i = 0; i < 4; i++) {
354 av_buffer_pool_uninit(&pool->pools[i]);
355 pool->linesize[i] = picture.linesize[i];
357 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
358 if (!pool->pools[i]) {
359 ret = AVERROR(ENOMEM);
364 pool->format = frame->format;
365 pool->width = frame->width;
366 pool->height = frame->height;
370 case AVMEDIA_TYPE_AUDIO: {
371 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
372 int planar = av_sample_fmt_is_planar(frame->format);
373 int planes = planar ? ch : 1;
375 if (pool->format == frame->format && pool->planes == planes &&
376 pool->channels == ch && frame->nb_samples == pool->samples)
379 av_buffer_pool_uninit(&pool->pools[0]);
380 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
381 frame->nb_samples, frame->format, 0);
385 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
386 if (!pool->pools[0]) {
387 ret = AVERROR(ENOMEM);
391 pool->format = frame->format;
392 pool->planes = planes;
394 pool->samples = frame->nb_samples;
397 default: av_assert0(0);
401 for (i = 0; i < 4; i++)
402 av_buffer_pool_uninit(&pool->pools[i]);
404 pool->planes = pool->channels = pool->samples = 0;
405 pool->width = pool->height = 0;
409 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
411 FramePool *pool = avctx->internal->pool;
412 int planes = pool->planes;
415 frame->linesize[0] = pool->linesize[0];
417 if (planes > AV_NUM_DATA_POINTERS) {
418 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
419 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
420 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
421 sizeof(*frame->extended_buf));
422 if (!frame->extended_data || !frame->extended_buf) {
423 av_freep(&frame->extended_data);
424 av_freep(&frame->extended_buf);
425 return AVERROR(ENOMEM);
428 frame->extended_data = frame->data;
430 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
431 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
434 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
436 for (i = 0; i < frame->nb_extended_buf; i++) {
437 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
438 if (!frame->extended_buf[i])
440 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
443 if (avctx->debug & FF_DEBUG_BUFFERS)
444 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
448 av_frame_unref(frame);
449 return AVERROR(ENOMEM);
452 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
454 FramePool *pool = s->internal->pool;
455 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
456 int pixel_size = desc->comp[0].step_minus1 + 1;
457 int h_chroma_shift, v_chroma_shift;
460 if (pic->data[0] != NULL) {
461 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
465 memset(pic->data, 0, sizeof(pic->data));
466 pic->extended_data = pic->data;
468 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
470 for (i = 0; i < 4 && pool->pools[i]; i++) {
471 const int h_shift = i == 0 ? 0 : h_chroma_shift;
472 const int v_shift = i == 0 ? 0 : v_chroma_shift;
474 pic->linesize[i] = pool->linesize[i];
476 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
480 // no edge if EDGE EMU or not planar YUV
481 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
482 pic->data[i] = pic->buf[i]->data;
484 pic->data[i] = pic->buf[i]->data +
485 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
486 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
489 for (; i < AV_NUM_DATA_POINTERS; i++) {
491 pic->linesize[i] = 0;
493 if (pic->data[1] && !pic->data[2])
494 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
496 if (s->debug & FF_DEBUG_BUFFERS)
497 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
502 return AVERROR(ENOMEM);
505 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
509 if ((ret = update_frame_pool(avctx, frame)) < 0)
512 #if FF_API_GET_BUFFER
513 frame->type = FF_BUFFER_TYPE_INTERNAL;
516 switch (avctx->codec_type) {
517 case AVMEDIA_TYPE_VIDEO:
518 return video_get_buffer(avctx, frame);
519 case AVMEDIA_TYPE_AUDIO:
520 return audio_get_buffer(avctx, frame);
526 #if FF_API_GET_BUFFER
527 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
529 return avcodec_default_get_buffer2(avctx, frame, 0);
532 typedef struct CompatReleaseBufPriv {
533 AVCodecContext avctx;
535 } CompatReleaseBufPriv;
537 static void compat_free_buffer(void *opaque, uint8_t *data)
539 CompatReleaseBufPriv *priv = opaque;
540 if (priv->avctx.release_buffer)
541 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
545 static void compat_release_buffer(void *opaque, uint8_t *data)
547 AVBufferRef *buf = opaque;
548 av_buffer_unref(&buf);
552 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
556 switch (avctx->codec_type) {
557 case AVMEDIA_TYPE_VIDEO:
559 frame->width = avctx->width;
561 frame->height = avctx->height;
562 if (frame->format < 0)
563 frame->format = avctx->pix_fmt;
564 if (!frame->sample_aspect_ratio.num)
565 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
567 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
570 case AVMEDIA_TYPE_AUDIO:
571 if (!frame->sample_rate)
572 frame->sample_rate = avctx->sample_rate;
573 if (frame->format < 0)
574 frame->format = avctx->sample_fmt;
575 if (!frame->channel_layout) {
576 if (avctx->channel_layout) {
577 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
579 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
581 return AVERROR(EINVAL);
584 frame->channel_layout = avctx->channel_layout;
586 if (avctx->channels > FF_SANE_NB_CHANNELS) {
587 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
589 return AVERROR(ENOSYS);
592 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
593 if (!frame->channel_layout)
594 frame->channel_layout = (1ULL << avctx->channels) - 1;
598 default: return AVERROR(EINVAL);
601 frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
602 frame->reordered_opaque = avctx->reordered_opaque;
604 #if FF_API_GET_BUFFER
606 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
607 * We wrap each plane in its own AVBuffer. Each of those has a reference to
608 * a dummy AVBuffer as its private data, unreffing it on free.
609 * When all the planes are freed, the dummy buffer's free callback calls
612 if (avctx->get_buffer) {
613 CompatReleaseBufPriv *priv = NULL;
614 AVBufferRef *dummy_buf = NULL;
617 if (flags & AV_GET_BUFFER_FLAG_REF)
618 frame->reference = 1;
620 ret = avctx->get_buffer(avctx, frame);
624 /* return if the buffers are already set up
625 * this would happen e.g. when a custom get_buffer() calls
626 * avcodec_default_get_buffer
631 priv = av_mallocz(sizeof(*priv));
633 ret = AVERROR(ENOMEM);
636 priv->avctx = *avctx;
637 priv->frame = *frame;
639 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
641 ret = AVERROR(ENOMEM);
645 #define WRAP_PLANE(ref_out, data, data_size) \
647 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
649 ret = AVERROR(ENOMEM); \
652 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
655 av_frame_unref(frame); \
656 ret = AVERROR(ENOMEM); \
661 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
662 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
664 planes = av_pix_fmt_count_planes(frame->format);
665 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
666 check for allocated buffers: make libavcodec happy */
667 if (desc && desc->flags & PIX_FMT_HWACCEL)
669 if (!desc || planes <= 0) {
670 ret = AVERROR(EINVAL);
674 for (i = 0; i < planes; i++) {
675 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
676 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
678 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
681 int planar = av_sample_fmt_is_planar(frame->format);
682 planes = planar ? avctx->channels : 1;
684 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
685 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
686 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
687 frame->nb_extended_buf);
688 if (!frame->extended_buf) {
689 ret = AVERROR(ENOMEM);
694 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
695 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
697 for (i = 0; i < frame->nb_extended_buf; i++)
698 WRAP_PLANE(frame->extended_buf[i],
699 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
703 av_buffer_unref(&dummy_buf);
708 avctx->release_buffer(avctx, frame);
710 av_buffer_unref(&dummy_buf);
715 return avctx->get_buffer2(avctx, frame, flags);
718 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
723 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
726 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
728 if (av_frame_is_writable(frame))
731 av_frame_move_ref(&tmp, frame);
733 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
735 av_frame_unref(&tmp);
739 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
740 frame->format, frame->width, frame->height);
742 av_frame_unref(&tmp);
747 #if FF_API_GET_BUFFER
748 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
753 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
759 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
763 for (i = 0; i < count; i++) {
764 int r = func(c, (char *)arg + i * size);
771 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
775 for (i = 0; i < count; i++) {
776 int r = func(c, arg, i, 0);
783 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
785 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
786 return desc->flags & PIX_FMT_HWACCEL;
789 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
791 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
796 void avcodec_get_frame_defaults(AVFrame *frame)
798 if (frame->extended_data != frame->data)
799 av_freep(&frame->extended_data);
801 memset(frame, 0, sizeof(AVFrame));
803 frame->pts = AV_NOPTS_VALUE;
804 frame->key_frame = 1;
805 frame->sample_aspect_ratio = (AVRational) {0, 1 };
806 frame->format = -1; /* unknown */
807 frame->extended_data = frame->data;
810 AVFrame *avcodec_alloc_frame(void)
812 AVFrame *frame = av_mallocz(sizeof(AVFrame));
817 avcodec_get_frame_defaults(frame);
822 void avcodec_free_frame(AVFrame **frame)
826 if (!frame || !*frame)
831 if (f->extended_data != f->data)
832 av_freep(&f->extended_data);
837 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
840 AVDictionary *tmp = NULL;
842 if (avcodec_is_open(avctx))
845 if ((!codec && !avctx->codec)) {
846 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
847 return AVERROR(EINVAL);
849 if ((codec && avctx->codec && codec != avctx->codec)) {
850 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
851 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
852 return AVERROR(EINVAL);
855 codec = avctx->codec;
857 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
858 return AVERROR(EINVAL);
861 av_dict_copy(&tmp, *options, 0);
863 /* If there is a user-supplied mutex locking routine, call it. */
865 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
869 entangled_thread_counter++;
870 if (entangled_thread_counter != 1) {
871 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
876 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
877 if (!avctx->internal) {
878 ret = AVERROR(ENOMEM);
882 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
883 if (!avctx->internal->pool) {
884 ret = AVERROR(ENOMEM);
888 if (codec->priv_data_size > 0) {
889 if (!avctx->priv_data) {
890 avctx->priv_data = av_mallocz(codec->priv_data_size);
891 if (!avctx->priv_data) {
892 ret = AVERROR(ENOMEM);
895 if (codec->priv_class) {
896 *(const AVClass **)avctx->priv_data = codec->priv_class;
897 av_opt_set_defaults(avctx->priv_data);
900 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
903 avctx->priv_data = NULL;
905 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
908 if (avctx->coded_width && avctx->coded_height)
909 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
910 else if (avctx->width && avctx->height)
911 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
913 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
914 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
915 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
916 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
917 avcodec_set_dimensions(avctx, 0, 0);
920 /* if the decoder init function was already called previously,
921 * free the already allocated subtitle_header before overwriting it */
922 if (av_codec_is_decoder(codec))
923 av_freep(&avctx->subtitle_header);
925 if (avctx->channels > FF_SANE_NB_CHANNELS) {
926 ret = AVERROR(EINVAL);
930 avctx->codec = codec;
931 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
932 avctx->codec_id == AV_CODEC_ID_NONE) {
933 avctx->codec_type = codec->type;
934 avctx->codec_id = codec->id;
936 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
937 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
938 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
939 ret = AVERROR(EINVAL);
942 avctx->frame_number = 0;
944 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
945 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
946 ret = AVERROR_EXPERIMENTAL;
950 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
951 (!avctx->time_base.num || !avctx->time_base.den)) {
952 avctx->time_base.num = 1;
953 avctx->time_base.den = avctx->sample_rate;
956 if (HAVE_THREADS && !avctx->thread_opaque) {
957 ret = ff_thread_init(avctx);
962 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
963 avctx->thread_count = 1;
965 if (av_codec_is_encoder(avctx->codec)) {
967 if (avctx->codec->sample_fmts) {
968 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
969 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
971 if (avctx->channels == 1 &&
972 av_get_planar_sample_fmt(avctx->sample_fmt) ==
973 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
974 avctx->sample_fmt = avctx->codec->sample_fmts[i];
978 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
979 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
980 ret = AVERROR(EINVAL);
984 if (avctx->codec->pix_fmts) {
985 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
986 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
988 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
989 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
990 ret = AVERROR(EINVAL);
994 if (avctx->codec->supported_samplerates) {
995 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
996 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
998 if (avctx->codec->supported_samplerates[i] == 0) {
999 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1000 ret = AVERROR(EINVAL);
1004 if (avctx->codec->channel_layouts) {
1005 if (!avctx->channel_layout) {
1006 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1008 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1009 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1011 if (avctx->codec->channel_layouts[i] == 0) {
1012 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1013 ret = AVERROR(EINVAL);
1018 if (avctx->channel_layout && avctx->channels) {
1019 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1020 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1021 ret = AVERROR(EINVAL);
1024 } else if (avctx->channel_layout) {
1025 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1028 if (!avctx->rc_initial_buffer_occupancy)
1029 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1032 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1033 ret = avctx->codec->init(avctx);
1039 if (av_codec_is_decoder(avctx->codec)) {
1040 /* validate channel layout from the decoder */
1041 if (avctx->channel_layout) {
1042 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1043 if (!avctx->channels)
1044 avctx->channels = channels;
1045 else if (channels != avctx->channels) {
1046 av_log(avctx, AV_LOG_WARNING,
1047 "channel layout does not match number of channels\n");
1048 avctx->channel_layout = 0;
1051 if (avctx->channels && avctx->channels < 0 ||
1052 avctx->channels > FF_SANE_NB_CHANNELS) {
1053 ret = AVERROR(EINVAL);
1058 entangled_thread_counter--;
1060 /* Release any user-supplied mutex. */
1061 if (ff_lockmgr_cb) {
1062 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1065 av_dict_free(options);
1072 av_freep(&avctx->priv_data);
1073 if (avctx->internal)
1074 av_freep(&avctx->internal->pool);
1075 av_freep(&avctx->internal);
1076 avctx->codec = NULL;
1080 int ff_alloc_packet(AVPacket *avpkt, int size)
1082 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1083 return AVERROR(EINVAL);
1086 AVBufferRef *buf = avpkt->buf;
1087 #if FF_API_DESTRUCT_PACKET
1088 void *destruct = avpkt->destruct;
1091 if (avpkt->size < size)
1092 return AVERROR(EINVAL);
1094 av_init_packet(avpkt);
1095 #if FF_API_DESTRUCT_PACKET
1096 avpkt->destruct = destruct;
1102 return av_new_packet(avpkt, size);
1107 * Pad last frame with silence.
1109 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1111 AVFrame *frame = NULL;
1112 uint8_t *buf = NULL;
1115 if (!(frame = avcodec_alloc_frame()))
1116 return AVERROR(ENOMEM);
1119 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1120 s->frame_size, s->sample_fmt, 0)) < 0)
1123 if (!(buf = av_malloc(ret))) {
1124 ret = AVERROR(ENOMEM);
1128 frame->nb_samples = s->frame_size;
1129 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1132 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1133 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1135 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1136 frame->nb_samples - src->nb_samples,
1137 s->channels, s->sample_fmt)) < 0)
1145 if (frame->extended_data != frame->data)
1146 av_freep(&frame->extended_data);
1152 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1154 const AVFrame *frame,
1155 int *got_packet_ptr)
1158 AVFrame *padded_frame = NULL;
1160 int user_packet = !!avpkt->data;
1162 *got_packet_ptr = 0;
1164 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1165 av_free_packet(avpkt);
1166 av_init_packet(avpkt);
1170 /* ensure that extended_data is properly set */
1171 if (frame && !frame->extended_data) {
1172 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1173 avctx->channels > AV_NUM_DATA_POINTERS) {
1174 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1175 "with more than %d channels, but extended_data is not set.\n",
1176 AV_NUM_DATA_POINTERS);
1177 return AVERROR(EINVAL);
1179 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1182 tmp.extended_data = tmp.data;
1186 /* check for valid frame size */
1188 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1189 if (frame->nb_samples > avctx->frame_size)
1190 return AVERROR(EINVAL);
1191 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1192 if (frame->nb_samples < avctx->frame_size &&
1193 !avctx->internal->last_audio_frame) {
1194 ret = pad_last_frame(avctx, &padded_frame, frame);
1198 frame = padded_frame;
1199 avctx->internal->last_audio_frame = 1;
1202 if (frame->nb_samples != avctx->frame_size) {
1203 ret = AVERROR(EINVAL);
1209 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1211 if (*got_packet_ptr) {
1212 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1213 if (avpkt->pts == AV_NOPTS_VALUE)
1214 avpkt->pts = frame->pts;
1215 if (!avpkt->duration)
1216 avpkt->duration = ff_samples_to_time_base(avctx,
1219 avpkt->dts = avpkt->pts;
1224 if (!user_packet && avpkt->size) {
1225 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1227 avpkt->data = avpkt->buf->data;
1230 avctx->frame_number++;
1233 if (ret < 0 || !*got_packet_ptr) {
1234 av_free_packet(avpkt);
1235 av_init_packet(avpkt);
1239 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1240 * this needs to be moved to the encoders, but for now we can do it
1241 * here to simplify things */
1242 avpkt->flags |= AV_PKT_FLAG_KEY;
1246 av_freep(&padded_frame->data[0]);
1247 if (padded_frame->extended_data != padded_frame->data)
1248 av_freep(&padded_frame->extended_data);
1249 av_freep(&padded_frame);
1255 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1257 const AVFrame *frame,
1258 int *got_packet_ptr)
1261 int user_packet = !!avpkt->data;
1263 *got_packet_ptr = 0;
1265 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1266 av_free_packet(avpkt);
1267 av_init_packet(avpkt);
1272 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1273 return AVERROR(EINVAL);
1275 av_assert0(avctx->codec->encode2);
1277 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1279 if (!*got_packet_ptr)
1281 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1282 avpkt->pts = avpkt->dts = frame->pts;
1284 if (!user_packet && avpkt->size) {
1285 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1287 avpkt->data = avpkt->buf->data;
1290 avctx->frame_number++;
1293 if (ret < 0 || !*got_packet_ptr)
1294 av_free_packet(avpkt);
1300 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1301 const AVSubtitle *sub)
1304 if (sub->start_display_time) {
1305 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1308 if (sub->num_rects == 0 || !sub->rects)
1310 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1311 avctx->frame_number++;
1315 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1318 const uint8_t *data;
1321 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1324 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1325 if (!data || size < 4)
1327 flags = bytestream_get_le32(&data);
1329 if (size < 4) /* Required for any of the changes */
1331 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1332 avctx->channels = bytestream_get_le32(&data);
1335 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1338 avctx->channel_layout = bytestream_get_le64(&data);
1343 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1344 avctx->sample_rate = bytestream_get_le32(&data);
1347 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1350 avctx->width = bytestream_get_le32(&data);
1351 avctx->height = bytestream_get_le32(&data);
1352 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1357 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1358 int *got_picture_ptr,
1361 AVCodecInternal *avci = avctx->internal;
1364 *got_picture_ptr = 0;
1365 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1369 apply_param_change(avctx, avpkt);
1371 avcodec_get_frame_defaults(picture);
1373 if (!avctx->refcounted_frames)
1374 av_frame_unref(&avci->to_free);
1376 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1377 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1378 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1381 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1383 picture->pkt_dts = avpkt->dts;
1384 /* get_buffer is supposed to set frame parameters */
1385 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1386 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1387 picture->width = avctx->width;
1388 picture->height = avctx->height;
1389 picture->format = avctx->pix_fmt;
1393 emms_c(); //needed to avoid an emms_c() call before every return;
1395 if (ret < 0 && picture->data[0])
1396 av_frame_unref(picture);
1398 if (*got_picture_ptr) {
1399 if (!avctx->refcounted_frames) {
1400 avci->to_free = *picture;
1401 avci->to_free.extended_data = avci->to_free.data;
1404 avctx->frame_number++;
1409 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1410 * make sure it's set correctly */
1411 picture->extended_data = picture->data;
1416 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1421 AVCodecInternal *avci = avctx->internal;
1422 int planar, channels;
1429 if (!avpkt->data && avpkt->size) {
1430 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1431 return AVERROR(EINVAL);
1434 apply_param_change(avctx, avpkt);
1436 avcodec_get_frame_defaults(frame);
1438 if (!avctx->refcounted_frames)
1439 av_frame_unref(&avci->to_free);
1441 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1442 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1443 if (ret >= 0 && *got_frame_ptr) {
1444 avctx->frame_number++;
1445 frame->pkt_dts = avpkt->dts;
1446 if (frame->format == AV_SAMPLE_FMT_NONE)
1447 frame->format = avctx->sample_fmt;
1449 if (!avctx->refcounted_frames) {
1450 avci->to_free = *frame;
1451 avci->to_free.extended_data = avci->to_free.data;
1455 if (ret < 0 && frame->data[0])
1456 av_frame_unref(frame);
1459 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1460 * make sure it's set correctly; assume decoders that actually use
1461 * extended_data are doing it correctly */
1462 planar = av_sample_fmt_is_planar(frame->format);
1463 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1464 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1465 frame->extended_data = frame->data;
1470 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1478 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1480 avctx->frame_number++;
1484 void avsubtitle_free(AVSubtitle *sub)
1488 for (i = 0; i < sub->num_rects; i++) {
1489 av_freep(&sub->rects[i]->pict.data[0]);
1490 av_freep(&sub->rects[i]->pict.data[1]);
1491 av_freep(&sub->rects[i]->pict.data[2]);
1492 av_freep(&sub->rects[i]->pict.data[3]);
1493 av_freep(&sub->rects[i]->text);
1494 av_freep(&sub->rects[i]->ass);
1495 av_freep(&sub->rects[i]);
1498 av_freep(&sub->rects);
1500 memset(sub, 0, sizeof(AVSubtitle));
1503 av_cold int avcodec_close(AVCodecContext *avctx)
1505 /* If there is a user-supplied mutex locking routine, call it. */
1506 if (ff_lockmgr_cb) {
1507 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1511 entangled_thread_counter++;
1512 if (entangled_thread_counter != 1) {
1513 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1514 entangled_thread_counter--;
1518 if (avcodec_is_open(avctx)) {
1519 FramePool *pool = avctx->internal->pool;
1521 if (HAVE_THREADS && avctx->thread_opaque)
1522 ff_thread_free(avctx);
1523 if (avctx->codec && avctx->codec->close)
1524 avctx->codec->close(avctx);
1525 avctx->coded_frame = NULL;
1526 if (!avctx->refcounted_frames)
1527 av_frame_unref(&avctx->internal->to_free);
1528 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1529 av_buffer_pool_uninit(&pool->pools[i]);
1530 av_freep(&avctx->internal->pool);
1531 av_freep(&avctx->internal);
1534 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1535 av_opt_free(avctx->priv_data);
1537 av_freep(&avctx->priv_data);
1538 if (av_codec_is_encoder(avctx->codec))
1539 av_freep(&avctx->extradata);
1540 avctx->codec = NULL;
1541 avctx->active_thread_type = 0;
1542 entangled_thread_counter--;
1544 /* Release any user-supplied mutex. */
1545 if (ff_lockmgr_cb) {
1546 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1551 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1553 AVCodec *p, *experimental = NULL;
1556 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1558 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1565 return experimental;
1568 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1570 return find_encdec(id, 1);
1573 AVCodec *avcodec_find_encoder_by_name(const char *name)
1580 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1587 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1589 return find_encdec(id, 0);
1592 AVCodec *avcodec_find_decoder_by_name(const char *name)
1599 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1606 static int get_bit_rate(AVCodecContext *ctx)
1609 int bits_per_sample;
1611 switch (ctx->codec_type) {
1612 case AVMEDIA_TYPE_VIDEO:
1613 case AVMEDIA_TYPE_DATA:
1614 case AVMEDIA_TYPE_SUBTITLE:
1615 case AVMEDIA_TYPE_ATTACHMENT:
1616 bit_rate = ctx->bit_rate;
1618 case AVMEDIA_TYPE_AUDIO:
1619 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1620 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1629 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1631 int i, len, ret = 0;
1633 #define TAG_PRINT(x) \
1634 (((x) >= '0' && (x) <= '9') || \
1635 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1636 ((x) == '.' || (x) == ' '))
1638 for (i = 0; i < 4; i++) {
1639 len = snprintf(buf, buf_size,
1640 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1642 buf_size = buf_size > len ? buf_size - len : 0;
1649 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1651 const char *codec_name;
1652 const char *profile = NULL;
1656 AVRational display_aspect_ratio;
1661 p = avcodec_find_encoder(enc->codec_id);
1663 p = avcodec_find_decoder(enc->codec_id);
1666 codec_name = p->name;
1667 profile = av_get_profile_name(p, enc->profile);
1668 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1669 /* fake mpeg2 transport stream codec (currently not
1671 codec_name = "mpeg2ts";
1672 } else if (enc->codec_name[0] != '\0') {
1673 codec_name = enc->codec_name;
1675 /* output avi tags */
1677 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1678 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1682 switch (enc->codec_type) {
1683 case AVMEDIA_TYPE_VIDEO:
1684 snprintf(buf, buf_size,
1686 codec_name, enc->mb_decision ? " (hq)" : "");
1688 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1690 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1691 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1693 av_get_pix_fmt_name(enc->pix_fmt));
1696 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1698 enc->width, enc->height);
1699 if (enc->sample_aspect_ratio.num) {
1700 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1701 enc->width * enc->sample_aspect_ratio.num,
1702 enc->height * enc->sample_aspect_ratio.den,
1704 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1705 " [PAR %d:%d DAR %d:%d]",
1706 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1707 display_aspect_ratio.num, display_aspect_ratio.den);
1709 if (av_log_get_level() >= AV_LOG_DEBUG) {
1710 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1711 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1713 enc->time_base.num / g, enc->time_base.den / g);
1717 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1718 ", q=%d-%d", enc->qmin, enc->qmax);
1721 case AVMEDIA_TYPE_AUDIO:
1722 snprintf(buf, buf_size,
1726 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1728 if (enc->sample_rate) {
1729 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1730 ", %d Hz", enc->sample_rate);
1732 av_strlcat(buf, ", ", buf_size);
1733 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1734 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1735 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1736 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1739 case AVMEDIA_TYPE_DATA:
1740 snprintf(buf, buf_size, "Data: %s", codec_name);
1742 case AVMEDIA_TYPE_SUBTITLE:
1743 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1745 case AVMEDIA_TYPE_ATTACHMENT:
1746 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1749 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1753 if (enc->flags & CODEC_FLAG_PASS1)
1754 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1756 if (enc->flags & CODEC_FLAG_PASS2)
1757 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1760 bitrate = get_bit_rate(enc);
1762 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1763 ", %d kb/s", bitrate / 1000);
1767 const char *av_get_profile_name(const AVCodec *codec, int profile)
1770 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1773 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1774 if (p->profile == profile)
1780 unsigned avcodec_version(void)
1782 return LIBAVCODEC_VERSION_INT;
1785 const char *avcodec_configuration(void)
1787 return LIBAV_CONFIGURATION;
1790 const char *avcodec_license(void)
1792 #define LICENSE_PREFIX "libavcodec license: "
1793 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1796 void avcodec_flush_buffers(AVCodecContext *avctx)
1798 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1799 ff_thread_flush(avctx);
1800 else if (avctx->codec->flush)
1801 avctx->codec->flush(avctx);
1804 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1807 case AV_CODEC_ID_ADPCM_CT:
1808 case AV_CODEC_ID_ADPCM_IMA_APC:
1809 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1810 case AV_CODEC_ID_ADPCM_IMA_WS:
1811 case AV_CODEC_ID_ADPCM_G722:
1812 case AV_CODEC_ID_ADPCM_YAMAHA:
1814 case AV_CODEC_ID_PCM_ALAW:
1815 case AV_CODEC_ID_PCM_MULAW:
1816 case AV_CODEC_ID_PCM_S8:
1817 case AV_CODEC_ID_PCM_U8:
1818 case AV_CODEC_ID_PCM_ZORK:
1820 case AV_CODEC_ID_PCM_S16BE:
1821 case AV_CODEC_ID_PCM_S16LE:
1822 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1823 case AV_CODEC_ID_PCM_U16BE:
1824 case AV_CODEC_ID_PCM_U16LE:
1826 case AV_CODEC_ID_PCM_S24DAUD:
1827 case AV_CODEC_ID_PCM_S24BE:
1828 case AV_CODEC_ID_PCM_S24LE:
1829 case AV_CODEC_ID_PCM_U24BE:
1830 case AV_CODEC_ID_PCM_U24LE:
1832 case AV_CODEC_ID_PCM_S32BE:
1833 case AV_CODEC_ID_PCM_S32LE:
1834 case AV_CODEC_ID_PCM_U32BE:
1835 case AV_CODEC_ID_PCM_U32LE:
1836 case AV_CODEC_ID_PCM_F32BE:
1837 case AV_CODEC_ID_PCM_F32LE:
1839 case AV_CODEC_ID_PCM_F64BE:
1840 case AV_CODEC_ID_PCM_F64LE:
1847 int av_get_bits_per_sample(enum AVCodecID codec_id)
1850 case AV_CODEC_ID_ADPCM_SBPRO_2:
1852 case AV_CODEC_ID_ADPCM_SBPRO_3:
1854 case AV_CODEC_ID_ADPCM_SBPRO_4:
1855 case AV_CODEC_ID_ADPCM_IMA_WAV:
1856 case AV_CODEC_ID_ADPCM_IMA_QT:
1857 case AV_CODEC_ID_ADPCM_SWF:
1858 case AV_CODEC_ID_ADPCM_MS:
1861 return av_get_exact_bits_per_sample(codec_id);
1865 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1867 int id, sr, ch, ba, tag, bps;
1869 id = avctx->codec_id;
1870 sr = avctx->sample_rate;
1871 ch = avctx->channels;
1872 ba = avctx->block_align;
1873 tag = avctx->codec_tag;
1874 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1876 /* codecs with an exact constant bits per sample */
1877 if (bps > 0 && ch > 0 && frame_bytes > 0)
1878 return (frame_bytes * 8) / (bps * ch);
1879 bps = avctx->bits_per_coded_sample;
1881 /* codecs with a fixed packet duration */
1883 case AV_CODEC_ID_ADPCM_ADX: return 32;
1884 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1885 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1886 case AV_CODEC_ID_AMR_NB:
1887 case AV_CODEC_ID_GSM:
1888 case AV_CODEC_ID_QCELP:
1889 case AV_CODEC_ID_RA_144:
1890 case AV_CODEC_ID_RA_288: return 160;
1891 case AV_CODEC_ID_IMC: return 256;
1892 case AV_CODEC_ID_AMR_WB:
1893 case AV_CODEC_ID_GSM_MS: return 320;
1894 case AV_CODEC_ID_MP1: return 384;
1895 case AV_CODEC_ID_ATRAC1: return 512;
1896 case AV_CODEC_ID_ATRAC3: return 1024;
1897 case AV_CODEC_ID_MP2:
1898 case AV_CODEC_ID_MUSEPACK7: return 1152;
1899 case AV_CODEC_ID_AC3: return 1536;
1903 /* calc from sample rate */
1904 if (id == AV_CODEC_ID_TTA)
1905 return 256 * sr / 245;
1908 /* calc from sample rate and channels */
1909 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1910 return (480 << (sr / 22050)) / ch;
1915 /* calc from block_align */
1916 if (id == AV_CODEC_ID_SIPR) {
1918 case 20: return 160;
1919 case 19: return 144;
1920 case 29: return 288;
1921 case 37: return 480;
1923 } else if (id == AV_CODEC_ID_ILBC) {
1925 case 38: return 160;
1926 case 50: return 240;
1931 if (frame_bytes > 0) {
1932 /* calc from frame_bytes only */
1933 if (id == AV_CODEC_ID_TRUESPEECH)
1934 return 240 * (frame_bytes / 32);
1935 if (id == AV_CODEC_ID_NELLYMOSER)
1936 return 256 * (frame_bytes / 64);
1939 /* calc from frame_bytes and bits_per_coded_sample */
1940 if (id == AV_CODEC_ID_ADPCM_G726)
1941 return frame_bytes * 8 / bps;
1945 /* calc from frame_bytes and channels */
1947 case AV_CODEC_ID_ADPCM_4XM:
1948 case AV_CODEC_ID_ADPCM_IMA_ISS:
1949 return (frame_bytes - 4 * ch) * 2 / ch;
1950 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1951 return (frame_bytes - 4) * 2 / ch;
1952 case AV_CODEC_ID_ADPCM_IMA_AMV:
1953 return (frame_bytes - 8) * 2 / ch;
1954 case AV_CODEC_ID_ADPCM_XA:
1955 return (frame_bytes / 128) * 224 / ch;
1956 case AV_CODEC_ID_INTERPLAY_DPCM:
1957 return (frame_bytes - 6 - ch) / ch;
1958 case AV_CODEC_ID_ROQ_DPCM:
1959 return (frame_bytes - 8) / ch;
1960 case AV_CODEC_ID_XAN_DPCM:
1961 return (frame_bytes - 2 * ch) / ch;
1962 case AV_CODEC_ID_MACE3:
1963 return 3 * frame_bytes / ch;
1964 case AV_CODEC_ID_MACE6:
1965 return 6 * frame_bytes / ch;
1966 case AV_CODEC_ID_PCM_LXF:
1967 return 2 * (frame_bytes / (5 * ch));
1971 /* calc from frame_bytes, channels, and codec_tag */
1972 if (id == AV_CODEC_ID_SOL_DPCM) {
1974 return frame_bytes / ch;
1976 return frame_bytes * 2 / ch;
1981 /* calc from frame_bytes, channels, and block_align */
1982 int blocks = frame_bytes / ba;
1983 switch (avctx->codec_id) {
1984 case AV_CODEC_ID_ADPCM_IMA_WAV:
1985 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1986 case AV_CODEC_ID_ADPCM_IMA_DK3:
1987 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1988 case AV_CODEC_ID_ADPCM_IMA_DK4:
1989 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1990 case AV_CODEC_ID_ADPCM_MS:
1991 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1996 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1997 switch (avctx->codec_id) {
1998 case AV_CODEC_ID_PCM_DVD:
1999 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2000 case AV_CODEC_ID_PCM_BLURAY:
2001 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2002 case AV_CODEC_ID_S302M:
2003 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2013 int ff_thread_init(AVCodecContext *s)
2020 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2034 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2037 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2041 #if FF_API_MISSING_SAMPLE
2042 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2044 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2045 "version to the newest one from Git. If the problem still "
2046 "occurs, it means that your file has a feature which has not "
2047 "been implemented.\n", feature);
2049 av_log_ask_for_sample(avc, NULL);
2052 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2054 va_list argument_list;
2056 va_start(argument_list, msg);
2059 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2060 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2061 "of this file to ftp://upload.libav.org/incoming/ "
2062 "and contact the libav-devel mailing list.\n");
2064 va_end(argument_list);
2066 #endif /* FF_API_MISSING_SAMPLE */
2068 static AVHWAccel *first_hwaccel = NULL;
2070 void av_register_hwaccel(AVHWAccel *hwaccel)
2072 AVHWAccel **p = &first_hwaccel;
2076 hwaccel->next = NULL;
2079 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2081 return hwaccel ? hwaccel->next : first_hwaccel;
2084 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2086 AVHWAccel *hwaccel = NULL;
2088 while ((hwaccel = av_hwaccel_next(hwaccel)))
2089 if (hwaccel->id == codec_id
2090 && hwaccel->pix_fmt == pix_fmt)
2095 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2097 if (ff_lockmgr_cb) {
2098 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2100 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2106 if (ff_lockmgr_cb) {
2107 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2109 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2115 int avpriv_lock_avformat(void)
2117 if (ff_lockmgr_cb) {
2118 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2124 int avpriv_unlock_avformat(void)
2126 if (ff_lockmgr_cb) {
2127 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2133 unsigned int avpriv_toupper4(unsigned int x)
2135 return av_toupper(x & 0xFF) +
2136 (av_toupper((x >> 8) & 0xFF) << 8) +
2137 (av_toupper((x >> 16) & 0xFF) << 16) +
2138 (av_toupper((x >> 24) & 0xFF) << 24);
2141 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2145 dst->owner = src->owner;
2147 ret = av_frame_ref(dst->f, src->f);
2151 if (src->progress &&
2152 !(dst->progress = av_buffer_ref(src->progress))) {
2153 ff_thread_release_buffer(dst->owner, dst);
2154 return AVERROR(ENOMEM);
2162 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2165 return ff_get_buffer(avctx, f->f, flags);
2168 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2170 av_frame_unref(f->f);
2173 void ff_thread_finish_setup(AVCodecContext *avctx)
2177 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2181 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2187 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2189 if (codec_id <= AV_CODEC_ID_NONE)
2190 return AVMEDIA_TYPE_UNKNOWN;
2191 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2192 return AVMEDIA_TYPE_VIDEO;
2193 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2194 return AVMEDIA_TYPE_AUDIO;
2195 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2196 return AVMEDIA_TYPE_SUBTITLE;
2198 return AVMEDIA_TYPE_UNKNOWN;
2201 int avcodec_is_open(AVCodecContext *s)
2203 return !!s->internal;
2206 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2208 uint32_t * restrict state)
2216 for (i = 0; i < 3; i++) {
2217 uint32_t tmp = *state << 8;
2218 *state = tmp + *(p++);
2219 if (tmp == 0x100 || p == end)
2224 if (p[-1] > 1 ) p += 3;
2225 else if (p[-2] ) p += 2;
2226 else if (p[-3]|(p[-1]-1)) p++;
2233 p = FFMIN(p, end) - 4;
2234 *state = AV_RB32(p);