[vlc-devel] [PATCH 11/17] Stream Filter: Stream use vlc_data_t

Denis Charmet typx at dinauz.org
Mon Apr 22 19:10:40 CEST 2019


---
 modules/stream_filter/aribcam.c         |  12 +-
 modules/stream_filter/cache_block.c     |  35 +++---
 modules/stream_filter/skiptags.c        |  22 ++--
 modules/stream_filter/vlc_data_helper.h | 157 ++++++++++++++++++++++++
 4 files changed, 192 insertions(+), 34 deletions(-)
 create mode 100644 modules/stream_filter/vlc_data_helper.h

diff --git a/modules/stream_filter/aribcam.c b/modules/stream_filter/aribcam.c
index 4af82d364a..a6126f26c5 100644
--- a/modules/stream_filter/aribcam.c
+++ b/modules/stream_filter/aribcam.c
@@ -92,7 +92,7 @@ typedef struct
     {
         uint8_t *p_buf;
         size_t   i_size;
-        block_t *p_list;
+        vlc_data_t *p_list;
     } remain;
 } stream_sys_t;
 
@@ -131,9 +131,9 @@ static size_t RemainRead( stream_t *p_stream, uint8_t *p_data, size_t i_toread )
 
         if ( p_sys->remain.p_list->i_buffer == 0 )
         {
-            block_t *p_prevhead = p_sys->remain.p_list;
+            vlc_data_t *p_prevhead = p_sys->remain.p_list;
             p_sys->remain.p_list = p_sys->remain.p_list->p_next;
-            block_Release( p_prevhead );
+            vlc_data_Release( p_prevhead );
         }
     }
     return i_total;
@@ -144,19 +144,19 @@ static bool RemainAdd( stream_t *p_stream, const uint8_t *p_data, size_t i_size
     stream_sys_t *p_sys = p_stream->p_sys;
     if ( i_size == 0 )
         return true;
-    block_t *p_block = block_Alloc( i_size );
+    vlc_data_t *p_block = vlc_data_Alloc( i_size );
     if ( !p_block )
         return false;
     memcpy( p_block->p_buffer, p_data, i_size );
     p_block->i_buffer = i_size;
-    block_ChainAppend( & p_sys->remain.p_list, p_block );
+    vlc_data_ChainAppend( & p_sys->remain.p_list, p_block );
     p_sys->remain.i_size += i_size;
     return true;
 }
 
 static void RemainFlush( stream_sys_t *p_sys )
 {
-    block_ChainRelease( p_sys->remain.p_list );
+    vlc_data_ChainRelease( p_sys->remain.p_list );
     p_sys->remain.p_list = NULL;
     p_sys->remain.i_size = 0;
 }
diff --git a/modules/stream_filter/cache_block.c b/modules/stream_filter/cache_block.c
index 4d6cb363bb..c59118f48e 100644
--- a/modules/stream_filter/cache_block.c
+++ b/modules/stream_filter/cache_block.c
@@ -32,7 +32,7 @@
 #include <vlc_plugin.h>
 #include <vlc_stream.h>
 #include <vlc_interrupt.h>
-#include <vlc_block_helper.h>
+#include "vlc_data_helper.h"
 
 /* TODO:
  *  - tune the 2 methods (block/stream)
@@ -41,7 +41,7 @@
  *  - ...
  */
 
-/* 
+/*
  * One linked list of data read
  */
 
@@ -66,7 +66,7 @@
 
 typedef struct
 {
-    block_bytestream_t cache; /* bytestream chain for storing cache */
+    vlc_data_bytestream_t cache; /* bytestream chain for storing cache */
 
     struct
     {
@@ -84,7 +84,7 @@ static int AStreamRefillBlock(stream_t *s)
     /* Release data */
     if (cache_size >= STREAM_CACHE_SIZE)
     {
-        block_BytestreamFlush( &sys->cache );
+        vlc_data_BytestreamFlush( &sys->cache );
         cache_size = sys->cache.i_total;
     }
     if (cache_size >= STREAM_CACHE_SIZE &&
@@ -96,7 +96,7 @@ static int AStreamRefillBlock(stream_t *s)
 
     /* Now read a new block */
     const vlc_tick_t start = vlc_tick_now();
-    block_t *b;
+    vlc_data_t *b;
 
     for (;;)
     {
@@ -111,10 +111,9 @@ static int AStreamRefillBlock(stream_t *s)
     }
     sys->stat.read_time += vlc_tick_now() - start;
     size_t added_bytes;
-    block_ChainProperties( b, NULL, &added_bytes, NULL );
     sys->stat.read_bytes += added_bytes;
 
-    block_BytestreamPush( &sys->cache, b );
+    vlc_data_BytestreamPush( &sys->cache, b );
     return VLC_SUCCESS;
 }
 
@@ -128,7 +127,7 @@ static void AStreamPrebufferBlock(stream_t *s)
     for (;;)
     {
         const vlc_tick_t now = vlc_tick_now();
-        size_t cache_size = block_BytestreamRemaining( &sys->cache );
+        size_t cache_size = vlc_data_BytestreamRemaining( &sys->cache );
 
         if (vlc_killed() || cache_size > STREAM_CACHE_PREBUFFER_SIZE)
         {
@@ -147,7 +146,7 @@ static void AStreamPrebufferBlock(stream_t *s)
         }
 
         /* Fetch a block */
-        block_t *b = vlc_stream_ReadBlock(s->s);
+        vlc_data_t *b = vlc_stream_ReadBlock(s->s);
         if (b == NULL)
         {
             if (vlc_stream_Eof(s->s))
@@ -155,7 +154,7 @@ static void AStreamPrebufferBlock(stream_t *s)
             continue;
         }
 
-        block_BytestreamPush( &sys->cache, b);
+        vlc_data_BytestreamPush( &sys->cache, b);
 
         if (first)
         {
@@ -173,7 +172,7 @@ static void AStreamControlReset(stream_t *s)
 {
     stream_sys_t *sys = s->p_sys;
 
-    block_BytestreamEmpty( &sys->cache );
+    vlc_data_BytestreamEmpty( &sys->cache );
 
     /* Do the prebuffering */
     AStreamPrebufferBlock(s);
@@ -183,14 +182,14 @@ static int AStreamSeekBlock(stream_t *s, uint64_t i_pos)
 {
     stream_sys_t *sys = s->p_sys;
 
-    if( block_SkipBytes( &sys->cache, i_pos) == VLC_SUCCESS )
+    if( vlc_data_SkipBytes( &sys->cache, i_pos) == VLC_SUCCESS )
         return VLC_SUCCESS;
 
     /* Not enought bytes, empty and seek */
     /* Do the access seek */
     if (vlc_stream_Seek(s->s, i_pos)) return VLC_EGENERIC;
 
-    block_BytestreamEmpty( &sys->cache );
+    vlc_data_BytestreamEmpty( &sys->cache );
 
     /* Refill a block */
     if (AStreamRefillBlock(s))
@@ -203,7 +202,7 @@ static ssize_t AStreamReadBlock(stream_t *s, void *buf, size_t len)
 {
     stream_sys_t *sys = s->p_sys;
 
-    ssize_t i_current = block_BytestreamRemaining( &sys->cache );
+    ssize_t i_current = vlc_data_BytestreamRemaining( &sys->cache );
     size_t i_copy = VLC_CLIP((size_t)i_current, 0, len);
 
     /**
@@ -220,7 +219,7 @@ static ssize_t AStreamReadBlock(stream_t *s, void *buf, size_t len)
     }
 
     /* Copy data */
-    if( block_GetBytes( &sys->cache, buf, i_copy ) )
+    if( vlc_data_GetBytes( &sys->cache, buf, i_copy ) )
         return -1;
 
 
@@ -288,13 +287,13 @@ static int Open(vlc_object_t *obj)
     msg_Dbg(s, "Using block method for AStream*");
 
     /* Init all fields of sys->block */
-    block_BytestreamInit( &sys->cache );
+    vlc_data_BytestreamInit( &sys->cache );
 
     s->p_sys = sys;
     /* Do the prebuffering */
     AStreamPrebufferBlock(s);
 
-    if (block_BytestreamRemaining( &sys->cache ) <= 0)
+    if (vlc_data_BytestreamRemaining( &sys->cache ) <= 0)
     {
         msg_Err(s, "cannot pre fill buffer");
         free(sys);
@@ -315,7 +314,7 @@ static void Close(vlc_object_t *obj)
     stream_t *s = (stream_t *)obj;
     stream_sys_t *sys = s->p_sys;
 
-    block_BytestreamEmpty( &sys->cache );
+    vlc_data_BytestreamEmpty( &sys->cache );
     free(sys);
 }
 
diff --git a/modules/stream_filter/skiptags.c b/modules/stream_filter/skiptags.c
index 1097fc2764..0f1eb9324b 100644
--- a/modules/stream_filter/skiptags.c
+++ b/modules/stream_filter/skiptags.c
@@ -30,7 +30,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_stream.h>
-#include <vlc_block.h>
+#include <vlc_data.h>
 
 #define MAX_TAGS 16
 #define MAX_TAG_SIZE (1<<17)
@@ -39,7 +39,7 @@ struct skiptags_sys_t
 {
     uint64_t header_skip;
     /* TODO? also discard trailer tags? */
-    block_t *p_tags;
+    vlc_data_t *p_tags;
 };
 
 static struct skiptags_sys_t * skiptags_sys_New(void)
@@ -55,7 +55,7 @@ static struct skiptags_sys_t * skiptags_sys_New(void)
 
 static void skiptags_sys_Delete(struct skiptags_sys_t *sys)
 {
-    block_ChainRelease(sys->p_tags);
+    vlc_data_ChainRelease(sys->p_tags);
     free(sys);
 }
 
@@ -113,7 +113,7 @@ static uint_fast32_t SkipAPETag(stream_t *s)
 }
 
 static bool SkipTag(stream_t *s, uint_fast32_t (*skipper)(stream_t *),
-                    block_t **pp_block, unsigned *pi_tags_count)
+                    vlc_data_t **pp_block, unsigned *pi_tags_count)
 {
     uint_fast64_t offset = vlc_stream_Tell(s);
     uint_fast32_t size = skipper(s);
@@ -123,8 +123,10 @@ static bool SkipTag(stream_t *s, uint_fast32_t (*skipper)(stream_t *),
         ssize_t read;
         if(*pi_tags_count < MAX_TAGS && size <= MAX_TAG_SIZE)
         {
-            *pp_block = vlc_stream_Block(s, size);
-            read = *pp_block ? (ssize_t)(*pp_block)->i_buffer : -1;
+            *pp_block = vlc_data_Alloc(size);
+            if (*pp_block == NULL)
+                return -1;
+            read = (*pp_block)->i_buffer = vlc_stream_Read(s, (*pp_block)->p_buffer, size);
         }
         else
         {
@@ -133,7 +135,7 @@ static bool SkipTag(stream_t *s, uint_fast32_t (*skipper)(stream_t *),
 
         if(read < (ssize_t)size)
         {
-            block_ChainRelease(*pp_block);
+            vlc_data_ChainRelease(*pp_block);
             *pp_block = NULL;
             if (unlikely(read < 0))
             {   /* I/O error, try to restore offset. If it fails, screwed. */
@@ -173,7 +175,7 @@ static int Control(stream_t *stream, int query, va_list args)
         case STREAM_GET_TAGS:
             if (sys->p_tags == NULL)
                 break;
-            *va_arg(args, const block_t **) = sys->p_tags;
+            *va_arg(args, const vlc_data_t **) = sys->p_tags;
             return VLC_SUCCESS;
 
         case STREAM_GET_SIZE:
@@ -195,7 +197,7 @@ static int Open(vlc_object_t *obj)
     stream_t *s = stream->s;
     struct skiptags_sys_t *sys;
 
-    block_t *p_tags = NULL, *p_tag = NULL;
+    vlc_data_t *p_tags = NULL, *p_tag = NULL;
     unsigned i_tagscount = 0;
 
     while (SkipTag(s, SkipID3Tag, &p_tag, &i_tagscount)||
@@ -212,7 +214,7 @@ static int Open(vlc_object_t *obj)
     uint_fast64_t offset = vlc_stream_Tell(s);
     if (offset == 0 || !(sys = skiptags_sys_New()))
     {
-        block_ChainRelease( p_tags );
+        vlc_data_ChainRelease( p_tags );
         return VLC_EGENERIC; /* nothing to do */
     }
 
diff --git a/modules/stream_filter/vlc_data_helper.h b/modules/stream_filter/vlc_data_helper.h
new file mode 100644
index 0000000000..4a67d532f1
--- /dev/null
+++ b/modules/stream_filter/vlc_data_helper.h
@@ -0,0 +1,157 @@
+/*****************************************************************************
+ * vlc_data_helper.h: Helper functions for data blocks management.
+ *****************************************************************************
+ * Copyright (C) 2003-2017 VLC authors and VideoLAN
+ *
+ * Authors: Gildas Bazin <gbazin at netcourrier.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef VLC_DATA_HELPER_H
+#define VLC_DATA_HELPER_H 1
+
+/* TODO: This has been shamelessly stolen from vlc_frame_helper remove when
+ * reworking Chain/Fifo/Bytestream
+ */
+
+#include <vlc_data.h>
+
+typedef struct vlc_data_bytestream_t
+{
+    vlc_data_t *p_chain;  /**< byte stream head block */
+    vlc_data_t **pp_last; /**< tail ppointer for appends */
+    vlc_data_t *p_data;  /**< byte stream read pointer data */
+    size_t   i_data_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 */
+} vlc_data_bytestream_t;
+
+/*****************************************************************************
+ * vlc_data_bytestream_t management
+ *****************************************************************************/
+static inline void vlc_data_BytestreamInit( vlc_data_bytestream_t *p_bytestream )
+{
+    p_bytestream->p_chain = p_bytestream->p_data = NULL;
+    p_bytestream->pp_last = &p_bytestream->p_chain;
+    p_bytestream->i_data_offset = 0;
+    p_bytestream->i_base_offset = 0;
+    p_bytestream->i_total = 0;
+}
+
+static inline void vlc_data_BytestreamRelease( vlc_data_bytestream_t *p_bytestream )
+{
+    vlc_data_ChainRelease( p_bytestream->p_chain );
+}
+
+/**
+ * It flush all data (read and unread) from a vlc_data_bytestream_t.
+ */
+static inline void vlc_data_BytestreamEmpty( vlc_data_bytestream_t *p_bytestream )
+{
+    vlc_data_BytestreamRelease( p_bytestream );
+    vlc_data_BytestreamInit( p_bytestream );
+}
+
+/**
+ * It flushes all already read data from a vlc_data_bytestream_t.
+ */
+static inline void vlc_data_BytestreamFlush( vlc_data_bytestream_t *p_bytestream )
+{
+    vlc_data_t *data = p_bytestream->p_chain;
+
+    while( data != p_bytestream->p_data )
+    {
+        vlc_data_t *p_next = data->p_next;
+
+        p_bytestream->i_total -= data->i_buffer;
+        p_bytestream->i_base_offset -= data->i_buffer;
+        vlc_data_Release( data );
+        data = p_next;
+    }
+
+    while( data != NULL && data->i_buffer == p_bytestream->i_data_offset )
+    {
+        vlc_data_t *p_next = data->p_next;
+
+        p_bytestream->i_total -= data->i_buffer;
+        vlc_data_Release( data );
+        data = p_next;
+        p_bytestream->i_data_offset = 0;
+    }
+
+    p_bytestream->p_chain = p_bytestream->p_data = data;
+    if( p_bytestream->p_chain == NULL )
+        p_bytestream->pp_last = &p_bytestream->p_chain;
+}
+
+static inline void vlc_data_BytestreamPush( vlc_data_bytestream_t *p_bytestream,
+                                         vlc_data_t *p_data )
+{
+    vlc_data_ChainLastAppend( &p_bytestream->pp_last, p_data );
+    if( !p_bytestream->p_data ) p_bytestream->p_data = p_data;
+    for( ; p_data; p_data = p_data->p_next )
+        p_bytestream->i_total += p_data->i_buffer;
+}
+
+static inline size_t vlc_data_BytestreamRemaining( const vlc_data_bytestream_t *p_bytestream )
+{
+    return ( p_bytestream->i_total > p_bytestream->i_base_offset + p_bytestream->i_data_offset ) ?
+             p_bytestream->i_total - p_bytestream->i_base_offset - p_bytestream->i_data_offset : 0;
+}
+
+static inline int vlc_data_GetBytes( vlc_data_bytestream_t *p_bytestream,
+                                  uint8_t *p_data, size_t i_data )
+{
+    if( vlc_data_BytestreamRemaining( p_bytestream ) < i_data )
+        return VLC_EGENERIC;
+
+    /* Copy the data */
+    size_t i_offset = p_bytestream->i_data_offset;
+    size_t i_size = i_data;
+    size_t i_copy = 0;
+    vlc_data_t *p_block;
+    for( p_block = p_bytestream->p_data;
+         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 && p_data != NULL )
+        {
+            memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
+            p_data += i_copy;
+        }
+
+        if( i_size == 0 )
+            break;
+
+        p_bytestream->i_base_offset += p_block->i_buffer;
+        i_offset = 0;
+    }
+
+    p_bytestream->p_data = p_block;
+    p_bytestream->i_data_offset = i_offset + i_copy;
+
+    return VLC_SUCCESS;
+}
+
+static inline int vlc_data_SkipBytes( vlc_data_bytestream_t *p_bytestream,
+                                   size_t i_data )
+{
+    return vlc_data_GetBytes( p_bytestream, NULL, i_data );
+}
+
+#endif /* VLC_DATA_HELPER_H */
-- 
2.20.1



More information about the vlc-devel mailing list