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/attributes.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/crc.h"
35 #include "libavutil/frame.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/samplefmt.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/avassert.h"
44 #include "libavutil/opt.h"
46 #include "frame_thread_encoder.h"
48 #include "bytestream.h"
58 volatile int ff_avcodec_locked;
59 static int volatile entangled_thread_counter = 0;
60 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
61 static void *codec_mutex;
62 static void *avformat_mutex;
64 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
69 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
71 ptr = av_realloc(ptr, min_size);
72 /* we could set this to the unmodified min_size but this is safer
73 * if the user lost the ptr and uses NULL now
83 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
88 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
90 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
97 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
99 ff_fast_malloc(ptr, size, min_size, 0);
102 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
105 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
110 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
111 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
114 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
117 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
122 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
123 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
126 /* encoder management */
127 static AVCodec *first_avcodec = NULL;
129 AVCodec *av_codec_next(const AVCodec *c)
134 return first_avcodec;
137 static av_cold void avcodec_init(void)
139 static int initialized = 0;
141 if (initialized != 0)
146 ff_dsputil_static_init();
149 int av_codec_is_encoder(const AVCodec *codec)
151 return codec && (codec->encode_sub || codec->encode2);
154 int av_codec_is_decoder(const AVCodec *codec)
156 return codec && codec->decode;
159 av_cold void avcodec_register(AVCodec *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_fill_audio_frame(AVFrame *frame, int nb_channels,
342 enum AVSampleFormat sample_fmt, const uint8_t *buf,
343 int buf_size, int align)
345 int ch, planar, needed_size, ret = 0;
347 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
348 frame->nb_samples, sample_fmt,
350 if (buf_size < needed_size)
351 return AVERROR(EINVAL);
353 planar = av_sample_fmt_is_planar(sample_fmt);
354 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
355 if (!(frame->extended_data = av_mallocz(nb_channels *
356 sizeof(*frame->extended_data))))
357 return AVERROR(ENOMEM);
359 frame->extended_data = frame->data;
362 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
363 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
364 sample_fmt, align)) < 0) {
365 if (frame->extended_data != frame->data)
366 av_freep(&frame->extended_data);
369 if (frame->extended_data != frame->data) {
370 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
371 frame->data[ch] = frame->extended_data[ch];
377 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
379 FramePool *pool = avctx->internal->pool;
382 switch (avctx->codec_type) {
383 case AVMEDIA_TYPE_VIDEO: {
386 int w = frame->width;
387 int h = frame->height;
388 int tmpsize, unaligned;
390 if (pool->format == frame->format &&
391 pool->width == frame->width && pool->height == frame->height)
394 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
396 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
402 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
403 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
404 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
405 // increase alignment of w for next try (rhs gives the lowest bit set in w)
409 for (i = 0; i < 4; i++)
410 unaligned |= picture.linesize[i] % pool->stride_align[i];
413 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
414 NULL, picture.linesize);
418 for (i = 0; i < 3 && picture.data[i + 1]; i++)
419 size[i] = picture.data[i + 1] - picture.data[i];
420 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
422 for (i = 0; i < 4; i++) {
423 av_buffer_pool_uninit(&pool->pools[i]);
424 pool->linesize[i] = picture.linesize[i];
426 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
427 CONFIG_MEMORY_POISONING ?
430 if (!pool->pools[i]) {
431 ret = AVERROR(ENOMEM);
436 pool->format = frame->format;
437 pool->width = frame->width;
438 pool->height = frame->height;
442 case AVMEDIA_TYPE_AUDIO: {
443 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
444 int planar = av_sample_fmt_is_planar(frame->format);
445 int planes = planar ? ch : 1;
447 if (pool->format == frame->format && pool->planes == planes &&
448 pool->channels == ch && frame->nb_samples == pool->samples)
451 av_buffer_pool_uninit(&pool->pools[0]);
452 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
453 frame->nb_samples, frame->format, 0);
457 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
458 if (!pool->pools[0]) {
459 ret = AVERROR(ENOMEM);
463 pool->format = frame->format;
464 pool->planes = planes;
466 pool->samples = frame->nb_samples;
469 default: av_assert0(0);
473 for (i = 0; i < 4; i++)
474 av_buffer_pool_uninit(&pool->pools[i]);
476 pool->planes = pool->channels = pool->samples = 0;
477 pool->width = pool->height = 0;
481 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
483 FramePool *pool = avctx->internal->pool;
484 int planes = pool->planes;
487 frame->linesize[0] = pool->linesize[0];
489 if (planes > AV_NUM_DATA_POINTERS) {
490 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
491 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
492 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
493 sizeof(*frame->extended_buf));
494 if (!frame->extended_data || !frame->extended_buf) {
495 av_freep(&frame->extended_data);
496 av_freep(&frame->extended_buf);
497 return AVERROR(ENOMEM);
500 frame->extended_data = frame->data;
501 av_assert0(frame->nb_extended_buf == 0);
504 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
505 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
508 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
510 for (i = 0; i < frame->nb_extended_buf; i++) {
511 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
512 if (!frame->extended_buf[i])
514 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
517 if (avctx->debug & FF_DEBUG_BUFFERS)
518 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
522 av_frame_unref(frame);
523 return AVERROR(ENOMEM);
526 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
528 FramePool *pool = s->internal->pool;
529 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
530 int pixel_size = desc->comp[0].step_minus1 + 1;
531 int h_chroma_shift, v_chroma_shift;
534 if (pic->data[0] != NULL) {
535 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
539 memset(pic->data, 0, sizeof(pic->data));
540 pic->extended_data = pic->data;
542 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
544 for (i = 0; i < 4 && pool->pools[i]; i++) {
545 const int h_shift = i == 0 ? 0 : h_chroma_shift;
546 const int v_shift = i == 0 ? 0 : v_chroma_shift;
548 pic->linesize[i] = pool->linesize[i];
550 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
554 // no edge if EDGE EMU or not planar YUV
555 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
556 pic->data[i] = pic->buf[i]->data;
558 pic->data[i] = pic->buf[i]->data +
559 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
560 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
563 for (; i < AV_NUM_DATA_POINTERS; i++) {
565 pic->linesize[i] = 0;
567 if (pic->data[1] && !pic->data[2])
568 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
570 if (s->debug & FF_DEBUG_BUFFERS)
571 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
576 return AVERROR(ENOMEM);
579 void avpriv_color_frame(AVFrame *frame, const int c[4])
581 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
584 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
586 for (p = 0; p<desc->nb_components; p++) {
587 uint8_t *dst = frame->data[p];
588 int is_chroma = p == 1 || p == 2;
589 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
590 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
591 for (y = 0; y < height; y++) {
592 if (desc->comp[0].depth_minus1 >= 8) {
593 for (x = 0; x<bytes; x++)
594 ((uint16_t*)dst)[x] = c[p];
596 memset(dst, c[p], bytes);
597 dst += frame->linesize[p];
602 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
606 if ((ret = update_frame_pool(avctx, frame)) < 0)
609 #if FF_API_GET_BUFFER
610 frame->type = FF_BUFFER_TYPE_INTERNAL;
613 switch (avctx->codec_type) {
614 case AVMEDIA_TYPE_VIDEO:
615 return video_get_buffer(avctx, frame);
616 case AVMEDIA_TYPE_AUDIO:
617 return audio_get_buffer(avctx, frame);
623 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
626 frame->pkt_pts = avctx->pkt->pts;
627 av_frame_set_pkt_pos (frame, avctx->pkt->pos);
628 av_frame_set_pkt_duration(frame, avctx->pkt->duration);
629 av_frame_set_pkt_size (frame, avctx->pkt->size);
631 frame->pkt_pts = AV_NOPTS_VALUE;
632 av_frame_set_pkt_pos (frame, -1);
633 av_frame_set_pkt_duration(frame, 0);
634 av_frame_set_pkt_size (frame, -1);
636 frame->reordered_opaque = avctx->reordered_opaque;
638 switch (avctx->codec->type) {
639 case AVMEDIA_TYPE_VIDEO:
640 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
641 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
642 if (frame->format < 0)
643 frame->format = avctx->pix_fmt;
644 if (!frame->sample_aspect_ratio.num)
645 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
647 case AVMEDIA_TYPE_AUDIO:
648 if (!frame->sample_rate)
649 frame->sample_rate = avctx->sample_rate;
650 if (frame->format < 0)
651 frame->format = avctx->sample_fmt;
652 if (!frame->channel_layout) {
653 if (avctx->channel_layout) {
654 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
656 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
658 return AVERROR(EINVAL);
661 frame->channel_layout = avctx->channel_layout;
663 if (avctx->channels > FF_SANE_NB_CHANNELS) {
664 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
666 return AVERROR(ENOSYS);
670 av_frame_set_channels(frame, avctx->channels);
676 #if FF_API_GET_BUFFER
677 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
679 return avcodec_default_get_buffer2(avctx, frame, 0);
682 typedef struct CompatReleaseBufPriv {
683 AVCodecContext avctx;
685 } CompatReleaseBufPriv;
687 static void compat_free_buffer(void *opaque, uint8_t *data)
689 CompatReleaseBufPriv *priv = opaque;
690 if (priv->avctx.release_buffer)
691 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
695 static void compat_release_buffer(void *opaque, uint8_t *data)
697 AVBufferRef *buf = opaque;
698 av_buffer_unref(&buf);
702 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
706 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
707 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
708 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
709 return AVERROR(EINVAL);
712 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
715 #if FF_API_GET_BUFFER
717 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
718 * We wrap each plane in its own AVBuffer. Each of those has a reference to
719 * a dummy AVBuffer as its private data, unreffing it on free.
720 * When all the planes are freed, the dummy buffer's free callback calls
723 if (avctx->get_buffer) {
724 CompatReleaseBufPriv *priv = NULL;
725 AVBufferRef *dummy_buf = NULL;
728 if (flags & AV_GET_BUFFER_FLAG_REF)
729 frame->reference = 1;
731 ret = avctx->get_buffer(avctx, frame);
735 /* return if the buffers are already set up
736 * this would happen e.g. when a custom get_buffer() calls
737 * avcodec_default_get_buffer
742 priv = av_mallocz(sizeof(*priv));
744 ret = AVERROR(ENOMEM);
747 priv->avctx = *avctx;
748 priv->frame = *frame;
750 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
752 ret = AVERROR(ENOMEM);
756 #define WRAP_PLANE(ref_out, data, data_size) \
758 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
760 ret = AVERROR(ENOMEM); \
763 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
766 av_frame_unref(frame); \
767 ret = AVERROR(ENOMEM); \
772 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
773 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
775 planes = av_pix_fmt_count_planes(frame->format);
776 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
777 check for allocated buffers: make libavcodec happy */
778 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
780 if (!desc || planes <= 0) {
781 ret = AVERROR(EINVAL);
785 for (i = 0; i < planes; i++) {
786 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
787 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
789 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
792 int planar = av_sample_fmt_is_planar(frame->format);
793 planes = planar ? avctx->channels : 1;
795 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
796 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
797 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
798 frame->nb_extended_buf);
799 if (!frame->extended_buf) {
800 ret = AVERROR(ENOMEM);
805 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
806 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
808 for (i = 0; i < frame->nb_extended_buf; i++)
809 WRAP_PLANE(frame->extended_buf[i],
810 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
814 av_buffer_unref(&dummy_buf);
817 frame->width = avctx->width;
818 frame->height = avctx->height;
823 avctx->release_buffer(avctx, frame);
825 av_buffer_unref(&dummy_buf);
830 ret = avctx->get_buffer2(avctx, frame, flags);
832 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
833 frame->width = avctx->width;
834 frame->height = avctx->height;
840 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
842 int ret = get_buffer_internal(avctx, frame, flags);
844 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
848 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
853 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
855 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
856 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
857 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
858 av_frame_unref(frame);
861 ff_init_buffer_info(avctx, frame);
864 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
866 if (av_frame_is_writable(frame))
869 av_frame_move_ref(&tmp, frame);
871 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
873 av_frame_unref(&tmp);
877 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
878 frame->format, frame->width, frame->height);
880 av_frame_unref(&tmp);
885 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
887 int ret = reget_buffer_internal(avctx, frame);
889 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
893 #if FF_API_GET_BUFFER
894 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
896 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
901 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
908 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
912 for (i = 0; i < count; i++) {
913 int r = func(c, (char *)arg + i * size);
920 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
924 for (i = 0; i < count; i++) {
925 int r = func(c, arg, i, 0);
932 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
934 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
935 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
938 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
940 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
945 void avcodec_get_frame_defaults(AVFrame *frame)
947 #if LIBAVCODEC_VERSION_MAJOR >= 55
948 // extended_data should explicitly be freed when needed, this code is unsafe currently
949 // also this is not compatible to the <55 ABI/API
950 if (frame->extended_data != frame->data && 0)
951 av_freep(&frame->extended_data);
954 memset(frame, 0, sizeof(AVFrame));
958 frame->pkt_pts = AV_NOPTS_VALUE;
959 av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
960 av_frame_set_pkt_duration (frame, 0);
961 av_frame_set_pkt_pos (frame, -1);
962 av_frame_set_pkt_size (frame, -1);
963 frame->key_frame = 1;
964 frame->sample_aspect_ratio = (AVRational) {0, 1 };
965 frame->format = -1; /* unknown */
966 frame->extended_data = frame->data;
969 AVFrame *avcodec_alloc_frame(void)
971 AVFrame *frame = av_malloc(sizeof(AVFrame));
976 frame->extended_data = NULL;
977 avcodec_get_frame_defaults(frame);
982 void avcodec_free_frame(AVFrame **frame)
986 if (!frame || !*frame)
991 if (f->extended_data != f->data)
992 av_freep(&f->extended_data);
997 #define MAKE_ACCESSORS(str, name, type, field) \
998 type av_##name##_get_##field(const str *s) { return s->field; } \
999 void av_##name##_set_##field(str *s, type v) { s->field = v; }
1001 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1002 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1003 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1005 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1007 memset(sub, 0, sizeof(*sub));
1008 sub->pts = AV_NOPTS_VALUE;
1011 static int get_bit_rate(AVCodecContext *ctx)
1014 int bits_per_sample;
1016 switch (ctx->codec_type) {
1017 case AVMEDIA_TYPE_VIDEO:
1018 case AVMEDIA_TYPE_DATA:
1019 case AVMEDIA_TYPE_SUBTITLE:
1020 case AVMEDIA_TYPE_ATTACHMENT:
1021 bit_rate = ctx->bit_rate;
1023 case AVMEDIA_TYPE_AUDIO:
1024 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1025 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1034 #if FF_API_AVCODEC_OPEN
1035 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1037 return avcodec_open2(avctx, codec, NULL);
1041 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1045 ff_unlock_avcodec();
1047 ret = avcodec_open2(avctx, codec, options);
1049 ff_lock_avcodec(avctx);
1053 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1056 AVDictionary *tmp = NULL;
1058 if (avcodec_is_open(avctx))
1061 if ((!codec && !avctx->codec)) {
1062 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1063 return AVERROR(EINVAL);
1065 if ((codec && avctx->codec && codec != avctx->codec)) {
1066 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1067 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1068 return AVERROR(EINVAL);
1071 codec = avctx->codec;
1073 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1074 return AVERROR(EINVAL);
1077 av_dict_copy(&tmp, *options, 0);
1079 ret = ff_lock_avcodec(avctx);
1083 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1084 if (!avctx->internal) {
1085 ret = AVERROR(ENOMEM);
1089 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1090 if (!avctx->internal->pool) {
1091 ret = AVERROR(ENOMEM);
1095 if (codec->priv_data_size > 0) {
1096 if (!avctx->priv_data) {
1097 avctx->priv_data = av_mallocz(codec->priv_data_size);
1098 if (!avctx->priv_data) {
1099 ret = AVERROR(ENOMEM);
1102 if (codec->priv_class) {
1103 *(const AVClass **)avctx->priv_data = codec->priv_class;
1104 av_opt_set_defaults(avctx->priv_data);
1107 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1110 avctx->priv_data = NULL;
1112 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1115 // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1116 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1117 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1118 if (avctx->coded_width && avctx->coded_height)
1119 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1120 else if (avctx->width && avctx->height)
1121 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1124 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1125 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1126 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1127 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1128 avcodec_set_dimensions(avctx, 0, 0);
1131 /* if the decoder init function was already called previously,
1132 * free the already allocated subtitle_header before overwriting it */
1133 if (av_codec_is_decoder(codec))
1134 av_freep(&avctx->subtitle_header);
1136 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1137 ret = AVERROR(EINVAL);
1141 avctx->codec = codec;
1142 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1143 avctx->codec_id == AV_CODEC_ID_NONE) {
1144 avctx->codec_type = codec->type;
1145 avctx->codec_id = codec->id;
1147 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1148 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1149 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1150 ret = AVERROR(EINVAL);
1153 avctx->frame_number = 0;
1154 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1156 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1157 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1158 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1160 av_log(avctx, AV_LOG_ERROR,
1161 "The %s '%s' is experimental but experimental codecs are not enabled, "
1162 "add '-strict %d' if you want to use it.\n",
1163 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1164 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1165 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1166 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1167 codec_string, codec2->name);
1168 ret = AVERROR_EXPERIMENTAL;
1172 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1173 (!avctx->time_base.num || !avctx->time_base.den)) {
1174 avctx->time_base.num = 1;
1175 avctx->time_base.den = avctx->sample_rate;
1179 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1181 if (CONFIG_FRAME_THREAD_ENCODER) {
1182 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1183 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1184 ff_lock_avcodec(avctx);
1190 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1191 ret = ff_thread_init(avctx);
1196 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1197 avctx->thread_count = 1;
1199 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1200 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1201 avctx->codec->max_lowres);
1202 ret = AVERROR(EINVAL);
1206 if (av_codec_is_encoder(avctx->codec)) {
1208 if (avctx->codec->sample_fmts) {
1209 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1210 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1212 if (avctx->channels == 1 &&
1213 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1214 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1215 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1219 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1221 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1222 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1223 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1224 ret = AVERROR(EINVAL);
1228 if (avctx->codec->pix_fmts) {
1229 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1230 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1232 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1233 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1234 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1236 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1237 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1238 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1239 ret = AVERROR(EINVAL);
1243 if (avctx->codec->supported_samplerates) {
1244 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1245 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1247 if (avctx->codec->supported_samplerates[i] == 0) {
1248 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1249 avctx->sample_rate);
1250 ret = AVERROR(EINVAL);
1254 if (avctx->codec->channel_layouts) {
1255 if (!avctx->channel_layout) {
1256 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1258 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1259 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1261 if (avctx->codec->channel_layouts[i] == 0) {
1263 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1264 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1265 ret = AVERROR(EINVAL);
1270 if (avctx->channel_layout && avctx->channels) {
1271 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1272 if (channels != avctx->channels) {
1274 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1275 av_log(avctx, AV_LOG_ERROR,
1276 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1277 buf, channels, avctx->channels);
1278 ret = AVERROR(EINVAL);
1281 } else if (avctx->channel_layout) {
1282 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1284 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1285 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1287 if (avctx->width <= 0 || avctx->height <= 0) {
1288 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1289 ret = AVERROR(EINVAL);
1293 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1294 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1295 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1298 if (!avctx->rc_initial_buffer_occupancy)
1299 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1302 avctx->pts_correction_num_faulty_pts =
1303 avctx->pts_correction_num_faulty_dts = 0;
1304 avctx->pts_correction_last_pts =
1305 avctx->pts_correction_last_dts = INT64_MIN;
1307 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1308 || avctx->internal->frame_thread_encoder)) {
1309 ret = avctx->codec->init(avctx);
1317 if (av_codec_is_decoder(avctx->codec)) {
1318 if (!avctx->bit_rate)
1319 avctx->bit_rate = get_bit_rate(avctx);
1320 /* validate channel layout from the decoder */
1321 if (avctx->channel_layout) {
1322 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1323 if (!avctx->channels)
1324 avctx->channels = channels;
1325 else if (channels != avctx->channels) {
1327 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1328 av_log(avctx, AV_LOG_WARNING,
1329 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1330 "ignoring specified channel layout\n",
1331 buf, channels, avctx->channels);
1332 avctx->channel_layout = 0;
1335 if (avctx->channels && avctx->channels < 0 ||
1336 avctx->channels > FF_SANE_NB_CHANNELS) {
1337 ret = AVERROR(EINVAL);
1340 if (avctx->sub_charenc) {
1341 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1342 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1343 "supported with subtitles codecs\n");
1344 ret = AVERROR(EINVAL);
1346 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1347 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1348 "subtitles character encoding will be ignored\n",
1349 avctx->codec_descriptor->name);
1350 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1352 /* input character encoding is set for a text based subtitle
1353 * codec at this point */
1354 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1355 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1357 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1359 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1360 if (cd == (iconv_t)-1) {
1361 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1362 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1363 ret = AVERROR(errno);
1368 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1369 "conversion needs a libavcodec built with iconv support "
1370 "for this codec\n");
1371 ret = AVERROR(ENOSYS);
1379 ff_unlock_avcodec();
1381 av_dict_free(options);
1388 av_freep(&avctx->priv_data);
1389 if (avctx->internal)
1390 av_freep(&avctx->internal->pool);
1391 av_freep(&avctx->internal);
1392 avctx->codec = NULL;
1396 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1398 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1399 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1400 return AVERROR(EINVAL);
1404 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1405 if (!avpkt->data || avpkt->size < size) {
1406 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1407 avpkt->data = avctx->internal->byte_buffer;
1408 avpkt->size = avctx->internal->byte_buffer_size;
1409 avpkt->destruct = NULL;
1414 AVBufferRef *buf = avpkt->buf;
1415 #if FF_API_DESTRUCT_PACKET
1416 void *destruct = avpkt->destruct;
1419 if (avpkt->size < size) {
1420 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1421 return AVERROR(EINVAL);
1424 av_init_packet(avpkt);
1425 #if FF_API_DESTRUCT_PACKET
1426 avpkt->destruct = destruct;
1432 int ret = av_new_packet(avpkt, size);
1434 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1439 int ff_alloc_packet(AVPacket *avpkt, int size)
1441 return ff_alloc_packet2(NULL, avpkt, size);
1445 * Pad last frame with silence.
1447 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1449 AVFrame *frame = NULL;
1452 if (!(frame = avcodec_alloc_frame()))
1453 return AVERROR(ENOMEM);
1455 frame->format = src->format;
1456 frame->channel_layout = src->channel_layout;
1457 av_frame_set_channels(frame, av_frame_get_channels(src));
1458 frame->nb_samples = s->frame_size;
1459 ret = av_frame_get_buffer(frame, 32);
1463 ret = av_frame_copy_props(frame, src);
1467 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1468 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1470 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1471 frame->nb_samples - src->nb_samples,
1472 s->channels, s->sample_fmt)) < 0)
1480 av_frame_free(&frame);
1484 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1486 const AVFrame *frame,
1487 int *got_packet_ptr)
1490 AVFrame *padded_frame = NULL;
1492 AVPacket user_pkt = *avpkt;
1493 int needs_realloc = !user_pkt.data;
1495 *got_packet_ptr = 0;
1497 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1498 av_free_packet(avpkt);
1499 av_init_packet(avpkt);
1503 /* ensure that extended_data is properly set */
1504 if (frame && !frame->extended_data) {
1505 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1506 avctx->channels > AV_NUM_DATA_POINTERS) {
1507 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1508 "with more than %d channels, but extended_data is not set.\n",
1509 AV_NUM_DATA_POINTERS);
1510 return AVERROR(EINVAL);
1512 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1515 tmp.extended_data = tmp.data;
1519 /* check for valid frame size */
1521 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1522 if (frame->nb_samples > avctx->frame_size) {
1523 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1524 return AVERROR(EINVAL);
1526 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1527 if (frame->nb_samples < avctx->frame_size &&
1528 !avctx->internal->last_audio_frame) {
1529 ret = pad_last_frame(avctx, &padded_frame, frame);
1533 frame = padded_frame;
1534 avctx->internal->last_audio_frame = 1;
1537 if (frame->nb_samples != avctx->frame_size) {
1538 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1539 ret = AVERROR(EINVAL);
1545 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1547 if (*got_packet_ptr) {
1548 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1549 if (avpkt->pts == AV_NOPTS_VALUE)
1550 avpkt->pts = frame->pts;
1551 if (!avpkt->duration)
1552 avpkt->duration = ff_samples_to_time_base(avctx,
1555 avpkt->dts = avpkt->pts;
1560 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1562 if (user_pkt.data) {
1563 if (user_pkt.size >= avpkt->size) {
1564 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1566 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1567 avpkt->size = user_pkt.size;
1570 avpkt->buf = user_pkt.buf;
1571 avpkt->data = user_pkt.data;
1572 avpkt->destruct = user_pkt.destruct;
1574 if (av_dup_packet(avpkt) < 0) {
1575 ret = AVERROR(ENOMEM);
1581 if (needs_realloc && avpkt->data) {
1582 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1584 avpkt->data = avpkt->buf->data;
1587 avctx->frame_number++;
1590 if (ret < 0 || !*got_packet_ptr) {
1591 av_free_packet(avpkt);
1592 av_init_packet(avpkt);
1596 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1597 * this needs to be moved to the encoders, but for now we can do it
1598 * here to simplify things */
1599 avpkt->flags |= AV_PKT_FLAG_KEY;
1602 av_frame_free(&padded_frame);
1607 #if FF_API_OLD_ENCODE_AUDIO
1608 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1609 uint8_t *buf, int buf_size,
1610 const short *samples)
1613 AVFrame frame0 = { { 0 } };
1615 int ret, samples_size, got_packet;
1617 av_init_packet(&pkt);
1619 pkt.size = buf_size;
1623 avcodec_get_frame_defaults(frame);
1625 if (avctx->frame_size) {
1626 frame->nb_samples = avctx->frame_size;
1628 /* if frame_size is not set, the number of samples must be
1629 * calculated from the buffer size */
1631 if (!av_get_bits_per_sample(avctx->codec_id)) {
1632 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1633 "support this codec\n");
1634 return AVERROR(EINVAL);
1636 nb_samples = (int64_t)buf_size * 8 /
1637 (av_get_bits_per_sample(avctx->codec_id) *
1639 if (nb_samples >= INT_MAX)
1640 return AVERROR(EINVAL);
1641 frame->nb_samples = nb_samples;
1644 /* it is assumed that the samples buffer is large enough based on the
1645 * relevant parameters */
1646 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1648 avctx->sample_fmt, 1);
1649 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1651 (const uint8_t *)samples,
1652 samples_size, 1)) < 0)
1655 /* fabricate frame pts from sample count.
1656 * this is needed because the avcodec_encode_audio() API does not have
1657 * a way for the user to provide pts */
1658 if (avctx->sample_rate && avctx->time_base.num)
1659 frame->pts = ff_samples_to_time_base(avctx,
1660 avctx->internal->sample_count);
1662 frame->pts = AV_NOPTS_VALUE;
1663 avctx->internal->sample_count += frame->nb_samples;
1669 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1670 if (!ret && got_packet && avctx->coded_frame) {
1671 avctx->coded_frame->pts = pkt.pts;
1672 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1674 /* free any side data since we cannot return it */
1675 ff_packet_free_side_data(&pkt);
1677 if (frame && frame->extended_data != frame->data)
1678 av_freep(&frame->extended_data);
1680 return ret ? ret : pkt.size;
1685 #if FF_API_OLD_ENCODE_VIDEO
1686 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1687 const AVFrame *pict)
1690 int ret, got_packet = 0;
1692 if (buf_size < FF_MIN_BUFFER_SIZE) {
1693 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1697 av_init_packet(&pkt);
1699 pkt.size = buf_size;
1701 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1702 if (!ret && got_packet && avctx->coded_frame) {
1703 avctx->coded_frame->pts = pkt.pts;
1704 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1707 /* free any side data since we cannot return it */
1708 if (pkt.side_data_elems > 0) {
1710 for (i = 0; i < pkt.side_data_elems; i++)
1711 av_free(pkt.side_data[i].data);
1712 av_freep(&pkt.side_data);
1713 pkt.side_data_elems = 0;
1716 return ret ? ret : pkt.size;
1721 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1723 const AVFrame *frame,
1724 int *got_packet_ptr)
1727 AVPacket user_pkt = *avpkt;
1728 int needs_realloc = !user_pkt.data;
1730 *got_packet_ptr = 0;
1732 if(CONFIG_FRAME_THREAD_ENCODER &&
1733 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1734 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1736 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1737 avctx->stats_out[0] = '\0';
1739 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1740 av_free_packet(avpkt);
1741 av_init_packet(avpkt);
1746 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1747 return AVERROR(EINVAL);
1749 av_assert0(avctx->codec->encode2);
1751 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1752 av_assert0(ret <= 0);
1754 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1756 if (user_pkt.data) {
1757 if (user_pkt.size >= avpkt->size) {
1758 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1760 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1761 avpkt->size = user_pkt.size;
1764 avpkt->buf = user_pkt.buf;
1765 avpkt->data = user_pkt.data;
1766 avpkt->destruct = user_pkt.destruct;
1768 if (av_dup_packet(avpkt) < 0) {
1769 ret = AVERROR(ENOMEM);
1775 if (!*got_packet_ptr)
1777 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1778 avpkt->pts = avpkt->dts = frame->pts;
1780 if (needs_realloc && avpkt->data) {
1781 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1783 avpkt->data = avpkt->buf->data;
1786 avctx->frame_number++;
1789 if (ret < 0 || !*got_packet_ptr)
1790 av_free_packet(avpkt);
1792 av_packet_merge_side_data(avpkt);
1798 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1799 const AVSubtitle *sub)
1802 if (sub->start_display_time) {
1803 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1807 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1808 avctx->frame_number++;
1813 * Attempt to guess proper monotonic timestamps for decoded video frames
1814 * which might have incorrect times. Input timestamps may wrap around, in
1815 * which case the output will as well.
1817 * @param pts the pts field of the decoded AVPacket, as passed through
1819 * @param dts the dts field of the decoded AVPacket
1820 * @return one of the input values, may be AV_NOPTS_VALUE
1822 static int64_t guess_correct_pts(AVCodecContext *ctx,
1823 int64_t reordered_pts, int64_t dts)
1825 int64_t pts = AV_NOPTS_VALUE;
1827 if (dts != AV_NOPTS_VALUE) {
1828 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1829 ctx->pts_correction_last_dts = dts;
1831 if (reordered_pts != AV_NOPTS_VALUE) {
1832 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1833 ctx->pts_correction_last_pts = reordered_pts;
1835 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1836 && reordered_pts != AV_NOPTS_VALUE)
1837 pts = reordered_pts;
1844 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1847 const uint8_t *data;
1850 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1853 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1854 if (!data || size < 4)
1856 flags = bytestream_get_le32(&data);
1858 if (size < 4) /* Required for any of the changes */
1860 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1861 avctx->channels = bytestream_get_le32(&data);
1864 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1867 avctx->channel_layout = bytestream_get_le64(&data);
1872 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1873 avctx->sample_rate = bytestream_get_le32(&data);
1876 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1879 avctx->width = bytestream_get_le32(&data);
1880 avctx->height = bytestream_get_le32(&data);
1881 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1886 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1889 const uint8_t *side_metadata;
1892 side_metadata = av_packet_get_side_data(avctx->pkt,
1893 AV_PKT_DATA_STRINGS_METADATA, &size);
1896 end = side_metadata + size;
1897 while (side_metadata < end) {
1898 const uint8_t *key = side_metadata;
1899 const uint8_t *val = side_metadata + strlen(key) + 1;
1900 int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
1903 side_metadata = val + strlen(val) + 1;
1909 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1910 int *got_picture_ptr,
1911 const AVPacket *avpkt)
1913 AVCodecInternal *avci = avctx->internal;
1915 // copy to ensure we do not change avpkt
1916 AVPacket tmp = *avpkt;
1918 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1919 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1920 return AVERROR(EINVAL);
1923 *got_picture_ptr = 0;
1924 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1925 return AVERROR(EINVAL);
1927 avcodec_get_frame_defaults(picture);
1929 if (!avctx->refcounted_frames)
1930 av_frame_unref(&avci->to_free);
1932 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1933 int did_split = av_packet_split_side_data(&tmp);
1934 apply_param_change(avctx, &tmp);
1936 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1937 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1940 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1942 picture->pkt_dts = avpkt->dts;
1944 if(!avctx->has_b_frames){
1945 av_frame_set_pkt_pos(picture, avpkt->pos);
1947 //FIXME these should be under if(!avctx->has_b_frames)
1948 /* get_buffer is supposed to set frame parameters */
1949 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1950 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1951 if (!picture->width) picture->width = avctx->width;
1952 if (!picture->height) picture->height = avctx->height;
1953 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1956 add_metadata_from_side_data(avctx, picture);
1958 emms_c(); //needed to avoid an emms_c() call before every return;
1962 ff_packet_free_side_data(&tmp);
1967 if (ret < 0 && picture->data[0])
1968 av_frame_unref(picture);
1970 if (*got_picture_ptr) {
1971 if (!avctx->refcounted_frames) {
1972 avci->to_free = *picture;
1973 avci->to_free.extended_data = avci->to_free.data;
1974 memset(picture->buf, 0, sizeof(picture->buf));
1977 avctx->frame_number++;
1978 av_frame_set_best_effort_timestamp(picture,
1979 guess_correct_pts(avctx,
1986 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1987 * make sure it's set correctly */
1988 picture->extended_data = picture->data;
1993 #if FF_API_OLD_DECODE_AUDIO
1994 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1995 int *frame_size_ptr,
1998 AVFrame frame = { { 0 } };
1999 int ret, got_frame = 0;
2001 if (avctx->get_buffer != avcodec_default_get_buffer) {
2002 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2003 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2004 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2005 "avcodec_decode_audio4()\n");
2006 avctx->get_buffer = avcodec_default_get_buffer;
2007 avctx->release_buffer = avcodec_default_release_buffer;
2010 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
2012 if (ret >= 0 && got_frame) {
2014 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2015 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2017 avctx->sample_fmt, 1);
2018 if (*frame_size_ptr < data_size) {
2019 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2020 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2021 return AVERROR(EINVAL);
2024 memcpy(samples, frame.extended_data[0], plane_size);
2026 if (planar && avctx->channels > 1) {
2027 uint8_t *out = ((uint8_t *)samples) + plane_size;
2028 for (ch = 1; ch < avctx->channels; ch++) {
2029 memcpy(out, frame.extended_data[ch], plane_size);
2033 *frame_size_ptr = data_size;
2035 *frame_size_ptr = 0;
2042 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2045 const AVPacket *avpkt)
2047 AVCodecInternal *avci = avctx->internal;
2048 int planar, channels;
2053 if (!avpkt->data && avpkt->size) {
2054 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2055 return AVERROR(EINVAL);
2057 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2058 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2059 return AVERROR(EINVAL);
2062 avcodec_get_frame_defaults(frame);
2064 if (!avctx->refcounted_frames)
2065 av_frame_unref(&avci->to_free);
2067 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2070 // copy to ensure we do not change avpkt
2071 AVPacket tmp = *avpkt;
2072 int did_split = av_packet_split_side_data(&tmp);
2073 apply_param_change(avctx, &tmp);
2076 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2077 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2079 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2080 frame->pkt_dts = avpkt->dts;
2082 if (ret >= 0 && *got_frame_ptr) {
2083 add_metadata_from_side_data(avctx, frame);
2084 avctx->frame_number++;
2085 av_frame_set_best_effort_timestamp(frame,
2086 guess_correct_pts(avctx,
2089 if (frame->format == AV_SAMPLE_FMT_NONE)
2090 frame->format = avctx->sample_fmt;
2091 if (!frame->channel_layout)
2092 frame->channel_layout = avctx->channel_layout;
2093 if (!av_frame_get_channels(frame))
2094 av_frame_set_channels(frame, avctx->channels);
2095 if (!frame->sample_rate)
2096 frame->sample_rate = avctx->sample_rate;
2099 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2100 if(side && side_size>=10) {
2101 avctx->internal->skip_samples = AV_RL32(side);
2102 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2103 avctx->internal->skip_samples);
2105 if (avctx->internal->skip_samples && *got_frame_ptr) {
2106 if(frame->nb_samples <= avctx->internal->skip_samples){
2108 avctx->internal->skip_samples -= frame->nb_samples;
2109 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2110 avctx->internal->skip_samples);
2112 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2113 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2114 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2115 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2116 (AVRational){1, avctx->sample_rate},
2117 avctx->pkt_timebase);
2118 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2119 frame->pkt_pts += diff_ts;
2120 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2121 frame->pkt_dts += diff_ts;
2122 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2123 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2125 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2127 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2128 avctx->internal->skip_samples, frame->nb_samples);
2129 frame->nb_samples -= avctx->internal->skip_samples;
2130 avctx->internal->skip_samples = 0;
2136 ff_packet_free_side_data(&tmp);
2141 if (ret >= 0 && *got_frame_ptr) {
2142 if (!avctx->refcounted_frames) {
2143 avci->to_free = *frame;
2144 avci->to_free.extended_data = avci->to_free.data;
2145 memset(frame->buf, 0, sizeof(frame->buf));
2146 frame->extended_buf = NULL;
2147 frame->nb_extended_buf = 0;
2149 } else if (frame->data[0])
2150 av_frame_unref(frame);
2153 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2154 * make sure it's set correctly; assume decoders that actually use
2155 * extended_data are doing it correctly */
2156 if (*got_frame_ptr) {
2157 planar = av_sample_fmt_is_planar(frame->format);
2158 channels = av_frame_get_channels(frame);
2159 if (!(planar && channels > AV_NUM_DATA_POINTERS))
2160 frame->extended_data = frame->data;
2162 frame->extended_data = NULL;
2168 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2169 static int recode_subtitle(AVCodecContext *avctx,
2170 AVPacket *outpkt, const AVPacket *inpkt)
2173 iconv_t cd = (iconv_t)-1;
2180 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
2184 cd = iconv_open("UTF-8", avctx->sub_charenc);
2185 av_assert0(cd != (iconv_t)-1);
2190 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2191 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2192 ret = AVERROR(ENOMEM);
2196 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2199 outpkt->buf = tmp.buf;
2200 outpkt->data = tmp.data;
2201 outpkt->size = tmp.size;
2202 outb = outpkt->data;
2203 outl = outpkt->size;
2205 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2206 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2207 outl >= outpkt->size || inl != 0) {
2208 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2209 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2210 av_free_packet(&tmp);
2211 ret = AVERROR(errno);
2214 outpkt->size -= outl;
2215 memset(outpkt->data + outpkt->size, 0, outl);
2218 if (cd != (iconv_t)-1)
2222 av_assert0(!"requesting subtitles recoding without iconv");
2226 static int utf8_check(const uint8_t *str)
2228 const uint8_t *byte;
2229 uint32_t codepoint, min;
2233 GET_UTF8(codepoint, *(byte++), return 0;);
2234 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2235 1 << (5 * (byte - str) - 4);
2236 if (codepoint < min || codepoint >= 0x110000 ||
2237 codepoint == 0xFFFE /* BOM */ ||
2238 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2245 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2251 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2252 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2253 return AVERROR(EINVAL);
2257 avcodec_get_subtitle_defaults(sub);
2260 AVPacket pkt_recoded;
2261 AVPacket tmp = *avpkt;
2262 int did_split = av_packet_split_side_data(&tmp);
2263 //apply_param_change(avctx, &tmp);
2266 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2270 avctx->pkt = &pkt_recoded;
2272 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2273 sub->pts = av_rescale_q(avpkt->pts,
2274 avctx->pkt_timebase, AV_TIME_BASE_Q);
2275 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2276 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2277 !!*got_sub_ptr >= !!sub->num_rects);
2279 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2280 avctx->pkt_timebase.num) {
2281 AVRational ms = { 1, 1000 };
2282 sub->end_display_time = av_rescale_q(avpkt->duration,
2283 avctx->pkt_timebase, ms);
2286 for (i = 0; i < sub->num_rects; i++) {
2287 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2288 av_log(avctx, AV_LOG_ERROR,
2289 "Invalid UTF-8 in decoded subtitles text; "
2290 "maybe missing -sub_charenc option\n");
2291 avsubtitle_free(sub);
2292 return AVERROR_INVALIDDATA;
2296 if (tmp.data != pkt_recoded.data) { // did we recode?
2297 /* prevent from destroying side data from original packet */
2298 pkt_recoded.side_data = NULL;
2299 pkt_recoded.side_data_elems = 0;
2301 av_free_packet(&pkt_recoded);
2303 sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
2308 ff_packet_free_side_data(&tmp);
2314 avctx->frame_number++;
2320 void avsubtitle_free(AVSubtitle *sub)
2324 for (i = 0; i < sub->num_rects; i++) {
2325 av_freep(&sub->rects[i]->pict.data[0]);
2326 av_freep(&sub->rects[i]->pict.data[1]);
2327 av_freep(&sub->rects[i]->pict.data[2]);
2328 av_freep(&sub->rects[i]->pict.data[3]);
2329 av_freep(&sub->rects[i]->text);
2330 av_freep(&sub->rects[i]->ass);
2331 av_freep(&sub->rects[i]);
2334 av_freep(&sub->rects);
2336 memset(sub, 0, sizeof(AVSubtitle));
2339 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2343 ff_unlock_avcodec();
2345 ret = avcodec_close(avctx);
2347 ff_lock_avcodec(NULL);
2351 av_cold int avcodec_close(AVCodecContext *avctx)
2353 int ret = ff_lock_avcodec(avctx);
2357 if (avcodec_is_open(avctx)) {
2358 FramePool *pool = avctx->internal->pool;
2360 if (CONFIG_FRAME_THREAD_ENCODER &&
2361 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2362 ff_unlock_avcodec();
2363 ff_frame_thread_encoder_free(avctx);
2364 ff_lock_avcodec(avctx);
2366 if (HAVE_THREADS && avctx->thread_opaque)
2367 ff_thread_free(avctx);
2368 if (avctx->codec && avctx->codec->close)
2369 avctx->codec->close(avctx);
2370 avctx->coded_frame = NULL;
2371 avctx->internal->byte_buffer_size = 0;
2372 av_freep(&avctx->internal->byte_buffer);
2373 if (!avctx->refcounted_frames)
2374 av_frame_unref(&avctx->internal->to_free);
2375 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2376 av_buffer_pool_uninit(&pool->pools[i]);
2377 av_freep(&avctx->internal->pool);
2378 av_freep(&avctx->internal);
2381 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2382 av_opt_free(avctx->priv_data);
2384 av_freep(&avctx->priv_data);
2385 if (av_codec_is_encoder(avctx->codec))
2386 av_freep(&avctx->extradata);
2387 avctx->codec = NULL;
2388 avctx->active_thread_type = 0;
2390 ff_unlock_avcodec();
2394 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2397 //This is for future deprecatec codec ids, its empty since
2398 //last major bump but will fill up again over time, please don't remove it
2399 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2400 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
2401 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2402 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2403 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2404 default : return id;
2408 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2410 AVCodec *p, *experimental = NULL;
2412 id= remap_deprecated_codec_id(id);
2414 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2416 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2423 return experimental;
2426 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2428 return find_encdec(id, 1);
2431 AVCodec *avcodec_find_encoder_by_name(const char *name)
2438 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2445 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2447 return find_encdec(id, 0);
2450 AVCodec *avcodec_find_decoder_by_name(const char *name)
2457 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2464 const char *avcodec_get_name(enum AVCodecID id)
2466 const AVCodecDescriptor *cd;
2469 if (id == AV_CODEC_ID_NONE)
2471 cd = avcodec_descriptor_get(id);
2474 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2475 codec = avcodec_find_decoder(id);
2478 codec = avcodec_find_encoder(id);
2481 return "unknown_codec";
2484 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2486 int i, len, ret = 0;
2488 #define TAG_PRINT(x) \
2489 (((x) >= '0' && (x) <= '9') || \
2490 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2491 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2493 for (i = 0; i < 4; i++) {
2494 len = snprintf(buf, buf_size,
2495 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2497 buf_size = buf_size > len ? buf_size - len : 0;
2504 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2506 const char *codec_type;
2507 const char *codec_name;
2508 const char *profile = NULL;
2511 AVRational display_aspect_ratio;
2513 if (!buf || buf_size <= 0)
2515 codec_type = av_get_media_type_string(enc->codec_type);
2516 codec_name = avcodec_get_name(enc->codec_id);
2517 if (enc->profile != FF_PROFILE_UNKNOWN) {
2521 p = encode ? avcodec_find_encoder(enc->codec_id) :
2522 avcodec_find_decoder(enc->codec_id);
2524 profile = av_get_profile_name(p, enc->profile);
2527 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2529 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2531 if (enc->codec && strcmp(enc->codec->name, codec_name))
2532 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2535 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2536 if (enc->codec_tag) {
2538 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2539 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2540 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2543 switch (enc->codec_type) {
2544 case AVMEDIA_TYPE_VIDEO:
2545 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2546 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2548 av_get_pix_fmt_name(enc->pix_fmt));
2549 if (enc->bits_per_raw_sample &&
2550 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2551 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2552 " (%d bpc)", enc->bits_per_raw_sample);
2555 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2557 enc->width, enc->height);
2558 if (enc->sample_aspect_ratio.num) {
2559 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2560 enc->width * enc->sample_aspect_ratio.num,
2561 enc->height * enc->sample_aspect_ratio.den,
2563 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2564 " [SAR %d:%d DAR %d:%d]",
2565 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2566 display_aspect_ratio.num, display_aspect_ratio.den);
2568 if (av_log_get_level() >= AV_LOG_DEBUG) {
2569 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2570 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2572 enc->time_base.num / g, enc->time_base.den / g);
2576 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2577 ", q=%d-%d", enc->qmin, enc->qmax);
2580 case AVMEDIA_TYPE_AUDIO:
2581 if (enc->sample_rate) {
2582 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2583 ", %d Hz", enc->sample_rate);
2585 av_strlcat(buf, ", ", buf_size);
2586 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2587 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2588 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2589 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2592 case AVMEDIA_TYPE_DATA:
2593 if (av_log_get_level() >= AV_LOG_DEBUG) {
2594 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2596 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2598 enc->time_base.num / g, enc->time_base.den / g);
2605 if (enc->flags & CODEC_FLAG_PASS1)
2606 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2608 if (enc->flags & CODEC_FLAG_PASS2)
2609 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2612 bitrate = get_bit_rate(enc);
2614 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2615 ", %d kb/s", bitrate / 1000);
2619 const char *av_get_profile_name(const AVCodec *codec, int profile)
2622 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2625 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2626 if (p->profile == profile)
2632 unsigned avcodec_version(void)
2634 // av_assert0(AV_CODEC_ID_V410==164);
2635 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2636 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2637 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2638 av_assert0(AV_CODEC_ID_SRT==94216);
2639 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2641 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2642 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2643 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2644 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2645 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2646 return LIBAVCODEC_VERSION_INT;
2649 const char *avcodec_configuration(void)
2651 return FFMPEG_CONFIGURATION;
2654 const char *avcodec_license(void)
2656 #define LICENSE_PREFIX "libavcodec license: "
2657 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2660 void avcodec_flush_buffers(AVCodecContext *avctx)
2662 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2663 ff_thread_flush(avctx);
2664 else if (avctx->codec->flush)
2665 avctx->codec->flush(avctx);
2667 avctx->pts_correction_last_pts =
2668 avctx->pts_correction_last_dts = INT64_MIN;
2670 if (!avctx->refcounted_frames)
2671 av_frame_unref(&avctx->internal->to_free);
2674 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2677 case AV_CODEC_ID_8SVX_EXP:
2678 case AV_CODEC_ID_8SVX_FIB:
2679 case AV_CODEC_ID_ADPCM_CT:
2680 case AV_CODEC_ID_ADPCM_IMA_APC:
2681 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2682 case AV_CODEC_ID_ADPCM_IMA_OKI:
2683 case AV_CODEC_ID_ADPCM_IMA_WS:
2684 case AV_CODEC_ID_ADPCM_G722:
2685 case AV_CODEC_ID_ADPCM_YAMAHA:
2687 case AV_CODEC_ID_PCM_ALAW:
2688 case AV_CODEC_ID_PCM_MULAW:
2689 case AV_CODEC_ID_PCM_S8:
2690 case AV_CODEC_ID_PCM_S8_PLANAR:
2691 case AV_CODEC_ID_PCM_U8:
2692 case AV_CODEC_ID_PCM_ZORK:
2694 case AV_CODEC_ID_PCM_S16BE:
2695 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2696 case AV_CODEC_ID_PCM_S16LE:
2697 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2698 case AV_CODEC_ID_PCM_U16BE:
2699 case AV_CODEC_ID_PCM_U16LE:
2701 case AV_CODEC_ID_PCM_S24DAUD:
2702 case AV_CODEC_ID_PCM_S24BE:
2703 case AV_CODEC_ID_PCM_S24LE:
2704 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2705 case AV_CODEC_ID_PCM_U24BE:
2706 case AV_CODEC_ID_PCM_U24LE:
2708 case AV_CODEC_ID_PCM_S32BE:
2709 case AV_CODEC_ID_PCM_S32LE:
2710 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2711 case AV_CODEC_ID_PCM_U32BE:
2712 case AV_CODEC_ID_PCM_U32LE:
2713 case AV_CODEC_ID_PCM_F32BE:
2714 case AV_CODEC_ID_PCM_F32LE:
2716 case AV_CODEC_ID_PCM_F64BE:
2717 case AV_CODEC_ID_PCM_F64LE:
2724 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2726 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2727 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2728 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2729 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2730 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2731 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2732 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2733 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2734 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2735 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2736 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2738 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2739 return AV_CODEC_ID_NONE;
2740 if (be < 0 || be > 1)
2742 return map[fmt][be];
2745 int av_get_bits_per_sample(enum AVCodecID codec_id)
2748 case AV_CODEC_ID_ADPCM_SBPRO_2:
2750 case AV_CODEC_ID_ADPCM_SBPRO_3:
2752 case AV_CODEC_ID_ADPCM_SBPRO_4:
2753 case AV_CODEC_ID_ADPCM_IMA_WAV:
2754 case AV_CODEC_ID_ADPCM_IMA_QT:
2755 case AV_CODEC_ID_ADPCM_SWF:
2756 case AV_CODEC_ID_ADPCM_MS:
2759 return av_get_exact_bits_per_sample(codec_id);
2763 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2765 int id, sr, ch, ba, tag, bps;
2767 id = avctx->codec_id;
2768 sr = avctx->sample_rate;
2769 ch = avctx->channels;
2770 ba = avctx->block_align;
2771 tag = avctx->codec_tag;
2772 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2774 /* codecs with an exact constant bits per sample */
2775 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2776 return (frame_bytes * 8LL) / (bps * ch);
2777 bps = avctx->bits_per_coded_sample;
2779 /* codecs with a fixed packet duration */
2781 case AV_CODEC_ID_ADPCM_ADX: return 32;
2782 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2783 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2784 case AV_CODEC_ID_AMR_NB:
2785 case AV_CODEC_ID_EVRC:
2786 case AV_CODEC_ID_GSM:
2787 case AV_CODEC_ID_QCELP:
2788 case AV_CODEC_ID_RA_288: return 160;
2789 case AV_CODEC_ID_AMR_WB:
2790 case AV_CODEC_ID_GSM_MS: return 320;
2791 case AV_CODEC_ID_MP1: return 384;
2792 case AV_CODEC_ID_ATRAC1: return 512;
2793 case AV_CODEC_ID_ATRAC3: return 1024;
2794 case AV_CODEC_ID_MP2:
2795 case AV_CODEC_ID_MUSEPACK7: return 1152;
2796 case AV_CODEC_ID_AC3: return 1536;
2800 /* calc from sample rate */
2801 if (id == AV_CODEC_ID_TTA)
2802 return 256 * sr / 245;
2805 /* calc from sample rate and channels */
2806 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2807 return (480 << (sr / 22050)) / ch;
2812 /* calc from block_align */
2813 if (id == AV_CODEC_ID_SIPR) {
2815 case 20: return 160;
2816 case 19: return 144;
2817 case 29: return 288;
2818 case 37: return 480;
2820 } else if (id == AV_CODEC_ID_ILBC) {
2822 case 38: return 160;
2823 case 50: return 240;
2828 if (frame_bytes > 0) {
2829 /* calc from frame_bytes only */
2830 if (id == AV_CODEC_ID_TRUESPEECH)
2831 return 240 * (frame_bytes / 32);
2832 if (id == AV_CODEC_ID_NELLYMOSER)
2833 return 256 * (frame_bytes / 64);
2834 if (id == AV_CODEC_ID_RA_144)
2835 return 160 * (frame_bytes / 20);
2836 if (id == AV_CODEC_ID_G723_1)
2837 return 240 * (frame_bytes / 24);
2840 /* calc from frame_bytes and bits_per_coded_sample */
2841 if (id == AV_CODEC_ID_ADPCM_G726)
2842 return frame_bytes * 8 / bps;
2846 /* calc from frame_bytes and channels */
2848 case AV_CODEC_ID_ADPCM_AFC:
2849 return frame_bytes / (9 * ch) * 16;
2850 case AV_CODEC_ID_ADPCM_DTK:
2851 return frame_bytes / (16 * ch) * 28;
2852 case AV_CODEC_ID_ADPCM_4XM:
2853 case AV_CODEC_ID_ADPCM_IMA_ISS:
2854 return (frame_bytes - 4 * ch) * 2 / ch;
2855 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2856 return (frame_bytes - 4) * 2 / ch;
2857 case AV_CODEC_ID_ADPCM_IMA_AMV:
2858 return (frame_bytes - 8) * 2 / ch;
2859 case AV_CODEC_ID_ADPCM_XA:
2860 return (frame_bytes / 128) * 224 / ch;
2861 case AV_CODEC_ID_INTERPLAY_DPCM:
2862 return (frame_bytes - 6 - ch) / ch;
2863 case AV_CODEC_ID_ROQ_DPCM:
2864 return (frame_bytes - 8) / ch;
2865 case AV_CODEC_ID_XAN_DPCM:
2866 return (frame_bytes - 2 * ch) / ch;
2867 case AV_CODEC_ID_MACE3:
2868 return 3 * frame_bytes / ch;
2869 case AV_CODEC_ID_MACE6:
2870 return 6 * frame_bytes / ch;
2871 case AV_CODEC_ID_PCM_LXF:
2872 return 2 * (frame_bytes / (5 * ch));
2873 case AV_CODEC_ID_IAC:
2874 case AV_CODEC_ID_IMC:
2875 return 4 * frame_bytes / ch;
2879 /* calc from frame_bytes, channels, and codec_tag */
2880 if (id == AV_CODEC_ID_SOL_DPCM) {
2882 return frame_bytes / ch;
2884 return frame_bytes * 2 / ch;
2889 /* calc from frame_bytes, channels, and block_align */
2890 int blocks = frame_bytes / ba;
2891 switch (avctx->codec_id) {
2892 case AV_CODEC_ID_ADPCM_IMA_WAV:
2893 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2894 case AV_CODEC_ID_ADPCM_IMA_DK3:
2895 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2896 case AV_CODEC_ID_ADPCM_IMA_DK4:
2897 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2898 case AV_CODEC_ID_ADPCM_IMA_RAD:
2899 return blocks * ((ba - 4 * ch) * 2 / ch);
2900 case AV_CODEC_ID_ADPCM_MS:
2901 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2906 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2907 switch (avctx->codec_id) {
2908 case AV_CODEC_ID_PCM_DVD:
2911 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2912 case AV_CODEC_ID_PCM_BLURAY:
2915 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2916 case AV_CODEC_ID_S302M:
2917 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2927 int ff_thread_init(AVCodecContext *s)
2934 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2948 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2951 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2955 #if FF_API_MISSING_SAMPLE
2956 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2958 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2959 "version to the newest one from Git. If the problem still "
2960 "occurs, it means that your file has a feature which has not "
2961 "been implemented.\n", feature);
2963 av_log_ask_for_sample(avc, NULL);
2966 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2968 va_list argument_list;
2970 va_start(argument_list, msg);
2973 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2974 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2975 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2976 "and contact the ffmpeg-devel mailing list.\n");
2978 va_end(argument_list);
2980 #endif /* FF_API_MISSING_SAMPLE */
2982 static AVHWAccel *first_hwaccel = NULL;
2984 void av_register_hwaccel(AVHWAccel *hwaccel)
2986 AVHWAccel **p = &first_hwaccel;
2990 hwaccel->next = NULL;
2993 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2995 return hwaccel ? hwaccel->next : first_hwaccel;
2998 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
3000 AVHWAccel *hwaccel = NULL;
3002 while ((hwaccel = av_hwaccel_next(hwaccel)))
3003 if (hwaccel->id == codec_id
3004 && hwaccel->pix_fmt == pix_fmt)
3009 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3012 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3014 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3021 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3023 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3029 int ff_lock_avcodec(AVCodecContext *log_ctx)
3032 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3035 entangled_thread_counter++;
3036 if (entangled_thread_counter != 1) {
3037 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3038 ff_avcodec_locked = 1;
3039 ff_unlock_avcodec();
3040 return AVERROR(EINVAL);
3042 av_assert0(!ff_avcodec_locked);
3043 ff_avcodec_locked = 1;
3047 int ff_unlock_avcodec(void)
3049 av_assert0(ff_avcodec_locked);
3050 ff_avcodec_locked = 0;
3051 entangled_thread_counter--;
3053 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3059 int avpriv_lock_avformat(void)
3062 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3068 int avpriv_unlock_avformat(void)
3071 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3077 unsigned int avpriv_toupper4(unsigned int x)
3079 return av_toupper(x & 0xFF) +
3080 (av_toupper((x >> 8) & 0xFF) << 8) +
3081 (av_toupper((x >> 16) & 0xFF) << 16) +
3082 (av_toupper((x >> 24) & 0xFF) << 24);
3085 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3089 dst->owner = src->owner;
3091 ret = av_frame_ref(dst->f, src->f);
3095 if (src->progress &&
3096 !(dst->progress = av_buffer_ref(src->progress))) {
3097 ff_thread_release_buffer(dst->owner, dst);
3098 return AVERROR(ENOMEM);
3106 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3108 return avctx->get_format(avctx, fmt);
3111 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3114 return ff_get_buffer(avctx, f->f, flags);
3117 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3119 av_frame_unref(f->f);
3122 void ff_thread_finish_setup(AVCodecContext *avctx)
3126 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3130 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3134 int ff_thread_can_start_frame(AVCodecContext *avctx)
3141 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3143 AVCodec *c= avcodec_find_decoder(codec_id);
3145 c= avcodec_find_encoder(codec_id);
3149 if (codec_id <= AV_CODEC_ID_NONE)
3150 return AVMEDIA_TYPE_UNKNOWN;
3151 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3152 return AVMEDIA_TYPE_VIDEO;
3153 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3154 return AVMEDIA_TYPE_AUDIO;
3155 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3156 return AVMEDIA_TYPE_SUBTITLE;
3158 return AVMEDIA_TYPE_UNKNOWN;
3161 int avcodec_is_open(AVCodecContext *s)
3163 return !!s->internal;
3166 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3171 ret = av_bprint_finalize(buf, &str);
3174 avctx->extradata = str;
3175 /* Note: the string is NUL terminated (so extradata can be read as a
3176 * string), but the ending character is not accounted in the size (in
3177 * binary formats you are likely not supposed to mux that character). When
3178 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3180 avctx->extradata_size = buf->len;
3184 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3186 uint32_t *av_restrict state)
3194 for (i = 0; i < 3; i++) {
3195 uint32_t tmp = *state << 8;
3196 *state = tmp + *(p++);
3197 if (tmp == 0x100 || p == end)
3202 if (p[-1] > 1 ) p += 3;
3203 else if (p[-2] ) p += 2;
3204 else if (p[-3]|(p[-1]-1)) p++;
3211 p = FFMIN(p, end) - 4;
3212 *state = AV_RB32(p);