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/mem_internal.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/samplefmt.h"
43 #include "libavutil/dict.h"
44 #include "libavutil/thread.h"
46 #include "libavutil/opt.h"
48 #include "mpegvideo.h"
50 #include "frame_thread_encoder.h"
53 #include "bytestream.h"
63 #include "libavutil/ffversion.h"
64 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
66 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
67 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
69 void * volatile * mutex = arg;
77 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
79 return AVERROR(ENOMEM);
80 if ((err = pthread_mutex_init(tmp, NULL))) {
84 if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
85 pthread_mutex_destroy(tmp);
90 if ((err = pthread_mutex_lock(*mutex)))
95 if ((err = pthread_mutex_unlock(*mutex)))
101 pthread_mutex_destroy(*mutex);
103 avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
108 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
110 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
114 volatile int ff_avcodec_locked;
115 static int volatile entangled_thread_counter = 0;
116 static void *codec_mutex;
117 static void *avformat_mutex;
119 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
122 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
127 if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
128 memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
131 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
134 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
139 if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
140 memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
143 /* encoder management */
144 static AVCodec *first_avcodec = NULL;
145 static AVCodec **last_avcodec = &first_avcodec;
147 AVCodec *av_codec_next(const AVCodec *c)
152 return first_avcodec;
155 static av_cold void avcodec_init(void)
157 static int initialized = 0;
159 if (initialized != 0)
164 ff_me_cmp_init_static();
167 int av_codec_is_encoder(const AVCodec *codec)
169 return codec && (codec->encode_sub || codec->encode2);
172 int av_codec_is_decoder(const AVCodec *codec)
174 return codec && codec->decode;
177 av_cold void avcodec_register(AVCodec *codec)
184 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
186 last_avcodec = &codec->next;
188 if (codec->init_static_data)
189 codec->init_static_data(codec);
193 unsigned avcodec_get_edge_width(void)
199 #if FF_API_SET_DIMENSIONS
200 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
202 int ret = ff_set_dimensions(s, width, height);
204 av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
209 int ff_set_dimensions(AVCodecContext *s, int width, int height)
211 int ret = av_image_check_size(width, height, 0, s);
216 s->coded_width = width;
217 s->coded_height = height;
218 s->width = AV_CEIL_RSHIFT(width, s->lowres);
219 s->height = AV_CEIL_RSHIFT(height, s->lowres);
224 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
226 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
229 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
231 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
234 avctx->sample_aspect_ratio = sar;
239 int ff_side_data_update_matrix_encoding(AVFrame *frame,
240 enum AVMatrixEncoding matrix_encoding)
242 AVFrameSideData *side_data;
243 enum AVMatrixEncoding *data;
245 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
247 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
248 sizeof(enum AVMatrixEncoding));
251 return AVERROR(ENOMEM);
253 data = (enum AVMatrixEncoding*)side_data->data;
254 *data = matrix_encoding;
259 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
260 int linesize_align[AV_NUM_DATA_POINTERS])
265 AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
268 w_align = 1 << desc->log2_chroma_w;
269 h_align = 1 << desc->log2_chroma_h;
272 switch (s->pix_fmt) {
273 case AV_PIX_FMT_YUV420P:
274 case AV_PIX_FMT_YUYV422:
275 case AV_PIX_FMT_YVYU422:
276 case AV_PIX_FMT_UYVY422:
277 case AV_PIX_FMT_YUV422P:
278 case AV_PIX_FMT_YUV440P:
279 case AV_PIX_FMT_YUV444P:
280 case AV_PIX_FMT_GBRP:
281 case AV_PIX_FMT_GBRAP:
282 case AV_PIX_FMT_GRAY8:
283 case AV_PIX_FMT_GRAY16BE:
284 case AV_PIX_FMT_GRAY16LE:
285 case AV_PIX_FMT_YUVJ420P:
286 case AV_PIX_FMT_YUVJ422P:
287 case AV_PIX_FMT_YUVJ440P:
288 case AV_PIX_FMT_YUVJ444P:
289 case AV_PIX_FMT_YUVA420P:
290 case AV_PIX_FMT_YUVA422P:
291 case AV_PIX_FMT_YUVA444P:
292 case AV_PIX_FMT_YUV420P9LE:
293 case AV_PIX_FMT_YUV420P9BE:
294 case AV_PIX_FMT_YUV420P10LE:
295 case AV_PIX_FMT_YUV420P10BE:
296 case AV_PIX_FMT_YUV420P12LE:
297 case AV_PIX_FMT_YUV420P12BE:
298 case AV_PIX_FMT_YUV420P14LE:
299 case AV_PIX_FMT_YUV420P14BE:
300 case AV_PIX_FMT_YUV420P16LE:
301 case AV_PIX_FMT_YUV420P16BE:
302 case AV_PIX_FMT_YUVA420P9LE:
303 case AV_PIX_FMT_YUVA420P9BE:
304 case AV_PIX_FMT_YUVA420P10LE:
305 case AV_PIX_FMT_YUVA420P10BE:
306 case AV_PIX_FMT_YUVA420P16LE:
307 case AV_PIX_FMT_YUVA420P16BE:
308 case AV_PIX_FMT_YUV422P9LE:
309 case AV_PIX_FMT_YUV422P9BE:
310 case AV_PIX_FMT_YUV422P10LE:
311 case AV_PIX_FMT_YUV422P10BE:
312 case AV_PIX_FMT_YUV422P12LE:
313 case AV_PIX_FMT_YUV422P12BE:
314 case AV_PIX_FMT_YUV422P14LE:
315 case AV_PIX_FMT_YUV422P14BE:
316 case AV_PIX_FMT_YUV422P16LE:
317 case AV_PIX_FMT_YUV422P16BE:
318 case AV_PIX_FMT_YUVA422P9LE:
319 case AV_PIX_FMT_YUVA422P9BE:
320 case AV_PIX_FMT_YUVA422P10LE:
321 case AV_PIX_FMT_YUVA422P10BE:
322 case AV_PIX_FMT_YUVA422P16LE:
323 case AV_PIX_FMT_YUVA422P16BE:
324 case AV_PIX_FMT_YUV440P10LE:
325 case AV_PIX_FMT_YUV440P10BE:
326 case AV_PIX_FMT_YUV440P12LE:
327 case AV_PIX_FMT_YUV440P12BE:
328 case AV_PIX_FMT_YUV444P9LE:
329 case AV_PIX_FMT_YUV444P9BE:
330 case AV_PIX_FMT_YUV444P10LE:
331 case AV_PIX_FMT_YUV444P10BE:
332 case AV_PIX_FMT_YUV444P12LE:
333 case AV_PIX_FMT_YUV444P12BE:
334 case AV_PIX_FMT_YUV444P14LE:
335 case AV_PIX_FMT_YUV444P14BE:
336 case AV_PIX_FMT_YUV444P16LE:
337 case AV_PIX_FMT_YUV444P16BE:
338 case AV_PIX_FMT_YUVA444P9LE:
339 case AV_PIX_FMT_YUVA444P9BE:
340 case AV_PIX_FMT_YUVA444P10LE:
341 case AV_PIX_FMT_YUVA444P10BE:
342 case AV_PIX_FMT_YUVA444P16LE:
343 case AV_PIX_FMT_YUVA444P16BE:
344 case AV_PIX_FMT_GBRP9LE:
345 case AV_PIX_FMT_GBRP9BE:
346 case AV_PIX_FMT_GBRP10LE:
347 case AV_PIX_FMT_GBRP10BE:
348 case AV_PIX_FMT_GBRP12LE:
349 case AV_PIX_FMT_GBRP12BE:
350 case AV_PIX_FMT_GBRP14LE:
351 case AV_PIX_FMT_GBRP14BE:
352 case AV_PIX_FMT_GBRP16LE:
353 case AV_PIX_FMT_GBRP16BE:
354 case AV_PIX_FMT_GBRAP16LE:
355 case AV_PIX_FMT_GBRAP16BE:
356 w_align = 16; //FIXME assume 16 pixel per macroblock
357 h_align = 16 * 2; // interlaced needs 2 macroblocks height
359 case AV_PIX_FMT_YUV411P:
360 case AV_PIX_FMT_YUVJ411P:
361 case AV_PIX_FMT_UYYVYY411:
365 case AV_PIX_FMT_YUV410P:
366 if (s->codec_id == AV_CODEC_ID_SVQ1) {
371 case AV_PIX_FMT_RGB555:
372 if (s->codec_id == AV_CODEC_ID_RPZA) {
377 case AV_PIX_FMT_PAL8:
378 case AV_PIX_FMT_BGR8:
379 case AV_PIX_FMT_RGB8:
380 if (s->codec_id == AV_CODEC_ID_SMC ||
381 s->codec_id == AV_CODEC_ID_CINEPAK) {
385 if (s->codec_id == AV_CODEC_ID_JV) {
390 case AV_PIX_FMT_BGR24:
391 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
392 (s->codec_id == AV_CODEC_ID_ZLIB)) {
397 case AV_PIX_FMT_RGB24:
398 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
407 if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
408 w_align = FFMAX(w_align, 8);
411 *width = FFALIGN(*width, w_align);
412 *height = FFALIGN(*height, h_align);
413 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres) {
414 // some of the optimized chroma MC reads one line too much
415 // which is also done in mpeg decoders with lowres > 0
418 // H.264 uses edge emulation for out of frame motion vectors, for this
419 // it requires a temporary area large enough to hold a 21x21 block,
420 // increasing witdth ensure that the temporary area is large enough,
421 // the next rounded up width is 32
422 *width = FFMAX(*width, 32);
425 for (i = 0; i < 4; i++)
426 linesize_align[i] = STRIDE_ALIGN;
429 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
431 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
432 int chroma_shift = desc->log2_chroma_w;
433 int linesize_align[AV_NUM_DATA_POINTERS];
436 avcodec_align_dimensions2(s, width, height, linesize_align);
437 align = FFMAX(linesize_align[0], linesize_align[3]);
438 linesize_align[1] <<= chroma_shift;
439 linesize_align[2] <<= chroma_shift;
440 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
441 *width = FFALIGN(*width, align);
444 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
446 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
447 return AVERROR(EINVAL);
450 *xpos = (pos&1) * 128;
451 *ypos = ((pos>>1)^(pos<4)) * 128;
456 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
460 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
461 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
464 return AVCHROMA_LOC_UNSPECIFIED;
467 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
468 enum AVSampleFormat sample_fmt, const uint8_t *buf,
469 int buf_size, int align)
471 int ch, planar, needed_size, ret = 0;
473 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
474 frame->nb_samples, sample_fmt,
476 if (buf_size < needed_size)
477 return AVERROR(EINVAL);
479 planar = av_sample_fmt_is_planar(sample_fmt);
480 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
481 if (!(frame->extended_data = av_mallocz_array(nb_channels,
482 sizeof(*frame->extended_data))))
483 return AVERROR(ENOMEM);
485 frame->extended_data = frame->data;
488 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
489 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
490 sample_fmt, align)) < 0) {
491 if (frame->extended_data != frame->data)
492 av_freep(&frame->extended_data);
495 if (frame->extended_data != frame->data) {
496 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
497 frame->data[ch] = frame->extended_data[ch];
503 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
505 FramePool *pool = avctx->internal->pool;
508 switch (avctx->codec_type) {
509 case AVMEDIA_TYPE_VIDEO: {
513 int w = frame->width;
514 int h = frame->height;
515 int tmpsize, unaligned;
517 if (pool->format == frame->format &&
518 pool->width == frame->width && pool->height == frame->height)
521 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
524 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
525 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
526 ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
529 // increase alignment of w for next try (rhs gives the lowest bit set in w)
533 for (i = 0; i < 4; i++)
534 unaligned |= linesize[i] % pool->stride_align[i];
537 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
542 for (i = 0; i < 3 && data[i + 1]; i++)
543 size[i] = data[i + 1] - data[i];
544 size[i] = tmpsize - (data[i] - data[0]);
546 for (i = 0; i < 4; i++) {
547 av_buffer_pool_uninit(&pool->pools[i]);
548 pool->linesize[i] = linesize[i];
550 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
551 CONFIG_MEMORY_POISONING ?
554 if (!pool->pools[i]) {
555 ret = AVERROR(ENOMEM);
560 pool->format = frame->format;
561 pool->width = frame->width;
562 pool->height = frame->height;
566 case AVMEDIA_TYPE_AUDIO: {
567 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
568 int planar = av_sample_fmt_is_planar(frame->format);
569 int planes = planar ? ch : 1;
571 if (pool->format == frame->format && pool->planes == planes &&
572 pool->channels == ch && frame->nb_samples == pool->samples)
575 av_buffer_pool_uninit(&pool->pools[0]);
576 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
577 frame->nb_samples, frame->format, 0);
581 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
582 if (!pool->pools[0]) {
583 ret = AVERROR(ENOMEM);
587 pool->format = frame->format;
588 pool->planes = planes;
590 pool->samples = frame->nb_samples;
593 default: av_assert0(0);
597 for (i = 0; i < 4; i++)
598 av_buffer_pool_uninit(&pool->pools[i]);
600 pool->planes = pool->channels = pool->samples = 0;
601 pool->width = pool->height = 0;
605 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
607 FramePool *pool = avctx->internal->pool;
608 int planes = pool->planes;
611 frame->linesize[0] = pool->linesize[0];
613 if (planes > AV_NUM_DATA_POINTERS) {
614 frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
615 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
616 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
617 sizeof(*frame->extended_buf));
618 if (!frame->extended_data || !frame->extended_buf) {
619 av_freep(&frame->extended_data);
620 av_freep(&frame->extended_buf);
621 return AVERROR(ENOMEM);
624 frame->extended_data = frame->data;
625 av_assert0(frame->nb_extended_buf == 0);
628 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
629 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
632 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
634 for (i = 0; i < frame->nb_extended_buf; i++) {
635 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
636 if (!frame->extended_buf[i])
638 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
641 if (avctx->debug & FF_DEBUG_BUFFERS)
642 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
646 av_frame_unref(frame);
647 return AVERROR(ENOMEM);
650 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
652 FramePool *pool = s->internal->pool;
653 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
657 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
662 av_log(s, AV_LOG_ERROR,
663 "Unable to get pixel format descriptor for format %s\n",
664 av_get_pix_fmt_name(pic->format));
665 return AVERROR(EINVAL);
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 (desc->flags & AV_PIX_FMT_FLAG_PAL ||
685 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
686 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
688 if (s->debug & FF_DEBUG_BUFFERS)
689 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
694 return AVERROR(ENOMEM);
697 void ff_color_frame(AVFrame *frame, const int c[4])
699 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
702 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
704 for (p = 0; p<desc->nb_components; p++) {
705 uint8_t *dst = frame->data[p];
706 int is_chroma = p == 1 || p == 2;
707 int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
708 int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
709 for (y = 0; y < height; y++) {
710 if (desc->comp[0].depth >= 9) {
711 for (x = 0; x<bytes; x++)
712 ((uint16_t*)dst)[x] = c[p];
714 memset(dst, c[p], bytes);
715 dst += frame->linesize[p];
720 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
724 if ((ret = update_frame_pool(avctx, frame)) < 0)
727 switch (avctx->codec_type) {
728 case AVMEDIA_TYPE_VIDEO:
729 return video_get_buffer(avctx, frame);
730 case AVMEDIA_TYPE_AUDIO:
731 return audio_get_buffer(avctx, frame);
737 static int add_metadata_from_side_data(AVPacket *avpkt, AVFrame *frame)
740 const uint8_t *side_metadata;
742 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
744 side_metadata = av_packet_get_side_data(avpkt,
745 AV_PKT_DATA_STRINGS_METADATA, &size);
746 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
749 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
751 AVPacket *pkt = avctx->internal->pkt;
753 static const struct {
754 enum AVPacketSideDataType packet;
755 enum AVFrameSideDataType frame;
757 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
758 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
759 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
760 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
764 frame->pkt_pts = pkt->pts;
765 av_frame_set_pkt_pos (frame, pkt->pos);
766 av_frame_set_pkt_duration(frame, pkt->duration);
767 av_frame_set_pkt_size (frame, pkt->size);
769 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
771 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
773 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
777 return AVERROR(ENOMEM);
779 memcpy(frame_sd->data, packet_sd, size);
782 add_metadata_from_side_data(pkt, frame);
784 frame->pkt_pts = AV_NOPTS_VALUE;
785 av_frame_set_pkt_pos (frame, -1);
786 av_frame_set_pkt_duration(frame, 0);
787 av_frame_set_pkt_size (frame, -1);
789 frame->reordered_opaque = avctx->reordered_opaque;
791 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
792 frame->color_primaries = avctx->color_primaries;
793 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
794 frame->color_trc = avctx->color_trc;
795 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
796 av_frame_set_colorspace(frame, avctx->colorspace);
797 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
798 av_frame_set_color_range(frame, avctx->color_range);
799 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
800 frame->chroma_location = avctx->chroma_sample_location;
802 switch (avctx->codec->type) {
803 case AVMEDIA_TYPE_VIDEO:
804 frame->format = avctx->pix_fmt;
805 if (!frame->sample_aspect_ratio.num)
806 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
808 if (frame->width && frame->height &&
809 av_image_check_sar(frame->width, frame->height,
810 frame->sample_aspect_ratio) < 0) {
811 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
812 frame->sample_aspect_ratio.num,
813 frame->sample_aspect_ratio.den);
814 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
818 case AVMEDIA_TYPE_AUDIO:
819 if (!frame->sample_rate)
820 frame->sample_rate = avctx->sample_rate;
821 if (frame->format < 0)
822 frame->format = avctx->sample_fmt;
823 if (!frame->channel_layout) {
824 if (avctx->channel_layout) {
825 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
827 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
829 return AVERROR(EINVAL);
832 frame->channel_layout = avctx->channel_layout;
834 if (avctx->channels > FF_SANE_NB_CHANNELS) {
835 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
837 return AVERROR(ENOSYS);
841 av_frame_set_channels(frame, avctx->channels);
847 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
849 return ff_init_buffer_info(avctx, frame);
852 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
854 const AVHWAccel *hwaccel = avctx->hwaccel;
855 int override_dimensions = 1;
858 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
859 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
860 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
861 return AVERROR(EINVAL);
864 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
865 if (frame->width <= 0 || frame->height <= 0) {
866 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
867 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
868 override_dimensions = 0;
871 ret = ff_decode_frame_props(avctx, frame);
876 if (hwaccel->alloc_frame) {
877 ret = hwaccel->alloc_frame(avctx, frame);
881 avctx->sw_pix_fmt = avctx->pix_fmt;
883 ret = avctx->get_buffer2(avctx, frame, flags);
886 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
887 frame->width = avctx->width;
888 frame->height = avctx->height;
894 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
896 int ret = get_buffer_internal(avctx, frame, flags);
898 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
899 frame->width = frame->height = 0;
904 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
909 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
911 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
912 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
913 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
914 av_frame_unref(frame);
917 ff_init_buffer_info(avctx, frame);
920 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
922 if (av_frame_is_writable(frame))
923 return ff_decode_frame_props(avctx, frame);
925 tmp = av_frame_alloc();
927 return AVERROR(ENOMEM);
929 av_frame_move_ref(tmp, frame);
931 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
937 av_frame_copy(frame, tmp);
943 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
945 int ret = reget_buffer_internal(avctx, frame);
947 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
951 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
955 for (i = 0; i < count; i++) {
956 int r = func(c, (char *)arg + i * size);
963 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
967 for (i = 0; i < count; i++) {
968 int r = func(c, arg, i, 0);
975 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
978 while (tags->pix_fmt >= 0) {
979 if (tags->fourcc == fourcc)
980 return tags->pix_fmt;
983 return AV_PIX_FMT_NONE;
986 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
988 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
989 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
992 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
994 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
999 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
1000 enum AVPixelFormat pix_fmt)
1002 AVHWAccel *hwaccel = NULL;
1004 while ((hwaccel = av_hwaccel_next(hwaccel)))
1005 if (hwaccel->id == codec_id
1006 && hwaccel->pix_fmt == pix_fmt)
1011 static int setup_hwaccel(AVCodecContext *avctx,
1012 const enum AVPixelFormat fmt,
1015 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
1018 if (avctx->active_thread_type & FF_THREAD_FRAME) {
1019 av_log(avctx, AV_LOG_WARNING,
1020 "Hardware accelerated decoding with frame threading is known to be unstable and its use is discouraged.\n");
1024 av_log(avctx, AV_LOG_ERROR,
1025 "Could not find an AVHWAccel for the pixel format: %s",
1027 return AVERROR(ENOENT);
1030 if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1031 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1032 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1034 return AVERROR_PATCHWELCOME;
1037 if (hwa->priv_data_size) {
1038 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
1039 if (!avctx->internal->hwaccel_priv_data)
1040 return AVERROR(ENOMEM);
1044 ret = hwa->init(avctx);
1046 av_freep(&avctx->internal->hwaccel_priv_data);
1051 avctx->hwaccel = hwa;
1056 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1058 const AVPixFmtDescriptor *desc;
1059 enum AVPixelFormat *choices;
1060 enum AVPixelFormat ret;
1063 while (fmt[n] != AV_PIX_FMT_NONE)
1067 avctx->sw_pix_fmt = fmt[n - 1];
1068 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
1070 choices = av_malloc_array(n + 1, sizeof(*choices));
1072 return AV_PIX_FMT_NONE;
1074 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
1077 if (avctx->hwaccel && avctx->hwaccel->uninit)
1078 avctx->hwaccel->uninit(avctx);
1079 av_freep(&avctx->internal->hwaccel_priv_data);
1080 avctx->hwaccel = NULL;
1082 ret = avctx->get_format(avctx, choices);
1084 desc = av_pix_fmt_desc_get(ret);
1086 ret = AV_PIX_FMT_NONE;
1090 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1092 #if FF_API_CAP_VDPAU
1093 if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
1097 if (!setup_hwaccel(avctx, ret, desc->name))
1100 /* Remove failed hwaccel from choices */
1101 for (n = 0; choices[n] != ret; n++)
1102 av_assert0(choices[n] != AV_PIX_FMT_NONE);
1105 choices[n] = choices[n + 1];
1106 while (choices[n++] != AV_PIX_FMT_NONE);
1113 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1114 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1115 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1116 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1117 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1119 unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
1121 return codec->properties;
1124 int av_codec_get_max_lowres(const AVCodec *codec)
1126 return codec->max_lowres;
1129 static void get_subtitle_defaults(AVSubtitle *sub)
1131 memset(sub, 0, sizeof(*sub));
1132 sub->pts = AV_NOPTS_VALUE;
1135 static int64_t get_bit_rate(AVCodecContext *ctx)
1138 int bits_per_sample;
1140 switch (ctx->codec_type) {
1141 case AVMEDIA_TYPE_VIDEO:
1142 case AVMEDIA_TYPE_DATA:
1143 case AVMEDIA_TYPE_SUBTITLE:
1144 case AVMEDIA_TYPE_ATTACHMENT:
1145 bit_rate = ctx->bit_rate;
1147 case AVMEDIA_TYPE_AUDIO:
1148 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1149 bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
1158 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1162 ff_unlock_avcodec(codec);
1164 ret = avcodec_open2(avctx, codec, options);
1166 ff_lock_avcodec(avctx, codec);
1170 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1173 AVDictionary *tmp = NULL;
1174 const AVPixFmtDescriptor *pixdesc;
1176 if (avcodec_is_open(avctx))
1179 if ((!codec && !avctx->codec)) {
1180 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1181 return AVERROR(EINVAL);
1183 if ((codec && avctx->codec && codec != avctx->codec)) {
1184 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1185 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1186 return AVERROR(EINVAL);
1189 codec = avctx->codec;
1191 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1192 return AVERROR(EINVAL);
1195 av_dict_copy(&tmp, *options, 0);
1197 ret = ff_lock_avcodec(avctx, codec);
1201 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1202 if (!avctx->internal) {
1203 ret = AVERROR(ENOMEM);
1207 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1208 if (!avctx->internal->pool) {
1209 ret = AVERROR(ENOMEM);
1213 avctx->internal->to_free = av_frame_alloc();
1214 if (!avctx->internal->to_free) {
1215 ret = AVERROR(ENOMEM);
1219 if (codec->priv_data_size > 0) {
1220 if (!avctx->priv_data) {
1221 avctx->priv_data = av_mallocz(codec->priv_data_size);
1222 if (!avctx->priv_data) {
1223 ret = AVERROR(ENOMEM);
1226 if (codec->priv_class) {
1227 *(const AVClass **)avctx->priv_data = codec->priv_class;
1228 av_opt_set_defaults(avctx->priv_data);
1231 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1234 avctx->priv_data = NULL;
1236 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1239 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
1240 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
1241 ret = AVERROR(EINVAL);
1245 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
1246 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1247 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
1248 if (avctx->coded_width && avctx->coded_height)
1249 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1250 else if (avctx->width && avctx->height)
1251 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1256 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1257 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1258 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1259 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1260 ff_set_dimensions(avctx, 0, 0);
1263 if (avctx->width > 0 && avctx->height > 0) {
1264 if (av_image_check_sar(avctx->width, avctx->height,
1265 avctx->sample_aspect_ratio) < 0) {
1266 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1267 avctx->sample_aspect_ratio.num,
1268 avctx->sample_aspect_ratio.den);
1269 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1273 /* if the decoder init function was already called previously,
1274 * free the already allocated subtitle_header before overwriting it */
1275 if (av_codec_is_decoder(codec))
1276 av_freep(&avctx->subtitle_header);
1278 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1279 ret = AVERROR(EINVAL);
1283 avctx->codec = codec;
1284 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1285 avctx->codec_id == AV_CODEC_ID_NONE) {
1286 avctx->codec_type = codec->type;
1287 avctx->codec_id = codec->id;
1289 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1290 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1291 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1292 ret = AVERROR(EINVAL);
1295 avctx->frame_number = 0;
1296 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1298 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
1299 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1300 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1302 av_log(avctx, AV_LOG_ERROR,
1303 "The %s '%s' is experimental but experimental codecs are not enabled, "
1304 "add '-strict %d' if you want to use it.\n",
1305 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1306 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1307 if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
1308 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1309 codec_string, codec2->name);
1310 ret = AVERROR_EXPERIMENTAL;
1314 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1315 (!avctx->time_base.num || !avctx->time_base.den)) {
1316 avctx->time_base.num = 1;
1317 avctx->time_base.den = avctx->sample_rate;
1321 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1323 if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
1324 ff_unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
1325 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1326 ff_lock_avcodec(avctx, codec);
1332 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1333 ret = ff_thread_init(avctx);
1338 if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
1339 avctx->thread_count = 1;
1341 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1342 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1343 avctx->codec->max_lowres);
1344 ret = AVERROR(EINVAL);
1349 if (avctx->debug_mv)
1350 av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
1351 "see the codecview filter instead.\n");
1354 if (av_codec_is_encoder(avctx->codec)) {
1356 #if FF_API_CODED_FRAME
1357 FF_DISABLE_DEPRECATION_WARNINGS
1358 avctx->coded_frame = av_frame_alloc();
1359 if (!avctx->coded_frame) {
1360 ret = AVERROR(ENOMEM);
1363 FF_ENABLE_DEPRECATION_WARNINGS
1365 if (avctx->codec->sample_fmts) {
1366 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1367 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1369 if (avctx->channels == 1 &&
1370 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1371 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1372 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1376 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1378 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1379 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1380 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1381 ret = AVERROR(EINVAL);
1385 if (avctx->codec->pix_fmts) {
1386 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1387 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1389 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1390 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1391 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1393 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1394 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1395 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1396 ret = AVERROR(EINVAL);
1399 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1400 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
1401 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1402 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1403 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1404 avctx->color_range = AVCOL_RANGE_JPEG;
1406 if (avctx->codec->supported_samplerates) {
1407 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1408 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1410 if (avctx->codec->supported_samplerates[i] == 0) {
1411 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1412 avctx->sample_rate);
1413 ret = AVERROR(EINVAL);
1417 if (avctx->sample_rate < 0) {
1418 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1419 avctx->sample_rate);
1420 ret = AVERROR(EINVAL);
1423 if (avctx->codec->channel_layouts) {
1424 if (!avctx->channel_layout) {
1425 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1427 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1428 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1430 if (avctx->codec->channel_layouts[i] == 0) {
1432 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1433 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1434 ret = AVERROR(EINVAL);
1439 if (avctx->channel_layout && avctx->channels) {
1440 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1441 if (channels != avctx->channels) {
1443 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1444 av_log(avctx, AV_LOG_ERROR,
1445 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1446 buf, channels, avctx->channels);
1447 ret = AVERROR(EINVAL);
1450 } else if (avctx->channel_layout) {
1451 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1453 if (avctx->channels < 0) {
1454 av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
1456 ret = AVERROR(EINVAL);
1459 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1460 pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
1461 if ( avctx->bits_per_raw_sample < 0
1462 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
1463 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
1464 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
1465 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
1467 if (avctx->width <= 0 || avctx->height <= 0) {
1468 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1469 ret = AVERROR(EINVAL);
1473 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1474 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1475 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", (int64_t)avctx->bit_rate, (int64_t)avctx->bit_rate);
1478 if (!avctx->rc_initial_buffer_occupancy)
1479 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1481 if (avctx->ticks_per_frame && avctx->time_base.num &&
1482 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
1483 av_log(avctx, AV_LOG_ERROR,
1484 "ticks_per_frame %d too large for the timebase %d/%d.",
1485 avctx->ticks_per_frame,
1486 avctx->time_base.num,
1487 avctx->time_base.den);
1492 avctx->pts_correction_num_faulty_pts =
1493 avctx->pts_correction_num_faulty_dts = 0;
1494 avctx->pts_correction_last_pts =
1495 avctx->pts_correction_last_dts = INT64_MIN;
1497 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
1498 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
1499 av_log(avctx, AV_LOG_WARNING,
1500 "gray decoding requested but not enabled at configuration time\n");
1502 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1503 || avctx->internal->frame_thread_encoder)) {
1504 ret = avctx->codec->init(avctx);
1512 #if FF_API_AUDIOENC_DELAY
1513 if (av_codec_is_encoder(avctx->codec))
1514 avctx->delay = avctx->initial_padding;
1517 if (av_codec_is_decoder(avctx->codec)) {
1518 if (!avctx->bit_rate)
1519 avctx->bit_rate = get_bit_rate(avctx);
1520 /* validate channel layout from the decoder */
1521 if (avctx->channel_layout) {
1522 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1523 if (!avctx->channels)
1524 avctx->channels = channels;
1525 else if (channels != avctx->channels) {
1527 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1528 av_log(avctx, AV_LOG_WARNING,
1529 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1530 "ignoring specified channel layout\n",
1531 buf, channels, avctx->channels);
1532 avctx->channel_layout = 0;
1535 if (avctx->channels && avctx->channels < 0 ||
1536 avctx->channels > FF_SANE_NB_CHANNELS) {
1537 ret = AVERROR(EINVAL);
1540 if (avctx->sub_charenc) {
1541 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1542 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1543 "supported with subtitles codecs\n");
1544 ret = AVERROR(EINVAL);
1546 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1547 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1548 "subtitles character encoding will be ignored\n",
1549 avctx->codec_descriptor->name);
1550 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1552 /* input character encoding is set for a text based subtitle
1553 * codec at this point */
1554 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1555 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1557 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1559 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1560 if (cd == (iconv_t)-1) {
1561 ret = AVERROR(errno);
1562 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1563 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1568 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1569 "conversion needs a libavcodec built with iconv support "
1570 "for this codec\n");
1571 ret = AVERROR(ENOSYS);
1578 #if FF_API_AVCTX_TIMEBASE
1579 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1580 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
1583 if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
1584 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
1588 ff_unlock_avcodec(codec);
1590 av_dict_free(options);
1597 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1598 avctx->codec->close(avctx);
1600 if (codec->priv_class && codec->priv_data_size)
1601 av_opt_free(avctx->priv_data);
1604 #if FF_API_CODED_FRAME
1605 FF_DISABLE_DEPRECATION_WARNINGS
1606 av_frame_free(&avctx->coded_frame);
1607 FF_ENABLE_DEPRECATION_WARNINGS
1611 av_freep(&avctx->priv_data);
1612 if (avctx->internal) {
1613 av_frame_free(&avctx->internal->to_free);
1614 av_freep(&avctx->internal->pool);
1616 av_freep(&avctx->internal);
1617 avctx->codec = NULL;
1621 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
1623 if (avpkt->size < 0) {
1624 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1625 return AVERROR(EINVAL);
1627 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
1628 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1629 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
1630 return AVERROR(EINVAL);
1633 if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
1634 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1635 if (!avpkt->data || avpkt->size < size) {
1636 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1637 avpkt->data = avctx->internal->byte_buffer;
1638 avpkt->size = avctx->internal->byte_buffer_size;
1643 AVBufferRef *buf = avpkt->buf;
1645 if (avpkt->size < size) {
1646 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1647 return AVERROR(EINVAL);
1650 av_init_packet(avpkt);
1655 int ret = av_new_packet(avpkt, size);
1657 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1662 int ff_alloc_packet(AVPacket *avpkt, int size)
1664 return ff_alloc_packet2(NULL, avpkt, size, 0);
1668 * Pad last frame with silence.
1670 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1672 AVFrame *frame = NULL;
1675 if (!(frame = av_frame_alloc()))
1676 return AVERROR(ENOMEM);
1678 frame->format = src->format;
1679 frame->channel_layout = src->channel_layout;
1680 av_frame_set_channels(frame, av_frame_get_channels(src));
1681 frame->nb_samples = s->frame_size;
1682 ret = av_frame_get_buffer(frame, 32);
1686 ret = av_frame_copy_props(frame, src);
1690 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1691 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1693 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1694 frame->nb_samples - src->nb_samples,
1695 s->channels, s->sample_fmt)) < 0)
1703 av_frame_free(&frame);
1707 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1709 const AVFrame *frame,
1710 int *got_packet_ptr)
1712 AVFrame *extended_frame = NULL;
1713 AVFrame *padded_frame = NULL;
1715 AVPacket user_pkt = *avpkt;
1716 int needs_realloc = !user_pkt.data;
1718 *got_packet_ptr = 0;
1720 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1721 av_packet_unref(avpkt);
1722 av_init_packet(avpkt);
1726 /* ensure that extended_data is properly set */
1727 if (frame && !frame->extended_data) {
1728 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1729 avctx->channels > AV_NUM_DATA_POINTERS) {
1730 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1731 "with more than %d channels, but extended_data is not set.\n",
1732 AV_NUM_DATA_POINTERS);
1733 return AVERROR(EINVAL);
1735 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1737 extended_frame = av_frame_alloc();
1738 if (!extended_frame)
1739 return AVERROR(ENOMEM);
1741 memcpy(extended_frame, frame, sizeof(AVFrame));
1742 extended_frame->extended_data = extended_frame->data;
1743 frame = extended_frame;
1746 /* extract audio service type metadata */
1748 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
1749 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1750 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1753 /* check for valid frame size */
1755 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
1756 if (frame->nb_samples > avctx->frame_size) {
1757 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1758 ret = AVERROR(EINVAL);
1761 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1762 if (frame->nb_samples < avctx->frame_size &&
1763 !avctx->internal->last_audio_frame) {
1764 ret = pad_last_frame(avctx, &padded_frame, frame);
1768 frame = padded_frame;
1769 avctx->internal->last_audio_frame = 1;
1772 if (frame->nb_samples != avctx->frame_size) {
1773 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1774 ret = AVERROR(EINVAL);
1780 av_assert0(avctx->codec->encode2);
1782 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1784 if (*got_packet_ptr) {
1785 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
1786 if (avpkt->pts == AV_NOPTS_VALUE)
1787 avpkt->pts = frame->pts;
1788 if (!avpkt->duration)
1789 avpkt->duration = ff_samples_to_time_base(avctx,
1792 avpkt->dts = avpkt->pts;
1797 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1799 if (user_pkt.data) {
1800 if (user_pkt.size >= avpkt->size) {
1801 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1803 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1804 avpkt->size = user_pkt.size;
1807 avpkt->buf = user_pkt.buf;
1808 avpkt->data = user_pkt.data;
1810 if (av_dup_packet(avpkt) < 0) {
1811 ret = AVERROR(ENOMEM);
1817 if (needs_realloc && avpkt->data) {
1818 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
1820 avpkt->data = avpkt->buf->data;
1823 avctx->frame_number++;
1826 if (ret < 0 || !*got_packet_ptr) {
1827 av_packet_unref(avpkt);
1828 av_init_packet(avpkt);
1832 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1833 * this needs to be moved to the encoders, but for now we can do it
1834 * here to simplify things */
1835 avpkt->flags |= AV_PKT_FLAG_KEY;
1838 av_frame_free(&padded_frame);
1839 av_free(extended_frame);
1841 #if FF_API_AUDIOENC_DELAY
1842 avctx->delay = avctx->initial_padding;
1848 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1850 const AVFrame *frame,
1851 int *got_packet_ptr)
1854 AVPacket user_pkt = *avpkt;
1855 int needs_realloc = !user_pkt.data;
1857 *got_packet_ptr = 0;
1859 if(CONFIG_FRAME_THREAD_ENCODER &&
1860 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1861 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1863 if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
1864 avctx->stats_out[0] = '\0';
1866 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
1867 av_packet_unref(avpkt);
1868 av_init_packet(avpkt);
1873 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1874 return AVERROR(EINVAL);
1876 if (frame && frame->format == AV_PIX_FMT_NONE)
1877 av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
1878 if (frame && (frame->width == 0 || frame->height == 0))
1879 av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
1881 av_assert0(avctx->codec->encode2);
1883 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1884 av_assert0(ret <= 0);
1886 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1888 if (user_pkt.data) {
1889 if (user_pkt.size >= avpkt->size) {
1890 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1892 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1893 avpkt->size = user_pkt.size;
1896 avpkt->buf = user_pkt.buf;
1897 avpkt->data = user_pkt.data;
1899 if (av_dup_packet(avpkt) < 0) {
1900 ret = AVERROR(ENOMEM);
1906 if (!*got_packet_ptr)
1908 else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
1909 avpkt->pts = avpkt->dts = frame->pts;
1911 if (needs_realloc && avpkt->data) {
1912 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
1914 avpkt->data = avpkt->buf->data;
1917 avctx->frame_number++;
1920 if (ret < 0 || !*got_packet_ptr)
1921 av_packet_unref(avpkt);
1927 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1928 const AVSubtitle *sub)
1931 if (sub->start_display_time) {
1932 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1936 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1937 avctx->frame_number++;
1942 * Attempt to guess proper monotonic timestamps for decoded video frames
1943 * which might have incorrect times. Input timestamps may wrap around, in
1944 * which case the output will as well.
1946 * @param pts the pts field of the decoded AVPacket, as passed through
1948 * @param dts the dts field of the decoded AVPacket
1949 * @return one of the input values, may be AV_NOPTS_VALUE
1951 static int64_t guess_correct_pts(AVCodecContext *ctx,
1952 int64_t reordered_pts, int64_t dts)
1954 int64_t pts = AV_NOPTS_VALUE;
1956 if (dts != AV_NOPTS_VALUE) {
1957 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1958 ctx->pts_correction_last_dts = dts;
1959 } else if (reordered_pts != AV_NOPTS_VALUE)
1960 ctx->pts_correction_last_dts = reordered_pts;
1962 if (reordered_pts != AV_NOPTS_VALUE) {
1963 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1964 ctx->pts_correction_last_pts = reordered_pts;
1965 } else if(dts != AV_NOPTS_VALUE)
1966 ctx->pts_correction_last_pts = dts;
1968 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1969 && reordered_pts != AV_NOPTS_VALUE)
1970 pts = reordered_pts;
1977 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1980 const uint8_t *data;
1984 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1988 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
1989 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1990 "changes, but PARAM_CHANGE side data was sent to it.\n");
1991 return AVERROR(EINVAL);
1997 flags = bytestream_get_le32(&data);
2000 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
2003 val = bytestream_get_le32(&data);
2004 if (val <= 0 || val > INT_MAX) {
2005 av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
2006 return AVERROR_INVALIDDATA;
2008 avctx->channels = val;
2011 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
2014 avctx->channel_layout = bytestream_get_le64(&data);
2017 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
2020 val = bytestream_get_le32(&data);
2021 if (val <= 0 || val > INT_MAX) {
2022 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
2023 return AVERROR_INVALIDDATA;
2025 avctx->sample_rate = val;
2028 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
2031 avctx->width = bytestream_get_le32(&data);
2032 avctx->height = bytestream_get_le32(&data);
2034 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2041 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2042 return AVERROR_INVALIDDATA;
2045 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2049 /* move the original frame to our backup */
2050 av_frame_unref(avci->to_free);
2051 av_frame_move_ref(avci->to_free, frame);
2053 /* now copy everything except the AVBufferRefs back
2054 * note that we make a COPY of the side data, so calling av_frame_free() on
2055 * the caller's frame will work properly */
2056 ret = av_frame_copy_props(frame, avci->to_free);
2060 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2061 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2062 if (avci->to_free->extended_data != avci->to_free->data) {
2063 int planes = av_frame_get_channels(avci->to_free);
2064 int size = planes * sizeof(*frame->extended_data);
2067 av_frame_unref(frame);
2071 frame->extended_data = av_malloc(size);
2072 if (!frame->extended_data) {
2073 av_frame_unref(frame);
2074 return AVERROR(ENOMEM);
2076 memcpy(frame->extended_data, avci->to_free->extended_data,
2079 frame->extended_data = frame->data;
2081 frame->format = avci->to_free->format;
2082 frame->width = avci->to_free->width;
2083 frame->height = avci->to_free->height;
2084 frame->channel_layout = avci->to_free->channel_layout;
2085 frame->nb_samples = avci->to_free->nb_samples;
2086 av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
2091 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2092 int *got_picture_ptr,
2093 const AVPacket *avpkt)
2095 AVCodecInternal *avci = avctx->internal;
2097 // copy to ensure we do not change avpkt
2098 AVPacket tmp = *avpkt;
2101 return AVERROR(EINVAL);
2102 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2103 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2104 return AVERROR(EINVAL);
2107 *got_picture_ptr = 0;
2108 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2109 return AVERROR(EINVAL);
2111 av_frame_unref(picture);
2113 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
2114 (avctx->active_thread_type & FF_THREAD_FRAME)) {
2115 int did_split = av_packet_split_side_data(&tmp);
2116 ret = apply_param_change(avctx, &tmp);
2118 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2119 if (avctx->err_recognition & AV_EF_EXPLODE)
2123 avctx->internal->pkt = &tmp;
2124 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2125 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2128 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2130 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
2131 picture->pkt_dts = avpkt->dts;
2133 if(!avctx->has_b_frames){
2134 av_frame_set_pkt_pos(picture, avpkt->pos);
2136 //FIXME these should be under if(!avctx->has_b_frames)
2137 /* get_buffer is supposed to set frame parameters */
2138 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
2139 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2140 if (!picture->width) picture->width = avctx->width;
2141 if (!picture->height) picture->height = avctx->height;
2142 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2147 emms_c(); //needed to avoid an emms_c() call before every return;
2149 avctx->internal->pkt = NULL;
2151 av_packet_free_side_data(&tmp);
2156 if (*got_picture_ptr) {
2157 if (!avctx->refcounted_frames) {
2158 int err = unrefcount_frame(avci, picture);
2163 avctx->frame_number++;
2164 av_frame_set_best_effort_timestamp(picture,
2165 guess_correct_pts(avctx,
2169 av_frame_unref(picture);
2173 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2174 * make sure it's set correctly */
2175 av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2177 #if FF_API_AVCTX_TIMEBASE
2178 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
2179 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
2185 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2188 const AVPacket *avpkt)
2190 AVCodecInternal *avci = avctx->internal;
2195 if (!avpkt->data && avpkt->size) {
2196 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2197 return AVERROR(EINVAL);
2200 return AVERROR(EINVAL);
2201 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2202 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2203 return AVERROR(EINVAL);
2206 av_frame_unref(frame);
2208 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2211 uint32_t discard_padding = 0;
2212 uint8_t skip_reason = 0;
2213 uint8_t discard_reason = 0;
2214 // copy to ensure we do not change avpkt
2215 AVPacket tmp = *avpkt;
2216 int did_split = av_packet_split_side_data(&tmp);
2217 ret = apply_param_change(avctx, &tmp);
2219 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2220 if (avctx->err_recognition & AV_EF_EXPLODE)
2224 avctx->internal->pkt = &tmp;
2225 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2226 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2228 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2229 av_assert0(ret <= tmp.size);
2230 frame->pkt_dts = avpkt->dts;
2232 if (ret >= 0 && *got_frame_ptr) {
2233 avctx->frame_number++;
2234 av_frame_set_best_effort_timestamp(frame,
2235 guess_correct_pts(avctx,
2238 if (frame->format == AV_SAMPLE_FMT_NONE)
2239 frame->format = avctx->sample_fmt;
2240 if (!frame->channel_layout)
2241 frame->channel_layout = avctx->channel_layout;
2242 if (!av_frame_get_channels(frame))
2243 av_frame_set_channels(frame, avctx->channels);
2244 if (!frame->sample_rate)
2245 frame->sample_rate = avctx->sample_rate;
2248 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2249 if(side && side_size>=10) {
2250 avctx->internal->skip_samples = AV_RL32(side);
2251 discard_padding = AV_RL32(side + 4);
2252 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
2253 avctx->internal->skip_samples, (int)discard_padding);
2254 skip_reason = AV_RL8(side + 8);
2255 discard_reason = AV_RL8(side + 9);
2257 if (avctx->internal->skip_samples && *got_frame_ptr &&
2258 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
2259 if(frame->nb_samples <= avctx->internal->skip_samples){
2261 avctx->internal->skip_samples -= frame->nb_samples;
2262 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2263 avctx->internal->skip_samples);
2265 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2266 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2267 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2268 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2269 (AVRational){1, avctx->sample_rate},
2270 avctx->pkt_timebase);
2271 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2272 frame->pkt_pts += diff_ts;
2273 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2274 frame->pkt_dts += diff_ts;
2275 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2276 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2278 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2280 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2281 avctx->internal->skip_samples, frame->nb_samples);
2282 frame->nb_samples -= avctx->internal->skip_samples;
2283 avctx->internal->skip_samples = 0;
2287 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
2288 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
2289 if (discard_padding == frame->nb_samples) {
2292 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2293 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2294 (AVRational){1, avctx->sample_rate},
2295 avctx->pkt_timebase);
2296 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2297 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2299 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2301 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2302 (int)discard_padding, frame->nb_samples);
2303 frame->nb_samples -= discard_padding;
2307 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
2308 AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
2310 AV_WL32(fside->data, avctx->internal->skip_samples);
2311 AV_WL32(fside->data + 4, discard_padding);
2312 AV_WL8(fside->data + 8, skip_reason);
2313 AV_WL8(fside->data + 9, discard_reason);
2314 avctx->internal->skip_samples = 0;
2318 avctx->internal->pkt = NULL;
2320 av_packet_free_side_data(&tmp);
2325 if (ret >= 0 && *got_frame_ptr) {
2326 if (!avctx->refcounted_frames) {
2327 int err = unrefcount_frame(avci, frame);
2332 av_frame_unref(frame);
2338 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2339 static int recode_subtitle(AVCodecContext *avctx,
2340 AVPacket *outpkt, const AVPacket *inpkt)
2343 iconv_t cd = (iconv_t)-1;
2350 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2354 cd = iconv_open("UTF-8", avctx->sub_charenc);
2355 av_assert0(cd != (iconv_t)-1);
2360 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
2361 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2362 ret = AVERROR(ENOMEM);
2366 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2369 outpkt->buf = tmp.buf;
2370 outpkt->data = tmp.data;
2371 outpkt->size = tmp.size;
2372 outb = outpkt->data;
2373 outl = outpkt->size;
2375 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2376 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2377 outl >= outpkt->size || inl != 0) {
2378 ret = FFMIN(AVERROR(errno), -1);
2379 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2380 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2381 av_packet_unref(&tmp);
2384 outpkt->size -= outl;
2385 memset(outpkt->data + outpkt->size, 0, outl);
2388 if (cd != (iconv_t)-1)
2392 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
2393 return AVERROR(EINVAL);
2397 static int utf8_check(const uint8_t *str)
2399 const uint8_t *byte;
2400 uint32_t codepoint, min;
2404 GET_UTF8(codepoint, *(byte++), return 0;);
2405 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2406 1 << (5 * (byte - str) - 4);
2407 if (codepoint < min || codepoint >= 0x110000 ||
2408 codepoint == 0xFFFE /* BOM */ ||
2409 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2416 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2422 if (!avpkt->data && avpkt->size) {
2423 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2424 return AVERROR(EINVAL);
2427 return AVERROR(EINVAL);
2428 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2429 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2430 return AVERROR(EINVAL);
2434 get_subtitle_defaults(sub);
2436 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
2437 AVPacket pkt_recoded;
2438 AVPacket tmp = *avpkt;
2439 int did_split = av_packet_split_side_data(&tmp);
2440 //apply_param_change(avctx, &tmp);
2443 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2445 * If the side data is smaller than the buffer padding size, the
2446 * remaining bytes should have already been filled with zeros by the
2447 * original packet allocation anyway. */
2448 memset(tmp.data + tmp.size, 0,
2449 FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
2453 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2457 avctx->internal->pkt = &pkt_recoded;
2459 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
2460 sub->pts = av_rescale_q(avpkt->pts,
2461 avctx->pkt_timebase, AV_TIME_BASE_Q);
2462 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2463 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2464 !!*got_sub_ptr >= !!sub->num_rects);
2466 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2467 avctx->pkt_timebase.num) {
2468 AVRational ms = { 1, 1000 };
2469 sub->end_display_time = av_rescale_q(avpkt->duration,
2470 avctx->pkt_timebase, ms);
2473 for (i = 0; i < sub->num_rects; i++) {
2474 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2475 av_log(avctx, AV_LOG_ERROR,
2476 "Invalid UTF-8 in decoded subtitles text; "
2477 "maybe missing -sub_charenc option\n");
2478 avsubtitle_free(sub);
2479 return AVERROR_INVALIDDATA;
2483 if (tmp.data != pkt_recoded.data) { // did we recode?
2484 /* prevent from destroying side data from original packet */
2485 pkt_recoded.side_data = NULL;
2486 pkt_recoded.side_data_elems = 0;
2488 av_packet_unref(&pkt_recoded);
2490 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2492 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2494 avctx->internal->pkt = NULL;
2498 av_packet_free_side_data(&tmp);
2504 avctx->frame_number++;
2510 void avsubtitle_free(AVSubtitle *sub)
2514 for (i = 0; i < sub->num_rects; i++) {
2515 av_freep(&sub->rects[i]->data[0]);
2516 av_freep(&sub->rects[i]->data[1]);
2517 av_freep(&sub->rects[i]->data[2]);
2518 av_freep(&sub->rects[i]->data[3]);
2519 av_freep(&sub->rects[i]->text);
2520 av_freep(&sub->rects[i]->ass);
2521 av_freep(&sub->rects[i]);
2524 av_freep(&sub->rects);
2526 memset(sub, 0, sizeof(AVSubtitle));
2529 av_cold int avcodec_close(AVCodecContext *avctx)
2536 if (avcodec_is_open(avctx)) {
2537 FramePool *pool = avctx->internal->pool;
2538 if (CONFIG_FRAME_THREAD_ENCODER &&
2539 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2540 ff_frame_thread_encoder_free(avctx);
2542 if (HAVE_THREADS && avctx->internal->thread_ctx)
2543 ff_thread_free(avctx);
2544 if (avctx->codec && avctx->codec->close)
2545 avctx->codec->close(avctx);
2546 avctx->internal->byte_buffer_size = 0;
2547 av_freep(&avctx->internal->byte_buffer);
2548 av_frame_free(&avctx->internal->to_free);
2549 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2550 av_buffer_pool_uninit(&pool->pools[i]);
2551 av_freep(&avctx->internal->pool);
2553 if (avctx->hwaccel && avctx->hwaccel->uninit)
2554 avctx->hwaccel->uninit(avctx);
2555 av_freep(&avctx->internal->hwaccel_priv_data);
2557 av_freep(&avctx->internal);
2560 for (i = 0; i < avctx->nb_coded_side_data; i++)
2561 av_freep(&avctx->coded_side_data[i].data);
2562 av_freep(&avctx->coded_side_data);
2563 avctx->nb_coded_side_data = 0;
2565 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2566 av_opt_free(avctx->priv_data);
2568 av_freep(&avctx->priv_data);
2569 if (av_codec_is_encoder(avctx->codec)) {
2570 av_freep(&avctx->extradata);
2571 #if FF_API_CODED_FRAME
2572 FF_DISABLE_DEPRECATION_WARNINGS
2573 av_frame_free(&avctx->coded_frame);
2574 FF_ENABLE_DEPRECATION_WARNINGS
2577 avctx->codec = NULL;
2578 avctx->active_thread_type = 0;
2583 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2586 //This is for future deprecatec codec ids, its empty since
2587 //last major bump but will fill up again over time, please don't remove it
2588 default : return id;
2592 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2594 AVCodec *p, *experimental = NULL;
2596 id= remap_deprecated_codec_id(id);
2598 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2600 if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
2607 return experimental;
2610 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2612 return find_encdec(id, 1);
2615 AVCodec *avcodec_find_encoder_by_name(const char *name)
2622 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2629 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2631 return find_encdec(id, 0);
2634 AVCodec *avcodec_find_decoder_by_name(const char *name)
2641 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2648 const char *avcodec_get_name(enum AVCodecID id)
2650 const AVCodecDescriptor *cd;
2653 if (id == AV_CODEC_ID_NONE)
2655 cd = avcodec_descriptor_get(id);
2658 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2659 codec = avcodec_find_decoder(id);
2662 codec = avcodec_find_encoder(id);
2665 return "unknown_codec";
2668 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2670 int i, len, ret = 0;
2672 #define TAG_PRINT(x) \
2673 (((x) >= '0' && (x) <= '9') || \
2674 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2675 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2677 for (i = 0; i < 4; i++) {
2678 len = snprintf(buf, buf_size,
2679 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2681 buf_size = buf_size > len ? buf_size - len : 0;
2688 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2690 const char *codec_type;
2691 const char *codec_name;
2692 const char *profile = NULL;
2695 AVRational display_aspect_ratio;
2696 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
2698 if (!buf || buf_size <= 0)
2700 codec_type = av_get_media_type_string(enc->codec_type);
2701 codec_name = avcodec_get_name(enc->codec_id);
2702 profile = avcodec_profile_name(enc->codec_id, enc->profile);
2704 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2706 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2708 if (enc->codec && strcmp(enc->codec->name, codec_name))
2709 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2712 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2713 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
2714 && av_log_get_level() >= AV_LOG_VERBOSE
2716 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2717 ", %d reference frame%s",
2718 enc->refs, enc->refs > 1 ? "s" : "");
2720 if (enc->codec_tag) {
2722 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2723 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2724 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2727 switch (enc->codec_type) {
2728 case AVMEDIA_TYPE_VIDEO:
2730 char detail[256] = "(";
2732 av_strlcat(buf, separator, buf_size);
2734 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2735 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
2736 av_get_pix_fmt_name(enc->pix_fmt));
2737 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
2738 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
2739 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2740 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2741 av_strlcatf(detail, sizeof(detail), "%s, ",
2742 av_color_range_name(enc->color_range));
2744 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
2745 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
2746 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
2747 if (enc->colorspace != (int)enc->color_primaries ||
2748 enc->colorspace != (int)enc->color_trc) {
2750 av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
2751 av_color_space_name(enc->colorspace),
2752 av_color_primaries_name(enc->color_primaries),
2753 av_color_transfer_name(enc->color_trc));
2755 av_strlcatf(detail, sizeof(detail), "%s, ",
2756 av_get_colorspace_name(enc->colorspace));
2759 if (av_log_get_level() >= AV_LOG_DEBUG &&
2760 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
2761 av_strlcatf(detail, sizeof(detail), "%s, ",
2762 av_chroma_location_name(enc->chroma_sample_location));
2764 if (strlen(detail) > 1) {
2765 detail[strlen(detail) - 2] = 0;
2766 av_strlcatf(buf, buf_size, "%s)", detail);
2771 av_strlcat(buf, new_line ? separator : ", ", buf_size);
2773 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2775 enc->width, enc->height);
2777 if (av_log_get_level() >= AV_LOG_VERBOSE &&
2778 (enc->width != enc->coded_width ||
2779 enc->height != enc->coded_height))
2780 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2781 " (%dx%d)", enc->coded_width, enc->coded_height);
2783 if (enc->sample_aspect_ratio.num) {
2784 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2785 enc->width * (int64_t)enc->sample_aspect_ratio.num,
2786 enc->height * (int64_t)enc->sample_aspect_ratio.den,
2788 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2789 " [SAR %d:%d DAR %d:%d]",
2790 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2791 display_aspect_ratio.num, display_aspect_ratio.den);
2793 if (av_log_get_level() >= AV_LOG_DEBUG) {
2794 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2795 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2797 enc->time_base.num / g, enc->time_base.den / g);
2801 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2802 ", q=%d-%d", enc->qmin, enc->qmax);
2804 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
2805 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2806 ", Closed Captions");
2807 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
2808 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2812 case AVMEDIA_TYPE_AUDIO:
2813 av_strlcat(buf, separator, buf_size);
2815 if (enc->sample_rate) {
2816 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2817 "%d Hz, ", enc->sample_rate);
2819 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2820 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2821 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2822 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2824 if ( enc->bits_per_raw_sample > 0
2825 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
2826 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2827 " (%d bit)", enc->bits_per_raw_sample);
2829 case AVMEDIA_TYPE_DATA:
2830 if (av_log_get_level() >= AV_LOG_DEBUG) {
2831 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2833 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2835 enc->time_base.num / g, enc->time_base.den / g);
2838 case AVMEDIA_TYPE_SUBTITLE:
2840 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2841 ", %dx%d", enc->width, enc->height);
2847 if (enc->flags & AV_CODEC_FLAG_PASS1)
2848 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2850 if (enc->flags & AV_CODEC_FLAG_PASS2)
2851 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2854 bitrate = get_bit_rate(enc);
2856 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2857 ", %"PRId64" kb/s", bitrate / 1000);
2858 } else if (enc->rc_max_rate > 0) {
2859 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2860 ", max. %"PRId64" kb/s", (int64_t)enc->rc_max_rate / 1000);
2864 const char *av_get_profile_name(const AVCodec *codec, int profile)
2867 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2870 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2871 if (p->profile == profile)
2877 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
2879 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
2882 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
2885 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2886 if (p->profile == profile)
2892 unsigned avcodec_version(void)
2894 // av_assert0(AV_CODEC_ID_V410==164);
2895 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2896 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2897 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2898 av_assert0(AV_CODEC_ID_SRT==94216);
2899 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2901 return LIBAVCODEC_VERSION_INT;
2904 const char *avcodec_configuration(void)
2906 return FFMPEG_CONFIGURATION;
2909 const char *avcodec_license(void)
2911 #define LICENSE_PREFIX "libavcodec license: "
2912 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2915 void avcodec_flush_buffers(AVCodecContext *avctx)
2917 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2918 ff_thread_flush(avctx);
2919 else if (avctx->codec->flush)
2920 avctx->codec->flush(avctx);
2922 avctx->pts_correction_last_pts =
2923 avctx->pts_correction_last_dts = INT64_MIN;
2925 if (!avctx->refcounted_frames)
2926 av_frame_unref(avctx->internal->to_free);
2929 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2932 case AV_CODEC_ID_8SVX_EXP:
2933 case AV_CODEC_ID_8SVX_FIB:
2934 case AV_CODEC_ID_ADPCM_CT:
2935 case AV_CODEC_ID_ADPCM_IMA_APC:
2936 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2937 case AV_CODEC_ID_ADPCM_IMA_OKI:
2938 case AV_CODEC_ID_ADPCM_IMA_WS:
2939 case AV_CODEC_ID_ADPCM_G722:
2940 case AV_CODEC_ID_ADPCM_YAMAHA:
2941 case AV_CODEC_ID_ADPCM_AICA:
2943 case AV_CODEC_ID_DSD_LSBF:
2944 case AV_CODEC_ID_DSD_MSBF:
2945 case AV_CODEC_ID_DSD_LSBF_PLANAR:
2946 case AV_CODEC_ID_DSD_MSBF_PLANAR:
2947 case AV_CODEC_ID_PCM_ALAW:
2948 case AV_CODEC_ID_PCM_MULAW:
2949 case AV_CODEC_ID_PCM_S8:
2950 case AV_CODEC_ID_PCM_S8_PLANAR:
2951 case AV_CODEC_ID_PCM_U8:
2952 case AV_CODEC_ID_PCM_ZORK:
2953 case AV_CODEC_ID_SDX2_DPCM:
2955 case AV_CODEC_ID_PCM_S16BE:
2956 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2957 case AV_CODEC_ID_PCM_S16LE:
2958 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2959 case AV_CODEC_ID_PCM_U16BE:
2960 case AV_CODEC_ID_PCM_U16LE:
2962 case AV_CODEC_ID_PCM_S24DAUD:
2963 case AV_CODEC_ID_PCM_S24BE:
2964 case AV_CODEC_ID_PCM_S24LE:
2965 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2966 case AV_CODEC_ID_PCM_U24BE:
2967 case AV_CODEC_ID_PCM_U24LE:
2969 case AV_CODEC_ID_PCM_S32BE:
2970 case AV_CODEC_ID_PCM_S32LE:
2971 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2972 case AV_CODEC_ID_PCM_U32BE:
2973 case AV_CODEC_ID_PCM_U32LE:
2974 case AV_CODEC_ID_PCM_F32BE:
2975 case AV_CODEC_ID_PCM_F32LE:
2977 case AV_CODEC_ID_PCM_F64BE:
2978 case AV_CODEC_ID_PCM_F64LE:
2985 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2987 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2988 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2989 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2990 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2991 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2992 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2993 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2994 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2995 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2996 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2997 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2999 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3000 return AV_CODEC_ID_NONE;
3001 if (be < 0 || be > 1)
3003 return map[fmt][be];
3006 int av_get_bits_per_sample(enum AVCodecID codec_id)
3009 case AV_CODEC_ID_ADPCM_SBPRO_2:
3011 case AV_CODEC_ID_ADPCM_SBPRO_3:
3013 case AV_CODEC_ID_ADPCM_SBPRO_4:
3014 case AV_CODEC_ID_ADPCM_IMA_WAV:
3015 case AV_CODEC_ID_ADPCM_IMA_QT:
3016 case AV_CODEC_ID_ADPCM_SWF:
3017 case AV_CODEC_ID_ADPCM_MS:
3020 return av_get_exact_bits_per_sample(codec_id);
3024 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3026 int id, sr, ch, ba, tag, bps;
3028 id = avctx->codec_id;
3029 sr = avctx->sample_rate;
3030 ch = avctx->channels;
3031 ba = avctx->block_align;
3032 tag = avctx->codec_tag;
3033 bps = av_get_exact_bits_per_sample(avctx->codec_id);
3035 /* codecs with an exact constant bits per sample */
3036 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3037 return (frame_bytes * 8LL) / (bps * ch);
3038 bps = avctx->bits_per_coded_sample;
3040 /* codecs with a fixed packet duration */
3042 case AV_CODEC_ID_ADPCM_ADX: return 32;
3043 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3044 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3045 case AV_CODEC_ID_AMR_NB:
3046 case AV_CODEC_ID_EVRC:
3047 case AV_CODEC_ID_GSM:
3048 case AV_CODEC_ID_QCELP:
3049 case AV_CODEC_ID_RA_288: return 160;
3050 case AV_CODEC_ID_AMR_WB:
3051 case AV_CODEC_ID_GSM_MS: return 320;
3052 case AV_CODEC_ID_MP1: return 384;
3053 case AV_CODEC_ID_ATRAC1: return 512;
3054 case AV_CODEC_ID_ATRAC3: return 1024;
3055 case AV_CODEC_ID_ATRAC3P: return 2048;
3056 case AV_CODEC_ID_MP2:
3057 case AV_CODEC_ID_MUSEPACK7: return 1152;
3058 case AV_CODEC_ID_AC3: return 1536;
3062 /* calc from sample rate */
3063 if (id == AV_CODEC_ID_TTA)
3064 return 256 * sr / 245;
3067 /* calc from sample rate and channels */
3068 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3069 return (480 << (sr / 22050)) / ch;
3074 /* calc from block_align */
3075 if (id == AV_CODEC_ID_SIPR) {
3077 case 20: return 160;
3078 case 19: return 144;
3079 case 29: return 288;
3080 case 37: return 480;
3082 } else if (id == AV_CODEC_ID_ILBC) {
3084 case 38: return 160;
3085 case 50: return 240;
3090 if (frame_bytes > 0) {
3091 /* calc from frame_bytes only */
3092 if (id == AV_CODEC_ID_TRUESPEECH)
3093 return 240 * (frame_bytes / 32);
3094 if (id == AV_CODEC_ID_NELLYMOSER)
3095 return 256 * (frame_bytes / 64);
3096 if (id == AV_CODEC_ID_RA_144)
3097 return 160 * (frame_bytes / 20);
3098 if (id == AV_CODEC_ID_G723_1)
3099 return 240 * (frame_bytes / 24);
3102 /* calc from frame_bytes and bits_per_coded_sample */
3103 if (id == AV_CODEC_ID_ADPCM_G726)
3104 return frame_bytes * 8 / bps;
3107 if (ch > 0 && ch < INT_MAX/16) {
3108 /* calc from frame_bytes and channels */
3110 case AV_CODEC_ID_ADPCM_AFC:
3111 return frame_bytes / (9 * ch) * 16;
3112 case AV_CODEC_ID_ADPCM_PSX:
3113 case AV_CODEC_ID_ADPCM_DTK:
3114 return frame_bytes / (16 * ch) * 28;
3115 case AV_CODEC_ID_ADPCM_4XM:
3116 case AV_CODEC_ID_ADPCM_IMA_ISS:
3117 return (frame_bytes - 4 * ch) * 2 / ch;
3118 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3119 return (frame_bytes - 4) * 2 / ch;
3120 case AV_CODEC_ID_ADPCM_IMA_AMV:
3121 return (frame_bytes - 8) * 2 / ch;
3122 case AV_CODEC_ID_ADPCM_THP:
3123 case AV_CODEC_ID_ADPCM_THP_LE:
3124 if (avctx->extradata)
3125 return frame_bytes * 14 / (8 * ch);
3127 case AV_CODEC_ID_ADPCM_XA:
3128 return (frame_bytes / 128) * 224 / ch;
3129 case AV_CODEC_ID_INTERPLAY_DPCM:
3130 return (frame_bytes - 6 - ch) / ch;
3131 case AV_CODEC_ID_ROQ_DPCM:
3132 return (frame_bytes - 8) / ch;
3133 case AV_CODEC_ID_XAN_DPCM:
3134 return (frame_bytes - 2 * ch) / ch;
3135 case AV_CODEC_ID_MACE3:
3136 return 3 * frame_bytes / ch;
3137 case AV_CODEC_ID_MACE6:
3138 return 6 * frame_bytes / ch;
3139 case AV_CODEC_ID_PCM_LXF:
3140 return 2 * (frame_bytes / (5 * ch));
3141 case AV_CODEC_ID_IAC:
3142 case AV_CODEC_ID_IMC:
3143 return 4 * frame_bytes / ch;
3147 /* calc from frame_bytes, channels, and codec_tag */
3148 if (id == AV_CODEC_ID_SOL_DPCM) {
3150 return frame_bytes / ch;
3152 return frame_bytes * 2 / ch;
3157 /* calc from frame_bytes, channels, and block_align */
3158 int blocks = frame_bytes / ba;
3159 switch (avctx->codec_id) {
3160 case AV_CODEC_ID_ADPCM_IMA_WAV:
3161 if (bps < 2 || bps > 5)
3163 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3164 case AV_CODEC_ID_ADPCM_IMA_DK3:
3165 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3166 case AV_CODEC_ID_ADPCM_IMA_DK4:
3167 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3168 case AV_CODEC_ID_ADPCM_IMA_RAD:
3169 return blocks * ((ba - 4 * ch) * 2 / ch);
3170 case AV_CODEC_ID_ADPCM_MS:
3171 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3176 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3177 switch (avctx->codec_id) {
3178 case AV_CODEC_ID_PCM_DVD:
3181 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3182 case AV_CODEC_ID_PCM_BLURAY:
3185 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3186 case AV_CODEC_ID_S302M:
3187 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3193 /* Fall back on using frame_size */
3194 if (avctx->frame_size > 1 && frame_bytes)
3195 return avctx->frame_size;
3197 //For WMA we currently have no other means to calculate duration thus we
3198 //do it here by assuming CBR, which is true for all known cases.
3199 if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
3200 if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
3201 return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
3208 int ff_thread_init(AVCodecContext *s)
3215 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3229 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3232 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3236 #if FF_API_MISSING_SAMPLE
3237 FF_DISABLE_DEPRECATION_WARNINGS
3238 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3240 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3241 "version to the newest one from Git. If the problem still "
3242 "occurs, it means that your file has a feature which has not "
3243 "been implemented.\n", feature);
3245 av_log_ask_for_sample(avc, NULL);
3248 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3250 va_list argument_list;
3252 va_start(argument_list, msg);
3255 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3256 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3257 "of this file to ftp://upload.ffmpeg.org/incoming/ "
3258 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3260 va_end(argument_list);
3262 FF_ENABLE_DEPRECATION_WARNINGS
3263 #endif /* FF_API_MISSING_SAMPLE */
3265 static AVHWAccel *first_hwaccel = NULL;
3266 static AVHWAccel **last_hwaccel = &first_hwaccel;
3268 void av_register_hwaccel(AVHWAccel *hwaccel)
3270 AVHWAccel **p = last_hwaccel;
3271 hwaccel->next = NULL;
3272 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3274 last_hwaccel = &hwaccel->next;
3277 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
3279 return hwaccel ? hwaccel->next : first_hwaccel;
3282 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3285 // There is no good way to rollback a failure to destroy the
3286 // mutex, so we ignore failures.
3287 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
3288 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
3291 avformat_mutex = NULL;
3295 void *new_codec_mutex = NULL;
3296 void *new_avformat_mutex = NULL;
3298 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
3299 return err > 0 ? AVERROR_UNKNOWN : err;
3301 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
3302 // Ignore failures to destroy the newly created mutex.
3303 cb(&new_codec_mutex, AV_LOCK_DESTROY);
3304 return err > 0 ? AVERROR_UNKNOWN : err;
3307 codec_mutex = new_codec_mutex;
3308 avformat_mutex = new_avformat_mutex;
3314 int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
3316 if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
3320 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3324 if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1) {
3325 av_log(log_ctx, AV_LOG_ERROR,
3326 "Insufficient thread locking. At least %d threads are "
3327 "calling avcodec_open2() at the same time right now.\n",
3328 entangled_thread_counter);
3330 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3331 ff_avcodec_locked = 1;
3332 ff_unlock_avcodec(codec);
3333 return AVERROR(EINVAL);
3335 av_assert0(!ff_avcodec_locked);
3336 ff_avcodec_locked = 1;
3340 int ff_unlock_avcodec(const AVCodec *codec)
3342 if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
3345 av_assert0(ff_avcodec_locked);
3346 ff_avcodec_locked = 0;
3347 avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
3349 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3356 int avpriv_lock_avformat(void)
3359 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3365 int avpriv_unlock_avformat(void)
3368 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3374 unsigned int avpriv_toupper4(unsigned int x)
3376 return av_toupper(x & 0xFF) +
3377 (av_toupper((x >> 8) & 0xFF) << 8) +
3378 (av_toupper((x >> 16) & 0xFF) << 16) +
3379 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
3382 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3386 dst->owner = src->owner;
3388 ret = av_frame_ref(dst->f, src->f);
3392 av_assert0(!dst->progress);
3394 if (src->progress &&
3395 !(dst->progress = av_buffer_ref(src->progress))) {
3396 ff_thread_release_buffer(dst->owner, dst);
3397 return AVERROR(ENOMEM);
3405 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3407 return ff_get_format(avctx, fmt);
3410 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3413 return ff_get_buffer(avctx, f->f, flags);
3416 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3419 av_frame_unref(f->f);
3422 void ff_thread_finish_setup(AVCodecContext *avctx)
3426 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3430 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3434 int ff_thread_can_start_frame(AVCodecContext *avctx)
3439 int ff_alloc_entries(AVCodecContext *avctx, int count)
3444 void ff_reset_entries(AVCodecContext *avctx)
3448 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3452 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3458 int avcodec_is_open(AVCodecContext *s)
3460 return !!s->internal;
3463 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3468 ret = av_bprint_finalize(buf, &str);
3471 if (!av_bprint_is_complete(buf)) {
3473 return AVERROR(ENOMEM);
3476 avctx->extradata = str;
3477 /* Note: the string is NUL terminated (so extradata can be read as a
3478 * string), but the ending character is not accounted in the size (in
3479 * binary formats you are likely not supposed to mux that character). When
3480 * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
3482 avctx->extradata_size = buf->len;
3486 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3488 uint32_t *av_restrict state)
3492 av_assert0(p <= end);
3496 for (i = 0; i < 3; i++) {
3497 uint32_t tmp = *state << 8;
3498 *state = tmp + *(p++);
3499 if (tmp == 0x100 || p == end)
3504 if (p[-1] > 1 ) p += 3;
3505 else if (p[-2] ) p += 2;
3506 else if (p[-3]|(p[-1]-1)) p++;
3513 p = FFMIN(p, end) - 4;
3514 *state = AV_RB32(p);
3519 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
3521 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
3526 *size = sizeof(*props);
3528 props->vbv_delay = UINT64_MAX;
3533 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
3535 AVPacketSideData *tmp;
3536 AVCPBProperties *props;
3539 props = av_cpb_properties_alloc(&size);
3543 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
3549 avctx->coded_side_data = tmp;
3550 avctx->nb_coded_side_data++;
3552 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
3553 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
3554 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;