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, 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 assert(s->pix_fmt == pic->format);
558 /* If internal buffer type return the same buffer */
559 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
560 if(s->pkt) pic->pkt_pts= s->pkt->pts;
561 else pic->pkt_pts= AV_NOPTS_VALUE;
562 pic->reordered_opaque= s->reordered_opaque;
567 * Not internal type and reget_buffer not overridden, emulate cr buffer
570 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
571 pic->data[i] = pic->base[i] = NULL;
573 /* Allocate new frame */
574 if (s->get_buffer(s, pic))
576 /* Copy image data from old buffer to new buffer */
577 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
579 s->release_buffer(s, &temp_pic); // Release old frame
583 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
586 for(i=0; i<count; i++){
587 int r= func(c, (char*)arg + i*size);
593 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
596 for(i=0; i<count; i++){
597 int r= func(c, arg, i, 0);
603 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
604 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
609 void avcodec_get_frame_defaults(AVFrame *pic){
610 memset(pic, 0, sizeof(AVFrame));
612 pic->pts= AV_NOPTS_VALUE;
614 pic->sample_aspect_ratio = (AVRational){0, 1};
615 pic->format = -1; /* unknown */
618 AVFrame *avcodec_alloc_frame(void){
619 AVFrame *pic= av_malloc(sizeof(AVFrame));
621 if(pic==NULL) return NULL;
623 avcodec_get_frame_defaults(pic);
628 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
631 AVDictionary *tmp = NULL;
633 if (avcodec_is_open(avctx))
636 if ((!codec && !avctx->codec)) {
637 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
638 return AVERROR(EINVAL);
640 if ((codec && avctx->codec && codec != avctx->codec)) {
641 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
642 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
643 return AVERROR(EINVAL);
646 codec = avctx->codec;
648 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
649 return AVERROR(EINVAL);
652 av_dict_copy(&tmp, *options, 0);
654 /* If there is a user-supplied mutex locking routine, call it. */
656 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
660 entangled_thread_counter++;
661 if(entangled_thread_counter != 1){
662 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
667 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
668 if (!avctx->internal) {
669 ret = AVERROR(ENOMEM);
673 if (codec->priv_data_size > 0) {
674 if(!avctx->priv_data){
675 avctx->priv_data = av_mallocz(codec->priv_data_size);
676 if (!avctx->priv_data) {
677 ret = AVERROR(ENOMEM);
680 if (codec->priv_class) {
681 *(AVClass**)avctx->priv_data= codec->priv_class;
682 av_opt_set_defaults(avctx->priv_data);
685 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
688 avctx->priv_data = NULL;
690 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
693 if(avctx->coded_width && avctx->coded_height)
694 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
695 else if(avctx->width && avctx->height)
696 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
698 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
699 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
700 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
701 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
702 avcodec_set_dimensions(avctx, 0, 0);
705 /* if the decoder init function was already called previously,
706 free the already allocated subtitle_header before overwriting it */
707 if (av_codec_is_decoder(codec))
708 av_freep(&avctx->subtitle_header);
710 #define SANE_NB_CHANNELS 128U
711 if (avctx->channels > SANE_NB_CHANNELS) {
712 ret = AVERROR(EINVAL);
716 avctx->codec = codec;
717 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
718 avctx->codec_id == CODEC_ID_NONE) {
719 avctx->codec_type = codec->type;
720 avctx->codec_id = codec->id;
722 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
723 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
724 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
725 ret = AVERROR(EINVAL);
728 avctx->frame_number = 0;
730 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
731 (!avctx->time_base.num || !avctx->time_base.den)) {
732 avctx->time_base.num = 1;
733 avctx->time_base.den = avctx->sample_rate;
736 if (HAVE_THREADS && !avctx->thread_opaque) {
737 ret = ff_thread_init(avctx);
742 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
743 avctx->thread_count = 1;
745 if (avctx->codec->max_lowres < avctx->lowres) {
746 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
747 avctx->codec->max_lowres);
748 ret = AVERROR(EINVAL);
751 if (av_codec_is_encoder(avctx->codec)) {
753 if (avctx->codec->sample_fmts) {
754 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
755 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
757 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
758 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
759 ret = AVERROR(EINVAL);
763 if (avctx->codec->pix_fmts) {
764 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
765 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
767 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
768 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
769 ret = AVERROR(EINVAL);
773 if (avctx->codec->supported_samplerates) {
774 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
775 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
777 if (avctx->codec->supported_samplerates[i] == 0) {
778 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
779 ret = AVERROR(EINVAL);
783 if (avctx->codec->channel_layouts) {
784 if (!avctx->channel_layout) {
785 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
787 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
788 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
790 if (avctx->codec->channel_layouts[i] == 0) {
791 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
792 ret = AVERROR(EINVAL);
797 if (avctx->channel_layout && avctx->channels) {
798 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
799 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
800 ret = AVERROR(EINVAL);
803 } else if (avctx->channel_layout) {
804 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
808 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
809 ret = avctx->codec->init(avctx);
815 entangled_thread_counter--;
817 /* Release any user-supplied mutex. */
819 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
822 av_dict_free(options);
829 av_freep(&avctx->priv_data);
830 av_freep(&avctx->internal);
835 int ff_alloc_packet(AVPacket *avpkt, int size)
837 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
838 return AVERROR(EINVAL);
841 void *destruct = avpkt->destruct;
843 if (avpkt->size < size)
844 return AVERROR(EINVAL);
846 av_init_packet(avpkt);
847 avpkt->destruct = destruct;
851 return av_new_packet(avpkt, size);
855 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
857 const AVFrame *frame,
861 int user_packet = !!avpkt->data;
866 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
867 av_free_packet(avpkt);
868 av_init_packet(avpkt);
873 /* check for valid frame size */
875 nb_samples = frame->nb_samples;
876 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
877 if (nb_samples > avctx->frame_size)
878 return AVERROR(EINVAL);
879 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
880 if (nb_samples != avctx->frame_size)
881 return AVERROR(EINVAL);
884 nb_samples = avctx->frame_size;
887 if (avctx->codec->encode2) {
888 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
889 if (!ret && *got_packet_ptr) {
890 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
891 if (avpkt->pts == AV_NOPTS_VALUE)
892 avpkt->pts = frame->pts;
893 if (!avpkt->duration)
894 avpkt->duration = ff_samples_to_time_base(avctx,
897 avpkt->dts = avpkt->pts;
902 /* for compatibility with encoders not supporting encode2(), we need to
903 allocate a packet buffer if the user has not provided one or check
904 the size otherwise */
906 int buf_size = avpkt->size;
908 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
909 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
910 buf_size = nb_samples * avctx->channels *
911 av_get_bits_per_sample(avctx->codec_id) / 8;
913 /* this is a guess as to the required size.
914 if an encoder needs more than this, it should probably
915 implement encode2() */
916 buf_size = 2 * avctx->frame_size * avctx->channels *
917 av_get_bytes_per_sample(avctx->sample_fmt);
918 buf_size += FF_MIN_BUFFER_SIZE;
921 if ((ret = ff_alloc_packet(avpkt, buf_size)))
924 /* Encoders using AVCodec.encode() that support
925 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
926 the smaller size when encoding the last frame.
927 This code can be removed once all encoders supporting
928 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
929 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
930 nb_samples < avctx->frame_size) {
931 fs_tmp = avctx->frame_size;
932 avctx->frame_size = nb_samples;
935 /* encode the frame */
936 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
937 frame ? frame->data[0] : NULL);
940 /* no output. if the packet data was allocated by libavcodec,
943 av_freep(&avpkt->data);
945 if (avctx->coded_frame)
946 avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
947 /* Set duration for final small packet. This can be removed
948 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
951 avpkt->duration = ff_samples_to_time_base(avctx,
956 *got_packet_ptr = (ret > 0);
961 avctx->frame_size = fs_tmp;
964 avctx->frame_number++;
966 if (ret < 0 || !*got_packet_ptr)
967 av_free_packet(avpkt);
969 /* NOTE: if we add any audio encoders which output non-keyframe packets,
970 this needs to be moved to the encoders, but for now we can do it
971 here to simplify things */
972 avpkt->flags |= AV_PKT_FLAG_KEY;
977 #if FF_API_OLD_DECODE_AUDIO
978 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
979 uint8_t *buf, int buf_size,
980 const short *samples)
985 int ret, samples_size, got_packet;
987 av_init_packet(&pkt);
993 avcodec_get_frame_defaults(frame);
995 if (avctx->frame_size) {
996 frame->nb_samples = avctx->frame_size;
998 /* if frame_size is not set, the number of samples must be
999 calculated from the buffer size */
1001 if (!av_get_bits_per_sample(avctx->codec_id)) {
1002 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1003 "support this codec\n");
1004 return AVERROR(EINVAL);
1006 nb_samples = (int64_t)buf_size * 8 /
1007 (av_get_bits_per_sample(avctx->codec_id) *
1009 if (nb_samples >= INT_MAX)
1010 return AVERROR(EINVAL);
1011 frame->nb_samples = nb_samples;
1014 /* it is assumed that the samples buffer is large enough based on the
1015 relevant parameters */
1016 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1018 avctx->sample_fmt, 1);
1019 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1021 samples, samples_size, 1)))
1024 /* fabricate frame pts from sample count.
1025 this is needed because the avcodec_encode_audio() API does not have
1026 a way for the user to provide pts */
1027 frame->pts = ff_samples_to_time_base(avctx,
1028 avctx->internal->sample_count);
1029 avctx->internal->sample_count += frame->nb_samples;
1035 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1036 if (!ret && got_packet && avctx->coded_frame) {
1037 avctx->coded_frame->pts = pkt.pts;
1038 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1040 /* free any side data since we cannot return it */
1041 if (pkt.side_data_elems > 0) {
1043 for (i = 0; i < pkt.side_data_elems; i++)
1044 av_free(pkt.side_data[i].data);
1045 av_freep(&pkt.side_data);
1046 pkt.side_data_elems = 0;
1049 if (frame && frame->extended_data != frame->data)
1050 av_free(frame->extended_data);
1052 return ret ? ret : pkt.size;
1056 #if FF_API_OLD_ENCODE_VIDEO
1057 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1058 const AVFrame *pict)
1061 int ret, got_packet = 0;
1063 if(buf_size < FF_MIN_BUFFER_SIZE){
1064 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1068 av_init_packet(&pkt);
1070 pkt.size = buf_size;
1072 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1073 if (!ret && got_packet && avctx->coded_frame) {
1074 avctx->coded_frame->pts = pkt.pts;
1075 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1078 /* free any side data since we cannot return it */
1079 if (pkt.side_data_elems > 0) {
1081 for (i = 0; i < pkt.side_data_elems; i++)
1082 av_free(pkt.side_data[i].data);
1083 av_freep(&pkt.side_data);
1084 pkt.side_data_elems = 0;
1087 return ret ? ret : pkt.size;
1091 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1093 const AVFrame *frame,
1094 int *got_packet_ptr)
1097 int user_packet = !!avpkt->data;
1099 *got_packet_ptr = 0;
1101 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1102 av_free_packet(avpkt);
1103 av_init_packet(avpkt);
1108 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1109 return AVERROR(EINVAL);
1111 av_assert0(avctx->codec->encode2);
1113 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1115 if (!*got_packet_ptr)
1117 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1118 avpkt->pts = avpkt->dts = frame->pts;
1120 if (!user_packet && avpkt->data) {
1121 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1123 avpkt->data = new_data;
1126 avctx->frame_number++;
1129 if (ret < 0 || !*got_packet_ptr)
1130 av_free_packet(avpkt);
1136 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1137 const AVSubtitle *sub)
1140 if(sub->start_display_time) {
1141 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1144 if(sub->num_rects == 0 || !sub->rects)
1146 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1147 avctx->frame_number++;
1151 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1154 const uint8_t *data;
1157 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1160 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1161 if (!data || size < 4)
1163 flags = bytestream_get_le32(&data);
1165 if (size < 4) /* Required for any of the changes */
1167 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1168 avctx->channels = bytestream_get_le32(&data);
1171 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1174 avctx->channel_layout = bytestream_get_le64(&data);
1179 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1180 avctx->sample_rate = bytestream_get_le32(&data);
1183 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1186 avctx->width = bytestream_get_le32(&data);
1187 avctx->height = bytestream_get_le32(&data);
1188 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1193 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1194 int *got_picture_ptr,
1199 *got_picture_ptr= 0;
1200 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1204 apply_param_change(avctx, avpkt);
1206 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1207 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1208 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1211 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1213 picture->pkt_dts= avpkt->dts;
1214 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1215 picture->width = avctx->width;
1216 picture->height = avctx->height;
1217 picture->format = avctx->pix_fmt;
1220 emms_c(); //needed to avoid an emms_c() call before every return;
1222 if (*got_picture_ptr)
1223 avctx->frame_number++;
1230 #if FF_API_OLD_DECODE_AUDIO
1231 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1232 int *frame_size_ptr,
1236 int ret, got_frame = 0;
1238 if (avctx->get_buffer != avcodec_default_get_buffer) {
1239 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1240 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1241 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1242 "avcodec_decode_audio4()\n");
1243 avctx->get_buffer = avcodec_default_get_buffer;
1246 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1248 if (ret >= 0 && got_frame) {
1250 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1251 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1253 avctx->sample_fmt, 1);
1254 if (*frame_size_ptr < data_size) {
1255 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1256 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1257 return AVERROR(EINVAL);
1260 memcpy(samples, frame.extended_data[0], plane_size);
1262 if (planar && avctx->channels > 1) {
1263 uint8_t *out = ((uint8_t *)samples) + plane_size;
1264 for (ch = 1; ch < avctx->channels; ch++) {
1265 memcpy(out, frame.extended_data[ch], plane_size);
1269 *frame_size_ptr = data_size;
1271 *frame_size_ptr = 0;
1277 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1288 if (!avpkt->data && avpkt->size) {
1289 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1290 return AVERROR(EINVAL);
1293 apply_param_change(avctx, avpkt);
1295 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1296 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1297 if (ret >= 0 && *got_frame_ptr) {
1298 avctx->frame_number++;
1299 frame->pkt_dts = avpkt->dts;
1300 if (frame->format == AV_SAMPLE_FMT_NONE)
1301 frame->format = avctx->sample_fmt;
1307 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1315 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1317 avctx->frame_number++;
1321 void avsubtitle_free(AVSubtitle *sub)
1325 for (i = 0; i < sub->num_rects; i++)
1327 av_freep(&sub->rects[i]->pict.data[0]);
1328 av_freep(&sub->rects[i]->pict.data[1]);
1329 av_freep(&sub->rects[i]->pict.data[2]);
1330 av_freep(&sub->rects[i]->pict.data[3]);
1331 av_freep(&sub->rects[i]->text);
1332 av_freep(&sub->rects[i]->ass);
1333 av_freep(&sub->rects[i]);
1336 av_freep(&sub->rects);
1338 memset(sub, 0, sizeof(AVSubtitle));
1341 av_cold int avcodec_close(AVCodecContext *avctx)
1343 /* If there is a user-supplied mutex locking routine, call it. */
1344 if (ff_lockmgr_cb) {
1345 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1349 entangled_thread_counter++;
1350 if(entangled_thread_counter != 1){
1351 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1352 entangled_thread_counter--;
1356 if (avcodec_is_open(avctx)) {
1357 if (HAVE_THREADS && avctx->thread_opaque)
1358 ff_thread_free(avctx);
1359 if (avctx->codec && avctx->codec->close)
1360 avctx->codec->close(avctx);
1361 avcodec_default_free_buffers(avctx);
1362 avctx->coded_frame = NULL;
1363 av_freep(&avctx->internal);
1366 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1367 av_opt_free(avctx->priv_data);
1369 av_freep(&avctx->priv_data);
1370 if (av_codec_is_encoder(avctx->codec))
1371 av_freep(&avctx->extradata);
1372 avctx->codec = NULL;
1373 avctx->active_thread_type = 0;
1374 entangled_thread_counter--;
1376 /* Release any user-supplied mutex. */
1377 if (ff_lockmgr_cb) {
1378 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1383 AVCodec *avcodec_find_encoder(enum CodecID id)
1385 AVCodec *p, *experimental=NULL;
1388 if (av_codec_is_encoder(p) && p->id == id) {
1389 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1396 return experimental;
1399 AVCodec *avcodec_find_encoder_by_name(const char *name)
1406 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1413 AVCodec *avcodec_find_decoder(enum CodecID id)
1418 if (av_codec_is_decoder(p) && p->id == id)
1425 AVCodec *avcodec_find_decoder_by_name(const char *name)
1432 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1439 static int get_bit_rate(AVCodecContext *ctx)
1442 int bits_per_sample;
1444 switch(ctx->codec_type) {
1445 case AVMEDIA_TYPE_VIDEO:
1446 case AVMEDIA_TYPE_DATA:
1447 case AVMEDIA_TYPE_SUBTITLE:
1448 case AVMEDIA_TYPE_ATTACHMENT:
1449 bit_rate = ctx->bit_rate;
1451 case AVMEDIA_TYPE_AUDIO:
1452 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1453 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1462 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1464 int i, len, ret = 0;
1466 for (i = 0; i < 4; i++) {
1467 len = snprintf(buf, buf_size,
1468 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1470 buf_size = buf_size > len ? buf_size - len : 0;
1477 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1479 const char *codec_name;
1480 const char *profile = NULL;
1484 AVRational display_aspect_ratio;
1487 p = avcodec_find_encoder(enc->codec_id);
1489 p = avcodec_find_decoder(enc->codec_id);
1492 codec_name = p->name;
1493 profile = av_get_profile_name(p, enc->profile);
1494 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1495 /* fake mpeg2 transport stream codec (currently not
1497 codec_name = "mpeg2ts";
1498 } else if (enc->codec_name[0] != '\0') {
1499 codec_name = enc->codec_name;
1501 /* output avi tags */
1503 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1504 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1508 switch(enc->codec_type) {
1509 case AVMEDIA_TYPE_VIDEO:
1510 snprintf(buf, buf_size,
1512 codec_name, enc->mb_decision ? " (hq)" : "");
1514 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1516 if (enc->pix_fmt != PIX_FMT_NONE) {
1517 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1519 av_get_pix_fmt_name(enc->pix_fmt));
1522 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1524 enc->width, enc->height);
1525 if (enc->sample_aspect_ratio.num) {
1526 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1527 enc->width*enc->sample_aspect_ratio.num,
1528 enc->height*enc->sample_aspect_ratio.den,
1530 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1531 " [PAR %d:%d DAR %d:%d]",
1532 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1533 display_aspect_ratio.num, display_aspect_ratio.den);
1535 if(av_log_get_level() >= AV_LOG_DEBUG){
1536 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1537 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1539 enc->time_base.num/g, enc->time_base.den/g);
1543 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1544 ", q=%d-%d", enc->qmin, enc->qmax);
1547 case AVMEDIA_TYPE_AUDIO:
1548 snprintf(buf, buf_size,
1552 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1554 if (enc->sample_rate) {
1555 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1556 ", %d Hz", enc->sample_rate);
1558 av_strlcat(buf, ", ", buf_size);
1559 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1560 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1561 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1562 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1565 case AVMEDIA_TYPE_DATA:
1566 snprintf(buf, buf_size, "Data: %s", codec_name);
1568 case AVMEDIA_TYPE_SUBTITLE:
1569 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1571 case AVMEDIA_TYPE_ATTACHMENT:
1572 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1575 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1579 if (enc->flags & CODEC_FLAG_PASS1)
1580 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1582 if (enc->flags & CODEC_FLAG_PASS2)
1583 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1586 bitrate = get_bit_rate(enc);
1588 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1589 ", %d kb/s", bitrate / 1000);
1593 const char *av_get_profile_name(const AVCodec *codec, int profile)
1596 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1599 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1600 if (p->profile == profile)
1606 unsigned avcodec_version( void )
1608 return LIBAVCODEC_VERSION_INT;
1611 const char *avcodec_configuration(void)
1613 return LIBAV_CONFIGURATION;
1616 const char *avcodec_license(void)
1618 #define LICENSE_PREFIX "libavcodec license: "
1619 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1622 void avcodec_flush_buffers(AVCodecContext *avctx)
1624 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1625 ff_thread_flush(avctx);
1626 else if(avctx->codec->flush)
1627 avctx->codec->flush(avctx);
1630 static void video_free_buffers(AVCodecContext *s)
1632 AVCodecInternal *avci = s->internal;
1638 if (avci->buffer_count)
1639 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1640 avci->buffer_count);
1641 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1642 InternalBuffer *buf = &avci->buffer[i];
1644 av_freep(&buf->base[j]);
1648 av_freep(&avci->buffer);
1650 avci->buffer_count=0;
1653 static void audio_free_buffers(AVCodecContext *avctx)
1655 AVCodecInternal *avci = avctx->internal;
1656 InternalBuffer *buf;
1662 if (buf->extended_data) {
1663 av_free(buf->extended_data[0]);
1664 if (buf->extended_data != buf->data)
1665 av_free(buf->extended_data);
1667 av_freep(&avci->buffer);
1670 void avcodec_default_free_buffers(AVCodecContext *avctx)
1672 switch (avctx->codec_type) {
1673 case AVMEDIA_TYPE_VIDEO:
1674 video_free_buffers(avctx);
1676 case AVMEDIA_TYPE_AUDIO:
1677 audio_free_buffers(avctx);
1684 int av_get_exact_bits_per_sample(enum CodecID codec_id)
1687 case CODEC_ID_ADPCM_CT:
1688 case CODEC_ID_ADPCM_IMA_APC:
1689 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1690 case CODEC_ID_ADPCM_IMA_WS:
1691 case CODEC_ID_ADPCM_G722:
1692 case CODEC_ID_ADPCM_YAMAHA:
1694 case CODEC_ID_PCM_ALAW:
1695 case CODEC_ID_PCM_MULAW:
1696 case CODEC_ID_PCM_S8:
1697 case CODEC_ID_PCM_U8:
1698 case CODEC_ID_PCM_ZORK:
1700 case CODEC_ID_PCM_S16BE:
1701 case CODEC_ID_PCM_S16LE:
1702 case CODEC_ID_PCM_S16LE_PLANAR:
1703 case CODEC_ID_PCM_U16BE:
1704 case CODEC_ID_PCM_U16LE:
1706 case CODEC_ID_PCM_S24DAUD:
1707 case CODEC_ID_PCM_S24BE:
1708 case CODEC_ID_PCM_S24LE:
1709 case CODEC_ID_PCM_U24BE:
1710 case CODEC_ID_PCM_U24LE:
1712 case CODEC_ID_PCM_S32BE:
1713 case CODEC_ID_PCM_S32LE:
1714 case CODEC_ID_PCM_U32BE:
1715 case CODEC_ID_PCM_U32LE:
1716 case CODEC_ID_PCM_F32BE:
1717 case CODEC_ID_PCM_F32LE:
1719 case CODEC_ID_PCM_F64BE:
1720 case CODEC_ID_PCM_F64LE:
1727 int av_get_bits_per_sample(enum CodecID codec_id)
1730 case CODEC_ID_ADPCM_SBPRO_2:
1732 case CODEC_ID_ADPCM_SBPRO_3:
1734 case CODEC_ID_ADPCM_SBPRO_4:
1735 case CODEC_ID_ADPCM_IMA_WAV:
1736 case CODEC_ID_ADPCM_IMA_QT:
1737 case CODEC_ID_ADPCM_SWF:
1738 case CODEC_ID_ADPCM_MS:
1741 return av_get_exact_bits_per_sample(codec_id);
1745 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1747 int id, sr, ch, ba, tag, bps;
1749 id = avctx->codec_id;
1750 sr = avctx->sample_rate;
1751 ch = avctx->channels;
1752 ba = avctx->block_align;
1753 tag = avctx->codec_tag;
1754 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1756 /* codecs with an exact constant bits per sample */
1757 if (bps > 0 && ch > 0 && frame_bytes > 0)
1758 return (frame_bytes * 8) / (bps * ch);
1759 bps = avctx->bits_per_coded_sample;
1761 /* codecs with a fixed packet duration */
1763 case CODEC_ID_ADPCM_ADX: return 32;
1764 case CODEC_ID_ADPCM_IMA_QT: return 64;
1765 case CODEC_ID_ADPCM_EA_XAS: return 128;
1766 case CODEC_ID_AMR_NB:
1768 case CODEC_ID_QCELP:
1769 case CODEC_ID_RA_144:
1770 case CODEC_ID_RA_288: return 160;
1771 case CODEC_ID_IMC: return 256;
1772 case CODEC_ID_AMR_WB:
1773 case CODEC_ID_GSM_MS: return 320;
1774 case CODEC_ID_MP1: return 384;
1775 case CODEC_ID_ATRAC1: return 512;
1776 case CODEC_ID_ATRAC3: return 1024;
1778 case CODEC_ID_MUSEPACK7: return 1152;
1779 case CODEC_ID_AC3: return 1536;
1783 /* calc from sample rate */
1784 if (id == CODEC_ID_TTA)
1785 return 256 * sr / 245;
1788 /* calc from sample rate and channels */
1789 if (id == CODEC_ID_BINKAUDIO_DCT)
1790 return (480 << (sr / 22050)) / ch;
1795 /* calc from block_align */
1796 if (id == CODEC_ID_SIPR) {
1798 case 20: return 160;
1799 case 19: return 144;
1800 case 29: return 288;
1801 case 37: return 480;
1806 if (frame_bytes > 0) {
1807 /* calc from frame_bytes only */
1808 if (id == CODEC_ID_TRUESPEECH)
1809 return 240 * (frame_bytes / 32);
1810 if (id == CODEC_ID_NELLYMOSER)
1811 return 256 * (frame_bytes / 64);
1814 /* calc from frame_bytes and bits_per_coded_sample */
1815 if (id == CODEC_ID_ADPCM_G726)
1816 return frame_bytes * 8 / bps;
1820 /* calc from frame_bytes and channels */
1822 case CODEC_ID_ADPCM_4XM:
1823 case CODEC_ID_ADPCM_IMA_ISS:
1824 return (frame_bytes - 4 * ch) * 2 / ch;
1825 case CODEC_ID_ADPCM_IMA_SMJPEG:
1826 return (frame_bytes - 4) * 2 / ch;
1827 case CODEC_ID_ADPCM_IMA_AMV:
1828 return (frame_bytes - 8) * 2 / ch;
1829 case CODEC_ID_ADPCM_XA:
1830 return (frame_bytes / 128) * 224 / ch;
1831 case CODEC_ID_INTERPLAY_DPCM:
1832 return (frame_bytes - 6 - ch) / ch;
1833 case CODEC_ID_ROQ_DPCM:
1834 return (frame_bytes - 8) / ch;
1835 case CODEC_ID_XAN_DPCM:
1836 return (frame_bytes - 2 * ch) / ch;
1837 case CODEC_ID_MACE3:
1838 return 3 * frame_bytes / ch;
1839 case CODEC_ID_MACE6:
1840 return 6 * frame_bytes / ch;
1841 case CODEC_ID_PCM_LXF:
1842 return 2 * (frame_bytes / (5 * ch));
1846 /* calc from frame_bytes, channels, and codec_tag */
1847 if (id == CODEC_ID_SOL_DPCM) {
1849 return frame_bytes / ch;
1851 return frame_bytes * 2 / ch;
1856 /* calc from frame_bytes, channels, and block_align */
1857 int blocks = frame_bytes / ba;
1858 switch (avctx->codec_id) {
1859 case CODEC_ID_ADPCM_IMA_WAV:
1860 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1861 case CODEC_ID_ADPCM_IMA_DK3:
1862 return blocks * (((ba - 16) * 8 / 3) / ch);
1863 case CODEC_ID_ADPCM_IMA_DK4:
1864 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1865 case CODEC_ID_ADPCM_MS:
1866 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1871 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1872 switch (avctx->codec_id) {
1873 case CODEC_ID_PCM_DVD:
1874 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1875 case CODEC_ID_PCM_BLURAY:
1876 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1877 case CODEC_ID_S302M:
1878 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1888 int ff_thread_init(AVCodecContext *s){
1893 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1907 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1909 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1913 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1915 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1916 "version to the newest one from Git. If the problem still "
1917 "occurs, it means that your file has a feature which has not "
1918 "been implemented.\n", feature);
1920 av_log_ask_for_sample(avc, NULL);
1923 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1925 va_list argument_list;
1927 va_start(argument_list, msg);
1930 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1931 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1932 "of this file to ftp://upload.libav.org/incoming/ "
1933 "and contact the libav-devel mailing list.\n");
1935 va_end(argument_list);
1938 static AVHWAccel *first_hwaccel = NULL;
1940 void av_register_hwaccel(AVHWAccel *hwaccel)
1942 AVHWAccel **p = &first_hwaccel;
1946 hwaccel->next = NULL;
1949 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1951 return hwaccel ? hwaccel->next : first_hwaccel;
1954 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1956 AVHWAccel *hwaccel=NULL;
1958 while((hwaccel= av_hwaccel_next(hwaccel))){
1959 if ( hwaccel->id == codec_id
1960 && hwaccel->pix_fmt == pix_fmt)
1966 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1968 if (ff_lockmgr_cb) {
1969 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1971 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1977 if (ff_lockmgr_cb) {
1978 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1980 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1986 int avpriv_lock_avformat(void)
1988 if (ff_lockmgr_cb) {
1989 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1995 int avpriv_unlock_avformat(void)
1997 if (ff_lockmgr_cb) {
1998 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2004 unsigned int avpriv_toupper4(unsigned int x)
2006 return toupper( x &0xFF)
2007 + (toupper((x>>8 )&0xFF)<<8 )
2008 + (toupper((x>>16)&0xFF)<<16)
2009 + (toupper((x>>24)&0xFF)<<24);
2014 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2017 return avctx->get_buffer(avctx, f);
2020 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2022 f->owner->release_buffer(f->owner, f);
2025 void ff_thread_finish_setup(AVCodecContext *avctx)
2029 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2033 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2039 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2041 if (codec_id <= CODEC_ID_NONE)
2042 return AVMEDIA_TYPE_UNKNOWN;
2043 else if (codec_id < CODEC_ID_FIRST_AUDIO)
2044 return AVMEDIA_TYPE_VIDEO;
2045 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2046 return AVMEDIA_TYPE_AUDIO;
2047 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2048 return AVMEDIA_TYPE_SUBTITLE;
2050 return AVMEDIA_TYPE_UNKNOWN;
2053 int avcodec_is_open(AVCodecContext *s)
2055 return !!s->internal;