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/crc.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/audioconvert.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"
41 #include "imgconvert.h"
43 #include "frame_thread_encoder.h"
44 #include "audioconvert.h"
46 #include "bytestream.h"
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 PIX_FMT_YUV420P:
189 case PIX_FMT_YUYV422:
190 case PIX_FMT_UYVY422:
191 case PIX_FMT_YUV422P:
192 case PIX_FMT_YUV440P:
193 case PIX_FMT_YUV444P:
196 case PIX_FMT_GRAY16BE:
197 case PIX_FMT_GRAY16LE:
198 case PIX_FMT_YUVJ420P:
199 case PIX_FMT_YUVJ422P:
200 case PIX_FMT_YUVJ440P:
201 case PIX_FMT_YUVJ444P:
202 case PIX_FMT_YUVA420P:
203 case PIX_FMT_YUVA422P:
204 case PIX_FMT_YUVA444P:
205 case PIX_FMT_YUV420P9LE:
206 case PIX_FMT_YUV420P9BE:
207 case PIX_FMT_YUV420P10LE:
208 case PIX_FMT_YUV420P10BE:
209 case PIX_FMT_YUV420P12LE:
210 case PIX_FMT_YUV420P12BE:
211 case PIX_FMT_YUV420P14LE:
212 case PIX_FMT_YUV420P14BE:
213 case PIX_FMT_YUV422P9LE:
214 case PIX_FMT_YUV422P9BE:
215 case PIX_FMT_YUV422P10LE:
216 case PIX_FMT_YUV422P10BE:
217 case PIX_FMT_YUV422P12LE:
218 case PIX_FMT_YUV422P12BE:
219 case PIX_FMT_YUV422P14LE:
220 case PIX_FMT_YUV422P14BE:
221 case PIX_FMT_YUV444P9LE:
222 case PIX_FMT_YUV444P9BE:
223 case PIX_FMT_YUV444P10LE:
224 case PIX_FMT_YUV444P10BE:
225 case PIX_FMT_YUV444P12LE:
226 case PIX_FMT_YUV444P12BE:
227 case PIX_FMT_YUV444P14LE:
228 case PIX_FMT_YUV444P14BE:
229 case PIX_FMT_GBRP9LE:
230 case PIX_FMT_GBRP9BE:
231 case PIX_FMT_GBRP10LE:
232 case PIX_FMT_GBRP10BE:
233 case PIX_FMT_GBRP12LE:
234 case PIX_FMT_GBRP12BE:
235 case PIX_FMT_GBRP14LE:
236 case 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 PIX_FMT_YUV411P:
241 case PIX_FMT_UYYVYY411:
245 case PIX_FMT_YUV410P:
246 if (s->codec_id == AV_CODEC_ID_SVQ1) {
251 if (s->codec_id == AV_CODEC_ID_RPZA) {
258 if (s->codec_id == AV_CODEC_ID_SMC) {
264 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
265 (s->codec_id == AV_CODEC_ID_ZLIB)) {
276 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
277 w_align = FFMAX(w_align, 8);
280 *width = FFALIGN(*width, w_align);
281 *height = FFALIGN(*height, h_align);
282 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
283 // some of the optimized chroma MC reads one line too much
284 // which is also done in mpeg decoders with lowres > 0
287 for (i = 0; i < 4; i++)
288 linesize_align[i] = STRIDE_ALIGN;
291 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
293 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
294 int linesize_align[AV_NUM_DATA_POINTERS];
297 avcodec_align_dimensions2(s, width, height, linesize_align);
298 align = FFMAX(linesize_align[0], linesize_align[3]);
299 linesize_align[1] <<= chroma_shift;
300 linesize_align[2] <<= chroma_shift;
301 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
302 *width = FFALIGN(*width, align);
305 void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
308 frame->pkt_pts = s->pkt->pts;
309 frame->pkt_pos = s->pkt->pos;
310 frame->pkt_duration = s->pkt->duration;
312 frame->pkt_pts = AV_NOPTS_VALUE;
314 frame->pkt_duration = 0;
316 frame->reordered_opaque = s->reordered_opaque;
318 switch (s->codec->type) {
319 case AVMEDIA_TYPE_VIDEO:
320 frame->width = s->width;
321 frame->height = s->height;
322 frame->format = s->pix_fmt;
323 frame->sample_aspect_ratio = s->sample_aspect_ratio;
325 case AVMEDIA_TYPE_AUDIO:
326 frame->sample_rate = s->sample_rate;
327 frame->format = s->sample_fmt;
328 frame->channel_layout = s->channel_layout;
329 frame->channels = s->channels;
334 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
335 enum AVSampleFormat sample_fmt, const uint8_t *buf,
336 int buf_size, int align)
338 int ch, planar, needed_size, ret = 0;
340 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
341 frame->nb_samples, sample_fmt,
343 if (buf_size < needed_size)
344 return AVERROR(EINVAL);
346 planar = av_sample_fmt_is_planar(sample_fmt);
347 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
348 if (!(frame->extended_data = av_mallocz(nb_channels *
349 sizeof(*frame->extended_data))))
350 return AVERROR(ENOMEM);
352 frame->extended_data = frame->data;
355 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
356 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
357 sample_fmt, align)) < 0) {
358 if (frame->extended_data != frame->data)
359 av_freep(&frame->extended_data);
362 if (frame->extended_data != frame->data) {
363 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
364 frame->data[ch] = frame->extended_data[ch];
370 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
372 AVCodecInternal *avci = avctx->internal;
376 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
377 frame->nb_samples, avctx->sample_fmt,
380 return AVERROR(EINVAL);
382 /* allocate InternalBuffer if needed */
384 avci->buffer = av_mallocz(sizeof(InternalBuffer));
386 return AVERROR(ENOMEM);
390 /* if there is a previously-used internal buffer, check its size and
391 * channel count to see if we can reuse it */
392 if (buf->extended_data) {
393 /* if current buffer is too small, free it */
394 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
395 av_free(buf->extended_data[0]);
396 if (buf->extended_data != buf->data)
397 av_freep(&buf->extended_data);
398 buf->extended_data = NULL;
401 /* if number of channels has changed, reset and/or free extended data
402 * pointers but leave data buffer in buf->data[0] for reuse */
403 if (buf->nb_channels != avctx->channels) {
404 if (buf->extended_data != buf->data)
405 av_free(buf->extended_data);
406 buf->extended_data = NULL;
410 /* if there is no previous buffer or the previous buffer cannot be used
411 * as-is, allocate a new buffer and/or rearrange the channel pointers */
412 if (!buf->extended_data) {
414 if (!(buf->data[0] = av_mallocz(buf_size)))
415 return AVERROR(ENOMEM);
416 buf->audio_data_size = buf_size;
418 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
419 avctx->sample_fmt, buf->data[0],
420 buf->audio_data_size, 0)))
423 if (frame->extended_data == frame->data)
424 buf->extended_data = buf->data;
426 buf->extended_data = frame->extended_data;
427 memcpy(buf->data, frame->data, sizeof(frame->data));
428 buf->linesize[0] = frame->linesize[0];
429 buf->nb_channels = avctx->channels;
431 /* copy InternalBuffer info to the AVFrame */
432 frame->extended_data = buf->extended_data;
433 frame->linesize[0] = buf->linesize[0];
434 memcpy(frame->data, buf->data, sizeof(frame->data));
437 frame->type = FF_BUFFER_TYPE_INTERNAL;
438 ff_init_buffer_info(avctx, frame);
440 if (avctx->debug & FF_DEBUG_BUFFERS)
441 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
442 "internal audio buffer used\n", frame);
447 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
453 AVCodecInternal *avci = s->internal;
455 if (pic->data[0] != NULL) {
456 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
459 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
460 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
464 if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
465 av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
470 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
471 sizeof(InternalBuffer));
474 buf = &avci->buffer[avci->buffer_count];
476 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
477 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
478 av_freep(&buf->base[i]);
484 int h_chroma_shift, v_chroma_shift;
489 int stride_align[AV_NUM_DATA_POINTERS];
490 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1 + 1;
492 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
494 avcodec_align_dimensions2(s, &w, &h, stride_align);
496 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
502 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
503 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
504 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
505 // increase alignment of w for next try (rhs gives the lowest bit set in w)
509 for (i = 0; i < 4; i++)
510 unaligned |= picture.linesize[i] % stride_align[i];
513 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
517 for (i = 0; i < 3 && picture.data[i + 1]; i++)
518 size[i] = picture.data[i + 1] - picture.data[i];
519 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
521 memset(buf->base, 0, sizeof(buf->base));
522 memset(buf->data, 0, sizeof(buf->data));
524 for (i = 0; i < 4 && size[i]; i++) {
525 const int h_shift = i == 0 ? 0 : h_chroma_shift;
526 const int v_shift = i == 0 ? 0 : v_chroma_shift;
528 buf->linesize[i] = picture.linesize[i];
530 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
531 if (buf->base[i] == NULL)
532 return AVERROR(ENOMEM);
533 memset(buf->base[i], 128, size[i]);
535 // no edge if EDGE EMU or not planar YUV
536 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
537 buf->data[i] = buf->base[i];
539 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
541 for (; i < AV_NUM_DATA_POINTERS; i++) {
542 buf->base[i] = buf->data[i] = NULL;
543 buf->linesize[i] = 0;
545 if (size[1] && !size[2])
546 ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
547 buf->width = s->width;
548 buf->height = s->height;
549 buf->pix_fmt = s->pix_fmt;
551 pic->type = FF_BUFFER_TYPE_INTERNAL;
553 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
554 pic->base[i] = buf->base[i];
555 pic->data[i] = buf->data[i];
556 pic->linesize[i] = buf->linesize[i];
558 pic->extended_data = pic->data;
559 avci->buffer_count++;
560 pic->width = buf->width;
561 pic->height = buf->height;
562 pic->format = buf->pix_fmt;
564 ff_init_buffer_info(s, pic);
566 if (s->debug & FF_DEBUG_BUFFERS)
567 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
568 "buffers used\n", pic, avci->buffer_count);
573 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
575 switch (avctx->codec_type) {
576 case AVMEDIA_TYPE_VIDEO:
577 return video_get_buffer(avctx, frame);
578 case AVMEDIA_TYPE_AUDIO:
579 return audio_get_buffer(avctx, frame);
585 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
588 InternalBuffer *buf, *last;
589 AVCodecInternal *avci = s->internal;
591 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
593 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
594 assert(avci->buffer_count);
597 buf = NULL; /* avoids warning */
598 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
599 buf = &avci->buffer[i];
600 if (buf->data[0] == pic->data[0])
603 av_assert0(i < avci->buffer_count);
604 avci->buffer_count--;
605 last = &avci->buffer[avci->buffer_count];
608 FFSWAP(InternalBuffer, *buf, *last);
611 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
613 // pic->base[i]=NULL;
614 //printf("R%X\n", pic->opaque);
616 if (s->debug & FF_DEBUG_BUFFERS)
617 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
618 "buffers used\n", pic, avci->buffer_count);
621 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
626 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
628 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
629 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
630 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
631 s->release_buffer(s, pic);
634 ff_init_buffer_info(s, pic);
636 /* If no picture return a new buffer */
637 if (pic->data[0] == NULL) {
638 /* We will copy from buffer, so must be readable */
639 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
640 return s->get_buffer(s, pic);
643 assert(s->pix_fmt == pic->format);
645 /* If internal buffer type return the same buffer */
646 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
651 * Not internal type and reget_buffer not overridden, emulate cr buffer
654 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
655 pic->data[i] = pic->base[i] = NULL;
657 /* Allocate new frame */
658 if (s->get_buffer(s, pic))
660 /* Copy image data from old buffer to new buffer */
661 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
663 s->release_buffer(s, &temp_pic); // Release old frame
667 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
671 for (i = 0; i < count; i++) {
672 int r = func(c, (char *)arg + i * size);
679 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
683 for (i = 0; i < count; i++) {
684 int r = func(c, arg, i, 0);
691 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt)
693 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
698 void avcodec_get_frame_defaults(AVFrame *pic)
700 memset(pic, 0, sizeof(AVFrame));
705 pic->best_effort_timestamp = AV_NOPTS_VALUE;
706 pic->pkt_duration = 0;
709 pic->sample_aspect_ratio = (AVRational) {0, 1 };
710 pic->format = -1; /* unknown */
713 AVFrame *avcodec_alloc_frame(void)
715 AVFrame *pic = av_malloc(sizeof(AVFrame));
720 avcodec_get_frame_defaults(pic);
725 #define MAKE_ACCESSORS(str, name, type, field) \
726 type av_##name##_get_##field(const str *s) { return s->field; } \
727 void av_##name##_set_##field(str *s, type v) { s->field = v; }
729 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
730 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
731 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
732 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
733 MAKE_ACCESSORS(AVFrame, frame, int, channels)
734 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
735 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
736 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
738 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
739 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
741 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
743 memset(sub, 0, sizeof(*sub));
744 sub->pts = AV_NOPTS_VALUE;
747 static int get_bit_rate(AVCodecContext *ctx)
752 switch (ctx->codec_type) {
753 case AVMEDIA_TYPE_VIDEO:
754 case AVMEDIA_TYPE_DATA:
755 case AVMEDIA_TYPE_SUBTITLE:
756 case AVMEDIA_TYPE_ATTACHMENT:
757 bit_rate = ctx->bit_rate;
759 case AVMEDIA_TYPE_AUDIO:
760 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
761 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
770 #if FF_API_AVCODEC_OPEN
771 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
773 return avcodec_open2(avctx, codec, NULL);
777 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
780 AVDictionary *tmp = NULL;
782 if (avcodec_is_open(avctx))
785 if ((!codec && !avctx->codec)) {
786 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
787 return AVERROR(EINVAL);
789 if ((codec && avctx->codec && codec != avctx->codec)) {
790 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
791 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
792 return AVERROR(EINVAL);
795 codec = avctx->codec;
797 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
798 return AVERROR(EINVAL);
801 av_dict_copy(&tmp, *options, 0);
803 /* If there is a user-supplied mutex locking routine, call it. */
805 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
809 entangled_thread_counter++;
810 if (entangled_thread_counter != 1) {
811 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
816 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
817 if (!avctx->internal) {
818 ret = AVERROR(ENOMEM);
822 if (codec->priv_data_size > 0) {
823 if (!avctx->priv_data) {
824 avctx->priv_data = av_mallocz(codec->priv_data_size);
825 if (!avctx->priv_data) {
826 ret = AVERROR(ENOMEM);
829 if (codec->priv_class) {
830 *(const AVClass **)avctx->priv_data = codec->priv_class;
831 av_opt_set_defaults(avctx->priv_data);
834 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
837 avctx->priv_data = NULL;
839 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
842 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
843 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
844 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
849 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
850 if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
852 if (avctx->coded_width && avctx->coded_height)
853 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
854 else if (avctx->width && avctx->height)
855 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
858 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
859 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
860 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
861 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
862 avcodec_set_dimensions(avctx, 0, 0);
865 /* if the decoder init function was already called previously,
866 * free the already allocated subtitle_header before overwriting it */
867 if (av_codec_is_decoder(codec))
868 av_freep(&avctx->subtitle_header);
870 #define SANE_NB_CHANNELS 128U
871 if (avctx->channels > SANE_NB_CHANNELS) {
872 ret = AVERROR(EINVAL);
876 avctx->codec = codec;
877 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
878 avctx->codec_id == AV_CODEC_ID_NONE) {
879 avctx->codec_type = codec->type;
880 avctx->codec_id = codec->id;
882 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
883 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
884 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
885 ret = AVERROR(EINVAL);
888 avctx->frame_number = 0;
889 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
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 entangled_thread_counter--; //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 entangled_thread_counter++;
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->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
932 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
933 ret = AVERROR(EINVAL);
937 if (avctx->codec->pix_fmts) {
938 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
939 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
941 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
942 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
943 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
944 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
945 ret = AVERROR(EINVAL);
949 if (avctx->codec->supported_samplerates) {
950 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
951 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
953 if (avctx->codec->supported_samplerates[i] == 0) {
954 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
955 ret = AVERROR(EINVAL);
959 if (avctx->codec->channel_layouts) {
960 if (!avctx->channel_layout) {
961 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
963 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
964 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
966 if (avctx->codec->channel_layouts[i] == 0) {
967 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
968 ret = AVERROR(EINVAL);
973 if (avctx->channel_layout && avctx->channels) {
974 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
975 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
976 ret = AVERROR(EINVAL);
979 } else if (avctx->channel_layout) {
980 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
984 avctx->pts_correction_num_faulty_pts =
985 avctx->pts_correction_num_faulty_dts = 0;
986 avctx->pts_correction_last_pts =
987 avctx->pts_correction_last_dts = INT64_MIN;
989 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
990 || avctx->internal->frame_thread_encoder)) {
991 ret = avctx->codec->init(avctx);
999 if (av_codec_is_decoder(avctx->codec)) {
1000 if (!avctx->bit_rate)
1001 avctx->bit_rate = get_bit_rate(avctx);
1002 /* validate channel layout from the decoder */
1003 if (avctx->channel_layout &&
1004 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1005 av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
1006 avctx->channel_layout = 0;
1010 entangled_thread_counter--;
1012 /* Release any user-supplied mutex. */
1013 if (ff_lockmgr_cb) {
1014 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1017 av_dict_free(options);
1024 av_freep(&avctx->priv_data);
1025 av_freep(&avctx->internal);
1026 avctx->codec = NULL;
1030 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1032 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1033 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1034 return AVERROR(EINVAL);
1038 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1039 if (!avpkt->data || avpkt->size < size) {
1040 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1041 avpkt->data = avctx->internal->byte_buffer;
1042 avpkt->size = avctx->internal->byte_buffer_size;
1043 avpkt->destruct = NULL;
1048 void *destruct = avpkt->destruct;
1050 if (avpkt->size < size) {
1051 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1052 return AVERROR(EINVAL);
1055 av_init_packet(avpkt);
1056 avpkt->destruct = destruct;
1060 int ret = av_new_packet(avpkt, size);
1062 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1067 int ff_alloc_packet(AVPacket *avpkt, int size)
1069 return ff_alloc_packet2(NULL, avpkt, size);
1073 * Pad last frame with silence.
1075 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1077 AVFrame *frame = NULL;
1078 uint8_t *buf = NULL;
1081 if (!(frame = avcodec_alloc_frame()))
1082 return AVERROR(ENOMEM);
1085 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1086 s->frame_size, s->sample_fmt, 0)) < 0)
1089 if (!(buf = av_malloc(ret))) {
1090 ret = AVERROR(ENOMEM);
1094 frame->nb_samples = s->frame_size;
1095 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1098 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1099 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1101 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1102 frame->nb_samples - src->nb_samples,
1103 s->channels, s->sample_fmt)) < 0)
1111 if (frame->extended_data != frame->data)
1112 av_freep(&frame->extended_data);
1118 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1120 const AVFrame *frame,
1121 int *got_packet_ptr)
1124 AVFrame *padded_frame = NULL;
1126 AVPacket user_pkt = *avpkt;
1127 int needs_realloc = !user_pkt.data;
1129 *got_packet_ptr = 0;
1131 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1132 av_free_packet(avpkt);
1133 av_init_packet(avpkt);
1137 /* ensure that extended_data is properly set */
1138 if (frame && !frame->extended_data) {
1139 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1140 avctx->channels > AV_NUM_DATA_POINTERS) {
1141 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1142 "with more than %d channels, but extended_data is not set.\n",
1143 AV_NUM_DATA_POINTERS);
1144 return AVERROR(EINVAL);
1146 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1149 tmp.extended_data = tmp.data;
1153 /* check for valid frame size */
1155 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1156 if (frame->nb_samples > avctx->frame_size) {
1157 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1158 return AVERROR(EINVAL);
1160 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1161 if (frame->nb_samples < avctx->frame_size &&
1162 !avctx->internal->last_audio_frame) {
1163 ret = pad_last_frame(avctx, &padded_frame, frame);
1167 frame = padded_frame;
1168 avctx->internal->last_audio_frame = 1;
1171 if (frame->nb_samples != avctx->frame_size) {
1172 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1173 return AVERROR(EINVAL);
1178 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1180 if (*got_packet_ptr) {
1181 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1182 if (avpkt->pts == AV_NOPTS_VALUE)
1183 avpkt->pts = frame->pts;
1184 if (!avpkt->duration)
1185 avpkt->duration = ff_samples_to_time_base(avctx,
1188 avpkt->dts = avpkt->pts;
1193 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1195 if (user_pkt.data) {
1196 if (user_pkt.size >= avpkt->size) {
1197 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1199 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1200 avpkt->size = user_pkt.size;
1203 avpkt->data = user_pkt.data;
1204 avpkt->destruct = user_pkt.destruct;
1206 if (av_dup_packet(avpkt) < 0) {
1207 ret = AVERROR(ENOMEM);
1213 if (needs_realloc && avpkt->data) {
1214 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1216 avpkt->data = new_data;
1219 avctx->frame_number++;
1222 if (ret < 0 || !*got_packet_ptr) {
1223 av_free_packet(avpkt);
1224 av_init_packet(avpkt);
1228 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1229 * this needs to be moved to the encoders, but for now we can do it
1230 * here to simplify things */
1231 avpkt->flags |= AV_PKT_FLAG_KEY;
1234 av_freep(&padded_frame->data[0]);
1235 if (padded_frame->extended_data != padded_frame->data)
1236 av_freep(&padded_frame->extended_data);
1237 av_freep(&padded_frame);
1243 #if FF_API_OLD_DECODE_AUDIO
1244 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1245 uint8_t *buf, int buf_size,
1246 const short *samples)
1251 int ret, samples_size, got_packet;
1253 av_init_packet(&pkt);
1255 pkt.size = buf_size;
1259 avcodec_get_frame_defaults(frame);
1261 if (avctx->frame_size) {
1262 frame->nb_samples = avctx->frame_size;
1264 /* if frame_size is not set, the number of samples must be
1265 * calculated from the buffer size */
1267 if (!av_get_bits_per_sample(avctx->codec_id)) {
1268 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1269 "support this codec\n");
1270 return AVERROR(EINVAL);
1272 nb_samples = (int64_t)buf_size * 8 /
1273 (av_get_bits_per_sample(avctx->codec_id) *
1275 if (nb_samples >= INT_MAX)
1276 return AVERROR(EINVAL);
1277 frame->nb_samples = nb_samples;
1280 /* it is assumed that the samples buffer is large enough based on the
1281 * relevant parameters */
1282 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1284 avctx->sample_fmt, 1);
1285 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1287 (const uint8_t *)samples,
1291 /* fabricate frame pts from sample count.
1292 * this is needed because the avcodec_encode_audio() API does not have
1293 * a way for the user to provide pts */
1294 if (avctx->sample_rate && avctx->time_base.num)
1295 frame->pts = ff_samples_to_time_base(avctx,
1296 avctx->internal->sample_count);
1298 frame->pts = AV_NOPTS_VALUE;
1299 avctx->internal->sample_count += frame->nb_samples;
1305 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1306 if (!ret && got_packet && avctx->coded_frame) {
1307 avctx->coded_frame->pts = pkt.pts;
1308 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1310 /* free any side data since we cannot return it */
1311 ff_packet_free_side_data(&pkt);
1313 if (frame && frame->extended_data != frame->data)
1314 av_freep(&frame->extended_data);
1316 return ret ? ret : pkt.size;
1321 #if FF_API_OLD_ENCODE_VIDEO
1322 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1323 const AVFrame *pict)
1326 int ret, got_packet = 0;
1328 if (buf_size < FF_MIN_BUFFER_SIZE) {
1329 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1333 av_init_packet(&pkt);
1335 pkt.size = buf_size;
1337 ret = avcodec_encode_video2(avctx, &pkt, pict, &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);
1343 /* free any side data since we cannot return it */
1344 if (pkt.side_data_elems > 0) {
1346 for (i = 0; i < pkt.side_data_elems; i++)
1347 av_free(pkt.side_data[i].data);
1348 av_freep(&pkt.side_data);
1349 pkt.side_data_elems = 0;
1352 return ret ? ret : pkt.size;
1357 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1359 const AVFrame *frame,
1360 int *got_packet_ptr)
1363 AVPacket user_pkt = *avpkt;
1364 int needs_realloc = !user_pkt.data;
1366 *got_packet_ptr = 0;
1368 if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1369 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1371 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1372 av_free_packet(avpkt);
1373 av_init_packet(avpkt);
1378 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1379 return AVERROR(EINVAL);
1381 av_assert0(avctx->codec->encode2);
1383 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1384 av_assert0(ret <= 0);
1386 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1388 if (user_pkt.data) {
1389 if (user_pkt.size >= avpkt->size) {
1390 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1392 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1393 avpkt->size = user_pkt.size;
1396 avpkt->data = user_pkt.data;
1397 avpkt->destruct = user_pkt.destruct;
1399 if (av_dup_packet(avpkt) < 0) {
1400 ret = AVERROR(ENOMEM);
1406 if (!*got_packet_ptr)
1408 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1409 avpkt->pts = avpkt->dts = frame->pts;
1411 if (needs_realloc && avpkt->data &&
1412 avpkt->destruct == av_destruct_packet) {
1413 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1415 avpkt->data = new_data;
1418 avctx->frame_number++;
1421 if (ret < 0 || !*got_packet_ptr)
1422 av_free_packet(avpkt);
1428 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1429 const AVSubtitle *sub)
1432 if (sub->start_display_time) {
1433 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1437 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1438 avctx->frame_number++;
1443 * Attempt to guess proper monotonic timestamps for decoded video frames
1444 * which might have incorrect times. Input timestamps may wrap around, in
1445 * which case the output will as well.
1447 * @param pts the pts field of the decoded AVPacket, as passed through
1449 * @param dts the dts field of the decoded AVPacket
1450 * @return one of the input values, may be AV_NOPTS_VALUE
1452 static int64_t guess_correct_pts(AVCodecContext *ctx,
1453 int64_t reordered_pts, int64_t dts)
1455 int64_t pts = AV_NOPTS_VALUE;
1457 if (dts != AV_NOPTS_VALUE) {
1458 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1459 ctx->pts_correction_last_dts = dts;
1461 if (reordered_pts != AV_NOPTS_VALUE) {
1462 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1463 ctx->pts_correction_last_pts = reordered_pts;
1465 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1466 && reordered_pts != AV_NOPTS_VALUE)
1467 pts = reordered_pts;
1474 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1477 const uint8_t *data;
1480 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1483 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1484 if (!data || size < 4)
1486 flags = bytestream_get_le32(&data);
1488 if (size < 4) /* Required for any of the changes */
1490 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1491 avctx->channels = bytestream_get_le32(&data);
1494 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1497 avctx->channel_layout = bytestream_get_le64(&data);
1502 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1503 avctx->sample_rate = bytestream_get_le32(&data);
1506 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1509 avctx->width = bytestream_get_le32(&data);
1510 avctx->height = bytestream_get_le32(&data);
1511 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1516 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1517 int *got_picture_ptr,
1518 const AVPacket *avpkt)
1521 // copy to ensure we do not change avpkt
1522 AVPacket tmp = *avpkt;
1524 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1525 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1526 return AVERROR(EINVAL);
1529 *got_picture_ptr = 0;
1530 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1531 return AVERROR(EINVAL);
1533 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1534 int did_split = av_packet_split_side_data(&tmp);
1535 apply_param_change(avctx, &tmp);
1537 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1538 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1541 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1543 picture->pkt_dts = avpkt->dts;
1545 if(!avctx->has_b_frames){
1546 picture->pkt_pos = avpkt->pos;
1548 //FIXME these should be under if(!avctx->has_b_frames)
1549 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1550 if (!picture->width) picture->width = avctx->width;
1551 if (!picture->height) picture->height = avctx->height;
1552 if (picture->format == PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1555 emms_c(); //needed to avoid an emms_c() call before every return;
1559 ff_packet_free_side_data(&tmp);
1564 if (*got_picture_ptr){
1565 avctx->frame_number++;
1566 picture->best_effort_timestamp = guess_correct_pts(avctx,
1576 #if FF_API_OLD_DECODE_AUDIO
1577 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1578 int *frame_size_ptr,
1582 int ret, got_frame = 0;
1584 if (avctx->get_buffer != avcodec_default_get_buffer) {
1585 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1586 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1587 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1588 "avcodec_decode_audio4()\n");
1589 avctx->get_buffer = avcodec_default_get_buffer;
1590 avctx->release_buffer = avcodec_default_release_buffer;
1593 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1595 if (ret >= 0 && got_frame) {
1597 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1598 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1600 avctx->sample_fmt, 1);
1601 if (*frame_size_ptr < data_size) {
1602 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1603 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1604 return AVERROR(EINVAL);
1607 memcpy(samples, frame.extended_data[0], plane_size);
1609 if (planar && avctx->channels > 1) {
1610 uint8_t *out = ((uint8_t *)samples) + plane_size;
1611 for (ch = 1; ch < avctx->channels; ch++) {
1612 memcpy(out, frame.extended_data[ch], plane_size);
1616 *frame_size_ptr = data_size;
1618 *frame_size_ptr = 0;
1625 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1628 const AVPacket *avpkt)
1634 if (!avpkt->data && avpkt->size) {
1635 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1636 return AVERROR(EINVAL);
1638 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1639 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1640 return AVERROR(EINVAL);
1643 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1646 // copy to ensure we do not change avpkt
1647 AVPacket tmp = *avpkt;
1648 int did_split = av_packet_split_side_data(&tmp);
1649 apply_param_change(avctx, &tmp);
1652 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1653 if (ret >= 0 && *got_frame_ptr) {
1654 avctx->frame_number++;
1655 frame->pkt_dts = avpkt->dts;
1656 frame->best_effort_timestamp = guess_correct_pts(avctx,
1659 if (frame->format == AV_SAMPLE_FMT_NONE)
1660 frame->format = avctx->sample_fmt;
1661 if (!frame->channel_layout)
1662 frame->channel_layout = avctx->channel_layout;
1663 if (!frame->channels)
1664 frame->channels = avctx->channels;
1665 if (!frame->sample_rate)
1666 frame->sample_rate = avctx->sample_rate;
1669 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1670 if(side && side_size>=10) {
1671 avctx->internal->skip_samples = AV_RL32(side);
1672 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1673 avctx->internal->skip_samples);
1675 if (avctx->internal->skip_samples) {
1676 if(frame->nb_samples <= avctx->internal->skip_samples){
1678 avctx->internal->skip_samples -= frame->nb_samples;
1679 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1680 avctx->internal->skip_samples);
1682 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1683 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1684 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1685 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1686 (AVRational){1, avctx->sample_rate},
1687 avctx->pkt_timebase);
1688 if(frame->pkt_pts!=AV_NOPTS_VALUE)
1689 frame->pkt_pts += diff_ts;
1690 if(frame->pkt_dts!=AV_NOPTS_VALUE)
1691 frame->pkt_dts += diff_ts;
1692 if (frame->pkt_duration >= diff_ts)
1693 frame->pkt_duration -= diff_ts;
1695 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1697 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1698 avctx->internal->skip_samples, frame->nb_samples);
1699 frame->nb_samples -= avctx->internal->skip_samples;
1700 avctx->internal->skip_samples = 0;
1706 ff_packet_free_side_data(&tmp);
1714 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1720 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1721 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1722 return AVERROR(EINVAL);
1727 avcodec_get_subtitle_defaults(sub);
1728 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
1729 sub->pts = av_rescale_q(avpkt->pts,
1730 avctx->pkt_timebase, AV_TIME_BASE_Q);
1731 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1733 avctx->frame_number++;
1737 void avsubtitle_free(AVSubtitle *sub)
1741 for (i = 0; i < sub->num_rects; i++) {
1742 av_freep(&sub->rects[i]->pict.data[0]);
1743 av_freep(&sub->rects[i]->pict.data[1]);
1744 av_freep(&sub->rects[i]->pict.data[2]);
1745 av_freep(&sub->rects[i]->pict.data[3]);
1746 av_freep(&sub->rects[i]->text);
1747 av_freep(&sub->rects[i]->ass);
1748 av_freep(&sub->rects[i]);
1751 av_freep(&sub->rects);
1753 memset(sub, 0, sizeof(AVSubtitle));
1756 av_cold int avcodec_close(AVCodecContext *avctx)
1758 /* If there is a user-supplied mutex locking routine, call it. */
1759 if (ff_lockmgr_cb) {
1760 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1764 entangled_thread_counter++;
1765 if (entangled_thread_counter != 1) {
1766 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1767 entangled_thread_counter--;
1771 if (avcodec_is_open(avctx)) {
1772 if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1773 entangled_thread_counter --;
1774 ff_frame_thread_encoder_free(avctx);
1775 entangled_thread_counter ++;
1777 if (HAVE_THREADS && avctx->thread_opaque)
1778 ff_thread_free(avctx);
1779 if (avctx->codec && avctx->codec->close)
1780 avctx->codec->close(avctx);
1781 avcodec_default_free_buffers(avctx);
1782 avctx->coded_frame = NULL;
1783 avctx->internal->byte_buffer_size = 0;
1784 av_freep(&avctx->internal->byte_buffer);
1785 av_freep(&avctx->internal);
1788 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1789 av_opt_free(avctx->priv_data);
1791 av_freep(&avctx->priv_data);
1792 if (av_codec_is_encoder(avctx->codec))
1793 av_freep(&avctx->extradata);
1794 avctx->codec = NULL;
1795 avctx->active_thread_type = 0;
1796 entangled_thread_counter--;
1798 /* Release any user-supplied mutex. */
1799 if (ff_lockmgr_cb) {
1800 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1805 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1808 //This is for future deprecatec codec ids, its empty since
1809 //last major bump but will fill up again over time, please don't remove it
1810 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
1811 default : return id;
1815 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1817 AVCodec *p, *experimental = NULL;
1819 id= remap_deprecated_codec_id(id);
1821 if (av_codec_is_encoder(p) && p->id == id) {
1822 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1829 return experimental;
1832 AVCodec *avcodec_find_encoder_by_name(const char *name)
1839 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1846 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1848 AVCodec *p, *experimental=NULL;
1850 id= remap_deprecated_codec_id(id);
1852 if (av_codec_is_decoder(p) && p->id == id) {
1853 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1860 return experimental;
1863 AVCodec *avcodec_find_decoder_by_name(const char *name)
1870 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1877 const char *avcodec_get_name(enum AVCodecID id)
1879 const AVCodecDescriptor *cd;
1882 if (id == AV_CODEC_ID_NONE)
1884 cd = avcodec_descriptor_get(id);
1887 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1888 codec = avcodec_find_decoder(id);
1891 codec = avcodec_find_encoder(id);
1894 return "unknown_codec";
1897 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1899 int i, len, ret = 0;
1901 #define IS_PRINT(x) \
1902 (((x) >= '0' && (x) <= '9') || \
1903 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1904 ((x) == '.' || (x) == ' ' || (x) == '-'))
1906 for (i = 0; i < 4; i++) {
1907 len = snprintf(buf, buf_size,
1908 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1910 buf_size = buf_size > len ? buf_size - len : 0;
1917 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1919 const char *codec_type;
1920 const char *codec_name;
1921 const char *profile = NULL;
1924 AVRational display_aspect_ratio;
1926 if (!buf || buf_size <= 0)
1928 codec_type = av_get_media_type_string(enc->codec_type);
1929 codec_name = avcodec_get_name(enc->codec_id);
1930 if (enc->profile != FF_PROFILE_UNKNOWN) {
1934 p = encode ? avcodec_find_encoder(enc->codec_id) :
1935 avcodec_find_decoder(enc->codec_id);
1937 profile = av_get_profile_name(p, enc->profile);
1940 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1941 codec_name, enc->mb_decision ? " (hq)" : "");
1942 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1944 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1945 if (enc->codec_tag) {
1947 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1948 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1949 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1952 switch (enc->codec_type) {
1953 case AVMEDIA_TYPE_VIDEO:
1954 if (enc->pix_fmt != PIX_FMT_NONE) {
1955 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1957 av_get_pix_fmt_name(enc->pix_fmt));
1960 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1962 enc->width, enc->height);
1963 if (enc->sample_aspect_ratio.num) {
1964 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1965 enc->width * enc->sample_aspect_ratio.num,
1966 enc->height * enc->sample_aspect_ratio.den,
1968 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1969 " [SAR %d:%d DAR %d:%d]",
1970 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1971 display_aspect_ratio.num, display_aspect_ratio.den);
1973 if (av_log_get_level() >= AV_LOG_DEBUG) {
1974 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1975 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1977 enc->time_base.num / g, enc->time_base.den / g);
1981 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1982 ", q=%d-%d", enc->qmin, enc->qmax);
1985 case AVMEDIA_TYPE_AUDIO:
1986 if (enc->sample_rate) {
1987 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1988 ", %d Hz", enc->sample_rate);
1990 av_strlcat(buf, ", ", buf_size);
1991 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1992 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1993 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1994 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2001 if (enc->flags & CODEC_FLAG_PASS1)
2002 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2004 if (enc->flags & CODEC_FLAG_PASS2)
2005 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2008 bitrate = get_bit_rate(enc);
2010 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2011 ", %d kb/s", bitrate / 1000);
2015 const char *av_get_profile_name(const AVCodec *codec, int profile)
2018 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2021 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2022 if (p->profile == profile)
2028 unsigned avcodec_version(void)
2030 // av_assert0(AV_CODEC_ID_V410==164);
2031 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2032 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2033 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2034 av_assert0(AV_CODEC_ID_SRT==94216);
2035 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2037 return LIBAVCODEC_VERSION_INT;
2040 const char *avcodec_configuration(void)
2042 return FFMPEG_CONFIGURATION;
2045 const char *avcodec_license(void)
2047 #define LICENSE_PREFIX "libavcodec license: "
2048 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2051 void avcodec_flush_buffers(AVCodecContext *avctx)
2053 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2054 ff_thread_flush(avctx);
2055 else if (avctx->codec->flush)
2056 avctx->codec->flush(avctx);
2058 avctx->pts_correction_last_pts =
2059 avctx->pts_correction_last_dts = INT64_MIN;
2062 static void video_free_buffers(AVCodecContext *s)
2064 AVCodecInternal *avci = s->internal;
2070 if (avci->buffer_count)
2071 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2072 avci->buffer_count);
2073 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
2074 InternalBuffer *buf = &avci->buffer[i];
2075 for (j = 0; j < 4; j++) {
2076 av_freep(&buf->base[j]);
2077 buf->data[j] = NULL;
2080 av_freep(&avci->buffer);
2082 avci->buffer_count = 0;
2085 static void audio_free_buffers(AVCodecContext *avctx)
2087 AVCodecInternal *avci = avctx->internal;
2088 InternalBuffer *buf;
2094 if (buf->extended_data) {
2095 av_free(buf->extended_data[0]);
2096 if (buf->extended_data != buf->data)
2097 av_freep(&buf->extended_data);
2099 av_freep(&avci->buffer);
2102 void avcodec_default_free_buffers(AVCodecContext *avctx)
2104 switch (avctx->codec_type) {
2105 case AVMEDIA_TYPE_VIDEO:
2106 video_free_buffers(avctx);
2108 case AVMEDIA_TYPE_AUDIO:
2109 audio_free_buffers(avctx);
2116 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2119 case AV_CODEC_ID_ADPCM_CT:
2120 case AV_CODEC_ID_ADPCM_IMA_APC:
2121 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2122 case AV_CODEC_ID_ADPCM_IMA_WS:
2123 case AV_CODEC_ID_ADPCM_G722:
2124 case AV_CODEC_ID_ADPCM_YAMAHA:
2126 case AV_CODEC_ID_PCM_ALAW:
2127 case AV_CODEC_ID_PCM_MULAW:
2128 case AV_CODEC_ID_PCM_S8:
2129 case AV_CODEC_ID_PCM_U8:
2130 case AV_CODEC_ID_PCM_ZORK:
2132 case AV_CODEC_ID_PCM_S16BE:
2133 case AV_CODEC_ID_PCM_S16LE:
2134 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2135 case AV_CODEC_ID_PCM_U16BE:
2136 case AV_CODEC_ID_PCM_U16LE:
2138 case AV_CODEC_ID_PCM_S24DAUD:
2139 case AV_CODEC_ID_PCM_S24BE:
2140 case AV_CODEC_ID_PCM_S24LE:
2141 case AV_CODEC_ID_PCM_U24BE:
2142 case AV_CODEC_ID_PCM_U24LE:
2144 case AV_CODEC_ID_PCM_S32BE:
2145 case AV_CODEC_ID_PCM_S32LE:
2146 case AV_CODEC_ID_PCM_U32BE:
2147 case AV_CODEC_ID_PCM_U32LE:
2148 case AV_CODEC_ID_PCM_F32BE:
2149 case AV_CODEC_ID_PCM_F32LE:
2151 case AV_CODEC_ID_PCM_F64BE:
2152 case AV_CODEC_ID_PCM_F64LE:
2159 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2161 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2162 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2163 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2164 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2165 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2166 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2167 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2168 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2169 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2170 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2171 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2173 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2174 return AV_CODEC_ID_NONE;
2175 if (be < 0 || be > 1)
2177 return map[fmt][be];
2180 int av_get_bits_per_sample(enum AVCodecID codec_id)
2183 case AV_CODEC_ID_ADPCM_SBPRO_2:
2185 case AV_CODEC_ID_ADPCM_SBPRO_3:
2187 case AV_CODEC_ID_ADPCM_SBPRO_4:
2188 case AV_CODEC_ID_ADPCM_IMA_WAV:
2189 case AV_CODEC_ID_ADPCM_IMA_QT:
2190 case AV_CODEC_ID_ADPCM_SWF:
2191 case AV_CODEC_ID_ADPCM_MS:
2194 return av_get_exact_bits_per_sample(codec_id);
2198 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2200 int id, sr, ch, ba, tag, bps;
2202 id = avctx->codec_id;
2203 sr = avctx->sample_rate;
2204 ch = avctx->channels;
2205 ba = avctx->block_align;
2206 tag = avctx->codec_tag;
2207 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2209 /* codecs with an exact constant bits per sample */
2210 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2211 return (frame_bytes * 8LL) / (bps * ch);
2212 bps = avctx->bits_per_coded_sample;
2214 /* codecs with a fixed packet duration */
2216 case AV_CODEC_ID_ADPCM_ADX: return 32;
2217 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2218 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2219 case AV_CODEC_ID_AMR_NB:
2220 case AV_CODEC_ID_GSM:
2221 case AV_CODEC_ID_QCELP:
2222 case AV_CODEC_ID_RA_288: return 160;
2223 case AV_CODEC_ID_IMC: return 256;
2224 case AV_CODEC_ID_AMR_WB:
2225 case AV_CODEC_ID_GSM_MS: return 320;
2226 case AV_CODEC_ID_MP1: return 384;
2227 case AV_CODEC_ID_ATRAC1: return 512;
2228 case AV_CODEC_ID_ATRAC3: return 1024;
2229 case AV_CODEC_ID_MP2:
2230 case AV_CODEC_ID_MUSEPACK7: return 1152;
2231 case AV_CODEC_ID_AC3: return 1536;
2235 /* calc from sample rate */
2236 if (id == AV_CODEC_ID_TTA)
2237 return 256 * sr / 245;
2240 /* calc from sample rate and channels */
2241 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2242 return (480 << (sr / 22050)) / ch;
2247 /* calc from block_align */
2248 if (id == AV_CODEC_ID_SIPR) {
2250 case 20: return 160;
2251 case 19: return 144;
2252 case 29: return 288;
2253 case 37: return 480;
2255 } else if (id == AV_CODEC_ID_ILBC) {
2257 case 38: return 160;
2258 case 50: return 240;
2263 if (frame_bytes > 0) {
2264 /* calc from frame_bytes only */
2265 if (id == AV_CODEC_ID_TRUESPEECH)
2266 return 240 * (frame_bytes / 32);
2267 if (id == AV_CODEC_ID_NELLYMOSER)
2268 return 256 * (frame_bytes / 64);
2269 if (id == AV_CODEC_ID_RA_144)
2270 return 160 * (frame_bytes / 20);
2273 /* calc from frame_bytes and bits_per_coded_sample */
2274 if (id == AV_CODEC_ID_ADPCM_G726)
2275 return frame_bytes * 8 / bps;
2279 /* calc from frame_bytes and channels */
2281 case AV_CODEC_ID_ADPCM_4XM:
2282 case AV_CODEC_ID_ADPCM_IMA_ISS:
2283 return (frame_bytes - 4 * ch) * 2 / ch;
2284 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2285 return (frame_bytes - 4) * 2 / ch;
2286 case AV_CODEC_ID_ADPCM_IMA_AMV:
2287 return (frame_bytes - 8) * 2 / ch;
2288 case AV_CODEC_ID_ADPCM_XA:
2289 return (frame_bytes / 128) * 224 / ch;
2290 case AV_CODEC_ID_INTERPLAY_DPCM:
2291 return (frame_bytes - 6 - ch) / ch;
2292 case AV_CODEC_ID_ROQ_DPCM:
2293 return (frame_bytes - 8) / ch;
2294 case AV_CODEC_ID_XAN_DPCM:
2295 return (frame_bytes - 2 * ch) / ch;
2296 case AV_CODEC_ID_MACE3:
2297 return 3 * frame_bytes / ch;
2298 case AV_CODEC_ID_MACE6:
2299 return 6 * frame_bytes / ch;
2300 case AV_CODEC_ID_PCM_LXF:
2301 return 2 * (frame_bytes / (5 * ch));
2305 /* calc from frame_bytes, channels, and codec_tag */
2306 if (id == AV_CODEC_ID_SOL_DPCM) {
2308 return frame_bytes / ch;
2310 return frame_bytes * 2 / ch;
2315 /* calc from frame_bytes, channels, and block_align */
2316 int blocks = frame_bytes / ba;
2317 switch (avctx->codec_id) {
2318 case AV_CODEC_ID_ADPCM_IMA_WAV:
2319 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2320 case AV_CODEC_ID_ADPCM_IMA_DK3:
2321 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2322 case AV_CODEC_ID_ADPCM_IMA_DK4:
2323 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2324 case AV_CODEC_ID_ADPCM_MS:
2325 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2330 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2331 switch (avctx->codec_id) {
2332 case AV_CODEC_ID_PCM_DVD:
2335 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2336 case AV_CODEC_ID_PCM_BLURAY:
2339 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2340 case AV_CODEC_ID_S302M:
2341 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2351 int ff_thread_init(AVCodecContext *s)
2358 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2372 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2375 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2379 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2381 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
2382 "version to the newest one from Git. If the problem still "
2383 "occurs, it means that your file has a feature which has not "
2384 "been implemented.\n", feature);
2386 av_log_ask_for_sample(avc, NULL);
2389 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2391 va_list argument_list;
2393 va_start(argument_list, msg);
2396 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2397 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2398 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2399 "and contact the ffmpeg-devel mailing list.\n");
2401 va_end(argument_list);
2404 static AVHWAccel *first_hwaccel = NULL;
2406 void av_register_hwaccel(AVHWAccel *hwaccel)
2408 AVHWAccel **p = &first_hwaccel;
2412 hwaccel->next = NULL;
2415 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2417 return hwaccel ? hwaccel->next : first_hwaccel;
2420 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
2422 AVHWAccel *hwaccel = NULL;
2424 while ((hwaccel = av_hwaccel_next(hwaccel)))
2425 if (hwaccel->id == codec_id
2426 && hwaccel->pix_fmt == pix_fmt)
2431 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2433 if (ff_lockmgr_cb) {
2434 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2436 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2442 if (ff_lockmgr_cb) {
2443 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2445 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2451 int avpriv_lock_avformat(void)
2453 if (ff_lockmgr_cb) {
2454 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2460 int avpriv_unlock_avformat(void)
2462 if (ff_lockmgr_cb) {
2463 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2469 unsigned int avpriv_toupper4(unsigned int x)
2471 return toupper(x & 0xFF)
2472 + (toupper((x >> 8) & 0xFF) << 8)
2473 + (toupper((x >> 16) & 0xFF) << 16)
2474 + (toupper((x >> 24) & 0xFF) << 24);
2479 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2483 ff_init_buffer_info(avctx, f);
2485 return avctx->get_buffer(avctx, f);
2488 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2490 f->owner->release_buffer(f->owner, f);
2493 void ff_thread_finish_setup(AVCodecContext *avctx)
2497 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2501 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2505 int ff_thread_can_start_frame(AVCodecContext *avctx)
2512 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2514 AVCodec *c= avcodec_find_decoder(codec_id);
2516 c= avcodec_find_encoder(codec_id);
2520 if (codec_id <= AV_CODEC_ID_NONE)
2521 return AVMEDIA_TYPE_UNKNOWN;
2522 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2523 return AVMEDIA_TYPE_VIDEO;
2524 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2525 return AVMEDIA_TYPE_AUDIO;
2526 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2527 return AVMEDIA_TYPE_SUBTITLE;
2529 return AVMEDIA_TYPE_UNKNOWN;
2532 int avcodec_is_open(AVCodecContext *s)
2534 return !!s->internal;