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"
42 #include "frame_thread_encoder.h"
43 #include "audioconvert.h"
45 #include "bytestream.h"
51 static int volatile entangled_thread_counter = 0;
52 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
53 static void *codec_mutex;
54 static void *avformat_mutex;
56 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
61 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
63 ptr = av_realloc(ptr, min_size);
64 /* we could set this to the unmodified min_size but this is safer
65 * if the user lost the ptr and uses NULL now
75 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
80 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
82 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
89 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
91 ff_fast_malloc(ptr, size, min_size, 0);
94 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
97 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
102 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
103 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
106 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
109 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
114 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
115 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
118 /* encoder management */
119 static AVCodec *first_avcodec = NULL;
121 AVCodec *av_codec_next(const AVCodec *c)
126 return first_avcodec;
129 static void avcodec_init(void)
131 static int initialized = 0;
133 if (initialized != 0)
137 ff_dsputil_static_init();
140 int av_codec_is_encoder(const AVCodec *codec)
142 return codec && (codec->encode_sub || codec->encode2);
145 int av_codec_is_decoder(const AVCodec *codec)
147 return codec && codec->decode;
150 void avcodec_register(AVCodec *codec)
160 if (codec->init_static_data)
161 codec->init_static_data(codec);
164 unsigned avcodec_get_edge_width(void)
169 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
171 s->coded_width = width;
172 s->coded_height = height;
173 s->width = -((-width ) >> s->lowres);
174 s->height = -((-height) >> s->lowres);
177 #define INTERNAL_BUFFER_SIZE (32 + 1)
179 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
180 int linesize_align[AV_NUM_DATA_POINTERS])
186 switch (s->pix_fmt) {
187 case AV_PIX_FMT_YUV420P:
188 case AV_PIX_FMT_YUYV422:
189 case AV_PIX_FMT_UYVY422:
190 case AV_PIX_FMT_YUV422P:
191 case AV_PIX_FMT_YUV440P:
192 case AV_PIX_FMT_YUV444P:
193 case AV_PIX_FMT_GBRP:
194 case AV_PIX_FMT_GRAY8:
195 case AV_PIX_FMT_GRAY16BE:
196 case AV_PIX_FMT_GRAY16LE:
197 case AV_PIX_FMT_YUVJ420P:
198 case AV_PIX_FMT_YUVJ422P:
199 case AV_PIX_FMT_YUVJ440P:
200 case AV_PIX_FMT_YUVJ444P:
201 case AV_PIX_FMT_YUVA420P:
202 case AV_PIX_FMT_YUVA422P:
203 case AV_PIX_FMT_YUVA444P:
204 case AV_PIX_FMT_YUV420P9LE:
205 case AV_PIX_FMT_YUV420P9BE:
206 case AV_PIX_FMT_YUV420P10LE:
207 case AV_PIX_FMT_YUV420P10BE:
208 case AV_PIX_FMT_YUV420P12LE:
209 case AV_PIX_FMT_YUV420P12BE:
210 case AV_PIX_FMT_YUV420P14LE:
211 case AV_PIX_FMT_YUV420P14BE:
212 case AV_PIX_FMT_YUV422P9LE:
213 case AV_PIX_FMT_YUV422P9BE:
214 case AV_PIX_FMT_YUV422P10LE:
215 case AV_PIX_FMT_YUV422P10BE:
216 case AV_PIX_FMT_YUV422P12LE:
217 case AV_PIX_FMT_YUV422P12BE:
218 case AV_PIX_FMT_YUV422P14LE:
219 case AV_PIX_FMT_YUV422P14BE:
220 case AV_PIX_FMT_YUV444P9LE:
221 case AV_PIX_FMT_YUV444P9BE:
222 case AV_PIX_FMT_YUV444P10LE:
223 case AV_PIX_FMT_YUV444P10BE:
224 case AV_PIX_FMT_YUV444P12LE:
225 case AV_PIX_FMT_YUV444P12BE:
226 case AV_PIX_FMT_YUV444P14LE:
227 case AV_PIX_FMT_YUV444P14BE:
228 case AV_PIX_FMT_GBRP9LE:
229 case AV_PIX_FMT_GBRP9BE:
230 case AV_PIX_FMT_GBRP10LE:
231 case AV_PIX_FMT_GBRP10BE:
232 case AV_PIX_FMT_GBRP12LE:
233 case AV_PIX_FMT_GBRP12BE:
234 case AV_PIX_FMT_GBRP14LE:
235 case AV_PIX_FMT_GBRP14BE:
236 w_align = 16; //FIXME assume 16 pixel per macroblock
237 h_align = 16 * 2; // interlaced needs 2 macroblocks height
239 case AV_PIX_FMT_YUV411P:
240 case AV_PIX_FMT_UYYVYY411:
244 case AV_PIX_FMT_YUV410P:
245 if (s->codec_id == AV_CODEC_ID_SVQ1) {
250 case AV_PIX_FMT_RGB555:
251 if (s->codec_id == AV_CODEC_ID_RPZA) {
256 case AV_PIX_FMT_PAL8:
257 case AV_PIX_FMT_BGR8:
258 case AV_PIX_FMT_RGB8:
259 if (s->codec_id == AV_CODEC_ID_SMC) {
264 case AV_PIX_FMT_BGR24:
265 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
266 (s->codec_id == AV_CODEC_ID_ZLIB)) {
277 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
278 w_align = FFMAX(w_align, 8);
281 *width = FFALIGN(*width, w_align);
282 *height = FFALIGN(*height, h_align);
283 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
284 // some of the optimized chroma MC reads one line too much
285 // which is also done in mpeg decoders with lowres > 0
288 for (i = 0; i < 4; i++)
289 linesize_align[i] = STRIDE_ALIGN;
292 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
294 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
295 int chroma_shift = desc->log2_chroma_w;
296 int linesize_align[AV_NUM_DATA_POINTERS];
299 avcodec_align_dimensions2(s, width, height, linesize_align);
300 align = FFMAX(linesize_align[0], linesize_align[3]);
301 linesize_align[1] <<= chroma_shift;
302 linesize_align[2] <<= chroma_shift;
303 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
304 *width = FFALIGN(*width, align);
307 void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
310 frame->pkt_pts = s->pkt->pts;
311 frame->pkt_pos = s->pkt->pos;
312 frame->pkt_duration = s->pkt->duration;
314 frame->pkt_pts = AV_NOPTS_VALUE;
316 frame->pkt_duration = 0;
318 frame->reordered_opaque = s->reordered_opaque;
320 switch (s->codec->type) {
321 case AVMEDIA_TYPE_VIDEO:
322 frame->width = s->width;
323 frame->height = s->height;
324 frame->format = s->pix_fmt;
325 frame->sample_aspect_ratio = s->sample_aspect_ratio;
327 case AVMEDIA_TYPE_AUDIO:
328 frame->sample_rate = s->sample_rate;
329 frame->format = s->sample_fmt;
330 frame->channel_layout = s->channel_layout;
331 frame->channels = s->channels;
336 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
337 enum AVSampleFormat sample_fmt, const uint8_t *buf,
338 int buf_size, int align)
340 int ch, planar, needed_size, ret = 0;
342 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
343 frame->nb_samples, sample_fmt,
345 if (buf_size < needed_size)
346 return AVERROR(EINVAL);
348 planar = av_sample_fmt_is_planar(sample_fmt);
349 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
350 if (!(frame->extended_data = av_mallocz(nb_channels *
351 sizeof(*frame->extended_data))))
352 return AVERROR(ENOMEM);
354 frame->extended_data = frame->data;
357 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
358 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
359 sample_fmt, align)) < 0) {
360 if (frame->extended_data != frame->data)
361 av_freep(&frame->extended_data);
364 if (frame->extended_data != frame->data) {
365 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
366 frame->data[ch] = frame->extended_data[ch];
372 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
374 AVCodecInternal *avci = avctx->internal;
378 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
379 frame->nb_samples, avctx->sample_fmt,
382 return AVERROR(EINVAL);
384 /* allocate InternalBuffer if needed */
386 avci->buffer = av_mallocz(sizeof(InternalBuffer));
388 return AVERROR(ENOMEM);
392 /* if there is a previously-used internal buffer, check its size and
393 * channel count to see if we can reuse it */
394 if (buf->extended_data) {
395 /* if current buffer is too small, free it */
396 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
397 av_free(buf->extended_data[0]);
398 if (buf->extended_data != buf->data)
399 av_free(buf->extended_data);
400 buf->extended_data = NULL;
403 /* if number of channels has changed, reset and/or free extended data
404 * pointers but leave data buffer in buf->data[0] for reuse */
405 if (buf->nb_channels != avctx->channels) {
406 if (buf->extended_data != buf->data)
407 av_free(buf->extended_data);
408 buf->extended_data = NULL;
412 /* if there is no previous buffer or the previous buffer cannot be used
413 * as-is, allocate a new buffer and/or rearrange the channel pointers */
414 if (!buf->extended_data) {
416 if (!(buf->data[0] = av_mallocz(buf_size)))
417 return AVERROR(ENOMEM);
418 buf->audio_data_size = buf_size;
420 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
421 avctx->sample_fmt, buf->data[0],
422 buf->audio_data_size, 0)))
425 if (frame->extended_data == frame->data)
426 buf->extended_data = buf->data;
428 buf->extended_data = frame->extended_data;
429 memcpy(buf->data, frame->data, sizeof(frame->data));
430 buf->linesize[0] = frame->linesize[0];
431 buf->nb_channels = avctx->channels;
433 /* copy InternalBuffer info to the AVFrame */
434 frame->extended_data = buf->extended_data;
435 frame->linesize[0] = buf->linesize[0];
436 memcpy(frame->data, buf->data, sizeof(frame->data));
439 frame->type = FF_BUFFER_TYPE_INTERNAL;
440 ff_init_buffer_info(avctx, frame);
442 if (avctx->debug & FF_DEBUG_BUFFERS)
443 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
444 "internal audio buffer used\n", frame);
449 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
455 AVCodecInternal *avci = s->internal;
457 if (pic->data[0] != NULL) {
458 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
461 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
462 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
466 if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
467 av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
472 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
473 sizeof(InternalBuffer));
476 buf = &avci->buffer[avci->buffer_count];
478 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
479 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
480 av_freep(&buf->base[i]);
486 int h_chroma_shift, v_chroma_shift;
491 int stride_align[AV_NUM_DATA_POINTERS];
492 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
493 const int pixel_size = desc->comp[0].step_minus1 + 1;
495 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
497 avcodec_align_dimensions2(s, &w, &h, stride_align);
499 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
505 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
506 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
507 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
508 // increase alignment of w for next try (rhs gives the lowest bit set in w)
512 for (i = 0; i < 4; i++)
513 unaligned |= picture.linesize[i] % stride_align[i];
516 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
520 for (i = 0; i < 3 && picture.data[i + 1]; i++)
521 size[i] = picture.data[i + 1] - picture.data[i];
522 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
524 memset(buf->base, 0, sizeof(buf->base));
525 memset(buf->data, 0, sizeof(buf->data));
527 for (i = 0; i < 4 && size[i]; i++) {
528 const int h_shift = i == 0 ? 0 : h_chroma_shift;
529 const int v_shift = i == 0 ? 0 : v_chroma_shift;
531 buf->linesize[i] = picture.linesize[i];
533 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
534 if (buf->base[i] == NULL)
535 return AVERROR(ENOMEM);
536 memset(buf->base[i], 128, size[i]);
538 // no edge if EDGE EMU or not planar YUV
539 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
540 buf->data[i] = buf->base[i];
542 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
544 for (; i < AV_NUM_DATA_POINTERS; i++) {
545 buf->base[i] = buf->data[i] = NULL;
546 buf->linesize[i] = 0;
548 if (size[1] && !size[2])
549 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
550 buf->width = s->width;
551 buf->height = s->height;
552 buf->pix_fmt = s->pix_fmt;
554 pic->type = FF_BUFFER_TYPE_INTERNAL;
556 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
557 pic->base[i] = buf->base[i];
558 pic->data[i] = buf->data[i];
559 pic->linesize[i] = buf->linesize[i];
561 pic->extended_data = pic->data;
562 avci->buffer_count++;
563 pic->width = buf->width;
564 pic->height = buf->height;
565 pic->format = buf->pix_fmt;
567 ff_init_buffer_info(s, pic);
569 if (s->debug & FF_DEBUG_BUFFERS)
570 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
571 "buffers used\n", pic, avci->buffer_count);
576 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
578 switch (avctx->codec_type) {
579 case AVMEDIA_TYPE_VIDEO:
580 return video_get_buffer(avctx, frame);
581 case AVMEDIA_TYPE_AUDIO:
582 return audio_get_buffer(avctx, frame);
588 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
591 InternalBuffer *buf, *last;
592 AVCodecInternal *avci = s->internal;
594 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
596 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
597 assert(avci->buffer_count);
600 buf = NULL; /* avoids warning */
601 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
602 buf = &avci->buffer[i];
603 if (buf->data[0] == pic->data[0])
606 av_assert0(i < avci->buffer_count);
607 avci->buffer_count--;
608 last = &avci->buffer[avci->buffer_count];
611 FFSWAP(InternalBuffer, *buf, *last);
614 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
616 // pic->base[i]=NULL;
618 if (s->debug & FF_DEBUG_BUFFERS)
619 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
620 "buffers used\n", pic, avci->buffer_count);
623 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
628 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
630 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
631 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
632 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
633 s->release_buffer(s, pic);
636 ff_init_buffer_info(s, pic);
638 /* If no picture return a new buffer */
639 if (pic->data[0] == NULL) {
640 /* We will copy from buffer, so must be readable */
641 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
642 return s->get_buffer(s, pic);
645 assert(s->pix_fmt == pic->format);
647 /* If internal buffer type return the same buffer */
648 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
653 * Not internal type and reget_buffer not overridden, emulate cr buffer
656 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
657 pic->data[i] = pic->base[i] = NULL;
659 /* Allocate new frame */
660 if ((ret = s->get_buffer(s, pic)))
662 /* Copy image data from old buffer to new buffer */
663 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
665 s->release_buffer(s, &temp_pic); // Release old frame
669 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
673 for (i = 0; i < count; i++) {
674 int r = func(c, (char *)arg + i * size);
681 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
685 for (i = 0; i < count; i++) {
686 int r = func(c, arg, i, 0);
693 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
695 while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
700 void avcodec_get_frame_defaults(AVFrame *frame)
702 #if LIBAVCODEC_VERSION_MAJOR >= 55
703 // extended_data should explicitly be freed when needed, this code is unsafe currently
704 // also this is not compatible to the <55 ABI/API
705 if (frame->extended_data != frame->data && 0)
706 av_freep(&frame->extended_data);
709 memset(frame, 0, sizeof(AVFrame));
714 frame->best_effort_timestamp = AV_NOPTS_VALUE;
715 frame->pkt_duration = 0;
717 frame->key_frame = 1;
718 frame->sample_aspect_ratio = (AVRational) {0, 1 };
719 frame->format = -1; /* unknown */
720 frame->extended_data = frame->data;
723 AVFrame *avcodec_alloc_frame(void)
725 AVFrame *frame = av_malloc(sizeof(AVFrame));
730 frame->extended_data = NULL;
731 avcodec_get_frame_defaults(frame);
736 void avcodec_free_frame(AVFrame **frame)
740 if (!frame || !*frame)
745 if (f->extended_data != f->data)
746 av_freep(&f->extended_data);
751 #define MAKE_ACCESSORS(str, name, type, field) \
752 type av_##name##_get_##field(const str *s) { return s->field; } \
753 void av_##name##_set_##field(str *s, type v) { s->field = v; }
755 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
756 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
757 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
758 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
759 MAKE_ACCESSORS(AVFrame, frame, int, channels)
760 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
761 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
762 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
764 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
765 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
767 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
769 memset(sub, 0, sizeof(*sub));
770 sub->pts = AV_NOPTS_VALUE;
773 static int get_bit_rate(AVCodecContext *ctx)
778 switch (ctx->codec_type) {
779 case AVMEDIA_TYPE_VIDEO:
780 case AVMEDIA_TYPE_DATA:
781 case AVMEDIA_TYPE_SUBTITLE:
782 case AVMEDIA_TYPE_ATTACHMENT:
783 bit_rate = ctx->bit_rate;
785 case AVMEDIA_TYPE_AUDIO:
786 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
787 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
796 #if FF_API_AVCODEC_OPEN
797 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
799 return avcodec_open2(avctx, codec, NULL);
803 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
807 entangled_thread_counter--;
808 /* Release any user-supplied mutex. */
810 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
813 ret = avcodec_open2(avctx, codec, options);
815 /* If there is a user-supplied mutex locking routine, call it. */
817 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
820 entangled_thread_counter++;
824 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
827 AVDictionary *tmp = NULL;
829 if (avcodec_is_open(avctx))
832 if ((!codec && !avctx->codec)) {
833 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
834 return AVERROR(EINVAL);
836 if ((codec && avctx->codec && codec != avctx->codec)) {
837 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
838 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
839 return AVERROR(EINVAL);
842 codec = avctx->codec;
844 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
845 return AVERROR(EINVAL);
848 av_dict_copy(&tmp, *options, 0);
850 /* If there is a user-supplied mutex locking routine, call it. */
852 if ((ret = (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) < 0)
856 entangled_thread_counter++;
857 if (entangled_thread_counter != 1) {
858 av_log(avctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
859 ret = AVERROR(EINVAL);
863 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
864 if (!avctx->internal) {
865 ret = AVERROR(ENOMEM);
869 if (codec->priv_data_size > 0) {
870 if (!avctx->priv_data) {
871 avctx->priv_data = av_mallocz(codec->priv_data_size);
872 if (!avctx->priv_data) {
873 ret = AVERROR(ENOMEM);
876 if (codec->priv_class) {
877 *(const AVClass **)avctx->priv_data = codec->priv_class;
878 av_opt_set_defaults(avctx->priv_data);
881 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
884 avctx->priv_data = NULL;
886 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
889 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
890 if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
892 if (avctx->coded_width && avctx->coded_height)
893 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
894 else if (avctx->width && avctx->height)
895 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
898 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
899 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
900 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
901 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
902 avcodec_set_dimensions(avctx, 0, 0);
905 /* if the decoder init function was already called previously,
906 * free the already allocated subtitle_header before overwriting it */
907 if (av_codec_is_decoder(codec))
908 av_freep(&avctx->subtitle_header);
910 #define SANE_NB_CHANNELS 128U
911 if (avctx->channels > SANE_NB_CHANNELS) {
912 ret = AVERROR(EINVAL);
916 avctx->codec = codec;
917 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
918 avctx->codec_id == AV_CODEC_ID_NONE) {
919 avctx->codec_type = codec->type;
920 avctx->codec_id = codec->id;
922 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
923 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
924 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
925 ret = AVERROR(EINVAL);
928 avctx->frame_number = 0;
929 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
931 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
932 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
933 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
935 av_log(NULL, AV_LOG_ERROR,
936 "The %s '%s' is experimental but experimental codecs are not enabled, "
937 "add '-strict %d' if you want to use it.\n",
938 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
939 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
940 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
941 av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
942 codec_string, codec2->name);
943 ret = AVERROR_EXPERIMENTAL;
947 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
948 (!avctx->time_base.num || !avctx->time_base.den)) {
949 avctx->time_base.num = 1;
950 avctx->time_base.den = avctx->sample_rate;
954 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
957 entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
958 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
959 entangled_thread_counter++;
964 if (HAVE_THREADS && !avctx->thread_opaque
965 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
966 ret = ff_thread_init(avctx);
971 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
972 avctx->thread_count = 1;
974 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
975 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
976 avctx->codec->max_lowres);
977 ret = AVERROR(EINVAL);
981 if (av_codec_is_encoder(avctx->codec)) {
983 if (avctx->codec->sample_fmts) {
984 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
985 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
987 if (avctx->channels == 1 &&
988 av_get_planar_sample_fmt(avctx->sample_fmt) ==
989 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
990 avctx->sample_fmt = avctx->codec->sample_fmts[i];
994 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
996 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
997 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
998 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
999 ret = AVERROR(EINVAL);
1003 if (avctx->codec->pix_fmts) {
1004 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1005 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1007 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1008 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1009 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1011 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1012 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1013 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1014 ret = AVERROR(EINVAL);
1018 if (avctx->codec->supported_samplerates) {
1019 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1020 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1022 if (avctx->codec->supported_samplerates[i] == 0) {
1023 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1024 avctx->sample_rate);
1025 ret = AVERROR(EINVAL);
1029 if (avctx->codec->channel_layouts) {
1030 if (!avctx->channel_layout) {
1031 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1033 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1034 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1036 if (avctx->codec->channel_layouts[i] == 0) {
1038 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1039 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1040 ret = AVERROR(EINVAL);
1045 if (avctx->channel_layout && avctx->channels) {
1046 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1047 if (channels != avctx->channels) {
1049 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1050 av_log(avctx, AV_LOG_ERROR,
1051 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1052 buf, channels, avctx->channels);
1053 ret = AVERROR(EINVAL);
1056 } else if (avctx->channel_layout) {
1057 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1061 avctx->pts_correction_num_faulty_pts =
1062 avctx->pts_correction_num_faulty_dts = 0;
1063 avctx->pts_correction_last_pts =
1064 avctx->pts_correction_last_dts = INT64_MIN;
1066 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1067 || avctx->internal->frame_thread_encoder)) {
1068 ret = avctx->codec->init(avctx);
1076 if (av_codec_is_decoder(avctx->codec)) {
1077 if (!avctx->bit_rate)
1078 avctx->bit_rate = get_bit_rate(avctx);
1079 /* validate channel layout from the decoder */
1080 if (avctx->channel_layout) {
1081 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1082 if (!avctx->channels)
1083 avctx->channels = channels;
1084 else if (channels != avctx->channels) {
1086 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1087 av_log(avctx, AV_LOG_WARNING,
1088 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1089 "ignoring specified channel layout\n",
1090 buf, channels, avctx->channels);
1091 avctx->channel_layout = 0;
1096 entangled_thread_counter--;
1098 /* Release any user-supplied mutex. */
1099 if (ff_lockmgr_cb) {
1100 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1103 av_dict_free(options);
1110 av_freep(&avctx->priv_data);
1111 av_freep(&avctx->internal);
1112 avctx->codec = NULL;
1116 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1118 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1119 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1120 return AVERROR(EINVAL);
1124 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1125 if (!avpkt->data || avpkt->size < size) {
1126 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1127 avpkt->data = avctx->internal->byte_buffer;
1128 avpkt->size = avctx->internal->byte_buffer_size;
1129 avpkt->destruct = NULL;
1134 void *destruct = avpkt->destruct;
1136 if (avpkt->size < size) {
1137 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1138 return AVERROR(EINVAL);
1141 av_init_packet(avpkt);
1142 avpkt->destruct = destruct;
1146 int ret = av_new_packet(avpkt, size);
1148 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1153 int ff_alloc_packet(AVPacket *avpkt, int size)
1155 return ff_alloc_packet2(NULL, avpkt, size);
1159 * Pad last frame with silence.
1161 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1163 AVFrame *frame = NULL;
1164 uint8_t *buf = NULL;
1167 if (!(frame = avcodec_alloc_frame()))
1168 return AVERROR(ENOMEM);
1171 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1172 s->frame_size, s->sample_fmt, 0)) < 0)
1175 if (!(buf = av_malloc(ret))) {
1176 ret = AVERROR(ENOMEM);
1180 frame->nb_samples = s->frame_size;
1181 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1184 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1185 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1187 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1188 frame->nb_samples - src->nb_samples,
1189 s->channels, s->sample_fmt)) < 0)
1197 if (frame->extended_data != frame->data)
1198 av_freep(&frame->extended_data);
1204 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1206 const AVFrame *frame,
1207 int *got_packet_ptr)
1210 AVFrame *padded_frame = NULL;
1212 AVPacket user_pkt = *avpkt;
1213 int needs_realloc = !user_pkt.data;
1215 *got_packet_ptr = 0;
1217 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1218 av_free_packet(avpkt);
1219 av_init_packet(avpkt);
1223 /* ensure that extended_data is properly set */
1224 if (frame && !frame->extended_data) {
1225 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1226 avctx->channels > AV_NUM_DATA_POINTERS) {
1227 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1228 "with more than %d channels, but extended_data is not set.\n",
1229 AV_NUM_DATA_POINTERS);
1230 return AVERROR(EINVAL);
1232 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1235 tmp.extended_data = tmp.data;
1239 /* check for valid frame size */
1241 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1242 if (frame->nb_samples > avctx->frame_size) {
1243 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1244 return AVERROR(EINVAL);
1246 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1247 if (frame->nb_samples < avctx->frame_size &&
1248 !avctx->internal->last_audio_frame) {
1249 ret = pad_last_frame(avctx, &padded_frame, frame);
1253 frame = padded_frame;
1254 avctx->internal->last_audio_frame = 1;
1257 if (frame->nb_samples != avctx->frame_size) {
1258 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1259 ret = AVERROR(EINVAL);
1265 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1267 if (*got_packet_ptr) {
1268 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1269 if (avpkt->pts == AV_NOPTS_VALUE)
1270 avpkt->pts = frame->pts;
1271 if (!avpkt->duration)
1272 avpkt->duration = ff_samples_to_time_base(avctx,
1275 avpkt->dts = avpkt->pts;
1280 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1282 if (user_pkt.data) {
1283 if (user_pkt.size >= avpkt->size) {
1284 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1286 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1287 avpkt->size = user_pkt.size;
1290 avpkt->data = user_pkt.data;
1291 avpkt->destruct = user_pkt.destruct;
1293 if (av_dup_packet(avpkt) < 0) {
1294 ret = AVERROR(ENOMEM);
1300 if (needs_realloc && avpkt->data) {
1301 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1303 avpkt->data = new_data;
1306 avctx->frame_number++;
1309 if (ret < 0 || !*got_packet_ptr) {
1310 av_free_packet(avpkt);
1311 av_init_packet(avpkt);
1315 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1316 * this needs to be moved to the encoders, but for now we can do it
1317 * here to simplify things */
1318 avpkt->flags |= AV_PKT_FLAG_KEY;
1322 av_freep(&padded_frame->data[0]);
1323 if (padded_frame->extended_data != padded_frame->data)
1324 av_freep(&padded_frame->extended_data);
1325 av_freep(&padded_frame);
1331 #if FF_API_OLD_ENCODE_AUDIO
1332 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1333 uint8_t *buf, int buf_size,
1334 const short *samples)
1337 AVFrame frame0 = { 0 };
1339 int ret, samples_size, got_packet;
1341 av_init_packet(&pkt);
1343 pkt.size = buf_size;
1347 avcodec_get_frame_defaults(frame);
1349 if (avctx->frame_size) {
1350 frame->nb_samples = avctx->frame_size;
1352 /* if frame_size is not set, the number of samples must be
1353 * calculated from the buffer size */
1355 if (!av_get_bits_per_sample(avctx->codec_id)) {
1356 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1357 "support this codec\n");
1358 return AVERROR(EINVAL);
1360 nb_samples = (int64_t)buf_size * 8 /
1361 (av_get_bits_per_sample(avctx->codec_id) *
1363 if (nb_samples >= INT_MAX)
1364 return AVERROR(EINVAL);
1365 frame->nb_samples = nb_samples;
1368 /* it is assumed that the samples buffer is large enough based on the
1369 * relevant parameters */
1370 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1372 avctx->sample_fmt, 1);
1373 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1375 (const uint8_t *)samples,
1379 /* fabricate frame pts from sample count.
1380 * this is needed because the avcodec_encode_audio() API does not have
1381 * a way for the user to provide pts */
1382 if (avctx->sample_rate && avctx->time_base.num)
1383 frame->pts = ff_samples_to_time_base(avctx,
1384 avctx->internal->sample_count);
1386 frame->pts = AV_NOPTS_VALUE;
1387 avctx->internal->sample_count += frame->nb_samples;
1393 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1394 if (!ret && got_packet && avctx->coded_frame) {
1395 avctx->coded_frame->pts = pkt.pts;
1396 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1398 /* free any side data since we cannot return it */
1399 ff_packet_free_side_data(&pkt);
1401 if (frame && frame->extended_data != frame->data)
1402 av_freep(&frame->extended_data);
1404 return ret ? ret : pkt.size;
1409 #if FF_API_OLD_ENCODE_VIDEO
1410 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1411 const AVFrame *pict)
1414 int ret, got_packet = 0;
1416 if (buf_size < FF_MIN_BUFFER_SIZE) {
1417 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1421 av_init_packet(&pkt);
1423 pkt.size = buf_size;
1425 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1426 if (!ret && got_packet && avctx->coded_frame) {
1427 avctx->coded_frame->pts = pkt.pts;
1428 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1431 /* free any side data since we cannot return it */
1432 if (pkt.side_data_elems > 0) {
1434 for (i = 0; i < pkt.side_data_elems; i++)
1435 av_free(pkt.side_data[i].data);
1436 av_freep(&pkt.side_data);
1437 pkt.side_data_elems = 0;
1440 return ret ? ret : pkt.size;
1445 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1447 const AVFrame *frame,
1448 int *got_packet_ptr)
1451 AVPacket user_pkt = *avpkt;
1452 int needs_realloc = !user_pkt.data;
1454 *got_packet_ptr = 0;
1456 if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1457 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1459 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1460 av_free_packet(avpkt);
1461 av_init_packet(avpkt);
1466 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1467 return AVERROR(EINVAL);
1469 av_assert0(avctx->codec->encode2);
1471 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1472 av_assert0(ret <= 0);
1474 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1476 if (user_pkt.data) {
1477 if (user_pkt.size >= avpkt->size) {
1478 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1480 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1481 avpkt->size = user_pkt.size;
1484 avpkt->data = user_pkt.data;
1485 avpkt->destruct = user_pkt.destruct;
1487 if (av_dup_packet(avpkt) < 0) {
1488 ret = AVERROR(ENOMEM);
1494 if (!*got_packet_ptr)
1496 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1497 avpkt->pts = avpkt->dts = frame->pts;
1499 if (needs_realloc && avpkt->data &&
1500 avpkt->destruct == av_destruct_packet) {
1501 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1503 avpkt->data = new_data;
1506 avctx->frame_number++;
1509 if (ret < 0 || !*got_packet_ptr)
1510 av_free_packet(avpkt);
1516 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1517 const AVSubtitle *sub)
1520 if (sub->start_display_time) {
1521 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1525 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1526 avctx->frame_number++;
1531 * Attempt to guess proper monotonic timestamps for decoded video frames
1532 * which might have incorrect times. Input timestamps may wrap around, in
1533 * which case the output will as well.
1535 * @param pts the pts field of the decoded AVPacket, as passed through
1537 * @param dts the dts field of the decoded AVPacket
1538 * @return one of the input values, may be AV_NOPTS_VALUE
1540 static int64_t guess_correct_pts(AVCodecContext *ctx,
1541 int64_t reordered_pts, int64_t dts)
1543 int64_t pts = AV_NOPTS_VALUE;
1545 if (dts != AV_NOPTS_VALUE) {
1546 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1547 ctx->pts_correction_last_dts = dts;
1549 if (reordered_pts != AV_NOPTS_VALUE) {
1550 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1551 ctx->pts_correction_last_pts = reordered_pts;
1553 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1554 && reordered_pts != AV_NOPTS_VALUE)
1555 pts = reordered_pts;
1562 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1565 const uint8_t *data;
1568 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1571 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1572 if (!data || size < 4)
1574 flags = bytestream_get_le32(&data);
1576 if (size < 4) /* Required for any of the changes */
1578 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1579 avctx->channels = bytestream_get_le32(&data);
1582 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1585 avctx->channel_layout = bytestream_get_le64(&data);
1590 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1591 avctx->sample_rate = bytestream_get_le32(&data);
1594 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1597 avctx->width = bytestream_get_le32(&data);
1598 avctx->height = bytestream_get_le32(&data);
1599 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1604 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
1607 const uint8_t *side_metadata;
1610 av_dict_free(&avctx->metadata);
1611 side_metadata = av_packet_get_side_data(avctx->pkt,
1612 AV_PKT_DATA_STRINGS_METADATA, &size);
1615 end = side_metadata + size;
1616 while (side_metadata < end) {
1617 const uint8_t *key = side_metadata;
1618 const uint8_t *val = side_metadata + strlen(key) + 1;
1619 int ret = av_dict_set(&frame->metadata, key, val, 0);
1622 side_metadata = val + strlen(val) + 1;
1625 avctx->metadata = frame->metadata;
1629 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1630 int *got_picture_ptr,
1631 const AVPacket *avpkt)
1634 // copy to ensure we do not change avpkt
1635 AVPacket tmp = *avpkt;
1637 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1638 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1639 return AVERROR(EINVAL);
1642 *got_picture_ptr = 0;
1643 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1644 return AVERROR(EINVAL);
1646 avcodec_get_frame_defaults(picture);
1648 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1649 int did_split = av_packet_split_side_data(&tmp);
1650 apply_param_change(avctx, &tmp);
1652 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1653 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1656 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1658 picture->pkt_dts = avpkt->dts;
1660 if(!avctx->has_b_frames){
1661 picture->pkt_pos = avpkt->pos;
1663 //FIXME these should be under if(!avctx->has_b_frames)
1664 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1665 if (!picture->width) picture->width = avctx->width;
1666 if (!picture->height) picture->height = avctx->height;
1667 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1669 add_metadata_from_side_data(avctx, picture);
1671 emms_c(); //needed to avoid an emms_c() call before every return;
1675 ff_packet_free_side_data(&tmp);
1680 if (*got_picture_ptr){
1681 avctx->frame_number++;
1682 picture->best_effort_timestamp = guess_correct_pts(avctx,
1689 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1690 * make sure it's set correctly */
1691 picture->extended_data = picture->data;
1696 #if FF_API_OLD_DECODE_AUDIO
1697 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1698 int *frame_size_ptr,
1702 int ret, got_frame = 0;
1704 if (avctx->get_buffer != avcodec_default_get_buffer) {
1705 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1706 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1707 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1708 "avcodec_decode_audio4()\n");
1709 avctx->get_buffer = avcodec_default_get_buffer;
1710 avctx->release_buffer = avcodec_default_release_buffer;
1713 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1715 if (ret >= 0 && got_frame) {
1717 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1718 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1720 avctx->sample_fmt, 1);
1721 if (*frame_size_ptr < data_size) {
1722 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1723 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1724 return AVERROR(EINVAL);
1727 memcpy(samples, frame.extended_data[0], plane_size);
1729 if (planar && avctx->channels > 1) {
1730 uint8_t *out = ((uint8_t *)samples) + plane_size;
1731 for (ch = 1; ch < avctx->channels; ch++) {
1732 memcpy(out, frame.extended_data[ch], plane_size);
1736 *frame_size_ptr = data_size;
1738 *frame_size_ptr = 0;
1745 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1748 const AVPacket *avpkt)
1750 int planar, channels;
1755 if (!avpkt->data && avpkt->size) {
1756 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1757 return AVERROR(EINVAL);
1759 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1760 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1761 return AVERROR(EINVAL);
1764 avcodec_get_frame_defaults(frame);
1766 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1769 // copy to ensure we do not change avpkt
1770 AVPacket tmp = *avpkt;
1771 int did_split = av_packet_split_side_data(&tmp);
1772 apply_param_change(avctx, &tmp);
1775 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1776 if (ret >= 0 && *got_frame_ptr) {
1777 avctx->frame_number++;
1778 frame->pkt_dts = avpkt->dts;
1779 frame->best_effort_timestamp = guess_correct_pts(avctx,
1782 if (frame->format == AV_SAMPLE_FMT_NONE)
1783 frame->format = avctx->sample_fmt;
1784 if (!frame->channel_layout)
1785 frame->channel_layout = avctx->channel_layout;
1786 if (!frame->channels)
1787 frame->channels = avctx->channels;
1788 if (!frame->sample_rate)
1789 frame->sample_rate = avctx->sample_rate;
1791 add_metadata_from_side_data(avctx, frame);
1793 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1794 if(side && side_size>=10) {
1795 avctx->internal->skip_samples = AV_RL32(side);
1796 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1797 avctx->internal->skip_samples);
1799 if (avctx->internal->skip_samples) {
1800 if(frame->nb_samples <= avctx->internal->skip_samples){
1802 avctx->internal->skip_samples -= frame->nb_samples;
1803 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1804 avctx->internal->skip_samples);
1806 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1807 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1808 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1809 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1810 (AVRational){1, avctx->sample_rate},
1811 avctx->pkt_timebase);
1812 if(frame->pkt_pts!=AV_NOPTS_VALUE)
1813 frame->pkt_pts += diff_ts;
1814 if(frame->pkt_dts!=AV_NOPTS_VALUE)
1815 frame->pkt_dts += diff_ts;
1816 if (frame->pkt_duration >= diff_ts)
1817 frame->pkt_duration -= diff_ts;
1819 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1821 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1822 avctx->internal->skip_samples, frame->nb_samples);
1823 frame->nb_samples -= avctx->internal->skip_samples;
1824 avctx->internal->skip_samples = 0;
1830 ff_packet_free_side_data(&tmp);
1836 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1837 * make sure it's set correctly; assume decoders that actually use
1838 * extended_data are doing it correctly */
1839 if (*got_frame_ptr) {
1840 planar = av_sample_fmt_is_planar(frame->format);
1841 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1842 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1843 frame->extended_data = frame->data;
1845 frame->extended_data = NULL;
1851 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1857 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1858 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1859 return AVERROR(EINVAL);
1863 avcodec_get_subtitle_defaults(sub);
1866 AVPacket tmp = *avpkt;
1867 int did_split = av_packet_split_side_data(&tmp);
1868 //apply_param_change(avctx, &tmp);
1872 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
1873 sub->pts = av_rescale_q(avpkt->pts,
1874 avctx->pkt_timebase, AV_TIME_BASE_Q);
1875 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &tmp);
1879 ff_packet_free_side_data(&tmp);
1885 avctx->frame_number++;
1891 void avsubtitle_free(AVSubtitle *sub)
1895 for (i = 0; i < sub->num_rects; i++) {
1896 av_freep(&sub->rects[i]->pict.data[0]);
1897 av_freep(&sub->rects[i]->pict.data[1]);
1898 av_freep(&sub->rects[i]->pict.data[2]);
1899 av_freep(&sub->rects[i]->pict.data[3]);
1900 av_freep(&sub->rects[i]->text);
1901 av_freep(&sub->rects[i]->ass);
1902 av_freep(&sub->rects[i]);
1905 av_freep(&sub->rects);
1907 memset(sub, 0, sizeof(AVSubtitle));
1910 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
1914 entangled_thread_counter--;
1915 /* Release any user-supplied mutex. */
1916 if (ff_lockmgr_cb) {
1917 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1920 ret = avcodec_close(avctx);
1922 /* If there is a user-supplied mutex locking routine, call it. */
1923 if (ff_lockmgr_cb) {
1924 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1927 entangled_thread_counter++;
1931 av_cold int avcodec_close(AVCodecContext *avctx)
1933 /* If there is a user-supplied mutex locking routine, call it. */
1934 if (ff_lockmgr_cb) {
1935 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1939 entangled_thread_counter++;
1940 if (entangled_thread_counter != 1) {
1941 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1942 entangled_thread_counter--;
1946 if (avcodec_is_open(avctx)) {
1947 if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1948 entangled_thread_counter --;
1949 ff_frame_thread_encoder_free(avctx);
1950 entangled_thread_counter ++;
1952 if (HAVE_THREADS && avctx->thread_opaque)
1953 ff_thread_free(avctx);
1954 if (avctx->codec && avctx->codec->close)
1955 avctx->codec->close(avctx);
1956 avcodec_default_free_buffers(avctx);
1957 avctx->coded_frame = NULL;
1958 avctx->internal->byte_buffer_size = 0;
1959 av_freep(&avctx->internal->byte_buffer);
1960 av_freep(&avctx->internal);
1961 av_dict_free(&avctx->metadata);
1964 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1965 av_opt_free(avctx->priv_data);
1967 av_freep(&avctx->priv_data);
1968 if (av_codec_is_encoder(avctx->codec))
1969 av_freep(&avctx->extradata);
1970 avctx->codec = NULL;
1971 avctx->active_thread_type = 0;
1972 entangled_thread_counter--;
1974 /* Release any user-supplied mutex. */
1975 if (ff_lockmgr_cb) {
1976 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1981 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1984 //This is for future deprecatec codec ids, its empty since
1985 //last major bump but will fill up again over time, please don't remove it
1986 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
1987 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
1988 default : return id;
1992 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1994 AVCodec *p, *experimental = NULL;
1996 id= remap_deprecated_codec_id(id);
1998 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2000 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2007 return experimental;
2010 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2012 return find_encdec(id, 1);
2015 AVCodec *avcodec_find_encoder_by_name(const char *name)
2022 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2029 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2031 return find_encdec(id, 0);
2034 AVCodec *avcodec_find_decoder_by_name(const char *name)
2041 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2048 const char *avcodec_get_name(enum AVCodecID id)
2050 const AVCodecDescriptor *cd;
2053 if (id == AV_CODEC_ID_NONE)
2055 cd = avcodec_descriptor_get(id);
2058 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2059 codec = avcodec_find_decoder(id);
2062 codec = avcodec_find_encoder(id);
2065 return "unknown_codec";
2068 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2070 int i, len, ret = 0;
2072 #define IS_PRINT(x) \
2073 (((x) >= '0' && (x) <= '9') || \
2074 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2075 ((x) == '.' || (x) == ' ' || (x) == '-'))
2077 for (i = 0; i < 4; i++) {
2078 len = snprintf(buf, buf_size,
2079 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
2081 buf_size = buf_size > len ? buf_size - len : 0;
2088 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2090 const char *codec_type;
2091 const char *codec_name;
2092 const char *profile = NULL;
2095 AVRational display_aspect_ratio;
2097 if (!buf || buf_size <= 0)
2099 codec_type = av_get_media_type_string(enc->codec_type);
2100 codec_name = avcodec_get_name(enc->codec_id);
2101 if (enc->profile != FF_PROFILE_UNKNOWN) {
2105 p = encode ? avcodec_find_encoder(enc->codec_id) :
2106 avcodec_find_decoder(enc->codec_id);
2108 profile = av_get_profile_name(p, enc->profile);
2111 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2112 codec_name, enc->mb_decision ? " (hq)" : "");
2113 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2115 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2116 if (enc->codec_tag) {
2118 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2119 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2120 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2123 switch (enc->codec_type) {
2124 case AVMEDIA_TYPE_VIDEO:
2125 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2126 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2128 av_get_pix_fmt_name(enc->pix_fmt));
2131 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2133 enc->width, enc->height);
2134 if (enc->sample_aspect_ratio.num) {
2135 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2136 enc->width * enc->sample_aspect_ratio.num,
2137 enc->height * enc->sample_aspect_ratio.den,
2139 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2140 " [SAR %d:%d DAR %d:%d]",
2141 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2142 display_aspect_ratio.num, display_aspect_ratio.den);
2144 if (av_log_get_level() >= AV_LOG_DEBUG) {
2145 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2146 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2148 enc->time_base.num / g, enc->time_base.den / g);
2152 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2153 ", q=%d-%d", enc->qmin, enc->qmax);
2156 case AVMEDIA_TYPE_AUDIO:
2157 if (enc->sample_rate) {
2158 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2159 ", %d Hz", enc->sample_rate);
2161 av_strlcat(buf, ", ", buf_size);
2162 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2163 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2164 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2165 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2172 if (enc->flags & CODEC_FLAG_PASS1)
2173 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2175 if (enc->flags & CODEC_FLAG_PASS2)
2176 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2179 bitrate = get_bit_rate(enc);
2181 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2182 ", %d kb/s", bitrate / 1000);
2186 const char *av_get_profile_name(const AVCodec *codec, int profile)
2189 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2192 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2193 if (p->profile == profile)
2199 unsigned avcodec_version(void)
2201 // av_assert0(AV_CODEC_ID_V410==164);
2202 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2203 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2204 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2205 av_assert0(AV_CODEC_ID_SRT==94216);
2206 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2208 return LIBAVCODEC_VERSION_INT;
2211 const char *avcodec_configuration(void)
2213 return FFMPEG_CONFIGURATION;
2216 const char *avcodec_license(void)
2218 #define LICENSE_PREFIX "libavcodec license: "
2219 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2222 void avcodec_flush_buffers(AVCodecContext *avctx)
2224 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2225 ff_thread_flush(avctx);
2226 else if (avctx->codec->flush)
2227 avctx->codec->flush(avctx);
2229 avctx->pts_correction_last_pts =
2230 avctx->pts_correction_last_dts = INT64_MIN;
2233 static void video_free_buffers(AVCodecContext *s)
2235 AVCodecInternal *avci = s->internal;
2241 if (avci->buffer_count)
2242 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2243 avci->buffer_count);
2244 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
2245 InternalBuffer *buf = &avci->buffer[i];
2246 for (j = 0; j < 4; j++) {
2247 av_freep(&buf->base[j]);
2248 buf->data[j] = NULL;
2251 av_freep(&avci->buffer);
2253 avci->buffer_count = 0;
2256 static void audio_free_buffers(AVCodecContext *avctx)
2258 AVCodecInternal *avci = avctx->internal;
2259 InternalBuffer *buf;
2265 if (buf->extended_data) {
2266 av_free(buf->extended_data[0]);
2267 if (buf->extended_data != buf->data)
2268 av_freep(&buf->extended_data);
2270 av_freep(&avci->buffer);
2273 void avcodec_default_free_buffers(AVCodecContext *avctx)
2275 switch (avctx->codec_type) {
2276 case AVMEDIA_TYPE_VIDEO:
2277 video_free_buffers(avctx);
2279 case AVMEDIA_TYPE_AUDIO:
2280 audio_free_buffers(avctx);
2287 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2290 case AV_CODEC_ID_ADPCM_CT:
2291 case AV_CODEC_ID_ADPCM_IMA_APC:
2292 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2293 case AV_CODEC_ID_ADPCM_IMA_WS:
2294 case AV_CODEC_ID_ADPCM_G722:
2295 case AV_CODEC_ID_ADPCM_YAMAHA:
2297 case AV_CODEC_ID_PCM_ALAW:
2298 case AV_CODEC_ID_PCM_MULAW:
2299 case AV_CODEC_ID_PCM_S8:
2300 case AV_CODEC_ID_PCM_U8:
2301 case AV_CODEC_ID_PCM_ZORK:
2303 case AV_CODEC_ID_PCM_S16BE:
2304 case AV_CODEC_ID_PCM_S16LE:
2305 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2306 case AV_CODEC_ID_PCM_U16BE:
2307 case AV_CODEC_ID_PCM_U16LE:
2309 case AV_CODEC_ID_PCM_S24DAUD:
2310 case AV_CODEC_ID_PCM_S24BE:
2311 case AV_CODEC_ID_PCM_S24LE:
2312 case AV_CODEC_ID_PCM_U24BE:
2313 case AV_CODEC_ID_PCM_U24LE:
2315 case AV_CODEC_ID_PCM_S32BE:
2316 case AV_CODEC_ID_PCM_S32LE:
2317 case AV_CODEC_ID_PCM_U32BE:
2318 case AV_CODEC_ID_PCM_U32LE:
2319 case AV_CODEC_ID_PCM_F32BE:
2320 case AV_CODEC_ID_PCM_F32LE:
2322 case AV_CODEC_ID_PCM_F64BE:
2323 case AV_CODEC_ID_PCM_F64LE:
2330 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2332 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2333 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2334 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2335 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2336 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2337 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2338 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2339 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2340 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2341 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2342 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2344 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2345 return AV_CODEC_ID_NONE;
2346 if (be < 0 || be > 1)
2348 return map[fmt][be];
2351 int av_get_bits_per_sample(enum AVCodecID codec_id)
2354 case AV_CODEC_ID_ADPCM_SBPRO_2:
2356 case AV_CODEC_ID_ADPCM_SBPRO_3:
2358 case AV_CODEC_ID_ADPCM_SBPRO_4:
2359 case AV_CODEC_ID_ADPCM_IMA_WAV:
2360 case AV_CODEC_ID_ADPCM_IMA_QT:
2361 case AV_CODEC_ID_ADPCM_SWF:
2362 case AV_CODEC_ID_ADPCM_MS:
2365 return av_get_exact_bits_per_sample(codec_id);
2369 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2371 int id, sr, ch, ba, tag, bps;
2373 id = avctx->codec_id;
2374 sr = avctx->sample_rate;
2375 ch = avctx->channels;
2376 ba = avctx->block_align;
2377 tag = avctx->codec_tag;
2378 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2380 /* codecs with an exact constant bits per sample */
2381 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2382 return (frame_bytes * 8LL) / (bps * ch);
2383 bps = avctx->bits_per_coded_sample;
2385 /* codecs with a fixed packet duration */
2387 case AV_CODEC_ID_ADPCM_ADX: return 32;
2388 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2389 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2390 case AV_CODEC_ID_AMR_NB:
2391 case AV_CODEC_ID_GSM:
2392 case AV_CODEC_ID_QCELP:
2393 case AV_CODEC_ID_RA_288: return 160;
2394 case AV_CODEC_ID_AMR_WB:
2395 case AV_CODEC_ID_GSM_MS: return 320;
2396 case AV_CODEC_ID_MP1: return 384;
2397 case AV_CODEC_ID_ATRAC1: return 512;
2398 case AV_CODEC_ID_ATRAC3: return 1024;
2399 case AV_CODEC_ID_MP2:
2400 case AV_CODEC_ID_MUSEPACK7: return 1152;
2401 case AV_CODEC_ID_AC3: return 1536;
2405 /* calc from sample rate */
2406 if (id == AV_CODEC_ID_TTA)
2407 return 256 * sr / 245;
2410 /* calc from sample rate and channels */
2411 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2412 return (480 << (sr / 22050)) / ch;
2417 /* calc from block_align */
2418 if (id == AV_CODEC_ID_SIPR) {
2420 case 20: return 160;
2421 case 19: return 144;
2422 case 29: return 288;
2423 case 37: return 480;
2425 } else if (id == AV_CODEC_ID_ILBC) {
2427 case 38: return 160;
2428 case 50: return 240;
2433 if (frame_bytes > 0) {
2434 /* calc from frame_bytes only */
2435 if (id == AV_CODEC_ID_TRUESPEECH)
2436 return 240 * (frame_bytes / 32);
2437 if (id == AV_CODEC_ID_NELLYMOSER)
2438 return 256 * (frame_bytes / 64);
2439 if (id == AV_CODEC_ID_RA_144)
2440 return 160 * (frame_bytes / 20);
2443 /* calc from frame_bytes and bits_per_coded_sample */
2444 if (id == AV_CODEC_ID_ADPCM_G726)
2445 return frame_bytes * 8 / bps;
2449 /* calc from frame_bytes and channels */
2451 case AV_CODEC_ID_ADPCM_4XM:
2452 case AV_CODEC_ID_ADPCM_IMA_ISS:
2453 return (frame_bytes - 4 * ch) * 2 / ch;
2454 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2455 return (frame_bytes - 4) * 2 / ch;
2456 case AV_CODEC_ID_ADPCM_IMA_AMV:
2457 return (frame_bytes - 8) * 2 / ch;
2458 case AV_CODEC_ID_ADPCM_XA:
2459 return (frame_bytes / 128) * 224 / ch;
2460 case AV_CODEC_ID_INTERPLAY_DPCM:
2461 return (frame_bytes - 6 - ch) / ch;
2462 case AV_CODEC_ID_ROQ_DPCM:
2463 return (frame_bytes - 8) / ch;
2464 case AV_CODEC_ID_XAN_DPCM:
2465 return (frame_bytes - 2 * ch) / ch;
2466 case AV_CODEC_ID_MACE3:
2467 return 3 * frame_bytes / ch;
2468 case AV_CODEC_ID_MACE6:
2469 return 6 * frame_bytes / ch;
2470 case AV_CODEC_ID_PCM_LXF:
2471 return 2 * (frame_bytes / (5 * ch));
2472 case AV_CODEC_ID_IMC:
2473 return 4 * frame_bytes / ch;
2477 /* calc from frame_bytes, channels, and codec_tag */
2478 if (id == AV_CODEC_ID_SOL_DPCM) {
2480 return frame_bytes / ch;
2482 return frame_bytes * 2 / ch;
2487 /* calc from frame_bytes, channels, and block_align */
2488 int blocks = frame_bytes / ba;
2489 switch (avctx->codec_id) {
2490 case AV_CODEC_ID_ADPCM_IMA_WAV:
2491 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2492 case AV_CODEC_ID_ADPCM_IMA_DK3:
2493 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2494 case AV_CODEC_ID_ADPCM_IMA_DK4:
2495 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2496 case AV_CODEC_ID_ADPCM_MS:
2497 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2502 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2503 switch (avctx->codec_id) {
2504 case AV_CODEC_ID_PCM_DVD:
2507 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2508 case AV_CODEC_ID_PCM_BLURAY:
2511 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2512 case AV_CODEC_ID_S302M:
2513 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2523 int ff_thread_init(AVCodecContext *s)
2530 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2544 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2547 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2551 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2553 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2554 "version to the newest one from Git. If the problem still "
2555 "occurs, it means that your file has a feature which has not "
2556 "been implemented.\n", feature);
2558 av_log_ask_for_sample(avc, NULL);
2561 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2563 va_list argument_list;
2565 va_start(argument_list, msg);
2568 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2569 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2570 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2571 "and contact the ffmpeg-devel mailing list.\n");
2573 va_end(argument_list);
2576 static AVHWAccel *first_hwaccel = NULL;
2578 void av_register_hwaccel(AVHWAccel *hwaccel)
2580 AVHWAccel **p = &first_hwaccel;
2584 hwaccel->next = NULL;
2587 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2589 return hwaccel ? hwaccel->next : first_hwaccel;
2592 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2594 AVHWAccel *hwaccel = NULL;
2596 while ((hwaccel = av_hwaccel_next(hwaccel)))
2597 if (hwaccel->id == codec_id
2598 && hwaccel->pix_fmt == pix_fmt)
2603 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2605 if (ff_lockmgr_cb) {
2606 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2608 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2614 if (ff_lockmgr_cb) {
2615 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2617 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2623 int avpriv_lock_avformat(void)
2625 if (ff_lockmgr_cb) {
2626 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2632 int avpriv_unlock_avformat(void)
2634 if (ff_lockmgr_cb) {
2635 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2641 unsigned int avpriv_toupper4(unsigned int x)
2643 return toupper(x & 0xFF)
2644 + (toupper((x >> 8) & 0xFF) << 8)
2645 + (toupper((x >> 16) & 0xFF) << 16)
2646 + (toupper((x >> 24) & 0xFF) << 24);
2651 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2655 ff_init_buffer_info(avctx, f);
2657 return avctx->get_buffer(avctx, f);
2660 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2662 f->owner->release_buffer(f->owner, f);
2665 void ff_thread_finish_setup(AVCodecContext *avctx)
2669 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2673 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2677 int ff_thread_can_start_frame(AVCodecContext *avctx)
2684 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2686 AVCodec *c= avcodec_find_decoder(codec_id);
2688 c= avcodec_find_encoder(codec_id);
2692 if (codec_id <= AV_CODEC_ID_NONE)
2693 return AVMEDIA_TYPE_UNKNOWN;
2694 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2695 return AVMEDIA_TYPE_VIDEO;
2696 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2697 return AVMEDIA_TYPE_AUDIO;
2698 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2699 return AVMEDIA_TYPE_SUBTITLE;
2701 return AVMEDIA_TYPE_UNKNOWN;
2704 int avcodec_is_open(AVCodecContext *s)
2706 return !!s->internal;