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/avstring.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/dict.h"
38 #include "libavutil/opt.h"
39 #include "imgconvert.h"
41 #include "audioconvert.h"
48 static int volatile entangled_thread_counter=0;
49 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
50 static void *codec_mutex;
51 static void *avformat_mutex;
53 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
58 min_size= FFMAX(17*min_size/16 + 32, min_size);
60 ptr= av_realloc(ptr, min_size);
61 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
69 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
74 min_size= FFMAX(17*min_size/16 + 32, min_size);
76 *p = av_malloc(min_size);
77 if (!*p) min_size = 0;
81 /* encoder management */
82 static AVCodec *first_avcodec = NULL;
84 AVCodec *av_codec_next(AVCodec *c){
86 else return first_avcodec;
89 #if !FF_API_AVCODEC_INIT
92 void avcodec_init(void)
94 static int initialized = 0;
100 dsputil_static_init();
103 void avcodec_register(AVCodec *codec)
108 while (*p != NULL) p = &(*p)->next;
112 if (codec->init_static_data)
113 codec->init_static_data(codec);
116 unsigned avcodec_get_edge_width(void)
121 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
122 s->coded_width = width;
123 s->coded_height= height;
124 s->width = -((-width )>>s->lowres);
125 s->height= -((-height)>>s->lowres);
128 typedef struct InternalBuffer{
134 enum PixelFormat pix_fmt;
137 #define INTERNAL_BUFFER_SIZE (32+1)
139 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
144 case PIX_FMT_YUV420P:
145 case PIX_FMT_YUYV422:
146 case PIX_FMT_UYVY422:
147 case PIX_FMT_YUV422P:
148 case PIX_FMT_YUV440P:
149 case PIX_FMT_YUV444P:
151 case PIX_FMT_GRAY16BE:
152 case PIX_FMT_GRAY16LE:
153 case PIX_FMT_YUVJ420P:
154 case PIX_FMT_YUVJ422P:
155 case PIX_FMT_YUVJ440P:
156 case PIX_FMT_YUVJ444P:
157 case PIX_FMT_YUVA420P:
158 case PIX_FMT_YUV420P9LE:
159 case PIX_FMT_YUV420P9BE:
160 case PIX_FMT_YUV420P10LE:
161 case PIX_FMT_YUV420P10BE:
162 case PIX_FMT_YUV422P9LE:
163 case PIX_FMT_YUV422P9BE:
164 case PIX_FMT_YUV422P10LE:
165 case PIX_FMT_YUV422P10BE:
166 case PIX_FMT_YUV444P9LE:
167 case PIX_FMT_YUV444P9BE:
168 case PIX_FMT_YUV444P10LE:
169 case PIX_FMT_YUV444P10BE:
171 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
173 if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264 || s->codec_id == CODEC_ID_PRORES)
174 h_align= 32; // interlaced is rounded up to 2 MBs
176 case PIX_FMT_YUV411P:
177 case PIX_FMT_UYYVYY411:
181 case PIX_FMT_YUV410P:
182 if(s->codec_id == CODEC_ID_SVQ1){
187 if(s->codec_id == CODEC_ID_RPZA){
194 if(s->codec_id == CODEC_ID_SMC){
200 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
211 *width = FFALIGN(*width , w_align);
212 *height= FFALIGN(*height, h_align);
213 if(s->codec_id == CODEC_ID_H264 || s->lowres)
214 *height+=2; // some of the optimized chroma MC reads one line too much
215 // which is also done in mpeg decoders with lowres > 0
220 linesize_align[3] = STRIDE_ALIGN;
221 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
222 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
223 //picture size unneccessarily in some cases. The solution here is not
224 //pretty and better ideas are welcome!
226 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
227 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
228 s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
231 linesize_align[2] = 16;
236 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
237 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
238 int linesize_align[4];
240 avcodec_align_dimensions2(s, width, height, linesize_align);
241 align = FFMAX(linesize_align[0], linesize_align[3]);
242 linesize_align[1] <<= chroma_shift;
243 linesize_align[2] <<= chroma_shift;
244 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
245 *width=FFALIGN(*width, align);
248 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
251 pic->pkt_pts = s->pkt->pts;
252 pic->pkt_pos = s->pkt->pos;
254 pic->pkt_pts = AV_NOPTS_VALUE;
257 pic->reordered_opaque= s->reordered_opaque;
258 pic->sample_aspect_ratio = s->sample_aspect_ratio;
259 pic->width = s->width;
260 pic->height = s->height;
261 pic->format = s->pix_fmt;
264 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
271 if(pic->data[0]!=NULL) {
272 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
275 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
276 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
280 if(av_image_check_size(w, h, 0, s))
283 if(s->internal_buffer==NULL){
284 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
287 s->internal_buffer= av_fast_realloc(
289 &s->internal_buffer_size,
290 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
294 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
295 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
298 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
299 if(s->active_thread_type&FF_THREAD_FRAME) {
300 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
305 av_freep(&buf->base[i]);
311 pic->age= *picture_number - buf->last_pic_num;
312 buf->last_pic_num= *picture_number;
314 int h_chroma_shift, v_chroma_shift;
320 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
322 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
324 avcodec_align_dimensions2(s, &w, &h, stride_align);
326 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
332 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
333 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
334 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
335 // increase alignment of w for next try (rhs gives the lowest bit set in w)
340 unaligned |= picture.linesize[i] % stride_align[i];
344 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
348 for (i=0; i<3 && picture.data[i+1]; i++)
349 size[i] = picture.data[i+1] - picture.data[i];
350 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
352 buf->last_pic_num= -256*256*256*64;
353 memset(buf->base, 0, sizeof(buf->base));
354 memset(buf->data, 0, sizeof(buf->data));
356 for(i=0; i<4 && size[i]; i++){
357 const int h_shift= i==0 ? 0 : h_chroma_shift;
358 const int v_shift= i==0 ? 0 : v_chroma_shift;
360 buf->linesize[i]= picture.linesize[i];
362 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
363 if(buf->base[i]==NULL) return -1;
364 memset(buf->base[i], 128, size[i]);
366 // no edge if EDGE EMU or not planar YUV
367 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
368 buf->data[i] = buf->base[i];
370 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
372 if(size[1] && !size[2])
373 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
374 buf->width = s->width;
375 buf->height = s->height;
376 buf->pix_fmt= s->pix_fmt;
377 pic->age= 256*256*256*64;
379 pic->type= FF_BUFFER_TYPE_INTERNAL;
382 pic->base[i]= buf->base[i];
383 pic->data[i]= buf->data[i];
384 pic->linesize[i]= buf->linesize[i];
386 s->internal_buffer_count++;
389 pic->pkt_pts = s->pkt->pts;
390 pic->pkt_pos = s->pkt->pos;
392 pic->pkt_pts = AV_NOPTS_VALUE;
395 pic->reordered_opaque= s->reordered_opaque;
396 pic->sample_aspect_ratio = s->sample_aspect_ratio;
397 pic->width = s->width;
398 pic->height = s->height;
399 pic->format = s->pix_fmt;
401 if(s->debug&FF_DEBUG_BUFFERS)
402 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
407 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
409 InternalBuffer *buf, *last;
411 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
412 assert(s->internal_buffer_count);
414 if(s->internal_buffer){
415 buf = NULL; /* avoids warning */
416 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
417 buf= &((InternalBuffer*)s->internal_buffer)[i];
418 if(buf->data[0] == pic->data[0])
421 assert(i < s->internal_buffer_count);
422 s->internal_buffer_count--;
423 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
425 FFSWAP(InternalBuffer, *buf, *last);
430 // pic->base[i]=NULL;
432 //printf("R%X\n", pic->opaque);
434 if(s->debug&FF_DEBUG_BUFFERS)
435 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
438 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
442 /* If no picture return a new buffer */
443 if(pic->data[0] == NULL) {
444 /* We will copy from buffer, so must be readable */
445 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
446 return s->get_buffer(s, pic);
449 /* If internal buffer type return the same buffer */
450 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
451 if(s->pkt) pic->pkt_pts= s->pkt->pts;
452 else pic->pkt_pts= AV_NOPTS_VALUE;
453 pic->reordered_opaque= s->reordered_opaque;
458 * Not internal type and reget_buffer not overridden, emulate cr buffer
461 for(i = 0; i < 4; i++)
462 pic->data[i] = pic->base[i] = NULL;
464 /* Allocate new frame */
465 if (s->get_buffer(s, pic))
467 /* Copy image data from old buffer to new buffer */
468 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
470 s->release_buffer(s, &temp_pic); // Release old frame
474 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
477 for(i=0; i<count; i++){
478 int r= func(c, (char*)arg + i*size);
484 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
487 for(i=0; i<count; i++){
488 int r= func(c, arg, i, 0);
494 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
495 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
500 void avcodec_get_frame_defaults(AVFrame *pic){
501 memset(pic, 0, sizeof(AVFrame));
503 pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
506 pic->sample_aspect_ratio = (AVRational){0, 1};
507 pic->format = -1; /* unknown */
510 AVFrame *avcodec_alloc_frame(void){
511 AVFrame *pic= av_malloc(sizeof(AVFrame));
513 if(pic==NULL) return NULL;
515 avcodec_get_frame_defaults(pic);
520 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
522 memset(sub, 0, sizeof(*sub));
523 sub->pts = AV_NOPTS_VALUE;
526 #if FF_API_AVCODEC_OPEN
527 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
529 return avcodec_open2(avctx, codec, NULL);
533 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
536 AVDictionary *tmp = NULL;
539 av_dict_copy(&tmp, *options, 0);
541 /* If there is a user-supplied mutex locking routine, call it. */
543 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
547 entangled_thread_counter++;
548 if(entangled_thread_counter != 1){
549 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
554 if(avctx->codec || !codec) {
555 ret = AVERROR(EINVAL);
559 if (codec->priv_data_size > 0) {
560 if(!avctx->priv_data){
561 avctx->priv_data = av_mallocz(codec->priv_data_size);
562 if (!avctx->priv_data) {
563 ret = AVERROR(ENOMEM);
566 if (codec->priv_class) {
567 *(AVClass**)avctx->priv_data= codec->priv_class;
568 av_opt_set_defaults(avctx->priv_data);
571 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
574 avctx->priv_data = NULL;
576 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
579 if(avctx->coded_width && avctx->coded_height)
580 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
581 else if(avctx->width && avctx->height)
582 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
584 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
585 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
586 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
587 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
588 avcodec_set_dimensions(avctx, 0, 0);
591 /* if the decoder init function was already called previously,
592 free the already allocated subtitle_header before overwriting it */
594 av_freep(&avctx->subtitle_header);
596 #define SANE_NB_CHANNELS 128U
597 if (avctx->channels > SANE_NB_CHANNELS) {
598 ret = AVERROR(EINVAL);
602 avctx->codec = codec;
603 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
604 avctx->codec_id == CODEC_ID_NONE) {
605 avctx->codec_type = codec->type;
606 avctx->codec_id = codec->id;
608 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
609 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
610 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
611 ret = AVERROR(EINVAL);
614 avctx->frame_number = 0;
617 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
618 avctx->error_recognition, avctx->err_recognition);
619 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
620 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
621 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
622 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
623 avctx->error_recognition, avctx->err_recognition);
627 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
629 if (HAVE_THREADS && !avctx->thread_opaque) {
630 ret = ff_thread_init(avctx);
636 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
637 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
638 avctx->codec->max_lowres);
639 ret = AVERROR(EINVAL);
642 if (avctx->codec->encode) {
644 if (avctx->codec->sample_fmts) {
645 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
646 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
648 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
649 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
650 ret = AVERROR(EINVAL);
654 if (avctx->codec->supported_samplerates) {
655 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
656 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
658 if (avctx->codec->supported_samplerates[i] == 0) {
659 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
660 ret = AVERROR(EINVAL);
664 if (avctx->codec->channel_layouts) {
665 if (!avctx->channel_layout) {
666 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
668 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
669 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
671 if (avctx->codec->channel_layouts[i] == 0) {
672 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
673 ret = AVERROR(EINVAL);
678 if (avctx->channel_layout && avctx->channels) {
679 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
680 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
681 ret = AVERROR(EINVAL);
684 } else if (avctx->channel_layout) {
685 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
689 avctx->pts_correction_num_faulty_pts =
690 avctx->pts_correction_num_faulty_dts = 0;
691 avctx->pts_correction_last_pts =
692 avctx->pts_correction_last_dts = INT64_MIN;
694 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
695 ret = avctx->codec->init(avctx);
703 entangled_thread_counter--;
705 /* Release any user-supplied mutex. */
707 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
710 av_dict_free(options);
717 av_freep(&avctx->priv_data);
722 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
723 const short *samples)
725 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
726 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
729 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
730 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
731 avctx->frame_number++;
737 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
740 if(buf_size < FF_MIN_BUFFER_SIZE){
741 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
744 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
746 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
747 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
748 avctx->frame_number++;
749 emms_c(); //needed to avoid an emms_c() call before every return;
756 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
757 const AVSubtitle *sub)
760 if(sub->start_display_time) {
761 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
765 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
766 avctx->frame_number++;
771 * Attempt to guess proper monotonic timestamps for decoded video frames
772 * which might have incorrect times. Input timestamps may wrap around, in
773 * which case the output will as well.
775 * @param pts the pts field of the decoded AVPacket, as passed through
777 * @param dts the dts field of the decoded AVPacket
778 * @return one of the input values, may be AV_NOPTS_VALUE
780 static int64_t guess_correct_pts(AVCodecContext *ctx,
781 int64_t reordered_pts, int64_t dts)
783 int64_t pts = AV_NOPTS_VALUE;
785 if (dts != AV_NOPTS_VALUE) {
786 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
787 ctx->pts_correction_last_dts = dts;
789 if (reordered_pts != AV_NOPTS_VALUE) {
790 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
791 ctx->pts_correction_last_pts = reordered_pts;
793 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
794 && reordered_pts != AV_NOPTS_VALUE)
802 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
803 int *got_picture_ptr,
809 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
812 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
813 av_packet_split_side_data(avpkt);
815 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
816 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
819 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
821 picture->pkt_dts= avpkt->dts;
823 if(!avctx->has_b_frames){
824 picture->pkt_pos= avpkt->pos;
826 //FIXME these should be under if(!avctx->has_b_frames)
827 if (!picture->sample_aspect_ratio.num)
828 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
830 picture->width = avctx->width;
831 if (!picture->height)
832 picture->height = avctx->height;
833 if (picture->format == PIX_FMT_NONE)
834 picture->format = avctx->pix_fmt;
837 emms_c(); //needed to avoid an emms_c() call before every return;
840 if (*got_picture_ptr){
841 avctx->frame_number++;
842 picture->best_effort_timestamp = guess_correct_pts(avctx,
852 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
860 if (!avpkt->data && avpkt->size) {
861 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
862 return AVERROR(EINVAL);
865 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
866 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
867 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
868 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
871 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
872 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
873 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
877 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
878 avctx->frame_number++;
886 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
894 avcodec_get_subtitle_defaults(sub);
895 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
897 avctx->frame_number++;
901 void avsubtitle_free(AVSubtitle *sub)
905 for (i = 0; i < sub->num_rects; i++)
907 av_freep(&sub->rects[i]->pict.data[0]);
908 av_freep(&sub->rects[i]->pict.data[1]);
909 av_freep(&sub->rects[i]->pict.data[2]);
910 av_freep(&sub->rects[i]->pict.data[3]);
911 av_freep(&sub->rects[i]->text);
912 av_freep(&sub->rects[i]->ass);
913 av_freep(&sub->rects[i]);
916 av_freep(&sub->rects);
918 memset(sub, 0, sizeof(AVSubtitle));
921 av_cold int avcodec_close(AVCodecContext *avctx)
923 /* If there is a user-supplied mutex locking routine, call it. */
925 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
929 entangled_thread_counter++;
930 if(entangled_thread_counter != 1){
931 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
932 entangled_thread_counter--;
936 if (HAVE_THREADS && avctx->thread_opaque)
937 ff_thread_free(avctx);
938 if (avctx->codec && avctx->codec->close)
939 avctx->codec->close(avctx);
940 avcodec_default_free_buffers(avctx);
941 avctx->coded_frame = NULL;
942 if (avctx->codec && avctx->codec->priv_class)
943 av_opt_free(avctx->priv_data);
945 av_freep(&avctx->priv_data);
946 if(avctx->codec && avctx->codec->encode)
947 av_freep(&avctx->extradata);
949 avctx->active_thread_type = 0;
950 entangled_thread_counter--;
952 /* Release any user-supplied mutex. */
954 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
959 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
962 case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
963 case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
964 case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
969 AVCodec *avcodec_find_encoder(enum CodecID id)
971 AVCodec *p, *experimental=NULL;
973 id= remap_deprecated_codec_id(id);
975 if (p->encode != NULL && p->id == id) {
976 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
986 AVCodec *avcodec_find_encoder_by_name(const char *name)
993 if (p->encode != NULL && strcmp(name,p->name) == 0)
1000 AVCodec *avcodec_find_decoder(enum CodecID id)
1002 AVCodec *p, *experimental=NULL;
1004 id= remap_deprecated_codec_id(id);
1006 if (p->decode != NULL && p->id == id) {
1007 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1014 return experimental;
1017 AVCodec *avcodec_find_decoder_by_name(const char *name)
1024 if (p->decode != NULL && strcmp(name,p->name) == 0)
1031 static int get_bit_rate(AVCodecContext *ctx)
1034 int bits_per_sample;
1036 switch(ctx->codec_type) {
1037 case AVMEDIA_TYPE_VIDEO:
1038 case AVMEDIA_TYPE_DATA:
1039 case AVMEDIA_TYPE_SUBTITLE:
1040 case AVMEDIA_TYPE_ATTACHMENT:
1041 bit_rate = ctx->bit_rate;
1043 case AVMEDIA_TYPE_AUDIO:
1044 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1045 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1054 const char *avcodec_get_name(enum CodecID id)
1060 #include "libavcodec/codec_names.h"
1062 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1064 codec = avcodec_find_decoder(id);
1067 codec = avcodec_find_encoder(id);
1070 return "unknown_codec";
1073 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1075 int i, len, ret = 0;
1077 for (i = 0; i < 4; i++) {
1078 len = snprintf(buf, buf_size,
1079 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1081 buf_size = buf_size > len ? buf_size - len : 0;
1088 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1090 const char *codec_type;
1091 const char *codec_name;
1092 const char *profile = NULL;
1095 AVRational display_aspect_ratio;
1097 if (!buf || buf_size <= 0)
1099 codec_type = av_get_media_type_string(enc->codec_type);
1100 codec_name = avcodec_get_name(enc->codec_id);
1101 if (enc->profile != FF_PROFILE_UNKNOWN) {
1102 p = encode ? avcodec_find_encoder(enc->codec_id) :
1103 avcodec_find_decoder(enc->codec_id);
1105 profile = av_get_profile_name(p, enc->profile);
1108 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1109 codec_name, enc->mb_decision ? " (hq)" : "");
1110 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1112 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1113 if (enc->codec_tag) {
1115 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1116 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1117 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1119 switch(enc->codec_type) {
1120 case AVMEDIA_TYPE_VIDEO:
1121 if (enc->pix_fmt != PIX_FMT_NONE) {
1122 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1124 av_get_pix_fmt_name(enc->pix_fmt));
1127 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1129 enc->width, enc->height);
1130 if (enc->sample_aspect_ratio.num) {
1131 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1132 enc->width*enc->sample_aspect_ratio.num,
1133 enc->height*enc->sample_aspect_ratio.den,
1135 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1136 " [SAR %d:%d DAR %d:%d]",
1137 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1138 display_aspect_ratio.num, display_aspect_ratio.den);
1140 if(av_log_get_level() >= AV_LOG_DEBUG){
1141 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1142 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1144 enc->time_base.num/g, enc->time_base.den/g);
1148 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1149 ", q=%d-%d", enc->qmin, enc->qmax);
1152 case AVMEDIA_TYPE_AUDIO:
1153 if (enc->sample_rate) {
1154 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1155 ", %d Hz", enc->sample_rate);
1157 av_strlcat(buf, ", ", buf_size);
1158 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1159 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1160 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1161 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1168 if (enc->flags & CODEC_FLAG_PASS1)
1169 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1171 if (enc->flags & CODEC_FLAG_PASS2)
1172 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1175 bitrate = get_bit_rate(enc);
1177 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1178 ", %d kb/s", bitrate / 1000);
1182 const char *av_get_profile_name(const AVCodec *codec, int profile)
1185 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1188 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1189 if (p->profile == profile)
1195 unsigned avcodec_version( void )
1197 return LIBAVCODEC_VERSION_INT;
1200 const char *avcodec_configuration(void)
1202 return FFMPEG_CONFIGURATION;
1205 const char *avcodec_license(void)
1207 #define LICENSE_PREFIX "libavcodec license: "
1208 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1211 void avcodec_flush_buffers(AVCodecContext *avctx)
1213 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1214 ff_thread_flush(avctx);
1215 else if(avctx->codec->flush)
1216 avctx->codec->flush(avctx);
1219 void avcodec_default_free_buffers(AVCodecContext *s){
1222 if(s->internal_buffer==NULL) return;
1224 if (s->internal_buffer_count)
1225 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1226 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1227 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1229 av_freep(&buf->base[j]);
1233 av_freep(&s->internal_buffer);
1235 s->internal_buffer_count=0;
1238 #if FF_API_OLD_FF_PICT_TYPES
1239 char av_get_pict_type_char(int pict_type){
1240 return av_get_picture_type_char(pict_type);
1244 int av_get_bits_per_sample(enum CodecID codec_id){
1246 case CODEC_ID_ADPCM_SBPRO_2:
1248 case CODEC_ID_ADPCM_SBPRO_3:
1250 case CODEC_ID_ADPCM_SBPRO_4:
1251 case CODEC_ID_ADPCM_CT:
1252 case CODEC_ID_ADPCM_IMA_WAV:
1253 case CODEC_ID_ADPCM_IMA_QT:
1254 case CODEC_ID_ADPCM_SWF:
1255 case CODEC_ID_ADPCM_MS:
1256 case CODEC_ID_ADPCM_YAMAHA:
1258 case CODEC_ID_ADPCM_G722:
1259 case CODEC_ID_PCM_ALAW:
1260 case CODEC_ID_PCM_MULAW:
1261 case CODEC_ID_PCM_S8:
1262 case CODEC_ID_PCM_U8:
1263 case CODEC_ID_PCM_ZORK:
1265 case CODEC_ID_PCM_S16BE:
1266 case CODEC_ID_PCM_S16LE:
1267 case CODEC_ID_PCM_S16LE_PLANAR:
1268 case CODEC_ID_PCM_U16BE:
1269 case CODEC_ID_PCM_U16LE:
1271 case CODEC_ID_PCM_S24DAUD:
1272 case CODEC_ID_PCM_S24BE:
1273 case CODEC_ID_PCM_S24LE:
1274 case CODEC_ID_PCM_U24BE:
1275 case CODEC_ID_PCM_U24LE:
1277 case CODEC_ID_PCM_S32BE:
1278 case CODEC_ID_PCM_S32LE:
1279 case CODEC_ID_PCM_U32BE:
1280 case CODEC_ID_PCM_U32LE:
1281 case CODEC_ID_PCM_F32BE:
1282 case CODEC_ID_PCM_F32LE:
1284 case CODEC_ID_PCM_F64BE:
1285 case CODEC_ID_PCM_F64LE:
1292 #if FF_API_OLD_SAMPLE_FMT
1293 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1294 return av_get_bytes_per_sample(sample_fmt) << 3;
1299 int ff_thread_init(AVCodecContext *s){
1304 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1318 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1320 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1324 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1326 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1327 "version to the newest one from Git. If the problem still "
1328 "occurs, it means that your file has a feature which has not "
1329 "been implemented.\n", feature);
1331 av_log_ask_for_sample(avc, NULL);
1334 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1336 va_list argument_list;
1338 va_start(argument_list, msg);
1341 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1342 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1343 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1344 "and contact the ffmpeg-devel mailing list.\n");
1346 va_end(argument_list);
1349 static AVHWAccel *first_hwaccel = NULL;
1351 void av_register_hwaccel(AVHWAccel *hwaccel)
1353 AVHWAccel **p = &first_hwaccel;
1357 hwaccel->next = NULL;
1360 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1362 return hwaccel ? hwaccel->next : first_hwaccel;
1365 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1367 AVHWAccel *hwaccel=NULL;
1369 while((hwaccel= av_hwaccel_next(hwaccel))){
1370 if ( hwaccel->id == codec_id
1371 && hwaccel->pix_fmt == pix_fmt)
1377 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1379 if (ff_lockmgr_cb) {
1380 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1382 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1388 if (ff_lockmgr_cb) {
1389 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1391 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1397 int avpriv_lock_avformat(void)
1399 if (ff_lockmgr_cb) {
1400 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1406 int avpriv_unlock_avformat(void)
1408 if (ff_lockmgr_cb) {
1409 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1415 unsigned int avpriv_toupper4(unsigned int x)
1417 return toupper( x &0xFF)
1418 + (toupper((x>>8 )&0xFF)<<8 )
1419 + (toupper((x>>16)&0xFF)<<16)
1420 + (toupper((x>>24)&0xFF)<<24);
1425 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1429 ff_init_buffer_info(avctx, f);
1431 return avctx->get_buffer(avctx, f);
1434 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1436 f->owner->release_buffer(f->owner, f);
1439 void ff_thread_finish_setup(AVCodecContext *avctx)
1443 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1447 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1453 #if FF_API_THREAD_INIT
1454 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1456 s->thread_count = thread_count;
1457 return ff_thread_init(s);
1461 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1463 AVCodec *c= avcodec_find_decoder(codec_id);
1465 c= avcodec_find_encoder(codec_id);
1469 if (codec_id <= CODEC_ID_NONE)
1470 return AVMEDIA_TYPE_UNKNOWN;
1471 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1472 return AVMEDIA_TYPE_VIDEO;
1473 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1474 return AVMEDIA_TYPE_AUDIO;
1475 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1476 return AVMEDIA_TYPE_SUBTITLE;
1478 return AVMEDIA_TYPE_UNKNOWN;