[vlc-devel] [PATCH 20/20] mkv: Reimplemented MkvTree and moved it to `Util.hpp`

Filip Roséen filip at videolabs.io
Fri Mar 4 17:04:18 CET 2016


`MkvTree` can be used all across the module, meaning that it better
belongs in `Util.hpp` than with static linkage in `matroska_segment_parse.cpp`.

 - A helper function `MkvTree_va` has been introduced so that one can
   easily call the function from functions that accepts a variadic
   number of arguments (using va_list).

 - The implementation has been optimized so that we do not need to
   allocate storage for the format string on every invocation if it
   would fit inside a fixed length buffer.

 - An error check has been added to signal if we run out of memory.
---
 modules/demux/mkv/matroska_segment_parse.cpp | 25 -----------------
 modules/demux/mkv/mkv.cpp                    |  1 -
 modules/demux/mkv/util.cpp                   | 42 ++++++++++++++++++++++++++++
 modules/demux/mkv/util.hpp                   |  4 +++
 4 files changed, 46 insertions(+), 26 deletions(-)

diff --git a/modules/demux/mkv/matroska_segment_parse.cpp b/modules/demux/mkv/matroska_segment_parse.cpp
index 5344309..d316cd2 100644
--- a/modules/demux/mkv/matroska_segment_parse.cpp
+++ b/modules/demux/mkv/matroska_segment_parse.cpp
@@ -187,31 +187,6 @@ void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
     }
 }
 
-
-/**
- * Helper function to print the mkv parse tree
- */
-static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
-{
-    va_list args;
-    if( i_level > 9 )
-    {
-        msg_Err( &demuxer, "MKV tree is too deep" );
-        return;
-    }
-    va_start( args, psz_format );
-    static const char psz_foo[] = "|   |   |   |   |   |   |   |   |   |";
-    char *psz_foo2 = static_cast<char*>( malloc( i_level * 4 + 3 + strlen( psz_format ) ) );
-    strncpy( psz_foo2, psz_foo, 4 * i_level );
-    psz_foo2[ 4 * i_level ] = '+';
-    psz_foo2[ 4 * i_level + 1 ] = ' ';
-    strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
-    msg_GenericVa( &demuxer,VLC_MSG_DBG, psz_foo2, args );
-    free( psz_foo2 );
-    va_end( args );
-}
-
-
 /*****************************************************************************
  * ParseTrackEntry:
  *****************************************************************************/
diff --git a/modules/demux/mkv/mkv.cpp b/modules/demux/mkv/mkv.cpp
index 9a0a53f..04b7f2d 100644
--- a/modules/demux/mkv/mkv.cpp
+++ b/modules/demux/mkv/mkv.cpp
@@ -794,4 +794,3 @@ static int Demux( demux_t *p_demux)
 
     return i_return;
 }
-
diff --git a/modules/demux/mkv/util.cpp b/modules/demux/mkv/util.cpp
index a61c209..bd3398c 100644
--- a/modules/demux/mkv/util.cpp
+++ b/modules/demux/mkv/util.cpp
@@ -415,3 +415,45 @@ block_t * packetize_wavpack( mkv_track_t * p_tk, uint8_t * buffer, size_t  size)
 
     return p_block;
 }
+
+void MkvTree_va( demux_t& demuxer, int i_level, const char* fmt, va_list args)
+{
+    static char const * indent = "|    ";
+    static char const * prefix = "+ ";
+    static int  const   indent_len = strlen( indent );
+    static int  const   prefix_len = strlen( prefix );
+
+    char   fixed_buffer[256] = {};
+    size_t const  static_len = sizeof( fixed_buffer );
+    char *            buffer = fixed_buffer;
+    size_t         total_len = indent_len * i_level + prefix_len + strlen( fmt );
+
+    if( total_len >= static_len ) {
+        buffer = new (std::nothrow) char[total_len] ();
+
+        if (buffer == NULL) {
+            msg_Err (&demuxer, "Unable to allocate memory for format string");
+            return;
+        }
+    }
+
+    char * dst = buffer;
+
+    for (int i = 0; i < i_level; ++i, dst += indent_len)
+        memcpy( dst, indent, indent_len );
+
+    strcat( dst, prefix );
+    strcat( dst, fmt );
+
+    msg_GenericVa( &demuxer, VLC_MSG_DBG, buffer, args );
+
+    if (buffer != fixed_buffer)
+        delete [] buffer;
+}
+
+void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
+{
+    va_list args; va_start( args, psz_format );
+    MkvTree_va( demuxer, i_level, psz_format, args );
+    va_end( args );
+}
diff --git a/modules/demux/mkv/util.hpp b/modules/demux/mkv/util.hpp
index 8c6cc6a..5867d1d 100644
--- a/modules/demux/mkv/util.hpp
+++ b/modules/demux/mkv/util.hpp
@@ -90,3 +90,7 @@ public:
 };
 
 block_t * packetize_wavpack( mkv_track_t *, uint8_t *, size_t);
+
+/* helper functions to print the mkv parse tree */
+void MkvTree_va( demux_t& demuxer, int i_level, const char* fmt, va_list args);
+void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... );
-- 
2.7.2



More information about the vlc-devel mailing list