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 #if !FF_API_AVCODEC_INIT
91 void avcodec_init(void)
93 static int initialized = 0;
99 dsputil_static_init();
102 void avcodec_register(AVCodec *codec)
107 while (*p != NULL) p = &(*p)->next;
111 if (codec->init_static_data)
112 codec->init_static_data(codec);
115 unsigned avcodec_get_edge_width(void)
120 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
121 s->coded_width = width;
122 s->coded_height= height;
123 s->width = -((-width )>>s->lowres);
124 s->height= -((-height)>>s->lowres);
127 typedef struct InternalBuffer{
133 enum PixelFormat pix_fmt;
136 #define INTERNAL_BUFFER_SIZE (32+1)
138 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
143 case PIX_FMT_YUV420P:
144 case PIX_FMT_YUYV422:
145 case PIX_FMT_UYVY422:
146 case PIX_FMT_YUV422P:
147 case PIX_FMT_YUV440P:
148 case PIX_FMT_YUV444P:
150 case PIX_FMT_GRAY16BE:
151 case PIX_FMT_GRAY16LE:
152 case PIX_FMT_YUVJ420P:
153 case PIX_FMT_YUVJ422P:
154 case PIX_FMT_YUVJ440P:
155 case PIX_FMT_YUVJ444P:
156 case PIX_FMT_YUVA420P:
157 case PIX_FMT_YUV420P9LE:
158 case PIX_FMT_YUV420P9BE:
159 case PIX_FMT_YUV420P10LE:
160 case PIX_FMT_YUV420P10BE:
161 case PIX_FMT_YUV422P9LE:
162 case PIX_FMT_YUV422P9BE:
163 case PIX_FMT_YUV422P10LE:
164 case PIX_FMT_YUV422P10BE:
165 case PIX_FMT_YUV444P9LE:
166 case PIX_FMT_YUV444P9BE:
167 case PIX_FMT_YUV444P10LE:
168 case PIX_FMT_YUV444P10BE:
169 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
171 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)
172 h_align= 32; // interlaced is rounded up to 2 MBs
174 case PIX_FMT_YUV411P:
175 case PIX_FMT_UYYVYY411:
179 case PIX_FMT_YUV410P:
180 if(s->codec_id == CODEC_ID_SVQ1){
185 if(s->codec_id == CODEC_ID_RPZA){
192 if(s->codec_id == CODEC_ID_SMC){
198 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
209 *width = FFALIGN(*width , w_align);
210 *height= FFALIGN(*height, h_align);
211 if(s->codec_id == CODEC_ID_H264 || s->lowres)
212 *height+=2; // some of the optimized chroma MC reads one line too much
213 // which is also done in mpeg decoders with lowres > 0
218 linesize_align[3] = STRIDE_ALIGN;
219 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
220 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
221 //picture size unneccessarily in some cases. The solution here is not
222 //pretty and better ideas are welcome!
224 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
225 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
226 s->codec_id == CODEC_ID_VP6A) {
229 linesize_align[2] = 16;
234 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
235 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
236 int linesize_align[4];
238 avcodec_align_dimensions2(s, width, height, linesize_align);
239 align = FFMAX(linesize_align[0], linesize_align[3]);
240 linesize_align[1] <<= chroma_shift;
241 linesize_align[2] <<= chroma_shift;
242 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
243 *width=FFALIGN(*width, align);
246 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
249 pic->pkt_pts = s->pkt->pts;
250 pic->pkt_pos = s->pkt->pos;
252 pic->pkt_pts = AV_NOPTS_VALUE;
255 pic->reordered_opaque= s->reordered_opaque;
256 pic->sample_aspect_ratio = s->sample_aspect_ratio;
257 pic->width = s->width;
258 pic->height = s->height;
259 pic->format = s->pix_fmt;
262 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
269 if(pic->data[0]!=NULL) {
270 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
273 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
274 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
278 if(av_image_check_size(w, h, 0, s))
281 if(s->internal_buffer==NULL){
282 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
285 s->internal_buffer= av_fast_realloc(
287 &s->internal_buffer_size,
288 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
292 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
293 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
296 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
297 if(s->active_thread_type&FF_THREAD_FRAME) {
298 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
303 av_freep(&buf->base[i]);
309 pic->age= *picture_number - buf->last_pic_num;
310 buf->last_pic_num= *picture_number;
312 int h_chroma_shift, v_chroma_shift;
318 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
320 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
322 avcodec_align_dimensions2(s, &w, &h, stride_align);
324 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
330 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
331 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
332 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
333 // increase alignment of w for next try (rhs gives the lowest bit set in w)
338 unaligned |= picture.linesize[i] % stride_align[i];
342 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
346 for (i=0; i<3 && picture.data[i+1]; i++)
347 size[i] = picture.data[i+1] - picture.data[i];
348 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
350 buf->last_pic_num= -256*256*256*64;
351 memset(buf->base, 0, sizeof(buf->base));
352 memset(buf->data, 0, sizeof(buf->data));
354 for(i=0; i<4 && size[i]; i++){
355 const int h_shift= i==0 ? 0 : h_chroma_shift;
356 const int v_shift= i==0 ? 0 : v_chroma_shift;
358 buf->linesize[i]= picture.linesize[i];
360 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
361 if(buf->base[i]==NULL) return -1;
362 memset(buf->base[i], 128, size[i]);
364 // no edge if EDGE EMU or not planar YUV
365 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
366 buf->data[i] = buf->base[i];
368 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
370 if(size[1] && !size[2])
371 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
372 buf->width = s->width;
373 buf->height = s->height;
374 buf->pix_fmt= s->pix_fmt;
375 pic->age= 256*256*256*64;
377 pic->type= FF_BUFFER_TYPE_INTERNAL;
380 pic->base[i]= buf->base[i];
381 pic->data[i]= buf->data[i];
382 pic->linesize[i]= buf->linesize[i];
384 s->internal_buffer_count++;
387 pic->pkt_pts = s->pkt->pts;
388 pic->pkt_pos = s->pkt->pos;
390 pic->pkt_pts = AV_NOPTS_VALUE;
393 pic->reordered_opaque= s->reordered_opaque;
394 pic->sample_aspect_ratio = s->sample_aspect_ratio;
395 pic->width = s->width;
396 pic->height = s->height;
397 pic->format = s->pix_fmt;
399 if(s->debug&FF_DEBUG_BUFFERS)
400 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
405 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
407 InternalBuffer *buf, *last;
409 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
410 assert(s->internal_buffer_count);
412 if(s->internal_buffer){
413 buf = NULL; /* avoids warning */
414 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
415 buf= &((InternalBuffer*)s->internal_buffer)[i];
416 if(buf->data[0] == pic->data[0])
419 assert(i < s->internal_buffer_count);
420 s->internal_buffer_count--;
421 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
423 FFSWAP(InternalBuffer, *buf, *last);
428 // pic->base[i]=NULL;
430 //printf("R%X\n", pic->opaque);
432 if(s->debug&FF_DEBUG_BUFFERS)
433 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
436 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
440 /* If no picture return a new buffer */
441 if(pic->data[0] == NULL) {
442 /* We will copy from buffer, so must be readable */
443 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
444 return s->get_buffer(s, pic);
447 /* If internal buffer type return the same buffer */
448 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
449 if(s->pkt) pic->pkt_pts= s->pkt->pts;
450 else pic->pkt_pts= AV_NOPTS_VALUE;
451 pic->reordered_opaque= s->reordered_opaque;
456 * Not internal type and reget_buffer not overridden, emulate cr buffer
459 for(i = 0; i < 4; i++)
460 pic->data[i] = pic->base[i] = NULL;
462 /* Allocate new frame */
463 if (s->get_buffer(s, pic))
465 /* Copy image data from old buffer to new buffer */
466 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
468 s->release_buffer(s, &temp_pic); // Release old frame
472 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
475 for(i=0; i<count; i++){
476 int r= func(c, (char*)arg + i*size);
482 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
485 for(i=0; i<count; i++){
486 int r= func(c, arg, i, 0);
492 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
493 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
498 void avcodec_get_frame_defaults(AVFrame *pic){
499 memset(pic, 0, sizeof(AVFrame));
501 pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
504 pic->sample_aspect_ratio = (AVRational){0, 1};
505 pic->format = -1; /* unknown */
508 AVFrame *avcodec_alloc_frame(void){
509 AVFrame *pic= av_malloc(sizeof(AVFrame));
511 if(pic==NULL) return NULL;
513 avcodec_get_frame_defaults(pic);
518 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
520 memset(sub, 0, sizeof(*sub));
521 sub->pts = AV_NOPTS_VALUE;
524 #if FF_API_AVCODEC_OPEN
525 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
527 return avcodec_open2(avctx, codec, NULL);
531 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
534 AVDictionary *tmp = NULL;
537 av_dict_copy(&tmp, *options, 0);
539 /* If there is a user-supplied mutex locking routine, call it. */
541 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
545 entangled_thread_counter++;
546 if(entangled_thread_counter != 1){
547 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
552 if(avctx->codec || !codec) {
553 ret = AVERROR(EINVAL);
557 if (codec->priv_data_size > 0) {
558 if(!avctx->priv_data){
559 avctx->priv_data = av_mallocz(codec->priv_data_size);
560 if (!avctx->priv_data) {
561 ret = AVERROR(ENOMEM);
564 if (codec->priv_class) {
565 *(AVClass**)avctx->priv_data= codec->priv_class;
566 av_opt_set_defaults(avctx->priv_data);
569 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
572 avctx->priv_data = NULL;
574 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
577 if(avctx->coded_width && avctx->coded_height)
578 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
579 else if(avctx->width && avctx->height)
580 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
582 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
583 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
584 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
585 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
586 avcodec_set_dimensions(avctx, 0, 0);
589 /* if the decoder init function was already called previously,
590 free the already allocated subtitle_header before overwriting it */
592 av_freep(&avctx->subtitle_header);
594 #define SANE_NB_CHANNELS 128U
595 if (avctx->channels > SANE_NB_CHANNELS) {
596 ret = AVERROR(EINVAL);
600 avctx->codec = codec;
601 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
602 avctx->codec_id == CODEC_ID_NONE) {
603 avctx->codec_type = codec->type;
604 avctx->codec_id = codec->id;
606 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
607 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
608 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
609 ret = AVERROR(EINVAL);
612 avctx->frame_number = 0;
615 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
617 if (HAVE_THREADS && !avctx->thread_opaque) {
618 ret = ff_thread_init(avctx);
624 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
625 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
626 avctx->codec->max_lowres);
627 avctx->lowres = avctx->codec->max_lowres;
629 if (avctx->codec->encode) {
631 if (avctx->codec->sample_fmts) {
632 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
633 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
635 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
636 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
637 ret = AVERROR(EINVAL);
641 if (avctx->codec->supported_samplerates) {
642 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
643 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
645 if (avctx->codec->supported_samplerates[i] == 0) {
646 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
647 ret = AVERROR(EINVAL);
651 if (avctx->codec->channel_layouts) {
652 if (!avctx->channel_layout) {
653 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
655 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
656 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
658 if (avctx->codec->channel_layouts[i] == 0) {
659 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
660 ret = AVERROR(EINVAL);
665 if (avctx->channel_layout && avctx->channels) {
666 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
667 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
668 ret = AVERROR(EINVAL);
671 } else if (avctx->channel_layout) {
672 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
676 avctx->pts_correction_num_faulty_pts =
677 avctx->pts_correction_num_faulty_dts = 0;
678 avctx->pts_correction_last_pts =
679 avctx->pts_correction_last_dts = INT64_MIN;
681 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
682 ret = avctx->codec->init(avctx);
690 entangled_thread_counter--;
692 /* Release any user-supplied mutex. */
694 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
697 av_dict_free(options);
704 av_freep(&avctx->priv_data);
709 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
710 const short *samples)
712 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
713 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
716 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
717 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
718 avctx->frame_number++;
724 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
727 if(buf_size < FF_MIN_BUFFER_SIZE){
728 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
731 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
733 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
734 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
735 avctx->frame_number++;
736 emms_c(); //needed to avoid an emms_c() call before every return;
743 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
744 const AVSubtitle *sub)
747 if(sub->start_display_time) {
748 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
752 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
753 avctx->frame_number++;
758 * Attempt to guess proper monotonic timestamps for decoded video frames
759 * which might have incorrect times. Input timestamps may wrap around, in
760 * which case the output will as well.
762 * @param pts the pts field of the decoded AVPacket, as passed through
764 * @param dts the dts field of the decoded AVPacket
765 * @return one of the input values, may be AV_NOPTS_VALUE
767 static int64_t guess_correct_pts(AVCodecContext *ctx,
768 int64_t reordered_pts, int64_t dts)
770 int64_t pts = AV_NOPTS_VALUE;
772 if (dts != AV_NOPTS_VALUE) {
773 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
774 ctx->pts_correction_last_dts = dts;
776 if (reordered_pts != AV_NOPTS_VALUE) {
777 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
778 ctx->pts_correction_last_pts = reordered_pts;
780 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
781 && reordered_pts != AV_NOPTS_VALUE)
789 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
790 int *got_picture_ptr,
796 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
799 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
800 av_packet_split_side_data(avpkt);
802 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
803 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
806 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
808 picture->pkt_dts= avpkt->dts;
810 if(!avctx->has_b_frames){
811 picture->pkt_pos= avpkt->pos;
813 //FIXME these should be under if(!avctx->has_b_frames)
814 if (!picture->sample_aspect_ratio.num)
815 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
817 picture->width = avctx->width;
818 if (!picture->height)
819 picture->height = avctx->height;
820 if (picture->format == PIX_FMT_NONE)
821 picture->format = avctx->pix_fmt;
824 emms_c(); //needed to avoid an emms_c() call before every return;
827 if (*got_picture_ptr){
828 avctx->frame_number++;
829 picture->best_effort_timestamp = guess_correct_pts(avctx,
839 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
847 if (!avpkt->data && avpkt->size) {
848 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
849 return AVERROR(EINVAL);
852 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
853 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
854 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
855 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
858 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
859 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
860 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
864 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
865 avctx->frame_number++;
873 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
881 avcodec_get_subtitle_defaults(sub);
882 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
884 avctx->frame_number++;
888 void avsubtitle_free(AVSubtitle *sub)
892 for (i = 0; i < sub->num_rects; i++)
894 av_freep(&sub->rects[i]->pict.data[0]);
895 av_freep(&sub->rects[i]->pict.data[1]);
896 av_freep(&sub->rects[i]->pict.data[2]);
897 av_freep(&sub->rects[i]->pict.data[3]);
898 av_freep(&sub->rects[i]->text);
899 av_freep(&sub->rects[i]->ass);
900 av_freep(&sub->rects[i]);
903 av_freep(&sub->rects);
905 memset(sub, 0, sizeof(AVSubtitle));
908 av_cold int avcodec_close(AVCodecContext *avctx)
910 /* If there is a user-supplied mutex locking routine, call it. */
912 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
916 entangled_thread_counter++;
917 if(entangled_thread_counter != 1){
918 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
919 entangled_thread_counter--;
923 if (HAVE_THREADS && avctx->thread_opaque)
924 ff_thread_free(avctx);
925 if (avctx->codec && avctx->codec->close)
926 avctx->codec->close(avctx);
927 avcodec_default_free_buffers(avctx);
928 avctx->coded_frame = NULL;
929 if (avctx->codec && avctx->codec->priv_class)
930 av_opt_free(avctx->priv_data);
932 av_freep(&avctx->priv_data);
933 if(avctx->codec && avctx->codec->encode)
934 av_freep(&avctx->extradata);
936 avctx->active_thread_type = 0;
937 entangled_thread_counter--;
939 /* Release any user-supplied mutex. */
941 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
946 AVCodec *avcodec_find_encoder(enum CodecID id)
948 AVCodec *p, *experimental=NULL;
951 if (p->encode != NULL && p->id == id) {
952 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
962 AVCodec *avcodec_find_encoder_by_name(const char *name)
969 if (p->encode != NULL && strcmp(name,p->name) == 0)
976 AVCodec *avcodec_find_decoder(enum CodecID id)
978 AVCodec *p, *experimental=NULL;
981 if (p->decode != NULL && p->id == id) {
982 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
992 AVCodec *avcodec_find_decoder_by_name(const char *name)
999 if (p->decode != NULL && strcmp(name,p->name) == 0)
1006 static int get_bit_rate(AVCodecContext *ctx)
1009 int bits_per_sample;
1011 switch(ctx->codec_type) {
1012 case AVMEDIA_TYPE_VIDEO:
1013 case AVMEDIA_TYPE_DATA:
1014 case AVMEDIA_TYPE_SUBTITLE:
1015 case AVMEDIA_TYPE_ATTACHMENT:
1016 bit_rate = ctx->bit_rate;
1018 case AVMEDIA_TYPE_AUDIO:
1019 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1020 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1029 const char *avcodec_get_name(enum CodecID id)
1035 #include "libavcodec/codec_names.h"
1037 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1039 codec = avcodec_find_decoder(id);
1042 codec = avcodec_find_encoder(id);
1045 return "unknown_codec";
1048 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1050 int i, len, ret = 0;
1052 for (i = 0; i < 4; i++) {
1053 len = snprintf(buf, buf_size,
1054 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1056 buf_size = buf_size > len ? buf_size - len : 0;
1063 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1065 const char *codec_type;
1066 const char *codec_name;
1067 const char *profile = NULL;
1070 AVRational display_aspect_ratio;
1072 if (!buf || buf_size <= 0)
1074 codec_type = av_get_media_type_string(enc->codec_type);
1075 codec_name = avcodec_get_name(enc->codec_id);
1076 if (enc->profile != FF_PROFILE_UNKNOWN) {
1077 p = encode ? avcodec_find_encoder(enc->codec_id) :
1078 avcodec_find_decoder(enc->codec_id);
1080 profile = av_get_profile_name(p, enc->profile);
1083 snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1084 codec_name, enc->mb_decision ? " (hq)" : "");
1085 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1087 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1088 if (enc->codec_tag) {
1090 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1091 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1092 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1094 switch(enc->codec_type) {
1095 case AVMEDIA_TYPE_VIDEO:
1096 if (enc->pix_fmt != PIX_FMT_NONE) {
1097 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1099 av_get_pix_fmt_name(enc->pix_fmt));
1102 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1104 enc->width, enc->height);
1105 if (enc->sample_aspect_ratio.num) {
1106 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1107 enc->width*enc->sample_aspect_ratio.num,
1108 enc->height*enc->sample_aspect_ratio.den,
1110 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1111 " [SAR %d:%d DAR %d:%d]",
1112 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1113 display_aspect_ratio.num, display_aspect_ratio.den);
1115 if(av_log_get_level() >= AV_LOG_DEBUG){
1116 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1117 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1119 enc->time_base.num/g, enc->time_base.den/g);
1123 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1124 ", q=%d-%d", enc->qmin, enc->qmax);
1127 case AVMEDIA_TYPE_AUDIO:
1128 if (enc->sample_rate) {
1129 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1130 ", %d Hz", enc->sample_rate);
1132 av_strlcat(buf, ", ", buf_size);
1133 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1134 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1135 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1136 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1143 if (enc->flags & CODEC_FLAG_PASS1)
1144 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1146 if (enc->flags & CODEC_FLAG_PASS2)
1147 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1150 bitrate = get_bit_rate(enc);
1152 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1153 ", %d kb/s", bitrate / 1000);
1157 const char *av_get_profile_name(const AVCodec *codec, int profile)
1160 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1163 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1164 if (p->profile == profile)
1170 unsigned avcodec_version( void )
1172 return LIBAVCODEC_VERSION_INT;
1175 const char *avcodec_configuration(void)
1177 return FFMPEG_CONFIGURATION;
1180 const char *avcodec_license(void)
1182 #define LICENSE_PREFIX "libavcodec license: "
1183 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1186 void avcodec_flush_buffers(AVCodecContext *avctx)
1188 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1189 ff_thread_flush(avctx);
1190 else if(avctx->codec->flush)
1191 avctx->codec->flush(avctx);
1194 void avcodec_default_free_buffers(AVCodecContext *s){
1197 if(s->internal_buffer==NULL) return;
1199 if (s->internal_buffer_count)
1200 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1201 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1202 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1204 av_freep(&buf->base[j]);
1208 av_freep(&s->internal_buffer);
1210 s->internal_buffer_count=0;
1213 #if FF_API_OLD_FF_PICT_TYPES
1214 char av_get_pict_type_char(int pict_type){
1215 return av_get_picture_type_char(pict_type);
1219 int av_get_bits_per_sample(enum CodecID codec_id){
1221 case CODEC_ID_ADPCM_SBPRO_2:
1223 case CODEC_ID_ADPCM_SBPRO_3:
1225 case CODEC_ID_ADPCM_SBPRO_4:
1226 case CODEC_ID_ADPCM_CT:
1227 case CODEC_ID_ADPCM_IMA_WAV:
1228 case CODEC_ID_ADPCM_IMA_QT:
1229 case CODEC_ID_ADPCM_SWF:
1230 case CODEC_ID_ADPCM_MS:
1231 case CODEC_ID_ADPCM_YAMAHA:
1233 case CODEC_ID_ADPCM_G722:
1234 case CODEC_ID_PCM_ALAW:
1235 case CODEC_ID_PCM_MULAW:
1236 case CODEC_ID_PCM_S8:
1237 case CODEC_ID_PCM_U8:
1238 case CODEC_ID_PCM_ZORK:
1240 case CODEC_ID_PCM_S16BE:
1241 case CODEC_ID_PCM_S16LE:
1242 case CODEC_ID_PCM_S16LE_PLANAR:
1243 case CODEC_ID_PCM_U16BE:
1244 case CODEC_ID_PCM_U16LE:
1246 case CODEC_ID_PCM_S24DAUD:
1247 case CODEC_ID_PCM_S24BE:
1248 case CODEC_ID_PCM_S24LE:
1249 case CODEC_ID_PCM_U24BE:
1250 case CODEC_ID_PCM_U24LE:
1252 case CODEC_ID_PCM_S32BE:
1253 case CODEC_ID_PCM_S32LE:
1254 case CODEC_ID_PCM_U32BE:
1255 case CODEC_ID_PCM_U32LE:
1256 case CODEC_ID_PCM_F32BE:
1257 case CODEC_ID_PCM_F32LE:
1259 case CODEC_ID_PCM_F64BE:
1260 case CODEC_ID_PCM_F64LE:
1267 #if FF_API_OLD_SAMPLE_FMT
1268 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1269 return av_get_bytes_per_sample(sample_fmt) << 3;
1274 int ff_thread_init(AVCodecContext *s){
1279 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1293 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1295 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1299 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1301 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1302 "version to the newest one from Git. If the problem still "
1303 "occurs, it means that your file has a feature which has not "
1304 "been implemented.\n", feature);
1306 av_log_ask_for_sample(avc, NULL);
1309 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1311 va_list argument_list;
1313 va_start(argument_list, msg);
1316 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1317 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1318 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1319 "and contact the ffmpeg-devel mailing list.\n");
1321 va_end(argument_list);
1324 static AVHWAccel *first_hwaccel = NULL;
1326 void av_register_hwaccel(AVHWAccel *hwaccel)
1328 AVHWAccel **p = &first_hwaccel;
1332 hwaccel->next = NULL;
1335 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1337 return hwaccel ? hwaccel->next : first_hwaccel;
1340 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1342 AVHWAccel *hwaccel=NULL;
1344 while((hwaccel= av_hwaccel_next(hwaccel))){
1345 if ( hwaccel->id == codec_id
1346 && hwaccel->pix_fmt == pix_fmt)
1352 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1354 if (ff_lockmgr_cb) {
1355 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1361 if (ff_lockmgr_cb) {
1362 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1368 unsigned int avpriv_toupper4(unsigned int x)
1370 return toupper( x &0xFF)
1371 + (toupper((x>>8 )&0xFF)<<8 )
1372 + (toupper((x>>16)&0xFF)<<16)
1373 + (toupper((x>>24)&0xFF)<<24);
1378 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1382 ff_init_buffer_info(avctx, f);
1384 return avctx->get_buffer(avctx, f);
1387 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1389 f->owner->release_buffer(f->owner, f);
1392 void ff_thread_finish_setup(AVCodecContext *avctx)
1396 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1400 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1406 #if FF_API_THREAD_INIT
1407 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1409 s->thread_count = thread_count;
1410 return ff_thread_init(s);
1414 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1416 if (codec_id <= CODEC_ID_NONE)
1417 return AVMEDIA_TYPE_UNKNOWN;
1418 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1419 return AVMEDIA_TYPE_VIDEO;
1420 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1421 return AVMEDIA_TYPE_AUDIO;
1422 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1423 return AVMEDIA_TYPE_SUBTITLE;
1425 return AVMEDIA_TYPE_UNKNOWN;