]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/libmp4.c
Mark the callback list as const
[vlc] / modules / demux / mp4 / libmp4.c
index 60ce1273f9b4cc978edf5e7b88edd01cb2b6b913..426890ff026a00375be07330daf29f9760f275dc 100644 (file)
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 
-#include <stdio.h>
-#include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc_demux.h>
 
@@ -76,7 +78,6 @@
         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; \
 
 */
 
-static uint32_t Get24bBE( uint8_t *p )
+/* This macro is used when we want to printf the box type
+ * APPLE annotation box is :
+ *  either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
+ *  either 32-bit ASCII text string
+ */
+#define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
+
+static uint32_t Get24bBE( const uint8_t *p )
 {
     return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
 }
 
-static void GetUUID( UUID_t *p_uuid, uint8_t *p_buff )
+static void GetUUID( UUID_t *p_uuid, const uint8_t *p_buff )
 {
     memcpy( p_uuid, p_buff, 16 );
 }
@@ -128,6 +136,8 @@ 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;
 }
 
 /* some functions for mp4 encoding of variables */
@@ -166,7 +176,7 @@ static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
 int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box )
 {
     int      i_read;
-    uint8_t  *p_peek;
+    const uint8_t  *p_peek;
 
     if( ( ( i_read = stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
     {
@@ -209,8 +219,12 @@ int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box )
 #ifdef MP4_VERBOSE
     if( p_box->i_size )
     {
-        msg_Dbg( p_stream, "found Box: %4.4s size "I64Fd,
-                 (char*)&p_box->i_type, p_box->i_size );
+        if MP4_BOX_TYPE_ASCII()
+            msg_Dbg( p_stream, "found Box: %4.4s size "I64Fd,
+                    (char*)&p_box->i_type, p_box->i_size );
+        else
+            msg_Dbg( p_stream, "found Box: c%3.3s size "I64Fd,
+                    (char*)&p_box->i_type+1, p_box->i_size );
     }
 #endif
 
@@ -239,11 +253,14 @@ static int MP4_NextBox( stream_t *p_stream, MP4_Box_t *p_box )
 
     if( p_box->p_father )
     {
+        const int i_box_end = p_box->i_size + p_box->i_pos;
+        const int i_father_end = p_box->p_father->i_size + p_box->p_father->i_pos;
+
         /* check if it's within p-father */
-        if( p_box->i_size + p_box->i_pos >=
-            p_box->p_father->i_size + p_box->p_father->i_pos )
+        if( i_box_end >= i_father_end )
         {
-            msg_Dbg( p_stream, "out of bound child" );
+            if( i_box_end > i_father_end )
+                msg_Dbg( p_stream, "out of bound child" );
             return 0; /* out of bound */
         }
     }
@@ -304,6 +321,7 @@ static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
 static void MP4_FreeBox_Common( MP4_Box_t *p_box )
 {
     /* Up to now do nothing */
+    (void)p_box;
 }
 
 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
@@ -313,7 +331,7 @@ static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
         p_box->p_father->i_type == VLC_FOURCC( 'r', 'o', 'o', 't' ) &&
         p_box->i_type == FOURCC_free )
     {
-        uint8_t *p_peek;
+        const uint8_t *p_peek;
         int     i_read;
         vlc_fourcc_t i_fcc;
 
@@ -338,7 +356,10 @@ static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
 
     /* Nothing to do */
 #ifdef MP4_VERBOSE
-    msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
+    if MP4_BOX_TYPE_ASCII()
+        msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
+    else
+        msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
 #endif
     return 1;
 }
@@ -604,9 +625,9 @@ static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
     }
 
 #ifdef MP4_VERBOSE
-    msg_Dbg( p_stream, "read box: \"hdlr\" handler type %4.4s name %s",
-                       (char*)&p_box->data.p_hdlr->i_handler_type,
-                       p_box->data.p_hdlr->psz_name );
+        msg_Dbg( p_stream, "read box: \"hdlr\" handler type %4.4s name %s",
+                   (char*)&p_box->data.p_hdlr->i_handler_type,
+                   p_box->data.p_hdlr->psz_name );
 
 #endif
     MP4_READBOX_EXIT( 1 );
@@ -703,7 +724,7 @@ static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
 
 static void MP4_FreeBox_url( MP4_Box_t *p_box )
 {
-    FREENULL( p_box->data.p_url->psz_location )
+    FREENULL( p_box->data.p_url->psz_location );
 }
 
 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
@@ -1117,12 +1138,12 @@ static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
     {
         /* SoundDescriptionV2 */
         double f_sample_rate;
-       int64_t dummy;
+    int64_t dummy;
         uint32_t i_channel;
 
         MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
         MP4_GET8BYTES( dummy );
-       memcpy( &f_sample_rate, &dummy, 8 );
+    memcpy( &f_sample_rate, &dummy, 8 );
 
         msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
         p_box->data.p_sample_soun->i_sampleratehi = (int)f_sample_rate % 65536;
@@ -1552,7 +1573,7 @@ static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
 
 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
 {
-    FREENULL( p_box->data.p_stss->i_sample_number )
+    FREENULL( p_box->data.p_stss->i_sample_number );
 }
 
 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
@@ -1589,8 +1610,8 @@ static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
 
 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
 {
-    FREENULL( p_box->data.p_stsh->i_shadowed_sample_number )
-    FREENULL( p_box->data.p_stsh->i_sync_sample_number )
+    FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
+    FREENULL( p_box->data.p_stsh->i_sync_sample_number );
 }
 
 
@@ -1621,7 +1642,7 @@ static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
 
 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
 {
-    FREENULL( p_box->data.p_stdp->i_priority )
+    FREENULL( p_box->data.p_stdp->i_priority );
 }
 
 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
@@ -1715,7 +1736,8 @@ static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
     }
 
 #ifdef MP4_VERBOSE
-    msg_Dbg( p_stream, "read box: \"elst\" entry-count "I64Fd, p_box->data.p_elst->i_entry_count );
+    msg_Dbg( p_stream, "read box: \"elst\" entry-count %lu",
+             (unsigned long)p_box->data.p_elst->i_entry_count );
 #endif
     MP4_READBOX_EXIT( 1 );
 }
@@ -1959,17 +1981,16 @@ static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
 
 #ifdef MP4_VERBOSE
     msg_Dbg( p_stream,
-             "read box: \"rdrf\" type:%4.4s ref %s",
-             (char*)&p_box->data.p_rdrf->i_ref_type,
-             p_box->data.p_rdrf->psz_ref );
-
+            "read box: \"rdrf\" type:%4.4s ref %s",
+            (char*)&p_box->data.p_rdrf->i_ref_type,
+            p_box->data.p_rdrf->psz_ref );
 #endif
     MP4_READBOX_EXIT( 1 );
 }
 
 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
 {
-    FREENULL( p_box->data.p_rdrf->psz_ref )
+    FREENULL( p_box->data.p_rdrf->psz_ref );
 }
 
 
@@ -2037,16 +2058,8 @@ static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
 
     if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
     {
-        int i_ret;
-        if( config_GetInt( p_stream, "france" ) )
-        {
-            i_ret = -7;
-        }
-        else
-        {
-            i_ret= drms_init( p_drms_box->data.p_sample_soun->p_drms,
+        int i_ret = drms_init( p_drms_box->data.p_sample_soun->p_drms,
                                p_box->i_type, p_peek, i_read );
-        }
         if( i_ret )
         {
             const char *psz_error;
@@ -2059,12 +2072,14 @@ static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
                 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;
-                case -7: psz_error = "you live in France"; break;
                 default: psz_error = "unknown error"; break;
             }
-
-            msg_Err( p_stream, "drms_init(%4.4s) failed (%s)",
-                     (char *)&p_box->i_type, psz_error );
+            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_box->data.p_sample_soun->p_drms );
             p_drms_box->data.p_sample_soun->p_drms = NULL;
@@ -2097,8 +2112,8 @@ static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
 
 #ifdef MP4_VERBOSE
         msg_Dbg( p_stream,
-                 "read box: \"%4.4s\" text=`%s'",
-                 (char*)&p_box->i_type,
+                 "read box: \"c%3.3s\" text=`%s'",
+                 ((char*)&p_box->i_type + 1),
                  p_box->data.p_0xa9xxx->psz_text );
 #endif
     }
@@ -2132,8 +2147,8 @@ static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
                 p_box->data.p_0xa9xxx->psz_text[i_data_len] = '\0';
 #ifdef MP4_VERBOSE
         msg_Dbg( p_stream,
-                 "read box: \"%4.4s\" text=`%s'",
-                 (char*)&p_box->i_type,
+                 "read box: \"c%3.3s\" text=`%s'",
+                 ((char*)&p_box->i_type+1),
                  p_box->data.p_0xa9xxx->psz_text );
 #endif
             }
@@ -2221,6 +2236,32 @@ static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
         free( p_chpl->chapter[i].psz_name );
 }
 
+static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
+{
+    unsigned int i;
+    MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t );
+
+    p_box->data.p_tref_generic->i_track_ID = NULL;
+    p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
+    if( p_box->data.p_tref_generic->i_entry_count > 0 )
+        p_box->data.p_tref_generic->i_track_ID = malloc( p_box->data.p_tref_generic->i_entry_count * sizeof(uint32_t) );
+
+    for( i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
+    {
+        MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
+    }
+#ifdef MP4_VERBOSE
+        msg_Dbg( p_stream, "read box: \"chap\" %d references",
+                 p_box->data.p_tref_generic->i_entry_count );
+#endif
+
+    MP4_READBOX_EXIT( 1 );
+}
+static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
+{
+    FREENULL( p_box->data.p_tref_generic->i_track_ID );
+}
+
 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
 {
     uint8_t meta_data[8];
@@ -2269,15 +2310,20 @@ static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
                 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
             default:
                 msg_Warn( p_stream,
-                          "unknown handler type in stsd (uncompletetly loaded)" );
+                          "unknown handler type in stsd (incompletely loaded)" );
                 return 1;
         }
     }
 
 unknown:
-    msg_Warn( p_stream,
-              "unknown box type %4.4s (uncompletetly loaded)",
-              (char*)&p_box->i_type );
+    if MP4_BOX_TYPE_ASCII()
+        msg_Warn( p_stream,
+                "unknown box type %4.4s (incompletely loaded)",
+                (char*)&p_box->i_type );
+    else
+        msg_Warn( p_stream,
+                "unknown box type c%3.3s (incompletely loaded)",
+                (char*)&p_box->i_type+1 );
 
     return 1;
 }
@@ -2286,7 +2332,7 @@ unknown:
 /****                   "Higher level" Functions                          ****/
 /**** ------------------------------------------------------------------- ****/
 
-static struct
+static const struct
 {
     uint32_t i_type;
     int  (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
@@ -2422,6 +2468,7 @@ static struct
     { FOURCC_dpnd,  MP4_ReadBox_default,   NULL },
     { FOURCC_ipir,  MP4_ReadBox_default,   NULL },
     { FOURCC_mpod,  MP4_ReadBox_default,   NULL },
+    { FOURCC_chap,  MP4_ReadBox_tref_generic,   MP4_FreeBox_tref_generic },
 
     /* found in hnti */
     { FOURCC_rtp,   MP4_ReadBox_default,   NULL },
@@ -2558,9 +2605,14 @@ void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
         if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
         {
             /* Should not happen */
-            msg_Warn( s,
-                      "cannot free box %4.4s, type unknown",
-                      (char*)&p_box->i_type );
+            if MP4_BOX_TYPE_ASCII()
+                msg_Warn( s,
+                        "cannot free box %4.4s, type unknown",
+                        (char*)&p_box->i_type );
+            else
+                msg_Warn( s,
+                        "cannot free box c%3.3s, type unknown",
+                        (char*)&p_box->i_type+1 );
         }
         else
         {
@@ -2642,8 +2694,12 @@ static void __MP4_BoxDumpStructure( stream_t *s,
 
     if( !i_level )
     {
-        msg_Dbg( s, "dumping root Box \"%4.4s\"",
-                          (char*)&p_box->i_type );
+        if MP4_BOX_TYPE_ASCII()
+            msg_Dbg( s, "dumping root Box \"%4.4s\"",
+                              (char*)&p_box->i_type );
+        else
+            msg_Dbg( s, "dumping root Box \"c%3.3s\"",
+                              (char*)&p_box->i_type+1 );
     }
     else
     {
@@ -2654,10 +2710,12 @@ static void __MP4_BoxDumpStructure( stream_t *s,
         {
             str[i*5] = '|';
         }
-        sprintf( str + i_level * 5, "+ %4.4s size %d",
-                      (char*)&p_box->i_type,
-                      (uint32_t)p_box->i_size );
-
+        if MP4_BOX_TYPE_ASCII()
+            sprintf( str + i_level * 5, "+ %4.4s size %d",
+                        (char*)&p_box->i_type, (uint32_t)p_box->i_size );
+        else
+            sprintf( str + i_level * 5, "+ c%3.3s size %d",
+                        (char*)&p_box->i_type+1, (uint32_t)p_box->i_size );
         msg_Dbg( s, "%s", str );
     }
     p_child = p_box->p_first;
@@ -2744,7 +2802,8 @@ static void __MP4_BoxGet( MP4_Box_t **pp_result,
         return;
     }
 
-    vasprintf( &psz_path, psz_fmt, args );
+    if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
+        psz_path = NULL;
 
     if( !psz_path || !psz_path[0] )
     {