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 #if !FF_API_AVCODEC_INIT
113 void avcodec_init(void)
115 static int initialized = 0;
117 if (initialized != 0)
121 dsputil_static_init();
124 static av_always_inline int codec_is_encoder(AVCodec *codec)
126 return codec && (codec->encode || codec->encode2);
129 static av_always_inline int codec_is_decoder(AVCodec *codec)
131 return codec && codec->decode;
134 void avcodec_register(AVCodec *codec)
139 while (*p != NULL) p = &(*p)->next;
143 if (codec->init_static_data)
144 codec->init_static_data(codec);
147 unsigned avcodec_get_edge_width(void)
152 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
153 s->coded_width = width;
154 s->coded_height= height;
155 s->width = -((-width )>>s->lowres);
156 s->height= -((-height)>>s->lowres);
159 #define INTERNAL_BUFFER_SIZE (32+1)
161 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
162 int linesize_align[AV_NUM_DATA_POINTERS])
169 case PIX_FMT_YUV420P:
170 case PIX_FMT_YUYV422:
171 case PIX_FMT_UYVY422:
172 case PIX_FMT_YUV422P:
173 case PIX_FMT_YUV440P:
174 case PIX_FMT_YUV444P:
177 case PIX_FMT_GRAY16BE:
178 case PIX_FMT_GRAY16LE:
179 case PIX_FMT_YUVJ420P:
180 case PIX_FMT_YUVJ422P:
181 case PIX_FMT_YUVJ440P:
182 case PIX_FMT_YUVJ444P:
183 case PIX_FMT_YUVA420P:
184 case PIX_FMT_YUV420P9LE:
185 case PIX_FMT_YUV420P9BE:
186 case PIX_FMT_YUV420P10LE:
187 case PIX_FMT_YUV420P10BE:
188 case PIX_FMT_YUV422P9LE:
189 case PIX_FMT_YUV422P9BE:
190 case PIX_FMT_YUV422P10LE:
191 case PIX_FMT_YUV422P10BE:
192 case PIX_FMT_YUV444P9LE:
193 case PIX_FMT_YUV444P9BE:
194 case PIX_FMT_YUV444P10LE:
195 case PIX_FMT_YUV444P10BE:
196 case PIX_FMT_GBRP9LE:
197 case PIX_FMT_GBRP9BE:
198 case PIX_FMT_GBRP10LE:
199 case 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 PIX_FMT_YUV411P:
204 case PIX_FMT_UYYVYY411:
208 case PIX_FMT_YUV410P:
209 if(s->codec_id == CODEC_ID_SVQ1){
214 if(s->codec_id == CODEC_ID_RPZA){
221 if(s->codec_id == CODEC_ID_SMC){
227 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
238 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
239 w_align= FFMAX(w_align, 8);
242 *width = FFALIGN(*width , w_align);
243 *height= FFALIGN(*height, h_align);
244 if(s->codec_id == CODEC_ID_H264 || s->lowres)
245 *height+=2; // some of the optimized chroma MC reads one line too much
246 // which is also done in mpeg decoders with lowres > 0
248 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
249 linesize_align[i] = STRIDE_ALIGN;
250 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
251 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
252 //picture size unneccessarily in some cases. The solution here is not
253 //pretty and better ideas are welcome!
255 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
256 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
257 s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
258 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
259 linesize_align[i] = 16;
264 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
265 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
266 int linesize_align[AV_NUM_DATA_POINTERS];
268 avcodec_align_dimensions2(s, width, height, linesize_align);
269 align = FFMAX(linesize_align[0], linesize_align[3]);
270 linesize_align[1] <<= chroma_shift;
271 linesize_align[2] <<= chroma_shift;
272 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
273 *width=FFALIGN(*width, align);
276 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
279 pic->pkt_pts = s->pkt->pts;
280 pic->pkt_pos = s->pkt->pos;
282 pic->pkt_pts = AV_NOPTS_VALUE;
285 pic->reordered_opaque= s->reordered_opaque;
286 pic->sample_aspect_ratio = s->sample_aspect_ratio;
287 pic->width = s->width;
288 pic->height = s->height;
289 pic->format = s->pix_fmt;
292 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
293 enum AVSampleFormat sample_fmt, const uint8_t *buf,
294 int buf_size, int align)
296 int ch, planar, needed_size, ret = 0;
298 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
299 frame->nb_samples, sample_fmt,
301 if (buf_size < needed_size)
302 return AVERROR(EINVAL);
304 planar = av_sample_fmt_is_planar(sample_fmt);
305 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
306 if (!(frame->extended_data = av_mallocz(nb_channels *
307 sizeof(*frame->extended_data))))
308 return AVERROR(ENOMEM);
310 frame->extended_data = frame->data;
313 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
314 buf, nb_channels, frame->nb_samples,
315 sample_fmt, align)) < 0) {
316 if (frame->extended_data != frame->data)
317 av_freep(&frame->extended_data);
320 if (frame->extended_data != frame->data) {
321 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
322 frame->data[ch] = frame->extended_data[ch];
328 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
330 AVCodecInternal *avci = avctx->internal;
334 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
335 frame->nb_samples, avctx->sample_fmt,
338 return AVERROR(EINVAL);
340 /* allocate InternalBuffer if needed */
342 avci->buffer = av_mallocz(sizeof(InternalBuffer));
344 return AVERROR(ENOMEM);
348 /* if there is a previously-used internal buffer, check its size and
349 channel count to see if we can reuse it */
350 if (buf->extended_data) {
351 /* if current buffer is too small, free it */
352 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
353 av_free(buf->extended_data[0]);
354 if (buf->extended_data != buf->data)
355 av_freep(&buf->extended_data);
356 buf->extended_data = NULL;
359 /* if number of channels has changed, reset and/or free extended data
360 pointers but leave data buffer in buf->data[0] for reuse */
361 if (buf->nb_channels != avctx->channels) {
362 if (buf->extended_data != buf->data)
363 av_free(buf->extended_data);
364 buf->extended_data = NULL;
368 /* if there is no previous buffer or the previous buffer cannot be used
369 as-is, allocate a new buffer and/or rearrange the channel pointers */
370 if (!buf->extended_data) {
372 if (!(buf->data[0] = av_mallocz(buf_size)))
373 return AVERROR(ENOMEM);
374 buf->audio_data_size = buf_size;
376 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
377 avctx->sample_fmt, buf->data[0],
378 buf->audio_data_size, 32)))
381 if (frame->extended_data == frame->data)
382 buf->extended_data = buf->data;
384 buf->extended_data = frame->extended_data;
385 memcpy(buf->data, frame->data, sizeof(frame->data));
386 buf->linesize[0] = frame->linesize[0];
387 buf->nb_channels = avctx->channels;
389 /* copy InternalBuffer info to the AVFrame */
390 frame->extended_data = buf->extended_data;
391 frame->linesize[0] = buf->linesize[0];
392 memcpy(frame->data, buf->data, sizeof(frame->data));
395 frame->type = FF_BUFFER_TYPE_INTERNAL;
398 frame->pkt_pts = avctx->pkt->pts;
399 frame->pkt_pos = avctx->pkt->pos;
401 frame->pkt_pts = AV_NOPTS_VALUE;
405 frame->reordered_opaque = avctx->reordered_opaque;
407 if (avctx->debug & FF_DEBUG_BUFFERS)
408 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
409 "internal audio buffer used\n", frame);
414 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
420 AVCodecInternal *avci = s->internal;
422 if(pic->data[0]!=NULL) {
423 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
426 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
427 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
431 if(av_image_check_size(w, h, 0, s))
435 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
436 sizeof(InternalBuffer));
439 buf = &avci->buffer[avci->buffer_count];
441 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
442 if(s->active_thread_type&FF_THREAD_FRAME) {
443 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
447 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
448 av_freep(&buf->base[i]);
454 int h_chroma_shift, v_chroma_shift;
459 int stride_align[AV_NUM_DATA_POINTERS];
460 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
462 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
464 avcodec_align_dimensions2(s, &w, &h, stride_align);
466 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
472 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
473 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
474 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
475 // increase alignment of w for next try (rhs gives the lowest bit set in w)
480 unaligned |= picture.linesize[i] % stride_align[i];
484 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
488 for (i=0; i<3 && picture.data[i+1]; i++)
489 size[i] = picture.data[i+1] - picture.data[i];
490 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
492 memset(buf->base, 0, sizeof(buf->base));
493 memset(buf->data, 0, sizeof(buf->data));
495 for(i=0; i<4 && size[i]; i++){
496 const int h_shift= i==0 ? 0 : h_chroma_shift;
497 const int v_shift= i==0 ? 0 : v_chroma_shift;
499 buf->linesize[i]= picture.linesize[i];
501 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
502 if(buf->base[i]==NULL) return -1;
503 memset(buf->base[i], 128, size[i]);
505 // no edge if EDGE EMU or not planar YUV
506 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
507 buf->data[i] = buf->base[i];
509 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
511 for (; i < AV_NUM_DATA_POINTERS; i++) {
512 buf->base[i] = buf->data[i] = NULL;
513 buf->linesize[i] = 0;
515 if(size[1] && !size[2])
516 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
517 buf->width = s->width;
518 buf->height = s->height;
519 buf->pix_fmt= s->pix_fmt;
521 pic->type= FF_BUFFER_TYPE_INTERNAL;
523 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
524 pic->base[i]= buf->base[i];
525 pic->data[i]= buf->data[i];
526 pic->linesize[i]= buf->linesize[i];
528 pic->extended_data = pic->data;
529 avci->buffer_count++;
532 pic->pkt_pts = s->pkt->pts;
533 pic->pkt_pos = s->pkt->pos;
535 pic->pkt_pts = AV_NOPTS_VALUE;
538 pic->reordered_opaque= s->reordered_opaque;
539 pic->sample_aspect_ratio = s->sample_aspect_ratio;
540 pic->width = s->width;
541 pic->height = s->height;
542 pic->format = s->pix_fmt;
544 if(s->debug&FF_DEBUG_BUFFERS)
545 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
546 "buffers used\n", pic, avci->buffer_count);
551 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
553 switch (avctx->codec_type) {
554 case AVMEDIA_TYPE_VIDEO:
555 return video_get_buffer(avctx, frame);
556 case AVMEDIA_TYPE_AUDIO:
557 return audio_get_buffer(avctx, frame);
563 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
565 InternalBuffer *buf, *last;
566 AVCodecInternal *avci = s->internal;
568 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
570 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
571 assert(avci->buffer_count);
574 buf = NULL; /* avoids warning */
575 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
576 buf = &avci->buffer[i];
577 if (buf->data[0] == pic->data[0])
580 assert(i < avci->buffer_count);
581 avci->buffer_count--;
582 last = &avci->buffer[avci->buffer_count];
585 FFSWAP(InternalBuffer, *buf, *last);
588 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
590 // pic->base[i]=NULL;
592 //printf("R%X\n", pic->opaque);
594 if(s->debug&FF_DEBUG_BUFFERS)
595 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
596 "buffers used\n", pic, avci->buffer_count);
599 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
603 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
605 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
606 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
607 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
608 s->release_buffer(s, pic);
611 ff_init_buffer_info(s, pic);
613 /* If no picture return a new buffer */
614 if(pic->data[0] == NULL) {
615 /* We will copy from buffer, so must be readable */
616 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
617 return s->get_buffer(s, pic);
620 /* If internal buffer type return the same buffer */
621 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
626 * Not internal type and reget_buffer not overridden, emulate cr buffer
629 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
630 pic->data[i] = pic->base[i] = NULL;
632 /* Allocate new frame */
633 if (s->get_buffer(s, pic))
635 /* Copy image data from old buffer to new buffer */
636 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
638 s->release_buffer(s, &temp_pic); // Release old frame
642 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
645 for(i=0; i<count; i++){
646 int r= func(c, (char*)arg + i*size);
652 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
655 for(i=0; i<count; i++){
656 int r= func(c, arg, i, 0);
662 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
663 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
668 void avcodec_get_frame_defaults(AVFrame *pic){
669 memset(pic, 0, sizeof(AVFrame));
671 pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
674 pic->sample_aspect_ratio = (AVRational){0, 1};
675 pic->format = -1; /* unknown */
678 AVFrame *avcodec_alloc_frame(void){
679 AVFrame *pic= av_malloc(sizeof(AVFrame));
681 if(pic==NULL) return NULL;
683 avcodec_get_frame_defaults(pic);
688 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
690 memset(sub, 0, sizeof(*sub));
691 sub->pts = AV_NOPTS_VALUE;
694 #if FF_API_AVCODEC_OPEN
695 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
697 return avcodec_open2(avctx, codec, NULL);
701 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
704 AVDictionary *tmp = NULL;
706 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
707 return AVERROR(EINVAL);
710 av_dict_copy(&tmp, *options, 0);
712 /* If there is a user-supplied mutex locking routine, call it. */
714 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
718 entangled_thread_counter++;
719 if(entangled_thread_counter != 1){
720 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
725 if(avctx->codec || !codec) {
726 ret = AVERROR(EINVAL);
730 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
731 if (!avctx->internal) {
732 ret = AVERROR(ENOMEM);
736 if (codec->priv_data_size > 0) {
737 if(!avctx->priv_data){
738 avctx->priv_data = av_mallocz(codec->priv_data_size);
739 if (!avctx->priv_data) {
740 ret = AVERROR(ENOMEM);
743 if (codec->priv_class) {
744 *(AVClass**)avctx->priv_data= codec->priv_class;
745 av_opt_set_defaults(avctx->priv_data);
748 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
751 avctx->priv_data = NULL;
753 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
756 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
757 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
758 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
763 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
764 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
765 if(avctx->coded_width && avctx->coded_height)
766 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
767 else if(avctx->width && avctx->height)
768 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
771 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
772 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
773 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
774 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
775 avcodec_set_dimensions(avctx, 0, 0);
778 /* if the decoder init function was already called previously,
779 free the already allocated subtitle_header before overwriting it */
780 if (codec_is_decoder(codec))
781 av_freep(&avctx->subtitle_header);
783 #define SANE_NB_CHANNELS 128U
784 if (avctx->channels > SANE_NB_CHANNELS) {
785 ret = AVERROR(EINVAL);
789 avctx->codec = codec;
790 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
791 avctx->codec_id == CODEC_ID_NONE) {
792 avctx->codec_type = codec->type;
793 avctx->codec_id = codec->id;
795 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
796 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
797 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
798 ret = AVERROR(EINVAL);
801 avctx->frame_number = 0;
804 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %X\n",
805 avctx->error_recognition, avctx->err_recognition);
806 switch(avctx->error_recognition){
807 case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
809 case FF_ER_VERY_AGGRESSIVE:
810 case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
811 case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
812 case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
815 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %X\n",
816 avctx->error_recognition, avctx->err_recognition);
820 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
822 if (HAVE_THREADS && !avctx->thread_opaque) {
823 ret = ff_thread_init(avctx);
828 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
829 avctx->thread_count = 1;
831 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
832 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
833 avctx->codec->max_lowres);
834 ret = AVERROR(EINVAL);
837 if (codec_is_encoder(avctx->codec)) {
839 if (avctx->codec->sample_fmts) {
840 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
841 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
843 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
844 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
845 ret = AVERROR(EINVAL);
849 if (avctx->codec->supported_samplerates) {
850 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
851 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
853 if (avctx->codec->supported_samplerates[i] == 0) {
854 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
855 ret = AVERROR(EINVAL);
859 if (avctx->codec->channel_layouts) {
860 if (!avctx->channel_layout) {
861 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
863 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
864 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
866 if (avctx->codec->channel_layouts[i] == 0) {
867 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
868 ret = AVERROR(EINVAL);
873 if (avctx->channel_layout && avctx->channels) {
874 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
875 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
876 ret = AVERROR(EINVAL);
879 } else if (avctx->channel_layout) {
880 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
884 avctx->pts_correction_num_faulty_pts =
885 avctx->pts_correction_num_faulty_dts = 0;
886 avctx->pts_correction_last_pts =
887 avctx->pts_correction_last_dts = INT64_MIN;
889 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
890 ret = avctx->codec->init(avctx);
898 entangled_thread_counter--;
900 /* Release any user-supplied mutex. */
902 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
905 av_dict_free(options);
912 av_freep(&avctx->priv_data);
913 av_freep(&avctx->internal);
918 int ff_alloc_packet(AVPacket *avpkt, int size)
920 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
921 return AVERROR(EINVAL);
927 if (avpkt->size < size)
928 return AVERROR(EINVAL);
930 pkt_data = avpkt->data;
931 pkt_size = avpkt->size;
932 av_init_packet(avpkt);
933 avpkt->data = pkt_data;
934 avpkt->size = pkt_size;
937 return av_new_packet(avpkt, size);
941 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
943 const AVFrame *frame,
947 int user_packet = !!avpkt->data;
950 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
951 av_init_packet(avpkt);
956 /* check for valid frame size */
958 nb_samples = frame->nb_samples;
959 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
960 if (nb_samples > avctx->frame_size)
961 return AVERROR(EINVAL);
962 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
963 if (nb_samples != avctx->frame_size)
964 return AVERROR(EINVAL);
967 nb_samples = avctx->frame_size;
970 if (avctx->codec->encode2) {
972 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
973 if (!ret && *got_packet_ptr &&
974 !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
975 avpkt->pts = frame->pts;
976 avpkt->duration = av_rescale_q(frame->nb_samples,
977 (AVRational){ 1, avctx->sample_rate },
981 /* for compatibility with encoders not supporting encode2(), we need to
982 allocate a packet buffer if the user has not provided one or check
983 the size otherwise */
985 int buf_size = avpkt->size;
987 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
988 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
990 return AVERROR(EINVAL);
991 buf_size = nb_samples * avctx->channels *
992 av_get_bits_per_sample(avctx->codec_id) / 8;
994 /* this is a guess as to the required size.
995 if an encoder needs more than this, it should probably
996 implement encode2() */
997 buf_size = 2 * avctx->frame_size * avctx->channels *
998 av_get_bytes_per_sample(avctx->sample_fmt);
999 buf_size += FF_MIN_BUFFER_SIZE;
1002 if ((ret = ff_alloc_packet(avpkt, buf_size)))
1005 /* Encoders using AVCodec.encode() that support
1006 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
1007 the smaller size when encoding the last frame.
1008 This code can be removed once all encoders supporting
1009 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
1010 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
1011 nb_samples < avctx->frame_size) {
1012 fs_tmp = avctx->frame_size;
1013 avctx->frame_size = nb_samples;
1016 /* encode the frame */
1017 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
1018 frame ? frame->data[0] : NULL);
1021 /* no output. if the packet data was allocated by libavcodec,
1024 av_freep(&avpkt->data);
1026 if (avctx->coded_frame)
1027 avpkt->pts = avctx->coded_frame->pts;
1028 /* Set duration for final small packet. This can be removed
1029 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1032 avpkt->duration = av_rescale_q(avctx->frame_size,
1033 (AVRational){ 1, avctx->sample_rate },
1038 *got_packet_ptr = (ret > 0);
1043 avctx->frame_size = fs_tmp;
1046 avctx->frame_number++;
1048 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1049 this needs to be moved to the encoders, but for now we can do it
1050 here to simplify things */
1051 avpkt->flags |= AV_PKT_FLAG_KEY;
1056 #if FF_API_OLD_DECODE_AUDIO
1057 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1058 uint8_t *buf, int buf_size,
1059 const short *samples)
1064 int ret, samples_size, got_packet;
1066 av_init_packet(&pkt);
1068 pkt.size = buf_size;
1072 avcodec_get_frame_defaults(frame);
1074 if (avctx->frame_size) {
1075 frame->nb_samples = avctx->frame_size;
1077 /* if frame_size is not set, the number of samples must be
1078 calculated from the buffer size */
1080 if (!av_get_bits_per_sample(avctx->codec_id)) {
1081 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1082 "support this codec\n");
1083 return AVERROR(EINVAL);
1085 nb_samples = (int64_t)buf_size * 8 /
1086 (av_get_bits_per_sample(avctx->codec_id) *
1088 if (nb_samples >= INT_MAX)
1089 return AVERROR(EINVAL);
1090 frame->nb_samples = nb_samples;
1093 /* it is assumed that the samples buffer is large enough based on the
1094 relevant parameters */
1095 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1097 avctx->sample_fmt, 1);
1098 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1100 samples, samples_size, 1)))
1103 /* fabricate frame pts from sample count.
1104 this is needed because the avcodec_encode_audio() API does not have
1105 a way for the user to provide pts */
1106 if(avctx->sample_rate && avctx->time_base.num)
1107 frame->pts = av_rescale_q(avctx->internal->sample_count,
1108 (AVRational){ 1, avctx->sample_rate },
1111 frame->pts = AV_NOPTS_VALUE;
1112 avctx->internal->sample_count += frame->nb_samples;
1118 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1119 if (!ret && got_packet && avctx->coded_frame) {
1120 avctx->coded_frame->pts = pkt.pts;
1121 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1123 /* free any side data since we cannot return it */
1124 ff_packet_free_side_data(&pkt);
1126 if (frame && frame->extended_data != frame->data)
1127 av_freep(&frame->extended_data);
1129 return ret ? ret : pkt.size;
1133 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1134 const AVFrame *pict)
1136 if(buf_size < FF_MIN_BUFFER_SIZE){
1137 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1140 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
1142 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
1143 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
1144 avctx->frame_number++;
1145 emms_c(); //needed to avoid an emms_c() call before every return;
1152 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1153 const AVSubtitle *sub)
1156 if(sub->start_display_time) {
1157 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1161 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1162 avctx->frame_number++;
1167 * Attempt to guess proper monotonic timestamps for decoded video frames
1168 * which might have incorrect times. Input timestamps may wrap around, in
1169 * which case the output will as well.
1171 * @param pts the pts field of the decoded AVPacket, as passed through
1173 * @param dts the dts field of the decoded AVPacket
1174 * @return one of the input values, may be AV_NOPTS_VALUE
1176 static int64_t guess_correct_pts(AVCodecContext *ctx,
1177 int64_t reordered_pts, int64_t dts)
1179 int64_t pts = AV_NOPTS_VALUE;
1181 if (dts != AV_NOPTS_VALUE) {
1182 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1183 ctx->pts_correction_last_dts = dts;
1185 if (reordered_pts != AV_NOPTS_VALUE) {
1186 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1187 ctx->pts_correction_last_pts = reordered_pts;
1189 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1190 && reordered_pts != AV_NOPTS_VALUE)
1191 pts = reordered_pts;
1198 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1201 const uint8_t *data;
1204 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1207 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1208 if (!data || size < 4)
1210 flags = bytestream_get_le32(&data);
1212 if (size < 4) /* Required for any of the changes */
1214 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1215 avctx->channels = bytestream_get_le32(&data);
1218 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1221 avctx->channel_layout = bytestream_get_le64(&data);
1226 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1227 avctx->sample_rate = bytestream_get_le32(&data);
1230 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1233 avctx->width = bytestream_get_le32(&data);
1234 avctx->height = bytestream_get_le32(&data);
1235 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1240 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1241 int *got_picture_ptr,
1242 const AVPacket *avpkt)
1245 // copy to ensure we do not change avpkt
1246 AVPacket tmp = *avpkt;
1248 *got_picture_ptr= 0;
1249 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1252 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1253 int did_split = av_packet_split_side_data(&tmp);
1254 apply_param_change(avctx, &tmp);
1256 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1257 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1260 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1262 picture->pkt_dts= avpkt->dts;
1264 if(!avctx->has_b_frames){
1265 picture->pkt_pos= avpkt->pos;
1267 //FIXME these should be under if(!avctx->has_b_frames)
1268 if (!picture->sample_aspect_ratio.num)
1269 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1270 if (!picture->width)
1271 picture->width = avctx->width;
1272 if (!picture->height)
1273 picture->height = avctx->height;
1274 if (picture->format == PIX_FMT_NONE)
1275 picture->format = avctx->pix_fmt;
1278 emms_c(); //needed to avoid an emms_c() call before every return;
1282 ff_packet_free_side_data(&tmp);
1284 if (*got_picture_ptr){
1285 avctx->frame_number++;
1286 picture->best_effort_timestamp = guess_correct_pts(avctx,
1296 #if FF_API_OLD_DECODE_AUDIO
1297 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1298 int *frame_size_ptr,
1302 int ret, got_frame = 0;
1304 if (avctx->get_buffer != avcodec_default_get_buffer) {
1305 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1306 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1307 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1308 "avcodec_decode_audio4()\n");
1309 avctx->get_buffer = avcodec_default_get_buffer;
1310 avctx->release_buffer = avcodec_default_release_buffer;
1313 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1315 if (ret >= 0 && got_frame) {
1317 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1318 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1320 avctx->sample_fmt, 1);
1321 if (*frame_size_ptr < data_size) {
1322 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1323 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1324 return AVERROR(EINVAL);
1327 memcpy(samples, frame.extended_data[0], plane_size);
1329 if (planar && avctx->channels > 1) {
1330 uint8_t *out = ((uint8_t *)samples) + plane_size;
1331 for (ch = 1; ch < avctx->channels; ch++) {
1332 memcpy(out, frame.extended_data[ch], plane_size);
1336 *frame_size_ptr = data_size;
1338 *frame_size_ptr = 0;
1344 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1353 if (!avpkt->data && avpkt->size) {
1354 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1355 return AVERROR(EINVAL);
1358 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1359 av_packet_split_side_data(avpkt);
1360 apply_param_change(avctx, avpkt);
1363 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1364 if (ret >= 0 && *got_frame_ptr) {
1365 avctx->frame_number++;
1366 frame->pkt_dts = avpkt->dts;
1367 if (frame->format == AV_SAMPLE_FMT_NONE)
1368 frame->format = avctx->sample_fmt;
1374 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1382 avcodec_get_subtitle_defaults(sub);
1383 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1385 avctx->frame_number++;
1389 void avsubtitle_free(AVSubtitle *sub)
1393 for (i = 0; i < sub->num_rects; i++)
1395 av_freep(&sub->rects[i]->pict.data[0]);
1396 av_freep(&sub->rects[i]->pict.data[1]);
1397 av_freep(&sub->rects[i]->pict.data[2]);
1398 av_freep(&sub->rects[i]->pict.data[3]);
1399 av_freep(&sub->rects[i]->text);
1400 av_freep(&sub->rects[i]->ass);
1401 av_freep(&sub->rects[i]);
1404 av_freep(&sub->rects);
1406 memset(sub, 0, sizeof(AVSubtitle));
1409 av_cold int avcodec_close(AVCodecContext *avctx)
1411 /* If there is a user-supplied mutex locking routine, call it. */
1412 if (ff_lockmgr_cb) {
1413 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1417 entangled_thread_counter++;
1418 if(entangled_thread_counter != 1){
1419 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1420 entangled_thread_counter--;
1424 if (HAVE_THREADS && avctx->thread_opaque)
1425 ff_thread_free(avctx);
1426 if (avctx->codec && avctx->codec->close)
1427 avctx->codec->close(avctx);
1428 avcodec_default_free_buffers(avctx);
1429 avctx->coded_frame = NULL;
1430 av_freep(&avctx->internal);
1431 if (avctx->codec && avctx->codec->priv_class)
1432 av_opt_free(avctx->priv_data);
1434 av_freep(&avctx->priv_data);
1435 if (codec_is_encoder(avctx->codec))
1436 av_freep(&avctx->extradata);
1437 avctx->codec = NULL;
1438 avctx->active_thread_type = 0;
1439 entangled_thread_counter--;
1441 /* Release any user-supplied mutex. */
1442 if (ff_lockmgr_cb) {
1443 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1448 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1451 case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
1452 case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
1453 case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1454 default : return id;
1458 AVCodec *avcodec_find_encoder(enum CodecID id)
1460 AVCodec *p, *experimental=NULL;
1462 id= remap_deprecated_codec_id(id);
1464 if (codec_is_encoder(p) && p->id == id) {
1465 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1472 return experimental;
1475 AVCodec *avcodec_find_encoder_by_name(const char *name)
1482 if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
1489 AVCodec *avcodec_find_decoder(enum CodecID id)
1491 AVCodec *p, *experimental=NULL;
1493 id= remap_deprecated_codec_id(id);
1495 if (codec_is_decoder(p) && p->id == id) {
1496 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1503 return experimental;
1506 AVCodec *avcodec_find_decoder_by_name(const char *name)
1513 if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
1520 static int get_bit_rate(AVCodecContext *ctx)
1523 int bits_per_sample;
1525 switch(ctx->codec_type) {
1526 case AVMEDIA_TYPE_VIDEO:
1527 case AVMEDIA_TYPE_DATA:
1528 case AVMEDIA_TYPE_SUBTITLE:
1529 case AVMEDIA_TYPE_ATTACHMENT:
1530 bit_rate = ctx->bit_rate;
1532 case AVMEDIA_TYPE_AUDIO:
1533 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1534 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1543 const char *avcodec_get_name(enum CodecID id)
1549 #include "libavcodec/codec_names.h"
1551 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1553 codec = avcodec_find_decoder(id);
1556 codec = avcodec_find_encoder(id);
1559 return "unknown_codec";
1562 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1564 int i, len, ret = 0;
1566 for (i = 0; i < 4; i++) {
1567 len = snprintf(buf, buf_size,
1568 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1570 buf_size = buf_size > len ? buf_size - len : 0;
1577 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1579 const char *codec_type;
1580 const char *codec_name;
1581 const char *profile = NULL;
1584 AVRational display_aspect_ratio;
1586 if (!buf || buf_size <= 0)
1588 codec_type = av_get_media_type_string(enc->codec_type);
1589 codec_name = avcodec_get_name(enc->codec_id);
1590 if (enc->profile != FF_PROFILE_UNKNOWN) {
1591 p = encode ? avcodec_find_encoder(enc->codec_id) :
1592 avcodec_find_decoder(enc->codec_id);
1594 profile = av_get_profile_name(p, enc->profile);
1597 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1598 codec_name, enc->mb_decision ? " (hq)" : "");
1599 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1601 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1602 if (enc->codec_tag) {
1604 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1605 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1606 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1608 switch(enc->codec_type) {
1609 case AVMEDIA_TYPE_VIDEO:
1610 if (enc->pix_fmt != PIX_FMT_NONE) {
1611 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1613 av_get_pix_fmt_name(enc->pix_fmt));
1616 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1618 enc->width, enc->height);
1619 if (enc->sample_aspect_ratio.num) {
1620 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1621 enc->width*enc->sample_aspect_ratio.num,
1622 enc->height*enc->sample_aspect_ratio.den,
1624 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1625 " [SAR %d:%d DAR %d:%d]",
1626 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1627 display_aspect_ratio.num, display_aspect_ratio.den);
1629 if(av_log_get_level() >= AV_LOG_DEBUG){
1630 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1631 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1633 enc->time_base.num/g, enc->time_base.den/g);
1637 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1638 ", q=%d-%d", enc->qmin, enc->qmax);
1641 case AVMEDIA_TYPE_AUDIO:
1642 if (enc->sample_rate) {
1643 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1644 ", %d Hz", enc->sample_rate);
1646 av_strlcat(buf, ", ", buf_size);
1647 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1648 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1649 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1650 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1657 if (enc->flags & CODEC_FLAG_PASS1)
1658 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1660 if (enc->flags & CODEC_FLAG_PASS2)
1661 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1664 bitrate = get_bit_rate(enc);
1666 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1667 ", %d kb/s", bitrate / 1000);
1671 const char *av_get_profile_name(const AVCodec *codec, int profile)
1674 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1677 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1678 if (p->profile == profile)
1684 unsigned avcodec_version( void )
1686 av_assert0(CODEC_ID_V410==164);
1687 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1688 av_assert0(CODEC_ID_ADPCM_G722==69660);
1689 av_assert0(CODEC_ID_BMV_AUDIO==86071);
1690 av_assert0(CODEC_ID_SRT==94216);
1691 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1693 return LIBAVCODEC_VERSION_INT;
1696 const char *avcodec_configuration(void)
1698 return FFMPEG_CONFIGURATION;
1701 const char *avcodec_license(void)
1703 #define LICENSE_PREFIX "libavcodec license: "
1704 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1707 void avcodec_flush_buffers(AVCodecContext *avctx)
1709 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1710 ff_thread_flush(avctx);
1711 else if(avctx->codec->flush)
1712 avctx->codec->flush(avctx);
1715 static void video_free_buffers(AVCodecContext *s)
1717 AVCodecInternal *avci = s->internal;
1723 if (avci->buffer_count)
1724 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1725 avci->buffer_count);
1726 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1727 InternalBuffer *buf = &avci->buffer[i];
1729 av_freep(&buf->base[j]);
1733 av_freep(&avci->buffer);
1735 avci->buffer_count=0;
1738 static void audio_free_buffers(AVCodecContext *avctx)
1740 AVCodecInternal *avci = avctx->internal;
1741 InternalBuffer *buf;
1747 if (buf->extended_data) {
1748 av_free(buf->extended_data[0]);
1749 if (buf->extended_data != buf->data)
1750 av_freep(&buf->extended_data);
1752 av_freep(&avci->buffer);
1755 void avcodec_default_free_buffers(AVCodecContext *avctx)
1757 switch (avctx->codec_type) {
1758 case AVMEDIA_TYPE_VIDEO:
1759 video_free_buffers(avctx);
1761 case AVMEDIA_TYPE_AUDIO:
1762 audio_free_buffers(avctx);
1769 #if FF_API_OLD_FF_PICT_TYPES
1770 char av_get_pict_type_char(int pict_type){
1771 return av_get_picture_type_char(pict_type);
1775 int av_get_bits_per_sample(enum CodecID codec_id){
1777 case CODEC_ID_ADPCM_SBPRO_2:
1779 case CODEC_ID_ADPCM_SBPRO_3:
1781 case CODEC_ID_ADPCM_SBPRO_4:
1782 case CODEC_ID_ADPCM_CT:
1783 case CODEC_ID_ADPCM_IMA_WAV:
1784 case CODEC_ID_ADPCM_IMA_QT:
1785 case CODEC_ID_ADPCM_SWF:
1786 case CODEC_ID_ADPCM_MS:
1787 case CODEC_ID_ADPCM_YAMAHA:
1788 case CODEC_ID_ADPCM_G722:
1790 case CODEC_ID_PCM_ALAW:
1791 case CODEC_ID_PCM_MULAW:
1792 case CODEC_ID_PCM_S8:
1793 case CODEC_ID_PCM_U8:
1794 case CODEC_ID_PCM_ZORK:
1796 case CODEC_ID_PCM_S16BE:
1797 case CODEC_ID_PCM_S16LE:
1798 case CODEC_ID_PCM_S16LE_PLANAR:
1799 case CODEC_ID_PCM_U16BE:
1800 case CODEC_ID_PCM_U16LE:
1802 case CODEC_ID_PCM_S24DAUD:
1803 case CODEC_ID_PCM_S24BE:
1804 case CODEC_ID_PCM_S24LE:
1805 case CODEC_ID_PCM_U24BE:
1806 case CODEC_ID_PCM_U24LE:
1808 case CODEC_ID_PCM_S32BE:
1809 case CODEC_ID_PCM_S32LE:
1810 case CODEC_ID_PCM_U32BE:
1811 case CODEC_ID_PCM_U32LE:
1812 case CODEC_ID_PCM_F32BE:
1813 case CODEC_ID_PCM_F32LE:
1815 case CODEC_ID_PCM_F64BE:
1816 case CODEC_ID_PCM_F64LE:
1823 #if FF_API_OLD_SAMPLE_FMT
1824 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1825 return av_get_bytes_per_sample(sample_fmt) << 3;
1830 int ff_thread_init(AVCodecContext *s){
1835 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1849 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1851 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1855 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1857 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1858 "version to the newest one from Git. If the problem still "
1859 "occurs, it means that your file has a feature which has not "
1860 "been implemented.\n", feature);
1862 av_log_ask_for_sample(avc, NULL);
1865 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1867 va_list argument_list;
1869 va_start(argument_list, msg);
1872 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1873 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1874 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1875 "and contact the ffmpeg-devel mailing list.\n");
1877 va_end(argument_list);
1880 static AVHWAccel *first_hwaccel = NULL;
1882 void av_register_hwaccel(AVHWAccel *hwaccel)
1884 AVHWAccel **p = &first_hwaccel;
1888 hwaccel->next = NULL;
1891 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1893 return hwaccel ? hwaccel->next : first_hwaccel;
1896 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1898 AVHWAccel *hwaccel=NULL;
1900 while((hwaccel= av_hwaccel_next(hwaccel))){
1901 if ( hwaccel->id == codec_id
1902 && hwaccel->pix_fmt == pix_fmt)
1908 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1910 if (ff_lockmgr_cb) {
1911 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1913 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1919 if (ff_lockmgr_cb) {
1920 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1922 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1928 int avpriv_lock_avformat(void)
1930 if (ff_lockmgr_cb) {
1931 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1937 int avpriv_unlock_avformat(void)
1939 if (ff_lockmgr_cb) {
1940 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1946 unsigned int avpriv_toupper4(unsigned int x)
1948 return toupper( x &0xFF)
1949 + (toupper((x>>8 )&0xFF)<<8 )
1950 + (toupper((x>>16)&0xFF)<<16)
1951 + (toupper((x>>24)&0xFF)<<24);
1956 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1960 ff_init_buffer_info(avctx, f);
1962 return avctx->get_buffer(avctx, f);
1965 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1967 f->owner->release_buffer(f->owner, f);
1970 void ff_thread_finish_setup(AVCodecContext *avctx)
1974 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1978 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1984 #if FF_API_THREAD_INIT
1985 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1987 s->thread_count = thread_count;
1988 return ff_thread_init(s);
1992 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1994 AVCodec *c= avcodec_find_decoder(codec_id);
1996 c= avcodec_find_encoder(codec_id);
2000 if (codec_id <= CODEC_ID_NONE)
2001 return AVMEDIA_TYPE_UNKNOWN;
2002 else if (codec_id < CODEC_ID_FIRST_AUDIO)
2003 return AVMEDIA_TYPE_VIDEO;
2004 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2005 return AVMEDIA_TYPE_AUDIO;
2006 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2007 return AVMEDIA_TYPE_SUBTITLE;
2009 return AVMEDIA_TYPE_UNKNOWN;