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 FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
121 FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
123 return av_fast_realloc(ptr, size, min_size);
126 FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
128 av_fast_malloc(ptr, size, min_size);
132 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
135 if (min_size < *size)
137 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
139 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
146 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
149 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
154 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
155 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
158 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
161 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
166 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
167 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
170 /* encoder management */
171 static AVCodec *first_avcodec = NULL;
173 AVCodec *av_codec_next(const AVCodec *c)
178 return first_avcodec;
181 static av_cold void avcodec_init(void)
183 static int initialized = 0;
185 if (initialized != 0)
190 ff_dsputil_static_init();
193 int av_codec_is_encoder(const AVCodec *codec)
195 return codec && (codec->encode_sub || codec->encode2);
198 int av_codec_is_decoder(const AVCodec *codec)
200 return codec && codec->decode;
203 av_cold void avcodec_register(AVCodec *codec)
209 while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
212 if (codec->init_static_data)
213 codec->init_static_data(codec);
216 unsigned avcodec_get_edge_width(void)
221 #if FF_API_SET_DIMENSIONS
222 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
224 ff_set_dimensions(s, width, height);
228 int ff_set_dimensions(AVCodecContext *s, int width, int height)
230 int ret = av_image_check_size(width, height, 0, s);
235 s->coded_width = width;
236 s->coded_height = height;
237 s->width = FF_CEIL_RSHIFT(width, s->lowres);
238 s->height = FF_CEIL_RSHIFT(height, s->lowres);
243 #if HAVE_NEON || ARCH_PPC || HAVE_MMX
244 # define STRIDE_ALIGN 16
246 # define STRIDE_ALIGN 8
249 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
250 int linesize_align[AV_NUM_DATA_POINTERS])
256 switch (s->pix_fmt) {
257 case AV_PIX_FMT_YUV420P:
258 case AV_PIX_FMT_YUYV422:
259 case AV_PIX_FMT_UYVY422:
260 case AV_PIX_FMT_YUV422P:
261 case AV_PIX_FMT_YUV440P:
262 case AV_PIX_FMT_YUV444P:
263 case AV_PIX_FMT_GBRAP:
264 case AV_PIX_FMT_GBRP:
265 case AV_PIX_FMT_GRAY8:
266 case AV_PIX_FMT_GRAY16BE:
267 case AV_PIX_FMT_GRAY16LE:
268 case AV_PIX_FMT_YUVJ420P:
269 case AV_PIX_FMT_YUVJ422P:
270 case AV_PIX_FMT_YUVJ440P:
271 case AV_PIX_FMT_YUVJ444P:
272 case AV_PIX_FMT_YUVA420P:
273 case AV_PIX_FMT_YUVA422P:
274 case AV_PIX_FMT_YUVA444P:
275 case AV_PIX_FMT_YUV420P9LE:
276 case AV_PIX_FMT_YUV420P9BE:
277 case AV_PIX_FMT_YUV420P10LE:
278 case AV_PIX_FMT_YUV420P10BE:
279 case AV_PIX_FMT_YUV420P12LE:
280 case AV_PIX_FMT_YUV420P12BE:
281 case AV_PIX_FMT_YUV420P14LE:
282 case AV_PIX_FMT_YUV420P14BE:
283 case AV_PIX_FMT_YUV420P16LE:
284 case AV_PIX_FMT_YUV420P16BE:
285 case AV_PIX_FMT_YUV422P9LE:
286 case AV_PIX_FMT_YUV422P9BE:
287 case AV_PIX_FMT_YUV422P10LE:
288 case AV_PIX_FMT_YUV422P10BE:
289 case AV_PIX_FMT_YUV422P12LE:
290 case AV_PIX_FMT_YUV422P12BE:
291 case AV_PIX_FMT_YUV422P14LE:
292 case AV_PIX_FMT_YUV422P14BE:
293 case AV_PIX_FMT_YUV422P16LE:
294 case AV_PIX_FMT_YUV422P16BE:
295 case AV_PIX_FMT_YUV444P9LE:
296 case AV_PIX_FMT_YUV444P9BE:
297 case AV_PIX_FMT_YUV444P10LE:
298 case AV_PIX_FMT_YUV444P10BE:
299 case AV_PIX_FMT_YUV444P12LE:
300 case AV_PIX_FMT_YUV444P12BE:
301 case AV_PIX_FMT_YUV444P14LE:
302 case AV_PIX_FMT_YUV444P14BE:
303 case AV_PIX_FMT_YUV444P16LE:
304 case AV_PIX_FMT_YUV444P16BE:
305 case AV_PIX_FMT_YUVA420P9LE:
306 case AV_PIX_FMT_YUVA420P9BE:
307 case AV_PIX_FMT_YUVA420P10LE:
308 case AV_PIX_FMT_YUVA420P10BE:
309 case AV_PIX_FMT_YUVA420P16LE:
310 case AV_PIX_FMT_YUVA420P16BE:
311 case AV_PIX_FMT_YUVA422P9LE:
312 case AV_PIX_FMT_YUVA422P9BE:
313 case AV_PIX_FMT_YUVA422P10LE:
314 case AV_PIX_FMT_YUVA422P10BE:
315 case AV_PIX_FMT_YUVA422P16LE:
316 case AV_PIX_FMT_YUVA422P16BE:
317 case AV_PIX_FMT_YUVA444P9LE:
318 case AV_PIX_FMT_YUVA444P9BE:
319 case AV_PIX_FMT_YUVA444P10LE:
320 case AV_PIX_FMT_YUVA444P10BE:
321 case AV_PIX_FMT_YUVA444P16LE:
322 case AV_PIX_FMT_YUVA444P16BE:
323 case AV_PIX_FMT_GBRP9LE:
324 case AV_PIX_FMT_GBRP9BE:
325 case AV_PIX_FMT_GBRP10LE:
326 case AV_PIX_FMT_GBRP10BE:
327 case AV_PIX_FMT_GBRP12LE:
328 case AV_PIX_FMT_GBRP12BE:
329 case AV_PIX_FMT_GBRP14LE:
330 case AV_PIX_FMT_GBRP14BE:
331 w_align = 16; //FIXME assume 16 pixel per macroblock
332 h_align = 16 * 2; // interlaced needs 2 macroblocks height
334 case AV_PIX_FMT_YUV411P:
335 case AV_PIX_FMT_YUVJ411P:
336 case AV_PIX_FMT_UYYVYY411:
340 case AV_PIX_FMT_YUV410P:
341 if (s->codec_id == AV_CODEC_ID_SVQ1) {
346 case AV_PIX_FMT_RGB555:
347 if (s->codec_id == AV_CODEC_ID_RPZA) {
352 case AV_PIX_FMT_PAL8:
353 case AV_PIX_FMT_BGR8:
354 case AV_PIX_FMT_RGB8:
355 if (s->codec_id == AV_CODEC_ID_SMC ||
356 s->codec_id == AV_CODEC_ID_CINEPAK) {
361 case AV_PIX_FMT_BGR24:
362 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
363 (s->codec_id == AV_CODEC_ID_ZLIB)) {
368 case AV_PIX_FMT_RGB24:
369 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
380 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
381 w_align = FFMAX(w_align, 8);
384 *width = FFALIGN(*width, w_align);
385 *height = FFALIGN(*height, h_align);
386 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
387 // some of the optimized chroma MC reads one line too much
388 // which is also done in mpeg decoders with lowres > 0
391 for (i = 0; i < 4; i++)
392 linesize_align[i] = STRIDE_ALIGN;
395 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
397 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
398 int chroma_shift = desc->log2_chroma_w;
399 int linesize_align[AV_NUM_DATA_POINTERS];
402 avcodec_align_dimensions2(s, width, height, linesize_align);
403 align = FFMAX(linesize_align[0], linesize_align[3]);
404 linesize_align[1] <<= chroma_shift;
405 linesize_align[2] <<= chroma_shift;
406 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
407 *width = FFALIGN(*width, align);
410 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
412 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
413 return AVERROR(EINVAL);
416 *xpos = (pos&1) * 128;
417 *ypos = ((pos>>1)^(pos<4)) * 128;
422 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
426 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
427 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
430 return AVCHROMA_LOC_UNSPECIFIED;
433 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
434 enum AVSampleFormat sample_fmt, const uint8_t *buf,
435 int buf_size, int align)
437 int ch, planar, needed_size, ret = 0;
439 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
440 frame->nb_samples, sample_fmt,
442 if (buf_size < needed_size)
443 return AVERROR(EINVAL);
445 planar = av_sample_fmt_is_planar(sample_fmt);
446 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
447 if (!(frame->extended_data = av_mallocz(nb_channels *
448 sizeof(*frame->extended_data))))
449 return AVERROR(ENOMEM);
451 frame->extended_data = frame->data;
454 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
455 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
456 sample_fmt, align)) < 0) {
457 if (frame->extended_data != frame->data)
458 av_freep(&frame->extended_data);
461 if (frame->extended_data != frame->data) {
462 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
463 frame->data[ch] = frame->extended_data[ch];
469 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
471 FramePool *pool = avctx->internal->pool;
474 switch (avctx->codec_type) {
475 case AVMEDIA_TYPE_VIDEO: {
478 int w = frame->width;
479 int h = frame->height;
480 int tmpsize, unaligned;
482 if (pool->format == frame->format &&
483 pool->width == frame->width && pool->height == frame->height)
486 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
488 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
494 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
495 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
496 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
497 // increase alignment of w for next try (rhs gives the lowest bit set in w)
501 for (i = 0; i < 4; i++)
502 unaligned |= picture.linesize[i] % pool->stride_align[i];
505 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
506 NULL, picture.linesize);
510 for (i = 0; i < 3 && picture.data[i + 1]; i++)
511 size[i] = picture.data[i + 1] - picture.data[i];
512 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
514 for (i = 0; i < 4; i++) {
515 av_buffer_pool_uninit(&pool->pools[i]);
516 pool->linesize[i] = picture.linesize[i];
518 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
519 CONFIG_MEMORY_POISONING ?
522 if (!pool->pools[i]) {
523 ret = AVERROR(ENOMEM);
528 pool->format = frame->format;
529 pool->width = frame->width;
530 pool->height = frame->height;
534 case AVMEDIA_TYPE_AUDIO: {
535 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
536 int planar = av_sample_fmt_is_planar(frame->format);
537 int planes = planar ? ch : 1;
539 if (pool->format == frame->format && pool->planes == planes &&
540 pool->channels == ch && frame->nb_samples == pool->samples)
543 av_buffer_pool_uninit(&pool->pools[0]);
544 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
545 frame->nb_samples, frame->format, 0);
549 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
550 if (!pool->pools[0]) {
551 ret = AVERROR(ENOMEM);
555 pool->format = frame->format;
556 pool->planes = planes;
558 pool->samples = frame->nb_samples;
561 default: av_assert0(0);
565 for (i = 0; i < 4; i++)
566 av_buffer_pool_uninit(&pool->pools[i]);
568 pool->planes = pool->channels = pool->samples = 0;
569 pool->width = pool->height = 0;
573 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
575 FramePool *pool = avctx->internal->pool;
576 int planes = pool->planes;
579 frame->linesize[0] = pool->linesize[0];
581 if (planes > AV_NUM_DATA_POINTERS) {
582 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
583 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
584 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
585 sizeof(*frame->extended_buf));
586 if (!frame->extended_data || !frame->extended_buf) {
587 av_freep(&frame->extended_data);
588 av_freep(&frame->extended_buf);
589 return AVERROR(ENOMEM);
592 frame->extended_data = frame->data;
593 av_assert0(frame->nb_extended_buf == 0);
596 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
597 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
600 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
602 for (i = 0; i < frame->nb_extended_buf; i++) {
603 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
604 if (!frame->extended_buf[i])
606 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
609 if (avctx->debug & FF_DEBUG_BUFFERS)
610 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
614 av_frame_unref(frame);
615 return AVERROR(ENOMEM);
618 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
620 FramePool *pool = s->internal->pool;
621 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
622 int pixel_size = desc->comp[0].step_minus1 + 1;
623 int h_chroma_shift, v_chroma_shift;
626 if (pic->data[0] != NULL) {
627 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
631 memset(pic->data, 0, sizeof(pic->data));
632 pic->extended_data = pic->data;
634 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
636 for (i = 0; i < 4 && pool->pools[i]; i++) {
637 const int h_shift = i == 0 ? 0 : h_chroma_shift;
638 const int v_shift = i == 0 ? 0 : v_chroma_shift;
639 int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
641 pic->linesize[i] = pool->linesize[i];
643 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
647 // no edge if EDGE EMU or not planar YUV
648 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
649 pic->data[i] = pic->buf[i]->data;
651 pic->data[i] = pic->buf[i]->data +
652 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
653 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
656 for (; i < AV_NUM_DATA_POINTERS; i++) {
658 pic->linesize[i] = 0;
660 if (pic->data[1] && !pic->data[2])
661 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
663 if (s->debug & FF_DEBUG_BUFFERS)
664 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
669 return AVERROR(ENOMEM);
672 void avpriv_color_frame(AVFrame *frame, const int c[4])
674 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
677 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
679 for (p = 0; p<desc->nb_components; p++) {
680 uint8_t *dst = frame->data[p];
681 int is_chroma = p == 1 || p == 2;
682 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
683 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
684 for (y = 0; y < height; y++) {
685 if (desc->comp[0].depth_minus1 >= 8) {
686 for (x = 0; x<bytes; x++)
687 ((uint16_t*)dst)[x] = c[p];
689 memset(dst, c[p], bytes);
690 dst += frame->linesize[p];
695 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
699 if ((ret = update_frame_pool(avctx, frame)) < 0)
702 #if FF_API_GET_BUFFER
703 FF_DISABLE_DEPRECATION_WARNINGS
704 frame->type = FF_BUFFER_TYPE_INTERNAL;
705 FF_ENABLE_DEPRECATION_WARNINGS
708 switch (avctx->codec_type) {
709 case AVMEDIA_TYPE_VIDEO:
710 return video_get_buffer(avctx, frame);
711 case AVMEDIA_TYPE_AUDIO:
712 return audio_get_buffer(avctx, frame);
718 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
720 if (avctx->internal->pkt) {
721 frame->pkt_pts = avctx->internal->pkt->pts;
722 av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
723 av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
724 av_frame_set_pkt_size (frame, avctx->internal->pkt->size);
726 frame->pkt_pts = AV_NOPTS_VALUE;
727 av_frame_set_pkt_pos (frame, -1);
728 av_frame_set_pkt_duration(frame, 0);
729 av_frame_set_pkt_size (frame, -1);
731 frame->reordered_opaque = avctx->reordered_opaque;
733 switch (avctx->codec->type) {
734 case AVMEDIA_TYPE_VIDEO:
735 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
736 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
737 if (frame->format < 0)
738 frame->format = avctx->pix_fmt;
739 if (!frame->sample_aspect_ratio.num)
740 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
741 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
742 av_frame_set_colorspace(frame, avctx->colorspace);
743 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
744 av_frame_set_color_range(frame, avctx->color_range);
746 case AVMEDIA_TYPE_AUDIO:
747 if (!frame->sample_rate)
748 frame->sample_rate = avctx->sample_rate;
749 if (frame->format < 0)
750 frame->format = avctx->sample_fmt;
751 if (!frame->channel_layout) {
752 if (avctx->channel_layout) {
753 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
755 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
757 return AVERROR(EINVAL);
760 frame->channel_layout = avctx->channel_layout;
762 if (avctx->channels > FF_SANE_NB_CHANNELS) {
763 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
765 return AVERROR(ENOSYS);
769 av_frame_set_channels(frame, avctx->channels);
775 #if FF_API_GET_BUFFER
776 FF_DISABLE_DEPRECATION_WARNINGS
777 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
779 return avcodec_default_get_buffer2(avctx, frame, 0);
782 typedef struct CompatReleaseBufPriv {
783 AVCodecContext avctx;
785 } CompatReleaseBufPriv;
787 static void compat_free_buffer(void *opaque, uint8_t *data)
789 CompatReleaseBufPriv *priv = opaque;
790 if (priv->avctx.release_buffer)
791 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
795 static void compat_release_buffer(void *opaque, uint8_t *data)
797 AVBufferRef *buf = opaque;
798 av_buffer_unref(&buf);
800 FF_ENABLE_DEPRECATION_WARNINGS
803 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
807 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
808 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
809 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
810 return AVERROR(EINVAL);
813 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
816 #if FF_API_GET_BUFFER
817 FF_DISABLE_DEPRECATION_WARNINGS
819 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
820 * We wrap each plane in its own AVBuffer. Each of those has a reference to
821 * a dummy AVBuffer as its private data, unreffing it on free.
822 * When all the planes are freed, the dummy buffer's free callback calls
825 if (avctx->get_buffer) {
826 CompatReleaseBufPriv *priv = NULL;
827 AVBufferRef *dummy_buf = NULL;
830 if (flags & AV_GET_BUFFER_FLAG_REF)
831 frame->reference = 1;
833 ret = avctx->get_buffer(avctx, frame);
837 /* return if the buffers are already set up
838 * this would happen e.g. when a custom get_buffer() calls
839 * avcodec_default_get_buffer
844 priv = av_mallocz(sizeof(*priv));
846 ret = AVERROR(ENOMEM);
849 priv->avctx = *avctx;
850 priv->frame = *frame;
852 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
854 ret = AVERROR(ENOMEM);
858 #define WRAP_PLANE(ref_out, data, data_size) \
860 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
862 ret = AVERROR(ENOMEM); \
865 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
868 av_frame_unref(frame); \
869 ret = AVERROR(ENOMEM); \
874 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
875 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
877 planes = av_pix_fmt_count_planes(frame->format);
878 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
879 check for allocated buffers: make libavcodec happy */
880 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
882 if (!desc || planes <= 0) {
883 ret = AVERROR(EINVAL);
887 for (i = 0; i < planes; i++) {
888 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
889 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
891 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
894 int planar = av_sample_fmt_is_planar(frame->format);
895 planes = planar ? avctx->channels : 1;
897 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
898 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
899 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
900 frame->nb_extended_buf);
901 if (!frame->extended_buf) {
902 ret = AVERROR(ENOMEM);
907 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
908 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
910 for (i = 0; i < frame->nb_extended_buf; i++)
911 WRAP_PLANE(frame->extended_buf[i],
912 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
916 av_buffer_unref(&dummy_buf);
919 frame->width = avctx->width;
920 frame->height = avctx->height;
925 avctx->release_buffer(avctx, frame);
927 av_buffer_unref(&dummy_buf);
930 FF_ENABLE_DEPRECATION_WARNINGS
933 ret = avctx->get_buffer2(avctx, frame, flags);
935 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
936 frame->width = avctx->width;
937 frame->height = avctx->height;
943 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
945 int ret = get_buffer_internal(avctx, frame, flags);
947 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
951 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
956 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
958 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
959 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
960 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
961 av_frame_unref(frame);
964 ff_init_buffer_info(avctx, frame);
967 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
969 if (av_frame_is_writable(frame))
972 av_frame_move_ref(&tmp, frame);
974 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
976 av_frame_unref(&tmp);
980 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
981 frame->format, frame->width, frame->height);
983 av_frame_unref(&tmp);
988 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
990 int ret = reget_buffer_internal(avctx, frame);
992 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
996 #if FF_API_GET_BUFFER
997 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
999 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
1001 av_frame_unref(pic);
1004 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1011 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1015 for (i = 0; i < count; i++) {
1016 int r = func(c, (char *)arg + i * size);
1023 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1027 for (i = 0; i < count; i++) {
1028 int r = func(c, arg, i, 0);
1035 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1037 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1038 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1041 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1043 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1048 void avcodec_get_frame_defaults(AVFrame *frame)
1050 #if LIBAVCODEC_VERSION_MAJOR >= 55
1051 // extended_data should explicitly be freed when needed, this code is unsafe currently
1052 // also this is not compatible to the <55 ABI/API
1053 if (frame->extended_data != frame->data && 0)
1054 av_freep(&frame->extended_data);
1057 memset(frame, 0, sizeof(AVFrame));
1061 frame->pkt_pts = AV_NOPTS_VALUE;
1062 av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
1063 av_frame_set_pkt_duration (frame, 0);
1064 av_frame_set_pkt_pos (frame, -1);
1065 av_frame_set_pkt_size (frame, -1);
1066 frame->key_frame = 1;
1067 frame->sample_aspect_ratio = (AVRational) {0, 1 };
1068 frame->format = -1; /* unknown */
1069 frame->extended_data = frame->data;
1070 av_frame_set_colorspace(frame, AVCOL_SPC_UNSPECIFIED);
1073 #if FF_API_AVFRAME_LAVC
1074 AVFrame *avcodec_alloc_frame(void)
1076 AVFrame *frame = av_malloc(sizeof(AVFrame));
1081 frame->extended_data = NULL;
1082 avcodec_get_frame_defaults(frame);
1088 void avcodec_free_frame(AVFrame **frame)
1092 if (!frame || !*frame)
1097 if (f->extended_data != f->data)
1098 av_freep(&f->extended_data);
1103 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1104 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1105 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1106 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1108 int av_codec_get_max_lowres(const AVCodec *codec)
1110 return codec->max_lowres;
1113 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1115 memset(sub, 0, sizeof(*sub));
1116 sub->pts = AV_NOPTS_VALUE;
1119 static int get_bit_rate(AVCodecContext *ctx)
1122 int bits_per_sample;
1124 switch (ctx->codec_type) {
1125 case AVMEDIA_TYPE_VIDEO:
1126 case AVMEDIA_TYPE_DATA:
1127 case AVMEDIA_TYPE_SUBTITLE:
1128 case AVMEDIA_TYPE_ATTACHMENT:
1129 bit_rate = ctx->bit_rate;
1131 case AVMEDIA_TYPE_AUDIO:
1132 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1133 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1142 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1146 ff_unlock_avcodec();
1148 ret = avcodec_open2(avctx, codec, options);
1150 ff_lock_avcodec(avctx);
1154 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1157 AVDictionary *tmp = NULL;
1159 if (avcodec_is_open(avctx))
1162 if ((!codec && !avctx->codec)) {
1163 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1164 return AVERROR(EINVAL);
1166 if ((codec && avctx->codec && codec != avctx->codec)) {
1167 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1168 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1169 return AVERROR(EINVAL);
1172 codec = avctx->codec;
1174 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1175 return AVERROR(EINVAL);
1178 av_dict_copy(&tmp, *options, 0);
1180 ret = ff_lock_avcodec(avctx);
1184 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1185 if (!avctx->internal) {
1186 ret = AVERROR(ENOMEM);
1190 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1191 if (!avctx->internal->pool) {
1192 ret = AVERROR(ENOMEM);
1196 if (codec->priv_data_size > 0) {
1197 if (!avctx->priv_data) {
1198 avctx->priv_data = av_mallocz(codec->priv_data_size);
1199 if (!avctx->priv_data) {
1200 ret = AVERROR(ENOMEM);
1203 if (codec->priv_class) {
1204 *(const AVClass **)avctx->priv_data = codec->priv_class;
1205 av_opt_set_defaults(avctx->priv_data);
1208 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1211 avctx->priv_data = NULL;
1213 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1216 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1217 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1218 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1219 if (avctx->coded_width && avctx->coded_height)
1220 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1221 else if (avctx->width && avctx->height)
1222 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1227 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1228 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1229 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1230 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1231 ff_set_dimensions(avctx, 0, 0);
1234 /* if the decoder init function was already called previously,
1235 * free the already allocated subtitle_header before overwriting it */
1236 if (av_codec_is_decoder(codec))
1237 av_freep(&avctx->subtitle_header);
1239 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1240 ret = AVERROR(EINVAL);
1244 avctx->codec = codec;
1245 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1246 avctx->codec_id == AV_CODEC_ID_NONE) {
1247 avctx->codec_type = codec->type;
1248 avctx->codec_id = codec->id;
1250 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1251 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1252 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1253 ret = AVERROR(EINVAL);
1256 avctx->frame_number = 0;
1257 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1259 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1260 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1261 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1263 av_log(avctx, AV_LOG_ERROR,
1264 "The %s '%s' is experimental but experimental codecs are not enabled, "
1265 "add '-strict %d' if you want to use it.\n",
1266 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1267 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1268 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1269 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1270 codec_string, codec2->name);
1271 ret = AVERROR_EXPERIMENTAL;
1275 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1276 (!avctx->time_base.num || !avctx->time_base.den)) {
1277 avctx->time_base.num = 1;
1278 avctx->time_base.den = avctx->sample_rate;
1282 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1284 if (CONFIG_FRAME_THREAD_ENCODER) {
1285 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1286 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1287 ff_lock_avcodec(avctx);
1293 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1294 ret = ff_thread_init(avctx);
1299 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1300 avctx->thread_count = 1;
1302 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1303 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1304 avctx->codec->max_lowres);
1305 ret = AVERROR(EINVAL);
1309 if (av_codec_is_encoder(avctx->codec)) {
1311 if (avctx->codec->sample_fmts) {
1312 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1313 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1315 if (avctx->channels == 1 &&
1316 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1317 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1318 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1322 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1324 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1325 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1326 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1327 ret = AVERROR(EINVAL);
1331 if (avctx->codec->pix_fmts) {
1332 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1333 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1335 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1336 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1337 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1339 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1340 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1341 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1342 ret = AVERROR(EINVAL);
1346 if (avctx->codec->supported_samplerates) {
1347 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1348 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1350 if (avctx->codec->supported_samplerates[i] == 0) {
1351 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1352 avctx->sample_rate);
1353 ret = AVERROR(EINVAL);
1357 if (avctx->codec->channel_layouts) {
1358 if (!avctx->channel_layout) {
1359 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1361 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1362 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1364 if (avctx->codec->channel_layouts[i] == 0) {
1366 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1367 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1368 ret = AVERROR(EINVAL);
1373 if (avctx->channel_layout && avctx->channels) {
1374 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1375 if (channels != avctx->channels) {
1377 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1378 av_log(avctx, AV_LOG_ERROR,
1379 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1380 buf, channels, avctx->channels);
1381 ret = AVERROR(EINVAL);
1384 } else if (avctx->channel_layout) {
1385 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1387 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1388 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1390 if (avctx->width <= 0 || avctx->height <= 0) {
1391 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1392 ret = AVERROR(EINVAL);
1396 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1397 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1398 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1401 if (!avctx->rc_initial_buffer_occupancy)
1402 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1405 avctx->pts_correction_num_faulty_pts =
1406 avctx->pts_correction_num_faulty_dts = 0;
1407 avctx->pts_correction_last_pts =
1408 avctx->pts_correction_last_dts = INT64_MIN;
1410 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1411 || avctx->internal->frame_thread_encoder)) {
1412 ret = avctx->codec->init(avctx);
1420 if (av_codec_is_decoder(avctx->codec)) {
1421 if (!avctx->bit_rate)
1422 avctx->bit_rate = get_bit_rate(avctx);
1423 /* validate channel layout from the decoder */
1424 if (avctx->channel_layout) {
1425 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1426 if (!avctx->channels)
1427 avctx->channels = channels;
1428 else if (channels != avctx->channels) {
1430 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1431 av_log(avctx, AV_LOG_WARNING,
1432 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1433 "ignoring specified channel layout\n",
1434 buf, channels, avctx->channels);
1435 avctx->channel_layout = 0;
1438 if (avctx->channels && avctx->channels < 0 ||
1439 avctx->channels > FF_SANE_NB_CHANNELS) {
1440 ret = AVERROR(EINVAL);
1443 if (avctx->sub_charenc) {
1444 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1445 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1446 "supported with subtitles codecs\n");
1447 ret = AVERROR(EINVAL);
1449 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1450 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1451 "subtitles character encoding will be ignored\n",
1452 avctx->codec_descriptor->name);
1453 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1455 /* input character encoding is set for a text based subtitle
1456 * codec at this point */
1457 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1458 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1460 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1462 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1463 if (cd == (iconv_t)-1) {
1464 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1465 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1466 ret = AVERROR(errno);
1471 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1472 "conversion needs a libavcodec built with iconv support "
1473 "for this codec\n");
1474 ret = AVERROR(ENOSYS);
1482 ff_unlock_avcodec();
1484 av_dict_free(options);
1491 av_freep(&avctx->priv_data);
1492 if (avctx->internal)
1493 av_freep(&avctx->internal->pool);
1494 av_freep(&avctx->internal);
1495 avctx->codec = NULL;
1499 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1501 if (avpkt->size < 0) {
1502 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1503 return AVERROR(EINVAL);
1505 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1506 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1507 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1508 return AVERROR(EINVAL);
1512 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1513 if (!avpkt->data || avpkt->size < size) {
1514 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1515 avpkt->data = avctx->internal->byte_buffer;
1516 avpkt->size = avctx->internal->byte_buffer_size;
1517 avpkt->destruct = NULL;
1522 AVBufferRef *buf = avpkt->buf;
1523 #if FF_API_DESTRUCT_PACKET
1524 FF_DISABLE_DEPRECATION_WARNINGS
1525 void *destruct = avpkt->destruct;
1526 FF_ENABLE_DEPRECATION_WARNINGS
1529 if (avpkt->size < size) {
1530 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1531 return AVERROR(EINVAL);
1534 av_init_packet(avpkt);
1535 #if FF_API_DESTRUCT_PACKET
1536 FF_DISABLE_DEPRECATION_WARNINGS
1537 avpkt->destruct = destruct;
1538 FF_ENABLE_DEPRECATION_WARNINGS
1544 int ret = av_new_packet(avpkt, size);
1546 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1551 int ff_alloc_packet(AVPacket *avpkt, int size)
1553 return ff_alloc_packet2(NULL, avpkt, size);
1557 * Pad last frame with silence.
1559 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1561 AVFrame *frame = NULL;
1564 if (!(frame = av_frame_alloc()))
1565 return AVERROR(ENOMEM);
1567 frame->format = src->format;
1568 frame->channel_layout = src->channel_layout;
1569 av_frame_set_channels(frame, av_frame_get_channels(src));
1570 frame->nb_samples = s->frame_size;
1571 ret = av_frame_get_buffer(frame, 32);
1575 ret = av_frame_copy_props(frame, src);
1579 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1580 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1582 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1583 frame->nb_samples - src->nb_samples,
1584 s->channels, s->sample_fmt)) < 0)
1592 av_frame_free(&frame);
1596 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1598 const AVFrame *frame,
1599 int *got_packet_ptr)
1602 AVFrame *padded_frame = NULL;
1604 AVPacket user_pkt = *avpkt;
1605 int needs_realloc = !user_pkt.data;
1607 *got_packet_ptr = 0;
1609 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1610 av_free_packet(avpkt);
1611 av_init_packet(avpkt);
1615 /* ensure that extended_data is properly set */
1616 if (frame && !frame->extended_data) {
1617 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1618 avctx->channels > AV_NUM_DATA_POINTERS) {
1619 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1620 "with more than %d channels, but extended_data is not set.\n",
1621 AV_NUM_DATA_POINTERS);
1622 return AVERROR(EINVAL);
1624 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1627 tmp.extended_data = tmp.data;
1631 /* check for valid frame size */
1633 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1634 if (frame->nb_samples > avctx->frame_size) {
1635 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1636 return AVERROR(EINVAL);
1638 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1639 if (frame->nb_samples < avctx->frame_size &&
1640 !avctx->internal->last_audio_frame) {
1641 ret = pad_last_frame(avctx, &padded_frame, frame);
1645 frame = padded_frame;
1646 avctx->internal->last_audio_frame = 1;
1649 if (frame->nb_samples != avctx->frame_size) {
1650 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1651 ret = AVERROR(EINVAL);
1657 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1659 if (*got_packet_ptr) {
1660 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1661 if (avpkt->pts == AV_NOPTS_VALUE)
1662 avpkt->pts = frame->pts;
1663 if (!avpkt->duration)
1664 avpkt->duration = ff_samples_to_time_base(avctx,
1667 avpkt->dts = avpkt->pts;
1672 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1674 if (user_pkt.data) {
1675 if (user_pkt.size >= avpkt->size) {
1676 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1678 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1679 avpkt->size = user_pkt.size;
1682 avpkt->buf = user_pkt.buf;
1683 avpkt->data = user_pkt.data;
1684 avpkt->destruct = user_pkt.destruct;
1686 if (av_dup_packet(avpkt) < 0) {
1687 ret = AVERROR(ENOMEM);
1693 if (needs_realloc && avpkt->data) {
1694 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1696 avpkt->data = avpkt->buf->data;
1699 avctx->frame_number++;
1702 if (ret < 0 || !*got_packet_ptr) {
1703 av_free_packet(avpkt);
1704 av_init_packet(avpkt);
1708 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1709 * this needs to be moved to the encoders, but for now we can do it
1710 * here to simplify things */
1711 avpkt->flags |= AV_PKT_FLAG_KEY;
1714 av_frame_free(&padded_frame);
1719 #if FF_API_OLD_ENCODE_AUDIO
1720 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1721 uint8_t *buf, int buf_size,
1722 const short *samples)
1725 AVFrame frame0 = { { 0 } };
1727 int ret, samples_size, got_packet;
1729 av_init_packet(&pkt);
1731 pkt.size = buf_size;
1735 avcodec_get_frame_defaults(frame);
1737 if (avctx->frame_size) {
1738 frame->nb_samples = avctx->frame_size;
1740 /* if frame_size is not set, the number of samples must be
1741 * calculated from the buffer size */
1743 if (!av_get_bits_per_sample(avctx->codec_id)) {
1744 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1745 "support this codec\n");
1746 return AVERROR(EINVAL);
1748 nb_samples = (int64_t)buf_size * 8 /
1749 (av_get_bits_per_sample(avctx->codec_id) *
1751 if (nb_samples >= INT_MAX)
1752 return AVERROR(EINVAL);
1753 frame->nb_samples = nb_samples;
1756 /* it is assumed that the samples buffer is large enough based on the
1757 * relevant parameters */
1758 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1760 avctx->sample_fmt, 1);
1761 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1763 (const uint8_t *)samples,
1764 samples_size, 1)) < 0)
1767 /* fabricate frame pts from sample count.
1768 * this is needed because the avcodec_encode_audio() API does not have
1769 * a way for the user to provide pts */
1770 if (avctx->sample_rate && avctx->time_base.num)
1771 frame->pts = ff_samples_to_time_base(avctx,
1772 avctx->internal->sample_count);
1774 frame->pts = AV_NOPTS_VALUE;
1775 avctx->internal->sample_count += frame->nb_samples;
1781 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1782 if (!ret && got_packet && avctx->coded_frame) {
1783 avctx->coded_frame->pts = pkt.pts;
1784 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1786 /* free any side data since we cannot return it */
1787 av_packet_free_side_data(&pkt);
1789 if (frame && frame->extended_data != frame->data)
1790 av_freep(&frame->extended_data);
1792 return ret ? ret : pkt.size;
1797 #if FF_API_OLD_ENCODE_VIDEO
1798 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1799 const AVFrame *pict)
1802 int ret, got_packet = 0;
1804 if (buf_size < FF_MIN_BUFFER_SIZE) {
1805 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1809 av_init_packet(&pkt);
1811 pkt.size = buf_size;
1813 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1814 if (!ret && got_packet && avctx->coded_frame) {
1815 avctx->coded_frame->pts = pkt.pts;
1816 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1819 /* free any side data since we cannot return it */
1820 if (pkt.side_data_elems > 0) {
1822 for (i = 0; i < pkt.side_data_elems; i++)
1823 av_free(pkt.side_data[i].data);
1824 av_freep(&pkt.side_data);
1825 pkt.side_data_elems = 0;
1828 return ret ? ret : pkt.size;
1833 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1835 const AVFrame *frame,
1836 int *got_packet_ptr)
1839 AVPacket user_pkt = *avpkt;
1840 int needs_realloc = !user_pkt.data;
1842 *got_packet_ptr = 0;
1844 if(CONFIG_FRAME_THREAD_ENCODER &&
1845 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1846 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1848 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1849 avctx->stats_out[0] = '\0';
1851 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1852 av_free_packet(avpkt);
1853 av_init_packet(avpkt);
1858 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1859 return AVERROR(EINVAL);
1861 av_assert0(avctx->codec->encode2);
1863 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1864 av_assert0(ret <= 0);
1866 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1868 if (user_pkt.data) {
1869 if (user_pkt.size >= avpkt->size) {
1870 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1872 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1873 avpkt->size = user_pkt.size;
1876 avpkt->buf = user_pkt.buf;
1877 avpkt->data = user_pkt.data;
1878 avpkt->destruct = user_pkt.destruct;
1880 if (av_dup_packet(avpkt) < 0) {
1881 ret = AVERROR(ENOMEM);
1887 if (!*got_packet_ptr)
1889 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1890 avpkt->pts = avpkt->dts = frame->pts;
1892 if (needs_realloc && avpkt->data) {
1893 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1895 avpkt->data = avpkt->buf->data;
1898 avctx->frame_number++;
1901 if (ret < 0 || !*got_packet_ptr)
1902 av_free_packet(avpkt);
1904 av_packet_merge_side_data(avpkt);
1910 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1911 const AVSubtitle *sub)
1914 if (sub->start_display_time) {
1915 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1919 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1920 avctx->frame_number++;
1925 * Attempt to guess proper monotonic timestamps for decoded video frames
1926 * which might have incorrect times. Input timestamps may wrap around, in
1927 * which case the output will as well.
1929 * @param pts the pts field of the decoded AVPacket, as passed through
1931 * @param dts the dts field of the decoded AVPacket
1932 * @return one of the input values, may be AV_NOPTS_VALUE
1934 static int64_t guess_correct_pts(AVCodecContext *ctx,
1935 int64_t reordered_pts, int64_t dts)
1937 int64_t pts = AV_NOPTS_VALUE;
1939 if (dts != AV_NOPTS_VALUE) {
1940 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1941 ctx->pts_correction_last_dts = dts;
1943 if (reordered_pts != AV_NOPTS_VALUE) {
1944 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1945 ctx->pts_correction_last_pts = reordered_pts;
1947 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1948 && reordered_pts != AV_NOPTS_VALUE)
1949 pts = reordered_pts;
1956 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1959 const uint8_t *data;
1962 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1966 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1967 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1968 "changes, but PARAM_CHANGE side data was sent to it.\n");
1969 return AVERROR(EINVAL);
1975 flags = bytestream_get_le32(&data);
1978 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1981 avctx->channels = bytestream_get_le32(&data);
1984 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1987 avctx->channel_layout = bytestream_get_le64(&data);
1990 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1993 avctx->sample_rate = bytestream_get_le32(&data);
1996 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1999 avctx->width = bytestream_get_le32(&data);
2000 avctx->height = bytestream_get_le32(&data);
2002 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2009 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2010 return AVERROR_INVALIDDATA;
2013 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
2016 const uint8_t *side_metadata;
2018 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2020 side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2021 AV_PKT_DATA_STRINGS_METADATA, &size);
2022 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2025 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2026 int *got_picture_ptr,
2027 const AVPacket *avpkt)
2029 AVCodecInternal *avci = avctx->internal;
2031 // copy to ensure we do not change avpkt
2032 AVPacket tmp = *avpkt;
2035 return AVERROR(EINVAL);
2036 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2037 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2038 return AVERROR(EINVAL);
2041 *got_picture_ptr = 0;
2042 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2043 return AVERROR(EINVAL);
2045 avcodec_get_frame_defaults(picture);
2047 if (!avctx->refcounted_frames)
2048 av_frame_unref(&avci->to_free);
2050 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2051 int did_split = av_packet_split_side_data(&tmp);
2052 ret = apply_param_change(avctx, &tmp);
2054 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2055 if (avctx->err_recognition & AV_EF_EXPLODE)
2059 avctx->internal->pkt = &tmp;
2060 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2061 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2064 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2066 picture->pkt_dts = avpkt->dts;
2068 if(!avctx->has_b_frames){
2069 av_frame_set_pkt_pos(picture, avpkt->pos);
2071 //FIXME these should be under if(!avctx->has_b_frames)
2072 /* get_buffer is supposed to set frame parameters */
2073 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2074 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2075 if (!picture->width) picture->width = avctx->width;
2076 if (!picture->height) picture->height = avctx->height;
2077 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2080 add_metadata_from_side_data(avctx, picture);
2083 emms_c(); //needed to avoid an emms_c() call before every return;
2085 avctx->internal->pkt = NULL;
2087 av_packet_free_side_data(&tmp);
2092 if (ret < 0 && picture->data[0])
2093 av_frame_unref(picture);
2095 if (*got_picture_ptr) {
2096 if (!avctx->refcounted_frames) {
2097 avci->to_free = *picture;
2098 avci->to_free.extended_data = avci->to_free.data;
2099 memset(picture->buf, 0, sizeof(picture->buf));
2102 avctx->frame_number++;
2103 av_frame_set_best_effort_timestamp(picture,
2104 guess_correct_pts(avctx,
2111 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2112 * make sure it's set correctly */
2113 picture->extended_data = picture->data;
2118 #if FF_API_OLD_DECODE_AUDIO
2119 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2120 int *frame_size_ptr,
2123 AVFrame frame = { { 0 } };
2124 int ret, got_frame = 0;
2126 if (avctx->get_buffer != avcodec_default_get_buffer) {
2127 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2128 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2129 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2130 "avcodec_decode_audio4()\n");
2131 avctx->get_buffer = avcodec_default_get_buffer;
2132 avctx->release_buffer = avcodec_default_release_buffer;
2135 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
2137 if (ret >= 0 && got_frame) {
2139 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2140 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2142 avctx->sample_fmt, 1);
2143 if (*frame_size_ptr < data_size) {
2144 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2145 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2146 return AVERROR(EINVAL);
2149 memcpy(samples, frame.extended_data[0], plane_size);
2151 if (planar && avctx->channels > 1) {
2152 uint8_t *out = ((uint8_t *)samples) + plane_size;
2153 for (ch = 1; ch < avctx->channels; ch++) {
2154 memcpy(out, frame.extended_data[ch], plane_size);
2158 *frame_size_ptr = data_size;
2160 *frame_size_ptr = 0;
2167 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2170 const AVPacket *avpkt)
2172 AVCodecInternal *avci = avctx->internal;
2173 int planar, channels;
2178 if (!avpkt->data && avpkt->size) {
2179 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2180 return AVERROR(EINVAL);
2183 return AVERROR(EINVAL);
2184 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2185 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2186 return AVERROR(EINVAL);
2189 avcodec_get_frame_defaults(frame);
2191 if (!avctx->refcounted_frames)
2192 av_frame_unref(&avci->to_free);
2194 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2197 uint32_t discard_padding = 0;
2198 // copy to ensure we do not change avpkt
2199 AVPacket tmp = *avpkt;
2200 int did_split = av_packet_split_side_data(&tmp);
2201 ret = apply_param_change(avctx, &tmp);
2203 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2204 if (avctx->err_recognition & AV_EF_EXPLODE)
2208 avctx->internal->pkt = &tmp;
2209 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2210 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2212 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2213 frame->pkt_dts = avpkt->dts;
2215 if (ret >= 0 && *got_frame_ptr) {
2216 add_metadata_from_side_data(avctx, frame);
2217 avctx->frame_number++;
2218 av_frame_set_best_effort_timestamp(frame,
2219 guess_correct_pts(avctx,
2222 if (frame->format == AV_SAMPLE_FMT_NONE)
2223 frame->format = avctx->sample_fmt;
2224 if (!frame->channel_layout)
2225 frame->channel_layout = avctx->channel_layout;
2226 if (!av_frame_get_channels(frame))
2227 av_frame_set_channels(frame, avctx->channels);
2228 if (!frame->sample_rate)
2229 frame->sample_rate = avctx->sample_rate;
2232 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2233 if(side && side_size>=10) {
2234 avctx->internal->skip_samples = AV_RL32(side);
2235 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2236 avctx->internal->skip_samples);
2237 discard_padding = AV_RL32(side + 4);
2239 if (avctx->internal->skip_samples && *got_frame_ptr) {
2240 if(frame->nb_samples <= avctx->internal->skip_samples){
2242 avctx->internal->skip_samples -= frame->nb_samples;
2243 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2244 avctx->internal->skip_samples);
2246 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2247 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2248 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2249 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2250 (AVRational){1, avctx->sample_rate},
2251 avctx->pkt_timebase);
2252 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2253 frame->pkt_pts += diff_ts;
2254 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2255 frame->pkt_dts += diff_ts;
2256 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2257 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2259 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2261 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2262 avctx->internal->skip_samples, frame->nb_samples);
2263 frame->nb_samples -= avctx->internal->skip_samples;
2264 avctx->internal->skip_samples = 0;
2268 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
2269 if (discard_padding == frame->nb_samples) {
2272 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2273 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2274 (AVRational){1, avctx->sample_rate},
2275 avctx->pkt_timebase);
2276 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2277 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2279 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2281 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2282 discard_padding, frame->nb_samples);
2283 frame->nb_samples -= discard_padding;
2287 avctx->internal->pkt = NULL;
2289 av_packet_free_side_data(&tmp);
2294 if (ret >= 0 && *got_frame_ptr) {
2295 if (!avctx->refcounted_frames) {
2296 avci->to_free = *frame;
2297 avci->to_free.extended_data = avci->to_free.data;
2298 memset(frame->buf, 0, sizeof(frame->buf));
2299 frame->extended_buf = NULL;
2300 frame->nb_extended_buf = 0;
2302 } else if (frame->data[0])
2303 av_frame_unref(frame);
2306 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2307 * make sure it's set correctly; assume decoders that actually use
2308 * extended_data are doing it correctly */
2309 if (*got_frame_ptr) {
2310 planar = av_sample_fmt_is_planar(frame->format);
2311 channels = av_frame_get_channels(frame);
2312 if (!(planar && channels > AV_NUM_DATA_POINTERS))
2313 frame->extended_data = frame->data;
2315 frame->extended_data = NULL;
2321 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2322 static int recode_subtitle(AVCodecContext *avctx,
2323 AVPacket *outpkt, const AVPacket *inpkt)
2326 iconv_t cd = (iconv_t)-1;
2333 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2337 cd = iconv_open("UTF-8", avctx->sub_charenc);
2338 av_assert0(cd != (iconv_t)-1);
2343 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2344 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2345 ret = AVERROR(ENOMEM);
2349 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2352 outpkt->buf = tmp.buf;
2353 outpkt->data = tmp.data;
2354 outpkt->size = tmp.size;
2355 outb = outpkt->data;
2356 outl = outpkt->size;
2358 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2359 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2360 outl >= outpkt->size || inl != 0) {
2361 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2362 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2363 av_free_packet(&tmp);
2364 ret = AVERROR(errno);
2367 outpkt->size -= outl;
2368 memset(outpkt->data + outpkt->size, 0, outl);
2371 if (cd != (iconv_t)-1)
2375 av_assert0(!"requesting subtitles recoding without iconv");
2379 static int utf8_check(const uint8_t *str)
2381 const uint8_t *byte;
2382 uint32_t codepoint, min;
2386 GET_UTF8(codepoint, *(byte++), return 0;);
2387 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2388 1 << (5 * (byte - str) - 4);
2389 if (codepoint < min || codepoint >= 0x110000 ||
2390 codepoint == 0xFFFE /* BOM */ ||
2391 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2398 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2404 if (!avpkt->data && avpkt->size) {
2405 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2406 return AVERROR(EINVAL);
2409 return AVERROR(EINVAL);
2410 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2411 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2412 return AVERROR(EINVAL);
2416 avcodec_get_subtitle_defaults(sub);
2418 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2419 AVPacket pkt_recoded;
2420 AVPacket tmp = *avpkt;
2421 int did_split = av_packet_split_side_data(&tmp);
2422 //apply_param_change(avctx, &tmp);
2425 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2427 * If the side data is smaller than the buffer padding size, the
2428 * remaining bytes should have already been filled with zeros by the
2429 * original packet allocation anyway. */
2430 memset(tmp.data + tmp.size, 0,
2431 FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2435 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2439 avctx->internal->pkt = &pkt_recoded;
2441 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2442 sub->pts = av_rescale_q(avpkt->pts,
2443 avctx->pkt_timebase, AV_TIME_BASE_Q);
2444 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2445 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2446 !!*got_sub_ptr >= !!sub->num_rects);
2448 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2449 avctx->pkt_timebase.num) {
2450 AVRational ms = { 1, 1000 };
2451 sub->end_display_time = av_rescale_q(avpkt->duration,
2452 avctx->pkt_timebase, ms);
2455 for (i = 0; i < sub->num_rects; i++) {
2456 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2457 av_log(avctx, AV_LOG_ERROR,
2458 "Invalid UTF-8 in decoded subtitles text; "
2459 "maybe missing -sub_charenc option\n");
2460 avsubtitle_free(sub);
2461 return AVERROR_INVALIDDATA;
2465 if (tmp.data != pkt_recoded.data) { // did we recode?
2466 /* prevent from destroying side data from original packet */
2467 pkt_recoded.side_data = NULL;
2468 pkt_recoded.side_data_elems = 0;
2470 av_free_packet(&pkt_recoded);
2472 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2474 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2476 avctx->internal->pkt = NULL;
2480 av_packet_free_side_data(&tmp);
2486 avctx->frame_number++;
2492 void avsubtitle_free(AVSubtitle *sub)
2496 for (i = 0; i < sub->num_rects; i++) {
2497 av_freep(&sub->rects[i]->pict.data[0]);
2498 av_freep(&sub->rects[i]->pict.data[1]);
2499 av_freep(&sub->rects[i]->pict.data[2]);
2500 av_freep(&sub->rects[i]->pict.data[3]);
2501 av_freep(&sub->rects[i]->text);
2502 av_freep(&sub->rects[i]->ass);
2503 av_freep(&sub->rects[i]);
2506 av_freep(&sub->rects);
2508 memset(sub, 0, sizeof(AVSubtitle));
2511 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2515 ff_unlock_avcodec();
2517 ret = avcodec_close(avctx);
2519 ff_lock_avcodec(NULL);
2523 av_cold int avcodec_close(AVCodecContext *avctx)
2530 ret = ff_lock_avcodec(avctx);
2534 if (avcodec_is_open(avctx)) {
2535 FramePool *pool = avctx->internal->pool;
2537 if (CONFIG_FRAME_THREAD_ENCODER &&
2538 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2539 ff_unlock_avcodec();
2540 ff_frame_thread_encoder_free(avctx);
2541 ff_lock_avcodec(avctx);
2543 if (HAVE_THREADS && avctx->internal->thread_ctx)
2544 ff_thread_free(avctx);
2545 if (avctx->codec && avctx->codec->close)
2546 avctx->codec->close(avctx);
2547 avctx->coded_frame = NULL;
2548 avctx->internal->byte_buffer_size = 0;
2549 av_freep(&avctx->internal->byte_buffer);
2550 if (!avctx->refcounted_frames)
2551 av_frame_unref(&avctx->internal->to_free);
2552 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2553 av_buffer_pool_uninit(&pool->pools[i]);
2554 av_freep(&avctx->internal->pool);
2555 av_freep(&avctx->internal);
2558 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2559 av_opt_free(avctx->priv_data);
2561 av_freep(&avctx->priv_data);
2562 if (av_codec_is_encoder(avctx->codec))
2563 av_freep(&avctx->extradata);
2564 avctx->codec = NULL;
2565 avctx->active_thread_type = 0;
2567 ff_unlock_avcodec();
2571 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2574 //This is for future deprecatec codec ids, its empty since
2575 //last major bump but will fill up again over time, please don't remove it
2576 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2577 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
2578 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2579 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2580 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2581 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2582 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2583 case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
2584 case AV_CODEC_ID_HEVC_DEPRECATED: return AV_CODEC_ID_HEVC;
2585 default : return id;
2589 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2591 AVCodec *p, *experimental = NULL;
2593 id= remap_deprecated_codec_id(id);
2595 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2597 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2604 return experimental;
2607 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2609 return find_encdec(id, 1);
2612 AVCodec *avcodec_find_encoder_by_name(const char *name)
2619 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2626 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2628 return find_encdec(id, 0);
2631 AVCodec *avcodec_find_decoder_by_name(const char *name)
2638 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2645 const char *avcodec_get_name(enum AVCodecID id)
2647 const AVCodecDescriptor *cd;
2650 if (id == AV_CODEC_ID_NONE)
2652 cd = avcodec_descriptor_get(id);
2655 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2656 codec = avcodec_find_decoder(id);
2659 codec = avcodec_find_encoder(id);
2662 return "unknown_codec";
2665 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2667 int i, len, ret = 0;
2669 #define TAG_PRINT(x) \
2670 (((x) >= '0' && (x) <= '9') || \
2671 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2672 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2674 for (i = 0; i < 4; i++) {
2675 len = snprintf(buf, buf_size,
2676 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2678 buf_size = buf_size > len ? buf_size - len : 0;
2685 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2687 const char *codec_type;
2688 const char *codec_name;
2689 const char *profile = NULL;
2692 AVRational display_aspect_ratio;
2694 if (!buf || buf_size <= 0)
2696 codec_type = av_get_media_type_string(enc->codec_type);
2697 codec_name = avcodec_get_name(enc->codec_id);
2698 if (enc->profile != FF_PROFILE_UNKNOWN) {
2702 p = encode ? avcodec_find_encoder(enc->codec_id) :
2703 avcodec_find_decoder(enc->codec_id);
2705 profile = av_get_profile_name(p, enc->profile);
2708 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2710 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2712 if (enc->codec && strcmp(enc->codec->name, codec_name))
2713 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2716 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2717 if (enc->codec_tag) {
2719 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2720 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2721 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2724 switch (enc->codec_type) {
2725 case AVMEDIA_TYPE_VIDEO:
2726 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2727 char detail[256] = "(";
2728 const char *colorspace_name;
2729 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2731 av_get_pix_fmt_name(enc->pix_fmt));
2732 if (enc->bits_per_raw_sample &&
2733 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2734 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2735 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2736 av_strlcatf(detail, sizeof(detail),
2737 enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
2739 colorspace_name = av_get_colorspace_name(enc->colorspace);
2740 if (colorspace_name)
2741 av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
2743 if (strlen(detail) > 1) {
2744 detail[strlen(detail) - 2] = 0;
2745 av_strlcatf(buf, buf_size, "%s)", detail);
2749 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2751 enc->width, enc->height);
2752 if (enc->sample_aspect_ratio.num) {
2753 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2754 enc->width * enc->sample_aspect_ratio.num,
2755 enc->height * enc->sample_aspect_ratio.den,
2757 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2758 " [SAR %d:%d DAR %d:%d]",
2759 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2760 display_aspect_ratio.num, display_aspect_ratio.den);
2762 if (av_log_get_level() >= AV_LOG_DEBUG) {
2763 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2764 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2766 enc->time_base.num / g, enc->time_base.den / g);
2770 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2771 ", q=%d-%d", enc->qmin, enc->qmax);
2774 case AVMEDIA_TYPE_AUDIO:
2775 if (enc->sample_rate) {
2776 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2777 ", %d Hz", enc->sample_rate);
2779 av_strlcat(buf, ", ", buf_size);
2780 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2781 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2782 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2783 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2786 case AVMEDIA_TYPE_DATA:
2787 if (av_log_get_level() >= AV_LOG_DEBUG) {
2788 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2790 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2792 enc->time_base.num / g, enc->time_base.den / g);
2795 case AVMEDIA_TYPE_SUBTITLE:
2797 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2798 ", %dx%d", enc->width, enc->height);
2804 if (enc->flags & CODEC_FLAG_PASS1)
2805 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2807 if (enc->flags & CODEC_FLAG_PASS2)
2808 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2811 bitrate = get_bit_rate(enc);
2813 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2814 ", %d kb/s", bitrate / 1000);
2815 } else if (enc->rc_max_rate > 0) {
2816 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2817 ", max. %d kb/s", enc->rc_max_rate / 1000);
2821 const char *av_get_profile_name(const AVCodec *codec, int profile)
2824 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2827 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2828 if (p->profile == profile)
2834 unsigned avcodec_version(void)
2836 // av_assert0(AV_CODEC_ID_V410==164);
2837 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2838 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2839 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2840 av_assert0(AV_CODEC_ID_SRT==94216);
2841 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2843 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2844 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2845 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2846 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2847 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2848 return LIBAVCODEC_VERSION_INT;
2851 const char *avcodec_configuration(void)
2853 return FFMPEG_CONFIGURATION;
2856 const char *avcodec_license(void)
2858 #define LICENSE_PREFIX "libavcodec license: "
2859 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2862 void avcodec_flush_buffers(AVCodecContext *avctx)
2864 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2865 ff_thread_flush(avctx);
2866 else if (avctx->codec->flush)
2867 avctx->codec->flush(avctx);
2869 avctx->pts_correction_last_pts =
2870 avctx->pts_correction_last_dts = INT64_MIN;
2872 if (!avctx->refcounted_frames)
2873 av_frame_unref(&avctx->internal->to_free);
2876 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2879 case AV_CODEC_ID_8SVX_EXP:
2880 case AV_CODEC_ID_8SVX_FIB:
2881 case AV_CODEC_ID_ADPCM_CT:
2882 case AV_CODEC_ID_ADPCM_IMA_APC:
2883 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2884 case AV_CODEC_ID_ADPCM_IMA_OKI:
2885 case AV_CODEC_ID_ADPCM_IMA_WS:
2886 case AV_CODEC_ID_ADPCM_G722:
2887 case AV_CODEC_ID_ADPCM_YAMAHA:
2889 case AV_CODEC_ID_PCM_ALAW:
2890 case AV_CODEC_ID_PCM_MULAW:
2891 case AV_CODEC_ID_PCM_S8:
2892 case AV_CODEC_ID_PCM_S8_PLANAR:
2893 case AV_CODEC_ID_PCM_U8:
2894 case AV_CODEC_ID_PCM_ZORK:
2896 case AV_CODEC_ID_PCM_S16BE:
2897 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2898 case AV_CODEC_ID_PCM_S16LE:
2899 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2900 case AV_CODEC_ID_PCM_U16BE:
2901 case AV_CODEC_ID_PCM_U16LE:
2903 case AV_CODEC_ID_PCM_S24DAUD:
2904 case AV_CODEC_ID_PCM_S24BE:
2905 case AV_CODEC_ID_PCM_S24LE:
2906 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2907 case AV_CODEC_ID_PCM_U24BE:
2908 case AV_CODEC_ID_PCM_U24LE:
2910 case AV_CODEC_ID_PCM_S32BE:
2911 case AV_CODEC_ID_PCM_S32LE:
2912 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2913 case AV_CODEC_ID_PCM_U32BE:
2914 case AV_CODEC_ID_PCM_U32LE:
2915 case AV_CODEC_ID_PCM_F32BE:
2916 case AV_CODEC_ID_PCM_F32LE:
2918 case AV_CODEC_ID_PCM_F64BE:
2919 case AV_CODEC_ID_PCM_F64LE:
2926 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2928 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2929 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2930 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2931 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2932 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2933 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2934 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2935 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2936 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2937 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2938 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2940 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2941 return AV_CODEC_ID_NONE;
2942 if (be < 0 || be > 1)
2944 return map[fmt][be];
2947 int av_get_bits_per_sample(enum AVCodecID codec_id)
2950 case AV_CODEC_ID_ADPCM_SBPRO_2:
2952 case AV_CODEC_ID_ADPCM_SBPRO_3:
2954 case AV_CODEC_ID_ADPCM_SBPRO_4:
2955 case AV_CODEC_ID_ADPCM_IMA_WAV:
2956 case AV_CODEC_ID_ADPCM_IMA_QT:
2957 case AV_CODEC_ID_ADPCM_SWF:
2958 case AV_CODEC_ID_ADPCM_MS:
2961 return av_get_exact_bits_per_sample(codec_id);
2965 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2967 int id, sr, ch, ba, tag, bps;
2969 id = avctx->codec_id;
2970 sr = avctx->sample_rate;
2971 ch = avctx->channels;
2972 ba = avctx->block_align;
2973 tag = avctx->codec_tag;
2974 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2976 /* codecs with an exact constant bits per sample */
2977 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2978 return (frame_bytes * 8LL) / (bps * ch);
2979 bps = avctx->bits_per_coded_sample;
2981 /* codecs with a fixed packet duration */
2983 case AV_CODEC_ID_ADPCM_ADX: return 32;
2984 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2985 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2986 case AV_CODEC_ID_AMR_NB:
2987 case AV_CODEC_ID_EVRC:
2988 case AV_CODEC_ID_GSM:
2989 case AV_CODEC_ID_QCELP:
2990 case AV_CODEC_ID_RA_288: return 160;
2991 case AV_CODEC_ID_AMR_WB:
2992 case AV_CODEC_ID_GSM_MS: return 320;
2993 case AV_CODEC_ID_MP1: return 384;
2994 case AV_CODEC_ID_ATRAC1: return 512;
2995 case AV_CODEC_ID_ATRAC3: return 1024;
2996 case AV_CODEC_ID_MP2:
2997 case AV_CODEC_ID_MUSEPACK7: return 1152;
2998 case AV_CODEC_ID_AC3: return 1536;
3002 /* calc from sample rate */
3003 if (id == AV_CODEC_ID_TTA)
3004 return 256 * sr / 245;
3007 /* calc from sample rate and channels */
3008 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3009 return (480 << (sr / 22050)) / ch;
3014 /* calc from block_align */
3015 if (id == AV_CODEC_ID_SIPR) {
3017 case 20: return 160;
3018 case 19: return 144;
3019 case 29: return 288;
3020 case 37: return 480;
3022 } else if (id == AV_CODEC_ID_ILBC) {
3024 case 38: return 160;
3025 case 50: return 240;
3030 if (frame_bytes > 0) {
3031 /* calc from frame_bytes only */
3032 if (id == AV_CODEC_ID_TRUESPEECH)
3033 return 240 * (frame_bytes / 32);
3034 if (id == AV_CODEC_ID_NELLYMOSER)
3035 return 256 * (frame_bytes / 64);
3036 if (id == AV_CODEC_ID_RA_144)
3037 return 160 * (frame_bytes / 20);
3038 if (id == AV_CODEC_ID_G723_1)
3039 return 240 * (frame_bytes / 24);
3042 /* calc from frame_bytes and bits_per_coded_sample */
3043 if (id == AV_CODEC_ID_ADPCM_G726)
3044 return frame_bytes * 8 / bps;
3048 /* calc from frame_bytes and channels */
3050 case AV_CODEC_ID_ADPCM_AFC:
3051 return frame_bytes / (9 * ch) * 16;
3052 case AV_CODEC_ID_ADPCM_DTK:
3053 return frame_bytes / (16 * ch) * 28;
3054 case AV_CODEC_ID_ADPCM_4XM:
3055 case AV_CODEC_ID_ADPCM_IMA_ISS:
3056 return (frame_bytes - 4 * ch) * 2 / ch;
3057 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3058 return (frame_bytes - 4) * 2 / ch;
3059 case AV_CODEC_ID_ADPCM_IMA_AMV:
3060 return (frame_bytes - 8) * 2 / ch;
3061 case AV_CODEC_ID_ADPCM_XA:
3062 return (frame_bytes / 128) * 224 / ch;
3063 case AV_CODEC_ID_INTERPLAY_DPCM:
3064 return (frame_bytes - 6 - ch) / ch;
3065 case AV_CODEC_ID_ROQ_DPCM:
3066 return (frame_bytes - 8) / ch;
3067 case AV_CODEC_ID_XAN_DPCM:
3068 return (frame_bytes - 2 * ch) / ch;
3069 case AV_CODEC_ID_MACE3:
3070 return 3 * frame_bytes / ch;
3071 case AV_CODEC_ID_MACE6:
3072 return 6 * frame_bytes / ch;
3073 case AV_CODEC_ID_PCM_LXF:
3074 return 2 * (frame_bytes / (5 * ch));
3075 case AV_CODEC_ID_IAC:
3076 case AV_CODEC_ID_IMC:
3077 return 4 * frame_bytes / ch;
3081 /* calc from frame_bytes, channels, and codec_tag */
3082 if (id == AV_CODEC_ID_SOL_DPCM) {
3084 return frame_bytes / ch;
3086 return frame_bytes * 2 / ch;
3091 /* calc from frame_bytes, channels, and block_align */
3092 int blocks = frame_bytes / ba;
3093 switch (avctx->codec_id) {
3094 case AV_CODEC_ID_ADPCM_IMA_WAV:
3095 if (bps < 2 || bps > 5)
3097 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3098 case AV_CODEC_ID_ADPCM_IMA_DK3:
3099 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3100 case AV_CODEC_ID_ADPCM_IMA_DK4:
3101 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3102 case AV_CODEC_ID_ADPCM_IMA_RAD:
3103 return blocks * ((ba - 4 * ch) * 2 / ch);
3104 case AV_CODEC_ID_ADPCM_MS:
3105 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3110 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3111 switch (avctx->codec_id) {
3112 case AV_CODEC_ID_PCM_DVD:
3115 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3116 case AV_CODEC_ID_PCM_BLURAY:
3119 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3120 case AV_CODEC_ID_S302M:
3121 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3131 int ff_thread_init(AVCodecContext *s)
3138 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3152 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3155 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3159 #if FF_API_MISSING_SAMPLE
3160 FF_DISABLE_DEPRECATION_WARNINGS
3161 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3163 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3164 "version to the newest one from Git. If the problem still "
3165 "occurs, it means that your file has a feature which has not "
3166 "been implemented.\n", feature);
3168 av_log_ask_for_sample(avc, NULL);
3171 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3173 va_list argument_list;
3175 va_start(argument_list, msg);
3178 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3179 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3180 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
3181 "and contact the ffmpeg-devel mailing list.\n");
3183 va_end(argument_list);
3185 FF_ENABLE_DEPRECATION_WARNINGS
3186 #endif /* FF_API_MISSING_SAMPLE */
3188 static AVHWAccel *first_hwaccel = NULL;
3190 void av_register_hwaccel(AVHWAccel *hwaccel)
3192 AVHWAccel **p = &first_hwaccel;
3193 hwaccel->next = NULL;
3194 while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3198 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
3200 return hwaccel ? hwaccel->next : first_hwaccel;
3203 AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
3205 enum AVCodecID codec_id = avctx->codec->id;
3206 enum AVPixelFormat pix_fmt = avctx->pix_fmt;
3208 AVHWAccel *hwaccel = NULL;
3210 while ((hwaccel = av_hwaccel_next(hwaccel)))
3211 if (hwaccel->id == codec_id
3212 && hwaccel->pix_fmt == pix_fmt)
3217 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3220 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3222 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3229 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3231 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3237 int ff_lock_avcodec(AVCodecContext *log_ctx)
3240 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3243 entangled_thread_counter++;
3244 if (entangled_thread_counter != 1) {
3245 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3247 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3248 ff_avcodec_locked = 1;
3249 ff_unlock_avcodec();
3250 return AVERROR(EINVAL);
3252 av_assert0(!ff_avcodec_locked);
3253 ff_avcodec_locked = 1;
3257 int ff_unlock_avcodec(void)
3259 av_assert0(ff_avcodec_locked);
3260 ff_avcodec_locked = 0;
3261 entangled_thread_counter--;
3263 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3269 int avpriv_lock_avformat(void)
3272 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3278 int avpriv_unlock_avformat(void)
3281 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3287 unsigned int avpriv_toupper4(unsigned int x)
3289 return av_toupper(x & 0xFF) +
3290 (av_toupper((x >> 8) & 0xFF) << 8) +
3291 (av_toupper((x >> 16) & 0xFF) << 16) +
3292 (av_toupper((x >> 24) & 0xFF) << 24);
3295 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3299 dst->owner = src->owner;
3301 ret = av_frame_ref(dst->f, src->f);
3305 if (src->progress &&
3306 !(dst->progress = av_buffer_ref(src->progress))) {
3307 ff_thread_release_buffer(dst->owner, dst);
3308 return AVERROR(ENOMEM);
3316 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3318 return avctx->get_format(avctx, fmt);
3321 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3324 return ff_get_buffer(avctx, f->f, flags);
3327 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3329 av_frame_unref(f->f);
3332 void ff_thread_finish_setup(AVCodecContext *avctx)
3336 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3340 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3344 int ff_thread_can_start_frame(AVCodecContext *avctx)
3349 int ff_alloc_entries(AVCodecContext *avctx, int count)
3354 void ff_reset_entries(AVCodecContext *avctx)
3358 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3362 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3368 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3370 AVCodec *c= avcodec_find_decoder(codec_id);
3372 c= avcodec_find_encoder(codec_id);
3376 if (codec_id <= AV_CODEC_ID_NONE)
3377 return AVMEDIA_TYPE_UNKNOWN;
3378 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3379 return AVMEDIA_TYPE_VIDEO;
3380 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3381 return AVMEDIA_TYPE_AUDIO;
3382 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3383 return AVMEDIA_TYPE_SUBTITLE;
3385 return AVMEDIA_TYPE_UNKNOWN;
3388 int avcodec_is_open(AVCodecContext *s)
3390 return !!s->internal;
3393 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3398 ret = av_bprint_finalize(buf, &str);
3401 avctx->extradata = str;
3402 /* Note: the string is NUL terminated (so extradata can be read as a
3403 * string), but the ending character is not accounted in the size (in
3404 * binary formats you are likely not supposed to mux that character). When
3405 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3407 avctx->extradata_size = buf->len;
3411 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3413 uint32_t *av_restrict state)
3417 av_assert0(p <= end);
3421 for (i = 0; i < 3; i++) {
3422 uint32_t tmp = *state << 8;
3423 *state = tmp + *(p++);
3424 if (tmp == 0x100 || p == end)
3429 if (p[-1] > 1 ) p += 3;
3430 else if (p[-2] ) p += 2;
3431 else if (p[-3]|(p[-1]-1)) p++;
3438 p = FFMIN(p, end) - 4;
3439 *state = AV_RB32(p);