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 avci->audio_data = frame->data[0];
324 if (avctx->debug & FF_DEBUG_BUFFERS)
325 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
326 "internal audio buffer used\n", frame);
331 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
337 AVCodecInternal *avci = s->internal;
339 if (pic->data[0] != NULL) {
340 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
343 if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
344 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
348 if (av_image_check_size(w, h, 0, s))
352 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
353 sizeof(InternalBuffer));
356 buf = &avci->buffer[avci->buffer_count];
358 if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
359 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
360 av_freep(&buf->base[i]);
366 int h_chroma_shift, v_chroma_shift;
371 int stride_align[AV_NUM_DATA_POINTERS];
372 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
373 const int pixel_size = desc->comp[0].step_minus1 + 1;
375 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
378 avcodec_align_dimensions2(s, &w, &h, stride_align);
380 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
386 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
387 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
388 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
389 // increase alignment of w for next try (rhs gives the lowest bit set in w)
393 for (i = 0; i < 4; i++)
394 unaligned |= picture.linesize[i] % stride_align[i];
397 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
401 for (i = 0; i < 3 && picture.data[i + 1]; i++)
402 size[i] = picture.data[i + 1] - picture.data[i];
403 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
405 memset(buf->base, 0, sizeof(buf->base));
406 memset(buf->data, 0, sizeof(buf->data));
408 for (i = 0; i < 4 && size[i]; i++) {
409 const int h_shift = i == 0 ? 0 : h_chroma_shift;
410 const int v_shift = i == 0 ? 0 : v_chroma_shift;
412 buf->linesize[i] = picture.linesize[i];
414 buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
415 if (buf->base[i] == NULL)
417 memset(buf->base[i], 128, size[i]);
419 // no edge if EDGE EMU or not planar YUV
420 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
421 buf->data[i] = buf->base[i];
423 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
425 for (; i < AV_NUM_DATA_POINTERS; i++) {
426 buf->base[i] = buf->data[i] = NULL;
427 buf->linesize[i] = 0;
429 if (size[1] && !size[2])
430 avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
431 buf->width = s->width;
432 buf->height = s->height;
433 buf->pix_fmt = s->pix_fmt;
436 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
437 pic->base[i] = buf->base[i];
438 pic->data[i] = buf->data[i];
439 pic->linesize[i] = buf->linesize[i];
441 pic->extended_data = pic->data;
442 avci->buffer_count++;
444 if (s->debug & FF_DEBUG_BUFFERS)
445 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
446 "buffers used\n", pic, avci->buffer_count);
451 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
453 frame->type = FF_BUFFER_TYPE_INTERNAL;
454 switch (avctx->codec_type) {
455 case AVMEDIA_TYPE_VIDEO:
456 return video_get_buffer(avctx, frame);
457 case AVMEDIA_TYPE_AUDIO:
458 return audio_get_buffer(avctx, frame);
464 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
466 switch (avctx->codec_type) {
467 case AVMEDIA_TYPE_VIDEO:
468 frame->width = avctx->width;
469 frame->height = avctx->height;
470 frame->format = avctx->pix_fmt;
471 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
473 case AVMEDIA_TYPE_AUDIO:
474 frame->sample_rate = avctx->sample_rate;
475 frame->format = avctx->sample_fmt;
476 frame->channel_layout = avctx->channel_layout;
478 default: return AVERROR(EINVAL);
481 frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
482 frame->reordered_opaque = avctx->reordered_opaque;
484 return avctx->get_buffer(avctx, frame);
487 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
490 InternalBuffer *buf, *last;
491 AVCodecInternal *avci = s->internal;
493 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
495 assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
496 assert(avci->buffer_count);
499 buf = NULL; /* avoids warning */
500 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
501 buf = &avci->buffer[i];
502 if (buf->data[0] == pic->data[0])
505 assert(i < avci->buffer_count);
506 avci->buffer_count--;
507 last = &avci->buffer[avci->buffer_count];
510 FFSWAP(InternalBuffer, *buf, *last);
513 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
515 // pic->base[i]=NULL;
517 if (s->debug & FF_DEBUG_BUFFERS)
518 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
519 "buffers used\n", pic, avci->buffer_count);
522 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
527 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
529 /* If no picture return a new buffer */
530 if (pic->data[0] == NULL) {
531 /* We will copy from buffer, so must be readable */
532 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
533 return ff_get_buffer(s, pic);
536 assert(s->pix_fmt == pic->format);
538 /* If internal buffer type return the same buffer */
539 if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
541 pic->pkt_pts = s->pkt->pts;
543 pic->pkt_pts = AV_NOPTS_VALUE;
544 pic->reordered_opaque = s->reordered_opaque;
549 * Not internal type and reget_buffer not overridden, emulate cr buffer
552 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
553 pic->data[i] = pic->base[i] = NULL;
555 /* Allocate new frame */
556 if (ff_get_buffer(s, pic))
558 /* Copy image data from old buffer to new buffer */
559 av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
561 s->release_buffer(s, &temp_pic); // Release old frame
565 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
569 for (i = 0; i < count; i++) {
570 int r = func(c, (char *)arg + i * size);
577 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
581 for (i = 0; i < count; i++) {
582 int r = func(c, arg, i, 0);
589 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
591 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
592 return desc->flags & PIX_FMT_HWACCEL;
595 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
597 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
602 void avcodec_get_frame_defaults(AVFrame *frame)
604 if (frame->extended_data != frame->data)
605 av_freep(&frame->extended_data);
607 memset(frame, 0, sizeof(AVFrame));
609 frame->pts = AV_NOPTS_VALUE;
610 frame->key_frame = 1;
611 frame->sample_aspect_ratio = (AVRational) {0, 1 };
612 frame->format = -1; /* unknown */
613 frame->extended_data = frame->data;
616 AVFrame *avcodec_alloc_frame(void)
618 AVFrame *frame = av_mallocz(sizeof(AVFrame));
623 avcodec_get_frame_defaults(frame);
628 void avcodec_free_frame(AVFrame **frame)
632 if (!frame || !*frame)
637 if (f->extended_data != f->data)
638 av_freep(&f->extended_data);
643 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
646 AVDictionary *tmp = NULL;
648 if (avcodec_is_open(avctx))
651 if ((!codec && !avctx->codec)) {
652 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
653 return AVERROR(EINVAL);
655 if ((codec && avctx->codec && codec != avctx->codec)) {
656 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
657 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
658 return AVERROR(EINVAL);
661 codec = avctx->codec;
663 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
664 return AVERROR(EINVAL);
667 av_dict_copy(&tmp, *options, 0);
669 /* If there is a user-supplied mutex locking routine, call it. */
671 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
675 entangled_thread_counter++;
676 if (entangled_thread_counter != 1) {
677 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
682 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
683 if (!avctx->internal) {
684 ret = AVERROR(ENOMEM);
688 if (codec->priv_data_size > 0) {
689 if (!avctx->priv_data) {
690 avctx->priv_data = av_mallocz(codec->priv_data_size);
691 if (!avctx->priv_data) {
692 ret = AVERROR(ENOMEM);
695 if (codec->priv_class) {
696 *(const AVClass **)avctx->priv_data = codec->priv_class;
697 av_opt_set_defaults(avctx->priv_data);
700 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
703 avctx->priv_data = NULL;
705 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
708 if (avctx->coded_width && avctx->coded_height)
709 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
710 else if (avctx->width && avctx->height)
711 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
713 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
714 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
715 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
716 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
717 avcodec_set_dimensions(avctx, 0, 0);
720 /* if the decoder init function was already called previously,
721 * free the already allocated subtitle_header before overwriting it */
722 if (av_codec_is_decoder(codec))
723 av_freep(&avctx->subtitle_header);
725 if (avctx->channels > FF_SANE_NB_CHANNELS) {
726 ret = AVERROR(EINVAL);
730 avctx->codec = codec;
731 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
732 avctx->codec_id == AV_CODEC_ID_NONE) {
733 avctx->codec_type = codec->type;
734 avctx->codec_id = codec->id;
736 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
737 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
738 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
739 ret = AVERROR(EINVAL);
742 avctx->frame_number = 0;
744 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
745 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
746 ret = AVERROR_EXPERIMENTAL;
750 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
751 (!avctx->time_base.num || !avctx->time_base.den)) {
752 avctx->time_base.num = 1;
753 avctx->time_base.den = avctx->sample_rate;
756 if (HAVE_THREADS && !avctx->thread_opaque) {
757 ret = ff_thread_init(avctx);
762 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
763 avctx->thread_count = 1;
765 if (av_codec_is_encoder(avctx->codec)) {
767 if (avctx->codec->sample_fmts) {
768 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
769 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
771 if (avctx->channels == 1 &&
772 av_get_planar_sample_fmt(avctx->sample_fmt) ==
773 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
774 avctx->sample_fmt = avctx->codec->sample_fmts[i];
778 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
779 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
780 ret = AVERROR(EINVAL);
784 if (avctx->codec->pix_fmts) {
785 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
786 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
788 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
789 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
790 ret = AVERROR(EINVAL);
794 if (avctx->codec->supported_samplerates) {
795 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
796 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
798 if (avctx->codec->supported_samplerates[i] == 0) {
799 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
800 ret = AVERROR(EINVAL);
804 if (avctx->codec->channel_layouts) {
805 if (!avctx->channel_layout) {
806 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
808 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
809 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
811 if (avctx->codec->channel_layouts[i] == 0) {
812 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
813 ret = AVERROR(EINVAL);
818 if (avctx->channel_layout && avctx->channels) {
819 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
820 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
821 ret = AVERROR(EINVAL);
824 } else if (avctx->channel_layout) {
825 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
828 if (!avctx->rc_initial_buffer_occupancy)
829 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
832 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
833 ret = avctx->codec->init(avctx);
839 if (av_codec_is_decoder(avctx->codec)) {
840 /* validate channel layout from the decoder */
841 if (avctx->channel_layout) {
842 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
843 if (!avctx->channels)
844 avctx->channels = channels;
845 else if (channels != avctx->channels) {
846 av_log(avctx, AV_LOG_WARNING,
847 "channel layout does not match number of channels\n");
848 avctx->channel_layout = 0;
851 if (avctx->channels && avctx->channels < 0 ||
852 avctx->channels > FF_SANE_NB_CHANNELS) {
853 ret = AVERROR(EINVAL);
858 entangled_thread_counter--;
860 /* Release any user-supplied mutex. */
862 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
865 av_dict_free(options);
872 av_freep(&avctx->priv_data);
873 av_freep(&avctx->internal);
878 int ff_alloc_packet(AVPacket *avpkt, int size)
880 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
881 return AVERROR(EINVAL);
884 void *destruct = avpkt->destruct;
886 if (avpkt->size < size)
887 return AVERROR(EINVAL);
889 av_init_packet(avpkt);
890 avpkt->destruct = destruct;
894 return av_new_packet(avpkt, size);
899 * Pad last frame with silence.
901 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
903 AVFrame *frame = NULL;
907 if (!(frame = avcodec_alloc_frame()))
908 return AVERROR(ENOMEM);
911 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
912 s->frame_size, s->sample_fmt, 0)) < 0)
915 if (!(buf = av_malloc(ret))) {
916 ret = AVERROR(ENOMEM);
920 frame->nb_samples = s->frame_size;
921 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
924 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
925 src->nb_samples, s->channels, s->sample_fmt)) < 0)
927 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
928 frame->nb_samples - src->nb_samples,
929 s->channels, s->sample_fmt)) < 0)
937 if (frame->extended_data != frame->data)
938 av_freep(&frame->extended_data);
944 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
946 const AVFrame *frame,
950 AVFrame *padded_frame = NULL;
952 int user_packet = !!avpkt->data;
956 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
957 av_free_packet(avpkt);
958 av_init_packet(avpkt);
962 /* ensure that extended_data is properly set */
963 if (frame && !frame->extended_data) {
964 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
965 avctx->channels > AV_NUM_DATA_POINTERS) {
966 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
967 "with more than %d channels, but extended_data is not set.\n",
968 AV_NUM_DATA_POINTERS);
969 return AVERROR(EINVAL);
971 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
974 tmp.extended_data = tmp.data;
978 /* check for valid frame size */
980 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
981 if (frame->nb_samples > avctx->frame_size)
982 return AVERROR(EINVAL);
983 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
984 if (frame->nb_samples < avctx->frame_size &&
985 !avctx->internal->last_audio_frame) {
986 ret = pad_last_frame(avctx, &padded_frame, frame);
990 frame = padded_frame;
991 avctx->internal->last_audio_frame = 1;
994 if (frame->nb_samples != avctx->frame_size) {
995 ret = AVERROR(EINVAL);
1001 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1003 if (*got_packet_ptr) {
1004 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1005 if (avpkt->pts == AV_NOPTS_VALUE)
1006 avpkt->pts = frame->pts;
1007 if (!avpkt->duration)
1008 avpkt->duration = ff_samples_to_time_base(avctx,
1011 avpkt->dts = avpkt->pts;
1016 if (!user_packet && avpkt->size) {
1017 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1019 avpkt->data = new_data;
1022 avctx->frame_number++;
1025 if (ret < 0 || !*got_packet_ptr) {
1026 av_free_packet(avpkt);
1027 av_init_packet(avpkt);
1031 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1032 * this needs to be moved to the encoders, but for now we can do it
1033 * here to simplify things */
1034 avpkt->flags |= AV_PKT_FLAG_KEY;
1038 av_freep(&padded_frame->data[0]);
1039 if (padded_frame->extended_data != padded_frame->data)
1040 av_freep(&padded_frame->extended_data);
1041 av_freep(&padded_frame);
1047 #if FF_API_OLD_ENCODE_AUDIO
1048 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1049 uint8_t *buf, int buf_size,
1050 const short *samples)
1053 AVFrame frame0 = { { 0 } };
1055 int ret, samples_size, got_packet;
1057 av_init_packet(&pkt);
1059 pkt.size = buf_size;
1063 avcodec_get_frame_defaults(frame);
1065 if (avctx->frame_size) {
1066 frame->nb_samples = avctx->frame_size;
1068 /* if frame_size is not set, the number of samples must be
1069 * calculated from the buffer size */
1071 if (!av_get_bits_per_sample(avctx->codec_id)) {
1072 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1073 "support this codec\n");
1074 return AVERROR(EINVAL);
1076 nb_samples = (int64_t)buf_size * 8 /
1077 (av_get_bits_per_sample(avctx->codec_id) *
1079 if (nb_samples >= INT_MAX)
1080 return AVERROR(EINVAL);
1081 frame->nb_samples = nb_samples;
1084 /* it is assumed that the samples buffer is large enough based on the
1085 * relevant parameters */
1086 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1088 avctx->sample_fmt, 1);
1089 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1091 (const uint8_t *)samples,
1095 /* fabricate frame pts from sample count.
1096 * this is needed because the avcodec_encode_audio() API does not have
1097 * a way for the user to provide pts */
1098 frame->pts = ff_samples_to_time_base(avctx,
1099 avctx->internal->sample_count);
1100 avctx->internal->sample_count += frame->nb_samples;
1106 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1107 if (!ret && got_packet && avctx->coded_frame) {
1108 avctx->coded_frame->pts = pkt.pts;
1109 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1111 /* free any side data since we cannot return it */
1112 if (pkt.side_data_elems > 0) {
1114 for (i = 0; i < pkt.side_data_elems; i++)
1115 av_free(pkt.side_data[i].data);
1116 av_freep(&pkt.side_data);
1117 pkt.side_data_elems = 0;
1120 if (frame && frame->extended_data != frame->data)
1121 av_free(frame->extended_data);
1123 return ret ? ret : pkt.size;
1128 #if FF_API_OLD_ENCODE_VIDEO
1129 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1130 const AVFrame *pict)
1133 int ret, got_packet = 0;
1135 if (buf_size < FF_MIN_BUFFER_SIZE) {
1136 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1140 av_init_packet(&pkt);
1142 pkt.size = buf_size;
1144 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1145 if (!ret && got_packet && avctx->coded_frame) {
1146 avctx->coded_frame->pts = pkt.pts;
1147 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1150 /* free any side data since we cannot return it */
1151 if (pkt.side_data_elems > 0) {
1153 for (i = 0; i < pkt.side_data_elems; i++)
1154 av_free(pkt.side_data[i].data);
1155 av_freep(&pkt.side_data);
1156 pkt.side_data_elems = 0;
1159 return ret ? ret : pkt.size;
1164 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1166 const AVFrame *frame,
1167 int *got_packet_ptr)
1170 int user_packet = !!avpkt->data;
1172 *got_packet_ptr = 0;
1174 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1175 av_free_packet(avpkt);
1176 av_init_packet(avpkt);
1181 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1182 return AVERROR(EINVAL);
1184 av_assert0(avctx->codec->encode2);
1186 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1188 if (!*got_packet_ptr)
1190 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1191 avpkt->pts = avpkt->dts = frame->pts;
1193 if (!user_packet && avpkt->size) {
1194 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1196 avpkt->data = new_data;
1199 avctx->frame_number++;
1202 if (ret < 0 || !*got_packet_ptr)
1203 av_free_packet(avpkt);
1209 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1210 const AVSubtitle *sub)
1213 if (sub->start_display_time) {
1214 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1217 if (sub->num_rects == 0 || !sub->rects)
1219 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1220 avctx->frame_number++;
1224 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1227 const uint8_t *data;
1230 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1233 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1234 if (!data || size < 4)
1236 flags = bytestream_get_le32(&data);
1238 if (size < 4) /* Required for any of the changes */
1240 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1241 avctx->channels = bytestream_get_le32(&data);
1244 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1247 avctx->channel_layout = bytestream_get_le64(&data);
1252 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1253 avctx->sample_rate = bytestream_get_le32(&data);
1256 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1259 avctx->width = bytestream_get_le32(&data);
1260 avctx->height = bytestream_get_le32(&data);
1261 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1266 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1267 int *got_picture_ptr,
1272 *got_picture_ptr = 0;
1273 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1277 apply_param_change(avctx, avpkt);
1279 avcodec_get_frame_defaults(picture);
1281 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1282 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1283 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1286 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1288 picture->pkt_dts = avpkt->dts;
1289 /* get_buffer is supposed to set frame parameters */
1290 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1291 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1292 picture->width = avctx->width;
1293 picture->height = avctx->height;
1294 picture->format = avctx->pix_fmt;
1298 emms_c(); //needed to avoid an emms_c() call before every return;
1300 if (*got_picture_ptr)
1301 avctx->frame_number++;
1305 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1306 * make sure it's set correctly */
1307 picture->extended_data = picture->data;
1312 #if FF_API_OLD_DECODE_AUDIO
1313 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1314 int *frame_size_ptr,
1317 AVFrame frame = { { 0 } };
1318 int ret, got_frame = 0;
1320 if (avctx->get_buffer != avcodec_default_get_buffer) {
1321 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1322 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1323 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1324 "avcodec_decode_audio4()\n");
1325 avctx->get_buffer = avcodec_default_get_buffer;
1328 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1330 if (ret >= 0 && got_frame) {
1332 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1333 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1335 avctx->sample_fmt, 1);
1336 if (*frame_size_ptr < data_size) {
1337 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1338 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1339 return AVERROR(EINVAL);
1342 memcpy(samples, frame.extended_data[0], plane_size);
1344 if (planar && avctx->channels > 1) {
1345 uint8_t *out = ((uint8_t *)samples) + plane_size;
1346 for (ch = 1; ch < avctx->channels; ch++) {
1347 memcpy(out, frame.extended_data[ch], plane_size);
1351 *frame_size_ptr = data_size;
1353 *frame_size_ptr = 0;
1360 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1365 int planar, channels;
1372 if (!avpkt->data && avpkt->size) {
1373 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1374 return AVERROR(EINVAL);
1377 apply_param_change(avctx, avpkt);
1379 avcodec_get_frame_defaults(frame);
1381 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1382 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1383 if (ret >= 0 && *got_frame_ptr) {
1384 avctx->frame_number++;
1385 frame->pkt_dts = avpkt->dts;
1386 if (frame->format == AV_SAMPLE_FMT_NONE)
1387 frame->format = avctx->sample_fmt;
1391 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1392 * make sure it's set correctly; assume decoders that actually use
1393 * extended_data are doing it correctly */
1394 planar = av_sample_fmt_is_planar(frame->format);
1395 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1396 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1397 frame->extended_data = frame->data;
1402 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1410 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1412 avctx->frame_number++;
1416 void avsubtitle_free(AVSubtitle *sub)
1420 for (i = 0; i < sub->num_rects; i++) {
1421 av_freep(&sub->rects[i]->pict.data[0]);
1422 av_freep(&sub->rects[i]->pict.data[1]);
1423 av_freep(&sub->rects[i]->pict.data[2]);
1424 av_freep(&sub->rects[i]->pict.data[3]);
1425 av_freep(&sub->rects[i]->text);
1426 av_freep(&sub->rects[i]->ass);
1427 av_freep(&sub->rects[i]);
1430 av_freep(&sub->rects);
1432 memset(sub, 0, sizeof(AVSubtitle));
1435 av_cold int avcodec_close(AVCodecContext *avctx)
1437 /* If there is a user-supplied mutex locking routine, call it. */
1438 if (ff_lockmgr_cb) {
1439 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1443 entangled_thread_counter++;
1444 if (entangled_thread_counter != 1) {
1445 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1446 entangled_thread_counter--;
1450 if (avcodec_is_open(avctx)) {
1451 if (HAVE_THREADS && avctx->thread_opaque)
1452 ff_thread_free(avctx);
1453 if (avctx->codec && avctx->codec->close)
1454 avctx->codec->close(avctx);
1455 avcodec_default_free_buffers(avctx);
1456 avctx->coded_frame = NULL;
1457 av_freep(&avctx->internal);
1460 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1461 av_opt_free(avctx->priv_data);
1463 av_freep(&avctx->priv_data);
1464 if (av_codec_is_encoder(avctx->codec))
1465 av_freep(&avctx->extradata);
1466 avctx->codec = NULL;
1467 avctx->active_thread_type = 0;
1468 entangled_thread_counter--;
1470 /* Release any user-supplied mutex. */
1471 if (ff_lockmgr_cb) {
1472 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1477 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1479 AVCodec *p, *experimental = NULL;
1482 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1484 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1491 return experimental;
1494 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1496 return find_encdec(id, 1);
1499 AVCodec *avcodec_find_encoder_by_name(const char *name)
1506 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1513 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1515 return find_encdec(id, 0);
1518 AVCodec *avcodec_find_decoder_by_name(const char *name)
1525 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1532 static int get_bit_rate(AVCodecContext *ctx)
1535 int bits_per_sample;
1537 switch (ctx->codec_type) {
1538 case AVMEDIA_TYPE_VIDEO:
1539 case AVMEDIA_TYPE_DATA:
1540 case AVMEDIA_TYPE_SUBTITLE:
1541 case AVMEDIA_TYPE_ATTACHMENT:
1542 bit_rate = ctx->bit_rate;
1544 case AVMEDIA_TYPE_AUDIO:
1545 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1546 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1555 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1557 int i, len, ret = 0;
1559 for (i = 0; i < 4; i++) {
1560 len = snprintf(buf, buf_size,
1561 isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1563 buf_size = buf_size > len ? buf_size - len : 0;
1570 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1572 const char *codec_name;
1573 const char *profile = NULL;
1577 AVRational display_aspect_ratio;
1582 p = avcodec_find_encoder(enc->codec_id);
1584 p = avcodec_find_decoder(enc->codec_id);
1587 codec_name = p->name;
1588 profile = av_get_profile_name(p, enc->profile);
1589 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1590 /* fake mpeg2 transport stream codec (currently not
1592 codec_name = "mpeg2ts";
1593 } else if (enc->codec_name[0] != '\0') {
1594 codec_name = enc->codec_name;
1596 /* output avi tags */
1598 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1599 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1603 switch (enc->codec_type) {
1604 case AVMEDIA_TYPE_VIDEO:
1605 snprintf(buf, buf_size,
1607 codec_name, enc->mb_decision ? " (hq)" : "");
1609 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1611 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
1612 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1614 av_get_pix_fmt_name(enc->pix_fmt));
1617 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1619 enc->width, enc->height);
1620 if (enc->sample_aspect_ratio.num) {
1621 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1622 enc->width * enc->sample_aspect_ratio.num,
1623 enc->height * enc->sample_aspect_ratio.den,
1625 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1626 " [PAR %d:%d DAR %d:%d]",
1627 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1628 display_aspect_ratio.num, display_aspect_ratio.den);
1630 if (av_log_get_level() >= AV_LOG_DEBUG) {
1631 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1632 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1634 enc->time_base.num / g, enc->time_base.den / g);
1638 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1639 ", q=%d-%d", enc->qmin, enc->qmax);
1642 case AVMEDIA_TYPE_AUDIO:
1643 snprintf(buf, buf_size,
1647 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1649 if (enc->sample_rate) {
1650 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1651 ", %d Hz", enc->sample_rate);
1653 av_strlcat(buf, ", ", buf_size);
1654 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1655 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1656 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1657 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1660 case AVMEDIA_TYPE_DATA:
1661 snprintf(buf, buf_size, "Data: %s", codec_name);
1663 case AVMEDIA_TYPE_SUBTITLE:
1664 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1666 case AVMEDIA_TYPE_ATTACHMENT:
1667 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1670 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1674 if (enc->flags & CODEC_FLAG_PASS1)
1675 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1677 if (enc->flags & CODEC_FLAG_PASS2)
1678 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1681 bitrate = get_bit_rate(enc);
1683 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1684 ", %d kb/s", bitrate / 1000);
1688 const char *av_get_profile_name(const AVCodec *codec, int profile)
1691 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1694 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1695 if (p->profile == profile)
1701 unsigned avcodec_version(void)
1703 return LIBAVCODEC_VERSION_INT;
1706 const char *avcodec_configuration(void)
1708 return LIBAV_CONFIGURATION;
1711 const char *avcodec_license(void)
1713 #define LICENSE_PREFIX "libavcodec license: "
1714 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1717 void avcodec_flush_buffers(AVCodecContext *avctx)
1719 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1720 ff_thread_flush(avctx);
1721 else if (avctx->codec->flush)
1722 avctx->codec->flush(avctx);
1725 static void video_free_buffers(AVCodecContext *s)
1727 AVCodecInternal *avci = s->internal;
1733 if (avci->buffer_count)
1734 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1735 avci->buffer_count);
1736 for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
1737 InternalBuffer *buf = &avci->buffer[i];
1738 for (j = 0; j < 4; j++) {
1739 av_freep(&buf->base[j]);
1740 buf->data[j] = NULL;
1743 av_freep(&avci->buffer);
1745 avci->buffer_count = 0;
1748 static void audio_free_buffers(AVCodecContext *avctx)
1750 AVCodecInternal *avci = avctx->internal;
1751 av_freep(&avci->audio_data);
1754 void avcodec_default_free_buffers(AVCodecContext *avctx)
1756 switch (avctx->codec_type) {
1757 case AVMEDIA_TYPE_VIDEO:
1758 video_free_buffers(avctx);
1760 case AVMEDIA_TYPE_AUDIO:
1761 audio_free_buffers(avctx);
1768 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1771 case AV_CODEC_ID_ADPCM_CT:
1772 case AV_CODEC_ID_ADPCM_IMA_APC:
1773 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1774 case AV_CODEC_ID_ADPCM_IMA_WS:
1775 case AV_CODEC_ID_ADPCM_G722:
1776 case AV_CODEC_ID_ADPCM_YAMAHA:
1778 case AV_CODEC_ID_PCM_ALAW:
1779 case AV_CODEC_ID_PCM_MULAW:
1780 case AV_CODEC_ID_PCM_S8:
1781 case AV_CODEC_ID_PCM_U8:
1782 case AV_CODEC_ID_PCM_ZORK:
1784 case AV_CODEC_ID_PCM_S16BE:
1785 case AV_CODEC_ID_PCM_S16LE:
1786 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1787 case AV_CODEC_ID_PCM_U16BE:
1788 case AV_CODEC_ID_PCM_U16LE:
1790 case AV_CODEC_ID_PCM_S24DAUD:
1791 case AV_CODEC_ID_PCM_S24BE:
1792 case AV_CODEC_ID_PCM_S24LE:
1793 case AV_CODEC_ID_PCM_U24BE:
1794 case AV_CODEC_ID_PCM_U24LE:
1796 case AV_CODEC_ID_PCM_S32BE:
1797 case AV_CODEC_ID_PCM_S32LE:
1798 case AV_CODEC_ID_PCM_U32BE:
1799 case AV_CODEC_ID_PCM_U32LE:
1800 case AV_CODEC_ID_PCM_F32BE:
1801 case AV_CODEC_ID_PCM_F32LE:
1803 case AV_CODEC_ID_PCM_F64BE:
1804 case AV_CODEC_ID_PCM_F64LE:
1811 int av_get_bits_per_sample(enum AVCodecID codec_id)
1814 case AV_CODEC_ID_ADPCM_SBPRO_2:
1816 case AV_CODEC_ID_ADPCM_SBPRO_3:
1818 case AV_CODEC_ID_ADPCM_SBPRO_4:
1819 case AV_CODEC_ID_ADPCM_IMA_WAV:
1820 case AV_CODEC_ID_ADPCM_IMA_QT:
1821 case AV_CODEC_ID_ADPCM_SWF:
1822 case AV_CODEC_ID_ADPCM_MS:
1825 return av_get_exact_bits_per_sample(codec_id);
1829 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1831 int id, sr, ch, ba, tag, bps;
1833 id = avctx->codec_id;
1834 sr = avctx->sample_rate;
1835 ch = avctx->channels;
1836 ba = avctx->block_align;
1837 tag = avctx->codec_tag;
1838 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1840 /* codecs with an exact constant bits per sample */
1841 if (bps > 0 && ch > 0 && frame_bytes > 0)
1842 return (frame_bytes * 8) / (bps * ch);
1843 bps = avctx->bits_per_coded_sample;
1845 /* codecs with a fixed packet duration */
1847 case AV_CODEC_ID_ADPCM_ADX: return 32;
1848 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1849 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1850 case AV_CODEC_ID_AMR_NB:
1851 case AV_CODEC_ID_GSM:
1852 case AV_CODEC_ID_QCELP:
1853 case AV_CODEC_ID_RA_144:
1854 case AV_CODEC_ID_RA_288: return 160;
1855 case AV_CODEC_ID_IMC: return 256;
1856 case AV_CODEC_ID_AMR_WB:
1857 case AV_CODEC_ID_GSM_MS: return 320;
1858 case AV_CODEC_ID_MP1: return 384;
1859 case AV_CODEC_ID_ATRAC1: return 512;
1860 case AV_CODEC_ID_ATRAC3: return 1024;
1861 case AV_CODEC_ID_MP2:
1862 case AV_CODEC_ID_MUSEPACK7: return 1152;
1863 case AV_CODEC_ID_AC3: return 1536;
1867 /* calc from sample rate */
1868 if (id == AV_CODEC_ID_TTA)
1869 return 256 * sr / 245;
1872 /* calc from sample rate and channels */
1873 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1874 return (480 << (sr / 22050)) / ch;
1879 /* calc from block_align */
1880 if (id == AV_CODEC_ID_SIPR) {
1882 case 20: return 160;
1883 case 19: return 144;
1884 case 29: return 288;
1885 case 37: return 480;
1887 } else if (id == AV_CODEC_ID_ILBC) {
1889 case 38: return 160;
1890 case 50: return 240;
1895 if (frame_bytes > 0) {
1896 /* calc from frame_bytes only */
1897 if (id == AV_CODEC_ID_TRUESPEECH)
1898 return 240 * (frame_bytes / 32);
1899 if (id == AV_CODEC_ID_NELLYMOSER)
1900 return 256 * (frame_bytes / 64);
1903 /* calc from frame_bytes and bits_per_coded_sample */
1904 if (id == AV_CODEC_ID_ADPCM_G726)
1905 return frame_bytes * 8 / bps;
1909 /* calc from frame_bytes and channels */
1911 case AV_CODEC_ID_ADPCM_4XM:
1912 case AV_CODEC_ID_ADPCM_IMA_ISS:
1913 return (frame_bytes - 4 * ch) * 2 / ch;
1914 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1915 return (frame_bytes - 4) * 2 / ch;
1916 case AV_CODEC_ID_ADPCM_IMA_AMV:
1917 return (frame_bytes - 8) * 2 / ch;
1918 case AV_CODEC_ID_ADPCM_XA:
1919 return (frame_bytes / 128) * 224 / ch;
1920 case AV_CODEC_ID_INTERPLAY_DPCM:
1921 return (frame_bytes - 6 - ch) / ch;
1922 case AV_CODEC_ID_ROQ_DPCM:
1923 return (frame_bytes - 8) / ch;
1924 case AV_CODEC_ID_XAN_DPCM:
1925 return (frame_bytes - 2 * ch) / ch;
1926 case AV_CODEC_ID_MACE3:
1927 return 3 * frame_bytes / ch;
1928 case AV_CODEC_ID_MACE6:
1929 return 6 * frame_bytes / ch;
1930 case AV_CODEC_ID_PCM_LXF:
1931 return 2 * (frame_bytes / (5 * ch));
1935 /* calc from frame_bytes, channels, and codec_tag */
1936 if (id == AV_CODEC_ID_SOL_DPCM) {
1938 return frame_bytes / ch;
1940 return frame_bytes * 2 / ch;
1945 /* calc from frame_bytes, channels, and block_align */
1946 int blocks = frame_bytes / ba;
1947 switch (avctx->codec_id) {
1948 case AV_CODEC_ID_ADPCM_IMA_WAV:
1949 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1950 case AV_CODEC_ID_ADPCM_IMA_DK3:
1951 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1952 case AV_CODEC_ID_ADPCM_IMA_DK4:
1953 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1954 case AV_CODEC_ID_ADPCM_MS:
1955 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1960 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1961 switch (avctx->codec_id) {
1962 case AV_CODEC_ID_PCM_DVD:
1963 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1964 case AV_CODEC_ID_PCM_BLURAY:
1965 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1966 case AV_CODEC_ID_S302M:
1967 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1977 int ff_thread_init(AVCodecContext *s)
1984 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1998 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2001 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2005 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2007 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2008 "version to the newest one from Git. If the problem still "
2009 "occurs, it means that your file has a feature which has not "
2010 "been implemented.\n", feature);
2012 av_log_ask_for_sample(avc, NULL);
2015 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2017 va_list argument_list;
2019 va_start(argument_list, msg);
2022 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2023 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2024 "of this file to ftp://upload.libav.org/incoming/ "
2025 "and contact the libav-devel mailing list.\n");
2027 va_end(argument_list);
2030 static AVHWAccel *first_hwaccel = NULL;
2032 void av_register_hwaccel(AVHWAccel *hwaccel)
2034 AVHWAccel **p = &first_hwaccel;
2038 hwaccel->next = NULL;
2041 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2043 return hwaccel ? hwaccel->next : first_hwaccel;
2046 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
2048 AVHWAccel *hwaccel = NULL;
2050 while ((hwaccel = av_hwaccel_next(hwaccel)))
2051 if (hwaccel->id == codec_id
2052 && hwaccel->pix_fmt == pix_fmt)
2057 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2059 if (ff_lockmgr_cb) {
2060 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2062 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2068 if (ff_lockmgr_cb) {
2069 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2071 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2077 int avpriv_lock_avformat(void)
2079 if (ff_lockmgr_cb) {
2080 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2086 int avpriv_unlock_avformat(void)
2088 if (ff_lockmgr_cb) {
2089 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2095 unsigned int avpriv_toupper4(unsigned int x)
2097 return toupper(x & 0xFF)
2098 + (toupper((x >> 8) & 0xFF) << 8)
2099 + (toupper((x >> 16) & 0xFF) << 16)
2100 + (toupper((x >> 24) & 0xFF) << 24);
2105 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2108 return ff_get_buffer(avctx, f);
2111 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2113 f->owner->release_buffer(f->owner, f);
2116 void ff_thread_finish_setup(AVCodecContext *avctx)
2120 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2124 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2130 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2132 if (codec_id <= AV_CODEC_ID_NONE)
2133 return AVMEDIA_TYPE_UNKNOWN;
2134 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2135 return AVMEDIA_TYPE_VIDEO;
2136 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2137 return AVMEDIA_TYPE_AUDIO;
2138 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2139 return AVMEDIA_TYPE_SUBTITLE;
2141 return AVMEDIA_TYPE_UNKNOWN;
2144 int avcodec_is_open(AVCodecContext *s)
2146 return !!s->internal;