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 static void refill(CABACContext *c){
00094 #if CABAC_BITS == 16
00095 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
00096 #else
00097 c->low+= c->bytestream[0]<<1;
00098 #endif
00099 c->low -= CABAC_MASK;
00100 c->bytestream+= CABAC_BITS/8;
00101 }
00102
00103 static inline void renorm_cabac_decoder(CABACContext *c){
00104 while(c->range < 0x100){
00105 c->range+= c->range;
00106 c->low+= c->low;
00107 if(!(c->low & CABAC_MASK))
00108 refill(c);
00109 }
00110 }
00111
00112 static inline void renorm_cabac_decoder_once(CABACContext *c){
00113 int shift= (uint32_t)(c->range - 0x100)>>31;
00114 c->range<<= shift;
00115 c->low <<= shift;
00116 if(!(c->low & CABAC_MASK))
00117 refill(c);
00118 }
00119
00120 #ifndef get_cabac_inline
00121 static void refill2(CABACContext *c){
00122 int i, x;
00123
00124 x= c->low ^ (c->low-1);
00125 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
00126
00127 x= -CABAC_MASK;
00128
00129 #if CABAC_BITS == 16
00130 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
00131 #else
00132 x+= c->bytestream[0]<<1;
00133 #endif
00134
00135 c->low += x<<i;
00136 c->bytestream+= CABAC_BITS/8;
00137 }
00138
00139 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
00140 int s = *state;
00141 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
00142 int bit, lps_mask;
00143
00144 c->range -= RangeLPS;
00145 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
00146
00147 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
00148 c->range += (RangeLPS - c->range) & lps_mask;
00149
00150 s^=lps_mask;
00151 *state= (ff_h264_mlps_state+128)[s];
00152 bit= s&1;
00153
00154 lps_mask= ff_h264_norm_shift[c->range];
00155 c->range<<= lps_mask;
00156 c->low <<= lps_mask;
00157 if(!(c->low & CABAC_MASK))
00158 refill2(c);
00159 return bit;
00160 }
00161 #endif
00162
00163 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
00164 return get_cabac_inline(c,state);
00165 }
00166
00167 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
00168 return get_cabac_inline(c,state);
00169 }
00170
00171 static int av_unused get_cabac_bypass(CABACContext *c){
00172 int range;
00173 c->low += c->low;
00174
00175 if(!(c->low & CABAC_MASK))
00176 refill(c);
00177
00178 range= c->range<<(CABAC_BITS+1);
00179 if(c->low < range){
00180 return 0;
00181 }else{
00182 c->low -= range;
00183 return 1;
00184 }
00185 }
00186
00187
00188 #ifndef get_cabac_bypass_sign
00189 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
00190 int range, mask;
00191 c->low += c->low;
00192
00193 if(!(c->low & CABAC_MASK))
00194 refill(c);
00195
00196 range= c->range<<(CABAC_BITS+1);
00197 c->low -= range;
00198 mask= c->low >> 31;
00199 range &= mask;
00200 c->low += range;
00201 return (val^mask)-mask;
00202 }
00203 #endif
00204
00209 static int av_unused get_cabac_terminate(CABACContext *c){
00210 c->range -= 2;
00211 if(c->low < c->range<<(CABAC_BITS+1)){
00212 renorm_cabac_decoder_once(c);
00213 return 0;
00214 }else{
00215 return c->bytestream - c->bytestream_start;
00216 }
00217 }
00218
00219 #if 0
00220
00223 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
00224 int i;
00225
00226 for(i=0; i<max; i++){
00227 if(get_cabac(c, state)==0)
00228 return i;
00229
00230 if(i< max_index) state++;
00231 }
00232
00233 return truncated ? max : -1;
00234 }
00235
00239 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
00240 int i, v;
00241 int m= 1<<k;
00242
00243 if(get_cabac(c, state)==0)
00244 return 0;
00245
00246 if(0 < max_index) state++;
00247
00248 for(i=1; i<max; i++){
00249 if(get_cabac(c, state)==0){
00250 if(is_signed && get_cabac_bypass(c)){
00251 return -i;
00252 }else
00253 return i;
00254 }
00255
00256 if(i < max_index) state++;
00257 }
00258
00259 while(get_cabac_bypass(c)){
00260 i+= m;
00261 m+= m;
00262 }
00263
00264 v=0;
00265 while(m>>=1){
00266 v+= v + get_cabac_bypass(c);
00267 }
00268 i += v;
00269
00270 if(is_signed && get_cabac_bypass(c)){
00271 return -i;
00272 }else
00273 return i;
00274 }
00275 #endif
00276
00277 #endif