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