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