[vlc-commits] vlc_block_helper: simplify using byte and offset accounting

Francois Cartegnie git at videolan.org
Wed Feb 8 19:09:48 CET 2017


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed Feb  8 15:07:57 2017 +0100| [1af7965c2f99955808460eb0d24cf6fa12d33298] | committer: Francois Cartegnie

vlc_block_helper: simplify using byte and offset accounting

also removes unused helpers

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

 include/vlc_block_helper.h             | 287 ++++++++-------------------------
 modules/packetizer/dirac.c             |   2 +-
 modules/packetizer/packetizer_helper.h |   2 +-
 3 files changed, 65 insertions(+), 226 deletions(-)

diff --git a/include/vlc_block_helper.h b/include/vlc_block_helper.h
index 9adc798..0e9e6bf 100644
--- a/include/vlc_block_helper.h
+++ b/include/vlc_block_helper.h
@@ -1,8 +1,7 @@
 /*****************************************************************************
  * vlc_block_helper.h: Helper functions for data blocks management.
  *****************************************************************************
- * Copyright (C) 2003 VLC authors and VideoLAN
- * $Id$
+ * Copyright (C) 2003-2017 VLC authors and VideoLAN
  *
  * Authors: Gildas Bazin <gbazin at netcourrier.com>
  *
@@ -31,7 +30,9 @@ typedef struct block_bytestream_t
     block_t *p_chain;  /**< byte stream head block */
     block_t **pp_last; /**< tail ppointer for appends */
     block_t *p_block;  /**< byte stream read pointer block */
-    size_t   i_offset; /**< byte stream read pointer offset within block */
+    size_t   i_block_offset; /**< byte stream read pointer offset within block */
+    size_t   i_base_offset; /**< block base offset (previous blocks total size) */
+    size_t   i_total;  /**< total bytes over all linked blocks */
 } block_bytestream_t;
 
 /*****************************************************************************
@@ -41,7 +42,9 @@ static inline void block_BytestreamInit( block_bytestream_t *p_bytestream )
 {
     p_bytestream->p_chain = p_bytestream->p_block = NULL;
     p_bytestream->pp_last = &p_bytestream->p_chain;
-    p_bytestream->i_offset = 0;
+    p_bytestream->i_block_offset = 0;
+    p_bytestream->i_base_offset = 0;
+    p_bytestream->i_total = 0;
 }
 
 static inline void block_BytestreamRelease( block_bytestream_t *p_bytestream )
@@ -69,17 +72,20 @@ static inline void block_BytestreamFlush( block_bytestream_t *p_bytestream )
     {
         block_t *p_next = block->p_next;
 
+        p_bytestream->i_total -= block->i_buffer;
+        p_bytestream->i_base_offset -= block->i_buffer;
         block_Release( block );
         block = p_next;
     }
 
-    while( block != NULL && block->i_buffer == p_bytestream->i_offset )
+    while( block != NULL && block->i_buffer == p_bytestream->i_block_offset )
     {
         block_t *p_next = block->p_next;
 
+        p_bytestream->i_total -= block->i_buffer;
         block_Release( block );
         block = p_next;
-        p_bytestream->i_offset = 0;
+        p_bytestream->i_block_offset = 0;
     }
 
     p_bytestream->p_chain = p_bytestream->p_block = block;
@@ -92,6 +98,14 @@ static inline void block_BytestreamPush( block_bytestream_t *p_bytestream,
 {
     block_ChainLastAppend( &p_bytestream->pp_last, p_block );
     if( !p_bytestream->p_block ) p_bytestream->p_block = p_block;
+    for( ; p_block; p_block = p_block->p_next )
+        p_bytestream->i_total += p_block->i_buffer;
+}
+
+static inline size_t block_BytestreamRemaining( const block_bytestream_t *p_bytestream )
+{
+    return ( p_bytestream->i_total > p_bytestream->i_base_offset + p_bytestream->i_block_offset ) ?
+             p_bytestream->i_total - p_bytestream->i_base_offset - p_bytestream->i_block_offset : 0;
 }
 
 VLC_USED
@@ -108,9 +122,10 @@ static inline block_t *block_BytestreamPop( block_bytestream_t *p_bytestream )
     }
     else if( !p_block->p_next )
     {
-        p_block->p_buffer += p_bytestream->i_offset;
-        p_block->i_buffer -= p_bytestream->i_offset;
-        p_bytestream->i_offset = 0;
+        p_block->p_buffer += p_bytestream->i_block_offset;
+        p_block->i_buffer -= p_bytestream->i_block_offset;
+        p_bytestream->i_block_offset = 0;
+        p_bytestream->i_total = 0;
         p_bytestream->p_chain = p_bytestream->p_block = NULL;
         p_bytestream->pp_last = &p_bytestream->p_chain;
         return p_block;
@@ -123,190 +138,32 @@ static inline block_t *block_BytestreamPop( block_bytestream_t *p_bytestream )
     p_block = p_block->p_next;
     p_block_old->p_next = NULL;
     p_bytestream->pp_last = &p_block_old->p_next;
+    p_bytestream->i_total -= p_block->i_buffer;
 
     return p_block;
 }
 
-static inline int block_SkipByte( block_bytestream_t *p_bytestream )
-{
-    /* Most common case first */
-    if( likely( p_bytestream->p_block->i_buffer - p_bytestream->i_offset ) )
-    {
-        p_bytestream->i_offset++;
-        return VLC_SUCCESS;
-    }
-    else
-    {
-        block_t *p_block;
-
-        /* Less common case which is also slower */
-        for( p_block = p_bytestream->p_block->p_next;
-             p_block != NULL; p_block = p_block->p_next )
-        {
-            if( p_block->i_buffer )
-            {
-                p_bytestream->i_offset = 1;
-                p_bytestream->p_block = p_block;
-                return VLC_SUCCESS;
-            }
-        }
-    }
-
-    /* Not enough data, bail out */
-    return VLC_EGENERIC;
-}
-
-static inline int block_PeekByte( block_bytestream_t *p_bytestream,
-                                  uint8_t *p_data )
-{
-    /* Most common case first */
-    if( likely( p_bytestream->p_block->i_buffer - p_bytestream->i_offset ) )
-    {
-        *p_data = p_bytestream->p_block->p_buffer[p_bytestream->i_offset];
-        return VLC_SUCCESS;
-    }
-    else
-    {
-        block_t *p_block;
-
-        /* Less common case which is also slower */
-        for( p_block = p_bytestream->p_block->p_next;
-             p_block != NULL; p_block = p_block->p_next )
-        {
-            if( p_block->i_buffer )
-            {
-                *p_data = p_block->p_buffer[0];
-                return VLC_SUCCESS;
-            }
-        }
-    }
-
-    /* Not enough data, bail out */
-    return VLC_EGENERIC;
-}
-
-static inline int block_GetByte( block_bytestream_t *p_bytestream,
-                                 uint8_t *p_data )
-{
-    /* Most common case first */
-    if( likely( p_bytestream->p_block->i_buffer - p_bytestream->i_offset ) )
-    {
-        *p_data = p_bytestream->p_block->p_buffer[p_bytestream->i_offset];
-        p_bytestream->i_offset++;
-        return VLC_SUCCESS;
-    }
-    else
-    {
-        block_t *p_block;
-
-        /* Less common case which is also slower */
-        for( p_block = p_bytestream->p_block->p_next;
-             p_block != NULL; p_block = p_block->p_next )
-        {
-            if( p_block->i_buffer )
-            {
-                *p_data = p_block->p_buffer[0];
-                p_bytestream->i_offset = 1;
-                p_bytestream->p_block = p_block;
-                return VLC_SUCCESS;
-            }
-        }
-    }
-
-    /* Not enough data, bail out */
-    return VLC_EGENERIC;
-}
-
 static inline int block_WaitBytes( block_bytestream_t *p_bytestream,
                                    size_t i_data )
 {
-    block_t *p_block;
-    size_t i_offset, i_copy, i_size;
-
-    /* Check we have that much data */
-    i_offset = p_bytestream->i_offset;
-    i_size = i_data;
-    for( p_block = p_bytestream->p_block;
-         p_block != NULL; p_block = p_block->p_next )
-    {
-        i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
-        i_size -= i_copy;
-        i_offset = 0;
-
-        if( !i_size ) break;
-    }
-
-    if( i_size )
-    {
-        /* Not enough data, bail out */
-        return VLC_EGENERIC;
-    }
-    return VLC_SUCCESS;
-}
-
-static inline int block_SkipBytes( block_bytestream_t *p_bytestream,
-                                   size_t i_data )
-{
-    block_t *p_block;
-    size_t i_offset, i_copy;
-
-    /* Check we have that much data */
-    i_offset = p_bytestream->i_offset;
-    i_copy = 0;
-    for( p_block = p_bytestream->p_block;
-         p_block != NULL; p_block = p_block->p_next )
-    {
-        i_copy = __MIN( i_data, p_block->i_buffer - i_offset );
-        i_data -= i_copy;
-
-        if( !i_data ) break;
-
-        i_offset = 0;
-    }
-
-    if( i_data )
-    {
-        /* Not enough data, bail out */
-        return VLC_EGENERIC;
-    }
-
-    p_bytestream->p_block = p_block;
-    p_bytestream->i_offset = i_offset + i_copy;
-    return VLC_SUCCESS;
+    if( block_BytestreamRemaining( p_bytestream ) >= i_data )
+        return VLC_SUCCESS;
+    return VLC_EGENERIC;
 }
 
 static inline int block_PeekBytes( block_bytestream_t *p_bytestream,
                                    uint8_t *p_data, size_t i_data )
 {
-    block_t *p_block;
-    size_t i_offset, i_copy, i_size;
-
-    /* Check we have that much data */
-    i_offset = p_bytestream->i_offset;
-    i_size = i_data;
-    for( p_block = p_bytestream->p_block;
-         p_block != NULL; p_block = p_block->p_next )
-    {
-        i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
-        i_size -= i_copy;
-        i_offset = 0;
-
-        if( !i_size ) break;
-    }
-
-    if( i_size )
-    {
-        /* Not enough data, bail out */
+    if( block_BytestreamRemaining( p_bytestream ) < i_data )
         return VLC_EGENERIC;
-    }
 
     /* Copy the data */
-    i_offset = p_bytestream->i_offset;
-    i_size = i_data;
-    for( p_block = p_bytestream->p_block;
+    size_t i_offset = p_bytestream->i_block_offset;
+    size_t i_size = i_data;
+    for( block_t *p_block = p_bytestream->p_block;
          p_block != NULL; p_block = p_block->p_next )
     {
-        i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
+        size_t i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
         i_size -= i_copy;
 
         if( i_copy )
@@ -326,84 +183,66 @@ static inline int block_PeekBytes( block_bytestream_t *p_bytestream,
 static inline int block_GetBytes( block_bytestream_t *p_bytestream,
                                   uint8_t *p_data, size_t i_data )
 {
-    block_t *p_block;
-    size_t i_offset, i_copy, i_size;
-
-    /* Check we have that much data */
-    i_offset = p_bytestream->i_offset;
-    i_size = i_data;
-    for( p_block = p_bytestream->p_block;
-         p_block != NULL; p_block = p_block->p_next )
-    {
-        i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
-        i_size -= i_copy;
-        i_offset = 0;
-
-        if( !i_size ) break;
-    }
-
-    if( i_size )
-    {
-        /* Not enough data, bail out */
+    if( block_BytestreamRemaining( p_bytestream ) < i_data )
         return VLC_EGENERIC;
-    }
 
     /* Copy the data */
-    i_offset = p_bytestream->i_offset;
-    i_size = i_data;
-    i_copy = 0;
+    size_t i_offset = p_bytestream->i_block_offset;
+    size_t i_size = i_data;
+    size_t i_copy = 0;
+    block_t *p_block;
     for( p_block = p_bytestream->p_block;
          p_block != NULL; p_block = p_block->p_next )
     {
         i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
         i_size -= i_copy;
 
-        if( i_copy )
+        if( i_copy && p_data != NULL )
         {
             memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
             p_data += i_copy;
         }
 
-        if( !i_size ) break;
+        if( i_size == 0 )
+            break;
 
+        p_bytestream->i_base_offset += p_block->i_buffer;
         i_offset = 0;
     }
 
     p_bytestream->p_block = p_block;
-    p_bytestream->i_offset = i_offset + i_copy;
+    p_bytestream->i_block_offset = i_offset + i_copy;
 
     return VLC_SUCCESS;
 }
 
-static inline int block_PeekOffsetBytes( block_bytestream_t *p_bytestream,
-    size_t i_peek_offset, uint8_t *p_data, size_t i_data )
+static inline int block_SkipBytes( block_bytestream_t *p_bytestream,
+                                   size_t i_data )
 {
-    block_t *p_block;
-    size_t i_offset, i_copy, i_size;
-
-    /* Check we have that much data */
-    i_offset = p_bytestream->i_offset;
-    i_size = i_data + i_peek_offset;
-    for( p_block = p_bytestream->p_block;
-         p_block != NULL; p_block = p_block->p_next )
-    {
-        i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
-        i_size -= i_copy;
-        i_offset = 0;
+    return block_GetBytes( p_bytestream, NULL, i_data );
+}
 
-        if( !i_size ) break;
-    }
+static inline int block_SkipByte( block_bytestream_t *p_bytestream )
+{
+    return block_GetBytes( p_bytestream, NULL, 1 );
+}
 
-    if( i_size )
+static inline int block_PeekOffsetBytes( block_bytestream_t *p_bytestream,
+    size_t i_peek_offset, uint8_t *p_data, size_t i_data )
+{
+    /* Check we have that much data, ignoring read offset */
+    if( p_bytestream->i_total < i_peek_offset ||
+        p_bytestream->i_total - i_peek_offset < i_data )
     {
         /* Not enough data, bail out */
         return VLC_EGENERIC;
     }
 
     /* Find the right place */
-    i_offset = p_bytestream->i_offset;
-    i_size = i_peek_offset;
-    i_copy = 0;
+    size_t i_offset = p_bytestream->i_block_offset;
+    size_t i_size = i_peek_offset;
+    size_t i_copy = 0;
+    block_t *p_block;
     for( p_block = p_bytestream->p_block;
          p_block != NULL; p_block = p_block->p_next )
     {
@@ -445,12 +284,12 @@ static inline int block_FindStartcodeFromOffset(
     block_startcode_helper_t p_startcode_helper )
 {
     block_t *p_block, *p_block_backup = 0;
-    int i_size = 0;
+    ssize_t i_size = 0;
     size_t i_offset, i_offset_backup = 0;
     int i_caller_offset_backup = 0, i_match;
 
     /* Find the right place */
-    i_size = *pi_offset + p_bytestream->i_offset;
+    i_size = *pi_offset + p_bytestream->i_block_offset;
     for( p_block = p_bytestream->p_block;
          p_block != NULL; p_block = p_block->p_next )
     {
diff --git a/modules/packetizer/dirac.c b/modules/packetizer/dirac.c
index f950535..d18e1e5 100644
--- a/modules/packetizer/dirac.c
+++ b/modules/packetizer/dirac.c
@@ -302,7 +302,7 @@ static void dirac_RecoverTimestamps ( decoder_t *p_dec, size_t i_length )
     block_t *p_block = p_sys->bytestream.p_block;
 
     /* Find the block with first non-flushed data */
-    size_t i_offset = p_sys->bytestream.i_offset;
+    size_t i_offset = p_sys->bytestream.i_block_offset;
     for(; p_block != NULL; p_block = p_block->p_next )
     {
         if( i_offset < p_block->i_buffer )
diff --git a/modules/packetizer/packetizer_helper.h b/modules/packetizer/packetizer_helper.h
index 00e98d8..171ca5c 100644
--- a/modules/packetizer/packetizer_helper.h
+++ b/modules/packetizer/packetizer_helper.h
@@ -169,7 +169,7 @@ static inline block_t *packetizer_Packetize( packetizer_t *p_pack, block_t **pp_
                  * the data extend up to the end */
                 block_ChainProperties( p_pack->bytestream.p_chain,
                                        NULL, &p_pack->i_offset, NULL );
-                p_pack->i_offset -= p_pack->bytestream .i_offset;
+                p_pack->i_offset -= p_pack->bytestream.i_block_offset;
 
                 if( p_pack->i_offset <= (size_t)p_pack->i_startcode )
                     return NULL;



More information about the vlc-commits mailing list