3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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"
43 #include "bytestream.h"
49 static int volatile entangled_thread_counter=0;
50 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
51 static void *codec_mutex;
52 static void *avformat_mutex;
54 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
59 min_size= FFMAX(17*min_size/16 + 32, min_size);
61 ptr= av_realloc(ptr, min_size);
62 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
70 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
75 min_size= FFMAX(17*min_size/16 + 32, min_size);
77 *p = av_malloc(min_size);
78 if (!*p) min_size = 0;
82 /* encoder management */
83 static AVCodec *first_avcodec = NULL;
85 AVCodec *av_codec_next(AVCodec *c){
87 else return first_avcodec;
90 #if !FF_API_AVCODEC_INIT
93 void avcodec_init(void)
95 static int initialized = 0;
101 dsputil_static_init();
104 void avcodec_register(AVCodec *codec)
109 while (*p != NULL) p = &(*p)->next;
113 if (codec->init_static_data)
114 codec->init_static_data(codec);
117 unsigned avcodec_get_edge_width(void)
122 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
123 s->coded_width = width;
124 s->coded_height= height;
125 s->width = -((-width )>>s->lowres);
126 s->height= -((-height)>>s->lowres);
129 #define INTERNAL_BUFFER_SIZE (32+1)
131 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
132 int linesize_align[AV_NUM_DATA_POINTERS])
139 case PIX_FMT_YUV420P:
140 case PIX_FMT_YUYV422:
141 case PIX_FMT_UYVY422:
142 case PIX_FMT_YUV422P:
143 case PIX_FMT_YUV440P:
144 case PIX_FMT_YUV444P:
147 case PIX_FMT_GRAY16BE:
148 case PIX_FMT_GRAY16LE:
149 case PIX_FMT_YUVJ420P:
150 case PIX_FMT_YUVJ422P:
151 case PIX_FMT_YUVJ440P:
152 case PIX_FMT_YUVJ444P:
153 case PIX_FMT_YUVA420P:
154 case PIX_FMT_YUV420P9LE:
155 case PIX_FMT_YUV420P9BE:
156 case PIX_FMT_YUV420P10LE:
157 case PIX_FMT_YUV420P10BE:
158 case PIX_FMT_YUV422P9LE:
159 case PIX_FMT_YUV422P9BE:
160 case PIX_FMT_YUV422P10LE:
161 case PIX_FMT_YUV422P10BE:
162 case PIX_FMT_YUV444P9LE:
163 case PIX_FMT_YUV444P9BE:
164 case PIX_FMT_YUV444P10LE:
165 case PIX_FMT_YUV444P10BE:
166 case PIX_FMT_GBRP9LE:
167 case PIX_FMT_GBRP9BE:
168 case PIX_FMT_GBRP10LE:
169 case PIX_FMT_GBRP10BE:
170 w_align = 16; //FIXME assume 16 pixel per macroblock
171 h_align = 16 * 2; // interlaced needs 2 macroblocks height
173 case PIX_FMT_YUV411P:
174 case PIX_FMT_UYYVYY411:
178 case PIX_FMT_YUV410P:
179 if(s->codec_id == CODEC_ID_SVQ1){
184 if(s->codec_id == CODEC_ID_RPZA){
191 if(s->codec_id == CODEC_ID_SMC){
197 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
208 *width = FFALIGN(*width , w_align);
209 *height= FFALIGN(*height, h_align);
210 if(s->codec_id == CODEC_ID_H264 || s->lowres)
211 *height+=2; // some of the optimized chroma MC reads one line too much
212 // which is also done in mpeg decoders with lowres > 0
214 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
215 linesize_align[i] = 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) {
224 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
225 linesize_align[i] = 16;
230 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
231 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
232 int linesize_align[AV_NUM_DATA_POINTERS];
234 avcodec_align_dimensions2(s, width, height, linesize_align);
235 align = FFMAX(linesize_align[0], linesize_align[3]);
236 linesize_align[1] <<= chroma_shift;
237 linesize_align[2] <<= chroma_shift;
238 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
239 *width=FFALIGN(*width, align);
242 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
244 AVCodecInternal *avci = avctx->internal;
246 int buf_size, ret, i, needs_extended_data;
248 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
249 frame->nb_samples, avctx->sample_fmt,
252 return AVERROR(EINVAL);
254 needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
255 avctx->channels > AV_NUM_DATA_POINTERS;
257 /* allocate InternalBuffer if needed */
259 avci->buffer = av_mallocz(sizeof(InternalBuffer));
261 return AVERROR(ENOMEM);
265 /* if there is a previously-used internal buffer, check its size and
266 channel count to see if we can reuse it */
267 if (buf->extended_data) {
268 /* if current buffer is too small, free it */
269 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
270 av_free(buf->extended_data[0]);
271 if (buf->extended_data != buf->data)
272 av_free(&buf->extended_data);
273 buf->extended_data = NULL;
276 /* if number of channels has changed, reset and/or free extended data
277 pointers but leave data buffer in buf->data[0] for reuse */
278 if (buf->nb_channels != avctx->channels) {
279 if (buf->extended_data != buf->data)
280 av_free(buf->extended_data);
281 buf->extended_data = NULL;
285 /* if there is no previous buffer or the previous buffer cannot be used
286 as-is, allocate a new buffer and/or rearrange the channel pointers */
287 if (!buf->extended_data) {
288 /* if the channel pointers will fit, just set extended_data to data,
289 otherwise allocate the extended_data channel pointers */
290 if (needs_extended_data) {
291 buf->extended_data = av_mallocz(avctx->channels *
292 sizeof(*buf->extended_data));
293 if (!buf->extended_data)
294 return AVERROR(ENOMEM);
296 buf->extended_data = buf->data;
299 /* if there is a previous buffer and it is large enough, reuse it and
300 just fill-in new channel pointers and linesize, otherwise allocate
302 if (buf->extended_data[0]) {
303 ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
304 buf->extended_data[0], avctx->channels,
305 frame->nb_samples, avctx->sample_fmt,
308 ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
309 avctx->channels, frame->nb_samples,
310 avctx->sample_fmt, 32);
315 /* if data was not used for extended_data, we need to copy as many of
316 the extended_data channel pointers as will fit */
317 if (needs_extended_data) {
318 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
319 buf->data[i] = buf->extended_data[i];
321 buf->audio_data_size = buf_size;
322 buf->nb_channels = avctx->channels;
325 /* copy InternalBuffer info to the AVFrame */
326 frame->type = FF_BUFFER_TYPE_INTERNAL;
327 frame->extended_data = buf->extended_data;
328 frame->linesize[0] = buf->linesize[0];
329 memcpy(frame->data, buf->data, sizeof(frame->data));
331 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
332 else frame->pkt_pts = AV_NOPTS_VALUE;
333 frame->reordered_opaque = avctx->reordered_opaque;
335 if (avctx->debug & FF_DEBUG_BUFFERS)
336 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
337 "internal audio buffer used\n", frame);
342 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
348 AVCodecInternal *avci = s->internal;
350 if(pic->data[0]!=NULL) {
351 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
354 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
355 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
359 if(av_image_check_size(w, h, 0, s))
363 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
364 sizeof(InternalBuffer));
367 buf = &avci->buffer[avci->buffer_count];
369 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
370 if(s->active_thread_type&FF_THREAD_FRAME) {
371 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
375 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
376 av_freep(&buf->base[i]);
382 int h_chroma_shift, v_chroma_shift;
387 int stride_align[AV_NUM_DATA_POINTERS];
388 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
390 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
392 avcodec_align_dimensions2(s, &w, &h, stride_align);
394 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
400 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
401 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
402 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
403 // increase alignment of w for next try (rhs gives the lowest bit set in w)
408 unaligned |= picture.linesize[i] % stride_align[i];
412 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
416 for (i=0; i<3 && picture.data[i+1]; i++)
417 size[i] = picture.data[i+1] - picture.data[i];
418 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
420 memset(buf->base, 0, sizeof(buf->base));
421 memset(buf->data, 0, sizeof(buf->data));
423 for(i=0; i<4 && size[i]; i++){
424 const int h_shift= i==0 ? 0 : h_chroma_shift;
425 const int v_shift= i==0 ? 0 : v_chroma_shift;
427 buf->linesize[i]= picture.linesize[i];
429 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
430 if(buf->base[i]==NULL) return -1;
431 memset(buf->base[i], 128, size[i]);
433 // no edge if EDGE EMU or not planar YUV
434 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
435 buf->data[i] = buf->base[i];
437 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
439 for (; i < AV_NUM_DATA_POINTERS; i++) {
440 buf->base[i] = buf->data[i] = NULL;
441 buf->linesize[i] = 0;
443 if(size[1] && !size[2])
444 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
445 buf->width = s->width;
446 buf->height = s->height;
447 buf->pix_fmt= s->pix_fmt;
449 pic->type= FF_BUFFER_TYPE_INTERNAL;
451 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
452 pic->base[i]= buf->base[i];
453 pic->data[i]= buf->data[i];
454 pic->linesize[i]= buf->linesize[i];
456 pic->extended_data = pic->data;
457 avci->buffer_count++;
459 if(s->pkt) pic->pkt_pts= s->pkt->pts;
460 else pic->pkt_pts= AV_NOPTS_VALUE;
461 pic->reordered_opaque= s->reordered_opaque;
463 if(s->debug&FF_DEBUG_BUFFERS)
464 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
465 "buffers used\n", pic, avci->buffer_count);
470 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
472 switch (avctx->codec_type) {
473 case AVMEDIA_TYPE_VIDEO:
474 return video_get_buffer(avctx, frame);
475 case AVMEDIA_TYPE_AUDIO:
476 return audio_get_buffer(avctx, frame);
482 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
484 InternalBuffer *buf, *last;
485 AVCodecInternal *avci = s->internal;
487 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
489 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
490 assert(avci->buffer_count);
493 buf = NULL; /* avoids warning */
494 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
495 buf = &avci->buffer[i];
496 if (buf->data[0] == pic->data[0])
499 assert(i < avci->buffer_count);
500 avci->buffer_count--;
501 last = &avci->buffer[avci->buffer_count];
504 FFSWAP(InternalBuffer, *buf, *last);
507 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
509 // pic->base[i]=NULL;
511 //printf("R%X\n", pic->opaque);
513 if(s->debug&FF_DEBUG_BUFFERS)
514 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
515 "buffers used\n", pic, avci->buffer_count);
518 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
522 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
524 /* If no picture return a new buffer */
525 if(pic->data[0] == NULL) {
526 /* We will copy from buffer, so must be readable */
527 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
528 return s->get_buffer(s, pic);
531 /* If internal buffer type return the same buffer */
532 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
533 if(s->pkt) pic->pkt_pts= s->pkt->pts;
534 else pic->pkt_pts= AV_NOPTS_VALUE;
535 pic->reordered_opaque= s->reordered_opaque;
540 * Not internal type and reget_buffer not overridden, emulate cr buffer
543 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
544 pic->data[i] = pic->base[i] = NULL;
546 /* Allocate new frame */
547 if (s->get_buffer(s, pic))
549 /* Copy image data from old buffer to new buffer */
550 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
552 s->release_buffer(s, &temp_pic); // Release old frame
556 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
559 for(i=0; i<count; i++){
560 int r= func(c, (char*)arg + i*size);
566 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
569 for(i=0; i<count; i++){
570 int r= func(c, arg, i, 0);
576 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
577 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
582 void avcodec_get_frame_defaults(AVFrame *pic){
583 memset(pic, 0, sizeof(AVFrame));
585 pic->pts= AV_NOPTS_VALUE;
587 pic->sample_aspect_ratio = (AVRational){0, 1};
588 pic->format = -1; /* unknown */
591 AVFrame *avcodec_alloc_frame(void){
592 AVFrame *pic= av_malloc(sizeof(AVFrame));
594 if(pic==NULL) return NULL;
596 avcodec_get_frame_defaults(pic);
601 #if FF_API_AVCODEC_OPEN
602 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
604 return avcodec_open2(avctx, codec, NULL);
608 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
611 AVDictionary *tmp = NULL;
614 av_dict_copy(&tmp, *options, 0);
616 /* If there is a user-supplied mutex locking routine, call it. */
618 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
622 entangled_thread_counter++;
623 if(entangled_thread_counter != 1){
624 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
629 if(avctx->codec || !codec) {
630 ret = AVERROR(EINVAL);
634 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
635 if (!avctx->internal) {
636 ret = AVERROR(ENOMEM);
640 if (codec->priv_data_size > 0) {
641 if(!avctx->priv_data){
642 avctx->priv_data = av_mallocz(codec->priv_data_size);
643 if (!avctx->priv_data) {
644 ret = AVERROR(ENOMEM);
647 if (codec->priv_class) {
648 *(AVClass**)avctx->priv_data= codec->priv_class;
649 av_opt_set_defaults(avctx->priv_data);
652 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
655 avctx->priv_data = NULL;
657 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
660 if(avctx->coded_width && avctx->coded_height)
661 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
662 else if(avctx->width && avctx->height)
663 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
665 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
666 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
667 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
668 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
669 avcodec_set_dimensions(avctx, 0, 0);
672 /* if the decoder init function was already called previously,
673 free the already allocated subtitle_header before overwriting it */
675 av_freep(&avctx->subtitle_header);
677 #define SANE_NB_CHANNELS 128U
678 if (avctx->channels > SANE_NB_CHANNELS) {
679 ret = AVERROR(EINVAL);
683 avctx->codec = codec;
684 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
685 avctx->codec_id == CODEC_ID_NONE) {
686 avctx->codec_type = codec->type;
687 avctx->codec_id = codec->id;
689 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
690 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
691 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
692 ret = AVERROR(EINVAL);
695 avctx->frame_number = 0;
698 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
699 avctx->error_recognition, avctx->err_recognition);
700 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
701 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
702 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
703 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
704 avctx->error_recognition, avctx->err_recognition);
707 if (HAVE_THREADS && !avctx->thread_opaque) {
708 ret = ff_thread_init(avctx);
713 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
714 avctx->thread_count = 1;
716 if (avctx->codec->max_lowres < avctx->lowres) {
717 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
718 avctx->codec->max_lowres);
719 ret = AVERROR(EINVAL);
722 if (avctx->codec->encode) {
724 if (avctx->codec->sample_fmts) {
725 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
726 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
728 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
729 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
730 ret = AVERROR(EINVAL);
734 if (avctx->codec->supported_samplerates) {
735 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
736 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
738 if (avctx->codec->supported_samplerates[i] == 0) {
739 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
740 ret = AVERROR(EINVAL);
744 if (avctx->codec->channel_layouts) {
745 if (!avctx->channel_layout) {
746 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
748 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
749 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
751 if (avctx->codec->channel_layouts[i] == 0) {
752 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
753 ret = AVERROR(EINVAL);
758 if (avctx->channel_layout && avctx->channels) {
759 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
760 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
761 ret = AVERROR(EINVAL);
764 } else if (avctx->channel_layout) {
765 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
769 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
770 ret = avctx->codec->init(avctx);
776 entangled_thread_counter--;
778 /* Release any user-supplied mutex. */
780 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
783 av_dict_free(options);
790 av_freep(&avctx->priv_data);
791 av_freep(&avctx->internal);
796 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
797 const short *samples)
799 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
800 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
803 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
804 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
805 avctx->frame_number++;
811 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
814 if(buf_size < FF_MIN_BUFFER_SIZE){
815 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
818 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
820 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
821 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
822 avctx->frame_number++;
823 emms_c(); //needed to avoid an emms_c() call before every return;
830 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
831 const AVSubtitle *sub)
834 if(sub->start_display_time) {
835 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
838 if(sub->num_rects == 0 || !sub->rects)
840 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
841 avctx->frame_number++;
845 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
851 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
854 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
855 if (!data || size < 4)
857 flags = bytestream_get_le32(&data);
859 if (size < 4) /* Required for any of the changes */
861 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
862 avctx->channels = bytestream_get_le32(&data);
865 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
868 avctx->channel_layout = bytestream_get_le64(&data);
873 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
874 avctx->sample_rate = bytestream_get_le32(&data);
877 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
880 avctx->width = bytestream_get_le32(&data);
881 avctx->height = bytestream_get_le32(&data);
882 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
887 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
888 int *got_picture_ptr,
894 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
898 apply_param_change(avctx, avpkt);
900 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
901 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
902 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
905 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
907 picture->pkt_dts= avpkt->dts;
908 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
909 picture->width = avctx->width;
910 picture->height = avctx->height;
911 picture->format = avctx->pix_fmt;
914 emms_c(); //needed to avoid an emms_c() call before every return;
916 if (*got_picture_ptr)
917 avctx->frame_number++;
924 #if FF_API_OLD_DECODE_AUDIO
925 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
930 int ret, got_frame = 0;
932 if (avctx->get_buffer != avcodec_default_get_buffer) {
933 av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
934 "avcodec_decode_audio3()\n");
935 return AVERROR(EINVAL);
938 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
940 if (ret >= 0 && got_frame) {
942 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
943 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
945 avctx->sample_fmt, 1);
946 if (*frame_size_ptr < data_size) {
947 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
948 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
949 return AVERROR(EINVAL);
952 memcpy(samples, frame.extended_data[0], plane_size);
954 if (planar && avctx->channels > 1) {
955 uint8_t *out = ((uint8_t *)samples) + plane_size;
956 for (ch = 1; ch < avctx->channels; ch++) {
957 memcpy(out, frame.extended_data[ch], plane_size);
961 *frame_size_ptr = data_size;
969 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
980 if (!avpkt->data && avpkt->size) {
981 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
982 return AVERROR(EINVAL);
985 apply_param_change(avctx, avpkt);
987 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
988 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
989 if (ret >= 0 && *got_frame_ptr) {
990 avctx->frame_number++;
991 frame->pkt_dts = avpkt->dts;
992 if (frame->format == AV_SAMPLE_FMT_NONE)
993 frame->format = avctx->sample_fmt;
999 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1007 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1009 avctx->frame_number++;
1013 void avsubtitle_free(AVSubtitle *sub)
1017 for (i = 0; i < sub->num_rects; i++)
1019 av_freep(&sub->rects[i]->pict.data[0]);
1020 av_freep(&sub->rects[i]->pict.data[1]);
1021 av_freep(&sub->rects[i]->pict.data[2]);
1022 av_freep(&sub->rects[i]->pict.data[3]);
1023 av_freep(&sub->rects[i]->text);
1024 av_freep(&sub->rects[i]->ass);
1025 av_freep(&sub->rects[i]);
1028 av_freep(&sub->rects);
1030 memset(sub, 0, sizeof(AVSubtitle));
1033 av_cold int avcodec_close(AVCodecContext *avctx)
1035 /* If there is a user-supplied mutex locking routine, call it. */
1036 if (ff_lockmgr_cb) {
1037 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1041 entangled_thread_counter++;
1042 if(entangled_thread_counter != 1){
1043 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1044 entangled_thread_counter--;
1048 if (HAVE_THREADS && avctx->thread_opaque)
1049 ff_thread_free(avctx);
1050 if (avctx->codec && avctx->codec->close)
1051 avctx->codec->close(avctx);
1052 avcodec_default_free_buffers(avctx);
1053 avctx->coded_frame = NULL;
1054 av_freep(&avctx->internal);
1055 if (avctx->codec && avctx->codec->priv_class)
1056 av_opt_free(avctx->priv_data);
1058 av_freep(&avctx->priv_data);
1059 if(avctx->codec && avctx->codec->encode)
1060 av_freep(&avctx->extradata);
1061 avctx->codec = NULL;
1062 avctx->active_thread_type = 0;
1063 entangled_thread_counter--;
1065 /* Release any user-supplied mutex. */
1066 if (ff_lockmgr_cb) {
1067 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1072 AVCodec *avcodec_find_encoder(enum CodecID id)
1074 AVCodec *p, *experimental=NULL;
1077 if (p->encode != NULL && p->id == id) {
1078 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1085 return experimental;
1088 AVCodec *avcodec_find_encoder_by_name(const char *name)
1095 if (p->encode != NULL && strcmp(name,p->name) == 0)
1102 AVCodec *avcodec_find_decoder(enum CodecID id)
1107 if (p->decode != NULL && p->id == id)
1114 AVCodec *avcodec_find_decoder_by_name(const char *name)
1121 if (p->decode != NULL && strcmp(name,p->name) == 0)
1128 static int get_bit_rate(AVCodecContext *ctx)
1131 int bits_per_sample;
1133 switch(ctx->codec_type) {
1134 case AVMEDIA_TYPE_VIDEO:
1135 case AVMEDIA_TYPE_DATA:
1136 case AVMEDIA_TYPE_SUBTITLE:
1137 case AVMEDIA_TYPE_ATTACHMENT:
1138 bit_rate = ctx->bit_rate;
1140 case AVMEDIA_TYPE_AUDIO:
1141 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1142 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1151 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1153 int i, len, ret = 0;
1155 for (i = 0; i < 4; i++) {
1156 len = snprintf(buf, buf_size,
1157 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1159 buf_size = buf_size > len ? buf_size - len : 0;
1166 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1168 const char *codec_name;
1169 const char *profile = NULL;
1173 AVRational display_aspect_ratio;
1176 p = avcodec_find_encoder(enc->codec_id);
1178 p = avcodec_find_decoder(enc->codec_id);
1181 codec_name = p->name;
1182 profile = av_get_profile_name(p, enc->profile);
1183 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1184 /* fake mpeg2 transport stream codec (currently not
1186 codec_name = "mpeg2ts";
1187 } else if (enc->codec_name[0] != '\0') {
1188 codec_name = enc->codec_name;
1190 /* output avi tags */
1192 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1193 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1197 switch(enc->codec_type) {
1198 case AVMEDIA_TYPE_VIDEO:
1199 snprintf(buf, buf_size,
1201 codec_name, enc->mb_decision ? " (hq)" : "");
1203 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1205 if (enc->pix_fmt != PIX_FMT_NONE) {
1206 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1208 av_get_pix_fmt_name(enc->pix_fmt));
1211 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1213 enc->width, enc->height);
1214 if (enc->sample_aspect_ratio.num) {
1215 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1216 enc->width*enc->sample_aspect_ratio.num,
1217 enc->height*enc->sample_aspect_ratio.den,
1219 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1220 " [PAR %d:%d DAR %d:%d]",
1221 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1222 display_aspect_ratio.num, display_aspect_ratio.den);
1224 if(av_log_get_level() >= AV_LOG_DEBUG){
1225 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1226 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1228 enc->time_base.num/g, enc->time_base.den/g);
1232 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1233 ", q=%d-%d", enc->qmin, enc->qmax);
1236 case AVMEDIA_TYPE_AUDIO:
1237 snprintf(buf, buf_size,
1241 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1243 if (enc->sample_rate) {
1244 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1245 ", %d Hz", enc->sample_rate);
1247 av_strlcat(buf, ", ", buf_size);
1248 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1249 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1250 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1251 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1254 case AVMEDIA_TYPE_DATA:
1255 snprintf(buf, buf_size, "Data: %s", codec_name);
1257 case AVMEDIA_TYPE_SUBTITLE:
1258 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1260 case AVMEDIA_TYPE_ATTACHMENT:
1261 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1264 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1268 if (enc->flags & CODEC_FLAG_PASS1)
1269 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1271 if (enc->flags & CODEC_FLAG_PASS2)
1272 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1275 bitrate = get_bit_rate(enc);
1277 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1278 ", %d kb/s", bitrate / 1000);
1282 const char *av_get_profile_name(const AVCodec *codec, int profile)
1285 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1288 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1289 if (p->profile == profile)
1295 unsigned avcodec_version( void )
1297 return LIBAVCODEC_VERSION_INT;
1300 const char *avcodec_configuration(void)
1302 return LIBAV_CONFIGURATION;
1305 const char *avcodec_license(void)
1307 #define LICENSE_PREFIX "libavcodec license: "
1308 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1311 void avcodec_flush_buffers(AVCodecContext *avctx)
1313 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1314 ff_thread_flush(avctx);
1315 else if(avctx->codec->flush)
1316 avctx->codec->flush(avctx);
1319 static void video_free_buffers(AVCodecContext *s)
1321 AVCodecInternal *avci = s->internal;
1327 if (avci->buffer_count)
1328 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1329 avci->buffer_count);
1330 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1331 InternalBuffer *buf = &avci->buffer[i];
1333 av_freep(&buf->base[j]);
1337 av_freep(&avci->buffer);
1339 avci->buffer_count=0;
1342 static void audio_free_buffers(AVCodecContext *avctx)
1344 AVCodecInternal *avci = avctx->internal;
1345 InternalBuffer *buf;
1351 if (buf->extended_data) {
1352 av_free(buf->extended_data[0]);
1353 if (buf->extended_data != buf->data)
1354 av_free(buf->extended_data);
1356 av_freep(&avci->buffer);
1359 void avcodec_default_free_buffers(AVCodecContext *avctx)
1361 switch (avctx->codec_type) {
1362 case AVMEDIA_TYPE_VIDEO:
1363 video_free_buffers(avctx);
1365 case AVMEDIA_TYPE_AUDIO:
1366 audio_free_buffers(avctx);
1373 #if FF_API_OLD_FF_PICT_TYPES
1374 char av_get_pict_type_char(int pict_type){
1375 return av_get_picture_type_char(pict_type);
1379 int av_get_bits_per_sample(enum CodecID codec_id){
1381 case CODEC_ID_ADPCM_SBPRO_2:
1383 case CODEC_ID_ADPCM_SBPRO_3:
1385 case CODEC_ID_ADPCM_SBPRO_4:
1386 case CODEC_ID_ADPCM_CT:
1387 case CODEC_ID_ADPCM_IMA_WAV:
1388 case CODEC_ID_ADPCM_IMA_QT:
1389 case CODEC_ID_ADPCM_SWF:
1390 case CODEC_ID_ADPCM_MS:
1391 case CODEC_ID_ADPCM_YAMAHA:
1392 case CODEC_ID_ADPCM_G722:
1394 case CODEC_ID_PCM_ALAW:
1395 case CODEC_ID_PCM_MULAW:
1396 case CODEC_ID_PCM_S8:
1397 case CODEC_ID_PCM_U8:
1398 case CODEC_ID_PCM_ZORK:
1400 case CODEC_ID_PCM_S16BE:
1401 case CODEC_ID_PCM_S16LE:
1402 case CODEC_ID_PCM_S16LE_PLANAR:
1403 case CODEC_ID_PCM_U16BE:
1404 case CODEC_ID_PCM_U16LE:
1406 case CODEC_ID_PCM_S24DAUD:
1407 case CODEC_ID_PCM_S24BE:
1408 case CODEC_ID_PCM_S24LE:
1409 case CODEC_ID_PCM_U24BE:
1410 case CODEC_ID_PCM_U24LE:
1412 case CODEC_ID_PCM_S32BE:
1413 case CODEC_ID_PCM_S32LE:
1414 case CODEC_ID_PCM_U32BE:
1415 case CODEC_ID_PCM_U32LE:
1416 case CODEC_ID_PCM_F32BE:
1417 case CODEC_ID_PCM_F32LE:
1419 case CODEC_ID_PCM_F64BE:
1420 case CODEC_ID_PCM_F64LE:
1427 #if FF_API_OLD_SAMPLE_FMT
1428 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1429 return av_get_bytes_per_sample(sample_fmt) << 3;
1434 int ff_thread_init(AVCodecContext *s){
1439 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1453 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1455 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1459 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1461 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1462 "version to the newest one from Git. If the problem still "
1463 "occurs, it means that your file has a feature which has not "
1464 "been implemented.\n", feature);
1466 av_log_ask_for_sample(avc, NULL);
1469 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1471 va_list argument_list;
1473 va_start(argument_list, msg);
1476 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1477 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1478 "of this file to ftp://upload.libav.org/incoming/ "
1479 "and contact the libav-devel mailing list.\n");
1481 va_end(argument_list);
1484 static AVHWAccel *first_hwaccel = NULL;
1486 void av_register_hwaccel(AVHWAccel *hwaccel)
1488 AVHWAccel **p = &first_hwaccel;
1492 hwaccel->next = NULL;
1495 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1497 return hwaccel ? hwaccel->next : first_hwaccel;
1500 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1502 AVHWAccel *hwaccel=NULL;
1504 while((hwaccel= av_hwaccel_next(hwaccel))){
1505 if ( hwaccel->id == codec_id
1506 && hwaccel->pix_fmt == pix_fmt)
1512 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1514 if (ff_lockmgr_cb) {
1515 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1517 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1523 if (ff_lockmgr_cb) {
1524 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1526 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1532 int avpriv_lock_avformat(void)
1534 if (ff_lockmgr_cb) {
1535 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1541 int avpriv_unlock_avformat(void)
1543 if (ff_lockmgr_cb) {
1544 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1550 unsigned int avpriv_toupper4(unsigned int x)
1552 return toupper( x &0xFF)
1553 + (toupper((x>>8 )&0xFF)<<8 )
1554 + (toupper((x>>16)&0xFF)<<16)
1555 + (toupper((x>>24)&0xFF)<<24);
1560 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1563 return avctx->get_buffer(avctx, f);
1566 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1568 f->owner->release_buffer(f->owner, f);
1571 void ff_thread_finish_setup(AVCodecContext *avctx)
1575 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1579 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1585 #if FF_API_THREAD_INIT
1586 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1588 s->thread_count = thread_count;
1589 return ff_thread_init(s);
1593 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1595 if (codec_id <= CODEC_ID_NONE)
1596 return AVMEDIA_TYPE_UNKNOWN;
1597 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1598 return AVMEDIA_TYPE_VIDEO;
1599 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1600 return AVMEDIA_TYPE_AUDIO;
1601 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1602 return AVMEDIA_TYPE_SUBTITLE;
1604 return AVMEDIA_TYPE_UNKNOWN;