Revision 61ccfcc0

View differences:

libavcodec/cabac.c
127 127
int main(){
128 128
    CABACContext c;
129 129
    uint8_t b[9*SIZE];
130
    uint8_t bit[9*SIZE];
130
    uint8_t r[9*SIZE];
131 131
    int i;
132
    uint8_t state= 0;
132
    uint8_t state[10]= {0};
133 133
    
134 134
    ff_init_cabac_encoder(&c, b, SIZE);
135 135
    ff_init_cabac_states(&c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
136 136
    
137 137
    for(i=0; i<SIZE; i++){
138
        bit[i]= (random()%7)&1;
138
        r[i]= random()%7;
139 139
    }
140 140
    
141 141
    for(i=0; i<SIZE; i++){
142 142
START_TIMER
143
        put_cabac_bypass(&c, bit[i]);
143
        put_cabac_bypass(&c, r[i]&1);
144 144
STOP_TIMER("put_cabac_bypass")
145 145
    }
146 146
    
147 147
    for(i=0; i<SIZE; i++){
148 148
START_TIMER
149
        put_cabac(&c, &state, bit[i]);
149
        put_cabac(&c, state, r[i]&1);
150 150
STOP_TIMER("put_cabac")
151 151
    }
152 152

  
153
    for(i=0; i<SIZE; i++){
154
START_TIMER
155
        put_cabac_u(&c, state, r[i], 6, 3, i&1);
156
STOP_TIMER("put_cabac_u")
157
    }    
158

  
159
    for(i=0; i<SIZE; i++){
160
START_TIMER
161
        put_cabac_ueg(&c, state, r[i], 0, 3, 0, 1, 2);
162
STOP_TIMER("put_cabac_ueg")
163
    }    
164
   
153 165
    put_cabac_terminate(&c, 1);
154 166
    
155 167
    ff_init_cabac_decoder(&c, b, SIZE);
156 168
    
157
    state=0;
169
    memset(state, 0, sizeof(state));
158 170
    
159 171
    for(i=0; i<SIZE; i++){
160 172
START_TIMER
161
        if( bit[i] != get_cabac_bypass(&c) )
173
        if( (r[i]&1) != get_cabac_bypass(&c) )
162 174
            printf("CABAC bypass failure at %d\n", i);
163 175
STOP_TIMER("get_cabac_bypass")
164 176
    }
165 177
    
166 178
    for(i=0; i<SIZE; i++){
167 179
START_TIMER
168
        if( bit[i] != get_cabac(&c, &state) )
180
        if( (r[i]&1) != get_cabac(&c, state) )
169 181
            printf("CABAC failure at %d\n", i);
170 182
STOP_TIMER("get_cabac")
171 183
    }
172
    
184

  
185
    for(i=0; i<SIZE; i++){
186
START_TIMER
187
        if( r[i] != get_cabac_u(&c, state, (i&1) ? 6 : 7, 3, i&1) )
188
            printf("CABAC unary (truncated) binarization failure at %d\n", i);
189
STOP_TIMER("get_cabac_u")
190
    }
191

  
192
    for(i=0; i<SIZE; i++){
193
START_TIMER
194
        if( r[i] != get_cabac_ueg(&c, state, 3, 0, 1, 2))
195
            printf("CABAC unary (truncated) binarization failure at %d\n", i);
196
STOP_TIMER("get_cabac_ueg")
197
    }
198

  
173 199
    if(!get_cabac_terminate(&c))
174 200
        printf("where's the Terminator?\n");
175 201
    
libavcodec/cabac.h
113 113
#endif
114 114
}
115 115

  
116
/**
117
 * @param bit 0 -> write zero bit, !=0 write one bit
118
 */
116 119
static inline void put_cabac_bypass(CABACContext *c, int bit){
117 120
    c->low += c->low;
118 121

  
......
158 161
#endif
159 162
}
160 163

  
164
/**
165
 * put (truncated) unary binarization.
166
 */
167
static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
168
    int i;
169
    
170
    assert(v <= max);
171
    
172
#if 1
173
    for(i=0; i<v; i++){
174
        put_cabac(c, state, 1);
175
        if(i < max_index) state++;
176
    }
177
    if(truncated==0 || v<max)
178
        put_cabac(c, state, 0);
179
#else
180
    if(v <= max_index){
181
        for(i=0; i<v; i++){
182
            put_cabac(c, state+i, 1);
183
        }
184
        if(truncated==0 || v<max)
185
            put_cabac(c, state+i, 0);
186
    }else{
187
        for(i=0; i<=max_index; i++){
188
            put_cabac(c, state+i, 1);
189
        }
190
        for(; i<v; i++){
191
            put_cabac(c, state+max_index, 1);
192
        }
193
        if(truncated==0 || v<max)
194
            put_cabac(c, state+max_index, 0);
195
    }
196
#endif
197
}
198

  
199
/**
200
 * put unary exp golomb k-th order binarization.
201
 */
202
static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int sign, int max, int is_signed, int k, int max_index){
203
    int i;
204
    
205
    if(v==0)
206
        put_cabac(c, state, 0);
207
    else{
208
        if(v<max){
209
            for(i=0; i<v; i++){
210
                put_cabac(c, state, 1);
211
                if(i < max_index) state++;
212
            }
213

  
214
            put_cabac(c, state, 0);
215
        }else{
216
            int m= 1<<k;
217

  
218
            for(i=0; i<max; i++){
219
                put_cabac(c, state, 1);
220
                if(i < max_index) state++;
221
            }
222

  
223
            v -= max;
224
            while(v >= m){ //FIXME optimize
225
                put_cabac_bypass(c, 1);
226
                v-= m;
227
                m+= m;
228
            }
229
            put_cabac_bypass(c, 0);
230
            while(m>>=1){
231
                put_cabac_bypass(c, v&m);
232
            }
233
        }
234

  
235
        if(is_signed)
236
            put_cabac_bypass(c, sign);
237
    }
238
}
239

  
161 240
static inline void renorm_cabac_decoder(CABACContext *c){
162 241
    while(c->range < 0x10000){
163 242
        c->range+= c->range;
......
230 309
    }    
231 310
}
232 311

  
312
/**
313
 * get (truncated) unnary binarization.
314
 */
315
static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
316
    int i;
317
    
318
    for(i=0; i<max; i++){ 
319
        if(get_cabac(c, state)==0)
320
            return i;
321
            
322
        if(i< max_index) state++;
323
    }
324

  
325
    return truncated ? max : -1;
326
}
327

  
328
/**
329
 * get unary exp golomb k-th order binarization.
330
 */
331
static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
332
    int i, v;
333
    int m= 1<<k;
334
    
335
    if(get_cabac(c, state)==0) 
336
        return 0;
337
        
338
    if(0 < max_index) state++;
339
    
340
    for(i=1; i<max; i++){ 
341
        if(get_cabac(c, state)==0){
342
            if(is_signed && get_cabac_bypass(c)){
343
                return -i;
344
            }else
345
                return i;
346
        }
347

  
348
        if(i < max_index) state++;
349
    }
350
    
351
    while(get_cabac_bypass(c)){
352
        i+= m;
353
        m+= m;
354
    }
355
    
356
    v=0;
357
    while(m>>=1){
358
        v+= v + get_cabac_bypass(c);
359
    }
360
    i += v;
361

  
362
    if(is_signed && get_cabac_bypass(c)){
363
        return -i;
364
    }else
365
        return i;
366
}

Also available in: Unified diff