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/avstring.h"
29 #include "libavutil/integer.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
37 #include "libavutil/opt.h"
38 #include "imgconvert.h"
40 #include "audioconvert.h"
47 static int volatile entangled_thread_counter=0;
48 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
49 static void *codec_mutex;
51 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
56 min_size= FFMAX(17*min_size/16 + 32, min_size);
58 ptr= av_realloc(ptr, min_size);
59 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
67 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
72 min_size= FFMAX(17*min_size/16 + 32, min_size);
74 *p = av_malloc(min_size);
75 if (!*p) min_size = 0;
79 /* encoder management */
80 static AVCodec *first_avcodec = NULL;
82 AVCodec *av_codec_next(AVCodec *c){
84 else return first_avcodec;
87 void avcodec_register(AVCodec *codec)
92 while (*p != NULL) p = &(*p)->next;
97 unsigned avcodec_get_edge_width(void)
102 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
103 s->coded_width = width;
104 s->coded_height= height;
105 s->width = -((-width )>>s->lowres);
106 s->height= -((-height)>>s->lowres);
109 typedef struct InternalBuffer{
115 enum PixelFormat pix_fmt;
118 #define INTERNAL_BUFFER_SIZE 32
120 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
125 case PIX_FMT_YUV420P:
126 case PIX_FMT_YUYV422:
127 case PIX_FMT_UYVY422:
128 case PIX_FMT_YUV422P:
129 case PIX_FMT_YUV440P:
130 case PIX_FMT_YUV444P:
132 case PIX_FMT_GRAY16BE:
133 case PIX_FMT_GRAY16LE:
134 case PIX_FMT_YUVJ420P:
135 case PIX_FMT_YUVJ422P:
136 case PIX_FMT_YUVJ440P:
137 case PIX_FMT_YUVJ444P:
138 case PIX_FMT_YUVA420P:
139 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
141 if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
142 h_align= 32; // interlaced is rounded up to 2 MBs
144 case PIX_FMT_YUV411P:
145 case PIX_FMT_UYYVYY411:
149 case PIX_FMT_YUV410P:
150 if(s->codec_id == CODEC_ID_SVQ1){
155 if(s->codec_id == CODEC_ID_RPZA){
162 if(s->codec_id == CODEC_ID_SMC){
168 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
179 *width = FFALIGN(*width , w_align);
180 *height= FFALIGN(*height, h_align);
181 if(s->codec_id == CODEC_ID_H264 || s->lowres)
182 *height+=2; // some of the optimized chroma MC reads one line too much
183 // which is also done in mpeg decoders with lowres > 0
188 linesize_align[3] = STRIDE_ALIGN;
189 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
190 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
191 //picture size unneccessarily in some cases. The solution here is not
192 //pretty and better ideas are welcome!
194 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
195 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
196 s->codec_id == CODEC_ID_VP6A) {
199 linesize_align[2] = 16;
204 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
205 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
206 int linesize_align[4];
208 avcodec_align_dimensions2(s, width, height, linesize_align);
209 align = FFMAX(linesize_align[0], linesize_align[3]);
210 linesize_align[1] <<= chroma_shift;
211 linesize_align[2] <<= chroma_shift;
212 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
213 *width=FFALIGN(*width, align);
216 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
223 if(pic->data[0]!=NULL) {
224 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
227 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
228 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
232 if(av_image_check_size(w, h, 0, s))
235 if(s->internal_buffer==NULL){
236 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
239 s->internal_buffer= av_fast_realloc(
241 &s->internal_buffer_size,
242 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
246 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
247 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
250 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
251 if(s->active_thread_type&FF_THREAD_FRAME) {
252 av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
257 av_freep(&buf->base[i]);
263 pic->age= *picture_number - buf->last_pic_num;
264 buf->last_pic_num= *picture_number;
266 int h_chroma_shift, v_chroma_shift;
273 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
275 avcodec_align_dimensions2(s, &w, &h, stride_align);
277 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
283 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
284 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
285 av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
286 // increase alignment of w for next try (rhs gives the lowest bit set in w)
291 unaligned |= picture.linesize[i] % stride_align[i];
295 tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
299 for (i=0; i<3 && picture.data[i+1]; i++)
300 size[i] = picture.data[i+1] - picture.data[i];
301 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
303 buf->last_pic_num= -256*256*256*64;
304 memset(buf->base, 0, sizeof(buf->base));
305 memset(buf->data, 0, sizeof(buf->data));
307 for(i=0; i<4 && size[i]; i++){
308 const int h_shift= i==0 ? 0 : h_chroma_shift;
309 const int v_shift= i==0 ? 0 : v_chroma_shift;
311 buf->linesize[i]= picture.linesize[i];
313 buf->base[i]= av_malloc(size[i]+16); //FIXME 16
314 if(buf->base[i]==NULL) return -1;
315 memset(buf->base[i], 128, size[i]);
317 // no edge if EDGE EMU or not planar YUV
318 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
319 buf->data[i] = buf->base[i];
321 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
323 if(size[1] && !size[2])
324 ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
325 buf->width = s->width;
326 buf->height = s->height;
327 buf->pix_fmt= s->pix_fmt;
328 pic->age= 256*256*256*64;
330 pic->type= FF_BUFFER_TYPE_INTERNAL;
333 pic->base[i]= buf->base[i];
334 pic->data[i]= buf->data[i];
335 pic->linesize[i]= buf->linesize[i];
337 s->internal_buffer_count++;
339 if(s->pkt) pic->pkt_pts= s->pkt->pts;
340 else pic->pkt_pts= AV_NOPTS_VALUE;
341 pic->reordered_opaque= s->reordered_opaque;
343 if(s->debug&FF_DEBUG_BUFFERS)
344 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
349 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
351 InternalBuffer *buf, *last;
353 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
354 assert(s->internal_buffer_count);
356 buf = NULL; /* avoids warning */
357 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
358 buf= &((InternalBuffer*)s->internal_buffer)[i];
359 if(buf->data[0] == pic->data[0])
362 assert(i < s->internal_buffer_count);
363 s->internal_buffer_count--;
364 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
366 FFSWAP(InternalBuffer, *buf, *last);
370 // pic->base[i]=NULL;
372 //printf("R%X\n", pic->opaque);
374 if(s->debug&FF_DEBUG_BUFFERS)
375 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
378 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
382 /* If no picture return a new buffer */
383 if(pic->data[0] == NULL) {
384 /* We will copy from buffer, so must be readable */
385 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
386 return s->get_buffer(s, pic);
389 /* If internal buffer type return the same buffer */
390 if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
391 if(s->pkt) pic->pkt_pts= s->pkt->pts;
392 else pic->pkt_pts= AV_NOPTS_VALUE;
393 pic->reordered_opaque= s->reordered_opaque;
398 * Not internal type and reget_buffer not overridden, emulate cr buffer
401 for(i = 0; i < 4; i++)
402 pic->data[i] = pic->base[i] = NULL;
404 /* Allocate new frame */
405 if (s->get_buffer(s, pic))
407 /* Copy image data from old buffer to new buffer */
408 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
410 s->release_buffer(s, &temp_pic); // Release old frame
414 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
417 for(i=0; i<count; i++){
418 int r= func(c, (char*)arg + i*size);
424 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
427 for(i=0; i<count; i++){
428 int r= func(c, arg, i, 0);
434 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
435 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
440 void avcodec_get_frame_defaults(AVFrame *pic){
441 memset(pic, 0, sizeof(AVFrame));
443 pic->pts= AV_NOPTS_VALUE;
447 AVFrame *avcodec_alloc_frame(void){
448 AVFrame *pic= av_malloc(sizeof(AVFrame));
450 if(pic==NULL) return NULL;
452 avcodec_get_frame_defaults(pic);
457 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
461 /* If there is a user-supplied mutex locking routine, call it. */
463 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
467 entangled_thread_counter++;
468 if(entangled_thread_counter != 1){
469 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
474 if(avctx->codec || !codec) {
475 ret = AVERROR(EINVAL);
479 if (codec->priv_data_size > 0) {
480 if(!avctx->priv_data){
481 avctx->priv_data = av_mallocz(codec->priv_data_size);
482 if (!avctx->priv_data) {
483 ret = AVERROR(ENOMEM);
486 if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
487 *(AVClass**)avctx->priv_data= codec->priv_class;
488 av_opt_set_defaults(avctx->priv_data);
492 avctx->priv_data = NULL;
495 if(avctx->coded_width && avctx->coded_height)
496 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
497 else if(avctx->width && avctx->height)
498 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
500 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
501 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
502 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
503 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
504 avcodec_set_dimensions(avctx, 0, 0);
507 /* if the decoder init function was already called previously,
508 free the already allocated subtitle_header before overwriting it */
510 av_freep(&avctx->subtitle_header);
512 #define SANE_NB_CHANNELS 128U
513 if (avctx->channels > SANE_NB_CHANNELS) {
514 ret = AVERROR(EINVAL);
518 avctx->codec = codec;
519 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
520 avctx->codec_id == CODEC_ID_NONE) {
521 avctx->codec_type = codec->type;
522 avctx->codec_id = codec->id;
524 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
525 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
526 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
527 ret = AVERROR(EINVAL);
530 avctx->frame_number = 0;
532 if (HAVE_THREADS && !avctx->thread_opaque) {
533 ret = ff_thread_init(avctx);
539 if (avctx->codec->max_lowres < avctx->lowres) {
540 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
541 avctx->codec->max_lowres);
542 ret = AVERROR(EINVAL);
545 if (avctx->codec->encode) {
547 if (avctx->codec->sample_fmts) {
548 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
549 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
551 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
552 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
553 ret = AVERROR(EINVAL);
557 if (avctx->codec->supported_samplerates) {
558 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
559 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
561 if (avctx->codec->supported_samplerates[i] == 0) {
562 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
563 ret = AVERROR(EINVAL);
567 if (avctx->codec->channel_layouts) {
568 if (!avctx->channel_layout) {
569 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
571 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
572 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
574 if (avctx->codec->channel_layouts[i] == 0) {
575 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
576 ret = AVERROR(EINVAL);
581 if (avctx->channel_layout && avctx->channels) {
582 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
583 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
584 ret = AVERROR(EINVAL);
587 } else if (avctx->channel_layout) {
588 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
592 if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
593 ret = avctx->codec->init(avctx);
599 entangled_thread_counter--;
601 /* Release any user-supplied mutex. */
603 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
607 av_freep(&avctx->priv_data);
612 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
613 const short *samples)
615 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
616 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
619 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
620 int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
621 avctx->frame_number++;
627 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
630 if(buf_size < FF_MIN_BUFFER_SIZE){
631 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
634 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
636 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
637 int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
638 avctx->frame_number++;
639 emms_c(); //needed to avoid an emms_c() call before every return;
646 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
647 const AVSubtitle *sub)
650 if(sub->start_display_time) {
651 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
654 if(sub->num_rects == 0 || !sub->rects)
656 ret = avctx->codec->encode(avctx, buf, buf_size, sub);
657 avctx->frame_number++;
661 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
662 int *got_picture_ptr,
668 if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
673 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
674 if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
675 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
678 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
680 picture->pkt_dts= avpkt->dts;
683 emms_c(); //needed to avoid an emms_c() call before every return;
685 if (*got_picture_ptr)
686 avctx->frame_number++;
693 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
701 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
702 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
703 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
704 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
707 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
708 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
709 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
713 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
714 avctx->frame_number++;
722 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
730 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
732 avctx->frame_number++;
736 void avsubtitle_free(AVSubtitle *sub)
740 for (i = 0; i < sub->num_rects; i++)
742 av_freep(&sub->rects[i]->pict.data[0]);
743 av_freep(&sub->rects[i]->pict.data[1]);
744 av_freep(&sub->rects[i]->pict.data[2]);
745 av_freep(&sub->rects[i]->pict.data[3]);
746 av_freep(&sub->rects[i]->text);
747 av_freep(&sub->rects[i]->ass);
748 av_freep(&sub->rects[i]);
751 av_freep(&sub->rects);
753 memset(sub, 0, sizeof(AVSubtitle));
756 av_cold int avcodec_close(AVCodecContext *avctx)
758 /* If there is a user-supplied mutex locking routine, call it. */
760 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
764 entangled_thread_counter++;
765 if(entangled_thread_counter != 1){
766 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
767 entangled_thread_counter--;
771 if (HAVE_THREADS && avctx->thread_opaque)
772 ff_thread_free(avctx);
773 if (avctx->codec && avctx->codec->close)
774 avctx->codec->close(avctx);
775 avcodec_default_free_buffers(avctx);
776 avctx->coded_frame = NULL;
777 av_freep(&avctx->priv_data);
778 if(avctx->codec && avctx->codec->encode)
779 av_freep(&avctx->extradata);
781 avctx->active_thread_type = 0;
782 entangled_thread_counter--;
784 /* Release any user-supplied mutex. */
786 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
791 AVCodec *avcodec_find_encoder(enum CodecID id)
793 AVCodec *p, *experimental=NULL;
796 if (p->encode != NULL && p->id == id) {
797 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
807 AVCodec *avcodec_find_encoder_by_name(const char *name)
814 if (p->encode != NULL && strcmp(name,p->name) == 0)
821 AVCodec *avcodec_find_decoder(enum CodecID id)
826 if (p->decode != NULL && p->id == id)
833 AVCodec *avcodec_find_decoder_by_name(const char *name)
840 if (p->decode != NULL && strcmp(name,p->name) == 0)
847 static int get_bit_rate(AVCodecContext *ctx)
852 switch(ctx->codec_type) {
853 case AVMEDIA_TYPE_VIDEO:
854 case AVMEDIA_TYPE_DATA:
855 case AVMEDIA_TYPE_SUBTITLE:
856 case AVMEDIA_TYPE_ATTACHMENT:
857 bit_rate = ctx->bit_rate;
859 case AVMEDIA_TYPE_AUDIO:
860 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
861 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
870 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
874 for (i = 0; i < 4; i++) {
875 len = snprintf(buf, buf_size,
876 isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
878 buf_size = buf_size > len ? buf_size - len : 0;
885 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
887 const char *codec_name;
888 const char *profile = NULL;
892 AVRational display_aspect_ratio;
895 p = avcodec_find_encoder(enc->codec_id);
897 p = avcodec_find_decoder(enc->codec_id);
900 codec_name = p->name;
901 profile = av_get_profile_name(p, enc->profile);
902 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
903 /* fake mpeg2 transport stream codec (currently not
905 codec_name = "mpeg2ts";
906 } else if (enc->codec_name[0] != '\0') {
907 codec_name = enc->codec_name;
909 /* output avi tags */
911 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
912 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
916 switch(enc->codec_type) {
917 case AVMEDIA_TYPE_VIDEO:
918 snprintf(buf, buf_size,
920 codec_name, enc->mb_decision ? " (hq)" : "");
922 snprintf(buf + strlen(buf), buf_size - strlen(buf),
924 if (enc->pix_fmt != PIX_FMT_NONE) {
925 snprintf(buf + strlen(buf), buf_size - strlen(buf),
927 avcodec_get_pix_fmt_name(enc->pix_fmt));
930 snprintf(buf + strlen(buf), buf_size - strlen(buf),
932 enc->width, enc->height);
933 if (enc->sample_aspect_ratio.num) {
934 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
935 enc->width*enc->sample_aspect_ratio.num,
936 enc->height*enc->sample_aspect_ratio.den,
938 snprintf(buf + strlen(buf), buf_size - strlen(buf),
939 " [PAR %d:%d DAR %d:%d]",
940 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
941 display_aspect_ratio.num, display_aspect_ratio.den);
943 if(av_log_get_level() >= AV_LOG_DEBUG){
944 int g= av_gcd(enc->time_base.num, enc->time_base.den);
945 snprintf(buf + strlen(buf), buf_size - strlen(buf),
947 enc->time_base.num/g, enc->time_base.den/g);
951 snprintf(buf + strlen(buf), buf_size - strlen(buf),
952 ", q=%d-%d", enc->qmin, enc->qmax);
955 case AVMEDIA_TYPE_AUDIO:
956 snprintf(buf, buf_size,
960 snprintf(buf + strlen(buf), buf_size - strlen(buf),
962 if (enc->sample_rate) {
963 snprintf(buf + strlen(buf), buf_size - strlen(buf),
964 ", %d Hz", enc->sample_rate);
966 av_strlcat(buf, ", ", buf_size);
967 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
968 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
969 snprintf(buf + strlen(buf), buf_size - strlen(buf),
970 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
973 case AVMEDIA_TYPE_DATA:
974 snprintf(buf, buf_size, "Data: %s", codec_name);
976 case AVMEDIA_TYPE_SUBTITLE:
977 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
979 case AVMEDIA_TYPE_ATTACHMENT:
980 snprintf(buf, buf_size, "Attachment: %s", codec_name);
983 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
987 if (enc->flags & CODEC_FLAG_PASS1)
988 snprintf(buf + strlen(buf), buf_size - strlen(buf),
990 if (enc->flags & CODEC_FLAG_PASS2)
991 snprintf(buf + strlen(buf), buf_size - strlen(buf),
994 bitrate = get_bit_rate(enc);
996 snprintf(buf + strlen(buf), buf_size - strlen(buf),
997 ", %d kb/s", bitrate / 1000);
1001 const char *av_get_profile_name(const AVCodec *codec, int profile)
1004 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1007 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1008 if (p->profile == profile)
1014 unsigned avcodec_version( void )
1016 return LIBAVCODEC_VERSION_INT;
1019 const char *avcodec_configuration(void)
1021 return LIBAV_CONFIGURATION;
1024 const char *avcodec_license(void)
1026 #define LICENSE_PREFIX "libavcodec license: "
1027 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1030 void avcodec_init(void)
1032 static int initialized = 0;
1034 if (initialized != 0)
1038 dsputil_static_init();
1041 void avcodec_flush_buffers(AVCodecContext *avctx)
1043 if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1044 ff_thread_flush(avctx);
1045 if(avctx->codec->flush)
1046 avctx->codec->flush(avctx);
1049 void avcodec_default_free_buffers(AVCodecContext *s){
1052 if(s->internal_buffer==NULL) return;
1054 if (s->internal_buffer_count)
1055 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1056 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1057 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1059 av_freep(&buf->base[j]);
1063 av_freep(&s->internal_buffer);
1065 s->internal_buffer_count=0;
1068 #if FF_API_OLD_FF_PICT_TYPES
1069 char av_get_pict_type_char(int pict_type){
1070 return av_get_picture_type_char(pict_type);
1074 int av_get_bits_per_sample(enum CodecID codec_id){
1076 case CODEC_ID_ADPCM_SBPRO_2:
1078 case CODEC_ID_ADPCM_SBPRO_3:
1080 case CODEC_ID_ADPCM_SBPRO_4:
1081 case CODEC_ID_ADPCM_CT:
1082 case CODEC_ID_ADPCM_IMA_WAV:
1083 case CODEC_ID_ADPCM_MS:
1084 case CODEC_ID_ADPCM_YAMAHA:
1086 case CODEC_ID_ADPCM_G722:
1087 case CODEC_ID_PCM_ALAW:
1088 case CODEC_ID_PCM_MULAW:
1089 case CODEC_ID_PCM_S8:
1090 case CODEC_ID_PCM_U8:
1091 case CODEC_ID_PCM_ZORK:
1093 case CODEC_ID_PCM_S16BE:
1094 case CODEC_ID_PCM_S16LE:
1095 case CODEC_ID_PCM_S16LE_PLANAR:
1096 case CODEC_ID_PCM_U16BE:
1097 case CODEC_ID_PCM_U16LE:
1099 case CODEC_ID_PCM_S24DAUD:
1100 case CODEC_ID_PCM_S24BE:
1101 case CODEC_ID_PCM_S24LE:
1102 case CODEC_ID_PCM_U24BE:
1103 case CODEC_ID_PCM_U24LE:
1105 case CODEC_ID_PCM_S32BE:
1106 case CODEC_ID_PCM_S32LE:
1107 case CODEC_ID_PCM_U32BE:
1108 case CODEC_ID_PCM_U32LE:
1109 case CODEC_ID_PCM_F32BE:
1110 case CODEC_ID_PCM_F32LE:
1112 case CODEC_ID_PCM_F64BE:
1113 case CODEC_ID_PCM_F64LE:
1120 #if FF_API_OLD_SAMPLE_FMT
1121 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1122 return av_get_bits_per_sample_fmt(sample_fmt);
1127 int ff_thread_init(AVCodecContext *s){
1132 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1146 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1148 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1152 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1154 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1155 "version to the newest one from Git. If the problem still "
1156 "occurs, it means that your file has a feature which has not "
1157 "been implemented.\n", feature);
1159 av_log_ask_for_sample(avc, NULL);
1162 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1164 va_list argument_list;
1166 va_start(argument_list, msg);
1169 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1170 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1171 "of this file to ftp://upload.libav.org/incoming/ "
1172 "and contact the libav-devel mailing list.\n");
1174 va_end(argument_list);
1177 static AVHWAccel *first_hwaccel = NULL;
1179 void av_register_hwaccel(AVHWAccel *hwaccel)
1181 AVHWAccel **p = &first_hwaccel;
1185 hwaccel->next = NULL;
1188 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1190 return hwaccel ? hwaccel->next : first_hwaccel;
1193 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1195 AVHWAccel *hwaccel=NULL;
1197 while((hwaccel= av_hwaccel_next(hwaccel))){
1198 if ( hwaccel->id == codec_id
1199 && hwaccel->pix_fmt == pix_fmt)
1205 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1207 if (ff_lockmgr_cb) {
1208 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1214 if (ff_lockmgr_cb) {
1215 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1221 unsigned int ff_toupper4(unsigned int x)
1223 return toupper( x &0xFF)
1224 + (toupper((x>>8 )&0xFF)<<8 )
1225 + (toupper((x>>16)&0xFF)<<16)
1226 + (toupper((x>>24)&0xFF)<<24);
1231 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1234 return avctx->get_buffer(avctx, f);
1237 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1239 f->owner->release_buffer(f->owner, f);
1242 void ff_thread_finish_setup(AVCodecContext *avctx)
1246 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1250 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1256 #if FF_API_THREAD_INIT
1257 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1259 s->thread_count = thread_count;
1260 return ff_thread_init(s);