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/mathematics.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/samplefmt.h"
41 #include "libavutil/dict.h"
42 #include "libavutil/avassert.h"
45 #include "libavutil/opt.h"
47 #include "frame_thread_encoder.h"
49 #include "bytestream.h"
59 volatile int ff_avcodec_locked;
60 static int volatile entangled_thread_counter = 0;
61 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
62 static void *codec_mutex;
63 static void *avformat_mutex;
65 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
70 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
72 ptr = av_realloc(ptr, min_size);
73 /* we could set this to the unmodified min_size but this is safer
74 * if the user lost the ptr and uses NULL now
84 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
89 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
91 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
98 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
100 ff_fast_malloc(ptr, size, min_size, 0);
103 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
106 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
111 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
112 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
115 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
118 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
123 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
124 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
127 /* encoder management */
128 static AVCodec *first_avcodec = NULL;
130 AVCodec *av_codec_next(const AVCodec *c)
135 return first_avcodec;
138 static av_cold void avcodec_init(void)
140 static int initialized = 0;
142 if (initialized != 0)
147 ff_dsputil_static_init();
150 int av_codec_is_encoder(const AVCodec *codec)
152 return codec && (codec->encode_sub || codec->encode2);
155 int av_codec_is_decoder(const AVCodec *codec)
157 return codec && codec->decode;
160 av_cold void avcodec_register(AVCodec *codec)
166 while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
169 if (codec->init_static_data)
170 codec->init_static_data(codec);
173 unsigned avcodec_get_edge_width(void)
178 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
180 s->coded_width = width;
181 s->coded_height = height;
182 s->width = FF_CEIL_RSHIFT(width, s->lowres);
183 s->height = FF_CEIL_RSHIFT(height, s->lowres);
186 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
187 # define STRIDE_ALIGN 16
189 # define STRIDE_ALIGN 8
192 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
193 int linesize_align[AV_NUM_DATA_POINTERS])
199 switch (s->pix_fmt) {
200 case AV_PIX_FMT_YUV420P:
201 case AV_PIX_FMT_YUYV422:
202 case AV_PIX_FMT_UYVY422:
203 case AV_PIX_FMT_YUV422P:
204 case AV_PIX_FMT_YUV440P:
205 case AV_PIX_FMT_YUV444P:
206 case AV_PIX_FMT_GBRAP:
207 case AV_PIX_FMT_GBRP:
208 case AV_PIX_FMT_GRAY8:
209 case AV_PIX_FMT_GRAY16BE:
210 case AV_PIX_FMT_GRAY16LE:
211 case AV_PIX_FMT_YUVJ420P:
212 case AV_PIX_FMT_YUVJ422P:
213 case AV_PIX_FMT_YUVJ440P:
214 case AV_PIX_FMT_YUVJ444P:
215 case AV_PIX_FMT_YUVA420P:
216 case AV_PIX_FMT_YUVA422P:
217 case AV_PIX_FMT_YUVA444P:
218 case AV_PIX_FMT_YUV420P9LE:
219 case AV_PIX_FMT_YUV420P9BE:
220 case AV_PIX_FMT_YUV420P10LE:
221 case AV_PIX_FMT_YUV420P10BE:
222 case AV_PIX_FMT_YUV420P12LE:
223 case AV_PIX_FMT_YUV420P12BE:
224 case AV_PIX_FMT_YUV420P14LE:
225 case AV_PIX_FMT_YUV420P14BE:
226 case AV_PIX_FMT_YUV422P9LE:
227 case AV_PIX_FMT_YUV422P9BE:
228 case AV_PIX_FMT_YUV422P10LE:
229 case AV_PIX_FMT_YUV422P10BE:
230 case AV_PIX_FMT_YUV422P12LE:
231 case AV_PIX_FMT_YUV422P12BE:
232 case AV_PIX_FMT_YUV422P14LE:
233 case AV_PIX_FMT_YUV422P14BE:
234 case AV_PIX_FMT_YUV444P9LE:
235 case AV_PIX_FMT_YUV444P9BE:
236 case AV_PIX_FMT_YUV444P10LE:
237 case AV_PIX_FMT_YUV444P10BE:
238 case AV_PIX_FMT_YUV444P12LE:
239 case AV_PIX_FMT_YUV444P12BE:
240 case AV_PIX_FMT_YUV444P14LE:
241 case AV_PIX_FMT_YUV444P14BE:
242 case AV_PIX_FMT_YUVA420P9LE:
243 case AV_PIX_FMT_YUVA420P9BE:
244 case AV_PIX_FMT_YUVA420P10LE:
245 case AV_PIX_FMT_YUVA420P10BE:
246 case AV_PIX_FMT_YUVA422P9LE:
247 case AV_PIX_FMT_YUVA422P9BE:
248 case AV_PIX_FMT_YUVA422P10LE:
249 case AV_PIX_FMT_YUVA422P10BE:
250 case AV_PIX_FMT_YUVA444P9LE:
251 case AV_PIX_FMT_YUVA444P9BE:
252 case AV_PIX_FMT_YUVA444P10LE:
253 case AV_PIX_FMT_YUVA444P10BE:
254 case AV_PIX_FMT_GBRP9LE:
255 case AV_PIX_FMT_GBRP9BE:
256 case AV_PIX_FMT_GBRP10LE:
257 case AV_PIX_FMT_GBRP10BE:
258 case AV_PIX_FMT_GBRP12LE:
259 case AV_PIX_FMT_GBRP12BE:
260 case AV_PIX_FMT_GBRP14LE:
261 case AV_PIX_FMT_GBRP14BE:
262 w_align = 16; //FIXME assume 16 pixel per macroblock
263 h_align = 16 * 2; // interlaced needs 2 macroblocks height
265 case AV_PIX_FMT_YUV411P:
266 case AV_PIX_FMT_YUVJ411P:
267 case AV_PIX_FMT_UYYVYY411:
271 case AV_PIX_FMT_YUV410P:
272 if (s->codec_id == AV_CODEC_ID_SVQ1) {
277 case AV_PIX_FMT_RGB555:
278 if (s->codec_id == AV_CODEC_ID_RPZA) {
283 case AV_PIX_FMT_PAL8:
284 case AV_PIX_FMT_BGR8:
285 case AV_PIX_FMT_RGB8:
286 if (s->codec_id == AV_CODEC_ID_SMC ||
287 s->codec_id == AV_CODEC_ID_CINEPAK) {
292 case AV_PIX_FMT_BGR24:
293 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
294 (s->codec_id == AV_CODEC_ID_ZLIB)) {
299 case AV_PIX_FMT_RGB24:
300 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
311 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
312 w_align = FFMAX(w_align, 8);
315 *width = FFALIGN(*width, w_align);
316 *height = FFALIGN(*height, h_align);
317 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
318 // some of the optimized chroma MC reads one line too much
319 // which is also done in mpeg decoders with lowres > 0
322 for (i = 0; i < 4; i++)
323 linesize_align[i] = STRIDE_ALIGN;
326 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
328 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
329 int chroma_shift = desc->log2_chroma_w;
330 int linesize_align[AV_NUM_DATA_POINTERS];
333 avcodec_align_dimensions2(s, width, height, linesize_align);
334 align = FFMAX(linesize_align[0], linesize_align[3]);
335 linesize_align[1] <<= chroma_shift;
336 linesize_align[2] <<= chroma_shift;
337 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
338 *width = FFALIGN(*width, align);
341 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
343 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
344 return AVERROR(EINVAL);
347 *xpos = (pos&1) * 128;
348 *ypos = ((pos>>1)^(pos<4)) * 128;
353 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
357 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
358 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
361 return AVCHROMA_LOC_UNSPECIFIED;
364 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
365 enum AVSampleFormat sample_fmt, const uint8_t *buf,
366 int buf_size, int align)
368 int ch, planar, needed_size, ret = 0;
370 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
371 frame->nb_samples, sample_fmt,
373 if (buf_size < needed_size)
374 return AVERROR(EINVAL);
376 planar = av_sample_fmt_is_planar(sample_fmt);
377 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
378 if (!(frame->extended_data = av_mallocz(nb_channels *
379 sizeof(*frame->extended_data))))
380 return AVERROR(ENOMEM);
382 frame->extended_data = frame->data;
385 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
386 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
387 sample_fmt, align)) < 0) {
388 if (frame->extended_data != frame->data)
389 av_freep(&frame->extended_data);
392 if (frame->extended_data != frame->data) {
393 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
394 frame->data[ch] = frame->extended_data[ch];
400 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
402 FramePool *pool = avctx->internal->pool;
405 switch (avctx->codec_type) {
406 case AVMEDIA_TYPE_VIDEO: {
409 int w = frame->width;
410 int h = frame->height;
411 int tmpsize, unaligned;
413 if (pool->format == frame->format &&
414 pool->width == frame->width && pool->height == frame->height)
417 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
419 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
425 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
426 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
427 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
428 // increase alignment of w for next try (rhs gives the lowest bit set in w)
432 for (i = 0; i < 4; i++)
433 unaligned |= picture.linesize[i] % pool->stride_align[i];
436 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
437 NULL, picture.linesize);
441 for (i = 0; i < 3 && picture.data[i + 1]; i++)
442 size[i] = picture.data[i + 1] - picture.data[i];
443 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
445 for (i = 0; i < 4; i++) {
446 av_buffer_pool_uninit(&pool->pools[i]);
447 pool->linesize[i] = picture.linesize[i];
449 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
450 CONFIG_MEMORY_POISONING ?
453 if (!pool->pools[i]) {
454 ret = AVERROR(ENOMEM);
459 pool->format = frame->format;
460 pool->width = frame->width;
461 pool->height = frame->height;
465 case AVMEDIA_TYPE_AUDIO: {
466 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
467 int planar = av_sample_fmt_is_planar(frame->format);
468 int planes = planar ? ch : 1;
470 if (pool->format == frame->format && pool->planes == planes &&
471 pool->channels == ch && frame->nb_samples == pool->samples)
474 av_buffer_pool_uninit(&pool->pools[0]);
475 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
476 frame->nb_samples, frame->format, 0);
480 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
481 if (!pool->pools[0]) {
482 ret = AVERROR(ENOMEM);
486 pool->format = frame->format;
487 pool->planes = planes;
489 pool->samples = frame->nb_samples;
492 default: av_assert0(0);
496 for (i = 0; i < 4; i++)
497 av_buffer_pool_uninit(&pool->pools[i]);
499 pool->planes = pool->channels = pool->samples = 0;
500 pool->width = pool->height = 0;
504 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
506 FramePool *pool = avctx->internal->pool;
507 int planes = pool->planes;
510 frame->linesize[0] = pool->linesize[0];
512 if (planes > AV_NUM_DATA_POINTERS) {
513 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
514 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
515 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
516 sizeof(*frame->extended_buf));
517 if (!frame->extended_data || !frame->extended_buf) {
518 av_freep(&frame->extended_data);
519 av_freep(&frame->extended_buf);
520 return AVERROR(ENOMEM);
523 frame->extended_data = frame->data;
524 av_assert0(frame->nb_extended_buf == 0);
527 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
528 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
531 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
533 for (i = 0; i < frame->nb_extended_buf; i++) {
534 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
535 if (!frame->extended_buf[i])
537 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
540 if (avctx->debug & FF_DEBUG_BUFFERS)
541 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
545 av_frame_unref(frame);
546 return AVERROR(ENOMEM);
549 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
551 FramePool *pool = s->internal->pool;
552 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
553 int pixel_size = desc->comp[0].step_minus1 + 1;
554 int h_chroma_shift, v_chroma_shift;
557 if (pic->data[0] != NULL) {
558 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
562 memset(pic->data, 0, sizeof(pic->data));
563 pic->extended_data = pic->data;
565 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
567 for (i = 0; i < 4 && pool->pools[i]; i++) {
568 const int h_shift = i == 0 ? 0 : h_chroma_shift;
569 const int v_shift = i == 0 ? 0 : v_chroma_shift;
571 pic->linesize[i] = pool->linesize[i];
573 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
577 // no edge if EDGE EMU or not planar YUV
578 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
579 pic->data[i] = pic->buf[i]->data;
581 pic->data[i] = pic->buf[i]->data +
582 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
583 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
586 for (; i < AV_NUM_DATA_POINTERS; i++) {
588 pic->linesize[i] = 0;
590 if (pic->data[1] && !pic->data[2])
591 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
593 if (s->debug & FF_DEBUG_BUFFERS)
594 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
599 return AVERROR(ENOMEM);
602 void avpriv_color_frame(AVFrame *frame, const int c[4])
604 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
607 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
609 for (p = 0; p<desc->nb_components; p++) {
610 uint8_t *dst = frame->data[p];
611 int is_chroma = p == 1 || p == 2;
612 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
613 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
614 for (y = 0; y < height; y++) {
615 if (desc->comp[0].depth_minus1 >= 8) {
616 for (x = 0; x<bytes; x++)
617 ((uint16_t*)dst)[x] = c[p];
619 memset(dst, c[p], bytes);
620 dst += frame->linesize[p];
625 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
629 if ((ret = update_frame_pool(avctx, frame)) < 0)
632 #if FF_API_GET_BUFFER
633 frame->type = FF_BUFFER_TYPE_INTERNAL;
636 switch (avctx->codec_type) {
637 case AVMEDIA_TYPE_VIDEO:
638 return video_get_buffer(avctx, frame);
639 case AVMEDIA_TYPE_AUDIO:
640 return audio_get_buffer(avctx, frame);
646 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
649 frame->pkt_pts = avctx->pkt->pts;
650 av_frame_set_pkt_pos (frame, avctx->pkt->pos);
651 av_frame_set_pkt_duration(frame, avctx->pkt->duration);
652 av_frame_set_pkt_size (frame, avctx->pkt->size);
654 frame->pkt_pts = AV_NOPTS_VALUE;
655 av_frame_set_pkt_pos (frame, -1);
656 av_frame_set_pkt_duration(frame, 0);
657 av_frame_set_pkt_size (frame, -1);
659 frame->reordered_opaque = avctx->reordered_opaque;
661 switch (avctx->codec->type) {
662 case AVMEDIA_TYPE_VIDEO:
663 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
664 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
665 if (frame->format < 0)
666 frame->format = avctx->pix_fmt;
667 if (!frame->sample_aspect_ratio.num)
668 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
669 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
670 av_frame_set_colorspace(frame, avctx->colorspace);
671 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
672 av_frame_set_color_range(frame, avctx->color_range);
674 case AVMEDIA_TYPE_AUDIO:
675 if (!frame->sample_rate)
676 frame->sample_rate = avctx->sample_rate;
677 if (frame->format < 0)
678 frame->format = avctx->sample_fmt;
679 if (!frame->channel_layout) {
680 if (avctx->channel_layout) {
681 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
683 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
685 return AVERROR(EINVAL);
688 frame->channel_layout = avctx->channel_layout;
690 if (avctx->channels > FF_SANE_NB_CHANNELS) {
691 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
693 return AVERROR(ENOSYS);
697 av_frame_set_channels(frame, avctx->channels);
703 #if FF_API_GET_BUFFER
704 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
706 return avcodec_default_get_buffer2(avctx, frame, 0);
709 typedef struct CompatReleaseBufPriv {
710 AVCodecContext avctx;
712 } CompatReleaseBufPriv;
714 static void compat_free_buffer(void *opaque, uint8_t *data)
716 CompatReleaseBufPriv *priv = opaque;
717 if (priv->avctx.release_buffer)
718 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
722 static void compat_release_buffer(void *opaque, uint8_t *data)
724 AVBufferRef *buf = opaque;
725 av_buffer_unref(&buf);
729 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
733 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
734 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
735 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
736 return AVERROR(EINVAL);
739 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
742 #if FF_API_GET_BUFFER
744 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
745 * We wrap each plane in its own AVBuffer. Each of those has a reference to
746 * a dummy AVBuffer as its private data, unreffing it on free.
747 * When all the planes are freed, the dummy buffer's free callback calls
750 if (avctx->get_buffer) {
751 CompatReleaseBufPriv *priv = NULL;
752 AVBufferRef *dummy_buf = NULL;
755 if (flags & AV_GET_BUFFER_FLAG_REF)
756 frame->reference = 1;
758 ret = avctx->get_buffer(avctx, frame);
762 /* return if the buffers are already set up
763 * this would happen e.g. when a custom get_buffer() calls
764 * avcodec_default_get_buffer
769 priv = av_mallocz(sizeof(*priv));
771 ret = AVERROR(ENOMEM);
774 priv->avctx = *avctx;
775 priv->frame = *frame;
777 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
779 ret = AVERROR(ENOMEM);
783 #define WRAP_PLANE(ref_out, data, data_size) \
785 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
787 ret = AVERROR(ENOMEM); \
790 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
793 av_frame_unref(frame); \
794 ret = AVERROR(ENOMEM); \
799 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
800 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
802 planes = av_pix_fmt_count_planes(frame->format);
803 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
804 check for allocated buffers: make libavcodec happy */
805 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
807 if (!desc || planes <= 0) {
808 ret = AVERROR(EINVAL);
812 for (i = 0; i < planes; i++) {
813 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
814 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
816 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
819 int planar = av_sample_fmt_is_planar(frame->format);
820 planes = planar ? avctx->channels : 1;
822 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
823 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
824 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
825 frame->nb_extended_buf);
826 if (!frame->extended_buf) {
827 ret = AVERROR(ENOMEM);
832 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
833 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
835 for (i = 0; i < frame->nb_extended_buf; i++)
836 WRAP_PLANE(frame->extended_buf[i],
837 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
841 av_buffer_unref(&dummy_buf);
844 frame->width = avctx->width;
845 frame->height = avctx->height;
850 avctx->release_buffer(avctx, frame);
852 av_buffer_unref(&dummy_buf);
857 ret = avctx->get_buffer2(avctx, frame, flags);
859 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
860 frame->width = avctx->width;
861 frame->height = avctx->height;
867 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
869 int ret = get_buffer_internal(avctx, frame, flags);
871 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
875 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
880 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
882 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
883 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
884 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
885 av_frame_unref(frame);
888 ff_init_buffer_info(avctx, frame);
891 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
893 if (av_frame_is_writable(frame))
896 av_frame_move_ref(&tmp, frame);
898 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
900 av_frame_unref(&tmp);
904 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
905 frame->format, frame->width, frame->height);
907 av_frame_unref(&tmp);
912 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
914 int ret = reget_buffer_internal(avctx, frame);
916 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
920 #if FF_API_GET_BUFFER
921 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
923 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
928 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
935 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
939 for (i = 0; i < count; i++) {
940 int r = func(c, (char *)arg + i * size);
947 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
951 for (i = 0; i < count; i++) {
952 int r = func(c, arg, i, 0);
959 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
961 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
962 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
965 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
967 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
972 void avcodec_get_frame_defaults(AVFrame *frame)
974 #if LIBAVCODEC_VERSION_MAJOR >= 55
975 // extended_data should explicitly be freed when needed, this code is unsafe currently
976 // also this is not compatible to the <55 ABI/API
977 if (frame->extended_data != frame->data && 0)
978 av_freep(&frame->extended_data);
981 memset(frame, 0, sizeof(AVFrame));
985 frame->pkt_pts = AV_NOPTS_VALUE;
986 av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
987 av_frame_set_pkt_duration (frame, 0);
988 av_frame_set_pkt_pos (frame, -1);
989 av_frame_set_pkt_size (frame, -1);
990 frame->key_frame = 1;
991 frame->sample_aspect_ratio = (AVRational) {0, 1 };
992 frame->format = -1; /* unknown */
993 frame->extended_data = frame->data;
994 av_frame_set_colorspace(frame, AVCOL_SPC_UNSPECIFIED);
997 AVFrame *avcodec_alloc_frame(void)
999 AVFrame *frame = av_malloc(sizeof(AVFrame));
1004 frame->extended_data = NULL;
1005 avcodec_get_frame_defaults(frame);
1010 void avcodec_free_frame(AVFrame **frame)
1014 if (!frame || !*frame)
1019 if (f->extended_data != f->data)
1020 av_freep(&f->extended_data);
1025 #define MAKE_ACCESSORS(str, name, type, field) \
1026 type av_##name##_get_##field(const str *s) { return s->field; } \
1027 void av_##name##_set_##field(str *s, type v) { s->field = v; }
1029 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1030 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1031 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1033 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1035 memset(sub, 0, sizeof(*sub));
1036 sub->pts = AV_NOPTS_VALUE;
1039 static int get_bit_rate(AVCodecContext *ctx)
1042 int bits_per_sample;
1044 switch (ctx->codec_type) {
1045 case AVMEDIA_TYPE_VIDEO:
1046 case AVMEDIA_TYPE_DATA:
1047 case AVMEDIA_TYPE_SUBTITLE:
1048 case AVMEDIA_TYPE_ATTACHMENT:
1049 bit_rate = ctx->bit_rate;
1051 case AVMEDIA_TYPE_AUDIO:
1052 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1053 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1062 #if FF_API_AVCODEC_OPEN
1063 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1065 return avcodec_open2(avctx, codec, NULL);
1069 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1073 ff_unlock_avcodec();
1075 ret = avcodec_open2(avctx, codec, options);
1077 ff_lock_avcodec(avctx);
1081 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1084 AVDictionary *tmp = NULL;
1086 if (avcodec_is_open(avctx))
1089 if ((!codec && !avctx->codec)) {
1090 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1091 return AVERROR(EINVAL);
1093 if ((codec && avctx->codec && codec != avctx->codec)) {
1094 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1095 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1096 return AVERROR(EINVAL);
1099 codec = avctx->codec;
1101 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1102 return AVERROR(EINVAL);
1105 av_dict_copy(&tmp, *options, 0);
1107 ret = ff_lock_avcodec(avctx);
1111 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1112 if (!avctx->internal) {
1113 ret = AVERROR(ENOMEM);
1117 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1118 if (!avctx->internal->pool) {
1119 ret = AVERROR(ENOMEM);
1123 if (codec->priv_data_size > 0) {
1124 if (!avctx->priv_data) {
1125 avctx->priv_data = av_mallocz(codec->priv_data_size);
1126 if (!avctx->priv_data) {
1127 ret = AVERROR(ENOMEM);
1130 if (codec->priv_class) {
1131 *(const AVClass **)avctx->priv_data = codec->priv_class;
1132 av_opt_set_defaults(avctx->priv_data);
1135 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1138 avctx->priv_data = NULL;
1140 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1143 // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1144 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1145 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1146 if (avctx->coded_width && avctx->coded_height)
1147 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1148 else if (avctx->width && avctx->height)
1149 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1152 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1153 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1154 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1155 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1156 avcodec_set_dimensions(avctx, 0, 0);
1159 /* if the decoder init function was already called previously,
1160 * free the already allocated subtitle_header before overwriting it */
1161 if (av_codec_is_decoder(codec))
1162 av_freep(&avctx->subtitle_header);
1164 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1165 ret = AVERROR(EINVAL);
1169 avctx->codec = codec;
1170 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1171 avctx->codec_id == AV_CODEC_ID_NONE) {
1172 avctx->codec_type = codec->type;
1173 avctx->codec_id = codec->id;
1175 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1176 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1177 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1178 ret = AVERROR(EINVAL);
1181 avctx->frame_number = 0;
1182 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1184 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1185 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1186 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1188 av_log(avctx, AV_LOG_ERROR,
1189 "The %s '%s' is experimental but experimental codecs are not enabled, "
1190 "add '-strict %d' if you want to use it.\n",
1191 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1192 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1193 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1194 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1195 codec_string, codec2->name);
1196 ret = AVERROR_EXPERIMENTAL;
1200 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1201 (!avctx->time_base.num || !avctx->time_base.den)) {
1202 avctx->time_base.num = 1;
1203 avctx->time_base.den = avctx->sample_rate;
1207 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1209 if (CONFIG_FRAME_THREAD_ENCODER) {
1210 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1211 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1212 ff_lock_avcodec(avctx);
1218 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1219 ret = ff_thread_init(avctx);
1224 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1225 avctx->thread_count = 1;
1227 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1228 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1229 avctx->codec->max_lowres);
1230 ret = AVERROR(EINVAL);
1234 if (av_codec_is_encoder(avctx->codec)) {
1236 if (avctx->codec->sample_fmts) {
1237 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1238 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1240 if (avctx->channels == 1 &&
1241 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1242 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1243 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1247 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1249 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1250 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1251 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1252 ret = AVERROR(EINVAL);
1256 if (avctx->codec->pix_fmts) {
1257 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1258 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1260 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1261 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1262 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1264 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1265 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1266 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1267 ret = AVERROR(EINVAL);
1271 if (avctx->codec->supported_samplerates) {
1272 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1273 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1275 if (avctx->codec->supported_samplerates[i] == 0) {
1276 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1277 avctx->sample_rate);
1278 ret = AVERROR(EINVAL);
1282 if (avctx->codec->channel_layouts) {
1283 if (!avctx->channel_layout) {
1284 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1286 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1287 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1289 if (avctx->codec->channel_layouts[i] == 0) {
1291 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1292 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1293 ret = AVERROR(EINVAL);
1298 if (avctx->channel_layout && avctx->channels) {
1299 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1300 if (channels != avctx->channels) {
1302 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1303 av_log(avctx, AV_LOG_ERROR,
1304 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1305 buf, channels, avctx->channels);
1306 ret = AVERROR(EINVAL);
1309 } else if (avctx->channel_layout) {
1310 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1312 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1313 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1315 if (avctx->width <= 0 || avctx->height <= 0) {
1316 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1317 ret = AVERROR(EINVAL);
1321 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1322 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1323 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1326 if (!avctx->rc_initial_buffer_occupancy)
1327 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1330 avctx->pts_correction_num_faulty_pts =
1331 avctx->pts_correction_num_faulty_dts = 0;
1332 avctx->pts_correction_last_pts =
1333 avctx->pts_correction_last_dts = INT64_MIN;
1335 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1336 || avctx->internal->frame_thread_encoder)) {
1337 ret = avctx->codec->init(avctx);
1345 if (av_codec_is_decoder(avctx->codec)) {
1346 if (!avctx->bit_rate)
1347 avctx->bit_rate = get_bit_rate(avctx);
1348 /* validate channel layout from the decoder */
1349 if (avctx->channel_layout) {
1350 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1351 if (!avctx->channels)
1352 avctx->channels = channels;
1353 else if (channels != avctx->channels) {
1355 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1356 av_log(avctx, AV_LOG_WARNING,
1357 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1358 "ignoring specified channel layout\n",
1359 buf, channels, avctx->channels);
1360 avctx->channel_layout = 0;
1363 if (avctx->channels && avctx->channels < 0 ||
1364 avctx->channels > FF_SANE_NB_CHANNELS) {
1365 ret = AVERROR(EINVAL);
1368 if (avctx->sub_charenc) {
1369 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1370 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1371 "supported with subtitles codecs\n");
1372 ret = AVERROR(EINVAL);
1374 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1375 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1376 "subtitles character encoding will be ignored\n",
1377 avctx->codec_descriptor->name);
1378 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1380 /* input character encoding is set for a text based subtitle
1381 * codec at this point */
1382 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1383 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1385 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1387 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1388 if (cd == (iconv_t)-1) {
1389 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1390 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1391 ret = AVERROR(errno);
1396 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1397 "conversion needs a libavcodec built with iconv support "
1398 "for this codec\n");
1399 ret = AVERROR(ENOSYS);
1407 ff_unlock_avcodec();
1409 av_dict_free(options);
1416 av_freep(&avctx->priv_data);
1417 if (avctx->internal)
1418 av_freep(&avctx->internal->pool);
1419 av_freep(&avctx->internal);
1420 avctx->codec = NULL;
1424 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1426 if (avpkt->size < 0) {
1427 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1428 return AVERROR(EINVAL);
1430 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1431 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %d (max allowed is %d)\n",
1432 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1433 return AVERROR(EINVAL);
1437 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1438 if (!avpkt->data || avpkt->size < size) {
1439 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1440 avpkt->data = avctx->internal->byte_buffer;
1441 avpkt->size = avctx->internal->byte_buffer_size;
1442 avpkt->destruct = NULL;
1447 AVBufferRef *buf = avpkt->buf;
1448 #if FF_API_DESTRUCT_PACKET
1449 void *destruct = avpkt->destruct;
1452 if (avpkt->size < size) {
1453 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1454 return AVERROR(EINVAL);
1457 av_init_packet(avpkt);
1458 #if FF_API_DESTRUCT_PACKET
1459 avpkt->destruct = destruct;
1465 int ret = av_new_packet(avpkt, size);
1467 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1472 int ff_alloc_packet(AVPacket *avpkt, int size)
1474 return ff_alloc_packet2(NULL, avpkt, size);
1478 * Pad last frame with silence.
1480 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1482 AVFrame *frame = NULL;
1485 if (!(frame = avcodec_alloc_frame()))
1486 return AVERROR(ENOMEM);
1488 frame->format = src->format;
1489 frame->channel_layout = src->channel_layout;
1490 av_frame_set_channels(frame, av_frame_get_channels(src));
1491 frame->nb_samples = s->frame_size;
1492 ret = av_frame_get_buffer(frame, 32);
1496 ret = av_frame_copy_props(frame, src);
1500 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1501 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1503 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1504 frame->nb_samples - src->nb_samples,
1505 s->channels, s->sample_fmt)) < 0)
1513 av_frame_free(&frame);
1517 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1519 const AVFrame *frame,
1520 int *got_packet_ptr)
1523 AVFrame *padded_frame = NULL;
1525 AVPacket user_pkt = *avpkt;
1526 int needs_realloc = !user_pkt.data;
1528 *got_packet_ptr = 0;
1530 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1531 av_free_packet(avpkt);
1532 av_init_packet(avpkt);
1536 /* ensure that extended_data is properly set */
1537 if (frame && !frame->extended_data) {
1538 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1539 avctx->channels > AV_NUM_DATA_POINTERS) {
1540 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1541 "with more than %d channels, but extended_data is not set.\n",
1542 AV_NUM_DATA_POINTERS);
1543 return AVERROR(EINVAL);
1545 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1548 tmp.extended_data = tmp.data;
1552 /* check for valid frame size */
1554 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1555 if (frame->nb_samples > avctx->frame_size) {
1556 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1557 return AVERROR(EINVAL);
1559 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1560 if (frame->nb_samples < avctx->frame_size &&
1561 !avctx->internal->last_audio_frame) {
1562 ret = pad_last_frame(avctx, &padded_frame, frame);
1566 frame = padded_frame;
1567 avctx->internal->last_audio_frame = 1;
1570 if (frame->nb_samples != avctx->frame_size) {
1571 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1572 ret = AVERROR(EINVAL);
1578 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1580 if (*got_packet_ptr) {
1581 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1582 if (avpkt->pts == AV_NOPTS_VALUE)
1583 avpkt->pts = frame->pts;
1584 if (!avpkt->duration)
1585 avpkt->duration = ff_samples_to_time_base(avctx,
1588 avpkt->dts = avpkt->pts;
1593 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1595 if (user_pkt.data) {
1596 if (user_pkt.size >= avpkt->size) {
1597 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1599 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1600 avpkt->size = user_pkt.size;
1603 avpkt->buf = user_pkt.buf;
1604 avpkt->data = user_pkt.data;
1605 avpkt->destruct = user_pkt.destruct;
1607 if (av_dup_packet(avpkt) < 0) {
1608 ret = AVERROR(ENOMEM);
1614 if (needs_realloc && avpkt->data) {
1615 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1617 avpkt->data = avpkt->buf->data;
1620 avctx->frame_number++;
1623 if (ret < 0 || !*got_packet_ptr) {
1624 av_free_packet(avpkt);
1625 av_init_packet(avpkt);
1629 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1630 * this needs to be moved to the encoders, but for now we can do it
1631 * here to simplify things */
1632 avpkt->flags |= AV_PKT_FLAG_KEY;
1635 av_frame_free(&padded_frame);
1640 #if FF_API_OLD_ENCODE_AUDIO
1641 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1642 uint8_t *buf, int buf_size,
1643 const short *samples)
1646 AVFrame frame0 = { { 0 } };
1648 int ret, samples_size, got_packet;
1650 av_init_packet(&pkt);
1652 pkt.size = buf_size;
1656 avcodec_get_frame_defaults(frame);
1658 if (avctx->frame_size) {
1659 frame->nb_samples = avctx->frame_size;
1661 /* if frame_size is not set, the number of samples must be
1662 * calculated from the buffer size */
1664 if (!av_get_bits_per_sample(avctx->codec_id)) {
1665 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1666 "support this codec\n");
1667 return AVERROR(EINVAL);
1669 nb_samples = (int64_t)buf_size * 8 /
1670 (av_get_bits_per_sample(avctx->codec_id) *
1672 if (nb_samples >= INT_MAX)
1673 return AVERROR(EINVAL);
1674 frame->nb_samples = nb_samples;
1677 /* it is assumed that the samples buffer is large enough based on the
1678 * relevant parameters */
1679 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1681 avctx->sample_fmt, 1);
1682 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1684 (const uint8_t *)samples,
1685 samples_size, 1)) < 0)
1688 /* fabricate frame pts from sample count.
1689 * this is needed because the avcodec_encode_audio() API does not have
1690 * a way for the user to provide pts */
1691 if (avctx->sample_rate && avctx->time_base.num)
1692 frame->pts = ff_samples_to_time_base(avctx,
1693 avctx->internal->sample_count);
1695 frame->pts = AV_NOPTS_VALUE;
1696 avctx->internal->sample_count += frame->nb_samples;
1702 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1703 if (!ret && got_packet && avctx->coded_frame) {
1704 avctx->coded_frame->pts = pkt.pts;
1705 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1707 /* free any side data since we cannot return it */
1708 ff_packet_free_side_data(&pkt);
1710 if (frame && frame->extended_data != frame->data)
1711 av_freep(&frame->extended_data);
1713 return ret ? ret : pkt.size;
1718 #if FF_API_OLD_ENCODE_VIDEO
1719 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1720 const AVFrame *pict)
1723 int ret, got_packet = 0;
1725 if (buf_size < FF_MIN_BUFFER_SIZE) {
1726 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1730 av_init_packet(&pkt);
1732 pkt.size = buf_size;
1734 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1735 if (!ret && got_packet && avctx->coded_frame) {
1736 avctx->coded_frame->pts = pkt.pts;
1737 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1740 /* free any side data since we cannot return it */
1741 if (pkt.side_data_elems > 0) {
1743 for (i = 0; i < pkt.side_data_elems; i++)
1744 av_free(pkt.side_data[i].data);
1745 av_freep(&pkt.side_data);
1746 pkt.side_data_elems = 0;
1749 return ret ? ret : pkt.size;
1754 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1756 const AVFrame *frame,
1757 int *got_packet_ptr)
1760 AVPacket user_pkt = *avpkt;
1761 int needs_realloc = !user_pkt.data;
1763 *got_packet_ptr = 0;
1765 if(CONFIG_FRAME_THREAD_ENCODER &&
1766 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1767 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1769 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1770 avctx->stats_out[0] = '\0';
1772 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1773 av_free_packet(avpkt);
1774 av_init_packet(avpkt);
1779 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1780 return AVERROR(EINVAL);
1782 av_assert0(avctx->codec->encode2);
1784 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1785 av_assert0(ret <= 0);
1787 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1789 if (user_pkt.data) {
1790 if (user_pkt.size >= avpkt->size) {
1791 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1793 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1794 avpkt->size = user_pkt.size;
1797 avpkt->buf = user_pkt.buf;
1798 avpkt->data = user_pkt.data;
1799 avpkt->destruct = user_pkt.destruct;
1801 if (av_dup_packet(avpkt) < 0) {
1802 ret = AVERROR(ENOMEM);
1808 if (!*got_packet_ptr)
1810 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1811 avpkt->pts = avpkt->dts = frame->pts;
1813 if (needs_realloc && avpkt->data) {
1814 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1816 avpkt->data = avpkt->buf->data;
1819 avctx->frame_number++;
1822 if (ret < 0 || !*got_packet_ptr)
1823 av_free_packet(avpkt);
1825 av_packet_merge_side_data(avpkt);
1831 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1832 const AVSubtitle *sub)
1835 if (sub->start_display_time) {
1836 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1840 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1841 avctx->frame_number++;
1846 * Attempt to guess proper monotonic timestamps for decoded video frames
1847 * which might have incorrect times. Input timestamps may wrap around, in
1848 * which case the output will as well.
1850 * @param pts the pts field of the decoded AVPacket, as passed through
1852 * @param dts the dts field of the decoded AVPacket
1853 * @return one of the input values, may be AV_NOPTS_VALUE
1855 static int64_t guess_correct_pts(AVCodecContext *ctx,
1856 int64_t reordered_pts, int64_t dts)
1858 int64_t pts = AV_NOPTS_VALUE;
1860 if (dts != AV_NOPTS_VALUE) {
1861 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1862 ctx->pts_correction_last_dts = dts;
1864 if (reordered_pts != AV_NOPTS_VALUE) {
1865 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1866 ctx->pts_correction_last_pts = reordered_pts;
1868 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1869 && reordered_pts != AV_NOPTS_VALUE)
1870 pts = reordered_pts;
1877 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1880 const uint8_t *data;
1883 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1886 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1887 if (!data || size < 4)
1889 flags = bytestream_get_le32(&data);
1891 if (size < 4) /* Required for any of the changes */
1893 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1894 avctx->channels = bytestream_get_le32(&data);
1897 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1900 avctx->channel_layout = bytestream_get_le64(&data);
1905 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1906 avctx->sample_rate = bytestream_get_le32(&data);
1909 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1912 avctx->width = bytestream_get_le32(&data);
1913 avctx->height = bytestream_get_le32(&data);
1914 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1919 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1922 const uint8_t *side_metadata;
1925 side_metadata = av_packet_get_side_data(avctx->pkt,
1926 AV_PKT_DATA_STRINGS_METADATA, &size);
1929 end = side_metadata + size;
1930 while (side_metadata < end) {
1931 const uint8_t *key = side_metadata;
1932 const uint8_t *val = side_metadata + strlen(key) + 1;
1933 int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
1936 side_metadata = val + strlen(val) + 1;
1942 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1943 int *got_picture_ptr,
1944 const AVPacket *avpkt)
1946 AVCodecInternal *avci = avctx->internal;
1948 // copy to ensure we do not change avpkt
1949 AVPacket tmp = *avpkt;
1952 return AVERROR(EINVAL);
1953 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1954 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1955 return AVERROR(EINVAL);
1958 *got_picture_ptr = 0;
1959 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1960 return AVERROR(EINVAL);
1962 avcodec_get_frame_defaults(picture);
1964 if (!avctx->refcounted_frames)
1965 av_frame_unref(&avci->to_free);
1967 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1968 int did_split = av_packet_split_side_data(&tmp);
1969 apply_param_change(avctx, &tmp);
1971 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1972 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1975 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1977 picture->pkt_dts = avpkt->dts;
1979 if(!avctx->has_b_frames){
1980 av_frame_set_pkt_pos(picture, avpkt->pos);
1982 //FIXME these should be under if(!avctx->has_b_frames)
1983 /* get_buffer is supposed to set frame parameters */
1984 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1985 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1986 if (!picture->width) picture->width = avctx->width;
1987 if (!picture->height) picture->height = avctx->height;
1988 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1991 add_metadata_from_side_data(avctx, picture);
1993 emms_c(); //needed to avoid an emms_c() call before every return;
1997 ff_packet_free_side_data(&tmp);
2002 if (ret < 0 && picture->data[0])
2003 av_frame_unref(picture);
2005 if (*got_picture_ptr) {
2006 if (!avctx->refcounted_frames) {
2007 avci->to_free = *picture;
2008 avci->to_free.extended_data = avci->to_free.data;
2009 memset(picture->buf, 0, sizeof(picture->buf));
2012 avctx->frame_number++;
2013 av_frame_set_best_effort_timestamp(picture,
2014 guess_correct_pts(avctx,
2021 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2022 * make sure it's set correctly */
2023 picture->extended_data = picture->data;
2028 #if FF_API_OLD_DECODE_AUDIO
2029 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2030 int *frame_size_ptr,
2033 AVFrame frame = { { 0 } };
2034 int ret, got_frame = 0;
2036 if (avctx->get_buffer != avcodec_default_get_buffer) {
2037 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2038 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2039 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2040 "avcodec_decode_audio4()\n");
2041 avctx->get_buffer = avcodec_default_get_buffer;
2042 avctx->release_buffer = avcodec_default_release_buffer;
2045 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
2047 if (ret >= 0 && got_frame) {
2049 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2050 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2052 avctx->sample_fmt, 1);
2053 if (*frame_size_ptr < data_size) {
2054 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2055 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2056 return AVERROR(EINVAL);
2059 memcpy(samples, frame.extended_data[0], plane_size);
2061 if (planar && avctx->channels > 1) {
2062 uint8_t *out = ((uint8_t *)samples) + plane_size;
2063 for (ch = 1; ch < avctx->channels; ch++) {
2064 memcpy(out, frame.extended_data[ch], plane_size);
2068 *frame_size_ptr = data_size;
2070 *frame_size_ptr = 0;
2077 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2080 const AVPacket *avpkt)
2082 AVCodecInternal *avci = avctx->internal;
2083 int planar, channels;
2088 if (!avpkt->data && avpkt->size) {
2089 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2090 return AVERROR(EINVAL);
2093 return AVERROR(EINVAL);
2094 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2095 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2096 return AVERROR(EINVAL);
2099 avcodec_get_frame_defaults(frame);
2101 if (!avctx->refcounted_frames)
2102 av_frame_unref(&avci->to_free);
2104 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2107 // copy to ensure we do not change avpkt
2108 AVPacket tmp = *avpkt;
2109 int did_split = av_packet_split_side_data(&tmp);
2110 apply_param_change(avctx, &tmp);
2113 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2114 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2116 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2117 frame->pkt_dts = avpkt->dts;
2119 if (ret >= 0 && *got_frame_ptr) {
2120 add_metadata_from_side_data(avctx, frame);
2121 avctx->frame_number++;
2122 av_frame_set_best_effort_timestamp(frame,
2123 guess_correct_pts(avctx,
2126 if (frame->format == AV_SAMPLE_FMT_NONE)
2127 frame->format = avctx->sample_fmt;
2128 if (!frame->channel_layout)
2129 frame->channel_layout = avctx->channel_layout;
2130 if (!av_frame_get_channels(frame))
2131 av_frame_set_channels(frame, avctx->channels);
2132 if (!frame->sample_rate)
2133 frame->sample_rate = avctx->sample_rate;
2136 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2137 if(side && side_size>=10) {
2138 avctx->internal->skip_samples = AV_RL32(side);
2139 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2140 avctx->internal->skip_samples);
2142 if (avctx->internal->skip_samples && *got_frame_ptr) {
2143 if(frame->nb_samples <= avctx->internal->skip_samples){
2145 avctx->internal->skip_samples -= frame->nb_samples;
2146 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2147 avctx->internal->skip_samples);
2149 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2150 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2151 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2152 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2153 (AVRational){1, avctx->sample_rate},
2154 avctx->pkt_timebase);
2155 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2156 frame->pkt_pts += diff_ts;
2157 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2158 frame->pkt_dts += diff_ts;
2159 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2160 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2162 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2164 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2165 avctx->internal->skip_samples, frame->nb_samples);
2166 frame->nb_samples -= avctx->internal->skip_samples;
2167 avctx->internal->skip_samples = 0;
2173 ff_packet_free_side_data(&tmp);
2178 if (ret >= 0 && *got_frame_ptr) {
2179 if (!avctx->refcounted_frames) {
2180 avci->to_free = *frame;
2181 avci->to_free.extended_data = avci->to_free.data;
2182 memset(frame->buf, 0, sizeof(frame->buf));
2183 frame->extended_buf = NULL;
2184 frame->nb_extended_buf = 0;
2186 } else if (frame->data[0])
2187 av_frame_unref(frame);
2190 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2191 * make sure it's set correctly; assume decoders that actually use
2192 * extended_data are doing it correctly */
2193 if (*got_frame_ptr) {
2194 planar = av_sample_fmt_is_planar(frame->format);
2195 channels = av_frame_get_channels(frame);
2196 if (!(planar && channels > AV_NUM_DATA_POINTERS))
2197 frame->extended_data = frame->data;
2199 frame->extended_data = NULL;
2205 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2206 static int recode_subtitle(AVCodecContext *avctx,
2207 AVPacket *outpkt, const AVPacket *inpkt)
2210 iconv_t cd = (iconv_t)-1;
2217 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
2221 cd = iconv_open("UTF-8", avctx->sub_charenc);
2222 av_assert0(cd != (iconv_t)-1);
2227 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2228 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2229 ret = AVERROR(ENOMEM);
2233 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2236 outpkt->buf = tmp.buf;
2237 outpkt->data = tmp.data;
2238 outpkt->size = tmp.size;
2239 outb = outpkt->data;
2240 outl = outpkt->size;
2242 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2243 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2244 outl >= outpkt->size || inl != 0) {
2245 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2246 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2247 av_free_packet(&tmp);
2248 ret = AVERROR(errno);
2251 outpkt->size -= outl;
2252 memset(outpkt->data + outpkt->size, 0, outl);
2255 if (cd != (iconv_t)-1)
2259 av_assert0(!"requesting subtitles recoding without iconv");
2263 static int utf8_check(const uint8_t *str)
2265 const uint8_t *byte;
2266 uint32_t codepoint, min;
2270 GET_UTF8(codepoint, *(byte++), return 0;);
2271 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2272 1 << (5 * (byte - str) - 4);
2273 if (codepoint < min || codepoint >= 0x110000 ||
2274 codepoint == 0xFFFE /* BOM */ ||
2275 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2282 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2288 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2289 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2290 return AVERROR(EINVAL);
2294 avcodec_get_subtitle_defaults(sub);
2297 AVPacket pkt_recoded;
2298 AVPacket tmp = *avpkt;
2299 int did_split = av_packet_split_side_data(&tmp);
2300 //apply_param_change(avctx, &tmp);
2303 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2307 avctx->pkt = &pkt_recoded;
2309 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2310 sub->pts = av_rescale_q(avpkt->pts,
2311 avctx->pkt_timebase, AV_TIME_BASE_Q);
2312 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2313 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2314 !!*got_sub_ptr >= !!sub->num_rects);
2316 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2317 avctx->pkt_timebase.num) {
2318 AVRational ms = { 1, 1000 };
2319 sub->end_display_time = av_rescale_q(avpkt->duration,
2320 avctx->pkt_timebase, ms);
2323 for (i = 0; i < sub->num_rects; i++) {
2324 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2325 av_log(avctx, AV_LOG_ERROR,
2326 "Invalid UTF-8 in decoded subtitles text; "
2327 "maybe missing -sub_charenc option\n");
2328 avsubtitle_free(sub);
2329 return AVERROR_INVALIDDATA;
2333 if (tmp.data != pkt_recoded.data) { // did we recode?
2334 /* prevent from destroying side data from original packet */
2335 pkt_recoded.side_data = NULL;
2336 pkt_recoded.side_data_elems = 0;
2338 av_free_packet(&pkt_recoded);
2340 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2342 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2348 ff_packet_free_side_data(&tmp);
2354 avctx->frame_number++;
2360 void avsubtitle_free(AVSubtitle *sub)
2364 for (i = 0; i < sub->num_rects; i++) {
2365 av_freep(&sub->rects[i]->pict.data[0]);
2366 av_freep(&sub->rects[i]->pict.data[1]);
2367 av_freep(&sub->rects[i]->pict.data[2]);
2368 av_freep(&sub->rects[i]->pict.data[3]);
2369 av_freep(&sub->rects[i]->text);
2370 av_freep(&sub->rects[i]->ass);
2371 av_freep(&sub->rects[i]);
2374 av_freep(&sub->rects);
2376 memset(sub, 0, sizeof(AVSubtitle));
2379 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2383 ff_unlock_avcodec();
2385 ret = avcodec_close(avctx);
2387 ff_lock_avcodec(NULL);
2391 av_cold int avcodec_close(AVCodecContext *avctx)
2393 int ret = ff_lock_avcodec(avctx);
2397 if (avcodec_is_open(avctx)) {
2398 FramePool *pool = avctx->internal->pool;
2400 if (CONFIG_FRAME_THREAD_ENCODER &&
2401 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2402 ff_unlock_avcodec();
2403 ff_frame_thread_encoder_free(avctx);
2404 ff_lock_avcodec(avctx);
2406 if (HAVE_THREADS && avctx->thread_opaque)
2407 ff_thread_free(avctx);
2408 if (avctx->codec && avctx->codec->close)
2409 avctx->codec->close(avctx);
2410 avctx->coded_frame = NULL;
2411 avctx->internal->byte_buffer_size = 0;
2412 av_freep(&avctx->internal->byte_buffer);
2413 if (!avctx->refcounted_frames)
2414 av_frame_unref(&avctx->internal->to_free);
2415 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2416 av_buffer_pool_uninit(&pool->pools[i]);
2417 av_freep(&avctx->internal->pool);
2418 av_freep(&avctx->internal);
2421 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2422 av_opt_free(avctx->priv_data);
2424 av_freep(&avctx->priv_data);
2425 if (av_codec_is_encoder(avctx->codec))
2426 av_freep(&avctx->extradata);
2427 avctx->codec = NULL;
2428 avctx->active_thread_type = 0;
2430 ff_unlock_avcodec();
2434 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2437 //This is for future deprecatec codec ids, its empty since
2438 //last major bump but will fill up again over time, please don't remove it
2439 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2440 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
2441 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2442 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2443 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2444 default : return id;
2448 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2450 AVCodec *p, *experimental = NULL;
2452 id= remap_deprecated_codec_id(id);
2454 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2456 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2463 return experimental;
2466 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2468 return find_encdec(id, 1);
2471 AVCodec *avcodec_find_encoder_by_name(const char *name)
2478 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2485 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2487 return find_encdec(id, 0);
2490 AVCodec *avcodec_find_decoder_by_name(const char *name)
2497 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2504 const char *avcodec_get_name(enum AVCodecID id)
2506 const AVCodecDescriptor *cd;
2509 if (id == AV_CODEC_ID_NONE)
2511 cd = avcodec_descriptor_get(id);
2514 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2515 codec = avcodec_find_decoder(id);
2518 codec = avcodec_find_encoder(id);
2521 return "unknown_codec";
2524 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2526 int i, len, ret = 0;
2528 #define TAG_PRINT(x) \
2529 (((x) >= '0' && (x) <= '9') || \
2530 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2531 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2533 for (i = 0; i < 4; i++) {
2534 len = snprintf(buf, buf_size,
2535 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2537 buf_size = buf_size > len ? buf_size - len : 0;
2544 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2546 const char *codec_type;
2547 const char *codec_name;
2548 const char *profile = NULL;
2551 AVRational display_aspect_ratio;
2553 if (!buf || buf_size <= 0)
2555 codec_type = av_get_media_type_string(enc->codec_type);
2556 codec_name = avcodec_get_name(enc->codec_id);
2557 if (enc->profile != FF_PROFILE_UNKNOWN) {
2561 p = encode ? avcodec_find_encoder(enc->codec_id) :
2562 avcodec_find_decoder(enc->codec_id);
2564 profile = av_get_profile_name(p, enc->profile);
2567 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2569 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2571 if (enc->codec && strcmp(enc->codec->name, codec_name))
2572 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2575 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2576 if (enc->codec_tag) {
2578 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2579 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2580 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2583 switch (enc->codec_type) {
2584 case AVMEDIA_TYPE_VIDEO:
2585 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2586 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2588 av_get_pix_fmt_name(enc->pix_fmt));
2589 if (enc->bits_per_raw_sample &&
2590 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2591 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2592 " (%d bpc)", enc->bits_per_raw_sample);
2595 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2597 enc->width, enc->height);
2598 if (enc->sample_aspect_ratio.num) {
2599 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2600 enc->width * enc->sample_aspect_ratio.num,
2601 enc->height * enc->sample_aspect_ratio.den,
2603 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2604 " [SAR %d:%d DAR %d:%d]",
2605 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2606 display_aspect_ratio.num, display_aspect_ratio.den);
2608 if (av_log_get_level() >= AV_LOG_DEBUG) {
2609 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2610 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2612 enc->time_base.num / g, enc->time_base.den / g);
2616 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2617 ", q=%d-%d", enc->qmin, enc->qmax);
2620 case AVMEDIA_TYPE_AUDIO:
2621 if (enc->sample_rate) {
2622 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2623 ", %d Hz", enc->sample_rate);
2625 av_strlcat(buf, ", ", buf_size);
2626 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2627 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2628 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2629 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2632 case AVMEDIA_TYPE_DATA:
2633 if (av_log_get_level() >= AV_LOG_DEBUG) {
2634 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2636 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2638 enc->time_base.num / g, enc->time_base.den / g);
2645 if (enc->flags & CODEC_FLAG_PASS1)
2646 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2648 if (enc->flags & CODEC_FLAG_PASS2)
2649 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2652 bitrate = get_bit_rate(enc);
2654 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2655 ", %d kb/s", bitrate / 1000);
2656 } else if (enc->rc_max_rate > 0) {
2657 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2658 ", max. %d kb/s", enc->rc_max_rate / 1000);
2662 const char *av_get_profile_name(const AVCodec *codec, int profile)
2665 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2668 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2669 if (p->profile == profile)
2675 unsigned avcodec_version(void)
2677 // av_assert0(AV_CODEC_ID_V410==164);
2678 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2679 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2680 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2681 av_assert0(AV_CODEC_ID_SRT==94216);
2682 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2684 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2685 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2686 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2687 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2688 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2689 return LIBAVCODEC_VERSION_INT;
2692 const char *avcodec_configuration(void)
2694 return FFMPEG_CONFIGURATION;
2697 const char *avcodec_license(void)
2699 #define LICENSE_PREFIX "libavcodec license: "
2700 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2703 void avcodec_flush_buffers(AVCodecContext *avctx)
2705 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2706 ff_thread_flush(avctx);
2707 else if (avctx->codec->flush)
2708 avctx->codec->flush(avctx);
2710 avctx->pts_correction_last_pts =
2711 avctx->pts_correction_last_dts = INT64_MIN;
2713 if (!avctx->refcounted_frames)
2714 av_frame_unref(&avctx->internal->to_free);
2717 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2720 case AV_CODEC_ID_8SVX_EXP:
2721 case AV_CODEC_ID_8SVX_FIB:
2722 case AV_CODEC_ID_ADPCM_CT:
2723 case AV_CODEC_ID_ADPCM_IMA_APC:
2724 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2725 case AV_CODEC_ID_ADPCM_IMA_OKI:
2726 case AV_CODEC_ID_ADPCM_IMA_WS:
2727 case AV_CODEC_ID_ADPCM_G722:
2728 case AV_CODEC_ID_ADPCM_YAMAHA:
2730 case AV_CODEC_ID_PCM_ALAW:
2731 case AV_CODEC_ID_PCM_MULAW:
2732 case AV_CODEC_ID_PCM_S8:
2733 case AV_CODEC_ID_PCM_S8_PLANAR:
2734 case AV_CODEC_ID_PCM_U8:
2735 case AV_CODEC_ID_PCM_ZORK:
2737 case AV_CODEC_ID_PCM_S16BE:
2738 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2739 case AV_CODEC_ID_PCM_S16LE:
2740 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2741 case AV_CODEC_ID_PCM_U16BE:
2742 case AV_CODEC_ID_PCM_U16LE:
2744 case AV_CODEC_ID_PCM_S24DAUD:
2745 case AV_CODEC_ID_PCM_S24BE:
2746 case AV_CODEC_ID_PCM_S24LE:
2747 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2748 case AV_CODEC_ID_PCM_U24BE:
2749 case AV_CODEC_ID_PCM_U24LE:
2751 case AV_CODEC_ID_PCM_S32BE:
2752 case AV_CODEC_ID_PCM_S32LE:
2753 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2754 case AV_CODEC_ID_PCM_U32BE:
2755 case AV_CODEC_ID_PCM_U32LE:
2756 case AV_CODEC_ID_PCM_F32BE:
2757 case AV_CODEC_ID_PCM_F32LE:
2759 case AV_CODEC_ID_PCM_F64BE:
2760 case AV_CODEC_ID_PCM_F64LE:
2767 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2769 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2770 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2771 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2772 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2773 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2774 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2775 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2776 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2777 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2778 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2779 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2781 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2782 return AV_CODEC_ID_NONE;
2783 if (be < 0 || be > 1)
2785 return map[fmt][be];
2788 int av_get_bits_per_sample(enum AVCodecID codec_id)
2791 case AV_CODEC_ID_ADPCM_SBPRO_2:
2793 case AV_CODEC_ID_ADPCM_SBPRO_3:
2795 case AV_CODEC_ID_ADPCM_SBPRO_4:
2796 case AV_CODEC_ID_ADPCM_IMA_WAV:
2797 case AV_CODEC_ID_ADPCM_IMA_QT:
2798 case AV_CODEC_ID_ADPCM_SWF:
2799 case AV_CODEC_ID_ADPCM_MS:
2802 return av_get_exact_bits_per_sample(codec_id);
2806 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2808 int id, sr, ch, ba, tag, bps;
2810 id = avctx->codec_id;
2811 sr = avctx->sample_rate;
2812 ch = avctx->channels;
2813 ba = avctx->block_align;
2814 tag = avctx->codec_tag;
2815 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2817 /* codecs with an exact constant bits per sample */
2818 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2819 return (frame_bytes * 8LL) / (bps * ch);
2820 bps = avctx->bits_per_coded_sample;
2822 /* codecs with a fixed packet duration */
2824 case AV_CODEC_ID_ADPCM_ADX: return 32;
2825 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2826 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2827 case AV_CODEC_ID_AMR_NB:
2828 case AV_CODEC_ID_EVRC:
2829 case AV_CODEC_ID_GSM:
2830 case AV_CODEC_ID_QCELP:
2831 case AV_CODEC_ID_RA_288: return 160;
2832 case AV_CODEC_ID_AMR_WB:
2833 case AV_CODEC_ID_GSM_MS: return 320;
2834 case AV_CODEC_ID_MP1: return 384;
2835 case AV_CODEC_ID_ATRAC1: return 512;
2836 case AV_CODEC_ID_ATRAC3: return 1024;
2837 case AV_CODEC_ID_MP2:
2838 case AV_CODEC_ID_MUSEPACK7: return 1152;
2839 case AV_CODEC_ID_AC3: return 1536;
2843 /* calc from sample rate */
2844 if (id == AV_CODEC_ID_TTA)
2845 return 256 * sr / 245;
2848 /* calc from sample rate and channels */
2849 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2850 return (480 << (sr / 22050)) / ch;
2855 /* calc from block_align */
2856 if (id == AV_CODEC_ID_SIPR) {
2858 case 20: return 160;
2859 case 19: return 144;
2860 case 29: return 288;
2861 case 37: return 480;
2863 } else if (id == AV_CODEC_ID_ILBC) {
2865 case 38: return 160;
2866 case 50: return 240;
2871 if (frame_bytes > 0) {
2872 /* calc from frame_bytes only */
2873 if (id == AV_CODEC_ID_TRUESPEECH)
2874 return 240 * (frame_bytes / 32);
2875 if (id == AV_CODEC_ID_NELLYMOSER)
2876 return 256 * (frame_bytes / 64);
2877 if (id == AV_CODEC_ID_RA_144)
2878 return 160 * (frame_bytes / 20);
2879 if (id == AV_CODEC_ID_G723_1)
2880 return 240 * (frame_bytes / 24);
2883 /* calc from frame_bytes and bits_per_coded_sample */
2884 if (id == AV_CODEC_ID_ADPCM_G726)
2885 return frame_bytes * 8 / bps;
2889 /* calc from frame_bytes and channels */
2891 case AV_CODEC_ID_ADPCM_AFC:
2892 return frame_bytes / (9 * ch) * 16;
2893 case AV_CODEC_ID_ADPCM_DTK:
2894 return frame_bytes / (16 * ch) * 28;
2895 case AV_CODEC_ID_ADPCM_4XM:
2896 case AV_CODEC_ID_ADPCM_IMA_ISS:
2897 return (frame_bytes - 4 * ch) * 2 / ch;
2898 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2899 return (frame_bytes - 4) * 2 / ch;
2900 case AV_CODEC_ID_ADPCM_IMA_AMV:
2901 return (frame_bytes - 8) * 2 / ch;
2902 case AV_CODEC_ID_ADPCM_XA:
2903 return (frame_bytes / 128) * 224 / ch;
2904 case AV_CODEC_ID_INTERPLAY_DPCM:
2905 return (frame_bytes - 6 - ch) / ch;
2906 case AV_CODEC_ID_ROQ_DPCM:
2907 return (frame_bytes - 8) / ch;
2908 case AV_CODEC_ID_XAN_DPCM:
2909 return (frame_bytes - 2 * ch) / ch;
2910 case AV_CODEC_ID_MACE3:
2911 return 3 * frame_bytes / ch;
2912 case AV_CODEC_ID_MACE6:
2913 return 6 * frame_bytes / ch;
2914 case AV_CODEC_ID_PCM_LXF:
2915 return 2 * (frame_bytes / (5 * ch));
2916 case AV_CODEC_ID_IAC:
2917 case AV_CODEC_ID_IMC:
2918 return 4 * frame_bytes / ch;
2922 /* calc from frame_bytes, channels, and codec_tag */
2923 if (id == AV_CODEC_ID_SOL_DPCM) {
2925 return frame_bytes / ch;
2927 return frame_bytes * 2 / ch;
2932 /* calc from frame_bytes, channels, and block_align */
2933 int blocks = frame_bytes / ba;
2934 switch (avctx->codec_id) {
2935 case AV_CODEC_ID_ADPCM_IMA_WAV:
2936 if (bps < 2 || bps > 5)
2938 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
2939 case AV_CODEC_ID_ADPCM_IMA_DK3:
2940 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2941 case AV_CODEC_ID_ADPCM_IMA_DK4:
2942 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2943 case AV_CODEC_ID_ADPCM_IMA_RAD:
2944 return blocks * ((ba - 4 * ch) * 2 / ch);
2945 case AV_CODEC_ID_ADPCM_MS:
2946 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2951 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2952 switch (avctx->codec_id) {
2953 case AV_CODEC_ID_PCM_DVD:
2956 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2957 case AV_CODEC_ID_PCM_BLURAY:
2960 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2961 case AV_CODEC_ID_S302M:
2962 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2972 int ff_thread_init(AVCodecContext *s)
2979 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2993 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2996 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3000 #if FF_API_MISSING_SAMPLE
3001 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3003 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3004 "version to the newest one from Git. If the problem still "
3005 "occurs, it means that your file has a feature which has not "
3006 "been implemented.\n", feature);
3008 av_log_ask_for_sample(avc, NULL);
3011 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3013 va_list argument_list;
3015 va_start(argument_list, msg);
3018 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3019 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3020 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
3021 "and contact the ffmpeg-devel mailing list.\n");
3023 va_end(argument_list);
3025 #endif /* FF_API_MISSING_SAMPLE */
3027 static AVHWAccel *first_hwaccel = NULL;
3029 void av_register_hwaccel(AVHWAccel *hwaccel)
3031 AVHWAccel **p = &first_hwaccel;
3032 hwaccel->next = NULL;
3033 while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3037 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
3039 return hwaccel ? hwaccel->next : first_hwaccel;
3042 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
3044 AVHWAccel *hwaccel = NULL;
3046 while ((hwaccel = av_hwaccel_next(hwaccel)))
3047 if (hwaccel->id == codec_id
3048 && hwaccel->pix_fmt == pix_fmt)
3053 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3056 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3058 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3065 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3067 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3073 int ff_lock_avcodec(AVCodecContext *log_ctx)
3076 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3079 entangled_thread_counter++;
3080 if (entangled_thread_counter != 1) {
3081 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3083 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3084 ff_avcodec_locked = 1;
3085 ff_unlock_avcodec();
3086 return AVERROR(EINVAL);
3088 av_assert0(!ff_avcodec_locked);
3089 ff_avcodec_locked = 1;
3093 int ff_unlock_avcodec(void)
3095 av_assert0(ff_avcodec_locked);
3096 ff_avcodec_locked = 0;
3097 entangled_thread_counter--;
3099 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3105 int avpriv_lock_avformat(void)
3108 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3114 int avpriv_unlock_avformat(void)
3117 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3123 unsigned int avpriv_toupper4(unsigned int x)
3125 return av_toupper(x & 0xFF) +
3126 (av_toupper((x >> 8) & 0xFF) << 8) +
3127 (av_toupper((x >> 16) & 0xFF) << 16) +
3128 (av_toupper((x >> 24) & 0xFF) << 24);
3131 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3135 dst->owner = src->owner;
3137 ret = av_frame_ref(dst->f, src->f);
3141 if (src->progress &&
3142 !(dst->progress = av_buffer_ref(src->progress))) {
3143 ff_thread_release_buffer(dst->owner, dst);
3144 return AVERROR(ENOMEM);
3152 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3154 return avctx->get_format(avctx, fmt);
3157 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3160 return ff_get_buffer(avctx, f->f, flags);
3163 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3165 av_frame_unref(f->f);
3168 void ff_thread_finish_setup(AVCodecContext *avctx)
3172 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3176 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3180 int ff_thread_can_start_frame(AVCodecContext *avctx)
3187 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3189 AVCodec *c= avcodec_find_decoder(codec_id);
3191 c= avcodec_find_encoder(codec_id);
3195 if (codec_id <= AV_CODEC_ID_NONE)
3196 return AVMEDIA_TYPE_UNKNOWN;
3197 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3198 return AVMEDIA_TYPE_VIDEO;
3199 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3200 return AVMEDIA_TYPE_AUDIO;
3201 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3202 return AVMEDIA_TYPE_SUBTITLE;
3204 return AVMEDIA_TYPE_UNKNOWN;
3207 int avcodec_is_open(AVCodecContext *s)
3209 return !!s->internal;
3212 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3217 ret = av_bprint_finalize(buf, &str);
3220 avctx->extradata = str;
3221 /* Note: the string is NUL terminated (so extradata can be read as a
3222 * string), but the ending character is not accounted in the size (in
3223 * binary formats you are likely not supposed to mux that character). When
3224 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3226 avctx->extradata_size = buf->len;
3230 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3232 uint32_t *av_restrict state)
3236 av_assert0(p <= end);
3240 for (i = 0; i < 3; i++) {
3241 uint32_t tmp = *state << 8;
3242 *state = tmp + *(p++);
3243 if (tmp == 0x100 || p == end)
3248 if (p[-1] > 1 ) p += 3;
3249 else if (p[-2] ) p += 2;
3250 else if (p[-3]|(p[-1]-1)) p++;
3257 p = FFMIN(p, end) - 4;
3258 *state = AV_RB32(p);