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 /* encoder management */
84 static AVCodec *first_avcodec = NULL;
86 AVCodec *av_codec_next(AVCodec *c){
88 else return first_avcodec;
91 #if !FF_API_AVCODEC_INIT
94 void avcodec_init(void)
96 static int initialized = 0;
102 dsputil_static_init();
105 static av_always_inline int codec_is_encoder(AVCodec *codec)
107 return codec && (codec->encode || codec->encode2);
110 static av_always_inline int codec_is_decoder(AVCodec *codec)
112 return codec && codec->decode;
115 void avcodec_register(AVCodec *codec)
120 while (*p != NULL) p = &(*p)->next;
124 if (codec->init_static_data)
125 codec->init_static_data(codec);
128 unsigned avcodec_get_edge_width(void)
133 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
134 s->coded_width = width;
135 s->coded_height= height;
136 s->width = -((-width )>>s->lowres);
137 s->height= -((-height)>>s->lowres);
140 #define INTERNAL_BUFFER_SIZE (32+1)
142 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
143 int linesize_align[AV_NUM_DATA_POINTERS])
150 case PIX_FMT_YUV420P:
151 case PIX_FMT_YUYV422:
152 case PIX_FMT_UYVY422:
153 case PIX_FMT_YUV422P:
154 case PIX_FMT_YUV440P:
155 case PIX_FMT_YUV444P:
158 case PIX_FMT_GRAY16BE:
159 case PIX_FMT_GRAY16LE:
160 case PIX_FMT_YUVJ420P:
161 case PIX_FMT_YUVJ422P:
162 case PIX_FMT_YUVJ440P:
163 case PIX_FMT_YUVJ444P:
164 case PIX_FMT_YUVA420P:
165 case PIX_FMT_YUV420P9LE:
166 case PIX_FMT_YUV420P9BE:
167 case PIX_FMT_YUV420P10LE:
168 case PIX_FMT_YUV420P10BE:
169 case PIX_FMT_YUV422P9LE:
170 case PIX_FMT_YUV422P9BE:
171 case PIX_FMT_YUV422P10LE:
172 case PIX_FMT_YUV422P10BE:
173 case PIX_FMT_YUV444P9LE:
174 case PIX_FMT_YUV444P9BE:
175 case PIX_FMT_YUV444P10LE:
176 case PIX_FMT_YUV444P10BE:
177 case PIX_FMT_GBRP9LE:
178 case PIX_FMT_GBRP9BE:
179 case PIX_FMT_GBRP10LE:
180 case PIX_FMT_GBRP10BE:
181 w_align = 16; //FIXME assume 16 pixel per macroblock
182 h_align = 16 * 2; // interlaced needs 2 macroblocks height
184 case PIX_FMT_YUV411P:
185 case PIX_FMT_UYYVYY411:
189 case PIX_FMT_YUV410P:
190 if(s->codec_id == CODEC_ID_SVQ1){
195 if(s->codec_id == CODEC_ID_RPZA){
202 if(s->codec_id == CODEC_ID_SMC){
208 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
219 *width = FFALIGN(*width , w_align);
220 *height= FFALIGN(*height, h_align);
221 if(s->codec_id == CODEC_ID_H264 || s->lowres)
222 *height+=2; // some of the optimized chroma MC reads one line too much
223 // which is also done in mpeg decoders with lowres > 0
225 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
226 linesize_align[i] = STRIDE_ALIGN;
227 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
228 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
229 //picture size unneccessarily in some cases. The solution here is not
230 //pretty and better ideas are welcome!
232 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
233 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
234 s->codec_id == CODEC_ID_VP6A) {
235 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
236 linesize_align[i] = 16;
241 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
242 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
243 int linesize_align[AV_NUM_DATA_POINTERS];
245 avcodec_align_dimensions2(s, width, height, linesize_align);
246 align = FFMAX(linesize_align[0], linesize_align[3]);
247 linesize_align[1] <<= chroma_shift;
248 linesize_align[2] <<= chroma_shift;
249 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
250 *width=FFALIGN(*width, align);
253 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
254 enum AVSampleFormat sample_fmt, const uint8_t *buf,
255 int buf_size, int align)
257 int ch, planar, needed_size, ret = 0;
259 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
260 frame->nb_samples, sample_fmt,
262 if (buf_size < needed_size)
263 return AVERROR(EINVAL);
265 planar = av_sample_fmt_is_planar(sample_fmt);
266 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
267 if (!(frame->extended_data = av_mallocz(nb_channels *
268 sizeof(*frame->extended_data))))
269 return AVERROR(ENOMEM);
271 frame->extended_data = frame->data;
274 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
275 buf, nb_channels, frame->nb_samples,
276 sample_fmt, align)) < 0) {
277 if (frame->extended_data != frame->data)
278 av_free(frame->extended_data);
281 if (frame->extended_data != frame->data) {
282 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
283 frame->data[ch] = frame->extended_data[ch];
289 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
291 AVCodecInternal *avci = avctx->internal;
295 buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
296 frame->nb_samples, avctx->sample_fmt,
299 return AVERROR(EINVAL);
301 /* allocate InternalBuffer if needed */
303 avci->buffer = av_mallocz(sizeof(InternalBuffer));
305 return AVERROR(ENOMEM);
309 /* if there is a previously-used internal buffer, check its size and
310 channel count to see if we can reuse it */
311 if (buf->extended_data) {
312 /* if current buffer is too small, free it */
313 if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
314 av_free(buf->extended_data[0]);
315 if (buf->extended_data != buf->data)
316 av_free(&buf->extended_data);
317 buf->extended_data = NULL;
320 /* if number of channels has changed, reset and/or free extended data
321 pointers but leave data buffer in buf->data[0] for reuse */
322 if (buf->nb_channels != avctx->channels) {
323 if (buf->extended_data != buf->data)
324 av_free(buf->extended_data);
325 buf->extended_data = NULL;
329 /* if there is no previous buffer or the previous buffer cannot be used
330 as-is, allocate a new buffer and/or rearrange the channel pointers */
331 if (!buf->extended_data) {
333 if (!(buf->data[0] = av_mallocz(buf_size)))
334 return AVERROR(ENOMEM);
335 buf->audio_data_size = buf_size;
337 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
338 avctx->sample_fmt, buf->data[0],
339 buf->audio_data_size, 32)))
342 if (frame->extended_data == frame->data)
343 buf->extended_data = buf->data;
345 buf->extended_data = frame->extended_data;
346 memcpy(buf->data, frame->data, sizeof(frame->data));
347 buf->linesize[0] = frame->linesize[0];
348 buf->nb_channels = avctx->channels;
350 /* copy InternalBuffer info to the AVFrame */
351 frame->extended_data = buf->extended_data;
352 frame->linesize[0] = buf->linesize[0];
353 memcpy(frame->data, buf->data, sizeof(frame->data));
356 frame->type = FF_BUFFER_TYPE_INTERNAL;
358 if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
359 else frame->pkt_pts = AV_NOPTS_VALUE;
360 frame->reordered_opaque = avctx->reordered_opaque;
362 if (avctx->debug & FF_DEBUG_BUFFERS)
363 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
364 "internal audio buffer used\n", frame);
369 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
375 AVCodecInternal *avci = s->internal;
377 if(pic->data[0]!=NULL) {
378 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
381 if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
382 av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
386 if(av_image_check_size(w, h, 0, s))
390 avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
391 sizeof(InternalBuffer));
394 buf = &avci->buffer[avci->buffer_count];
396 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
397 if(s->active_thread_type&FF_THREAD_FRAME) {
398 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
402 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
403 av_freep(&buf->base[i]);
409 int h_chroma_shift, v_chroma_shift;
414 int stride_align[AV_NUM_DATA_POINTERS];
415 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
417 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
419 avcodec_align_dimensions2(s, &w, &h, stride_align);
421 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
427 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
428 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
429 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
430 // increase alignment of w for next try (rhs gives the lowest bit set in w)
435 unaligned |= picture.linesize[i] % stride_align[i];
439 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
443 for (i=0; i<3 && picture.data[i+1]; i++)
444 size[i] = picture.data[i+1] - picture.data[i];
445 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
447 memset(buf->base, 0, sizeof(buf->base));
448 memset(buf->data, 0, sizeof(buf->data));
450 for(i=0; i<4 && size[i]; i++){
451 const int h_shift= i==0 ? 0 : h_chroma_shift;
452 const int v_shift= i==0 ? 0 : v_chroma_shift;
454 buf->linesize[i]= picture.linesize[i];
456 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
457 if(buf->base[i]==NULL) return -1;
458 memset(buf->base[i], 128, size[i]);
460 // no edge if EDGE EMU or not planar YUV
461 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
462 buf->data[i] = buf->base[i];
464 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
466 for (; i < AV_NUM_DATA_POINTERS; i++) {
467 buf->base[i] = buf->data[i] = NULL;
468 buf->linesize[i] = 0;
470 if(size[1] && !size[2])
471 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
472 buf->width = s->width;
473 buf->height = s->height;
474 buf->pix_fmt= s->pix_fmt;
476 pic->type= FF_BUFFER_TYPE_INTERNAL;
478 for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
479 pic->base[i]= buf->base[i];
480 pic->data[i]= buf->data[i];
481 pic->linesize[i]= buf->linesize[i];
483 pic->extended_data = pic->data;
484 avci->buffer_count++;
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 /* If internal buffer type return the same buffer */
559 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
560 if(s->pkt) pic->pkt_pts= s->pkt->pts;
561 else pic->pkt_pts= AV_NOPTS_VALUE;
562 pic->reordered_opaque= s->reordered_opaque;
567 * Not internal type and reget_buffer not overridden, emulate cr buffer
570 for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
571 pic->data[i] = pic->base[i] = NULL;
573 /* Allocate new frame */
574 if (s->get_buffer(s, pic))
576 /* Copy image data from old buffer to new buffer */
577 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
579 s->release_buffer(s, &temp_pic); // Release old frame
583 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
586 for(i=0; i<count; i++){
587 int r= func(c, (char*)arg + i*size);
593 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
596 for(i=0; i<count; i++){
597 int r= func(c, arg, i, 0);
603 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
604 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
609 void avcodec_get_frame_defaults(AVFrame *pic){
610 memset(pic, 0, sizeof(AVFrame));
612 pic->pts= AV_NOPTS_VALUE;
614 pic->sample_aspect_ratio = (AVRational){0, 1};
615 pic->format = -1; /* unknown */
618 AVFrame *avcodec_alloc_frame(void){
619 AVFrame *pic= av_malloc(sizeof(AVFrame));
621 if(pic==NULL) return NULL;
623 avcodec_get_frame_defaults(pic);
628 #if FF_API_AVCODEC_OPEN
629 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
631 return avcodec_open2(avctx, codec, NULL);
635 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
638 AVDictionary *tmp = NULL;
640 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
641 return AVERROR(EINVAL);
644 av_dict_copy(&tmp, *options, 0);
646 /* If there is a user-supplied mutex locking routine, call it. */
648 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
652 entangled_thread_counter++;
653 if(entangled_thread_counter != 1){
654 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
659 if(avctx->codec || !codec) {
660 ret = AVERROR(EINVAL);
664 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
665 if (!avctx->internal) {
666 ret = AVERROR(ENOMEM);
670 if (codec->priv_data_size > 0) {
671 if(!avctx->priv_data){
672 avctx->priv_data = av_mallocz(codec->priv_data_size);
673 if (!avctx->priv_data) {
674 ret = AVERROR(ENOMEM);
677 if (codec->priv_class) {
678 *(AVClass**)avctx->priv_data= codec->priv_class;
679 av_opt_set_defaults(avctx->priv_data);
682 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
685 avctx->priv_data = NULL;
687 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
690 if(avctx->coded_width && avctx->coded_height)
691 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
692 else if(avctx->width && avctx->height)
693 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
695 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
696 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
697 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
698 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
699 avcodec_set_dimensions(avctx, 0, 0);
702 /* if the decoder init function was already called previously,
703 free the already allocated subtitle_header before overwriting it */
704 if (codec_is_decoder(codec))
705 av_freep(&avctx->subtitle_header);
707 #define SANE_NB_CHANNELS 128U
708 if (avctx->channels > SANE_NB_CHANNELS) {
709 ret = AVERROR(EINVAL);
713 avctx->codec = codec;
714 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
715 avctx->codec_id == CODEC_ID_NONE) {
716 avctx->codec_type = codec->type;
717 avctx->codec_id = codec->id;
719 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
720 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
721 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
722 ret = AVERROR(EINVAL);
725 avctx->frame_number = 0;
728 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
729 avctx->error_recognition, avctx->err_recognition);
730 /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
731 FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
732 avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
733 av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
734 avctx->error_recognition, avctx->err_recognition);
737 if (HAVE_THREADS && !avctx->thread_opaque) {
738 ret = ff_thread_init(avctx);
743 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
744 avctx->thread_count = 1;
746 if (avctx->codec->max_lowres < avctx->lowres) {
747 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
748 avctx->codec->max_lowres);
749 ret = AVERROR(EINVAL);
752 if (codec_is_encoder(avctx->codec)) {
754 if (avctx->codec->sample_fmts) {
755 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
756 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
758 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
759 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
760 ret = AVERROR(EINVAL);
764 if (avctx->codec->supported_samplerates) {
765 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
766 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
768 if (avctx->codec->supported_samplerates[i] == 0) {
769 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
770 ret = AVERROR(EINVAL);
774 if (avctx->codec->channel_layouts) {
775 if (!avctx->channel_layout) {
776 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
778 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
779 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
781 if (avctx->codec->channel_layouts[i] == 0) {
782 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
783 ret = AVERROR(EINVAL);
788 if (avctx->channel_layout && avctx->channels) {
789 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
790 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
791 ret = AVERROR(EINVAL);
794 } else if (avctx->channel_layout) {
795 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
799 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
800 ret = avctx->codec->init(avctx);
806 entangled_thread_counter--;
808 /* Release any user-supplied mutex. */
810 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
813 av_dict_free(options);
820 av_freep(&avctx->priv_data);
821 av_freep(&avctx->internal);
826 int ff_alloc_packet(AVPacket *avpkt, int size)
828 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
829 return AVERROR(EINVAL);
835 if (avpkt->size < size)
836 return AVERROR(EINVAL);
838 pkt_data = avpkt->data;
839 pkt_size = avpkt->size;
840 av_init_packet(avpkt);
841 avpkt->data = pkt_data;
842 avpkt->size = pkt_size;
845 return av_new_packet(avpkt, size);
849 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
851 const AVFrame *frame,
855 int user_packet = !!avpkt->data;
858 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
859 av_init_packet(avpkt);
864 /* check for valid frame size */
866 nb_samples = frame->nb_samples;
867 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
868 if (nb_samples > avctx->frame_size)
869 return AVERROR(EINVAL);
870 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
871 if (nb_samples != avctx->frame_size)
872 return AVERROR(EINVAL);
875 nb_samples = avctx->frame_size;
878 if (avctx->codec->encode2) {
880 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
881 if (!ret && *got_packet_ptr &&
882 !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
883 avpkt->pts = frame->pts;
884 avpkt->duration = av_rescale_q(frame->nb_samples,
885 (AVRational){ 1, avctx->sample_rate },
889 /* for compatibility with encoders not supporting encode2(), we need to
890 allocate a packet buffer if the user has not provided one or check
891 the size otherwise */
893 int buf_size = avpkt->size;
895 if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
896 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
897 buf_size = nb_samples * avctx->channels *
898 av_get_bits_per_sample(avctx->codec_id) / 8;
900 /* this is a guess as to the required size.
901 if an encoder needs more than this, it should probably
902 implement encode2() */
903 buf_size = 2 * avctx->frame_size * avctx->channels *
904 av_get_bytes_per_sample(avctx->sample_fmt);
905 buf_size += FF_MIN_BUFFER_SIZE;
908 if ((ret = ff_alloc_packet(avpkt, buf_size)))
911 /* Encoders using AVCodec.encode() that support
912 CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
913 the smaller size when encoding the last frame.
914 This code can be removed once all encoders supporting
915 CODEC_CAP_SMALL_LAST_FRAME use encode2() */
916 if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
917 nb_samples < avctx->frame_size) {
918 fs_tmp = avctx->frame_size;
919 avctx->frame_size = nb_samples;
922 /* encode the frame */
923 ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
924 frame ? frame->data[0] : NULL);
927 /* no output. if the packet data was allocated by libavcodec,
930 av_freep(&avpkt->data);
932 if (avctx->coded_frame)
933 avpkt->pts = avctx->coded_frame->pts;
934 /* Set duration for final small packet. This can be removed
935 once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
938 avpkt->duration = av_rescale_q(avctx->frame_size,
939 (AVRational){ 1, avctx->sample_rate },
944 *got_packet_ptr = (ret > 0);
949 avctx->frame_size = fs_tmp;
952 avctx->frame_number++;
954 /* NOTE: if we add any audio encoders which output non-keyframe packets,
955 this needs to be moved to the encoders, but for now we can do it
956 here to simplify things */
957 avpkt->flags |= AV_PKT_FLAG_KEY;
962 #if FF_API_OLD_DECODE_AUDIO
963 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
964 uint8_t *buf, int buf_size,
965 const short *samples)
970 int ret, samples_size, got_packet;
972 av_init_packet(&pkt);
978 avcodec_get_frame_defaults(frame);
980 if (avctx->frame_size) {
981 frame->nb_samples = avctx->frame_size;
983 /* if frame_size is not set, the number of samples must be
984 calculated from the buffer size */
986 if (!av_get_bits_per_sample(avctx->codec_id)) {
987 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
988 "support this codec\n");
989 return AVERROR(EINVAL);
991 nb_samples = (int64_t)buf_size * 8 /
992 (av_get_bits_per_sample(avctx->codec_id) *
994 if (nb_samples >= INT_MAX)
995 return AVERROR(EINVAL);
996 frame->nb_samples = nb_samples;
999 /* it is assumed that the samples buffer is large enough based on the
1000 relevant parameters */
1001 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1003 avctx->sample_fmt, 1);
1004 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1006 samples, samples_size, 1)))
1009 /* fabricate frame pts from sample count.
1010 this is needed because the avcodec_encode_audio() API does not have
1011 a way for the user to provide pts */
1012 frame->pts = av_rescale_q(avctx->internal->sample_count,
1013 (AVRational){ 1, avctx->sample_rate },
1015 avctx->internal->sample_count += frame->nb_samples;
1021 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1022 if (!ret && got_packet && avctx->coded_frame) {
1023 avctx->coded_frame->pts = pkt.pts;
1024 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 if (frame && frame->extended_data != frame->data)
1036 av_free(frame->extended_data);
1038 return ret ? ret : pkt.size;
1042 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1043 const AVFrame *pict)
1045 if(buf_size < FF_MIN_BUFFER_SIZE){
1046 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1049 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
1051 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
1052 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
1053 avctx->frame_number++;
1054 emms_c(); //needed to avoid an emms_c() call before every return;
1061 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1062 const AVSubtitle *sub)
1065 if(sub->start_display_time) {
1066 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1069 if(sub->num_rects == 0 || !sub->rects)
1071 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1072 avctx->frame_number++;
1076 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1079 const uint8_t *data;
1082 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1085 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1086 if (!data || size < 4)
1088 flags = bytestream_get_le32(&data);
1090 if (size < 4) /* Required for any of the changes */
1092 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1093 avctx->channels = bytestream_get_le32(&data);
1096 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1099 avctx->channel_layout = bytestream_get_le64(&data);
1104 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1105 avctx->sample_rate = bytestream_get_le32(&data);
1108 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1111 avctx->width = bytestream_get_le32(&data);
1112 avctx->height = bytestream_get_le32(&data);
1113 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1118 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1119 int *got_picture_ptr,
1124 *got_picture_ptr= 0;
1125 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1129 apply_param_change(avctx, avpkt);
1131 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1132 if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1133 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1136 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1138 picture->pkt_dts= avpkt->dts;
1139 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1140 picture->width = avctx->width;
1141 picture->height = avctx->height;
1142 picture->format = avctx->pix_fmt;
1145 emms_c(); //needed to avoid an emms_c() call before every return;
1147 if (*got_picture_ptr)
1148 avctx->frame_number++;
1155 #if FF_API_OLD_DECODE_AUDIO
1156 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1157 int *frame_size_ptr,
1161 int ret, got_frame = 0;
1163 if (avctx->get_buffer != avcodec_default_get_buffer) {
1164 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1165 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1166 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1167 "avcodec_decode_audio4()\n");
1168 avctx->get_buffer = avcodec_default_get_buffer;
1171 ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1173 if (ret >= 0 && got_frame) {
1175 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1176 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1178 avctx->sample_fmt, 1);
1179 if (*frame_size_ptr < data_size) {
1180 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1181 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1182 return AVERROR(EINVAL);
1185 memcpy(samples, frame.extended_data[0], plane_size);
1187 if (planar && avctx->channels > 1) {
1188 uint8_t *out = ((uint8_t *)samples) + plane_size;
1189 for (ch = 1; ch < avctx->channels; ch++) {
1190 memcpy(out, frame.extended_data[ch], plane_size);
1194 *frame_size_ptr = data_size;
1196 *frame_size_ptr = 0;
1202 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1213 if (!avpkt->data && avpkt->size) {
1214 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1215 return AVERROR(EINVAL);
1218 apply_param_change(avctx, avpkt);
1220 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1221 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1222 if (ret >= 0 && *got_frame_ptr) {
1223 avctx->frame_number++;
1224 frame->pkt_dts = avpkt->dts;
1225 if (frame->format == AV_SAMPLE_FMT_NONE)
1226 frame->format = avctx->sample_fmt;
1232 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1240 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1242 avctx->frame_number++;
1246 void avsubtitle_free(AVSubtitle *sub)
1250 for (i = 0; i < sub->num_rects; i++)
1252 av_freep(&sub->rects[i]->pict.data[0]);
1253 av_freep(&sub->rects[i]->pict.data[1]);
1254 av_freep(&sub->rects[i]->pict.data[2]);
1255 av_freep(&sub->rects[i]->pict.data[3]);
1256 av_freep(&sub->rects[i]->text);
1257 av_freep(&sub->rects[i]->ass);
1258 av_freep(&sub->rects[i]);
1261 av_freep(&sub->rects);
1263 memset(sub, 0, sizeof(AVSubtitle));
1266 av_cold int avcodec_close(AVCodecContext *avctx)
1268 /* If there is a user-supplied mutex locking routine, call it. */
1269 if (ff_lockmgr_cb) {
1270 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1274 entangled_thread_counter++;
1275 if(entangled_thread_counter != 1){
1276 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1277 entangled_thread_counter--;
1281 if (HAVE_THREADS && avctx->thread_opaque)
1282 ff_thread_free(avctx);
1283 if (avctx->codec && avctx->codec->close)
1284 avctx->codec->close(avctx);
1285 avcodec_default_free_buffers(avctx);
1286 avctx->coded_frame = NULL;
1287 av_freep(&avctx->internal);
1288 if (avctx->codec && avctx->codec->priv_class)
1289 av_opt_free(avctx->priv_data);
1291 av_freep(&avctx->priv_data);
1292 if (codec_is_encoder(avctx->codec))
1293 av_freep(&avctx->extradata);
1294 avctx->codec = NULL;
1295 avctx->active_thread_type = 0;
1296 entangled_thread_counter--;
1298 /* Release any user-supplied mutex. */
1299 if (ff_lockmgr_cb) {
1300 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1305 AVCodec *avcodec_find_encoder(enum CodecID id)
1307 AVCodec *p, *experimental=NULL;
1310 if (codec_is_encoder(p) && p->id == id) {
1311 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1318 return experimental;
1321 AVCodec *avcodec_find_encoder_by_name(const char *name)
1328 if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
1335 AVCodec *avcodec_find_decoder(enum CodecID id)
1340 if (codec_is_decoder(p) && p->id == id)
1347 AVCodec *avcodec_find_decoder_by_name(const char *name)
1354 if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
1361 static int get_bit_rate(AVCodecContext *ctx)
1364 int bits_per_sample;
1366 switch(ctx->codec_type) {
1367 case AVMEDIA_TYPE_VIDEO:
1368 case AVMEDIA_TYPE_DATA:
1369 case AVMEDIA_TYPE_SUBTITLE:
1370 case AVMEDIA_TYPE_ATTACHMENT:
1371 bit_rate = ctx->bit_rate;
1373 case AVMEDIA_TYPE_AUDIO:
1374 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1375 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1384 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1386 int i, len, ret = 0;
1388 for (i = 0; i < 4; i++) {
1389 len = snprintf(buf, buf_size,
1390 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1392 buf_size = buf_size > len ? buf_size - len : 0;
1399 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1401 const char *codec_name;
1402 const char *profile = NULL;
1406 AVRational display_aspect_ratio;
1409 p = avcodec_find_encoder(enc->codec_id);
1411 p = avcodec_find_decoder(enc->codec_id);
1414 codec_name = p->name;
1415 profile = av_get_profile_name(p, enc->profile);
1416 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1417 /* fake mpeg2 transport stream codec (currently not
1419 codec_name = "mpeg2ts";
1420 } else if (enc->codec_name[0] != '\0') {
1421 codec_name = enc->codec_name;
1423 /* output avi tags */
1425 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1426 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1430 switch(enc->codec_type) {
1431 case AVMEDIA_TYPE_VIDEO:
1432 snprintf(buf, buf_size,
1434 codec_name, enc->mb_decision ? " (hq)" : "");
1436 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1438 if (enc->pix_fmt != PIX_FMT_NONE) {
1439 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1441 av_get_pix_fmt_name(enc->pix_fmt));
1444 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1446 enc->width, enc->height);
1447 if (enc->sample_aspect_ratio.num) {
1448 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1449 enc->width*enc->sample_aspect_ratio.num,
1450 enc->height*enc->sample_aspect_ratio.den,
1452 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1453 " [PAR %d:%d DAR %d:%d]",
1454 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1455 display_aspect_ratio.num, display_aspect_ratio.den);
1457 if(av_log_get_level() >= AV_LOG_DEBUG){
1458 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1459 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1461 enc->time_base.num/g, enc->time_base.den/g);
1465 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1466 ", q=%d-%d", enc->qmin, enc->qmax);
1469 case AVMEDIA_TYPE_AUDIO:
1470 snprintf(buf, buf_size,
1474 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1476 if (enc->sample_rate) {
1477 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1478 ", %d Hz", enc->sample_rate);
1480 av_strlcat(buf, ", ", buf_size);
1481 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1482 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1483 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1484 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1487 case AVMEDIA_TYPE_DATA:
1488 snprintf(buf, buf_size, "Data: %s", codec_name);
1490 case AVMEDIA_TYPE_SUBTITLE:
1491 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1493 case AVMEDIA_TYPE_ATTACHMENT:
1494 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1497 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1501 if (enc->flags & CODEC_FLAG_PASS1)
1502 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1504 if (enc->flags & CODEC_FLAG_PASS2)
1505 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1508 bitrate = get_bit_rate(enc);
1510 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1511 ", %d kb/s", bitrate / 1000);
1515 const char *av_get_profile_name(const AVCodec *codec, int profile)
1518 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1521 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1522 if (p->profile == profile)
1528 unsigned avcodec_version( void )
1530 return LIBAVCODEC_VERSION_INT;
1533 const char *avcodec_configuration(void)
1535 return LIBAV_CONFIGURATION;
1538 const char *avcodec_license(void)
1540 #define LICENSE_PREFIX "libavcodec license: "
1541 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1544 void avcodec_flush_buffers(AVCodecContext *avctx)
1546 if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1547 ff_thread_flush(avctx);
1548 else if(avctx->codec->flush)
1549 avctx->codec->flush(avctx);
1552 static void video_free_buffers(AVCodecContext *s)
1554 AVCodecInternal *avci = s->internal;
1560 if (avci->buffer_count)
1561 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1562 avci->buffer_count);
1563 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1564 InternalBuffer *buf = &avci->buffer[i];
1566 av_freep(&buf->base[j]);
1570 av_freep(&avci->buffer);
1572 avci->buffer_count=0;
1575 static void audio_free_buffers(AVCodecContext *avctx)
1577 AVCodecInternal *avci = avctx->internal;
1578 InternalBuffer *buf;
1584 if (buf->extended_data) {
1585 av_free(buf->extended_data[0]);
1586 if (buf->extended_data != buf->data)
1587 av_free(buf->extended_data);
1589 av_freep(&avci->buffer);
1592 void avcodec_default_free_buffers(AVCodecContext *avctx)
1594 switch (avctx->codec_type) {
1595 case AVMEDIA_TYPE_VIDEO:
1596 video_free_buffers(avctx);
1598 case AVMEDIA_TYPE_AUDIO:
1599 audio_free_buffers(avctx);
1606 #if FF_API_OLD_FF_PICT_TYPES
1607 char av_get_pict_type_char(int pict_type){
1608 return av_get_picture_type_char(pict_type);
1612 int av_get_bits_per_sample(enum CodecID codec_id){
1614 case CODEC_ID_ADPCM_SBPRO_2:
1616 case CODEC_ID_ADPCM_SBPRO_3:
1618 case CODEC_ID_ADPCM_SBPRO_4:
1619 case CODEC_ID_ADPCM_CT:
1620 case CODEC_ID_ADPCM_IMA_WAV:
1621 case CODEC_ID_ADPCM_IMA_QT:
1622 case CODEC_ID_ADPCM_SWF:
1623 case CODEC_ID_ADPCM_MS:
1624 case CODEC_ID_ADPCM_YAMAHA:
1625 case CODEC_ID_ADPCM_G722:
1627 case CODEC_ID_PCM_ALAW:
1628 case CODEC_ID_PCM_MULAW:
1629 case CODEC_ID_PCM_S8:
1630 case CODEC_ID_PCM_U8:
1631 case CODEC_ID_PCM_ZORK:
1633 case CODEC_ID_PCM_S16BE:
1634 case CODEC_ID_PCM_S16LE:
1635 case CODEC_ID_PCM_S16LE_PLANAR:
1636 case CODEC_ID_PCM_U16BE:
1637 case CODEC_ID_PCM_U16LE:
1639 case CODEC_ID_PCM_S24DAUD:
1640 case CODEC_ID_PCM_S24BE:
1641 case CODEC_ID_PCM_S24LE:
1642 case CODEC_ID_PCM_U24BE:
1643 case CODEC_ID_PCM_U24LE:
1645 case CODEC_ID_PCM_S32BE:
1646 case CODEC_ID_PCM_S32LE:
1647 case CODEC_ID_PCM_U32BE:
1648 case CODEC_ID_PCM_U32LE:
1649 case CODEC_ID_PCM_F32BE:
1650 case CODEC_ID_PCM_F32LE:
1652 case CODEC_ID_PCM_F64BE:
1653 case CODEC_ID_PCM_F64LE:
1660 #if FF_API_OLD_SAMPLE_FMT
1661 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1662 return av_get_bytes_per_sample(sample_fmt) << 3;
1667 int ff_thread_init(AVCodecContext *s){
1672 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1686 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1688 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1692 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1694 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1695 "version to the newest one from Git. If the problem still "
1696 "occurs, it means that your file has a feature which has not "
1697 "been implemented.\n", feature);
1699 av_log_ask_for_sample(avc, NULL);
1702 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1704 va_list argument_list;
1706 va_start(argument_list, msg);
1709 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1710 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1711 "of this file to ftp://upload.libav.org/incoming/ "
1712 "and contact the libav-devel mailing list.\n");
1714 va_end(argument_list);
1717 static AVHWAccel *first_hwaccel = NULL;
1719 void av_register_hwaccel(AVHWAccel *hwaccel)
1721 AVHWAccel **p = &first_hwaccel;
1725 hwaccel->next = NULL;
1728 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1730 return hwaccel ? hwaccel->next : first_hwaccel;
1733 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1735 AVHWAccel *hwaccel=NULL;
1737 while((hwaccel= av_hwaccel_next(hwaccel))){
1738 if ( hwaccel->id == codec_id
1739 && hwaccel->pix_fmt == pix_fmt)
1745 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1747 if (ff_lockmgr_cb) {
1748 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1750 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1756 if (ff_lockmgr_cb) {
1757 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1759 if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1765 int avpriv_lock_avformat(void)
1767 if (ff_lockmgr_cb) {
1768 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1774 int avpriv_unlock_avformat(void)
1776 if (ff_lockmgr_cb) {
1777 if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1783 unsigned int avpriv_toupper4(unsigned int x)
1785 return toupper( x &0xFF)
1786 + (toupper((x>>8 )&0xFF)<<8 )
1787 + (toupper((x>>16)&0xFF)<<16)
1788 + (toupper((x>>24)&0xFF)<<24);
1793 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1796 return avctx->get_buffer(avctx, f);
1799 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1801 f->owner->release_buffer(f->owner, f);
1804 void ff_thread_finish_setup(AVCodecContext *avctx)
1808 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1812 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1818 #if FF_API_THREAD_INIT
1819 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1821 s->thread_count = thread_count;
1822 return ff_thread_init(s);
1826 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1828 if (codec_id <= CODEC_ID_NONE)
1829 return AVMEDIA_TYPE_UNKNOWN;
1830 else if (codec_id < CODEC_ID_FIRST_AUDIO)
1831 return AVMEDIA_TYPE_VIDEO;
1832 else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1833 return AVMEDIA_TYPE_AUDIO;
1834 else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1835 return AVMEDIA_TYPE_SUBTITLE;
1837 return AVMEDIA_TYPE_UNKNOWN;