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(const AVCodec *c)
101 if(c) return c->next;
102 else return first_avcodec;
105 static void avcodec_init(void)
107 static int initialized = 0;
109 if (initialized != 0)
113 ff_dsputil_static_init();
116 int av_codec_is_encoder(const AVCodec *codec)
118 return codec && (codec->encode || codec->encode2);
121 int av_codec_is_decoder(const AVCodec *codec)
123 return codec && codec->decode;
126 void avcodec_register(AVCodec *codec)
131 while (*p != NULL) p = &(*p)->next;
135 if (codec->init_static_data)
136 codec->init_static_data(codec);
139 unsigned avcodec_get_edge_width(void)
144 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
145 s->coded_width = width;
146 s->coded_height= height;
151 #define INTERNAL_BUFFER_SIZE (32+1)
153 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
154 int linesize_align[AV_NUM_DATA_POINTERS])
161 case PIX_FMT_YUV420P:
162 case PIX_FMT_YUYV422:
163 case PIX_FMT_UYVY422:
164 case PIX_FMT_YUV422P:
165 case PIX_FMT_YUV440P:
166 case PIX_FMT_YUV444P:
169 case PIX_FMT_GRAY16BE:
170 case PIX_FMT_GRAY16LE:
171 case PIX_FMT_YUVJ420P:
172 case PIX_FMT_YUVJ422P:
173 case PIX_FMT_YUVJ440P:
174 case PIX_FMT_YUVJ444P:
175 case PIX_FMT_YUVA420P:
176 case PIX_FMT_YUV420P9LE:
177 case PIX_FMT_YUV420P9BE:
178 case PIX_FMT_YUV420P10LE:
179 case PIX_FMT_YUV420P10BE:
180 case PIX_FMT_YUV422P9LE:
181 case PIX_FMT_YUV422P9BE:
182 case PIX_FMT_YUV422P10LE:
183 case PIX_FMT_YUV422P10BE:
184 case PIX_FMT_YUV444P9LE:
185 case PIX_FMT_YUV444P9BE:
186 case PIX_FMT_YUV444P10LE:
187 case PIX_FMT_YUV444P10BE:
188 case PIX_FMT_GBRP9LE:
189 case PIX_FMT_GBRP9BE:
190 case PIX_FMT_GBRP10LE:
191 case PIX_FMT_GBRP10BE:
192 w_align = 16; //FIXME assume 16 pixel per macroblock
193 h_align = 16 * 2; // interlaced needs 2 macroblocks height
195 case PIX_FMT_YUV411P:
196 case PIX_FMT_UYYVYY411:
200 case PIX_FMT_YUV410P:
201 if(s->codec_id == AV_CODEC_ID_SVQ1){
206 if(s->codec_id == AV_CODEC_ID_RPZA){
213 if(s->codec_id == AV_CODEC_ID_SMC){
219 if((s->codec_id == AV_CODEC_ID_MSZH) || (s->codec_id == AV_CODEC_ID_ZLIB)){
230 *width = FFALIGN(*width , w_align);
231 *height= FFALIGN(*height, h_align);
232 if (s->codec_id == AV_CODEC_ID_H264)
233 *height+=2; // some of the optimized chroma MC reads one line too much
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 frame->sample_rate = avctx->sample_rate;
361 frame->format = avctx->sample_fmt;
362 frame->channel_layout = avctx->channel_layout;
364 if (avctx->debug & FF_DEBUG_BUFFERS)
365 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
366 "internal audio buffer used\n", frame);
371 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
377 AVCodecInternal *avci = s->internal;
379 if(pic->data[0]!=NULL) {
380 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
383 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
384 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
388 if(av_image_check_size(w, h, 0, s))
392 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
393 sizeof(InternalBuffer));
396 buf = &avci->buffer[avci->buffer_count];
398 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
399 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
400 av_freep(&buf->base[i]);
406 int h_chroma_shift, v_chroma_shift;
411 int stride_align[AV_NUM_DATA_POINTERS];
412 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
414 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
416 avcodec_align_dimensions2(s, &w, &h, stride_align);
418 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
424 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
425 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
426 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
427 // increase alignment of w for next try (rhs gives the lowest bit set in w)
432 unaligned |= picture.linesize[i] % stride_align[i];
436 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
440 for (i=0; i<3 && picture.data[i+1]; i++)
441 size[i] = picture.data[i+1] - picture.data[i];
442 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
444 memset(buf->base, 0, sizeof(buf->base));
445 memset(buf->data, 0, sizeof(buf->data));
447 for(i=0; i<4 && size[i]; i++){
448 const int h_shift= i==0 ? 0 : h_chroma_shift;
449 const int v_shift= i==0 ? 0 : v_chroma_shift;
451 buf->linesize[i]= picture.linesize[i];
453 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
454 if(buf->base[i]==NULL) return -1;
455 memset(buf->base[i], 128, size[i]);
457 // no edge if EDGE EMU or not planar YUV
458 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
459 buf->data[i] = buf->base[i];
461 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
463 for (; i < AV_NUM_DATA_POINTERS; i++) {
464 buf->base[i] = buf->data[i] = NULL;
465 buf->linesize[i] = 0;
467 if(size[1] && !size[2])
468 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
469 buf->width = s->width;
470 buf->height = s->height;
471 buf->pix_fmt= s->pix_fmt;
473 pic->type= FF_BUFFER_TYPE_INTERNAL;
475 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
476 pic->base[i]= buf->base[i];
477 pic->data[i]= buf->data[i];
478 pic->linesize[i]= buf->linesize[i];
480 pic->extended_data = pic->data;
481 avci->buffer_count++;
482 pic->width = buf->width;
483 pic->height = buf->height;
484 pic->format = buf->pix_fmt;
485 pic->sample_aspect_ratio = s->sample_aspect_ratio;
487 if(s->pkt) pic->pkt_pts= s->pkt->pts;
488 else pic->pkt_pts= AV_NOPTS_VALUE;
489 pic->reordered_opaque= s->reordered_opaque;
491 if(s->debug&FF_DEBUG_BUFFERS)
492 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
493 "buffers used\n", pic, avci->buffer_count);
498 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
500 switch (avctx->codec_type) {
501 case AVMEDIA_TYPE_VIDEO:
502 return video_get_buffer(avctx, frame);
503 case AVMEDIA_TYPE_AUDIO:
504 return audio_get_buffer(avctx, frame);
510 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
512 InternalBuffer *buf, *last;
513 AVCodecInternal *avci = s->internal;
515 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
517 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
518 assert(avci->buffer_count);
521 buf = NULL; /* avoids warning */
522 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
523 buf = &avci->buffer[i];
524 if (buf->data[0] == pic->data[0])
527 assert(i < avci->buffer_count);
528 avci->buffer_count--;
529 last = &avci->buffer[avci->buffer_count];
532 FFSWAP(InternalBuffer, *buf, *last);
535 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
537 // pic->base[i]=NULL;
539 //printf("R%X\n", pic->opaque);
541 if(s->debug&FF_DEBUG_BUFFERS)
542 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
543 "buffers used\n", pic, avci->buffer_count);
546 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
550 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
552 /* If no picture return a new buffer */
553 if(pic->data[0] == NULL) {
554 /* We will copy from buffer, so must be readable */
555 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
556 return s->get_buffer(s, pic);
559 assert(s->pix_fmt == pic->format);
561 /* If internal buffer type return the same buffer */
562 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
563 if(s->pkt) pic->pkt_pts= s->pkt->pts;
564 else pic->pkt_pts= AV_NOPTS_VALUE;
565 pic->reordered_opaque= s->reordered_opaque;
570 * Not internal type and reget_buffer not overridden, emulate cr buffer
573 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
574 pic->data[i] = pic->base[i] = NULL;
576 /* Allocate new frame */
577 if (s->get_buffer(s, pic))
579 /* Copy image data from old buffer to new buffer */
580 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
582 s->release_buffer(s, &temp_pic); // Release old frame
586 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
589 for(i=0; i<count; i++){
590 int r= func(c, (char*)arg + i*size);
596 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
599 for(i=0; i<count; i++){
600 int r= func(c, arg, i, 0);
606 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
607 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
612 void avcodec_get_frame_defaults(AVFrame *pic){
613 memset(pic, 0, sizeof(AVFrame));
615 pic->pts= AV_NOPTS_VALUE;
617 pic->sample_aspect_ratio = (AVRational){0, 1};
618 pic->format = -1; /* unknown */
621 AVFrame *avcodec_alloc_frame(void){
622 AVFrame *pic= av_malloc(sizeof(AVFrame));
624 if(pic==NULL) return NULL;
626 avcodec_get_frame_defaults(pic);
631 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
634 AVDictionary *tmp = NULL;
636 if (avcodec_is_open(avctx))
639 if ((!codec && !avctx->codec)) {
640 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
641 return AVERROR(EINVAL);
643 if ((codec && avctx->codec && codec != avctx->codec)) {
644 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
645 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
646 return AVERROR(EINVAL);
649 codec = avctx->codec;
651 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
652 return AVERROR(EINVAL);
655 av_dict_copy(&tmp, *options, 0);
657 /* If there is a user-supplied mutex locking routine, call it. */
659 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
663 entangled_thread_counter++;
664 if(entangled_thread_counter != 1){
665 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
670 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
671 if (!avctx->internal) {
672 ret = AVERROR(ENOMEM);
676 if (codec->priv_data_size > 0) {
677 if(!avctx->priv_data){
678 avctx->priv_data = av_mallocz(codec->priv_data_size);
679 if (!avctx->priv_data) {
680 ret = AVERROR(ENOMEM);
683 if (codec->priv_class) {
684 *(AVClass**)avctx->priv_data= codec->priv_class;
685 av_opt_set_defaults(avctx->priv_data);
688 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
691 avctx->priv_data = NULL;
693 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
696 if(avctx->coded_width && avctx->coded_height)
697 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
698 else if(avctx->width && avctx->height)
699 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
701 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
702 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
703 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
704 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
705 avcodec_set_dimensions(avctx, 0, 0);
708 /* if the decoder init function was already called previously,
709 free the already allocated subtitle_header before overwriting it */
710 if (av_codec_is_decoder(codec))
711 av_freep(&avctx->subtitle_header);
713 #define SANE_NB_CHANNELS 128U
714 if (avctx->channels > SANE_NB_CHANNELS) {
715 ret = AVERROR(EINVAL);
719 avctx->codec = codec;
720 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
721 avctx->codec_id == AV_CODEC_ID_NONE) {
722 avctx->codec_type = codec->type;
723 avctx->codec_id = codec->id;
725 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
726 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
727 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
728 ret = AVERROR(EINVAL);
731 avctx->frame_number = 0;
733 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
734 (!avctx->time_base.num || !avctx->time_base.den)) {
735 avctx->time_base.num = 1;
736 avctx->time_base.den = avctx->sample_rate;
739 if (HAVE_THREADS && !avctx->thread_opaque) {
740 ret = ff_thread_init(avctx);
745 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
746 avctx->thread_count = 1;
748 if (av_codec_is_encoder(avctx->codec)) {
750 if (avctx->codec->sample_fmts) {
751 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
752 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
754 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
755 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
756 ret = AVERROR(EINVAL);
760 if (avctx->codec->pix_fmts) {
761 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
762 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
764 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
765 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
766 ret = AVERROR(EINVAL);
770 if (avctx->codec->supported_samplerates) {
771 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
772 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
774 if (avctx->codec->supported_samplerates[i] == 0) {
775 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
776 ret = AVERROR(EINVAL);
780 if (avctx->codec->channel_layouts) {
781 if (!avctx->channel_layout) {
782 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
784 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
785 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
787 if (avctx->codec->channel_layouts[i] == 0) {
788 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
789 ret = AVERROR(EINVAL);
794 if (avctx->channel_layout && avctx->channels) {
795 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
796 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
797 ret = AVERROR(EINVAL);
800 } else if (avctx->channel_layout) {
801 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
805 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
806 ret = avctx->codec->init(avctx);
812 if (av_codec_is_decoder(avctx->codec)) {
813 /* validate channel layout from the decoder */
814 if (avctx->channel_layout &&
815 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
816 av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
817 avctx->channel_layout = 0;
821 entangled_thread_counter--;
823 /* Release any user-supplied mutex. */
825 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
828 av_dict_free(options);
835 av_freep(&avctx->priv_data);
836 av_freep(&avctx->internal);
841 int ff_alloc_packet(AVPacket *avpkt, int size)
843 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
844 return AVERROR(EINVAL);
847 void *destruct = avpkt->destruct;
849 if (avpkt->size < size)
850 return AVERROR(EINVAL);
852 av_init_packet(avpkt);
853 avpkt->destruct = destruct;
857 return av_new_packet(avpkt, size);
862 * Pad last frame with silence.
864 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
866 AVFrame *frame = NULL;
870 if (!(frame = avcodec_alloc_frame()))
871 return AVERROR(ENOMEM);
874 if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
875 s->frame_size, s->sample_fmt, 0)) < 0)
878 if (!(buf = av_malloc(ret))) {
879 ret = AVERROR(ENOMEM);
883 frame->nb_samples = s->frame_size;
884 if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
887 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
888 src->nb_samples, s->channels, s->sample_fmt)) < 0)
890 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
891 frame->nb_samples - src->nb_samples,
892 s->channels, s->sample_fmt)) < 0)
900 if (frame->extended_data != frame->data)
901 av_freep(&frame->extended_data);
907 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
909 const AVFrame *frame,
913 AVFrame *padded_frame = NULL;
915 int user_packet = !!avpkt->data;
919 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
920 av_free_packet(avpkt);
921 av_init_packet(avpkt);
925 /* ensure that extended_data is properly set */
926 if (frame && !frame->extended_data) {
927 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
928 avctx->channels > AV_NUM_DATA_POINTERS) {
929 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
930 "with more than %d channels, but extended_data is not set.\n",
931 AV_NUM_DATA_POINTERS);
932 return AVERROR(EINVAL);
934 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
937 tmp.extended_data = tmp.data;
941 /* check for valid frame size */
943 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
944 if (frame->nb_samples > avctx->frame_size)
945 return AVERROR(EINVAL);
946 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
947 if (frame->nb_samples < avctx->frame_size &&
948 !avctx->internal->last_audio_frame) {
949 ret = pad_last_frame(avctx, &padded_frame, frame);
953 frame = padded_frame;
954 avctx->internal->last_audio_frame = 1;
957 if (frame->nb_samples != avctx->frame_size)
958 return AVERROR(EINVAL);
962 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
964 if (*got_packet_ptr) {
965 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
966 if (avpkt->pts == AV_NOPTS_VALUE)
967 avpkt->pts = frame->pts;
968 if (!avpkt->duration)
969 avpkt->duration = ff_samples_to_time_base(avctx,
972 avpkt->dts = avpkt->pts;
977 if (!user_packet && avpkt->size) {
978 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
980 avpkt->data = new_data;
983 avctx->frame_number++;
986 if (ret < 0 || !*got_packet_ptr) {
987 av_free_packet(avpkt);
988 av_init_packet(avpkt);
992 /* NOTE: if we add any audio encoders which output non-keyframe packets,
993 this needs to be moved to the encoders, but for now we can do it
994 here to simplify things */
995 avpkt->flags |= AV_PKT_FLAG_KEY;
998 av_freep(&padded_frame->data[0]);
999 if (padded_frame->extended_data != padded_frame->data)
1000 av_freep(&padded_frame->extended_data);
1001 av_freep(&padded_frame);
1007 #if FF_API_OLD_DECODE_AUDIO
1008 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1009 uint8_t *buf, int buf_size,
1010 const short *samples)
1015 int ret, samples_size, got_packet;
1017 av_init_packet(&pkt);
1019 pkt.size = buf_size;
1023 avcodec_get_frame_defaults(frame);
1025 if (avctx->frame_size) {
1026 frame->nb_samples = avctx->frame_size;
1028 /* if frame_size is not set, the number of samples must be
1029 calculated from the buffer size */
1031 if (!av_get_bits_per_sample(avctx->codec_id)) {
1032 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1033 "support this codec\n");
1034 return AVERROR(EINVAL);
1036 nb_samples = (int64_t)buf_size * 8 /
1037 (av_get_bits_per_sample(avctx->codec_id) *
1039 if (nb_samples >= INT_MAX)
1040 return AVERROR(EINVAL);
1041 frame->nb_samples = nb_samples;
1044 /* it is assumed that the samples buffer is large enough based on the
1045 relevant parameters */
1046 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1048 avctx->sample_fmt, 1);
1049 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1051 (const uint8_t *) samples,
1055 /* fabricate frame pts from sample count.
1056 this is needed because the avcodec_encode_audio() API does not have
1057 a way for the user to provide pts */
1058 frame->pts = ff_samples_to_time_base(avctx,
1059 avctx->internal->sample_count);
1060 avctx->internal->sample_count += frame->nb_samples;
1066 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1067 if (!ret && got_packet && avctx->coded_frame) {
1068 avctx->coded_frame->pts = pkt.pts;
1069 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1071 /* free any side data since we cannot return it */
1072 if (pkt.side_data_elems > 0) {
1074 for (i = 0; i < pkt.side_data_elems; i++)
1075 av_free(pkt.side_data[i].data);
1076 av_freep(&pkt.side_data);
1077 pkt.side_data_elems = 0;
1080 if (frame && frame->extended_data != frame->data)
1081 av_free(frame->extended_data);
1083 return ret ? ret : pkt.size;
1087 #if FF_API_OLD_ENCODE_VIDEO
1088 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1089 const AVFrame *pict)
1092 int ret, got_packet = 0;
1094 if(buf_size < FF_MIN_BUFFER_SIZE){
1095 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1099 av_init_packet(&pkt);
1101 pkt.size = buf_size;
1103 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1104 if (!ret && got_packet && avctx->coded_frame) {
1105 avctx->coded_frame->pts = pkt.pts;
1106 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1109 /* free any side data since we cannot return it */
1110 if (pkt.side_data_elems > 0) {
1112 for (i = 0; i < pkt.side_data_elems; i++)
1113 av_free(pkt.side_data[i].data);
1114 av_freep(&pkt.side_data);
1115 pkt.side_data_elems = 0;
1118 return ret ? ret : pkt.size;
1122 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1124 const AVFrame *frame,
1125 int *got_packet_ptr)
1128 int user_packet = !!avpkt->data;
1130 *got_packet_ptr = 0;
1132 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1133 av_free_packet(avpkt);
1134 av_init_packet(avpkt);
1139 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1140 return AVERROR(EINVAL);
1142 av_assert0(avctx->codec->encode2);
1144 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1146 if (!*got_packet_ptr)
1148 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1149 avpkt->pts = avpkt->dts = frame->pts;
1151 if (!user_packet && avpkt->size) {
1152 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1154 avpkt->data = new_data;
1157 avctx->frame_number++;
1160 if (ret < 0 || !*got_packet_ptr)
1161 av_free_packet(avpkt);
1167 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1168 const AVSubtitle *sub)
1171 if(sub->start_display_time) {
1172 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1175 if(sub->num_rects == 0 || !sub->rects)
1177 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1178 avctx->frame_number++;
1182 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1185 const uint8_t *data;
1188 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1191 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1192 if (!data || size < 4)
1194 flags = bytestream_get_le32(&data);
1196 if (size < 4) /* Required for any of the changes */
1198 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1199 avctx->channels = bytestream_get_le32(&data);
1202 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1205 avctx->channel_layout = bytestream_get_le64(&data);
1210 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1211 avctx->sample_rate = bytestream_get_le32(&data);
1214 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1217 avctx->width = bytestream_get_le32(&data);
1218 avctx->height = bytestream_get_le32(&data);
1219 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1224 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1225 int *got_picture_ptr,
1230 *got_picture_ptr= 0;
1231 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1235 apply_param_change(avctx, avpkt);
1237 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1238 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1239 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1242 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1244 picture->pkt_dts= avpkt->dts;
1245 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1246 picture->width = avctx->width;
1247 picture->height = avctx->height;
1248 picture->format = avctx->pix_fmt;
1251 emms_c(); //needed to avoid an emms_c() call before every return;
1253 if (*got_picture_ptr)
1254 avctx->frame_number++;
1261 #if FF_API_OLD_DECODE_AUDIO
1262 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1263 int *frame_size_ptr,
1267 int ret, got_frame = 0;
1269 if (avctx->get_buffer != avcodec_default_get_buffer) {
1270 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1271 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1272 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1273 "avcodec_decode_audio4()\n");
1274 avctx->get_buffer = avcodec_default_get_buffer;
1277 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1279 if (ret >= 0 && got_frame) {
1281 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1282 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1284 avctx->sample_fmt, 1);
1285 if (*frame_size_ptr < data_size) {
1286 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1287 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1288 return AVERROR(EINVAL);
1291 memcpy(samples, frame.extended_data[0], plane_size);
1293 if (planar && avctx->channels > 1) {
1294 uint8_t *out = ((uint8_t *)samples) + plane_size;
1295 for (ch = 1; ch < avctx->channels; ch++) {
1296 memcpy(out, frame.extended_data[ch], plane_size);
1300 *frame_size_ptr = data_size;
1302 *frame_size_ptr = 0;
1308 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1319 if (!avpkt->data && avpkt->size) {
1320 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1321 return AVERROR(EINVAL);
1324 apply_param_change(avctx, avpkt);
1326 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1327 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1328 if (ret >= 0 && *got_frame_ptr) {
1329 avctx->frame_number++;
1330 frame->pkt_dts = avpkt->dts;
1331 if (frame->format == AV_SAMPLE_FMT_NONE)
1332 frame->format = avctx->sample_fmt;
1338 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1346 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1348 avctx->frame_number++;
1352 void avsubtitle_free(AVSubtitle *sub)
1356 for (i = 0; i < sub->num_rects; i++)
1358 av_freep(&sub->rects[i]->pict.data[0]);
1359 av_freep(&sub->rects[i]->pict.data[1]);
1360 av_freep(&sub->rects[i]->pict.data[2]);
1361 av_freep(&sub->rects[i]->pict.data[3]);
1362 av_freep(&sub->rects[i]->text);
1363 av_freep(&sub->rects[i]->ass);
1364 av_freep(&sub->rects[i]);
1367 av_freep(&sub->rects);
1369 memset(sub, 0, sizeof(AVSubtitle));
1372 av_cold int avcodec_close(AVCodecContext *avctx)
1374 /* If there is a user-supplied mutex locking routine, call it. */
1375 if (ff_lockmgr_cb) {
1376 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1380 entangled_thread_counter++;
1381 if(entangled_thread_counter != 1){
1382 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1383 entangled_thread_counter--;
1387 if (avcodec_is_open(avctx)) {
1388 if (HAVE_THREADS && avctx->thread_opaque)
1389 ff_thread_free(avctx);
1390 if (avctx->codec && avctx->codec->close)
1391 avctx->codec->close(avctx);
1392 avcodec_default_free_buffers(avctx);
1393 avctx->coded_frame = NULL;
1394 av_freep(&avctx->internal);
1397 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1398 av_opt_free(avctx->priv_data);
1400 av_freep(&avctx->priv_data);
1401 if (av_codec_is_encoder(avctx->codec))
1402 av_freep(&avctx->extradata);
1403 avctx->codec = NULL;
1404 avctx->active_thread_type = 0;
1405 entangled_thread_counter--;
1407 /* Release any user-supplied mutex. */
1408 if (ff_lockmgr_cb) {
1409 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1414 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1416 AVCodec *p, *experimental=NULL;
1419 if (av_codec_is_encoder(p) && p->id == id) {
1420 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1427 return experimental;
1430 AVCodec *avcodec_find_encoder_by_name(const char *name)
1437 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1444 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1449 if (av_codec_is_decoder(p) && p->id == id)
1456 AVCodec *avcodec_find_decoder_by_name(const char *name)
1463 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1470 static int get_bit_rate(AVCodecContext *ctx)
1473 int bits_per_sample;
1475 switch(ctx->codec_type) {
1476 case AVMEDIA_TYPE_VIDEO:
1477 case AVMEDIA_TYPE_DATA:
1478 case AVMEDIA_TYPE_SUBTITLE:
1479 case AVMEDIA_TYPE_ATTACHMENT:
1480 bit_rate = ctx->bit_rate;
1482 case AVMEDIA_TYPE_AUDIO:
1483 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1484 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1493 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1495 int i, len, ret = 0;
1497 for (i = 0; i < 4; i++) {
1498 len = snprintf(buf, buf_size,
1499 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1501 buf_size = buf_size > len ? buf_size - len : 0;
1508 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1510 const char *codec_name;
1511 const char *profile = NULL;
1515 AVRational display_aspect_ratio;
1520 p = avcodec_find_encoder(enc->codec_id);
1522 p = avcodec_find_decoder(enc->codec_id);
1525 codec_name = p->name;
1526 profile = av_get_profile_name(p, enc->profile);
1527 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1528 /* fake mpeg2 transport stream codec (currently not
1530 codec_name = "mpeg2ts";
1531 } else if (enc->codec_name[0] != '\0') {
1532 codec_name = enc->codec_name;
1534 /* output avi tags */
1536 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1537 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1541 switch(enc->codec_type) {
1542 case AVMEDIA_TYPE_VIDEO:
1543 snprintf(buf, buf_size,
1545 codec_name, enc->mb_decision ? " (hq)" : "");
1547 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1549 if (enc->pix_fmt != PIX_FMT_NONE) {
1550 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1552 av_get_pix_fmt_name(enc->pix_fmt));
1555 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1557 enc->width, enc->height);
1558 if (enc->sample_aspect_ratio.num) {
1559 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1560 enc->width*enc->sample_aspect_ratio.num,
1561 enc->height*enc->sample_aspect_ratio.den,
1563 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1564 " [PAR %d:%d DAR %d:%d]",
1565 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1566 display_aspect_ratio.num, display_aspect_ratio.den);
1568 if(av_log_get_level() >= AV_LOG_DEBUG){
1569 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1570 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1572 enc->time_base.num/g, enc->time_base.den/g);
1576 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1577 ", q=%d-%d", enc->qmin, enc->qmax);
1580 case AVMEDIA_TYPE_AUDIO:
1581 snprintf(buf, buf_size,
1585 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1587 if (enc->sample_rate) {
1588 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1589 ", %d Hz", enc->sample_rate);
1591 av_strlcat(buf, ", ", buf_size);
1592 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1593 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1594 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1595 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1598 case AVMEDIA_TYPE_DATA:
1599 snprintf(buf, buf_size, "Data: %s", codec_name);
1601 case AVMEDIA_TYPE_SUBTITLE:
1602 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1604 case AVMEDIA_TYPE_ATTACHMENT:
1605 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1608 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1612 if (enc->flags & CODEC_FLAG_PASS1)
1613 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1615 if (enc->flags & CODEC_FLAG_PASS2)
1616 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1619 bitrate = get_bit_rate(enc);
1621 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1622 ", %d kb/s", bitrate / 1000);
1626 const char *av_get_profile_name(const AVCodec *codec, int profile)
1629 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1632 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1633 if (p->profile == profile)
1639 unsigned avcodec_version( void )
1641 return LIBAVCODEC_VERSION_INT;
1644 const char *avcodec_configuration(void)
1646 return LIBAV_CONFIGURATION;
1649 const char *avcodec_license(void)
1651 #define LICENSE_PREFIX "libavcodec license: "
1652 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1655 void avcodec_flush_buffers(AVCodecContext *avctx)
1657 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1658 ff_thread_flush(avctx);
1659 else if(avctx->codec->flush)
1660 avctx->codec->flush(avctx);
1663 static void video_free_buffers(AVCodecContext *s)
1665 AVCodecInternal *avci = s->internal;
1671 if (avci->buffer_count)
1672 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1673 avci->buffer_count);
1674 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1675 InternalBuffer *buf = &avci->buffer[i];
1677 av_freep(&buf->base[j]);
1681 av_freep(&avci->buffer);
1683 avci->buffer_count=0;
1686 static void audio_free_buffers(AVCodecContext *avctx)
1688 AVCodecInternal *avci = avctx->internal;
1689 InternalBuffer *buf;
1695 if (buf->extended_data) {
1696 av_free(buf->extended_data[0]);
1697 if (buf->extended_data != buf->data)
1698 av_free(buf->extended_data);
1700 av_freep(&avci->buffer);
1703 void avcodec_default_free_buffers(AVCodecContext *avctx)
1705 switch (avctx->codec_type) {
1706 case AVMEDIA_TYPE_VIDEO:
1707 video_free_buffers(avctx);
1709 case AVMEDIA_TYPE_AUDIO:
1710 audio_free_buffers(avctx);
1717 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1720 case AV_CODEC_ID_ADPCM_CT:
1721 case AV_CODEC_ID_ADPCM_IMA_APC:
1722 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1723 case AV_CODEC_ID_ADPCM_IMA_WS:
1724 case AV_CODEC_ID_ADPCM_G722:
1725 case AV_CODEC_ID_ADPCM_YAMAHA:
1727 case AV_CODEC_ID_PCM_ALAW:
1728 case AV_CODEC_ID_PCM_MULAW:
1729 case AV_CODEC_ID_PCM_S8:
1730 case AV_CODEC_ID_PCM_U8:
1731 case AV_CODEC_ID_PCM_ZORK:
1733 case AV_CODEC_ID_PCM_S16BE:
1734 case AV_CODEC_ID_PCM_S16LE:
1735 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1736 case AV_CODEC_ID_PCM_U16BE:
1737 case AV_CODEC_ID_PCM_U16LE:
1739 case AV_CODEC_ID_PCM_S24DAUD:
1740 case AV_CODEC_ID_PCM_S24BE:
1741 case AV_CODEC_ID_PCM_S24LE:
1742 case AV_CODEC_ID_PCM_U24BE:
1743 case AV_CODEC_ID_PCM_U24LE:
1745 case AV_CODEC_ID_PCM_S32BE:
1746 case AV_CODEC_ID_PCM_S32LE:
1747 case AV_CODEC_ID_PCM_U32BE:
1748 case AV_CODEC_ID_PCM_U32LE:
1749 case AV_CODEC_ID_PCM_F32BE:
1750 case AV_CODEC_ID_PCM_F32LE:
1752 case AV_CODEC_ID_PCM_F64BE:
1753 case AV_CODEC_ID_PCM_F64LE:
1760 int av_get_bits_per_sample(enum AVCodecID codec_id)
1763 case AV_CODEC_ID_ADPCM_SBPRO_2:
1765 case AV_CODEC_ID_ADPCM_SBPRO_3:
1767 case AV_CODEC_ID_ADPCM_SBPRO_4:
1768 case AV_CODEC_ID_ADPCM_IMA_WAV:
1769 case AV_CODEC_ID_ADPCM_IMA_QT:
1770 case AV_CODEC_ID_ADPCM_SWF:
1771 case AV_CODEC_ID_ADPCM_MS:
1774 return av_get_exact_bits_per_sample(codec_id);
1778 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1780 int id, sr, ch, ba, tag, bps;
1782 id = avctx->codec_id;
1783 sr = avctx->sample_rate;
1784 ch = avctx->channels;
1785 ba = avctx->block_align;
1786 tag = avctx->codec_tag;
1787 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1789 /* codecs with an exact constant bits per sample */
1790 if (bps > 0 && ch > 0 && frame_bytes > 0)
1791 return (frame_bytes * 8) / (bps * ch);
1792 bps = avctx->bits_per_coded_sample;
1794 /* codecs with a fixed packet duration */
1796 case AV_CODEC_ID_ADPCM_ADX: return 32;
1797 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1798 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1799 case AV_CODEC_ID_AMR_NB:
1800 case AV_CODEC_ID_GSM:
1801 case AV_CODEC_ID_QCELP:
1802 case AV_CODEC_ID_RA_144:
1803 case AV_CODEC_ID_RA_288: return 160;
1804 case AV_CODEC_ID_IMC: return 256;
1805 case AV_CODEC_ID_AMR_WB:
1806 case AV_CODEC_ID_GSM_MS: return 320;
1807 case AV_CODEC_ID_MP1: return 384;
1808 case AV_CODEC_ID_ATRAC1: return 512;
1809 case AV_CODEC_ID_ATRAC3: return 1024;
1810 case AV_CODEC_ID_MP2:
1811 case AV_CODEC_ID_MUSEPACK7: return 1152;
1812 case AV_CODEC_ID_AC3: return 1536;
1816 /* calc from sample rate */
1817 if (id == AV_CODEC_ID_TTA)
1818 return 256 * sr / 245;
1821 /* calc from sample rate and channels */
1822 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1823 return (480 << (sr / 22050)) / ch;
1828 /* calc from block_align */
1829 if (id == AV_CODEC_ID_SIPR) {
1831 case 20: return 160;
1832 case 19: return 144;
1833 case 29: return 288;
1834 case 37: return 480;
1836 } else if (id == AV_CODEC_ID_ILBC) {
1838 case 38: return 160;
1839 case 50: return 240;
1844 if (frame_bytes > 0) {
1845 /* calc from frame_bytes only */
1846 if (id == AV_CODEC_ID_TRUESPEECH)
1847 return 240 * (frame_bytes / 32);
1848 if (id == AV_CODEC_ID_NELLYMOSER)
1849 return 256 * (frame_bytes / 64);
1852 /* calc from frame_bytes and bits_per_coded_sample */
1853 if (id == AV_CODEC_ID_ADPCM_G726)
1854 return frame_bytes * 8 / bps;
1858 /* calc from frame_bytes and channels */
1860 case AV_CODEC_ID_ADPCM_4XM:
1861 case AV_CODEC_ID_ADPCM_IMA_ISS:
1862 return (frame_bytes - 4 * ch) * 2 / ch;
1863 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1864 return (frame_bytes - 4) * 2 / ch;
1865 case AV_CODEC_ID_ADPCM_IMA_AMV:
1866 return (frame_bytes - 8) * 2 / ch;
1867 case AV_CODEC_ID_ADPCM_XA:
1868 return (frame_bytes / 128) * 224 / ch;
1869 case AV_CODEC_ID_INTERPLAY_DPCM:
1870 return (frame_bytes - 6 - ch) / ch;
1871 case AV_CODEC_ID_ROQ_DPCM:
1872 return (frame_bytes - 8) / ch;
1873 case AV_CODEC_ID_XAN_DPCM:
1874 return (frame_bytes - 2 * ch) / ch;
1875 case AV_CODEC_ID_MACE3:
1876 return 3 * frame_bytes / ch;
1877 case AV_CODEC_ID_MACE6:
1878 return 6 * frame_bytes / ch;
1879 case AV_CODEC_ID_PCM_LXF:
1880 return 2 * (frame_bytes / (5 * ch));
1884 /* calc from frame_bytes, channels, and codec_tag */
1885 if (id == AV_CODEC_ID_SOL_DPCM) {
1887 return frame_bytes / ch;
1889 return frame_bytes * 2 / ch;
1894 /* calc from frame_bytes, channels, and block_align */
1895 int blocks = frame_bytes / ba;
1896 switch (avctx->codec_id) {
1897 case AV_CODEC_ID_ADPCM_IMA_WAV:
1898 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1899 case AV_CODEC_ID_ADPCM_IMA_DK3:
1900 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1901 case AV_CODEC_ID_ADPCM_IMA_DK4:
1902 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1903 case AV_CODEC_ID_ADPCM_MS:
1904 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1909 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1910 switch (avctx->codec_id) {
1911 case AV_CODEC_ID_PCM_DVD:
1912 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1913 case AV_CODEC_ID_PCM_BLURAY:
1914 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1915 case AV_CODEC_ID_S302M:
1916 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1926 int ff_thread_init(AVCodecContext *s){
1931 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1945 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1947 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1951 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1953 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1954 "version to the newest one from Git. If the problem still "
1955 "occurs, it means that your file has a feature which has not "
1956 "been implemented.\n", feature);
1958 av_log_ask_for_sample(avc, NULL);
1961 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1963 va_list argument_list;
1965 va_start(argument_list, msg);
1968 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1969 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1970 "of this file to ftp://upload.libav.org/incoming/ "
1971 "and contact the libav-devel mailing list.\n");
1973 va_end(argument_list);
1976 static AVHWAccel *first_hwaccel = NULL;
1978 void av_register_hwaccel(AVHWAccel *hwaccel)
1980 AVHWAccel **p = &first_hwaccel;
1984 hwaccel->next = NULL;
1987 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1989 return hwaccel ? hwaccel->next : first_hwaccel;
1992 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
1994 AVHWAccel *hwaccel=NULL;
1996 while((hwaccel= av_hwaccel_next(hwaccel))){
1997 if ( hwaccel->id == codec_id
1998 && hwaccel->pix_fmt == pix_fmt)
2004 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2006 if (ff_lockmgr_cb) {
2007 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2009 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2015 if (ff_lockmgr_cb) {
2016 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2018 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2024 int avpriv_lock_avformat(void)
2026 if (ff_lockmgr_cb) {
2027 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2033 int avpriv_unlock_avformat(void)
2035 if (ff_lockmgr_cb) {
2036 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2042 unsigned int avpriv_toupper4(unsigned int x)
2044 return toupper( x &0xFF)
2045 + (toupper((x>>8 )&0xFF)<<8 )
2046 + (toupper((x>>16)&0xFF)<<16)
2047 + (toupper((x>>24)&0xFF)<<24);
2052 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2055 return avctx->get_buffer(avctx, f);
2058 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2060 f->owner->release_buffer(f->owner, f);
2063 void ff_thread_finish_setup(AVCodecContext *avctx)
2067 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2071 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2077 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2079 if (codec_id <= AV_CODEC_ID_NONE)
2080 return AVMEDIA_TYPE_UNKNOWN;
2081 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2082 return AVMEDIA_TYPE_VIDEO;
2083 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2084 return AVMEDIA_TYPE_AUDIO;
2085 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2086 return AVMEDIA_TYPE_SUBTITLE;
2088 return AVMEDIA_TYPE_UNKNOWN;
2091 int avcodec_is_open(AVCodecContext *s)
2093 return !!s->internal;