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