00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <string.h>
00028
00029 #include "libavutil/common.h"
00030 #include "get_bits.h"
00031 #include "cabac.h"
00032
00033 static const uint8_t lps_range[64][4]= {
00034 {128,176,208,240}, {128,167,197,227}, {128,158,187,216}, {123,150,178,205},
00035 {116,142,169,195}, {111,135,160,185}, {105,128,152,175}, {100,122,144,166},
00036 { 95,116,137,158}, { 90,110,130,150}, { 85,104,123,142}, { 81, 99,117,135},
00037 { 77, 94,111,128}, { 73, 89,105,122}, { 69, 85,100,116}, { 66, 80, 95,110},
00038 { 62, 76, 90,104}, { 59, 72, 86, 99}, { 56, 69, 81, 94}, { 53, 65, 77, 89},
00039 { 51, 62, 73, 85}, { 48, 59, 69, 80}, { 46, 56, 66, 76}, { 43, 53, 63, 72},
00040 { 41, 50, 59, 69}, { 39, 48, 56, 65}, { 37, 45, 54, 62}, { 35, 43, 51, 59},
00041 { 33, 41, 48, 56}, { 32, 39, 46, 53}, { 30, 37, 43, 50}, { 29, 35, 41, 48},
00042 { 27, 33, 39, 45}, { 26, 31, 37, 43}, { 24, 30, 35, 41}, { 23, 28, 33, 39},
00043 { 22, 27, 32, 37}, { 21, 26, 30, 35}, { 20, 24, 29, 33}, { 19, 23, 27, 31},
00044 { 18, 22, 26, 30}, { 17, 21, 25, 28}, { 16, 20, 23, 27}, { 15, 19, 22, 25},
00045 { 14, 18, 21, 24}, { 14, 17, 20, 23}, { 13, 16, 19, 22}, { 12, 15, 18, 21},
00046 { 12, 14, 17, 20}, { 11, 14, 16, 19}, { 11, 13, 15, 18}, { 10, 12, 15, 17},
00047 { 10, 12, 14, 16}, { 9, 11, 13, 15}, { 9, 11, 12, 14}, { 8, 10, 12, 14},
00048 { 8, 9, 11, 13}, { 7, 9, 11, 12}, { 7, 9, 10, 12}, { 7, 8, 10, 11},
00049 { 6, 8, 9, 11}, { 6, 7, 9, 10}, { 6, 7, 8, 9}, { 2, 2, 2, 2},
00050 };
00051
00052 uint8_t ff_h264_mlps_state[4*64];
00053 uint8_t ff_h264_lps_range[4*2*64];
00054 uint8_t ff_h264_lps_state[2*64];
00055 uint8_t ff_h264_mps_state[2*64];
00056
00057 static const uint8_t mps_state[64]= {
00058 1, 2, 3, 4, 5, 6, 7, 8,
00059 9,10,11,12,13,14,15,16,
00060 17,18,19,20,21,22,23,24,
00061 25,26,27,28,29,30,31,32,
00062 33,34,35,36,37,38,39,40,
00063 41,42,43,44,45,46,47,48,
00064 49,50,51,52,53,54,55,56,
00065 57,58,59,60,61,62,62,63,
00066 };
00067
00068 static const uint8_t lps_state[64]= {
00069 0, 0, 1, 2, 2, 4, 4, 5,
00070 6, 7, 8, 9, 9,11,11,12,
00071 13,13,15,15,16,16,18,18,
00072 19,19,21,21,22,22,23,24,
00073 24,25,26,26,27,27,28,29,
00074 29,30,30,30,31,32,32,33,
00075 33,33,34,34,35,35,35,36,
00076 36,36,37,37,37,38,38,63,
00077 };
00078
00079 const uint8_t ff_h264_norm_shift[512]= {
00080 9,8,7,7,6,6,6,6,5,5,5,5,5,5,5,5,
00081 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
00082 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
00083 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
00084 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00085 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00086 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00087 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00088 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00089 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00090 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00091 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00092 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00093 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00094 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00095 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00096 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00097 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00098 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00099 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00100 };
00101
00106 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){
00107 init_put_bits(&c->pb, buf, buf_size);
00108
00109 c->low= 0;
00110 c->range= 0x1FE;
00111 c->outstanding_count= 0;
00112 #ifdef STRICT_LIMITS
00113 c->sym_count =0;
00114 #endif
00115
00116 c->pb.bit_left++;
00117 }
00118
00123 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
00124 c->bytestream_start=
00125 c->bytestream= buf;
00126 c->bytestream_end= buf + buf_size;
00127
00128 #if CABAC_BITS == 16
00129 c->low = (*c->bytestream++)<<18;
00130 c->low+= (*c->bytestream++)<<10;
00131 #else
00132 c->low = (*c->bytestream++)<<10;
00133 #endif
00134 c->low+= ((*c->bytestream++)<<2) + 2;
00135 c->range= 0x1FE;
00136 }
00137
00138 void ff_init_cabac_states(CABACContext *c){
00139 int i, j;
00140
00141 for(i=0; i<64; i++){
00142 for(j=0; j<4; j++){
00143 ff_h264_lps_range[j*2*64+2*i+0]=
00144 ff_h264_lps_range[j*2*64+2*i+1]= lps_range[i][j];
00145 }
00146
00147 ff_h264_mlps_state[128+2*i+0]=
00148 ff_h264_mps_state[2*i+0]= 2*mps_state[i]+0;
00149 ff_h264_mlps_state[128+2*i+1]=
00150 ff_h264_mps_state[2*i+1]= 2*mps_state[i]+1;
00151
00152 if( i ){
00153 ff_h264_lps_state[2*i+0]=
00154 ff_h264_mlps_state[128-2*i-1]= 2*lps_state[i]+0;
00155 ff_h264_lps_state[2*i+1]=
00156 ff_h264_mlps_state[128-2*i-2]= 2*lps_state[i]+1;
00157 }else{
00158 ff_h264_lps_state[2*i+0]=
00159 ff_h264_mlps_state[128-2*i-1]= 1;
00160 ff_h264_lps_state[2*i+1]=
00161 ff_h264_mlps_state[128-2*i-2]= 0;
00162 }
00163 }
00164 }
00165
00166 #ifdef TEST
00167 #define SIZE 10240
00168
00169 #include "libavutil/lfg.h"
00170 #include "avcodec.h"
00171 #include "cabac.h"
00172
00173 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
00174 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
00175
00176 if(bit == ((*state)&1)){
00177 c->range -= RangeLPS;
00178 *state= ff_h264_mps_state[*state];
00179 }else{
00180 c->low += c->range - RangeLPS;
00181 c->range = RangeLPS;
00182 *state= ff_h264_lps_state[*state];
00183 }
00184
00185 renorm_cabac_encoder(c);
00186
00187 #ifdef STRICT_LIMITS
00188 c->symCount++;
00189 #endif
00190 }
00191
00195 static void put_cabac_bypass(CABACContext *c, int bit){
00196 c->low += c->low;
00197
00198 if(bit){
00199 c->low += c->range;
00200 }
00201
00202 if(c->low<0x200){
00203 put_cabac_bit(c, 0);
00204 }else if(c->low<0x400){
00205 c->outstanding_count++;
00206 c->low -= 0x200;
00207 }else{
00208 put_cabac_bit(c, 1);
00209 c->low -= 0x400;
00210 }
00211
00212 #ifdef STRICT_LIMITS
00213 c->symCount++;
00214 #endif
00215 }
00216
00221 static int put_cabac_terminate(CABACContext *c, int bit){
00222 c->range -= 2;
00223
00224 if(!bit){
00225 renorm_cabac_encoder(c);
00226 }else{
00227 c->low += c->range;
00228 c->range= 2;
00229
00230 renorm_cabac_encoder(c);
00231
00232 assert(c->low <= 0x1FF);
00233 put_cabac_bit(c, c->low>>9);
00234 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
00235
00236 flush_put_bits(&c->pb);
00237 }
00238
00239 #ifdef STRICT_LIMITS
00240 c->symCount++;
00241 #endif
00242
00243 return (put_bits_count(&c->pb)+7)>>3;
00244 }
00245
00249 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
00250 int i;
00251
00252 assert(v <= max);
00253
00254 for(i=0; i<v; i++){
00255 put_cabac(c, state, 1);
00256 if(i < max_index) state++;
00257 }
00258 if(truncated==0 || v<max)
00259 put_cabac(c, state, 0);
00260 }
00261
00265 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
00266 int i;
00267
00268 if(v==0)
00269 put_cabac(c, state, 0);
00270 else{
00271 const int sign= v < 0;
00272
00273 if(is_signed) v= FFABS(v);
00274
00275 if(v<max){
00276 for(i=0; i<v; i++){
00277 put_cabac(c, state, 1);
00278 if(i < max_index) state++;
00279 }
00280
00281 put_cabac(c, state, 0);
00282 }else{
00283 int m= 1<<k;
00284
00285 for(i=0; i<max; i++){
00286 put_cabac(c, state, 1);
00287 if(i < max_index) state++;
00288 }
00289
00290 v -= max;
00291 while(v >= m){
00292 put_cabac_bypass(c, 1);
00293 v-= m;
00294 m+= m;
00295 }
00296 put_cabac_bypass(c, 0);
00297 while(m>>=1){
00298 put_cabac_bypass(c, v&m);
00299 }
00300 }
00301
00302 if(is_signed)
00303 put_cabac_bypass(c, sign);
00304 }
00305 }
00306
00307 int main(void){
00308 CABACContext c;
00309 uint8_t b[9*SIZE];
00310 uint8_t r[9*SIZE];
00311 int i;
00312 uint8_t state[10]= {0};
00313 AVLFG prng;
00314
00315 av_lfg_init(&prng, 1);
00316 ff_init_cabac_encoder(&c, b, SIZE);
00317 ff_init_cabac_states(&c);
00318
00319 for(i=0; i<SIZE; i++){
00320 if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
00321 else r[i] = (i>>8)&1;
00322 }
00323
00324 for(i=0; i<SIZE; i++){
00325 START_TIMER
00326 put_cabac_bypass(&c, r[i]&1);
00327 STOP_TIMER("put_cabac_bypass")
00328 }
00329
00330 for(i=0; i<SIZE; i++){
00331 START_TIMER
00332 put_cabac(&c, state, r[i]&1);
00333 STOP_TIMER("put_cabac")
00334 }
00335
00336 #if 0
00337 for(i=0; i<SIZE; i++){
00338 START_TIMER
00339 put_cabac_u(&c, state, r[i], 6, 3, i&1);
00340 STOP_TIMER("put_cabac_u")
00341 }
00342
00343 for(i=0; i<SIZE; i++){
00344 START_TIMER
00345 put_cabac_ueg(&c, state, r[i], 3, 0, 1, 2);
00346 STOP_TIMER("put_cabac_ueg")
00347 }
00348 #endif
00349 put_cabac_terminate(&c, 1);
00350
00351 ff_init_cabac_decoder(&c, b, SIZE);
00352
00353 memset(state, 0, sizeof(state));
00354
00355 for(i=0; i<SIZE; i++){
00356 START_TIMER
00357 if( (r[i]&1) != get_cabac_bypass(&c) )
00358 av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
00359 STOP_TIMER("get_cabac_bypass")
00360 }
00361
00362 for(i=0; i<SIZE; i++){
00363 START_TIMER
00364 if( (r[i]&1) != get_cabac(&c, state) )
00365 av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
00366 STOP_TIMER("get_cabac")
00367 }
00368 #if 0
00369 for(i=0; i<SIZE; i++){
00370 START_TIMER
00371 if( r[i] != get_cabac_u(&c, state, (i&1) ? 6 : 7, 3, i&1) )
00372 av_log(NULL, AV_LOG_ERROR, "CABAC unary (truncated) binarization failure at %d\n", i);
00373 STOP_TIMER("get_cabac_u")
00374 }
00375
00376 for(i=0; i<SIZE; i++){
00377 START_TIMER
00378 if( r[i] != get_cabac_ueg(&c, state, 3, 0, 1, 2))
00379 av_log(NULL, AV_LOG_ERROR, "CABAC unary (truncated) binarization failure at %d\n", i);
00380 STOP_TIMER("get_cabac_ueg")
00381 }
00382 #endif
00383 if(!get_cabac_terminate(&c))
00384 av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
00385
00386 return 0;
00387 }
00388
00389 #endif