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;
52 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
57 min_size= FFMAX(17*min_size/16 + 32, min_size);
59 ptr= av_realloc(ptr, min_size);
60 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
68 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
73 min_size= FFMAX(17*min_size/16 + 32, min_size);
75 *p = av_malloc(min_size);
76 if (!*p) min_size = 0;
80 /* encoder management */
81 static AVCodec *first_avcodec = NULL;
83 AVCodec *av_codec_next(AVCodec *c){
85 else return first_avcodec;
88 void avcodec_register(AVCodec *codec)
93 while (*p != NULL) p = &(*p)->next;
98 unsigned avcodec_get_edge_width(void)
103 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
104 s->coded_width = width;
105 s->coded_height= height;
106 s->width = -((-width )>>s->lowres);
107 s->height= -((-height)>>s->lowres);
110 typedef struct InternalBuffer{
116 enum PixelFormat pix_fmt;
119 #define INTERNAL_BUFFER_SIZE (32+1)
121 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
126 case PIX_FMT_YUV420P:
127 case PIX_FMT_YUYV422:
128 case PIX_FMT_UYVY422:
129 case PIX_FMT_YUV422P:
130 case PIX_FMT_YUV440P:
131 case PIX_FMT_YUV444P:
133 case PIX_FMT_GRAY16BE:
134 case PIX_FMT_GRAY16LE:
135 case PIX_FMT_YUVJ420P:
136 case PIX_FMT_YUVJ422P:
137 case PIX_FMT_YUVJ440P:
138 case PIX_FMT_YUVJ444P:
139 case PIX_FMT_YUVA420P:
140 case PIX_FMT_YUV420P9LE:
141 case PIX_FMT_YUV420P9BE:
142 case PIX_FMT_YUV420P10LE:
143 case PIX_FMT_YUV420P10BE:
144 case PIX_FMT_YUV422P10LE:
145 case PIX_FMT_YUV422P10BE:
146 case PIX_FMT_YUV444P9LE:
147 case PIX_FMT_YUV444P9BE:
148 case PIX_FMT_YUV444P10LE:
149 case PIX_FMT_YUV444P10BE:
150 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
152 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)
153 h_align= 32; // interlaced is rounded up to 2 MBs
155 case PIX_FMT_YUV411P:
156 case PIX_FMT_UYYVYY411:
160 case PIX_FMT_YUV410P:
161 if(s->codec_id == CODEC_ID_SVQ1){
166 if(s->codec_id == CODEC_ID_RPZA){
173 if(s->codec_id == CODEC_ID_SMC){
179 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
190 *width = FFALIGN(*width , w_align);
191 *height= FFALIGN(*height, h_align);
192 if(s->codec_id == CODEC_ID_H264 || s->lowres)
193 *height+=2; // some of the optimized chroma MC reads one line too much
194 // which is also done in mpeg decoders with lowres > 0
199 linesize_align[3] = STRIDE_ALIGN;
200 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
201 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
202 //picture size unneccessarily in some cases. The solution here is not
203 //pretty and better ideas are welcome!
205 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
206 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
207 s->codec_id == CODEC_ID_VP6A) {
210 linesize_align[2] = 16;
215 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
216 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
217 int linesize_align[4];
219 avcodec_align_dimensions2(s, width, height, linesize_align);
220 align = FFMAX(linesize_align[0], linesize_align[3]);
221 linesize_align[1] <<= chroma_shift;
222 linesize_align[2] <<= chroma_shift;
223 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
224 *width=FFALIGN(*width, align);
227 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
234 if(pic->data[0]!=NULL) {
235 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
238 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
239 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
243 if(av_image_check_size(w, h, 0, s))
246 if(s->internal_buffer==NULL){
247 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
250 s->internal_buffer= av_fast_realloc(
252 &s->internal_buffer_size,
253 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
257 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
258 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
261 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
262 if(s->active_thread_type&FF_THREAD_FRAME) {
263 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
268 av_freep(&buf->base[i]);
274 pic->age= *picture_number - buf->last_pic_num;
275 buf->last_pic_num= *picture_number;
277 int h_chroma_shift, v_chroma_shift;
283 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
285 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
287 avcodec_align_dimensions2(s, &w, &h, stride_align);
289 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
295 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
296 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
297 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
298 // increase alignment of w for next try (rhs gives the lowest bit set in w)
303 unaligned |= picture.linesize[i] % stride_align[i];
307 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
311 for (i=0; i<3 && picture.data[i+1]; i++)
312 size[i] = picture.data[i+1] - picture.data[i];
313 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
315 buf->last_pic_num= -256*256*256*64;
316 memset(buf->base, 0, sizeof(buf->base));
317 memset(buf->data, 0, sizeof(buf->data));
319 for(i=0; i<4 && size[i]; i++){
320 const int h_shift= i==0 ? 0 : h_chroma_shift;
321 const int v_shift= i==0 ? 0 : v_chroma_shift;
323 buf->linesize[i]= picture.linesize[i];
325 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
326 if(buf->base[i]==NULL) return -1;
327 memset(buf->base[i], 128, size[i]);
329 // no edge if EDGE EMU or not planar YUV
330 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
331 buf->data[i] = buf->base[i];
333 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
335 if(size[1] && !size[2])
336 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
337 buf->width = s->width;
338 buf->height = s->height;
339 buf->pix_fmt= s->pix_fmt;
340 pic->age= 256*256*256*64;
342 pic->type= FF_BUFFER_TYPE_INTERNAL;
345 pic->base[i]= buf->base[i];
346 pic->data[i]= buf->data[i];
347 pic->linesize[i]= buf->linesize[i];
349 s->internal_buffer_count++;
352 pic->pkt_pts = s->pkt->pts;
353 pic->pkt_pos = s->pkt->pos;
355 pic->pkt_pts = AV_NOPTS_VALUE;
358 pic->reordered_opaque= s->reordered_opaque;
359 pic->sample_aspect_ratio = s->sample_aspect_ratio;
360 pic->width = s->width;
361 pic->height = s->height;
362 pic->format = s->pix_fmt;
364 if(s->debug&FF_DEBUG_BUFFERS)
365 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
370 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
372 InternalBuffer *buf, *last;
374 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
375 assert(s->internal_buffer_count);
377 if(s->internal_buffer){
378 buf = NULL; /* avoids warning */
379 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
380 buf= &((InternalBuffer*)s->internal_buffer)[i];
381 if(buf->data[0] == pic->data[0])
384 assert(i < s->internal_buffer_count);
385 s->internal_buffer_count--;
386 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
388 FFSWAP(InternalBuffer, *buf, *last);
393 // pic->base[i]=NULL;
395 //printf("R%X\n", pic->opaque);
397 if(s->debug&FF_DEBUG_BUFFERS)
398 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
401 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
405 /* If no picture return a new buffer */
406 if(pic->data[0] == NULL) {
407 /* We will copy from buffer, so must be readable */
408 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
409 return s->get_buffer(s, pic);
412 /* If internal buffer type return the same buffer */
413 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
414 if(s->pkt) pic->pkt_pts= s->pkt->pts;
415 else pic->pkt_pts= AV_NOPTS_VALUE;
416 pic->reordered_opaque= s->reordered_opaque;
421 * Not internal type and reget_buffer not overridden, emulate cr buffer
424 for(i = 0; i < 4; i++)
425 pic->data[i] = pic->base[i] = NULL;
427 /* Allocate new frame */
428 if (s->get_buffer(s, pic))
430 /* Copy image data from old buffer to new buffer */
431 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
433 s->release_buffer(s, &temp_pic); // Release old frame
437 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
440 for(i=0; i<count; i++){
441 int r= func(c, (char*)arg + i*size);
447 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
450 for(i=0; i<count; i++){
451 int r= func(c, arg, i, 0);
457 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
458 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
463 void avcodec_get_frame_defaults(AVFrame *pic){
464 memset(pic, 0, sizeof(AVFrame));
466 pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
469 pic->sample_aspect_ratio = (AVRational){0, 1};
470 pic->format = -1; /* unknown */
473 AVFrame *avcodec_alloc_frame(void){
474 AVFrame *pic= av_malloc(sizeof(AVFrame));
476 if(pic==NULL) return NULL;
478 avcodec_get_frame_defaults(pic);
483 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
485 memset(sub, 0, sizeof(*sub));
486 sub->pts = AV_NOPTS_VALUE;
489 #if FF_API_AVCODEC_OPEN
490 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
492 return avcodec_open2(avctx, codec, NULL);
496 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
499 AVDictionary *tmp = NULL;
502 av_dict_copy(&tmp, *options, 0);
504 /* If there is a user-supplied mutex locking routine, call it. */
506 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
510 entangled_thread_counter++;
511 if(entangled_thread_counter != 1){
512 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
517 if(avctx->codec || !codec) {
518 ret = AVERROR(EINVAL);
522 if (codec->priv_data_size > 0) {
523 if(!avctx->priv_data){
524 avctx->priv_data = av_mallocz(codec->priv_data_size);
525 if (!avctx->priv_data) {
526 ret = AVERROR(ENOMEM);
529 if (codec->priv_class) {
530 *(AVClass**)avctx->priv_data= codec->priv_class;
531 av_opt_set_defaults(avctx->priv_data);
534 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp) < 0))
537 avctx->priv_data = NULL;
539 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
542 if(avctx->coded_width && avctx->coded_height)
543 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
544 else if(avctx->width && avctx->height)
545 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
547 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
548 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
549 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
550 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
551 avcodec_set_dimensions(avctx, 0, 0);
554 /* if the decoder init function was already called previously,
555 free the already allocated subtitle_header before overwriting it */
557 av_freep(&avctx->subtitle_header);
559 #define SANE_NB_CHANNELS 128U
560 if (avctx->channels > SANE_NB_CHANNELS) {
561 ret = AVERROR(EINVAL);
565 avctx->codec = codec;
566 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
567 avctx->codec_id == CODEC_ID_NONE) {
568 avctx->codec_type = codec->type;
569 avctx->codec_id = codec->id;
571 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
572 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
573 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
574 ret = AVERROR(EINVAL);
577 avctx->frame_number = 0;
579 if (HAVE_THREADS && !avctx->thread_opaque) {
580 ret = ff_thread_init(avctx);
586 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
587 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
588 avctx->codec->max_lowres);
589 ret = AVERROR(EINVAL);
592 if (avctx->codec->encode) {
594 if (avctx->codec->sample_fmts) {
595 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
596 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
598 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
599 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
600 ret = AVERROR(EINVAL);
604 if (avctx->codec->supported_samplerates) {
605 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
606 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
608 if (avctx->codec->supported_samplerates[i] == 0) {
609 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
610 ret = AVERROR(EINVAL);
614 if (avctx->codec->channel_layouts) {
615 if (!avctx->channel_layout) {
616 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
618 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
619 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
621 if (avctx->codec->channel_layouts[i] == 0) {
622 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
623 ret = AVERROR(EINVAL);
628 if (avctx->channel_layout && avctx->channels) {
629 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
630 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
631 ret = AVERROR(EINVAL);
634 } else if (avctx->channel_layout) {
635 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
639 avctx->pts_correction_num_faulty_pts =
640 avctx->pts_correction_num_faulty_dts = 0;
641 avctx->pts_correction_last_pts =
642 avctx->pts_correction_last_dts = INT64_MIN;
644 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
645 ret = avctx->codec->init(avctx);
653 entangled_thread_counter--;
655 /* Release any user-supplied mutex. */
657 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
660 av_dict_free(options);
667 av_freep(&avctx->priv_data);
672 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
673 const short *samples)
675 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
676 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
679 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
680 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
681 avctx->frame_number++;
687 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
690 if(buf_size < FF_MIN_BUFFER_SIZE){
691 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
694 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
696 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
697 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
698 avctx->frame_number++;
699 emms_c(); //needed to avoid an emms_c() call before every return;
706 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
707 const AVSubtitle *sub)
710 if(sub->start_display_time) {
711 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
715 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
716 avctx->frame_number++;
721 * Attempt to guess proper monotonic timestamps for decoded video frames
722 * which might have incorrect times. Input timestamps may wrap around, in
723 * which case the output will as well.
725 * @param pts the pts field of the decoded AVPacket, as passed through
727 * @param dts the dts field of the decoded AVPacket
728 * @return one of the input values, may be AV_NOPTS_VALUE
730 static int64_t guess_correct_pts(AVCodecContext *ctx,
731 int64_t reordered_pts, int64_t dts)
733 int64_t pts = AV_NOPTS_VALUE;
735 if (dts != AV_NOPTS_VALUE) {
736 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
737 ctx->pts_correction_last_dts = dts;
739 if (reordered_pts != AV_NOPTS_VALUE) {
740 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
741 ctx->pts_correction_last_pts = reordered_pts;
743 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
744 && reordered_pts != AV_NOPTS_VALUE)
752 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
753 int *got_picture_ptr,
759 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
762 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
763 av_packet_split_side_data(avpkt);
765 if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
766 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
769 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
771 picture->pkt_dts= avpkt->dts;
773 if(!avctx->has_b_frames){
774 picture->pkt_pos= avpkt->pos;
776 //FIXME these should be under if(!avctx->has_b_frames)
777 if (!picture->sample_aspect_ratio.num)
778 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
780 picture->width = avctx->width;
781 if (!picture->height)
782 picture->height = avctx->height;
783 if (picture->format == PIX_FMT_NONE)
784 picture->format = avctx->pix_fmt;
787 emms_c(); //needed to avoid an emms_c() call before every return;
790 if (*got_picture_ptr){
791 avctx->frame_number++;
792 picture->best_effort_timestamp = guess_correct_pts(avctx,
802 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
810 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
811 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
812 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
813 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
816 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
817 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
818 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
822 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
823 avctx->frame_number++;
831 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
839 avcodec_get_subtitle_defaults(sub);
840 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
842 avctx->frame_number++;
846 void avsubtitle_free(AVSubtitle *sub)
850 for (i = 0; i < sub->num_rects; i++)
852 av_freep(&sub->rects[i]->pict.data[0]);
853 av_freep(&sub->rects[i]->pict.data[1]);
854 av_freep(&sub->rects[i]->pict.data[2]);
855 av_freep(&sub->rects[i]->pict.data[3]);
856 av_freep(&sub->rects[i]->text);
857 av_freep(&sub->rects[i]->ass);
858 av_freep(&sub->rects[i]);
861 av_freep(&sub->rects);
863 memset(sub, 0, sizeof(AVSubtitle));
866 av_cold int avcodec_close(AVCodecContext *avctx)
868 /* If there is a user-supplied mutex locking routine, call it. */
870 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
874 entangled_thread_counter++;
875 if(entangled_thread_counter != 1){
876 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
877 entangled_thread_counter--;
881 if (HAVE_THREADS && avctx->thread_opaque)
882 ff_thread_free(avctx);
883 if (avctx->codec && avctx->codec->close)
884 avctx->codec->close(avctx);
885 avcodec_default_free_buffers(avctx);
886 avctx->coded_frame = NULL;
887 if (avctx->codec && avctx->codec->priv_class)
888 av_opt_free(avctx->priv_data);
890 av_freep(&avctx->priv_data);
891 if(avctx->codec && avctx->codec->encode)
892 av_freep(&avctx->extradata);
894 avctx->active_thread_type = 0;
895 entangled_thread_counter--;
897 /* Release any user-supplied mutex. */
899 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
904 AVCodec *avcodec_find_encoder(enum CodecID id)
906 AVCodec *p, *experimental=NULL;
909 if (p->encode != NULL && p->id == id) {
910 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
920 AVCodec *avcodec_find_encoder_by_name(const char *name)
927 if (p->encode != NULL && strcmp(name,p->name) == 0)
934 AVCodec *avcodec_find_decoder(enum CodecID id)
936 AVCodec *p, *experimental=NULL;
939 if (p->decode != NULL && p->id == id) {
940 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
950 AVCodec *avcodec_find_decoder_by_name(const char *name)
957 if (p->decode != NULL && strcmp(name,p->name) == 0)
964 static int get_bit_rate(AVCodecContext *ctx)
969 switch(ctx->codec_type) {
970 case AVMEDIA_TYPE_VIDEO:
971 case AVMEDIA_TYPE_DATA:
972 case AVMEDIA_TYPE_SUBTITLE:
973 case AVMEDIA_TYPE_ATTACHMENT:
974 bit_rate = ctx->bit_rate;
976 case AVMEDIA_TYPE_AUDIO:
977 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
978 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
987 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
991 for (i = 0; i < 4; i++) {
992 len = snprintf(buf, buf_size,
993 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
995 buf_size = buf_size > len ? buf_size - len : 0;
1002 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1004 const char *codec_name;
1005 const char *profile = NULL;
1009 AVRational display_aspect_ratio;
1012 p = avcodec_find_encoder(enc->codec_id);
1014 p = avcodec_find_decoder(enc->codec_id);
1017 codec_name = p->name;
1018 profile = av_get_profile_name(p, enc->profile);
1019 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1020 /* fake mpeg2 transport stream codec (currently not
1022 codec_name = "mpeg2ts";
1023 } else if (enc->codec_name[0] != '\0') {
1024 codec_name = enc->codec_name;
1026 /* output avi tags */
1028 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1029 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1033 switch(enc->codec_type) {
1034 case AVMEDIA_TYPE_VIDEO:
1035 snprintf(buf, buf_size,
1037 codec_name, enc->mb_decision ? " (hq)" : "");
1039 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1041 if (enc->pix_fmt != PIX_FMT_NONE) {
1042 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1044 av_get_pix_fmt_name(enc->pix_fmt));
1047 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1049 enc->width, enc->height);
1050 if (enc->sample_aspect_ratio.num) {
1051 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1052 enc->width*enc->sample_aspect_ratio.num,
1053 enc->height*enc->sample_aspect_ratio.den,
1055 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1056 " [PAR %d:%d DAR %d:%d]",
1057 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1058 display_aspect_ratio.num, display_aspect_ratio.den);
1060 if(av_log_get_level() >= AV_LOG_DEBUG){
1061 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1062 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1064 enc->time_base.num/g, enc->time_base.den/g);
1068 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1069 ", q=%d-%d", enc->qmin, enc->qmax);
1072 case AVMEDIA_TYPE_AUDIO:
1073 snprintf(buf, buf_size,
1077 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1079 if (enc->sample_rate) {
1080 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1081 ", %d Hz", enc->sample_rate);
1083 av_strlcat(buf, ", ", buf_size);
1084 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1085 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1086 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1087 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1090 case AVMEDIA_TYPE_DATA:
1091 snprintf(buf, buf_size, "Data: %s", codec_name);
1093 case AVMEDIA_TYPE_SUBTITLE:
1094 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1096 case AVMEDIA_TYPE_ATTACHMENT:
1097 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1100 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1104 if (enc->flags & CODEC_FLAG_PASS1)
1105 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1107 if (enc->flags & CODEC_FLAG_PASS2)
1108 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1111 bitrate = get_bit_rate(enc);
1113 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1114 ", %d kb/s", bitrate / 1000);
1118 const char *av_get_profile_name(const AVCodec *codec, int profile)
1121 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1124 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1125 if (p->profile == profile)
1131 unsigned avcodec_version( void )
1133 return LIBAVCODEC_VERSION_INT;
1136 const char *avcodec_configuration(void)
1138 return FFMPEG_CONFIGURATION;
1141 const char *avcodec_license(void)
1143 #define LICENSE_PREFIX "libavcodec license: "
1144 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1147 void avcodec_init(void)
1149 static int initialized = 0;
1151 if (initialized != 0)
1155 dsputil_static_init();
1158 void avcodec_flush_buffers(AVCodecContext *avctx)
1160 if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1161 ff_thread_flush(avctx);
1162 if(avctx->codec->flush)
1163 avctx->codec->flush(avctx);
1166 void avcodec_default_free_buffers(AVCodecContext *s){
1169 if(s->internal_buffer==NULL) return;
1171 if (s->internal_buffer_count)
1172 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1173 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1174 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1176 av_freep(&buf->base[j]);
1180 av_freep(&s->internal_buffer);
1182 s->internal_buffer_count=0;
1185 #if FF_API_OLD_FF_PICT_TYPES
1186 char av_get_pict_type_char(int pict_type){
1187 return av_get_picture_type_char(pict_type);
1191 int av_get_bits_per_sample(enum CodecID codec_id){
1193 case CODEC_ID_ADPCM_SBPRO_2:
1195 case CODEC_ID_ADPCM_SBPRO_3:
1197 case CODEC_ID_ADPCM_SBPRO_4:
1198 case CODEC_ID_ADPCM_CT:
1199 case CODEC_ID_ADPCM_IMA_WAV:
1200 case CODEC_ID_ADPCM_MS:
1201 case CODEC_ID_ADPCM_YAMAHA:
1203 case CODEC_ID_ADPCM_G722:
1204 case CODEC_ID_PCM_ALAW:
1205 case CODEC_ID_PCM_MULAW:
1206 case CODEC_ID_PCM_S8:
1207 case CODEC_ID_PCM_U8:
1208 case CODEC_ID_PCM_ZORK:
1210 case CODEC_ID_PCM_S16BE:
1211 case CODEC_ID_PCM_S16LE:
1212 case CODEC_ID_PCM_S16LE_PLANAR:
1213 case CODEC_ID_PCM_U16BE:
1214 case CODEC_ID_PCM_U16LE:
1216 case CODEC_ID_PCM_S24DAUD:
1217 case CODEC_ID_PCM_S24BE:
1218 case CODEC_ID_PCM_S24LE:
1219 case CODEC_ID_PCM_U24BE:
1220 case CODEC_ID_PCM_U24LE:
1222 case CODEC_ID_PCM_S32BE:
1223 case CODEC_ID_PCM_S32LE:
1224 case CODEC_ID_PCM_U32BE:
1225 case CODEC_ID_PCM_U32LE:
1226 case CODEC_ID_PCM_F32BE:
1227 case CODEC_ID_PCM_F32LE:
1229 case CODEC_ID_PCM_F64BE:
1230 case CODEC_ID_PCM_F64LE:
1237 #if FF_API_OLD_SAMPLE_FMT
1238 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1239 return av_get_bytes_per_sample(sample_fmt) << 3;
1244 int ff_thread_init(AVCodecContext *s){
1249 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1263 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1265 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1269 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1271 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1272 "version to the newest one from Git. If the problem still "
1273 "occurs, it means that your file has a feature which has not "
1274 "been implemented.\n", feature);
1276 av_log_ask_for_sample(avc, NULL);
1279 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1281 va_list argument_list;
1283 va_start(argument_list, msg);
1286 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1287 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1288 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1289 "and contact the ffmpeg-devel mailing list.\n");
1291 va_end(argument_list);
1294 static AVHWAccel *first_hwaccel = NULL;
1296 void av_register_hwaccel(AVHWAccel *hwaccel)
1298 AVHWAccel **p = &first_hwaccel;
1302 hwaccel->next = NULL;
1305 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1307 return hwaccel ? hwaccel->next : first_hwaccel;
1310 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1312 AVHWAccel *hwaccel=NULL;
1314 while((hwaccel= av_hwaccel_next(hwaccel))){
1315 if ( hwaccel->id == codec_id
1316 && hwaccel->pix_fmt == pix_fmt)
1322 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1324 if (ff_lockmgr_cb) {
1325 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1331 if (ff_lockmgr_cb) {
1332 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1338 unsigned int ff_toupper4(unsigned int x)
1340 return toupper( x &0xFF)
1341 + (toupper((x>>8 )&0xFF)<<8 )
1342 + (toupper((x>>16)&0xFF)<<16)
1343 + (toupper((x>>24)&0xFF)<<24);
1348 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1351 return avctx->get_buffer(avctx, f);
1354 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1356 f->owner->release_buffer(f->owner, f);
1359 void ff_thread_finish_setup(AVCodecContext *avctx)
1363 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1367 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1373 #if FF_API_THREAD_INIT
1374 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1376 s->thread_count = thread_count;
1377 return ff_thread_init(s);