00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #undef NDEBUG
00028 #include <assert.h>
00029
00030 #define CABAC_BITS 8
00031 #define CABAC_MASK ((1<<CABAC_BITS)-1)
00032
00033 typedef struct CABACContext{
00034 int low;
00035 int range;
00036 int outstanding_count;
00037 #ifdef STRICT_LIMITS
00038 int symCount;
00039 #endif
00040 uint8_t lps_range[2*65][4];
00041 uint8_t lps_state[2*64];
00042 uint8_t mps_state[2*64];
00043 const uint8_t *bytestream_start;
00044 const uint8_t *bytestream;
00045 const uint8_t *bytestream_end;
00046 PutBitContext pb;
00047 }CABACContext;
00048
00049 extern const uint8_t ff_h264_lps_range[64][4];
00050 extern const uint8_t ff_h264_mps_state[64];
00051 extern const uint8_t ff_h264_lps_state[64];
00052 extern const uint8_t ff_h264_norm_shift[256];
00053
00054
00055 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
00056 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
00057 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
00058 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
00059
00060
00061 static inline void put_cabac_bit(CABACContext *c, int b){
00062 put_bits(&c->pb, 1, b);
00063 for(;c->outstanding_count; c->outstanding_count--){
00064 put_bits(&c->pb, 1, 1-b);
00065 }
00066 }
00067
00068 static inline void renorm_cabac_encoder(CABACContext *c){
00069 while(c->range < 0x100){
00070
00071 if(c->low<0x100){
00072 put_cabac_bit(c, 0);
00073 }else if(c->low<0x200){
00074 c->outstanding_count++;
00075 c->low -= 0x100;
00076 }else{
00077 put_cabac_bit(c, 1);
00078 c->low -= 0x200;
00079 }
00080
00081 c->range+= c->range;
00082 c->low += c->low;
00083 }
00084 }
00085
00086 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
00087 int RangeLPS= c->lps_range[*state][c->range>>6];
00088
00089 if(bit == ((*state)&1)){
00090 c->range -= RangeLPS;
00091 *state= c->mps_state[*state];
00092 }else{
00093 c->low += c->range - RangeLPS;
00094 c->range = RangeLPS;
00095 *state= c->lps_state[*state];
00096 }
00097
00098 renorm_cabac_encoder(c);
00099
00100 #ifdef STRICT_LIMITS
00101 c->symCount++;
00102 #endif
00103 }
00104
00105 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
00106 assert(c->range > RangeLPS);
00107
00108 if(!bit){
00109 c->range -= RangeLPS;
00110 }else{
00111 c->low += c->range - RangeLPS;
00112 c->range = RangeLPS;
00113 }
00114
00115 renorm_cabac_encoder(c);
00116
00117 #ifdef STRICT_LIMITS
00118 c->symCount++;
00119 #endif
00120 }
00121
00125 static inline void put_cabac_bypass(CABACContext *c, int bit){
00126 c->low += c->low;
00127
00128 if(bit){
00129 c->low += c->range;
00130 }
00131
00132 if(c->low<0x200){
00133 put_cabac_bit(c, 0);
00134 }else if(c->low<0x400){
00135 c->outstanding_count++;
00136 c->low -= 0x200;
00137 }else{
00138 put_cabac_bit(c, 1);
00139 c->low -= 0x400;
00140 }
00141
00142 #ifdef STRICT_LIMITS
00143 c->symCount++;
00144 #endif
00145 }
00146
00151 static inline int put_cabac_terminate(CABACContext *c, int bit){
00152 c->range -= 2;
00153
00154 if(!bit){
00155 renorm_cabac_encoder(c);
00156 }else{
00157 c->low += c->range;
00158 c->range= 2;
00159
00160 renorm_cabac_encoder(c);
00161
00162 assert(c->low <= 0x1FF);
00163 put_cabac_bit(c, c->low>>9);
00164 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
00165
00166 flush_put_bits(&c->pb);
00167 }
00168
00169 #ifdef STRICT_LIMITS
00170 c->symCount++;
00171 #endif
00172
00173 return (put_bits_count(&c->pb)+7)>>3;
00174 }
00175
00179 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
00180 int i;
00181
00182 assert(v <= max);
00183
00184 #if 1
00185 for(i=0; i<v; i++){
00186 put_cabac(c, state, 1);
00187 if(i < max_index) state++;
00188 }
00189 if(truncated==0 || v<max)
00190 put_cabac(c, state, 0);
00191 #else
00192 if(v <= max_index){
00193 for(i=0; i<v; i++){
00194 put_cabac(c, state+i, 1);
00195 }
00196 if(truncated==0 || v<max)
00197 put_cabac(c, state+i, 0);
00198 }else{
00199 for(i=0; i<=max_index; i++){
00200 put_cabac(c, state+i, 1);
00201 }
00202 for(; i<v; i++){
00203 put_cabac(c, state+max_index, 1);
00204 }
00205 if(truncated==0 || v<max)
00206 put_cabac(c, state+max_index, 0);
00207 }
00208 #endif
00209 }
00210
00214 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
00215 int i;
00216
00217 if(v==0)
00218 put_cabac(c, state, 0);
00219 else{
00220 const int sign= v < 0;
00221
00222 if(is_signed) v= ABS(v);
00223
00224 if(v<max){
00225 for(i=0; i<v; i++){
00226 put_cabac(c, state, 1);
00227 if(i < max_index) state++;
00228 }
00229
00230 put_cabac(c, state, 0);
00231 }else{
00232 int m= 1<<k;
00233
00234 for(i=0; i<max; i++){
00235 put_cabac(c, state, 1);
00236 if(i < max_index) state++;
00237 }
00238
00239 v -= max;
00240 while(v >= m){
00241 put_cabac_bypass(c, 1);
00242 v-= m;
00243 m+= m;
00244 }
00245 put_cabac_bypass(c, 0);
00246 while(m>>=1){
00247 put_cabac_bypass(c, v&m);
00248 }
00249 }
00250
00251 if(is_signed)
00252 put_cabac_bypass(c, sign);
00253 }
00254 }
00255
00256 static void refill(CABACContext *c){
00257 if(c->bytestream <= c->bytestream_end)
00258 #if CABAC_BITS == 16
00259 c->low+= ((c->bytestream[0]<<9) + (c->bytestream[1])<<1);
00260 #else
00261 c->low+= c->bytestream[0]<<1;
00262 #endif
00263 c->low -= CABAC_MASK;
00264 c->bytestream+= CABAC_BITS/8;
00265 }
00266
00267 #if 0
00268 static void refill2(CABACContext *c){
00269 int i, x;
00270
00271 x= c->low ^ (c->low-1);
00272 i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
00273
00274 x= -CABAC_MASK;
00275
00276 if(c->bytestream < c->bytestream_end)
00277 #if CABAC_BITS == 16
00278 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
00279 #else
00280 x+= c->bytestream[0]<<1;
00281 #endif
00282
00283 c->low += x<<i;
00284 c->bytestream+= CABAC_BITS/8;
00285 }
00286 #endif
00287
00288 static inline void renorm_cabac_decoder(CABACContext *c){
00289 while(c->range < (0x200 << CABAC_BITS)){
00290 c->range+= c->range;
00291 c->low+= c->low;
00292 if(!(c->low & CABAC_MASK))
00293 refill(c);
00294 }
00295 }
00296
00297 static inline void renorm_cabac_decoder_once(CABACContext *c){
00298 int mask= (c->range - (0x200 << CABAC_BITS))>>31;
00299 c->range+= c->range&mask;
00300 c->low += c->low &mask;
00301 if(!(c->low & CABAC_MASK))
00302 refill(c);
00303 }
00304
00305 static inline int get_cabac(CABACContext *c, uint8_t * const state){
00306 int RangeLPS= c->lps_range[*state][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
00307 int bit, lps_mask attribute_unused;
00308
00309 c->range -= RangeLPS;
00310 #if 1
00311 if(c->low < c->range){
00312 bit= (*state)&1;
00313 *state= c->mps_state[*state];
00314 renorm_cabac_decoder_once(c);
00315 }else{
00316
00317 bit= ((*state)&1)^1;
00318 c->low -= c->range;
00319 *state= c->lps_state[*state];
00320 c->range = RangeLPS;
00321 renorm_cabac_decoder(c);
00322
00323
00324
00325
00326
00327 }
00328 #else
00329 lps_mask= (c->range - c->low)>>31;
00330
00331 c->low -= c->range & lps_mask;
00332 c->range += (RangeLPS - c->range) & lps_mask;
00333
00334 bit= ((*state)^lps_mask)&1;
00335 *state= c->mps_state[(*state) - (128&lps_mask)];
00336
00337 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)];
00338 c->range<<= lps_mask;
00339 c->low <<= lps_mask;
00340 if(!(c->low & CABAC_MASK))
00341 refill2(c);
00342 #endif
00343
00344 return bit;
00345 }
00346
00347 static inline int get_cabac_bypass(CABACContext *c){
00348 c->low += c->low;
00349
00350 if(!(c->low & CABAC_MASK))
00351 refill(c);
00352
00353 if(c->low < c->range){
00354 return 0;
00355 }else{
00356 c->low -= c->range;
00357 return 1;
00358 }
00359 }
00360
00365 static inline int get_cabac_terminate(CABACContext *c){
00366 c->range -= 4<<CABAC_BITS;
00367 if(c->low < c->range){
00368 renorm_cabac_decoder_once(c);
00369 return 0;
00370 }else{
00371 return c->bytestream - c->bytestream_start;
00372 }
00373 }
00374
00378 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
00379 int i;
00380
00381 for(i=0; i<max; i++){
00382 if(get_cabac(c, state)==0)
00383 return i;
00384
00385 if(i< max_index) state++;
00386 }
00387
00388 return truncated ? max : -1;
00389 }
00390
00394 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
00395 int i, v;
00396 int m= 1<<k;
00397
00398 if(get_cabac(c, state)==0)
00399 return 0;
00400
00401 if(0 < max_index) state++;
00402
00403 for(i=1; i<max; i++){
00404 if(get_cabac(c, state)==0){
00405 if(is_signed && get_cabac_bypass(c)){
00406 return -i;
00407 }else
00408 return i;
00409 }
00410
00411 if(i < max_index) state++;
00412 }
00413
00414 while(get_cabac_bypass(c)){
00415 i+= m;
00416 m+= m;
00417 }
00418
00419 v=0;
00420 while(m>>=1){
00421 v+= v + get_cabac_bypass(c);
00422 }
00423 i += v;
00424
00425 if(is_signed && get_cabac_bypass(c)){
00426 return -i;
00427 }else
00428 return i;
00429 }