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