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"
50 #include "bytestream.h"
63 #include "compat/w32pthreads.h"
65 #include "compat/os2threads.h"
68 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
69 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
71 void * volatile * mutex = arg;
79 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
81 return AVERROR(ENOMEM);
82 if ((err = pthread_mutex_init(tmp, NULL))) {
86 if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
87 pthread_mutex_destroy(tmp);
92 if ((err = pthread_mutex_lock(*mutex)))
97 if ((err = pthread_mutex_unlock(*mutex)))
101 case AV_LOCK_DESTROY:
103 pthread_mutex_destroy(*mutex);
105 avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
110 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
112 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
116 volatile int ff_avcodec_locked;
117 static int volatile entangled_thread_counter = 0;
118 static void *codec_mutex;
119 static void *avformat_mutex;
121 #if CONFIG_RAISE_MAJOR
122 # define LIBNAME "LIBAVCODEC_155"
124 # define LIBNAME "LIBAVCODEC_55"
127 #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
128 FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), LIBNAME)
130 return av_fast_realloc(ptr, size, min_size);
133 FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), LIBNAME)
135 av_fast_malloc(ptr, size, min_size);
139 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
142 if (min_size < *size)
144 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
146 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
153 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
156 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
161 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
162 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
165 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
168 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
173 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
174 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
177 /* encoder management */
178 static AVCodec *first_avcodec = NULL;
179 static AVCodec **last_avcodec = &first_avcodec;
181 AVCodec *av_codec_next(const AVCodec *c)
186 return first_avcodec;
189 static av_cold void avcodec_init(void)
191 static int initialized = 0;
193 if (initialized != 0)
198 ff_dsputil_static_init();
201 int av_codec_is_encoder(const AVCodec *codec)
203 return codec && (codec->encode_sub || codec->encode2);
206 int av_codec_is_decoder(const AVCodec *codec)
208 return codec && codec->decode;
211 av_cold void avcodec_register(AVCodec *codec)
218 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
220 last_avcodec = &codec->next;
222 if (codec->init_static_data)
223 codec->init_static_data(codec);
227 unsigned avcodec_get_edge_width(void)
233 #if FF_API_SET_DIMENSIONS
234 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
236 int ret = ff_set_dimensions(s, width, height);
238 av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
243 int ff_set_dimensions(AVCodecContext *s, int width, int height)
245 int ret = av_image_check_size(width, height, 0, s);
250 s->coded_width = width;
251 s->coded_height = height;
252 s->width = FF_CEIL_RSHIFT(width, s->lowres);
253 s->height = FF_CEIL_RSHIFT(height, s->lowres);
258 int ff_side_data_update_matrix_encoding(AVFrame *frame,
259 enum AVMatrixEncoding matrix_encoding)
261 AVFrameSideData *side_data;
262 enum AVMatrixEncoding *data;
264 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
266 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
267 sizeof(enum AVMatrixEncoding));
270 return AVERROR(ENOMEM);
272 data = (enum AVMatrixEncoding*)side_data->data;
273 *data = matrix_encoding;
278 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
279 int linesize_align[AV_NUM_DATA_POINTERS])
285 switch (s->pix_fmt) {
286 case AV_PIX_FMT_YUV420P:
287 case AV_PIX_FMT_YUYV422:
288 case AV_PIX_FMT_UYVY422:
289 case AV_PIX_FMT_YUV422P:
290 case AV_PIX_FMT_YUV440P:
291 case AV_PIX_FMT_YUV444P:
292 case AV_PIX_FMT_GBRAP:
293 case AV_PIX_FMT_GBRP:
294 case AV_PIX_FMT_GRAY8:
295 case AV_PIX_FMT_GRAY16BE:
296 case AV_PIX_FMT_GRAY16LE:
297 case AV_PIX_FMT_YUVJ420P:
298 case AV_PIX_FMT_YUVJ422P:
299 case AV_PIX_FMT_YUVJ440P:
300 case AV_PIX_FMT_YUVJ444P:
301 case AV_PIX_FMT_YUVA420P:
302 case AV_PIX_FMT_YUVA422P:
303 case AV_PIX_FMT_YUVA444P:
304 case AV_PIX_FMT_YUV420P9LE:
305 case AV_PIX_FMT_YUV420P9BE:
306 case AV_PIX_FMT_YUV420P10LE:
307 case AV_PIX_FMT_YUV420P10BE:
308 case AV_PIX_FMT_YUV420P12LE:
309 case AV_PIX_FMT_YUV420P12BE:
310 case AV_PIX_FMT_YUV420P14LE:
311 case AV_PIX_FMT_YUV420P14BE:
312 case AV_PIX_FMT_YUV420P16LE:
313 case AV_PIX_FMT_YUV420P16BE:
314 case AV_PIX_FMT_YUVA420P9LE:
315 case AV_PIX_FMT_YUVA420P9BE:
316 case AV_PIX_FMT_YUVA420P10LE:
317 case AV_PIX_FMT_YUVA420P10BE:
318 case AV_PIX_FMT_YUVA420P16LE:
319 case AV_PIX_FMT_YUVA420P16BE:
320 case AV_PIX_FMT_YUV422P9LE:
321 case AV_PIX_FMT_YUV422P9BE:
322 case AV_PIX_FMT_YUV422P10LE:
323 case AV_PIX_FMT_YUV422P10BE:
324 case AV_PIX_FMT_YUV422P12LE:
325 case AV_PIX_FMT_YUV422P12BE:
326 case AV_PIX_FMT_YUV422P14LE:
327 case AV_PIX_FMT_YUV422P14BE:
328 case AV_PIX_FMT_YUV422P16LE:
329 case AV_PIX_FMT_YUV422P16BE:
330 case AV_PIX_FMT_YUVA422P9LE:
331 case AV_PIX_FMT_YUVA422P9BE:
332 case AV_PIX_FMT_YUVA422P10LE:
333 case AV_PIX_FMT_YUVA422P10BE:
334 case AV_PIX_FMT_YUVA422P16LE:
335 case AV_PIX_FMT_YUVA422P16BE:
336 case AV_PIX_FMT_YUV444P9LE:
337 case AV_PIX_FMT_YUV444P9BE:
338 case AV_PIX_FMT_YUV444P10LE:
339 case AV_PIX_FMT_YUV444P10BE:
340 case AV_PIX_FMT_YUV444P12LE:
341 case AV_PIX_FMT_YUV444P12BE:
342 case AV_PIX_FMT_YUV444P14LE:
343 case AV_PIX_FMT_YUV444P14BE:
344 case AV_PIX_FMT_YUV444P16LE:
345 case AV_PIX_FMT_YUV444P16BE:
346 case AV_PIX_FMT_YUVA444P9LE:
347 case AV_PIX_FMT_YUVA444P9BE:
348 case AV_PIX_FMT_YUVA444P10LE:
349 case AV_PIX_FMT_YUVA444P10BE:
350 case AV_PIX_FMT_YUVA444P16LE:
351 case AV_PIX_FMT_YUVA444P16BE:
352 case AV_PIX_FMT_GBRP9LE:
353 case AV_PIX_FMT_GBRP9BE:
354 case AV_PIX_FMT_GBRP10LE:
355 case AV_PIX_FMT_GBRP10BE:
356 case AV_PIX_FMT_GBRP12LE:
357 case AV_PIX_FMT_GBRP12BE:
358 case AV_PIX_FMT_GBRP14LE:
359 case AV_PIX_FMT_GBRP14BE:
360 w_align = 16; //FIXME assume 16 pixel per macroblock
361 h_align = 16 * 2; // interlaced needs 2 macroblocks height
363 case AV_PIX_FMT_YUV411P:
364 case AV_PIX_FMT_YUVJ411P:
365 case AV_PIX_FMT_UYYVYY411:
369 case AV_PIX_FMT_YUV410P:
370 if (s->codec_id == AV_CODEC_ID_SVQ1) {
375 case AV_PIX_FMT_RGB555:
376 if (s->codec_id == AV_CODEC_ID_RPZA) {
381 case AV_PIX_FMT_PAL8:
382 case AV_PIX_FMT_BGR8:
383 case AV_PIX_FMT_RGB8:
384 if (s->codec_id == AV_CODEC_ID_SMC ||
385 s->codec_id == AV_CODEC_ID_CINEPAK) {
390 case AV_PIX_FMT_BGR24:
391 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
392 (s->codec_id == AV_CODEC_ID_ZLIB)) {
397 case AV_PIX_FMT_RGB24:
398 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
409 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
410 w_align = FFMAX(w_align, 8);
413 *width = FFALIGN(*width, w_align);
414 *height = FFALIGN(*height, h_align);
415 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
416 // some of the optimized chroma MC reads one line too much
417 // which is also done in mpeg decoders with lowres > 0
420 for (i = 0; i < 4; i++)
421 linesize_align[i] = STRIDE_ALIGN;
424 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
426 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
427 int chroma_shift = desc->log2_chroma_w;
428 int linesize_align[AV_NUM_DATA_POINTERS];
431 avcodec_align_dimensions2(s, width, height, linesize_align);
432 align = FFMAX(linesize_align[0], linesize_align[3]);
433 linesize_align[1] <<= chroma_shift;
434 linesize_align[2] <<= chroma_shift;
435 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
436 *width = FFALIGN(*width, align);
439 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
441 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
442 return AVERROR(EINVAL);
445 *xpos = (pos&1) * 128;
446 *ypos = ((pos>>1)^(pos<4)) * 128;
451 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
455 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
456 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
459 return AVCHROMA_LOC_UNSPECIFIED;
462 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
463 enum AVSampleFormat sample_fmt, const uint8_t *buf,
464 int buf_size, int align)
466 int ch, planar, needed_size, ret = 0;
468 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
469 frame->nb_samples, sample_fmt,
471 if (buf_size < needed_size)
472 return AVERROR(EINVAL);
474 planar = av_sample_fmt_is_planar(sample_fmt);
475 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
476 if (!(frame->extended_data = av_mallocz(nb_channels *
477 sizeof(*frame->extended_data))))
478 return AVERROR(ENOMEM);
480 frame->extended_data = frame->data;
483 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
484 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
485 sample_fmt, align)) < 0) {
486 if (frame->extended_data != frame->data)
487 av_freep(&frame->extended_data);
490 if (frame->extended_data != frame->data) {
491 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
492 frame->data[ch] = frame->extended_data[ch];
498 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
500 FramePool *pool = avctx->internal->pool;
503 switch (avctx->codec_type) {
504 case AVMEDIA_TYPE_VIDEO: {
507 int w = frame->width;
508 int h = frame->height;
509 int tmpsize, unaligned;
511 if (pool->format == frame->format &&
512 pool->width == frame->width && pool->height == frame->height)
515 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
517 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
523 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
524 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
525 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
526 // increase alignment of w for next try (rhs gives the lowest bit set in w)
530 for (i = 0; i < 4; i++)
531 unaligned |= picture.linesize[i] % pool->stride_align[i];
534 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
535 NULL, picture.linesize);
539 for (i = 0; i < 3 && picture.data[i + 1]; i++)
540 size[i] = picture.data[i + 1] - picture.data[i];
541 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
543 for (i = 0; i < 4; i++) {
544 av_buffer_pool_uninit(&pool->pools[i]);
545 pool->linesize[i] = picture.linesize[i];
547 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
548 CONFIG_MEMORY_POISONING ?
551 if (!pool->pools[i]) {
552 ret = AVERROR(ENOMEM);
557 pool->format = frame->format;
558 pool->width = frame->width;
559 pool->height = frame->height;
563 case AVMEDIA_TYPE_AUDIO: {
564 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
565 int planar = av_sample_fmt_is_planar(frame->format);
566 int planes = planar ? ch : 1;
568 if (pool->format == frame->format && pool->planes == planes &&
569 pool->channels == ch && frame->nb_samples == pool->samples)
572 av_buffer_pool_uninit(&pool->pools[0]);
573 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
574 frame->nb_samples, frame->format, 0);
578 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
579 if (!pool->pools[0]) {
580 ret = AVERROR(ENOMEM);
584 pool->format = frame->format;
585 pool->planes = planes;
587 pool->samples = frame->nb_samples;
590 default: av_assert0(0);
594 for (i = 0; i < 4; i++)
595 av_buffer_pool_uninit(&pool->pools[i]);
597 pool->planes = pool->channels = pool->samples = 0;
598 pool->width = pool->height = 0;
602 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
604 FramePool *pool = avctx->internal->pool;
605 int planes = pool->planes;
608 frame->linesize[0] = pool->linesize[0];
610 if (planes > AV_NUM_DATA_POINTERS) {
611 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
612 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
613 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
614 sizeof(*frame->extended_buf));
615 if (!frame->extended_data || !frame->extended_buf) {
616 av_freep(&frame->extended_data);
617 av_freep(&frame->extended_buf);
618 return AVERROR(ENOMEM);
621 frame->extended_data = frame->data;
622 av_assert0(frame->nb_extended_buf == 0);
625 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
626 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
629 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
631 for (i = 0; i < frame->nb_extended_buf; i++) {
632 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
633 if (!frame->extended_buf[i])
635 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
638 if (avctx->debug & FF_DEBUG_BUFFERS)
639 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
643 av_frame_unref(frame);
644 return AVERROR(ENOMEM);
647 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
649 FramePool *pool = s->internal->pool;
650 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
651 int pixel_size = desc->comp[0].step_minus1 + 1;
652 int h_chroma_shift, v_chroma_shift;
655 if (pic->data[0] != NULL) {
656 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
660 memset(pic->data, 0, sizeof(pic->data));
661 pic->extended_data = pic->data;
663 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
665 for (i = 0; i < 4 && pool->pools[i]; i++) {
666 const int h_shift = i == 0 ? 0 : h_chroma_shift;
667 const int v_shift = i == 0 ? 0 : v_chroma_shift;
668 int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
670 pic->linesize[i] = pool->linesize[i];
672 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
676 // no edge if EDGE EMU or not planar YUV
677 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
678 pic->data[i] = pic->buf[i]->data;
680 pic->data[i] = pic->buf[i]->data +
681 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
682 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
685 for (; i < AV_NUM_DATA_POINTERS; i++) {
687 pic->linesize[i] = 0;
689 if (pic->data[1] && !pic->data[2])
690 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
692 if (s->debug & FF_DEBUG_BUFFERS)
693 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
698 return AVERROR(ENOMEM);
701 void avpriv_color_frame(AVFrame *frame, const int c[4])
703 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
706 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
708 for (p = 0; p<desc->nb_components; p++) {
709 uint8_t *dst = frame->data[p];
710 int is_chroma = p == 1 || p == 2;
711 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
712 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
713 for (y = 0; y < height; y++) {
714 if (desc->comp[0].depth_minus1 >= 8) {
715 for (x = 0; x<bytes; x++)
716 ((uint16_t*)dst)[x] = c[p];
718 memset(dst, c[p], bytes);
719 dst += frame->linesize[p];
724 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
728 if ((ret = update_frame_pool(avctx, frame)) < 0)
731 #if FF_API_GET_BUFFER
732 FF_DISABLE_DEPRECATION_WARNINGS
733 frame->type = FF_BUFFER_TYPE_INTERNAL;
734 FF_ENABLE_DEPRECATION_WARNINGS
737 switch (avctx->codec_type) {
738 case AVMEDIA_TYPE_VIDEO:
739 return video_get_buffer(avctx, frame);
740 case AVMEDIA_TYPE_AUDIO:
741 return audio_get_buffer(avctx, frame);
747 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
749 if (avctx->internal->pkt) {
750 frame->pkt_pts = avctx->internal->pkt->pts;
751 av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
752 av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
753 av_frame_set_pkt_size (frame, avctx->internal->pkt->size);
755 frame->pkt_pts = AV_NOPTS_VALUE;
756 av_frame_set_pkt_pos (frame, -1);
757 av_frame_set_pkt_duration(frame, 0);
758 av_frame_set_pkt_size (frame, -1);
760 frame->reordered_opaque = avctx->reordered_opaque;
762 switch (avctx->codec->type) {
763 case AVMEDIA_TYPE_VIDEO:
764 frame->format = avctx->pix_fmt;
765 if (!frame->sample_aspect_ratio.num)
766 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
767 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
768 av_frame_set_colorspace(frame, avctx->colorspace);
769 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
770 av_frame_set_color_range(frame, avctx->color_range);
772 case AVMEDIA_TYPE_AUDIO:
773 if (!frame->sample_rate)
774 frame->sample_rate = avctx->sample_rate;
775 if (frame->format < 0)
776 frame->format = avctx->sample_fmt;
777 if (!frame->channel_layout) {
778 if (avctx->channel_layout) {
779 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
781 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
783 return AVERROR(EINVAL);
786 frame->channel_layout = avctx->channel_layout;
788 if (avctx->channels > FF_SANE_NB_CHANNELS) {
789 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
791 return AVERROR(ENOSYS);
795 av_frame_set_channels(frame, avctx->channels);
801 #if FF_API_GET_BUFFER
802 FF_DISABLE_DEPRECATION_WARNINGS
803 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
805 return avcodec_default_get_buffer2(avctx, frame, 0);
808 typedef struct CompatReleaseBufPriv {
809 AVCodecContext avctx;
811 uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
812 } CompatReleaseBufPriv;
814 static void compat_free_buffer(void *opaque, uint8_t *data)
816 CompatReleaseBufPriv *priv = opaque;
817 if (priv->avctx.release_buffer)
818 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
822 static void compat_release_buffer(void *opaque, uint8_t *data)
824 AVBufferRef *buf = opaque;
825 av_buffer_unref(&buf);
827 FF_ENABLE_DEPRECATION_WARNINGS
830 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
832 AVPacket *pkt = avctx->internal->pkt;
835 AVFrameSideData *frame_sd;
838 frame->reordered_opaque = avctx->reordered_opaque;
840 frame->pkt_pts = AV_NOPTS_VALUE;
844 frame->pkt_pts = pkt->pts;
846 /* copy the replaygain data to the output frame */
847 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
849 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
851 return AVERROR(ENOMEM);
853 memcpy(frame_sd->data, packet_sd, size);
859 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
861 int override_dimensions = 1;
864 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
865 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
866 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
867 return AVERROR(EINVAL);
870 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
871 if (frame->width <= 0 || frame->height <= 0) {
872 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
873 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
874 override_dimensions = 0;
877 ret = ff_decode_frame_props(avctx, frame);
880 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
883 #if FF_API_GET_BUFFER
884 FF_DISABLE_DEPRECATION_WARNINGS
886 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
887 * We wrap each plane in its own AVBuffer. Each of those has a reference to
888 * a dummy AVBuffer as its private data, unreffing it on free.
889 * When all the planes are freed, the dummy buffer's free callback calls
892 if (avctx->get_buffer) {
893 CompatReleaseBufPriv *priv = NULL;
894 AVBufferRef *dummy_buf = NULL;
897 if (flags & AV_GET_BUFFER_FLAG_REF)
898 frame->reference = 1;
900 ret = avctx->get_buffer(avctx, frame);
904 /* return if the buffers are already set up
905 * this would happen e.g. when a custom get_buffer() calls
906 * avcodec_default_get_buffer
911 priv = av_mallocz(sizeof(*priv));
913 ret = AVERROR(ENOMEM);
916 priv->avctx = *avctx;
917 priv->frame = *frame;
919 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
921 ret = AVERROR(ENOMEM);
925 #define WRAP_PLANE(ref_out, data, data_size) \
927 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
929 ret = AVERROR(ENOMEM); \
932 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
935 av_frame_unref(frame); \
936 ret = AVERROR(ENOMEM); \
941 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
942 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
944 planes = av_pix_fmt_count_planes(frame->format);
945 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
946 check for allocated buffers: make libavcodec happy */
947 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
949 if (!desc || planes <= 0) {
950 ret = AVERROR(EINVAL);
954 for (i = 0; i < planes; i++) {
955 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
956 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
958 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
961 int planar = av_sample_fmt_is_planar(frame->format);
962 planes = planar ? avctx->channels : 1;
964 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
965 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
966 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
967 frame->nb_extended_buf);
968 if (!frame->extended_buf) {
969 ret = AVERROR(ENOMEM);
974 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
975 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
977 for (i = 0; i < frame->nb_extended_buf; i++)
978 WRAP_PLANE(frame->extended_buf[i],
979 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
983 av_buffer_unref(&dummy_buf);
986 frame->width = avctx->width;
987 frame->height = avctx->height;
992 avctx->release_buffer(avctx, frame);
994 av_buffer_unref(&dummy_buf);
997 FF_ENABLE_DEPRECATION_WARNINGS
1000 ret = avctx->get_buffer2(avctx, frame, flags);
1002 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
1003 frame->width = avctx->width;
1004 frame->height = avctx->height;
1010 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1012 int ret = get_buffer_internal(avctx, frame, flags);
1014 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1018 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
1023 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1025 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1026 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1027 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1028 av_frame_unref(frame);
1031 ff_init_buffer_info(avctx, frame);
1033 if (!frame->data[0])
1034 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1036 if (av_frame_is_writable(frame))
1037 return ff_decode_frame_props(avctx, frame);
1039 tmp = av_frame_alloc();
1041 return AVERROR(ENOMEM);
1043 av_frame_move_ref(tmp, frame);
1045 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1047 av_frame_free(&tmp);
1051 av_frame_copy(frame, tmp);
1052 av_frame_free(&tmp);
1057 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1059 int ret = reget_buffer_internal(avctx, frame);
1061 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1065 #if FF_API_GET_BUFFER
1066 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
1068 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
1070 av_frame_unref(pic);
1073 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1080 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1084 for (i = 0; i < count; i++) {
1085 int r = func(c, (char *)arg + i * size);
1092 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1096 for (i = 0; i < count; i++) {
1097 int r = func(c, arg, i, 0);
1104 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
1105 unsigned int fourcc)
1107 while (tags->pix_fmt >= 0) {
1108 if (tags->fourcc == fourcc)
1109 return tags->pix_fmt;
1112 return AV_PIX_FMT_NONE;
1115 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1117 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1118 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1121 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1123 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1128 #if FF_API_AVFRAME_LAVC
1129 void avcodec_get_frame_defaults(AVFrame *frame)
1131 #if LIBAVCODEC_VERSION_MAJOR >= 55
1132 // extended_data should explicitly be freed when needed, this code is unsafe currently
1133 // also this is not compatible to the <55 ABI/API
1134 if (frame->extended_data != frame->data && 0)
1135 av_freep(&frame->extended_data);
1138 memset(frame, 0, sizeof(AVFrame));
1139 av_frame_unref(frame);
1142 AVFrame *avcodec_alloc_frame(void)
1144 return av_frame_alloc();
1147 void avcodec_free_frame(AVFrame **frame)
1149 av_frame_free(frame);
1153 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1154 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1155 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1156 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1157 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1159 int av_codec_get_max_lowres(const AVCodec *codec)
1161 return codec->max_lowres;
1164 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1166 memset(sub, 0, sizeof(*sub));
1167 sub->pts = AV_NOPTS_VALUE;
1170 static int get_bit_rate(AVCodecContext *ctx)
1173 int bits_per_sample;
1175 switch (ctx->codec_type) {
1176 case AVMEDIA_TYPE_VIDEO:
1177 case AVMEDIA_TYPE_DATA:
1178 case AVMEDIA_TYPE_SUBTITLE:
1179 case AVMEDIA_TYPE_ATTACHMENT:
1180 bit_rate = ctx->bit_rate;
1182 case AVMEDIA_TYPE_AUDIO:
1183 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1184 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1193 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1197 ff_unlock_avcodec();
1199 ret = avcodec_open2(avctx, codec, options);
1201 ff_lock_avcodec(avctx);
1205 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1208 AVDictionary *tmp = NULL;
1210 if (avcodec_is_open(avctx))
1213 if ((!codec && !avctx->codec)) {
1214 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1215 return AVERROR(EINVAL);
1217 if ((codec && avctx->codec && codec != avctx->codec)) {
1218 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1219 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1220 return AVERROR(EINVAL);
1223 codec = avctx->codec;
1225 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1226 return AVERROR(EINVAL);
1229 av_dict_copy(&tmp, *options, 0);
1231 ret = ff_lock_avcodec(avctx);
1235 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1236 if (!avctx->internal) {
1237 ret = AVERROR(ENOMEM);
1241 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1242 if (!avctx->internal->pool) {
1243 ret = AVERROR(ENOMEM);
1247 avctx->internal->to_free = av_frame_alloc();
1248 if (!avctx->internal->to_free) {
1249 ret = AVERROR(ENOMEM);
1253 if (codec->priv_data_size > 0) {
1254 if (!avctx->priv_data) {
1255 avctx->priv_data = av_mallocz(codec->priv_data_size);
1256 if (!avctx->priv_data) {
1257 ret = AVERROR(ENOMEM);
1260 if (codec->priv_class) {
1261 *(const AVClass **)avctx->priv_data = codec->priv_class;
1262 av_opt_set_defaults(avctx->priv_data);
1265 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1268 avctx->priv_data = NULL;
1270 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1273 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1274 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1275 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1276 if (avctx->coded_width && avctx->coded_height)
1277 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1278 else if (avctx->width && avctx->height)
1279 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1284 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1285 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1286 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1287 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1288 ff_set_dimensions(avctx, 0, 0);
1291 /* if the decoder init function was already called previously,
1292 * free the already allocated subtitle_header before overwriting it */
1293 if (av_codec_is_decoder(codec))
1294 av_freep(&avctx->subtitle_header);
1296 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1297 ret = AVERROR(EINVAL);
1301 avctx->codec = codec;
1302 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1303 avctx->codec_id == AV_CODEC_ID_NONE) {
1304 avctx->codec_type = codec->type;
1305 avctx->codec_id = codec->id;
1307 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1308 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1309 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1310 ret = AVERROR(EINVAL);
1313 avctx->frame_number = 0;
1314 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1316 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1317 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1318 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1320 av_log(avctx, AV_LOG_ERROR,
1321 "The %s '%s' is experimental but experimental codecs are not enabled, "
1322 "add '-strict %d' if you want to use it.\n",
1323 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1324 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1325 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1326 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1327 codec_string, codec2->name);
1328 ret = AVERROR_EXPERIMENTAL;
1332 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1333 (!avctx->time_base.num || !avctx->time_base.den)) {
1334 avctx->time_base.num = 1;
1335 avctx->time_base.den = avctx->sample_rate;
1339 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1341 if (CONFIG_FRAME_THREAD_ENCODER) {
1342 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1343 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1344 ff_lock_avcodec(avctx);
1350 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1351 ret = ff_thread_init(avctx);
1356 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1357 avctx->thread_count = 1;
1359 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1360 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1361 avctx->codec->max_lowres);
1362 ret = AVERROR(EINVAL);
1366 if (av_codec_is_encoder(avctx->codec)) {
1368 if (avctx->codec->sample_fmts) {
1369 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1370 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1372 if (avctx->channels == 1 &&
1373 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1374 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1375 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1379 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1381 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1382 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1383 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1384 ret = AVERROR(EINVAL);
1388 if (avctx->codec->pix_fmts) {
1389 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1390 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1392 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1393 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1394 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1396 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1397 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1398 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1399 ret = AVERROR(EINVAL);
1403 if (avctx->codec->supported_samplerates) {
1404 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1405 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1407 if (avctx->codec->supported_samplerates[i] == 0) {
1408 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1409 avctx->sample_rate);
1410 ret = AVERROR(EINVAL);
1414 if (avctx->codec->channel_layouts) {
1415 if (!avctx->channel_layout) {
1416 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1418 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1419 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1421 if (avctx->codec->channel_layouts[i] == 0) {
1423 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1424 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1425 ret = AVERROR(EINVAL);
1430 if (avctx->channel_layout && avctx->channels) {
1431 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1432 if (channels != avctx->channels) {
1434 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1435 av_log(avctx, AV_LOG_ERROR,
1436 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1437 buf, channels, avctx->channels);
1438 ret = AVERROR(EINVAL);
1441 } else if (avctx->channel_layout) {
1442 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1444 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1445 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1447 if (avctx->width <= 0 || avctx->height <= 0) {
1448 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1449 ret = AVERROR(EINVAL);
1453 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1454 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1455 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1458 if (!avctx->rc_initial_buffer_occupancy)
1459 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1462 avctx->pts_correction_num_faulty_pts =
1463 avctx->pts_correction_num_faulty_dts = 0;
1464 avctx->pts_correction_last_pts =
1465 avctx->pts_correction_last_dts = INT64_MIN;
1467 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1468 || avctx->internal->frame_thread_encoder)) {
1469 ret = avctx->codec->init(avctx);
1477 if (av_codec_is_decoder(avctx->codec)) {
1478 if (!avctx->bit_rate)
1479 avctx->bit_rate = get_bit_rate(avctx);
1480 /* validate channel layout from the decoder */
1481 if (avctx->channel_layout) {
1482 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1483 if (!avctx->channels)
1484 avctx->channels = channels;
1485 else if (channels != avctx->channels) {
1487 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1488 av_log(avctx, AV_LOG_WARNING,
1489 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1490 "ignoring specified channel layout\n",
1491 buf, channels, avctx->channels);
1492 avctx->channel_layout = 0;
1495 if (avctx->channels && avctx->channels < 0 ||
1496 avctx->channels > FF_SANE_NB_CHANNELS) {
1497 ret = AVERROR(EINVAL);
1500 if (avctx->sub_charenc) {
1501 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1502 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1503 "supported with subtitles codecs\n");
1504 ret = AVERROR(EINVAL);
1506 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1507 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1508 "subtitles character encoding will be ignored\n",
1509 avctx->codec_descriptor->name);
1510 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1512 /* input character encoding is set for a text based subtitle
1513 * codec at this point */
1514 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1515 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1517 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1519 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1520 if (cd == (iconv_t)-1) {
1521 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1522 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1523 ret = AVERROR(errno);
1528 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1529 "conversion needs a libavcodec built with iconv support "
1530 "for this codec\n");
1531 ret = AVERROR(ENOSYS);
1539 ff_unlock_avcodec();
1541 av_dict_free(options);
1548 av_freep(&avctx->priv_data);
1549 if (avctx->internal) {
1550 av_frame_free(&avctx->internal->to_free);
1551 av_freep(&avctx->internal->pool);
1553 av_freep(&avctx->internal);
1554 avctx->codec = NULL;
1558 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1560 if (avpkt->size < 0) {
1561 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1562 return AVERROR(EINVAL);
1564 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1565 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1566 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1567 return AVERROR(EINVAL);
1571 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1572 if (!avpkt->data || avpkt->size < size) {
1573 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1574 avpkt->data = avctx->internal->byte_buffer;
1575 avpkt->size = avctx->internal->byte_buffer_size;
1576 avpkt->destruct = NULL;
1581 AVBufferRef *buf = avpkt->buf;
1582 #if FF_API_DESTRUCT_PACKET
1583 FF_DISABLE_DEPRECATION_WARNINGS
1584 void *destruct = avpkt->destruct;
1585 FF_ENABLE_DEPRECATION_WARNINGS
1588 if (avpkt->size < size) {
1589 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1590 return AVERROR(EINVAL);
1593 av_init_packet(avpkt);
1594 #if FF_API_DESTRUCT_PACKET
1595 FF_DISABLE_DEPRECATION_WARNINGS
1596 avpkt->destruct = destruct;
1597 FF_ENABLE_DEPRECATION_WARNINGS
1603 int ret = av_new_packet(avpkt, size);
1605 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1610 int ff_alloc_packet(AVPacket *avpkt, int size)
1612 return ff_alloc_packet2(NULL, avpkt, size);
1616 * Pad last frame with silence.
1618 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1620 AVFrame *frame = NULL;
1623 if (!(frame = av_frame_alloc()))
1624 return AVERROR(ENOMEM);
1626 frame->format = src->format;
1627 frame->channel_layout = src->channel_layout;
1628 av_frame_set_channels(frame, av_frame_get_channels(src));
1629 frame->nb_samples = s->frame_size;
1630 ret = av_frame_get_buffer(frame, 32);
1634 ret = av_frame_copy_props(frame, src);
1638 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1639 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1641 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1642 frame->nb_samples - src->nb_samples,
1643 s->channels, s->sample_fmt)) < 0)
1651 av_frame_free(&frame);
1655 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1657 const AVFrame *frame,
1658 int *got_packet_ptr)
1660 AVFrame *extended_frame = NULL;
1661 AVFrame *padded_frame = NULL;
1663 AVPacket user_pkt = *avpkt;
1664 int needs_realloc = !user_pkt.data;
1666 *got_packet_ptr = 0;
1668 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1669 av_free_packet(avpkt);
1670 av_init_packet(avpkt);
1674 /* ensure that extended_data is properly set */
1675 if (frame && !frame->extended_data) {
1676 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1677 avctx->channels > AV_NUM_DATA_POINTERS) {
1678 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1679 "with more than %d channels, but extended_data is not set.\n",
1680 AV_NUM_DATA_POINTERS);
1681 return AVERROR(EINVAL);
1683 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1685 extended_frame = av_frame_alloc();
1686 if (!extended_frame)
1687 return AVERROR(ENOMEM);
1689 memcpy(extended_frame, frame, sizeof(AVFrame));
1690 extended_frame->extended_data = extended_frame->data;
1691 frame = extended_frame;
1694 /* check for valid frame size */
1696 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1697 if (frame->nb_samples > avctx->frame_size) {
1698 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1699 ret = AVERROR(EINVAL);
1702 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1703 if (frame->nb_samples < avctx->frame_size &&
1704 !avctx->internal->last_audio_frame) {
1705 ret = pad_last_frame(avctx, &padded_frame, frame);
1709 frame = padded_frame;
1710 avctx->internal->last_audio_frame = 1;
1713 if (frame->nb_samples != avctx->frame_size) {
1714 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1715 ret = AVERROR(EINVAL);
1721 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1723 if (*got_packet_ptr) {
1724 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1725 if (avpkt->pts == AV_NOPTS_VALUE)
1726 avpkt->pts = frame->pts;
1727 if (!avpkt->duration)
1728 avpkt->duration = ff_samples_to_time_base(avctx,
1731 avpkt->dts = avpkt->pts;
1736 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1738 if (user_pkt.data) {
1739 if (user_pkt.size >= avpkt->size) {
1740 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1742 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1743 avpkt->size = user_pkt.size;
1746 avpkt->buf = user_pkt.buf;
1747 avpkt->data = user_pkt.data;
1748 avpkt->destruct = user_pkt.destruct;
1750 if (av_dup_packet(avpkt) < 0) {
1751 ret = AVERROR(ENOMEM);
1757 if (needs_realloc && avpkt->data) {
1758 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1760 avpkt->data = avpkt->buf->data;
1763 avctx->frame_number++;
1766 if (ret < 0 || !*got_packet_ptr) {
1767 av_free_packet(avpkt);
1768 av_init_packet(avpkt);
1772 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1773 * this needs to be moved to the encoders, but for now we can do it
1774 * here to simplify things */
1775 avpkt->flags |= AV_PKT_FLAG_KEY;
1778 av_frame_free(&padded_frame);
1779 av_free(extended_frame);
1784 #if FF_API_OLD_ENCODE_AUDIO
1785 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1786 uint8_t *buf, int buf_size,
1787 const short *samples)
1791 int ret, samples_size, got_packet;
1793 av_init_packet(&pkt);
1795 pkt.size = buf_size;
1798 frame = av_frame_alloc();
1800 return AVERROR(ENOMEM);
1802 if (avctx->frame_size) {
1803 frame->nb_samples = avctx->frame_size;
1805 /* if frame_size is not set, the number of samples must be
1806 * calculated from the buffer size */
1808 if (!av_get_bits_per_sample(avctx->codec_id)) {
1809 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1810 "support this codec\n");
1811 av_frame_free(&frame);
1812 return AVERROR(EINVAL);
1814 nb_samples = (int64_t)buf_size * 8 /
1815 (av_get_bits_per_sample(avctx->codec_id) *
1817 if (nb_samples >= INT_MAX) {
1818 av_frame_free(&frame);
1819 return AVERROR(EINVAL);
1821 frame->nb_samples = nb_samples;
1824 /* it is assumed that the samples buffer is large enough based on the
1825 * relevant parameters */
1826 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1828 avctx->sample_fmt, 1);
1829 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1831 (const uint8_t *)samples,
1832 samples_size, 1)) < 0) {
1833 av_frame_free(&frame);
1837 /* fabricate frame pts from sample count.
1838 * this is needed because the avcodec_encode_audio() API does not have
1839 * a way for the user to provide pts */
1840 if (avctx->sample_rate && avctx->time_base.num)
1841 frame->pts = ff_samples_to_time_base(avctx,
1842 avctx->internal->sample_count);
1844 frame->pts = AV_NOPTS_VALUE;
1845 avctx->internal->sample_count += frame->nb_samples;
1851 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1852 if (!ret && got_packet && avctx->coded_frame) {
1853 avctx->coded_frame->pts = pkt.pts;
1854 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1856 /* free any side data since we cannot return it */
1857 av_packet_free_side_data(&pkt);
1859 if (frame && frame->extended_data != frame->data)
1860 av_freep(&frame->extended_data);
1862 av_frame_free(&frame);
1863 return ret ? ret : pkt.size;
1868 #if FF_API_OLD_ENCODE_VIDEO
1869 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1870 const AVFrame *pict)
1873 int ret, got_packet = 0;
1875 if (buf_size < FF_MIN_BUFFER_SIZE) {
1876 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1880 av_init_packet(&pkt);
1882 pkt.size = buf_size;
1884 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1885 if (!ret && got_packet && avctx->coded_frame) {
1886 avctx->coded_frame->pts = pkt.pts;
1887 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1890 /* free any side data since we cannot return it */
1891 if (pkt.side_data_elems > 0) {
1893 for (i = 0; i < pkt.side_data_elems; i++)
1894 av_free(pkt.side_data[i].data);
1895 av_freep(&pkt.side_data);
1896 pkt.side_data_elems = 0;
1899 return ret ? ret : pkt.size;
1904 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1906 const AVFrame *frame,
1907 int *got_packet_ptr)
1910 AVPacket user_pkt = *avpkt;
1911 int needs_realloc = !user_pkt.data;
1913 *got_packet_ptr = 0;
1915 if(CONFIG_FRAME_THREAD_ENCODER &&
1916 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1917 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1919 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1920 avctx->stats_out[0] = '\0';
1922 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1923 av_free_packet(avpkt);
1924 av_init_packet(avpkt);
1929 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1930 return AVERROR(EINVAL);
1932 av_assert0(avctx->codec->encode2);
1934 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1935 av_assert0(ret <= 0);
1937 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1939 if (user_pkt.data) {
1940 if (user_pkt.size >= avpkt->size) {
1941 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1943 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1944 avpkt->size = user_pkt.size;
1947 avpkt->buf = user_pkt.buf;
1948 avpkt->data = user_pkt.data;
1949 avpkt->destruct = user_pkt.destruct;
1951 if (av_dup_packet(avpkt) < 0) {
1952 ret = AVERROR(ENOMEM);
1958 if (!*got_packet_ptr)
1960 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1961 avpkt->pts = avpkt->dts = frame->pts;
1963 if (needs_realloc && avpkt->data) {
1964 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1966 avpkt->data = avpkt->buf->data;
1969 avctx->frame_number++;
1972 if (ret < 0 || !*got_packet_ptr)
1973 av_free_packet(avpkt);
1975 av_packet_merge_side_data(avpkt);
1981 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1982 const AVSubtitle *sub)
1985 if (sub->start_display_time) {
1986 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1990 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1991 avctx->frame_number++;
1996 * Attempt to guess proper monotonic timestamps for decoded video frames
1997 * which might have incorrect times. Input timestamps may wrap around, in
1998 * which case the output will as well.
2000 * @param pts the pts field of the decoded AVPacket, as passed through
2002 * @param dts the dts field of the decoded AVPacket
2003 * @return one of the input values, may be AV_NOPTS_VALUE
2005 static int64_t guess_correct_pts(AVCodecContext *ctx,
2006 int64_t reordered_pts, int64_t dts)
2008 int64_t pts = AV_NOPTS_VALUE;
2010 if (dts != AV_NOPTS_VALUE) {
2011 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
2012 ctx->pts_correction_last_dts = dts;
2013 } else if (reordered_pts != AV_NOPTS_VALUE)
2014 ctx->pts_correction_last_dts = reordered_pts;
2016 if (reordered_pts != AV_NOPTS_VALUE) {
2017 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
2018 ctx->pts_correction_last_pts = reordered_pts;
2019 } else if(dts != AV_NOPTS_VALUE)
2020 ctx->pts_correction_last_pts = dts;
2022 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
2023 && reordered_pts != AV_NOPTS_VALUE)
2024 pts = reordered_pts;
2031 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
2034 const uint8_t *data;
2037 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
2041 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
2042 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
2043 "changes, but PARAM_CHANGE side data was sent to it.\n");
2044 return AVERROR(EINVAL);
2050 flags = bytestream_get_le32(&data);
2053 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
2056 avctx->channels = bytestream_get_le32(&data);
2059 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
2062 avctx->channel_layout = bytestream_get_le64(&data);
2065 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
2068 avctx->sample_rate = bytestream_get_le32(&data);
2071 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
2074 avctx->width = bytestream_get_le32(&data);
2075 avctx->height = bytestream_get_le32(&data);
2077 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2084 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2085 return AVERROR_INVALIDDATA;
2088 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
2091 const uint8_t *side_metadata;
2093 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2095 side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2096 AV_PKT_DATA_STRINGS_METADATA, &size);
2097 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2100 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2104 /* move the original frame to our backup */
2105 av_frame_unref(avci->to_free);
2106 av_frame_move_ref(avci->to_free, frame);
2108 /* now copy everything except the AVBufferRefs back
2109 * note that we make a COPY of the side data, so calling av_frame_free() on
2110 * the caller's frame will work properly */
2111 ret = av_frame_copy_props(frame, avci->to_free);
2115 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2116 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2117 if (avci->to_free->extended_data != avci->to_free->data) {
2118 int planes = av_frame_get_channels(avci->to_free);
2119 int size = planes * sizeof(*frame->extended_data);
2122 av_frame_unref(frame);
2126 frame->extended_data = av_malloc(size);
2127 if (!frame->extended_data) {
2128 av_frame_unref(frame);
2129 return AVERROR(ENOMEM);
2131 memcpy(frame->extended_data, avci->to_free->extended_data,
2134 frame->extended_data = frame->data;
2136 frame->format = avci->to_free->format;
2137 frame->width = avci->to_free->width;
2138 frame->height = avci->to_free->height;
2139 frame->channel_layout = avci->to_free->channel_layout;
2140 frame->nb_samples = avci->to_free->nb_samples;
2141 av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
2146 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2147 int *got_picture_ptr,
2148 const AVPacket *avpkt)
2150 AVCodecInternal *avci = avctx->internal;
2152 // copy to ensure we do not change avpkt
2153 AVPacket tmp = *avpkt;
2156 return AVERROR(EINVAL);
2157 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2158 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2159 return AVERROR(EINVAL);
2162 *got_picture_ptr = 0;
2163 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2164 return AVERROR(EINVAL);
2166 av_frame_unref(picture);
2168 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2169 int did_split = av_packet_split_side_data(&tmp);
2170 ret = apply_param_change(avctx, &tmp);
2172 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2173 if (avctx->err_recognition & AV_EF_EXPLODE)
2177 avctx->internal->pkt = &tmp;
2178 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2179 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2182 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2184 picture->pkt_dts = avpkt->dts;
2186 if(!avctx->has_b_frames){
2187 av_frame_set_pkt_pos(picture, avpkt->pos);
2189 //FIXME these should be under if(!avctx->has_b_frames)
2190 /* get_buffer is supposed to set frame parameters */
2191 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2192 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2193 if (!picture->width) picture->width = avctx->width;
2194 if (!picture->height) picture->height = avctx->height;
2195 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2198 add_metadata_from_side_data(avctx, picture);
2201 emms_c(); //needed to avoid an emms_c() call before every return;
2203 avctx->internal->pkt = NULL;
2205 av_packet_free_side_data(&tmp);
2210 if (*got_picture_ptr) {
2211 if (!avctx->refcounted_frames) {
2212 int err = unrefcount_frame(avci, picture);
2217 avctx->frame_number++;
2218 av_frame_set_best_effort_timestamp(picture,
2219 guess_correct_pts(avctx,
2223 av_frame_unref(picture);
2227 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2228 * make sure it's set correctly */
2229 av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2234 #if FF_API_OLD_DECODE_AUDIO
2235 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2236 int *frame_size_ptr,
2239 AVFrame *frame = av_frame_alloc();
2240 int ret, got_frame = 0;
2243 return AVERROR(ENOMEM);
2244 if (avctx->get_buffer != avcodec_default_get_buffer) {
2245 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2246 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2247 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2248 "avcodec_decode_audio4()\n");
2249 avctx->get_buffer = avcodec_default_get_buffer;
2250 avctx->release_buffer = avcodec_default_release_buffer;
2253 ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2255 if (ret >= 0 && got_frame) {
2257 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2258 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2260 avctx->sample_fmt, 1);
2261 if (*frame_size_ptr < data_size) {
2262 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2263 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2264 av_frame_free(&frame);
2265 return AVERROR(EINVAL);
2268 memcpy(samples, frame->extended_data[0], plane_size);
2270 if (planar && avctx->channels > 1) {
2271 uint8_t *out = ((uint8_t *)samples) + plane_size;
2272 for (ch = 1; ch < avctx->channels; ch++) {
2273 memcpy(out, frame->extended_data[ch], plane_size);
2277 *frame_size_ptr = data_size;
2279 *frame_size_ptr = 0;
2281 av_frame_free(&frame);
2287 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2290 const AVPacket *avpkt)
2292 AVCodecInternal *avci = avctx->internal;
2297 if (!avpkt->data && avpkt->size) {
2298 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2299 return AVERROR(EINVAL);
2302 return AVERROR(EINVAL);
2303 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2304 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2305 return AVERROR(EINVAL);
2308 av_frame_unref(frame);
2310 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2313 uint32_t discard_padding = 0;
2314 // copy to ensure we do not change avpkt
2315 AVPacket tmp = *avpkt;
2316 int did_split = av_packet_split_side_data(&tmp);
2317 ret = apply_param_change(avctx, &tmp);
2319 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2320 if (avctx->err_recognition & AV_EF_EXPLODE)
2324 avctx->internal->pkt = &tmp;
2325 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2326 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2328 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2329 frame->pkt_dts = avpkt->dts;
2331 if (ret >= 0 && *got_frame_ptr) {
2332 add_metadata_from_side_data(avctx, frame);
2333 avctx->frame_number++;
2334 av_frame_set_best_effort_timestamp(frame,
2335 guess_correct_pts(avctx,
2338 if (frame->format == AV_SAMPLE_FMT_NONE)
2339 frame->format = avctx->sample_fmt;
2340 if (!frame->channel_layout)
2341 frame->channel_layout = avctx->channel_layout;
2342 if (!av_frame_get_channels(frame))
2343 av_frame_set_channels(frame, avctx->channels);
2344 if (!frame->sample_rate)
2345 frame->sample_rate = avctx->sample_rate;
2348 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2349 if(side && side_size>=10) {
2350 avctx->internal->skip_samples = AV_RL32(side);
2351 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2352 avctx->internal->skip_samples);
2353 discard_padding = AV_RL32(side + 4);
2355 if (avctx->internal->skip_samples && *got_frame_ptr) {
2356 if(frame->nb_samples <= avctx->internal->skip_samples){
2358 avctx->internal->skip_samples -= frame->nb_samples;
2359 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2360 avctx->internal->skip_samples);
2362 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2363 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2364 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2365 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2366 (AVRational){1, avctx->sample_rate},
2367 avctx->pkt_timebase);
2368 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2369 frame->pkt_pts += diff_ts;
2370 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2371 frame->pkt_dts += diff_ts;
2372 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2373 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2375 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2377 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2378 avctx->internal->skip_samples, frame->nb_samples);
2379 frame->nb_samples -= avctx->internal->skip_samples;
2380 avctx->internal->skip_samples = 0;
2384 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
2385 if (discard_padding == frame->nb_samples) {
2388 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2389 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2390 (AVRational){1, avctx->sample_rate},
2391 avctx->pkt_timebase);
2392 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2393 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2395 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2397 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2398 discard_padding, frame->nb_samples);
2399 frame->nb_samples -= discard_padding;
2403 avctx->internal->pkt = NULL;
2405 av_packet_free_side_data(&tmp);
2410 if (ret >= 0 && *got_frame_ptr) {
2411 if (!avctx->refcounted_frames) {
2412 int err = unrefcount_frame(avci, frame);
2417 av_frame_unref(frame);
2423 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2424 static int recode_subtitle(AVCodecContext *avctx,
2425 AVPacket *outpkt, const AVPacket *inpkt)
2428 iconv_t cd = (iconv_t)-1;
2435 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2439 cd = iconv_open("UTF-8", avctx->sub_charenc);
2440 av_assert0(cd != (iconv_t)-1);
2445 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2446 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2447 ret = AVERROR(ENOMEM);
2451 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2454 outpkt->buf = tmp.buf;
2455 outpkt->data = tmp.data;
2456 outpkt->size = tmp.size;
2457 outb = outpkt->data;
2458 outl = outpkt->size;
2460 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2461 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2462 outl >= outpkt->size || inl != 0) {
2463 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2464 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2465 av_free_packet(&tmp);
2466 ret = AVERROR(errno);
2469 outpkt->size -= outl;
2470 memset(outpkt->data + outpkt->size, 0, outl);
2473 if (cd != (iconv_t)-1)
2477 av_assert0(!"requesting subtitles recoding without iconv");
2481 static int utf8_check(const uint8_t *str)
2483 const uint8_t *byte;
2484 uint32_t codepoint, min;
2488 GET_UTF8(codepoint, *(byte++), return 0;);
2489 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2490 1 << (5 * (byte - str) - 4);
2491 if (codepoint < min || codepoint >= 0x110000 ||
2492 codepoint == 0xFFFE /* BOM */ ||
2493 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2500 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2506 if (!avpkt->data && avpkt->size) {
2507 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2508 return AVERROR(EINVAL);
2511 return AVERROR(EINVAL);
2512 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2513 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2514 return AVERROR(EINVAL);
2518 avcodec_get_subtitle_defaults(sub);
2520 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2521 AVPacket pkt_recoded;
2522 AVPacket tmp = *avpkt;
2523 int did_split = av_packet_split_side_data(&tmp);
2524 //apply_param_change(avctx, &tmp);
2527 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2529 * If the side data is smaller than the buffer padding size, the
2530 * remaining bytes should have already been filled with zeros by the
2531 * original packet allocation anyway. */
2532 memset(tmp.data + tmp.size, 0,
2533 FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2537 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2541 avctx->internal->pkt = &pkt_recoded;
2543 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2544 sub->pts = av_rescale_q(avpkt->pts,
2545 avctx->pkt_timebase, AV_TIME_BASE_Q);
2546 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2547 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2548 !!*got_sub_ptr >= !!sub->num_rects);
2550 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2551 avctx->pkt_timebase.num) {
2552 AVRational ms = { 1, 1000 };
2553 sub->end_display_time = av_rescale_q(avpkt->duration,
2554 avctx->pkt_timebase, ms);
2557 for (i = 0; i < sub->num_rects; i++) {
2558 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2559 av_log(avctx, AV_LOG_ERROR,
2560 "Invalid UTF-8 in decoded subtitles text; "
2561 "maybe missing -sub_charenc option\n");
2562 avsubtitle_free(sub);
2563 return AVERROR_INVALIDDATA;
2567 if (tmp.data != pkt_recoded.data) { // did we recode?
2568 /* prevent from destroying side data from original packet */
2569 pkt_recoded.side_data = NULL;
2570 pkt_recoded.side_data_elems = 0;
2572 av_free_packet(&pkt_recoded);
2574 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2576 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2578 avctx->internal->pkt = NULL;
2582 av_packet_free_side_data(&tmp);
2588 avctx->frame_number++;
2594 void avsubtitle_free(AVSubtitle *sub)
2598 for (i = 0; i < sub->num_rects; i++) {
2599 av_freep(&sub->rects[i]->pict.data[0]);
2600 av_freep(&sub->rects[i]->pict.data[1]);
2601 av_freep(&sub->rects[i]->pict.data[2]);
2602 av_freep(&sub->rects[i]->pict.data[3]);
2603 av_freep(&sub->rects[i]->text);
2604 av_freep(&sub->rects[i]->ass);
2605 av_freep(&sub->rects[i]);
2608 av_freep(&sub->rects);
2610 memset(sub, 0, sizeof(AVSubtitle));
2613 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2617 ff_unlock_avcodec();
2619 ret = avcodec_close(avctx);
2621 ff_lock_avcodec(NULL);
2625 av_cold int avcodec_close(AVCodecContext *avctx)
2632 ret = ff_lock_avcodec(avctx);
2636 if (avcodec_is_open(avctx)) {
2637 FramePool *pool = avctx->internal->pool;
2639 if (CONFIG_FRAME_THREAD_ENCODER &&
2640 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2641 ff_unlock_avcodec();
2642 ff_frame_thread_encoder_free(avctx);
2643 ff_lock_avcodec(avctx);
2645 if (HAVE_THREADS && avctx->internal->thread_ctx)
2646 ff_thread_free(avctx);
2647 if (avctx->codec && avctx->codec->close)
2648 avctx->codec->close(avctx);
2649 avctx->coded_frame = NULL;
2650 avctx->internal->byte_buffer_size = 0;
2651 av_freep(&avctx->internal->byte_buffer);
2652 av_frame_free(&avctx->internal->to_free);
2653 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2654 av_buffer_pool_uninit(&pool->pools[i]);
2655 av_freep(&avctx->internal->pool);
2656 av_freep(&avctx->internal);
2659 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2660 av_opt_free(avctx->priv_data);
2662 av_freep(&avctx->priv_data);
2663 if (av_codec_is_encoder(avctx->codec))
2664 av_freep(&avctx->extradata);
2665 avctx->codec = NULL;
2666 avctx->active_thread_type = 0;
2668 ff_unlock_avcodec();
2672 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2675 //This is for future deprecatec codec ids, its empty since
2676 //last major bump but will fill up again over time, please don't remove it
2677 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2678 case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
2679 case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
2680 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2681 case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
2682 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2683 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2684 case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
2685 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2686 case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
2687 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2688 case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
2689 case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
2690 case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
2691 case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
2692 case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
2693 default : return id;
2697 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2699 AVCodec *p, *experimental = NULL;
2701 id= remap_deprecated_codec_id(id);
2703 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2705 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2712 return experimental;
2715 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2717 return find_encdec(id, 1);
2720 AVCodec *avcodec_find_encoder_by_name(const char *name)
2727 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2734 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2736 return find_encdec(id, 0);
2739 AVCodec *avcodec_find_decoder_by_name(const char *name)
2746 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2753 const char *avcodec_get_name(enum AVCodecID id)
2755 const AVCodecDescriptor *cd;
2758 if (id == AV_CODEC_ID_NONE)
2760 cd = avcodec_descriptor_get(id);
2763 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2764 codec = avcodec_find_decoder(id);
2767 codec = avcodec_find_encoder(id);
2770 return "unknown_codec";
2773 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2775 int i, len, ret = 0;
2777 #define TAG_PRINT(x) \
2778 (((x) >= '0' && (x) <= '9') || \
2779 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2780 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2782 for (i = 0; i < 4; i++) {
2783 len = snprintf(buf, buf_size,
2784 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2786 buf_size = buf_size > len ? buf_size - len : 0;
2793 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2795 const char *codec_type;
2796 const char *codec_name;
2797 const char *profile = NULL;
2800 AVRational display_aspect_ratio;
2802 if (!buf || buf_size <= 0)
2804 codec_type = av_get_media_type_string(enc->codec_type);
2805 codec_name = avcodec_get_name(enc->codec_id);
2806 if (enc->profile != FF_PROFILE_UNKNOWN) {
2810 p = encode ? avcodec_find_encoder(enc->codec_id) :
2811 avcodec_find_decoder(enc->codec_id);
2813 profile = av_get_profile_name(p, enc->profile);
2816 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2818 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2820 if (enc->codec && strcmp(enc->codec->name, codec_name))
2821 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2824 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2825 if (enc->codec_tag) {
2827 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2828 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2829 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2832 switch (enc->codec_type) {
2833 case AVMEDIA_TYPE_VIDEO:
2834 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2835 char detail[256] = "(";
2836 const char *colorspace_name;
2837 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2839 av_get_pix_fmt_name(enc->pix_fmt));
2840 if (enc->bits_per_raw_sample &&
2841 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2842 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2843 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2844 av_strlcatf(detail, sizeof(detail),
2845 enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
2847 colorspace_name = av_get_colorspace_name(enc->colorspace);
2848 if (colorspace_name)
2849 av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
2851 if (strlen(detail) > 1) {
2852 detail[strlen(detail) - 2] = 0;
2853 av_strlcatf(buf, buf_size, "%s)", detail);
2857 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2859 enc->width, enc->height);
2860 if (enc->sample_aspect_ratio.num) {
2861 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2862 enc->width * enc->sample_aspect_ratio.num,
2863 enc->height * enc->sample_aspect_ratio.den,
2865 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2866 " [SAR %d:%d DAR %d:%d]",
2867 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2868 display_aspect_ratio.num, display_aspect_ratio.den);
2870 if (av_log_get_level() >= AV_LOG_DEBUG) {
2871 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2872 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2874 enc->time_base.num / g, enc->time_base.den / g);
2878 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2879 ", q=%d-%d", enc->qmin, enc->qmax);
2882 case AVMEDIA_TYPE_AUDIO:
2883 if (enc->sample_rate) {
2884 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2885 ", %d Hz", enc->sample_rate);
2887 av_strlcat(buf, ", ", buf_size);
2888 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2889 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2890 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2891 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2894 case AVMEDIA_TYPE_DATA:
2895 if (av_log_get_level() >= AV_LOG_DEBUG) {
2896 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2898 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2900 enc->time_base.num / g, enc->time_base.den / g);
2903 case AVMEDIA_TYPE_SUBTITLE:
2905 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2906 ", %dx%d", enc->width, enc->height);
2912 if (enc->flags & CODEC_FLAG_PASS1)
2913 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2915 if (enc->flags & CODEC_FLAG_PASS2)
2916 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2919 bitrate = get_bit_rate(enc);
2921 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2922 ", %d kb/s", bitrate / 1000);
2923 } else if (enc->rc_max_rate > 0) {
2924 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2925 ", max. %d kb/s", enc->rc_max_rate / 1000);
2929 const char *av_get_profile_name(const AVCodec *codec, int profile)
2932 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2935 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2936 if (p->profile == profile)
2942 unsigned avcodec_version(void)
2944 // av_assert0(AV_CODEC_ID_V410==164);
2945 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2946 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2947 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2948 av_assert0(AV_CODEC_ID_SRT==94216);
2949 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2951 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2952 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2953 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2954 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2955 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2956 return LIBAVCODEC_VERSION_INT;
2959 const char *avcodec_configuration(void)
2961 return FFMPEG_CONFIGURATION;
2964 const char *avcodec_license(void)
2966 #define LICENSE_PREFIX "libavcodec license: "
2967 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2970 void avcodec_flush_buffers(AVCodecContext *avctx)
2972 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2973 ff_thread_flush(avctx);
2974 else if (avctx->codec->flush)
2975 avctx->codec->flush(avctx);
2977 avctx->pts_correction_last_pts =
2978 avctx->pts_correction_last_dts = INT64_MIN;
2980 if (!avctx->refcounted_frames)
2981 av_frame_unref(avctx->internal->to_free);
2984 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2987 case AV_CODEC_ID_8SVX_EXP:
2988 case AV_CODEC_ID_8SVX_FIB:
2989 case AV_CODEC_ID_ADPCM_CT:
2990 case AV_CODEC_ID_ADPCM_IMA_APC:
2991 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2992 case AV_CODEC_ID_ADPCM_IMA_OKI:
2993 case AV_CODEC_ID_ADPCM_IMA_WS:
2994 case AV_CODEC_ID_ADPCM_G722:
2995 case AV_CODEC_ID_ADPCM_YAMAHA:
2997 case AV_CODEC_ID_DSD_LSBF:
2998 case AV_CODEC_ID_DSD_MSBF:
2999 case AV_CODEC_ID_DSD_LSBF_PLANAR:
3000 case AV_CODEC_ID_DSD_MSBF_PLANAR:
3001 case AV_CODEC_ID_PCM_ALAW:
3002 case AV_CODEC_ID_PCM_MULAW:
3003 case AV_CODEC_ID_PCM_S8:
3004 case AV_CODEC_ID_PCM_S8_PLANAR:
3005 case AV_CODEC_ID_PCM_U8:
3006 case AV_CODEC_ID_PCM_ZORK:
3008 case AV_CODEC_ID_PCM_S16BE:
3009 case AV_CODEC_ID_PCM_S16BE_PLANAR:
3010 case AV_CODEC_ID_PCM_S16LE:
3011 case AV_CODEC_ID_PCM_S16LE_PLANAR:
3012 case AV_CODEC_ID_PCM_U16BE:
3013 case AV_CODEC_ID_PCM_U16LE:
3015 case AV_CODEC_ID_PCM_S24DAUD:
3016 case AV_CODEC_ID_PCM_S24BE:
3017 case AV_CODEC_ID_PCM_S24LE:
3018 case AV_CODEC_ID_PCM_S24LE_PLANAR:
3019 case AV_CODEC_ID_PCM_U24BE:
3020 case AV_CODEC_ID_PCM_U24LE:
3022 case AV_CODEC_ID_PCM_S32BE:
3023 case AV_CODEC_ID_PCM_S32LE:
3024 case AV_CODEC_ID_PCM_S32LE_PLANAR:
3025 case AV_CODEC_ID_PCM_U32BE:
3026 case AV_CODEC_ID_PCM_U32LE:
3027 case AV_CODEC_ID_PCM_F32BE:
3028 case AV_CODEC_ID_PCM_F32LE:
3030 case AV_CODEC_ID_PCM_F64BE:
3031 case AV_CODEC_ID_PCM_F64LE:
3038 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
3040 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
3041 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3042 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3043 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3044 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3045 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3046 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3047 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3048 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3049 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3050 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3052 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3053 return AV_CODEC_ID_NONE;
3054 if (be < 0 || be > 1)
3056 return map[fmt][be];
3059 int av_get_bits_per_sample(enum AVCodecID codec_id)
3062 case AV_CODEC_ID_ADPCM_SBPRO_2:
3064 case AV_CODEC_ID_ADPCM_SBPRO_3:
3066 case AV_CODEC_ID_ADPCM_SBPRO_4:
3067 case AV_CODEC_ID_ADPCM_IMA_WAV:
3068 case AV_CODEC_ID_ADPCM_IMA_QT:
3069 case AV_CODEC_ID_ADPCM_SWF:
3070 case AV_CODEC_ID_ADPCM_MS:
3073 return av_get_exact_bits_per_sample(codec_id);
3077 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3079 int id, sr, ch, ba, tag, bps;
3081 id = avctx->codec_id;
3082 sr = avctx->sample_rate;
3083 ch = avctx->channels;
3084 ba = avctx->block_align;
3085 tag = avctx->codec_tag;
3086 bps = av_get_exact_bits_per_sample(avctx->codec_id);
3088 /* codecs with an exact constant bits per sample */
3089 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3090 return (frame_bytes * 8LL) / (bps * ch);
3091 bps = avctx->bits_per_coded_sample;
3093 /* codecs with a fixed packet duration */
3095 case AV_CODEC_ID_ADPCM_ADX: return 32;
3096 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3097 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3098 case AV_CODEC_ID_AMR_NB:
3099 case AV_CODEC_ID_EVRC:
3100 case AV_CODEC_ID_GSM:
3101 case AV_CODEC_ID_QCELP:
3102 case AV_CODEC_ID_RA_288: return 160;
3103 case AV_CODEC_ID_AMR_WB:
3104 case AV_CODEC_ID_GSM_MS: return 320;
3105 case AV_CODEC_ID_MP1: return 384;
3106 case AV_CODEC_ID_ATRAC1: return 512;
3107 case AV_CODEC_ID_ATRAC3: return 1024;
3108 case AV_CODEC_ID_MP2:
3109 case AV_CODEC_ID_MUSEPACK7: return 1152;
3110 case AV_CODEC_ID_AC3: return 1536;
3114 /* calc from sample rate */
3115 if (id == AV_CODEC_ID_TTA)
3116 return 256 * sr / 245;
3119 /* calc from sample rate and channels */
3120 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3121 return (480 << (sr / 22050)) / ch;
3126 /* calc from block_align */
3127 if (id == AV_CODEC_ID_SIPR) {
3129 case 20: return 160;
3130 case 19: return 144;
3131 case 29: return 288;
3132 case 37: return 480;
3134 } else if (id == AV_CODEC_ID_ILBC) {
3136 case 38: return 160;
3137 case 50: return 240;
3142 if (frame_bytes > 0) {
3143 /* calc from frame_bytes only */
3144 if (id == AV_CODEC_ID_TRUESPEECH)
3145 return 240 * (frame_bytes / 32);
3146 if (id == AV_CODEC_ID_NELLYMOSER)
3147 return 256 * (frame_bytes / 64);
3148 if (id == AV_CODEC_ID_RA_144)
3149 return 160 * (frame_bytes / 20);
3150 if (id == AV_CODEC_ID_G723_1)
3151 return 240 * (frame_bytes / 24);
3154 /* calc from frame_bytes and bits_per_coded_sample */
3155 if (id == AV_CODEC_ID_ADPCM_G726)
3156 return frame_bytes * 8 / bps;
3160 /* calc from frame_bytes and channels */
3162 case AV_CODEC_ID_ADPCM_AFC:
3163 return frame_bytes / (9 * ch) * 16;
3164 case AV_CODEC_ID_ADPCM_DTK:
3165 return frame_bytes / (16 * ch) * 28;
3166 case AV_CODEC_ID_ADPCM_4XM:
3167 case AV_CODEC_ID_ADPCM_IMA_ISS:
3168 return (frame_bytes - 4 * ch) * 2 / ch;
3169 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3170 return (frame_bytes - 4) * 2 / ch;
3171 case AV_CODEC_ID_ADPCM_IMA_AMV:
3172 return (frame_bytes - 8) * 2 / ch;
3173 case AV_CODEC_ID_ADPCM_XA:
3174 return (frame_bytes / 128) * 224 / ch;
3175 case AV_CODEC_ID_INTERPLAY_DPCM:
3176 return (frame_bytes - 6 - ch) / ch;
3177 case AV_CODEC_ID_ROQ_DPCM:
3178 return (frame_bytes - 8) / ch;
3179 case AV_CODEC_ID_XAN_DPCM:
3180 return (frame_bytes - 2 * ch) / ch;
3181 case AV_CODEC_ID_MACE3:
3182 return 3 * frame_bytes / ch;
3183 case AV_CODEC_ID_MACE6:
3184 return 6 * frame_bytes / ch;
3185 case AV_CODEC_ID_PCM_LXF:
3186 return 2 * (frame_bytes / (5 * ch));
3187 case AV_CODEC_ID_IAC:
3188 case AV_CODEC_ID_IMC:
3189 return 4 * frame_bytes / ch;
3193 /* calc from frame_bytes, channels, and codec_tag */
3194 if (id == AV_CODEC_ID_SOL_DPCM) {
3196 return frame_bytes / ch;
3198 return frame_bytes * 2 / ch;
3203 /* calc from frame_bytes, channels, and block_align */
3204 int blocks = frame_bytes / ba;
3205 switch (avctx->codec_id) {
3206 case AV_CODEC_ID_ADPCM_IMA_WAV:
3207 if (bps < 2 || bps > 5)
3209 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3210 case AV_CODEC_ID_ADPCM_IMA_DK3:
3211 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3212 case AV_CODEC_ID_ADPCM_IMA_DK4:
3213 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3214 case AV_CODEC_ID_ADPCM_IMA_RAD:
3215 return blocks * ((ba - 4 * ch) * 2 / ch);
3216 case AV_CODEC_ID_ADPCM_MS:
3217 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3222 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3223 switch (avctx->codec_id) {
3224 case AV_CODEC_ID_PCM_DVD:
3227 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3228 case AV_CODEC_ID_PCM_BLURAY:
3231 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3232 case AV_CODEC_ID_S302M:
3233 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3243 int ff_thread_init(AVCodecContext *s)
3250 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3264 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3267 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3271 #if FF_API_MISSING_SAMPLE
3272 FF_DISABLE_DEPRECATION_WARNINGS
3273 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3275 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3276 "version to the newest one from Git. If the problem still "
3277 "occurs, it means that your file has a feature which has not "
3278 "been implemented.\n", feature);
3280 av_log_ask_for_sample(avc, NULL);
3283 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3285 va_list argument_list;
3287 va_start(argument_list, msg);
3290 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3291 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3292 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
3293 "and contact the ffmpeg-devel mailing list.\n");
3295 va_end(argument_list);
3297 FF_ENABLE_DEPRECATION_WARNINGS
3298 #endif /* FF_API_MISSING_SAMPLE */
3300 static AVHWAccel *first_hwaccel = NULL;
3301 static AVHWAccel **last_hwaccel = &first_hwaccel;
3303 void av_register_hwaccel(AVHWAccel *hwaccel)
3305 AVHWAccel **p = last_hwaccel;
3306 hwaccel->next = NULL;
3307 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3309 last_hwaccel = &hwaccel->next;
3312 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
3314 return hwaccel ? hwaccel->next : first_hwaccel;
3317 AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
3319 enum AVCodecID codec_id = avctx->codec->id;
3320 enum AVPixelFormat pix_fmt = avctx->pix_fmt;
3322 AVHWAccel *hwaccel = NULL;
3324 while ((hwaccel = av_hwaccel_next(hwaccel)))
3325 if (hwaccel->id == codec_id
3326 && hwaccel->pix_fmt == pix_fmt)
3331 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3334 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3336 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3343 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3345 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3351 int ff_lock_avcodec(AVCodecContext *log_ctx)
3354 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3357 entangled_thread_counter++;
3358 if (entangled_thread_counter != 1) {
3359 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3361 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3362 ff_avcodec_locked = 1;
3363 ff_unlock_avcodec();
3364 return AVERROR(EINVAL);
3366 av_assert0(!ff_avcodec_locked);
3367 ff_avcodec_locked = 1;
3371 int ff_unlock_avcodec(void)
3373 av_assert0(ff_avcodec_locked);
3374 ff_avcodec_locked = 0;
3375 entangled_thread_counter--;
3377 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3383 int avpriv_lock_avformat(void)
3386 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3392 int avpriv_unlock_avformat(void)
3395 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3401 unsigned int avpriv_toupper4(unsigned int x)
3403 return av_toupper(x & 0xFF) +
3404 (av_toupper((x >> 8) & 0xFF) << 8) +
3405 (av_toupper((x >> 16) & 0xFF) << 16) +
3406 (av_toupper((x >> 24) & 0xFF) << 24);
3409 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3413 dst->owner = src->owner;
3415 ret = av_frame_ref(dst->f, src->f);
3419 if (src->progress &&
3420 !(dst->progress = av_buffer_ref(src->progress))) {
3421 ff_thread_release_buffer(dst->owner, dst);
3422 return AVERROR(ENOMEM);
3430 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3432 return avctx->get_format(avctx, fmt);
3435 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3438 return ff_get_buffer(avctx, f->f, flags);
3441 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3444 av_frame_unref(f->f);
3447 void ff_thread_finish_setup(AVCodecContext *avctx)
3451 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3455 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3459 int ff_thread_can_start_frame(AVCodecContext *avctx)
3464 int ff_alloc_entries(AVCodecContext *avctx, int count)
3469 void ff_reset_entries(AVCodecContext *avctx)
3473 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3477 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3483 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3485 AVCodec *c= avcodec_find_decoder(codec_id);
3487 c= avcodec_find_encoder(codec_id);
3491 if (codec_id <= AV_CODEC_ID_NONE)
3492 return AVMEDIA_TYPE_UNKNOWN;
3493 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3494 return AVMEDIA_TYPE_VIDEO;
3495 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3496 return AVMEDIA_TYPE_AUDIO;
3497 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3498 return AVMEDIA_TYPE_SUBTITLE;
3500 return AVMEDIA_TYPE_UNKNOWN;
3503 int avcodec_is_open(AVCodecContext *s)
3505 return !!s->internal;
3508 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3513 ret = av_bprint_finalize(buf, &str);
3516 avctx->extradata = str;
3517 /* Note: the string is NUL terminated (so extradata can be read as a
3518 * string), but the ending character is not accounted in the size (in
3519 * binary formats you are likely not supposed to mux that character). When
3520 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3522 avctx->extradata_size = buf->len;
3526 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3528 uint32_t *av_restrict state)
3532 av_assert0(p <= end);
3536 for (i = 0; i < 3; i++) {
3537 uint32_t tmp = *state << 8;
3538 *state = tmp + *(p++);
3539 if (tmp == 0x100 || p == end)
3544 if (p[-1] > 1 ) p += 3;
3545 else if (p[-2] ) p += 2;
3546 else if (p[-3]|(p[-1]-1)) p++;
3553 p = FFMIN(p, end) - 4;
3554 *state = AV_RB32(p);