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