Statistics
| Branch: | Revision:

ffmpeg / libavcodec / cabac.h @ 8f8c0800

History | View | Annotate | Download (8.46 KB)

1
/*
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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
 
21
/**
22
 * @file cabac.h
23
 * Context Adaptive Binary Arithmetic Coder.
24
 */
25

    
26

    
27
#undef NDEBUG
28
#include <assert.h>
29

    
30
typedef struct CABACContext{
31
    int low;
32
    int range;
33
    int outstanding_count;
34
#ifdef STRICT_LIMITS
35
    int symCount;
36
#endif
37
    uint8_t lps_range[2*64][4];   ///< rangeTabLPS
38
    uint8_t lps_state[2*64];      ///< transIdxLPS
39
    uint8_t mps_state[2*64];      ///< transIdxMPS
40
    uint8_t *bytestream;
41
    int bits_left;                ///<
42
    PutBitContext pb;
43
}CABACContext;
44

    
45
const uint8_t ff_h264_lps_range[64][4];
46
const uint8_t ff_h264_mps_state[64];
47
const uint8_t ff_h264_lps_state[64];
48

    
49
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
50
void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
51
void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], 
52
                          uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
53

    
54

    
55
static inline void put_cabac_bit(CABACContext *c, int b){
56
    put_bits(&c->pb, 1, b); 
57
    for(;c->outstanding_count; c->outstanding_count--){ 
58
        put_bits(&c->pb, 1, 1-b);
59
    }
60
}
61

    
62
static inline void renorm_cabac_encoder(CABACContext *c){
63
    while(c->range < 0x100){
64
        //FIXME optimize
65
        if(c->low<0x100){
66
            put_cabac_bit(c, 0);
67
        }else if(c->low<0x200){
68
            c->outstanding_count++;
69
            c->low -= 0x100;
70
        }else{
71
            put_cabac_bit(c, 1);
72
            c->low -= 0x200;
73
        }
74
        
75
        c->range+= c->range;
76
        c->low += c->low;
77
    }
78
}
79

    
80
static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
81
    int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
82
    
83
    if(bit == ((*state)&1)){
84
        c->range -= RangeLPS;
85
        *state= c->mps_state[*state];
86
    }else{
87
        c->low += c->range - RangeLPS;
88
        c->range = RangeLPS;
89
        *state= c->lps_state[*state];
90
    }
91
    
92
    renorm_cabac_encoder(c);
93

    
94
#ifdef STRICT_LIMITS
95
    c->symCount++;
96
#endif
97
}
98

    
99
static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
100
    assert(c->range > RangeLPS);
101

    
102
    if(!bit){
103
        c->range -= RangeLPS;
104
    }else{
105
        c->low += c->range - RangeLPS;
106
        c->range = RangeLPS;
107
    }
108

    
109
    renorm_cabac_encoder(c);
110

    
111
#ifdef STRICT_LIMITS
112
    c->symCount++;
113
#endif
114
}
115

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

    
122
    if(bit){
123
        c->low += c->range;
124
    }
125
//FIXME optimize
126
    if(c->low<0x200){
127
        put_cabac_bit(c, 0);
128
    }else if(c->low<0x400){
129
        c->outstanding_count++;
130
        c->low -= 0x200;
131
    }else{
132
        put_cabac_bit(c, 1);
133
        c->low -= 0x400;
134
    }
135
        
136
#ifdef STRICT_LIMITS
137
    c->symCount++;
138
#endif
139
}
140

    
141
static inline void put_cabac_terminate(CABACContext *c, int bit){
142
    c->range -= 2;
143

    
144
    if(!bit){
145
        renorm_cabac_encoder(c);
146
    }else{
147
        c->low += c->range;
148
        c->range= 2;
149
        
150
        renorm_cabac_encoder(c);
151

    
152
        assert(c->low <= 0x1FF);
153
        put_cabac_bit(c, c->low>>9);
154
        put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
155
        
156
        flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
157
    }
158
        
159
#ifdef STRICT_LIMITS
160
    c->symCount++;
161
#endif
162
}
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 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
        const int sign= v < 0;
209
        
210
        if(is_signed) v= ABS(v);
211
        
212
        if(v<max){
213
            for(i=0; i<v; i++){
214
                put_cabac(c, state, 1);
215
                if(i < max_index) state++;
216
            }
217

    
218
            put_cabac(c, state, 0);
219
        }else{
220
            int m= 1<<k;
221

    
222
            for(i=0; i<max; i++){
223
                put_cabac(c, state, 1);
224
                if(i < max_index) state++;
225
            }
226

    
227
            v -= max;
228
            while(v >= m){ //FIXME optimize
229
                put_cabac_bypass(c, 1);
230
                v-= m;
231
                m+= m;
232
            }
233
            put_cabac_bypass(c, 0);
234
            while(m>>=1){
235
                put_cabac_bypass(c, v&m);
236
            }
237
        }
238

    
239
        if(is_signed)
240
            put_cabac_bypass(c, sign);
241
    }
242
}
243

    
244
static inline void renorm_cabac_decoder(CABACContext *c){
245
    while(c->range < 0x10000){
246
        c->range+= c->range;
247
        c->low+= c->low;
248
        if(--c->bits_left == 0){
249
            c->low+= *c->bytestream++;
250
            c->bits_left= 8;
251
        }
252
    }
253
}
254

    
255
static inline int get_cabac(CABACContext *c, uint8_t * const state){
256
    int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
257
    int bit;
258
    
259
    c->range -= RangeLPS;
260
    if(c->low < c->range){
261
        bit= (*state)&1;
262
        *state= c->mps_state[*state];
263
    }else{
264
        bit= ((*state)&1)^1;
265
        c->low -= c->range;
266
        c->range = RangeLPS;
267
        *state= c->lps_state[*state];
268
    }
269
    renorm_cabac_decoder(c);
270
    
271
    return bit;    
272
}
273

    
274
static inline int get_cabac_static(CABACContext *c, int RangeLPS){
275
    int bit;
276
    
277
    c->range -= RangeLPS;
278
    if(c->low < c->range){
279
        bit= 0;
280
    }else{
281
        bit= 1;
282
        c->low -= c->range;
283
        c->range = RangeLPS;
284
    }
285
    renorm_cabac_decoder(c);
286
    
287
    return bit;    
288
}
289

    
290
static inline int get_cabac_bypass(CABACContext *c){
291
    c->low += c->low;
292

    
293
    if(--c->bits_left == 0){
294
        c->low+= *c->bytestream++;
295
        c->bits_left= 8;
296
    }
297
    
298
    if(c->low < c->range){
299
        return 0;
300
    }else{
301
        c->low -= c->range;
302
        return 1;
303
    }
304
}
305

    
306
static inline int get_cabac_terminate(CABACContext *c){
307
    c->range -= 2<<8;
308
    if(c->low < c->range){
309
        renorm_cabac_decoder(c);    
310
        return 0;
311
    }else{
312
        return 1;
313
    }    
314
}
315

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

    
329
    return truncated ? max : -1;
330
}
331

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

    
352
        if(i < max_index) state++;
353
    }
354
    
355
    while(get_cabac_bypass(c)){
356
        i+= m;
357
        m+= m;
358
    }
359
    
360
    v=0;
361
    while(m>>=1){
362
        v+= v + get_cabac_bypass(c);
363
    }
364
    i += v;
365

    
366
    if(is_signed && get_cabac_bypass(c)){
367
        return -i;
368
    }else
369
        return i;
370
}