• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files

hvirtual/quicktime/ffmpeg/libavcodec/utils.c

Go to the documentation of this file.
00001 /*
00002  * utils for libavcodec
00003  * Copyright (c) 2001 Fabrice Bellard.
00004  * Copyright (c) 2003 Michel Bardiaux for the av_log API
00005  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021  
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "mpegvideo.h"
00030 #include "integer.h"
00031 #include <stdarg.h>
00032 #include <limits.h>
00033 
00034 const uint8_t ff_sqrt_tab[128]={
00035         0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
00036         5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00037         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
00038         9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
00039 };
00040 
00041 const uint8_t ff_log2_tab[256]={
00042         0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
00043         5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00044         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00045         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00046         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00047         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00048         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00049         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
00050 };
00051 
00052 const uint8_t ff_reverse[256]={
00053 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
00054 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
00055 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
00056 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
00057 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
00058 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
00059 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
00060 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
00061 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
00062 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
00063 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
00064 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
00065 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
00066 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
00067 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
00068 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
00069 };
00070 
00071 static int volatile entangled_thread_counter=0;
00072 
00073 void avcodec_default_free_buffers(AVCodecContext *s);
00074 
00075 void *av_mallocz(unsigned int size)
00076 {
00077     void *ptr;
00078     
00079     ptr = av_malloc(size);
00080     if (!ptr)
00081         return NULL;
00082     memset(ptr, 0, size);
00083     return ptr;
00084 }
00085 
00086 char *av_strdup(const char *s)
00087 {
00088     char *ptr;
00089     int len;
00090     len = strlen(s) + 1;
00091     ptr = av_malloc(len);
00092     if (!ptr)
00093         return NULL;
00094     memcpy(ptr, s, len);
00095     return ptr;
00096 }
00097 
00101 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
00102 {
00103     if(min_size < *size) 
00104         return ptr;
00105     
00106     *size= FFMAX(17*min_size/16 + 32, min_size);
00107 
00108     return av_realloc(ptr, *size);
00109 }
00110 
00111 
00112 static unsigned int last_static = 0;
00113 static unsigned int allocated_static = 0;
00114 static void** array_static = NULL;
00115 
00119 void *av_mallocz_static(unsigned int size)
00120 {
00121     void *ptr = av_mallocz(size);
00122 
00123     if(ptr){ 
00124         array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
00125         if(!array_static)
00126             return NULL;
00127         array_static[last_static++] = ptr;
00128     }
00129 
00130     return ptr;
00131 }
00132 
00137 void *av_realloc_static(void *ptr, unsigned int size)
00138 {
00139     int i;
00140     if(!ptr)
00141       return av_mallocz_static(size);
00142     /* Look for the old ptr */
00143     for(i = 0; i < last_static; i++) {
00144         if(array_static[i] == ptr) {
00145             array_static[i] = av_realloc(array_static[i], size);
00146             return array_static[i];
00147         }
00148     }
00149     return NULL;
00150 
00151 }
00152 
00156 void av_free_static(void)
00157 {
00158     while(last_static){
00159         av_freep(&array_static[--last_static]);
00160     }
00161     av_freep(&array_static);
00162 }
00163 
00168 void av_freep(void *arg)
00169 {
00170     void **ptr= (void**)arg;
00171     av_free(*ptr);
00172     *ptr = NULL;
00173 }
00174 
00175 /* encoder management */
00176 AVCodec *first_avcodec = NULL;
00177 
00178 void register_avcodec(AVCodec *format)
00179 {
00180     AVCodec **p;
00181     p = &first_avcodec;
00182     while (*p != NULL) p = &(*p)->next;
00183     *p = format;
00184     format->next = NULL;
00185 }
00186 
00187 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
00188     s->coded_width = width;
00189     s->coded_height= height;
00190     s->width = -((-width )>>s->lowres);
00191     s->height= -((-height)>>s->lowres);
00192 }
00193 
00194 typedef struct InternalBuffer{
00195     int last_pic_num;
00196     uint8_t *base[4];
00197     uint8_t *data[4];
00198     int linesize[4];
00199 }InternalBuffer;
00200 
00201 #define INTERNAL_BUFFER_SIZE 32
00202 
00203 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
00204 
00205 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
00206     int w_align= 1;    
00207     int h_align= 1;    
00208     
00209     switch(s->pix_fmt){
00210     case PIX_FMT_YUV420P:
00211     case PIX_FMT_YUV422:
00212     case PIX_FMT_UYVY422:
00213     case PIX_FMT_YUV422P:
00214     case PIX_FMT_YUV444P:
00215     case PIX_FMT_GRAY8:
00216     case PIX_FMT_YUVJ420P:
00217     case PIX_FMT_YUVJ422P:
00218     case PIX_FMT_YUVJ444P:
00219         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
00220         h_align= 16;
00221         break;
00222     case PIX_FMT_YUV411P:
00223     case PIX_FMT_UYVY411:
00224         w_align=32;
00225         h_align=8;
00226         break;
00227     case PIX_FMT_YUV410P:
00228         if(s->codec_id == CODEC_ID_SVQ1){
00229             w_align=64;
00230             h_align=64;
00231         }
00232     case PIX_FMT_RGB555:
00233         if(s->codec_id == CODEC_ID_RPZA){
00234             w_align=4;
00235             h_align=4;
00236         }
00237     case PIX_FMT_PAL8:
00238         if(s->codec_id == CODEC_ID_SMC){
00239             w_align=4;
00240             h_align=4;
00241         }
00242         break;
00243     case PIX_FMT_BGR24:
00244         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
00245             w_align=4;
00246             h_align=4;
00247         }
00248         break;
00249     default:
00250         w_align= 1;
00251         h_align= 1;
00252         break;
00253     }
00254 
00255     *width = ALIGN(*width , w_align);
00256     *height= ALIGN(*height, h_align);
00257 }
00258 
00259 int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
00260     if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
00261         return 0;
00262     
00263     av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
00264     return -1;
00265 }
00266 
00267 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
00268     int i;
00269     int w= s->width;
00270     int h= s->height;
00271     InternalBuffer *buf;
00272     int *picture_number;
00273 
00274     assert(pic->data[0]==NULL);
00275     assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
00276 
00277     if(avcodec_check_dimensions(s,w,h))
00278         return -1;
00279 
00280     if(s->internal_buffer==NULL){
00281         s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
00282     }
00283 #if 0
00284     s->internal_buffer= av_fast_realloc(
00285         s->internal_buffer, 
00286         &s->internal_buffer_size, 
00287         sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
00288         );
00289 #endif
00290      
00291     buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00292     picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
00293     (*picture_number)++;
00294     
00295     if(buf->base[0]){
00296         pic->age= *picture_number - buf->last_pic_num;
00297         buf->last_pic_num= *picture_number;
00298     }else{
00299         int h_chroma_shift, v_chroma_shift;
00300         int pixel_size;
00301         
00302         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00303         
00304         switch(s->pix_fmt){
00305         case PIX_FMT_RGB555:
00306         case PIX_FMT_RGB565:
00307         case PIX_FMT_YUV422:
00308         case PIX_FMT_UYVY422:
00309             pixel_size=2;
00310             break;
00311         case PIX_FMT_RGB24:
00312         case PIX_FMT_BGR24:
00313             pixel_size=3;
00314             break;
00315         case PIX_FMT_RGBA32:
00316             pixel_size=4;
00317             break;
00318         default:
00319             pixel_size=1;
00320         }
00321 
00322         avcodec_align_dimensions(s, &w, &h);
00323             
00324         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
00325             w+= EDGE_WIDTH*2;
00326             h+= EDGE_WIDTH*2;
00327         }
00328         
00329         buf->last_pic_num= -256*256*256*64;
00330 
00331         for(i=0; i<3; i++){
00332             const int h_shift= i==0 ? 0 : h_chroma_shift;
00333             const int v_shift= i==0 ? 0 : v_chroma_shift;
00334 
00335             if(s->pix_fmt == PIX_FMT_PAL8 && i == 1)
00336             {
00337                 buf->base[i] = av_malloc(256 * 4);
00338                 if(buf->base[i] == NULL)
00339                     return -1;
00340                 buf->data[i] = buf->base[i];
00341                 continue;
00342             }
00343 
00344             //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
00345             buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, STRIDE_ALIGN<<(h_chroma_shift-h_shift)); 
00346 
00347             buf->base[i]= av_malloc((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
00348             if(buf->base[i]==NULL) return -1;
00349             memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
00350         
00351             if(s->flags&CODEC_FLAG_EMU_EDGE)
00352                 buf->data[i] = buf->base[i];
00353             else
00354                 buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
00355         }
00356         pic->age= 256*256*256*64;
00357     }
00358     pic->type= FF_BUFFER_TYPE_INTERNAL;
00359 
00360     for(i=0; i<4; i++){
00361         pic->base[i]= buf->base[i];
00362         pic->data[i]= buf->data[i];
00363         pic->linesize[i]= buf->linesize[i];
00364     }
00365     s->internal_buffer_count++;
00366 
00367     return 0;
00368 }
00369 
00370 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
00371     int i;
00372     InternalBuffer *buf, *last, temp;
00373 
00374     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
00375     assert(s->internal_buffer_count);
00376 
00377     buf = NULL; /* avoids warning */
00378     for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
00379         buf= &((InternalBuffer*)s->internal_buffer)[i];
00380         if(buf->data[0] == pic->data[0])
00381             break;
00382     }
00383     assert(i < s->internal_buffer_count);
00384     s->internal_buffer_count--;
00385     last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
00386 
00387     temp= *buf;
00388     *buf= *last;
00389     *last= temp;
00390 
00391     for(i=0; i<3; i++){
00392         pic->data[i]=NULL;
00393 //        pic->base[i]=NULL;
00394     }
00395 //printf("R%X\n", pic->opaque);
00396 }
00397 
00398 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
00399     AVFrame temp_pic;
00400     int i;
00401 
00402     /* If no picture return a new buffer */
00403     if(pic->data[0] == NULL) {
00404         /* We will copy from buffer, so must be readable */
00405         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
00406         return s->get_buffer(s, pic);
00407     }
00408 
00409     /* If internal buffer type return the same buffer */
00410     if(pic->type == FF_BUFFER_TYPE_INTERNAL)
00411         return 0;
00412 
00413     /*
00414      * Not internal type and reget_buffer not overridden, emulate cr buffer
00415      */
00416     temp_pic = *pic;
00417     for(i = 0; i < 4; i++)
00418         pic->data[i] = pic->base[i] = NULL;
00419     pic->opaque = NULL;
00420     /* Allocate new frame */
00421     if (s->get_buffer(s, pic))
00422         return -1;
00423     /* Copy image data from old buffer to new buffer */
00424     img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
00425              s->height);
00426     s->release_buffer(s, &temp_pic); // Release old frame
00427     return 0;
00428 }
00429 
00430 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
00431     int i;
00432 
00433     for(i=0; i<count; i++){
00434         int r= func(c, arg[i]);
00435         if(ret) ret[i]= r;
00436     }
00437     return 0;
00438 }
00439 
00440 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
00441     return fmt[0];
00442 }
00443 
00444 static const char* context_to_name(void* ptr) {
00445     AVCodecContext *avc= ptr;
00446 
00447     if(avc && avc->codec && avc->codec->name)
00448         return avc->codec->name; 
00449     else
00450         return "NULL";
00451 }
00452 
00453 static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
00454 
00455 void avcodec_get_context_defaults(AVCodecContext *s){
00456     memset(s, 0, sizeof(AVCodecContext));
00457 
00458     s->av_class= &av_codec_context_class;
00459     s->bit_rate= 800*1000;
00460     s->bit_rate_tolerance= s->bit_rate*10;
00461     s->qmin= 2;
00462     s->qmax= 31;
00463     s->mb_lmin= FF_QP2LAMBDA * 2;
00464     s->mb_lmax= FF_QP2LAMBDA * 31;
00465     s->rc_eq= "tex^qComp";
00466     s->qcompress= 0.5;
00467     s->max_qdiff= 3;
00468     s->b_quant_factor=1.25;
00469     s->b_quant_offset=1.25;
00470     s->i_quant_factor=-0.8;
00471     s->i_quant_offset=0.0;
00472     s->error_concealment= 3;
00473     s->error_resilience= 1;
00474     s->workaround_bugs= FF_BUG_AUTODETECT;
00475     s->time_base= (AVRational){0,1};
00476     s->gop_size= 50;
00477     s->me_method= ME_EPZS;
00478     s->get_buffer= avcodec_default_get_buffer;
00479     s->release_buffer= avcodec_default_release_buffer;
00480     s->get_format= avcodec_default_get_format;
00481     s->execute= avcodec_default_execute;
00482     s->thread_count=1;
00483     s->me_subpel_quality=8;
00484     s->lmin= FF_QP2LAMBDA * s->qmin;
00485     s->lmax= FF_QP2LAMBDA * s->qmax;
00486     s->sample_aspect_ratio= (AVRational){0,1};
00487     s->ildct_cmp= FF_CMP_VSAD;
00488     s->profile= FF_PROFILE_UNKNOWN;
00489     s->level= FF_LEVEL_UNKNOWN;
00490     s->me_penalty_compensation= 256;
00491     s->pix_fmt= PIX_FMT_NONE;
00492     
00493     s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
00494     s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
00495     s->palctrl = NULL;
00496     s->reget_buffer= avcodec_default_reget_buffer;
00497 }
00498 
00503 AVCodecContext *avcodec_alloc_context(void){
00504     AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
00505     
00506     if(avctx==NULL) return NULL;
00507     
00508     avcodec_get_context_defaults(avctx);
00509     
00510     return avctx;
00511 }
00512 
00513 void avcodec_get_frame_defaults(AVFrame *pic){
00514     memset(pic, 0, sizeof(AVFrame));
00515 
00516     pic->pts= AV_NOPTS_VALUE;
00517     pic->key_frame= 1;
00518 }
00519 
00524 AVFrame *avcodec_alloc_frame(void){
00525     AVFrame *pic= av_malloc(sizeof(AVFrame));
00526     
00527     if(pic==NULL) return NULL;
00528     
00529     avcodec_get_frame_defaults(pic);
00530     
00531     return pic;
00532 }
00533 
00534 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
00535 {
00536     int ret= -1;
00537     
00538     entangled_thread_counter++;
00539     if(entangled_thread_counter != 1){
00540         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00541         goto end;
00542     }
00543 
00544     if(avctx->codec)
00545         goto end;
00546 
00547     avctx->codec = codec;
00548     avctx->codec_id = codec->id;
00549     avctx->frame_number = 0;
00550     if (codec->priv_data_size > 0) {
00551         avctx->priv_data = av_mallocz(codec->priv_data_size);
00552         if (!avctx->priv_data) 
00553             goto end;
00554     } else {
00555         avctx->priv_data = NULL;
00556     }
00557 
00558     if(avctx->coded_width && avctx->coded_height)
00559         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
00560     else if(avctx->width && avctx->height)
00561         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
00562 
00563     if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
00564         av_freep(&avctx->priv_data);
00565         goto end;
00566     }
00567 
00568     ret = avctx->codec->init(avctx);
00569     if (ret < 0) {
00570         av_freep(&avctx->priv_data);
00571         goto end;
00572     }
00573     ret=0;
00574 end:
00575     entangled_thread_counter--;
00576     return ret;
00577 }
00578 
00579 int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
00580                          const short *samples)
00581 {
00582     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
00583         av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
00584         return -1;
00585     }
00586     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
00587         int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
00588         avctx->frame_number++;
00589         return ret;
00590     }else
00591         return 0;
00592 }
00593 
00594 int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
00595                          const AVFrame *pict)
00596 {
00597     if(buf_size < FF_MIN_BUFFER_SIZE){
00598         av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
00599         return -1;
00600     }
00601     if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
00602         return -1;
00603     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
00604         int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
00605         avctx->frame_number++;
00606         emms_c(); //needed to avoid an emms_c() call before every return;
00607     
00608         return ret;
00609     }else
00610         return 0;
00611 }
00612 
00613 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
00614                             const AVSubtitle *sub)
00615 {
00616     int ret;
00617     ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);
00618     avctx->frame_number++;
00619     return ret;
00620 }
00621 
00631 int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture, 
00632                          int *got_picture_ptr,
00633                          uint8_t *buf, int buf_size)
00634 {
00635     int ret;
00636     
00637     *got_picture_ptr= 0;
00638     if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
00639         return -1;
00640     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
00641         ret = avctx->codec->decode(avctx, picture, got_picture_ptr, 
00642                                 buf, buf_size);
00643 
00644         emms_c(); //needed to avoid an emms_c() call before every return;
00645     
00646         if (*got_picture_ptr)                           
00647             avctx->frame_number++;
00648     }else
00649         ret= 0;
00650 
00651     return ret;
00652 }
00653 
00654 /* decode an audio frame. return -1 if error, otherwise return the
00655    *number of bytes used. If no frame could be decompressed,
00656    *frame_size_ptr is zero. Otherwise, it is the decompressed frame
00657    *size in BYTES. */
00658 int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples, 
00659                          int *frame_size_ptr,
00660                          uint8_t *buf, int buf_size)
00661 {
00662     int ret;
00663 
00664     *frame_size_ptr= 0;
00665     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
00666         ret = avctx->codec->decode(avctx, samples, frame_size_ptr, 
00667                                 buf, buf_size);
00668         avctx->frame_number++;
00669     }else
00670         ret= 0;
00671     return ret;
00672 }
00673 
00674 /* decode a subtitle message. return -1 if error, otherwise return the
00675    *number of bytes used. If no subtitle could be decompressed,
00676    *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */
00677 int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
00678                             int *got_sub_ptr,
00679                             const uint8_t *buf, int buf_size)
00680 {
00681     int ret;
00682 
00683     *got_sub_ptr = 0;
00684     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, 
00685                                (uint8_t *)buf, buf_size);
00686     if (*got_sub_ptr)
00687         avctx->frame_number++;
00688     return ret;
00689 }
00690 
00691 int avcodec_close(AVCodecContext *avctx)
00692 {
00693     entangled_thread_counter++;
00694     if(entangled_thread_counter != 1){
00695         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
00696         entangled_thread_counter--;
00697         return -1;
00698     }
00699 
00700     if (avctx->codec->close)
00701         avctx->codec->close(avctx);
00702     avcodec_default_free_buffers(avctx);
00703     av_freep(&avctx->priv_data);
00704     avctx->codec = NULL;
00705     entangled_thread_counter--;
00706     return 0;
00707 }
00708 
00709 AVCodec *avcodec_find_encoder(enum CodecID id)
00710 {
00711     AVCodec *p;
00712     p = first_avcodec;
00713     while (p) {
00714         if (p->encode != NULL && p->id == id)
00715             return p;
00716         p = p->next;
00717     }
00718     return NULL;
00719 }
00720 
00721 AVCodec *avcodec_find_encoder_by_name(const char *name)
00722 {
00723     AVCodec *p;
00724     p = first_avcodec;
00725     while (p) {
00726         if (p->encode != NULL && strcmp(name,p->name) == 0)
00727             return p;
00728         p = p->next;
00729     }
00730     return NULL;
00731 }
00732 
00733 AVCodec *avcodec_find_decoder(enum CodecID id)
00734 {
00735     AVCodec *p;
00736     p = first_avcodec;
00737     while (p) {
00738         if (p->decode != NULL && p->id == id)
00739             return p;
00740         p = p->next;
00741     }
00742     return NULL;
00743 }
00744 
00745 AVCodec *avcodec_find_decoder_by_name(const char *name)
00746 {
00747     AVCodec *p;
00748     p = first_avcodec;
00749     while (p) {
00750         if (p->decode != NULL && strcmp(name,p->name) == 0)
00751             return p;
00752         p = p->next;
00753     }
00754     return NULL;
00755 }
00756 
00757 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
00758 {
00759     const char *codec_name;
00760     AVCodec *p;
00761     char buf1[32];
00762     char channels_str[100];
00763     int bitrate;
00764 
00765     if (encode)
00766         p = avcodec_find_encoder(enc->codec_id);
00767     else
00768         p = avcodec_find_decoder(enc->codec_id);
00769 
00770     if (p) {
00771         codec_name = p->name;
00772         if (!encode && enc->codec_id == CODEC_ID_MP3) {
00773             if (enc->sub_id == 2)
00774                 codec_name = "mp2";
00775             else if (enc->sub_id == 1)
00776                 codec_name = "mp1";
00777         }
00778     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
00779         /* fake mpeg2 transport stream codec (currently not
00780            registered) */
00781         codec_name = "mpeg2ts";
00782     } else if (enc->codec_name[0] != '\0') {
00783         codec_name = enc->codec_name;
00784     } else {
00785         /* output avi tags */
00786         if (enc->codec_type == CODEC_TYPE_VIDEO) {
00787             snprintf(buf1, sizeof(buf1), "%c%c%c%c", 
00788                      enc->codec_tag & 0xff,
00789                      (enc->codec_tag >> 8) & 0xff,
00790                      (enc->codec_tag >> 16) & 0xff,
00791                      (enc->codec_tag >> 24) & 0xff);
00792         } else {
00793             snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
00794         }
00795         codec_name = buf1;
00796     }
00797 
00798     switch(enc->codec_type) {
00799     case CODEC_TYPE_VIDEO:
00800         snprintf(buf, buf_size,
00801                  "Video: %s%s",
00802                  codec_name, enc->mb_decision ? " (hq)" : "");
00803         if (enc->pix_fmt != PIX_FMT_NONE) {
00804             snprintf(buf + strlen(buf), buf_size - strlen(buf),
00805                      ", %s",
00806                      avcodec_get_pix_fmt_name(enc->pix_fmt));
00807         }
00808         if (enc->width) {
00809             snprintf(buf + strlen(buf), buf_size - strlen(buf),
00810                      ", %dx%d, %0.2f fps",
00811                      enc->width, enc->height, 
00812                      1/av_q2d(enc->time_base));
00813         }
00814         if (encode) {
00815             snprintf(buf + strlen(buf), buf_size - strlen(buf),
00816                      ", q=%d-%d", enc->qmin, enc->qmax);
00817         }
00818         bitrate = enc->bit_rate;
00819         break;
00820     case CODEC_TYPE_AUDIO:
00821         snprintf(buf, buf_size,
00822                  "Audio: %s",
00823                  codec_name);
00824         switch (enc->channels) {
00825             case 1:
00826                 strcpy(channels_str, "mono");
00827                 break;
00828             case 2:
00829                 strcpy(channels_str, "stereo");
00830                 break;
00831             case 6:
00832                 strcpy(channels_str, "5:1");
00833                 break;
00834             default:
00835                 snprintf(channels_str, sizeof(channels_str), "%d channels", enc->channels);
00836                 break;
00837         }
00838         if (enc->sample_rate) {
00839             snprintf(buf + strlen(buf), buf_size - strlen(buf),
00840                      ", %d Hz, %s",
00841                      enc->sample_rate,
00842                      channels_str);
00843         }
00844         
00845         /* for PCM codecs, compute bitrate directly */
00846         switch(enc->codec_id) {
00847         case CODEC_ID_PCM_S16LE:
00848         case CODEC_ID_PCM_S16BE:
00849         case CODEC_ID_PCM_U16LE:
00850         case CODEC_ID_PCM_U16BE:
00851             bitrate = enc->sample_rate * enc->channels * 16;
00852             break;
00853         case CODEC_ID_PCM_S8:
00854         case CODEC_ID_PCM_U8:
00855         case CODEC_ID_PCM_ALAW:
00856         case CODEC_ID_PCM_MULAW:
00857             bitrate = enc->sample_rate * enc->channels * 8;
00858             break;
00859         default:
00860             bitrate = enc->bit_rate;
00861             break;
00862         }
00863         break;
00864     case CODEC_TYPE_DATA:
00865         snprintf(buf, buf_size, "Data: %s", codec_name);
00866         bitrate = enc->bit_rate;
00867         break;
00868     case CODEC_TYPE_SUBTITLE:
00869         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
00870         bitrate = enc->bit_rate;
00871         break;
00872     default:
00873         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
00874         return;
00875     }
00876     if (encode) {
00877         if (enc->flags & CODEC_FLAG_PASS1)
00878             snprintf(buf + strlen(buf), buf_size - strlen(buf),
00879                      ", pass 1");
00880         if (enc->flags & CODEC_FLAG_PASS2)
00881             snprintf(buf + strlen(buf), buf_size - strlen(buf),
00882                      ", pass 2");
00883     }
00884     if (bitrate != 0) {
00885         snprintf(buf + strlen(buf), buf_size - strlen(buf), 
00886                  ", %d kb/s", bitrate / 1000);
00887     }
00888 }
00889 
00890 unsigned avcodec_version( void )
00891 {
00892   return LIBAVCODEC_VERSION_INT;
00893 }
00894 
00895 unsigned avcodec_build( void )
00896 {
00897   return LIBAVCODEC_BUILD;
00898 }
00899 
00900 /* must be called before any other functions */
00901 void avcodec_init(void)
00902 {
00903     static int inited = 0;
00904 
00905     if (inited != 0)
00906         return;
00907     inited = 1;
00908 
00909     dsputil_static_init();
00910 }
00911 
00915 void avcodec_flush_buffers(AVCodecContext *avctx)
00916 {
00917     if(avctx->codec->flush)
00918         avctx->codec->flush(avctx);
00919 }
00920 
00921 void avcodec_default_free_buffers(AVCodecContext *s){
00922     int i, j;
00923 
00924     if(s->internal_buffer==NULL) return;
00925     
00926     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
00927         InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
00928         for(j=0; j<4; j++){
00929             av_freep(&buf->base[j]);
00930             buf->data[j]= NULL;
00931         }
00932     }
00933     av_freep(&s->internal_buffer);
00934     
00935     s->internal_buffer_count=0;
00936 }
00937 
00938 char av_get_pict_type_char(int pict_type){
00939     switch(pict_type){
00940     case I_TYPE: return 'I'; 
00941     case P_TYPE: return 'P'; 
00942     case B_TYPE: return 'B'; 
00943     case S_TYPE: return 'S'; 
00944     case SI_TYPE:return 'i'; 
00945     case SP_TYPE:return 'p'; 
00946     default:     return '?';
00947     }
00948 }
00949 
00950 int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
00951     AVRational a0={0,1}, a1={1,0};
00952     int sign= (nom<0) ^ (den<0);
00953     int64_t gcd= ff_gcd(ABS(nom), ABS(den));
00954 
00955     nom = ABS(nom)/gcd;
00956     den = ABS(den)/gcd;
00957     if(nom<=max && den<=max){
00958         a1= (AVRational){nom, den};
00959         den=0;
00960     }
00961     
00962     while(den){
00963         int64_t x       = nom / den;
00964         int64_t next_den= nom - den*x;
00965         int64_t a2n= x*a1.num + a0.num;
00966         int64_t a2d= x*a1.den + a0.den;
00967 
00968         if(a2n > max || a2d > max) break;
00969 
00970         a0= a1;
00971         a1= (AVRational){a2n, a2d};
00972         nom= den;
00973         den= next_den;
00974     }
00975     assert(ff_gcd(a1.num, a1.den) == 1);
00976     
00977     *dst_nom = sign ? -a1.num : a1.num;
00978     *dst_den = a1.den;
00979     
00980     return den==0;
00981 }
00982 
00983 int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
00984     AVInteger ai;
00985     int64_t r=0;
00986     assert(c > 0);
00987     assert(b >=0);
00988     assert(rnd >=0 && rnd<=5 && rnd!=4);
00989     
00990     if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1)); 
00991     
00992     if(rnd==AV_ROUND_NEAR_INF) r= c/2;
00993     else if(rnd&1)             r= c-1;
00994 
00995     if(b<=INT_MAX && c<=INT_MAX){
00996         if(a<=INT_MAX)
00997