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 #include "cabac_functions.h"
00033
00034 static const uint8_t lps_range[64][4]= {
00035 {128,176,208,240}, {128,167,197,227}, {128,158,187,216}, {123,150,178,205},
00036 {116,142,169,195}, {111,135,160,185}, {105,128,152,175}, {100,122,144,166},
00037 { 95,116,137,158}, { 90,110,130,150}, { 85,104,123,142}, { 81, 99,117,135},
00038 { 77, 94,111,128}, { 73, 89,105,122}, { 69, 85,100,116}, { 66, 80, 95,110},
00039 { 62, 76, 90,104}, { 59, 72, 86, 99}, { 56, 69, 81, 94}, { 53, 65, 77, 89},
00040 { 51, 62, 73, 85}, { 48, 59, 69, 80}, { 46, 56, 66, 76}, { 43, 53, 63, 72},
00041 { 41, 50, 59, 69}, { 39, 48, 56, 65}, { 37, 45, 54, 62}, { 35, 43, 51, 59},
00042 { 33, 41, 48, 56}, { 32, 39, 46, 53}, { 30, 37, 43, 50}, { 29, 35, 41, 48},
00043 { 27, 33, 39, 45}, { 26, 31, 37, 43}, { 24, 30, 35, 41}, { 23, 28, 33, 39},
00044 { 22, 27, 32, 37}, { 21, 26, 30, 35}, { 20, 24, 29, 33}, { 19, 23, 27, 31},
00045 { 18, 22, 26, 30}, { 17, 21, 25, 28}, { 16, 20, 23, 27}, { 15, 19, 22, 25},
00046 { 14, 18, 21, 24}, { 14, 17, 20, 23}, { 13, 16, 19, 22}, { 12, 15, 18, 21},
00047 { 12, 14, 17, 20}, { 11, 14, 16, 19}, { 11, 13, 15, 18}, { 10, 12, 15, 17},
00048 { 10, 12, 14, 16}, { 9, 11, 13, 15}, { 9, 11, 12, 14}, { 8, 10, 12, 14},
00049 { 8, 9, 11, 13}, { 7, 9, 11, 12}, { 7, 9, 10, 12}, { 7, 8, 10, 11},
00050 { 6, 8, 9, 11}, { 6, 7, 9, 10}, { 6, 7, 8, 9}, { 2, 2, 2, 2},
00051 };
00052
00053 uint8_t ff_h264_mlps_state[4*64];
00054 uint8_t ff_h264_lps_range[4*2*64];
00055 uint8_t ff_h264_lps_state[2*64];
00056 static uint8_t h264_mps_state[2 * 64];
00057
00058 static const uint8_t mps_state[64]= {
00059 1, 2, 3, 4, 5, 6, 7, 8,
00060 9,10,11,12,13,14,15,16,
00061 17,18,19,20,21,22,23,24,
00062 25,26,27,28,29,30,31,32,
00063 33,34,35,36,37,38,39,40,
00064 41,42,43,44,45,46,47,48,
00065 49,50,51,52,53,54,55,56,
00066 57,58,59,60,61,62,62,63,
00067 };
00068
00069 static const uint8_t lps_state[64]= {
00070 0, 0, 1, 2, 2, 4, 4, 5,
00071 6, 7, 8, 9, 9,11,11,12,
00072 13,13,15,15,16,16,18,18,
00073 19,19,21,21,22,22,23,24,
00074 24,25,26,26,27,27,28,29,
00075 29,30,30,30,31,32,32,33,
00076 33,33,34,34,35,35,35,36,
00077 36,36,37,37,37,38,38,63,
00078 };
00079
00080 const uint8_t ff_h264_norm_shift[512]= {
00081 9,8,7,7,6,6,6,6,5,5,5,5,5,5,5,5,
00082 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
00083 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
00084 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
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 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
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 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,
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 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,
00101 };
00102
00107 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){
00108 init_put_bits(&c->pb, buf, buf_size);
00109
00110 c->low= 0;
00111 c->range= 0x1FE;
00112 c->outstanding_count= 0;
00113 c->pb.bit_left++;
00114 }
00115
00120 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
00121 c->bytestream_start=
00122 c->bytestream= buf;
00123 c->bytestream_end= buf + buf_size;
00124
00125 #if CABAC_BITS == 16
00126 c->low = (*c->bytestream++)<<18;
00127 c->low+= (*c->bytestream++)<<10;
00128 #else
00129 c->low = (*c->bytestream++)<<10;
00130 #endif
00131 c->low+= ((*c->bytestream++)<<2) + 2;
00132 c->range= 0x1FE;
00133 }
00134
00135 void ff_init_cabac_states(CABACContext *c){
00136 int i, j;
00137
00138 for(i=0; i<64; i++){
00139 for(j=0; j<4; j++){
00140 ff_h264_lps_range[j*2*64+2*i+0]=
00141 ff_h264_lps_range[j*2*64+2*i+1]= lps_range[i][j];
00142 }
00143
00144 ff_h264_mlps_state[128+2*i+0]=
00145 h264_mps_state[2 * i + 0] = 2 * mps_state[i] + 0;
00146 ff_h264_mlps_state[128+2*i+1]=
00147 h264_mps_state[2 * i + 1] = 2 * mps_state[i] + 1;
00148
00149 if( i ){
00150 ff_h264_lps_state[2*i+0]=
00151 ff_h264_mlps_state[128-2*i-1]= 2*lps_state[i]+0;
00152 ff_h264_lps_state[2*i+1]=
00153 ff_h264_mlps_state[128-2*i-2]= 2*lps_state[i]+1;
00154 }else{
00155 ff_h264_lps_state[2*i+0]=
00156 ff_h264_mlps_state[128-2*i-1]= 1;
00157 ff_h264_lps_state[2*i+1]=
00158 ff_h264_mlps_state[128-2*i-2]= 0;
00159 }
00160 }
00161 }
00162
00163 #ifdef TEST
00164 #define SIZE 10240
00165
00166 #include "libavutil/lfg.h"
00167 #include "avcodec.h"
00168 #include "cabac.h"
00169
00170 static inline void put_cabac_bit(CABACContext *c, int b){
00171 put_bits(&c->pb, 1, b);
00172 for(;c->outstanding_count; c->outstanding_count--){
00173 put_bits(&c->pb, 1, 1-b);
00174 }
00175 }
00176
00177 static inline void renorm_cabac_encoder(CABACContext *c){
00178 while(c->range < 0x100){
00179
00180 if(c->low<0x100){
00181 put_cabac_bit(c, 0);
00182 }else if(c->low<0x200){
00183 c->outstanding_count++;
00184 c->low -= 0x100;
00185 }else{
00186 put_cabac_bit(c, 1);
00187 c->low -= 0x200;
00188 }
00189
00190 c->range+= c->range;
00191 c->low += c->low;
00192 }
00193 }
00194
00195 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
00196 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
00197
00198 if(bit == ((*state)&1)){
00199 c->range -= RangeLPS;
00200 *state = h264_mps_state[*state];
00201 }else{
00202 c->low += c->range - RangeLPS;
00203 c->range = RangeLPS;
00204 *state= ff_h264_lps_state[*state];
00205 }
00206
00207 renorm_cabac_encoder(c);
00208 }
00209
00213 static void put_cabac_bypass(CABACContext *c, int bit){
00214 c->low += c->low;
00215
00216 if(bit){
00217 c->low += c->range;
00218 }
00219
00220 if(c->low<0x200){
00221 put_cabac_bit(c, 0);
00222 }else if(c->low<0x400){
00223 c->outstanding_count++;
00224 c->low -= 0x200;
00225 }else{
00226 put_cabac_bit(c, 1);
00227 c->low -= 0x400;
00228 }
00229 }
00230
00235 static int put_cabac_terminate(CABACContext *c, int bit){
00236 c->range -= 2;
00237
00238 if(!bit){
00239 renorm_cabac_encoder(c);
00240 }else{
00241 c->low += c->range;
00242 c->range= 2;
00243
00244 renorm_cabac_encoder(c);
00245
00246 assert(c->low <= 0x1FF);
00247 put_cabac_bit(c, c->low>>9);
00248 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
00249
00250 flush_put_bits(&c->pb);
00251 }
00252
00253 return (put_bits_count(&c->pb)+7)>>3;
00254 }
00255
00256 int main(void){
00257 CABACContext c;
00258 uint8_t b[9*SIZE];
00259 uint8_t r[9*SIZE];
00260 int i;
00261 uint8_t state[10]= {0};
00262 AVLFG prng;
00263
00264 av_lfg_init(&prng, 1);
00265 ff_init_cabac_encoder(&c, b, SIZE);
00266 ff_init_cabac_states(&c);
00267
00268 for(i=0; i<SIZE; i++){
00269 if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
00270 else r[i] = (i>>8)&1;
00271 }
00272
00273 for(i=0; i<SIZE; i++){
00274 START_TIMER
00275 put_cabac_bypass(&c, r[i]&1);
00276 STOP_TIMER("put_cabac_bypass")
00277 }
00278
00279 for(i=0; i<SIZE; i++){
00280 START_TIMER
00281 put_cabac(&c, state, r[i]&1);
00282 STOP_TIMER("put_cabac")
00283 }
00284
00285 put_cabac_terminate(&c, 1);
00286
00287 ff_init_cabac_decoder(&c, b, SIZE);
00288
00289 memset(state, 0, sizeof(state));
00290
00291 for(i=0; i<SIZE; i++){
00292 START_TIMER
00293 if( (r[i]&1) != get_cabac_bypass(&c) )
00294 av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
00295 STOP_TIMER("get_cabac_bypass")
00296 }
00297
00298 for(i=0; i<SIZE; i++){
00299 START_TIMER
00300 if( (r[i]&1) != get_cabac(&c, state) )
00301 av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
00302 STOP_TIMER("get_cabac")
00303 }
00304 if(!get_cabac_terminate(&c))
00305 av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
00306
00307 return 0;
00308 }
00309
00310 #endif