]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/libmp4.c
demux/mp4: bug fix for files with 'moov' at the end
[vlc] / modules / demux / mp4 / libmp4.c
index 6964b0a1d7b7741a6c2db6406b6a6e9066bedee0..838b0f965cfd2f86c29d2ab9ffbe8a67e680d88b 100644 (file)
 #endif
 
 #include "libmp4.h"
-#include "drms.h"
 #include <math.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)
-
-
 /* 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 inline uint32_t Get24bBE( const uint8_t *p )
-{
-    return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
-}
-
-static inline 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;
@@ -267,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 ) )
@@ -292,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;
@@ -312,14 +214,23 @@ 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 )
+            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;
@@ -488,6 +399,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
@@ -497,6 +410,166 @@ 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_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;
+    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;
+}
+
+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 );
+    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 )
 {
     MP4_READBOX_ENTER( MP4_Box_data_sidx_t );
@@ -565,45 +638,58 @@ static int MP4_ReadBox_tfhd(  stream_t *p_stream, MP4_Box_t *p_box )
 
     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 %"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
+    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 );
 }
@@ -640,7 +726,7 @@ static int MP4_ReadBox_trun(  stream_t *p_stream, MP4_Box_t *p_box )
     }
 
 #ifdef MP4_VERBOSE
-    msg_Dbg( p_stream, "read box: \"trun\" version %d flags 0x%x sample count %d",
+    msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %u",
                   p_box->data.p_trun->i_version,
                   p_box->data.p_trun->i_flags,
                   p_box->data.p_trun->i_sample_count );
@@ -648,7 +734,8 @@ static int MP4_ReadBox_trun(  stream_t *p_stream, MP4_Box_t *p_box )
     for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
     {
         MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
-        msg_Dbg( p_stream, "read box: \"trun\" sample %4.4d flags 0x%x duration %d size %d composition time offset %d",
+        msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
+            "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
                         i, p_sample->i_flags, p_sample->i_duration,
                         p_sample->i_size, p_sample->i_composition_time_offset );
     }
@@ -710,22 +797,24 @@ static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
     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 );
@@ -1351,6 +1440,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;
@@ -1415,13 +1537,14 @@ static int MP4_ReadBox_trkn( stream_t *p_stream, MP4_Box_t *p_box )
     uint32_t i_reserved;
     MP4_GET4BYTES( i_version );
     MP4_GET4BYTES( i_reserved );
-    MP4_GET4BYTES( p_trkn->i_track_number );
+    MP4_GET2BYTES( i_reserved );
+    MP4_GET2BYTES( p_trkn->i_track_number );
 #ifdef MP4_VERBOSE
     msg_Dbg( p_stream, "read box: \"trkn\" number=%i", p_trkn->i_track_number );
 #endif
     if( i_data_len > 15 )
     {
-       MP4_GET4BYTES( p_trkn->i_track_total );
+       MP4_GET2BYTES( p_trkn->i_track_total );
 #ifdef MP4_VERBOSE
        msg_Dbg( p_stream, "read box: \"trkn\" total=%i", p_trkn->i_track_total );
 #endif
@@ -1538,13 +1661,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 )
@@ -1571,14 +1689,6 @@ 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 );
-        }
-    }
 }
 
 
@@ -1637,13 +1747,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 );
@@ -1662,14 +1767,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 )
@@ -2460,56 +2557,12 @@ 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 );
+    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... */
+    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 )
@@ -3124,6 +3177,7 @@ 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 },
@@ -3267,6 +3321,20 @@ static const struct
     { ATOM_0xa9PRD, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
     { ATOM_0xa9grp, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
     { ATOM_0xa9lyr, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9gen, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9st3, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9ard, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9arg, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9cak, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9con, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9des, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9lnt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9phg, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9pub, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9sne, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9sol, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9thx, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
+    { ATOM_0xa9xpd, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx },
 
     { ATOM_chpl,    MP4_ReadBox_chpl,         MP4_FreeBox_chpl },
 
@@ -3286,6 +3354,7 @@ static const struct
     { 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 }
@@ -3390,6 +3459,116 @@ void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
     free( p_box );
 }
 
+/* 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 smoo box */
+    MP4_Box_t *p_chunk;
+    MP4_Box_t *p_smoo;
+
+    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_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 smoo box found!");
+        goto error;
+    }
+
+    p_chunk->p_first = p_smoo;
+    p_chunk->p_last = p_smoo;
+
+    return p_chunk;
+
+error:
+    free( p_chunk );
+    return NULL;
+}
+
+#define MAX_SKIP 8
+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_tmp_box = calloc( 1, sizeof( MP4_Box_t ) );
+    if( unlikely( p_tmp_box == NULL ) )
+        return NULL;
+
+    /* We might get a ftyp box or a SmooBox */
+    MP4_ReadBoxCommon( s, p_tmp_box );
+
+    if( (p_tmp_box->i_type == ATOM_uuid && !CmpUUID( &p_tmp_box->i_uuid, &SmooBoxUUID )) )
+    {
+        return MP4_BoxGetSmooBox( s );
+    }
+    else if( p_tmp_box->i_type == ATOM_ftyp )
+    {
+        return MP4_BoxGetRoot( s );
+    }
+    free( p_tmp_box );
+
+    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
  *****************************************************************************
@@ -3409,7 +3588,8 @@ 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;
@@ -3420,37 +3600,53 @@ MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
 
     p_stream = s;
 
+    /* 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;
+
+    p_root->i_size = stream_Size( s );
+    stream_Seek( p_stream, 0 );
+    /* Get the rest of the file */
     i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
 
-    if( i_result )
-    {
-        MP4_Box_t *p_moov;
-        MP4_Box_t *p_cmov;
+    if( !i_result )
+        goto error;
 
-        /* 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;
+    MP4_Box_t *p_moov;
+    MP4_Box_t *p_cmov;
 
-            /* get uncompressed p_moov */
-            p_moov = p_cmov->data.p_cmov->p_moov;
-            p_cmov->data.p_cmov->p_moov = NULL;
+    /* 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;
 
-            /* make p_root father of this new moov */
-            p_moov->p_father = p_root;
+        /* get uncompressed p_moov */
+        p_moov = p_cmov->data.p_cmov->p_moov;
+        p_cmov->data.p_cmov->p_moov = NULL;
 
-            /* 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;
-        }
+        /* 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 );
+    return NULL;
 }
 
 
@@ -3524,11 +3720,9 @@ static void get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
     {
         i_len = 1;
     }
-    *ppsz_token = xmalloc( 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;