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