[vlc-commits] libmp4: add two functions for fragmented MP4

Frédéric Yhuel git at videolan.org
Fri Apr 13 17:03:20 CEST 2012


vlc | branch: master | Frédéric Yhuel <fyhuel at viotech.net> | Wed Apr 11 17:24:40 2012 +0200| [baff262e647231c27ff6cb802bb579e843caeaf7] | committer: Jean-Baptiste Kempf

libmp4: add two functions for fragmented MP4

- MP4_BoxGetInitFrag()
- MP4_BoxGetNextChunk()

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

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

 modules/demux/mp4/libmp4.c |  109 ++++++++++++++++++++++++++++++++++++++++++++
 modules/demux/mp4/libmp4.h |   15 ++++++
 2 files changed, 124 insertions(+), 0 deletions(-)

diff --git a/modules/demux/mp4/libmp4.c b/modules/demux/mp4/libmp4.c
index a50c525..42a56df 100644
--- a/modules/demux/mp4/libmp4.c
+++ b/modules/demux/mp4/libmp4.c
@@ -3366,6 +3366,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 */
+#define MAX_SKIP 8
+    for( int i = 0 ; i < MAX_SKIP; i++ )
+    {
+        p_moov = MP4_ReadBox( s, p_chunk );
+        if( !p_moov )
+            goto error;
+        if( p_moov->i_type != ATOM_moov )
+        {
+            if( i == MAX_SKIP - 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 */
+    for( int i = 0 ; i < MAX_SKIP; i++ )
+    {
+        p_moof = MP4_ReadBox( s, p_chunk );
+        if( !p_moof )
+            goto error;
+        if( p_moof->i_type != ATOM_moof )
+        {
+            if( i == MAX_SKIP - 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;
+}
+
+#undef MAX_SKIP
+
 /*****************************************************************************
  * 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 b8490d5..86fc0fc 100644
--- a/modules/demux/mp4/libmp4.h
+++ b/modules/demux/mp4/libmp4.h
@@ -1342,6 +1342,21 @@ static const UUID_t TfxdBoxUUID = {
                   0x80, 0xe2, 0x14, 0x1b, 0xaf, 0xf7, 0x57, 0xb2 } };
 
 
+/*****************************************************************************
+ * MP4_BoxGetInitFrag : Parse the initialization segment.
+ *****************************************************************************
+ *  The first box is a virtual box "root", and is the father of the boxes
+ *  'ftyp' and 'moov'.
+ *****************************************************************************/
+MP4_Box_t *MP4_BoxGetInitFrag( stream_t * );
+
+/*****************************************************************************
+ * MP4_BoxGetNextChunk : Parse the entire moof box.
+ *****************************************************************************
+ *  The first box is a virtual box "root" and is the father of the boxes
+ *  'moof' and, possibly, 'sidx'.
+ *****************************************************************************/
+MP4_Box_t *MP4_BoxGetNextChunk( stream_t * );
 
 /*****************************************************************************
  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory



More information about the vlc-commits mailing list