Commit f4237473 authored by François Cartegnie's avatar François Cartegnie 🤞

vlc_bits: add pf_forward

will allow to modify/convert upstream
parent f91c968b
......@@ -40,6 +40,10 @@ typedef struct bs_s
ssize_t i_left; /* i_count number of available bits */
bool b_read_only;
/* forward read modifier (p_start, p_end, p_fwpriv, count) */
uint8_t *(*pf_forward)(uint8_t *, uint8_t *, void *, size_t);
void *p_fwpriv;
} bs_t;
static inline void bs_write_init( bs_t *s, void *p_data, size_t i_data )
......@@ -49,6 +53,7 @@ static inline void bs_write_init( bs_t *s, void *p_data, size_t i_data )
s->p_end = s->p_start + i_data;
s->i_left = 8;
s->b_read_only = false;
s->pf_forward = s->p_fwpriv = NULL;
}
static inline void bs_init( bs_t *s, const void *p_data, size_t i_data )
......@@ -75,6 +80,9 @@ static inline int bs_eof( const bs_t *s )
return( s->p >= s->p_end ? 1: 0 );
}
#define bs_forward( s, i ) \
s->p = s->pf_forward ? s->pf_forward( s->p, s->p_end, s->p_fwpriv, i ) : s->p + i
static inline uint32_t bs_read( bs_t *s, int i_count )
{
static const uint32_t i_mask[33] =
......@@ -104,7 +112,7 @@ static inline uint32_t bs_read( bs_t *s, int i_count )
s->i_left -= i_count;
if( s->i_left == 0 )
{
s->p++;
bs_forward( s, 1 );
s->i_left = 8;
}
return( i_result );
......@@ -114,7 +122,7 @@ static inline uint32_t bs_read( bs_t *s, int i_count )
/* less in the buffer than requested */
i_result |= (*s->p&i_mask[s->i_left]) << -i_shr;
i_count -= s->i_left;
s->p++;
bs_forward( s, 1);
s->i_left = 8;
}
}
......@@ -132,7 +140,7 @@ static inline uint32_t bs_read1( bs_t *s )
i_result = ( *s->p >> s->i_left )&0x01;
if( s->i_left == 0 )
{
s->p++;
bs_forward( s, 1 );
s->i_left = 8;
}
return i_result;
......@@ -155,7 +163,7 @@ static inline void bs_skip( bs_t *s, ssize_t i_count )
{
const int i_bytes = ( -s->i_left + 8 ) / 8;
s->p += i_bytes;
bs_forward( s, i_bytes );
s->i_left += 8 * i_bytes;
}
}
......@@ -185,7 +193,7 @@ static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
s->i_left--;
if( s->i_left == 0 )
{
s->p++;
bs_forward( s, 1 );
s->i_left = 8;
}
}
......@@ -235,4 +243,6 @@ static inline int32_t bs_read_se( bs_t *s )
return val&0x01 ? (val+1)/2 : -(val/2);
}
#undef bs_forward
#endif
......@@ -25,6 +25,18 @@
#include <vlc_bits.h>
#include <assert.h>
static uint8_t *skip1( uint8_t *p, uint8_t *end, void *priv, size_t i_count )
{
(void) priv;
for( size_t i=0; i<i_count; i++ )
{
p += 2;
if( p >= end )
return p;
}
return p;
}
int main( void )
{
test_init();
......@@ -106,5 +118,14 @@ int main( void )
assert( bs_read( &bs, 8 ) == 0xEE );
assert( bs_remain( &bs ) == 8 );
/* Check forwarding by correctly decoding a 1 byte skip sequence */
const uint8_t ok[6] = { 0xAA, 0xCC, 0xEE, /* ovfw fillers */ 0, 0, 0 };
uint8_t work[6] = { 0 };
bs_init( &bs, &abc, 6 );
bs.pf_forward = skip1;
for( unsigned i=0; i<6 && !bs_eof( &bs ); i++ )
work[i] = bs_read( &bs, 8 );
assert(!memcmp( &work, &ok, 6 ));
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment