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;
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)
232 *height+=2; // some of the optimized chroma MC reads one line too much
234 for (i = 0; i < 4; i++)
235 linesize_align[i] = STRIDE_ALIGN;
238 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
239 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
240 int linesize_align[AV_NUM_DATA_POINTERS];
242 avcodec_align_dimensions2(s, width, height, linesize_align);
243 align = FFMAX(linesize_align[0], linesize_align[3]);
244 linesize_align[1] <<= chroma_shift;
245 linesize_align[2] <<= chroma_shift;
246 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
247 *width=FFALIGN(*width, align);
250 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
251 enum AVSampleFormat sample_fmt, const uint8_t *buf,
252 int buf_size, int align)
254 int ch, planar, needed_size, ret = 0;
256 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
257 frame->nb_samples, sample_fmt,
259 if (buf_size < needed_size)
260 return AVERROR(EINVAL);
262 planar = av_sample_fmt_is_planar(sample_fmt);
263 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
264 if (!(frame->extended_data = av_mallocz(nb_channels *
265 sizeof(*frame->extended_data))))
266 return AVERROR(ENOMEM);
268 frame->extended_data = frame->data;
271 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
272 buf, nb_channels, frame->nb_samples,
273 sample_fmt, align)) < 0) {
274 if (frame->extended_data != frame->data)
275 av_free(frame->extended_data);
278 if (frame->extended_data != frame->data) {
279 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
280 frame->data[ch] = frame->extended_data[ch];
286 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
288 AVCodecInternal *avci = avctx->internal;
292 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
293 frame->nb_samples, avctx->sample_fmt,
296 return AVERROR(EINVAL);
298 /* allocate InternalBuffer if needed */
300 avci->buffer = av_mallocz(sizeof(InternalBuffer));
302 return AVERROR(ENOMEM);
306 /* if there is a previously-used internal buffer, check its size and
307 channel count to see if we can reuse it */
308 if (buf->extended_data) {
309 /* if current buffer is too small, free it */
310 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
311 av_free(buf->extended_data[0]);
312 if (buf->extended_data != buf->data)
313 av_free(&buf->extended_data);
314 buf->extended_data = NULL;
317 /* if number of channels has changed, reset and/or free extended data
318 pointers but leave data buffer in buf->data[0] for reuse */
319 if (buf->nb_channels != avctx->channels) {
320 if (buf->extended_data != buf->data)
321 av_free(buf->extended_data);
322 buf->extended_data = NULL;
326 /* if there is no previous buffer or the previous buffer cannot be used
327 as-is, allocate a new buffer and/or rearrange the channel pointers */
328 if (!buf->extended_data) {
330 if (!(buf->data[0] = av_mallocz(buf_size)))
331 return AVERROR(ENOMEM);
332 buf->audio_data_size = buf_size;
334 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
335 avctx->sample_fmt, buf->data[0],
336 buf->audio_data_size, 0)))
339 if (frame->extended_data == frame->data)
340 buf->extended_data = buf->data;
342 buf->extended_data = frame->extended_data;
343 memcpy(buf->data, frame->data, sizeof(frame->data));
344 buf->linesize[0] = frame->linesize[0];
345 buf->nb_channels = avctx->channels;
347 /* copy InternalBuffer info to the AVFrame */
348 frame->extended_data = buf->extended_data;
349 frame->linesize[0] = buf->linesize[0];
350 memcpy(frame->data, buf->data, sizeof(frame->data));
353 frame->type = FF_BUFFER_TYPE_INTERNAL;
355 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
356 else frame->pkt_pts = AV_NOPTS_VALUE;
357 frame->reordered_opaque = avctx->reordered_opaque;
359 frame->sample_rate = avctx->sample_rate;
360 frame->format = avctx->sample_fmt;
361 frame->channel_layout = avctx->channel_layout;
363 if (avctx->debug & FF_DEBUG_BUFFERS)
364 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
365 "internal audio buffer used\n", frame);
370 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
376 AVCodecInternal *avci = s->internal;
378 if(pic->data[0]!=NULL) {
379 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
382 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
383 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
387 if(av_image_check_size(w, h, 0, s))
391 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
392 sizeof(InternalBuffer));
395 buf = &avci->buffer[avci->buffer_count];
397 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
398 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
399 av_freep(&buf->base[i]);
405 int h_chroma_shift, v_chroma_shift;
410 int stride_align[AV_NUM_DATA_POINTERS];
411 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
413 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
415 avcodec_align_dimensions2(s, &w, &h, stride_align);
417 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
423 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
424 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
425 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
426 // increase alignment of w for next try (rhs gives the lowest bit set in w)
431 unaligned |= picture.linesize[i] % stride_align[i];
435 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
439 for (i=0; i<3 && picture.data[i+1]; i++)
440 size[i] = picture.data[i+1] - picture.data[i];
441 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
443 memset(buf->base, 0, sizeof(buf->base));
444 memset(buf->data, 0, sizeof(buf->data));
446 for(i=0; i<4 && size[i]; i++){
447 const int h_shift= i==0 ? 0 : h_chroma_shift;
448 const int v_shift= i==0 ? 0 : v_chroma_shift;
450 buf->linesize[i]= picture.linesize[i];
452 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
453 if(buf->base[i]==NULL) return -1;
454 memset(buf->base[i], 128, size[i]);
456 // no edge if EDGE EMU or not planar YUV
457 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
458 buf->data[i] = buf->base[i];
460 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
462 for (; i < AV_NUM_DATA_POINTERS; i++) {
463 buf->base[i] = buf->data[i] = NULL;
464 buf->linesize[i] = 0;
466 if(size[1] && !size[2])
467 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
468 buf->width = s->width;
469 buf->height = s->height;
470 buf->pix_fmt= s->pix_fmt;
472 pic->type= FF_BUFFER_TYPE_INTERNAL;
474 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
475 pic->base[i]= buf->base[i];
476 pic->data[i]= buf->data[i];
477 pic->linesize[i]= buf->linesize[i];
479 pic->extended_data = pic->data;
480 avci->buffer_count++;
481 pic->width = buf->width;
482 pic->height = buf->height;
483 pic->format = buf->pix_fmt;
484 pic->sample_aspect_ratio = s->sample_aspect_ratio;
486 if(s->pkt) pic->pkt_pts= s->pkt->pts;
487 else pic->pkt_pts= AV_NOPTS_VALUE;
488 pic->reordered_opaque= s->reordered_opaque;
490 if(s->debug&FF_DEBUG_BUFFERS)
491 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
492 "buffers used\n", pic, avci->buffer_count);
497 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
499 switch (avctx->codec_type) {
500 case AVMEDIA_TYPE_VIDEO:
501 return video_get_buffer(avctx, frame);
502 case AVMEDIA_TYPE_AUDIO:
503 return audio_get_buffer(avctx, frame);
509 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
511 InternalBuffer *buf, *last;
512 AVCodecInternal *avci = s->internal;
514 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
516 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
517 assert(avci->buffer_count);
520 buf = NULL; /* avoids warning */
521 for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
522 buf = &avci->buffer[i];
523 if (buf->data[0] == pic->data[0])
526 assert(i < avci->buffer_count);
527 avci->buffer_count--;
528 last = &avci->buffer[avci->buffer_count];
531 FFSWAP(InternalBuffer, *buf, *last);
534 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
536 // pic->base[i]=NULL;
538 //printf("R%X\n", pic->opaque);
540 if(s->debug&FF_DEBUG_BUFFERS)
541 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
542 "buffers used\n", pic, avci->buffer_count);
545 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
549 assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
551 /* If no picture return a new buffer */
552 if(pic->data[0] == NULL) {
553 /* We will copy from buffer, so must be readable */
554 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
555 return s->get_buffer(s, pic);
558 assert(s->pix_fmt == pic->format);
560 /* If internal buffer type return the same buffer */
561 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
562 if(s->pkt) pic->pkt_pts= s->pkt->pts;
563 else pic->pkt_pts= AV_NOPTS_VALUE;
564 pic->reordered_opaque= s->reordered_opaque;
569 * Not internal type and reget_buffer not overridden, emulate cr buffer
572 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
573 pic->data[i] = pic->base[i] = NULL;
575 /* Allocate new frame */
576 if (s->get_buffer(s, pic))
578 /* Copy image data from old buffer to new buffer */
579 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
581 s->release_buffer(s, &temp_pic); // Release old frame
585 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
588 for(i=0; i<count; i++){
589 int r= func(c, (char*)arg + i*size);
595 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
598 for(i=0; i<count; i++){
599 int r= func(c, arg, i, 0);
605 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
606 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
611 void avcodec_get_frame_defaults(AVFrame *pic){
612 memset(pic, 0, sizeof(AVFrame));
614 pic->pts= AV_NOPTS_VALUE;
616 pic->sample_aspect_ratio = (AVRational){0, 1};
617 pic->format = -1; /* unknown */
620 AVFrame *avcodec_alloc_frame(void){
621 AVFrame *pic= av_malloc(sizeof(AVFrame));
623 if(pic==NULL) return NULL;
625 avcodec_get_frame_defaults(pic);
630 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
633 AVDictionary *tmp = NULL;
635 if (avcodec_is_open(avctx))
638 if ((!codec && !avctx->codec)) {
639 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
640 return AVERROR(EINVAL);
642 if ((codec && avctx->codec && codec != avctx->codec)) {
643 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
644 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
645 return AVERROR(EINVAL);
648 codec = avctx->codec;
650 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
651 return AVERROR(EINVAL);
654 av_dict_copy(&tmp, *options, 0);
656 /* If there is a user-supplied mutex locking routine, call it. */
658 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
662 entangled_thread_counter++;
663 if(entangled_thread_counter != 1){
664 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
669 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
670 if (!avctx->internal) {
671 ret = AVERROR(ENOMEM);
675 if (codec->priv_data_size > 0) {
676 if(!avctx->priv_data){
677 avctx->priv_data = av_mallocz(codec->priv_data_size);
678 if (!avctx->priv_data) {
679 ret = AVERROR(ENOMEM);
682 if (codec->priv_class) {
683 *(AVClass**)avctx->priv_data= codec->priv_class;
684 av_opt_set_defaults(avctx->priv_data);
687 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
690 avctx->priv_data = NULL;
692 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
695 if(avctx->coded_width && avctx->coded_height)
696 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
697 else if(avctx->width && avctx->height)
698 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
700 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
701 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
702 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
703 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
704 avcodec_set_dimensions(avctx, 0, 0);
707 /* if the decoder init function was already called previously,
708 free the already allocated subtitle_header before overwriting it */
709 if (av_codec_is_decoder(codec))
710 av_freep(&avctx->subtitle_header);
712 #define SANE_NB_CHANNELS 128U
713 if (avctx->channels > SANE_NB_CHANNELS) {
714 ret = AVERROR(EINVAL);
718 avctx->codec = codec;
719 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
720 avctx->codec_id == CODEC_ID_NONE) {
721 avctx->codec_type = codec->type;
722 avctx->codec_id = codec->id;
724 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
725 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
726 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
727 ret = AVERROR(EINVAL);
730 avctx->frame_number = 0;
732 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
733 (!avctx->time_base.num || !avctx->time_base.den)) {
734 avctx->time_base.num = 1;
735 avctx->time_base.den = avctx->sample_rate;
738 if (HAVE_THREADS && !avctx->thread_opaque) {
739 ret = ff_thread_init(avctx);
744 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
745 avctx->thread_count = 1;
747 if (av_codec_is_encoder(avctx->codec)) {
749 if (avctx->codec->sample_fmts) {
750 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
751 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
753 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
754 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
755 ret = AVERROR(EINVAL);
759 if (avctx->codec->pix_fmts) {
760 for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
761 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
763 if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
764 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
765 ret = AVERROR(EINVAL);
769 if (avctx->codec->supported_samplerates) {
770 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
771 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
773 if (avctx->codec->supported_samplerates[i] == 0) {
774 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
775 ret = AVERROR(EINVAL);
779 if (avctx->codec->channel_layouts) {
780 if (!avctx->channel_layout) {
781 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
783 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
784 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
786 if (avctx->codec->channel_layouts[i] == 0) {
787 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
788 ret = AVERROR(EINVAL);
793 if (avctx->channel_layout && avctx->channels) {
794 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
795 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
796 ret = AVERROR(EINVAL);
799 } else if (avctx->channel_layout) {
800 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
804 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
805 ret = avctx->codec->init(avctx);
811 if (av_codec_is_decoder(avctx->codec)) {
812 /* validate channel layout from the decoder */
813 if (avctx->channel_layout &&
814 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
815 av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
816 avctx->channel_layout = 0;
820 entangled_thread_counter--;
822 /* Release any user-supplied mutex. */
824 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
827 av_dict_free(options);
834 av_freep(&avctx->priv_data);
835 av_freep(&avctx->internal);
840 int ff_alloc_packet(AVPacket *avpkt, int size)
842 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
843 return AVERROR(EINVAL);
846 void *destruct = avpkt->destruct;
848 if (avpkt->size < size)
849 return AVERROR(EINVAL);
851 av_init_packet(avpkt);
852 avpkt->destruct = destruct;
856 return av_new_packet(avpkt, size);
860 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
862 const AVFrame *frame,
866 int user_packet = !!avpkt->data;
870 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
871 av_free_packet(avpkt);
872 av_init_packet(avpkt);
876 /* check for valid frame size */
878 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
879 if (frame->nb_samples > avctx->frame_size)
880 return AVERROR(EINVAL);
881 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
882 if (frame->nb_samples != avctx->frame_size)
883 return AVERROR(EINVAL);
887 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
889 if (*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 if (!user_packet && avpkt->size) {
903 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
905 avpkt->data = new_data;
908 avctx->frame_number++;
911 if (ret < 0 || !*got_packet_ptr) {
912 av_free_packet(avpkt);
913 av_init_packet(avpkt);
917 /* NOTE: if we add any audio encoders which output non-keyframe packets,
918 this needs to be moved to the encoders, but for now we can do it
919 here to simplify things */
920 avpkt->flags |= AV_PKT_FLAG_KEY;
925 #if FF_API_OLD_DECODE_AUDIO
926 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
927 uint8_t *buf, int buf_size,
928 const short *samples)
933 int ret, samples_size, got_packet;
935 av_init_packet(&pkt);
941 avcodec_get_frame_defaults(frame);
943 if (avctx->frame_size) {
944 frame->nb_samples = avctx->frame_size;
946 /* if frame_size is not set, the number of samples must be
947 calculated from the buffer size */
949 if (!av_get_bits_per_sample(avctx->codec_id)) {
950 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
951 "support this codec\n");
952 return AVERROR(EINVAL);
954 nb_samples = (int64_t)buf_size * 8 /
955 (av_get_bits_per_sample(avctx->codec_id) *
957 if (nb_samples >= INT_MAX)
958 return AVERROR(EINVAL);
959 frame->nb_samples = nb_samples;
962 /* it is assumed that the samples buffer is large enough based on the
963 relevant parameters */
964 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
966 avctx->sample_fmt, 1);
967 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
969 samples, samples_size, 1)))
972 /* fabricate frame pts from sample count.
973 this is needed because the avcodec_encode_audio() API does not have
974 a way for the user to provide pts */
975 frame->pts = ff_samples_to_time_base(avctx,
976 avctx->internal->sample_count);
977 avctx->internal->sample_count += frame->nb_samples;
983 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
984 if (!ret && got_packet && avctx->coded_frame) {
985 avctx->coded_frame->pts = pkt.pts;
986 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
988 /* free any side data since we cannot return it */
989 if (pkt.side_data_elems > 0) {
991 for (i = 0; i < pkt.side_data_elems; i++)
992 av_free(pkt.side_data[i].data);
993 av_freep(&pkt.side_data);
994 pkt.side_data_elems = 0;
997 if (frame && frame->extended_data != frame->data)
998 av_free(frame->extended_data);
1000 return ret ? ret : pkt.size;
1004 #if FF_API_OLD_ENCODE_VIDEO
1005 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1006 const AVFrame *pict)
1009 int ret, got_packet = 0;
1011 if(buf_size < FF_MIN_BUFFER_SIZE){
1012 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1016 av_init_packet(&pkt);
1018 pkt.size = buf_size;
1020 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1021 if (!ret && got_packet && avctx->coded_frame) {
1022 avctx->coded_frame->pts = pkt.pts;
1023 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1026 /* free any side data since we cannot return it */
1027 if (pkt.side_data_elems > 0) {
1029 for (i = 0; i < pkt.side_data_elems; i++)
1030 av_free(pkt.side_data[i].data);
1031 av_freep(&pkt.side_data);
1032 pkt.side_data_elems = 0;
1035 return ret ? ret : pkt.size;
1039 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1041 const AVFrame *frame,
1042 int *got_packet_ptr)
1045 int user_packet = !!avpkt->data;
1047 *got_packet_ptr = 0;
1049 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1050 av_free_packet(avpkt);
1051 av_init_packet(avpkt);
1056 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1057 return AVERROR(EINVAL);
1059 av_assert0(avctx->codec->encode2);
1061 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1063 if (!*got_packet_ptr)
1065 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1066 avpkt->pts = avpkt->dts = frame->pts;
1068 if (!user_packet && avpkt->size) {
1069 uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1071 avpkt->data = new_data;
1074 avctx->frame_number++;
1077 if (ret < 0 || !*got_packet_ptr)
1078 av_free_packet(avpkt);
1084 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1085 const AVSubtitle *sub)
1088 if(sub->start_display_time) {
1089 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1092 if(sub->num_rects == 0 || !sub->rects)
1094 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1095 avctx->frame_number++;
1099 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1102 const uint8_t *data;
1105 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1108 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1109 if (!data || size < 4)
1111 flags = bytestream_get_le32(&data);
1113 if (size < 4) /* Required for any of the changes */
1115 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1116 avctx->channels = bytestream_get_le32(&data);
1119 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1122 avctx->channel_layout = bytestream_get_le64(&data);
1127 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1128 avctx->sample_rate = bytestream_get_le32(&data);
1131 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1134 avctx->width = bytestream_get_le32(&data);
1135 avctx->height = bytestream_get_le32(&data);
1136 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1141 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1142 int *got_picture_ptr,
1147 *got_picture_ptr= 0;
1148 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1152 apply_param_change(avctx, avpkt);
1154 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1155 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1156 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1159 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1161 picture->pkt_dts= avpkt->dts;
1162 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1163 picture->width = avctx->width;
1164 picture->height = avctx->height;
1165 picture->format = avctx->pix_fmt;
1168 emms_c(); //needed to avoid an emms_c() call before every return;
1170 if (*got_picture_ptr)
1171 avctx->frame_number++;
1178 #if FF_API_OLD_DECODE_AUDIO
1179 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1180 int *frame_size_ptr,
1184 int ret, got_frame = 0;
1186 if (avctx->get_buffer != avcodec_default_get_buffer) {
1187 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1188 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1189 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1190 "avcodec_decode_audio4()\n");
1191 avctx->get_buffer = avcodec_default_get_buffer;
1194 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1196 if (ret >= 0 && got_frame) {
1198 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1199 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1201 avctx->sample_fmt, 1);
1202 if (*frame_size_ptr < data_size) {
1203 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1204 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1205 return AVERROR(EINVAL);
1208 memcpy(samples, frame.extended_data[0], plane_size);
1210 if (planar && avctx->channels > 1) {
1211 uint8_t *out = ((uint8_t *)samples) + plane_size;
1212 for (ch = 1; ch < avctx->channels; ch++) {
1213 memcpy(out, frame.extended_data[ch], plane_size);
1217 *frame_size_ptr = data_size;
1219 *frame_size_ptr = 0;
1225 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1236 if (!avpkt->data && avpkt->size) {
1237 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1238 return AVERROR(EINVAL);
1241 apply_param_change(avctx, avpkt);
1243 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1244 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1245 if (ret >= 0 && *got_frame_ptr) {
1246 avctx->frame_number++;
1247 frame->pkt_dts = avpkt->dts;
1248 if (frame->format == AV_SAMPLE_FMT_NONE)
1249 frame->format = avctx->sample_fmt;
1255 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1263 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1265 avctx->frame_number++;
1269 void avsubtitle_free(AVSubtitle *sub)
1273 for (i = 0; i < sub->num_rects; i++)
1275 av_freep(&sub->rects[i]->pict.data[0]);
1276 av_freep(&sub->rects[i]->pict.data[1]);
1277 av_freep(&sub->rects[i]->pict.data[2]);
1278 av_freep(&sub->rects[i]->pict.data[3]);
1279 av_freep(&sub->rects[i]->text);
1280 av_freep(&sub->rects[i]->ass);
1281 av_freep(&sub->rects[i]);
1284 av_freep(&sub->rects);
1286 memset(sub, 0, sizeof(AVSubtitle));
1289 av_cold int avcodec_close(AVCodecContext *avctx)
1291 /* If there is a user-supplied mutex locking routine, call it. */
1292 if (ff_lockmgr_cb) {
1293 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1297 entangled_thread_counter++;
1298 if(entangled_thread_counter != 1){
1299 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1300 entangled_thread_counter--;
1304 if (avcodec_is_open(avctx)) {
1305 if (HAVE_THREADS && avctx->thread_opaque)
1306 ff_thread_free(avctx);
1307 if (avctx->codec && avctx->codec->close)
1308 avctx->codec->close(avctx);
1309 avcodec_default_free_buffers(avctx);
1310 avctx->coded_frame = NULL;
1311 av_freep(&avctx->internal);
1314 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1315 av_opt_free(avctx->priv_data);
1317 av_freep(&avctx->priv_data);
1318 if (av_codec_is_encoder(avctx->codec))
1319 av_freep(&avctx->extradata);
1320 avctx->codec = NULL;
1321 avctx->active_thread_type = 0;
1322 entangled_thread_counter--;
1324 /* Release any user-supplied mutex. */
1325 if (ff_lockmgr_cb) {
1326 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1331 AVCodec *avcodec_find_encoder(enum CodecID id)
1333 AVCodec *p, *experimental=NULL;
1336 if (av_codec_is_encoder(p) && p->id == id) {
1337 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1344 return experimental;
1347 AVCodec *avcodec_find_encoder_by_name(const char *name)
1354 if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1361 AVCodec *avcodec_find_decoder(enum CodecID id)
1366 if (av_codec_is_decoder(p) && p->id == id)
1373 AVCodec *avcodec_find_decoder_by_name(const char *name)
1380 if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1387 static int get_bit_rate(AVCodecContext *ctx)
1390 int bits_per_sample;
1392 switch(ctx->codec_type) {
1393 case AVMEDIA_TYPE_VIDEO:
1394 case AVMEDIA_TYPE_DATA:
1395 case AVMEDIA_TYPE_SUBTITLE:
1396 case AVMEDIA_TYPE_ATTACHMENT:
1397 bit_rate = ctx->bit_rate;
1399 case AVMEDIA_TYPE_AUDIO:
1400 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1401 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1410 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1412 int i, len, ret = 0;
1414 for (i = 0; i < 4; i++) {
1415 len = snprintf(buf, buf_size,
1416 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1418 buf_size = buf_size > len ? buf_size - len : 0;
1425 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1427 const char *codec_name;
1428 const char *profile = NULL;
1432 AVRational display_aspect_ratio;
1437 p = avcodec_find_encoder(enc->codec_id);
1439 p = avcodec_find_decoder(enc->codec_id);
1442 codec_name = p->name;
1443 profile = av_get_profile_name(p, enc->profile);
1444 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1445 /* fake mpeg2 transport stream codec (currently not
1447 codec_name = "mpeg2ts";
1448 } else if (enc->codec_name[0] != '\0') {
1449 codec_name = enc->codec_name;
1451 /* output avi tags */
1453 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1454 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1458 switch(enc->codec_type) {
1459 case AVMEDIA_TYPE_VIDEO:
1460 snprintf(buf, buf_size,
1462 codec_name, enc->mb_decision ? " (hq)" : "");
1464 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1466 if (enc->pix_fmt != PIX_FMT_NONE) {
1467 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1469 av_get_pix_fmt_name(enc->pix_fmt));
1472 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1474 enc->width, enc->height);
1475 if (enc->sample_aspect_ratio.num) {
1476 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1477 enc->width*enc->sample_aspect_ratio.num,
1478 enc->height*enc->sample_aspect_ratio.den,
1480 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1481 " [PAR %d:%d DAR %d:%d]",
1482 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1483 display_aspect_ratio.num, display_aspect_ratio.den);
1485 if(av_log_get_level() >= AV_LOG_DEBUG){
1486 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1487 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1489 enc->time_base.num/g, enc->time_base.den/g);
1493 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1494 ", q=%d-%d", enc->qmin, enc->qmax);
1497 case AVMEDIA_TYPE_AUDIO:
1498 snprintf(buf, buf_size,
1502 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1504 if (enc->sample_rate) {
1505 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1506 ", %d Hz", enc->sample_rate);
1508 av_strlcat(buf, ", ", buf_size);
1509 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1510 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1511 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1512 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1515 case AVMEDIA_TYPE_DATA:
1516 snprintf(buf, buf_size, "Data: %s", codec_name);
1518 case AVMEDIA_TYPE_SUBTITLE:
1519 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1521 case AVMEDIA_TYPE_ATTACHMENT:
1522 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1525 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1529 if (enc->flags & CODEC_FLAG_PASS1)
1530 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1532 if (enc->flags & CODEC_FLAG_PASS2)
1533 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1536 bitrate = get_bit_rate(enc);
1538 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1539 ", %d kb/s", bitrate / 1000);
1543 const char *av_get_profile_name(const AVCodec *codec, int profile)
1546 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1549 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1550 if (p->profile == profile)
1556 unsigned avcodec_version( void )
1558 return LIBAVCODEC_VERSION_INT;
1561 const char *avcodec_configuration(void)
1563 return LIBAV_CONFIGURATION;
1566 const char *avcodec_license(void)
1568 #define LICENSE_PREFIX "libavcodec license: "
1569 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1572 void avcodec_flush_buffers(AVCodecContext *avctx)
1574 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1575 ff_thread_flush(avctx);
1576 else if(avctx->codec->flush)
1577 avctx->codec->flush(avctx);
1580 static void video_free_buffers(AVCodecContext *s)
1582 AVCodecInternal *avci = s->internal;
1588 if (avci->buffer_count)
1589 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1590 avci->buffer_count);
1591 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1592 InternalBuffer *buf = &avci->buffer[i];
1594 av_freep(&buf->base[j]);
1598 av_freep(&avci->buffer);
1600 avci->buffer_count=0;
1603 static void audio_free_buffers(AVCodecContext *avctx)
1605 AVCodecInternal *avci = avctx->internal;
1606 InternalBuffer *buf;
1612 if (buf->extended_data) {
1613 av_free(buf->extended_data[0]);
1614 if (buf->extended_data != buf->data)
1615 av_free(buf->extended_data);
1617 av_freep(&avci->buffer);
1620 void avcodec_default_free_buffers(AVCodecContext *avctx)
1622 switch (avctx->codec_type) {
1623 case AVMEDIA_TYPE_VIDEO:
1624 video_free_buffers(avctx);
1626 case AVMEDIA_TYPE_AUDIO:
1627 audio_free_buffers(avctx);
1634 int av_get_exact_bits_per_sample(enum CodecID codec_id)
1637 case CODEC_ID_ADPCM_CT:
1638 case CODEC_ID_ADPCM_IMA_APC:
1639 case CODEC_ID_ADPCM_IMA_EA_SEAD:
1640 case CODEC_ID_ADPCM_IMA_WS:
1641 case CODEC_ID_ADPCM_G722:
1642 case CODEC_ID_ADPCM_YAMAHA:
1644 case CODEC_ID_PCM_ALAW:
1645 case CODEC_ID_PCM_MULAW:
1646 case CODEC_ID_PCM_S8:
1647 case CODEC_ID_PCM_U8:
1648 case CODEC_ID_PCM_ZORK:
1650 case CODEC_ID_PCM_S16BE:
1651 case CODEC_ID_PCM_S16LE:
1652 case CODEC_ID_PCM_S16LE_PLANAR:
1653 case CODEC_ID_PCM_U16BE:
1654 case CODEC_ID_PCM_U16LE:
1656 case CODEC_ID_PCM_S24DAUD:
1657 case CODEC_ID_PCM_S24BE:
1658 case CODEC_ID_PCM_S24LE:
1659 case CODEC_ID_PCM_U24BE:
1660 case CODEC_ID_PCM_U24LE:
1662 case CODEC_ID_PCM_S32BE:
1663 case CODEC_ID_PCM_S32LE:
1664 case CODEC_ID_PCM_U32BE:
1665 case CODEC_ID_PCM_U32LE:
1666 case CODEC_ID_PCM_F32BE:
1667 case CODEC_ID_PCM_F32LE:
1669 case CODEC_ID_PCM_F64BE:
1670 case CODEC_ID_PCM_F64LE:
1677 int av_get_bits_per_sample(enum CodecID codec_id)
1680 case CODEC_ID_ADPCM_SBPRO_2:
1682 case CODEC_ID_ADPCM_SBPRO_3:
1684 case CODEC_ID_ADPCM_SBPRO_4:
1685 case CODEC_ID_ADPCM_IMA_WAV:
1686 case CODEC_ID_ADPCM_IMA_QT:
1687 case CODEC_ID_ADPCM_SWF:
1688 case CODEC_ID_ADPCM_MS:
1691 return av_get_exact_bits_per_sample(codec_id);
1695 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1697 int id, sr, ch, ba, tag, bps;
1699 id = avctx->codec_id;
1700 sr = avctx->sample_rate;
1701 ch = avctx->channels;
1702 ba = avctx->block_align;
1703 tag = avctx->codec_tag;
1704 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1706 /* codecs with an exact constant bits per sample */
1707 if (bps > 0 && ch > 0 && frame_bytes > 0)
1708 return (frame_bytes * 8) / (bps * ch);
1709 bps = avctx->bits_per_coded_sample;
1711 /* codecs with a fixed packet duration */
1713 case CODEC_ID_ADPCM_ADX: return 32;
1714 case CODEC_ID_ADPCM_IMA_QT: return 64;
1715 case CODEC_ID_ADPCM_EA_XAS: return 128;
1716 case CODEC_ID_AMR_NB:
1718 case CODEC_ID_QCELP:
1719 case CODEC_ID_RA_144:
1720 case CODEC_ID_RA_288: return 160;
1721 case CODEC_ID_IMC: return 256;
1722 case CODEC_ID_AMR_WB:
1723 case CODEC_ID_GSM_MS: return 320;
1724 case CODEC_ID_MP1: return 384;
1725 case CODEC_ID_ATRAC1: return 512;
1726 case CODEC_ID_ATRAC3: return 1024;
1728 case CODEC_ID_MUSEPACK7: return 1152;
1729 case CODEC_ID_AC3: return 1536;
1733 /* calc from sample rate */
1734 if (id == CODEC_ID_TTA)
1735 return 256 * sr / 245;
1738 /* calc from sample rate and channels */
1739 if (id == CODEC_ID_BINKAUDIO_DCT)
1740 return (480 << (sr / 22050)) / ch;
1745 /* calc from block_align */
1746 if (id == CODEC_ID_SIPR) {
1748 case 20: return 160;
1749 case 19: return 144;
1750 case 29: return 288;
1751 case 37: return 480;
1756 if (frame_bytes > 0) {
1757 /* calc from frame_bytes only */
1758 if (id == CODEC_ID_TRUESPEECH)
1759 return 240 * (frame_bytes / 32);
1760 if (id == CODEC_ID_NELLYMOSER)
1761 return 256 * (frame_bytes / 64);
1764 /* calc from frame_bytes and bits_per_coded_sample */
1765 if (id == CODEC_ID_ADPCM_G726)
1766 return frame_bytes * 8 / bps;
1770 /* calc from frame_bytes and channels */
1772 case CODEC_ID_ADPCM_4XM:
1773 case CODEC_ID_ADPCM_IMA_ISS:
1774 return (frame_bytes - 4 * ch) * 2 / ch;
1775 case CODEC_ID_ADPCM_IMA_SMJPEG:
1776 return (frame_bytes - 4) * 2 / ch;
1777 case CODEC_ID_ADPCM_IMA_AMV:
1778 return (frame_bytes - 8) * 2 / ch;
1779 case CODEC_ID_ADPCM_XA:
1780 return (frame_bytes / 128) * 224 / ch;
1781 case CODEC_ID_INTERPLAY_DPCM:
1782 return (frame_bytes - 6 - ch) / ch;
1783 case CODEC_ID_ROQ_DPCM:
1784 return (frame_bytes - 8) / ch;
1785 case CODEC_ID_XAN_DPCM:
1786 return (frame_bytes - 2 * ch) / ch;
1787 case CODEC_ID_MACE3:
1788 return 3 * frame_bytes / ch;
1789 case CODEC_ID_MACE6:
1790 return 6 * frame_bytes / ch;
1791 case CODEC_ID_PCM_LXF:
1792 return 2 * (frame_bytes / (5 * ch));
1796 /* calc from frame_bytes, channels, and codec_tag */
1797 if (id == CODEC_ID_SOL_DPCM) {
1799 return frame_bytes / ch;
1801 return frame_bytes * 2 / ch;
1806 /* calc from frame_bytes, channels, and block_align */
1807 int blocks = frame_bytes / ba;
1808 switch (avctx->codec_id) {
1809 case CODEC_ID_ADPCM_IMA_WAV:
1810 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1811 case CODEC_ID_ADPCM_IMA_DK3:
1812 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1813 case CODEC_ID_ADPCM_IMA_DK4:
1814 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1815 case CODEC_ID_ADPCM_MS:
1816 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1821 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1822 switch (avctx->codec_id) {
1823 case CODEC_ID_PCM_DVD:
1824 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1825 case CODEC_ID_PCM_BLURAY:
1826 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1827 case CODEC_ID_S302M:
1828 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1838 int ff_thread_init(AVCodecContext *s){
1843 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1857 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1859 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1863 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1865 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1866 "version to the newest one from Git. If the problem still "
1867 "occurs, it means that your file has a feature which has not "
1868 "been implemented.\n", feature);
1870 av_log_ask_for_sample(avc, NULL);
1873 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1875 va_list argument_list;
1877 va_start(argument_list, msg);
1880 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1881 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1882 "of this file to ftp://upload.libav.org/incoming/ "
1883 "and contact the libav-devel mailing list.\n");
1885 va_end(argument_list);
1888 static AVHWAccel *first_hwaccel = NULL;
1890 void av_register_hwaccel(AVHWAccel *hwaccel)
1892 AVHWAccel **p = &first_hwaccel;
1896 hwaccel->next = NULL;
1899 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1901 return hwaccel ? hwaccel->next : first_hwaccel;
1904 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1906 AVHWAccel *hwaccel=NULL;
1908 while((hwaccel= av_hwaccel_next(hwaccel))){
1909 if ( hwaccel->id == codec_id
1910 && hwaccel->pix_fmt == pix_fmt)
1916 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1918 if (ff_lockmgr_cb) {
1919 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1921 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1927 if (ff_lockmgr_cb) {
1928 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1930 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1936 int avpriv_lock_avformat(void)
1938 if (ff_lockmgr_cb) {
1939 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1945 int avpriv_unlock_avformat(void)
1947 if (ff_lockmgr_cb) {
1948 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1954 unsigned int avpriv_toupper4(unsigned int x)
1956 return toupper( x &0xFF)
1957 + (toupper((x>>8 )&0xFF)<<8 )
1958 + (toupper((x>>16)&0xFF)<<16)
1959 + (toupper((x>>24)&0xFF)<<24);
1964 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1967 return avctx->get_buffer(avctx, f);
1970 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1972 f->owner->release_buffer(f->owner, f);
1975 void ff_thread_finish_setup(AVCodecContext *avctx)
1979 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1983 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1989 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1991 if (codec_id <= CODEC_ID_NONE)
1992 return AVMEDIA_TYPE_UNKNOWN;
1993 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1994 return AVMEDIA_TYPE_VIDEO;
1995 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1996 return AVMEDIA_TYPE_AUDIO;
1997 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1998 return AVMEDIA_TYPE_SUBTITLE;
2000 return AVMEDIA_TYPE_UNKNOWN;
2003 int avcodec_is_open(AVCodecContext *s)
2005 return !!s->internal;