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 int av_codec_is_encoder(AVCodec *codec)
117 return codec && (codec->encode || codec->encode2);
120 int av_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, 0)))
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 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
396 av_freep(&buf->base[i]);
402 int h_chroma_shift, v_chroma_shift;
407 int stride_align[AV_NUM_DATA_POINTERS];
408 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
410 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
412 avcodec_align_dimensions2(s, &w, &h, stride_align);
414 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
420 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
421 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
422 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
423 // increase alignment of w for next try (rhs gives the lowest bit set in w)
428 unaligned |= picture.linesize[i] % stride_align[i];
432 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
436 for (i=0; i<3 && picture.data[i+1]; i++)
437 size[i] = picture.data[i+1] - picture.data[i];
438 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
440 memset(buf->base, 0, sizeof(buf->base));
441 memset(buf->data, 0, sizeof(buf->data));
443 for(i=0; i<4 && size[i]; i++){
444 const int h_shift= i==0 ? 0 : h_chroma_shift;
445 const int v_shift= i==0 ? 0 : v_chroma_shift;
447 buf->linesize[i]= picture.linesize[i];
449 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
450 if(buf->base[i]==NULL) return -1;
451 memset(buf->base[i], 128, size[i]);
453 // no edge if EDGE EMU or not planar YUV
454 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
455 buf->data[i] = buf->base[i];
457 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
459 for (; i < AV_NUM_DATA_POINTERS; i++) {
460 buf->base[i] = buf->data[i] = NULL;
461 buf->linesize[i] = 0;
463 if(size[1] && !size[2])
464 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
465 buf->width = s->width;
466 buf->height = s->height;
467 buf->pix_fmt= s->pix_fmt;
469 pic->type= FF_BUFFER_TYPE_INTERNAL;
471 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
472 pic->base[i]= buf->base[i];
473 pic->data[i]= buf->data[i];
474 pic->linesize[i]= buf->linesize[i];
476 pic->extended_data = pic->data;
477 avci->buffer_count++;
478 pic->width = buf->width;
479 pic->height = buf->height;
480 pic->format = buf->pix_fmt;
481 pic->sample_aspect_ratio = s->sample_aspect_ratio;
483 if(s->pkt) pic->pkt_pts= s->pkt->pts;
484 else pic->pkt_pts= AV_NOPTS_VALUE;
485 pic->reordered_opaque= s->reordered_opaque;
487 if(s->debug&FF_DEBUG_BUFFERS)
488 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
489 "buffers used\n", pic, avci->buffer_count);
494 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
496 switch (avctx->codec_type) {
497 case AVMEDIA_TYPE_VIDEO:
498 return video_get_buffer(avctx, frame);
499 case AVMEDIA_TYPE_AUDIO:
500 return audio_get_buffer(avctx, frame);
506 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
508 InternalBuffer *buf, *last;
509 AVCodecInternal *avci = s->internal;
511 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
513 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
514 assert(avci->buffer_count);
517 buf = NULL; /* avoids warning */
518 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
519 buf = &avci->buffer[i];
520 if (buf->data[0] == pic->data[0])
523 assert(i < avci->buffer_count);
524 avci->buffer_count--;
525 last = &avci->buffer[avci->buffer_count];
528 FFSWAP(InternalBuffer, *buf, *last);
531 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
533 // pic->base[i]=NULL;
535 //printf("R%X\n", pic->opaque);
537 if(s->debug&FF_DEBUG_BUFFERS)
538 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
539 "buffers used\n", pic, avci->buffer_count);
542 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
546 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
548 /* If no picture return a new buffer */
549 if(pic->data[0] == NULL) {
550 /* We will copy from buffer, so must be readable */
551 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
552 return s->get_buffer(s, pic);
555 assert(s->pix_fmt == pic->format);
557 /* If internal buffer type return the same buffer */
558 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
559 if(s->pkt) pic->pkt_pts= s->pkt->pts;
560 else pic->pkt_pts= AV_NOPTS_VALUE;
561 pic->reordered_opaque= s->reordered_opaque;
566 * Not internal type and reget_buffer not overridden, emulate cr buffer
569 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
570 pic->data[i] = pic->base[i] = NULL;
572 /* Allocate new frame */
573 if (s->get_buffer(s, pic))
575 /* Copy image data from old buffer to new buffer */
576 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
578 s->release_buffer(s, &temp_pic); // Release old frame
582 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
585 for(i=0; i<count; i++){
586 int r= func(c, (char*)arg + i*size);
592 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
595 for(i=0; i<count; i++){
596 int r= func(c, arg, i, 0);
602 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
603 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
608 void avcodec_get_frame_defaults(AVFrame *pic){
609 memset(pic, 0, sizeof(AVFrame));
611 pic->pts= AV_NOPTS_VALUE;
613 pic->sample_aspect_ratio = (AVRational){0, 1};
614 pic->format = -1; /* unknown */
617 AVFrame *avcodec_alloc_frame(void){
618 AVFrame *pic= av_malloc(sizeof(AVFrame));
620 if(pic==NULL) return NULL;
622 avcodec_get_frame_defaults(pic);
627 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
630 AVDictionary *tmp = NULL;
632 if (avcodec_is_open(avctx))
635 if ((!codec && !avctx->codec)) {
636 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
637 return AVERROR(EINVAL);
639 if ((codec && avctx->codec && codec != avctx->codec)) {
640 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
641 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
642 return AVERROR(EINVAL);
645 codec = avctx->codec;
647 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
648 return AVERROR(EINVAL);
651 av_dict_copy(&tmp, *options, 0);
653 /* If there is a user-supplied mutex locking routine, call it. */
655 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
659 entangled_thread_counter++;
660 if(entangled_thread_counter != 1){
661 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
666 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
667 if (!avctx->internal) {
668 ret = AVERROR(ENOMEM);
672 if (codec->priv_data_size > 0) {
673 if(!avctx->priv_data){
674 avctx->priv_data = av_mallocz(codec->priv_data_size);
675 if (!avctx->priv_data) {
676 ret = AVERROR(ENOMEM);
679 if (codec->priv_class) {
680 *(AVClass**)avctx->priv_data= codec->priv_class;
681 av_opt_set_defaults(avctx->priv_data);
684 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
687 avctx->priv_data = NULL;
689 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
692 if(avctx->coded_width && avctx->coded_height)
693 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
694 else if(avctx->width && avctx->height)
695 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
697 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
698 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
699 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
700 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
701 avcodec_set_dimensions(avctx, 0, 0);
704 /* if the decoder init function was already called previously,
705 free the already allocated subtitle_header before overwriting it */
706 if (av_codec_is_decoder(codec))
707 av_freep(&avctx->subtitle_header);
709 #define SANE_NB_CHANNELS 128U
710 if (avctx->channels > SANE_NB_CHANNELS) {
711 ret = AVERROR(EINVAL);
715 avctx->codec = codec;
716 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
717 avctx->codec_id == CODEC_ID_NONE) {
718 avctx->codec_type = codec->type;
719 avctx->codec_id = codec->id;
721 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
722 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
723 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
724 ret = AVERROR(EINVAL);
727 avctx->frame_number = 0;
729 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
730 (!avctx->time_base.num || !avctx->time_base.den)) {
731 avctx->time_base.num = 1;
732 avctx->time_base.den = avctx->sample_rate;
735 if (HAVE_THREADS && !avctx->thread_opaque) {
736 ret = ff_thread_init(avctx);
741 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
742 avctx->thread_count = 1;
744 if (avctx->codec->max_lowres < avctx->lowres) {
745 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
746 avctx->codec->max_lowres);
747 ret = AVERROR(EINVAL);
750 if (av_codec_is_encoder(avctx->codec)) {
752 if (avctx->codec->sample_fmts) {
753 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
754 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
756 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
757 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
758 ret = AVERROR(EINVAL);
762 if (avctx->codec->pix_fmts) {
763 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
764 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
766 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
767 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
768 ret = AVERROR(EINVAL);
772 if (avctx->codec->supported_samplerates) {
773 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
774 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
776 if (avctx->codec->supported_samplerates[i] == 0) {
777 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
778 ret = AVERROR(EINVAL);
782 if (avctx->codec->channel_layouts) {
783 if (!avctx->channel_layout) {
784 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
786 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
787 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
789 if (avctx->codec->channel_layouts[i] == 0) {
790 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
791 ret = AVERROR(EINVAL);
796 if (avctx->channel_layout && avctx->channels) {
797 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
798 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
799 ret = AVERROR(EINVAL);
802 } else if (avctx->channel_layout) {
803 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
807 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
808 ret = avctx->codec->init(avctx);
814 if (av_codec_is_decoder(avctx->codec)) {
815 /* validate channel layout from the decoder */
816 if (avctx->channel_layout &&
817 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
818 av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
819 avctx->channel_layout = 0;
823 entangled_thread_counter--;
825 /* Release any user-supplied mutex. */
827 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
830 av_dict_free(options);
837 av_freep(&avctx->priv_data);
838 av_freep(&avctx->internal);
843 int ff_alloc_packet(AVPacket *avpkt, int size)
845 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
846 return AVERROR(EINVAL);
849 void *destruct = avpkt->destruct;
851 if (avpkt->size < size)
852 return AVERROR(EINVAL);
854 av_init_packet(avpkt);
855 avpkt->destruct = destruct;
859 return av_new_packet(avpkt, size);
863 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
865 const AVFrame *frame,
869 int user_packet = !!avpkt->data;
874 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
875 av_free_packet(avpkt);
876 av_init_packet(avpkt);
881 /* check for valid frame size */
883 nb_samples = frame->nb_samples;
884 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
885 if (nb_samples > avctx->frame_size)
886 return AVERROR(EINVAL);
887 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
888 if (nb_samples != avctx->frame_size)
889 return AVERROR(EINVAL);
892 nb_samples = avctx->frame_size;
895 if (avctx->codec->encode2) {
896 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
897 if (!ret && *got_packet_ptr) {
898 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
899 if (avpkt->pts == AV_NOPTS_VALUE)
900 avpkt->pts = frame->pts;
901 if (!avpkt->duration)
902 avpkt->duration = ff_samples_to_time_base(avctx,
905 avpkt->dts = avpkt->pts;
910 /* for compatibility with encoders not supporting encode2(), we need to
911 allocate a packet buffer if the user has not provided one or check
912 the size otherwise */
914 int buf_size = avpkt->size;
916 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
917 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
918 buf_size = nb_samples * avctx->channels *
919 av_get_bits_per_sample(avctx->codec_id) / 8;
921 /* this is a guess as to the required size.
922 if an encoder needs more than this, it should probably
923 implement encode2() */
924 buf_size = 2 * avctx->frame_size * avctx->channels *
925 av_get_bytes_per_sample(avctx->sample_fmt);
926 buf_size += FF_MIN_BUFFER_SIZE;
929 if ((ret = ff_alloc_packet(avpkt, buf_size)))
932 /* Encoders using AVCodec.encode() that support
933 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
934 the smaller size when encoding the last frame.
935 This code can be removed once all encoders supporting
936 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
937 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
938 nb_samples < avctx->frame_size) {
939 fs_tmp = avctx->frame_size;
940 avctx->frame_size = nb_samples;
943 /* encode the frame */
944 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
945 frame ? frame->data[0] : NULL);
948 /* no output. if the packet data was allocated by libavcodec,
951 av_freep(&avpkt->data);
953 if (avctx->coded_frame)
954 avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
955 /* Set duration for final small packet. This can be removed
956 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
959 avpkt->duration = ff_samples_to_time_base(avctx,
964 *got_packet_ptr = (ret > 0);
969 avctx->frame_size = fs_tmp;
972 if (!user_packet && avpkt->size) {
973 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
975 avpkt->data = new_data;
978 avctx->frame_number++;
981 if (ret < 0 || !*got_packet_ptr)
982 av_free_packet(avpkt);
984 /* NOTE: if we add any audio encoders which output non-keyframe packets,
985 this needs to be moved to the encoders, but for now we can do it
986 here to simplify things */
987 avpkt->flags |= AV_PKT_FLAG_KEY;
992 #if FF_API_OLD_DECODE_AUDIO
993 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
994 uint8_t *buf, int buf_size,
995 const short *samples)
1000 int ret, samples_size, got_packet;
1002 av_init_packet(&pkt);
1004 pkt.size = buf_size;
1008 avcodec_get_frame_defaults(frame);
1010 if (avctx->frame_size) {
1011 frame->nb_samples = avctx->frame_size;
1013 /* if frame_size is not set, the number of samples must be
1014 calculated from the buffer size */
1016 if (!av_get_bits_per_sample(avctx->codec_id)) {
1017 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1018 "support this codec\n");
1019 return AVERROR(EINVAL);
1021 nb_samples = (int64_t)buf_size * 8 /
1022 (av_get_bits_per_sample(avctx->codec_id) *
1024 if (nb_samples >= INT_MAX)
1025 return AVERROR(EINVAL);
1026 frame->nb_samples = nb_samples;
1029 /* it is assumed that the samples buffer is large enough based on the
1030 relevant parameters */
1031 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1033 avctx->sample_fmt, 1);
1034 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1036 samples, samples_size, 1)))
1039 /* fabricate frame pts from sample count.
1040 this is needed because the avcodec_encode_audio() API does not have
1041 a way for the user to provide pts */
1042 frame->pts = ff_samples_to_time_base(avctx,
1043 avctx->internal->sample_count);
1044 avctx->internal->sample_count += frame->nb_samples;
1050 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1051 if (!ret && got_packet && avctx->coded_frame) {
1052 avctx->coded_frame->pts = pkt.pts;
1053 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1055 /* free any side data since we cannot return it */
1056 if (pkt.side_data_elems > 0) {
1058 for (i = 0; i < pkt.side_data_elems; i++)
1059 av_free(pkt.side_data[i].data);
1060 av_freep(&pkt.side_data);
1061 pkt.side_data_elems = 0;
1064 if (frame && frame->extended_data != frame->data)
1065 av_free(frame->extended_data);
1067 return ret ? ret : pkt.size;
1071 #if FF_API_OLD_ENCODE_VIDEO
1072 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1073 const AVFrame *pict)
1076 int ret, got_packet = 0;
1078 if(buf_size < FF_MIN_BUFFER_SIZE){
1079 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1083 av_init_packet(&pkt);
1085 pkt.size = buf_size;
1087 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1088 if (!ret && got_packet && avctx->coded_frame) {
1089 avctx->coded_frame->pts = pkt.pts;
1090 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1093 /* free any side data since we cannot return it */
1094 if (pkt.side_data_elems > 0) {
1096 for (i = 0; i < pkt.side_data_elems; i++)
1097 av_free(pkt.side_data[i].data);
1098 av_freep(&pkt.side_data);
1099 pkt.side_data_elems = 0;
1102 return ret ? ret : pkt.size;
1106 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1108 const AVFrame *frame,
1109 int *got_packet_ptr)
1112 int user_packet = !!avpkt->data;
1114 *got_packet_ptr = 0;
1116 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1117 av_free_packet(avpkt);
1118 av_init_packet(avpkt);
1123 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1124 return AVERROR(EINVAL);
1126 av_assert0(avctx->codec->encode2);
1128 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1130 if (!*got_packet_ptr)
1132 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1133 avpkt->pts = avpkt->dts = frame->pts;
1135 if (!user_packet && avpkt->size) {
1136 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1138 avpkt->data = new_data;
1141 avctx->frame_number++;
1144 if (ret < 0 || !*got_packet_ptr)
1145 av_free_packet(avpkt);
1151 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1152 const AVSubtitle *sub)
1155 if(sub->start_display_time) {
1156 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1159 if(sub->num_rects == 0 || !sub->rects)
1161 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1162 avctx->frame_number++;
1166 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1169 const uint8_t *data;
1172 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1175 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1176 if (!data || size < 4)
1178 flags = bytestream_get_le32(&data);
1180 if (size < 4) /* Required for any of the changes */
1182 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1183 avctx->channels = bytestream_get_le32(&data);
1186 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1189 avctx->channel_layout = bytestream_get_le64(&data);
1194 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1195 avctx->sample_rate = bytestream_get_le32(&data);
1198 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1201 avctx->width = bytestream_get_le32(&data);
1202 avctx->height = bytestream_get_le32(&data);
1203 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1208 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1209 int *got_picture_ptr,
1214 *got_picture_ptr= 0;
1215 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1219 apply_param_change(avctx, avpkt);
1221 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1222 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1223 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1226 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1228 picture->pkt_dts= avpkt->dts;
1229 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1230 picture->width = avctx->width;
1231 picture->height = avctx->height;
1232 picture->format = avctx->pix_fmt;
1235 emms_c(); //needed to avoid an emms_c() call before every return;
1237 if (*got_picture_ptr)
1238 avctx->frame_number++;
1245 #if FF_API_OLD_DECODE_AUDIO
1246 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1247 int *frame_size_ptr,
1251 int ret, got_frame = 0;
1253 if (avctx->get_buffer != avcodec_default_get_buffer) {
1254 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1255 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1256 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1257 "avcodec_decode_audio4()\n");
1258 avctx->get_buffer = avcodec_default_get_buffer;
1261 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1263 if (ret >= 0 && got_frame) {
1265 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1266 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1268 avctx->sample_fmt, 1);
1269 if (*frame_size_ptr < data_size) {
1270 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1271 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1272 return AVERROR(EINVAL);
1275 memcpy(samples, frame.extended_data[0], plane_size);
1277 if (planar && avctx->channels > 1) {
1278 uint8_t *out = ((uint8_t *)samples) + plane_size;
1279 for (ch = 1; ch < avctx->channels; ch++) {
1280 memcpy(out, frame.extended_data[ch], plane_size);
1284 *frame_size_ptr = data_size;
1286 *frame_size_ptr = 0;
1292 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1303 if (!avpkt->data && avpkt->size) {
1304 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1305 return AVERROR(EINVAL);
1308 apply_param_change(avctx, avpkt);
1310 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1311 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1312 if (ret >= 0 && *got_frame_ptr) {
1313 avctx->frame_number++;
1314 frame->pkt_dts = avpkt->dts;
1315 if (frame->format == AV_SAMPLE_FMT_NONE)
1316 frame->format = avctx->sample_fmt;
1322 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1330 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1332 avctx->frame_number++;
1336 void avsubtitle_free(AVSubtitle *sub)
1340 for (i = 0; i < sub->num_rects; i++)
1342 av_freep(&sub->rects[i]->pict.data[0]);
1343 av_freep(&sub->rects[i]->pict.data[1]);
1344 av_freep(&sub->rects[i]->pict.data[2]);
1345 av_freep(&sub->rects[i]->pict.data[3]);
1346 av_freep(&sub->rects[i]->text);
1347 av_freep(&sub->rects[i]->ass);
1348 av_freep(&sub->rects[i]);
1351 av_freep(&sub->rects);
1353 memset(sub, 0, sizeof(AVSubtitle));
1356 av_cold int avcodec_close(AVCodecContext *avctx)
1358 /* If there is a user-supplied mutex locking routine, call it. */
1359 if (ff_lockmgr_cb) {
1360 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1364 entangled_thread_counter++;
1365 if(entangled_thread_counter != 1){
1366 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1367 entangled_thread_counter--;
1371 if (avcodec_is_open(avctx)) {
1372 if (HAVE_THREADS && avctx->thread_opaque)
1373 ff_thread_free(avctx);
1374 if (avctx->codec && avctx->codec->close)
1375 avctx->codec->close(avctx);
1376 avcodec_default_free_buffers(avctx);
1377 avctx->coded_frame = NULL;
1378 av_freep(&avctx->internal);
1381 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1382 av_opt_free(avctx->priv_data);
1384 av_freep(&avctx->priv_data);
1385 if (av_codec_is_encoder(avctx->codec))
1386 av_freep(&avctx->extradata);
1387 avctx->codec = NULL;
1388 avctx->active_thread_type = 0;
1389 entangled_thread_counter--;
1391 /* Release any user-supplied mutex. */
1392 if (ff_lockmgr_cb) {
1393 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1398 AVCodec *avcodec_find_encoder(enum CodecID id)
1400 AVCodec *p, *experimental=NULL;
1403 if (av_codec_is_encoder(p) && p->id == id) {
1404 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1411 return experimental;
1414 AVCodec *avcodec_find_encoder_by_name(const char *name)
1421 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1428 AVCodec *avcodec_find_decoder(enum CodecID id)
1433 if (av_codec_is_decoder(p) && p->id == id)
1440 AVCodec *avcodec_find_decoder_by_name(const char *name)
1447 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1454 static int get_bit_rate(AVCodecContext *ctx)
1457 int bits_per_sample;
1459 switch(ctx->codec_type) {
1460 case AVMEDIA_TYPE_VIDEO:
1461 case AVMEDIA_TYPE_DATA:
1462 case AVMEDIA_TYPE_SUBTITLE:
1463 case AVMEDIA_TYPE_ATTACHMENT:
1464 bit_rate = ctx->bit_rate;
1466 case AVMEDIA_TYPE_AUDIO:
1467 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1468 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1477 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1479 int i, len, ret = 0;
1481 for (i = 0; i < 4; i++) {
1482 len = snprintf(buf, buf_size,
1483 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1485 buf_size = buf_size > len ? buf_size - len : 0;
1492 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1494 const char *codec_name;
1495 const char *profile = NULL;
1499 AVRational display_aspect_ratio;
1502 p = avcodec_find_encoder(enc->codec_id);
1504 p = avcodec_find_decoder(enc->codec_id);
1507 codec_name = p->name;
1508 profile = av_get_profile_name(p, enc->profile);
1509 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1510 /* fake mpeg2 transport stream codec (currently not
1512 codec_name = "mpeg2ts";
1513 } else if (enc->codec_name[0] != '\0') {
1514 codec_name = enc->codec_name;
1516 /* output avi tags */
1518 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1519 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1523 switch(enc->codec_type) {
1524 case AVMEDIA_TYPE_VIDEO:
1525 snprintf(buf, buf_size,
1527 codec_name, enc->mb_decision ? " (hq)" : "");
1529 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1531 if (enc->pix_fmt != PIX_FMT_NONE) {
1532 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1534 av_get_pix_fmt_name(enc->pix_fmt));
1537 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1539 enc->width, enc->height);
1540 if (enc->sample_aspect_ratio.num) {
1541 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1542 enc->width*enc->sample_aspect_ratio.num,
1543 enc->height*enc->sample_aspect_ratio.den,
1545 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1546 " [PAR %d:%d DAR %d:%d]",
1547 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1548 display_aspect_ratio.num, display_aspect_ratio.den);
1550 if(av_log_get_level() >= AV_LOG_DEBUG){
1551 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1552 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1554 enc->time_base.num/g, enc->time_base.den/g);
1558 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1559 ", q=%d-%d", enc->qmin, enc->qmax);
1562 case AVMEDIA_TYPE_AUDIO:
1563 snprintf(buf, buf_size,
1567 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1569 if (enc->sample_rate) {
1570 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1571 ", %d Hz", enc->sample_rate);
1573 av_strlcat(buf, ", ", buf_size);
1574 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1575 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1576 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1577 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1580 case AVMEDIA_TYPE_DATA:
1581 snprintf(buf, buf_size, "Data: %s", codec_name);
1583 case AVMEDIA_TYPE_SUBTITLE:
1584 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1586 case AVMEDIA_TYPE_ATTACHMENT:
1587 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1590 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1594 if (enc->flags & CODEC_FLAG_PASS1)
1595 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1597 if (enc->flags & CODEC_FLAG_PASS2)
1598 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1601 bitrate = get_bit_rate(enc);
1603 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1604 ", %d kb/s", bitrate / 1000);
1608 const char *av_get_profile_name(const AVCodec *codec, int profile)
1611 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1614 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1615 if (p->profile == profile)
1621 unsigned avcodec_version( void )
1623 return LIBAVCODEC_VERSION_INT;
1626 const char *avcodec_configuration(void)
1628 return LIBAV_CONFIGURATION;
1631 const char *avcodec_license(void)
1633 #define LICENSE_PREFIX "libavcodec license: "
1634 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1637 void avcodec_flush_buffers(AVCodecContext *avctx)
1639 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1640 ff_thread_flush(avctx);
1641 else if(avctx->codec->flush)
1642 avctx->codec->flush(avctx);
1645 static void video_free_buffers(AVCodecContext *s)
1647 AVCodecInternal *avci = s->internal;
1653 if (avci->buffer_count)
1654 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1655 avci->buffer_count);
1656 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1657 InternalBuffer *buf = &avci->buffer[i];
1659 av_freep(&buf->base[j]);
1663 av_freep(&avci->buffer);
1665 avci->buffer_count=0;
1668 static void audio_free_buffers(AVCodecContext *avctx)
1670 AVCodecInternal *avci = avctx->internal;
1671 InternalBuffer *buf;
1677 if (buf->extended_data) {
1678 av_free(buf->extended_data[0]);
1679 if (buf->extended_data != buf->data)
1680 av_free(buf->extended_data);
1682 av_freep(&avci->buffer);
1685 void avcodec_default_free_buffers(AVCodecContext *avctx)
1687 switch (avctx->codec_type) {
1688 case AVMEDIA_TYPE_VIDEO:
1689 video_free_buffers(avctx);
1691 case AVMEDIA_TYPE_AUDIO:
1692 audio_free_buffers(avctx);
1699 int av_get_exact_bits_per_sample(enum CodecID codec_id)
1702 case CODEC_ID_ADPCM_CT:
1703 case CODEC_ID_ADPCM_IMA_APC:
1704 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1705 case CODEC_ID_ADPCM_IMA_WS:
1706 case CODEC_ID_ADPCM_G722:
1707 case CODEC_ID_ADPCM_YAMAHA:
1709 case CODEC_ID_PCM_ALAW:
1710 case CODEC_ID_PCM_MULAW:
1711 case CODEC_ID_PCM_S8:
1712 case CODEC_ID_PCM_U8:
1713 case CODEC_ID_PCM_ZORK:
1715 case CODEC_ID_PCM_S16BE:
1716 case CODEC_ID_PCM_S16LE:
1717 case CODEC_ID_PCM_S16LE_PLANAR:
1718 case CODEC_ID_PCM_U16BE:
1719 case CODEC_ID_PCM_U16LE:
1721 case CODEC_ID_PCM_S24DAUD:
1722 case CODEC_ID_PCM_S24BE:
1723 case CODEC_ID_PCM_S24LE:
1724 case CODEC_ID_PCM_U24BE:
1725 case CODEC_ID_PCM_U24LE:
1727 case CODEC_ID_PCM_S32BE:
1728 case CODEC_ID_PCM_S32LE:
1729 case CODEC_ID_PCM_U32BE:
1730 case CODEC_ID_PCM_U32LE:
1731 case CODEC_ID_PCM_F32BE:
1732 case CODEC_ID_PCM_F32LE:
1734 case CODEC_ID_PCM_F64BE:
1735 case CODEC_ID_PCM_F64LE:
1742 int av_get_bits_per_sample(enum CodecID codec_id)
1745 case CODEC_ID_ADPCM_SBPRO_2:
1747 case CODEC_ID_ADPCM_SBPRO_3:
1749 case CODEC_ID_ADPCM_SBPRO_4:
1750 case CODEC_ID_ADPCM_IMA_WAV:
1751 case CODEC_ID_ADPCM_IMA_QT:
1752 case CODEC_ID_ADPCM_SWF:
1753 case CODEC_ID_ADPCM_MS:
1756 return av_get_exact_bits_per_sample(codec_id);
1760 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1762 int id, sr, ch, ba, tag, bps;
1764 id = avctx->codec_id;
1765 sr = avctx->sample_rate;
1766 ch = avctx->channels;
1767 ba = avctx->block_align;
1768 tag = avctx->codec_tag;
1769 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1771 /* codecs with an exact constant bits per sample */
1772 if (bps > 0 && ch > 0 && frame_bytes > 0)
1773 return (frame_bytes * 8) / (bps * ch);
1774 bps = avctx->bits_per_coded_sample;
1776 /* codecs with a fixed packet duration */
1778 case CODEC_ID_ADPCM_ADX: return 32;
1779 case CODEC_ID_ADPCM_IMA_QT: return 64;
1780 case CODEC_ID_ADPCM_EA_XAS: return 128;
1781 case CODEC_ID_AMR_NB:
1783 case CODEC_ID_QCELP:
1784 case CODEC_ID_RA_144:
1785 case CODEC_ID_RA_288: return 160;
1786 case CODEC_ID_IMC: return 256;
1787 case CODEC_ID_AMR_WB:
1788 case CODEC_ID_GSM_MS: return 320;
1789 case CODEC_ID_MP1: return 384;
1790 case CODEC_ID_ATRAC1: return 512;
1791 case CODEC_ID_ATRAC3: return 1024;
1793 case CODEC_ID_MUSEPACK7: return 1152;
1794 case CODEC_ID_AC3: return 1536;
1798 /* calc from sample rate */
1799 if (id == CODEC_ID_TTA)
1800 return 256 * sr / 245;
1803 /* calc from sample rate and channels */
1804 if (id == CODEC_ID_BINKAUDIO_DCT)
1805 return (480 << (sr / 22050)) / ch;
1810 /* calc from block_align */
1811 if (id == CODEC_ID_SIPR) {
1813 case 20: return 160;
1814 case 19: return 144;
1815 case 29: return 288;
1816 case 37: return 480;
1821 if (frame_bytes > 0) {
1822 /* calc from frame_bytes only */
1823 if (id == CODEC_ID_TRUESPEECH)
1824 return 240 * (frame_bytes / 32);
1825 if (id == CODEC_ID_NELLYMOSER)
1826 return 256 * (frame_bytes / 64);
1829 /* calc from frame_bytes and bits_per_coded_sample */
1830 if (id == CODEC_ID_ADPCM_G726)
1831 return frame_bytes * 8 / bps;
1835 /* calc from frame_bytes and channels */
1837 case CODEC_ID_ADPCM_4XM:
1838 case CODEC_ID_ADPCM_IMA_ISS:
1839 return (frame_bytes - 4 * ch) * 2 / ch;
1840 case CODEC_ID_ADPCM_IMA_SMJPEG:
1841 return (frame_bytes - 4) * 2 / ch;
1842 case CODEC_ID_ADPCM_IMA_AMV:
1843 return (frame_bytes - 8) * 2 / ch;
1844 case CODEC_ID_ADPCM_XA:
1845 return (frame_bytes / 128) * 224 / ch;
1846 case CODEC_ID_INTERPLAY_DPCM:
1847 return (frame_bytes - 6 - ch) / ch;
1848 case CODEC_ID_ROQ_DPCM:
1849 return (frame_bytes - 8) / ch;
1850 case CODEC_ID_XAN_DPCM:
1851 return (frame_bytes - 2 * ch) / ch;
1852 case CODEC_ID_MACE3:
1853 return 3 * frame_bytes / ch;
1854 case CODEC_ID_MACE6:
1855 return 6 * frame_bytes / ch;
1856 case CODEC_ID_PCM_LXF:
1857 return 2 * (frame_bytes / (5 * ch));
1861 /* calc from frame_bytes, channels, and codec_tag */
1862 if (id == CODEC_ID_SOL_DPCM) {
1864 return frame_bytes / ch;
1866 return frame_bytes * 2 / ch;
1871 /* calc from frame_bytes, channels, and block_align */
1872 int blocks = frame_bytes / ba;
1873 switch (avctx->codec_id) {
1874 case CODEC_ID_ADPCM_IMA_WAV:
1875 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1876 case CODEC_ID_ADPCM_IMA_DK3:
1877 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1878 case CODEC_ID_ADPCM_IMA_DK4:
1879 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1880 case CODEC_ID_ADPCM_MS:
1881 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1886 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1887 switch (avctx->codec_id) {
1888 case CODEC_ID_PCM_DVD:
1889 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1890 case CODEC_ID_PCM_BLURAY:
1891 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1892 case CODEC_ID_S302M:
1893 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1903 int ff_thread_init(AVCodecContext *s){
1908 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1922 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1924 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1928 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1930 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1931 "version to the newest one from Git. If the problem still "
1932 "occurs, it means that your file has a feature which has not "
1933 "been implemented.\n", feature);
1935 av_log_ask_for_sample(avc, NULL);
1938 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1940 va_list argument_list;
1942 va_start(argument_list, msg);
1945 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1946 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1947 "of this file to ftp://upload.libav.org/incoming/ "
1948 "and contact the libav-devel mailing list.\n");
1950 va_end(argument_list);
1953 static AVHWAccel *first_hwaccel = NULL;
1955 void av_register_hwaccel(AVHWAccel *hwaccel)
1957 AVHWAccel **p = &first_hwaccel;
1961 hwaccel->next = NULL;
1964 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1966 return hwaccel ? hwaccel->next : first_hwaccel;
1969 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1971 AVHWAccel *hwaccel=NULL;
1973 while((hwaccel= av_hwaccel_next(hwaccel))){
1974 if ( hwaccel->id == codec_id
1975 && hwaccel->pix_fmt == pix_fmt)
1981 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1983 if (ff_lockmgr_cb) {
1984 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1986 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1992 if (ff_lockmgr_cb) {
1993 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1995 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2001 int avpriv_lock_avformat(void)
2003 if (ff_lockmgr_cb) {
2004 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2010 int avpriv_unlock_avformat(void)
2012 if (ff_lockmgr_cb) {
2013 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2019 unsigned int avpriv_toupper4(unsigned int x)
2021 return toupper( x &0xFF)
2022 + (toupper((x>>8 )&0xFF)<<8 )
2023 + (toupper((x>>16)&0xFF)<<16)
2024 + (toupper((x>>24)&0xFF)<<24);
2029 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2032 return avctx->get_buffer(avctx, f);
2035 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2037 f->owner->release_buffer(f->owner, f);
2040 void ff_thread_finish_setup(AVCodecContext *avctx)
2044 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2048 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2054 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2056 if (codec_id <= CODEC_ID_NONE)
2057 return AVMEDIA_TYPE_UNKNOWN;
2058 else if (codec_id < CODEC_ID_FIRST_AUDIO)
2059 return AVMEDIA_TYPE_VIDEO;
2060 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2061 return AVMEDIA_TYPE_AUDIO;
2062 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2063 return AVMEDIA_TYPE_SUBTITLE;
2065 return AVMEDIA_TYPE_UNKNOWN;
2068 int avcodec_is_open(AVCodecContext *s)
2070 return !!s->internal;