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 dsputil_static_init();
121 static av_always_inline int codec_is_encoder(AVCodec *codec)
123 return codec && (codec->encode || codec->encode2);
126 static av_always_inline int 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_YUV420P9LE:
182 case PIX_FMT_YUV420P9BE:
183 case PIX_FMT_YUV420P10LE:
184 case PIX_FMT_YUV420P10BE:
185 case PIX_FMT_YUV422P9LE:
186 case PIX_FMT_YUV422P9BE:
187 case PIX_FMT_YUV422P10LE:
188 case PIX_FMT_YUV422P10BE:
189 case PIX_FMT_YUV444P9LE:
190 case PIX_FMT_YUV444P9BE:
191 case PIX_FMT_YUV444P10LE:
192 case PIX_FMT_YUV444P10BE:
193 case PIX_FMT_GBRP9LE:
194 case PIX_FMT_GBRP9BE:
195 case PIX_FMT_GBRP10LE:
196 case PIX_FMT_GBRP10BE:
197 w_align = 16; //FIXME assume 16 pixel per macroblock
198 h_align = 16 * 2; // interlaced needs 2 macroblocks height
200 case PIX_FMT_YUV411P:
201 case PIX_FMT_UYYVYY411:
205 case PIX_FMT_YUV410P:
206 if(s->codec_id == CODEC_ID_SVQ1){
211 if(s->codec_id == CODEC_ID_RPZA){
218 if(s->codec_id == CODEC_ID_SMC){
224 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
235 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
236 w_align= FFMAX(w_align, 8);
239 *width = FFALIGN(*width , w_align);
240 *height= FFALIGN(*height, h_align);
241 if(s->codec_id == CODEC_ID_H264 || s->lowres)
242 *height+=2; // some of the optimized chroma MC reads one line too much
243 // which is also done in mpeg decoders with lowres > 0
245 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
246 linesize_align[i] = STRIDE_ALIGN;
247 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
248 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
249 //picture size unneccessarily in some cases. The solution here is not
250 //pretty and better ideas are welcome!
252 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
253 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
254 s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
255 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
256 linesize_align[i] = 16;
261 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
262 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
263 int linesize_align[AV_NUM_DATA_POINTERS];
265 avcodec_align_dimensions2(s, width, height, linesize_align);
266 align = FFMAX(linesize_align[0], linesize_align[3]);
267 linesize_align[1] <<= chroma_shift;
268 linesize_align[2] <<= chroma_shift;
269 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
270 *width=FFALIGN(*width, align);
273 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
276 pic->pkt_pts = s->pkt->pts;
277 pic->pkt_pos = s->pkt->pos;
279 pic->pkt_pts = AV_NOPTS_VALUE;
282 pic->reordered_opaque= s->reordered_opaque;
283 pic->sample_aspect_ratio = s->sample_aspect_ratio;
284 pic->width = s->width;
285 pic->height = s->height;
286 pic->format = s->pix_fmt;
289 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
290 enum AVSampleFormat sample_fmt, const uint8_t *buf,
291 int buf_size, int align)
293 int ch, planar, needed_size, ret = 0;
295 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
296 frame->nb_samples, sample_fmt,
298 if (buf_size < needed_size)
299 return AVERROR(EINVAL);
301 planar = av_sample_fmt_is_planar(sample_fmt);
302 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
303 if (!(frame->extended_data = av_mallocz(nb_channels *
304 sizeof(*frame->extended_data))))
305 return AVERROR(ENOMEM);
307 frame->extended_data = frame->data;
310 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
311 buf, nb_channels, frame->nb_samples,
312 sample_fmt, align)) < 0) {
313 if (frame->extended_data != frame->data)
314 av_freep(&frame->extended_data);
317 if (frame->extended_data != frame->data) {
318 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
319 frame->data[ch] = frame->extended_data[ch];
325 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
327 AVCodecInternal *avci = avctx->internal;
331 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
332 frame->nb_samples, avctx->sample_fmt,
335 return AVERROR(EINVAL);
337 /* allocate InternalBuffer if needed */
339 avci->buffer = av_mallocz(sizeof(InternalBuffer));
341 return AVERROR(ENOMEM);
345 /* if there is a previously-used internal buffer, check its size and
346 channel count to see if we can reuse it */
347 if (buf->extended_data) {
348 /* if current buffer is too small, free it */
349 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
350 av_free(buf->extended_data[0]);
351 if (buf->extended_data != buf->data)
352 av_freep(&buf->extended_data);
353 buf->extended_data = NULL;
356 /* if number of channels has changed, reset and/or free extended data
357 pointers but leave data buffer in buf->data[0] for reuse */
358 if (buf->nb_channels != avctx->channels) {
359 if (buf->extended_data != buf->data)
360 av_free(buf->extended_data);
361 buf->extended_data = NULL;
365 /* if there is no previous buffer or the previous buffer cannot be used
366 as-is, allocate a new buffer and/or rearrange the channel pointers */
367 if (!buf->extended_data) {
369 if (!(buf->data[0] = av_mallocz(buf_size)))
370 return AVERROR(ENOMEM);
371 buf->audio_data_size = buf_size;
373 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
374 avctx->sample_fmt, buf->data[0],
375 buf->audio_data_size, 32)))
378 if (frame->extended_data == frame->data)
379 buf->extended_data = buf->data;
381 buf->extended_data = frame->extended_data;
382 memcpy(buf->data, frame->data, sizeof(frame->data));
383 buf->linesize[0] = frame->linesize[0];
384 buf->nb_channels = avctx->channels;
386 /* copy InternalBuffer info to the AVFrame */
387 frame->extended_data = buf->extended_data;
388 frame->linesize[0] = buf->linesize[0];
389 memcpy(frame->data, buf->data, sizeof(frame->data));
392 frame->type = FF_BUFFER_TYPE_INTERNAL;
395 frame->pkt_pts = avctx->pkt->pts;
396 frame->pkt_pos = avctx->pkt->pos;
398 frame->pkt_pts = AV_NOPTS_VALUE;
402 frame->reordered_opaque = avctx->reordered_opaque;
404 if (avctx->debug & FF_DEBUG_BUFFERS)
405 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
406 "internal audio buffer used\n", frame);
411 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
417 AVCodecInternal *avci = s->internal;
419 if(pic->data[0]!=NULL) {
420 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
423 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
424 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
428 if(av_image_check_size(w, h, 0, s))
432 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
433 sizeof(InternalBuffer));
436 buf = &avci->buffer[avci->buffer_count];
438 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
439 if(s->active_thread_type&FF_THREAD_FRAME) {
440 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
444 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
445 av_freep(&buf->base[i]);
451 int h_chroma_shift, v_chroma_shift;
456 int stride_align[AV_NUM_DATA_POINTERS];
457 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
459 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
461 avcodec_align_dimensions2(s, &w, &h, stride_align);
463 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
469 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
470 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
471 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
472 // increase alignment of w for next try (rhs gives the lowest bit set in w)
477 unaligned |= picture.linesize[i] % stride_align[i];
481 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
485 for (i=0; i<3 && picture.data[i+1]; i++)
486 size[i] = picture.data[i+1] - picture.data[i];
487 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
489 memset(buf->base, 0, sizeof(buf->base));
490 memset(buf->data, 0, sizeof(buf->data));
492 for(i=0; i<4 && size[i]; i++){
493 const int h_shift= i==0 ? 0 : h_chroma_shift;
494 const int v_shift= i==0 ? 0 : v_chroma_shift;
496 buf->linesize[i]= picture.linesize[i];
498 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
499 if(buf->base[i]==NULL) return -1;
500 memset(buf->base[i], 128, size[i]);
502 // no edge if EDGE EMU or not planar YUV
503 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
504 buf->data[i] = buf->base[i];
506 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
508 for (; i < AV_NUM_DATA_POINTERS; i++) {
509 buf->base[i] = buf->data[i] = NULL;
510 buf->linesize[i] = 0;
512 if(size[1] && !size[2])
513 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
514 buf->width = s->width;
515 buf->height = s->height;
516 buf->pix_fmt= s->pix_fmt;
518 pic->type= FF_BUFFER_TYPE_INTERNAL;
520 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
521 pic->base[i]= buf->base[i];
522 pic->data[i]= buf->data[i];
523 pic->linesize[i]= buf->linesize[i];
525 pic->extended_data = pic->data;
526 avci->buffer_count++;
529 pic->pkt_pts = s->pkt->pts;
530 pic->pkt_pos = s->pkt->pos;
532 pic->pkt_pts = AV_NOPTS_VALUE;
535 pic->reordered_opaque= s->reordered_opaque;
536 pic->sample_aspect_ratio = s->sample_aspect_ratio;
537 pic->width = s->width;
538 pic->height = s->height;
539 pic->format = s->pix_fmt;
541 if(s->debug&FF_DEBUG_BUFFERS)
542 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
543 "buffers used\n", pic, avci->buffer_count);
548 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
550 switch (avctx->codec_type) {
551 case AVMEDIA_TYPE_VIDEO:
552 return video_get_buffer(avctx, frame);
553 case AVMEDIA_TYPE_AUDIO:
554 return audio_get_buffer(avctx, frame);
560 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
562 InternalBuffer *buf, *last;
563 AVCodecInternal *avci = s->internal;
565 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
567 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
568 assert(avci->buffer_count);
571 buf = NULL; /* avoids warning */
572 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
573 buf = &avci->buffer[i];
574 if (buf->data[0] == pic->data[0])
577 assert(i < avci->buffer_count);
578 avci->buffer_count--;
579 last = &avci->buffer[avci->buffer_count];
582 FFSWAP(InternalBuffer, *buf, *last);
585 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
587 // pic->base[i]=NULL;
589 //printf("R%X\n", pic->opaque);
591 if(s->debug&FF_DEBUG_BUFFERS)
592 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
593 "buffers used\n", pic, avci->buffer_count);
596 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
600 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
602 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
603 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
604 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
605 s->release_buffer(s, pic);
608 ff_init_buffer_info(s, pic);
610 /* If no picture return a new buffer */
611 if(pic->data[0] == NULL) {
612 /* We will copy from buffer, so must be readable */
613 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
614 return s->get_buffer(s, pic);
617 /* If internal buffer type return the same buffer */
618 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
623 * Not internal type and reget_buffer not overridden, emulate cr buffer
626 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
627 pic->data[i] = pic->base[i] = NULL;
629 /* Allocate new frame */
630 if (s->get_buffer(s, pic))
632 /* Copy image data from old buffer to new buffer */
633 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
635 s->release_buffer(s, &temp_pic); // Release old frame
639 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
642 for(i=0; i<count; i++){
643 int r= func(c, (char*)arg + i*size);
649 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
652 for(i=0; i<count; i++){
653 int r= func(c, arg, i, 0);
659 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
660 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
665 void avcodec_get_frame_defaults(AVFrame *pic){
666 memset(pic, 0, sizeof(AVFrame));
668 pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
671 pic->sample_aspect_ratio = (AVRational){0, 1};
672 pic->format = -1; /* unknown */
675 AVFrame *avcodec_alloc_frame(void){
676 AVFrame *pic= av_malloc(sizeof(AVFrame));
678 if(pic==NULL) return NULL;
680 avcodec_get_frame_defaults(pic);
685 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
687 memset(sub, 0, sizeof(*sub));
688 sub->pts = AV_NOPTS_VALUE;
691 #if FF_API_AVCODEC_OPEN
692 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
694 return avcodec_open2(avctx, codec, NULL);
698 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
701 AVDictionary *tmp = NULL;
703 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
704 return AVERROR(EINVAL);
707 av_dict_copy(&tmp, *options, 0);
709 /* If there is a user-supplied mutex locking routine, call it. */
711 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
715 entangled_thread_counter++;
716 if(entangled_thread_counter != 1){
717 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
722 if(avctx->codec || !codec) {
723 ret = AVERROR(EINVAL);
727 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
728 if (!avctx->internal) {
729 ret = AVERROR(ENOMEM);
733 if (codec->priv_data_size > 0) {
734 if(!avctx->priv_data){
735 avctx->priv_data = av_mallocz(codec->priv_data_size);
736 if (!avctx->priv_data) {
737 ret = AVERROR(ENOMEM);
740 if (codec->priv_class) {
741 *(AVClass**)avctx->priv_data= codec->priv_class;
742 av_opt_set_defaults(avctx->priv_data);
745 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
748 avctx->priv_data = NULL;
750 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
753 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
754 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
755 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
760 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
761 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
762 if(avctx->coded_width && avctx->coded_height)
763 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
764 else if(avctx->width && avctx->height)
765 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
768 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
769 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
770 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
771 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
772 avcodec_set_dimensions(avctx, 0, 0);
775 /* if the decoder init function was already called previously,
776 free the already allocated subtitle_header before overwriting it */
777 if (codec_is_decoder(codec))
778 av_freep(&avctx->subtitle_header);
780 #define SANE_NB_CHANNELS 128U
781 if (avctx->channels > SANE_NB_CHANNELS) {
782 ret = AVERROR(EINVAL);
786 avctx->codec = codec;
787 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
788 avctx->codec_id == CODEC_ID_NONE) {
789 avctx->codec_type = codec->type;
790 avctx->codec_id = codec->id;
792 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
793 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
794 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
795 ret = AVERROR(EINVAL);
798 avctx->frame_number = 0;
800 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
801 (!avctx->time_base.num || !avctx->time_base.den)) {
802 avctx->time_base.num = 1;
803 avctx->time_base.den = avctx->sample_rate;
807 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
809 if (HAVE_THREADS && !avctx->thread_opaque) {
810 ret = ff_thread_init(avctx);
815 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
816 avctx->thread_count = 1;
818 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
819 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
820 avctx->codec->max_lowres);
821 ret = AVERROR(EINVAL);
824 if (codec_is_encoder(avctx->codec)) {
826 if (avctx->codec->sample_fmts) {
827 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
828 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
830 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
831 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
832 ret = AVERROR(EINVAL);
836 if (avctx->codec->supported_samplerates) {
837 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
838 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
840 if (avctx->codec->supported_samplerates[i] == 0) {
841 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
842 ret = AVERROR(EINVAL);
846 if (avctx->codec->channel_layouts) {
847 if (!avctx->channel_layout) {
848 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
850 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
851 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
853 if (avctx->codec->channel_layouts[i] == 0) {
854 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
855 ret = AVERROR(EINVAL);
860 if (avctx->channel_layout && avctx->channels) {
861 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
862 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
863 ret = AVERROR(EINVAL);
866 } else if (avctx->channel_layout) {
867 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
871 avctx->pts_correction_num_faulty_pts =
872 avctx->pts_correction_num_faulty_dts = 0;
873 avctx->pts_correction_last_pts =
874 avctx->pts_correction_last_dts = INT64_MIN;
876 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
877 ret = avctx->codec->init(avctx);
885 entangled_thread_counter--;
887 /* Release any user-supplied mutex. */
889 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
892 av_dict_free(options);
899 av_freep(&avctx->priv_data);
900 av_freep(&avctx->internal);
905 int ff_alloc_packet(AVPacket *avpkt, int size)
907 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
908 return AVERROR(EINVAL);
914 if (avpkt->size < size)
915 return AVERROR(EINVAL);
917 pkt_data = avpkt->data;
918 pkt_size = avpkt->size;
919 av_init_packet(avpkt);
920 avpkt->data = pkt_data;
921 avpkt->size = pkt_size;
924 return av_new_packet(avpkt, size);
928 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
930 const AVFrame *frame,
934 int user_packet = !!avpkt->data;
937 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
938 av_init_packet(avpkt);
943 /* check for valid frame size */
945 nb_samples = frame->nb_samples;
946 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
947 if (nb_samples > avctx->frame_size)
948 return AVERROR(EINVAL);
949 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
950 if (nb_samples != avctx->frame_size)
951 return AVERROR(EINVAL);
954 nb_samples = avctx->frame_size;
957 if (avctx->codec->encode2) {
959 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
960 if (!ret && *got_packet_ptr &&
961 !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
962 avpkt->pts = frame->pts;
963 avpkt->duration = av_rescale_q(frame->nb_samples,
964 (AVRational){ 1, avctx->sample_rate },
968 /* for compatibility with encoders not supporting encode2(), we need to
969 allocate a packet buffer if the user has not provided one or check
970 the size otherwise */
972 int buf_size = avpkt->size;
974 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
975 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
977 return AVERROR(EINVAL);
978 buf_size = nb_samples * avctx->channels *
979 av_get_bits_per_sample(avctx->codec_id) / 8;
981 /* this is a guess as to the required size.
982 if an encoder needs more than this, it should probably
983 implement encode2() */
984 buf_size = 2 * avctx->frame_size * avctx->channels *
985 av_get_bytes_per_sample(avctx->sample_fmt);
986 buf_size += FF_MIN_BUFFER_SIZE;
989 if ((ret = ff_alloc_packet(avpkt, buf_size)))
992 /* Encoders using AVCodec.encode() that support
993 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
994 the smaller size when encoding the last frame.
995 This code can be removed once all encoders supporting
996 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
997 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
998 nb_samples < avctx->frame_size) {
999 fs_tmp = avctx->frame_size;
1000 avctx->frame_size = nb_samples;
1003 /* encode the frame */
1004 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
1005 frame ? frame->data[0] : NULL);
1008 /* no output. if the packet data was allocated by libavcodec,
1011 av_freep(&avpkt->data);
1013 if (avctx->coded_frame)
1014 avpkt->pts = avctx->coded_frame->pts;
1015 /* Set duration for final small packet. This can be removed
1016 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1019 avpkt->duration = av_rescale_q(avctx->frame_size,
1020 (AVRational){ 1, avctx->sample_rate },
1025 *got_packet_ptr = (ret > 0);
1030 avctx->frame_size = fs_tmp;
1033 avctx->frame_number++;
1035 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1036 this needs to be moved to the encoders, but for now we can do it
1037 here to simplify things */
1038 avpkt->flags |= AV_PKT_FLAG_KEY;
1043 #if FF_API_OLD_DECODE_AUDIO
1044 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1045 uint8_t *buf, int buf_size,
1046 const short *samples)
1051 int ret, samples_size, got_packet;
1053 av_init_packet(&pkt);
1055 pkt.size = buf_size;
1059 avcodec_get_frame_defaults(frame);
1061 if (avctx->frame_size) {
1062 frame->nb_samples = avctx->frame_size;
1064 /* if frame_size is not set, the number of samples must be
1065 calculated from the buffer size */
1067 if (!av_get_bits_per_sample(avctx->codec_id)) {
1068 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1069 "support this codec\n");
1070 return AVERROR(EINVAL);
1072 nb_samples = (int64_t)buf_size * 8 /
1073 (av_get_bits_per_sample(avctx->codec_id) *
1075 if (nb_samples >= INT_MAX)
1076 return AVERROR(EINVAL);
1077 frame->nb_samples = nb_samples;
1080 /* it is assumed that the samples buffer is large enough based on the
1081 relevant parameters */
1082 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1084 avctx->sample_fmt, 1);
1085 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1087 samples, samples_size, 1)))
1090 /* fabricate frame pts from sample count.
1091 this is needed because the avcodec_encode_audio() API does not have
1092 a way for the user to provide pts */
1093 if(avctx->sample_rate && avctx->time_base.num)
1094 frame->pts = av_rescale_q(avctx->internal->sample_count,
1095 (AVRational){ 1, avctx->sample_rate },
1098 frame->pts = AV_NOPTS_VALUE;
1099 avctx->internal->sample_count += frame->nb_samples;
1105 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1106 if (!ret && got_packet && avctx->coded_frame) {
1107 avctx->coded_frame->pts = pkt.pts;
1108 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1110 /* free any side data since we cannot return it */
1111 ff_packet_free_side_data(&pkt);
1113 if (frame && frame->extended_data != frame->data)
1114 av_freep(&frame->extended_data);
1116 return ret ? ret : pkt.size;
1120 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1121 const AVFrame *pict)
1123 if(buf_size < FF_MIN_BUFFER_SIZE){
1124 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1127 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
1129 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
1130 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
1131 avctx->frame_number++;
1132 emms_c(); //needed to avoid an emms_c() call before every return;
1139 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1140 const AVSubtitle *sub)
1143 if(sub->start_display_time) {
1144 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1148 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1149 avctx->frame_number++;
1154 * Attempt to guess proper monotonic timestamps for decoded video frames
1155 * which might have incorrect times. Input timestamps may wrap around, in
1156 * which case the output will as well.
1158 * @param pts the pts field of the decoded AVPacket, as passed through
1160 * @param dts the dts field of the decoded AVPacket
1161 * @return one of the input values, may be AV_NOPTS_VALUE
1163 static int64_t guess_correct_pts(AVCodecContext *ctx,
1164 int64_t reordered_pts, int64_t dts)
1166 int64_t pts = AV_NOPTS_VALUE;
1168 if (dts != AV_NOPTS_VALUE) {
1169 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1170 ctx->pts_correction_last_dts = dts;
1172 if (reordered_pts != AV_NOPTS_VALUE) {
1173 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1174 ctx->pts_correction_last_pts = reordered_pts;
1176 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1177 && reordered_pts != AV_NOPTS_VALUE)
1178 pts = reordered_pts;
1185 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1188 const uint8_t *data;
1191 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1194 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1195 if (!data || size < 4)
1197 flags = bytestream_get_le32(&data);
1199 if (size < 4) /* Required for any of the changes */
1201 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1202 avctx->channels = bytestream_get_le32(&data);
1205 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1208 avctx->channel_layout = bytestream_get_le64(&data);
1213 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1214 avctx->sample_rate = bytestream_get_le32(&data);
1217 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1220 avctx->width = bytestream_get_le32(&data);
1221 avctx->height = bytestream_get_le32(&data);
1222 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1227 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1228 int *got_picture_ptr,
1229 const AVPacket *avpkt)
1232 // copy to ensure we do not change avpkt
1233 AVPacket tmp = *avpkt;
1235 *got_picture_ptr= 0;
1236 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1239 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1240 int did_split = av_packet_split_side_data(&tmp);
1241 apply_param_change(avctx, &tmp);
1243 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1244 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1247 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1249 picture->pkt_dts= avpkt->dts;
1251 if(!avctx->has_b_frames){
1252 picture->pkt_pos= avpkt->pos;
1254 //FIXME these should be under if(!avctx->has_b_frames)
1255 if (!picture->sample_aspect_ratio.num)
1256 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1257 if (!picture->width)
1258 picture->width = avctx->width;
1259 if (!picture->height)
1260 picture->height = avctx->height;
1261 if (picture->format == PIX_FMT_NONE)
1262 picture->format = avctx->pix_fmt;
1265 emms_c(); //needed to avoid an emms_c() call before every return;
1269 ff_packet_free_side_data(&tmp);
1271 if (*got_picture_ptr){
1272 avctx->frame_number++;
1273 picture->best_effort_timestamp = guess_correct_pts(avctx,
1283 #if FF_API_OLD_DECODE_AUDIO
1284 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1285 int *frame_size_ptr,
1289 int ret, got_frame = 0;
1291 if (avctx->get_buffer != avcodec_default_get_buffer) {
1292 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1293 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1294 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1295 "avcodec_decode_audio4()\n");
1296 avctx->get_buffer = avcodec_default_get_buffer;
1297 avctx->release_buffer = avcodec_default_release_buffer;
1300 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1302 if (ret >= 0 && got_frame) {
1304 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1305 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1307 avctx->sample_fmt, 1);
1308 if (*frame_size_ptr < data_size) {
1309 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1310 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1311 return AVERROR(EINVAL);
1314 memcpy(samples, frame.extended_data[0], plane_size);
1316 if (planar && avctx->channels > 1) {
1317 uint8_t *out = ((uint8_t *)samples) + plane_size;
1318 for (ch = 1; ch < avctx->channels; ch++) {
1319 memcpy(out, frame.extended_data[ch], plane_size);
1323 *frame_size_ptr = data_size;
1325 *frame_size_ptr = 0;
1331 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1340 if (!avpkt->data && avpkt->size) {
1341 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1342 return AVERROR(EINVAL);
1345 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1346 av_packet_split_side_data(avpkt);
1347 apply_param_change(avctx, avpkt);
1350 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1351 if (ret >= 0 && *got_frame_ptr) {
1352 avctx->frame_number++;
1353 frame->pkt_dts = avpkt->dts;
1354 if (frame->format == AV_SAMPLE_FMT_NONE)
1355 frame->format = avctx->sample_fmt;
1361 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1369 avcodec_get_subtitle_defaults(sub);
1370 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1372 avctx->frame_number++;
1376 void avsubtitle_free(AVSubtitle *sub)
1380 for (i = 0; i < sub->num_rects; i++)
1382 av_freep(&sub->rects[i]->pict.data[0]);
1383 av_freep(&sub->rects[i]->pict.data[1]);
1384 av_freep(&sub->rects[i]->pict.data[2]);
1385 av_freep(&sub->rects[i]->pict.data[3]);
1386 av_freep(&sub->rects[i]->text);
1387 av_freep(&sub->rects[i]->ass);
1388 av_freep(&sub->rects[i]);
1391 av_freep(&sub->rects);
1393 memset(sub, 0, sizeof(AVSubtitle));
1396 av_cold int avcodec_close(AVCodecContext *avctx)
1398 /* If there is a user-supplied mutex locking routine, call it. */
1399 if (ff_lockmgr_cb) {
1400 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1404 entangled_thread_counter++;
1405 if(entangled_thread_counter != 1){
1406 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1407 entangled_thread_counter--;
1411 if (HAVE_THREADS && avctx->thread_opaque)
1412 ff_thread_free(avctx);
1413 if (avctx->codec && avctx->codec->close)
1414 avctx->codec->close(avctx);
1415 avcodec_default_free_buffers(avctx);
1416 avctx->coded_frame = NULL;
1417 av_freep(&avctx->internal);
1418 if (avctx->codec && avctx->codec->priv_class)
1419 av_opt_free(avctx->priv_data);
1421 av_freep(&avctx->priv_data);
1422 if (codec_is_encoder(avctx->codec))
1423 av_freep(&avctx->extradata);
1424 avctx->codec = NULL;
1425 avctx->active_thread_type = 0;
1426 entangled_thread_counter--;
1428 /* Release any user-supplied mutex. */
1429 if (ff_lockmgr_cb) {
1430 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1435 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1438 case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
1439 case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
1440 case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1441 default : return id;
1445 AVCodec *avcodec_find_encoder(enum CodecID id)
1447 AVCodec *p, *experimental=NULL;
1449 id= remap_deprecated_codec_id(id);
1451 if (codec_is_encoder(p) && p->id == id) {
1452 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1459 return experimental;
1462 AVCodec *avcodec_find_encoder_by_name(const char *name)
1469 if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
1476 AVCodec *avcodec_find_decoder(enum CodecID id)
1478 AVCodec *p, *experimental=NULL;
1480 id= remap_deprecated_codec_id(id);
1482 if (codec_is_decoder(p) && p->id == id) {
1483 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1490 return experimental;
1493 AVCodec *avcodec_find_decoder_by_name(const char *name)
1500 if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
1507 static int get_bit_rate(AVCodecContext *ctx)
1510 int bits_per_sample;
1512 switch(ctx->codec_type) {
1513 case AVMEDIA_TYPE_VIDEO:
1514 case AVMEDIA_TYPE_DATA:
1515 case AVMEDIA_TYPE_SUBTITLE:
1516 case AVMEDIA_TYPE_ATTACHMENT:
1517 bit_rate = ctx->bit_rate;
1519 case AVMEDIA_TYPE_AUDIO:
1520 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1521 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1530 const char *avcodec_get_name(enum CodecID id)
1536 #include "libavcodec/codec_names.h"
1538 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1540 codec = avcodec_find_decoder(id);
1543 codec = avcodec_find_encoder(id);
1546 return "unknown_codec";
1549 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1551 int i, len, ret = 0;
1553 for (i = 0; i < 4; i++) {
1554 len = snprintf(buf, buf_size,
1555 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1557 buf_size = buf_size > len ? buf_size - len : 0;
1564 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1566 const char *codec_type;
1567 const char *codec_name;
1568 const char *profile = NULL;
1571 AVRational display_aspect_ratio;
1573 if (!buf || buf_size <= 0)
1575 codec_type = av_get_media_type_string(enc->codec_type);
1576 codec_name = avcodec_get_name(enc->codec_id);
1577 if (enc->profile != FF_PROFILE_UNKNOWN) {
1578 p = encode ? avcodec_find_encoder(enc->codec_id) :
1579 avcodec_find_decoder(enc->codec_id);
1581 profile = av_get_profile_name(p, enc->profile);
1584 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1585 codec_name, enc->mb_decision ? " (hq)" : "");
1586 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1588 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1589 if (enc->codec_tag) {
1591 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1592 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1593 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1595 switch(enc->codec_type) {
1596 case AVMEDIA_TYPE_VIDEO:
1597 if (enc->pix_fmt != PIX_FMT_NONE) {
1598 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1600 av_get_pix_fmt_name(enc->pix_fmt));
1603 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1605 enc->width, enc->height);
1606 if (enc->sample_aspect_ratio.num) {
1607 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1608 enc->width*enc->sample_aspect_ratio.num,
1609 enc->height*enc->sample_aspect_ratio.den,
1611 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1612 " [SAR %d:%d DAR %d:%d]",
1613 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1614 display_aspect_ratio.num, display_aspect_ratio.den);
1616 if(av_log_get_level() >= AV_LOG_DEBUG){
1617 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1618 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1620 enc->time_base.num/g, enc->time_base.den/g);
1624 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1625 ", q=%d-%d", enc->qmin, enc->qmax);
1628 case AVMEDIA_TYPE_AUDIO:
1629 if (enc->sample_rate) {
1630 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1631 ", %d Hz", enc->sample_rate);
1633 av_strlcat(buf, ", ", buf_size);
1634 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1635 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1636 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1637 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1644 if (enc->flags & CODEC_FLAG_PASS1)
1645 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1647 if (enc->flags & CODEC_FLAG_PASS2)
1648 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1651 bitrate = get_bit_rate(enc);
1653 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1654 ", %d kb/s", bitrate / 1000);
1658 const char *av_get_profile_name(const AVCodec *codec, int profile)
1661 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1664 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1665 if (p->profile == profile)
1671 unsigned avcodec_version( void )
1673 // av_assert0(CODEC_ID_V410==164);
1674 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1675 av_assert0(CODEC_ID_ADPCM_G722==69660);
1676 // av_assert0(CODEC_ID_BMV_AUDIO==86071);
1677 av_assert0(CODEC_ID_SRT==94216);
1678 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1680 return LIBAVCODEC_VERSION_INT;
1683 const char *avcodec_configuration(void)
1685 return FFMPEG_CONFIGURATION;
1688 const char *avcodec_license(void)
1690 #define LICENSE_PREFIX "libavcodec license: "
1691 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1694 void avcodec_flush_buffers(AVCodecContext *avctx)
1696 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1697 ff_thread_flush(avctx);
1698 else if(avctx->codec->flush)
1699 avctx->codec->flush(avctx);
1702 static void video_free_buffers(AVCodecContext *s)
1704 AVCodecInternal *avci = s->internal;
1710 if (avci->buffer_count)
1711 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1712 avci->buffer_count);
1713 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1714 InternalBuffer *buf = &avci->buffer[i];
1716 av_freep(&buf->base[j]);
1720 av_freep(&avci->buffer);
1722 avci->buffer_count=0;
1725 static void audio_free_buffers(AVCodecContext *avctx)
1727 AVCodecInternal *avci = avctx->internal;
1728 InternalBuffer *buf;
1734 if (buf->extended_data) {
1735 av_free(buf->extended_data[0]);
1736 if (buf->extended_data != buf->data)
1737 av_freep(&buf->extended_data);
1739 av_freep(&avci->buffer);
1742 void avcodec_default_free_buffers(AVCodecContext *avctx)
1744 switch (avctx->codec_type) {
1745 case AVMEDIA_TYPE_VIDEO:
1746 video_free_buffers(avctx);
1748 case AVMEDIA_TYPE_AUDIO:
1749 audio_free_buffers(avctx);
1756 int av_get_bits_per_sample(enum CodecID codec_id){
1758 case CODEC_ID_ADPCM_SBPRO_2:
1760 case CODEC_ID_ADPCM_SBPRO_3:
1762 case CODEC_ID_ADPCM_SBPRO_4:
1763 case CODEC_ID_ADPCM_CT:
1764 case CODEC_ID_ADPCM_IMA_APC:
1765 case CODEC_ID_ADPCM_IMA_WAV:
1766 case CODEC_ID_ADPCM_IMA_QT:
1767 case CODEC_ID_ADPCM_SWF:
1768 case CODEC_ID_ADPCM_MS:
1769 case CODEC_ID_ADPCM_YAMAHA:
1770 case CODEC_ID_ADPCM_G722:
1772 case CODEC_ID_PCM_ALAW:
1773 case CODEC_ID_PCM_MULAW:
1774 case CODEC_ID_PCM_S8:
1775 case CODEC_ID_PCM_U8:
1776 case CODEC_ID_PCM_ZORK:
1778 case CODEC_ID_PCM_S16BE:
1779 case CODEC_ID_PCM_S16LE:
1780 case CODEC_ID_PCM_S16LE_PLANAR:
1781 case CODEC_ID_PCM_U16BE:
1782 case CODEC_ID_PCM_U16LE:
1784 case CODEC_ID_PCM_S24DAUD:
1785 case CODEC_ID_PCM_S24BE:
1786 case CODEC_ID_PCM_S24LE:
1787 case CODEC_ID_PCM_U24BE:
1788 case CODEC_ID_PCM_U24LE:
1790 case CODEC_ID_PCM_S32BE:
1791 case CODEC_ID_PCM_S32LE:
1792 case CODEC_ID_PCM_U32BE:
1793 case CODEC_ID_PCM_U32LE:
1794 case CODEC_ID_PCM_F32BE:
1795 case CODEC_ID_PCM_F32LE:
1797 case CODEC_ID_PCM_F64BE:
1798 case CODEC_ID_PCM_F64LE:
1806 int ff_thread_init(AVCodecContext *s){
1811 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1825 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1827 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1831 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1833 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1834 "version to the newest one from Git. If the problem still "
1835 "occurs, it means that your file has a feature which has not "
1836 "been implemented.\n", feature);
1838 av_log_ask_for_sample(avc, NULL);
1841 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1843 va_list argument_list;
1845 va_start(argument_list, msg);
1848 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1849 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1850 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1851 "and contact the ffmpeg-devel mailing list.\n");
1853 va_end(argument_list);
1856 static AVHWAccel *first_hwaccel = NULL;
1858 void av_register_hwaccel(AVHWAccel *hwaccel)
1860 AVHWAccel **p = &first_hwaccel;
1864 hwaccel->next = NULL;
1867 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1869 return hwaccel ? hwaccel->next : first_hwaccel;
1872 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1874 AVHWAccel *hwaccel=NULL;
1876 while((hwaccel= av_hwaccel_next(hwaccel))){
1877 if ( hwaccel->id == codec_id
1878 && hwaccel->pix_fmt == pix_fmt)
1884 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1886 if (ff_lockmgr_cb) {
1887 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1889 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1895 if (ff_lockmgr_cb) {
1896 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1898 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1904 int avpriv_lock_avformat(void)
1906 if (ff_lockmgr_cb) {
1907 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1913 int avpriv_unlock_avformat(void)
1915 if (ff_lockmgr_cb) {
1916 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1922 unsigned int avpriv_toupper4(unsigned int x)
1924 return toupper( x &0xFF)
1925 + (toupper((x>>8 )&0xFF)<<8 )
1926 + (toupper((x>>16)&0xFF)<<16)
1927 + (toupper((x>>24)&0xFF)<<24);
1932 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1936 ff_init_buffer_info(avctx, f);
1938 return avctx->get_buffer(avctx, f);
1941 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1943 f->owner->release_buffer(f->owner, f);
1946 void ff_thread_finish_setup(AVCodecContext *avctx)
1950 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1954 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1960 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1962 AVCodec *c= avcodec_find_decoder(codec_id);
1964 c= avcodec_find_encoder(codec_id);
1968 if (codec_id <= CODEC_ID_NONE)
1969 return AVMEDIA_TYPE_UNKNOWN;
1970 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1971 return AVMEDIA_TYPE_VIDEO;
1972 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1973 return AVMEDIA_TYPE_AUDIO;
1974 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1975 return AVMEDIA_TYPE_SUBTITLE;
1977 return AVMEDIA_TYPE_UNKNOWN;