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"
48 static int volatile entangled_thread_counter=0;
49 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
50 static void *codec_mutex;
51 static void *avformat_mutex;
53 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
58 min_size= FFMAX(17*min_size/16 + 32, min_size);
60 ptr= av_realloc(ptr, min_size);
61 if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
69 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
74 min_size= FFMAX(17*min_size/16 + 32, min_size);
76 *p = av_malloc(min_size);
77 if (!*p) min_size = 0;
81 /* encoder management */
82 static AVCodec *first_avcodec = NULL;
84 AVCodec *av_codec_next(AVCodec *c){
86 else return first_avcodec;
89 #if !FF_API_AVCODEC_INIT
92 void avcodec_init(void)
94 static int initialized = 0;
100 dsputil_static_init();
103 void avcodec_register(AVCodec *codec)
108 while (*p != NULL) p = &(*p)->next;
112 if (codec->init_static_data)
113 codec->init_static_data(codec);
116 unsigned avcodec_get_edge_width(void)
121 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
122 s->coded_width = width;
123 s->coded_height= height;
124 s->width = -((-width )>>s->lowres);
125 s->height= -((-height)>>s->lowres);
128 #define INTERNAL_BUFFER_SIZE (32+1)
130 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
131 int linesize_align[AV_NUM_DATA_POINTERS])
138 case PIX_FMT_YUV420P:
139 case PIX_FMT_YUYV422:
140 case PIX_FMT_UYVY422:
141 case PIX_FMT_YUV422P:
142 case PIX_FMT_YUV440P:
143 case PIX_FMT_YUV444P:
146 case PIX_FMT_GRAY16BE:
147 case PIX_FMT_GRAY16LE:
148 case PIX_FMT_YUVJ420P:
149 case PIX_FMT_YUVJ422P:
150 case PIX_FMT_YUVJ440P:
151 case PIX_FMT_YUVJ444P:
152 case PIX_FMT_YUVA420P:
153 case PIX_FMT_YUV420P9LE:
154 case PIX_FMT_YUV420P9BE:
155 case PIX_FMT_YUV420P10LE:
156 case PIX_FMT_YUV420P10BE:
157 case PIX_FMT_YUV422P9LE:
158 case PIX_FMT_YUV422P9BE:
159 case PIX_FMT_YUV422P10LE:
160 case PIX_FMT_YUV422P10BE:
161 case PIX_FMT_YUV444P9LE:
162 case PIX_FMT_YUV444P9BE:
163 case PIX_FMT_YUV444P10LE:
164 case PIX_FMT_YUV444P10BE:
165 case PIX_FMT_GBRP9LE:
166 case PIX_FMT_GBRP9BE:
167 case PIX_FMT_GBRP10LE:
168 case PIX_FMT_GBRP10BE:
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)
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
215 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
216 linesize_align[i] = STRIDE_ALIGN;
217 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
218 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
219 //picture size unneccessarily in some cases. The solution here is not
220 //pretty and better ideas are welcome!
222 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
223 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
224 s->codec_id == CODEC_ID_VP6A) {
225 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
226 linesize_align[i] = 16;
231 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
232 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
233 int linesize_align[AV_NUM_DATA_POINTERS];
235 avcodec_align_dimensions2(s, width, height, linesize_align);
236 align = FFMAX(linesize_align[0], linesize_align[3]);
237 linesize_align[1] <<= chroma_shift;
238 linesize_align[2] <<= chroma_shift;
239 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
240 *width=FFALIGN(*width, align);
243 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
245 AVCodecInternal *avci = avctx->internal;
247 int buf_size, ret, i, needs_extended_data;
249 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
250 frame->nb_samples, avctx->sample_fmt,
253 return AVERROR(EINVAL);
255 needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
256 avctx->channels > AV_NUM_DATA_POINTERS;
258 /* allocate InternalBuffer if needed */
260 avci->buffer = av_mallocz(sizeof(InternalBuffer));
262 return AVERROR(ENOMEM);
266 /* if there is a previously-used internal buffer, check its size and
267 channel count to see if we can reuse it */
268 if (buf->extended_data) {
269 /* if current buffer is too small, free it */
270 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
271 av_free(buf->extended_data[0]);
272 if (buf->extended_data != buf->data)
273 av_free(&buf->extended_data);
274 buf->extended_data = NULL;
277 /* if number of channels has changed, reset and/or free extended data
278 pointers but leave data buffer in buf->data[0] for reuse */
279 if (buf->nb_channels != avctx->channels) {
280 if (buf->extended_data != buf->data)
281 av_free(buf->extended_data);
282 buf->extended_data = NULL;
286 /* if there is no previous buffer or the previous buffer cannot be used
287 as-is, allocate a new buffer and/or rearrange the channel pointers */
288 if (!buf->extended_data) {
289 /* if the channel pointers will fit, just set extended_data to data,
290 otherwise allocate the extended_data channel pointers */
291 if (needs_extended_data) {
292 buf->extended_data = av_mallocz(avctx->channels *
293 sizeof(*buf->extended_data));
294 if (!buf->extended_data)
295 return AVERROR(ENOMEM);
297 buf->extended_data = buf->data;
300 /* if there is a previous buffer and it is large enough, reuse it and
301 just fill-in new channel pointers and linesize, otherwise allocate
303 if (buf->extended_data[0]) {
304 ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
305 buf->extended_data[0], avctx->channels,
306 frame->nb_samples, avctx->sample_fmt,
309 ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
310 avctx->channels, frame->nb_samples,
311 avctx->sample_fmt, 32);
316 /* if data was not used for extended_data, we need to copy as many of
317 the extended_data channel pointers as will fit */
318 if (needs_extended_data) {
319 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
320 buf->data[i] = buf->extended_data[i];
322 buf->audio_data_size = buf_size;
323 buf->nb_channels = avctx->channels;
326 /* copy InternalBuffer info to the AVFrame */
327 frame->type = FF_BUFFER_TYPE_INTERNAL;
328 frame->extended_data = buf->extended_data;
329 frame->linesize[0] = buf->linesize[0];
330 memcpy(frame->data, buf->data, sizeof(frame->data));
332 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
333 else frame->pkt_pts = AV_NOPTS_VALUE;
334 frame->reordered_opaque = avctx->reordered_opaque;
336 if (avctx->debug & FF_DEBUG_BUFFERS)
337 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
338 "internal audio buffer used\n", frame);
343 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
349 AVCodecInternal *avci = s->internal;
351 if(pic->data[0]!=NULL) {
352 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
355 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
356 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
360 if(av_image_check_size(w, h, 0, s))
364 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
365 sizeof(InternalBuffer));
368 buf = &avci->buffer[avci->buffer_count];
370 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
371 if(s->active_thread_type&FF_THREAD_FRAME) {
372 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
376 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
377 av_freep(&buf->base[i]);
383 int h_chroma_shift, v_chroma_shift;
388 int stride_align[AV_NUM_DATA_POINTERS];
389 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
391 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
393 avcodec_align_dimensions2(s, &w, &h, stride_align);
395 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
401 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
402 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
403 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
404 // increase alignment of w for next try (rhs gives the lowest bit set in w)
409 unaligned |= picture.linesize[i] % stride_align[i];
413 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
417 for (i=0; i<3 && picture.data[i+1]; i++)
418 size[i] = picture.data[i+1] - picture.data[i];
419 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
421 memset(buf->base, 0, sizeof(buf->base));
422 memset(buf->data, 0, sizeof(buf->data));
424 for(i=0; i<4 && size[i]; i++){
425 const int h_shift= i==0 ? 0 : h_chroma_shift;
426 const int v_shift= i==0 ? 0 : v_chroma_shift;
428 buf->linesize[i]= picture.linesize[i];
430 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
431 if(buf->base[i]==NULL) return -1;
432 memset(buf->base[i], 128, size[i]);
434 // no edge if EDGE EMU or not planar YUV
435 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
436 buf->data[i] = buf->base[i];
438 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
440 for (; i < AV_NUM_DATA_POINTERS; i++) {
441 buf->base[i] = buf->data[i] = NULL;
442 buf->linesize[i] = 0;
444 if(size[1] && !size[2])
445 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
446 buf->width = s->width;
447 buf->height = s->height;
448 buf->pix_fmt= s->pix_fmt;
450 pic->type= FF_BUFFER_TYPE_INTERNAL;
452 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
453 pic->base[i]= buf->base[i];
454 pic->data[i]= buf->data[i];
455 pic->linesize[i]= buf->linesize[i];
457 pic->extended_data = pic->data;
458 avci->buffer_count++;
460 if(s->pkt) pic->pkt_pts= s->pkt->pts;
461 else pic->pkt_pts= AV_NOPTS_VALUE;
462 pic->reordered_opaque= s->reordered_opaque;
464 if(s->debug&FF_DEBUG_BUFFERS)
465 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
466 "buffers used\n", pic, avci->buffer_count);
471 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
473 switch (avctx->codec_type) {
474 case AVMEDIA_TYPE_VIDEO:
475 return video_get_buffer(avctx, frame);
476 case AVMEDIA_TYPE_AUDIO:
477 return audio_get_buffer(avctx, frame);
483 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
485 InternalBuffer *buf, *last;
486 AVCodecInternal *avci = s->internal;
488 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
490 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
491 assert(avci->buffer_count);
494 buf = NULL; /* avoids warning */
495 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
496 buf = &avci->buffer[i];
497 if (buf->data[0] == pic->data[0])
500 assert(i < avci->buffer_count);
501 avci->buffer_count--;
502 last = &avci->buffer[avci->buffer_count];
505 FFSWAP(InternalBuffer, *buf, *last);
508 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
510 // pic->base[i]=NULL;
512 //printf("R%X\n", pic->opaque);
514 if(s->debug&FF_DEBUG_BUFFERS)
515 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
516 "buffers used\n", pic, avci->buffer_count);
519 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
523 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
525 /* If no picture return a new buffer */
526 if(pic->data[0] == NULL) {
527 /* We will copy from buffer, so must be readable */
528 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
529 return s->get_buffer(s, pic);
532 /* If internal buffer type return the same buffer */
533 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
534 if(s->pkt) pic->pkt_pts= s->pkt->pts;
535 else pic->pkt_pts= AV_NOPTS_VALUE;
536 pic->reordered_opaque= s->reordered_opaque;
541 * Not internal type and reget_buffer not overridden, emulate cr buffer
544 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
545 pic->data[i] = pic->base[i] = NULL;
547 /* Allocate new frame */
548 if (s->get_buffer(s, pic))
550 /* Copy image data from old buffer to new buffer */
551 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
553 s->release_buffer(s, &temp_pic); // Release old frame
557 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
560 for(i=0; i<count; i++){
561 int r= func(c, (char*)arg + i*size);
567 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
570 for(i=0; i<count; i++){
571 int r= func(c, arg, i, 0);
577 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
578 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
583 void avcodec_get_frame_defaults(AVFrame *pic){
584 memset(pic, 0, sizeof(AVFrame));
586 pic->pts= AV_NOPTS_VALUE;
590 AVFrame *avcodec_alloc_frame(void){
591 AVFrame *pic= av_malloc(sizeof(AVFrame));
593 if(pic==NULL) return NULL;
595 avcodec_get_frame_defaults(pic);
600 #if FF_API_AVCODEC_OPEN
601 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
603 return avcodec_open2(avctx, codec, NULL);
607 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
610 AVDictionary *tmp = NULL;
613 av_dict_copy(&tmp, *options, 0);
615 /* If there is a user-supplied mutex locking routine, call it. */
617 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
621 entangled_thread_counter++;
622 if(entangled_thread_counter != 1){
623 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
628 if(avctx->codec || !codec) {
629 ret = AVERROR(EINVAL);
633 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
634 if (!avctx->internal) {
635 ret = AVERROR(ENOMEM);
639 if (codec->priv_data_size > 0) {
640 if(!avctx->priv_data){
641 avctx->priv_data = av_mallocz(codec->priv_data_size);
642 if (!avctx->priv_data) {
643 ret = AVERROR(ENOMEM);
646 if (codec->priv_class) {
647 *(AVClass**)avctx->priv_data= codec->priv_class;
648 av_opt_set_defaults(avctx->priv_data);
651 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
654 avctx->priv_data = NULL;
656 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
659 if(avctx->coded_width && avctx->coded_height)
660 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
661 else if(avctx->width && avctx->height)
662 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
664 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
665 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
666 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
667 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
668 avcodec_set_dimensions(avctx, 0, 0);
671 /* if the decoder init function was already called previously,
672 free the already allocated subtitle_header before overwriting it */
674 av_freep(&avctx->subtitle_header);
676 #define SANE_NB_CHANNELS 128U
677 if (avctx->channels > SANE_NB_CHANNELS) {
678 ret = AVERROR(EINVAL);
682 avctx->codec = codec;
683 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
684 avctx->codec_id == CODEC_ID_NONE) {
685 avctx->codec_type = codec->type;
686 avctx->codec_id = codec->id;
688 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
689 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
690 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
691 ret = AVERROR(EINVAL);
694 avctx->frame_number = 0;
697 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
698 avctx->error_recognition, avctx->err_recognition);
699 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
700 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
701 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
702 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
703 avctx->error_recognition, avctx->err_recognition);
706 if (HAVE_THREADS && !avctx->thread_opaque) {
707 ret = ff_thread_init(avctx);
713 if (avctx->codec->max_lowres < avctx->lowres) {
714 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
715 avctx->codec->max_lowres);
716 ret = AVERROR(EINVAL);
719 if (avctx->codec->encode) {
721 if (avctx->codec->sample_fmts) {
722 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
723 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
725 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
726 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
727 ret = AVERROR(EINVAL);
731 if (avctx->codec->supported_samplerates) {
732 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
733 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
735 if (avctx->codec->supported_samplerates[i] == 0) {
736 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
737 ret = AVERROR(EINVAL);
741 if (avctx->codec->channel_layouts) {
742 if (!avctx->channel_layout) {
743 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
745 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
746 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
748 if (avctx->codec->channel_layouts[i] == 0) {
749 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
750 ret = AVERROR(EINVAL);
755 if (avctx->channel_layout && avctx->channels) {
756 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
757 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
758 ret = AVERROR(EINVAL);
761 } else if (avctx->channel_layout) {
762 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
766 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
767 ret = avctx->codec->init(avctx);
773 entangled_thread_counter--;
775 /* Release any user-supplied mutex. */
777 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
780 av_dict_free(options);
787 av_freep(&avctx->priv_data);
788 av_freep(&avctx->internal);
793 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
794 const short *samples)
796 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
797 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
800 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
801 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
802 avctx->frame_number++;
808 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
811 if(buf_size < FF_MIN_BUFFER_SIZE){
812 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
815 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
817 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
818 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
819 avctx->frame_number++;
820 emms_c(); //needed to avoid an emms_c() call before every return;
827 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
828 const AVSubtitle *sub)
831 if(sub->start_display_time) {
832 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
835 if(sub->num_rects == 0 || !sub->rects)
837 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
838 avctx->frame_number++;
842 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
843 int *got_picture_ptr,
849 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
854 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
855 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
856 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
859 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
861 picture->pkt_dts= avpkt->dts;
864 emms_c(); //needed to avoid an emms_c() call before every return;
866 if (*got_picture_ptr)
867 avctx->frame_number++;
874 #if FF_API_OLD_DECODE_AUDIO
875 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
880 int ret, got_frame = 0;
882 if (avctx->get_buffer != avcodec_default_get_buffer) {
883 av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
884 "avcodec_decode_audio3()\n");
885 return AVERROR(EINVAL);
888 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
890 if (ret >= 0 && got_frame) {
892 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
893 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
895 avctx->sample_fmt, 1);
896 if (*frame_size_ptr < data_size) {
897 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
898 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
899 return AVERROR(EINVAL);
902 memcpy(samples, frame.extended_data[0], plane_size);
904 if (planar && avctx->channels > 1) {
905 uint8_t *out = ((uint8_t *)samples) + plane_size;
906 for (ch = 1; ch < avctx->channels; ch++) {
907 memcpy(out, frame.extended_data[ch], plane_size);
911 *frame_size_ptr = data_size;
919 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
930 if (!avpkt->data && avpkt->size) {
931 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
932 return AVERROR(EINVAL);
935 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
936 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
937 if (ret >= 0 && *got_frame_ptr) {
938 avctx->frame_number++;
939 frame->pkt_dts = avpkt->dts;
945 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
953 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
955 avctx->frame_number++;
959 void avsubtitle_free(AVSubtitle *sub)
963 for (i = 0; i < sub->num_rects; i++)
965 av_freep(&sub->rects[i]->pict.data[0]);
966 av_freep(&sub->rects[i]->pict.data[1]);
967 av_freep(&sub->rects[i]->pict.data[2]);
968 av_freep(&sub->rects[i]->pict.data[3]);
969 av_freep(&sub->rects[i]->text);
970 av_freep(&sub->rects[i]->ass);
971 av_freep(&sub->rects[i]);
974 av_freep(&sub->rects);
976 memset(sub, 0, sizeof(AVSubtitle));
979 av_cold int avcodec_close(AVCodecContext *avctx)
981 /* If there is a user-supplied mutex locking routine, call it. */
983 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
987 entangled_thread_counter++;
988 if(entangled_thread_counter != 1){
989 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
990 entangled_thread_counter--;
994 if (HAVE_THREADS && avctx->thread_opaque)
995 ff_thread_free(avctx);
996 if (avctx->codec && avctx->codec->close)
997 avctx->codec->close(avctx);
998 avcodec_default_free_buffers(avctx);
999 avctx->coded_frame = NULL;
1000 av_freep(&avctx->internal);
1001 if (avctx->codec && avctx->codec->priv_class)
1002 av_opt_free(avctx->priv_data);
1004 av_freep(&avctx->priv_data);
1005 if(avctx->codec && avctx->codec->encode)
1006 av_freep(&avctx->extradata);
1007 avctx->codec = NULL;
1008 avctx->active_thread_type = 0;
1009 entangled_thread_counter--;
1011 /* Release any user-supplied mutex. */
1012 if (ff_lockmgr_cb) {
1013 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1018 AVCodec *avcodec_find_encoder(enum CodecID id)
1020 AVCodec *p, *experimental=NULL;
1023 if (p->encode != NULL && p->id == id) {
1024 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1031 return experimental;
1034 AVCodec *avcodec_find_encoder_by_name(const char *name)
1041 if (p->encode != NULL && strcmp(name,p->name) == 0)
1048 AVCodec *avcodec_find_decoder(enum CodecID id)
1053 if (p->decode != NULL && p->id == id)
1060 AVCodec *avcodec_find_decoder_by_name(const char *name)
1067 if (p->decode != NULL && strcmp(name,p->name) == 0)
1074 static int get_bit_rate(AVCodecContext *ctx)
1077 int bits_per_sample;
1079 switch(ctx->codec_type) {
1080 case AVMEDIA_TYPE_VIDEO:
1081 case AVMEDIA_TYPE_DATA:
1082 case AVMEDIA_TYPE_SUBTITLE:
1083 case AVMEDIA_TYPE_ATTACHMENT:
1084 bit_rate = ctx->bit_rate;
1086 case AVMEDIA_TYPE_AUDIO:
1087 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1088 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1097 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1099 int i, len, ret = 0;
1101 for (i = 0; i < 4; i++) {
1102 len = snprintf(buf, buf_size,
1103 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1105 buf_size = buf_size > len ? buf_size - len : 0;
1112 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1114 const char *codec_name;
1115 const char *profile = NULL;
1119 AVRational display_aspect_ratio;
1122 p = avcodec_find_encoder(enc->codec_id);
1124 p = avcodec_find_decoder(enc->codec_id);
1127 codec_name = p->name;
1128 profile = av_get_profile_name(p, enc->profile);
1129 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1130 /* fake mpeg2 transport stream codec (currently not
1132 codec_name = "mpeg2ts";
1133 } else if (enc->codec_name[0] != '\0') {
1134 codec_name = enc->codec_name;
1136 /* output avi tags */
1138 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1139 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1143 switch(enc->codec_type) {
1144 case AVMEDIA_TYPE_VIDEO:
1145 snprintf(buf, buf_size,
1147 codec_name, enc->mb_decision ? " (hq)" : "");
1149 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1151 if (enc->pix_fmt != PIX_FMT_NONE) {
1152 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1154 av_get_pix_fmt_name(enc->pix_fmt));
1157 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1159 enc->width, enc->height);
1160 if (enc->sample_aspect_ratio.num) {
1161 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1162 enc->width*enc->sample_aspect_ratio.num,
1163 enc->height*enc->sample_aspect_ratio.den,
1165 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1166 " [PAR %d:%d DAR %d:%d]",
1167 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1168 display_aspect_ratio.num, display_aspect_ratio.den);
1170 if(av_log_get_level() >= AV_LOG_DEBUG){
1171 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1172 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1174 enc->time_base.num/g, enc->time_base.den/g);
1178 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1179 ", q=%d-%d", enc->qmin, enc->qmax);
1182 case AVMEDIA_TYPE_AUDIO:
1183 snprintf(buf, buf_size,
1187 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1189 if (enc->sample_rate) {
1190 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1191 ", %d Hz", enc->sample_rate);
1193 av_strlcat(buf, ", ", buf_size);
1194 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1195 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1196 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1197 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1200 case AVMEDIA_TYPE_DATA:
1201 snprintf(buf, buf_size, "Data: %s", codec_name);
1203 case AVMEDIA_TYPE_SUBTITLE:
1204 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1206 case AVMEDIA_TYPE_ATTACHMENT:
1207 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1210 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1214 if (enc->flags & CODEC_FLAG_PASS1)
1215 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1217 if (enc->flags & CODEC_FLAG_PASS2)
1218 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1221 bitrate = get_bit_rate(enc);
1223 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1224 ", %d kb/s", bitrate / 1000);
1228 const char *av_get_profile_name(const AVCodec *codec, int profile)
1231 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1234 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1235 if (p->profile == profile)
1241 unsigned avcodec_version( void )
1243 return LIBAVCODEC_VERSION_INT;
1246 const char *avcodec_configuration(void)
1248 return LIBAV_CONFIGURATION;
1251 const char *avcodec_license(void)
1253 #define LICENSE_PREFIX "libavcodec license: "
1254 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1257 void avcodec_flush_buffers(AVCodecContext *avctx)
1259 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1260 ff_thread_flush(avctx);
1261 else if(avctx->codec->flush)
1262 avctx->codec->flush(avctx);
1265 static void video_free_buffers(AVCodecContext *s)
1267 AVCodecInternal *avci = s->internal;
1273 if (avci->buffer_count)
1274 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1275 avci->buffer_count);
1276 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1277 InternalBuffer *buf = &avci->buffer[i];
1279 av_freep(&buf->base[j]);
1283 av_freep(&avci->buffer);
1285 avci->buffer_count=0;
1288 static void audio_free_buffers(AVCodecContext *avctx)
1290 AVCodecInternal *avci = avctx->internal;
1291 InternalBuffer *buf;
1297 if (buf->extended_data) {
1298 av_free(buf->extended_data[0]);
1299 if (buf->extended_data != buf->data)
1300 av_free(buf->extended_data);
1302 av_freep(&avci->buffer);
1305 void avcodec_default_free_buffers(AVCodecContext *avctx)
1307 switch (avctx->codec_type) {
1308 case AVMEDIA_TYPE_VIDEO:
1309 video_free_buffers(avctx);
1311 case AVMEDIA_TYPE_AUDIO:
1312 audio_free_buffers(avctx);
1319 #if FF_API_OLD_FF_PICT_TYPES
1320 char av_get_pict_type_char(int pict_type){
1321 return av_get_picture_type_char(pict_type);
1325 int av_get_bits_per_sample(enum CodecID codec_id){
1327 case CODEC_ID_ADPCM_SBPRO_2:
1329 case CODEC_ID_ADPCM_SBPRO_3:
1331 case CODEC_ID_ADPCM_SBPRO_4:
1332 case CODEC_ID_ADPCM_CT:
1333 case CODEC_ID_ADPCM_IMA_WAV:
1334 case CODEC_ID_ADPCM_IMA_QT:
1335 case CODEC_ID_ADPCM_SWF:
1336 case CODEC_ID_ADPCM_MS:
1337 case CODEC_ID_ADPCM_YAMAHA:
1338 case CODEC_ID_ADPCM_G722:
1340 case CODEC_ID_PCM_ALAW:
1341 case CODEC_ID_PCM_MULAW:
1342 case CODEC_ID_PCM_S8:
1343 case CODEC_ID_PCM_U8:
1344 case CODEC_ID_PCM_ZORK:
1346 case CODEC_ID_PCM_S16BE:
1347 case CODEC_ID_PCM_S16LE:
1348 case CODEC_ID_PCM_S16LE_PLANAR:
1349 case CODEC_ID_PCM_U16BE:
1350 case CODEC_ID_PCM_U16LE:
1352 case CODEC_ID_PCM_S24DAUD:
1353 case CODEC_ID_PCM_S24BE:
1354 case CODEC_ID_PCM_S24LE:
1355 case CODEC_ID_PCM_U24BE:
1356 case CODEC_ID_PCM_U24LE:
1358 case CODEC_ID_PCM_S32BE:
1359 case CODEC_ID_PCM_S32LE:
1360 case CODEC_ID_PCM_U32BE:
1361 case CODEC_ID_PCM_U32LE:
1362 case CODEC_ID_PCM_F32BE:
1363 case CODEC_ID_PCM_F32LE:
1365 case CODEC_ID_PCM_F64BE:
1366 case CODEC_ID_PCM_F64LE:
1373 #if FF_API_OLD_SAMPLE_FMT
1374 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1375 return av_get_bytes_per_sample(sample_fmt) << 3;
1380 int ff_thread_init(AVCodecContext *s){
1385 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1399 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1401 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1405 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1407 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1408 "version to the newest one from Git. If the problem still "
1409 "occurs, it means that your file has a feature which has not "
1410 "been implemented.\n", feature);
1412 av_log_ask_for_sample(avc, NULL);
1415 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1417 va_list argument_list;
1419 va_start(argument_list, msg);
1422 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1423 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1424 "of this file to ftp://upload.libav.org/incoming/ "
1425 "and contact the libav-devel mailing list.\n");
1427 va_end(argument_list);
1430 static AVHWAccel *first_hwaccel = NULL;
1432 void av_register_hwaccel(AVHWAccel *hwaccel)
1434 AVHWAccel **p = &first_hwaccel;
1438 hwaccel->next = NULL;
1441 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1443 return hwaccel ? hwaccel->next : first_hwaccel;
1446 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1448 AVHWAccel *hwaccel=NULL;
1450 while((hwaccel= av_hwaccel_next(hwaccel))){
1451 if ( hwaccel->id == codec_id
1452 && hwaccel->pix_fmt == pix_fmt)
1458 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1460 if (ff_lockmgr_cb) {
1461 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1463 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1469 if (ff_lockmgr_cb) {
1470 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1472 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1478 int avpriv_lock_avformat(void)
1480 if (ff_lockmgr_cb) {
1481 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1487 int avpriv_unlock_avformat(void)
1489 if (ff_lockmgr_cb) {
1490 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1496 unsigned int avpriv_toupper4(unsigned int x)
1498 return toupper( x &0xFF)
1499 + (toupper((x>>8 )&0xFF)<<8 )
1500 + (toupper((x>>16)&0xFF)<<16)
1501 + (toupper((x>>24)&0xFF)<<24);
1506 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1509 return avctx->get_buffer(avctx, f);
1512 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1514 f->owner->release_buffer(f->owner, f);
1517 void ff_thread_finish_setup(AVCodecContext *avctx)
1521 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1525 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1531 #if FF_API_THREAD_INIT
1532 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1534 s->thread_count = thread_count;
1535 return ff_thread_init(s);
1539 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1541 if (codec_id <= CODEC_ID_NONE)
1542 return AVMEDIA_TYPE_UNKNOWN;
1543 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1544 return AVMEDIA_TYPE_VIDEO;
1545 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1546 return AVMEDIA_TYPE_AUDIO;
1547 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1548 return AVMEDIA_TYPE_SUBTITLE;
1550 return AVMEDIA_TYPE_UNKNOWN;