Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_libavcodec / libavcodec / cabac.h
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_start;
41     uint8_t *bytestream;
42     int bits_left;                ///<
43     PutBitContext pb;
44 }CABACContext;
45
46 extern const uint8_t ff_h264_lps_range[64][4];
47 extern const uint8_t ff_h264_mps_state[64];
48 extern const uint8_t ff_h264_lps_state[64];
49
50 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
51 void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
52 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], 
53                           uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
54
55
56 static inline void put_cabac_bit(CABACContext *c, int b){
57     put_bits(&c->pb, 1, b); 
58     for(;c->outstanding_count; c->outstanding_count--){ 
59         put_bits(&c->pb, 1, 1-b);
60     }
61 }
62
63 static inline void renorm_cabac_encoder(CABACContext *c){
64     while(c->range < 0x100){
65         //FIXME optimize
66         if(c->low<0x100){
67             put_cabac_bit(c, 0);
68         }else if(c->low<0x200){
69             c->outstanding_count++;
70             c->low -= 0x100;
71         }else{
72             put_cabac_bit(c, 1);
73             c->low -= 0x200;
74         }
75         
76         c->range+= c->range;
77         c->low += c->low;
78     }
79 }
80
81 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
82     int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
83     
84     if(bit == ((*state)&1)){
85         c->range -= RangeLPS;
86         *state= c->mps_state[*state];
87     }else{
88         c->low += c->range - RangeLPS;
89         c->range = RangeLPS;
90         *state= c->lps_state[*state];
91     }
92     
93     renorm_cabac_encoder(c);
94
95 #ifdef STRICT_LIMITS
96     c->symCount++;
97 #endif
98 }
99
100 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
101     assert(c->range > RangeLPS);
102
103     if(!bit){
104         c->range -= RangeLPS;
105     }else{
106         c->low += c->range - RangeLPS;
107         c->range = RangeLPS;
108     }
109
110     renorm_cabac_encoder(c);
111
112 #ifdef STRICT_LIMITS
113     c->symCount++;
114 #endif
115 }
116
117 /**
118  * @param bit 0 -> write zero bit, !=0 write one bit
119  */
120 static inline void put_cabac_bypass(CABACContext *c, int bit){
121     c->low += c->low;
122
123     if(bit){
124         c->low += c->range;
125     }
126 //FIXME optimize
127     if(c->low<0x200){
128         put_cabac_bit(c, 0);
129     }else if(c->low<0x400){
130         c->outstanding_count++;
131         c->low -= 0x200;
132     }else{
133         put_cabac_bit(c, 1);
134         c->low -= 0x400;
135     }
136         
137 #ifdef STRICT_LIMITS
138     c->symCount++;
139 #endif
140 }
141
142 /**
143  *
144  * @return the number of bytes written
145  */
146 static inline int put_cabac_terminate(CABACContext *c, int bit){
147     c->range -= 2;
148
149     if(!bit){
150         renorm_cabac_encoder(c);
151     }else{
152         c->low += c->range;
153         c->range= 2;
154         
155         renorm_cabac_encoder(c);
156
157         assert(c->low <= 0x1FF);
158         put_cabac_bit(c, c->low>>9);
159         put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
160         
161         flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
162     }
163         
164 #ifdef STRICT_LIMITS
165     c->symCount++;
166 #endif
167
168     return (get_bit_count(&c->pb)+7)>>3;
169 }
170
171 /**
172  * put (truncated) unary binarization.
173  */
174 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
175     int i;
176     
177     assert(v <= max);
178     
179 #if 1
180     for(i=0; i<v; i++){
181         put_cabac(c, state, 1);
182         if(i < max_index) state++;
183     }
184     if(truncated==0 || v<max)
185         put_cabac(c, state, 0);
186 #else
187     if(v <= max_index){
188         for(i=0; i<v; i++){
189             put_cabac(c, state+i, 1);
190         }
191         if(truncated==0 || v<max)
192             put_cabac(c, state+i, 0);
193     }else{
194         for(i=0; i<=max_index; i++){
195             put_cabac(c, state+i, 1);
196         }
197         for(; i<v; i++){
198             put_cabac(c, state+max_index, 1);
199         }
200         if(truncated==0 || v<max)
201             put_cabac(c, state+max_index, 0);
202     }
203 #endif
204 }
205
206 /**
207  * put unary exp golomb k-th order binarization.
208  */
209 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
210     int i;
211     
212     if(v==0)
213         put_cabac(c, state, 0);
214     else{
215         const int sign= v < 0;
216         
217         if(is_signed) v= ABS(v);
218         
219         if(v<max){
220             for(i=0; i<v; i++){
221                 put_cabac(c, state, 1);
222                 if(i < max_index) state++;
223             }
224
225             put_cabac(c, state, 0);
226         }else{
227             int m= 1<<k;
228
229             for(i=0; i<max; i++){
230                 put_cabac(c, state, 1);
231                 if(i < max_index) state++;
232             }
233
234             v -= max;
235             while(v >= m){ //FIXME optimize
236                 put_cabac_bypass(c, 1);
237                 v-= m;
238                 m+= m;
239             }
240             put_cabac_bypass(c, 0);
241             while(m>>=1){
242                 put_cabac_bypass(c, v&m);
243             }
244         }
245
246         if(is_signed)
247             put_cabac_bypass(c, sign);
248     }
249 }
250
251 static inline void renorm_cabac_decoder(CABACContext *c){
252     while(c->range < 0x10000){
253         c->range+= c->range;
254         c->low+= c->low;
255         if(--c->bits_left == 0){
256             c->low+= *c->bytestream++;
257             c->bits_left= 8;
258         }
259     }
260 }
261
262 static inline int get_cabac(CABACContext *c, uint8_t * const state){
263     int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
264     int bit;
265     
266     c->range -= RangeLPS;
267     if(c->low < c->range){
268         bit= (*state)&1;
269         *state= c->mps_state[*state];
270     }else{
271         bit= ((*state)&1)^1;
272         c->low -= c->range;
273         c->range = RangeLPS;
274         *state= c->lps_state[*state];
275     }
276     renorm_cabac_decoder(c);
277     
278     return bit;    
279 }
280
281 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
282     int bit;
283     
284     c->range -= RangeLPS;
285     if(c->low < c->range){
286         bit= 0;
287     }else{
288         bit= 1;
289         c->low -= c->range;
290         c->range = RangeLPS;
291     }
292     renorm_cabac_decoder(c);
293     
294     return bit;    
295 }
296
297 static inline int get_cabac_bypass(CABACContext *c){
298     c->low += c->low;
299
300     if(--c->bits_left == 0){
301         c->low+= *c->bytestream++;
302         c->bits_left= 8;
303     }
304     
305     if(c->low < c->range){
306         return 0;
307     }else{
308         c->low -= c->range;
309         return 1;
310     }
311 }
312
313 /**
314  *
315  * @return the number of bytes read or 0 if no end
316  */
317 static inline int get_cabac_terminate(CABACContext *c){
318     c->range -= 2<<8;
319     if(c->low < c->range){
320         renorm_cabac_decoder(c);    
321         return 0;
322     }else{
323         return c->bytestream - c->bytestream_start;
324     }    
325 }
326
327 /**
328  * get (truncated) unnary binarization.
329  */
330 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
331     int i;
332     
333     for(i=0; i<max; i++){ 
334         if(get_cabac(c, state)==0)
335             return i;
336             
337         if(i< max_index) state++;
338     }
339
340     return truncated ? max : -1;
341 }
342
343 /**
344  * get unary exp golomb k-th order binarization.
345  */
346 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
347     int i, v;
348     int m= 1<<k;
349     
350     if(get_cabac(c, state)==0) 
351         return 0;
352         
353     if(0 < max_index) state++;
354     
355     for(i=1; i<max; i++){ 
356         if(get_cabac(c, state)==0){
357             if(is_signed && get_cabac_bypass(c)){
358                 return -i;
359             }else
360                 return i;
361         }
362
363         if(i < max_index) state++;
364     }
365     
366     while(get_cabac_bypass(c)){
367         i+= m;
368         m+= m;
369     }
370     
371     v=0;
372     while(m>>=1){
373         v+= v + get_cabac_bypass(c);
374     }
375     i += v;
376
377     if(is_signed && get_cabac_bypass(c)){
378         return -i;
379     }else
380         return i;
381 }