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 #if FF_API_AVFRAME_COLORSPACE
580 frame->color_primaries = avctx->color_primaries;
581 frame->color_trc = avctx->color_trc;
582 frame->colorspace = avctx->colorspace;
583 frame->color_range = avctx->color_range;
584 frame->chroma_location = avctx->chroma_sample_location;
587 frame->reordered_opaque = avctx->reordered_opaque;
589 frame->pkt_pts = AV_NOPTS_VALUE;
593 frame->pkt_pts = pkt->pts;
595 /* copy the replaygain data to the output frame */
596 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
598 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
600 return AVERROR(ENOMEM);
602 memcpy(frame_sd->data, packet_sd, size);
604 /* copy the displaymatrix to the output frame */
605 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
607 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, size);
609 return AVERROR(ENOMEM);
611 memcpy(frame_sd->data, packet_sd, size);
617 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
619 const AVHWAccel *hwaccel = avctx->hwaccel;
620 int override_dimensions = 1;
623 switch (avctx->codec_type) {
624 case AVMEDIA_TYPE_VIDEO:
625 if (frame->width <= 0 || frame->height <= 0) {
626 frame->width = FFMAX(avctx->width, avctx->coded_width);
627 frame->height = FFMAX(avctx->height, avctx->coded_height);
628 override_dimensions = 0;
630 if (frame->format < 0)
631 frame->format = avctx->pix_fmt;
632 if (!frame->sample_aspect_ratio.num)
633 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
635 if (av_image_check_sar(frame->width, frame->height,
636 frame->sample_aspect_ratio) < 0) {
637 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
638 frame->sample_aspect_ratio.num,
639 frame->sample_aspect_ratio.den);
640 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
643 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
646 case AVMEDIA_TYPE_AUDIO:
647 if (!frame->sample_rate)
648 frame->sample_rate = avctx->sample_rate;
649 if (frame->format < 0)
650 frame->format = avctx->sample_fmt;
651 if (!frame->channel_layout) {
652 if (avctx->channel_layout) {
653 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
655 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
657 return AVERROR(EINVAL);
660 frame->channel_layout = avctx->channel_layout;
662 if (avctx->channels > FF_SANE_NB_CHANNELS) {
663 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
665 return AVERROR(ENOSYS);
668 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
669 if (!frame->channel_layout)
670 frame->channel_layout = (1ULL << avctx->channels) - 1;
674 default: return AVERROR(EINVAL);
677 ret = ff_decode_frame_props(avctx, frame);
681 if (hwaccel && hwaccel->alloc_frame) {
682 ret = hwaccel->alloc_frame(avctx, frame);
686 #if FF_API_GET_BUFFER
687 FF_DISABLE_DEPRECATION_WARNINGS
689 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
690 * We wrap each plane in its own AVBuffer. Each of those has a reference to
691 * a dummy AVBuffer as its private data, unreffing it on free.
692 * When all the planes are freed, the dummy buffer's free callback calls
695 if (avctx->get_buffer) {
696 CompatReleaseBufPriv *priv = NULL;
697 AVBufferRef *dummy_buf = NULL;
700 if (flags & AV_GET_BUFFER_FLAG_REF)
701 frame->reference = 1;
703 ret = avctx->get_buffer(avctx, frame);
707 /* return if the buffers are already set up
708 * this would happen e.g. when a custom get_buffer() calls
709 * avcodec_default_get_buffer
714 priv = av_mallocz(sizeof(*priv));
716 ret = AVERROR(ENOMEM);
719 priv->avctx = *avctx;
720 priv->frame = *frame;
722 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
724 ret = AVERROR(ENOMEM);
728 #define WRAP_PLANE(ref_out, data, data_size) \
730 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
732 ret = AVERROR(ENOMEM); \
735 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
738 av_frame_unref(frame); \
739 ret = AVERROR(ENOMEM); \
744 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
745 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
747 planes = av_pix_fmt_count_planes(frame->format);
748 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
749 check for allocated buffers: make libavcodec happy */
750 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
752 if (!desc || planes <= 0) {
753 ret = AVERROR(EINVAL);
757 for (i = 0; i < planes; i++) {
758 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
759 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
761 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
764 int planar = av_sample_fmt_is_planar(frame->format);
765 planes = planar ? avctx->channels : 1;
767 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
768 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
769 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
770 frame->nb_extended_buf);
771 if (!frame->extended_buf) {
772 ret = AVERROR(ENOMEM);
777 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
778 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
780 for (i = 0; i < frame->nb_extended_buf; i++)
781 WRAP_PLANE(frame->extended_buf[i],
782 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
786 av_buffer_unref(&dummy_buf);
788 frame->width = avctx->width;
789 frame->height = avctx->height;
794 avctx->release_buffer(avctx, frame);
796 av_buffer_unref(&dummy_buf);
799 FF_ENABLE_DEPRECATION_WARNINGS
802 ret = avctx->get_buffer2(avctx, frame, flags);
805 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
806 frame->width = avctx->width;
807 frame->height = avctx->height;
813 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
818 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
821 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
823 if (av_frame_is_writable(frame))
824 return ff_decode_frame_props(avctx, frame);
826 tmp = av_frame_alloc();
828 return AVERROR(ENOMEM);
830 av_frame_move_ref(tmp, frame);
832 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
838 av_frame_copy(frame, tmp);
844 #if FF_API_GET_BUFFER
845 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
850 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
857 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
861 for (i = 0; i < count; i++) {
862 int r = func(c, (char *)arg + i * size);
869 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
873 for (i = 0; i < count; i++) {
874 int r = func(c, arg, i, 0);
881 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
883 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
884 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
887 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
889 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
894 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
895 enum AVPixelFormat pix_fmt)
897 AVHWAccel *hwaccel = NULL;
899 while ((hwaccel = av_hwaccel_next(hwaccel)))
900 if (hwaccel->id == codec_id
901 && hwaccel->pix_fmt == pix_fmt)
907 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
909 const AVPixFmtDescriptor *desc;
910 enum AVPixelFormat ret = avctx->get_format(avctx, fmt);
912 desc = av_pix_fmt_desc_get(ret);
914 return AV_PIX_FMT_NONE;
916 if (avctx->hwaccel && avctx->hwaccel->uninit)
917 avctx->hwaccel->uninit(avctx);
918 av_freep(&avctx->internal->hwaccel_priv_data);
919 avctx->hwaccel = NULL;
921 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
925 hwaccel = find_hwaccel(avctx->codec_id, ret);
927 av_log(avctx, AV_LOG_ERROR,
928 "Could not find an AVHWAccel for the pixel format: %s",
930 return AV_PIX_FMT_NONE;
933 if (hwaccel->priv_data_size) {
934 avctx->internal->hwaccel_priv_data = av_mallocz(hwaccel->priv_data_size);
935 if (!avctx->internal->hwaccel_priv_data)
936 return AV_PIX_FMT_NONE;
940 err = hwaccel->init(avctx);
942 av_freep(&avctx->internal->hwaccel_priv_data);
943 return AV_PIX_FMT_NONE;
946 avctx->hwaccel = hwaccel;
952 #if FF_API_AVFRAME_LAVC
953 void avcodec_get_frame_defaults(AVFrame *frame)
955 if (frame->extended_data != frame->data)
956 av_freep(&frame->extended_data);
958 memset(frame, 0, sizeof(AVFrame));
960 frame->pts = AV_NOPTS_VALUE;
961 frame->key_frame = 1;
962 frame->sample_aspect_ratio = (AVRational) {0, 1 };
963 frame->format = -1; /* unknown */
964 frame->extended_data = frame->data;
967 AVFrame *avcodec_alloc_frame(void)
969 AVFrame *frame = av_mallocz(sizeof(AVFrame));
974 FF_DISABLE_DEPRECATION_WARNINGS
975 avcodec_get_frame_defaults(frame);
976 FF_ENABLE_DEPRECATION_WARNINGS
981 void avcodec_free_frame(AVFrame **frame)
983 av_frame_free(frame);
987 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
990 AVDictionary *tmp = NULL;
992 if (avcodec_is_open(avctx))
995 if ((!codec && !avctx->codec)) {
996 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
997 return AVERROR(EINVAL);
999 if ((codec && avctx->codec && codec != avctx->codec)) {
1000 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1001 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
1002 return AVERROR(EINVAL);
1005 codec = avctx->codec;
1007 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1008 return AVERROR(EINVAL);
1011 av_dict_copy(&tmp, *options, 0);
1013 /* If there is a user-supplied mutex locking routine, call it. */
1015 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1019 entangled_thread_counter++;
1020 if (entangled_thread_counter != 1) {
1021 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1026 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1027 if (!avctx->internal) {
1028 ret = AVERROR(ENOMEM);
1032 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1033 if (!avctx->internal->pool) {
1034 ret = AVERROR(ENOMEM);
1038 avctx->internal->to_free = av_frame_alloc();
1039 if (!avctx->internal->to_free) {
1040 ret = AVERROR(ENOMEM);
1044 if (codec->priv_data_size > 0) {
1045 if (!avctx->priv_data) {
1046 avctx->priv_data = av_mallocz(codec->priv_data_size);
1047 if (!avctx->priv_data) {
1048 ret = AVERROR(ENOMEM);
1051 if (codec->priv_class) {
1052 *(const AVClass **)avctx->priv_data = codec->priv_class;
1053 av_opt_set_defaults(avctx->priv_data);
1056 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1059 avctx->priv_data = NULL;
1061 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1064 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
1065 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1066 else if (avctx->width && avctx->height)
1067 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1071 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1072 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1073 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1074 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
1075 ff_set_dimensions(avctx, 0, 0);
1078 if (avctx->width > 0 && avctx->height > 0) {
1079 if (av_image_check_sar(avctx->width, avctx->height,
1080 avctx->sample_aspect_ratio) < 0) {
1081 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1082 avctx->sample_aspect_ratio.num,
1083 avctx->sample_aspect_ratio.den);
1084 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1088 /* if the decoder init function was already called previously,
1089 * free the already allocated subtitle_header before overwriting it */
1090 if (av_codec_is_decoder(codec))
1091 av_freep(&avctx->subtitle_header);
1093 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1094 ret = AVERROR(EINVAL);
1098 avctx->codec = codec;
1099 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1100 avctx->codec_id == AV_CODEC_ID_NONE) {
1101 avctx->codec_type = codec->type;
1102 avctx->codec_id = codec->id;
1104 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1105 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1106 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
1107 ret = AVERROR(EINVAL);
1110 avctx->frame_number = 0;
1112 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1113 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1114 ret = AVERROR_EXPERIMENTAL;
1118 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1119 (!avctx->time_base.num || !avctx->time_base.den)) {
1120 avctx->time_base.num = 1;
1121 avctx->time_base.den = avctx->sample_rate;
1125 ret = ff_thread_init(avctx);
1130 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1131 avctx->thread_count = 1;
1133 if (av_codec_is_encoder(avctx->codec)) {
1135 if (avctx->codec->sample_fmts) {
1136 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1137 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1139 if (avctx->channels == 1 &&
1140 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1141 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1142 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1146 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1147 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1148 ret = AVERROR(EINVAL);
1152 if (avctx->codec->pix_fmts) {
1153 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1154 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1156 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1157 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1158 ret = AVERROR(EINVAL);
1162 if (avctx->codec->supported_samplerates) {
1163 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1164 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1166 if (avctx->codec->supported_samplerates[i] == 0) {
1167 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1168 ret = AVERROR(EINVAL);
1172 if (avctx->codec->channel_layouts) {
1173 if (!avctx->channel_layout) {
1174 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1176 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1177 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1179 if (avctx->codec->channel_layouts[i] == 0) {
1180 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1181 ret = AVERROR(EINVAL);
1186 if (avctx->channel_layout && avctx->channels) {
1187 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1188 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1189 ret = AVERROR(EINVAL);
1192 } else if (avctx->channel_layout) {
1193 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1196 if (!avctx->rc_initial_buffer_occupancy)
1197 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1200 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1201 ret = avctx->codec->init(avctx);
1207 if (av_codec_is_decoder(avctx->codec)) {
1208 /* validate channel layout from the decoder */
1209 if (avctx->channel_layout) {
1210 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1211 if (!avctx->channels)
1212 avctx->channels = channels;
1213 else if (channels != avctx->channels) {
1214 av_log(avctx, AV_LOG_WARNING,
1215 "channel layout does not match number of channels\n");
1216 avctx->channel_layout = 0;
1219 if (avctx->channels && avctx->channels < 0 ||
1220 avctx->channels > FF_SANE_NB_CHANNELS) {
1221 ret = AVERROR(EINVAL);
1226 entangled_thread_counter--;
1228 /* Release any user-supplied mutex. */
1230 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1233 av_dict_free(options);
1240 av_freep(&avctx->priv_data);
1241 if (avctx->internal) {
1242 av_frame_free(&avctx->internal->to_free);
1243 av_freep(&avctx->internal->pool);
1245 av_freep(&avctx->internal);
1246 avctx->codec = NULL;
1250 int ff_alloc_packet(AVPacket *avpkt, int size)
1252 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1253 return AVERROR(EINVAL);
1256 AVBufferRef *buf = avpkt->buf;
1257 #if FF_API_DESTRUCT_PACKET
1258 FF_DISABLE_DEPRECATION_WARNINGS
1259 void *destruct = avpkt->destruct;
1260 FF_ENABLE_DEPRECATION_WARNINGS
1263 if (avpkt->size < size)
1264 return AVERROR(EINVAL);
1266 av_init_packet(avpkt);
1267 #if FF_API_DESTRUCT_PACKET
1268 FF_DISABLE_DEPRECATION_WARNINGS
1269 avpkt->destruct = destruct;
1270 FF_ENABLE_DEPRECATION_WARNINGS
1276 return av_new_packet(avpkt, size);
1281 * Pad last frame with silence.
1283 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1285 AVFrame *frame = NULL;
1288 if (!(frame = av_frame_alloc()))
1289 return AVERROR(ENOMEM);
1291 frame->format = src->format;
1292 frame->channel_layout = src->channel_layout;
1293 frame->nb_samples = s->frame_size;
1294 ret = av_frame_get_buffer(frame, 32);
1298 ret = av_frame_copy_props(frame, src);
1302 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1303 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1305 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1306 frame->nb_samples - src->nb_samples,
1307 s->channels, s->sample_fmt)) < 0)
1315 av_frame_free(&frame);
1319 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1321 const AVFrame *frame,
1322 int *got_packet_ptr)
1325 AVFrame *padded_frame = NULL;
1327 int user_packet = !!avpkt->data;
1329 *got_packet_ptr = 0;
1331 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1332 av_free_packet(avpkt);
1333 av_init_packet(avpkt);
1337 /* ensure that extended_data is properly set */
1338 if (frame && !frame->extended_data) {
1339 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1340 avctx->channels > AV_NUM_DATA_POINTERS) {
1341 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1342 "with more than %d channels, but extended_data is not set.\n",
1343 AV_NUM_DATA_POINTERS);
1344 return AVERROR(EINVAL);
1346 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1349 tmp.extended_data = tmp.data;
1353 /* check for valid frame size */
1355 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1356 if (frame->nb_samples > avctx->frame_size)
1357 return AVERROR(EINVAL);
1358 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1359 if (frame->nb_samples < avctx->frame_size &&
1360 !avctx->internal->last_audio_frame) {
1361 ret = pad_last_frame(avctx, &padded_frame, frame);
1365 frame = padded_frame;
1366 avctx->internal->last_audio_frame = 1;
1369 if (frame->nb_samples != avctx->frame_size) {
1370 ret = AVERROR(EINVAL);
1376 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1378 if (*got_packet_ptr) {
1379 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1380 if (avpkt->pts == AV_NOPTS_VALUE)
1381 avpkt->pts = frame->pts;
1382 if (!avpkt->duration)
1383 avpkt->duration = ff_samples_to_time_base(avctx,
1386 avpkt->dts = avpkt->pts;
1391 if (!user_packet && avpkt->size) {
1392 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1394 avpkt->data = avpkt->buf->data;
1397 avctx->frame_number++;
1400 if (ret < 0 || !*got_packet_ptr) {
1401 av_free_packet(avpkt);
1402 av_init_packet(avpkt);
1406 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1407 * this needs to be moved to the encoders, but for now we can do it
1408 * here to simplify things */
1409 avpkt->flags |= AV_PKT_FLAG_KEY;
1412 av_frame_free(&padded_frame);
1417 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1419 const AVFrame *frame,
1420 int *got_packet_ptr)
1423 int user_packet = !!avpkt->data;
1425 *got_packet_ptr = 0;
1427 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1428 av_free_packet(avpkt);
1429 av_init_packet(avpkt);
1434 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1435 return AVERROR(EINVAL);
1437 av_assert0(avctx->codec->encode2);
1439 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1441 if (!*got_packet_ptr)
1443 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1444 avpkt->pts = avpkt->dts = frame->pts;
1446 if (!user_packet && avpkt->size) {
1447 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1449 avpkt->data = avpkt->buf->data;
1452 avctx->frame_number++;
1455 if (ret < 0 || !*got_packet_ptr)
1456 av_free_packet(avpkt);
1462 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1463 const AVSubtitle *sub)
1466 if (sub->start_display_time) {
1467 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1470 if (sub->num_rects == 0 || !sub->rects)
1472 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1473 avctx->frame_number++;
1477 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1480 const uint8_t *data;
1483 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1487 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1488 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1489 "changes, but PARAM_CHANGE side data was sent to it.\n");
1490 return AVERROR(EINVAL);
1496 flags = bytestream_get_le32(&data);
1499 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1502 avctx->channels = bytestream_get_le32(&data);
1505 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1508 avctx->channel_layout = bytestream_get_le64(&data);
1511 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1514 avctx->sample_rate = bytestream_get_le32(&data);
1517 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1520 avctx->width = bytestream_get_le32(&data);
1521 avctx->height = bytestream_get_le32(&data);
1523 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1530 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1531 return AVERROR_INVALIDDATA;
1534 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1538 /* move the original frame to our backup */
1539 av_frame_unref(avci->to_free);
1540 av_frame_move_ref(avci->to_free, frame);
1542 /* now copy everything except the AVBufferRefs back
1543 * note that we make a COPY of the side data, so calling av_frame_free() on
1544 * the caller's frame will work properly */
1545 ret = av_frame_copy_props(frame, avci->to_free);
1549 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1550 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1551 if (avci->to_free->extended_data != avci->to_free->data) {
1552 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1553 int size = planes * sizeof(*frame->extended_data);
1556 av_frame_unref(frame);
1560 frame->extended_data = av_malloc(size);
1561 if (!frame->extended_data) {
1562 av_frame_unref(frame);
1563 return AVERROR(ENOMEM);
1565 memcpy(frame->extended_data, avci->to_free->extended_data,
1568 frame->extended_data = frame->data;
1570 frame->format = avci->to_free->format;
1571 frame->width = avci->to_free->width;
1572 frame->height = avci->to_free->height;
1573 frame->channel_layout = avci->to_free->channel_layout;
1574 frame->nb_samples = avci->to_free->nb_samples;
1579 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1580 int *got_picture_ptr,
1583 AVCodecInternal *avci = avctx->internal;
1586 *got_picture_ptr = 0;
1587 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1590 avctx->internal->pkt = avpkt;
1591 ret = apply_param_change(avctx, avpkt);
1593 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1594 if (avctx->err_recognition & AV_EF_EXPLODE)
1598 av_frame_unref(picture);
1600 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1601 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1602 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1605 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1607 picture->pkt_dts = avpkt->dts;
1608 /* get_buffer is supposed to set frame parameters */
1609 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1610 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1611 picture->width = avctx->width;
1612 picture->height = avctx->height;
1613 picture->format = avctx->pix_fmt;
1617 emms_c(); //needed to avoid an emms_c() call before every return;
1619 if (*got_picture_ptr) {
1620 if (!avctx->refcounted_frames) {
1621 int err = unrefcount_frame(avci, picture);
1626 avctx->frame_number++;
1628 av_frame_unref(picture);
1635 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1640 AVCodecInternal *avci = avctx->internal;
1645 avctx->internal->pkt = avpkt;
1647 if (!avpkt->data && avpkt->size) {
1648 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1649 return AVERROR(EINVAL);
1652 ret = apply_param_change(avctx, avpkt);
1654 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1655 if (avctx->err_recognition & AV_EF_EXPLODE)
1659 av_frame_unref(frame);
1661 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1662 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1663 if (ret >= 0 && *got_frame_ptr) {
1664 avctx->frame_number++;
1665 frame->pkt_dts = avpkt->dts;
1666 if (frame->format == AV_SAMPLE_FMT_NONE)
1667 frame->format = avctx->sample_fmt;
1669 if (!avctx->refcounted_frames) {
1670 int err = unrefcount_frame(avci, frame);
1675 av_frame_unref(frame);
1682 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1688 avctx->internal->pkt = avpkt;
1690 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1692 avctx->frame_number++;
1696 void avsubtitle_free(AVSubtitle *sub)
1700 for (i = 0; i < sub->num_rects; i++) {
1701 av_freep(&sub->rects[i]->pict.data[0]);
1702 av_freep(&sub->rects[i]->pict.data[1]);
1703 av_freep(&sub->rects[i]->pict.data[2]);
1704 av_freep(&sub->rects[i]->pict.data[3]);
1705 av_freep(&sub->rects[i]->text);
1706 av_freep(&sub->rects[i]->ass);
1707 av_freep(&sub->rects[i]);
1710 av_freep(&sub->rects);
1712 memset(sub, 0, sizeof(AVSubtitle));
1715 av_cold int avcodec_close(AVCodecContext *avctx)
1717 if (avcodec_is_open(avctx)) {
1718 FramePool *pool = avctx->internal->pool;
1720 if (HAVE_THREADS && avctx->internal->thread_ctx)
1721 ff_thread_free(avctx);
1722 if (avctx->codec && avctx->codec->close)
1723 avctx->codec->close(avctx);
1724 avctx->coded_frame = NULL;
1725 av_frame_free(&avctx->internal->to_free);
1726 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1727 av_buffer_pool_uninit(&pool->pools[i]);
1728 av_freep(&avctx->internal->pool);
1730 if (avctx->hwaccel && avctx->hwaccel->uninit)
1731 avctx->hwaccel->uninit(avctx);
1732 av_freep(&avctx->internal->hwaccel_priv_data);
1734 av_freep(&avctx->internal);
1737 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1738 av_opt_free(avctx->priv_data);
1740 av_freep(&avctx->priv_data);
1741 if (av_codec_is_encoder(avctx->codec))
1742 av_freep(&avctx->extradata);
1743 avctx->codec = NULL;
1744 avctx->active_thread_type = 0;
1749 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1751 AVCodec *p, *experimental = NULL;
1754 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1756 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1763 return experimental;
1766 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1768 return find_encdec(id, 1);
1771 AVCodec *avcodec_find_encoder_by_name(const char *name)
1778 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1785 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1787 return find_encdec(id, 0);
1790 AVCodec *avcodec_find_decoder_by_name(const char *name)
1797 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1804 static int get_bit_rate(AVCodecContext *ctx)
1807 int bits_per_sample;
1809 switch (ctx->codec_type) {
1810 case AVMEDIA_TYPE_VIDEO:
1811 case AVMEDIA_TYPE_DATA:
1812 case AVMEDIA_TYPE_SUBTITLE:
1813 case AVMEDIA_TYPE_ATTACHMENT:
1814 bit_rate = ctx->bit_rate;
1816 case AVMEDIA_TYPE_AUDIO:
1817 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1818 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1827 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1829 int i, len, ret = 0;
1831 #define TAG_PRINT(x) \
1832 (((x) >= '0' && (x) <= '9') || \
1833 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1834 ((x) == '.' || (x) == ' '))
1836 for (i = 0; i < 4; i++) {
1837 len = snprintf(buf, buf_size,
1838 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1840 buf_size = buf_size > len ? buf_size - len : 0;
1847 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1849 const char *codec_name;
1850 const char *profile = NULL;
1854 AVRational display_aspect_ratio;
1859 p = avcodec_find_encoder(enc->codec_id);
1861 p = avcodec_find_decoder(enc->codec_id);
1864 codec_name = p->name;
1865 profile = av_get_profile_name(p, enc->profile);
1866 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1867 /* fake mpeg2 transport stream codec (currently not
1869 codec_name = "mpeg2ts";
1871 /* output avi tags */
1873 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1874 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1878 switch (enc->codec_type) {
1879 case AVMEDIA_TYPE_VIDEO:
1880 snprintf(buf, buf_size,
1882 codec_name, enc->mb_decision ? " (hq)" : "");
1884 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1886 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1887 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1889 av_get_pix_fmt_name(enc->pix_fmt));
1892 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1894 enc->width, enc->height);
1895 if (enc->sample_aspect_ratio.num) {
1896 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1897 enc->width * enc->sample_aspect_ratio.num,
1898 enc->height * enc->sample_aspect_ratio.den,
1900 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1901 " [PAR %d:%d DAR %d:%d]",
1902 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1903 display_aspect_ratio.num, display_aspect_ratio.den);
1905 if (av_log_get_level() >= AV_LOG_DEBUG) {
1906 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1907 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1909 enc->time_base.num / g, enc->time_base.den / g);
1913 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1914 ", q=%d-%d", enc->qmin, enc->qmax);
1917 case AVMEDIA_TYPE_AUDIO:
1918 snprintf(buf, buf_size,
1922 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1924 if (enc->sample_rate) {
1925 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1926 ", %d Hz", enc->sample_rate);
1928 av_strlcat(buf, ", ", buf_size);
1929 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1930 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1931 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1932 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1935 case AVMEDIA_TYPE_DATA:
1936 snprintf(buf, buf_size, "Data: %s", codec_name);
1938 case AVMEDIA_TYPE_SUBTITLE:
1939 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1941 case AVMEDIA_TYPE_ATTACHMENT:
1942 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1945 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1949 if (enc->flags & CODEC_FLAG_PASS1)
1950 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1952 if (enc->flags & CODEC_FLAG_PASS2)
1953 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1956 bitrate = get_bit_rate(enc);
1958 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1959 ", %d kb/s", bitrate / 1000);
1963 const char *av_get_profile_name(const AVCodec *codec, int profile)
1966 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1969 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1970 if (p->profile == profile)
1976 unsigned avcodec_version(void)
1978 return LIBAVCODEC_VERSION_INT;
1981 const char *avcodec_configuration(void)
1983 return LIBAV_CONFIGURATION;
1986 const char *avcodec_license(void)
1988 #define LICENSE_PREFIX "libavcodec license: "
1989 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1992 void avcodec_flush_buffers(AVCodecContext *avctx)
1994 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1995 ff_thread_flush(avctx);
1996 else if (avctx->codec->flush)
1997 avctx->codec->flush(avctx);
1999 if (!avctx->refcounted_frames)
2000 av_frame_unref(avctx->internal->to_free);
2003 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2006 case AV_CODEC_ID_ADPCM_CT:
2007 case AV_CODEC_ID_ADPCM_IMA_APC:
2008 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2009 case AV_CODEC_ID_ADPCM_IMA_WS:
2010 case AV_CODEC_ID_ADPCM_G722:
2011 case AV_CODEC_ID_ADPCM_YAMAHA:
2013 case AV_CODEC_ID_PCM_ALAW:
2014 case AV_CODEC_ID_PCM_MULAW:
2015 case AV_CODEC_ID_PCM_S8:
2016 case AV_CODEC_ID_PCM_U8:
2017 case AV_CODEC_ID_PCM_ZORK:
2019 case AV_CODEC_ID_PCM_S16BE:
2020 case AV_CODEC_ID_PCM_S16LE:
2021 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2022 case AV_CODEC_ID_PCM_U16BE:
2023 case AV_CODEC_ID_PCM_U16LE:
2025 case AV_CODEC_ID_PCM_S24DAUD:
2026 case AV_CODEC_ID_PCM_S24BE:
2027 case AV_CODEC_ID_PCM_S24LE:
2028 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2029 case AV_CODEC_ID_PCM_U24BE:
2030 case AV_CODEC_ID_PCM_U24LE:
2032 case AV_CODEC_ID_PCM_S32BE:
2033 case AV_CODEC_ID_PCM_S32LE:
2034 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2035 case AV_CODEC_ID_PCM_U32BE:
2036 case AV_CODEC_ID_PCM_U32LE:
2037 case AV_CODEC_ID_PCM_F32BE:
2038 case AV_CODEC_ID_PCM_F32LE:
2040 case AV_CODEC_ID_PCM_F64BE:
2041 case AV_CODEC_ID_PCM_F64LE:
2048 int av_get_bits_per_sample(enum AVCodecID codec_id)
2051 case AV_CODEC_ID_ADPCM_SBPRO_2:
2053 case AV_CODEC_ID_ADPCM_SBPRO_3:
2055 case AV_CODEC_ID_ADPCM_SBPRO_4:
2056 case AV_CODEC_ID_ADPCM_IMA_WAV:
2057 case AV_CODEC_ID_ADPCM_IMA_QT:
2058 case AV_CODEC_ID_ADPCM_SWF:
2059 case AV_CODEC_ID_ADPCM_MS:
2062 return av_get_exact_bits_per_sample(codec_id);
2066 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2068 int id, sr, ch, ba, tag, bps;
2070 id = avctx->codec_id;
2071 sr = avctx->sample_rate;
2072 ch = avctx->channels;
2073 ba = avctx->block_align;
2074 tag = avctx->codec_tag;
2075 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2077 /* codecs with an exact constant bits per sample */
2078 if (bps > 0 && ch > 0 && frame_bytes > 0)
2079 return (frame_bytes * 8) / (bps * ch);
2080 bps = avctx->bits_per_coded_sample;
2082 /* codecs with a fixed packet duration */
2084 case AV_CODEC_ID_ADPCM_ADX: return 32;
2085 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2086 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2087 case AV_CODEC_ID_AMR_NB:
2088 case AV_CODEC_ID_GSM:
2089 case AV_CODEC_ID_QCELP:
2090 case AV_CODEC_ID_RA_144:
2091 case AV_CODEC_ID_RA_288: return 160;
2092 case AV_CODEC_ID_IMC: return 256;
2093 case AV_CODEC_ID_AMR_WB:
2094 case AV_CODEC_ID_GSM_MS: return 320;
2095 case AV_CODEC_ID_MP1: return 384;
2096 case AV_CODEC_ID_ATRAC1: return 512;
2097 case AV_CODEC_ID_ATRAC3: return 1024;
2098 case AV_CODEC_ID_MP2:
2099 case AV_CODEC_ID_MUSEPACK7: return 1152;
2100 case AV_CODEC_ID_AC3: return 1536;
2104 /* calc from sample rate */
2105 if (id == AV_CODEC_ID_TTA)
2106 return 256 * sr / 245;
2109 /* calc from sample rate and channels */
2110 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2111 return (480 << (sr / 22050)) / ch;
2116 /* calc from block_align */
2117 if (id == AV_CODEC_ID_SIPR) {
2119 case 20: return 160;
2120 case 19: return 144;
2121 case 29: return 288;
2122 case 37: return 480;
2124 } else if (id == AV_CODEC_ID_ILBC) {
2126 case 38: return 160;
2127 case 50: return 240;
2132 if (frame_bytes > 0) {
2133 /* calc from frame_bytes only */
2134 if (id == AV_CODEC_ID_TRUESPEECH)
2135 return 240 * (frame_bytes / 32);
2136 if (id == AV_CODEC_ID_NELLYMOSER)
2137 return 256 * (frame_bytes / 64);
2140 /* calc from frame_bytes and bits_per_coded_sample */
2141 if (id == AV_CODEC_ID_ADPCM_G726)
2142 return frame_bytes * 8 / bps;
2146 /* calc from frame_bytes and channels */
2148 case AV_CODEC_ID_ADPCM_4XM:
2149 case AV_CODEC_ID_ADPCM_IMA_ISS:
2150 return (frame_bytes - 4 * ch) * 2 / ch;
2151 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2152 return (frame_bytes - 4) * 2 / ch;
2153 case AV_CODEC_ID_ADPCM_IMA_AMV:
2154 return (frame_bytes - 8) * 2 / ch;
2155 case AV_CODEC_ID_ADPCM_XA:
2156 return (frame_bytes / 128) * 224 / ch;
2157 case AV_CODEC_ID_INTERPLAY_DPCM:
2158 return (frame_bytes - 6 - ch) / ch;
2159 case AV_CODEC_ID_ROQ_DPCM:
2160 return (frame_bytes - 8) / ch;
2161 case AV_CODEC_ID_XAN_DPCM:
2162 return (frame_bytes - 2 * ch) / ch;
2163 case AV_CODEC_ID_MACE3:
2164 return 3 * frame_bytes / ch;
2165 case AV_CODEC_ID_MACE6:
2166 return 6 * frame_bytes / ch;
2167 case AV_CODEC_ID_PCM_LXF:
2168 return 2 * (frame_bytes / (5 * ch));
2172 /* calc from frame_bytes, channels, and codec_tag */
2173 if (id == AV_CODEC_ID_SOL_DPCM) {
2175 return frame_bytes / ch;
2177 return frame_bytes * 2 / ch;
2182 /* calc from frame_bytes, channels, and block_align */
2183 int blocks = frame_bytes / ba;
2184 switch (avctx->codec_id) {
2185 case AV_CODEC_ID_ADPCM_IMA_WAV:
2186 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2187 case AV_CODEC_ID_ADPCM_IMA_DK3:
2188 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2189 case AV_CODEC_ID_ADPCM_IMA_DK4:
2190 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2191 case AV_CODEC_ID_ADPCM_MS:
2192 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2197 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2198 switch (avctx->codec_id) {
2199 case AV_CODEC_ID_PCM_DVD:
2200 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2201 case AV_CODEC_ID_PCM_BLURAY:
2202 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2203 case AV_CODEC_ID_S302M:
2204 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2214 int ff_thread_init(AVCodecContext *s)
2221 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2235 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2238 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2242 #if FF_API_MISSING_SAMPLE
2243 FF_DISABLE_DEPRECATION_WARNINGS
2244 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2246 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2247 "version to the newest one from Git. If the problem still "
2248 "occurs, it means that your file has a feature which has not "
2249 "been implemented.\n", feature);
2251 av_log_ask_for_sample(avc, NULL);
2254 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2256 va_list argument_list;
2258 va_start(argument_list, msg);
2261 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2262 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2263 "of this file to ftp://upload.libav.org/incoming/ "
2264 "and contact the libav-devel mailing list.\n");
2266 va_end(argument_list);
2268 FF_ENABLE_DEPRECATION_WARNINGS
2269 #endif /* FF_API_MISSING_SAMPLE */
2271 static AVHWAccel *first_hwaccel = NULL;
2273 void av_register_hwaccel(AVHWAccel *hwaccel)
2275 AVHWAccel **p = &first_hwaccel;
2279 hwaccel->next = NULL;
2282 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2284 return hwaccel ? hwaccel->next : first_hwaccel;
2287 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2290 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2292 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2299 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2301 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2307 int avpriv_lock_avformat(void)
2310 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2316 int avpriv_unlock_avformat(void)
2319 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2325 unsigned int avpriv_toupper4(unsigned int x)
2327 return av_toupper(x & 0xFF) +
2328 (av_toupper((x >> 8) & 0xFF) << 8) +
2329 (av_toupper((x >> 16) & 0xFF) << 16) +
2330 (av_toupper((x >> 24) & 0xFF) << 24);
2333 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2337 dst->owner = src->owner;
2339 ret = av_frame_ref(dst->f, src->f);
2343 if (src->progress &&
2344 !(dst->progress = av_buffer_ref(src->progress))) {
2345 ff_thread_release_buffer(dst->owner, dst);
2346 return AVERROR(ENOMEM);
2354 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2357 return ff_get_buffer(avctx, f->f, flags);
2360 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2363 av_frame_unref(f->f);
2366 void ff_thread_finish_setup(AVCodecContext *avctx)
2370 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2374 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2380 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2382 if (codec_id <= AV_CODEC_ID_NONE)
2383 return AVMEDIA_TYPE_UNKNOWN;
2384 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2385 return AVMEDIA_TYPE_VIDEO;
2386 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2387 return AVMEDIA_TYPE_AUDIO;
2388 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2389 return AVMEDIA_TYPE_SUBTITLE;
2391 return AVMEDIA_TYPE_UNKNOWN;
2394 int avcodec_is_open(AVCodecContext *s)
2396 return !!s->internal;
2399 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2401 uint32_t * restrict state)
2409 for (i = 0; i < 3; i++) {
2410 uint32_t tmp = *state << 8;
2411 *state = tmp + *(p++);
2412 if (tmp == 0x100 || p == end)
2417 if (p[-1] > 1 ) p += 3;
2418 else if (p[-2] ) p += 2;
2419 else if (p[-3]|(p[-1]-1)) p++;
2426 p = FFMIN(p, end) - 4;
2427 *state = AV_RB32(p);