3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/audioconvert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avassert.h"
40 #include "libavutil/opt.h"
41 #include "imgconvert.h"
43 #include "audioconvert.h"
45 #include "bytestream.h"
51 static int volatile entangled_thread_counter=0;
52 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
53 static void *codec_mutex;
54 static void *avformat_mutex;
56 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
61 min_size= FFMAX(17*min_size/16 + 32, min_size);
63 ptr= av_realloc(ptr, min_size);
64 if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
72 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
77 min_size= FFMAX(17*min_size/16 + 32, min_size);
79 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
80 if (!*p) min_size = 0;
85 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
87 ff_fast_malloc(ptr, size, min_size, 0);
90 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
93 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
98 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
99 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
102 /* encoder management */
103 static AVCodec *first_avcodec = NULL;
105 AVCodec *av_codec_next(AVCodec *c){
106 if(c) return c->next;
107 else return first_avcodec;
110 static void avcodec_init(void)
112 static int initialized = 0;
114 if (initialized != 0)
118 ff_dsputil_static_init();
121 int av_codec_is_encoder(AVCodec *codec)
123 return codec && (codec->encode || codec->encode2);
126 int av_codec_is_decoder(AVCodec *codec)
128 return codec && codec->decode;
131 void avcodec_register(AVCodec *codec)
136 while (*p != NULL) p = &(*p)->next;
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){
150 s->coded_width = width;
151 s->coded_height= height;
152 s->width = -((-width )>>s->lowres);
153 s->height= -((-height)>>s->lowres);
156 #define INTERNAL_BUFFER_SIZE (32+1)
158 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
159 int linesize_align[AV_NUM_DATA_POINTERS])
166 case PIX_FMT_YUV420P:
167 case PIX_FMT_YUYV422:
168 case PIX_FMT_UYVY422:
169 case PIX_FMT_YUV422P:
170 case PIX_FMT_YUV440P:
171 case PIX_FMT_YUV444P:
174 case PIX_FMT_GRAY16BE:
175 case PIX_FMT_GRAY16LE:
176 case PIX_FMT_YUVJ420P:
177 case PIX_FMT_YUVJ422P:
178 case PIX_FMT_YUVJ440P:
179 case PIX_FMT_YUVJ444P:
180 case PIX_FMT_YUVA420P:
181 case PIX_FMT_YUVA444P:
182 case PIX_FMT_YUV420P9LE:
183 case PIX_FMT_YUV420P9BE:
184 case PIX_FMT_YUV420P10LE:
185 case PIX_FMT_YUV420P10BE:
186 case PIX_FMT_YUV422P9LE:
187 case PIX_FMT_YUV422P9BE:
188 case PIX_FMT_YUV422P10LE:
189 case PIX_FMT_YUV422P10BE:
190 case PIX_FMT_YUV444P9LE:
191 case PIX_FMT_YUV444P9BE:
192 case PIX_FMT_YUV444P10LE:
193 case PIX_FMT_YUV444P10BE:
194 case PIX_FMT_GBRP9LE:
195 case PIX_FMT_GBRP9BE:
196 case PIX_FMT_GBRP10LE:
197 case PIX_FMT_GBRP10BE:
198 w_align = 16; //FIXME assume 16 pixel per macroblock
199 h_align = 16 * 2; // interlaced needs 2 macroblocks height
201 case PIX_FMT_YUV411P:
202 case PIX_FMT_UYYVYY411:
206 case PIX_FMT_YUV410P:
207 if(s->codec_id == CODEC_ID_SVQ1){
212 if(s->codec_id == CODEC_ID_RPZA){
219 if(s->codec_id == CODEC_ID_SMC){
225 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
236 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
237 w_align= FFMAX(w_align, 8);
240 *width = FFALIGN(*width , w_align);
241 *height= FFALIGN(*height, h_align);
242 if(s->codec_id == CODEC_ID_H264 || s->lowres)
243 *height+=2; // some of the optimized chroma MC reads one line too much
244 // which is also done in mpeg decoders with lowres > 0
246 for (i = 0; i < 4; i++)
247 linesize_align[i] = STRIDE_ALIGN;
250 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
251 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
252 int linesize_align[AV_NUM_DATA_POINTERS];
254 avcodec_align_dimensions2(s, width, height, linesize_align);
255 align = FFMAX(linesize_align[0], linesize_align[3]);
256 linesize_align[1] <<= chroma_shift;
257 linesize_align[2] <<= chroma_shift;
258 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
259 *width=FFALIGN(*width, align);
262 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
265 pic->pkt_pts = s->pkt->pts;
266 pic->pkt_pos = s->pkt->pos;
268 pic->pkt_pts = AV_NOPTS_VALUE;
271 pic->reordered_opaque= s->reordered_opaque;
272 pic->sample_aspect_ratio = s->sample_aspect_ratio;
273 pic->width = s->width;
274 pic->height = s->height;
275 pic->format = s->pix_fmt;
278 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
279 enum AVSampleFormat sample_fmt, const uint8_t *buf,
280 int buf_size, int align)
282 int ch, planar, needed_size, ret = 0;
284 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
285 frame->nb_samples, sample_fmt,
287 if (buf_size < needed_size)
288 return AVERROR(EINVAL);
290 planar = av_sample_fmt_is_planar(sample_fmt);
291 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
292 if (!(frame->extended_data = av_mallocz(nb_channels *
293 sizeof(*frame->extended_data))))
294 return AVERROR(ENOMEM);
296 frame->extended_data = frame->data;
299 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
300 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
301 sample_fmt, align)) < 0) {
302 if (frame->extended_data != frame->data)
303 av_freep(&frame->extended_data);
306 if (frame->extended_data != frame->data) {
307 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
308 frame->data[ch] = frame->extended_data[ch];
314 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
316 AVCodecInternal *avci = avctx->internal;
320 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
321 frame->nb_samples, avctx->sample_fmt,
324 return AVERROR(EINVAL);
326 /* allocate InternalBuffer if needed */
328 avci->buffer = av_mallocz(sizeof(InternalBuffer));
330 return AVERROR(ENOMEM);
334 /* if there is a previously-used internal buffer, check its size and
335 channel count to see if we can reuse it */
336 if (buf->extended_data) {
337 /* if current buffer is too small, free it */
338 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
339 av_free(buf->extended_data[0]);
340 if (buf->extended_data != buf->data)
341 av_freep(&buf->extended_data);
342 buf->extended_data = NULL;
345 /* if number of channels has changed, reset and/or free extended data
346 pointers but leave data buffer in buf->data[0] for reuse */
347 if (buf->nb_channels != avctx->channels) {
348 if (buf->extended_data != buf->data)
349 av_free(buf->extended_data);
350 buf->extended_data = NULL;
354 /* if there is no previous buffer or the previous buffer cannot be used
355 as-is, allocate a new buffer and/or rearrange the channel pointers */
356 if (!buf->extended_data) {
358 if (!(buf->data[0] = av_mallocz(buf_size)))
359 return AVERROR(ENOMEM);
360 buf->audio_data_size = buf_size;
362 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
363 avctx->sample_fmt, buf->data[0],
364 buf->audio_data_size, 32)))
367 if (frame->extended_data == frame->data)
368 buf->extended_data = buf->data;
370 buf->extended_data = frame->extended_data;
371 memcpy(buf->data, frame->data, sizeof(frame->data));
372 buf->linesize[0] = frame->linesize[0];
373 buf->nb_channels = avctx->channels;
375 /* copy InternalBuffer info to the AVFrame */
376 frame->extended_data = buf->extended_data;
377 frame->linesize[0] = buf->linesize[0];
378 memcpy(frame->data, buf->data, sizeof(frame->data));
381 frame->type = FF_BUFFER_TYPE_INTERNAL;
384 frame->pkt_pts = avctx->pkt->pts;
385 frame->pkt_pos = avctx->pkt->pos;
387 frame->pkt_pts = AV_NOPTS_VALUE;
391 frame->reordered_opaque = avctx->reordered_opaque;
393 if (avctx->debug & FF_DEBUG_BUFFERS)
394 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
395 "internal audio buffer used\n", frame);
400 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
406 AVCodecInternal *avci = s->internal;
408 if(pic->data[0]!=NULL) {
409 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
412 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
413 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
417 if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
421 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
422 sizeof(InternalBuffer));
425 buf = &avci->buffer[avci->buffer_count];
427 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
428 if(s->active_thread_type&FF_THREAD_FRAME) {
429 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
433 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
434 av_freep(&buf->base[i]);
440 int h_chroma_shift, v_chroma_shift;
445 int stride_align[AV_NUM_DATA_POINTERS];
446 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
448 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
450 avcodec_align_dimensions2(s, &w, &h, stride_align);
452 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
458 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
459 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
460 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
461 // increase alignment of w for next try (rhs gives the lowest bit set in w)
466 unaligned |= picture.linesize[i] % stride_align[i];
470 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
474 for (i=0; i<3 && picture.data[i+1]; i++)
475 size[i] = picture.data[i+1] - picture.data[i];
476 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
478 memset(buf->base, 0, sizeof(buf->base));
479 memset(buf->data, 0, sizeof(buf->data));
481 for(i=0; i<4 && size[i]; i++){
482 const int h_shift= i==0 ? 0 : h_chroma_shift;
483 const int v_shift= i==0 ? 0 : v_chroma_shift;
485 buf->linesize[i]= picture.linesize[i];
487 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
488 if(buf->base[i]==NULL) return -1;
489 memset(buf->base[i], 128, size[i]);
491 // no edge if EDGE EMU or not planar YUV
492 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
493 buf->data[i] = buf->base[i];
495 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
497 for (; i < AV_NUM_DATA_POINTERS; i++) {
498 buf->base[i] = buf->data[i] = NULL;
499 buf->linesize[i] = 0;
501 if(size[1] && !size[2])
502 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
503 buf->width = s->width;
504 buf->height = s->height;
505 buf->pix_fmt= s->pix_fmt;
507 pic->type= FF_BUFFER_TYPE_INTERNAL;
509 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
510 pic->base[i]= buf->base[i];
511 pic->data[i]= buf->data[i];
512 pic->linesize[i]= buf->linesize[i];
514 pic->extended_data = pic->data;
515 avci->buffer_count++;
518 pic->pkt_pts = s->pkt->pts;
519 pic->pkt_pos = s->pkt->pos;
521 pic->pkt_pts = AV_NOPTS_VALUE;
524 pic->reordered_opaque= s->reordered_opaque;
525 pic->sample_aspect_ratio = s->sample_aspect_ratio;
526 pic->width = s->width;
527 pic->height = s->height;
528 pic->format = s->pix_fmt;
530 if(s->debug&FF_DEBUG_BUFFERS)
531 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
532 "buffers used\n", pic, avci->buffer_count);
537 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
539 switch (avctx->codec_type) {
540 case AVMEDIA_TYPE_VIDEO:
541 return video_get_buffer(avctx, frame);
542 case AVMEDIA_TYPE_AUDIO:
543 return audio_get_buffer(avctx, frame);
549 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
551 InternalBuffer *buf, *last;
552 AVCodecInternal *avci = s->internal;
554 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
556 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
557 assert(avci->buffer_count);
560 buf = NULL; /* avoids warning */
561 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
562 buf = &avci->buffer[i];
563 if (buf->data[0] == pic->data[0])
566 assert(i < avci->buffer_count);
567 avci->buffer_count--;
568 last = &avci->buffer[avci->buffer_count];
571 FFSWAP(InternalBuffer, *buf, *last);
574 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
576 // pic->base[i]=NULL;
578 //printf("R%X\n", pic->opaque);
580 if(s->debug&FF_DEBUG_BUFFERS)
581 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
582 "buffers used\n", pic, avci->buffer_count);
585 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
589 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
591 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
592 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
593 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
594 s->release_buffer(s, pic);
597 ff_init_buffer_info(s, pic);
599 /* If no picture return a new buffer */
600 if(pic->data[0] == NULL) {
601 /* We will copy from buffer, so must be readable */
602 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
603 return s->get_buffer(s, pic);
606 assert(s->pix_fmt == pic->format);
608 /* If internal buffer type return the same buffer */
609 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
614 * Not internal type and reget_buffer not overridden, emulate cr buffer
617 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
618 pic->data[i] = pic->base[i] = NULL;
620 /* Allocate new frame */
621 if (s->get_buffer(s, pic))
623 /* Copy image data from old buffer to new buffer */
624 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
626 s->release_buffer(s, &temp_pic); // Release old frame
630 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
633 for(i=0; i<count; i++){
634 int r= func(c, (char*)arg + i*size);
640 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
643 for(i=0; i<count; i++){
644 int r= func(c, arg, i, 0);
650 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
651 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
656 void avcodec_get_frame_defaults(AVFrame *pic){
657 memset(pic, 0, sizeof(AVFrame));
659 pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
662 pic->sample_aspect_ratio = (AVRational){0, 1};
663 pic->format = -1; /* unknown */
666 AVFrame *avcodec_alloc_frame(void){
667 AVFrame *pic= av_malloc(sizeof(AVFrame));
669 if(pic==NULL) return NULL;
671 avcodec_get_frame_defaults(pic);
676 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
678 memset(sub, 0, sizeof(*sub));
679 sub->pts = AV_NOPTS_VALUE;
682 static int get_bit_rate(AVCodecContext *ctx)
687 switch(ctx->codec_type) {
688 case AVMEDIA_TYPE_VIDEO:
689 case AVMEDIA_TYPE_DATA:
690 case AVMEDIA_TYPE_SUBTITLE:
691 case AVMEDIA_TYPE_ATTACHMENT:
692 bit_rate = ctx->bit_rate;
694 case AVMEDIA_TYPE_AUDIO:
695 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
696 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
705 #if FF_API_AVCODEC_OPEN
706 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
708 return avcodec_open2(avctx, codec, NULL);
712 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
715 AVDictionary *tmp = NULL;
717 if (avcodec_is_open(avctx))
720 if ((!codec && !avctx->codec)) {
721 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
722 return AVERROR(EINVAL);
724 if ((codec && avctx->codec && codec != avctx->codec)) {
725 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
726 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
727 return AVERROR(EINVAL);
730 codec = avctx->codec;
732 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
733 return AVERROR(EINVAL);
736 av_dict_copy(&tmp, *options, 0);
738 /* If there is a user-supplied mutex locking routine, call it. */
740 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
744 entangled_thread_counter++;
745 if(entangled_thread_counter != 1){
746 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
751 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
752 if (!avctx->internal) {
753 ret = AVERROR(ENOMEM);
757 if (codec->priv_data_size > 0) {
758 if(!avctx->priv_data){
759 avctx->priv_data = av_mallocz(codec->priv_data_size);
760 if (!avctx->priv_data) {
761 ret = AVERROR(ENOMEM);
764 if (codec->priv_class) {
765 *(const AVClass**)avctx->priv_data= codec->priv_class;
766 av_opt_set_defaults(avctx->priv_data);
769 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
772 avctx->priv_data = NULL;
774 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
777 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
778 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
779 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
784 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
785 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
786 if(avctx->coded_width && avctx->coded_height)
787 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
788 else if(avctx->width && avctx->height)
789 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
792 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
793 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
794 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
795 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
796 avcodec_set_dimensions(avctx, 0, 0);
799 /* if the decoder init function was already called previously,
800 free the already allocated subtitle_header before overwriting it */
801 if (av_codec_is_decoder(codec))
802 av_freep(&avctx->subtitle_header);
804 #define SANE_NB_CHANNELS 128U
805 if (avctx->channels > SANE_NB_CHANNELS) {
806 ret = AVERROR(EINVAL);
810 avctx->codec = codec;
811 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
812 avctx->codec_id == CODEC_ID_NONE) {
813 avctx->codec_type = codec->type;
814 avctx->codec_id = codec->id;
816 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
817 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
818 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
819 ret = AVERROR(EINVAL);
822 avctx->frame_number = 0;
824 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
825 (!avctx->time_base.num || !avctx->time_base.den)) {
826 avctx->time_base.num = 1;
827 avctx->time_base.den = avctx->sample_rate;
831 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
833 if (HAVE_THREADS && !avctx->thread_opaque) {
834 ret = ff_thread_init(avctx);
839 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
840 avctx->thread_count = 1;
842 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
843 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
844 avctx->codec->max_lowres);
845 ret = AVERROR(EINVAL);
848 if (av_codec_is_encoder(avctx->codec)) {
850 if (avctx->codec->sample_fmts) {
851 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
852 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
854 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
855 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
856 ret = AVERROR(EINVAL);
860 if (avctx->codec->pix_fmts) {
861 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
862 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
864 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
865 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
866 ret = AVERROR(EINVAL);
870 if (avctx->codec->supported_samplerates) {
871 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
872 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
874 if (avctx->codec->supported_samplerates[i] == 0) {
875 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
876 ret = AVERROR(EINVAL);
880 if (avctx->codec->channel_layouts) {
881 if (!avctx->channel_layout) {
882 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
884 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
885 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
887 if (avctx->codec->channel_layouts[i] == 0) {
888 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
889 ret = AVERROR(EINVAL);
894 if (avctx->channel_layout && avctx->channels) {
895 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
896 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
897 ret = AVERROR(EINVAL);
900 } else if (avctx->channel_layout) {
901 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
905 avctx->pts_correction_num_faulty_pts =
906 avctx->pts_correction_num_faulty_dts = 0;
907 avctx->pts_correction_last_pts =
908 avctx->pts_correction_last_dts = INT64_MIN;
910 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
911 ret = avctx->codec->init(avctx);
917 if (av_codec_is_decoder(avctx->codec) && !avctx->bit_rate)
918 avctx->bit_rate = get_bit_rate(avctx);
922 entangled_thread_counter--;
924 /* Release any user-supplied mutex. */
926 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
929 av_dict_free(options);
936 av_freep(&avctx->priv_data);
937 av_freep(&avctx->internal);
942 int ff_alloc_packet(AVPacket *avpkt, int size)
944 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
945 return AVERROR(EINVAL);
948 void *destruct = avpkt->destruct;
950 if (avpkt->size < size)
951 return AVERROR(EINVAL);
953 av_init_packet(avpkt);
954 avpkt->destruct = destruct;
958 return av_new_packet(avpkt, size);
962 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
964 const AVFrame *frame,
968 int user_packet = !!avpkt->data;
973 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
974 av_free_packet(avpkt);
975 av_init_packet(avpkt);
980 /* check for valid frame size */
982 nb_samples = frame->nb_samples;
983 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
984 if (nb_samples > avctx->frame_size)
985 return AVERROR(EINVAL);
986 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
987 if (nb_samples != avctx->frame_size)
988 return AVERROR(EINVAL);
991 nb_samples = avctx->frame_size;
994 if (avctx->codec->encode2) {
995 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
996 if (!ret && *got_packet_ptr) {
997 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
998 if (avpkt->pts == AV_NOPTS_VALUE)
999 avpkt->pts = frame->pts;
1000 if (!avpkt->duration)
1001 avpkt->duration = ff_samples_to_time_base(avctx,
1004 avpkt->dts = avpkt->pts;
1009 /* for compatibility with encoders not supporting encode2(), we need to
1010 allocate a packet buffer if the user has not provided one or check
1011 the size otherwise */
1013 int buf_size = avpkt->size;
1015 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
1016 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
1018 return AVERROR(EINVAL);
1019 buf_size = nb_samples * avctx->channels *
1020 av_get_bits_per_sample(avctx->codec_id) / 8;
1022 /* this is a guess as to the required size.
1023 if an encoder needs more than this, it should probably
1024 implement encode2() */
1025 buf_size = 2 * avctx->frame_size * avctx->channels *
1026 av_get_bytes_per_sample(avctx->sample_fmt);
1027 buf_size += 2*FF_MIN_BUFFER_SIZE;
1030 if ((ret = ff_alloc_packet(avpkt, buf_size)))
1033 /* Encoders using AVCodec.encode() that support
1034 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
1035 the smaller size when encoding the last frame.
1036 This code can be removed once all encoders supporting
1037 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
1038 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
1039 nb_samples < avctx->frame_size) {
1040 fs_tmp = avctx->frame_size;
1041 avctx->frame_size = nb_samples;
1044 /* encode the frame */
1045 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
1046 frame ? frame->data[0] : NULL);
1049 /* no output. if the packet data was allocated by libavcodec,
1052 av_freep(&avpkt->data);
1054 if (avctx->coded_frame)
1055 avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
1056 /* Set duration for final small packet. This can be removed
1057 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1060 avpkt->duration = ff_samples_to_time_base(avctx,
1065 *got_packet_ptr = (ret > 0);
1070 avctx->frame_size = fs_tmp;
1073 avctx->frame_number++;
1075 if (ret < 0 || !*got_packet_ptr)
1076 av_free_packet(avpkt);
1078 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1079 this needs to be moved to the encoders, but for now we can do it
1080 here to simplify things */
1081 avpkt->flags |= AV_PKT_FLAG_KEY;
1086 #if FF_API_OLD_DECODE_AUDIO
1087 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1088 uint8_t *buf, int buf_size,
1089 const short *samples)
1094 int ret, samples_size, got_packet;
1096 av_init_packet(&pkt);
1098 pkt.size = buf_size;
1102 avcodec_get_frame_defaults(frame);
1104 if (avctx->frame_size) {
1105 frame->nb_samples = avctx->frame_size;
1107 /* if frame_size is not set, the number of samples must be
1108 calculated from the buffer size */
1110 if (!av_get_bits_per_sample(avctx->codec_id)) {
1111 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1112 "support this codec\n");
1113 return AVERROR(EINVAL);
1115 nb_samples = (int64_t)buf_size * 8 /
1116 (av_get_bits_per_sample(avctx->codec_id) *
1118 if (nb_samples >= INT_MAX)
1119 return AVERROR(EINVAL);
1120 frame->nb_samples = nb_samples;
1123 /* it is assumed that the samples buffer is large enough based on the
1124 relevant parameters */
1125 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1127 avctx->sample_fmt, 1);
1128 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1130 (const uint8_t *)samples, samples_size, 1)))
1133 /* fabricate frame pts from sample count.
1134 this is needed because the avcodec_encode_audio() API does not have
1135 a way for the user to provide pts */
1136 if(avctx->sample_rate && avctx->time_base.num)
1137 frame->pts = ff_samples_to_time_base(avctx,
1138 avctx->internal->sample_count);
1140 frame->pts = AV_NOPTS_VALUE;
1141 avctx->internal->sample_count += frame->nb_samples;
1147 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1148 if (!ret && got_packet && avctx->coded_frame) {
1149 avctx->coded_frame->pts = pkt.pts;
1150 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1152 /* free any side data since we cannot return it */
1153 ff_packet_free_side_data(&pkt);
1155 if (frame && frame->extended_data != frame->data)
1156 av_freep(&frame->extended_data);
1158 return ret ? ret : pkt.size;
1162 #if FF_API_OLD_ENCODE_VIDEO
1163 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1164 const AVFrame *pict)
1167 int ret, got_packet = 0;
1169 if(buf_size < FF_MIN_BUFFER_SIZE){
1170 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1174 av_init_packet(&pkt);
1176 pkt.size = buf_size;
1178 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1179 if (!ret && got_packet && avctx->coded_frame) {
1180 avctx->coded_frame->pts = pkt.pts;
1181 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1184 /* free any side data since we cannot return it */
1185 if (pkt.side_data_elems > 0) {
1187 for (i = 0; i < pkt.side_data_elems; i++)
1188 av_free(pkt.side_data[i].data);
1189 av_freep(&pkt.side_data);
1190 pkt.side_data_elems = 0;
1193 return ret ? ret : pkt.size;
1197 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1199 const AVFrame *frame,
1200 int *got_packet_ptr)
1203 int user_packet = !!avpkt->data;
1205 *got_packet_ptr = 0;
1207 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1208 av_free_packet(avpkt);
1209 av_init_packet(avpkt);
1214 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1215 return AVERROR(EINVAL);
1217 av_assert0(avctx->codec->encode2);
1219 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1221 if (!*got_packet_ptr)
1223 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1224 avpkt->pts = avpkt->dts = frame->pts;
1226 if (!user_packet && avpkt->data &&
1227 avpkt->destruct == av_destruct_packet) {
1228 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1230 avpkt->data = new_data;
1233 avctx->frame_number++;
1236 if (ret < 0 || !*got_packet_ptr)
1237 av_free_packet(avpkt);
1243 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1244 const AVSubtitle *sub)
1247 if(sub->start_display_time) {
1248 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1252 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
1253 avctx->frame_number++;
1258 * Attempt to guess proper monotonic timestamps for decoded video frames
1259 * which might have incorrect times. Input timestamps may wrap around, in
1260 * which case the output will as well.
1262 * @param pts the pts field of the decoded AVPacket, as passed through
1264 * @param dts the dts field of the decoded AVPacket
1265 * @return one of the input values, may be AV_NOPTS_VALUE
1267 static int64_t guess_correct_pts(AVCodecContext *ctx,
1268 int64_t reordered_pts, int64_t dts)
1270 int64_t pts = AV_NOPTS_VALUE;
1272 if (dts != AV_NOPTS_VALUE) {
1273 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1274 ctx->pts_correction_last_dts = dts;
1276 if (reordered_pts != AV_NOPTS_VALUE) {
1277 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1278 ctx->pts_correction_last_pts = reordered_pts;
1280 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1281 && reordered_pts != AV_NOPTS_VALUE)
1282 pts = reordered_pts;
1289 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1292 const uint8_t *data;
1295 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1298 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1299 if (!data || size < 4)
1301 flags = bytestream_get_le32(&data);
1303 if (size < 4) /* Required for any of the changes */
1305 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1306 avctx->channels = bytestream_get_le32(&data);
1309 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1312 avctx->channel_layout = bytestream_get_le64(&data);
1317 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1318 avctx->sample_rate = bytestream_get_le32(&data);
1321 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1324 avctx->width = bytestream_get_le32(&data);
1325 avctx->height = bytestream_get_le32(&data);
1326 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1331 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1332 int *got_picture_ptr,
1333 const AVPacket *avpkt)
1336 // copy to ensure we do not change avpkt
1337 AVPacket tmp = *avpkt;
1339 *got_picture_ptr= 0;
1340 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1343 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1344 int did_split = av_packet_split_side_data(&tmp);
1345 apply_param_change(avctx, &tmp);
1347 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1348 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1351 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1353 picture->pkt_dts= avpkt->dts;
1355 if(!avctx->has_b_frames){
1356 picture->pkt_pos= avpkt->pos;
1358 //FIXME these should be under if(!avctx->has_b_frames)
1359 if (!picture->sample_aspect_ratio.num)
1360 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1361 if (!picture->width)
1362 picture->width = avctx->width;
1363 if (!picture->height)
1364 picture->height = avctx->height;
1365 if (picture->format == PIX_FMT_NONE)
1366 picture->format = avctx->pix_fmt;
1369 emms_c(); //needed to avoid an emms_c() call before every return;
1373 ff_packet_free_side_data(&tmp);
1375 if (*got_picture_ptr){
1376 avctx->frame_number++;
1377 picture->best_effort_timestamp = guess_correct_pts(avctx,
1387 #if FF_API_OLD_DECODE_AUDIO
1388 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1389 int *frame_size_ptr,
1393 int ret, got_frame = 0;
1395 if (avctx->get_buffer != avcodec_default_get_buffer) {
1396 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1397 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1398 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1399 "avcodec_decode_audio4()\n");
1400 avctx->get_buffer = avcodec_default_get_buffer;
1401 avctx->release_buffer = avcodec_default_release_buffer;
1404 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1406 if (ret >= 0 && got_frame) {
1408 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1409 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1411 avctx->sample_fmt, 1);
1412 if (*frame_size_ptr < data_size) {
1413 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1414 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1415 return AVERROR(EINVAL);
1418 memcpy(samples, frame.extended_data[0], plane_size);
1420 if (planar && avctx->channels > 1) {
1421 uint8_t *out = ((uint8_t *)samples) + plane_size;
1422 for (ch = 1; ch < avctx->channels; ch++) {
1423 memcpy(out, frame.extended_data[ch], plane_size);
1427 *frame_size_ptr = data_size;
1429 *frame_size_ptr = 0;
1435 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1444 if (!avpkt->data && avpkt->size) {
1445 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1446 return AVERROR(EINVAL);
1449 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1450 av_packet_split_side_data(avpkt);
1451 apply_param_change(avctx, avpkt);
1454 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1455 if (ret >= 0 && *got_frame_ptr) {
1456 avctx->frame_number++;
1457 frame->pkt_dts = avpkt->dts;
1458 if (frame->format == AV_SAMPLE_FMT_NONE)
1459 frame->format = avctx->sample_fmt;
1465 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1473 avcodec_get_subtitle_defaults(sub);
1474 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1476 avctx->frame_number++;
1480 void avsubtitle_free(AVSubtitle *sub)
1484 for (i = 0; i < sub->num_rects; i++)
1486 av_freep(&sub->rects[i]->pict.data[0]);
1487 av_freep(&sub->rects[i]->pict.data[1]);
1488 av_freep(&sub->rects[i]->pict.data[2]);
1489 av_freep(&sub->rects[i]->pict.data[3]);
1490 av_freep(&sub->rects[i]->text);
1491 av_freep(&sub->rects[i]->ass);
1492 av_freep(&sub->rects[i]);
1495 av_freep(&sub->rects);
1497 memset(sub, 0, sizeof(AVSubtitle));
1500 av_cold int avcodec_close(AVCodecContext *avctx)
1502 /* If there is a user-supplied mutex locking routine, call it. */
1503 if (ff_lockmgr_cb) {
1504 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1508 entangled_thread_counter++;
1509 if(entangled_thread_counter != 1){
1510 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1511 entangled_thread_counter--;
1515 if (avcodec_is_open(avctx)) {
1516 if (HAVE_THREADS && avctx->thread_opaque)
1517 ff_thread_free(avctx);
1518 if (avctx->codec && avctx->codec->close)
1519 avctx->codec->close(avctx);
1520 avcodec_default_free_buffers(avctx);
1521 avctx->coded_frame = NULL;
1522 av_freep(&avctx->internal);
1525 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1526 av_opt_free(avctx->priv_data);
1528 av_freep(&avctx->priv_data);
1529 if (av_codec_is_encoder(avctx->codec))
1530 av_freep(&avctx->extradata);
1531 avctx->codec = NULL;
1532 avctx->active_thread_type = 0;
1533 entangled_thread_counter--;
1535 /* Release any user-supplied mutex. */
1536 if (ff_lockmgr_cb) {
1537 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1542 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1545 //This is for future deprecatec codec ids, its empty since
1546 //last major bump but will fill up again over time, please dont remove it
1547 // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1548 default : return id;
1552 AVCodec *avcodec_find_encoder(enum CodecID id)
1554 AVCodec *p, *experimental=NULL;
1556 id= remap_deprecated_codec_id(id);
1558 if (av_codec_is_encoder(p) && p->id == id) {
1559 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1566 return experimental;
1569 AVCodec *avcodec_find_encoder_by_name(const char *name)
1576 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1583 AVCodec *avcodec_find_decoder(enum CodecID id)
1585 AVCodec *p, *experimental=NULL;
1587 id= remap_deprecated_codec_id(id);
1589 if (av_codec_is_decoder(p) && p->id == id) {
1590 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1597 return experimental;
1600 AVCodec *avcodec_find_decoder_by_name(const char *name)
1607 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1614 const char *avcodec_get_name(enum CodecID id)
1620 #include "libavcodec/codec_names.h"
1622 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1624 codec = avcodec_find_decoder(id);
1627 codec = avcodec_find_encoder(id);
1630 return "unknown_codec";
1633 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1635 int i, len, ret = 0;
1637 for (i = 0; i < 4; i++) {
1638 len = snprintf(buf, buf_size,
1639 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1641 buf_size = buf_size > len ? buf_size - len : 0;
1648 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1650 const char *codec_type;
1651 const char *codec_name;
1652 const char *profile = NULL;
1655 AVRational display_aspect_ratio;
1657 if (!buf || buf_size <= 0)
1659 codec_type = av_get_media_type_string(enc->codec_type);
1660 codec_name = avcodec_get_name(enc->codec_id);
1661 if (enc->profile != FF_PROFILE_UNKNOWN) {
1662 p = encode ? avcodec_find_encoder(enc->codec_id) :
1663 avcodec_find_decoder(enc->codec_id);
1665 profile = av_get_profile_name(p, enc->profile);
1668 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1669 codec_name, enc->mb_decision ? " (hq)" : "");
1670 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1672 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1673 if (enc->codec_tag) {
1675 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1676 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1677 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1679 switch(enc->codec_type) {
1680 case AVMEDIA_TYPE_VIDEO:
1681 if (enc->pix_fmt != PIX_FMT_NONE) {
1682 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1684 av_get_pix_fmt_name(enc->pix_fmt));
1687 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1689 enc->width, enc->height);
1690 if (enc->sample_aspect_ratio.num) {
1691 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1692 enc->width*enc->sample_aspect_ratio.num,
1693 enc->height*enc->sample_aspect_ratio.den,
1695 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1696 " [SAR %d:%d DAR %d:%d]",
1697 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1698 display_aspect_ratio.num, display_aspect_ratio.den);
1700 if(av_log_get_level() >= AV_LOG_DEBUG){
1701 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1702 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1704 enc->time_base.num/g, enc->time_base.den/g);
1708 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1709 ", q=%d-%d", enc->qmin, enc->qmax);
1712 case AVMEDIA_TYPE_AUDIO:
1713 if (enc->sample_rate) {
1714 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1715 ", %d Hz", enc->sample_rate);
1717 av_strlcat(buf, ", ", buf_size);
1718 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1719 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1720 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1721 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1728 if (enc->flags & CODEC_FLAG_PASS1)
1729 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1731 if (enc->flags & CODEC_FLAG_PASS2)
1732 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1735 bitrate = get_bit_rate(enc);
1737 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1738 ", %d kb/s", bitrate / 1000);
1742 const char *av_get_profile_name(const AVCodec *codec, int profile)
1745 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1748 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1749 if (p->profile == profile)
1755 unsigned avcodec_version( void )
1757 // av_assert0(CODEC_ID_V410==164);
1758 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1759 av_assert0(CODEC_ID_ADPCM_G722==69660);
1760 // av_assert0(CODEC_ID_BMV_AUDIO==86071);
1761 av_assert0(CODEC_ID_SRT==94216);
1762 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1764 return LIBAVCODEC_VERSION_INT;
1767 const char *avcodec_configuration(void)
1769 return FFMPEG_CONFIGURATION;
1772 const char *avcodec_license(void)
1774 #define LICENSE_PREFIX "libavcodec license: "
1775 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1778 void avcodec_flush_buffers(AVCodecContext *avctx)
1780 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1781 ff_thread_flush(avctx);
1782 else if(avctx->codec->flush)
1783 avctx->codec->flush(avctx);
1786 static void video_free_buffers(AVCodecContext *s)
1788 AVCodecInternal *avci = s->internal;
1794 if (avci->buffer_count)
1795 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1796 avci->buffer_count);
1797 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1798 InternalBuffer *buf = &avci->buffer[i];
1800 av_freep(&buf->base[j]);
1804 av_freep(&avci->buffer);
1806 avci->buffer_count=0;
1809 static void audio_free_buffers(AVCodecContext *avctx)
1811 AVCodecInternal *avci = avctx->internal;
1812 InternalBuffer *buf;
1818 if (buf->extended_data) {
1819 av_free(buf->extended_data[0]);
1820 if (buf->extended_data != buf->data)
1821 av_freep(&buf->extended_data);
1823 av_freep(&avci->buffer);
1826 void avcodec_default_free_buffers(AVCodecContext *avctx)
1828 switch (avctx->codec_type) {
1829 case AVMEDIA_TYPE_VIDEO:
1830 video_free_buffers(avctx);
1832 case AVMEDIA_TYPE_AUDIO:
1833 audio_free_buffers(avctx);
1840 int av_get_exact_bits_per_sample(enum CodecID codec_id)
1843 case CODEC_ID_ADPCM_CT:
1844 case CODEC_ID_ADPCM_IMA_APC:
1845 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1846 case CODEC_ID_ADPCM_IMA_WS:
1847 case CODEC_ID_ADPCM_G722:
1848 case CODEC_ID_ADPCM_YAMAHA:
1850 case CODEC_ID_PCM_ALAW:
1851 case CODEC_ID_PCM_MULAW:
1852 case CODEC_ID_PCM_S8:
1853 case CODEC_ID_PCM_U8:
1854 case CODEC_ID_PCM_ZORK:
1856 case CODEC_ID_PCM_S16BE:
1857 case CODEC_ID_PCM_S16LE:
1858 case CODEC_ID_PCM_S16LE_PLANAR:
1859 case CODEC_ID_PCM_U16BE:
1860 case CODEC_ID_PCM_U16LE:
1862 case CODEC_ID_PCM_S24DAUD:
1863 case CODEC_ID_PCM_S24BE:
1864 case CODEC_ID_PCM_S24LE:
1865 case CODEC_ID_PCM_U24BE:
1866 case CODEC_ID_PCM_U24LE:
1868 case CODEC_ID_PCM_S32BE:
1869 case CODEC_ID_PCM_S32LE:
1870 case CODEC_ID_PCM_U32BE:
1871 case CODEC_ID_PCM_U32LE:
1872 case CODEC_ID_PCM_F32BE:
1873 case CODEC_ID_PCM_F32LE:
1875 case CODEC_ID_PCM_F64BE:
1876 case CODEC_ID_PCM_F64LE:
1883 enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
1885 static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
1886 [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
1887 [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
1888 [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
1889 [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
1890 [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
1891 [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
1892 [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
1893 [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
1894 [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
1895 [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
1897 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
1898 return CODEC_ID_NONE;
1899 if (be < 0 || be > 1)
1901 return map[fmt][be];
1904 int av_get_bits_per_sample(enum CodecID codec_id)
1907 case CODEC_ID_ADPCM_SBPRO_2:
1909 case CODEC_ID_ADPCM_SBPRO_3:
1911 case CODEC_ID_ADPCM_SBPRO_4:
1912 case CODEC_ID_ADPCM_IMA_WAV:
1913 case CODEC_ID_ADPCM_IMA_QT:
1914 case CODEC_ID_ADPCM_SWF:
1915 case CODEC_ID_ADPCM_MS:
1918 return av_get_exact_bits_per_sample(codec_id);
1922 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1924 int id, sr, ch, ba, tag, bps;
1926 id = avctx->codec_id;
1927 sr = avctx->sample_rate;
1928 ch = avctx->channels;
1929 ba = avctx->block_align;
1930 tag = avctx->codec_tag;
1931 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1933 /* codecs with an exact constant bits per sample */
1934 if (bps > 0 && ch > 0 && frame_bytes > 0)
1935 return (frame_bytes * 8) / (bps * ch);
1936 bps = avctx->bits_per_coded_sample;
1938 /* codecs with a fixed packet duration */
1940 case CODEC_ID_ADPCM_ADX: return 32;
1941 case CODEC_ID_ADPCM_IMA_QT: return 64;
1942 case CODEC_ID_ADPCM_EA_XAS: return 128;
1943 case CODEC_ID_AMR_NB:
1945 case CODEC_ID_QCELP:
1946 case CODEC_ID_RA_144:
1947 case CODEC_ID_RA_288: return 160;
1948 case CODEC_ID_IMC: return 256;
1949 case CODEC_ID_AMR_WB:
1950 case CODEC_ID_GSM_MS: return 320;
1951 case CODEC_ID_MP1: return 384;
1952 case CODEC_ID_ATRAC1: return 512;
1953 case CODEC_ID_ATRAC3: return 1024;
1955 case CODEC_ID_MUSEPACK7: return 1152;
1956 case CODEC_ID_AC3: return 1536;
1960 /* calc from sample rate */
1961 if (id == CODEC_ID_TTA)
1962 return 256 * sr / 245;
1965 /* calc from sample rate and channels */
1966 if (id == CODEC_ID_BINKAUDIO_DCT)
1967 return (480 << (sr / 22050)) / ch;
1972 /* calc from block_align */
1973 if (id == CODEC_ID_SIPR) {
1975 case 20: return 160;
1976 case 19: return 144;
1977 case 29: return 288;
1978 case 37: return 480;
1983 if (frame_bytes > 0) {
1984 /* calc from frame_bytes only */
1985 if (id == CODEC_ID_TRUESPEECH)
1986 return 240 * (frame_bytes / 32);
1987 if (id == CODEC_ID_NELLYMOSER)
1988 return 256 * (frame_bytes / 64);
1991 /* calc from frame_bytes and bits_per_coded_sample */
1992 if (id == CODEC_ID_ADPCM_G726)
1993 return frame_bytes * 8 / bps;
1997 /* calc from frame_bytes and channels */
1999 case CODEC_ID_ADPCM_4XM:
2000 case CODEC_ID_ADPCM_IMA_ISS:
2001 return (frame_bytes - 4 * ch) * 2 / ch;
2002 case CODEC_ID_ADPCM_IMA_SMJPEG:
2003 return (frame_bytes - 4) * 2 / ch;
2004 case CODEC_ID_ADPCM_IMA_AMV:
2005 return (frame_bytes - 8) * 2 / ch;
2006 case CODEC_ID_ADPCM_XA:
2007 return (frame_bytes / 128) * 224 / ch;
2008 case CODEC_ID_INTERPLAY_DPCM:
2009 return (frame_bytes - 6 - ch) / ch;
2010 case CODEC_ID_ROQ_DPCM:
2011 return (frame_bytes - 8) / ch;
2012 case CODEC_ID_XAN_DPCM:
2013 return (frame_bytes - 2 * ch) / ch;
2014 case CODEC_ID_MACE3:
2015 return 3 * frame_bytes / ch;
2016 case CODEC_ID_MACE6:
2017 return 6 * frame_bytes / ch;
2018 case CODEC_ID_PCM_LXF:
2019 return 2 * (frame_bytes / (5 * ch));
2023 /* calc from frame_bytes, channels, and codec_tag */
2024 if (id == CODEC_ID_SOL_DPCM) {
2026 return frame_bytes / ch;
2028 return frame_bytes * 2 / ch;
2033 /* calc from frame_bytes, channels, and block_align */
2034 int blocks = frame_bytes / ba;
2035 switch (avctx->codec_id) {
2036 case CODEC_ID_ADPCM_IMA_WAV:
2037 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2038 case CODEC_ID_ADPCM_IMA_DK3:
2039 return blocks * (((ba - 16) * 8 / 3) / ch);
2040 case CODEC_ID_ADPCM_IMA_DK4:
2041 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2042 case CODEC_ID_ADPCM_MS:
2043 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2048 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2049 switch (avctx->codec_id) {
2050 case CODEC_ID_PCM_DVD:
2051 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2052 case CODEC_ID_PCM_BLURAY:
2053 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2054 case CODEC_ID_S302M:
2055 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2065 int ff_thread_init(AVCodecContext *s){
2070 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2084 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
2086 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
2090 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2092 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
2093 "version to the newest one from Git. If the problem still "
2094 "occurs, it means that your file has a feature which has not "
2095 "been implemented.\n", feature);
2097 av_log_ask_for_sample(avc, NULL);
2100 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2102 va_list argument_list;
2104 va_start(argument_list, msg);
2107 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2108 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2109 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2110 "and contact the ffmpeg-devel mailing list.\n");
2112 va_end(argument_list);
2115 static AVHWAccel *first_hwaccel = NULL;
2117 void av_register_hwaccel(AVHWAccel *hwaccel)
2119 AVHWAccel **p = &first_hwaccel;
2123 hwaccel->next = NULL;
2126 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2128 return hwaccel ? hwaccel->next : first_hwaccel;
2131 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
2133 AVHWAccel *hwaccel=NULL;
2135 while((hwaccel= av_hwaccel_next(hwaccel))){
2136 if ( hwaccel->id == codec_id
2137 && hwaccel->pix_fmt == pix_fmt)
2143 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2145 if (ff_lockmgr_cb) {
2146 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2148 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2154 if (ff_lockmgr_cb) {
2155 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2157 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2163 int avpriv_lock_avformat(void)
2165 if (ff_lockmgr_cb) {
2166 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2172 int avpriv_unlock_avformat(void)
2174 if (ff_lockmgr_cb) {
2175 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2181 unsigned int avpriv_toupper4(unsigned int x)
2183 return toupper( x &0xFF)
2184 + (toupper((x>>8 )&0xFF)<<8 )
2185 + (toupper((x>>16)&0xFF)<<16)
2186 + (toupper((x>>24)&0xFF)<<24);
2191 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2195 ff_init_buffer_info(avctx, f);
2197 return avctx->get_buffer(avctx, f);
2200 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2202 f->owner->release_buffer(f->owner, f);
2205 void ff_thread_finish_setup(AVCodecContext *avctx)
2209 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2213 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2219 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2221 AVCodec *c= avcodec_find_decoder(codec_id);
2223 c= avcodec_find_encoder(codec_id);
2227 if (codec_id <= CODEC_ID_NONE)
2228 return AVMEDIA_TYPE_UNKNOWN;
2229 else if (codec_id < CODEC_ID_FIRST_AUDIO)
2230 return AVMEDIA_TYPE_VIDEO;
2231 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2232 return AVMEDIA_TYPE_AUDIO;
2233 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2234 return AVMEDIA_TYPE_SUBTITLE;
2236 return AVMEDIA_TYPE_UNKNOWN;
2239 int avcodec_is_open(AVCodecContext *s)
2241 return !!s->internal;