]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/libmp4.c
mp4: use strndup() where applicable
[vlc] / modules / demux / mp4 / libmp4.c
index 20f546b72a158ebfd4c15ccadb7e966444ef42aa..ec734a611ef447539830782cb9e198840be833c0 100644 (file)
 #endif
 
 #include <vlc_common.h>
-#include <vlc_demux.h>
+#include <vlc_stream.h>                               /* stream_Peek*/
 
 #ifdef HAVE_ZLIB_H
 #   include <zlib.h>                                  /* for compressed moov */
 #endif
 
 #include "libmp4.h"
-#include "drms.h"
-
-/*****************************************************************************
- * Here are defined some macro to make life simpler but before using it
- * *look* at the code.
- *
- *****************************************************************************/
-
-static inline size_t mp4_box_headersize( MP4_Box_t *p_box )
-{
-    return 8
-        + ( p_box->i_shortsize == 1 ? 8 : 0 )
-        + ( p_box->i_type == ATOM_uuid ? 16 : 0 );
-}
-
-#define MP4_GETX_PRIVATE(dst, code, size) do { \
-    if( (i_read) >= (size) ) { dst = (code); p_peek += (size); } \
-    else { dst = 0; }   \
-    i_read -= (size);   \
-  } while(0)
-
-#define MP4_GET1BYTE( dst )  MP4_GETX_PRIVATE( dst, *p_peek, 1 )
-#define MP4_GET2BYTES( dst ) MP4_GETX_PRIVATE( dst, GetWBE(p_peek), 2 )
-#define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
-#define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
-#define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
-#define MP4_GETFOURCC( dst ) MP4_GETX_PRIVATE( dst, \
-                VLC_FOURCC(p_peek[0],p_peek[1],p_peek[2],p_peek[3]), 4)
-
-#define MP4_GETVERSIONFLAGS( p_void ) \
-    MP4_GET1BYTE( p_void->i_version ); \
-    MP4_GET3BYTES( p_void->i_flags )
-
-#define MP4_GETSTRINGZ( p_str )         \
-    if( (i_read > 0) && (p_peek[0]) )   \
-    {       \
-        const int __i_copy__ = strnlen( (char*)p_peek, i_read-1 );  \
-        p_str = malloc( __i_copy__+1 );               \
-        if( p_str ) \
-        { \
-             memcpy( p_str, p_peek, __i_copy__ ); \
-             p_str[__i_copy__] = 0; \
-        } \
-        p_peek += __i_copy__ + 1;   \
-        i_read -= __i_copy__ + 1;   \
-    }       \
-    else    \
-    {       \
-        p_str = NULL; \
-    }
-
-#define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t ) \
-    int64_t  i_read = p_box->i_size; \
-    uint8_t *p_peek, *p_buff; \
-    int i_actually_read; \
-    if( !( p_peek = p_buff = malloc( i_read ) ) ) \
-    { \
-        return( 0 ); \
-    } \
-    i_actually_read = stream_Read( p_stream, p_peek, i_read ); \
-    if( i_actually_read < 0 || (int64_t)i_actually_read < i_read )\
-    { \
-        free( p_buff ); \
-        return( 0 ); \
-    } \
-    p_peek += mp4_box_headersize( p_box ); \
-    i_read -= mp4_box_headersize( p_box ); \
-    if( !( p_box->data.p_data = calloc( 1, sizeof( MP4_Box_data_TYPE_t ) ) ) ) \
-    { \
-        free( p_buff ); \
-        return( 0 ); \
-    }
-
-#define MP4_READBOX_EXIT( i_code ) \
-    do \
-    { \
-        free( p_buff ); \
-        if( i_read < 0 ) \
-            msg_Warn( p_stream, "Not enough data" ); \
-        return( i_code ); \
-    } while (0)
-
+#include <math.h>
 
 /* Some assumptions:
  * The input method HAS to be seekable
-
-*/
-
-/* This macro is used when we want to printf the box type
- * APPLE annotation box is :
- *  either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
- *  either 32-bit ASCII text string
  */
-#define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
-
-static uint32_t Get24bBE( const uint8_t *p )
-{
-    return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
-}
 
-static void GetUUID( UUID_t *p_uuid, const uint8_t *p_buff )
-{
-    memcpy( p_uuid, p_buff, 16 );
-}
-
-static void CreateUUID( UUID_t *p_uuid, uint32_t i_fourcc )
-{
-    /* made by 0xXXXXXXXX-0011-0010-8000-00aa00389b71
-            where XXXXXXXX is the fourcc */
-    /* FIXME implement this */
-    (void)p_uuid;
-    (void)i_fourcc;
+/* convert 16.16 fixed point to floating point */
+static double conv_fx( int32_t fx ) {
+    double fp = fx;
+    fp /= 65536.;
+    return fp;
 }
 
 /* some functions for mp4 encoding of variables */
-
+#ifdef MP4_VERBOSE
 static void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
 {
     int i_day;
@@ -164,6 +63,7 @@ static void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
     i_sec =  i_date % 60;
     sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
 }
+#endif
 
 /*****************************************************************************
  * Some prototypes.
@@ -334,7 +234,7 @@ static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
 {
     /* XXX sometime moov is hiden in a free box */
     if( p_box->p_father &&
-        p_box->p_father->i_type == VLC_FOURCC( 'r', 'o', 'o', 't' ) &&
+        p_box->p_father->i_type == ATOM_root &&
         p_box->i_type == ATOM_free )
     {
         const uint8_t *p_peek;
@@ -379,15 +279,14 @@ static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
 
     if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
     {
-        unsigned int i;
         uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
             calloc( p_box->data.p_ftyp->i_compatible_brands_count,
                     sizeof(uint32_t));
 
-        if( tab == NULL )
+        if( unlikely( tab == NULL ) )
             MP4_READBOX_EXIT( 0 );
 
-        for( i =0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
+        for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
         {
             MP4_GETFOURCC( tab[i] );
         }
@@ -408,7 +307,6 @@ static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_mvhd(  stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
 #ifdef MP4_VERBOSE
     char s_creation_time[128];
     char s_modification_time[128];
@@ -437,15 +335,15 @@ static int MP4_ReadBox_mvhd(  stream_t *p_stream, MP4_Box_t *p_box )
     MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
 
 
-    for( i = 0; i < 2; i++ )
+    for( unsigned i = 0; i < 2; i++ )
     {
         MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
     }
-    for( i = 0; i < 9; i++ )
+    for( unsigned i = 0; i < 9; i++ )
     {
         MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
     }
-    for( i = 0; i < 6; i++ )
+    for( unsigned i = 0; i < 6; i++ )
     {
         MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
     }
@@ -482,6 +380,8 @@ static int MP4_ReadBox_mfhd(  stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_mfhd_t );
 
+    MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
+
     MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
 
 #ifdef MP4_VERBOSE
@@ -491,51 +391,229 @@ static int MP4_ReadBox_mfhd(  stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
+static int MP4_ReadBox_tfxd(  stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );
+
+    MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
+    MP4_GETVERSIONFLAGS( p_tfxd_data );
+
+    if( p_tfxd_data->i_version == 0 )
+    {
+        MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
+        MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
+    }
+    else
+    {
+        MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
+        MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
+    }
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
+            "fragment duration %"PRIu64", fragment abs time %"PRIu64,
+                p_tfxd_data->i_version,
+                p_tfxd_data->i_flags,
+                p_tfxd_data->i_fragment_duration,
+                p_tfxd_data->i_fragment_abs_time
+           );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static int MP4_ReadBox_tfrf(  stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );
+
+    MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
+    MP4_GETVERSIONFLAGS( p_tfrf_data );
+
+    MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
+
+    p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
+                                              sizeof( TfrfBoxDataFields_t ) );
+    if( !p_tfrf_data->p_tfrf_data_fields )
+        MP4_READBOX_EXIT( 0 );
+
+    for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
+    {
+        TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
+        if( p_tfrf_data->i_version == 0 )
+        {
+            MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
+            MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
+        }
+        else
+        {
+            MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
+            MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
+        }
+    }
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
+            "fragment count %"PRIu8, p_tfrf_data->i_version,
+                p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
+
+    for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
+    {
+        TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
+        msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
+                                    "fragment abs time %"PRIu64,
+                    TfrfBoxDataField->i_fragment_duration,
+                    TfrfBoxDataField->i_fragment_abs_time );
+    }
+
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
+{
+    FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
+}
+
+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 );
+
+    msg_Warn( p_stream, "Unknown uuid type box" );
+    return 1;
+}
+
+static void MP4_FreeBox_uuid( MP4_Box_t *p_box )
+{
+    if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
+        return MP4_FreeBox_tfrf( p_box );
+    if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
+        return MP4_FreeBox_Common( p_box );
+}
+
+static int MP4_ReadBox_sidx(  stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_sidx_t );
+
+    MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
+    MP4_GETVERSIONFLAGS( p_sidx_data );
+
+    MP4_GET4BYTES( p_sidx_data->i_reference_ID );
+    MP4_GET4BYTES( p_sidx_data->i_timescale );
+
+    if( p_sidx_data->i_version == 0 )
+    {
+        MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
+        MP4_GET4BYTES( p_sidx_data->i_first_offset );
+    }
+    else
+    {
+        MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
+        MP4_GET8BYTES( p_sidx_data->i_first_offset );
+    }
+
+    uint16_t i_reserved;
+    MP4_GET2BYTES( i_reserved );
+    MP4_GET2BYTES( p_sidx_data->i_reference_count );
+    uint16_t i_count = p_sidx_data->i_reference_count;
+
+    p_sidx_data->p_items = calloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
+    uint32_t tmp;
+    for( unsigned i = 0; i < i_count; i++ )
+    {
+        MP4_GET4BYTES( tmp );
+        p_sidx_data->p_items[i].b_reference_type = (bool)((tmp & 0x80000000)>>24);
+        p_sidx_data->p_items[i].i_referenced_size = tmp & 0x7fffffff;
+        MP4_GET4BYTES( p_sidx_data->p_items[i].i_subsegment_duration );
+
+        MP4_GET4BYTES( tmp );
+        p_sidx_data->p_items[i].b_starts_with_SAP = (bool)((tmp & 0x80000000)>>24);
+        p_sidx_data->p_items[i].i_SAP_type = (tmp & 0x70000000)>>24;
+        p_sidx_data->p_items[i].i_SAP_delta_time = tmp & 0xfffffff;
+    }
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
+            "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
+            "first subsegmt duration %"PRIu32,
+                p_sidx_data->i_version,
+                p_sidx_data->i_flags,
+                p_sidx_data->i_reference_ID,
+                p_sidx_data->i_timescale,
+                p_sidx_data->i_reference_count,
+                p_sidx_data->p_items[0].i_subsegment_duration
+           );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
+{
+    FREENULL( p_box->data.p_sidx->p_items );
+}
+
 static int MP4_ReadBox_tfhd(  stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_tfhd_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
 
+    if( p_box->data.p_tfhd->i_version != 0 )
+    {
+        msg_Warn( p_stream, "'tfhd' box with version != 0. "\
+                " Don't know what to do with that, please patch" );
+        MP4_READBOX_EXIT( 0 );
+    }
+
     MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
 
-    if( p_box->data.p_tfhd->i_version == 0 )
-    {
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
-            MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
-            MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
-            MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
-            MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
-            MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
-
-#ifdef MP4_VERBOSE
-        char psz_base[128] = "\0";
-        char psz_desc[128] = "\0";
-        char psz_dura[128] = "\0";
-        char psz_size[128] = "\0";
-        char psz_flag[128] = "\0";
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
-            snprintf(psz_base, sizeof(psz_base), "base offset %lld", p_box->data.p_tfhd->i_base_data_offset);
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
-            snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
-            snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
-            snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
-        if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
-            snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
-
-        msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
-                    p_box->data.p_tfhd->i_version,
-                    p_box->data.p_tfhd->i_flags,
-                    p_box->data.p_tfhd->i_track_ID,
-                    psz_base, psz_desc, psz_dura, psz_size, psz_flag );
-#endif
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
+    {
+        msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
+                "=> no samples for this time interval." );
+        p_box->data.p_tfhd->b_empty = true;
     }
+    else
+        p_box->data.p_tfhd->b_empty = false;
+
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
+        MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
+        MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
+        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
+        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
+        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
+
+#ifdef MP4_VERBOSE
+    char psz_base[128] = "\0";
+    char psz_desc[128] = "\0";
+    char psz_dura[128] = "\0";
+    char psz_size[128] = "\0";
+    char psz_flag[128] = "\0";
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
+        snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
+        snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
+        snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
+        snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
+    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
+        snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
+
+    msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
+                p_box->data.p_tfhd->i_version,
+                p_box->data.p_tfhd->i_flags,
+                p_box->data.p_tfhd->i_track_ID,
+                psz_base, psz_desc, psz_dura, psz_size, psz_flag );
+#endif
 
     MP4_READBOX_EXIT( 1 );
 }
@@ -549,7 +627,7 @@ static int MP4_ReadBox_trun(  stream_t *p_stream, MP4_Box_t *p_box )
     MP4_GET4BYTES( p_box->data.p_trun->i_sample_count );
 
     if( p_box->data.p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
-        MP4_GET8BYTES( p_box->data.p_trun->i_data_offset );
+        MP4_GET4BYTES( p_box->data.p_trun->i_data_offset );
     if( p_box->data.p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
         MP4_GET4BYTES( p_box->data.p_trun->i_first_sample_flags );
 
@@ -595,10 +673,8 @@ static void MP4_FreeBox_trun( MP4_Box_t *p_box )
 }
 
 
-
 static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
 #ifdef MP4_VERBOSE
     char s_creation_time[128];
     char s_modification_time[128];
@@ -625,7 +701,7 @@ static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
         MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
     }
 
-    for( i = 0; i < 2; i++ )
+    for( unsigned i = 0; i < 2; i++ )
     {
         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
     }
@@ -634,27 +710,63 @@ static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
     MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
     MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
 
-    for( i = 0; i < 9; i++ )
+    for( unsigned i = 0; i < 9; i++ )
     {
         MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
     }
     MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
     MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
 
+    double rotation;    //angle in degrees to be rotated clockwise
+    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;
+
+    translate[0] = conv_fx(matrix[6]);
+    translate[1] = conv_fx(matrix[7]);
+
+    scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
+                    conv_fx(matrix[3]) * conv_fx(matrix[3]));
+    scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
+                    conv_fx(matrix[4]) * conv_fx(matrix[4]));
+
+    rotation = atan2(conv_fx(matrix[1]) / scale[1], conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
+
+    if (rotation < 0)
+        rotation += 360.;
+
+    p_box->data.p_tkhd->f_rotation = rotation;
+
 #ifdef MP4_VERBOSE
     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
     MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time );
     MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration );
 
-    msg_Dbg( p_stream, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f width %f height %f",
+    msg_Dbg( p_stream, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f rotation %f scaleX %f scaleY %f translateX %f translateY %f width %f height %f. "
+            "Matrix: %i %i %i %i %i %i %i %i %i",
                   s_creation_time,
                   s_modification_time,
                   s_duration,
                   p_box->data.p_tkhd->i_track_ID,
                   p_box->data.p_tkhd->i_layer,
                   (float)p_box->data.p_tkhd->i_volume / 256 ,
+                  rotation,
+                  scale[0],
+                  scale[1],
+                  translate[0],
+                  translate[1],
                   (float)p_box->data.p_tkhd->i_width / 65536,
-                  (float)p_box->data.p_tkhd->i_height / 65536 );
+                  (float)p_box->data.p_tkhd->i_height / 65536,
+                  p_box->data.p_tkhd->i_matrix[0],
+                  p_box->data.p_tkhd->i_matrix[1],
+                  p_box->data.p_tkhd->i_matrix[2],
+                  p_box->data.p_tkhd->i_matrix[3],
+                  p_box->data.p_tkhd->i_matrix[4],
+                  p_box->data.p_tkhd->i_matrix[5],
+                  p_box->data.p_tkhd->i_matrix[6],
+                  p_box->data.p_tkhd->i_matrix[7],
+                  p_box->data.p_tkhd->i_matrix[8] );
 #endif
     MP4_READBOX_EXIT( 1 );
 }
@@ -662,7 +774,6 @@ static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
 
 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
     uint16_t i_language;
 #ifdef MP4_VERBOSE
     char s_creation_time[128];
@@ -688,7 +799,7 @@ static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
     }
     p_box->data.p_mdhd->i_language_code = i_language = GetWBE( p_peek );
-    for( i = 0; i < 3; i++ )
+    for( unsigned i = 0; i < 3; i++ )
     {
         p_box->data.p_mdhd->i_language[i] =
                     ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
@@ -732,7 +843,7 @@ static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
     if( i_read > 0 )
     {
         uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
-        if( psz == NULL )
+        if( unlikely( psz == NULL ) )
             MP4_READBOX_EXIT( 0 );
 
         /* Yes, I love .mp4 :( */
@@ -770,14 +881,12 @@ static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
 
     MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
-    for( i = 0; i < 3; i++ )
+    for( unsigned i = 0; i < 3; i++ )
     {
         MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
     }
@@ -907,7 +1016,6 @@ static void MP4_FreeBox_stts( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
     MP4_READBOX_ENTER( MP4_Box_data_stts_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_stts );
@@ -923,7 +1031,7 @@ static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_READBOX_EXIT( 0 );
     }
 
-    for( i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
+    for( unsigned int i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
     {
         MP4_GET4BYTES( p_box->data.p_stts->i_sample_count[i] );
         MP4_GET4BYTES( p_box->data.p_stts->i_sample_delta[i] );
@@ -946,7 +1054,6 @@ static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
     MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
@@ -963,7 +1070,7 @@ static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_READBOX_EXIT( 0 );
     }
 
-    for( i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
+    for( unsigned int i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
     {
         MP4_GET4BYTES( p_box->data.p_ctts->i_sample_count[i] );
         MP4_GET4BYTES( p_box->data.p_ctts->i_sample_offset[i] );
@@ -1077,7 +1184,7 @@ static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
 
     es_descriptor.p_decConfigDescr =
             calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
-    if( es_descriptor.p_decConfigDescr == NULL )
+    if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
         MP4_READBOX_EXIT( 0 );
 
     MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectTypeIndication );
@@ -1106,7 +1213,7 @@ static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
 
     es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
     es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
-    if( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL )
+    if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
         MP4_READBOX_EXIT( 0 );
 
     memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
@@ -1256,6 +1363,39 @@ static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
+static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_Box_data_dvc1_t *p_dvc1;
+
+    MP4_READBOX_ENTER( MP4_Box_data_dvc1_t );
+    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 )
+    {
+        msg_Warn( p_stream, "unsupported VC-1 profile, please report" );
+        MP4_READBOX_EXIT( 0 );
+    }
+
+
+    p_dvc1->i_vc1 = p_box->i_size - 7; /* Header + profile_level */
+
+    if( p_dvc1->i_vc1 > 0 )
+    {
+        uint8_t *p = p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 );
+        if( p )
+            memcpy( p, p_peek, i_read );
+    }
+
+#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 );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_Box_data_enda_t *p_enda;
@@ -1284,7 +1424,7 @@ static int MP4_ReadBox_gnre( stream_t *p_stream, MP4_Box_t *p_box )
 
     MP4_GET4BYTES( i_data_len );
     MP4_GETFOURCC( i_data_tag );
-    if( i_data_len < 10 || i_data_tag != VLC_FOURCC('d', 'a', 't', 'a') )
+    if( i_data_len < 10 || i_data_tag != ATOM_data )
         MP4_READBOX_EXIT( 0 );
 
     uint32_t i_version;
@@ -1313,7 +1453,7 @@ static int MP4_ReadBox_trkn( stream_t *p_stream, MP4_Box_t *p_box )
 
     MP4_GET4BYTES( i_data_len );
     MP4_GETFOURCC( i_data_tag );
-    if( i_data_len < 12 || i_data_tag != VLC_FOURCC('d', 'a', 't', 'a') )
+    if( i_data_len < 12 || i_data_tag != ATOM_data )
         MP4_READBOX_EXIT( 0 );
 
     uint32_t i_version;
@@ -1338,8 +1478,6 @@ static int MP4_ReadBox_trkn( stream_t *p_stream, MP4_Box_t *p_box )
 
 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
     p_box->data.p_sample_soun->p_qt_description = NULL;
 
@@ -1351,7 +1489,7 @@ static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_READBOX_EXIT( 1 );
     }
 
-    for( i = 0; i < 6 ; i++ )
+    for( unsigned i = 0; i < 6 ; i++ )
     {
         MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
     }
@@ -1445,13 +1583,8 @@ static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
 
     if( p_box->i_type == ATOM_drms )
     {
-        char *home = config_GetUserDir( VLC_HOME_DIR );
-        if( home != NULL )
-        {
-            p_box->data.p_sample_soun->p_drms = drms_alloc( home );
-            if( p_box->data.p_sample_soun->p_drms == NULL )
-                msg_Err( p_stream, "drms_alloc() failed" );
-        }
+        msg_Warn( p_stream, "DRM protected streams are not supported." );
+        MP4_READBOX_EXIT( 0 );
     }
 
     if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
@@ -1478,24 +1611,14 @@ static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
 {
     FREENULL( p_box->data.p_sample_soun->p_qt_description );
-
-    if( p_box->i_type == ATOM_drms )
-    {
-        if( p_box->data.p_sample_soun->p_drms )
-        {
-            drms_free( p_box->data.p_sample_soun->p_drms );
-        }
-    }
 }
 
 
 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
 
-    for( i = 0; i < 6 ; i++ )
+    for( unsigned i = 0; i < 6 ; i++ )
     {
         MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
     }
@@ -1508,7 +1631,7 @@ int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
     if( i_read > 0 )
     {
         p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
-        if( p_box->data.p_sample_vide->p_qt_image_description == NULL )
+        if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
             MP4_READBOX_EXIT( 0 );
         p_box->data.p_sample_vide->i_qt_image_description = i_read;
         memcpy( p_box->data.p_sample_vide->p_qt_image_description,
@@ -1546,13 +1669,8 @@ int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
 
     if( p_box->i_type == ATOM_drmi )
     {
-        char *home = config_GetUserDir( VLC_HOME_DIR );
-        if( home != NULL )
-        {
-            p_box->data.p_sample_vide->p_drms = drms_alloc( home );
-            if( p_box->data.p_sample_vide->p_drms == NULL )
-                msg_Err( p_stream, "drms_alloc() failed" );
-        }
+        msg_Warn( p_stream, "DRM protected streams are not supported." );
+        MP4_READBOX_EXIT( 0 );
     }
 
     MP4_ReadBoxContainerRaw( p_stream, p_box );
@@ -1571,14 +1689,6 @@ int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
 void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
 {
     FREENULL( p_box->data.p_sample_vide->p_qt_image_description );
-
-    if( p_box->i_type == ATOM_drmi )
-    {
-        if( p_box->data.p_sample_vide->p_drms )
-        {
-            drms_free( p_box->data.p_sample_vide->p_drms );
-        }
-    }
 }
 
 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
@@ -1704,28 +1814,27 @@ static int MP4_ReadBox_stsd( stream_t *p_stream, MP4_Box_t *p_box )
 
 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
 
     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
-
     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
 
-    p_box->data.p_stsz->i_entry_size =
-        calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
-    if( p_box->data.p_stsz->i_entry_size == NULL )
-        MP4_READBOX_EXIT( 0 );
-
-    if( !p_box->data.p_stsz->i_sample_size )
+    if( p_box->data.p_stsz->i_sample_size == 0 )
     {
-        for( i=0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
+        p_box->data.p_stsz->i_entry_size =
+            calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
+        if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
+            MP4_READBOX_EXIT( 0 );
+
+        for( unsigned int i = 0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
         {
             MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
         }
     }
+    else
+        p_box->data.p_stsz->i_entry_size = NULL;
 
 #ifdef MP4_VERBOSE
     msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
@@ -1750,8 +1859,6 @@ static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
@@ -1764,14 +1871,14 @@ static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
     p_box->data.p_stsc->i_sample_description_index =
         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
-    if( p_box->data.p_stsc->i_first_chunk == NULL
+    if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
      || p_box->data.p_stsc->i_samples_per_chunk == NULL
-     || p_box->data.p_stsc->i_sample_description_index == NULL )
+     || p_box->data.p_stsc->i_sample_description_index == NULL ) )
     {
         MP4_READBOX_EXIT( 0 );
     }
 
-    for( i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
+    for( unsigned int i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
     {
         MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
         MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
@@ -1788,8 +1895,6 @@ static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
 
 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_co64_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
@@ -1801,7 +1906,7 @@ static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
     if( p_box->data.p_co64->i_chunk_offset == NULL )
         MP4_READBOX_EXIT( 0 );
 
-    for( i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
+    for( unsigned int i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
     {
         if( p_box->i_type == ATOM_stco )
         {
@@ -1836,8 +1941,6 @@ static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_stss_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_stss );
@@ -1846,10 +1949,10 @@ static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
 
     p_box->data.p_stss->i_sample_number =
         calloc( p_box->data.p_stss->i_entry_count, sizeof(uint32_t) );
-    if( p_box->data.p_stss->i_sample_number == NULL )
+    if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
         MP4_READBOX_EXIT( 0 );
 
-    for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
+    for( unsigned int i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
     {
 
         MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
@@ -1878,8 +1981,6 @@ static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
@@ -1898,9 +1999,8 @@ static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_READBOX_EXIT( 0 );
     }
 
-    for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
+    for( unsigned i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
     {
-
         MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
         MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
     }
@@ -1915,8 +2015,6 @@ static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
 
 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
@@ -1924,9 +2022,11 @@ static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
     p_box->data.p_stdp->i_priority =
         calloc( i_read / 2, sizeof(uint16_t) );
 
-    for( i = 0; i < i_read / 2 ; i++ )
-    {
+    if( unlikely( !p_box->data.p_stdp->i_priority ) )
+        MP4_READBOX_EXIT( 0 );
 
+    for( unsigned i = 0; i < i_read / 2 ; i++ )
+    {
         MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
     }
 
@@ -1953,7 +2053,6 @@ static void MP4_FreeBox_padb( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
     uint32_t count;
 
     MP4_READBOX_ENTER( MP4_Box_data_padb_t );
@@ -1975,7 +2074,7 @@ static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_READBOX_EXIT( 0 );
     }
 
-    for( i = 0; i < i_read / 2 ; i++ )
+    for( unsigned int i = 0; i < i_read / 2 ; i++ )
     {
         if( i >= count )
         {
@@ -2007,8 +2106,6 @@ static void MP4_FreeBox_elst( MP4_Box_t *p_box )
 
 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
-
     MP4_READBOX_ENTER( MP4_Box_data_elst_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_elst );
@@ -2033,7 +2130,7 @@ static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
     }
 
 
-    for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
+    for( unsigned i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
     {
         if( p_box->data.p_elst->i_version == 1 )
         {
@@ -2065,14 +2162,13 @@ static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i_language;
-    unsigned int i;
 
     MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
 
     MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
 
     i_language = GetWBE( p_peek );
-    for( i = 0; i < 3; i++ )
+    for( unsigned i = 0; i < 3; i++ )
     {
         p_box->data.p_cprt->i_language[i] =
             ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
@@ -2119,12 +2215,10 @@ static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
     p_box->data.p_cmvd->i_compressed_size = i_read;
 
     if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
-        return( 1 );
+        MP4_READBOX_EXIT( 0 );
 
     /* now copy compressed data */
-    memcpy( p_box->data.p_cmvd->p_data,
-            p_peek,
-            i_read);
+    memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
 
     p_box->data.p_cmvd->b_compressed = 1;
 
@@ -2153,9 +2247,8 @@ static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
     int i_result;
 #endif
 
-    if( !( p_box->data.p_cmov = malloc( sizeof( MP4_Box_data_cmov_t ) ) ) )
+    if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
         return 0;
-    memset( p_box->data.p_cmov, 0, sizeof( MP4_Box_data_cmov_t ) );
 
     if( !p_box->p_father ||
         ( p_box->p_father->i_type != ATOM_moov &&
@@ -2270,13 +2363,12 @@ static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
 
     if( i_len > 0 )
     {
-        uint32_t i;
         p_box->data.p_rdrf->psz_ref = malloc( i_len );
         if( p_box->data.p_rdrf->psz_ref == NULL )
             MP4_READBOX_EXIT( 0 );
         i_len--;
 
-        for( i = 0; i < i_len; i++ )
+        for( unsigned i = 0; i < i_len; i++ )
         {
             MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
         }
@@ -2387,56 +2479,11 @@ 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 )
 {
-    MP4_Box_t *p_drms_box = p_box;
-    void *p_drms = NULL;
-
-    MP4_READBOX_ENTER( uint8_t );
-
-    do
-    {
-        p_drms_box = p_drms_box->p_father;
-    } while( p_drms_box && p_drms_box->i_type != ATOM_drms
-                        && p_drms_box->i_type != ATOM_drmi );
-
-    if( p_drms_box && p_drms_box->i_type == ATOM_drms )
-        p_drms = p_drms_box->data.p_sample_soun->p_drms;
-    else if( p_drms_box && p_drms_box->i_type == ATOM_drmi )
-        p_drms = p_drms_box->data.p_sample_vide->p_drms;
-
-    if( p_drms_box && p_drms )
-    {
-        int i_ret = drms_init( p_drms, p_box->i_type, p_peek, i_read );
-        if( i_ret )
-        {
-            const char *psz_error;
-
-            switch( i_ret )
-            {
-                case -1: psz_error = "unimplemented"; break;
-                case -2: psz_error = "invalid argument"; break;
-                case -3: psz_error = "could not get system key"; break;
-                case -4: psz_error = "could not get SCI data"; break;
-                case -5: psz_error = "no user key found in SCI data"; break;
-                case -6: psz_error = "invalid user key"; break;
-                default: psz_error = "unknown error"; break;
-            }
-            if MP4_BOX_TYPE_ASCII()
-                msg_Err( p_stream, "drms_init(%4.4s) failed (%s)",
-                        (char *)&p_box->i_type, psz_error );
-            else
-                msg_Err( p_stream, "drms_init(c%3.3s) failed (%s)",
-                        (char *)&p_box->i_type+1, psz_error );
-
-            drms_free( p_drms );
-
-            if( p_drms_box->i_type == ATOM_drms )
-                p_drms_box->data.p_sample_soun->p_drms = NULL;
-            else if( p_drms_box->i_type == ATOM_drmi )
-                p_drms_box->data.p_sample_vide->p_drms = NULL;
-        }
-    }
-
-    MP4_READBOX_EXIT( 1 );
+    /* 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... */
+    msg_Warn( p_stream, "DRM protected streams are not supported." );
+    return 1;
 }
 
 static int MP4_ReadBox_name( stream_t *p_stream, MP4_Box_t *p_box )
@@ -2506,7 +2553,7 @@ static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
         MP4_GET4BYTES( i_data_len );
         if( i_data_len > i_read ) i_data_len = i_read;
         MP4_GETFOURCC( i_data_tag );
-        if( (i_data_len > 0) && (i_data_tag == VLC_FOURCC('d', 'a', 't', 'a')) )
+        if( (i_data_len > 0) && (i_data_tag == ATOM_data) )
         {
             /* data box contains a version/flags field */
             uint32_t i_version;
@@ -2550,8 +2597,7 @@ static void MP4_FreeBox_0xa9xxx( MP4_Box_t *p_box )
 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
 {
     MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
-    int i;
-    for( i = 0; i < p_chpl->i_chapter; i++ )
+    for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
         free( p_chpl->chapter[i].psz_name );
 }
 
@@ -2580,7 +2626,7 @@ static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
 
         p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
         if( !p_chpl->chapter[i].psz_name )
-            goto error;
+            MP4_READBOX_EXIT( 0 );
 
         i_copy = __MIN( i_len, i_read );
         if( i_copy > 0 )
@@ -2618,14 +2664,10 @@ static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
                        p_chpl->i_chapter );
 #endif
     MP4_READBOX_EXIT( 1 );
-
-error:
-    MP4_READBOX_EXIT( 0 );
 }
 
 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
 {
-    unsigned int i;
     MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t );
 
     p_box->data.p_tref_generic->i_track_ID = NULL;
@@ -2635,7 +2677,7 @@ static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
     if( p_box->data.p_tref_generic->i_track_ID == NULL )
         MP4_READBOX_EXIT( 0 );
 
-    for( i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
+    for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
     {
         MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
     }
@@ -2703,6 +2745,248 @@ static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
+static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_pasp_t );
+
+    MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
+    MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream,
+             "read box: \"paps\" %dx%d",
+             p_box->data.p_pasp->i_horizontal_spacing,
+             p_box->data.p_pasp->i_vertical_spacing);
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_mehd_t );
+
+    MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
+    if( p_box->data.p_mehd->i_version == 1 )
+        MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
+    else /* version == 0 */
+        MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream,
+             "read box: \"mehd\" frag dur. %"PRIu64"",
+             p_box->data.p_mehd->i_fragment_duration );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_trex_t );
+    MP4_GETVERSIONFLAGS( p_box->data.p_trex );
+
+    MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
+    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
+    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
+    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
+    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream,
+             "read box: \"trex\" trackID: %"PRIu32"",
+             p_box->data.p_trex->i_track_ID );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    uint32_t i_sample_count;
+    MP4_READBOX_ENTER( MP4_Box_data_sdtp_t );
+    MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
+    MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
+    i_sample_count = i_read;
+
+    p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
+
+    if( !p_sdtp->p_sample_table )
+        MP4_READBOX_EXIT( 0 );
+
+    for( uint32_t i = 0; i < i_sample_count; i++ )
+        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,
+             "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
+                 p_sdtp->p_sample_table[0],
+                 p_sdtp->p_sample_table[1],
+                 p_sdtp->p_sample_table[2],
+                 p_sdtp->p_sample_table[3] );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
+{
+    FREENULL( p_box->data.p_sdtp->p_sample_table );
+}
+
+static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( MP4_Box_data_mfro_t );
+
+    MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
+    MP4_GET4BYTES( p_box->data.p_mfro->i_size );
+
+#ifdef MP4_VERBOSE
+    msg_Dbg( p_stream,
+             "read box: \"mfro\" size: %"PRIu32"",
+             p_box->data.p_mfro->i_size);
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    uint32_t i_number_of_entries;
+    MP4_READBOX_ENTER( MP4_Box_data_tfra_t );
+    MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
+    MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
+
+    MP4_GET4BYTES( p_tfra->i_track_ID );
+    uint32_t i_lengths = 0;
+    MP4_GET4BYTES( i_lengths );
+    MP4_GET4BYTES( p_tfra->i_number_of_entries );
+    i_number_of_entries = p_tfra->i_number_of_entries;
+    p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
+    p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
+    p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
+
+    size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
+    p_tfra->p_time = calloc( i_number_of_entries, size );
+    p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
+
+    size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
+    p_tfra->p_traf_number = calloc( i_number_of_entries, size );
+    size = 1 + p_tfra->i_length_size_of_trun_num;
+    p_tfra->p_trun_number = calloc( i_number_of_entries, size );
+    size = 1 + p_tfra->i_length_size_of_sample_num;
+    p_tfra->p_sample_number = calloc( i_number_of_entries, size );
+
+    if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
+                        || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
+        goto error;
+
+    for( uint32_t i = 0; i < i_number_of_entries; i++ )
+    {
+        if( p_tfra->i_version == 1 )
+        {
+            MP4_GET8BYTES( p_tfra->p_time[i*2] );
+            MP4_GET8BYTES( p_tfra->p_moof_offset[i*2] );
+        }
+        else
+        {
+            MP4_GET4BYTES( p_tfra->p_time[i] );
+            MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
+        }
+        switch (p_tfra->i_length_size_of_traf_num)
+        {
+            case 0:
+                MP4_GET1BYTE( p_tfra->p_traf_number[i] );
+                break;
+            case 1:
+                MP4_GET2BYTES( p_tfra->p_traf_number[i*2] );
+                break;
+            case 2:
+                MP4_GET3BYTES( p_tfra->p_traf_number[i*3] );
+                break;
+            case 3:
+                MP4_GET4BYTES( p_tfra->p_traf_number[i*4] );
+                break;
+            default:
+                goto error;
+        }
+
+        switch (p_tfra->i_length_size_of_trun_num)
+        {
+            case 0:
+                MP4_GET1BYTE( p_tfra->p_trun_number[i] );
+                break;
+            case 1:
+                MP4_GET2BYTES( p_tfra->p_trun_number[i*2] );
+                break;
+            case 2:
+                MP4_GET3BYTES( p_tfra->p_trun_number[i*3] );
+                break;
+            case 3:
+                MP4_GET4BYTES( p_tfra->p_trun_number[i*4] );
+                break;
+            default:
+                goto error;
+        }
+
+        switch (p_tfra->i_length_size_of_sample_num)
+        {
+            case 0:
+                MP4_GET1BYTE( p_tfra->p_sample_number[i] );
+                break;
+            case 1:
+                MP4_GET2BYTES( p_tfra->p_sample_number[i*2] );
+                break;
+            case 2:
+                MP4_GET3BYTES( p_tfra->p_sample_number[i*3] );
+                break;
+            case 3:
+                MP4_GET4BYTES( p_tfra->p_sample_number[i*4] );
+                break;
+            default:
+                goto error;
+        }
+    }
+
+#ifdef MP4_VERBOSE
+    if( p_tfra->i_version == 0 )
+    {
+        msg_Dbg( p_stream, "time[0]: %"PRIu32", moof_offset[0]: %"PRIx32"",
+                         p_tfra->p_time[0], p_tfra->p_moof_offset[0] );
+
+        msg_Dbg( p_stream, "time[1]: %"PRIu32", moof_offset[1]: %"PRIx32"",
+                         p_tfra->p_time[1], p_tfra->p_moof_offset[1] );
+    }
+    else
+    {
+        msg_Dbg( p_stream, "time[0]: %"PRIu64", moof_offset[0]: %"PRIx64"",
+                ((uint64_t *)(p_tfra->p_time))[0],
+                ((uint64_t *)(p_tfra->p_moof_offset))[0] );
+
+        msg_Dbg( p_stream, "time[1]: %"PRIu64", moof_offset[1]: %"PRIx64"",
+                ((uint64_t *)(p_tfra->p_time))[1],
+                ((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 );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+error:
+    MP4_READBOX_EXIT( 0 );
+}
+
+static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
+{
+    FREENULL( p_box->data.p_tfra->p_time );
+    FREENULL( p_box->data.p_tfra->p_moof_offset );
+    FREENULL( p_box->data.p_tfra->p_traf_number );
+    FREENULL( p_box->data.p_tfra->p_trun_number );
+    FREENULL( p_box->data.p_tfra->p_sample_number );
+}
+
 
 /* For generic */
 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
@@ -2781,6 +3065,7 @@ static const struct
     { ATOM_gmhd,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
     { ATOM_wave,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
     { ATOM_ilst,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
+    { ATOM_mvex,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
 
     /* specific box */
     { ATOM_ftyp,    MP4_ReadBox_ftyp,         MP4_FreeBox_ftyp },
@@ -2813,10 +3098,12 @@ static const struct
     { ATOM_cmvd,    MP4_ReadBox_cmvd,         MP4_FreeBox_cmvd },
     { ATOM_avcC,    MP4_ReadBox_avcC,         MP4_FreeBox_avcC },
     { ATOM_dac3,    MP4_ReadBox_dac3,         MP4_FreeBox_Common },
+    { ATOM_dvc1,    MP4_ReadBox_dvc1,         MP4_FreeBox_Common },
     { ATOM_enda,    MP4_ReadBox_enda,         MP4_FreeBox_Common },
     { ATOM_gnre,    MP4_ReadBox_gnre,         MP4_FreeBox_Common },
     { ATOM_trkn,    MP4_ReadBox_trkn,         MP4_FreeBox_Common },
     { ATOM_iods,    MP4_ReadBox_iods,         MP4_FreeBox_Common },
+    { ATOM_pasp,    MP4_ReadBox_pasp,         MP4_FreeBox_Common },
 
     /* Nothing to do with this box */
     { ATOM_mdat,    MP4_ReadBoxSkip,          MP4_FreeBox_Common },
@@ -2964,9 +3251,17 @@ static const struct
 
     /* found in smoothstreaming */
     { ATOM_traf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
+    { ATOM_mfra,    MP4_ReadBoxContainer,     MP4_FreeBox_Common },
     { ATOM_mfhd,    MP4_ReadBox_mfhd,         MP4_FreeBox_Common },
+    { ATOM_sidx,    MP4_ReadBox_sidx,         MP4_FreeBox_sidx },
     { ATOM_tfhd,    MP4_ReadBox_tfhd,         MP4_FreeBox_Common },
     { ATOM_trun,    MP4_ReadBox_trun,         MP4_FreeBox_trun },
+    { ATOM_trex,    MP4_ReadBox_trex,         MP4_FreeBox_Common },
+    { ATOM_mehd,    MP4_ReadBox_mehd,         MP4_FreeBox_Common },
+    { ATOM_sdtp,    MP4_ReadBox_sdtp,         MP4_FreeBox_sdtp },
+    { ATOM_tfra,    MP4_ReadBox_tfra,         MP4_FreeBox_tfra },
+    { ATOM_mfro,    MP4_ReadBox_mfro,         MP4_FreeBox_Common },
+    { ATOM_uuid,    MP4_ReadBox_uuid,         MP4_FreeBox_uuid },
 
     /* Last entry */
     { 0,              MP4_ReadBox_default,      NULL }
@@ -2979,7 +3274,7 @@ static const struct
  *****************************************************************************/
 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
 {
-    MP4_Box_t    *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
+    MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
     unsigned int i_index;
 
     if( p_box == NULL )
@@ -3071,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
  *****************************************************************************
@@ -3088,7 +3492,7 @@ MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
         return NULL;
 
     p_root->i_pos = 0;
-    p_root->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
+    p_root->i_type = ATOM_root;
     p_root->i_shortsize = 1;
     p_root->i_size = stream_Size( s );
     CreateUUID( &p_root->i_uuid, p_root->i_type );
@@ -3151,14 +3555,12 @@ static void MP4_BoxDumpStructure_Internal( stream_t *s,
     }
     else
     {
-        unsigned int i;
-
         char str[512];
         if( i_level >= (sizeof(str) - 1)/4 )
             return;
 
         memset( str, ' ', sizeof(str) );
-        for( i = 0; i < i_level; i++ )
+        for( unsigned i = 0; i < i_level; i++ )
         {
             str[i*4] = '|';
         }
@@ -3202,21 +3604,14 @@ static void get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
         *pi_number = 0;
         return;
     }
-    i_len = 0;
-    while(  (*ppsz_path)[i_len] &&
-            (*ppsz_path)[i_len] != '/' && (*ppsz_path)[i_len] != '[' )
-    {
-        i_len++;
-    }
+    i_len = strcspn( *ppsz_path, "/[" );
     if( !i_len && **ppsz_path == '/' )
     {
         i_len = 1;
     }
-    *ppsz_token = malloc( i_len + 1 );
-
-    memcpy( *ppsz_token, *ppsz_path, i_len );
-
-    (*ppsz_token)[i_len] = '\0';
+    *ppsz_token = strndup( *ppsz_path, i_len );
+    if( unlikely(!*ppsz_token) )
+        abort();
 
     *ppsz_path += i_len;
 
@@ -3285,7 +3680,7 @@ static void MP4_BoxGet_Internal( MP4_Box_t **pp_result,
         if( !strcmp( psz_token, "/" ) )
         {
             /* Find root box */
-            while( p_box && p_box->i_type != VLC_FOURCC( 'r', 'o', 'o', 't' ) )
+            while( p_box && p_box->i_type != ATOM_root )
             {
                 p_box = p_box->p_father;
             }