]> git.sesse.net Git - x264/blobdiff - common/bs.h
Change priority handling on some OSs
[x264] / common / bs.h
index 9882c23c11319d97edbb1dc03245a389dd2da7ef..0c009921a1200c2efa4e8b5eb8e377bb2b7db837 100644 (file)
@@ -50,6 +50,13 @@ typedef struct bs_s
     int     i_bits_encoded; /* RD only */
 } bs_t;
 
+typedef struct
+{
+    int     last;
+    int16_t level[16];
+    uint8_t run[16];
+} x264_run_level_t;
+
 extern const vlc_t x264_coeff0_token[5];
 extern const vlc_t x264_coeff_token[5][16*4];
 extern const vlc_t x264_total_zeros[15][16];
@@ -66,21 +73,22 @@ extern vlc_large_t x264_level_token[7][LEVEL_TABLE_SIZE];
 
 static inline void bs_init( bs_t *s, void *p_data, int i_data )
 {
-    int offset = ((intptr_t)p_data & (WORD_SIZE-1));
+    int offset = ((intptr_t)p_data & 3);
     s->p       = s->p_start = (uint8_t*)p_data - offset;
     s->p_end   = (uint8_t*)p_data + i_data;
-    s->i_left  = offset ? 8*offset : (WORD_SIZE*8);
-    s->cur_bits = endian_fix( *(intptr_t*)s->p );
+    s->i_left  = (WORD_SIZE - offset)*8;
+    s->cur_bits = endian_fix32(*(uint32_t *)(s->p));
+    s->cur_bits >>= (4-offset)*8;
 }
 static inline int bs_pos( bs_t *s )
 {
     return( 8 * (s->p - s->p_start) + (WORD_SIZE*8) - s->i_left );
 }
 
-/* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32/64-bit aligned. */
+/* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32-bit aligned. */
 static inline void bs_flush( bs_t *s )
 {
-    *(intptr_t*)s->p = endian_fix( s->cur_bits << s->i_left );
+    *(uint32_t*)s->p = endian_fix32( s->cur_bits << (s->i_left&31) );
     s->p += WORD_SIZE - s->i_left / 8;
     s->i_left = WORD_SIZE*8;
 }
@@ -144,21 +152,12 @@ static inline void bs_write1( bs_t *s, uint32_t i_bit )
 
 static inline void bs_align_0( bs_t *s )
 {
-    if( s->i_left&7 )
-    {
-        s->cur_bits <<= s->i_left&7;
-        s->i_left &= ~7;
-    }
+    bs_write( s, s->i_left&7, 0 );
     bs_flush( s );
 }
 static inline void bs_align_1( bs_t *s )
 {
-    if( s->i_left&7 )
-    {
-        s->cur_bits <<= s->i_left&7;
-        s->cur_bits |= (1 << (s->i_left&7)) - 1;
-        s->i_left &= ~7;
-    }
+    bs_write( s, s->i_left&7, (1 << (s->i_left&7)) - 1 );
     bs_flush( s );
 }
 
@@ -212,7 +211,12 @@ static inline void bs_write_ue( bs_t *s, int val )
 static inline void bs_write_se( bs_t *s, int val )
 {
     int size = 0;
-    int tmp = val = val <= 0 ? -val*2+1 : val*2;
+    /* Faster than (val <= 0 ? -val*2+1 : val*2) */
+    /* 4 instructions on x86, 3 on ARM */
+    int tmp = 1 - val*2;
+    if( tmp < 0 ) tmp = val*2;
+    val = tmp;
+
     if( tmp >= 0x100 )
     {
         size = 16;
@@ -226,14 +230,14 @@ static inline void bs_write_te( bs_t *s, int x, int val )
 {
     if( x == 1 )
         bs_write1( s, 1^val );
-    else if( x > 1 )
+    else //if( x > 1 )
         bs_write_ue( s, val );
 }
 
 static inline void bs_rbsp_trailing( bs_t *s )
 {
     bs_write1( s, 1 );
-    bs_flush( s );
+    bs_write( s, s->i_left&7, 0  );
 }
 
 static inline int bs_size_ue( unsigned int val )
@@ -251,17 +255,20 @@ static inline int bs_size_ue_big( unsigned int val )
 
 static inline int bs_size_se( int val )
 {
-    return bs_size_ue_big( val <= 0 ? -val*2 : val*2-1 );
+    int tmp = 1 - val*2;
+    if( tmp < 0 ) tmp = val*2;
+    if( tmp < 256 )
+        return x264_ue_size_tab[tmp];
+    else
+        return x264_ue_size_tab[tmp>>8]+16;
 }
 
 static inline int bs_size_te( int x, int val )
 {
     if( x == 1 )
         return 1;
-    else if( x > 1 )
+    else //if( x > 1 )
         return x264_ue_size_tab[val+1];
-    else
-        return 0;
 }
 
 #endif