]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/libmp4.c
* ./modules/demux/mp4: DRMS support.
[vlc] / modules / demux / mp4 / libmp4.c
index 3644d8ea531024ab848961935ebcebfaac6088d3..cb819ed72cc99303fe261b4bd2390433faa0514c 100644 (file)
@@ -2,8 +2,9 @@
  * libmp4.c : LibMP4 library for mp4 module for vlc
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: libmp4.c,v 1.24 2003/05/06 16:05:10 fenrir Exp $
- * Authors: Laurent Aimar <fenrir@via.ecp.fr>
+ * $Id: libmp4.c,v 1.40 2004/01/05 12:37:52 jlj Exp $
+ *
+ * Author: Laurent Aimar <fenrir@via.ecp.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 #include <stdlib.h>                                      /* malloc(), free() */
-#include <stdarg.h>
-#include <string.h>                                              /* strdup() */
-#include <errno.h>
-#include <sys/types.h>
 
 #include <vlc/vlc.h>
 #include <vlc/input.h>
 
 #ifdef HAVE_ZLIB_H
-#   include <zlib.h>                                     /* for compressed moov */
+#   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.
  *
- *  XXX: All macro are written in capital letters
- *
  *****************************************************************************/
 #define MP4_BOX_HEADERSIZE( p_box ) \
   ( 8 + ( p_box->i_shortsize == 1 ? 8 : 0 ) \
     free( p_buff ); \
     if( i_read < 0 ) \
     { \
-        msg_Warn( p_stream->p_input, "Not enougth data" ); \
+        msg_Warn( p_stream->p_input, "Not enough data" ); \
     } \
     return( i_code )
 
 
 */
 
-/* Some functions to manipulate memory */
-static uint16_t GetWLE( uint8_t *p_buff )
-{
-    return( (p_buff[0]) + ( p_buff[1] <<8 ) );
-}
-
-static uint32_t GetDWLE( uint8_t *p_buff )
-{
-    return( p_buff[0] + ( p_buff[1] <<8 ) +
-            ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
-}
-
-static uint16_t GetWBE( uint8_t *p_buff )
-{
-    return( (p_buff[0]<<8) + p_buff[1] );
-}
-
-static uint32_t Get24bBE( uint8_t *p_buff )
-{
-    return( ( p_buff[0] <<16 ) + ( p_buff[1] <<8 ) + p_buff[2] );
-}
-
-
-static uint32_t GetDWBE( uint8_t *p_buff )
+static uint32_t Get24bBE( uint8_t *p )
 {
-    return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
-            ( p_buff[2] <<8 ) + p_buff[3] );
+    return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
 }
 
-static uint64_t GetQWBE( uint8_t *p_buff )
-{
-    return( ( (uint64_t)GetDWBE( p_buff ) << 32 )|( (uint64_t)GetDWBE( p_buff + 4 ) ) );
-}
-
-
 static void GetUUID( UUID_t *p_uuid, uint8_t *p_buff )
 {
-    memcpy( p_uuid,
-            p_buff,
-            16 );
+    memcpy( p_uuid, p_buff, 16 );
 }
 
 static void CreateUUID( UUID_t *p_uuid, uint32_t i_fourcc )
@@ -173,159 +137,28 @@ static void CreateUUID( UUID_t *p_uuid, uint32_t i_fourcc )
 
 /* some functions for mp4 encoding of variables */
 
-void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
+static void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
 {
     int i_day;
     int i_hour;
     int i_min;
     int i_sec;
 
+    /* date begin at 1 jan 1904 */
+    i_date += ((1904U * 365) + 17) * 24 * 60 * 60;
+
     i_day = i_date / ( 60*60*24);
     i_hour = ( i_date /( 60*60 ) ) % 60;
     i_min  = ( i_date / 60 ) % 60;
     i_sec =  i_date % 60;
-    /* FIXME do it correctly, date begin at 1 jan 1904 */
     sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds",
                    i_day, i_hour, i_min, i_sec );
 }
 
-#if 0
-static void DataDump( uint8_t *p_data, int i_data )
-{
-    int i;
-    fprintf( stderr, "\nDumping %d bytes\n", i_data );
-    for( i = 0; i < i_data; i++ )
-    {
-        int c;
-
-        c = p_data[i];
-        if( c < 32 || c > 127 ) c = '.';
-        fprintf( stderr, "%c", c );
-        if( i % 60 == 59 ) fprintf( stderr, "\n" );
-    }
-    fprintf( stderr, "\n" );
-}
-#endif
-
 /*****************************************************************************
- * Some basic functions to manipulate stream more easily in vlc
- *
- * MP4_TellAbsolute get file position
- *
- * MP4_SeekAbsolute seek in the file
- *
- * MP4_ReadData read data from the file in a buffer
- *
+ * Some prototypes.
  *****************************************************************************/
-off_t MP4_TellAbsolute( input_thread_t *p_input )
-{
-    off_t i_pos;
-
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-
-    i_pos= p_input->stream.p_selected_area->i_tell;
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    return( i_pos );
-}
-
-int MP4_SeekAbsolute( input_thread_t *p_input,
-                      off_t i_pos)
-{
-    off_t i_filepos;
-
-    //msg_Warn( p_input, "seek to %lld/%lld", i_pos, p_input->stream.p_selected_area->i_size  );
-
-    if( p_input->stream.p_selected_area->i_size > 0 &&
-        i_pos >= p_input->stream.p_selected_area->i_size )
-    {
-        msg_Warn( p_input, "seek:after end of file" );
-        return VLC_EGENERIC;
-    }
-
-    i_filepos = MP4_TellAbsolute( p_input );
-
-    if( i_filepos == i_pos )
-    {
-        return VLC_SUCCESS;
-    }
-
-    if( p_input->stream.b_seekable &&
-        ( p_input->stream.i_method == INPUT_METHOD_FILE ||
-          i_pos - i_filepos < 0 ||
-          i_pos - i_filepos > 1024 ) )
-    {
-        input_AccessReinit( p_input );
-        p_input->pf_seek( p_input, i_pos );
-        return VLC_SUCCESS;
-    }
-    else if( i_pos - i_filepos > 0 )
-    {
-        data_packet_t   *p_data;
-        int             i_skip = i_pos - i_filepos;
-
-        msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
-
-        while (i_skip > 0 )
-        {
-            int i_read;
-
-            i_read = input_SplitBuffer( p_input, &p_data, 
-                                        __MIN( 4096, i_skip ) );
-            if( i_read < 0 )
-            {
-                msg_Warn( p_input, "seek:cannot read" );
-                return VLC_EGENERIC;
-            }
-            i_skip -= i_read;
-
-            input_DeletePacket( p_input->p_method_data, p_data );
-            if( i_read == 0 && i_skip > 0 )
-            {
-                msg_Warn( p_input, "seek:cannot read" );
-                return VLC_EGENERIC;
-            }
-        }
-        return VLC_SUCCESS;
-    }
-    else
-    {
-        msg_Warn( p_input, "seek:failed" );
-        return VLC_EGENERIC;
-    }
-}
-
-/* return 1 if success, 0 if fail */
-int MP4_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
-{
-    data_packet_t *p_data;
-
-    int i_read;
-
-
-    if( !i_size )
-    {
-        return( VLC_SUCCESS );
-    }
-
-    do
-    {
-        i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
-        if( i_read <= 0 )
-        {
-            return( VLC_EGENERIC );
-        }
-        memcpy( p_buff, p_data->p_payload_start, i_read );
-        input_DeletePacket( p_input->p_method_data, p_data );
-
-        p_buff += i_read;
-        i_size -= i_read;
-
-    } while( i_size );
-
-    return( VLC_SUCCESS );
-}
+static MP4_Box_t *MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_father );
 
 /*****************************************************************************
  * Some basic functions to manipulate MP4_Stream_t, an abstraction o p_input
@@ -410,7 +243,7 @@ int MP4_ReadStream( MP4_Stream_t *p_stream, uint8_t *p_buff, int i_size )
     }
     else
     {
-        return( MP4_ReadData( p_stream->p_input, p_buff, i_size ) );
+        return( stream_Read( p_stream->p_input->s, p_buff, i_size ) < i_size ? VLC_EGENERIC : VLC_SUCCESS);
     }
 }
 
@@ -432,13 +265,13 @@ int MP4_PeekStream( MP4_Stream_t *p_stream, uint8_t **pp_peek, int i_size )
         if( p_stream->p_input->stream.p_selected_area->i_size > 0 )
         {
             int64_t i_max =
-                p_stream->p_input->stream.p_selected_area->i_size - MP4_TellAbsolute( p_stream->p_input );
+                p_stream->p_input->stream.p_selected_area->i_size - stream_Tell( p_stream->p_input->s );
             if( i_size > i_max )
             {
                 i_size = i_max;
             }
         }
-        return( input_Peek( p_stream->p_input, pp_peek, i_size ) );
+        return( stream_Peek( p_stream->p_input->s, pp_peek, i_size ) );
     }
 }
 
@@ -454,7 +287,7 @@ off_t MP4_TellStream( MP4_Stream_t *p_stream )
     }
     else
     {
-        return( MP4_TellAbsolute( p_stream->p_input ) );
+        return( stream_Tell( p_stream->p_input->s ) );
     }
 }
 
@@ -478,7 +311,7 @@ int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos)
     }
     else
     {
-        return( MP4_SeekAbsolute( p_stream->p_input, i_pos ) );
+        return( stream_Seek( p_stream->p_input->s, (int64_t)i_pos ) );
     }
 }
 
@@ -492,7 +325,7 @@ int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos)
  *
  * RETURN : 0 if it fail, 1 otherwise
  *****************************************************************************/
-int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     int      i_read;
     uint8_t  *p_peek;
@@ -549,11 +382,11 @@ int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 
 
 /*****************************************************************************
- * MP4_MP4_NextBox : Go to the next box
+ * MP4_NextBox : Go to the next box
  *****************************************************************************
  * if p_box == NULL, go to the next box in witch we are( at the begining ).
  *****************************************************************************/
-int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_Box_t box;
 
@@ -579,16 +412,6 @@ int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     }
     return( MP4_SeekStream( p_stream, p_box->i_size + p_box->i_pos ) ? 0 : 1 );
 }
-/*****************************************************************************
- * MP4_MP4_GotoBox : Go to this particular box
- *****************************************************************************
- * RETURN : 0 if it fail, 1 otherwise
- *****************************************************************************/
-int MP4_GotoBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
-{
-    return( MP4_SeekStream( p_stream, p_box->i_pos ) ? 0 : 1 );
-}
-
 
 /*****************************************************************************
  * For all known box a loader is given,
@@ -596,7 +419,7 @@ int MP4_GotoBox( MP4_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
  *****************************************************************************/
-int MP4_ReadBoxContainerRaw( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
+static int MP4_ReadBoxContainerRaw( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
 {
     MP4_Box_t *p_box;
 
@@ -609,35 +432,27 @@ int MP4_ReadBoxContainerRaw( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
 
     do
     {
-        p_box = malloc( sizeof( MP4_Box_t ) );
-
-        if( MP4_ReadBox( p_stream, p_box , p_container ) )
+        if( ( p_box = MP4_ReadBox( p_stream, p_container ) ) == NULL )
         {
-            /* chain this box with the father and the other at same level */
-            if( !p_container->p_first )
-            {
-                p_container->p_first = p_box;
-            }
-            else
-            {
-                p_container->p_last->p_next = p_box;
-            }
-            p_container->p_last = p_box;
+            break;
+        }
+        /* chain this box with the father and the other at same level */
+        if( !p_container->p_first )
+        {
+            p_container->p_first = p_box;
         }
         else
         {
-            /* free memory */
-            free( p_box );
-            break;
+            p_container->p_last->p_next = p_box;
         }
-
-    }while( MP4_NextBox( p_stream, p_box ) == 1 );
+        p_container->p_last = p_box;
+    } while( MP4_NextBox( p_stream, p_box ) == 1 );
 
     return( 1 );
 }
 
 
-int MP4_ReadBoxContainer( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
+static int MP4_ReadBoxContainer( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
 {
     if( p_container->i_size <= (size_t)MP4_BOX_HEADERSIZE(p_container ) + 8 )
     {
@@ -651,13 +466,39 @@ int MP4_ReadBoxContainer( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
     return( MP4_ReadBoxContainerRaw( p_stream, p_container ) );
 }
 
-void MP4_FreeBox_Common( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_Common( MP4_Box_t *p_box )
 {
     /* Up to now do nothing */
 }
 
-int MP4_ReadBoxSkip( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBoxSkip( MP4_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->i_type == FOURCC_free )
+    {
+        uint8_t *p_peek;
+        int     i_read;
+        vlc_fourcc_t i_fcc;
+
+        i_read  = MP4_PeekStream( p_stream, &p_peek, 44 );
+
+        p_peek += MP4_BOX_HEADERSIZE( p_box ) + 4;
+        i_read -= MP4_BOX_HEADERSIZE( p_box ) + 4;
+
+        if( i_read >= 8 )
+        {
+            i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
+
+            if( i_fcc == FOURCC_cmov || i_fcc == FOURCC_mvhd )
+            {
+                msg_Warn( p_stream->p_input, "Detected moov hidden in a free box ..." );
+
+                p_box->i_type = FOURCC_foov;
+                return MP4_ReadBoxContainer( p_stream, p_box );
+            }
+        }
+    }
     /* Nothing to do */
 #ifdef MP4_VERBOSE
     msg_Dbg( p_stream->p_input, "Skip box: \"%4.4s\"",
@@ -666,7 +507,7 @@ int MP4_ReadBoxSkip( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     return( 1 );
 }
 
-int MP4_ReadBox_ftyp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_ftyp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
 
@@ -692,13 +533,13 @@ int MP4_ReadBox_ftyp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_ftyp( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_ftyp->i_compatible_brands );
 }
 
 
-int MP4_ReadBox_mvhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_mvhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 #ifdef MP4_VERBOSE
@@ -770,7 +611,7 @@ int MP4_ReadBox_mvhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-int MP4_ReadBox_tkhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_tkhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 #ifdef MP4_VERBOSE
@@ -834,7 +675,7 @@ int MP4_ReadBox_tkhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-int MP4_ReadBox_mdhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_mdhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
     uint16_t i_language;
@@ -887,7 +728,7 @@ int MP4_ReadBox_mdhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-int MP4_ReadBox_hdlr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_hdlr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
 
@@ -908,12 +749,12 @@ int MP4_ReadBox_hdlr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_hdlr( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_hdlr->psz_name );
 }
 
-int MP4_ReadBox_vmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_vmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -937,7 +778,7 @@ int MP4_ReadBox_vmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-int MP4_ReadBox_smhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_smhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
 
@@ -957,7 +798,7 @@ int MP4_ReadBox_smhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-int MP4_ReadBox_hmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_hmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
 
@@ -981,7 +822,7 @@ int MP4_ReadBox_hmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-int MP4_ReadBox_url( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_url( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_url_t );
 
@@ -997,12 +838,12 @@ int MP4_ReadBox_url( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-void MP4_FreeBox_url( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_url( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_url->psz_location )
 }
 
-int MP4_ReadBox_urn( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_urn( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_urn_t );
 
@@ -1018,14 +859,14 @@ int MP4_ReadBox_urn( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 #endif
     MP4_READBOX_EXIT( 1 );
 }
-void MP4_FreeBox_urn( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_urn( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_urn->psz_name );
     FREE( p_box->data.p_urn->psz_location );
 }
 
 
-int MP4_ReadBox_dref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_dref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_dref_t );
 
@@ -1045,7 +886,7 @@ int MP4_ReadBox_dref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-int MP4_ReadBox_stts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
     MP4_READBOX_ENTER( MP4_Box_data_stts_t );
@@ -1072,13 +913,13 @@ int MP4_ReadBox_stts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_stts( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_stts( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_stts->i_sample_count );
     FREE( p_box->data.p_stts->i_sample_delta );
 }
 
-int MP4_ReadBox_ctts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_ctts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
     MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
@@ -1106,7 +947,7 @@ int MP4_ReadBox_ctts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_ctts( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_ctts->i_sample_count );
     FREE( p_box->data.p_ctts->i_sample_offset );
@@ -1127,7 +968,7 @@ static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t  *i_read )
     return( i_len );
 }
 
-int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
 #define es_descriptor p_box->data.p_esds->es_descriptor
     unsigned int i_len;
@@ -1144,6 +985,11 @@ int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     {
         i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
 
+#ifdef MP4_VERBOSE
+        msg_Dbg( p_stream->p_input, "Found esds MPEG4ESDescr (%dBytes)",
+                 i_len );
+#endif
+
         MP4_GET2BYTES( es_descriptor.i_ES_ID );
         MP4_GET1BYTE( i_flags );
         es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
@@ -1179,11 +1025,17 @@ int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 
     if( i_type != 0x04)/* MP4DecConfigDescrTag */
     {
-        es_descriptor.p_decConfigDescr = NULL;
-        MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
+         es_descriptor.p_decConfigDescr = NULL;
+         MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
     }
 
     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
+
+#ifdef MP4_VERBOSE
+        msg_Dbg( p_stream->p_input, "Found esds MP4DecConfigDescr (%dBytes)",
+                 i_len );
+#endif
+
     es_descriptor.p_decConfigDescr =
             malloc( sizeof( MP4_descriptor_decoder_config_t ));
 
@@ -1203,6 +1055,12 @@ int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     }
 
     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
+
+#ifdef MP4_VERBOSE
+        msg_Dbg( p_stream->p_input, "Found esds MP4DecSpecificDescr (%dBytes)",
+                 i_len );
+#endif
+
     es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
     es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
     memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
@@ -1213,7 +1071,7 @@ int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 #undef es_descriptor
 }
 
-void MP4_FreeBox_esds( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_esds( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_esds->es_descriptor.psz_URL );
     if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
@@ -1223,12 +1081,20 @@ void MP4_FreeBox_esds( input_thread_t *p_input, MP4_Box_t *p_box )
     FREE( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
 }
 
-int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
     MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
 
+    /* Sanity check needed because the "wave" box does also contain an
+     * "mp4a" box that we don't understand. */
+    if( i_read < 28 )
+    {
+        i_read -= 30;
+        MP4_READBOX_EXIT( 1 );
+    }
+
     for( i = 0; i < 6 ; i++ )
     {
         MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
@@ -1239,19 +1105,9 @@ int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     /*
      * XXX hack -> produce a copy of the nearly complete chunk
      */
-    if( i_read > 0 )
-    {
-        p_box->data.p_sample_soun->i_qt_description = i_read;
-        p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
-        memcpy( p_box->data.p_sample_soun->p_qt_description,
-                p_peek,
-                i_read );
-    }
-    else
-    {
-        p_box->data.p_sample_soun->i_qt_description = 0;
-        p_box->data.p_sample_soun->p_qt_description = NULL;
-    }
+    p_box->data.p_sample_soun->i_qt_description = i_read;
+    p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
+    memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
 
     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
@@ -1291,6 +1147,79 @@ int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
         msg_Dbg( p_stream->p_input, "Read Box: \"soun\" mp4 or qt1/2 (rest="I64Fd")", i_read );
         MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
     }
+
+    p_box->data.p_sample_soun->p_drms =
+        p_box->i_type == FOURCC_drms ? drms_alloc() : NULL;
+
+    if( p_box->data.p_sample_soun->p_drms )
+    {
+        FILE *file;
+        char *psz_homedir;
+        char *psz_filename;
+        uint32_t p_user_key[ 4 ];
+
+        int i_ret = 0;
+        vlc_bool_t b_key = VLC_FALSE;
+
+        psz_filename = NULL;
+        psz_homedir = p_stream->p_input->p_vlc->psz_homedir;
+
+#define DRMS_FILENAME "drms"
+
+        if( psz_homedir != NULL )
+        {
+            psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/"
+                                           DRMS_FILENAME) +
+                                           strlen( psz_homedir ) );
+            if( psz_filename != NULL )
+            {
+                sprintf( psz_filename, "%s/" CONFIG_DIR "/" DRMS_FILENAME,
+                         psz_homedir );
+
+                file = fopen( psz_filename, "r" );
+                if( file != NULL )
+                {
+                    b_key = fread( p_user_key, sizeof(uint32_t),
+                                   4, file ) == 4 ? VLC_TRUE : VLC_FALSE;
+                    fclose( file );
+                }
+            }
+        }
+
+        if( b_key == VLC_FALSE )
+        {
+            i_ret = drms_get_user_key( NULL, p_user_key );
+        }
+
+        if( !i_ret )
+        {
+            if( b_key == VLC_FALSE && psz_filename != NULL )
+            {
+                file = fopen( psz_filename, "w" );
+                if( file != NULL )
+                {
+                    fwrite( p_user_key, sizeof(uint32_t), 4, file );
+                    fclose( file );
+                }
+            }
+
+            i_ret = drms_init( p_box->data.p_sample_soun->p_drms,
+                               DRMS_INIT_UKEY, (uint8_t *)p_user_key,
+                               sizeof(p_user_key) );
+        }
+
+        if( psz_filename != NULL )
+        {
+            free( (void *)psz_filename );
+        }
+
+        if( i_ret )
+        {
+            drms_free( p_box->data.p_sample_soun->p_drms );
+            p_box->data.p_sample_soun->p_drms = NULL;
+        }
+    }
+
     MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
 
 #ifdef MP4_VERBOSE
@@ -1305,7 +1234,19 @@ int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
+{
+    if( p_box->i_type == FOURCC_drms )
+    {
+        if( p_box->data.p_sample_soun->p_drms )
+        {
+            drms_free( p_box->data.p_sample_soun->p_drms );
+        }
+    }
+}
+
+
+static int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1371,7 +1312,7 @@ int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
 
     MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
@@ -1393,7 +1334,7 @@ int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 }
 
 
-int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1425,12 +1366,12 @@ int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_stsz( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_stsz->i_entry_size );
 }
 
-int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1462,14 +1403,14 @@ int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_stsc( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_stsc->i_first_chunk );
     FREE( p_box->data.p_stsc->i_samples_per_chunk );
     FREE( p_box->data.p_stsc->i_sample_description_index );
 }
 
-int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1510,12 +1451,12 @@ int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_stco_co64( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_co64->i_chunk_offset );
 }
 
-int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1544,12 +1485,12 @@ int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_stss( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_stss( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_stss->i_sample_number )
 }
 
-int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1581,14 +1522,14 @@ int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_stsh( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_stsh->i_shadowed_sample_number )
     FREE( p_box->data.p_stsh->i_sync_sample_number )
 }
 
 
-int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1613,12 +1554,12 @@ int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_stdp( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_stdp->i_priority )
 }
 
-int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1657,7 +1598,7 @@ int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_padb( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_padb( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_padb->i_reserved1 );
     FREE( p_box->data.p_padb->i_pad2 );
@@ -1665,7 +1606,7 @@ void MP4_FreeBox_padb( input_thread_t *p_input, MP4_Box_t *p_box )
     FREE( p_box->data.p_padb->i_pad1 );
 }
 
-int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i;
 
@@ -1715,7 +1656,7 @@ int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_elst( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_elst( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_elst->i_segment_duration );
     FREE( p_box->data.p_elst->i_media_time );
@@ -1723,7 +1664,7 @@ void MP4_FreeBox_elst( input_thread_t *p_input, MP4_Box_t *p_box )
     FREE( p_box->data.p_elst->i_media_rate_fraction );
 }
 
-int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     unsigned int i_language;
     unsigned int i;
@@ -1752,13 +1693,13 @@ int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-void MP4_FreeBox_cprt( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_cprt->psz_notice );
 }
 
 
-int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
 
@@ -1771,7 +1712,7 @@ int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
 
@@ -1799,16 +1740,15 @@ int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 
     MP4_READBOX_EXIT( 1 );
 }
-void MP4_FreeBox_cmvd( input_thread_t *p_input, MP4_Box_t *p_box )
+static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_cmvd->p_data );
 }
 
 
-int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_Stream_t *p_stream_memory;
-    MP4_Box_t *p_umov;
 
     MP4_Box_t *p_dcom;
     MP4_Box_t *p_cmvd;
@@ -1826,8 +1766,8 @@ int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     }
     memset( p_box->data.p_cmov, 0, sizeof( MP4_Box_data_cmov_t ) );
 
-    if( !( p_box->p_father )||
-        ( p_box->p_father->i_type != FOURCC_moov ) )
+    if( !p_box->p_father ||
+        ( p_box->p_father->i_type != FOURCC_moov && p_box->p_father->i_type != FOURCC_foov) )
     {
         msg_Warn( p_stream->p_input, "Read box: \"cmov\" box alone" );
         return( 1 );
@@ -1838,9 +1778,9 @@ int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
         return( 0 );
     }
 
-    if( !( p_dcom = MP4_FindBox( p_box, FOURCC_dcom ) )||
-        !( p_cmvd = MP4_FindBox( p_box, FOURCC_cmvd ) )||
-        !( p_cmvd->data.p_cmvd->p_data ) )
+    if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
+        ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
+        p_cmvd->data.p_cmvd->p_data == NULL )
     {
         msg_Warn( p_stream->p_input, "Read Box: \"cmov\" incomplete" );
         return( 1 );
@@ -1917,31 +1857,25 @@ int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     msg_Dbg( p_stream->p_input,
              "Read Box: \"cmov\" box succesfully uncompressed" );
 
-    //DataDump( p_data, p_cmvd->data.p_cmvd->i_uncompressed_size );
     /* now create a memory stream */
     p_stream_memory = MP4_MemoryStream( p_stream->p_input,
                                         p_cmvd->data.p_cmvd->i_uncompressed_size,
                                         p_cmvd->data.p_cmvd->p_data );
 
-    //DataDump( p_stream_memory->p_buffer, p_stream_memory->i_stop );
-
     /* and read uncompressd moov */
-    p_umov = malloc( sizeof( MP4_Box_t ) );
-
-    i_result = MP4_ReadBox( p_stream_memory, p_umov, NULL );
+    p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
 
-    p_box->data.p_cmov->p_moov = p_umov;
     free( p_stream_memory );
 
 #ifdef MP4_VERBOSE
     msg_Dbg( p_stream->p_input,
              "Read Box: \"cmov\" compressed movie header completed" );
 #endif
-    return( i_result );
+    return( p_box->data.p_cmov->p_moov ? 1 : 0 );
 #endif /* HAVE_ZLIB_H */
 }
 
-int MP4_ReadBox_rdrf( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_rdrf( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     uint32_t i_len;
     MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
@@ -1973,13 +1907,14 @@ int MP4_ReadBox_rdrf( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 #endif
     MP4_READBOX_EXIT( 1 );
 }
-void MP4_FreeBox_rdrf( input_thread_t *p_input, MP4_Box_t *p_box )
+
+static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
 {
     FREE( p_box->data.p_rdrf->psz_ref )
 }
 
 
-int MP4_ReadBox_rmdr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_rmdr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
 
@@ -1995,7 +1930,7 @@ int MP4_ReadBox_rmdr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-int MP4_ReadBox_rmqu( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_rmqu( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
 
@@ -2009,7 +1944,7 @@ int MP4_ReadBox_rmqu( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
-int MP4_ReadBox_rmvc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+static int MP4_ReadBox_rmvc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 {
     MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
     MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
@@ -2030,6 +1965,81 @@ int MP4_ReadBox_rmvc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     MP4_READBOX_EXIT( 1 );
 }
 
+static int MP4_ReadBox_iviv( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_READBOX_ENTER( uint8_t );
+
+    if( i_read >= sizeof(uint32_t) * 4 )
+    {
+        MP4_Box_t *p_drms_box = p_box;
+
+        do
+        {
+            p_drms_box = p_drms_box->p_father;
+        } while( p_drms_box && p_drms_box->i_type != FOURCC_drms );
+
+        if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
+        {
+            if( drms_init( p_drms_box->data.p_sample_soun->p_drms,
+                           DRMS_INIT_IVIV, p_peek, sizeof(uint32_t) * 4 ) )
+            {
+                drms_free( p_drms_box->data.p_sample_soun->p_drms );
+                p_drms_box->data.p_sample_soun->p_drms = NULL;
+            }
+        }
+    }
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static int MP4_ReadBox_name( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_Box_t *p_drms_box = p_box;
+
+    MP4_READBOX_ENTER( uint8_t );
+
+    do
+    {
+        p_drms_box = p_drms_box->p_father;
+    } while( p_drms_box && p_drms_box->i_type != FOURCC_drms );
+
+    if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
+    {
+        if( drms_init( p_drms_box->data.p_sample_soun->p_drms,
+                       DRMS_INIT_NAME, p_peek, strlen( p_peek ) ) )
+        {
+            drms_free( p_drms_box->data.p_sample_soun->p_drms );
+            p_drms_box->data.p_sample_soun->p_drms = NULL;
+        }
+    }
+
+    MP4_READBOX_EXIT( 1 );
+}
+
+static int MP4_ReadBox_priv( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
+{
+    MP4_Box_t *p_drms_box = p_box;
+
+    MP4_READBOX_ENTER( uint8_t );
+
+    do
+    {
+        p_drms_box = p_drms_box->p_father;
+    } while( p_drms_box && p_drms_box->i_type != FOURCC_drms );
+
+    if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
+    {
+        if( drms_init( p_drms_box->data.p_sample_soun->p_drms,
+                       DRMS_INIT_PRIV, p_peek, i_read ) )
+        {
+            drms_free( p_drms_box->data.p_sample_soun->p_drms );
+            p_drms_box->data.p_sample_soun->p_drms = NULL;
+        }
+    }
+
+    MP4_READBOX_EXIT( 1 );
+}
+
 /**** ------------------------------------------------------------------- ****/
 /****                   "Higher level" Functions                          ****/
 /**** ------------------------------------------------------------------- ****/
@@ -2038,7 +2048,7 @@ static struct
 {
     uint32_t i_type;
     int  (*MP4_ReadBox_function )( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
-    void (*MP4_FreeBox_function )( input_thread_t *p_input, MP4_Box_t *p_box );
+    void (*MP4_FreeBox_function )( MP4_Box_t *p_box );
 } MP4_Box_Function [] =
 {
     /* Containers */
@@ -2056,6 +2066,8 @@ static struct
     { FOURCC_rmra,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
     { FOURCC_rmda,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
     { FOURCC_tref,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
+    { FOURCC_gmhd,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
+    { FOURCC_wave,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
 
     /* specific box */
     { FOURCC_ftyp,  MP4_ReadBox_ftyp,       MP4_FreeBox_ftyp },
@@ -2113,6 +2125,8 @@ static struct
     { FOURCC_MAC3,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
     { FOURCC_MAC6,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
     { FOURCC_Qclp,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
+    { FOURCC_samr,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
+    { FOURCC_OggS,  MP4_ReadBox_sample_soun,    MP4_FreeBox_Common },
 
     { FOURCC_vide,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
     { FOURCC_mp4v,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
@@ -2137,6 +2151,8 @@ static struct
     { FOURCC_mjht,  NULL,                       NULL },
     { FOURCC_dvc,   MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
     { FOURCC_dvp,   MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
+    { FOURCC_VP31,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
+    { FOURCC_vp31,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
 
     { FOURCC_jpeg,  MP4_ReadBox_sample_vide,    MP4_FreeBox_Common },
 
@@ -2159,6 +2175,13 @@ static struct
     { FOURCC_rmqu,  MP4_ReadBox_rmqu,           MP4_FreeBox_Common },
     { FOURCC_rmvc,  MP4_ReadBox_rmvc,           MP4_FreeBox_Common },
 
+    { FOURCC_drms,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
+    { FOURCC_sinf,  MP4_ReadBoxContainer,       MP4_FreeBox_Common },
+    { FOURCC_schi,  MP4_ReadBoxContainer,       MP4_FreeBox_Common },
+    { FOURCC_iviv,  MP4_ReadBox_iviv,           MP4_FreeBox_Common },
+    { FOURCC_name,  MP4_ReadBox_name,           MP4_FreeBox_Common },
+    { FOURCC_priv,  MP4_ReadBox_priv,           MP4_FreeBox_Common },
+
     /* Last entry */
     { 0,            NULL,                   NULL }
 };
@@ -2169,20 +2192,22 @@ static struct
  * MP4_ReadBox : parse the actual box and the children
  *  XXX : Do not go to the next box
  *****************************************************************************/
-int MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
+static MP4_Box_t *MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_father )
 {
-    int i_result;
+    MP4_Box_t    *p_box = malloc( sizeof( MP4_Box_t ) );
     unsigned int i_index;
 
     if( !MP4_ReadBoxCommon( p_stream, p_box ) )
     {
         msg_Warn( p_stream->p_input, "Cannot read one box" );
-        return( 0 );
+        free( p_box );
+        return NULL;
     }
     if( !p_box->i_size )
     {
         msg_Dbg( p_stream->p_input, "Found an empty box (null size)" );
-        return( 0 );
+        free( p_box );
+        return NULL;
     }
     p_box->p_father = p_father;
 
@@ -2200,124 +2225,16 @@ int MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
         msg_Warn( p_stream->p_input,
                   "Unknown box type %4.4s (uncompletetly loaded)",
                   (char*)&p_box->i_type );
-        return( 1 );
     }
-    else
+    else if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
     {
-        i_result =
-           (MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box );
+        free( p_box );
+        return NULL;
     }
 
-    return( i_result );
+    return p_box;
 }
 
-#if 0
-/*****************************************************************************
- * MP4_CountBox: given a box, count how many child have the requested type
- * FIXME : support GUUID
- *****************************************************************************/
-int MP4_CountBox( MP4_Box_t *p_box, uint32_t i_type )
-{
-    unsigned int i_count;
-    MP4_Box_t *p_child;
-
-    if( !p_box )
-    {
-        return( 0 );
-    }
-
-    i_count = 0;
-    p_child = p_box->p_first;
-    while( p_child )
-    {
-        if( p_child->i_type == i_type )
-        {
-            i_count++;
-        }
-        p_child = p_child->p_next;
-    }
-
-    return( i_count );
-}
-#endif
-
-/*****************************************************************************
- * MP4_FindBox:  find first box with i_type child of p_box
- *      return NULL if not found
- *****************************************************************************/
-
-MP4_Box_t *MP4_FindBox( MP4_Box_t *p_box, uint32_t i_type )
-{
-    MP4_Box_t *p_child;
-
-    if( !p_box )
-    {
-        return( NULL );
-    }
-
-    p_child = p_box->p_first;
-    while( p_child )
-    {
-        if( p_child->i_type == i_type )
-        {
-            return( p_child );
-        }
-        p_child = p_child->p_next;
-    }
-
-    return( NULL );
-}
-
-
-#if 0
-/*****************************************************************************
- * MP4_FindNextBox:  find next box with thesame type and at the same level
- *                  than p_box
- *****************************************************************************/
-MP4_Box_t *MP4_FindNextBox( MP4_Box_t *p_box )
-{
-    MP4_Box_t *p_next;
-
-    if( !p_box )
-    {
-        return( NULL );
-    }
-
-    p_next = p_box->p_next;
-    while( p_next )
-    {
-        if( p_next->i_type == p_box->i_type )
-        {
-            return( p_next );
-        }
-        p_next = p_next->p_next;
-    }
-    return( NULL );
-}
-/*****************************************************************************
- * MP4_FindNbBox:  find the box i_number
- *****************************************************************************/
-MP4_Box_t *MP4_FindNbBox( MP4_Box_t *p_box, uint32_t i_number )
-{
-    MP4_Box_t *p_child = p_box->p_first;
-
-    if( !p_child )
-    {
-        return( NULL );
-    }
-
-    while( i_number )
-    {
-        if( !( p_child = p_child->p_next ) )
-        {
-            return( NULL );
-        }
-        i_number--;
-    }
-    return( p_child );
-}
-#endif
-
 /*****************************************************************************
  * MP4_FreeBox : free memory after read with MP4_ReadBox and all
  * the children
@@ -2325,21 +2242,19 @@ MP4_Box_t *MP4_FindNbBox( MP4_Box_t *p_box, uint32_t i_number )
 void MP4_BoxFree( input_thread_t *p_input, MP4_Box_t *p_box )
 {
     unsigned int i_index;
-
-    MP4_Box_t *p_child;
-    MP4_Box_t *p_next;
+    MP4_Box_t    *p_child;
 
     if( !p_box )
     {
         return; /* hehe */
     }
-    p_child = p_box->p_first;
-    while( p_child )
+
+    for( p_child = p_box->p_first; p_child != NULL; )
     {
+        MP4_Box_t *p_next;
+
         p_next = p_child->p_next;
         MP4_BoxFree( p_input, p_child );
-        /* MP4_FreeBoxChildren have free all data expect p_child itself */
-        free( p_child );
         p_child = p_next;
     }
 
@@ -2363,16 +2278,13 @@ void MP4_BoxFree( input_thread_t *p_input, MP4_Box_t *p_box )
         }
         else
         {
-            MP4_Box_Function[i_index].MP4_FreeBox_function( p_input, p_box );
+            MP4_Box_Function[i_index].MP4_FreeBox_function( p_box );
         }
 
         free( p_box->data.p_data );
-        p_box->data.p_data = NULL;
     }
 
-    p_box->p_first = NULL;
-    p_box->p_last = NULL;
-
+    free( p_box );
 }
 
 /*****************************************************************************
@@ -2381,12 +2293,13 @@ void MP4_BoxFree( input_thread_t *p_input, MP4_Box_t *p_box )
  *  The first box is a virtual box "root" and is the father for all first
  *  level boxes for the file, a sort of virtual contener
  *****************************************************************************/
-int MP4_BoxGetRoot( input_thread_t *p_input, MP4_Box_t *p_root )
+MP4_Box_t *MP4_BoxGetRoot( input_thread_t *p_input )
 {
+    MP4_Box_t *p_root;
     MP4_Stream_t *p_stream;
     int i_result;
 
-    MP4_SeekAbsolute( p_input, 0 );     /* Go to the begining */
+    p_root = malloc( sizeof( MP4_Box_t ) );
     p_root->i_pos = 0;
     p_root->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
     p_root->i_shortsize = 1;
@@ -2413,8 +2326,10 @@ int MP4_BoxGetRoot( input_thread_t *p_input, MP4_Box_t *p_root )
 
         /* check if there is a cmov, if so replace
           compressed moov by  uncompressed one */
-        if( ( p_moov = MP4_FindBox( p_root, FOURCC_moov ) )&&
-            ( p_cmov = MP4_FindBox( p_moov, FOURCC_cmov ) ) )
+        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 = FOURCC_skip;
@@ -2431,7 +2346,8 @@ int MP4_BoxGetRoot( input_thread_t *p_input, MP4_Box_t *p_root )
             p_root->p_first = p_moov;
         }
     }
-    return( i_result );
+
+    return p_root;
 }
 
 
@@ -2536,7 +2452,7 @@ static void __MP4_BoxGet( MP4_Box_t **pp_result,
                           MP4_Box_t *p_box, char *psz_fmt, va_list args)
 {
     char    *psz_path;
-#if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
+#if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
     size_t  i_size;
 #endif
 
@@ -2546,7 +2462,7 @@ static void __MP4_BoxGet( MP4_Box_t **pp_result,
         return;
     }
 
-#if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
+#if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
     vasprintf( &psz_path, psz_fmt, args );
 #else
     i_size = strlen( psz_fmt ) + 1024;