3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/audioconvert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avassert.h"
40 #include "libavutil/opt.h"
41 #include "imgconvert.h"
43 #include "audioconvert.h"
45 #include "bytestream.h"
51 static int volatile entangled_thread_counter=0;
52 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
53 static void *codec_mutex;
54 static void *avformat_mutex;
56 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
61 min_size= FFMAX(17*min_size/16 + 32, min_size);
63 ptr= av_realloc(ptr, min_size);
64 if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
72 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
77 min_size= FFMAX(17*min_size/16 + 32, min_size);
79 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
80 if (!*p) min_size = 0;
85 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
87 ff_fast_malloc(ptr, size, min_size, 0);
90 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
93 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
98 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
99 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
102 /* encoder management */
103 static AVCodec *first_avcodec = NULL;
105 AVCodec *av_codec_next(AVCodec *c){
106 if(c) return c->next;
107 else return first_avcodec;
110 static void avcodec_init(void)
112 static int initialized = 0;
114 if (initialized != 0)
118 ff_dsputil_static_init();
121 int av_codec_is_encoder(AVCodec *codec)
123 return codec && (codec->encode || codec->encode2);
126 int av_codec_is_decoder(AVCodec *codec)
128 return codec && codec->decode;
131 void avcodec_register(AVCodec *codec)
136 while (*p != NULL) p = &(*p)->next;
140 if (codec->init_static_data)
141 codec->init_static_data(codec);
144 unsigned avcodec_get_edge_width(void)
149 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
150 s->coded_width = width;
151 s->coded_height= height;
152 s->width = -((-width )>>s->lowres);
153 s->height= -((-height)>>s->lowres);
156 #define INTERNAL_BUFFER_SIZE (32+1)
158 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
159 int linesize_align[AV_NUM_DATA_POINTERS])
166 case PIX_FMT_YUV420P:
167 case PIX_FMT_YUYV422:
168 case PIX_FMT_UYVY422:
169 case PIX_FMT_YUV422P:
170 case PIX_FMT_YUV440P:
171 case PIX_FMT_YUV444P:
174 case PIX_FMT_GRAY16BE:
175 case PIX_FMT_GRAY16LE:
176 case PIX_FMT_YUVJ420P:
177 case PIX_FMT_YUVJ422P:
178 case PIX_FMT_YUVJ440P:
179 case PIX_FMT_YUVJ444P:
180 case PIX_FMT_YUVA420P:
181 case PIX_FMT_YUVA444P:
182 case PIX_FMT_YUV420P9LE:
183 case PIX_FMT_YUV420P9BE:
184 case PIX_FMT_YUV420P10LE:
185 case PIX_FMT_YUV420P10BE:
186 case PIX_FMT_YUV422P9LE:
187 case PIX_FMT_YUV422P9BE:
188 case PIX_FMT_YUV422P10LE:
189 case PIX_FMT_YUV422P10BE:
190 case PIX_FMT_YUV444P9LE:
191 case PIX_FMT_YUV444P9BE:
192 case PIX_FMT_YUV444P10LE:
193 case PIX_FMT_YUV444P10BE:
194 case PIX_FMT_GBRP9LE:
195 case PIX_FMT_GBRP9BE:
196 case PIX_FMT_GBRP10LE:
197 case PIX_FMT_GBRP10BE:
198 w_align = 16; //FIXME assume 16 pixel per macroblock
199 h_align = 16 * 2; // interlaced needs 2 macroblocks height
201 case PIX_FMT_YUV411P:
202 case PIX_FMT_UYYVYY411:
206 case PIX_FMT_YUV410P:
207 if(s->codec_id == CODEC_ID_SVQ1){
212 if(s->codec_id == CODEC_ID_RPZA){
219 if(s->codec_id == CODEC_ID_SMC){
225 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
236 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
237 w_align= FFMAX(w_align, 8);
240 *width = FFALIGN(*width , w_align);
241 *height= FFALIGN(*height, h_align);
242 if(s->codec_id == CODEC_ID_H264 || s->lowres)
243 *height+=2; // some of the optimized chroma MC reads one line too much
244 // which is also done in mpeg decoders with lowres > 0
246 for (i = 0; i < 4; i++)
247 linesize_align[i] = STRIDE_ALIGN;
250 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
251 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
252 int linesize_align[AV_NUM_DATA_POINTERS];
254 avcodec_align_dimensions2(s, width, height, linesize_align);
255 align = FFMAX(linesize_align[0], linesize_align[3]);
256 linesize_align[1] <<= chroma_shift;
257 linesize_align[2] <<= chroma_shift;
258 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
259 *width=FFALIGN(*width, align);
262 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
265 pic->pkt_pts = s->pkt->pts;
266 pic->pkt_pos = s->pkt->pos;
268 pic->pkt_pts = AV_NOPTS_VALUE;
271 pic->reordered_opaque= s->reordered_opaque;
272 pic->sample_aspect_ratio = s->sample_aspect_ratio;
273 pic->width = s->width;
274 pic->height = s->height;
275 pic->format = s->pix_fmt;
278 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
279 enum AVSampleFormat sample_fmt, const uint8_t *buf,
280 int buf_size, int align)
282 int ch, planar, needed_size, ret = 0;
284 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
285 frame->nb_samples, sample_fmt,
287 if (buf_size < needed_size)
288 return AVERROR(EINVAL);
290 planar = av_sample_fmt_is_planar(sample_fmt);
291 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
292 if (!(frame->extended_data = av_mallocz(nb_channels *
293 sizeof(*frame->extended_data))))
294 return AVERROR(ENOMEM);
296 frame->extended_data = frame->data;
299 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
300 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
301 sample_fmt, align)) < 0) {
302 if (frame->extended_data != frame->data)
303 av_freep(&frame->extended_data);
306 if (frame->extended_data != frame->data) {
307 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
308 frame->data[ch] = frame->extended_data[ch];
314 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
316 AVCodecInternal *avci = avctx->internal;
320 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
321 frame->nb_samples, avctx->sample_fmt,
324 return AVERROR(EINVAL);
326 /* allocate InternalBuffer if needed */
328 avci->buffer = av_mallocz(sizeof(InternalBuffer));
330 return AVERROR(ENOMEM);
334 /* if there is a previously-used internal buffer, check its size and
335 channel count to see if we can reuse it */
336 if (buf->extended_data) {
337 /* if current buffer is too small, free it */
338 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
339 av_free(buf->extended_data[0]);
340 if (buf->extended_data != buf->data)
341 av_freep(&buf->extended_data);
342 buf->extended_data = NULL;
345 /* if number of channels has changed, reset and/or free extended data
346 pointers but leave data buffer in buf->data[0] for reuse */
347 if (buf->nb_channels != avctx->channels) {
348 if (buf->extended_data != buf->data)
349 av_free(buf->extended_data);
350 buf->extended_data = NULL;
354 /* if there is no previous buffer or the previous buffer cannot be used
355 as-is, allocate a new buffer and/or rearrange the channel pointers */
356 if (!buf->extended_data) {
358 if (!(buf->data[0] = av_mallocz(buf_size)))
359 return AVERROR(ENOMEM);
360 buf->audio_data_size = buf_size;
362 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
363 avctx->sample_fmt, buf->data[0],
364 buf->audio_data_size, 32)))
367 if (frame->extended_data == frame->data)
368 buf->extended_data = buf->data;
370 buf->extended_data = frame->extended_data;
371 memcpy(buf->data, frame->data, sizeof(frame->data));
372 buf->linesize[0] = frame->linesize[0];
373 buf->nb_channels = avctx->channels;
375 /* copy InternalBuffer info to the AVFrame */
376 frame->extended_data = buf->extended_data;
377 frame->linesize[0] = buf->linesize[0];
378 memcpy(frame->data, buf->data, sizeof(frame->data));
381 frame->type = FF_BUFFER_TYPE_INTERNAL;
384 frame->pkt_pts = avctx->pkt->pts;
385 frame->pkt_pos = avctx->pkt->pos;
387 frame->pkt_pts = AV_NOPTS_VALUE;
391 frame->reordered_opaque = avctx->reordered_opaque;
393 if (avctx->debug & FF_DEBUG_BUFFERS)
394 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
395 "internal audio buffer used\n", frame);
400 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
406 AVCodecInternal *avci = s->internal;
408 if(pic->data[0]!=NULL) {
409 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
412 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
413 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
417 if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
421 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
422 sizeof(InternalBuffer));
425 buf = &avci->buffer[avci->buffer_count];
427 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
428 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
429 av_freep(&buf->base[i]);
435 int h_chroma_shift, v_chroma_shift;
440 int stride_align[AV_NUM_DATA_POINTERS];
441 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
443 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
445 avcodec_align_dimensions2(s, &w, &h, stride_align);
447 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
453 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
454 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
455 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
456 // increase alignment of w for next try (rhs gives the lowest bit set in w)
461 unaligned |= picture.linesize[i] % stride_align[i];
465 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
469 for (i=0; i<3 && picture.data[i+1]; i++)
470 size[i] = picture.data[i+1] - picture.data[i];
471 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
473 memset(buf->base, 0, sizeof(buf->base));
474 memset(buf->data, 0, sizeof(buf->data));
476 for(i=0; i<4 && size[i]; i++){
477 const int h_shift= i==0 ? 0 : h_chroma_shift;
478 const int v_shift= i==0 ? 0 : v_chroma_shift;
480 buf->linesize[i]= picture.linesize[i];
482 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
483 if(buf->base[i]==NULL) return -1;
484 memset(buf->base[i], 128, size[i]);
486 // no edge if EDGE EMU or not planar YUV
487 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
488 buf->data[i] = buf->base[i];
490 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
492 for (; i < AV_NUM_DATA_POINTERS; i++) {
493 buf->base[i] = buf->data[i] = NULL;
494 buf->linesize[i] = 0;
496 if(size[1] && !size[2])
497 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
498 buf->width = s->width;
499 buf->height = s->height;
500 buf->pix_fmt= s->pix_fmt;
502 pic->type= FF_BUFFER_TYPE_INTERNAL;
504 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
505 pic->base[i]= buf->base[i];
506 pic->data[i]= buf->data[i];
507 pic->linesize[i]= buf->linesize[i];
509 pic->extended_data = pic->data;
510 avci->buffer_count++;
511 pic->width = buf->width;
512 pic->height = buf->height;
513 pic->format = buf->pix_fmt;
514 pic->sample_aspect_ratio = s->sample_aspect_ratio;
517 pic->pkt_pts = s->pkt->pts;
518 pic->pkt_pos = s->pkt->pos;
520 pic->pkt_pts = AV_NOPTS_VALUE;
523 pic->reordered_opaque= s->reordered_opaque;
524 pic->sample_aspect_ratio = s->sample_aspect_ratio;
525 pic->width = s->width;
526 pic->height = s->height;
527 pic->format = s->pix_fmt;
529 if(s->debug&FF_DEBUG_BUFFERS)
530 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
531 "buffers used\n", pic, avci->buffer_count);
536 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
538 switch (avctx->codec_type) {
539 case AVMEDIA_TYPE_VIDEO:
540 return video_get_buffer(avctx, frame);
541 case AVMEDIA_TYPE_AUDIO:
542 return audio_get_buffer(avctx, frame);
548 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
550 InternalBuffer *buf, *last;
551 AVCodecInternal *avci = s->internal;
553 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
555 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
556 assert(avci->buffer_count);
559 buf = NULL; /* avoids warning */
560 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
561 buf = &avci->buffer[i];
562 if (buf->data[0] == pic->data[0])
565 assert(i < avci->buffer_count);
566 avci->buffer_count--;
567 last = &avci->buffer[avci->buffer_count];
570 FFSWAP(InternalBuffer, *buf, *last);
573 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
575 // pic->base[i]=NULL;
577 //printf("R%X\n", pic->opaque);
579 if(s->debug&FF_DEBUG_BUFFERS)
580 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
581 "buffers used\n", pic, avci->buffer_count);
584 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
588 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
590 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
591 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
592 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
593 s->release_buffer(s, pic);
596 ff_init_buffer_info(s, pic);
598 /* If no picture return a new buffer */
599 if(pic->data[0] == NULL) {
600 /* We will copy from buffer, so must be readable */
601 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
602 return s->get_buffer(s, pic);
605 assert(s->pix_fmt == pic->format);
607 /* If internal buffer type return the same buffer */
608 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
613 * Not internal type and reget_buffer not overridden, emulate cr buffer
616 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
617 pic->data[i] = pic->base[i] = NULL;
619 /* Allocate new frame */
620 if (s->get_buffer(s, pic))
622 /* Copy image data from old buffer to new buffer */
623 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
625 s->release_buffer(s, &temp_pic); // Release old frame
629 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
632 for(i=0; i<count; i++){
633 int r= func(c, (char*)arg + i*size);
639 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
642 for(i=0; i<count; i++){
643 int r= func(c, arg, i, 0);
649 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
650 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
655 void avcodec_get_frame_defaults(AVFrame *pic){
656 memset(pic, 0, sizeof(AVFrame));
658 pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
661 pic->sample_aspect_ratio = (AVRational){0, 1};
662 pic->format = -1; /* unknown */
665 AVFrame *avcodec_alloc_frame(void){
666 AVFrame *pic= av_malloc(sizeof(AVFrame));
668 if(pic==NULL) return NULL;
670 avcodec_get_frame_defaults(pic);
675 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
677 memset(sub, 0, sizeof(*sub));
678 sub->pts = AV_NOPTS_VALUE;
681 static int get_bit_rate(AVCodecContext *ctx)
686 switch(ctx->codec_type) {
687 case AVMEDIA_TYPE_VIDEO:
688 case AVMEDIA_TYPE_DATA:
689 case AVMEDIA_TYPE_SUBTITLE:
690 case AVMEDIA_TYPE_ATTACHMENT:
691 bit_rate = ctx->bit_rate;
693 case AVMEDIA_TYPE_AUDIO:
694 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
695 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
704 #if FF_API_AVCODEC_OPEN
705 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
707 return avcodec_open2(avctx, codec, NULL);
711 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
714 AVDictionary *tmp = NULL;
716 if (avcodec_is_open(avctx))
719 if ((!codec && !avctx->codec)) {
720 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
721 return AVERROR(EINVAL);
723 if ((codec && avctx->codec && codec != avctx->codec)) {
724 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
725 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
726 return AVERROR(EINVAL);
729 codec = avctx->codec;
731 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
732 return AVERROR(EINVAL);
735 av_dict_copy(&tmp, *options, 0);
737 /* If there is a user-supplied mutex locking routine, call it. */
739 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
743 entangled_thread_counter++;
744 if(entangled_thread_counter != 1){
745 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
750 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
751 if (!avctx->internal) {
752 ret = AVERROR(ENOMEM);
756 if (codec->priv_data_size > 0) {
757 if(!avctx->priv_data){
758 avctx->priv_data = av_mallocz(codec->priv_data_size);
759 if (!avctx->priv_data) {
760 ret = AVERROR(ENOMEM);
763 if (codec->priv_class) {
764 *(const AVClass**)avctx->priv_data= codec->priv_class;
765 av_opt_set_defaults(avctx->priv_data);
768 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
771 avctx->priv_data = NULL;
773 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
776 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
777 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
778 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
783 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
784 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
785 if(avctx->coded_width && avctx->coded_height)
786 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
787 else if(avctx->width && avctx->height)
788 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
791 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
792 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
793 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
794 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
795 avcodec_set_dimensions(avctx, 0, 0);
798 /* if the decoder init function was already called previously,
799 free the already allocated subtitle_header before overwriting it */
800 if (av_codec_is_decoder(codec))
801 av_freep(&avctx->subtitle_header);
803 #define SANE_NB_CHANNELS 128U
804 if (avctx->channels > SANE_NB_CHANNELS) {
805 ret = AVERROR(EINVAL);
809 avctx->codec = codec;
810 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
811 avctx->codec_id == CODEC_ID_NONE) {
812 avctx->codec_type = codec->type;
813 avctx->codec_id = codec->id;
815 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
816 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
817 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
818 ret = AVERROR(EINVAL);
821 avctx->frame_number = 0;
823 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
824 (!avctx->time_base.num || !avctx->time_base.den)) {
825 avctx->time_base.num = 1;
826 avctx->time_base.den = avctx->sample_rate;
830 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
832 if (HAVE_THREADS && !avctx->thread_opaque) {
833 ret = ff_thread_init(avctx);
838 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
839 avctx->thread_count = 1;
841 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
842 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
843 avctx->codec->max_lowres);
844 ret = AVERROR(EINVAL);
847 if (av_codec_is_encoder(avctx->codec)) {
849 if (avctx->codec->sample_fmts) {
850 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
851 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
853 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
854 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
855 ret = AVERROR(EINVAL);
859 if (avctx->codec->pix_fmts) {
860 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
861 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
863 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
864 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
865 ret = AVERROR(EINVAL);
869 if (avctx->codec->supported_samplerates) {
870 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
871 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
873 if (avctx->codec->supported_samplerates[i] == 0) {
874 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
875 ret = AVERROR(EINVAL);
879 if (avctx->codec->channel_layouts) {
880 if (!avctx->channel_layout) {
881 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
883 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
884 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
886 if (avctx->codec->channel_layouts[i] == 0) {
887 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
888 ret = AVERROR(EINVAL);
893 if (avctx->channel_layout && avctx->channels) {
894 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
895 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
896 ret = AVERROR(EINVAL);
899 } else if (avctx->channel_layout) {
900 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
904 avctx->pts_correction_num_faulty_pts =
905 avctx->pts_correction_num_faulty_dts = 0;
906 avctx->pts_correction_last_pts =
907 avctx->pts_correction_last_dts = INT64_MIN;
909 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
910 ret = avctx->codec->init(avctx);
916 if (av_codec_is_decoder(avctx->codec) && !avctx->bit_rate)
917 avctx->bit_rate = get_bit_rate(avctx);
921 entangled_thread_counter--;
923 /* Release any user-supplied mutex. */
925 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
928 av_dict_free(options);
935 av_freep(&avctx->priv_data);
936 av_freep(&avctx->internal);
941 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
943 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
944 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
945 return AVERROR(EINVAL);
948 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
949 if (!avpkt->data || avpkt->size < size) {
950 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
951 avpkt->data = avctx->internal->byte_buffer;
952 avpkt->size = avctx->internal->byte_buffer_size;
953 avpkt->destruct = NULL;
957 void *destruct = avpkt->destruct;
959 if (avpkt->size < size) {
960 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
961 return AVERROR(EINVAL);
964 av_init_packet(avpkt);
965 avpkt->destruct = destruct;
969 int ret = av_new_packet(avpkt, size);
971 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
976 int ff_alloc_packet(AVPacket *avpkt, int size)
978 return ff_alloc_packet2(NULL, avpkt, size);
981 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
983 const AVFrame *frame,
987 AVPacket user_pkt = *avpkt;
989 int needs_realloc = !user_pkt.data;
993 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
994 av_free_packet(avpkt);
995 av_init_packet(avpkt);
1000 /* check for valid frame size */
1002 nb_samples = frame->nb_samples;
1003 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1004 if (nb_samples > avctx->frame_size)
1005 return AVERROR(EINVAL);
1006 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1007 if (nb_samples != avctx->frame_size)
1008 return AVERROR(EINVAL);
1011 nb_samples = avctx->frame_size;
1014 if (avctx->codec->encode2) {
1015 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1016 if (!ret && *got_packet_ptr) {
1017 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1018 if (avpkt->pts == AV_NOPTS_VALUE)
1019 avpkt->pts = frame->pts;
1020 if (!avpkt->duration)
1021 avpkt->duration = ff_samples_to_time_base(avctx,
1024 avpkt->dts = avpkt->pts;
1029 /* for compatibility with encoders not supporting encode2(), we need to
1030 allocate a packet buffer if the user has not provided one or check
1031 the size otherwise */
1033 int buf_size = avpkt->size;
1034 if (!user_pkt.data) {
1035 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
1036 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
1038 return AVERROR(EINVAL);
1039 buf_size = nb_samples * avctx->channels *
1040 av_get_bits_per_sample(avctx->codec_id) / 8;
1042 /* this is a guess as to the required size.
1043 if an encoder needs more than this, it should probably
1044 implement encode2() */
1045 buf_size = 2 * avctx->frame_size * avctx->channels *
1046 av_get_bytes_per_sample(avctx->sample_fmt);
1047 buf_size += 2*FF_MIN_BUFFER_SIZE;
1050 if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)))
1053 /* Encoders using AVCodec.encode() that support
1054 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
1055 the smaller size when encoding the last frame.
1056 This code can be removed once all encoders supporting
1057 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
1058 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
1059 nb_samples < avctx->frame_size) {
1060 fs_tmp = avctx->frame_size;
1061 avctx->frame_size = nb_samples;
1064 /* encode the frame */
1065 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
1066 frame ? frame->data[0] : NULL);
1069 /* no output. if the packet data was allocated by libavcodec,
1071 if (!user_pkt.data && avpkt->data != avctx->internal->byte_buffer)
1072 av_freep(&avpkt->data);
1074 if (avctx->coded_frame)
1075 avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
1076 /* Set duration for final small packet. This can be removed
1077 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1080 avpkt->duration = ff_samples_to_time_base(avctx,
1085 *got_packet_ptr = (ret > 0);
1090 avctx->frame_size = fs_tmp;
1092 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1094 if (user_pkt.data) {
1095 if (user_pkt.size >= avpkt->size) {
1096 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1098 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1099 avpkt->size = user_pkt.size;
1102 avpkt->data = user_pkt.data;
1103 avpkt->destruct = user_pkt.destruct;
1105 if (av_dup_packet(avpkt) < 0) {
1106 ret = AVERROR(ENOMEM);
1112 if (needs_realloc && avpkt->data) {
1113 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1115 avpkt->data = new_data;
1118 avctx->frame_number++;
1121 if (ret < 0 || !*got_packet_ptr)
1122 av_free_packet(avpkt);
1124 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1125 this needs to be moved to the encoders, but for now we can do it
1126 here to simplify things */
1127 avpkt->flags |= AV_PKT_FLAG_KEY;
1132 #if FF_API_OLD_DECODE_AUDIO
1133 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1134 uint8_t *buf, int buf_size,
1135 const short *samples)
1140 int ret, samples_size, got_packet;
1142 av_init_packet(&pkt);
1144 pkt.size = buf_size;
1148 avcodec_get_frame_defaults(frame);
1150 if (avctx->frame_size) {
1151 frame->nb_samples = avctx->frame_size;
1153 /* if frame_size is not set, the number of samples must be
1154 calculated from the buffer size */
1156 if (!av_get_bits_per_sample(avctx->codec_id)) {
1157 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1158 "support this codec\n");
1159 return AVERROR(EINVAL);
1161 nb_samples = (int64_t)buf_size * 8 /
1162 (av_get_bits_per_sample(avctx->codec_id) *
1164 if (nb_samples >= INT_MAX)
1165 return AVERROR(EINVAL);
1166 frame->nb_samples = nb_samples;
1169 /* it is assumed that the samples buffer is large enough based on the
1170 relevant parameters */
1171 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1173 avctx->sample_fmt, 1);
1174 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1176 (const uint8_t *)samples, samples_size, 1)))
1179 /* fabricate frame pts from sample count.
1180 this is needed because the avcodec_encode_audio() API does not have
1181 a way for the user to provide pts */
1182 if(avctx->sample_rate && avctx->time_base.num)
1183 frame->pts = ff_samples_to_time_base(avctx,
1184 avctx->internal->sample_count);
1186 frame->pts = AV_NOPTS_VALUE;
1187 avctx->internal->sample_count += frame->nb_samples;
1193 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1194 if (!ret && got_packet && avctx->coded_frame) {
1195 avctx->coded_frame->pts = pkt.pts;
1196 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1198 /* free any side data since we cannot return it */
1199 ff_packet_free_side_data(&pkt);
1201 if (frame && frame->extended_data != frame->data)
1202 av_freep(&frame->extended_data);
1204 return ret ? ret : pkt.size;
1208 #if FF_API_OLD_ENCODE_VIDEO
1209 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1210 const AVFrame *pict)
1213 int ret, got_packet = 0;
1215 if(buf_size < FF_MIN_BUFFER_SIZE){
1216 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1220 av_init_packet(&pkt);
1222 pkt.size = buf_size;
1224 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1225 if (!ret && got_packet && avctx->coded_frame) {
1226 avctx->coded_frame->pts = pkt.pts;
1227 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1230 /* free any side data since we cannot return it */
1231 if (pkt.side_data_elems > 0) {
1233 for (i = 0; i < pkt.side_data_elems; i++)
1234 av_free(pkt.side_data[i].data);
1235 av_freep(&pkt.side_data);
1236 pkt.side_data_elems = 0;
1239 return ret ? ret : pkt.size;
1243 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1245 const AVFrame *frame,
1246 int *got_packet_ptr)
1249 AVPacket user_pkt = *avpkt;
1250 int needs_realloc = !user_pkt.data;
1252 *got_packet_ptr = 0;
1254 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1255 av_free_packet(avpkt);
1256 av_init_packet(avpkt);
1261 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1262 return AVERROR(EINVAL);
1264 av_assert0(avctx->codec->encode2);
1266 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1267 av_assert0(ret <= 0);
1269 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1271 if (user_pkt.data) {
1272 if (user_pkt.size >= avpkt->size) {
1273 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1275 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1276 avpkt->size = user_pkt.size;
1279 avpkt->data = user_pkt.data;
1280 avpkt->destruct = user_pkt.destruct;
1282 if (av_dup_packet(avpkt) < 0) {
1283 ret = AVERROR(ENOMEM);
1289 if (!*got_packet_ptr)
1291 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1292 avpkt->pts = avpkt->dts = frame->pts;
1294 if (needs_realloc && avpkt->data &&
1295 avpkt->destruct == av_destruct_packet) {
1296 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1298 avpkt->data = new_data;
1301 avctx->frame_number++;
1304 if (ret < 0 || !*got_packet_ptr)
1305 av_free_packet(avpkt);
1311 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1312 const AVSubtitle *sub)
1315 if(sub->start_display_time) {
1316 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1320 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
1321 avctx->frame_number++;
1326 * Attempt to guess proper monotonic timestamps for decoded video frames
1327 * which might have incorrect times. Input timestamps may wrap around, in
1328 * which case the output will as well.
1330 * @param pts the pts field of the decoded AVPacket, as passed through
1332 * @param dts the dts field of the decoded AVPacket
1333 * @return one of the input values, may be AV_NOPTS_VALUE
1335 static int64_t guess_correct_pts(AVCodecContext *ctx,
1336 int64_t reordered_pts, int64_t dts)
1338 int64_t pts = AV_NOPTS_VALUE;
1340 if (dts != AV_NOPTS_VALUE) {
1341 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1342 ctx->pts_correction_last_dts = dts;
1344 if (reordered_pts != AV_NOPTS_VALUE) {
1345 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1346 ctx->pts_correction_last_pts = reordered_pts;
1348 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1349 && reordered_pts != AV_NOPTS_VALUE)
1350 pts = reordered_pts;
1357 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1360 const uint8_t *data;
1363 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1366 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1367 if (!data || size < 4)
1369 flags = bytestream_get_le32(&data);
1371 if (size < 4) /* Required for any of the changes */
1373 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1374 avctx->channels = bytestream_get_le32(&data);
1377 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1380 avctx->channel_layout = bytestream_get_le64(&data);
1385 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1386 avctx->sample_rate = bytestream_get_le32(&data);
1389 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1392 avctx->width = bytestream_get_le32(&data);
1393 avctx->height = bytestream_get_le32(&data);
1394 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1399 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1400 int *got_picture_ptr,
1401 const AVPacket *avpkt)
1404 // copy to ensure we do not change avpkt
1405 AVPacket tmp = *avpkt;
1407 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1408 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1409 return AVERROR(EINVAL);
1412 *got_picture_ptr= 0;
1413 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1416 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1417 int did_split = av_packet_split_side_data(&tmp);
1418 apply_param_change(avctx, &tmp);
1420 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1421 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1424 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1426 picture->pkt_dts= avpkt->dts;
1428 if(!avctx->has_b_frames){
1429 picture->pkt_pos= avpkt->pos;
1431 //FIXME these should be under if(!avctx->has_b_frames)
1432 if (!picture->sample_aspect_ratio.num)
1433 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1434 if (!picture->width)
1435 picture->width = avctx->width;
1436 if (!picture->height)
1437 picture->height = avctx->height;
1438 if (picture->format == PIX_FMT_NONE)
1439 picture->format = avctx->pix_fmt;
1442 emms_c(); //needed to avoid an emms_c() call before every return;
1446 ff_packet_free_side_data(&tmp);
1448 if (*got_picture_ptr){
1449 avctx->frame_number++;
1450 picture->best_effort_timestamp = guess_correct_pts(avctx,
1460 #if FF_API_OLD_DECODE_AUDIO
1461 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1462 int *frame_size_ptr,
1466 int ret, got_frame = 0;
1468 if (avctx->get_buffer != avcodec_default_get_buffer) {
1469 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1470 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1471 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1472 "avcodec_decode_audio4()\n");
1473 avctx->get_buffer = avcodec_default_get_buffer;
1474 avctx->release_buffer = avcodec_default_release_buffer;
1477 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1479 if (ret >= 0 && got_frame) {
1481 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1482 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1484 avctx->sample_fmt, 1);
1485 if (*frame_size_ptr < data_size) {
1486 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1487 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1488 return AVERROR(EINVAL);
1491 memcpy(samples, frame.extended_data[0], plane_size);
1493 if (planar && avctx->channels > 1) {
1494 uint8_t *out = ((uint8_t *)samples) + plane_size;
1495 for (ch = 1; ch < avctx->channels; ch++) {
1496 memcpy(out, frame.extended_data[ch], plane_size);
1500 *frame_size_ptr = data_size;
1502 *frame_size_ptr = 0;
1508 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1517 if (!avpkt->data && avpkt->size) {
1518 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1519 return AVERROR(EINVAL);
1521 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1522 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1523 return AVERROR(EINVAL);
1526 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1527 av_packet_split_side_data(avpkt);
1528 apply_param_change(avctx, avpkt);
1531 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1532 if (ret >= 0 && *got_frame_ptr) {
1533 avctx->frame_number++;
1534 frame->pkt_dts = avpkt->dts;
1535 if (frame->format == AV_SAMPLE_FMT_NONE)
1536 frame->format = avctx->sample_fmt;
1542 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1548 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1549 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1550 return AVERROR(EINVAL);
1555 avcodec_get_subtitle_defaults(sub);
1556 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1558 avctx->frame_number++;
1562 void avsubtitle_free(AVSubtitle *sub)
1566 for (i = 0; i < sub->num_rects; i++)
1568 av_freep(&sub->rects[i]->pict.data[0]);
1569 av_freep(&sub->rects[i]->pict.data[1]);
1570 av_freep(&sub->rects[i]->pict.data[2]);
1571 av_freep(&sub->rects[i]->pict.data[3]);
1572 av_freep(&sub->rects[i]->text);
1573 av_freep(&sub->rects[i]->ass);
1574 av_freep(&sub->rects[i]);
1577 av_freep(&sub->rects);
1579 memset(sub, 0, sizeof(AVSubtitle));
1582 av_cold int avcodec_close(AVCodecContext *avctx)
1584 /* If there is a user-supplied mutex locking routine, call it. */
1585 if (ff_lockmgr_cb) {
1586 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1590 entangled_thread_counter++;
1591 if(entangled_thread_counter != 1){
1592 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1593 entangled_thread_counter--;
1597 if (avcodec_is_open(avctx)) {
1598 if (HAVE_THREADS && avctx->thread_opaque)
1599 ff_thread_free(avctx);
1600 if (avctx->codec && avctx->codec->close)
1601 avctx->codec->close(avctx);
1602 avcodec_default_free_buffers(avctx);
1603 avctx->coded_frame = NULL;
1604 avctx->internal->byte_buffer_size = 0;
1605 av_freep(&avctx->internal->byte_buffer);
1606 av_freep(&avctx->internal);
1609 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1610 av_opt_free(avctx->priv_data);
1612 av_freep(&avctx->priv_data);
1613 if (av_codec_is_encoder(avctx->codec))
1614 av_freep(&avctx->extradata);
1615 avctx->codec = NULL;
1616 avctx->active_thread_type = 0;
1617 entangled_thread_counter--;
1619 /* Release any user-supplied mutex. */
1620 if (ff_lockmgr_cb) {
1621 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1626 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1629 //This is for future deprecatec codec ids, its empty since
1630 //last major bump but will fill up again over time, please dont remove it
1631 // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1632 default : return id;
1636 AVCodec *avcodec_find_encoder(enum CodecID id)
1638 AVCodec *p, *experimental=NULL;
1640 id= remap_deprecated_codec_id(id);
1642 if (av_codec_is_encoder(p) && p->id == id) {
1643 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1650 return experimental;
1653 AVCodec *avcodec_find_encoder_by_name(const char *name)
1660 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1667 AVCodec *avcodec_find_decoder(enum CodecID id)
1669 AVCodec *p, *experimental=NULL;
1671 id= remap_deprecated_codec_id(id);
1673 if (av_codec_is_decoder(p) && p->id == id) {
1674 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1681 return experimental;
1684 AVCodec *avcodec_find_decoder_by_name(const char *name)
1691 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1698 const char *avcodec_get_name(enum CodecID id)
1704 #include "libavcodec/codec_names.h"
1706 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1708 codec = avcodec_find_decoder(id);
1711 codec = avcodec_find_encoder(id);
1714 return "unknown_codec";
1717 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1719 int i, len, ret = 0;
1721 for (i = 0; i < 4; i++) {
1722 len = snprintf(buf, buf_size,
1723 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1725 buf_size = buf_size > len ? buf_size - len : 0;
1732 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1734 const char *codec_type;
1735 const char *codec_name;
1736 const char *profile = NULL;
1739 AVRational display_aspect_ratio;
1741 if (!buf || buf_size <= 0)
1743 codec_type = av_get_media_type_string(enc->codec_type);
1744 codec_name = avcodec_get_name(enc->codec_id);
1745 if (enc->profile != FF_PROFILE_UNKNOWN) {
1746 p = encode ? avcodec_find_encoder(enc->codec_id) :
1747 avcodec_find_decoder(enc->codec_id);
1749 profile = av_get_profile_name(p, enc->profile);
1752 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1753 codec_name, enc->mb_decision ? " (hq)" : "");
1754 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1756 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1757 if (enc->codec_tag) {
1759 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1760 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1761 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1763 switch(enc->codec_type) {
1764 case AVMEDIA_TYPE_VIDEO:
1765 if (enc->pix_fmt != PIX_FMT_NONE) {
1766 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1768 av_get_pix_fmt_name(enc->pix_fmt));
1771 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1773 enc->width, enc->height);
1774 if (enc->sample_aspect_ratio.num) {
1775 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1776 enc->width*enc->sample_aspect_ratio.num,
1777 enc->height*enc->sample_aspect_ratio.den,
1779 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1780 " [SAR %d:%d DAR %d:%d]",
1781 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1782 display_aspect_ratio.num, display_aspect_ratio.den);
1784 if(av_log_get_level() >= AV_LOG_DEBUG){
1785 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1786 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1788 enc->time_base.num/g, enc->time_base.den/g);
1792 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1793 ", q=%d-%d", enc->qmin, enc->qmax);
1796 case AVMEDIA_TYPE_AUDIO:
1797 if (enc->sample_rate) {
1798 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1799 ", %d Hz", enc->sample_rate);
1801 av_strlcat(buf, ", ", buf_size);
1802 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1803 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1804 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1805 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1812 if (enc->flags & CODEC_FLAG_PASS1)
1813 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1815 if (enc->flags & CODEC_FLAG_PASS2)
1816 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1819 bitrate = get_bit_rate(enc);
1821 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1822 ", %d kb/s", bitrate / 1000);
1826 const char *av_get_profile_name(const AVCodec *codec, int profile)
1829 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1832 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1833 if (p->profile == profile)
1839 unsigned avcodec_version( void )
1841 // av_assert0(CODEC_ID_V410==164);
1842 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1843 av_assert0(CODEC_ID_ADPCM_G722==69660);
1844 // av_assert0(CODEC_ID_BMV_AUDIO==86071);
1845 av_assert0(CODEC_ID_SRT==94216);
1846 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1848 return LIBAVCODEC_VERSION_INT;
1851 const char *avcodec_configuration(void)
1853 return FFMPEG_CONFIGURATION;
1856 const char *avcodec_license(void)
1858 #define LICENSE_PREFIX "libavcodec license: "
1859 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1862 void avcodec_flush_buffers(AVCodecContext *avctx)
1864 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1865 ff_thread_flush(avctx);
1866 else if(avctx->codec->flush)
1867 avctx->codec->flush(avctx);
1870 static void video_free_buffers(AVCodecContext *s)
1872 AVCodecInternal *avci = s->internal;
1878 if (avci->buffer_count)
1879 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1880 avci->buffer_count);
1881 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1882 InternalBuffer *buf = &avci->buffer[i];
1884 av_freep(&buf->base[j]);
1888 av_freep(&avci->buffer);
1890 avci->buffer_count=0;
1893 static void audio_free_buffers(AVCodecContext *avctx)
1895 AVCodecInternal *avci = avctx->internal;
1896 InternalBuffer *buf;
1902 if (buf->extended_data) {
1903 av_free(buf->extended_data[0]);
1904 if (buf->extended_data != buf->data)
1905 av_freep(&buf->extended_data);
1907 av_freep(&avci->buffer);
1910 void avcodec_default_free_buffers(AVCodecContext *avctx)
1912 switch (avctx->codec_type) {
1913 case AVMEDIA_TYPE_VIDEO:
1914 video_free_buffers(avctx);
1916 case AVMEDIA_TYPE_AUDIO:
1917 audio_free_buffers(avctx);
1924 int av_get_exact_bits_per_sample(enum CodecID codec_id)
1927 case CODEC_ID_ADPCM_CT:
1928 case CODEC_ID_ADPCM_IMA_APC:
1929 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1930 case CODEC_ID_ADPCM_IMA_WS:
1931 case CODEC_ID_ADPCM_G722:
1932 case CODEC_ID_ADPCM_YAMAHA:
1934 case CODEC_ID_PCM_ALAW:
1935 case CODEC_ID_PCM_MULAW:
1936 case CODEC_ID_PCM_S8:
1937 case CODEC_ID_PCM_U8:
1938 case CODEC_ID_PCM_ZORK:
1940 case CODEC_ID_PCM_S16BE:
1941 case CODEC_ID_PCM_S16LE:
1942 case CODEC_ID_PCM_S16LE_PLANAR:
1943 case CODEC_ID_PCM_U16BE:
1944 case CODEC_ID_PCM_U16LE:
1946 case CODEC_ID_PCM_S24DAUD:
1947 case CODEC_ID_PCM_S24BE:
1948 case CODEC_ID_PCM_S24LE:
1949 case CODEC_ID_PCM_U24BE:
1950 case CODEC_ID_PCM_U24LE:
1952 case CODEC_ID_PCM_S32BE:
1953 case CODEC_ID_PCM_S32LE:
1954 case CODEC_ID_PCM_U32BE:
1955 case CODEC_ID_PCM_U32LE:
1956 case CODEC_ID_PCM_F32BE:
1957 case CODEC_ID_PCM_F32LE:
1959 case CODEC_ID_PCM_F64BE:
1960 case CODEC_ID_PCM_F64LE:
1967 enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
1969 static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
1970 [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
1971 [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
1972 [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
1973 [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
1974 [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
1975 [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
1976 [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
1977 [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
1978 [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
1979 [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
1981 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
1982 return CODEC_ID_NONE;
1983 if (be < 0 || be > 1)
1985 return map[fmt][be];
1988 int av_get_bits_per_sample(enum CodecID codec_id)
1991 case CODEC_ID_ADPCM_SBPRO_2:
1993 case CODEC_ID_ADPCM_SBPRO_3:
1995 case CODEC_ID_ADPCM_SBPRO_4:
1996 case CODEC_ID_ADPCM_IMA_WAV:
1997 case CODEC_ID_ADPCM_IMA_QT:
1998 case CODEC_ID_ADPCM_SWF:
1999 case CODEC_ID_ADPCM_MS:
2002 return av_get_exact_bits_per_sample(codec_id);
2006 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2008 int id, sr, ch, ba, tag, bps;
2010 id = avctx->codec_id;
2011 sr = avctx->sample_rate;
2012 ch = avctx->channels;
2013 ba = avctx->block_align;
2014 tag = avctx->codec_tag;
2015 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2017 /* codecs with an exact constant bits per sample */
2018 if (bps > 0 && ch > 0 && frame_bytes > 0)
2019 return (frame_bytes * 8) / (bps * ch);
2020 bps = avctx->bits_per_coded_sample;
2022 /* codecs with a fixed packet duration */
2024 case CODEC_ID_ADPCM_ADX: return 32;
2025 case CODEC_ID_ADPCM_IMA_QT: return 64;
2026 case CODEC_ID_ADPCM_EA_XAS: return 128;
2027 case CODEC_ID_AMR_NB:
2029 case CODEC_ID_QCELP:
2030 case CODEC_ID_RA_144:
2031 case CODEC_ID_RA_288: return 160;
2032 case CODEC_ID_IMC: return 256;
2033 case CODEC_ID_AMR_WB:
2034 case CODEC_ID_GSM_MS: return 320;
2035 case CODEC_ID_MP1: return 384;
2036 case CODEC_ID_ATRAC1: return 512;
2037 case CODEC_ID_ATRAC3: return 1024;
2039 case CODEC_ID_MUSEPACK7: return 1152;
2040 case CODEC_ID_AC3: return 1536;
2044 /* calc from sample rate */
2045 if (id == CODEC_ID_TTA)
2046 return 256 * sr / 245;
2049 /* calc from sample rate and channels */
2050 if (id == CODEC_ID_BINKAUDIO_DCT)
2051 return (480 << (sr / 22050)) / ch;
2056 /* calc from block_align */
2057 if (id == CODEC_ID_SIPR) {
2059 case 20: return 160;
2060 case 19: return 144;
2061 case 29: return 288;
2062 case 37: return 480;
2067 if (frame_bytes > 0) {
2068 /* calc from frame_bytes only */
2069 if (id == CODEC_ID_TRUESPEECH)
2070 return 240 * (frame_bytes / 32);
2071 if (id == CODEC_ID_NELLYMOSER)
2072 return 256 * (frame_bytes / 64);
2075 /* calc from frame_bytes and bits_per_coded_sample */
2076 if (id == CODEC_ID_ADPCM_G726)
2077 return frame_bytes * 8 / bps;
2081 /* calc from frame_bytes and channels */
2083 case CODEC_ID_ADPCM_4XM:
2084 case CODEC_ID_ADPCM_IMA_ISS:
2085 return (frame_bytes - 4 * ch) * 2 / ch;
2086 case CODEC_ID_ADPCM_IMA_SMJPEG:
2087 return (frame_bytes - 4) * 2 / ch;
2088 case CODEC_ID_ADPCM_IMA_AMV:
2089 return (frame_bytes - 8) * 2 / ch;
2090 case CODEC_ID_ADPCM_XA:
2091 return (frame_bytes / 128) * 224 / ch;
2092 case CODEC_ID_INTERPLAY_DPCM:
2093 return (frame_bytes - 6 - ch) / ch;
2094 case CODEC_ID_ROQ_DPCM:
2095 return (frame_bytes - 8) / ch;
2096 case CODEC_ID_XAN_DPCM:
2097 return (frame_bytes - 2 * ch) / ch;
2098 case CODEC_ID_MACE3:
2099 return 3 * frame_bytes / ch;
2100 case CODEC_ID_MACE6:
2101 return 6 * frame_bytes / ch;
2102 case CODEC_ID_PCM_LXF:
2103 return 2 * (frame_bytes / (5 * ch));
2107 /* calc from frame_bytes, channels, and codec_tag */
2108 if (id == CODEC_ID_SOL_DPCM) {
2110 return frame_bytes / ch;
2112 return frame_bytes * 2 / ch;
2117 /* calc from frame_bytes, channels, and block_align */
2118 int blocks = frame_bytes / ba;
2119 switch (avctx->codec_id) {
2120 case CODEC_ID_ADPCM_IMA_WAV:
2121 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2122 case CODEC_ID_ADPCM_IMA_DK3:
2123 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2124 case CODEC_ID_ADPCM_IMA_DK4:
2125 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2126 case CODEC_ID_ADPCM_MS:
2127 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2132 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2133 switch (avctx->codec_id) {
2134 case CODEC_ID_PCM_DVD:
2135 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2136 case CODEC_ID_PCM_BLURAY:
2137 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2138 case CODEC_ID_S302M:
2139 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2149 int ff_thread_init(AVCodecContext *s){
2154 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2168 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
2170 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
2174 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2176 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
2177 "version to the newest one from Git. If the problem still "
2178 "occurs, it means that your file has a feature which has not "
2179 "been implemented.\n", feature);
2181 av_log_ask_for_sample(avc, NULL);
2184 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2186 va_list argument_list;
2188 va_start(argument_list, msg);
2191 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2192 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2193 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2194 "and contact the ffmpeg-devel mailing list.\n");
2196 va_end(argument_list);
2199 static AVHWAccel *first_hwaccel = NULL;
2201 void av_register_hwaccel(AVHWAccel *hwaccel)
2203 AVHWAccel **p = &first_hwaccel;
2207 hwaccel->next = NULL;
2210 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2212 return hwaccel ? hwaccel->next : first_hwaccel;
2215 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
2217 AVHWAccel *hwaccel=NULL;
2219 while((hwaccel= av_hwaccel_next(hwaccel))){
2220 if ( hwaccel->id == codec_id
2221 && hwaccel->pix_fmt == pix_fmt)
2227 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2229 if (ff_lockmgr_cb) {
2230 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2232 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2238 if (ff_lockmgr_cb) {
2239 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2241 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2247 int avpriv_lock_avformat(void)
2249 if (ff_lockmgr_cb) {
2250 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2256 int avpriv_unlock_avformat(void)
2258 if (ff_lockmgr_cb) {
2259 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2265 unsigned int avpriv_toupper4(unsigned int x)
2267 return toupper( x &0xFF)
2268 + (toupper((x>>8 )&0xFF)<<8 )
2269 + (toupper((x>>16)&0xFF)<<16)
2270 + (toupper((x>>24)&0xFF)<<24);
2275 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2279 ff_init_buffer_info(avctx, f);
2281 return avctx->get_buffer(avctx, f);
2284 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2286 f->owner->release_buffer(f->owner, f);
2289 void ff_thread_finish_setup(AVCodecContext *avctx)
2293 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2297 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2301 int ff_thread_can_start_frame(AVCodecContext *avctx)
2308 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2310 AVCodec *c= avcodec_find_decoder(codec_id);
2312 c= avcodec_find_encoder(codec_id);
2316 if (codec_id <= CODEC_ID_NONE)
2317 return AVMEDIA_TYPE_UNKNOWN;
2318 else if (codec_id < CODEC_ID_FIRST_AUDIO)
2319 return AVMEDIA_TYPE_VIDEO;
2320 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2321 return AVMEDIA_TYPE_AUDIO;
2322 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2323 return AVMEDIA_TYPE_SUBTITLE;
2325 return AVMEDIA_TYPE_UNKNOWN;
2328 int avcodec_is_open(AVCodecContext *s)
2330 return !!s->internal;