block_helper: add startcode helper callback
authorFrancois Cartegnie <fcvlcdev@free.fr>
Tue, 5 Jan 2016 15:08:09 +0000 (16:08 +0100)
committerFrancois Cartegnie <fcvlcdev@free.fr>
Tue, 5 Jan 2016 18:14:33 +0000 (19:14 +0100)
include/vlc_block_helper.h
modules/packetizer/dirac.c
modules/packetizer/packetizer_helper.h

index 4414307..444286f 100644 (file)
@@ -438,9 +438,12 @@ static inline int block_PeekOffsetBytes( block_bytestream_t *p_bytestream,
     return VLC_SUCCESS;
 }
 
+typedef const uint8_t * (*block_startcode_helper_t)( const uint8_t *, const uint8_t * );
+
 static inline int block_FindStartcodeFromOffset(
     block_bytestream_t *p_bytestream, size_t *pi_offset,
-    const uint8_t *p_startcode, int i_startcode_length )
+    const uint8_t *p_startcode, int i_startcode_length,
+    block_startcode_helper_t p_startcode_helper )
 {
     block_t *p_block, *p_block_backup = 0;
     int i_size = 0;
@@ -472,6 +475,21 @@ static inline int block_FindStartcodeFromOffset(
     {
         for( i_offset = i_size; i_offset < p_block->i_buffer; i_offset++ )
         {
+            /* Use optimized helper when possible */
+            if( p_startcode_helper && !i_match &&
+               (p_block->i_buffer - i_offset) > ((size_t)i_startcode_length - 1) )
+            {
+                const uint8_t *p_res = p_startcode_helper( &p_block->p_buffer[i_offset],
+                                                           &p_block->p_buffer[p_block->i_buffer] );
+                if( p_res )
+                {
+                    *pi_offset += i_offset + (p_res - &p_block->p_buffer[i_offset]);
+                    return VLC_SUCCESS;
+                }
+                /* Then parsing boundary with legacy code */
+                i_offset = p_block->i_buffer - (i_startcode_length - 1);
+            }
+
             if( p_block->p_buffer[i_offset] == p_startcode[i_match] )
             {
                 if( !i_match )
index 59b9474..f950535 100644 (file)
@@ -677,7 +677,7 @@ static block_t *dirac_DoSync( decoder_t *p_dec )
         case NOT_SYNCED:
         {
             if( VLC_SUCCESS !=
-                block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_parsecode, 4 ) )
+                block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_parsecode, 4, NULL ) )
             {
                 /* p_sys->i_offset will have been set to:
                  *   end of bytestream - amount of prefix found
index c38c255..7503ed1 100644 (file)
@@ -132,7 +132,7 @@ static inline block_t *packetizer_Packetize( packetizer_t *p_pack, block_t **pp_
         case STATE_NOSYNC:
             /* Find a startcode */
             if( !block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
-                                                p_pack->p_startcode, p_pack->i_startcode ) )
+                                                p_pack->p_startcode, p_pack->i_startcode, NULL ) )
                 p_pack->i_state = STATE_NEXT_SYNC;
 
             if( p_pack->i_offset )
@@ -150,7 +150,7 @@ static inline block_t *packetizer_Packetize( packetizer_t *p_pack, block_t **pp_
         case STATE_NEXT_SYNC:
             /* Find the next startcode */
             if( block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
-                                               p_pack->p_startcode, p_pack->i_startcode ) )
+                                               p_pack->p_startcode, p_pack->i_startcode, NULL ) )
             {
                 if( !p_pack->b_flushing || !p_pack->bytestream.p_chain )
                     return NULL; /* Need more data */