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