]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
Removes trailing spaces. Removes tabs.
[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
28 #include <vlc/vlc.h>
29 #include <vlc_demux.h>
30 #include <vlc_interface.h>
31
32 #include <vlc_meta.h>
33 #include <vlc_access.h>                /* GET_PRIVATE_ID_STATE */
34 #include <vlc_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_bool_t          b_index;
94     vlc_meta_t          *meta;
95 };
96
97 static mtime_t  GetMoviePTS( demux_sys_t * );
98 static int      DemuxInit( demux_t * );
99 static void     DemuxEnd( demux_t * );
100 static int      DemuxPacket( demux_t * );
101
102 /*****************************************************************************
103  * Open: check file and initializes ASF structures
104  *****************************************************************************/
105 static int Open( vlc_object_t * p_this )
106 {
107     demux_t     *p_demux = (demux_t *)p_this;
108     demux_sys_t *p_sys;
109     guid_t      guid;
110     const uint8_t     *p_peek;
111
112     /* A little test to see if it could be a asf stream */
113     if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
114
115     ASF_GetGUID( &guid, p_peek );
116     if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
117
118     /* Set p_demux fields */
119     p_demux->pf_demux = Demux;
120     p_demux->pf_control = Control;
121     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
122     memset( p_sys, 0, sizeof( demux_sys_t ) );
123
124     /* Load the headers */
125     if( DemuxInit( p_demux ) )
126     {
127         free( p_sys );
128         return VLC_EGENERIC;
129     }
130     return VLC_SUCCESS;
131 }
132
133
134 /*****************************************************************************
135  * Demux: read packet and send them to decoders
136  *****************************************************************************/
137 static int Demux( demux_t *p_demux )
138 {
139     demux_sys_t *p_sys = p_demux->p_sys;
140
141     for( ;; )
142     {
143         const uint8_t *p_peek;
144         mtime_t i_length;
145         mtime_t i_time_begin = GetMoviePTS( p_sys );
146         int i_result;
147
148         if( p_demux->b_die ) break;
149
150         /* Check if we have concatenated files */
151         if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
152         {
153             guid_t guid;
154
155             ASF_GetGUID( &guid, p_peek );
156             if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
157             {
158                 msg_Warn( p_demux, "found a new ASF header" );
159                 /* We end this stream */
160                 DemuxEnd( p_demux );
161
162                 /* And we prepare to read the next one */
163                 if( DemuxInit( p_demux ) )
164                 {
165                     msg_Err( p_demux, "failed to load the new header" );
166                     intf_UserFatal( p_demux, VLC_FALSE, _("Could not demux ASF stream"),
167                                     _("VLC failed to load the ASF header.") );
168                     return 0;
169                 }
170                 continue;
171             }
172         }
173
174         /* Read and demux a packet */
175         if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
176         {
177             return i_result;
178         }
179         if( i_time_begin == -1 )
180         {
181             i_time_begin = GetMoviePTS( p_sys );
182         }
183         else
184         {
185             i_length = GetMoviePTS( p_sys ) - i_time_begin;
186             if( i_length < 0 || i_length >= 40 * 1000 ) break;
187         }
188     }
189
190     /* Set the PCR */
191     p_sys->i_time = GetMoviePTS( p_sys );
192     if( p_sys->i_time >= 0 )
193     {
194         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
195     }
196
197     return 1;
198 }
199
200 /*****************************************************************************
201  * Close: frees unused data
202  *****************************************************************************/
203 static void Close( vlc_object_t * p_this )
204 {
205     demux_t     *p_demux = (demux_t *)p_this;
206
207     DemuxEnd( p_demux );
208
209     free( p_demux->p_sys );
210 }
211
212 /*****************************************************************************
213  * SeekIndex: goto to i_date or i_percent
214  *****************************************************************************/
215 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
216 {
217     demux_sys_t *p_sys = p_demux->p_sys;
218     asf_object_index_t *p_index;
219     int64_t i_pos;
220
221     msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
222              (int)(i_date/1000000), f_pos );
223
224     p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
225
226     if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
227
228     i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
229     i_pos = p_index->index_entry[i_pos].i_packet_number *
230         p_sys->p_fp->i_min_data_packet_size;
231
232     return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
233 }
234
235 /*****************************************************************************
236  * Control:
237  *****************************************************************************/
238 static int Control( demux_t *p_demux, int i_query, va_list args )
239 {
240     demux_sys_t *p_sys = p_demux->p_sys;
241     vlc_meta_t  *p_meta;
242     int64_t     i64, *pi64;
243     double      f, *pf;
244     int         i;
245
246     switch( i_query )
247     {
248     case DEMUX_GET_LENGTH:
249         pi64 = (int64_t*)va_arg( args, int64_t * );
250         *pi64 = p_sys->i_length;
251         return VLC_SUCCESS;
252
253     case DEMUX_GET_TIME:
254         pi64 = (int64_t*)va_arg( args, int64_t * );
255         if( p_sys->i_time < 0 ) return VLC_EGENERIC;
256         *pi64 = p_sys->i_time;
257         return VLC_SUCCESS;
258
259     case DEMUX_SET_TIME:
260         p_sys->i_time = -1;
261         for( i = 0; i < 128 ; i++ )
262             if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
263
264         if( p_sys->b_index && p_sys->i_length > 0 )
265         {
266             i64 = (int64_t)va_arg( args, int64_t );
267             return SeekIndex( p_demux, i64, -1 );
268         }
269         else
270         {
271             return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
272                                            p_sys->i_data_end, p_sys->i_bitrate,
273                                            p_sys->p_fp->i_min_data_packet_size,
274                                            i_query, args );
275         }
276
277     case DEMUX_GET_POSITION:
278         if( p_sys->i_time < 0 ) return VLC_EGENERIC;
279         if( p_sys->i_length > 0 )
280         {
281             pf = (double*)va_arg( args, double * );
282             *pf = p_sys->i_time / (double)p_sys->i_length;
283             return VLC_SUCCESS;
284         }
285         return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
286                                        p_sys->i_data_end, p_sys->i_bitrate,
287                                        p_sys->p_fp->i_min_data_packet_size,
288                                        i_query, args );
289
290     case DEMUX_SET_POSITION:
291         p_sys->i_time = -1;
292         for( i = 0; i < 128 ; i++ )
293             if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
294
295         if( p_sys->b_index && p_sys->i_length > 0 )
296         {
297             f = (double)va_arg( args, double );
298             return SeekIndex( p_demux, -1, f );
299         }
300         else
301         {
302             return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
303                                            p_sys->i_data_end, p_sys->i_bitrate,
304                                            p_sys->p_fp->i_min_data_packet_size,
305                                            i_query, args );
306         }
307
308     case DEMUX_GET_META:
309         p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
310         vlc_meta_Merge( p_meta, p_sys->meta );
311         return VLC_SUCCESS;
312
313     default:
314         return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
315                                        p_sys->i_data_end, p_sys->i_bitrate,
316                                        p_sys->p_fp->i_min_data_packet_size,
317                                        i_query, args );
318     }
319 }
320
321 /*****************************************************************************
322  *
323  *****************************************************************************/
324 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
325 {
326     mtime_t i_time = -1;
327     int     i;
328
329     for( i = 0; i < 128 ; i++ )
330     {
331         asf_track_t *tk = p_sys->track[i];
332
333         if( tk && tk->p_es && tk->i_time > 0)
334         {
335             if( i_time < 0 ) i_time = tk->i_time;
336             else i_time = __MIN( i_time, tk->i_time );
337         }
338     }
339
340     return i_time;
341 }
342
343 #define GETVALUE2b( bits, var, def ) \
344     switch( (bits)&0x03 ) \
345     { \
346         case 1: var = p_peek[i_skip]; i_skip++; break; \
347         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
348         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
349         case 0: \
350         default: var = def; break;\
351     }
352
353 static int DemuxPacket( demux_t *p_demux )
354 {
355     demux_sys_t *p_sys = p_demux->p_sys;
356     int         i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
357     const uint8_t *p_peek;
358     int         i_skip;
359
360     int         i_packet_size_left;
361     int         i_packet_flags;
362     int         i_packet_property;
363
364     int         b_packet_multiple_payload;
365     int         i_packet_length;
366     int         i_packet_sequence;
367     int         i_packet_padding_length;
368
369     uint32_t    i_packet_send_time;
370     uint16_t    i_packet_duration;
371     int         i_payload;
372     int         i_payload_count;
373     int         i_payload_length_type;
374
375
376     if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
377     {
378         msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
379         return 0;
380     }
381     i_skip = 0;
382
383     /* *** parse error correction if present *** */
384     if( p_peek[0]&0x80 )
385     {
386         unsigned int i_error_correction_length_type;
387         unsigned int i_error_correction_data_length;
388         unsigned int i_opaque_data_present;
389
390         i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
391         i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
392         i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
393         i_skip += 1; // skip error correction flags
394
395         if( i_error_correction_length_type != 0x00 ||
396             i_opaque_data_present != 0 ||
397             i_error_correction_data_length != 0x02 )
398         {
399             goto loop_error_recovery;
400         }
401
402         i_skip += i_error_correction_data_length;
403     }
404     else
405     {
406         msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
407     }
408
409     /* sanity check */
410     if( i_skip + 2 >= i_data_packet_min )
411     {
412         goto loop_error_recovery;
413     }
414
415     i_packet_flags = p_peek[i_skip]; i_skip++;
416     i_packet_property = p_peek[i_skip]; i_skip++;
417
418     b_packet_multiple_payload = i_packet_flags&0x01;
419
420     /* read some value */
421     GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
422     GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
423     GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
424
425     i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
426     i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
427
428 //        i_packet_size_left = i_packet_length;   // XXX data really read
429     /* FIXME I have to do that for some file, I don't known why */
430     i_packet_size_left = i_data_packet_min;
431
432     if( b_packet_multiple_payload )
433     {
434         i_payload_count = p_peek[i_skip] & 0x3f;
435         i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
436         i_skip++;
437     }
438     else
439     {
440         i_payload_count = 1;
441         i_payload_length_type = 0x02; // unused
442     }
443
444     for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
445     {
446         asf_track_t   *tk;
447
448         int i_packet_keyframe;
449         int i_stream_number;
450         int i_media_object_number;
451         int i_media_object_offset;
452         int i_replicated_data_length;
453         int i_payload_data_length;
454         int i_payload_data_pos;
455         int i_sub_payload_data_length;
456         int i_tmp;
457
458         mtime_t i_pts;
459         mtime_t i_pts_delta;
460
461         if( i_skip >= i_packet_size_left )
462         {
463             /* prevent some segfault with invalid file */
464             break;
465         }
466
467         i_packet_keyframe = p_peek[i_skip] >> 7;
468         i_stream_number = p_peek[i_skip++] & 0x7f;
469
470         GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
471         GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
472         GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
473
474         if( i_replicated_data_length > 1 ) // should be at least 8 bytes
475         {
476             i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
477             i_skip += i_replicated_data_length;
478             i_pts_delta = 0;
479
480             i_media_object_offset = i_tmp;
481
482             if( i_skip >= i_packet_size_left )
483             {
484                 break;
485             }
486         }
487         else if( i_replicated_data_length == 1 )
488         {
489             /* msg_Dbg( p_demux, "found compressed payload" ); */
490
491             i_pts = (mtime_t)i_tmp * 1000;
492             i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
493
494             i_media_object_offset = 0;
495         }
496         else
497         {
498             i_pts = (mtime_t)i_packet_send_time * 1000;
499             i_pts_delta = 0;
500
501             i_media_object_offset = i_tmp;
502         }
503
504         i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
505         if( b_packet_multiple_payload )
506         {
507             GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
508         }
509         else
510         {
511             i_payload_data_length = i_packet_length -
512                                         i_packet_padding_length - i_skip;
513         }
514
515         if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
516         {
517             break;
518         }
519 #if 0
520          msg_Dbg( p_demux,
521                   "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
522                   i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
523                   i_media_object_offset, i_replicated_data_length, i_payload_data_length );
524 #endif
525
526         if( ( tk = p_sys->track[i_stream_number] ) == NULL )
527         {
528             msg_Warn( p_demux,
529                       "undeclared stream[Id 0x%x]", i_stream_number );
530             i_skip += i_payload_data_length;
531             continue;   // over payload
532         }
533
534         if( !tk->p_es )
535         {
536             i_skip += i_payload_data_length;
537             continue;
538         }
539
540
541         for( i_payload_data_pos = 0;
542              i_payload_data_pos < i_payload_data_length &&
543                     i_packet_size_left > 0;
544              i_payload_data_pos += i_sub_payload_data_length )
545         {
546             block_t *p_frag;
547             int i_read;
548
549             // read sub payload length
550             if( i_replicated_data_length == 1 )
551             {
552                 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
553                 i_payload_data_pos++;
554             }
555             else
556             {
557                 i_sub_payload_data_length = i_payload_data_length;
558             }
559
560             /* FIXME I don't use i_media_object_number, sould I ? */
561             if( tk->p_frame && i_media_object_offset == 0 )
562             {
563                 /* send complete packet to decoder */
564                 block_t *p_gather = block_ChainGather( tk->p_frame );
565
566                 es_out_Send( p_demux->out, tk->p_es, p_gather );
567
568                 tk->p_frame = NULL;
569             }
570
571             i_read = i_sub_payload_data_length + i_skip;
572             if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
573             {
574                 msg_Warn( p_demux, "cannot read data" );
575                 return 0;
576             }
577             i_packet_size_left -= i_read;
578
579             p_frag->p_buffer += i_skip;
580             p_frag->i_buffer -= i_skip;
581
582             if( tk->p_frame == NULL )
583             {
584                 tk->i_time =
585                     ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
586
587                 p_frag->i_pts = tk->i_time;
588
589                 if( tk->i_cat != VIDEO_ES )
590                     p_frag->i_dts = p_frag->i_pts;
591                 else
592                 {
593                     p_frag->i_dts = p_frag->i_pts;
594                     p_frag->i_pts = 0;
595                 }
596             }
597
598             block_ChainAppend( &tk->p_frame, p_frag );
599
600             i_skip = 0;
601             if( i_packet_size_left > 0 )
602             {
603                 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
604                                                          < i_packet_size_left )
605                 {
606                     msg_Warn( p_demux, "cannot peek, EOF ?" );
607                     return 0;
608                 }
609             }
610         }
611     }
612
613     if( i_packet_size_left > 0 )
614     {
615         if( stream_Read( p_demux->s, NULL, i_packet_size_left )
616                                                          < i_packet_size_left )
617         {
618             msg_Warn( p_demux, "cannot skip data, EOF ?" );
619             return 0;
620         }
621     }
622
623     return 1;
624
625 loop_error_recovery:
626     msg_Warn( p_demux, "unsupported packet header" );
627     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
628     {
629         msg_Err( p_demux, "unsupported packet header, fatal error" );
630         return -1;
631     }
632     stream_Read( p_demux->s, NULL, i_data_packet_min );
633
634     return 1;
635 }
636
637 /*****************************************************************************
638  *
639  *****************************************************************************/
640 static int DemuxInit( demux_t *p_demux )
641 {
642     demux_sys_t *p_sys = p_demux->p_sys;
643     vlc_bool_t b_seekable;
644     unsigned int i_stream, i;
645     asf_object_content_description_t *p_cd;
646     asf_object_index_t *p_index;
647     vlc_bool_t b_index;
648
649     /* init context */
650     p_sys->i_time   = -1;
651     p_sys->i_length = 0;
652     p_sys->i_bitrate = 0;
653     p_sys->p_root   = NULL;
654     p_sys->p_fp     = NULL;
655     p_sys->b_index  = 0;
656     p_sys->i_track  = 0;
657     for( i = 0; i < 128; i++ )
658     {
659         p_sys->track[i] = NULL;
660     }
661     p_sys->i_data_begin = -1;
662     p_sys->i_data_end   = -1;
663     p_sys->meta         = NULL;
664
665     /* Now load all object ( except raw data ) */
666     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
667     if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
668     {
669         msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
670         return VLC_EGENERIC;
671     }
672     p_sys->p_fp = p_sys->p_root->p_fp;
673
674     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
675     {
676         msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
677         goto error;
678     }
679
680     p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
681                                       &asf_object_stream_properties_guid );
682     if( p_sys->i_track <= 0 )
683     {
684         msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
685         goto error;
686     }
687
688     /* check if index is available */
689     p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
690     b_index = p_index && p_index->i_index_entry_count;
691
692     msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
693
694     for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
695     {
696         asf_track_t    *tk;
697         asf_object_stream_properties_t *p_sp;
698         vlc_bool_t b_access_selected;
699
700         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
701                                &asf_object_stream_properties_guid,
702                                i_stream );
703
704         tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
705         memset( tk, 0, sizeof( asf_track_t ) );
706
707         tk->i_time = -1;
708         tk->p_sp = p_sp;
709         tk->p_es = NULL;
710         tk->p_frame = NULL;
711
712         /* Check (in case of mms) if this track is selected (ie will receive data) */
713         if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
714                              p_sp->i_stream_number, &b_access_selected ) &&
715             !b_access_selected )
716         {
717             tk->i_cat = UNKNOWN_ES;
718             msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
719                      p_sp->i_stream_number );
720             continue;
721         }
722
723         if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
724             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
725         {
726             es_format_t fmt;
727             uint8_t *p_data = p_sp->p_type_specific_data;
728             int i_format;
729
730             es_format_Init( &fmt, AUDIO_ES, 0 );
731             i_format = GetWLE( &p_data[0] );
732             wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
733             fmt.audio.i_channels        = GetWLE(  &p_data[2] );
734             fmt.audio.i_rate      = GetDWLE( &p_data[4] );
735             fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
736             fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
737             fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
738
739             if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
740                 i_format != WAVE_FORMAT_MPEGLAYER3 &&
741                 i_format != WAVE_FORMAT_MPEG )
742             {
743                 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
744                                      p_sp->i_type_specific_data_length -
745                                      sizeof( WAVEFORMATEX ) );
746                 fmt.p_extra = malloc( fmt.i_extra );
747                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
748                         fmt.i_extra );
749             }
750
751             tk->i_cat = AUDIO_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 audio stream(codec:0x%x,ID:%d)",
756                     GetWLE( p_data ), p_sp->i_stream_number );
757         }
758         else if( ASF_CmpGUID( &p_sp->i_stream_type,
759                               &asf_object_stream_type_video ) &&
760                  p_sp->i_type_specific_data_length >= 11 +
761                  sizeof( BITMAPINFOHEADER ) )
762         {
763             es_format_t  fmt;
764             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
765
766             es_format_Init( &fmt, VIDEO_ES,
767                             VLC_FOURCC( p_data[16], p_data[17],
768                                         p_data[18], p_data[19] ) );
769             fmt.video.i_width = GetDWLE( p_data + 4 );
770             fmt.video.i_height= GetDWLE( p_data + 8 );
771
772
773             if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
774             {
775                 /* DVR-MS special ASF */
776                 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
777                 fmt.b_packetized = VLC_FALSE;
778             }
779
780             if( p_sp->i_type_specific_data_length > 11 +
781                 sizeof( BITMAPINFOHEADER ) )
782             {
783                 fmt.i_extra = __MIN( GetDWLE( p_data ),
784                                      p_sp->i_type_specific_data_length - 11 -
785                                      sizeof( BITMAPINFOHEADER ) );
786                 fmt.p_extra = malloc( fmt.i_extra );
787                 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
788                         fmt.i_extra );
789             }
790
791             /* Look for an aspect ratio */
792             if( p_sys->p_root->p_metadata )
793             {
794                 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
795                 int i_aspect_x = 0, i_aspect_y = 0;
796                 unsigned int i;
797
798                 for( i = 0; i < p_meta->i_record_entries_count; i++ )
799                 {
800                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
801                     {
802                         if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
803                             p_meta->record[i].i_stream ==
804                             p_sp->i_stream_number )
805                             i_aspect_x = p_meta->record[i].i_val;
806                     }
807                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
808                     {
809                         if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
810                             p_meta->record[i].i_stream ==
811                             p_sp->i_stream_number )
812                             i_aspect_y = p_meta->record[i].i_val;
813                     }
814                 }
815
816                 if( i_aspect_x && i_aspect_y )
817                 {
818                     fmt.video.i_aspect = i_aspect_x *
819                         (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
820                         fmt.video.i_height / i_aspect_y;
821                 }
822         }
823
824             tk->i_cat = VIDEO_ES;
825             tk->p_es = es_out_Add( p_demux->out, &fmt );
826             es_format_Clean( &fmt );
827
828             /* If there is a video track then use the index for seeking */
829             p_sys->b_index = b_index;
830
831             msg_Dbg( p_demux, "added new video stream(ID:%d)",
832                      p_sp->i_stream_number );
833         }
834         else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
835             p_sp->i_type_specific_data_length >= 64 )
836         {
837             /* Now follows a 64 byte header of which we don't know much */
838             es_format_t fmt;
839             guid_t  *p_ref  = (guid_t *)p_sp->p_type_specific_data;
840             uint8_t *p_data = p_sp->p_type_specific_data + 64;
841             unsigned int i_data = p_sp->i_type_specific_data_length - 64;
842
843             msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
844             if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
845                 i_data >= sizeof( WAVEFORMATEX ) - 2)
846             {
847                 int      i_format;
848                 es_format_Init( &fmt, AUDIO_ES, 0 );
849                 i_format = GetWLE( &p_data[0] );
850                 if( i_format == 0 )
851                     fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
852                 else
853                     wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
854                 fmt.audio.i_channels        = GetWLE(  &p_data[2] );
855                 fmt.audio.i_rate      = GetDWLE( &p_data[4] );
856                 fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
857                 fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
858                 fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
859                 fmt.b_packetized = VLC_TRUE;
860
861                 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
862                     i_format != WAVE_FORMAT_MPEGLAYER3 &&
863                     i_format != WAVE_FORMAT_MPEG )
864                 {
865                     fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
866                                          p_sp->i_type_specific_data_length -
867                                          sizeof( WAVEFORMATEX ) );
868                     fmt.p_extra = malloc( fmt.i_extra );
869                     memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
870                         fmt.i_extra );
871                 }
872
873                 tk->i_cat = AUDIO_ES;
874                 tk->p_es = es_out_Add( p_demux->out, &fmt );
875                 es_format_Clean( &fmt );
876
877                 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
878                     i_format, p_sp->i_stream_number );
879             }
880         }
881         else
882         {
883             tk->i_cat = UNKNOWN_ES;
884             msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
885                      p_sp->i_stream_number );
886         }
887     }
888
889     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
890     if( p_sys->p_root->p_data->i_object_size != 0 )
891     { /* local file */
892         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
893                                     p_sys->p_root->p_data->i_object_size;
894     }
895     else
896     { /* live/broacast */
897         p_sys->i_data_end = -1;
898     }
899
900     /* go to first packet */
901     stream_Seek( p_demux->s, p_sys->i_data_begin );
902
903     /* try to calculate movie time */
904     if( p_sys->p_fp->i_data_packets_count > 0 )
905     {
906         int64_t i_count;
907         int64_t i_size = stream_Size( p_demux->s );
908
909         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
910         {
911             i_size = p_sys->i_data_end;
912         }
913
914         /* real number of packets */
915         i_count = ( i_size - p_sys->i_data_begin ) /
916                   p_sys->p_fp->i_min_data_packet_size;
917
918         /* calculate the time duration in micro-s */
919         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
920                    (mtime_t)i_count /
921                    (mtime_t)p_sys->p_fp->i_data_packets_count;
922
923         if( p_sys->i_length > 0 )
924         {
925             p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
926         }
927     }
928
929     /* Create meta information */
930     p_sys->meta = vlc_meta_New();
931
932     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
933                                  &asf_object_content_description_guid, 0 ) ) )
934     {
935         if( p_cd->psz_title && *p_cd->psz_title )
936         {
937             vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
938         }
939         if( p_cd->psz_artist && *p_cd->psz_artist )
940         {
941              vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
942         }
943         if( p_cd->psz_copyright && *p_cd->psz_copyright )
944         {
945             vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
946         }
947         if( p_cd->psz_description && *p_cd->psz_description )
948         {
949             vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
950         }
951         if( p_cd->psz_rating && *p_cd->psz_rating )
952         {
953             vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
954         }
955     }
956     /// \tood Fix Child meta for ASF tracks
957 #if 0
958     for( i_stream = 0, i = 0; i < 128; i++ )
959     {
960         asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
961                                                         &asf_object_codec_list_guid, 0 );
962
963         if( p_sys->track[i] )
964         {
965             vlc_meta_t *tk = vlc_meta_New();
966             TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
967
968             if( p_cl && i_stream < p_cl->i_codec_entries_count )
969             {
970                 if( p_cl->codec[i_stream].psz_name &&
971                     *p_cl->codec[i_stream].psz_name )
972                 {
973                     vlc_meta_Add( tk, VLC_META_CODEC_NAME,
974                                   p_cl->codec[i_stream].psz_name );
975                 }
976                 if( p_cl->codec[i_stream].psz_description &&
977                     *p_cl->codec[i_stream].psz_description )
978                 {
979                     vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
980                                   p_cl->codec[i_stream].psz_description );
981                 }
982             }
983             i_stream++;
984         }
985     }
986 #endif
987
988     es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
989     return VLC_SUCCESS;
990
991 error:
992     ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
993     return VLC_EGENERIC;
994 }
995 /*****************************************************************************
996  *
997  *****************************************************************************/
998 static void DemuxEnd( demux_t *p_demux )
999 {
1000     demux_sys_t *p_sys = p_demux->p_sys;
1001     int         i;
1002
1003     if( p_sys->p_root )
1004     {
1005         ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1006         p_sys->p_root = NULL;
1007     }
1008     if( p_sys->meta )
1009     {
1010         vlc_meta_Delete( p_sys->meta );
1011         p_sys->meta = NULL;
1012     }
1013
1014     for( i = 0; i < 128; i++ )
1015     {
1016         asf_track_t *tk = p_sys->track[i];
1017
1018         if( tk )
1019         {
1020             if( tk->p_frame )
1021             {
1022                 block_ChainRelease( tk->p_frame );
1023             }
1024             if( tk->p_es )
1025             {
1026                 es_out_Del( p_demux->out, tk->p_es );
1027             }
1028             free( tk );
1029         }
1030         p_sys->track[i] = 0;
1031     }
1032 }
1033