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_32
182 # define STRIDE_ALIGN 32
183 #elif HAVE_SIMD_ALIGN_16
184 # define STRIDE_ALIGN 16
186 # define STRIDE_ALIGN 8
189 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
190 int linesize_align[AV_NUM_DATA_POINTERS])
196 switch (s->pix_fmt) {
197 case AV_PIX_FMT_YUV420P:
198 case AV_PIX_FMT_YUYV422:
199 case AV_PIX_FMT_YVYU422:
200 case AV_PIX_FMT_UYVY422:
201 case AV_PIX_FMT_YUV422P:
202 case AV_PIX_FMT_YUV440P:
203 case AV_PIX_FMT_YUV444P:
204 case AV_PIX_FMT_GBRP:
205 case AV_PIX_FMT_GBRAP:
206 case AV_PIX_FMT_GRAY8:
207 case AV_PIX_FMT_GRAY16BE:
208 case AV_PIX_FMT_GRAY16LE:
209 case AV_PIX_FMT_YUVJ420P:
210 case AV_PIX_FMT_YUVJ422P:
211 case AV_PIX_FMT_YUVJ440P:
212 case AV_PIX_FMT_YUVJ444P:
213 case AV_PIX_FMT_YUVA420P:
214 case AV_PIX_FMT_YUVA422P:
215 case AV_PIX_FMT_YUVA444P:
216 case AV_PIX_FMT_YUV420P9LE:
217 case AV_PIX_FMT_YUV420P9BE:
218 case AV_PIX_FMT_YUV420P10LE:
219 case AV_PIX_FMT_YUV420P10BE:
220 case AV_PIX_FMT_YUV422P9LE:
221 case AV_PIX_FMT_YUV422P9BE:
222 case AV_PIX_FMT_YUV422P10LE:
223 case AV_PIX_FMT_YUV422P10BE:
224 case AV_PIX_FMT_YUVA422P10LE:
225 case AV_PIX_FMT_YUVA422P10BE:
226 case AV_PIX_FMT_YUV444P9LE:
227 case AV_PIX_FMT_YUV444P9BE:
228 case AV_PIX_FMT_YUV444P10LE:
229 case AV_PIX_FMT_YUV444P10BE:
230 case AV_PIX_FMT_YUVA444P10LE:
231 case AV_PIX_FMT_YUVA444P10BE:
232 case AV_PIX_FMT_GBRP9LE:
233 case AV_PIX_FMT_GBRP9BE:
234 case AV_PIX_FMT_GBRP10LE:
235 case AV_PIX_FMT_GBRP10BE:
236 w_align = 16; //FIXME assume 16 pixel per macroblock
237 h_align = 16 * 2; // interlaced needs 2 macroblocks height
239 case AV_PIX_FMT_YUV411P:
240 case AV_PIX_FMT_UYYVYY411:
244 case AV_PIX_FMT_YUV410P:
245 if (s->codec_id == AV_CODEC_ID_SVQ1) {
249 case AV_PIX_FMT_RGB555:
250 if (s->codec_id == AV_CODEC_ID_RPZA) {
254 case AV_PIX_FMT_PAL8:
255 case AV_PIX_FMT_BGR8:
256 case AV_PIX_FMT_RGB8:
257 if (s->codec_id == AV_CODEC_ID_SMC) {
262 case AV_PIX_FMT_BGR24:
263 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
264 (s->codec_id == AV_CODEC_ID_ZLIB)) {
275 *width = FFALIGN(*width, w_align);
276 *height = FFALIGN(*height, h_align);
277 if (s->codec_id == AV_CODEC_ID_H264)
278 // some of the optimized chroma MC reads one line too much
281 for (i = 0; i < 4; i++)
282 linesize_align[i] = STRIDE_ALIGN;
285 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
287 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
288 int chroma_shift = desc->log2_chroma_w;
289 int linesize_align[AV_NUM_DATA_POINTERS];
292 avcodec_align_dimensions2(s, width, height, linesize_align);
293 align = FFMAX(linesize_align[0], linesize_align[3]);
294 linesize_align[1] <<= chroma_shift;
295 linesize_align[2] <<= chroma_shift;
296 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
297 *width = FFALIGN(*width, align);
300 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
301 enum AVSampleFormat sample_fmt, const uint8_t *buf,
302 int buf_size, int align)
304 int ch, planar, needed_size, ret = 0;
306 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
307 frame->nb_samples, sample_fmt,
309 if (buf_size < needed_size)
310 return AVERROR(EINVAL);
312 planar = av_sample_fmt_is_planar(sample_fmt);
313 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
314 if (!(frame->extended_data = av_mallocz(nb_channels *
315 sizeof(*frame->extended_data))))
316 return AVERROR(ENOMEM);
318 frame->extended_data = frame->data;
321 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
322 buf, nb_channels, frame->nb_samples,
323 sample_fmt, align)) < 0) {
324 if (frame->extended_data != frame->data)
325 av_free(frame->extended_data);
328 if (frame->extended_data != frame->data) {
329 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
330 frame->data[ch] = frame->extended_data[ch];
336 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
338 FramePool *pool = avctx->internal->pool;
341 switch (avctx->codec_type) {
342 case AVMEDIA_TYPE_VIDEO: {
346 int w = frame->width;
347 int h = frame->height;
348 int tmpsize, unaligned;
350 if (pool->format == frame->format &&
351 pool->width == frame->width && pool->height == frame->height)
354 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
357 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
358 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
359 av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
360 // increase alignment of w for next try (rhs gives the lowest bit set in w)
364 for (i = 0; i < 4; i++)
365 unaligned |= linesize[i] % pool->stride_align[i];
368 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
373 for (i = 0; i < 3 && data[i + 1]; i++)
374 size[i] = data[i + 1] - data[i];
375 size[i] = tmpsize - (data[i] - data[0]);
377 for (i = 0; i < 4; i++) {
378 av_buffer_pool_uninit(&pool->pools[i]);
379 pool->linesize[i] = linesize[i];
381 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
382 if (!pool->pools[i]) {
383 ret = AVERROR(ENOMEM);
388 pool->format = frame->format;
389 pool->width = frame->width;
390 pool->height = frame->height;
394 case AVMEDIA_TYPE_AUDIO: {
395 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
396 int planar = av_sample_fmt_is_planar(frame->format);
397 int planes = planar ? ch : 1;
399 if (pool->format == frame->format && pool->planes == planes &&
400 pool->channels == ch && frame->nb_samples == pool->samples)
403 av_buffer_pool_uninit(&pool->pools[0]);
404 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
405 frame->nb_samples, frame->format, 0);
409 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
410 if (!pool->pools[0]) {
411 ret = AVERROR(ENOMEM);
415 pool->format = frame->format;
416 pool->planes = planes;
418 pool->samples = frame->nb_samples;
421 default: av_assert0(0);
425 for (i = 0; i < 4; i++)
426 av_buffer_pool_uninit(&pool->pools[i]);
428 pool->planes = pool->channels = pool->samples = 0;
429 pool->width = pool->height = 0;
433 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
435 FramePool *pool = avctx->internal->pool;
436 int planes = pool->planes;
439 frame->linesize[0] = pool->linesize[0];
441 if (planes > AV_NUM_DATA_POINTERS) {
442 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
443 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
444 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
445 sizeof(*frame->extended_buf));
446 if (!frame->extended_data || !frame->extended_buf) {
447 av_freep(&frame->extended_data);
448 av_freep(&frame->extended_buf);
449 return AVERROR(ENOMEM);
452 frame->extended_data = frame->data;
454 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
455 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
458 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
460 for (i = 0; i < frame->nb_extended_buf; i++) {
461 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
462 if (!frame->extended_buf[i])
464 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
467 if (avctx->debug & FF_DEBUG_BUFFERS)
468 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
472 av_frame_unref(frame);
473 return AVERROR(ENOMEM);
476 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
478 FramePool *pool = s->internal->pool;
482 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
486 memset(pic->data, 0, sizeof(pic->data));
487 pic->extended_data = pic->data;
489 for (i = 0; i < 4 && pool->pools[i]; i++) {
490 pic->linesize[i] = pool->linesize[i];
492 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
496 pic->data[i] = pic->buf[i]->data;
498 for (; i < AV_NUM_DATA_POINTERS; i++) {
500 pic->linesize[i] = 0;
502 if (pic->data[1] && !pic->data[2])
503 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
505 if (s->debug & FF_DEBUG_BUFFERS)
506 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
511 return AVERROR(ENOMEM);
514 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
518 if (avctx->hw_frames_ctx)
519 return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
521 if ((ret = update_frame_pool(avctx, frame)) < 0)
524 switch (avctx->codec_type) {
525 case AVMEDIA_TYPE_VIDEO:
526 return video_get_buffer(avctx, frame);
527 case AVMEDIA_TYPE_AUDIO:
528 return audio_get_buffer(avctx, frame);
534 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
536 AVPacket *pkt = avctx->internal->pkt;
539 enum AVPacketSideDataType packet;
540 enum AVFrameSideDataType frame;
542 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
543 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
544 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
545 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
548 frame->color_primaries = avctx->color_primaries;
549 frame->color_trc = avctx->color_trc;
550 frame->colorspace = avctx->colorspace;
551 frame->color_range = avctx->color_range;
552 frame->chroma_location = avctx->chroma_sample_location;
554 frame->reordered_opaque = avctx->reordered_opaque;
557 FF_DISABLE_DEPRECATION_WARNINGS
558 frame->pkt_pts = AV_NOPTS_VALUE;
559 FF_ENABLE_DEPRECATION_WARNINGS
561 frame->pts = AV_NOPTS_VALUE;
566 FF_DISABLE_DEPRECATION_WARNINGS
567 frame->pkt_pts = pkt->pts;
568 FF_ENABLE_DEPRECATION_WARNINGS
570 frame->pts = pkt->pts;
572 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
574 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
576 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
580 return AVERROR(ENOMEM);
582 memcpy(frame_sd->data, packet_sd, size);
589 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
591 const AVHWAccel *hwaccel = avctx->hwaccel;
592 int override_dimensions = 1;
595 switch (avctx->codec_type) {
596 case AVMEDIA_TYPE_VIDEO:
597 if (frame->width <= 0 || frame->height <= 0) {
598 frame->width = FFMAX(avctx->width, avctx->coded_width);
599 frame->height = FFMAX(avctx->height, avctx->coded_height);
600 override_dimensions = 0;
602 if (frame->format < 0)
603 frame->format = avctx->pix_fmt;
604 if (!frame->sample_aspect_ratio.num)
605 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
607 if (av_image_check_sar(frame->width, frame->height,
608 frame->sample_aspect_ratio) < 0) {
609 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
610 frame->sample_aspect_ratio.num,
611 frame->sample_aspect_ratio.den);
612 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
615 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
618 case AVMEDIA_TYPE_AUDIO:
619 if (!frame->sample_rate)
620 frame->sample_rate = avctx->sample_rate;
621 if (frame->format < 0)
622 frame->format = avctx->sample_fmt;
623 if (!frame->channel_layout) {
624 if (avctx->channel_layout) {
625 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
627 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
629 return AVERROR(EINVAL);
632 frame->channel_layout = avctx->channel_layout;
634 if (avctx->channels > FF_SANE_NB_CHANNELS) {
635 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
637 return AVERROR(ENOSYS);
640 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
641 if (!frame->channel_layout)
642 frame->channel_layout = (1ULL << avctx->channels) - 1;
646 default: return AVERROR(EINVAL);
649 ret = ff_decode_frame_props(avctx, frame);
654 if (hwaccel->alloc_frame) {
655 ret = hwaccel->alloc_frame(avctx, frame);
659 avctx->sw_pix_fmt = avctx->pix_fmt;
661 ret = avctx->get_buffer2(avctx, frame, flags);
664 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
665 frame->width = avctx->width;
666 frame->height = avctx->height;
672 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
677 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
680 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
682 if (av_frame_is_writable(frame))
683 return ff_decode_frame_props(avctx, frame);
685 tmp = av_frame_alloc();
687 return AVERROR(ENOMEM);
689 av_frame_move_ref(tmp, frame);
691 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
697 av_frame_copy(frame, tmp);
703 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
707 for (i = 0; i < count; i++) {
708 int r = func(c, (char *)arg + i * size);
715 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
719 for (i = 0; i < count; i++) {
720 int r = func(c, arg, i, 0);
727 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
729 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
730 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
733 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
735 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
740 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
741 enum AVPixelFormat pix_fmt)
743 AVHWAccel *hwaccel = NULL;
745 while ((hwaccel = av_hwaccel_next(hwaccel)))
746 if (hwaccel->id == codec_id
747 && hwaccel->pix_fmt == pix_fmt)
752 static int setup_hwaccel(AVCodecContext *avctx,
753 const enum AVPixelFormat fmt,
756 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
760 av_log(avctx, AV_LOG_ERROR,
761 "Could not find an AVHWAccel for the pixel format: %s",
763 return AVERROR(ENOENT);
766 if (hwa->priv_data_size) {
767 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
768 if (!avctx->internal->hwaccel_priv_data)
769 return AVERROR(ENOMEM);
773 ret = hwa->init(avctx);
775 av_freep(&avctx->internal->hwaccel_priv_data);
780 avctx->hwaccel = hwa;
785 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
787 const AVPixFmtDescriptor *desc;
788 enum AVPixelFormat *choices;
789 enum AVPixelFormat ret;
792 while (fmt[n] != AV_PIX_FMT_NONE)
796 avctx->sw_pix_fmt = fmt[n - 1];
797 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
799 choices = av_malloc_array(n + 1, sizeof(*choices));
801 return AV_PIX_FMT_NONE;
803 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
806 if (avctx->hwaccel && avctx->hwaccel->uninit)
807 avctx->hwaccel->uninit(avctx);
808 av_freep(&avctx->internal->hwaccel_priv_data);
809 avctx->hwaccel = NULL;
811 av_buffer_unref(&avctx->hw_frames_ctx);
813 ret = avctx->get_format(avctx, choices);
815 desc = av_pix_fmt_desc_get(ret);
817 ret = AV_PIX_FMT_NONE;
821 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
824 if (avctx->hw_frames_ctx) {
825 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
826 if (hw_frames_ctx->format != ret) {
827 av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
828 "does not match the format of provided AVHWFramesContext\n");
829 ret = AV_PIX_FMT_NONE;
834 if (!setup_hwaccel(avctx, ret, desc->name))
837 /* Remove failed hwaccel from choices */
838 for (n = 0; choices[n] != ret; n++)
839 av_assert0(choices[n] != AV_PIX_FMT_NONE);
842 choices[n] = choices[n + 1];
843 while (choices[n++] != AV_PIX_FMT_NONE);
850 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
853 AVDictionary *tmp = NULL;
855 if (avcodec_is_open(avctx))
858 if ((!codec && !avctx->codec)) {
859 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
860 return AVERROR(EINVAL);
862 if ((codec && avctx->codec && codec != avctx->codec)) {
863 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
864 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
865 return AVERROR(EINVAL);
868 codec = avctx->codec;
870 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
871 return AVERROR(EINVAL);
874 av_dict_copy(&tmp, *options, 0);
876 /* If there is a user-supplied mutex locking routine, call it. */
877 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
879 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
883 entangled_thread_counter++;
884 if (entangled_thread_counter != 1) {
885 av_log(avctx, AV_LOG_ERROR,
886 "Insufficient thread locking. At least %d threads are "
887 "calling avcodec_open2() at the same time right now.\n",
888 entangled_thread_counter);
894 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
895 if (!avctx->internal) {
896 ret = AVERROR(ENOMEM);
900 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
901 if (!avctx->internal->pool) {
902 ret = AVERROR(ENOMEM);
906 avctx->internal->to_free = av_frame_alloc();
907 if (!avctx->internal->to_free) {
908 ret = AVERROR(ENOMEM);
912 avctx->internal->buffer_frame = av_frame_alloc();
913 if (!avctx->internal->buffer_frame) {
914 ret = AVERROR(ENOMEM);
918 avctx->internal->buffer_pkt = av_packet_alloc();
919 if (!avctx->internal->buffer_pkt) {
920 ret = AVERROR(ENOMEM);
924 if (codec->priv_data_size > 0) {
925 if (!avctx->priv_data) {
926 avctx->priv_data = av_mallocz(codec->priv_data_size);
927 if (!avctx->priv_data) {
928 ret = AVERROR(ENOMEM);
931 if (codec->priv_class) {
932 *(const AVClass **)avctx->priv_data = codec->priv_class;
933 av_opt_set_defaults(avctx->priv_data);
936 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
939 avctx->priv_data = NULL;
941 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
944 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
945 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
946 else if (avctx->width && avctx->height)
947 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
951 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
952 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
953 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
954 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
955 ff_set_dimensions(avctx, 0, 0);
958 if (avctx->width > 0 && avctx->height > 0) {
959 if (av_image_check_sar(avctx->width, avctx->height,
960 avctx->sample_aspect_ratio) < 0) {
961 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
962 avctx->sample_aspect_ratio.num,
963 avctx->sample_aspect_ratio.den);
964 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
968 /* if the decoder init function was already called previously,
969 * free the already allocated subtitle_header before overwriting it */
970 if (av_codec_is_decoder(codec))
971 av_freep(&avctx->subtitle_header);
973 if (avctx->channels > FF_SANE_NB_CHANNELS) {
974 ret = AVERROR(EINVAL);
978 avctx->codec = codec;
979 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
980 avctx->codec_id == AV_CODEC_ID_NONE) {
981 avctx->codec_type = codec->type;
982 avctx->codec_id = codec->id;
984 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
985 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
986 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
987 ret = AVERROR(EINVAL);
990 avctx->frame_number = 0;
992 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
993 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
994 ret = AVERROR_EXPERIMENTAL;
998 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
999 (!avctx->time_base.num || !avctx->time_base.den)) {
1000 avctx->time_base.num = 1;
1001 avctx->time_base.den = avctx->sample_rate;
1005 ret = ff_thread_init(avctx);
1010 if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
1011 avctx->thread_count = 1;
1013 if (av_codec_is_encoder(avctx->codec)) {
1015 #if FF_API_CODED_FRAME
1016 FF_DISABLE_DEPRECATION_WARNINGS
1017 avctx->coded_frame = av_frame_alloc();
1018 if (!avctx->coded_frame) {
1019 ret = AVERROR(ENOMEM);
1022 FF_ENABLE_DEPRECATION_WARNINGS
1025 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
1026 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
1027 ret = AVERROR(EINVAL);
1031 if (avctx->codec->sample_fmts) {
1032 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1033 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1035 if (avctx->channels == 1 &&
1036 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1037 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1038 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1042 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1043 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1044 ret = AVERROR(EINVAL);
1048 if (avctx->codec->pix_fmts) {
1049 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1050 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1052 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1053 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1054 ret = AVERROR(EINVAL);
1057 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1058 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1059 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1060 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1061 avctx->color_range = AVCOL_RANGE_JPEG;
1063 if (avctx->codec->supported_samplerates) {
1064 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1065 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1067 if (avctx->codec->supported_samplerates[i] == 0) {
1068 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1069 ret = AVERROR(EINVAL);
1073 if (avctx->codec->channel_layouts) {
1074 if (!avctx->channel_layout) {
1075 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1077 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1078 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1080 if (avctx->codec->channel_layouts[i] == 0) {
1081 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1082 ret = AVERROR(EINVAL);
1087 if (avctx->channel_layout && avctx->channels) {
1088 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1089 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1090 ret = AVERROR(EINVAL);
1093 } else if (avctx->channel_layout) {
1094 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1097 if (!avctx->rc_initial_buffer_occupancy)
1098 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1100 if (avctx->ticks_per_frame &&
1101 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
1102 av_log(avctx, AV_LOG_ERROR,
1103 "ticks_per_frame %d too large for the timebase %d/%d.",
1104 avctx->ticks_per_frame,
1105 avctx->time_base.num,
1106 avctx->time_base.den);
1110 if (avctx->hw_frames_ctx) {
1111 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1112 if (frames_ctx->format != avctx->pix_fmt) {
1113 av_log(avctx, AV_LOG_ERROR,
1114 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
1115 ret = AVERROR(EINVAL);
1118 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
1119 avctx->sw_pix_fmt != frames_ctx->sw_format) {
1120 av_log(avctx, AV_LOG_ERROR,
1121 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
1122 "and AVHWFramesContext.sw_format (%s)\n",
1123 av_get_pix_fmt_name(avctx->sw_pix_fmt),
1124 av_get_pix_fmt_name(frames_ctx->sw_format));
1125 ret = AVERROR(EINVAL);
1128 avctx->sw_pix_fmt = frames_ctx->sw_format;
1132 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1133 ret = avctx->codec->init(avctx);
1139 #if FF_API_AUDIOENC_DELAY
1140 if (av_codec_is_encoder(avctx->codec))
1141 avctx->delay = avctx->initial_padding;
1144 if (av_codec_is_decoder(avctx->codec)) {
1145 /* validate channel layout from the decoder */
1146 if (avctx->channel_layout) {
1147 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1148 if (!avctx->channels)
1149 avctx->channels = channels;
1150 else if (channels != avctx->channels) {
1151 av_log(avctx, AV_LOG_WARNING,
1152 "channel layout does not match number of channels\n");
1153 avctx->channel_layout = 0;
1156 if (avctx->channels && avctx->channels < 0 ||
1157 avctx->channels > FF_SANE_NB_CHANNELS) {
1158 ret = AVERROR(EINVAL);
1162 #if FF_API_AVCTX_TIMEBASE
1163 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1164 avctx->time_base = av_inv_q(avctx->framerate);
1168 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
1169 entangled_thread_counter--;
1171 /* Release any user-supplied mutex. */
1173 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1178 av_dict_free(options);
1185 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1186 avctx->codec->close(avctx);
1188 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1189 av_opt_free(avctx->priv_data);
1192 #if FF_API_CODED_FRAME
1193 FF_DISABLE_DEPRECATION_WARNINGS
1194 av_frame_free(&avctx->coded_frame);
1195 FF_ENABLE_DEPRECATION_WARNINGS
1199 av_freep(&avctx->priv_data);
1200 if (avctx->internal) {
1201 av_frame_free(&avctx->internal->to_free);
1202 av_freep(&avctx->internal->pool);
1204 av_freep(&avctx->internal);
1205 avctx->codec = NULL;
1209 int ff_alloc_packet(AVPacket *avpkt, int size)
1211 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
1212 return AVERROR(EINVAL);
1215 AVBufferRef *buf = avpkt->buf;
1217 if (avpkt->size < size)
1218 return AVERROR(EINVAL);
1220 av_init_packet(avpkt);
1225 return av_new_packet(avpkt, size);
1230 * Pad last frame with silence.
1232 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1234 AVFrame *frame = NULL;
1237 if (!(frame = av_frame_alloc()))
1238 return AVERROR(ENOMEM);
1240 frame->format = src->format;
1241 frame->channel_layout = src->channel_layout;
1242 frame->nb_samples = s->frame_size;
1243 ret = av_frame_get_buffer(frame, 32);
1247 ret = av_frame_copy_props(frame, src);
1251 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1252 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1254 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1255 frame->nb_samples - src->nb_samples,
1256 s->channels, s->sample_fmt)) < 0)
1264 av_frame_free(&frame);
1268 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1270 const AVFrame *frame,
1271 int *got_packet_ptr)
1274 AVFrame *padded_frame = NULL;
1276 int user_packet = !!avpkt->data;
1278 *got_packet_ptr = 0;
1280 if (!avctx->codec->encode2) {
1281 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
1282 return AVERROR(ENOSYS);
1285 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1286 av_packet_unref(avpkt);
1287 av_init_packet(avpkt);
1291 /* ensure that extended_data is properly set */
1292 if (frame && !frame->extended_data) {
1293 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1294 avctx->channels > AV_NUM_DATA_POINTERS) {
1295 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1296 "with more than %d channels, but extended_data is not set.\n",
1297 AV_NUM_DATA_POINTERS);
1298 return AVERROR(EINVAL);
1300 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1303 tmp.extended_data = tmp.data;
1307 /* extract audio service type metadata */
1309 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
1310 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1311 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1314 /* check for valid frame size */
1316 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
1317 if (frame->nb_samples > avctx->frame_size)
1318 return AVERROR(EINVAL);
1319 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1320 if (frame->nb_samples < avctx->frame_size &&
1321 !avctx->internal->last_audio_frame) {
1322 ret = pad_last_frame(avctx, &padded_frame, frame);
1326 frame = padded_frame;
1327 avctx->internal->last_audio_frame = 1;
1330 if (frame->nb_samples != avctx->frame_size) {
1331 ret = AVERROR(EINVAL);
1337 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1339 if (*got_packet_ptr) {
1340 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
1341 if (avpkt->pts == AV_NOPTS_VALUE)
1342 avpkt->pts = frame->pts;
1343 if (!avpkt->duration)
1344 avpkt->duration = ff_samples_to_time_base(avctx,
1347 avpkt->dts = avpkt->pts;
1352 if (!user_packet && avpkt->size) {
1353 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1355 avpkt->data = avpkt->buf->data;
1358 avctx->frame_number++;
1361 if (ret < 0 || !*got_packet_ptr) {
1362 av_packet_unref(avpkt);
1363 av_init_packet(avpkt);
1367 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1368 * this needs to be moved to the encoders, but for now we can do it
1369 * here to simplify things */
1370 avpkt->flags |= AV_PKT_FLAG_KEY;
1373 av_frame_free(&padded_frame);
1375 #if FF_API_AUDIOENC_DELAY
1376 avctx->delay = avctx->initial_padding;
1382 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1384 const AVFrame *frame,
1385 int *got_packet_ptr)
1388 int user_packet = !!avpkt->data;
1390 *got_packet_ptr = 0;
1392 if (!avctx->codec->encode2) {
1393 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
1394 return AVERROR(ENOSYS);
1397 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1398 av_packet_unref(avpkt);
1399 av_init_packet(avpkt);
1404 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1405 return AVERROR(EINVAL);
1407 av_assert0(avctx->codec->encode2);
1409 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1411 if (!*got_packet_ptr)
1413 else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1414 avpkt->pts = avpkt->dts = frame->pts;
1416 if (!user_packet && avpkt->size) {
1417 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1419 avpkt->data = avpkt->buf->data;
1422 avctx->frame_number++;
1425 if (ret < 0 || !*got_packet_ptr)
1426 av_packet_unref(avpkt);
1432 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1433 const AVSubtitle *sub)
1436 if (sub->start_display_time) {
1437 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1440 if (sub->num_rects == 0 || !sub->rects)
1442 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1443 avctx->frame_number++;
1447 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1450 const uint8_t *data;
1453 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1457 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
1458 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1459 "changes, but PARAM_CHANGE side data was sent to it.\n");
1460 ret = AVERROR(EINVAL);
1467 flags = bytestream_get_le32(&data);
1470 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1473 avctx->channels = bytestream_get_le32(&data);
1476 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1479 avctx->channel_layout = bytestream_get_le64(&data);
1482 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1485 avctx->sample_rate = bytestream_get_le32(&data);
1488 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1491 avctx->width = bytestream_get_le32(&data);
1492 avctx->height = bytestream_get_le32(&data);
1494 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1501 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1502 ret = AVERROR_INVALIDDATA;
1505 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1506 if (avctx->err_recognition & AV_EF_EXPLODE)
1512 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1516 /* move the original frame to our backup */
1517 av_frame_unref(avci->to_free);
1518 av_frame_move_ref(avci->to_free, frame);
1520 /* now copy everything except the AVBufferRefs back
1521 * note that we make a COPY of the side data, so calling av_frame_free() on
1522 * the caller's frame will work properly */
1523 ret = av_frame_copy_props(frame, avci->to_free);
1527 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1528 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1529 if (avci->to_free->extended_data != avci->to_free->data) {
1530 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1531 int size = planes * sizeof(*frame->extended_data);
1534 av_frame_unref(frame);
1538 frame->extended_data = av_malloc(size);
1539 if (!frame->extended_data) {
1540 av_frame_unref(frame);
1541 return AVERROR(ENOMEM);
1543 memcpy(frame->extended_data, avci->to_free->extended_data,
1546 frame->extended_data = frame->data;
1548 frame->format = avci->to_free->format;
1549 frame->width = avci->to_free->width;
1550 frame->height = avci->to_free->height;
1551 frame->channel_layout = avci->to_free->channel_layout;
1552 frame->nb_samples = avci->to_free->nb_samples;
1557 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1558 int *got_picture_ptr,
1561 AVCodecInternal *avci = avctx->internal;
1564 *got_picture_ptr = 0;
1565 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1568 if (!avctx->codec->decode) {
1569 av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
1570 return AVERROR(ENOSYS);
1573 avctx->internal->pkt = avpkt;
1574 ret = apply_param_change(avctx, avpkt);
1578 av_frame_unref(picture);
1580 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
1581 (avctx->active_thread_type & FF_THREAD_FRAME)) {
1582 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1583 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1586 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1588 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
1589 picture->pkt_dts = avpkt->dts;
1590 /* get_buffer is supposed to set frame parameters */
1591 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
1592 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1593 picture->width = avctx->width;
1594 picture->height = avctx->height;
1595 picture->format = avctx->pix_fmt;
1599 emms_c(); //needed to avoid an emms_c() call before every return;
1601 if (*got_picture_ptr) {
1602 if (!avctx->refcounted_frames) {
1603 int err = unrefcount_frame(avci, picture);
1608 avctx->frame_number++;
1610 av_frame_unref(picture);
1614 #if FF_API_AVCTX_TIMEBASE
1615 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1616 avctx->time_base = av_inv_q(avctx->framerate);
1622 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1627 AVCodecInternal *avci = avctx->internal;
1632 if (!avctx->codec->decode) {
1633 av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
1634 return AVERROR(ENOSYS);
1637 avctx->internal->pkt = avpkt;
1639 if (!avpkt->data && avpkt->size) {
1640 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1641 return AVERROR(EINVAL);
1644 ret = apply_param_change(avctx, avpkt);
1648 av_frame_unref(frame);
1650 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
1651 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1652 if (ret >= 0 && *got_frame_ptr) {
1653 avctx->frame_number++;
1654 frame->pkt_dts = avpkt->dts;
1655 if (frame->format == AV_SAMPLE_FMT_NONE)
1656 frame->format = avctx->sample_fmt;
1658 if (!avctx->refcounted_frames) {
1659 int err = unrefcount_frame(avci, frame);
1664 av_frame_unref(frame);
1671 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1677 avctx->internal->pkt = avpkt;
1679 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1681 avctx->frame_number++;
1685 void avsubtitle_free(AVSubtitle *sub)
1689 for (i = 0; i < sub->num_rects; i++) {
1690 av_freep(&sub->rects[i]->data[0]);
1691 av_freep(&sub->rects[i]->data[1]);
1692 av_freep(&sub->rects[i]->data[2]);
1693 av_freep(&sub->rects[i]->data[3]);
1694 av_freep(&sub->rects[i]->text);
1695 av_freep(&sub->rects[i]->ass);
1696 av_freep(&sub->rects[i]);
1699 av_freep(&sub->rects);
1701 memset(sub, 0, sizeof(AVSubtitle));
1704 static int do_decode(AVCodecContext *avctx, AVPacket *pkt)
1709 av_assert0(!avctx->internal->buffer_frame->buf[0]);
1712 pkt = avctx->internal->buffer_pkt;
1714 // This is the lesser evil. The field is for compatibility with legacy users
1715 // of the legacy API, and users using the new API should not be forced to
1716 // even know about this field.
1717 avctx->refcounted_frames = 1;
1719 // Some codecs (at least wma lossless) will crash when feeding drain packets
1720 // after EOF was signaled.
1721 if (avctx->internal->draining_done)
1724 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1725 ret = avcodec_decode_video2(avctx, avctx->internal->buffer_frame,
1729 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1730 ret = avcodec_decode_audio4(avctx, avctx->internal->buffer_frame,
1733 ret = AVERROR(EINVAL);
1739 if (avctx->internal->draining && !got_frame)
1740 avctx->internal->draining_done = 1;
1742 if (ret >= pkt->size) {
1743 av_packet_unref(avctx->internal->buffer_pkt);
1747 if (pkt != avctx->internal->buffer_pkt) {
1748 av_packet_unref(avctx->internal->buffer_pkt);
1749 if ((ret = av_packet_ref(avctx->internal->buffer_pkt, pkt)) < 0)
1753 avctx->internal->buffer_pkt->data += consumed;
1754 avctx->internal->buffer_pkt->size -= consumed;
1755 avctx->internal->buffer_pkt->pts = AV_NOPTS_VALUE;
1756 avctx->internal->buffer_pkt->dts = AV_NOPTS_VALUE;
1760 av_assert0(avctx->internal->buffer_frame->buf[0]);
1765 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
1769 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
1770 return AVERROR(EINVAL);
1772 if (avctx->internal->draining)
1775 if (!avpkt || !avpkt->size) {
1776 avctx->internal->draining = 1;
1779 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1783 if (avctx->codec->send_packet) {
1785 ret = apply_param_change(avctx, (AVPacket *)avpkt);
1789 return avctx->codec->send_packet(avctx, avpkt);
1792 // Emulation via old API. Assume avpkt is likely not refcounted, while
1793 // decoder output is always refcounted, and avoid copying.
1795 if (avctx->internal->buffer_pkt->size || avctx->internal->buffer_frame->buf[0])
1796 return AVERROR(EAGAIN);
1798 // The goal is decoding the first frame of the packet without using memcpy,
1799 // because the common case is having only 1 frame per packet (especially
1800 // with video, but audio too). In other cases, it can't be avoided, unless
1801 // the user is feeding refcounted packets.
1802 return do_decode(avctx, (AVPacket *)avpkt);
1805 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1809 av_frame_unref(frame);
1811 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
1812 return AVERROR(EINVAL);
1814 if (avctx->codec->receive_frame) {
1815 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1817 return avctx->codec->receive_frame(avctx, frame);
1820 // Emulation via old API.
1822 if (!avctx->internal->buffer_frame->buf[0]) {
1823 if (!avctx->internal->buffer_pkt->size && !avctx->internal->draining)
1824 return AVERROR(EAGAIN);
1827 if ((ret = do_decode(avctx, avctx->internal->buffer_pkt)) < 0) {
1828 av_packet_unref(avctx->internal->buffer_pkt);
1831 // Some audio decoders may consume partial data without returning
1832 // a frame (fate-wmapro-2ch). There is no way to make the caller
1833 // call avcodec_receive_frame() again without returning a frame,
1834 // so try to decode more in these cases.
1835 if (avctx->internal->buffer_frame->buf[0] ||
1836 !avctx->internal->buffer_pkt->size)
1841 if (!avctx->internal->buffer_frame->buf[0])
1842 return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
1844 av_frame_move_ref(frame, avctx->internal->buffer_frame);
1848 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
1853 av_packet_unref(avctx->internal->buffer_pkt);
1854 avctx->internal->buffer_pkt_valid = 0;
1856 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1857 ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
1859 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1860 ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
1863 ret = AVERROR(EINVAL);
1866 if (ret >= 0 && *got_packet) {
1867 // Encoders must always return ref-counted buffers.
1868 // Side-data only packets have no data and can be not ref-counted.
1869 av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
1870 avctx->internal->buffer_pkt_valid = 1;
1873 av_packet_unref(avctx->internal->buffer_pkt);
1879 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
1881 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
1882 return AVERROR(EINVAL);
1884 if (avctx->internal->draining)
1888 avctx->internal->draining = 1;
1890 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1894 if (avctx->codec->send_frame)
1895 return avctx->codec->send_frame(avctx, frame);
1897 // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
1898 // 1. if the AVFrame is not refcounted, the copying will be much more
1899 // expensive than copying the packet data
1900 // 2. assume few users use non-refcounted AVPackets, so usually no copy is
1903 if (avctx->internal->buffer_pkt_valid)
1904 return AVERROR(EAGAIN);
1906 return do_encode(avctx, frame, &(int){0});
1909 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
1911 av_packet_unref(avpkt);
1913 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
1914 return AVERROR(EINVAL);
1916 if (avctx->codec->receive_packet) {
1917 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1919 return avctx->codec->receive_packet(avctx, avpkt);
1922 // Emulation via old API.
1924 if (!avctx->internal->buffer_pkt_valid) {
1927 if (!avctx->internal->draining)
1928 return AVERROR(EAGAIN);
1929 ret = do_encode(avctx, NULL, &got_packet);
1932 if (ret >= 0 && !got_packet)
1936 av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
1937 avctx->internal->buffer_pkt_valid = 0;
1941 av_cold int avcodec_close(AVCodecContext *avctx)
1945 if (avcodec_is_open(avctx)) {
1946 FramePool *pool = avctx->internal->pool;
1948 if (HAVE_THREADS && avctx->internal->thread_ctx)
1949 ff_thread_free(avctx);
1950 if (avctx->codec && avctx->codec->close)
1951 avctx->codec->close(avctx);
1952 av_frame_free(&avctx->internal->to_free);
1953 av_frame_free(&avctx->internal->buffer_frame);
1954 av_packet_free(&avctx->internal->buffer_pkt);
1955 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1956 av_buffer_pool_uninit(&pool->pools[i]);
1957 av_freep(&avctx->internal->pool);
1959 if (avctx->hwaccel && avctx->hwaccel->uninit)
1960 avctx->hwaccel->uninit(avctx);
1961 av_freep(&avctx->internal->hwaccel_priv_data);
1963 av_freep(&avctx->internal);
1966 for (i = 0; i < avctx->nb_coded_side_data; i++)
1967 av_freep(&avctx->coded_side_data[i].data);
1968 av_freep(&avctx->coded_side_data);
1969 avctx->nb_coded_side_data = 0;
1971 av_buffer_unref(&avctx->hw_frames_ctx);
1973 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1974 av_opt_free(avctx->priv_data);
1976 av_freep(&avctx->priv_data);
1977 if (av_codec_is_encoder(avctx->codec)) {
1978 av_freep(&avctx->extradata);
1979 #if FF_API_CODED_FRAME
1980 FF_DISABLE_DEPRECATION_WARNINGS
1981 av_frame_free(&avctx->coded_frame);
1982 FF_ENABLE_DEPRECATION_WARNINGS
1985 avctx->codec = NULL;
1986 avctx->active_thread_type = 0;
1991 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1993 AVCodec *p, *experimental = NULL;
1996 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1998 if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
2005 return experimental;
2008 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2010 return find_encdec(id, 1);
2013 AVCodec *avcodec_find_encoder_by_name(const char *name)
2020 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2027 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2029 return find_encdec(id, 0);
2032 AVCodec *avcodec_find_decoder_by_name(const char *name)
2039 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2046 static int get_bit_rate(AVCodecContext *ctx)
2049 int bits_per_sample;
2051 switch (ctx->codec_type) {
2052 case AVMEDIA_TYPE_VIDEO:
2053 case AVMEDIA_TYPE_DATA:
2054 case AVMEDIA_TYPE_SUBTITLE:
2055 case AVMEDIA_TYPE_ATTACHMENT:
2056 bit_rate = ctx->bit_rate;
2058 case AVMEDIA_TYPE_AUDIO:
2059 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
2060 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
2069 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2071 int i, len, ret = 0;
2073 #define TAG_PRINT(x) \
2074 (((x) >= '0' && (x) <= '9') || \
2075 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2076 ((x) == '.' || (x) == ' '))
2078 for (i = 0; i < 4; i++) {
2079 len = snprintf(buf, buf_size,
2080 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2082 buf_size = buf_size > len ? buf_size - len : 0;
2089 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2091 const char *codec_name;
2092 const char *profile = NULL;
2096 AVRational display_aspect_ratio;
2097 const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
2100 codec_name = desc->name;
2101 profile = avcodec_profile_name(enc->codec_id, enc->profile);
2102 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
2103 /* fake mpeg2 transport stream codec (currently not
2105 codec_name = "mpeg2ts";
2107 /* output avi tags */
2109 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2110 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
2114 switch (enc->codec_type) {
2115 case AVMEDIA_TYPE_VIDEO:
2116 snprintf(buf, buf_size,
2118 codec_name, enc->mb_decision ? " (hq)" : "");
2120 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2122 if (enc->codec_tag) {
2124 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2125 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2126 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2129 av_strlcat(buf, "\n ", buf_size);
2130 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2131 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
2132 av_get_pix_fmt_name(enc->pix_fmt));
2134 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2135 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
2136 av_color_range_name(enc->color_range));
2137 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
2138 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
2139 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
2141 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
2142 av_color_space_name(enc->colorspace),
2143 av_color_primaries_name(enc->color_primaries),
2144 av_color_transfer_name(enc->color_trc));
2146 if (av_log_get_level() >= AV_LOG_DEBUG &&
2147 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
2148 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
2149 av_chroma_location_name(enc->chroma_sample_location));
2152 av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
2154 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2156 enc->width, enc->height);
2158 if (av_log_get_level() >= AV_LOG_VERBOSE &&
2159 (enc->width != enc->coded_width ||
2160 enc->height != enc->coded_height))
2161 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2162 " (%dx%d)", enc->coded_width, enc->coded_height);
2164 if (enc->sample_aspect_ratio.num) {
2165 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2166 enc->width * enc->sample_aspect_ratio.num,
2167 enc->height * enc->sample_aspect_ratio.den,
2169 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2170 " [PAR %d:%d DAR %d:%d]",
2171 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2172 display_aspect_ratio.num, display_aspect_ratio.den);
2174 if (av_log_get_level() >= AV_LOG_DEBUG) {
2175 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2176 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2178 enc->time_base.num / g, enc->time_base.den / g);
2182 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2183 ", q=%d-%d", enc->qmin, enc->qmax);
2186 case AVMEDIA_TYPE_AUDIO:
2187 snprintf(buf, buf_size,
2191 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2193 if (enc->codec_tag) {
2195 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2196 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2197 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2200 av_strlcat(buf, "\n ", buf_size);
2201 if (enc->sample_rate) {
2202 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2203 "%d Hz, ", enc->sample_rate);
2205 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2206 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2207 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2208 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2211 case AVMEDIA_TYPE_DATA:
2212 snprintf(buf, buf_size, "Data: %s", codec_name);
2214 case AVMEDIA_TYPE_SUBTITLE:
2215 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
2217 case AVMEDIA_TYPE_ATTACHMENT:
2218 snprintf(buf, buf_size, "Attachment: %s", codec_name);
2221 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
2225 if (enc->flags & AV_CODEC_FLAG_PASS1)
2226 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2228 if (enc->flags & AV_CODEC_FLAG_PASS2)
2229 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2232 bitrate = get_bit_rate(enc);
2234 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2235 ", %d kb/s", bitrate / 1000);
2239 const char *av_get_profile_name(const AVCodec *codec, int profile)
2242 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2245 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2246 if (p->profile == profile)
2252 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
2254 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
2257 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
2260 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2261 if (p->profile == profile)
2267 unsigned avcodec_version(void)
2269 return LIBAVCODEC_VERSION_INT;
2272 const char *avcodec_configuration(void)
2274 return LIBAV_CONFIGURATION;
2277 const char *avcodec_license(void)
2279 #define LICENSE_PREFIX "libavcodec license: "
2280 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2283 void avcodec_flush_buffers(AVCodecContext *avctx)
2285 avctx->internal->draining = 0;
2286 avctx->internal->draining_done = 0;
2287 av_frame_unref(avctx->internal->buffer_frame);
2288 av_packet_unref(avctx->internal->buffer_pkt);
2289 avctx->internal->buffer_pkt_valid = 0;
2291 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2292 ff_thread_flush(avctx);
2293 else if (avctx->codec->flush)
2294 avctx->codec->flush(avctx);
2296 if (!avctx->refcounted_frames)
2297 av_frame_unref(avctx->internal->to_free);
2300 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2303 case AV_CODEC_ID_ADPCM_CT:
2304 case AV_CODEC_ID_ADPCM_IMA_APC:
2305 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2306 case AV_CODEC_ID_ADPCM_IMA_WS:
2307 case AV_CODEC_ID_ADPCM_G722:
2308 case AV_CODEC_ID_ADPCM_YAMAHA:
2310 case AV_CODEC_ID_PCM_ALAW:
2311 case AV_CODEC_ID_PCM_MULAW:
2312 case AV_CODEC_ID_PCM_S8:
2313 case AV_CODEC_ID_PCM_U8:
2314 case AV_CODEC_ID_PCM_ZORK:
2316 case AV_CODEC_ID_PCM_S16BE:
2317 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2318 case AV_CODEC_ID_PCM_S16LE:
2319 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2320 case AV_CODEC_ID_PCM_U16BE:
2321 case AV_CODEC_ID_PCM_U16LE:
2323 case AV_CODEC_ID_PCM_S24DAUD:
2324 case AV_CODEC_ID_PCM_S24BE:
2325 case AV_CODEC_ID_PCM_S24LE:
2326 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2327 case AV_CODEC_ID_PCM_U24BE:
2328 case AV_CODEC_ID_PCM_U24LE:
2330 case AV_CODEC_ID_PCM_S32BE:
2331 case AV_CODEC_ID_PCM_S32LE:
2332 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2333 case AV_CODEC_ID_PCM_U32BE:
2334 case AV_CODEC_ID_PCM_U32LE:
2335 case AV_CODEC_ID_PCM_F32BE:
2336 case AV_CODEC_ID_PCM_F32LE:
2338 case AV_CODEC_ID_PCM_F64BE:
2339 case AV_CODEC_ID_PCM_F64LE:
2346 int av_get_bits_per_sample(enum AVCodecID codec_id)
2349 case AV_CODEC_ID_ADPCM_SBPRO_2:
2351 case AV_CODEC_ID_ADPCM_SBPRO_3:
2353 case AV_CODEC_ID_ADPCM_SBPRO_4:
2354 case AV_CODEC_ID_ADPCM_IMA_WAV:
2355 case AV_CODEC_ID_ADPCM_IMA_QT:
2356 case AV_CODEC_ID_ADPCM_SWF:
2357 case AV_CODEC_ID_ADPCM_MS:
2360 return av_get_exact_bits_per_sample(codec_id);
2364 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
2365 uint32_t tag, int bits_per_coded_sample, int frame_bytes)
2367 int bps = av_get_exact_bits_per_sample(id);
2369 /* codecs with an exact constant bits per sample */
2370 if (bps > 0 && ch > 0 && frame_bytes > 0)
2371 return (frame_bytes * 8) / (bps * ch);
2372 bps = bits_per_coded_sample;
2374 /* codecs with a fixed packet duration */
2376 case AV_CODEC_ID_ADPCM_ADX: return 32;
2377 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2378 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2379 case AV_CODEC_ID_AMR_NB:
2380 case AV_CODEC_ID_GSM:
2381 case AV_CODEC_ID_QCELP:
2382 case AV_CODEC_ID_RA_144:
2383 case AV_CODEC_ID_RA_288: return 160;
2384 case AV_CODEC_ID_IMC: return 256;
2385 case AV_CODEC_ID_AMR_WB:
2386 case AV_CODEC_ID_GSM_MS: return 320;
2387 case AV_CODEC_ID_MP1: return 384;
2388 case AV_CODEC_ID_ATRAC1: return 512;
2389 case AV_CODEC_ID_ATRAC3: return 1024;
2390 case AV_CODEC_ID_MP2:
2391 case AV_CODEC_ID_MUSEPACK7: return 1152;
2392 case AV_CODEC_ID_AC3: return 1536;
2396 /* calc from sample rate */
2397 if (id == AV_CODEC_ID_TTA)
2398 return 256 * sr / 245;
2401 /* calc from sample rate and channels */
2402 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2403 return (480 << (sr / 22050)) / ch;
2408 /* calc from block_align */
2409 if (id == AV_CODEC_ID_SIPR) {
2411 case 20: return 160;
2412 case 19: return 144;
2413 case 29: return 288;
2414 case 37: return 480;
2416 } else if (id == AV_CODEC_ID_ILBC) {
2418 case 38: return 160;
2419 case 50: return 240;
2424 if (frame_bytes > 0) {
2425 /* calc from frame_bytes only */
2426 if (id == AV_CODEC_ID_TRUESPEECH)
2427 return 240 * (frame_bytes / 32);
2428 if (id == AV_CODEC_ID_NELLYMOSER)
2429 return 256 * (frame_bytes / 64);
2432 /* calc from frame_bytes and bits_per_coded_sample */
2433 if (id == AV_CODEC_ID_ADPCM_G726)
2434 return frame_bytes * 8 / bps;
2438 /* calc from frame_bytes and channels */
2440 case AV_CODEC_ID_ADPCM_4XM:
2441 case AV_CODEC_ID_ADPCM_IMA_ISS:
2442 return (frame_bytes - 4 * ch) * 2 / ch;
2443 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2444 return (frame_bytes - 4) * 2 / ch;
2445 case AV_CODEC_ID_ADPCM_IMA_AMV:
2446 return (frame_bytes - 8) * 2 / ch;
2447 case AV_CODEC_ID_ADPCM_XA:
2448 return (frame_bytes / 128) * 224 / ch;
2449 case AV_CODEC_ID_INTERPLAY_DPCM:
2450 return (frame_bytes - 6 - ch) / ch;
2451 case AV_CODEC_ID_ROQ_DPCM:
2452 return (frame_bytes - 8) / ch;
2453 case AV_CODEC_ID_XAN_DPCM:
2454 return (frame_bytes - 2 * ch) / ch;
2455 case AV_CODEC_ID_MACE3:
2456 return 3 * frame_bytes / ch;
2457 case AV_CODEC_ID_MACE6:
2458 return 6 * frame_bytes / ch;
2459 case AV_CODEC_ID_PCM_LXF:
2460 return 2 * (frame_bytes / (5 * ch));
2464 /* calc from frame_bytes, channels, and codec_tag */
2465 if (id == AV_CODEC_ID_SOL_DPCM) {
2467 return frame_bytes / ch;
2469 return frame_bytes * 2 / ch;
2474 /* calc from frame_bytes, channels, and block_align */
2475 int blocks = frame_bytes / ba;
2477 case AV_CODEC_ID_ADPCM_IMA_WAV:
2478 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2479 case AV_CODEC_ID_ADPCM_IMA_DK3:
2480 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2481 case AV_CODEC_ID_ADPCM_IMA_DK4:
2482 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2483 case AV_CODEC_ID_ADPCM_MS:
2484 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2489 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2491 case AV_CODEC_ID_PCM_DVD:
2492 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2493 case AV_CODEC_ID_PCM_BLURAY:
2494 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2495 case AV_CODEC_ID_S302M:
2496 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2505 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2507 return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
2508 avctx->channels, avctx->block_align,
2509 avctx->codec_tag, avctx->bits_per_coded_sample,
2513 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
2515 return get_audio_frame_duration(par->codec_id, par->sample_rate,
2516 par->channels, par->block_align,
2517 par->codec_tag, par->bits_per_coded_sample,
2522 int ff_thread_init(AVCodecContext *s)
2529 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2543 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2546 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2550 #if FF_API_MISSING_SAMPLE
2551 FF_DISABLE_DEPRECATION_WARNINGS
2552 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2554 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2555 "version to the newest one from Git. If the problem still "
2556 "occurs, it means that your file has a feature which has not "
2557 "been implemented.\n", feature);
2559 av_log_ask_for_sample(avc, NULL);
2562 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2564 va_list argument_list;
2566 va_start(argument_list, msg);
2569 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2570 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2571 "of this file to ftp://upload.libav.org/incoming/ "
2572 "and contact the libav-devel mailing list.\n");
2574 va_end(argument_list);
2576 FF_ENABLE_DEPRECATION_WARNINGS
2577 #endif /* FF_API_MISSING_SAMPLE */
2579 static AVHWAccel *first_hwaccel = NULL;
2581 void av_register_hwaccel(AVHWAccel *hwaccel)
2583 AVHWAccel **p = &first_hwaccel;
2587 hwaccel->next = NULL;
2590 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2592 return hwaccel ? hwaccel->next : first_hwaccel;
2595 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2598 // There is no good way to rollback a failure to destroy the
2599 // mutex, so we ignore failures.
2600 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
2601 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
2604 avformat_mutex = NULL;
2608 void *new_codec_mutex = NULL;
2609 void *new_avformat_mutex = NULL;
2611 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
2612 return err > 0 ? AVERROR_UNKNOWN : err;
2614 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
2615 // Ignore failures to destroy the newly created mutex.
2616 cb(&new_codec_mutex, AV_LOCK_DESTROY);
2617 return err > 0 ? AVERROR_UNKNOWN : err;
2620 codec_mutex = new_codec_mutex;
2621 avformat_mutex = new_avformat_mutex;
2627 int avpriv_lock_avformat(void)
2630 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2636 int avpriv_unlock_avformat(void)
2639 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2645 unsigned int avpriv_toupper4(unsigned int x)
2647 return av_toupper(x & 0xFF) +
2648 (av_toupper((x >> 8) & 0xFF) << 8) +
2649 (av_toupper((x >> 16) & 0xFF) << 16) +
2650 (av_toupper((x >> 24) & 0xFF) << 24);
2653 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2657 dst->owner = src->owner;
2659 ret = av_frame_ref(dst->f, src->f);
2663 if (src->progress &&
2664 !(dst->progress = av_buffer_ref(src->progress))) {
2665 ff_thread_release_buffer(dst->owner, dst);
2666 return AVERROR(ENOMEM);
2674 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2677 return ff_get_buffer(avctx, f->f, flags);
2680 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2683 av_frame_unref(f->f);
2686 void ff_thread_finish_setup(AVCodecContext *avctx)
2690 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2694 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2700 int avcodec_is_open(AVCodecContext *s)
2702 return !!s->internal;
2705 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2707 uint32_t * restrict state)
2715 for (i = 0; i < 3; i++) {
2716 uint32_t tmp = *state << 8;
2717 *state = tmp + *(p++);
2718 if (tmp == 0x100 || p == end)
2723 if (p[-1] > 1 ) p += 3;
2724 else if (p[-2] ) p += 2;
2725 else if (p[-3]|(p[-1]-1)) p++;
2732 p = FFMIN(p, end) - 4;
2733 *state = AV_RB32(p);
2738 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
2740 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
2745 *size = sizeof(*props);
2747 props->vbv_delay = UINT64_MAX;
2752 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
2754 AVPacketSideData *tmp;
2755 AVCPBProperties *props;
2758 props = av_cpb_properties_alloc(&size);
2762 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
2768 avctx->coded_side_data = tmp;
2769 avctx->nb_coded_side_data++;
2771 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
2772 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
2773 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
2778 static void codec_parameters_reset(AVCodecParameters *par)
2780 av_freep(&par->extradata);
2782 memset(par, 0, sizeof(*par));
2784 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
2785 par->codec_id = AV_CODEC_ID_NONE;
2787 par->field_order = AV_FIELD_UNKNOWN;
2788 par->color_range = AVCOL_RANGE_UNSPECIFIED;
2789 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
2790 par->color_trc = AVCOL_TRC_UNSPECIFIED;
2791 par->color_space = AVCOL_SPC_UNSPECIFIED;
2792 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
2793 par->sample_aspect_ratio = (AVRational){ 0, 1 };
2796 AVCodecParameters *avcodec_parameters_alloc(void)
2798 AVCodecParameters *par = av_mallocz(sizeof(*par));
2802 codec_parameters_reset(par);
2806 void avcodec_parameters_free(AVCodecParameters **ppar)
2808 AVCodecParameters *par = *ppar;
2812 codec_parameters_reset(par);
2817 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
2819 codec_parameters_reset(dst);
2820 memcpy(dst, src, sizeof(*dst));
2822 dst->extradata = NULL;
2823 dst->extradata_size = 0;
2824 if (src->extradata) {
2825 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2826 if (!dst->extradata)
2827 return AVERROR(ENOMEM);
2828 memcpy(dst->extradata, src->extradata, src->extradata_size);
2829 dst->extradata_size = src->extradata_size;
2835 int avcodec_parameters_from_context(AVCodecParameters *par,
2836 const AVCodecContext *codec)
2838 codec_parameters_reset(par);
2840 par->codec_type = codec->codec_type;
2841 par->codec_id = codec->codec_id;
2842 par->codec_tag = codec->codec_tag;
2844 par->bit_rate = codec->bit_rate;
2845 par->bits_per_coded_sample = codec->bits_per_coded_sample;
2846 par->profile = codec->profile;
2847 par->level = codec->level;
2849 switch (par->codec_type) {
2850 case AVMEDIA_TYPE_VIDEO:
2851 par->format = codec->pix_fmt;
2852 par->width = codec->width;
2853 par->height = codec->height;
2854 par->field_order = codec->field_order;
2855 par->color_range = codec->color_range;
2856 par->color_primaries = codec->color_primaries;
2857 par->color_trc = codec->color_trc;
2858 par->color_space = codec->colorspace;
2859 par->chroma_location = codec->chroma_sample_location;
2860 par->sample_aspect_ratio = codec->sample_aspect_ratio;
2862 case AVMEDIA_TYPE_AUDIO:
2863 par->format = codec->sample_fmt;
2864 par->channel_layout = codec->channel_layout;
2865 par->channels = codec->channels;
2866 par->sample_rate = codec->sample_rate;
2867 par->block_align = codec->block_align;
2868 par->initial_padding = codec->initial_padding;
2872 if (codec->extradata) {
2873 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2874 if (!par->extradata)
2875 return AVERROR(ENOMEM);
2876 memcpy(par->extradata, codec->extradata, codec->extradata_size);
2877 par->extradata_size = codec->extradata_size;
2883 int avcodec_parameters_to_context(AVCodecContext *codec,
2884 const AVCodecParameters *par)
2886 codec->codec_type = par->codec_type;
2887 codec->codec_id = par->codec_id;
2888 codec->codec_tag = par->codec_tag;
2890 codec->bit_rate = par->bit_rate;
2891 codec->bits_per_coded_sample = par->bits_per_coded_sample;
2892 codec->profile = par->profile;
2893 codec->level = par->level;
2895 switch (par->codec_type) {
2896 case AVMEDIA_TYPE_VIDEO:
2897 codec->pix_fmt = par->format;
2898 codec->width = par->width;
2899 codec->height = par->height;
2900 codec->field_order = par->field_order;
2901 codec->color_range = par->color_range;
2902 codec->color_primaries = par->color_primaries;
2903 codec->color_trc = par->color_trc;
2904 codec->colorspace = par->color_space;
2905 codec->chroma_sample_location = par->chroma_location;
2906 codec->sample_aspect_ratio = par->sample_aspect_ratio;
2908 case AVMEDIA_TYPE_AUDIO:
2909 codec->sample_fmt = par->format;
2910 codec->channel_layout = par->channel_layout;
2911 codec->channels = par->channels;
2912 codec->sample_rate = par->sample_rate;
2913 codec->block_align = par->block_align;
2914 codec->initial_padding = par->initial_padding;
2918 if (par->extradata) {
2919 av_freep(&codec->extradata);
2920 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2921 if (!codec->extradata)
2922 return AVERROR(ENOMEM);
2923 memcpy(codec->extradata, par->extradata, par->extradata_size);
2924 codec->extradata_size = par->extradata_size;