3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/atomic.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/hwcontext.h"
38 #include "libavutil/internal.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/mem_internal.h"
41 #include "libavutil/pixdesc.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/samplefmt.h"
44 #include "libavutil/dict.h"
45 #include "libavutil/thread.h"
49 #include "libavutil/opt.h"
51 #include "mpegvideo.h"
53 #include "frame_thread_encoder.h"
56 #include "bytestream.h"
60 #include <stdatomic.h>
67 #include "libavutil/ffversion.h"
68 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
70 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
71 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
73 void * volatile * mutex = arg;
81 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
83 return AVERROR(ENOMEM);
84 if ((err = pthread_mutex_init(tmp, NULL))) {
88 if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
89 pthread_mutex_destroy(tmp);
94 if ((err = pthread_mutex_lock(*mutex)))
99 if ((err = pthread_mutex_unlock(*mutex)))
103 case AV_LOCK_DESTROY:
105 pthread_mutex_destroy(*mutex);
107 avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
112 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
114 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
118 static atomic_bool ff_avcodec_locked;
119 static atomic_int entangled_thread_counter = ATOMIC_VAR_INIT(0);
120 static void *codec_mutex;
121 static void *avformat_mutex;
123 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
126 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
131 if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
132 memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
135 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
138 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
143 if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
144 memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
147 /* encoder management */
148 static AVCodec *first_avcodec = NULL;
149 static AVCodec **last_avcodec = &first_avcodec;
151 AVCodec *av_codec_next(const AVCodec *c)
156 return first_avcodec;
159 static av_cold void avcodec_init(void)
161 static int initialized = 0;
163 if (initialized != 0)
168 ff_me_cmp_init_static();
171 int av_codec_is_encoder(const AVCodec *codec)
173 return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
176 int av_codec_is_decoder(const AVCodec *codec)
178 return codec && (codec->decode || codec->receive_frame);
181 av_cold void avcodec_register(AVCodec *codec)
188 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
190 last_avcodec = &codec->next;
192 if (codec->init_static_data)
193 codec->init_static_data(codec);
196 int ff_set_dimensions(AVCodecContext *s, int width, int height)
198 int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
203 s->coded_width = width;
204 s->coded_height = height;
205 s->width = AV_CEIL_RSHIFT(width, s->lowres);
206 s->height = AV_CEIL_RSHIFT(height, s->lowres);
211 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
213 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
216 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
218 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
221 avctx->sample_aspect_ratio = sar;
226 int ff_side_data_update_matrix_encoding(AVFrame *frame,
227 enum AVMatrixEncoding matrix_encoding)
229 AVFrameSideData *side_data;
230 enum AVMatrixEncoding *data;
232 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
234 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
235 sizeof(enum AVMatrixEncoding));
238 return AVERROR(ENOMEM);
240 data = (enum AVMatrixEncoding*)side_data->data;
241 *data = matrix_encoding;
246 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
247 int linesize_align[AV_NUM_DATA_POINTERS])
252 AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
255 w_align = 1 << desc->log2_chroma_w;
256 h_align = 1 << desc->log2_chroma_h;
259 switch (s->pix_fmt) {
260 case AV_PIX_FMT_YUV420P:
261 case AV_PIX_FMT_YUYV422:
262 case AV_PIX_FMT_YVYU422:
263 case AV_PIX_FMT_UYVY422:
264 case AV_PIX_FMT_YUV422P:
265 case AV_PIX_FMT_YUV440P:
266 case AV_PIX_FMT_YUV444P:
267 case AV_PIX_FMT_GBRP:
268 case AV_PIX_FMT_GBRAP:
269 case AV_PIX_FMT_GRAY8:
270 case AV_PIX_FMT_GRAY16BE:
271 case AV_PIX_FMT_GRAY16LE:
272 case AV_PIX_FMT_YUVJ420P:
273 case AV_PIX_FMT_YUVJ422P:
274 case AV_PIX_FMT_YUVJ440P:
275 case AV_PIX_FMT_YUVJ444P:
276 case AV_PIX_FMT_YUVA420P:
277 case AV_PIX_FMT_YUVA422P:
278 case AV_PIX_FMT_YUVA444P:
279 case AV_PIX_FMT_YUV420P9LE:
280 case AV_PIX_FMT_YUV420P9BE:
281 case AV_PIX_FMT_YUV420P10LE:
282 case AV_PIX_FMT_YUV420P10BE:
283 case AV_PIX_FMT_YUV420P12LE:
284 case AV_PIX_FMT_YUV420P12BE:
285 case AV_PIX_FMT_YUV420P14LE:
286 case AV_PIX_FMT_YUV420P14BE:
287 case AV_PIX_FMT_YUV420P16LE:
288 case AV_PIX_FMT_YUV420P16BE:
289 case AV_PIX_FMT_YUVA420P9LE:
290 case AV_PIX_FMT_YUVA420P9BE:
291 case AV_PIX_FMT_YUVA420P10LE:
292 case AV_PIX_FMT_YUVA420P10BE:
293 case AV_PIX_FMT_YUVA420P16LE:
294 case AV_PIX_FMT_YUVA420P16BE:
295 case AV_PIX_FMT_YUV422P9LE:
296 case AV_PIX_FMT_YUV422P9BE:
297 case AV_PIX_FMT_YUV422P10LE:
298 case AV_PIX_FMT_YUV422P10BE:
299 case AV_PIX_FMT_YUV422P12LE:
300 case AV_PIX_FMT_YUV422P12BE:
301 case AV_PIX_FMT_YUV422P14LE:
302 case AV_PIX_FMT_YUV422P14BE:
303 case AV_PIX_FMT_YUV422P16LE:
304 case AV_PIX_FMT_YUV422P16BE:
305 case AV_PIX_FMT_YUVA422P9LE:
306 case AV_PIX_FMT_YUVA422P9BE:
307 case AV_PIX_FMT_YUVA422P10LE:
308 case AV_PIX_FMT_YUVA422P10BE:
309 case AV_PIX_FMT_YUVA422P16LE:
310 case AV_PIX_FMT_YUVA422P16BE:
311 case AV_PIX_FMT_YUV440P10LE:
312 case AV_PIX_FMT_YUV440P10BE:
313 case AV_PIX_FMT_YUV440P12LE:
314 case AV_PIX_FMT_YUV440P12BE:
315 case AV_PIX_FMT_YUV444P9LE:
316 case AV_PIX_FMT_YUV444P9BE:
317 case AV_PIX_FMT_YUV444P10LE:
318 case AV_PIX_FMT_YUV444P10BE:
319 case AV_PIX_FMT_YUV444P12LE:
320 case AV_PIX_FMT_YUV444P12BE:
321 case AV_PIX_FMT_YUV444P14LE:
322 case AV_PIX_FMT_YUV444P14BE:
323 case AV_PIX_FMT_YUV444P16LE:
324 case AV_PIX_FMT_YUV444P16BE:
325 case AV_PIX_FMT_YUVA444P9LE:
326 case AV_PIX_FMT_YUVA444P9BE:
327 case AV_PIX_FMT_YUVA444P10LE:
328 case AV_PIX_FMT_YUVA444P10BE:
329 case AV_PIX_FMT_YUVA444P16LE:
330 case AV_PIX_FMT_YUVA444P16BE:
331 case AV_PIX_FMT_GBRP9LE:
332 case AV_PIX_FMT_GBRP9BE:
333 case AV_PIX_FMT_GBRP10LE:
334 case AV_PIX_FMT_GBRP10BE:
335 case AV_PIX_FMT_GBRP12LE:
336 case AV_PIX_FMT_GBRP12BE:
337 case AV_PIX_FMT_GBRP14LE:
338 case AV_PIX_FMT_GBRP14BE:
339 case AV_PIX_FMT_GBRP16LE:
340 case AV_PIX_FMT_GBRP16BE:
341 case AV_PIX_FMT_GBRAP12LE:
342 case AV_PIX_FMT_GBRAP12BE:
343 case AV_PIX_FMT_GBRAP16LE:
344 case AV_PIX_FMT_GBRAP16BE:
345 w_align = 16; //FIXME assume 16 pixel per macroblock
346 h_align = 16 * 2; // interlaced needs 2 macroblocks height
348 case AV_PIX_FMT_YUV411P:
349 case AV_PIX_FMT_YUVJ411P:
350 case AV_PIX_FMT_UYYVYY411:
354 case AV_PIX_FMT_YUV410P:
355 if (s->codec_id == AV_CODEC_ID_SVQ1) {
360 case AV_PIX_FMT_RGB555:
361 if (s->codec_id == AV_CODEC_ID_RPZA) {
365 if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
370 case AV_PIX_FMT_PAL8:
371 case AV_PIX_FMT_BGR8:
372 case AV_PIX_FMT_RGB8:
373 if (s->codec_id == AV_CODEC_ID_SMC ||
374 s->codec_id == AV_CODEC_ID_CINEPAK) {
378 if (s->codec_id == AV_CODEC_ID_JV ||
379 s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
384 case AV_PIX_FMT_BGR24:
385 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
386 (s->codec_id == AV_CODEC_ID_ZLIB)) {
391 case AV_PIX_FMT_RGB24:
392 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
401 if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
402 w_align = FFMAX(w_align, 8);
405 *width = FFALIGN(*width, w_align);
406 *height = FFALIGN(*height, h_align);
407 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres) {
408 // some of the optimized chroma MC reads one line too much
409 // which is also done in mpeg decoders with lowres > 0
412 // H.264 uses edge emulation for out of frame motion vectors, for this
413 // it requires a temporary area large enough to hold a 21x21 block,
414 // increasing witdth ensure that the temporary area is large enough,
415 // the next rounded up width is 32
416 *width = FFMAX(*width, 32);
419 for (i = 0; i < 4; i++)
420 linesize_align[i] = STRIDE_ALIGN;
423 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
425 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
426 int chroma_shift = desc->log2_chroma_w;
427 int linesize_align[AV_NUM_DATA_POINTERS];
430 avcodec_align_dimensions2(s, width, height, linesize_align);
431 align = FFMAX(linesize_align[0], linesize_align[3]);
432 linesize_align[1] <<= chroma_shift;
433 linesize_align[2] <<= chroma_shift;
434 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
435 *width = FFALIGN(*width, align);
438 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
440 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
441 return AVERROR(EINVAL);
444 *xpos = (pos&1) * 128;
445 *ypos = ((pos>>1)^(pos<4)) * 128;
450 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
454 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
455 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
458 return AVCHROMA_LOC_UNSPECIFIED;
461 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
462 enum AVSampleFormat sample_fmt, const uint8_t *buf,
463 int buf_size, int align)
465 int ch, planar, needed_size, ret = 0;
467 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
468 frame->nb_samples, sample_fmt,
470 if (buf_size < needed_size)
471 return AVERROR(EINVAL);
473 planar = av_sample_fmt_is_planar(sample_fmt);
474 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
475 if (!(frame->extended_data = av_mallocz_array(nb_channels,
476 sizeof(*frame->extended_data))))
477 return AVERROR(ENOMEM);
479 frame->extended_data = frame->data;
482 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
483 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
484 sample_fmt, align)) < 0) {
485 if (frame->extended_data != frame->data)
486 av_freep(&frame->extended_data);
489 if (frame->extended_data != frame->data) {
490 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
491 frame->data[ch] = frame->extended_data[ch];
497 void ff_color_frame(AVFrame *frame, const int c[4])
499 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
502 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
504 for (p = 0; p<desc->nb_components; p++) {
505 uint8_t *dst = frame->data[p];
506 int is_chroma = p == 1 || p == 2;
507 int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
508 int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
509 for (y = 0; y < height; y++) {
510 if (desc->comp[0].depth >= 9) {
511 for (x = 0; x<bytes; x++)
512 ((uint16_t*)dst)[x] = c[p];
514 memset(dst, c[p], bytes);
515 dst += frame->linesize[p];
520 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
524 for (i = 0; i < count; i++) {
525 int r = func(c, (char *)arg + i * size);
533 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
537 for (i = 0; i < count; i++) {
538 int r = func(c, arg, i, 0);
546 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
549 while (tags->pix_fmt >= 0) {
550 if (tags->fourcc == fourcc)
551 return tags->pix_fmt;
554 return AV_PIX_FMT_NONE;
557 #if FF_API_CODEC_GET_SET
558 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
559 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
560 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
561 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
562 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
564 unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
566 return codec->properties;
569 int av_codec_get_max_lowres(const AVCodec *codec)
571 return codec->max_lowres;
575 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
576 return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
579 static int64_t get_bit_rate(AVCodecContext *ctx)
584 switch (ctx->codec_type) {
585 case AVMEDIA_TYPE_VIDEO:
586 case AVMEDIA_TYPE_DATA:
587 case AVMEDIA_TYPE_SUBTITLE:
588 case AVMEDIA_TYPE_ATTACHMENT:
589 bit_rate = ctx->bit_rate;
591 case AVMEDIA_TYPE_AUDIO:
592 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
593 bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
602 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
606 ff_unlock_avcodec(codec);
608 ret = avcodec_open2(avctx, codec, options);
610 ff_lock_avcodec(avctx, codec);
614 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
617 AVDictionary *tmp = NULL;
618 const AVPixFmtDescriptor *pixdesc;
620 if (avcodec_is_open(avctx))
623 if ((!codec && !avctx->codec)) {
624 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
625 return AVERROR(EINVAL);
627 if ((codec && avctx->codec && codec != avctx->codec)) {
628 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
629 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
630 return AVERROR(EINVAL);
633 codec = avctx->codec;
635 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
636 return AVERROR(EINVAL);
639 av_dict_copy(&tmp, *options, 0);
641 ret = ff_lock_avcodec(avctx, codec);
645 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
646 if (!avctx->internal) {
647 ret = AVERROR(ENOMEM);
651 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
652 if (!avctx->internal->pool) {
653 ret = AVERROR(ENOMEM);
657 avctx->internal->to_free = av_frame_alloc();
658 if (!avctx->internal->to_free) {
659 ret = AVERROR(ENOMEM);
663 avctx->internal->compat_decode_frame = av_frame_alloc();
664 if (!avctx->internal->compat_decode_frame) {
665 ret = AVERROR(ENOMEM);
669 avctx->internal->buffer_frame = av_frame_alloc();
670 if (!avctx->internal->buffer_frame) {
671 ret = AVERROR(ENOMEM);
675 avctx->internal->buffer_pkt = av_packet_alloc();
676 if (!avctx->internal->buffer_pkt) {
677 ret = AVERROR(ENOMEM);
681 avctx->internal->ds.in_pkt = av_packet_alloc();
682 if (!avctx->internal->ds.in_pkt) {
683 ret = AVERROR(ENOMEM);
687 avctx->internal->last_pkt_props = av_packet_alloc();
688 if (!avctx->internal->last_pkt_props) {
689 ret = AVERROR(ENOMEM);
693 avctx->internal->skip_samples_multiplier = 1;
695 if (codec->priv_data_size > 0) {
696 if (!avctx->priv_data) {
697 avctx->priv_data = av_mallocz(codec->priv_data_size);
698 if (!avctx->priv_data) {
699 ret = AVERROR(ENOMEM);
702 if (codec->priv_class) {
703 *(const AVClass **)avctx->priv_data = codec->priv_class;
704 av_opt_set_defaults(avctx->priv_data);
707 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
710 avctx->priv_data = NULL;
712 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
715 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
716 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
717 ret = AVERROR(EINVAL);
721 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
722 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
723 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
724 if (avctx->coded_width && avctx->coded_height)
725 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
726 else if (avctx->width && avctx->height)
727 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
732 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
733 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
734 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
735 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
736 ff_set_dimensions(avctx, 0, 0);
739 if (avctx->width > 0 && avctx->height > 0) {
740 if (av_image_check_sar(avctx->width, avctx->height,
741 avctx->sample_aspect_ratio) < 0) {
742 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
743 avctx->sample_aspect_ratio.num,
744 avctx->sample_aspect_ratio.den);
745 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
749 /* if the decoder init function was already called previously,
750 * free the already allocated subtitle_header before overwriting it */
751 if (av_codec_is_decoder(codec))
752 av_freep(&avctx->subtitle_header);
754 if (avctx->channels > FF_SANE_NB_CHANNELS) {
755 ret = AVERROR(EINVAL);
759 avctx->codec = codec;
760 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
761 avctx->codec_id == AV_CODEC_ID_NONE) {
762 avctx->codec_type = codec->type;
763 avctx->codec_id = codec->id;
765 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
766 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
767 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
768 ret = AVERROR(EINVAL);
771 avctx->frame_number = 0;
772 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
774 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
775 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
776 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
778 av_log(avctx, AV_LOG_ERROR,
779 "The %s '%s' is experimental but experimental codecs are not enabled, "
780 "add '-strict %d' if you want to use it.\n",
781 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
782 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
783 if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
784 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
785 codec_string, codec2->name);
786 ret = AVERROR_EXPERIMENTAL;
790 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
791 (!avctx->time_base.num || !avctx->time_base.den)) {
792 avctx->time_base.num = 1;
793 avctx->time_base.den = avctx->sample_rate;
797 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
799 if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
800 ff_unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
801 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
802 ff_lock_avcodec(avctx, codec);
808 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
809 ret = ff_thread_init(avctx);
814 if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
815 avctx->thread_count = 1;
817 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
818 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
819 avctx->codec->max_lowres);
820 avctx->lowres = avctx->codec->max_lowres;
823 if (av_codec_is_encoder(avctx->codec)) {
825 #if FF_API_CODED_FRAME
826 FF_DISABLE_DEPRECATION_WARNINGS
827 avctx->coded_frame = av_frame_alloc();
828 if (!avctx->coded_frame) {
829 ret = AVERROR(ENOMEM);
832 FF_ENABLE_DEPRECATION_WARNINGS
835 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
836 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
837 ret = AVERROR(EINVAL);
841 if (avctx->codec->sample_fmts) {
842 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
843 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
845 if (avctx->channels == 1 &&
846 av_get_planar_sample_fmt(avctx->sample_fmt) ==
847 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
848 avctx->sample_fmt = avctx->codec->sample_fmts[i];
852 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
854 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
855 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
856 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
857 ret = AVERROR(EINVAL);
861 if (avctx->codec->pix_fmts) {
862 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
863 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
865 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
866 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
867 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
869 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
870 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
871 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
872 ret = AVERROR(EINVAL);
875 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
876 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
877 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
878 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
879 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
880 avctx->color_range = AVCOL_RANGE_JPEG;
882 if (avctx->codec->supported_samplerates) {
883 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
884 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
886 if (avctx->codec->supported_samplerates[i] == 0) {
887 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
889 ret = AVERROR(EINVAL);
893 if (avctx->sample_rate < 0) {
894 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
896 ret = AVERROR(EINVAL);
899 if (avctx->codec->channel_layouts) {
900 if (!avctx->channel_layout) {
901 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
903 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
904 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
906 if (avctx->codec->channel_layouts[i] == 0) {
908 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
909 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
910 ret = AVERROR(EINVAL);
915 if (avctx->channel_layout && avctx->channels) {
916 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
917 if (channels != avctx->channels) {
919 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
920 av_log(avctx, AV_LOG_ERROR,
921 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
922 buf, channels, avctx->channels);
923 ret = AVERROR(EINVAL);
926 } else if (avctx->channel_layout) {
927 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
929 if (avctx->channels < 0) {
930 av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
932 ret = AVERROR(EINVAL);
935 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
936 pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
937 if ( avctx->bits_per_raw_sample < 0
938 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
939 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
940 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
941 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
943 if (avctx->width <= 0 || avctx->height <= 0) {
944 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
945 ret = AVERROR(EINVAL);
949 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
950 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
951 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
954 if (!avctx->rc_initial_buffer_occupancy)
955 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
957 if (avctx->ticks_per_frame && avctx->time_base.num &&
958 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
959 av_log(avctx, AV_LOG_ERROR,
960 "ticks_per_frame %d too large for the timebase %d/%d.",
961 avctx->ticks_per_frame,
962 avctx->time_base.num,
963 avctx->time_base.den);
967 if (avctx->hw_frames_ctx) {
968 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
969 if (frames_ctx->format != avctx->pix_fmt) {
970 av_log(avctx, AV_LOG_ERROR,
971 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
972 ret = AVERROR(EINVAL);
975 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
976 avctx->sw_pix_fmt != frames_ctx->sw_format) {
977 av_log(avctx, AV_LOG_ERROR,
978 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
979 "and AVHWFramesContext.sw_format (%s)\n",
980 av_get_pix_fmt_name(avctx->sw_pix_fmt),
981 av_get_pix_fmt_name(frames_ctx->sw_format));
982 ret = AVERROR(EINVAL);
985 avctx->sw_pix_fmt = frames_ctx->sw_format;
989 avctx->pts_correction_num_faulty_pts =
990 avctx->pts_correction_num_faulty_dts = 0;
991 avctx->pts_correction_last_pts =
992 avctx->pts_correction_last_dts = INT64_MIN;
994 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
995 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
996 av_log(avctx, AV_LOG_WARNING,
997 "gray decoding requested but not enabled at configuration time\n");
999 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1000 || avctx->internal->frame_thread_encoder)) {
1001 ret = avctx->codec->init(avctx);
1009 if (av_codec_is_decoder(avctx->codec)) {
1010 if (!avctx->bit_rate)
1011 avctx->bit_rate = get_bit_rate(avctx);
1012 /* validate channel layout from the decoder */
1013 if (avctx->channel_layout) {
1014 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1015 if (!avctx->channels)
1016 avctx->channels = channels;
1017 else if (channels != avctx->channels) {
1019 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1020 av_log(avctx, AV_LOG_WARNING,
1021 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1022 "ignoring specified channel layout\n",
1023 buf, channels, avctx->channels);
1024 avctx->channel_layout = 0;
1027 if (avctx->channels && avctx->channels < 0 ||
1028 avctx->channels > FF_SANE_NB_CHANNELS) {
1029 ret = AVERROR(EINVAL);
1032 if (avctx->sub_charenc) {
1033 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1034 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1035 "supported with subtitles codecs\n");
1036 ret = AVERROR(EINVAL);
1038 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1039 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1040 "subtitles character encoding will be ignored\n",
1041 avctx->codec_descriptor->name);
1042 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1044 /* input character encoding is set for a text based subtitle
1045 * codec at this point */
1046 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1047 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1049 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1051 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1052 if (cd == (iconv_t)-1) {
1053 ret = AVERROR(errno);
1054 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1055 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1060 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1061 "conversion needs a libavcodec built with iconv support "
1062 "for this codec\n");
1063 ret = AVERROR(ENOSYS);
1070 #if FF_API_AVCTX_TIMEBASE
1071 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1072 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
1075 if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
1076 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
1080 ff_unlock_avcodec(codec);
1082 av_dict_free(options);
1089 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1090 avctx->codec->close(avctx);
1092 if (codec->priv_class && codec->priv_data_size)
1093 av_opt_free(avctx->priv_data);
1096 #if FF_API_CODED_FRAME
1097 FF_DISABLE_DEPRECATION_WARNINGS
1098 av_frame_free(&avctx->coded_frame);
1099 FF_ENABLE_DEPRECATION_WARNINGS
1103 av_freep(&avctx->priv_data);
1104 if (avctx->internal) {
1105 av_frame_free(&avctx->internal->to_free);
1106 av_frame_free(&avctx->internal->compat_decode_frame);
1107 av_frame_free(&avctx->internal->buffer_frame);
1108 av_packet_free(&avctx->internal->buffer_pkt);
1109 av_packet_free(&avctx->internal->last_pkt_props);
1111 av_packet_free(&avctx->internal->ds.in_pkt);
1113 av_freep(&avctx->internal->pool);
1115 av_freep(&avctx->internal);
1116 avctx->codec = NULL;
1120 void avsubtitle_free(AVSubtitle *sub)
1124 for (i = 0; i < sub->num_rects; i++) {
1125 av_freep(&sub->rects[i]->data[0]);
1126 av_freep(&sub->rects[i]->data[1]);
1127 av_freep(&sub->rects[i]->data[2]);
1128 av_freep(&sub->rects[i]->data[3]);
1129 av_freep(&sub->rects[i]->text);
1130 av_freep(&sub->rects[i]->ass);
1131 av_freep(&sub->rects[i]);
1134 av_freep(&sub->rects);
1136 memset(sub, 0, sizeof(AVSubtitle));
1139 av_cold int avcodec_close(AVCodecContext *avctx)
1146 if (avcodec_is_open(avctx)) {
1147 FramePool *pool = avctx->internal->pool;
1148 if (CONFIG_FRAME_THREAD_ENCODER &&
1149 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1150 ff_frame_thread_encoder_free(avctx);
1152 if (HAVE_THREADS && avctx->internal->thread_ctx)
1153 ff_thread_free(avctx);
1154 if (avctx->codec && avctx->codec->close)
1155 avctx->codec->close(avctx);
1156 avctx->internal->byte_buffer_size = 0;
1157 av_freep(&avctx->internal->byte_buffer);
1158 av_frame_free(&avctx->internal->to_free);
1159 av_frame_free(&avctx->internal->compat_decode_frame);
1160 av_frame_free(&avctx->internal->buffer_frame);
1161 av_packet_free(&avctx->internal->buffer_pkt);
1162 av_packet_free(&avctx->internal->last_pkt_props);
1164 av_packet_free(&avctx->internal->ds.in_pkt);
1166 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1167 av_buffer_pool_uninit(&pool->pools[i]);
1168 av_freep(&avctx->internal->pool);
1170 if (avctx->hwaccel && avctx->hwaccel->uninit)
1171 avctx->hwaccel->uninit(avctx);
1172 av_freep(&avctx->internal->hwaccel_priv_data);
1174 ff_decode_bsfs_uninit(avctx);
1176 av_freep(&avctx->internal);
1179 for (i = 0; i < avctx->nb_coded_side_data; i++)
1180 av_freep(&avctx->coded_side_data[i].data);
1181 av_freep(&avctx->coded_side_data);
1182 avctx->nb_coded_side_data = 0;
1184 av_buffer_unref(&avctx->hw_frames_ctx);
1185 av_buffer_unref(&avctx->hw_device_ctx);
1187 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1188 av_opt_free(avctx->priv_data);
1190 av_freep(&avctx->priv_data);
1191 if (av_codec_is_encoder(avctx->codec)) {
1192 av_freep(&avctx->extradata);
1193 #if FF_API_CODED_FRAME
1194 FF_DISABLE_DEPRECATION_WARNINGS
1195 av_frame_free(&avctx->coded_frame);
1196 FF_ENABLE_DEPRECATION_WARNINGS
1199 avctx->codec = NULL;
1200 avctx->active_thread_type = 0;
1205 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1208 //This is for future deprecatec codec ids, its empty since
1209 //last major bump but will fill up again over time, please don't remove it
1210 default : return id;
1214 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1216 AVCodec *p, *experimental = NULL;
1218 id= remap_deprecated_codec_id(id);
1220 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1222 if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
1229 return experimental;
1232 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1234 return find_encdec(id, 1);
1237 AVCodec *avcodec_find_encoder_by_name(const char *name)
1244 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1251 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1253 return find_encdec(id, 0);
1256 AVCodec *avcodec_find_decoder_by_name(const char *name)
1263 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1270 const char *avcodec_get_name(enum AVCodecID id)
1272 const AVCodecDescriptor *cd;
1275 if (id == AV_CODEC_ID_NONE)
1277 cd = avcodec_descriptor_get(id);
1280 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1281 codec = avcodec_find_decoder(id);
1284 codec = avcodec_find_encoder(id);
1287 return "unknown_codec";
1290 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1292 int i, len, ret = 0;
1294 #define TAG_PRINT(x) \
1295 (((x) >= '0' && (x) <= '9') || \
1296 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1297 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
1299 for (i = 0; i < 4; i++) {
1300 len = snprintf(buf, buf_size,
1301 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1303 buf_size = buf_size > len ? buf_size - len : 0;
1310 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1312 const char *codec_type;
1313 const char *codec_name;
1314 const char *profile = NULL;
1317 AVRational display_aspect_ratio;
1318 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
1320 if (!buf || buf_size <= 0)
1322 codec_type = av_get_media_type_string(enc->codec_type);
1323 codec_name = avcodec_get_name(enc->codec_id);
1324 profile = avcodec_profile_name(enc->codec_id, enc->profile);
1326 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
1328 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1330 if (enc->codec && strcmp(enc->codec->name, codec_name))
1331 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
1334 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1335 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
1336 && av_log_get_level() >= AV_LOG_VERBOSE
1338 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1339 ", %d reference frame%s",
1340 enc->refs, enc->refs > 1 ? "s" : "");
1343 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
1344 av_fourcc2str(enc->codec_tag), enc->codec_tag);
1346 switch (enc->codec_type) {
1347 case AVMEDIA_TYPE_VIDEO:
1349 char detail[256] = "(";
1351 av_strlcat(buf, separator, buf_size);
1353 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1354 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
1355 av_get_pix_fmt_name(enc->pix_fmt));
1356 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
1357 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
1358 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
1359 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
1360 av_strlcatf(detail, sizeof(detail), "%s, ",
1361 av_color_range_name(enc->color_range));
1363 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
1364 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
1365 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
1366 if (enc->colorspace != (int)enc->color_primaries ||
1367 enc->colorspace != (int)enc->color_trc) {
1369 av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
1370 av_color_space_name(enc->colorspace),
1371 av_color_primaries_name(enc->color_primaries),
1372 av_color_transfer_name(enc->color_trc));
1374 av_strlcatf(detail, sizeof(detail), "%s, ",
1375 av_get_colorspace_name(enc->colorspace));
1378 if (enc->field_order != AV_FIELD_UNKNOWN) {
1379 const char *field_order = "progressive";
1380 if (enc->field_order == AV_FIELD_TT)
1381 field_order = "top first";
1382 else if (enc->field_order == AV_FIELD_BB)
1383 field_order = "bottom first";
1384 else if (enc->field_order == AV_FIELD_TB)
1385 field_order = "top coded first (swapped)";
1386 else if (enc->field_order == AV_FIELD_BT)
1387 field_order = "bottom coded first (swapped)";
1389 av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
1392 if (av_log_get_level() >= AV_LOG_VERBOSE &&
1393 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
1394 av_strlcatf(detail, sizeof(detail), "%s, ",
1395 av_chroma_location_name(enc->chroma_sample_location));
1397 if (strlen(detail) > 1) {
1398 detail[strlen(detail) - 2] = 0;
1399 av_strlcatf(buf, buf_size, "%s)", detail);
1404 av_strlcat(buf, new_line ? separator : ", ", buf_size);
1406 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1408 enc->width, enc->height);
1410 if (av_log_get_level() >= AV_LOG_VERBOSE &&
1411 (enc->width != enc->coded_width ||
1412 enc->height != enc->coded_height))
1413 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1414 " (%dx%d)", enc->coded_width, enc->coded_height);
1416 if (enc->sample_aspect_ratio.num) {
1417 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1418 enc->width * (int64_t)enc->sample_aspect_ratio.num,
1419 enc->height * (int64_t)enc->sample_aspect_ratio.den,
1421 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1422 " [SAR %d:%d DAR %d:%d]",
1423 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1424 display_aspect_ratio.num, display_aspect_ratio.den);
1426 if (av_log_get_level() >= AV_LOG_DEBUG) {
1427 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1428 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1430 enc->time_base.num / g, enc->time_base.den / g);
1434 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1435 ", q=%d-%d", enc->qmin, enc->qmax);
1437 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
1438 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1439 ", Closed Captions");
1440 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
1441 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1445 case AVMEDIA_TYPE_AUDIO:
1446 av_strlcat(buf, separator, buf_size);
1448 if (enc->sample_rate) {
1449 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1450 "%d Hz, ", enc->sample_rate);
1452 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1453 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1454 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1455 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1457 if ( enc->bits_per_raw_sample > 0
1458 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
1459 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1460 " (%d bit)", enc->bits_per_raw_sample);
1461 if (av_log_get_level() >= AV_LOG_VERBOSE) {
1462 if (enc->initial_padding)
1463 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1464 ", delay %d", enc->initial_padding);
1465 if (enc->trailing_padding)
1466 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1467 ", padding %d", enc->trailing_padding);
1470 case AVMEDIA_TYPE_DATA:
1471 if (av_log_get_level() >= AV_LOG_DEBUG) {
1472 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1474 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1476 enc->time_base.num / g, enc->time_base.den / g);
1479 case AVMEDIA_TYPE_SUBTITLE:
1481 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1482 ", %dx%d", enc->width, enc->height);
1488 if (enc->flags & AV_CODEC_FLAG_PASS1)
1489 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1491 if (enc->flags & AV_CODEC_FLAG_PASS2)
1492 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1495 bitrate = get_bit_rate(enc);
1497 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1498 ", %"PRId64" kb/s", bitrate / 1000);
1499 } else if (enc->rc_max_rate > 0) {
1500 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1501 ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
1505 const char *av_get_profile_name(const AVCodec *codec, int profile)
1508 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1511 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1512 if (p->profile == profile)
1518 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
1520 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
1523 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
1526 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1527 if (p->profile == profile)
1533 unsigned avcodec_version(void)
1535 // av_assert0(AV_CODEC_ID_V410==164);
1536 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
1537 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
1538 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
1539 av_assert0(AV_CODEC_ID_SRT==94216);
1540 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1542 return LIBAVCODEC_VERSION_INT;
1545 const char *avcodec_configuration(void)
1547 return FFMPEG_CONFIGURATION;
1550 const char *avcodec_license(void)
1552 #define LICENSE_PREFIX "libavcodec license: "
1553 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1556 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1559 case AV_CODEC_ID_8SVX_EXP:
1560 case AV_CODEC_ID_8SVX_FIB:
1561 case AV_CODEC_ID_ADPCM_CT:
1562 case AV_CODEC_ID_ADPCM_IMA_APC:
1563 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1564 case AV_CODEC_ID_ADPCM_IMA_OKI:
1565 case AV_CODEC_ID_ADPCM_IMA_WS:
1566 case AV_CODEC_ID_ADPCM_G722:
1567 case AV_CODEC_ID_ADPCM_YAMAHA:
1568 case AV_CODEC_ID_ADPCM_AICA:
1570 case AV_CODEC_ID_DSD_LSBF:
1571 case AV_CODEC_ID_DSD_MSBF:
1572 case AV_CODEC_ID_DSD_LSBF_PLANAR:
1573 case AV_CODEC_ID_DSD_MSBF_PLANAR:
1574 case AV_CODEC_ID_PCM_ALAW:
1575 case AV_CODEC_ID_PCM_MULAW:
1576 case AV_CODEC_ID_PCM_S8:
1577 case AV_CODEC_ID_PCM_S8_PLANAR:
1578 case AV_CODEC_ID_PCM_U8:
1579 case AV_CODEC_ID_PCM_ZORK:
1580 case AV_CODEC_ID_SDX2_DPCM:
1582 case AV_CODEC_ID_PCM_S16BE:
1583 case AV_CODEC_ID_PCM_S16BE_PLANAR:
1584 case AV_CODEC_ID_PCM_S16LE:
1585 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1586 case AV_CODEC_ID_PCM_U16BE:
1587 case AV_CODEC_ID_PCM_U16LE:
1589 case AV_CODEC_ID_PCM_S24DAUD:
1590 case AV_CODEC_ID_PCM_S24BE:
1591 case AV_CODEC_ID_PCM_S24LE:
1592 case AV_CODEC_ID_PCM_S24LE_PLANAR:
1593 case AV_CODEC_ID_PCM_U24BE:
1594 case AV_CODEC_ID_PCM_U24LE:
1596 case AV_CODEC_ID_PCM_S32BE:
1597 case AV_CODEC_ID_PCM_S32LE:
1598 case AV_CODEC_ID_PCM_S32LE_PLANAR:
1599 case AV_CODEC_ID_PCM_U32BE:
1600 case AV_CODEC_ID_PCM_U32LE:
1601 case AV_CODEC_ID_PCM_F32BE:
1602 case AV_CODEC_ID_PCM_F32LE:
1603 case AV_CODEC_ID_PCM_F24LE:
1604 case AV_CODEC_ID_PCM_F16LE:
1606 case AV_CODEC_ID_PCM_F64BE:
1607 case AV_CODEC_ID_PCM_F64LE:
1608 case AV_CODEC_ID_PCM_S64BE:
1609 case AV_CODEC_ID_PCM_S64LE:
1616 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
1618 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
1619 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
1620 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
1621 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
1622 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
1623 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
1624 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
1625 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
1626 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
1627 [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
1628 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
1629 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
1631 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
1632 return AV_CODEC_ID_NONE;
1633 if (be < 0 || be > 1)
1635 return map[fmt][be];
1638 int av_get_bits_per_sample(enum AVCodecID codec_id)
1641 case AV_CODEC_ID_ADPCM_SBPRO_2:
1643 case AV_CODEC_ID_ADPCM_SBPRO_3:
1645 case AV_CODEC_ID_ADPCM_SBPRO_4:
1646 case AV_CODEC_ID_ADPCM_IMA_WAV:
1647 case AV_CODEC_ID_ADPCM_IMA_QT:
1648 case AV_CODEC_ID_ADPCM_SWF:
1649 case AV_CODEC_ID_ADPCM_MS:
1652 return av_get_exact_bits_per_sample(codec_id);
1656 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
1657 uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
1658 uint8_t * extradata, int frame_size, int frame_bytes)
1660 int bps = av_get_exact_bits_per_sample(id);
1661 int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
1663 /* codecs with an exact constant bits per sample */
1664 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
1665 return (frame_bytes * 8LL) / (bps * ch);
1666 bps = bits_per_coded_sample;
1668 /* codecs with a fixed packet duration */
1670 case AV_CODEC_ID_ADPCM_ADX: return 32;
1671 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1672 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1673 case AV_CODEC_ID_AMR_NB:
1674 case AV_CODEC_ID_EVRC:
1675 case AV_CODEC_ID_GSM:
1676 case AV_CODEC_ID_QCELP:
1677 case AV_CODEC_ID_RA_288: return 160;
1678 case AV_CODEC_ID_AMR_WB:
1679 case AV_CODEC_ID_GSM_MS: return 320;
1680 case AV_CODEC_ID_MP1: return 384;
1681 case AV_CODEC_ID_ATRAC1: return 512;
1682 case AV_CODEC_ID_ATRAC3: return 1024 * framecount;
1683 case AV_CODEC_ID_ATRAC3P: return 2048;
1684 case AV_CODEC_ID_MP2:
1685 case AV_CODEC_ID_MUSEPACK7: return 1152;
1686 case AV_CODEC_ID_AC3: return 1536;
1690 /* calc from sample rate */
1691 if (id == AV_CODEC_ID_TTA)
1692 return 256 * sr / 245;
1693 else if (id == AV_CODEC_ID_DST)
1694 return 588 * sr / 44100;
1697 /* calc from sample rate and channels */
1698 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1699 return (480 << (sr / 22050)) / ch;
1702 if (id == AV_CODEC_ID_MP3)
1703 return sr <= 24000 ? 576 : 1152;
1707 /* calc from block_align */
1708 if (id == AV_CODEC_ID_SIPR) {
1710 case 20: return 160;
1711 case 19: return 144;
1712 case 29: return 288;
1713 case 37: return 480;
1715 } else if (id == AV_CODEC_ID_ILBC) {
1717 case 38: return 160;
1718 case 50: return 240;
1723 if (frame_bytes > 0) {
1724 /* calc from frame_bytes only */
1725 if (id == AV_CODEC_ID_TRUESPEECH)
1726 return 240 * (frame_bytes / 32);
1727 if (id == AV_CODEC_ID_NELLYMOSER)
1728 return 256 * (frame_bytes / 64);
1729 if (id == AV_CODEC_ID_RA_144)
1730 return 160 * (frame_bytes / 20);
1731 if (id == AV_CODEC_ID_G723_1)
1732 return 240 * (frame_bytes / 24);
1735 /* calc from frame_bytes and bits_per_coded_sample */
1736 if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
1737 return frame_bytes * 8 / bps;
1740 if (ch > 0 && ch < INT_MAX/16) {
1741 /* calc from frame_bytes and channels */
1743 case AV_CODEC_ID_ADPCM_AFC:
1744 return frame_bytes / (9 * ch) * 16;
1745 case AV_CODEC_ID_ADPCM_PSX:
1746 case AV_CODEC_ID_ADPCM_DTK:
1747 return frame_bytes / (16 * ch) * 28;
1748 case AV_CODEC_ID_ADPCM_4XM:
1749 case AV_CODEC_ID_ADPCM_IMA_DAT4:
1750 case AV_CODEC_ID_ADPCM_IMA_ISS:
1751 return (frame_bytes - 4 * ch) * 2 / ch;
1752 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1753 return (frame_bytes - 4) * 2 / ch;
1754 case AV_CODEC_ID_ADPCM_IMA_AMV:
1755 return (frame_bytes - 8) * 2 / ch;
1756 case AV_CODEC_ID_ADPCM_THP:
1757 case AV_CODEC_ID_ADPCM_THP_LE:
1759 return frame_bytes * 14 / (8 * ch);
1761 case AV_CODEC_ID_ADPCM_XA:
1762 return (frame_bytes / 128) * 224 / ch;
1763 case AV_CODEC_ID_INTERPLAY_DPCM:
1764 return (frame_bytes - 6 - ch) / ch;
1765 case AV_CODEC_ID_ROQ_DPCM:
1766 return (frame_bytes - 8) / ch;
1767 case AV_CODEC_ID_XAN_DPCM:
1768 return (frame_bytes - 2 * ch) / ch;
1769 case AV_CODEC_ID_MACE3:
1770 return 3 * frame_bytes / ch;
1771 case AV_CODEC_ID_MACE6:
1772 return 6 * frame_bytes / ch;
1773 case AV_CODEC_ID_PCM_LXF:
1774 return 2 * (frame_bytes / (5 * ch));
1775 case AV_CODEC_ID_IAC:
1776 case AV_CODEC_ID_IMC:
1777 return 4 * frame_bytes / ch;
1781 /* calc from frame_bytes, channels, and codec_tag */
1782 if (id == AV_CODEC_ID_SOL_DPCM) {
1784 return frame_bytes / ch;
1786 return frame_bytes * 2 / ch;
1791 /* calc from frame_bytes, channels, and block_align */
1792 int blocks = frame_bytes / ba;
1794 case AV_CODEC_ID_ADPCM_IMA_WAV:
1795 if (bps < 2 || bps > 5)
1797 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
1798 case AV_CODEC_ID_ADPCM_IMA_DK3:
1799 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1800 case AV_CODEC_ID_ADPCM_IMA_DK4:
1801 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1802 case AV_CODEC_ID_ADPCM_IMA_RAD:
1803 return blocks * ((ba - 4 * ch) * 2 / ch);
1804 case AV_CODEC_ID_ADPCM_MS:
1805 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1806 case AV_CODEC_ID_ADPCM_MTAF:
1807 return blocks * (ba - 16) * 2 / ch;
1812 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1814 case AV_CODEC_ID_PCM_DVD:
1815 if(bps<4 || frame_bytes<3)
1817 return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
1818 case AV_CODEC_ID_PCM_BLURAY:
1819 if(bps<4 || frame_bytes<4)
1821 return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
1822 case AV_CODEC_ID_S302M:
1823 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1829 /* Fall back on using frame_size */
1830 if (frame_size > 1 && frame_bytes)
1833 //For WMA we currently have no other means to calculate duration thus we
1834 //do it here by assuming CBR, which is true for all known cases.
1835 if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
1836 if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
1837 return (frame_bytes * 8LL * sr) / bitrate;
1843 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1845 return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
1846 avctx->channels, avctx->block_align,
1847 avctx->codec_tag, avctx->bits_per_coded_sample,
1848 avctx->bit_rate, avctx->extradata, avctx->frame_size,
1852 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
1854 return get_audio_frame_duration(par->codec_id, par->sample_rate,
1855 par->channels, par->block_align,
1856 par->codec_tag, par->bits_per_coded_sample,
1857 par->bit_rate, par->extradata, par->frame_size,
1862 int ff_thread_init(AVCodecContext *s)
1869 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1883 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
1886 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
1890 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
1893 if (!codec->hw_configs || index < 0)
1895 for (i = 0; i <= index; i++)
1896 if (!codec->hw_configs[i])
1898 return &codec->hw_configs[index]->public;
1901 #if FF_API_USER_VISIBLE_AVHWACCEL
1902 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
1907 void av_register_hwaccel(AVHWAccel *hwaccel)
1912 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1915 // There is no good way to rollback a failure to destroy the
1916 // mutex, so we ignore failures.
1917 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
1918 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
1921 avformat_mutex = NULL;
1925 void *new_codec_mutex = NULL;
1926 void *new_avformat_mutex = NULL;
1928 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
1929 return err > 0 ? AVERROR_UNKNOWN : err;
1931 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
1932 // Ignore failures to destroy the newly created mutex.
1933 cb(&new_codec_mutex, AV_LOCK_DESTROY);
1934 return err > 0 ? AVERROR_UNKNOWN : err;
1937 codec_mutex = new_codec_mutex;
1938 avformat_mutex = new_avformat_mutex;
1944 int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
1947 if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
1951 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1955 if (atomic_fetch_add(&entangled_thread_counter, 1)) {
1956 av_log(log_ctx, AV_LOG_ERROR,
1957 "Insufficient thread locking. At least %d threads are "
1958 "calling avcodec_open2() at the same time right now.\n",
1959 atomic_load(&entangled_thread_counter));
1961 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
1962 atomic_store(&ff_avcodec_locked, 1);
1963 ff_unlock_avcodec(codec);
1964 return AVERROR(EINVAL);
1966 av_assert0(atomic_compare_exchange_strong(&ff_avcodec_locked, &exp, 1));
1970 int ff_unlock_avcodec(const AVCodec *codec)
1973 if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
1976 av_assert0(atomic_compare_exchange_strong(&ff_avcodec_locked, &exp, 0));
1977 atomic_fetch_add(&entangled_thread_counter, -1);
1979 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
1986 int avpriv_lock_avformat(void)
1989 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1995 int avpriv_unlock_avformat(void)
1998 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2004 unsigned int avpriv_toupper4(unsigned int x)
2006 return av_toupper(x & 0xFF) +
2007 (av_toupper((x >> 8) & 0xFF) << 8) +
2008 (av_toupper((x >> 16) & 0xFF) << 16) +
2009 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
2012 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2016 dst->owner[0] = src->owner[0];
2017 dst->owner[1] = src->owner[1];
2019 ret = av_frame_ref(dst->f, src->f);
2023 av_assert0(!dst->progress);
2025 if (src->progress &&
2026 !(dst->progress = av_buffer_ref(src->progress))) {
2027 ff_thread_release_buffer(dst->owner[0], dst);
2028 return AVERROR(ENOMEM);
2036 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
2038 return ff_get_format(avctx, fmt);
2041 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2043 f->owner[0] = f->owner[1] = avctx;
2044 return ff_get_buffer(avctx, f->f, flags);
2047 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2050 av_frame_unref(f->f);
2053 void ff_thread_finish_setup(AVCodecContext *avctx)
2057 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2061 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2065 int ff_thread_can_start_frame(AVCodecContext *avctx)
2070 int ff_alloc_entries(AVCodecContext *avctx, int count)
2075 void ff_reset_entries(AVCodecContext *avctx)
2079 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
2083 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
2089 int avcodec_is_open(AVCodecContext *s)
2091 return !!s->internal;
2094 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
2099 ret = av_bprint_finalize(buf, &str);
2102 if (!av_bprint_is_complete(buf)) {
2104 return AVERROR(ENOMEM);
2107 avctx->extradata = str;
2108 /* Note: the string is NUL terminated (so extradata can be read as a
2109 * string), but the ending character is not accounted in the size (in
2110 * binary formats you are likely not supposed to mux that character). When
2111 * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
2113 avctx->extradata_size = buf->len;
2117 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
2119 uint32_t *av_restrict state)
2123 av_assert0(p <= end);
2127 for (i = 0; i < 3; i++) {
2128 uint32_t tmp = *state << 8;
2129 *state = tmp + *(p++);
2130 if (tmp == 0x100 || p == end)
2135 if (p[-1] > 1 ) p += 3;
2136 else if (p[-2] ) p += 2;
2137 else if (p[-3]|(p[-1]-1)) p++;
2144 p = FFMIN(p, end) - 4;
2145 *state = AV_RB32(p);
2150 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
2152 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
2157 *size = sizeof(*props);
2159 props->vbv_delay = UINT64_MAX;
2164 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
2166 AVPacketSideData *tmp;
2167 AVCPBProperties *props;
2170 props = av_cpb_properties_alloc(&size);
2174 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
2180 avctx->coded_side_data = tmp;
2181 avctx->nb_coded_side_data++;
2183 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
2184 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
2185 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
2190 static void codec_parameters_reset(AVCodecParameters *par)
2192 av_freep(&par->extradata);
2194 memset(par, 0, sizeof(*par));
2196 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
2197 par->codec_id = AV_CODEC_ID_NONE;
2199 par->field_order = AV_FIELD_UNKNOWN;
2200 par->color_range = AVCOL_RANGE_UNSPECIFIED;
2201 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
2202 par->color_trc = AVCOL_TRC_UNSPECIFIED;
2203 par->color_space = AVCOL_SPC_UNSPECIFIED;
2204 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
2205 par->sample_aspect_ratio = (AVRational){ 0, 1 };
2206 par->profile = FF_PROFILE_UNKNOWN;
2207 par->level = FF_LEVEL_UNKNOWN;
2210 AVCodecParameters *avcodec_parameters_alloc(void)
2212 AVCodecParameters *par = av_mallocz(sizeof(*par));
2216 codec_parameters_reset(par);
2220 void avcodec_parameters_free(AVCodecParameters **ppar)
2222 AVCodecParameters *par = *ppar;
2226 codec_parameters_reset(par);
2231 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
2233 codec_parameters_reset(dst);
2234 memcpy(dst, src, sizeof(*dst));
2236 dst->extradata = NULL;
2237 dst->extradata_size = 0;
2238 if (src->extradata) {
2239 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2240 if (!dst->extradata)
2241 return AVERROR(ENOMEM);
2242 memcpy(dst->extradata, src->extradata, src->extradata_size);
2243 dst->extradata_size = src->extradata_size;
2249 int avcodec_parameters_from_context(AVCodecParameters *par,
2250 const AVCodecContext *codec)
2252 codec_parameters_reset(par);
2254 par->codec_type = codec->codec_type;
2255 par->codec_id = codec->codec_id;
2256 par->codec_tag = codec->codec_tag;
2258 par->bit_rate = codec->bit_rate;
2259 par->bits_per_coded_sample = codec->bits_per_coded_sample;
2260 par->bits_per_raw_sample = codec->bits_per_raw_sample;
2261 par->profile = codec->profile;
2262 par->level = codec->level;
2264 switch (par->codec_type) {
2265 case AVMEDIA_TYPE_VIDEO:
2266 par->format = codec->pix_fmt;
2267 par->width = codec->width;
2268 par->height = codec->height;
2269 par->field_order = codec->field_order;
2270 par->color_range = codec->color_range;
2271 par->color_primaries = codec->color_primaries;
2272 par->color_trc = codec->color_trc;
2273 par->color_space = codec->colorspace;
2274 par->chroma_location = codec->chroma_sample_location;
2275 par->sample_aspect_ratio = codec->sample_aspect_ratio;
2276 par->video_delay = codec->has_b_frames;
2278 case AVMEDIA_TYPE_AUDIO:
2279 par->format = codec->sample_fmt;
2280 par->channel_layout = codec->channel_layout;
2281 par->channels = codec->channels;
2282 par->sample_rate = codec->sample_rate;
2283 par->block_align = codec->block_align;
2284 par->frame_size = codec->frame_size;
2285 par->initial_padding = codec->initial_padding;
2286 par->trailing_padding = codec->trailing_padding;
2287 par->seek_preroll = codec->seek_preroll;
2289 case AVMEDIA_TYPE_SUBTITLE:
2290 par->width = codec->width;
2291 par->height = codec->height;
2295 if (codec->extradata) {
2296 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2297 if (!par->extradata)
2298 return AVERROR(ENOMEM);
2299 memcpy(par->extradata, codec->extradata, codec->extradata_size);
2300 par->extradata_size = codec->extradata_size;
2306 int avcodec_parameters_to_context(AVCodecContext *codec,
2307 const AVCodecParameters *par)
2309 codec->codec_type = par->codec_type;
2310 codec->codec_id = par->codec_id;
2311 codec->codec_tag = par->codec_tag;
2313 codec->bit_rate = par->bit_rate;
2314 codec->bits_per_coded_sample = par->bits_per_coded_sample;
2315 codec->bits_per_raw_sample = par->bits_per_raw_sample;
2316 codec->profile = par->profile;
2317 codec->level = par->level;
2319 switch (par->codec_type) {
2320 case AVMEDIA_TYPE_VIDEO:
2321 codec->pix_fmt = par->format;
2322 codec->width = par->width;
2323 codec->height = par->height;
2324 codec->field_order = par->field_order;
2325 codec->color_range = par->color_range;
2326 codec->color_primaries = par->color_primaries;
2327 codec->color_trc = par->color_trc;
2328 codec->colorspace = par->color_space;
2329 codec->chroma_sample_location = par->chroma_location;
2330 codec->sample_aspect_ratio = par->sample_aspect_ratio;
2331 codec->has_b_frames = par->video_delay;
2333 case AVMEDIA_TYPE_AUDIO:
2334 codec->sample_fmt = par->format;
2335 codec->channel_layout = par->channel_layout;
2336 codec->channels = par->channels;
2337 codec->sample_rate = par->sample_rate;
2338 codec->block_align = par->block_align;
2339 codec->frame_size = par->frame_size;
2341 codec->initial_padding = par->initial_padding;
2342 codec->trailing_padding = par->trailing_padding;
2343 codec->seek_preroll = par->seek_preroll;
2345 case AVMEDIA_TYPE_SUBTITLE:
2346 codec->width = par->width;
2347 codec->height = par->height;
2351 if (par->extradata) {
2352 av_freep(&codec->extradata);
2353 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2354 if (!codec->extradata)
2355 return AVERROR(ENOMEM);
2356 memcpy(codec->extradata, par->extradata, par->extradata_size);
2357 codec->extradata_size = par->extradata_size;
2363 int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
2364 void **data, size_t *sei_size)
2366 AVFrameSideData *side_data = NULL;
2370 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
2377 *sei_size = side_data->size + 11;
2378 *data = av_mallocz(*sei_size + prefix_len);
2380 return AVERROR(ENOMEM);
2381 sei_data = (uint8_t*)*data + prefix_len;
2389 * 'GA94' is standard in North America for ATSC, but hard coding
2390 * this style may not be the right thing to do -- other formats
2391 * do exist. This information is not available in the side_data
2392 * so we are going with this right now.
2394 AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
2396 sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
2399 memcpy(sei_data + 10, side_data->data, side_data->size);
2401 sei_data[side_data->size+10] = 255;
2406 int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
2408 AVRational framerate = avctx->framerate;
2409 int bits_per_coded_sample = avctx->bits_per_coded_sample;
2412 if (!(framerate.num && framerate.den))
2413 framerate = av_inv_q(avctx->time_base);
2414 if (!(framerate.num && framerate.den))
2417 if (!bits_per_coded_sample) {
2418 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
2419 bits_per_coded_sample = av_get_bits_per_pixel(desc);
2421 bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
2422 framerate.num / framerate.den;