[vlc-commits] demux: libmp4: rework tref reading

Francois Cartegnie git at videolan.org
Wed May 22 17:11:08 CEST 2019


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed May 22 14:43:00 2019 +0200| [e30acc033505a70fc088b790398c3bbcd34211dd] | committer: Francois Cartegnie

demux: libmp4: rework tref reading

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

 modules/demux/mp4/libmp4.c | 55 ++++++++++++++++++++++++++++++----------------
 modules/demux/mp4/libmp4.h | 17 ++++++++------
 modules/demux/mp4/mp4.c    |  8 +++----
 3 files changed, 50 insertions(+), 30 deletions(-)

diff --git a/modules/demux/mp4/libmp4.c b/modules/demux/mp4/libmp4.c
index 3b6f64a136..0471163576 100644
--- a/modules/demux/mp4/libmp4.c
+++ b/modules/demux/mp4/libmp4.c
@@ -3741,37 +3741,62 @@ static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
+static void MP4_FreeBox_TrackReference( MP4_Box_t *p_box )
 {
-    free( p_box->data.p_tref_generic->i_track_ID );
+    free( p_box->data.p_track_reference->i_track_ID );
 }
 
-static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_TrackReference( stream_t *p_stream, MP4_Box_t *p_box )
 {
     uint32_t count;
 
-    MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t, MP4_FreeBox_tref_generic );
+    MP4_READBOX_ENTER( MP4_Box_data_trak_reference_t, MP4_FreeBox_TrackReference );
 
-    p_box->data.p_tref_generic->i_track_ID = NULL;
+    p_box->data.p_track_reference->i_track_ID = NULL;
     count = i_read / sizeof(uint32_t);
-    p_box->data.p_tref_generic->i_entry_count = count;
-    p_box->data.p_tref_generic->i_track_ID = vlc_alloc( count,
+    p_box->data.p_track_reference->i_entry_count = count;
+    p_box->data.p_track_reference->i_track_ID = vlc_alloc( count,
                                                         sizeof(uint32_t) );
-    if( p_box->data.p_tref_generic->i_track_ID == NULL )
+    if( p_box->data.p_track_reference->i_track_ID == NULL )
         MP4_READBOX_EXIT( 0 );
 
     for( unsigned i = 0; i < count; i++ )
     {
-        MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
+        MP4_GET4BYTES( p_box->data.p_track_reference->i_track_ID[i] );
     }
 #ifdef MP4_VERBOSE
         msg_Dbg( p_stream, "read box: \"chap\" %d references",
-                 p_box->data.p_tref_generic->i_entry_count );
+                 p_box->data.p_track_reference->i_entry_count );
 #endif
 
     MP4_READBOX_EXIT( 1 );
 }
 
+static int MP4_ReadBox_tref( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    /* skip header */
+    ssize_t i_header = mp4_box_headersize( p_box );
+    if( vlc_stream_Read( p_stream, NULL, i_header ) != i_header )
+        return 0;
+    /* read each reference atom with forced handler */
+    uint64_t i_remain = p_box->i_size - 8;
+    while ( i_remain > 8 )
+    {
+        MP4_Box_t *p_childbox = MP4_ReadBoxUsing( p_stream, p_box,
+                                                  MP4_ReadBox_TrackReference );
+        if( !p_childbox || i_remain < p_childbox->i_size )
+        {
+            MP4_BoxFree( p_childbox );
+            break;
+        }
+
+        MP4_BoxAddChild( p_box, p_childbox );
+        i_remain -= p_childbox->i_size;
+    }
+
+    return MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) ? 0 : 1;
+}
+
 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
 {
     for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
@@ -4760,7 +4785,7 @@ static const struct
     { ATOM_hnti,    MP4_ReadBoxContainer,     ATOM_udta },
     { ATOM_rmra,    MP4_ReadBoxContainer,     ATOM_moov },
     { ATOM_rmda,    MP4_ReadBoxContainer,     ATOM_rmra },
-    { ATOM_tref,    MP4_ReadBoxContainer,     ATOM_trak },
+    { ATOM_tref,    MP4_ReadBox_tref,         ATOM_trak },
     { ATOM_gmhd,    MP4_ReadBoxContainer,     ATOM_minf },
     { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_stsd },
     { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
@@ -4974,16 +4999,8 @@ static const struct
 
     { ATOM_mp4s,    MP4_ReadBox_sample_mp4s,  ATOM_stsd },
 
-    /* XXX there is 2 box where we could find this entry stbl and tref*/
-    { ATOM_hint,    MP4_ReadBox_default,      ATOM_tref },
     { ATOM_hint,    MP4_ReadBox_default,      ATOM_stbl },
 
-    /* found in tref box */
-    { ATOM_dpnd,    MP4_ReadBox_default,      ATOM_tref },
-    { ATOM_ipir,    MP4_ReadBox_default,      ATOM_tref },
-    { ATOM_mpod,    MP4_ReadBox_default,      ATOM_tref },
-    { ATOM_chap,    MP4_ReadBox_tref_generic, ATOM_tref },
-
     /* found in hnti */
     { ATOM_rtp,     MP4_ReadBox_rtp,          ATOM_hnti },
     { ATOM_sdp,     MP4_ReadBox_sdp,          ATOM_hnti },
diff --git a/modules/demux/mp4/libmp4.h b/modules/demux/mp4/libmp4.h
index 5b8b75fcaa..8603a9ec04 100644
--- a/modules/demux/mp4/libmp4.h
+++ b/modules/demux/mp4/libmp4.h
@@ -156,10 +156,6 @@ typedef int64_t stime_t;
 #define ATOM_hdv2 VLC_FOURCC( 'h', 'd', 'v', '2' )
 #define ATOM_rrtp VLC_FOURCC( 'r', 'r', 't', 'p' )
 
-#define ATOM_dpnd VLC_FOURCC( 'd', 'p', 'n', 'd' )
-#define ATOM_cdsc VLC_FOURCC( 'c', 'd', 's', 'c' )
-#define ATOM_ipir VLC_FOURCC( 'i', 'p', 'i', 'r' )
-#define ATOM_mpod VLC_FOURCC( 'm', 'p', 'o', 'd' )
 #define ATOM_hnti VLC_FOURCC( 'h', 'n', 't', 'i' )
 #define ATOM_rtp  VLC_FOURCC( 'r', 't', 'p', ' ' )
 #define ATOM_btrt VLC_FOURCC( 'b', 't', 'r', 't' )
@@ -415,7 +411,6 @@ typedef int64_t stime_t;
 
 #define ATOM_0x40PRM VLC_FOURCC( '@', 'P', 'R', 'M' )
 #define ATOM_0x40PRQ VLC_FOURCC( '@', 'P', 'R', 'Q' )
-#define ATOM_chap VLC_FOURCC( 'c', 'h', 'a', 'p' )
 #define ATOM_MCPS VLC_FOURCC( 'M', 'C', 'P', 'S' )
 #define ATOM_SDLN VLC_FOURCC( 'S', 'D', 'L', 'N' )
 #define ATOM_vndr VLC_FOURCC( 'v', 'n', 'd', 'r' )
@@ -441,6 +436,14 @@ typedef int64_t stime_t;
 
 #define SAMPLEGROUP_rap  VLC_FOURCC('r', 'a', 'p', ' ')
 
+/* tref reference type boxes */
+#define ATOM_chap VLC_FOURCC( 'c', 'h', 'a', 'p' )
+#define ATOM_dpnd VLC_FOURCC( 'd', 'p', 'n', 'd' )
+#define ATOM_hint VLC_FOURCC( 'h', 'i', 'n', 't' )
+#define ATOM_ipir VLC_FOURCC( 'i', 'p', 'i', 'r' )
+#define ATOM_mpod VLC_FOURCC( 'm', 'p', 'o', 'd' )
+#define ATOM_cdsc VLC_FOURCC( 'c', 'd', 's', 'c' )
+
 /* HEIF Specific */
 #define HANDLER_pict VLC_FOURCC('p', 'i', 'c', 't')
 
@@ -1254,7 +1257,7 @@ typedef struct
     uint32_t i_entry_count;
     uint32_t *i_track_ID;
 
-} MP4_Box_data_tref_generic_t;
+} MP4_Box_data_trak_reference_t;
 
 typedef struct
 {
@@ -1790,7 +1793,7 @@ typedef union MP4_Box_data_s
     MP4_Box_data_pnot_t *p_pnot;
     MP4_Box_data_chpl_t *p_chpl;
     MP4_Box_data_HMMT_t *p_hmmt;
-    MP4_Box_data_tref_generic_t *p_tref_generic;
+    MP4_Box_data_trak_reference_t *p_track_reference;
 
     MP4_Box_data_tfrf_t *p_tfrf;
     MP4_Box_data_tfxd_t *p_tfxd;
diff --git a/modules/demux/mp4/mp4.c b/modules/demux/mp4/mp4.c
index b882c84dd7..7393be3f2d 100644
--- a/modules/demux/mp4/mp4.c
+++ b/modules/demux/mp4/mp4.c
@@ -990,8 +990,8 @@ static int Open( vlc_object_t * p_this )
             b_enabled_es = true;
 
         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
-        if( p_chap && p_chap->data.p_tref_generic &&
-            p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
+        if( p_chap && p_chap->data.p_track_reference &&
+            p_chap->data.p_track_reference->i_entry_count > 0 && !p_sys->p_tref_chap )
             p_sys->p_tref_chap = p_chap;
     }
 
@@ -2384,7 +2384,7 @@ static void LoadChapter( demux_t  *p_demux )
     }
     else if( p_sys->p_tref_chap )
     {
-        MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
+        MP4_Box_data_trak_reference_t *p_chap = p_sys->p_tref_chap->data.p_track_reference;
         unsigned int i, j;
 
         /* Load the first subtitle track like quicktime */
@@ -3534,7 +3534,7 @@ static void MP4_TrackSetup( demux_t *p_demux, mp4_track_t *p_track,
     /* Mark chapter only track */
     if( p_sys->p_tref_chap )
     {
-        MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
+        MP4_Box_data_trak_reference_t *p_chap = p_sys->p_tref_chap->data.p_track_reference;
         unsigned int i;
 
         for( i = 0; i < p_chap->i_entry_count; i++ )



More information about the vlc-commits mailing list