Statistics
| Branch: | Revision:

ffmpeg / libavcodec / cabac.h @ 5509bffa

History | View | Annotate | Download (9.86 KB)

1 d592f67f Michael Niedermayer
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 d592f67f Michael Niedermayer
 *
19
 */
20 115329f1 Diego Biurrun
21 d592f67f Michael Niedermayer
/**
22
 * @file cabac.h
23
 * Context Adaptive Binary Arithmetic Coder.
24
 */
25
26
27
#undef NDEBUG
28
#include <assert.h>
29
30 ec7eb896 Michael Niedermayer
#define CABAC_BITS 8
31
#define CABAC_MASK ((1<<CABAC_BITS)-1)
32
33 d592f67f Michael Niedermayer
typedef struct CABACContext{
34
    int low;
35
    int range;
36
    int outstanding_count;
37
#ifdef STRICT_LIMITS
38
    int symCount;
39
#endif
40 ec7eb896 Michael Niedermayer
    uint8_t lps_range[2*65][4];   ///< rangeTabLPS
41 d592f67f Michael Niedermayer
    uint8_t lps_state[2*64];      ///< transIdxLPS
42
    uint8_t mps_state[2*64];      ///< transIdxMPS
43 e96682e6 Michael Niedermayer
    const uint8_t *bytestream_start;
44
    const uint8_t *bytestream;
45 bba83349 Michael Niedermayer
    const uint8_t *bytestream_end;
46 d592f67f Michael Niedermayer
    PutBitContext pb;
47
}CABACContext;
48
49 7408ad05 Michael Niedermayer
extern const uint8_t ff_h264_lps_range[64][4];
50
extern const uint8_t ff_h264_mps_state[64];
51
extern const uint8_t ff_h264_lps_state[64];
52 ec7eb896 Michael Niedermayer
extern const uint8_t ff_h264_norm_shift[256];
53
54 d592f67f Michael Niedermayer
55
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
56 e96682e6 Michael Niedermayer
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
57 115329f1 Diego Biurrun
void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
58 d592f67f Michael Niedermayer
                          uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
59
60
61
static inline void put_cabac_bit(CABACContext *c, int b){
62 115329f1 Diego Biurrun
    put_bits(&c->pb, 1, b);
63
    for(;c->outstanding_count; c->outstanding_count--){
64 d592f67f Michael Niedermayer
        put_bits(&c->pb, 1, 1-b);
65
    }
66
}
67
68
static inline void renorm_cabac_encoder(CABACContext *c){
69
    while(c->range < 0x100){
70
        //FIXME optimize
71
        if(c->low<0x100){
72
            put_cabac_bit(c, 0);
73
        }else if(c->low<0x200){
74
            c->outstanding_count++;
75
            c->low -= 0x100;
76
        }else{
77
            put_cabac_bit(c, 1);
78
            c->low -= 0x200;
79
        }
80 115329f1 Diego Biurrun
81 d592f67f Michael Niedermayer
        c->range+= c->range;
82
        c->low += c->low;
83
    }
84
}
85
86
static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
87 ec7eb896 Michael Niedermayer
    int RangeLPS= c->lps_range[*state][c->range>>6];
88 115329f1 Diego Biurrun
89 d592f67f Michael Niedermayer
    if(bit == ((*state)&1)){
90
        c->range -= RangeLPS;
91
        *state= c->mps_state[*state];
92
    }else{
93
        c->low += c->range - RangeLPS;
94
        c->range = RangeLPS;
95
        *state= c->lps_state[*state];
96
    }
97 115329f1 Diego Biurrun
98 d592f67f Michael Niedermayer
    renorm_cabac_encoder(c);
99
100
#ifdef STRICT_LIMITS
101
    c->symCount++;
102
#endif
103
}
104
105
static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
106
    assert(c->range > RangeLPS);
107
108
    if(!bit){
109
        c->range -= RangeLPS;
110
    }else{
111
        c->low += c->range - RangeLPS;
112
        c->range = RangeLPS;
113
    }
114
115
    renorm_cabac_encoder(c);
116
117
#ifdef STRICT_LIMITS
118
    c->symCount++;
119
#endif
120
}
121
122 61ccfcc0 Michael Niedermayer
/**
123
 * @param bit 0 -> write zero bit, !=0 write one bit
124
 */
125 d592f67f Michael Niedermayer
static inline void put_cabac_bypass(CABACContext *c, int bit){
126
    c->low += c->low;
127
128
    if(bit){
129
        c->low += c->range;
130
    }
131
//FIXME optimize
132
    if(c->low<0x200){
133
        put_cabac_bit(c, 0);
134
    }else if(c->low<0x400){
135
        c->outstanding_count++;
136
        c->low -= 0x200;
137
    }else{
138
        put_cabac_bit(c, 1);
139
        c->low -= 0x400;
140
    }
141 115329f1 Diego Biurrun
142 d592f67f Michael Niedermayer
#ifdef STRICT_LIMITS
143
    c->symCount++;
144
#endif
145
}
146
147 5e20f836 Michael Niedermayer
/**
148
 *
149
 * @return the number of bytes written
150
 */
151
static inline int put_cabac_terminate(CABACContext *c, int bit){
152 d592f67f Michael Niedermayer
    c->range -= 2;
153
154
    if(!bit){
155
        renorm_cabac_encoder(c);
156
    }else{
157
        c->low += c->range;
158
        c->range= 2;
159 115329f1 Diego Biurrun
160 d592f67f Michael Niedermayer
        renorm_cabac_encoder(c);
161
162
        assert(c->low <= 0x1FF);
163
        put_cabac_bit(c, c->low>>9);
164
        put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
165 115329f1 Diego Biurrun
166 d592f67f Michael Niedermayer
        flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
167
    }
168 115329f1 Diego Biurrun
169 d592f67f Michael Niedermayer
#ifdef STRICT_LIMITS
170
    c->symCount++;
171
#endif
172 5e20f836 Michael Niedermayer
173 b46243ed Alex Beregszaszi
    return (put_bits_count(&c->pb)+7)>>3;
174 d592f67f Michael Niedermayer
}
175
176 61ccfcc0 Michael Niedermayer
/**
177
 * put (truncated) unary binarization.
178
 */
179
static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
180
    int i;
181 115329f1 Diego Biurrun
182 61ccfcc0 Michael Niedermayer
    assert(v <= max);
183 115329f1 Diego Biurrun
184 61ccfcc0 Michael Niedermayer
#if 1
185
    for(i=0; i<v; i++){
186
        put_cabac(c, state, 1);
187
        if(i < max_index) state++;
188
    }
189
    if(truncated==0 || v<max)
190
        put_cabac(c, state, 0);
191
#else
192
    if(v <= max_index){
193
        for(i=0; i<v; i++){
194
            put_cabac(c, state+i, 1);
195
        }
196
        if(truncated==0 || v<max)
197
            put_cabac(c, state+i, 0);
198
    }else{
199
        for(i=0; i<=max_index; i++){
200
            put_cabac(c, state+i, 1);
201
        }
202
        for(; i<v; i++){
203
            put_cabac(c, state+max_index, 1);
204
        }
205
        if(truncated==0 || v<max)
206
            put_cabac(c, state+max_index, 0);
207
    }
208
#endif
209
}
210
211
/**
212
 * put unary exp golomb k-th order binarization.
213
 */
214 8f8c0800 Michael Niedermayer
static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
215 61ccfcc0 Michael Niedermayer
    int i;
216 115329f1 Diego Biurrun
217 61ccfcc0 Michael Niedermayer
    if(v==0)
218
        put_cabac(c, state, 0);
219
    else{
220 8f8c0800 Michael Niedermayer
        const int sign= v < 0;
221 115329f1 Diego Biurrun
222 8f8c0800 Michael Niedermayer
        if(is_signed) v= ABS(v);
223 115329f1 Diego Biurrun
224 61ccfcc0 Michael Niedermayer
        if(v<max){
225
            for(i=0; i<v; i++){
226
                put_cabac(c, state, 1);
227
                if(i < max_index) state++;
228
            }
229
230
            put_cabac(c, state, 0);
231
        }else{
232
            int m= 1<<k;
233
234
            for(i=0; i<max; i++){
235
                put_cabac(c, state, 1);
236
                if(i < max_index) state++;
237
            }
238
239
            v -= max;
240
            while(v >= m){ //FIXME optimize
241
                put_cabac_bypass(c, 1);
242
                v-= m;
243
                m+= m;
244
            }
245
            put_cabac_bypass(c, 0);
246
            while(m>>=1){
247
                put_cabac_bypass(c, v&m);
248
            }
249
        }
250
251
        if(is_signed)
252
            put_cabac_bypass(c, sign);
253
    }
254
}
255
256 ec7eb896 Michael Niedermayer
static void refill(CABACContext *c){
257 6d1feb02 Loren Merritt
    if(c->bytestream <= c->bytestream_end)
258 ec7eb896 Michael Niedermayer
#if CABAC_BITS == 16
259
        c->low+= ((c->bytestream[0]<<9) + (c->bytestream[1])<<1);
260
#else
261
        c->low+= c->bytestream[0]<<1;
262
#endif
263
    c->low -= CABAC_MASK;
264
    c->bytestream+= CABAC_BITS/8;
265
}
266
267 88730be6 Måns Rullgård
#if 0 /* all use commented */
268 ec7eb896 Michael Niedermayer
static void refill2(CABACContext *c){
269
    int i, x;
270

271
    x= c->low ^ (c->low-1);
272
    i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
273

274
    x= -CABAC_MASK;
275 115329f1 Diego Biurrun

276 ec7eb896 Michael Niedermayer
    if(c->bytestream < c->bytestream_end)
277
#if CABAC_BITS == 16
278
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
279
#else
280
        x+= c->bytestream[0]<<1;
281
#endif
282 115329f1 Diego Biurrun
283 ec7eb896 Michael Niedermayer
    c->low += x<<i;
284
    c->bytestream+= CABAC_BITS/8;
285
}
286 88730be6 Måns Rullgård
#endif
287 ec7eb896 Michael Niedermayer
288 d592f67f Michael Niedermayer
static inline void renorm_cabac_decoder(CABACContext *c){
289 ec7eb896 Michael Niedermayer
    while(c->range < (0x200 << CABAC_BITS)){
290 d592f67f Michael Niedermayer
        c->range+= c->range;
291
        c->low+= c->low;
292 ec7eb896 Michael Niedermayer
        if(!(c->low & CABAC_MASK))
293
            refill(c);
294 d592f67f Michael Niedermayer
    }
295
}
296
297 ec7eb896 Michael Niedermayer
static inline void renorm_cabac_decoder_once(CABACContext *c){
298
    int mask= (c->range - (0x200 << CABAC_BITS))>>31;
299
    c->range+= c->range&mask;
300
    c->low  += c->low  &mask;
301
    if(!(c->low & CABAC_MASK))
302
        refill(c);
303
}
304
305 d592f67f Michael Niedermayer
static inline int get_cabac(CABACContext *c, uint8_t * const state){
306 ec7eb896 Michael Niedermayer
    int RangeLPS= c->lps_range[*state][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
307 88730be6 Måns Rullgård
    int bit, lps_mask attribute_unused;
308 115329f1 Diego Biurrun
309 d592f67f Michael Niedermayer
    c->range -= RangeLPS;
310 ec7eb896 Michael Niedermayer
#if 1
311 d592f67f Michael Niedermayer
    if(c->low < c->range){
312
        bit= (*state)&1;
313
        *state= c->mps_state[*state];
314 ec7eb896 Michael Niedermayer
        renorm_cabac_decoder_once(c);
315 d592f67f Michael Niedermayer
    }else{
316 ec7eb896 Michael Niedermayer
//        int shift= ff_h264_norm_shift[RangeLPS>>17];
317 d592f67f Michael Niedermayer
        bit= ((*state)&1)^1;
318
        c->low -= c->range;
319
        *state= c->lps_state[*state];
320 ec7eb896 Michael Niedermayer
        c->range = RangeLPS;
321
        renorm_cabac_decoder(c);
322
/*        c->range = RangeLPS<<shift;
323
        c->low <<= shift;
324
        if(!(c->low & 0xFFFF)){
325
            refill2(c);
326
        }*/
327 d592f67f Michael Niedermayer
    }
328 ec7eb896 Michael Niedermayer
#else
329
    lps_mask= (c->range - c->low)>>31;
330 115329f1 Diego Biurrun
331 ec7eb896 Michael Niedermayer
    c->low -= c->range & lps_mask;
332
    c->range += (RangeLPS - c->range) & lps_mask;
333 115329f1 Diego Biurrun
334 ec7eb896 Michael Niedermayer
    bit= ((*state)^lps_mask)&1;
335
    *state= c->mps_state[(*state) - (128&lps_mask)];
336 115329f1 Diego Biurrun
337 ec7eb896 Michael Niedermayer
    lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)];
338
    c->range<<= lps_mask;
339
    c->low  <<= lps_mask;
340
    if(!(c->low & CABAC_MASK))
341
        refill2(c);
342
#endif
343
344 115329f1 Diego Biurrun
    return bit;
345 d592f67f Michael Niedermayer
}
346
347
static inline int get_cabac_bypass(CABACContext *c){
348
    c->low += c->low;
349
350 ec7eb896 Michael Niedermayer
    if(!(c->low & CABAC_MASK))
351
        refill(c);
352 115329f1 Diego Biurrun
353 d592f67f Michael Niedermayer
    if(c->low < c->range){
354
        return 0;
355
    }else{
356
        c->low -= c->range;
357
        return 1;
358
    }
359
}
360
361 5e20f836 Michael Niedermayer
/**
362
 *
363
 * @return the number of bytes read or 0 if no end
364
 */
365 d592f67f Michael Niedermayer
static inline int get_cabac_terminate(CABACContext *c){
366 ec7eb896 Michael Niedermayer
    c->range -= 4<<CABAC_BITS;
367 d592f67f Michael Niedermayer
    if(c->low < c->range){
368 ec7eb896 Michael Niedermayer
        renorm_cabac_decoder_once(c);
369 d592f67f Michael Niedermayer
        return 0;
370
    }else{
371 5e20f836 Michael Niedermayer
        return c->bytestream - c->bytestream_start;
372 115329f1 Diego Biurrun
    }
373 d592f67f Michael Niedermayer
}
374
375 61ccfcc0 Michael Niedermayer
/**
376
 * get (truncated) unnary binarization.
377
 */
378
static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
379
    int i;
380 115329f1 Diego Biurrun
381
    for(i=0; i<max; i++){
382 61ccfcc0 Michael Niedermayer
        if(get_cabac(c, state)==0)
383
            return i;
384 115329f1 Diego Biurrun
385 61ccfcc0 Michael Niedermayer
        if(i< max_index) state++;
386
    }
387
388
    return truncated ? max : -1;
389
}
390
391
/**
392
 * get unary exp golomb k-th order binarization.
393
 */
394
static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
395
    int i, v;
396
    int m= 1<<k;
397 115329f1 Diego Biurrun
398
    if(get_cabac(c, state)==0)
399 61ccfcc0 Michael Niedermayer
        return 0;
400 115329f1 Diego Biurrun
401 61ccfcc0 Michael Niedermayer
    if(0 < max_index) state++;
402 115329f1 Diego Biurrun
403
    for(i=1; i<max; i++){
404 61ccfcc0 Michael Niedermayer
        if(get_cabac(c, state)==0){
405
            if(is_signed && get_cabac_bypass(c)){
406
                return -i;
407
            }else
408
                return i;
409
        }
410
411
        if(i < max_index) state++;
412
    }
413 115329f1 Diego Biurrun
414 61ccfcc0 Michael Niedermayer
    while(get_cabac_bypass(c)){
415
        i+= m;
416
        m+= m;
417
    }
418 115329f1 Diego Biurrun
419 61ccfcc0 Michael Niedermayer
    v=0;
420
    while(m>>=1){
421
        v+= v + get_cabac_bypass(c);
422
    }
423
    i += v;
424
425
    if(is_signed && get_cabac_bypass(c)){
426
        return -i;
427
    }else
428
        return i;
429
}