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 volatile int ff_avcodec_locked;
51 static int volatile entangled_thread_counter = 0;
52 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
53 static void *codec_mutex;
54 static void *avformat_mutex;
56 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
61 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
63 ptr = av_realloc(ptr, min_size);
64 /* we could set this to the unmodified min_size but this is safer
65 * if the user lost the ptr and uses NULL now
75 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
80 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
82 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
89 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
91 ff_fast_malloc(ptr, size, min_size, 0);
94 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
97 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
102 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
103 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
106 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
109 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
114 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
115 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
118 /* encoder management */
119 static AVCodec *first_avcodec = NULL;
121 AVCodec *av_codec_next(const AVCodec *c)
126 return first_avcodec;
129 static void avcodec_init(void)
131 static int initialized = 0;
133 if (initialized != 0)
137 ff_dsputil_static_init();
140 int av_codec_is_encoder(const AVCodec *codec)
142 return codec && (codec->encode_sub || codec->encode2);
145 int av_codec_is_decoder(const AVCodec *codec)
147 return codec && codec->decode;
150 void avcodec_register(AVCodec *codec)
160 if (codec->init_static_data)
161 codec->init_static_data(codec);
164 unsigned avcodec_get_edge_width(void)
169 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
171 s->coded_width = width;
172 s->coded_height = height;
173 s->width = -((-width ) >> s->lowres);
174 s->height = -((-height) >> s->lowres);
177 #define INTERNAL_BUFFER_SIZE (32 + 1)
179 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
180 int linesize_align[AV_NUM_DATA_POINTERS])
186 switch (s->pix_fmt) {
187 case AV_PIX_FMT_YUV420P:
188 case AV_PIX_FMT_YUYV422:
189 case AV_PIX_FMT_UYVY422:
190 case AV_PIX_FMT_YUV422P:
191 case AV_PIX_FMT_YUV440P:
192 case AV_PIX_FMT_YUV444P:
193 case AV_PIX_FMT_GBRP:
194 case AV_PIX_FMT_GRAY8:
195 case AV_PIX_FMT_GRAY16BE:
196 case AV_PIX_FMT_GRAY16LE:
197 case AV_PIX_FMT_YUVJ420P:
198 case AV_PIX_FMT_YUVJ422P:
199 case AV_PIX_FMT_YUVJ440P:
200 case AV_PIX_FMT_YUVJ444P:
201 case AV_PIX_FMT_YUVA420P:
202 case AV_PIX_FMT_YUVA422P:
203 case AV_PIX_FMT_YUVA444P:
204 case AV_PIX_FMT_YUV420P9LE:
205 case AV_PIX_FMT_YUV420P9BE:
206 case AV_PIX_FMT_YUV420P10LE:
207 case AV_PIX_FMT_YUV420P10BE:
208 case AV_PIX_FMT_YUV420P12LE:
209 case AV_PIX_FMT_YUV420P12BE:
210 case AV_PIX_FMT_YUV420P14LE:
211 case AV_PIX_FMT_YUV420P14BE:
212 case AV_PIX_FMT_YUV422P9LE:
213 case AV_PIX_FMT_YUV422P9BE:
214 case AV_PIX_FMT_YUV422P10LE:
215 case AV_PIX_FMT_YUV422P10BE:
216 case AV_PIX_FMT_YUV422P12LE:
217 case AV_PIX_FMT_YUV422P12BE:
218 case AV_PIX_FMT_YUV422P14LE:
219 case AV_PIX_FMT_YUV422P14BE:
220 case AV_PIX_FMT_YUV444P9LE:
221 case AV_PIX_FMT_YUV444P9BE:
222 case AV_PIX_FMT_YUV444P10LE:
223 case AV_PIX_FMT_YUV444P10BE:
224 case AV_PIX_FMT_YUV444P12LE:
225 case AV_PIX_FMT_YUV444P12BE:
226 case AV_PIX_FMT_YUV444P14LE:
227 case AV_PIX_FMT_YUV444P14BE:
228 case AV_PIX_FMT_GBRP9LE:
229 case AV_PIX_FMT_GBRP9BE:
230 case AV_PIX_FMT_GBRP10LE:
231 case AV_PIX_FMT_GBRP10BE:
232 case AV_PIX_FMT_GBRP12LE:
233 case AV_PIX_FMT_GBRP12BE:
234 case AV_PIX_FMT_GBRP14LE:
235 case AV_PIX_FMT_GBRP14BE:
236 w_align = 16; //FIXME assume 16 pixel per macroblock
237 h_align = 16 * 2; // interlaced needs 2 macroblocks height
239 case AV_PIX_FMT_YUV411P:
240 case AV_PIX_FMT_UYYVYY411:
244 case AV_PIX_FMT_YUV410P:
245 if (s->codec_id == AV_CODEC_ID_SVQ1) {
250 case AV_PIX_FMT_RGB555:
251 if (s->codec_id == AV_CODEC_ID_RPZA) {
256 case AV_PIX_FMT_PAL8:
257 case AV_PIX_FMT_BGR8:
258 case AV_PIX_FMT_RGB8:
259 if (s->codec_id == AV_CODEC_ID_SMC) {
264 case AV_PIX_FMT_BGR24:
265 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
266 (s->codec_id == AV_CODEC_ID_ZLIB)) {
277 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
278 w_align = FFMAX(w_align, 8);
281 *width = FFALIGN(*width, w_align);
282 *height = FFALIGN(*height, h_align);
283 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
284 // some of the optimized chroma MC reads one line too much
285 // which is also done in mpeg decoders with lowres > 0
288 for (i = 0; i < 4; i++)
289 linesize_align[i] = STRIDE_ALIGN;
292 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
294 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
295 int chroma_shift = desc->log2_chroma_w;
296 int linesize_align[AV_NUM_DATA_POINTERS];
299 avcodec_align_dimensions2(s, width, height, linesize_align);
300 align = FFMAX(linesize_align[0], linesize_align[3]);
301 linesize_align[1] <<= chroma_shift;
302 linesize_align[2] <<= chroma_shift;
303 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
304 *width = FFALIGN(*width, align);
307 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
308 enum AVSampleFormat sample_fmt, const uint8_t *buf,
309 int buf_size, int align)
311 int ch, planar, needed_size, ret = 0;
313 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
314 frame->nb_samples, sample_fmt,
316 if (buf_size < needed_size)
317 return AVERROR(EINVAL);
319 planar = av_sample_fmt_is_planar(sample_fmt);
320 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
321 if (!(frame->extended_data = av_mallocz(nb_channels *
322 sizeof(*frame->extended_data))))
323 return AVERROR(ENOMEM);
325 frame->extended_data = frame->data;
328 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
329 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
330 sample_fmt, align)) < 0) {
331 if (frame->extended_data != frame->data)
332 av_freep(&frame->extended_data);
335 if (frame->extended_data != frame->data) {
336 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
337 frame->data[ch] = frame->extended_data[ch];
343 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
345 AVCodecInternal *avci = avctx->internal;
348 av_freep(&avci->audio_data);
349 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
350 frame->nb_samples, avctx->sample_fmt,
353 return AVERROR(EINVAL);
355 frame->data[0] = av_mallocz(buf_size);
357 return AVERROR(ENOMEM);
359 ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
360 frame->data[0], buf_size, 0);
362 av_freep(&frame->data[0]);
366 avci->audio_data = frame->data[0];
367 if (avctx->debug & FF_DEBUG_BUFFERS)
368 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
369 "internal audio buffer used\n", frame);
374 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
380 AVCodecInternal *avci = s->internal;
382 if (pic->data[0] != NULL) {
383 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
386 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
387 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
391 if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
392 av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
397 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
398 sizeof(InternalBuffer));
401 buf = &avci->buffer[avci->buffer_count];
403 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
404 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
405 av_freep(&buf->base[i]);
411 int h_chroma_shift, v_chroma_shift;
416 int stride_align[AV_NUM_DATA_POINTERS];
417 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
418 const int pixel_size = desc->comp[0].step_minus1 + 1;
420 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
423 avcodec_align_dimensions2(s, &w, &h, stride_align);
425 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
431 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
432 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
433 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
434 // increase alignment of w for next try (rhs gives the lowest bit set in w)
438 for (i = 0; i < 4; i++)
439 unaligned |= picture.linesize[i] % stride_align[i];
442 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
446 for (i = 0; i < 3 && picture.data[i + 1]; i++)
447 size[i] = picture.data[i + 1] - picture.data[i];
448 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
450 memset(buf->base, 0, sizeof(buf->base));
451 memset(buf->data, 0, sizeof(buf->data));
453 for (i = 0; i < 4 && size[i]; i++) {
454 const int h_shift = i == 0 ? 0 : h_chroma_shift;
455 const int v_shift = i == 0 ? 0 : v_chroma_shift;
457 buf->linesize[i] = picture.linesize[i];
459 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
460 if (buf->base[i] == NULL)
461 return AVERROR(ENOMEM);
462 memset(buf->base[i], 128, size[i]);
464 // no edge if EDGE EMU or not planar YUV
465 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
466 buf->data[i] = buf->base[i];
468 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
470 for (; i < AV_NUM_DATA_POINTERS; i++) {
471 buf->base[i] = buf->data[i] = NULL;
472 buf->linesize[i] = 0;
474 if (size[1] && !size[2])
475 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
476 buf->width = s->width;
477 buf->height = s->height;
478 buf->pix_fmt = s->pix_fmt;
481 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
482 pic->base[i] = buf->base[i];
483 pic->data[i] = buf->data[i];
484 pic->linesize[i] = buf->linesize[i];
486 pic->extended_data = pic->data;
487 avci->buffer_count++;
489 if (s->debug & FF_DEBUG_BUFFERS)
490 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
491 "buffers used\n", pic, avci->buffer_count);
496 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
498 frame->type = FF_BUFFER_TYPE_INTERNAL;
499 switch (avctx->codec_type) {
500 case AVMEDIA_TYPE_VIDEO:
501 return video_get_buffer(avctx, frame);
502 case AVMEDIA_TYPE_AUDIO:
503 return audio_get_buffer(avctx, frame);
509 void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
512 frame->pkt_pts = s->pkt->pts;
513 frame->pkt_pos = s->pkt->pos;
514 frame->pkt_duration = s->pkt->duration;
516 frame->pkt_pts = AV_NOPTS_VALUE;
518 frame->pkt_duration = 0;
520 frame->reordered_opaque = s->reordered_opaque;
522 switch (s->codec->type) {
523 case AVMEDIA_TYPE_VIDEO:
524 frame->width = s->width;
525 frame->height = s->height;
526 frame->format = s->pix_fmt;
527 frame->sample_aspect_ratio = s->sample_aspect_ratio;
529 case AVMEDIA_TYPE_AUDIO:
530 frame->sample_rate = s->sample_rate;
531 frame->format = s->sample_fmt;
532 frame->channel_layout = s->channel_layout;
533 frame->channels = s->channels;
538 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
540 ff_init_buffer_info(avctx, frame);
542 return avctx->get_buffer(avctx, frame);
545 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
548 InternalBuffer *buf, *last;
549 AVCodecInternal *avci = s->internal;
551 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
553 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
554 assert(avci->buffer_count);
557 buf = NULL; /* avoids warning */
558 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
559 buf = &avci->buffer[i];
560 if (buf->data[0] == pic->data[0])
563 av_assert0(i < avci->buffer_count);
564 avci->buffer_count--;
565 last = &avci->buffer[avci->buffer_count];
568 FFSWAP(InternalBuffer, *buf, *last);
571 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
573 // pic->base[i]=NULL;
575 if (s->debug & FF_DEBUG_BUFFERS)
576 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
577 "buffers used\n", pic, avci->buffer_count);
580 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
585 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
587 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
588 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
589 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
590 s->release_buffer(s, pic);
593 ff_init_buffer_info(s, pic);
595 /* If no picture return a new buffer */
596 if (pic->data[0] == NULL) {
597 /* We will copy from buffer, so must be readable */
598 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
599 return ff_get_buffer(s, pic);
602 assert(s->pix_fmt == pic->format);
604 /* If internal buffer type return the same buffer */
605 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
610 * Not internal type and reget_buffer not overridden, emulate cr buffer
613 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
614 pic->data[i] = pic->base[i] = NULL;
616 /* Allocate new frame */
617 if ((ret = ff_get_buffer(s, pic)))
619 /* Copy image data from old buffer to new buffer */
620 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
622 s->release_buffer(s, &temp_pic); // Release old frame
626 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
630 for (i = 0; i < count; i++) {
631 int r = func(c, (char *)arg + i * size);
638 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
642 for (i = 0; i < count; i++) {
643 int r = func(c, arg, i, 0);
650 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
652 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
653 return desc->flags & PIX_FMT_HWACCEL;
656 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
658 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
663 void avcodec_get_frame_defaults(AVFrame *frame)
665 #if LIBAVCODEC_VERSION_MAJOR >= 55
666 // extended_data should explicitly be freed when needed, this code is unsafe currently
667 // also this is not compatible to the <55 ABI/API
668 if (frame->extended_data != frame->data && 0)
669 av_freep(&frame->extended_data);
672 memset(frame, 0, sizeof(AVFrame));
677 frame->best_effort_timestamp = AV_NOPTS_VALUE;
678 frame->pkt_duration = 0;
680 frame->key_frame = 1;
681 frame->sample_aspect_ratio = (AVRational) {0, 1 };
682 frame->format = -1; /* unknown */
683 frame->extended_data = frame->data;
686 AVFrame *avcodec_alloc_frame(void)
688 AVFrame *frame = av_malloc(sizeof(AVFrame));
693 frame->extended_data = NULL;
694 avcodec_get_frame_defaults(frame);
699 void avcodec_free_frame(AVFrame **frame)
703 if (!frame || !*frame)
708 if (f->extended_data != f->data)
709 av_freep(&f->extended_data);
714 #define MAKE_ACCESSORS(str, name, type, field) \
715 type av_##name##_get_##field(const str *s) { return s->field; } \
716 void av_##name##_set_##field(str *s, type v) { s->field = v; }
718 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
719 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
720 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
721 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
722 MAKE_ACCESSORS(AVFrame, frame, int, channels)
723 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
724 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
725 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
727 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
728 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
730 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
732 memset(sub, 0, sizeof(*sub));
733 sub->pts = AV_NOPTS_VALUE;
736 static int get_bit_rate(AVCodecContext *ctx)
741 switch (ctx->codec_type) {
742 case AVMEDIA_TYPE_VIDEO:
743 case AVMEDIA_TYPE_DATA:
744 case AVMEDIA_TYPE_SUBTITLE:
745 case AVMEDIA_TYPE_ATTACHMENT:
746 bit_rate = ctx->bit_rate;
748 case AVMEDIA_TYPE_AUDIO:
749 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
750 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
759 #if FF_API_AVCODEC_OPEN
760 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
762 return avcodec_open2(avctx, codec, NULL);
766 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
772 ret = avcodec_open2(avctx, codec, options);
774 ff_lock_avcodec(avctx);
778 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
781 AVDictionary *tmp = NULL;
783 if (avcodec_is_open(avctx))
786 if ((!codec && !avctx->codec)) {
787 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
788 return AVERROR(EINVAL);
790 if ((codec && avctx->codec && codec != avctx->codec)) {
791 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
792 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
793 return AVERROR(EINVAL);
796 codec = avctx->codec;
798 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
799 return AVERROR(EINVAL);
802 av_dict_copy(&tmp, *options, 0);
804 ret = ff_lock_avcodec(avctx);
808 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
809 if (!avctx->internal) {
810 ret = AVERROR(ENOMEM);
814 if (codec->priv_data_size > 0) {
815 if (!avctx->priv_data) {
816 avctx->priv_data = av_mallocz(codec->priv_data_size);
817 if (!avctx->priv_data) {
818 ret = AVERROR(ENOMEM);
821 if (codec->priv_class) {
822 *(const AVClass **)avctx->priv_data = codec->priv_class;
823 av_opt_set_defaults(avctx->priv_data);
826 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
829 avctx->priv_data = NULL;
831 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
834 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
835 if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
837 if (avctx->coded_width && avctx->coded_height)
838 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
839 else if (avctx->width && avctx->height)
840 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
843 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
844 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
845 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
846 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
847 avcodec_set_dimensions(avctx, 0, 0);
850 /* if the decoder init function was already called previously,
851 * free the already allocated subtitle_header before overwriting it */
852 if (av_codec_is_decoder(codec))
853 av_freep(&avctx->subtitle_header);
855 if (avctx->channels > FF_SANE_NB_CHANNELS) {
856 ret = AVERROR(EINVAL);
860 avctx->codec = codec;
861 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
862 avctx->codec_id == AV_CODEC_ID_NONE) {
863 avctx->codec_type = codec->type;
864 avctx->codec_id = codec->id;
866 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
867 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
868 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
869 ret = AVERROR(EINVAL);
872 avctx->frame_number = 0;
873 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
875 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
876 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
877 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
879 av_log(NULL, AV_LOG_ERROR,
880 "The %s '%s' is experimental but experimental codecs are not enabled, "
881 "add '-strict %d' if you want to use it.\n",
882 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
883 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
884 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
885 av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
886 codec_string, codec2->name);
887 ret = AVERROR_EXPERIMENTAL;
891 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
892 (!avctx->time_base.num || !avctx->time_base.den)) {
893 avctx->time_base.num = 1;
894 avctx->time_base.den = avctx->sample_rate;
898 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
901 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
902 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
903 ff_lock_avcodec(avctx);
908 if (HAVE_THREADS && !avctx->thread_opaque
909 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
910 ret = ff_thread_init(avctx);
915 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
916 avctx->thread_count = 1;
918 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
919 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
920 avctx->codec->max_lowres);
921 ret = AVERROR(EINVAL);
925 if (av_codec_is_encoder(avctx->codec)) {
927 if (avctx->codec->sample_fmts) {
928 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
929 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
931 if (avctx->channels == 1 &&
932 av_get_planar_sample_fmt(avctx->sample_fmt) ==
933 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
934 avctx->sample_fmt = avctx->codec->sample_fmts[i];
938 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
940 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
941 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
942 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
943 ret = AVERROR(EINVAL);
947 if (avctx->codec->pix_fmts) {
948 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
949 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
951 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
952 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
953 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
955 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
956 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
957 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
958 ret = AVERROR(EINVAL);
962 if (avctx->codec->supported_samplerates) {
963 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
964 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
966 if (avctx->codec->supported_samplerates[i] == 0) {
967 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
969 ret = AVERROR(EINVAL);
973 if (avctx->codec->channel_layouts) {
974 if (!avctx->channel_layout) {
975 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
977 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
978 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
980 if (avctx->codec->channel_layouts[i] == 0) {
982 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
983 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
984 ret = AVERROR(EINVAL);
989 if (avctx->channel_layout && avctx->channels) {
990 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
991 if (channels != avctx->channels) {
993 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
994 av_log(avctx, AV_LOG_ERROR,
995 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
996 buf, channels, avctx->channels);
997 ret = AVERROR(EINVAL);
1000 } else if (avctx->channel_layout) {
1001 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1005 avctx->pts_correction_num_faulty_pts =
1006 avctx->pts_correction_num_faulty_dts = 0;
1007 avctx->pts_correction_last_pts =
1008 avctx->pts_correction_last_dts = INT64_MIN;
1010 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1011 || avctx->internal->frame_thread_encoder)) {
1012 ret = avctx->codec->init(avctx);
1020 if (av_codec_is_decoder(avctx->codec)) {
1021 if (!avctx->bit_rate)
1022 avctx->bit_rate = get_bit_rate(avctx);
1023 /* validate channel layout from the decoder */
1024 if (avctx->channel_layout) {
1025 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1026 if (!avctx->channels)
1027 avctx->channels = channels;
1028 else if (channels != avctx->channels) {
1030 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1031 av_log(avctx, AV_LOG_WARNING,
1032 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1033 "ignoring specified channel layout\n",
1034 buf, channels, avctx->channels);
1035 avctx->channel_layout = 0;
1038 if (avctx->channels && avctx->channels < 0 ||
1039 avctx->channels > FF_SANE_NB_CHANNELS) {
1040 ret = AVERROR(EINVAL);
1045 ff_unlock_avcodec();
1047 av_dict_free(options);
1054 av_freep(&avctx->priv_data);
1055 av_freep(&avctx->internal);
1056 avctx->codec = NULL;
1060 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1062 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1063 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1064 return AVERROR(EINVAL);
1068 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1069 if (!avpkt->data || avpkt->size < size) {
1070 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1071 avpkt->data = avctx->internal->byte_buffer;
1072 avpkt->size = avctx->internal->byte_buffer_size;
1073 avpkt->destruct = NULL;
1078 void *destruct = avpkt->destruct;
1080 if (avpkt->size < size) {
1081 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1082 return AVERROR(EINVAL);
1085 av_init_packet(avpkt);
1086 avpkt->destruct = destruct;
1090 int ret = av_new_packet(avpkt, size);
1092 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1097 int ff_alloc_packet(AVPacket *avpkt, int size)
1099 return ff_alloc_packet2(NULL, avpkt, size);
1103 * Pad last frame with silence.
1105 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1107 AVFrame *frame = NULL;
1108 uint8_t *buf = NULL;
1111 if (!(frame = avcodec_alloc_frame()))
1112 return AVERROR(ENOMEM);
1115 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1116 s->frame_size, s->sample_fmt, 0)) < 0)
1119 if (!(buf = av_malloc(ret))) {
1120 ret = AVERROR(ENOMEM);
1124 frame->nb_samples = s->frame_size;
1125 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1128 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1129 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1131 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1132 frame->nb_samples - src->nb_samples,
1133 s->channels, s->sample_fmt)) < 0)
1141 if (frame->extended_data != frame->data)
1142 av_freep(&frame->extended_data);
1148 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1150 const AVFrame *frame,
1151 int *got_packet_ptr)
1154 AVFrame *padded_frame = NULL;
1156 AVPacket user_pkt = *avpkt;
1157 int needs_realloc = !user_pkt.data;
1159 *got_packet_ptr = 0;
1161 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1162 av_free_packet(avpkt);
1163 av_init_packet(avpkt);
1167 /* ensure that extended_data is properly set */
1168 if (frame && !frame->extended_data) {
1169 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1170 avctx->channels > AV_NUM_DATA_POINTERS) {
1171 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1172 "with more than %d channels, but extended_data is not set.\n",
1173 AV_NUM_DATA_POINTERS);
1174 return AVERROR(EINVAL);
1176 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1179 tmp.extended_data = tmp.data;
1183 /* check for valid frame size */
1185 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1186 if (frame->nb_samples > avctx->frame_size) {
1187 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1188 return AVERROR(EINVAL);
1190 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1191 if (frame->nb_samples < avctx->frame_size &&
1192 !avctx->internal->last_audio_frame) {
1193 ret = pad_last_frame(avctx, &padded_frame, frame);
1197 frame = padded_frame;
1198 avctx->internal->last_audio_frame = 1;
1201 if (frame->nb_samples != avctx->frame_size) {
1202 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1203 ret = AVERROR(EINVAL);
1209 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1211 if (*got_packet_ptr) {
1212 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1213 if (avpkt->pts == AV_NOPTS_VALUE)
1214 avpkt->pts = frame->pts;
1215 if (!avpkt->duration)
1216 avpkt->duration = ff_samples_to_time_base(avctx,
1219 avpkt->dts = avpkt->pts;
1224 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1226 if (user_pkt.data) {
1227 if (user_pkt.size >= avpkt->size) {
1228 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1230 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1231 avpkt->size = user_pkt.size;
1234 avpkt->data = user_pkt.data;
1235 avpkt->destruct = user_pkt.destruct;
1237 if (av_dup_packet(avpkt) < 0) {
1238 ret = AVERROR(ENOMEM);
1244 if (needs_realloc && avpkt->data) {
1245 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1247 avpkt->data = new_data;
1250 avctx->frame_number++;
1253 if (ret < 0 || !*got_packet_ptr) {
1254 av_free_packet(avpkt);
1255 av_init_packet(avpkt);
1259 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1260 * this needs to be moved to the encoders, but for now we can do it
1261 * here to simplify things */
1262 avpkt->flags |= AV_PKT_FLAG_KEY;
1266 av_freep(&padded_frame->data[0]);
1267 if (padded_frame->extended_data != padded_frame->data)
1268 av_freep(&padded_frame->extended_data);
1269 av_freep(&padded_frame);
1275 #if FF_API_OLD_ENCODE_AUDIO
1276 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1277 uint8_t *buf, int buf_size,
1278 const short *samples)
1281 AVFrame frame0 = { 0 };
1283 int ret, samples_size, got_packet;
1285 av_init_packet(&pkt);
1287 pkt.size = buf_size;
1291 avcodec_get_frame_defaults(frame);
1293 if (avctx->frame_size) {
1294 frame->nb_samples = avctx->frame_size;
1296 /* if frame_size is not set, the number of samples must be
1297 * calculated from the buffer size */
1299 if (!av_get_bits_per_sample(avctx->codec_id)) {
1300 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1301 "support this codec\n");
1302 return AVERROR(EINVAL);
1304 nb_samples = (int64_t)buf_size * 8 /
1305 (av_get_bits_per_sample(avctx->codec_id) *
1307 if (nb_samples >= INT_MAX)
1308 return AVERROR(EINVAL);
1309 frame->nb_samples = nb_samples;
1312 /* it is assumed that the samples buffer is large enough based on the
1313 * relevant parameters */
1314 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1316 avctx->sample_fmt, 1);
1317 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1319 (const uint8_t *)samples,
1320 samples_size, 1)) < 0)
1323 /* fabricate frame pts from sample count.
1324 * this is needed because the avcodec_encode_audio() API does not have
1325 * a way for the user to provide pts */
1326 if (avctx->sample_rate && avctx->time_base.num)
1327 frame->pts = ff_samples_to_time_base(avctx,
1328 avctx->internal->sample_count);
1330 frame->pts = AV_NOPTS_VALUE;
1331 avctx->internal->sample_count += frame->nb_samples;
1337 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1338 if (!ret && got_packet && avctx->coded_frame) {
1339 avctx->coded_frame->pts = pkt.pts;
1340 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1342 /* free any side data since we cannot return it */
1343 ff_packet_free_side_data(&pkt);
1345 if (frame && frame->extended_data != frame->data)
1346 av_freep(&frame->extended_data);
1348 return ret ? ret : pkt.size;
1353 #if FF_API_OLD_ENCODE_VIDEO
1354 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1355 const AVFrame *pict)
1358 int ret, got_packet = 0;
1360 if (buf_size < FF_MIN_BUFFER_SIZE) {
1361 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1365 av_init_packet(&pkt);
1367 pkt.size = buf_size;
1369 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1370 if (!ret && got_packet && avctx->coded_frame) {
1371 avctx->coded_frame->pts = pkt.pts;
1372 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1375 /* free any side data since we cannot return it */
1376 if (pkt.side_data_elems > 0) {
1378 for (i = 0; i < pkt.side_data_elems; i++)
1379 av_free(pkt.side_data[i].data);
1380 av_freep(&pkt.side_data);
1381 pkt.side_data_elems = 0;
1384 return ret ? ret : pkt.size;
1389 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1391 const AVFrame *frame,
1392 int *got_packet_ptr)
1395 AVPacket user_pkt = *avpkt;
1396 int needs_realloc = !user_pkt.data;
1398 *got_packet_ptr = 0;
1400 if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1401 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1403 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1404 avctx->stats_out[0] = '\0';
1406 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1407 av_free_packet(avpkt);
1408 av_init_packet(avpkt);
1413 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1414 return AVERROR(EINVAL);
1416 av_assert0(avctx->codec->encode2);
1418 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1419 av_assert0(ret <= 0);
1421 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1423 if (user_pkt.data) {
1424 if (user_pkt.size >= avpkt->size) {
1425 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1427 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1428 avpkt->size = user_pkt.size;
1431 avpkt->data = user_pkt.data;
1432 avpkt->destruct = user_pkt.destruct;
1434 if (av_dup_packet(avpkt) < 0) {
1435 ret = AVERROR(ENOMEM);
1441 if (!*got_packet_ptr)
1443 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1444 avpkt->pts = avpkt->dts = frame->pts;
1446 if (needs_realloc && avpkt->data &&
1447 avpkt->destruct == av_destruct_packet) {
1448 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1450 avpkt->data = new_data;
1453 avctx->frame_number++;
1456 if (ret < 0 || !*got_packet_ptr)
1457 av_free_packet(avpkt);
1463 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1464 const AVSubtitle *sub)
1467 if (sub->start_display_time) {
1468 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1472 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1473 avctx->frame_number++;
1478 * Attempt to guess proper monotonic timestamps for decoded video frames
1479 * which might have incorrect times. Input timestamps may wrap around, in
1480 * which case the output will as well.
1482 * @param pts the pts field of the decoded AVPacket, as passed through
1484 * @param dts the dts field of the decoded AVPacket
1485 * @return one of the input values, may be AV_NOPTS_VALUE
1487 static int64_t guess_correct_pts(AVCodecContext *ctx,
1488 int64_t reordered_pts, int64_t dts)
1490 int64_t pts = AV_NOPTS_VALUE;
1492 if (dts != AV_NOPTS_VALUE) {
1493 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1494 ctx->pts_correction_last_dts = dts;
1496 if (reordered_pts != AV_NOPTS_VALUE) {
1497 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1498 ctx->pts_correction_last_pts = reordered_pts;
1500 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1501 && reordered_pts != AV_NOPTS_VALUE)
1502 pts = reordered_pts;
1509 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1512 const uint8_t *data;
1515 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1518 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1519 if (!data || size < 4)
1521 flags = bytestream_get_le32(&data);
1523 if (size < 4) /* Required for any of the changes */
1525 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1526 avctx->channels = bytestream_get_le32(&data);
1529 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1532 avctx->channel_layout = bytestream_get_le64(&data);
1537 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1538 avctx->sample_rate = bytestream_get_le32(&data);
1541 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1544 avctx->width = bytestream_get_le32(&data);
1545 avctx->height = bytestream_get_le32(&data);
1546 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1551 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1554 const uint8_t *side_metadata;
1557 av_dict_free(&avctx->metadata);
1558 side_metadata = av_packet_get_side_data(avctx->pkt,
1559 AV_PKT_DATA_STRINGS_METADATA, &size);
1562 end = side_metadata + size;
1563 while (side_metadata < end) {
1564 const uint8_t *key = side_metadata;
1565 const uint8_t *val = side_metadata + strlen(key) + 1;
1566 int ret = av_dict_set(&frame->metadata, key, val, 0);
1569 side_metadata = val + strlen(val) + 1;
1572 avctx->metadata = frame->metadata;
1576 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1577 int *got_picture_ptr,
1578 const AVPacket *avpkt)
1581 // copy to ensure we do not change avpkt
1582 AVPacket tmp = *avpkt;
1584 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1585 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1586 return AVERROR(EINVAL);
1589 *got_picture_ptr = 0;
1590 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1591 return AVERROR(EINVAL);
1593 avcodec_get_frame_defaults(picture);
1595 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1596 int did_split = av_packet_split_side_data(&tmp);
1597 apply_param_change(avctx, &tmp);
1599 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1600 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1603 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1605 picture->pkt_dts = avpkt->dts;
1607 if(!avctx->has_b_frames){
1608 picture->pkt_pos = avpkt->pos;
1610 //FIXME these should be under if(!avctx->has_b_frames)
1611 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1612 if (!picture->width) picture->width = avctx->width;
1613 if (!picture->height) picture->height = avctx->height;
1614 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1616 add_metadata_from_side_data(avctx, picture);
1618 emms_c(); //needed to avoid an emms_c() call before every return;
1622 ff_packet_free_side_data(&tmp);
1627 if (*got_picture_ptr){
1628 avctx->frame_number++;
1629 picture->best_effort_timestamp = guess_correct_pts(avctx,
1636 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1637 * make sure it's set correctly */
1638 picture->extended_data = picture->data;
1643 #if FF_API_OLD_DECODE_AUDIO
1644 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1645 int *frame_size_ptr,
1648 AVFrame frame = {0};
1649 int ret, got_frame = 0;
1651 if (avctx->get_buffer != avcodec_default_get_buffer) {
1652 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1653 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1654 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1655 "avcodec_decode_audio4()\n");
1656 avctx->get_buffer = avcodec_default_get_buffer;
1657 avctx->release_buffer = avcodec_default_release_buffer;
1660 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1662 if (ret >= 0 && got_frame) {
1664 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1665 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1667 avctx->sample_fmt, 1);
1668 if (*frame_size_ptr < data_size) {
1669 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1670 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1671 return AVERROR(EINVAL);
1674 memcpy(samples, frame.extended_data[0], plane_size);
1676 if (planar && avctx->channels > 1) {
1677 uint8_t *out = ((uint8_t *)samples) + plane_size;
1678 for (ch = 1; ch < avctx->channels; ch++) {
1679 memcpy(out, frame.extended_data[ch], plane_size);
1683 *frame_size_ptr = data_size;
1685 *frame_size_ptr = 0;
1692 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1695 const AVPacket *avpkt)
1697 int planar, channels;
1702 if (!avpkt->data && avpkt->size) {
1703 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1704 return AVERROR(EINVAL);
1706 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1707 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1708 return AVERROR(EINVAL);
1711 avcodec_get_frame_defaults(frame);
1713 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1716 // copy to ensure we do not change avpkt
1717 AVPacket tmp = *avpkt;
1718 int did_split = av_packet_split_side_data(&tmp);
1719 apply_param_change(avctx, &tmp);
1722 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1723 if (ret >= 0 && *got_frame_ptr) {
1724 avctx->frame_number++;
1725 frame->pkt_dts = avpkt->dts;
1726 frame->best_effort_timestamp = guess_correct_pts(avctx,
1729 if (frame->format == AV_SAMPLE_FMT_NONE)
1730 frame->format = avctx->sample_fmt;
1731 if (!frame->channel_layout)
1732 frame->channel_layout = avctx->channel_layout;
1733 if (!frame->channels)
1734 frame->channels = avctx->channels;
1735 if (!frame->sample_rate)
1736 frame->sample_rate = avctx->sample_rate;
1738 add_metadata_from_side_data(avctx, frame);
1740 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1741 if(side && side_size>=10) {
1742 avctx->internal->skip_samples = AV_RL32(side);
1743 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1744 avctx->internal->skip_samples);
1746 if (avctx->internal->skip_samples) {
1747 if(frame->nb_samples <= avctx->internal->skip_samples){
1749 avctx->internal->skip_samples -= frame->nb_samples;
1750 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1751 avctx->internal->skip_samples);
1753 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1754 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1755 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1756 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1757 (AVRational){1, avctx->sample_rate},
1758 avctx->pkt_timebase);
1759 if(frame->pkt_pts!=AV_NOPTS_VALUE)
1760 frame->pkt_pts += diff_ts;
1761 if(frame->pkt_dts!=AV_NOPTS_VALUE)
1762 frame->pkt_dts += diff_ts;
1763 if (frame->pkt_duration >= diff_ts)
1764 frame->pkt_duration -= diff_ts;
1766 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1768 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1769 avctx->internal->skip_samples, frame->nb_samples);
1770 frame->nb_samples -= avctx->internal->skip_samples;
1771 avctx->internal->skip_samples = 0;
1777 ff_packet_free_side_data(&tmp);
1783 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1784 * make sure it's set correctly; assume decoders that actually use
1785 * extended_data are doing it correctly */
1786 if (*got_frame_ptr) {
1787 planar = av_sample_fmt_is_planar(frame->format);
1788 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1789 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1790 frame->extended_data = frame->data;
1792 frame->extended_data = NULL;
1798 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1804 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1805 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1806 return AVERROR(EINVAL);
1810 avcodec_get_subtitle_defaults(sub);
1813 AVPacket tmp = *avpkt;
1814 int did_split = av_packet_split_side_data(&tmp);
1815 //apply_param_change(avctx, &tmp);
1819 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
1820 sub->pts = av_rescale_q(avpkt->pts,
1821 avctx->pkt_timebase, AV_TIME_BASE_Q);
1822 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &tmp);
1826 ff_packet_free_side_data(&tmp);
1832 avctx->frame_number++;
1838 void avsubtitle_free(AVSubtitle *sub)
1842 for (i = 0; i < sub->num_rects; i++) {
1843 av_freep(&sub->rects[i]->pict.data[0]);
1844 av_freep(&sub->rects[i]->pict.data[1]);
1845 av_freep(&sub->rects[i]->pict.data[2]);
1846 av_freep(&sub->rects[i]->pict.data[3]);
1847 av_freep(&sub->rects[i]->text);
1848 av_freep(&sub->rects[i]->ass);
1849 av_freep(&sub->rects[i]);
1852 av_freep(&sub->rects);
1854 memset(sub, 0, sizeof(AVSubtitle));
1857 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
1861 ff_unlock_avcodec();
1863 ret = avcodec_close(avctx);
1865 ff_lock_avcodec(NULL);
1869 av_cold int avcodec_close(AVCodecContext *avctx)
1871 int ret = ff_lock_avcodec(avctx);
1875 if (avcodec_is_open(avctx)) {
1876 if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1877 ff_unlock_avcodec();
1878 ff_frame_thread_encoder_free(avctx);
1879 ff_lock_avcodec(avctx);
1881 if (HAVE_THREADS && avctx->thread_opaque)
1882 ff_thread_free(avctx);
1883 if (avctx->codec && avctx->codec->close)
1884 avctx->codec->close(avctx);
1885 avcodec_default_free_buffers(avctx);
1886 avctx->coded_frame = NULL;
1887 avctx->internal->byte_buffer_size = 0;
1888 av_freep(&avctx->internal->byte_buffer);
1889 av_freep(&avctx->internal);
1890 av_dict_free(&avctx->metadata);
1893 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1894 av_opt_free(avctx->priv_data);
1896 av_freep(&avctx->priv_data);
1897 if (av_codec_is_encoder(avctx->codec))
1898 av_freep(&avctx->extradata);
1899 avctx->codec = NULL;
1900 avctx->active_thread_type = 0;
1902 ff_unlock_avcodec();
1906 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1909 //This is for future deprecatec codec ids, its empty since
1910 //last major bump but will fill up again over time, please don't remove it
1911 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
1912 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
1913 default : return id;
1917 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1919 AVCodec *p, *experimental = NULL;
1921 id= remap_deprecated_codec_id(id);
1923 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1925 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1932 return experimental;
1935 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1937 return find_encdec(id, 1);
1940 AVCodec *avcodec_find_encoder_by_name(const char *name)
1947 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1954 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1956 return find_encdec(id, 0);
1959 AVCodec *avcodec_find_decoder_by_name(const char *name)
1966 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1973 const char *avcodec_get_name(enum AVCodecID id)
1975 const AVCodecDescriptor *cd;
1978 if (id == AV_CODEC_ID_NONE)
1980 cd = avcodec_descriptor_get(id);
1983 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1984 codec = avcodec_find_decoder(id);
1987 codec = avcodec_find_encoder(id);
1990 return "unknown_codec";
1993 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1995 int i, len, ret = 0;
1997 #define IS_PRINT(x) \
1998 (((x) >= '0' && (x) <= '9') || \
1999 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2000 ((x) == '.' || (x) == ' ' || (x) == '-'))
2002 for (i = 0; i < 4; i++) {
2003 len = snprintf(buf, buf_size,
2004 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
2006 buf_size = buf_size > len ? buf_size - len : 0;
2013 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2015 const char *codec_type;
2016 const char *codec_name;
2017 const char *profile = NULL;
2020 AVRational display_aspect_ratio;
2022 if (!buf || buf_size <= 0)
2024 codec_type = av_get_media_type_string(enc->codec_type);
2025 codec_name = avcodec_get_name(enc->codec_id);
2026 if (enc->profile != FF_PROFILE_UNKNOWN) {
2030 p = encode ? avcodec_find_encoder(enc->codec_id) :
2031 avcodec_find_decoder(enc->codec_id);
2033 profile = av_get_profile_name(p, enc->profile);
2036 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2037 codec_name, enc->mb_decision ? " (hq)" : "");
2038 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2040 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2041 if (enc->codec_tag) {
2043 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2044 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2045 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2048 switch (enc->codec_type) {
2049 case AVMEDIA_TYPE_VIDEO:
2050 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2051 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2053 av_get_pix_fmt_name(enc->pix_fmt));
2056 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2058 enc->width, enc->height);
2059 if (enc->sample_aspect_ratio.num) {
2060 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2061 enc->width * enc->sample_aspect_ratio.num,
2062 enc->height * enc->sample_aspect_ratio.den,
2064 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2065 " [SAR %d:%d DAR %d:%d]",
2066 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2067 display_aspect_ratio.num, display_aspect_ratio.den);
2069 if (av_log_get_level() >= AV_LOG_DEBUG) {
2070 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2071 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2073 enc->time_base.num / g, enc->time_base.den / g);
2077 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2078 ", q=%d-%d", enc->qmin, enc->qmax);
2081 case AVMEDIA_TYPE_AUDIO:
2082 if (enc->sample_rate) {
2083 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2084 ", %d Hz", enc->sample_rate);
2086 av_strlcat(buf, ", ", buf_size);
2087 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2088 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2089 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2090 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2097 if (enc->flags & CODEC_FLAG_PASS1)
2098 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2100 if (enc->flags & CODEC_FLAG_PASS2)
2101 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2104 bitrate = get_bit_rate(enc);
2106 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2107 ", %d kb/s", bitrate / 1000);
2111 const char *av_get_profile_name(const AVCodec *codec, int profile)
2114 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2117 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2118 if (p->profile == profile)
2124 unsigned avcodec_version(void)
2126 // av_assert0(AV_CODEC_ID_V410==164);
2127 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2128 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2129 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2130 av_assert0(AV_CODEC_ID_SRT==94216);
2131 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2133 return LIBAVCODEC_VERSION_INT;
2136 const char *avcodec_configuration(void)
2138 return FFMPEG_CONFIGURATION;
2141 const char *avcodec_license(void)
2143 #define LICENSE_PREFIX "libavcodec license: "
2144 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2147 void avcodec_flush_buffers(AVCodecContext *avctx)
2149 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2150 ff_thread_flush(avctx);
2151 else if (avctx->codec->flush)
2152 avctx->codec->flush(avctx);
2154 avctx->pts_correction_last_pts =
2155 avctx->pts_correction_last_dts = INT64_MIN;
2158 static void video_free_buffers(AVCodecContext *s)
2160 AVCodecInternal *avci = s->internal;
2166 if (avci->buffer_count)
2167 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2168 avci->buffer_count);
2169 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
2170 InternalBuffer *buf = &avci->buffer[i];
2171 for (j = 0; j < 4; j++) {
2172 av_freep(&buf->base[j]);
2173 buf->data[j] = NULL;
2176 av_freep(&avci->buffer);
2178 avci->buffer_count = 0;
2181 static void audio_free_buffers(AVCodecContext *avctx)
2183 AVCodecInternal *avci = avctx->internal;
2184 av_freep(&avci->audio_data);
2187 void avcodec_default_free_buffers(AVCodecContext *avctx)
2189 switch (avctx->codec_type) {
2190 case AVMEDIA_TYPE_VIDEO:
2191 video_free_buffers(avctx);
2193 case AVMEDIA_TYPE_AUDIO:
2194 audio_free_buffers(avctx);
2201 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2204 case AV_CODEC_ID_8SVX_EXP:
2205 case AV_CODEC_ID_8SVX_FIB:
2206 case AV_CODEC_ID_ADPCM_CT:
2207 case AV_CODEC_ID_ADPCM_IMA_APC:
2208 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2209 case AV_CODEC_ID_ADPCM_IMA_OKI:
2210 case AV_CODEC_ID_ADPCM_IMA_WS:
2211 case AV_CODEC_ID_ADPCM_G722:
2212 case AV_CODEC_ID_ADPCM_YAMAHA:
2214 case AV_CODEC_ID_PCM_ALAW:
2215 case AV_CODEC_ID_PCM_MULAW:
2216 case AV_CODEC_ID_PCM_S8:
2217 case AV_CODEC_ID_PCM_S8_PLANAR:
2218 case AV_CODEC_ID_PCM_U8:
2219 case AV_CODEC_ID_PCM_ZORK:
2221 case AV_CODEC_ID_PCM_S16BE:
2222 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2223 case AV_CODEC_ID_PCM_S16LE:
2224 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2225 case AV_CODEC_ID_PCM_U16BE:
2226 case AV_CODEC_ID_PCM_U16LE:
2228 case AV_CODEC_ID_PCM_S24DAUD:
2229 case AV_CODEC_ID_PCM_S24BE:
2230 case AV_CODEC_ID_PCM_S24LE:
2231 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2232 case AV_CODEC_ID_PCM_U24BE:
2233 case AV_CODEC_ID_PCM_U24LE:
2235 case AV_CODEC_ID_PCM_S32BE:
2236 case AV_CODEC_ID_PCM_S32LE:
2237 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2238 case AV_CODEC_ID_PCM_U32BE:
2239 case AV_CODEC_ID_PCM_U32LE:
2240 case AV_CODEC_ID_PCM_F32BE:
2241 case AV_CODEC_ID_PCM_F32LE:
2243 case AV_CODEC_ID_PCM_F64BE:
2244 case AV_CODEC_ID_PCM_F64LE:
2251 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2253 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2254 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2255 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2256 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2257 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2258 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2259 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2260 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2261 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2262 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2263 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2265 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2266 return AV_CODEC_ID_NONE;
2267 if (be < 0 || be > 1)
2269 return map[fmt][be];
2272 int av_get_bits_per_sample(enum AVCodecID codec_id)
2275 case AV_CODEC_ID_ADPCM_SBPRO_2:
2277 case AV_CODEC_ID_ADPCM_SBPRO_3:
2279 case AV_CODEC_ID_ADPCM_SBPRO_4:
2280 case AV_CODEC_ID_ADPCM_IMA_WAV:
2281 case AV_CODEC_ID_ADPCM_IMA_QT:
2282 case AV_CODEC_ID_ADPCM_SWF:
2283 case AV_CODEC_ID_ADPCM_MS:
2286 return av_get_exact_bits_per_sample(codec_id);
2290 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2292 int id, sr, ch, ba, tag, bps;
2294 id = avctx->codec_id;
2295 sr = avctx->sample_rate;
2296 ch = avctx->channels;
2297 ba = avctx->block_align;
2298 tag = avctx->codec_tag;
2299 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2301 /* codecs with an exact constant bits per sample */
2302 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2303 return (frame_bytes * 8LL) / (bps * ch);
2304 bps = avctx->bits_per_coded_sample;
2306 /* codecs with a fixed packet duration */
2308 case AV_CODEC_ID_ADPCM_ADX: return 32;
2309 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2310 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2311 case AV_CODEC_ID_AMR_NB:
2312 case AV_CODEC_ID_GSM:
2313 case AV_CODEC_ID_QCELP:
2314 case AV_CODEC_ID_RA_288: return 160;
2315 case AV_CODEC_ID_AMR_WB:
2316 case AV_CODEC_ID_GSM_MS: return 320;
2317 case AV_CODEC_ID_MP1: return 384;
2318 case AV_CODEC_ID_ATRAC1: return 512;
2319 case AV_CODEC_ID_ATRAC3: return 1024;
2320 case AV_CODEC_ID_MP2:
2321 case AV_CODEC_ID_MUSEPACK7: return 1152;
2322 case AV_CODEC_ID_AC3: return 1536;
2326 /* calc from sample rate */
2327 if (id == AV_CODEC_ID_TTA)
2328 return 256 * sr / 245;
2331 /* calc from sample rate and channels */
2332 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2333 return (480 << (sr / 22050)) / ch;
2338 /* calc from block_align */
2339 if (id == AV_CODEC_ID_SIPR) {
2341 case 20: return 160;
2342 case 19: return 144;
2343 case 29: return 288;
2344 case 37: return 480;
2346 } else if (id == AV_CODEC_ID_ILBC) {
2348 case 38: return 160;
2349 case 50: return 240;
2354 if (frame_bytes > 0) {
2355 /* calc from frame_bytes only */
2356 if (id == AV_CODEC_ID_TRUESPEECH)
2357 return 240 * (frame_bytes / 32);
2358 if (id == AV_CODEC_ID_NELLYMOSER)
2359 return 256 * (frame_bytes / 64);
2360 if (id == AV_CODEC_ID_RA_144)
2361 return 160 * (frame_bytes / 20);
2362 if (id == AV_CODEC_ID_G723_1)
2363 return 240 * (frame_bytes / 24);
2366 /* calc from frame_bytes and bits_per_coded_sample */
2367 if (id == AV_CODEC_ID_ADPCM_G726)
2368 return frame_bytes * 8 / bps;
2372 /* calc from frame_bytes and channels */
2374 case AV_CODEC_ID_ADPCM_AFC:
2375 return frame_bytes / (9 * ch) * 16;
2376 case AV_CODEC_ID_ADPCM_4XM:
2377 case AV_CODEC_ID_ADPCM_IMA_ISS:
2378 return (frame_bytes - 4 * ch) * 2 / ch;
2379 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2380 return (frame_bytes - 4) * 2 / ch;
2381 case AV_CODEC_ID_ADPCM_IMA_AMV:
2382 return (frame_bytes - 8) * 2 / ch;
2383 case AV_CODEC_ID_ADPCM_XA:
2384 return (frame_bytes / 128) * 224 / ch;
2385 case AV_CODEC_ID_INTERPLAY_DPCM:
2386 return (frame_bytes - 6 - ch) / ch;
2387 case AV_CODEC_ID_ROQ_DPCM:
2388 return (frame_bytes - 8) / ch;
2389 case AV_CODEC_ID_XAN_DPCM:
2390 return (frame_bytes - 2 * ch) / ch;
2391 case AV_CODEC_ID_MACE3:
2392 return 3 * frame_bytes / ch;
2393 case AV_CODEC_ID_MACE6:
2394 return 6 * frame_bytes / ch;
2395 case AV_CODEC_ID_PCM_LXF:
2396 return 2 * (frame_bytes / (5 * ch));
2397 case AV_CODEC_ID_IAC:
2398 case AV_CODEC_ID_IMC:
2399 return 4 * frame_bytes / ch;
2403 /* calc from frame_bytes, channels, and codec_tag */
2404 if (id == AV_CODEC_ID_SOL_DPCM) {
2406 return frame_bytes / ch;
2408 return frame_bytes * 2 / ch;
2413 /* calc from frame_bytes, channels, and block_align */
2414 int blocks = frame_bytes / ba;
2415 switch (avctx->codec_id) {
2416 case AV_CODEC_ID_ADPCM_IMA_WAV:
2417 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2418 case AV_CODEC_ID_ADPCM_IMA_DK3:
2419 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2420 case AV_CODEC_ID_ADPCM_IMA_DK4:
2421 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2422 case AV_CODEC_ID_ADPCM_MS:
2423 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2428 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2429 switch (avctx->codec_id) {
2430 case AV_CODEC_ID_PCM_DVD:
2433 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2434 case AV_CODEC_ID_PCM_BLURAY:
2437 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2438 case AV_CODEC_ID_S302M:
2439 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2449 int ff_thread_init(AVCodecContext *s)
2456 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2470 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2473 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2477 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2479 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2480 "version to the newest one from Git. If the problem still "
2481 "occurs, it means that your file has a feature which has not "
2482 "been implemented.\n", feature);
2484 av_log_ask_for_sample(avc, NULL);
2487 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2489 va_list argument_list;
2491 va_start(argument_list, msg);
2494 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2495 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2496 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2497 "and contact the ffmpeg-devel mailing list.\n");
2499 va_end(argument_list);
2502 static AVHWAccel *first_hwaccel = NULL;
2504 void av_register_hwaccel(AVHWAccel *hwaccel)
2506 AVHWAccel **p = &first_hwaccel;
2510 hwaccel->next = NULL;
2513 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2515 return hwaccel ? hwaccel->next : first_hwaccel;
2518 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2520 AVHWAccel *hwaccel = NULL;
2522 while ((hwaccel = av_hwaccel_next(hwaccel)))
2523 if (hwaccel->id == codec_id
2524 && hwaccel->pix_fmt == pix_fmt)
2529 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2531 if (ff_lockmgr_cb) {
2532 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2534 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2540 if (ff_lockmgr_cb) {
2541 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2543 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2549 int ff_lock_avcodec(AVCodecContext *log_ctx)
2551 if (ff_lockmgr_cb) {
2552 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
2555 entangled_thread_counter++;
2556 if (entangled_thread_counter != 1) {
2557 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
2558 return AVERROR(EINVAL);
2560 av_assert0(!ff_avcodec_locked);
2561 ff_avcodec_locked = 1;
2565 int ff_unlock_avcodec(void)
2567 av_assert0(ff_avcodec_locked);
2568 ff_avcodec_locked = 0;
2569 entangled_thread_counter--;
2570 if (ff_lockmgr_cb) {
2571 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
2577 int avpriv_lock_avformat(void)
2579 if (ff_lockmgr_cb) {
2580 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2586 int avpriv_unlock_avformat(void)
2588 if (ff_lockmgr_cb) {
2589 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2595 unsigned int avpriv_toupper4(unsigned int x)
2597 return toupper(x & 0xFF)
2598 + (toupper((x >> 8) & 0xFF) << 8)
2599 + (toupper((x >> 16) & 0xFF) << 16)
2600 + (toupper((x >> 24) & 0xFF) << 24);
2605 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2609 return ff_get_buffer(avctx, f);
2612 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2614 f->owner->release_buffer(f->owner, f);
2617 void ff_thread_finish_setup(AVCodecContext *avctx)
2621 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2625 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2629 int ff_thread_can_start_frame(AVCodecContext *avctx)
2636 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2638 AVCodec *c= avcodec_find_decoder(codec_id);
2640 c= avcodec_find_encoder(codec_id);
2644 if (codec_id <= AV_CODEC_ID_NONE)
2645 return AVMEDIA_TYPE_UNKNOWN;
2646 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2647 return AVMEDIA_TYPE_VIDEO;
2648 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2649 return AVMEDIA_TYPE_AUDIO;
2650 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2651 return AVMEDIA_TYPE_SUBTITLE;
2653 return AVMEDIA_TYPE_UNKNOWN;
2656 int avcodec_is_open(AVCodecContext *s)
2658 return !!s->internal;