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"
44 #include "libavutil/opt.h"
46 #include "mpegvideo.h"
48 #include "frame_thread_encoder.h"
51 #include "bytestream.h"
64 #include "compat/w32pthreads.h"
66 #include "compat/os2threads.h"
69 #include "libavutil/ffversion.h"
70 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
72 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
73 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
75 void * volatile * mutex = arg;
83 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
85 return AVERROR(ENOMEM);
86 if ((err = pthread_mutex_init(tmp, NULL))) {
90 if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
91 pthread_mutex_destroy(tmp);
96 if ((err = pthread_mutex_lock(*mutex)))
100 case AV_LOCK_RELEASE:
101 if ((err = pthread_mutex_unlock(*mutex)))
105 case AV_LOCK_DESTROY:
107 pthread_mutex_destroy(*mutex);
109 avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
114 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
116 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
120 volatile int ff_avcodec_locked;
121 static int volatile entangled_thread_counter = 0;
122 static void *codec_mutex;
123 static void *avformat_mutex;
125 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
128 if (min_size <= *size && *p)
130 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
132 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
139 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
142 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
147 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
148 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
151 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
154 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
159 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
160 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
163 /* encoder management */
164 static AVCodec *first_avcodec = NULL;
165 static AVCodec **last_avcodec = &first_avcodec;
167 AVCodec *av_codec_next(const AVCodec *c)
172 return first_avcodec;
175 static av_cold void avcodec_init(void)
177 static int initialized = 0;
179 if (initialized != 0)
184 ff_me_cmp_init_static();
187 int av_codec_is_encoder(const AVCodec *codec)
189 return codec && (codec->encode_sub || codec->encode2);
192 int av_codec_is_decoder(const AVCodec *codec)
194 return codec && codec->decode;
197 av_cold void avcodec_register(AVCodec *codec)
204 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
206 last_avcodec = &codec->next;
208 if (codec->init_static_data)
209 codec->init_static_data(codec);
213 unsigned avcodec_get_edge_width(void)
219 #if FF_API_SET_DIMENSIONS
220 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
222 int ret = ff_set_dimensions(s, width, height);
224 av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
229 int ff_set_dimensions(AVCodecContext *s, int width, int height)
231 int ret = av_image_check_size(width, height, 0, s);
236 s->coded_width = width;
237 s->coded_height = height;
238 s->width = FF_CEIL_RSHIFT(width, s->lowres);
239 s->height = FF_CEIL_RSHIFT(height, s->lowres);
244 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
246 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
249 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
251 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
254 avctx->sample_aspect_ratio = sar;
259 int ff_side_data_update_matrix_encoding(AVFrame *frame,
260 enum AVMatrixEncoding matrix_encoding)
262 AVFrameSideData *side_data;
263 enum AVMatrixEncoding *data;
265 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
267 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
268 sizeof(enum AVMatrixEncoding));
271 return AVERROR(ENOMEM);
273 data = (enum AVMatrixEncoding*)side_data->data;
274 *data = matrix_encoding;
279 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
280 int linesize_align[AV_NUM_DATA_POINTERS])
285 AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
288 w_align = 1 << desc->log2_chroma_w;
289 h_align = 1 << desc->log2_chroma_h;
292 switch (s->pix_fmt) {
293 case AV_PIX_FMT_YUV420P:
294 case AV_PIX_FMT_YUYV422:
295 case AV_PIX_FMT_YVYU422:
296 case AV_PIX_FMT_UYVY422:
297 case AV_PIX_FMT_YUV422P:
298 case AV_PIX_FMT_YUV440P:
299 case AV_PIX_FMT_YUV444P:
300 case AV_PIX_FMT_GBRP:
301 case AV_PIX_FMT_GBRAP:
302 case AV_PIX_FMT_GRAY8:
303 case AV_PIX_FMT_GRAY16BE:
304 case AV_PIX_FMT_GRAY16LE:
305 case AV_PIX_FMT_YUVJ420P:
306 case AV_PIX_FMT_YUVJ422P:
307 case AV_PIX_FMT_YUVJ440P:
308 case AV_PIX_FMT_YUVJ444P:
309 case AV_PIX_FMT_YUVA420P:
310 case AV_PIX_FMT_YUVA422P:
311 case AV_PIX_FMT_YUVA444P:
312 case AV_PIX_FMT_YUV420P9LE:
313 case AV_PIX_FMT_YUV420P9BE:
314 case AV_PIX_FMT_YUV420P10LE:
315 case AV_PIX_FMT_YUV420P10BE:
316 case AV_PIX_FMT_YUV420P12LE:
317 case AV_PIX_FMT_YUV420P12BE:
318 case AV_PIX_FMT_YUV420P14LE:
319 case AV_PIX_FMT_YUV420P14BE:
320 case AV_PIX_FMT_YUV420P16LE:
321 case AV_PIX_FMT_YUV420P16BE:
322 case AV_PIX_FMT_YUVA420P9LE:
323 case AV_PIX_FMT_YUVA420P9BE:
324 case AV_PIX_FMT_YUVA420P10LE:
325 case AV_PIX_FMT_YUVA420P10BE:
326 case AV_PIX_FMT_YUVA420P16LE:
327 case AV_PIX_FMT_YUVA420P16BE:
328 case AV_PIX_FMT_YUV422P9LE:
329 case AV_PIX_FMT_YUV422P9BE:
330 case AV_PIX_FMT_YUV422P10LE:
331 case AV_PIX_FMT_YUV422P10BE:
332 case AV_PIX_FMT_YUV422P12LE:
333 case AV_PIX_FMT_YUV422P12BE:
334 case AV_PIX_FMT_YUV422P14LE:
335 case AV_PIX_FMT_YUV422P14BE:
336 case AV_PIX_FMT_YUV422P16LE:
337 case AV_PIX_FMT_YUV422P16BE:
338 case AV_PIX_FMT_YUVA422P9LE:
339 case AV_PIX_FMT_YUVA422P9BE:
340 case AV_PIX_FMT_YUVA422P10LE:
341 case AV_PIX_FMT_YUVA422P10BE:
342 case AV_PIX_FMT_YUVA422P16LE:
343 case AV_PIX_FMT_YUVA422P16BE:
344 case AV_PIX_FMT_YUV440P10LE:
345 case AV_PIX_FMT_YUV440P10BE:
346 case AV_PIX_FMT_YUV440P12LE:
347 case AV_PIX_FMT_YUV440P12BE:
348 case AV_PIX_FMT_YUV444P9LE:
349 case AV_PIX_FMT_YUV444P9BE:
350 case AV_PIX_FMT_YUV444P10LE:
351 case AV_PIX_FMT_YUV444P10BE:
352 case AV_PIX_FMT_YUV444P12LE:
353 case AV_PIX_FMT_YUV444P12BE:
354 case AV_PIX_FMT_YUV444P14LE:
355 case AV_PIX_FMT_YUV444P14BE:
356 case AV_PIX_FMT_YUV444P16LE:
357 case AV_PIX_FMT_YUV444P16BE:
358 case AV_PIX_FMT_YUVA444P9LE:
359 case AV_PIX_FMT_YUVA444P9BE:
360 case AV_PIX_FMT_YUVA444P10LE:
361 case AV_PIX_FMT_YUVA444P10BE:
362 case AV_PIX_FMT_YUVA444P16LE:
363 case AV_PIX_FMT_YUVA444P16BE:
364 case AV_PIX_FMT_GBRP9LE:
365 case AV_PIX_FMT_GBRP9BE:
366 case AV_PIX_FMT_GBRP10LE:
367 case AV_PIX_FMT_GBRP10BE:
368 case AV_PIX_FMT_GBRP12LE:
369 case AV_PIX_FMT_GBRP12BE:
370 case AV_PIX_FMT_GBRP14LE:
371 case AV_PIX_FMT_GBRP14BE:
372 case AV_PIX_FMT_GBRP16LE:
373 case AV_PIX_FMT_GBRP16BE:
374 w_align = 16; //FIXME assume 16 pixel per macroblock
375 h_align = 16 * 2; // interlaced needs 2 macroblocks height
377 case AV_PIX_FMT_YUV411P:
378 case AV_PIX_FMT_YUVJ411P:
379 case AV_PIX_FMT_UYYVYY411:
383 case AV_PIX_FMT_YUV410P:
384 if (s->codec_id == AV_CODEC_ID_SVQ1) {
389 case AV_PIX_FMT_RGB555:
390 if (s->codec_id == AV_CODEC_ID_RPZA) {
395 case AV_PIX_FMT_PAL8:
396 case AV_PIX_FMT_BGR8:
397 case AV_PIX_FMT_RGB8:
398 if (s->codec_id == AV_CODEC_ID_SMC ||
399 s->codec_id == AV_CODEC_ID_CINEPAK) {
403 if (s->codec_id == AV_CODEC_ID_JV) {
408 case AV_PIX_FMT_BGR24:
409 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
410 (s->codec_id == AV_CODEC_ID_ZLIB)) {
415 case AV_PIX_FMT_RGB24:
416 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
425 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
426 w_align = FFMAX(w_align, 8);
429 *width = FFALIGN(*width, w_align);
430 *height = FFALIGN(*height, h_align);
431 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
432 // some of the optimized chroma MC reads one line too much
433 // which is also done in mpeg decoders with lowres > 0
436 for (i = 0; i < 4; i++)
437 linesize_align[i] = STRIDE_ALIGN;
440 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
442 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
443 int chroma_shift = desc->log2_chroma_w;
444 int linesize_align[AV_NUM_DATA_POINTERS];
447 avcodec_align_dimensions2(s, width, height, linesize_align);
448 align = FFMAX(linesize_align[0], linesize_align[3]);
449 linesize_align[1] <<= chroma_shift;
450 linesize_align[2] <<= chroma_shift;
451 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
452 *width = FFALIGN(*width, align);
455 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
457 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
458 return AVERROR(EINVAL);
461 *xpos = (pos&1) * 128;
462 *ypos = ((pos>>1)^(pos<4)) * 128;
467 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
471 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
472 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
475 return AVCHROMA_LOC_UNSPECIFIED;
478 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
479 enum AVSampleFormat sample_fmt, const uint8_t *buf,
480 int buf_size, int align)
482 int ch, planar, needed_size, ret = 0;
484 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
485 frame->nb_samples, sample_fmt,
487 if (buf_size < needed_size)
488 return AVERROR(EINVAL);
490 planar = av_sample_fmt_is_planar(sample_fmt);
491 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
492 if (!(frame->extended_data = av_mallocz_array(nb_channels,
493 sizeof(*frame->extended_data))))
494 return AVERROR(ENOMEM);
496 frame->extended_data = frame->data;
499 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
500 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
501 sample_fmt, align)) < 0) {
502 if (frame->extended_data != frame->data)
503 av_freep(&frame->extended_data);
506 if (frame->extended_data != frame->data) {
507 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
508 frame->data[ch] = frame->extended_data[ch];
514 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
516 FramePool *pool = avctx->internal->pool;
519 switch (avctx->codec_type) {
520 case AVMEDIA_TYPE_VIDEO: {
523 int w = frame->width;
524 int h = frame->height;
525 int tmpsize, unaligned;
527 if (pool->format == frame->format &&
528 pool->width == frame->width && pool->height == frame->height)
531 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
534 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
535 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
536 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
537 // increase alignment of w for next try (rhs gives the lowest bit set in w)
541 for (i = 0; i < 4; i++)
542 unaligned |= picture.linesize[i] % pool->stride_align[i];
545 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
546 NULL, picture.linesize);
550 for (i = 0; i < 3 && picture.data[i + 1]; i++)
551 size[i] = picture.data[i + 1] - picture.data[i];
552 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
554 for (i = 0; i < 4; i++) {
555 av_buffer_pool_uninit(&pool->pools[i]);
556 pool->linesize[i] = picture.linesize[i];
558 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
559 CONFIG_MEMORY_POISONING ?
562 if (!pool->pools[i]) {
563 ret = AVERROR(ENOMEM);
568 pool->format = frame->format;
569 pool->width = frame->width;
570 pool->height = frame->height;
574 case AVMEDIA_TYPE_AUDIO: {
575 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
576 int planar = av_sample_fmt_is_planar(frame->format);
577 int planes = planar ? ch : 1;
579 if (pool->format == frame->format && pool->planes == planes &&
580 pool->channels == ch && frame->nb_samples == pool->samples)
583 av_buffer_pool_uninit(&pool->pools[0]);
584 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
585 frame->nb_samples, frame->format, 0);
589 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
590 if (!pool->pools[0]) {
591 ret = AVERROR(ENOMEM);
595 pool->format = frame->format;
596 pool->planes = planes;
598 pool->samples = frame->nb_samples;
601 default: av_assert0(0);
605 for (i = 0; i < 4; i++)
606 av_buffer_pool_uninit(&pool->pools[i]);
608 pool->planes = pool->channels = pool->samples = 0;
609 pool->width = pool->height = 0;
613 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
615 FramePool *pool = avctx->internal->pool;
616 int planes = pool->planes;
619 frame->linesize[0] = pool->linesize[0];
621 if (planes > AV_NUM_DATA_POINTERS) {
622 frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
623 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
624 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
625 sizeof(*frame->extended_buf));
626 if (!frame->extended_data || !frame->extended_buf) {
627 av_freep(&frame->extended_data);
628 av_freep(&frame->extended_buf);
629 return AVERROR(ENOMEM);
632 frame->extended_data = frame->data;
633 av_assert0(frame->nb_extended_buf == 0);
636 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
637 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
640 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
642 for (i = 0; i < frame->nb_extended_buf; i++) {
643 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
644 if (!frame->extended_buf[i])
646 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
649 if (avctx->debug & FF_DEBUG_BUFFERS)
650 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
654 av_frame_unref(frame);
655 return AVERROR(ENOMEM);
658 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
660 FramePool *pool = s->internal->pool;
664 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
668 memset(pic->data, 0, sizeof(pic->data));
669 pic->extended_data = pic->data;
671 for (i = 0; i < 4 && pool->pools[i]; i++) {
672 pic->linesize[i] = pool->linesize[i];
674 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
678 pic->data[i] = pic->buf[i]->data;
680 for (; i < AV_NUM_DATA_POINTERS; i++) {
682 pic->linesize[i] = 0;
684 if (pic->data[1] && !pic->data[2])
685 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
687 if (s->debug & FF_DEBUG_BUFFERS)
688 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
693 return AVERROR(ENOMEM);
696 void avpriv_color_frame(AVFrame *frame, const int c[4])
698 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
701 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
703 for (p = 0; p<desc->nb_components; p++) {
704 uint8_t *dst = frame->data[p];
705 int is_chroma = p == 1 || p == 2;
706 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
707 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
708 for (y = 0; y < height; y++) {
709 if (desc->comp[0].depth_minus1 >= 8) {
710 for (x = 0; x<bytes; x++)
711 ((uint16_t*)dst)[x] = c[p];
713 memset(dst, c[p], bytes);
714 dst += frame->linesize[p];
719 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
723 if ((ret = update_frame_pool(avctx, frame)) < 0)
726 #if FF_API_GET_BUFFER
727 FF_DISABLE_DEPRECATION_WARNINGS
728 frame->type = FF_BUFFER_TYPE_INTERNAL;
729 FF_ENABLE_DEPRECATION_WARNINGS
732 switch (avctx->codec_type) {
733 case AVMEDIA_TYPE_VIDEO:
734 return video_get_buffer(avctx, frame);
735 case AVMEDIA_TYPE_AUDIO:
736 return audio_get_buffer(avctx, frame);
742 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
744 AVPacket *pkt = avctx->internal->pkt;
746 static const struct {
747 enum AVPacketSideDataType packet;
748 enum AVFrameSideDataType frame;
750 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
751 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
752 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
753 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
757 frame->pkt_pts = pkt->pts;
758 av_frame_set_pkt_pos (frame, pkt->pos);
759 av_frame_set_pkt_duration(frame, pkt->duration);
760 av_frame_set_pkt_size (frame, pkt->size);
762 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
764 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
766 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
770 return AVERROR(ENOMEM);
772 memcpy(frame_sd->data, packet_sd, size);
776 frame->pkt_pts = AV_NOPTS_VALUE;
777 av_frame_set_pkt_pos (frame, -1);
778 av_frame_set_pkt_duration(frame, 0);
779 av_frame_set_pkt_size (frame, -1);
781 frame->reordered_opaque = avctx->reordered_opaque;
783 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
784 frame->color_primaries = avctx->color_primaries;
785 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
786 frame->color_trc = avctx->color_trc;
787 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
788 av_frame_set_colorspace(frame, avctx->colorspace);
789 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
790 av_frame_set_color_range(frame, avctx->color_range);
791 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
792 frame->chroma_location = avctx->chroma_sample_location;
794 switch (avctx->codec->type) {
795 case AVMEDIA_TYPE_VIDEO:
796 frame->format = avctx->pix_fmt;
797 if (!frame->sample_aspect_ratio.num)
798 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
800 if (frame->width && frame->height &&
801 av_image_check_sar(frame->width, frame->height,
802 frame->sample_aspect_ratio) < 0) {
803 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
804 frame->sample_aspect_ratio.num,
805 frame->sample_aspect_ratio.den);
806 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
810 case AVMEDIA_TYPE_AUDIO:
811 if (!frame->sample_rate)
812 frame->sample_rate = avctx->sample_rate;
813 if (frame->format < 0)
814 frame->format = avctx->sample_fmt;
815 if (!frame->channel_layout) {
816 if (avctx->channel_layout) {
817 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
819 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
821 return AVERROR(EINVAL);
824 frame->channel_layout = avctx->channel_layout;
826 if (avctx->channels > FF_SANE_NB_CHANNELS) {
827 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
829 return AVERROR(ENOSYS);
833 av_frame_set_channels(frame, avctx->channels);
839 #if FF_API_GET_BUFFER
840 FF_DISABLE_DEPRECATION_WARNINGS
841 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
843 return avcodec_default_get_buffer2(avctx, frame, 0);
846 typedef struct CompatReleaseBufPriv {
847 AVCodecContext avctx;
849 uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
850 } CompatReleaseBufPriv;
852 static void compat_free_buffer(void *opaque, uint8_t *data)
854 CompatReleaseBufPriv *priv = opaque;
855 if (priv->avctx.release_buffer)
856 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
860 static void compat_release_buffer(void *opaque, uint8_t *data)
862 AVBufferRef *buf = opaque;
863 av_buffer_unref(&buf);
865 FF_ENABLE_DEPRECATION_WARNINGS
868 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
870 return ff_init_buffer_info(avctx, frame);
873 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
875 const AVHWAccel *hwaccel = avctx->hwaccel;
876 int override_dimensions = 1;
879 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
880 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
881 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
882 return AVERROR(EINVAL);
885 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
886 if (frame->width <= 0 || frame->height <= 0) {
887 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
888 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
889 override_dimensions = 0;
892 ret = ff_decode_frame_props(avctx, frame);
895 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
899 if (hwaccel->alloc_frame) {
900 ret = hwaccel->alloc_frame(avctx, frame);
904 avctx->sw_pix_fmt = avctx->pix_fmt;
906 #if FF_API_GET_BUFFER
907 FF_DISABLE_DEPRECATION_WARNINGS
909 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
910 * We wrap each plane in its own AVBuffer. Each of those has a reference to
911 * a dummy AVBuffer as its private data, unreffing it on free.
912 * When all the planes are freed, the dummy buffer's free callback calls
915 if (avctx->get_buffer) {
916 CompatReleaseBufPriv *priv = NULL;
917 AVBufferRef *dummy_buf = NULL;
920 if (flags & AV_GET_BUFFER_FLAG_REF)
921 frame->reference = 1;
923 ret = avctx->get_buffer(avctx, frame);
927 /* return if the buffers are already set up
928 * this would happen e.g. when a custom get_buffer() calls
929 * avcodec_default_get_buffer
934 priv = av_mallocz(sizeof(*priv));
936 ret = AVERROR(ENOMEM);
939 priv->avctx = *avctx;
940 priv->frame = *frame;
942 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
944 ret = AVERROR(ENOMEM);
948 #define WRAP_PLANE(ref_out, data, data_size) \
950 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
952 ret = AVERROR(ENOMEM); \
955 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
958 av_buffer_unref(&dummy_ref); \
959 av_frame_unref(frame); \
960 ret = AVERROR(ENOMEM); \
965 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
966 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
968 planes = av_pix_fmt_count_planes(frame->format);
969 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
970 check for allocated buffers: make libavcodec happy */
971 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
973 if (!desc || planes <= 0) {
974 ret = AVERROR(EINVAL);
978 for (i = 0; i < planes; i++) {
979 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
980 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
982 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
985 int planar = av_sample_fmt_is_planar(frame->format);
986 planes = planar ? avctx->channels : 1;
988 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
989 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
990 frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
991 frame->nb_extended_buf);
992 if (!frame->extended_buf) {
993 ret = AVERROR(ENOMEM);
998 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
999 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
1001 for (i = 0; i < frame->nb_extended_buf; i++)
1002 WRAP_PLANE(frame->extended_buf[i],
1003 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
1004 frame->linesize[0]);
1007 av_buffer_unref(&dummy_buf);
1010 frame->width = avctx->width;
1011 frame->height = avctx->height;
1016 avctx->release_buffer(avctx, frame);
1018 av_buffer_unref(&dummy_buf);
1021 FF_ENABLE_DEPRECATION_WARNINGS
1024 ret = avctx->get_buffer2(avctx, frame, flags);
1027 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
1028 frame->width = avctx->width;
1029 frame->height = avctx->height;
1035 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1037 int ret = get_buffer_internal(avctx, frame, flags);
1039 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1043 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
1048 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1050 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1051 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1052 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1053 av_frame_unref(frame);
1056 ff_init_buffer_info(avctx, frame);
1058 if (!frame->data[0])
1059 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1061 if (av_frame_is_writable(frame))
1062 return ff_decode_frame_props(avctx, frame);
1064 tmp = av_frame_alloc();
1066 return AVERROR(ENOMEM);
1068 av_frame_move_ref(tmp, frame);
1070 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1072 av_frame_free(&tmp);
1076 av_frame_copy(frame, tmp);
1077 av_frame_free(&tmp);
1082 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1084 int ret = reget_buffer_internal(avctx, frame);
1086 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1090 #if FF_API_GET_BUFFER
1091 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
1093 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
1095 av_frame_unref(pic);
1098 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1105 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1109 for (i = 0; i < count; i++) {
1110 int r = func(c, (char *)arg + i * size);
1117 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1121 for (i = 0; i < count; i++) {
1122 int r = func(c, arg, i, 0);
1129 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
1130 unsigned int fourcc)
1132 while (tags->pix_fmt >= 0) {
1133 if (tags->fourcc == fourcc)
1134 return tags->pix_fmt;
1137 return AV_PIX_FMT_NONE;
1140 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1142 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1143 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1146 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1148 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1153 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
1154 enum AVPixelFormat pix_fmt)
1156 AVHWAccel *hwaccel = NULL;
1158 while ((hwaccel = av_hwaccel_next(hwaccel)))
1159 if (hwaccel->id == codec_id
1160 && hwaccel->pix_fmt == pix_fmt)
1165 static int setup_hwaccel(AVCodecContext *avctx,
1166 const enum AVPixelFormat fmt,
1169 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
1173 av_log(avctx, AV_LOG_ERROR,
1174 "Could not find an AVHWAccel for the pixel format: %s",
1176 return AVERROR(ENOENT);
1179 if (hwa->priv_data_size) {
1180 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
1181 if (!avctx->internal->hwaccel_priv_data)
1182 return AVERROR(ENOMEM);
1186 ret = hwa->init(avctx);
1188 av_freep(&avctx->internal->hwaccel_priv_data);
1193 avctx->hwaccel = hwa;
1198 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1200 const AVPixFmtDescriptor *desc;
1201 enum AVPixelFormat *choices;
1202 enum AVPixelFormat ret;
1205 while (fmt[n] != AV_PIX_FMT_NONE)
1209 avctx->sw_pix_fmt = fmt[n - 1];
1210 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
1212 choices = av_malloc_array(n + 1, sizeof(*choices));
1214 return AV_PIX_FMT_NONE;
1216 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
1219 if (avctx->hwaccel && avctx->hwaccel->uninit)
1220 avctx->hwaccel->uninit(avctx);
1221 av_freep(&avctx->internal->hwaccel_priv_data);
1222 avctx->hwaccel = NULL;
1224 ret = avctx->get_format(avctx, choices);
1226 desc = av_pix_fmt_desc_get(ret);
1228 ret = AV_PIX_FMT_NONE;
1232 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1234 if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
1237 if (!setup_hwaccel(avctx, ret, desc->name))
1240 /* Remove failed hwaccel from choices */
1241 for (n = 0; choices[n] != ret; n++)
1242 av_assert0(choices[n] != AV_PIX_FMT_NONE);
1245 choices[n] = choices[n + 1];
1246 while (choices[n++] != AV_PIX_FMT_NONE);
1253 #if FF_API_AVFRAME_LAVC
1254 void avcodec_get_frame_defaults(AVFrame *frame)
1256 #if LIBAVCODEC_VERSION_MAJOR >= 55
1257 // extended_data should explicitly be freed when needed, this code is unsafe currently
1258 // also this is not compatible to the <55 ABI/API
1259 if (frame->extended_data != frame->data && 0)
1260 av_freep(&frame->extended_data);
1263 memset(frame, 0, sizeof(AVFrame));
1264 av_frame_unref(frame);
1267 AVFrame *avcodec_alloc_frame(void)
1269 return av_frame_alloc();
1272 void avcodec_free_frame(AVFrame **frame)
1274 av_frame_free(frame);
1278 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1279 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1280 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1281 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1282 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1284 int av_codec_get_max_lowres(const AVCodec *codec)
1286 return codec->max_lowres;
1289 static void get_subtitle_defaults(AVSubtitle *sub)
1291 memset(sub, 0, sizeof(*sub));
1292 sub->pts = AV_NOPTS_VALUE;
1295 static int get_bit_rate(AVCodecContext *ctx)
1298 int bits_per_sample;
1300 switch (ctx->codec_type) {
1301 case AVMEDIA_TYPE_VIDEO:
1302 case AVMEDIA_TYPE_DATA:
1303 case AVMEDIA_TYPE_SUBTITLE:
1304 case AVMEDIA_TYPE_ATTACHMENT:
1305 bit_rate = ctx->bit_rate;
1307 case AVMEDIA_TYPE_AUDIO:
1308 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1309 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1318 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1322 ff_unlock_avcodec();
1324 ret = avcodec_open2(avctx, codec, options);
1326 ff_lock_avcodec(avctx, codec);
1330 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1333 AVDictionary *tmp = NULL;
1335 if (avcodec_is_open(avctx))
1338 if ((!codec && !avctx->codec)) {
1339 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1340 return AVERROR(EINVAL);
1342 if ((codec && avctx->codec && codec != avctx->codec)) {
1343 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1344 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1345 return AVERROR(EINVAL);
1348 codec = avctx->codec;
1350 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1351 return AVERROR(EINVAL);
1354 av_dict_copy(&tmp, *options, 0);
1356 ret = ff_lock_avcodec(avctx, codec);
1360 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1361 if (!avctx->internal) {
1362 ret = AVERROR(ENOMEM);
1366 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1367 if (!avctx->internal->pool) {
1368 ret = AVERROR(ENOMEM);
1372 avctx->internal->to_free = av_frame_alloc();
1373 if (!avctx->internal->to_free) {
1374 ret = AVERROR(ENOMEM);
1378 if (codec->priv_data_size > 0) {
1379 if (!avctx->priv_data) {
1380 avctx->priv_data = av_mallocz(codec->priv_data_size);
1381 if (!avctx->priv_data) {
1382 ret = AVERROR(ENOMEM);
1385 if (codec->priv_class) {
1386 *(const AVClass **)avctx->priv_data = codec->priv_class;
1387 av_opt_set_defaults(avctx->priv_data);
1390 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1393 avctx->priv_data = NULL;
1395 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1398 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
1399 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist\n", codec->name);
1400 ret = AVERROR(EINVAL);
1404 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1405 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1406 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1407 if (avctx->coded_width && avctx->coded_height)
1408 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1409 else if (avctx->width && avctx->height)
1410 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1415 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1416 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1417 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1418 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1419 ff_set_dimensions(avctx, 0, 0);
1422 if (avctx->width > 0 && avctx->height > 0) {
1423 if (av_image_check_sar(avctx->width, avctx->height,
1424 avctx->sample_aspect_ratio) < 0) {
1425 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1426 avctx->sample_aspect_ratio.num,
1427 avctx->sample_aspect_ratio.den);
1428 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1432 /* if the decoder init function was already called previously,
1433 * free the already allocated subtitle_header before overwriting it */
1434 if (av_codec_is_decoder(codec))
1435 av_freep(&avctx->subtitle_header);
1437 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1438 ret = AVERROR(EINVAL);
1442 avctx->codec = codec;
1443 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1444 avctx->codec_id == AV_CODEC_ID_NONE) {
1445 avctx->codec_type = codec->type;
1446 avctx->codec_id = codec->id;
1448 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1449 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1450 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1451 ret = AVERROR(EINVAL);
1454 avctx->frame_number = 0;
1455 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1457 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1458 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1459 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1461 av_log(avctx, AV_LOG_ERROR,
1462 "The %s '%s' is experimental but experimental codecs are not enabled, "
1463 "add '-strict %d' if you want to use it.\n",
1464 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1465 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1466 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1467 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1468 codec_string, codec2->name);
1469 ret = AVERROR_EXPERIMENTAL;
1473 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1474 (!avctx->time_base.num || !avctx->time_base.den)) {
1475 avctx->time_base.num = 1;
1476 avctx->time_base.den = avctx->sample_rate;
1480 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1482 if (CONFIG_FRAME_THREAD_ENCODER) {
1483 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1484 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1485 ff_lock_avcodec(avctx, codec);
1491 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1492 ret = ff_thread_init(avctx);
1497 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1498 avctx->thread_count = 1;
1500 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1501 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1502 avctx->codec->max_lowres);
1503 ret = AVERROR(EINVAL);
1508 if (avctx->debug_mv)
1509 av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
1510 "see the codecview filter instead.\n");
1513 if (av_codec_is_encoder(avctx->codec)) {
1515 if (avctx->codec->sample_fmts) {
1516 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1517 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1519 if (avctx->channels == 1 &&
1520 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1521 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1522 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1526 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1528 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1529 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1530 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1531 ret = AVERROR(EINVAL);
1535 if (avctx->codec->pix_fmts) {
1536 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1537 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1539 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1540 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1541 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1543 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1544 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1545 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1546 ret = AVERROR(EINVAL);
1549 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1550 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
1551 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1552 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1553 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1554 avctx->color_range = AVCOL_RANGE_JPEG;
1556 if (avctx->codec->supported_samplerates) {
1557 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1558 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1560 if (avctx->codec->supported_samplerates[i] == 0) {
1561 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1562 avctx->sample_rate);
1563 ret = AVERROR(EINVAL);
1567 if (avctx->codec->channel_layouts) {
1568 if (!avctx->channel_layout) {
1569 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1571 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1572 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1574 if (avctx->codec->channel_layouts[i] == 0) {
1576 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1577 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1578 ret = AVERROR(EINVAL);
1583 if (avctx->channel_layout && avctx->channels) {
1584 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1585 if (channels != avctx->channels) {
1587 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1588 av_log(avctx, AV_LOG_ERROR,
1589 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1590 buf, channels, avctx->channels);
1591 ret = AVERROR(EINVAL);
1594 } else if (avctx->channel_layout) {
1595 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1597 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1598 if (avctx->width <= 0 || avctx->height <= 0) {
1599 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1600 ret = AVERROR(EINVAL);
1604 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1605 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1606 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1609 if (!avctx->rc_initial_buffer_occupancy)
1610 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1613 avctx->pts_correction_num_faulty_pts =
1614 avctx->pts_correction_num_faulty_dts = 0;
1615 avctx->pts_correction_last_pts =
1616 avctx->pts_correction_last_dts = INT64_MIN;
1618 if ( !CONFIG_GRAY && avctx->flags & CODEC_FLAG_GRAY
1619 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
1620 av_log(avctx, AV_LOG_WARNING,
1621 "gray decoding requested but not enabled at configuration time\n");
1623 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1624 || avctx->internal->frame_thread_encoder)) {
1625 ret = avctx->codec->init(avctx);
1633 #if FF_API_AUDIOENC_DELAY
1634 if (av_codec_is_encoder(avctx->codec))
1635 avctx->delay = avctx->initial_padding;
1638 if (av_codec_is_decoder(avctx->codec)) {
1639 if (!avctx->bit_rate)
1640 avctx->bit_rate = get_bit_rate(avctx);
1641 /* validate channel layout from the decoder */
1642 if (avctx->channel_layout) {
1643 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1644 if (!avctx->channels)
1645 avctx->channels = channels;
1646 else if (channels != avctx->channels) {
1648 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1649 av_log(avctx, AV_LOG_WARNING,
1650 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1651 "ignoring specified channel layout\n",
1652 buf, channels, avctx->channels);
1653 avctx->channel_layout = 0;
1656 if (avctx->channels && avctx->channels < 0 ||
1657 avctx->channels > FF_SANE_NB_CHANNELS) {
1658 ret = AVERROR(EINVAL);
1661 if (avctx->sub_charenc) {
1662 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1663 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1664 "supported with subtitles codecs\n");
1665 ret = AVERROR(EINVAL);
1667 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1668 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1669 "subtitles character encoding will be ignored\n",
1670 avctx->codec_descriptor->name);
1671 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1673 /* input character encoding is set for a text based subtitle
1674 * codec at this point */
1675 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1676 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1678 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1680 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1681 if (cd == (iconv_t)-1) {
1682 ret = AVERROR(errno);
1683 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1684 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1689 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1690 "conversion needs a libavcodec built with iconv support "
1691 "for this codec\n");
1692 ret = AVERROR(ENOSYS);
1699 #if FF_API_AVCTX_TIMEBASE
1700 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1701 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
1704 if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
1705 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
1709 ff_unlock_avcodec();
1711 av_dict_free(options);
1718 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1719 avctx->codec->close(avctx);
1721 if (codec->priv_class && codec->priv_data_size)
1722 av_opt_free(avctx->priv_data);
1726 av_freep(&avctx->priv_data);
1727 if (avctx->internal) {
1728 av_frame_free(&avctx->internal->to_free);
1729 av_freep(&avctx->internal->pool);
1731 av_freep(&avctx->internal);
1732 avctx->codec = NULL;
1736 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1738 if (avpkt->size < 0) {
1739 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1740 return AVERROR(EINVAL);
1742 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1743 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1744 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1745 return AVERROR(EINVAL);
1749 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1750 if (!avpkt->data || avpkt->size < size) {
1751 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1752 avpkt->data = avctx->internal->byte_buffer;
1753 avpkt->size = avctx->internal->byte_buffer_size;
1754 #if FF_API_DESTRUCT_PACKET
1755 FF_DISABLE_DEPRECATION_WARNINGS
1756 avpkt->destruct = NULL;
1757 FF_ENABLE_DEPRECATION_WARNINGS
1763 AVBufferRef *buf = avpkt->buf;
1764 #if FF_API_DESTRUCT_PACKET
1765 FF_DISABLE_DEPRECATION_WARNINGS
1766 void *destruct = avpkt->destruct;
1767 FF_ENABLE_DEPRECATION_WARNINGS
1770 if (avpkt->size < size) {
1771 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1772 return AVERROR(EINVAL);
1775 av_init_packet(avpkt);
1776 #if FF_API_DESTRUCT_PACKET
1777 FF_DISABLE_DEPRECATION_WARNINGS
1778 avpkt->destruct = destruct;
1779 FF_ENABLE_DEPRECATION_WARNINGS
1785 int ret = av_new_packet(avpkt, size);
1787 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1792 int ff_alloc_packet(AVPacket *avpkt, int size)
1794 return ff_alloc_packet2(NULL, avpkt, size);
1798 * Pad last frame with silence.
1800 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1802 AVFrame *frame = NULL;
1805 if (!(frame = av_frame_alloc()))
1806 return AVERROR(ENOMEM);
1808 frame->format = src->format;
1809 frame->channel_layout = src->channel_layout;
1810 av_frame_set_channels(frame, av_frame_get_channels(src));
1811 frame->nb_samples = s->frame_size;
1812 ret = av_frame_get_buffer(frame, 32);
1816 ret = av_frame_copy_props(frame, src);
1820 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1821 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1823 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1824 frame->nb_samples - src->nb_samples,
1825 s->channels, s->sample_fmt)) < 0)
1833 av_frame_free(&frame);
1837 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1839 const AVFrame *frame,
1840 int *got_packet_ptr)
1842 AVFrame *extended_frame = NULL;
1843 AVFrame *padded_frame = NULL;
1845 AVPacket user_pkt = *avpkt;
1846 int needs_realloc = !user_pkt.data;
1848 *got_packet_ptr = 0;
1850 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1851 av_free_packet(avpkt);
1852 av_init_packet(avpkt);
1856 /* ensure that extended_data is properly set */
1857 if (frame && !frame->extended_data) {
1858 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1859 avctx->channels > AV_NUM_DATA_POINTERS) {
1860 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1861 "with more than %d channels, but extended_data is not set.\n",
1862 AV_NUM_DATA_POINTERS);
1863 return AVERROR(EINVAL);
1865 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1867 extended_frame = av_frame_alloc();
1868 if (!extended_frame)
1869 return AVERROR(ENOMEM);
1871 memcpy(extended_frame, frame, sizeof(AVFrame));
1872 extended_frame->extended_data = extended_frame->data;
1873 frame = extended_frame;
1876 /* extract audio service type metadata */
1878 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
1879 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1880 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1883 /* check for valid frame size */
1885 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1886 if (frame->nb_samples > avctx->frame_size) {
1887 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1888 ret = AVERROR(EINVAL);
1891 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1892 if (frame->nb_samples < avctx->frame_size &&
1893 !avctx->internal->last_audio_frame) {
1894 ret = pad_last_frame(avctx, &padded_frame, frame);
1898 frame = padded_frame;
1899 avctx->internal->last_audio_frame = 1;
1902 if (frame->nb_samples != avctx->frame_size) {
1903 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1904 ret = AVERROR(EINVAL);
1910 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1912 if (*got_packet_ptr) {
1913 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1914 if (avpkt->pts == AV_NOPTS_VALUE)
1915 avpkt->pts = frame->pts;
1916 if (!avpkt->duration)
1917 avpkt->duration = ff_samples_to_time_base(avctx,
1920 avpkt->dts = avpkt->pts;
1925 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1927 if (user_pkt.data) {
1928 if (user_pkt.size >= avpkt->size) {
1929 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1931 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1932 avpkt->size = user_pkt.size;
1935 avpkt->buf = user_pkt.buf;
1936 avpkt->data = user_pkt.data;
1937 #if FF_API_DESTRUCT_PACKET
1938 FF_DISABLE_DEPRECATION_WARNINGS
1939 avpkt->destruct = user_pkt.destruct;
1940 FF_ENABLE_DEPRECATION_WARNINGS
1943 if (av_dup_packet(avpkt) < 0) {
1944 ret = AVERROR(ENOMEM);
1950 if (needs_realloc && avpkt->data) {
1951 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1953 avpkt->data = avpkt->buf->data;
1956 avctx->frame_number++;
1959 if (ret < 0 || !*got_packet_ptr) {
1960 av_free_packet(avpkt);
1961 av_init_packet(avpkt);
1965 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1966 * this needs to be moved to the encoders, but for now we can do it
1967 * here to simplify things */
1968 avpkt->flags |= AV_PKT_FLAG_KEY;
1971 av_frame_free(&padded_frame);
1972 av_free(extended_frame);
1974 #if FF_API_AUDIOENC_DELAY
1975 avctx->delay = avctx->initial_padding;
1981 #if FF_API_OLD_ENCODE_AUDIO
1982 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1983 uint8_t *buf, int buf_size,
1984 const short *samples)
1988 int ret, samples_size, got_packet;
1990 av_init_packet(&pkt);
1992 pkt.size = buf_size;
1995 frame = av_frame_alloc();
1997 return AVERROR(ENOMEM);
1999 if (avctx->frame_size) {
2000 frame->nb_samples = avctx->frame_size;
2002 /* if frame_size is not set, the number of samples must be
2003 * calculated from the buffer size */
2005 if (!av_get_bits_per_sample(avctx->codec_id)) {
2006 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
2007 "support this codec\n");
2008 av_frame_free(&frame);
2009 return AVERROR(EINVAL);
2011 nb_samples = (int64_t)buf_size * 8 /
2012 (av_get_bits_per_sample(avctx->codec_id) *
2014 if (nb_samples >= INT_MAX) {
2015 av_frame_free(&frame);
2016 return AVERROR(EINVAL);
2018 frame->nb_samples = nb_samples;
2021 /* it is assumed that the samples buffer is large enough based on the
2022 * relevant parameters */
2023 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
2025 avctx->sample_fmt, 1);
2026 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
2028 (const uint8_t *)samples,
2029 samples_size, 1)) < 0) {
2030 av_frame_free(&frame);
2034 /* fabricate frame pts from sample count.
2035 * this is needed because the avcodec_encode_audio() API does not have
2036 * a way for the user to provide pts */
2037 if (avctx->sample_rate && avctx->time_base.num)
2038 frame->pts = ff_samples_to_time_base(avctx,
2039 avctx->internal->sample_count);
2041 frame->pts = AV_NOPTS_VALUE;
2042 avctx->internal->sample_count += frame->nb_samples;
2048 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
2049 if (!ret && got_packet && avctx->coded_frame) {
2050 avctx->coded_frame->pts = pkt.pts;
2051 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2053 /* free any side data since we cannot return it */
2054 av_packet_free_side_data(&pkt);
2056 if (frame && frame->extended_data != frame->data)
2057 av_freep(&frame->extended_data);
2059 av_frame_free(&frame);
2060 return ret ? ret : pkt.size;
2065 #if FF_API_OLD_ENCODE_VIDEO
2066 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
2067 const AVFrame *pict)
2070 int ret, got_packet = 0;
2072 if (buf_size < FF_MIN_BUFFER_SIZE) {
2073 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
2077 av_init_packet(&pkt);
2079 pkt.size = buf_size;
2081 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
2082 if (!ret && got_packet && avctx->coded_frame) {
2083 avctx->coded_frame->pts = pkt.pts;
2084 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2087 /* free any side data since we cannot return it */
2088 if (pkt.side_data_elems > 0) {
2090 for (i = 0; i < pkt.side_data_elems; i++)
2091 av_free(pkt.side_data[i].data);
2092 av_freep(&pkt.side_data);
2093 pkt.side_data_elems = 0;
2096 return ret ? ret : pkt.size;
2101 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
2103 const AVFrame *frame,
2104 int *got_packet_ptr)
2107 AVPacket user_pkt = *avpkt;
2108 int needs_realloc = !user_pkt.data;
2110 *got_packet_ptr = 0;
2112 if(CONFIG_FRAME_THREAD_ENCODER &&
2113 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
2114 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
2116 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
2117 avctx->stats_out[0] = '\0';
2119 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
2120 av_free_packet(avpkt);
2121 av_init_packet(avpkt);
2126 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
2127 return AVERROR(EINVAL);
2129 if (frame && frame->format == AV_PIX_FMT_NONE)
2130 av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
2131 if (frame && (frame->width == 0 || frame->height == 0))
2132 av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
2134 av_assert0(avctx->codec->encode2);
2136 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
2137 av_assert0(ret <= 0);
2139 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
2141 if (user_pkt.data) {
2142 if (user_pkt.size >= avpkt->size) {
2143 memcpy(user_pkt.data, avpkt->data, avpkt->size);
2145 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
2146 avpkt->size = user_pkt.size;
2149 avpkt->buf = user_pkt.buf;
2150 avpkt->data = user_pkt.data;
2151 #if FF_API_DESTRUCT_PACKET
2152 FF_DISABLE_DEPRECATION_WARNINGS
2153 avpkt->destruct = user_pkt.destruct;
2154 FF_ENABLE_DEPRECATION_WARNINGS
2157 if (av_dup_packet(avpkt) < 0) {
2158 ret = AVERROR(ENOMEM);
2164 if (!*got_packet_ptr)
2166 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
2167 avpkt->pts = avpkt->dts = frame->pts;
2169 if (needs_realloc && avpkt->data) {
2170 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
2172 avpkt->data = avpkt->buf->data;
2175 avctx->frame_number++;
2178 if (ret < 0 || !*got_packet_ptr)
2179 av_free_packet(avpkt);
2181 av_packet_merge_side_data(avpkt);
2187 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
2188 const AVSubtitle *sub)
2191 if (sub->start_display_time) {
2192 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
2196 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
2197 avctx->frame_number++;
2202 * Attempt to guess proper monotonic timestamps for decoded video frames
2203 * which might have incorrect times. Input timestamps may wrap around, in
2204 * which case the output will as well.
2206 * @param pts the pts field of the decoded AVPacket, as passed through
2208 * @param dts the dts field of the decoded AVPacket
2209 * @return one of the input values, may be AV_NOPTS_VALUE
2211 static int64_t guess_correct_pts(AVCodecContext *ctx,
2212 int64_t reordered_pts, int64_t dts)
2214 int64_t pts = AV_NOPTS_VALUE;
2216 if (dts != AV_NOPTS_VALUE) {
2217 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
2218 ctx->pts_correction_last_dts = dts;
2219 } else if (reordered_pts != AV_NOPTS_VALUE)
2220 ctx->pts_correction_last_dts = reordered_pts;
2222 if (reordered_pts != AV_NOPTS_VALUE) {
2223 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
2224 ctx->pts_correction_last_pts = reordered_pts;
2225 } else if(dts != AV_NOPTS_VALUE)
2226 ctx->pts_correction_last_pts = dts;
2228 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
2229 && reordered_pts != AV_NOPTS_VALUE)
2230 pts = reordered_pts;
2237 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
2240 const uint8_t *data;
2243 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
2247 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
2248 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
2249 "changes, but PARAM_CHANGE side data was sent to it.\n");
2250 return AVERROR(EINVAL);
2256 flags = bytestream_get_le32(&data);
2259 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
2262 avctx->channels = bytestream_get_le32(&data);
2265 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
2268 avctx->channel_layout = bytestream_get_le64(&data);
2271 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
2274 avctx->sample_rate = bytestream_get_le32(&data);
2277 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
2280 avctx->width = bytestream_get_le32(&data);
2281 avctx->height = bytestream_get_le32(&data);
2283 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2290 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2291 return AVERROR_INVALIDDATA;
2294 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
2297 const uint8_t *side_metadata;
2299 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2301 side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2302 AV_PKT_DATA_STRINGS_METADATA, &size);
2303 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2306 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2310 /* move the original frame to our backup */
2311 av_frame_unref(avci->to_free);
2312 av_frame_move_ref(avci->to_free, frame);
2314 /* now copy everything except the AVBufferRefs back
2315 * note that we make a COPY of the side data, so calling av_frame_free() on
2316 * the caller's frame will work properly */
2317 ret = av_frame_copy_props(frame, avci->to_free);
2321 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2322 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2323 if (avci->to_free->extended_data != avci->to_free->data) {
2324 int planes = av_frame_get_channels(avci->to_free);
2325 int size = planes * sizeof(*frame->extended_data);
2328 av_frame_unref(frame);
2332 frame->extended_data = av_malloc(size);
2333 if (!frame->extended_data) {
2334 av_frame_unref(frame);
2335 return AVERROR(ENOMEM);
2337 memcpy(frame->extended_data, avci->to_free->extended_data,
2340 frame->extended_data = frame->data;
2342 frame->format = avci->to_free->format;
2343 frame->width = avci->to_free->width;
2344 frame->height = avci->to_free->height;
2345 frame->channel_layout = avci->to_free->channel_layout;
2346 frame->nb_samples = avci->to_free->nb_samples;
2347 av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
2352 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2353 int *got_picture_ptr,
2354 const AVPacket *avpkt)
2356 AVCodecInternal *avci = avctx->internal;
2358 // copy to ensure we do not change avpkt
2359 AVPacket tmp = *avpkt;
2362 return AVERROR(EINVAL);
2363 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2364 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2365 return AVERROR(EINVAL);
2368 *got_picture_ptr = 0;
2369 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2370 return AVERROR(EINVAL);
2372 av_frame_unref(picture);
2374 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2375 int did_split = av_packet_split_side_data(&tmp);
2376 ret = apply_param_change(avctx, &tmp);
2378 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2379 if (avctx->err_recognition & AV_EF_EXPLODE)
2383 avctx->internal->pkt = &tmp;
2384 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2385 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2388 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2390 picture->pkt_dts = avpkt->dts;
2392 if(!avctx->has_b_frames){
2393 av_frame_set_pkt_pos(picture, avpkt->pos);
2395 //FIXME these should be under if(!avctx->has_b_frames)
2396 /* get_buffer is supposed to set frame parameters */
2397 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2398 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2399 if (!picture->width) picture->width = avctx->width;
2400 if (!picture->height) picture->height = avctx->height;
2401 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2404 add_metadata_from_side_data(avctx, picture);
2407 emms_c(); //needed to avoid an emms_c() call before every return;
2409 avctx->internal->pkt = NULL;
2411 av_packet_free_side_data(&tmp);
2416 if (*got_picture_ptr) {
2417 if (!avctx->refcounted_frames) {
2418 int err = unrefcount_frame(avci, picture);
2423 avctx->frame_number++;
2424 av_frame_set_best_effort_timestamp(picture,
2425 guess_correct_pts(avctx,
2429 av_frame_unref(picture);
2433 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2434 * make sure it's set correctly */
2435 av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2437 #if FF_API_AVCTX_TIMEBASE
2438 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
2439 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
2445 #if FF_API_OLD_DECODE_AUDIO
2446 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2447 int *frame_size_ptr,
2450 AVFrame *frame = av_frame_alloc();
2451 int ret, got_frame = 0;
2454 return AVERROR(ENOMEM);
2455 if (avctx->get_buffer != avcodec_default_get_buffer) {
2456 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2457 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2458 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2459 "avcodec_decode_audio4()\n");
2460 avctx->get_buffer = avcodec_default_get_buffer;
2461 avctx->release_buffer = avcodec_default_release_buffer;
2464 ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2466 if (ret >= 0 && got_frame) {
2468 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2469 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2471 avctx->sample_fmt, 1);
2472 if (*frame_size_ptr < data_size) {
2473 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2474 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2475 av_frame_free(&frame);
2476 return AVERROR(EINVAL);
2479 memcpy(samples, frame->extended_data[0], plane_size);
2481 if (planar && avctx->channels > 1) {
2482 uint8_t *out = ((uint8_t *)samples) + plane_size;
2483 for (ch = 1; ch < avctx->channels; ch++) {
2484 memcpy(out, frame->extended_data[ch], plane_size);
2488 *frame_size_ptr = data_size;
2490 *frame_size_ptr = 0;
2492 av_frame_free(&frame);
2498 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2501 const AVPacket *avpkt)
2503 AVCodecInternal *avci = avctx->internal;
2508 if (!avpkt->data && avpkt->size) {
2509 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2510 return AVERROR(EINVAL);
2513 return AVERROR(EINVAL);
2514 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2515 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2516 return AVERROR(EINVAL);
2519 av_frame_unref(frame);
2521 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2524 uint32_t discard_padding = 0;
2525 uint8_t skip_reason = 0;
2526 uint8_t discard_reason = 0;
2527 // copy to ensure we do not change avpkt
2528 AVPacket tmp = *avpkt;
2529 int did_split = av_packet_split_side_data(&tmp);
2530 ret = apply_param_change(avctx, &tmp);
2532 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2533 if (avctx->err_recognition & AV_EF_EXPLODE)
2537 avctx->internal->pkt = &tmp;
2538 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2539 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2541 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2542 av_assert0(ret <= tmp.size);
2543 frame->pkt_dts = avpkt->dts;
2545 if (ret >= 0 && *got_frame_ptr) {
2546 add_metadata_from_side_data(avctx, frame);
2547 avctx->frame_number++;
2548 av_frame_set_best_effort_timestamp(frame,
2549 guess_correct_pts(avctx,
2552 if (frame->format == AV_SAMPLE_FMT_NONE)
2553 frame->format = avctx->sample_fmt;
2554 if (!frame->channel_layout)
2555 frame->channel_layout = avctx->channel_layout;
2556 if (!av_frame_get_channels(frame))
2557 av_frame_set_channels(frame, avctx->channels);
2558 if (!frame->sample_rate)
2559 frame->sample_rate = avctx->sample_rate;
2562 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2563 if(side && side_size>=10) {
2564 avctx->internal->skip_samples = AV_RL32(side);
2565 discard_padding = AV_RL32(side + 4);
2566 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
2567 avctx->internal->skip_samples, (int)discard_padding);
2568 skip_reason = AV_RL8(side + 8);
2569 discard_reason = AV_RL8(side + 9);
2571 if (avctx->internal->skip_samples && *got_frame_ptr &&
2572 !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
2573 if(frame->nb_samples <= avctx->internal->skip_samples){
2575 avctx->internal->skip_samples -= frame->nb_samples;
2576 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2577 avctx->internal->skip_samples);
2579 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2580 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2581 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2582 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2583 (AVRational){1, avctx->sample_rate},
2584 avctx->pkt_timebase);
2585 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2586 frame->pkt_pts += diff_ts;
2587 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2588 frame->pkt_dts += diff_ts;
2589 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2590 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2592 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2594 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2595 avctx->internal->skip_samples, frame->nb_samples);
2596 frame->nb_samples -= avctx->internal->skip_samples;
2597 avctx->internal->skip_samples = 0;
2601 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
2602 !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
2603 if (discard_padding == frame->nb_samples) {
2606 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2607 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2608 (AVRational){1, avctx->sample_rate},
2609 avctx->pkt_timebase);
2610 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2611 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2613 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2615 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2616 (int)discard_padding, frame->nb_samples);
2617 frame->nb_samples -= discard_padding;
2621 if ((avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
2622 AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
2624 AV_WL32(fside->data, avctx->internal->skip_samples);
2625 AV_WL32(fside->data + 4, discard_padding);
2626 AV_WL8(fside->data + 8, skip_reason);
2627 AV_WL8(fside->data + 9, discard_reason);
2628 avctx->internal->skip_samples = 0;
2632 avctx->internal->pkt = NULL;
2634 av_packet_free_side_data(&tmp);
2639 if (ret >= 0 && *got_frame_ptr) {
2640 if (!avctx->refcounted_frames) {
2641 int err = unrefcount_frame(avci, frame);
2646 av_frame_unref(frame);
2652 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2653 static int recode_subtitle(AVCodecContext *avctx,
2654 AVPacket *outpkt, const AVPacket *inpkt)
2657 iconv_t cd = (iconv_t)-1;
2664 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2668 cd = iconv_open("UTF-8", avctx->sub_charenc);
2669 av_assert0(cd != (iconv_t)-1);
2674 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2675 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2676 ret = AVERROR(ENOMEM);
2680 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2683 outpkt->buf = tmp.buf;
2684 outpkt->data = tmp.data;
2685 outpkt->size = tmp.size;
2686 outb = outpkt->data;
2687 outl = outpkt->size;
2689 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2690 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2691 outl >= outpkt->size || inl != 0) {
2692 ret = FFMIN(AVERROR(errno), -1);
2693 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2694 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2695 av_free_packet(&tmp);
2698 outpkt->size -= outl;
2699 memset(outpkt->data + outpkt->size, 0, outl);
2702 if (cd != (iconv_t)-1)
2706 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
2707 return AVERROR(EINVAL);
2711 static int utf8_check(const uint8_t *str)
2713 const uint8_t *byte;
2714 uint32_t codepoint, min;
2718 GET_UTF8(codepoint, *(byte++), return 0;);
2719 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2720 1 << (5 * (byte - str) - 4);
2721 if (codepoint < min || codepoint >= 0x110000 ||
2722 codepoint == 0xFFFE /* BOM */ ||
2723 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2730 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2736 if (!avpkt->data && avpkt->size) {
2737 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2738 return AVERROR(EINVAL);
2741 return AVERROR(EINVAL);
2742 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2743 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2744 return AVERROR(EINVAL);
2748 get_subtitle_defaults(sub);
2750 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2751 AVPacket pkt_recoded;
2752 AVPacket tmp = *avpkt;
2753 int did_split = av_packet_split_side_data(&tmp);
2754 //apply_param_change(avctx, &tmp);
2757 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2759 * If the side data is smaller than the buffer padding size, the
2760 * remaining bytes should have already been filled with zeros by the
2761 * original packet allocation anyway. */
2762 memset(tmp.data + tmp.size, 0,
2763 FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2767 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2771 avctx->internal->pkt = &pkt_recoded;
2773 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2774 sub->pts = av_rescale_q(avpkt->pts,
2775 avctx->pkt_timebase, AV_TIME_BASE_Q);
2776 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2777 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2778 !!*got_sub_ptr >= !!sub->num_rects);
2780 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2781 avctx->pkt_timebase.num) {
2782 AVRational ms = { 1, 1000 };
2783 sub->end_display_time = av_rescale_q(avpkt->duration,
2784 avctx->pkt_timebase, ms);
2787 for (i = 0; i < sub->num_rects; i++) {
2788 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2789 av_log(avctx, AV_LOG_ERROR,
2790 "Invalid UTF-8 in decoded subtitles text; "
2791 "maybe missing -sub_charenc option\n");
2792 avsubtitle_free(sub);
2793 return AVERROR_INVALIDDATA;
2797 if (tmp.data != pkt_recoded.data) { // did we recode?
2798 /* prevent from destroying side data from original packet */
2799 pkt_recoded.side_data = NULL;
2800 pkt_recoded.side_data_elems = 0;
2802 av_free_packet(&pkt_recoded);
2804 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2806 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2808 avctx->internal->pkt = NULL;
2812 av_packet_free_side_data(&tmp);
2818 avctx->frame_number++;
2824 void avsubtitle_free(AVSubtitle *sub)
2828 for (i = 0; i < sub->num_rects; i++) {
2829 av_freep(&sub->rects[i]->pict.data[0]);
2830 av_freep(&sub->rects[i]->pict.data[1]);
2831 av_freep(&sub->rects[i]->pict.data[2]);
2832 av_freep(&sub->rects[i]->pict.data[3]);
2833 av_freep(&sub->rects[i]->text);
2834 av_freep(&sub->rects[i]->ass);
2835 av_freep(&sub->rects[i]);
2838 av_freep(&sub->rects);
2840 memset(sub, 0, sizeof(AVSubtitle));
2843 av_cold int avcodec_close(AVCodecContext *avctx)
2848 if (avcodec_is_open(avctx)) {
2849 FramePool *pool = avctx->internal->pool;
2851 if (CONFIG_FRAME_THREAD_ENCODER &&
2852 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2853 ff_frame_thread_encoder_free(avctx);
2855 if (HAVE_THREADS && avctx->internal->thread_ctx)
2856 ff_thread_free(avctx);
2857 if (avctx->codec && avctx->codec->close)
2858 avctx->codec->close(avctx);
2859 avctx->coded_frame = NULL;
2860 avctx->internal->byte_buffer_size = 0;
2861 av_freep(&avctx->internal->byte_buffer);
2862 av_frame_free(&avctx->internal->to_free);
2863 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2864 av_buffer_pool_uninit(&pool->pools[i]);
2865 av_freep(&avctx->internal->pool);
2867 if (avctx->hwaccel && avctx->hwaccel->uninit)
2868 avctx->hwaccel->uninit(avctx);
2869 av_freep(&avctx->internal->hwaccel_priv_data);
2871 av_freep(&avctx->internal);
2874 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2875 av_opt_free(avctx->priv_data);
2877 av_freep(&avctx->priv_data);
2878 if (av_codec_is_encoder(avctx->codec))
2879 av_freep(&avctx->extradata);
2880 avctx->codec = NULL;
2881 avctx->active_thread_type = 0;
2886 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2889 //This is for future deprecatec codec ids, its empty since
2890 //last major bump but will fill up again over time, please don't remove it
2891 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2892 case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
2893 case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
2894 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2895 case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
2896 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2897 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2898 case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
2899 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2900 case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
2901 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2902 case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
2903 case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
2904 case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
2905 case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
2906 case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
2907 case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
2908 case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
2909 case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
2910 default : return id;
2914 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2916 AVCodec *p, *experimental = NULL;
2918 id= remap_deprecated_codec_id(id);
2920 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2922 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2929 return experimental;
2932 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2934 return find_encdec(id, 1);
2937 AVCodec *avcodec_find_encoder_by_name(const char *name)
2944 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2951 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2953 return find_encdec(id, 0);
2956 AVCodec *avcodec_find_decoder_by_name(const char *name)
2963 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2970 const char *avcodec_get_name(enum AVCodecID id)
2972 const AVCodecDescriptor *cd;
2975 if (id == AV_CODEC_ID_NONE)
2977 cd = avcodec_descriptor_get(id);
2980 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2981 codec = avcodec_find_decoder(id);
2984 codec = avcodec_find_encoder(id);
2987 return "unknown_codec";
2990 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2992 int i, len, ret = 0;
2994 #define TAG_PRINT(x) \
2995 (((x) >= '0' && (x) <= '9') || \
2996 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2997 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2999 for (i = 0; i < 4; i++) {
3000 len = snprintf(buf, buf_size,
3001 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
3003 buf_size = buf_size > len ? buf_size - len : 0;
3010 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
3012 const char *codec_type;
3013 const char *codec_name;
3014 const char *profile = NULL;
3018 AVRational display_aspect_ratio;
3019 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
3021 if (!buf || buf_size <= 0)
3023 codec_type = av_get_media_type_string(enc->codec_type);
3024 codec_name = avcodec_get_name(enc->codec_id);
3025 if (enc->profile != FF_PROFILE_UNKNOWN) {
3029 p = encode ? avcodec_find_encoder(enc->codec_id) :
3030 avcodec_find_decoder(enc->codec_id);
3032 profile = av_get_profile_name(p, enc->profile);
3035 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
3037 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
3039 if (enc->codec && strcmp(enc->codec->name, codec_name))
3040 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
3043 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
3044 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
3045 && av_log_get_level() >= AV_LOG_VERBOSE
3047 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3048 ", %d reference frame%s",
3049 enc->refs, enc->refs > 1 ? "s" : "");
3051 if (enc->codec_tag) {
3053 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
3054 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3055 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
3058 switch (enc->codec_type) {
3059 case AVMEDIA_TYPE_VIDEO:
3061 char detail[256] = "(";
3063 av_strlcat(buf, separator, buf_size);
3065 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3066 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
3067 av_get_pix_fmt_name(enc->pix_fmt));
3068 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
3069 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
3070 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
3071 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
3072 av_strlcatf(detail, sizeof(detail), "%s, ",
3073 av_color_range_name(enc->color_range));
3075 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
3076 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
3077 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
3078 if (enc->colorspace != (int)enc->color_primaries ||
3079 enc->colorspace != (int)enc->color_trc) {
3081 av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
3082 av_color_space_name(enc->colorspace),
3083 av_color_primaries_name(enc->color_primaries),
3084 av_color_transfer_name(enc->color_trc));
3086 av_strlcatf(detail, sizeof(detail), "%s, ",
3087 av_get_colorspace_name(enc->colorspace));
3090 if (av_log_get_level() >= AV_LOG_DEBUG &&
3091 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
3092 av_strlcatf(detail, sizeof(detail), "%s, ",
3093 av_chroma_location_name(enc->chroma_sample_location));
3095 if (strlen(detail) > 1) {
3096 detail[strlen(detail) - 2] = 0;
3097 av_strlcatf(buf, buf_size, "%s)", detail);
3102 av_strlcat(buf, new_line ? separator : ", ", buf_size);
3104 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3106 enc->width, enc->height);
3108 if (av_log_get_level() >= AV_LOG_VERBOSE &&
3109 (enc->width != enc->coded_width ||
3110 enc->height != enc->coded_height))
3111 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3112 " (%dx%d)", enc->coded_width, enc->coded_height);
3114 if (enc->sample_aspect_ratio.num) {
3115 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
3116 enc->width * enc->sample_aspect_ratio.num,
3117 enc->height * enc->sample_aspect_ratio.den,
3119 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3120 " [SAR %d:%d DAR %d:%d]",
3121 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
3122 display_aspect_ratio.num, display_aspect_ratio.den);
3124 if (av_log_get_level() >= AV_LOG_DEBUG) {
3125 int g = av_gcd(enc->time_base.num, enc->time_base.den);
3126 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3128 enc->time_base.num / g, enc->time_base.den / g);
3132 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3133 ", q=%d-%d", enc->qmin, enc->qmax);
3136 case AVMEDIA_TYPE_AUDIO:
3137 av_strlcat(buf, separator, buf_size);
3139 if (enc->sample_rate) {
3140 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3141 "%d Hz, ", enc->sample_rate);
3143 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
3144 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
3145 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3146 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
3148 if ( enc->bits_per_raw_sample > 0
3149 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
3150 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3151 " (%d bit)", enc->bits_per_raw_sample);
3153 case AVMEDIA_TYPE_DATA:
3154 if (av_log_get_level() >= AV_LOG_DEBUG) {
3155 int g = av_gcd(enc->time_base.num, enc->time_base.den);
3157 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3159 enc->time_base.num / g, enc->time_base.den / g);
3162 case AVMEDIA_TYPE_SUBTITLE:
3164 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3165 ", %dx%d", enc->width, enc->height);
3171 if (enc->flags & CODEC_FLAG_PASS1)
3172 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3174 if (enc->flags & CODEC_FLAG_PASS2)
3175 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3178 bitrate = get_bit_rate(enc);
3180 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3181 ", %d kb/s", bitrate / 1000);
3182 } else if (enc->rc_max_rate > 0) {
3183 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3184 ", max. %d kb/s", enc->rc_max_rate / 1000);
3188 const char *av_get_profile_name(const AVCodec *codec, int profile)
3191 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
3194 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
3195 if (p->profile == profile)
3201 unsigned avcodec_version(void)
3203 // av_assert0(AV_CODEC_ID_V410==164);
3204 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
3205 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
3206 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
3207 av_assert0(AV_CODEC_ID_SRT==94216);
3208 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
3210 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
3211 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
3212 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
3213 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
3214 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
3215 return LIBAVCODEC_VERSION_INT;
3218 const char *avcodec_configuration(void)
3220 return FFMPEG_CONFIGURATION;
3223 const char *avcodec_license(void)
3225 #define LICENSE_PREFIX "libavcodec license: "
3226 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
3229 void avcodec_flush_buffers(AVCodecContext *avctx)
3231 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
3232 ff_thread_flush(avctx);
3233 else if (avctx->codec->flush)
3234 avctx->codec->flush(avctx);
3236 avctx->pts_correction_last_pts =
3237 avctx->pts_correction_last_dts = INT64_MIN;
3239 if (!avctx->refcounted_frames)
3240 av_frame_unref(avctx->internal->to_free);
3243 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
3246 case AV_CODEC_ID_8SVX_EXP:
3247 case AV_CODEC_ID_8SVX_FIB:
3248 case AV_CODEC_ID_ADPCM_CT:
3249 case AV_CODEC_ID_ADPCM_IMA_APC:
3250 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
3251 case AV_CODEC_ID_ADPCM_IMA_OKI:
3252 case AV_CODEC_ID_ADPCM_IMA_WS:
3253 case AV_CODEC_ID_ADPCM_G722:
3254 case AV_CODEC_ID_ADPCM_YAMAHA:
3256 case AV_CODEC_ID_DSD_LSBF:
3257 case AV_CODEC_ID_DSD_MSBF:
3258 case AV_CODEC_ID_DSD_LSBF_PLANAR:
3259 case AV_CODEC_ID_DSD_MSBF_PLANAR:
3260 case AV_CODEC_ID_PCM_ALAW:
3261 case AV_CODEC_ID_PCM_MULAW:
3262 case AV_CODEC_ID_PCM_S8:
3263 case AV_CODEC_ID_PCM_S8_PLANAR:
3264 case AV_CODEC_ID_PCM_U8:
3265 case AV_CODEC_ID_PCM_ZORK:
3267 case AV_CODEC_ID_PCM_S16BE:
3268 case AV_CODEC_ID_PCM_S16BE_PLANAR:
3269 case AV_CODEC_ID_PCM_S16LE:
3270 case AV_CODEC_ID_PCM_S16LE_PLANAR:
3271 case AV_CODEC_ID_PCM_U16BE:
3272 case AV_CODEC_ID_PCM_U16LE:
3274 case AV_CODEC_ID_PCM_S24DAUD:
3275 case AV_CODEC_ID_PCM_S24BE:
3276 case AV_CODEC_ID_PCM_S24LE:
3277 case AV_CODEC_ID_PCM_S24LE_PLANAR:
3278 case AV_CODEC_ID_PCM_U24BE:
3279 case AV_CODEC_ID_PCM_U24LE:
3281 case AV_CODEC_ID_PCM_S32BE:
3282 case AV_CODEC_ID_PCM_S32LE:
3283 case AV_CODEC_ID_PCM_S32LE_PLANAR:
3284 case AV_CODEC_ID_PCM_U32BE:
3285 case AV_CODEC_ID_PCM_U32LE:
3286 case AV_CODEC_ID_PCM_F32BE:
3287 case AV_CODEC_ID_PCM_F32LE:
3289 case AV_CODEC_ID_PCM_F64BE:
3290 case AV_CODEC_ID_PCM_F64LE:
3297 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
3299 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
3300 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3301 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3302 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3303 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3304 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3305 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3306 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3307 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3308 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3309 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3311 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3312 return AV_CODEC_ID_NONE;
3313 if (be < 0 || be > 1)
3315 return map[fmt][be];
3318 int av_get_bits_per_sample(enum AVCodecID codec_id)
3321 case AV_CODEC_ID_ADPCM_SBPRO_2:
3323 case AV_CODEC_ID_ADPCM_SBPRO_3:
3325 case AV_CODEC_ID_ADPCM_SBPRO_4:
3326 case AV_CODEC_ID_ADPCM_IMA_WAV:
3327 case AV_CODEC_ID_ADPCM_IMA_QT:
3328 case AV_CODEC_ID_ADPCM_SWF:
3329 case AV_CODEC_ID_ADPCM_MS:
3332 return av_get_exact_bits_per_sample(codec_id);
3336 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3338 int id, sr, ch, ba, tag, bps;
3340 id = avctx->codec_id;
3341 sr = avctx->sample_rate;
3342 ch = avctx->channels;
3343 ba = avctx->block_align;
3344 tag = avctx->codec_tag;
3345 bps = av_get_exact_bits_per_sample(avctx->codec_id);
3347 /* codecs with an exact constant bits per sample */
3348 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3349 return (frame_bytes * 8LL) / (bps * ch);
3350 bps = avctx->bits_per_coded_sample;
3352 /* codecs with a fixed packet duration */
3354 case AV_CODEC_ID_ADPCM_ADX: return 32;
3355 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3356 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3357 case AV_CODEC_ID_AMR_NB:
3358 case AV_CODEC_ID_EVRC:
3359 case AV_CODEC_ID_GSM:
3360 case AV_CODEC_ID_QCELP:
3361 case AV_CODEC_ID_RA_288: return 160;
3362 case AV_CODEC_ID_AMR_WB:
3363 case AV_CODEC_ID_GSM_MS: return 320;
3364 case AV_CODEC_ID_MP1: return 384;
3365 case AV_CODEC_ID_ATRAC1: return 512;
3366 case AV_CODEC_ID_ATRAC3: return 1024;
3367 case AV_CODEC_ID_ATRAC3P: return 2048;
3368 case AV_CODEC_ID_MP2:
3369 case AV_CODEC_ID_MUSEPACK7: return 1152;
3370 case AV_CODEC_ID_AC3: return 1536;
3374 /* calc from sample rate */
3375 if (id == AV_CODEC_ID_TTA)
3376 return 256 * sr / 245;
3379 /* calc from sample rate and channels */
3380 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3381 return (480 << (sr / 22050)) / ch;
3386 /* calc from block_align */
3387 if (id == AV_CODEC_ID_SIPR) {
3389 case 20: return 160;
3390 case 19: return 144;
3391 case 29: return 288;
3392 case 37: return 480;
3394 } else if (id == AV_CODEC_ID_ILBC) {
3396 case 38: return 160;
3397 case 50: return 240;
3402 if (frame_bytes > 0) {
3403 /* calc from frame_bytes only */
3404 if (id == AV_CODEC_ID_TRUESPEECH)
3405 return 240 * (frame_bytes / 32);
3406 if (id == AV_CODEC_ID_NELLYMOSER)
3407 return 256 * (frame_bytes / 64);
3408 if (id == AV_CODEC_ID_RA_144)
3409 return 160 * (frame_bytes / 20);
3410 if (id == AV_CODEC_ID_G723_1)
3411 return 240 * (frame_bytes / 24);
3414 /* calc from frame_bytes and bits_per_coded_sample */
3415 if (id == AV_CODEC_ID_ADPCM_G726)
3416 return frame_bytes * 8 / bps;
3420 /* calc from frame_bytes and channels */
3422 case AV_CODEC_ID_ADPCM_AFC:
3423 return frame_bytes / (9 * ch) * 16;
3424 case AV_CODEC_ID_ADPCM_DTK:
3425 return frame_bytes / (16 * ch) * 28;
3426 case AV_CODEC_ID_ADPCM_4XM:
3427 case AV_CODEC_ID_ADPCM_IMA_ISS:
3428 return (frame_bytes - 4 * ch) * 2 / ch;
3429 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3430 return (frame_bytes - 4) * 2 / ch;
3431 case AV_CODEC_ID_ADPCM_IMA_AMV:
3432 return (frame_bytes - 8) * 2 / ch;
3433 case AV_CODEC_ID_ADPCM_THP:
3434 if (avctx->extradata)
3435 return frame_bytes * 14 / (8 * ch);
3437 case AV_CODEC_ID_ADPCM_XA:
3438 return (frame_bytes / 128) * 224 / ch;
3439 case AV_CODEC_ID_INTERPLAY_DPCM:
3440 return (frame_bytes - 6 - ch) / ch;
3441 case AV_CODEC_ID_ROQ_DPCM:
3442 return (frame_bytes - 8) / ch;
3443 case AV_CODEC_ID_XAN_DPCM:
3444 return (frame_bytes - 2 * ch) / ch;
3445 case AV_CODEC_ID_MACE3:
3446 return 3 * frame_bytes / ch;
3447 case AV_CODEC_ID_MACE6:
3448 return 6 * frame_bytes / ch;
3449 case AV_CODEC_ID_PCM_LXF:
3450 return 2 * (frame_bytes / (5 * ch));
3451 case AV_CODEC_ID_IAC:
3452 case AV_CODEC_ID_IMC:
3453 return 4 * frame_bytes / ch;
3457 /* calc from frame_bytes, channels, and codec_tag */
3458 if (id == AV_CODEC_ID_SOL_DPCM) {
3460 return frame_bytes / ch;
3462 return frame_bytes * 2 / ch;
3467 /* calc from frame_bytes, channels, and block_align */
3468 int blocks = frame_bytes / ba;
3469 switch (avctx->codec_id) {
3470 case AV_CODEC_ID_ADPCM_IMA_WAV:
3471 if (bps < 2 || bps > 5)
3473 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3474 case AV_CODEC_ID_ADPCM_IMA_DK3:
3475 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3476 case AV_CODEC_ID_ADPCM_IMA_DK4:
3477 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3478 case AV_CODEC_ID_ADPCM_IMA_RAD:
3479 return blocks * ((ba - 4 * ch) * 2 / ch);
3480 case AV_CODEC_ID_ADPCM_MS:
3481 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3486 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3487 switch (avctx->codec_id) {
3488 case AV_CODEC_ID_PCM_DVD:
3491 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3492 case AV_CODEC_ID_PCM_BLURAY:
3495 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3496 case AV_CODEC_ID_S302M:
3497 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3503 /* Fall back on using frame_size */
3504 if (avctx->frame_size > 1 && frame_bytes)
3505 return avctx->frame_size;
3507 //For WMA we currently have no other means to calculate duration thus we
3508 //do it here by assuming CBR, which is true for all known cases.
3509 if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
3510 if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
3511 return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
3518 int ff_thread_init(AVCodecContext *s)
3525 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3539 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3542 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3546 #if FF_API_MISSING_SAMPLE
3547 FF_DISABLE_DEPRECATION_WARNINGS
3548 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3550 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3551 "version to the newest one from Git. If the problem still "
3552 "occurs, it means that your file has a feature which has not "
3553 "been implemented.\n", feature);
3555 av_log_ask_for_sample(avc, NULL);
3558 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3560 va_list argument_list;
3562 va_start(argument_list, msg);
3565 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3566 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3567 "of this file to ftp://upload.ffmpeg.org/incoming/ "
3568 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3570 va_end(argument_list);
3572 FF_ENABLE_DEPRECATION_WARNINGS
3573 #endif /* FF_API_MISSING_SAMPLE */
3575 static AVHWAccel *first_hwaccel = NULL;
3576 static AVHWAccel **last_hwaccel = &first_hwaccel;
3578 void av_register_hwaccel(AVHWAccel *hwaccel)
3580 AVHWAccel **p = last_hwaccel;
3581 hwaccel->next = NULL;
3582 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3584 last_hwaccel = &hwaccel->next;
3587 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
3589 return hwaccel ? hwaccel->next : first_hwaccel;
3592 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3595 // There is no good way to rollback a failure to destroy the
3596 // mutex, so we ignore failures.
3597 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
3598 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
3601 avformat_mutex = NULL;
3605 void *new_codec_mutex = NULL;
3606 void *new_avformat_mutex = NULL;
3608 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
3609 return err > 0 ? AVERROR_UNKNOWN : err;
3611 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
3612 // Ignore failures to destroy the newly created mutex.
3613 cb(&new_codec_mutex, AV_LOCK_DESTROY);
3614 return err > 0 ? AVERROR_UNKNOWN : err;
3617 codec_mutex = new_codec_mutex;
3618 avformat_mutex = new_avformat_mutex;
3624 int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
3627 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3631 if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1 &&
3632 !(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE)) {
3633 av_log(log_ctx, AV_LOG_ERROR,
3634 "Insufficient thread locking. At least %d threads are "
3635 "calling avcodec_open2() at the same time right now.\n",
3636 entangled_thread_counter);
3638 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3639 ff_avcodec_locked = 1;
3640 ff_unlock_avcodec();
3641 return AVERROR(EINVAL);
3643 av_assert0(!ff_avcodec_locked);
3644 ff_avcodec_locked = 1;
3648 int ff_unlock_avcodec(void)
3650 av_assert0(ff_avcodec_locked);
3651 ff_avcodec_locked = 0;
3652 avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
3654 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3661 int avpriv_lock_avformat(void)
3664 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3670 int avpriv_unlock_avformat(void)
3673 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3679 unsigned int avpriv_toupper4(unsigned int x)
3681 return av_toupper(x & 0xFF) +
3682 (av_toupper((x >> 8) & 0xFF) << 8) +
3683 (av_toupper((x >> 16) & 0xFF) << 16) +
3684 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
3687 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3691 dst->owner = src->owner;
3693 ret = av_frame_ref(dst->f, src->f);
3697 av_assert0(!dst->progress);
3699 if (src->progress &&
3700 !(dst->progress = av_buffer_ref(src->progress))) {
3701 ff_thread_release_buffer(dst->owner, dst);
3702 return AVERROR(ENOMEM);
3710 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3712 return ff_get_format(avctx, fmt);
3715 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3718 return ff_get_buffer(avctx, f->f, flags);
3721 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3724 av_frame_unref(f->f);
3727 void ff_thread_finish_setup(AVCodecContext *avctx)
3731 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3735 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3739 int ff_thread_can_start_frame(AVCodecContext *avctx)
3744 int ff_alloc_entries(AVCodecContext *avctx, int count)
3749 void ff_reset_entries(AVCodecContext *avctx)
3753 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3757 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3763 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3765 AVCodec *c= avcodec_find_decoder(codec_id);
3767 c= avcodec_find_encoder(codec_id);
3771 if (codec_id <= AV_CODEC_ID_NONE)
3772 return AVMEDIA_TYPE_UNKNOWN;
3773 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3774 return AVMEDIA_TYPE_VIDEO;
3775 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3776 return AVMEDIA_TYPE_AUDIO;
3777 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3778 return AVMEDIA_TYPE_SUBTITLE;
3780 return AVMEDIA_TYPE_UNKNOWN;
3783 int avcodec_is_open(AVCodecContext *s)
3785 return !!s->internal;
3788 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3793 ret = av_bprint_finalize(buf, &str);
3796 if (!av_bprint_is_complete(buf)) {
3798 return AVERROR(ENOMEM);
3801 avctx->extradata = str;
3802 /* Note: the string is NUL terminated (so extradata can be read as a
3803 * string), but the ending character is not accounted in the size (in
3804 * binary formats you are likely not supposed to mux that character). When
3805 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3807 avctx->extradata_size = buf->len;
3811 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3813 uint32_t *av_restrict state)
3817 av_assert0(p <= end);
3821 for (i = 0; i < 3; i++) {
3822 uint32_t tmp = *state << 8;
3823 *state = tmp + *(p++);
3824 if (tmp == 0x100 || p == end)
3829 if (p[-1] > 1 ) p += 3;
3830 else if (p[-2] ) p += 2;
3831 else if (p[-3]|(p[-1]-1)) p++;
3838 p = FFMIN(p, end) - 4;
3839 *state = AV_RB32(p);