]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/libmp4.c
mp4: fix pointer type
[vlc] / modules / demux / mp4 / libmp4.c
index 5a899356614f7a4001ac2b0515e1d8680cff734a..90489dad47867e58f1c2599033af24b719d1c998 100644 (file)
@@ -1,23 +1,23 @@
 /*****************************************************************************
  * libmp4.c : LibMP4 library for mp4 module for vlc
  *****************************************************************************
- * Copyright (C) 2001-2004, 2010 the VideoLAN team
+ * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
  *
  * Author: Laurent Aimar <fenrir@via.ecp.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -90,7 +90,7 @@ int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box )
     }
     p_box->i_pos = stream_Tell( p_stream );
 
-    p_box->data.p_data = NULL;
+    p_box->data.p_payload = NULL;
     p_box->p_father = NULL;
     p_box->p_first  = NULL;
     p_box->p_last  = NULL;
@@ -159,15 +159,20 @@ static int MP4_NextBox( stream_t *p_stream, MP4_Box_t *p_box )
 
     if( p_box->p_father )
     {
-        const off_t i_box_end = p_box->i_size + p_box->i_pos;
-        const off_t i_father_end = p_box->p_father->i_size + p_box->p_father->i_pos;
-
-        /* check if it's within p-father */
-        if( i_box_end >= i_father_end )
+        /* if father's size == 0, it means unknown or infinite size,
+         * and we skip the followong check */
+        if( p_box->p_father->i_size > 0 )
         {
-            if( i_box_end > i_father_end )
-                msg_Dbg( p_stream, "out of bound child" );
-            return 0; /* out of bound */
+            const off_t i_box_end = p_box->i_size + p_box->i_pos;
+            const off_t i_father_end = p_box->p_father->i_size + p_box->p_father->i_pos;
+
+            /* check if it's within p-father */
+            if( i_box_end >= i_father_end )
+            {
+                if( i_box_end > i_father_end )
+                    msg_Dbg( p_stream, "out of bound child" );
+                return 0; /* out of bound */
+            }
         }
     }
     if( stream_Seek( p_stream, p_box->i_size + p_box->i_pos ) )
@@ -184,12 +189,17 @@ static int MP4_NextBox( stream_t *p_stream, MP4_Box_t *p_box )
  *       after called one of theses functions, file position is unknown
  *       you need to call MP4_GotoBox to go where you want
  *****************************************************************************/
-static int MP4_ReadBoxContainerRaw( stream_t *p_stream, MP4_Box_t *p_container )
+static int MP4_ReadBoxContainerChildren( stream_t *p_stream,
+                                    MP4_Box_t *p_container, uint32_t i_last_child )
 {
     MP4_Box_t *p_box;
 
-    if( stream_Tell( p_stream ) + 8 >
+    /* Size of root container is set to 0 when unknown, for exemple
+     * with a DASH stream. In that case, we skip the following check */
+    if( p_container->i_size
+            && ( stream_Tell( p_stream ) + 8 >
         (off_t)(p_container->i_pos + p_container->i_size) )
+      )
     {
         /* there is no box to load */
         return 0;
@@ -204,14 +214,26 @@ static int MP4_ReadBoxContainerRaw( stream_t *p_stream, MP4_Box_t *p_container )
         else p_container->p_last->p_next = p_box;
         p_container->p_last = p_box;
 
+        if( p_box->i_type == i_last_child )
+        {
+            MP4_NextBox( p_stream, p_box );
+            break;
+        }
+
     } while( MP4_NextBox( p_stream, p_box ) == 1 );
 
     return 1;
 }
 
+static int MP4_ReadBoxContainerRaw( stream_t *p_stream, MP4_Box_t *p_container )
+{
+    return MP4_ReadBoxContainerChildren( p_stream, p_container, 0 );
+}
+
 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
 {
-    if( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 )
+    if( p_container->i_size &&
+        ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
     {
         /* container is empty, 8 stand for the first header in this box */
         return 1;
@@ -475,12 +497,66 @@ static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
     FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
 }
 
+static int MP4_ReadBox_stra( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_stra_t );
+    MP4_Box_data_stra_t *p_stra = p_box->data.p_stra;
+
+    uint8_t i_reserved;
+    VLC_UNUSED(i_reserved);
+    MP4_GET1BYTE( p_stra->i_es_cat );
+    MP4_GET1BYTE( i_reserved );
+    MP4_GET2BYTES( p_stra->i_track_ID );
+
+    MP4_GET4BYTES( p_stra->i_timescale );
+    MP4_GET8BYTES( p_stra->i_duration );
+
+    MP4_GET4BYTES( p_stra->FourCC );
+    MP4_GET4BYTES( p_stra->Bitrate );
+    MP4_GET4BYTES( p_stra->MaxWidth );
+    MP4_GET4BYTES( p_stra->MaxHeight );
+    MP4_GET4BYTES( p_stra->SamplingRate );
+    MP4_GET4BYTES( p_stra->Channels );
+    MP4_GET4BYTES( p_stra->BitsPerSample );
+    MP4_GET4BYTES( p_stra->AudioTag );
+    MP4_GET2BYTES( p_stra->nBlockAlign );
+
+    MP4_GET1BYTE( i_reserved );
+    MP4_GET1BYTE( i_reserved );
+    MP4_GET1BYTE( i_reserved );
+    MP4_GET1BYTE( p_stra->cpd_len );
+    if( p_stra->cpd_len > i_read )
+        goto error;
+    p_stra->CodecPrivateData = malloc( p_stra->cpd_len );
+    if( unlikely( p_stra->CodecPrivateData == NULL ) )
+        goto error;
+    memcpy( p_stra->CodecPrivateData, p_peek, p_stra->cpd_len );
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream, "es_cat is %"PRIu8", birate is %"PRIu32,
+              p_stra->i_es_cat, p_stra->Bitrate );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+error:
+    MP4_READBOX_EXIT( 0 );
+}
+
+static void MP4_FreeBox_stra( MP4_Box_t *p_box )
+{
+    FREENULL( p_box->data.p_stra->CodecPrivateData );
+}
+
 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
 {
     if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
         return MP4_ReadBox_tfrf( p_stream, p_box );
     if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
         return MP4_ReadBox_tfxd( p_stream, p_box );
+    if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
+        return MP4_ReadBoxContainer( p_stream, p_box );
+    if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
+        return MP4_ReadBox_stra( p_stream, p_box );
 
     msg_Warn( p_stream, "Unknown uuid type box" );
     return 1;
@@ -492,6 +568,10 @@ static void MP4_FreeBox_uuid( MP4_Box_t *p_box )
         return MP4_FreeBox_tfrf( p_box );
     if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
         return MP4_FreeBox_Common( p_box );
+    if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
+        return MP4_FreeBox_Common( p_box );
+    if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
+        return MP4_FreeBox_stra( p_box );
 }
 
 static int MP4_ReadBox_sidx(  stream_t *p_stream, MP4_Box_t *p_box )
@@ -516,6 +596,7 @@ static int MP4_ReadBox_sidx(  stream_t *p_stream, MP4_Box_t *p_box )
     }
 
     uint16_t i_reserved;
+    VLC_UNUSED(i_reserved);
     MP4_GET2BYTES( i_reserved );
     MP4_GET2BYTES( p_sidx_data->i_reference_count );
     uint16_t i_count = p_sidx_data->i_reference_count;
@@ -722,7 +803,7 @@ static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
     double scale[2];    // scale factor; sx = scale[0] , sy = scale[1]
     double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
 
-    int *matrix = p_box->data.p_tkhd->i_matrix;
+    int32_t *matrix = p_box->data.p_tkhd->i_matrix;
 
     translate[0] = conv_fx(matrix[6]);
     translate[1] = conv_fx(matrix[7]);
@@ -828,6 +909,7 @@ static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
 {
     int32_t i_reserved;
+    VLC_UNUSED(i_reserved);
 
     MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
 
@@ -1025,7 +1107,7 @@ static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
     p_box->data.p_stts->i_sample_count =
         calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
     p_box->data.p_stts->i_sample_delta =
-        calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
+        calloc( p_box->data.p_stts->i_entry_count, sizeof(int32_t) );
     if( p_box->data.p_stts->i_sample_count == NULL
      || p_box->data.p_stts->i_sample_delta == NULL )
     {
@@ -1064,7 +1146,7 @@ static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
     p_box->data.p_ctts->i_sample_count =
         calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
     p_box->data.p_ctts->i_sample_offset =
-        calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
+        calloc( p_box->data.p_ctts->i_entry_count, sizeof(int32_t) );
     if( ( p_box->data.p_ctts->i_sample_count == NULL )
      || ( p_box->data.p_ctts->i_sample_offset == NULL ) )
     {
@@ -1224,6 +1306,29 @@ static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
 #undef es_descriptor
 }
 
+static void MP4_FreeBox_hvcC(MP4_Box_t *p_box )
+{
+    MP4_Box_data_hvcC_t *p_hvcC =  p_box->data.p_hvcC;
+    if( p_hvcC->i_hvcC > 0 ) FREENULL( p_hvcC->p_hvcC) ;
+}
+
+static int MP4_ReadBox_hvcC( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_Box_data_hvcC_t *p_hvcC;
+
+    MP4_READBOX_ENTER( MP4_Box_data_hvcC_t );
+    p_hvcC = p_box->data.p_hvcC;
+
+    p_hvcC->i_hvcC = i_read;
+    if( p_hvcC->i_hvcC > 0 )
+    {
+        uint8_t * p = p_hvcC->p_hvcC = malloc( p_hvcC->i_hvcC );
+        if( p )
+            memcpy( p, p_peek, i_read );
+    }
+    MP4_READBOX_EXIT( 1 );
+}
+
 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
 {
     MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
@@ -1372,9 +1477,10 @@ static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
     p_dvc1 = p_box->data.p_dvc1;
 
     MP4_GET1BYTE( p_dvc1->i_profile_level ); /* profile is on 4bits, level 3bits */
-    if( (p_dvc1->i_profile_level & 0xf0) >> 4 != 0x06 )
+    uint8_t i_profile = (p_dvc1->i_profile_level & 0xf0) >> 4;
+    if( i_profile != 0x06 && i_profile != 0x0c )
     {
-        msg_Warn( p_stream, "unsupported VC-1 profile, please report" );
+        msg_Warn( p_stream, "unsupported VC-1 profile (%"PRIu8"), please report", i_profile );
         MP4_READBOX_EXIT( 0 );
     }
 
@@ -1390,8 +1496,8 @@ static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
 
 #ifdef MP4_VERBOSE
     msg_Dbg( p_stream,
-             "read box: \"dvc1\" profile=%i level=%i",
-             p_dvc1->i_profile_level & 0xf0 >> 4, p_dvc1->i_profile_level & 0x0e >> 1 );
+             "read box: \"dvc1\" profile=%"PRIu8" level=%i",
+             i_profile, p_dvc1->i_profile_level & 0x0e >> 1 );
 #endif
 
     MP4_READBOX_EXIT( 1 );
@@ -1429,7 +1535,9 @@ static int MP4_ReadBox_gnre( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_READBOX_EXIT( 0 );
 
     uint32_t i_version;
+    VLC_UNUSED(i_version);
     uint32_t i_reserved;
+    VLC_UNUSED(i_reserved);
     MP4_GET4BYTES( i_version );
     MP4_GET4BYTES( i_reserved );
     MP4_GET2BYTES( p_gnre->i_genre );
@@ -1458,7 +1566,9 @@ static int MP4_ReadBox_trkn( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_READBOX_EXIT( 0 );
 
     uint32_t i_version;
+    VLC_UNUSED(i_version);
     uint32_t i_reserved;
+    VLC_UNUSED(i_reserved);
     MP4_GET4BYTES( i_version );
     MP4_GET4BYTES( i_reserved );
     MP4_GET2BYTES( i_reserved );
@@ -1477,7 +1587,6 @@ static int MP4_ReadBox_trkn( stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-
 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
@@ -1739,7 +1848,7 @@ static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[0] );
     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[1] );
     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[2] );
-    p_box->data.p_sample_text->i_background_color[3] = 0;
+    p_box->data.p_sample_text->i_background_color[3] = 0xFF;
 
     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
@@ -1776,6 +1885,13 @@ static int MP4_ReadBox_sample_tx3g( stream_t *p_stream, MP4_Box_t *p_box )
     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
 
+    MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved3 );
+
+    MP4_GET2BYTES( p_box->data.p_sample_text->i_font_id );
+    MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_face );
+    MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_size );
+    MP4_GET4BYTES( p_box->data.p_sample_text->i_font_color );
+
 #ifdef MP4_VERBOSE
     msg_Dbg( p_stream, "read box: \"tx3g\" in stsd text" );
 #endif
@@ -2118,7 +2234,7 @@ static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
     p_box->data.p_elst->i_segment_duration =
         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
     p_box->data.p_elst->i_media_time =
-        calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
+        calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
     p_box->data.p_elst->i_media_rate_integer =
         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
     p_box->data.p_elst->i_media_rate_fraction =
@@ -2481,6 +2597,7 @@ static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
 
 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
 {
+    VLC_UNUSED(p_box);
     /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
      * so unless data decrypt itself by magic, there will be no playback,
      * but we never know... */
@@ -2511,6 +2628,29 @@ static void MP4_FreeBox_name( MP4_Box_t *p_box )
     FREENULL( p_box->data.p_name->psz_text );
 }
 
+static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_data_t );
+
+    /* What's that header ?? */
+    if ( i_read <= 8 )
+        MP4_READBOX_EXIT( 0 );
+
+    p_box->data.p_data->p_blob = malloc( i_read - 8 );
+    if ( !p_box->data.p_data->p_blob )
+        MP4_READBOX_EXIT( 0 );
+
+    p_box->data.p_data->i_blob = i_read - 8;
+    memcpy( p_box->data.p_data->p_blob, p_peek + 8, i_read - 8 );
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static void MP4_FreeBox_data( MP4_Box_t *p_box )
+{
+    free( p_box->data.p_data->p_blob );
+}
+
 static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
 {
     uint16_t i16;
@@ -2560,6 +2700,7 @@ static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
             /* data box contains a version/flags field */
             uint32_t i_version;
             uint32_t i_reserved;
+            VLC_UNUSED(i_reserved);
             MP4_GET4BYTES( i_version );
             MP4_GET4BYTES( i_reserved );
             // version should be 0, flags should be 1 for text, 0 for data
@@ -2607,6 +2748,7 @@ static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_Box_data_chpl_t *p_chpl;
     uint32_t i_dummy;
+    VLC_UNUSED(i_dummy);
     int i;
     MP4_READBOX_ENTER( MP4_Box_data_chpl_t );
 
@@ -2717,6 +2859,7 @@ static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
 {
     char i_unused;
+    VLC_UNUSED(i_unused);
 
     MP4_READBOX_ENTER( MP4_Box_data_iods_t );
     MP4_GETVERSIONFLAGS( p_box->data.p_iods );
@@ -2820,7 +2963,7 @@ static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
 
 #ifdef MP4_VERBOSE
-    msg_Info( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
+    msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
     msg_Dbg( p_stream,
              "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
                  p_sdtp->p_sample_table[0],
@@ -2971,8 +3114,8 @@ static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
                 ((uint64_t *)(p_tfra->p_moof_offset))[1] );
     }
 
-    msg_Info( p_stream, "number_of_entries is %"PRIu32"", i_number_of_entries );
-    msg_Info( p_stream, "track ID is: %"PRIu32"", p_tfra->i_track_ID );
+    msg_Dbg( p_stream, "number_of_entries is %"PRIu32"", i_number_of_entries );
+    msg_Dbg( p_stream, "track ID is: %"PRIu32"", p_tfra->i_track_ID );
 #endif
 
     MP4_READBOX_EXIT( 1 );
@@ -3099,6 +3242,7 @@ static const struct
     { ATOM_dcom,    MP4_ReadBox_dcom,         MP4_FreeBox_Common },
     { ATOM_cmvd,    MP4_ReadBox_cmvd,         MP4_FreeBox_cmvd },
     { ATOM_avcC,    MP4_ReadBox_avcC,         MP4_FreeBox_avcC },
+    { ATOM_hvcC,    MP4_ReadBox_hvcC,         MP4_FreeBox_hvcC },
     { ATOM_dac3,    MP4_ReadBox_dac3,         MP4_FreeBox_Common },
     { ATOM_dvc1,    MP4_ReadBox_dvc1,         MP4_FreeBox_Common },
     { ATOM_enda,    MP4_ReadBox_enda,         MP4_FreeBox_Common },
@@ -3113,6 +3257,10 @@ static const struct
     { ATOM_free,    MP4_ReadBoxSkip,          MP4_FreeBox_Common },
     { ATOM_wide,    MP4_ReadBoxSkip,          MP4_FreeBox_Common },
 
+    /* Subtitles */
+    { ATOM_tx3g,    MP4_ReadBox_sample_tx3g,      MP4_FreeBox_Common },
+    //{ ATOM_text,    MP4_ReadBox_sample_text,      MP4_FreeBox_Common },
+
     /* for codecs */
     { ATOM_soun,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun },
     { ATOM_ms02,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun },
@@ -3264,6 +3412,8 @@ static const struct
     /* iTunes/Quicktime meta info */
     { ATOM_meta,    MP4_ReadBox_meta,         MP4_FreeBox_Common },
     { ATOM_name,    MP4_ReadBox_name,         MP4_FreeBox_name },
+    { ATOM_covr,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
+    { ATOM_data,    MP4_ReadBox_data,         MP4_FreeBox_data },
 
     /* found in smoothstreaming */
     { ATOM_traf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
@@ -3351,7 +3501,7 @@ void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
     }
 
     /* Now search function to call */
-    if( p_box->data.p_data )
+    if( p_box->data.p_payload )
     {
         for( i_index = 0; ; i_index++ )
         {
@@ -3377,17 +3527,19 @@ void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
         {
             MP4_Box_Function[i_index].MP4_FreeBox_function( p_box );
         }
-        free( p_box->data.p_data );
+        free( p_box->data.p_payload );
     }
     free( p_box );
 }
 
-MP4_Box_t *MP4_BoxGetInitFrag( stream_t *s )
+/* SmooBox is a very simple MP4 box, VLC specific, used only for the stream_filter to
+ * send information to the demux. SmooBox is actually a simplified moov box (we wanted
+ * to avoid the hassle of building a moov box at the stream_filter level) */
+MP4_Box_t *MP4_BoxGetSmooBox( stream_t *s )
 {
-    /* p_chunk is a virtual root container for the ftyp and moov boxes */
+    /* p_chunk is a virtual root container for the smoo box */
     MP4_Box_t *p_chunk;
-    MP4_Box_t *p_ftyp;
-    MP4_Box_t *p_moov;
+    MP4_Box_t *p_smoo;
 
     p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
     if( unlikely( p_chunk == NULL ) )
@@ -3396,100 +3548,59 @@ MP4_Box_t *MP4_BoxGetInitFrag( stream_t *s )
     p_chunk->i_type = ATOM_root;
     p_chunk->i_shortsize = 1;
 
-    p_ftyp = MP4_ReadBox( s, p_chunk );
-    if( !p_ftyp )
+    p_smoo = MP4_ReadBox( s, p_chunk );
+    if( !p_smoo || p_smoo->i_type != ATOM_uuid || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
     {
-        msg_Warn( s, "no ftyp box found!");
+        msg_Warn( s, "no smoo 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;
+    p_chunk->p_first = p_smoo;
+    p_chunk->p_last = p_smoo;
 
     return p_chunk;
 
 error:
-        free( p_chunk );
-        return NULL;
+    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;
+    MP4_Box_t *p_tmp_box = NULL;
 
-    p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
-    if( unlikely( p_chunk == NULL ) )
+    p_tmp_box = calloc( 1, sizeof( MP4_Box_t ) );
+    if( unlikely( p_tmp_box == NULL ) )
         return NULL;
 
-    p_chunk->i_type = ATOM_root;
-    p_chunk->i_shortsize = 1;
+    /* We might get a ftyp box or a SmooBox */
+    MP4_ReadBoxCommon( s, p_tmp_box );
 
-    /* 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++ )
+    if( (p_tmp_box->i_type == ATOM_uuid && !CmpUUID( &p_tmp_box->i_uuid, &SmooBoxUUID )) )
     {
-        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;
+        free( p_tmp_box );
+        return MP4_BoxGetSmooBox( s );
     }
-
-    p_chunk->p_first = p_moof;
-    p_chunk->p_last = p_moof;
-
-    if( p_sidx )
+    else if( p_tmp_box->i_type == ATOM_ftyp )
     {
-        p_chunk->p_first = p_sidx;
-        p_sidx->p_next = p_moof;
+        free( p_tmp_box );
+        return MP4_BoxGetRoot( s );
     }
+    free( p_tmp_box );
 
-    return p_chunk;
+    p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
+    if( unlikely( p_chunk == NULL ) )
+        return NULL;
 
-error:
-    free( p_chunk );
-    return NULL;
-}
+    p_chunk->i_type = ATOM_root;
+    p_chunk->i_shortsize = 1;
 
-#undef MAX_SKIP
+    MP4_ReadBoxContainerChildren( s, p_chunk, ATOM_moof );
+
+    return p_chunk;
+}
 
 /*****************************************************************************
  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
@@ -3510,10 +3621,11 @@ MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
     p_root->i_pos = 0;
     p_root->i_type = ATOM_root;
     p_root->i_shortsize = 1;
-    p_root->i_size = stream_Size( s );
+    /* could be a DASH stream for exemple, 0 means unknown or infinite size */
+    p_root->i_size = 0;
     CreateUUID( &p_root->i_uuid, p_root->i_type );
 
-    p_root->data.p_data = NULL;
+    p_root->data.p_payload = NULL;
     p_root->p_father    = NULL;
     p_root->p_first     = NULL;
     p_root->p_last      = NULL;
@@ -3521,37 +3633,56 @@ MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
 
     p_stream = s;
 
-    i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
+    /* First get the moov */
+    i_result = MP4_ReadBoxContainerChildren( p_stream, p_root, ATOM_moov );
+
+    if( !i_result )
+        goto error;
+    /* If there is a mvex box, it means fragmented MP4, and we're done */
+    else if( MP4_BoxCount( p_root, "moov/mvex" ) > 0 )
+        return p_root;
 
-    if( i_result )
+    p_root->i_size = stream_Size( s );
+    if( stream_Tell( s ) + 8 < stream_Size( s ) )
     {
-        MP4_Box_t *p_moov;
-        MP4_Box_t *p_cmov;
+        /* Get the rest of the file */
+        i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
 
-        /* check if there is a cmov, if so replace
-          compressed moov by  uncompressed one */
-        if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
-              ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
-            ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
-              ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
-        {
-            /* rename the compressed moov as a box to skip */
-            p_moov->i_type = ATOM_skip;
+        if( !i_result )
+            goto error;
+    }
 
-            /* get uncompressed p_moov */
-            p_moov = p_cmov->data.p_cmov->p_moov;
-            p_cmov->data.p_cmov->p_moov = NULL;
+    MP4_Box_t *p_moov;
+    MP4_Box_t *p_cmov;
 
-            /* make p_root father of this new moov */
-            p_moov->p_father = p_root;
+    /* check if there is a cmov, if so replace
+      compressed moov by  uncompressed one */
+    if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
+          ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
+        ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
+          ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
+    {
+        /* rename the compressed moov as a box to skip */
+        p_moov->i_type = ATOM_skip;
 
-            /* insert this new moov box as first child of p_root */
-            p_moov->p_next = p_root->p_first;
-            p_root->p_first = p_moov;
-        }
+        /* get uncompressed p_moov */
+        p_moov = p_cmov->data.p_cmov->p_moov;
+        p_cmov->data.p_cmov->p_moov = NULL;
+
+        /* make p_root father of this new moov */
+        p_moov->p_father = p_root;
+
+        /* insert this new moov box as first child of p_root */
+        p_moov->p_next = p_root->p_first;
+        p_root->p_first = p_moov;
     }
 
     return p_root;
+
+error:
+    free( p_root );
+    stream_Seek( p_stream, 0 );
+    return NULL;
 }
 
 
@@ -3582,12 +3713,12 @@ static void MP4_BoxDumpStructure_Internal( stream_t *s,
         }
         if( MP4_BOX_TYPE_ASCII() )
             snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
-                      "+ %4.4s size %d",
-                        (char*)&p_box->i_type, (uint32_t)p_box->i_size );
+                      "+ %4.4s size %"PRIu64" offset %ld",
+                        (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
         else
             snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
-                      "+ c%3.3s size %d",
-                        (char*)&p_box->i_type+1, (uint32_t)p_box->i_size );
+                      "+ c%3.3s size %"PRIu64" offset %ld",
+                        (char*)&p_box->i_type+1, p_box->i_size, p_box->i_pos );
         msg_Dbg( s, "%s", str );
     }
     p_child = p_box->p_first;