comparison cabac.h @ 2967:ef2149182f1c libavcodec

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents aaa8b65d1253
children 0b546eab515d
comparison
equal deleted inserted replaced
2966:564788471dd4 2967:ef2149182f1c
15 * You should have received a copy of the GNU Lesser General Public 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 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 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * 18 *
19 */ 19 */
20 20
21 /** 21 /**
22 * @file cabac.h 22 * @file cabac.h
23 * Context Adaptive Binary Arithmetic Coder. 23 * Context Adaptive Binary Arithmetic Coder.
24 */ 24 */
25 25
52 extern const uint8_t ff_h264_norm_shift[256]; 52 extern const uint8_t ff_h264_norm_shift[256];
53 53
54 54
55 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size); 55 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
56 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size); 56 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
57 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], 57 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
58 uint8_t const *mps_state, uint8_t const *lps_state, int state_count); 58 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
59 59
60 60
61 static inline void put_cabac_bit(CABACContext *c, int b){ 61 static inline void put_cabac_bit(CABACContext *c, int b){
62 put_bits(&c->pb, 1, b); 62 put_bits(&c->pb, 1, b);
63 for(;c->outstanding_count; c->outstanding_count--){ 63 for(;c->outstanding_count; c->outstanding_count--){
64 put_bits(&c->pb, 1, 1-b); 64 put_bits(&c->pb, 1, 1-b);
65 } 65 }
66 } 66 }
67 67
68 static inline void renorm_cabac_encoder(CABACContext *c){ 68 static inline void renorm_cabac_encoder(CABACContext *c){
75 c->low -= 0x100; 75 c->low -= 0x100;
76 }else{ 76 }else{
77 put_cabac_bit(c, 1); 77 put_cabac_bit(c, 1);
78 c->low -= 0x200; 78 c->low -= 0x200;
79 } 79 }
80 80
81 c->range+= c->range; 81 c->range+= c->range;
82 c->low += c->low; 82 c->low += c->low;
83 } 83 }
84 } 84 }
85 85
86 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){ 86 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
87 int RangeLPS= c->lps_range[*state][c->range>>6]; 87 int RangeLPS= c->lps_range[*state][c->range>>6];
88 88
89 if(bit == ((*state)&1)){ 89 if(bit == ((*state)&1)){
90 c->range -= RangeLPS; 90 c->range -= RangeLPS;
91 *state= c->mps_state[*state]; 91 *state= c->mps_state[*state];
92 }else{ 92 }else{
93 c->low += c->range - RangeLPS; 93 c->low += c->range - RangeLPS;
94 c->range = RangeLPS; 94 c->range = RangeLPS;
95 *state= c->lps_state[*state]; 95 *state= c->lps_state[*state];
96 } 96 }
97 97
98 renorm_cabac_encoder(c); 98 renorm_cabac_encoder(c);
99 99
100 #ifdef STRICT_LIMITS 100 #ifdef STRICT_LIMITS
101 c->symCount++; 101 c->symCount++;
102 #endif 102 #endif
136 c->low -= 0x200; 136 c->low -= 0x200;
137 }else{ 137 }else{
138 put_cabac_bit(c, 1); 138 put_cabac_bit(c, 1);
139 c->low -= 0x400; 139 c->low -= 0x400;
140 } 140 }
141 141
142 #ifdef STRICT_LIMITS 142 #ifdef STRICT_LIMITS
143 c->symCount++; 143 c->symCount++;
144 #endif 144 #endif
145 } 145 }
146 146
154 if(!bit){ 154 if(!bit){
155 renorm_cabac_encoder(c); 155 renorm_cabac_encoder(c);
156 }else{ 156 }else{
157 c->low += c->range; 157 c->low += c->range;
158 c->range= 2; 158 c->range= 2;
159 159
160 renorm_cabac_encoder(c); 160 renorm_cabac_encoder(c);
161 161
162 assert(c->low <= 0x1FF); 162 assert(c->low <= 0x1FF);
163 put_cabac_bit(c, c->low>>9); 163 put_cabac_bit(c, c->low>>9);
164 put_bits(&c->pb, 2, ((c->low>>7)&3)|1); 164 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
165 165
166 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong 166 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
167 } 167 }
168 168
169 #ifdef STRICT_LIMITS 169 #ifdef STRICT_LIMITS
170 c->symCount++; 170 c->symCount++;
171 #endif 171 #endif
172 172
173 return (put_bits_count(&c->pb)+7)>>3; 173 return (put_bits_count(&c->pb)+7)>>3;
176 /** 176 /**
177 * put (truncated) unary binarization. 177 * put (truncated) unary binarization.
178 */ 178 */
179 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){ 179 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
180 int i; 180 int i;
181 181
182 assert(v <= max); 182 assert(v <= max);
183 183
184 #if 1 184 #if 1
185 for(i=0; i<v; i++){ 185 for(i=0; i<v; i++){
186 put_cabac(c, state, 1); 186 put_cabac(c, state, 1);
187 if(i < max_index) state++; 187 if(i < max_index) state++;
188 } 188 }
211 /** 211 /**
212 * put unary exp golomb k-th order binarization. 212 * put unary exp golomb k-th order binarization.
213 */ 213 */
214 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){ 214 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
215 int i; 215 int i;
216 216
217 if(v==0) 217 if(v==0)
218 put_cabac(c, state, 0); 218 put_cabac(c, state, 0);
219 else{ 219 else{
220 const int sign= v < 0; 220 const int sign= v < 0;
221 221
222 if(is_signed) v= ABS(v); 222 if(is_signed) v= ABS(v);
223 223
224 if(v<max){ 224 if(v<max){
225 for(i=0; i<v; i++){ 225 for(i=0; i<v; i++){
226 put_cabac(c, state, 1); 226 put_cabac(c, state, 1);
227 if(i < max_index) state++; 227 if(i < max_index) state++;
228 } 228 }
270 270
271 x= c->low ^ (c->low-1); 271 x= c->low ^ (c->low-1);
272 i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)]; 272 i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
273 273
274 x= -CABAC_MASK; 274 x= -CABAC_MASK;
275 275
276 if(c->bytestream < c->bytestream_end) 276 if(c->bytestream < c->bytestream_end)
277 #if CABAC_BITS == 16 277 #if CABAC_BITS == 16
278 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); 278 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
279 #else 279 #else
280 x+= c->bytestream[0]<<1; 280 x+= c->bytestream[0]<<1;
281 #endif 281 #endif
282 282
283 c->low += x<<i; 283 c->low += x<<i;
284 c->bytestream+= CABAC_BITS/8; 284 c->bytestream+= CABAC_BITS/8;
285 } 285 }
286 #endif 286 #endif
287 287
303 } 303 }
304 304
305 static inline int get_cabac(CABACContext *c, uint8_t * const state){ 305 static inline int get_cabac(CABACContext *c, uint8_t * const state){
306 int RangeLPS= c->lps_range[*state][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1); 306 int RangeLPS= c->lps_range[*state][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
307 int bit, lps_mask attribute_unused; 307 int bit, lps_mask attribute_unused;
308 308
309 c->range -= RangeLPS; 309 c->range -= RangeLPS;
310 #if 1 310 #if 1
311 if(c->low < c->range){ 311 if(c->low < c->range){
312 bit= (*state)&1; 312 bit= (*state)&1;
313 *state= c->mps_state[*state]; 313 *state= c->mps_state[*state];
325 refill2(c); 325 refill2(c);
326 }*/ 326 }*/
327 } 327 }
328 #else 328 #else
329 lps_mask= (c->range - c->low)>>31; 329 lps_mask= (c->range - c->low)>>31;
330 330
331 c->low -= c->range & lps_mask; 331 c->low -= c->range & lps_mask;
332 c->range += (RangeLPS - c->range) & lps_mask; 332 c->range += (RangeLPS - c->range) & lps_mask;
333 333
334 bit= ((*state)^lps_mask)&1; 334 bit= ((*state)^lps_mask)&1;
335 *state= c->mps_state[(*state) - (128&lps_mask)]; 335 *state= c->mps_state[(*state) - (128&lps_mask)];
336 336
337 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)]; 337 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)];
338 c->range<<= lps_mask; 338 c->range<<= lps_mask;
339 c->low <<= lps_mask; 339 c->low <<= lps_mask;
340 if(!(c->low & CABAC_MASK)) 340 if(!(c->low & CABAC_MASK))
341 refill2(c); 341 refill2(c);
342 #endif 342 #endif
343 343
344 return bit; 344 return bit;
345 } 345 }
346 346
347 static inline int get_cabac_bypass(CABACContext *c){ 347 static inline int get_cabac_bypass(CABACContext *c){
348 c->low += c->low; 348 c->low += c->low;
349 349
350 if(!(c->low & CABAC_MASK)) 350 if(!(c->low & CABAC_MASK))
351 refill(c); 351 refill(c);
352 352
353 if(c->low < c->range){ 353 if(c->low < c->range){
354 return 0; 354 return 0;
355 }else{ 355 }else{
356 c->low -= c->range; 356 c->low -= c->range;
357 return 1; 357 return 1;
367 if(c->low < c->range){ 367 if(c->low < c->range){
368 renorm_cabac_decoder_once(c); 368 renorm_cabac_decoder_once(c);
369 return 0; 369 return 0;
370 }else{ 370 }else{
371 return c->bytestream - c->bytestream_start; 371 return c->bytestream - c->bytestream_start;
372 } 372 }
373 } 373 }
374 374
375 /** 375 /**
376 * get (truncated) unnary binarization. 376 * get (truncated) unnary binarization.
377 */ 377 */
378 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){ 378 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
379 int i; 379 int i;
380 380
381 for(i=0; i<max; i++){ 381 for(i=0; i<max; i++){
382 if(get_cabac(c, state)==0) 382 if(get_cabac(c, state)==0)
383 return i; 383 return i;
384 384
385 if(i< max_index) state++; 385 if(i< max_index) state++;
386 } 386 }
387 387
388 return truncated ? max : -1; 388 return truncated ? max : -1;
389 } 389 }
392 * get unary exp golomb k-th order binarization. 392 * get unary exp golomb k-th order binarization.
393 */ 393 */
394 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){ 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; 395 int i, v;
396 int m= 1<<k; 396 int m= 1<<k;
397 397
398 if(get_cabac(c, state)==0) 398 if(get_cabac(c, state)==0)
399 return 0; 399 return 0;
400 400
401 if(0 < max_index) state++; 401 if(0 < max_index) state++;
402 402
403 for(i=1; i<max; i++){ 403 for(i=1; i<max; i++){
404 if(get_cabac(c, state)==0){ 404 if(get_cabac(c, state)==0){
405 if(is_signed && get_cabac_bypass(c)){ 405 if(is_signed && get_cabac_bypass(c)){
406 return -i; 406 return -i;
407 }else 407 }else
408 return i; 408 return i;
409 } 409 }
410 410
411 if(i < max_index) state++; 411 if(i < max_index) state++;
412 } 412 }
413 413
414 while(get_cabac_bypass(c)){ 414 while(get_cabac_bypass(c)){
415 i+= m; 415 i+= m;
416 m+= m; 416 m+= m;
417 } 417 }
418 418
419 v=0; 419 v=0;
420 while(m>>=1){ 420 while(m>>=1){
421 v+= v + get_cabac_bypass(c); 421 v+= v + get_cabac_bypass(c);
422 } 422 }
423 i += v; 423 i += v;