3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/attributes.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/samplefmt.h"
40 #include "libavutil/dict.h"
43 #include "libavutil/opt.h"
46 #include "bytestream.h"
53 static int volatile entangled_thread_counter = 0;
54 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
55 static void *codec_mutex;
56 static void *avformat_mutex;
58 #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
59 FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
61 return av_fast_realloc(ptr, size, min_size);
64 FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
66 av_fast_malloc(ptr, size, min_size);
70 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
73 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
78 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
80 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
83 /* encoder management */
84 static AVCodec *first_avcodec = NULL;
86 AVCodec *av_codec_next(const AVCodec *c)
94 static av_cold void avcodec_init(void)
96 static int initialized = 0;
103 ff_dsputil_static_init();
106 int av_codec_is_encoder(const AVCodec *codec)
108 return codec && (codec->encode_sub || codec->encode2);
111 int av_codec_is_decoder(const AVCodec *codec)
113 return codec && codec->decode;
116 av_cold void avcodec_register(AVCodec *codec)
126 if (codec->init_static_data)
127 codec->init_static_data(codec);
131 unsigned avcodec_get_edge_width(void)
137 #if FF_API_SET_DIMENSIONS
138 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
140 ff_set_dimensions(s, width, height);
144 int ff_set_dimensions(AVCodecContext *s, int width, int height)
146 int ret = av_image_check_size(width, height, 0, s);
150 s->width = s->coded_width = width;
151 s->height = s->coded_height = height;
156 int ff_side_data_update_matrix_encoding(AVFrame *frame,
157 enum AVMatrixEncoding matrix_encoding)
159 AVFrameSideData *side_data;
160 enum AVMatrixEncoding *data;
162 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
164 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
165 sizeof(enum AVMatrixEncoding));
168 return AVERROR(ENOMEM);
170 data = (enum AVMatrixEncoding*)side_data->data;
171 *data = matrix_encoding;
176 #if HAVE_NEON || ARCH_PPC || HAVE_MMX
177 # define STRIDE_ALIGN 16
179 # define STRIDE_ALIGN 8
182 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
183 int linesize_align[AV_NUM_DATA_POINTERS])
189 switch (s->pix_fmt) {
190 case AV_PIX_FMT_YUV420P:
191 case AV_PIX_FMT_YUYV422:
192 case AV_PIX_FMT_YVYU422:
193 case AV_PIX_FMT_UYVY422:
194 case AV_PIX_FMT_YUV422P:
195 case AV_PIX_FMT_YUV440P:
196 case AV_PIX_FMT_YUV444P:
197 case AV_PIX_FMT_GBRP:
198 case AV_PIX_FMT_GRAY8:
199 case AV_PIX_FMT_GRAY16BE:
200 case AV_PIX_FMT_GRAY16LE:
201 case AV_PIX_FMT_YUVJ420P:
202 case AV_PIX_FMT_YUVJ422P:
203 case AV_PIX_FMT_YUVJ440P:
204 case AV_PIX_FMT_YUVJ444P:
205 case AV_PIX_FMT_YUVA420P:
206 case AV_PIX_FMT_YUVA422P:
207 case AV_PIX_FMT_YUVA444P:
208 case AV_PIX_FMT_YUV420P9LE:
209 case AV_PIX_FMT_YUV420P9BE:
210 case AV_PIX_FMT_YUV420P10LE:
211 case AV_PIX_FMT_YUV420P10BE:
212 case AV_PIX_FMT_YUV422P9LE:
213 case AV_PIX_FMT_YUV422P9BE:
214 case AV_PIX_FMT_YUV422P10LE:
215 case AV_PIX_FMT_YUV422P10BE:
216 case AV_PIX_FMT_YUVA422P10LE:
217 case AV_PIX_FMT_YUVA422P10BE:
218 case AV_PIX_FMT_YUV444P9LE:
219 case AV_PIX_FMT_YUV444P9BE:
220 case AV_PIX_FMT_YUV444P10LE:
221 case AV_PIX_FMT_YUV444P10BE:
222 case AV_PIX_FMT_YUVA444P10LE:
223 case AV_PIX_FMT_YUVA444P10BE:
224 case AV_PIX_FMT_GBRP9LE:
225 case AV_PIX_FMT_GBRP9BE:
226 case AV_PIX_FMT_GBRP10LE:
227 case AV_PIX_FMT_GBRP10BE:
228 w_align = 16; //FIXME assume 16 pixel per macroblock
229 h_align = 16 * 2; // interlaced needs 2 macroblocks height
231 case AV_PIX_FMT_YUV411P:
232 case AV_PIX_FMT_UYYVYY411:
236 case AV_PIX_FMT_YUV410P:
237 if (s->codec_id == AV_CODEC_ID_SVQ1) {
241 case AV_PIX_FMT_RGB555:
242 if (s->codec_id == AV_CODEC_ID_RPZA) {
246 case AV_PIX_FMT_PAL8:
247 case AV_PIX_FMT_BGR8:
248 case AV_PIX_FMT_RGB8:
249 if (s->codec_id == AV_CODEC_ID_SMC) {
254 case AV_PIX_FMT_BGR24:
255 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
256 (s->codec_id == AV_CODEC_ID_ZLIB)) {
267 *width = FFALIGN(*width, w_align);
268 *height = FFALIGN(*height, h_align);
269 if (s->codec_id == AV_CODEC_ID_H264)
270 // some of the optimized chroma MC reads one line too much
273 for (i = 0; i < 4; i++)
274 linesize_align[i] = STRIDE_ALIGN;
277 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
279 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
280 int chroma_shift = desc->log2_chroma_w;
281 int linesize_align[AV_NUM_DATA_POINTERS];
284 avcodec_align_dimensions2(s, width, height, linesize_align);
285 align = FFMAX(linesize_align[0], linesize_align[3]);
286 linesize_align[1] <<= chroma_shift;
287 linesize_align[2] <<= chroma_shift;
288 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
289 *width = FFALIGN(*width, align);
292 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
293 enum AVSampleFormat sample_fmt, const uint8_t *buf,
294 int buf_size, int align)
296 int ch, planar, needed_size, ret = 0;
298 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
299 frame->nb_samples, sample_fmt,
301 if (buf_size < needed_size)
302 return AVERROR(EINVAL);
304 planar = av_sample_fmt_is_planar(sample_fmt);
305 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
306 if (!(frame->extended_data = av_mallocz(nb_channels *
307 sizeof(*frame->extended_data))))
308 return AVERROR(ENOMEM);
310 frame->extended_data = frame->data;
313 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
314 buf, nb_channels, frame->nb_samples,
315 sample_fmt, align)) < 0) {
316 if (frame->extended_data != frame->data)
317 av_free(frame->extended_data);
320 if (frame->extended_data != frame->data) {
321 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
322 frame->data[ch] = frame->extended_data[ch];
328 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
330 FramePool *pool = avctx->internal->pool;
333 switch (avctx->codec_type) {
334 case AVMEDIA_TYPE_VIDEO: {
337 int w = frame->width;
338 int h = frame->height;
339 int tmpsize, unaligned;
341 if (pool->format == frame->format &&
342 pool->width == frame->width && pool->height == frame->height)
345 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
347 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
353 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
354 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
355 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
356 // increase alignment of w for next try (rhs gives the lowest bit set in w)
360 for (i = 0; i < 4; i++)
361 unaligned |= picture.linesize[i] % pool->stride_align[i];
364 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
365 NULL, picture.linesize);
369 for (i = 0; i < 3 && picture.data[i + 1]; i++)
370 size[i] = picture.data[i + 1] - picture.data[i];
371 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
373 for (i = 0; i < 4; i++) {
374 av_buffer_pool_uninit(&pool->pools[i]);
375 pool->linesize[i] = picture.linesize[i];
377 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
378 if (!pool->pools[i]) {
379 ret = AVERROR(ENOMEM);
384 pool->format = frame->format;
385 pool->width = frame->width;
386 pool->height = frame->height;
390 case AVMEDIA_TYPE_AUDIO: {
391 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
392 int planar = av_sample_fmt_is_planar(frame->format);
393 int planes = planar ? ch : 1;
395 if (pool->format == frame->format && pool->planes == planes &&
396 pool->channels == ch && frame->nb_samples == pool->samples)
399 av_buffer_pool_uninit(&pool->pools[0]);
400 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
401 frame->nb_samples, frame->format, 0);
405 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
406 if (!pool->pools[0]) {
407 ret = AVERROR(ENOMEM);
411 pool->format = frame->format;
412 pool->planes = planes;
414 pool->samples = frame->nb_samples;
417 default: av_assert0(0);
421 for (i = 0; i < 4; i++)
422 av_buffer_pool_uninit(&pool->pools[i]);
424 pool->planes = pool->channels = pool->samples = 0;
425 pool->width = pool->height = 0;
429 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
431 FramePool *pool = avctx->internal->pool;
432 int planes = pool->planes;
435 frame->linesize[0] = pool->linesize[0];
437 if (planes > AV_NUM_DATA_POINTERS) {
438 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
439 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
440 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
441 sizeof(*frame->extended_buf));
442 if (!frame->extended_data || !frame->extended_buf) {
443 av_freep(&frame->extended_data);
444 av_freep(&frame->extended_buf);
445 return AVERROR(ENOMEM);
448 frame->extended_data = frame->data;
450 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
451 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
454 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
456 for (i = 0; i < frame->nb_extended_buf; i++) {
457 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
458 if (!frame->extended_buf[i])
460 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
463 if (avctx->debug & FF_DEBUG_BUFFERS)
464 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
468 av_frame_unref(frame);
469 return AVERROR(ENOMEM);
472 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
474 FramePool *pool = s->internal->pool;
475 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
476 int pixel_size = desc->comp[0].step_minus1 + 1;
477 int h_chroma_shift, v_chroma_shift;
480 if (pic->data[0] != NULL) {
481 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
485 memset(pic->data, 0, sizeof(pic->data));
486 pic->extended_data = pic->data;
488 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
490 for (i = 0; i < 4 && pool->pools[i]; i++) {
491 const int h_shift = i == 0 ? 0 : h_chroma_shift;
492 const int v_shift = i == 0 ? 0 : v_chroma_shift;
494 pic->linesize[i] = pool->linesize[i];
496 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
500 // no edge if EDGE EMU or not planar YUV
501 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
502 pic->data[i] = pic->buf[i]->data;
504 pic->data[i] = pic->buf[i]->data +
505 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
506 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
509 for (; i < AV_NUM_DATA_POINTERS; i++) {
511 pic->linesize[i] = 0;
513 if (pic->data[1] && !pic->data[2])
514 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
516 if (s->debug & FF_DEBUG_BUFFERS)
517 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
522 return AVERROR(ENOMEM);
525 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
529 if ((ret = update_frame_pool(avctx, frame)) < 0)
532 #if FF_API_GET_BUFFER
533 FF_DISABLE_DEPRECATION_WARNINGS
534 frame->type = FF_BUFFER_TYPE_INTERNAL;
535 FF_ENABLE_DEPRECATION_WARNINGS
538 switch (avctx->codec_type) {
539 case AVMEDIA_TYPE_VIDEO:
540 return video_get_buffer(avctx, frame);
541 case AVMEDIA_TYPE_AUDIO:
542 return audio_get_buffer(avctx, frame);
548 #if FF_API_GET_BUFFER
549 FF_DISABLE_DEPRECATION_WARNINGS
550 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
552 return avcodec_default_get_buffer2(avctx, frame, 0);
555 typedef struct CompatReleaseBufPriv {
556 AVCodecContext avctx;
558 } CompatReleaseBufPriv;
560 static void compat_free_buffer(void *opaque, uint8_t *data)
562 CompatReleaseBufPriv *priv = opaque;
563 if (priv->avctx.release_buffer)
564 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
568 static void compat_release_buffer(void *opaque, uint8_t *data)
570 AVBufferRef *buf = opaque;
571 av_buffer_unref(&buf);
573 FF_ENABLE_DEPRECATION_WARNINGS
576 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
578 AVPacket *pkt = avctx->internal->pkt;
581 AVFrameSideData *frame_sd;
584 frame->reordered_opaque = avctx->reordered_opaque;
586 frame->pkt_pts = AV_NOPTS_VALUE;
590 frame->pkt_pts = pkt->pts;
592 /* copy the replaygain data to the output frame */
593 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
595 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
597 return AVERROR(ENOMEM);
599 memcpy(frame_sd->data, packet_sd, size);
605 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
607 int override_dimensions = 1;
610 switch (avctx->codec_type) {
611 case AVMEDIA_TYPE_VIDEO:
612 if (frame->width <= 0 || frame->height <= 0) {
613 frame->width = FFMAX(avctx->width, avctx->coded_width);
614 frame->height = FFMAX(avctx->height, avctx->coded_height);
615 override_dimensions = 0;
617 if (frame->format < 0)
618 frame->format = avctx->pix_fmt;
619 if (!frame->sample_aspect_ratio.num)
620 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
622 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
625 case AVMEDIA_TYPE_AUDIO:
626 if (!frame->sample_rate)
627 frame->sample_rate = avctx->sample_rate;
628 if (frame->format < 0)
629 frame->format = avctx->sample_fmt;
630 if (!frame->channel_layout) {
631 if (avctx->channel_layout) {
632 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
634 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
636 return AVERROR(EINVAL);
639 frame->channel_layout = avctx->channel_layout;
641 if (avctx->channels > FF_SANE_NB_CHANNELS) {
642 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
644 return AVERROR(ENOSYS);
647 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
648 if (!frame->channel_layout)
649 frame->channel_layout = (1ULL << avctx->channels) - 1;
653 default: return AVERROR(EINVAL);
656 ret = ff_decode_frame_props(avctx, frame);
660 #if FF_API_GET_BUFFER
661 FF_DISABLE_DEPRECATION_WARNINGS
663 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
664 * We wrap each plane in its own AVBuffer. Each of those has a reference to
665 * a dummy AVBuffer as its private data, unreffing it on free.
666 * When all the planes are freed, the dummy buffer's free callback calls
669 if (avctx->get_buffer) {
670 CompatReleaseBufPriv *priv = NULL;
671 AVBufferRef *dummy_buf = NULL;
674 if (flags & AV_GET_BUFFER_FLAG_REF)
675 frame->reference = 1;
677 ret = avctx->get_buffer(avctx, frame);
681 /* return if the buffers are already set up
682 * this would happen e.g. when a custom get_buffer() calls
683 * avcodec_default_get_buffer
688 priv = av_mallocz(sizeof(*priv));
690 ret = AVERROR(ENOMEM);
693 priv->avctx = *avctx;
694 priv->frame = *frame;
696 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
698 ret = AVERROR(ENOMEM);
702 #define WRAP_PLANE(ref_out, data, data_size) \
704 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
706 ret = AVERROR(ENOMEM); \
709 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
712 av_frame_unref(frame); \
713 ret = AVERROR(ENOMEM); \
718 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
719 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
721 planes = av_pix_fmt_count_planes(frame->format);
722 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
723 check for allocated buffers: make libavcodec happy */
724 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
726 if (!desc || planes <= 0) {
727 ret = AVERROR(EINVAL);
731 for (i = 0; i < planes; i++) {
732 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
733 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
735 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
738 int planar = av_sample_fmt_is_planar(frame->format);
739 planes = planar ? avctx->channels : 1;
741 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
742 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
743 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
744 frame->nb_extended_buf);
745 if (!frame->extended_buf) {
746 ret = AVERROR(ENOMEM);
751 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
752 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
754 for (i = 0; i < frame->nb_extended_buf; i++)
755 WRAP_PLANE(frame->extended_buf[i],
756 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
760 av_buffer_unref(&dummy_buf);
762 frame->width = avctx->width;
763 frame->height = avctx->height;
768 avctx->release_buffer(avctx, frame);
770 av_buffer_unref(&dummy_buf);
773 FF_ENABLE_DEPRECATION_WARNINGS
776 ret = avctx->get_buffer2(avctx, frame, flags);
778 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
779 frame->width = avctx->width;
780 frame->height = avctx->height;
786 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
791 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
794 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
796 if (av_frame_is_writable(frame))
797 return ff_decode_frame_props(avctx, frame);
799 tmp = av_frame_alloc();
801 return AVERROR(ENOMEM);
803 av_frame_move_ref(tmp, frame);
805 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
811 av_frame_copy(frame, tmp);
817 #if FF_API_GET_BUFFER
818 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
823 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
830 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
834 for (i = 0; i < count; i++) {
835 int r = func(c, (char *)arg + i * size);
842 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
846 for (i = 0; i < count; i++) {
847 int r = func(c, arg, i, 0);
854 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
856 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
857 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
860 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
862 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
867 #if FF_API_AVFRAME_LAVC
868 void avcodec_get_frame_defaults(AVFrame *frame)
870 if (frame->extended_data != frame->data)
871 av_freep(&frame->extended_data);
873 memset(frame, 0, sizeof(AVFrame));
875 frame->pts = AV_NOPTS_VALUE;
876 frame->key_frame = 1;
877 frame->sample_aspect_ratio = (AVRational) {0, 1 };
878 frame->format = -1; /* unknown */
879 frame->extended_data = frame->data;
882 AVFrame *avcodec_alloc_frame(void)
884 AVFrame *frame = av_mallocz(sizeof(AVFrame));
889 FF_DISABLE_DEPRECATION_WARNINGS
890 avcodec_get_frame_defaults(frame);
891 FF_ENABLE_DEPRECATION_WARNINGS
896 void avcodec_free_frame(AVFrame **frame)
898 av_frame_free(frame);
902 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
905 AVDictionary *tmp = NULL;
907 if (avcodec_is_open(avctx))
910 if ((!codec && !avctx->codec)) {
911 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
912 return AVERROR(EINVAL);
914 if ((codec && avctx->codec && codec != avctx->codec)) {
915 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
916 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
917 return AVERROR(EINVAL);
920 codec = avctx->codec;
922 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
923 return AVERROR(EINVAL);
926 av_dict_copy(&tmp, *options, 0);
928 /* If there is a user-supplied mutex locking routine, call it. */
930 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
934 entangled_thread_counter++;
935 if (entangled_thread_counter != 1) {
936 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
941 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
942 if (!avctx->internal) {
943 ret = AVERROR(ENOMEM);
947 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
948 if (!avctx->internal->pool) {
949 ret = AVERROR(ENOMEM);
953 avctx->internal->to_free = av_frame_alloc();
954 if (!avctx->internal->to_free) {
955 ret = AVERROR(ENOMEM);
959 if (codec->priv_data_size > 0) {
960 if (!avctx->priv_data) {
961 avctx->priv_data = av_mallocz(codec->priv_data_size);
962 if (!avctx->priv_data) {
963 ret = AVERROR(ENOMEM);
966 if (codec->priv_class) {
967 *(const AVClass **)avctx->priv_data = codec->priv_class;
968 av_opt_set_defaults(avctx->priv_data);
971 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
974 avctx->priv_data = NULL;
976 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
979 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
980 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
981 else if (avctx->width && avctx->height)
982 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
986 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
987 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
988 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
989 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
990 ff_set_dimensions(avctx, 0, 0);
993 /* if the decoder init function was already called previously,
994 * free the already allocated subtitle_header before overwriting it */
995 if (av_codec_is_decoder(codec))
996 av_freep(&avctx->subtitle_header);
998 if (avctx->channels > FF_SANE_NB_CHANNELS) {
999 ret = AVERROR(EINVAL);
1003 avctx->codec = codec;
1004 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1005 avctx->codec_id == AV_CODEC_ID_NONE) {
1006 avctx->codec_type = codec->type;
1007 avctx->codec_id = codec->id;
1009 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1010 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1011 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
1012 ret = AVERROR(EINVAL);
1015 avctx->frame_number = 0;
1017 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1018 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1019 ret = AVERROR_EXPERIMENTAL;
1023 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1024 (!avctx->time_base.num || !avctx->time_base.den)) {
1025 avctx->time_base.num = 1;
1026 avctx->time_base.den = avctx->sample_rate;
1030 ret = ff_thread_init(avctx);
1035 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1036 avctx->thread_count = 1;
1038 if (av_codec_is_encoder(avctx->codec)) {
1040 if (avctx->codec->sample_fmts) {
1041 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1042 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1044 if (avctx->channels == 1 &&
1045 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1046 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1047 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1051 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1052 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1053 ret = AVERROR(EINVAL);
1057 if (avctx->codec->pix_fmts) {
1058 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1059 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1061 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1062 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1063 ret = AVERROR(EINVAL);
1067 if (avctx->codec->supported_samplerates) {
1068 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1069 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1071 if (avctx->codec->supported_samplerates[i] == 0) {
1072 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1073 ret = AVERROR(EINVAL);
1077 if (avctx->codec->channel_layouts) {
1078 if (!avctx->channel_layout) {
1079 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1081 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1082 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1084 if (avctx->codec->channel_layouts[i] == 0) {
1085 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1086 ret = AVERROR(EINVAL);
1091 if (avctx->channel_layout && avctx->channels) {
1092 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1093 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1094 ret = AVERROR(EINVAL);
1097 } else if (avctx->channel_layout) {
1098 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1101 if (!avctx->rc_initial_buffer_occupancy)
1102 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1105 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1106 ret = avctx->codec->init(avctx);
1112 if (av_codec_is_decoder(avctx->codec)) {
1113 /* validate channel layout from the decoder */
1114 if (avctx->channel_layout) {
1115 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1116 if (!avctx->channels)
1117 avctx->channels = channels;
1118 else if (channels != avctx->channels) {
1119 av_log(avctx, AV_LOG_WARNING,
1120 "channel layout does not match number of channels\n");
1121 avctx->channel_layout = 0;
1124 if (avctx->channels && avctx->channels < 0 ||
1125 avctx->channels > FF_SANE_NB_CHANNELS) {
1126 ret = AVERROR(EINVAL);
1131 entangled_thread_counter--;
1133 /* Release any user-supplied mutex. */
1135 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1138 av_dict_free(options);
1145 av_freep(&avctx->priv_data);
1146 if (avctx->internal) {
1147 av_frame_free(&avctx->internal->to_free);
1148 av_freep(&avctx->internal->pool);
1150 av_freep(&avctx->internal);
1151 avctx->codec = NULL;
1155 int ff_alloc_packet(AVPacket *avpkt, int size)
1157 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1158 return AVERROR(EINVAL);
1161 AVBufferRef *buf = avpkt->buf;
1162 #if FF_API_DESTRUCT_PACKET
1163 FF_DISABLE_DEPRECATION_WARNINGS
1164 void *destruct = avpkt->destruct;
1165 FF_ENABLE_DEPRECATION_WARNINGS
1168 if (avpkt->size < size)
1169 return AVERROR(EINVAL);
1171 av_init_packet(avpkt);
1172 #if FF_API_DESTRUCT_PACKET
1173 FF_DISABLE_DEPRECATION_WARNINGS
1174 avpkt->destruct = destruct;
1175 FF_ENABLE_DEPRECATION_WARNINGS
1181 return av_new_packet(avpkt, size);
1186 * Pad last frame with silence.
1188 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1190 AVFrame *frame = NULL;
1193 if (!(frame = av_frame_alloc()))
1194 return AVERROR(ENOMEM);
1196 frame->format = src->format;
1197 frame->channel_layout = src->channel_layout;
1198 frame->nb_samples = s->frame_size;
1199 ret = av_frame_get_buffer(frame, 32);
1203 ret = av_frame_copy_props(frame, src);
1207 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1208 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1210 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1211 frame->nb_samples - src->nb_samples,
1212 s->channels, s->sample_fmt)) < 0)
1220 av_frame_free(&frame);
1224 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1226 const AVFrame *frame,
1227 int *got_packet_ptr)
1230 AVFrame *padded_frame = NULL;
1232 int user_packet = !!avpkt->data;
1234 *got_packet_ptr = 0;
1236 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1237 av_free_packet(avpkt);
1238 av_init_packet(avpkt);
1242 /* ensure that extended_data is properly set */
1243 if (frame && !frame->extended_data) {
1244 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1245 avctx->channels > AV_NUM_DATA_POINTERS) {
1246 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1247 "with more than %d channels, but extended_data is not set.\n",
1248 AV_NUM_DATA_POINTERS);
1249 return AVERROR(EINVAL);
1251 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1254 tmp.extended_data = tmp.data;
1258 /* check for valid frame size */
1260 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1261 if (frame->nb_samples > avctx->frame_size)
1262 return AVERROR(EINVAL);
1263 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1264 if (frame->nb_samples < avctx->frame_size &&
1265 !avctx->internal->last_audio_frame) {
1266 ret = pad_last_frame(avctx, &padded_frame, frame);
1270 frame = padded_frame;
1271 avctx->internal->last_audio_frame = 1;
1274 if (frame->nb_samples != avctx->frame_size) {
1275 ret = AVERROR(EINVAL);
1281 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1283 if (*got_packet_ptr) {
1284 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1285 if (avpkt->pts == AV_NOPTS_VALUE)
1286 avpkt->pts = frame->pts;
1287 if (!avpkt->duration)
1288 avpkt->duration = ff_samples_to_time_base(avctx,
1291 avpkt->dts = avpkt->pts;
1296 if (!user_packet && avpkt->size) {
1297 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1299 avpkt->data = avpkt->buf->data;
1302 avctx->frame_number++;
1305 if (ret < 0 || !*got_packet_ptr) {
1306 av_free_packet(avpkt);
1307 av_init_packet(avpkt);
1311 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1312 * this needs to be moved to the encoders, but for now we can do it
1313 * here to simplify things */
1314 avpkt->flags |= AV_PKT_FLAG_KEY;
1317 av_frame_free(&padded_frame);
1322 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1324 const AVFrame *frame,
1325 int *got_packet_ptr)
1328 int user_packet = !!avpkt->data;
1330 *got_packet_ptr = 0;
1332 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1333 av_free_packet(avpkt);
1334 av_init_packet(avpkt);
1339 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1340 return AVERROR(EINVAL);
1342 av_assert0(avctx->codec->encode2);
1344 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1346 if (!*got_packet_ptr)
1348 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1349 avpkt->pts = avpkt->dts = frame->pts;
1351 if (!user_packet && avpkt->size) {
1352 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1354 avpkt->data = avpkt->buf->data;
1357 avctx->frame_number++;
1360 if (ret < 0 || !*got_packet_ptr)
1361 av_free_packet(avpkt);
1367 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1368 const AVSubtitle *sub)
1371 if (sub->start_display_time) {
1372 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1375 if (sub->num_rects == 0 || !sub->rects)
1377 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1378 avctx->frame_number++;
1382 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1385 const uint8_t *data;
1388 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1392 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1393 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1394 "changes, but PARAM_CHANGE side data was sent to it.\n");
1395 return AVERROR(EINVAL);
1401 flags = bytestream_get_le32(&data);
1404 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1407 avctx->channels = bytestream_get_le32(&data);
1410 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1413 avctx->channel_layout = bytestream_get_le64(&data);
1416 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1419 avctx->sample_rate = bytestream_get_le32(&data);
1422 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1425 avctx->width = bytestream_get_le32(&data);
1426 avctx->height = bytestream_get_le32(&data);
1428 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1435 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1436 return AVERROR_INVALIDDATA;
1439 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1443 /* move the original frame to our backup */
1444 av_frame_unref(avci->to_free);
1445 av_frame_move_ref(avci->to_free, frame);
1447 /* now copy everything except the AVBufferRefs back
1448 * note that we make a COPY of the side data, so calling av_frame_free() on
1449 * the caller's frame will work properly */
1450 ret = av_frame_copy_props(frame, avci->to_free);
1454 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1455 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1456 if (avci->to_free->extended_data != avci->to_free->data) {
1457 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1458 int size = planes * sizeof(*frame->extended_data);
1461 av_frame_unref(frame);
1465 frame->extended_data = av_malloc(size);
1466 if (!frame->extended_data) {
1467 av_frame_unref(frame);
1468 return AVERROR(ENOMEM);
1470 memcpy(frame->extended_data, avci->to_free->extended_data,
1473 frame->extended_data = frame->data;
1475 frame->format = avci->to_free->format;
1476 frame->width = avci->to_free->width;
1477 frame->height = avci->to_free->height;
1478 frame->channel_layout = avci->to_free->channel_layout;
1479 frame->nb_samples = avci->to_free->nb_samples;
1484 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1485 int *got_picture_ptr,
1488 AVCodecInternal *avci = avctx->internal;
1491 *got_picture_ptr = 0;
1492 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1495 avctx->internal->pkt = avpkt;
1496 ret = apply_param_change(avctx, avpkt);
1498 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1499 if (avctx->err_recognition & AV_EF_EXPLODE)
1503 av_frame_unref(picture);
1505 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1506 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1507 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1510 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1512 picture->pkt_dts = avpkt->dts;
1513 /* get_buffer is supposed to set frame parameters */
1514 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1515 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1516 picture->width = avctx->width;
1517 picture->height = avctx->height;
1518 picture->format = avctx->pix_fmt;
1522 emms_c(); //needed to avoid an emms_c() call before every return;
1524 if (*got_picture_ptr) {
1525 if (!avctx->refcounted_frames) {
1526 int err = unrefcount_frame(avci, picture);
1531 avctx->frame_number++;
1533 av_frame_unref(picture);
1540 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1545 AVCodecInternal *avci = avctx->internal;
1550 avctx->internal->pkt = avpkt;
1552 if (!avpkt->data && avpkt->size) {
1553 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1554 return AVERROR(EINVAL);
1557 ret = apply_param_change(avctx, avpkt);
1559 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1560 if (avctx->err_recognition & AV_EF_EXPLODE)
1564 av_frame_unref(frame);
1566 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1567 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1568 if (ret >= 0 && *got_frame_ptr) {
1569 avctx->frame_number++;
1570 frame->pkt_dts = avpkt->dts;
1571 if (frame->format == AV_SAMPLE_FMT_NONE)
1572 frame->format = avctx->sample_fmt;
1574 if (!avctx->refcounted_frames) {
1575 int err = unrefcount_frame(avci, frame);
1580 av_frame_unref(frame);
1587 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1593 avctx->internal->pkt = avpkt;
1595 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1597 avctx->frame_number++;
1601 void avsubtitle_free(AVSubtitle *sub)
1605 for (i = 0; i < sub->num_rects; i++) {
1606 av_freep(&sub->rects[i]->pict.data[0]);
1607 av_freep(&sub->rects[i]->pict.data[1]);
1608 av_freep(&sub->rects[i]->pict.data[2]);
1609 av_freep(&sub->rects[i]->pict.data[3]);
1610 av_freep(&sub->rects[i]->text);
1611 av_freep(&sub->rects[i]->ass);
1612 av_freep(&sub->rects[i]);
1615 av_freep(&sub->rects);
1617 memset(sub, 0, sizeof(AVSubtitle));
1620 av_cold int avcodec_close(AVCodecContext *avctx)
1622 /* If there is a user-supplied mutex locking routine, call it. */
1624 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1628 entangled_thread_counter++;
1629 if (entangled_thread_counter != 1) {
1630 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1631 entangled_thread_counter--;
1635 if (avcodec_is_open(avctx)) {
1636 FramePool *pool = avctx->internal->pool;
1638 if (HAVE_THREADS && avctx->internal->thread_ctx)
1639 ff_thread_free(avctx);
1640 if (avctx->codec && avctx->codec->close)
1641 avctx->codec->close(avctx);
1642 avctx->coded_frame = NULL;
1643 av_frame_free(&avctx->internal->to_free);
1644 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1645 av_buffer_pool_uninit(&pool->pools[i]);
1646 av_freep(&avctx->internal->pool);
1647 av_freep(&avctx->internal);
1650 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1651 av_opt_free(avctx->priv_data);
1653 av_freep(&avctx->priv_data);
1654 if (av_codec_is_encoder(avctx->codec))
1655 av_freep(&avctx->extradata);
1656 avctx->codec = NULL;
1657 avctx->active_thread_type = 0;
1658 entangled_thread_counter--;
1660 /* Release any user-supplied mutex. */
1662 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1667 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1669 AVCodec *p, *experimental = NULL;
1672 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1674 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1681 return experimental;
1684 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1686 return find_encdec(id, 1);
1689 AVCodec *avcodec_find_encoder_by_name(const char *name)
1696 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1703 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1705 return find_encdec(id, 0);
1708 AVCodec *avcodec_find_decoder_by_name(const char *name)
1715 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1722 static int get_bit_rate(AVCodecContext *ctx)
1725 int bits_per_sample;
1727 switch (ctx->codec_type) {
1728 case AVMEDIA_TYPE_VIDEO:
1729 case AVMEDIA_TYPE_DATA:
1730 case AVMEDIA_TYPE_SUBTITLE:
1731 case AVMEDIA_TYPE_ATTACHMENT:
1732 bit_rate = ctx->bit_rate;
1734 case AVMEDIA_TYPE_AUDIO:
1735 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1736 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1745 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1747 int i, len, ret = 0;
1749 #define TAG_PRINT(x) \
1750 (((x) >= '0' && (x) <= '9') || \
1751 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1752 ((x) == '.' || (x) == ' '))
1754 for (i = 0; i < 4; i++) {
1755 len = snprintf(buf, buf_size,
1756 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1758 buf_size = buf_size > len ? buf_size - len : 0;
1765 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1767 const char *codec_name;
1768 const char *profile = NULL;
1772 AVRational display_aspect_ratio;
1777 p = avcodec_find_encoder(enc->codec_id);
1779 p = avcodec_find_decoder(enc->codec_id);
1782 codec_name = p->name;
1783 profile = av_get_profile_name(p, enc->profile);
1784 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1785 /* fake mpeg2 transport stream codec (currently not
1787 codec_name = "mpeg2ts";
1789 /* output avi tags */
1791 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1792 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1796 switch (enc->codec_type) {
1797 case AVMEDIA_TYPE_VIDEO:
1798 snprintf(buf, buf_size,
1800 codec_name, enc->mb_decision ? " (hq)" : "");
1802 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1804 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1805 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1807 av_get_pix_fmt_name(enc->pix_fmt));
1810 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1812 enc->width, enc->height);
1813 if (enc->sample_aspect_ratio.num) {
1814 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1815 enc->width * enc->sample_aspect_ratio.num,
1816 enc->height * enc->sample_aspect_ratio.den,
1818 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1819 " [PAR %d:%d DAR %d:%d]",
1820 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1821 display_aspect_ratio.num, display_aspect_ratio.den);
1823 if (av_log_get_level() >= AV_LOG_DEBUG) {
1824 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1825 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1827 enc->time_base.num / g, enc->time_base.den / g);
1831 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1832 ", q=%d-%d", enc->qmin, enc->qmax);
1835 case AVMEDIA_TYPE_AUDIO:
1836 snprintf(buf, buf_size,
1840 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1842 if (enc->sample_rate) {
1843 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1844 ", %d Hz", enc->sample_rate);
1846 av_strlcat(buf, ", ", buf_size);
1847 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1848 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1849 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1850 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1853 case AVMEDIA_TYPE_DATA:
1854 snprintf(buf, buf_size, "Data: %s", codec_name);
1856 case AVMEDIA_TYPE_SUBTITLE:
1857 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1859 case AVMEDIA_TYPE_ATTACHMENT:
1860 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1863 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1867 if (enc->flags & CODEC_FLAG_PASS1)
1868 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1870 if (enc->flags & CODEC_FLAG_PASS2)
1871 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1874 bitrate = get_bit_rate(enc);
1876 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1877 ", %d kb/s", bitrate / 1000);
1881 const char *av_get_profile_name(const AVCodec *codec, int profile)
1884 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1887 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1888 if (p->profile == profile)
1894 unsigned avcodec_version(void)
1896 return LIBAVCODEC_VERSION_INT;
1899 const char *avcodec_configuration(void)
1901 return LIBAV_CONFIGURATION;
1904 const char *avcodec_license(void)
1906 #define LICENSE_PREFIX "libavcodec license: "
1907 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1910 void avcodec_flush_buffers(AVCodecContext *avctx)
1912 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1913 ff_thread_flush(avctx);
1914 else if (avctx->codec->flush)
1915 avctx->codec->flush(avctx);
1917 if (!avctx->refcounted_frames)
1918 av_frame_unref(avctx->internal->to_free);
1921 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1924 case AV_CODEC_ID_ADPCM_CT:
1925 case AV_CODEC_ID_ADPCM_IMA_APC:
1926 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1927 case AV_CODEC_ID_ADPCM_IMA_WS:
1928 case AV_CODEC_ID_ADPCM_G722:
1929 case AV_CODEC_ID_ADPCM_YAMAHA:
1931 case AV_CODEC_ID_PCM_ALAW:
1932 case AV_CODEC_ID_PCM_MULAW:
1933 case AV_CODEC_ID_PCM_S8:
1934 case AV_CODEC_ID_PCM_U8:
1935 case AV_CODEC_ID_PCM_ZORK:
1937 case AV_CODEC_ID_PCM_S16BE:
1938 case AV_CODEC_ID_PCM_S16LE:
1939 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1940 case AV_CODEC_ID_PCM_U16BE:
1941 case AV_CODEC_ID_PCM_U16LE:
1943 case AV_CODEC_ID_PCM_S24DAUD:
1944 case AV_CODEC_ID_PCM_S24BE:
1945 case AV_CODEC_ID_PCM_S24LE:
1946 case AV_CODEC_ID_PCM_S24LE_PLANAR:
1947 case AV_CODEC_ID_PCM_U24BE:
1948 case AV_CODEC_ID_PCM_U24LE:
1950 case AV_CODEC_ID_PCM_S32BE:
1951 case AV_CODEC_ID_PCM_S32LE:
1952 case AV_CODEC_ID_PCM_S32LE_PLANAR:
1953 case AV_CODEC_ID_PCM_U32BE:
1954 case AV_CODEC_ID_PCM_U32LE:
1955 case AV_CODEC_ID_PCM_F32BE:
1956 case AV_CODEC_ID_PCM_F32LE:
1958 case AV_CODEC_ID_PCM_F64BE:
1959 case AV_CODEC_ID_PCM_F64LE:
1966 int av_get_bits_per_sample(enum AVCodecID codec_id)
1969 case AV_CODEC_ID_ADPCM_SBPRO_2:
1971 case AV_CODEC_ID_ADPCM_SBPRO_3:
1973 case AV_CODEC_ID_ADPCM_SBPRO_4:
1974 case AV_CODEC_ID_ADPCM_IMA_WAV:
1975 case AV_CODEC_ID_ADPCM_IMA_QT:
1976 case AV_CODEC_ID_ADPCM_SWF:
1977 case AV_CODEC_ID_ADPCM_MS:
1980 return av_get_exact_bits_per_sample(codec_id);
1984 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1986 int id, sr, ch, ba, tag, bps;
1988 id = avctx->codec_id;
1989 sr = avctx->sample_rate;
1990 ch = avctx->channels;
1991 ba = avctx->block_align;
1992 tag = avctx->codec_tag;
1993 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1995 /* codecs with an exact constant bits per sample */
1996 if (bps > 0 && ch > 0 && frame_bytes > 0)
1997 return (frame_bytes * 8) / (bps * ch);
1998 bps = avctx->bits_per_coded_sample;
2000 /* codecs with a fixed packet duration */
2002 case AV_CODEC_ID_ADPCM_ADX: return 32;
2003 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2004 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2005 case AV_CODEC_ID_AMR_NB:
2006 case AV_CODEC_ID_GSM:
2007 case AV_CODEC_ID_QCELP:
2008 case AV_CODEC_ID_RA_144:
2009 case AV_CODEC_ID_RA_288: return 160;
2010 case AV_CODEC_ID_IMC: return 256;
2011 case AV_CODEC_ID_AMR_WB:
2012 case AV_CODEC_ID_GSM_MS: return 320;
2013 case AV_CODEC_ID_MP1: return 384;
2014 case AV_CODEC_ID_ATRAC1: return 512;
2015 case AV_CODEC_ID_ATRAC3: return 1024;
2016 case AV_CODEC_ID_MP2:
2017 case AV_CODEC_ID_MUSEPACK7: return 1152;
2018 case AV_CODEC_ID_AC3: return 1536;
2022 /* calc from sample rate */
2023 if (id == AV_CODEC_ID_TTA)
2024 return 256 * sr / 245;
2027 /* calc from sample rate and channels */
2028 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2029 return (480 << (sr / 22050)) / ch;
2034 /* calc from block_align */
2035 if (id == AV_CODEC_ID_SIPR) {
2037 case 20: return 160;
2038 case 19: return 144;
2039 case 29: return 288;
2040 case 37: return 480;
2042 } else if (id == AV_CODEC_ID_ILBC) {
2044 case 38: return 160;
2045 case 50: return 240;
2050 if (frame_bytes > 0) {
2051 /* calc from frame_bytes only */
2052 if (id == AV_CODEC_ID_TRUESPEECH)
2053 return 240 * (frame_bytes / 32);
2054 if (id == AV_CODEC_ID_NELLYMOSER)
2055 return 256 * (frame_bytes / 64);
2058 /* calc from frame_bytes and bits_per_coded_sample */
2059 if (id == AV_CODEC_ID_ADPCM_G726)
2060 return frame_bytes * 8 / bps;
2064 /* calc from frame_bytes and channels */
2066 case AV_CODEC_ID_ADPCM_4XM:
2067 case AV_CODEC_ID_ADPCM_IMA_ISS:
2068 return (frame_bytes - 4 * ch) * 2 / ch;
2069 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2070 return (frame_bytes - 4) * 2 / ch;
2071 case AV_CODEC_ID_ADPCM_IMA_AMV:
2072 return (frame_bytes - 8) * 2 / ch;
2073 case AV_CODEC_ID_ADPCM_XA:
2074 return (frame_bytes / 128) * 224 / ch;
2075 case AV_CODEC_ID_INTERPLAY_DPCM:
2076 return (frame_bytes - 6 - ch) / ch;
2077 case AV_CODEC_ID_ROQ_DPCM:
2078 return (frame_bytes - 8) / ch;
2079 case AV_CODEC_ID_XAN_DPCM:
2080 return (frame_bytes - 2 * ch) / ch;
2081 case AV_CODEC_ID_MACE3:
2082 return 3 * frame_bytes / ch;
2083 case AV_CODEC_ID_MACE6:
2084 return 6 * frame_bytes / ch;
2085 case AV_CODEC_ID_PCM_LXF:
2086 return 2 * (frame_bytes / (5 * ch));
2090 /* calc from frame_bytes, channels, and codec_tag */
2091 if (id == AV_CODEC_ID_SOL_DPCM) {
2093 return frame_bytes / ch;
2095 return frame_bytes * 2 / ch;
2100 /* calc from frame_bytes, channels, and block_align */
2101 int blocks = frame_bytes / ba;
2102 switch (avctx->codec_id) {
2103 case AV_CODEC_ID_ADPCM_IMA_WAV:
2104 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2105 case AV_CODEC_ID_ADPCM_IMA_DK3:
2106 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2107 case AV_CODEC_ID_ADPCM_IMA_DK4:
2108 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2109 case AV_CODEC_ID_ADPCM_MS:
2110 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2115 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2116 switch (avctx->codec_id) {
2117 case AV_CODEC_ID_PCM_DVD:
2118 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2119 case AV_CODEC_ID_PCM_BLURAY:
2120 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2121 case AV_CODEC_ID_S302M:
2122 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2132 int ff_thread_init(AVCodecContext *s)
2139 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2153 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2156 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2160 #if FF_API_MISSING_SAMPLE
2161 FF_DISABLE_DEPRECATION_WARNINGS
2162 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2164 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2165 "version to the newest one from Git. If the problem still "
2166 "occurs, it means that your file has a feature which has not "
2167 "been implemented.\n", feature);
2169 av_log_ask_for_sample(avc, NULL);
2172 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2174 va_list argument_list;
2176 va_start(argument_list, msg);
2179 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2180 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2181 "of this file to ftp://upload.libav.org/incoming/ "
2182 "and contact the libav-devel mailing list.\n");
2184 va_end(argument_list);
2186 FF_ENABLE_DEPRECATION_WARNINGS
2187 #endif /* FF_API_MISSING_SAMPLE */
2189 static AVHWAccel *first_hwaccel = NULL;
2191 void av_register_hwaccel(AVHWAccel *hwaccel)
2193 AVHWAccel **p = &first_hwaccel;
2197 hwaccel->next = NULL;
2200 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2202 return hwaccel ? hwaccel->next : first_hwaccel;
2205 AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
2207 enum AVCodecID codec_id = avctx->codec->id;
2208 enum AVPixelFormat pix_fmt = avctx->pix_fmt;
2210 AVHWAccel *hwaccel = NULL;
2212 while ((hwaccel = av_hwaccel_next(hwaccel)))
2213 if (hwaccel->id == codec_id
2214 && hwaccel->pix_fmt == pix_fmt)
2219 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2222 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2224 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2231 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2233 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2239 int avpriv_lock_avformat(void)
2242 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2248 int avpriv_unlock_avformat(void)
2251 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2257 unsigned int avpriv_toupper4(unsigned int x)
2259 return av_toupper(x & 0xFF) +
2260 (av_toupper((x >> 8) & 0xFF) << 8) +
2261 (av_toupper((x >> 16) & 0xFF) << 16) +
2262 (av_toupper((x >> 24) & 0xFF) << 24);
2265 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2269 dst->owner = src->owner;
2271 ret = av_frame_ref(dst->f, src->f);
2275 if (src->progress &&
2276 !(dst->progress = av_buffer_ref(src->progress))) {
2277 ff_thread_release_buffer(dst->owner, dst);
2278 return AVERROR(ENOMEM);
2286 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2289 return ff_get_buffer(avctx, f->f, flags);
2292 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2295 av_frame_unref(f->f);
2298 void ff_thread_finish_setup(AVCodecContext *avctx)
2302 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2306 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2312 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2314 if (codec_id <= AV_CODEC_ID_NONE)
2315 return AVMEDIA_TYPE_UNKNOWN;
2316 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2317 return AVMEDIA_TYPE_VIDEO;
2318 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2319 return AVMEDIA_TYPE_AUDIO;
2320 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2321 return AVMEDIA_TYPE_SUBTITLE;
2323 return AVMEDIA_TYPE_UNKNOWN;
2326 int avcodec_is_open(AVCodecContext *s)
2328 return !!s->internal;
2331 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2333 uint32_t * restrict state)
2341 for (i = 0; i < 3; i++) {
2342 uint32_t tmp = *state << 8;
2343 *state = tmp + *(p++);
2344 if (tmp == 0x100 || p == end)
2349 if (p[-1] > 1 ) p += 3;
2350 else if (p[-2] ) p += 2;
2351 else if (p[-3]|(p[-1]-1)) p++;
2358 p = FFMIN(p, end) - 4;
2359 *state = AV_RB32(p);