]> git.sesse.net Git - vlc/blobdiff - modules/demux/asf/asf.c
Memory leak (fixes #2442)
[vlc] / modules / demux / asf / asf.c
index 199dfc415253e8d5da6a31d70290fd73fa477565..a7e9e201b3b324b915031e9a1cfc6706af1a24bf 100644 (file)
@@ -29,7 +29,8 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_demux.h>
 #include <vlc_interface.h>
 
 static int  Open  ( vlc_object_t * );
 static void Close ( vlc_object_t * );
 
-vlc_module_begin();
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_DEMUX );
-    set_description( _("ASF v1.0 demuxer") );
-    set_capability( "demux2", 200 );
-    set_callbacks( Open, Close );
-    add_shortcut( "asf" );
-vlc_module_end();
+vlc_module_begin ()
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_DEMUX )
+    set_description( N_("ASF v1.0 demuxer") )
+    set_capability( "demux", 200 )
+    set_callbacks( Open, Close )
+    add_shortcut( "asf" )
+vlc_module_end ()
 
 
 /*****************************************************************************
@@ -94,7 +95,7 @@ struct demux_sys_t
     int64_t             i_data_begin;
     int64_t             i_data_end;
 
-    vlc_bool_t          b_index;
+    bool          b_index;
     vlc_meta_t          *meta;
 };
 
@@ -149,11 +150,14 @@ static int Demux( demux_t *p_demux )
         mtime_t i_time_begin = GetMoviePTS( p_sys );
         int i_result;
 
-        if( p_demux->b_die )
+        if( !vlc_object_alive (p_demux) )
             break;
+#if 0
+        /* FIXME: returns EOF too early for some mms streams */
         if( p_sys->i_data_end >= 0 &&
                 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
             return 0; /* EOF */
+#endif
 
         /* Check if we have concatenated files */
         if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
@@ -171,10 +175,11 @@ static int Demux( demux_t *p_demux )
                 if( DemuxInit( p_demux ) )
                 {
                     msg_Err( p_demux, "failed to load the new header" );
-                    intf_UserFatal( p_demux, VLC_FALSE, _("Could not demux ASF stream"),
+                    intf_UserFatal( p_demux, false, _("Could not demux ASF stream"),
                                     _("VLC failed to load the ASF header.") );
                     return 0;
                 }
+                es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
                 continue;
             }
         }
@@ -199,7 +204,7 @@ static int Demux( demux_t *p_demux )
     p_sys->i_time = GetMoviePTS( p_sys );
     if( p_sys->i_time >= 0 )
     {
-        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
+        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time+1 );
     }
 
     return 1;
@@ -276,7 +281,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         }
         else
         {
-            return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
+            return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
                                            p_sys->i_data_end, p_sys->i_bitrate,
                                            p_sys->p_fp->i_min_data_packet_size,
                                            i_query, args );
@@ -290,7 +295,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             *pf = p_sys->i_time / (double)p_sys->i_length;
             return VLC_SUCCESS;
         }
-        return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
+        return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
                                        p_sys->i_data_end, p_sys->i_bitrate,
                                        p_sys->p_fp->i_min_data_packet_size,
                                        i_query, args );
@@ -307,7 +312,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         }
         else
         {
-            return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
+            return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
                                            p_sys->i_data_end, p_sys->i_bitrate,
                                            p_sys->p_fp->i_min_data_packet_size,
                                            i_query, args );
@@ -319,7 +324,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         return VLC_SUCCESS;
 
     default:
-        return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
+        return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
                                        p_sys->i_data_end, p_sys->i_bitrate,
                                        p_sys->p_fp->i_min_data_packet_size,
                                        i_query, args );
@@ -342,6 +347,7 @@ static mtime_t GetMoviePTS( demux_sys_t *p_sys )
         {
             if( i_time < 0 ) i_time = tk->i_time;
             else i_time = __MIN( i_time, tk->i_time );
+            break;
         }
     }
 
@@ -439,7 +445,8 @@ static int DemuxPacket( demux_t *p_demux )
     i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
     i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
 
-    i_packet_size_left = i_packet_length - i_packet_padding_length;
+    /* FIXME I have to do that for some file, I don't known why */
+    i_packet_size_left = i_data_packet_min /*i_packet_length*/ ;
 
     if( b_packet_multiple_payload )
     {
@@ -520,7 +527,8 @@ static int DemuxPacket( demux_t *p_demux )
         }
         else
         {
-            i_payload_data_length = i_packet_length - i_skip;
+            i_payload_data_length = i_packet_length -
+                                    i_packet_padding_length - i_skip;
         }
 
         if( i_payload_data_length < 0 || i_payload_data_length > i_packet_size_left )
@@ -574,6 +582,9 @@ static int DemuxPacket( demux_t *p_demux )
                 /* send complete packet to decoder */
                 block_t *p_gather = block_ChainGather( tk->p_frame );
 
+                if( p_sys->i_time < 0 )
+                    es_out_Control( p_demux->out, ES_OUT_SET_PCR, tk->i_time );
+
                 es_out_Send( p_demux->out, tk->p_es, p_gather );
 
                 tk->p_frame = NULL;
@@ -623,8 +634,14 @@ static int DemuxPacket( demux_t *p_demux )
 
     if( i_packet_size_left > 0 )
     {
-        msg_Warn( p_demux, "Didn't read %d bytes in the packet",
-                            i_packet_size_left );
+#ifdef ASF_DEBUG
+        if( i_packet_size_left > i_packet_padding_length )
+            msg_Warn( p_demux, "Didn't read %d bytes in the packet",
+                            i_packet_size_left - i_packet_padding_length );
+        else if( i_packet_size_left < i_packet_padding_length )
+            msg_Warn( p_demux, "Read %d too much bytes in the packet",
+                            i_packet_padding_length - i_packet_size_left );
+#endif
         if( stream_Read( p_demux->s, NULL, i_packet_size_left )
                                                          < i_packet_size_left )
         {
@@ -633,14 +650,6 @@ static int DemuxPacket( demux_t *p_demux )
         }
     }
 
-    if( i_packet_padding_length > 0 )
-        if( stream_Read( p_demux->s, NULL, i_packet_padding_length )
-                                                    < i_packet_padding_length )
-        {
-            msg_Err( p_demux, "cannot skip padding data, EOF ?" );
-            return 0;
-        }
-
     return 1;
 
 loop_error_recovery:
@@ -650,7 +659,11 @@ loop_error_recovery:
         msg_Err( p_demux, "unsupported packet header, fatal error" );
         return -1;
     }
-    stream_Read( p_demux->s, NULL, i_data_packet_min );
+    if( stream_Read( p_demux->s, NULL, i_data_packet_min ) != i_data_packet_min )
+    {
+        msg_Warn( p_demux, "cannot skip data, EOF ?" );
+        return 0;
+    }
 
     return 1;
 }
@@ -661,11 +674,11 @@ loop_error_recovery:
 static int DemuxInit( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    vlc_bool_t b_seekable;
-    unsigned int i_stream, i;
+    bool b_seekable;
+    unsigned int i_stream;
     asf_object_content_description_t *p_cd;
     asf_object_index_t *p_index;
-    vlc_bool_t b_index;
+    bool b_index;
 
     /* init context */
     p_sys->i_time   = -1;
@@ -675,7 +688,7 @@ static int DemuxInit( demux_t *p_demux )
     p_sys->p_fp     = NULL;
     p_sys->b_index  = 0;
     p_sys->i_track  = 0;
-    for( i = 0; i < 128; i++ )
+    for( int i = 0; i < 128; i++ )
     {
         p_sys->track[i] = NULL;
     }
@@ -716,11 +729,14 @@ static int DemuxInit( demux_t *p_demux )
     {
         asf_track_t    *tk;
         asf_object_stream_properties_t *p_sp;
-        vlc_bool_t b_access_selected;
+        asf_object_extended_stream_properties_t *p_esp;
+        asf_object_t *p_hdr_ext;
+        bool b_access_selected;
 
         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
                                &asf_object_stream_properties_guid,
                                i_stream );
+        p_esp = NULL;
 
         tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
         memset( tk, 0, sizeof( asf_track_t ) );
@@ -741,6 +757,26 @@ static int DemuxInit( demux_t *p_demux )
             continue;
         }
 
+        /* Find the associated extended_stream_properties if any */
+        p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
+                                    &asf_object_header_extension_guid, 0 );
+        if( p_hdr_ext )
+        {
+            int i_ext_stream = ASF_CountObject( p_hdr_ext,
+                                                &asf_object_extended_stream_properties );
+            for( int i = 0; i < i_ext_stream; i++ )
+            {
+                asf_object_t *p_tmp =
+                    ASF_FindObject( p_hdr_ext,
+                                    &asf_object_extended_stream_properties, i );
+                if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
+                {
+                    p_esp = &p_tmp->ext_stream;
+                    break;
+                }
+            }
+        }
+
         if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
         {
@@ -790,12 +826,17 @@ static int DemuxInit( demux_t *p_demux )
             fmt.video.i_width = GetDWLE( p_data + 4 );
             fmt.video.i_height= GetDWLE( p_data + 8 );
 
+            if( p_esp && p_esp->i_average_time_per_frame > 0 )
+            {
+                fmt.video.i_frame_rate = 10000000;
+                fmt.video.i_frame_rate_base = p_esp->i_average_time_per_frame;
+            }
 
             if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
             {
                 /* DVR-MS special ASF */
                 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
-                fmt.b_packetized = VLC_FALSE;
+                fmt.b_packetized = false;
             }
 
             if( p_sp->i_type_specific_data_length > 11 +
@@ -840,7 +881,7 @@ static int DemuxInit( demux_t *p_demux )
                         (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
                         fmt.video.i_height / i_aspect_y;
                 }
-        }
+            }
 
             tk->i_cat = VIDEO_ES;
             tk->p_es = es_out_Add( p_demux->out, &fmt );
@@ -877,7 +918,7 @@ static int DemuxInit( demux_t *p_demux )
                 fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
                 fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
                 fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
-                fmt.b_packetized = VLC_TRUE;
+                fmt.b_packetized = true;
 
                 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
                     i_format != WAVE_FORMAT_MPEGLAYER3 &&
@@ -1005,8 +1046,6 @@ static int DemuxInit( demux_t *p_demux )
         }
     }
 #endif
-
-    es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
     return VLC_SUCCESS;
 
 error: