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 ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
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");
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 "%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, "Or 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_DECODE_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 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1647 int did_split = av_packet_split_side_data(&tmp);
1648 apply_param_change(avctx, &tmp);
1650 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1651 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1654 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1656 picture->pkt_dts = avpkt->dts;
1658 if(!avctx->has_b_frames){
1659 picture->pkt_pos = avpkt->pos;
1661 //FIXME these should be under if(!avctx->has_b_frames)
1662 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1663 if (!picture->width) picture->width = avctx->width;
1664 if (!picture->height) picture->height = avctx->height;
1665 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
1667 add_metadata_from_side_data(avctx, picture);
1669 emms_c(); //needed to avoid an emms_c() call before every return;
1673 ff_packet_free_side_data(&tmp);
1678 if (*got_picture_ptr){
1679 avctx->frame_number++;
1680 picture->best_effort_timestamp = guess_correct_pts(avctx,
1687 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1688 * make sure it's set correctly */
1689 picture->extended_data = picture->data;
1694 #if FF_API_OLD_DECODE_AUDIO
1695 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1696 int *frame_size_ptr,
1700 int ret, got_frame = 0;
1702 if (avctx->get_buffer != avcodec_default_get_buffer) {
1703 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1704 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1705 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1706 "avcodec_decode_audio4()\n");
1707 avctx->get_buffer = avcodec_default_get_buffer;
1708 avctx->release_buffer = avcodec_default_release_buffer;
1711 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1713 if (ret >= 0 && got_frame) {
1715 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1716 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1718 avctx->sample_fmt, 1);
1719 if (*frame_size_ptr < data_size) {
1720 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1721 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1722 return AVERROR(EINVAL);
1725 memcpy(samples, frame.extended_data[0], plane_size);
1727 if (planar && avctx->channels > 1) {
1728 uint8_t *out = ((uint8_t *)samples) + plane_size;
1729 for (ch = 1; ch < avctx->channels; ch++) {
1730 memcpy(out, frame.extended_data[ch], plane_size);
1734 *frame_size_ptr = data_size;
1736 *frame_size_ptr = 0;
1743 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1746 const AVPacket *avpkt)
1748 int planar, channels;
1753 if (!avpkt->data && avpkt->size) {
1754 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1755 return AVERROR(EINVAL);
1757 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1758 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1759 return AVERROR(EINVAL);
1762 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1765 // copy to ensure we do not change avpkt
1766 AVPacket tmp = *avpkt;
1767 int did_split = av_packet_split_side_data(&tmp);
1768 apply_param_change(avctx, &tmp);
1771 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1772 if (ret >= 0 && *got_frame_ptr) {
1773 avctx->frame_number++;
1774 frame->pkt_dts = avpkt->dts;
1775 frame->best_effort_timestamp = guess_correct_pts(avctx,
1778 if (frame->format == AV_SAMPLE_FMT_NONE)
1779 frame->format = avctx->sample_fmt;
1780 if (!frame->channel_layout)
1781 frame->channel_layout = avctx->channel_layout;
1782 if (!frame->channels)
1783 frame->channels = avctx->channels;
1784 if (!frame->sample_rate)
1785 frame->sample_rate = avctx->sample_rate;
1787 add_metadata_from_side_data(avctx, frame);
1789 side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1790 if(side && side_size>=10) {
1791 avctx->internal->skip_samples = AV_RL32(side);
1792 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1793 avctx->internal->skip_samples);
1795 if (avctx->internal->skip_samples) {
1796 if(frame->nb_samples <= avctx->internal->skip_samples){
1798 avctx->internal->skip_samples -= frame->nb_samples;
1799 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1800 avctx->internal->skip_samples);
1802 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1803 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1804 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1805 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1806 (AVRational){1, avctx->sample_rate},
1807 avctx->pkt_timebase);
1808 if(frame->pkt_pts!=AV_NOPTS_VALUE)
1809 frame->pkt_pts += diff_ts;
1810 if(frame->pkt_dts!=AV_NOPTS_VALUE)
1811 frame->pkt_dts += diff_ts;
1812 if (frame->pkt_duration >= diff_ts)
1813 frame->pkt_duration -= diff_ts;
1815 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1817 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1818 avctx->internal->skip_samples, frame->nb_samples);
1819 frame->nb_samples -= avctx->internal->skip_samples;
1820 avctx->internal->skip_samples = 0;
1826 ff_packet_free_side_data(&tmp);
1832 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1833 * make sure it's set correctly; assume decoders that actually use
1834 * extended_data are doing it correctly */
1835 if (*got_frame_ptr) {
1836 planar = av_sample_fmt_is_planar(frame->format);
1837 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1838 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1839 frame->extended_data = frame->data;
1841 frame->extended_data = NULL;
1847 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1853 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1854 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1855 return AVERROR(EINVAL);
1860 avcodec_get_subtitle_defaults(sub);
1861 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
1862 sub->pts = av_rescale_q(avpkt->pts,
1863 avctx->pkt_timebase, AV_TIME_BASE_Q);
1864 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1866 avctx->frame_number++;
1870 void avsubtitle_free(AVSubtitle *sub)
1874 for (i = 0; i < sub->num_rects; i++) {
1875 av_freep(&sub->rects[i]->pict.data[0]);
1876 av_freep(&sub->rects[i]->pict.data[1]);
1877 av_freep(&sub->rects[i]->pict.data[2]);
1878 av_freep(&sub->rects[i]->pict.data[3]);
1879 av_freep(&sub->rects[i]->text);
1880 av_freep(&sub->rects[i]->ass);
1881 av_freep(&sub->rects[i]);
1884 av_freep(&sub->rects);
1886 memset(sub, 0, sizeof(AVSubtitle));
1889 av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
1893 entangled_thread_counter--;
1894 /* Release any user-supplied mutex. */
1895 if (ff_lockmgr_cb) {
1896 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1899 ret = avcodec_close(avctx);
1901 /* If there is a user-supplied mutex locking routine, call it. */
1902 if (ff_lockmgr_cb) {
1903 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1906 entangled_thread_counter++;
1910 av_cold int avcodec_close(AVCodecContext *avctx)
1912 /* If there is a user-supplied mutex locking routine, call it. */
1913 if (ff_lockmgr_cb) {
1914 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1918 entangled_thread_counter++;
1919 if (entangled_thread_counter != 1) {
1920 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1921 entangled_thread_counter--;
1925 if (avcodec_is_open(avctx)) {
1926 if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1927 entangled_thread_counter --;
1928 ff_frame_thread_encoder_free(avctx);
1929 entangled_thread_counter ++;
1931 if (HAVE_THREADS && avctx->thread_opaque)
1932 ff_thread_free(avctx);
1933 if (avctx->codec && avctx->codec->close)
1934 avctx->codec->close(avctx);
1935 avcodec_default_free_buffers(avctx);
1936 avctx->coded_frame = NULL;
1937 avctx->internal->byte_buffer_size = 0;
1938 av_freep(&avctx->internal->byte_buffer);
1939 av_freep(&avctx->internal);
1940 av_dict_free(&avctx->metadata);
1943 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1944 av_opt_free(avctx->priv_data);
1946 av_freep(&avctx->priv_data);
1947 if (av_codec_is_encoder(avctx->codec))
1948 av_freep(&avctx->extradata);
1949 avctx->codec = NULL;
1950 avctx->active_thread_type = 0;
1951 entangled_thread_counter--;
1953 /* Release any user-supplied mutex. */
1954 if (ff_lockmgr_cb) {
1955 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1960 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1963 //This is for future deprecatec codec ids, its empty since
1964 //last major bump but will fill up again over time, please don't remove it
1965 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
1966 case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
1967 default : return id;
1971 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1973 AVCodec *p, *experimental = NULL;
1975 id= remap_deprecated_codec_id(id);
1977 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1979 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1986 return experimental;
1989 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1991 return find_encdec(id, 1);
1994 AVCodec *avcodec_find_encoder_by_name(const char *name)
2001 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2008 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2010 return find_encdec(id, 0);
2013 AVCodec *avcodec_find_decoder_by_name(const char *name)
2020 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2027 const char *avcodec_get_name(enum AVCodecID id)
2029 const AVCodecDescriptor *cd;
2032 if (id == AV_CODEC_ID_NONE)
2034 cd = avcodec_descriptor_get(id);
2037 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2038 codec = avcodec_find_decoder(id);
2041 codec = avcodec_find_encoder(id);
2044 return "unknown_codec";
2047 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2049 int i, len, ret = 0;
2051 #define IS_PRINT(x) \
2052 (((x) >= '0' && (x) <= '9') || \
2053 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2054 ((x) == '.' || (x) == ' ' || (x) == '-'))
2056 for (i = 0; i < 4; i++) {
2057 len = snprintf(buf, buf_size,
2058 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
2060 buf_size = buf_size > len ? buf_size - len : 0;
2067 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2069 const char *codec_type;
2070 const char *codec_name;
2071 const char *profile = NULL;
2074 AVRational display_aspect_ratio;
2076 if (!buf || buf_size <= 0)
2078 codec_type = av_get_media_type_string(enc->codec_type);
2079 codec_name = avcodec_get_name(enc->codec_id);
2080 if (enc->profile != FF_PROFILE_UNKNOWN) {
2084 p = encode ? avcodec_find_encoder(enc->codec_id) :
2085 avcodec_find_decoder(enc->codec_id);
2087 profile = av_get_profile_name(p, enc->profile);
2090 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
2091 codec_name, enc->mb_decision ? " (hq)" : "");
2092 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2094 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2095 if (enc->codec_tag) {
2097 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2098 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2099 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2102 switch (enc->codec_type) {
2103 case AVMEDIA_TYPE_VIDEO:
2104 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2105 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2107 av_get_pix_fmt_name(enc->pix_fmt));
2110 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2112 enc->width, enc->height);
2113 if (enc->sample_aspect_ratio.num) {
2114 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2115 enc->width * enc->sample_aspect_ratio.num,
2116 enc->height * enc->sample_aspect_ratio.den,
2118 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2119 " [SAR %d:%d DAR %d:%d]",
2120 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2121 display_aspect_ratio.num, display_aspect_ratio.den);
2123 if (av_log_get_level() >= AV_LOG_DEBUG) {
2124 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2125 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2127 enc->time_base.num / g, enc->time_base.den / g);
2131 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2132 ", q=%d-%d", enc->qmin, enc->qmax);
2135 case AVMEDIA_TYPE_AUDIO:
2136 if (enc->sample_rate) {
2137 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2138 ", %d Hz", enc->sample_rate);
2140 av_strlcat(buf, ", ", buf_size);
2141 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2142 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2143 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2144 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2151 if (enc->flags & CODEC_FLAG_PASS1)
2152 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2154 if (enc->flags & CODEC_FLAG_PASS2)
2155 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2158 bitrate = get_bit_rate(enc);
2160 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2161 ", %d kb/s", bitrate / 1000);
2165 const char *av_get_profile_name(const AVCodec *codec, int profile)
2168 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2171 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2172 if (p->profile == profile)
2178 unsigned avcodec_version(void)
2180 // av_assert0(AV_CODEC_ID_V410==164);
2181 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2182 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2183 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2184 av_assert0(AV_CODEC_ID_SRT==94216);
2185 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2187 return LIBAVCODEC_VERSION_INT;
2190 const char *avcodec_configuration(void)
2192 return FFMPEG_CONFIGURATION;
2195 const char *avcodec_license(void)
2197 #define LICENSE_PREFIX "libavcodec license: "
2198 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2201 void avcodec_flush_buffers(AVCodecContext *avctx)
2203 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2204 ff_thread_flush(avctx);
2205 else if (avctx->codec->flush)
2206 avctx->codec->flush(avctx);
2208 avctx->pts_correction_last_pts =
2209 avctx->pts_correction_last_dts = INT64_MIN;
2212 static void video_free_buffers(AVCodecContext *s)
2214 AVCodecInternal *avci = s->internal;
2220 if (avci->buffer_count)
2221 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2222 avci->buffer_count);
2223 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
2224 InternalBuffer *buf = &avci->buffer[i];
2225 for (j = 0; j < 4; j++) {
2226 av_freep(&buf->base[j]);
2227 buf->data[j] = NULL;
2230 av_freep(&avci->buffer);
2232 avci->buffer_count = 0;
2235 static void audio_free_buffers(AVCodecContext *avctx)
2237 AVCodecInternal *avci = avctx->internal;
2238 InternalBuffer *buf;
2244 if (buf->extended_data) {
2245 av_free(buf->extended_data[0]);
2246 if (buf->extended_data != buf->data)
2247 av_freep(&buf->extended_data);
2249 av_freep(&avci->buffer);
2252 void avcodec_default_free_buffers(AVCodecContext *avctx)
2254 switch (avctx->codec_type) {
2255 case AVMEDIA_TYPE_VIDEO:
2256 video_free_buffers(avctx);
2258 case AVMEDIA_TYPE_AUDIO:
2259 audio_free_buffers(avctx);
2266 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2269 case AV_CODEC_ID_ADPCM_CT:
2270 case AV_CODEC_ID_ADPCM_IMA_APC:
2271 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2272 case AV_CODEC_ID_ADPCM_IMA_WS:
2273 case AV_CODEC_ID_ADPCM_G722:
2274 case AV_CODEC_ID_ADPCM_YAMAHA:
2276 case AV_CODEC_ID_PCM_ALAW:
2277 case AV_CODEC_ID_PCM_MULAW:
2278 case AV_CODEC_ID_PCM_S8:
2279 case AV_CODEC_ID_PCM_U8:
2280 case AV_CODEC_ID_PCM_ZORK:
2282 case AV_CODEC_ID_PCM_S16BE:
2283 case AV_CODEC_ID_PCM_S16LE:
2284 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2285 case AV_CODEC_ID_PCM_U16BE:
2286 case AV_CODEC_ID_PCM_U16LE:
2288 case AV_CODEC_ID_PCM_S24DAUD:
2289 case AV_CODEC_ID_PCM_S24BE:
2290 case AV_CODEC_ID_PCM_S24LE:
2291 case AV_CODEC_ID_PCM_U24BE:
2292 case AV_CODEC_ID_PCM_U24LE:
2294 case AV_CODEC_ID_PCM_S32BE:
2295 case AV_CODEC_ID_PCM_S32LE:
2296 case AV_CODEC_ID_PCM_U32BE:
2297 case AV_CODEC_ID_PCM_U32LE:
2298 case AV_CODEC_ID_PCM_F32BE:
2299 case AV_CODEC_ID_PCM_F32LE:
2301 case AV_CODEC_ID_PCM_F64BE:
2302 case AV_CODEC_ID_PCM_F64LE:
2309 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2311 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2312 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2313 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2314 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2315 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2316 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2317 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
2318 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2319 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2320 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2321 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2323 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2324 return AV_CODEC_ID_NONE;
2325 if (be < 0 || be > 1)
2327 return map[fmt][be];
2330 int av_get_bits_per_sample(enum AVCodecID codec_id)
2333 case AV_CODEC_ID_ADPCM_SBPRO_2:
2335 case AV_CODEC_ID_ADPCM_SBPRO_3:
2337 case AV_CODEC_ID_ADPCM_SBPRO_4:
2338 case AV_CODEC_ID_ADPCM_IMA_WAV:
2339 case AV_CODEC_ID_ADPCM_IMA_QT:
2340 case AV_CODEC_ID_ADPCM_SWF:
2341 case AV_CODEC_ID_ADPCM_MS:
2344 return av_get_exact_bits_per_sample(codec_id);
2348 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2350 int id, sr, ch, ba, tag, bps;
2352 id = avctx->codec_id;
2353 sr = avctx->sample_rate;
2354 ch = avctx->channels;
2355 ba = avctx->block_align;
2356 tag = avctx->codec_tag;
2357 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2359 /* codecs with an exact constant bits per sample */
2360 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2361 return (frame_bytes * 8LL) / (bps * ch);
2362 bps = avctx->bits_per_coded_sample;
2364 /* codecs with a fixed packet duration */
2366 case AV_CODEC_ID_ADPCM_ADX: return 32;
2367 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2368 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2369 case AV_CODEC_ID_AMR_NB:
2370 case AV_CODEC_ID_GSM:
2371 case AV_CODEC_ID_QCELP:
2372 case AV_CODEC_ID_RA_288: return 160;
2373 case AV_CODEC_ID_IMC: return 256;
2374 case AV_CODEC_ID_AMR_WB:
2375 case AV_CODEC_ID_GSM_MS: return 320;
2376 case AV_CODEC_ID_MP1: return 384;
2377 case AV_CODEC_ID_ATRAC1: return 512;
2378 case AV_CODEC_ID_ATRAC3: return 1024;
2379 case AV_CODEC_ID_MP2:
2380 case AV_CODEC_ID_MUSEPACK7: return 1152;
2381 case AV_CODEC_ID_AC3: return 1536;
2385 /* calc from sample rate */
2386 if (id == AV_CODEC_ID_TTA)
2387 return 256 * sr / 245;
2390 /* calc from sample rate and channels */
2391 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2392 return (480 << (sr / 22050)) / ch;
2397 /* calc from block_align */
2398 if (id == AV_CODEC_ID_SIPR) {
2400 case 20: return 160;
2401 case 19: return 144;
2402 case 29: return 288;
2403 case 37: return 480;
2405 } else if (id == AV_CODEC_ID_ILBC) {
2407 case 38: return 160;
2408 case 50: return 240;
2413 if (frame_bytes > 0) {
2414 /* calc from frame_bytes only */
2415 if (id == AV_CODEC_ID_TRUESPEECH)
2416 return 240 * (frame_bytes / 32);
2417 if (id == AV_CODEC_ID_NELLYMOSER)
2418 return 256 * (frame_bytes / 64);
2419 if (id == AV_CODEC_ID_RA_144)
2420 return 160 * (frame_bytes / 20);
2423 /* calc from frame_bytes and bits_per_coded_sample */
2424 if (id == AV_CODEC_ID_ADPCM_G726)
2425 return frame_bytes * 8 / bps;
2429 /* calc from frame_bytes and channels */
2431 case AV_CODEC_ID_ADPCM_4XM:
2432 case AV_CODEC_ID_ADPCM_IMA_ISS:
2433 return (frame_bytes - 4 * ch) * 2 / ch;
2434 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2435 return (frame_bytes - 4) * 2 / ch;
2436 case AV_CODEC_ID_ADPCM_IMA_AMV:
2437 return (frame_bytes - 8) * 2 / ch;
2438 case AV_CODEC_ID_ADPCM_XA:
2439 return (frame_bytes / 128) * 224 / ch;
2440 case AV_CODEC_ID_INTERPLAY_DPCM:
2441 return (frame_bytes - 6 - ch) / ch;
2442 case AV_CODEC_ID_ROQ_DPCM:
2443 return (frame_bytes - 8) / ch;
2444 case AV_CODEC_ID_XAN_DPCM:
2445 return (frame_bytes - 2 * ch) / ch;
2446 case AV_CODEC_ID_MACE3:
2447 return 3 * frame_bytes / ch;
2448 case AV_CODEC_ID_MACE6:
2449 return 6 * frame_bytes / ch;
2450 case AV_CODEC_ID_PCM_LXF:
2451 return 2 * (frame_bytes / (5 * ch));
2455 /* calc from frame_bytes, channels, and codec_tag */
2456 if (id == AV_CODEC_ID_SOL_DPCM) {
2458 return frame_bytes / ch;
2460 return frame_bytes * 2 / ch;
2465 /* calc from frame_bytes, channels, and block_align */
2466 int blocks = frame_bytes / ba;
2467 switch (avctx->codec_id) {
2468 case AV_CODEC_ID_ADPCM_IMA_WAV:
2469 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2470 case AV_CODEC_ID_ADPCM_IMA_DK3:
2471 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2472 case AV_CODEC_ID_ADPCM_IMA_DK4:
2473 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2474 case AV_CODEC_ID_ADPCM_MS:
2475 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2480 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2481 switch (avctx->codec_id) {
2482 case AV_CODEC_ID_PCM_DVD:
2485 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2486 case AV_CODEC_ID_PCM_BLURAY:
2489 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2490 case AV_CODEC_ID_S302M:
2491 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2501 int ff_thread_init(AVCodecContext *s)
2508 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2522 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2525 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2529 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2531 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
2532 "version to the newest one from Git. If the problem still "
2533 "occurs, it means that your file has a feature which has not "
2534 "been implemented.\n", feature);
2536 av_log_ask_for_sample(avc, NULL);
2539 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2541 va_list argument_list;
2543 va_start(argument_list, msg);
2546 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2547 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2548 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2549 "and contact the ffmpeg-devel mailing list.\n");
2551 va_end(argument_list);
2554 static AVHWAccel *first_hwaccel = NULL;
2556 void av_register_hwaccel(AVHWAccel *hwaccel)
2558 AVHWAccel **p = &first_hwaccel;
2562 hwaccel->next = NULL;
2565 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2567 return hwaccel ? hwaccel->next : first_hwaccel;
2570 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2572 AVHWAccel *hwaccel = NULL;
2574 while ((hwaccel = av_hwaccel_next(hwaccel)))
2575 if (hwaccel->id == codec_id
2576 && hwaccel->pix_fmt == pix_fmt)
2581 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2583 if (ff_lockmgr_cb) {
2584 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2586 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2592 if (ff_lockmgr_cb) {
2593 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2595 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2601 int avpriv_lock_avformat(void)
2603 if (ff_lockmgr_cb) {
2604 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2610 int avpriv_unlock_avformat(void)
2612 if (ff_lockmgr_cb) {
2613 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2619 unsigned int avpriv_toupper4(unsigned int x)
2621 return toupper(x & 0xFF)
2622 + (toupper((x >> 8) & 0xFF) << 8)
2623 + (toupper((x >> 16) & 0xFF) << 16)
2624 + (toupper((x >> 24) & 0xFF) << 24);
2629 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2633 ff_init_buffer_info(avctx, f);
2635 return avctx->get_buffer(avctx, f);
2638 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2640 f->owner->release_buffer(f->owner, f);
2643 void ff_thread_finish_setup(AVCodecContext *avctx)
2647 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2651 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2655 int ff_thread_can_start_frame(AVCodecContext *avctx)
2662 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2664 AVCodec *c= avcodec_find_decoder(codec_id);
2666 c= avcodec_find_encoder(codec_id);
2670 if (codec_id <= AV_CODEC_ID_NONE)
2671 return AVMEDIA_TYPE_UNKNOWN;
2672 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2673 return AVMEDIA_TYPE_VIDEO;
2674 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2675 return AVMEDIA_TYPE_AUDIO;
2676 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2677 return AVMEDIA_TYPE_SUBTITLE;
2679 return AVMEDIA_TYPE_UNKNOWN;
2682 int avcodec_is_open(AVCodecContext *s)
2684 return !!s->internal;