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/channel_layout.h"
31 #include "libavutil/crc.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
39 #include "libavutil/opt.h"
42 #include "bytestream.h"
48 static int volatile entangled_thread_counter = 0;
49 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
50 static void *codec_mutex;
51 static void *avformat_mutex;
53 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
58 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
60 ptr = av_realloc(ptr, min_size);
61 /* we could set this to the unmodified min_size but this is safer
62 * if the user lost the ptr and uses NULL now
72 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
77 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
79 *p = av_malloc(min_size);
85 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
88 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
93 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
95 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
98 /* encoder management */
99 static AVCodec *first_avcodec = NULL;
101 AVCodec *av_codec_next(const AVCodec *c)
106 return first_avcodec;
109 static void avcodec_init(void)
111 static int initialized = 0;
113 if (initialized != 0)
117 ff_dsputil_static_init();
120 int av_codec_is_encoder(const AVCodec *codec)
122 return codec && (codec->encode_sub || codec->encode2);
125 int av_codec_is_decoder(const AVCodec *codec)
127 return codec && codec->decode;
130 void avcodec_register(AVCodec *codec)
140 if (codec->init_static_data)
141 codec->init_static_data(codec);
144 unsigned avcodec_get_edge_width(void)
149 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
151 s->coded_width = width;
152 s->coded_height = height;
157 #define INTERNAL_BUFFER_SIZE (32 + 1)
159 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
160 int linesize_align[AV_NUM_DATA_POINTERS])
166 switch (s->pix_fmt) {
167 case AV_PIX_FMT_YUV420P:
168 case AV_PIX_FMT_YUYV422:
169 case AV_PIX_FMT_UYVY422:
170 case AV_PIX_FMT_YUV422P:
171 case AV_PIX_FMT_YUV440P:
172 case AV_PIX_FMT_YUV444P:
173 case AV_PIX_FMT_GBRP:
174 case AV_PIX_FMT_GRAY8:
175 case AV_PIX_FMT_GRAY16BE:
176 case AV_PIX_FMT_GRAY16LE:
177 case AV_PIX_FMT_YUVJ420P:
178 case AV_PIX_FMT_YUVJ422P:
179 case AV_PIX_FMT_YUVJ440P:
180 case AV_PIX_FMT_YUVJ444P:
181 case AV_PIX_FMT_YUVA420P:
182 case AV_PIX_FMT_YUVA422P:
183 case AV_PIX_FMT_YUVA444P:
184 case AV_PIX_FMT_YUV420P9LE:
185 case AV_PIX_FMT_YUV420P9BE:
186 case AV_PIX_FMT_YUV420P10LE:
187 case AV_PIX_FMT_YUV420P10BE:
188 case AV_PIX_FMT_YUV422P9LE:
189 case AV_PIX_FMT_YUV422P9BE:
190 case AV_PIX_FMT_YUV422P10LE:
191 case AV_PIX_FMT_YUV422P10BE:
192 case AV_PIX_FMT_YUV444P9LE:
193 case AV_PIX_FMT_YUV444P9BE:
194 case AV_PIX_FMT_YUV444P10LE:
195 case AV_PIX_FMT_YUV444P10BE:
196 case AV_PIX_FMT_GBRP9LE:
197 case AV_PIX_FMT_GBRP9BE:
198 case AV_PIX_FMT_GBRP10LE:
199 case AV_PIX_FMT_GBRP10BE:
200 w_align = 16; //FIXME assume 16 pixel per macroblock
201 h_align = 16 * 2; // interlaced needs 2 macroblocks height
203 case AV_PIX_FMT_YUV411P:
204 case AV_PIX_FMT_UYYVYY411:
208 case AV_PIX_FMT_YUV410P:
209 if (s->codec_id == AV_CODEC_ID_SVQ1) {
213 case AV_PIX_FMT_RGB555:
214 if (s->codec_id == AV_CODEC_ID_RPZA) {
218 case AV_PIX_FMT_PAL8:
219 case AV_PIX_FMT_BGR8:
220 case AV_PIX_FMT_RGB8:
221 if (s->codec_id == AV_CODEC_ID_SMC) {
226 case AV_PIX_FMT_BGR24:
227 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
228 (s->codec_id == AV_CODEC_ID_ZLIB)) {
239 *width = FFALIGN(*width, w_align);
240 *height = FFALIGN(*height, h_align);
241 if (s->codec_id == AV_CODEC_ID_H264)
242 // some of the optimized chroma MC reads one line too much
245 for (i = 0; i < 4; i++)
246 linesize_align[i] = STRIDE_ALIGN;
249 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
251 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
252 int chroma_shift = desc->log2_chroma_w;
253 int linesize_align[AV_NUM_DATA_POINTERS];
256 avcodec_align_dimensions2(s, width, height, linesize_align);
257 align = FFMAX(linesize_align[0], linesize_align[3]);
258 linesize_align[1] <<= chroma_shift;
259 linesize_align[2] <<= chroma_shift;
260 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
261 *width = FFALIGN(*width, align);
264 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
265 enum AVSampleFormat sample_fmt, const uint8_t *buf,
266 int buf_size, int align)
268 int ch, planar, needed_size, ret = 0;
270 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
271 frame->nb_samples, sample_fmt,
273 if (buf_size < needed_size)
274 return AVERROR(EINVAL);
276 planar = av_sample_fmt_is_planar(sample_fmt);
277 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
278 if (!(frame->extended_data = av_mallocz(nb_channels *
279 sizeof(*frame->extended_data))))
280 return AVERROR(ENOMEM);
282 frame->extended_data = frame->data;
285 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
286 buf, nb_channels, frame->nb_samples,
287 sample_fmt, align)) < 0) {
288 if (frame->extended_data != frame->data)
289 av_free(frame->extended_data);
292 if (frame->extended_data != frame->data) {
293 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
294 frame->data[ch] = frame->extended_data[ch];
300 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
302 AVCodecInternal *avci = avctx->internal;
305 av_freep(&avci->audio_data);
306 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
307 frame->nb_samples, avctx->sample_fmt,
310 return AVERROR(EINVAL);
312 frame->data[0] = av_mallocz(buf_size);
314 return AVERROR(ENOMEM);
316 ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
317 frame->data[0], buf_size, 0);
319 av_freep(&frame->data[0]);
323 frame->type = FF_BUFFER_TYPE_INTERNAL;
325 avci->audio_data = frame->data[0];
326 if (avctx->debug & FF_DEBUG_BUFFERS)
327 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
328 "internal audio buffer used\n", frame);
333 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
339 AVCodecInternal *avci = s->internal;
341 if (pic->data[0] != NULL) {
342 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
345 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
346 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
350 if (av_image_check_size(w, h, 0, s))
354 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
355 sizeof(InternalBuffer));
358 buf = &avci->buffer[avci->buffer_count];
360 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
361 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
362 av_freep(&buf->base[i]);
368 int h_chroma_shift, v_chroma_shift;
373 int stride_align[AV_NUM_DATA_POINTERS];
374 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
375 const int pixel_size = desc->comp[0].step_minus1 + 1;
377 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
380 avcodec_align_dimensions2(s, &w, &h, stride_align);
382 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
388 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
389 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
390 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
391 // increase alignment of w for next try (rhs gives the lowest bit set in w)
395 for (i = 0; i < 4; i++)
396 unaligned |= picture.linesize[i] % stride_align[i];
399 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
403 for (i = 0; i < 3 && picture.data[i + 1]; i++)
404 size[i] = picture.data[i + 1] - picture.data[i];
405 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
407 memset(buf->base, 0, sizeof(buf->base));
408 memset(buf->data, 0, sizeof(buf->data));
410 for (i = 0; i < 4 && size[i]; i++) {
411 const int h_shift = i == 0 ? 0 : h_chroma_shift;
412 const int v_shift = i == 0 ? 0 : v_chroma_shift;
414 buf->linesize[i] = picture.linesize[i];
416 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
417 if (buf->base[i] == NULL)
419 memset(buf->base[i], 128, size[i]);
421 // no edge if EDGE EMU or not planar YUV
422 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
423 buf->data[i] = buf->base[i];
425 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
427 for (; i < AV_NUM_DATA_POINTERS; i++) {
428 buf->base[i] = buf->data[i] = NULL;
429 buf->linesize[i] = 0;
431 if (size[1] && !size[2])
432 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
433 buf->width = s->width;
434 buf->height = s->height;
435 buf->pix_fmt = s->pix_fmt;
437 pic->type = FF_BUFFER_TYPE_INTERNAL;
439 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
440 pic->base[i] = buf->base[i];
441 pic->data[i] = buf->data[i];
442 pic->linesize[i] = buf->linesize[i];
444 pic->extended_data = pic->data;
445 avci->buffer_count++;
447 if (s->debug & FF_DEBUG_BUFFERS)
448 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
449 "buffers used\n", pic, avci->buffer_count);
454 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
456 switch (avctx->codec_type) {
457 case AVMEDIA_TYPE_VIDEO:
458 return video_get_buffer(avctx, frame);
459 case AVMEDIA_TYPE_AUDIO:
460 return audio_get_buffer(avctx, frame);
466 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
468 switch (avctx->codec_type) {
469 case AVMEDIA_TYPE_VIDEO:
470 frame->width = avctx->width;
471 frame->height = avctx->height;
472 frame->format = avctx->pix_fmt;
473 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
475 case AVMEDIA_TYPE_AUDIO:
476 frame->sample_rate = avctx->sample_rate;
477 frame->format = avctx->sample_fmt;
478 frame->channel_layout = avctx->channel_layout;
480 default: return AVERROR(EINVAL);
483 frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
484 frame->reordered_opaque = avctx->reordered_opaque;
486 return avctx->get_buffer(avctx, frame);
489 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
492 InternalBuffer *buf, *last;
493 AVCodecInternal *avci = s->internal;
495 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
497 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
498 assert(avci->buffer_count);
501 buf = NULL; /* avoids warning */
502 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
503 buf = &avci->buffer[i];
504 if (buf->data[0] == pic->data[0])
507 assert(i < avci->buffer_count);
508 avci->buffer_count--;
509 last = &avci->buffer[avci->buffer_count];
512 FFSWAP(InternalBuffer, *buf, *last);
515 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
517 // pic->base[i]=NULL;
519 if (s->debug & FF_DEBUG_BUFFERS)
520 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
521 "buffers used\n", pic, avci->buffer_count);
524 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
529 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
531 /* If no picture return a new buffer */
532 if (pic->data[0] == NULL) {
533 /* We will copy from buffer, so must be readable */
534 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
535 return ff_get_buffer(s, pic);
538 assert(s->pix_fmt == pic->format);
540 /* If internal buffer type return the same buffer */
541 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
543 pic->pkt_pts = s->pkt->pts;
545 pic->pkt_pts = AV_NOPTS_VALUE;
546 pic->reordered_opaque = s->reordered_opaque;
551 * Not internal type and reget_buffer not overridden, emulate cr buffer
554 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
555 pic->data[i] = pic->base[i] = NULL;
557 /* Allocate new frame */
558 if (ff_get_buffer(s, pic))
560 /* Copy image data from old buffer to new buffer */
561 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
563 s->release_buffer(s, &temp_pic); // Release old frame
567 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
571 for (i = 0; i < count; i++) {
572 int r = func(c, (char *)arg + i * size);
579 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
583 for (i = 0; i < count; i++) {
584 int r = func(c, arg, i, 0);
591 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
593 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
594 return desc->flags & PIX_FMT_HWACCEL;
597 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
599 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
604 void avcodec_get_frame_defaults(AVFrame *frame)
606 if (frame->extended_data != frame->data)
607 av_freep(&frame->extended_data);
609 memset(frame, 0, sizeof(AVFrame));
611 frame->pts = AV_NOPTS_VALUE;
612 frame->key_frame = 1;
613 frame->sample_aspect_ratio = (AVRational) {0, 1 };
614 frame->format = -1; /* unknown */
615 frame->extended_data = frame->data;
618 AVFrame *avcodec_alloc_frame(void)
620 AVFrame *frame = av_mallocz(sizeof(AVFrame));
625 avcodec_get_frame_defaults(frame);
630 void avcodec_free_frame(AVFrame **frame)
634 if (!frame || !*frame)
639 if (f->extended_data != f->data)
640 av_freep(&f->extended_data);
645 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
648 AVDictionary *tmp = NULL;
650 if (avcodec_is_open(avctx))
653 if ((!codec && !avctx->codec)) {
654 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
655 return AVERROR(EINVAL);
657 if ((codec && avctx->codec && codec != avctx->codec)) {
658 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
659 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
660 return AVERROR(EINVAL);
663 codec = avctx->codec;
665 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
666 return AVERROR(EINVAL);
669 av_dict_copy(&tmp, *options, 0);
671 /* If there is a user-supplied mutex locking routine, call it. */
673 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
677 entangled_thread_counter++;
678 if (entangled_thread_counter != 1) {
679 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
684 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
685 if (!avctx->internal) {
686 ret = AVERROR(ENOMEM);
690 if (codec->priv_data_size > 0) {
691 if (!avctx->priv_data) {
692 avctx->priv_data = av_mallocz(codec->priv_data_size);
693 if (!avctx->priv_data) {
694 ret = AVERROR(ENOMEM);
697 if (codec->priv_class) {
698 *(const AVClass **)avctx->priv_data = codec->priv_class;
699 av_opt_set_defaults(avctx->priv_data);
702 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
705 avctx->priv_data = NULL;
707 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
710 if (avctx->coded_width && avctx->coded_height)
711 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
712 else if (avctx->width && avctx->height)
713 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
715 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
716 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
717 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
718 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
719 avcodec_set_dimensions(avctx, 0, 0);
722 /* if the decoder init function was already called previously,
723 * free the already allocated subtitle_header before overwriting it */
724 if (av_codec_is_decoder(codec))
725 av_freep(&avctx->subtitle_header);
727 if (avctx->channels > FF_SANE_NB_CHANNELS) {
728 ret = AVERROR(EINVAL);
732 avctx->codec = codec;
733 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
734 avctx->codec_id == AV_CODEC_ID_NONE) {
735 avctx->codec_type = codec->type;
736 avctx->codec_id = codec->id;
738 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
739 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
740 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
741 ret = AVERROR(EINVAL);
744 avctx->frame_number = 0;
746 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
747 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
748 ret = AVERROR_EXPERIMENTAL;
752 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
753 (!avctx->time_base.num || !avctx->time_base.den)) {
754 avctx->time_base.num = 1;
755 avctx->time_base.den = avctx->sample_rate;
758 if (HAVE_THREADS && !avctx->thread_opaque) {
759 ret = ff_thread_init(avctx);
764 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
765 avctx->thread_count = 1;
767 if (av_codec_is_encoder(avctx->codec)) {
769 if (avctx->codec->sample_fmts) {
770 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
771 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
773 if (avctx->channels == 1 &&
774 av_get_planar_sample_fmt(avctx->sample_fmt) ==
775 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
776 avctx->sample_fmt = avctx->codec->sample_fmts[i];
780 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
781 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
782 ret = AVERROR(EINVAL);
786 if (avctx->codec->pix_fmts) {
787 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
788 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
790 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
791 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
792 ret = AVERROR(EINVAL);
796 if (avctx->codec->supported_samplerates) {
797 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
798 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
800 if (avctx->codec->supported_samplerates[i] == 0) {
801 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
802 ret = AVERROR(EINVAL);
806 if (avctx->codec->channel_layouts) {
807 if (!avctx->channel_layout) {
808 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
810 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
811 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
813 if (avctx->codec->channel_layouts[i] == 0) {
814 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
815 ret = AVERROR(EINVAL);
820 if (avctx->channel_layout && avctx->channels) {
821 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
822 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
823 ret = AVERROR(EINVAL);
826 } else if (avctx->channel_layout) {
827 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
831 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
832 ret = avctx->codec->init(avctx);
838 if (av_codec_is_decoder(avctx->codec)) {
839 /* validate channel layout from the decoder */
840 if (avctx->channel_layout) {
841 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
842 if (!avctx->channels)
843 avctx->channels = channels;
844 else if (channels != avctx->channels) {
845 av_log(avctx, AV_LOG_WARNING,
846 "channel layout does not match number of channels\n");
847 avctx->channel_layout = 0;
850 if (avctx->channels && avctx->channels < 0 ||
851 avctx->channels > FF_SANE_NB_CHANNELS) {
852 ret = AVERROR(EINVAL);
857 entangled_thread_counter--;
859 /* Release any user-supplied mutex. */
861 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
864 av_dict_free(options);
871 av_freep(&avctx->priv_data);
872 av_freep(&avctx->internal);
877 int ff_alloc_packet(AVPacket *avpkt, int size)
879 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
880 return AVERROR(EINVAL);
883 void *destruct = avpkt->destruct;
885 if (avpkt->size < size)
886 return AVERROR(EINVAL);
888 av_init_packet(avpkt);
889 avpkt->destruct = destruct;
893 return av_new_packet(avpkt, size);
898 * Pad last frame with silence.
900 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
902 AVFrame *frame = NULL;
906 if (!(frame = avcodec_alloc_frame()))
907 return AVERROR(ENOMEM);
910 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
911 s->frame_size, s->sample_fmt, 0)) < 0)
914 if (!(buf = av_malloc(ret))) {
915 ret = AVERROR(ENOMEM);
919 frame->nb_samples = s->frame_size;
920 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
923 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
924 src->nb_samples, s->channels, s->sample_fmt)) < 0)
926 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
927 frame->nb_samples - src->nb_samples,
928 s->channels, s->sample_fmt)) < 0)
936 if (frame->extended_data != frame->data)
937 av_freep(&frame->extended_data);
943 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
945 const AVFrame *frame,
949 AVFrame *padded_frame = NULL;
951 int user_packet = !!avpkt->data;
955 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
956 av_free_packet(avpkt);
957 av_init_packet(avpkt);
961 /* ensure that extended_data is properly set */
962 if (frame && !frame->extended_data) {
963 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
964 avctx->channels > AV_NUM_DATA_POINTERS) {
965 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
966 "with more than %d channels, but extended_data is not set.\n",
967 AV_NUM_DATA_POINTERS);
968 return AVERROR(EINVAL);
970 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
973 tmp.extended_data = tmp.data;
977 /* check for valid frame size */
979 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
980 if (frame->nb_samples > avctx->frame_size)
981 return AVERROR(EINVAL);
982 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
983 if (frame->nb_samples < avctx->frame_size &&
984 !avctx->internal->last_audio_frame) {
985 ret = pad_last_frame(avctx, &padded_frame, frame);
989 frame = padded_frame;
990 avctx->internal->last_audio_frame = 1;
993 if (frame->nb_samples != avctx->frame_size) {
994 ret = AVERROR(EINVAL);
1000 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1002 if (*got_packet_ptr) {
1003 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1004 if (avpkt->pts == AV_NOPTS_VALUE)
1005 avpkt->pts = frame->pts;
1006 if (!avpkt->duration)
1007 avpkt->duration = ff_samples_to_time_base(avctx,
1010 avpkt->dts = avpkt->pts;
1015 if (!user_packet && avpkt->size) {
1016 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1018 avpkt->data = new_data;
1021 avctx->frame_number++;
1024 if (ret < 0 || !*got_packet_ptr) {
1025 av_free_packet(avpkt);
1026 av_init_packet(avpkt);
1030 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1031 * this needs to be moved to the encoders, but for now we can do it
1032 * here to simplify things */
1033 avpkt->flags |= AV_PKT_FLAG_KEY;
1037 av_freep(&padded_frame->data[0]);
1038 if (padded_frame->extended_data != padded_frame->data)
1039 av_freep(&padded_frame->extended_data);
1040 av_freep(&padded_frame);
1046 #if FF_API_OLD_ENCODE_AUDIO
1047 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1048 uint8_t *buf, int buf_size,
1049 const short *samples)
1052 AVFrame frame0 = { 0 };
1054 int ret, samples_size, got_packet;
1056 av_init_packet(&pkt);
1058 pkt.size = buf_size;
1062 avcodec_get_frame_defaults(frame);
1064 if (avctx->frame_size) {
1065 frame->nb_samples = avctx->frame_size;
1067 /* if frame_size is not set, the number of samples must be
1068 * calculated from the buffer size */
1070 if (!av_get_bits_per_sample(avctx->codec_id)) {
1071 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1072 "support this codec\n");
1073 return AVERROR(EINVAL);
1075 nb_samples = (int64_t)buf_size * 8 /
1076 (av_get_bits_per_sample(avctx->codec_id) *
1078 if (nb_samples >= INT_MAX)
1079 return AVERROR(EINVAL);
1080 frame->nb_samples = nb_samples;
1083 /* it is assumed that the samples buffer is large enough based on the
1084 * relevant parameters */
1085 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1087 avctx->sample_fmt, 1);
1088 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1090 (const uint8_t *)samples,
1094 /* fabricate frame pts from sample count.
1095 * this is needed because the avcodec_encode_audio() API does not have
1096 * a way for the user to provide pts */
1097 frame->pts = ff_samples_to_time_base(avctx,
1098 avctx->internal->sample_count);
1099 avctx->internal->sample_count += frame->nb_samples;
1105 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1106 if (!ret && got_packet && avctx->coded_frame) {
1107 avctx->coded_frame->pts = pkt.pts;
1108 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1110 /* free any side data since we cannot return it */
1111 if (pkt.side_data_elems > 0) {
1113 for (i = 0; i < pkt.side_data_elems; i++)
1114 av_free(pkt.side_data[i].data);
1115 av_freep(&pkt.side_data);
1116 pkt.side_data_elems = 0;
1119 if (frame && frame->extended_data != frame->data)
1120 av_free(frame->extended_data);
1122 return ret ? ret : pkt.size;
1127 #if FF_API_OLD_ENCODE_VIDEO
1128 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1129 const AVFrame *pict)
1132 int ret, got_packet = 0;
1134 if (buf_size < FF_MIN_BUFFER_SIZE) {
1135 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1139 av_init_packet(&pkt);
1141 pkt.size = buf_size;
1143 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1144 if (!ret && got_packet && avctx->coded_frame) {
1145 avctx->coded_frame->pts = pkt.pts;
1146 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1149 /* free any side data since we cannot return it */
1150 if (pkt.side_data_elems > 0) {
1152 for (i = 0; i < pkt.side_data_elems; i++)
1153 av_free(pkt.side_data[i].data);
1154 av_freep(&pkt.side_data);
1155 pkt.side_data_elems = 0;
1158 return ret ? ret : pkt.size;
1163 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1165 const AVFrame *frame,
1166 int *got_packet_ptr)
1169 int user_packet = !!avpkt->data;
1171 *got_packet_ptr = 0;
1173 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1174 av_free_packet(avpkt);
1175 av_init_packet(avpkt);
1180 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1181 return AVERROR(EINVAL);
1183 av_assert0(avctx->codec->encode2);
1185 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1187 if (!*got_packet_ptr)
1189 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1190 avpkt->pts = avpkt->dts = frame->pts;
1192 if (!user_packet && avpkt->size) {
1193 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1195 avpkt->data = new_data;
1198 avctx->frame_number++;
1201 if (ret < 0 || !*got_packet_ptr)
1202 av_free_packet(avpkt);
1208 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1209 const AVSubtitle *sub)
1212 if (sub->start_display_time) {
1213 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1216 if (sub->num_rects == 0 || !sub->rects)
1218 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1219 avctx->frame_number++;
1223 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1226 const uint8_t *data;
1229 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1232 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1233 if (!data || size < 4)
1235 flags = bytestream_get_le32(&data);
1237 if (size < 4) /* Required for any of the changes */
1239 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1240 avctx->channels = bytestream_get_le32(&data);
1243 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1246 avctx->channel_layout = bytestream_get_le64(&data);
1251 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1252 avctx->sample_rate = bytestream_get_le32(&data);
1255 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1258 avctx->width = bytestream_get_le32(&data);
1259 avctx->height = bytestream_get_le32(&data);
1260 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1265 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1266 int *got_picture_ptr,
1271 *got_picture_ptr = 0;
1272 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1276 apply_param_change(avctx, avpkt);
1278 avcodec_get_frame_defaults(picture);
1280 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1281 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1282 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1285 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1287 picture->pkt_dts = avpkt->dts;
1288 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1289 picture->width = avctx->width;
1290 picture->height = avctx->height;
1291 picture->format = avctx->pix_fmt;
1294 emms_c(); //needed to avoid an emms_c() call before every return;
1296 if (*got_picture_ptr)
1297 avctx->frame_number++;
1301 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1302 * make sure it's set correctly */
1303 picture->extended_data = picture->data;
1308 #if FF_API_OLD_DECODE_AUDIO
1309 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1310 int *frame_size_ptr,
1313 AVFrame frame = {0};
1314 int ret, got_frame = 0;
1316 if (avctx->get_buffer != avcodec_default_get_buffer) {
1317 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1318 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1319 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1320 "avcodec_decode_audio4()\n");
1321 avctx->get_buffer = avcodec_default_get_buffer;
1324 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1326 if (ret >= 0 && got_frame) {
1328 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1329 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1331 avctx->sample_fmt, 1);
1332 if (*frame_size_ptr < data_size) {
1333 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1334 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1335 return AVERROR(EINVAL);
1338 memcpy(samples, frame.extended_data[0], plane_size);
1340 if (planar && avctx->channels > 1) {
1341 uint8_t *out = ((uint8_t *)samples) + plane_size;
1342 for (ch = 1; ch < avctx->channels; ch++) {
1343 memcpy(out, frame.extended_data[ch], plane_size);
1347 *frame_size_ptr = data_size;
1349 *frame_size_ptr = 0;
1356 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1361 int planar, channels;
1368 if (!avpkt->data && avpkt->size) {
1369 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1370 return AVERROR(EINVAL);
1373 apply_param_change(avctx, avpkt);
1375 avcodec_get_frame_defaults(frame);
1377 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1378 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1379 if (ret >= 0 && *got_frame_ptr) {
1380 avctx->frame_number++;
1381 frame->pkt_dts = avpkt->dts;
1382 if (frame->format == AV_SAMPLE_FMT_NONE)
1383 frame->format = avctx->sample_fmt;
1387 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1388 * make sure it's set correctly; assume decoders that actually use
1389 * extended_data are doing it correctly */
1390 planar = av_sample_fmt_is_planar(frame->format);
1391 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1392 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1393 frame->extended_data = frame->data;
1398 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1406 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1408 avctx->frame_number++;
1412 void avsubtitle_free(AVSubtitle *sub)
1416 for (i = 0; i < sub->num_rects; i++) {
1417 av_freep(&sub->rects[i]->pict.data[0]);
1418 av_freep(&sub->rects[i]->pict.data[1]);
1419 av_freep(&sub->rects[i]->pict.data[2]);
1420 av_freep(&sub->rects[i]->pict.data[3]);
1421 av_freep(&sub->rects[i]->text);
1422 av_freep(&sub->rects[i]->ass);
1423 av_freep(&sub->rects[i]);
1426 av_freep(&sub->rects);
1428 memset(sub, 0, sizeof(AVSubtitle));
1431 av_cold int avcodec_close(AVCodecContext *avctx)
1433 /* If there is a user-supplied mutex locking routine, call it. */
1434 if (ff_lockmgr_cb) {
1435 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1439 entangled_thread_counter++;
1440 if (entangled_thread_counter != 1) {
1441 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1442 entangled_thread_counter--;
1446 if (avcodec_is_open(avctx)) {
1447 if (HAVE_THREADS && avctx->thread_opaque)
1448 ff_thread_free(avctx);
1449 if (avctx->codec && avctx->codec->close)
1450 avctx->codec->close(avctx);
1451 avcodec_default_free_buffers(avctx);
1452 avctx->coded_frame = NULL;
1453 av_freep(&avctx->internal);
1456 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1457 av_opt_free(avctx->priv_data);
1459 av_freep(&avctx->priv_data);
1460 if (av_codec_is_encoder(avctx->codec))
1461 av_freep(&avctx->extradata);
1462 avctx->codec = NULL;
1463 avctx->active_thread_type = 0;
1464 entangled_thread_counter--;
1466 /* Release any user-supplied mutex. */
1467 if (ff_lockmgr_cb) {
1468 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1473 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1475 AVCodec *p, *experimental = NULL;
1478 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1480 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1487 return experimental;
1490 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1492 return find_encdec(id, 1);
1495 AVCodec *avcodec_find_encoder_by_name(const char *name)
1502 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1509 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1511 return find_encdec(id, 0);
1514 AVCodec *avcodec_find_decoder_by_name(const char *name)
1521 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1528 static int get_bit_rate(AVCodecContext *ctx)
1531 int bits_per_sample;
1533 switch (ctx->codec_type) {
1534 case AVMEDIA_TYPE_VIDEO:
1535 case AVMEDIA_TYPE_DATA:
1536 case AVMEDIA_TYPE_SUBTITLE:
1537 case AVMEDIA_TYPE_ATTACHMENT:
1538 bit_rate = ctx->bit_rate;
1540 case AVMEDIA_TYPE_AUDIO:
1541 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1542 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1551 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1553 int i, len, ret = 0;
1555 for (i = 0; i < 4; i++) {
1556 len = snprintf(buf, buf_size,
1557 isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1559 buf_size = buf_size > len ? buf_size - len : 0;
1566 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1568 const char *codec_name;
1569 const char *profile = NULL;
1573 AVRational display_aspect_ratio;
1578 p = avcodec_find_encoder(enc->codec_id);
1580 p = avcodec_find_decoder(enc->codec_id);
1583 codec_name = p->name;
1584 profile = av_get_profile_name(p, enc->profile);
1585 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1586 /* fake mpeg2 transport stream codec (currently not
1588 codec_name = "mpeg2ts";
1589 } else if (enc->codec_name[0] != '\0') {
1590 codec_name = enc->codec_name;
1592 /* output avi tags */
1594 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1595 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1599 switch (enc->codec_type) {
1600 case AVMEDIA_TYPE_VIDEO:
1601 snprintf(buf, buf_size,
1603 codec_name, enc->mb_decision ? " (hq)" : "");
1605 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1607 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1608 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1610 av_get_pix_fmt_name(enc->pix_fmt));
1613 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1615 enc->width, enc->height);
1616 if (enc->sample_aspect_ratio.num) {
1617 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1618 enc->width * enc->sample_aspect_ratio.num,
1619 enc->height * enc->sample_aspect_ratio.den,
1621 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1622 " [PAR %d:%d DAR %d:%d]",
1623 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1624 display_aspect_ratio.num, display_aspect_ratio.den);
1626 if (av_log_get_level() >= AV_LOG_DEBUG) {
1627 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1628 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1630 enc->time_base.num / g, enc->time_base.den / g);
1634 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1635 ", q=%d-%d", enc->qmin, enc->qmax);
1638 case AVMEDIA_TYPE_AUDIO:
1639 snprintf(buf, buf_size,
1643 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1645 if (enc->sample_rate) {
1646 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1647 ", %d Hz", enc->sample_rate);
1649 av_strlcat(buf, ", ", buf_size);
1650 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1651 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1652 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1653 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1656 case AVMEDIA_TYPE_DATA:
1657 snprintf(buf, buf_size, "Data: %s", codec_name);
1659 case AVMEDIA_TYPE_SUBTITLE:
1660 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1662 case AVMEDIA_TYPE_ATTACHMENT:
1663 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1666 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1670 if (enc->flags & CODEC_FLAG_PASS1)
1671 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1673 if (enc->flags & CODEC_FLAG_PASS2)
1674 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1677 bitrate = get_bit_rate(enc);
1679 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1680 ", %d kb/s", bitrate / 1000);
1684 const char *av_get_profile_name(const AVCodec *codec, int profile)
1687 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1690 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1691 if (p->profile == profile)
1697 unsigned avcodec_version(void)
1699 return LIBAVCODEC_VERSION_INT;
1702 const char *avcodec_configuration(void)
1704 return LIBAV_CONFIGURATION;
1707 const char *avcodec_license(void)
1709 #define LICENSE_PREFIX "libavcodec license: "
1710 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1713 void avcodec_flush_buffers(AVCodecContext *avctx)
1715 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1716 ff_thread_flush(avctx);
1717 else if (avctx->codec->flush)
1718 avctx->codec->flush(avctx);
1721 static void video_free_buffers(AVCodecContext *s)
1723 AVCodecInternal *avci = s->internal;
1729 if (avci->buffer_count)
1730 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1731 avci->buffer_count);
1732 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
1733 InternalBuffer *buf = &avci->buffer[i];
1734 for (j = 0; j < 4; j++) {
1735 av_freep(&buf->base[j]);
1736 buf->data[j] = NULL;
1739 av_freep(&avci->buffer);
1741 avci->buffer_count = 0;
1744 static void audio_free_buffers(AVCodecContext *avctx)
1746 AVCodecInternal *avci = avctx->internal;
1747 av_freep(&avci->audio_data);
1750 void avcodec_default_free_buffers(AVCodecContext *avctx)
1752 switch (avctx->codec_type) {
1753 case AVMEDIA_TYPE_VIDEO:
1754 video_free_buffers(avctx);
1756 case AVMEDIA_TYPE_AUDIO:
1757 audio_free_buffers(avctx);
1764 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1767 case AV_CODEC_ID_ADPCM_CT:
1768 case AV_CODEC_ID_ADPCM_IMA_APC:
1769 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1770 case AV_CODEC_ID_ADPCM_IMA_WS:
1771 case AV_CODEC_ID_ADPCM_G722:
1772 case AV_CODEC_ID_ADPCM_YAMAHA:
1774 case AV_CODEC_ID_PCM_ALAW:
1775 case AV_CODEC_ID_PCM_MULAW:
1776 case AV_CODEC_ID_PCM_S8:
1777 case AV_CODEC_ID_PCM_U8:
1778 case AV_CODEC_ID_PCM_ZORK:
1780 case AV_CODEC_ID_PCM_S16BE:
1781 case AV_CODEC_ID_PCM_S16LE:
1782 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1783 case AV_CODEC_ID_PCM_U16BE:
1784 case AV_CODEC_ID_PCM_U16LE:
1786 case AV_CODEC_ID_PCM_S24DAUD:
1787 case AV_CODEC_ID_PCM_S24BE:
1788 case AV_CODEC_ID_PCM_S24LE:
1789 case AV_CODEC_ID_PCM_U24BE:
1790 case AV_CODEC_ID_PCM_U24LE:
1792 case AV_CODEC_ID_PCM_S32BE:
1793 case AV_CODEC_ID_PCM_S32LE:
1794 case AV_CODEC_ID_PCM_U32BE:
1795 case AV_CODEC_ID_PCM_U32LE:
1796 case AV_CODEC_ID_PCM_F32BE:
1797 case AV_CODEC_ID_PCM_F32LE:
1799 case AV_CODEC_ID_PCM_F64BE:
1800 case AV_CODEC_ID_PCM_F64LE:
1807 int av_get_bits_per_sample(enum AVCodecID codec_id)
1810 case AV_CODEC_ID_ADPCM_SBPRO_2:
1812 case AV_CODEC_ID_ADPCM_SBPRO_3:
1814 case AV_CODEC_ID_ADPCM_SBPRO_4:
1815 case AV_CODEC_ID_ADPCM_IMA_WAV:
1816 case AV_CODEC_ID_ADPCM_IMA_QT:
1817 case AV_CODEC_ID_ADPCM_SWF:
1818 case AV_CODEC_ID_ADPCM_MS:
1821 return av_get_exact_bits_per_sample(codec_id);
1825 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1827 int id, sr, ch, ba, tag, bps;
1829 id = avctx->codec_id;
1830 sr = avctx->sample_rate;
1831 ch = avctx->channels;
1832 ba = avctx->block_align;
1833 tag = avctx->codec_tag;
1834 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1836 /* codecs with an exact constant bits per sample */
1837 if (bps > 0 && ch > 0 && frame_bytes > 0)
1838 return (frame_bytes * 8) / (bps * ch);
1839 bps = avctx->bits_per_coded_sample;
1841 /* codecs with a fixed packet duration */
1843 case AV_CODEC_ID_ADPCM_ADX: return 32;
1844 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1845 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1846 case AV_CODEC_ID_AMR_NB:
1847 case AV_CODEC_ID_GSM:
1848 case AV_CODEC_ID_QCELP:
1849 case AV_CODEC_ID_RA_144:
1850 case AV_CODEC_ID_RA_288: return 160;
1851 case AV_CODEC_ID_IMC: return 256;
1852 case AV_CODEC_ID_AMR_WB:
1853 case AV_CODEC_ID_GSM_MS: return 320;
1854 case AV_CODEC_ID_MP1: return 384;
1855 case AV_CODEC_ID_ATRAC1: return 512;
1856 case AV_CODEC_ID_ATRAC3: return 1024;
1857 case AV_CODEC_ID_MP2:
1858 case AV_CODEC_ID_MUSEPACK7: return 1152;
1859 case AV_CODEC_ID_AC3: return 1536;
1863 /* calc from sample rate */
1864 if (id == AV_CODEC_ID_TTA)
1865 return 256 * sr / 245;
1868 /* calc from sample rate and channels */
1869 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1870 return (480 << (sr / 22050)) / ch;
1875 /* calc from block_align */
1876 if (id == AV_CODEC_ID_SIPR) {
1878 case 20: return 160;
1879 case 19: return 144;
1880 case 29: return 288;
1881 case 37: return 480;
1883 } else if (id == AV_CODEC_ID_ILBC) {
1885 case 38: return 160;
1886 case 50: return 240;
1891 if (frame_bytes > 0) {
1892 /* calc from frame_bytes only */
1893 if (id == AV_CODEC_ID_TRUESPEECH)
1894 return 240 * (frame_bytes / 32);
1895 if (id == AV_CODEC_ID_NELLYMOSER)
1896 return 256 * (frame_bytes / 64);
1899 /* calc from frame_bytes and bits_per_coded_sample */
1900 if (id == AV_CODEC_ID_ADPCM_G726)
1901 return frame_bytes * 8 / bps;
1905 /* calc from frame_bytes and channels */
1907 case AV_CODEC_ID_ADPCM_4XM:
1908 case AV_CODEC_ID_ADPCM_IMA_ISS:
1909 return (frame_bytes - 4 * ch) * 2 / ch;
1910 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1911 return (frame_bytes - 4) * 2 / ch;
1912 case AV_CODEC_ID_ADPCM_IMA_AMV:
1913 return (frame_bytes - 8) * 2 / ch;
1914 case AV_CODEC_ID_ADPCM_XA:
1915 return (frame_bytes / 128) * 224 / ch;
1916 case AV_CODEC_ID_INTERPLAY_DPCM:
1917 return (frame_bytes - 6 - ch) / ch;
1918 case AV_CODEC_ID_ROQ_DPCM:
1919 return (frame_bytes - 8) / ch;
1920 case AV_CODEC_ID_XAN_DPCM:
1921 return (frame_bytes - 2 * ch) / ch;
1922 case AV_CODEC_ID_MACE3:
1923 return 3 * frame_bytes / ch;
1924 case AV_CODEC_ID_MACE6:
1925 return 6 * frame_bytes / ch;
1926 case AV_CODEC_ID_PCM_LXF:
1927 return 2 * (frame_bytes / (5 * ch));
1931 /* calc from frame_bytes, channels, and codec_tag */
1932 if (id == AV_CODEC_ID_SOL_DPCM) {
1934 return frame_bytes / ch;
1936 return frame_bytes * 2 / ch;
1941 /* calc from frame_bytes, channels, and block_align */
1942 int blocks = frame_bytes / ba;
1943 switch (avctx->codec_id) {
1944 case AV_CODEC_ID_ADPCM_IMA_WAV:
1945 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1946 case AV_CODEC_ID_ADPCM_IMA_DK3:
1947 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1948 case AV_CODEC_ID_ADPCM_IMA_DK4:
1949 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1950 case AV_CODEC_ID_ADPCM_MS:
1951 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1956 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1957 switch (avctx->codec_id) {
1958 case AV_CODEC_ID_PCM_DVD:
1959 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1960 case AV_CODEC_ID_PCM_BLURAY:
1961 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1962 case AV_CODEC_ID_S302M:
1963 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1973 int ff_thread_init(AVCodecContext *s)
1980 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1994 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
1997 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2001 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2003 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2004 "version to the newest one from Git. If the problem still "
2005 "occurs, it means that your file has a feature which has not "
2006 "been implemented.\n", feature);
2008 av_log_ask_for_sample(avc, NULL);
2011 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2013 va_list argument_list;
2015 va_start(argument_list, msg);
2018 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2019 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2020 "of this file to ftp://upload.libav.org/incoming/ "
2021 "and contact the libav-devel mailing list.\n");
2023 va_end(argument_list);
2026 static AVHWAccel *first_hwaccel = NULL;
2028 void av_register_hwaccel(AVHWAccel *hwaccel)
2030 AVHWAccel **p = &first_hwaccel;
2034 hwaccel->next = NULL;
2037 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2039 return hwaccel ? hwaccel->next : first_hwaccel;
2042 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2044 AVHWAccel *hwaccel = NULL;
2046 while ((hwaccel = av_hwaccel_next(hwaccel)))
2047 if (hwaccel->id == codec_id
2048 && hwaccel->pix_fmt == pix_fmt)
2053 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2055 if (ff_lockmgr_cb) {
2056 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2058 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2064 if (ff_lockmgr_cb) {
2065 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2067 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2073 int avpriv_lock_avformat(void)
2075 if (ff_lockmgr_cb) {
2076 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2082 int avpriv_unlock_avformat(void)
2084 if (ff_lockmgr_cb) {
2085 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2091 unsigned int avpriv_toupper4(unsigned int x)
2093 return toupper(x & 0xFF)
2094 + (toupper((x >> 8) & 0xFF) << 8)
2095 + (toupper((x >> 16) & 0xFF) << 16)
2096 + (toupper((x >> 24) & 0xFF) << 24);
2101 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2104 return ff_get_buffer(avctx, f);
2107 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2109 f->owner->release_buffer(f->owner, f);
2112 void ff_thread_finish_setup(AVCodecContext *avctx)
2116 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2120 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2126 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2128 if (codec_id <= AV_CODEC_ID_NONE)
2129 return AVMEDIA_TYPE_UNKNOWN;
2130 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2131 return AVMEDIA_TYPE_VIDEO;
2132 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2133 return AVMEDIA_TYPE_AUDIO;
2134 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2135 return AVMEDIA_TYPE_SUBTITLE;
2137 return AVMEDIA_TYPE_UNKNOWN;
2140 int avcodec_is_open(AVCodecContext *s)
2142 return !!s->internal;