3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/attributes.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/samplefmt.h"
40 #include "libavutil/dict.h"
42 #include "libavutil/opt.h"
44 #include "mpegvideo.h"
47 #include "bytestream.h"
54 static int volatile entangled_thread_counter = 0;
55 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
56 static void *codec_mutex;
57 static void *avformat_mutex;
59 #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
60 FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
62 return av_fast_realloc(ptr, size, min_size);
65 FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
67 av_fast_malloc(ptr, size, min_size);
71 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
74 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
79 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
81 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
84 /* encoder management */
85 static AVCodec *first_avcodec = NULL;
87 AVCodec *av_codec_next(const AVCodec *c)
95 static av_cold void avcodec_init(void)
97 static int initialized = 0;
104 ff_me_cmp_init_static();
107 int av_codec_is_encoder(const AVCodec *codec)
109 return codec && (codec->encode_sub || codec->encode2);
112 int av_codec_is_decoder(const AVCodec *codec)
114 return codec && codec->decode;
117 av_cold void avcodec_register(AVCodec *codec)
127 if (codec->init_static_data)
128 codec->init_static_data(codec);
132 unsigned avcodec_get_edge_width(void)
138 #if FF_API_SET_DIMENSIONS
139 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
141 ff_set_dimensions(s, width, height);
145 int ff_set_dimensions(AVCodecContext *s, int width, int height)
147 int ret = av_image_check_size(width, height, 0, s);
151 s->width = s->coded_width = width;
152 s->height = s->coded_height = height;
157 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
159 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
162 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
164 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
167 avctx->sample_aspect_ratio = sar;
172 int ff_side_data_update_matrix_encoding(AVFrame *frame,
173 enum AVMatrixEncoding matrix_encoding)
175 AVFrameSideData *side_data;
176 enum AVMatrixEncoding *data;
178 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
180 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
181 sizeof(enum AVMatrixEncoding));
184 return AVERROR(ENOMEM);
186 data = (enum AVMatrixEncoding*)side_data->data;
187 *data = matrix_encoding;
192 #if HAVE_SIMD_ALIGN_16
193 # define STRIDE_ALIGN 16
195 # define STRIDE_ALIGN 8
198 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
199 int linesize_align[AV_NUM_DATA_POINTERS])
205 switch (s->pix_fmt) {
206 case AV_PIX_FMT_YUV420P:
207 case AV_PIX_FMT_YUYV422:
208 case AV_PIX_FMT_YVYU422:
209 case AV_PIX_FMT_UYVY422:
210 case AV_PIX_FMT_YUV422P:
211 case AV_PIX_FMT_YUV440P:
212 case AV_PIX_FMT_YUV444P:
213 case AV_PIX_FMT_GBRP:
214 case AV_PIX_FMT_GRAY8:
215 case AV_PIX_FMT_GRAY16BE:
216 case AV_PIX_FMT_GRAY16LE:
217 case AV_PIX_FMT_YUVJ420P:
218 case AV_PIX_FMT_YUVJ422P:
219 case AV_PIX_FMT_YUVJ440P:
220 case AV_PIX_FMT_YUVJ444P:
221 case AV_PIX_FMT_YUVA420P:
222 case AV_PIX_FMT_YUVA422P:
223 case AV_PIX_FMT_YUVA444P:
224 case AV_PIX_FMT_YUV420P9LE:
225 case AV_PIX_FMT_YUV420P9BE:
226 case AV_PIX_FMT_YUV420P10LE:
227 case AV_PIX_FMT_YUV420P10BE:
228 case AV_PIX_FMT_YUV422P9LE:
229 case AV_PIX_FMT_YUV422P9BE:
230 case AV_PIX_FMT_YUV422P10LE:
231 case AV_PIX_FMT_YUV422P10BE:
232 case AV_PIX_FMT_YUVA422P10LE:
233 case AV_PIX_FMT_YUVA422P10BE:
234 case AV_PIX_FMT_YUV444P9LE:
235 case AV_PIX_FMT_YUV444P9BE:
236 case AV_PIX_FMT_YUV444P10LE:
237 case AV_PIX_FMT_YUV444P10BE:
238 case AV_PIX_FMT_YUVA444P10LE:
239 case AV_PIX_FMT_YUVA444P10BE:
240 case AV_PIX_FMT_GBRP9LE:
241 case AV_PIX_FMT_GBRP9BE:
242 case AV_PIX_FMT_GBRP10LE:
243 case AV_PIX_FMT_GBRP10BE:
244 w_align = 16; //FIXME assume 16 pixel per macroblock
245 h_align = 16 * 2; // interlaced needs 2 macroblocks height
247 case AV_PIX_FMT_YUV411P:
248 case AV_PIX_FMT_UYYVYY411:
252 case AV_PIX_FMT_YUV410P:
253 if (s->codec_id == AV_CODEC_ID_SVQ1) {
257 case AV_PIX_FMT_RGB555:
258 if (s->codec_id == AV_CODEC_ID_RPZA) {
262 case AV_PIX_FMT_PAL8:
263 case AV_PIX_FMT_BGR8:
264 case AV_PIX_FMT_RGB8:
265 if (s->codec_id == AV_CODEC_ID_SMC) {
270 case AV_PIX_FMT_BGR24:
271 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
272 (s->codec_id == AV_CODEC_ID_ZLIB)) {
283 *width = FFALIGN(*width, w_align);
284 *height = FFALIGN(*height, h_align);
285 if (s->codec_id == AV_CODEC_ID_H264)
286 // some of the optimized chroma MC reads one line too much
289 for (i = 0; i < 4; i++)
290 linesize_align[i] = STRIDE_ALIGN;
293 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
295 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
296 int chroma_shift = desc->log2_chroma_w;
297 int linesize_align[AV_NUM_DATA_POINTERS];
300 avcodec_align_dimensions2(s, width, height, linesize_align);
301 align = FFMAX(linesize_align[0], linesize_align[3]);
302 linesize_align[1] <<= chroma_shift;
303 linesize_align[2] <<= chroma_shift;
304 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
305 *width = FFALIGN(*width, align);
308 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
309 enum AVSampleFormat sample_fmt, const uint8_t *buf,
310 int buf_size, int align)
312 int ch, planar, needed_size, ret = 0;
314 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
315 frame->nb_samples, sample_fmt,
317 if (buf_size < needed_size)
318 return AVERROR(EINVAL);
320 planar = av_sample_fmt_is_planar(sample_fmt);
321 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
322 if (!(frame->extended_data = av_mallocz(nb_channels *
323 sizeof(*frame->extended_data))))
324 return AVERROR(ENOMEM);
326 frame->extended_data = frame->data;
329 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
330 buf, nb_channels, frame->nb_samples,
331 sample_fmt, align)) < 0) {
332 if (frame->extended_data != frame->data)
333 av_free(frame->extended_data);
336 if (frame->extended_data != frame->data) {
337 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
338 frame->data[ch] = frame->extended_data[ch];
344 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
346 FramePool *pool = avctx->internal->pool;
349 switch (avctx->codec_type) {
350 case AVMEDIA_TYPE_VIDEO: {
353 int w = frame->width;
354 int h = frame->height;
355 int tmpsize, unaligned;
357 if (pool->format == frame->format &&
358 pool->width == frame->width && pool->height == frame->height)
361 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
364 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
365 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
366 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
367 // increase alignment of w for next try (rhs gives the lowest bit set in w)
371 for (i = 0; i < 4; i++)
372 unaligned |= picture.linesize[i] % pool->stride_align[i];
375 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
376 NULL, picture.linesize);
380 for (i = 0; i < 3 && picture.data[i + 1]; i++)
381 size[i] = picture.data[i + 1] - picture.data[i];
382 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
384 for (i = 0; i < 4; i++) {
385 av_buffer_pool_uninit(&pool->pools[i]);
386 pool->linesize[i] = picture.linesize[i];
388 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
389 if (!pool->pools[i]) {
390 ret = AVERROR(ENOMEM);
395 pool->format = frame->format;
396 pool->width = frame->width;
397 pool->height = frame->height;
401 case AVMEDIA_TYPE_AUDIO: {
402 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
403 int planar = av_sample_fmt_is_planar(frame->format);
404 int planes = planar ? ch : 1;
406 if (pool->format == frame->format && pool->planes == planes &&
407 pool->channels == ch && frame->nb_samples == pool->samples)
410 av_buffer_pool_uninit(&pool->pools[0]);
411 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
412 frame->nb_samples, frame->format, 0);
416 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
417 if (!pool->pools[0]) {
418 ret = AVERROR(ENOMEM);
422 pool->format = frame->format;
423 pool->planes = planes;
425 pool->samples = frame->nb_samples;
428 default: av_assert0(0);
432 for (i = 0; i < 4; i++)
433 av_buffer_pool_uninit(&pool->pools[i]);
435 pool->planes = pool->channels = pool->samples = 0;
436 pool->width = pool->height = 0;
440 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
442 FramePool *pool = avctx->internal->pool;
443 int planes = pool->planes;
446 frame->linesize[0] = pool->linesize[0];
448 if (planes > AV_NUM_DATA_POINTERS) {
449 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
450 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
451 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
452 sizeof(*frame->extended_buf));
453 if (!frame->extended_data || !frame->extended_buf) {
454 av_freep(&frame->extended_data);
455 av_freep(&frame->extended_buf);
456 return AVERROR(ENOMEM);
459 frame->extended_data = frame->data;
461 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
462 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
465 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
467 for (i = 0; i < frame->nb_extended_buf; i++) {
468 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
469 if (!frame->extended_buf[i])
471 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
474 if (avctx->debug & FF_DEBUG_BUFFERS)
475 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
479 av_frame_unref(frame);
480 return AVERROR(ENOMEM);
483 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
485 FramePool *pool = s->internal->pool;
488 if (pic->data[0] != NULL) {
489 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
493 memset(pic->data, 0, sizeof(pic->data));
494 pic->extended_data = pic->data;
496 for (i = 0; i < 4 && pool->pools[i]; i++) {
497 pic->linesize[i] = pool->linesize[i];
499 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
503 pic->data[i] = pic->buf[i]->data;
505 for (; i < AV_NUM_DATA_POINTERS; i++) {
507 pic->linesize[i] = 0;
509 if (pic->data[1] && !pic->data[2])
510 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
512 if (s->debug & FF_DEBUG_BUFFERS)
513 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
518 return AVERROR(ENOMEM);
521 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
525 if ((ret = update_frame_pool(avctx, frame)) < 0)
528 #if FF_API_GET_BUFFER
529 FF_DISABLE_DEPRECATION_WARNINGS
530 frame->type = FF_BUFFER_TYPE_INTERNAL;
531 FF_ENABLE_DEPRECATION_WARNINGS
534 switch (avctx->codec_type) {
535 case AVMEDIA_TYPE_VIDEO:
536 return video_get_buffer(avctx, frame);
537 case AVMEDIA_TYPE_AUDIO:
538 return audio_get_buffer(avctx, frame);
544 #if FF_API_GET_BUFFER
545 FF_DISABLE_DEPRECATION_WARNINGS
546 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
548 return avcodec_default_get_buffer2(avctx, frame, 0);
551 typedef struct CompatReleaseBufPriv {
552 AVCodecContext avctx;
554 } CompatReleaseBufPriv;
556 static void compat_free_buffer(void *opaque, uint8_t *data)
558 CompatReleaseBufPriv *priv = opaque;
559 if (priv->avctx.release_buffer)
560 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
564 static void compat_release_buffer(void *opaque, uint8_t *data)
566 AVBufferRef *buf = opaque;
567 av_buffer_unref(&buf);
569 FF_ENABLE_DEPRECATION_WARNINGS
572 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
574 AVPacket *pkt = avctx->internal->pkt;
577 AVFrameSideData *frame_sd;
579 frame->color_primaries = avctx->color_primaries;
580 frame->color_trc = avctx->color_trc;
581 frame->colorspace = avctx->colorspace;
582 frame->color_range = avctx->color_range;
583 frame->chroma_location = avctx->chroma_sample_location;
585 frame->reordered_opaque = avctx->reordered_opaque;
587 frame->pkt_pts = AV_NOPTS_VALUE;
591 frame->pkt_pts = pkt->pts;
593 /* copy the replaygain data to the output frame */
594 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
596 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
598 return AVERROR(ENOMEM);
600 memcpy(frame_sd->data, packet_sd, size);
602 /* copy the displaymatrix to the output frame */
603 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
605 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, size);
607 return AVERROR(ENOMEM);
609 memcpy(frame_sd->data, packet_sd, size);
615 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
617 const AVHWAccel *hwaccel = avctx->hwaccel;
618 int override_dimensions = 1;
621 switch (avctx->codec_type) {
622 case AVMEDIA_TYPE_VIDEO:
623 if (frame->width <= 0 || frame->height <= 0) {
624 frame->width = FFMAX(avctx->width, avctx->coded_width);
625 frame->height = FFMAX(avctx->height, avctx->coded_height);
626 override_dimensions = 0;
628 if (frame->format < 0)
629 frame->format = avctx->pix_fmt;
630 if (!frame->sample_aspect_ratio.num)
631 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
633 if (av_image_check_sar(frame->width, frame->height,
634 frame->sample_aspect_ratio) < 0) {
635 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
636 frame->sample_aspect_ratio.num,
637 frame->sample_aspect_ratio.den);
638 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
641 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
644 case AVMEDIA_TYPE_AUDIO:
645 if (!frame->sample_rate)
646 frame->sample_rate = avctx->sample_rate;
647 if (frame->format < 0)
648 frame->format = avctx->sample_fmt;
649 if (!frame->channel_layout) {
650 if (avctx->channel_layout) {
651 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
653 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
655 return AVERROR(EINVAL);
658 frame->channel_layout = avctx->channel_layout;
660 if (avctx->channels > FF_SANE_NB_CHANNELS) {
661 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
663 return AVERROR(ENOSYS);
666 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
667 if (!frame->channel_layout)
668 frame->channel_layout = (1ULL << avctx->channels) - 1;
672 default: return AVERROR(EINVAL);
675 ret = ff_decode_frame_props(avctx, frame);
679 if (hwaccel && hwaccel->alloc_frame) {
680 ret = hwaccel->alloc_frame(avctx, frame);
684 #if FF_API_GET_BUFFER
685 FF_DISABLE_DEPRECATION_WARNINGS
687 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
688 * We wrap each plane in its own AVBuffer. Each of those has a reference to
689 * a dummy AVBuffer as its private data, unreffing it on free.
690 * When all the planes are freed, the dummy buffer's free callback calls
693 if (avctx->get_buffer) {
694 CompatReleaseBufPriv *priv = NULL;
695 AVBufferRef *dummy_buf = NULL;
698 if (flags & AV_GET_BUFFER_FLAG_REF)
699 frame->reference = 1;
701 ret = avctx->get_buffer(avctx, frame);
705 /* return if the buffers are already set up
706 * this would happen e.g. when a custom get_buffer() calls
707 * avcodec_default_get_buffer
712 priv = av_mallocz(sizeof(*priv));
714 ret = AVERROR(ENOMEM);
717 priv->avctx = *avctx;
718 priv->frame = *frame;
720 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
722 ret = AVERROR(ENOMEM);
726 #define WRAP_PLANE(ref_out, data, data_size) \
728 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
730 ret = AVERROR(ENOMEM); \
733 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
736 av_frame_unref(frame); \
737 ret = AVERROR(ENOMEM); \
742 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
743 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
745 planes = av_pix_fmt_count_planes(frame->format);
746 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
747 check for allocated buffers: make libavcodec happy */
748 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
750 if (!desc || planes <= 0) {
751 ret = AVERROR(EINVAL);
755 for (i = 0; i < planes; i++) {
756 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
757 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
759 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
762 int planar = av_sample_fmt_is_planar(frame->format);
763 planes = planar ? avctx->channels : 1;
765 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
766 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
767 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
768 frame->nb_extended_buf);
769 if (!frame->extended_buf) {
770 ret = AVERROR(ENOMEM);
775 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
776 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
778 for (i = 0; i < frame->nb_extended_buf; i++)
779 WRAP_PLANE(frame->extended_buf[i],
780 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
784 av_buffer_unref(&dummy_buf);
786 frame->width = avctx->width;
787 frame->height = avctx->height;
792 avctx->release_buffer(avctx, frame);
794 av_buffer_unref(&dummy_buf);
797 FF_ENABLE_DEPRECATION_WARNINGS
800 ret = avctx->get_buffer2(avctx, frame, flags);
803 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
804 frame->width = avctx->width;
805 frame->height = avctx->height;
811 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
816 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
819 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
821 if (av_frame_is_writable(frame))
822 return ff_decode_frame_props(avctx, frame);
824 tmp = av_frame_alloc();
826 return AVERROR(ENOMEM);
828 av_frame_move_ref(tmp, frame);
830 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
836 av_frame_copy(frame, tmp);
842 #if FF_API_GET_BUFFER
843 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
848 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
855 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
859 for (i = 0; i < count; i++) {
860 int r = func(c, (char *)arg + i * size);
867 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
871 for (i = 0; i < count; i++) {
872 int r = func(c, arg, i, 0);
879 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
881 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
882 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
885 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
887 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
892 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
893 enum AVPixelFormat pix_fmt)
895 AVHWAccel *hwaccel = NULL;
897 while ((hwaccel = av_hwaccel_next(hwaccel)))
898 if (hwaccel->id == codec_id
899 && hwaccel->pix_fmt == pix_fmt)
905 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
907 const AVPixFmtDescriptor *desc;
908 enum AVPixelFormat ret = avctx->get_format(avctx, fmt);
910 desc = av_pix_fmt_desc_get(ret);
912 return AV_PIX_FMT_NONE;
914 if (avctx->hwaccel && avctx->hwaccel->uninit)
915 avctx->hwaccel->uninit(avctx);
916 av_freep(&avctx->internal->hwaccel_priv_data);
917 avctx->hwaccel = NULL;
919 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
923 hwaccel = find_hwaccel(avctx->codec_id, ret);
925 av_log(avctx, AV_LOG_ERROR,
926 "Could not find an AVHWAccel for the pixel format: %s",
928 return AV_PIX_FMT_NONE;
931 if (hwaccel->priv_data_size) {
932 avctx->internal->hwaccel_priv_data = av_mallocz(hwaccel->priv_data_size);
933 if (!avctx->internal->hwaccel_priv_data)
934 return AV_PIX_FMT_NONE;
938 err = hwaccel->init(avctx);
940 av_freep(&avctx->internal->hwaccel_priv_data);
941 return AV_PIX_FMT_NONE;
944 avctx->hwaccel = hwaccel;
950 #if FF_API_AVFRAME_LAVC
951 void avcodec_get_frame_defaults(AVFrame *frame)
953 if (frame->extended_data != frame->data)
954 av_freep(&frame->extended_data);
956 memset(frame, 0, sizeof(AVFrame));
958 frame->pts = AV_NOPTS_VALUE;
959 frame->key_frame = 1;
960 frame->sample_aspect_ratio = (AVRational) {0, 1 };
961 frame->format = -1; /* unknown */
962 frame->extended_data = frame->data;
965 AVFrame *avcodec_alloc_frame(void)
967 AVFrame *frame = av_mallocz(sizeof(AVFrame));
972 FF_DISABLE_DEPRECATION_WARNINGS
973 avcodec_get_frame_defaults(frame);
974 FF_ENABLE_DEPRECATION_WARNINGS
979 void avcodec_free_frame(AVFrame **frame)
981 av_frame_free(frame);
985 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
988 AVDictionary *tmp = NULL;
990 if (avcodec_is_open(avctx))
993 if ((!codec && !avctx->codec)) {
994 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
995 return AVERROR(EINVAL);
997 if ((codec && avctx->codec && codec != avctx->codec)) {
998 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
999 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
1000 return AVERROR(EINVAL);
1003 codec = avctx->codec;
1005 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1006 return AVERROR(EINVAL);
1009 av_dict_copy(&tmp, *options, 0);
1011 /* If there is a user-supplied mutex locking routine, call it. */
1013 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1017 entangled_thread_counter++;
1018 if (entangled_thread_counter != 1) {
1019 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1024 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1025 if (!avctx->internal) {
1026 ret = AVERROR(ENOMEM);
1030 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1031 if (!avctx->internal->pool) {
1032 ret = AVERROR(ENOMEM);
1036 avctx->internal->to_free = av_frame_alloc();
1037 if (!avctx->internal->to_free) {
1038 ret = AVERROR(ENOMEM);
1042 if (codec->priv_data_size > 0) {
1043 if (!avctx->priv_data) {
1044 avctx->priv_data = av_mallocz(codec->priv_data_size);
1045 if (!avctx->priv_data) {
1046 ret = AVERROR(ENOMEM);
1049 if (codec->priv_class) {
1050 *(const AVClass **)avctx->priv_data = codec->priv_class;
1051 av_opt_set_defaults(avctx->priv_data);
1054 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1057 avctx->priv_data = NULL;
1059 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1062 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
1063 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1064 else if (avctx->width && avctx->height)
1065 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1069 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1070 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1071 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1072 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
1073 ff_set_dimensions(avctx, 0, 0);
1076 if (avctx->width > 0 && avctx->height > 0) {
1077 if (av_image_check_sar(avctx->width, avctx->height,
1078 avctx->sample_aspect_ratio) < 0) {
1079 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1080 avctx->sample_aspect_ratio.num,
1081 avctx->sample_aspect_ratio.den);
1082 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1086 /* if the decoder init function was already called previously,
1087 * free the already allocated subtitle_header before overwriting it */
1088 if (av_codec_is_decoder(codec))
1089 av_freep(&avctx->subtitle_header);
1091 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1092 ret = AVERROR(EINVAL);
1096 avctx->codec = codec;
1097 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1098 avctx->codec_id == AV_CODEC_ID_NONE) {
1099 avctx->codec_type = codec->type;
1100 avctx->codec_id = codec->id;
1102 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1103 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1104 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
1105 ret = AVERROR(EINVAL);
1108 avctx->frame_number = 0;
1110 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1111 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1112 ret = AVERROR_EXPERIMENTAL;
1116 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1117 (!avctx->time_base.num || !avctx->time_base.den)) {
1118 avctx->time_base.num = 1;
1119 avctx->time_base.den = avctx->sample_rate;
1123 ret = ff_thread_init(avctx);
1128 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1129 avctx->thread_count = 1;
1131 if (av_codec_is_encoder(avctx->codec)) {
1133 if (avctx->codec->sample_fmts) {
1134 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1135 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1137 if (avctx->channels == 1 &&
1138 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1139 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1140 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1144 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1145 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1146 ret = AVERROR(EINVAL);
1150 if (avctx->codec->pix_fmts) {
1151 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1152 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1154 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1155 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1156 ret = AVERROR(EINVAL);
1160 if (avctx->codec->supported_samplerates) {
1161 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1162 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1164 if (avctx->codec->supported_samplerates[i] == 0) {
1165 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1166 ret = AVERROR(EINVAL);
1170 if (avctx->codec->channel_layouts) {
1171 if (!avctx->channel_layout) {
1172 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1174 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1175 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1177 if (avctx->codec->channel_layouts[i] == 0) {
1178 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1179 ret = AVERROR(EINVAL);
1184 if (avctx->channel_layout && avctx->channels) {
1185 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1186 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1187 ret = AVERROR(EINVAL);
1190 } else if (avctx->channel_layout) {
1191 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1194 if (!avctx->rc_initial_buffer_occupancy)
1195 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1198 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1199 ret = avctx->codec->init(avctx);
1205 if (av_codec_is_decoder(avctx->codec)) {
1206 /* validate channel layout from the decoder */
1207 if (avctx->channel_layout) {
1208 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1209 if (!avctx->channels)
1210 avctx->channels = channels;
1211 else if (channels != avctx->channels) {
1212 av_log(avctx, AV_LOG_WARNING,
1213 "channel layout does not match number of channels\n");
1214 avctx->channel_layout = 0;
1217 if (avctx->channels && avctx->channels < 0 ||
1218 avctx->channels > FF_SANE_NB_CHANNELS) {
1219 ret = AVERROR(EINVAL);
1224 entangled_thread_counter--;
1226 /* Release any user-supplied mutex. */
1228 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1231 av_dict_free(options);
1238 av_freep(&avctx->priv_data);
1239 if (avctx->internal) {
1240 av_frame_free(&avctx->internal->to_free);
1241 av_freep(&avctx->internal->pool);
1243 av_freep(&avctx->internal);
1244 avctx->codec = NULL;
1248 int ff_alloc_packet(AVPacket *avpkt, int size)
1250 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1251 return AVERROR(EINVAL);
1254 AVBufferRef *buf = avpkt->buf;
1255 #if FF_API_DESTRUCT_PACKET
1256 FF_DISABLE_DEPRECATION_WARNINGS
1257 void *destruct = avpkt->destruct;
1258 FF_ENABLE_DEPRECATION_WARNINGS
1261 if (avpkt->size < size)
1262 return AVERROR(EINVAL);
1264 av_init_packet(avpkt);
1265 #if FF_API_DESTRUCT_PACKET
1266 FF_DISABLE_DEPRECATION_WARNINGS
1267 avpkt->destruct = destruct;
1268 FF_ENABLE_DEPRECATION_WARNINGS
1274 return av_new_packet(avpkt, size);
1279 * Pad last frame with silence.
1281 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1283 AVFrame *frame = NULL;
1286 if (!(frame = av_frame_alloc()))
1287 return AVERROR(ENOMEM);
1289 frame->format = src->format;
1290 frame->channel_layout = src->channel_layout;
1291 frame->nb_samples = s->frame_size;
1292 ret = av_frame_get_buffer(frame, 32);
1296 ret = av_frame_copy_props(frame, src);
1300 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1301 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1303 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1304 frame->nb_samples - src->nb_samples,
1305 s->channels, s->sample_fmt)) < 0)
1313 av_frame_free(&frame);
1317 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1319 const AVFrame *frame,
1320 int *got_packet_ptr)
1323 AVFrame *padded_frame = NULL;
1325 int user_packet = !!avpkt->data;
1327 *got_packet_ptr = 0;
1329 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1330 av_free_packet(avpkt);
1331 av_init_packet(avpkt);
1335 /* ensure that extended_data is properly set */
1336 if (frame && !frame->extended_data) {
1337 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1338 avctx->channels > AV_NUM_DATA_POINTERS) {
1339 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1340 "with more than %d channels, but extended_data is not set.\n",
1341 AV_NUM_DATA_POINTERS);
1342 return AVERROR(EINVAL);
1344 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1347 tmp.extended_data = tmp.data;
1351 /* check for valid frame size */
1353 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1354 if (frame->nb_samples > avctx->frame_size)
1355 return AVERROR(EINVAL);
1356 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1357 if (frame->nb_samples < avctx->frame_size &&
1358 !avctx->internal->last_audio_frame) {
1359 ret = pad_last_frame(avctx, &padded_frame, frame);
1363 frame = padded_frame;
1364 avctx->internal->last_audio_frame = 1;
1367 if (frame->nb_samples != avctx->frame_size) {
1368 ret = AVERROR(EINVAL);
1374 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1376 if (*got_packet_ptr) {
1377 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1378 if (avpkt->pts == AV_NOPTS_VALUE)
1379 avpkt->pts = frame->pts;
1380 if (!avpkt->duration)
1381 avpkt->duration = ff_samples_to_time_base(avctx,
1384 avpkt->dts = avpkt->pts;
1389 if (!user_packet && avpkt->size) {
1390 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1392 avpkt->data = avpkt->buf->data;
1395 avctx->frame_number++;
1398 if (ret < 0 || !*got_packet_ptr) {
1399 av_free_packet(avpkt);
1400 av_init_packet(avpkt);
1404 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1405 * this needs to be moved to the encoders, but for now we can do it
1406 * here to simplify things */
1407 avpkt->flags |= AV_PKT_FLAG_KEY;
1410 av_frame_free(&padded_frame);
1415 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1417 const AVFrame *frame,
1418 int *got_packet_ptr)
1421 int user_packet = !!avpkt->data;
1423 *got_packet_ptr = 0;
1425 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1426 av_free_packet(avpkt);
1427 av_init_packet(avpkt);
1432 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1433 return AVERROR(EINVAL);
1435 av_assert0(avctx->codec->encode2);
1437 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1439 if (!*got_packet_ptr)
1441 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1442 avpkt->pts = avpkt->dts = frame->pts;
1444 if (!user_packet && avpkt->size) {
1445 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1447 avpkt->data = avpkt->buf->data;
1450 avctx->frame_number++;
1453 if (ret < 0 || !*got_packet_ptr)
1454 av_free_packet(avpkt);
1460 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1461 const AVSubtitle *sub)
1464 if (sub->start_display_time) {
1465 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1468 if (sub->num_rects == 0 || !sub->rects)
1470 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1471 avctx->frame_number++;
1475 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1478 const uint8_t *data;
1481 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1485 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1486 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1487 "changes, but PARAM_CHANGE side data was sent to it.\n");
1488 return AVERROR(EINVAL);
1494 flags = bytestream_get_le32(&data);
1497 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1500 avctx->channels = bytestream_get_le32(&data);
1503 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1506 avctx->channel_layout = bytestream_get_le64(&data);
1509 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1512 avctx->sample_rate = bytestream_get_le32(&data);
1515 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1518 avctx->width = bytestream_get_le32(&data);
1519 avctx->height = bytestream_get_le32(&data);
1521 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1528 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1529 return AVERROR_INVALIDDATA;
1532 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1536 /* move the original frame to our backup */
1537 av_frame_unref(avci->to_free);
1538 av_frame_move_ref(avci->to_free, frame);
1540 /* now copy everything except the AVBufferRefs back
1541 * note that we make a COPY of the side data, so calling av_frame_free() on
1542 * the caller's frame will work properly */
1543 ret = av_frame_copy_props(frame, avci->to_free);
1547 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1548 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1549 if (avci->to_free->extended_data != avci->to_free->data) {
1550 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1551 int size = planes * sizeof(*frame->extended_data);
1554 av_frame_unref(frame);
1558 frame->extended_data = av_malloc(size);
1559 if (!frame->extended_data) {
1560 av_frame_unref(frame);
1561 return AVERROR(ENOMEM);
1563 memcpy(frame->extended_data, avci->to_free->extended_data,
1566 frame->extended_data = frame->data;
1568 frame->format = avci->to_free->format;
1569 frame->width = avci->to_free->width;
1570 frame->height = avci->to_free->height;
1571 frame->channel_layout = avci->to_free->channel_layout;
1572 frame->nb_samples = avci->to_free->nb_samples;
1577 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1578 int *got_picture_ptr,
1581 AVCodecInternal *avci = avctx->internal;
1584 *got_picture_ptr = 0;
1585 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1588 avctx->internal->pkt = avpkt;
1589 ret = apply_param_change(avctx, avpkt);
1591 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1592 if (avctx->err_recognition & AV_EF_EXPLODE)
1596 av_frame_unref(picture);
1598 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1599 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1600 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1603 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1605 picture->pkt_dts = avpkt->dts;
1606 /* get_buffer is supposed to set frame parameters */
1607 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1608 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1609 picture->width = avctx->width;
1610 picture->height = avctx->height;
1611 picture->format = avctx->pix_fmt;
1615 emms_c(); //needed to avoid an emms_c() call before every return;
1617 if (*got_picture_ptr) {
1618 if (!avctx->refcounted_frames) {
1619 int err = unrefcount_frame(avci, picture);
1624 avctx->frame_number++;
1626 av_frame_unref(picture);
1633 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1638 AVCodecInternal *avci = avctx->internal;
1643 avctx->internal->pkt = avpkt;
1645 if (!avpkt->data && avpkt->size) {
1646 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1647 return AVERROR(EINVAL);
1650 ret = apply_param_change(avctx, avpkt);
1652 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1653 if (avctx->err_recognition & AV_EF_EXPLODE)
1657 av_frame_unref(frame);
1659 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1660 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1661 if (ret >= 0 && *got_frame_ptr) {
1662 avctx->frame_number++;
1663 frame->pkt_dts = avpkt->dts;
1664 if (frame->format == AV_SAMPLE_FMT_NONE)
1665 frame->format = avctx->sample_fmt;
1667 if (!avctx->refcounted_frames) {
1668 int err = unrefcount_frame(avci, frame);
1673 av_frame_unref(frame);
1680 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1686 avctx->internal->pkt = avpkt;
1688 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1690 avctx->frame_number++;
1694 void avsubtitle_free(AVSubtitle *sub)
1698 for (i = 0; i < sub->num_rects; i++) {
1699 av_freep(&sub->rects[i]->pict.data[0]);
1700 av_freep(&sub->rects[i]->pict.data[1]);
1701 av_freep(&sub->rects[i]->pict.data[2]);
1702 av_freep(&sub->rects[i]->pict.data[3]);
1703 av_freep(&sub->rects[i]->text);
1704 av_freep(&sub->rects[i]->ass);
1705 av_freep(&sub->rects[i]);
1708 av_freep(&sub->rects);
1710 memset(sub, 0, sizeof(AVSubtitle));
1713 av_cold int avcodec_close(AVCodecContext *avctx)
1715 if (avcodec_is_open(avctx)) {
1716 FramePool *pool = avctx->internal->pool;
1718 if (HAVE_THREADS && avctx->internal->thread_ctx)
1719 ff_thread_free(avctx);
1720 if (avctx->codec && avctx->codec->close)
1721 avctx->codec->close(avctx);
1722 avctx->coded_frame = NULL;
1723 av_frame_free(&avctx->internal->to_free);
1724 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1725 av_buffer_pool_uninit(&pool->pools[i]);
1726 av_freep(&avctx->internal->pool);
1728 if (avctx->hwaccel && avctx->hwaccel->uninit)
1729 avctx->hwaccel->uninit(avctx);
1730 av_freep(&avctx->internal->hwaccel_priv_data);
1732 av_freep(&avctx->internal);
1735 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1736 av_opt_free(avctx->priv_data);
1738 av_freep(&avctx->priv_data);
1739 if (av_codec_is_encoder(avctx->codec))
1740 av_freep(&avctx->extradata);
1741 avctx->codec = NULL;
1742 avctx->active_thread_type = 0;
1747 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1749 AVCodec *p, *experimental = NULL;
1752 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1754 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1761 return experimental;
1764 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1766 return find_encdec(id, 1);
1769 AVCodec *avcodec_find_encoder_by_name(const char *name)
1776 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1783 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1785 return find_encdec(id, 0);
1788 AVCodec *avcodec_find_decoder_by_name(const char *name)
1795 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1802 static int get_bit_rate(AVCodecContext *ctx)
1805 int bits_per_sample;
1807 switch (ctx->codec_type) {
1808 case AVMEDIA_TYPE_VIDEO:
1809 case AVMEDIA_TYPE_DATA:
1810 case AVMEDIA_TYPE_SUBTITLE:
1811 case AVMEDIA_TYPE_ATTACHMENT:
1812 bit_rate = ctx->bit_rate;
1814 case AVMEDIA_TYPE_AUDIO:
1815 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1816 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1825 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1827 int i, len, ret = 0;
1829 #define TAG_PRINT(x) \
1830 (((x) >= '0' && (x) <= '9') || \
1831 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1832 ((x) == '.' || (x) == ' '))
1834 for (i = 0; i < 4; i++) {
1835 len = snprintf(buf, buf_size,
1836 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1838 buf_size = buf_size > len ? buf_size - len : 0;
1845 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1847 const char *codec_name;
1848 const char *profile = NULL;
1852 AVRational display_aspect_ratio;
1857 p = avcodec_find_encoder(enc->codec_id);
1859 p = avcodec_find_decoder(enc->codec_id);
1862 codec_name = p->name;
1863 profile = av_get_profile_name(p, enc->profile);
1864 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1865 /* fake mpeg2 transport stream codec (currently not
1867 codec_name = "mpeg2ts";
1869 /* output avi tags */
1871 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1872 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1876 switch (enc->codec_type) {
1877 case AVMEDIA_TYPE_VIDEO:
1878 snprintf(buf, buf_size,
1880 codec_name, enc->mb_decision ? " (hq)" : "");
1882 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1884 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1885 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1887 av_get_pix_fmt_name(enc->pix_fmt));
1890 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1892 enc->width, enc->height);
1893 if (enc->sample_aspect_ratio.num) {
1894 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1895 enc->width * enc->sample_aspect_ratio.num,
1896 enc->height * enc->sample_aspect_ratio.den,
1898 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1899 " [PAR %d:%d DAR %d:%d]",
1900 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1901 display_aspect_ratio.num, display_aspect_ratio.den);
1903 if (av_log_get_level() >= AV_LOG_DEBUG) {
1904 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1905 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1907 enc->time_base.num / g, enc->time_base.den / g);
1911 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1912 ", q=%d-%d", enc->qmin, enc->qmax);
1915 case AVMEDIA_TYPE_AUDIO:
1916 snprintf(buf, buf_size,
1920 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1922 if (enc->sample_rate) {
1923 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1924 ", %d Hz", enc->sample_rate);
1926 av_strlcat(buf, ", ", buf_size);
1927 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1928 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1929 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1930 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1933 case AVMEDIA_TYPE_DATA:
1934 snprintf(buf, buf_size, "Data: %s", codec_name);
1936 case AVMEDIA_TYPE_SUBTITLE:
1937 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1939 case AVMEDIA_TYPE_ATTACHMENT:
1940 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1943 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1947 if (enc->flags & CODEC_FLAG_PASS1)
1948 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1950 if (enc->flags & CODEC_FLAG_PASS2)
1951 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1954 bitrate = get_bit_rate(enc);
1956 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1957 ", %d kb/s", bitrate / 1000);
1961 const char *av_get_profile_name(const AVCodec *codec, int profile)
1964 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1967 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1968 if (p->profile == profile)
1974 unsigned avcodec_version(void)
1976 return LIBAVCODEC_VERSION_INT;
1979 const char *avcodec_configuration(void)
1981 return LIBAV_CONFIGURATION;
1984 const char *avcodec_license(void)
1986 #define LICENSE_PREFIX "libavcodec license: "
1987 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1990 void avcodec_flush_buffers(AVCodecContext *avctx)
1992 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1993 ff_thread_flush(avctx);
1994 else if (avctx->codec->flush)
1995 avctx->codec->flush(avctx);
1997 if (!avctx->refcounted_frames)
1998 av_frame_unref(avctx->internal->to_free);
2001 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2004 case AV_CODEC_ID_ADPCM_CT:
2005 case AV_CODEC_ID_ADPCM_IMA_APC:
2006 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2007 case AV_CODEC_ID_ADPCM_IMA_WS:
2008 case AV_CODEC_ID_ADPCM_G722:
2009 case AV_CODEC_ID_ADPCM_YAMAHA:
2011 case AV_CODEC_ID_PCM_ALAW:
2012 case AV_CODEC_ID_PCM_MULAW:
2013 case AV_CODEC_ID_PCM_S8:
2014 case AV_CODEC_ID_PCM_U8:
2015 case AV_CODEC_ID_PCM_ZORK:
2017 case AV_CODEC_ID_PCM_S16BE:
2018 case AV_CODEC_ID_PCM_S16LE:
2019 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2020 case AV_CODEC_ID_PCM_U16BE:
2021 case AV_CODEC_ID_PCM_U16LE:
2023 case AV_CODEC_ID_PCM_S24DAUD:
2024 case AV_CODEC_ID_PCM_S24BE:
2025 case AV_CODEC_ID_PCM_S24LE:
2026 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2027 case AV_CODEC_ID_PCM_U24BE:
2028 case AV_CODEC_ID_PCM_U24LE:
2030 case AV_CODEC_ID_PCM_S32BE:
2031 case AV_CODEC_ID_PCM_S32LE:
2032 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2033 case AV_CODEC_ID_PCM_U32BE:
2034 case AV_CODEC_ID_PCM_U32LE:
2035 case AV_CODEC_ID_PCM_F32BE:
2036 case AV_CODEC_ID_PCM_F32LE:
2038 case AV_CODEC_ID_PCM_F64BE:
2039 case AV_CODEC_ID_PCM_F64LE:
2046 int av_get_bits_per_sample(enum AVCodecID codec_id)
2049 case AV_CODEC_ID_ADPCM_SBPRO_2:
2051 case AV_CODEC_ID_ADPCM_SBPRO_3:
2053 case AV_CODEC_ID_ADPCM_SBPRO_4:
2054 case AV_CODEC_ID_ADPCM_IMA_WAV:
2055 case AV_CODEC_ID_ADPCM_IMA_QT:
2056 case AV_CODEC_ID_ADPCM_SWF:
2057 case AV_CODEC_ID_ADPCM_MS:
2060 return av_get_exact_bits_per_sample(codec_id);
2064 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2066 int id, sr, ch, ba, tag, bps;
2068 id = avctx->codec_id;
2069 sr = avctx->sample_rate;
2070 ch = avctx->channels;
2071 ba = avctx->block_align;
2072 tag = avctx->codec_tag;
2073 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2075 /* codecs with an exact constant bits per sample */
2076 if (bps > 0 && ch > 0 && frame_bytes > 0)
2077 return (frame_bytes * 8) / (bps * ch);
2078 bps = avctx->bits_per_coded_sample;
2080 /* codecs with a fixed packet duration */
2082 case AV_CODEC_ID_ADPCM_ADX: return 32;
2083 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2084 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2085 case AV_CODEC_ID_AMR_NB:
2086 case AV_CODEC_ID_GSM:
2087 case AV_CODEC_ID_QCELP:
2088 case AV_CODEC_ID_RA_144:
2089 case AV_CODEC_ID_RA_288: return 160;
2090 case AV_CODEC_ID_IMC: return 256;
2091 case AV_CODEC_ID_AMR_WB:
2092 case AV_CODEC_ID_GSM_MS: return 320;
2093 case AV_CODEC_ID_MP1: return 384;
2094 case AV_CODEC_ID_ATRAC1: return 512;
2095 case AV_CODEC_ID_ATRAC3: return 1024;
2096 case AV_CODEC_ID_MP2:
2097 case AV_CODEC_ID_MUSEPACK7: return 1152;
2098 case AV_CODEC_ID_AC3: return 1536;
2102 /* calc from sample rate */
2103 if (id == AV_CODEC_ID_TTA)
2104 return 256 * sr / 245;
2107 /* calc from sample rate and channels */
2108 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2109 return (480 << (sr / 22050)) / ch;
2114 /* calc from block_align */
2115 if (id == AV_CODEC_ID_SIPR) {
2117 case 20: return 160;
2118 case 19: return 144;
2119 case 29: return 288;
2120 case 37: return 480;
2122 } else if (id == AV_CODEC_ID_ILBC) {
2124 case 38: return 160;
2125 case 50: return 240;
2130 if (frame_bytes > 0) {
2131 /* calc from frame_bytes only */
2132 if (id == AV_CODEC_ID_TRUESPEECH)
2133 return 240 * (frame_bytes / 32);
2134 if (id == AV_CODEC_ID_NELLYMOSER)
2135 return 256 * (frame_bytes / 64);
2138 /* calc from frame_bytes and bits_per_coded_sample */
2139 if (id == AV_CODEC_ID_ADPCM_G726)
2140 return frame_bytes * 8 / bps;
2144 /* calc from frame_bytes and channels */
2146 case AV_CODEC_ID_ADPCM_4XM:
2147 case AV_CODEC_ID_ADPCM_IMA_ISS:
2148 return (frame_bytes - 4 * ch) * 2 / ch;
2149 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2150 return (frame_bytes - 4) * 2 / ch;
2151 case AV_CODEC_ID_ADPCM_IMA_AMV:
2152 return (frame_bytes - 8) * 2 / ch;
2153 case AV_CODEC_ID_ADPCM_XA:
2154 return (frame_bytes / 128) * 224 / ch;
2155 case AV_CODEC_ID_INTERPLAY_DPCM:
2156 return (frame_bytes - 6 - ch) / ch;
2157 case AV_CODEC_ID_ROQ_DPCM:
2158 return (frame_bytes - 8) / ch;
2159 case AV_CODEC_ID_XAN_DPCM:
2160 return (frame_bytes - 2 * ch) / ch;
2161 case AV_CODEC_ID_MACE3:
2162 return 3 * frame_bytes / ch;
2163 case AV_CODEC_ID_MACE6:
2164 return 6 * frame_bytes / ch;
2165 case AV_CODEC_ID_PCM_LXF:
2166 return 2 * (frame_bytes / (5 * ch));
2170 /* calc from frame_bytes, channels, and codec_tag */
2171 if (id == AV_CODEC_ID_SOL_DPCM) {
2173 return frame_bytes / ch;
2175 return frame_bytes * 2 / ch;
2180 /* calc from frame_bytes, channels, and block_align */
2181 int blocks = frame_bytes / ba;
2182 switch (avctx->codec_id) {
2183 case AV_CODEC_ID_ADPCM_IMA_WAV:
2184 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2185 case AV_CODEC_ID_ADPCM_IMA_DK3:
2186 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2187 case AV_CODEC_ID_ADPCM_IMA_DK4:
2188 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2189 case AV_CODEC_ID_ADPCM_MS:
2190 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2195 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2196 switch (avctx->codec_id) {
2197 case AV_CODEC_ID_PCM_DVD:
2198 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2199 case AV_CODEC_ID_PCM_BLURAY:
2200 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2201 case AV_CODEC_ID_S302M:
2202 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2212 int ff_thread_init(AVCodecContext *s)
2219 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2233 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2236 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2240 #if FF_API_MISSING_SAMPLE
2241 FF_DISABLE_DEPRECATION_WARNINGS
2242 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2244 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2245 "version to the newest one from Git. If the problem still "
2246 "occurs, it means that your file has a feature which has not "
2247 "been implemented.\n", feature);
2249 av_log_ask_for_sample(avc, NULL);
2252 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2254 va_list argument_list;
2256 va_start(argument_list, msg);
2259 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2260 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2261 "of this file to ftp://upload.libav.org/incoming/ "
2262 "and contact the libav-devel mailing list.\n");
2264 va_end(argument_list);
2266 FF_ENABLE_DEPRECATION_WARNINGS
2267 #endif /* FF_API_MISSING_SAMPLE */
2269 static AVHWAccel *first_hwaccel = NULL;
2271 void av_register_hwaccel(AVHWAccel *hwaccel)
2273 AVHWAccel **p = &first_hwaccel;
2277 hwaccel->next = NULL;
2280 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2282 return hwaccel ? hwaccel->next : first_hwaccel;
2285 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2288 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2290 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2297 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2299 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2305 int avpriv_lock_avformat(void)
2308 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2314 int avpriv_unlock_avformat(void)
2317 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2323 unsigned int avpriv_toupper4(unsigned int x)
2325 return av_toupper(x & 0xFF) +
2326 (av_toupper((x >> 8) & 0xFF) << 8) +
2327 (av_toupper((x >> 16) & 0xFF) << 16) +
2328 (av_toupper((x >> 24) & 0xFF) << 24);
2331 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2335 dst->owner = src->owner;
2337 ret = av_frame_ref(dst->f, src->f);
2341 if (src->progress &&
2342 !(dst->progress = av_buffer_ref(src->progress))) {
2343 ff_thread_release_buffer(dst->owner, dst);
2344 return AVERROR(ENOMEM);
2352 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2355 return ff_get_buffer(avctx, f->f, flags);
2358 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2361 av_frame_unref(f->f);
2364 void ff_thread_finish_setup(AVCodecContext *avctx)
2368 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2372 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2378 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2380 if (codec_id <= AV_CODEC_ID_NONE)
2381 return AVMEDIA_TYPE_UNKNOWN;
2382 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2383 return AVMEDIA_TYPE_VIDEO;
2384 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2385 return AVMEDIA_TYPE_AUDIO;
2386 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2387 return AVMEDIA_TYPE_SUBTITLE;
2389 return AVMEDIA_TYPE_UNKNOWN;
2392 int avcodec_is_open(AVCodecContext *s)
2394 return !!s->internal;
2397 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2399 uint32_t * restrict state)
2407 for (i = 0; i < 3; i++) {
2408 uint32_t tmp = *state << 8;
2409 *state = tmp + *(p++);
2410 if (tmp == 0x100 || p == end)
2415 if (p[-1] > 1 ) p += 3;
2416 else if (p[-2] ) p += 2;
2417 else if (p[-3]|(p[-1]-1)) p++;
2424 p = FFMIN(p, end) - 4;
2425 *state = AV_RB32(p);