]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/libmp4.c
* ./modules/demux/mp4: DRMS support.
[vlc] / modules / demux / mp4 / libmp4.c
index 560d7ccd35ac0ba9ef88b7d187616320653312ca..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.36 2003/11/27 20:50:48 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
@@ -29,6 +30,7 @@
 #endif
 
 #include "libmp4.h"
+#include "drms.h"
 
 /*****************************************************************************
  * Here are defined some macro to make life simpler but before using it
     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 )
 
@@ -143,7 +145,7 @@ static void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
     int i_sec;
 
     /* date begin at 1 jan 1904 */
-    i_date += ((1904ULL * 365) + 17) * 24 * 60 * 60;
+    i_date += ((1904U * 365) + 17) * 24 * 60 * 60;
 
     i_day = i_date / ( 60*60*24);
     i_hour = ( i_date /( 60*60 ) ) % 60;
@@ -985,7 +987,7 @@ static int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 
 #ifdef MP4_VERBOSE
         msg_Dbg( p_stream->p_input, "Found esds MPEG4ESDescr (%dBytes)",
-                i_len );
+                 i_len );
 #endif
 
         MP4_GET2BYTES( es_descriptor.i_ES_ID );
@@ -1031,7 +1033,7 @@ static int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 
 #ifdef MP4_VERBOSE
         msg_Dbg( p_stream->p_input, "Found esds MP4DecConfigDescr (%dBytes)",
-                i_len );
+                 i_len );
 #endif
 
     es_descriptor.p_decConfigDescr =
@@ -1056,7 +1058,7 @@ static int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
 
 #ifdef MP4_VERBOSE
         msg_Dbg( p_stream->p_input, "Found esds MP4DecSpecificDescr (%dBytes)",
-                i_len );
+                 i_len );
 #endif
 
     es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
@@ -1090,7 +1092,7 @@ static int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
     if( i_read < 28 )
     {
         i_read -= 30;
-        MP4_READBOX_EXIT( 0 );
+        MP4_READBOX_EXIT( 1 );
     }
 
     for( i = 0; i < 6 ; i++ )
@@ -1145,6 +1147,79 @@ static 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
@@ -1159,6 +1234,18 @@ static int MP4_ReadBox_sample_soun( 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;
@@ -1878,6 +1965,81 @@ static 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                          ****/
 /**** ------------------------------------------------------------------- ****/
@@ -2013,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 }
 };