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 (avctx->hw_frames_ctx)
517 return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
519 if ((ret = update_frame_pool(avctx, frame)) < 0)
522 switch (avctx->codec_type) {
523 case AVMEDIA_TYPE_VIDEO:
524 return video_get_buffer(avctx, frame);
525 case AVMEDIA_TYPE_AUDIO:
526 return audio_get_buffer(avctx, frame);
532 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
534 AVPacket *pkt = avctx->internal->pkt;
537 enum AVPacketSideDataType packet;
538 enum AVFrameSideDataType frame;
540 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
541 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
542 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
543 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
546 frame->color_primaries = avctx->color_primaries;
547 frame->color_trc = avctx->color_trc;
548 frame->colorspace = avctx->colorspace;
549 frame->color_range = avctx->color_range;
550 frame->chroma_location = avctx->chroma_sample_location;
552 frame->reordered_opaque = avctx->reordered_opaque;
554 frame->pkt_pts = AV_NOPTS_VALUE;
558 frame->pkt_pts = pkt->pts;
560 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
562 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
564 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
568 return AVERROR(ENOMEM);
570 memcpy(frame_sd->data, packet_sd, size);
577 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
579 const AVHWAccel *hwaccel = avctx->hwaccel;
580 int override_dimensions = 1;
583 switch (avctx->codec_type) {
584 case AVMEDIA_TYPE_VIDEO:
585 if (frame->width <= 0 || frame->height <= 0) {
586 frame->width = FFMAX(avctx->width, avctx->coded_width);
587 frame->height = FFMAX(avctx->height, avctx->coded_height);
588 override_dimensions = 0;
590 if (frame->format < 0)
591 frame->format = avctx->pix_fmt;
592 if (!frame->sample_aspect_ratio.num)
593 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
595 if (av_image_check_sar(frame->width, frame->height,
596 frame->sample_aspect_ratio) < 0) {
597 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
598 frame->sample_aspect_ratio.num,
599 frame->sample_aspect_ratio.den);
600 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
603 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
606 case AVMEDIA_TYPE_AUDIO:
607 if (!frame->sample_rate)
608 frame->sample_rate = avctx->sample_rate;
609 if (frame->format < 0)
610 frame->format = avctx->sample_fmt;
611 if (!frame->channel_layout) {
612 if (avctx->channel_layout) {
613 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
615 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
617 return AVERROR(EINVAL);
620 frame->channel_layout = avctx->channel_layout;
622 if (avctx->channels > FF_SANE_NB_CHANNELS) {
623 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
625 return AVERROR(ENOSYS);
628 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
629 if (!frame->channel_layout)
630 frame->channel_layout = (1ULL << avctx->channels) - 1;
634 default: return AVERROR(EINVAL);
637 ret = ff_decode_frame_props(avctx, frame);
642 if (hwaccel->alloc_frame) {
643 ret = hwaccel->alloc_frame(avctx, frame);
647 avctx->sw_pix_fmt = avctx->pix_fmt;
649 ret = avctx->get_buffer2(avctx, frame, flags);
652 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
653 frame->width = avctx->width;
654 frame->height = avctx->height;
660 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
665 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
668 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
670 if (av_frame_is_writable(frame))
671 return ff_decode_frame_props(avctx, frame);
673 tmp = av_frame_alloc();
675 return AVERROR(ENOMEM);
677 av_frame_move_ref(tmp, frame);
679 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
685 av_frame_copy(frame, tmp);
691 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
695 for (i = 0; i < count; i++) {
696 int r = func(c, (char *)arg + i * size);
703 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
707 for (i = 0; i < count; i++) {
708 int r = func(c, arg, i, 0);
715 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
717 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
718 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
721 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
723 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
728 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
729 enum AVPixelFormat pix_fmt)
731 AVHWAccel *hwaccel = NULL;
733 while ((hwaccel = av_hwaccel_next(hwaccel)))
734 if (hwaccel->id == codec_id
735 && hwaccel->pix_fmt == pix_fmt)
740 static int setup_hwaccel(AVCodecContext *avctx,
741 const enum AVPixelFormat fmt,
744 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
748 av_log(avctx, AV_LOG_ERROR,
749 "Could not find an AVHWAccel for the pixel format: %s",
751 return AVERROR(ENOENT);
754 if (hwa->priv_data_size) {
755 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
756 if (!avctx->internal->hwaccel_priv_data)
757 return AVERROR(ENOMEM);
761 ret = hwa->init(avctx);
763 av_freep(&avctx->internal->hwaccel_priv_data);
768 avctx->hwaccel = hwa;
773 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
775 const AVPixFmtDescriptor *desc;
776 enum AVPixelFormat *choices;
777 enum AVPixelFormat ret;
780 while (fmt[n] != AV_PIX_FMT_NONE)
784 avctx->sw_pix_fmt = fmt[n - 1];
785 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
787 choices = av_malloc_array(n + 1, sizeof(*choices));
789 return AV_PIX_FMT_NONE;
791 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
794 if (avctx->hwaccel && avctx->hwaccel->uninit)
795 avctx->hwaccel->uninit(avctx);
796 av_freep(&avctx->internal->hwaccel_priv_data);
797 avctx->hwaccel = NULL;
799 av_buffer_unref(&avctx->hw_frames_ctx);
801 ret = avctx->get_format(avctx, choices);
803 desc = av_pix_fmt_desc_get(ret);
805 ret = AV_PIX_FMT_NONE;
809 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
812 if (avctx->hw_frames_ctx) {
813 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
814 if (hw_frames_ctx->format != ret) {
815 av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
816 "does not match the format of provided AVHWFramesContext\n");
817 ret = AV_PIX_FMT_NONE;
822 if (!setup_hwaccel(avctx, ret, desc->name))
825 /* Remove failed hwaccel from choices */
826 for (n = 0; choices[n] != ret; n++)
827 av_assert0(choices[n] != AV_PIX_FMT_NONE);
830 choices[n] = choices[n + 1];
831 while (choices[n++] != AV_PIX_FMT_NONE);
838 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
841 AVDictionary *tmp = NULL;
843 if (avcodec_is_open(avctx))
846 if ((!codec && !avctx->codec)) {
847 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
848 return AVERROR(EINVAL);
850 if ((codec && avctx->codec && codec != avctx->codec)) {
851 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
852 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
853 return AVERROR(EINVAL);
856 codec = avctx->codec;
858 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
859 return AVERROR(EINVAL);
862 av_dict_copy(&tmp, *options, 0);
864 /* If there is a user-supplied mutex locking routine, call it. */
865 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
867 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
871 entangled_thread_counter++;
872 if (entangled_thread_counter != 1) {
873 av_log(avctx, AV_LOG_ERROR,
874 "Insufficient thread locking. At least %d threads are "
875 "calling avcodec_open2() at the same time right now.\n",
876 entangled_thread_counter);
882 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
883 if (!avctx->internal) {
884 ret = AVERROR(ENOMEM);
888 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
889 if (!avctx->internal->pool) {
890 ret = AVERROR(ENOMEM);
894 avctx->internal->to_free = av_frame_alloc();
895 if (!avctx->internal->to_free) {
896 ret = AVERROR(ENOMEM);
900 avctx->internal->buffer_frame = av_frame_alloc();
901 if (!avctx->internal->buffer_frame) {
902 ret = AVERROR(ENOMEM);
906 avctx->internal->buffer_pkt = av_packet_alloc();
907 if (!avctx->internal->buffer_pkt) {
908 ret = AVERROR(ENOMEM);
912 if (codec->priv_data_size > 0) {
913 if (!avctx->priv_data) {
914 avctx->priv_data = av_mallocz(codec->priv_data_size);
915 if (!avctx->priv_data) {
916 ret = AVERROR(ENOMEM);
919 if (codec->priv_class) {
920 *(const AVClass **)avctx->priv_data = codec->priv_class;
921 av_opt_set_defaults(avctx->priv_data);
924 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
927 avctx->priv_data = NULL;
929 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
932 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
933 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
934 else if (avctx->width && avctx->height)
935 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
939 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
940 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
941 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
942 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
943 ff_set_dimensions(avctx, 0, 0);
946 if (avctx->width > 0 && avctx->height > 0) {
947 if (av_image_check_sar(avctx->width, avctx->height,
948 avctx->sample_aspect_ratio) < 0) {
949 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
950 avctx->sample_aspect_ratio.num,
951 avctx->sample_aspect_ratio.den);
952 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
956 /* if the decoder init function was already called previously,
957 * free the already allocated subtitle_header before overwriting it */
958 if (av_codec_is_decoder(codec))
959 av_freep(&avctx->subtitle_header);
961 if (avctx->channels > FF_SANE_NB_CHANNELS) {
962 ret = AVERROR(EINVAL);
966 avctx->codec = codec;
967 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
968 avctx->codec_id == AV_CODEC_ID_NONE) {
969 avctx->codec_type = codec->type;
970 avctx->codec_id = codec->id;
972 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
973 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
974 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
975 ret = AVERROR(EINVAL);
978 avctx->frame_number = 0;
980 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
981 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
982 ret = AVERROR_EXPERIMENTAL;
986 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
987 (!avctx->time_base.num || !avctx->time_base.den)) {
988 avctx->time_base.num = 1;
989 avctx->time_base.den = avctx->sample_rate;
993 ret = ff_thread_init(avctx);
998 if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
999 avctx->thread_count = 1;
1001 if (av_codec_is_encoder(avctx->codec)) {
1003 #if FF_API_CODED_FRAME
1004 FF_DISABLE_DEPRECATION_WARNINGS
1005 avctx->coded_frame = av_frame_alloc();
1006 if (!avctx->coded_frame) {
1007 ret = AVERROR(ENOMEM);
1010 FF_ENABLE_DEPRECATION_WARNINGS
1013 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
1014 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
1015 ret = AVERROR(EINVAL);
1019 if (avctx->codec->sample_fmts) {
1020 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1021 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1023 if (avctx->channels == 1 &&
1024 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1025 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1026 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1030 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1031 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1032 ret = AVERROR(EINVAL);
1036 if (avctx->codec->pix_fmts) {
1037 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1038 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1040 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1041 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1042 ret = AVERROR(EINVAL);
1045 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1046 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1047 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1048 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1049 avctx->color_range = AVCOL_RANGE_JPEG;
1051 if (avctx->codec->supported_samplerates) {
1052 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1053 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1055 if (avctx->codec->supported_samplerates[i] == 0) {
1056 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1057 ret = AVERROR(EINVAL);
1061 if (avctx->codec->channel_layouts) {
1062 if (!avctx->channel_layout) {
1063 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1065 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1066 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1068 if (avctx->codec->channel_layouts[i] == 0) {
1069 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1070 ret = AVERROR(EINVAL);
1075 if (avctx->channel_layout && avctx->channels) {
1076 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1077 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1078 ret = AVERROR(EINVAL);
1081 } else if (avctx->channel_layout) {
1082 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1085 if (!avctx->rc_initial_buffer_occupancy)
1086 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1088 if (avctx->ticks_per_frame &&
1089 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
1090 av_log(avctx, AV_LOG_ERROR,
1091 "ticks_per_frame %d too large for the timebase %d/%d.",
1092 avctx->ticks_per_frame,
1093 avctx->time_base.num,
1094 avctx->time_base.den);
1098 if (avctx->hw_frames_ctx) {
1099 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1100 if (frames_ctx->format != avctx->pix_fmt) {
1101 av_log(avctx, AV_LOG_ERROR,
1102 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
1103 ret = AVERROR(EINVAL);
1109 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1110 ret = avctx->codec->init(avctx);
1116 #if FF_API_AUDIOENC_DELAY
1117 if (av_codec_is_encoder(avctx->codec))
1118 avctx->delay = avctx->initial_padding;
1121 if (av_codec_is_decoder(avctx->codec)) {
1122 /* validate channel layout from the decoder */
1123 if (avctx->channel_layout) {
1124 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1125 if (!avctx->channels)
1126 avctx->channels = channels;
1127 else if (channels != avctx->channels) {
1128 av_log(avctx, AV_LOG_WARNING,
1129 "channel layout does not match number of channels\n");
1130 avctx->channel_layout = 0;
1133 if (avctx->channels && avctx->channels < 0 ||
1134 avctx->channels > FF_SANE_NB_CHANNELS) {
1135 ret = AVERROR(EINVAL);
1139 #if FF_API_AVCTX_TIMEBASE
1140 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1141 avctx->time_base = av_inv_q(avctx->framerate);
1145 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
1146 entangled_thread_counter--;
1148 /* Release any user-supplied mutex. */
1150 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1155 av_dict_free(options);
1162 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1163 avctx->codec->close(avctx);
1165 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1166 av_opt_free(avctx->priv_data);
1169 #if FF_API_CODED_FRAME
1170 FF_DISABLE_DEPRECATION_WARNINGS
1171 av_frame_free(&avctx->coded_frame);
1172 FF_ENABLE_DEPRECATION_WARNINGS
1176 av_freep(&avctx->priv_data);
1177 if (avctx->internal) {
1178 av_frame_free(&avctx->internal->to_free);
1179 av_freep(&avctx->internal->pool);
1181 av_freep(&avctx->internal);
1182 avctx->codec = NULL;
1186 int ff_alloc_packet(AVPacket *avpkt, int size)
1188 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
1189 return AVERROR(EINVAL);
1192 AVBufferRef *buf = avpkt->buf;
1194 if (avpkt->size < size)
1195 return AVERROR(EINVAL);
1197 av_init_packet(avpkt);
1202 return av_new_packet(avpkt, size);
1207 * Pad last frame with silence.
1209 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1211 AVFrame *frame = NULL;
1214 if (!(frame = av_frame_alloc()))
1215 return AVERROR(ENOMEM);
1217 frame->format = src->format;
1218 frame->channel_layout = src->channel_layout;
1219 frame->nb_samples = s->frame_size;
1220 ret = av_frame_get_buffer(frame, 32);
1224 ret = av_frame_copy_props(frame, src);
1228 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1229 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1231 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1232 frame->nb_samples - src->nb_samples,
1233 s->channels, s->sample_fmt)) < 0)
1241 av_frame_free(&frame);
1245 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1247 const AVFrame *frame,
1248 int *got_packet_ptr)
1251 AVFrame *padded_frame = NULL;
1253 int user_packet = !!avpkt->data;
1255 *got_packet_ptr = 0;
1257 if (!avctx->codec->encode2) {
1258 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
1259 return AVERROR(ENOSYS);
1262 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1263 av_packet_unref(avpkt);
1264 av_init_packet(avpkt);
1268 /* ensure that extended_data is properly set */
1269 if (frame && !frame->extended_data) {
1270 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1271 avctx->channels > AV_NUM_DATA_POINTERS) {
1272 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1273 "with more than %d channels, but extended_data is not set.\n",
1274 AV_NUM_DATA_POINTERS);
1275 return AVERROR(EINVAL);
1277 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1280 tmp.extended_data = tmp.data;
1284 /* extract audio service type metadata */
1286 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
1287 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1288 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1291 /* check for valid frame size */
1293 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
1294 if (frame->nb_samples > avctx->frame_size)
1295 return AVERROR(EINVAL);
1296 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1297 if (frame->nb_samples < avctx->frame_size &&
1298 !avctx->internal->last_audio_frame) {
1299 ret = pad_last_frame(avctx, &padded_frame, frame);
1303 frame = padded_frame;
1304 avctx->internal->last_audio_frame = 1;
1307 if (frame->nb_samples != avctx->frame_size) {
1308 ret = AVERROR(EINVAL);
1314 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1316 if (*got_packet_ptr) {
1317 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
1318 if (avpkt->pts == AV_NOPTS_VALUE)
1319 avpkt->pts = frame->pts;
1320 if (!avpkt->duration)
1321 avpkt->duration = ff_samples_to_time_base(avctx,
1324 avpkt->dts = avpkt->pts;
1329 if (!user_packet && avpkt->size) {
1330 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1332 avpkt->data = avpkt->buf->data;
1335 avctx->frame_number++;
1338 if (ret < 0 || !*got_packet_ptr) {
1339 av_packet_unref(avpkt);
1340 av_init_packet(avpkt);
1344 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1345 * this needs to be moved to the encoders, but for now we can do it
1346 * here to simplify things */
1347 avpkt->flags |= AV_PKT_FLAG_KEY;
1350 av_frame_free(&padded_frame);
1352 #if FF_API_AUDIOENC_DELAY
1353 avctx->delay = avctx->initial_padding;
1359 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1361 const AVFrame *frame,
1362 int *got_packet_ptr)
1365 int user_packet = !!avpkt->data;
1367 *got_packet_ptr = 0;
1369 if (!avctx->codec->encode2) {
1370 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
1371 return AVERROR(ENOSYS);
1374 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1375 av_packet_unref(avpkt);
1376 av_init_packet(avpkt);
1381 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1382 return AVERROR(EINVAL);
1384 av_assert0(avctx->codec->encode2);
1386 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1388 if (!*got_packet_ptr)
1390 else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1391 avpkt->pts = avpkt->dts = frame->pts;
1393 if (!user_packet && avpkt->size) {
1394 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1396 avpkt->data = avpkt->buf->data;
1399 avctx->frame_number++;
1402 if (ret < 0 || !*got_packet_ptr)
1403 av_packet_unref(avpkt);
1409 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1410 const AVSubtitle *sub)
1413 if (sub->start_display_time) {
1414 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1417 if (sub->num_rects == 0 || !sub->rects)
1419 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1420 avctx->frame_number++;
1424 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1427 const uint8_t *data;
1430 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1434 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
1435 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1436 "changes, but PARAM_CHANGE side data was sent to it.\n");
1437 ret = AVERROR(EINVAL);
1444 flags = bytestream_get_le32(&data);
1447 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1450 avctx->channels = bytestream_get_le32(&data);
1453 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1456 avctx->channel_layout = bytestream_get_le64(&data);
1459 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1462 avctx->sample_rate = bytestream_get_le32(&data);
1465 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1468 avctx->width = bytestream_get_le32(&data);
1469 avctx->height = bytestream_get_le32(&data);
1471 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1478 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1479 ret = AVERROR_INVALIDDATA;
1482 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1483 if (avctx->err_recognition & AV_EF_EXPLODE)
1489 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1493 /* move the original frame to our backup */
1494 av_frame_unref(avci->to_free);
1495 av_frame_move_ref(avci->to_free, frame);
1497 /* now copy everything except the AVBufferRefs back
1498 * note that we make a COPY of the side data, so calling av_frame_free() on
1499 * the caller's frame will work properly */
1500 ret = av_frame_copy_props(frame, avci->to_free);
1504 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1505 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1506 if (avci->to_free->extended_data != avci->to_free->data) {
1507 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1508 int size = planes * sizeof(*frame->extended_data);
1511 av_frame_unref(frame);
1515 frame->extended_data = av_malloc(size);
1516 if (!frame->extended_data) {
1517 av_frame_unref(frame);
1518 return AVERROR(ENOMEM);
1520 memcpy(frame->extended_data, avci->to_free->extended_data,
1523 frame->extended_data = frame->data;
1525 frame->format = avci->to_free->format;
1526 frame->width = avci->to_free->width;
1527 frame->height = avci->to_free->height;
1528 frame->channel_layout = avci->to_free->channel_layout;
1529 frame->nb_samples = avci->to_free->nb_samples;
1534 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1535 int *got_picture_ptr,
1538 AVCodecInternal *avci = avctx->internal;
1541 *got_picture_ptr = 0;
1542 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1545 if (!avctx->codec->decode) {
1546 av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
1547 return AVERROR(ENOSYS);
1550 avctx->internal->pkt = avpkt;
1551 ret = apply_param_change(avctx, avpkt);
1555 av_frame_unref(picture);
1557 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
1558 (avctx->active_thread_type & FF_THREAD_FRAME)) {
1559 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1560 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1563 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1565 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
1566 picture->pkt_dts = avpkt->dts;
1567 /* get_buffer is supposed to set frame parameters */
1568 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
1569 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1570 picture->width = avctx->width;
1571 picture->height = avctx->height;
1572 picture->format = avctx->pix_fmt;
1576 emms_c(); //needed to avoid an emms_c() call before every return;
1578 if (*got_picture_ptr) {
1579 if (!avctx->refcounted_frames) {
1580 int err = unrefcount_frame(avci, picture);
1585 avctx->frame_number++;
1587 av_frame_unref(picture);
1591 #if FF_API_AVCTX_TIMEBASE
1592 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1593 avctx->time_base = av_inv_q(avctx->framerate);
1599 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1604 AVCodecInternal *avci = avctx->internal;
1609 if (!avctx->codec->decode) {
1610 av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
1611 return AVERROR(ENOSYS);
1614 avctx->internal->pkt = avpkt;
1616 if (!avpkt->data && avpkt->size) {
1617 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1618 return AVERROR(EINVAL);
1621 ret = apply_param_change(avctx, avpkt);
1625 av_frame_unref(frame);
1627 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
1628 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1629 if (ret >= 0 && *got_frame_ptr) {
1630 avctx->frame_number++;
1631 frame->pkt_dts = avpkt->dts;
1632 if (frame->format == AV_SAMPLE_FMT_NONE)
1633 frame->format = avctx->sample_fmt;
1635 if (!avctx->refcounted_frames) {
1636 int err = unrefcount_frame(avci, frame);
1641 av_frame_unref(frame);
1648 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1654 avctx->internal->pkt = avpkt;
1656 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1658 avctx->frame_number++;
1662 void avsubtitle_free(AVSubtitle *sub)
1666 for (i = 0; i < sub->num_rects; i++) {
1667 av_freep(&sub->rects[i]->data[0]);
1668 av_freep(&sub->rects[i]->data[1]);
1669 av_freep(&sub->rects[i]->data[2]);
1670 av_freep(&sub->rects[i]->data[3]);
1671 av_freep(&sub->rects[i]->text);
1672 av_freep(&sub->rects[i]->ass);
1673 av_freep(&sub->rects[i]);
1676 av_freep(&sub->rects);
1678 memset(sub, 0, sizeof(AVSubtitle));
1681 static int do_decode(AVCodecContext *avctx, AVPacket *pkt)
1686 av_assert0(!avctx->internal->buffer_frame->buf[0]);
1689 pkt = avctx->internal->buffer_pkt;
1691 // This is the lesser evil. The field is for compatibility with legacy users
1692 // of the legacy API, and users using the new API should not be forced to
1693 // even know about this field.
1694 avctx->refcounted_frames = 1;
1696 // Some codecs (at least wma lossless) will crash when feeding drain packets
1697 // after EOF was signaled.
1698 if (avctx->internal->draining_done)
1701 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1702 ret = avcodec_decode_video2(avctx, avctx->internal->buffer_frame,
1706 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1707 ret = avcodec_decode_audio4(avctx, avctx->internal->buffer_frame,
1710 ret = AVERROR(EINVAL);
1716 if (avctx->internal->draining && !got_frame)
1717 avctx->internal->draining_done = 1;
1719 if (ret >= pkt->size) {
1720 av_packet_unref(avctx->internal->buffer_pkt);
1724 if (pkt != avctx->internal->buffer_pkt) {
1725 av_packet_unref(avctx->internal->buffer_pkt);
1726 if ((ret = av_packet_ref(avctx->internal->buffer_pkt, pkt)) < 0)
1730 avctx->internal->buffer_pkt->data += consumed;
1731 avctx->internal->buffer_pkt->size -= consumed;
1732 avctx->internal->buffer_pkt->pts = AV_NOPTS_VALUE;
1733 avctx->internal->buffer_pkt->dts = AV_NOPTS_VALUE;
1737 av_assert0(avctx->internal->buffer_frame->buf[0]);
1742 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
1746 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
1747 return AVERROR(EINVAL);
1749 if (avctx->internal->draining)
1752 if (!avpkt || !avpkt->size) {
1753 avctx->internal->draining = 1;
1756 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1760 if (avctx->codec->send_packet) {
1762 ret = apply_param_change(avctx, (AVPacket *)avpkt);
1766 return avctx->codec->send_packet(avctx, avpkt);
1769 // Emulation via old API. Assume avpkt is likely not refcounted, while
1770 // decoder output is always refcounted, and avoid copying.
1772 if (avctx->internal->buffer_pkt->size || avctx->internal->buffer_frame->buf[0])
1773 return AVERROR(EAGAIN);
1775 // The goal is decoding the first frame of the packet without using memcpy,
1776 // because the common case is having only 1 frame per packet (especially
1777 // with video, but audio too). In other cases, it can't be avoided, unless
1778 // the user is feeding refcounted packets.
1779 return do_decode(avctx, (AVPacket *)avpkt);
1782 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1786 av_frame_unref(frame);
1788 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
1789 return AVERROR(EINVAL);
1791 if (avctx->codec->receive_frame) {
1792 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1794 return avctx->codec->receive_frame(avctx, frame);
1797 // Emulation via old API.
1799 if (!avctx->internal->buffer_frame->buf[0]) {
1800 if (!avctx->internal->buffer_pkt->size && !avctx->internal->draining)
1801 return AVERROR(EAGAIN);
1804 if ((ret = do_decode(avctx, avctx->internal->buffer_pkt)) < 0) {
1805 av_packet_unref(avctx->internal->buffer_pkt);
1808 // Some audio decoders may consume partial data without returning
1809 // a frame (fate-wmapro-2ch). There is no way to make the caller
1810 // call avcodec_receive_frame() again without returning a frame,
1811 // so try to decode more in these cases.
1812 if (avctx->internal->buffer_frame->buf[0] ||
1813 !avctx->internal->buffer_pkt->size)
1818 if (!avctx->internal->buffer_frame->buf[0])
1819 return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
1821 av_frame_move_ref(frame, avctx->internal->buffer_frame);
1825 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
1830 av_packet_unref(avctx->internal->buffer_pkt);
1831 avctx->internal->buffer_pkt_valid = 0;
1833 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1834 ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
1836 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1837 ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
1840 ret = AVERROR(EINVAL);
1843 if (ret >= 0 && *got_packet) {
1844 // Encoders must always return ref-counted buffers.
1845 // Side-data only packets have no data and can be not ref-counted.
1846 av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
1847 avctx->internal->buffer_pkt_valid = 1;
1850 av_packet_unref(avctx->internal->buffer_pkt);
1856 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
1858 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
1859 return AVERROR(EINVAL);
1861 if (avctx->internal->draining)
1865 avctx->internal->draining = 1;
1867 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1871 if (avctx->codec->send_frame)
1872 return avctx->codec->send_frame(avctx, frame);
1874 // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
1875 // 1. if the AVFrame is not refcounted, the copying will be much more
1876 // expensive than copying the packet data
1877 // 2. assume few users use non-refcounted AVPackets, so usually no copy is
1880 if (avctx->internal->buffer_pkt_valid)
1881 return AVERROR(EAGAIN);
1883 return do_encode(avctx, frame, &(int){0});
1886 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
1888 av_packet_unref(avpkt);
1890 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
1891 return AVERROR(EINVAL);
1893 if (avctx->codec->receive_packet) {
1894 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1896 return avctx->codec->receive_packet(avctx, avpkt);
1899 // Emulation via old API.
1901 if (!avctx->internal->buffer_pkt_valid) {
1904 if (!avctx->internal->draining)
1905 return AVERROR(EAGAIN);
1906 ret = do_encode(avctx, NULL, &got_packet);
1909 if (ret >= 0 && !got_packet)
1913 av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
1914 avctx->internal->buffer_pkt_valid = 0;
1918 av_cold int avcodec_close(AVCodecContext *avctx)
1922 if (avcodec_is_open(avctx)) {
1923 FramePool *pool = avctx->internal->pool;
1925 if (HAVE_THREADS && avctx->internal->thread_ctx)
1926 ff_thread_free(avctx);
1927 if (avctx->codec && avctx->codec->close)
1928 avctx->codec->close(avctx);
1929 av_frame_free(&avctx->internal->to_free);
1930 av_frame_free(&avctx->internal->buffer_frame);
1931 av_packet_free(&avctx->internal->buffer_pkt);
1932 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1933 av_buffer_pool_uninit(&pool->pools[i]);
1934 av_freep(&avctx->internal->pool);
1936 if (avctx->hwaccel && avctx->hwaccel->uninit)
1937 avctx->hwaccel->uninit(avctx);
1938 av_freep(&avctx->internal->hwaccel_priv_data);
1940 av_freep(&avctx->internal);
1943 for (i = 0; i < avctx->nb_coded_side_data; i++)
1944 av_freep(&avctx->coded_side_data[i].data);
1945 av_freep(&avctx->coded_side_data);
1946 avctx->nb_coded_side_data = 0;
1948 av_buffer_unref(&avctx->hw_frames_ctx);
1950 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1951 av_opt_free(avctx->priv_data);
1953 av_freep(&avctx->priv_data);
1954 if (av_codec_is_encoder(avctx->codec)) {
1955 av_freep(&avctx->extradata);
1956 #if FF_API_CODED_FRAME
1957 FF_DISABLE_DEPRECATION_WARNINGS
1958 av_frame_free(&avctx->coded_frame);
1959 FF_ENABLE_DEPRECATION_WARNINGS
1962 avctx->codec = NULL;
1963 avctx->active_thread_type = 0;
1968 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1970 AVCodec *p, *experimental = NULL;
1973 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1975 if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
1982 return experimental;
1985 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1987 return find_encdec(id, 1);
1990 AVCodec *avcodec_find_encoder_by_name(const char *name)
1997 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2004 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2006 return find_encdec(id, 0);
2009 AVCodec *avcodec_find_decoder_by_name(const char *name)
2016 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2023 static int get_bit_rate(AVCodecContext *ctx)
2026 int bits_per_sample;
2028 switch (ctx->codec_type) {
2029 case AVMEDIA_TYPE_VIDEO:
2030 case AVMEDIA_TYPE_DATA:
2031 case AVMEDIA_TYPE_SUBTITLE:
2032 case AVMEDIA_TYPE_ATTACHMENT:
2033 bit_rate = ctx->bit_rate;
2035 case AVMEDIA_TYPE_AUDIO:
2036 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
2037 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
2046 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2048 int i, len, ret = 0;
2050 #define TAG_PRINT(x) \
2051 (((x) >= '0' && (x) <= '9') || \
2052 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2053 ((x) == '.' || (x) == ' '))
2055 for (i = 0; i < 4; i++) {
2056 len = snprintf(buf, buf_size,
2057 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2059 buf_size = buf_size > len ? buf_size - len : 0;
2066 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2068 const char *codec_name;
2069 const char *profile = NULL;
2073 AVRational display_aspect_ratio;
2074 const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
2077 codec_name = desc->name;
2078 profile = avcodec_profile_name(enc->codec_id, enc->profile);
2079 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
2080 /* fake mpeg2 transport stream codec (currently not
2082 codec_name = "mpeg2ts";
2084 /* output avi tags */
2086 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2087 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
2091 switch (enc->codec_type) {
2092 case AVMEDIA_TYPE_VIDEO:
2093 snprintf(buf, buf_size,
2095 codec_name, enc->mb_decision ? " (hq)" : "");
2097 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2099 if (enc->codec_tag) {
2101 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2102 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2103 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2106 av_strlcat(buf, "\n ", buf_size);
2107 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2108 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
2109 av_get_pix_fmt_name(enc->pix_fmt));
2111 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2112 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
2113 av_color_range_name(enc->color_range));
2114 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
2115 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
2116 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
2118 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
2119 av_color_space_name(enc->colorspace),
2120 av_color_primaries_name(enc->color_primaries),
2121 av_color_transfer_name(enc->color_trc));
2123 if (av_log_get_level() >= AV_LOG_DEBUG &&
2124 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
2125 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
2126 av_chroma_location_name(enc->chroma_sample_location));
2129 av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
2131 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2133 enc->width, enc->height);
2135 if (av_log_get_level() >= AV_LOG_VERBOSE &&
2136 (enc->width != enc->coded_width ||
2137 enc->height != enc->coded_height))
2138 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2139 " (%dx%d)", enc->coded_width, enc->coded_height);
2141 if (enc->sample_aspect_ratio.num) {
2142 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2143 enc->width * enc->sample_aspect_ratio.num,
2144 enc->height * enc->sample_aspect_ratio.den,
2146 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2147 " [PAR %d:%d DAR %d:%d]",
2148 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2149 display_aspect_ratio.num, display_aspect_ratio.den);
2151 if (av_log_get_level() >= AV_LOG_DEBUG) {
2152 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2153 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2155 enc->time_base.num / g, enc->time_base.den / g);
2159 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2160 ", q=%d-%d", enc->qmin, enc->qmax);
2163 case AVMEDIA_TYPE_AUDIO:
2164 snprintf(buf, buf_size,
2168 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2170 if (enc->codec_tag) {
2172 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2173 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2174 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2177 av_strlcat(buf, "\n ", buf_size);
2178 if (enc->sample_rate) {
2179 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2180 "%d Hz, ", enc->sample_rate);
2182 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2183 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2184 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2185 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2188 case AVMEDIA_TYPE_DATA:
2189 snprintf(buf, buf_size, "Data: %s", codec_name);
2191 case AVMEDIA_TYPE_SUBTITLE:
2192 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
2194 case AVMEDIA_TYPE_ATTACHMENT:
2195 snprintf(buf, buf_size, "Attachment: %s", codec_name);
2198 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
2202 if (enc->flags & AV_CODEC_FLAG_PASS1)
2203 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2205 if (enc->flags & AV_CODEC_FLAG_PASS2)
2206 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2209 bitrate = get_bit_rate(enc);
2211 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2212 ", %d kb/s", bitrate / 1000);
2216 const char *av_get_profile_name(const AVCodec *codec, int profile)
2219 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2222 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2223 if (p->profile == profile)
2229 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
2231 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
2234 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
2237 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2238 if (p->profile == profile)
2244 unsigned avcodec_version(void)
2246 return LIBAVCODEC_VERSION_INT;
2249 const char *avcodec_configuration(void)
2251 return LIBAV_CONFIGURATION;
2254 const char *avcodec_license(void)
2256 #define LICENSE_PREFIX "libavcodec license: "
2257 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2260 void avcodec_flush_buffers(AVCodecContext *avctx)
2262 avctx->internal->draining = 0;
2263 avctx->internal->draining_done = 0;
2264 av_frame_unref(avctx->internal->buffer_frame);
2265 av_packet_unref(avctx->internal->buffer_pkt);
2266 avctx->internal->buffer_pkt_valid = 0;
2268 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2269 ff_thread_flush(avctx);
2270 else if (avctx->codec->flush)
2271 avctx->codec->flush(avctx);
2273 if (!avctx->refcounted_frames)
2274 av_frame_unref(avctx->internal->to_free);
2277 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2280 case AV_CODEC_ID_ADPCM_CT:
2281 case AV_CODEC_ID_ADPCM_IMA_APC:
2282 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2283 case AV_CODEC_ID_ADPCM_IMA_WS:
2284 case AV_CODEC_ID_ADPCM_G722:
2285 case AV_CODEC_ID_ADPCM_YAMAHA:
2287 case AV_CODEC_ID_PCM_ALAW:
2288 case AV_CODEC_ID_PCM_MULAW:
2289 case AV_CODEC_ID_PCM_S8:
2290 case AV_CODEC_ID_PCM_U8:
2291 case AV_CODEC_ID_PCM_ZORK:
2293 case AV_CODEC_ID_PCM_S16BE:
2294 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2295 case AV_CODEC_ID_PCM_S16LE:
2296 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2297 case AV_CODEC_ID_PCM_U16BE:
2298 case AV_CODEC_ID_PCM_U16LE:
2300 case AV_CODEC_ID_PCM_S24DAUD:
2301 case AV_CODEC_ID_PCM_S24BE:
2302 case AV_CODEC_ID_PCM_S24LE:
2303 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2304 case AV_CODEC_ID_PCM_U24BE:
2305 case AV_CODEC_ID_PCM_U24LE:
2307 case AV_CODEC_ID_PCM_S32BE:
2308 case AV_CODEC_ID_PCM_S32LE:
2309 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2310 case AV_CODEC_ID_PCM_U32BE:
2311 case AV_CODEC_ID_PCM_U32LE:
2312 case AV_CODEC_ID_PCM_F32BE:
2313 case AV_CODEC_ID_PCM_F32LE:
2315 case AV_CODEC_ID_PCM_F64BE:
2316 case AV_CODEC_ID_PCM_F64LE:
2323 int av_get_bits_per_sample(enum AVCodecID codec_id)
2326 case AV_CODEC_ID_ADPCM_SBPRO_2:
2328 case AV_CODEC_ID_ADPCM_SBPRO_3:
2330 case AV_CODEC_ID_ADPCM_SBPRO_4:
2331 case AV_CODEC_ID_ADPCM_IMA_WAV:
2332 case AV_CODEC_ID_ADPCM_IMA_QT:
2333 case AV_CODEC_ID_ADPCM_SWF:
2334 case AV_CODEC_ID_ADPCM_MS:
2337 return av_get_exact_bits_per_sample(codec_id);
2341 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
2342 uint32_t tag, int bits_per_coded_sample, int frame_bytes)
2344 int bps = av_get_exact_bits_per_sample(id);
2346 /* codecs with an exact constant bits per sample */
2347 if (bps > 0 && ch > 0 && frame_bytes > 0)
2348 return (frame_bytes * 8) / (bps * ch);
2349 bps = bits_per_coded_sample;
2351 /* codecs with a fixed packet duration */
2353 case AV_CODEC_ID_ADPCM_ADX: return 32;
2354 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2355 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2356 case AV_CODEC_ID_AMR_NB:
2357 case AV_CODEC_ID_GSM:
2358 case AV_CODEC_ID_QCELP:
2359 case AV_CODEC_ID_RA_144:
2360 case AV_CODEC_ID_RA_288: return 160;
2361 case AV_CODEC_ID_IMC: return 256;
2362 case AV_CODEC_ID_AMR_WB:
2363 case AV_CODEC_ID_GSM_MS: return 320;
2364 case AV_CODEC_ID_MP1: return 384;
2365 case AV_CODEC_ID_ATRAC1: return 512;
2366 case AV_CODEC_ID_ATRAC3: return 1024;
2367 case AV_CODEC_ID_MP2:
2368 case AV_CODEC_ID_MUSEPACK7: return 1152;
2369 case AV_CODEC_ID_AC3: return 1536;
2373 /* calc from sample rate */
2374 if (id == AV_CODEC_ID_TTA)
2375 return 256 * sr / 245;
2378 /* calc from sample rate and channels */
2379 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2380 return (480 << (sr / 22050)) / ch;
2385 /* calc from block_align */
2386 if (id == AV_CODEC_ID_SIPR) {
2388 case 20: return 160;
2389 case 19: return 144;
2390 case 29: return 288;
2391 case 37: return 480;
2393 } else if (id == AV_CODEC_ID_ILBC) {
2395 case 38: return 160;
2396 case 50: return 240;
2401 if (frame_bytes > 0) {
2402 /* calc from frame_bytes only */
2403 if (id == AV_CODEC_ID_TRUESPEECH)
2404 return 240 * (frame_bytes / 32);
2405 if (id == AV_CODEC_ID_NELLYMOSER)
2406 return 256 * (frame_bytes / 64);
2409 /* calc from frame_bytes and bits_per_coded_sample */
2410 if (id == AV_CODEC_ID_ADPCM_G726)
2411 return frame_bytes * 8 / bps;
2415 /* calc from frame_bytes and channels */
2417 case AV_CODEC_ID_ADPCM_4XM:
2418 case AV_CODEC_ID_ADPCM_IMA_ISS:
2419 return (frame_bytes - 4 * ch) * 2 / ch;
2420 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2421 return (frame_bytes - 4) * 2 / ch;
2422 case AV_CODEC_ID_ADPCM_IMA_AMV:
2423 return (frame_bytes - 8) * 2 / ch;
2424 case AV_CODEC_ID_ADPCM_XA:
2425 return (frame_bytes / 128) * 224 / ch;
2426 case AV_CODEC_ID_INTERPLAY_DPCM:
2427 return (frame_bytes - 6 - ch) / ch;
2428 case AV_CODEC_ID_ROQ_DPCM:
2429 return (frame_bytes - 8) / ch;
2430 case AV_CODEC_ID_XAN_DPCM:
2431 return (frame_bytes - 2 * ch) / ch;
2432 case AV_CODEC_ID_MACE3:
2433 return 3 * frame_bytes / ch;
2434 case AV_CODEC_ID_MACE6:
2435 return 6 * frame_bytes / ch;
2436 case AV_CODEC_ID_PCM_LXF:
2437 return 2 * (frame_bytes / (5 * ch));
2441 /* calc from frame_bytes, channels, and codec_tag */
2442 if (id == AV_CODEC_ID_SOL_DPCM) {
2444 return frame_bytes / ch;
2446 return frame_bytes * 2 / ch;
2451 /* calc from frame_bytes, channels, and block_align */
2452 int blocks = frame_bytes / ba;
2454 case AV_CODEC_ID_ADPCM_IMA_WAV:
2455 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2456 case AV_CODEC_ID_ADPCM_IMA_DK3:
2457 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2458 case AV_CODEC_ID_ADPCM_IMA_DK4:
2459 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2460 case AV_CODEC_ID_ADPCM_MS:
2461 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2466 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2468 case AV_CODEC_ID_PCM_DVD:
2469 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2470 case AV_CODEC_ID_PCM_BLURAY:
2471 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2472 case AV_CODEC_ID_S302M:
2473 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2482 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2484 return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
2485 avctx->channels, avctx->block_align,
2486 avctx->codec_tag, avctx->bits_per_coded_sample,
2490 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
2492 return get_audio_frame_duration(par->codec_id, par->sample_rate,
2493 par->channels, par->block_align,
2494 par->codec_tag, par->bits_per_coded_sample,
2499 int ff_thread_init(AVCodecContext *s)
2506 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2520 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2523 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2527 #if FF_API_MISSING_SAMPLE
2528 FF_DISABLE_DEPRECATION_WARNINGS
2529 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2531 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2532 "version to the newest one from Git. If the problem still "
2533 "occurs, it means that your file has a feature which has not "
2534 "been implemented.\n", feature);
2536 av_log_ask_for_sample(avc, NULL);
2539 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2541 va_list argument_list;
2543 va_start(argument_list, msg);
2546 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2547 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2548 "of this file to ftp://upload.libav.org/incoming/ "
2549 "and contact the libav-devel mailing list.\n");
2551 va_end(argument_list);
2553 FF_ENABLE_DEPRECATION_WARNINGS
2554 #endif /* FF_API_MISSING_SAMPLE */
2556 static AVHWAccel *first_hwaccel = NULL;
2558 void av_register_hwaccel(AVHWAccel *hwaccel)
2560 AVHWAccel **p = &first_hwaccel;
2564 hwaccel->next = NULL;
2567 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2569 return hwaccel ? hwaccel->next : first_hwaccel;
2572 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2575 // There is no good way to rollback a failure to destroy the
2576 // mutex, so we ignore failures.
2577 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
2578 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
2581 avformat_mutex = NULL;
2585 void *new_codec_mutex = NULL;
2586 void *new_avformat_mutex = NULL;
2588 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
2589 return err > 0 ? AVERROR_UNKNOWN : err;
2591 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
2592 // Ignore failures to destroy the newly created mutex.
2593 cb(&new_codec_mutex, AV_LOCK_DESTROY);
2594 return err > 0 ? AVERROR_UNKNOWN : err;
2597 codec_mutex = new_codec_mutex;
2598 avformat_mutex = new_avformat_mutex;
2604 int avpriv_lock_avformat(void)
2607 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2613 int avpriv_unlock_avformat(void)
2616 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2622 unsigned int avpriv_toupper4(unsigned int x)
2624 return av_toupper(x & 0xFF) +
2625 (av_toupper((x >> 8) & 0xFF) << 8) +
2626 (av_toupper((x >> 16) & 0xFF) << 16) +
2627 (av_toupper((x >> 24) & 0xFF) << 24);
2630 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2634 dst->owner = src->owner;
2636 ret = av_frame_ref(dst->f, src->f);
2640 if (src->progress &&
2641 !(dst->progress = av_buffer_ref(src->progress))) {
2642 ff_thread_release_buffer(dst->owner, dst);
2643 return AVERROR(ENOMEM);
2651 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2654 return ff_get_buffer(avctx, f->f, flags);
2657 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2660 av_frame_unref(f->f);
2663 void ff_thread_finish_setup(AVCodecContext *avctx)
2667 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2671 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2677 int avcodec_is_open(AVCodecContext *s)
2679 return !!s->internal;
2682 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2684 uint32_t * restrict state)
2692 for (i = 0; i < 3; i++) {
2693 uint32_t tmp = *state << 8;
2694 *state = tmp + *(p++);
2695 if (tmp == 0x100 || p == end)
2700 if (p[-1] > 1 ) p += 3;
2701 else if (p[-2] ) p += 2;
2702 else if (p[-3]|(p[-1]-1)) p++;
2709 p = FFMIN(p, end) - 4;
2710 *state = AV_RB32(p);
2715 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
2717 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
2722 *size = sizeof(*props);
2724 props->vbv_delay = UINT64_MAX;
2729 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
2731 AVPacketSideData *tmp;
2732 AVCPBProperties *props;
2735 props = av_cpb_properties_alloc(&size);
2739 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
2745 avctx->coded_side_data = tmp;
2746 avctx->nb_coded_side_data++;
2748 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
2749 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
2750 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
2755 static void codec_parameters_reset(AVCodecParameters *par)
2757 av_freep(&par->extradata);
2759 memset(par, 0, sizeof(*par));
2761 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
2762 par->codec_id = AV_CODEC_ID_NONE;
2764 par->field_order = AV_FIELD_UNKNOWN;
2765 par->color_range = AVCOL_RANGE_UNSPECIFIED;
2766 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
2767 par->color_trc = AVCOL_TRC_UNSPECIFIED;
2768 par->color_space = AVCOL_SPC_UNSPECIFIED;
2769 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
2770 par->sample_aspect_ratio = (AVRational){ 0, 1 };
2773 AVCodecParameters *avcodec_parameters_alloc(void)
2775 AVCodecParameters *par = av_mallocz(sizeof(*par));
2779 codec_parameters_reset(par);
2783 void avcodec_parameters_free(AVCodecParameters **ppar)
2785 AVCodecParameters *par = *ppar;
2789 codec_parameters_reset(par);
2794 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
2796 codec_parameters_reset(dst);
2797 memcpy(dst, src, sizeof(*dst));
2799 dst->extradata = NULL;
2800 dst->extradata_size = 0;
2801 if (src->extradata) {
2802 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2803 if (!dst->extradata)
2804 return AVERROR(ENOMEM);
2805 memcpy(dst->extradata, src->extradata, src->extradata_size);
2806 dst->extradata_size = src->extradata_size;
2812 int avcodec_parameters_from_context(AVCodecParameters *par,
2813 const AVCodecContext *codec)
2815 codec_parameters_reset(par);
2817 par->codec_type = codec->codec_type;
2818 par->codec_id = codec->codec_id;
2819 par->codec_tag = codec->codec_tag;
2821 par->bit_rate = codec->bit_rate;
2822 par->bits_per_coded_sample = codec->bits_per_coded_sample;
2823 par->profile = codec->profile;
2824 par->level = codec->level;
2826 switch (par->codec_type) {
2827 case AVMEDIA_TYPE_VIDEO:
2828 par->format = codec->pix_fmt;
2829 par->width = codec->width;
2830 par->height = codec->height;
2831 par->field_order = codec->field_order;
2832 par->color_range = codec->color_range;
2833 par->color_primaries = codec->color_primaries;
2834 par->color_trc = codec->color_trc;
2835 par->color_space = codec->colorspace;
2836 par->chroma_location = codec->chroma_sample_location;
2837 par->sample_aspect_ratio = codec->sample_aspect_ratio;
2839 case AVMEDIA_TYPE_AUDIO:
2840 par->format = codec->sample_fmt;
2841 par->channel_layout = codec->channel_layout;
2842 par->channels = codec->channels;
2843 par->sample_rate = codec->sample_rate;
2844 par->block_align = codec->block_align;
2845 par->initial_padding = codec->initial_padding;
2849 if (codec->extradata) {
2850 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2851 if (!par->extradata)
2852 return AVERROR(ENOMEM);
2853 memcpy(par->extradata, codec->extradata, codec->extradata_size);
2854 par->extradata_size = codec->extradata_size;
2860 int avcodec_parameters_to_context(AVCodecContext *codec,
2861 const AVCodecParameters *par)
2863 codec->codec_type = par->codec_type;
2864 codec->codec_id = par->codec_id;
2865 codec->codec_tag = par->codec_tag;
2867 codec->bit_rate = par->bit_rate;
2868 codec->bits_per_coded_sample = par->bits_per_coded_sample;
2869 codec->profile = par->profile;
2870 codec->level = par->level;
2872 switch (par->codec_type) {
2873 case AVMEDIA_TYPE_VIDEO:
2874 codec->pix_fmt = par->format;
2875 codec->width = par->width;
2876 codec->height = par->height;
2877 codec->field_order = par->field_order;
2878 codec->color_range = par->color_range;
2879 codec->color_primaries = par->color_primaries;
2880 codec->color_trc = par->color_trc;
2881 codec->colorspace = par->color_space;
2882 codec->chroma_sample_location = par->chroma_location;
2883 codec->sample_aspect_ratio = par->sample_aspect_ratio;
2885 case AVMEDIA_TYPE_AUDIO:
2886 codec->sample_fmt = par->format;
2887 codec->channel_layout = par->channel_layout;
2888 codec->channels = par->channels;
2889 codec->sample_rate = par->sample_rate;
2890 codec->block_align = par->block_align;
2891 codec->initial_padding = par->initial_padding;
2895 if (par->extradata) {
2896 av_freep(&codec->extradata);
2897 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2898 if (!codec->extradata)
2899 return AVERROR(ENOMEM);
2900 memcpy(codec->extradata, par->extradata, par->extradata_size);
2901 codec->extradata_size = par->extradata_size;