• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/ffv1.c

Go to the documentation of this file.
00001 /*
00002  * FFV1 codec for libavcodec
00003  *
00004  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "put_bits.h"
00031 #include "dsputil.h"
00032 #include "rangecoder.h"
00033 #include "golomb.h"
00034 #include "mathops.h"
00035 #include "libavutil/avassert.h"
00036 
00037 #define MAX_PLANES 4
00038 #define CONTEXT_SIZE 32
00039 
00040 #define MAX_QUANT_TABLES 8
00041 #define MAX_CONTEXT_INPUTS 5
00042 
00043 extern const uint8_t ff_log2_run[41];
00044 
00045 static const int8_t quant5_10bit[256]={
00046  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
00047  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00048  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00049  1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00050  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00051  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00052  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00053  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00054 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00055 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00056 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00057 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00058 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,
00059 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00060 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00061 -1,-1,-1,-1,-1,-1,-0,-0,-0,-0,-0,-0,-0,-0,-0,-0,
00062 };
00063 
00064 static const int8_t quant5[256]={
00065  0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00066  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00067  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00068  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00069  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00070  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00071  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00072  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00073 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00074 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00075 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00076 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00077 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00078 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00079 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00080 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
00081 };
00082 
00083 static const int8_t quant9_10bit[256]={
00084  0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
00085  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
00086  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
00087  3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
00088  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00089  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00090  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00091  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00092 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00093 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00094 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00095 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00096 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,
00097 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
00098 -3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
00099 -2,-2,-2,-2,-1,-1,-1,-1,-1,-1,-1,-1,-0,-0,-0,-0,
00100 };
00101 
00102 static const int8_t quant11[256]={
00103  0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
00104  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00105  4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00106  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00107  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00108  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00109  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00110  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00111 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00112 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00113 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00114 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00115 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
00116 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
00117 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
00118 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
00119 };
00120 
00121 static const uint8_t ver2_state[256]= {
00122    0,  10,  10,  10,  10,  16,  16,  16,  28,  16,  16,  29,  42,  49,  20,  49,
00123   59,  25,  26,  26,  27,  31,  33,  33,  33,  34,  34,  37,  67,  38,  39,  39,
00124   40,  40,  41,  79,  43,  44,  45,  45,  48,  48,  64,  50,  51,  52,  88,  52,
00125   53,  74,  55,  57,  58,  58,  74,  60, 101,  61,  62,  84,  66,  66,  68,  69,
00126   87,  82,  71,  97,  73,  73,  82,  75, 111,  77,  94,  78,  87,  81,  83,  97,
00127   85,  83,  94,  86,  99,  89,  90,  99, 111,  92,  93, 134,  95,  98, 105,  98,
00128  105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
00129  115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
00130  165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
00131  147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
00132  172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
00133  175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
00134  197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
00135  209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
00136  226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
00137  241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
00138 };
00139 
00140 typedef struct VlcState{
00141     int16_t drift;
00142     uint16_t error_sum;
00143     int8_t bias;
00144     uint8_t count;
00145 } VlcState;
00146 
00147 typedef struct PlaneContext{
00148     int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00149     int quant_table_index;
00150     int context_count;
00151     uint8_t (*state)[CONTEXT_SIZE];
00152     VlcState *vlc_state;
00153     uint8_t interlace_bit_state[2];
00154 } PlaneContext;
00155 
00156 #define MAX_SLICES 256
00157 
00158 typedef struct FFV1Context{
00159     AVCodecContext *avctx;
00160     RangeCoder c;
00161     GetBitContext gb;
00162     PutBitContext pb;
00163     uint64_t rc_stat[256][2];
00164     uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
00165     int version;
00166     int width, height;
00167     int chroma_h_shift, chroma_v_shift;
00168     int transparency;
00169     int flags;
00170     int picture_number;
00171     AVFrame picture;
00172     int plane_count;
00173     int ac;                              
00174     PlaneContext plane[MAX_PLANES];
00175     int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00176     int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
00177     int context_count[MAX_QUANT_TABLES];
00178     uint8_t state_transition[256];
00179     uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
00180     int run_index;
00181     int colorspace;
00182     int16_t *sample_buffer;
00183     int gob_count;
00184     int packed_at_lsb;
00185 
00186     int quant_table_count;
00187 
00188     DSPContext dsp;
00189 
00190     struct FFV1Context *slice_context[MAX_SLICES];
00191     int slice_count;
00192     int num_v_slices;
00193     int num_h_slices;
00194     int slice_width;
00195     int slice_height;
00196     int slice_x;
00197     int slice_y;
00198 }FFV1Context;
00199 
00200 static av_always_inline int fold(int diff, int bits){
00201     if(bits==8)
00202         diff= (int8_t)diff;
00203     else{
00204         diff+= 1<<(bits-1);
00205         diff&=(1<<bits)-1;
00206         diff-= 1<<(bits-1);
00207     }
00208 
00209     return diff;
00210 }
00211 
00212 static inline int predict(int16_t *src, int16_t *last)
00213 {
00214     const int LT= last[-1];
00215     const int  T= last[ 0];
00216     const int L =  src[-1];
00217 
00218     return mid_pred(L, L + T - LT, T);
00219 }
00220 
00221 static inline int get_context(PlaneContext *p, int16_t *src,
00222                               int16_t *last, int16_t *last2)
00223 {
00224     const int LT= last[-1];
00225     const int  T= last[ 0];
00226     const int RT= last[ 1];
00227     const int L =  src[-1];
00228 
00229     if(p->quant_table[3][127]){
00230         const int TT= last2[0];
00231         const int LL=  src[-2];
00232         return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF]
00233               +p->quant_table[3][(LL-L) & 0xFF] + p->quant_table[4][(TT-T) & 0xFF];
00234     }else
00235         return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
00236 }
00237 
00238 static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256]){
00239     int i,j,k,m;
00240     double l2tab[256];
00241 
00242     for(i=1; i<256; i++)
00243         l2tab[i]= log2(i/256.0);
00244 
00245     for(i=0; i<256; i++){
00246         double best_len[256];
00247         double p= i/256.0;
00248 
00249         for(j=0; j<256; j++)
00250             best_len[j]= 1<<30;
00251 
00252         for(j=FFMAX(i-10,1); j<FFMIN(i+11,256); j++){
00253             double occ[256]={0};
00254             double len=0;
00255             occ[j]=1.0;
00256             for(k=0; k<256; k++){
00257                 double newocc[256]={0};
00258                 for(m=1; m<256; m++){
00259                     if(occ[m]){
00260                         len -=occ[m]*(     p *l2tab[    m]
00261                                       + (1-p)*l2tab[256-m]);
00262                     }
00263                 }
00264                 if(len < best_len[k]){
00265                     best_len[k]= len;
00266                     best_state[i][k]= j;
00267                 }
00268                 for(m=0; m<256; m++){
00269                     if(occ[m]){
00270                         newocc[    one_state[    m]] += occ[m]*   p ;
00271                         newocc[256-one_state[256-m]] += occ[m]*(1-p);
00272                     }
00273                 }
00274                 memcpy(occ, newocc, sizeof(occ));
00275             }
00276         }
00277     }
00278 }
00279 
00280 static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2]){
00281     int i;
00282 
00283 #define put_rac(C,S,B) \
00284 do{\
00285     if(rc_stat){\
00286     rc_stat[*(S)][B]++;\
00287         rc_stat2[(S)-state][B]++;\
00288     }\
00289     put_rac(C,S,B);\
00290 }while(0)
00291 
00292     if(v){
00293         const int a= FFABS(v);
00294         const int e= av_log2(a);
00295         put_rac(c, state+0, 0);
00296         if(e<=9){
00297             for(i=0; i<e; i++){
00298                 put_rac(c, state+1+i, 1);  //1..10
00299             }
00300             put_rac(c, state+1+i, 0);
00301 
00302             for(i=e-1; i>=0; i--){
00303                 put_rac(c, state+22+i, (a>>i)&1); //22..31
00304             }
00305 
00306             if(is_signed)
00307                 put_rac(c, state+11 + e, v < 0); //11..21
00308         }else{
00309             for(i=0; i<e; i++){
00310                 put_rac(c, state+1+FFMIN(i,9), 1);  //1..10
00311             }
00312             put_rac(c, state+1+9, 0);
00313 
00314             for(i=e-1; i>=0; i--){
00315                 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
00316             }
00317 
00318             if(is_signed)
00319                 put_rac(c, state+11 + 10, v < 0); //11..21
00320         }
00321     }else{
00322         put_rac(c, state+0, 1);
00323     }
00324 #undef put_rac
00325 }
00326 
00327 static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00328     put_symbol_inline(c, state, v, is_signed, NULL, NULL);
00329 }
00330 
00331 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
00332     if(get_rac(c, state+0))
00333         return 0;
00334     else{
00335         int i, e, a;
00336         e= 0;
00337         while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
00338             e++;
00339         }
00340 
00341         a= 1;
00342         for(i=e-1; i>=0; i--){
00343             a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
00344         }
00345 
00346         e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
00347         return (a^e)-e;
00348     }
00349 }
00350 
00351 static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00352     return get_symbol_inline(c, state, is_signed);
00353 }
00354 
00355 static inline void update_vlc_state(VlcState * const state, const int v){
00356     int drift= state->drift;
00357     int count= state->count;
00358     state->error_sum += FFABS(v);
00359     drift += v;
00360 
00361     if(count == 128){ //FIXME variable
00362         count >>= 1;
00363         drift >>= 1;
00364         state->error_sum >>= 1;
00365     }
00366     count++;
00367 
00368     if(drift <= -count){
00369         if(state->bias > -128) state->bias--;
00370 
00371         drift += count;
00372         if(drift <= -count)
00373             drift= -count + 1;
00374     }else if(drift > 0){
00375         if(state->bias <  127) state->bias++;
00376 
00377         drift -= count;
00378         if(drift > 0)
00379             drift= 0;
00380     }
00381 
00382     state->drift= drift;
00383     state->count= count;
00384 }
00385 
00386 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00387     int i, k, code;
00388 //printf("final: %d ", v);
00389     v = fold(v - state->bias, bits);
00390 
00391     i= state->count;
00392     k=0;
00393     while(i < state->error_sum){ //FIXME optimize
00394         k++;
00395         i += i;
00396     }
00397 
00398     assert(k<=8);
00399 
00400 #if 0 // JPEG LS
00401     if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00402     else                                         code= v;
00403 #else
00404      code= v ^ ((2*state->drift + state->count)>>31);
00405 #endif
00406 
00407 //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k);
00408     set_sr_golomb(pb, code, k, 12, bits);
00409 
00410     update_vlc_state(state, v);
00411 }
00412 
00413 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00414     int k, i, v, ret;
00415 
00416     i= state->count;
00417     k=0;
00418     while(i < state->error_sum){ //FIXME optimize
00419         k++;
00420         i += i;
00421     }
00422 
00423     assert(k<=8);
00424 
00425     v= get_sr_golomb(gb, k, 12, bits);
00426 //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
00427 
00428 #if 0 // JPEG LS
00429     if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00430 #else
00431      v ^= ((2*state->drift + state->count)>>31);
00432 #endif
00433 
00434     ret= fold(v + state->bias, bits);
00435 
00436     update_vlc_state(state, v);
00437 //printf("final: %d\n", ret);
00438     return ret;
00439 }
00440 
00441 #if CONFIG_FFV1_ENCODER
00442 static av_always_inline int encode_line(FFV1Context *s, int w,
00443                                         int16_t *sample[2],
00444                                         int plane_index, int bits)
00445 {
00446     PlaneContext * const p= &s->plane[plane_index];
00447     RangeCoder * const c= &s->c;
00448     int x;
00449     int run_index= s->run_index;
00450     int run_count=0;
00451     int run_mode=0;
00452 
00453     if(s->ac){
00454         if(c->bytestream_end - c->bytestream < w*20){
00455             av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00456             return -1;
00457         }
00458     }else{
00459         if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00460             av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00461             return -1;
00462         }
00463     }
00464 
00465     for(x=0; x<w; x++){
00466         int diff, context;
00467 
00468         context= get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
00469         diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00470 
00471         if(context < 0){
00472             context = -context;
00473             diff= -diff;
00474         }
00475 
00476         diff= fold(diff, bits);
00477 
00478         if(s->ac){
00479             if(s->flags & CODEC_FLAG_PASS1){
00480                 put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat, s->rc_stat2[p->quant_table_index][context]);
00481             }else{
00482                 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
00483             }
00484         }else{
00485             if(context == 0) run_mode=1;
00486 
00487             if(run_mode){
00488 
00489                 if(diff){
00490                     while(run_count >= 1<<ff_log2_run[run_index]){
00491                         run_count -= 1<<ff_log2_run[run_index];
00492                         run_index++;
00493                         put_bits(&s->pb, 1, 1);
00494                     }
00495 
00496                     put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
00497                     if(run_index) run_index--;
00498                     run_count=0;
00499                     run_mode=0;
00500                     if(diff>0) diff--;
00501                 }else{
00502                     run_count++;
00503                 }
00504             }
00505 
00506 //            printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)put_bits_count(&s->pb));
00507 
00508             if(run_mode == 0)
00509                 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00510         }
00511     }
00512     if(run_mode){
00513         while(run_count >= 1<<ff_log2_run[run_index]){
00514             run_count -= 1<<ff_log2_run[run_index];
00515             run_index++;
00516             put_bits(&s->pb, 1, 1);
00517         }
00518 
00519         if(run_count)
00520             put_bits(&s->pb, 1, 1);
00521     }
00522     s->run_index= run_index;
00523 
00524     return 0;
00525 }
00526 
00527 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00528     int x,y,i;
00529     const int ring_size= s->avctx->context_model ? 3 : 2;
00530     int16_t *sample[3];
00531     s->run_index=0;
00532 
00533     memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
00534 
00535     for(y=0; y<h; y++){
00536         for(i=0; i<ring_size; i++)
00537             sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
00538 
00539         sample[0][-1]= sample[1][0  ];
00540         sample[1][ w]= sample[1][w-1];
00541 //{START_TIMER
00542         if(s->avctx->bits_per_raw_sample<=8){
00543             for(x=0; x<w; x++){
00544                 sample[0][x]= src[x + stride*y];
00545             }
00546             encode_line(s, w, sample, plane_index, 8);
00547         }else{
00548             if(s->packed_at_lsb){
00549                 for(x=0; x<w; x++){
00550                     sample[0][x]= ((uint16_t*)(src + stride*y))[x];
00551                 }
00552             }else{
00553                 for(x=0; x<w; x++){
00554                     sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
00555                 }
00556             }
00557             encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
00558         }
00559 //STOP_TIMER("encode line")}
00560     }
00561 }
00562 
00563 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00564     int x, y, p, i;
00565     const int ring_size= s->avctx->context_model ? 3 : 2;
00566     int16_t *sample[4][3];
00567     s->run_index=0;
00568 
00569     memset(s->sample_buffer, 0, ring_size*4*(w+6)*sizeof(*s->sample_buffer));
00570 
00571     for(y=0; y<h; y++){
00572         for(i=0; i<ring_size; i++)
00573             for(p=0; p<4; p++)
00574                 sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
00575 
00576         for(x=0; x<w; x++){
00577             unsigned v= src[x + stride*y];
00578             int b= v&0xFF;
00579             int g= (v>>8)&0xFF;
00580             int r= (v>>16)&0xFF;
00581             int a=  v>>24;
00582 
00583             b -= g;
00584             r -= g;
00585             g += (b + r)>>2;
00586             b += 0x100;
00587             r += 0x100;
00588 
00589 //            assert(g>=0 && b>=0 && r>=0);
00590 //            assert(g<256 && b<512 && r<512);
00591             sample[0][0][x]= g;
00592             sample[1][0][x]= b;
00593             sample[2][0][x]= r;
00594             sample[3][0][x]= a;
00595         }
00596         for(p=0; p<3 + s->transparency; p++){
00597             sample[p][0][-1]= sample[p][1][0  ];
00598             sample[p][1][ w]= sample[p][1][w-1];
00599             encode_line(s, w, sample[p], (p+1)/2, 9);
00600         }
00601     }
00602 }
00603 
00604 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00605     int last=0;
00606     int i;
00607     uint8_t state[CONTEXT_SIZE];
00608     memset(state, 128, sizeof(state));
00609 
00610     for(i=1; i<128 ; i++){
00611         if(quant_table[i] != quant_table[i-1]){
00612             put_symbol(c, state, i-last-1, 0);
00613             last= i;
00614         }
00615     }
00616     put_symbol(c, state, i-last-1, 0);
00617 }
00618 
00619 static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
00620     int i;
00621     for(i=0; i<5; i++)
00622         write_quant_table(c, quant_table[i]);
00623 }
00624 
00625 static void write_header(FFV1Context *f){
00626     uint8_t state[CONTEXT_SIZE];
00627     int i, j;
00628     RangeCoder * const c= &f->slice_context[0]->c;
00629 
00630     memset(state, 128, sizeof(state));
00631 
00632     if(f->version < 2){
00633         put_symbol(c, state, f->version, 0);
00634         put_symbol(c, state, f->ac, 0);
00635         if(f->ac>1){
00636             for(i=1; i<256; i++){
00637                 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00638             }
00639         }
00640         put_symbol(c, state, f->colorspace, 0); //YUV cs type
00641         if(f->version>0)
00642             put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
00643         put_rac(c, state, 1); //chroma planes
00644             put_symbol(c, state, f->chroma_h_shift, 0);
00645             put_symbol(c, state, f->chroma_v_shift, 0);
00646         put_rac(c, state, f->transparency);
00647 
00648         write_quant_tables(c, f->quant_table);
00649     }else{
00650         put_symbol(c, state, f->slice_count, 0);
00651         for(i=0; i<f->slice_count; i++){
00652             FFV1Context *fs= f->slice_context[i];
00653             put_symbol(c, state, (fs->slice_x     +1)*f->num_h_slices / f->width   , 0);
00654             put_symbol(c, state, (fs->slice_y     +1)*f->num_v_slices / f->height  , 0);
00655             put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
00656             put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
00657             for(j=0; j<f->plane_count; j++){
00658                 put_symbol(c, state, f->plane[j].quant_table_index, 0);
00659                 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
00660             }
00661         }
00662     }
00663 }
00664 #endif /* CONFIG_FFV1_ENCODER */
00665 
00666 static av_cold int common_init(AVCodecContext *avctx){
00667     FFV1Context *s = avctx->priv_data;
00668 
00669     s->avctx= avctx;
00670     s->flags= avctx->flags;
00671 
00672     avcodec_get_frame_defaults(&s->picture);
00673 
00674     dsputil_init(&s->dsp, avctx);
00675 
00676     s->width = avctx->width;
00677     s->height= avctx->height;
00678 
00679     assert(s->width && s->height);
00680     //defaults
00681     s->num_h_slices=1;
00682     s->num_v_slices=1;
00683 
00684 
00685     return 0;
00686 }
00687 
00688 static int init_slice_state(FFV1Context *f){
00689     int i, j;
00690 
00691     for(i=0; i<f->slice_count; i++){
00692         FFV1Context *fs= f->slice_context[i];
00693         fs->plane_count= f->plane_count;
00694         fs->transparency= f->transparency;
00695         for(j=0; j<f->plane_count; j++){
00696             PlaneContext * const p= &fs->plane[j];
00697 
00698             if(fs->ac){
00699                 if(!p->    state) p->    state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00700                 if(!p->    state)
00701                     return AVERROR(ENOMEM);
00702             }else{
00703                 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00704                 if(!p->vlc_state)
00705                     return AVERROR(ENOMEM);
00706             }
00707         }
00708 
00709         if (fs->ac>1){
00710             //FIXME only redo if state_transition changed
00711             for(j=1; j<256; j++){
00712                 fs->c.one_state [    j]= fs->state_transition[j];
00713                 fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
00714             }
00715         }
00716     }
00717 
00718     return 0;
00719 }
00720 
00721 static av_cold int init_slice_contexts(FFV1Context *f){
00722     int i;
00723 
00724     f->slice_count= f->num_h_slices * f->num_v_slices;
00725 
00726     for(i=0; i<f->slice_count; i++){
00727         FFV1Context *fs= av_mallocz(sizeof(*fs));
00728         int sx= i % f->num_h_slices;
00729         int sy= i / f->num_h_slices;
00730         int sxs= f->avctx->width * sx    / f->num_h_slices;
00731         int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
00732         int sys= f->avctx->height* sy    / f->num_v_slices;
00733         int sye= f->avctx->height*(sy+1) / f->num_v_slices;
00734         f->slice_context[i]= fs;
00735         memcpy(fs, f, sizeof(*fs));
00736         memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
00737 
00738         fs->slice_width = sxe - sxs;
00739         fs->slice_height= sye - sys;
00740         fs->slice_x     = sxs;
00741         fs->slice_y     = sys;
00742 
00743         fs->sample_buffer = av_malloc(3*4 * (fs->width+6) * sizeof(*fs->sample_buffer));
00744         if (!fs->sample_buffer)
00745             return AVERROR(ENOMEM);
00746     }
00747     return 0;
00748 }
00749 
00750 static int allocate_initial_states(FFV1Context *f){
00751     int i;
00752 
00753     for(i=0; i<f->quant_table_count; i++){
00754         f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
00755         if(!f->initial_states[i])
00756             return AVERROR(ENOMEM);
00757         memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
00758     }
00759     return 0;
00760 }
00761 
00762 #if CONFIG_FFV1_ENCODER
00763 static int write_extra_header(FFV1Context *f){
00764     RangeCoder * const c= &f->c;
00765     uint8_t state[CONTEXT_SIZE];
00766     int i, j, k;
00767     uint8_t state2[32][CONTEXT_SIZE];
00768 
00769     memset(state2, 128, sizeof(state2));
00770     memset(state, 128, sizeof(state));
00771 
00772     f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
00773     ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
00774     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00775 
00776     put_symbol(c, state, f->version, 0);
00777     put_symbol(c, state, f->ac, 0);
00778     if(f->ac>1){
00779         for(i=1; i<256; i++){
00780             put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00781         }
00782     }
00783     put_symbol(c, state, f->colorspace, 0); //YUV cs type
00784     put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
00785     put_rac(c, state, 1); //chroma planes
00786         put_symbol(c, state, f->chroma_h_shift, 0);
00787         put_symbol(c, state, f->chroma_v_shift, 0);
00788     put_rac(c, state, f->transparency);
00789     put_symbol(c, state, f->num_h_slices-1, 0);
00790     put_symbol(c, state, f->num_v_slices-1, 0);
00791 
00792     put_symbol(c, state, f->quant_table_count, 0);
00793     for(i=0; i<f->quant_table_count; i++)
00794         write_quant_tables(c, f->quant_tables[i]);
00795 
00796     for(i=0; i<f->quant_table_count; i++){
00797         for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
00798             if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
00799                 break;
00800         if(j<f->context_count[i]*CONTEXT_SIZE){
00801             put_rac(c, state, 1);
00802             for(j=0; j<f->context_count[i]; j++){
00803                 for(k=0; k<CONTEXT_SIZE; k++){
00804                     int pred= j ? f->initial_states[i][j-1][k] : 128;
00805                     put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
00806                 }
00807             }
00808         }else{
00809             put_rac(c, state, 0);
00810         }
00811     }
00812 
00813     f->avctx->extradata_size= ff_rac_terminate(c);
00814 
00815     return 0;
00816 }
00817 
00818 static int sort_stt(FFV1Context *s, uint8_t stt[256]){
00819     int i,i2,changed,print=0;
00820 
00821     do{
00822         changed=0;
00823         for(i=12; i<244; i++){
00824             for(i2=i+1; i2<245 && i2<i+4; i2++){
00825 #define COST(old, new) \
00826     s->rc_stat[old][0]*-log2((256-(new))/256.0)\
00827    +s->rc_stat[old][1]*-log2(     (new) /256.0)
00828 
00829 #define COST2(old, new) \
00830     COST(old, new)\
00831    +COST(256-(old), 256-(new))
00832 
00833                 double size0= COST2(i, i ) + COST2(i2, i2);
00834                 double sizeX= COST2(i, i2) + COST2(i2, i );
00835                 if(sizeX < size0 && i!=128 && i2!=128){
00836                     int j;
00837                     FFSWAP(int, stt[    i], stt[    i2]);
00838                     FFSWAP(int, s->rc_stat[i    ][0],s->rc_stat[    i2][0]);
00839                     FFSWAP(int, s->rc_stat[i    ][1],s->rc_stat[    i2][1]);
00840                     if(i != 256-i2){
00841                         FFSWAP(int, stt[256-i], stt[256-i2]);
00842                         FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
00843                         FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
00844                     }
00845                     for(j=1; j<256; j++){
00846                         if     (stt[j] == i ) stt[j] = i2;
00847                         else if(stt[j] == i2) stt[j] = i ;
00848                         if(i != 256-i2){
00849                             if     (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
00850                             else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
00851                         }
00852                     }
00853                     print=changed=1;
00854                 }
00855             }
00856         }
00857     }while(changed);
00858     return print;
00859 }
00860 
00861 static av_cold int encode_init(AVCodecContext *avctx)
00862 {
00863     FFV1Context *s = avctx->priv_data;
00864     int i, j, k, m;
00865 
00866     common_init(avctx);
00867 
00868     s->version=0;
00869     s->ac= avctx->coder_type ? 2:0;
00870 
00871     if(s->ac>1)
00872         for(i=1; i<256; i++)
00873             s->state_transition[i]=ver2_state[i];
00874 
00875     s->plane_count=3;
00876     for(i=0; i<256; i++){
00877         s->quant_table_count=2;
00878         if(avctx->bits_per_raw_sample <=8){
00879             s->quant_tables[0][0][i]=           quant11[i];
00880             s->quant_tables[0][1][i]=        11*quant11[i];
00881             s->quant_tables[0][2][i]=     11*11*quant11[i];
00882             s->quant_tables[1][0][i]=           quant11[i];
00883             s->quant_tables[1][1][i]=        11*quant11[i];
00884             s->quant_tables[1][2][i]=     11*11*quant5 [i];
00885             s->quant_tables[1][3][i]=   5*11*11*quant5 [i];
00886             s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
00887         }else{
00888             s->quant_tables[0][0][i]=           quant9_10bit[i];
00889             s->quant_tables[0][1][i]=        11*quant9_10bit[i];
00890             s->quant_tables[0][2][i]=     11*11*quant9_10bit[i];
00891             s->quant_tables[1][0][i]=           quant9_10bit[i];
00892             s->quant_tables[1][1][i]=        11*quant9_10bit[i];
00893             s->quant_tables[1][2][i]=     11*11*quant5_10bit[i];
00894             s->quant_tables[1][3][i]=   5*11*11*quant5_10bit[i];
00895             s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
00896         }
00897     }
00898     s->context_count[0]= (11*11*11+1)/2;
00899     s->context_count[1]= (11*11*5*5*5+1)/2;
00900     memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
00901 
00902     for(i=0; i<s->plane_count; i++){
00903         PlaneContext * const p= &s->plane[i];
00904 
00905         memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
00906         p->quant_table_index= avctx->context_model;
00907         p->context_count= s->context_count[p->quant_table_index];
00908     }
00909 
00910     if(allocate_initial_states(s) < 0)
00911         return AVERROR(ENOMEM);
00912 
00913     avctx->coded_frame= &s->picture;
00914     switch(avctx->pix_fmt){
00915     case PIX_FMT_YUV420P9:
00916     case PIX_FMT_YUV420P10:
00917     case PIX_FMT_YUV422P10:
00918         s->packed_at_lsb = 1;
00919     case PIX_FMT_YUV444P16:
00920     case PIX_FMT_YUV422P16:
00921     case PIX_FMT_YUV420P16:
00922         if(avctx->bits_per_raw_sample <=8){
00923             av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
00924             return -1;
00925         }
00926         if(!s->ac){
00927             av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
00928             return -1;
00929         }
00930         s->version= FFMAX(s->version, 1);
00931     case PIX_FMT_YUV444P:
00932     case PIX_FMT_YUV422P:
00933     case PIX_FMT_YUV420P:
00934     case PIX_FMT_YUV411P:
00935     case PIX_FMT_YUV410P:
00936         s->colorspace= 0;
00937         break;
00938     case PIX_FMT_RGB32:
00939         s->colorspace= 1;
00940         s->transparency= 1;
00941         break;
00942     case PIX_FMT_0RGB32:
00943         s->colorspace= 1;
00944         break;
00945     default:
00946         av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00947         return -1;
00948     }
00949     if(!s->transparency)
00950         s->plane_count= 2;
00951     avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00952 
00953     s->picture_number=0;
00954 
00955     if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
00956         for(i=0; i<s->quant_table_count; i++){
00957             s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
00958             if(!s->rc_stat2[i])
00959                 return AVERROR(ENOMEM);
00960         }
00961     }
00962     if(avctx->stats_in){
00963         char *p= avctx->stats_in;
00964         uint8_t best_state[256][256];
00965         int gob_count=0;
00966         char *next;
00967 
00968         av_assert0(s->version>=2);
00969 
00970         for(;;){
00971             for(j=0; j<256; j++){
00972                 for(i=0; i<2; i++){
00973                     s->rc_stat[j][i]= strtol(p, &next, 0);
00974                     if(next==p){
00975                         av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
00976                         return -1;
00977                     }
00978                     p=next;
00979                 }
00980             }
00981             for(i=0; i<s->quant_table_count; i++){
00982                 for(j=0; j<s->context_count[i]; j++){
00983                     for(k=0; k<32; k++){
00984                         for(m=0; m<2; m++){
00985                             s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
00986                             if(next==p){
00987                                 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
00988                                 return -1;
00989                             }
00990                             p=next;
00991                         }
00992                     }
00993                 }
00994             }
00995             gob_count= strtol(p, &next, 0);
00996             if(next==p || gob_count <=0){
00997                 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
00998                 return -1;
00999             }
01000             p=next;
01001             while(*p=='\n' || *p==' ') p++;
01002             if(p[0]==0) break;
01003         }
01004         sort_stt(s, s->state_transition);
01005 
01006         find_best_state(best_state, s->state_transition);
01007 
01008         for(i=0; i<s->quant_table_count; i++){
01009             for(j=0; j<s->context_count[i]; j++){
01010                 for(k=0; k<32; k++){
01011                     double p= 128;
01012                     if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
01013                         p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
01014                     }
01015                     s->initial_states[i][j][k]= best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1])/gob_count, 0, 255)];
01016                 }
01017             }
01018         }
01019     }
01020 
01021     if(s->version>1){
01022         s->num_h_slices=2;
01023         s->num_v_slices=2;
01024         write_extra_header(s);
01025     }
01026 
01027     if(init_slice_contexts(s) < 0)
01028         return -1;
01029     if(init_slice_state(s) < 0)
01030         return -1;
01031 
01032 #define STATS_OUT_SIZE 1024*1024*6
01033     if(avctx->flags & CODEC_FLAG_PASS1){
01034         avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
01035         for(i=0; i<s->quant_table_count; i++){
01036             for(j=0; j<s->slice_count; j++){
01037                 FFV1Context *sf= s->slice_context[j];
01038                 av_assert0(!sf->rc_stat2[i]);
01039                 sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
01040                 if(!sf->rc_stat2[i])
01041                     return AVERROR(ENOMEM);
01042             }
01043         }
01044     }
01045 
01046     return 0;
01047 }
01048 #endif /* CONFIG_FFV1_ENCODER */
01049 
01050 
01051 static void clear_state(FFV1Context *f){
01052     int i, si, j;
01053 
01054     for(si=0; si<f->slice_count; si++){
01055         FFV1Context *fs= f->slice_context[si];
01056         for(i=0; i<f->plane_count; i++){
01057             PlaneContext *p= &fs->plane[i];
01058 
01059             p->interlace_bit_state[0]= 128;
01060             p->interlace_bit_state[1]= 128;
01061 
01062             if(fs->ac){
01063                 if(f->initial_states[p->quant_table_index]){
01064                     memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
01065                 }else
01066                 memset(p->state, 128, CONTEXT_SIZE*p->context_count);
01067             }else{
01068             for(j=0; j<p->context_count; j++){
01069                     p->vlc_state[j].drift= 0;
01070                     p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
01071                     p->vlc_state[j].bias= 0;
01072                     p->vlc_state[j].count= 1;
01073             }
01074             }
01075         }
01076     }
01077 }
01078 
01079 #if CONFIG_FFV1_ENCODER
01080 static int encode_slice(AVCodecContext *c, void *arg){
01081     FFV1Context *fs= *(void**)arg;
01082     FFV1Context *f= fs->avctx->priv_data;
01083     int width = fs->slice_width;
01084     int height= fs->slice_height;
01085     int x= fs->slice_x;
01086     int y= fs->slice_y;
01087     AVFrame * const p= &f->picture;
01088     const int ps= (c->bits_per_raw_sample>8)+1;
01089 
01090     if(f->colorspace==0){
01091         const int chroma_width = -((-width )>>f->chroma_h_shift);
01092         const int chroma_height= -((-height)>>f->chroma_v_shift);
01093         const int cx= x>>f->chroma_h_shift;
01094         const int cy= y>>f->chroma_v_shift;
01095 
01096         encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
01097 
01098         encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01099         encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
01100     }else{
01101         encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + ps*x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
01102     }
01103     emms_c();
01104 
01105     return 0;
01106 }
01107 
01108 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
01109     FFV1Context *f = avctx->priv_data;
01110     RangeCoder * const c= &f->slice_context[0]->c;
01111     AVFrame *pict = data;
01112     AVFrame * const p= &f->picture;
01113     int used_count= 0;
01114     uint8_t keystate=128;
01115     uint8_t *buf_p;
01116     int i;
01117 
01118     ff_init_range_encoder(c, buf, buf_size);
01119     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01120 
01121     *p = *pict;
01122     p->pict_type= AV_PICTURE_TYPE_I;
01123 
01124     if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
01125         put_rac(c, &keystate, 1);
01126         p->key_frame= 1;
01127         f->gob_count++;
01128         write_header(f);
01129         clear_state(f);
01130     }else{
01131         put_rac(c, &keystate, 0);
01132         p->key_frame= 0;
01133     }
01134 
01135     if(!f->ac){
01136         used_count += ff_rac_terminate(c);
01137 //printf("pos=%d\n", used_count);
01138         init_put_bits(&f->slice_context[0]->pb, buf + used_count, buf_size - used_count);
01139     }else if (f->ac>1){
01140         int i;
01141         for(i=1; i<256; i++){
01142             c->one_state[i]= f->state_transition[i];
01143             c->zero_state[256-i]= 256-c->one_state[i];
01144         }
01145     }
01146 
01147     for(i=1; i<f->slice_count; i++){
01148         FFV1Context *fs= f->slice_context[i];
01149         uint8_t *start= buf + (buf_size-used_count)*i/f->slice_count;
01150         int len= buf_size/f->slice_count;
01151 
01152         if(fs->ac){
01153             ff_init_range_encoder(&fs->c, start, len);
01154         }else{
01155             init_put_bits(&fs->pb, start, len);
01156         }
01157     }
01158     avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
01159 
01160     buf_p=buf;
01161     for(i=0; i<f->slice_count; i++){
01162         FFV1Context *fs= f->slice_context[i];
01163         int bytes;
01164 
01165         if(fs->ac){
01166             uint8_t state=128;
01167             put_rac(&fs->c, &state, 0);
01168             bytes= ff_rac_terminate(&fs->c);
01169         }else{
01170             flush_put_bits(&fs->pb); //nicer padding FIXME
01171             bytes= used_count + (put_bits_count(&fs->pb)+7)/8;
01172             used_count= 0;
01173         }
01174         if(i>0){
01175             av_assert0(bytes < buf_size/f->slice_count);
01176             memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
01177             av_assert0(bytes < (1<<24));
01178             AV_WB24(buf_p+bytes, bytes);
01179             bytes+=3;
01180         }
01181         buf_p += bytes;
01182     }
01183 
01184     if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
01185         int j, k, m;
01186         char *p= avctx->stats_out;
01187         char *end= p + STATS_OUT_SIZE;
01188 
01189         memset(f->rc_stat, 0, sizeof(f->rc_stat));
01190         for(i=0; i<f->quant_table_count; i++)
01191             memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
01192 
01193         for(j=0; j<f->slice_count; j++){
01194             FFV1Context *fs= f->slice_context[j];
01195             for(i=0; i<256; i++){
01196                 f->rc_stat[i][0] += fs->rc_stat[i][0];
01197                 f->rc_stat[i][1] += fs->rc_stat[i][1];
01198             }
01199             for(i=0; i<f->quant_table_count; i++){
01200                 for(k=0; k<f->context_count[i]; k++){
01201                     for(m=0; m<32; m++){
01202                         f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
01203                         f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
01204                     }
01205                 }
01206             }
01207         }
01208 
01209         for(j=0; j<256; j++){
01210             snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
01211             p+= strlen(p);
01212         }
01213         snprintf(p, end-p, "\n");
01214 
01215         for(i=0; i<f->quant_table_count; i++){
01216             for(j=0; j<f->context_count[i]; j++){
01217                 for(m=0; m<32; m++){
01218                     snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
01219                     p+= strlen(p);
01220                 }
01221             }
01222         }
01223         snprintf(p, end-p, "%d\n", f->gob_count);
01224     } else if(avctx->flags&CODEC_FLAG_PASS1)
01225         avctx->stats_out[0] = '\0';
01226 
01227     f->picture_number++;
01228     return buf_p-buf;
01229 }
01230 #endif /* CONFIG_FFV1_ENCODER */
01231 
01232 static av_cold int common_end(AVCodecContext *avctx){
01233     FFV1Context *s = avctx->priv_data;
01234     int i, j;
01235 
01236     if (avctx->codec->decode && s->picture.data[0])
01237         avctx->release_buffer(avctx, &s->picture);
01238 
01239     for(j=0; j<s->slice_count; j++){
01240         FFV1Context *fs= s->slice_context[j];
01241         for(i=0; i<s->plane_count; i++){
01242             PlaneContext *p= &fs->plane[i];
01243 
01244             av_freep(&p->state);
01245             av_freep(&p->vlc_state);
01246         }
01247         av_freep(&fs->sample_buffer);
01248     }
01249 
01250     av_freep(&avctx->stats_out);
01251     for(j=0; j<s->quant_table_count; j++){
01252         av_freep(&s->initial_states[j]);
01253         for(i=0; i<s->slice_count; i++){
01254             FFV1Context *sf= s->slice_context[i];
01255             av_freep(&sf->rc_stat2[j]);
01256         }
01257         av_freep(&s->rc_stat2[j]);
01258     }
01259 
01260     for(i=0; i<s->slice_count; i++){
01261         av_freep(&s->slice_context[i]);
01262     }
01263 
01264     return 0;
01265 }
01266 
01267 static av_always_inline void decode_line(FFV1Context *s, int w,
01268                                          int16_t *sample[2],
01269                                          int plane_index, int bits)
01270 {
01271     PlaneContext * const p= &s->plane[plane_index];
01272     RangeCoder * const c= &s->c;
01273     int x;
01274     int run_count=0;
01275     int run_mode=0;
01276     int run_index= s->run_index;
01277 
01278     for(x=0; x<w; x++){
01279         int diff, context, sign;
01280 
01281         context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
01282         if(context < 0){
01283             context= -context;
01284             sign=1;
01285         }else
01286             sign=0;
01287 
01288         av_assert2(context < p->context_count);
01289 
01290         if(s->ac){
01291             diff= get_symbol_inline(c, p->state[context], 1);
01292         }else{
01293             if(context == 0 && run_mode==0) run_mode=1;
01294 
01295             if(run_mode){
01296                 if(run_count==0 && run_mode==1){
01297                     if(get_bits1(&s->gb)){
01298                         run_count = 1<<ff_log2_run[run_index];
01299                         if(x + run_count <= w) run_index++;
01300                     }else{
01301                         if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
01302                         else run_count=0;
01303                         if(run_index) run_index--;
01304                         run_mode=2;
01305                     }
01306                 }
01307                 run_count--;
01308                 if(run_count < 0){
01309                     run_mode=0;
01310                     run_count=0;
01311                     diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01312                     if(diff>=0) diff++;
01313                 }else
01314                     diff=0;
01315             }else
01316                 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01317 
01318 //            printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, get_bits_count(&s->gb));
01319         }
01320 
01321         if(sign) diff= -diff;
01322 
01323         sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
01324     }
01325     s->run_index= run_index;
01326 }
01327 
01328 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
01329     int x, y;
01330     int16_t *sample[2];
01331     sample[0]=s->sample_buffer    +3;
01332     sample[1]=s->sample_buffer+w+6+3;
01333 
01334     s->run_index=0;
01335 
01336     memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
01337 
01338     for(y=0; y<h; y++){
01339         int16_t *temp = sample[0]; //FIXME try a normal buffer
01340 
01341         sample[0]= sample[1];
01342         sample[1]= temp;
01343 
01344         sample[1][-1]= sample[0][0  ];
01345         sample[0][ w]= sample[0][w-1];
01346 
01347 //{START_TIMER
01348         if(s->avctx->bits_per_raw_sample <= 8){
01349             decode_line(s, w, sample, plane_index, 8);
01350             for(x=0; x<w; x++){
01351                 src[x + stride*y]= sample[1][x];
01352             }
01353         }else{
01354             decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
01355             if(s->packed_at_lsb){
01356                 for(x=0; x<w; x++){
01357                     ((uint16_t*)(src + stride*y))[x]= sample[1][x];
01358                 }
01359             }else{
01360                 for(x=0; x<w; x++){
01361                     ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
01362                 }
01363             }
01364         }
01365 //STOP_TIMER("decode-line")}
01366     }
01367 }
01368 
01369 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
01370     int x, y, p;
01371     int16_t *sample[4][2];
01372     for(x=0; x<4; x++){
01373         sample[x][0] = s->sample_buffer +  x*2   *(w+6) + 3;
01374         sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
01375     }
01376 
01377     s->run_index=0;
01378 
01379     memset(s->sample_buffer, 0, 8*(w+6)*sizeof(*s->sample_buffer));
01380 
01381     for(y=0; y<h; y++){
01382         for(p=0; p<3 + s->transparency; p++){
01383             int16_t *temp = sample[p][0]; //FIXME try a normal buffer
01384 
01385             sample[p][0]= sample[p][1];
01386             sample[p][1]= temp;
01387 
01388             sample[p][1][-1]= sample[p][0][0  ];
01389             sample[p][0][ w]= sample[p][0][w-1];
01390             decode_line(s, w, sample[p], (p+1)/2, 9);
01391         }
01392         for(x=0; x<w; x++){
01393             int g= sample[0][1][x];
01394             int b= sample[1][1][x];
01395             int r= sample[2][1][x];
01396             int a= sample[3][1][x];
01397 
01398 //            assert(g>=0 && b>=0 && r>=0);
01399 //            assert(g<256 && b<512 && r<512);
01400 
01401             b -= 0x100;
01402             r -= 0x100;
01403             g -= (b + r)>>2;
01404             b += g;
01405             r += g;
01406 
01407             src[x + stride*y]= b + (g<<8) + (r<<16) + (a<<24);
01408         }
01409     }
01410 }
01411 
01412 static int decode_slice(AVCodecContext *c, void *arg){
01413     FFV1Context *fs= *(void**)arg;
01414     FFV1Context *f= fs->avctx->priv_data;
01415     int width = fs->slice_width;
01416     int height= fs->slice_height;
01417     int x= fs->slice_x;
01418     int y= fs->slice_y;
01419     const int ps= (c->bits_per_raw_sample>8)+1;
01420     AVFrame * const p= &f->picture;
01421 
01422     av_assert1(width && height);
01423     if(f->colorspace==0){
01424         const int chroma_width = -((-width )>>f->chroma_h_shift);
01425         const int chroma_height= -((-height)>>f->chroma_v_shift);
01426         const int cx= x>>f->chroma_h_shift;
01427         const int cy= y>>f->chroma_v_shift;
01428         decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
01429 
01430         decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01431         decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[2], 1);
01432     }else{
01433         decode_rgb_frame(fs, (uint32_t*)p->data[0] + ps*x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
01434     }
01435 
01436     emms_c();
01437 
01438     return 0;
01439 }
01440 
01441 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
01442     int v;
01443     int i=0;
01444     uint8_t state[CONTEXT_SIZE];
01445 
01446     memset(state, 128, sizeof(state));
01447 
01448     for(v=0; i<128 ; v++){
01449         int len= get_symbol(c, state, 0) + 1;
01450 
01451         if(len + i > 128) return -1;
01452 
01453         while(len--){
01454             quant_table[i] = scale*v;
01455             i++;
01456 //printf("%2d ",v);
01457 //if(i%16==0) printf("\n");
01458         }
01459     }
01460 
01461     for(i=1; i<128; i++){
01462         quant_table[256-i]= -quant_table[i];
01463     }
01464     quant_table[128]= -quant_table[127];
01465 
01466     return 2*v - 1;
01467 }
01468 
01469 static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
01470     int i;
01471     int context_count=1;
01472 
01473     for(i=0; i<5; i++){
01474         context_count*= read_quant_table(c, quant_table[i], context_count);
01475         if(context_count > 32768U){
01476             return -1;
01477         }
01478     }
01479     return (context_count+1)/2;
01480 }
01481 
01482 static int read_extra_header(FFV1Context *f){
01483     RangeCoder * const c= &f->c;
01484     uint8_t state[CONTEXT_SIZE];
01485     int i, j, k;
01486     uint8_t state2[32][CONTEXT_SIZE];
01487 
01488     memset(state2, 128, sizeof(state2));
01489     memset(state, 128, sizeof(state));
01490 
01491     ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
01492     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01493 
01494     f->version= get_symbol(c, state, 0);
01495     f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01496     if(f->ac>1){
01497         for(i=1; i<256; i++){
01498             f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01499         }
01500     }
01501     f->colorspace= get_symbol(c, state, 0); //YUV cs type
01502     f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01503     get_rac(c, state); //no chroma = false
01504     f->chroma_h_shift= get_symbol(c, state, 0);
01505     f->chroma_v_shift= get_symbol(c, state, 0);
01506     f->transparency= get_rac(c, state);
01507     f->plane_count= 2 + f->transparency;
01508     f->num_h_slices= 1 + get_symbol(c, state, 0);
01509     f->num_v_slices= 1 + get_symbol(c, state, 0);
01510     if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
01511         av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
01512         return -1;
01513     }
01514 
01515     f->quant_table_count= get_symbol(c, state, 0);
01516     if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
01517         return -1;
01518     for(i=0; i<f->quant_table_count; i++){
01519         if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
01520             av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01521             return -1;
01522         }
01523     }
01524 
01525     if(allocate_initial_states(f) < 0)
01526         return AVERROR(ENOMEM);
01527 
01528     for(i=0; i<f->quant_table_count; i++){
01529         if(get_rac(c, state)){
01530             for(j=0; j<f->context_count[i]; j++){
01531                 for(k=0; k<CONTEXT_SIZE; k++){
01532                     int pred= j ? f->initial_states[i][j-1][k] : 128;
01533                     f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
01534                 }
01535             }
01536         }
01537     }
01538 
01539     return 0;
01540 }
01541 
01542 static int read_header(FFV1Context *f){
01543     uint8_t state[CONTEXT_SIZE];
01544     int i, j, context_count;
01545     RangeCoder * const c= &f->slice_context[0]->c;
01546 
01547     memset(state, 128, sizeof(state));
01548 
01549     if(f->version < 2){
01550         f->version= get_symbol(c, state, 0);
01551         f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01552         if(f->ac>1){
01553             for(i=1; i<256; i++){
01554                 f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01555             }
01556         }
01557         f->colorspace= get_symbol(c, state, 0); //YUV cs type
01558         if(f->version>0)
01559             f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01560         get_rac(c, state); //no chroma = false
01561         f->chroma_h_shift= get_symbol(c, state, 0);
01562         f->chroma_v_shift= get_symbol(c, state, 0);
01563         f->transparency= get_rac(c, state);
01564         f->plane_count= 2 + f->transparency;
01565     }
01566 
01567     if(f->colorspace==0){
01568         if(f->avctx->bits_per_raw_sample<=8){
01569             switch(16*f->chroma_h_shift + f->chroma_v_shift){
01570             case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
01571             case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
01572             case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
01573             case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
01574             case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
01575             default:
01576                 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01577                 return -1;
01578             }
01579         }else if(f->avctx->bits_per_raw_sample==9) {
01580             switch(16*f->chroma_h_shift + f->chroma_v_shift){
01581             case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
01582             case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
01583             case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P9 ; f->packed_at_lsb=1; break;
01584             default:
01585                 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01586                 return -1;
01587             }
01588         }else if(f->avctx->bits_per_raw_sample==10) {
01589             switch(16*f->chroma_h_shift + f->chroma_v_shift){
01590             case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
01591             case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P10; f->packed_at_lsb=1; break;
01592             case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P10; f->packed_at_lsb=1; break;
01593             default:
01594                 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01595                 return -1;
01596             }
01597         }else {
01598             switch(16*f->chroma_h_shift + f->chroma_v_shift){
01599             case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
01600             case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
01601             case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
01602             default:
01603                 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01604                 return -1;
01605             }
01606         }
01607     }else if(f->colorspace==1){
01608         if(f->chroma_h_shift || f->chroma_v_shift){
01609             av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
01610             return -1;
01611         }
01612         if(f->transparency) f->avctx->pix_fmt= PIX_FMT_RGB32;
01613         else                f->avctx->pix_fmt= PIX_FMT_0RGB32;
01614     }else{
01615         av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
01616         return -1;
01617     }
01618 
01619 //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
01620     if(f->version < 2){
01621         context_count= read_quant_tables(c, f->quant_table);
01622         if(context_count < 0){
01623                 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01624                 return -1;
01625         }
01626     }else{
01627         f->slice_count= get_symbol(c, state, 0);
01628         if(f->slice_count > (unsigned)MAX_SLICES)
01629             return -1;
01630     }
01631 
01632     for(j=0; j<f->slice_count; j++){
01633         FFV1Context *fs= f->slice_context[j];
01634         fs->ac= f->ac;
01635         fs->packed_at_lsb= f->packed_at_lsb;
01636 
01637         if(f->version >= 2){
01638             fs->slice_x     = get_symbol(c, state, 0)   *f->width ;
01639             fs->slice_y     = get_symbol(c, state, 0)   *f->height;
01640             fs->slice_width =(get_symbol(c, state, 0)+1)*f->width  + fs->slice_x;
01641             fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
01642 
01643             fs->slice_x /= f->num_h_slices;
01644             fs->slice_y /= f->num_v_slices;
01645             fs->slice_width  = fs->slice_width /f->num_h_slices - fs->slice_x;
01646             fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
01647             if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
01648                 return -1;
01649             if(    (unsigned)fs->slice_x + (uint64_t)fs->slice_width  > f->width
01650                 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
01651                 return -1;
01652         }
01653 
01654         for(i=0; i<f->plane_count; i++){
01655             PlaneContext * const p= &fs->plane[i];
01656 
01657             if(f->version >= 2){
01658                 int idx=get_symbol(c, state, 0);
01659                 if(idx > (unsigned)f->quant_table_count){
01660                     av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
01661                     return -1;
01662                 }
01663                 p->quant_table_index= idx;
01664                 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
01665                 context_count= f->context_count[idx];
01666             }else{
01667                 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
01668             }
01669 
01670             if(p->context_count < context_count){
01671                 av_freep(&p->state);
01672                 av_freep(&p->vlc_state);
01673             }
01674             p->context_count= context_count;
01675         }
01676     }
01677 
01678     return 0;
01679 }
01680 
01681 static av_cold int decode_init(AVCodecContext *avctx)
01682 {
01683     FFV1Context *f = avctx->priv_data;
01684 
01685     common_init(avctx);
01686 
01687     if(avctx->extradata && read_extra_header(f) < 0)
01688         return -1;
01689 
01690     if(init_slice_contexts(f) < 0)
01691         return -1;
01692 
01693     return 0;
01694 }
01695 
01696 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
01697     const uint8_t *buf = avpkt->data;
01698     int buf_size = avpkt->size;
01699     FFV1Context *f = avctx->priv_data;
01700     RangeCoder * const c= &f->slice_context[0]->c;
01701     AVFrame * const p= &f->picture;
01702     int bytes_read, i;
01703     uint8_t keystate= 128;
01704     const uint8_t *buf_p;
01705 
01706     AVFrame *picture = data;
01707 
01708     /* release previously stored data */
01709     if (p->data[0])
01710         avctx->release_buffer(avctx, p);
01711 
01712     ff_init_range_decoder(c, buf, buf_size);
01713     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01714 
01715 
01716     p->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
01717     if(get_rac(c, &keystate)){
01718         p->key_frame= 1;
01719         if(read_header(f) < 0)
01720             return -1;
01721         if(init_slice_state(f) < 0)
01722             return -1;
01723 
01724         clear_state(f);
01725     }else{
01726         p->key_frame= 0;
01727     }
01728     if(f->ac>1){
01729         int i;
01730         for(i=1; i<256; i++){
01731             c->one_state[i]= f->state_transition[i];
01732             c->zero_state[256-i]= 256-c->one_state[i];
01733         }
01734     }
01735 
01736     p->reference= 0;
01737     if(avctx->get_buffer(avctx, p) < 0){
01738         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01739         return -1;
01740     }
01741 
01742     if(avctx->debug&FF_DEBUG_PICT_INFO)
01743         av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
01744 
01745     if(!f->ac){
01746         bytes_read = c->bytestream - c->bytestream_start - 1;
01747         if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
01748 //printf("pos=%d\n", bytes_read);
01749         init_get_bits(&f->slice_context[0]->gb, buf + bytes_read, (buf_size - bytes_read) * 8);
01750     } else {
01751         bytes_read = 0; /* avoid warning */
01752     }
01753 
01754     buf_p= buf + buf_size;
01755     for(i=f->slice_count-1; i>0; i--){
01756         FFV1Context *fs= f->slice_context[i];
01757         int v= AV_RB24(buf_p-3)+3;
01758         if(buf_p - buf <= v){
01759             av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
01760             return -1;
01761         }
01762         buf_p -= v;
01763         if(fs->ac){
01764             ff_init_range_decoder(&fs->c, buf_p, v);
01765         }else{
01766             init_get_bits(&fs->gb, buf_p, v * 8);
01767         }
01768     }
01769 
01770     avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
01771     f->picture_number++;
01772 
01773     *picture= *p;
01774     *data_size = sizeof(AVFrame);
01775 
01776     return buf_size;
01777 }
01778 
01779 AVCodec ff_ffv1_decoder = {
01780     .name           = "ffv1",
01781     .type           = AVMEDIA_TYPE_VIDEO,
01782     .id             = CODEC_ID_FFV1,
01783     .priv_data_size = sizeof(FFV1Context),
01784     .init           = decode_init,
01785     .close          = common_end,
01786     .decode         = decode_frame,
01787     .capabilities   = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ | CODEC_CAP_SLICE_THREADS,
01788     .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
01789 };
01790 
01791 #if CONFIG_FFV1_ENCODER
01792 AVCodec ff_ffv1_encoder = {
01793     .name           = "ffv1",
01794     .type           = AVMEDIA_TYPE_VIDEO,
01795     .id             = CODEC_ID_FFV1,
01796     .priv_data_size = sizeof(FFV1Context),
01797     .init           = encode_init,
01798     .encode         = encode_frame,
01799     .close          = common_end,
01800     .capabilities = CODEC_CAP_SLICE_THREADS,
01801     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_0RGB32, PIX_FMT_RGB32, PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_YUV420P9, PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_NONE},
01802     .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
01803 };
01804 #endif
Generated on Fri Feb 1 2013 14:34:34 for FFmpeg by doxygen 1.7.1