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);
101 int av_codec_is_decoder(const AVCodec *codec)
103 return codec && codec->decode;
106 av_cold void avcodec_register(AVCodec *codec)
116 if (codec->init_static_data)
117 codec->init_static_data(codec);
121 unsigned avcodec_get_edge_width(void)
127 #if FF_API_SET_DIMENSIONS
128 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
130 ff_set_dimensions(s, width, height);
134 int ff_set_dimensions(AVCodecContext *s, int width, int height)
136 int ret = av_image_check_size(width, height, 0, s);
140 s->width = s->coded_width = width;
141 s->height = s->coded_height = height;
146 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
148 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
151 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
153 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
156 avctx->sample_aspect_ratio = sar;
161 int ff_side_data_update_matrix_encoding(AVFrame *frame,
162 enum AVMatrixEncoding matrix_encoding)
164 AVFrameSideData *side_data;
165 enum AVMatrixEncoding *data;
167 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
169 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
170 sizeof(enum AVMatrixEncoding));
173 return AVERROR(ENOMEM);
175 data = (enum AVMatrixEncoding*)side_data->data;
176 *data = matrix_encoding;
181 #if HAVE_SIMD_ALIGN_16
182 # define STRIDE_ALIGN 16
184 # define STRIDE_ALIGN 8
187 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
188 int linesize_align[AV_NUM_DATA_POINTERS])
194 switch (s->pix_fmt) {
195 case AV_PIX_FMT_YUV420P:
196 case AV_PIX_FMT_YUYV422:
197 case AV_PIX_FMT_YVYU422:
198 case AV_PIX_FMT_UYVY422:
199 case AV_PIX_FMT_YUV422P:
200 case AV_PIX_FMT_YUV440P:
201 case AV_PIX_FMT_YUV444P:
202 case AV_PIX_FMT_GBRP:
203 case AV_PIX_FMT_GBRAP:
204 case AV_PIX_FMT_GRAY8:
205 case AV_PIX_FMT_GRAY16BE:
206 case AV_PIX_FMT_GRAY16LE:
207 case AV_PIX_FMT_YUVJ420P:
208 case AV_PIX_FMT_YUVJ422P:
209 case AV_PIX_FMT_YUVJ440P:
210 case AV_PIX_FMT_YUVJ444P:
211 case AV_PIX_FMT_YUVA420P:
212 case AV_PIX_FMT_YUVA422P:
213 case AV_PIX_FMT_YUVA444P:
214 case AV_PIX_FMT_YUV420P9LE:
215 case AV_PIX_FMT_YUV420P9BE:
216 case AV_PIX_FMT_YUV420P10LE:
217 case AV_PIX_FMT_YUV420P10BE:
218 case AV_PIX_FMT_YUV422P9LE:
219 case AV_PIX_FMT_YUV422P9BE:
220 case AV_PIX_FMT_YUV422P10LE:
221 case AV_PIX_FMT_YUV422P10BE:
222 case AV_PIX_FMT_YUVA422P10LE:
223 case AV_PIX_FMT_YUVA422P10BE:
224 case AV_PIX_FMT_YUV444P9LE:
225 case AV_PIX_FMT_YUV444P9BE:
226 case AV_PIX_FMT_YUV444P10LE:
227 case AV_PIX_FMT_YUV444P10BE:
228 case AV_PIX_FMT_YUVA444P10LE:
229 case AV_PIX_FMT_YUVA444P10BE:
230 case AV_PIX_FMT_GBRP9LE:
231 case AV_PIX_FMT_GBRP9BE:
232 case AV_PIX_FMT_GBRP10LE:
233 case AV_PIX_FMT_GBRP10BE:
234 w_align = 16; //FIXME assume 16 pixel per macroblock
235 h_align = 16 * 2; // interlaced needs 2 macroblocks height
237 case AV_PIX_FMT_YUV411P:
238 case AV_PIX_FMT_UYYVYY411:
242 case AV_PIX_FMT_YUV410P:
243 if (s->codec_id == AV_CODEC_ID_SVQ1) {
247 case AV_PIX_FMT_RGB555:
248 if (s->codec_id == AV_CODEC_ID_RPZA) {
252 case AV_PIX_FMT_PAL8:
253 case AV_PIX_FMT_BGR8:
254 case AV_PIX_FMT_RGB8:
255 if (s->codec_id == AV_CODEC_ID_SMC) {
260 case AV_PIX_FMT_BGR24:
261 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
262 (s->codec_id == AV_CODEC_ID_ZLIB)) {
273 *width = FFALIGN(*width, w_align);
274 *height = FFALIGN(*height, h_align);
275 if (s->codec_id == AV_CODEC_ID_H264)
276 // some of the optimized chroma MC reads one line too much
279 for (i = 0; i < 4; i++)
280 linesize_align[i] = STRIDE_ALIGN;
283 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
285 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
286 int chroma_shift = desc->log2_chroma_w;
287 int linesize_align[AV_NUM_DATA_POINTERS];
290 avcodec_align_dimensions2(s, width, height, linesize_align);
291 align = FFMAX(linesize_align[0], linesize_align[3]);
292 linesize_align[1] <<= chroma_shift;
293 linesize_align[2] <<= chroma_shift;
294 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
295 *width = FFALIGN(*width, align);
298 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
299 enum AVSampleFormat sample_fmt, const uint8_t *buf,
300 int buf_size, int align)
302 int ch, planar, needed_size, ret = 0;
304 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
305 frame->nb_samples, sample_fmt,
307 if (buf_size < needed_size)
308 return AVERROR(EINVAL);
310 planar = av_sample_fmt_is_planar(sample_fmt);
311 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
312 if (!(frame->extended_data = av_mallocz(nb_channels *
313 sizeof(*frame->extended_data))))
314 return AVERROR(ENOMEM);
316 frame->extended_data = frame->data;
319 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
320 buf, nb_channels, frame->nb_samples,
321 sample_fmt, align)) < 0) {
322 if (frame->extended_data != frame->data)
323 av_free(frame->extended_data);
326 if (frame->extended_data != frame->data) {
327 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
328 frame->data[ch] = frame->extended_data[ch];
334 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
336 FramePool *pool = avctx->internal->pool;
339 switch (avctx->codec_type) {
340 case AVMEDIA_TYPE_VIDEO: {
344 int w = frame->width;
345 int h = frame->height;
346 int tmpsize, unaligned;
348 if (pool->format == frame->format &&
349 pool->width == frame->width && pool->height == frame->height)
352 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
355 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
356 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
357 av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
358 // increase alignment of w for next try (rhs gives the lowest bit set in w)
362 for (i = 0; i < 4; i++)
363 unaligned |= linesize[i] % pool->stride_align[i];
366 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
371 for (i = 0; i < 3 && data[i + 1]; i++)
372 size[i] = data[i + 1] - data[i];
373 size[i] = tmpsize - (data[i] - data[0]);
375 for (i = 0; i < 4; i++) {
376 av_buffer_pool_uninit(&pool->pools[i]);
377 pool->linesize[i] = linesize[i];
379 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
380 if (!pool->pools[i]) {
381 ret = AVERROR(ENOMEM);
386 pool->format = frame->format;
387 pool->width = frame->width;
388 pool->height = frame->height;
392 case AVMEDIA_TYPE_AUDIO: {
393 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
394 int planar = av_sample_fmt_is_planar(frame->format);
395 int planes = planar ? ch : 1;
397 if (pool->format == frame->format && pool->planes == planes &&
398 pool->channels == ch && frame->nb_samples == pool->samples)
401 av_buffer_pool_uninit(&pool->pools[0]);
402 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
403 frame->nb_samples, frame->format, 0);
407 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
408 if (!pool->pools[0]) {
409 ret = AVERROR(ENOMEM);
413 pool->format = frame->format;
414 pool->planes = planes;
416 pool->samples = frame->nb_samples;
419 default: av_assert0(0);
423 for (i = 0; i < 4; i++)
424 av_buffer_pool_uninit(&pool->pools[i]);
426 pool->planes = pool->channels = pool->samples = 0;
427 pool->width = pool->height = 0;
431 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
433 FramePool *pool = avctx->internal->pool;
434 int planes = pool->planes;
437 frame->linesize[0] = pool->linesize[0];
439 if (planes > AV_NUM_DATA_POINTERS) {
440 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
441 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
442 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
443 sizeof(*frame->extended_buf));
444 if (!frame->extended_data || !frame->extended_buf) {
445 av_freep(&frame->extended_data);
446 av_freep(&frame->extended_buf);
447 return AVERROR(ENOMEM);
450 frame->extended_data = frame->data;
452 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
453 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
456 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
458 for (i = 0; i < frame->nb_extended_buf; i++) {
459 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
460 if (!frame->extended_buf[i])
462 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
465 if (avctx->debug & FF_DEBUG_BUFFERS)
466 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
470 av_frame_unref(frame);
471 return AVERROR(ENOMEM);
474 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
476 FramePool *pool = s->internal->pool;
480 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
484 memset(pic->data, 0, sizeof(pic->data));
485 pic->extended_data = pic->data;
487 for (i = 0; i < 4 && pool->pools[i]; i++) {
488 pic->linesize[i] = pool->linesize[i];
490 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
494 pic->data[i] = pic->buf[i]->data;
496 for (; i < AV_NUM_DATA_POINTERS; i++) {
498 pic->linesize[i] = 0;
500 if (pic->data[1] && !pic->data[2])
501 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
503 if (s->debug & FF_DEBUG_BUFFERS)
504 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
509 return AVERROR(ENOMEM);
512 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
516 if ((ret = update_frame_pool(avctx, frame)) < 0)
519 switch (avctx->codec_type) {
520 case AVMEDIA_TYPE_VIDEO:
521 return video_get_buffer(avctx, frame);
522 case AVMEDIA_TYPE_AUDIO:
523 return audio_get_buffer(avctx, frame);
529 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
531 AVPacket *pkt = avctx->internal->pkt;
534 enum AVPacketSideDataType packet;
535 enum AVFrameSideDataType frame;
537 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
538 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
539 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
540 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
543 frame->color_primaries = avctx->color_primaries;
544 frame->color_trc = avctx->color_trc;
545 frame->colorspace = avctx->colorspace;
546 frame->color_range = avctx->color_range;
547 frame->chroma_location = avctx->chroma_sample_location;
549 frame->reordered_opaque = avctx->reordered_opaque;
551 frame->pkt_pts = AV_NOPTS_VALUE;
555 frame->pkt_pts = pkt->pts;
557 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
559 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
561 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
565 return AVERROR(ENOMEM);
567 memcpy(frame_sd->data, packet_sd, size);
574 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
576 const AVHWAccel *hwaccel = avctx->hwaccel;
577 int override_dimensions = 1;
580 switch (avctx->codec_type) {
581 case AVMEDIA_TYPE_VIDEO:
582 if (frame->width <= 0 || frame->height <= 0) {
583 frame->width = FFMAX(avctx->width, avctx->coded_width);
584 frame->height = FFMAX(avctx->height, avctx->coded_height);
585 override_dimensions = 0;
587 if (frame->format < 0)
588 frame->format = avctx->pix_fmt;
589 if (!frame->sample_aspect_ratio.num)
590 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
592 if (av_image_check_sar(frame->width, frame->height,
593 frame->sample_aspect_ratio) < 0) {
594 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
595 frame->sample_aspect_ratio.num,
596 frame->sample_aspect_ratio.den);
597 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
600 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
603 case AVMEDIA_TYPE_AUDIO:
604 if (!frame->sample_rate)
605 frame->sample_rate = avctx->sample_rate;
606 if (frame->format < 0)
607 frame->format = avctx->sample_fmt;
608 if (!frame->channel_layout) {
609 if (avctx->channel_layout) {
610 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
612 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
614 return AVERROR(EINVAL);
617 frame->channel_layout = avctx->channel_layout;
619 if (avctx->channels > FF_SANE_NB_CHANNELS) {
620 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
622 return AVERROR(ENOSYS);
625 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
626 if (!frame->channel_layout)
627 frame->channel_layout = (1ULL << avctx->channels) - 1;
631 default: return AVERROR(EINVAL);
634 ret = ff_decode_frame_props(avctx, frame);
639 if (hwaccel->alloc_frame) {
640 ret = hwaccel->alloc_frame(avctx, frame);
644 avctx->sw_pix_fmt = avctx->pix_fmt;
646 ret = avctx->get_buffer2(avctx, frame, flags);
649 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
650 frame->width = avctx->width;
651 frame->height = avctx->height;
657 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
662 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
665 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
667 if (av_frame_is_writable(frame))
668 return ff_decode_frame_props(avctx, frame);
670 tmp = av_frame_alloc();
672 return AVERROR(ENOMEM);
674 av_frame_move_ref(tmp, frame);
676 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
682 av_frame_copy(frame, tmp);
688 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
692 for (i = 0; i < count; i++) {
693 int r = func(c, (char *)arg + i * size);
700 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
704 for (i = 0; i < count; i++) {
705 int r = func(c, arg, i, 0);
712 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
714 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
715 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
718 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
720 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
725 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
726 enum AVPixelFormat pix_fmt)
728 AVHWAccel *hwaccel = NULL;
730 while ((hwaccel = av_hwaccel_next(hwaccel)))
731 if (hwaccel->id == codec_id
732 && hwaccel->pix_fmt == pix_fmt)
737 static int setup_hwaccel(AVCodecContext *avctx,
738 const enum AVPixelFormat fmt,
741 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
745 av_log(avctx, AV_LOG_ERROR,
746 "Could not find an AVHWAccel for the pixel format: %s",
748 return AVERROR(ENOENT);
751 if (hwa->priv_data_size) {
752 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
753 if (!avctx->internal->hwaccel_priv_data)
754 return AVERROR(ENOMEM);
758 ret = hwa->init(avctx);
760 av_freep(&avctx->internal->hwaccel_priv_data);
765 avctx->hwaccel = hwa;
770 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
772 const AVPixFmtDescriptor *desc;
773 enum AVPixelFormat *choices;
774 enum AVPixelFormat ret;
777 while (fmt[n] != AV_PIX_FMT_NONE)
781 avctx->sw_pix_fmt = fmt[n - 1];
782 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
784 choices = av_malloc_array(n + 1, sizeof(*choices));
786 return AV_PIX_FMT_NONE;
788 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
791 if (avctx->hwaccel && avctx->hwaccel->uninit)
792 avctx->hwaccel->uninit(avctx);
793 av_freep(&avctx->internal->hwaccel_priv_data);
794 avctx->hwaccel = NULL;
796 ret = avctx->get_format(avctx, choices);
798 desc = av_pix_fmt_desc_get(ret);
800 ret = AV_PIX_FMT_NONE;
804 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
807 if (!setup_hwaccel(avctx, ret, desc->name))
810 /* Remove failed hwaccel from choices */
811 for (n = 0; choices[n] != ret; n++)
812 av_assert0(choices[n] != AV_PIX_FMT_NONE);
815 choices[n] = choices[n + 1];
816 while (choices[n++] != AV_PIX_FMT_NONE);
823 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
826 AVDictionary *tmp = NULL;
828 if (avcodec_is_open(avctx))
831 if ((!codec && !avctx->codec)) {
832 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
833 return AVERROR(EINVAL);
835 if ((codec && avctx->codec && codec != avctx->codec)) {
836 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
837 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
838 return AVERROR(EINVAL);
841 codec = avctx->codec;
843 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
844 return AVERROR(EINVAL);
847 av_dict_copy(&tmp, *options, 0);
849 /* If there is a user-supplied mutex locking routine, call it. */
850 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
852 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
856 entangled_thread_counter++;
857 if (entangled_thread_counter != 1) {
858 av_log(avctx, AV_LOG_ERROR,
859 "Insufficient thread locking. At least %d threads are "
860 "calling avcodec_open2() at the same time right now.\n",
861 entangled_thread_counter);
867 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
868 if (!avctx->internal) {
869 ret = AVERROR(ENOMEM);
873 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
874 if (!avctx->internal->pool) {
875 ret = AVERROR(ENOMEM);
879 avctx->internal->to_free = av_frame_alloc();
880 if (!avctx->internal->to_free) {
881 ret = AVERROR(ENOMEM);
885 if (codec->priv_data_size > 0) {
886 if (!avctx->priv_data) {
887 avctx->priv_data = av_mallocz(codec->priv_data_size);
888 if (!avctx->priv_data) {
889 ret = AVERROR(ENOMEM);
892 if (codec->priv_class) {
893 *(const AVClass **)avctx->priv_data = codec->priv_class;
894 av_opt_set_defaults(avctx->priv_data);
897 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
900 avctx->priv_data = NULL;
902 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
905 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
906 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
907 else if (avctx->width && avctx->height)
908 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
912 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
913 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
914 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
915 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
916 ff_set_dimensions(avctx, 0, 0);
919 if (avctx->width > 0 && avctx->height > 0) {
920 if (av_image_check_sar(avctx->width, avctx->height,
921 avctx->sample_aspect_ratio) < 0) {
922 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
923 avctx->sample_aspect_ratio.num,
924 avctx->sample_aspect_ratio.den);
925 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
929 /* if the decoder init function was already called previously,
930 * free the already allocated subtitle_header before overwriting it */
931 if (av_codec_is_decoder(codec))
932 av_freep(&avctx->subtitle_header);
934 if (avctx->channels > FF_SANE_NB_CHANNELS) {
935 ret = AVERROR(EINVAL);
939 avctx->codec = codec;
940 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
941 avctx->codec_id == AV_CODEC_ID_NONE) {
942 avctx->codec_type = codec->type;
943 avctx->codec_id = codec->id;
945 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
946 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
947 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
948 ret = AVERROR(EINVAL);
951 avctx->frame_number = 0;
953 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
954 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
955 ret = AVERROR_EXPERIMENTAL;
959 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
960 (!avctx->time_base.num || !avctx->time_base.den)) {
961 avctx->time_base.num = 1;
962 avctx->time_base.den = avctx->sample_rate;
966 ret = ff_thread_init(avctx);
971 if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
972 avctx->thread_count = 1;
974 if (av_codec_is_encoder(avctx->codec)) {
976 #if FF_API_CODED_FRAME
977 FF_DISABLE_DEPRECATION_WARNINGS
978 avctx->coded_frame = av_frame_alloc();
979 if (!avctx->coded_frame) {
980 ret = AVERROR(ENOMEM);
983 FF_ENABLE_DEPRECATION_WARNINGS
985 if (avctx->codec->sample_fmts) {
986 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
987 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
989 if (avctx->channels == 1 &&
990 av_get_planar_sample_fmt(avctx->sample_fmt) ==
991 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
992 avctx->sample_fmt = avctx->codec->sample_fmts[i];
996 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
997 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
998 ret = AVERROR(EINVAL);
1002 if (avctx->codec->pix_fmts) {
1003 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1004 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1006 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1007 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1008 ret = AVERROR(EINVAL);
1011 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1012 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1013 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1014 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1015 avctx->color_range = AVCOL_RANGE_JPEG;
1017 if (avctx->codec->supported_samplerates) {
1018 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1019 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1021 if (avctx->codec->supported_samplerates[i] == 0) {
1022 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1023 ret = AVERROR(EINVAL);
1027 if (avctx->codec->channel_layouts) {
1028 if (!avctx->channel_layout) {
1029 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1031 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1032 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1034 if (avctx->codec->channel_layouts[i] == 0) {
1035 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1036 ret = AVERROR(EINVAL);
1041 if (avctx->channel_layout && avctx->channels) {
1042 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1043 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1044 ret = AVERROR(EINVAL);
1047 } else if (avctx->channel_layout) {
1048 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1051 if (!avctx->rc_initial_buffer_occupancy)
1052 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1054 if (avctx->ticks_per_frame &&
1055 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
1056 av_log(avctx, AV_LOG_ERROR,
1057 "ticks_per_frame %d too large for the timebase %d/%d.",
1058 avctx->ticks_per_frame,
1059 avctx->time_base.num,
1060 avctx->time_base.den);
1064 if (avctx->hw_frames_ctx) {
1065 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1066 if (frames_ctx->format != avctx->pix_fmt) {
1067 av_log(avctx, AV_LOG_ERROR,
1068 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
1069 ret = AVERROR(EINVAL);
1075 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1076 ret = avctx->codec->init(avctx);
1082 #if FF_API_AUDIOENC_DELAY
1083 if (av_codec_is_encoder(avctx->codec))
1084 avctx->delay = avctx->initial_padding;
1087 if (av_codec_is_decoder(avctx->codec)) {
1088 /* validate channel layout from the decoder */
1089 if (avctx->channel_layout) {
1090 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1091 if (!avctx->channels)
1092 avctx->channels = channels;
1093 else if (channels != avctx->channels) {
1094 av_log(avctx, AV_LOG_WARNING,
1095 "channel layout does not match number of channels\n");
1096 avctx->channel_layout = 0;
1099 if (avctx->channels && avctx->channels < 0 ||
1100 avctx->channels > FF_SANE_NB_CHANNELS) {
1101 ret = AVERROR(EINVAL);
1105 #if FF_API_AVCTX_TIMEBASE
1106 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1107 avctx->time_base = av_inv_q(avctx->framerate);
1111 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
1112 entangled_thread_counter--;
1114 /* Release any user-supplied mutex. */
1116 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1121 av_dict_free(options);
1128 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1129 avctx->codec->close(avctx);
1131 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1132 av_opt_free(avctx->priv_data);
1135 #if FF_API_CODED_FRAME
1136 FF_DISABLE_DEPRECATION_WARNINGS
1137 av_frame_free(&avctx->coded_frame);
1138 FF_ENABLE_DEPRECATION_WARNINGS
1142 av_freep(&avctx->priv_data);
1143 if (avctx->internal) {
1144 av_frame_free(&avctx->internal->to_free);
1145 av_freep(&avctx->internal->pool);
1147 av_freep(&avctx->internal);
1148 avctx->codec = NULL;
1152 int ff_alloc_packet(AVPacket *avpkt, int size)
1154 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
1155 return AVERROR(EINVAL);
1158 AVBufferRef *buf = avpkt->buf;
1160 if (avpkt->size < size)
1161 return AVERROR(EINVAL);
1163 av_init_packet(avpkt);
1168 return av_new_packet(avpkt, size);
1173 * Pad last frame with silence.
1175 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1177 AVFrame *frame = NULL;
1180 if (!(frame = av_frame_alloc()))
1181 return AVERROR(ENOMEM);
1183 frame->format = src->format;
1184 frame->channel_layout = src->channel_layout;
1185 frame->nb_samples = s->frame_size;
1186 ret = av_frame_get_buffer(frame, 32);
1190 ret = av_frame_copy_props(frame, src);
1194 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1195 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1197 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1198 frame->nb_samples - src->nb_samples,
1199 s->channels, s->sample_fmt)) < 0)
1207 av_frame_free(&frame);
1211 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1213 const AVFrame *frame,
1214 int *got_packet_ptr)
1217 AVFrame *padded_frame = NULL;
1219 int user_packet = !!avpkt->data;
1221 *got_packet_ptr = 0;
1223 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1224 av_packet_unref(avpkt);
1225 av_init_packet(avpkt);
1229 /* ensure that extended_data is properly set */
1230 if (frame && !frame->extended_data) {
1231 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1232 avctx->channels > AV_NUM_DATA_POINTERS) {
1233 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1234 "with more than %d channels, but extended_data is not set.\n",
1235 AV_NUM_DATA_POINTERS);
1236 return AVERROR(EINVAL);
1238 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1241 tmp.extended_data = tmp.data;
1245 /* extract audio service type metadata */
1247 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
1248 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1249 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1252 /* check for valid frame size */
1254 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
1255 if (frame->nb_samples > avctx->frame_size)
1256 return AVERROR(EINVAL);
1257 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1258 if (frame->nb_samples < avctx->frame_size &&
1259 !avctx->internal->last_audio_frame) {
1260 ret = pad_last_frame(avctx, &padded_frame, frame);
1264 frame = padded_frame;
1265 avctx->internal->last_audio_frame = 1;
1268 if (frame->nb_samples != avctx->frame_size) {
1269 ret = AVERROR(EINVAL);
1275 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1277 if (*got_packet_ptr) {
1278 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
1279 if (avpkt->pts == AV_NOPTS_VALUE)
1280 avpkt->pts = frame->pts;
1281 if (!avpkt->duration)
1282 avpkt->duration = ff_samples_to_time_base(avctx,
1285 avpkt->dts = avpkt->pts;
1290 if (!user_packet && avpkt->size) {
1291 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1293 avpkt->data = avpkt->buf->data;
1296 avctx->frame_number++;
1299 if (ret < 0 || !*got_packet_ptr) {
1300 av_packet_unref(avpkt);
1301 av_init_packet(avpkt);
1305 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1306 * this needs to be moved to the encoders, but for now we can do it
1307 * here to simplify things */
1308 avpkt->flags |= AV_PKT_FLAG_KEY;
1311 av_frame_free(&padded_frame);
1313 #if FF_API_AUDIOENC_DELAY
1314 avctx->delay = avctx->initial_padding;
1320 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1322 const AVFrame *frame,
1323 int *got_packet_ptr)
1326 int user_packet = !!avpkt->data;
1328 *got_packet_ptr = 0;
1330 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1331 av_packet_unref(avpkt);
1332 av_init_packet(avpkt);
1337 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1338 return AVERROR(EINVAL);
1340 av_assert0(avctx->codec->encode2);
1342 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1344 if (!*got_packet_ptr)
1346 else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1347 avpkt->pts = avpkt->dts = frame->pts;
1349 if (!user_packet && avpkt->size) {
1350 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1352 avpkt->data = avpkt->buf->data;
1355 avctx->frame_number++;
1358 if (ret < 0 || !*got_packet_ptr)
1359 av_packet_unref(avpkt);
1365 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1366 const AVSubtitle *sub)
1369 if (sub->start_display_time) {
1370 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1373 if (sub->num_rects == 0 || !sub->rects)
1375 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1376 avctx->frame_number++;
1380 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1383 const uint8_t *data;
1386 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1390 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
1391 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1392 "changes, but PARAM_CHANGE side data was sent to it.\n");
1393 return AVERROR(EINVAL);
1399 flags = bytestream_get_le32(&data);
1402 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1405 avctx->channels = bytestream_get_le32(&data);
1408 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1411 avctx->channel_layout = bytestream_get_le64(&data);
1414 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1417 avctx->sample_rate = bytestream_get_le32(&data);
1420 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1423 avctx->width = bytestream_get_le32(&data);
1424 avctx->height = bytestream_get_le32(&data);
1426 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1433 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1434 return AVERROR_INVALIDDATA;
1437 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1441 /* move the original frame to our backup */
1442 av_frame_unref(avci->to_free);
1443 av_frame_move_ref(avci->to_free, frame);
1445 /* now copy everything except the AVBufferRefs back
1446 * note that we make a COPY of the side data, so calling av_frame_free() on
1447 * the caller's frame will work properly */
1448 ret = av_frame_copy_props(frame, avci->to_free);
1452 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1453 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1454 if (avci->to_free->extended_data != avci->to_free->data) {
1455 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1456 int size = planes * sizeof(*frame->extended_data);
1459 av_frame_unref(frame);
1463 frame->extended_data = av_malloc(size);
1464 if (!frame->extended_data) {
1465 av_frame_unref(frame);
1466 return AVERROR(ENOMEM);
1468 memcpy(frame->extended_data, avci->to_free->extended_data,
1471 frame->extended_data = frame->data;
1473 frame->format = avci->to_free->format;
1474 frame->width = avci->to_free->width;
1475 frame->height = avci->to_free->height;
1476 frame->channel_layout = avci->to_free->channel_layout;
1477 frame->nb_samples = avci->to_free->nb_samples;
1482 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1483 int *got_picture_ptr,
1486 AVCodecInternal *avci = avctx->internal;
1489 *got_picture_ptr = 0;
1490 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1493 avctx->internal->pkt = avpkt;
1494 ret = apply_param_change(avctx, avpkt);
1496 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1497 if (avctx->err_recognition & AV_EF_EXPLODE)
1501 av_frame_unref(picture);
1503 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
1504 (avctx->active_thread_type & FF_THREAD_FRAME)) {
1505 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1506 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1509 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1511 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
1512 picture->pkt_dts = avpkt->dts;
1513 /* get_buffer is supposed to set frame parameters */
1514 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
1515 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1516 picture->width = avctx->width;
1517 picture->height = avctx->height;
1518 picture->format = avctx->pix_fmt;
1522 emms_c(); //needed to avoid an emms_c() call before every return;
1524 if (*got_picture_ptr) {
1525 if (!avctx->refcounted_frames) {
1526 int err = unrefcount_frame(avci, picture);
1531 avctx->frame_number++;
1533 av_frame_unref(picture);
1537 #if FF_API_AVCTX_TIMEBASE
1538 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1539 avctx->time_base = av_inv_q(avctx->framerate);
1545 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1550 AVCodecInternal *avci = avctx->internal;
1555 avctx->internal->pkt = avpkt;
1557 if (!avpkt->data && avpkt->size) {
1558 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1559 return AVERROR(EINVAL);
1562 ret = apply_param_change(avctx, avpkt);
1564 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1565 if (avctx->err_recognition & AV_EF_EXPLODE)
1569 av_frame_unref(frame);
1571 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
1572 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1573 if (ret >= 0 && *got_frame_ptr) {
1574 avctx->frame_number++;
1575 frame->pkt_dts = avpkt->dts;
1576 if (frame->format == AV_SAMPLE_FMT_NONE)
1577 frame->format = avctx->sample_fmt;
1579 if (!avctx->refcounted_frames) {
1580 int err = unrefcount_frame(avci, frame);
1585 av_frame_unref(frame);
1592 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1598 avctx->internal->pkt = avpkt;
1600 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1602 avctx->frame_number++;
1606 void avsubtitle_free(AVSubtitle *sub)
1610 for (i = 0; i < sub->num_rects; i++) {
1611 av_freep(&sub->rects[i]->data[0]);
1612 av_freep(&sub->rects[i]->data[1]);
1613 av_freep(&sub->rects[i]->data[2]);
1614 av_freep(&sub->rects[i]->data[3]);
1615 av_freep(&sub->rects[i]->text);
1616 av_freep(&sub->rects[i]->ass);
1617 av_freep(&sub->rects[i]);
1620 av_freep(&sub->rects);
1622 memset(sub, 0, sizeof(AVSubtitle));
1625 av_cold int avcodec_close(AVCodecContext *avctx)
1629 if (avcodec_is_open(avctx)) {
1630 FramePool *pool = avctx->internal->pool;
1632 if (HAVE_THREADS && avctx->internal->thread_ctx)
1633 ff_thread_free(avctx);
1634 if (avctx->codec && avctx->codec->close)
1635 avctx->codec->close(avctx);
1636 av_frame_free(&avctx->internal->to_free);
1637 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1638 av_buffer_pool_uninit(&pool->pools[i]);
1639 av_freep(&avctx->internal->pool);
1641 if (avctx->hwaccel && avctx->hwaccel->uninit)
1642 avctx->hwaccel->uninit(avctx);
1643 av_freep(&avctx->internal->hwaccel_priv_data);
1645 av_freep(&avctx->internal);
1648 for (i = 0; i < avctx->nb_coded_side_data; i++)
1649 av_freep(&avctx->coded_side_data[i].data);
1650 av_freep(&avctx->coded_side_data);
1651 avctx->nb_coded_side_data = 0;
1653 av_buffer_unref(&avctx->hw_frames_ctx);
1655 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1656 av_opt_free(avctx->priv_data);
1658 av_freep(&avctx->priv_data);
1659 if (av_codec_is_encoder(avctx->codec)) {
1660 av_freep(&avctx->extradata);
1661 #if FF_API_CODED_FRAME
1662 FF_DISABLE_DEPRECATION_WARNINGS
1663 av_frame_free(&avctx->coded_frame);
1664 FF_ENABLE_DEPRECATION_WARNINGS
1667 avctx->codec = NULL;
1668 avctx->active_thread_type = 0;
1673 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1675 AVCodec *p, *experimental = NULL;
1678 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1680 if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
1687 return experimental;
1690 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1692 return find_encdec(id, 1);
1695 AVCodec *avcodec_find_encoder_by_name(const char *name)
1702 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1709 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1711 return find_encdec(id, 0);
1714 AVCodec *avcodec_find_decoder_by_name(const char *name)
1721 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1728 static int get_bit_rate(AVCodecContext *ctx)
1731 int bits_per_sample;
1733 switch (ctx->codec_type) {
1734 case AVMEDIA_TYPE_VIDEO:
1735 case AVMEDIA_TYPE_DATA:
1736 case AVMEDIA_TYPE_SUBTITLE:
1737 case AVMEDIA_TYPE_ATTACHMENT:
1738 bit_rate = ctx->bit_rate;
1740 case AVMEDIA_TYPE_AUDIO:
1741 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1742 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1751 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1753 int i, len, ret = 0;
1755 #define TAG_PRINT(x) \
1756 (((x) >= '0' && (x) <= '9') || \
1757 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1758 ((x) == '.' || (x) == ' '))
1760 for (i = 0; i < 4; i++) {
1761 len = snprintf(buf, buf_size,
1762 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1764 buf_size = buf_size > len ? buf_size - len : 0;
1771 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1773 const char *codec_name;
1774 const char *profile = NULL;
1778 AVRational display_aspect_ratio;
1779 const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
1782 codec_name = desc->name;
1783 profile = avcodec_profile_name(enc->codec_id, enc->profile);
1784 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1785 /* fake mpeg2 transport stream codec (currently not
1787 codec_name = "mpeg2ts";
1789 /* output avi tags */
1791 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1792 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1796 switch (enc->codec_type) {
1797 case AVMEDIA_TYPE_VIDEO:
1798 snprintf(buf, buf_size,
1800 codec_name, enc->mb_decision ? " (hq)" : "");
1802 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1804 if (enc->codec_tag) {
1806 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1807 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1808 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
1811 av_strlcat(buf, "\n ", buf_size);
1812 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1813 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
1814 av_get_pix_fmt_name(enc->pix_fmt));
1816 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
1817 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
1818 av_color_range_name(enc->color_range));
1819 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
1820 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
1821 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
1823 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
1824 av_color_space_name(enc->colorspace),
1825 av_color_primaries_name(enc->color_primaries),
1826 av_color_transfer_name(enc->color_trc));
1828 if (av_log_get_level() >= AV_LOG_DEBUG &&
1829 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
1830 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
1831 av_chroma_location_name(enc->chroma_sample_location));
1834 av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
1836 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1838 enc->width, enc->height);
1840 if (av_log_get_level() >= AV_LOG_VERBOSE &&
1841 (enc->width != enc->coded_width ||
1842 enc->height != enc->coded_height))
1843 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1844 " (%dx%d)", enc->coded_width, enc->coded_height);
1846 if (enc->sample_aspect_ratio.num) {
1847 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1848 enc->width * enc->sample_aspect_ratio.num,
1849 enc->height * enc->sample_aspect_ratio.den,
1851 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1852 " [PAR %d:%d DAR %d:%d]",
1853 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1854 display_aspect_ratio.num, display_aspect_ratio.den);
1856 if (av_log_get_level() >= AV_LOG_DEBUG) {
1857 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1858 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1860 enc->time_base.num / g, enc->time_base.den / g);
1864 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1865 ", q=%d-%d", enc->qmin, enc->qmax);
1868 case AVMEDIA_TYPE_AUDIO:
1869 snprintf(buf, buf_size,
1873 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1875 if (enc->codec_tag) {
1877 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1878 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1879 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
1882 av_strlcat(buf, "\n ", buf_size);
1883 if (enc->sample_rate) {
1884 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1885 "%d Hz, ", enc->sample_rate);
1887 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1888 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1889 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1890 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1893 case AVMEDIA_TYPE_DATA:
1894 snprintf(buf, buf_size, "Data: %s", codec_name);
1896 case AVMEDIA_TYPE_SUBTITLE:
1897 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1899 case AVMEDIA_TYPE_ATTACHMENT:
1900 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1903 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1907 if (enc->flags & AV_CODEC_FLAG_PASS1)
1908 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1910 if (enc->flags & AV_CODEC_FLAG_PASS2)
1911 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1914 bitrate = get_bit_rate(enc);
1916 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1917 ", %d kb/s", bitrate / 1000);
1921 const char *av_get_profile_name(const AVCodec *codec, int profile)
1924 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1927 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1928 if (p->profile == profile)
1934 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
1936 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
1939 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
1942 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1943 if (p->profile == profile)
1949 unsigned avcodec_version(void)
1951 return LIBAVCODEC_VERSION_INT;
1954 const char *avcodec_configuration(void)
1956 return LIBAV_CONFIGURATION;
1959 const char *avcodec_license(void)
1961 #define LICENSE_PREFIX "libavcodec license: "
1962 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1965 void avcodec_flush_buffers(AVCodecContext *avctx)
1967 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1968 ff_thread_flush(avctx);
1969 else if (avctx->codec->flush)
1970 avctx->codec->flush(avctx);
1972 if (!avctx->refcounted_frames)
1973 av_frame_unref(avctx->internal->to_free);
1976 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1979 case AV_CODEC_ID_ADPCM_CT:
1980 case AV_CODEC_ID_ADPCM_IMA_APC:
1981 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1982 case AV_CODEC_ID_ADPCM_IMA_WS:
1983 case AV_CODEC_ID_ADPCM_G722:
1984 case AV_CODEC_ID_ADPCM_YAMAHA:
1986 case AV_CODEC_ID_PCM_ALAW:
1987 case AV_CODEC_ID_PCM_MULAW:
1988 case AV_CODEC_ID_PCM_S8:
1989 case AV_CODEC_ID_PCM_U8:
1990 case AV_CODEC_ID_PCM_ZORK:
1992 case AV_CODEC_ID_PCM_S16BE:
1993 case AV_CODEC_ID_PCM_S16BE_PLANAR:
1994 case AV_CODEC_ID_PCM_S16LE:
1995 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1996 case AV_CODEC_ID_PCM_U16BE:
1997 case AV_CODEC_ID_PCM_U16LE:
1999 case AV_CODEC_ID_PCM_S24DAUD:
2000 case AV_CODEC_ID_PCM_S24BE:
2001 case AV_CODEC_ID_PCM_S24LE:
2002 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2003 case AV_CODEC_ID_PCM_U24BE:
2004 case AV_CODEC_ID_PCM_U24LE:
2006 case AV_CODEC_ID_PCM_S32BE:
2007 case AV_CODEC_ID_PCM_S32LE:
2008 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2009 case AV_CODEC_ID_PCM_U32BE:
2010 case AV_CODEC_ID_PCM_U32LE:
2011 case AV_CODEC_ID_PCM_F32BE:
2012 case AV_CODEC_ID_PCM_F32LE:
2014 case AV_CODEC_ID_PCM_F64BE:
2015 case AV_CODEC_ID_PCM_F64LE:
2022 int av_get_bits_per_sample(enum AVCodecID codec_id)
2025 case AV_CODEC_ID_ADPCM_SBPRO_2:
2027 case AV_CODEC_ID_ADPCM_SBPRO_3:
2029 case AV_CODEC_ID_ADPCM_SBPRO_4:
2030 case AV_CODEC_ID_ADPCM_IMA_WAV:
2031 case AV_CODEC_ID_ADPCM_IMA_QT:
2032 case AV_CODEC_ID_ADPCM_SWF:
2033 case AV_CODEC_ID_ADPCM_MS:
2036 return av_get_exact_bits_per_sample(codec_id);
2040 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
2041 uint32_t tag, int bits_per_coded_sample, int frame_bytes)
2043 int bps = av_get_exact_bits_per_sample(id);
2045 /* codecs with an exact constant bits per sample */
2046 if (bps > 0 && ch > 0 && frame_bytes > 0)
2047 return (frame_bytes * 8) / (bps * ch);
2048 bps = bits_per_coded_sample;
2050 /* codecs with a fixed packet duration */
2052 case AV_CODEC_ID_ADPCM_ADX: return 32;
2053 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2054 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2055 case AV_CODEC_ID_AMR_NB:
2056 case AV_CODEC_ID_GSM:
2057 case AV_CODEC_ID_QCELP:
2058 case AV_CODEC_ID_RA_144:
2059 case AV_CODEC_ID_RA_288: return 160;
2060 case AV_CODEC_ID_IMC: return 256;
2061 case AV_CODEC_ID_AMR_WB:
2062 case AV_CODEC_ID_GSM_MS: return 320;
2063 case AV_CODEC_ID_MP1: return 384;
2064 case AV_CODEC_ID_ATRAC1: return 512;
2065 case AV_CODEC_ID_ATRAC3: return 1024;
2066 case AV_CODEC_ID_MP2:
2067 case AV_CODEC_ID_MUSEPACK7: return 1152;
2068 case AV_CODEC_ID_AC3: return 1536;
2072 /* calc from sample rate */
2073 if (id == AV_CODEC_ID_TTA)
2074 return 256 * sr / 245;
2077 /* calc from sample rate and channels */
2078 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2079 return (480 << (sr / 22050)) / ch;
2084 /* calc from block_align */
2085 if (id == AV_CODEC_ID_SIPR) {
2087 case 20: return 160;
2088 case 19: return 144;
2089 case 29: return 288;
2090 case 37: return 480;
2092 } else if (id == AV_CODEC_ID_ILBC) {
2094 case 38: return 160;
2095 case 50: return 240;
2100 if (frame_bytes > 0) {
2101 /* calc from frame_bytes only */
2102 if (id == AV_CODEC_ID_TRUESPEECH)
2103 return 240 * (frame_bytes / 32);
2104 if (id == AV_CODEC_ID_NELLYMOSER)
2105 return 256 * (frame_bytes / 64);
2108 /* calc from frame_bytes and bits_per_coded_sample */
2109 if (id == AV_CODEC_ID_ADPCM_G726)
2110 return frame_bytes * 8 / bps;
2114 /* calc from frame_bytes and channels */
2116 case AV_CODEC_ID_ADPCM_4XM:
2117 case AV_CODEC_ID_ADPCM_IMA_ISS:
2118 return (frame_bytes - 4 * ch) * 2 / ch;
2119 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2120 return (frame_bytes - 4) * 2 / ch;
2121 case AV_CODEC_ID_ADPCM_IMA_AMV:
2122 return (frame_bytes - 8) * 2 / ch;
2123 case AV_CODEC_ID_ADPCM_XA:
2124 return (frame_bytes / 128) * 224 / ch;
2125 case AV_CODEC_ID_INTERPLAY_DPCM:
2126 return (frame_bytes - 6 - ch) / ch;
2127 case AV_CODEC_ID_ROQ_DPCM:
2128 return (frame_bytes - 8) / ch;
2129 case AV_CODEC_ID_XAN_DPCM:
2130 return (frame_bytes - 2 * ch) / ch;
2131 case AV_CODEC_ID_MACE3:
2132 return 3 * frame_bytes / ch;
2133 case AV_CODEC_ID_MACE6:
2134 return 6 * frame_bytes / ch;
2135 case AV_CODEC_ID_PCM_LXF:
2136 return 2 * (frame_bytes / (5 * ch));
2140 /* calc from frame_bytes, channels, and codec_tag */
2141 if (id == AV_CODEC_ID_SOL_DPCM) {
2143 return frame_bytes / ch;
2145 return frame_bytes * 2 / ch;
2150 /* calc from frame_bytes, channels, and block_align */
2151 int blocks = frame_bytes / ba;
2153 case AV_CODEC_ID_ADPCM_IMA_WAV:
2154 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2155 case AV_CODEC_ID_ADPCM_IMA_DK3:
2156 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2157 case AV_CODEC_ID_ADPCM_IMA_DK4:
2158 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2159 case AV_CODEC_ID_ADPCM_MS:
2160 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2165 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2167 case AV_CODEC_ID_PCM_DVD:
2168 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2169 case AV_CODEC_ID_PCM_BLURAY:
2170 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2171 case AV_CODEC_ID_S302M:
2172 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2181 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2183 return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
2184 avctx->channels, avctx->block_align,
2185 avctx->codec_tag, avctx->bits_per_coded_sample,
2189 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
2191 return get_audio_frame_duration(par->codec_id, par->sample_rate,
2192 par->channels, par->block_align,
2193 par->codec_tag, par->bits_per_coded_sample,
2198 int ff_thread_init(AVCodecContext *s)
2205 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2219 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2222 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2226 #if FF_API_MISSING_SAMPLE
2227 FF_DISABLE_DEPRECATION_WARNINGS
2228 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2230 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2231 "version to the newest one from Git. If the problem still "
2232 "occurs, it means that your file has a feature which has not "
2233 "been implemented.\n", feature);
2235 av_log_ask_for_sample(avc, NULL);
2238 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2240 va_list argument_list;
2242 va_start(argument_list, msg);
2245 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2246 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2247 "of this file to ftp://upload.libav.org/incoming/ "
2248 "and contact the libav-devel mailing list.\n");
2250 va_end(argument_list);
2252 FF_ENABLE_DEPRECATION_WARNINGS
2253 #endif /* FF_API_MISSING_SAMPLE */
2255 static AVHWAccel *first_hwaccel = NULL;
2257 void av_register_hwaccel(AVHWAccel *hwaccel)
2259 AVHWAccel **p = &first_hwaccel;
2263 hwaccel->next = NULL;
2266 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2268 return hwaccel ? hwaccel->next : first_hwaccel;
2271 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2274 // There is no good way to rollback a failure to destroy the
2275 // mutex, so we ignore failures.
2276 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
2277 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
2280 avformat_mutex = NULL;
2284 void *new_codec_mutex = NULL;
2285 void *new_avformat_mutex = NULL;
2287 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
2288 return err > 0 ? AVERROR_UNKNOWN : err;
2290 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
2291 // Ignore failures to destroy the newly created mutex.
2292 cb(&new_codec_mutex, AV_LOCK_DESTROY);
2293 return err > 0 ? AVERROR_UNKNOWN : err;
2296 codec_mutex = new_codec_mutex;
2297 avformat_mutex = new_avformat_mutex;
2303 int avpriv_lock_avformat(void)
2306 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2312 int avpriv_unlock_avformat(void)
2315 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2321 unsigned int avpriv_toupper4(unsigned int x)
2323 return av_toupper(x & 0xFF) +
2324 (av_toupper((x >> 8) & 0xFF) << 8) +
2325 (av_toupper((x >> 16) & 0xFF) << 16) +
2326 (av_toupper((x >> 24) & 0xFF) << 24);
2329 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2333 dst->owner = src->owner;
2335 ret = av_frame_ref(dst->f, src->f);
2339 if (src->progress &&
2340 !(dst->progress = av_buffer_ref(src->progress))) {
2341 ff_thread_release_buffer(dst->owner, dst);
2342 return AVERROR(ENOMEM);
2350 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2353 return ff_get_buffer(avctx, f->f, flags);
2356 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2359 av_frame_unref(f->f);
2362 void ff_thread_finish_setup(AVCodecContext *avctx)
2366 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2370 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2376 int avcodec_is_open(AVCodecContext *s)
2378 return !!s->internal;
2381 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2383 uint32_t * restrict state)
2391 for (i = 0; i < 3; i++) {
2392 uint32_t tmp = *state << 8;
2393 *state = tmp + *(p++);
2394 if (tmp == 0x100 || p == end)
2399 if (p[-1] > 1 ) p += 3;
2400 else if (p[-2] ) p += 2;
2401 else if (p[-3]|(p[-1]-1)) p++;
2408 p = FFMIN(p, end) - 4;
2409 *state = AV_RB32(p);
2414 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
2416 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
2421 *size = sizeof(*props);
2423 props->vbv_delay = UINT64_MAX;
2428 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
2430 AVPacketSideData *tmp;
2431 AVCPBProperties *props;
2434 props = av_cpb_properties_alloc(&size);
2438 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
2444 avctx->coded_side_data = tmp;
2445 avctx->nb_coded_side_data++;
2447 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
2448 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
2449 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
2454 static void codec_parameters_reset(AVCodecParameters *par)
2456 av_freep(&par->extradata);
2458 memset(par, 0, sizeof(*par));
2460 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
2461 par->codec_id = AV_CODEC_ID_NONE;
2463 par->field_order = AV_FIELD_UNKNOWN;
2464 par->color_range = AVCOL_RANGE_UNSPECIFIED;
2465 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
2466 par->color_trc = AVCOL_TRC_UNSPECIFIED;
2467 par->color_space = AVCOL_SPC_UNSPECIFIED;
2468 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
2469 par->sample_aspect_ratio = (AVRational){ 0, 1 };
2472 AVCodecParameters *avcodec_parameters_alloc(void)
2474 AVCodecParameters *par = av_mallocz(sizeof(*par));
2478 codec_parameters_reset(par);
2482 void avcodec_parameters_free(AVCodecParameters **ppar)
2484 AVCodecParameters *par = *ppar;
2488 codec_parameters_reset(par);
2493 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
2495 codec_parameters_reset(dst);
2496 memcpy(dst, src, sizeof(*dst));
2498 dst->extradata = NULL;
2499 dst->extradata_size = 0;
2500 if (src->extradata) {
2501 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2502 if (!dst->extradata)
2503 return AVERROR(ENOMEM);
2504 memcpy(dst->extradata, src->extradata, src->extradata_size);
2505 dst->extradata_size = src->extradata_size;
2511 int avcodec_parameters_from_context(AVCodecParameters *par,
2512 const AVCodecContext *codec)
2514 codec_parameters_reset(par);
2516 par->codec_type = codec->codec_type;
2517 par->codec_id = codec->codec_id;
2518 par->codec_tag = codec->codec_tag;
2520 par->bit_rate = codec->bit_rate;
2521 par->bits_per_coded_sample = codec->bits_per_coded_sample;
2522 par->profile = codec->profile;
2523 par->level = codec->level;
2525 switch (par->codec_type) {
2526 case AVMEDIA_TYPE_VIDEO:
2527 par->format = codec->pix_fmt;
2528 par->width = codec->width;
2529 par->height = codec->height;
2530 par->field_order = codec->field_order;
2531 par->color_range = codec->color_range;
2532 par->color_primaries = codec->color_primaries;
2533 par->color_trc = codec->color_trc;
2534 par->color_space = codec->colorspace;
2535 par->chroma_location = codec->chroma_sample_location;
2536 par->sample_aspect_ratio = codec->sample_aspect_ratio;
2538 case AVMEDIA_TYPE_AUDIO:
2539 par->format = codec->sample_fmt;
2540 par->channel_layout = codec->channel_layout;
2541 par->channels = codec->channels;
2542 par->sample_rate = codec->sample_rate;
2543 par->block_align = codec->block_align;
2544 par->initial_padding = codec->initial_padding;
2548 if (codec->extradata) {
2549 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2550 if (!par->extradata)
2551 return AVERROR(ENOMEM);
2552 memcpy(par->extradata, codec->extradata, codec->extradata_size);
2553 par->extradata_size = codec->extradata_size;
2559 int avcodec_parameters_to_context(AVCodecContext *codec,
2560 const AVCodecParameters *par)
2562 codec->codec_type = par->codec_type;
2563 codec->codec_id = par->codec_id;
2564 codec->codec_tag = par->codec_tag;
2566 codec->bit_rate = par->bit_rate;
2567 codec->bits_per_coded_sample = par->bits_per_coded_sample;
2568 codec->profile = par->profile;
2569 codec->level = par->level;
2571 switch (par->codec_type) {
2572 case AVMEDIA_TYPE_VIDEO:
2573 codec->pix_fmt = par->format;
2574 codec->width = par->width;
2575 codec->height = par->height;
2576 codec->field_order = par->field_order;
2577 codec->color_range = par->color_range;
2578 codec->color_primaries = par->color_primaries;
2579 codec->color_trc = par->color_trc;
2580 codec->colorspace = par->color_space;
2581 codec->chroma_sample_location = par->chroma_location;
2582 codec->sample_aspect_ratio = par->sample_aspect_ratio;
2584 case AVMEDIA_TYPE_AUDIO:
2585 codec->sample_fmt = par->format;
2586 codec->channel_layout = par->channel_layout;
2587 codec->channels = par->channels;
2588 codec->sample_rate = par->sample_rate;
2589 codec->block_align = par->block_align;
2590 codec->initial_padding = par->initial_padding;
2594 if (par->extradata) {
2595 av_freep(&codec->extradata);
2596 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2597 if (!codec->extradata)
2598 return AVERROR(ENOMEM);
2599 memcpy(codec->extradata, par->extradata, par->extradata_size);
2600 codec->extradata_size = par->extradata_size;