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

Frédéric Yhuel fyhuel at viotech.net
Wed Apr 11 16:15:18 CEST 2012


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

diff --git a/modules/demux/mp4/libmp4.c b/modules/demux/mp4/libmp4.c
index b8424bb..a36a2e3 100644
--- a/modules/demux/mp4/libmp4.c
+++ b/modules/demux/mp4/libmp4.c
@@ -3367,6 +3367,115 @@ 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;
+
+    p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
+    if( unlikely( p_chunk == NULL ) )
+        return NULL;
+
+    p_chunk->i_type = ATOM_root;
+    p_chunk->i_shortsize = 1;
+
+    p_ftyp = MP4_ReadBox( s, p_chunk );
+    if( !p_ftyp )
+    {
+        msg_Warn( s, "no ftyp box found!");
+        goto error;
+    }
+
+    /* there may be some boxes between ftyp and moov,
+     * we skip them, but put a reasonable limit */
+    const int stop = 8;
+    for( int i = 0 ; i < stop; i++ )
+    {
+        p_moov = MP4_ReadBox( s, p_chunk );
+        if( !p_moov )
+            goto error;
+        if( p_moov->i_type != ATOM_moov )
+        {
+            if( i == stop - 1 )
+                return NULL;
+            stream_Read( s, NULL, p_moov->i_size );
+            MP4_BoxFree( s, p_moov );
+        }
+        else
+            break;
+    }
+
+    p_chunk->p_first = p_ftyp;
+    p_ftyp->p_next = p_moov;
+    p_chunk->p_last = p_moov;
+
+    return p_chunk;
+
+error:
+        free( p_chunk );
+        return NULL;
+}
+
+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_sidx = NULL;
+
+    p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
+    if( unlikely( p_chunk == NULL ) )
+        return NULL;
+
+    p_chunk->i_type = ATOM_root;
+    p_chunk->i_shortsize = 1;
+
+    /* there may be some boxes before moof,
+     * we skip them (but sidx) for now, but put a reasonable limit */
+    const int stop = 8;
+    for( int i = 0 ; i < stop; i++ )
+    {
+        p_moof = MP4_ReadBox( s, p_chunk );
+        if( !p_moof )
+            goto error;
+        if( p_moof->i_type != ATOM_moof )
+        {
+            if( i == stop - 1 )
+            {
+                MP4_BoxFree( s, p_moof );
+                goto error;
+            }
+            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;
+    }
+
+   p_chunk->p_first = p_moof;
+   p_chunk->p_last = p_moof;
+
+    if( p_sidx )
+    {
+        p_chunk->p_first = p_sidx;
+        p_sidx->p_next = p_moof;
+    }
+
+    return p_chunk;
+
+error:
+    free( p_chunk );
+    return NULL;
+}
+
+
 /*****************************************************************************
  * 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