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 #if 0
00079 const uint8_t ff_h264_norm_shift_old[128]= {
00080 7,6,5,5,4,4,4,4,3,3,3,3,3,3,3,3,
00081 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00082 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00083 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00084 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00085 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00086 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00087 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00088 };
00089 #endif
00090 const uint8_t ff_h264_norm_shift[512]= {
00091 9,8,7,7,6,6,6,6,5,5,5,5,5,5,5,5,
00092 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
00093 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
00094 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
00095 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00096 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00097 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00098 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00099 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,
00100 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,
00101 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,
00102 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,
00103 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,
00104 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,
00105 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,
00106 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,
00107 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,
00108 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,
00109 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,
00110 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,
00111 };
00112
00117 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){
00118 init_put_bits(&c->pb, buf, buf_size);
00119
00120 c->low= 0;
00121 c->range= 0x1FE;
00122 c->outstanding_count= 0;
00123 #ifdef STRICT_LIMITS
00124 c->sym_count =0;
00125 #endif
00126
00127 c->pb.bit_left++;
00128 }
00129
00134 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
00135 c->bytestream_start=
00136 c->bytestream= buf;
00137 c->bytestream_end= buf + buf_size;
00138
00139 #if CABAC_BITS == 16
00140 c->low = (*c->bytestream++)<<18;
00141 c->low+= (*c->bytestream++)<<10;
00142 #else
00143 c->low = (*c->bytestream++)<<10;
00144 #endif
00145 c->low+= ((*c->bytestream++)<<2) + 2;
00146 c->range= 0x1FE;
00147 }
00148
00149 void ff_init_cabac_states(CABACContext *c){
00150 int i, j;
00151
00152 for(i=0; i<64; i++){
00153 for(j=0; j<4; j++){
00154 ff_h264_lps_range[j*2*64+2*i+0]=
00155 ff_h264_lps_range[j*2*64+2*i+1]= lps_range[i][j];
00156 }
00157
00158 ff_h264_mlps_state[128+2*i+0]=
00159 ff_h264_mps_state[2*i+0]= 2*mps_state[i]+0;
00160 ff_h264_mlps_state[128+2*i+1]=
00161 ff_h264_mps_state[2*i+1]= 2*mps_state[i]+1;
00162
00163 if( i ){
00164 ff_h264_lps_state[2*i+0]=
00165 ff_h264_mlps_state[128-2*i-1]= 2*lps_state[i]+0;
00166 ff_h264_lps_state[2*i+1]=
00167 ff_h264_mlps_state[128-2*i-2]= 2*lps_state[i]+1;
00168 }else{
00169 ff_h264_lps_state[2*i+0]=
00170 ff_h264_mlps_state[128-2*i-1]= 1;
00171 ff_h264_lps_state[2*i+1]=
00172 ff_h264_mlps_state[128-2*i-2]= 0;
00173 }
00174 }
00175 }
00176
00177 #ifdef TEST
00178 #define SIZE 10240
00179
00180 #include "libavutil/lfg.h"
00181 #include "avcodec.h"
00182 #include "cabac.h"
00183
00184 int main(void){
00185 CABACContext c;
00186 uint8_t b[9*SIZE];
00187 uint8_t r[9*SIZE];
00188 int i;
00189 uint8_t state[10]= {0};
00190 AVLFG prng;
00191
00192 av_lfg_init(&prng, 1);
00193 ff_init_cabac_encoder(&c, b, SIZE);
00194 ff_init_cabac_states(&c);
00195
00196 for(i=0; i<SIZE; i++){
00197 if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
00198 else r[i] = (i>>8)&1;
00199 }
00200
00201 for(i=0; i<SIZE; i++){
00202 START_TIMER
00203 put_cabac_bypass(&c, r[i]&1);
00204 STOP_TIMER("put_cabac_bypass")
00205 }
00206
00207 for(i=0; i<SIZE; i++){
00208 START_TIMER
00209 put_cabac(&c, state, r[i]&1);
00210 STOP_TIMER("put_cabac")
00211 }
00212
00213 #if 0
00214 for(i=0; i<SIZE; i++){
00215 START_TIMER
00216 put_cabac_u(&c, state, r[i], 6, 3, i&1);
00217 STOP_TIMER("put_cabac_u")
00218 }
00219
00220 for(i=0; i<SIZE; i++){
00221 START_TIMER
00222 put_cabac_ueg(&c, state, r[i], 3, 0, 1, 2);
00223 STOP_TIMER("put_cabac_ueg")
00224 }
00225 #endif
00226 put_cabac_terminate(&c, 1);
00227
00228 ff_init_cabac_decoder(&c, b, SIZE);
00229
00230 memset(state, 0, sizeof(state));
00231
00232 for(i=0; i<SIZE; i++){
00233 START_TIMER
00234 if( (r[i]&1) != get_cabac_bypass(&c) )
00235 av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
00236 STOP_TIMER("get_cabac_bypass")
00237 }
00238
00239 for(i=0; i<SIZE; i++){
00240 START_TIMER
00241 if( (r[i]&1) != get_cabac(&c, state) )
00242 av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
00243 STOP_TIMER("get_cabac")
00244 }
00245 #if 0
00246 for(i=0; i<SIZE; i++){
00247 START_TIMER
00248 if( r[i] != get_cabac_u(&c, state, (i&1) ? 6 : 7, 3, i&1) )
00249 av_log(NULL, AV_LOG_ERROR, "CABAC unary (truncated) binarization failure at %d\n", i);
00250 STOP_TIMER("get_cabac_u")
00251 }
00252
00253 for(i=0; i<SIZE; i++){
00254 START_TIMER
00255 if( r[i] != get_cabac_ueg(&c, state, 3, 0, 1, 2))
00256 av_log(NULL, AV_LOG_ERROR, "CABAC unary (truncated) binarization failure at %d\n", i);
00257 STOP_TIMER("get_cabac_ueg")
00258 }
00259 #endif
00260 if(!get_cabac_terminate(&c))
00261 av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
00262
00263 return 0;
00264 }
00265
00266 #endif