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
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/crc.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avassert.h"
40 #include "libavutil/opt.h"
42 #include "frame_thread_encoder.h"
44 #include "bytestream.h"
50 static int volatile entangled_thread_counter = 0;
51 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
52 static void *codec_mutex;
53 static void *avformat_mutex;
55 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
60 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
62 ptr = av_realloc(ptr, min_size);
63 /* we could set this to the unmodified min_size but this is safer
64 * if the user lost the ptr and uses NULL now
74 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
79 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
81 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
88 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
90 ff_fast_malloc(ptr, size, min_size, 0);
93 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
96 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
101 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
102 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
105 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
108 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
113 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
114 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
117 /* encoder management */
118 static AVCodec *first_avcodec = NULL;
120 AVCodec *av_codec_next(const AVCodec *c)
125 return first_avcodec;
128 static void avcodec_init(void)
130 static int initialized = 0;
132 if (initialized != 0)
136 ff_dsputil_static_init();
139 int av_codec_is_encoder(const AVCodec *codec)
141 return codec && (codec->encode_sub || codec->encode2);
144 int av_codec_is_decoder(const AVCodec *codec)
146 return codec && codec->decode;
149 void avcodec_register(AVCodec *codec)
159 if (codec->init_static_data)
160 codec->init_static_data(codec);
163 unsigned avcodec_get_edge_width(void)
168 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
170 s->coded_width = width;
171 s->coded_height = height;
172 s->width = -((-width ) >> s->lowres);
173 s->height = -((-height) >> s->lowres);
176 #define INTERNAL_BUFFER_SIZE (32 + 1)
178 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
179 int linesize_align[AV_NUM_DATA_POINTERS])
185 switch (s->pix_fmt) {
186 case AV_PIX_FMT_YUV420P:
187 case AV_PIX_FMT_YUYV422:
188 case AV_PIX_FMT_UYVY422:
189 case AV_PIX_FMT_YUV422P:
190 case AV_PIX_FMT_YUV440P:
191 case AV_PIX_FMT_YUV444P:
192 case AV_PIX_FMT_GBRP:
193 case AV_PIX_FMT_GRAY8:
194 case AV_PIX_FMT_GRAY16BE:
195 case AV_PIX_FMT_GRAY16LE:
196 case AV_PIX_FMT_YUVJ420P:
197 case AV_PIX_FMT_YUVJ422P:
198 case AV_PIX_FMT_YUVJ440P:
199 case AV_PIX_FMT_YUVJ444P:
200 case AV_PIX_FMT_YUVA420P:
201 case AV_PIX_FMT_YUVA422P:
202 case AV_PIX_FMT_YUVA444P:
203 case AV_PIX_FMT_YUV420P9LE:
204 case AV_PIX_FMT_YUV420P9BE:
205 case AV_PIX_FMT_YUV420P10LE:
206 case AV_PIX_FMT_YUV420P10BE:
207 case AV_PIX_FMT_YUV420P12LE:
208 case AV_PIX_FMT_YUV420P12BE:
209 case AV_PIX_FMT_YUV420P14LE:
210 case AV_PIX_FMT_YUV420P14BE:
211 case AV_PIX_FMT_YUV422P9LE:
212 case AV_PIX_FMT_YUV422P9BE:
213 case AV_PIX_FMT_YUV422P10LE:
214 case AV_PIX_FMT_YUV422P10BE:
215 case AV_PIX_FMT_YUV422P12LE:
216 case AV_PIX_FMT_YUV422P12BE:
217 case AV_PIX_FMT_YUV422P14LE:
218 case AV_PIX_FMT_YUV422P14BE:
219 case AV_PIX_FMT_YUV444P9LE:
220 case AV_PIX_FMT_YUV444P9BE:
221 case AV_PIX_FMT_YUV444P10LE:
222 case AV_PIX_FMT_YUV444P10BE:
223 case AV_PIX_FMT_YUV444P12LE:
224 case AV_PIX_FMT_YUV444P12BE:
225 case AV_PIX_FMT_YUV444P14LE:
226 case AV_PIX_FMT_YUV444P14BE:
227 case AV_PIX_FMT_GBRP9LE:
228 case AV_PIX_FMT_GBRP9BE:
229 case AV_PIX_FMT_GBRP10LE:
230 case AV_PIX_FMT_GBRP10BE:
231 case AV_PIX_FMT_GBRP12LE:
232 case AV_PIX_FMT_GBRP12BE:
233 case AV_PIX_FMT_GBRP14LE:
234 case AV_PIX_FMT_GBRP14BE:
235 w_align = 16; //FIXME assume 16 pixel per macroblock
236 h_align = 16 * 2; // interlaced needs 2 macroblocks height
238 case AV_PIX_FMT_YUV411P:
239 case AV_PIX_FMT_UYYVYY411:
243 case AV_PIX_FMT_YUV410P:
244 if (s->codec_id == AV_CODEC_ID_SVQ1) {
249 case AV_PIX_FMT_RGB555:
250 if (s->codec_id == AV_CODEC_ID_RPZA) {
255 case AV_PIX_FMT_PAL8:
256 case AV_PIX_FMT_BGR8:
257 case AV_PIX_FMT_RGB8:
258 if (s->codec_id == AV_CODEC_ID_SMC) {
263 case AV_PIX_FMT_BGR24:
264 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
265 (s->codec_id == AV_CODEC_ID_ZLIB)) {
276 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
277 w_align = FFMAX(w_align, 8);
280 *width = FFALIGN(*width, w_align);
281 *height = FFALIGN(*height, h_align);
282 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
283 // some of the optimized chroma MC reads one line too much
284 // which is also done in mpeg decoders with lowres > 0
287 for (i = 0; i < 4; i++)
288 linesize_align[i] = STRIDE_ALIGN;
291 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
293 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
294 int chroma_shift = desc->log2_chroma_w;
295 int linesize_align[AV_NUM_DATA_POINTERS];
298 avcodec_align_dimensions2(s, width, height, linesize_align);
299 align = FFMAX(linesize_align[0], linesize_align[3]);
300 linesize_align[1] <<= chroma_shift;
301 linesize_align[2] <<= chroma_shift;
302 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
303 *width = FFALIGN(*width, align);
306 void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
309 frame->pkt_pts = s->pkt->pts;
310 frame->pkt_pos = s->pkt->pos;
311 frame->pkt_duration = s->pkt->duration;
313 frame->pkt_pts = AV_NOPTS_VALUE;
315 frame->pkt_duration = 0;
317 frame->reordered_opaque = s->reordered_opaque;
319 switch (s->codec->type) {
320 case AVMEDIA_TYPE_VIDEO:
321 frame->width = s->width;
322 frame->height = s->height;
323 frame->format = s->pix_fmt;
324 frame->sample_aspect_ratio = s->sample_aspect_ratio;
326 case AVMEDIA_TYPE_AUDIO:
327 frame->sample_rate = s->sample_rate;
328 frame->format = s->sample_fmt;
329 frame->channel_layout = s->channel_layout;
330 frame->channels = s->channels;
335 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
336 enum AVSampleFormat sample_fmt, const uint8_t *buf,
337 int buf_size, int align)
339 int ch, planar, needed_size, ret = 0;
341 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
342 frame->nb_samples, sample_fmt,
344 if (buf_size < needed_size)
345 return AVERROR(EINVAL);
347 planar = av_sample_fmt_is_planar(sample_fmt);
348 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
349 if (!(frame->extended_data = av_mallocz(nb_channels *
350 sizeof(*frame->extended_data))))
351 return AVERROR(ENOMEM);
353 frame->extended_data = frame->data;
356 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
357 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
358 sample_fmt, align)) < 0) {
359 if (frame->extended_data != frame->data)
360 av_freep(&frame->extended_data);
363 if (frame->extended_data != frame->data) {
364 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
365 frame->data[ch] = frame->extended_data[ch];
371 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
373 AVCodecInternal *avci = avctx->internal;
377 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
378 frame->nb_samples, avctx->sample_fmt,
381 return AVERROR(EINVAL);
383 /* allocate InternalBuffer if needed */
385 avci->buffer = av_mallocz(sizeof(InternalBuffer));
387 return AVERROR(ENOMEM);
391 /* if there is a previously-used internal buffer, check its size and
392 * channel count to see if we can reuse it */
393 if (buf->extended_data) {
394 /* if current buffer is too small, free it */
395 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
396 av_free(buf->extended_data[0]);
397 if (buf->extended_data != buf->data)
398 av_free(buf->extended_data);
399 buf->extended_data = NULL;
402 /* if number of channels has changed, reset and/or free extended data
403 * pointers but leave data buffer in buf->data[0] for reuse */
404 if (buf->nb_channels != avctx->channels) {
405 if (buf->extended_data != buf->data)
406 av_free(buf->extended_data);
407 buf->extended_data = NULL;
411 /* if there is no previous buffer or the previous buffer cannot be used
412 * as-is, allocate a new buffer and/or rearrange the channel pointers */
413 if (!buf->extended_data) {
415 if (!(buf->data[0] = av_mallocz(buf_size)))
416 return AVERROR(ENOMEM);
417 buf->audio_data_size = buf_size;
419 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
420 avctx->sample_fmt, buf->data[0],
421 buf->audio_data_size, 0)))
424 if (frame->extended_data == frame->data)
425 buf->extended_data = buf->data;
427 buf->extended_data = frame->extended_data;
428 memcpy(buf->data, frame->data, sizeof(frame->data));
429 buf->linesize[0] = frame->linesize[0];
430 buf->nb_channels = avctx->channels;
432 /* copy InternalBuffer info to the AVFrame */
433 frame->extended_data = buf->extended_data;
434 frame->linesize[0] = buf->linesize[0];
435 memcpy(frame->data, buf->data, sizeof(frame->data));
438 frame->type = FF_BUFFER_TYPE_INTERNAL;
439 ff_init_buffer_info(avctx, frame);
441 if (avctx->debug & FF_DEBUG_BUFFERS)
442 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
443 "internal audio buffer used\n", frame);
448 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
454 AVCodecInternal *avci = s->internal;
456 if (pic->data[0] != NULL) {
457 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
460 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
461 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
465 if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
466 av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
471 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
472 sizeof(InternalBuffer));
475 buf = &avci->buffer[avci->buffer_count];
477 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
478 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
479 av_freep(&buf->base[i]);
485 int h_chroma_shift, v_chroma_shift;
490 int stride_align[AV_NUM_DATA_POINTERS];
491 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
492 const int pixel_size = desc->comp[0].step_minus1 + 1;
494 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
496 avcodec_align_dimensions2(s, &w, &h, stride_align);
498 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
504 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
505 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
506 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
507 // increase alignment of w for next try (rhs gives the lowest bit set in w)
511 for (i = 0; i < 4; i++)
512 unaligned |= picture.linesize[i] % stride_align[i];
515 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
519 for (i = 0; i < 3 && picture.data[i + 1]; i++)
520 size[i] = picture.data[i + 1] - picture.data[i];
521 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
523 memset(buf->base, 0, sizeof(buf->base));
524 memset(buf->data, 0, sizeof(buf->data));
526 for (i = 0; i < 4 && size[i]; i++) {
527 const int h_shift = i == 0 ? 0 : h_chroma_shift;
528 const int v_shift = i == 0 ? 0 : v_chroma_shift;
530 buf->linesize[i] = picture.linesize[i];
532 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
533 if (buf->base[i] == NULL)
534 return AVERROR(ENOMEM);
535 memset(buf->base[i], 128, size[i]);
537 // no edge if EDGE EMU or not planar YUV
538 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
539 buf->data[i] = buf->base[i];
541 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
543 for (; i < AV_NUM_DATA_POINTERS; i++) {
544 buf->base[i] = buf->data[i] = NULL;
545 buf->linesize[i] = 0;
547 if (size[1] && !size[2])
548 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
549 buf->width = s->width;
550 buf->height = s->height;
551 buf->pix_fmt = s->pix_fmt;
553 pic->type = FF_BUFFER_TYPE_INTERNAL;
555 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
556 pic->base[i] = buf->base[i];
557 pic->data[i] = buf->data[i];
558 pic->linesize[i] = buf->linesize[i];
560 pic->extended_data = pic->data;
561 avci->buffer_count++;
562 pic->width = buf->width;
563 pic->height = buf->height;
564 pic->format = buf->pix_fmt;
566 ff_init_buffer_info(s, pic);
568 if (s->debug & FF_DEBUG_BUFFERS)
569 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
570 "buffers used\n", pic, avci->buffer_count);
575 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
577 switch (avctx->codec_type) {
578 case AVMEDIA_TYPE_VIDEO:
579 return video_get_buffer(avctx, frame);
580 case AVMEDIA_TYPE_AUDIO:
581 return audio_get_buffer(avctx, frame);
587 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
590 InternalBuffer *buf, *last;
591 AVCodecInternal *avci = s->internal;
593 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
595 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
596 assert(avci->buffer_count);
599 buf = NULL; /* avoids warning */
600 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
601 buf = &avci->buffer[i];
602 if (buf->data[0] == pic->data[0])
605 av_assert0(i < avci->buffer_count);
606 avci->buffer_count--;
607 last = &avci->buffer[avci->buffer_count];
610 FFSWAP(InternalBuffer, *buf, *last);
613 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
615 // pic->base[i]=NULL;
617 if (s->debug & FF_DEBUG_BUFFERS)
618 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
619 "buffers used\n", pic, avci->buffer_count);
622 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
627 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
629 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
630 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
631 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
632 s->release_buffer(s, pic);
635 ff_init_buffer_info(s, pic);
637 /* If no picture return a new buffer */
638 if (pic->data[0] == NULL) {
639 /* We will copy from buffer, so must be readable */
640 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
641 return s->get_buffer(s, pic);
644 assert(s->pix_fmt == pic->format);
646 /* If internal buffer type return the same buffer */
647 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
652 * Not internal type and reget_buffer not overridden, emulate cr buffer
655 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
656 pic->data[i] = pic->base[i] = NULL;
658 /* Allocate new frame */
659 if ((ret = s->get_buffer(s, pic)))
661 /* Copy image data from old buffer to new buffer */
662 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
664 s->release_buffer(s, &temp_pic); // Release old frame
668 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
672 for (i = 0; i < count; i++) {
673 int r = func(c, (char *)arg + i * size);
680 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
684 for (i = 0; i < count; i++) {
685 int r = func(c, arg, i, 0);
692 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
694 while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
699 void avcodec_get_frame_defaults(AVFrame *frame)
701 #if LIBAVCODEC_VERSION_MAJOR >= 55
702 // extended_data should explicitly be freed when needed, this code is unsafe currently
703 // also this is not compatible to the <55 ABI/API
704 if (frame->extended_data != frame->data && 0)
705 av_freep(&frame->extended_data);
708 memset(frame, 0, sizeof(AVFrame));
713 frame->best_effort_timestamp = AV_NOPTS_VALUE;
714 frame->pkt_duration = 0;
716 frame->key_frame = 1;
717 frame->sample_aspect_ratio = (AVRational) {0, 1 };
718 frame->format = -1; /* unknown */
719 frame->extended_data = frame->data;
722 AVFrame *avcodec_alloc_frame(void)
724 AVFrame *frame = av_malloc(sizeof(AVFrame));
729 frame->extended_data = NULL;
730 avcodec_get_frame_defaults(frame);
735 void avcodec_free_frame(AVFrame **frame)
739 if (!frame || !*frame)
744 if (f->extended_data != f->data)
745 av_freep(&f->extended_data);
750 #define MAKE_ACCESSORS(str, name, type, field) \
751 type av_##name##_get_##field(const str *s) { return s->field; } \
752 void av_##name##_set_##field(str *s, type v) { s->field = v; }
754 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
755 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
756 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
757 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
758 MAKE_ACCESSORS(AVFrame, frame, int, channels)
759 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
760 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
761 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
763 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
764 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
766 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
768 memset(sub, 0, sizeof(*sub));
769 sub->pts = AV_NOPTS_VALUE;
772 static int get_bit_rate(AVCodecContext *ctx)
777 switch (ctx->codec_type) {
778 case AVMEDIA_TYPE_VIDEO:
779 case AVMEDIA_TYPE_DATA:
780 case AVMEDIA_TYPE_SUBTITLE:
781 case AVMEDIA_TYPE_ATTACHMENT:
782 bit_rate = ctx->bit_rate;
784 case AVMEDIA_TYPE_AUDIO:
785 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
786 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
795 #if FF_API_AVCODEC_OPEN
796 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
798 return avcodec_open2(avctx, codec, NULL);
802 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
806 entangled_thread_counter--;
807 /* Release any user-supplied mutex. */
809 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
812 ret = avcodec_open2(avctx, codec, options);
814 /* If there is a user-supplied mutex locking routine, call it. */
816 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
819 entangled_thread_counter++;
823 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
826 AVDictionary *tmp = NULL;
828 if (avcodec_is_open(avctx))
831 if ((!codec && !avctx->codec)) {
832 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
833 return AVERROR(EINVAL);
835 if ((codec && avctx->codec && codec != avctx->codec)) {
836 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
837 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
838 return AVERROR(EINVAL);
841 codec = avctx->codec;
843 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
844 return AVERROR(EINVAL);
847 av_dict_copy(&tmp, *options, 0);
849 /* If there is a user-supplied mutex locking routine, call it. */
851 if ((ret = (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) < 0)
855 entangled_thread_counter++;
856 if (entangled_thread_counter != 1) {
857 av_log(avctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
858 ret = AVERROR(EINVAL);
862 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
863 if (!avctx->internal) {
864 ret = AVERROR(ENOMEM);
868 if (codec->priv_data_size > 0) {
869 if (!avctx->priv_data) {
870 avctx->priv_data = av_mallocz(codec->priv_data_size);
871 if (!avctx->priv_data) {
872 ret = AVERROR(ENOMEM);
875 if (codec->priv_class) {
876 *(const AVClass **)avctx->priv_data = codec->priv_class;
877 av_opt_set_defaults(avctx->priv_data);
880 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
883 avctx->priv_data = NULL;
885 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
888 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
889 if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
891 if (avctx->coded_width && avctx->coded_height)
892 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
893 else if (avctx->width && avctx->height)
894 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
897 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
898 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
899 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
900 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
901 avcodec_set_dimensions(avctx, 0, 0);
904 /* if the decoder init function was already called previously,
905 * free the already allocated subtitle_header before overwriting it */
906 if (av_codec_is_decoder(codec))
907 av_freep(&avctx->subtitle_header);
909 if (avctx->channels > FF_SANE_NB_CHANNELS) {
910 ret = AVERROR(EINVAL);
914 avctx->codec = codec;
915 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
916 avctx->codec_id == AV_CODEC_ID_NONE) {
917 avctx->codec_type = codec->type;
918 avctx->codec_id = codec->id;
920 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
921 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
922 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
923 ret = AVERROR(EINVAL);
926 avctx->frame_number = 0;
927 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
929 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
930 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
931 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
933 av_log(NULL, AV_LOG_ERROR,
934 "The %s '%s' is experimental but experimental codecs are not enabled, "
935 "add '-strict %d' if you want to use it.\n",
936 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
937 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
938 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
939 av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
940 codec_string, codec2->name);
941 ret = AVERROR_EXPERIMENTAL;
945 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
946 (!avctx->time_base.num || !avctx->time_base.den)) {
947 avctx->time_base.num = 1;
948 avctx->time_base.den = avctx->sample_rate;
952 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
955 entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
956 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
957 entangled_thread_counter++;
962 if (HAVE_THREADS && !avctx->thread_opaque
963 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
964 ret = ff_thread_init(avctx);
969 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
970 avctx->thread_count = 1;
972 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
973 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
974 avctx->codec->max_lowres);
975 ret = AVERROR(EINVAL);
979 if (av_codec_is_encoder(avctx->codec)) {
981 if (avctx->codec->sample_fmts) {
982 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
983 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
985 if (avctx->channels == 1 &&
986 av_get_planar_sample_fmt(avctx->sample_fmt) ==
987 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
988 avctx->sample_fmt = avctx->codec->sample_fmts[i];
992 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
994 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
995 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
996 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
997 ret = AVERROR(EINVAL);
1001 if (avctx->codec->pix_fmts) {
1002 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1003 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1005 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1006 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1007 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1009 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1010 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1011 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1012 ret = AVERROR(EINVAL);
1016 if (avctx->codec->supported_samplerates) {
1017 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1018 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1020 if (avctx->codec->supported_samplerates[i] == 0) {
1021 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1022 avctx->sample_rate);
1023 ret = AVERROR(EINVAL);
1027 if (avctx->codec->channel_layouts) {
1028 if (!avctx->channel_layout) {
1029 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1031 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1032 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1034 if (avctx->codec->channel_layouts[i] == 0) {
1036 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1037 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1038 ret = AVERROR(EINVAL);
1043 if (avctx->channel_layout && avctx->channels) {
1044 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1045 if (channels != avctx->channels) {
1047 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1048 av_log(avctx, AV_LOG_ERROR,
1049 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1050 buf, channels, avctx->channels);
1051 ret = AVERROR(EINVAL);
1054 } else if (avctx->channel_layout) {
1055 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1059 avctx->pts_correction_num_faulty_pts =
1060 avctx->pts_correction_num_faulty_dts = 0;
1061 avctx->pts_correction_last_pts =
1062 avctx->pts_correction_last_dts = INT64_MIN;
1064 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1065 || avctx->internal->frame_thread_encoder)) {
1066 ret = avctx->codec->init(avctx);
1074 if (av_codec_is_decoder(avctx->codec)) {
1075 if (!avctx->bit_rate)
1076 avctx->bit_rate = get_bit_rate(avctx);
1077 /* validate channel layout from the decoder */
1078 if (avctx->channel_layout) {
1079 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1080 if (!avctx->channels)
1081 avctx->channels = channels;
1082 else if (channels != avctx->channels) {
1084 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1085 av_log(avctx, AV_LOG_WARNING,
1086 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1087 "ignoring specified channel layout\n",
1088 buf, channels, avctx->channels);
1089 avctx->channel_layout = 0;
1092 if (avctx->channels && avctx->channels < 0 ||
1093 avctx->channels > FF_SANE_NB_CHANNELS) {
1094 ret = AVERROR(EINVAL);
1099 entangled_thread_counter--;
1101 /* Release any user-supplied mutex. */
1102 if (ff_lockmgr_cb) {
1103 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1106 av_dict_free(options);
1113 av_freep(&avctx->priv_data);
1114 av_freep(&avctx->internal);
1115 avctx->codec = NULL;
1119 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1121 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1122 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1123 return AVERROR(EINVAL);
1127 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1128 if (!avpkt->data || avpkt->size < size) {
1129 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1130 avpkt->data = avctx->internal->byte_buffer;
1131 avpkt->size = avctx->internal->byte_buffer_size;
1132 avpkt->destruct = NULL;
1137 void *destruct = avpkt->destruct;
1139 if (avpkt->size < size) {
1140 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1141 return AVERROR(EINVAL);
1144 av_init_packet(avpkt);
1145 avpkt->destruct = destruct;
1149 int ret = av_new_packet(avpkt, size);
1151 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1156 int ff_alloc_packet(AVPacket *avpkt, int size)
1158 return ff_alloc_packet2(NULL, avpkt, size);
1162 * Pad last frame with silence.
1164 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1166 AVFrame *frame = NULL;
1167 uint8_t *buf = NULL;
1170 if (!(frame = avcodec_alloc_frame()))
1171 return AVERROR(ENOMEM);
1174 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1175 s->frame_size, s->sample_fmt, 0)) < 0)
1178 if (!(buf = av_malloc(ret))) {
1179 ret = AVERROR(ENOMEM);
1183 frame->nb_samples = s->frame_size;
1184 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1187 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1188 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1190 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1191 frame->nb_samples - src->nb_samples,
1192 s->channels, s->sample_fmt)) < 0)
1200 if (frame->extended_data != frame->data)
1201 av_freep(&frame->extended_data);
1207 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1209 const AVFrame *frame,
1210 int *got_packet_ptr)
1213 AVFrame *padded_frame = NULL;
1215 AVPacket user_pkt = *avpkt;
1216 int needs_realloc = !user_pkt.data;
1218 *got_packet_ptr = 0;
1220 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1221 av_free_packet(avpkt);
1222 av_init_packet(avpkt);
1226 /* ensure that extended_data is properly set */
1227 if (frame && !frame->extended_data) {
1228 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1229 avctx->channels > AV_NUM_DATA_POINTERS) {
1230 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1231 "with more than %d channels, but extended_data is not set.\n",
1232 AV_NUM_DATA_POINTERS);
1233 return AVERROR(EINVAL);
1235 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1238 tmp.extended_data = tmp.data;
1242 /* check for valid frame size */
1244 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1245 if (frame->nb_samples > avctx->frame_size) {
1246 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1247 return AVERROR(EINVAL);
1249 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1250 if (frame->nb_samples < avctx->frame_size &&
1251 !avctx->internal->last_audio_frame) {
1252 ret = pad_last_frame(avctx, &padded_frame, frame);
1256 frame = padded_frame;
1257 avctx->internal->last_audio_frame = 1;
1260 if (frame->nb_samples != avctx->frame_size) {
1261 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1262 ret = AVERROR(EINVAL);
1268 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1270 if (*got_packet_ptr) {
1271 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1272 if (avpkt->pts == AV_NOPTS_VALUE)
1273 avpkt->pts = frame->pts;
1274 if (!avpkt->duration)
1275 avpkt->duration = ff_samples_to_time_base(avctx,
1278 avpkt->dts = avpkt->pts;
1283 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1285 if (user_pkt.data) {
1286 if (user_pkt.size >= avpkt->size) {
1287 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1289 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1290 avpkt->size = user_pkt.size;
1293 avpkt->data = user_pkt.data;
1294 avpkt->destruct = user_pkt.destruct;
1296 if (av_dup_packet(avpkt) < 0) {
1297 ret = AVERROR(ENOMEM);
1303 if (needs_realloc && avpkt->data) {
1304 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1306 avpkt->data = new_data;
1309 avctx->frame_number++;
1312 if (ret < 0 || !*got_packet_ptr) {
1313 av_free_packet(avpkt);
1314 av_init_packet(avpkt);
1318 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1319 * this needs to be moved to the encoders, but for now we can do it
1320 * here to simplify things */
1321 avpkt->flags |= AV_PKT_FLAG_KEY;
1325 av_freep(&padded_frame->data[0]);
1326 if (padded_frame->extended_data != padded_frame->data)
1327 av_freep(&padded_frame->extended_data);
1328 av_freep(&padded_frame);
1334 #if FF_API_OLD_ENCODE_AUDIO
1335 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1336 uint8_t *buf, int buf_size,
1337 const short *samples)
1340 AVFrame frame0 = { 0 };
1342 int ret, samples_size, got_packet;
1344 av_init_packet(&pkt);
1346 pkt.size = buf_size;
1350 avcodec_get_frame_defaults(frame);
1352 if (avctx->frame_size) {
1353 frame->nb_samples = avctx->frame_size;
1355 /* if frame_size is not set, the number of samples must be
1356 * calculated from the buffer size */
1358 if (!av_get_bits_per_sample(avctx->codec_id)) {
1359 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1360 "support this codec\n");
1361 return AVERROR(EINVAL);
1363 nb_samples = (int64_t)buf_size * 8 /
1364 (av_get_bits_per_sample(avctx->codec_id) *
1366 if (nb_samples >= INT_MAX)
1367 return AVERROR(EINVAL);
1368 frame->nb_samples = nb_samples;
1371 /* it is assumed that the samples buffer is large enough based on the
1372 * relevant parameters */
1373 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1375 avctx->sample_fmt, 1);
1376 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1378 (const uint8_t *)samples,
1382 /* fabricate frame pts from sample count.
1383 * this is needed because the avcodec_encode_audio() API does not have
1384 * a way for the user to provide pts */
1385 if (avctx->sample_rate && avctx->time_base.num)
1386 frame->pts = ff_samples_to_time_base(avctx,
1387 avctx->internal->sample_count);
1389 frame->pts = AV_NOPTS_VALUE;
1390 avctx->internal->sample_count += frame->nb_samples;
1396 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1397 if (!ret && got_packet && avctx->coded_frame) {
1398 avctx->coded_frame->pts = pkt.pts;
1399 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1401 /* free any side data since we cannot return it */
1402 ff_packet_free_side_data(&pkt);
1404 if (frame && frame->extended_data != frame->data)
1405 av_freep(&frame->extended_data);
1407 return ret ? ret : pkt.size;
1412 #if FF_API_OLD_ENCODE_VIDEO
1413 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1414 const AVFrame *pict)
1417 int ret, got_packet = 0;
1419 if (buf_size < FF_MIN_BUFFER_SIZE) {
1420 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1424 av_init_packet(&pkt);
1426 pkt.size = buf_size;
1428 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1429 if (!ret && got_packet && avctx->coded_frame) {
1430 avctx->coded_frame->pts = pkt.pts;
1431 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1434 /* free any side data since we cannot return it */
1435 if (pkt.side_data_elems > 0) {
1437 for (i = 0; i < pkt.side_data_elems; i++)
1438 av_free(pkt.side_data[i].data);
1439 av_freep(&pkt.side_data);
1440 pkt.side_data_elems = 0;
1443 return ret ? ret : pkt.size;
1448 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1450 const AVFrame *frame,
1451 int *got_packet_ptr)
1454 AVPacket user_pkt = *avpkt;
1455 int needs_realloc = !user_pkt.data;
1457 *got_packet_ptr = 0;
1459 if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1460 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1462 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1463 av_free_packet(avpkt);
1464 av_init_packet(avpkt);
1469 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1470 return AVERROR(EINVAL);
1472 av_assert0(avctx->codec->encode2);
1474 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1475 av_assert0(ret <= 0);
1477 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1479 if (user_pkt.data) {
1480 if (user_pkt.size >= avpkt->size) {
1481 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1483 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1484 avpkt->size = user_pkt.size;
1487 avpkt->data = user_pkt.data;
1488 avpkt->destruct = user_pkt.destruct;
1490 if (av_dup_packet(avpkt) < 0) {
1491 ret = AVERROR(ENOMEM);
1497 if (!*got_packet_ptr)
1499 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1500 avpkt->pts = avpkt->dts = frame->pts;
1502 if (needs_realloc && avpkt->data &&
1503 avpkt->destruct == av_destruct_packet) {
1504 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1506 avpkt->data = new_data;
1509 avctx->frame_number++;
1512 if (ret < 0 || !*got_packet_ptr)
1513 av_free_packet(avpkt);
1519 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1520 const AVSubtitle *sub)
1523 if (sub->start_display_time) {
1524 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1528 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1529 avctx->frame_number++;
1534 * Attempt to guess proper monotonic timestamps for decoded video frames
1535 * which might have incorrect times. Input timestamps may wrap around, in
1536 * which case the output will as well.
1538 * @param pts the pts field of the decoded AVPacket, as passed through
1540 * @param dts the dts field of the decoded AVPacket
1541 * @return one of the input values, may be AV_NOPTS_VALUE
1543 static int64_t guess_correct_pts(AVCodecContext *ctx,
1544 int64_t reordered_pts, int64_t dts)
1546 int64_t pts = AV_NOPTS_VALUE;
1548 if (dts != AV_NOPTS_VALUE) {
1549 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1550 ctx->pts_correction_last_dts = dts;
1552 if (reordered_pts != AV_NOPTS_VALUE) {
1553 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1554 ctx->pts_correction_last_pts = reordered_pts;
1556 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1557 && reordered_pts != AV_NOPTS_VALUE)
1558 pts = reordered_pts;
1565 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1568 const uint8_t *data;
1571 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1574 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1575 if (!data || size < 4)
1577 flags = bytestream_get_le32(&data);
1579 if (size < 4) /* Required for any of the changes */
1581 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1582 avctx->channels = bytestream_get_le32(&data);
1585 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1588 avctx->channel_layout = bytestream_get_le64(&data);
1593 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1594 avctx->sample_rate = bytestream_get_le32(&data);
1597 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1600 avctx->width = bytestream_get_le32(&data);
1601 avctx->height = bytestream_get_le32(&data);
1602 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1607 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1610 const uint8_t *side_metadata;
1613 av_dict_free(&avctx->metadata);
1614 side_metadata = av_packet_get_side_data(avctx->pkt,
1615 AV_PKT_DATA_STRINGS_METADATA, &size);
1618 end = side_metadata + size;
1619 while (side_metadata < end) {
1620 const uint8_t *key = side_metadata;
1621 const uint8_t *val = side_metadata + strlen(key) + 1;
1622 int ret = av_dict_set(&frame->metadata, key, val, 0);
1625 side_metadata = val + strlen(val) + 1;
1628 avctx->metadata = frame->metadata;
1632 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1633 int *got_picture_ptr,
1634 const AVPacket *avpkt)
1637 // copy to ensure we do not change avpkt
1638 AVPacket tmp = *avpkt;
1640 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1641 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1642 return AVERROR(EINVAL);
1645 *got_picture_ptr = 0;
1646 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1647 return AVERROR(EINVAL);
1649 avcodec_get_frame_defaults(picture);
1651 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1652 int did_split = av_packet_split_side_data(&tmp);
1653 apply_param_change(avctx, &tmp);
1655 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1656 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1659 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1661 picture->pkt_dts = avpkt->dts;
1663 if(!avctx->has_b_frames){
1664 picture->pkt_pos = avpkt->pos;
1666 //FIXME these should be under if(!avctx->has_b_frames)
1667 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1668 if (!picture->width) picture->width = avctx->width;
1669 if (!picture->height) picture->height = avctx->height;
1670 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1672 add_metadata_from_side_data(avctx, picture);
1674 emms_c(); //needed to avoid an emms_c() call before every return;
1678 ff_packet_free_side_data(&tmp);
1683 if (*got_picture_ptr){
1684 avctx->frame_number++;
1685 picture->best_effort_timestamp = guess_correct_pts(avctx,
1692 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1693 * make sure it's set correctly */
1694 picture->extended_data = picture->data;
1699 #if FF_API_OLD_DECODE_AUDIO
1700 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1701 int *frame_size_ptr,
1704 AVFrame frame = {0};
1705 int ret, got_frame = 0;
1707 if (avctx->get_buffer != avcodec_default_get_buffer) {
1708 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1709 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1710 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1711 "avcodec_decode_audio4()\n");
1712 avctx->get_buffer = avcodec_default_get_buffer;
1713 avctx->release_buffer = avcodec_default_release_buffer;
1716 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1718 if (ret >= 0 && got_frame) {
1720 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1721 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1723 avctx->sample_fmt, 1);
1724 if (*frame_size_ptr < data_size) {
1725 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1726 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1727 return AVERROR(EINVAL);
1730 memcpy(samples, frame.extended_data[0], plane_size);
1732 if (planar && avctx->channels > 1) {
1733 uint8_t *out = ((uint8_t *)samples) + plane_size;
1734 for (ch = 1; ch < avctx->channels; ch++) {
1735 memcpy(out, frame.extended_data[ch], plane_size);
1739 *frame_size_ptr = data_size;
1741 *frame_size_ptr = 0;
1748 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1751 const AVPacket *avpkt)
1753 int planar, channels;
1758 if (!avpkt->data && avpkt->size) {
1759 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1760 return AVERROR(EINVAL);
1762 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1763 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1764 return AVERROR(EINVAL);
1767 avcodec_get_frame_defaults(frame);
1769 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1772 // copy to ensure we do not change avpkt
1773 AVPacket tmp = *avpkt;
1774 int did_split = av_packet_split_side_data(&tmp);
1775 apply_param_change(avctx, &tmp);
1778 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1779 if (ret >= 0 && *got_frame_ptr) {
1780 avctx->frame_number++;
1781 frame->pkt_dts = avpkt->dts;
1782 frame->best_effort_timestamp = guess_correct_pts(avctx,
1785 if (frame->format == AV_SAMPLE_FMT_NONE)
1786 frame->format = avctx->sample_fmt;
1787 if (!frame->channel_layout)
1788 frame->channel_layout = avctx->channel_layout;
1789 if (!frame->channels)
1790 frame->channels = avctx->channels;
1791 if (!frame->sample_rate)
1792 frame->sample_rate = avctx->sample_rate;
1794 add_metadata_from_side_data(avctx, frame);
1796 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1797 if(side && side_size>=10) {
1798 avctx->internal->skip_samples = AV_RL32(side);
1799 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1800 avctx->internal->skip_samples);
1802 if (avctx->internal->skip_samples) {
1803 if(frame->nb_samples <= avctx->internal->skip_samples){
1805 avctx->internal->skip_samples -= frame->nb_samples;
1806 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1807 avctx->internal->skip_samples);
1809 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1810 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1811 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1812 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1813 (AVRational){1, avctx->sample_rate},
1814 avctx->pkt_timebase);
1815 if(frame->pkt_pts!=AV_NOPTS_VALUE)
1816 frame->pkt_pts += diff_ts;
1817 if(frame->pkt_dts!=AV_NOPTS_VALUE)
1818 frame->pkt_dts += diff_ts;
1819 if (frame->pkt_duration >= diff_ts)
1820 frame->pkt_duration -= diff_ts;
1822 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1824 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1825 avctx->internal->skip_samples, frame->nb_samples);
1826 frame->nb_samples -= avctx->internal->skip_samples;
1827 avctx->internal->skip_samples = 0;
1833 ff_packet_free_side_data(&tmp);
1839 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1840 * make sure it's set correctly; assume decoders that actually use
1841 * extended_data are doing it correctly */
1842 if (*got_frame_ptr) {
1843 planar = av_sample_fmt_is_planar(frame->format);
1844 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1845 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1846 frame->extended_data = frame->data;
1848 frame->extended_data = NULL;
1854 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1860 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1861 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1862 return AVERROR(EINVAL);
1866 avcodec_get_subtitle_defaults(sub);
1869 AVPacket tmp = *avpkt;
1870 int did_split = av_packet_split_side_data(&tmp);
1871 //apply_param_change(avctx, &tmp);
1875 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
1876 sub->pts = av_rescale_q(avpkt->pts,
1877 avctx->pkt_timebase, AV_TIME_BASE_Q);
1878 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &tmp);
1882 ff_packet_free_side_data(&tmp);
1888 avctx->frame_number++;
1894 void avsubtitle_free(AVSubtitle *sub)
1898 for (i = 0; i < sub->num_rects; i++) {
1899 av_freep(&sub->rects[i]->pict.data[0]);
1900 av_freep(&sub->rects[i]->pict.data[1]);
1901 av_freep(&sub->rects[i]->pict.data[2]);
1902 av_freep(&sub->rects[i]->pict.data[3]);
1903 av_freep(&sub->rects[i]->text);
1904 av_freep(&sub->rects[i]->ass);
1905 av_freep(&sub->rects[i]);
1908 av_freep(&sub->rects);
1910 memset(sub, 0, sizeof(AVSubtitle));
1913 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
1917 entangled_thread_counter--;
1918 /* Release any user-supplied mutex. */
1919 if (ff_lockmgr_cb) {
1920 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1923 ret = avcodec_close(avctx);
1925 /* If there is a user-supplied mutex locking routine, call it. */
1926 if (ff_lockmgr_cb) {
1927 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1930 entangled_thread_counter++;
1934 av_cold int avcodec_close(AVCodecContext *avctx)
1936 /* If there is a user-supplied mutex locking routine, call it. */
1937 if (ff_lockmgr_cb) {
1938 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1942 entangled_thread_counter++;
1943 if (entangled_thread_counter != 1) {
1944 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1945 entangled_thread_counter--;
1949 if (avcodec_is_open(avctx)) {
1950 if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1951 entangled_thread_counter --;
1952 ff_frame_thread_encoder_free(avctx);
1953 entangled_thread_counter ++;
1955 if (HAVE_THREADS && avctx->thread_opaque)
1956 ff_thread_free(avctx);
1957 if (avctx->codec && avctx->codec->close)
1958 avctx->codec->close(avctx);
1959 avcodec_default_free_buffers(avctx);
1960 avctx->coded_frame = NULL;
1961 avctx->internal->byte_buffer_size = 0;
1962 av_freep(&avctx->internal->byte_buffer);
1963 av_freep(&avctx->internal);
1964 av_dict_free(&avctx->metadata);
1967 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1968 av_opt_free(avctx->priv_data);
1970 av_freep(&avctx->priv_data);
1971 if (av_codec_is_encoder(avctx->codec))
1972 av_freep(&avctx->extradata);
1973 avctx->codec = NULL;
1974 avctx->active_thread_type = 0;
1975 entangled_thread_counter--;
1977 /* Release any user-supplied mutex. */
1978 if (ff_lockmgr_cb) {
1979 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1984 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1987 //This is for future deprecatec codec ids, its empty since
1988 //last major bump but will fill up again over time, please don't remove it
1989 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
1990 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
1991 default : return id;
1995 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1997 AVCodec *p, *experimental = NULL;
1999 id= remap_deprecated_codec_id(id);
2001 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2003 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2010 return experimental;
2013 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2015 return find_encdec(id, 1);
2018 AVCodec *avcodec_find_encoder_by_name(const char *name)
2025 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2032 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2034 return find_encdec(id, 0);
2037 AVCodec *avcodec_find_decoder_by_name(const char *name)
2044 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2051 const char *avcodec_get_name(enum AVCodecID id)
2053 const AVCodecDescriptor *cd;
2056 if (id == AV_CODEC_ID_NONE)
2058 cd = avcodec_descriptor_get(id);
2061 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2062 codec = avcodec_find_decoder(id);
2065 codec = avcodec_find_encoder(id);
2068 return "unknown_codec";
2071 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2073 int i, len, ret = 0;
2075 #define IS_PRINT(x) \
2076 (((x) >= '0' && (x) <= '9') || \
2077 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2078 ((x) == '.' || (x) == ' ' || (x) == '-'))
2080 for (i = 0; i < 4; i++) {
2081 len = snprintf(buf, buf_size,
2082 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
2084 buf_size = buf_size > len ? buf_size - len : 0;
2091 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2093 const char *codec_type;
2094 const char *codec_name;
2095 const char *profile = NULL;
2098 AVRational display_aspect_ratio;
2100 if (!buf || buf_size <= 0)
2102 codec_type = av_get_media_type_string(enc->codec_type);
2103 codec_name = avcodec_get_name(enc->codec_id);
2104 if (enc->profile != FF_PROFILE_UNKNOWN) {
2108 p = encode ? avcodec_find_encoder(enc->codec_id) :
2109 avcodec_find_decoder(enc->codec_id);
2111 profile = av_get_profile_name(p, enc->profile);
2114 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2115 codec_name, enc->mb_decision ? " (hq)" : "");
2116 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2118 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2119 if (enc->codec_tag) {
2121 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2122 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2123 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2126 switch (enc->codec_type) {
2127 case AVMEDIA_TYPE_VIDEO:
2128 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2129 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2131 av_get_pix_fmt_name(enc->pix_fmt));
2134 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2136 enc->width, enc->height);
2137 if (enc->sample_aspect_ratio.num) {
2138 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2139 enc->width * enc->sample_aspect_ratio.num,
2140 enc->height * enc->sample_aspect_ratio.den,
2142 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2143 " [SAR %d:%d DAR %d:%d]",
2144 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2145 display_aspect_ratio.num, display_aspect_ratio.den);
2147 if (av_log_get_level() >= AV_LOG_DEBUG) {
2148 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2149 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2151 enc->time_base.num / g, enc->time_base.den / g);
2155 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2156 ", q=%d-%d", enc->qmin, enc->qmax);
2159 case AVMEDIA_TYPE_AUDIO:
2160 if (enc->sample_rate) {
2161 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2162 ", %d Hz", enc->sample_rate);
2164 av_strlcat(buf, ", ", buf_size);
2165 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2166 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2167 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2168 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2175 if (enc->flags & CODEC_FLAG_PASS1)
2176 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2178 if (enc->flags & CODEC_FLAG_PASS2)
2179 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2182 bitrate = get_bit_rate(enc);
2184 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2185 ", %d kb/s", bitrate / 1000);
2189 const char *av_get_profile_name(const AVCodec *codec, int profile)
2192 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2195 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2196 if (p->profile == profile)
2202 unsigned avcodec_version(void)
2204 // av_assert0(AV_CODEC_ID_V410==164);
2205 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2206 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2207 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2208 av_assert0(AV_CODEC_ID_SRT==94216);
2209 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2211 return LIBAVCODEC_VERSION_INT;
2214 const char *avcodec_configuration(void)
2216 return FFMPEG_CONFIGURATION;
2219 const char *avcodec_license(void)
2221 #define LICENSE_PREFIX "libavcodec license: "
2222 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2225 void avcodec_flush_buffers(AVCodecContext *avctx)
2227 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2228 ff_thread_flush(avctx);
2229 else if (avctx->codec->flush)
2230 avctx->codec->flush(avctx);
2232 avctx->pts_correction_last_pts =
2233 avctx->pts_correction_last_dts = INT64_MIN;
2236 static void video_free_buffers(AVCodecContext *s)
2238 AVCodecInternal *avci = s->internal;
2244 if (avci->buffer_count)
2245 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2246 avci->buffer_count);
2247 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
2248 InternalBuffer *buf = &avci->buffer[i];
2249 for (j = 0; j < 4; j++) {
2250 av_freep(&buf->base[j]);
2251 buf->data[j] = NULL;
2254 av_freep(&avci->buffer);
2256 avci->buffer_count = 0;
2259 static void audio_free_buffers(AVCodecContext *avctx)
2261 AVCodecInternal *avci = avctx->internal;
2262 InternalBuffer *buf;
2268 if (buf->extended_data) {
2269 av_free(buf->extended_data[0]);
2270 if (buf->extended_data != buf->data)
2271 av_freep(&buf->extended_data);
2273 av_freep(&avci->buffer);
2276 void avcodec_default_free_buffers(AVCodecContext *avctx)
2278 switch (avctx->codec_type) {
2279 case AVMEDIA_TYPE_VIDEO:
2280 video_free_buffers(avctx);
2282 case AVMEDIA_TYPE_AUDIO:
2283 audio_free_buffers(avctx);
2290 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2293 case AV_CODEC_ID_ADPCM_CT:
2294 case AV_CODEC_ID_ADPCM_IMA_APC:
2295 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2296 case AV_CODEC_ID_ADPCM_IMA_WS:
2297 case AV_CODEC_ID_ADPCM_G722:
2298 case AV_CODEC_ID_ADPCM_YAMAHA:
2300 case AV_CODEC_ID_PCM_ALAW:
2301 case AV_CODEC_ID_PCM_MULAW:
2302 case AV_CODEC_ID_PCM_S8:
2303 case AV_CODEC_ID_PCM_U8:
2304 case AV_CODEC_ID_PCM_ZORK:
2306 case AV_CODEC_ID_PCM_S16BE:
2307 case AV_CODEC_ID_PCM_S16LE:
2308 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2309 case AV_CODEC_ID_PCM_U16BE:
2310 case AV_CODEC_ID_PCM_U16LE:
2312 case AV_CODEC_ID_PCM_S24DAUD:
2313 case AV_CODEC_ID_PCM_S24BE:
2314 case AV_CODEC_ID_PCM_S24LE:
2315 case AV_CODEC_ID_PCM_U24BE:
2316 case AV_CODEC_ID_PCM_U24LE:
2318 case AV_CODEC_ID_PCM_S32BE:
2319 case AV_CODEC_ID_PCM_S32LE:
2320 case AV_CODEC_ID_PCM_U32BE:
2321 case AV_CODEC_ID_PCM_U32LE:
2322 case AV_CODEC_ID_PCM_F32BE:
2323 case AV_CODEC_ID_PCM_F32LE:
2325 case AV_CODEC_ID_PCM_F64BE:
2326 case AV_CODEC_ID_PCM_F64LE:
2333 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2335 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2336 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2337 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2338 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2339 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2340 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2341 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2342 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2343 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2344 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2345 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2347 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2348 return AV_CODEC_ID_NONE;
2349 if (be < 0 || be > 1)
2351 return map[fmt][be];
2354 int av_get_bits_per_sample(enum AVCodecID codec_id)
2357 case AV_CODEC_ID_ADPCM_SBPRO_2:
2359 case AV_CODEC_ID_ADPCM_SBPRO_3:
2361 case AV_CODEC_ID_ADPCM_SBPRO_4:
2362 case AV_CODEC_ID_ADPCM_IMA_WAV:
2363 case AV_CODEC_ID_ADPCM_IMA_QT:
2364 case AV_CODEC_ID_ADPCM_SWF:
2365 case AV_CODEC_ID_ADPCM_MS:
2368 return av_get_exact_bits_per_sample(codec_id);
2372 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2374 int id, sr, ch, ba, tag, bps;
2376 id = avctx->codec_id;
2377 sr = avctx->sample_rate;
2378 ch = avctx->channels;
2379 ba = avctx->block_align;
2380 tag = avctx->codec_tag;
2381 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2383 /* codecs with an exact constant bits per sample */
2384 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2385 return (frame_bytes * 8LL) / (bps * ch);
2386 bps = avctx->bits_per_coded_sample;
2388 /* codecs with a fixed packet duration */
2390 case AV_CODEC_ID_ADPCM_ADX: return 32;
2391 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2392 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2393 case AV_CODEC_ID_AMR_NB:
2394 case AV_CODEC_ID_GSM:
2395 case AV_CODEC_ID_QCELP:
2396 case AV_CODEC_ID_RA_288: return 160;
2397 case AV_CODEC_ID_AMR_WB:
2398 case AV_CODEC_ID_GSM_MS: return 320;
2399 case AV_CODEC_ID_MP1: return 384;
2400 case AV_CODEC_ID_ATRAC1: return 512;
2401 case AV_CODEC_ID_ATRAC3: return 1024;
2402 case AV_CODEC_ID_MP2:
2403 case AV_CODEC_ID_MUSEPACK7: return 1152;
2404 case AV_CODEC_ID_AC3: return 1536;
2408 /* calc from sample rate */
2409 if (id == AV_CODEC_ID_TTA)
2410 return 256 * sr / 245;
2413 /* calc from sample rate and channels */
2414 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2415 return (480 << (sr / 22050)) / ch;
2420 /* calc from block_align */
2421 if (id == AV_CODEC_ID_SIPR) {
2423 case 20: return 160;
2424 case 19: return 144;
2425 case 29: return 288;
2426 case 37: return 480;
2428 } else if (id == AV_CODEC_ID_ILBC) {
2430 case 38: return 160;
2431 case 50: return 240;
2436 if (frame_bytes > 0) {
2437 /* calc from frame_bytes only */
2438 if (id == AV_CODEC_ID_TRUESPEECH)
2439 return 240 * (frame_bytes / 32);
2440 if (id == AV_CODEC_ID_NELLYMOSER)
2441 return 256 * (frame_bytes / 64);
2442 if (id == AV_CODEC_ID_RA_144)
2443 return 160 * (frame_bytes / 20);
2444 if (id == AV_CODEC_ID_G723_1)
2445 return 240 * (frame_bytes / 24);
2448 /* calc from frame_bytes and bits_per_coded_sample */
2449 if (id == AV_CODEC_ID_ADPCM_G726)
2450 return frame_bytes * 8 / bps;
2454 /* calc from frame_bytes and channels */
2456 case AV_CODEC_ID_ADPCM_4XM:
2457 case AV_CODEC_ID_ADPCM_IMA_ISS:
2458 return (frame_bytes - 4 * ch) * 2 / ch;
2459 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2460 return (frame_bytes - 4) * 2 / ch;
2461 case AV_CODEC_ID_ADPCM_IMA_AMV:
2462 return (frame_bytes - 8) * 2 / ch;
2463 case AV_CODEC_ID_ADPCM_XA:
2464 return (frame_bytes / 128) * 224 / ch;
2465 case AV_CODEC_ID_INTERPLAY_DPCM:
2466 return (frame_bytes - 6 - ch) / ch;
2467 case AV_CODEC_ID_ROQ_DPCM:
2468 return (frame_bytes - 8) / ch;
2469 case AV_CODEC_ID_XAN_DPCM:
2470 return (frame_bytes - 2 * ch) / ch;
2471 case AV_CODEC_ID_MACE3:
2472 return 3 * frame_bytes / ch;
2473 case AV_CODEC_ID_MACE6:
2474 return 6 * frame_bytes / ch;
2475 case AV_CODEC_ID_PCM_LXF:
2476 return 2 * (frame_bytes / (5 * ch));
2477 case AV_CODEC_ID_IAC:
2478 case AV_CODEC_ID_IMC:
2479 return 4 * frame_bytes / ch;
2483 /* calc from frame_bytes, channels, and codec_tag */
2484 if (id == AV_CODEC_ID_SOL_DPCM) {
2486 return frame_bytes / ch;
2488 return frame_bytes * 2 / ch;
2493 /* calc from frame_bytes, channels, and block_align */
2494 int blocks = frame_bytes / ba;
2495 switch (avctx->codec_id) {
2496 case AV_CODEC_ID_ADPCM_IMA_WAV:
2497 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2498 case AV_CODEC_ID_ADPCM_IMA_DK3:
2499 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2500 case AV_CODEC_ID_ADPCM_IMA_DK4:
2501 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2502 case AV_CODEC_ID_ADPCM_MS:
2503 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2508 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2509 switch (avctx->codec_id) {
2510 case AV_CODEC_ID_PCM_DVD:
2513 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2514 case AV_CODEC_ID_PCM_BLURAY:
2517 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2518 case AV_CODEC_ID_S302M:
2519 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2529 int ff_thread_init(AVCodecContext *s)
2536 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2550 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2553 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2557 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2559 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2560 "version to the newest one from Git. If the problem still "
2561 "occurs, it means that your file has a feature which has not "
2562 "been implemented.\n", feature);
2564 av_log_ask_for_sample(avc, NULL);
2567 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2569 va_list argument_list;
2571 va_start(argument_list, msg);
2574 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2575 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2576 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2577 "and contact the ffmpeg-devel mailing list.\n");
2579 va_end(argument_list);
2582 static AVHWAccel *first_hwaccel = NULL;
2584 void av_register_hwaccel(AVHWAccel *hwaccel)
2586 AVHWAccel **p = &first_hwaccel;
2590 hwaccel->next = NULL;
2593 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2595 return hwaccel ? hwaccel->next : first_hwaccel;
2598 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2600 AVHWAccel *hwaccel = NULL;
2602 while ((hwaccel = av_hwaccel_next(hwaccel)))
2603 if (hwaccel->id == codec_id
2604 && hwaccel->pix_fmt == pix_fmt)
2609 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2611 if (ff_lockmgr_cb) {
2612 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2614 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2620 if (ff_lockmgr_cb) {
2621 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2623 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2629 int avpriv_lock_avformat(void)
2631 if (ff_lockmgr_cb) {
2632 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2638 int avpriv_unlock_avformat(void)
2640 if (ff_lockmgr_cb) {
2641 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2647 unsigned int avpriv_toupper4(unsigned int x)
2649 return toupper(x & 0xFF)
2650 + (toupper((x >> 8) & 0xFF) << 8)
2651 + (toupper((x >> 16) & 0xFF) << 16)
2652 + (toupper((x >> 24) & 0xFF) << 24);
2657 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2661 ff_init_buffer_info(avctx, f);
2663 return avctx->get_buffer(avctx, f);
2666 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2668 f->owner->release_buffer(f->owner, f);
2671 void ff_thread_finish_setup(AVCodecContext *avctx)
2675 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2679 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2683 int ff_thread_can_start_frame(AVCodecContext *avctx)
2690 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2692 AVCodec *c= avcodec_find_decoder(codec_id);
2694 c= avcodec_find_encoder(codec_id);
2698 if (codec_id <= AV_CODEC_ID_NONE)
2699 return AVMEDIA_TYPE_UNKNOWN;
2700 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2701 return AVMEDIA_TYPE_VIDEO;
2702 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2703 return AVMEDIA_TYPE_AUDIO;
2704 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2705 return AVMEDIA_TYPE_SUBTITLE;
2707 return AVMEDIA_TYPE_UNKNOWN;
2710 int avcodec_is_open(AVCodecContext *s)
2712 return !!s->internal;