# HG changeset patch # User michael # Date 1160944850 0 # Node ID d550343b5dacc7ef6c542656b3e171ca0733e7be # Parent 508f089d0b28a66e40898e3da3ac5363574c2001 shift CABACContext.range right, this reduces the number of shifts needed in get_cabac() and is slightly faster on P3 (and should be much faster on P4 as the P4 except the more recent variants lacks an integer shifter and so shifts have ~10 times longer latency then simple operations like adds) diff -r 508f089d0b28 -r d550343b5dac cabac.c --- a/cabac.c Sun Oct 15 16:52:33 2006 +0000 +++ b/cabac.c Sun Oct 15 20:40:50 2006 +0000 @@ -51,7 +51,7 @@ }; 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 @@ 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 @@ 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 @@ 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 @@ 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]= diff -r 508f089d0b28 -r d550343b5dac cabac.h --- a/cabac.h Sun Oct 15 16:52:33 2006 +0000 +++ b/cabac.h Sun Oct 15 20:40:50 2006 +0000 @@ -48,10 +48,10 @@ }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 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 @@ 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 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 @@ #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 @@ #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 @@ 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 @@ 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 @@ 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 @@ #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 @@ "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 @@ "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 @@ "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 @@ "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 @@ #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" + +//eax:bit ebx:low esi:range - "movl %%esi, %%edx \n\t" -//eax:bit ebx:low edx:range 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 @@ "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 @@ #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<low <<= bit; @@ -545,16 +545,16 @@ } } #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_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 @@ * @return the number of bytes read or 0 if no end */ static int get_cabac_terminate(CABACContext *c){ - c->range -= 4<low < c->range){ + c->range -= 2; + if(c->low < c->range<<17){ renorm_cabac_decoder_once(c); return 0; }else{