3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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"
39 #include "libavutil/opt.h"
41 #include "audioconvert.h"
43 #include "bytestream.h"
49 static int volatile entangled_thread_counter = 0;
50 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
51 static void *codec_mutex;
52 static void *avformat_mutex;
54 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
59 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
61 ptr = av_realloc(ptr, min_size);
62 /* we could set this to the unmodified min_size but this is safer
63 * if the user lost the ptr and uses NULL now
73 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
78 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
80 *p = av_malloc(min_size);
86 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
89 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
94 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
96 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
99 /* encoder management */
100 static AVCodec *first_avcodec = NULL;
102 AVCodec *av_codec_next(const AVCodec *c)
107 return first_avcodec;
110 static void avcodec_init(void)
112 static int initialized = 0;
114 if (initialized != 0)
118 ff_dsputil_static_init();
121 int av_codec_is_encoder(const AVCodec *codec)
123 return codec && (codec->encode_sub || codec->encode2);
126 int av_codec_is_decoder(const AVCodec *codec)
128 return codec && codec->decode;
131 void avcodec_register(AVCodec *codec)
141 if (codec->init_static_data)
142 codec->init_static_data(codec);
145 unsigned avcodec_get_edge_width(void)
150 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
152 s->coded_width = width;
153 s->coded_height = height;
158 #define INTERNAL_BUFFER_SIZE (32 + 1)
160 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
161 int linesize_align[AV_NUM_DATA_POINTERS])
167 switch (s->pix_fmt) {
168 case AV_PIX_FMT_YUV420P:
169 case AV_PIX_FMT_YUYV422:
170 case AV_PIX_FMT_UYVY422:
171 case AV_PIX_FMT_YUV422P:
172 case AV_PIX_FMT_YUV440P:
173 case AV_PIX_FMT_YUV444P:
174 case AV_PIX_FMT_GBRP:
175 case AV_PIX_FMT_GRAY8:
176 case AV_PIX_FMT_GRAY16BE:
177 case AV_PIX_FMT_GRAY16LE:
178 case AV_PIX_FMT_YUVJ420P:
179 case AV_PIX_FMT_YUVJ422P:
180 case AV_PIX_FMT_YUVJ440P:
181 case AV_PIX_FMT_YUVJ444P:
182 case AV_PIX_FMT_YUVA420P:
183 case AV_PIX_FMT_YUVA422P:
184 case AV_PIX_FMT_YUVA444P:
185 case AV_PIX_FMT_YUV420P9LE:
186 case AV_PIX_FMT_YUV420P9BE:
187 case AV_PIX_FMT_YUV420P10LE:
188 case AV_PIX_FMT_YUV420P10BE:
189 case AV_PIX_FMT_YUV422P9LE:
190 case AV_PIX_FMT_YUV422P9BE:
191 case AV_PIX_FMT_YUV422P10LE:
192 case AV_PIX_FMT_YUV422P10BE:
193 case AV_PIX_FMT_YUV444P9LE:
194 case AV_PIX_FMT_YUV444P9BE:
195 case AV_PIX_FMT_YUV444P10LE:
196 case AV_PIX_FMT_YUV444P10BE:
197 case AV_PIX_FMT_GBRP9LE:
198 case AV_PIX_FMT_GBRP9BE:
199 case AV_PIX_FMT_GBRP10LE:
200 case AV_PIX_FMT_GBRP10BE:
201 w_align = 16; //FIXME assume 16 pixel per macroblock
202 h_align = 16 * 2; // interlaced needs 2 macroblocks height
204 case AV_PIX_FMT_YUV411P:
205 case AV_PIX_FMT_UYYVYY411:
209 case AV_PIX_FMT_YUV410P:
210 if (s->codec_id == AV_CODEC_ID_SVQ1) {
214 case AV_PIX_FMT_RGB555:
215 if (s->codec_id == AV_CODEC_ID_RPZA) {
219 case AV_PIX_FMT_PAL8:
220 case AV_PIX_FMT_BGR8:
221 case AV_PIX_FMT_RGB8:
222 if (s->codec_id == AV_CODEC_ID_SMC) {
227 case AV_PIX_FMT_BGR24:
228 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
229 (s->codec_id == AV_CODEC_ID_ZLIB)) {
240 *width = FFALIGN(*width, w_align);
241 *height = FFALIGN(*height, h_align);
242 if (s->codec_id == AV_CODEC_ID_H264)
243 // some of the optimized chroma MC reads one line too much
246 for (i = 0; i < 4; i++)
247 linesize_align[i] = STRIDE_ALIGN;
250 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
252 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
253 int chroma_shift = desc->log2_chroma_w;
254 int linesize_align[AV_NUM_DATA_POINTERS];
257 avcodec_align_dimensions2(s, width, height, linesize_align);
258 align = FFMAX(linesize_align[0], linesize_align[3]);
259 linesize_align[1] <<= chroma_shift;
260 linesize_align[2] <<= chroma_shift;
261 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
262 *width = FFALIGN(*width, align);
265 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
266 enum AVSampleFormat sample_fmt, const uint8_t *buf,
267 int buf_size, int align)
269 int ch, planar, needed_size, ret = 0;
271 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
272 frame->nb_samples, sample_fmt,
274 if (buf_size < needed_size)
275 return AVERROR(EINVAL);
277 planar = av_sample_fmt_is_planar(sample_fmt);
278 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
279 if (!(frame->extended_data = av_mallocz(nb_channels *
280 sizeof(*frame->extended_data))))
281 return AVERROR(ENOMEM);
283 frame->extended_data = frame->data;
286 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
287 buf, nb_channels, frame->nb_samples,
288 sample_fmt, align)) < 0) {
289 if (frame->extended_data != frame->data)
290 av_free(frame->extended_data);
293 if (frame->extended_data != frame->data) {
294 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
295 frame->data[ch] = frame->extended_data[ch];
301 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
303 AVCodecInternal *avci = avctx->internal;
307 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
308 frame->nb_samples, avctx->sample_fmt,
311 return AVERROR(EINVAL);
313 /* allocate InternalBuffer if needed */
315 avci->buffer = av_mallocz(sizeof(InternalBuffer));
317 return AVERROR(ENOMEM);
321 /* if there is a previously-used internal buffer, check its size and
322 * channel count to see if we can reuse it */
323 if (buf->extended_data) {
324 /* if current buffer is too small, free it */
325 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
326 av_free(buf->extended_data[0]);
327 if (buf->extended_data != buf->data)
328 av_free(buf->extended_data);
329 buf->extended_data = NULL;
332 /* if number of channels has changed, reset and/or free extended data
333 * pointers but leave data buffer in buf->data[0] for reuse */
334 if (buf->nb_channels != avctx->channels) {
335 if (buf->extended_data != buf->data)
336 av_free(buf->extended_data);
337 buf->extended_data = NULL;
341 /* if there is no previous buffer or the previous buffer cannot be used
342 * as-is, allocate a new buffer and/or rearrange the channel pointers */
343 if (!buf->extended_data) {
345 if (!(buf->data[0] = av_mallocz(buf_size)))
346 return AVERROR(ENOMEM);
347 buf->audio_data_size = buf_size;
349 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
350 avctx->sample_fmt, buf->data[0],
351 buf->audio_data_size, 0)))
354 if (frame->extended_data == frame->data)
355 buf->extended_data = buf->data;
357 buf->extended_data = frame->extended_data;
358 memcpy(buf->data, frame->data, sizeof(frame->data));
359 buf->linesize[0] = frame->linesize[0];
360 buf->nb_channels = avctx->channels;
362 /* copy InternalBuffer info to the AVFrame */
363 frame->extended_data = buf->extended_data;
364 frame->linesize[0] = buf->linesize[0];
365 memcpy(frame->data, buf->data, sizeof(frame->data));
368 frame->type = FF_BUFFER_TYPE_INTERNAL;
371 frame->pkt_pts = avctx->pkt->pts;
373 frame->pkt_pts = AV_NOPTS_VALUE;
374 frame->reordered_opaque = avctx->reordered_opaque;
376 frame->sample_rate = avctx->sample_rate;
377 frame->format = avctx->sample_fmt;
378 frame->channel_layout = avctx->channel_layout;
380 if (avctx->debug & FF_DEBUG_BUFFERS)
381 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
382 "internal audio buffer used\n", frame);
387 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
393 AVCodecInternal *avci = s->internal;
395 if (pic->data[0] != NULL) {
396 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
399 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
400 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
404 if (av_image_check_size(w, h, 0, s))
408 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
409 sizeof(InternalBuffer));
412 buf = &avci->buffer[avci->buffer_count];
414 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
415 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
416 av_freep(&buf->base[i]);
422 int h_chroma_shift, v_chroma_shift;
427 int stride_align[AV_NUM_DATA_POINTERS];
428 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
429 const int pixel_size = desc->comp[0].step_minus1 + 1;
431 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
433 avcodec_align_dimensions2(s, &w, &h, stride_align);
435 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
441 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
442 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
443 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
444 // increase alignment of w for next try (rhs gives the lowest bit set in w)
448 for (i = 0; i < 4; i++)
449 unaligned |= picture.linesize[i] % stride_align[i];
452 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
456 for (i = 0; i < 3 && picture.data[i + 1]; i++)
457 size[i] = picture.data[i + 1] - picture.data[i];
458 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
460 memset(buf->base, 0, sizeof(buf->base));
461 memset(buf->data, 0, sizeof(buf->data));
463 for (i = 0; i < 4 && size[i]; i++) {
464 const int h_shift = i == 0 ? 0 : h_chroma_shift;
465 const int v_shift = i == 0 ? 0 : v_chroma_shift;
467 buf->linesize[i] = picture.linesize[i];
469 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
470 if (buf->base[i] == NULL)
472 memset(buf->base[i], 128, size[i]);
474 // no edge if EDGE EMU or not planar YUV
475 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
476 buf->data[i] = buf->base[i];
478 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
480 for (; i < AV_NUM_DATA_POINTERS; i++) {
481 buf->base[i] = buf->data[i] = NULL;
482 buf->linesize[i] = 0;
484 if (size[1] && !size[2])
485 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
486 buf->width = s->width;
487 buf->height = s->height;
488 buf->pix_fmt = s->pix_fmt;
490 pic->type = FF_BUFFER_TYPE_INTERNAL;
492 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
493 pic->base[i] = buf->base[i];
494 pic->data[i] = buf->data[i];
495 pic->linesize[i] = buf->linesize[i];
497 pic->extended_data = pic->data;
498 avci->buffer_count++;
499 pic->width = buf->width;
500 pic->height = buf->height;
501 pic->format = buf->pix_fmt;
502 pic->sample_aspect_ratio = s->sample_aspect_ratio;
505 pic->pkt_pts = s->pkt->pts;
507 pic->pkt_pts = AV_NOPTS_VALUE;
508 pic->reordered_opaque = s->reordered_opaque;
510 if (s->debug & FF_DEBUG_BUFFERS)
511 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
512 "buffers used\n", pic, avci->buffer_count);
517 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
519 switch (avctx->codec_type) {
520 case AVMEDIA_TYPE_VIDEO:
521 return video_get_buffer(avctx, frame);
522 case AVMEDIA_TYPE_AUDIO:
523 return audio_get_buffer(avctx, frame);
529 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
532 InternalBuffer *buf, *last;
533 AVCodecInternal *avci = s->internal;
535 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
537 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
538 assert(avci->buffer_count);
541 buf = NULL; /* avoids warning */
542 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
543 buf = &avci->buffer[i];
544 if (buf->data[0] == pic->data[0])
547 assert(i < avci->buffer_count);
548 avci->buffer_count--;
549 last = &avci->buffer[avci->buffer_count];
552 FFSWAP(InternalBuffer, *buf, *last);
555 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
557 // pic->base[i]=NULL;
559 if (s->debug & FF_DEBUG_BUFFERS)
560 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
561 "buffers used\n", pic, avci->buffer_count);
564 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
569 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
571 /* If no picture return a new buffer */
572 if (pic->data[0] == NULL) {
573 /* We will copy from buffer, so must be readable */
574 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
575 return s->get_buffer(s, pic);
578 assert(s->pix_fmt == pic->format);
580 /* If internal buffer type return the same buffer */
581 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
583 pic->pkt_pts = s->pkt->pts;
585 pic->pkt_pts = AV_NOPTS_VALUE;
586 pic->reordered_opaque = s->reordered_opaque;
591 * Not internal type and reget_buffer not overridden, emulate cr buffer
594 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
595 pic->data[i] = pic->base[i] = NULL;
597 /* Allocate new frame */
598 if (s->get_buffer(s, pic))
600 /* Copy image data from old buffer to new buffer */
601 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
603 s->release_buffer(s, &temp_pic); // Release old frame
607 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
611 for (i = 0; i < count; i++) {
612 int r = func(c, (char *)arg + i * size);
619 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
623 for (i = 0; i < count; i++) {
624 int r = func(c, arg, i, 0);
631 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
633 while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
638 void avcodec_get_frame_defaults(AVFrame *frame)
640 if (frame->extended_data != frame->data)
641 av_freep(&frame->extended_data);
643 memset(frame, 0, sizeof(AVFrame));
645 frame->pts = AV_NOPTS_VALUE;
646 frame->key_frame = 1;
647 frame->sample_aspect_ratio = (AVRational) {0, 1 };
648 frame->format = -1; /* unknown */
649 frame->extended_data = frame->data;
652 AVFrame *avcodec_alloc_frame(void)
654 AVFrame *frame = av_mallocz(sizeof(AVFrame));
659 avcodec_get_frame_defaults(frame);
664 void avcodec_free_frame(AVFrame **frame)
668 if (!frame || !*frame)
673 if (f->extended_data != f->data)
674 av_freep(&f->extended_data);
679 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
682 AVDictionary *tmp = NULL;
684 if (avcodec_is_open(avctx))
687 if ((!codec && !avctx->codec)) {
688 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
689 return AVERROR(EINVAL);
691 if ((codec && avctx->codec && codec != avctx->codec)) {
692 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
693 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
694 return AVERROR(EINVAL);
697 codec = avctx->codec;
699 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
700 return AVERROR(EINVAL);
703 av_dict_copy(&tmp, *options, 0);
705 /* If there is a user-supplied mutex locking routine, call it. */
707 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
711 entangled_thread_counter++;
712 if (entangled_thread_counter != 1) {
713 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
718 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
719 if (!avctx->internal) {
720 ret = AVERROR(ENOMEM);
724 if (codec->priv_data_size > 0) {
725 if (!avctx->priv_data) {
726 avctx->priv_data = av_mallocz(codec->priv_data_size);
727 if (!avctx->priv_data) {
728 ret = AVERROR(ENOMEM);
731 if (codec->priv_class) {
732 *(const AVClass **)avctx->priv_data = codec->priv_class;
733 av_opt_set_defaults(avctx->priv_data);
736 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
739 avctx->priv_data = NULL;
741 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
744 if (avctx->coded_width && avctx->coded_height)
745 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
746 else if (avctx->width && avctx->height)
747 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
749 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
750 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
751 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
752 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
753 avcodec_set_dimensions(avctx, 0, 0);
756 /* if the decoder init function was already called previously,
757 * free the already allocated subtitle_header before overwriting it */
758 if (av_codec_is_decoder(codec))
759 av_freep(&avctx->subtitle_header);
761 if (avctx->channels > FF_SANE_NB_CHANNELS) {
762 ret = AVERROR(EINVAL);
766 avctx->codec = codec;
767 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
768 avctx->codec_id == AV_CODEC_ID_NONE) {
769 avctx->codec_type = codec->type;
770 avctx->codec_id = codec->id;
772 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
773 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
774 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
775 ret = AVERROR(EINVAL);
778 avctx->frame_number = 0;
780 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
781 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
782 ret = AVERROR_EXPERIMENTAL;
786 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
787 (!avctx->time_base.num || !avctx->time_base.den)) {
788 avctx->time_base.num = 1;
789 avctx->time_base.den = avctx->sample_rate;
792 if (HAVE_THREADS && !avctx->thread_opaque) {
793 ret = ff_thread_init(avctx);
798 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
799 avctx->thread_count = 1;
801 if (av_codec_is_encoder(avctx->codec)) {
803 if (avctx->codec->sample_fmts) {
804 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
805 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
807 if (avctx->channels == 1 &&
808 av_get_planar_sample_fmt(avctx->sample_fmt) ==
809 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
810 avctx->sample_fmt = avctx->codec->sample_fmts[i];
814 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
815 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
816 ret = AVERROR(EINVAL);
820 if (avctx->codec->pix_fmts) {
821 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
822 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
824 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
825 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
826 ret = AVERROR(EINVAL);
830 if (avctx->codec->supported_samplerates) {
831 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
832 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
834 if (avctx->codec->supported_samplerates[i] == 0) {
835 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
836 ret = AVERROR(EINVAL);
840 if (avctx->codec->channel_layouts) {
841 if (!avctx->channel_layout) {
842 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
844 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
845 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
847 if (avctx->codec->channel_layouts[i] == 0) {
848 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
849 ret = AVERROR(EINVAL);
854 if (avctx->channel_layout && avctx->channels) {
855 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
856 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
857 ret = AVERROR(EINVAL);
860 } else if (avctx->channel_layout) {
861 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
865 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
866 ret = avctx->codec->init(avctx);
872 if (av_codec_is_decoder(avctx->codec)) {
873 /* validate channel layout from the decoder */
874 if (avctx->channel_layout) {
875 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
876 if (!avctx->channels)
877 avctx->channels = channels;
878 else if (channels != avctx->channels) {
879 av_log(avctx, AV_LOG_WARNING,
880 "channel layout does not match number of channels\n");
881 avctx->channel_layout = 0;
884 if (avctx->channels && avctx->channels < 0 ||
885 avctx->channels > FF_SANE_NB_CHANNELS) {
886 ret = AVERROR(EINVAL);
891 entangled_thread_counter--;
893 /* Release any user-supplied mutex. */
895 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
898 av_dict_free(options);
905 av_freep(&avctx->priv_data);
906 av_freep(&avctx->internal);
911 int ff_alloc_packet(AVPacket *avpkt, int size)
913 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
914 return AVERROR(EINVAL);
917 void *destruct = avpkt->destruct;
919 if (avpkt->size < size)
920 return AVERROR(EINVAL);
922 av_init_packet(avpkt);
923 avpkt->destruct = destruct;
927 return av_new_packet(avpkt, size);
932 * Pad last frame with silence.
934 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
936 AVFrame *frame = NULL;
940 if (!(frame = avcodec_alloc_frame()))
941 return AVERROR(ENOMEM);
944 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
945 s->frame_size, s->sample_fmt, 0)) < 0)
948 if (!(buf = av_malloc(ret))) {
949 ret = AVERROR(ENOMEM);
953 frame->nb_samples = s->frame_size;
954 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
957 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
958 src->nb_samples, s->channels, s->sample_fmt)) < 0)
960 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
961 frame->nb_samples - src->nb_samples,
962 s->channels, s->sample_fmt)) < 0)
970 if (frame->extended_data != frame->data)
971 av_freep(&frame->extended_data);
977 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
979 const AVFrame *frame,
983 AVFrame *padded_frame = NULL;
985 int user_packet = !!avpkt->data;
989 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
990 av_free_packet(avpkt);
991 av_init_packet(avpkt);
995 /* ensure that extended_data is properly set */
996 if (frame && !frame->extended_data) {
997 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
998 avctx->channels > AV_NUM_DATA_POINTERS) {
999 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1000 "with more than %d channels, but extended_data is not set.\n",
1001 AV_NUM_DATA_POINTERS);
1002 return AVERROR(EINVAL);
1004 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1007 tmp.extended_data = tmp.data;
1011 /* check for valid frame size */
1013 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1014 if (frame->nb_samples > avctx->frame_size)
1015 return AVERROR(EINVAL);
1016 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1017 if (frame->nb_samples < avctx->frame_size &&
1018 !avctx->internal->last_audio_frame) {
1019 ret = pad_last_frame(avctx, &padded_frame, frame);
1023 frame = padded_frame;
1024 avctx->internal->last_audio_frame = 1;
1027 if (frame->nb_samples != avctx->frame_size) {
1028 ret = AVERROR(EINVAL);
1034 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1036 if (*got_packet_ptr) {
1037 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1038 if (avpkt->pts == AV_NOPTS_VALUE)
1039 avpkt->pts = frame->pts;
1040 if (!avpkt->duration)
1041 avpkt->duration = ff_samples_to_time_base(avctx,
1044 avpkt->dts = avpkt->pts;
1049 if (!user_packet && avpkt->size) {
1050 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1052 avpkt->data = new_data;
1055 avctx->frame_number++;
1058 if (ret < 0 || !*got_packet_ptr) {
1059 av_free_packet(avpkt);
1060 av_init_packet(avpkt);
1064 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1065 * this needs to be moved to the encoders, but for now we can do it
1066 * here to simplify things */
1067 avpkt->flags |= AV_PKT_FLAG_KEY;
1071 av_freep(&padded_frame->data[0]);
1072 if (padded_frame->extended_data != padded_frame->data)
1073 av_freep(&padded_frame->extended_data);
1074 av_freep(&padded_frame);
1080 #if FF_API_OLD_ENCODE_AUDIO
1081 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1082 uint8_t *buf, int buf_size,
1083 const short *samples)
1086 AVFrame frame0 = { 0 };
1088 int ret, samples_size, got_packet;
1090 av_init_packet(&pkt);
1092 pkt.size = buf_size;
1096 avcodec_get_frame_defaults(frame);
1098 if (avctx->frame_size) {
1099 frame->nb_samples = avctx->frame_size;
1101 /* if frame_size is not set, the number of samples must be
1102 * calculated from the buffer size */
1104 if (!av_get_bits_per_sample(avctx->codec_id)) {
1105 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1106 "support this codec\n");
1107 return AVERROR(EINVAL);
1109 nb_samples = (int64_t)buf_size * 8 /
1110 (av_get_bits_per_sample(avctx->codec_id) *
1112 if (nb_samples >= INT_MAX)
1113 return AVERROR(EINVAL);
1114 frame->nb_samples = nb_samples;
1117 /* it is assumed that the samples buffer is large enough based on the
1118 * relevant parameters */
1119 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1121 avctx->sample_fmt, 1);
1122 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1124 (const uint8_t *)samples,
1128 /* fabricate frame pts from sample count.
1129 * this is needed because the avcodec_encode_audio() API does not have
1130 * a way for the user to provide pts */
1131 frame->pts = ff_samples_to_time_base(avctx,
1132 avctx->internal->sample_count);
1133 avctx->internal->sample_count += frame->nb_samples;
1139 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1140 if (!ret && got_packet && avctx->coded_frame) {
1141 avctx->coded_frame->pts = pkt.pts;
1142 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1144 /* free any side data since we cannot return it */
1145 if (pkt.side_data_elems > 0) {
1147 for (i = 0; i < pkt.side_data_elems; i++)
1148 av_free(pkt.side_data[i].data);
1149 av_freep(&pkt.side_data);
1150 pkt.side_data_elems = 0;
1153 if (frame && frame->extended_data != frame->data)
1154 av_free(frame->extended_data);
1156 return ret ? ret : pkt.size;
1161 #if FF_API_OLD_ENCODE_VIDEO
1162 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1163 const AVFrame *pict)
1166 int ret, got_packet = 0;
1168 if (buf_size < FF_MIN_BUFFER_SIZE) {
1169 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1173 av_init_packet(&pkt);
1175 pkt.size = buf_size;
1177 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1178 if (!ret && got_packet && avctx->coded_frame) {
1179 avctx->coded_frame->pts = pkt.pts;
1180 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1183 /* free any side data since we cannot return it */
1184 if (pkt.side_data_elems > 0) {
1186 for (i = 0; i < pkt.side_data_elems; i++)
1187 av_free(pkt.side_data[i].data);
1188 av_freep(&pkt.side_data);
1189 pkt.side_data_elems = 0;
1192 return ret ? ret : pkt.size;
1197 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1199 const AVFrame *frame,
1200 int *got_packet_ptr)
1203 int user_packet = !!avpkt->data;
1205 *got_packet_ptr = 0;
1207 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1208 av_free_packet(avpkt);
1209 av_init_packet(avpkt);
1214 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1215 return AVERROR(EINVAL);
1217 av_assert0(avctx->codec->encode2);
1219 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1221 if (!*got_packet_ptr)
1223 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1224 avpkt->pts = avpkt->dts = frame->pts;
1226 if (!user_packet && avpkt->size) {
1227 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1229 avpkt->data = new_data;
1232 avctx->frame_number++;
1235 if (ret < 0 || !*got_packet_ptr)
1236 av_free_packet(avpkt);
1242 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1243 const AVSubtitle *sub)
1246 if (sub->start_display_time) {
1247 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1250 if (sub->num_rects == 0 || !sub->rects)
1252 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1253 avctx->frame_number++;
1257 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1260 const uint8_t *data;
1263 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1266 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1267 if (!data || size < 4)
1269 flags = bytestream_get_le32(&data);
1271 if (size < 4) /* Required for any of the changes */
1273 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1274 avctx->channels = bytestream_get_le32(&data);
1277 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1280 avctx->channel_layout = bytestream_get_le64(&data);
1285 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1286 avctx->sample_rate = bytestream_get_le32(&data);
1289 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1292 avctx->width = bytestream_get_le32(&data);
1293 avctx->height = bytestream_get_le32(&data);
1294 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1299 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1300 int *got_picture_ptr,
1305 *got_picture_ptr = 0;
1306 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1310 apply_param_change(avctx, avpkt);
1312 avcodec_get_frame_defaults(picture);
1314 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1315 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1316 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1319 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1321 picture->pkt_dts = avpkt->dts;
1322 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1323 picture->width = avctx->width;
1324 picture->height = avctx->height;
1325 picture->format = avctx->pix_fmt;
1328 emms_c(); //needed to avoid an emms_c() call before every return;
1330 if (*got_picture_ptr)
1331 avctx->frame_number++;
1335 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1336 * make sure it's set correctly */
1337 picture->extended_data = picture->data;
1342 #if FF_API_OLD_DECODE_AUDIO
1343 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1344 int *frame_size_ptr,
1348 int ret, got_frame = 0;
1350 if (avctx->get_buffer != avcodec_default_get_buffer) {
1351 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1352 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1353 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1354 "avcodec_decode_audio4()\n");
1355 avctx->get_buffer = avcodec_default_get_buffer;
1358 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1360 if (ret >= 0 && got_frame) {
1362 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1363 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1365 avctx->sample_fmt, 1);
1366 if (*frame_size_ptr < data_size) {
1367 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1368 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1369 return AVERROR(EINVAL);
1372 memcpy(samples, frame.extended_data[0], plane_size);
1374 if (planar && avctx->channels > 1) {
1375 uint8_t *out = ((uint8_t *)samples) + plane_size;
1376 for (ch = 1; ch < avctx->channels; ch++) {
1377 memcpy(out, frame.extended_data[ch], plane_size);
1381 *frame_size_ptr = data_size;
1383 *frame_size_ptr = 0;
1390 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1395 int planar, channels;
1402 if (!avpkt->data && avpkt->size) {
1403 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1404 return AVERROR(EINVAL);
1407 apply_param_change(avctx, avpkt);
1409 avcodec_get_frame_defaults(frame);
1411 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1412 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1413 if (ret >= 0 && *got_frame_ptr) {
1414 avctx->frame_number++;
1415 frame->pkt_dts = avpkt->dts;
1416 if (frame->format == AV_SAMPLE_FMT_NONE)
1417 frame->format = avctx->sample_fmt;
1421 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1422 * make sure it's set correctly; assume decoders that actually use
1423 * extended_data are doing it correctly */
1424 planar = av_sample_fmt_is_planar(frame->format);
1425 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1426 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1427 frame->extended_data = frame->data;
1432 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1440 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1442 avctx->frame_number++;
1446 void avsubtitle_free(AVSubtitle *sub)
1450 for (i = 0; i < sub->num_rects; i++) {
1451 av_freep(&sub->rects[i]->pict.data[0]);
1452 av_freep(&sub->rects[i]->pict.data[1]);
1453 av_freep(&sub->rects[i]->pict.data[2]);
1454 av_freep(&sub->rects[i]->pict.data[3]);
1455 av_freep(&sub->rects[i]->text);
1456 av_freep(&sub->rects[i]->ass);
1457 av_freep(&sub->rects[i]);
1460 av_freep(&sub->rects);
1462 memset(sub, 0, sizeof(AVSubtitle));
1465 av_cold int avcodec_close(AVCodecContext *avctx)
1467 /* If there is a user-supplied mutex locking routine, call it. */
1468 if (ff_lockmgr_cb) {
1469 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1473 entangled_thread_counter++;
1474 if (entangled_thread_counter != 1) {
1475 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1476 entangled_thread_counter--;
1480 if (avcodec_is_open(avctx)) {
1481 if (HAVE_THREADS && avctx->thread_opaque)
1482 ff_thread_free(avctx);
1483 if (avctx->codec && avctx->codec->close)
1484 avctx->codec->close(avctx);
1485 avcodec_default_free_buffers(avctx);
1486 avctx->coded_frame = NULL;
1487 av_freep(&avctx->internal);
1490 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1491 av_opt_free(avctx->priv_data);
1493 av_freep(&avctx->priv_data);
1494 if (av_codec_is_encoder(avctx->codec))
1495 av_freep(&avctx->extradata);
1496 avctx->codec = NULL;
1497 avctx->active_thread_type = 0;
1498 entangled_thread_counter--;
1500 /* Release any user-supplied mutex. */
1501 if (ff_lockmgr_cb) {
1502 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1507 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1509 AVCodec *p, *experimental = NULL;
1512 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1514 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1521 return experimental;
1524 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1526 return find_encdec(id, 1);
1529 AVCodec *avcodec_find_encoder_by_name(const char *name)
1536 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1543 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1545 return find_encdec(id, 0);
1548 AVCodec *avcodec_find_decoder_by_name(const char *name)
1555 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1562 static int get_bit_rate(AVCodecContext *ctx)
1565 int bits_per_sample;
1567 switch (ctx->codec_type) {
1568 case AVMEDIA_TYPE_VIDEO:
1569 case AVMEDIA_TYPE_DATA:
1570 case AVMEDIA_TYPE_SUBTITLE:
1571 case AVMEDIA_TYPE_ATTACHMENT:
1572 bit_rate = ctx->bit_rate;
1574 case AVMEDIA_TYPE_AUDIO:
1575 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1576 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1585 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1587 int i, len, ret = 0;
1589 for (i = 0; i < 4; i++) {
1590 len = snprintf(buf, buf_size,
1591 isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1593 buf_size = buf_size > len ? buf_size - len : 0;
1600 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1602 const char *codec_name;
1603 const char *profile = NULL;
1607 AVRational display_aspect_ratio;
1612 p = avcodec_find_encoder(enc->codec_id);
1614 p = avcodec_find_decoder(enc->codec_id);
1617 codec_name = p->name;
1618 profile = av_get_profile_name(p, enc->profile);
1619 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1620 /* fake mpeg2 transport stream codec (currently not
1622 codec_name = "mpeg2ts";
1623 } else if (enc->codec_name[0] != '\0') {
1624 codec_name = enc->codec_name;
1626 /* output avi tags */
1628 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1629 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1633 switch (enc->codec_type) {
1634 case AVMEDIA_TYPE_VIDEO:
1635 snprintf(buf, buf_size,
1637 codec_name, enc->mb_decision ? " (hq)" : "");
1639 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1641 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1642 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1644 av_get_pix_fmt_name(enc->pix_fmt));
1647 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1649 enc->width, enc->height);
1650 if (enc->sample_aspect_ratio.num) {
1651 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1652 enc->width * enc->sample_aspect_ratio.num,
1653 enc->height * enc->sample_aspect_ratio.den,
1655 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1656 " [PAR %d:%d DAR %d:%d]",
1657 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1658 display_aspect_ratio.num, display_aspect_ratio.den);
1660 if (av_log_get_level() >= AV_LOG_DEBUG) {
1661 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1662 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1664 enc->time_base.num / g, enc->time_base.den / g);
1668 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1669 ", q=%d-%d", enc->qmin, enc->qmax);
1672 case AVMEDIA_TYPE_AUDIO:
1673 snprintf(buf, buf_size,
1677 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1679 if (enc->sample_rate) {
1680 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1681 ", %d Hz", enc->sample_rate);
1683 av_strlcat(buf, ", ", buf_size);
1684 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1685 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1686 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1687 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1690 case AVMEDIA_TYPE_DATA:
1691 snprintf(buf, buf_size, "Data: %s", codec_name);
1693 case AVMEDIA_TYPE_SUBTITLE:
1694 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1696 case AVMEDIA_TYPE_ATTACHMENT:
1697 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1700 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1704 if (enc->flags & CODEC_FLAG_PASS1)
1705 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1707 if (enc->flags & CODEC_FLAG_PASS2)
1708 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1711 bitrate = get_bit_rate(enc);
1713 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1714 ", %d kb/s", bitrate / 1000);
1718 const char *av_get_profile_name(const AVCodec *codec, int profile)
1721 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1724 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1725 if (p->profile == profile)
1731 unsigned avcodec_version(void)
1733 return LIBAVCODEC_VERSION_INT;
1736 const char *avcodec_configuration(void)
1738 return LIBAV_CONFIGURATION;
1741 const char *avcodec_license(void)
1743 #define LICENSE_PREFIX "libavcodec license: "
1744 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1747 void avcodec_flush_buffers(AVCodecContext *avctx)
1749 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1750 ff_thread_flush(avctx);
1751 else if (avctx->codec->flush)
1752 avctx->codec->flush(avctx);
1755 static void video_free_buffers(AVCodecContext *s)
1757 AVCodecInternal *avci = s->internal;
1763 if (avci->buffer_count)
1764 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1765 avci->buffer_count);
1766 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
1767 InternalBuffer *buf = &avci->buffer[i];
1768 for (j = 0; j < 4; j++) {
1769 av_freep(&buf->base[j]);
1770 buf->data[j] = NULL;
1773 av_freep(&avci->buffer);
1775 avci->buffer_count = 0;
1778 static void audio_free_buffers(AVCodecContext *avctx)
1780 AVCodecInternal *avci = avctx->internal;
1781 InternalBuffer *buf;
1787 if (buf->extended_data) {
1788 av_free(buf->extended_data[0]);
1789 if (buf->extended_data != buf->data)
1790 av_free(buf->extended_data);
1792 av_freep(&avci->buffer);
1795 void avcodec_default_free_buffers(AVCodecContext *avctx)
1797 switch (avctx->codec_type) {
1798 case AVMEDIA_TYPE_VIDEO:
1799 video_free_buffers(avctx);
1801 case AVMEDIA_TYPE_AUDIO:
1802 audio_free_buffers(avctx);
1809 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1812 case AV_CODEC_ID_ADPCM_CT:
1813 case AV_CODEC_ID_ADPCM_IMA_APC:
1814 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1815 case AV_CODEC_ID_ADPCM_IMA_WS:
1816 case AV_CODEC_ID_ADPCM_G722:
1817 case AV_CODEC_ID_ADPCM_YAMAHA:
1819 case AV_CODEC_ID_PCM_ALAW:
1820 case AV_CODEC_ID_PCM_MULAW:
1821 case AV_CODEC_ID_PCM_S8:
1822 case AV_CODEC_ID_PCM_U8:
1823 case AV_CODEC_ID_PCM_ZORK:
1825 case AV_CODEC_ID_PCM_S16BE:
1826 case AV_CODEC_ID_PCM_S16LE:
1827 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1828 case AV_CODEC_ID_PCM_U16BE:
1829 case AV_CODEC_ID_PCM_U16LE:
1831 case AV_CODEC_ID_PCM_S24DAUD:
1832 case AV_CODEC_ID_PCM_S24BE:
1833 case AV_CODEC_ID_PCM_S24LE:
1834 case AV_CODEC_ID_PCM_U24BE:
1835 case AV_CODEC_ID_PCM_U24LE:
1837 case AV_CODEC_ID_PCM_S32BE:
1838 case AV_CODEC_ID_PCM_S32LE:
1839 case AV_CODEC_ID_PCM_U32BE:
1840 case AV_CODEC_ID_PCM_U32LE:
1841 case AV_CODEC_ID_PCM_F32BE:
1842 case AV_CODEC_ID_PCM_F32LE:
1844 case AV_CODEC_ID_PCM_F64BE:
1845 case AV_CODEC_ID_PCM_F64LE:
1852 int av_get_bits_per_sample(enum AVCodecID codec_id)
1855 case AV_CODEC_ID_ADPCM_SBPRO_2:
1857 case AV_CODEC_ID_ADPCM_SBPRO_3:
1859 case AV_CODEC_ID_ADPCM_SBPRO_4:
1860 case AV_CODEC_ID_ADPCM_IMA_WAV:
1861 case AV_CODEC_ID_ADPCM_IMA_QT:
1862 case AV_CODEC_ID_ADPCM_SWF:
1863 case AV_CODEC_ID_ADPCM_MS:
1866 return av_get_exact_bits_per_sample(codec_id);
1870 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1872 int id, sr, ch, ba, tag, bps;
1874 id = avctx->codec_id;
1875 sr = avctx->sample_rate;
1876 ch = avctx->channels;
1877 ba = avctx->block_align;
1878 tag = avctx->codec_tag;
1879 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1881 /* codecs with an exact constant bits per sample */
1882 if (bps > 0 && ch > 0 && frame_bytes > 0)
1883 return (frame_bytes * 8) / (bps * ch);
1884 bps = avctx->bits_per_coded_sample;
1886 /* codecs with a fixed packet duration */
1888 case AV_CODEC_ID_ADPCM_ADX: return 32;
1889 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1890 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1891 case AV_CODEC_ID_AMR_NB:
1892 case AV_CODEC_ID_GSM:
1893 case AV_CODEC_ID_QCELP:
1894 case AV_CODEC_ID_RA_144:
1895 case AV_CODEC_ID_RA_288: return 160;
1896 case AV_CODEC_ID_IMC: return 256;
1897 case AV_CODEC_ID_AMR_WB:
1898 case AV_CODEC_ID_GSM_MS: return 320;
1899 case AV_CODEC_ID_MP1: return 384;
1900 case AV_CODEC_ID_ATRAC1: return 512;
1901 case AV_CODEC_ID_ATRAC3: return 1024;
1902 case AV_CODEC_ID_MP2:
1903 case AV_CODEC_ID_MUSEPACK7: return 1152;
1904 case AV_CODEC_ID_AC3: return 1536;
1908 /* calc from sample rate */
1909 if (id == AV_CODEC_ID_TTA)
1910 return 256 * sr / 245;
1913 /* calc from sample rate and channels */
1914 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1915 return (480 << (sr / 22050)) / ch;
1920 /* calc from block_align */
1921 if (id == AV_CODEC_ID_SIPR) {
1923 case 20: return 160;
1924 case 19: return 144;
1925 case 29: return 288;
1926 case 37: return 480;
1928 } else if (id == AV_CODEC_ID_ILBC) {
1930 case 38: return 160;
1931 case 50: return 240;
1936 if (frame_bytes > 0) {
1937 /* calc from frame_bytes only */
1938 if (id == AV_CODEC_ID_TRUESPEECH)
1939 return 240 * (frame_bytes / 32);
1940 if (id == AV_CODEC_ID_NELLYMOSER)
1941 return 256 * (frame_bytes / 64);
1944 /* calc from frame_bytes and bits_per_coded_sample */
1945 if (id == AV_CODEC_ID_ADPCM_G726)
1946 return frame_bytes * 8 / bps;
1950 /* calc from frame_bytes and channels */
1952 case AV_CODEC_ID_ADPCM_4XM:
1953 case AV_CODEC_ID_ADPCM_IMA_ISS:
1954 return (frame_bytes - 4 * ch) * 2 / ch;
1955 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1956 return (frame_bytes - 4) * 2 / ch;
1957 case AV_CODEC_ID_ADPCM_IMA_AMV:
1958 return (frame_bytes - 8) * 2 / ch;
1959 case AV_CODEC_ID_ADPCM_XA:
1960 return (frame_bytes / 128) * 224 / ch;
1961 case AV_CODEC_ID_INTERPLAY_DPCM:
1962 return (frame_bytes - 6 - ch) / ch;
1963 case AV_CODEC_ID_ROQ_DPCM:
1964 return (frame_bytes - 8) / ch;
1965 case AV_CODEC_ID_XAN_DPCM:
1966 return (frame_bytes - 2 * ch) / ch;
1967 case AV_CODEC_ID_MACE3:
1968 return 3 * frame_bytes / ch;
1969 case AV_CODEC_ID_MACE6:
1970 return 6 * frame_bytes / ch;
1971 case AV_CODEC_ID_PCM_LXF:
1972 return 2 * (frame_bytes / (5 * ch));
1976 /* calc from frame_bytes, channels, and codec_tag */
1977 if (id == AV_CODEC_ID_SOL_DPCM) {
1979 return frame_bytes / ch;
1981 return frame_bytes * 2 / ch;
1986 /* calc from frame_bytes, channels, and block_align */
1987 int blocks = frame_bytes / ba;
1988 switch (avctx->codec_id) {
1989 case AV_CODEC_ID_ADPCM_IMA_WAV:
1990 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1991 case AV_CODEC_ID_ADPCM_IMA_DK3:
1992 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1993 case AV_CODEC_ID_ADPCM_IMA_DK4:
1994 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1995 case AV_CODEC_ID_ADPCM_MS:
1996 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2001 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2002 switch (avctx->codec_id) {
2003 case AV_CODEC_ID_PCM_DVD:
2004 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2005 case AV_CODEC_ID_PCM_BLURAY:
2006 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2007 case AV_CODEC_ID_S302M:
2008 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2018 int ff_thread_init(AVCodecContext *s)
2025 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2039 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2042 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2046 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2048 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2049 "version to the newest one from Git. If the problem still "
2050 "occurs, it means that your file has a feature which has not "
2051 "been implemented.\n", feature);
2053 av_log_ask_for_sample(avc, NULL);
2056 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2058 va_list argument_list;
2060 va_start(argument_list, msg);
2063 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2064 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2065 "of this file to ftp://upload.libav.org/incoming/ "
2066 "and contact the libav-devel mailing list.\n");
2068 va_end(argument_list);
2071 static AVHWAccel *first_hwaccel = NULL;
2073 void av_register_hwaccel(AVHWAccel *hwaccel)
2075 AVHWAccel **p = &first_hwaccel;
2079 hwaccel->next = NULL;
2082 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2084 return hwaccel ? hwaccel->next : first_hwaccel;
2087 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2089 AVHWAccel *hwaccel = NULL;
2091 while ((hwaccel = av_hwaccel_next(hwaccel)))
2092 if (hwaccel->id == codec_id
2093 && hwaccel->pix_fmt == pix_fmt)
2098 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2100 if (ff_lockmgr_cb) {
2101 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2103 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2109 if (ff_lockmgr_cb) {
2110 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2112 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2118 int avpriv_lock_avformat(void)
2120 if (ff_lockmgr_cb) {
2121 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2127 int avpriv_unlock_avformat(void)
2129 if (ff_lockmgr_cb) {
2130 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2136 unsigned int avpriv_toupper4(unsigned int x)
2138 return toupper(x & 0xFF)
2139 + (toupper((x >> 8) & 0xFF) << 8)
2140 + (toupper((x >> 16) & 0xFF) << 16)
2141 + (toupper((x >> 24) & 0xFF) << 24);
2146 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2149 return avctx->get_buffer(avctx, f);
2152 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2154 f->owner->release_buffer(f->owner, f);
2157 void ff_thread_finish_setup(AVCodecContext *avctx)
2161 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2165 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2171 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2173 if (codec_id <= AV_CODEC_ID_NONE)
2174 return AVMEDIA_TYPE_UNKNOWN;
2175 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2176 return AVMEDIA_TYPE_VIDEO;
2177 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2178 return AVMEDIA_TYPE_AUDIO;
2179 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2180 return AVMEDIA_TYPE_SUBTITLE;
2182 return AVMEDIA_TYPE_UNKNOWN;
2185 int avcodec_is_open(AVCodecContext *s)
2187 return !!s->internal;