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 < 4; 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 < 4; 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 (avcodec_is_open(avctx))
706 if ((!codec && !avctx->codec)) {
707 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
708 return AVERROR(EINVAL);
710 if ((codec && avctx->codec && codec != avctx->codec)) {
711 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
712 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
713 return AVERROR(EINVAL);
716 codec = avctx->codec;
718 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
719 return AVERROR(EINVAL);
722 av_dict_copy(&tmp, *options, 0);
724 /* If there is a user-supplied mutex locking routine, call it. */
726 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
730 entangled_thread_counter++;
731 if(entangled_thread_counter != 1){
732 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
737 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
738 if (!avctx->internal) {
739 ret = AVERROR(ENOMEM);
743 if (codec->priv_data_size > 0) {
744 if(!avctx->priv_data){
745 avctx->priv_data = av_mallocz(codec->priv_data_size);
746 if (!avctx->priv_data) {
747 ret = AVERROR(ENOMEM);
750 if (codec->priv_class) {
751 *(AVClass**)avctx->priv_data= codec->priv_class;
752 av_opt_set_defaults(avctx->priv_data);
755 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
758 avctx->priv_data = NULL;
760 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
763 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
764 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
765 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
770 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
771 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
772 if(avctx->coded_width && avctx->coded_height)
773 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
774 else if(avctx->width && avctx->height)
775 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
778 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
779 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
780 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
781 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
782 avcodec_set_dimensions(avctx, 0, 0);
785 /* if the decoder init function was already called previously,
786 free the already allocated subtitle_header before overwriting it */
787 if (codec_is_decoder(codec))
788 av_freep(&avctx->subtitle_header);
790 #define SANE_NB_CHANNELS 128U
791 if (avctx->channels > SANE_NB_CHANNELS) {
792 ret = AVERROR(EINVAL);
796 avctx->codec = codec;
797 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
798 avctx->codec_id == CODEC_ID_NONE) {
799 avctx->codec_type = codec->type;
800 avctx->codec_id = codec->id;
802 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
803 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
804 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
805 ret = AVERROR(EINVAL);
808 avctx->frame_number = 0;
810 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
811 (!avctx->time_base.num || !avctx->time_base.den)) {
812 avctx->time_base.num = 1;
813 avctx->time_base.den = avctx->sample_rate;
817 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
819 if (HAVE_THREADS && !avctx->thread_opaque) {
820 ret = ff_thread_init(avctx);
825 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
826 avctx->thread_count = 1;
828 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
829 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
830 avctx->codec->max_lowres);
831 ret = AVERROR(EINVAL);
834 if (codec_is_encoder(avctx->codec)) {
836 if (avctx->codec->sample_fmts) {
837 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
838 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
840 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
841 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
842 ret = AVERROR(EINVAL);
846 if (avctx->codec->supported_samplerates) {
847 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
848 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
850 if (avctx->codec->supported_samplerates[i] == 0) {
851 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
852 ret = AVERROR(EINVAL);
856 if (avctx->codec->channel_layouts) {
857 if (!avctx->channel_layout) {
858 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
860 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
861 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
863 if (avctx->codec->channel_layouts[i] == 0) {
864 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
865 ret = AVERROR(EINVAL);
870 if (avctx->channel_layout && avctx->channels) {
871 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
872 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
873 ret = AVERROR(EINVAL);
876 } else if (avctx->channel_layout) {
877 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
881 avctx->pts_correction_num_faulty_pts =
882 avctx->pts_correction_num_faulty_dts = 0;
883 avctx->pts_correction_last_pts =
884 avctx->pts_correction_last_dts = INT64_MIN;
886 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
887 ret = avctx->codec->init(avctx);
895 entangled_thread_counter--;
897 /* Release any user-supplied mutex. */
899 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
902 av_dict_free(options);
909 av_freep(&avctx->priv_data);
910 av_freep(&avctx->internal);
915 int ff_alloc_packet(AVPacket *avpkt, int size)
917 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
918 return AVERROR(EINVAL);
923 if (avpkt->size < size)
924 return AVERROR(EINVAL);
926 pkt_data = avpkt->data;
927 av_init_packet(avpkt);
928 avpkt->data = pkt_data;
932 return av_new_packet(avpkt, size);
936 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
938 const AVFrame *frame,
942 int user_packet = !!avpkt->data;
945 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
946 av_init_packet(avpkt);
951 /* check for valid frame size */
953 nb_samples = frame->nb_samples;
954 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
955 if (nb_samples > avctx->frame_size)
956 return AVERROR(EINVAL);
957 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
958 if (nb_samples != avctx->frame_size)
959 return AVERROR(EINVAL);
962 nb_samples = avctx->frame_size;
965 if (avctx->codec->encode2) {
967 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
968 if (!ret && *got_packet_ptr &&
969 !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
970 avpkt->pts = frame->pts;
971 avpkt->duration = av_rescale_q(frame->nb_samples,
972 (AVRational){ 1, avctx->sample_rate },
976 /* for compatibility with encoders not supporting encode2(), we need to
977 allocate a packet buffer if the user has not provided one or check
978 the size otherwise */
980 int buf_size = avpkt->size;
982 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
983 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
985 return AVERROR(EINVAL);
986 buf_size = nb_samples * avctx->channels *
987 av_get_bits_per_sample(avctx->codec_id) / 8;
989 /* this is a guess as to the required size.
990 if an encoder needs more than this, it should probably
991 implement encode2() */
992 buf_size = 2 * avctx->frame_size * avctx->channels *
993 av_get_bytes_per_sample(avctx->sample_fmt);
994 buf_size += FF_MIN_BUFFER_SIZE;
997 if ((ret = ff_alloc_packet(avpkt, buf_size)))
1000 /* Encoders using AVCodec.encode() that support
1001 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
1002 the smaller size when encoding the last frame.
1003 This code can be removed once all encoders supporting
1004 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
1005 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
1006 nb_samples < avctx->frame_size) {
1007 fs_tmp = avctx->frame_size;
1008 avctx->frame_size = nb_samples;
1011 /* encode the frame */
1012 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
1013 frame ? frame->data[0] : NULL);
1016 /* no output. if the packet data was allocated by libavcodec,
1019 av_freep(&avpkt->data);
1021 if (avctx->coded_frame)
1022 avpkt->pts = avctx->coded_frame->pts;
1023 /* Set duration for final small packet. This can be removed
1024 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1027 avpkt->duration = av_rescale_q(avctx->frame_size,
1028 (AVRational){ 1, avctx->sample_rate },
1033 *got_packet_ptr = (ret > 0);
1038 avctx->frame_size = fs_tmp;
1041 avctx->frame_number++;
1043 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1044 this needs to be moved to the encoders, but for now we can do it
1045 here to simplify things */
1046 avpkt->flags |= AV_PKT_FLAG_KEY;
1051 #if FF_API_OLD_DECODE_AUDIO
1052 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1053 uint8_t *buf, int buf_size,
1054 const short *samples)
1059 int ret, samples_size, got_packet;
1061 av_init_packet(&pkt);
1063 pkt.size = buf_size;
1067 avcodec_get_frame_defaults(frame);
1069 if (avctx->frame_size) {
1070 frame->nb_samples = avctx->frame_size;
1072 /* if frame_size is not set, the number of samples must be
1073 calculated from the buffer size */
1075 if (!av_get_bits_per_sample(avctx->codec_id)) {
1076 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1077 "support this codec\n");
1078 return AVERROR(EINVAL);
1080 nb_samples = (int64_t)buf_size * 8 /
1081 (av_get_bits_per_sample(avctx->codec_id) *
1083 if (nb_samples >= INT_MAX)
1084 return AVERROR(EINVAL);
1085 frame->nb_samples = nb_samples;
1088 /* it is assumed that the samples buffer is large enough based on the
1089 relevant parameters */
1090 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1092 avctx->sample_fmt, 1);
1093 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1095 samples, samples_size, 1)))
1098 /* fabricate frame pts from sample count.
1099 this is needed because the avcodec_encode_audio() API does not have
1100 a way for the user to provide pts */
1101 if(avctx->sample_rate && avctx->time_base.num)
1102 frame->pts = av_rescale_q(avctx->internal->sample_count,
1103 (AVRational){ 1, avctx->sample_rate },
1106 frame->pts = AV_NOPTS_VALUE;
1107 avctx->internal->sample_count += frame->nb_samples;
1113 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1114 if (!ret && got_packet && avctx->coded_frame) {
1115 avctx->coded_frame->pts = pkt.pts;
1116 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1118 /* free any side data since we cannot return it */
1119 ff_packet_free_side_data(&pkt);
1121 if (frame && frame->extended_data != frame->data)
1122 av_freep(&frame->extended_data);
1124 return ret ? ret : pkt.size;
1128 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1129 const AVFrame *pict)
1131 if(buf_size < FF_MIN_BUFFER_SIZE){
1132 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1135 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
1137 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
1138 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
1139 avctx->frame_number++;
1140 emms_c(); //needed to avoid an emms_c() call before every return;
1147 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1148 const AVSubtitle *sub)
1151 if(sub->start_display_time) {
1152 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1156 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1157 avctx->frame_number++;
1162 * Attempt to guess proper monotonic timestamps for decoded video frames
1163 * which might have incorrect times. Input timestamps may wrap around, in
1164 * which case the output will as well.
1166 * @param pts the pts field of the decoded AVPacket, as passed through
1168 * @param dts the dts field of the decoded AVPacket
1169 * @return one of the input values, may be AV_NOPTS_VALUE
1171 static int64_t guess_correct_pts(AVCodecContext *ctx,
1172 int64_t reordered_pts, int64_t dts)
1174 int64_t pts = AV_NOPTS_VALUE;
1176 if (dts != AV_NOPTS_VALUE) {
1177 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1178 ctx->pts_correction_last_dts = dts;
1180 if (reordered_pts != AV_NOPTS_VALUE) {
1181 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1182 ctx->pts_correction_last_pts = reordered_pts;
1184 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1185 && reordered_pts != AV_NOPTS_VALUE)
1186 pts = reordered_pts;
1193 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1196 const uint8_t *data;
1199 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1202 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1203 if (!data || size < 4)
1205 flags = bytestream_get_le32(&data);
1207 if (size < 4) /* Required for any of the changes */
1209 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1210 avctx->channels = bytestream_get_le32(&data);
1213 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1216 avctx->channel_layout = bytestream_get_le64(&data);
1221 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1222 avctx->sample_rate = bytestream_get_le32(&data);
1225 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1228 avctx->width = bytestream_get_le32(&data);
1229 avctx->height = bytestream_get_le32(&data);
1230 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1235 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1236 int *got_picture_ptr,
1237 const AVPacket *avpkt)
1240 // copy to ensure we do not change avpkt
1241 AVPacket tmp = *avpkt;
1243 *got_picture_ptr= 0;
1244 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1247 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1248 int did_split = av_packet_split_side_data(&tmp);
1249 apply_param_change(avctx, &tmp);
1251 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1252 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1255 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1257 picture->pkt_dts= avpkt->dts;
1259 if(!avctx->has_b_frames){
1260 picture->pkt_pos= avpkt->pos;
1262 //FIXME these should be under if(!avctx->has_b_frames)
1263 if (!picture->sample_aspect_ratio.num)
1264 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1265 if (!picture->width)
1266 picture->width = avctx->width;
1267 if (!picture->height)
1268 picture->height = avctx->height;
1269 if (picture->format == PIX_FMT_NONE)
1270 picture->format = avctx->pix_fmt;
1273 emms_c(); //needed to avoid an emms_c() call before every return;
1277 ff_packet_free_side_data(&tmp);
1279 if (*got_picture_ptr){
1280 avctx->frame_number++;
1281 picture->best_effort_timestamp = guess_correct_pts(avctx,
1291 #if FF_API_OLD_DECODE_AUDIO
1292 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1293 int *frame_size_ptr,
1297 int ret, got_frame = 0;
1299 if (avctx->get_buffer != avcodec_default_get_buffer) {
1300 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1301 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1302 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1303 "avcodec_decode_audio4()\n");
1304 avctx->get_buffer = avcodec_default_get_buffer;
1305 avctx->release_buffer = avcodec_default_release_buffer;
1308 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1310 if (ret >= 0 && got_frame) {
1312 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1313 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1315 avctx->sample_fmt, 1);
1316 if (*frame_size_ptr < data_size) {
1317 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1318 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1319 return AVERROR(EINVAL);
1322 memcpy(samples, frame.extended_data[0], plane_size);
1324 if (planar && avctx->channels > 1) {
1325 uint8_t *out = ((uint8_t *)samples) + plane_size;
1326 for (ch = 1; ch < avctx->channels; ch++) {
1327 memcpy(out, frame.extended_data[ch], plane_size);
1331 *frame_size_ptr = data_size;
1333 *frame_size_ptr = 0;
1339 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1348 if (!avpkt->data && avpkt->size) {
1349 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1350 return AVERROR(EINVAL);
1353 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1354 av_packet_split_side_data(avpkt);
1355 apply_param_change(avctx, avpkt);
1358 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1359 if (ret >= 0 && *got_frame_ptr) {
1360 avctx->frame_number++;
1361 frame->pkt_dts = avpkt->dts;
1362 if (frame->format == AV_SAMPLE_FMT_NONE)
1363 frame->format = avctx->sample_fmt;
1369 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1377 avcodec_get_subtitle_defaults(sub);
1378 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1380 avctx->frame_number++;
1384 void avsubtitle_free(AVSubtitle *sub)
1388 for (i = 0; i < sub->num_rects; i++)
1390 av_freep(&sub->rects[i]->pict.data[0]);
1391 av_freep(&sub->rects[i]->pict.data[1]);
1392 av_freep(&sub->rects[i]->pict.data[2]);
1393 av_freep(&sub->rects[i]->pict.data[3]);
1394 av_freep(&sub->rects[i]->text);
1395 av_freep(&sub->rects[i]->ass);
1396 av_freep(&sub->rects[i]);
1399 av_freep(&sub->rects);
1401 memset(sub, 0, sizeof(AVSubtitle));
1404 av_cold int avcodec_close(AVCodecContext *avctx)
1406 /* If there is a user-supplied mutex locking routine, call it. */
1407 if (ff_lockmgr_cb) {
1408 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1412 entangled_thread_counter++;
1413 if(entangled_thread_counter != 1){
1414 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1415 entangled_thread_counter--;
1419 if (avcodec_is_open(avctx)) {
1420 if (HAVE_THREADS && avctx->thread_opaque)
1421 ff_thread_free(avctx);
1422 if (avctx->codec && avctx->codec->close)
1423 avctx->codec->close(avctx);
1424 avcodec_default_free_buffers(avctx);
1425 avctx->coded_frame = NULL;
1426 av_freep(&avctx->internal);
1429 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1430 av_opt_free(avctx->priv_data);
1432 av_freep(&avctx->priv_data);
1433 if (codec_is_encoder(avctx->codec))
1434 av_freep(&avctx->extradata);
1435 avctx->codec = NULL;
1436 avctx->active_thread_type = 0;
1437 entangled_thread_counter--;
1439 /* Release any user-supplied mutex. */
1440 if (ff_lockmgr_cb) {
1441 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1446 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1449 //This is for future deprecatec codec ids, its empty since
1450 //last major bump but will fill up again over time, please dont remove it
1451 // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1452 default : return id;
1456 AVCodec *avcodec_find_encoder(enum CodecID id)
1458 AVCodec *p, *experimental=NULL;
1460 id= remap_deprecated_codec_id(id);
1462 if (codec_is_encoder(p) && p->id == id) {
1463 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1470 return experimental;
1473 AVCodec *avcodec_find_encoder_by_name(const char *name)
1480 if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
1487 AVCodec *avcodec_find_decoder(enum CodecID id)
1489 AVCodec *p, *experimental=NULL;
1491 id= remap_deprecated_codec_id(id);
1493 if (codec_is_decoder(p) && p->id == id) {
1494 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1501 return experimental;
1504 AVCodec *avcodec_find_decoder_by_name(const char *name)
1511 if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
1518 static int get_bit_rate(AVCodecContext *ctx)
1521 int bits_per_sample;
1523 switch(ctx->codec_type) {
1524 case AVMEDIA_TYPE_VIDEO:
1525 case AVMEDIA_TYPE_DATA:
1526 case AVMEDIA_TYPE_SUBTITLE:
1527 case AVMEDIA_TYPE_ATTACHMENT:
1528 bit_rate = ctx->bit_rate;
1530 case AVMEDIA_TYPE_AUDIO:
1531 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1532 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1541 const char *avcodec_get_name(enum CodecID id)
1547 #include "libavcodec/codec_names.h"
1549 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1551 codec = avcodec_find_decoder(id);
1554 codec = avcodec_find_encoder(id);
1557 return "unknown_codec";
1560 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1562 int i, len, ret = 0;
1564 for (i = 0; i < 4; i++) {
1565 len = snprintf(buf, buf_size,
1566 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1568 buf_size = buf_size > len ? buf_size - len : 0;
1575 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1577 const char *codec_type;
1578 const char *codec_name;
1579 const char *profile = NULL;
1582 AVRational display_aspect_ratio;
1584 if (!buf || buf_size <= 0)
1586 codec_type = av_get_media_type_string(enc->codec_type);
1587 codec_name = avcodec_get_name(enc->codec_id);
1588 if (enc->profile != FF_PROFILE_UNKNOWN) {
1589 p = encode ? avcodec_find_encoder(enc->codec_id) :
1590 avcodec_find_decoder(enc->codec_id);
1592 profile = av_get_profile_name(p, enc->profile);
1595 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1596 codec_name, enc->mb_decision ? " (hq)" : "");
1597 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1599 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1600 if (enc->codec_tag) {
1602 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1603 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1604 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1606 switch(enc->codec_type) {
1607 case AVMEDIA_TYPE_VIDEO:
1608 if (enc->pix_fmt != PIX_FMT_NONE) {
1609 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1611 av_get_pix_fmt_name(enc->pix_fmt));
1614 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1616 enc->width, enc->height);
1617 if (enc->sample_aspect_ratio.num) {
1618 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1619 enc->width*enc->sample_aspect_ratio.num,
1620 enc->height*enc->sample_aspect_ratio.den,
1622 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1623 " [SAR %d:%d DAR %d:%d]",
1624 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1625 display_aspect_ratio.num, display_aspect_ratio.den);
1627 if(av_log_get_level() >= AV_LOG_DEBUG){
1628 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1629 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1631 enc->time_base.num/g, enc->time_base.den/g);
1635 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1636 ", q=%d-%d", enc->qmin, enc->qmax);
1639 case AVMEDIA_TYPE_AUDIO:
1640 if (enc->sample_rate) {
1641 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1642 ", %d Hz", enc->sample_rate);
1644 av_strlcat(buf, ", ", buf_size);
1645 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1646 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1647 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1648 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1655 if (enc->flags & CODEC_FLAG_PASS1)
1656 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1658 if (enc->flags & CODEC_FLAG_PASS2)
1659 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1662 bitrate = get_bit_rate(enc);
1664 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1665 ", %d kb/s", bitrate / 1000);
1669 const char *av_get_profile_name(const AVCodec *codec, int profile)
1672 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1675 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1676 if (p->profile == profile)
1682 unsigned avcodec_version( void )
1684 // av_assert0(CODEC_ID_V410==164);
1685 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1686 av_assert0(CODEC_ID_ADPCM_G722==69660);
1687 // av_assert0(CODEC_ID_BMV_AUDIO==86071);
1688 av_assert0(CODEC_ID_SRT==94216);
1689 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1691 return LIBAVCODEC_VERSION_INT;
1694 const char *avcodec_configuration(void)
1696 return FFMPEG_CONFIGURATION;
1699 const char *avcodec_license(void)
1701 #define LICENSE_PREFIX "libavcodec license: "
1702 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1705 void avcodec_flush_buffers(AVCodecContext *avctx)
1707 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1708 ff_thread_flush(avctx);
1709 else if(avctx->codec->flush)
1710 avctx->codec->flush(avctx);
1713 static void video_free_buffers(AVCodecContext *s)
1715 AVCodecInternal *avci = s->internal;
1721 if (avci->buffer_count)
1722 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1723 avci->buffer_count);
1724 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1725 InternalBuffer *buf = &avci->buffer[i];
1727 av_freep(&buf->base[j]);
1731 av_freep(&avci->buffer);
1733 avci->buffer_count=0;
1736 static void audio_free_buffers(AVCodecContext *avctx)
1738 AVCodecInternal *avci = avctx->internal;
1739 InternalBuffer *buf;
1745 if (buf->extended_data) {
1746 av_free(buf->extended_data[0]);
1747 if (buf->extended_data != buf->data)
1748 av_freep(&buf->extended_data);
1750 av_freep(&avci->buffer);
1753 void avcodec_default_free_buffers(AVCodecContext *avctx)
1755 switch (avctx->codec_type) {
1756 case AVMEDIA_TYPE_VIDEO:
1757 video_free_buffers(avctx);
1759 case AVMEDIA_TYPE_AUDIO:
1760 audio_free_buffers(avctx);
1767 int av_get_bits_per_sample(enum CodecID codec_id){
1769 case CODEC_ID_ADPCM_SBPRO_2:
1771 case CODEC_ID_ADPCM_SBPRO_3:
1773 case CODEC_ID_ADPCM_SBPRO_4:
1774 case CODEC_ID_ADPCM_CT:
1775 case CODEC_ID_ADPCM_IMA_APC:
1776 case CODEC_ID_ADPCM_IMA_WAV:
1777 case CODEC_ID_ADPCM_IMA_QT:
1778 case CODEC_ID_ADPCM_SWF:
1779 case CODEC_ID_ADPCM_MS:
1780 case CODEC_ID_ADPCM_YAMAHA:
1781 case CODEC_ID_ADPCM_G722:
1783 case CODEC_ID_PCM_ALAW:
1784 case CODEC_ID_PCM_MULAW:
1785 case CODEC_ID_PCM_S8:
1786 case CODEC_ID_PCM_U8:
1787 case CODEC_ID_PCM_ZORK:
1789 case CODEC_ID_PCM_S16BE:
1790 case CODEC_ID_PCM_S16LE:
1791 case CODEC_ID_PCM_S16LE_PLANAR:
1792 case CODEC_ID_PCM_U16BE:
1793 case CODEC_ID_PCM_U16LE:
1795 case CODEC_ID_PCM_S24DAUD:
1796 case CODEC_ID_PCM_S24BE:
1797 case CODEC_ID_PCM_S24LE:
1798 case CODEC_ID_PCM_U24BE:
1799 case CODEC_ID_PCM_U24LE:
1801 case CODEC_ID_PCM_S32BE:
1802 case CODEC_ID_PCM_S32LE:
1803 case CODEC_ID_PCM_U32BE:
1804 case CODEC_ID_PCM_U32LE:
1805 case CODEC_ID_PCM_F32BE:
1806 case CODEC_ID_PCM_F32LE:
1808 case CODEC_ID_PCM_F64BE:
1809 case CODEC_ID_PCM_F64LE:
1817 int ff_thread_init(AVCodecContext *s){
1822 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1836 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1838 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1842 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1844 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1845 "version to the newest one from Git. If the problem still "
1846 "occurs, it means that your file has a feature which has not "
1847 "been implemented.\n", feature);
1849 av_log_ask_for_sample(avc, NULL);
1852 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1854 va_list argument_list;
1856 va_start(argument_list, msg);
1859 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1860 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1861 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1862 "and contact the ffmpeg-devel mailing list.\n");
1864 va_end(argument_list);
1867 static AVHWAccel *first_hwaccel = NULL;
1869 void av_register_hwaccel(AVHWAccel *hwaccel)
1871 AVHWAccel **p = &first_hwaccel;
1875 hwaccel->next = NULL;
1878 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1880 return hwaccel ? hwaccel->next : first_hwaccel;
1883 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1885 AVHWAccel *hwaccel=NULL;
1887 while((hwaccel= av_hwaccel_next(hwaccel))){
1888 if ( hwaccel->id == codec_id
1889 && hwaccel->pix_fmt == pix_fmt)
1895 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1897 if (ff_lockmgr_cb) {
1898 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1900 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1906 if (ff_lockmgr_cb) {
1907 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1909 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1915 int avpriv_lock_avformat(void)
1917 if (ff_lockmgr_cb) {
1918 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1924 int avpriv_unlock_avformat(void)
1926 if (ff_lockmgr_cb) {
1927 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1933 unsigned int avpriv_toupper4(unsigned int x)
1935 return toupper( x &0xFF)
1936 + (toupper((x>>8 )&0xFF)<<8 )
1937 + (toupper((x>>16)&0xFF)<<16)
1938 + (toupper((x>>24)&0xFF)<<24);
1943 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1947 ff_init_buffer_info(avctx, f);
1949 return avctx->get_buffer(avctx, f);
1952 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1954 f->owner->release_buffer(f->owner, f);
1957 void ff_thread_finish_setup(AVCodecContext *avctx)
1961 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1965 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1971 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1973 AVCodec *c= avcodec_find_decoder(codec_id);
1975 c= avcodec_find_encoder(codec_id);
1979 if (codec_id <= CODEC_ID_NONE)
1980 return AVMEDIA_TYPE_UNKNOWN;
1981 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1982 return AVMEDIA_TYPE_VIDEO;
1983 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1984 return AVMEDIA_TYPE_AUDIO;
1985 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1986 return AVMEDIA_TYPE_SUBTITLE;
1988 return AVMEDIA_TYPE_UNKNOWN;
1991 int avcodec_is_open(AVCodecContext *s)
1993 return !!s->internal;