utils.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:24k
源码类别:

Audio

开发平台:

Visual C++

  1. /*
  2.  * utils for libavcodec
  3.  * Copyright (c) 2001 Fabrice Bellard.
  4.  * Copyright (c) 2003 Michel Bardiaux for the av_log API
  5.  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  */
  21.  
  22. /**
  23.  * @file utils.c
  24.  * utils.
  25.  */
  26.  
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "mpegvideo.h"
  30. //#include "integer.h" //Del by ty
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. static void avcodec_default_free_buffers(AVCodecContext *s);
  34. void *av_mallocz(unsigned int size)
  35. {
  36.     void *ptr;
  37.     
  38.     ptr = av_malloc(size);
  39.     if (!ptr)
  40.         return NULL;
  41.     memset(ptr, 0, size);
  42.     return ptr;
  43. }
  44. char *av_strdup(const char *s)
  45. {
  46.     char *ptr;
  47.     int len;
  48.     len = strlen(s) + 1;
  49.     ptr = av_malloc(len);
  50.     if (!ptr)
  51.         return NULL;
  52.     memcpy(ptr, s, len);
  53.     return ptr;
  54. }
  55. /**
  56.  * realloc which does nothing if the block is large enough
  57.  */
  58. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  59. {
  60.     if(min_size < *size) 
  61.         return ptr;
  62.     
  63.     *size= 17*min_size/16 + 32;
  64.     return av_realloc(ptr, *size);
  65. }
  66. //static unsigned int last_static = 0;
  67. //static unsigned int allocated_static = 0;
  68. //static void** array_static = NULL;
  69. ///**
  70. // * allocation of static arrays - do not use for normal allocation.
  71. // */
  72. //void *av_mallocz_static(unsigned int size)
  73. //{
  74. //    void *ptr = av_mallocz(size);
  75. //    if(ptr){ 
  76. //        array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
  77. //        array_static[last_static++] = ptr;
  78. //    }
  79. //    return ptr;
  80. //}
  81. ///**
  82. // * free all static arrays and reset pointers to 0.
  83. // */
  84. //void av_free_static(void)
  85. //{
  86. //    while(last_static){
  87. //        av_freep(&array_static[--last_static]);
  88. //    }
  89. //    av_freep(&array_static);
  90. //}
  91. /**
  92.  * Frees memory and sets the pointer to NULL.
  93.  * @param arg pointer to the pointer which should be freed
  94.  */
  95. void av_freep(void *arg)
  96. {
  97.     void **ptr= (void**)arg;
  98.     av_free(*ptr);
  99.     *ptr = NULL;
  100. }
  101. ///* encoder management */
  102. AVCodec *first_avcodec;
  103. void register_avcodec(AVCodec *format)
  104. {
  105.     AVCodec **p;
  106.     p = &first_avcodec;
  107.     while (*p != NULL) p = &(*p)->next;
  108.     *p = format;
  109.     format->next = NULL;
  110. }
  111. typedef struct InternalBuffer{
  112.     int last_pic_num;
  113.     uint8_t *base[4];
  114.     uint8_t *data[4];
  115.     int linesize[4];
  116. }InternalBuffer;
  117. #define INTERNAL_BUFFER_SIZE 32
  118. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  119. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  120.     int w_align= 1;    
  121.     int h_align= 1;    
  122.     
  123.     switch(s->pix_fmt){
  124.     case PIX_FMT_YUV420P:
  125.     case PIX_FMT_YUV422:
  126.     case PIX_FMT_YUV422P:
  127.     case PIX_FMT_YUV444P:
  128.     case PIX_FMT_GRAY8:
  129.     case PIX_FMT_YUVJ420P:
  130.     case PIX_FMT_YUVJ422P:
  131.     case PIX_FMT_YUVJ444P:
  132.         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  133.         h_align= 16;
  134.         break;
  135.     case PIX_FMT_YUV411P:
  136.         w_align=32;
  137.         h_align=8;
  138.         break;
  139.     case PIX_FMT_YUV410P:
  140.         if(s->codec_id == CODEC_ID_SVQ1){
  141.             w_align=64;
  142.             h_align=64;
  143.         }
  144.     case PIX_FMT_RGB555:
  145.         if(s->codec_id == CODEC_ID_RPZA){
  146.             w_align=4;
  147.             h_align=4;
  148.         }
  149.     case PIX_FMT_PAL8:
  150.         if(s->codec_id == CODEC_ID_SMC){
  151.             w_align=4;
  152.             h_align=4;
  153.         }
  154.         break;
  155.     default:
  156.         w_align= 1;
  157.         h_align= 1;
  158.         break;
  159.     }
  160.     *width = ALIGN(*width , w_align);
  161.     *height= ALIGN(*height, h_align);
  162. }
  163. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  164.     int i;
  165.     int w= s->width;
  166.     int h= s->height;
  167.     InternalBuffer *buf;
  168.     int *picture_number;
  169.     
  170.     assert(pic->data[0]==NULL);
  171.     assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
  172.     if(s->internal_buffer==NULL){
  173.         s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
  174.     }
  175. #if 0
  176.     s->internal_buffer= av_fast_realloc(
  177.         s->internal_buffer, 
  178.         &s->internal_buffer_size, 
  179.         sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
  180.         );
  181. #endif
  182.      
  183.     buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  184.     picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
  185.     (*picture_number)++;
  186.     
  187.     if(buf->base[0]){
  188.         pic->age= *picture_number - buf->last_pic_num;
  189.         buf->last_pic_num= *picture_number;
  190.     }else{
  191.         int h_chroma_shift, v_chroma_shift;
  192.         int s_align, pixel_size;
  193.         
  194.         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  195.         
  196.         switch(s->pix_fmt){
  197.         case PIX_FMT_RGB555:
  198.         case PIX_FMT_RGB565:
  199.         case PIX_FMT_YUV422:
  200.             pixel_size=2;
  201.             break;
  202.         case PIX_FMT_RGB24:
  203.         case PIX_FMT_BGR24:
  204.             pixel_size=3;
  205.             break;
  206.         case PIX_FMT_RGBA32:
  207.             pixel_size=4;
  208.             break;
  209.         default:
  210.             pixel_size=1;
  211.         }
  212.         avcodec_align_dimensions(s, &w, &h);
  213. #if defined(ARCH_POWERPC) || defined(HAVE_MMI) //FIXME some cleaner check
  214.         s_align= 16;
  215. #else
  216.         s_align= 8;
  217. #endif
  218.             
  219.         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  220.             w+= EDGE_WIDTH*2;
  221.             h+= EDGE_WIDTH*2;
  222.         }
  223.         
  224.         buf->last_pic_num= -256*256*256*64;
  225.         for(i=0; i<3; i++){
  226.             const int h_shift= i==0 ? 0 : h_chroma_shift;
  227.             const int v_shift= i==0 ? 0 : v_chroma_shift;
  228.             //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
  229.             buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, s_align<<(h_chroma_shift-h_shift)); 
  230.             buf->base[i]= av_mallocz((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
  231.             if(buf->base[i]==NULL) return -1;
  232.             memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
  233.         
  234.             if(s->flags&CODEC_FLAG_EMU_EDGE)
  235.                 buf->data[i] = buf->base[i];
  236.             else
  237.                 buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
  238.         }
  239.         pic->age= 256*256*256*64;
  240.     }
  241.     pic->type= FF_BUFFER_TYPE_INTERNAL;
  242.     for(i=0; i<4; i++){
  243.         pic->base[i]= buf->base[i];
  244.         pic->data[i]= buf->data[i];
  245.         pic->linesize[i]= buf->linesize[i];
  246.     }
  247.     s->internal_buffer_count++;
  248.     return 0;
  249. }
  250. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  251.     int i;
  252.     InternalBuffer *buf, *last, temp;
  253.     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  254.     assert(s->internal_buffer_count);
  255.     buf = NULL; /* avoids warning */
  256.     for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  257.         buf= &((InternalBuffer*)s->internal_buffer)[i];
  258.         if(buf->data[0] == pic->data[0])
  259.             break;
  260.     }
  261.     assert(i < s->internal_buffer_count);
  262.     s->internal_buffer_count--;
  263.     last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  264.     temp= *buf;
  265.     *buf= *last;
  266.     *last= temp;
  267.     for(i=0; i<3; i++){
  268.         pic->data[i]=NULL;
  269. //        pic->base[i]=NULL;
  270.     }
  271. //printf("R%Xn", pic->opaque);
  272. }
  273. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  274.     AVFrame temp_pic;
  275.     int i;
  276.     /* If no picture return a new buffer */
  277.     if(pic->data[0] == NULL) {
  278.         /* We will copy from buffer, so must be readable */
  279.         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  280.         return s->get_buffer(s, pic);
  281.     }
  282.     /* If internal buffer type return the same buffer */
  283.     if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  284.         return 0;
  285.     /*
  286.      * Not internal type and reget_buffer not overridden, emulate cr buffer
  287.      */
  288.     temp_pic = *pic;
  289.     for(i = 0; i < 4; i++)
  290.         pic->data[i] = pic->base[i] = NULL;
  291.     pic->opaque = NULL;
  292.     /* Allocate new frame */
  293.     if (s->get_buffer(s, pic))
  294.         return -1;
  295.     /* Copy image data from old buffer to new buffer */
  296.     img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  297.              s->height);
  298.     s->release_buffer(s, &temp_pic); // Release old frame
  299.     return 0;
  300. }
  301. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
  302.     int i;
  303.     for(i=0; i<count; i++){
  304.         int r= func(c, arg[i]);
  305.         if(ret) ret[i]= r;
  306.     }
  307.     return 0;
  308. }
  309. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
  310.     return fmt[0];
  311. }
  312. static const char* context_to_name(void* ptr) {
  313.     AVCodecContext *avc= ptr;
  314.     if(avc && avc->codec && avc->codec->name)
  315.         return avc->codec->name; 
  316.     else
  317.         return "NULL";
  318. }
  319. static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
  320. void avcodec_get_context_defaults(AVCodecContext *s){
  321.     memset(s, 0, sizeof(AVCodecContext));
  322.     s->av_class= &av_codec_context_class;
  323.     s->bit_rate= 800*1000;
  324.     s->bit_rate_tolerance= s->bit_rate*10;
  325.     s->qmin= 2;
  326.     s->qmax= 31;
  327.     s->mb_qmin= 2;
  328.     s->mb_qmax= 31;
  329.     s->rc_eq= "tex^qComp";
  330.     s->qcompress= 0.5;
  331.     s->max_qdiff= 3;
  332.     s->b_quant_factor=1.25;
  333.     s->b_quant_offset=1.25;
  334.     s->i_quant_factor=-0.8;
  335.     s->i_quant_offset=0.0;
  336.     s->error_concealment= 3;
  337.     s->error_resilience= 1;
  338.     s->workaround_bugs= FF_BUG_AUTODETECT;
  339.     s->frame_rate_base= 1;
  340.     s->frame_rate = 25;
  341.     s->gop_size= 50;
  342.     s->me_method= ME_EPZS;
  343.     s->get_buffer= avcodec_default_get_buffer;
  344.     s->release_buffer= avcodec_default_release_buffer;
  345.     s->get_format= avcodec_default_get_format;
  346.     s->execute= avcodec_default_execute;
  347.     s->thread_count=1;
  348.     s->me_subpel_quality=8;
  349.     s->lmin= FF_QP2LAMBDA * s->qmin;
  350.     s->lmax= FF_QP2LAMBDA * s->qmax;
  351. //    s->sample_aspect_ratio = (AVRational){0,1};
  352.     s->sample_aspect_ratio.num = 0;
  353.     s->sample_aspect_ratio.den = 1;
  354.     s->ildct_cmp= FF_CMP_VSAD;
  355.     
  356.     s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  357.     s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  358.     s->palctrl = NULL;
  359.     s->reget_buffer= avcodec_default_reget_buffer;
  360. }
  361. /**
  362.  * allocates a AVCodecContext and set it to defaults.
  363.  * this can be deallocated by simply calling free() 
  364.  */
  365. AVCodecContext *avcodec_alloc_context(void){
  366.     AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  367.     
  368.     if(avctx==NULL) return NULL;
  369.     
  370.     avcodec_get_context_defaults(avctx);
  371.     
  372.     return avctx;
  373. }
  374. void avcodec_get_frame_defaults(AVFrame *pic){
  375.     memset(pic, 0, sizeof(AVFrame));
  376.     pic->pts= AV_NOPTS_VALUE;
  377. }
  378. /**
  379.  * allocates a AVPFrame and set it to defaults.
  380.  * this can be deallocated by simply calling free() 
  381.  */
  382. AVFrame *avcodec_alloc_frame(void){
  383.     AVFrame *pic= av_malloc(sizeof(AVFrame));
  384.     
  385.     if(pic==NULL) return NULL;
  386.     
  387.     avcodec_get_frame_defaults(pic);
  388.     
  389.     return pic;
  390. }
  391. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  392. {
  393.     int ret;
  394.     if(avctx->codec)
  395.         return -1;
  396.     avctx->codec = codec;
  397.     avctx->codec_id = codec->id;
  398.     avctx->frame_number = 0;
  399.     if (codec->priv_data_size > 0) {
  400.         avctx->priv_data = av_mallocz(codec->priv_data_size);
  401.         if (!avctx->priv_data) 
  402.             return -ENOMEM;
  403.     } else {
  404.         avctx->priv_data = NULL;
  405.     }
  406.     ret = avctx->codec->init(avctx);
  407.     if (ret < 0) {
  408.         av_freep(&avctx->priv_data);
  409.         return ret;
  410.     }
  411.     return 0;
  412. }
  413. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
  414.                          const short *samples)
  415. {
  416.     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  417.         int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  418.         avctx->frame_number++;
  419.         return ret;
  420.     }else
  421.         return 0;
  422. }
  423. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
  424.                          const AVFrame *pict)
  425. {
  426.     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  427.         int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  428.         avctx->frame_number++;
  429.         emms_c(); //needed to avoid a emms_c() call before every return;
  430.     
  431.         return ret;
  432.     }else
  433.         return 0;
  434. }
  435. /** 
  436.  * decode a frame. 
  437.  * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  438.  * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  439.  * @param buf_size the size of the buffer in bytes
  440.  * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  441.  * @return -1 if error, otherwise return the number of
  442.  * bytes used. 
  443.  */
  444. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture, 
  445.                          int *got_picture_ptr,
  446.                          uint8_t *buf, int buf_size)
  447. {
  448.     int ret;
  449.     
  450.     *got_picture_ptr= 0;
  451.     ret = avctx->codec->decode(avctx, picture, got_picture_ptr, 
  452.                                buf, buf_size);
  453.     emms_c(); //needed to avoid a emms_c() call before every return;
  454.     
  455.     if (*got_picture_ptr)                           
  456.         avctx->frame_number++;
  457.     return ret;
  458. }
  459. /* decode an audio frame. return -1 if error, otherwise return the
  460.    *number of bytes used. If no frame could be decompressed,
  461.    *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  462.    *size in BYTES. */
  463. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples, 
  464.                          int *frame_size_ptr,
  465.                          uint8_t *buf, int buf_size)
  466. {
  467.     int ret;
  468.     *frame_size_ptr= 0;
  469.     ret = avctx->codec->decode(avctx, samples, frame_size_ptr, 
  470.                                buf, buf_size);
  471.     avctx->frame_number++;
  472.     return ret;
  473. }
  474. int avcodec_close(AVCodecContext *avctx)
  475. {
  476.     if (avctx->codec->close)
  477.         avctx->codec->close(avctx);
  478.     avcodec_default_free_buffers(avctx);
  479.     av_freep(&avctx->priv_data);
  480.     avctx->codec = NULL;
  481.     return 0;
  482. }
  483. AVCodec *avcodec_find_encoder(enum CodecID id)
  484. {
  485.     AVCodec *p;
  486.     p = first_avcodec;
  487.     while (p) {
  488.         if (p->encode != NULL && p->id == id)
  489.             return p;
  490.         p = p->next;
  491.     }
  492.     return NULL;
  493. }
  494. AVCodec *avcodec_find_encoder_by_name(const char *name)
  495. {
  496.     AVCodec *p;
  497.     p = first_avcodec;
  498.     while (p) {
  499.         if (p->encode != NULL && strcmp(name,p->name) == 0)
  500.             return p;
  501.         p = p->next;
  502.     }
  503.     return NULL;
  504. }
  505. AVCodec *avcodec_find_decoder(enum CodecID id)
  506. {
  507.     AVCodec *p;
  508.     p = first_avcodec;
  509.     while (p) {
  510.         if (p->decode != NULL && p->id == id)
  511.             return p;
  512.         p = p->next;
  513.     }
  514.     return NULL;
  515. }
  516. AVCodec *avcodec_find_decoder_by_name(const char *name)
  517. {
  518.     AVCodec *p;
  519.     p = first_avcodec;
  520.     while (p) {
  521.         if (p->decode != NULL && strcmp(name,p->name) == 0)
  522.             return p;
  523.         p = p->next;
  524.     }
  525.     return NULL;
  526. }
  527. static AVCodec *avcodec_find(enum CodecID id)
  528. {
  529.     AVCodec *p;
  530.     p = first_avcodec;
  531.     while (p) {
  532.         if (p->id == id)
  533.             return p;
  534.         p = p->next;
  535.     }
  536.     return NULL;
  537. }
  538. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  539. {
  540.     const char *codec_name;
  541.     AVCodec *p;
  542.     char buf1[32];
  543.     char channels_str[100];
  544.     int bitrate;
  545.     if (encode)
  546.         p = avcodec_find_encoder(enc->codec_id);
  547.     else
  548.         p = avcodec_find_decoder(enc->codec_id);
  549.     if (p) {
  550.         codec_name = p->name;
  551.         if (!encode && enc->codec_id == CODEC_ID_MP3) {
  552.             if (enc->sub_id == 2)
  553.                 codec_name = "mp2";
  554.             else if (enc->sub_id == 1)
  555.                 codec_name = "mp1";
  556.         }
  557.     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  558.         /* fake mpeg2 transport stream codec (currently not
  559.            registered) */
  560.         codec_name = "mpeg2ts";
  561.     } else if (enc->codec_name[0] != '') {
  562.         codec_name = enc->codec_name;
  563.     } else {
  564.         /* output avi tags */
  565.         if (enc->codec_type == CODEC_TYPE_VIDEO) {
  566.             snprintf(buf1, sizeof(buf1), "%c%c%c%c", 
  567.                      enc->codec_tag & 0xff,
  568.                      (enc->codec_tag >> 8) & 0xff,
  569.                      (enc->codec_tag >> 16) & 0xff,
  570.                      (enc->codec_tag >> 24) & 0xff);
  571.         } else {
  572.             snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  573.         }
  574.         codec_name = buf1;
  575.     }
  576.     switch(enc->codec_type) {
  577.     case CODEC_TYPE_VIDEO:
  578.         snprintf(buf, buf_size,
  579.                  "Video: %s%s",
  580.                  codec_name, enc->mb_decision ? " (hq)" : "");
  581.         if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  582.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  583.                      ", %s",
  584.                      avcodec_get_pix_fmt_name(enc->pix_fmt));
  585.         }
  586.         if (enc->width) {
  587.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  588.                      ", %dx%d, %0.2f fps",
  589.                      enc->width, enc->height, 
  590.                      (float)enc->frame_rate / enc->frame_rate_base);
  591.         }
  592.         if (encode) {
  593.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  594.                      ", q=%d-%d", enc->qmin, enc->qmax);
  595.         }
  596.         bitrate = enc->bit_rate;
  597.         break;
  598.     case CODEC_TYPE_AUDIO:
  599.         snprintf(buf, buf_size,
  600.                  "Audio: %s",
  601.                  codec_name);
  602.         switch (enc->channels) {
  603.             case 1:
  604.                 strcpy(channels_str, "mono");
  605.                 break;
  606.             case 2:
  607.                 strcpy(channels_str, "stereo");
  608.                 break;
  609.             case 6:
  610.                 strcpy(channels_str, "5:1");
  611.                 break;
  612.             default:
  613.                 sprintf(channels_str, "%d channels", enc->channels);
  614.                 break;
  615.         }
  616.         if (enc->sample_rate) {
  617.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  618.                      ", %d Hz, %s",
  619.                      enc->sample_rate,
  620.                      channels_str);
  621.         }
  622.         
  623.         /* for PCM codecs, compute bitrate directly */
  624.         switch(enc->codec_id) {
  625.         case CODEC_ID_PCM_S16LE:
  626.         case CODEC_ID_PCM_S16BE:
  627.         case CODEC_ID_PCM_U16LE:
  628.         case CODEC_ID_PCM_U16BE:
  629.             bitrate = enc->sample_rate * enc->channels * 16;
  630.             break;
  631.         case CODEC_ID_PCM_S8:
  632.         case CODEC_ID_PCM_U8:
  633.         case CODEC_ID_PCM_ALAW:
  634.         case CODEC_ID_PCM_MULAW:
  635.             bitrate = enc->sample_rate * enc->channels * 8;
  636.             break;
  637.         default:
  638.             bitrate = enc->bit_rate;
  639.             break;
  640.         }
  641.         break;
  642.     case CODEC_TYPE_DATA:
  643.         snprintf(buf, buf_size, "Data: %s", codec_name);
  644.         bitrate = enc->bit_rate;
  645.         break;
  646.     default:
  647.         av_abort();
  648.     }
  649.     if (encode) {
  650.         if (enc->flags & CODEC_FLAG_PASS1)
  651.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  652.                      ", pass 1");
  653.         if (enc->flags & CODEC_FLAG_PASS2)
  654.             snprintf(buf + strlen(buf), buf_size - strlen(buf),
  655.                      ", pass 2");
  656.     }
  657.     if (bitrate != 0) {
  658.         snprintf(buf + strlen(buf), buf_size - strlen(buf), 
  659.                  ", %d kb/s", bitrate / 1000);
  660.     }
  661. }
  662. unsigned avcodec_version( void )
  663. {
  664.   return LIBAVCODEC_VERSION_INT;
  665. }
  666. unsigned avcodec_build( void )
  667. {
  668.   return LIBAVCODEC_BUILD;
  669. }
  670. /* must be called before any other functions */
  671. void avcodec_init(void)
  672. {
  673.     static int inited = 0;
  674.     if (inited != 0)
  675. return;
  676.     inited = 1;
  677.     dsputil_static_init();
  678. }
  679. /**
  680.  * Flush buffers, should be called when seeking or when swicthing to a different stream.
  681.  */
  682. void avcodec_flush_buffers(AVCodecContext *avctx)
  683. {
  684.     if(avctx->codec->flush)
  685.         avctx->codec->flush(avctx);
  686. }
  687. static void avcodec_default_free_buffers(AVCodecContext *s){
  688.     int i, j;
  689.     if(s->internal_buffer==NULL) return;
  690.     
  691.     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  692.         InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  693.         for(j=0; j<4; j++){
  694.             av_freep(&buf->base[j]);
  695.             buf->data[j]= NULL;
  696.         }
  697.     }
  698.     av_freep(&s->internal_buffer);
  699.     
  700.     s->internal_buffer_count=0;
  701. }
  702. char av_get_pict_type_char(int pict_type){
  703.     switch(pict_type){
  704.     case I_TYPE: return 'I'; 
  705.     case P_TYPE: return 'P'; 
  706.     case B_TYPE: return 'B'; 
  707.     case S_TYPE: return 'S'; 
  708.     case SI_TYPE:return 'i'; 
  709.     case SP_TYPE:return 'p'; 
  710.     default:     return '?';
  711.     }
  712. }
  713. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  714.     int exact=1, sign=0;
  715.     int64_t gcd;
  716.     assert(den != 0);
  717.     if(den < 0)
  718.         return av_reduce(dst_nom, dst_den, -nom, -den, max);
  719.     
  720.     sign= nom < 0;
  721.     nom= ABS(nom);
  722.     
  723.     gcd = ff_gcd(nom, den);
  724.     nom /= gcd;
  725.     den /= gcd;
  726.     
  727.     if(nom > max || den > max){
  728.         AVRational a0={0,1}, a1={1,0};
  729.         exact=0;
  730.         for(;;){
  731.             int64_t x= nom / den;
  732.             int64_t a2n= x*a1.num + a0.num;
  733.             int64_t a2d= x*a1.den + a0.den;
  734.             if(a2n > max || a2d > max) break;
  735.             nom %= den;
  736.         
  737.             a0= a1;
  738. //            a1= (AVRational){a2n, a2d};
  739.             a1.num = a2n;
  740.             a1.den = a2d;
  741.             if(nom==0) break;
  742.             x= nom; nom=den; den=x;
  743.         }
  744.         nom= a1.num;
  745.         den= a1.den;
  746.     }
  747.     
  748.     assert(ff_gcd(nom, den) == 1);
  749.     
  750.     *dst_nom = sign ? -nom : nom;
  751.     *dst_den = den;
  752.     
  753.     return exact;
  754. }
  755. //int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  756. //    AVInteger ai, ci;
  757. //    assert(c > 0);
  758. //    assert(b >=0);
  759. //    
  760. //    if(a<0) return -av_rescale(-a, b, c);
  761. //    
  762. //    if(b<=INT_MAX && c<=INT_MAX){
  763. //        if(a<=INT_MAX)
  764. //            return (a * b + c/2)/c;
  765. //        else
  766. //            return a/c*b + (a%c*b + c/2)/c;
  767. //    }
  768. //    
  769. //    ai= av_mul_i(av_int2i(a), av_int2i(b));
  770. //    ci= av_int2i(c);
  771. //    ai= av_add_i(ai, av_shr_i(ci,1));
  772. //    
  773. //    return av_i2int(av_div_i(ai, ci));
  774. //}
  775. /* av_log API */
  776. static int av_log_level = AV_LOG_DEBUG;
  777. static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  778. {
  779.     static int print_prefix=1;
  780.     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  781.     if(level>av_log_level)
  782. return;
  783. #undef fprintf
  784.     if(print_prefix && avc) {
  785.     fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
  786.     }
  787. //#define fprintf please_use_av_log //Del by ty
  788.         
  789.     print_prefix= strstr(fmt, "n") != NULL;
  790.         
  791.     vfprintf(stderr, fmt, vl);
  792. }
  793. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  794. void av_log(void* avcl, int level, const char *fmt, ...)
  795. {
  796.     va_list vl;
  797.     va_start(vl, fmt);
  798.     av_vlog(avcl, level, fmt, vl);
  799.     va_end(vl);
  800. }
  801. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  802. {
  803.     av_log_callback(avcl, level, fmt, vl);
  804. }
  805. int av_log_get_level(void)
  806. {
  807.     return av_log_level;
  808. }
  809. void av_log_set_level(int level)
  810. {
  811.     av_log_level = level;
  812. }
  813. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  814. {
  815.     av_log_callback = callback;
  816. }
  817. #if !defined(HAVE_PTHREADS) && !defined(HAVE_W32THREADS)
  818. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  819.     return -1;
  820. }
  821. #endif