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_YVYU422:
289 case AV_PIX_FMT_UYVY422:
290 case AV_PIX_FMT_YUV422P:
291 case AV_PIX_FMT_YUV440P:
292 case AV_PIX_FMT_YUV444P:
293 case AV_PIX_FMT_GBRAP:
294 case AV_PIX_FMT_GBRP:
295 case AV_PIX_FMT_GRAY8:
296 case AV_PIX_FMT_GRAY16BE:
297 case AV_PIX_FMT_GRAY16LE:
298 case AV_PIX_FMT_YUVJ420P:
299 case AV_PIX_FMT_YUVJ422P:
300 case AV_PIX_FMT_YUVJ440P:
301 case AV_PIX_FMT_YUVJ444P:
302 case AV_PIX_FMT_YUVA420P:
303 case AV_PIX_FMT_YUVA422P:
304 case AV_PIX_FMT_YUVA444P:
305 case AV_PIX_FMT_YUV420P9LE:
306 case AV_PIX_FMT_YUV420P9BE:
307 case AV_PIX_FMT_YUV420P10LE:
308 case AV_PIX_FMT_YUV420P10BE:
309 case AV_PIX_FMT_YUV420P12LE:
310 case AV_PIX_FMT_YUV420P12BE:
311 case AV_PIX_FMT_YUV420P14LE:
312 case AV_PIX_FMT_YUV420P14BE:
313 case AV_PIX_FMT_YUV420P16LE:
314 case AV_PIX_FMT_YUV420P16BE:
315 case AV_PIX_FMT_YUVA420P9LE:
316 case AV_PIX_FMT_YUVA420P9BE:
317 case AV_PIX_FMT_YUVA420P10LE:
318 case AV_PIX_FMT_YUVA420P10BE:
319 case AV_PIX_FMT_YUVA420P16LE:
320 case AV_PIX_FMT_YUVA420P16BE:
321 case AV_PIX_FMT_YUV422P9LE:
322 case AV_PIX_FMT_YUV422P9BE:
323 case AV_PIX_FMT_YUV422P10LE:
324 case AV_PIX_FMT_YUV422P10BE:
325 case AV_PIX_FMT_YUV422P12LE:
326 case AV_PIX_FMT_YUV422P12BE:
327 case AV_PIX_FMT_YUV422P14LE:
328 case AV_PIX_FMT_YUV422P14BE:
329 case AV_PIX_FMT_YUV422P16LE:
330 case AV_PIX_FMT_YUV422P16BE:
331 case AV_PIX_FMT_YUVA422P9LE:
332 case AV_PIX_FMT_YUVA422P9BE:
333 case AV_PIX_FMT_YUVA422P10LE:
334 case AV_PIX_FMT_YUVA422P10BE:
335 case AV_PIX_FMT_YUVA422P16LE:
336 case AV_PIX_FMT_YUVA422P16BE:
337 case AV_PIX_FMT_YUV444P9LE:
338 case AV_PIX_FMT_YUV444P9BE:
339 case AV_PIX_FMT_YUV444P10LE:
340 case AV_PIX_FMT_YUV444P10BE:
341 case AV_PIX_FMT_YUV444P12LE:
342 case AV_PIX_FMT_YUV444P12BE:
343 case AV_PIX_FMT_YUV444P14LE:
344 case AV_PIX_FMT_YUV444P14BE:
345 case AV_PIX_FMT_YUV444P16LE:
346 case AV_PIX_FMT_YUV444P16BE:
347 case AV_PIX_FMT_YUVA444P9LE:
348 case AV_PIX_FMT_YUVA444P9BE:
349 case AV_PIX_FMT_YUVA444P10LE:
350 case AV_PIX_FMT_YUVA444P10BE:
351 case AV_PIX_FMT_YUVA444P16LE:
352 case AV_PIX_FMT_YUVA444P16BE:
353 case AV_PIX_FMT_GBRP9LE:
354 case AV_PIX_FMT_GBRP9BE:
355 case AV_PIX_FMT_GBRP10LE:
356 case AV_PIX_FMT_GBRP10BE:
357 case AV_PIX_FMT_GBRP12LE:
358 case AV_PIX_FMT_GBRP12BE:
359 case AV_PIX_FMT_GBRP14LE:
360 case AV_PIX_FMT_GBRP14BE:
361 w_align = 16; //FIXME assume 16 pixel per macroblock
362 h_align = 16 * 2; // interlaced needs 2 macroblocks height
364 case AV_PIX_FMT_YUV411P:
365 case AV_PIX_FMT_YUVJ411P:
366 case AV_PIX_FMT_UYYVYY411:
370 case AV_PIX_FMT_YUV410P:
371 if (s->codec_id == AV_CODEC_ID_SVQ1) {
376 case AV_PIX_FMT_RGB555:
377 if (s->codec_id == AV_CODEC_ID_RPZA) {
382 case AV_PIX_FMT_PAL8:
383 case AV_PIX_FMT_BGR8:
384 case AV_PIX_FMT_RGB8:
385 if (s->codec_id == AV_CODEC_ID_SMC ||
386 s->codec_id == AV_CODEC_ID_CINEPAK) {
391 case AV_PIX_FMT_BGR24:
392 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
393 (s->codec_id == AV_CODEC_ID_ZLIB)) {
398 case AV_PIX_FMT_RGB24:
399 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
410 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
411 w_align = FFMAX(w_align, 8);
414 *width = FFALIGN(*width, w_align);
415 *height = FFALIGN(*height, h_align);
416 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
417 // some of the optimized chroma MC reads one line too much
418 // which is also done in mpeg decoders with lowres > 0
421 for (i = 0; i < 4; i++)
422 linesize_align[i] = STRIDE_ALIGN;
425 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
427 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
428 int chroma_shift = desc->log2_chroma_w;
429 int linesize_align[AV_NUM_DATA_POINTERS];
432 avcodec_align_dimensions2(s, width, height, linesize_align);
433 align = FFMAX(linesize_align[0], linesize_align[3]);
434 linesize_align[1] <<= chroma_shift;
435 linesize_align[2] <<= chroma_shift;
436 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
437 *width = FFALIGN(*width, align);
440 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
442 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
443 return AVERROR(EINVAL);
446 *xpos = (pos&1) * 128;
447 *ypos = ((pos>>1)^(pos<4)) * 128;
452 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
456 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
457 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
460 return AVCHROMA_LOC_UNSPECIFIED;
463 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
464 enum AVSampleFormat sample_fmt, const uint8_t *buf,
465 int buf_size, int align)
467 int ch, planar, needed_size, ret = 0;
469 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
470 frame->nb_samples, sample_fmt,
472 if (buf_size < needed_size)
473 return AVERROR(EINVAL);
475 planar = av_sample_fmt_is_planar(sample_fmt);
476 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
477 if (!(frame->extended_data = av_mallocz_array(nb_channels,
478 sizeof(*frame->extended_data))))
479 return AVERROR(ENOMEM);
481 frame->extended_data = frame->data;
484 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
485 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
486 sample_fmt, align)) < 0) {
487 if (frame->extended_data != frame->data)
488 av_freep(&frame->extended_data);
491 if (frame->extended_data != frame->data) {
492 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
493 frame->data[ch] = frame->extended_data[ch];
499 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
501 FramePool *pool = avctx->internal->pool;
504 switch (avctx->codec_type) {
505 case AVMEDIA_TYPE_VIDEO: {
508 int w = frame->width;
509 int h = frame->height;
510 int tmpsize, unaligned;
512 if (pool->format == frame->format &&
513 pool->width == frame->width && pool->height == frame->height)
516 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
518 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
524 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
525 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
526 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
527 // increase alignment of w for next try (rhs gives the lowest bit set in w)
531 for (i = 0; i < 4; i++)
532 unaligned |= picture.linesize[i] % pool->stride_align[i];
535 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
536 NULL, picture.linesize);
540 for (i = 0; i < 3 && picture.data[i + 1]; i++)
541 size[i] = picture.data[i + 1] - picture.data[i];
542 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
544 for (i = 0; i < 4; i++) {
545 av_buffer_pool_uninit(&pool->pools[i]);
546 pool->linesize[i] = picture.linesize[i];
548 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
549 CONFIG_MEMORY_POISONING ?
552 if (!pool->pools[i]) {
553 ret = AVERROR(ENOMEM);
558 pool->format = frame->format;
559 pool->width = frame->width;
560 pool->height = frame->height;
564 case AVMEDIA_TYPE_AUDIO: {
565 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
566 int planar = av_sample_fmt_is_planar(frame->format);
567 int planes = planar ? ch : 1;
569 if (pool->format == frame->format && pool->planes == planes &&
570 pool->channels == ch && frame->nb_samples == pool->samples)
573 av_buffer_pool_uninit(&pool->pools[0]);
574 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
575 frame->nb_samples, frame->format, 0);
579 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
580 if (!pool->pools[0]) {
581 ret = AVERROR(ENOMEM);
585 pool->format = frame->format;
586 pool->planes = planes;
588 pool->samples = frame->nb_samples;
591 default: av_assert0(0);
595 for (i = 0; i < 4; i++)
596 av_buffer_pool_uninit(&pool->pools[i]);
598 pool->planes = pool->channels = pool->samples = 0;
599 pool->width = pool->height = 0;
603 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
605 FramePool *pool = avctx->internal->pool;
606 int planes = pool->planes;
609 frame->linesize[0] = pool->linesize[0];
611 if (planes > AV_NUM_DATA_POINTERS) {
612 frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
613 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
614 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
615 sizeof(*frame->extended_buf));
616 if (!frame->extended_data || !frame->extended_buf) {
617 av_freep(&frame->extended_data);
618 av_freep(&frame->extended_buf);
619 return AVERROR(ENOMEM);
622 frame->extended_data = frame->data;
623 av_assert0(frame->nb_extended_buf == 0);
626 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
627 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
630 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
632 for (i = 0; i < frame->nb_extended_buf; i++) {
633 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
634 if (!frame->extended_buf[i])
636 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
639 if (avctx->debug & FF_DEBUG_BUFFERS)
640 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
644 av_frame_unref(frame);
645 return AVERROR(ENOMEM);
648 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
650 FramePool *pool = s->internal->pool;
651 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
652 int pixel_size = desc->comp[0].step_minus1 + 1;
653 int h_chroma_shift, v_chroma_shift;
656 if (pic->data[0] != NULL) {
657 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
661 memset(pic->data, 0, sizeof(pic->data));
662 pic->extended_data = pic->data;
664 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
666 for (i = 0; i < 4 && pool->pools[i]; i++) {
667 const int h_shift = i == 0 ? 0 : h_chroma_shift;
668 const int v_shift = i == 0 ? 0 : v_chroma_shift;
669 int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
671 pic->linesize[i] = pool->linesize[i];
673 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
677 // no edge if EDGE EMU or not planar YUV
678 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
679 pic->data[i] = pic->buf[i]->data;
681 pic->data[i] = pic->buf[i]->data +
682 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
683 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
686 for (; i < AV_NUM_DATA_POINTERS; i++) {
688 pic->linesize[i] = 0;
690 if (pic->data[1] && !pic->data[2])
691 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
693 if (s->debug & FF_DEBUG_BUFFERS)
694 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
699 return AVERROR(ENOMEM);
702 void avpriv_color_frame(AVFrame *frame, const int c[4])
704 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
707 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
709 for (p = 0; p<desc->nb_components; p++) {
710 uint8_t *dst = frame->data[p];
711 int is_chroma = p == 1 || p == 2;
712 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
713 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
714 for (y = 0; y < height; y++) {
715 if (desc->comp[0].depth_minus1 >= 8) {
716 for (x = 0; x<bytes; x++)
717 ((uint16_t*)dst)[x] = c[p];
719 memset(dst, c[p], bytes);
720 dst += frame->linesize[p];
725 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
729 if ((ret = update_frame_pool(avctx, frame)) < 0)
732 #if FF_API_GET_BUFFER
733 FF_DISABLE_DEPRECATION_WARNINGS
734 frame->type = FF_BUFFER_TYPE_INTERNAL;
735 FF_ENABLE_DEPRECATION_WARNINGS
738 switch (avctx->codec_type) {
739 case AVMEDIA_TYPE_VIDEO:
740 return video_get_buffer(avctx, frame);
741 case AVMEDIA_TYPE_AUDIO:
742 return audio_get_buffer(avctx, frame);
748 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
750 AVPacket *pkt = avctx->internal->pkt;
754 AVFrameSideData *frame_sd;
756 frame->pkt_pts = pkt->pts;
757 av_frame_set_pkt_pos (frame, pkt->pos);
758 av_frame_set_pkt_duration(frame, pkt->duration);
759 av_frame_set_pkt_size (frame, pkt->size);
761 /* copy the replaygain data to the output frame */
762 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
764 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
766 return AVERROR(ENOMEM);
768 memcpy(frame_sd->data, packet_sd, size);
771 frame->pkt_pts = AV_NOPTS_VALUE;
772 av_frame_set_pkt_pos (frame, -1);
773 av_frame_set_pkt_duration(frame, 0);
774 av_frame_set_pkt_size (frame, -1);
776 frame->reordered_opaque = avctx->reordered_opaque;
778 switch (avctx->codec->type) {
779 case AVMEDIA_TYPE_VIDEO:
780 frame->format = avctx->pix_fmt;
781 if (!frame->sample_aspect_ratio.num)
782 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
783 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
784 av_frame_set_colorspace(frame, avctx->colorspace);
785 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
786 av_frame_set_color_range(frame, avctx->color_range);
788 case AVMEDIA_TYPE_AUDIO:
789 if (!frame->sample_rate)
790 frame->sample_rate = avctx->sample_rate;
791 if (frame->format < 0)
792 frame->format = avctx->sample_fmt;
793 if (!frame->channel_layout) {
794 if (avctx->channel_layout) {
795 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
797 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
799 return AVERROR(EINVAL);
802 frame->channel_layout = avctx->channel_layout;
804 if (avctx->channels > FF_SANE_NB_CHANNELS) {
805 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
807 return AVERROR(ENOSYS);
811 av_frame_set_channels(frame, avctx->channels);
817 #if FF_API_GET_BUFFER
818 FF_DISABLE_DEPRECATION_WARNINGS
819 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
821 return avcodec_default_get_buffer2(avctx, frame, 0);
824 typedef struct CompatReleaseBufPriv {
825 AVCodecContext avctx;
827 uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
828 } CompatReleaseBufPriv;
830 static void compat_free_buffer(void *opaque, uint8_t *data)
832 CompatReleaseBufPriv *priv = opaque;
833 if (priv->avctx.release_buffer)
834 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
838 static void compat_release_buffer(void *opaque, uint8_t *data)
840 AVBufferRef *buf = opaque;
841 av_buffer_unref(&buf);
843 FF_ENABLE_DEPRECATION_WARNINGS
846 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
848 return ff_init_buffer_info(avctx, frame);
851 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
853 int override_dimensions = 1;
856 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
857 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
858 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
859 return AVERROR(EINVAL);
862 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
863 if (frame->width <= 0 || frame->height <= 0) {
864 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
865 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
866 override_dimensions = 0;
869 ret = ff_decode_frame_props(avctx, frame);
872 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
875 #if FF_API_GET_BUFFER
876 FF_DISABLE_DEPRECATION_WARNINGS
878 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
879 * We wrap each plane in its own AVBuffer. Each of those has a reference to
880 * a dummy AVBuffer as its private data, unreffing it on free.
881 * When all the planes are freed, the dummy buffer's free callback calls
884 if (avctx->get_buffer) {
885 CompatReleaseBufPriv *priv = NULL;
886 AVBufferRef *dummy_buf = NULL;
889 if (flags & AV_GET_BUFFER_FLAG_REF)
890 frame->reference = 1;
892 ret = avctx->get_buffer(avctx, frame);
896 /* return if the buffers are already set up
897 * this would happen e.g. when a custom get_buffer() calls
898 * avcodec_default_get_buffer
903 priv = av_mallocz(sizeof(*priv));
905 ret = AVERROR(ENOMEM);
908 priv->avctx = *avctx;
909 priv->frame = *frame;
911 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
913 ret = AVERROR(ENOMEM);
917 #define WRAP_PLANE(ref_out, data, data_size) \
919 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
921 ret = AVERROR(ENOMEM); \
924 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
927 av_frame_unref(frame); \
928 ret = AVERROR(ENOMEM); \
933 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
934 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
936 planes = av_pix_fmt_count_planes(frame->format);
937 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
938 check for allocated buffers: make libavcodec happy */
939 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
941 if (!desc || planes <= 0) {
942 ret = AVERROR(EINVAL);
946 for (i = 0; i < planes; i++) {
947 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
948 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
950 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
953 int planar = av_sample_fmt_is_planar(frame->format);
954 planes = planar ? avctx->channels : 1;
956 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
957 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
958 frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
959 frame->nb_extended_buf);
960 if (!frame->extended_buf) {
961 ret = AVERROR(ENOMEM);
966 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
967 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
969 for (i = 0; i < frame->nb_extended_buf; i++)
970 WRAP_PLANE(frame->extended_buf[i],
971 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
975 av_buffer_unref(&dummy_buf);
978 frame->width = avctx->width;
979 frame->height = avctx->height;
984 avctx->release_buffer(avctx, frame);
986 av_buffer_unref(&dummy_buf);
989 FF_ENABLE_DEPRECATION_WARNINGS
992 ret = avctx->get_buffer2(avctx, frame, flags);
994 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
995 frame->width = avctx->width;
996 frame->height = avctx->height;
1002 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1004 int ret = get_buffer_internal(avctx, frame, flags);
1006 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1010 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
1015 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1017 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1018 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1019 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1020 av_frame_unref(frame);
1023 ff_init_buffer_info(avctx, frame);
1025 if (!frame->data[0])
1026 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1028 if (av_frame_is_writable(frame))
1029 return ff_decode_frame_props(avctx, frame);
1031 tmp = av_frame_alloc();
1033 return AVERROR(ENOMEM);
1035 av_frame_move_ref(tmp, frame);
1037 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1039 av_frame_free(&tmp);
1043 av_frame_copy(frame, tmp);
1044 av_frame_free(&tmp);
1049 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1051 int ret = reget_buffer_internal(avctx, frame);
1053 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1057 #if FF_API_GET_BUFFER
1058 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
1060 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
1062 av_frame_unref(pic);
1065 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1072 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1076 for (i = 0; i < count; i++) {
1077 int r = func(c, (char *)arg + i * size);
1084 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1088 for (i = 0; i < count; i++) {
1089 int r = func(c, arg, i, 0);
1096 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
1097 unsigned int fourcc)
1099 while (tags->pix_fmt >= 0) {
1100 if (tags->fourcc == fourcc)
1101 return tags->pix_fmt;
1104 return AV_PIX_FMT_NONE;
1107 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1109 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1110 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1113 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1115 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1120 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
1121 enum AVPixelFormat pix_fmt)
1123 AVHWAccel *hwaccel = NULL;
1125 while ((hwaccel = av_hwaccel_next(hwaccel)))
1126 if (hwaccel->id == codec_id
1127 && hwaccel->pix_fmt == pix_fmt)
1133 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1135 const AVPixFmtDescriptor *desc;
1136 enum AVPixelFormat ret = avctx->get_format(avctx, fmt);
1138 desc = av_pix_fmt_desc_get(ret);
1140 return AV_PIX_FMT_NONE;
1142 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1143 avctx->hwaccel = find_hwaccel(avctx->codec_id, ret);
1144 if (!avctx->hwaccel) {
1145 av_log(avctx, AV_LOG_ERROR,
1146 "Could not find an AVHWAccel for the pixel format: %s",
1148 return AV_PIX_FMT_NONE;
1151 avctx->hwaccel = NULL;
1157 #if FF_API_AVFRAME_LAVC
1158 void avcodec_get_frame_defaults(AVFrame *frame)
1160 #if LIBAVCODEC_VERSION_MAJOR >= 55
1161 // extended_data should explicitly be freed when needed, this code is unsafe currently
1162 // also this is not compatible to the <55 ABI/API
1163 if (frame->extended_data != frame->data && 0)
1164 av_freep(&frame->extended_data);
1167 memset(frame, 0, sizeof(AVFrame));
1168 av_frame_unref(frame);
1171 AVFrame *avcodec_alloc_frame(void)
1173 return av_frame_alloc();
1176 void avcodec_free_frame(AVFrame **frame)
1178 av_frame_free(frame);
1182 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1183 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1184 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1185 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1186 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1188 int av_codec_get_max_lowres(const AVCodec *codec)
1190 return codec->max_lowres;
1193 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1195 memset(sub, 0, sizeof(*sub));
1196 sub->pts = AV_NOPTS_VALUE;
1199 static int get_bit_rate(AVCodecContext *ctx)
1202 int bits_per_sample;
1204 switch (ctx->codec_type) {
1205 case AVMEDIA_TYPE_VIDEO:
1206 case AVMEDIA_TYPE_DATA:
1207 case AVMEDIA_TYPE_SUBTITLE:
1208 case AVMEDIA_TYPE_ATTACHMENT:
1209 bit_rate = ctx->bit_rate;
1211 case AVMEDIA_TYPE_AUDIO:
1212 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1213 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1222 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1226 ff_unlock_avcodec();
1228 ret = avcodec_open2(avctx, codec, options);
1230 ff_lock_avcodec(avctx);
1234 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1237 AVDictionary *tmp = NULL;
1239 if (avcodec_is_open(avctx))
1242 if ((!codec && !avctx->codec)) {
1243 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1244 return AVERROR(EINVAL);
1246 if ((codec && avctx->codec && codec != avctx->codec)) {
1247 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1248 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1249 return AVERROR(EINVAL);
1252 codec = avctx->codec;
1254 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1255 return AVERROR(EINVAL);
1258 av_dict_copy(&tmp, *options, 0);
1260 ret = ff_lock_avcodec(avctx);
1264 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1265 if (!avctx->internal) {
1266 ret = AVERROR(ENOMEM);
1270 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1271 if (!avctx->internal->pool) {
1272 ret = AVERROR(ENOMEM);
1276 avctx->internal->to_free = av_frame_alloc();
1277 if (!avctx->internal->to_free) {
1278 ret = AVERROR(ENOMEM);
1282 if (codec->priv_data_size > 0) {
1283 if (!avctx->priv_data) {
1284 avctx->priv_data = av_mallocz(codec->priv_data_size);
1285 if (!avctx->priv_data) {
1286 ret = AVERROR(ENOMEM);
1289 if (codec->priv_class) {
1290 *(const AVClass **)avctx->priv_data = codec->priv_class;
1291 av_opt_set_defaults(avctx->priv_data);
1294 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1297 avctx->priv_data = NULL;
1299 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1302 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1303 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1304 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1305 if (avctx->coded_width && avctx->coded_height)
1306 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1307 else if (avctx->width && avctx->height)
1308 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1313 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1314 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1315 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1316 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1317 ff_set_dimensions(avctx, 0, 0);
1320 /* if the decoder init function was already called previously,
1321 * free the already allocated subtitle_header before overwriting it */
1322 if (av_codec_is_decoder(codec))
1323 av_freep(&avctx->subtitle_header);
1325 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1326 ret = AVERROR(EINVAL);
1330 avctx->codec = codec;
1331 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1332 avctx->codec_id == AV_CODEC_ID_NONE) {
1333 avctx->codec_type = codec->type;
1334 avctx->codec_id = codec->id;
1336 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1337 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1338 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1339 ret = AVERROR(EINVAL);
1342 avctx->frame_number = 0;
1343 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1345 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1346 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1347 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1349 av_log(avctx, AV_LOG_ERROR,
1350 "The %s '%s' is experimental but experimental codecs are not enabled, "
1351 "add '-strict %d' if you want to use it.\n",
1352 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1353 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1354 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1355 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1356 codec_string, codec2->name);
1357 ret = AVERROR_EXPERIMENTAL;
1361 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1362 (!avctx->time_base.num || !avctx->time_base.den)) {
1363 avctx->time_base.num = 1;
1364 avctx->time_base.den = avctx->sample_rate;
1368 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1370 if (CONFIG_FRAME_THREAD_ENCODER) {
1371 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1372 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1373 ff_lock_avcodec(avctx);
1379 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1380 ret = ff_thread_init(avctx);
1385 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1386 avctx->thread_count = 1;
1388 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1389 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1390 avctx->codec->max_lowres);
1391 ret = AVERROR(EINVAL);
1395 if (av_codec_is_encoder(avctx->codec)) {
1397 if (avctx->codec->sample_fmts) {
1398 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1399 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1401 if (avctx->channels == 1 &&
1402 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1403 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1404 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1408 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1410 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1411 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1412 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1413 ret = AVERROR(EINVAL);
1417 if (avctx->codec->pix_fmts) {
1418 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1419 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1421 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1422 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1423 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1425 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1426 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1427 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1428 ret = AVERROR(EINVAL);
1432 if (avctx->codec->supported_samplerates) {
1433 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1434 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1436 if (avctx->codec->supported_samplerates[i] == 0) {
1437 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1438 avctx->sample_rate);
1439 ret = AVERROR(EINVAL);
1443 if (avctx->codec->channel_layouts) {
1444 if (!avctx->channel_layout) {
1445 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1447 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1448 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1450 if (avctx->codec->channel_layouts[i] == 0) {
1452 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1453 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1454 ret = AVERROR(EINVAL);
1459 if (avctx->channel_layout && avctx->channels) {
1460 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1461 if (channels != avctx->channels) {
1463 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1464 av_log(avctx, AV_LOG_ERROR,
1465 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1466 buf, channels, avctx->channels);
1467 ret = AVERROR(EINVAL);
1470 } else if (avctx->channel_layout) {
1471 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1473 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1474 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1476 if (avctx->width <= 0 || avctx->height <= 0) {
1477 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1478 ret = AVERROR(EINVAL);
1482 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1483 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1484 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1487 if (!avctx->rc_initial_buffer_occupancy)
1488 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1491 avctx->pts_correction_num_faulty_pts =
1492 avctx->pts_correction_num_faulty_dts = 0;
1493 avctx->pts_correction_last_pts =
1494 avctx->pts_correction_last_dts = INT64_MIN;
1496 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1497 || avctx->internal->frame_thread_encoder)) {
1498 ret = avctx->codec->init(avctx);
1506 if (av_codec_is_decoder(avctx->codec)) {
1507 if (!avctx->bit_rate)
1508 avctx->bit_rate = get_bit_rate(avctx);
1509 /* validate channel layout from the decoder */
1510 if (avctx->channel_layout) {
1511 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1512 if (!avctx->channels)
1513 avctx->channels = channels;
1514 else if (channels != avctx->channels) {
1516 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1517 av_log(avctx, AV_LOG_WARNING,
1518 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1519 "ignoring specified channel layout\n",
1520 buf, channels, avctx->channels);
1521 avctx->channel_layout = 0;
1524 if (avctx->channels && avctx->channels < 0 ||
1525 avctx->channels > FF_SANE_NB_CHANNELS) {
1526 ret = AVERROR(EINVAL);
1529 if (avctx->sub_charenc) {
1530 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1531 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1532 "supported with subtitles codecs\n");
1533 ret = AVERROR(EINVAL);
1535 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1536 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1537 "subtitles character encoding will be ignored\n",
1538 avctx->codec_descriptor->name);
1539 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1541 /* input character encoding is set for a text based subtitle
1542 * codec at this point */
1543 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1544 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1546 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1548 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1549 if (cd == (iconv_t)-1) {
1550 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1551 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1552 ret = AVERROR(errno);
1557 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1558 "conversion needs a libavcodec built with iconv support "
1559 "for this codec\n");
1560 ret = AVERROR(ENOSYS);
1568 ff_unlock_avcodec();
1570 av_dict_free(options);
1577 av_freep(&avctx->priv_data);
1578 if (avctx->internal) {
1579 av_frame_free(&avctx->internal->to_free);
1580 av_freep(&avctx->internal->pool);
1582 av_freep(&avctx->internal);
1583 avctx->codec = NULL;
1587 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1589 if (avpkt->size < 0) {
1590 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1591 return AVERROR(EINVAL);
1593 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1594 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1595 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1596 return AVERROR(EINVAL);
1600 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1601 if (!avpkt->data || avpkt->size < size) {
1602 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1603 avpkt->data = avctx->internal->byte_buffer;
1604 avpkt->size = avctx->internal->byte_buffer_size;
1605 avpkt->destruct = NULL;
1610 AVBufferRef *buf = avpkt->buf;
1611 #if FF_API_DESTRUCT_PACKET
1612 FF_DISABLE_DEPRECATION_WARNINGS
1613 void *destruct = avpkt->destruct;
1614 FF_ENABLE_DEPRECATION_WARNINGS
1617 if (avpkt->size < size) {
1618 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1619 return AVERROR(EINVAL);
1622 av_init_packet(avpkt);
1623 #if FF_API_DESTRUCT_PACKET
1624 FF_DISABLE_DEPRECATION_WARNINGS
1625 avpkt->destruct = destruct;
1626 FF_ENABLE_DEPRECATION_WARNINGS
1632 int ret = av_new_packet(avpkt, size);
1634 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1639 int ff_alloc_packet(AVPacket *avpkt, int size)
1641 return ff_alloc_packet2(NULL, avpkt, size);
1645 * Pad last frame with silence.
1647 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1649 AVFrame *frame = NULL;
1652 if (!(frame = av_frame_alloc()))
1653 return AVERROR(ENOMEM);
1655 frame->format = src->format;
1656 frame->channel_layout = src->channel_layout;
1657 av_frame_set_channels(frame, av_frame_get_channels(src));
1658 frame->nb_samples = s->frame_size;
1659 ret = av_frame_get_buffer(frame, 32);
1663 ret = av_frame_copy_props(frame, src);
1667 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1668 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1670 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1671 frame->nb_samples - src->nb_samples,
1672 s->channels, s->sample_fmt)) < 0)
1680 av_frame_free(&frame);
1684 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1686 const AVFrame *frame,
1687 int *got_packet_ptr)
1689 AVFrame *extended_frame = NULL;
1690 AVFrame *padded_frame = NULL;
1692 AVPacket user_pkt = *avpkt;
1693 int needs_realloc = !user_pkt.data;
1695 *got_packet_ptr = 0;
1697 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1698 av_free_packet(avpkt);
1699 av_init_packet(avpkt);
1703 /* ensure that extended_data is properly set */
1704 if (frame && !frame->extended_data) {
1705 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1706 avctx->channels > AV_NUM_DATA_POINTERS) {
1707 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1708 "with more than %d channels, but extended_data is not set.\n",
1709 AV_NUM_DATA_POINTERS);
1710 return AVERROR(EINVAL);
1712 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1714 extended_frame = av_frame_alloc();
1715 if (!extended_frame)
1716 return AVERROR(ENOMEM);
1718 memcpy(extended_frame, frame, sizeof(AVFrame));
1719 extended_frame->extended_data = extended_frame->data;
1720 frame = extended_frame;
1723 /* check for valid frame size */
1725 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1726 if (frame->nb_samples > avctx->frame_size) {
1727 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1728 ret = AVERROR(EINVAL);
1731 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1732 if (frame->nb_samples < avctx->frame_size &&
1733 !avctx->internal->last_audio_frame) {
1734 ret = pad_last_frame(avctx, &padded_frame, frame);
1738 frame = padded_frame;
1739 avctx->internal->last_audio_frame = 1;
1742 if (frame->nb_samples != avctx->frame_size) {
1743 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1744 ret = AVERROR(EINVAL);
1750 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1752 if (*got_packet_ptr) {
1753 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1754 if (avpkt->pts == AV_NOPTS_VALUE)
1755 avpkt->pts = frame->pts;
1756 if (!avpkt->duration)
1757 avpkt->duration = ff_samples_to_time_base(avctx,
1760 avpkt->dts = avpkt->pts;
1765 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1767 if (user_pkt.data) {
1768 if (user_pkt.size >= avpkt->size) {
1769 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1771 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1772 avpkt->size = user_pkt.size;
1775 avpkt->buf = user_pkt.buf;
1776 avpkt->data = user_pkt.data;
1777 avpkt->destruct = user_pkt.destruct;
1779 if (av_dup_packet(avpkt) < 0) {
1780 ret = AVERROR(ENOMEM);
1786 if (needs_realloc && avpkt->data) {
1787 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1789 avpkt->data = avpkt->buf->data;
1792 avctx->frame_number++;
1795 if (ret < 0 || !*got_packet_ptr) {
1796 av_free_packet(avpkt);
1797 av_init_packet(avpkt);
1801 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1802 * this needs to be moved to the encoders, but for now we can do it
1803 * here to simplify things */
1804 avpkt->flags |= AV_PKT_FLAG_KEY;
1807 av_frame_free(&padded_frame);
1808 av_free(extended_frame);
1813 #if FF_API_OLD_ENCODE_AUDIO
1814 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1815 uint8_t *buf, int buf_size,
1816 const short *samples)
1820 int ret, samples_size, got_packet;
1822 av_init_packet(&pkt);
1824 pkt.size = buf_size;
1827 frame = av_frame_alloc();
1829 return AVERROR(ENOMEM);
1831 if (avctx->frame_size) {
1832 frame->nb_samples = avctx->frame_size;
1834 /* if frame_size is not set, the number of samples must be
1835 * calculated from the buffer size */
1837 if (!av_get_bits_per_sample(avctx->codec_id)) {
1838 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1839 "support this codec\n");
1840 av_frame_free(&frame);
1841 return AVERROR(EINVAL);
1843 nb_samples = (int64_t)buf_size * 8 /
1844 (av_get_bits_per_sample(avctx->codec_id) *
1846 if (nb_samples >= INT_MAX) {
1847 av_frame_free(&frame);
1848 return AVERROR(EINVAL);
1850 frame->nb_samples = nb_samples;
1853 /* it is assumed that the samples buffer is large enough based on the
1854 * relevant parameters */
1855 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1857 avctx->sample_fmt, 1);
1858 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1860 (const uint8_t *)samples,
1861 samples_size, 1)) < 0) {
1862 av_frame_free(&frame);
1866 /* fabricate frame pts from sample count.
1867 * this is needed because the avcodec_encode_audio() API does not have
1868 * a way for the user to provide pts */
1869 if (avctx->sample_rate && avctx->time_base.num)
1870 frame->pts = ff_samples_to_time_base(avctx,
1871 avctx->internal->sample_count);
1873 frame->pts = AV_NOPTS_VALUE;
1874 avctx->internal->sample_count += frame->nb_samples;
1880 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1881 if (!ret && got_packet && avctx->coded_frame) {
1882 avctx->coded_frame->pts = pkt.pts;
1883 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1885 /* free any side data since we cannot return it */
1886 av_packet_free_side_data(&pkt);
1888 if (frame && frame->extended_data != frame->data)
1889 av_freep(&frame->extended_data);
1891 av_frame_free(&frame);
1892 return ret ? ret : pkt.size;
1897 #if FF_API_OLD_ENCODE_VIDEO
1898 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1899 const AVFrame *pict)
1902 int ret, got_packet = 0;
1904 if (buf_size < FF_MIN_BUFFER_SIZE) {
1905 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1909 av_init_packet(&pkt);
1911 pkt.size = buf_size;
1913 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1914 if (!ret && got_packet && avctx->coded_frame) {
1915 avctx->coded_frame->pts = pkt.pts;
1916 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1919 /* free any side data since we cannot return it */
1920 if (pkt.side_data_elems > 0) {
1922 for (i = 0; i < pkt.side_data_elems; i++)
1923 av_free(pkt.side_data[i].data);
1924 av_freep(&pkt.side_data);
1925 pkt.side_data_elems = 0;
1928 return ret ? ret : pkt.size;
1933 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1935 const AVFrame *frame,
1936 int *got_packet_ptr)
1939 AVPacket user_pkt = *avpkt;
1940 int needs_realloc = !user_pkt.data;
1942 *got_packet_ptr = 0;
1944 if(CONFIG_FRAME_THREAD_ENCODER &&
1945 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1946 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1948 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1949 avctx->stats_out[0] = '\0';
1951 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1952 av_free_packet(avpkt);
1953 av_init_packet(avpkt);
1958 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1959 return AVERROR(EINVAL);
1961 av_assert0(avctx->codec->encode2);
1963 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1964 av_assert0(ret <= 0);
1966 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1968 if (user_pkt.data) {
1969 if (user_pkt.size >= avpkt->size) {
1970 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1972 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1973 avpkt->size = user_pkt.size;
1976 avpkt->buf = user_pkt.buf;
1977 avpkt->data = user_pkt.data;
1978 avpkt->destruct = user_pkt.destruct;
1980 if (av_dup_packet(avpkt) < 0) {
1981 ret = AVERROR(ENOMEM);
1987 if (!*got_packet_ptr)
1989 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1990 avpkt->pts = avpkt->dts = frame->pts;
1992 if (needs_realloc && avpkt->data) {
1993 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1995 avpkt->data = avpkt->buf->data;
1998 avctx->frame_number++;
2001 if (ret < 0 || !*got_packet_ptr)
2002 av_free_packet(avpkt);
2004 av_packet_merge_side_data(avpkt);
2010 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
2011 const AVSubtitle *sub)
2014 if (sub->start_display_time) {
2015 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
2019 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
2020 avctx->frame_number++;
2025 * Attempt to guess proper monotonic timestamps for decoded video frames
2026 * which might have incorrect times. Input timestamps may wrap around, in
2027 * which case the output will as well.
2029 * @param pts the pts field of the decoded AVPacket, as passed through
2031 * @param dts the dts field of the decoded AVPacket
2032 * @return one of the input values, may be AV_NOPTS_VALUE
2034 static int64_t guess_correct_pts(AVCodecContext *ctx,
2035 int64_t reordered_pts, int64_t dts)
2037 int64_t pts = AV_NOPTS_VALUE;
2039 if (dts != AV_NOPTS_VALUE) {
2040 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
2041 ctx->pts_correction_last_dts = dts;
2042 } else if (reordered_pts != AV_NOPTS_VALUE)
2043 ctx->pts_correction_last_dts = reordered_pts;
2045 if (reordered_pts != AV_NOPTS_VALUE) {
2046 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
2047 ctx->pts_correction_last_pts = reordered_pts;
2048 } else if(dts != AV_NOPTS_VALUE)
2049 ctx->pts_correction_last_pts = dts;
2051 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
2052 && reordered_pts != AV_NOPTS_VALUE)
2053 pts = reordered_pts;
2060 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
2063 const uint8_t *data;
2066 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
2070 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
2071 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
2072 "changes, but PARAM_CHANGE side data was sent to it.\n");
2073 return AVERROR(EINVAL);
2079 flags = bytestream_get_le32(&data);
2082 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
2085 avctx->channels = bytestream_get_le32(&data);
2088 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
2091 avctx->channel_layout = bytestream_get_le64(&data);
2094 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
2097 avctx->sample_rate = bytestream_get_le32(&data);
2100 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
2103 avctx->width = bytestream_get_le32(&data);
2104 avctx->height = bytestream_get_le32(&data);
2106 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2113 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2114 return AVERROR_INVALIDDATA;
2117 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
2120 const uint8_t *side_metadata;
2122 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2124 side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2125 AV_PKT_DATA_STRINGS_METADATA, &size);
2126 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2129 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2133 /* move the original frame to our backup */
2134 av_frame_unref(avci->to_free);
2135 av_frame_move_ref(avci->to_free, frame);
2137 /* now copy everything except the AVBufferRefs back
2138 * note that we make a COPY of the side data, so calling av_frame_free() on
2139 * the caller's frame will work properly */
2140 ret = av_frame_copy_props(frame, avci->to_free);
2144 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2145 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2146 if (avci->to_free->extended_data != avci->to_free->data) {
2147 int planes = av_frame_get_channels(avci->to_free);
2148 int size = planes * sizeof(*frame->extended_data);
2151 av_frame_unref(frame);
2155 frame->extended_data = av_malloc(size);
2156 if (!frame->extended_data) {
2157 av_frame_unref(frame);
2158 return AVERROR(ENOMEM);
2160 memcpy(frame->extended_data, avci->to_free->extended_data,
2163 frame->extended_data = frame->data;
2165 frame->format = avci->to_free->format;
2166 frame->width = avci->to_free->width;
2167 frame->height = avci->to_free->height;
2168 frame->channel_layout = avci->to_free->channel_layout;
2169 frame->nb_samples = avci->to_free->nb_samples;
2170 av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
2175 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2176 int *got_picture_ptr,
2177 const AVPacket *avpkt)
2179 AVCodecInternal *avci = avctx->internal;
2181 // copy to ensure we do not change avpkt
2182 AVPacket tmp = *avpkt;
2185 return AVERROR(EINVAL);
2186 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2187 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2188 return AVERROR(EINVAL);
2191 *got_picture_ptr = 0;
2192 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2193 return AVERROR(EINVAL);
2195 av_frame_unref(picture);
2197 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2198 int did_split = av_packet_split_side_data(&tmp);
2199 ret = apply_param_change(avctx, &tmp);
2201 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2202 if (avctx->err_recognition & AV_EF_EXPLODE)
2206 avctx->internal->pkt = &tmp;
2207 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2208 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2211 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2213 picture->pkt_dts = avpkt->dts;
2215 if(!avctx->has_b_frames){
2216 av_frame_set_pkt_pos(picture, avpkt->pos);
2218 //FIXME these should be under if(!avctx->has_b_frames)
2219 /* get_buffer is supposed to set frame parameters */
2220 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2221 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2222 if (!picture->width) picture->width = avctx->width;
2223 if (!picture->height) picture->height = avctx->height;
2224 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2227 add_metadata_from_side_data(avctx, picture);
2230 emms_c(); //needed to avoid an emms_c() call before every return;
2232 avctx->internal->pkt = NULL;
2234 av_packet_free_side_data(&tmp);
2239 if (*got_picture_ptr) {
2240 if (!avctx->refcounted_frames) {
2241 int err = unrefcount_frame(avci, picture);
2246 avctx->frame_number++;
2247 av_frame_set_best_effort_timestamp(picture,
2248 guess_correct_pts(avctx,
2252 av_frame_unref(picture);
2256 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2257 * make sure it's set correctly */
2258 av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2263 #if FF_API_OLD_DECODE_AUDIO
2264 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2265 int *frame_size_ptr,
2268 AVFrame *frame = av_frame_alloc();
2269 int ret, got_frame = 0;
2272 return AVERROR(ENOMEM);
2273 if (avctx->get_buffer != avcodec_default_get_buffer) {
2274 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2275 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2276 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2277 "avcodec_decode_audio4()\n");
2278 avctx->get_buffer = avcodec_default_get_buffer;
2279 avctx->release_buffer = avcodec_default_release_buffer;
2282 ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2284 if (ret >= 0 && got_frame) {
2286 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2287 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2289 avctx->sample_fmt, 1);
2290 if (*frame_size_ptr < data_size) {
2291 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2292 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2293 av_frame_free(&frame);
2294 return AVERROR(EINVAL);
2297 memcpy(samples, frame->extended_data[0], plane_size);
2299 if (planar && avctx->channels > 1) {
2300 uint8_t *out = ((uint8_t *)samples) + plane_size;
2301 for (ch = 1; ch < avctx->channels; ch++) {
2302 memcpy(out, frame->extended_data[ch], plane_size);
2306 *frame_size_ptr = data_size;
2308 *frame_size_ptr = 0;
2310 av_frame_free(&frame);
2316 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2319 const AVPacket *avpkt)
2321 AVCodecInternal *avci = avctx->internal;
2326 if (!avpkt->data && avpkt->size) {
2327 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2328 return AVERROR(EINVAL);
2331 return AVERROR(EINVAL);
2332 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2333 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2334 return AVERROR(EINVAL);
2337 av_frame_unref(frame);
2339 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2342 uint32_t discard_padding = 0;
2343 // copy to ensure we do not change avpkt
2344 AVPacket tmp = *avpkt;
2345 int did_split = av_packet_split_side_data(&tmp);
2346 ret = apply_param_change(avctx, &tmp);
2348 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2349 if (avctx->err_recognition & AV_EF_EXPLODE)
2353 avctx->internal->pkt = &tmp;
2354 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2355 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2357 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2358 frame->pkt_dts = avpkt->dts;
2360 if (ret >= 0 && *got_frame_ptr) {
2361 add_metadata_from_side_data(avctx, frame);
2362 avctx->frame_number++;
2363 av_frame_set_best_effort_timestamp(frame,
2364 guess_correct_pts(avctx,
2367 if (frame->format == AV_SAMPLE_FMT_NONE)
2368 frame->format = avctx->sample_fmt;
2369 if (!frame->channel_layout)
2370 frame->channel_layout = avctx->channel_layout;
2371 if (!av_frame_get_channels(frame))
2372 av_frame_set_channels(frame, avctx->channels);
2373 if (!frame->sample_rate)
2374 frame->sample_rate = avctx->sample_rate;
2377 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2378 if(side && side_size>=10) {
2379 avctx->internal->skip_samples = AV_RL32(side);
2380 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2381 avctx->internal->skip_samples);
2382 discard_padding = AV_RL32(side + 4);
2384 if (avctx->internal->skip_samples && *got_frame_ptr) {
2385 if(frame->nb_samples <= avctx->internal->skip_samples){
2387 avctx->internal->skip_samples -= frame->nb_samples;
2388 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2389 avctx->internal->skip_samples);
2391 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2392 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2393 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2394 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2395 (AVRational){1, avctx->sample_rate},
2396 avctx->pkt_timebase);
2397 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2398 frame->pkt_pts += diff_ts;
2399 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2400 frame->pkt_dts += diff_ts;
2401 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2402 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2404 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2406 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2407 avctx->internal->skip_samples, frame->nb_samples);
2408 frame->nb_samples -= avctx->internal->skip_samples;
2409 avctx->internal->skip_samples = 0;
2413 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
2414 if (discard_padding == frame->nb_samples) {
2417 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2418 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2419 (AVRational){1, avctx->sample_rate},
2420 avctx->pkt_timebase);
2421 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2422 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2424 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2426 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2427 discard_padding, frame->nb_samples);
2428 frame->nb_samples -= discard_padding;
2432 avctx->internal->pkt = NULL;
2434 av_packet_free_side_data(&tmp);
2439 if (ret >= 0 && *got_frame_ptr) {
2440 if (!avctx->refcounted_frames) {
2441 int err = unrefcount_frame(avci, frame);
2446 av_frame_unref(frame);
2452 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2453 static int recode_subtitle(AVCodecContext *avctx,
2454 AVPacket *outpkt, const AVPacket *inpkt)
2457 iconv_t cd = (iconv_t)-1;
2464 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2468 cd = iconv_open("UTF-8", avctx->sub_charenc);
2469 av_assert0(cd != (iconv_t)-1);
2474 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2475 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2476 ret = AVERROR(ENOMEM);
2480 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2483 outpkt->buf = tmp.buf;
2484 outpkt->data = tmp.data;
2485 outpkt->size = tmp.size;
2486 outb = outpkt->data;
2487 outl = outpkt->size;
2489 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2490 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2491 outl >= outpkt->size || inl != 0) {
2492 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2493 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2494 av_free_packet(&tmp);
2495 ret = AVERROR(errno);
2498 outpkt->size -= outl;
2499 memset(outpkt->data + outpkt->size, 0, outl);
2502 if (cd != (iconv_t)-1)
2506 av_assert0(!"requesting subtitles recoding without iconv");
2510 static int utf8_check(const uint8_t *str)
2512 const uint8_t *byte;
2513 uint32_t codepoint, min;
2517 GET_UTF8(codepoint, *(byte++), return 0;);
2518 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2519 1 << (5 * (byte - str) - 4);
2520 if (codepoint < min || codepoint >= 0x110000 ||
2521 codepoint == 0xFFFE /* BOM */ ||
2522 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2529 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2535 if (!avpkt->data && avpkt->size) {
2536 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2537 return AVERROR(EINVAL);
2540 return AVERROR(EINVAL);
2541 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2542 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2543 return AVERROR(EINVAL);
2547 avcodec_get_subtitle_defaults(sub);
2549 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2550 AVPacket pkt_recoded;
2551 AVPacket tmp = *avpkt;
2552 int did_split = av_packet_split_side_data(&tmp);
2553 //apply_param_change(avctx, &tmp);
2556 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2558 * If the side data is smaller than the buffer padding size, the
2559 * remaining bytes should have already been filled with zeros by the
2560 * original packet allocation anyway. */
2561 memset(tmp.data + tmp.size, 0,
2562 FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2566 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2570 avctx->internal->pkt = &pkt_recoded;
2572 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2573 sub->pts = av_rescale_q(avpkt->pts,
2574 avctx->pkt_timebase, AV_TIME_BASE_Q);
2575 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2576 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2577 !!*got_sub_ptr >= !!sub->num_rects);
2579 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2580 avctx->pkt_timebase.num) {
2581 AVRational ms = { 1, 1000 };
2582 sub->end_display_time = av_rescale_q(avpkt->duration,
2583 avctx->pkt_timebase, ms);
2586 for (i = 0; i < sub->num_rects; i++) {
2587 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2588 av_log(avctx, AV_LOG_ERROR,
2589 "Invalid UTF-8 in decoded subtitles text; "
2590 "maybe missing -sub_charenc option\n");
2591 avsubtitle_free(sub);
2592 return AVERROR_INVALIDDATA;
2596 if (tmp.data != pkt_recoded.data) { // did we recode?
2597 /* prevent from destroying side data from original packet */
2598 pkt_recoded.side_data = NULL;
2599 pkt_recoded.side_data_elems = 0;
2601 av_free_packet(&pkt_recoded);
2603 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2605 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2607 avctx->internal->pkt = NULL;
2611 av_packet_free_side_data(&tmp);
2617 avctx->frame_number++;
2623 void avsubtitle_free(AVSubtitle *sub)
2627 for (i = 0; i < sub->num_rects; i++) {
2628 av_freep(&sub->rects[i]->pict.data[0]);
2629 av_freep(&sub->rects[i]->pict.data[1]);
2630 av_freep(&sub->rects[i]->pict.data[2]);
2631 av_freep(&sub->rects[i]->pict.data[3]);
2632 av_freep(&sub->rects[i]->text);
2633 av_freep(&sub->rects[i]->ass);
2634 av_freep(&sub->rects[i]);
2637 av_freep(&sub->rects);
2639 memset(sub, 0, sizeof(AVSubtitle));
2642 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2646 ff_unlock_avcodec();
2648 ret = avcodec_close(avctx);
2650 ff_lock_avcodec(NULL);
2654 av_cold int avcodec_close(AVCodecContext *avctx)
2661 ret = ff_lock_avcodec(avctx);
2665 if (avcodec_is_open(avctx)) {
2666 FramePool *pool = avctx->internal->pool;
2668 if (CONFIG_FRAME_THREAD_ENCODER &&
2669 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2670 ff_unlock_avcodec();
2671 ff_frame_thread_encoder_free(avctx);
2672 ff_lock_avcodec(avctx);
2674 if (HAVE_THREADS && avctx->internal->thread_ctx)
2675 ff_thread_free(avctx);
2676 if (avctx->codec && avctx->codec->close)
2677 avctx->codec->close(avctx);
2678 avctx->coded_frame = NULL;
2679 avctx->internal->byte_buffer_size = 0;
2680 av_freep(&avctx->internal->byte_buffer);
2681 av_frame_free(&avctx->internal->to_free);
2682 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2683 av_buffer_pool_uninit(&pool->pools[i]);
2684 av_freep(&avctx->internal->pool);
2685 av_freep(&avctx->internal);
2688 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2689 av_opt_free(avctx->priv_data);
2691 av_freep(&avctx->priv_data);
2692 if (av_codec_is_encoder(avctx->codec))
2693 av_freep(&avctx->extradata);
2694 avctx->codec = NULL;
2695 avctx->active_thread_type = 0;
2697 ff_unlock_avcodec();
2701 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2704 //This is for future deprecatec codec ids, its empty since
2705 //last major bump but will fill up again over time, please don't remove it
2706 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2707 case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
2708 case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
2709 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2710 case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
2711 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2712 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2713 case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
2714 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2715 case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
2716 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2717 case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
2718 case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
2719 case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
2720 case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
2721 case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
2722 case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
2723 case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
2724 case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
2725 default : return id;
2729 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2731 AVCodec *p, *experimental = NULL;
2733 id= remap_deprecated_codec_id(id);
2735 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2737 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2744 return experimental;
2747 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2749 return find_encdec(id, 1);
2752 AVCodec *avcodec_find_encoder_by_name(const char *name)
2759 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2766 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2768 return find_encdec(id, 0);
2771 AVCodec *avcodec_find_decoder_by_name(const char *name)
2778 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2785 const char *avcodec_get_name(enum AVCodecID id)
2787 const AVCodecDescriptor *cd;
2790 if (id == AV_CODEC_ID_NONE)
2792 cd = avcodec_descriptor_get(id);
2795 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2796 codec = avcodec_find_decoder(id);
2799 codec = avcodec_find_encoder(id);
2802 return "unknown_codec";
2805 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2807 int i, len, ret = 0;
2809 #define TAG_PRINT(x) \
2810 (((x) >= '0' && (x) <= '9') || \
2811 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2812 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2814 for (i = 0; i < 4; i++) {
2815 len = snprintf(buf, buf_size,
2816 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2818 buf_size = buf_size > len ? buf_size - len : 0;
2825 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2827 const char *codec_type;
2828 const char *codec_name;
2829 const char *profile = NULL;
2832 AVRational display_aspect_ratio;
2834 if (!buf || buf_size <= 0)
2836 codec_type = av_get_media_type_string(enc->codec_type);
2837 codec_name = avcodec_get_name(enc->codec_id);
2838 if (enc->profile != FF_PROFILE_UNKNOWN) {
2842 p = encode ? avcodec_find_encoder(enc->codec_id) :
2843 avcodec_find_decoder(enc->codec_id);
2845 profile = av_get_profile_name(p, enc->profile);
2848 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2850 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2852 if (enc->codec && strcmp(enc->codec->name, codec_name))
2853 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2856 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2857 if (enc->codec_tag) {
2859 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2860 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2861 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2864 switch (enc->codec_type) {
2865 case AVMEDIA_TYPE_VIDEO:
2866 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2867 char detail[256] = "(";
2868 const char *colorspace_name;
2869 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2871 av_get_pix_fmt_name(enc->pix_fmt));
2872 if (enc->bits_per_raw_sample &&
2873 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2874 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2875 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2876 av_strlcatf(detail, sizeof(detail),
2877 enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
2879 colorspace_name = av_get_colorspace_name(enc->colorspace);
2880 if (colorspace_name)
2881 av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
2883 if (strlen(detail) > 1) {
2884 detail[strlen(detail) - 2] = 0;
2885 av_strlcatf(buf, buf_size, "%s)", detail);
2889 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2891 enc->width, enc->height);
2892 if (enc->sample_aspect_ratio.num) {
2893 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2894 enc->width * enc->sample_aspect_ratio.num,
2895 enc->height * enc->sample_aspect_ratio.den,
2897 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2898 " [SAR %d:%d DAR %d:%d]",
2899 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2900 display_aspect_ratio.num, display_aspect_ratio.den);
2902 if (av_log_get_level() >= AV_LOG_DEBUG) {
2903 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2904 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2906 enc->time_base.num / g, enc->time_base.den / g);
2910 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2911 ", q=%d-%d", enc->qmin, enc->qmax);
2914 case AVMEDIA_TYPE_AUDIO:
2915 if (enc->sample_rate) {
2916 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2917 ", %d Hz", enc->sample_rate);
2919 av_strlcat(buf, ", ", buf_size);
2920 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2921 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2922 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2923 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2926 case AVMEDIA_TYPE_DATA:
2927 if (av_log_get_level() >= AV_LOG_DEBUG) {
2928 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2930 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2932 enc->time_base.num / g, enc->time_base.den / g);
2935 case AVMEDIA_TYPE_SUBTITLE:
2937 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2938 ", %dx%d", enc->width, enc->height);
2944 if (enc->flags & CODEC_FLAG_PASS1)
2945 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2947 if (enc->flags & CODEC_FLAG_PASS2)
2948 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2951 bitrate = get_bit_rate(enc);
2953 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2954 ", %d kb/s", bitrate / 1000);
2955 } else if (enc->rc_max_rate > 0) {
2956 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2957 ", max. %d kb/s", enc->rc_max_rate / 1000);
2961 const char *av_get_profile_name(const AVCodec *codec, int profile)
2964 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2967 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2968 if (p->profile == profile)
2974 unsigned avcodec_version(void)
2976 // av_assert0(AV_CODEC_ID_V410==164);
2977 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2978 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2979 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2980 av_assert0(AV_CODEC_ID_SRT==94216);
2981 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2983 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2984 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2985 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2986 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2987 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2988 return LIBAVCODEC_VERSION_INT;
2991 const char *avcodec_configuration(void)
2993 return FFMPEG_CONFIGURATION;
2996 const char *avcodec_license(void)
2998 #define LICENSE_PREFIX "libavcodec license: "
2999 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
3002 void avcodec_flush_buffers(AVCodecContext *avctx)
3004 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
3005 ff_thread_flush(avctx);
3006 else if (avctx->codec->flush)
3007 avctx->codec->flush(avctx);
3009 avctx->pts_correction_last_pts =
3010 avctx->pts_correction_last_dts = INT64_MIN;
3012 if (!avctx->refcounted_frames)
3013 av_frame_unref(avctx->internal->to_free);
3016 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
3019 case AV_CODEC_ID_8SVX_EXP:
3020 case AV_CODEC_ID_8SVX_FIB:
3021 case AV_CODEC_ID_ADPCM_CT:
3022 case AV_CODEC_ID_ADPCM_IMA_APC:
3023 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
3024 case AV_CODEC_ID_ADPCM_IMA_OKI:
3025 case AV_CODEC_ID_ADPCM_IMA_WS:
3026 case AV_CODEC_ID_ADPCM_G722:
3027 case AV_CODEC_ID_ADPCM_YAMAHA:
3029 case AV_CODEC_ID_DSD_LSBF:
3030 case AV_CODEC_ID_DSD_MSBF:
3031 case AV_CODEC_ID_DSD_LSBF_PLANAR:
3032 case AV_CODEC_ID_DSD_MSBF_PLANAR:
3033 case AV_CODEC_ID_PCM_ALAW:
3034 case AV_CODEC_ID_PCM_MULAW:
3035 case AV_CODEC_ID_PCM_S8:
3036 case AV_CODEC_ID_PCM_S8_PLANAR:
3037 case AV_CODEC_ID_PCM_U8:
3038 case AV_CODEC_ID_PCM_ZORK:
3040 case AV_CODEC_ID_PCM_S16BE:
3041 case AV_CODEC_ID_PCM_S16BE_PLANAR:
3042 case AV_CODEC_ID_PCM_S16LE:
3043 case AV_CODEC_ID_PCM_S16LE_PLANAR:
3044 case AV_CODEC_ID_PCM_U16BE:
3045 case AV_CODEC_ID_PCM_U16LE:
3047 case AV_CODEC_ID_PCM_S24DAUD:
3048 case AV_CODEC_ID_PCM_S24BE:
3049 case AV_CODEC_ID_PCM_S24LE:
3050 case AV_CODEC_ID_PCM_S24LE_PLANAR:
3051 case AV_CODEC_ID_PCM_U24BE:
3052 case AV_CODEC_ID_PCM_U24LE:
3054 case AV_CODEC_ID_PCM_S32BE:
3055 case AV_CODEC_ID_PCM_S32LE:
3056 case AV_CODEC_ID_PCM_S32LE_PLANAR:
3057 case AV_CODEC_ID_PCM_U32BE:
3058 case AV_CODEC_ID_PCM_U32LE:
3059 case AV_CODEC_ID_PCM_F32BE:
3060 case AV_CODEC_ID_PCM_F32LE:
3062 case AV_CODEC_ID_PCM_F64BE:
3063 case AV_CODEC_ID_PCM_F64LE:
3070 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
3072 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
3073 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3074 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3075 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3076 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3077 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3078 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3079 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3080 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3081 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3082 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3084 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3085 return AV_CODEC_ID_NONE;
3086 if (be < 0 || be > 1)
3088 return map[fmt][be];
3091 int av_get_bits_per_sample(enum AVCodecID codec_id)
3094 case AV_CODEC_ID_ADPCM_SBPRO_2:
3096 case AV_CODEC_ID_ADPCM_SBPRO_3:
3098 case AV_CODEC_ID_ADPCM_SBPRO_4:
3099 case AV_CODEC_ID_ADPCM_IMA_WAV:
3100 case AV_CODEC_ID_ADPCM_IMA_QT:
3101 case AV_CODEC_ID_ADPCM_SWF:
3102 case AV_CODEC_ID_ADPCM_MS:
3105 return av_get_exact_bits_per_sample(codec_id);
3109 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3111 int id, sr, ch, ba, tag, bps;
3113 id = avctx->codec_id;
3114 sr = avctx->sample_rate;
3115 ch = avctx->channels;
3116 ba = avctx->block_align;
3117 tag = avctx->codec_tag;
3118 bps = av_get_exact_bits_per_sample(avctx->codec_id);
3120 /* codecs with an exact constant bits per sample */
3121 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3122 return (frame_bytes * 8LL) / (bps * ch);
3123 bps = avctx->bits_per_coded_sample;
3125 /* codecs with a fixed packet duration */
3127 case AV_CODEC_ID_ADPCM_ADX: return 32;
3128 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3129 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3130 case AV_CODEC_ID_AMR_NB:
3131 case AV_CODEC_ID_EVRC:
3132 case AV_CODEC_ID_GSM:
3133 case AV_CODEC_ID_QCELP:
3134 case AV_CODEC_ID_RA_288: return 160;
3135 case AV_CODEC_ID_AMR_WB:
3136 case AV_CODEC_ID_GSM_MS: return 320;
3137 case AV_CODEC_ID_MP1: return 384;
3138 case AV_CODEC_ID_ATRAC1: return 512;
3139 case AV_CODEC_ID_ATRAC3: return 1024;
3140 case AV_CODEC_ID_MP2:
3141 case AV_CODEC_ID_MUSEPACK7: return 1152;
3142 case AV_CODEC_ID_AC3: return 1536;
3146 /* calc from sample rate */
3147 if (id == AV_CODEC_ID_TTA)
3148 return 256 * sr / 245;
3151 /* calc from sample rate and channels */
3152 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3153 return (480 << (sr / 22050)) / ch;
3158 /* calc from block_align */
3159 if (id == AV_CODEC_ID_SIPR) {
3161 case 20: return 160;
3162 case 19: return 144;
3163 case 29: return 288;
3164 case 37: return 480;
3166 } else if (id == AV_CODEC_ID_ILBC) {
3168 case 38: return 160;
3169 case 50: return 240;
3174 if (frame_bytes > 0) {
3175 /* calc from frame_bytes only */
3176 if (id == AV_CODEC_ID_TRUESPEECH)
3177 return 240 * (frame_bytes / 32);
3178 if (id == AV_CODEC_ID_NELLYMOSER)
3179 return 256 * (frame_bytes / 64);
3180 if (id == AV_CODEC_ID_RA_144)
3181 return 160 * (frame_bytes / 20);
3182 if (id == AV_CODEC_ID_G723_1)
3183 return 240 * (frame_bytes / 24);
3186 /* calc from frame_bytes and bits_per_coded_sample */
3187 if (id == AV_CODEC_ID_ADPCM_G726)
3188 return frame_bytes * 8 / bps;
3192 /* calc from frame_bytes and channels */
3194 case AV_CODEC_ID_ADPCM_AFC:
3195 return frame_bytes / (9 * ch) * 16;
3196 case AV_CODEC_ID_ADPCM_DTK:
3197 return frame_bytes / (16 * ch) * 28;
3198 case AV_CODEC_ID_ADPCM_4XM:
3199 case AV_CODEC_ID_ADPCM_IMA_ISS:
3200 return (frame_bytes - 4 * ch) * 2 / ch;
3201 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3202 return (frame_bytes - 4) * 2 / ch;
3203 case AV_CODEC_ID_ADPCM_IMA_AMV:
3204 return (frame_bytes - 8) * 2 / ch;
3205 case AV_CODEC_ID_ADPCM_XA:
3206 return (frame_bytes / 128) * 224 / ch;
3207 case AV_CODEC_ID_INTERPLAY_DPCM:
3208 return (frame_bytes - 6 - ch) / ch;
3209 case AV_CODEC_ID_ROQ_DPCM:
3210 return (frame_bytes - 8) / ch;
3211 case AV_CODEC_ID_XAN_DPCM:
3212 return (frame_bytes - 2 * ch) / ch;
3213 case AV_CODEC_ID_MACE3:
3214 return 3 * frame_bytes / ch;
3215 case AV_CODEC_ID_MACE6:
3216 return 6 * frame_bytes / ch;
3217 case AV_CODEC_ID_PCM_LXF:
3218 return 2 * (frame_bytes / (5 * ch));
3219 case AV_CODEC_ID_IAC:
3220 case AV_CODEC_ID_IMC:
3221 return 4 * frame_bytes / ch;
3225 /* calc from frame_bytes, channels, and codec_tag */
3226 if (id == AV_CODEC_ID_SOL_DPCM) {
3228 return frame_bytes / ch;
3230 return frame_bytes * 2 / ch;
3235 /* calc from frame_bytes, channels, and block_align */
3236 int blocks = frame_bytes / ba;
3237 switch (avctx->codec_id) {
3238 case AV_CODEC_ID_ADPCM_IMA_WAV:
3239 if (bps < 2 || bps > 5)
3241 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3242 case AV_CODEC_ID_ADPCM_IMA_DK3:
3243 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3244 case AV_CODEC_ID_ADPCM_IMA_DK4:
3245 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3246 case AV_CODEC_ID_ADPCM_IMA_RAD:
3247 return blocks * ((ba - 4 * ch) * 2 / ch);
3248 case AV_CODEC_ID_ADPCM_MS:
3249 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3254 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3255 switch (avctx->codec_id) {
3256 case AV_CODEC_ID_PCM_DVD:
3259 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3260 case AV_CODEC_ID_PCM_BLURAY:
3263 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3264 case AV_CODEC_ID_S302M:
3265 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3275 int ff_thread_init(AVCodecContext *s)
3282 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3296 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3299 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3303 #if FF_API_MISSING_SAMPLE
3304 FF_DISABLE_DEPRECATION_WARNINGS
3305 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3307 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3308 "version to the newest one from Git. If the problem still "
3309 "occurs, it means that your file has a feature which has not "
3310 "been implemented.\n", feature);
3312 av_log_ask_for_sample(avc, NULL);
3315 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3317 va_list argument_list;
3319 va_start(argument_list, msg);
3322 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3323 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3324 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
3325 "and contact the ffmpeg-devel mailing list.\n");
3327 va_end(argument_list);
3329 FF_ENABLE_DEPRECATION_WARNINGS
3330 #endif /* FF_API_MISSING_SAMPLE */
3332 static AVHWAccel *first_hwaccel = NULL;
3333 static AVHWAccel **last_hwaccel = &first_hwaccel;
3335 void av_register_hwaccel(AVHWAccel *hwaccel)
3337 AVHWAccel **p = last_hwaccel;
3338 hwaccel->next = NULL;
3339 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3341 last_hwaccel = &hwaccel->next;
3344 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
3346 return hwaccel ? hwaccel->next : first_hwaccel;
3349 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3352 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3354 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3361 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3363 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3369 int ff_lock_avcodec(AVCodecContext *log_ctx)
3372 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3375 entangled_thread_counter++;
3376 if (entangled_thread_counter != 1) {
3377 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3379 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3380 ff_avcodec_locked = 1;
3381 ff_unlock_avcodec();
3382 return AVERROR(EINVAL);
3384 av_assert0(!ff_avcodec_locked);
3385 ff_avcodec_locked = 1;
3389 int ff_unlock_avcodec(void)
3391 av_assert0(ff_avcodec_locked);
3392 ff_avcodec_locked = 0;
3393 entangled_thread_counter--;
3395 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3401 int avpriv_lock_avformat(void)
3404 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3410 int avpriv_unlock_avformat(void)
3413 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3419 unsigned int avpriv_toupper4(unsigned int x)
3421 return av_toupper(x & 0xFF) +
3422 (av_toupper((x >> 8) & 0xFF) << 8) +
3423 (av_toupper((x >> 16) & 0xFF) << 16) +
3424 (av_toupper((x >> 24) & 0xFF) << 24);
3427 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3431 dst->owner = src->owner;
3433 ret = av_frame_ref(dst->f, src->f);
3437 if (src->progress &&
3438 !(dst->progress = av_buffer_ref(src->progress))) {
3439 ff_thread_release_buffer(dst->owner, dst);
3440 return AVERROR(ENOMEM);
3448 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3450 return ff_get_format(avctx, fmt);
3453 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3456 return ff_get_buffer(avctx, f->f, flags);
3459 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3462 av_frame_unref(f->f);
3465 void ff_thread_finish_setup(AVCodecContext *avctx)
3469 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3473 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3477 int ff_thread_can_start_frame(AVCodecContext *avctx)
3482 int ff_alloc_entries(AVCodecContext *avctx, int count)
3487 void ff_reset_entries(AVCodecContext *avctx)
3491 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3495 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3501 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3503 AVCodec *c= avcodec_find_decoder(codec_id);
3505 c= avcodec_find_encoder(codec_id);
3509 if (codec_id <= AV_CODEC_ID_NONE)
3510 return AVMEDIA_TYPE_UNKNOWN;
3511 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3512 return AVMEDIA_TYPE_VIDEO;
3513 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3514 return AVMEDIA_TYPE_AUDIO;
3515 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3516 return AVMEDIA_TYPE_SUBTITLE;
3518 return AVMEDIA_TYPE_UNKNOWN;
3521 int avcodec_is_open(AVCodecContext *s)
3523 return !!s->internal;
3526 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3531 ret = av_bprint_finalize(buf, &str);
3534 avctx->extradata = str;
3535 /* Note: the string is NUL terminated (so extradata can be read as a
3536 * string), but the ending character is not accounted in the size (in
3537 * binary formats you are likely not supposed to mux that character). When
3538 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3540 avctx->extradata_size = buf->len;
3544 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3546 uint32_t *av_restrict state)
3550 av_assert0(p <= end);
3554 for (i = 0; i < 3; i++) {
3555 uint32_t tmp = *state << 8;
3556 *state = tmp + *(p++);
3557 if (tmp == 0x100 || p == end)
3562 if (p[-1] > 1 ) p += 3;
3563 else if (p[-2] ) p += 2;
3564 else if (p[-3]|(p[-1]-1)) p++;
3571 p = FFMIN(p, end) - 4;
3572 *state = AV_RB32(p);