]> git.sesse.net Git - vlc/blobdiff - modules/demux/asf/asf.c
* modules/demux/asf/* use index for seeking if available and if there's a video track.
[vlc] / modules / demux / asf / asf.c
index d5ba74c5d2a63b9fc3b13d52ed620057f8762b03..bef1b78613d2d9e9a4fbee77a3c10feb2169bbdb 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
- * asf.c : ASFv01 file input module for vlc
+ * asf.c : ASF demux module
  *****************************************************************************
- * Copyright (C) 2002-2003 VideoLAN
- * $Id: asf.c,v 1.46 2003/11/23 13:15:27 gbazin Exp $
+ * Copyright (C) 2002-2003 the VideoLAN team
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
@@ -18,7 +18,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
+#include <vlc_demux.h>
+#include <vlc_interface.h>
 
-#include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
+#include <vlc_meta.h>
+#include <vlc_access.h>                /* GET_PRIVATE_ID_STATE */
+#include <vlc_codecs.h>                /* BITMAPINFOHEADER, WAVEFORMATEX */
 #include "libasf.h"
 
+/* TODO
+ *  - add support for the newly added object: language, bitrate,
+ *                                            extended stream properties.
+ */
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -39,8 +47,10 @@ 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( "demux", 200 );
+    set_capability( "demux2", 200 );
     set_callbacks( Open, Close );
     add_shortcut( "asf" );
 vlc_module_end();
@@ -49,9 +59,10 @@ vlc_module_end();
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int  Demux   ( input_thread_t * );
+static int Demux  ( demux_t * );
+static int Control( demux_t *, int i_query, va_list args );
 
-typedef struct asf_stream_s
+typedef struct
 {
     int i_cat;
 
@@ -63,429 +74,271 @@ typedef struct asf_stream_s
 
     block_t         *p_frame; /* use to gather complete frame */
 
-} asf_stream_t;
+} asf_track_t;
 
 struct demux_sys_t
 {
-    mtime_t             i_time;     /* ยตs */
+    mtime_t             i_time;     /* s */
     mtime_t             i_length;   /* length of file file */
+    int64_t             i_bitrate;  /* global file bitrate */
 
     asf_object_root_t            *p_root;
     asf_object_file_properties_t *p_fp;
 
-    unsigned int        i_streams;
-    asf_stream_t        *stream[128];
+    unsigned int        i_track;
+    asf_track_t         *track[128];
 
     int64_t             i_data_begin;
     int64_t             i_data_end;
+
+    vlc_bool_t          b_index;
+    vlc_meta_t          *meta;
 };
 
 static mtime_t  GetMoviePTS( demux_sys_t * );
-static int      DemuxPacket( input_thread_t *, vlc_bool_t b_play_audio );
+static int      DemuxInit( demux_t * );
+static void     DemuxEnd( demux_t * );
+static int      DemuxPacket( demux_t * );
 
 /*****************************************************************************
  * Open: check file and initializes ASF structures
  *****************************************************************************/
 static int Open( vlc_object_t * p_this )
 {
-    input_thread_t  *p_input = (input_thread_t *)p_this;
-    uint8_t         *p_peek;
+    demux_t     *p_demux = (demux_t *)p_this;
+    demux_sys_t *p_sys;
+    guid_t      guid;
+    uint8_t     *p_peek;
 
-    guid_t          guid;
-
-    demux_sys_t     *p_sys;
-    unsigned int    i_stream, i;
-    asf_object_content_description_t *p_cd;
+    /* A little test to see if it could be a asf stream */
+    if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
 
-    vlc_bool_t      b_seekable;
-
-    input_info_category_t *p_cat;
-
-    /* a little test to see if it could be a asf stream */
-    if( input_Peek( p_input, &p_peek, 16 ) < 16 )
-    {
-        msg_Warn( p_input, "ASF plugin discarded (cannot peek)" );
-        return VLC_EGENERIC;
-    }
     ASF_GetGUID( &guid, p_peek );
-    if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) )
-    {
-        msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
-        return VLC_EGENERIC;
-    }
+    if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
 
-    /* Set p_input field */
-    p_input->pf_demux         = Demux;
-    p_input->pf_demux_control = demux_vaControlDefault;
-    p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
+    /* Set p_demux fields */
+    p_demux->pf_demux = Demux;
+    p_demux->pf_control = Control;
+    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
     memset( p_sys, 0, sizeof( demux_sys_t ) );
-    p_sys->i_time = -1;
-    p_sys->i_length = 0;
 
-    /* Now load all object ( except raw data ) */
-    stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
-    if( (p_sys->p_root = ASF_ReadObjectRoot( p_input->s, b_seekable )) == NULL )
+    /* Load the headers */
+    if( DemuxInit( p_demux ) )
     {
-        msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
         free( p_sys );
         return VLC_EGENERIC;
     }
-    p_sys->p_fp = p_sys->p_root->p_fp;
-
-    if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
-    {
-        msg_Warn( p_input,
-                  "ASF plugin discarded (invalid file_properties object)" );
-        goto error;
-    }
-
-    p_sys->i_streams = ASF_CountObject( p_sys->p_root->p_hdr,
-                                          &asf_object_stream_properties_guid );
-    if( !p_sys->i_streams )
-    {
-        msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
-        goto error;
-    }
+    return VLC_SUCCESS;
+}
 
-    msg_Dbg( p_input, "found %d streams", p_sys->i_streams );
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    if( input_InitStream( p_input, 0 ) == -1)
-    {
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        msg_Err( p_input, "cannot init stream" );
-        goto error;
-    }
-    p_input->stream.i_mux_rate = 0 ; /* updated later */
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
+/*****************************************************************************
+ * Demux: read packet and send them to decoders
+ *****************************************************************************/
+static int Demux( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
 
-    for( i_stream = 0; i_stream < p_sys->i_streams; i_stream ++ )
+    for( ;; )
     {
-        asf_stream_t    *p_stream;
-        asf_object_stream_properties_t *p_sp;
-
-        p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
-                               &asf_object_stream_properties_guid,
-                               i_stream );
-
-        p_stream =
-            p_sys->stream[p_sp->i_stream_number] =
-                malloc( sizeof( asf_stream_t ) );
-        memset( p_stream, 0, sizeof( asf_stream_t ) );
+        uint8_t *p_peek;
+        mtime_t i_length;
+        mtime_t i_time_begin = GetMoviePTS( p_sys );
+        int i_result;
 
-        p_stream->i_time = -1;
-        p_stream->p_sp = p_sp;
-        p_stream->p_es = NULL;
-        p_stream->p_frame = NULL;
+        if( p_demux->b_die ) break;
 
-        if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
-            p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
+        /* Check if we have concatenated files */
+        if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
         {
-            es_format_t  fmt;
-            uint8_t      *p_data = p_sp->p_type_specific_data;
-
-            es_format_Init( &fmt, AUDIO_ES, 0 );
-            wf_tag_to_fourcc( GetWLE( &p_data[0] ), &fmt.i_codec, NULL );
-            fmt.audio.i_channels        = GetWLE(  &p_data[2] );
-            fmt.audio.i_rate      = GetDWLE( &p_data[4] );
-            fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
-            fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
-            fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
+            guid_t guid;
 
-            if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) )
+            ASF_GetGUID( &guid, p_peek );
+            if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
             {
-                fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
-                                     p_sp->i_type_specific_data_length - sizeof( WAVEFORMATEX ) );
-                fmt.p_extra = malloc( fmt.i_extra );
-                memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )], fmt.i_extra );
-            }
+                msg_Warn( p_demux, "found a new ASF header" );
+                /* We end this stream */
+                DemuxEnd( p_demux );
 
-            p_stream->i_cat = AUDIO_ES;
-            p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
+                /* And we prepare to read the next one */
+                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"), 
+                                    _("VLC failed to load the ASF header.") );
+                    return 0;
+                }
+                continue;
+            }
+        }
 
-            msg_Dbg( p_input,
-                    "added new audio stream(codec:0x%x,ID:%d)",
-                    GetWLE( p_data ), p_sp->i_stream_number );
+        /* Read and demux a packet */
+        if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
+        {
+            return i_result;
         }
-        else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) &&
-                 p_sp->i_type_specific_data_length >= 11 + sizeof( BITMAPINFOHEADER ) )
+        if( i_time_begin == -1 )
         {
-            es_format_t  fmt;
-            uint8_t      *p_data = &p_sp->p_type_specific_data[11];
-
-            es_format_Init( &fmt, VIDEO_ES,
-                            VLC_FOURCC( p_data[16], p_data[17], p_data[18], p_data[19] ) );
-            fmt.video.i_width = GetDWLE( p_data + 4 );
-            fmt.video.i_height= GetDWLE( p_data + 8 );
-
-            if( p_sp->i_type_specific_data_length > 11 + sizeof( BITMAPINFOHEADER ) )
-            {
-                fmt.i_extra = __MIN( GetDWLE( p_data ),
-                                     p_sp->i_type_specific_data_length - 11 - sizeof( BITMAPINFOHEADER ) );
-                fmt.p_extra = malloc( fmt.i_extra );
-                memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )], fmt.i_extra );
-            }
-
-            p_stream->i_cat = VIDEO_ES;
-            p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
-
-            msg_Dbg( p_input, "added new video stream(ID:%d)",
-                     p_sp->i_stream_number );
+            i_time_begin = GetMoviePTS( p_sys );
         }
         else
         {
-            p_stream->i_cat = UNKNOWN_ES;
-            msg_Dbg( p_input, "ignoring unknown stream(ID:%d)",
-                     p_sp->i_stream_number );
+            i_length = GetMoviePTS( p_sys ) - i_time_begin;
+            if( i_length < 0 || i_length >= 40 * 1000 ) break;
         }
     }
 
-    p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
-    if( p_sys->p_root->p_data->i_object_size != 0 )
-    { /* local file */
-        p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
-                                    p_sys->p_root->p_data->i_object_size;
-    }
-    else
-    { /* live/broacast */
-        p_sys->i_data_end = -1;
-    }
-
-
-    /* go to first packet */
-    stream_Seek( p_input->s, p_sys->i_data_begin );
-
-    /* try to calculate movie time */
-    if( p_sys->p_fp->i_data_packets_count > 0 )
+    /* Set the PCR */
+    p_sys->i_time = GetMoviePTS( p_sys );
+    if( p_sys->i_time >= 0 )
     {
-        int64_t i_count;
-        int64_t i_size = stream_Size( p_input->s );
-
-        if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
-        {
-            i_size = p_sys->i_data_end;
-        }
-
-        /* real number of packets */
-        i_count = ( i_size - p_sys->i_data_begin ) /
-                  p_sys->p_fp->i_min_data_packet_size;
+        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
+    }
 
-        /* calculate the time duration in micro-s */
-        p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
-                   (mtime_t)i_count /
-                   (mtime_t)p_sys->p_fp->i_data_packets_count;
+    return 1;
+}
 
-        if( p_sys->i_length > 0 )
-        {
-            p_input->stream.i_mux_rate =
-                i_size / 50 * (int64_t)1000000 / p_sys->i_length;
-        }
-    }
+/*****************************************************************************
+ * Close: frees unused data
+ *****************************************************************************/
+static void Close( vlc_object_t * p_this )
+{
+    demux_t     *p_demux = (demux_t *)p_this;
 
-    /* We add all info about this stream */
-    p_cat = input_InfoCategory( p_input, "Asf" );
-    if( p_sys->i_length > 0 )
-    {
-        int64_t i_second = p_sys->i_length / (int64_t)1000000;
+    DemuxEnd( p_demux );
 
-        input_AddInfo( p_cat, _("Length"), "%d:%d:%d",
-                       (int)(i_second / 36000),
-                       (int)(( i_second / 60 ) % 60),
-                       (int)(i_second % 60) );
-    }
-    input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_streams );
+    free( p_demux->p_sys );
+}
 
-    if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
-                                 &asf_object_content_description_guid, 0 ) ) )
-    {
-        if( *p_cd->psz_title )
-            input_AddInfo( p_cat, _("Title"), p_cd->psz_title );
-        if( p_cd->psz_author )
-            input_AddInfo( p_cat, _("Author"), p_cd->psz_author );
-        if( p_cd->psz_copyright )
-            input_AddInfo( p_cat, _("Copyright"), p_cd->psz_copyright );
-        if( *p_cd->psz_description )
-            input_AddInfo( p_cat, _("Description"), p_cd->psz_description );
-        if( *p_cd->psz_rating )
-            input_AddInfo( p_cat, _("Rating"), p_cd->psz_rating );
-    }
+/*****************************************************************************
+ * SeekIndex: goto to i_date or i_percent
+ *****************************************************************************/
+static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+    asf_object_index_t *p_index;
+    int64_t i_pos;
 
-    /* FIXME to port to new way */
-    for( i_stream = 0, i = 0; i < 128; i++ )
-    {
-        asf_object_codec_list_t *p_cl =
-            ASF_FindObject( p_sys->p_root->p_hdr,
-                            &asf_object_codec_list_guid, 0 );
+    msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
+             (int)(i_date/1000000), f_pos );
 
-        if( p_sys->stream[i] )
-        {
-            char psz_cat[sizeof(_("Stream "))+10];
-            sprintf( psz_cat, _("Stream %d"), i_stream );
-            p_cat = input_InfoCategory( p_input, psz_cat);
+    p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
 
-            if( p_cl && i_stream < p_cl->i_codec_entries_count )
-            {
-                input_AddInfo( p_cat, _("Codec name"),
-                               p_cl->codec[i_stream].psz_name );
-                input_AddInfo( p_cat, _("Codec description"),
-                               p_cl->codec[i_stream].psz_description );
-            }
-            i_stream++;
-        }
-    }
+    if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
 
-    return VLC_SUCCESS;
+    i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
+    i_pos = p_index->index_entry[i_pos].i_packet_number *
+        p_sys->p_fp->i_min_data_packet_size;
 
-error:
-    ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
-    free( p_sys );
-    return VLC_EGENERIC;
+    return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
 }
 
-
 /*****************************************************************************
- * Demux: read packet and send them to decoders
+ * Control:
  *****************************************************************************/
-static int Demux( input_thread_t *p_input )
+static int Control( demux_t *p_demux, int i_query, va_list args )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
-    vlc_bool_t b_play_audio;
-    int i;
+    demux_sys_t *p_sys = p_demux->p_sys;
+    vlc_meta_t  *p_meta;
+    int64_t     i64, *pi64;
+    double      f, *pf;
+    int         i;
 
-    /* catch seek from user */
-    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
+    switch( i_query )
     {
-        int64_t i_offset;
-
-        msleep( p_input->i_pts_delay );
+    case DEMUX_GET_LENGTH:
+        pi64 = (int64_t*)va_arg( args, int64_t * );
+        *pi64 = p_sys->i_length;
+        return VLC_SUCCESS;
+
+    case DEMUX_GET_TIME:
+        pi64 = (int64_t*)va_arg( args, int64_t * );
+        if( p_sys->i_time < 0 ) return VLC_EGENERIC;
+        *pi64 = p_sys->i_time;
+        return VLC_SUCCESS;
+
+    case DEMUX_SET_TIME:
+        p_sys->i_time = -1;
+        for( i = 0; i < 128 ; i++ )
+            if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
 
-        i_offset = stream_Tell( p_input->s ) - p_sys->i_data_begin;
-        if( i_offset  < 0 )
+        if( p_sys->b_index && p_sys->i_length > 0 )
         {
-            i_offset = 0;
+            i64 = (int64_t)va_arg( args, int64_t );
+            return SeekIndex( p_demux, i64, -1 );
         }
-        if( i_offset % p_sys->p_fp->i_min_data_packet_size > 0 )
+        else
         {
-            i_offset -= i_offset % p_sys->p_fp->i_min_data_packet_size;
+            return demux2_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 );
         }
 
-        if( stream_Seek( p_input->s, i_offset + p_sys->i_data_begin ) )
+    case DEMUX_GET_POSITION:
+        if( p_sys->i_time < 0 ) return VLC_EGENERIC;
+        if( p_sys->i_length > 0 )
         {
-            msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
-            return -1;
+            pf = (double*)va_arg( args, double * );
+            *pf = p_sys->i_time / (double)p_sys->i_length;
+            return VLC_SUCCESS;
         }
+        return demux2_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 );
 
+    case DEMUX_SET_POSITION:
         p_sys->i_time = -1;
         for( i = 0; i < 128 ; i++ )
-        {
-#define p_stream p_sys->stream[i]
-            if( p_stream )
-            {
-                p_stream->i_time = -1;
-            }
-#undef p_stream
-        }
-    }
-
-    /* Check if we need to send the audio data to decoder */
-    b_play_audio = !p_input->stream.control.b_mute;
-
-    for( ;; )
-    {
-        mtime_t i_length;
-        mtime_t i_time_begin = GetMoviePTS( p_sys );
-        int i_result;
-
-        if( p_input->b_die )
-        {
-            break;
-        }
+            if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
 
-        if( ( i_result = DemuxPacket( p_input, b_play_audio ) ) <= 0 )
+        if( p_sys->b_index && p_sys->i_length > 0 )
         {
-            return i_result;
-        }
-        if( i_time_begin == -1 )
-        {
-            i_time_begin = GetMoviePTS( p_sys );
+            f = (double)va_arg( args, double );
+            return SeekIndex( p_demux, -1, f );
         }
         else
         {
-            i_length = GetMoviePTS( p_sys ) - i_time_begin;
-            if( i_length < 0 || i_length >= 40 * 1000 )
-            {
-                break;
-            }
+            return demux2_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 );
         }
-    }
 
-    p_sys->i_time = GetMoviePTS( p_sys );
-    if( p_sys->i_time >= 0 )
-    {
-        input_ClockManageRef( p_input,
-                              p_input->stream.p_selected_program,
-                              p_sys->i_time * 9 / 100 );
-    }
-
-    return( 1 );
-}
-
-/*****************************************************************************
- * Close: frees unused data
- *****************************************************************************/
-static void Close( vlc_object_t * p_this )
-{
-    input_thread_t *p_input = (input_thread_t *)p_this;
-    demux_sys_t    *p_sys = p_input->p_demux_data;
-    int i_stream;
-
-    msg_Dbg( p_input, "Freeing all memory" );
+    case DEMUX_GET_META:
+        p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
+        vlc_meta_Merge( p_meta, p_sys->meta );
+        return VLC_SUCCESS;
 
-    ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
-    for( i_stream = 0; i_stream < 128; i_stream++ )
-    {
-        asf_stream_t *p_stream = p_sys->stream[i_stream];
-
-        if( p_stream )
-        {
-            if( p_stream->p_frame )
-            {
-                block_ChainRelease( p_stream->p_frame );
-            }
-            free( p_stream );
-        }
+    default:
+        return demux2_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 );
     }
-    free( p_sys );
 }
 
-
 /*****************************************************************************
  *
  *****************************************************************************/
 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
 {
-    mtime_t i_time;
-    int     i_stream;
+    mtime_t i_time = -1;
+    int     i;
 
-    i_time = -1;
-    for( i_stream = 0; i_stream < 128 ; i_stream++ )
+    for( i = 0; i < 128 ; i++ )
     {
-        asf_stream_t *p_stream = p_sys->stream[i_stream];
+        asf_track_t *tk = p_sys->track[i];
 
-        if( p_stream && p_stream->p_es && p_stream->i_time > 0)
+        if( tk && tk->p_es && tk->i_time > 0)
         {
-            if( i_time < 0 )
-            {
-                i_time = p_stream->i_time;
-            }
-            else
-            {
-                i_time = __MIN( i_time, p_stream->i_time );
-            }
+            if( i_time < 0 ) i_time = tk->i_time;
+            else i_time = __MIN( i_time, tk->i_time );
         }
     }
 
-    return( i_time );
+    return i_time;
 }
 
 #define GETVALUE2b( bits, var, def ) \
@@ -498,21 +351,21 @@ static mtime_t GetMoviePTS( demux_sys_t *p_sys )
         default: var = def; break;\
     }
 
-static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
+static int DemuxPacket( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_input->p_demux_data;
-    int     i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
-    uint8_t *p_peek;
-    int     i_skip;
+    demux_sys_t *p_sys = p_demux->p_sys;
+    int         i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
+    uint8_t     *p_peek;
+    int         i_skip;
 
-    int     i_packet_size_left;
-    int     i_packet_flags;
-    int     i_packet_property;
+    int         i_packet_size_left;
+    int         i_packet_flags;
+    int         i_packet_property;
 
-    int     b_packet_multiple_payload;
-    int     i_packet_length;
-    int     i_packet_sequence;
-    int     i_packet_padding_length;
+    int         b_packet_multiple_payload;
+    int         i_packet_length;
+    int         i_packet_sequence;
+    int         i_packet_padding_length;
 
     uint32_t    i_packet_send_time;
     uint16_t    i_packet_duration;
@@ -521,11 +374,10 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
     int         i_payload_length_type;
 
 
-    if( stream_Peek( p_input->s, &p_peek,i_data_packet_min)<i_data_packet_min )
+    if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
     {
-        // EOF ?
-        msg_Warn( p_input, "cannot peek while getting new packet, EOF ?" );
-        return( 0 );
+        msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
+        return 0;
     }
     i_skip = 0;
 
@@ -552,7 +404,7 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
     }
     else
     {
-        msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
+        msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
     }
 
     /* sanity check */
@@ -574,7 +426,7 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
     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;   // XXX donnรฉes reellement lu
+//        i_packet_size_left = i_packet_length;   // XXX data really read 
     /* FIXME I have to do that for some file, I don't known why */
     i_packet_size_left = i_data_packet_min;
 
@@ -592,8 +444,9 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
 
     for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
     {
-        asf_stream_t   *p_stream;
+        asf_track_t   *tk;
 
+        int i_packet_keyframe;
         int i_stream_number;
         int i_media_object_number;
         int i_media_object_offset;
@@ -612,8 +465,8 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
             break;
         }
 
-        i_stream_number = p_peek[i_skip] & 0x7f;
-        i_skip++;
+        i_packet_keyframe = p_peek[i_skip] >> 7;
+        i_stream_number = p_peek[i_skip++] & 0x7f;
 
         GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
         GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
@@ -634,8 +487,7 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
         }
         else if( i_replicated_data_length == 1 )
         {
-
-            msg_Dbg( p_input, "found compressed payload" );
+            /* msg_Dbg( p_demux, "found compressed payload" ); */
 
             i_pts = (mtime_t)i_tmp * 1000;
             i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
@@ -665,28 +517,22 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
         {
             break;
         }
-
 #if 0
-         msg_Dbg( p_input,
+         msg_Dbg( p_demux,
                   "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
-                  i_payload + 1,
-                  i_payload_count,
-                  i_stream_number,
-                  i_media_object_number,
-                  i_media_object_offset,
-                  i_replicated_data_length,
-                  i_payload_data_length );
+                  i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
+                  i_media_object_offset, i_replicated_data_length, i_payload_data_length );
 #endif
 
-        if( !( p_stream = p_sys->stream[i_stream_number] ) )
+        if( ( tk = p_sys->track[i_stream_number] ) == NULL )
         {
-            msg_Warn( p_input,
+            msg_Warn( p_demux,
                       "undeclared stream[Id 0x%x]", i_stream_number );
             i_skip += i_payload_data_length;
             continue;   // over payload
         }
 
-        if( !p_stream->p_es )
+        if( !tk->p_es )
         {
             i_skip += i_payload_data_length;
             continue;
@@ -713,38 +559,35 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
             }
 
             /* FIXME I don't use i_media_object_number, sould I ? */
-            if( p_stream->p_frame && i_media_object_offset == 0 )
+            if( tk->p_frame && i_media_object_offset == 0 )
             {
                 /* send complete packet to decoder */
-                block_t *p_gather = block_ChainGather( p_stream->p_frame );
+                block_t *p_gather = block_ChainGather( tk->p_frame );
 
-                es_out_Send( p_input->p_es_out, p_stream->p_es, p_gather );
+                es_out_Send( p_demux->out, tk->p_es, p_gather );
 
-                p_stream->p_frame = NULL;
+                tk->p_frame = NULL;
             }
 
             i_read = i_sub_payload_data_length + i_skip;
-            if( ( p_frag = stream_Block( p_input->s, i_read ) ) == NULL )
+            if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
             {
-                msg_Warn( p_input, "cannot read data" );
-                return( 0 );
+                msg_Warn( p_demux, "cannot read data" );
+                return 0;
             }
             i_packet_size_left -= i_read;
 
             p_frag->p_buffer += i_skip;
             p_frag->i_buffer -= i_skip;
 
-            if( p_stream->p_frame == NULL )
+            if( tk->p_frame == NULL )
             {
-                p_stream->i_time =
+                tk->i_time =
                     ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
 
-                p_frag->i_pts =
-                    input_ClockGetTS( p_input,
-                                      p_input->stream.p_selected_program,
-                                      p_stream->i_time * 9 /100 );
+                p_frag->i_pts = tk->i_time;
 
-                if( p_stream->i_cat != VIDEO_ES )
+                if( tk->i_cat != VIDEO_ES )
                     p_frag->i_dts = p_frag->i_pts;
                 else
                 {
@@ -753,17 +596,16 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
                 }
             }
 
-            block_ChainAppend( &p_stream->p_frame, p_frag );
+            block_ChainAppend( &tk->p_frame, p_frag );
 
             i_skip = 0;
             if( i_packet_size_left > 0 )
             {
-                if( stream_Peek( p_input->s, &p_peek, i_packet_size_left )
+                if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
                                                          < i_packet_size_left )
                 {
-                    // EOF ?
-                    msg_Warn( p_input, "cannot peek, EOF ?" );
-                    return( 0 );
+                    msg_Warn( p_demux, "cannot peek, EOF ?" );
+                    return 0;
                 }
             }
         }
@@ -771,26 +613,422 @@ static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
 
     if( i_packet_size_left > 0 )
     {
-        if( stream_Read( p_input->s, NULL, i_packet_size_left )
+        if( stream_Read( p_demux->s, NULL, i_packet_size_left )
                                                          < i_packet_size_left )
         {
-            msg_Warn( p_input, "cannot skip data, EOF ?" );
-            return( 0 );
+            msg_Warn( p_demux, "cannot skip data, EOF ?" );
+            return 0;
         }
     }
 
-    return( 1 );
+    return 1;
 
 loop_error_recovery:
-    msg_Warn( p_input, "unsupported packet header" );
+    msg_Warn( p_demux, "unsupported packet header" );
     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
     {
-        msg_Err( p_input, "unsupported packet header, fatal error" );
-        return( -1 );
+        msg_Err( p_demux, "unsupported packet header, fatal error" );
+        return -1;
     }
-    stream_Read( p_input->s, NULL, i_data_packet_min );
+    stream_Read( p_demux->s, NULL, i_data_packet_min );
 
-    return( 1 );
+    return 1;
 }
 
+/*****************************************************************************
+ *
+ *****************************************************************************/
+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;
+    asf_object_content_description_t *p_cd;
+    asf_object_index_t *p_index;
+    vlc_bool_t b_index;
+
+    /* init context */
+    p_sys->i_time   = -1;
+    p_sys->i_length = 0;
+    p_sys->i_bitrate = 0;
+    p_sys->p_root   = NULL;
+    p_sys->p_fp     = NULL;
+    p_sys->b_index  = 0;
+    p_sys->i_track  = 0;
+    for( i = 0; i < 128; i++ )
+    {
+        p_sys->track[i] = NULL;
+    }
+    p_sys->i_data_begin = -1;
+    p_sys->i_data_end   = -1;
+    p_sys->meta         = NULL;
+
+    /* Now load all object ( except raw data ) */
+    stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
+    if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
+    {
+        msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
+        return VLC_EGENERIC;
+    }
+    p_sys->p_fp = p_sys->p_root->p_fp;
+
+    if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
+    {
+        msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
+        goto error;
+    }
+
+    p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
+                                      &asf_object_stream_properties_guid );
+    if( p_sys->i_track <= 0 )
+    {
+        msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
+        goto error;
+    }
+
+    /* check if index is available */
+    p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
+    b_index = p_index && p_index->i_index_entry_count;
+
+    msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
+
+    for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
+    {
+        asf_track_t    *tk;
+        asf_object_stream_properties_t *p_sp;
+        vlc_bool_t b_access_selected;
+
+        p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
+                               &asf_object_stream_properties_guid,
+                               i_stream );
+
+        tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
+        memset( tk, 0, sizeof( asf_track_t ) );
+
+        tk->i_time = -1;
+        tk->p_sp = p_sp;
+        tk->p_es = NULL;
+        tk->p_frame = NULL;
+
+        /* Check (in case of mms) if this track is selected (ie will receive data) */
+        if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
+                             p_sp->i_stream_number, &b_access_selected ) &&
+            !b_access_selected )
+        {
+            tk->i_cat = UNKNOWN_ES;
+            msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
+                     p_sp->i_stream_number );
+            continue;
+        }
+
+        if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
+            p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
+        {
+            es_format_t fmt;
+            uint8_t *p_data = p_sp->p_type_specific_data;
+            int i_format;
+
+            es_format_Init( &fmt, AUDIO_ES, 0 );
+            i_format = GetWLE( &p_data[0] );
+            wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
+            fmt.audio.i_channels        = GetWLE(  &p_data[2] );
+            fmt.audio.i_rate      = GetDWLE( &p_data[4] );
+            fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
+            fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
+            fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
+
+            if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
+                i_format != WAVE_FORMAT_MPEGLAYER3 &&
+                i_format != WAVE_FORMAT_MPEG )
+            {
+                fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
+                                     p_sp->i_type_specific_data_length -
+                                     sizeof( WAVEFORMATEX ) );
+                fmt.p_extra = malloc( fmt.i_extra );
+                memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
+                        fmt.i_extra );
+            }
+
+            tk->i_cat = AUDIO_ES;
+            tk->p_es = es_out_Add( p_demux->out, &fmt );
+            es_format_Clean( &fmt );
+
+            msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
+                    GetWLE( p_data ), p_sp->i_stream_number );
+        }
+        else if( ASF_CmpGUID( &p_sp->i_stream_type,
+                              &asf_object_stream_type_video ) &&
+                 p_sp->i_type_specific_data_length >= 11 +
+                 sizeof( BITMAPINFOHEADER ) )
+        {
+            es_format_t  fmt;
+            uint8_t      *p_data = &p_sp->p_type_specific_data[11];
+
+            es_format_Init( &fmt, VIDEO_ES,
+                            VLC_FOURCC( p_data[16], p_data[17],
+                                        p_data[18], p_data[19] ) );
+            fmt.video.i_width = GetDWLE( p_data + 4 );
+            fmt.video.i_height= GetDWLE( p_data + 8 );
+
+
+            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;
+            }
+
+            if( p_sp->i_type_specific_data_length > 11 +
+                sizeof( BITMAPINFOHEADER ) )
+            {
+                fmt.i_extra = __MIN( GetDWLE( p_data ),
+                                     p_sp->i_type_specific_data_length - 11 -
+                                     sizeof( BITMAPINFOHEADER ) );
+                fmt.p_extra = malloc( fmt.i_extra );
+                memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
+                        fmt.i_extra );
+            }
+
+            /* Look for an aspect ratio */
+            if( p_sys->p_root->p_metadata )
+            {
+                asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
+                int i_aspect_x = 0, i_aspect_y = 0;
+                unsigned int i;
+
+                for( i = 0; i < p_meta->i_record_entries_count; i++ )
+                {
+                    if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
+                    {
+                        if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
+                            p_meta->record[i].i_stream ==
+                            p_sp->i_stream_number )
+                            i_aspect_x = p_meta->record[i].i_val;
+                    }
+                    if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
+                    {
+                        if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
+                            p_meta->record[i].i_stream ==
+                            p_sp->i_stream_number )
+                            i_aspect_y = p_meta->record[i].i_val;
+                    }
+                }
+
+                if( i_aspect_x && i_aspect_y )
+                {
+                    fmt.video.i_aspect = i_aspect_x *
+                        (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 );
+            es_format_Clean( &fmt );
+
+            /* If there is a video track then use the index for seeking */
+            p_sys->b_index = b_index;
+
+            msg_Dbg( p_demux, "added new video stream(ID:%d)",
+                     p_sp->i_stream_number );
+        }
+        else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
+            p_sp->i_type_specific_data_length >= 64 )
+        {
+            /* Now follows a 64 byte header of which we don't know much */
+            es_format_t fmt;
+            guid_t  *p_ref  = (guid_t *)p_sp->p_type_specific_data;
+            uint8_t *p_data = p_sp->p_type_specific_data + 64;
+            unsigned int i_data = p_sp->i_type_specific_data_length - 64;
+
+            msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
+            if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) && 
+                i_data >= sizeof( WAVEFORMATEX ) - 2)
+            {
+                int      i_format;
+                es_format_Init( &fmt, AUDIO_ES, 0 );
+                i_format = GetWLE( &p_data[0] );
+                if( i_format == 0 ) 
+                    fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
+                else
+                    wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
+                fmt.audio.i_channels        = GetWLE(  &p_data[2] );
+                fmt.audio.i_rate      = GetDWLE( &p_data[4] );
+                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;
+
+                if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
+                    i_format != WAVE_FORMAT_MPEGLAYER3 &&
+                    i_format != WAVE_FORMAT_MPEG )
+                {
+                    fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
+                                         p_sp->i_type_specific_data_length -
+                                         sizeof( WAVEFORMATEX ) );
+                    fmt.p_extra = malloc( fmt.i_extra );
+                    memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
+                        fmt.i_extra );
+                }
+
+                tk->i_cat = AUDIO_ES;
+                tk->p_es = es_out_Add( p_demux->out, &fmt );
+                es_format_Clean( &fmt );
+
+                msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
+                    i_format, p_sp->i_stream_number );
+            }
+        }
+        else
+        {
+            tk->i_cat = UNKNOWN_ES;
+            msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
+                     p_sp->i_stream_number );
+        }
+    }
+
+    p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
+    if( p_sys->p_root->p_data->i_object_size != 0 )
+    { /* local file */
+        p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
+                                    p_sys->p_root->p_data->i_object_size;
+    }
+    else
+    { /* live/broacast */
+        p_sys->i_data_end = -1;
+    }
+
+    /* go to first packet */
+    stream_Seek( p_demux->s, p_sys->i_data_begin );
+
+    /* try to calculate movie time */
+    if( p_sys->p_fp->i_data_packets_count > 0 )
+    {
+        int64_t i_count;
+        int64_t i_size = stream_Size( p_demux->s );
+
+        if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
+        {
+            i_size = p_sys->i_data_end;
+        }
+
+        /* real number of packets */
+        i_count = ( i_size - p_sys->i_data_begin ) /
+                  p_sys->p_fp->i_min_data_packet_size;
+
+        /* calculate the time duration in micro-s */
+        p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
+                   (mtime_t)i_count /
+                   (mtime_t)p_sys->p_fp->i_data_packets_count;
+
+        if( p_sys->i_length > 0 )
+        {
+            p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
+        }
+    }
+
+    /* Create meta information */
+    p_sys->meta = vlc_meta_New();
+
+    if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
+                                 &asf_object_content_description_guid, 0 ) ) )
+    {
+        if( p_cd->psz_title && *p_cd->psz_title )
+        {
+            vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
+        }
+        if( p_cd->psz_artist && *p_cd->psz_artist )
+        {
+             vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
+        }
+        if( p_cd->psz_copyright && *p_cd->psz_copyright )
+        {
+            vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
+        }
+        if( p_cd->psz_description && *p_cd->psz_description )
+        {
+            vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
+        }
+        if( p_cd->psz_rating && *p_cd->psz_rating )
+        {
+            vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
+        }
+    }
+    /// \tood Fix Child meta for ASF tracks
+#if 0
+    for( i_stream = 0, i = 0; i < 128; i++ )
+    {
+        asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
+                                                        &asf_object_codec_list_guid, 0 );
+
+        if( p_sys->track[i] )
+        {
+            vlc_meta_t *tk = vlc_meta_New();
+            TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
+
+            if( p_cl && i_stream < p_cl->i_codec_entries_count )
+            {
+                if( p_cl->codec[i_stream].psz_name &&
+                    *p_cl->codec[i_stream].psz_name )
+                {
+                    vlc_meta_Add( tk, VLC_META_CODEC_NAME,
+                                  p_cl->codec[i_stream].psz_name );
+                }
+                if( p_cl->codec[i_stream].psz_description &&
+                    *p_cl->codec[i_stream].psz_description )
+                {
+                    vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
+                                  p_cl->codec[i_stream].psz_description );
+                }
+            }
+            i_stream++;
+        }
+    }
+#endif
+
+    es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
+    return VLC_SUCCESS;
+
+error:
+    ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
+    return VLC_EGENERIC;
+}
+/*****************************************************************************
+ *
+ *****************************************************************************/
+static void DemuxEnd( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+    int         i;
+
+    if( p_sys->p_root )
+    {
+        ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
+        p_sys->p_root = NULL;
+    }
+    if( p_sys->meta )
+    {
+        vlc_meta_Delete( p_sys->meta );
+        p_sys->meta = NULL;
+    }
+
+    for( i = 0; i < 128; i++ )
+    {
+        asf_track_t *tk = p_sys->track[i];
+
+        if( tk )
+        {
+            if( tk->p_frame )
+            {
+                block_ChainRelease( tk->p_frame );
+            }
+            if( tk->p_es )
+            {
+                es_out_Del( p_demux->out, tk->p_es );
+            }
+            free( tk );
+        }
+        p_sys->track[i] = 0;
+    }
+}