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 #define INTERNAL_BUFFER_SIZE (32+1)
130 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
135 case PIX_FMT_YUV420P:
136 case PIX_FMT_YUYV422:
137 case PIX_FMT_UYVY422:
138 case PIX_FMT_YUV422P:
139 case PIX_FMT_YUV440P:
140 case PIX_FMT_YUV444P:
143 case PIX_FMT_GRAY16BE:
144 case PIX_FMT_GRAY16LE:
145 case PIX_FMT_YUVJ420P:
146 case PIX_FMT_YUVJ422P:
147 case PIX_FMT_YUVJ440P:
148 case PIX_FMT_YUVJ444P:
149 case PIX_FMT_YUVA420P:
150 case PIX_FMT_YUV420P9LE:
151 case PIX_FMT_YUV420P9BE:
152 case PIX_FMT_YUV420P10LE:
153 case PIX_FMT_YUV420P10BE:
154 case PIX_FMT_YUV422P9LE:
155 case PIX_FMT_YUV422P9BE:
156 case PIX_FMT_YUV422P10LE:
157 case PIX_FMT_YUV422P10BE:
158 case PIX_FMT_YUV444P9LE:
159 case PIX_FMT_YUV444P9BE:
160 case PIX_FMT_YUV444P10LE:
161 case PIX_FMT_YUV444P10BE:
162 case PIX_FMT_GBRP9LE:
163 case PIX_FMT_GBRP9BE:
164 case PIX_FMT_GBRP10LE:
165 case PIX_FMT_GBRP10BE:
166 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
168 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)
169 h_align= 32; // interlaced is rounded up to 2 MBs
171 case PIX_FMT_YUV411P:
172 case PIX_FMT_UYYVYY411:
176 case PIX_FMT_YUV410P:
177 if(s->codec_id == CODEC_ID_SVQ1){
182 if(s->codec_id == CODEC_ID_RPZA){
189 if(s->codec_id == CODEC_ID_SMC){
195 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
206 *width = FFALIGN(*width , w_align);
207 *height= FFALIGN(*height, h_align);
208 if(s->codec_id == CODEC_ID_H264 || s->lowres)
209 *height+=2; // some of the optimized chroma MC reads one line too much
210 // which is also done in mpeg decoders with lowres > 0
215 linesize_align[3] = STRIDE_ALIGN;
216 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
217 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
218 //picture size unneccessarily in some cases. The solution here is not
219 //pretty and better ideas are welcome!
221 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
222 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
223 s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
226 linesize_align[2] = 16;
231 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
232 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
233 int linesize_align[4];
235 avcodec_align_dimensions2(s, width, height, linesize_align);
236 align = FFMAX(linesize_align[0], linesize_align[3]);
237 linesize_align[1] <<= chroma_shift;
238 linesize_align[2] <<= chroma_shift;
239 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
240 *width=FFALIGN(*width, align);
243 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
246 pic->pkt_pts = s->pkt->pts;
247 pic->pkt_pos = s->pkt->pos;
249 pic->pkt_pts = AV_NOPTS_VALUE;
252 pic->reordered_opaque= s->reordered_opaque;
253 pic->sample_aspect_ratio = s->sample_aspect_ratio;
254 pic->width = s->width;
255 pic->height = s->height;
256 pic->format = s->pix_fmt;
259 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
265 AVCodecInternal *avci = s->internal;
267 if(pic->data[0]!=NULL) {
268 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
271 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
272 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
276 if(av_image_check_size(w, h, 0, s))
280 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
281 sizeof(InternalBuffer));
284 buf = &avci->buffer[avci->buffer_count];
285 picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
288 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
289 if(s->active_thread_type&FF_THREAD_FRAME) {
290 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
295 av_freep(&buf->base[i]);
301 pic->age= *picture_number - buf->last_pic_num;
302 buf->last_pic_num= *picture_number;
304 int h_chroma_shift, v_chroma_shift;
310 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
312 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
314 avcodec_align_dimensions2(s, &w, &h, stride_align);
316 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
322 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
323 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
324 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
325 // increase alignment of w for next try (rhs gives the lowest bit set in w)
330 unaligned |= picture.linesize[i] % stride_align[i];
334 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
338 for (i=0; i<3 && picture.data[i+1]; i++)
339 size[i] = picture.data[i+1] - picture.data[i];
340 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
342 buf->last_pic_num= -256*256*256*64;
343 memset(buf->base, 0, sizeof(buf->base));
344 memset(buf->data, 0, sizeof(buf->data));
346 for(i=0; i<4 && size[i]; i++){
347 const int h_shift= i==0 ? 0 : h_chroma_shift;
348 const int v_shift= i==0 ? 0 : v_chroma_shift;
350 buf->linesize[i]= picture.linesize[i];
352 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
353 if(buf->base[i]==NULL) return -1;
354 memset(buf->base[i], 128, size[i]);
356 // no edge if EDGE EMU or not planar YUV
357 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
358 buf->data[i] = buf->base[i];
360 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
362 if(size[1] && !size[2])
363 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
364 buf->width = s->width;
365 buf->height = s->height;
366 buf->pix_fmt= s->pix_fmt;
367 pic->age= 256*256*256*64;
369 pic->type= FF_BUFFER_TYPE_INTERNAL;
372 pic->base[i]= buf->base[i];
373 pic->data[i]= buf->data[i];
374 pic->linesize[i]= buf->linesize[i];
376 avci->buffer_count++;
379 pic->pkt_pts = s->pkt->pts;
380 pic->pkt_pos = s->pkt->pos;
382 pic->pkt_pts = AV_NOPTS_VALUE;
385 pic->reordered_opaque= s->reordered_opaque;
386 pic->sample_aspect_ratio = s->sample_aspect_ratio;
387 pic->width = s->width;
388 pic->height = s->height;
389 pic->format = s->pix_fmt;
391 if(s->debug&FF_DEBUG_BUFFERS)
392 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
393 "buffers used\n", pic, avci->buffer_count);
398 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
400 InternalBuffer *buf, *last;
401 AVCodecInternal *avci = s->internal;
403 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
404 assert(avci->buffer_count);
407 buf = NULL; /* avoids warning */
408 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
409 buf = &avci->buffer[i];
410 if (buf->data[0] == pic->data[0])
413 assert(i < avci->buffer_count);
414 avci->buffer_count--;
415 last = &avci->buffer[avci->buffer_count];
417 FFSWAP(InternalBuffer, *buf, *last);
422 // pic->base[i]=NULL;
424 //printf("R%X\n", pic->opaque);
426 if(s->debug&FF_DEBUG_BUFFERS)
427 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
428 "buffers used\n", pic, avci->buffer_count);
431 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
435 /* If no picture return a new buffer */
436 if(pic->data[0] == NULL) {
437 /* We will copy from buffer, so must be readable */
438 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
439 return s->get_buffer(s, pic);
442 /* If internal buffer type return the same buffer */
443 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
444 if(s->pkt) pic->pkt_pts= s->pkt->pts;
445 else pic->pkt_pts= AV_NOPTS_VALUE;
446 pic->reordered_opaque= s->reordered_opaque;
451 * Not internal type and reget_buffer not overridden, emulate cr buffer
454 for(i = 0; i < 4; i++)
455 pic->data[i] = pic->base[i] = NULL;
457 /* Allocate new frame */
458 if (s->get_buffer(s, pic))
460 /* Copy image data from old buffer to new buffer */
461 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
463 s->release_buffer(s, &temp_pic); // Release old frame
467 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
470 for(i=0; i<count; i++){
471 int r= func(c, (char*)arg + i*size);
477 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
480 for(i=0; i<count; i++){
481 int r= func(c, arg, i, 0);
487 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
488 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
493 void avcodec_get_frame_defaults(AVFrame *pic){
494 memset(pic, 0, sizeof(AVFrame));
496 pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
499 pic->sample_aspect_ratio = (AVRational){0, 1};
500 pic->format = -1; /* unknown */
503 AVFrame *avcodec_alloc_frame(void){
504 AVFrame *pic= av_malloc(sizeof(AVFrame));
506 if(pic==NULL) return NULL;
508 avcodec_get_frame_defaults(pic);
513 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
515 memset(sub, 0, sizeof(*sub));
516 sub->pts = AV_NOPTS_VALUE;
519 #if FF_API_AVCODEC_OPEN
520 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
522 return avcodec_open2(avctx, codec, NULL);
526 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
529 AVDictionary *tmp = NULL;
532 av_dict_copy(&tmp, *options, 0);
534 /* If there is a user-supplied mutex locking routine, call it. */
536 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
540 entangled_thread_counter++;
541 if(entangled_thread_counter != 1){
542 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
547 if(avctx->codec || !codec) {
548 ret = AVERROR(EINVAL);
552 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
553 if (!avctx->internal) {
554 ret = AVERROR(ENOMEM);
558 if (codec->priv_data_size > 0) {
559 if(!avctx->priv_data){
560 avctx->priv_data = av_mallocz(codec->priv_data_size);
561 if (!avctx->priv_data) {
562 ret = AVERROR(ENOMEM);
565 if (codec->priv_class) {
566 *(AVClass**)avctx->priv_data= codec->priv_class;
567 av_opt_set_defaults(avctx->priv_data);
570 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
573 avctx->priv_data = NULL;
575 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
578 //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
579 if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
580 if(avctx->coded_width && avctx->coded_height)
581 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
582 else if(avctx->width && avctx->height)
583 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
586 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
587 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
588 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
589 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
590 avcodec_set_dimensions(avctx, 0, 0);
593 /* if the decoder init function was already called previously,
594 free the already allocated subtitle_header before overwriting it */
596 av_freep(&avctx->subtitle_header);
598 #define SANE_NB_CHANNELS 128U
599 if (avctx->channels > SANE_NB_CHANNELS) {
600 ret = AVERROR(EINVAL);
604 avctx->codec = codec;
605 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
606 avctx->codec_id == CODEC_ID_NONE) {
607 avctx->codec_type = codec->type;
608 avctx->codec_id = codec->id;
610 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
611 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
612 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
613 ret = AVERROR(EINVAL);
616 avctx->frame_number = 0;
619 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
620 avctx->error_recognition, avctx->err_recognition);
621 switch(avctx->error_recognition){
622 case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
624 case FF_ER_VERY_AGGRESSIVE:
625 case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
626 case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
627 case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
630 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
631 avctx->error_recognition, avctx->err_recognition);
635 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
637 if (HAVE_THREADS && !avctx->thread_opaque) {
638 ret = ff_thread_init(avctx);
644 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
645 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
646 avctx->codec->max_lowres);
647 ret = AVERROR(EINVAL);
650 if (avctx->codec->encode) {
652 if (avctx->codec->sample_fmts) {
653 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
654 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
656 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
657 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
658 ret = AVERROR(EINVAL);
662 if (avctx->codec->supported_samplerates) {
663 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
664 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
666 if (avctx->codec->supported_samplerates[i] == 0) {
667 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
668 ret = AVERROR(EINVAL);
672 if (avctx->codec->channel_layouts) {
673 if (!avctx->channel_layout) {
674 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
676 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
677 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
679 if (avctx->codec->channel_layouts[i] == 0) {
680 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
681 ret = AVERROR(EINVAL);
686 if (avctx->channel_layout && avctx->channels) {
687 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
688 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
689 ret = AVERROR(EINVAL);
692 } else if (avctx->channel_layout) {
693 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
697 avctx->pts_correction_num_faulty_pts =
698 avctx->pts_correction_num_faulty_dts = 0;
699 avctx->pts_correction_last_pts =
700 avctx->pts_correction_last_dts = INT64_MIN;
702 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
703 ret = avctx->codec->init(avctx);
711 entangled_thread_counter--;
713 /* Release any user-supplied mutex. */
715 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
718 av_dict_free(options);
725 av_freep(&avctx->priv_data);
726 av_freep(&avctx->internal);
731 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
732 const short *samples)
734 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
735 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
738 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
739 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
740 avctx->frame_number++;
746 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
749 if(buf_size < FF_MIN_BUFFER_SIZE){
750 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
753 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
755 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
756 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
757 avctx->frame_number++;
758 emms_c(); //needed to avoid an emms_c() call before every return;
765 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
766 const AVSubtitle *sub)
769 if(sub->start_display_time) {
770 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
774 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
775 avctx->frame_number++;
780 * Attempt to guess proper monotonic timestamps for decoded video frames
781 * which might have incorrect times. Input timestamps may wrap around, in
782 * which case the output will as well.
784 * @param pts the pts field of the decoded AVPacket, as passed through
786 * @param dts the dts field of the decoded AVPacket
787 * @return one of the input values, may be AV_NOPTS_VALUE
789 static int64_t guess_correct_pts(AVCodecContext *ctx,
790 int64_t reordered_pts, int64_t dts)
792 int64_t pts = AV_NOPTS_VALUE;
794 if (dts != AV_NOPTS_VALUE) {
795 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
796 ctx->pts_correction_last_dts = dts;
798 if (reordered_pts != AV_NOPTS_VALUE) {
799 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
800 ctx->pts_correction_last_pts = reordered_pts;
802 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
803 && reordered_pts != AV_NOPTS_VALUE)
811 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
812 int *got_picture_ptr,
818 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
821 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
822 av_packet_split_side_data(avpkt);
824 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
825 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
828 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
830 picture->pkt_dts= avpkt->dts;
832 if(!avctx->has_b_frames){
833 picture->pkt_pos= avpkt->pos;
835 //FIXME these should be under if(!avctx->has_b_frames)
836 if (!picture->sample_aspect_ratio.num)
837 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
839 picture->width = avctx->width;
840 if (!picture->height)
841 picture->height = avctx->height;
842 if (picture->format == PIX_FMT_NONE)
843 picture->format = avctx->pix_fmt;
846 emms_c(); //needed to avoid an emms_c() call before every return;
849 if (*got_picture_ptr){
850 avctx->frame_number++;
851 picture->best_effort_timestamp = guess_correct_pts(avctx,
861 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
869 if (!avpkt->data && avpkt->size) {
870 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
871 return AVERROR(EINVAL);
874 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
875 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
876 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
877 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
880 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
881 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
882 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
886 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
887 avctx->frame_number++;
895 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
903 avcodec_get_subtitle_defaults(sub);
904 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
906 avctx->frame_number++;
910 void avsubtitle_free(AVSubtitle *sub)
914 for (i = 0; i < sub->num_rects; i++)
916 av_freep(&sub->rects[i]->pict.data[0]);
917 av_freep(&sub->rects[i]->pict.data[1]);
918 av_freep(&sub->rects[i]->pict.data[2]);
919 av_freep(&sub->rects[i]->pict.data[3]);
920 av_freep(&sub->rects[i]->text);
921 av_freep(&sub->rects[i]->ass);
922 av_freep(&sub->rects[i]);
925 av_freep(&sub->rects);
927 memset(sub, 0, sizeof(AVSubtitle));
930 av_cold int avcodec_close(AVCodecContext *avctx)
932 /* If there is a user-supplied mutex locking routine, call it. */
934 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
938 entangled_thread_counter++;
939 if(entangled_thread_counter != 1){
940 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
941 entangled_thread_counter--;
945 if (HAVE_THREADS && avctx->thread_opaque)
946 ff_thread_free(avctx);
947 if (avctx->codec && avctx->codec->close)
948 avctx->codec->close(avctx);
949 avcodec_default_free_buffers(avctx);
950 avctx->coded_frame = NULL;
951 av_freep(&avctx->internal);
952 if (avctx->codec && avctx->codec->priv_class)
953 av_opt_free(avctx->priv_data);
955 av_freep(&avctx->priv_data);
956 if(avctx->codec && avctx->codec->encode)
957 av_freep(&avctx->extradata);
959 avctx->active_thread_type = 0;
960 entangled_thread_counter--;
962 /* Release any user-supplied mutex. */
964 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
969 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
972 case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
973 case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
974 case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
979 AVCodec *avcodec_find_encoder(enum CodecID id)
981 AVCodec *p, *experimental=NULL;
983 id= remap_deprecated_codec_id(id);
985 if (p->encode != NULL && p->id == id) {
986 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
996 AVCodec *avcodec_find_encoder_by_name(const char *name)
1003 if (p->encode != NULL && strcmp(name,p->name) == 0)
1010 AVCodec *avcodec_find_decoder(enum CodecID id)
1012 AVCodec *p, *experimental=NULL;
1014 id= remap_deprecated_codec_id(id);
1016 if (p->decode != NULL && p->id == id) {
1017 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1024 return experimental;
1027 AVCodec *avcodec_find_decoder_by_name(const char *name)
1034 if (p->decode != NULL && strcmp(name,p->name) == 0)
1041 static int get_bit_rate(AVCodecContext *ctx)
1044 int bits_per_sample;
1046 switch(ctx->codec_type) {
1047 case AVMEDIA_TYPE_VIDEO:
1048 case AVMEDIA_TYPE_DATA:
1049 case AVMEDIA_TYPE_SUBTITLE:
1050 case AVMEDIA_TYPE_ATTACHMENT:
1051 bit_rate = ctx->bit_rate;
1053 case AVMEDIA_TYPE_AUDIO:
1054 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1055 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1064 const char *avcodec_get_name(enum CodecID id)
1070 #include "libavcodec/codec_names.h"
1072 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1074 codec = avcodec_find_decoder(id);
1077 codec = avcodec_find_encoder(id);
1080 return "unknown_codec";
1083 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1085 int i, len, ret = 0;
1087 for (i = 0; i < 4; i++) {
1088 len = snprintf(buf, buf_size,
1089 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1091 buf_size = buf_size > len ? buf_size - len : 0;
1098 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1100 const char *codec_type;
1101 const char *codec_name;
1102 const char *profile = NULL;
1105 AVRational display_aspect_ratio;
1107 if (!buf || buf_size <= 0)
1109 codec_type = av_get_media_type_string(enc->codec_type);
1110 codec_name = avcodec_get_name(enc->codec_id);
1111 if (enc->profile != FF_PROFILE_UNKNOWN) {
1112 p = encode ? avcodec_find_encoder(enc->codec_id) :
1113 avcodec_find_decoder(enc->codec_id);
1115 profile = av_get_profile_name(p, enc->profile);
1118 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1119 codec_name, enc->mb_decision ? " (hq)" : "");
1120 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1122 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1123 if (enc->codec_tag) {
1125 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1126 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1127 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1129 switch(enc->codec_type) {
1130 case AVMEDIA_TYPE_VIDEO:
1131 if (enc->pix_fmt != PIX_FMT_NONE) {
1132 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1134 av_get_pix_fmt_name(enc->pix_fmt));
1137 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1139 enc->width, enc->height);
1140 if (enc->sample_aspect_ratio.num) {
1141 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1142 enc->width*enc->sample_aspect_ratio.num,
1143 enc->height*enc->sample_aspect_ratio.den,
1145 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1146 " [SAR %d:%d DAR %d:%d]",
1147 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1148 display_aspect_ratio.num, display_aspect_ratio.den);
1150 if(av_log_get_level() >= AV_LOG_DEBUG){
1151 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1152 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1154 enc->time_base.num/g, enc->time_base.den/g);
1158 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1159 ", q=%d-%d", enc->qmin, enc->qmax);
1162 case AVMEDIA_TYPE_AUDIO:
1163 if (enc->sample_rate) {
1164 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1165 ", %d Hz", enc->sample_rate);
1167 av_strlcat(buf, ", ", buf_size);
1168 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1169 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1170 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1171 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1178 if (enc->flags & CODEC_FLAG_PASS1)
1179 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1181 if (enc->flags & CODEC_FLAG_PASS2)
1182 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1185 bitrate = get_bit_rate(enc);
1187 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1188 ", %d kb/s", bitrate / 1000);
1192 const char *av_get_profile_name(const AVCodec *codec, int profile)
1195 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1198 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1199 if (p->profile == profile)
1205 unsigned avcodec_version( void )
1207 return LIBAVCODEC_VERSION_INT;
1210 const char *avcodec_configuration(void)
1212 return FFMPEG_CONFIGURATION;
1215 const char *avcodec_license(void)
1217 #define LICENSE_PREFIX "libavcodec license: "
1218 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1221 void avcodec_flush_buffers(AVCodecContext *avctx)
1223 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1224 ff_thread_flush(avctx);
1225 else if(avctx->codec->flush)
1226 avctx->codec->flush(avctx);
1229 void avcodec_default_free_buffers(AVCodecContext *s){
1230 AVCodecInternal *avci = s->internal;
1236 if (avci->buffer_count)
1237 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1238 avci->buffer_count);
1239 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1240 InternalBuffer *buf = &avci->buffer[i];
1242 av_freep(&buf->base[j]);
1246 av_freep(&avci->buffer);
1248 avci->buffer_count=0;
1251 #if FF_API_OLD_FF_PICT_TYPES
1252 char av_get_pict_type_char(int pict_type){
1253 return av_get_picture_type_char(pict_type);
1257 int av_get_bits_per_sample(enum CodecID codec_id){
1259 case CODEC_ID_ADPCM_SBPRO_2:
1261 case CODEC_ID_ADPCM_SBPRO_3:
1263 case CODEC_ID_ADPCM_SBPRO_4:
1264 case CODEC_ID_ADPCM_CT:
1265 case CODEC_ID_ADPCM_IMA_WAV:
1266 case CODEC_ID_ADPCM_IMA_QT:
1267 case CODEC_ID_ADPCM_SWF:
1268 case CODEC_ID_ADPCM_MS:
1269 case CODEC_ID_ADPCM_YAMAHA:
1271 case CODEC_ID_ADPCM_G722:
1272 case CODEC_ID_PCM_ALAW:
1273 case CODEC_ID_PCM_MULAW:
1274 case CODEC_ID_PCM_S8:
1275 case CODEC_ID_PCM_U8:
1276 case CODEC_ID_PCM_ZORK:
1278 case CODEC_ID_PCM_S16BE:
1279 case CODEC_ID_PCM_S16LE:
1280 case CODEC_ID_PCM_S16LE_PLANAR:
1281 case CODEC_ID_PCM_U16BE:
1282 case CODEC_ID_PCM_U16LE:
1284 case CODEC_ID_PCM_S24DAUD:
1285 case CODEC_ID_PCM_S24BE:
1286 case CODEC_ID_PCM_S24LE:
1287 case CODEC_ID_PCM_U24BE:
1288 case CODEC_ID_PCM_U24LE:
1290 case CODEC_ID_PCM_S32BE:
1291 case CODEC_ID_PCM_S32LE:
1292 case CODEC_ID_PCM_U32BE:
1293 case CODEC_ID_PCM_U32LE:
1294 case CODEC_ID_PCM_F32BE:
1295 case CODEC_ID_PCM_F32LE:
1297 case CODEC_ID_PCM_F64BE:
1298 case CODEC_ID_PCM_F64LE:
1305 #if FF_API_OLD_SAMPLE_FMT
1306 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1307 return av_get_bytes_per_sample(sample_fmt) << 3;
1312 int ff_thread_init(AVCodecContext *s){
1317 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1331 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1333 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1337 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1339 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1340 "version to the newest one from Git. If the problem still "
1341 "occurs, it means that your file has a feature which has not "
1342 "been implemented.\n", feature);
1344 av_log_ask_for_sample(avc, NULL);
1347 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1349 va_list argument_list;
1351 va_start(argument_list, msg);
1354 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1355 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1356 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1357 "and contact the ffmpeg-devel mailing list.\n");
1359 va_end(argument_list);
1362 static AVHWAccel *first_hwaccel = NULL;
1364 void av_register_hwaccel(AVHWAccel *hwaccel)
1366 AVHWAccel **p = &first_hwaccel;
1370 hwaccel->next = NULL;
1373 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1375 return hwaccel ? hwaccel->next : first_hwaccel;
1378 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1380 AVHWAccel *hwaccel=NULL;
1382 while((hwaccel= av_hwaccel_next(hwaccel))){
1383 if ( hwaccel->id == codec_id
1384 && hwaccel->pix_fmt == pix_fmt)
1390 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1392 if (ff_lockmgr_cb) {
1393 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1395 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1401 if (ff_lockmgr_cb) {
1402 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1404 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1410 int avpriv_lock_avformat(void)
1412 if (ff_lockmgr_cb) {
1413 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1419 int avpriv_unlock_avformat(void)
1421 if (ff_lockmgr_cb) {
1422 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1428 unsigned int avpriv_toupper4(unsigned int x)
1430 return toupper( x &0xFF)
1431 + (toupper((x>>8 )&0xFF)<<8 )
1432 + (toupper((x>>16)&0xFF)<<16)
1433 + (toupper((x>>24)&0xFF)<<24);
1438 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1442 ff_init_buffer_info(avctx, f);
1444 return avctx->get_buffer(avctx, f);
1447 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1449 f->owner->release_buffer(f->owner, f);
1452 void ff_thread_finish_setup(AVCodecContext *avctx)
1456 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1460 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1466 #if FF_API_THREAD_INIT
1467 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1469 s->thread_count = thread_count;
1470 return ff_thread_init(s);
1474 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1476 AVCodec *c= avcodec_find_decoder(codec_id);
1478 c= avcodec_find_encoder(codec_id);
1482 if (codec_id <= CODEC_ID_NONE)
1483 return AVMEDIA_TYPE_UNKNOWN;
1484 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1485 return AVMEDIA_TYPE_VIDEO;
1486 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1487 return AVMEDIA_TYPE_AUDIO;
1488 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1489 return AVMEDIA_TYPE_SUBTITLE;
1491 return AVMEDIA_TYPE_UNKNOWN;