2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Context Adaptive Binary Arithmetic Coder.
33 #define CABAC_MASK ((1<<CABAC_BITS)-1)
34 #define BRANCHLESS_CABAC_DECODER 1
35 #define CMOV_IS_FAST 1
36 //#define ARCH_X86_DISABLED 1
38 typedef struct CABACContext{
41 int outstanding_count;
45 const uint8_t *bytestream_start;
46 const uint8_t *bytestream;
47 const uint8_t *bytestream_end;
51 extern uint8_t ff_h264_mlps_state[4*64];
52 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
53 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
54 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
55 extern const uint8_t ff_h264_norm_shift[512];
58 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
59 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
60 void ff_init_cabac_states(CABACContext *c);
63 static inline void put_cabac_bit(CABACContext *c, int b){
64 put_bits(&c->pb, 1, b);
65 for(;c->outstanding_count; c->outstanding_count--){
66 put_bits(&c->pb, 1, 1-b);
70 static inline void renorm_cabac_encoder(CABACContext *c){
71 while(c->range < 0x100){
75 }else if(c->low<0x200){
76 c->outstanding_count++;
88 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
89 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
91 if(bit == ((*state)&1)){
93 *state= ff_h264_mps_state[*state];
95 c->low += c->range - RangeLPS;
97 *state= ff_h264_lps_state[*state];
100 renorm_cabac_encoder(c);
107 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
108 assert(c->range > RangeLPS);
111 c->range -= RangeLPS;
113 c->low += c->range - RangeLPS;
117 renorm_cabac_encoder(c);
125 * @param bit 0 -> write zero bit, !=0 write one bit
127 static void put_cabac_bypass(CABACContext *c, int bit){
136 }else if(c->low<0x400){
137 c->outstanding_count++;
151 * @return the number of bytes written
153 static int put_cabac_terminate(CABACContext *c, int bit){
157 renorm_cabac_encoder(c);
162 renorm_cabac_encoder(c);
164 assert(c->low <= 0x1FF);
165 put_cabac_bit(c, c->low>>9);
166 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
168 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
175 return (put_bits_count(&c->pb)+7)>>3;
179 * put (truncated) unary binarization.
181 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
188 put_cabac(c, state, 1);
189 if(i < max_index) state++;
191 if(truncated==0 || v<max)
192 put_cabac(c, state, 0);
196 put_cabac(c, state+i, 1);
198 if(truncated==0 || v<max)
199 put_cabac(c, state+i, 0);
201 for(i=0; i<=max_index; i++){
202 put_cabac(c, state+i, 1);
205 put_cabac(c, state+max_index, 1);
207 if(truncated==0 || v<max)
208 put_cabac(c, state+max_index, 0);
214 * put unary exp golomb k-th order binarization.
216 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
220 put_cabac(c, state, 0);
222 const int sign= v < 0;
224 if(is_signed) v= FFABS(v);
228 put_cabac(c, state, 1);
229 if(i < max_index) state++;
232 put_cabac(c, state, 0);
236 for(i=0; i<max; i++){
237 put_cabac(c, state, 1);
238 if(i < max_index) state++;
242 while(v >= m){ //FIXME optimize
243 put_cabac_bypass(c, 1);
247 put_cabac_bypass(c, 0);
249 put_cabac_bypass(c, v&m);
254 put_cabac_bypass(c, sign);
258 static void refill(CABACContext *c){
260 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
262 c->low+= c->bytestream[0]<<1;
264 c->low -= CABAC_MASK;
265 c->bytestream+= CABAC_BITS/8;
268 static void refill2(CABACContext *c){
271 x= c->low ^ (c->low-1);
272 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
277 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
279 x+= c->bytestream[0]<<1;
283 c->bytestream+= CABAC_BITS/8;
286 static inline void renorm_cabac_decoder(CABACContext *c){
287 while(c->range < 0x100){
290 if(!(c->low & CABAC_MASK))
295 static inline void renorm_cabac_decoder_once(CABACContext *c){
296 #ifdef ARCH_X86_DISABLED
301 "lea -0x100(%0), %2 \n\t"
302 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
305 : "+r"(c->range), "+r"(c->low), "+c"(temp)
310 "cmp $0x100, %0 \n\t"
311 "setb %%cl \n\t" //FIXME 31->63 for x86-64
314 : "+r"(c->range), "+r"(c->low), "+c"(temp)
320 "lea -0x100(%0), %%eax \n\t"
327 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
333 "cmp $0x100, %0 \n\t"
334 "sbb %%edx, %%edx \n\t"
340 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
346 "cmp $0x100, %0 \n\t"
347 "lea (%0, %0), %%eax \n\t"
348 "lea (%1, %1), %%edx \n\t"
349 "cmovb %%eax, %0 \n\t"
350 "cmovb %%edx, %1 \n\t"
351 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
356 int shift= (uint32_t)(c->range - 0x100)>>31;
360 if(!(c->low & CABAC_MASK))
364 static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state){
365 //FIXME gcc generates duplicate load/stores for c->low and c->range
368 #define BYTESTART "12"
371 #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
374 #ifndef BRANCHLESS_CABAC_DECODER
376 "movzbl (%1), %0 \n\t"
377 "movl "RANGE "(%2), %%ebx \n\t"
378 "movl "RANGE "(%2), %%edx \n\t"
379 "andl $0xC0, %%ebx \n\t"
380 "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
381 "movl "LOW "(%2), %%ebx \n\t"
382 //eax:state ebx:low, edx:range, esi:RangeLPS
383 "subl %%esi, %%edx \n\t"
384 "movl %%edx, %%ecx \n\t"
385 "shll $17, %%ecx \n\t"
386 "cmpl %%ecx, %%ebx \n\t"
390 //athlon:4067 P3:4110
391 "lea -0x100(%%edx), %%ecx \n\t"
392 "shr $31, %%ecx \n\t"
393 "shl %%cl, %%edx \n\t"
394 "shl %%cl, %%ebx \n\t"
396 //athlon:4057 P3:4130
397 "cmp $0x100, %%edx \n\t" //FIXME avoidable
399 "shl %%cl, %%edx \n\t"
400 "shl %%cl, %%ebx \n\t"
402 "movzbl "MANGLE(ff_h264_mps_state)"(%0), %%ecx \n\t"
403 "movb %%cl, (%1) \n\t"
404 //eax:state ebx:low, edx:range, esi:RangeLPS
405 "test %%bx, %%bx \n\t"
407 "movl "BYTE "(%2), %%esi \n\t"
408 "subl $0xFFFF, %%ebx \n\t"
409 "movzwl (%%esi), %%ecx \n\t"
411 "shrl $15, %%ecx \n\t"
412 "addl $2, %%esi \n\t"
413 "addl %%ecx, %%ebx \n\t"
414 "movl %%esi, "BYTE "(%2) \n\t"
417 //eax:state ebx:low, edx:range, esi:RangeLPS
418 "subl %%ecx, %%ebx \n\t"
419 "movl %%esi, %%edx \n\t"
420 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
421 "shll %%cl, %%ebx \n\t"
422 "shll %%cl, %%edx \n\t"
423 "movzbl "MANGLE(ff_h264_lps_state)"(%0), %%ecx \n\t"
424 "movb %%cl, (%1) \n\t"
426 "test %%bx, %%bx \n\t"
429 "movl "BYTE "(%2), %%ecx \n\t"
430 "movzwl (%%ecx), %%esi \n\t"
432 "shrl $15, %%esi \n\t"
433 "subl $0xFFFF, %%esi \n\t"
434 "addl $2, %%ecx \n\t"
435 "movl %%ecx, "BYTE "(%2) \n\t"
437 "leal -1(%%ebx), %%ecx \n\t"
438 "xorl %%ebx, %%ecx \n\t"
439 "shrl $15, %%ecx \n\t"
440 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
444 "shll %%cl , %%esi \n\t"
445 "addl %%esi, %%ebx \n\t"
447 "movl %%edx, "RANGE "(%2) \n\t"
448 "movl %%ebx, "LOW "(%2) \n\t"
449 :"=&a"(bit) //FIXME this is fragile gcc either runs out of registers or misscompiles it (for example if "+a"(bit) or "+m"(*state) is used
451 : "%ecx", "%ebx", "%edx", "%esi", "memory"
454 #else /* BRANCHLESS_CABAC_DECODER */
456 "movzbl (%1), %0 \n\t"
457 "movl "RANGE "(%2), %%ebx \n\t"
458 "movl "RANGE "(%2), %%edx \n\t"
459 "andl $0xC0, %%ebx \n\t"
460 "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
461 "movl "LOW "(%2), %%ebx \n\t"
462 //eax:state ebx:low, edx:range, esi:RangeLPS
463 "subl %%esi, %%edx \n\t"
464 #if (defined CMOV_IS_FAST && __CPU__ >= 686)
465 "movl %%edx, %%ecx \n\t"
466 "shl $17, %%edx \n\t"
467 "cmpl %%ebx, %%edx \n\t"
468 "cmova %%ecx, %%esi \n\t"
469 "sbbl %%ecx, %%ecx \n\t"
470 "andl %%ecx, %%edx \n\t"
471 "subl %%edx, %%ebx \n\t"
472 "xorl %%ecx, %0 \n\t"
473 #else /* CMOV_IS_FAST */
474 "movl %%edx, %%ecx \n\t"
475 "shl $17, %%edx \n\t"
476 "subl %%ebx, %%edx \n\t"
477 "sarl $31, %%edx \n\t" //lps_mask
478 "subl %%ecx, %%esi \n\t" //RangeLPS - range
479 "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
480 "addl %%ecx, %%esi \n\t" //new range
481 "shl $17, %%ecx \n\t"
482 "andl %%edx, %%ecx \n\t"
483 "subl %%ecx, %%ebx \n\t"
484 "xorl %%edx, %0 \n\t"
485 #endif /* CMOV_IS_FAST */
487 //eax:state ebx:low edx:mask esi:range
489 //eax:bit ebx:low esi:range
491 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
492 "shll %%cl, %%esi \n\t"
493 "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx \n\t"
494 "movb %%dl, (%1) \n\t"
495 "movl %%esi, "RANGE "(%2) \n\t"
496 "shll %%cl, %%ebx \n\t"
497 "movl %%ebx, "LOW "(%2) \n\t"
498 "test %%bx, %%bx \n\t"
501 "movl "BYTE "(%2), %%ecx \n\t"
502 "movzwl (%%ecx), %%esi \n\t"
504 "shrl $15, %%esi \n\t"
505 "subl $0xFFFF, %%esi \n\t"
506 "addl $2, %%ecx \n\t"
507 "movl %%ecx, "BYTE "(%2) \n\t"
509 "leal -1(%%ebx), %%ecx \n\t"
510 "xorl %%ebx, %%ecx \n\t"
511 "shrl $15, %%ecx \n\t"
512 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
516 "shll %%cl , %%esi \n\t"
517 "addl %%esi, %%ebx \n\t"
518 "movl %%ebx, "LOW "(%2) \n\t"
522 : "%ecx", "%ebx", "%edx", "%esi", "memory"
525 #endif /* BRANCHLESS_CABAC_DECODER */
526 #else /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
528 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
529 int bit, lps_mask attribute_unused;
531 c->range -= RangeLPS;
532 #ifndef BRANCHLESS_CABAC_DECODER
533 if(c->low < (c->range<<17)){
535 *state= ff_h264_mps_state[s];
536 renorm_cabac_decoder_once(c);
538 bit= ff_h264_norm_shift[RangeLPS];
539 c->low -= (c->range<<17);
540 *state= ff_h264_lps_state[s];
541 c->range = RangeLPS<<bit;
545 if(!(c->low & 0xFFFF)){
549 #else /* BRANCHLESS_CABAC_DECODER */
550 lps_mask= ((c->range<<17) - c->low)>>31;
552 c->low -= (c->range<<17) & lps_mask;
553 c->range += (RangeLPS - c->range) & lps_mask;
556 *state= (ff_h264_mlps_state+128)[s];
559 lps_mask= ff_h264_norm_shift[c->range];
560 c->range<<= lps_mask;
562 if(!(c->low & CABAC_MASK))
564 #endif /* BRANCHLESS_CABAC_DECODER */
565 #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
569 static int __attribute((noinline)) get_cabac_noinline(CABACContext *c, uint8_t * const state){
570 return get_cabac_inline(c,state);
573 static int get_cabac(CABACContext *c, uint8_t * const state){
574 return get_cabac_inline(c,state);
577 static int get_cabac_bypass(CABACContext *c){
581 "movl "RANGE "(%1), %%ebx \n\t"
582 "movl "LOW "(%1), %%eax \n\t"
583 "shl $17, %%ebx \n\t"
584 "add %%eax, %%eax \n\t"
585 "sub %%ebx, %%eax \n\t"
587 "and %%edx, %%ebx \n\t"
588 "add %%ebx, %%eax \n\t"
589 "test %%ax, %%ax \n\t"
591 "movl "BYTE "(%1), %%ebx \n\t"
592 "subl $0xFFFF, %%eax \n\t"
593 "movzwl (%%ebx), %%ecx \n\t"
595 "shrl $15, %%ecx \n\t"
596 "addl $2, %%ebx \n\t"
597 "addl %%ecx, %%eax \n\t"
598 "movl %%ebx, "BYTE "(%1) \n\t"
600 "movl %%eax, "LOW "(%1) \n\t"
604 : "%eax", "%ebx", "%ecx", "memory"
611 if(!(c->low & CABAC_MASK))
625 static always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
628 "movl "RANGE "(%1), %%ebx \n\t"
629 "movl "LOW "(%1), %%eax \n\t"
630 "shl $17, %%ebx \n\t"
631 "add %%eax, %%eax \n\t"
632 "sub %%ebx, %%eax \n\t"
634 "and %%edx, %%ebx \n\t"
635 "add %%ebx, %%eax \n\t"
636 "xor %%edx, %%ecx \n\t"
637 "sub %%edx, %%ecx \n\t"
638 "test %%ax, %%ax \n\t"
640 "movl "BYTE "(%1), %%ebx \n\t"
641 "subl $0xFFFF, %%eax \n\t"
642 "movzwl (%%ebx), %%edx \n\t"
644 "shrl $15, %%edx \n\t"
645 "addl $2, %%ebx \n\t"
646 "addl %%edx, %%eax \n\t"
647 "movl %%ebx, "BYTE "(%1) \n\t"
649 "movl %%eax, "LOW "(%1) \n\t"
653 : "%eax", "%ebx", "%edx", "memory"
660 if(!(c->low & CABAC_MASK))
668 return (val^mask)-mask;
672 //FIXME the x86 code from this file should be moved into i386/h264 or cabac something.c/h (note ill kill you if you move my code away from under my fingers before iam finished with it!)
673 //FIXME use some macros to avoid duplicatin get_cabac (cant be done yet as that would make optimization work hard)
675 static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
676 void *end= significant_coeff_ctx_base + max_coeff - 1;
677 int minusstart= -(int)significant_coeff_ctx_base;
678 int minusindex= -(int)index;
681 "movl "RANGE "(%3), %%esi \n\t"
682 "movl "LOW "(%3), %%ebx \n\t"
686 "movzbl (%1), %0 \n\t"
687 "movl %%esi, %%edx \n\t"
688 "andl $0xC0, %%esi \n\t"
689 "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%esi, 2), %%esi\n\t"
690 /*eax:state ebx:low, edx:range, esi:RangeLPS*/
691 "subl %%esi, %%edx \n\t"
693 #if (defined CMOV_IS_FAST && __CPU__ >= 686)
694 "movl %%edx, %%ecx \n\t"
695 "shl $17, %%edx \n\t"
696 "cmpl %%ebx, %%edx \n\t"
697 "cmova %%ecx, %%esi \n\t"
698 "sbbl %%ecx, %%ecx \n\t"
699 "andl %%ecx, %%edx \n\t"
700 "subl %%edx, %%ebx \n\t"
701 "xorl %%ecx, %0 \n\t"
702 #else /* CMOV_IS_FAST */
703 "movl %%edx, %%ecx \n\t"
704 "shl $17, %%edx \n\t"
705 "subl %%ebx, %%edx \n\t"
706 "sarl $31, %%edx \n\t" //lps_mask
707 "subl %%ecx, %%esi \n\t" //RangeLPS - range
708 "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
709 "addl %%ecx, %%esi \n\t" //new range
710 "shl $17, %%ecx \n\t"
711 "andl %%edx, %%ecx \n\t"
712 "subl %%ecx, %%ebx \n\t"
713 "xorl %%edx, %0 \n\t"
714 #endif /* CMOV_IS_FAST */
716 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
717 "shll %%cl, %%esi \n\t"
718 "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx \n\t"
719 "movb %%dl, (%1) \n\t"
720 "shll %%cl, %%ebx \n\t"
721 "test %%bx, %%bx \n\t"
724 "movl "BYTE "(%3), %%ecx \n\t"
725 "movzwl (%%ecx), %%edx \n\t"
727 "shrl $15, %%edx \n\t"
728 "subl $0xFFFF, %%edx \n\t"
729 "addl $2, %%ecx \n\t"
730 "movl %%ecx, "BYTE "(%3) \n\t"
732 "leal -1(%%ebx), %%ecx \n\t"
733 "xorl %%ebx, %%ecx \n\t"
734 "shrl $15, %%ecx \n\t"
735 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
739 "shll %%cl , %%edx \n\t"
740 "addl %%edx, %%ebx \n\t"
746 "movl %2, %%eax \n\t"
747 "movl %4, %%ecx \n\t"
748 "addl %1, %%ecx \n\t"
749 "movl %%ecx, (%%eax) \n\t"
750 "addl $4, %%eax \n\t"
751 "movl %%eax, %2 \n\t"
753 "movzbl 61(%1), %0 \n\t"
754 "movl %%esi, %%edx \n\t"
755 "andl $0xC0, %%esi \n\t"
756 "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%esi, 2), %%esi\n\t"
757 /*eax:state ebx:low, edx:range, esi:RangeLPS*/
758 "subl %%esi, %%edx \n\t"
760 #if (defined CMOV_IS_FAST && __CPU__ >= 686)
761 "movl %%edx, %%ecx \n\t"
762 "shl $17, %%edx \n\t"
763 "cmpl %%ebx, %%edx \n\t"
764 "cmova %%ecx, %%esi \n\t"
765 "sbbl %%ecx, %%ecx \n\t"
766 "andl %%ecx, %%edx \n\t"
767 "subl %%edx, %%ebx \n\t"
768 "xorl %%ecx, %0 \n\t"
769 #else /* CMOV_IS_FAST */
770 "movl %%edx, %%ecx \n\t"
771 "shl $17, %%edx \n\t"
772 "subl %%ebx, %%edx \n\t"
773 "sarl $31, %%edx \n\t" //lps_mask
774 "subl %%ecx, %%esi \n\t" //RangeLPS - range
775 "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
776 "addl %%ecx, %%esi \n\t" //new range
777 "shl $17, %%ecx \n\t"
778 "andl %%edx, %%ecx \n\t"
779 "subl %%ecx, %%ebx \n\t"
780 "xorl %%edx, %0 \n\t"
781 #endif /* CMOV_IS_FAST */
783 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
784 "shll %%cl, %%esi \n\t"
785 "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx \n\t"
786 "movb %%dl, 61(%1) \n\t"
787 "shll %%cl, %%ebx \n\t"
788 "test %%bx, %%bx \n\t"
791 "movl "BYTE "(%3), %%ecx \n\t"
792 "movzwl (%%ecx), %%edx \n\t"
794 "shrl $15, %%edx \n\t"
795 "subl $0xFFFF, %%edx \n\t"
796 "addl $2, %%ecx \n\t"
797 "movl %%ecx, "BYTE "(%3) \n\t"
799 "leal -1(%%ebx), %%ecx \n\t"
800 "xorl %%ebx, %%ecx \n\t"
801 "shrl $15, %%ecx \n\t"
802 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
806 "shll %%cl , %%edx \n\t"
807 "addl %%edx, %%ebx \n\t"
810 "test $1, %%eax \n\t"
817 "movl %2, %%eax \n\t"
818 "movl %4, %%ecx \n\t"
819 "addl %1, %%ecx \n\t"
820 "movl %%ecx, (%%eax) \n\t"
821 "addl $4, %%eax \n\t"
822 "movl %%eax, %2 \n\t"
824 "movl %2, %%eax \n\t"
825 "addl %6, %%eax \n\t"
828 "movl %%esi, "RANGE "(%3) \n\t"
829 "movl %%ebx, "LOW "(%3) \n\t"
830 :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
831 :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
832 : "%ecx", "%ebx", "%edx", "%esi", "memory"\
840 * @return the number of bytes read or 0 if no end
842 static int get_cabac_terminate(CABACContext *c){
844 if(c->low < c->range<<17){
845 renorm_cabac_decoder_once(c);
848 return c->bytestream - c->bytestream_start;
853 * get (truncated) unnary binarization.
855 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
858 for(i=0; i<max; i++){
859 if(get_cabac(c, state)==0)
862 if(i< max_index) state++;
865 return truncated ? max : -1;
869 * get unary exp golomb k-th order binarization.
871 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
875 if(get_cabac(c, state)==0)
878 if(0 < max_index) state++;
880 for(i=1; i<max; i++){
881 if(get_cabac(c, state)==0){
882 if(is_signed && get_cabac_bypass(c)){
888 if(i < max_index) state++;
891 while(get_cabac_bypass(c)){
898 v+= v + get_cabac_bypass(c);
902 if(is_signed && get_cabac_bypass(c)){