[vlc-commits] vlc_bits: add pf_forward

Francois Cartegnie git at videolan.org
Thu Dec 17 15:39:28 CET 2015


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Tue Dec 15 17:14:54 2015 +0100| [f42374732c71bd038ba86bb5089d132ea3ae93bb] | committer: Francois Cartegnie

vlc_bits: add pf_forward

will allow to modify/convert upstream

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=f42374732c71bd038ba86bb5089d132ea3ae93bb
---

 include/vlc_bits.h   |   20 +++++++++++++++-----
 test/src/misc/bits.c |   21 +++++++++++++++++++++
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/include/vlc_bits.h b/include/vlc_bits.h
index 379cf38..451b317 100644
--- a/include/vlc_bits.h
+++ b/include/vlc_bits.h
@@ -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
diff --git a/test/src/misc/bits.c b/test/src/misc/bits.c
index 17b8335..104d61b 100644
--- a/test/src/misc/bits.c
+++ b/test/src/misc/bits.c
@@ -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;
 }



More information about the vlc-commits mailing list