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