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 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
846 int *got_picture_ptr,
852 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
857 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
858 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
859 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
862 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
864 picture->pkt_dts= avpkt->dts;
865 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
866 picture->width = avctx->width;
867 picture->height = avctx->height;
868 picture->format = avctx->pix_fmt;
871 emms_c(); //needed to avoid an emms_c() call before every return;
873 if (*got_picture_ptr)
874 avctx->frame_number++;
881 #if FF_API_OLD_DECODE_AUDIO
882 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
887 int ret, got_frame = 0;
889 if (avctx->get_buffer != avcodec_default_get_buffer) {
890 av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
891 "avcodec_decode_audio3()\n");
892 return AVERROR(EINVAL);
895 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
897 if (ret >= 0 && got_frame) {
899 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
900 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
902 avctx->sample_fmt, 1);
903 if (*frame_size_ptr < data_size) {
904 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
905 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
906 return AVERROR(EINVAL);
909 memcpy(samples, frame.extended_data[0], plane_size);
911 if (planar && avctx->channels > 1) {
912 uint8_t *out = ((uint8_t *)samples) + plane_size;
913 for (ch = 1; ch < avctx->channels; ch++) {
914 memcpy(out, frame.extended_data[ch], plane_size);
918 *frame_size_ptr = data_size;
926 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
932 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
935 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
936 if (!data || size < 4)
938 flags = bytestream_get_le32(&data);
940 if (size < 4) /* Required for any of the changes */
942 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
943 avctx->channels = bytestream_get_le32(&data);
946 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
949 avctx->channel_layout = bytestream_get_le64(&data);
954 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
955 avctx->sample_rate = bytestream_get_le32(&data);
958 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
961 avctx->width = bytestream_get_le32(&data);
962 avctx->height = bytestream_get_le32(&data);
967 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
978 if (!avpkt->data && avpkt->size) {
979 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
980 return AVERROR(EINVAL);
983 apply_param_change(avctx, avpkt);
985 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
986 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
987 if (ret >= 0 && *got_frame_ptr) {
988 avctx->frame_number++;
989 frame->pkt_dts = avpkt->dts;
990 if (frame->format == AV_SAMPLE_FMT_NONE)
991 frame->format = avctx->sample_fmt;
997 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1005 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1007 avctx->frame_number++;
1011 void avsubtitle_free(AVSubtitle *sub)
1015 for (i = 0; i < sub->num_rects; i++)
1017 av_freep(&sub->rects[i]->pict.data[0]);
1018 av_freep(&sub->rects[i]->pict.data[1]);
1019 av_freep(&sub->rects[i]->pict.data[2]);
1020 av_freep(&sub->rects[i]->pict.data[3]);
1021 av_freep(&sub->rects[i]->text);
1022 av_freep(&sub->rects[i]->ass);
1023 av_freep(&sub->rects[i]);
1026 av_freep(&sub->rects);
1028 memset(sub, 0, sizeof(AVSubtitle));
1031 av_cold int avcodec_close(AVCodecContext *avctx)
1033 /* If there is a user-supplied mutex locking routine, call it. */
1034 if (ff_lockmgr_cb) {
1035 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1039 entangled_thread_counter++;
1040 if(entangled_thread_counter != 1){
1041 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1042 entangled_thread_counter--;
1046 if (HAVE_THREADS && avctx->thread_opaque)
1047 ff_thread_free(avctx);
1048 if (avctx->codec && avctx->codec->close)
1049 avctx->codec->close(avctx);
1050 avcodec_default_free_buffers(avctx);
1051 avctx->coded_frame = NULL;
1052 av_freep(&avctx->internal);
1053 if (avctx->codec && avctx->codec->priv_class)
1054 av_opt_free(avctx->priv_data);
1056 av_freep(&avctx->priv_data);
1057 if(avctx->codec && avctx->codec->encode)
1058 av_freep(&avctx->extradata);
1059 avctx->codec = NULL;
1060 avctx->active_thread_type = 0;
1061 entangled_thread_counter--;
1063 /* Release any user-supplied mutex. */
1064 if (ff_lockmgr_cb) {
1065 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1070 AVCodec *avcodec_find_encoder(enum CodecID id)
1072 AVCodec *p, *experimental=NULL;
1075 if (p->encode != NULL && p->id == id) {
1076 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1083 return experimental;
1086 AVCodec *avcodec_find_encoder_by_name(const char *name)
1093 if (p->encode != NULL && strcmp(name,p->name) == 0)
1100 AVCodec *avcodec_find_decoder(enum CodecID id)
1105 if (p->decode != NULL && p->id == id)
1112 AVCodec *avcodec_find_decoder_by_name(const char *name)
1119 if (p->decode != NULL && strcmp(name,p->name) == 0)
1126 static int get_bit_rate(AVCodecContext *ctx)
1129 int bits_per_sample;
1131 switch(ctx->codec_type) {
1132 case AVMEDIA_TYPE_VIDEO:
1133 case AVMEDIA_TYPE_DATA:
1134 case AVMEDIA_TYPE_SUBTITLE:
1135 case AVMEDIA_TYPE_ATTACHMENT:
1136 bit_rate = ctx->bit_rate;
1138 case AVMEDIA_TYPE_AUDIO:
1139 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1140 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1149 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1151 int i, len, ret = 0;
1153 for (i = 0; i < 4; i++) {
1154 len = snprintf(buf, buf_size,
1155 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1157 buf_size = buf_size > len ? buf_size - len : 0;
1164 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1166 const char *codec_name;
1167 const char *profile = NULL;
1171 AVRational display_aspect_ratio;
1174 p = avcodec_find_encoder(enc->codec_id);
1176 p = avcodec_find_decoder(enc->codec_id);
1179 codec_name = p->name;
1180 profile = av_get_profile_name(p, enc->profile);
1181 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1182 /* fake mpeg2 transport stream codec (currently not
1184 codec_name = "mpeg2ts";
1185 } else if (enc->codec_name[0] != '\0') {
1186 codec_name = enc->codec_name;
1188 /* output avi tags */
1190 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1191 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1195 switch(enc->codec_type) {
1196 case AVMEDIA_TYPE_VIDEO:
1197 snprintf(buf, buf_size,
1199 codec_name, enc->mb_decision ? " (hq)" : "");
1201 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1203 if (enc->pix_fmt != PIX_FMT_NONE) {
1204 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1206 av_get_pix_fmt_name(enc->pix_fmt));
1209 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1211 enc->width, enc->height);
1212 if (enc->sample_aspect_ratio.num) {
1213 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1214 enc->width*enc->sample_aspect_ratio.num,
1215 enc->height*enc->sample_aspect_ratio.den,
1217 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1218 " [PAR %d:%d DAR %d:%d]",
1219 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1220 display_aspect_ratio.num, display_aspect_ratio.den);
1222 if(av_log_get_level() >= AV_LOG_DEBUG){
1223 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1224 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1226 enc->time_base.num/g, enc->time_base.den/g);
1230 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1231 ", q=%d-%d", enc->qmin, enc->qmax);
1234 case AVMEDIA_TYPE_AUDIO:
1235 snprintf(buf, buf_size,
1239 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1241 if (enc->sample_rate) {
1242 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1243 ", %d Hz", enc->sample_rate);
1245 av_strlcat(buf, ", ", buf_size);
1246 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1247 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1248 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1249 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1252 case AVMEDIA_TYPE_DATA:
1253 snprintf(buf, buf_size, "Data: %s", codec_name);
1255 case AVMEDIA_TYPE_SUBTITLE:
1256 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1258 case AVMEDIA_TYPE_ATTACHMENT:
1259 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1262 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1266 if (enc->flags & CODEC_FLAG_PASS1)
1267 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1269 if (enc->flags & CODEC_FLAG_PASS2)
1270 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1273 bitrate = get_bit_rate(enc);
1275 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1276 ", %d kb/s", bitrate / 1000);
1280 const char *av_get_profile_name(const AVCodec *codec, int profile)
1283 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1286 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1287 if (p->profile == profile)
1293 unsigned avcodec_version( void )
1295 return LIBAVCODEC_VERSION_INT;
1298 const char *avcodec_configuration(void)
1300 return LIBAV_CONFIGURATION;
1303 const char *avcodec_license(void)
1305 #define LICENSE_PREFIX "libavcodec license: "
1306 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1309 void avcodec_flush_buffers(AVCodecContext *avctx)
1311 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1312 ff_thread_flush(avctx);
1313 else if(avctx->codec->flush)
1314 avctx->codec->flush(avctx);
1317 static void video_free_buffers(AVCodecContext *s)
1319 AVCodecInternal *avci = s->internal;
1325 if (avci->buffer_count)
1326 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1327 avci->buffer_count);
1328 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1329 InternalBuffer *buf = &avci->buffer[i];
1331 av_freep(&buf->base[j]);
1335 av_freep(&avci->buffer);
1337 avci->buffer_count=0;
1340 static void audio_free_buffers(AVCodecContext *avctx)
1342 AVCodecInternal *avci = avctx->internal;
1343 InternalBuffer *buf;
1349 if (buf->extended_data) {
1350 av_free(buf->extended_data[0]);
1351 if (buf->extended_data != buf->data)
1352 av_free(buf->extended_data);
1354 av_freep(&avci->buffer);
1357 void avcodec_default_free_buffers(AVCodecContext *avctx)
1359 switch (avctx->codec_type) {
1360 case AVMEDIA_TYPE_VIDEO:
1361 video_free_buffers(avctx);
1363 case AVMEDIA_TYPE_AUDIO:
1364 audio_free_buffers(avctx);
1371 #if FF_API_OLD_FF_PICT_TYPES
1372 char av_get_pict_type_char(int pict_type){
1373 return av_get_picture_type_char(pict_type);
1377 int av_get_bits_per_sample(enum CodecID codec_id){
1379 case CODEC_ID_ADPCM_SBPRO_2:
1381 case CODEC_ID_ADPCM_SBPRO_3:
1383 case CODEC_ID_ADPCM_SBPRO_4:
1384 case CODEC_ID_ADPCM_CT:
1385 case CODEC_ID_ADPCM_IMA_WAV:
1386 case CODEC_ID_ADPCM_IMA_QT:
1387 case CODEC_ID_ADPCM_SWF:
1388 case CODEC_ID_ADPCM_MS:
1389 case CODEC_ID_ADPCM_YAMAHA:
1390 case CODEC_ID_ADPCM_G722:
1392 case CODEC_ID_PCM_ALAW:
1393 case CODEC_ID_PCM_MULAW:
1394 case CODEC_ID_PCM_S8:
1395 case CODEC_ID_PCM_U8:
1396 case CODEC_ID_PCM_ZORK:
1398 case CODEC_ID_PCM_S16BE:
1399 case CODEC_ID_PCM_S16LE:
1400 case CODEC_ID_PCM_S16LE_PLANAR:
1401 case CODEC_ID_PCM_U16BE:
1402 case CODEC_ID_PCM_U16LE:
1404 case CODEC_ID_PCM_S24DAUD:
1405 case CODEC_ID_PCM_S24BE:
1406 case CODEC_ID_PCM_S24LE:
1407 case CODEC_ID_PCM_U24BE:
1408 case CODEC_ID_PCM_U24LE:
1410 case CODEC_ID_PCM_S32BE:
1411 case CODEC_ID_PCM_S32LE:
1412 case CODEC_ID_PCM_U32BE:
1413 case CODEC_ID_PCM_U32LE:
1414 case CODEC_ID_PCM_F32BE:
1415 case CODEC_ID_PCM_F32LE:
1417 case CODEC_ID_PCM_F64BE:
1418 case CODEC_ID_PCM_F64LE:
1425 #if FF_API_OLD_SAMPLE_FMT
1426 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1427 return av_get_bytes_per_sample(sample_fmt) << 3;
1432 int ff_thread_init(AVCodecContext *s){
1437 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1451 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1453 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1457 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1459 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1460 "version to the newest one from Git. If the problem still "
1461 "occurs, it means that your file has a feature which has not "
1462 "been implemented.\n", feature);
1464 av_log_ask_for_sample(avc, NULL);
1467 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1469 va_list argument_list;
1471 va_start(argument_list, msg);
1474 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1475 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1476 "of this file to ftp://upload.libav.org/incoming/ "
1477 "and contact the libav-devel mailing list.\n");
1479 va_end(argument_list);
1482 static AVHWAccel *first_hwaccel = NULL;
1484 void av_register_hwaccel(AVHWAccel *hwaccel)
1486 AVHWAccel **p = &first_hwaccel;
1490 hwaccel->next = NULL;
1493 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1495 return hwaccel ? hwaccel->next : first_hwaccel;
1498 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1500 AVHWAccel *hwaccel=NULL;
1502 while((hwaccel= av_hwaccel_next(hwaccel))){
1503 if ( hwaccel->id == codec_id
1504 && hwaccel->pix_fmt == pix_fmt)
1510 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1512 if (ff_lockmgr_cb) {
1513 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1515 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1521 if (ff_lockmgr_cb) {
1522 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1524 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1530 int avpriv_lock_avformat(void)
1532 if (ff_lockmgr_cb) {
1533 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1539 int avpriv_unlock_avformat(void)
1541 if (ff_lockmgr_cb) {
1542 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1548 unsigned int avpriv_toupper4(unsigned int x)
1550 return toupper( x &0xFF)
1551 + (toupper((x>>8 )&0xFF)<<8 )
1552 + (toupper((x>>16)&0xFF)<<16)
1553 + (toupper((x>>24)&0xFF)<<24);
1558 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1561 return avctx->get_buffer(avctx, f);
1564 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1566 f->owner->release_buffer(f->owner, f);
1569 void ff_thread_finish_setup(AVCodecContext *avctx)
1573 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1577 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1583 #if FF_API_THREAD_INIT
1584 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1586 s->thread_count = thread_count;
1587 return ff_thread_init(s);
1591 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1593 if (codec_id <= CODEC_ID_NONE)
1594 return AVMEDIA_TYPE_UNKNOWN;
1595 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1596 return AVMEDIA_TYPE_VIDEO;
1597 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1598 return AVMEDIA_TYPE_AUDIO;
1599 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1600 return AVMEDIA_TYPE_SUBTITLE;
1602 return AVMEDIA_TYPE_UNKNOWN;