]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
e6ad882a184ccd35007a3b752110110f4ff51e2d
[vlc] / modules / demux / asf / asf.c
1 /*****************************************************************************
2  * asf.c : ASF demux module
3  *****************************************************************************
4  * Copyright (C) 2002-2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "vlc_meta.h"
33
34 #include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
35 #include "libasf.h"
36
37 /* TODO
38  *  - add support for the newly added object: language, bitrate,
39  *                                            extended stream properties.
40  */
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open  ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
47
48 vlc_module_begin();
49     set_category( CAT_INPUT );
50     set_subcategory( SUBCAT_INPUT_DEMUX );
51     set_description( _("ASF v1.0 demuxer") );
52     set_capability( "demux2", 200 );
53     set_callbacks( Open, Close );
54     add_shortcut( "asf" );
55 vlc_module_end();
56
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int Demux  ( demux_t * );
62 static int Control( demux_t *, int i_query, va_list args );
63
64 typedef struct
65 {
66     int i_cat;
67
68     es_out_id_t     *p_es;
69
70     asf_object_stream_properties_t *p_sp;
71
72     mtime_t i_time;
73
74     block_t         *p_frame; /* use to gather complete frame */
75
76 } asf_track_t;
77
78 struct demux_sys_t
79 {
80     mtime_t             i_time;     /* s */
81     mtime_t             i_length;   /* length of file file */
82     int64_t             i_bitrate;  /* global file bitrate */
83
84     asf_object_root_t            *p_root;
85     asf_object_file_properties_t *p_fp;
86
87     unsigned int        i_track;
88     asf_track_t         *track[128];
89
90     int64_t             i_data_begin;
91     int64_t             i_data_end;
92
93     vlc_meta_t          *meta;
94 };
95
96 static mtime_t  GetMoviePTS( demux_sys_t * );
97 static int      DemuxInit( demux_t * );
98 static void     DemuxEnd( demux_t * );
99 static int      DemuxPacket( demux_t * );
100
101 /*****************************************************************************
102  * Open: check file and initializes ASF structures
103  *****************************************************************************/
104 static int Open( vlc_object_t * p_this )
105 {
106     demux_t     *p_demux = (demux_t *)p_this;
107     demux_sys_t *p_sys;
108     guid_t      guid;
109     uint8_t     *p_peek;
110
111     /* A little test to see if it could be a asf stream */
112     if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
113
114     ASF_GetGUID( &guid, p_peek );
115     if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
116
117     /* Set p_demux fields */
118     p_demux->pf_demux = Demux;
119     p_demux->pf_control = Control;
120     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
121     memset( p_sys, 0, sizeof( demux_sys_t ) );
122
123     /* Load the headers */
124     if( DemuxInit( p_demux ) )
125     {
126         return VLC_EGENERIC;
127     }
128     return VLC_SUCCESS;
129 }
130
131
132 /*****************************************************************************
133  * Demux: read packet and send them to decoders
134  *****************************************************************************/
135 static int Demux( demux_t *p_demux )
136 {
137     demux_sys_t *p_sys = p_demux->p_sys;
138
139     for( ;; )
140     {
141         uint8_t *p_peek;
142         mtime_t i_length;
143         mtime_t i_time_begin = GetMoviePTS( p_sys );
144         int i_result;
145
146         if( p_demux->b_die )
147         {
148             break;
149         }
150
151         /* Check if we have concatenated files */
152         if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
153         {
154             guid_t guid;
155
156             ASF_GetGUID( &guid, p_peek );
157             if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
158             {
159                 msg_Warn( p_demux, "found a new ASF header" );
160                 /* We end this stream */
161                 DemuxEnd( p_demux );
162
163                 /* And we prepare to read the next one */
164                 if( DemuxInit( p_demux ) )
165                 {
166                     msg_Err( p_demux, "failed to load the new header" );
167                     return 0;
168                 }
169                 continue;
170             }
171         }
172
173         /* Read and demux a packet */
174         if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
175         {
176             return i_result;
177         }
178         if( i_time_begin == -1 )
179         {
180             i_time_begin = GetMoviePTS( p_sys );
181         }
182         else
183         {
184             i_length = GetMoviePTS( p_sys ) - i_time_begin;
185             if( i_length < 0 || i_length >= 40 * 1000 )
186             {
187                 break;
188             }
189         }
190     }
191
192     /* Set the PCR */
193     p_sys->i_time = GetMoviePTS( p_sys );
194     if( p_sys->i_time >= 0 )
195     {
196         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
197     }
198
199     return 1;
200 }
201
202 /*****************************************************************************
203  * Close: frees unused data
204  *****************************************************************************/
205 static void Close( vlc_object_t * p_this )
206 {
207     demux_t     *p_demux = (demux_t *)p_this;
208
209     DemuxEnd( p_demux );
210
211     free( p_demux->p_sys );
212 }
213
214 /*****************************************************************************
215  * Control:
216  *****************************************************************************/
217 static int Control( demux_t *p_demux, int i_query, va_list args )
218 {
219     demux_sys_t *p_sys = p_demux->p_sys;
220     int64_t     *pi64;
221     int         i;
222     vlc_meta_t *p_meta;
223
224     switch( i_query )
225     {
226         case DEMUX_SET_TIME:
227             return VLC_EGENERIC;
228
229         case DEMUX_GET_LENGTH:
230             pi64 = (int64_t*)va_arg( args, int64_t * );
231             *pi64 = p_sys->i_length;
232             return VLC_SUCCESS;
233
234         case DEMUX_GET_META:
235             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
236             vlc_meta_Merge( p_meta, p_sys->meta );
237             return VLC_SUCCESS;
238
239         case DEMUX_SET_POSITION:
240             p_sys->i_time = -1;
241             for( i = 0; i < 128 ; i++ )
242             {
243                 asf_track_t *tk = p_sys->track[i];
244                 if( tk ) tk->i_time = -1;
245             }
246
247         default:
248             return demux2_vaControlHelper( p_demux->s,
249                                            p_sys->i_data_begin, p_sys->i_data_end,
250                                            p_sys->i_bitrate, p_sys->p_fp->i_min_data_packet_size,
251                                            i_query, args );
252     }
253 }
254
255 /*****************************************************************************
256  *
257  *****************************************************************************/
258 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
259 {
260     mtime_t i_time;
261     int     i;
262
263     i_time = -1;
264     for( i = 0; i < 128 ; i++ )
265     {
266         asf_track_t *tk = p_sys->track[i];
267
268         if( tk && tk->p_es && tk->i_time > 0)
269         {
270             if( i_time < 0 )
271             {
272                 i_time = tk->i_time;
273             }
274             else
275             {
276                 i_time = __MIN( i_time, tk->i_time );
277             }
278         }
279     }
280
281     return i_time;
282 }
283
284 #define GETVALUE2b( bits, var, def ) \
285     switch( (bits)&0x03 ) \
286     { \
287         case 1: var = p_peek[i_skip]; i_skip++; break; \
288         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
289         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
290         case 0: \
291         default: var = def; break;\
292     }
293
294 static int DemuxPacket( demux_t *p_demux )
295 {
296     demux_sys_t *p_sys = p_demux->p_sys;
297     int         i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
298     uint8_t     *p_peek;
299     int         i_skip;
300
301     int         i_packet_size_left;
302     int         i_packet_flags;
303     int         i_packet_property;
304
305     int         b_packet_multiple_payload;
306     int         i_packet_length;
307     int         i_packet_sequence;
308     int         i_packet_padding_length;
309
310     uint32_t    i_packet_send_time;
311     uint16_t    i_packet_duration;
312     int         i_payload;
313     int         i_payload_count;
314     int         i_payload_length_type;
315
316
317     if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
318     {
319         msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
320         return 0;
321     }
322     i_skip = 0;
323
324     /* *** parse error correction if present *** */
325     if( p_peek[0]&0x80 )
326     {
327         unsigned int i_error_correction_length_type;
328         unsigned int i_error_correction_data_length;
329         unsigned int i_opaque_data_present;
330
331         i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
332         i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
333         i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
334         i_skip += 1; // skip error correction flags
335
336         if( i_error_correction_length_type != 0x00 ||
337             i_opaque_data_present != 0 ||
338             i_error_correction_data_length != 0x02 )
339         {
340             goto loop_error_recovery;
341         }
342
343         i_skip += i_error_correction_data_length;
344     }
345     else
346     {
347         msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
348     }
349
350     /* sanity check */
351     if( i_skip + 2 >= i_data_packet_min )
352     {
353         goto loop_error_recovery;
354     }
355
356     i_packet_flags = p_peek[i_skip]; i_skip++;
357     i_packet_property = p_peek[i_skip]; i_skip++;
358
359     b_packet_multiple_payload = i_packet_flags&0x01;
360
361     /* read some value */
362     GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
363     GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
364     GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
365
366     i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
367     i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
368
369 //        i_packet_size_left = i_packet_length;   // XXX data really read 
370     /* FIXME I have to do that for some file, I don't known why */
371     i_packet_size_left = i_data_packet_min;
372
373     if( b_packet_multiple_payload )
374     {
375         i_payload_count = p_peek[i_skip] & 0x3f;
376         i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
377         i_skip++;
378     }
379     else
380     {
381         i_payload_count = 1;
382         i_payload_length_type = 0x02; // unused
383     }
384
385     for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
386     {
387         asf_track_t   *tk;
388
389         int i_stream_number;
390         int i_media_object_number;
391         int i_media_object_offset;
392         int i_replicated_data_length;
393         int i_payload_data_length;
394         int i_payload_data_pos;
395         int i_sub_payload_data_length;
396         int i_tmp;
397
398         mtime_t i_pts;
399         mtime_t i_pts_delta;
400
401         if( i_skip >= i_packet_size_left )
402         {
403             /* prevent some segfault with invalid file */
404             break;
405         }
406
407         i_stream_number = p_peek[i_skip] & 0x7f;
408         i_skip++;
409
410         GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
411         GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
412         GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
413
414         if( i_replicated_data_length > 1 ) // should be at least 8 bytes
415         {
416             i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
417             i_skip += i_replicated_data_length;
418             i_pts_delta = 0;
419
420             i_media_object_offset = i_tmp;
421
422             if( i_skip >= i_packet_size_left )
423             {
424                 break;
425             }
426         }
427         else if( i_replicated_data_length == 1 )
428         {
429             /* msg_Dbg( p_demux, "found compressed payload" ); */
430
431             i_pts = (mtime_t)i_tmp * 1000;
432             i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
433
434             i_media_object_offset = 0;
435         }
436         else
437         {
438             i_pts = (mtime_t)i_packet_send_time * 1000;
439             i_pts_delta = 0;
440
441             i_media_object_offset = i_tmp;
442         }
443
444         i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
445         if( b_packet_multiple_payload )
446         {
447             GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
448         }
449         else
450         {
451             i_payload_data_length = i_packet_length -
452                                         i_packet_padding_length - i_skip;
453         }
454
455         if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
456         {
457             break;
458         }
459 #if 0
460          msg_Dbg( p_demux,
461                   "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
462                   i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
463                   i_media_object_offset, i_replicated_data_length, i_payload_data_length );
464 #endif
465
466         if( ( tk = p_sys->track[i_stream_number] ) == NULL )
467         {
468             msg_Warn( p_demux,
469                       "undeclared stream[Id 0x%x]", i_stream_number );
470             i_skip += i_payload_data_length;
471             continue;   // over payload
472         }
473
474         if( !tk->p_es )
475         {
476             i_skip += i_payload_data_length;
477             continue;
478         }
479
480
481         for( i_payload_data_pos = 0;
482              i_payload_data_pos < i_payload_data_length &&
483                     i_packet_size_left > 0;
484              i_payload_data_pos += i_sub_payload_data_length )
485         {
486             block_t *p_frag;
487             int i_read;
488
489             // read sub payload length
490             if( i_replicated_data_length == 1 )
491             {
492                 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
493                 i_payload_data_pos++;
494             }
495             else
496             {
497                 i_sub_payload_data_length = i_payload_data_length;
498             }
499
500             /* FIXME I don't use i_media_object_number, sould I ? */
501             if( tk->p_frame && i_media_object_offset == 0 )
502             {
503                 /* send complete packet to decoder */
504                 block_t *p_gather = block_ChainGather( tk->p_frame );
505
506                 es_out_Send( p_demux->out, tk->p_es, p_gather );
507
508                 tk->p_frame = NULL;
509             }
510
511             i_read = i_sub_payload_data_length + i_skip;
512             if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
513             {
514                 msg_Warn( p_demux, "cannot read data" );
515                 return 0;
516             }
517             i_packet_size_left -= i_read;
518
519             p_frag->p_buffer += i_skip;
520             p_frag->i_buffer -= i_skip;
521
522             if( tk->p_frame == NULL )
523             {
524                 tk->i_time =
525                     ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
526
527                 p_frag->i_pts = tk->i_time;
528
529                 if( tk->i_cat != VIDEO_ES )
530                     p_frag->i_dts = p_frag->i_pts;
531                 else
532                 {
533                     p_frag->i_dts = p_frag->i_pts;
534                     p_frag->i_pts = 0;
535                 }
536             }
537
538             block_ChainAppend( &tk->p_frame, p_frag );
539
540             i_skip = 0;
541             if( i_packet_size_left > 0 )
542             {
543                 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
544                                                          < i_packet_size_left )
545                 {
546                     msg_Warn( p_demux, "cannot peek, EOF ?" );
547                     return 0;
548                 }
549             }
550         }
551     }
552
553     if( i_packet_size_left > 0 )
554     {
555         if( stream_Read( p_demux->s, NULL, i_packet_size_left )
556                                                          < i_packet_size_left )
557         {
558             msg_Warn( p_demux, "cannot skip data, EOF ?" );
559             return 0;
560         }
561     }
562
563     return 1;
564
565 loop_error_recovery:
566     msg_Warn( p_demux, "unsupported packet header" );
567     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
568     {
569         msg_Err( p_demux, "unsupported packet header, fatal error" );
570         return -1;
571     }
572     stream_Read( p_demux->s, NULL, i_data_packet_min );
573
574     return 1;
575 }
576
577 /*****************************************************************************
578  *
579  *****************************************************************************/
580 static int DemuxInit( demux_t *p_demux )
581 {
582     demux_sys_t *p_sys = p_demux->p_sys;
583     vlc_bool_t  b_seekable;
584     int         i;
585
586     unsigned int    i_stream;
587     asf_object_content_description_t *p_cd;
588
589     /* init context */
590     p_sys->i_time   = -1;
591     p_sys->i_length = 0;
592     p_sys->i_bitrate = 0;
593     p_sys->p_root   = NULL;
594     p_sys->p_fp     = NULL;
595     p_sys->i_track  = 0;
596     for( i = 0; i < 128; i++ )
597     {
598         p_sys->track[i] = NULL;
599     }
600     p_sys->i_data_begin = -1;
601     p_sys->i_data_end   = -1;
602     p_sys->meta         = NULL;
603
604     /* Now load all object ( except raw data ) */
605     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
606     if( (p_sys->p_root = ASF_ReadObjectRoot( p_demux->s, b_seekable )) == NULL )
607     {
608         msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
609         return VLC_EGENERIC;
610     }
611     p_sys->p_fp = p_sys->p_root->p_fp;
612
613     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
614     {
615         msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
616         goto error;
617     }
618
619     p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
620                                       &asf_object_stream_properties_guid );
621     if( p_sys->i_track <= 0 )
622     {
623         msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
624         goto error;
625     }
626
627     msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
628
629     for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
630     {
631         asf_track_t    *tk;
632         asf_object_stream_properties_t *p_sp;
633         vlc_bool_t b_access_selected;
634
635         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
636                                &asf_object_stream_properties_guid,
637                                i_stream );
638
639         tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
640         memset( tk, 0, sizeof( asf_track_t ) );
641
642         tk->i_time = -1;
643         tk->p_sp = p_sp;
644         tk->p_es = NULL;
645         tk->p_frame = NULL;
646
647         /* Check (in case of mms) if this track is selected (ie will receive data) */
648         if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
649                              p_sp->i_stream_number, &b_access_selected ) &&
650             !b_access_selected )
651         {
652             tk->i_cat = UNKNOWN_ES;
653             msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
654                      p_sp->i_stream_number );
655             continue;
656         }
657
658         if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
659             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
660         {
661             es_format_t fmt;
662             uint8_t *p_data = p_sp->p_type_specific_data;
663             int i_format;
664
665             es_format_Init( &fmt, AUDIO_ES, 0 );
666             i_format = GetWLE( &p_data[0] );
667             wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
668             fmt.audio.i_channels        = GetWLE(  &p_data[2] );
669             fmt.audio.i_rate      = GetDWLE( &p_data[4] );
670             fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
671             fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
672             fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
673
674             if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
675                 i_format != WAVE_FORMAT_MPEGLAYER3 &&
676                 i_format != WAVE_FORMAT_MPEG )
677             {
678                 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
679                                      p_sp->i_type_specific_data_length -
680                                      sizeof( WAVEFORMATEX ) );
681                 fmt.p_extra = malloc( fmt.i_extra );
682                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
683                         fmt.i_extra );
684             }
685
686             tk->i_cat = AUDIO_ES;
687             tk->p_es = es_out_Add( p_demux->out, &fmt );
688             es_format_Clean( &fmt );
689
690             msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
691                     GetWLE( p_data ), p_sp->i_stream_number );
692         }
693         else if( ASF_CmpGUID( &p_sp->i_stream_type,
694                               &asf_object_stream_type_video ) &&
695                  p_sp->i_type_specific_data_length >= 11 +
696                  sizeof( BITMAPINFOHEADER ) )
697         {
698             es_format_t  fmt;
699             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
700
701             es_format_Init( &fmt, VIDEO_ES,
702                             VLC_FOURCC( p_data[16], p_data[17],
703                                         p_data[18], p_data[19] ) );
704             fmt.video.i_width = GetDWLE( p_data + 4 );
705             fmt.video.i_height= GetDWLE( p_data + 8 );
706
707             if( p_sp->i_type_specific_data_length > 11 +
708                 sizeof( BITMAPINFOHEADER ) )
709             {
710                 fmt.i_extra = __MIN( GetDWLE( p_data ),
711                                      p_sp->i_type_specific_data_length - 11 -
712                                      sizeof( BITMAPINFOHEADER ) );
713                 fmt.p_extra = malloc( fmt.i_extra );
714                 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
715                         fmt.i_extra );
716             }
717
718             /* Look for an aspect ratio */
719             if( p_sys->p_root->p_metadata )
720             {
721                 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
722                 int i_aspect_x = 0, i_aspect_y = 0;
723                 unsigned int i;
724
725                 for( i = 0; i < p_meta->i_record_entries_count; i++ )
726                 {
727                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
728                     {
729                         if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
730                             p_meta->record[i].i_stream ==
731                             p_sp->i_stream_number )
732                             i_aspect_x = p_meta->record[i].i_val;
733                     }
734                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
735                     {
736                         if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
737                             p_meta->record[i].i_stream ==
738                             p_sp->i_stream_number )
739                             i_aspect_y = p_meta->record[i].i_val;
740                     }
741                 }
742
743                 if( i_aspect_x && i_aspect_y )
744                 {
745                     fmt.video.i_aspect = i_aspect_x *
746                         (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
747                         fmt.video.i_height / i_aspect_y;
748                 }
749             }
750
751             tk->i_cat = VIDEO_ES;
752             tk->p_es = es_out_Add( p_demux->out, &fmt );
753             es_format_Clean( &fmt );
754
755             msg_Dbg( p_demux, "added new video stream(ID:%d)",
756                      p_sp->i_stream_number );
757         }
758         else
759         {
760             tk->i_cat = UNKNOWN_ES;
761             msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
762                      p_sp->i_stream_number );
763         }
764     }
765
766     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
767     if( p_sys->p_root->p_data->i_object_size != 0 )
768     { /* local file */
769         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
770                                     p_sys->p_root->p_data->i_object_size;
771     }
772     else
773     { /* live/broacast */
774         p_sys->i_data_end = -1;
775     }
776
777
778     /* go to first packet */
779     stream_Seek( p_demux->s, p_sys->i_data_begin );
780
781     /* try to calculate movie time */
782     if( p_sys->p_fp->i_data_packets_count > 0 )
783     {
784         int64_t i_count;
785         int64_t i_size = stream_Size( p_demux->s );
786
787         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
788         {
789             i_size = p_sys->i_data_end;
790         }
791
792         /* real number of packets */
793         i_count = ( i_size - p_sys->i_data_begin ) /
794                   p_sys->p_fp->i_min_data_packet_size;
795
796         /* calculate the time duration in micro-s */
797         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
798                    (mtime_t)i_count /
799                    (mtime_t)p_sys->p_fp->i_data_packets_count;
800
801         if( p_sys->i_length > 0 )
802         {
803             p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
804         }
805     }
806
807     /* Create meta information */
808     p_sys->meta = vlc_meta_New();
809
810     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
811                                  &asf_object_content_description_guid, 0 ) ) )
812     {
813         if( p_cd->psz_title && *p_cd->psz_title )
814         {
815             vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
816         }
817         if( p_cd->psz_author && *p_cd->psz_author )
818         {
819              vlc_meta_SetAuthor( p_sys->meta, p_cd->psz_author );
820         }
821         if( p_cd->psz_copyright && *p_cd->psz_copyright )
822         {
823             vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
824         }
825         if( p_cd->psz_description && *p_cd->psz_description )
826         {
827             vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
828         }
829         if( p_cd->psz_rating && *p_cd->psz_rating )
830         {
831             vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
832         }
833     }
834     /// \tood Fix Child meta for ASF tracks
835 #if 0
836     for( i_stream = 0, i = 0; i < 128; i++ )
837     {
838         asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
839                                                         &asf_object_codec_list_guid, 0 );
840
841         if( p_sys->track[i] )
842         {
843             vlc_meta_t *tk = vlc_meta_New();
844             TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
845
846             if( p_cl && i_stream < p_cl->i_codec_entries_count )
847             {
848                 if( p_cl->codec[i_stream].psz_name &&
849                     *p_cl->codec[i_stream].psz_name )
850                 {
851                     vlc_meta_Add( tk, VLC_META_CODEC_NAME,
852                                   p_cl->codec[i_stream].psz_name );
853                 }
854                 if( p_cl->codec[i_stream].psz_description &&
855                     *p_cl->codec[i_stream].psz_description )
856                 {
857                     vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
858                                   p_cl->codec[i_stream].psz_description );
859                 }
860             }
861             i_stream++;
862         }
863     }
864 #endif
865
866     es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
867     return VLC_SUCCESS;
868
869 error:
870     ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
871     return VLC_EGENERIC;
872 }
873 /*****************************************************************************
874  *
875  *****************************************************************************/
876 static void DemuxEnd( demux_t *p_demux )
877 {
878     demux_sys_t *p_sys = p_demux->p_sys;
879     int         i;
880
881     if( p_sys->p_root )
882     {
883         ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
884         p_sys->p_root = NULL;
885     }
886     if( p_sys->meta )
887     {
888         vlc_meta_Delete( p_sys->meta );
889         p_sys->meta = NULL;
890     }
891
892     for( i = 0; i < 128; i++ )
893     {
894         asf_track_t *tk = p_sys->track[i];
895
896         if( tk )
897         {
898             if( tk->p_frame )
899             {
900                 block_ChainRelease( tk->p_frame );
901             }
902             if( tk->p_es )
903             {
904                 es_out_Del( p_demux->out, tk->p_es );
905             }
906             free( tk );
907         }
908         p_sys->track[i] = 0;
909     }
910 }
911