]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
demux: asf: fix double declaration (cid 1135582)
[vlc] / modules / demux / asf / asf.c
1 /*****************************************************************************
2  * asf.c : ASF demux module
3  *****************************************************************************
4  * Copyright © 2002-2004, 2006-2008, 2010 VLC authors and VideoLAN
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_dialog.h>
35
36 #include <vlc_meta.h>                  /* vlc_meta_Set*, vlc_meta_New */
37 #include <vlc_access.h>                /* GET_PRIVATE_ID_STATE */
38 #include <vlc_codecs.h>                /* VLC_BITMAPINFOHEADER, WAVEFORMATEX */
39
40 #include <limits.h>
41
42 #include "libasf.h"
43
44 /* TODO
45  *  - add support for the newly added object: language, bitrate,
46  *                                            extended stream properties.
47  */
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 static int  Open  ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
54
55 vlc_module_begin ()
56     set_category( CAT_INPUT )
57     set_subcategory( SUBCAT_INPUT_DEMUX )
58     set_description( N_("ASF/WMV demuxer") )
59     set_capability( "demux", 200 )
60     set_callbacks( Open, Close )
61     add_shortcut( "asf", "wmv" )
62 vlc_module_end ()
63
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int Demux  ( demux_t * );
69 static int Control( demux_t *, int i_query, va_list args );
70 static void FlushRemainingPackets( demux_t *p_demux );
71
72 #define MAX_ASF_TRACKS 128
73
74 typedef struct
75 {
76     int i_cat;
77
78     es_out_id_t     *p_es;
79
80     asf_object_stream_properties_t *p_sp;
81     asf_object_extended_stream_properties_t *p_esp;
82
83     mtime_t i_time;
84
85     block_t         *p_frame; /* use to gather complete frame */
86 } asf_track_t;
87
88 struct demux_sys_t
89 {
90     mtime_t             i_time;     /* s */
91     mtime_t             i_length;   /* length of file file */
92     uint64_t            i_bitrate;  /* global file bitrate */
93
94     asf_object_root_t            *p_root;
95     asf_object_file_properties_t *p_fp;
96
97     unsigned int        i_track;
98     asf_track_t         *track[MAX_ASF_TRACKS]; /* track number is stored on 7 bits */
99
100     uint64_t            i_data_begin;
101     uint64_t            i_data_end;
102
103     bool                b_index;
104     bool                b_canfastseek;
105     uint8_t             i_seek_track;
106     unsigned int        i_wait_keyframe;
107
108     vlc_meta_t          *meta;
109 };
110
111 static mtime_t  GetMoviePTS( demux_sys_t * );
112 static int      DemuxInit( demux_t * );
113 static void     DemuxEnd( demux_t * );
114 static int      DemuxPacket( demux_t * );
115
116 /*****************************************************************************
117  * Open: check file and initializes ASF structures
118  *****************************************************************************/
119 static int Open( vlc_object_t * p_this )
120 {
121     demux_t     *p_demux = (demux_t *)p_this;
122     demux_sys_t *p_sys;
123     guid_t      guid;
124     const uint8_t     *p_peek;
125
126     /* A little test to see if it could be a asf stream */
127     if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
128
129     ASF_GetGUID( &guid, p_peek );
130     if( !guidcmp( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
131
132     /* Set p_demux fields */
133     p_demux->pf_demux = Demux;
134     p_demux->pf_control = Control;
135     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
136
137     /* Load the headers */
138     if( DemuxInit( p_demux ) )
139     {
140         free( p_sys );
141         return VLC_EGENERIC;
142     }
143     return VLC_SUCCESS;
144 }
145
146 /*****************************************************************************
147  * Demux: read packet and send them to decoders
148  *****************************************************************************/
149 static int Demux( demux_t *p_demux )
150 {
151     demux_sys_t *p_sys = p_demux->p_sys;
152
153     for( ;; )
154     {
155         const uint8_t *p_peek;
156         mtime_t i_length;
157         mtime_t i_time_begin = GetMoviePTS( p_sys );
158         int i_result;
159
160         if( !vlc_object_alive (p_demux) )
161             break;
162 #if 0
163         /* FIXME: returns EOF too early for some mms streams */
164         if( p_sys->i_data_end >= 0 &&
165                 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
166             return 0; /* EOF */
167 #endif
168
169         /* Check if we have concatenated files */
170         if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
171         {
172             guid_t guid;
173
174             ASF_GetGUID( &guid, p_peek );
175             if( guidcmp( &guid, &asf_object_header_guid ) )
176             {
177                 msg_Warn( p_demux, "found a new ASF header" );
178                 /* We end this stream */
179                 DemuxEnd( p_demux );
180
181                 /* And we prepare to read the next one */
182                 if( DemuxInit( p_demux ) )
183                 {
184                     msg_Err( p_demux, "failed to load the new header" );
185                     dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
186                                     _("VLC failed to load the ASF header.") );
187                     return 0;
188                 }
189                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
190                 continue;
191             }
192         }
193
194         /* Read and demux a packet */
195         if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
196         {
197             FlushRemainingPackets( p_demux );
198             return i_result;
199         }
200         if( i_time_begin == -1 )
201         {
202             i_time_begin = GetMoviePTS( p_sys );
203         }
204         else
205         {
206             i_length = GetMoviePTS( p_sys ) - i_time_begin;
207             if( i_length < 0 || i_length >= 40 * 1000 ) break;
208         }
209     }
210
211     /* Set the PCR */
212     p_sys->i_time = GetMoviePTS( p_sys );
213     if( p_sys->i_time >= 0 )
214     {
215 #ifdef ASF_DEBUG
216         msg_Dbg( p_demux, "Setting PCR to %"PRId64, p_sys->i_time );
217 #endif
218         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time+1 );
219     }
220
221     return 1;
222 }
223
224 /*****************************************************************************
225  * Close: frees unused data
226  *****************************************************************************/
227 static void Close( vlc_object_t * p_this )
228 {
229     demux_t     *p_demux = (demux_t *)p_this;
230
231     DemuxEnd( p_demux );
232
233     free( p_demux->p_sys );
234 }
235
236 /*****************************************************************************
237  * WaitKeyframe: computes the number of frames to wait for a keyframe
238  *****************************************************************************/
239 static void WaitKeyframe( demux_t *p_demux )
240 {
241     demux_sys_t *p_sys = p_demux->p_sys;
242     if ( ! p_sys->i_seek_track )
243     {
244         for ( int i=0; i<MAX_ASF_TRACKS; i++ )
245         {
246             asf_track_t *tk = p_sys->track[i];
247             if ( tk && tk->p_sp && tk->i_cat == VIDEO_ES )
248             {
249                 bool b_selected = false;
250                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
251                                 tk->p_es, &b_selected );
252                 if ( b_selected )
253                 {
254                     p_sys->i_seek_track = tk->p_sp->i_stream_number;
255                     break;
256                 }
257             }
258         }
259     }
260
261     if ( p_sys->i_seek_track )
262     {
263         /* Skip forward at least 1 min */
264         asf_track_t *tk = p_sys->track[p_sys->i_seek_track];
265         if ( tk->p_esp && tk->p_esp->i_average_time_per_frame )
266         {
267             /* 1 min if fastseek, otherwise 5 sec */
268             /* That's a guess for bandwidth */
269             uint64_t i_maxwaittime = ( p_sys->b_canfastseek ) ? 600000000 : 50000000;
270             i_maxwaittime /= tk->p_esp->i_average_time_per_frame;
271             p_sys->i_wait_keyframe = __MIN( i_maxwaittime, UINT_MAX );
272         }
273         else
274         {
275             p_sys->i_wait_keyframe = ( p_sys->b_canfastseek ) ? 25 * 30 : 25 * 5;
276         }
277     }
278     else
279     {
280         p_sys->i_wait_keyframe = 0;
281     }
282
283 }
284
285 /*****************************************************************************
286  * SeekIndex: goto to i_date or i_percent
287  *****************************************************************************/
288 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
289 {
290     demux_sys_t *p_sys = p_demux->p_sys;
291
292     WaitKeyframe( p_demux );
293
294     msg_Dbg( p_demux, "seek with percent: waiting %i frames", p_sys->i_wait_keyframe );
295     return demux_vaControlHelper( p_demux->s, __MIN( INT64_MAX, p_sys->i_data_begin ),
296                                    __MIN( INT64_MAX, p_sys->i_data_end ),
297                                    __MIN( INT64_MAX, p_sys->i_bitrate ),
298                                    __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
299                                    i_query, args );
300 }
301
302 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
303 {
304     demux_sys_t *p_sys = p_demux->p_sys;
305     asf_object_index_t *p_index;
306
307     msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
308              i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
309
310     if( i_date < 0 )
311         i_date = p_sys->i_length * f_pos;
312
313     p_index = ASF_FindObject( p_sys->p_root, &asf_object_simple_index_guid, 0 );
314
315     uint64_t i_entry = i_date * 10 / p_index->i_index_entry_time_interval;
316     if( i_entry >= p_index->i_index_entry_count )
317     {
318         msg_Warn( p_demux, "Incomplete index" );
319         return VLC_EGENERIC;
320     }
321
322     WaitKeyframe( p_demux );
323
324     uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
325                         p_sys->p_fp->i_min_data_packet_size;
326
327     if ( stream_Seek( p_demux->s, i_offset + p_sys->i_data_begin ) == VLC_SUCCESS )
328     {
329         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_date );
330         return VLC_SUCCESS;
331     }
332     else return VLC_EGENERIC;
333 }
334
335 static void SeekPrepare( demux_t *p_demux )
336 {
337     demux_sys_t *p_sys = p_demux->p_sys;
338
339     p_sys->i_time = VLC_TS_INVALID;
340     for( int i = 0; i < MAX_ASF_TRACKS ; i++ )
341     {
342         asf_track_t *tk = p_sys->track[i];
343         if( !tk )
344             continue;
345
346         tk->i_time = VLC_TS_INVALID;
347         if( tk->p_frame )
348             block_ChainRelease( tk->p_frame );
349         tk->p_frame = NULL;
350     }
351
352     es_out_Control( p_demux->out, ES_OUT_RESET_PCR, VLC_TS_INVALID );
353 }
354
355 /*****************************************************************************
356  * Control:
357  *****************************************************************************/
358 static int Control( demux_t *p_demux, int i_query, va_list args )
359 {
360     demux_sys_t *p_sys = p_demux->p_sys;
361     vlc_meta_t  *p_meta;
362     int64_t     i64, *pi64;
363     double      f, *pf;
364
365     switch( i_query )
366     {
367     case DEMUX_GET_LENGTH:
368         pi64 = (int64_t*)va_arg( args, int64_t * );
369         *pi64 = p_sys->i_length;
370         return VLC_SUCCESS;
371
372     case DEMUX_GET_TIME:
373         pi64 = (int64_t*)va_arg( args, int64_t * );
374         if( p_sys->i_time < 0 ) return VLC_EGENERIC;
375         *pi64 = p_sys->i_time;
376         return VLC_SUCCESS;
377
378     case DEMUX_SET_TIME:
379         if ( p_sys->p_fp &&
380              ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
381             return VLC_EGENERIC;
382
383         SeekPrepare( p_demux );
384
385         if( p_sys->b_index && p_sys->i_length > 0 )
386         {
387             va_list acpy;
388             va_copy( acpy, args );
389             i64 = (int64_t)va_arg( acpy, int64_t );
390             va_end( acpy );
391
392             if( !SeekIndex( p_demux, i64, -1 ) )
393                 return VLC_SUCCESS;
394         }
395         return SeekPercent( p_demux, i_query, args );
396
397     case DEMUX_GET_POSITION:
398         if( p_sys->i_time < 0 ) return VLC_EGENERIC;
399         if( p_sys->i_length > 0 )
400         {
401             pf = (double*)va_arg( args, double * );
402             *pf = p_sys->i_time / (double)p_sys->i_length;
403             return VLC_SUCCESS;
404         }
405         return demux_vaControlHelper( p_demux->s,
406                                        __MIN( INT64_MAX, p_sys->i_data_begin ),
407                                        __MIN( INT64_MAX, p_sys->i_data_end ),
408                                        __MIN( INT64_MAX, p_sys->i_bitrate ),
409                                        __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
410                                        i_query, args );
411
412     case DEMUX_SET_POSITION:
413         if ( p_sys->p_fp &&
414              ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
415             return VLC_EGENERIC;
416
417         SeekPrepare( p_demux );
418
419         if( p_sys->b_index && p_sys->i_length > 0 )
420         {
421             va_list acpy;
422             va_copy( acpy, args );
423             f = (double)va_arg( acpy, double );
424             va_end( acpy );
425
426             if( !SeekIndex( p_demux, -1, f ) )
427                 return VLC_SUCCESS;
428         }
429         return SeekPercent( p_demux, i_query, args );
430
431     case DEMUX_GET_META:
432         p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
433         vlc_meta_Merge( p_meta, p_sys->meta );
434         return VLC_SUCCESS;
435
436     case DEMUX_CAN_SEEK:
437         if ( p_sys->p_fp &&
438              ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
439         {
440             bool *pb_bool = (bool*)va_arg( args, bool * );
441             *pb_bool = false;
442             return VLC_SUCCESS;
443         }
444         // ft
445
446     default:
447         return demux_vaControlHelper( p_demux->s,
448                                       __MIN( INT64_MAX, p_sys->i_data_begin ),
449                                       __MIN( INT64_MAX, p_sys->i_data_end),
450                                       __MIN( INT64_MAX, p_sys->i_bitrate ),
451                     ( p_sys->p_fp ) ? __MIN( INT_MAX, p_sys->p_fp->i_min_data_packet_size ) : 1,
452                     i_query, args );
453     }
454 }
455
456 /*****************************************************************************
457  *
458  *****************************************************************************/
459 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
460 {
461     mtime_t i_time = -1;
462     int     i;
463
464     for( i = 0; i < MAX_ASF_TRACKS ; i++ )
465     {
466         asf_track_t *tk = p_sys->track[i];
467
468         if( tk && tk->p_es && tk->i_time > 0)
469         {
470             if( i_time < 0 ) i_time = tk->i_time;
471             else i_time = __MIN( i_time, tk->i_time );
472         }
473     }
474
475     return i_time;
476 }
477
478 static inline int GetValue2b(uint32_t *var, const uint8_t *p, unsigned int *skip, int left, int bits)
479 {
480     switch(bits&0x03)
481     {
482     case 1:
483         if (left < 1)
484             return -1;
485         *var = p[*skip]; *skip += 1;
486         return 0;
487     case 2:
488         if (left < 2)
489             return -1;
490         *var = GetWLE(&p[*skip]); *skip += 2;
491         return 0;
492     case 3:
493         if (left < 4)
494             return -1;
495         *var = GetDWLE(&p[*skip]); *skip += 4;
496         return 0;
497     case 0:
498     default:
499         return 0;
500     }
501 }
502
503 struct asf_packet_t
504 {
505     uint32_t property;
506     uint32_t length;
507     uint32_t padding_length;
508     uint32_t send_time;
509     bool multiple;
510     int length_type;
511
512     /* buffer handling for this ASF packet */
513     uint32_t i_skip;
514     const uint8_t *p_peek;
515     uint32_t left;
516 };
517
518 static void SendPacket(demux_t *p_demux, asf_track_t *tk)
519 {
520     demux_sys_t *p_sys = p_demux->p_sys;
521
522     block_t *p_gather = block_ChainGather( tk->p_frame );
523
524     if( p_sys->i_time < VLC_TS_0 && tk->i_time > VLC_TS_INVALID )
525     {
526         p_sys->i_time = tk->i_time;
527         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
528 #ifdef ASF_DEBUG
529         msg_Dbg( p_demux, "    setting PCR to %"PRId64, p_sys->i_time );
530 #endif
531     }
532
533 #ifdef ASF_DEBUG
534     msg_Dbg( p_demux, "    sending packet dts %"PRId64" pts %"PRId64" pcr %"PRId64, p_gather->i_dts, p_gather->i_pts, p_sys->i_time );
535 #endif
536
537     es_out_Send( p_demux->out, tk->p_es, p_gather );
538     tk->p_frame = NULL;
539 }
540
541 static int DemuxSubPayload(demux_t *p_demux, asf_track_t *tk,
542         uint32_t i_sub_payload_data_length, mtime_t i_pts, mtime_t i_dts,
543         uint32_t i_media_object_offset, bool b_keyframe )
544 {
545     /* FIXME I don't use i_media_object_number, sould I ? */
546     if( tk->p_frame && i_media_object_offset == 0 )
547         SendPacket(p_demux, tk);
548
549     block_t *p_frag = stream_Block( p_demux->s, i_sub_payload_data_length );
550     if( p_frag == NULL ) {
551         msg_Warn( p_demux, "cannot read data" );
552         return -1;
553     }
554
555     p_frag->i_pts = VLC_TS_0 + i_pts;
556     p_frag->i_dts = VLC_TS_0 + i_dts;
557     if ( b_keyframe )
558         p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
559
560     block_ChainAppend( &tk->p_frame, p_frag );
561
562     return 0;
563 }
564
565 static uint32_t SkipBytes( stream_t *s, uint32_t i_bytes )
566 {
567     int i_read;
568     int i_to_read = __MIN(i_bytes, INT_MAX);
569     uint32_t i_bytes_read = 0;
570
571     while( i_bytes )
572     {
573         i_read = stream_Read( s, NULL, i_to_read );
574         i_bytes -= i_read;
575         i_bytes_read += i_read;
576         if ( i_read < i_to_read || i_bytes == 0 )
577         {
578             /* end of stream */
579             return i_bytes_read;
580         }
581         i_to_read = __MIN(i_bytes, INT_MAX);
582     }
583
584     return i_bytes_read;
585 }
586
587 static void ParsePayloadExtensions(demux_t *p_demux, asf_track_t *tk,
588                                    const struct asf_packet_t *pkt,
589                                    uint32_t i_length, bool *b_keyframe )
590 {
591     if ( !tk || !tk->p_esp || !tk->p_esp->p_ext ) return;
592     const uint8_t *p_data = pkt->p_peek + pkt->i_skip + 8;
593     i_length -= 8;
594     uint16_t i_payload_extensions_size;
595     asf_payload_extension_system_t *p_ext = NULL;
596
597     /* Extensions always come in the declared order */
598     for ( int i=0; i< tk->p_esp->i_payload_extension_system_count; i++ )
599     {
600         p_ext = &tk->p_esp->p_ext[i];
601         if ( p_ext->i_data_size == 0xFFFF ) /* Variable length extension data */
602         {
603             if ( i_length < 2 ) return;
604             i_payload_extensions_size = GetWLE( p_data );
605             p_data += 2;
606             i_length -= 2;
607             i_payload_extensions_size = 0;
608         }
609         else
610         {
611             i_payload_extensions_size = p_ext->i_data_size;
612         }
613
614         if ( i_length < i_payload_extensions_size ) return;
615
616         if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_outputcleanpoint_guid ) )
617         {
618             if ( i_payload_extensions_size != sizeof(uint8_t) ) goto sizeerror;
619             *b_keyframe |= *p_data;
620         }
621         else if ( guidcmp( &p_ext->i_extension_id, &asf_dvr_sampleextension_videoframe_guid ) )
622         {
623             if ( i_payload_extensions_size != sizeof(uint32_t) ) goto sizeerror;
624
625             uint32_t i_val = GetDWLE( p_data );
626             /* Valid keyframe must be a split frame start fragment */
627             *b_keyframe = i_val & ASF_EXTENSION_VIDEOFRAME_NEWFRAME;
628             if ( *b_keyframe )
629             {
630                 /* And flagged as IFRAME */
631                 *b_keyframe |= ( ( i_val & ASF_EXTENSION_VIDEOFRAME_TYPE_MASK )
632                                  == ASF_EXTENSION_VIDEOFRAME_IFRAME );
633             }
634         }
635
636         i_length -= i_payload_extensions_size;
637         p_data += i_payload_extensions_size;
638     }
639
640     return;
641
642 sizeerror:
643     msg_Warn( p_demux, "Unknown extension " GUID_FMT " data size of %u",
644               GUID_PRINT( p_ext->i_extension_id ), i_payload_extensions_size );
645 }
646
647 static int DemuxPayload(demux_t *p_demux, struct asf_packet_t *pkt, int i_payload)
648 {
649 #ifndef ASF_DEBUG
650     VLC_UNUSED( i_payload );
651 #endif
652     demux_sys_t *p_sys = p_demux->p_sys;
653
654     if( ! pkt->left || pkt->i_skip >= pkt->left )
655         return -1;
656
657     bool b_packet_keyframe = pkt->p_peek[pkt->i_skip] >> 7;
658     uint8_t i_stream_number = pkt->p_peek[pkt->i_skip++] & 0x7f;
659
660     uint32_t i_media_object_number = 0;
661     if (GetValue2b(&i_media_object_number, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 4) < 0)
662         return -1;
663     uint32_t i_media_object_offset = 0;
664     if (GetValue2b(&i_media_object_offset, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 2) < 0)
665         return -1;
666     uint32_t i_replicated_data_length = 0;
667     if (GetValue2b(&i_replicated_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property) < 0)
668         return -1;
669
670     mtime_t i_base_pts;
671     uint8_t i_pts_delta = 0;
672     uint32_t i_payload_data_length = 0;
673     uint32_t i_temp_payload_length = 0;
674     bool b_preroll_done = false;
675     p_sys->p_fp->i_preroll = __MIN( p_sys->p_fp->i_preroll, INT64_MAX );
676
677     /* Non compressed */
678     if( i_replicated_data_length > 7 ) // should be at least 8 bytes
679     {
680         /* Followed by 2 optional DWORDS, offset in media and presentation time */
681         i_base_pts = (mtime_t)GetDWLE( pkt->p_peek + pkt->i_skip + 4 );
682
683         /* Parsing extensions, See 7.3.1 */
684         ParsePayloadExtensions( p_demux, p_sys->track[i_stream_number], pkt,
685                                 i_replicated_data_length, &b_packet_keyframe );
686
687         b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
688         i_base_pts -= p_sys->p_fp->i_preroll;
689         pkt->i_skip += i_replicated_data_length;
690
691         if( ! pkt->left || pkt->i_skip >= pkt->left )
692             return -1;
693     }
694     else if ( i_replicated_data_length == 0 )
695     {
696         /* optional DWORDS missing */
697         i_base_pts = (mtime_t)pkt->send_time;
698         b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
699     }
700     /* Compressed payload */
701     else if( i_replicated_data_length == 1 )
702     {
703         /* i_media_object_offset is presentation time */
704         /* Next byte is Presentation Time Delta */
705         i_pts_delta = pkt->p_peek[pkt->i_skip];
706         i_base_pts = (mtime_t)i_media_object_offset;
707         b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
708         i_base_pts -= p_sys->p_fp->i_preroll;
709         pkt->i_skip++;
710         i_media_object_offset = 0;
711     }
712     else
713     {
714         /* >1 && <8 Invalid replicated length ! */
715         msg_Warn( p_demux, "Invalid replicated data length detected." );
716         i_payload_data_length = pkt->length - pkt->padding_length - pkt->i_skip;
717         goto skip;
718     }
719
720     if (i_base_pts < 0) i_base_pts = 0; // FIXME?
721     i_base_pts *= 1000;
722
723     if( pkt->multiple ) {
724         if (GetValue2b(&i_temp_payload_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->length_type) < 0)
725             return -1;
726     } else
727         i_temp_payload_length = pkt->length - pkt->padding_length - pkt->i_skip;
728
729     i_payload_data_length = i_temp_payload_length;
730
731 #ifdef ASF_DEBUG
732      msg_Dbg( p_demux,
733               "payload(%d) stream_number:%"PRIu8" media_object_number:%d media_object_offset:%"PRIu32" replicated_data_length:%"PRIu32" payload_data_length %"PRIu32,
734               i_payload + 1, i_stream_number, i_media_object_number,
735               i_media_object_offset, i_replicated_data_length, i_payload_data_length );
736      msg_Dbg( p_demux,
737               "   pts=%"PRId64" st=%"PRIu32, i_base_pts, pkt->send_time );
738 #endif
739
740      if( ! i_payload_data_length || i_payload_data_length > pkt->left )
741      {
742          msg_Dbg( p_demux, "  payload length problem %d %"PRIu32" %"PRIu32, pkt->multiple, i_payload_data_length, pkt->left );
743          return -1;
744      }
745
746     asf_track_t *tk = p_sys->track[i_stream_number];
747     if( tk == NULL )
748     {
749         msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
750         goto skip;
751     }
752
753     if( p_sys->i_wait_keyframe )
754     {
755         if ( i_stream_number == p_sys->i_seek_track )
756         {
757             if ( !b_packet_keyframe )
758             {
759                 p_sys->i_wait_keyframe--;
760                 goto skip;
761             }
762             else
763                 p_sys->i_wait_keyframe = 0;
764         }
765         else
766             goto skip;
767     }
768
769     if( !tk->p_es )
770         goto skip;
771
772     if ( b_preroll_done )
773     {
774         tk->i_time = INT64_C(1000) * pkt->send_time;
775         tk->i_time -= p_sys->p_fp->i_preroll * 1000;
776         tk->i_time -= tk->p_sp->i_time_offset * 10;
777     }
778
779     uint32_t i_subpayload_count = 0;
780     while (i_payload_data_length)
781     {
782         uint32_t i_sub_payload_data_length = i_payload_data_length;
783         if( i_replicated_data_length == 1 )
784         {
785             i_sub_payload_data_length = pkt->p_peek[pkt->i_skip++];
786             i_payload_data_length--;
787         }
788
789         SkipBytes( p_demux->s, pkt->i_skip );
790
791         mtime_t i_payload_pts = i_base_pts + (mtime_t)i_pts_delta * i_subpayload_count * 1000;
792         i_payload_pts -= tk->p_sp->i_time_offset * 10;
793         mtime_t i_payload_dts = INT64_C(1000) * pkt->send_time;
794         i_payload_dts -= tk->p_sp->i_time_offset * 10;
795
796         if ( i_sub_payload_data_length &&
797              DemuxSubPayload(p_demux, tk, i_sub_payload_data_length,
798                         i_payload_pts, i_payload_dts, i_media_object_offset,
799                         b_packet_keyframe ) < 0)
800             return -1;
801
802         if ( pkt->left > pkt->i_skip + i_sub_payload_data_length )
803             pkt->left -= pkt->i_skip + i_sub_payload_data_length;
804         else
805             pkt->left = 0;
806         pkt->i_skip = 0;
807         if( pkt->left > 0 )
808         {
809             int i_return = stream_Peek( p_demux->s, &pkt->p_peek, __MIN(pkt->left, INT_MAX) );
810             if ( i_return <= 0 || (unsigned int) i_return < __MIN(pkt->left, INT_MAX) )
811             {
812             msg_Warn( p_demux, "cannot peek, EOF ?" );
813             return -1;
814             }
815         }
816
817         if ( i_sub_payload_data_length <= i_payload_data_length )
818             i_payload_data_length -= i_sub_payload_data_length;
819         else
820             i_payload_data_length = 0;
821
822         i_subpayload_count++;
823     }
824
825     return 0;
826
827 skip:
828     pkt->i_skip += i_payload_data_length;
829     return 0;
830 }
831
832 static int DemuxPacket( demux_t *p_demux )
833 {
834     demux_sys_t *p_sys = p_demux->p_sys;
835
836     uint32_t i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
837
838     const uint8_t *p_peek;
839     int i_return = stream_Peek( p_demux->s, &p_peek,i_data_packet_min );
840     if( i_return <= 0 || ((unsigned int) i_return) < i_data_packet_min )
841     {
842         msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
843         return 0;
844     }
845     unsigned int i_skip = 0;
846
847     /* *** parse error correction if present *** */
848     if( p_peek[0]&0x80 )
849     {
850         unsigned int i_error_correction_data_length = p_peek[0] & 0x0f;
851         unsigned int i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;
852         unsigned int i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03;
853         i_skip += 1; // skip error correction flags
854
855         if( i_error_correction_length_type != 0x00 ||
856             i_opaque_data_present != 0 ||
857             i_error_correction_data_length != 0x02 )
858         {
859             goto loop_error_recovery;
860         }
861
862         i_skip += i_error_correction_data_length;
863     }
864     else
865         msg_Warn( p_demux, "no error correction" );
866
867     /* sanity check */
868     if( i_skip + 2 >= i_data_packet_min )
869         goto loop_error_recovery;
870
871     struct asf_packet_t pkt;
872     int i_packet_flags = p_peek[i_skip]; i_skip++;
873     pkt.property = p_peek[i_skip]; i_skip++;
874     pkt.multiple = !!(i_packet_flags&0x01);
875
876     pkt.length = i_data_packet_min;
877     pkt.padding_length = 0;
878
879     if (GetValue2b(&pkt.length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 5) < 0)
880         goto loop_error_recovery;
881     uint32_t i_packet_sequence;
882     if (GetValue2b(&i_packet_sequence, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 1) < 0)
883         goto loop_error_recovery;
884     if (GetValue2b(&pkt.padding_length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 3) < 0)
885         goto loop_error_recovery;
886
887     if( pkt.padding_length > pkt.length )
888     {
889         msg_Warn( p_demux, "Too large padding: %"PRIu32, pkt.padding_length );
890         goto loop_error_recovery;
891     }
892
893     if( pkt.length < i_data_packet_min )
894     {
895         /* if packet length too short, there is extra padding */
896         pkt.padding_length += i_data_packet_min - pkt.length;
897         pkt.length = i_data_packet_min;
898     }
899
900     pkt.send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
901     /* uint16_t i_packet_duration = GetWLE( p_peek + i_skip ); */ i_skip += 2;
902
903     i_return = stream_Peek( p_demux->s, &p_peek, pkt.length );
904     if( i_return <= 0 || pkt.length == 0 || (unsigned int)i_return < pkt.length )
905     {
906         msg_Warn( p_demux, "cannot peek, EOF ?" );
907         return 0;
908     }
909
910     int i_payload_count = 1;
911     pkt.length_type = 0x02; //unused
912     if( pkt.multiple )
913     {
914         i_payload_count = p_peek[i_skip] & 0x3f;
915         pkt.length_type = ( p_peek[i_skip] >> 6 )&0x03;
916         i_skip++;
917     }
918
919 #ifdef ASF_DEBUG
920     msg_Dbg(p_demux, "%d payloads", i_payload_count);
921 #endif
922
923     pkt.i_skip = i_skip;
924     pkt.p_peek = p_peek;
925     pkt.left = pkt.length;
926
927     for( int i_payload = 0; i_payload < i_payload_count ; i_payload++ )
928         if (DemuxPayload(p_demux, &pkt, i_payload) < 0)
929         {
930             msg_Warn( p_demux, "payload err %d / %d", i_payload + 1, i_payload_count );
931             return 0;
932         }
933
934     if( pkt.left > 0 )
935     {
936 #ifdef ASF_DEBUG
937         if( pkt.left > pkt.padding_length )
938             msg_Warn( p_demux, "Didn't read %"PRIu32" bytes in the packet",
939                             pkt.left - pkt.padding_length );
940         else if( pkt.left < pkt.padding_length )
941             msg_Warn( p_demux, "Read %"PRIu32" too much bytes in the packet",
942                             pkt.padding_length - pkt.left );
943 #endif
944         int i_return = stream_Read( p_demux->s, NULL, pkt.left );
945         if( i_return < 0 || (unsigned int) i_return < pkt.left )
946         {
947             msg_Err( p_demux, "cannot skip data, EOF ?" );
948             return 0;
949         }
950     }
951
952     return 1;
953
954 loop_error_recovery:
955     msg_Warn( p_demux, "unsupported packet header" );
956     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
957     {
958         msg_Err( p_demux, "unsupported packet header, fatal error" );
959         return -1;
960     }
961     i_return = stream_Read( p_demux->s, NULL, i_data_packet_min );
962     if( i_return <= 0 || (unsigned int) i_return != i_data_packet_min )
963     {
964         msg_Warn( p_demux, "cannot skip data, EOF ?" );
965         return 0;
966     }
967
968     return 1;
969 }
970
971 /*****************************************************************************
972  *
973  *****************************************************************************/
974 typedef struct asf_es_priorities_t
975 {
976     uint16_t *pi_stream_numbers;
977     uint16_t i_count;
978 } asf_es_priorities_t;
979
980 /* Fills up our exclusion list */
981 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
982                                          asf_es_priorities_t *p_prios )
983 {
984     /* Find stream exclusions */
985     asf_object_advanced_mutual_exclusion_t *p_mutex =
986             ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
987     if (! p_mutex ) return;
988
989     p_prios->pi_stream_numbers = malloc( p_sys->i_track * sizeof( uint16_t ) );
990     if ( !p_prios->pi_stream_numbers ) return;
991
992     if ( p_mutex->i_stream_number_count )
993     {
994         /* Just set highest prio on highest in the group */
995         for ( uint16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
996         {
997             if ( p_prios->i_count > p_sys->i_track ) break;
998             p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
999         }
1000     }
1001 }
1002
1003 /* Fills up our bitrate exclusion list */
1004 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1005                                                  asf_es_priorities_t *p_prios )
1006 {
1007     /* Find bitrate exclusions */
1008     asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
1009             ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
1010     if (! p_bitrate_mutex ) return;
1011
1012     p_prios->pi_stream_numbers = malloc( p_sys->i_track * sizeof( uint16_t ) );
1013     if ( !p_prios->pi_stream_numbers ) return;
1014
1015     if ( p_bitrate_mutex->i_stream_number_count )
1016     {
1017         /* Just remove < highest */
1018         for ( uint16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
1019         {
1020             if ( p_prios->i_count > p_sys->i_track ) break;
1021             p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
1022         }
1023     }
1024
1025 }
1026
1027 #define GET_CHECKED( target, getter, maxtarget, temp ) \
1028 {\
1029     temp i_temp = getter;\
1030     if ( i_temp > maxtarget ) {\
1031         msg_Warn( p_demux, "rejecting stream %u : " #target " overflow", i_stream );\
1032         es_format_Clean( &fmt );\
1033         goto error;\
1034     } else {\
1035         target = i_temp;\
1036     }\
1037 }
1038
1039 static int DemuxInit( demux_t *p_demux )
1040 {
1041     demux_sys_t *p_sys = p_demux->p_sys;
1042
1043     /* init context */
1044     p_sys->i_time   = -1;
1045     p_sys->i_length = 0;
1046     p_sys->i_bitrate = 0;
1047     p_sys->p_root   = NULL;
1048     p_sys->p_fp     = NULL;
1049     p_sys->b_index  = 0;
1050     p_sys->i_track  = 0;
1051     p_sys->i_seek_track = 0;
1052     p_sys->i_wait_keyframe = 0;
1053     for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1054     {
1055         p_sys->track[i] = NULL;
1056     }
1057     p_sys->i_data_begin = 0;
1058     p_sys->i_data_end   = 0;
1059     p_sys->meta         = NULL;
1060
1061     /* Now load all object ( except raw data ) */
1062     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1063     if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, p_sys->b_canfastseek)) )
1064     {
1065         msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
1066         return VLC_EGENERIC;
1067     }
1068     p_sys->p_fp = p_sys->p_root->p_fp;
1069
1070     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1071     {
1072         msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
1073         goto error;
1074     }
1075
1076     if ( ASF_FindObject( p_sys->p_root->p_hdr,
1077                          &asf_object_content_encryption_guid, 0 ) != NULL
1078          || ASF_FindObject( p_sys->p_root->p_hdr,
1079                             &asf_object_extended_content_encryption_guid, 0 ) != NULL
1080          || ASF_FindObject( p_sys->p_root->p_hdr,
1081                          &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
1082     {
1083         msg_Warn( p_demux, "ASF plugin discarded (DRM encumbered content)" );
1084         goto error;
1085     }
1086
1087     p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
1088                                       &asf_object_stream_properties_guid );
1089     if( p_sys->i_track == 0 )
1090     {
1091         msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
1092         goto error;
1093     }
1094     msg_Dbg( p_demux, "found %u streams", p_sys->i_track );
1095
1096     /* check if index is available */
1097     asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
1098                                                   &asf_object_simple_index_guid, 0 );
1099     const bool b_index = p_index && p_index->i_index_entry_count;
1100
1101     /* Find the extended header if any */
1102     asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
1103                                               &asf_object_header_extension_guid, 0 );
1104
1105     asf_object_language_list_t *p_languages = NULL;
1106     asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
1107     asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
1108
1109     if( p_hdr_ext )
1110     {
1111         p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
1112
1113         ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
1114         ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
1115     }
1116
1117     for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1118     {
1119         asf_track_t    *tk;
1120         asf_object_stream_properties_t *p_sp;
1121         asf_object_extended_stream_properties_t *p_esp;
1122         bool b_access_selected;
1123
1124         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
1125                                &asf_object_stream_properties_guid,
1126                                i_stream );
1127         p_esp = NULL;
1128
1129         tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
1130         memset( tk, 0, sizeof( asf_track_t ) );
1131
1132         tk->i_time = -1;
1133         tk->p_sp = p_sp;
1134         tk->p_es = NULL;
1135         tk->p_esp = NULL;
1136         tk->p_frame = NULL;
1137
1138         /* Check (in case of mms) if this track is selected (ie will receive data) */
1139         if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1140                              (int) p_sp->i_stream_number, &b_access_selected ) &&
1141             !b_access_selected )
1142         {
1143             tk->i_cat = UNKNOWN_ES;
1144             msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
1145                      p_sp->i_stream_number );
1146             continue;
1147         }
1148
1149         /* Find the associated extended_stream_properties if any */
1150         if( p_hdr_ext )
1151         {
1152             int i_ext_stream = ASF_CountObject( p_hdr_ext,
1153                                                 &asf_object_extended_stream_properties_guid );
1154             for( int i = 0; i < i_ext_stream; i++ )
1155             {
1156                 asf_object_t *p_tmp =
1157                     ASF_FindObject( p_hdr_ext,
1158                                     &asf_object_extended_stream_properties_guid, i );
1159                 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
1160                 {
1161                     p_esp = &p_tmp->ext_stream;
1162                     tk->p_esp = p_esp;
1163                     break;
1164                 }
1165             }
1166         }
1167
1168         es_format_t fmt;
1169
1170         if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
1171             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
1172         {
1173             uint8_t *p_data = p_sp->p_type_specific_data;
1174             int i_format;
1175
1176             es_format_Init( &fmt, AUDIO_ES, 0 );
1177             i_format = GetWLE( &p_data[0] );
1178             wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1179
1180             GET_CHECKED( fmt.audio.i_channels,      GetWLE( &p_data[2] ),
1181                                                         255, uint16_t );
1182             GET_CHECKED( fmt.audio.i_rate,          GetDWLE( &p_data[4] ),
1183                                                         UINT_MAX, uint32_t );
1184             GET_CHECKED( fmt.i_bitrate,             GetDWLE( &p_data[8] ) * 8,
1185                                                         UINT_MAX, uint32_t );
1186             fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
1187             fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
1188
1189             if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1190                 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1191                 i_format != WAVE_FORMAT_MPEG )
1192             {
1193                 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1194                                      p_sp->i_type_specific_data_length -
1195                                      sizeof( WAVEFORMATEX ) ),
1196                              INT_MAX, uint32_t );
1197                 fmt.p_extra = malloc( fmt.i_extra );
1198                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1199                         fmt.i_extra );
1200             }
1201
1202             msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
1203                     GetWLE( p_data ), p_sp->i_stream_number );
1204         }
1205         else if( guidcmp( &p_sp->i_stream_type,
1206                               &asf_object_stream_type_video ) &&
1207                  p_sp->i_type_specific_data_length >= 11 +
1208                  sizeof( VLC_BITMAPINFOHEADER ) )
1209         {
1210             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
1211
1212             es_format_Init( &fmt, VIDEO_ES,
1213                             VLC_FOURCC( p_data[16], p_data[17],
1214                                         p_data[18], p_data[19] ) );
1215
1216             GET_CHECKED( fmt.video.i_width,      GetDWLE( p_data + 4 ),
1217                                                      UINT_MAX, uint32_t );
1218             GET_CHECKED( fmt.video.i_height,     GetDWLE( p_data + 8 ),
1219                                                      UINT_MAX, uint32_t );
1220
1221             if( p_esp && p_esp->i_average_time_per_frame > 0 )
1222             {
1223                 fmt.video.i_frame_rate = 10000000;
1224                 GET_CHECKED( fmt.video.i_frame_rate_base,
1225                              p_esp->i_average_time_per_frame,
1226                              UINT_MAX, uint64_t );
1227             }
1228
1229             if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
1230             {
1231                 /* DVR-MS special ASF */
1232                 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
1233                 fmt.b_packetized = false;
1234             }
1235
1236             if( p_sp->i_type_specific_data_length > 11 +
1237                 sizeof( VLC_BITMAPINFOHEADER ) )
1238             {
1239                 GET_CHECKED( fmt.i_extra, __MIN( GetDWLE( p_data ),
1240                                      p_sp->i_type_specific_data_length - 11 -
1241                                      sizeof( VLC_BITMAPINFOHEADER ) ),
1242                              UINT_MAX, uint32_t );
1243                 fmt.p_extra = malloc( fmt.i_extra );
1244                 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
1245                         fmt.i_extra );
1246             }
1247
1248             /* Look for an aspect ratio */
1249             if( p_sys->p_root->p_metadata )
1250             {
1251                 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
1252                 unsigned int i_aspect_x = 0, i_aspect_y = 0;
1253                 uint32_t i;
1254                 for( i = 0; i < p_meta->i_record_entries_count; i++ )
1255                 {
1256                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
1257                     {
1258                         if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
1259                             p_meta->record[i].i_stream ==
1260                             p_sp->i_stream_number )
1261                             GET_CHECKED( i_aspect_x, p_meta->record[i].i_val,
1262                                          UINT_MAX, uint64_t );
1263                     }
1264                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
1265                     {
1266                         if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
1267                             p_meta->record[i].i_stream ==
1268                             p_sp->i_stream_number )
1269                             GET_CHECKED( i_aspect_y, p_meta->record[i].i_val,
1270                                          UINT_MAX, uint64_t );
1271                     }
1272                 }
1273
1274                 if( i_aspect_x && i_aspect_y )
1275                 {
1276                     fmt.video.i_sar_num = i_aspect_x;
1277                     fmt.video.i_sar_den = i_aspect_y;
1278                 }
1279             }
1280
1281             /* If there is a video track then use the index for seeking */
1282             p_sys->b_index = b_index;
1283
1284             msg_Dbg( p_demux, "added new video stream(ID:%d)",
1285                      p_sp->i_stream_number );
1286         }
1287         else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
1288             p_sp->i_type_specific_data_length >= 64 )
1289         {
1290             /* Now follows a 64 byte header of which we don't know much */
1291             guid_t  *p_ref  = (guid_t *)p_sp->p_type_specific_data;
1292             uint8_t *p_data = p_sp->p_type_specific_data + 64;
1293             unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1294
1295             msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1296             if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1297                 i_data >= sizeof( WAVEFORMATEX ) - 2)
1298             {
1299                 uint16_t i_format;
1300                 es_format_Init( &fmt, AUDIO_ES, 0 );
1301                 i_format = GetWLE( &p_data[0] );
1302                 if( i_format == 0 )
1303                     fmt.i_codec = VLC_CODEC_A52;
1304                 else
1305                     wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1306                 GET_CHECKED( fmt.audio.i_channels,      GetWLE( &p_data[2] ),
1307                                                             255, uint16_t );
1308                 GET_CHECKED( fmt.audio.i_rate,          GetDWLE( &p_data[4] ),
1309                                                             UINT_MAX, uint32_t );
1310                 GET_CHECKED( fmt.i_bitrate,             GetDWLE( &p_data[8] ) * 8,
1311                                                             UINT_MAX, uint32_t );
1312                 fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
1313                 fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
1314                 fmt.b_packetized = true;
1315
1316                 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1317                     i_format != WAVE_FORMAT_MPEGLAYER3 &&
1318                     i_format != WAVE_FORMAT_MPEG )
1319                 {
1320                     GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1321                                          p_sp->i_type_specific_data_length -
1322                                          sizeof( WAVEFORMATEX ) ),
1323                                  INT_MAX, uint32_t );
1324                     fmt.p_extra = malloc( fmt.i_extra );
1325                     memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1326                         fmt.i_extra );
1327                 }
1328
1329                 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1330                     i_format, p_sp->i_stream_number );
1331             }
1332             else
1333             {
1334                 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1335             }
1336         }
1337         else
1338         {
1339             es_format_Init( &fmt, UNKNOWN_ES, 0 );
1340         }
1341
1342         tk->i_cat = fmt.i_cat;
1343         if( fmt.i_cat != UNKNOWN_ES )
1344         {
1345             if( p_esp && p_languages &&
1346                 p_esp->i_language_index < p_languages->i_language )
1347             {
1348                 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1349                 char *p;
1350                 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1351                     *p = '\0';
1352             }
1353
1354             /* Set our priority so we won't get multiple videos */
1355             int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1356             for( uint16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1357             {
1358                 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1359                 {
1360                     i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1361                     break;
1362                 }
1363             }
1364             for( uint16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1365             {
1366                 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1367                 {
1368                     i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1369                     break;
1370                 }
1371             }
1372             fmt.i_priority = i_priority;
1373
1374             tk->p_es = es_out_Add( p_demux->out, &fmt );
1375         }
1376         else
1377         {
1378             msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1379                      p_sp->i_stream_number );
1380         }
1381
1382         es_format_Clean( &fmt );
1383     }
1384
1385     free( fmt_priorities_ex.pi_stream_numbers );
1386     free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1387
1388     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1389     if( p_sys->p_root->p_data->i_object_size != 0 )
1390     { /* local file */
1391         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1392                                     p_sys->p_root->p_data->i_object_size;
1393         p_sys->i_data_end = __MIN( (uint64_t)stream_Size( p_demux->s ), p_sys->i_data_end );
1394     }
1395     else
1396     { /* live/broacast */
1397         p_sys->i_data_end = 0;
1398     }
1399
1400     /* go to first packet */
1401     stream_Seek( p_demux->s, p_sys->i_data_begin );
1402
1403     /* try to calculate movie time */
1404     if( p_sys->p_fp->i_data_packets_count > 0 )
1405     {
1406         uint64_t i_count;
1407         uint64_t i_size = stream_Size( p_demux->s );
1408
1409         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1410         {
1411             i_size = p_sys->i_data_end;
1412         }
1413
1414         /* real number of packets */
1415         i_count = ( i_size - p_sys->i_data_begin ) /
1416                   p_sys->p_fp->i_min_data_packet_size;
1417
1418         /* calculate the time duration in micro-s */
1419         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1420                    (mtime_t)i_count /
1421                    (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1422         if( p_sys->i_length < 0 )
1423             p_sys->i_length = 0;
1424
1425         if( p_sys->i_length > 0 )
1426         {
1427             p_sys->i_bitrate = 8 * i_size * 1000000 / p_sys->i_length;
1428         }
1429     }
1430
1431     /* Create meta information */
1432     p_sys->meta = vlc_meta_New();
1433
1434     asf_object_content_description_t *p_cd;
1435     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1436                                  &asf_object_content_description_guid, 0 ) ) )
1437     {
1438         if( p_cd->psz_title && *p_cd->psz_title )
1439         {
1440             vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1441         }
1442         if( p_cd->psz_artist && *p_cd->psz_artist )
1443         {
1444              vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1445         }
1446         if( p_cd->psz_copyright && *p_cd->psz_copyright )
1447         {
1448             vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1449         }
1450         if( p_cd->psz_description && *p_cd->psz_description )
1451         {
1452             vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1453         }
1454         if( p_cd->psz_rating && *p_cd->psz_rating )
1455         {
1456             vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1457         }
1458     }
1459     /// \tood Fix Child meta for ASF tracks
1460 #if 0
1461     for( i_stream = 0, i = 0; i < MAX_ASF_TRACKS; i++ )
1462     {
1463         asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1464                                                         &asf_object_codec_list_guid, 0 );
1465
1466         if( p_sys->track[i] )
1467         {
1468             vlc_meta_t *tk = vlc_meta_New();
1469             TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1470
1471             if( p_cl && i_stream < p_cl->i_codec_entries_count )
1472             {
1473                 if( p_cl->codec[i_stream].psz_name &&
1474                     *p_cl->codec[i_stream].psz_name )
1475                 {
1476                     vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1477                                   p_cl->codec[i_stream].psz_name );
1478                 }
1479                 if( p_cl->codec[i_stream].psz_description &&
1480                     *p_cl->codec[i_stream].psz_description )
1481                 {
1482                     vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1483                                   p_cl->codec[i_stream].psz_description );
1484                 }
1485             }
1486             i_stream++;
1487         }
1488     }
1489 #endif
1490     return VLC_SUCCESS;
1491
1492 error:
1493     ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1494     return VLC_EGENERIC;
1495 }
1496
1497 /*****************************************************************************
1498  * FlushRemainingPackets: flushes tail packets
1499  *****************************************************************************/
1500
1501 static void FlushRemainingPackets( demux_t *p_demux )
1502 {
1503     demux_sys_t *p_sys = p_demux->p_sys;
1504     for ( unsigned int i = 0; i < MAX_ASF_TRACKS; i++ )
1505     {
1506         asf_track_t *tk = p_sys->track[i];
1507         if( tk && tk->p_frame )
1508             SendPacket( p_demux, tk );
1509     }
1510 }
1511
1512 /*****************************************************************************
1513  *
1514  *****************************************************************************/
1515 static void DemuxEnd( demux_t *p_demux )
1516 {
1517     demux_sys_t *p_sys = p_demux->p_sys;
1518
1519     if( p_sys->p_root )
1520     {
1521         ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1522         p_sys->p_root = NULL;
1523     }
1524     if( p_sys->meta )
1525     {
1526         vlc_meta_Delete( p_sys->meta );
1527         p_sys->meta = NULL;
1528     }
1529
1530     for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1531     {
1532         asf_track_t *tk = p_sys->track[i];
1533
1534         if( tk )
1535         {
1536             if( tk->p_frame )
1537             {
1538                 msg_Warn( p_demux, "Still one frame for track %u, something is wrong", i );
1539                 block_ChainRelease( tk->p_frame );
1540             }
1541             if( tk->p_es )
1542             {
1543                 es_out_Del( p_demux->out, tk->p_es );
1544             }
1545             free( tk );
1546         }
1547         p_sys->track[i] = 0;
1548     }
1549 }
1550