]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/mp4.c
* ALL: fixes for various compilation warnings and spelling errors.
[vlc] / modules / demux / mp4 / mp4.c
index cb7bc8e039cfbb77ee4d611059c60118bf4ffef3..874f827168d42f1c33a255f0ac16625cfd89f2e4 100644 (file)
@@ -2,14 +2,14 @@
  * mp4.c : MP4 file input module for vlc
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: mp4.c,v 1.8 2002/11/28 16:32:29 fenrir Exp $
+ * $Id: mp4.c,v 1.16 2003/01/28 16:57:28 sam Exp $
  * Authors: 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
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -56,7 +56,7 @@ vlc_module_begin();
 vlc_module_end();
 
 /*****************************************************************************
- * Declaration of local function 
+ * Declaration of local function
  *****************************************************************************/
 
 static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track );
@@ -87,26 +87,26 @@ static int  MP4_DecodeSample();
  * MP4Init: check file and initializes MP4 structures
  *****************************************************************************/
 static int MP4Init( vlc_object_t * p_this )
-{   
+{
     input_thread_t  *p_input = (input_thread_t *)p_this;
     uint8_t         *p_peek;
-    
+
     demux_sys_t     *p_demux;
-    
+
     MP4_Box_t       *p_ftyp;
 
 
     MP4_Box_t       *p_mvhd;
     MP4_Box_t       *p_trak;
 
-    int             i;
+    unsigned int    i;
     /* I need to seek */
     if( !p_input->stream.b_seekable )
     {
         msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
         return( -1 );
-            
-    } 
+
+    }
     /* Initialize access plug-in structures. */
     if( p_input->i_mtu == 0 )
     {
@@ -123,17 +123,17 @@ static int MP4Init( vlc_object_t * p_this )
         return( -1 );
     }
 
-    
+
     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
     {
         case( FOURCC_ftyp ):
         case( FOURCC_moov ):
         case( FOURCC_moof ):
         case( FOURCC_mdat ):
-        case( FOURCC_udta ): 
+        case( FOURCC_udta ):
         case( FOURCC_free ):
         case( FOURCC_skip ):
-        case( FOURCC_wide ): 
+        case( FOURCC_wide ):
             break;
          default:
             msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
@@ -141,7 +141,7 @@ static int MP4Init( vlc_object_t * p_this )
     }
 
     /* create our structure that will contains all data */
-    if( !( p_input->p_demux_data = 
+    if( !( p_input->p_demux_data =
                 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
     {
         msg_Err( p_input, "out of memory" );
@@ -149,7 +149,7 @@ static int MP4Init( vlc_object_t * p_this )
     }
     memset( p_demux, 0, sizeof( demux_sys_t ) );
     p_input->p_demux_data = p_demux;
-       
+
 
     /* Now load all boxes ( except raw data ) */
     if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
@@ -165,38 +165,38 @@ static int MP4Init( vlc_object_t * p_this )
         switch( p_ftyp->data.p_ftyp->i_major_brand )
         {
             case( FOURCC_isom ):
-                msg_Info( p_input, 
-                          "ISO Media file (isom) version %d.",
-                          p_ftyp->data.p_ftyp->i_minor_version );
+                msg_Dbg( p_input, 
+                         "ISO Media file (isom) version %d.",
+                         p_ftyp->data.p_ftyp->i_minor_version );
                 break;
             default:
-                msg_Info( p_input,
-                          "Unrecognize major file specification (%c%c%c%c).",
-                           p_ftyp->data.p_ftyp->i_major_brand&0xff,
-                           ( p_ftyp->data.p_ftyp->i_major_brand >>  8)&0xff,
-                           ( p_ftyp->data.p_ftyp->i_major_brand >> 16 )&0xff,
-                           ( p_ftyp->data.p_ftyp->i_major_brand >> 24 )&0xff );
+                msg_Dbg( p_input,
+                         "unrecognized major file specification (%c%c%c%c).",
+                          p_ftyp->data.p_ftyp->i_major_brand&0xff,
+                          ( p_ftyp->data.p_ftyp->i_major_brand >>  8)&0xff,
+                          ( p_ftyp->data.p_ftyp->i_major_brand >> 16 )&0xff,
+                          ( p_ftyp->data.p_ftyp->i_major_brand >> 24 )&0xff );
                 break;
         }
     }
     else
     {
-        msg_Info( p_input, "File Type box missing(assume ISO Media file)" );
+        msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
     }
 
     /* the file need to have one moov box */
     if( MP4_BoxCount( &p_demux->box_root, "/moov" ) != 1 )
     {
         msg_Err( p_input, 
-                 "MP4 plugin discarded (%d moov box)",
+                 "MP4 plugin discarded (%d moov boxes)",
                  MP4_BoxCount( &p_demux->box_root, "/moov" ) );
-        MP4End( p_input );
-        return( -1 );
+//        MP4End( p_input );
+//        return( -1 );
     }
 
     if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
     {
-        msg_Err( p_input, "cannot find /moov/mvhd !" );
+        msg_Err( p_input, "cannot find /moov/mvhd" );
         MP4End( p_input );
         return( -1 );
     }
@@ -205,11 +205,11 @@ static int MP4Init( vlc_object_t * p_this )
         p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
         p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
     }
-    
+
     if( !( p_demux->i_tracks = 
                 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
     {
-        msg_Err( p_input, "cannot find any /moov/trak !" );
+        msg_Err( p_input, "cannot find any /moov/trak" );
         MP4End( p_input );
         return( -1 );
     }
@@ -241,7 +241,7 @@ static int MP4Init( vlc_object_t * p_this )
                     psz_cat = "unknown";
                     break;
             }
-            
+
             msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
                             p_demux->track[i].i_track_ID,
                             psz_cat,
@@ -256,7 +256,7 @@ static int MP4Init( vlc_object_t * p_this )
         }
 
     }
-  
+
     /*  create one program */
     vlc_mutex_lock( &p_input->stream.stream_lock );
     if( input_InitStream( p_input, 0 ) == -1)
@@ -277,8 +277,8 @@ static int MP4Init( vlc_object_t * p_this )
     /* XXX beurk and beurk, see MP4Demux and MP4Seek */
     if( p_demux->i_duration/p_demux->i_timescale > 0 )
     {
-        p_input->stream.i_mux_rate =  
-            p_input->stream.p_selected_area->i_size / 50 / 
+        p_input->stream.i_mux_rate =
+            p_input->stream.p_selected_area->i_size / 50 /
             ( p_demux->i_duration / p_demux->i_timescale );
     }
     else
@@ -286,8 +286,8 @@ static int MP4Init( vlc_object_t * p_this )
         p_input->stream.i_mux_rate = 0;
     }
     vlc_mutex_unlock( &p_input->stream.stream_lock );
-   
-    
+
+
     for( i = 0; i < p_demux->i_tracks; i++ )
     {
         /* start decoder for this track if enable by default*/
@@ -300,20 +300,20 @@ static int MP4Init( vlc_object_t * p_this )
     vlc_mutex_lock( &p_input->stream.stream_lock );
     p_input->stream.p_selected_program->b_is_ok = 1;
     vlc_mutex_unlock( &p_input->stream.stream_lock );
-        
-    return( 0 );    
+
+    return( 0 );
 
 }
 
 /*****************************************************************************
- * MP4Demux: read packet and send them to decoders 
+ * MP4Demux: read packet and send them to decoders
  *****************************************************************************
  * TODO check for newly selected track (ie audio upt to now )
  *****************************************************************************/
 static int MP4Demux( input_thread_t *p_input )
 {
     demux_sys_t *p_demux = p_input->p_demux_data;
-    int i_track;
+    unsigned int i_track;
 
     /* XXX beurk, beuRK and BEURK, 
        but only way I've found to detect seek from interface */
@@ -401,7 +401,7 @@ static int MP4Demux( input_thread_t *p_input )
 static int   MP4Seek     ( input_thread_t *p_input, mtime_t i_date )
 {
     demux_sys_t *p_demux = p_input->p_demux_data;
-    int i_track;
+    unsigned int i_track;
 
     /* First update update global time */
     p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
@@ -420,19 +420,19 @@ static int   MP4Seek     ( input_thread_t *p_input, mtime_t i_date )
  * MP4End: frees unused data
  *****************************************************************************/
 static void __MP4End ( vlc_object_t * p_this )
-{   
+{
 #define FREE( p ) \
-    if( p ) { free( p ); } 
-    int i_track;
+    if( p ) { free( p ); }
+    unsigned int i_track;
     input_thread_t *  p_input = (input_thread_t *)p_this;
     demux_sys_t *p_demux = p_input->p_demux_data;
-    
-    msg_Dbg( p_input, "Freeing all memory" );
+
+    msg_Dbg( p_input, "freeing all memory" );
     MP4_BoxFree( p_input, &p_demux->box_root );
     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
     {
-        int i_chunk;
-        for( i_chunk = 0; 
+        unsigned int i_chunk;
+        for( i_chunk = 0;
                 i_chunk < p_demux->track[i_track].i_chunk_count; i_chunk++ )
         {
             if( p_demux->track[i_track].chunk )
@@ -441,6 +441,7 @@ static void __MP4End ( vlc_object_t * p_this )
                FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_delta_dts );
             }
         }
+        FREE( p_demux->track[i_track].chunk );
 
         if( !p_demux->track[i_track].i_sample_size )
         {
@@ -448,6 +449,8 @@ static void __MP4End ( vlc_object_t * p_this )
         }
     }
     FREE( p_demux->track );
+
+    FREE( p_input->p_demux_data );
 #undef FREE
 }
 
@@ -465,7 +468,7 @@ static void __MP4End ( vlc_object_t * p_this )
 static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track )
 {
     demux_sys_t *p_demux = p_input->p_demux_data;
-    int i_chunk_last;
+    unsigned int i_chunk_last;
     MP4_Box_t   *p_stss;
 
     if( !p_track->b_ok ||
@@ -508,9 +511,9 @@ static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track
     /* *** Try to find nearest sync points *** */
     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
     {
-        int i_index;
-        msg_Dbg( p_input, 
-                    "track[Id 0x%x] using Sync Sample Box (stss)", 
+        unsigned int i_index;
+        msg_Dbg( p_input,
+                    "track[Id 0x%x] using Sync Sample Box (stss)",
                     p_track->i_track_ID );
         for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
         {
@@ -550,7 +553,7 @@ static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track
     else
     {
         msg_Dbg( p_input, 
-                    "track[Id 0x%x] doesn't provided Sync Sample Box (stss)", 
+                    "track[Id 0x%x] does not provide Sync Sample Box (stss)", 
                     p_track->i_track_ID );
     }
 
@@ -574,7 +577,7 @@ static void MP4_ParseTrack( input_thread_t *p_input,
                      track_data_mp4_t *p_demux_track,
                      MP4_Box_t  * p_trak )
 {
-    int i;
+    unsigned int i;
 
     MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
     MP4_Box_t *p_tref = MP4_BoxGet( p_trak, "tref" );
@@ -609,12 +612,12 @@ static void MP4_ParseTrack( input_thread_t *p_input,
     
     if( ( p_elst = MP4_BoxGet( p_trak, "edts/elst" ) ) )
     {
-/*        msg_Warn( p_input, "Unhandled box: edts --> FIXME" ); */
+/*        msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
     }
 
     if( p_tref )
     {
-/*        msg_Warn( p_input, "Unhandled box: tref --> FIXME" ); */
+/*        msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
     } 
 
     p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
@@ -692,8 +695,8 @@ static int MP4_CreateChunksIndex( input_thread_t *p_input,
     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
     MP4_Box_t *p_stsc;
 
-    int i_chunk;
-    int i_index, i_last;
+    unsigned int i_chunk;
+    unsigned int i_index, i_last;
    
 
     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
@@ -706,7 +709,7 @@ static int MP4_CreateChunksIndex( input_thread_t *p_input,
     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
     if( !p_demux_track->i_chunk_count )
     {
-        msg_Warn( p_input, "No chunk defined" );
+        msg_Warn( p_input, "no chunk defined" );
         return( 0 );
     }
     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count, 
@@ -900,26 +903,27 @@ static int MP4_CreateSamplesIndex( input_thread_t *p_input,
 static void MP4_StartDecoder( input_thread_t *p_input,
                                  track_data_mp4_t *p_demux_track )
 {
-    MP4_Box_t   *p_sample;
-    int         i;
-    int         i_chunk;
+    MP4_Box_t *  p_sample;
+    unsigned int i;
+    unsigned int i_chunk;
+
+    unsigned int i_decoder_specific_info_len;
+    uint8_t *    p_decoder_specific_info;
+    pes_packet_t *p_pes_init;
 
-    int         i_decoder_specific_info_len;
-    uint8_t     *p_decoder_specific_info;
-    
     uint8_t             *p_init;
     BITMAPINFOHEADER    *p_bih;
     WAVEFORMATEX        *p_wf;
 
     MP4_Box_t   *p_esds;
 
-    
+
     if( (!p_demux_track->b_ok )||( p_demux_track->i_cat == UNKNOWN_ES ) )
     {
         return;
     }
-    
-    msg_Dbg( p_input, "Starting decoder for track[Id 0x%x]",
+
+    msg_Dbg( p_input, "starting decoder for track[Id 0x%x]",
                       p_demux_track->i_track_ID );
 
     /* launch decoder according in chunk we are */
@@ -956,7 +960,7 @@ static void MP4_StartDecoder( input_thread_t *p_input,
         p_demux_track->p_es->psz_desc[i] = p_demux_track->i_language[i];
     }
     p_demux_track->p_es->psz_desc[3] = '\0';
-    
+
     p_demux_track->p_es->i_stream_id = p_demux_track->i_track_ID;
 
     /* It's a little ugly but .. there are special cases */
@@ -970,11 +974,12 @@ static void MP4_StartDecoder( input_thread_t *p_input,
             p_demux_track->p_es->i_fourcc = p_sample->i_type;
             break;
     }
-    
+
     p_demux_track->p_es->i_cat = p_demux_track->i_cat;
-    
+
     i_decoder_specific_info_len = 0;
     p_decoder_specific_info = NULL;
+    p_pes_init = NULL;
 
     /* now see if esds is present and if so create a data packet 
         with decoder_specific_info  */
@@ -1022,7 +1027,7 @@ static void MP4_StartDecoder( input_thread_t *p_input,
             default:
                 /* Unknown entry, but don't touch i_fourcc */
                 msg_Warn( p_input, 
-                          "objectTypeIndication(0x%x) unknow (Track[ID 0x%x])",
+                          "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
                           p_decconfig->i_objectTypeIndication,
                           p_demux_track->i_track_ID );
                 break;
@@ -1045,9 +1050,9 @@ static void MP4_StartDecoder( input_thread_t *p_input,
         --> bitmapinfoheader_t : width and height 
         --> waveformatex_t : channels, samplerate, bitspersample
         and at the end I add p_decoder_specific_info 
-        
+
         TODO set more values
-     
+
      */
 
     switch( p_demux_track->i_cat )
@@ -1055,7 +1060,8 @@ static void MP4_StartDecoder( input_thread_t *p_input,
         case( VIDEO_ES ):    
             /* now create a bitmapinfoheader_t for decoder and 
                add information found in p_esds */
-            p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len );
+            /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
+            p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
             p_bih = (BITMAPINFOHEADER*)p_init;
 
             p_bih->biSize     = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
@@ -1080,17 +1086,42 @@ static void MP4_StartDecoder( input_thread_t *p_input,
                 // fall on display size
                 p_bih->biHeight = p_demux_track->i_height;
             }
-            
+
             if( i_decoder_specific_info_len )
             {
+                data_packet_t   *p_data;
+
                 memcpy( p_init + sizeof( BITMAPINFOHEADER ), 
                         p_decoder_specific_info,
                         i_decoder_specific_info_len);
+
+                /* If stream is mpeg4 video we send specific_info,
+                   as it's needed to decode it (vol) */
+                switch( p_demux_track->p_es->i_fourcc )
+                {
+                    case VLC_FOURCC( 'm','p','4','v' ):
+                    case VLC_FOURCC( 'D','I','V','X' ):
+                    case VLC_FOURCC( 'd','i','v','x' ):
+                        p_pes_init = input_NewPES( p_input->p_method_data );
+                        p_data = input_NewPacket( p_input->p_method_data,
+                                                  i_decoder_specific_info_len);
+                        memcpy( p_data->p_payload_start,
+                                p_decoder_specific_info,
+                                i_decoder_specific_info_len );
+                        p_pes_init->i_dts = p_pes_init->i_pts = 0;
+                        p_pes_init->p_first = p_pes_init->p_last = p_data;
+                        p_pes_init->i_nb_data = 1;
+                        p_pes_init->i_pes_size = i_decoder_specific_info_len;
+                        break;
+                    default:
+                        break;
+                }
+
             }
             break;
 
         case( AUDIO_ES ):
-            p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len);
+            p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
             p_wf = (WAVEFORMATEX*)p_init;
 
             p_wf->wFormatTag = 0;
@@ -1105,22 +1136,43 @@ static void MP4_StartDecoder( input_thread_t *p_input,
 
             if( i_decoder_specific_info_len )
             {
-                memcpy( p_init + sizeof( WAVEFORMATEX ), 
+                memcpy( p_init + sizeof( WAVEFORMATEX ),
                         p_decoder_specific_info,
                         i_decoder_specific_info_len);
             }
+
             break;
 
         default:
             p_init = NULL;
             break;
     }
-
-    p_demux_track->p_es->p_demux_data = (es_sys_t *)p_init;
+    if( p_demux_track->i_cat == AUDIO_ES )
+    {
+        p_demux_track->p_es->p_waveformatex = (void*)p_init;
+    }
+    else if( p_demux_track->i_cat == VIDEO_ES )
+    {
+        p_demux_track->p_es->p_bitmapinfoheader = (void*)p_init;
+    }
     vlc_mutex_lock( &p_input->stream.stream_lock );
     input_SelectES( p_input, p_demux_track->p_es );
     vlc_mutex_unlock( &p_input->stream.stream_lock );
 
+    if( !p_demux_track->p_es->p_decoder_fifo )
+    {
+        msg_Warn( p_input, "cannot start decoder" );
+        if( p_pes_init )
+        {
+            input_DeletePES( p_input->p_method_data, p_pes_init );
+        }
+        return;
+    }
+
+    if( p_pes_init != NULL )
+    {
+        input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes_init );
+    }
     p_demux_track->b_ok = 1;
     p_demux_track->b_selected = 1;
 }
@@ -1128,7 +1180,7 @@ static void MP4_StartDecoder( input_thread_t *p_input,
 static void MP4_StopDecoder( input_thread_t *p_input,
                              track_data_mp4_t *p_demux_track )
 {
-    msg_Dbg( p_input, "Stopping decoder (track[Id 0x%x])",
+    msg_Dbg( p_input, "stopping decoder (track[Id 0x%x])",
                       p_demux_track->i_track_ID );
 
     input_UnselectES( p_input, p_demux_track->p_es );
@@ -1141,7 +1193,7 @@ static int  MP4_ReadSample( input_thread_t *p_input,
                             track_data_mp4_t *p_demux_track,
                             pes_packet_t **pp_pes )
 {
-    int i_size;
+    size_t i_size;
     off_t i_pos;
 
     data_packet_t *p_data;
@@ -1195,7 +1247,6 @@ static int  MP4_ReadSample( input_thread_t *p_input,
     if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
     {
         input_DeletePES( p_input->p_method_data, *pp_pes );
-        input_DeletePacket( p_input->p_method_data, p_data );
         return( 0 );
     }