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"
50 static int volatile entangled_thread_counter = 0;
51 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
52 static void *codec_mutex;
53 static void *avformat_mutex;
55 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
60 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
62 ptr = av_realloc(ptr, min_size);
63 /* we could set this to the unmodified min_size but this is safer
64 * if the user lost the ptr and uses NULL now
74 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
79 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
81 *p = av_malloc(min_size);
87 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
90 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
95 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
97 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
100 /* encoder management */
101 static AVCodec *first_avcodec = NULL;
103 AVCodec *av_codec_next(const AVCodec *c)
108 return first_avcodec;
111 static void avcodec_init(void)
113 static int initialized = 0;
115 if (initialized != 0)
119 ff_dsputil_static_init();
122 int av_codec_is_encoder(const AVCodec *codec)
124 return codec && (codec->encode_sub || codec->encode2);
127 int av_codec_is_decoder(const AVCodec *codec)
129 return codec && codec->decode;
132 void avcodec_register(AVCodec *codec)
142 if (codec->init_static_data)
143 codec->init_static_data(codec);
146 unsigned avcodec_get_edge_width(void)
151 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
153 s->coded_width = width;
154 s->coded_height = height;
159 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
160 # define STRIDE_ALIGN 16
162 # define STRIDE_ALIGN 8
165 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
166 int linesize_align[AV_NUM_DATA_POINTERS])
172 switch (s->pix_fmt) {
173 case AV_PIX_FMT_YUV420P:
174 case AV_PIX_FMT_YUYV422:
175 case AV_PIX_FMT_UYVY422:
176 case AV_PIX_FMT_YUV422P:
177 case AV_PIX_FMT_YUV440P:
178 case AV_PIX_FMT_YUV444P:
179 case AV_PIX_FMT_GBRP:
180 case AV_PIX_FMT_GRAY8:
181 case AV_PIX_FMT_GRAY16BE:
182 case AV_PIX_FMT_GRAY16LE:
183 case AV_PIX_FMT_YUVJ420P:
184 case AV_PIX_FMT_YUVJ422P:
185 case AV_PIX_FMT_YUVJ440P:
186 case AV_PIX_FMT_YUVJ444P:
187 case AV_PIX_FMT_YUVA420P:
188 case AV_PIX_FMT_YUVA422P:
189 case AV_PIX_FMT_YUVA444P:
190 case AV_PIX_FMT_YUV420P9LE:
191 case AV_PIX_FMT_YUV420P9BE:
192 case AV_PIX_FMT_YUV420P10LE:
193 case AV_PIX_FMT_YUV420P10BE:
194 case AV_PIX_FMT_YUV422P9LE:
195 case AV_PIX_FMT_YUV422P9BE:
196 case AV_PIX_FMT_YUV422P10LE:
197 case AV_PIX_FMT_YUV422P10BE:
198 case AV_PIX_FMT_YUV444P9LE:
199 case AV_PIX_FMT_YUV444P9BE:
200 case AV_PIX_FMT_YUV444P10LE:
201 case AV_PIX_FMT_YUV444P10BE:
202 case AV_PIX_FMT_GBRP9LE:
203 case AV_PIX_FMT_GBRP9BE:
204 case AV_PIX_FMT_GBRP10LE:
205 case AV_PIX_FMT_GBRP10BE:
206 w_align = 16; //FIXME assume 16 pixel per macroblock
207 h_align = 16 * 2; // interlaced needs 2 macroblocks height
209 case AV_PIX_FMT_YUV411P:
210 case AV_PIX_FMT_UYYVYY411:
214 case AV_PIX_FMT_YUV410P:
215 if (s->codec_id == AV_CODEC_ID_SVQ1) {
219 case AV_PIX_FMT_RGB555:
220 if (s->codec_id == AV_CODEC_ID_RPZA) {
224 case AV_PIX_FMT_PAL8:
225 case AV_PIX_FMT_BGR8:
226 case AV_PIX_FMT_RGB8:
227 if (s->codec_id == AV_CODEC_ID_SMC) {
232 case AV_PIX_FMT_BGR24:
233 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
234 (s->codec_id == AV_CODEC_ID_ZLIB)) {
245 *width = FFALIGN(*width, w_align);
246 *height = FFALIGN(*height, h_align);
247 if (s->codec_id == AV_CODEC_ID_H264)
248 // some of the optimized chroma MC reads one line too much
251 for (i = 0; i < 4; i++)
252 linesize_align[i] = STRIDE_ALIGN;
255 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
257 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
258 int chroma_shift = desc->log2_chroma_w;
259 int linesize_align[AV_NUM_DATA_POINTERS];
262 avcodec_align_dimensions2(s, width, height, linesize_align);
263 align = FFMAX(linesize_align[0], linesize_align[3]);
264 linesize_align[1] <<= chroma_shift;
265 linesize_align[2] <<= chroma_shift;
266 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
267 *width = FFALIGN(*width, align);
270 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
271 enum AVSampleFormat sample_fmt, const uint8_t *buf,
272 int buf_size, int align)
274 int ch, planar, needed_size, ret = 0;
276 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
277 frame->nb_samples, sample_fmt,
279 if (buf_size < needed_size)
280 return AVERROR(EINVAL);
282 planar = av_sample_fmt_is_planar(sample_fmt);
283 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
284 if (!(frame->extended_data = av_mallocz(nb_channels *
285 sizeof(*frame->extended_data))))
286 return AVERROR(ENOMEM);
288 frame->extended_data = frame->data;
291 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
292 buf, nb_channels, frame->nb_samples,
293 sample_fmt, align)) < 0) {
294 if (frame->extended_data != frame->data)
295 av_free(frame->extended_data);
298 if (frame->extended_data != frame->data) {
299 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
300 frame->data[ch] = frame->extended_data[ch];
306 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
308 FramePool *pool = avctx->internal->pool;
311 switch (avctx->codec_type) {
312 case AVMEDIA_TYPE_VIDEO: {
315 int w = frame->width;
316 int h = frame->height;
317 int tmpsize, unaligned;
319 if (pool->format == frame->format &&
320 pool->width == frame->width && pool->height == frame->height)
323 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
325 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
331 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
332 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
333 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
334 // increase alignment of w for next try (rhs gives the lowest bit set in w)
338 for (i = 0; i < 4; i++)
339 unaligned |= picture.linesize[i] % pool->stride_align[i];
342 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
343 NULL, picture.linesize);
347 for (i = 0; i < 3 && picture.data[i + 1]; i++)
348 size[i] = picture.data[i + 1] - picture.data[i];
349 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
351 for (i = 0; i < 4; i++) {
352 av_buffer_pool_uninit(&pool->pools[i]);
353 pool->linesize[i] = picture.linesize[i];
355 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
356 if (!pool->pools[i]) {
357 ret = AVERROR(ENOMEM);
362 pool->format = frame->format;
363 pool->width = frame->width;
364 pool->height = frame->height;
368 case AVMEDIA_TYPE_AUDIO: {
369 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
370 int planar = av_sample_fmt_is_planar(frame->format);
371 int planes = planar ? ch : 1;
373 if (pool->format == frame->format && pool->planes == planes &&
374 pool->channels == ch && frame->nb_samples == pool->samples)
377 av_buffer_pool_uninit(&pool->pools[0]);
378 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
379 frame->nb_samples, frame->format, 0);
383 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
384 if (!pool->pools[0]) {
385 ret = AVERROR(ENOMEM);
389 pool->format = frame->format;
390 pool->planes = planes;
392 pool->samples = frame->nb_samples;
395 default: av_assert0(0);
399 for (i = 0; i < 4; i++)
400 av_buffer_pool_uninit(&pool->pools[i]);
402 pool->planes = pool->channels = pool->samples = 0;
403 pool->width = pool->height = 0;
407 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
409 FramePool *pool = avctx->internal->pool;
410 int planes = pool->planes;
413 frame->linesize[0] = pool->linesize[0];
415 if (planes > AV_NUM_DATA_POINTERS) {
416 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
417 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
418 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
419 sizeof(*frame->extended_buf));
420 if (!frame->extended_data || !frame->extended_buf) {
421 av_freep(&frame->extended_data);
422 av_freep(&frame->extended_buf);
423 return AVERROR(ENOMEM);
426 frame->extended_data = frame->data;
428 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
429 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
432 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
434 for (i = 0; i < frame->nb_extended_buf; i++) {
435 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
436 if (!frame->extended_buf[i])
438 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
441 if (avctx->debug & FF_DEBUG_BUFFERS)
442 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
446 av_frame_unref(frame);
447 return AVERROR(ENOMEM);
450 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
452 FramePool *pool = s->internal->pool;
453 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
454 int pixel_size = desc->comp[0].step_minus1 + 1;
455 int h_chroma_shift, v_chroma_shift;
458 if (pic->data[0] != NULL) {
459 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
463 memset(pic->data, 0, sizeof(pic->data));
464 pic->extended_data = pic->data;
466 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
468 for (i = 0; i < 4 && pool->pools[i]; i++) {
469 const int h_shift = i == 0 ? 0 : h_chroma_shift;
470 const int v_shift = i == 0 ? 0 : v_chroma_shift;
472 pic->linesize[i] = pool->linesize[i];
474 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
478 // no edge if EDGE EMU or not planar YUV
479 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
480 pic->data[i] = pic->buf[i]->data;
482 pic->data[i] = pic->buf[i]->data +
483 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
484 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
487 for (; i < AV_NUM_DATA_POINTERS; i++) {
489 pic->linesize[i] = 0;
491 if (pic->data[1] && !pic->data[2])
492 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
494 if (s->debug & FF_DEBUG_BUFFERS)
495 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
500 return AVERROR(ENOMEM);
503 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
507 if ((ret = update_frame_pool(avctx, frame)) < 0)
510 #if FF_API_GET_BUFFER
511 frame->type = FF_BUFFER_TYPE_INTERNAL;
514 switch (avctx->codec_type) {
515 case AVMEDIA_TYPE_VIDEO:
516 return video_get_buffer(avctx, frame);
517 case AVMEDIA_TYPE_AUDIO:
518 return audio_get_buffer(avctx, frame);
524 #if FF_API_GET_BUFFER
525 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
527 return avcodec_default_get_buffer2(avctx, frame, 0);
530 typedef struct CompatReleaseBufPriv {
531 AVCodecContext avctx;
533 } CompatReleaseBufPriv;
535 static void compat_free_buffer(void *opaque, uint8_t *data)
537 CompatReleaseBufPriv *priv = opaque;
538 if (priv->avctx.release_buffer)
539 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
543 static void compat_release_buffer(void *opaque, uint8_t *data)
545 AVBufferRef *buf = opaque;
546 av_buffer_unref(&buf);
550 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
554 switch (avctx->codec_type) {
555 case AVMEDIA_TYPE_VIDEO:
557 frame->width = avctx->width;
559 frame->height = avctx->height;
560 if (frame->format < 0)
561 frame->format = avctx->pix_fmt;
562 if (!frame->sample_aspect_ratio.num)
563 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
565 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
568 case AVMEDIA_TYPE_AUDIO:
569 if (!frame->sample_rate)
570 frame->sample_rate = avctx->sample_rate;
571 if (frame->format < 0)
572 frame->format = avctx->sample_fmt;
573 if (!frame->channel_layout) {
574 if (avctx->channel_layout) {
575 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
577 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
579 return AVERROR(EINVAL);
582 frame->channel_layout = avctx->channel_layout;
584 if (avctx->channels > FF_SANE_NB_CHANNELS) {
585 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
587 return AVERROR(ENOSYS);
590 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
591 if (!frame->channel_layout)
592 frame->channel_layout = (1ULL << avctx->channels) - 1;
596 default: return AVERROR(EINVAL);
599 frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
600 frame->reordered_opaque = avctx->reordered_opaque;
602 #if FF_API_GET_BUFFER
604 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
605 * We wrap each plane in its own AVBuffer. Each of those has a reference to
606 * a dummy AVBuffer as its private data, unreffing it on free.
607 * When all the planes are freed, the dummy buffer's free callback calls
610 if (avctx->get_buffer) {
611 CompatReleaseBufPriv *priv = NULL;
612 AVBufferRef *dummy_buf = NULL;
615 if (flags & AV_GET_BUFFER_FLAG_REF)
616 frame->reference = 1;
618 ret = avctx->get_buffer(avctx, frame);
622 /* return if the buffers are already set up
623 * this would happen e.g. when a custom get_buffer() calls
624 * avcodec_default_get_buffer
629 priv = av_mallocz(sizeof(*priv));
631 ret = AVERROR(ENOMEM);
634 priv->avctx = *avctx;
635 priv->frame = *frame;
637 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
639 ret = AVERROR(ENOMEM);
643 #define WRAP_PLANE(ref_out, data, data_size) \
645 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
647 ret = AVERROR(ENOMEM); \
650 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
653 av_frame_unref(frame); \
654 ret = AVERROR(ENOMEM); \
659 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
660 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
663 ret = AVERROR(EINVAL);
666 planes = (desc->flags & PIX_FMT_PLANAR) ? desc->nb_components : 1;
668 for (i = 0; i < planes; i++) {
669 int h_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
670 int plane_size = (frame->width >> h_shift) * frame->linesize[i];
672 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
675 int planar = av_sample_fmt_is_planar(frame->format);
676 planes = planar ? avctx->channels : 1;
678 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
679 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
680 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
681 frame->nb_extended_buf);
682 if (!frame->extended_buf) {
683 ret = AVERROR(ENOMEM);
688 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
689 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
691 for (i = 0; i < frame->nb_extended_buf; i++)
692 WRAP_PLANE(frame->extended_buf[i],
693 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
697 av_buffer_unref(&dummy_buf);
702 avctx->release_buffer(avctx, frame);
704 av_buffer_unref(&dummy_buf);
709 return avctx->get_buffer2(avctx, frame, flags);
712 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
717 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
720 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
722 if (av_frame_is_writable(frame))
725 av_frame_move_ref(&tmp, frame);
727 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
729 av_frame_unref(&tmp);
733 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
734 frame->format, frame->width, frame->height);
736 av_frame_unref(&tmp);
741 #if FF_API_GET_BUFFER
742 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
747 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
753 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
757 for (i = 0; i < count; i++) {
758 int r = func(c, (char *)arg + i * size);
765 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
769 for (i = 0; i < count; i++) {
770 int r = func(c, arg, i, 0);
777 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
779 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
780 return desc->flags & PIX_FMT_HWACCEL;
783 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
785 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
790 void avcodec_get_frame_defaults(AVFrame *frame)
792 if (frame->extended_data != frame->data)
793 av_freep(&frame->extended_data);
795 memset(frame, 0, sizeof(AVFrame));
797 frame->pts = AV_NOPTS_VALUE;
798 frame->key_frame = 1;
799 frame->sample_aspect_ratio = (AVRational) {0, 1 };
800 frame->format = -1; /* unknown */
801 frame->extended_data = frame->data;
804 AVFrame *avcodec_alloc_frame(void)
806 AVFrame *frame = av_mallocz(sizeof(AVFrame));
811 avcodec_get_frame_defaults(frame);
816 void avcodec_free_frame(AVFrame **frame)
820 if (!frame || !*frame)
825 if (f->extended_data != f->data)
826 av_freep(&f->extended_data);
831 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
834 AVDictionary *tmp = NULL;
836 if (avcodec_is_open(avctx))
839 if ((!codec && !avctx->codec)) {
840 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
841 return AVERROR(EINVAL);
843 if ((codec && avctx->codec && codec != avctx->codec)) {
844 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
845 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
846 return AVERROR(EINVAL);
849 codec = avctx->codec;
851 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
852 return AVERROR(EINVAL);
855 av_dict_copy(&tmp, *options, 0);
857 /* If there is a user-supplied mutex locking routine, call it. */
859 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
863 entangled_thread_counter++;
864 if (entangled_thread_counter != 1) {
865 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
870 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
871 if (!avctx->internal) {
872 ret = AVERROR(ENOMEM);
876 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
877 if (!avctx->internal->pool) {
878 ret = AVERROR(ENOMEM);
882 if (codec->priv_data_size > 0) {
883 if (!avctx->priv_data) {
884 avctx->priv_data = av_mallocz(codec->priv_data_size);
885 if (!avctx->priv_data) {
886 ret = AVERROR(ENOMEM);
889 if (codec->priv_class) {
890 *(const AVClass **)avctx->priv_data = codec->priv_class;
891 av_opt_set_defaults(avctx->priv_data);
894 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
897 avctx->priv_data = NULL;
899 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
902 if (avctx->coded_width && avctx->coded_height)
903 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
904 else if (avctx->width && avctx->height)
905 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
907 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
908 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
909 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
910 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
911 avcodec_set_dimensions(avctx, 0, 0);
914 /* if the decoder init function was already called previously,
915 * free the already allocated subtitle_header before overwriting it */
916 if (av_codec_is_decoder(codec))
917 av_freep(&avctx->subtitle_header);
919 if (avctx->channels > FF_SANE_NB_CHANNELS) {
920 ret = AVERROR(EINVAL);
924 avctx->codec = codec;
925 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
926 avctx->codec_id == AV_CODEC_ID_NONE) {
927 avctx->codec_type = codec->type;
928 avctx->codec_id = codec->id;
930 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
931 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
932 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
933 ret = AVERROR(EINVAL);
936 avctx->frame_number = 0;
938 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
939 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
940 ret = AVERROR_EXPERIMENTAL;
944 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
945 (!avctx->time_base.num || !avctx->time_base.den)) {
946 avctx->time_base.num = 1;
947 avctx->time_base.den = avctx->sample_rate;
950 if (HAVE_THREADS && !avctx->thread_opaque) {
951 ret = ff_thread_init(avctx);
956 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
957 avctx->thread_count = 1;
959 if (av_codec_is_encoder(avctx->codec)) {
961 if (avctx->codec->sample_fmts) {
962 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
963 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
965 if (avctx->channels == 1 &&
966 av_get_planar_sample_fmt(avctx->sample_fmt) ==
967 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
968 avctx->sample_fmt = avctx->codec->sample_fmts[i];
972 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
973 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
974 ret = AVERROR(EINVAL);
978 if (avctx->codec->pix_fmts) {
979 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
980 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
982 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
983 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
984 ret = AVERROR(EINVAL);
988 if (avctx->codec->supported_samplerates) {
989 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
990 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
992 if (avctx->codec->supported_samplerates[i] == 0) {
993 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
994 ret = AVERROR(EINVAL);
998 if (avctx->codec->channel_layouts) {
999 if (!avctx->channel_layout) {
1000 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1002 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1003 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1005 if (avctx->codec->channel_layouts[i] == 0) {
1006 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1007 ret = AVERROR(EINVAL);
1012 if (avctx->channel_layout && avctx->channels) {
1013 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1014 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1015 ret = AVERROR(EINVAL);
1018 } else if (avctx->channel_layout) {
1019 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1022 if (!avctx->rc_initial_buffer_occupancy)
1023 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1026 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1027 ret = avctx->codec->init(avctx);
1033 if (av_codec_is_decoder(avctx->codec)) {
1034 /* validate channel layout from the decoder */
1035 if (avctx->channel_layout) {
1036 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1037 if (!avctx->channels)
1038 avctx->channels = channels;
1039 else if (channels != avctx->channels) {
1040 av_log(avctx, AV_LOG_WARNING,
1041 "channel layout does not match number of channels\n");
1042 avctx->channel_layout = 0;
1045 if (avctx->channels && avctx->channels < 0 ||
1046 avctx->channels > FF_SANE_NB_CHANNELS) {
1047 ret = AVERROR(EINVAL);
1052 entangled_thread_counter--;
1054 /* Release any user-supplied mutex. */
1055 if (ff_lockmgr_cb) {
1056 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1059 av_dict_free(options);
1066 av_freep(&avctx->priv_data);
1067 if (avctx->internal)
1068 av_freep(&avctx->internal->pool);
1069 av_freep(&avctx->internal);
1070 avctx->codec = NULL;
1074 int ff_alloc_packet(AVPacket *avpkt, int size)
1076 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1077 return AVERROR(EINVAL);
1080 AVBufferRef *buf = avpkt->buf;
1081 #if FF_API_DESTRUCT_PACKET
1082 void *destruct = avpkt->destruct;
1085 if (avpkt->size < size)
1086 return AVERROR(EINVAL);
1088 av_init_packet(avpkt);
1089 #if FF_API_DESTRUCT_PACKET
1090 avpkt->destruct = destruct;
1096 return av_new_packet(avpkt, size);
1101 * Pad last frame with silence.
1103 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1105 AVFrame *frame = NULL;
1106 uint8_t *buf = NULL;
1109 if (!(frame = avcodec_alloc_frame()))
1110 return AVERROR(ENOMEM);
1113 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1114 s->frame_size, s->sample_fmt, 0)) < 0)
1117 if (!(buf = av_malloc(ret))) {
1118 ret = AVERROR(ENOMEM);
1122 frame->nb_samples = s->frame_size;
1123 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1126 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1127 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1129 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1130 frame->nb_samples - src->nb_samples,
1131 s->channels, s->sample_fmt)) < 0)
1139 if (frame->extended_data != frame->data)
1140 av_freep(&frame->extended_data);
1146 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1148 const AVFrame *frame,
1149 int *got_packet_ptr)
1152 AVFrame *padded_frame = NULL;
1154 int user_packet = !!avpkt->data;
1156 *got_packet_ptr = 0;
1158 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1159 av_free_packet(avpkt);
1160 av_init_packet(avpkt);
1164 /* ensure that extended_data is properly set */
1165 if (frame && !frame->extended_data) {
1166 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1167 avctx->channels > AV_NUM_DATA_POINTERS) {
1168 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1169 "with more than %d channels, but extended_data is not set.\n",
1170 AV_NUM_DATA_POINTERS);
1171 return AVERROR(EINVAL);
1173 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1176 tmp.extended_data = tmp.data;
1180 /* check for valid frame size */
1182 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1183 if (frame->nb_samples > avctx->frame_size)
1184 return AVERROR(EINVAL);
1185 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1186 if (frame->nb_samples < avctx->frame_size &&
1187 !avctx->internal->last_audio_frame) {
1188 ret = pad_last_frame(avctx, &padded_frame, frame);
1192 frame = padded_frame;
1193 avctx->internal->last_audio_frame = 1;
1196 if (frame->nb_samples != avctx->frame_size) {
1197 ret = AVERROR(EINVAL);
1203 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1205 if (*got_packet_ptr) {
1206 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1207 if (avpkt->pts == AV_NOPTS_VALUE)
1208 avpkt->pts = frame->pts;
1209 if (!avpkt->duration)
1210 avpkt->duration = ff_samples_to_time_base(avctx,
1213 avpkt->dts = avpkt->pts;
1218 if (!user_packet && avpkt->size) {
1219 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1221 avpkt->data = avpkt->buf->data;
1224 avctx->frame_number++;
1227 if (ret < 0 || !*got_packet_ptr) {
1228 av_free_packet(avpkt);
1229 av_init_packet(avpkt);
1233 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1234 * this needs to be moved to the encoders, but for now we can do it
1235 * here to simplify things */
1236 avpkt->flags |= AV_PKT_FLAG_KEY;
1240 av_freep(&padded_frame->data[0]);
1241 if (padded_frame->extended_data != padded_frame->data)
1242 av_freep(&padded_frame->extended_data);
1243 av_freep(&padded_frame);
1249 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1251 const AVFrame *frame,
1252 int *got_packet_ptr)
1255 int user_packet = !!avpkt->data;
1257 *got_packet_ptr = 0;
1259 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1260 av_free_packet(avpkt);
1261 av_init_packet(avpkt);
1266 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1267 return AVERROR(EINVAL);
1269 av_assert0(avctx->codec->encode2);
1271 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1273 if (!*got_packet_ptr)
1275 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1276 avpkt->pts = avpkt->dts = frame->pts;
1278 if (!user_packet && avpkt->size) {
1279 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1281 avpkt->data = avpkt->buf->data;
1284 avctx->frame_number++;
1287 if (ret < 0 || !*got_packet_ptr)
1288 av_free_packet(avpkt);
1294 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1295 const AVSubtitle *sub)
1298 if (sub->start_display_time) {
1299 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1302 if (sub->num_rects == 0 || !sub->rects)
1304 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1305 avctx->frame_number++;
1309 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1312 const uint8_t *data;
1315 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1318 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1319 if (!data || size < 4)
1321 flags = bytestream_get_le32(&data);
1323 if (size < 4) /* Required for any of the changes */
1325 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1326 avctx->channels = bytestream_get_le32(&data);
1329 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1332 avctx->channel_layout = bytestream_get_le64(&data);
1337 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1338 avctx->sample_rate = bytestream_get_le32(&data);
1341 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1344 avctx->width = bytestream_get_le32(&data);
1345 avctx->height = bytestream_get_le32(&data);
1346 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1351 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1352 int *got_picture_ptr,
1355 AVCodecInternal *avci = avctx->internal;
1358 *got_picture_ptr = 0;
1359 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1363 apply_param_change(avctx, avpkt);
1365 avcodec_get_frame_defaults(picture);
1367 if (!avctx->refcounted_frames)
1368 av_frame_unref(&avci->to_free);
1370 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1371 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1372 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1375 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1377 picture->pkt_dts = avpkt->dts;
1378 /* get_buffer is supposed to set frame parameters */
1379 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1380 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1381 picture->width = avctx->width;
1382 picture->height = avctx->height;
1383 picture->format = avctx->pix_fmt;
1387 emms_c(); //needed to avoid an emms_c() call before every return;
1389 if (ret < 0 && picture->data[0])
1390 av_frame_unref(picture);
1392 if (*got_picture_ptr) {
1393 if (!avctx->refcounted_frames) {
1394 avci->to_free = *picture;
1395 avci->to_free.extended_data = avci->to_free.data;
1398 avctx->frame_number++;
1403 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1404 * make sure it's set correctly */
1405 picture->extended_data = picture->data;
1410 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1415 AVCodecInternal *avci = avctx->internal;
1416 int planar, channels;
1423 if (!avpkt->data && avpkt->size) {
1424 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1425 return AVERROR(EINVAL);
1428 apply_param_change(avctx, avpkt);
1430 avcodec_get_frame_defaults(frame);
1432 if (!avctx->refcounted_frames)
1433 av_frame_unref(&avci->to_free);
1435 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1436 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1437 if (ret >= 0 && *got_frame_ptr) {
1438 avctx->frame_number++;
1439 frame->pkt_dts = avpkt->dts;
1440 if (frame->format == AV_SAMPLE_FMT_NONE)
1441 frame->format = avctx->sample_fmt;
1443 if (!avctx->refcounted_frames) {
1444 avci->to_free = *frame;
1445 avci->to_free.extended_data = avci->to_free.data;
1449 if (ret < 0 && frame->data[0])
1450 av_frame_unref(frame);
1453 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1454 * make sure it's set correctly; assume decoders that actually use
1455 * extended_data are doing it correctly */
1456 planar = av_sample_fmt_is_planar(frame->format);
1457 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1458 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1459 frame->extended_data = frame->data;
1464 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1472 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1474 avctx->frame_number++;
1478 void avsubtitle_free(AVSubtitle *sub)
1482 for (i = 0; i < sub->num_rects; i++) {
1483 av_freep(&sub->rects[i]->pict.data[0]);
1484 av_freep(&sub->rects[i]->pict.data[1]);
1485 av_freep(&sub->rects[i]->pict.data[2]);
1486 av_freep(&sub->rects[i]->pict.data[3]);
1487 av_freep(&sub->rects[i]->text);
1488 av_freep(&sub->rects[i]->ass);
1489 av_freep(&sub->rects[i]);
1492 av_freep(&sub->rects);
1494 memset(sub, 0, sizeof(AVSubtitle));
1497 av_cold int avcodec_close(AVCodecContext *avctx)
1499 /* If there is a user-supplied mutex locking routine, call it. */
1500 if (ff_lockmgr_cb) {
1501 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1505 entangled_thread_counter++;
1506 if (entangled_thread_counter != 1) {
1507 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1508 entangled_thread_counter--;
1512 if (avcodec_is_open(avctx)) {
1513 FramePool *pool = avctx->internal->pool;
1515 if (HAVE_THREADS && avctx->thread_opaque)
1516 ff_thread_free(avctx);
1517 if (avctx->codec && avctx->codec->close)
1518 avctx->codec->close(avctx);
1519 avctx->coded_frame = NULL;
1520 if (!avctx->refcounted_frames)
1521 av_frame_unref(&avctx->internal->to_free);
1522 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1523 av_buffer_pool_uninit(&pool->pools[i]);
1524 av_freep(&avctx->internal->pool);
1525 av_freep(&avctx->internal);
1528 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1529 av_opt_free(avctx->priv_data);
1531 av_freep(&avctx->priv_data);
1532 if (av_codec_is_encoder(avctx->codec))
1533 av_freep(&avctx->extradata);
1534 avctx->codec = NULL;
1535 avctx->active_thread_type = 0;
1536 entangled_thread_counter--;
1538 /* Release any user-supplied mutex. */
1539 if (ff_lockmgr_cb) {
1540 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1545 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1547 AVCodec *p, *experimental = NULL;
1550 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1552 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1559 return experimental;
1562 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1564 return find_encdec(id, 1);
1567 AVCodec *avcodec_find_encoder_by_name(const char *name)
1574 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1581 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1583 return find_encdec(id, 0);
1586 AVCodec *avcodec_find_decoder_by_name(const char *name)
1593 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1600 static int get_bit_rate(AVCodecContext *ctx)
1603 int bits_per_sample;
1605 switch (ctx->codec_type) {
1606 case AVMEDIA_TYPE_VIDEO:
1607 case AVMEDIA_TYPE_DATA:
1608 case AVMEDIA_TYPE_SUBTITLE:
1609 case AVMEDIA_TYPE_ATTACHMENT:
1610 bit_rate = ctx->bit_rate;
1612 case AVMEDIA_TYPE_AUDIO:
1613 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1614 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1623 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1625 int i, len, ret = 0;
1627 #define TAG_PRINT(x) \
1628 (((x) >= '0' && (x) <= '9') || \
1629 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1630 ((x) == '.' || (x) == ' '))
1632 for (i = 0; i < 4; i++) {
1633 len = snprintf(buf, buf_size,
1634 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1636 buf_size = buf_size > len ? buf_size - len : 0;
1643 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1645 const char *codec_name;
1646 const char *profile = NULL;
1650 AVRational display_aspect_ratio;
1655 p = avcodec_find_encoder(enc->codec_id);
1657 p = avcodec_find_decoder(enc->codec_id);
1660 codec_name = p->name;
1661 profile = av_get_profile_name(p, enc->profile);
1662 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1663 /* fake mpeg2 transport stream codec (currently not
1665 codec_name = "mpeg2ts";
1666 } else if (enc->codec_name[0] != '\0') {
1667 codec_name = enc->codec_name;
1669 /* output avi tags */
1671 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1672 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1676 switch (enc->codec_type) {
1677 case AVMEDIA_TYPE_VIDEO:
1678 snprintf(buf, buf_size,
1680 codec_name, enc->mb_decision ? " (hq)" : "");
1682 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1684 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1685 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1687 av_get_pix_fmt_name(enc->pix_fmt));
1690 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1692 enc->width, enc->height);
1693 if (enc->sample_aspect_ratio.num) {
1694 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1695 enc->width * enc->sample_aspect_ratio.num,
1696 enc->height * enc->sample_aspect_ratio.den,
1698 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1699 " [PAR %d:%d DAR %d:%d]",
1700 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1701 display_aspect_ratio.num, display_aspect_ratio.den);
1703 if (av_log_get_level() >= AV_LOG_DEBUG) {
1704 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1705 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1707 enc->time_base.num / g, enc->time_base.den / g);
1711 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1712 ", q=%d-%d", enc->qmin, enc->qmax);
1715 case AVMEDIA_TYPE_AUDIO:
1716 snprintf(buf, buf_size,
1720 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1722 if (enc->sample_rate) {
1723 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1724 ", %d Hz", enc->sample_rate);
1726 av_strlcat(buf, ", ", buf_size);
1727 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1728 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1729 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1730 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1733 case AVMEDIA_TYPE_DATA:
1734 snprintf(buf, buf_size, "Data: %s", codec_name);
1736 case AVMEDIA_TYPE_SUBTITLE:
1737 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1739 case AVMEDIA_TYPE_ATTACHMENT:
1740 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1743 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1747 if (enc->flags & CODEC_FLAG_PASS1)
1748 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1750 if (enc->flags & CODEC_FLAG_PASS2)
1751 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1754 bitrate = get_bit_rate(enc);
1756 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1757 ", %d kb/s", bitrate / 1000);
1761 const char *av_get_profile_name(const AVCodec *codec, int profile)
1764 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1767 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1768 if (p->profile == profile)
1774 unsigned avcodec_version(void)
1776 return LIBAVCODEC_VERSION_INT;
1779 const char *avcodec_configuration(void)
1781 return LIBAV_CONFIGURATION;
1784 const char *avcodec_license(void)
1786 #define LICENSE_PREFIX "libavcodec license: "
1787 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1790 void avcodec_flush_buffers(AVCodecContext *avctx)
1792 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1793 ff_thread_flush(avctx);
1794 else if (avctx->codec->flush)
1795 avctx->codec->flush(avctx);
1798 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1801 case AV_CODEC_ID_ADPCM_CT:
1802 case AV_CODEC_ID_ADPCM_IMA_APC:
1803 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1804 case AV_CODEC_ID_ADPCM_IMA_WS:
1805 case AV_CODEC_ID_ADPCM_G722:
1806 case AV_CODEC_ID_ADPCM_YAMAHA:
1808 case AV_CODEC_ID_PCM_ALAW:
1809 case AV_CODEC_ID_PCM_MULAW:
1810 case AV_CODEC_ID_PCM_S8:
1811 case AV_CODEC_ID_PCM_U8:
1812 case AV_CODEC_ID_PCM_ZORK:
1814 case AV_CODEC_ID_PCM_S16BE:
1815 case AV_CODEC_ID_PCM_S16LE:
1816 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1817 case AV_CODEC_ID_PCM_U16BE:
1818 case AV_CODEC_ID_PCM_U16LE:
1820 case AV_CODEC_ID_PCM_S24DAUD:
1821 case AV_CODEC_ID_PCM_S24BE:
1822 case AV_CODEC_ID_PCM_S24LE:
1823 case AV_CODEC_ID_PCM_U24BE:
1824 case AV_CODEC_ID_PCM_U24LE:
1826 case AV_CODEC_ID_PCM_S32BE:
1827 case AV_CODEC_ID_PCM_S32LE:
1828 case AV_CODEC_ID_PCM_U32BE:
1829 case AV_CODEC_ID_PCM_U32LE:
1830 case AV_CODEC_ID_PCM_F32BE:
1831 case AV_CODEC_ID_PCM_F32LE:
1833 case AV_CODEC_ID_PCM_F64BE:
1834 case AV_CODEC_ID_PCM_F64LE:
1841 int av_get_bits_per_sample(enum AVCodecID codec_id)
1844 case AV_CODEC_ID_ADPCM_SBPRO_2:
1846 case AV_CODEC_ID_ADPCM_SBPRO_3:
1848 case AV_CODEC_ID_ADPCM_SBPRO_4:
1849 case AV_CODEC_ID_ADPCM_IMA_WAV:
1850 case AV_CODEC_ID_ADPCM_IMA_QT:
1851 case AV_CODEC_ID_ADPCM_SWF:
1852 case AV_CODEC_ID_ADPCM_MS:
1855 return av_get_exact_bits_per_sample(codec_id);
1859 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1861 int id, sr, ch, ba, tag, bps;
1863 id = avctx->codec_id;
1864 sr = avctx->sample_rate;
1865 ch = avctx->channels;
1866 ba = avctx->block_align;
1867 tag = avctx->codec_tag;
1868 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1870 /* codecs with an exact constant bits per sample */
1871 if (bps > 0 && ch > 0 && frame_bytes > 0)
1872 return (frame_bytes * 8) / (bps * ch);
1873 bps = avctx->bits_per_coded_sample;
1875 /* codecs with a fixed packet duration */
1877 case AV_CODEC_ID_ADPCM_ADX: return 32;
1878 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1879 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1880 case AV_CODEC_ID_AMR_NB:
1881 case AV_CODEC_ID_GSM:
1882 case AV_CODEC_ID_QCELP:
1883 case AV_CODEC_ID_RA_144:
1884 case AV_CODEC_ID_RA_288: return 160;
1885 case AV_CODEC_ID_IMC: return 256;
1886 case AV_CODEC_ID_AMR_WB:
1887 case AV_CODEC_ID_GSM_MS: return 320;
1888 case AV_CODEC_ID_MP1: return 384;
1889 case AV_CODEC_ID_ATRAC1: return 512;
1890 case AV_CODEC_ID_ATRAC3: return 1024;
1891 case AV_CODEC_ID_MP2:
1892 case AV_CODEC_ID_MUSEPACK7: return 1152;
1893 case AV_CODEC_ID_AC3: return 1536;
1897 /* calc from sample rate */
1898 if (id == AV_CODEC_ID_TTA)
1899 return 256 * sr / 245;
1902 /* calc from sample rate and channels */
1903 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1904 return (480 << (sr / 22050)) / ch;
1909 /* calc from block_align */
1910 if (id == AV_CODEC_ID_SIPR) {
1912 case 20: return 160;
1913 case 19: return 144;
1914 case 29: return 288;
1915 case 37: return 480;
1917 } else if (id == AV_CODEC_ID_ILBC) {
1919 case 38: return 160;
1920 case 50: return 240;
1925 if (frame_bytes > 0) {
1926 /* calc from frame_bytes only */
1927 if (id == AV_CODEC_ID_TRUESPEECH)
1928 return 240 * (frame_bytes / 32);
1929 if (id == AV_CODEC_ID_NELLYMOSER)
1930 return 256 * (frame_bytes / 64);
1933 /* calc from frame_bytes and bits_per_coded_sample */
1934 if (id == AV_CODEC_ID_ADPCM_G726)
1935 return frame_bytes * 8 / bps;
1939 /* calc from frame_bytes and channels */
1941 case AV_CODEC_ID_ADPCM_4XM:
1942 case AV_CODEC_ID_ADPCM_IMA_ISS:
1943 return (frame_bytes - 4 * ch) * 2 / ch;
1944 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1945 return (frame_bytes - 4) * 2 / ch;
1946 case AV_CODEC_ID_ADPCM_IMA_AMV:
1947 return (frame_bytes - 8) * 2 / ch;
1948 case AV_CODEC_ID_ADPCM_XA:
1949 return (frame_bytes / 128) * 224 / ch;
1950 case AV_CODEC_ID_INTERPLAY_DPCM:
1951 return (frame_bytes - 6 - ch) / ch;
1952 case AV_CODEC_ID_ROQ_DPCM:
1953 return (frame_bytes - 8) / ch;
1954 case AV_CODEC_ID_XAN_DPCM:
1955 return (frame_bytes - 2 * ch) / ch;
1956 case AV_CODEC_ID_MACE3:
1957 return 3 * frame_bytes / ch;
1958 case AV_CODEC_ID_MACE6:
1959 return 6 * frame_bytes / ch;
1960 case AV_CODEC_ID_PCM_LXF:
1961 return 2 * (frame_bytes / (5 * ch));
1965 /* calc from frame_bytes, channels, and codec_tag */
1966 if (id == AV_CODEC_ID_SOL_DPCM) {
1968 return frame_bytes / ch;
1970 return frame_bytes * 2 / ch;
1975 /* calc from frame_bytes, channels, and block_align */
1976 int blocks = frame_bytes / ba;
1977 switch (avctx->codec_id) {
1978 case AV_CODEC_ID_ADPCM_IMA_WAV:
1979 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1980 case AV_CODEC_ID_ADPCM_IMA_DK3:
1981 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1982 case AV_CODEC_ID_ADPCM_IMA_DK4:
1983 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1984 case AV_CODEC_ID_ADPCM_MS:
1985 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1990 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1991 switch (avctx->codec_id) {
1992 case AV_CODEC_ID_PCM_DVD:
1993 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1994 case AV_CODEC_ID_PCM_BLURAY:
1995 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1996 case AV_CODEC_ID_S302M:
1997 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2007 int ff_thread_init(AVCodecContext *s)
2014 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2028 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2031 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2035 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2037 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2038 "version to the newest one from Git. If the problem still "
2039 "occurs, it means that your file has a feature which has not "
2040 "been implemented.\n", feature);
2042 av_log_ask_for_sample(avc, NULL);
2045 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2047 va_list argument_list;
2049 va_start(argument_list, msg);
2052 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2053 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2054 "of this file to ftp://upload.libav.org/incoming/ "
2055 "and contact the libav-devel mailing list.\n");
2057 va_end(argument_list);
2060 static AVHWAccel *first_hwaccel = NULL;
2062 void av_register_hwaccel(AVHWAccel *hwaccel)
2064 AVHWAccel **p = &first_hwaccel;
2068 hwaccel->next = NULL;
2071 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2073 return hwaccel ? hwaccel->next : first_hwaccel;
2076 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2078 AVHWAccel *hwaccel = NULL;
2080 while ((hwaccel = av_hwaccel_next(hwaccel)))
2081 if (hwaccel->id == codec_id
2082 && hwaccel->pix_fmt == pix_fmt)
2087 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2089 if (ff_lockmgr_cb) {
2090 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2092 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2098 if (ff_lockmgr_cb) {
2099 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2101 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2107 int avpriv_lock_avformat(void)
2109 if (ff_lockmgr_cb) {
2110 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2116 int avpriv_unlock_avformat(void)
2118 if (ff_lockmgr_cb) {
2119 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2125 unsigned int avpriv_toupper4(unsigned int x)
2127 return av_toupper(x & 0xFF) +
2128 (av_toupper((x >> 8) & 0xFF) << 8) +
2129 (av_toupper((x >> 16) & 0xFF) << 16) +
2130 (av_toupper((x >> 24) & 0xFF) << 24);
2133 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2137 dst->owner = src->owner;
2139 ret = av_frame_ref(dst->f, src->f);
2143 if (src->progress &&
2144 !(dst->progress = av_buffer_ref(src->progress))) {
2145 ff_thread_release_buffer(dst->owner, dst);
2146 return AVERROR(ENOMEM);
2154 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2157 return ff_get_buffer(avctx, f->f, flags);
2160 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2162 av_frame_unref(f->f);
2165 void ff_thread_finish_setup(AVCodecContext *avctx)
2169 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2173 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2179 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2181 if (codec_id <= AV_CODEC_ID_NONE)
2182 return AVMEDIA_TYPE_UNKNOWN;
2183 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2184 return AVMEDIA_TYPE_VIDEO;
2185 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2186 return AVMEDIA_TYPE_AUDIO;
2187 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2188 return AVMEDIA_TYPE_SUBTITLE;
2190 return AVMEDIA_TYPE_UNKNOWN;
2193 int avcodec_is_open(AVCodecContext *s)
2195 return !!s->internal;