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/internal.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/dict.h"
45 #include "libavutil/opt.h"
47 #include "frame_thread_encoder.h"
49 #include "bytestream.h"
62 #include "compat/w32pthreads.h"
64 #include "compat/os2threads.h"
67 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
68 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
70 void * volatile * mutex = arg;
78 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
80 return AVERROR(ENOMEM);
81 if ((err = pthread_mutex_init(tmp, NULL))) {
85 if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
86 pthread_mutex_destroy(tmp);
91 if ((err = pthread_mutex_lock(*mutex)))
96 if ((err = pthread_mutex_unlock(*mutex)))
100 case AV_LOCK_DESTROY:
102 pthread_mutex_destroy(*mutex);
104 avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
109 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
111 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
115 volatile int ff_avcodec_locked;
116 static int volatile entangled_thread_counter = 0;
117 static void *codec_mutex;
118 static void *avformat_mutex;
120 #if CONFIG_RAISE_MAJOR
121 # define LIBNAME "LIBAVCODEC_155"
123 # define LIBNAME "LIBAVCODEC_55"
126 #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
127 FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), LIBNAME)
129 return av_fast_realloc(ptr, size, min_size);
132 FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), LIBNAME)
134 av_fast_malloc(ptr, size, min_size);
138 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
141 if (min_size < *size)
143 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
145 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
152 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
155 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
160 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
161 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
164 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
167 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
172 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
173 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
176 /* encoder management */
177 static AVCodec *first_avcodec = NULL;
178 static AVCodec **last_avcodec = &first_avcodec;
180 AVCodec *av_codec_next(const AVCodec *c)
185 return first_avcodec;
188 static av_cold void avcodec_init(void)
190 static int initialized = 0;
192 if (initialized != 0)
197 ff_dsputil_static_init();
200 int av_codec_is_encoder(const AVCodec *codec)
202 return codec && (codec->encode_sub || codec->encode2);
205 int av_codec_is_decoder(const AVCodec *codec)
207 return codec && codec->decode;
210 av_cold void avcodec_register(AVCodec *codec)
217 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
219 last_avcodec = &codec->next;
221 if (codec->init_static_data)
222 codec->init_static_data(codec);
225 unsigned avcodec_get_edge_width(void)
230 #if FF_API_SET_DIMENSIONS
231 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
233 int ret = ff_set_dimensions(s, width, height);
235 av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
240 int ff_set_dimensions(AVCodecContext *s, int width, int height)
242 int ret = av_image_check_size(width, height, 0, s);
247 s->coded_width = width;
248 s->coded_height = height;
249 s->width = FF_CEIL_RSHIFT(width, s->lowres);
250 s->height = FF_CEIL_RSHIFT(height, s->lowres);
255 int ff_side_data_update_matrix_encoding(AVFrame *frame,
256 enum AVMatrixEncoding matrix_encoding)
258 AVFrameSideData *side_data;
259 enum AVMatrixEncoding *data;
261 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
263 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
264 sizeof(enum AVMatrixEncoding));
267 return AVERROR(ENOMEM);
269 data = (enum AVMatrixEncoding*)side_data->data;
270 *data = matrix_encoding;
275 #if HAVE_NEON || ARCH_PPC || HAVE_MMX
276 # define STRIDE_ALIGN 16
278 # define STRIDE_ALIGN 8
281 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
282 int linesize_align[AV_NUM_DATA_POINTERS])
288 switch (s->pix_fmt) {
289 case AV_PIX_FMT_YUV420P:
290 case AV_PIX_FMT_YUYV422:
291 case AV_PIX_FMT_UYVY422:
292 case AV_PIX_FMT_YUV422P:
293 case AV_PIX_FMT_YUV440P:
294 case AV_PIX_FMT_YUV444P:
295 case AV_PIX_FMT_GBRAP:
296 case AV_PIX_FMT_GBRP:
297 case AV_PIX_FMT_GRAY8:
298 case AV_PIX_FMT_GRAY16BE:
299 case AV_PIX_FMT_GRAY16LE:
300 case AV_PIX_FMT_YUVJ420P:
301 case AV_PIX_FMT_YUVJ422P:
302 case AV_PIX_FMT_YUVJ440P:
303 case AV_PIX_FMT_YUVJ444P:
304 case AV_PIX_FMT_YUVA420P:
305 case AV_PIX_FMT_YUVA422P:
306 case AV_PIX_FMT_YUVA444P:
307 case AV_PIX_FMT_YUV420P9LE:
308 case AV_PIX_FMT_YUV420P9BE:
309 case AV_PIX_FMT_YUV420P10LE:
310 case AV_PIX_FMT_YUV420P10BE:
311 case AV_PIX_FMT_YUV420P12LE:
312 case AV_PIX_FMT_YUV420P12BE:
313 case AV_PIX_FMT_YUV420P14LE:
314 case AV_PIX_FMT_YUV420P14BE:
315 case AV_PIX_FMT_YUV420P16LE:
316 case AV_PIX_FMT_YUV420P16BE:
317 case AV_PIX_FMT_YUVA420P9LE:
318 case AV_PIX_FMT_YUVA420P9BE:
319 case AV_PIX_FMT_YUVA420P10LE:
320 case AV_PIX_FMT_YUVA420P10BE:
321 case AV_PIX_FMT_YUVA420P16LE:
322 case AV_PIX_FMT_YUVA420P16BE:
323 case AV_PIX_FMT_YUV422P9LE:
324 case AV_PIX_FMT_YUV422P9BE:
325 case AV_PIX_FMT_YUV422P10LE:
326 case AV_PIX_FMT_YUV422P10BE:
327 case AV_PIX_FMT_YUV422P12LE:
328 case AV_PIX_FMT_YUV422P12BE:
329 case AV_PIX_FMT_YUV422P14LE:
330 case AV_PIX_FMT_YUV422P14BE:
331 case AV_PIX_FMT_YUV422P16LE:
332 case AV_PIX_FMT_YUV422P16BE:
333 case AV_PIX_FMT_YUVA422P9LE:
334 case AV_PIX_FMT_YUVA422P9BE:
335 case AV_PIX_FMT_YUVA422P10LE:
336 case AV_PIX_FMT_YUVA422P10BE:
337 case AV_PIX_FMT_YUVA422P16LE:
338 case AV_PIX_FMT_YUVA422P16BE:
339 case AV_PIX_FMT_YUV444P9LE:
340 case AV_PIX_FMT_YUV444P9BE:
341 case AV_PIX_FMT_YUV444P10LE:
342 case AV_PIX_FMT_YUV444P10BE:
343 case AV_PIX_FMT_YUV444P12LE:
344 case AV_PIX_FMT_YUV444P12BE:
345 case AV_PIX_FMT_YUV444P14LE:
346 case AV_PIX_FMT_YUV444P14BE:
347 case AV_PIX_FMT_YUV444P16LE:
348 case AV_PIX_FMT_YUV444P16BE:
349 case AV_PIX_FMT_YUVA444P9LE:
350 case AV_PIX_FMT_YUVA444P9BE:
351 case AV_PIX_FMT_YUVA444P10LE:
352 case AV_PIX_FMT_YUVA444P10BE:
353 case AV_PIX_FMT_YUVA444P16LE:
354 case AV_PIX_FMT_YUVA444P16BE:
355 case AV_PIX_FMT_GBRP9LE:
356 case AV_PIX_FMT_GBRP9BE:
357 case AV_PIX_FMT_GBRP10LE:
358 case AV_PIX_FMT_GBRP10BE:
359 case AV_PIX_FMT_GBRP12LE:
360 case AV_PIX_FMT_GBRP12BE:
361 case AV_PIX_FMT_GBRP14LE:
362 case AV_PIX_FMT_GBRP14BE:
363 w_align = 16; //FIXME assume 16 pixel per macroblock
364 h_align = 16 * 2; // interlaced needs 2 macroblocks height
366 case AV_PIX_FMT_YUV411P:
367 case AV_PIX_FMT_YUVJ411P:
368 case AV_PIX_FMT_UYYVYY411:
372 case AV_PIX_FMT_YUV410P:
373 if (s->codec_id == AV_CODEC_ID_SVQ1) {
378 case AV_PIX_FMT_RGB555:
379 if (s->codec_id == AV_CODEC_ID_RPZA) {
384 case AV_PIX_FMT_PAL8:
385 case AV_PIX_FMT_BGR8:
386 case AV_PIX_FMT_RGB8:
387 if (s->codec_id == AV_CODEC_ID_SMC ||
388 s->codec_id == AV_CODEC_ID_CINEPAK) {
393 case AV_PIX_FMT_BGR24:
394 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
395 (s->codec_id == AV_CODEC_ID_ZLIB)) {
400 case AV_PIX_FMT_RGB24:
401 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
412 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
413 w_align = FFMAX(w_align, 8);
416 *width = FFALIGN(*width, w_align);
417 *height = FFALIGN(*height, h_align);
418 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
419 // some of the optimized chroma MC reads one line too much
420 // which is also done in mpeg decoders with lowres > 0
423 for (i = 0; i < 4; i++)
424 linesize_align[i] = STRIDE_ALIGN;
427 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
429 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
430 int chroma_shift = desc->log2_chroma_w;
431 int linesize_align[AV_NUM_DATA_POINTERS];
434 avcodec_align_dimensions2(s, width, height, linesize_align);
435 align = FFMAX(linesize_align[0], linesize_align[3]);
436 linesize_align[1] <<= chroma_shift;
437 linesize_align[2] <<= chroma_shift;
438 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
439 *width = FFALIGN(*width, align);
442 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
444 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
445 return AVERROR(EINVAL);
448 *xpos = (pos&1) * 128;
449 *ypos = ((pos>>1)^(pos<4)) * 128;
454 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
458 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
459 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
462 return AVCHROMA_LOC_UNSPECIFIED;
465 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
466 enum AVSampleFormat sample_fmt, const uint8_t *buf,
467 int buf_size, int align)
469 int ch, planar, needed_size, ret = 0;
471 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
472 frame->nb_samples, sample_fmt,
474 if (buf_size < needed_size)
475 return AVERROR(EINVAL);
477 planar = av_sample_fmt_is_planar(sample_fmt);
478 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
479 if (!(frame->extended_data = av_mallocz(nb_channels *
480 sizeof(*frame->extended_data))))
481 return AVERROR(ENOMEM);
483 frame->extended_data = frame->data;
486 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
487 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
488 sample_fmt, align)) < 0) {
489 if (frame->extended_data != frame->data)
490 av_freep(&frame->extended_data);
493 if (frame->extended_data != frame->data) {
494 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
495 frame->data[ch] = frame->extended_data[ch];
501 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
503 FramePool *pool = avctx->internal->pool;
506 switch (avctx->codec_type) {
507 case AVMEDIA_TYPE_VIDEO: {
510 int w = frame->width;
511 int h = frame->height;
512 int tmpsize, unaligned;
514 if (pool->format == frame->format &&
515 pool->width == frame->width && pool->height == frame->height)
518 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
520 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
526 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
527 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
528 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
529 // increase alignment of w for next try (rhs gives the lowest bit set in w)
533 for (i = 0; i < 4; i++)
534 unaligned |= picture.linesize[i] % pool->stride_align[i];
537 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
538 NULL, picture.linesize);
542 for (i = 0; i < 3 && picture.data[i + 1]; i++)
543 size[i] = picture.data[i + 1] - picture.data[i];
544 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
546 for (i = 0; i < 4; i++) {
547 av_buffer_pool_uninit(&pool->pools[i]);
548 pool->linesize[i] = picture.linesize[i];
550 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
551 CONFIG_MEMORY_POISONING ?
554 if (!pool->pools[i]) {
555 ret = AVERROR(ENOMEM);
560 pool->format = frame->format;
561 pool->width = frame->width;
562 pool->height = frame->height;
566 case AVMEDIA_TYPE_AUDIO: {
567 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
568 int planar = av_sample_fmt_is_planar(frame->format);
569 int planes = planar ? ch : 1;
571 if (pool->format == frame->format && pool->planes == planes &&
572 pool->channels == ch && frame->nb_samples == pool->samples)
575 av_buffer_pool_uninit(&pool->pools[0]);
576 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
577 frame->nb_samples, frame->format, 0);
581 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
582 if (!pool->pools[0]) {
583 ret = AVERROR(ENOMEM);
587 pool->format = frame->format;
588 pool->planes = planes;
590 pool->samples = frame->nb_samples;
593 default: av_assert0(0);
597 for (i = 0; i < 4; i++)
598 av_buffer_pool_uninit(&pool->pools[i]);
600 pool->planes = pool->channels = pool->samples = 0;
601 pool->width = pool->height = 0;
605 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
607 FramePool *pool = avctx->internal->pool;
608 int planes = pool->planes;
611 frame->linesize[0] = pool->linesize[0];
613 if (planes > AV_NUM_DATA_POINTERS) {
614 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
615 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
616 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
617 sizeof(*frame->extended_buf));
618 if (!frame->extended_data || !frame->extended_buf) {
619 av_freep(&frame->extended_data);
620 av_freep(&frame->extended_buf);
621 return AVERROR(ENOMEM);
624 frame->extended_data = frame->data;
625 av_assert0(frame->nb_extended_buf == 0);
628 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
629 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
632 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
634 for (i = 0; i < frame->nb_extended_buf; i++) {
635 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
636 if (!frame->extended_buf[i])
638 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
641 if (avctx->debug & FF_DEBUG_BUFFERS)
642 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
646 av_frame_unref(frame);
647 return AVERROR(ENOMEM);
650 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
652 FramePool *pool = s->internal->pool;
653 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
654 int pixel_size = desc->comp[0].step_minus1 + 1;
655 int h_chroma_shift, v_chroma_shift;
658 if (pic->data[0] != NULL) {
659 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
663 memset(pic->data, 0, sizeof(pic->data));
664 pic->extended_data = pic->data;
666 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
668 for (i = 0; i < 4 && pool->pools[i]; i++) {
669 const int h_shift = i == 0 ? 0 : h_chroma_shift;
670 const int v_shift = i == 0 ? 0 : v_chroma_shift;
671 int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
673 pic->linesize[i] = pool->linesize[i];
675 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
679 // no edge if EDGE EMU or not planar YUV
680 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
681 pic->data[i] = pic->buf[i]->data;
683 pic->data[i] = pic->buf[i]->data +
684 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
685 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
688 for (; i < AV_NUM_DATA_POINTERS; i++) {
690 pic->linesize[i] = 0;
692 if (pic->data[1] && !pic->data[2])
693 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
695 if (s->debug & FF_DEBUG_BUFFERS)
696 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
701 return AVERROR(ENOMEM);
704 void avpriv_color_frame(AVFrame *frame, const int c[4])
706 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
709 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
711 for (p = 0; p<desc->nb_components; p++) {
712 uint8_t *dst = frame->data[p];
713 int is_chroma = p == 1 || p == 2;
714 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
715 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
716 for (y = 0; y < height; y++) {
717 if (desc->comp[0].depth_minus1 >= 8) {
718 for (x = 0; x<bytes; x++)
719 ((uint16_t*)dst)[x] = c[p];
721 memset(dst, c[p], bytes);
722 dst += frame->linesize[p];
727 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
731 if ((ret = update_frame_pool(avctx, frame)) < 0)
734 #if FF_API_GET_BUFFER
735 FF_DISABLE_DEPRECATION_WARNINGS
736 frame->type = FF_BUFFER_TYPE_INTERNAL;
737 FF_ENABLE_DEPRECATION_WARNINGS
740 switch (avctx->codec_type) {
741 case AVMEDIA_TYPE_VIDEO:
742 return video_get_buffer(avctx, frame);
743 case AVMEDIA_TYPE_AUDIO:
744 return audio_get_buffer(avctx, frame);
750 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
752 if (avctx->internal->pkt) {
753 frame->pkt_pts = avctx->internal->pkt->pts;
754 av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
755 av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
756 av_frame_set_pkt_size (frame, avctx->internal->pkt->size);
758 frame->pkt_pts = AV_NOPTS_VALUE;
759 av_frame_set_pkt_pos (frame, -1);
760 av_frame_set_pkt_duration(frame, 0);
761 av_frame_set_pkt_size (frame, -1);
763 frame->reordered_opaque = avctx->reordered_opaque;
765 switch (avctx->codec->type) {
766 case AVMEDIA_TYPE_VIDEO:
767 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
768 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
769 if (frame->format < 0)
770 frame->format = avctx->pix_fmt;
771 if (!frame->sample_aspect_ratio.num)
772 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
773 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
774 av_frame_set_colorspace(frame, avctx->colorspace);
775 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
776 av_frame_set_color_range(frame, avctx->color_range);
778 case AVMEDIA_TYPE_AUDIO:
779 if (!frame->sample_rate)
780 frame->sample_rate = avctx->sample_rate;
781 if (frame->format < 0)
782 frame->format = avctx->sample_fmt;
783 if (!frame->channel_layout) {
784 if (avctx->channel_layout) {
785 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
787 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
789 return AVERROR(EINVAL);
792 frame->channel_layout = avctx->channel_layout;
794 if (avctx->channels > FF_SANE_NB_CHANNELS) {
795 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
797 return AVERROR(ENOSYS);
801 av_frame_set_channels(frame, avctx->channels);
807 #if FF_API_GET_BUFFER
808 FF_DISABLE_DEPRECATION_WARNINGS
809 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
811 return avcodec_default_get_buffer2(avctx, frame, 0);
814 typedef struct CompatReleaseBufPriv {
815 AVCodecContext avctx;
817 } CompatReleaseBufPriv;
819 static void compat_free_buffer(void *opaque, uint8_t *data)
821 CompatReleaseBufPriv *priv = opaque;
822 if (priv->avctx.release_buffer)
823 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
827 static void compat_release_buffer(void *opaque, uint8_t *data)
829 AVBufferRef *buf = opaque;
830 av_buffer_unref(&buf);
832 FF_ENABLE_DEPRECATION_WARNINGS
835 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
839 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
840 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
841 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
842 return AVERROR(EINVAL);
845 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
848 #if FF_API_GET_BUFFER
849 FF_DISABLE_DEPRECATION_WARNINGS
851 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
852 * We wrap each plane in its own AVBuffer. Each of those has a reference to
853 * a dummy AVBuffer as its private data, unreffing it on free.
854 * When all the planes are freed, the dummy buffer's free callback calls
857 if (avctx->get_buffer) {
858 CompatReleaseBufPriv *priv = NULL;
859 AVBufferRef *dummy_buf = NULL;
862 if (flags & AV_GET_BUFFER_FLAG_REF)
863 frame->reference = 1;
865 ret = avctx->get_buffer(avctx, frame);
869 /* return if the buffers are already set up
870 * this would happen e.g. when a custom get_buffer() calls
871 * avcodec_default_get_buffer
876 priv = av_mallocz(sizeof(*priv));
878 ret = AVERROR(ENOMEM);
881 priv->avctx = *avctx;
882 priv->frame = *frame;
884 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
886 ret = AVERROR(ENOMEM);
890 #define WRAP_PLANE(ref_out, data, data_size) \
892 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
894 ret = AVERROR(ENOMEM); \
897 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
900 av_frame_unref(frame); \
901 ret = AVERROR(ENOMEM); \
906 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
907 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
909 planes = av_pix_fmt_count_planes(frame->format);
910 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
911 check for allocated buffers: make libavcodec happy */
912 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
914 if (!desc || planes <= 0) {
915 ret = AVERROR(EINVAL);
919 for (i = 0; i < planes; i++) {
920 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
921 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
923 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
926 int planar = av_sample_fmt_is_planar(frame->format);
927 planes = planar ? avctx->channels : 1;
929 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
930 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
931 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
932 frame->nb_extended_buf);
933 if (!frame->extended_buf) {
934 ret = AVERROR(ENOMEM);
939 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
940 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
942 for (i = 0; i < frame->nb_extended_buf; i++)
943 WRAP_PLANE(frame->extended_buf[i],
944 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
948 av_buffer_unref(&dummy_buf);
951 frame->width = avctx->width;
952 frame->height = avctx->height;
957 avctx->release_buffer(avctx, frame);
959 av_buffer_unref(&dummy_buf);
962 FF_ENABLE_DEPRECATION_WARNINGS
965 ret = avctx->get_buffer2(avctx, frame, flags);
967 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
968 frame->width = avctx->width;
969 frame->height = avctx->height;
975 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
977 int ret = get_buffer_internal(avctx, frame, flags);
979 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
983 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
988 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
990 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
991 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
992 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
993 av_frame_unref(frame);
996 ff_init_buffer_info(avctx, frame);
999 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1001 if (av_frame_is_writable(frame))
1004 av_frame_move_ref(&tmp, frame);
1006 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1008 av_frame_unref(&tmp);
1012 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
1013 frame->format, frame->width, frame->height);
1015 av_frame_unref(&tmp);
1020 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1022 int ret = reget_buffer_internal(avctx, frame);
1024 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1028 #if FF_API_GET_BUFFER
1029 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
1031 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
1033 av_frame_unref(pic);
1036 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1043 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1047 for (i = 0; i < count; i++) {
1048 int r = func(c, (char *)arg + i * size);
1055 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1059 for (i = 0; i < count; i++) {
1060 int r = func(c, arg, i, 0);
1067 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1069 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1070 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1073 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1075 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1080 #if FF_API_AVFRAME_LAVC
1081 void avcodec_get_frame_defaults(AVFrame *frame)
1083 #if LIBAVCODEC_VERSION_MAJOR >= 55
1084 // extended_data should explicitly be freed when needed, this code is unsafe currently
1085 // also this is not compatible to the <55 ABI/API
1086 if (frame->extended_data != frame->data && 0)
1087 av_freep(&frame->extended_data);
1090 memset(frame, 0, sizeof(AVFrame));
1091 av_frame_unref(frame);
1094 AVFrame *avcodec_alloc_frame(void)
1096 return av_frame_alloc();
1099 void avcodec_free_frame(AVFrame **frame)
1101 av_frame_free(frame);
1105 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1106 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1107 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1108 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1110 int av_codec_get_max_lowres(const AVCodec *codec)
1112 return codec->max_lowres;
1115 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1117 memset(sub, 0, sizeof(*sub));
1118 sub->pts = AV_NOPTS_VALUE;
1121 static int get_bit_rate(AVCodecContext *ctx)
1124 int bits_per_sample;
1126 switch (ctx->codec_type) {
1127 case AVMEDIA_TYPE_VIDEO:
1128 case AVMEDIA_TYPE_DATA:
1129 case AVMEDIA_TYPE_SUBTITLE:
1130 case AVMEDIA_TYPE_ATTACHMENT:
1131 bit_rate = ctx->bit_rate;
1133 case AVMEDIA_TYPE_AUDIO:
1134 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1135 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1144 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1148 ff_unlock_avcodec();
1150 ret = avcodec_open2(avctx, codec, options);
1152 ff_lock_avcodec(avctx);
1156 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1159 AVDictionary *tmp = NULL;
1161 if (avcodec_is_open(avctx))
1164 if ((!codec && !avctx->codec)) {
1165 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1166 return AVERROR(EINVAL);
1168 if ((codec && avctx->codec && codec != avctx->codec)) {
1169 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1170 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1171 return AVERROR(EINVAL);
1174 codec = avctx->codec;
1176 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1177 return AVERROR(EINVAL);
1180 av_dict_copy(&tmp, *options, 0);
1182 ret = ff_lock_avcodec(avctx);
1186 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1187 if (!avctx->internal) {
1188 ret = AVERROR(ENOMEM);
1192 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1193 if (!avctx->internal->pool) {
1194 ret = AVERROR(ENOMEM);
1198 avctx->internal->to_free = av_frame_alloc();
1199 if (!avctx->internal->to_free) {
1200 ret = AVERROR(ENOMEM);
1204 if (codec->priv_data_size > 0) {
1205 if (!avctx->priv_data) {
1206 avctx->priv_data = av_mallocz(codec->priv_data_size);
1207 if (!avctx->priv_data) {
1208 ret = AVERROR(ENOMEM);
1211 if (codec->priv_class) {
1212 *(const AVClass **)avctx->priv_data = codec->priv_class;
1213 av_opt_set_defaults(avctx->priv_data);
1216 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1219 avctx->priv_data = NULL;
1221 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1224 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1225 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1226 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1227 if (avctx->coded_width && avctx->coded_height)
1228 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1229 else if (avctx->width && avctx->height)
1230 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1235 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1236 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1237 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1238 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1239 ff_set_dimensions(avctx, 0, 0);
1242 /* if the decoder init function was already called previously,
1243 * free the already allocated subtitle_header before overwriting it */
1244 if (av_codec_is_decoder(codec))
1245 av_freep(&avctx->subtitle_header);
1247 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1248 ret = AVERROR(EINVAL);
1252 avctx->codec = codec;
1253 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1254 avctx->codec_id == AV_CODEC_ID_NONE) {
1255 avctx->codec_type = codec->type;
1256 avctx->codec_id = codec->id;
1258 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1259 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1260 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1261 ret = AVERROR(EINVAL);
1264 avctx->frame_number = 0;
1265 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1267 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1268 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1269 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1271 av_log(avctx, AV_LOG_ERROR,
1272 "The %s '%s' is experimental but experimental codecs are not enabled, "
1273 "add '-strict %d' if you want to use it.\n",
1274 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1275 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1276 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1277 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1278 codec_string, codec2->name);
1279 ret = AVERROR_EXPERIMENTAL;
1283 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1284 (!avctx->time_base.num || !avctx->time_base.den)) {
1285 avctx->time_base.num = 1;
1286 avctx->time_base.den = avctx->sample_rate;
1290 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1292 if (CONFIG_FRAME_THREAD_ENCODER) {
1293 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1294 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1295 ff_lock_avcodec(avctx);
1301 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1302 ret = ff_thread_init(avctx);
1307 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1308 avctx->thread_count = 1;
1310 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1311 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1312 avctx->codec->max_lowres);
1313 ret = AVERROR(EINVAL);
1317 if (av_codec_is_encoder(avctx->codec)) {
1319 if (avctx->codec->sample_fmts) {
1320 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1321 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1323 if (avctx->channels == 1 &&
1324 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1325 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1326 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1330 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1332 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1333 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1334 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1335 ret = AVERROR(EINVAL);
1339 if (avctx->codec->pix_fmts) {
1340 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1341 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1343 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1344 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1345 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1347 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1348 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1349 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1350 ret = AVERROR(EINVAL);
1354 if (avctx->codec->supported_samplerates) {
1355 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1356 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1358 if (avctx->codec->supported_samplerates[i] == 0) {
1359 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1360 avctx->sample_rate);
1361 ret = AVERROR(EINVAL);
1365 if (avctx->codec->channel_layouts) {
1366 if (!avctx->channel_layout) {
1367 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1369 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1370 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1372 if (avctx->codec->channel_layouts[i] == 0) {
1374 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1375 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1376 ret = AVERROR(EINVAL);
1381 if (avctx->channel_layout && avctx->channels) {
1382 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1383 if (channels != avctx->channels) {
1385 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1386 av_log(avctx, AV_LOG_ERROR,
1387 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1388 buf, channels, avctx->channels);
1389 ret = AVERROR(EINVAL);
1392 } else if (avctx->channel_layout) {
1393 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1395 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1396 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1398 if (avctx->width <= 0 || avctx->height <= 0) {
1399 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1400 ret = AVERROR(EINVAL);
1404 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1405 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1406 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1409 if (!avctx->rc_initial_buffer_occupancy)
1410 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1413 avctx->pts_correction_num_faulty_pts =
1414 avctx->pts_correction_num_faulty_dts = 0;
1415 avctx->pts_correction_last_pts =
1416 avctx->pts_correction_last_dts = INT64_MIN;
1418 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1419 || avctx->internal->frame_thread_encoder)) {
1420 ret = avctx->codec->init(avctx);
1428 if (av_codec_is_decoder(avctx->codec)) {
1429 if (!avctx->bit_rate)
1430 avctx->bit_rate = get_bit_rate(avctx);
1431 /* validate channel layout from the decoder */
1432 if (avctx->channel_layout) {
1433 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1434 if (!avctx->channels)
1435 avctx->channels = channels;
1436 else if (channels != avctx->channels) {
1438 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1439 av_log(avctx, AV_LOG_WARNING,
1440 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1441 "ignoring specified channel layout\n",
1442 buf, channels, avctx->channels);
1443 avctx->channel_layout = 0;
1446 if (avctx->channels && avctx->channels < 0 ||
1447 avctx->channels > FF_SANE_NB_CHANNELS) {
1448 ret = AVERROR(EINVAL);
1451 if (avctx->sub_charenc) {
1452 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1453 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1454 "supported with subtitles codecs\n");
1455 ret = AVERROR(EINVAL);
1457 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1458 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1459 "subtitles character encoding will be ignored\n",
1460 avctx->codec_descriptor->name);
1461 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1463 /* input character encoding is set for a text based subtitle
1464 * codec at this point */
1465 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1466 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1468 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1470 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1471 if (cd == (iconv_t)-1) {
1472 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1473 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1474 ret = AVERROR(errno);
1479 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1480 "conversion needs a libavcodec built with iconv support "
1481 "for this codec\n");
1482 ret = AVERROR(ENOSYS);
1490 ff_unlock_avcodec();
1492 av_dict_free(options);
1499 av_freep(&avctx->priv_data);
1500 if (avctx->internal) {
1501 av_frame_free(&avctx->internal->to_free);
1502 av_freep(&avctx->internal->pool);
1504 av_freep(&avctx->internal);
1505 avctx->codec = NULL;
1509 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1511 if (avpkt->size < 0) {
1512 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1513 return AVERROR(EINVAL);
1515 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1516 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1517 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1518 return AVERROR(EINVAL);
1522 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1523 if (!avpkt->data || avpkt->size < size) {
1524 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1525 avpkt->data = avctx->internal->byte_buffer;
1526 avpkt->size = avctx->internal->byte_buffer_size;
1527 avpkt->destruct = NULL;
1532 AVBufferRef *buf = avpkt->buf;
1533 #if FF_API_DESTRUCT_PACKET
1534 FF_DISABLE_DEPRECATION_WARNINGS
1535 void *destruct = avpkt->destruct;
1536 FF_ENABLE_DEPRECATION_WARNINGS
1539 if (avpkt->size < size) {
1540 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1541 return AVERROR(EINVAL);
1544 av_init_packet(avpkt);
1545 #if FF_API_DESTRUCT_PACKET
1546 FF_DISABLE_DEPRECATION_WARNINGS
1547 avpkt->destruct = destruct;
1548 FF_ENABLE_DEPRECATION_WARNINGS
1554 int ret = av_new_packet(avpkt, size);
1556 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1561 int ff_alloc_packet(AVPacket *avpkt, int size)
1563 return ff_alloc_packet2(NULL, avpkt, size);
1567 * Pad last frame with silence.
1569 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1571 AVFrame *frame = NULL;
1574 if (!(frame = av_frame_alloc()))
1575 return AVERROR(ENOMEM);
1577 frame->format = src->format;
1578 frame->channel_layout = src->channel_layout;
1579 av_frame_set_channels(frame, av_frame_get_channels(src));
1580 frame->nb_samples = s->frame_size;
1581 ret = av_frame_get_buffer(frame, 32);
1585 ret = av_frame_copy_props(frame, src);
1589 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1590 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1592 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1593 frame->nb_samples - src->nb_samples,
1594 s->channels, s->sample_fmt)) < 0)
1602 av_frame_free(&frame);
1606 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1608 const AVFrame *frame,
1609 int *got_packet_ptr)
1612 AVFrame *padded_frame = NULL;
1614 AVPacket user_pkt = *avpkt;
1615 int needs_realloc = !user_pkt.data;
1617 *got_packet_ptr = 0;
1619 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1620 av_free_packet(avpkt);
1621 av_init_packet(avpkt);
1625 /* ensure that extended_data is properly set */
1626 if (frame && !frame->extended_data) {
1627 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1628 avctx->channels > AV_NUM_DATA_POINTERS) {
1629 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1630 "with more than %d channels, but extended_data is not set.\n",
1631 AV_NUM_DATA_POINTERS);
1632 return AVERROR(EINVAL);
1634 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1637 tmp.extended_data = tmp.data;
1641 /* check for valid frame size */
1643 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1644 if (frame->nb_samples > avctx->frame_size) {
1645 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1646 return AVERROR(EINVAL);
1648 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1649 if (frame->nb_samples < avctx->frame_size &&
1650 !avctx->internal->last_audio_frame) {
1651 ret = pad_last_frame(avctx, &padded_frame, frame);
1655 frame = padded_frame;
1656 avctx->internal->last_audio_frame = 1;
1659 if (frame->nb_samples != avctx->frame_size) {
1660 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1661 ret = AVERROR(EINVAL);
1667 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1669 if (*got_packet_ptr) {
1670 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1671 if (avpkt->pts == AV_NOPTS_VALUE)
1672 avpkt->pts = frame->pts;
1673 if (!avpkt->duration)
1674 avpkt->duration = ff_samples_to_time_base(avctx,
1677 avpkt->dts = avpkt->pts;
1682 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1684 if (user_pkt.data) {
1685 if (user_pkt.size >= avpkt->size) {
1686 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1688 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1689 avpkt->size = user_pkt.size;
1692 avpkt->buf = user_pkt.buf;
1693 avpkt->data = user_pkt.data;
1694 avpkt->destruct = user_pkt.destruct;
1696 if (av_dup_packet(avpkt) < 0) {
1697 ret = AVERROR(ENOMEM);
1703 if (needs_realloc && avpkt->data) {
1704 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1706 avpkt->data = avpkt->buf->data;
1709 avctx->frame_number++;
1712 if (ret < 0 || !*got_packet_ptr) {
1713 av_free_packet(avpkt);
1714 av_init_packet(avpkt);
1718 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1719 * this needs to be moved to the encoders, but for now we can do it
1720 * here to simplify things */
1721 avpkt->flags |= AV_PKT_FLAG_KEY;
1724 av_frame_free(&padded_frame);
1729 #if FF_API_OLD_ENCODE_AUDIO
1730 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1731 uint8_t *buf, int buf_size,
1732 const short *samples)
1736 int ret, samples_size, got_packet;
1738 av_init_packet(&pkt);
1740 pkt.size = buf_size;
1743 frame = av_frame_alloc();
1745 return AVERROR(ENOMEM);
1747 if (avctx->frame_size) {
1748 frame->nb_samples = avctx->frame_size;
1750 /* if frame_size is not set, the number of samples must be
1751 * calculated from the buffer size */
1753 if (!av_get_bits_per_sample(avctx->codec_id)) {
1754 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1755 "support this codec\n");
1756 av_frame_free(&frame);
1757 return AVERROR(EINVAL);
1759 nb_samples = (int64_t)buf_size * 8 /
1760 (av_get_bits_per_sample(avctx->codec_id) *
1762 if (nb_samples >= INT_MAX) {
1763 av_frame_free(&frame);
1764 return AVERROR(EINVAL);
1766 frame->nb_samples = nb_samples;
1769 /* it is assumed that the samples buffer is large enough based on the
1770 * relevant parameters */
1771 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1773 avctx->sample_fmt, 1);
1774 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1776 (const uint8_t *)samples,
1777 samples_size, 1)) < 0) {
1778 av_frame_free(&frame);
1782 /* fabricate frame pts from sample count.
1783 * this is needed because the avcodec_encode_audio() API does not have
1784 * a way for the user to provide pts */
1785 if (avctx->sample_rate && avctx->time_base.num)
1786 frame->pts = ff_samples_to_time_base(avctx,
1787 avctx->internal->sample_count);
1789 frame->pts = AV_NOPTS_VALUE;
1790 avctx->internal->sample_count += frame->nb_samples;
1796 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1797 if (!ret && got_packet && avctx->coded_frame) {
1798 avctx->coded_frame->pts = pkt.pts;
1799 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1801 /* free any side data since we cannot return it */
1802 av_packet_free_side_data(&pkt);
1804 if (frame && frame->extended_data != frame->data)
1805 av_freep(&frame->extended_data);
1807 av_frame_free(&frame);
1808 return ret ? ret : pkt.size;
1813 #if FF_API_OLD_ENCODE_VIDEO
1814 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1815 const AVFrame *pict)
1818 int ret, got_packet = 0;
1820 if (buf_size < FF_MIN_BUFFER_SIZE) {
1821 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1825 av_init_packet(&pkt);
1827 pkt.size = buf_size;
1829 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1830 if (!ret && got_packet && avctx->coded_frame) {
1831 avctx->coded_frame->pts = pkt.pts;
1832 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1835 /* free any side data since we cannot return it */
1836 if (pkt.side_data_elems > 0) {
1838 for (i = 0; i < pkt.side_data_elems; i++)
1839 av_free(pkt.side_data[i].data);
1840 av_freep(&pkt.side_data);
1841 pkt.side_data_elems = 0;
1844 return ret ? ret : pkt.size;
1849 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1851 const AVFrame *frame,
1852 int *got_packet_ptr)
1855 AVPacket user_pkt = *avpkt;
1856 int needs_realloc = !user_pkt.data;
1858 *got_packet_ptr = 0;
1860 if(CONFIG_FRAME_THREAD_ENCODER &&
1861 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1862 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1864 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1865 avctx->stats_out[0] = '\0';
1867 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1868 av_free_packet(avpkt);
1869 av_init_packet(avpkt);
1874 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1875 return AVERROR(EINVAL);
1877 av_assert0(avctx->codec->encode2);
1879 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1880 av_assert0(ret <= 0);
1882 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1884 if (user_pkt.data) {
1885 if (user_pkt.size >= avpkt->size) {
1886 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1888 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1889 avpkt->size = user_pkt.size;
1892 avpkt->buf = user_pkt.buf;
1893 avpkt->data = user_pkt.data;
1894 avpkt->destruct = user_pkt.destruct;
1896 if (av_dup_packet(avpkt) < 0) {
1897 ret = AVERROR(ENOMEM);
1903 if (!*got_packet_ptr)
1905 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1906 avpkt->pts = avpkt->dts = frame->pts;
1908 if (needs_realloc && avpkt->data) {
1909 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1911 avpkt->data = avpkt->buf->data;
1914 avctx->frame_number++;
1917 if (ret < 0 || !*got_packet_ptr)
1918 av_free_packet(avpkt);
1920 av_packet_merge_side_data(avpkt);
1926 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1927 const AVSubtitle *sub)
1930 if (sub->start_display_time) {
1931 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1935 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1936 avctx->frame_number++;
1941 * Attempt to guess proper monotonic timestamps for decoded video frames
1942 * which might have incorrect times. Input timestamps may wrap around, in
1943 * which case the output will as well.
1945 * @param pts the pts field of the decoded AVPacket, as passed through
1947 * @param dts the dts field of the decoded AVPacket
1948 * @return one of the input values, may be AV_NOPTS_VALUE
1950 static int64_t guess_correct_pts(AVCodecContext *ctx,
1951 int64_t reordered_pts, int64_t dts)
1953 int64_t pts = AV_NOPTS_VALUE;
1955 if (dts != AV_NOPTS_VALUE) {
1956 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1957 ctx->pts_correction_last_dts = dts;
1959 if (reordered_pts != AV_NOPTS_VALUE) {
1960 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1961 ctx->pts_correction_last_pts = reordered_pts;
1963 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1964 && reordered_pts != AV_NOPTS_VALUE)
1965 pts = reordered_pts;
1972 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1975 const uint8_t *data;
1978 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1982 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1983 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1984 "changes, but PARAM_CHANGE side data was sent to it.\n");
1985 return AVERROR(EINVAL);
1991 flags = bytestream_get_le32(&data);
1994 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1997 avctx->channels = bytestream_get_le32(&data);
2000 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
2003 avctx->channel_layout = bytestream_get_le64(&data);
2006 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
2009 avctx->sample_rate = bytestream_get_le32(&data);
2012 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
2015 avctx->width = bytestream_get_le32(&data);
2016 avctx->height = bytestream_get_le32(&data);
2018 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2025 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2026 return AVERROR_INVALIDDATA;
2029 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
2032 const uint8_t *side_metadata;
2034 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2036 side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2037 AV_PKT_DATA_STRINGS_METADATA, &size);
2038 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2041 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2045 /* move the original frame to our backup */
2046 av_frame_unref(avci->to_free);
2047 av_frame_move_ref(avci->to_free, frame);
2049 /* now copy everything except the AVBufferRefs back
2050 * note that we make a COPY of the side data, so calling av_frame_free() on
2051 * the caller's frame will work properly */
2052 ret = av_frame_copy_props(frame, avci->to_free);
2056 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2057 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2058 if (avci->to_free->extended_data != avci->to_free->data) {
2059 int planes = av_frame_get_channels(avci->to_free);
2060 int size = planes * sizeof(*frame->extended_data);
2063 av_frame_unref(frame);
2067 frame->extended_data = av_malloc(size);
2068 if (!frame->extended_data) {
2069 av_frame_unref(frame);
2070 return AVERROR(ENOMEM);
2072 memcpy(frame->extended_data, avci->to_free->extended_data,
2075 frame->extended_data = frame->data;
2077 frame->format = avci->to_free->format;
2078 frame->width = avci->to_free->width;
2079 frame->height = avci->to_free->height;
2080 frame->channel_layout = avci->to_free->channel_layout;
2081 frame->nb_samples = avci->to_free->nb_samples;
2082 av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
2087 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2088 int *got_picture_ptr,
2089 const AVPacket *avpkt)
2091 AVCodecInternal *avci = avctx->internal;
2093 // copy to ensure we do not change avpkt
2094 AVPacket tmp = *avpkt;
2097 return AVERROR(EINVAL);
2098 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2099 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2100 return AVERROR(EINVAL);
2103 *got_picture_ptr = 0;
2104 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2105 return AVERROR(EINVAL);
2107 av_frame_unref(picture);
2109 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2110 int did_split = av_packet_split_side_data(&tmp);
2111 ret = apply_param_change(avctx, &tmp);
2113 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2114 if (avctx->err_recognition & AV_EF_EXPLODE)
2118 avctx->internal->pkt = &tmp;
2119 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2120 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2123 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2125 picture->pkt_dts = avpkt->dts;
2127 if(!avctx->has_b_frames){
2128 av_frame_set_pkt_pos(picture, avpkt->pos);
2130 //FIXME these should be under if(!avctx->has_b_frames)
2131 /* get_buffer is supposed to set frame parameters */
2132 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2133 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2134 if (!picture->width) picture->width = avctx->width;
2135 if (!picture->height) picture->height = avctx->height;
2136 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2139 add_metadata_from_side_data(avctx, picture);
2142 emms_c(); //needed to avoid an emms_c() call before every return;
2144 avctx->internal->pkt = NULL;
2146 av_packet_free_side_data(&tmp);
2151 if (*got_picture_ptr) {
2152 if (!avctx->refcounted_frames) {
2153 int err = unrefcount_frame(avci, picture);
2158 avctx->frame_number++;
2159 av_frame_set_best_effort_timestamp(picture,
2160 guess_correct_pts(avctx,
2164 av_frame_unref(picture);
2168 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2169 * make sure it's set correctly */
2170 av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2175 #if FF_API_OLD_DECODE_AUDIO
2176 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2177 int *frame_size_ptr,
2180 AVFrame *frame = av_frame_alloc();
2181 int ret, got_frame = 0;
2184 return AVERROR(ENOMEM);
2185 if (avctx->get_buffer != avcodec_default_get_buffer) {
2186 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2187 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2188 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2189 "avcodec_decode_audio4()\n");
2190 avctx->get_buffer = avcodec_default_get_buffer;
2191 avctx->release_buffer = avcodec_default_release_buffer;
2194 ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2196 if (ret >= 0 && got_frame) {
2198 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2199 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2201 avctx->sample_fmt, 1);
2202 if (*frame_size_ptr < data_size) {
2203 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2204 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2205 av_frame_free(&frame);
2206 return AVERROR(EINVAL);
2209 memcpy(samples, frame->extended_data[0], plane_size);
2211 if (planar && avctx->channels > 1) {
2212 uint8_t *out = ((uint8_t *)samples) + plane_size;
2213 for (ch = 1; ch < avctx->channels; ch++) {
2214 memcpy(out, frame->extended_data[ch], plane_size);
2218 *frame_size_ptr = data_size;
2220 *frame_size_ptr = 0;
2222 av_frame_free(&frame);
2228 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2231 const AVPacket *avpkt)
2233 AVCodecInternal *avci = avctx->internal;
2238 if (!avpkt->data && avpkt->size) {
2239 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2240 return AVERROR(EINVAL);
2243 return AVERROR(EINVAL);
2244 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2245 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2246 return AVERROR(EINVAL);
2249 av_frame_unref(frame);
2251 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2254 uint32_t discard_padding = 0;
2255 // copy to ensure we do not change avpkt
2256 AVPacket tmp = *avpkt;
2257 int did_split = av_packet_split_side_data(&tmp);
2258 ret = apply_param_change(avctx, &tmp);
2260 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2261 if (avctx->err_recognition & AV_EF_EXPLODE)
2265 avctx->internal->pkt = &tmp;
2266 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2267 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2269 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2270 frame->pkt_dts = avpkt->dts;
2272 if (ret >= 0 && *got_frame_ptr) {
2273 add_metadata_from_side_data(avctx, frame);
2274 avctx->frame_number++;
2275 av_frame_set_best_effort_timestamp(frame,
2276 guess_correct_pts(avctx,
2279 if (frame->format == AV_SAMPLE_FMT_NONE)
2280 frame->format = avctx->sample_fmt;
2281 if (!frame->channel_layout)
2282 frame->channel_layout = avctx->channel_layout;
2283 if (!av_frame_get_channels(frame))
2284 av_frame_set_channels(frame, avctx->channels);
2285 if (!frame->sample_rate)
2286 frame->sample_rate = avctx->sample_rate;
2289 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2290 if(side && side_size>=10) {
2291 avctx->internal->skip_samples = AV_RL32(side);
2292 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2293 avctx->internal->skip_samples);
2294 discard_padding = AV_RL32(side + 4);
2296 if (avctx->internal->skip_samples && *got_frame_ptr) {
2297 if(frame->nb_samples <= avctx->internal->skip_samples){
2299 avctx->internal->skip_samples -= frame->nb_samples;
2300 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2301 avctx->internal->skip_samples);
2303 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2304 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2305 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2306 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2307 (AVRational){1, avctx->sample_rate},
2308 avctx->pkt_timebase);
2309 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2310 frame->pkt_pts += diff_ts;
2311 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2312 frame->pkt_dts += diff_ts;
2313 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2314 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2316 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2318 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2319 avctx->internal->skip_samples, frame->nb_samples);
2320 frame->nb_samples -= avctx->internal->skip_samples;
2321 avctx->internal->skip_samples = 0;
2325 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
2326 if (discard_padding == frame->nb_samples) {
2329 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2330 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2331 (AVRational){1, avctx->sample_rate},
2332 avctx->pkt_timebase);
2333 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2334 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2336 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2338 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2339 discard_padding, frame->nb_samples);
2340 frame->nb_samples -= discard_padding;
2344 avctx->internal->pkt = NULL;
2346 av_packet_free_side_data(&tmp);
2351 if (ret >= 0 && *got_frame_ptr) {
2352 if (!avctx->refcounted_frames) {
2353 int err = unrefcount_frame(avci, frame);
2358 av_frame_unref(frame);
2364 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2365 static int recode_subtitle(AVCodecContext *avctx,
2366 AVPacket *outpkt, const AVPacket *inpkt)
2369 iconv_t cd = (iconv_t)-1;
2376 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2380 cd = iconv_open("UTF-8", avctx->sub_charenc);
2381 av_assert0(cd != (iconv_t)-1);
2386 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2387 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2388 ret = AVERROR(ENOMEM);
2392 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2395 outpkt->buf = tmp.buf;
2396 outpkt->data = tmp.data;
2397 outpkt->size = tmp.size;
2398 outb = outpkt->data;
2399 outl = outpkt->size;
2401 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2402 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2403 outl >= outpkt->size || inl != 0) {
2404 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2405 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2406 av_free_packet(&tmp);
2407 ret = AVERROR(errno);
2410 outpkt->size -= outl;
2411 memset(outpkt->data + outpkt->size, 0, outl);
2414 if (cd != (iconv_t)-1)
2418 av_assert0(!"requesting subtitles recoding without iconv");
2422 static int utf8_check(const uint8_t *str)
2424 const uint8_t *byte;
2425 uint32_t codepoint, min;
2429 GET_UTF8(codepoint, *(byte++), return 0;);
2430 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2431 1 << (5 * (byte - str) - 4);
2432 if (codepoint < min || codepoint >= 0x110000 ||
2433 codepoint == 0xFFFE /* BOM */ ||
2434 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2441 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2447 if (!avpkt->data && avpkt->size) {
2448 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2449 return AVERROR(EINVAL);
2452 return AVERROR(EINVAL);
2453 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2454 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2455 return AVERROR(EINVAL);
2459 avcodec_get_subtitle_defaults(sub);
2461 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2462 AVPacket pkt_recoded;
2463 AVPacket tmp = *avpkt;
2464 int did_split = av_packet_split_side_data(&tmp);
2465 //apply_param_change(avctx, &tmp);
2468 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2470 * If the side data is smaller than the buffer padding size, the
2471 * remaining bytes should have already been filled with zeros by the
2472 * original packet allocation anyway. */
2473 memset(tmp.data + tmp.size, 0,
2474 FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2478 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2482 avctx->internal->pkt = &pkt_recoded;
2484 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2485 sub->pts = av_rescale_q(avpkt->pts,
2486 avctx->pkt_timebase, AV_TIME_BASE_Q);
2487 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2488 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2489 !!*got_sub_ptr >= !!sub->num_rects);
2491 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2492 avctx->pkt_timebase.num) {
2493 AVRational ms = { 1, 1000 };
2494 sub->end_display_time = av_rescale_q(avpkt->duration,
2495 avctx->pkt_timebase, ms);
2498 for (i = 0; i < sub->num_rects; i++) {
2499 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2500 av_log(avctx, AV_LOG_ERROR,
2501 "Invalid UTF-8 in decoded subtitles text; "
2502 "maybe missing -sub_charenc option\n");
2503 avsubtitle_free(sub);
2504 return AVERROR_INVALIDDATA;
2508 if (tmp.data != pkt_recoded.data) { // did we recode?
2509 /* prevent from destroying side data from original packet */
2510 pkt_recoded.side_data = NULL;
2511 pkt_recoded.side_data_elems = 0;
2513 av_free_packet(&pkt_recoded);
2515 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2517 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2519 avctx->internal->pkt = NULL;
2523 av_packet_free_side_data(&tmp);
2529 avctx->frame_number++;
2535 void avsubtitle_free(AVSubtitle *sub)
2539 for (i = 0; i < sub->num_rects; i++) {
2540 av_freep(&sub->rects[i]->pict.data[0]);
2541 av_freep(&sub->rects[i]->pict.data[1]);
2542 av_freep(&sub->rects[i]->pict.data[2]);
2543 av_freep(&sub->rects[i]->pict.data[3]);
2544 av_freep(&sub->rects[i]->text);
2545 av_freep(&sub->rects[i]->ass);
2546 av_freep(&sub->rects[i]);
2549 av_freep(&sub->rects);
2551 memset(sub, 0, sizeof(AVSubtitle));
2554 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2558 ff_unlock_avcodec();
2560 ret = avcodec_close(avctx);
2562 ff_lock_avcodec(NULL);
2566 av_cold int avcodec_close(AVCodecContext *avctx)
2573 ret = ff_lock_avcodec(avctx);
2577 if (avcodec_is_open(avctx)) {
2578 FramePool *pool = avctx->internal->pool;
2580 if (CONFIG_FRAME_THREAD_ENCODER &&
2581 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2582 ff_unlock_avcodec();
2583 ff_frame_thread_encoder_free(avctx);
2584 ff_lock_avcodec(avctx);
2586 if (HAVE_THREADS && avctx->internal->thread_ctx)
2587 ff_thread_free(avctx);
2588 if (avctx->codec && avctx->codec->close)
2589 avctx->codec->close(avctx);
2590 avctx->coded_frame = NULL;
2591 avctx->internal->byte_buffer_size = 0;
2592 av_freep(&avctx->internal->byte_buffer);
2593 av_frame_free(&avctx->internal->to_free);
2594 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2595 av_buffer_pool_uninit(&pool->pools[i]);
2596 av_freep(&avctx->internal->pool);
2597 av_freep(&avctx->internal);
2600 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2601 av_opt_free(avctx->priv_data);
2603 av_freep(&avctx->priv_data);
2604 if (av_codec_is_encoder(avctx->codec))
2605 av_freep(&avctx->extradata);
2606 avctx->codec = NULL;
2607 avctx->active_thread_type = 0;
2609 ff_unlock_avcodec();
2613 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2616 //This is for future deprecatec codec ids, its empty since
2617 //last major bump but will fill up again over time, please don't remove it
2618 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2619 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
2620 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2621 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2622 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2623 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2624 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2625 case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
2626 case AV_CODEC_ID_HEVC_DEPRECATED: return AV_CODEC_ID_HEVC;
2627 default : return id;
2631 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2633 AVCodec *p, *experimental = NULL;
2635 id= remap_deprecated_codec_id(id);
2637 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2639 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2646 return experimental;
2649 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2651 return find_encdec(id, 1);
2654 AVCodec *avcodec_find_encoder_by_name(const char *name)
2661 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2668 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2670 return find_encdec(id, 0);
2673 AVCodec *avcodec_find_decoder_by_name(const char *name)
2680 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2687 const char *avcodec_get_name(enum AVCodecID id)
2689 const AVCodecDescriptor *cd;
2692 if (id == AV_CODEC_ID_NONE)
2694 cd = avcodec_descriptor_get(id);
2697 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2698 codec = avcodec_find_decoder(id);
2701 codec = avcodec_find_encoder(id);
2704 return "unknown_codec";
2707 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2709 int i, len, ret = 0;
2711 #define TAG_PRINT(x) \
2712 (((x) >= '0' && (x) <= '9') || \
2713 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2714 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2716 for (i = 0; i < 4; i++) {
2717 len = snprintf(buf, buf_size,
2718 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2720 buf_size = buf_size > len ? buf_size - len : 0;
2727 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2729 const char *codec_type;
2730 const char *codec_name;
2731 const char *profile = NULL;
2734 AVRational display_aspect_ratio;
2736 if (!buf || buf_size <= 0)
2738 codec_type = av_get_media_type_string(enc->codec_type);
2739 codec_name = avcodec_get_name(enc->codec_id);
2740 if (enc->profile != FF_PROFILE_UNKNOWN) {
2744 p = encode ? avcodec_find_encoder(enc->codec_id) :
2745 avcodec_find_decoder(enc->codec_id);
2747 profile = av_get_profile_name(p, enc->profile);
2750 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2752 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2754 if (enc->codec && strcmp(enc->codec->name, codec_name))
2755 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2758 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2759 if (enc->codec_tag) {
2761 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2762 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2763 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2766 switch (enc->codec_type) {
2767 case AVMEDIA_TYPE_VIDEO:
2768 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2769 char detail[256] = "(";
2770 const char *colorspace_name;
2771 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2773 av_get_pix_fmt_name(enc->pix_fmt));
2774 if (enc->bits_per_raw_sample &&
2775 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2776 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2777 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2778 av_strlcatf(detail, sizeof(detail),
2779 enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
2781 colorspace_name = av_get_colorspace_name(enc->colorspace);
2782 if (colorspace_name)
2783 av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
2785 if (strlen(detail) > 1) {
2786 detail[strlen(detail) - 2] = 0;
2787 av_strlcatf(buf, buf_size, "%s)", detail);
2791 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2793 enc->width, enc->height);
2794 if (enc->sample_aspect_ratio.num) {
2795 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2796 enc->width * enc->sample_aspect_ratio.num,
2797 enc->height * enc->sample_aspect_ratio.den,
2799 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2800 " [SAR %d:%d DAR %d:%d]",
2801 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2802 display_aspect_ratio.num, display_aspect_ratio.den);
2804 if (av_log_get_level() >= AV_LOG_DEBUG) {
2805 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2806 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2808 enc->time_base.num / g, enc->time_base.den / g);
2812 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2813 ", q=%d-%d", enc->qmin, enc->qmax);
2816 case AVMEDIA_TYPE_AUDIO:
2817 if (enc->sample_rate) {
2818 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2819 ", %d Hz", enc->sample_rate);
2821 av_strlcat(buf, ", ", buf_size);
2822 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2823 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2824 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2825 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2828 case AVMEDIA_TYPE_DATA:
2829 if (av_log_get_level() >= AV_LOG_DEBUG) {
2830 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2832 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2834 enc->time_base.num / g, enc->time_base.den / g);
2837 case AVMEDIA_TYPE_SUBTITLE:
2839 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2840 ", %dx%d", enc->width, enc->height);
2846 if (enc->flags & CODEC_FLAG_PASS1)
2847 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2849 if (enc->flags & CODEC_FLAG_PASS2)
2850 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2853 bitrate = get_bit_rate(enc);
2855 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2856 ", %d kb/s", bitrate / 1000);
2857 } else if (enc->rc_max_rate > 0) {
2858 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2859 ", max. %d kb/s", enc->rc_max_rate / 1000);
2863 const char *av_get_profile_name(const AVCodec *codec, int profile)
2866 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2869 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2870 if (p->profile == profile)
2876 unsigned avcodec_version(void)
2878 // av_assert0(AV_CODEC_ID_V410==164);
2879 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2880 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2881 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2882 av_assert0(AV_CODEC_ID_SRT==94216);
2883 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2885 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2886 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2887 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2888 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2889 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2890 return LIBAVCODEC_VERSION_INT;
2893 const char *avcodec_configuration(void)
2895 return FFMPEG_CONFIGURATION;
2898 const char *avcodec_license(void)
2900 #define LICENSE_PREFIX "libavcodec license: "
2901 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2904 void avcodec_flush_buffers(AVCodecContext *avctx)
2906 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2907 ff_thread_flush(avctx);
2908 else if (avctx->codec->flush)
2909 avctx->codec->flush(avctx);
2911 avctx->pts_correction_last_pts =
2912 avctx->pts_correction_last_dts = INT64_MIN;
2914 if (!avctx->refcounted_frames)
2915 av_frame_unref(avctx->internal->to_free);
2918 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2921 case AV_CODEC_ID_8SVX_EXP:
2922 case AV_CODEC_ID_8SVX_FIB:
2923 case AV_CODEC_ID_ADPCM_CT:
2924 case AV_CODEC_ID_ADPCM_IMA_APC:
2925 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2926 case AV_CODEC_ID_ADPCM_IMA_OKI:
2927 case AV_CODEC_ID_ADPCM_IMA_WS:
2928 case AV_CODEC_ID_ADPCM_G722:
2929 case AV_CODEC_ID_ADPCM_YAMAHA:
2931 case AV_CODEC_ID_PCM_ALAW:
2932 case AV_CODEC_ID_PCM_MULAW:
2933 case AV_CODEC_ID_PCM_S8:
2934 case AV_CODEC_ID_PCM_S8_PLANAR:
2935 case AV_CODEC_ID_PCM_U8:
2936 case AV_CODEC_ID_PCM_ZORK:
2938 case AV_CODEC_ID_PCM_S16BE:
2939 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2940 case AV_CODEC_ID_PCM_S16LE:
2941 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2942 case AV_CODEC_ID_PCM_U16BE:
2943 case AV_CODEC_ID_PCM_U16LE:
2945 case AV_CODEC_ID_PCM_S24DAUD:
2946 case AV_CODEC_ID_PCM_S24BE:
2947 case AV_CODEC_ID_PCM_S24LE:
2948 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2949 case AV_CODEC_ID_PCM_U24BE:
2950 case AV_CODEC_ID_PCM_U24LE:
2952 case AV_CODEC_ID_PCM_S32BE:
2953 case AV_CODEC_ID_PCM_S32LE:
2954 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2955 case AV_CODEC_ID_PCM_U32BE:
2956 case AV_CODEC_ID_PCM_U32LE:
2957 case AV_CODEC_ID_PCM_F32BE:
2958 case AV_CODEC_ID_PCM_F32LE:
2960 case AV_CODEC_ID_PCM_F64BE:
2961 case AV_CODEC_ID_PCM_F64LE:
2968 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2970 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2971 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2972 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2973 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2974 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2975 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2976 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2977 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2978 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2979 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2980 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2982 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2983 return AV_CODEC_ID_NONE;
2984 if (be < 0 || be > 1)
2986 return map[fmt][be];
2989 int av_get_bits_per_sample(enum AVCodecID codec_id)
2992 case AV_CODEC_ID_ADPCM_SBPRO_2:
2994 case AV_CODEC_ID_ADPCM_SBPRO_3:
2996 case AV_CODEC_ID_ADPCM_SBPRO_4:
2997 case AV_CODEC_ID_ADPCM_IMA_WAV:
2998 case AV_CODEC_ID_ADPCM_IMA_QT:
2999 case AV_CODEC_ID_ADPCM_SWF:
3000 case AV_CODEC_ID_ADPCM_MS:
3003 return av_get_exact_bits_per_sample(codec_id);
3007 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3009 int id, sr, ch, ba, tag, bps;
3011 id = avctx->codec_id;
3012 sr = avctx->sample_rate;
3013 ch = avctx->channels;
3014 ba = avctx->block_align;
3015 tag = avctx->codec_tag;
3016 bps = av_get_exact_bits_per_sample(avctx->codec_id);
3018 /* codecs with an exact constant bits per sample */
3019 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3020 return (frame_bytes * 8LL) / (bps * ch);
3021 bps = avctx->bits_per_coded_sample;
3023 /* codecs with a fixed packet duration */
3025 case AV_CODEC_ID_ADPCM_ADX: return 32;
3026 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3027 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3028 case AV_CODEC_ID_AMR_NB:
3029 case AV_CODEC_ID_EVRC:
3030 case AV_CODEC_ID_GSM:
3031 case AV_CODEC_ID_QCELP:
3032 case AV_CODEC_ID_RA_288: return 160;
3033 case AV_CODEC_ID_AMR_WB:
3034 case AV_CODEC_ID_GSM_MS: return 320;
3035 case AV_CODEC_ID_MP1: return 384;
3036 case AV_CODEC_ID_ATRAC1: return 512;
3037 case AV_CODEC_ID_ATRAC3: return 1024;
3038 case AV_CODEC_ID_MP2:
3039 case AV_CODEC_ID_MUSEPACK7: return 1152;
3040 case AV_CODEC_ID_AC3: return 1536;
3044 /* calc from sample rate */
3045 if (id == AV_CODEC_ID_TTA)
3046 return 256 * sr / 245;
3049 /* calc from sample rate and channels */
3050 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3051 return (480 << (sr / 22050)) / ch;
3056 /* calc from block_align */
3057 if (id == AV_CODEC_ID_SIPR) {
3059 case 20: return 160;
3060 case 19: return 144;
3061 case 29: return 288;
3062 case 37: return 480;
3064 } else if (id == AV_CODEC_ID_ILBC) {
3066 case 38: return 160;
3067 case 50: return 240;
3072 if (frame_bytes > 0) {
3073 /* calc from frame_bytes only */
3074 if (id == AV_CODEC_ID_TRUESPEECH)
3075 return 240 * (frame_bytes / 32);
3076 if (id == AV_CODEC_ID_NELLYMOSER)
3077 return 256 * (frame_bytes / 64);
3078 if (id == AV_CODEC_ID_RA_144)
3079 return 160 * (frame_bytes / 20);
3080 if (id == AV_CODEC_ID_G723_1)
3081 return 240 * (frame_bytes / 24);
3084 /* calc from frame_bytes and bits_per_coded_sample */
3085 if (id == AV_CODEC_ID_ADPCM_G726)
3086 return frame_bytes * 8 / bps;
3090 /* calc from frame_bytes and channels */
3092 case AV_CODEC_ID_ADPCM_AFC:
3093 return frame_bytes / (9 * ch) * 16;
3094 case AV_CODEC_ID_ADPCM_DTK:
3095 return frame_bytes / (16 * ch) * 28;
3096 case AV_CODEC_ID_ADPCM_4XM:
3097 case AV_CODEC_ID_ADPCM_IMA_ISS:
3098 return (frame_bytes - 4 * ch) * 2 / ch;
3099 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3100 return (frame_bytes - 4) * 2 / ch;
3101 case AV_CODEC_ID_ADPCM_IMA_AMV:
3102 return (frame_bytes - 8) * 2 / ch;
3103 case AV_CODEC_ID_ADPCM_XA:
3104 return (frame_bytes / 128) * 224 / ch;
3105 case AV_CODEC_ID_INTERPLAY_DPCM:
3106 return (frame_bytes - 6 - ch) / ch;
3107 case AV_CODEC_ID_ROQ_DPCM:
3108 return (frame_bytes - 8) / ch;
3109 case AV_CODEC_ID_XAN_DPCM:
3110 return (frame_bytes - 2 * ch) / ch;
3111 case AV_CODEC_ID_MACE3:
3112 return 3 * frame_bytes / ch;
3113 case AV_CODEC_ID_MACE6:
3114 return 6 * frame_bytes / ch;
3115 case AV_CODEC_ID_PCM_LXF:
3116 return 2 * (frame_bytes / (5 * ch));
3117 case AV_CODEC_ID_IAC:
3118 case AV_CODEC_ID_IMC:
3119 return 4 * frame_bytes / ch;
3123 /* calc from frame_bytes, channels, and codec_tag */
3124 if (id == AV_CODEC_ID_SOL_DPCM) {
3126 return frame_bytes / ch;
3128 return frame_bytes * 2 / ch;
3133 /* calc from frame_bytes, channels, and block_align */
3134 int blocks = frame_bytes / ba;
3135 switch (avctx->codec_id) {
3136 case AV_CODEC_ID_ADPCM_IMA_WAV:
3137 if (bps < 2 || bps > 5)
3139 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3140 case AV_CODEC_ID_ADPCM_IMA_DK3:
3141 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3142 case AV_CODEC_ID_ADPCM_IMA_DK4:
3143 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3144 case AV_CODEC_ID_ADPCM_IMA_RAD:
3145 return blocks * ((ba - 4 * ch) * 2 / ch);
3146 case AV_CODEC_ID_ADPCM_MS:
3147 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3152 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3153 switch (avctx->codec_id) {
3154 case AV_CODEC_ID_PCM_DVD:
3157 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3158 case AV_CODEC_ID_PCM_BLURAY:
3161 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3162 case AV_CODEC_ID_S302M:
3163 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3173 int ff_thread_init(AVCodecContext *s)
3180 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3194 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3197 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3201 #if FF_API_MISSING_SAMPLE
3202 FF_DISABLE_DEPRECATION_WARNINGS
3203 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3205 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3206 "version to the newest one from Git. If the problem still "
3207 "occurs, it means that your file has a feature which has not "
3208 "been implemented.\n", feature);
3210 av_log_ask_for_sample(avc, NULL);
3213 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3215 va_list argument_list;
3217 va_start(argument_list, msg);
3220 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3221 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3222 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
3223 "and contact the ffmpeg-devel mailing list.\n");
3225 va_end(argument_list);
3227 FF_ENABLE_DEPRECATION_WARNINGS
3228 #endif /* FF_API_MISSING_SAMPLE */
3230 static AVHWAccel *first_hwaccel = NULL;
3231 static AVHWAccel **last_hwaccel = &first_hwaccel;
3233 void av_register_hwaccel(AVHWAccel *hwaccel)
3235 AVHWAccel **p = last_hwaccel;
3236 hwaccel->next = NULL;
3237 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3239 last_hwaccel = &hwaccel->next;
3242 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
3244 return hwaccel ? hwaccel->next : first_hwaccel;
3247 AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
3249 enum AVCodecID codec_id = avctx->codec->id;
3250 enum AVPixelFormat pix_fmt = avctx->pix_fmt;
3252 AVHWAccel *hwaccel = NULL;
3254 while ((hwaccel = av_hwaccel_next(hwaccel)))
3255 if (hwaccel->id == codec_id
3256 && hwaccel->pix_fmt == pix_fmt)
3261 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3264 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3266 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3273 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3275 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3281 int ff_lock_avcodec(AVCodecContext *log_ctx)
3284 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3287 entangled_thread_counter++;
3288 if (entangled_thread_counter != 1) {
3289 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3291 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3292 ff_avcodec_locked = 1;
3293 ff_unlock_avcodec();
3294 return AVERROR(EINVAL);
3296 av_assert0(!ff_avcodec_locked);
3297 ff_avcodec_locked = 1;
3301 int ff_unlock_avcodec(void)
3303 av_assert0(ff_avcodec_locked);
3304 ff_avcodec_locked = 0;
3305 entangled_thread_counter--;
3307 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3313 int avpriv_lock_avformat(void)
3316 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3322 int avpriv_unlock_avformat(void)
3325 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3331 unsigned int avpriv_toupper4(unsigned int x)
3333 return av_toupper(x & 0xFF) +
3334 (av_toupper((x >> 8) & 0xFF) << 8) +
3335 (av_toupper((x >> 16) & 0xFF) << 16) +
3336 (av_toupper((x >> 24) & 0xFF) << 24);
3339 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3343 dst->owner = src->owner;
3345 ret = av_frame_ref(dst->f, src->f);
3349 if (src->progress &&
3350 !(dst->progress = av_buffer_ref(src->progress))) {
3351 ff_thread_release_buffer(dst->owner, dst);
3352 return AVERROR(ENOMEM);
3360 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3362 return avctx->get_format(avctx, fmt);
3365 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3368 return ff_get_buffer(avctx, f->f, flags);
3371 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3373 av_frame_unref(f->f);
3376 void ff_thread_finish_setup(AVCodecContext *avctx)
3380 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3384 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3388 int ff_thread_can_start_frame(AVCodecContext *avctx)
3393 int ff_alloc_entries(AVCodecContext *avctx, int count)
3398 void ff_reset_entries(AVCodecContext *avctx)
3402 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3406 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3412 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3414 AVCodec *c= avcodec_find_decoder(codec_id);
3416 c= avcodec_find_encoder(codec_id);
3420 if (codec_id <= AV_CODEC_ID_NONE)
3421 return AVMEDIA_TYPE_UNKNOWN;
3422 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3423 return AVMEDIA_TYPE_VIDEO;
3424 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3425 return AVMEDIA_TYPE_AUDIO;
3426 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3427 return AVMEDIA_TYPE_SUBTITLE;
3429 return AVMEDIA_TYPE_UNKNOWN;
3432 int avcodec_is_open(AVCodecContext *s)
3434 return !!s->internal;
3437 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3442 ret = av_bprint_finalize(buf, &str);
3445 avctx->extradata = str;
3446 /* Note: the string is NUL terminated (so extradata can be read as a
3447 * string), but the ending character is not accounted in the size (in
3448 * binary formats you are likely not supposed to mux that character). When
3449 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3451 avctx->extradata_size = buf->len;
3455 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3457 uint32_t *av_restrict state)
3461 av_assert0(p <= end);
3465 for (i = 0; i < 3; i++) {
3466 uint32_t tmp = *state << 8;
3467 *state = tmp + *(p++);
3468 if (tmp == 0x100 || p == end)
3473 if (p[-1] > 1 ) p += 3;
3474 else if (p[-2] ) p += 2;
3475 else if (p[-3]|(p[-1]-1)) p++;
3482 p = FFMIN(p, end) - 4;
3483 *state = AV_RB32(p);