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/bprint.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/crc.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/samplefmt.h"
37 #include "libavutil/dict.h"
38 #include "libavutil/avassert.h"
41 #include "libavutil/opt.h"
43 #include "frame_thread_encoder.h"
45 #include "bytestream.h"
51 volatile int ff_avcodec_locked;
52 static int volatile entangled_thread_counter = 0;
53 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
54 static void *codec_mutex;
55 static void *avformat_mutex;
57 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
62 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
64 ptr = av_realloc(ptr, min_size);
65 /* we could set this to the unmodified min_size but this is safer
66 * if the user lost the ptr and uses NULL now
76 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
81 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
83 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
90 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
92 ff_fast_malloc(ptr, size, min_size, 0);
95 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
98 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
103 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
104 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
107 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
110 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
115 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
116 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
119 /* encoder management */
120 static AVCodec *first_avcodec = NULL;
122 AVCodec *av_codec_next(const AVCodec *c)
127 return first_avcodec;
130 static void avcodec_init(void)
132 static int initialized = 0;
134 if (initialized != 0)
138 ff_dsputil_static_init();
141 int av_codec_is_encoder(const AVCodec *codec)
143 return codec && (codec->encode_sub || codec->encode2);
146 int av_codec_is_decoder(const AVCodec *codec)
148 return codec && codec->decode;
151 void avcodec_register(AVCodec *codec)
161 if (codec->init_static_data)
162 codec->init_static_data(codec);
165 unsigned avcodec_get_edge_width(void)
170 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
172 s->coded_width = width;
173 s->coded_height = height;
174 s->width = -((-width ) >> s->lowres);
175 s->height = -((-height) >> s->lowres);
178 #define INTERNAL_BUFFER_SIZE (32 + 1)
180 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
181 int linesize_align[AV_NUM_DATA_POINTERS])
187 switch (s->pix_fmt) {
188 case AV_PIX_FMT_YUV420P:
189 case AV_PIX_FMT_YUYV422:
190 case AV_PIX_FMT_UYVY422:
191 case AV_PIX_FMT_YUV422P:
192 case AV_PIX_FMT_YUV440P:
193 case AV_PIX_FMT_YUV444P:
194 case AV_PIX_FMT_GBRP:
195 case AV_PIX_FMT_GRAY8:
196 case AV_PIX_FMT_GRAY16BE:
197 case AV_PIX_FMT_GRAY16LE:
198 case AV_PIX_FMT_YUVJ420P:
199 case AV_PIX_FMT_YUVJ422P:
200 case AV_PIX_FMT_YUVJ440P:
201 case AV_PIX_FMT_YUVJ444P:
202 case AV_PIX_FMT_YUVA420P:
203 case AV_PIX_FMT_YUVA422P:
204 case AV_PIX_FMT_YUVA444P:
205 case AV_PIX_FMT_YUV420P9LE:
206 case AV_PIX_FMT_YUV420P9BE:
207 case AV_PIX_FMT_YUV420P10LE:
208 case AV_PIX_FMT_YUV420P10BE:
209 case AV_PIX_FMT_YUV420P12LE:
210 case AV_PIX_FMT_YUV420P12BE:
211 case AV_PIX_FMT_YUV420P14LE:
212 case AV_PIX_FMT_YUV420P14BE:
213 case AV_PIX_FMT_YUV422P9LE:
214 case AV_PIX_FMT_YUV422P9BE:
215 case AV_PIX_FMT_YUV422P10LE:
216 case AV_PIX_FMT_YUV422P10BE:
217 case AV_PIX_FMT_YUV422P12LE:
218 case AV_PIX_FMT_YUV422P12BE:
219 case AV_PIX_FMT_YUV422P14LE:
220 case AV_PIX_FMT_YUV422P14BE:
221 case AV_PIX_FMT_YUV444P9LE:
222 case AV_PIX_FMT_YUV444P9BE:
223 case AV_PIX_FMT_YUV444P10LE:
224 case AV_PIX_FMT_YUV444P10BE:
225 case AV_PIX_FMT_YUV444P12LE:
226 case AV_PIX_FMT_YUV444P12BE:
227 case AV_PIX_FMT_YUV444P14LE:
228 case AV_PIX_FMT_YUV444P14BE:
229 case AV_PIX_FMT_GBRP9LE:
230 case AV_PIX_FMT_GBRP9BE:
231 case AV_PIX_FMT_GBRP10LE:
232 case AV_PIX_FMT_GBRP10BE:
233 case AV_PIX_FMT_GBRP12LE:
234 case AV_PIX_FMT_GBRP12BE:
235 case AV_PIX_FMT_GBRP14LE:
236 case AV_PIX_FMT_GBRP14BE:
237 w_align = 16; //FIXME assume 16 pixel per macroblock
238 h_align = 16 * 2; // interlaced needs 2 macroblocks height
240 case AV_PIX_FMT_YUV411P:
241 case AV_PIX_FMT_UYYVYY411:
245 case AV_PIX_FMT_YUV410P:
246 if (s->codec_id == AV_CODEC_ID_SVQ1) {
251 case AV_PIX_FMT_RGB555:
252 if (s->codec_id == AV_CODEC_ID_RPZA) {
257 case AV_PIX_FMT_PAL8:
258 case AV_PIX_FMT_BGR8:
259 case AV_PIX_FMT_RGB8:
260 if (s->codec_id == AV_CODEC_ID_SMC) {
265 case AV_PIX_FMT_BGR24:
266 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
267 (s->codec_id == AV_CODEC_ID_ZLIB)) {
278 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
279 w_align = FFMAX(w_align, 8);
282 *width = FFALIGN(*width, w_align);
283 *height = FFALIGN(*height, h_align);
284 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
285 // some of the optimized chroma MC reads one line too much
286 // which is also done in mpeg decoders with lowres > 0
289 for (i = 0; i < 4; i++)
290 linesize_align[i] = STRIDE_ALIGN;
293 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
295 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
296 int chroma_shift = desc->log2_chroma_w;
297 int linesize_align[AV_NUM_DATA_POINTERS];
300 avcodec_align_dimensions2(s, width, height, linesize_align);
301 align = FFMAX(linesize_align[0], linesize_align[3]);
302 linesize_align[1] <<= chroma_shift;
303 linesize_align[2] <<= chroma_shift;
304 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
305 *width = FFALIGN(*width, align);
308 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
309 enum AVSampleFormat sample_fmt, const uint8_t *buf,
310 int buf_size, int align)
312 int ch, planar, needed_size, ret = 0;
314 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
315 frame->nb_samples, sample_fmt,
317 if (buf_size < needed_size)
318 return AVERROR(EINVAL);
320 planar = av_sample_fmt_is_planar(sample_fmt);
321 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
322 if (!(frame->extended_data = av_mallocz(nb_channels *
323 sizeof(*frame->extended_data))))
324 return AVERROR(ENOMEM);
326 frame->extended_data = frame->data;
329 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
330 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
331 sample_fmt, align)) < 0) {
332 if (frame->extended_data != frame->data)
333 av_freep(&frame->extended_data);
336 if (frame->extended_data != frame->data) {
337 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
338 frame->data[ch] = frame->extended_data[ch];
344 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
346 AVCodecInternal *avci = avctx->internal;
349 av_freep(&avci->audio_data);
350 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
351 frame->nb_samples, avctx->sample_fmt,
354 return AVERROR(EINVAL);
356 frame->data[0] = av_mallocz(buf_size);
358 return AVERROR(ENOMEM);
360 ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
361 frame->data[0], buf_size, 0);
363 av_freep(&frame->data[0]);
367 avci->audio_data = frame->data[0];
368 if (avctx->debug & FF_DEBUG_BUFFERS)
369 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
370 "internal audio buffer used\n", frame);
375 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
381 AVCodecInternal *avci = s->internal;
383 if (pic->data[0] != NULL) {
384 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
387 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
388 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
392 if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
393 av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
398 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
399 sizeof(InternalBuffer));
402 buf = &avci->buffer[avci->buffer_count];
404 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
405 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
406 av_freep(&buf->base[i]);
412 int h_chroma_shift, v_chroma_shift;
417 int stride_align[AV_NUM_DATA_POINTERS];
418 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
419 const int pixel_size = desc->comp[0].step_minus1 + 1;
421 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
424 avcodec_align_dimensions2(s, &w, &h, stride_align);
426 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
432 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
433 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
434 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
435 // increase alignment of w for next try (rhs gives the lowest bit set in w)
439 for (i = 0; i < 4; i++)
440 unaligned |= picture.linesize[i] % stride_align[i];
443 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
447 for (i = 0; i < 3 && picture.data[i + 1]; i++)
448 size[i] = picture.data[i + 1] - picture.data[i];
449 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
451 memset(buf->base, 0, sizeof(buf->base));
452 memset(buf->data, 0, sizeof(buf->data));
454 for (i = 0; i < 4 && size[i]; i++) {
455 const int h_shift = i == 0 ? 0 : h_chroma_shift;
456 const int v_shift = i == 0 ? 0 : v_chroma_shift;
458 buf->linesize[i] = picture.linesize[i];
460 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
461 if (buf->base[i] == NULL)
462 return AVERROR(ENOMEM);
463 memset(buf->base[i], 128, size[i]);
465 // no edge if EDGE EMU or not planar YUV
466 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
467 buf->data[i] = buf->base[i];
469 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
471 for (; i < AV_NUM_DATA_POINTERS; i++) {
472 buf->base[i] = buf->data[i] = NULL;
473 buf->linesize[i] = 0;
475 if (size[1] && !size[2])
476 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
477 buf->width = s->width;
478 buf->height = s->height;
479 buf->pix_fmt = s->pix_fmt;
482 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
483 pic->base[i] = buf->base[i];
484 pic->data[i] = buf->data[i];
485 pic->linesize[i] = buf->linesize[i];
487 pic->extended_data = pic->data;
488 avci->buffer_count++;
490 if (s->debug & FF_DEBUG_BUFFERS)
491 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
492 "buffers used\n", pic, avci->buffer_count);
497 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
499 frame->type = FF_BUFFER_TYPE_INTERNAL;
500 switch (avctx->codec_type) {
501 case AVMEDIA_TYPE_VIDEO:
502 return video_get_buffer(avctx, frame);
503 case AVMEDIA_TYPE_AUDIO:
504 return audio_get_buffer(avctx, frame);
510 void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
513 frame->pkt_pts = s->pkt->pts;
514 frame->pkt_pos = s->pkt->pos;
515 frame->pkt_duration = s->pkt->duration;
516 frame->pkt_size = s->pkt->size;
518 frame->pkt_pts = AV_NOPTS_VALUE;
520 frame->pkt_duration = 0;
521 frame->pkt_size = -1;
523 frame->reordered_opaque = s->reordered_opaque;
525 switch (s->codec->type) {
526 case AVMEDIA_TYPE_VIDEO:
527 frame->width = s->width;
528 frame->height = s->height;
529 frame->format = s->pix_fmt;
530 frame->sample_aspect_ratio = s->sample_aspect_ratio;
532 case AVMEDIA_TYPE_AUDIO:
533 frame->sample_rate = s->sample_rate;
534 frame->format = s->sample_fmt;
535 frame->channel_layout = s->channel_layout;
536 frame->channels = s->channels;
541 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
543 ff_init_buffer_info(avctx, frame);
545 return avctx->get_buffer(avctx, frame);
548 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
551 InternalBuffer *buf, *last;
552 AVCodecInternal *avci = s->internal;
554 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
556 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
557 assert(avci->buffer_count);
560 buf = NULL; /* avoids warning */
561 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
562 buf = &avci->buffer[i];
563 if (buf->data[0] == pic->data[0])
566 av_assert0(i < avci->buffer_count);
567 avci->buffer_count--;
568 last = &avci->buffer[avci->buffer_count];
571 FFSWAP(InternalBuffer, *buf, *last);
574 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
576 // pic->base[i]=NULL;
578 if (s->debug & FF_DEBUG_BUFFERS)
579 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
580 "buffers used\n", pic, avci->buffer_count);
583 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
588 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
590 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
591 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
592 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
593 s->release_buffer(s, pic);
596 ff_init_buffer_info(s, pic);
598 /* If no picture return a new buffer */
599 if (pic->data[0] == NULL) {
600 /* We will copy from buffer, so must be readable */
601 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
602 return ff_get_buffer(s, pic);
605 assert(s->pix_fmt == pic->format);
607 /* If internal buffer type return the same buffer */
608 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
613 * Not internal type and reget_buffer not overridden, emulate cr buffer
616 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
617 pic->data[i] = pic->base[i] = NULL;
619 /* Allocate new frame */
620 if ((ret = ff_get_buffer(s, pic)))
622 /* Copy image data from old buffer to new buffer */
623 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
625 s->release_buffer(s, &temp_pic); // Release old frame
629 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
633 for (i = 0; i < count; i++) {
634 int r = func(c, (char *)arg + i * size);
641 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
645 for (i = 0; i < count; i++) {
646 int r = func(c, arg, i, 0);
653 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
655 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
656 return desc->flags & PIX_FMT_HWACCEL;
659 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
661 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
666 void avcodec_get_frame_defaults(AVFrame *frame)
668 #if LIBAVCODEC_VERSION_MAJOR >= 55
669 // extended_data should explicitly be freed when needed, this code is unsafe currently
670 // also this is not compatible to the <55 ABI/API
671 if (frame->extended_data != frame->data && 0)
672 av_freep(&frame->extended_data);
675 memset(frame, 0, sizeof(AVFrame));
680 frame->best_effort_timestamp = AV_NOPTS_VALUE;
681 frame->pkt_duration = 0;
683 frame->pkt_size = -1;
684 frame->key_frame = 1;
685 frame->sample_aspect_ratio = (AVRational) {0, 1 };
686 frame->format = -1; /* unknown */
687 frame->extended_data = frame->data;
690 AVFrame *avcodec_alloc_frame(void)
692 AVFrame *frame = av_malloc(sizeof(AVFrame));
697 frame->extended_data = NULL;
698 avcodec_get_frame_defaults(frame);
703 void avcodec_free_frame(AVFrame **frame)
707 if (!frame || !*frame)
712 if (f->extended_data != f->data)
713 av_freep(&f->extended_data);
718 #define MAKE_ACCESSORS(str, name, type, field) \
719 type av_##name##_get_##field(const str *s) { return s->field; } \
720 void av_##name##_set_##field(str *s, type v) { s->field = v; }
722 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
723 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
724 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
725 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
726 MAKE_ACCESSORS(AVFrame, frame, int, channels)
727 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
728 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
729 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
730 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
732 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
733 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
735 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
737 memset(sub, 0, sizeof(*sub));
738 sub->pts = AV_NOPTS_VALUE;
741 static int get_bit_rate(AVCodecContext *ctx)
746 switch (ctx->codec_type) {
747 case AVMEDIA_TYPE_VIDEO:
748 case AVMEDIA_TYPE_DATA:
749 case AVMEDIA_TYPE_SUBTITLE:
750 case AVMEDIA_TYPE_ATTACHMENT:
751 bit_rate = ctx->bit_rate;
753 case AVMEDIA_TYPE_AUDIO:
754 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
755 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
764 #if FF_API_AVCODEC_OPEN
765 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
767 return avcodec_open2(avctx, codec, NULL);
771 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
777 ret = avcodec_open2(avctx, codec, options);
779 ff_lock_avcodec(avctx);
783 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
786 AVDictionary *tmp = NULL;
788 if (avcodec_is_open(avctx))
791 if ((!codec && !avctx->codec)) {
792 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
793 return AVERROR(EINVAL);
795 if ((codec && avctx->codec && codec != avctx->codec)) {
796 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
797 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
798 return AVERROR(EINVAL);
801 codec = avctx->codec;
803 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
804 return AVERROR(EINVAL);
807 av_dict_copy(&tmp, *options, 0);
809 ret = ff_lock_avcodec(avctx);
813 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
814 if (!avctx->internal) {
815 ret = AVERROR(ENOMEM);
819 if (codec->priv_data_size > 0) {
820 if (!avctx->priv_data) {
821 avctx->priv_data = av_mallocz(codec->priv_data_size);
822 if (!avctx->priv_data) {
823 ret = AVERROR(ENOMEM);
826 if (codec->priv_class) {
827 *(const AVClass **)avctx->priv_data = codec->priv_class;
828 av_opt_set_defaults(avctx->priv_data);
831 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
834 avctx->priv_data = NULL;
836 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
839 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
840 if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
842 if (avctx->coded_width && avctx->coded_height)
843 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
844 else if (avctx->width && avctx->height)
845 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
848 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
849 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
850 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
851 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
852 avcodec_set_dimensions(avctx, 0, 0);
855 /* if the decoder init function was already called previously,
856 * free the already allocated subtitle_header before overwriting it */
857 if (av_codec_is_decoder(codec))
858 av_freep(&avctx->subtitle_header);
860 if (avctx->channels > FF_SANE_NB_CHANNELS) {
861 ret = AVERROR(EINVAL);
865 avctx->codec = codec;
866 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
867 avctx->codec_id == AV_CODEC_ID_NONE) {
868 avctx->codec_type = codec->type;
869 avctx->codec_id = codec->id;
871 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
872 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
873 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
874 ret = AVERROR(EINVAL);
877 avctx->frame_number = 0;
878 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
880 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
881 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
882 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
884 av_log(NULL, AV_LOG_ERROR,
885 "The %s '%s' is experimental but experimental codecs are not enabled, "
886 "add '-strict %d' if you want to use it.\n",
887 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
888 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
889 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
890 av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
891 codec_string, codec2->name);
892 ret = AVERROR_EXPERIMENTAL;
896 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
897 (!avctx->time_base.num || !avctx->time_base.den)) {
898 avctx->time_base.num = 1;
899 avctx->time_base.den = avctx->sample_rate;
903 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
906 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
907 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
908 ff_lock_avcodec(avctx);
913 if (HAVE_THREADS && !avctx->thread_opaque
914 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
915 ret = ff_thread_init(avctx);
920 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
921 avctx->thread_count = 1;
923 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
924 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
925 avctx->codec->max_lowres);
926 ret = AVERROR(EINVAL);
930 if (av_codec_is_encoder(avctx->codec)) {
932 if (avctx->codec->sample_fmts) {
933 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
934 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
936 if (avctx->channels == 1 &&
937 av_get_planar_sample_fmt(avctx->sample_fmt) ==
938 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
939 avctx->sample_fmt = avctx->codec->sample_fmts[i];
943 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
945 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
946 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
947 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
948 ret = AVERROR(EINVAL);
952 if (avctx->codec->pix_fmts) {
953 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
954 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
956 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
957 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
958 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
960 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
961 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
962 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
963 ret = AVERROR(EINVAL);
967 if (avctx->codec->supported_samplerates) {
968 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
969 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
971 if (avctx->codec->supported_samplerates[i] == 0) {
972 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
974 ret = AVERROR(EINVAL);
978 if (avctx->codec->channel_layouts) {
979 if (!avctx->channel_layout) {
980 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
982 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
983 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
985 if (avctx->codec->channel_layouts[i] == 0) {
987 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
988 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
989 ret = AVERROR(EINVAL);
994 if (avctx->channel_layout && avctx->channels) {
995 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
996 if (channels != avctx->channels) {
998 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
999 av_log(avctx, AV_LOG_ERROR,
1000 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1001 buf, channels, avctx->channels);
1002 ret = AVERROR(EINVAL);
1005 } else if (avctx->channel_layout) {
1006 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1008 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1009 avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
1011 if (avctx->width <= 0 || avctx->height <= 0) {
1012 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1013 ret = AVERROR(EINVAL);
1017 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1018 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1019 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extreemly low, did you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1023 avctx->pts_correction_num_faulty_pts =
1024 avctx->pts_correction_num_faulty_dts = 0;
1025 avctx->pts_correction_last_pts =
1026 avctx->pts_correction_last_dts = INT64_MIN;
1028 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1029 || avctx->internal->frame_thread_encoder)) {
1030 ret = avctx->codec->init(avctx);
1038 if (av_codec_is_decoder(avctx->codec)) {
1039 if (!avctx->bit_rate)
1040 avctx->bit_rate = get_bit_rate(avctx);
1041 /* validate channel layout from the decoder */
1042 if (avctx->channel_layout) {
1043 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1044 if (!avctx->channels)
1045 avctx->channels = channels;
1046 else if (channels != avctx->channels) {
1048 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1049 av_log(avctx, AV_LOG_WARNING,
1050 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1051 "ignoring specified channel layout\n",
1052 buf, channels, avctx->channels);
1053 avctx->channel_layout = 0;
1056 if (avctx->channels && avctx->channels < 0 ||
1057 avctx->channels > FF_SANE_NB_CHANNELS) {
1058 ret = AVERROR(EINVAL);
1063 ff_unlock_avcodec();
1065 av_dict_free(options);
1072 av_freep(&avctx->priv_data);
1073 av_freep(&avctx->internal);
1074 avctx->codec = NULL;
1078 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1080 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1081 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1082 return AVERROR(EINVAL);
1086 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1087 if (!avpkt->data || avpkt->size < size) {
1088 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1089 avpkt->data = avctx->internal->byte_buffer;
1090 avpkt->size = avctx->internal->byte_buffer_size;
1091 avpkt->destruct = NULL;
1096 void *destruct = avpkt->destruct;
1098 if (avpkt->size < size) {
1099 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1100 return AVERROR(EINVAL);
1103 av_init_packet(avpkt);
1104 avpkt->destruct = destruct;
1108 int ret = av_new_packet(avpkt, size);
1110 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1115 int ff_alloc_packet(AVPacket *avpkt, int size)
1117 return ff_alloc_packet2(NULL, avpkt, size);
1121 * Pad last frame with silence.
1123 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1125 AVFrame *frame = NULL;
1126 uint8_t *buf = NULL;
1129 if (!(frame = avcodec_alloc_frame()))
1130 return AVERROR(ENOMEM);
1133 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1134 s->frame_size, s->sample_fmt, 0)) < 0)
1137 if (!(buf = av_malloc(ret))) {
1138 ret = AVERROR(ENOMEM);
1142 frame->nb_samples = s->frame_size;
1143 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1146 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1147 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1149 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1150 frame->nb_samples - src->nb_samples,
1151 s->channels, s->sample_fmt)) < 0)
1159 if (frame->extended_data != frame->data)
1160 av_freep(&frame->extended_data);
1166 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1168 const AVFrame *frame,
1169 int *got_packet_ptr)
1172 AVFrame *padded_frame = NULL;
1174 AVPacket user_pkt = *avpkt;
1175 int needs_realloc = !user_pkt.data;
1177 *got_packet_ptr = 0;
1179 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1180 av_free_packet(avpkt);
1181 av_init_packet(avpkt);
1185 /* ensure that extended_data is properly set */
1186 if (frame && !frame->extended_data) {
1187 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1188 avctx->channels > AV_NUM_DATA_POINTERS) {
1189 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1190 "with more than %d channels, but extended_data is not set.\n",
1191 AV_NUM_DATA_POINTERS);
1192 return AVERROR(EINVAL);
1194 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1197 tmp.extended_data = tmp.data;
1201 /* check for valid frame size */
1203 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1204 if (frame->nb_samples > avctx->frame_size) {
1205 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1206 return AVERROR(EINVAL);
1208 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1209 if (frame->nb_samples < avctx->frame_size &&
1210 !avctx->internal->last_audio_frame) {
1211 ret = pad_last_frame(avctx, &padded_frame, frame);
1215 frame = padded_frame;
1216 avctx->internal->last_audio_frame = 1;
1219 if (frame->nb_samples != avctx->frame_size) {
1220 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1221 ret = AVERROR(EINVAL);
1227 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1229 if (*got_packet_ptr) {
1230 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1231 if (avpkt->pts == AV_NOPTS_VALUE)
1232 avpkt->pts = frame->pts;
1233 if (!avpkt->duration)
1234 avpkt->duration = ff_samples_to_time_base(avctx,
1237 avpkt->dts = avpkt->pts;
1242 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1244 if (user_pkt.data) {
1245 if (user_pkt.size >= avpkt->size) {
1246 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1248 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1249 avpkt->size = user_pkt.size;
1252 avpkt->data = user_pkt.data;
1253 avpkt->destruct = user_pkt.destruct;
1255 if (av_dup_packet(avpkt) < 0) {
1256 ret = AVERROR(ENOMEM);
1262 if (needs_realloc && avpkt->data) {
1263 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1265 avpkt->data = new_data;
1268 avctx->frame_number++;
1271 if (ret < 0 || !*got_packet_ptr) {
1272 av_free_packet(avpkt);
1273 av_init_packet(avpkt);
1277 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1278 * this needs to be moved to the encoders, but for now we can do it
1279 * here to simplify things */
1280 avpkt->flags |= AV_PKT_FLAG_KEY;
1284 av_freep(&padded_frame->data[0]);
1285 if (padded_frame->extended_data != padded_frame->data)
1286 av_freep(&padded_frame->extended_data);
1287 av_freep(&padded_frame);
1293 #if FF_API_OLD_ENCODE_AUDIO
1294 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1295 uint8_t *buf, int buf_size,
1296 const short *samples)
1299 AVFrame frame0 = { { 0 } };
1301 int ret, samples_size, got_packet;
1303 av_init_packet(&pkt);
1305 pkt.size = buf_size;
1309 avcodec_get_frame_defaults(frame);
1311 if (avctx->frame_size) {
1312 frame->nb_samples = avctx->frame_size;
1314 /* if frame_size is not set, the number of samples must be
1315 * calculated from the buffer size */
1317 if (!av_get_bits_per_sample(avctx->codec_id)) {
1318 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1319 "support this codec\n");
1320 return AVERROR(EINVAL);
1322 nb_samples = (int64_t)buf_size * 8 /
1323 (av_get_bits_per_sample(avctx->codec_id) *
1325 if (nb_samples >= INT_MAX)
1326 return AVERROR(EINVAL);
1327 frame->nb_samples = nb_samples;
1330 /* it is assumed that the samples buffer is large enough based on the
1331 * relevant parameters */
1332 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1334 avctx->sample_fmt, 1);
1335 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1337 (const uint8_t *)samples,
1338 samples_size, 1)) < 0)
1341 /* fabricate frame pts from sample count.
1342 * this is needed because the avcodec_encode_audio() API does not have
1343 * a way for the user to provide pts */
1344 if (avctx->sample_rate && avctx->time_base.num)
1345 frame->pts = ff_samples_to_time_base(avctx,
1346 avctx->internal->sample_count);
1348 frame->pts = AV_NOPTS_VALUE;
1349 avctx->internal->sample_count += frame->nb_samples;
1355 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1356 if (!ret && got_packet && avctx->coded_frame) {
1357 avctx->coded_frame->pts = pkt.pts;
1358 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1360 /* free any side data since we cannot return it */
1361 ff_packet_free_side_data(&pkt);
1363 if (frame && frame->extended_data != frame->data)
1364 av_freep(&frame->extended_data);
1366 return ret ? ret : pkt.size;
1371 #if FF_API_OLD_ENCODE_VIDEO
1372 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1373 const AVFrame *pict)
1376 int ret, got_packet = 0;
1378 if (buf_size < FF_MIN_BUFFER_SIZE) {
1379 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1383 av_init_packet(&pkt);
1385 pkt.size = buf_size;
1387 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1388 if (!ret && got_packet && avctx->coded_frame) {
1389 avctx->coded_frame->pts = pkt.pts;
1390 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1393 /* free any side data since we cannot return it */
1394 if (pkt.side_data_elems > 0) {
1396 for (i = 0; i < pkt.side_data_elems; i++)
1397 av_free(pkt.side_data[i].data);
1398 av_freep(&pkt.side_data);
1399 pkt.side_data_elems = 0;
1402 return ret ? ret : pkt.size;
1407 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1409 const AVFrame *frame,
1410 int *got_packet_ptr)
1413 AVPacket user_pkt = *avpkt;
1414 int needs_realloc = !user_pkt.data;
1416 *got_packet_ptr = 0;
1418 if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1419 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1421 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
1422 avctx->stats_out[0] = '\0';
1424 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1425 av_free_packet(avpkt);
1426 av_init_packet(avpkt);
1431 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1432 return AVERROR(EINVAL);
1434 av_assert0(avctx->codec->encode2);
1436 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1437 av_assert0(ret <= 0);
1439 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1441 if (user_pkt.data) {
1442 if (user_pkt.size >= avpkt->size) {
1443 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1445 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1446 avpkt->size = user_pkt.size;
1449 avpkt->data = user_pkt.data;
1450 avpkt->destruct = user_pkt.destruct;
1452 if (av_dup_packet(avpkt) < 0) {
1453 ret = AVERROR(ENOMEM);
1459 if (!*got_packet_ptr)
1461 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1462 avpkt->pts = avpkt->dts = frame->pts;
1464 if (needs_realloc && avpkt->data &&
1465 avpkt->destruct == av_destruct_packet) {
1466 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1468 avpkt->data = new_data;
1471 avctx->frame_number++;
1474 if (ret < 0 || !*got_packet_ptr)
1475 av_free_packet(avpkt);
1481 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1482 const AVSubtitle *sub)
1485 if (sub->start_display_time) {
1486 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1490 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1491 avctx->frame_number++;
1496 * Attempt to guess proper monotonic timestamps for decoded video frames
1497 * which might have incorrect times. Input timestamps may wrap around, in
1498 * which case the output will as well.
1500 * @param pts the pts field of the decoded AVPacket, as passed through
1502 * @param dts the dts field of the decoded AVPacket
1503 * @return one of the input values, may be AV_NOPTS_VALUE
1505 static int64_t guess_correct_pts(AVCodecContext *ctx,
1506 int64_t reordered_pts, int64_t dts)
1508 int64_t pts = AV_NOPTS_VALUE;
1510 if (dts != AV_NOPTS_VALUE) {
1511 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1512 ctx->pts_correction_last_dts = dts;
1514 if (reordered_pts != AV_NOPTS_VALUE) {
1515 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1516 ctx->pts_correction_last_pts = reordered_pts;
1518 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1519 && reordered_pts != AV_NOPTS_VALUE)
1520 pts = reordered_pts;
1527 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1530 const uint8_t *data;
1533 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1536 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1537 if (!data || size < 4)
1539 flags = bytestream_get_le32(&data);
1541 if (size < 4) /* Required for any of the changes */
1543 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1544 avctx->channels = bytestream_get_le32(&data);
1547 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1550 avctx->channel_layout = bytestream_get_le64(&data);
1555 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1556 avctx->sample_rate = bytestream_get_le32(&data);
1559 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1562 avctx->width = bytestream_get_le32(&data);
1563 avctx->height = bytestream_get_le32(&data);
1564 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1569 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1572 const uint8_t *side_metadata;
1575 av_dict_free(&avctx->metadata);
1576 side_metadata = av_packet_get_side_data(avctx->pkt,
1577 AV_PKT_DATA_STRINGS_METADATA, &size);
1580 end = side_metadata + size;
1581 while (side_metadata < end) {
1582 const uint8_t *key = side_metadata;
1583 const uint8_t *val = side_metadata + strlen(key) + 1;
1584 int ret = av_dict_set(&frame->metadata, key, val, 0);
1587 side_metadata = val + strlen(val) + 1;
1590 avctx->metadata = frame->metadata;
1594 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1595 int *got_picture_ptr,
1596 const AVPacket *avpkt)
1599 // copy to ensure we do not change avpkt
1600 AVPacket tmp = *avpkt;
1602 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1603 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1604 return AVERROR(EINVAL);
1607 *got_picture_ptr = 0;
1608 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1609 return AVERROR(EINVAL);
1611 avcodec_get_frame_defaults(picture);
1613 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1614 int did_split = av_packet_split_side_data(&tmp);
1615 apply_param_change(avctx, &tmp);
1617 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1618 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1621 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1623 picture->pkt_dts = avpkt->dts;
1625 if(!avctx->has_b_frames){
1626 picture->pkt_pos = avpkt->pos;
1628 //FIXME these should be under if(!avctx->has_b_frames)
1629 /* get_buffer is supposed to set frame parameters */
1630 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1631 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1632 if (!picture->width) picture->width = avctx->width;
1633 if (!picture->height) picture->height = avctx->height;
1634 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1637 add_metadata_from_side_data(avctx, picture);
1639 emms_c(); //needed to avoid an emms_c() call before every return;
1643 ff_packet_free_side_data(&tmp);
1648 if (*got_picture_ptr){
1649 avctx->frame_number++;
1650 picture->best_effort_timestamp = guess_correct_pts(avctx,
1657 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1658 * make sure it's set correctly */
1659 picture->extended_data = picture->data;
1664 #if FF_API_OLD_DECODE_AUDIO
1665 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1666 int *frame_size_ptr,
1669 AVFrame frame = { { 0 } };
1670 int ret, got_frame = 0;
1672 if (avctx->get_buffer != avcodec_default_get_buffer) {
1673 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1674 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1675 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1676 "avcodec_decode_audio4()\n");
1677 avctx->get_buffer = avcodec_default_get_buffer;
1678 avctx->release_buffer = avcodec_default_release_buffer;
1681 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1683 if (ret >= 0 && got_frame) {
1685 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1686 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1688 avctx->sample_fmt, 1);
1689 if (*frame_size_ptr < data_size) {
1690 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1691 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1692 return AVERROR(EINVAL);
1695 memcpy(samples, frame.extended_data[0], plane_size);
1697 if (planar && avctx->channels > 1) {
1698 uint8_t *out = ((uint8_t *)samples) + plane_size;
1699 for (ch = 1; ch < avctx->channels; ch++) {
1700 memcpy(out, frame.extended_data[ch], plane_size);
1704 *frame_size_ptr = data_size;
1706 *frame_size_ptr = 0;
1713 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1716 const AVPacket *avpkt)
1718 int planar, channels;
1723 if (!avpkt->data && avpkt->size) {
1724 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1725 return AVERROR(EINVAL);
1727 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1728 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1729 return AVERROR(EINVAL);
1732 avcodec_get_frame_defaults(frame);
1734 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1737 // copy to ensure we do not change avpkt
1738 AVPacket tmp = *avpkt;
1739 int did_split = av_packet_split_side_data(&tmp);
1740 apply_param_change(avctx, &tmp);
1743 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1744 if (ret >= 0 && *got_frame_ptr) {
1745 avctx->frame_number++;
1746 frame->pkt_dts = avpkt->dts;
1747 frame->best_effort_timestamp = guess_correct_pts(avctx,
1750 if (frame->format == AV_SAMPLE_FMT_NONE)
1751 frame->format = avctx->sample_fmt;
1752 if (!frame->channel_layout)
1753 frame->channel_layout = avctx->channel_layout;
1754 if (!frame->channels)
1755 frame->channels = avctx->channels;
1756 if (!frame->sample_rate)
1757 frame->sample_rate = avctx->sample_rate;
1759 add_metadata_from_side_data(avctx, frame);
1761 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1762 if(side && side_size>=10) {
1763 avctx->internal->skip_samples = AV_RL32(side);
1764 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1765 avctx->internal->skip_samples);
1767 if (avctx->internal->skip_samples) {
1768 if(frame->nb_samples <= avctx->internal->skip_samples){
1770 avctx->internal->skip_samples -= frame->nb_samples;
1771 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1772 avctx->internal->skip_samples);
1774 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1775 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1776 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1777 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1778 (AVRational){1, avctx->sample_rate},
1779 avctx->pkt_timebase);
1780 if(frame->pkt_pts!=AV_NOPTS_VALUE)
1781 frame->pkt_pts += diff_ts;
1782 if(frame->pkt_dts!=AV_NOPTS_VALUE)
1783 frame->pkt_dts += diff_ts;
1784 if (frame->pkt_duration >= diff_ts)
1785 frame->pkt_duration -= diff_ts;
1787 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1789 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1790 avctx->internal->skip_samples, frame->nb_samples);
1791 frame->nb_samples -= avctx->internal->skip_samples;
1792 avctx->internal->skip_samples = 0;
1798 ff_packet_free_side_data(&tmp);
1804 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1805 * make sure it's set correctly; assume decoders that actually use
1806 * extended_data are doing it correctly */
1807 if (*got_frame_ptr) {
1808 planar = av_sample_fmt_is_planar(frame->format);
1809 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1810 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1811 frame->extended_data = frame->data;
1813 frame->extended_data = NULL;
1819 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1825 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1826 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1827 return AVERROR(EINVAL);
1831 avcodec_get_subtitle_defaults(sub);
1834 AVPacket tmp = *avpkt;
1835 int did_split = av_packet_split_side_data(&tmp);
1836 //apply_param_change(avctx, &tmp);
1840 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
1841 sub->pts = av_rescale_q(avpkt->pts,
1842 avctx->pkt_timebase, AV_TIME_BASE_Q);
1843 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &tmp);
1844 sub->format = sub->num_rects && sub->rects[0]->ass;
1848 ff_packet_free_side_data(&tmp);
1854 avctx->frame_number++;
1860 void avsubtitle_free(AVSubtitle *sub)
1864 for (i = 0; i < sub->num_rects; i++) {
1865 av_freep(&sub->rects[i]->pict.data[0]);
1866 av_freep(&sub->rects[i]->pict.data[1]);
1867 av_freep(&sub->rects[i]->pict.data[2]);
1868 av_freep(&sub->rects[i]->pict.data[3]);
1869 av_freep(&sub->rects[i]->text);
1870 av_freep(&sub->rects[i]->ass);
1871 av_freep(&sub->rects[i]);
1874 av_freep(&sub->rects);
1876 memset(sub, 0, sizeof(AVSubtitle));
1879 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
1883 ff_unlock_avcodec();
1885 ret = avcodec_close(avctx);
1887 ff_lock_avcodec(NULL);
1891 av_cold int avcodec_close(AVCodecContext *avctx)
1893 int ret = ff_lock_avcodec(avctx);
1897 if (avcodec_is_open(avctx)) {
1898 if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1899 ff_unlock_avcodec();
1900 ff_frame_thread_encoder_free(avctx);
1901 ff_lock_avcodec(avctx);
1903 if (HAVE_THREADS && avctx->thread_opaque)
1904 ff_thread_free(avctx);
1905 if (avctx->codec && avctx->codec->close)
1906 avctx->codec->close(avctx);
1907 avcodec_default_free_buffers(avctx);
1908 avctx->coded_frame = NULL;
1909 avctx->internal->byte_buffer_size = 0;
1910 av_freep(&avctx->internal->byte_buffer);
1911 av_freep(&avctx->internal);
1912 av_dict_free(&avctx->metadata);
1915 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1916 av_opt_free(avctx->priv_data);
1918 av_freep(&avctx->priv_data);
1919 if (av_codec_is_encoder(avctx->codec))
1920 av_freep(&avctx->extradata);
1921 avctx->codec = NULL;
1922 avctx->active_thread_type = 0;
1924 ff_unlock_avcodec();
1928 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1931 //This is for future deprecatec codec ids, its empty since
1932 //last major bump but will fill up again over time, please don't remove it
1933 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
1934 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
1935 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
1936 default : return id;
1940 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1942 AVCodec *p, *experimental = NULL;
1944 id= remap_deprecated_codec_id(id);
1946 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1948 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1955 return experimental;
1958 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1960 return find_encdec(id, 1);
1963 AVCodec *avcodec_find_encoder_by_name(const char *name)
1970 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1977 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1979 return find_encdec(id, 0);
1982 AVCodec *avcodec_find_decoder_by_name(const char *name)
1989 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1996 const char *avcodec_get_name(enum AVCodecID id)
1998 const AVCodecDescriptor *cd;
2001 if (id == AV_CODEC_ID_NONE)
2003 cd = avcodec_descriptor_get(id);
2006 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2007 codec = avcodec_find_decoder(id);
2010 codec = avcodec_find_encoder(id);
2013 return "unknown_codec";
2016 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2018 int i, len, ret = 0;
2020 #define IS_PRINT(x) \
2021 (((x) >= '0' && (x) <= '9') || \
2022 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2023 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2025 for (i = 0; i < 4; i++) {
2026 len = snprintf(buf, buf_size,
2027 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
2029 buf_size = buf_size > len ? buf_size - len : 0;
2036 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2038 const char *codec_type;
2039 const char *codec_name;
2040 const char *profile = NULL;
2043 AVRational display_aspect_ratio;
2045 if (!buf || buf_size <= 0)
2047 codec_type = av_get_media_type_string(enc->codec_type);
2048 codec_name = avcodec_get_name(enc->codec_id);
2049 if (enc->profile != FF_PROFILE_UNKNOWN) {
2053 p = encode ? avcodec_find_encoder(enc->codec_id) :
2054 avcodec_find_decoder(enc->codec_id);
2056 profile = av_get_profile_name(p, enc->profile);
2059 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2060 codec_name, enc->mb_decision ? " (hq)" : "");
2061 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2063 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2064 if (enc->codec_tag) {
2066 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2067 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2068 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2071 switch (enc->codec_type) {
2072 case AVMEDIA_TYPE_VIDEO:
2073 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2074 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2076 av_get_pix_fmt_name(enc->pix_fmt));
2077 if (enc->bits_per_raw_sample &&
2078 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2079 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2080 " (%d bpc)", enc->bits_per_raw_sample);
2083 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2085 enc->width, enc->height);
2086 if (enc->sample_aspect_ratio.num) {
2087 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2088 enc->width * enc->sample_aspect_ratio.num,
2089 enc->height * enc->sample_aspect_ratio.den,
2091 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2092 " [SAR %d:%d DAR %d:%d]",
2093 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2094 display_aspect_ratio.num, display_aspect_ratio.den);
2096 if (av_log_get_level() >= AV_LOG_DEBUG) {
2097 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2098 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2100 enc->time_base.num / g, enc->time_base.den / g);
2104 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2105 ", q=%d-%d", enc->qmin, enc->qmax);
2108 case AVMEDIA_TYPE_AUDIO:
2109 if (enc->sample_rate) {
2110 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2111 ", %d Hz", enc->sample_rate);
2113 av_strlcat(buf, ", ", buf_size);
2114 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2115 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2116 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2117 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2124 if (enc->flags & CODEC_FLAG_PASS1)
2125 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2127 if (enc->flags & CODEC_FLAG_PASS2)
2128 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2131 bitrate = get_bit_rate(enc);
2133 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2134 ", %d kb/s", bitrate / 1000);
2138 const char *av_get_profile_name(const AVCodec *codec, int profile)
2141 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2144 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2145 if (p->profile == profile)
2151 unsigned avcodec_version(void)
2153 // av_assert0(AV_CODEC_ID_V410==164);
2154 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2155 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2156 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2157 av_assert0(AV_CODEC_ID_SRT==94216);
2158 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2160 return LIBAVCODEC_VERSION_INT;
2163 const char *avcodec_configuration(void)
2165 return FFMPEG_CONFIGURATION;
2168 const char *avcodec_license(void)
2170 #define LICENSE_PREFIX "libavcodec license: "
2171 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2174 void avcodec_flush_buffers(AVCodecContext *avctx)
2176 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2177 ff_thread_flush(avctx);
2178 else if (avctx->codec->flush)
2179 avctx->codec->flush(avctx);
2181 avctx->pts_correction_last_pts =
2182 avctx->pts_correction_last_dts = INT64_MIN;
2185 static void video_free_buffers(AVCodecContext *s)
2187 AVCodecInternal *avci = s->internal;
2193 if (avci->buffer_count)
2194 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2195 avci->buffer_count);
2196 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
2197 InternalBuffer *buf = &avci->buffer[i];
2198 for (j = 0; j < 4; j++) {
2199 av_freep(&buf->base[j]);
2200 buf->data[j] = NULL;
2203 av_freep(&avci->buffer);
2205 avci->buffer_count = 0;
2208 static void audio_free_buffers(AVCodecContext *avctx)
2210 AVCodecInternal *avci = avctx->internal;
2211 av_freep(&avci->audio_data);
2214 void avcodec_default_free_buffers(AVCodecContext *avctx)
2216 switch (avctx->codec_type) {
2217 case AVMEDIA_TYPE_VIDEO:
2218 video_free_buffers(avctx);
2220 case AVMEDIA_TYPE_AUDIO:
2221 audio_free_buffers(avctx);
2228 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2231 case AV_CODEC_ID_8SVX_EXP:
2232 case AV_CODEC_ID_8SVX_FIB:
2233 case AV_CODEC_ID_ADPCM_CT:
2234 case AV_CODEC_ID_ADPCM_IMA_APC:
2235 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2236 case AV_CODEC_ID_ADPCM_IMA_OKI:
2237 case AV_CODEC_ID_ADPCM_IMA_WS:
2238 case AV_CODEC_ID_ADPCM_G722:
2239 case AV_CODEC_ID_ADPCM_YAMAHA:
2241 case AV_CODEC_ID_PCM_ALAW:
2242 case AV_CODEC_ID_PCM_MULAW:
2243 case AV_CODEC_ID_PCM_S8:
2244 case AV_CODEC_ID_PCM_S8_PLANAR:
2245 case AV_CODEC_ID_PCM_U8:
2246 case AV_CODEC_ID_PCM_ZORK:
2248 case AV_CODEC_ID_PCM_S16BE:
2249 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2250 case AV_CODEC_ID_PCM_S16LE:
2251 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2252 case AV_CODEC_ID_PCM_U16BE:
2253 case AV_CODEC_ID_PCM_U16LE:
2255 case AV_CODEC_ID_PCM_S24DAUD:
2256 case AV_CODEC_ID_PCM_S24BE:
2257 case AV_CODEC_ID_PCM_S24LE:
2258 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2259 case AV_CODEC_ID_PCM_U24BE:
2260 case AV_CODEC_ID_PCM_U24LE:
2262 case AV_CODEC_ID_PCM_S32BE:
2263 case AV_CODEC_ID_PCM_S32LE:
2264 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2265 case AV_CODEC_ID_PCM_U32BE:
2266 case AV_CODEC_ID_PCM_U32LE:
2267 case AV_CODEC_ID_PCM_F32BE:
2268 case AV_CODEC_ID_PCM_F32LE:
2270 case AV_CODEC_ID_PCM_F64BE:
2271 case AV_CODEC_ID_PCM_F64LE:
2278 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2280 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2281 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2282 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2283 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2284 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2285 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2286 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2287 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2288 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2289 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2290 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2292 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2293 return AV_CODEC_ID_NONE;
2294 if (be < 0 || be > 1)
2296 return map[fmt][be];
2299 int av_get_bits_per_sample(enum AVCodecID codec_id)
2302 case AV_CODEC_ID_ADPCM_SBPRO_2:
2304 case AV_CODEC_ID_ADPCM_SBPRO_3:
2306 case AV_CODEC_ID_ADPCM_SBPRO_4:
2307 case AV_CODEC_ID_ADPCM_IMA_WAV:
2308 case AV_CODEC_ID_ADPCM_IMA_QT:
2309 case AV_CODEC_ID_ADPCM_SWF:
2310 case AV_CODEC_ID_ADPCM_MS:
2313 return av_get_exact_bits_per_sample(codec_id);
2317 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2319 int id, sr, ch, ba, tag, bps;
2321 id = avctx->codec_id;
2322 sr = avctx->sample_rate;
2323 ch = avctx->channels;
2324 ba = avctx->block_align;
2325 tag = avctx->codec_tag;
2326 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2328 /* codecs with an exact constant bits per sample */
2329 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2330 return (frame_bytes * 8LL) / (bps * ch);
2331 bps = avctx->bits_per_coded_sample;
2333 /* codecs with a fixed packet duration */
2335 case AV_CODEC_ID_ADPCM_ADX: return 32;
2336 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2337 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2338 case AV_CODEC_ID_AMR_NB:
2339 case AV_CODEC_ID_GSM:
2340 case AV_CODEC_ID_QCELP:
2341 case AV_CODEC_ID_RA_288: return 160;
2342 case AV_CODEC_ID_AMR_WB:
2343 case AV_CODEC_ID_GSM_MS: return 320;
2344 case AV_CODEC_ID_MP1: return 384;
2345 case AV_CODEC_ID_ATRAC1: return 512;
2346 case AV_CODEC_ID_ATRAC3: return 1024;
2347 case AV_CODEC_ID_MP2:
2348 case AV_CODEC_ID_MUSEPACK7: return 1152;
2349 case AV_CODEC_ID_AC3: return 1536;
2353 /* calc from sample rate */
2354 if (id == AV_CODEC_ID_TTA)
2355 return 256 * sr / 245;
2358 /* calc from sample rate and channels */
2359 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2360 return (480 << (sr / 22050)) / ch;
2365 /* calc from block_align */
2366 if (id == AV_CODEC_ID_SIPR) {
2368 case 20: return 160;
2369 case 19: return 144;
2370 case 29: return 288;
2371 case 37: return 480;
2373 } else if (id == AV_CODEC_ID_ILBC) {
2375 case 38: return 160;
2376 case 50: return 240;
2381 if (frame_bytes > 0) {
2382 /* calc from frame_bytes only */
2383 if (id == AV_CODEC_ID_TRUESPEECH)
2384 return 240 * (frame_bytes / 32);
2385 if (id == AV_CODEC_ID_NELLYMOSER)
2386 return 256 * (frame_bytes / 64);
2387 if (id == AV_CODEC_ID_RA_144)
2388 return 160 * (frame_bytes / 20);
2389 if (id == AV_CODEC_ID_G723_1)
2390 return 240 * (frame_bytes / 24);
2393 /* calc from frame_bytes and bits_per_coded_sample */
2394 if (id == AV_CODEC_ID_ADPCM_G726)
2395 return frame_bytes * 8 / bps;
2399 /* calc from frame_bytes and channels */
2401 case AV_CODEC_ID_ADPCM_AFC:
2402 return frame_bytes / (9 * ch) * 16;
2403 case AV_CODEC_ID_ADPCM_4XM:
2404 case AV_CODEC_ID_ADPCM_IMA_ISS:
2405 return (frame_bytes - 4 * ch) * 2 / ch;
2406 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2407 return (frame_bytes - 4) * 2 / ch;
2408 case AV_CODEC_ID_ADPCM_IMA_AMV:
2409 return (frame_bytes - 8) * 2 / ch;
2410 case AV_CODEC_ID_ADPCM_XA:
2411 return (frame_bytes / 128) * 224 / ch;
2412 case AV_CODEC_ID_INTERPLAY_DPCM:
2413 return (frame_bytes - 6 - ch) / ch;
2414 case AV_CODEC_ID_ROQ_DPCM:
2415 return (frame_bytes - 8) / ch;
2416 case AV_CODEC_ID_XAN_DPCM:
2417 return (frame_bytes - 2 * ch) / ch;
2418 case AV_CODEC_ID_MACE3:
2419 return 3 * frame_bytes / ch;
2420 case AV_CODEC_ID_MACE6:
2421 return 6 * frame_bytes / ch;
2422 case AV_CODEC_ID_PCM_LXF:
2423 return 2 * (frame_bytes / (5 * ch));
2424 case AV_CODEC_ID_IAC:
2425 case AV_CODEC_ID_IMC:
2426 return 4 * frame_bytes / ch;
2430 /* calc from frame_bytes, channels, and codec_tag */
2431 if (id == AV_CODEC_ID_SOL_DPCM) {
2433 return frame_bytes / ch;
2435 return frame_bytes * 2 / ch;
2440 /* calc from frame_bytes, channels, and block_align */
2441 int blocks = frame_bytes / ba;
2442 switch (avctx->codec_id) {
2443 case AV_CODEC_ID_ADPCM_IMA_WAV:
2444 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2445 case AV_CODEC_ID_ADPCM_IMA_DK3:
2446 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2447 case AV_CODEC_ID_ADPCM_IMA_DK4:
2448 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2449 case AV_CODEC_ID_ADPCM_MS:
2450 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2455 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2456 switch (avctx->codec_id) {
2457 case AV_CODEC_ID_PCM_DVD:
2460 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2461 case AV_CODEC_ID_PCM_BLURAY:
2464 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2465 case AV_CODEC_ID_S302M:
2466 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2476 int ff_thread_init(AVCodecContext *s)
2483 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2497 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2500 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2504 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2506 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2507 "version to the newest one from Git. If the problem still "
2508 "occurs, it means that your file has a feature which has not "
2509 "been implemented.\n", feature);
2511 av_log_ask_for_sample(avc, NULL);
2514 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2516 va_list argument_list;
2518 va_start(argument_list, msg);
2521 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2522 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2523 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2524 "and contact the ffmpeg-devel mailing list.\n");
2526 va_end(argument_list);
2529 static AVHWAccel *first_hwaccel = NULL;
2531 void av_register_hwaccel(AVHWAccel *hwaccel)
2533 AVHWAccel **p = &first_hwaccel;
2537 hwaccel->next = NULL;
2540 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2542 return hwaccel ? hwaccel->next : first_hwaccel;
2545 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2547 AVHWAccel *hwaccel = NULL;
2549 while ((hwaccel = av_hwaccel_next(hwaccel)))
2550 if (hwaccel->id == codec_id
2551 && hwaccel->pix_fmt == pix_fmt)
2556 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2558 if (ff_lockmgr_cb) {
2559 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2561 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2567 if (ff_lockmgr_cb) {
2568 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2570 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2576 int ff_lock_avcodec(AVCodecContext *log_ctx)
2578 if (ff_lockmgr_cb) {
2579 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
2582 entangled_thread_counter++;
2583 if (entangled_thread_counter != 1) {
2584 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
2585 ff_avcodec_locked = 1;
2586 ff_unlock_avcodec();
2587 return AVERROR(EINVAL);
2589 av_assert0(!ff_avcodec_locked);
2590 ff_avcodec_locked = 1;
2594 int ff_unlock_avcodec(void)
2596 av_assert0(ff_avcodec_locked);
2597 ff_avcodec_locked = 0;
2598 entangled_thread_counter--;
2599 if (ff_lockmgr_cb) {
2600 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
2606 int avpriv_lock_avformat(void)
2608 if (ff_lockmgr_cb) {
2609 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2615 int avpriv_unlock_avformat(void)
2617 if (ff_lockmgr_cb) {
2618 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2624 unsigned int avpriv_toupper4(unsigned int x)
2626 return toupper(x & 0xFF)
2627 + (toupper((x >> 8) & 0xFF) << 8)
2628 + (toupper((x >> 16) & 0xFF) << 16)
2629 + (toupper((x >> 24) & 0xFF) << 24);
2634 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2638 return ff_get_buffer(avctx, f);
2641 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2643 f->owner->release_buffer(f->owner, f);
2646 void ff_thread_finish_setup(AVCodecContext *avctx)
2650 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2654 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2658 int ff_thread_can_start_frame(AVCodecContext *avctx)
2665 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2667 AVCodec *c= avcodec_find_decoder(codec_id);
2669 c= avcodec_find_encoder(codec_id);
2673 if (codec_id <= AV_CODEC_ID_NONE)
2674 return AVMEDIA_TYPE_UNKNOWN;
2675 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2676 return AVMEDIA_TYPE_VIDEO;
2677 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2678 return AVMEDIA_TYPE_AUDIO;
2679 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2680 return AVMEDIA_TYPE_SUBTITLE;
2682 return AVMEDIA_TYPE_UNKNOWN;
2685 int avcodec_is_open(AVCodecContext *s)
2687 return !!s->internal;
2690 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
2695 ret = av_bprint_finalize(buf, &str);
2698 avctx->extradata = str;
2699 /* Note: the string is NUL terminated (so extradata can be read as a
2700 * string), but the ending character is not accounted in the size (in
2701 * binary formats you are likely not supposed to mux that character). When
2702 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
2704 avctx->extradata_size = buf->len;