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 Libav.
00007  *
00008  * Libav 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  * Libav 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 Libav; 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 flags;
00169     int picture_number;
00170     AVFrame picture;
00171     int plane_count;
00172     int ac;                              
00173     PlaneContext plane[MAX_PLANES];
00174     int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00175     int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
00176     int context_count[MAX_QUANT_TABLES];
00177     uint8_t state_transition[256];
00178     uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
00179     int run_index;
00180     int colorspace;
00181     int16_t *sample_buffer;
00182     int gob_count;
00183 
00184     int quant_table_count;
00185 
00186     DSPContext dsp;
00187 
00188     struct FFV1Context *slice_context[MAX_SLICES];
00189     int slice_count;
00190     int num_v_slices;
00191     int num_h_slices;
00192     int slice_width;
00193     int slice_height;
00194     int slice_x;
00195     int slice_y;
00196 }FFV1Context;
00197 
00198 static av_always_inline int fold(int diff, int bits){
00199     if(bits==8)
00200         diff= (int8_t)diff;
00201     else{
00202         diff+= 1<<(bits-1);
00203         diff&=(1<<bits)-1;
00204         diff-= 1<<(bits-1);
00205     }
00206 
00207     return diff;
00208 }
00209 
00210 static inline int predict(int16_t *src, int16_t *last)
00211 {
00212     const int LT= last[-1];
00213     const int  T= last[ 0];
00214     const int L =  src[-1];
00215 
00216     return mid_pred(L, L + T - LT, T);
00217 }
00218 
00219 static inline int get_context(PlaneContext *p, int16_t *src,
00220                               int16_t *last, int16_t *last2)
00221 {
00222     const int LT= last[-1];
00223     const int  T= last[ 0];
00224     const int RT= last[ 1];
00225     const int L =  src[-1];
00226 
00227     if(p->quant_table[3][127]){
00228         const int TT= last2[0];
00229         const int LL=  src[-2];
00230         return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF]
00231               +p->quant_table[3][(LL-L) & 0xFF] + p->quant_table[4][(TT-T) & 0xFF];
00232     }else
00233         return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
00234 }
00235 
00236 static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256]){
00237     int i,j,k,m;
00238     double l2tab[256];
00239 
00240     for(i=1; i<256; i++)
00241         l2tab[i]= log2(i/256.0);
00242 
00243     for(i=0; i<256; i++){
00244         double best_len[256];
00245         double p= i/256.0;
00246 
00247         for(j=0; j<256; j++)
00248             best_len[j]= 1<<30;
00249 
00250         for(j=FFMAX(i-10,1); j<FFMIN(i+11,256); j++){
00251             double occ[256]={0};
00252             double len=0;
00253             occ[j]=1.0;
00254             for(k=0; k<256; k++){
00255                 double newocc[256]={0};
00256                 for(m=0; m<256; m++){
00257                     if(occ[m]){
00258                         len -=occ[m]*(     p *l2tab[    m]
00259                                       + (1-p)*l2tab[256-m]);
00260                     }
00261                 }
00262                 if(len < best_len[k]){
00263                     best_len[k]= len;
00264                     best_state[i][k]= j;
00265                 }
00266                 for(m=0; m<256; m++){
00267                     if(occ[m]){
00268                         newocc[    one_state[    m]] += occ[m]*   p ;
00269                         newocc[256-one_state[256-m]] += occ[m]*(1-p);
00270                     }
00271                 }
00272                 memcpy(occ, newocc, sizeof(occ));
00273             }
00274         }
00275     }
00276 }
00277 
00278 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]){
00279     int i;
00280 
00281 #define put_rac(C,S,B) \
00282 do{\
00283     if(rc_stat){\
00284     rc_stat[*(S)][B]++;\
00285         rc_stat2[(S)-state][B]++;\
00286     }\
00287     put_rac(C,S,B);\
00288 }while(0)
00289 
00290     if(v){
00291         const int a= FFABS(v);
00292         const int e= av_log2(a);
00293         put_rac(c, state+0, 0);
00294         if(e<=9){
00295             for(i=0; i<e; i++){
00296                 put_rac(c, state+1+i, 1);  //1..10
00297             }
00298             put_rac(c, state+1+i, 0);
00299 
00300             for(i=e-1; i>=0; i--){
00301                 put_rac(c, state+22+i, (a>>i)&1); //22..31
00302             }
00303 
00304             if(is_signed)
00305                 put_rac(c, state+11 + e, v < 0); //11..21
00306         }else{
00307             for(i=0; i<e; i++){
00308                 put_rac(c, state+1+FFMIN(i,9), 1);  //1..10
00309             }
00310             put_rac(c, state+1+9, 0);
00311 
00312             for(i=e-1; i>=0; i--){
00313                 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
00314             }
00315 
00316             if(is_signed)
00317                 put_rac(c, state+11 + 10, v < 0); //11..21
00318         }
00319     }else{
00320         put_rac(c, state+0, 1);
00321     }
00322 #undef put_rac
00323 }
00324 
00325 static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
00326     put_symbol_inline(c, state, v, is_signed, NULL, NULL);
00327 }
00328 
00329 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
00330     if(get_rac(c, state+0))
00331         return 0;
00332     else{
00333         int i, e, a;
00334         e= 0;
00335         while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
00336             e++;
00337         }
00338 
00339         a= 1;
00340         for(i=e-1; i>=0; i--){
00341             a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
00342         }
00343 
00344         e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
00345         return (a^e)-e;
00346     }
00347 }
00348 
00349 static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
00350     return get_symbol_inline(c, state, is_signed);
00351 }
00352 
00353 static inline void update_vlc_state(VlcState * const state, const int v){
00354     int drift= state->drift;
00355     int count= state->count;
00356     state->error_sum += FFABS(v);
00357     drift += v;
00358 
00359     if(count == 128){ //FIXME variable
00360         count >>= 1;
00361         drift >>= 1;
00362         state->error_sum >>= 1;
00363     }
00364     count++;
00365 
00366     if(drift <= -count){
00367         if(state->bias > -128) state->bias--;
00368 
00369         drift += count;
00370         if(drift <= -count)
00371             drift= -count + 1;
00372     }else if(drift > 0){
00373         if(state->bias <  127) state->bias++;
00374 
00375         drift -= count;
00376         if(drift > 0)
00377             drift= 0;
00378     }
00379 
00380     state->drift= drift;
00381     state->count= count;
00382 }
00383 
00384 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
00385     int i, k, code;
00386 //printf("final: %d ", v);
00387     v = fold(v - state->bias, bits);
00388 
00389     i= state->count;
00390     k=0;
00391     while(i < state->error_sum){ //FIXME optimize
00392         k++;
00393         i += i;
00394     }
00395 
00396     assert(k<=8);
00397 
00398 #if 0 // JPEG LS
00399     if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
00400     else                                         code= v;
00401 #else
00402      code= v ^ ((2*state->drift + state->count)>>31);
00403 #endif
00404 
00405 //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);
00406     set_sr_golomb(pb, code, k, 12, bits);
00407 
00408     update_vlc_state(state, v);
00409 }
00410 
00411 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
00412     int k, i, v, ret;
00413 
00414     i= state->count;
00415     k=0;
00416     while(i < state->error_sum){ //FIXME optimize
00417         k++;
00418         i += i;
00419     }
00420 
00421     assert(k<=8);
00422 
00423     v= get_sr_golomb(gb, k, 12, bits);
00424 //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
00425 
00426 #if 0 // JPEG LS
00427     if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
00428 #else
00429      v ^= ((2*state->drift + state->count)>>31);
00430 #endif
00431 
00432     ret= fold(v + state->bias, bits);
00433 
00434     update_vlc_state(state, v);
00435 //printf("final: %d\n", ret);
00436     return ret;
00437 }
00438 
00439 #if CONFIG_FFV1_ENCODER
00440 static av_always_inline int encode_line(FFV1Context *s, int w,
00441                                         int16_t *sample[2],
00442                                         int plane_index, int bits)
00443 {
00444     PlaneContext * const p= &s->plane[plane_index];
00445     RangeCoder * const c= &s->c;
00446     int x;
00447     int run_index= s->run_index;
00448     int run_count=0;
00449     int run_mode=0;
00450 
00451     if(s->ac){
00452         if(c->bytestream_end - c->bytestream < w*20){
00453             av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00454             return -1;
00455         }
00456     }else{
00457         if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
00458             av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00459             return -1;
00460         }
00461     }
00462 
00463     for(x=0; x<w; x++){
00464         int diff, context;
00465 
00466         context= get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
00467         diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
00468 
00469         if(context < 0){
00470             context = -context;
00471             diff= -diff;
00472         }
00473 
00474         diff= fold(diff, bits);
00475 
00476         if(s->ac){
00477             if(s->flags & CODEC_FLAG_PASS1){
00478                 put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat, s->rc_stat2[p->quant_table_index][context]);
00479             }else{
00480                 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
00481             }
00482         }else{
00483             if(context == 0) run_mode=1;
00484 
00485             if(run_mode){
00486 
00487                 if(diff){
00488                     while(run_count >= 1<<ff_log2_run[run_index]){
00489                         run_count -= 1<<ff_log2_run[run_index];
00490                         run_index++;
00491                         put_bits(&s->pb, 1, 1);
00492                     }
00493 
00494                     put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
00495                     if(run_index) run_index--;
00496                     run_count=0;
00497                     run_mode=0;
00498                     if(diff>0) diff--;
00499                 }else{
00500                     run_count++;
00501                 }
00502             }
00503 
00504 //            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));
00505 
00506             if(run_mode == 0)
00507                 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
00508         }
00509     }
00510     if(run_mode){
00511         while(run_count >= 1<<ff_log2_run[run_index]){
00512             run_count -= 1<<ff_log2_run[run_index];
00513             run_index++;
00514             put_bits(&s->pb, 1, 1);
00515         }
00516 
00517         if(run_count)
00518             put_bits(&s->pb, 1, 1);
00519     }
00520     s->run_index= run_index;
00521 
00522     return 0;
00523 }
00524 
00525 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
00526     int x,y,i;
00527     const int ring_size= s->avctx->context_model ? 3 : 2;
00528     int16_t *sample[3];
00529     s->run_index=0;
00530 
00531     memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
00532 
00533     for(y=0; y<h; y++){
00534         for(i=0; i<ring_size; i++)
00535             sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
00536 
00537         sample[0][-1]= sample[1][0  ];
00538         sample[1][ w]= sample[1][w-1];
00539 //{START_TIMER
00540         if(s->avctx->bits_per_raw_sample<=8){
00541             for(x=0; x<w; x++){
00542                 sample[0][x]= src[x + stride*y];
00543             }
00544             encode_line(s, w, sample, plane_index, 8);
00545         }else{
00546             for(x=0; x<w; x++){
00547                 sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
00548             }
00549             encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
00550         }
00551 //STOP_TIMER("encode line")}
00552     }
00553 }
00554 
00555 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
00556     int x, y, p, i;
00557     const int ring_size= s->avctx->context_model ? 3 : 2;
00558     int16_t *sample[3][3];
00559     s->run_index=0;
00560 
00561     memset(s->sample_buffer, 0, ring_size*3*(w+6)*sizeof(*s->sample_buffer));
00562 
00563     for(y=0; y<h; y++){
00564         for(i=0; i<ring_size; i++)
00565             for(p=0; p<3; p++)
00566                 sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
00567 
00568         for(x=0; x<w; x++){
00569             int v= src[x + stride*y];
00570             int b= v&0xFF;
00571             int g= (v>>8)&0xFF;
00572             int r= (v>>16)&0xFF;
00573 
00574             b -= g;
00575             r -= g;
00576             g += (b + r)>>2;
00577             b += 0x100;
00578             r += 0x100;
00579 
00580 //            assert(g>=0 && b>=0 && r>=0);
00581 //            assert(g<256 && b<512 && r<512);
00582             sample[0][0][x]= g;
00583             sample[1][0][x]= b;
00584             sample[2][0][x]= r;
00585         }
00586         for(p=0; p<3; p++){
00587             sample[p][0][-1]= sample[p][1][0  ];
00588             sample[p][1][ w]= sample[p][1][w-1];
00589             encode_line(s, w, sample[p], FFMIN(p, 1), 9);
00590         }
00591     }
00592 }
00593 
00594 static void write_quant_table(RangeCoder *c, int16_t *quant_table){
00595     int last=0;
00596     int i;
00597     uint8_t state[CONTEXT_SIZE];
00598     memset(state, 128, sizeof(state));
00599 
00600     for(i=1; i<128 ; i++){
00601         if(quant_table[i] != quant_table[i-1]){
00602             put_symbol(c, state, i-last-1, 0);
00603             last= i;
00604         }
00605     }
00606     put_symbol(c, state, i-last-1, 0);
00607 }
00608 
00609 static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
00610     int i;
00611     for(i=0; i<5; i++)
00612         write_quant_table(c, quant_table[i]);
00613 }
00614 
00615 static void write_header(FFV1Context *f){
00616     uint8_t state[CONTEXT_SIZE];
00617     int i, j;
00618     RangeCoder * const c= &f->slice_context[0]->c;
00619 
00620     memset(state, 128, sizeof(state));
00621 
00622     if(f->version < 2){
00623         put_symbol(c, state, f->version, 0);
00624         put_symbol(c, state, f->ac, 0);
00625         if(f->ac>1){
00626             for(i=1; i<256; i++){
00627                 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00628             }
00629         }
00630         put_symbol(c, state, f->colorspace, 0); //YUV cs type
00631         if(f->version>0)
00632             put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
00633         put_rac(c, state, 1); //chroma planes
00634             put_symbol(c, state, f->chroma_h_shift, 0);
00635             put_symbol(c, state, f->chroma_v_shift, 0);
00636         put_rac(c, state, 0); //no transparency plane
00637 
00638         write_quant_tables(c, f->quant_table);
00639     }else{
00640         put_symbol(c, state, f->slice_count, 0);
00641         for(i=0; i<f->slice_count; i++){
00642             FFV1Context *fs= f->slice_context[i];
00643             put_symbol(c, state, (fs->slice_x     +1)*f->num_h_slices / f->width   , 0);
00644             put_symbol(c, state, (fs->slice_y     +1)*f->num_v_slices / f->height  , 0);
00645             put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
00646             put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
00647             for(j=0; j<f->plane_count; j++){
00648                 put_symbol(c, state, f->plane[j].quant_table_index, 0);
00649                 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
00650             }
00651         }
00652     }
00653 }
00654 #endif /* CONFIG_FFV1_ENCODER */
00655 
00656 static av_cold int common_init(AVCodecContext *avctx){
00657     FFV1Context *s = avctx->priv_data;
00658 
00659     s->avctx= avctx;
00660     s->flags= avctx->flags;
00661 
00662     dsputil_init(&s->dsp, avctx);
00663 
00664     s->width = avctx->width;
00665     s->height= avctx->height;
00666 
00667     assert(s->width && s->height);
00668     //defaults
00669     s->num_h_slices=1;
00670     s->num_v_slices=1;
00671 
00672 
00673     return 0;
00674 }
00675 
00676 static int init_slice_state(FFV1Context *f){
00677     int i, j;
00678 
00679     for(i=0; i<f->slice_count; i++){
00680         FFV1Context *fs= f->slice_context[i];
00681         for(j=0; j<f->plane_count; j++){
00682             PlaneContext * const p= &fs->plane[j];
00683 
00684             if(fs->ac){
00685                 if(!p->    state) p->    state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
00686                 if(!p->    state)
00687                     return AVERROR(ENOMEM);
00688             }else{
00689                 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
00690                 if(!p->vlc_state)
00691                     return AVERROR(ENOMEM);
00692             }
00693         }
00694 
00695         if (fs->ac>1){
00696             //FIXME only redo if state_transition changed
00697             for(j=1; j<256; j++){
00698                 fs->c.one_state [    j]= fs->state_transition[j];
00699                 fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
00700             }
00701         }
00702     }
00703 
00704     return 0;
00705 }
00706 
00707 static av_cold int init_slice_contexts(FFV1Context *f){
00708     int i;
00709 
00710     f->slice_count= f->num_h_slices * f->num_v_slices;
00711     if (f->slice_count <= 0) {
00712         av_log(f->avctx, AV_LOG_ERROR, "Invalid number of slices\n");
00713         return AVERROR(EINVAL);
00714     }
00715 
00716     for(i=0; i<f->slice_count; i++){
00717         FFV1Context *fs= av_mallocz(sizeof(*fs));
00718         int sx= i % f->num_h_slices;
00719         int sy= i / f->num_h_slices;
00720         int sxs= f->avctx->width * sx    / f->num_h_slices;
00721         int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
00722         int sys= f->avctx->height* sy    / f->num_v_slices;
00723         int sye= f->avctx->height*(sy+1) / f->num_v_slices;
00724         f->slice_context[i]= fs;
00725         memcpy(fs, f, sizeof(*fs));
00726         memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
00727 
00728         fs->slice_width = sxe - sxs;
00729         fs->slice_height= sye - sys;
00730         fs->slice_x     = sxs;
00731         fs->slice_y     = sys;
00732 
00733         fs->sample_buffer = av_malloc(9 * (fs->width+6) * sizeof(*fs->sample_buffer));
00734         if (!fs->sample_buffer)
00735             return AVERROR(ENOMEM);
00736     }
00737     return 0;
00738 }
00739 
00740 static int allocate_initial_states(FFV1Context *f){
00741     int i;
00742 
00743     for(i=0; i<f->quant_table_count; i++){
00744         f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
00745         if(!f->initial_states[i])
00746             return AVERROR(ENOMEM);
00747         memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
00748     }
00749     return 0;
00750 }
00751 
00752 #if CONFIG_FFV1_ENCODER
00753 static int write_extra_header(FFV1Context *f){
00754     RangeCoder * const c= &f->c;
00755     uint8_t state[CONTEXT_SIZE];
00756     int i, j, k;
00757     uint8_t state2[32][CONTEXT_SIZE];
00758 
00759     memset(state2, 128, sizeof(state2));
00760     memset(state, 128, sizeof(state));
00761 
00762     f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
00763     ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
00764     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00765 
00766     put_symbol(c, state, f->version, 0);
00767     put_symbol(c, state, f->ac, 0);
00768     if(f->ac>1){
00769         for(i=1; i<256; i++){
00770             put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
00771         }
00772     }
00773     put_symbol(c, state, f->colorspace, 0); //YUV cs type
00774     put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
00775     put_rac(c, state, 1); //chroma planes
00776         put_symbol(c, state, f->chroma_h_shift, 0);
00777         put_symbol(c, state, f->chroma_v_shift, 0);
00778     put_rac(c, state, 0); //no transparency plane
00779     put_symbol(c, state, f->num_h_slices-1, 0);
00780     put_symbol(c, state, f->num_v_slices-1, 0);
00781 
00782     put_symbol(c, state, f->quant_table_count, 0);
00783     for(i=0; i<f->quant_table_count; i++)
00784         write_quant_tables(c, f->quant_tables[i]);
00785 
00786     for(i=0; i<f->quant_table_count; i++){
00787         for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
00788             if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
00789                 break;
00790         if(j<f->context_count[i]*CONTEXT_SIZE){
00791             put_rac(c, state, 1);
00792             for(j=0; j<f->context_count[i]; j++){
00793                 for(k=0; k<CONTEXT_SIZE; k++){
00794                     int pred= j ? f->initial_states[i][j-1][k] : 128;
00795                     put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
00796                 }
00797             }
00798         }else{
00799             put_rac(c, state, 0);
00800         }
00801     }
00802 
00803     f->avctx->extradata_size= ff_rac_terminate(c);
00804 
00805     return 0;
00806 }
00807 
00808 static int sort_stt(FFV1Context *s, uint8_t stt[256]){
00809     int i,i2,changed,print=0;
00810 
00811     do{
00812         changed=0;
00813         for(i=12; i<244; i++){
00814             for(i2=i+1; i2<245 && i2<i+4; i2++){
00815 #define COST(old, new) \
00816     s->rc_stat[old][0]*-log2((256-(new))/256.0)\
00817    +s->rc_stat[old][1]*-log2(     (new) /256.0)
00818 
00819 #define COST2(old, new) \
00820     COST(old, new)\
00821    +COST(256-(old), 256-(new))
00822 
00823                 double size0= COST2(i, i ) + COST2(i2, i2);
00824                 double sizeX= COST2(i, i2) + COST2(i2, i );
00825                 if(sizeX < size0 && i!=128 && i2!=128){
00826                     int j;
00827                     FFSWAP(int, stt[    i], stt[    i2]);
00828                     FFSWAP(int, s->rc_stat[i    ][0],s->rc_stat[    i2][0]);
00829                     FFSWAP(int, s->rc_stat[i    ][1],s->rc_stat[    i2][1]);
00830                     if(i != 256-i2){
00831                         FFSWAP(int, stt[256-i], stt[256-i2]);
00832                         FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
00833                         FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
00834                     }
00835                     for(j=1; j<256; j++){
00836                         if     (stt[j] == i ) stt[j] = i2;
00837                         else if(stt[j] == i2) stt[j] = i ;
00838                         if(i != 256-i2){
00839                             if     (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
00840                             else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
00841                         }
00842                     }
00843                     print=changed=1;
00844                 }
00845             }
00846         }
00847     }while(changed);
00848     return print;
00849 }
00850 
00851 static av_cold int encode_init(AVCodecContext *avctx)
00852 {
00853     FFV1Context *s = avctx->priv_data;
00854     int i, j, k, m;
00855 
00856     common_init(avctx);
00857 
00858     s->version=0;
00859     s->ac= avctx->coder_type ? 2:0;
00860 
00861     if(s->ac>1)
00862         for(i=1; i<256; i++)
00863             s->state_transition[i]=ver2_state[i];
00864 
00865     s->plane_count=2;
00866     for(i=0; i<256; i++){
00867         s->quant_table_count=2;
00868         if(avctx->bits_per_raw_sample <=8){
00869             s->quant_tables[0][0][i]=           quant11[i];
00870             s->quant_tables[0][1][i]=        11*quant11[i];
00871             s->quant_tables[0][2][i]=     11*11*quant11[i];
00872             s->quant_tables[1][0][i]=           quant11[i];
00873             s->quant_tables[1][1][i]=        11*quant11[i];
00874             s->quant_tables[1][2][i]=     11*11*quant5 [i];
00875             s->quant_tables[1][3][i]=   5*11*11*quant5 [i];
00876             s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
00877         }else{
00878             s->quant_tables[0][0][i]=           quant9_10bit[i];
00879             s->quant_tables[0][1][i]=        11*quant9_10bit[i];
00880             s->quant_tables[0][2][i]=     11*11*quant9_10bit[i];
00881             s->quant_tables[1][0][i]=           quant9_10bit[i];
00882             s->quant_tables[1][1][i]=        11*quant9_10bit[i];
00883             s->quant_tables[1][2][i]=     11*11*quant5_10bit[i];
00884             s->quant_tables[1][3][i]=   5*11*11*quant5_10bit[i];
00885             s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
00886         }
00887     }
00888     s->context_count[0]= (11*11*11+1)/2;
00889     s->context_count[1]= (11*11*5*5*5+1)/2;
00890     memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
00891 
00892     for(i=0; i<s->plane_count; i++){
00893         PlaneContext * const p= &s->plane[i];
00894 
00895         memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
00896         p->quant_table_index= avctx->context_model;
00897         p->context_count= s->context_count[p->quant_table_index];
00898     }
00899 
00900     if(allocate_initial_states(s) < 0)
00901         return AVERROR(ENOMEM);
00902 
00903     avctx->coded_frame= &s->picture;
00904     switch(avctx->pix_fmt){
00905     case PIX_FMT_YUV444P16:
00906     case PIX_FMT_YUV422P16:
00907     case PIX_FMT_YUV420P16:
00908         if(avctx->bits_per_raw_sample <=8){
00909             av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
00910             return -1;
00911         }
00912         if(!s->ac){
00913             av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
00914             return -1;
00915         }
00916         s->version= FFMAX(s->version, 1);
00917     case PIX_FMT_YUV444P:
00918     case PIX_FMT_YUV422P:
00919     case PIX_FMT_YUV420P:
00920     case PIX_FMT_YUV411P:
00921     case PIX_FMT_YUV410P:
00922         s->colorspace= 0;
00923         break;
00924     case PIX_FMT_RGB32:
00925         s->colorspace= 1;
00926         break;
00927     default:
00928         av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00929         return -1;
00930     }
00931     avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
00932 
00933     s->picture_number=0;
00934 
00935     if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
00936         for(i=0; i<s->quant_table_count; i++){
00937             s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
00938             if(!s->rc_stat2[i])
00939                 return AVERROR(ENOMEM);
00940         }
00941     }
00942     if(avctx->stats_in){
00943         char *p= avctx->stats_in;
00944         uint8_t best_state[256][256];
00945         int gob_count=0;
00946         char *next;
00947 
00948         av_assert0(s->version>=2);
00949 
00950         for(;;){
00951             for(j=0; j<256; j++){
00952                 for(i=0; i<2; i++){
00953                     s->rc_stat[j][i]= strtol(p, &next, 0);
00954                     if(next==p){
00955                         av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
00956                         return -1;
00957                     }
00958                     p=next;
00959                 }
00960             }
00961             for(i=0; i<s->quant_table_count; i++){
00962                 for(j=0; j<s->context_count[i]; j++){
00963                     for(k=0; k<32; k++){
00964                         for(m=0; m<2; m++){
00965                             s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
00966                             if(next==p){
00967                                 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
00968                                 return -1;
00969                             }
00970                             p=next;
00971                         }
00972                     }
00973                 }
00974             }
00975             gob_count= strtol(p, &next, 0);
00976             if(next==p || gob_count <0){
00977                 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
00978                 return -1;
00979             }
00980             p=next;
00981             while(*p=='\n' || *p==' ') p++;
00982             if(p[0]==0) break;
00983         }
00984         sort_stt(s, s->state_transition);
00985 
00986         find_best_state(best_state, s->state_transition);
00987 
00988         for(i=0; i<s->quant_table_count; i++){
00989             for(j=0; j<s->context_count[i]; j++){
00990                 for(k=0; k<32; k++){
00991                     double p= 128;
00992                     if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
00993                         p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
00994                     }
00995                     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)];
00996                 }
00997             }
00998         }
00999     }
01000 
01001     if(s->version>1){
01002         s->num_h_slices=2;
01003         s->num_v_slices=2;
01004         write_extra_header(s);
01005     }
01006 
01007     if(init_slice_contexts(s) < 0)
01008         return -1;
01009     if(init_slice_state(s) < 0)
01010         return -1;
01011 
01012 #define STATS_OUT_SIZE 1024*1024*6
01013     if(avctx->flags & CODEC_FLAG_PASS1){
01014         avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
01015         for(i=0; i<s->quant_table_count; i++){
01016             for(j=0; j<s->slice_count; j++){
01017                 FFV1Context *sf= s->slice_context[j];
01018                 av_assert0(!sf->rc_stat2[i]);
01019                 sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
01020                 if(!sf->rc_stat2[i])
01021                     return AVERROR(ENOMEM);
01022             }
01023         }
01024     }
01025 
01026     return 0;
01027 }
01028 #endif /* CONFIG_FFV1_ENCODER */
01029 
01030 
01031 static void clear_state(FFV1Context *f){
01032     int i, si, j;
01033 
01034     for(si=0; si<f->slice_count; si++){
01035         FFV1Context *fs= f->slice_context[si];
01036         for(i=0; i<f->plane_count; i++){
01037             PlaneContext *p= &fs->plane[i];
01038 
01039             p->interlace_bit_state[0]= 128;
01040             p->interlace_bit_state[1]= 128;
01041 
01042             if(fs->ac){
01043                 if(f->initial_states[p->quant_table_index]){
01044                     memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
01045                 }else
01046                 memset(p->state, 128, CONTEXT_SIZE*p->context_count);
01047             }else{
01048             for(j=0; j<p->context_count; j++){
01049                     p->vlc_state[j].drift= 0;
01050                     p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
01051                     p->vlc_state[j].bias= 0;
01052                     p->vlc_state[j].count= 1;
01053             }
01054             }
01055         }
01056     }
01057 }
01058 
01059 #if CONFIG_FFV1_ENCODER
01060 static int encode_slice(AVCodecContext *c, void *arg){
01061     FFV1Context *fs= *(void**)arg;
01062     FFV1Context *f= fs->avctx->priv_data;
01063     int width = fs->slice_width;
01064     int height= fs->slice_height;
01065     int x= fs->slice_x;
01066     int y= fs->slice_y;
01067     AVFrame * const p= &f->picture;
01068 
01069     if(f->colorspace==0){
01070         const int chroma_width = -((-width )>>f->chroma_h_shift);
01071         const int chroma_height= -((-height)>>f->chroma_v_shift);
01072         const int cx= x>>f->chroma_h_shift;
01073         const int cy= y>>f->chroma_v_shift;
01074 
01075         encode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
01076 
01077         encode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01078         encode_plane(fs, p->data[2] + cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
01079     }else{
01080         encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
01081     }
01082     emms_c();
01083 
01084     return 0;
01085 }
01086 
01087 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
01088     FFV1Context *f = avctx->priv_data;
01089     RangeCoder * const c= &f->slice_context[0]->c;
01090     AVFrame *pict = data;
01091     AVFrame * const p= &f->picture;
01092     int used_count= 0;
01093     uint8_t keystate=128;
01094     uint8_t *buf_p;
01095     int i;
01096 
01097     ff_init_range_encoder(c, buf, buf_size);
01098     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01099 
01100     *p = *pict;
01101     p->pict_type= AV_PICTURE_TYPE_I;
01102 
01103     if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
01104         put_rac(c, &keystate, 1);
01105         p->key_frame= 1;
01106         f->gob_count++;
01107         write_header(f);
01108         clear_state(f);
01109     }else{
01110         put_rac(c, &keystate, 0);
01111         p->key_frame= 0;
01112     }
01113 
01114     if(!f->ac){
01115         used_count += ff_rac_terminate(c);
01116 //printf("pos=%d\n", used_count);
01117         init_put_bits(&f->slice_context[0]->pb, buf + used_count, buf_size - used_count);
01118     }else if (f->ac>1){
01119         int i;
01120         for(i=1; i<256; i++){
01121             c->one_state[i]= f->state_transition[i];
01122             c->zero_state[256-i]= 256-c->one_state[i];
01123         }
01124     }
01125 
01126     for(i=1; i<f->slice_count; i++){
01127         FFV1Context *fs= f->slice_context[i];
01128         uint8_t *start= buf + (buf_size-used_count)*i/f->slice_count;
01129         int len= buf_size/f->slice_count;
01130 
01131         if(fs->ac){
01132             ff_init_range_encoder(&fs->c, start, len);
01133         }else{
01134             init_put_bits(&fs->pb, start, len);
01135         }
01136     }
01137     avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
01138 
01139     buf_p=buf;
01140     for(i=0; i<f->slice_count; i++){
01141         FFV1Context *fs= f->slice_context[i];
01142         int bytes;
01143 
01144         if(fs->ac){
01145             uint8_t state=128;
01146             put_rac(&fs->c, &state, 0);
01147             bytes= ff_rac_terminate(&fs->c);
01148         }else{
01149             flush_put_bits(&fs->pb); //nicer padding FIXME
01150             bytes= used_count + (put_bits_count(&fs->pb)+7)/8;
01151             used_count= 0;
01152         }
01153         if(i>0){
01154             av_assert0(bytes < buf_size/f->slice_count);
01155             memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
01156             av_assert0(bytes < (1<<24));
01157             AV_WB24(buf_p+bytes, bytes);
01158             bytes+=3;
01159         }
01160         buf_p += bytes;
01161     }
01162 
01163     if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
01164         int j, k, m;
01165         char *p= avctx->stats_out;
01166         char *end= p + STATS_OUT_SIZE;
01167 
01168         memset(f->rc_stat, 0, sizeof(f->rc_stat));
01169         for(i=0; i<f->quant_table_count; i++)
01170             memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
01171 
01172         for(j=0; j<f->slice_count; j++){
01173             FFV1Context *fs= f->slice_context[j];
01174             for(i=0; i<256; i++){
01175                 f->rc_stat[i][0] += fs->rc_stat[i][0];
01176                 f->rc_stat[i][1] += fs->rc_stat[i][1];
01177             }
01178             for(i=0; i<f->quant_table_count; i++){
01179                 for(k=0; k<f->context_count[i]; k++){
01180                     for(m=0; m<32; m++){
01181                         f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
01182                         f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
01183                     }
01184                 }
01185             }
01186         }
01187 
01188         for(j=0; j<256; j++){
01189             snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
01190             p+= strlen(p);
01191         }
01192         snprintf(p, end-p, "\n");
01193 
01194         for(i=0; i<f->quant_table_count; i++){
01195             for(j=0; j<f->context_count[i]; j++){
01196                 for(m=0; m<32; m++){
01197                     snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
01198                     p+= strlen(p);
01199                 }
01200             }
01201         }
01202         snprintf(p, end-p, "%d\n", f->gob_count);
01203     } else if(avctx->flags&CODEC_FLAG_PASS1)
01204         avctx->stats_out[0] = '\0';
01205 
01206     f->picture_number++;
01207     return buf_p-buf;
01208 }
01209 #endif /* CONFIG_FFV1_ENCODER */
01210 
01211 static av_cold int common_end(AVCodecContext *avctx){
01212     FFV1Context *s = avctx->priv_data;
01213     int i, j;
01214 
01215     if (avctx->codec->decode && s->picture.data[0])
01216         avctx->release_buffer(avctx, &s->picture);
01217 
01218     for(j=0; j<s->slice_count; j++){
01219         FFV1Context *fs= s->slice_context[j];
01220         for(i=0; i<s->plane_count; i++){
01221             PlaneContext *p= &fs->plane[i];
01222 
01223             av_freep(&p->state);
01224             av_freep(&p->vlc_state);
01225         }
01226         av_freep(&fs->sample_buffer);
01227     }
01228 
01229     av_freep(&avctx->stats_out);
01230     for(j=0; j<s->quant_table_count; j++){
01231         av_freep(&s->initial_states[j]);
01232         for(i=0; i<s->slice_count; i++){
01233             FFV1Context *sf= s->slice_context[i];
01234             av_freep(&sf->rc_stat2[j]);
01235         }
01236         av_freep(&s->rc_stat2[j]);
01237     }
01238 
01239     for(i=0; i<s->slice_count; i++){
01240         av_freep(&s->slice_context[i]);
01241     }
01242 
01243     return 0;
01244 }
01245 
01246 static av_always_inline void decode_line(FFV1Context *s, int w,
01247                                          int16_t *sample[2],
01248                                          int plane_index, int bits)
01249 {
01250     PlaneContext * const p= &s->plane[plane_index];
01251     RangeCoder * const c= &s->c;
01252     int x;
01253     int run_count=0;
01254     int run_mode=0;
01255     int run_index= s->run_index;
01256 
01257     for(x=0; x<w; x++){
01258         int diff, context, sign;
01259 
01260         context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
01261         if(context < 0){
01262             context= -context;
01263             sign=1;
01264         }else
01265             sign=0;
01266 
01267         av_assert2(context < p->context_count);
01268 
01269         if(s->ac){
01270             diff= get_symbol_inline(c, p->state[context], 1);
01271         }else{
01272             if(context == 0 && run_mode==0) run_mode=1;
01273 
01274             if(run_mode){
01275                 if(run_count==0 && run_mode==1){
01276                     if(get_bits1(&s->gb)){
01277                         run_count = 1<<ff_log2_run[run_index];
01278                         if(x + run_count <= w) run_index++;
01279                     }else{
01280                         if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
01281                         else run_count=0;
01282                         if(run_index) run_index--;
01283                         run_mode=2;
01284                     }
01285                 }
01286                 run_count--;
01287                 if(run_count < 0){
01288                     run_mode=0;
01289                     run_count=0;
01290                     diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01291                     if(diff>=0) diff++;
01292                 }else
01293                     diff=0;
01294             }else
01295                 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
01296 
01297 //            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));
01298         }
01299 
01300         if(sign) diff= -diff;
01301 
01302         sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
01303     }
01304     s->run_index= run_index;
01305 }
01306 
01307 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
01308     int x, y;
01309     int16_t *sample[2];
01310     sample[0]=s->sample_buffer    +3;
01311     sample[1]=s->sample_buffer+w+6+3;
01312 
01313     s->run_index=0;
01314 
01315     memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
01316 
01317     for(y=0; y<h; y++){
01318         int16_t *temp = sample[0]; //FIXME try a normal buffer
01319 
01320         sample[0]= sample[1];
01321         sample[1]= temp;
01322 
01323         sample[1][-1]= sample[0][0  ];
01324         sample[0][ w]= sample[0][w-1];
01325 
01326 //{START_TIMER
01327         if(s->avctx->bits_per_raw_sample <= 8){
01328             decode_line(s, w, sample, plane_index, 8);
01329             for(x=0; x<w; x++){
01330                 src[x + stride*y]= sample[1][x];
01331             }
01332         }else{
01333             decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
01334             for(x=0; x<w; x++){
01335                 ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
01336             }
01337         }
01338 //STOP_TIMER("decode-line")}
01339     }
01340 }
01341 
01342 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
01343     int x, y, p;
01344     int16_t *sample[3][2];
01345     for(x=0; x<3; x++){
01346         sample[x][0] = s->sample_buffer +  x*2   *(w+6) + 3;
01347         sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
01348     }
01349 
01350     s->run_index=0;
01351 
01352     memset(s->sample_buffer, 0, 6*(w+6)*sizeof(*s->sample_buffer));
01353 
01354     for(y=0; y<h; y++){
01355         for(p=0; p<3; p++){
01356             int16_t *temp = sample[p][0]; //FIXME try a normal buffer
01357 
01358             sample[p][0]= sample[p][1];
01359             sample[p][1]= temp;
01360 
01361             sample[p][1][-1]= sample[p][0][0  ];
01362             sample[p][0][ w]= sample[p][0][w-1];
01363             decode_line(s, w, sample[p], FFMIN(p, 1), 9);
01364         }
01365         for(x=0; x<w; x++){
01366             int g= sample[0][1][x];
01367             int b= sample[1][1][x];
01368             int r= sample[2][1][x];
01369 
01370 //            assert(g>=0 && b>=0 && r>=0);
01371 //            assert(g<256 && b<512 && r<512);
01372 
01373             b -= 0x100;
01374             r -= 0x100;
01375             g -= (b + r)>>2;
01376             b += g;
01377             r += g;
01378 
01379             src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
01380         }
01381     }
01382 }
01383 
01384 static int decode_slice(AVCodecContext *c, void *arg){
01385     FFV1Context *fs= *(void**)arg;
01386     FFV1Context *f= fs->avctx->priv_data;
01387     int width = fs->slice_width;
01388     int height= fs->slice_height;
01389     int x= fs->slice_x;
01390     int y= fs->slice_y;
01391     AVFrame * const p= &f->picture;
01392 
01393     av_assert1(width && height);
01394     if(f->colorspace==0){
01395         const int chroma_width = -((-width )>>f->chroma_h_shift);
01396         const int chroma_height= -((-height)>>f->chroma_v_shift);
01397         const int cx= x>>f->chroma_h_shift;
01398         const int cy= y>>f->chroma_v_shift;
01399         decode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
01400 
01401         decode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
01402         decode_plane(fs, p->data[2] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[2], 1);
01403     }else{
01404         decode_rgb_frame(fs, (uint32_t*)p->data[0] + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
01405     }
01406 
01407     emms_c();
01408 
01409     return 0;
01410 }
01411 
01412 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
01413     int v;
01414     int i=0;
01415     uint8_t state[CONTEXT_SIZE];
01416 
01417     memset(state, 128, sizeof(state));
01418 
01419     for(v=0; i<128 ; v++){
01420         int len= get_symbol(c, state, 0) + 1;
01421 
01422         if(len + i > 128) return -1;
01423 
01424         while(len--){
01425             quant_table[i] = scale*v;
01426             i++;
01427 //printf("%2d ",v);
01428 //if(i%16==0) printf("\n");
01429         }
01430     }
01431 
01432     for(i=1; i<128; i++){
01433         quant_table[256-i]= -quant_table[i];
01434     }
01435     quant_table[128]= -quant_table[127];
01436 
01437     return 2*v - 1;
01438 }
01439 
01440 static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
01441     int i;
01442     int context_count=1;
01443 
01444     for(i=0; i<5; i++){
01445         context_count*= read_quant_table(c, quant_table[i], context_count);
01446         if(context_count > 32768U){
01447             return -1;
01448         }
01449     }
01450     return (context_count+1)/2;
01451 }
01452 
01453 static int read_extra_header(FFV1Context *f){
01454     RangeCoder * const c= &f->c;
01455     uint8_t state[CONTEXT_SIZE];
01456     int i, j, k;
01457     uint8_t state2[32][CONTEXT_SIZE];
01458 
01459     memset(state2, 128, sizeof(state2));
01460     memset(state, 128, sizeof(state));
01461 
01462     ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
01463     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01464 
01465     f->version= get_symbol(c, state, 0);
01466     f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01467     if(f->ac>1){
01468         for(i=1; i<256; i++){
01469             f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01470         }
01471     }
01472     f->colorspace= get_symbol(c, state, 0); //YUV cs type
01473     f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01474     get_rac(c, state); //no chroma = false
01475     f->chroma_h_shift= get_symbol(c, state, 0);
01476     f->chroma_v_shift= get_symbol(c, state, 0);
01477     get_rac(c, state); //transparency plane
01478     f->plane_count= 2;
01479     f->num_h_slices= 1 + get_symbol(c, state, 0);
01480     f->num_v_slices= 1 + get_symbol(c, state, 0);
01481     if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
01482         av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
01483         return -1;
01484     }
01485 
01486     f->quant_table_count= get_symbol(c, state, 0);
01487     if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
01488         return -1;
01489     for(i=0; i<f->quant_table_count; i++){
01490         if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
01491             av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01492             return -1;
01493         }
01494     }
01495 
01496     if(allocate_initial_states(f) < 0)
01497         return AVERROR(ENOMEM);
01498 
01499     for(i=0; i<f->quant_table_count; i++){
01500         if(get_rac(c, state)){
01501             for(j=0; j<f->context_count[i]; j++){
01502                 for(k=0; k<CONTEXT_SIZE; k++){
01503                     int pred= j ? f->initial_states[i][j-1][k] : 128;
01504                     f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
01505                 }
01506             }
01507         }
01508     }
01509 
01510     return 0;
01511 }
01512 
01513 static int read_header(FFV1Context *f){
01514     uint8_t state[CONTEXT_SIZE];
01515     int i, j, context_count;
01516     RangeCoder * const c= &f->slice_context[0]->c;
01517 
01518     memset(state, 128, sizeof(state));
01519 
01520     if(f->version < 2){
01521         f->version= get_symbol(c, state, 0);
01522         f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
01523         if(f->ac>1){
01524             for(i=1; i<256; i++){
01525                 f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
01526             }
01527         }
01528         f->colorspace= get_symbol(c, state, 0); //YUV cs type
01529         if(f->version>0)
01530             f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
01531         get_rac(c, state); //no chroma = false
01532         f->chroma_h_shift= get_symbol(c, state, 0);
01533         f->chroma_v_shift= get_symbol(c, state, 0);
01534         get_rac(c, state); //transparency plane
01535         f->plane_count= 2;
01536     }
01537 
01538     if(f->colorspace==0){
01539         if(f->avctx->bits_per_raw_sample<=8){
01540             switch(16*f->chroma_h_shift + f->chroma_v_shift){
01541             case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
01542             case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
01543             case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
01544             case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
01545             case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
01546             default:
01547                 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01548                 return -1;
01549             }
01550         }else{
01551             switch(16*f->chroma_h_shift + f->chroma_v_shift){
01552             case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
01553             case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
01554             case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
01555             default:
01556                 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
01557                 return -1;
01558             }
01559         }
01560     }else if(f->colorspace==1){
01561         if(f->chroma_h_shift || f->chroma_v_shift){
01562             av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
01563             return -1;
01564         }
01565         f->avctx->pix_fmt= PIX_FMT_RGB32;
01566     }else{
01567         av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
01568         return -1;
01569     }
01570 
01571 //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
01572     if(f->version < 2){
01573         context_count= read_quant_tables(c, f->quant_table);
01574         if(context_count < 0){
01575                 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
01576                 return -1;
01577         }
01578     }else{
01579         f->slice_count= get_symbol(c, state, 0);
01580         if(f->slice_count > (unsigned)MAX_SLICES)
01581             return -1;
01582     }
01583 
01584     for(j=0; j<f->slice_count; j++){
01585         FFV1Context *fs= f->slice_context[j];
01586         fs->ac= f->ac;
01587 
01588         if(f->version >= 2){
01589             fs->slice_x     = get_symbol(c, state, 0)   *f->width ;
01590             fs->slice_y     = get_symbol(c, state, 0)   *f->height;
01591             fs->slice_width =(get_symbol(c, state, 0)+1)*f->width  + fs->slice_x;
01592             fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
01593 
01594             fs->slice_x /= f->num_h_slices;
01595             fs->slice_y /= f->num_v_slices;
01596             fs->slice_width  = fs->slice_width /f->num_h_slices - fs->slice_x;
01597             fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
01598             if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
01599                 return -1;
01600             if(    (unsigned)fs->slice_x + (uint64_t)fs->slice_width  > f->width
01601                 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
01602                 return -1;
01603         }
01604 
01605         for(i=0; i<f->plane_count; i++){
01606             PlaneContext * const p= &fs->plane[i];
01607 
01608             if(f->version >= 2){
01609                 int idx=get_symbol(c, state, 0);
01610                 if(idx > (unsigned)f->quant_table_count){
01611                     av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
01612                     return -1;
01613                 }
01614                 p->quant_table_index= idx;
01615                 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
01616                 context_count= f->context_count[idx];
01617             }else{
01618                 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
01619             }
01620 
01621             if(p->context_count < context_count){
01622                 av_freep(&p->state);
01623                 av_freep(&p->vlc_state);
01624             }
01625             p->context_count= context_count;
01626         }
01627     }
01628 
01629     return 0;
01630 }
01631 
01632 static av_cold int decode_init(AVCodecContext *avctx)
01633 {
01634     FFV1Context *f = avctx->priv_data;
01635 
01636     common_init(avctx);
01637 
01638     if(avctx->extradata && read_extra_header(f) < 0)
01639         return -1;
01640 
01641     if(init_slice_contexts(f) < 0)
01642         return -1;
01643 
01644     return 0;
01645 }
01646 
01647 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
01648     const uint8_t *buf = avpkt->data;
01649     int buf_size = avpkt->size;
01650     FFV1Context *f = avctx->priv_data;
01651     RangeCoder * const c= &f->slice_context[0]->c;
01652     AVFrame * const p= &f->picture;
01653     int bytes_read, i;
01654     uint8_t keystate= 128;
01655     const uint8_t *buf_p;
01656 
01657     AVFrame *picture = data;
01658 
01659     /* release previously stored data */
01660     if (p->data[0])
01661         avctx->release_buffer(avctx, p);
01662 
01663     ff_init_range_decoder(c, buf, buf_size);
01664     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01665 
01666 
01667     p->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
01668     if(get_rac(c, &keystate)){
01669         p->key_frame= 1;
01670         if(read_header(f) < 0)
01671             return -1;
01672         if(init_slice_state(f) < 0)
01673             return -1;
01674 
01675         clear_state(f);
01676     }else{
01677         p->key_frame= 0;
01678     }
01679     if(f->ac>1){
01680         int i;
01681         for(i=1; i<256; i++){
01682             c->one_state[i]= f->state_transition[i];
01683             c->zero_state[256-i]= 256-c->one_state[i];
01684         }
01685     }
01686 
01687     p->reference= 0;
01688     if(avctx->get_buffer(avctx, p) < 0){
01689         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01690         return -1;
01691     }
01692 
01693     if(avctx->debug&FF_DEBUG_PICT_INFO)
01694         av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
01695 
01696     if(!f->ac){
01697         bytes_read = c->bytestream - c->bytestream_start - 1;
01698         if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
01699 //printf("pos=%d\n", bytes_read);
01700         init_get_bits(&f->slice_context[0]->gb, buf + bytes_read, (buf_size - bytes_read) * 8);
01701     } else {
01702         bytes_read = 0; /* avoid warning */
01703     }
01704 
01705     buf_p= buf + buf_size;
01706     for(i=f->slice_count-1; i>0; i--){
01707         FFV1Context *fs= f->slice_context[i];
01708         int v= AV_RB24(buf_p-3)+3;
01709         if(buf_p - buf <= v){
01710             av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
01711             return -1;
01712         }
01713         buf_p -= v;
01714         if(fs->ac){
01715             ff_init_range_decoder(&fs->c, buf_p, v);
01716         }else{
01717             init_get_bits(&fs->gb, buf_p, v * 8);
01718         }
01719     }
01720 
01721     avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
01722     f->picture_number++;
01723 
01724     *picture= *p;
01725     *data_size = sizeof(AVFrame);
01726 
01727     return buf_size;
01728 }
01729 
01730 AVCodec ff_ffv1_decoder = {
01731     .name           = "ffv1",
01732     .type           = AVMEDIA_TYPE_VIDEO,
01733     .id             = CODEC_ID_FFV1,
01734     .priv_data_size = sizeof(FFV1Context),
01735     .init           = decode_init,
01736     .close          = common_end,
01737     .decode         = decode_frame,
01738     .capabilities   = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ | CODEC_CAP_SLICE_THREADS,
01739     .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
01740 };
01741 
01742 #if CONFIG_FFV1_ENCODER
01743 AVCodec ff_ffv1_encoder = {
01744     .name           = "ffv1",
01745     .type           = AVMEDIA_TYPE_VIDEO,
01746     .id             = CODEC_ID_FFV1,
01747     .priv_data_size = sizeof(FFV1Context),
01748     .init           = encode_init,
01749     .encode         = encode_frame,
01750     .close          = common_end,
01751     .capabilities = CODEC_CAP_SLICE_THREADS,
01752     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_NONE},
01753     .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
01754 };
01755 #endif