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/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/audioconvert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
39 #include "libavutil/opt.h"
40 #include "imgconvert.h"
42 #include "audioconvert.h"
44 #include "bytestream.h"
50 static int volatile entangled_thread_counter=0;
51 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
52 static void *codec_mutex;
53 static void *avformat_mutex;
55 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
60 min_size= FFMAX(17*min_size/16 + 32, min_size);
62 ptr= av_realloc(ptr, min_size);
63 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
71 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
76 min_size= FFMAX(17*min_size/16 + 32, min_size);
78 *p = av_malloc(min_size);
79 if (!*p) min_size = 0;
83 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
86 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
91 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
93 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
96 /* encoder management */
97 static AVCodec *first_avcodec = NULL;
99 AVCodec *av_codec_next(AVCodec *c){
100 if(c) return c->next;
101 else return first_avcodec;
104 static void avcodec_init(void)
106 static int initialized = 0;
108 if (initialized != 0)
112 ff_dsputil_static_init();
115 static av_always_inline int codec_is_encoder(AVCodec *codec)
117 return codec && (codec->encode || codec->encode2);
120 static av_always_inline int codec_is_decoder(AVCodec *codec)
122 return codec && codec->decode;
125 void avcodec_register(AVCodec *codec)
130 while (*p != NULL) p = &(*p)->next;
134 if (codec->init_static_data)
135 codec->init_static_data(codec);
138 unsigned avcodec_get_edge_width(void)
143 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
144 s->coded_width = width;
145 s->coded_height= height;
146 s->width = -((-width )>>s->lowres);
147 s->height= -((-height)>>s->lowres);
150 #define INTERNAL_BUFFER_SIZE (32+1)
152 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
153 int linesize_align[AV_NUM_DATA_POINTERS])
160 case PIX_FMT_YUV420P:
161 case PIX_FMT_YUYV422:
162 case PIX_FMT_UYVY422:
163 case PIX_FMT_YUV422P:
164 case PIX_FMT_YUV440P:
165 case PIX_FMT_YUV444P:
168 case PIX_FMT_GRAY16BE:
169 case PIX_FMT_GRAY16LE:
170 case PIX_FMT_YUVJ420P:
171 case PIX_FMT_YUVJ422P:
172 case PIX_FMT_YUVJ440P:
173 case PIX_FMT_YUVJ444P:
174 case PIX_FMT_YUVA420P:
175 case PIX_FMT_YUV420P9LE:
176 case PIX_FMT_YUV420P9BE:
177 case PIX_FMT_YUV420P10LE:
178 case PIX_FMT_YUV420P10BE:
179 case PIX_FMT_YUV422P9LE:
180 case PIX_FMT_YUV422P9BE:
181 case PIX_FMT_YUV422P10LE:
182 case PIX_FMT_YUV422P10BE:
183 case PIX_FMT_YUV444P9LE:
184 case PIX_FMT_YUV444P9BE:
185 case PIX_FMT_YUV444P10LE:
186 case PIX_FMT_YUV444P10BE:
187 case PIX_FMT_GBRP9LE:
188 case PIX_FMT_GBRP9BE:
189 case PIX_FMT_GBRP10LE:
190 case PIX_FMT_GBRP10BE:
191 w_align = 16; //FIXME assume 16 pixel per macroblock
192 h_align = 16 * 2; // interlaced needs 2 macroblocks height
194 case PIX_FMT_YUV411P:
195 case PIX_FMT_UYYVYY411:
199 case PIX_FMT_YUV410P:
200 if(s->codec_id == CODEC_ID_SVQ1){
205 if(s->codec_id == CODEC_ID_RPZA){
212 if(s->codec_id == CODEC_ID_SMC){
218 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
229 *width = FFALIGN(*width , w_align);
230 *height= FFALIGN(*height, h_align);
231 if(s->codec_id == CODEC_ID_H264 || s->lowres)
232 *height+=2; // some of the optimized chroma MC reads one line too much
233 // which is also done in mpeg decoders with lowres > 0
235 for (i = 0; i < 4; i++)
236 linesize_align[i] = STRIDE_ALIGN;
239 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
240 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
241 int linesize_align[AV_NUM_DATA_POINTERS];
243 avcodec_align_dimensions2(s, width, height, linesize_align);
244 align = FFMAX(linesize_align[0], linesize_align[3]);
245 linesize_align[1] <<= chroma_shift;
246 linesize_align[2] <<= chroma_shift;
247 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
248 *width=FFALIGN(*width, align);
251 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
252 enum AVSampleFormat sample_fmt, const uint8_t *buf,
253 int buf_size, int align)
255 int ch, planar, needed_size, ret = 0;
257 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
258 frame->nb_samples, sample_fmt,
260 if (buf_size < needed_size)
261 return AVERROR(EINVAL);
263 planar = av_sample_fmt_is_planar(sample_fmt);
264 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
265 if (!(frame->extended_data = av_mallocz(nb_channels *
266 sizeof(*frame->extended_data))))
267 return AVERROR(ENOMEM);
269 frame->extended_data = frame->data;
272 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
273 buf, nb_channels, frame->nb_samples,
274 sample_fmt, align)) < 0) {
275 if (frame->extended_data != frame->data)
276 av_free(frame->extended_data);
279 if (frame->extended_data != frame->data) {
280 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
281 frame->data[ch] = frame->extended_data[ch];
287 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
289 AVCodecInternal *avci = avctx->internal;
293 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
294 frame->nb_samples, avctx->sample_fmt,
297 return AVERROR(EINVAL);
299 /* allocate InternalBuffer if needed */
301 avci->buffer = av_mallocz(sizeof(InternalBuffer));
303 return AVERROR(ENOMEM);
307 /* if there is a previously-used internal buffer, check its size and
308 channel count to see if we can reuse it */
309 if (buf->extended_data) {
310 /* if current buffer is too small, free it */
311 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
312 av_free(buf->extended_data[0]);
313 if (buf->extended_data != buf->data)
314 av_free(&buf->extended_data);
315 buf->extended_data = NULL;
318 /* if number of channels has changed, reset and/or free extended data
319 pointers but leave data buffer in buf->data[0] for reuse */
320 if (buf->nb_channels != avctx->channels) {
321 if (buf->extended_data != buf->data)
322 av_free(buf->extended_data);
323 buf->extended_data = NULL;
327 /* if there is no previous buffer or the previous buffer cannot be used
328 as-is, allocate a new buffer and/or rearrange the channel pointers */
329 if (!buf->extended_data) {
331 if (!(buf->data[0] = av_mallocz(buf_size)))
332 return AVERROR(ENOMEM);
333 buf->audio_data_size = buf_size;
335 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
336 avctx->sample_fmt, buf->data[0],
337 buf->audio_data_size, 32)))
340 if (frame->extended_data == frame->data)
341 buf->extended_data = buf->data;
343 buf->extended_data = frame->extended_data;
344 memcpy(buf->data, frame->data, sizeof(frame->data));
345 buf->linesize[0] = frame->linesize[0];
346 buf->nb_channels = avctx->channels;
348 /* copy InternalBuffer info to the AVFrame */
349 frame->extended_data = buf->extended_data;
350 frame->linesize[0] = buf->linesize[0];
351 memcpy(frame->data, buf->data, sizeof(frame->data));
354 frame->type = FF_BUFFER_TYPE_INTERNAL;
356 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
357 else frame->pkt_pts = AV_NOPTS_VALUE;
358 frame->reordered_opaque = avctx->reordered_opaque;
360 if (avctx->debug & FF_DEBUG_BUFFERS)
361 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
362 "internal audio buffer used\n", frame);
367 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
373 AVCodecInternal *avci = s->internal;
375 if(pic->data[0]!=NULL) {
376 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
379 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
380 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
384 if(av_image_check_size(w, h, 0, s))
388 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
389 sizeof(InternalBuffer));
392 buf = &avci->buffer[avci->buffer_count];
394 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
395 if(s->active_thread_type&FF_THREAD_FRAME) {
396 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
400 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
401 av_freep(&buf->base[i]);
407 int h_chroma_shift, v_chroma_shift;
412 int stride_align[AV_NUM_DATA_POINTERS];
413 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
415 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
417 avcodec_align_dimensions2(s, &w, &h, stride_align);
419 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
425 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
426 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
427 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
428 // increase alignment of w for next try (rhs gives the lowest bit set in w)
433 unaligned |= picture.linesize[i] % stride_align[i];
437 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
441 for (i=0; i<3 && picture.data[i+1]; i++)
442 size[i] = picture.data[i+1] - picture.data[i];
443 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
445 memset(buf->base, 0, sizeof(buf->base));
446 memset(buf->data, 0, sizeof(buf->data));
448 for(i=0; i<4 && size[i]; i++){
449 const int h_shift= i==0 ? 0 : h_chroma_shift;
450 const int v_shift= i==0 ? 0 : v_chroma_shift;
452 buf->linesize[i]= picture.linesize[i];
454 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
455 if(buf->base[i]==NULL) return -1;
456 memset(buf->base[i], 128, size[i]);
458 // no edge if EDGE EMU or not planar YUV
459 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
460 buf->data[i] = buf->base[i];
462 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
464 for (; i < AV_NUM_DATA_POINTERS; i++) {
465 buf->base[i] = buf->data[i] = NULL;
466 buf->linesize[i] = 0;
468 if(size[1] && !size[2])
469 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
470 buf->width = s->width;
471 buf->height = s->height;
472 buf->pix_fmt= s->pix_fmt;
474 pic->type= FF_BUFFER_TYPE_INTERNAL;
476 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
477 pic->base[i]= buf->base[i];
478 pic->data[i]= buf->data[i];
479 pic->linesize[i]= buf->linesize[i];
481 pic->extended_data = pic->data;
482 avci->buffer_count++;
484 if(s->pkt) pic->pkt_pts= s->pkt->pts;
485 else pic->pkt_pts= AV_NOPTS_VALUE;
486 pic->reordered_opaque= s->reordered_opaque;
488 if(s->debug&FF_DEBUG_BUFFERS)
489 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
490 "buffers used\n", pic, avci->buffer_count);
495 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
497 switch (avctx->codec_type) {
498 case AVMEDIA_TYPE_VIDEO:
499 return video_get_buffer(avctx, frame);
500 case AVMEDIA_TYPE_AUDIO:
501 return audio_get_buffer(avctx, frame);
507 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
509 InternalBuffer *buf, *last;
510 AVCodecInternal *avci = s->internal;
512 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
514 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
515 assert(avci->buffer_count);
518 buf = NULL; /* avoids warning */
519 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
520 buf = &avci->buffer[i];
521 if (buf->data[0] == pic->data[0])
524 assert(i < avci->buffer_count);
525 avci->buffer_count--;
526 last = &avci->buffer[avci->buffer_count];
529 FFSWAP(InternalBuffer, *buf, *last);
532 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
534 // pic->base[i]=NULL;
536 //printf("R%X\n", pic->opaque);
538 if(s->debug&FF_DEBUG_BUFFERS)
539 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
540 "buffers used\n", pic, avci->buffer_count);
543 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
547 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
549 /* If no picture return a new buffer */
550 if(pic->data[0] == NULL) {
551 /* We will copy from buffer, so must be readable */
552 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
553 return s->get_buffer(s, pic);
556 /* If internal buffer type return the same buffer */
557 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
558 if(s->pkt) pic->pkt_pts= s->pkt->pts;
559 else pic->pkt_pts= AV_NOPTS_VALUE;
560 pic->reordered_opaque= s->reordered_opaque;
565 * Not internal type and reget_buffer not overridden, emulate cr buffer
568 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
569 pic->data[i] = pic->base[i] = NULL;
571 /* Allocate new frame */
572 if (s->get_buffer(s, pic))
574 /* Copy image data from old buffer to new buffer */
575 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
577 s->release_buffer(s, &temp_pic); // Release old frame
581 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
584 for(i=0; i<count; i++){
585 int r= func(c, (char*)arg + i*size);
591 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
594 for(i=0; i<count; i++){
595 int r= func(c, arg, i, 0);
601 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
602 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
607 void avcodec_get_frame_defaults(AVFrame *pic){
608 memset(pic, 0, sizeof(AVFrame));
610 pic->pts= AV_NOPTS_VALUE;
612 pic->sample_aspect_ratio = (AVRational){0, 1};
613 pic->format = -1; /* unknown */
616 AVFrame *avcodec_alloc_frame(void){
617 AVFrame *pic= av_malloc(sizeof(AVFrame));
619 if(pic==NULL) return NULL;
621 avcodec_get_frame_defaults(pic);
626 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
629 AVDictionary *tmp = NULL;
631 if (avcodec_is_open(avctx))
634 if ((!codec && !avctx->codec)) {
635 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
636 return AVERROR(EINVAL);
638 if ((codec && avctx->codec && codec != avctx->codec)) {
639 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
640 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
641 return AVERROR(EINVAL);
644 codec = avctx->codec;
646 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
647 return AVERROR(EINVAL);
650 av_dict_copy(&tmp, *options, 0);
652 /* If there is a user-supplied mutex locking routine, call it. */
654 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
658 entangled_thread_counter++;
659 if(entangled_thread_counter != 1){
660 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
665 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
666 if (!avctx->internal) {
667 ret = AVERROR(ENOMEM);
671 if (codec->priv_data_size > 0) {
672 if(!avctx->priv_data){
673 avctx->priv_data = av_mallocz(codec->priv_data_size);
674 if (!avctx->priv_data) {
675 ret = AVERROR(ENOMEM);
678 if (codec->priv_class) {
679 *(AVClass**)avctx->priv_data= codec->priv_class;
680 av_opt_set_defaults(avctx->priv_data);
683 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
686 avctx->priv_data = NULL;
688 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
691 if(avctx->coded_width && avctx->coded_height)
692 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
693 else if(avctx->width && avctx->height)
694 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
696 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
697 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
698 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
699 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
700 avcodec_set_dimensions(avctx, 0, 0);
703 /* if the decoder init function was already called previously,
704 free the already allocated subtitle_header before overwriting it */
705 if (codec_is_decoder(codec))
706 av_freep(&avctx->subtitle_header);
708 #define SANE_NB_CHANNELS 128U
709 if (avctx->channels > SANE_NB_CHANNELS) {
710 ret = AVERROR(EINVAL);
714 avctx->codec = codec;
715 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
716 avctx->codec_id == CODEC_ID_NONE) {
717 avctx->codec_type = codec->type;
718 avctx->codec_id = codec->id;
720 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
721 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
722 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
723 ret = AVERROR(EINVAL);
726 avctx->frame_number = 0;
728 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
729 (!avctx->time_base.num || !avctx->time_base.den)) {
730 avctx->time_base.num = 1;
731 avctx->time_base.den = avctx->sample_rate;
734 if (HAVE_THREADS && !avctx->thread_opaque) {
735 ret = ff_thread_init(avctx);
740 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
741 avctx->thread_count = 1;
743 if (avctx->codec->max_lowres < avctx->lowres) {
744 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
745 avctx->codec->max_lowres);
746 ret = AVERROR(EINVAL);
749 if (codec_is_encoder(avctx->codec)) {
751 if (avctx->codec->sample_fmts) {
752 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
753 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
755 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
756 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
757 ret = AVERROR(EINVAL);
761 if (avctx->codec->pix_fmts) {
762 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
763 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
765 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
766 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
767 ret = AVERROR(EINVAL);
771 if (avctx->codec->supported_samplerates) {
772 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
773 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
775 if (avctx->codec->supported_samplerates[i] == 0) {
776 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
777 ret = AVERROR(EINVAL);
781 if (avctx->codec->channel_layouts) {
782 if (!avctx->channel_layout) {
783 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
785 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
786 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
788 if (avctx->codec->channel_layouts[i] == 0) {
789 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
790 ret = AVERROR(EINVAL);
795 if (avctx->channel_layout && avctx->channels) {
796 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
797 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
798 ret = AVERROR(EINVAL);
801 } else if (avctx->channel_layout) {
802 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
806 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
807 ret = avctx->codec->init(avctx);
813 entangled_thread_counter--;
815 /* Release any user-supplied mutex. */
817 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
820 av_dict_free(options);
827 av_freep(&avctx->priv_data);
828 av_freep(&avctx->internal);
833 int ff_alloc_packet(AVPacket *avpkt, int size)
835 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
836 return AVERROR(EINVAL);
841 if (avpkt->size < size)
842 return AVERROR(EINVAL);
844 pkt_data = avpkt->data;
845 av_init_packet(avpkt);
846 avpkt->data = pkt_data;
850 return av_new_packet(avpkt, size);
854 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
856 const AVFrame *frame,
860 int user_packet = !!avpkt->data;
863 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
864 av_init_packet(avpkt);
869 /* check for valid frame size */
871 nb_samples = frame->nb_samples;
872 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
873 if (nb_samples > avctx->frame_size)
874 return AVERROR(EINVAL);
875 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
876 if (nb_samples != avctx->frame_size)
877 return AVERROR(EINVAL);
880 nb_samples = avctx->frame_size;
883 if (avctx->codec->encode2) {
885 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
886 if (!ret && *got_packet_ptr) {
887 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
888 if (avpkt->pts == AV_NOPTS_VALUE)
889 avpkt->pts = frame->pts;
890 if (!avpkt->duration)
891 avpkt->duration = ff_samples_to_time_base(avctx,
894 avpkt->dts = avpkt->pts;
899 /* for compatibility with encoders not supporting encode2(), we need to
900 allocate a packet buffer if the user has not provided one or check
901 the size otherwise */
903 int buf_size = avpkt->size;
905 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
906 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
907 buf_size = nb_samples * avctx->channels *
908 av_get_bits_per_sample(avctx->codec_id) / 8;
910 /* this is a guess as to the required size.
911 if an encoder needs more than this, it should probably
912 implement encode2() */
913 buf_size = 2 * avctx->frame_size * avctx->channels *
914 av_get_bytes_per_sample(avctx->sample_fmt);
915 buf_size += FF_MIN_BUFFER_SIZE;
918 if ((ret = ff_alloc_packet(avpkt, buf_size)))
921 /* Encoders using AVCodec.encode() that support
922 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
923 the smaller size when encoding the last frame.
924 This code can be removed once all encoders supporting
925 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
926 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
927 nb_samples < avctx->frame_size) {
928 fs_tmp = avctx->frame_size;
929 avctx->frame_size = nb_samples;
932 /* encode the frame */
933 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
934 frame ? frame->data[0] : NULL);
937 /* no output. if the packet data was allocated by libavcodec,
940 av_freep(&avpkt->data);
942 if (avctx->coded_frame)
943 avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
944 /* Set duration for final small packet. This can be removed
945 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
948 avpkt->duration = ff_samples_to_time_base(avctx,
953 *got_packet_ptr = (ret > 0);
958 avctx->frame_size = fs_tmp;
961 avctx->frame_number++;
963 /* NOTE: if we add any audio encoders which output non-keyframe packets,
964 this needs to be moved to the encoders, but for now we can do it
965 here to simplify things */
966 avpkt->flags |= AV_PKT_FLAG_KEY;
971 #if FF_API_OLD_DECODE_AUDIO
972 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
973 uint8_t *buf, int buf_size,
974 const short *samples)
979 int ret, samples_size, got_packet;
981 av_init_packet(&pkt);
987 avcodec_get_frame_defaults(frame);
989 if (avctx->frame_size) {
990 frame->nb_samples = avctx->frame_size;
992 /* if frame_size is not set, the number of samples must be
993 calculated from the buffer size */
995 if (!av_get_bits_per_sample(avctx->codec_id)) {
996 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
997 "support this codec\n");
998 return AVERROR(EINVAL);
1000 nb_samples = (int64_t)buf_size * 8 /
1001 (av_get_bits_per_sample(avctx->codec_id) *
1003 if (nb_samples >= INT_MAX)
1004 return AVERROR(EINVAL);
1005 frame->nb_samples = nb_samples;
1008 /* it is assumed that the samples buffer is large enough based on the
1009 relevant parameters */
1010 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1012 avctx->sample_fmt, 1);
1013 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1015 samples, samples_size, 1)))
1018 /* fabricate frame pts from sample count.
1019 this is needed because the avcodec_encode_audio() API does not have
1020 a way for the user to provide pts */
1021 frame->pts = ff_samples_to_time_base(avctx,
1022 avctx->internal->sample_count);
1023 avctx->internal->sample_count += frame->nb_samples;
1029 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1030 if (!ret && got_packet && avctx->coded_frame) {
1031 avctx->coded_frame->pts = pkt.pts;
1032 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1034 /* free any side data since we cannot return it */
1035 if (pkt.side_data_elems > 0) {
1037 for (i = 0; i < pkt.side_data_elems; i++)
1038 av_free(pkt.side_data[i].data);
1039 av_freep(&pkt.side_data);
1040 pkt.side_data_elems = 0;
1043 if (frame && frame->extended_data != frame->data)
1044 av_free(frame->extended_data);
1046 return ret ? ret : pkt.size;
1050 #if FF_API_OLD_ENCODE_VIDEO
1051 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1052 const AVFrame *pict)
1055 int ret, got_packet = 0;
1057 if(buf_size < FF_MIN_BUFFER_SIZE){
1058 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1062 av_init_packet(&pkt);
1064 pkt.size = buf_size;
1066 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1067 if (!ret && got_packet && avctx->coded_frame) {
1068 avctx->coded_frame->pts = pkt.pts;
1069 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1072 /* free any side data since we cannot return it */
1073 if (pkt.side_data_elems > 0) {
1075 for (i = 0; i < pkt.side_data_elems; i++)
1076 av_free(pkt.side_data[i].data);
1077 av_freep(&pkt.side_data);
1078 pkt.side_data_elems = 0;
1081 return ret ? ret : pkt.size;
1085 #define MAX_CODED_FRAME_SIZE(width, height)\
1086 (8*(width)*(height) + FF_MIN_BUFFER_SIZE)
1088 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1090 const AVFrame *frame,
1091 int *got_packet_ptr)
1094 int user_packet = !!avpkt->data;
1096 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1097 av_init_packet(avpkt);
1099 *got_packet_ptr = 0;
1103 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1104 return AVERROR(EINVAL);
1106 if (avctx->codec->encode2) {
1107 *got_packet_ptr = 0;
1108 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1110 if (!*got_packet_ptr)
1112 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1113 avpkt->pts = avpkt->dts = frame->pts;
1116 /* for compatibility with encoders not supporting encode2(), we need to
1117 allocate a packet buffer if the user has not provided one or check
1118 the size otherwise */
1119 int buf_size = avpkt->size;
1122 buf_size = MAX_CODED_FRAME_SIZE(avctx->width, avctx->height);
1124 if ((ret = ff_alloc_packet(avpkt, buf_size)))
1127 /* encode the frame */
1128 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size, frame);
1131 /* no output. if the packet data was allocated by libavcodec,
1134 av_freep(&avpkt->data);
1135 } else if (avctx->coded_frame) {
1136 avpkt->pts = avctx->coded_frame->pts;
1137 avpkt->flags |= AV_PKT_FLAG_KEY*avctx->coded_frame->key_frame;
1141 *got_packet_ptr = (ret > 0);
1147 avctx->frame_number++;
1153 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1154 const AVSubtitle *sub)
1157 if(sub->start_display_time) {
1158 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1161 if(sub->num_rects == 0 || !sub->rects)
1163 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1164 avctx->frame_number++;
1168 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1171 const uint8_t *data;
1174 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1177 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1178 if (!data || size < 4)
1180 flags = bytestream_get_le32(&data);
1182 if (size < 4) /* Required for any of the changes */
1184 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1185 avctx->channels = bytestream_get_le32(&data);
1188 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1191 avctx->channel_layout = bytestream_get_le64(&data);
1196 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1197 avctx->sample_rate = bytestream_get_le32(&data);
1200 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1203 avctx->width = bytestream_get_le32(&data);
1204 avctx->height = bytestream_get_le32(&data);
1205 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1210 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1211 int *got_picture_ptr,
1216 *got_picture_ptr= 0;
1217 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1221 apply_param_change(avctx, avpkt);
1223 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1224 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1225 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1228 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1230 picture->pkt_dts= avpkt->dts;
1231 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1232 picture->width = avctx->width;
1233 picture->height = avctx->height;
1234 picture->format = avctx->pix_fmt;
1237 emms_c(); //needed to avoid an emms_c() call before every return;
1239 if (*got_picture_ptr)
1240 avctx->frame_number++;
1247 #if FF_API_OLD_DECODE_AUDIO
1248 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1249 int *frame_size_ptr,
1253 int ret, got_frame = 0;
1255 if (avctx->get_buffer != avcodec_default_get_buffer) {
1256 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1257 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1258 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1259 "avcodec_decode_audio4()\n");
1260 avctx->get_buffer = avcodec_default_get_buffer;
1263 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1265 if (ret >= 0 && got_frame) {
1267 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1268 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1270 avctx->sample_fmt, 1);
1271 if (*frame_size_ptr < data_size) {
1272 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1273 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1274 return AVERROR(EINVAL);
1277 memcpy(samples, frame.extended_data[0], plane_size);
1279 if (planar && avctx->channels > 1) {
1280 uint8_t *out = ((uint8_t *)samples) + plane_size;
1281 for (ch = 1; ch < avctx->channels; ch++) {
1282 memcpy(out, frame.extended_data[ch], plane_size);
1286 *frame_size_ptr = data_size;
1288 *frame_size_ptr = 0;
1294 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1305 if (!avpkt->data && avpkt->size) {
1306 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1307 return AVERROR(EINVAL);
1310 apply_param_change(avctx, avpkt);
1312 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1313 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1314 if (ret >= 0 && *got_frame_ptr) {
1315 avctx->frame_number++;
1316 frame->pkt_dts = avpkt->dts;
1317 if (frame->format == AV_SAMPLE_FMT_NONE)
1318 frame->format = avctx->sample_fmt;
1324 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1332 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1334 avctx->frame_number++;
1338 void avsubtitle_free(AVSubtitle *sub)
1342 for (i = 0; i < sub->num_rects; i++)
1344 av_freep(&sub->rects[i]->pict.data[0]);
1345 av_freep(&sub->rects[i]->pict.data[1]);
1346 av_freep(&sub->rects[i]->pict.data[2]);
1347 av_freep(&sub->rects[i]->pict.data[3]);
1348 av_freep(&sub->rects[i]->text);
1349 av_freep(&sub->rects[i]->ass);
1350 av_freep(&sub->rects[i]);
1353 av_freep(&sub->rects);
1355 memset(sub, 0, sizeof(AVSubtitle));
1358 av_cold int avcodec_close(AVCodecContext *avctx)
1360 /* If there is a user-supplied mutex locking routine, call it. */
1361 if (ff_lockmgr_cb) {
1362 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1366 entangled_thread_counter++;
1367 if(entangled_thread_counter != 1){
1368 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1369 entangled_thread_counter--;
1373 if (avcodec_is_open(avctx)) {
1374 if (HAVE_THREADS && avctx->thread_opaque)
1375 ff_thread_free(avctx);
1376 if (avctx->codec && avctx->codec->close)
1377 avctx->codec->close(avctx);
1378 avcodec_default_free_buffers(avctx);
1379 avctx->coded_frame = NULL;
1380 av_freep(&avctx->internal);
1383 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1384 av_opt_free(avctx->priv_data);
1386 av_freep(&avctx->priv_data);
1387 if (codec_is_encoder(avctx->codec))
1388 av_freep(&avctx->extradata);
1389 avctx->codec = NULL;
1390 avctx->active_thread_type = 0;
1391 entangled_thread_counter--;
1393 /* Release any user-supplied mutex. */
1394 if (ff_lockmgr_cb) {
1395 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1400 AVCodec *avcodec_find_encoder(enum CodecID id)
1402 AVCodec *p, *experimental=NULL;
1405 if (codec_is_encoder(p) && p->id == id) {
1406 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1413 return experimental;
1416 AVCodec *avcodec_find_encoder_by_name(const char *name)
1423 if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
1430 AVCodec *avcodec_find_decoder(enum CodecID id)
1435 if (codec_is_decoder(p) && p->id == id)
1442 AVCodec *avcodec_find_decoder_by_name(const char *name)
1449 if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
1456 static int get_bit_rate(AVCodecContext *ctx)
1459 int bits_per_sample;
1461 switch(ctx->codec_type) {
1462 case AVMEDIA_TYPE_VIDEO:
1463 case AVMEDIA_TYPE_DATA:
1464 case AVMEDIA_TYPE_SUBTITLE:
1465 case AVMEDIA_TYPE_ATTACHMENT:
1466 bit_rate = ctx->bit_rate;
1468 case AVMEDIA_TYPE_AUDIO:
1469 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1470 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1479 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1481 int i, len, ret = 0;
1483 for (i = 0; i < 4; i++) {
1484 len = snprintf(buf, buf_size,
1485 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1487 buf_size = buf_size > len ? buf_size - len : 0;
1494 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1496 const char *codec_name;
1497 const char *profile = NULL;
1501 AVRational display_aspect_ratio;
1504 p = avcodec_find_encoder(enc->codec_id);
1506 p = avcodec_find_decoder(enc->codec_id);
1509 codec_name = p->name;
1510 profile = av_get_profile_name(p, enc->profile);
1511 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1512 /* fake mpeg2 transport stream codec (currently not
1514 codec_name = "mpeg2ts";
1515 } else if (enc->codec_name[0] != '\0') {
1516 codec_name = enc->codec_name;
1518 /* output avi tags */
1520 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1521 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1525 switch(enc->codec_type) {
1526 case AVMEDIA_TYPE_VIDEO:
1527 snprintf(buf, buf_size,
1529 codec_name, enc->mb_decision ? " (hq)" : "");
1531 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1533 if (enc->pix_fmt != PIX_FMT_NONE) {
1534 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1536 av_get_pix_fmt_name(enc->pix_fmt));
1539 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1541 enc->width, enc->height);
1542 if (enc->sample_aspect_ratio.num) {
1543 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1544 enc->width*enc->sample_aspect_ratio.num,
1545 enc->height*enc->sample_aspect_ratio.den,
1547 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1548 " [PAR %d:%d DAR %d:%d]",
1549 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1550 display_aspect_ratio.num, display_aspect_ratio.den);
1552 if(av_log_get_level() >= AV_LOG_DEBUG){
1553 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1554 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1556 enc->time_base.num/g, enc->time_base.den/g);
1560 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1561 ", q=%d-%d", enc->qmin, enc->qmax);
1564 case AVMEDIA_TYPE_AUDIO:
1565 snprintf(buf, buf_size,
1569 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1571 if (enc->sample_rate) {
1572 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1573 ", %d Hz", enc->sample_rate);
1575 av_strlcat(buf, ", ", buf_size);
1576 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1577 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1578 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1579 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1582 case AVMEDIA_TYPE_DATA:
1583 snprintf(buf, buf_size, "Data: %s", codec_name);
1585 case AVMEDIA_TYPE_SUBTITLE:
1586 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1588 case AVMEDIA_TYPE_ATTACHMENT:
1589 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1592 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1596 if (enc->flags & CODEC_FLAG_PASS1)
1597 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1599 if (enc->flags & CODEC_FLAG_PASS2)
1600 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1603 bitrate = get_bit_rate(enc);
1605 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1606 ", %d kb/s", bitrate / 1000);
1610 const char *av_get_profile_name(const AVCodec *codec, int profile)
1613 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1616 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1617 if (p->profile == profile)
1623 unsigned avcodec_version( void )
1625 return LIBAVCODEC_VERSION_INT;
1628 const char *avcodec_configuration(void)
1630 return LIBAV_CONFIGURATION;
1633 const char *avcodec_license(void)
1635 #define LICENSE_PREFIX "libavcodec license: "
1636 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1639 void avcodec_flush_buffers(AVCodecContext *avctx)
1641 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1642 ff_thread_flush(avctx);
1643 else if(avctx->codec->flush)
1644 avctx->codec->flush(avctx);
1647 static void video_free_buffers(AVCodecContext *s)
1649 AVCodecInternal *avci = s->internal;
1655 if (avci->buffer_count)
1656 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1657 avci->buffer_count);
1658 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1659 InternalBuffer *buf = &avci->buffer[i];
1661 av_freep(&buf->base[j]);
1665 av_freep(&avci->buffer);
1667 avci->buffer_count=0;
1670 static void audio_free_buffers(AVCodecContext *avctx)
1672 AVCodecInternal *avci = avctx->internal;
1673 InternalBuffer *buf;
1679 if (buf->extended_data) {
1680 av_free(buf->extended_data[0]);
1681 if (buf->extended_data != buf->data)
1682 av_free(buf->extended_data);
1684 av_freep(&avci->buffer);
1687 void avcodec_default_free_buffers(AVCodecContext *avctx)
1689 switch (avctx->codec_type) {
1690 case AVMEDIA_TYPE_VIDEO:
1691 video_free_buffers(avctx);
1693 case AVMEDIA_TYPE_AUDIO:
1694 audio_free_buffers(avctx);
1701 int av_get_bits_per_sample(enum CodecID codec_id){
1703 case CODEC_ID_ADPCM_SBPRO_2:
1705 case CODEC_ID_ADPCM_SBPRO_3:
1707 case CODEC_ID_ADPCM_SBPRO_4:
1708 case CODEC_ID_ADPCM_CT:
1709 case CODEC_ID_ADPCM_IMA_APC:
1710 case CODEC_ID_ADPCM_IMA_WAV:
1711 case CODEC_ID_ADPCM_IMA_QT:
1712 case CODEC_ID_ADPCM_SWF:
1713 case CODEC_ID_ADPCM_MS:
1714 case CODEC_ID_ADPCM_YAMAHA:
1715 case CODEC_ID_ADPCM_G722:
1717 case CODEC_ID_PCM_ALAW:
1718 case CODEC_ID_PCM_MULAW:
1719 case CODEC_ID_PCM_S8:
1720 case CODEC_ID_PCM_U8:
1721 case CODEC_ID_PCM_ZORK:
1723 case CODEC_ID_PCM_S16BE:
1724 case CODEC_ID_PCM_S16LE:
1725 case CODEC_ID_PCM_S16LE_PLANAR:
1726 case CODEC_ID_PCM_U16BE:
1727 case CODEC_ID_PCM_U16LE:
1729 case CODEC_ID_PCM_S24DAUD:
1730 case CODEC_ID_PCM_S24BE:
1731 case CODEC_ID_PCM_S24LE:
1732 case CODEC_ID_PCM_U24BE:
1733 case CODEC_ID_PCM_U24LE:
1735 case CODEC_ID_PCM_S32BE:
1736 case CODEC_ID_PCM_S32LE:
1737 case CODEC_ID_PCM_U32BE:
1738 case CODEC_ID_PCM_U32LE:
1739 case CODEC_ID_PCM_F32BE:
1740 case CODEC_ID_PCM_F32LE:
1742 case CODEC_ID_PCM_F64BE:
1743 case CODEC_ID_PCM_F64LE:
1751 int ff_thread_init(AVCodecContext *s){
1756 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1770 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1772 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1776 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1778 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1779 "version to the newest one from Git. If the problem still "
1780 "occurs, it means that your file has a feature which has not "
1781 "been implemented.\n", feature);
1783 av_log_ask_for_sample(avc, NULL);
1786 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1788 va_list argument_list;
1790 va_start(argument_list, msg);
1793 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1794 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1795 "of this file to ftp://upload.libav.org/incoming/ "
1796 "and contact the libav-devel mailing list.\n");
1798 va_end(argument_list);
1801 static AVHWAccel *first_hwaccel = NULL;
1803 void av_register_hwaccel(AVHWAccel *hwaccel)
1805 AVHWAccel **p = &first_hwaccel;
1809 hwaccel->next = NULL;
1812 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1814 return hwaccel ? hwaccel->next : first_hwaccel;
1817 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1819 AVHWAccel *hwaccel=NULL;
1821 while((hwaccel= av_hwaccel_next(hwaccel))){
1822 if ( hwaccel->id == codec_id
1823 && hwaccel->pix_fmt == pix_fmt)
1829 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1831 if (ff_lockmgr_cb) {
1832 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1834 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1840 if (ff_lockmgr_cb) {
1841 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1843 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1849 int avpriv_lock_avformat(void)
1851 if (ff_lockmgr_cb) {
1852 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1858 int avpriv_unlock_avformat(void)
1860 if (ff_lockmgr_cb) {
1861 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1867 unsigned int avpriv_toupper4(unsigned int x)
1869 return toupper( x &0xFF)
1870 + (toupper((x>>8 )&0xFF)<<8 )
1871 + (toupper((x>>16)&0xFF)<<16)
1872 + (toupper((x>>24)&0xFF)<<24);
1877 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1880 return avctx->get_buffer(avctx, f);
1883 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1885 f->owner->release_buffer(f->owner, f);
1888 void ff_thread_finish_setup(AVCodecContext *avctx)
1892 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1896 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1902 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1904 if (codec_id <= CODEC_ID_NONE)
1905 return AVMEDIA_TYPE_UNKNOWN;
1906 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1907 return AVMEDIA_TYPE_VIDEO;
1908 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1909 return AVMEDIA_TYPE_AUDIO;
1910 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1911 return AVMEDIA_TYPE_SUBTITLE;
1913 return AVMEDIA_TYPE_UNKNOWN;
1916 int avcodec_is_open(AVCodecContext *s)
1918 return !!s->internal;