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_YUVA422P:
182 case PIX_FMT_YUVA444P:
183 case PIX_FMT_YUV420P9LE:
184 case PIX_FMT_YUV420P9BE:
185 case PIX_FMT_YUV420P10LE:
186 case PIX_FMT_YUV420P10BE:
187 case PIX_FMT_YUV422P9LE:
188 case PIX_FMT_YUV422P9BE:
189 case PIX_FMT_YUV422P10LE:
190 case PIX_FMT_YUV422P10BE:
191 case PIX_FMT_YUV444P9LE:
192 case PIX_FMT_YUV444P9BE:
193 case PIX_FMT_YUV444P10LE:
194 case PIX_FMT_YUV444P10BE:
195 case PIX_FMT_GBRP9LE:
196 case PIX_FMT_GBRP9BE:
197 case PIX_FMT_GBRP10LE:
198 case PIX_FMT_GBRP10BE:
199 w_align = 16; //FIXME assume 16 pixel per macroblock
200 h_align = 16 * 2; // interlaced needs 2 macroblocks height
202 case PIX_FMT_YUV411P:
203 case PIX_FMT_UYYVYY411:
207 case PIX_FMT_YUV410P:
208 if(s->codec_id == CODEC_ID_SVQ1){
213 if(s->codec_id == CODEC_ID_RPZA){
220 if(s->codec_id == CODEC_ID_SMC){
226 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
237 if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
238 w_align= FFMAX(w_align, 8);
241 *width = FFALIGN(*width , w_align);
242 *height= FFALIGN(*height, h_align);
243 if(s->codec_id == CODEC_ID_H264 || s->lowres)
244 *height+=2; // some of the optimized chroma MC reads one line too much
245 // which is also done in mpeg decoders with lowres > 0
247 for (i = 0; i < 4; i++)
248 linesize_align[i] = STRIDE_ALIGN;
251 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
252 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
253 int linesize_align[AV_NUM_DATA_POINTERS];
255 avcodec_align_dimensions2(s, width, height, linesize_align);
256 align = FFMAX(linesize_align[0], linesize_align[3]);
257 linesize_align[1] <<= chroma_shift;
258 linesize_align[2] <<= chroma_shift;
259 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
260 *width=FFALIGN(*width, align);
263 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
266 pic->pkt_pts = s->pkt->pts;
267 pic->pkt_pos = s->pkt->pos;
269 pic->pkt_pts = AV_NOPTS_VALUE;
272 pic->reordered_opaque= s->reordered_opaque;
273 pic->sample_aspect_ratio = s->sample_aspect_ratio;
274 pic->width = s->width;
275 pic->height = s->height;
276 pic->format = s->pix_fmt;
279 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
280 enum AVSampleFormat sample_fmt, const uint8_t *buf,
281 int buf_size, int align)
283 int ch, planar, needed_size, ret = 0;
285 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
286 frame->nb_samples, sample_fmt,
288 if (buf_size < needed_size)
289 return AVERROR(EINVAL);
291 planar = av_sample_fmt_is_planar(sample_fmt);
292 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
293 if (!(frame->extended_data = av_mallocz(nb_channels *
294 sizeof(*frame->extended_data))))
295 return AVERROR(ENOMEM);
297 frame->extended_data = frame->data;
300 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
301 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
302 sample_fmt, align)) < 0) {
303 if (frame->extended_data != frame->data)
304 av_freep(&frame->extended_data);
307 if (frame->extended_data != frame->data) {
308 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
309 frame->data[ch] = frame->extended_data[ch];
315 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
317 AVCodecInternal *avci = avctx->internal;
321 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
322 frame->nb_samples, avctx->sample_fmt,
325 return AVERROR(EINVAL);
327 /* allocate InternalBuffer if needed */
329 avci->buffer = av_mallocz(sizeof(InternalBuffer));
331 return AVERROR(ENOMEM);
335 /* if there is a previously-used internal buffer, check its size and
336 channel count to see if we can reuse it */
337 if (buf->extended_data) {
338 /* if current buffer is too small, free it */
339 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
340 av_free(buf->extended_data[0]);
341 if (buf->extended_data != buf->data)
342 av_freep(&buf->extended_data);
343 buf->extended_data = NULL;
346 /* if number of channels has changed, reset and/or free extended data
347 pointers but leave data buffer in buf->data[0] for reuse */
348 if (buf->nb_channels != avctx->channels) {
349 if (buf->extended_data != buf->data)
350 av_free(buf->extended_data);
351 buf->extended_data = NULL;
355 /* if there is no previous buffer or the previous buffer cannot be used
356 as-is, allocate a new buffer and/or rearrange the channel pointers */
357 if (!buf->extended_data) {
359 if (!(buf->data[0] = av_mallocz(buf_size)))
360 return AVERROR(ENOMEM);
361 buf->audio_data_size = buf_size;
363 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
364 avctx->sample_fmt, buf->data[0],
365 buf->audio_data_size, 0)))
368 if (frame->extended_data == frame->data)
369 buf->extended_data = buf->data;
371 buf->extended_data = frame->extended_data;
372 memcpy(buf->data, frame->data, sizeof(frame->data));
373 buf->linesize[0] = frame->linesize[0];
374 buf->nb_channels = avctx->channels;
376 /* copy InternalBuffer info to the AVFrame */
377 frame->extended_data = buf->extended_data;
378 frame->linesize[0] = buf->linesize[0];
379 memcpy(frame->data, buf->data, sizeof(frame->data));
382 frame->type = FF_BUFFER_TYPE_INTERNAL;
385 frame->pkt_pts = avctx->pkt->pts;
386 frame->pkt_pos = avctx->pkt->pos;
388 frame->pkt_pts = AV_NOPTS_VALUE;
392 frame->reordered_opaque = avctx->reordered_opaque;
394 frame->sample_rate = avctx->sample_rate;
395 frame->format = avctx->sample_fmt;
396 frame->channel_layout = avctx->channel_layout;
398 if (avctx->debug & FF_DEBUG_BUFFERS)
399 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
400 "internal audio buffer used\n", frame);
405 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
411 AVCodecInternal *avci = s->internal;
413 if(pic->data[0]!=NULL) {
414 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
417 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
418 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
422 if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
426 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
427 sizeof(InternalBuffer));
430 buf = &avci->buffer[avci->buffer_count];
432 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
433 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
434 av_freep(&buf->base[i]);
440 int h_chroma_shift, v_chroma_shift;
445 int stride_align[AV_NUM_DATA_POINTERS];
446 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
448 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
450 avcodec_align_dimensions2(s, &w, &h, stride_align);
452 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
458 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
459 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
460 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
461 // increase alignment of w for next try (rhs gives the lowest bit set in w)
466 unaligned |= picture.linesize[i] % stride_align[i];
470 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
474 for (i=0; i<3 && picture.data[i+1]; i++)
475 size[i] = picture.data[i+1] - picture.data[i];
476 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
478 memset(buf->base, 0, sizeof(buf->base));
479 memset(buf->data, 0, sizeof(buf->data));
481 for(i=0; i<4 && size[i]; i++){
482 const int h_shift= i==0 ? 0 : h_chroma_shift;
483 const int v_shift= i==0 ? 0 : v_chroma_shift;
485 buf->linesize[i]= picture.linesize[i];
487 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
488 if(buf->base[i]==NULL) return -1;
489 memset(buf->base[i], 128, size[i]);
491 // no edge if EDGE EMU or not planar YUV
492 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
493 buf->data[i] = buf->base[i];
495 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
497 for (; i < AV_NUM_DATA_POINTERS; i++) {
498 buf->base[i] = buf->data[i] = NULL;
499 buf->linesize[i] = 0;
501 if(size[1] && !size[2])
502 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
503 buf->width = s->width;
504 buf->height = s->height;
505 buf->pix_fmt= s->pix_fmt;
507 pic->type= FF_BUFFER_TYPE_INTERNAL;
509 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
510 pic->base[i]= buf->base[i];
511 pic->data[i]= buf->data[i];
512 pic->linesize[i]= buf->linesize[i];
514 pic->extended_data = pic->data;
515 avci->buffer_count++;
516 pic->width = buf->width;
517 pic->height = buf->height;
518 pic->format = buf->pix_fmt;
519 pic->sample_aspect_ratio = s->sample_aspect_ratio;
522 pic->pkt_pts = s->pkt->pts;
523 pic->pkt_pos = s->pkt->pos;
525 pic->pkt_pts = AV_NOPTS_VALUE;
528 pic->reordered_opaque= s->reordered_opaque;
529 pic->sample_aspect_ratio = s->sample_aspect_ratio;
530 pic->width = s->width;
531 pic->height = s->height;
532 pic->format = s->pix_fmt;
534 if(s->debug&FF_DEBUG_BUFFERS)
535 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
536 "buffers used\n", pic, avci->buffer_count);
541 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
543 switch (avctx->codec_type) {
544 case AVMEDIA_TYPE_VIDEO:
545 return video_get_buffer(avctx, frame);
546 case AVMEDIA_TYPE_AUDIO:
547 return audio_get_buffer(avctx, frame);
553 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
555 InternalBuffer *buf, *last;
556 AVCodecInternal *avci = s->internal;
558 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
560 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
561 assert(avci->buffer_count);
564 buf = NULL; /* avoids warning */
565 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
566 buf = &avci->buffer[i];
567 if (buf->data[0] == pic->data[0])
570 av_assert0(i < avci->buffer_count);
571 avci->buffer_count--;
572 last = &avci->buffer[avci->buffer_count];
575 FFSWAP(InternalBuffer, *buf, *last);
578 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
580 // pic->base[i]=NULL;
582 //printf("R%X\n", pic->opaque);
584 if(s->debug&FF_DEBUG_BUFFERS)
585 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
586 "buffers used\n", pic, avci->buffer_count);
589 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
593 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
595 if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
596 av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
597 pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
598 s->release_buffer(s, pic);
601 ff_init_buffer_info(s, pic);
603 /* If no picture return a new buffer */
604 if(pic->data[0] == NULL) {
605 /* We will copy from buffer, so must be readable */
606 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
607 return s->get_buffer(s, pic);
610 assert(s->pix_fmt == pic->format);
612 /* If internal buffer type return the same buffer */
613 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
618 * Not internal type and reget_buffer not overridden, emulate cr buffer
621 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
622 pic->data[i] = pic->base[i] = NULL;
624 /* Allocate new frame */
625 if (s->get_buffer(s, pic))
627 /* Copy image data from old buffer to new buffer */
628 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
630 s->release_buffer(s, &temp_pic); // Release old frame
634 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
637 for(i=0; i<count; i++){
638 int r= func(c, (char*)arg + i*size);
644 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
647 for(i=0; i<count; i++){
648 int r= func(c, arg, i, 0);
654 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
655 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
660 void avcodec_get_frame_defaults(AVFrame *pic){
661 memset(pic, 0, sizeof(AVFrame));
663 pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
666 pic->sample_aspect_ratio = (AVRational){0, 1};
667 pic->format = -1; /* unknown */
670 AVFrame *avcodec_alloc_frame(void){
671 AVFrame *pic= av_malloc(sizeof(AVFrame));
673 if(pic==NULL) return NULL;
675 avcodec_get_frame_defaults(pic);
680 #define MAKE_ACCESSORS(str, name, type, field) \
681 type av_##name##_get_##field(const str *s) { return s->field; } \
682 void av_##name##_set_##field(str *s, type v) { s->field = v; }
684 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
685 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
686 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
687 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
689 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
691 memset(sub, 0, sizeof(*sub));
692 sub->pts = AV_NOPTS_VALUE;
695 static int get_bit_rate(AVCodecContext *ctx)
700 switch(ctx->codec_type) {
701 case AVMEDIA_TYPE_VIDEO:
702 case AVMEDIA_TYPE_DATA:
703 case AVMEDIA_TYPE_SUBTITLE:
704 case AVMEDIA_TYPE_ATTACHMENT:
705 bit_rate = ctx->bit_rate;
707 case AVMEDIA_TYPE_AUDIO:
708 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
709 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
718 #if FF_API_AVCODEC_OPEN
719 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
721 return avcodec_open2(avctx, codec, NULL);
725 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
728 AVDictionary *tmp = NULL;
730 if (avcodec_is_open(avctx))
733 if ((!codec && !avctx->codec)) {
734 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
735 return AVERROR(EINVAL);
737 if ((codec && avctx->codec && codec != avctx->codec)) {
738 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
739 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
740 return AVERROR(EINVAL);
743 codec = avctx->codec;
745 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
746 return AVERROR(EINVAL);
749 av_dict_copy(&tmp, *options, 0);
751 /* If there is a user-supplied mutex locking routine, call it. */
753 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
757 entangled_thread_counter++;
758 if(entangled_thread_counter != 1){
759 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
764 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
765 if (!avctx->internal) {
766 ret = AVERROR(ENOMEM);
770 if (codec->priv_data_size > 0) {
771 if(!avctx->priv_data){
772 avctx->priv_data = av_mallocz(codec->priv_data_size);
773 if (!avctx->priv_data) {
774 ret = AVERROR(ENOMEM);
777 if (codec->priv_class) {
778 *(const AVClass**)avctx->priv_data= codec->priv_class;
779 av_opt_set_defaults(avctx->priv_data);
782 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
785 avctx->priv_data = NULL;
787 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
790 if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
791 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
792 av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
797 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
798 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
799 if(avctx->coded_width && avctx->coded_height)
800 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
801 else if(avctx->width && avctx->height)
802 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
805 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
806 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
807 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
808 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
809 avcodec_set_dimensions(avctx, 0, 0);
812 /* if the decoder init function was already called previously,
813 free the already allocated subtitle_header before overwriting it */
814 if (av_codec_is_decoder(codec))
815 av_freep(&avctx->subtitle_header);
817 #define SANE_NB_CHANNELS 128U
818 if (avctx->channels > SANE_NB_CHANNELS) {
819 ret = AVERROR(EINVAL);
823 avctx->codec = codec;
824 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
825 avctx->codec_id == CODEC_ID_NONE) {
826 avctx->codec_type = codec->type;
827 avctx->codec_id = codec->id;
829 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
830 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
831 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
832 ret = AVERROR(EINVAL);
835 avctx->frame_number = 0;
837 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
838 (!avctx->time_base.num || !avctx->time_base.den)) {
839 avctx->time_base.num = 1;
840 avctx->time_base.den = avctx->sample_rate;
844 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
846 if (HAVE_THREADS && !avctx->thread_opaque) {
847 ret = ff_thread_init(avctx);
852 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
853 avctx->thread_count = 1;
855 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
856 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
857 avctx->codec->max_lowres);
858 ret = AVERROR(EINVAL);
862 if (av_codec_is_encoder(avctx->codec)) {
864 if (avctx->codec->sample_fmts) {
865 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
866 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
868 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
869 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
870 ret = AVERROR(EINVAL);
874 if (avctx->codec->pix_fmts) {
875 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
876 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
878 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
879 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
880 ret = AVERROR(EINVAL);
884 if (avctx->codec->supported_samplerates) {
885 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
886 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
888 if (avctx->codec->supported_samplerates[i] == 0) {
889 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
890 ret = AVERROR(EINVAL);
894 if (avctx->codec->channel_layouts) {
895 if (!avctx->channel_layout) {
896 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
898 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
899 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
901 if (avctx->codec->channel_layouts[i] == 0) {
902 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
903 ret = AVERROR(EINVAL);
908 if (avctx->channel_layout && avctx->channels) {
909 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
910 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
911 ret = AVERROR(EINVAL);
914 } else if (avctx->channel_layout) {
915 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
919 avctx->pts_correction_num_faulty_pts =
920 avctx->pts_correction_num_faulty_dts = 0;
921 avctx->pts_correction_last_pts =
922 avctx->pts_correction_last_dts = INT64_MIN;
924 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
925 ret = avctx->codec->init(avctx);
933 if (av_codec_is_decoder(avctx->codec)) {
934 if (!avctx->bit_rate)
935 avctx->bit_rate = get_bit_rate(avctx);
936 /* validate channel layout from the decoder */
937 if (avctx->channel_layout &&
938 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
939 av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
940 avctx->channel_layout = 0;
944 entangled_thread_counter--;
946 /* Release any user-supplied mutex. */
948 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
951 av_dict_free(options);
958 av_freep(&avctx->priv_data);
959 av_freep(&avctx->internal);
964 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
966 if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
967 av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
968 return AVERROR(EINVAL);
971 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
972 if (!avpkt->data || avpkt->size < size) {
973 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
974 avpkt->data = avctx->internal->byte_buffer;
975 avpkt->size = avctx->internal->byte_buffer_size;
976 avpkt->destruct = NULL;
980 void *destruct = avpkt->destruct;
982 if (avpkt->size < size) {
983 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
984 return AVERROR(EINVAL);
987 av_init_packet(avpkt);
988 avpkt->destruct = destruct;
992 int ret = av_new_packet(avpkt, size);
994 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
999 int ff_alloc_packet(AVPacket *avpkt, int size)
1001 return ff_alloc_packet2(NULL, avpkt, size);
1005 * Pad last frame with silence.
1007 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1009 AVFrame *frame = NULL;
1010 uint8_t *buf = NULL;
1013 if (!(frame = avcodec_alloc_frame()))
1014 return AVERROR(ENOMEM);
1017 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1018 s->frame_size, s->sample_fmt, 0)) < 0)
1021 if (!(buf = av_malloc(ret))) {
1022 ret = AVERROR(ENOMEM);
1026 frame->nb_samples = s->frame_size;
1027 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1030 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1031 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1033 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1034 frame->nb_samples - src->nb_samples,
1035 s->channels, s->sample_fmt)) < 0)
1043 if (frame->extended_data != frame->data)
1044 av_freep(&frame->extended_data);
1050 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1052 const AVFrame *frame,
1053 int *got_packet_ptr)
1056 AVFrame *padded_frame = NULL;
1058 AVPacket user_pkt = *avpkt;
1059 int needs_realloc = !user_pkt.data;
1061 *got_packet_ptr = 0;
1063 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1064 av_free_packet(avpkt);
1065 av_init_packet(avpkt);
1069 /* ensure that extended_data is properly set */
1070 if (frame && !frame->extended_data) {
1071 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1072 avctx->channels > AV_NUM_DATA_POINTERS) {
1073 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1074 "with more than %d channels, but extended_data is not set.\n",
1075 AV_NUM_DATA_POINTERS);
1076 return AVERROR(EINVAL);
1078 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1081 tmp.extended_data = tmp.data;
1085 /* check for valid frame size */
1087 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1088 if (frame->nb_samples > avctx->frame_size)
1089 return AVERROR(EINVAL);
1090 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1091 if (frame->nb_samples < avctx->frame_size &&
1092 !avctx->internal->last_audio_frame) {
1093 ret = pad_last_frame(avctx, &padded_frame, frame);
1097 frame = padded_frame;
1098 avctx->internal->last_audio_frame = 1;
1101 if (frame->nb_samples != avctx->frame_size)
1102 return AVERROR(EINVAL);
1106 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1108 if (*got_packet_ptr) {
1109 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1110 if (avpkt->pts == AV_NOPTS_VALUE)
1111 avpkt->pts = frame->pts;
1112 if (!avpkt->duration)
1113 avpkt->duration = ff_samples_to_time_base(avctx,
1116 avpkt->dts = avpkt->pts;
1121 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1123 if (user_pkt.data) {
1124 if (user_pkt.size >= avpkt->size) {
1125 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1127 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1128 avpkt->size = user_pkt.size;
1131 avpkt->data = user_pkt.data;
1132 avpkt->destruct = user_pkt.destruct;
1134 if (av_dup_packet(avpkt) < 0) {
1135 ret = AVERROR(ENOMEM);
1141 if (needs_realloc && avpkt->data) {
1142 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1144 avpkt->data = new_data;
1147 avctx->frame_number++;
1150 if (ret < 0 || !*got_packet_ptr) {
1151 av_free_packet(avpkt);
1152 av_init_packet(avpkt);
1156 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1157 this needs to be moved to the encoders, but for now we can do it
1158 here to simplify things */
1159 avpkt->flags |= AV_PKT_FLAG_KEY;
1162 av_freep(&padded_frame->data[0]);
1163 if (padded_frame->extended_data != padded_frame->data)
1164 av_freep(&padded_frame->extended_data);
1165 av_freep(&padded_frame);
1171 #if FF_API_OLD_DECODE_AUDIO
1172 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1173 uint8_t *buf, int buf_size,
1174 const short *samples)
1179 int ret, samples_size, got_packet;
1181 av_init_packet(&pkt);
1183 pkt.size = buf_size;
1187 avcodec_get_frame_defaults(frame);
1189 if (avctx->frame_size) {
1190 frame->nb_samples = avctx->frame_size;
1192 /* if frame_size is not set, the number of samples must be
1193 calculated from the buffer size */
1195 if (!av_get_bits_per_sample(avctx->codec_id)) {
1196 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1197 "support this codec\n");
1198 return AVERROR(EINVAL);
1200 nb_samples = (int64_t)buf_size * 8 /
1201 (av_get_bits_per_sample(avctx->codec_id) *
1203 if (nb_samples >= INT_MAX)
1204 return AVERROR(EINVAL);
1205 frame->nb_samples = nb_samples;
1208 /* it is assumed that the samples buffer is large enough based on the
1209 relevant parameters */
1210 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1212 avctx->sample_fmt, 1);
1213 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1215 (const uint8_t *) samples,
1219 /* fabricate frame pts from sample count.
1220 this is needed because the avcodec_encode_audio() API does not have
1221 a way for the user to provide pts */
1222 if(avctx->sample_rate && avctx->time_base.num)
1223 frame->pts = ff_samples_to_time_base(avctx,
1224 avctx->internal->sample_count);
1226 frame->pts = AV_NOPTS_VALUE;
1227 avctx->internal->sample_count += frame->nb_samples;
1233 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1234 if (!ret && got_packet && avctx->coded_frame) {
1235 avctx->coded_frame->pts = pkt.pts;
1236 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1238 /* free any side data since we cannot return it */
1239 ff_packet_free_side_data(&pkt);
1241 if (frame && frame->extended_data != frame->data)
1242 av_freep(&frame->extended_data);
1244 return ret ? ret : pkt.size;
1248 #if FF_API_OLD_ENCODE_VIDEO
1249 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1250 const AVFrame *pict)
1253 int ret, got_packet = 0;
1255 if(buf_size < FF_MIN_BUFFER_SIZE){
1256 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1260 av_init_packet(&pkt);
1262 pkt.size = buf_size;
1264 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1265 if (!ret && got_packet && avctx->coded_frame) {
1266 avctx->coded_frame->pts = pkt.pts;
1267 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1270 /* free any side data since we cannot return it */
1271 if (pkt.side_data_elems > 0) {
1273 for (i = 0; i < pkt.side_data_elems; i++)
1274 av_free(pkt.side_data[i].data);
1275 av_freep(&pkt.side_data);
1276 pkt.side_data_elems = 0;
1279 return ret ? ret : pkt.size;
1283 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1285 const AVFrame *frame,
1286 int *got_packet_ptr)
1289 AVPacket user_pkt = *avpkt;
1290 int needs_realloc = !user_pkt.data;
1292 *got_packet_ptr = 0;
1294 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1295 av_free_packet(avpkt);
1296 av_init_packet(avpkt);
1301 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1302 return AVERROR(EINVAL);
1304 av_assert0(avctx->codec->encode2);
1306 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1307 av_assert0(ret <= 0);
1309 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1311 if (user_pkt.data) {
1312 if (user_pkt.size >= avpkt->size) {
1313 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1315 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1316 avpkt->size = user_pkt.size;
1319 avpkt->data = user_pkt.data;
1320 avpkt->destruct = user_pkt.destruct;
1322 if (av_dup_packet(avpkt) < 0) {
1323 ret = AVERROR(ENOMEM);
1329 if (!*got_packet_ptr)
1331 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1332 avpkt->pts = avpkt->dts = frame->pts;
1334 if (needs_realloc && avpkt->data &&
1335 avpkt->destruct == av_destruct_packet) {
1336 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1338 avpkt->data = new_data;
1341 avctx->frame_number++;
1344 if (ret < 0 || !*got_packet_ptr)
1345 av_free_packet(avpkt);
1351 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1352 const AVSubtitle *sub)
1355 if(sub->start_display_time) {
1356 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1360 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
1361 avctx->frame_number++;
1366 * Attempt to guess proper monotonic timestamps for decoded video frames
1367 * which might have incorrect times. Input timestamps may wrap around, in
1368 * which case the output will as well.
1370 * @param pts the pts field of the decoded AVPacket, as passed through
1372 * @param dts the dts field of the decoded AVPacket
1373 * @return one of the input values, may be AV_NOPTS_VALUE
1375 static int64_t guess_correct_pts(AVCodecContext *ctx,
1376 int64_t reordered_pts, int64_t dts)
1378 int64_t pts = AV_NOPTS_VALUE;
1380 if (dts != AV_NOPTS_VALUE) {
1381 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1382 ctx->pts_correction_last_dts = dts;
1384 if (reordered_pts != AV_NOPTS_VALUE) {
1385 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1386 ctx->pts_correction_last_pts = reordered_pts;
1388 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1389 && reordered_pts != AV_NOPTS_VALUE)
1390 pts = reordered_pts;
1397 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1400 const uint8_t *data;
1403 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1406 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1407 if (!data || size < 4)
1409 flags = bytestream_get_le32(&data);
1411 if (size < 4) /* Required for any of the changes */
1413 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1414 avctx->channels = bytestream_get_le32(&data);
1417 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1420 avctx->channel_layout = bytestream_get_le64(&data);
1425 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1426 avctx->sample_rate = bytestream_get_le32(&data);
1429 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1432 avctx->width = bytestream_get_le32(&data);
1433 avctx->height = bytestream_get_le32(&data);
1434 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1439 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1440 int *got_picture_ptr,
1441 const AVPacket *avpkt)
1444 // copy to ensure we do not change avpkt
1445 AVPacket tmp = *avpkt;
1447 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1448 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1449 return AVERROR(EINVAL);
1452 *got_picture_ptr= 0;
1453 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1456 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1457 int did_split = av_packet_split_side_data(&tmp);
1458 apply_param_change(avctx, &tmp);
1460 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1461 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1464 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1466 picture->pkt_dts= avpkt->dts;
1468 if(!avctx->has_b_frames){
1469 picture->pkt_pos= avpkt->pos;
1471 //FIXME these should be under if(!avctx->has_b_frames)
1472 if (!picture->sample_aspect_ratio.num)
1473 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1474 if (!picture->width)
1475 picture->width = avctx->width;
1476 if (!picture->height)
1477 picture->height = avctx->height;
1478 if (picture->format == PIX_FMT_NONE)
1479 picture->format = avctx->pix_fmt;
1482 emms_c(); //needed to avoid an emms_c() call before every return;
1486 ff_packet_free_side_data(&tmp);
1491 if (*got_picture_ptr){
1492 avctx->frame_number++;
1493 picture->best_effort_timestamp = guess_correct_pts(avctx,
1503 #if FF_API_OLD_DECODE_AUDIO
1504 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1505 int *frame_size_ptr,
1509 int ret, got_frame = 0;
1511 if (avctx->get_buffer != avcodec_default_get_buffer) {
1512 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1513 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1514 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1515 "avcodec_decode_audio4()\n");
1516 avctx->get_buffer = avcodec_default_get_buffer;
1517 avctx->release_buffer = avcodec_default_release_buffer;
1520 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1522 if (ret >= 0 && got_frame) {
1524 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1525 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1527 avctx->sample_fmt, 1);
1528 if (*frame_size_ptr < data_size) {
1529 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1530 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1531 return AVERROR(EINVAL);
1534 memcpy(samples, frame.extended_data[0], plane_size);
1536 if (planar && avctx->channels > 1) {
1537 uint8_t *out = ((uint8_t *)samples) + plane_size;
1538 for (ch = 1; ch < avctx->channels; ch++) {
1539 memcpy(out, frame.extended_data[ch], plane_size);
1543 *frame_size_ptr = data_size;
1545 *frame_size_ptr = 0;
1551 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1554 const AVPacket *avpkt)
1560 if (!avpkt->data && avpkt->size) {
1561 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1562 return AVERROR(EINVAL);
1564 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1565 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1566 return AVERROR(EINVAL);
1569 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1570 // copy to ensure we do not change avpkt
1571 AVPacket tmp = *avpkt;
1572 int did_split = av_packet_split_side_data(&tmp);
1573 apply_param_change(avctx, &tmp);
1576 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1577 if (ret >= 0 && *got_frame_ptr) {
1578 avctx->frame_number++;
1579 frame->pkt_dts = avpkt->dts;
1580 if (frame->format == AV_SAMPLE_FMT_NONE)
1581 frame->format = avctx->sample_fmt;
1582 if (!frame->channel_layout)
1583 frame->channel_layout = avctx->channel_layout;
1584 if (!frame->sample_rate)
1585 frame->sample_rate = avctx->sample_rate;
1590 ff_packet_free_side_data(&tmp);
1598 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1604 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1605 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1606 return AVERROR(EINVAL);
1611 avcodec_get_subtitle_defaults(sub);
1612 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1614 avctx->frame_number++;
1618 void avsubtitle_free(AVSubtitle *sub)
1622 for (i = 0; i < sub->num_rects; i++)
1624 av_freep(&sub->rects[i]->pict.data[0]);
1625 av_freep(&sub->rects[i]->pict.data[1]);
1626 av_freep(&sub->rects[i]->pict.data[2]);
1627 av_freep(&sub->rects[i]->pict.data[3]);
1628 av_freep(&sub->rects[i]->text);
1629 av_freep(&sub->rects[i]->ass);
1630 av_freep(&sub->rects[i]);
1633 av_freep(&sub->rects);
1635 memset(sub, 0, sizeof(AVSubtitle));
1638 av_cold int avcodec_close(AVCodecContext *avctx)
1640 /* If there is a user-supplied mutex locking routine, call it. */
1641 if (ff_lockmgr_cb) {
1642 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1646 entangled_thread_counter++;
1647 if(entangled_thread_counter != 1){
1648 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1649 entangled_thread_counter--;
1653 if (avcodec_is_open(avctx)) {
1654 if (HAVE_THREADS && avctx->thread_opaque)
1655 ff_thread_free(avctx);
1656 if (avctx->codec && avctx->codec->close)
1657 avctx->codec->close(avctx);
1658 avcodec_default_free_buffers(avctx);
1659 avctx->coded_frame = NULL;
1660 avctx->internal->byte_buffer_size = 0;
1661 av_freep(&avctx->internal->byte_buffer);
1662 av_freep(&avctx->internal);
1665 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1666 av_opt_free(avctx->priv_data);
1668 av_freep(&avctx->priv_data);
1669 if (av_codec_is_encoder(avctx->codec))
1670 av_freep(&avctx->extradata);
1671 avctx->codec = NULL;
1672 avctx->active_thread_type = 0;
1673 entangled_thread_counter--;
1675 /* Release any user-supplied mutex. */
1676 if (ff_lockmgr_cb) {
1677 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1682 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1685 //This is for future deprecatec codec ids, its empty since
1686 //last major bump but will fill up again over time, please dont remove it
1687 // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1688 default : return id;
1692 AVCodec *avcodec_find_encoder(enum CodecID id)
1694 AVCodec *p, *experimental=NULL;
1696 id= remap_deprecated_codec_id(id);
1698 if (av_codec_is_encoder(p) && p->id == id) {
1699 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1706 return experimental;
1709 AVCodec *avcodec_find_encoder_by_name(const char *name)
1716 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1723 AVCodec *avcodec_find_decoder(enum CodecID id)
1725 AVCodec *p, *experimental=NULL;
1727 id= remap_deprecated_codec_id(id);
1729 if (av_codec_is_decoder(p) && p->id == id) {
1730 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1737 return experimental;
1740 AVCodec *avcodec_find_decoder_by_name(const char *name)
1747 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1754 const char *avcodec_get_name(enum CodecID id)
1760 #include "libavcodec/codec_names.h"
1762 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1764 codec = avcodec_find_decoder(id);
1767 codec = avcodec_find_encoder(id);
1770 return "unknown_codec";
1773 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1775 int i, len, ret = 0;
1777 #define IS_PRINT(x) \
1778 (((x) >= '0' && (x) <= '9') || \
1779 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1780 ((x) == '.' || (x) == ' '))
1782 for (i = 0; i < 4; i++) {
1783 len = snprintf(buf, buf_size,
1784 IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1786 buf_size = buf_size > len ? buf_size - len : 0;
1793 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1795 const char *codec_type;
1796 const char *codec_name;
1797 const char *profile = NULL;
1800 AVRational display_aspect_ratio;
1802 if (!buf || buf_size <= 0)
1804 codec_type = av_get_media_type_string(enc->codec_type);
1805 codec_name = avcodec_get_name(enc->codec_id);
1806 if (enc->profile != FF_PROFILE_UNKNOWN) {
1810 p = encode ? avcodec_find_encoder(enc->codec_id) :
1811 avcodec_find_decoder(enc->codec_id);
1813 profile = av_get_profile_name(p, enc->profile);
1816 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1817 codec_name, enc->mb_decision ? " (hq)" : "");
1818 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1820 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1821 if (enc->codec_tag) {
1823 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1824 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1825 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1827 switch(enc->codec_type) {
1828 case AVMEDIA_TYPE_VIDEO:
1829 if (enc->pix_fmt != PIX_FMT_NONE) {
1830 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1832 av_get_pix_fmt_name(enc->pix_fmt));
1835 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1837 enc->width, enc->height);
1838 if (enc->sample_aspect_ratio.num) {
1839 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1840 enc->width*enc->sample_aspect_ratio.num,
1841 enc->height*enc->sample_aspect_ratio.den,
1843 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1844 " [SAR %d:%d DAR %d:%d]",
1845 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1846 display_aspect_ratio.num, display_aspect_ratio.den);
1848 if(av_log_get_level() >= AV_LOG_DEBUG){
1849 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1850 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1852 enc->time_base.num/g, enc->time_base.den/g);
1856 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1857 ", q=%d-%d", enc->qmin, enc->qmax);
1860 case AVMEDIA_TYPE_AUDIO:
1861 if (enc->sample_rate) {
1862 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1863 ", %d Hz", enc->sample_rate);
1865 av_strlcat(buf, ", ", buf_size);
1866 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1867 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1868 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1869 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1876 if (enc->flags & CODEC_FLAG_PASS1)
1877 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1879 if (enc->flags & CODEC_FLAG_PASS2)
1880 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1883 bitrate = get_bit_rate(enc);
1885 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1886 ", %d kb/s", bitrate / 1000);
1890 const char *av_get_profile_name(const AVCodec *codec, int profile)
1893 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1896 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1897 if (p->profile == profile)
1903 unsigned avcodec_version( void )
1905 // av_assert0(CODEC_ID_V410==164);
1906 av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1907 av_assert0(CODEC_ID_ADPCM_G722==69660);
1908 // av_assert0(CODEC_ID_BMV_AUDIO==86071);
1909 av_assert0(CODEC_ID_SRT==94216);
1910 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1912 return LIBAVCODEC_VERSION_INT;
1915 const char *avcodec_configuration(void)
1917 return FFMPEG_CONFIGURATION;
1920 const char *avcodec_license(void)
1922 #define LICENSE_PREFIX "libavcodec license: "
1923 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1926 void avcodec_flush_buffers(AVCodecContext *avctx)
1928 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1929 ff_thread_flush(avctx);
1930 else if(avctx->codec->flush)
1931 avctx->codec->flush(avctx);
1933 avctx->pts_correction_last_pts =
1934 avctx->pts_correction_last_dts = INT64_MIN;
1937 static void video_free_buffers(AVCodecContext *s)
1939 AVCodecInternal *avci = s->internal;
1945 if (avci->buffer_count)
1946 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1947 avci->buffer_count);
1948 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1949 InternalBuffer *buf = &avci->buffer[i];
1951 av_freep(&buf->base[j]);
1955 av_freep(&avci->buffer);
1957 avci->buffer_count=0;
1960 static void audio_free_buffers(AVCodecContext *avctx)
1962 AVCodecInternal *avci = avctx->internal;
1963 InternalBuffer *buf;
1969 if (buf->extended_data) {
1970 av_free(buf->extended_data[0]);
1971 if (buf->extended_data != buf->data)
1972 av_freep(&buf->extended_data);
1974 av_freep(&avci->buffer);
1977 void avcodec_default_free_buffers(AVCodecContext *avctx)
1979 switch (avctx->codec_type) {
1980 case AVMEDIA_TYPE_VIDEO:
1981 video_free_buffers(avctx);
1983 case AVMEDIA_TYPE_AUDIO:
1984 audio_free_buffers(avctx);
1991 int av_get_exact_bits_per_sample(enum CodecID codec_id)
1994 case CODEC_ID_ADPCM_CT:
1995 case CODEC_ID_ADPCM_IMA_APC:
1996 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1997 case CODEC_ID_ADPCM_IMA_WS:
1998 case CODEC_ID_ADPCM_G722:
1999 case CODEC_ID_ADPCM_YAMAHA:
2001 case CODEC_ID_PCM_ALAW:
2002 case CODEC_ID_PCM_MULAW:
2003 case CODEC_ID_PCM_S8:
2004 case CODEC_ID_PCM_U8:
2005 case CODEC_ID_PCM_ZORK:
2007 case CODEC_ID_PCM_S16BE:
2008 case CODEC_ID_PCM_S16LE:
2009 case CODEC_ID_PCM_S16LE_PLANAR:
2010 case CODEC_ID_PCM_U16BE:
2011 case CODEC_ID_PCM_U16LE:
2013 case CODEC_ID_PCM_S24DAUD:
2014 case CODEC_ID_PCM_S24BE:
2015 case CODEC_ID_PCM_S24LE:
2016 case CODEC_ID_PCM_U24BE:
2017 case CODEC_ID_PCM_U24LE:
2019 case CODEC_ID_PCM_S32BE:
2020 case CODEC_ID_PCM_S32LE:
2021 case CODEC_ID_PCM_U32BE:
2022 case CODEC_ID_PCM_U32LE:
2023 case CODEC_ID_PCM_F32BE:
2024 case CODEC_ID_PCM_F32LE:
2026 case CODEC_ID_PCM_F64BE:
2027 case CODEC_ID_PCM_F64LE:
2034 enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2036 static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
2037 [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
2038 [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
2039 [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
2040 [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
2041 [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
2042 [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
2043 [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
2044 [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
2045 [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
2046 [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
2048 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2049 return CODEC_ID_NONE;
2050 if (be < 0 || be > 1)
2052 return map[fmt][be];
2055 int av_get_bits_per_sample(enum CodecID codec_id)
2058 case CODEC_ID_ADPCM_SBPRO_2:
2060 case CODEC_ID_ADPCM_SBPRO_3:
2062 case CODEC_ID_ADPCM_SBPRO_4:
2063 case CODEC_ID_ADPCM_IMA_WAV:
2064 case CODEC_ID_ADPCM_IMA_QT:
2065 case CODEC_ID_ADPCM_SWF:
2066 case CODEC_ID_ADPCM_MS:
2069 return av_get_exact_bits_per_sample(codec_id);
2073 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2075 int id, sr, ch, ba, tag, bps;
2077 id = avctx->codec_id;
2078 sr = avctx->sample_rate;
2079 ch = avctx->channels;
2080 ba = avctx->block_align;
2081 tag = avctx->codec_tag;
2082 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2084 /* codecs with an exact constant bits per sample */
2085 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2086 return (frame_bytes * 8LL) / (bps * ch);
2087 bps = avctx->bits_per_coded_sample;
2089 /* codecs with a fixed packet duration */
2091 case CODEC_ID_ADPCM_ADX: return 32;
2092 case CODEC_ID_ADPCM_IMA_QT: return 64;
2093 case CODEC_ID_ADPCM_EA_XAS: return 128;
2094 case CODEC_ID_AMR_NB:
2096 case CODEC_ID_QCELP:
2097 case CODEC_ID_RA_144:
2098 case CODEC_ID_RA_288: return 160;
2099 case CODEC_ID_IMC: return 256;
2100 case CODEC_ID_AMR_WB:
2101 case CODEC_ID_GSM_MS: return 320;
2102 case CODEC_ID_MP1: return 384;
2103 case CODEC_ID_ATRAC1: return 512;
2104 case CODEC_ID_ATRAC3: return 1024;
2106 case CODEC_ID_MUSEPACK7: return 1152;
2107 case CODEC_ID_AC3: return 1536;
2111 /* calc from sample rate */
2112 if (id == CODEC_ID_TTA)
2113 return 256 * sr / 245;
2116 /* calc from sample rate and channels */
2117 if (id == CODEC_ID_BINKAUDIO_DCT)
2118 return (480 << (sr / 22050)) / ch;
2123 /* calc from block_align */
2124 if (id == CODEC_ID_SIPR) {
2126 case 20: return 160;
2127 case 19: return 144;
2128 case 29: return 288;
2129 case 37: return 480;
2134 if (frame_bytes > 0) {
2135 /* calc from frame_bytes only */
2136 if (id == CODEC_ID_TRUESPEECH)
2137 return 240 * (frame_bytes / 32);
2138 if (id == CODEC_ID_NELLYMOSER)
2139 return 256 * (frame_bytes / 64);
2142 /* calc from frame_bytes and bits_per_coded_sample */
2143 if (id == CODEC_ID_ADPCM_G726)
2144 return frame_bytes * 8 / bps;
2148 /* calc from frame_bytes and channels */
2150 case CODEC_ID_ADPCM_4XM:
2151 case CODEC_ID_ADPCM_IMA_ISS:
2152 return (frame_bytes - 4 * ch) * 2 / ch;
2153 case CODEC_ID_ADPCM_IMA_SMJPEG:
2154 return (frame_bytes - 4) * 2 / ch;
2155 case CODEC_ID_ADPCM_IMA_AMV:
2156 return (frame_bytes - 8) * 2 / ch;
2157 case CODEC_ID_ADPCM_XA:
2158 return (frame_bytes / 128) * 224 / ch;
2159 case CODEC_ID_INTERPLAY_DPCM:
2160 return (frame_bytes - 6 - ch) / ch;
2161 case CODEC_ID_ROQ_DPCM:
2162 return (frame_bytes - 8) / ch;
2163 case CODEC_ID_XAN_DPCM:
2164 return (frame_bytes - 2 * ch) / ch;
2165 case CODEC_ID_MACE3:
2166 return 3 * frame_bytes / ch;
2167 case CODEC_ID_MACE6:
2168 return 6 * frame_bytes / ch;
2169 case CODEC_ID_PCM_LXF:
2170 return 2 * (frame_bytes / (5 * ch));
2174 /* calc from frame_bytes, channels, and codec_tag */
2175 if (id == CODEC_ID_SOL_DPCM) {
2177 return frame_bytes / ch;
2179 return frame_bytes * 2 / ch;
2184 /* calc from frame_bytes, channels, and block_align */
2185 int blocks = frame_bytes / ba;
2186 switch (avctx->codec_id) {
2187 case CODEC_ID_ADPCM_IMA_WAV:
2188 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2189 case CODEC_ID_ADPCM_IMA_DK3:
2190 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2191 case CODEC_ID_ADPCM_IMA_DK4:
2192 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2193 case CODEC_ID_ADPCM_MS:
2194 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2199 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2200 switch (avctx->codec_id) {
2201 case CODEC_ID_PCM_DVD:
2204 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2205 case CODEC_ID_PCM_BLURAY:
2208 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2209 case CODEC_ID_S302M:
2210 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2220 int ff_thread_init(AVCodecContext *s){
2225 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2239 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
2241 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
2245 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2247 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
2248 "version to the newest one from Git. If the problem still "
2249 "occurs, it means that your file has a feature which has not "
2250 "been implemented.\n", feature);
2252 av_log_ask_for_sample(avc, NULL);
2255 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2257 va_list argument_list;
2259 va_start(argument_list, msg);
2262 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2263 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2264 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2265 "and contact the ffmpeg-devel mailing list.\n");
2267 va_end(argument_list);
2270 static AVHWAccel *first_hwaccel = NULL;
2272 void av_register_hwaccel(AVHWAccel *hwaccel)
2274 AVHWAccel **p = &first_hwaccel;
2278 hwaccel->next = NULL;
2281 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2283 return hwaccel ? hwaccel->next : first_hwaccel;
2286 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
2288 AVHWAccel *hwaccel=NULL;
2290 while((hwaccel= av_hwaccel_next(hwaccel))){
2291 if ( hwaccel->id == codec_id
2292 && hwaccel->pix_fmt == pix_fmt)
2298 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2300 if (ff_lockmgr_cb) {
2301 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2303 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2309 if (ff_lockmgr_cb) {
2310 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2312 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2318 int avpriv_lock_avformat(void)
2320 if (ff_lockmgr_cb) {
2321 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2327 int avpriv_unlock_avformat(void)
2329 if (ff_lockmgr_cb) {
2330 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2336 unsigned int avpriv_toupper4(unsigned int x)
2338 return toupper( x &0xFF)
2339 + (toupper((x>>8 )&0xFF)<<8 )
2340 + (toupper((x>>16)&0xFF)<<16)
2341 + (toupper((x>>24)&0xFF)<<24);
2346 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2350 ff_init_buffer_info(avctx, f);
2352 return avctx->get_buffer(avctx, f);
2355 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2357 f->owner->release_buffer(f->owner, f);
2360 void ff_thread_finish_setup(AVCodecContext *avctx)
2364 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2368 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2372 int ff_thread_can_start_frame(AVCodecContext *avctx)
2379 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2381 AVCodec *c= avcodec_find_decoder(codec_id);
2383 c= avcodec_find_encoder(codec_id);
2387 if (codec_id <= CODEC_ID_NONE)
2388 return AVMEDIA_TYPE_UNKNOWN;
2389 else if (codec_id < CODEC_ID_FIRST_AUDIO)
2390 return AVMEDIA_TYPE_VIDEO;
2391 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2392 return AVMEDIA_TYPE_AUDIO;
2393 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2394 return AVMEDIA_TYPE_SUBTITLE;
2396 return AVMEDIA_TYPE_UNKNOWN;
2399 int avcodec_is_open(AVCodecContext *s)
2401 return !!s->internal;