3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/atomic.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/dict.h"
45 #include "libavutil/opt.h"
47 #include "frame_thread_encoder.h"
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 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_YUV420P16LE:
227 case AV_PIX_FMT_YUV420P16BE:
228 case AV_PIX_FMT_YUV422P9LE:
229 case AV_PIX_FMT_YUV422P9BE:
230 case AV_PIX_FMT_YUV422P10LE:
231 case AV_PIX_FMT_YUV422P10BE:
232 case AV_PIX_FMT_YUV422P12LE:
233 case AV_PIX_FMT_YUV422P12BE:
234 case AV_PIX_FMT_YUV422P14LE:
235 case AV_PIX_FMT_YUV422P14BE:
236 case AV_PIX_FMT_YUV422P16LE:
237 case AV_PIX_FMT_YUV422P16BE:
238 case AV_PIX_FMT_YUV444P9LE:
239 case AV_PIX_FMT_YUV444P9BE:
240 case AV_PIX_FMT_YUV444P10LE:
241 case AV_PIX_FMT_YUV444P10BE:
242 case AV_PIX_FMT_YUV444P12LE:
243 case AV_PIX_FMT_YUV444P12BE:
244 case AV_PIX_FMT_YUV444P14LE:
245 case AV_PIX_FMT_YUV444P14BE:
246 case AV_PIX_FMT_YUV444P16LE:
247 case AV_PIX_FMT_YUV444P16BE:
248 case AV_PIX_FMT_YUVA420P9LE:
249 case AV_PIX_FMT_YUVA420P9BE:
250 case AV_PIX_FMT_YUVA420P10LE:
251 case AV_PIX_FMT_YUVA420P10BE:
252 case AV_PIX_FMT_YUVA420P16LE:
253 case AV_PIX_FMT_YUVA420P16BE:
254 case AV_PIX_FMT_YUVA422P9LE:
255 case AV_PIX_FMT_YUVA422P9BE:
256 case AV_PIX_FMT_YUVA422P10LE:
257 case AV_PIX_FMT_YUVA422P10BE:
258 case AV_PIX_FMT_YUVA422P16LE:
259 case AV_PIX_FMT_YUVA422P16BE:
260 case AV_PIX_FMT_YUVA444P9LE:
261 case AV_PIX_FMT_YUVA444P9BE:
262 case AV_PIX_FMT_YUVA444P10LE:
263 case AV_PIX_FMT_YUVA444P10BE:
264 case AV_PIX_FMT_YUVA444P16LE:
265 case AV_PIX_FMT_YUVA444P16BE:
266 case AV_PIX_FMT_GBRP9LE:
267 case AV_PIX_FMT_GBRP9BE:
268 case AV_PIX_FMT_GBRP10LE:
269 case AV_PIX_FMT_GBRP10BE:
270 case AV_PIX_FMT_GBRP12LE:
271 case AV_PIX_FMT_GBRP12BE:
272 case AV_PIX_FMT_GBRP14LE:
273 case AV_PIX_FMT_GBRP14BE:
274 w_align = 16; //FIXME assume 16 pixel per macroblock
275 h_align = 16 * 2; // interlaced needs 2 macroblocks height
277 case AV_PIX_FMT_YUV411P:
278 case AV_PIX_FMT_YUVJ411P:
279 case AV_PIX_FMT_UYYVYY411:
283 case AV_PIX_FMT_YUV410P:
284 if (s->codec_id == AV_CODEC_ID_SVQ1) {
289 case AV_PIX_FMT_RGB555:
290 if (s->codec_id == AV_CODEC_ID_RPZA) {
295 case AV_PIX_FMT_PAL8:
296 case AV_PIX_FMT_BGR8:
297 case AV_PIX_FMT_RGB8:
298 if (s->codec_id == AV_CODEC_ID_SMC ||
299 s->codec_id == AV_CODEC_ID_CINEPAK) {
304 case AV_PIX_FMT_BGR24:
305 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
306 (s->codec_id == AV_CODEC_ID_ZLIB)) {
311 case AV_PIX_FMT_RGB24:
312 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
323 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
324 w_align = FFMAX(w_align, 8);
327 *width = FFALIGN(*width, w_align);
328 *height = FFALIGN(*height, h_align);
329 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
330 // some of the optimized chroma MC reads one line too much
331 // which is also done in mpeg decoders with lowres > 0
334 for (i = 0; i < 4; i++)
335 linesize_align[i] = STRIDE_ALIGN;
338 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
340 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
341 int chroma_shift = desc->log2_chroma_w;
342 int linesize_align[AV_NUM_DATA_POINTERS];
345 avcodec_align_dimensions2(s, width, height, linesize_align);
346 align = FFMAX(linesize_align[0], linesize_align[3]);
347 linesize_align[1] <<= chroma_shift;
348 linesize_align[2] <<= chroma_shift;
349 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
350 *width = FFALIGN(*width, align);
353 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
355 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
356 return AVERROR(EINVAL);
359 *xpos = (pos&1) * 128;
360 *ypos = ((pos>>1)^(pos<4)) * 128;
365 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
369 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
370 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
373 return AVCHROMA_LOC_UNSPECIFIED;
376 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
377 enum AVSampleFormat sample_fmt, const uint8_t *buf,
378 int buf_size, int align)
380 int ch, planar, needed_size, ret = 0;
382 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
383 frame->nb_samples, sample_fmt,
385 if (buf_size < needed_size)
386 return AVERROR(EINVAL);
388 planar = av_sample_fmt_is_planar(sample_fmt);
389 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
390 if (!(frame->extended_data = av_mallocz(nb_channels *
391 sizeof(*frame->extended_data))))
392 return AVERROR(ENOMEM);
394 frame->extended_data = frame->data;
397 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
398 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
399 sample_fmt, align)) < 0) {
400 if (frame->extended_data != frame->data)
401 av_freep(&frame->extended_data);
404 if (frame->extended_data != frame->data) {
405 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
406 frame->data[ch] = frame->extended_data[ch];
412 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
414 FramePool *pool = avctx->internal->pool;
417 switch (avctx->codec_type) {
418 case AVMEDIA_TYPE_VIDEO: {
421 int w = frame->width;
422 int h = frame->height;
423 int tmpsize, unaligned;
425 if (pool->format == frame->format &&
426 pool->width == frame->width && pool->height == frame->height)
429 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
431 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
437 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
438 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
439 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
440 // increase alignment of w for next try (rhs gives the lowest bit set in w)
444 for (i = 0; i < 4; i++)
445 unaligned |= picture.linesize[i] % pool->stride_align[i];
448 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
449 NULL, picture.linesize);
453 for (i = 0; i < 3 && picture.data[i + 1]; i++)
454 size[i] = picture.data[i + 1] - picture.data[i];
455 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
457 for (i = 0; i < 4; i++) {
458 av_buffer_pool_uninit(&pool->pools[i]);
459 pool->linesize[i] = picture.linesize[i];
461 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
462 CONFIG_MEMORY_POISONING ?
465 if (!pool->pools[i]) {
466 ret = AVERROR(ENOMEM);
471 pool->format = frame->format;
472 pool->width = frame->width;
473 pool->height = frame->height;
477 case AVMEDIA_TYPE_AUDIO: {
478 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
479 int planar = av_sample_fmt_is_planar(frame->format);
480 int planes = planar ? ch : 1;
482 if (pool->format == frame->format && pool->planes == planes &&
483 pool->channels == ch && frame->nb_samples == pool->samples)
486 av_buffer_pool_uninit(&pool->pools[0]);
487 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
488 frame->nb_samples, frame->format, 0);
492 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
493 if (!pool->pools[0]) {
494 ret = AVERROR(ENOMEM);
498 pool->format = frame->format;
499 pool->planes = planes;
501 pool->samples = frame->nb_samples;
504 default: av_assert0(0);
508 for (i = 0; i < 4; i++)
509 av_buffer_pool_uninit(&pool->pools[i]);
511 pool->planes = pool->channels = pool->samples = 0;
512 pool->width = pool->height = 0;
516 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
518 FramePool *pool = avctx->internal->pool;
519 int planes = pool->planes;
522 frame->linesize[0] = pool->linesize[0];
524 if (planes > AV_NUM_DATA_POINTERS) {
525 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
526 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
527 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
528 sizeof(*frame->extended_buf));
529 if (!frame->extended_data || !frame->extended_buf) {
530 av_freep(&frame->extended_data);
531 av_freep(&frame->extended_buf);
532 return AVERROR(ENOMEM);
535 frame->extended_data = frame->data;
536 av_assert0(frame->nb_extended_buf == 0);
539 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
540 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
543 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
545 for (i = 0; i < frame->nb_extended_buf; i++) {
546 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
547 if (!frame->extended_buf[i])
549 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
552 if (avctx->debug & FF_DEBUG_BUFFERS)
553 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
557 av_frame_unref(frame);
558 return AVERROR(ENOMEM);
561 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
563 FramePool *pool = s->internal->pool;
564 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
565 int pixel_size = desc->comp[0].step_minus1 + 1;
566 int h_chroma_shift, v_chroma_shift;
569 if (pic->data[0] != NULL) {
570 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
574 memset(pic->data, 0, sizeof(pic->data));
575 pic->extended_data = pic->data;
577 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
579 for (i = 0; i < 4 && pool->pools[i]; i++) {
580 const int h_shift = i == 0 ? 0 : h_chroma_shift;
581 const int v_shift = i == 0 ? 0 : v_chroma_shift;
582 int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
584 pic->linesize[i] = pool->linesize[i];
586 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
590 // no edge if EDGE EMU or not planar YUV
591 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
592 pic->data[i] = pic->buf[i]->data;
594 pic->data[i] = pic->buf[i]->data +
595 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
596 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
599 for (; i < AV_NUM_DATA_POINTERS; i++) {
601 pic->linesize[i] = 0;
603 if (pic->data[1] && !pic->data[2])
604 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
606 if (s->debug & FF_DEBUG_BUFFERS)
607 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
612 return AVERROR(ENOMEM);
615 void avpriv_color_frame(AVFrame *frame, const int c[4])
617 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
620 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
622 for (p = 0; p<desc->nb_components; p++) {
623 uint8_t *dst = frame->data[p];
624 int is_chroma = p == 1 || p == 2;
625 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
626 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
627 for (y = 0; y < height; y++) {
628 if (desc->comp[0].depth_minus1 >= 8) {
629 for (x = 0; x<bytes; x++)
630 ((uint16_t*)dst)[x] = c[p];
632 memset(dst, c[p], bytes);
633 dst += frame->linesize[p];
638 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
642 if ((ret = update_frame_pool(avctx, frame)) < 0)
645 #if FF_API_GET_BUFFER
646 FF_DISABLE_DEPRECATION_WARNINGS
647 frame->type = FF_BUFFER_TYPE_INTERNAL;
648 FF_ENABLE_DEPRECATION_WARNINGS
651 switch (avctx->codec_type) {
652 case AVMEDIA_TYPE_VIDEO:
653 return video_get_buffer(avctx, frame);
654 case AVMEDIA_TYPE_AUDIO:
655 return audio_get_buffer(avctx, frame);
661 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
664 frame->pkt_pts = avctx->pkt->pts;
665 av_frame_set_pkt_pos (frame, avctx->pkt->pos);
666 av_frame_set_pkt_duration(frame, avctx->pkt->duration);
667 av_frame_set_pkt_size (frame, avctx->pkt->size);
669 frame->pkt_pts = AV_NOPTS_VALUE;
670 av_frame_set_pkt_pos (frame, -1);
671 av_frame_set_pkt_duration(frame, 0);
672 av_frame_set_pkt_size (frame, -1);
674 frame->reordered_opaque = avctx->reordered_opaque;
676 switch (avctx->codec->type) {
677 case AVMEDIA_TYPE_VIDEO:
678 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
679 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
680 if (frame->format < 0)
681 frame->format = avctx->pix_fmt;
682 if (!frame->sample_aspect_ratio.num)
683 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
684 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
685 av_frame_set_colorspace(frame, avctx->colorspace);
686 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
687 av_frame_set_color_range(frame, avctx->color_range);
689 case AVMEDIA_TYPE_AUDIO:
690 if (!frame->sample_rate)
691 frame->sample_rate = avctx->sample_rate;
692 if (frame->format < 0)
693 frame->format = avctx->sample_fmt;
694 if (!frame->channel_layout) {
695 if (avctx->channel_layout) {
696 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
698 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
700 return AVERROR(EINVAL);
703 frame->channel_layout = avctx->channel_layout;
705 if (avctx->channels > FF_SANE_NB_CHANNELS) {
706 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
708 return AVERROR(ENOSYS);
712 av_frame_set_channels(frame, avctx->channels);
718 #if FF_API_GET_BUFFER
719 FF_DISABLE_DEPRECATION_WARNINGS
720 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
722 return avcodec_default_get_buffer2(avctx, frame, 0);
725 typedef struct CompatReleaseBufPriv {
726 AVCodecContext avctx;
728 } CompatReleaseBufPriv;
730 static void compat_free_buffer(void *opaque, uint8_t *data)
732 CompatReleaseBufPriv *priv = opaque;
733 if (priv->avctx.release_buffer)
734 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
738 static void compat_release_buffer(void *opaque, uint8_t *data)
740 AVBufferRef *buf = opaque;
741 av_buffer_unref(&buf);
743 FF_ENABLE_DEPRECATION_WARNINGS
746 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
750 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
751 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
752 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
753 return AVERROR(EINVAL);
756 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
759 #if FF_API_GET_BUFFER
760 FF_DISABLE_DEPRECATION_WARNINGS
762 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
763 * We wrap each plane in its own AVBuffer. Each of those has a reference to
764 * a dummy AVBuffer as its private data, unreffing it on free.
765 * When all the planes are freed, the dummy buffer's free callback calls
768 if (avctx->get_buffer) {
769 CompatReleaseBufPriv *priv = NULL;
770 AVBufferRef *dummy_buf = NULL;
773 if (flags & AV_GET_BUFFER_FLAG_REF)
774 frame->reference = 1;
776 ret = avctx->get_buffer(avctx, frame);
780 /* return if the buffers are already set up
781 * this would happen e.g. when a custom get_buffer() calls
782 * avcodec_default_get_buffer
787 priv = av_mallocz(sizeof(*priv));
789 ret = AVERROR(ENOMEM);
792 priv->avctx = *avctx;
793 priv->frame = *frame;
795 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
797 ret = AVERROR(ENOMEM);
801 #define WRAP_PLANE(ref_out, data, data_size) \
803 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
805 ret = AVERROR(ENOMEM); \
808 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
811 av_frame_unref(frame); \
812 ret = AVERROR(ENOMEM); \
817 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
818 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
820 planes = av_pix_fmt_count_planes(frame->format);
821 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
822 check for allocated buffers: make libavcodec happy */
823 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
825 if (!desc || planes <= 0) {
826 ret = AVERROR(EINVAL);
830 for (i = 0; i < planes; i++) {
831 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
832 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
834 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
837 int planar = av_sample_fmt_is_planar(frame->format);
838 planes = planar ? avctx->channels : 1;
840 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
841 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
842 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
843 frame->nb_extended_buf);
844 if (!frame->extended_buf) {
845 ret = AVERROR(ENOMEM);
850 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
851 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
853 for (i = 0; i < frame->nb_extended_buf; i++)
854 WRAP_PLANE(frame->extended_buf[i],
855 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
859 av_buffer_unref(&dummy_buf);
862 frame->width = avctx->width;
863 frame->height = avctx->height;
868 avctx->release_buffer(avctx, frame);
870 av_buffer_unref(&dummy_buf);
873 FF_ENABLE_DEPRECATION_WARNINGS
876 ret = avctx->get_buffer2(avctx, frame, flags);
878 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
879 frame->width = avctx->width;
880 frame->height = avctx->height;
886 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
888 int ret = get_buffer_internal(avctx, frame, flags);
890 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
894 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
899 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
901 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
902 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
903 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
904 av_frame_unref(frame);
907 ff_init_buffer_info(avctx, frame);
910 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
912 if (av_frame_is_writable(frame))
915 av_frame_move_ref(&tmp, frame);
917 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
919 av_frame_unref(&tmp);
923 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
924 frame->format, frame->width, frame->height);
926 av_frame_unref(&tmp);
931 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
933 int ret = reget_buffer_internal(avctx, frame);
935 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
939 #if FF_API_GET_BUFFER
940 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
942 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
947 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
954 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
958 for (i = 0; i < count; i++) {
959 int r = func(c, (char *)arg + i * size);
966 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
970 for (i = 0; i < count; i++) {
971 int r = func(c, arg, i, 0);
978 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
980 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
981 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
984 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
986 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
991 void avcodec_get_frame_defaults(AVFrame *frame)
993 #if LIBAVCODEC_VERSION_MAJOR >= 55
994 // extended_data should explicitly be freed when needed, this code is unsafe currently
995 // also this is not compatible to the <55 ABI/API
996 if (frame->extended_data != frame->data && 0)
997 av_freep(&frame->extended_data);
1000 memset(frame, 0, sizeof(AVFrame));
1004 frame->pkt_pts = AV_NOPTS_VALUE;
1005 av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
1006 av_frame_set_pkt_duration (frame, 0);
1007 av_frame_set_pkt_pos (frame, -1);
1008 av_frame_set_pkt_size (frame, -1);
1009 frame->key_frame = 1;
1010 frame->sample_aspect_ratio = (AVRational) {0, 1 };
1011 frame->format = -1; /* unknown */
1012 frame->extended_data = frame->data;
1013 av_frame_set_colorspace(frame, AVCOL_SPC_UNSPECIFIED);
1016 AVFrame *avcodec_alloc_frame(void)
1018 AVFrame *frame = av_malloc(sizeof(AVFrame));
1023 frame->extended_data = NULL;
1024 avcodec_get_frame_defaults(frame);
1029 void avcodec_free_frame(AVFrame **frame)
1033 if (!frame || !*frame)
1038 if (f->extended_data != f->data)
1039 av_freep(&f->extended_data);
1044 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1045 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1046 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1047 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1049 int av_codec_get_max_lowres(const AVCodec *codec)
1051 return codec->max_lowres;
1054 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1056 memset(sub, 0, sizeof(*sub));
1057 sub->pts = AV_NOPTS_VALUE;
1060 static int get_bit_rate(AVCodecContext *ctx)
1063 int bits_per_sample;
1065 switch (ctx->codec_type) {
1066 case AVMEDIA_TYPE_VIDEO:
1067 case AVMEDIA_TYPE_DATA:
1068 case AVMEDIA_TYPE_SUBTITLE:
1069 case AVMEDIA_TYPE_ATTACHMENT:
1070 bit_rate = ctx->bit_rate;
1072 case AVMEDIA_TYPE_AUDIO:
1073 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1074 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1083 #if FF_API_AVCODEC_OPEN
1084 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
1086 return avcodec_open2(avctx, codec, NULL);
1090 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1094 ff_unlock_avcodec();
1096 ret = avcodec_open2(avctx, codec, options);
1098 ff_lock_avcodec(avctx);
1102 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1105 AVDictionary *tmp = NULL;
1107 if (avcodec_is_open(avctx))
1110 if ((!codec && !avctx->codec)) {
1111 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1112 return AVERROR(EINVAL);
1114 if ((codec && avctx->codec && codec != avctx->codec)) {
1115 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1116 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1117 return AVERROR(EINVAL);
1120 codec = avctx->codec;
1122 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1123 return AVERROR(EINVAL);
1126 av_dict_copy(&tmp, *options, 0);
1128 ret = ff_lock_avcodec(avctx);
1132 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1133 if (!avctx->internal) {
1134 ret = AVERROR(ENOMEM);
1138 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1139 if (!avctx->internal->pool) {
1140 ret = AVERROR(ENOMEM);
1144 if (codec->priv_data_size > 0) {
1145 if (!avctx->priv_data) {
1146 avctx->priv_data = av_mallocz(codec->priv_data_size);
1147 if (!avctx->priv_data) {
1148 ret = AVERROR(ENOMEM);
1151 if (codec->priv_class) {
1152 *(const AVClass **)avctx->priv_data = codec->priv_class;
1153 av_opt_set_defaults(avctx->priv_data);
1156 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1159 avctx->priv_data = NULL;
1161 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1164 // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1165 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1166 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1167 if (avctx->coded_width && avctx->coded_height)
1168 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1169 else if (avctx->width && avctx->height)
1170 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1173 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1174 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1175 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1176 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1177 avcodec_set_dimensions(avctx, 0, 0);
1180 /* if the decoder init function was already called previously,
1181 * free the already allocated subtitle_header before overwriting it */
1182 if (av_codec_is_decoder(codec))
1183 av_freep(&avctx->subtitle_header);
1185 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1186 ret = AVERROR(EINVAL);
1190 avctx->codec = codec;
1191 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1192 avctx->codec_id == AV_CODEC_ID_NONE) {
1193 avctx->codec_type = codec->type;
1194 avctx->codec_id = codec->id;
1196 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1197 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1198 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1199 ret = AVERROR(EINVAL);
1202 avctx->frame_number = 0;
1203 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1205 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1206 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1207 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1209 av_log(avctx, AV_LOG_ERROR,
1210 "The %s '%s' is experimental but experimental codecs are not enabled, "
1211 "add '-strict %d' if you want to use it.\n",
1212 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1213 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1214 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1215 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1216 codec_string, codec2->name);
1217 ret = AVERROR_EXPERIMENTAL;
1221 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1222 (!avctx->time_base.num || !avctx->time_base.den)) {
1223 avctx->time_base.num = 1;
1224 avctx->time_base.den = avctx->sample_rate;
1228 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1230 if (CONFIG_FRAME_THREAD_ENCODER) {
1231 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1232 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1233 ff_lock_avcodec(avctx);
1239 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1240 ret = ff_thread_init(avctx);
1245 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1246 avctx->thread_count = 1;
1248 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1249 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1250 avctx->codec->max_lowres);
1251 ret = AVERROR(EINVAL);
1255 if (av_codec_is_encoder(avctx->codec)) {
1257 if (avctx->codec->sample_fmts) {
1258 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1259 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1261 if (avctx->channels == 1 &&
1262 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1263 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1264 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1268 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1270 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1271 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1272 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1273 ret = AVERROR(EINVAL);
1277 if (avctx->codec->pix_fmts) {
1278 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1279 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1281 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1282 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1283 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1285 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1286 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1287 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1288 ret = AVERROR(EINVAL);
1292 if (avctx->codec->supported_samplerates) {
1293 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1294 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1296 if (avctx->codec->supported_samplerates[i] == 0) {
1297 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1298 avctx->sample_rate);
1299 ret = AVERROR(EINVAL);
1303 if (avctx->codec->channel_layouts) {
1304 if (!avctx->channel_layout) {
1305 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1307 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1308 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1310 if (avctx->codec->channel_layouts[i] == 0) {
1312 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1313 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1314 ret = AVERROR(EINVAL);
1319 if (avctx->channel_layout && avctx->channels) {
1320 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1321 if (channels != avctx->channels) {
1323 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1324 av_log(avctx, AV_LOG_ERROR,
1325 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1326 buf, channels, avctx->channels);
1327 ret = AVERROR(EINVAL);
1330 } else if (avctx->channel_layout) {
1331 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1333 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1334 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1336 if (avctx->width <= 0 || avctx->height <= 0) {
1337 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1338 ret = AVERROR(EINVAL);
1342 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1343 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1344 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1347 if (!avctx->rc_initial_buffer_occupancy)
1348 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1351 avctx->pts_correction_num_faulty_pts =
1352 avctx->pts_correction_num_faulty_dts = 0;
1353 avctx->pts_correction_last_pts =
1354 avctx->pts_correction_last_dts = INT64_MIN;
1356 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1357 || avctx->internal->frame_thread_encoder)) {
1358 ret = avctx->codec->init(avctx);
1366 if (av_codec_is_decoder(avctx->codec)) {
1367 if (!avctx->bit_rate)
1368 avctx->bit_rate = get_bit_rate(avctx);
1369 /* validate channel layout from the decoder */
1370 if (avctx->channel_layout) {
1371 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1372 if (!avctx->channels)
1373 avctx->channels = channels;
1374 else if (channels != avctx->channels) {
1376 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1377 av_log(avctx, AV_LOG_WARNING,
1378 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1379 "ignoring specified channel layout\n",
1380 buf, channels, avctx->channels);
1381 avctx->channel_layout = 0;
1384 if (avctx->channels && avctx->channels < 0 ||
1385 avctx->channels > FF_SANE_NB_CHANNELS) {
1386 ret = AVERROR(EINVAL);
1389 if (avctx->sub_charenc) {
1390 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1391 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1392 "supported with subtitles codecs\n");
1393 ret = AVERROR(EINVAL);
1395 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1396 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1397 "subtitles character encoding will be ignored\n",
1398 avctx->codec_descriptor->name);
1399 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1401 /* input character encoding is set for a text based subtitle
1402 * codec at this point */
1403 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1404 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1406 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1408 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1409 if (cd == (iconv_t)-1) {
1410 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1411 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1412 ret = AVERROR(errno);
1417 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1418 "conversion needs a libavcodec built with iconv support "
1419 "for this codec\n");
1420 ret = AVERROR(ENOSYS);
1428 ff_unlock_avcodec();
1430 av_dict_free(options);
1437 av_freep(&avctx->priv_data);
1438 if (avctx->internal)
1439 av_freep(&avctx->internal->pool);
1440 av_freep(&avctx->internal);
1441 avctx->codec = NULL;
1445 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1447 if (avpkt->size < 0) {
1448 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1449 return AVERROR(EINVAL);
1451 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1452 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1453 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1454 return AVERROR(EINVAL);
1458 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1459 if (!avpkt->data || avpkt->size < size) {
1460 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1461 avpkt->data = avctx->internal->byte_buffer;
1462 avpkt->size = avctx->internal->byte_buffer_size;
1463 avpkt->destruct = NULL;
1468 AVBufferRef *buf = avpkt->buf;
1469 #if FF_API_DESTRUCT_PACKET
1470 FF_DISABLE_DEPRECATION_WARNINGS
1471 void *destruct = avpkt->destruct;
1472 FF_ENABLE_DEPRECATION_WARNINGS
1475 if (avpkt->size < size) {
1476 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1477 return AVERROR(EINVAL);
1480 av_init_packet(avpkt);
1481 #if FF_API_DESTRUCT_PACKET
1482 FF_DISABLE_DEPRECATION_WARNINGS
1483 avpkt->destruct = destruct;
1484 FF_ENABLE_DEPRECATION_WARNINGS
1490 int ret = av_new_packet(avpkt, size);
1492 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1497 int ff_alloc_packet(AVPacket *avpkt, int size)
1499 return ff_alloc_packet2(NULL, avpkt, size);
1503 * Pad last frame with silence.
1505 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1507 AVFrame *frame = NULL;
1510 if (!(frame = avcodec_alloc_frame()))
1511 return AVERROR(ENOMEM);
1513 frame->format = src->format;
1514 frame->channel_layout = src->channel_layout;
1515 av_frame_set_channels(frame, av_frame_get_channels(src));
1516 frame->nb_samples = s->frame_size;
1517 ret = av_frame_get_buffer(frame, 32);
1521 ret = av_frame_copy_props(frame, src);
1525 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1526 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1528 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1529 frame->nb_samples - src->nb_samples,
1530 s->channels, s->sample_fmt)) < 0)
1538 av_frame_free(&frame);
1542 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1544 const AVFrame *frame,
1545 int *got_packet_ptr)
1548 AVFrame *padded_frame = NULL;
1550 AVPacket user_pkt = *avpkt;
1551 int needs_realloc = !user_pkt.data;
1553 *got_packet_ptr = 0;
1555 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1556 av_free_packet(avpkt);
1557 av_init_packet(avpkt);
1561 /* ensure that extended_data is properly set */
1562 if (frame && !frame->extended_data) {
1563 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1564 avctx->channels > AV_NUM_DATA_POINTERS) {
1565 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1566 "with more than %d channels, but extended_data is not set.\n",
1567 AV_NUM_DATA_POINTERS);
1568 return AVERROR(EINVAL);
1570 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1573 tmp.extended_data = tmp.data;
1577 /* check for valid frame size */
1579 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1580 if (frame->nb_samples > avctx->frame_size) {
1581 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1582 return AVERROR(EINVAL);
1584 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1585 if (frame->nb_samples < avctx->frame_size &&
1586 !avctx->internal->last_audio_frame) {
1587 ret = pad_last_frame(avctx, &padded_frame, frame);
1591 frame = padded_frame;
1592 avctx->internal->last_audio_frame = 1;
1595 if (frame->nb_samples != avctx->frame_size) {
1596 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1597 ret = AVERROR(EINVAL);
1603 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1605 if (*got_packet_ptr) {
1606 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1607 if (avpkt->pts == AV_NOPTS_VALUE)
1608 avpkt->pts = frame->pts;
1609 if (!avpkt->duration)
1610 avpkt->duration = ff_samples_to_time_base(avctx,
1613 avpkt->dts = avpkt->pts;
1618 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1620 if (user_pkt.data) {
1621 if (user_pkt.size >= avpkt->size) {
1622 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1624 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1625 avpkt->size = user_pkt.size;
1628 avpkt->buf = user_pkt.buf;
1629 avpkt->data = user_pkt.data;
1630 avpkt->destruct = user_pkt.destruct;
1632 if (av_dup_packet(avpkt) < 0) {
1633 ret = AVERROR(ENOMEM);
1639 if (needs_realloc && avpkt->data) {
1640 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1642 avpkt->data = avpkt->buf->data;
1645 avctx->frame_number++;
1648 if (ret < 0 || !*got_packet_ptr) {
1649 av_free_packet(avpkt);
1650 av_init_packet(avpkt);
1654 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1655 * this needs to be moved to the encoders, but for now we can do it
1656 * here to simplify things */
1657 avpkt->flags |= AV_PKT_FLAG_KEY;
1660 av_frame_free(&padded_frame);
1665 #if FF_API_OLD_ENCODE_AUDIO
1666 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1667 uint8_t *buf, int buf_size,
1668 const short *samples)
1671 AVFrame frame0 = { { 0 } };
1673 int ret, samples_size, got_packet;
1675 av_init_packet(&pkt);
1677 pkt.size = buf_size;
1681 avcodec_get_frame_defaults(frame);
1683 if (avctx->frame_size) {
1684 frame->nb_samples = avctx->frame_size;
1686 /* if frame_size is not set, the number of samples must be
1687 * calculated from the buffer size */
1689 if (!av_get_bits_per_sample(avctx->codec_id)) {
1690 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1691 "support this codec\n");
1692 return AVERROR(EINVAL);
1694 nb_samples = (int64_t)buf_size * 8 /
1695 (av_get_bits_per_sample(avctx->codec_id) *
1697 if (nb_samples >= INT_MAX)
1698 return AVERROR(EINVAL);
1699 frame->nb_samples = nb_samples;
1702 /* it is assumed that the samples buffer is large enough based on the
1703 * relevant parameters */
1704 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1706 avctx->sample_fmt, 1);
1707 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1709 (const uint8_t *)samples,
1710 samples_size, 1)) < 0)
1713 /* fabricate frame pts from sample count.
1714 * this is needed because the avcodec_encode_audio() API does not have
1715 * a way for the user to provide pts */
1716 if (avctx->sample_rate && avctx->time_base.num)
1717 frame->pts = ff_samples_to_time_base(avctx,
1718 avctx->internal->sample_count);
1720 frame->pts = AV_NOPTS_VALUE;
1721 avctx->internal->sample_count += frame->nb_samples;
1727 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1728 if (!ret && got_packet && avctx->coded_frame) {
1729 avctx->coded_frame->pts = pkt.pts;
1730 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1732 /* free any side data since we cannot return it */
1733 av_packet_free_side_data(&pkt);
1735 if (frame && frame->extended_data != frame->data)
1736 av_freep(&frame->extended_data);
1738 return ret ? ret : pkt.size;
1743 #if FF_API_OLD_ENCODE_VIDEO
1744 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1745 const AVFrame *pict)
1748 int ret, got_packet = 0;
1750 if (buf_size < FF_MIN_BUFFER_SIZE) {
1751 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1755 av_init_packet(&pkt);
1757 pkt.size = buf_size;
1759 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1760 if (!ret && got_packet && avctx->coded_frame) {
1761 avctx->coded_frame->pts = pkt.pts;
1762 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1765 /* free any side data since we cannot return it */
1766 if (pkt.side_data_elems > 0) {
1768 for (i = 0; i < pkt.side_data_elems; i++)
1769 av_free(pkt.side_data[i].data);
1770 av_freep(&pkt.side_data);
1771 pkt.side_data_elems = 0;
1774 return ret ? ret : pkt.size;
1779 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1781 const AVFrame *frame,
1782 int *got_packet_ptr)
1785 AVPacket user_pkt = *avpkt;
1786 int needs_realloc = !user_pkt.data;
1788 *got_packet_ptr = 0;
1790 if(CONFIG_FRAME_THREAD_ENCODER &&
1791 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1792 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1794 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1795 avctx->stats_out[0] = '\0';
1797 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1798 av_free_packet(avpkt);
1799 av_init_packet(avpkt);
1804 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1805 return AVERROR(EINVAL);
1807 av_assert0(avctx->codec->encode2);
1809 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1810 av_assert0(ret <= 0);
1812 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1814 if (user_pkt.data) {
1815 if (user_pkt.size >= avpkt->size) {
1816 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1818 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1819 avpkt->size = user_pkt.size;
1822 avpkt->buf = user_pkt.buf;
1823 avpkt->data = user_pkt.data;
1824 avpkt->destruct = user_pkt.destruct;
1826 if (av_dup_packet(avpkt) < 0) {
1827 ret = AVERROR(ENOMEM);
1833 if (!*got_packet_ptr)
1835 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1836 avpkt->pts = avpkt->dts = frame->pts;
1838 if (needs_realloc && avpkt->data) {
1839 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1841 avpkt->data = avpkt->buf->data;
1844 avctx->frame_number++;
1847 if (ret < 0 || !*got_packet_ptr)
1848 av_free_packet(avpkt);
1850 av_packet_merge_side_data(avpkt);
1856 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1857 const AVSubtitle *sub)
1860 if (sub->start_display_time) {
1861 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1865 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1866 avctx->frame_number++;
1871 * Attempt to guess proper monotonic timestamps for decoded video frames
1872 * which might have incorrect times. Input timestamps may wrap around, in
1873 * which case the output will as well.
1875 * @param pts the pts field of the decoded AVPacket, as passed through
1877 * @param dts the dts field of the decoded AVPacket
1878 * @return one of the input values, may be AV_NOPTS_VALUE
1880 static int64_t guess_correct_pts(AVCodecContext *ctx,
1881 int64_t reordered_pts, int64_t dts)
1883 int64_t pts = AV_NOPTS_VALUE;
1885 if (dts != AV_NOPTS_VALUE) {
1886 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1887 ctx->pts_correction_last_dts = dts;
1889 if (reordered_pts != AV_NOPTS_VALUE) {
1890 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1891 ctx->pts_correction_last_pts = reordered_pts;
1893 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1894 && reordered_pts != AV_NOPTS_VALUE)
1895 pts = reordered_pts;
1902 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1905 const uint8_t *data;
1908 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1911 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1912 if (!data || size < 4)
1914 flags = bytestream_get_le32(&data);
1916 if (size < 4) /* Required for any of the changes */
1918 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1919 avctx->channels = bytestream_get_le32(&data);
1922 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1925 avctx->channel_layout = bytestream_get_le64(&data);
1930 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1931 avctx->sample_rate = bytestream_get_le32(&data);
1934 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1937 avctx->width = bytestream_get_le32(&data);
1938 avctx->height = bytestream_get_le32(&data);
1939 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1944 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1947 const uint8_t *side_metadata;
1950 side_metadata = av_packet_get_side_data(avctx->pkt,
1951 AV_PKT_DATA_STRINGS_METADATA, &size);
1954 end = side_metadata + size;
1955 if (size && end[-1])
1956 return AVERROR_INVALIDDATA;
1957 while (side_metadata < end) {
1958 const uint8_t *key = side_metadata;
1959 const uint8_t *val = side_metadata + strlen(key) + 1;
1963 return AVERROR_INVALIDDATA;
1965 ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
1968 side_metadata = val + strlen(val) + 1;
1974 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1975 int *got_picture_ptr,
1976 const AVPacket *avpkt)
1978 AVCodecInternal *avci = avctx->internal;
1980 // copy to ensure we do not change avpkt
1981 AVPacket tmp = *avpkt;
1984 return AVERROR(EINVAL);
1985 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1986 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1987 return AVERROR(EINVAL);
1990 *got_picture_ptr = 0;
1991 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1992 return AVERROR(EINVAL);
1994 avcodec_get_frame_defaults(picture);
1996 if (!avctx->refcounted_frames)
1997 av_frame_unref(&avci->to_free);
1999 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2000 int did_split = av_packet_split_side_data(&tmp);
2001 apply_param_change(avctx, &tmp);
2003 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2004 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2007 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2009 picture->pkt_dts = avpkt->dts;
2011 if(!avctx->has_b_frames){
2012 av_frame_set_pkt_pos(picture, avpkt->pos);
2014 //FIXME these should be under if(!avctx->has_b_frames)
2015 /* get_buffer is supposed to set frame parameters */
2016 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2017 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2018 if (!picture->width) picture->width = avctx->width;
2019 if (!picture->height) picture->height = avctx->height;
2020 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2023 add_metadata_from_side_data(avctx, picture);
2025 emms_c(); //needed to avoid an emms_c() call before every return;
2029 av_packet_free_side_data(&tmp);
2034 if (ret < 0 && picture->data[0])
2035 av_frame_unref(picture);
2037 if (*got_picture_ptr) {
2038 if (!avctx->refcounted_frames) {
2039 avci->to_free = *picture;
2040 avci->to_free.extended_data = avci->to_free.data;
2041 memset(picture->buf, 0, sizeof(picture->buf));
2044 avctx->frame_number++;
2045 av_frame_set_best_effort_timestamp(picture,
2046 guess_correct_pts(avctx,
2053 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2054 * make sure it's set correctly */
2055 picture->extended_data = picture->data;
2060 #if FF_API_OLD_DECODE_AUDIO
2061 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2062 int *frame_size_ptr,
2065 AVFrame frame = { { 0 } };
2066 int ret, got_frame = 0;
2068 if (avctx->get_buffer != avcodec_default_get_buffer) {
2069 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2070 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2071 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2072 "avcodec_decode_audio4()\n");
2073 avctx->get_buffer = avcodec_default_get_buffer;
2074 avctx->release_buffer = avcodec_default_release_buffer;
2077 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
2079 if (ret >= 0 && got_frame) {
2081 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2082 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2084 avctx->sample_fmt, 1);
2085 if (*frame_size_ptr < data_size) {
2086 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2087 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2088 return AVERROR(EINVAL);
2091 memcpy(samples, frame.extended_data[0], plane_size);
2093 if (planar && avctx->channels > 1) {
2094 uint8_t *out = ((uint8_t *)samples) + plane_size;
2095 for (ch = 1; ch < avctx->channels; ch++) {
2096 memcpy(out, frame.extended_data[ch], plane_size);
2100 *frame_size_ptr = data_size;
2102 *frame_size_ptr = 0;
2109 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2112 const AVPacket *avpkt)
2114 AVCodecInternal *avci = avctx->internal;
2115 int planar, channels;
2120 if (!avpkt->data && avpkt->size) {
2121 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2122 return AVERROR(EINVAL);
2125 return AVERROR(EINVAL);
2126 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2127 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2128 return AVERROR(EINVAL);
2131 avcodec_get_frame_defaults(frame);
2133 if (!avctx->refcounted_frames)
2134 av_frame_unref(&avci->to_free);
2136 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2139 uint32_t discard_padding = 0;
2140 // copy to ensure we do not change avpkt
2141 AVPacket tmp = *avpkt;
2142 int did_split = av_packet_split_side_data(&tmp);
2143 apply_param_change(avctx, &tmp);
2146 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2147 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2149 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2150 frame->pkt_dts = avpkt->dts;
2152 if (ret >= 0 && *got_frame_ptr) {
2153 add_metadata_from_side_data(avctx, frame);
2154 avctx->frame_number++;
2155 av_frame_set_best_effort_timestamp(frame,
2156 guess_correct_pts(avctx,
2159 if (frame->format == AV_SAMPLE_FMT_NONE)
2160 frame->format = avctx->sample_fmt;
2161 if (!frame->channel_layout)
2162 frame->channel_layout = avctx->channel_layout;
2163 if (!av_frame_get_channels(frame))
2164 av_frame_set_channels(frame, avctx->channels);
2165 if (!frame->sample_rate)
2166 frame->sample_rate = avctx->sample_rate;
2169 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2170 if(side && side_size>=10) {
2171 avctx->internal->skip_samples = AV_RL32(side);
2172 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2173 avctx->internal->skip_samples);
2174 discard_padding = AV_RL32(side + 4);
2176 if (avctx->internal->skip_samples && *got_frame_ptr) {
2177 if(frame->nb_samples <= avctx->internal->skip_samples){
2179 avctx->internal->skip_samples -= frame->nb_samples;
2180 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2181 avctx->internal->skip_samples);
2183 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2184 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2185 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2186 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2187 (AVRational){1, avctx->sample_rate},
2188 avctx->pkt_timebase);
2189 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2190 frame->pkt_pts += diff_ts;
2191 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2192 frame->pkt_dts += diff_ts;
2193 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2194 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2196 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2198 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2199 avctx->internal->skip_samples, frame->nb_samples);
2200 frame->nb_samples -= avctx->internal->skip_samples;
2201 avctx->internal->skip_samples = 0;
2205 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
2206 if (discard_padding == frame->nb_samples) {
2209 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2210 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2211 (AVRational){1, avctx->sample_rate},
2212 avctx->pkt_timebase);
2213 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2214 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2216 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2218 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2219 discard_padding, frame->nb_samples);
2220 frame->nb_samples -= discard_padding;
2226 av_packet_free_side_data(&tmp);
2231 if (ret >= 0 && *got_frame_ptr) {
2232 if (!avctx->refcounted_frames) {
2233 avci->to_free = *frame;
2234 avci->to_free.extended_data = avci->to_free.data;
2235 memset(frame->buf, 0, sizeof(frame->buf));
2236 frame->extended_buf = NULL;
2237 frame->nb_extended_buf = 0;
2239 } else if (frame->data[0])
2240 av_frame_unref(frame);
2243 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2244 * make sure it's set correctly; assume decoders that actually use
2245 * extended_data are doing it correctly */
2246 if (*got_frame_ptr) {
2247 planar = av_sample_fmt_is_planar(frame->format);
2248 channels = av_frame_get_channels(frame);
2249 if (!(planar && channels > AV_NUM_DATA_POINTERS))
2250 frame->extended_data = frame->data;
2252 frame->extended_data = NULL;
2258 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2259 static int recode_subtitle(AVCodecContext *avctx,
2260 AVPacket *outpkt, const AVPacket *inpkt)
2263 iconv_t cd = (iconv_t)-1;
2270 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
2274 cd = iconv_open("UTF-8", avctx->sub_charenc);
2275 av_assert0(cd != (iconv_t)-1);
2280 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2281 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2282 ret = AVERROR(ENOMEM);
2286 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2289 outpkt->buf = tmp.buf;
2290 outpkt->data = tmp.data;
2291 outpkt->size = tmp.size;
2292 outb = outpkt->data;
2293 outl = outpkt->size;
2295 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2296 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2297 outl >= outpkt->size || inl != 0) {
2298 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2299 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2300 av_free_packet(&tmp);
2301 ret = AVERROR(errno);
2304 outpkt->size -= outl;
2305 memset(outpkt->data + outpkt->size, 0, outl);
2308 if (cd != (iconv_t)-1)
2312 av_assert0(!"requesting subtitles recoding without iconv");
2316 static int utf8_check(const uint8_t *str)
2318 const uint8_t *byte;
2319 uint32_t codepoint, min;
2323 GET_UTF8(codepoint, *(byte++), return 0;);
2324 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2325 1 << (5 * (byte - str) - 4);
2326 if (codepoint < min || codepoint >= 0x110000 ||
2327 codepoint == 0xFFFE /* BOM */ ||
2328 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2335 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2341 if (!avpkt->data && avpkt->size) {
2342 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2343 return AVERROR(EINVAL);
2346 return AVERROR(EINVAL);
2347 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2348 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2349 return AVERROR(EINVAL);
2353 avcodec_get_subtitle_defaults(sub);
2356 AVPacket pkt_recoded;
2357 AVPacket tmp = *avpkt;
2358 int did_split = av_packet_split_side_data(&tmp);
2359 //apply_param_change(avctx, &tmp);
2362 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2366 avctx->pkt = &pkt_recoded;
2368 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2369 sub->pts = av_rescale_q(avpkt->pts,
2370 avctx->pkt_timebase, AV_TIME_BASE_Q);
2371 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2372 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2373 !!*got_sub_ptr >= !!sub->num_rects);
2375 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2376 avctx->pkt_timebase.num) {
2377 AVRational ms = { 1, 1000 };
2378 sub->end_display_time = av_rescale_q(avpkt->duration,
2379 avctx->pkt_timebase, ms);
2382 for (i = 0; i < sub->num_rects; i++) {
2383 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2384 av_log(avctx, AV_LOG_ERROR,
2385 "Invalid UTF-8 in decoded subtitles text; "
2386 "maybe missing -sub_charenc option\n");
2387 avsubtitle_free(sub);
2388 return AVERROR_INVALIDDATA;
2392 if (tmp.data != pkt_recoded.data) { // did we recode?
2393 /* prevent from destroying side data from original packet */
2394 pkt_recoded.side_data = NULL;
2395 pkt_recoded.side_data_elems = 0;
2397 av_free_packet(&pkt_recoded);
2399 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2401 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2407 av_packet_free_side_data(&tmp);
2413 avctx->frame_number++;
2419 void avsubtitle_free(AVSubtitle *sub)
2423 for (i = 0; i < sub->num_rects; i++) {
2424 av_freep(&sub->rects[i]->pict.data[0]);
2425 av_freep(&sub->rects[i]->pict.data[1]);
2426 av_freep(&sub->rects[i]->pict.data[2]);
2427 av_freep(&sub->rects[i]->pict.data[3]);
2428 av_freep(&sub->rects[i]->text);
2429 av_freep(&sub->rects[i]->ass);
2430 av_freep(&sub->rects[i]);
2433 av_freep(&sub->rects);
2435 memset(sub, 0, sizeof(AVSubtitle));
2438 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
2442 ff_unlock_avcodec();
2444 ret = avcodec_close(avctx);
2446 ff_lock_avcodec(NULL);
2450 av_cold int avcodec_close(AVCodecContext *avctx)
2457 ret = ff_lock_avcodec(avctx);
2461 if (avcodec_is_open(avctx)) {
2462 FramePool *pool = avctx->internal->pool;
2464 if (CONFIG_FRAME_THREAD_ENCODER &&
2465 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2466 ff_unlock_avcodec();
2467 ff_frame_thread_encoder_free(avctx);
2468 ff_lock_avcodec(avctx);
2470 if (HAVE_THREADS && avctx->thread_opaque)
2471 ff_thread_free(avctx);
2472 if (avctx->codec && avctx->codec->close)
2473 avctx->codec->close(avctx);
2474 avctx->coded_frame = NULL;
2475 avctx->internal->byte_buffer_size = 0;
2476 av_freep(&avctx->internal->byte_buffer);
2477 if (!avctx->refcounted_frames)
2478 av_frame_unref(&avctx->internal->to_free);
2479 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2480 av_buffer_pool_uninit(&pool->pools[i]);
2481 av_freep(&avctx->internal->pool);
2482 av_freep(&avctx->internal);
2485 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2486 av_opt_free(avctx->priv_data);
2488 av_freep(&avctx->priv_data);
2489 if (av_codec_is_encoder(avctx->codec))
2490 av_freep(&avctx->extradata);
2491 avctx->codec = NULL;
2492 avctx->active_thread_type = 0;
2494 ff_unlock_avcodec();
2498 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2501 //This is for future deprecatec codec ids, its empty since
2502 //last major bump but will fill up again over time, please don't remove it
2503 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2504 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
2505 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2506 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2507 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2508 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2509 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2510 case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
2511 default : return id;
2515 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2517 AVCodec *p, *experimental = NULL;
2519 id= remap_deprecated_codec_id(id);
2521 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2523 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2530 return experimental;
2533 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2535 return find_encdec(id, 1);
2538 AVCodec *avcodec_find_encoder_by_name(const char *name)
2545 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2552 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2554 return find_encdec(id, 0);
2557 AVCodec *avcodec_find_decoder_by_name(const char *name)
2564 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2571 const char *avcodec_get_name(enum AVCodecID id)
2573 const AVCodecDescriptor *cd;
2576 if (id == AV_CODEC_ID_NONE)
2578 cd = avcodec_descriptor_get(id);
2581 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2582 codec = avcodec_find_decoder(id);
2585 codec = avcodec_find_encoder(id);
2588 return "unknown_codec";
2591 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2593 int i, len, ret = 0;
2595 #define TAG_PRINT(x) \
2596 (((x) >= '0' && (x) <= '9') || \
2597 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2598 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2600 for (i = 0; i < 4; i++) {
2601 len = snprintf(buf, buf_size,
2602 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2604 buf_size = buf_size > len ? buf_size - len : 0;
2611 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2613 const char *codec_type;
2614 const char *codec_name;
2615 const char *profile = NULL;
2618 AVRational display_aspect_ratio;
2620 if (!buf || buf_size <= 0)
2622 codec_type = av_get_media_type_string(enc->codec_type);
2623 codec_name = avcodec_get_name(enc->codec_id);
2624 if (enc->profile != FF_PROFILE_UNKNOWN) {
2628 p = encode ? avcodec_find_encoder(enc->codec_id) :
2629 avcodec_find_decoder(enc->codec_id);
2631 profile = av_get_profile_name(p, enc->profile);
2634 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2636 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2638 if (enc->codec && strcmp(enc->codec->name, codec_name))
2639 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2642 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2643 if (enc->codec_tag) {
2645 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2646 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2647 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2650 switch (enc->codec_type) {
2651 case AVMEDIA_TYPE_VIDEO:
2652 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2653 char detail[256] = "(";
2654 const char *colorspace_name;
2655 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2657 av_get_pix_fmt_name(enc->pix_fmt));
2658 if (enc->bits_per_raw_sample &&
2659 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2660 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2661 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2662 av_strlcatf(detail, sizeof(detail),
2663 enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
2665 colorspace_name = av_get_colorspace_name(enc->colorspace);
2666 if (colorspace_name)
2667 av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
2669 if (strlen(detail) > 1) {
2670 detail[strlen(detail) - 2] = 0;
2671 av_strlcatf(buf, buf_size, "%s)", detail);
2675 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2677 enc->width, enc->height);
2678 if (enc->sample_aspect_ratio.num) {
2679 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2680 enc->width * enc->sample_aspect_ratio.num,
2681 enc->height * enc->sample_aspect_ratio.den,
2683 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2684 " [SAR %d:%d DAR %d:%d]",
2685 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2686 display_aspect_ratio.num, display_aspect_ratio.den);
2688 if (av_log_get_level() >= AV_LOG_DEBUG) {
2689 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2690 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2692 enc->time_base.num / g, enc->time_base.den / g);
2696 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2697 ", q=%d-%d", enc->qmin, enc->qmax);
2700 case AVMEDIA_TYPE_AUDIO:
2701 if (enc->sample_rate) {
2702 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2703 ", %d Hz", enc->sample_rate);
2705 av_strlcat(buf, ", ", buf_size);
2706 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2707 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2708 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2709 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2712 case AVMEDIA_TYPE_DATA:
2713 if (av_log_get_level() >= AV_LOG_DEBUG) {
2714 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2716 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2718 enc->time_base.num / g, enc->time_base.den / g);
2721 case AVMEDIA_TYPE_SUBTITLE:
2723 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2724 ", %dx%d", enc->width, enc->height);
2730 if (enc->flags & CODEC_FLAG_PASS1)
2731 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2733 if (enc->flags & CODEC_FLAG_PASS2)
2734 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2737 bitrate = get_bit_rate(enc);
2739 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2740 ", %d kb/s", bitrate / 1000);
2741 } else if (enc->rc_max_rate > 0) {
2742 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2743 ", max. %d kb/s", enc->rc_max_rate / 1000);
2747 const char *av_get_profile_name(const AVCodec *codec, int profile)
2750 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2753 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2754 if (p->profile == profile)
2760 unsigned avcodec_version(void)
2762 // av_assert0(AV_CODEC_ID_V410==164);
2763 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2764 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2765 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2766 av_assert0(AV_CODEC_ID_SRT==94216);
2767 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2769 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
2770 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
2771 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
2772 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
2773 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
2774 return LIBAVCODEC_VERSION_INT;
2777 const char *avcodec_configuration(void)
2779 return FFMPEG_CONFIGURATION;
2782 const char *avcodec_license(void)
2784 #define LICENSE_PREFIX "libavcodec license: "
2785 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2788 void avcodec_flush_buffers(AVCodecContext *avctx)
2790 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2791 ff_thread_flush(avctx);
2792 else if (avctx->codec->flush)
2793 avctx->codec->flush(avctx);
2795 avctx->pts_correction_last_pts =
2796 avctx->pts_correction_last_dts = INT64_MIN;
2798 if (!avctx->refcounted_frames)
2799 av_frame_unref(&avctx->internal->to_free);
2802 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2805 case AV_CODEC_ID_8SVX_EXP:
2806 case AV_CODEC_ID_8SVX_FIB:
2807 case AV_CODEC_ID_ADPCM_CT:
2808 case AV_CODEC_ID_ADPCM_IMA_APC:
2809 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2810 case AV_CODEC_ID_ADPCM_IMA_OKI:
2811 case AV_CODEC_ID_ADPCM_IMA_WS:
2812 case AV_CODEC_ID_ADPCM_G722:
2813 case AV_CODEC_ID_ADPCM_YAMAHA:
2815 case AV_CODEC_ID_PCM_ALAW:
2816 case AV_CODEC_ID_PCM_MULAW:
2817 case AV_CODEC_ID_PCM_S8:
2818 case AV_CODEC_ID_PCM_S8_PLANAR:
2819 case AV_CODEC_ID_PCM_U8:
2820 case AV_CODEC_ID_PCM_ZORK:
2822 case AV_CODEC_ID_PCM_S16BE:
2823 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2824 case AV_CODEC_ID_PCM_S16LE:
2825 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2826 case AV_CODEC_ID_PCM_U16BE:
2827 case AV_CODEC_ID_PCM_U16LE:
2829 case AV_CODEC_ID_PCM_S24DAUD:
2830 case AV_CODEC_ID_PCM_S24BE:
2831 case AV_CODEC_ID_PCM_S24LE:
2832 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2833 case AV_CODEC_ID_PCM_U24BE:
2834 case AV_CODEC_ID_PCM_U24LE:
2836 case AV_CODEC_ID_PCM_S32BE:
2837 case AV_CODEC_ID_PCM_S32LE:
2838 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2839 case AV_CODEC_ID_PCM_U32BE:
2840 case AV_CODEC_ID_PCM_U32LE:
2841 case AV_CODEC_ID_PCM_F32BE:
2842 case AV_CODEC_ID_PCM_F32LE:
2844 case AV_CODEC_ID_PCM_F64BE:
2845 case AV_CODEC_ID_PCM_F64LE:
2852 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2854 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2855 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2856 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2857 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2858 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2859 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2860 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2861 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2862 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2863 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2864 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2866 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2867 return AV_CODEC_ID_NONE;
2868 if (be < 0 || be > 1)
2870 return map[fmt][be];
2873 int av_get_bits_per_sample(enum AVCodecID codec_id)
2876 case AV_CODEC_ID_ADPCM_SBPRO_2:
2878 case AV_CODEC_ID_ADPCM_SBPRO_3:
2880 case AV_CODEC_ID_ADPCM_SBPRO_4:
2881 case AV_CODEC_ID_ADPCM_IMA_WAV:
2882 case AV_CODEC_ID_ADPCM_IMA_QT:
2883 case AV_CODEC_ID_ADPCM_SWF:
2884 case AV_CODEC_ID_ADPCM_MS:
2887 return av_get_exact_bits_per_sample(codec_id);
2891 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2893 int id, sr, ch, ba, tag, bps;
2895 id = avctx->codec_id;
2896 sr = avctx->sample_rate;
2897 ch = avctx->channels;
2898 ba = avctx->block_align;
2899 tag = avctx->codec_tag;
2900 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2902 /* codecs with an exact constant bits per sample */
2903 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2904 return (frame_bytes * 8LL) / (bps * ch);
2905 bps = avctx->bits_per_coded_sample;
2907 /* codecs with a fixed packet duration */
2909 case AV_CODEC_ID_ADPCM_ADX: return 32;
2910 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2911 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2912 case AV_CODEC_ID_AMR_NB:
2913 case AV_CODEC_ID_EVRC:
2914 case AV_CODEC_ID_GSM:
2915 case AV_CODEC_ID_QCELP:
2916 case AV_CODEC_ID_RA_288: return 160;
2917 case AV_CODEC_ID_AMR_WB:
2918 case AV_CODEC_ID_GSM_MS: return 320;
2919 case AV_CODEC_ID_MP1: return 384;
2920 case AV_CODEC_ID_ATRAC1: return 512;
2921 case AV_CODEC_ID_ATRAC3: return 1024;
2922 case AV_CODEC_ID_MP2:
2923 case AV_CODEC_ID_MUSEPACK7: return 1152;
2924 case AV_CODEC_ID_AC3: return 1536;
2928 /* calc from sample rate */
2929 if (id == AV_CODEC_ID_TTA)
2930 return 256 * sr / 245;
2933 /* calc from sample rate and channels */
2934 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2935 return (480 << (sr / 22050)) / ch;
2940 /* calc from block_align */
2941 if (id == AV_CODEC_ID_SIPR) {
2943 case 20: return 160;
2944 case 19: return 144;
2945 case 29: return 288;
2946 case 37: return 480;
2948 } else if (id == AV_CODEC_ID_ILBC) {
2950 case 38: return 160;
2951 case 50: return 240;
2956 if (frame_bytes > 0) {
2957 /* calc from frame_bytes only */
2958 if (id == AV_CODEC_ID_TRUESPEECH)
2959 return 240 * (frame_bytes / 32);
2960 if (id == AV_CODEC_ID_NELLYMOSER)
2961 return 256 * (frame_bytes / 64);
2962 if (id == AV_CODEC_ID_RA_144)
2963 return 160 * (frame_bytes / 20);
2964 if (id == AV_CODEC_ID_G723_1)
2965 return 240 * (frame_bytes / 24);
2968 /* calc from frame_bytes and bits_per_coded_sample */
2969 if (id == AV_CODEC_ID_ADPCM_G726)
2970 return frame_bytes * 8 / bps;
2974 /* calc from frame_bytes and channels */
2976 case AV_CODEC_ID_ADPCM_AFC:
2977 return frame_bytes / (9 * ch) * 16;
2978 case AV_CODEC_ID_ADPCM_DTK:
2979 return frame_bytes / (16 * ch) * 28;
2980 case AV_CODEC_ID_ADPCM_4XM:
2981 case AV_CODEC_ID_ADPCM_IMA_ISS:
2982 return (frame_bytes - 4 * ch) * 2 / ch;
2983 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2984 return (frame_bytes - 4) * 2 / ch;
2985 case AV_CODEC_ID_ADPCM_IMA_AMV:
2986 return (frame_bytes - 8) * 2 / ch;
2987 case AV_CODEC_ID_ADPCM_XA:
2988 return (frame_bytes / 128) * 224 / ch;
2989 case AV_CODEC_ID_INTERPLAY_DPCM:
2990 return (frame_bytes - 6 - ch) / ch;
2991 case AV_CODEC_ID_ROQ_DPCM:
2992 return (frame_bytes - 8) / ch;
2993 case AV_CODEC_ID_XAN_DPCM:
2994 return (frame_bytes - 2 * ch) / ch;
2995 case AV_CODEC_ID_MACE3:
2996 return 3 * frame_bytes / ch;
2997 case AV_CODEC_ID_MACE6:
2998 return 6 * frame_bytes / ch;
2999 case AV_CODEC_ID_PCM_LXF:
3000 return 2 * (frame_bytes / (5 * ch));
3001 case AV_CODEC_ID_IAC:
3002 case AV_CODEC_ID_IMC:
3003 return 4 * frame_bytes / ch;
3007 /* calc from frame_bytes, channels, and codec_tag */
3008 if (id == AV_CODEC_ID_SOL_DPCM) {
3010 return frame_bytes / ch;
3012 return frame_bytes * 2 / ch;
3017 /* calc from frame_bytes, channels, and block_align */
3018 int blocks = frame_bytes / ba;
3019 switch (avctx->codec_id) {
3020 case AV_CODEC_ID_ADPCM_IMA_WAV:
3021 if (bps < 2 || bps > 5)
3023 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3024 case AV_CODEC_ID_ADPCM_IMA_DK3:
3025 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3026 case AV_CODEC_ID_ADPCM_IMA_DK4:
3027 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3028 case AV_CODEC_ID_ADPCM_IMA_RAD:
3029 return blocks * ((ba - 4 * ch) * 2 / ch);
3030 case AV_CODEC_ID_ADPCM_MS:
3031 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3036 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3037 switch (avctx->codec_id) {
3038 case AV_CODEC_ID_PCM_DVD:
3041 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3042 case AV_CODEC_ID_PCM_BLURAY:
3045 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3046 case AV_CODEC_ID_S302M:
3047 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3057 int ff_thread_init(AVCodecContext *s)
3064 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3078 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3081 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3085 #if FF_API_MISSING_SAMPLE
3086 FF_DISABLE_DEPRECATION_WARNINGS
3087 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3089 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3090 "version to the newest one from Git. If the problem still "
3091 "occurs, it means that your file has a feature which has not "
3092 "been implemented.\n", feature);
3094 av_log_ask_for_sample(avc, NULL);
3097 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3099 va_list argument_list;
3101 va_start(argument_list, msg);
3104 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3105 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3106 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
3107 "and contact the ffmpeg-devel mailing list.\n");
3109 va_end(argument_list);
3111 FF_ENABLE_DEPRECATION_WARNINGS
3112 #endif /* FF_API_MISSING_SAMPLE */
3114 static AVHWAccel *first_hwaccel = NULL;
3116 void av_register_hwaccel(AVHWAccel *hwaccel)
3118 AVHWAccel **p = &first_hwaccel;
3119 hwaccel->next = NULL;
3120 while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3124 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
3126 return hwaccel ? hwaccel->next : first_hwaccel;
3129 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
3131 AVHWAccel *hwaccel = NULL;
3133 while ((hwaccel = av_hwaccel_next(hwaccel)))
3134 if (hwaccel->id == codec_id
3135 && hwaccel->pix_fmt == pix_fmt)
3140 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3143 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3145 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3152 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3154 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3160 int ff_lock_avcodec(AVCodecContext *log_ctx)
3163 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3166 entangled_thread_counter++;
3167 if (entangled_thread_counter != 1) {
3168 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3170 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3171 ff_avcodec_locked = 1;
3172 ff_unlock_avcodec();
3173 return AVERROR(EINVAL);
3175 av_assert0(!ff_avcodec_locked);
3176 ff_avcodec_locked = 1;
3180 int ff_unlock_avcodec(void)
3182 av_assert0(ff_avcodec_locked);
3183 ff_avcodec_locked = 0;
3184 entangled_thread_counter--;
3186 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3192 int avpriv_lock_avformat(void)
3195 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3201 int avpriv_unlock_avformat(void)
3204 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3210 unsigned int avpriv_toupper4(unsigned int x)
3212 return av_toupper(x & 0xFF) +
3213 (av_toupper((x >> 8) & 0xFF) << 8) +
3214 (av_toupper((x >> 16) & 0xFF) << 16) +
3215 (av_toupper((x >> 24) & 0xFF) << 24);
3218 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3222 dst->owner = src->owner;
3224 ret = av_frame_ref(dst->f, src->f);
3228 if (src->progress &&
3229 !(dst->progress = av_buffer_ref(src->progress))) {
3230 ff_thread_release_buffer(dst->owner, dst);
3231 return AVERROR(ENOMEM);
3239 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3241 return avctx->get_format(avctx, fmt);
3244 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3247 return ff_get_buffer(avctx, f->f, flags);
3250 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3252 av_frame_unref(f->f);
3255 void ff_thread_finish_setup(AVCodecContext *avctx)
3259 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3263 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3267 int ff_thread_can_start_frame(AVCodecContext *avctx)
3272 int ff_alloc_entries(AVCodecContext *avctx, int count)
3277 void ff_reset_entries(AVCodecContext *avctx)
3281 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3285 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3291 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3293 AVCodec *c= avcodec_find_decoder(codec_id);
3295 c= avcodec_find_encoder(codec_id);
3299 if (codec_id <= AV_CODEC_ID_NONE)
3300 return AVMEDIA_TYPE_UNKNOWN;
3301 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3302 return AVMEDIA_TYPE_VIDEO;
3303 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3304 return AVMEDIA_TYPE_AUDIO;
3305 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3306 return AVMEDIA_TYPE_SUBTITLE;
3308 return AVMEDIA_TYPE_UNKNOWN;
3311 int avcodec_is_open(AVCodecContext *s)
3313 return !!s->internal;
3316 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3321 ret = av_bprint_finalize(buf, &str);
3324 avctx->extradata = str;
3325 /* Note: the string is NUL terminated (so extradata can be read as a
3326 * string), but the ending character is not accounted in the size (in
3327 * binary formats you are likely not supposed to mux that character). When
3328 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3330 avctx->extradata_size = buf->len;
3334 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3336 uint32_t *av_restrict state)
3340 av_assert0(p <= end);
3344 for (i = 0; i < 3; i++) {
3345 uint32_t tmp = *state << 8;
3346 *state = tmp + *(p++);
3347 if (tmp == 0x100 || p == end)
3352 if (p[-1] > 1 ) p += 3;
3353 else if (p[-2] ) p += 2;
3354 else if (p[-3]|(p[-1]-1)) p++;
3361 p = FFMIN(p, end) - 4;
3362 *state = AV_RB32(p);