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/hwcontext.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/samplefmt.h"
41 #include "libavutil/dict.h"
43 #include "libavutil/opt.h"
45 #include "mpegvideo.h"
48 #include "bytestream.h"
55 static int volatile entangled_thread_counter = 0;
56 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
57 static void *codec_mutex;
58 static void *avformat_mutex;
60 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
63 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
68 av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
70 memset((uint8_t *)*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
73 /* encoder management */
74 static AVCodec *first_avcodec = NULL;
76 AVCodec *av_codec_next(const AVCodec *c)
84 static av_cold void avcodec_init(void)
86 static int initialized = 0;
93 ff_me_cmp_init_static();
96 int av_codec_is_encoder(const AVCodec *codec)
98 return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
101 int av_codec_is_decoder(const AVCodec *codec)
103 return codec && (codec->decode || codec->send_packet);
106 av_cold void avcodec_register(AVCodec *codec)
116 if (codec->init_static_data)
117 codec->init_static_data(codec);
121 unsigned avcodec_get_edge_width(void)
127 #if FF_API_SET_DIMENSIONS
128 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
130 ff_set_dimensions(s, width, height);
134 int ff_set_dimensions(AVCodecContext *s, int width, int height)
136 int ret = av_image_check_size(width, height, 0, s);
140 s->width = s->coded_width = width;
141 s->height = s->coded_height = height;
146 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
148 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
151 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
153 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
156 avctx->sample_aspect_ratio = sar;
161 int ff_side_data_update_matrix_encoding(AVFrame *frame,
162 enum AVMatrixEncoding matrix_encoding)
164 AVFrameSideData *side_data;
165 enum AVMatrixEncoding *data;
167 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
169 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
170 sizeof(enum AVMatrixEncoding));
173 return AVERROR(ENOMEM);
175 data = (enum AVMatrixEncoding*)side_data->data;
176 *data = matrix_encoding;
181 #if HAVE_SIMD_ALIGN_16
182 # define STRIDE_ALIGN 16
184 # define STRIDE_ALIGN 8
187 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
188 int linesize_align[AV_NUM_DATA_POINTERS])
194 switch (s->pix_fmt) {
195 case AV_PIX_FMT_YUV420P:
196 case AV_PIX_FMT_YUYV422:
197 case AV_PIX_FMT_YVYU422:
198 case AV_PIX_FMT_UYVY422:
199 case AV_PIX_FMT_YUV422P:
200 case AV_PIX_FMT_YUV440P:
201 case AV_PIX_FMT_YUV444P:
202 case AV_PIX_FMT_GBRP:
203 case AV_PIX_FMT_GBRAP:
204 case AV_PIX_FMT_GRAY8:
205 case AV_PIX_FMT_GRAY16BE:
206 case AV_PIX_FMT_GRAY16LE:
207 case AV_PIX_FMT_YUVJ420P:
208 case AV_PIX_FMT_YUVJ422P:
209 case AV_PIX_FMT_YUVJ440P:
210 case AV_PIX_FMT_YUVJ444P:
211 case AV_PIX_FMT_YUVA420P:
212 case AV_PIX_FMT_YUVA422P:
213 case AV_PIX_FMT_YUVA444P:
214 case AV_PIX_FMT_YUV420P9LE:
215 case AV_PIX_FMT_YUV420P9BE:
216 case AV_PIX_FMT_YUV420P10LE:
217 case AV_PIX_FMT_YUV420P10BE:
218 case AV_PIX_FMT_YUV422P9LE:
219 case AV_PIX_FMT_YUV422P9BE:
220 case AV_PIX_FMT_YUV422P10LE:
221 case AV_PIX_FMT_YUV422P10BE:
222 case AV_PIX_FMT_YUVA422P10LE:
223 case AV_PIX_FMT_YUVA422P10BE:
224 case AV_PIX_FMT_YUV444P9LE:
225 case AV_PIX_FMT_YUV444P9BE:
226 case AV_PIX_FMT_YUV444P10LE:
227 case AV_PIX_FMT_YUV444P10BE:
228 case AV_PIX_FMT_YUVA444P10LE:
229 case AV_PIX_FMT_YUVA444P10BE:
230 case AV_PIX_FMT_GBRP9LE:
231 case AV_PIX_FMT_GBRP9BE:
232 case AV_PIX_FMT_GBRP10LE:
233 case AV_PIX_FMT_GBRP10BE:
234 w_align = 16; //FIXME assume 16 pixel per macroblock
235 h_align = 16 * 2; // interlaced needs 2 macroblocks height
237 case AV_PIX_FMT_YUV411P:
238 case AV_PIX_FMT_UYYVYY411:
242 case AV_PIX_FMT_YUV410P:
243 if (s->codec_id == AV_CODEC_ID_SVQ1) {
247 case AV_PIX_FMT_RGB555:
248 if (s->codec_id == AV_CODEC_ID_RPZA) {
252 case AV_PIX_FMT_PAL8:
253 case AV_PIX_FMT_BGR8:
254 case AV_PIX_FMT_RGB8:
255 if (s->codec_id == AV_CODEC_ID_SMC) {
260 case AV_PIX_FMT_BGR24:
261 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
262 (s->codec_id == AV_CODEC_ID_ZLIB)) {
273 *width = FFALIGN(*width, w_align);
274 *height = FFALIGN(*height, h_align);
275 if (s->codec_id == AV_CODEC_ID_H264)
276 // some of the optimized chroma MC reads one line too much
279 for (i = 0; i < 4; i++)
280 linesize_align[i] = STRIDE_ALIGN;
283 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
285 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
286 int chroma_shift = desc->log2_chroma_w;
287 int linesize_align[AV_NUM_DATA_POINTERS];
290 avcodec_align_dimensions2(s, width, height, linesize_align);
291 align = FFMAX(linesize_align[0], linesize_align[3]);
292 linesize_align[1] <<= chroma_shift;
293 linesize_align[2] <<= chroma_shift;
294 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
295 *width = FFALIGN(*width, align);
298 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
299 enum AVSampleFormat sample_fmt, const uint8_t *buf,
300 int buf_size, int align)
302 int ch, planar, needed_size, ret = 0;
304 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
305 frame->nb_samples, sample_fmt,
307 if (buf_size < needed_size)
308 return AVERROR(EINVAL);
310 planar = av_sample_fmt_is_planar(sample_fmt);
311 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
312 if (!(frame->extended_data = av_mallocz(nb_channels *
313 sizeof(*frame->extended_data))))
314 return AVERROR(ENOMEM);
316 frame->extended_data = frame->data;
319 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
320 buf, nb_channels, frame->nb_samples,
321 sample_fmt, align)) < 0) {
322 if (frame->extended_data != frame->data)
323 av_free(frame->extended_data);
326 if (frame->extended_data != frame->data) {
327 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
328 frame->data[ch] = frame->extended_data[ch];
334 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
336 FramePool *pool = avctx->internal->pool;
339 switch (avctx->codec_type) {
340 case AVMEDIA_TYPE_VIDEO: {
344 int w = frame->width;
345 int h = frame->height;
346 int tmpsize, unaligned;
348 if (pool->format == frame->format &&
349 pool->width == frame->width && pool->height == frame->height)
352 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
355 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
356 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
357 av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
358 // increase alignment of w for next try (rhs gives the lowest bit set in w)
362 for (i = 0; i < 4; i++)
363 unaligned |= linesize[i] % pool->stride_align[i];
366 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
371 for (i = 0; i < 3 && data[i + 1]; i++)
372 size[i] = data[i + 1] - data[i];
373 size[i] = tmpsize - (data[i] - data[0]);
375 for (i = 0; i < 4; i++) {
376 av_buffer_pool_uninit(&pool->pools[i]);
377 pool->linesize[i] = linesize[i];
379 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
380 if (!pool->pools[i]) {
381 ret = AVERROR(ENOMEM);
386 pool->format = frame->format;
387 pool->width = frame->width;
388 pool->height = frame->height;
392 case AVMEDIA_TYPE_AUDIO: {
393 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
394 int planar = av_sample_fmt_is_planar(frame->format);
395 int planes = planar ? ch : 1;
397 if (pool->format == frame->format && pool->planes == planes &&
398 pool->channels == ch && frame->nb_samples == pool->samples)
401 av_buffer_pool_uninit(&pool->pools[0]);
402 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
403 frame->nb_samples, frame->format, 0);
407 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
408 if (!pool->pools[0]) {
409 ret = AVERROR(ENOMEM);
413 pool->format = frame->format;
414 pool->planes = planes;
416 pool->samples = frame->nb_samples;
419 default: av_assert0(0);
423 for (i = 0; i < 4; i++)
424 av_buffer_pool_uninit(&pool->pools[i]);
426 pool->planes = pool->channels = pool->samples = 0;
427 pool->width = pool->height = 0;
431 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
433 FramePool *pool = avctx->internal->pool;
434 int planes = pool->planes;
437 frame->linesize[0] = pool->linesize[0];
439 if (planes > AV_NUM_DATA_POINTERS) {
440 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
441 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
442 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
443 sizeof(*frame->extended_buf));
444 if (!frame->extended_data || !frame->extended_buf) {
445 av_freep(&frame->extended_data);
446 av_freep(&frame->extended_buf);
447 return AVERROR(ENOMEM);
450 frame->extended_data = frame->data;
452 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
453 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
456 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
458 for (i = 0; i < frame->nb_extended_buf; i++) {
459 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
460 if (!frame->extended_buf[i])
462 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
465 if (avctx->debug & FF_DEBUG_BUFFERS)
466 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
470 av_frame_unref(frame);
471 return AVERROR(ENOMEM);
474 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
476 FramePool *pool = s->internal->pool;
480 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
484 memset(pic->data, 0, sizeof(pic->data));
485 pic->extended_data = pic->data;
487 for (i = 0; i < 4 && pool->pools[i]; i++) {
488 pic->linesize[i] = pool->linesize[i];
490 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
494 pic->data[i] = pic->buf[i]->data;
496 for (; i < AV_NUM_DATA_POINTERS; i++) {
498 pic->linesize[i] = 0;
500 if (pic->data[1] && !pic->data[2])
501 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
503 if (s->debug & FF_DEBUG_BUFFERS)
504 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
509 return AVERROR(ENOMEM);
512 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
516 if ((ret = update_frame_pool(avctx, frame)) < 0)
519 switch (avctx->codec_type) {
520 case AVMEDIA_TYPE_VIDEO:
521 return video_get_buffer(avctx, frame);
522 case AVMEDIA_TYPE_AUDIO:
523 return audio_get_buffer(avctx, frame);
529 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
531 AVPacket *pkt = avctx->internal->pkt;
534 enum AVPacketSideDataType packet;
535 enum AVFrameSideDataType frame;
537 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
538 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
539 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
540 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
543 frame->color_primaries = avctx->color_primaries;
544 frame->color_trc = avctx->color_trc;
545 frame->colorspace = avctx->colorspace;
546 frame->color_range = avctx->color_range;
547 frame->chroma_location = avctx->chroma_sample_location;
549 frame->reordered_opaque = avctx->reordered_opaque;
551 frame->pkt_pts = AV_NOPTS_VALUE;
555 frame->pkt_pts = pkt->pts;
557 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
559 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
561 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
565 return AVERROR(ENOMEM);
567 memcpy(frame_sd->data, packet_sd, size);
574 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
576 const AVHWAccel *hwaccel = avctx->hwaccel;
577 int override_dimensions = 1;
580 switch (avctx->codec_type) {
581 case AVMEDIA_TYPE_VIDEO:
582 if (frame->width <= 0 || frame->height <= 0) {
583 frame->width = FFMAX(avctx->width, avctx->coded_width);
584 frame->height = FFMAX(avctx->height, avctx->coded_height);
585 override_dimensions = 0;
587 if (frame->format < 0)
588 frame->format = avctx->pix_fmt;
589 if (!frame->sample_aspect_ratio.num)
590 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
592 if (av_image_check_sar(frame->width, frame->height,
593 frame->sample_aspect_ratio) < 0) {
594 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
595 frame->sample_aspect_ratio.num,
596 frame->sample_aspect_ratio.den);
597 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
600 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
603 case AVMEDIA_TYPE_AUDIO:
604 if (!frame->sample_rate)
605 frame->sample_rate = avctx->sample_rate;
606 if (frame->format < 0)
607 frame->format = avctx->sample_fmt;
608 if (!frame->channel_layout) {
609 if (avctx->channel_layout) {
610 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
612 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
614 return AVERROR(EINVAL);
617 frame->channel_layout = avctx->channel_layout;
619 if (avctx->channels > FF_SANE_NB_CHANNELS) {
620 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
622 return AVERROR(ENOSYS);
625 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
626 if (!frame->channel_layout)
627 frame->channel_layout = (1ULL << avctx->channels) - 1;
631 default: return AVERROR(EINVAL);
634 ret = ff_decode_frame_props(avctx, frame);
639 if (hwaccel->alloc_frame) {
640 ret = hwaccel->alloc_frame(avctx, frame);
644 avctx->sw_pix_fmt = avctx->pix_fmt;
646 ret = avctx->get_buffer2(avctx, frame, flags);
649 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
650 frame->width = avctx->width;
651 frame->height = avctx->height;
657 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
662 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
665 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
667 if (av_frame_is_writable(frame))
668 return ff_decode_frame_props(avctx, frame);
670 tmp = av_frame_alloc();
672 return AVERROR(ENOMEM);
674 av_frame_move_ref(tmp, frame);
676 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
682 av_frame_copy(frame, tmp);
688 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
692 for (i = 0; i < count; i++) {
693 int r = func(c, (char *)arg + i * size);
700 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
704 for (i = 0; i < count; i++) {
705 int r = func(c, arg, i, 0);
712 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
714 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
715 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
718 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
720 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
725 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
726 enum AVPixelFormat pix_fmt)
728 AVHWAccel *hwaccel = NULL;
730 while ((hwaccel = av_hwaccel_next(hwaccel)))
731 if (hwaccel->id == codec_id
732 && hwaccel->pix_fmt == pix_fmt)
737 static int setup_hwaccel(AVCodecContext *avctx,
738 const enum AVPixelFormat fmt,
741 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
745 av_log(avctx, AV_LOG_ERROR,
746 "Could not find an AVHWAccel for the pixel format: %s",
748 return AVERROR(ENOENT);
751 if (hwa->priv_data_size) {
752 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
753 if (!avctx->internal->hwaccel_priv_data)
754 return AVERROR(ENOMEM);
758 ret = hwa->init(avctx);
760 av_freep(&avctx->internal->hwaccel_priv_data);
765 avctx->hwaccel = hwa;
770 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
772 const AVPixFmtDescriptor *desc;
773 enum AVPixelFormat *choices;
774 enum AVPixelFormat ret;
777 while (fmt[n] != AV_PIX_FMT_NONE)
781 avctx->sw_pix_fmt = fmt[n - 1];
782 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
784 choices = av_malloc_array(n + 1, sizeof(*choices));
786 return AV_PIX_FMT_NONE;
788 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
791 if (avctx->hwaccel && avctx->hwaccel->uninit)
792 avctx->hwaccel->uninit(avctx);
793 av_freep(&avctx->internal->hwaccel_priv_data);
794 avctx->hwaccel = NULL;
796 ret = avctx->get_format(avctx, choices);
798 desc = av_pix_fmt_desc_get(ret);
800 ret = AV_PIX_FMT_NONE;
804 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
807 if (!setup_hwaccel(avctx, ret, desc->name))
810 /* Remove failed hwaccel from choices */
811 for (n = 0; choices[n] != ret; n++)
812 av_assert0(choices[n] != AV_PIX_FMT_NONE);
815 choices[n] = choices[n + 1];
816 while (choices[n++] != AV_PIX_FMT_NONE);
823 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
826 AVDictionary *tmp = NULL;
828 if (avcodec_is_open(avctx))
831 if ((!codec && !avctx->codec)) {
832 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
833 return AVERROR(EINVAL);
835 if ((codec && avctx->codec && codec != avctx->codec)) {
836 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
837 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
838 return AVERROR(EINVAL);
841 codec = avctx->codec;
843 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
844 return AVERROR(EINVAL);
847 av_dict_copy(&tmp, *options, 0);
849 /* If there is a user-supplied mutex locking routine, call it. */
850 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
852 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
856 entangled_thread_counter++;
857 if (entangled_thread_counter != 1) {
858 av_log(avctx, AV_LOG_ERROR,
859 "Insufficient thread locking. At least %d threads are "
860 "calling avcodec_open2() at the same time right now.\n",
861 entangled_thread_counter);
867 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
868 if (!avctx->internal) {
869 ret = AVERROR(ENOMEM);
873 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
874 if (!avctx->internal->pool) {
875 ret = AVERROR(ENOMEM);
879 avctx->internal->to_free = av_frame_alloc();
880 if (!avctx->internal->to_free) {
881 ret = AVERROR(ENOMEM);
885 avctx->internal->buffer_frame = av_frame_alloc();
886 if (!avctx->internal->buffer_frame) {
887 ret = AVERROR(ENOMEM);
891 avctx->internal->buffer_pkt = av_packet_alloc();
892 if (!avctx->internal->buffer_pkt) {
893 ret = AVERROR(ENOMEM);
897 if (codec->priv_data_size > 0) {
898 if (!avctx->priv_data) {
899 avctx->priv_data = av_mallocz(codec->priv_data_size);
900 if (!avctx->priv_data) {
901 ret = AVERROR(ENOMEM);
904 if (codec->priv_class) {
905 *(const AVClass **)avctx->priv_data = codec->priv_class;
906 av_opt_set_defaults(avctx->priv_data);
909 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
912 avctx->priv_data = NULL;
914 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
917 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
918 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
919 else if (avctx->width && avctx->height)
920 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
924 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
925 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
926 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
927 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
928 ff_set_dimensions(avctx, 0, 0);
931 if (avctx->width > 0 && avctx->height > 0) {
932 if (av_image_check_sar(avctx->width, avctx->height,
933 avctx->sample_aspect_ratio) < 0) {
934 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
935 avctx->sample_aspect_ratio.num,
936 avctx->sample_aspect_ratio.den);
937 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
941 /* if the decoder init function was already called previously,
942 * free the already allocated subtitle_header before overwriting it */
943 if (av_codec_is_decoder(codec))
944 av_freep(&avctx->subtitle_header);
946 if (avctx->channels > FF_SANE_NB_CHANNELS) {
947 ret = AVERROR(EINVAL);
951 avctx->codec = codec;
952 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
953 avctx->codec_id == AV_CODEC_ID_NONE) {
954 avctx->codec_type = codec->type;
955 avctx->codec_id = codec->id;
957 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
958 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
959 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
960 ret = AVERROR(EINVAL);
963 avctx->frame_number = 0;
965 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
966 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
967 ret = AVERROR_EXPERIMENTAL;
971 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
972 (!avctx->time_base.num || !avctx->time_base.den)) {
973 avctx->time_base.num = 1;
974 avctx->time_base.den = avctx->sample_rate;
978 ret = ff_thread_init(avctx);
983 if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
984 avctx->thread_count = 1;
986 if (av_codec_is_encoder(avctx->codec)) {
988 #if FF_API_CODED_FRAME
989 FF_DISABLE_DEPRECATION_WARNINGS
990 avctx->coded_frame = av_frame_alloc();
991 if (!avctx->coded_frame) {
992 ret = AVERROR(ENOMEM);
995 FF_ENABLE_DEPRECATION_WARNINGS
998 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
999 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
1000 ret = AVERROR(EINVAL);
1004 if (avctx->codec->sample_fmts) {
1005 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1006 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1008 if (avctx->channels == 1 &&
1009 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1010 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1011 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1015 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1016 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1017 ret = AVERROR(EINVAL);
1021 if (avctx->codec->pix_fmts) {
1022 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1023 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1025 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1026 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1027 ret = AVERROR(EINVAL);
1030 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1031 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1032 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1033 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1034 avctx->color_range = AVCOL_RANGE_JPEG;
1036 if (avctx->codec->supported_samplerates) {
1037 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1038 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1040 if (avctx->codec->supported_samplerates[i] == 0) {
1041 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1042 ret = AVERROR(EINVAL);
1046 if (avctx->codec->channel_layouts) {
1047 if (!avctx->channel_layout) {
1048 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1050 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1051 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1053 if (avctx->codec->channel_layouts[i] == 0) {
1054 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1055 ret = AVERROR(EINVAL);
1060 if (avctx->channel_layout && avctx->channels) {
1061 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1062 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1063 ret = AVERROR(EINVAL);
1066 } else if (avctx->channel_layout) {
1067 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1070 if (!avctx->rc_initial_buffer_occupancy)
1071 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1073 if (avctx->ticks_per_frame &&
1074 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
1075 av_log(avctx, AV_LOG_ERROR,
1076 "ticks_per_frame %d too large for the timebase %d/%d.",
1077 avctx->ticks_per_frame,
1078 avctx->time_base.num,
1079 avctx->time_base.den);
1083 if (avctx->hw_frames_ctx) {
1084 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1085 if (frames_ctx->format != avctx->pix_fmt) {
1086 av_log(avctx, AV_LOG_ERROR,
1087 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
1088 ret = AVERROR(EINVAL);
1094 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1095 ret = avctx->codec->init(avctx);
1101 #if FF_API_AUDIOENC_DELAY
1102 if (av_codec_is_encoder(avctx->codec))
1103 avctx->delay = avctx->initial_padding;
1106 if (av_codec_is_decoder(avctx->codec)) {
1107 /* validate channel layout from the decoder */
1108 if (avctx->channel_layout) {
1109 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1110 if (!avctx->channels)
1111 avctx->channels = channels;
1112 else if (channels != avctx->channels) {
1113 av_log(avctx, AV_LOG_WARNING,
1114 "channel layout does not match number of channels\n");
1115 avctx->channel_layout = 0;
1118 if (avctx->channels && avctx->channels < 0 ||
1119 avctx->channels > FF_SANE_NB_CHANNELS) {
1120 ret = AVERROR(EINVAL);
1124 #if FF_API_AVCTX_TIMEBASE
1125 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1126 avctx->time_base = av_inv_q(avctx->framerate);
1130 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
1131 entangled_thread_counter--;
1133 /* Release any user-supplied mutex. */
1135 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1140 av_dict_free(options);
1147 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1148 avctx->codec->close(avctx);
1150 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1151 av_opt_free(avctx->priv_data);
1154 #if FF_API_CODED_FRAME
1155 FF_DISABLE_DEPRECATION_WARNINGS
1156 av_frame_free(&avctx->coded_frame);
1157 FF_ENABLE_DEPRECATION_WARNINGS
1161 av_freep(&avctx->priv_data);
1162 if (avctx->internal) {
1163 av_frame_free(&avctx->internal->to_free);
1164 av_freep(&avctx->internal->pool);
1166 av_freep(&avctx->internal);
1167 avctx->codec = NULL;
1171 int ff_alloc_packet(AVPacket *avpkt, int size)
1173 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
1174 return AVERROR(EINVAL);
1177 AVBufferRef *buf = avpkt->buf;
1179 if (avpkt->size < size)
1180 return AVERROR(EINVAL);
1182 av_init_packet(avpkt);
1187 return av_new_packet(avpkt, size);
1192 * Pad last frame with silence.
1194 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1196 AVFrame *frame = NULL;
1199 if (!(frame = av_frame_alloc()))
1200 return AVERROR(ENOMEM);
1202 frame->format = src->format;
1203 frame->channel_layout = src->channel_layout;
1204 frame->nb_samples = s->frame_size;
1205 ret = av_frame_get_buffer(frame, 32);
1209 ret = av_frame_copy_props(frame, src);
1213 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1214 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1216 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1217 frame->nb_samples - src->nb_samples,
1218 s->channels, s->sample_fmt)) < 0)
1226 av_frame_free(&frame);
1230 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1232 const AVFrame *frame,
1233 int *got_packet_ptr)
1236 AVFrame *padded_frame = NULL;
1238 int user_packet = !!avpkt->data;
1240 *got_packet_ptr = 0;
1242 if (!avctx->codec->encode2) {
1243 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
1244 return AVERROR(ENOSYS);
1247 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1248 av_packet_unref(avpkt);
1249 av_init_packet(avpkt);
1253 /* ensure that extended_data is properly set */
1254 if (frame && !frame->extended_data) {
1255 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1256 avctx->channels > AV_NUM_DATA_POINTERS) {
1257 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1258 "with more than %d channels, but extended_data is not set.\n",
1259 AV_NUM_DATA_POINTERS);
1260 return AVERROR(EINVAL);
1262 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1265 tmp.extended_data = tmp.data;
1269 /* extract audio service type metadata */
1271 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
1272 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1273 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1276 /* check for valid frame size */
1278 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
1279 if (frame->nb_samples > avctx->frame_size)
1280 return AVERROR(EINVAL);
1281 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1282 if (frame->nb_samples < avctx->frame_size &&
1283 !avctx->internal->last_audio_frame) {
1284 ret = pad_last_frame(avctx, &padded_frame, frame);
1288 frame = padded_frame;
1289 avctx->internal->last_audio_frame = 1;
1292 if (frame->nb_samples != avctx->frame_size) {
1293 ret = AVERROR(EINVAL);
1299 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1301 if (*got_packet_ptr) {
1302 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
1303 if (avpkt->pts == AV_NOPTS_VALUE)
1304 avpkt->pts = frame->pts;
1305 if (!avpkt->duration)
1306 avpkt->duration = ff_samples_to_time_base(avctx,
1309 avpkt->dts = avpkt->pts;
1314 if (!user_packet && avpkt->size) {
1315 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1317 avpkt->data = avpkt->buf->data;
1320 avctx->frame_number++;
1323 if (ret < 0 || !*got_packet_ptr) {
1324 av_packet_unref(avpkt);
1325 av_init_packet(avpkt);
1329 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1330 * this needs to be moved to the encoders, but for now we can do it
1331 * here to simplify things */
1332 avpkt->flags |= AV_PKT_FLAG_KEY;
1335 av_frame_free(&padded_frame);
1337 #if FF_API_AUDIOENC_DELAY
1338 avctx->delay = avctx->initial_padding;
1344 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1346 const AVFrame *frame,
1347 int *got_packet_ptr)
1350 int user_packet = !!avpkt->data;
1352 *got_packet_ptr = 0;
1354 if (!avctx->codec->encode2) {
1355 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
1356 return AVERROR(ENOSYS);
1359 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1360 av_packet_unref(avpkt);
1361 av_init_packet(avpkt);
1366 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1367 return AVERROR(EINVAL);
1369 av_assert0(avctx->codec->encode2);
1371 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1373 if (!*got_packet_ptr)
1375 else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1376 avpkt->pts = avpkt->dts = frame->pts;
1378 if (!user_packet && avpkt->size) {
1379 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1381 avpkt->data = avpkt->buf->data;
1384 avctx->frame_number++;
1387 if (ret < 0 || !*got_packet_ptr)
1388 av_packet_unref(avpkt);
1394 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1395 const AVSubtitle *sub)
1398 if (sub->start_display_time) {
1399 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1402 if (sub->num_rects == 0 || !sub->rects)
1404 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1405 avctx->frame_number++;
1409 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1412 const uint8_t *data;
1415 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1419 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
1420 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1421 "changes, but PARAM_CHANGE side data was sent to it.\n");
1422 ret = AVERROR(EINVAL);
1429 flags = bytestream_get_le32(&data);
1432 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1435 avctx->channels = bytestream_get_le32(&data);
1438 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1441 avctx->channel_layout = bytestream_get_le64(&data);
1444 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1447 avctx->sample_rate = bytestream_get_le32(&data);
1450 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1453 avctx->width = bytestream_get_le32(&data);
1454 avctx->height = bytestream_get_le32(&data);
1456 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1463 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1464 ret = AVERROR_INVALIDDATA;
1467 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1468 if (avctx->err_recognition & AV_EF_EXPLODE)
1474 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1478 /* move the original frame to our backup */
1479 av_frame_unref(avci->to_free);
1480 av_frame_move_ref(avci->to_free, frame);
1482 /* now copy everything except the AVBufferRefs back
1483 * note that we make a COPY of the side data, so calling av_frame_free() on
1484 * the caller's frame will work properly */
1485 ret = av_frame_copy_props(frame, avci->to_free);
1489 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1490 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1491 if (avci->to_free->extended_data != avci->to_free->data) {
1492 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1493 int size = planes * sizeof(*frame->extended_data);
1496 av_frame_unref(frame);
1500 frame->extended_data = av_malloc(size);
1501 if (!frame->extended_data) {
1502 av_frame_unref(frame);
1503 return AVERROR(ENOMEM);
1505 memcpy(frame->extended_data, avci->to_free->extended_data,
1508 frame->extended_data = frame->data;
1510 frame->format = avci->to_free->format;
1511 frame->width = avci->to_free->width;
1512 frame->height = avci->to_free->height;
1513 frame->channel_layout = avci->to_free->channel_layout;
1514 frame->nb_samples = avci->to_free->nb_samples;
1519 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1520 int *got_picture_ptr,
1523 AVCodecInternal *avci = avctx->internal;
1526 *got_picture_ptr = 0;
1527 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1530 if (!avctx->codec->decode) {
1531 av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
1532 return AVERROR(ENOSYS);
1535 avctx->internal->pkt = avpkt;
1536 ret = apply_param_change(avctx, avpkt);
1540 av_frame_unref(picture);
1542 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
1543 (avctx->active_thread_type & FF_THREAD_FRAME)) {
1544 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1545 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1548 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1550 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
1551 picture->pkt_dts = avpkt->dts;
1552 /* get_buffer is supposed to set frame parameters */
1553 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
1554 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1555 picture->width = avctx->width;
1556 picture->height = avctx->height;
1557 picture->format = avctx->pix_fmt;
1561 emms_c(); //needed to avoid an emms_c() call before every return;
1563 if (*got_picture_ptr) {
1564 if (!avctx->refcounted_frames) {
1565 int err = unrefcount_frame(avci, picture);
1570 avctx->frame_number++;
1572 av_frame_unref(picture);
1576 #if FF_API_AVCTX_TIMEBASE
1577 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1578 avctx->time_base = av_inv_q(avctx->framerate);
1584 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1589 AVCodecInternal *avci = avctx->internal;
1594 if (!avctx->codec->decode) {
1595 av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
1596 return AVERROR(ENOSYS);
1599 avctx->internal->pkt = avpkt;
1601 if (!avpkt->data && avpkt->size) {
1602 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1603 return AVERROR(EINVAL);
1606 ret = apply_param_change(avctx, avpkt);
1610 av_frame_unref(frame);
1612 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
1613 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1614 if (ret >= 0 && *got_frame_ptr) {
1615 avctx->frame_number++;
1616 frame->pkt_dts = avpkt->dts;
1617 if (frame->format == AV_SAMPLE_FMT_NONE)
1618 frame->format = avctx->sample_fmt;
1620 if (!avctx->refcounted_frames) {
1621 int err = unrefcount_frame(avci, frame);
1626 av_frame_unref(frame);
1633 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1639 avctx->internal->pkt = avpkt;
1641 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1643 avctx->frame_number++;
1647 void avsubtitle_free(AVSubtitle *sub)
1651 for (i = 0; i < sub->num_rects; i++) {
1652 av_freep(&sub->rects[i]->data[0]);
1653 av_freep(&sub->rects[i]->data[1]);
1654 av_freep(&sub->rects[i]->data[2]);
1655 av_freep(&sub->rects[i]->data[3]);
1656 av_freep(&sub->rects[i]->text);
1657 av_freep(&sub->rects[i]->ass);
1658 av_freep(&sub->rects[i]);
1661 av_freep(&sub->rects);
1663 memset(sub, 0, sizeof(AVSubtitle));
1666 static int do_decode(AVCodecContext *avctx, AVPacket *pkt)
1671 av_assert0(!avctx->internal->buffer_frame->buf[0]);
1674 pkt = avctx->internal->buffer_pkt;
1676 // This is the lesser evil. The field is for compatibility with legacy users
1677 // of the legacy API, and users using the new API should not be forced to
1678 // even know about this field.
1679 avctx->refcounted_frames = 1;
1681 // Some codecs (at least wma lossless) will crash when feeding drain packets
1682 // after EOF was signaled.
1683 if (avctx->internal->draining_done)
1686 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1687 ret = avcodec_decode_video2(avctx, avctx->internal->buffer_frame,
1691 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1692 ret = avcodec_decode_audio4(avctx, avctx->internal->buffer_frame,
1695 ret = AVERROR(EINVAL);
1701 if (avctx->internal->draining && !got_frame)
1702 avctx->internal->draining_done = 1;
1704 if (ret >= pkt->size) {
1705 av_packet_unref(avctx->internal->buffer_pkt);
1709 if (pkt != avctx->internal->buffer_pkt) {
1710 av_packet_unref(avctx->internal->buffer_pkt);
1711 if ((ret = av_packet_ref(avctx->internal->buffer_pkt, pkt)) < 0)
1715 avctx->internal->buffer_pkt->data += consumed;
1716 avctx->internal->buffer_pkt->size -= consumed;
1717 avctx->internal->buffer_pkt->pts = AV_NOPTS_VALUE;
1718 avctx->internal->buffer_pkt->dts = AV_NOPTS_VALUE;
1722 av_assert0(avctx->internal->buffer_frame->buf[0]);
1727 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
1731 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
1732 return AVERROR(EINVAL);
1734 if (avctx->internal->draining)
1737 if (!avpkt || !avpkt->size) {
1738 avctx->internal->draining = 1;
1741 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1745 if (avctx->codec->send_packet) {
1747 ret = apply_param_change(avctx, (AVPacket *)avpkt);
1751 return avctx->codec->send_packet(avctx, avpkt);
1754 // Emulation via old API. Assume avpkt is likely not refcounted, while
1755 // decoder output is always refcounted, and avoid copying.
1757 if (avctx->internal->buffer_pkt->size || avctx->internal->buffer_frame->buf[0])
1758 return AVERROR(EAGAIN);
1760 // The goal is decoding the first frame of the packet without using memcpy,
1761 // because the common case is having only 1 frame per packet (especially
1762 // with video, but audio too). In other cases, it can't be avoided, unless
1763 // the user is feeding refcounted packets.
1764 return do_decode(avctx, (AVPacket *)avpkt);
1767 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1771 av_frame_unref(frame);
1773 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
1774 return AVERROR(EINVAL);
1776 if (avctx->codec->receive_frame) {
1777 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1779 return avctx->codec->receive_frame(avctx, frame);
1782 // Emulation via old API.
1784 if (!avctx->internal->buffer_frame->buf[0]) {
1785 if (!avctx->internal->buffer_pkt->size && !avctx->internal->draining)
1786 return AVERROR(EAGAIN);
1789 if ((ret = do_decode(avctx, avctx->internal->buffer_pkt)) < 0) {
1790 av_packet_unref(avctx->internal->buffer_pkt);
1793 // Some audio decoders may consume partial data without returning
1794 // a frame (fate-wmapro-2ch). There is no way to make the caller
1795 // call avcodec_receive_frame() again without returning a frame,
1796 // so try to decode more in these cases.
1797 if (avctx->internal->buffer_frame->buf[0] ||
1798 !avctx->internal->buffer_pkt->size)
1803 if (!avctx->internal->buffer_frame->buf[0])
1804 return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
1806 av_frame_move_ref(frame, avctx->internal->buffer_frame);
1810 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
1815 av_packet_unref(avctx->internal->buffer_pkt);
1816 avctx->internal->buffer_pkt_valid = 0;
1818 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1819 ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
1821 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1822 ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
1825 ret = AVERROR(EINVAL);
1828 if (ret >= 0 && *got_packet) {
1829 // Encoders must always return ref-counted buffers.
1830 // Side-data only packets have no data and can be not ref-counted.
1831 av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
1832 avctx->internal->buffer_pkt_valid = 1;
1835 av_packet_unref(avctx->internal->buffer_pkt);
1841 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
1843 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
1844 return AVERROR(EINVAL);
1846 if (avctx->internal->draining)
1850 avctx->internal->draining = 1;
1852 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1856 if (avctx->codec->send_frame)
1857 return avctx->codec->send_frame(avctx, frame);
1859 // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
1860 // 1. if the AVFrame is not refcounted, the copying will be much more
1861 // expensive than copying the packet data
1862 // 2. assume few users use non-refcounted AVPackets, so usually no copy is
1865 if (avctx->internal->buffer_pkt_valid)
1866 return AVERROR(EAGAIN);
1868 return do_encode(avctx, frame, &(int){0});
1871 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
1873 av_packet_unref(avpkt);
1875 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
1876 return AVERROR(EINVAL);
1878 if (avctx->codec->receive_packet) {
1879 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1881 return avctx->codec->receive_packet(avctx, avpkt);
1884 // Emulation via old API.
1886 if (!avctx->internal->buffer_pkt_valid) {
1889 if (!avctx->internal->draining)
1890 return AVERROR(EAGAIN);
1891 ret = do_encode(avctx, NULL, &got_packet);
1894 if (ret >= 0 && !got_packet)
1898 av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
1899 avctx->internal->buffer_pkt_valid = 0;
1903 av_cold int avcodec_close(AVCodecContext *avctx)
1907 if (avcodec_is_open(avctx)) {
1908 FramePool *pool = avctx->internal->pool;
1910 if (HAVE_THREADS && avctx->internal->thread_ctx)
1911 ff_thread_free(avctx);
1912 if (avctx->codec && avctx->codec->close)
1913 avctx->codec->close(avctx);
1914 av_frame_free(&avctx->internal->to_free);
1915 av_frame_free(&avctx->internal->buffer_frame);
1916 av_packet_free(&avctx->internal->buffer_pkt);
1917 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1918 av_buffer_pool_uninit(&pool->pools[i]);
1919 av_freep(&avctx->internal->pool);
1921 if (avctx->hwaccel && avctx->hwaccel->uninit)
1922 avctx->hwaccel->uninit(avctx);
1923 av_freep(&avctx->internal->hwaccel_priv_data);
1925 av_freep(&avctx->internal);
1928 for (i = 0; i < avctx->nb_coded_side_data; i++)
1929 av_freep(&avctx->coded_side_data[i].data);
1930 av_freep(&avctx->coded_side_data);
1931 avctx->nb_coded_side_data = 0;
1933 av_buffer_unref(&avctx->hw_frames_ctx);
1935 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1936 av_opt_free(avctx->priv_data);
1938 av_freep(&avctx->priv_data);
1939 if (av_codec_is_encoder(avctx->codec)) {
1940 av_freep(&avctx->extradata);
1941 #if FF_API_CODED_FRAME
1942 FF_DISABLE_DEPRECATION_WARNINGS
1943 av_frame_free(&avctx->coded_frame);
1944 FF_ENABLE_DEPRECATION_WARNINGS
1947 avctx->codec = NULL;
1948 avctx->active_thread_type = 0;
1953 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1955 AVCodec *p, *experimental = NULL;
1958 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1960 if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
1967 return experimental;
1970 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1972 return find_encdec(id, 1);
1975 AVCodec *avcodec_find_encoder_by_name(const char *name)
1982 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1989 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1991 return find_encdec(id, 0);
1994 AVCodec *avcodec_find_decoder_by_name(const char *name)
2001 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2008 static int get_bit_rate(AVCodecContext *ctx)
2011 int bits_per_sample;
2013 switch (ctx->codec_type) {
2014 case AVMEDIA_TYPE_VIDEO:
2015 case AVMEDIA_TYPE_DATA:
2016 case AVMEDIA_TYPE_SUBTITLE:
2017 case AVMEDIA_TYPE_ATTACHMENT:
2018 bit_rate = ctx->bit_rate;
2020 case AVMEDIA_TYPE_AUDIO:
2021 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
2022 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
2031 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2033 int i, len, ret = 0;
2035 #define TAG_PRINT(x) \
2036 (((x) >= '0' && (x) <= '9') || \
2037 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2038 ((x) == '.' || (x) == ' '))
2040 for (i = 0; i < 4; i++) {
2041 len = snprintf(buf, buf_size,
2042 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2044 buf_size = buf_size > len ? buf_size - len : 0;
2051 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2053 const char *codec_name;
2054 const char *profile = NULL;
2058 AVRational display_aspect_ratio;
2059 const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
2062 codec_name = desc->name;
2063 profile = avcodec_profile_name(enc->codec_id, enc->profile);
2064 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
2065 /* fake mpeg2 transport stream codec (currently not
2067 codec_name = "mpeg2ts";
2069 /* output avi tags */
2071 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2072 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
2076 switch (enc->codec_type) {
2077 case AVMEDIA_TYPE_VIDEO:
2078 snprintf(buf, buf_size,
2080 codec_name, enc->mb_decision ? " (hq)" : "");
2082 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2084 if (enc->codec_tag) {
2086 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2087 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2088 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2091 av_strlcat(buf, "\n ", buf_size);
2092 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2093 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
2094 av_get_pix_fmt_name(enc->pix_fmt));
2096 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2097 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
2098 av_color_range_name(enc->color_range));
2099 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
2100 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
2101 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
2103 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
2104 av_color_space_name(enc->colorspace),
2105 av_color_primaries_name(enc->color_primaries),
2106 av_color_transfer_name(enc->color_trc));
2108 if (av_log_get_level() >= AV_LOG_DEBUG &&
2109 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
2110 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
2111 av_chroma_location_name(enc->chroma_sample_location));
2114 av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
2116 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2118 enc->width, enc->height);
2120 if (av_log_get_level() >= AV_LOG_VERBOSE &&
2121 (enc->width != enc->coded_width ||
2122 enc->height != enc->coded_height))
2123 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2124 " (%dx%d)", enc->coded_width, enc->coded_height);
2126 if (enc->sample_aspect_ratio.num) {
2127 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2128 enc->width * enc->sample_aspect_ratio.num,
2129 enc->height * enc->sample_aspect_ratio.den,
2131 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2132 " [PAR %d:%d DAR %d:%d]",
2133 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2134 display_aspect_ratio.num, display_aspect_ratio.den);
2136 if (av_log_get_level() >= AV_LOG_DEBUG) {
2137 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2138 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2140 enc->time_base.num / g, enc->time_base.den / g);
2144 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2145 ", q=%d-%d", enc->qmin, enc->qmax);
2148 case AVMEDIA_TYPE_AUDIO:
2149 snprintf(buf, buf_size,
2153 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2155 if (enc->codec_tag) {
2157 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2158 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2159 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2162 av_strlcat(buf, "\n ", buf_size);
2163 if (enc->sample_rate) {
2164 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2165 "%d Hz, ", enc->sample_rate);
2167 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2168 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2169 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2170 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2173 case AVMEDIA_TYPE_DATA:
2174 snprintf(buf, buf_size, "Data: %s", codec_name);
2176 case AVMEDIA_TYPE_SUBTITLE:
2177 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
2179 case AVMEDIA_TYPE_ATTACHMENT:
2180 snprintf(buf, buf_size, "Attachment: %s", codec_name);
2183 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
2187 if (enc->flags & AV_CODEC_FLAG_PASS1)
2188 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2190 if (enc->flags & AV_CODEC_FLAG_PASS2)
2191 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2194 bitrate = get_bit_rate(enc);
2196 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2197 ", %d kb/s", bitrate / 1000);
2201 const char *av_get_profile_name(const AVCodec *codec, int profile)
2204 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2207 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2208 if (p->profile == profile)
2214 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
2216 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
2219 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
2222 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2223 if (p->profile == profile)
2229 unsigned avcodec_version(void)
2231 return LIBAVCODEC_VERSION_INT;
2234 const char *avcodec_configuration(void)
2236 return LIBAV_CONFIGURATION;
2239 const char *avcodec_license(void)
2241 #define LICENSE_PREFIX "libavcodec license: "
2242 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2245 void avcodec_flush_buffers(AVCodecContext *avctx)
2247 avctx->internal->draining = 0;
2248 avctx->internal->draining_done = 0;
2249 av_frame_unref(avctx->internal->buffer_frame);
2250 av_packet_unref(avctx->internal->buffer_pkt);
2251 avctx->internal->buffer_pkt_valid = 0;
2253 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2254 ff_thread_flush(avctx);
2255 else if (avctx->codec->flush)
2256 avctx->codec->flush(avctx);
2258 if (!avctx->refcounted_frames)
2259 av_frame_unref(avctx->internal->to_free);
2262 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2265 case AV_CODEC_ID_ADPCM_CT:
2266 case AV_CODEC_ID_ADPCM_IMA_APC:
2267 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2268 case AV_CODEC_ID_ADPCM_IMA_WS:
2269 case AV_CODEC_ID_ADPCM_G722:
2270 case AV_CODEC_ID_ADPCM_YAMAHA:
2272 case AV_CODEC_ID_PCM_ALAW:
2273 case AV_CODEC_ID_PCM_MULAW:
2274 case AV_CODEC_ID_PCM_S8:
2275 case AV_CODEC_ID_PCM_U8:
2276 case AV_CODEC_ID_PCM_ZORK:
2278 case AV_CODEC_ID_PCM_S16BE:
2279 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2280 case AV_CODEC_ID_PCM_S16LE:
2281 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2282 case AV_CODEC_ID_PCM_U16BE:
2283 case AV_CODEC_ID_PCM_U16LE:
2285 case AV_CODEC_ID_PCM_S24DAUD:
2286 case AV_CODEC_ID_PCM_S24BE:
2287 case AV_CODEC_ID_PCM_S24LE:
2288 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2289 case AV_CODEC_ID_PCM_U24BE:
2290 case AV_CODEC_ID_PCM_U24LE:
2292 case AV_CODEC_ID_PCM_S32BE:
2293 case AV_CODEC_ID_PCM_S32LE:
2294 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2295 case AV_CODEC_ID_PCM_U32BE:
2296 case AV_CODEC_ID_PCM_U32LE:
2297 case AV_CODEC_ID_PCM_F32BE:
2298 case AV_CODEC_ID_PCM_F32LE:
2300 case AV_CODEC_ID_PCM_F64BE:
2301 case AV_CODEC_ID_PCM_F64LE:
2308 int av_get_bits_per_sample(enum AVCodecID codec_id)
2311 case AV_CODEC_ID_ADPCM_SBPRO_2:
2313 case AV_CODEC_ID_ADPCM_SBPRO_3:
2315 case AV_CODEC_ID_ADPCM_SBPRO_4:
2316 case AV_CODEC_ID_ADPCM_IMA_WAV:
2317 case AV_CODEC_ID_ADPCM_IMA_QT:
2318 case AV_CODEC_ID_ADPCM_SWF:
2319 case AV_CODEC_ID_ADPCM_MS:
2322 return av_get_exact_bits_per_sample(codec_id);
2326 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
2327 uint32_t tag, int bits_per_coded_sample, int frame_bytes)
2329 int bps = av_get_exact_bits_per_sample(id);
2331 /* codecs with an exact constant bits per sample */
2332 if (bps > 0 && ch > 0 && frame_bytes > 0)
2333 return (frame_bytes * 8) / (bps * ch);
2334 bps = bits_per_coded_sample;
2336 /* codecs with a fixed packet duration */
2338 case AV_CODEC_ID_ADPCM_ADX: return 32;
2339 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2340 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2341 case AV_CODEC_ID_AMR_NB:
2342 case AV_CODEC_ID_GSM:
2343 case AV_CODEC_ID_QCELP:
2344 case AV_CODEC_ID_RA_144:
2345 case AV_CODEC_ID_RA_288: return 160;
2346 case AV_CODEC_ID_IMC: return 256;
2347 case AV_CODEC_ID_AMR_WB:
2348 case AV_CODEC_ID_GSM_MS: return 320;
2349 case AV_CODEC_ID_MP1: return 384;
2350 case AV_CODEC_ID_ATRAC1: return 512;
2351 case AV_CODEC_ID_ATRAC3: return 1024;
2352 case AV_CODEC_ID_MP2:
2353 case AV_CODEC_ID_MUSEPACK7: return 1152;
2354 case AV_CODEC_ID_AC3: return 1536;
2358 /* calc from sample rate */
2359 if (id == AV_CODEC_ID_TTA)
2360 return 256 * sr / 245;
2363 /* calc from sample rate and channels */
2364 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2365 return (480 << (sr / 22050)) / ch;
2370 /* calc from block_align */
2371 if (id == AV_CODEC_ID_SIPR) {
2373 case 20: return 160;
2374 case 19: return 144;
2375 case 29: return 288;
2376 case 37: return 480;
2378 } else if (id == AV_CODEC_ID_ILBC) {
2380 case 38: return 160;
2381 case 50: return 240;
2386 if (frame_bytes > 0) {
2387 /* calc from frame_bytes only */
2388 if (id == AV_CODEC_ID_TRUESPEECH)
2389 return 240 * (frame_bytes / 32);
2390 if (id == AV_CODEC_ID_NELLYMOSER)
2391 return 256 * (frame_bytes / 64);
2394 /* calc from frame_bytes and bits_per_coded_sample */
2395 if (id == AV_CODEC_ID_ADPCM_G726)
2396 return frame_bytes * 8 / bps;
2400 /* calc from frame_bytes and channels */
2402 case AV_CODEC_ID_ADPCM_4XM:
2403 case AV_CODEC_ID_ADPCM_IMA_ISS:
2404 return (frame_bytes - 4 * ch) * 2 / ch;
2405 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2406 return (frame_bytes - 4) * 2 / ch;
2407 case AV_CODEC_ID_ADPCM_IMA_AMV:
2408 return (frame_bytes - 8) * 2 / ch;
2409 case AV_CODEC_ID_ADPCM_XA:
2410 return (frame_bytes / 128) * 224 / ch;
2411 case AV_CODEC_ID_INTERPLAY_DPCM:
2412 return (frame_bytes - 6 - ch) / ch;
2413 case AV_CODEC_ID_ROQ_DPCM:
2414 return (frame_bytes - 8) / ch;
2415 case AV_CODEC_ID_XAN_DPCM:
2416 return (frame_bytes - 2 * ch) / ch;
2417 case AV_CODEC_ID_MACE3:
2418 return 3 * frame_bytes / ch;
2419 case AV_CODEC_ID_MACE6:
2420 return 6 * frame_bytes / ch;
2421 case AV_CODEC_ID_PCM_LXF:
2422 return 2 * (frame_bytes / (5 * ch));
2426 /* calc from frame_bytes, channels, and codec_tag */
2427 if (id == AV_CODEC_ID_SOL_DPCM) {
2429 return frame_bytes / ch;
2431 return frame_bytes * 2 / ch;
2436 /* calc from frame_bytes, channels, and block_align */
2437 int blocks = frame_bytes / ba;
2439 case AV_CODEC_ID_ADPCM_IMA_WAV:
2440 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2441 case AV_CODEC_ID_ADPCM_IMA_DK3:
2442 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2443 case AV_CODEC_ID_ADPCM_IMA_DK4:
2444 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2445 case AV_CODEC_ID_ADPCM_MS:
2446 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2451 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2453 case AV_CODEC_ID_PCM_DVD:
2454 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2455 case AV_CODEC_ID_PCM_BLURAY:
2456 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2457 case AV_CODEC_ID_S302M:
2458 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2467 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2469 return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
2470 avctx->channels, avctx->block_align,
2471 avctx->codec_tag, avctx->bits_per_coded_sample,
2475 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
2477 return get_audio_frame_duration(par->codec_id, par->sample_rate,
2478 par->channels, par->block_align,
2479 par->codec_tag, par->bits_per_coded_sample,
2484 int ff_thread_init(AVCodecContext *s)
2491 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2505 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2508 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2512 #if FF_API_MISSING_SAMPLE
2513 FF_DISABLE_DEPRECATION_WARNINGS
2514 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2516 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2517 "version to the newest one from Git. If the problem still "
2518 "occurs, it means that your file has a feature which has not "
2519 "been implemented.\n", feature);
2521 av_log_ask_for_sample(avc, NULL);
2524 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2526 va_list argument_list;
2528 va_start(argument_list, msg);
2531 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2532 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2533 "of this file to ftp://upload.libav.org/incoming/ "
2534 "and contact the libav-devel mailing list.\n");
2536 va_end(argument_list);
2538 FF_ENABLE_DEPRECATION_WARNINGS
2539 #endif /* FF_API_MISSING_SAMPLE */
2541 static AVHWAccel *first_hwaccel = NULL;
2543 void av_register_hwaccel(AVHWAccel *hwaccel)
2545 AVHWAccel **p = &first_hwaccel;
2549 hwaccel->next = NULL;
2552 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2554 return hwaccel ? hwaccel->next : first_hwaccel;
2557 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2560 // There is no good way to rollback a failure to destroy the
2561 // mutex, so we ignore failures.
2562 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
2563 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
2566 avformat_mutex = NULL;
2570 void *new_codec_mutex = NULL;
2571 void *new_avformat_mutex = NULL;
2573 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
2574 return err > 0 ? AVERROR_UNKNOWN : err;
2576 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
2577 // Ignore failures to destroy the newly created mutex.
2578 cb(&new_codec_mutex, AV_LOCK_DESTROY);
2579 return err > 0 ? AVERROR_UNKNOWN : err;
2582 codec_mutex = new_codec_mutex;
2583 avformat_mutex = new_avformat_mutex;
2589 int avpriv_lock_avformat(void)
2592 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2598 int avpriv_unlock_avformat(void)
2601 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2607 unsigned int avpriv_toupper4(unsigned int x)
2609 return av_toupper(x & 0xFF) +
2610 (av_toupper((x >> 8) & 0xFF) << 8) +
2611 (av_toupper((x >> 16) & 0xFF) << 16) +
2612 (av_toupper((x >> 24) & 0xFF) << 24);
2615 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2619 dst->owner = src->owner;
2621 ret = av_frame_ref(dst->f, src->f);
2625 if (src->progress &&
2626 !(dst->progress = av_buffer_ref(src->progress))) {
2627 ff_thread_release_buffer(dst->owner, dst);
2628 return AVERROR(ENOMEM);
2636 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2639 return ff_get_buffer(avctx, f->f, flags);
2642 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2645 av_frame_unref(f->f);
2648 void ff_thread_finish_setup(AVCodecContext *avctx)
2652 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2656 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2662 int avcodec_is_open(AVCodecContext *s)
2664 return !!s->internal;
2667 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2669 uint32_t * restrict state)
2677 for (i = 0; i < 3; i++) {
2678 uint32_t tmp = *state << 8;
2679 *state = tmp + *(p++);
2680 if (tmp == 0x100 || p == end)
2685 if (p[-1] > 1 ) p += 3;
2686 else if (p[-2] ) p += 2;
2687 else if (p[-3]|(p[-1]-1)) p++;
2694 p = FFMIN(p, end) - 4;
2695 *state = AV_RB32(p);
2700 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
2702 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
2707 *size = sizeof(*props);
2709 props->vbv_delay = UINT64_MAX;
2714 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
2716 AVPacketSideData *tmp;
2717 AVCPBProperties *props;
2720 props = av_cpb_properties_alloc(&size);
2724 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
2730 avctx->coded_side_data = tmp;
2731 avctx->nb_coded_side_data++;
2733 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
2734 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
2735 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
2740 static void codec_parameters_reset(AVCodecParameters *par)
2742 av_freep(&par->extradata);
2744 memset(par, 0, sizeof(*par));
2746 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
2747 par->codec_id = AV_CODEC_ID_NONE;
2749 par->field_order = AV_FIELD_UNKNOWN;
2750 par->color_range = AVCOL_RANGE_UNSPECIFIED;
2751 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
2752 par->color_trc = AVCOL_TRC_UNSPECIFIED;
2753 par->color_space = AVCOL_SPC_UNSPECIFIED;
2754 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
2755 par->sample_aspect_ratio = (AVRational){ 0, 1 };
2758 AVCodecParameters *avcodec_parameters_alloc(void)
2760 AVCodecParameters *par = av_mallocz(sizeof(*par));
2764 codec_parameters_reset(par);
2768 void avcodec_parameters_free(AVCodecParameters **ppar)
2770 AVCodecParameters *par = *ppar;
2774 codec_parameters_reset(par);
2779 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
2781 codec_parameters_reset(dst);
2782 memcpy(dst, src, sizeof(*dst));
2784 dst->extradata = NULL;
2785 dst->extradata_size = 0;
2786 if (src->extradata) {
2787 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2788 if (!dst->extradata)
2789 return AVERROR(ENOMEM);
2790 memcpy(dst->extradata, src->extradata, src->extradata_size);
2791 dst->extradata_size = src->extradata_size;
2797 int avcodec_parameters_from_context(AVCodecParameters *par,
2798 const AVCodecContext *codec)
2800 codec_parameters_reset(par);
2802 par->codec_type = codec->codec_type;
2803 par->codec_id = codec->codec_id;
2804 par->codec_tag = codec->codec_tag;
2806 par->bit_rate = codec->bit_rate;
2807 par->bits_per_coded_sample = codec->bits_per_coded_sample;
2808 par->profile = codec->profile;
2809 par->level = codec->level;
2811 switch (par->codec_type) {
2812 case AVMEDIA_TYPE_VIDEO:
2813 par->format = codec->pix_fmt;
2814 par->width = codec->width;
2815 par->height = codec->height;
2816 par->field_order = codec->field_order;
2817 par->color_range = codec->color_range;
2818 par->color_primaries = codec->color_primaries;
2819 par->color_trc = codec->color_trc;
2820 par->color_space = codec->colorspace;
2821 par->chroma_location = codec->chroma_sample_location;
2822 par->sample_aspect_ratio = codec->sample_aspect_ratio;
2824 case AVMEDIA_TYPE_AUDIO:
2825 par->format = codec->sample_fmt;
2826 par->channel_layout = codec->channel_layout;
2827 par->channels = codec->channels;
2828 par->sample_rate = codec->sample_rate;
2829 par->block_align = codec->block_align;
2830 par->initial_padding = codec->initial_padding;
2834 if (codec->extradata) {
2835 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2836 if (!par->extradata)
2837 return AVERROR(ENOMEM);
2838 memcpy(par->extradata, codec->extradata, codec->extradata_size);
2839 par->extradata_size = codec->extradata_size;
2845 int avcodec_parameters_to_context(AVCodecContext *codec,
2846 const AVCodecParameters *par)
2848 codec->codec_type = par->codec_type;
2849 codec->codec_id = par->codec_id;
2850 codec->codec_tag = par->codec_tag;
2852 codec->bit_rate = par->bit_rate;
2853 codec->bits_per_coded_sample = par->bits_per_coded_sample;
2854 codec->profile = par->profile;
2855 codec->level = par->level;
2857 switch (par->codec_type) {
2858 case AVMEDIA_TYPE_VIDEO:
2859 codec->pix_fmt = par->format;
2860 codec->width = par->width;
2861 codec->height = par->height;
2862 codec->field_order = par->field_order;
2863 codec->color_range = par->color_range;
2864 codec->color_primaries = par->color_primaries;
2865 codec->color_trc = par->color_trc;
2866 codec->colorspace = par->color_space;
2867 codec->chroma_sample_location = par->chroma_location;
2868 codec->sample_aspect_ratio = par->sample_aspect_ratio;
2870 case AVMEDIA_TYPE_AUDIO:
2871 codec->sample_fmt = par->format;
2872 codec->channel_layout = par->channel_layout;
2873 codec->channels = par->channels;
2874 codec->sample_rate = par->sample_rate;
2875 codec->block_align = par->block_align;
2876 codec->initial_padding = par->initial_padding;
2880 if (par->extradata) {
2881 av_freep(&codec->extradata);
2882 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2883 if (!codec->extradata)
2884 return AVERROR(ENOMEM);
2885 memcpy(codec->extradata, par->extradata, par->extradata_size);
2886 codec->extradata_size = par->extradata_size;