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