1287
|
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
|
1290
|
116 /**
|
|
117 * @param bit 0 -> write zero bit, !=0 write one bit
|
|
118 */
|
1287
|
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
|
1290
|
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
|
1287
|
240 static inline void renorm_cabac_decoder(CABACContext *c){
|
|
241 while(c->range < 0x10000){
|
|
242 c->range+= c->range;
|
|
243 c->low+= c->low;
|
|
244 if(--c->bits_left == 0){
|
|
245 c->low+= *c->bytestream++;
|
|
246 c->bits_left= 8;
|
|
247 }
|
|
248 }
|
|
249 }
|
|
250
|
|
251 static inline int get_cabac(CABACContext *c, uint8_t * const state){
|
|
252 int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
|
|
253 int bit;
|
|
254
|
|
255 c->range -= RangeLPS;
|
|
256 if(c->low < c->range){
|
|
257 bit= (*state)&1;
|
|
258 *state= c->mps_state[*state];
|
|
259 }else{
|
|
260 bit= ((*state)&1)^1;
|
|
261 c->low -= c->range;
|
|
262 c->range = RangeLPS;
|
|
263 *state= c->lps_state[*state];
|
|
264 }
|
|
265 renorm_cabac_decoder(c);
|
|
266
|
|
267 return bit;
|
|
268 }
|
|
269
|
|
270 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
|
|
271 int bit;
|
|
272
|
|
273 c->range -= RangeLPS;
|
|
274 if(c->low < c->range){
|
|
275 bit= 0;
|
|
276 }else{
|
|
277 bit= 1;
|
|
278 c->low -= c->range;
|
|
279 c->range = RangeLPS;
|
|
280 }
|
|
281 renorm_cabac_decoder(c);
|
|
282
|
|
283 return bit;
|
|
284 }
|
|
285
|
|
286 static inline int get_cabac_bypass(CABACContext *c){
|
|
287 c->low += c->low;
|
|
288
|
|
289 if(--c->bits_left == 0){
|
|
290 c->low+= *c->bytestream++;
|
|
291 c->bits_left= 8;
|
|
292 }
|
|
293
|
|
294 if(c->low < c->range){
|
|
295 return 0;
|
|
296 }else{
|
|
297 c->low -= c->range;
|
|
298 return 1;
|
|
299 }
|
|
300 }
|
|
301
|
|
302 static inline int get_cabac_terminate(CABACContext *c){
|
|
303 c->range -= 2<<8;
|
|
304 if(c->low < c->range){
|
|
305 renorm_cabac_decoder(c);
|
|
306 return 0;
|
|
307 }else{
|
|
308 return 1;
|
|
309 }
|
|
310 }
|
|
311
|
1290
|
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 }
|