[vlc-devel] [PATCH 2/5] libmp4: add two functions for fragmented MP4

Frédéric Yhuel fyhuel at viotech.net
Wed Apr 11 11:40:03 CEST 2012


- MP4_BoxGetInitFrag()
- MP4_BoxGetNextChunk()
---
 modules/demux/mp4/libmp4.c |  133 ++++++++++++++++++++++++++++++++++++++++++++
 modules/demux/mp4/libmp4.h |   10 +++
 2 files changed, 143 insertions(+), 0 deletions(-)

diff --git a/modules/demux/mp4/libmp4.c b/modules/demux/mp4/libmp4.c
index b8424bb..2771939 100644
--- a/modules/demux/mp4/libmp4.c
+++ b/modules/demux/mp4/libmp4.c
@@ -3367,6 +3367,139 @@ void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
     free( p_box );
 }
 
+MP4_Box_t *MP4_BoxGetInitFrag( stream_t *s )
+{
+    /* p_chunk is a virtual root container for the ftyp and moov boxes */
+    MP4_Box_t *p_chunk;
+    MP4_Box_t *p_ftyp;
+    MP4_Box_t *p_moov;
+    MP4_Box_t *p_trash;
+
+    p_chunk = malloc( sizeof( MP4_Box_t ) );
+    if( unlikely( p_chunk == NULL ) )
+        return NULL;
+
+    p_chunk->i_type = ATOM_root;
+    p_chunk->i_shortsize = 1;
+
+    p_chunk->data.p_data = NULL;
+    p_chunk->p_father    = NULL;
+    p_chunk->p_first     = NULL;
+    p_chunk->p_last      = NULL;
+    p_chunk->p_next      = NULL;
+
+    p_ftyp = MP4_ReadBox( s, p_chunk );
+    enum{ TRIES = 8 };
+    /* there may be some boxes between ftyp and moov,
+     * we skip them, but put a reasonable limit */
+    for( int i = 0 ; i < TRIES; i++ )
+    {
+        p_moov = MP4_ReadBox( s, p_chunk );
+        if( p_moov->i_type != ATOM_moov )
+        {
+            if( i == TRIES-1 )
+                return NULL;
+            stream_Read( s, NULL, p_moov->i_size );
+            MP4_BoxFree( s, p_moov );
+        }
+        else
+            break;
+    }
+
+    if( p_ftyp )
+    {
+        p_chunk->p_first = p_ftyp;
+        p_ftyp->p_next = p_moov;
+        p_chunk->p_last = p_moov;
+    }
+    else
+    {
+        msg_Warn( s, "no ftyp box found!");
+        return NULL;
+    }
+
+    return p_chunk;
+}
+
+MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
+{
+    /* p_chunk is a virtual root container for the moof and mdat boxes */
+    MP4_Box_t *p_chunk;
+    MP4_Box_t *p_moof = NULL;
+    MP4_Box_t *p_mdat = NULL;
+    MP4_Box_t *p_sidx = NULL;
+
+    p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
+    p_mdat = calloc( 1, sizeof( MP4_Box_t ) );
+    if( unlikely( p_chunk == NULL || p_mdat == NULL ) )
+        return NULL;
+
+    p_chunk->i_type = ATOM_root;
+    p_chunk->i_shortsize = 1;
+
+    enum{ TRIES = 8 };
+
+    /* there may be some boxes before moof,
+     * we skip them (but sidx) for now, but put a reasonable limit */
+    for( int i = 0 ; i < TRIES; i++ )
+    {
+        p_moof = MP4_ReadBox( s, p_chunk );
+        if( p_moof->i_type != ATOM_moof )
+        {
+            if( i == TRIES-1 )
+                return NULL;
+            if( p_moof->i_type != ATOM_sidx )
+            {
+                MP4_BoxFree( s, p_moof );
+                stream_Read( s, NULL, p_moof->i_size );
+            }
+            else
+                p_sidx = p_moof;
+        }
+        else
+            break;
+    }
+
+    /* there may be some boxes between moof and mdat,
+     * we skip them for now, but put a reasonable limit */
+    for( int i = 0 ; i < TRIES; i++ )
+    {
+        MP4_ReadBoxCommon( s, p_mdat );
+        if( p_mdat->i_type != ATOM_mdat )
+        {
+            if( i == TRIES-1 )
+                return NULL;
+            stream_Read( s, NULL, p_mdat->i_size );
+        }
+        else
+            break;
+    }
+
+    if( p_moof && p_mdat )
+    {
+        p_chunk->p_first = p_moof;
+        p_moof->p_next = p_mdat;
+        p_chunk->p_last = p_mdat;
+    }
+    else
+    {
+        msg_Warn( s, "no moof box found!");
+        MP4_BoxFree( s, p_chunk );
+        MP4_BoxFree( s, p_mdat );
+        MP4_BoxFree( s, p_moof );
+        MP4_BoxFree( s, p_sidx );
+        return NULL;
+    }
+    if( p_sidx )
+    {
+        p_chunk->p_first = p_sidx;
+        p_sidx->p_next = p_moof;
+    }
+
+    return p_chunk;
+}
+
+
 /*****************************************************************************
  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
  *****************************************************************************
diff --git a/modules/demux/mp4/libmp4.h b/modules/demux/mp4/libmp4.h
index d2c2e80..b0467cc 100644
--- a/modules/demux/mp4/libmp4.h
+++ b/modules/demux/mp4/libmp4.h
@@ -1340,6 +1340,16 @@ static const UUID_t TfxdBoxUUID = {
                   0x80, 0xe2, 0x14, 0x1b, 0xaf, 0xf7, 0x57, 0xb2 } };
 
 
+MP4_Box_t *MP4_BoxGetInitFrag( stream_t * );
+
+/*****************************************************************************
+ * MP4_BoxGetNextChunk : Parse the entire moof box.
+ *****************************************************************************
+ *  The first box is a virtual box "root".
+ *  if i_tk_id > 0, then seek to the next chunk, until
+ *  i_tk_id match the tfhd's id of the traf box.
+ *****************************************************************************/
+MP4_Box_t *MP4_BoxGetNextChunk( stream_t * );
 
 /*****************************************************************************
  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
-- 
1.7.5.4




More information about the vlc-devel mailing list