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( M32(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 );
+ M32( 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;
}
+/* The inverse of bs_flush: prepare the bitstream to be written to again. */
+static inline void bs_realign( bs_t *s )
+{
+ int offset = ((intptr_t)s->p & 3);
+ if( offset )
+ {
+ s->p = (uint8_t*)s->p - offset;
+ s->i_left = (WORD_SIZE - offset)*8;
+ s->cur_bits = endian_fix32( M32(s->p) );
+ s->cur_bits >>= (4-offset)*8;
+ }
+}
static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
{
if( s->i_left <= 32 )
{
#ifdef WORDS_BIGENDIAN
- *(uint32_t*)s->p = s->cur_bits >> (32 - s->i_left);
+ M32( s->p ) = s->cur_bits >> (32 - s->i_left);
#else
- *(uint32_t*)s->p = endian_fix( s->cur_bits << s->i_left );
+ M32( s->p ) = endian_fix( s->cur_bits << s->i_left );
#endif
s->i_left += 32;
s->p += 4;
{
i_count -= s->i_left;
s->cur_bits = (s->cur_bits << s->i_left) | (i_bits >> i_count);
- *(uint32_t*)s->p = endian_fix( s->cur_bits );
+ M32( s->p ) = endian_fix( s->cur_bits );
s->p += 4;
s->cur_bits = i_bits;
s->i_left = 32 - i_count;
s->i_left--;
if( s->i_left == WORD_SIZE*8-32 )
{
- *(uint32_t*)s->p = endian_fix32( s->cur_bits );
+ M32( s->p ) = endian_fix32( s->cur_bits );
s->p += 4;
s->i_left = WORD_SIZE*8;
}
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 );
}
+static inline void bs_align_10( bs_t *s )
+{
+ if( s->i_left&7 )
+ bs_write( s, s->i_left&7, 1 << ( (s->i_left&7) - 1 ) );
+}
/* golomb functions */
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;
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 )
+static ALWAYS_INLINE int bs_size_ue( unsigned int val )
{
return x264_ue_size_tab[val+1];
}
-static inline int bs_size_ue_big( unsigned int val )
+static ALWAYS_INLINE int bs_size_ue_big( unsigned int val )
{
if( val < 255 )
return x264_ue_size_tab[val+1];
return x264_ue_size_tab[(val+1)>>8] + 16;
}
-static inline int bs_size_se( int val )
+static ALWAYS_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 )
+static ALWAYS_INLINE int bs_size_te( int x, int val )
{
if( x == 1 )
return 1;