shift CABACContext.range right, this reduces the number of shifts needed in get_cabac...
authorMichael Niedermayer <michaelni@gmx.at>
Sun, 15 Oct 2006 20:40:50 +0000 (20:40 +0000)
committerMichael Niedermayer <michaelni@gmx.at>
Sun, 15 Oct 2006 20:40:50 +0000 (20:40 +0000)
Originally committed as revision 6702 to svn://svn.ffmpeg.org/ffmpeg/trunk

libavcodec/cabac.c
libavcodec/cabac.h

index 0cd5144..91db6c3 100644 (file)
@@ -51,7 +51,7 @@ static const uint8_t lps_range[64][4]= {
 };
 
 uint8_t ff_h264_mlps_state[4*64];
-uint8_t ff_h264_lps_range[2*65][4];
+uint8_t ff_h264_lps_range[4][2*64];
 uint8_t ff_h264_lps_state[2*64];
 uint8_t ff_h264_mps_state[2*64];
 
@@ -76,8 +76,8 @@ static const uint8_t lps_state[64]= {
  33,33,34,34,35,35,35,36,
  36,36,37,37,37,38,38,63,
 };
-
-const uint8_t ff_h264_norm_shift[128]= {
+#if 0
+const uint8_t ff_h264_norm_shift_old[128]= {
  7,6,5,5,4,4,4,4,3,3,3,3,3,3,3,3,
  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
@@ -87,6 +87,29 @@ const uint8_t ff_h264_norm_shift[128]= {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 };
+#endif
+const uint8_t ff_h264_norm_shift[512]= {
+ 9,8,7,7,6,6,6,6,5,5,5,5,5,5,5,5,
+ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+};
 
 /**
  *
@@ -121,7 +144,7 @@ void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
     c->low =  (*c->bytestream++)<<10;
 #endif
     c->low+= ((*c->bytestream++)<<2) + 2;
-    c->range= 0x1FE<<(CABAC_BITS + 1);
+    c->range= 0x1FE;
 }
 
 void ff_init_cabac_states(CABACContext *c){
@@ -129,8 +152,8 @@ void ff_init_cabac_states(CABACContext *c){
 
     for(i=0; i<64; i++){
         for(j=0; j<4; j++){ //FIXME check if this is worth the 1 shift we save
-            ff_h264_lps_range[2*i+0][j+4]=
-            ff_h264_lps_range[2*i+1][j+4]= lps_range[i][j];
+            ff_h264_lps_range[j][2*i+0]=
+            ff_h264_lps_range[j][2*i+1]= lps_range[i][j];
         }
 
         ff_h264_mlps_state[128+2*i+0]=
index c07d07b..dc114b4 100644 (file)
@@ -48,10 +48,10 @@ typedef struct CABACContext{
 }CABACContext;
 
 extern uint8_t ff_h264_mlps_state[4*64];
-extern uint8_t ff_h264_lps_range[2*65][4];  ///< rangeTabLPS
+extern uint8_t ff_h264_lps_range[4][2*64];  ///< rangeTabLPS
 extern uint8_t ff_h264_mps_state[2*64];     ///< transIdxMPS
 extern uint8_t ff_h264_lps_state[2*64];     ///< transIdxLPS
-extern const uint8_t ff_h264_norm_shift[128];
+extern const uint8_t ff_h264_norm_shift[512];
 
 
 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
@@ -85,7 +85,7 @@ static inline void renorm_cabac_encoder(CABACContext *c){
 }
 
 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
-    int RangeLPS= ff_h264_lps_range[*state][c->range>>6];
+    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
 
     if(bit == ((*state)&1)){
         c->range -= RangeLPS;
@@ -268,7 +268,7 @@ static void refill2(CABACContext *c){
     int i, x;
 
     x= c->low ^ (c->low-1);
-    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
+    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
 
     x= -CABAC_MASK;
 
@@ -283,7 +283,7 @@ static void refill2(CABACContext *c){
 }
 
 static inline void renorm_cabac_decoder(CABACContext *c){
-    while(c->range < (0x200 << CABAC_BITS)){
+    while(c->range < 0x100){
         c->range+= c->range;
         c->low+= c->low;
         if(!(c->low & CABAC_MASK))
@@ -297,7 +297,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
 #if 0
     //P3:683    athlon:475
     asm(
-        "lea -0x2000000(%0), %2     \n\t"
+        "lea -0x100(%0), %2         \n\t"
         "shr $31, %2                \n\t"  //FIXME 31->63 for x86-64
         "shl %%cl, %0               \n\t"
         "shl %%cl, %1               \n\t"
@@ -306,7 +306,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
 #elif 0
     //P3:680    athlon:474
     asm(
-        "cmp $0x2000000, %0         \n\t"
+        "cmp $0x100, %0             \n\t"
         "setb %%cl                  \n\t"  //FIXME 31->63 for x86-64
         "shl %%cl, %0               \n\t"
         "shl %%cl, %1               \n\t"
@@ -316,7 +316,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
     int temp2;
     //P3:665    athlon:517
     asm(
-        "lea -0x2000000(%0), %%eax  \n\t"
+        "lea -0x100(%0), %%eax      \n\t"
         "cdq                        \n\t"
         "mov %0, %%eax              \n\t"
         "and %%edx, %0              \n\t"
@@ -329,7 +329,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
     int temp2;
     //P3:673    athlon:509
     asm(
-        "cmp $0x2000000, %0         \n\t"
+        "cmp $0x100, %0             \n\t"
         "sbb %%edx, %%edx           \n\t"
         "mov %0, %%eax              \n\t"
         "and %%edx, %0              \n\t"
@@ -342,7 +342,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
     int temp2;
     //P3:677    athlon:511
     asm(
-        "cmp $0x2000000, %0         \n\t"
+        "cmp $0x100, %0             \n\t"
         "lea (%0, %0), %%eax        \n\t"
         "lea (%1, %1), %%edx        \n\t"
         "cmovb %%eax, %0            \n\t"
@@ -352,7 +352,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
 #endif
 #else
     //P3:675    athlon:476
-    int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
+    int shift= (uint32_t)(c->range - 0x100)>>31;
     c->range<<= shift;
     c->low  <<= shift;
 #endif
@@ -375,24 +375,25 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
         "movzbl (%1), %%eax                     \n\t"
         "movl "RANGE    "(%2), %%ebx            \n\t"
         "movl "RANGE    "(%2), %%edx            \n\t"
-        "shrl $23, %%ebx                        \n\t"
-        "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
-        "shll $17, %%esi                        \n\t"
+        "andl $0xC0, %%ebx                      \n\t"
+        "movzbl "MANGLE(ff_h264_lps_range)"(%%eax, %%ebx, 2), %%esi\n\t"
         "movl "LOW      "(%2), %%ebx            \n\t"
 //eax:state ebx:low, edx:range, esi:RangeLPS
         "subl %%esi, %%edx                      \n\t"
-        "cmpl %%edx, %%ebx                      \n\t"
+        "movl %%edx, %%ecx                      \n\t"
+        "shll $17, %%ecx                        \n\t"
+        "cmpl %%ecx, %%ebx                      \n\t"
         " ja 1f                                 \n\t"
 
 #if 1
         //athlon:4067 P3:4110
-        "lea -0x2000000(%%edx), %%ecx           \n\t"
+        "lea -0x100(%%edx), %%ecx               \n\t"
         "shr $31, %%ecx                         \n\t"
         "shl %%cl, %%edx                        \n\t"
         "shl %%cl, %%ebx                        \n\t"
 #else
         //athlon:4057 P3:4130
-        "cmp $0x2000000, %%edx                  \n\t" //FIXME avoidable
+        "cmp $0x100, %%edx                      \n\t" //FIXME avoidable
         "setb %%cl                              \n\t"
         "shl %%cl, %%edx                        \n\t"
         "shl %%cl, %%ebx                        \n\t"
@@ -413,14 +414,13 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
         "jmp 2f                                 \n\t"
         "1:                                     \n\t"
 //eax:state ebx:low, edx:range, esi:RangeLPS
-        "subl %%edx, %%ebx                      \n\t"
+        "subl %%ecx, %%ebx                      \n\t"
         "movl %%esi, %%edx                      \n\t"
-        "shr $19, %%esi                         \n\t"
-        "movzbl "MANGLE(ff_h264_lps_state)"(%%eax), %%ecx   \n\t"
-        "movb %%cl, (%1)                        \n\t"
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
         "shll %%cl, %%ebx                       \n\t"
         "shll %%cl, %%edx                       \n\t"
+        "movzbl "MANGLE(ff_h264_lps_state)"(%%eax), %%ecx   \n\t"
+        "movb %%cl, (%1)                        \n\t"
         "addl $1, %%eax                         \n\t"
         "test %%bx, %%bx                        \n\t"
         " jnz 2f                                \n\t"
@@ -435,7 +435,7 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
 
         "leal -1(%%ebx), %%ecx                  \n\t"
         "xorl %%ebx, %%ecx                      \n\t"
-        "shrl $17, %%ecx                        \n\t"
+        "shrl $15, %%ecx                        \n\t"
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
         "neg %%ecx                              \n\t"
         "add $7, %%ecx                          \n\t"
@@ -455,20 +455,22 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
         "movzbl (%1), %%eax                     \n\t"
         "movl "RANGE    "(%2), %%ebx            \n\t"
         "movl "RANGE    "(%2), %%edx            \n\t"
-        "shrl $23, %%ebx                        \n\t"
-        "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
-        "shll $17, %%esi                        \n\t"
+        "andl $0xC0, %%ebx                      \n\t"
+        "movzbl "MANGLE(ff_h264_lps_range)"(%%eax, %%ebx, 2), %%esi\n\t"
         "movl "LOW      "(%2), %%ebx            \n\t"
 //eax:state ebx:low, edx:range, esi:RangeLPS
         "subl %%esi, %%edx                      \n\t"
-#ifdef CMOV_IS_FAST //FIXME actually define this somewhere
+#ifdef CMOV_IS_FAST
+        "movl %%edx, %%ecx                      \n\t"
+        "shl $17, %%edx                         \n\t"
         "cmpl %%ebx, %%edx                      \n\t"
-        "cmova %%edx, %%esi                     \n\t"
+        "cmova %%ecx, %%esi                     \n\t"
         "sbbl %%ecx, %%ecx                      \n\t"
         "andl %%ecx, %%edx                      \n\t"
         "subl %%edx, %%ebx                      \n\t"
         "xorl %%ecx, %%eax                      \n\t"
 #else /* CMOV_IS_FAST */
+FIXTHIS
         "movl %%edx, %%ecx                      \n\t"
         "subl %%ebx, %%edx                      \n\t"
         "sarl $31, %%edx                        \n\t" //lps_mask
@@ -481,16 +483,14 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
 #endif /* CMOV_IS_FAST */
 
 //eax:state ebx:low edx:mask esi:range
-        "movzbl "MANGLE(ff_h264_mlps_state)"+128(%%eax), %%ecx   \n\t"
-        "movb %%cl, (%1)                        \n\t"
 
-        "movl %%esi, %%edx                      \n\t"
-//eax:bit ebx:low edx:range esi:range
+//eax:bit ebx:low esi:range
 
-        "shr $19, %%esi                         \n\t"
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
-        "shll %%cl, %%edx                       \n\t"
-        "movl %%edx, "RANGE    "(%2)            \n\t"
+        "shll %%cl, %%esi                       \n\t"
+        "movzbl "MANGLE(ff_h264_mlps_state)"+128(%%eax), %%edx   \n\t"
+        "movb %%dl, (%1)                        \n\t"
+        "movl %%esi, "RANGE    "(%2)            \n\t"
         "shll %%cl, %%ebx                       \n\t"
         "movl %%ebx, "LOW      "(%2)            \n\t"
         "test %%bx, %%bx                        \n\t"
@@ -506,7 +506,7 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
 
         "leal -1(%%ebx), %%ecx                  \n\t"
         "xorl %%ebx, %%ecx                      \n\t"
-        "shrl $17, %%ecx                        \n\t"
+        "shrl $15, %%ecx                        \n\t"
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
         "neg %%ecx                              \n\t"
         "add $7, %%ecx                          \n\t"
@@ -523,18 +523,18 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
 #endif /* BRANCHLESS_CABAC_DECODER */
 #else /* ARCH_X86 */
     int s = *state;
-    int RangeLPS= ff_h264_lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
+    int RangeLPS= ff_h264_lps_range[0][2*(c->range&0xC0) + s];
     int bit, lps_mask attribute_unused;
 
     c->range -= RangeLPS;
 #ifndef BRANCHLESS_CABAC_DECODER
-    if(c->low < c->range){
+    if(c->low < (c->range<<17)){
         bit= s&1;
         *state= ff_h264_mps_state[s];
         renorm_cabac_decoder_once(c);
     }else{
-        bit= ff_h264_norm_shift[RangeLPS>>19];
-        c->low -= c->range;
+        bit= ff_h264_norm_shift[RangeLPS];
+        c->low -= (c->range<<17);
         *state= ff_h264_lps_state[s];
         c->range = RangeLPS<<bit;
         c->low <<= bit;
@@ -545,16 +545,16 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
         }
     }
 #else /* BRANCHLESS_CABAC_DECODER */
-    lps_mask= (c->range - c->low)>>31;
+    lps_mask= ((c->range<<17) - c->low)>>31;
 
-    c->low -= c->range & lps_mask;
+    c->low -= (c->range<<17) & lps_mask;
     c->range += (RangeLPS - c->range) & lps_mask;
 
     s^=lps_mask;
     *state= (ff_h264_mlps_state+128)[s];
     bit= s&1;
 
-    lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+3)];
+    lps_mask= ff_h264_norm_shift[c->range];
     c->range<<= lps_mask;
     c->low  <<= lps_mask;
     if(!(c->low & CABAC_MASK))
@@ -573,15 +573,17 @@ static int get_cabac(CABACContext *c, uint8_t * const state){
 }
 
 static int get_cabac_bypass(CABACContext *c){
+    int range;
     c->low += c->low;
 
     if(!(c->low & CABAC_MASK))
         refill(c);
 
-    if(c->low < c->range){
+    range= c->range<<17;
+    if(c->low < range){
         return 0;
     }else{
-        c->low -= c->range;
+        c->low -= range;
         return 1;
     }
 }
@@ -591,8 +593,8 @@ static int get_cabac_bypass(CABACContext *c){
  * @return the number of bytes read or 0 if no end
  */
 static int get_cabac_terminate(CABACContext *c){
-    c->range -= 4<<CABAC_BITS;
-    if(c->low < c->range){
+    c->range -= 2;
+    if(c->low < c->range<<17){
         renorm_cabac_decoder_once(c);
         return 0;
     }else{