demux: asf: fix mmsh es id offset and PCR handling (fix #11470)
[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 #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;
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->i_time > 0 && tk->b_selected )
518         {
519             if( i_time < 0 ) i_time = tk->i_time;
520             else i_time = __MIN( i_time, tk->i_time );
521         }
522     }
523
524     return i_time;
525 }
526
527 static inline int GetValue2b(uint32_t *var, const uint8_t *p, unsigned int *skip, int left, int bits)
528 {
529     switch(bits&0x03)
530     {
531     case 1:
532         if (left < 1)
533             return -1;
534         *var = p[*skip]; *skip += 1;
535         return 0;
536     case 2:
537         if (left < 2)
538             return -1;
539         *var = GetWLE(&p[*skip]); *skip += 2;
540         return 0;
541     case 3:
542         if (left < 4)
543             return -1;
544         *var = GetDWLE(&p[*skip]); *skip += 4;
545         return 0;
546     case 0:
547     default:
548         return 0;
549     }
550 }
551
552 struct asf_packet_t
553 {
554     uint32_t property;
555     uint32_t length;
556     uint32_t padding_length;
557     uint32_t send_time;
558     bool multiple;
559     int length_type;
560
561     /* buffer handling for this ASF packet */
562     uint32_t i_skip;
563     const uint8_t *p_peek;
564     uint32_t left;
565 };
566
567 static void SendPacket(demux_t *p_demux, asf_track_t *tk)
568 {
569     demux_sys_t *p_sys = p_demux->p_sys;
570
571     block_t *p_gather = block_ChainGather( tk->p_frame );
572
573     if( p_sys->i_time < VLC_TS_0 && tk->i_time > VLC_TS_INVALID )
574     {
575         p_sys->i_time = tk->i_time;
576         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
577 #ifdef ASF_DEBUG
578         msg_Dbg( p_demux, "    setting PCR to %"PRId64, p_sys->i_time );
579 #endif
580     }
581
582 #ifdef ASF_DEBUG
583     msg_Dbg( p_demux, "    sending packet dts %"PRId64" pts %"PRId64" pcr %"PRId64, p_gather->i_dts, p_gather->i_pts, p_sys->i_time );
584 #endif
585
586     es_out_Send( p_demux->out, tk->p_es, p_gather );
587     tk->p_frame = NULL;
588 }
589
590 static int DemuxSubPayload(demux_t *p_demux, asf_track_t *tk,
591         uint32_t i_sub_payload_data_length, mtime_t i_pts, mtime_t i_dts,
592         uint32_t i_media_object_offset, bool b_keyframe )
593 {
594     /* FIXME I don't use i_media_object_number, sould I ? */
595     if( tk->p_frame && i_media_object_offset == 0 )
596         SendPacket(p_demux, tk);
597
598     block_t *p_frag = stream_Block( p_demux->s, i_sub_payload_data_length );
599     if( p_frag == NULL ) {
600         msg_Warn( p_demux, "cannot read data" );
601         return -1;
602     }
603
604     p_frag->i_pts = VLC_TS_0 + i_pts;
605     p_frag->i_dts = VLC_TS_0 + i_dts;
606     if ( b_keyframe )
607         p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
608
609     block_ChainAppend( &tk->p_frame, p_frag );
610
611     return 0;
612 }
613
614 static uint32_t SkipBytes( stream_t *s, uint32_t i_bytes )
615 {
616     int i_read;
617     int i_to_read = __MIN(i_bytes, INT_MAX);
618     uint32_t i_bytes_read = 0;
619
620     while( i_bytes )
621     {
622         i_read = stream_Read( s, NULL, i_to_read );
623         i_bytes -= i_read;
624         i_bytes_read += i_read;
625         if ( i_read < i_to_read || i_bytes == 0 )
626         {
627             /* end of stream */
628             return i_bytes_read;
629         }
630         i_to_read = __MIN(i_bytes, INT_MAX);
631     }
632
633     return i_bytes_read;
634 }
635
636 static void ParsePayloadExtensions(demux_t *p_demux, asf_track_t *tk,
637                                    const struct asf_packet_t *pkt,
638                                    uint32_t i_length, bool *b_keyframe )
639 {
640     if ( !tk || !tk->p_esp || !tk->p_esp->p_ext ) return;
641     const uint8_t *p_data = pkt->p_peek + pkt->i_skip + 8;
642     i_length -= 8;
643     uint16_t i_payload_extensions_size;
644     asf_payload_extension_system_t *p_ext = NULL;
645
646     /* Extensions always come in the declared order */
647     for ( int i=0; i< tk->p_esp->i_payload_extension_system_count; i++ )
648     {
649         p_ext = &tk->p_esp->p_ext[i];
650         if ( p_ext->i_data_size == 0xFFFF ) /* Variable length extension data */
651         {
652             if ( i_length < 2 ) return;
653             i_payload_extensions_size = GetWLE( p_data );
654             p_data += 2;
655             i_length -= 2;
656             i_payload_extensions_size = 0;
657         }
658         else
659         {
660             i_payload_extensions_size = p_ext->i_data_size;
661         }
662
663         if ( i_length < i_payload_extensions_size ) return;
664
665         if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_outputcleanpoint_guid ) )
666         {
667             if ( i_payload_extensions_size != sizeof(uint8_t) ) goto sizeerror;
668             *b_keyframe |= *p_data;
669         }
670         else if ( guidcmp( &p_ext->i_extension_id, &asf_dvr_sampleextension_videoframe_guid ) )
671         {
672             if ( i_payload_extensions_size != sizeof(uint32_t) ) goto sizeerror;
673
674             uint32_t i_val = GetDWLE( p_data );
675             /* Valid keyframe must be a split frame start fragment */
676             *b_keyframe = i_val & ASF_EXTENSION_VIDEOFRAME_NEWFRAME;
677             if ( *b_keyframe )
678             {
679                 /* And flagged as IFRAME */
680                 *b_keyframe |= ( ( i_val & ASF_EXTENSION_VIDEOFRAME_TYPE_MASK )
681                                  == ASF_EXTENSION_VIDEOFRAME_IFRAME );
682             }
683         }
684         else if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_pixelaspectratio_guid ) )
685         {
686             if ( i_payload_extensions_size != sizeof(uint16_t) ) goto sizeerror;
687
688             uint8_t i_ratio_x = *p_data;
689             uint8_t i_ratio_y = *(p_data + 1);
690             if ( tk->p_fmt->video.i_sar_num != i_ratio_x || tk->p_fmt->video.i_sar_den != i_ratio_y )
691             {
692                 /* Only apply if origin pixel size >= 1x1, due to broken yacast */
693                 if ( tk->p_fmt->video.i_height * i_ratio_x > tk->p_fmt->video.i_width * i_ratio_y )
694                 {
695                     vout_thread_t *p_vout = input_GetVout( p_demux->p_input );
696                     if ( p_vout )
697                     {
698                         msg_Info( p_demux, "Changing aspect ratio to %i/%i", i_ratio_x, i_ratio_y );
699                         vout_ChangeAspectRatio( p_vout, i_ratio_x, i_ratio_y );
700                         vlc_object_release( p_vout );
701                     }
702                 }
703                 tk->p_fmt->video.i_sar_num = i_ratio_x;
704                 tk->p_fmt->video.i_sar_den = i_ratio_y;
705             }
706         }
707         i_length -= i_payload_extensions_size;
708         p_data += i_payload_extensions_size;
709     }
710
711     return;
712
713 sizeerror:
714     msg_Warn( p_demux, "Unknown extension " GUID_FMT " data size of %u",
715               GUID_PRINT( p_ext->i_extension_id ), i_payload_extensions_size );
716 }
717
718 static int DemuxPayload(demux_t *p_demux, struct asf_packet_t *pkt, int i_payload)
719 {
720 #ifndef ASF_DEBUG
721     VLC_UNUSED( i_payload );
722 #endif
723     demux_sys_t *p_sys = p_demux->p_sys;
724
725     if( ! pkt->left || pkt->i_skip >= pkt->left )
726         return -1;
727
728     bool b_packet_keyframe = pkt->p_peek[pkt->i_skip] >> 7;
729     uint8_t i_stream_number = pkt->p_peek[pkt->i_skip++] & 0x7f;
730
731     uint32_t i_media_object_number = 0;
732     if (GetValue2b(&i_media_object_number, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 4) < 0)
733         return -1;
734     uint32_t i_media_object_offset = 0;
735     if (GetValue2b(&i_media_object_offset, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 2) < 0)
736         return -1;
737     uint32_t i_replicated_data_length = 0;
738     if (GetValue2b(&i_replicated_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property) < 0)
739         return -1;
740
741     mtime_t i_base_pts;
742     uint8_t i_pts_delta = 0;
743     uint32_t i_payload_data_length = 0;
744     uint32_t i_temp_payload_length = 0;
745     bool b_preroll_done = false;
746     p_sys->p_fp->i_preroll = __MIN( p_sys->p_fp->i_preroll, INT64_MAX );
747
748     /* Non compressed */
749     if( i_replicated_data_length > 7 ) // should be at least 8 bytes
750     {
751         /* Followed by 2 optional DWORDS, offset in media and presentation time */
752         i_base_pts = (mtime_t)GetDWLE( pkt->p_peek + pkt->i_skip + 4 );
753
754         /* Parsing extensions, See 7.3.1 */
755         ParsePayloadExtensions( p_demux, p_sys->track[i_stream_number], pkt,
756                                 i_replicated_data_length, &b_packet_keyframe );
757
758         b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
759         i_base_pts -= p_sys->p_fp->i_preroll;
760         pkt->i_skip += i_replicated_data_length;
761
762         if( ! pkt->left || pkt->i_skip >= pkt->left )
763             return -1;
764     }
765     else if ( i_replicated_data_length == 0 )
766     {
767         /* optional DWORDS missing */
768         i_base_pts = (mtime_t)pkt->send_time;
769         b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
770     }
771     /* Compressed payload */
772     else if( i_replicated_data_length == 1 )
773     {
774         /* i_media_object_offset is presentation time */
775         /* Next byte is Presentation Time Delta */
776         i_pts_delta = pkt->p_peek[pkt->i_skip];
777         i_base_pts = (mtime_t)i_media_object_offset;
778         b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
779         i_base_pts -= p_sys->p_fp->i_preroll;
780         pkt->i_skip++;
781         i_media_object_offset = 0;
782     }
783     else
784     {
785         /* >1 && <8 Invalid replicated length ! */
786         msg_Warn( p_demux, "Invalid replicated data length detected." );
787         i_payload_data_length = pkt->length - pkt->padding_length - pkt->i_skip;
788         goto skip;
789     }
790
791     if (i_base_pts < 0) i_base_pts = 0; // FIXME?
792     i_base_pts *= 1000;
793
794     if( pkt->multiple ) {
795         if (GetValue2b(&i_temp_payload_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->length_type) < 0)
796             return -1;
797     } else
798         i_temp_payload_length = pkt->length - pkt->padding_length - pkt->i_skip;
799
800     i_payload_data_length = i_temp_payload_length;
801
802 #ifdef ASF_DEBUG
803      msg_Dbg( p_demux,
804               "payload(%d) stream_number:%"PRIu8" media_object_number:%d media_object_offset:%"PRIu32" replicated_data_length:%"PRIu32" payload_data_length %"PRIu32,
805               i_payload + 1, i_stream_number, i_media_object_number,
806               i_media_object_offset, i_replicated_data_length, i_payload_data_length );
807      msg_Dbg( p_demux,
808               "   pts=%"PRId64" st=%"PRIu32, i_base_pts, pkt->send_time );
809 #endif
810
811      if( ! i_payload_data_length || i_payload_data_length > pkt->left )
812      {
813          msg_Dbg( p_demux, "  payload length problem %d %"PRIu32" %"PRIu32, pkt->multiple, i_payload_data_length, pkt->left );
814          return -1;
815      }
816
817     asf_track_t *tk = p_sys->track[i_stream_number];
818     if( tk == NULL )
819     {
820         msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
821         goto skip;
822     }
823
824     if( p_sys->i_wait_keyframe )
825     {
826         if ( i_stream_number == p_sys->i_seek_track )
827         {
828             if ( !b_packet_keyframe )
829             {
830                 p_sys->i_wait_keyframe--;
831                 goto skip;
832             }
833             else
834                 p_sys->i_wait_keyframe = 0;
835         }
836         else
837             goto skip;
838     }
839
840     if( !tk->p_es )
841         goto skip;
842
843     if ( b_preroll_done )
844     {
845         tk->i_time = INT64_C(1000) * pkt->send_time;
846         tk->i_time -= p_sys->p_fp->i_preroll * 1000;
847         tk->i_time -= tk->p_sp->i_time_offset * 10;
848     }
849
850     uint32_t i_subpayload_count = 0;
851     while (i_payload_data_length)
852     {
853         uint32_t i_sub_payload_data_length = i_payload_data_length;
854         if( i_replicated_data_length == 1 )
855         {
856             i_sub_payload_data_length = pkt->p_peek[pkt->i_skip++];
857             i_payload_data_length--;
858         }
859
860         SkipBytes( p_demux->s, pkt->i_skip );
861
862         mtime_t i_payload_pts = i_base_pts + (mtime_t)i_pts_delta * i_subpayload_count * 1000;
863         i_payload_pts -= tk->p_sp->i_time_offset * 10;
864         mtime_t i_payload_dts = INT64_C(1000) * pkt->send_time;
865         i_payload_dts -= p_sys->p_fp->i_preroll * 1000;
866         i_payload_dts -= tk->p_sp->i_time_offset * 10;
867
868         if ( i_sub_payload_data_length &&
869              DemuxSubPayload(p_demux, tk, i_sub_payload_data_length,
870                         i_payload_pts, i_payload_dts, i_media_object_offset,
871                         b_packet_keyframe ) < 0)
872             return -1;
873
874         if ( pkt->left > pkt->i_skip + i_sub_payload_data_length )
875             pkt->left -= pkt->i_skip + i_sub_payload_data_length;
876         else
877             pkt->left = 0;
878         pkt->i_skip = 0;
879         if( pkt->left > 0 )
880         {
881             int i_return = stream_Peek( p_demux->s, &pkt->p_peek, __MIN(pkt->left, INT_MAX) );
882             if ( i_return <= 0 || (unsigned int) i_return < __MIN(pkt->left, INT_MAX) )
883             {
884             msg_Warn( p_demux, "cannot peek, EOF ?" );
885             return -1;
886             }
887         }
888
889         if ( i_sub_payload_data_length <= i_payload_data_length )
890             i_payload_data_length -= i_sub_payload_data_length;
891         else
892             i_payload_data_length = 0;
893
894         i_subpayload_count++;
895     }
896
897     return 0;
898
899 skip:
900     pkt->i_skip += i_payload_data_length;
901     return 0;
902 }
903
904 static int DemuxPacket( demux_t *p_demux )
905 {
906     demux_sys_t *p_sys = p_demux->p_sys;
907
908     uint32_t i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
909
910     const uint8_t *p_peek;
911     int i_return = stream_Peek( p_demux->s, &p_peek,i_data_packet_min );
912     if( i_return <= 0 || ((unsigned int) i_return) < i_data_packet_min )
913     {
914         msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
915         return 0;
916     }
917     unsigned int i_skip = 0;
918
919     /* *** parse error correction if present *** */
920     if( p_peek[0]&0x80 )
921     {
922         unsigned int i_error_correction_data_length = p_peek[0] & 0x0f;
923         unsigned int i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;
924         unsigned int i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03;
925         i_skip += 1; // skip error correction flags
926
927         if( i_error_correction_length_type != 0x00 ||
928             i_opaque_data_present != 0 ||
929             i_error_correction_data_length != 0x02 )
930         {
931             goto loop_error_recovery;
932         }
933
934         i_skip += i_error_correction_data_length;
935     }
936     else
937         msg_Warn( p_demux, "no error correction" );
938
939     /* sanity check */
940     if( i_skip + 2 >= i_data_packet_min )
941         goto loop_error_recovery;
942
943     struct asf_packet_t pkt;
944     int i_packet_flags = p_peek[i_skip]; i_skip++;
945     pkt.property = p_peek[i_skip]; i_skip++;
946     pkt.multiple = !!(i_packet_flags&0x01);
947
948     pkt.length = i_data_packet_min;
949     pkt.padding_length = 0;
950
951     if (GetValue2b(&pkt.length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 5) < 0)
952         goto loop_error_recovery;
953     uint32_t i_packet_sequence;
954     if (GetValue2b(&i_packet_sequence, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 1) < 0)
955         goto loop_error_recovery;
956     if (GetValue2b(&pkt.padding_length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 3) < 0)
957         goto loop_error_recovery;
958
959     if( pkt.padding_length > pkt.length )
960     {
961         msg_Warn( p_demux, "Too large padding: %"PRIu32, pkt.padding_length );
962         goto loop_error_recovery;
963     }
964
965     if( pkt.length < i_data_packet_min )
966     {
967         /* if packet length too short, there is extra padding */
968         pkt.padding_length += i_data_packet_min - pkt.length;
969         pkt.length = i_data_packet_min;
970     }
971
972     pkt.send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
973     /* uint16_t i_packet_duration = GetWLE( p_peek + i_skip ); */ i_skip += 2;
974
975     i_return = stream_Peek( p_demux->s, &p_peek, pkt.length );
976     if( i_return <= 0 || pkt.length == 0 || (unsigned int)i_return < pkt.length )
977     {
978         msg_Warn( p_demux, "cannot peek, EOF ?" );
979         return 0;
980     }
981
982     int i_payload_count = 1;
983     pkt.length_type = 0x02; //unused
984     if( pkt.multiple )
985     {
986         i_payload_count = p_peek[i_skip] & 0x3f;
987         pkt.length_type = ( p_peek[i_skip] >> 6 )&0x03;
988         i_skip++;
989     }
990
991 #ifdef ASF_DEBUG
992     msg_Dbg(p_demux, "%d payloads", i_payload_count);
993 #endif
994
995     pkt.i_skip = i_skip;
996     pkt.p_peek = p_peek;
997     pkt.left = pkt.length;
998
999     for( int i_payload = 0; i_payload < i_payload_count ; i_payload++ )
1000         if (DemuxPayload(p_demux, &pkt, i_payload) < 0)
1001         {
1002             msg_Warn( p_demux, "payload err %d / %d", i_payload + 1, i_payload_count );
1003             return 0;
1004         }
1005
1006     if( pkt.left > 0 )
1007     {
1008 #ifdef ASF_DEBUG
1009         if( pkt.left > pkt.padding_length )
1010             msg_Warn( p_demux, "Didn't read %"PRIu32" bytes in the packet",
1011                             pkt.left - pkt.padding_length );
1012         else if( pkt.left < pkt.padding_length )
1013             msg_Warn( p_demux, "Read %"PRIu32" too much bytes in the packet",
1014                             pkt.padding_length - pkt.left );
1015 #endif
1016         int i_return = stream_Read( p_demux->s, NULL, pkt.left );
1017         if( i_return < 0 || (unsigned int) i_return < pkt.left )
1018         {
1019             msg_Err( p_demux, "cannot skip data, EOF ?" );
1020             return 0;
1021         }
1022     }
1023
1024     return 1;
1025
1026 loop_error_recovery:
1027     msg_Warn( p_demux, "unsupported packet header" );
1028     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1029     {
1030         msg_Err( p_demux, "unsupported packet header, fatal error" );
1031         return -1;
1032     }
1033     i_return = stream_Read( p_demux->s, NULL, i_data_packet_min );
1034     if( i_return <= 0 || (unsigned int) i_return != i_data_packet_min )
1035     {
1036         msg_Warn( p_demux, "cannot skip data, EOF ?" );
1037         return 0;
1038     }
1039
1040     return 1;
1041 }
1042
1043 /*****************************************************************************
1044  *
1045  *****************************************************************************/
1046 typedef struct asf_es_priorities_t
1047 {
1048     uint16_t *pi_stream_numbers;
1049     uint16_t i_count;
1050 } asf_es_priorities_t;
1051
1052 /* Fills up our exclusion list */
1053 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1054                                          asf_es_priorities_t *p_prios )
1055 {
1056     /* Find stream exclusions */
1057     asf_object_advanced_mutual_exclusion_t *p_mutex =
1058             ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
1059     if (! p_mutex ) return;
1060
1061 #if ( UINT_MAX > SIZE_MAX / 2 )
1062     if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1063         return;
1064 #endif
1065     p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof(uint16_t) );
1066     if ( !p_prios->pi_stream_numbers ) return;
1067
1068     if ( p_mutex->i_stream_number_count )
1069     {
1070         /* Just set highest prio on highest in the group */
1071         for ( uint16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
1072         {
1073             if ( p_prios->i_count > p_sys->i_track ) break;
1074             p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
1075         }
1076     }
1077 }
1078
1079 /* Fills up our bitrate exclusion list */
1080 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1081                                                  asf_es_priorities_t *p_prios )
1082 {
1083     /* Find bitrate exclusions */
1084     asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
1085             ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
1086     if (! p_bitrate_mutex ) return;
1087
1088 #if ( UINT_MAX > SIZE_MAX / 2 )
1089     if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1090         return;
1091 #endif
1092     p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof( uint16_t ) );
1093     if ( !p_prios->pi_stream_numbers ) return;
1094
1095     if ( p_bitrate_mutex->i_stream_number_count )
1096     {
1097         /* Just remove < highest */
1098         for ( uint16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
1099         {
1100             if ( p_prios->i_count > p_sys->i_track ) break;
1101             p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
1102         }
1103     }
1104
1105 }
1106
1107 #define GET_CHECKED( target, getter, maxtarget, temp ) \
1108 {\
1109     temp i_temp = getter;\
1110     if ( i_temp > maxtarget ) {\
1111         msg_Warn( p_demux, "rejecting stream %u : " #target " overflow", i_stream );\
1112         es_format_Clean( &fmt );\
1113         goto error;\
1114     } else {\
1115         target = i_temp;\
1116     }\
1117 }
1118
1119 static int DemuxInit( demux_t *p_demux )
1120 {
1121     demux_sys_t *p_sys = p_demux->p_sys;
1122
1123     /* init context */
1124     p_sys->i_time   = -1;
1125     p_sys->i_length = 0;
1126     p_sys->i_bitrate = 0;
1127     p_sys->p_root   = NULL;
1128     p_sys->p_fp     = NULL;
1129     p_sys->b_index  = 0;
1130     p_sys->i_track  = 0;
1131     p_sys->i_seek_track = 0;
1132     p_sys->i_wait_keyframe = 0;
1133     for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1134     {
1135         p_sys->track[i] = NULL;
1136     }
1137     p_sys->i_data_begin = 0;
1138     p_sys->i_data_end   = 0;
1139     p_sys->meta         = NULL;
1140
1141     /* Now load all object ( except raw data ) */
1142     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1143     if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, p_sys->b_canfastseek)) )
1144     {
1145         msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
1146         return VLC_EGENERIC;
1147     }
1148     p_sys->p_fp = p_sys->p_root->p_fp;
1149
1150     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1151     {
1152         msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
1153         goto error;
1154     }
1155
1156     if ( ASF_FindObject( p_sys->p_root->p_hdr,
1157                          &asf_object_content_encryption_guid, 0 ) != NULL
1158          || ASF_FindObject( p_sys->p_root->p_hdr,
1159                             &asf_object_extended_content_encryption_guid, 0 ) != NULL
1160          || ASF_FindObject( p_sys->p_root->p_hdr,
1161                          &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
1162     {
1163         dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
1164                         _("DRM protected streams are not supported.") );
1165         goto error;
1166     }
1167
1168     p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
1169                                       &asf_object_stream_properties_guid );
1170     if( p_sys->i_track == 0 )
1171     {
1172         msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
1173         goto error;
1174     }
1175     msg_Dbg( p_demux, "found %u streams", p_sys->i_track );
1176
1177     /* check if index is available */
1178     asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
1179                                                   &asf_object_simple_index_guid, 0 );
1180     const bool b_index = p_index && p_index->i_index_entry_count;
1181
1182     /* Find the extended header if any */
1183     asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
1184                                               &asf_object_header_extension_guid, 0 );
1185
1186     asf_object_language_list_t *p_languages = NULL;
1187     asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
1188     asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
1189
1190     if( p_hdr_ext )
1191     {
1192         p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
1193
1194         ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
1195         ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
1196     }
1197
1198     for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1199     {
1200         asf_track_t    *tk;
1201         asf_object_stream_properties_t *p_sp;
1202         asf_object_extended_stream_properties_t *p_esp;
1203         bool b_access_selected;
1204
1205         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
1206                                &asf_object_stream_properties_guid,
1207                                i_stream );
1208         p_esp = NULL;
1209
1210         tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
1211         memset( tk, 0, sizeof( asf_track_t ) );
1212
1213         tk->i_time = -1;
1214         tk->p_sp = p_sp;
1215         tk->p_es = NULL;
1216         tk->p_esp = NULL;
1217         tk->p_frame = NULL;
1218
1219         if ( strncmp( p_demux->psz_access, "mms", 3 ) )
1220         {
1221             /* Check (not mms) if this track is selected (ie will receive data) */
1222             if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1223                                  (int) p_sp->i_stream_number, &b_access_selected ) &&
1224                 !b_access_selected )
1225             {
1226                 tk->i_cat = UNKNOWN_ES;
1227                 msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
1228                          p_sp->i_stream_number );
1229                 continue;
1230             }
1231         }
1232
1233         /* Find the associated extended_stream_properties if any */
1234         if( p_hdr_ext )
1235         {
1236             int i_ext_stream = ASF_CountObject( p_hdr_ext,
1237                                                 &asf_object_extended_stream_properties_guid );
1238             for( int i = 0; i < i_ext_stream; i++ )
1239             {
1240                 asf_object_t *p_tmp =
1241                     ASF_FindObject( p_hdr_ext,
1242                                     &asf_object_extended_stream_properties_guid, i );
1243                 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
1244                 {
1245                     p_esp = &p_tmp->ext_stream;
1246                     tk->p_esp = p_esp;
1247                     break;
1248                 }
1249             }
1250         }
1251
1252         es_format_t fmt;
1253
1254         if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
1255             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
1256         {
1257             uint8_t *p_data = p_sp->p_type_specific_data;
1258             int i_format;
1259
1260             es_format_Init( &fmt, AUDIO_ES, 0 );
1261             i_format = GetWLE( &p_data[0] );
1262             wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1263
1264             GET_CHECKED( fmt.audio.i_channels,      GetWLE( &p_data[2] ),
1265                                                         255, uint16_t );
1266             GET_CHECKED( fmt.audio.i_rate,          GetDWLE( &p_data[4] ),
1267                                                         UINT_MAX, uint32_t );
1268             GET_CHECKED( fmt.i_bitrate,             GetDWLE( &p_data[8] ) * 8,
1269                                                         UINT_MAX, uint32_t );
1270             fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
1271             fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
1272
1273             if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1274                 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1275                 i_format != WAVE_FORMAT_MPEG )
1276             {
1277                 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1278                                      p_sp->i_type_specific_data_length -
1279                                      sizeof( WAVEFORMATEX ) ),
1280                              INT_MAX, uint32_t );
1281                 fmt.p_extra = malloc( fmt.i_extra );
1282                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1283                         fmt.i_extra );
1284             }
1285
1286             msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
1287                     GetWLE( p_data ), p_sp->i_stream_number );
1288         }
1289         else if( guidcmp( &p_sp->i_stream_type,
1290                               &asf_object_stream_type_video ) &&
1291                  p_sp->i_type_specific_data_length >= 11 +
1292                  sizeof( VLC_BITMAPINFOHEADER ) )
1293         {
1294             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
1295
1296             es_format_Init( &fmt, VIDEO_ES,
1297                             VLC_FOURCC( p_data[16], p_data[17],
1298                                         p_data[18], p_data[19] ) );
1299
1300             GET_CHECKED( fmt.video.i_width,      GetDWLE( p_data + 4 ),
1301                                                      UINT_MAX, uint32_t );
1302             GET_CHECKED( fmt.video.i_height,     GetDWLE( p_data + 8 ),
1303                                                      UINT_MAX, uint32_t );
1304
1305             if( p_esp && p_esp->i_average_time_per_frame > 0 )
1306             {
1307                 fmt.video.i_frame_rate = 10000000;
1308                 GET_CHECKED( fmt.video.i_frame_rate_base,
1309                              p_esp->i_average_time_per_frame,
1310                              UINT_MAX, uint64_t );
1311             }
1312
1313             if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
1314             {
1315                 /* DVR-MS special ASF */
1316                 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
1317                 fmt.b_packetized = false;
1318             }
1319
1320             if( p_sp->i_type_specific_data_length > 11 +
1321                 sizeof( VLC_BITMAPINFOHEADER ) )
1322             {
1323                 GET_CHECKED( fmt.i_extra, __MIN( GetDWLE( p_data ),
1324                                      p_sp->i_type_specific_data_length - 11 -
1325                                      sizeof( VLC_BITMAPINFOHEADER ) ),
1326                              UINT_MAX, uint32_t );
1327                 fmt.p_extra = malloc( fmt.i_extra );
1328                 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
1329                         fmt.i_extra );
1330             }
1331
1332             /* Look for an aspect ratio */
1333             if( p_sys->p_root->p_metadata )
1334             {
1335                 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
1336                 unsigned int i_aspect_x = 0, i_aspect_y = 0;
1337                 uint32_t i;
1338                 for( i = 0; i < p_meta->i_record_entries_count; i++ )
1339                 {
1340                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
1341                     {
1342                         if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
1343                             p_meta->record[i].i_stream ==
1344                             p_sp->i_stream_number )
1345                             GET_CHECKED( i_aspect_x, p_meta->record[i].i_val,
1346                                          UINT_MAX, uint64_t );
1347                     }
1348                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
1349                     {
1350                         if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
1351                             p_meta->record[i].i_stream ==
1352                             p_sp->i_stream_number )
1353                             GET_CHECKED( i_aspect_y, p_meta->record[i].i_val,
1354                                          UINT_MAX, uint64_t );
1355                     }
1356                 }
1357
1358                 if( i_aspect_x && i_aspect_y )
1359                 {
1360                     fmt.video.i_sar_num = i_aspect_x;
1361                     fmt.video.i_sar_den = i_aspect_y;
1362                 }
1363             }
1364
1365             /* If there is a video track then use the index for seeking */
1366             p_sys->b_index = b_index;
1367
1368             msg_Dbg( p_demux, "added new video stream(ID:%d)",
1369                      p_sp->i_stream_number );
1370         }
1371         else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
1372             p_sp->i_type_specific_data_length >= 64 )
1373         {
1374             /* Now follows a 64 byte header of which we don't know much */
1375             guid_t  *p_ref  = (guid_t *)p_sp->p_type_specific_data;
1376             uint8_t *p_data = p_sp->p_type_specific_data + 64;
1377             unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1378
1379             msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1380             if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1381                 i_data >= sizeof( WAVEFORMATEX ) - 2)
1382             {
1383                 uint16_t i_format;
1384                 es_format_Init( &fmt, AUDIO_ES, 0 );
1385                 i_format = GetWLE( &p_data[0] );
1386                 if( i_format == 0 )
1387                     fmt.i_codec = VLC_CODEC_A52;
1388                 else
1389                     wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1390                 GET_CHECKED( fmt.audio.i_channels,      GetWLE( &p_data[2] ),
1391                                                             255, uint16_t );
1392                 GET_CHECKED( fmt.audio.i_rate,          GetDWLE( &p_data[4] ),
1393                                                             UINT_MAX, uint32_t );
1394                 GET_CHECKED( fmt.i_bitrate,             GetDWLE( &p_data[8] ) * 8,
1395                                                             UINT_MAX, uint32_t );
1396                 fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
1397                 fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
1398                 fmt.b_packetized = true;
1399
1400                 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1401                     i_format != WAVE_FORMAT_MPEGLAYER3 &&
1402                     i_format != WAVE_FORMAT_MPEG )
1403                 {
1404                     GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1405                                          p_sp->i_type_specific_data_length -
1406                                          sizeof( WAVEFORMATEX ) ),
1407                                  INT_MAX, uint32_t );
1408                     fmt.p_extra = malloc( fmt.i_extra );
1409                     memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1410                         fmt.i_extra );
1411                 }
1412
1413                 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1414                     i_format, p_sp->i_stream_number );
1415             }
1416             else
1417             {
1418                 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1419             }
1420         }
1421         else
1422         {
1423             es_format_Init( &fmt, UNKNOWN_ES, 0 );
1424         }
1425
1426         tk->i_cat = fmt.i_cat;
1427         if( fmt.i_cat != UNKNOWN_ES )
1428         {
1429             if( p_esp && p_languages &&
1430                 p_esp->i_language_index < p_languages->i_language )
1431             {
1432                 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1433                 char *p;
1434                 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1435                     *p = '\0';
1436             }
1437
1438             /* Set our priority so we won't get multiple videos */
1439             int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1440             for( uint16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1441             {
1442                 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1443                 {
1444                     i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1445                     break;
1446                 }
1447             }
1448             for( uint16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1449             {
1450                 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1451                 {
1452                     i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1453                     break;
1454                 }
1455             }
1456             fmt.i_priority = i_priority;
1457
1458             if ( i_stream <= INT_MAX )
1459                 fmt.i_id = i_stream;
1460             else
1461                 msg_Warn( p_demux, "Can't set fmt.i_id to match stream id %u", i_stream );
1462
1463             if ( fmt.i_cat == VIDEO_ES )
1464             {
1465                 /* Backup our video format */
1466                 tk->p_fmt = malloc( sizeof( es_format_t ) );
1467                 if ( tk->p_fmt )
1468                     es_format_Copy( tk->p_fmt, &fmt );
1469             }
1470
1471             tk->p_es = es_out_Add( p_demux->out, &fmt );
1472
1473             if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1474                                  (int) p_sp->i_stream_number, &b_access_selected ) &&
1475                 b_access_selected )
1476             {
1477                 p_sys->i_access_selected_track[fmt.i_cat] = p_sp->i_stream_number;
1478             }
1479
1480         }
1481         else
1482         {
1483             msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1484                      p_sp->i_stream_number );
1485         }
1486
1487         es_format_Clean( &fmt );
1488     }
1489
1490     free( fmt_priorities_ex.pi_stream_numbers );
1491     free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1492
1493     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1494     if( p_sys->p_root->p_data->i_object_size != 0 )
1495     { /* local file */
1496         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1497                                     p_sys->p_root->p_data->i_object_size;
1498         p_sys->i_data_end = __MIN( (uint64_t)stream_Size( p_demux->s ), p_sys->i_data_end );
1499     }
1500     else
1501     { /* live/broacast */
1502         p_sys->i_data_end = 0;
1503     }
1504
1505     /* go to first packet */
1506     stream_Seek( p_demux->s, p_sys->i_data_begin );
1507
1508     /* try to calculate movie time */
1509     if( p_sys->p_fp->i_data_packets_count > 0 )
1510     {
1511         uint64_t i_count;
1512         uint64_t i_size = stream_Size( p_demux->s );
1513
1514         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1515         {
1516             i_size = p_sys->i_data_end;
1517         }
1518
1519         /* real number of packets */
1520         i_count = ( i_size - p_sys->i_data_begin ) /
1521                   p_sys->p_fp->i_min_data_packet_size;
1522
1523         /* calculate the time duration in micro-s */
1524         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1525                    (mtime_t)i_count /
1526                    (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1527         if( p_sys->i_length < 0 )
1528             p_sys->i_length = 0;
1529
1530         if( p_sys->i_length > 0 )
1531         {
1532             p_sys->i_bitrate = 8 * i_size * 1000000 / p_sys->i_length;
1533         }
1534     }
1535
1536     /* Create meta information */
1537     p_sys->meta = vlc_meta_New();
1538
1539     asf_object_content_description_t *p_cd;
1540     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1541                                  &asf_object_content_description_guid, 0 ) ) )
1542     {
1543         if( p_cd->psz_title && *p_cd->psz_title )
1544         {
1545             vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1546         }
1547         if( p_cd->psz_artist && *p_cd->psz_artist )
1548         {
1549              vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1550         }
1551         if( p_cd->psz_copyright && *p_cd->psz_copyright )
1552         {
1553             vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1554         }
1555         if( p_cd->psz_description && *p_cd->psz_description )
1556         {
1557             vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1558         }
1559         if( p_cd->psz_rating && *p_cd->psz_rating )
1560         {
1561             vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1562         }
1563     }
1564     /// \tood Fix Child meta for ASF tracks
1565 #if 0
1566     for( i_stream = 0, i = 0; i < MAX_ASF_TRACKS; i++ )
1567     {
1568         asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1569                                                         &asf_object_codec_list_guid, 0 );
1570
1571         if( p_sys->track[i] )
1572         {
1573             vlc_meta_t *tk = vlc_meta_New();
1574             TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1575
1576             if( p_cl && i_stream < p_cl->i_codec_entries_count )
1577             {
1578                 if( p_cl->codec[i_stream].psz_name &&
1579                     *p_cl->codec[i_stream].psz_name )
1580                 {
1581                     vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1582                                   p_cl->codec[i_stream].psz_name );
1583                 }
1584                 if( p_cl->codec[i_stream].psz_description &&
1585                     *p_cl->codec[i_stream].psz_description )
1586                 {
1587                     vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1588                                   p_cl->codec[i_stream].psz_description );
1589                 }
1590             }
1591             i_stream++;
1592         }
1593     }
1594 #endif
1595     return VLC_SUCCESS;
1596
1597 error:
1598     ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1599     return VLC_EGENERIC;
1600 }
1601
1602 /*****************************************************************************
1603  * FlushRemainingPackets: flushes tail packets
1604  *****************************************************************************/
1605
1606 static void FlushRemainingPackets( demux_t *p_demux )
1607 {
1608     demux_sys_t *p_sys = p_demux->p_sys;
1609     for ( unsigned int i = 0; i < MAX_ASF_TRACKS; i++ )
1610     {
1611         asf_track_t *tk = p_sys->track[i];
1612         if( tk && tk->p_frame )
1613             SendPacket( p_demux, tk );
1614     }
1615 }
1616
1617 /*****************************************************************************
1618  *
1619  *****************************************************************************/
1620 static void DemuxEnd( demux_t *p_demux )
1621 {
1622     demux_sys_t *p_sys = p_demux->p_sys;
1623
1624     if( p_sys->p_root )
1625     {
1626         ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1627         p_sys->p_root = NULL;
1628     }
1629     if( p_sys->meta )
1630     {
1631         vlc_meta_Delete( p_sys->meta );
1632         p_sys->meta = NULL;
1633     }
1634
1635     for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1636     {
1637         asf_track_t *tk = p_sys->track[i];
1638
1639         if( tk )
1640         {
1641             if( tk->p_frame )
1642                 block_ChainRelease( tk->p_frame );
1643
1644             if( tk->p_es )
1645             {
1646                 es_out_Del( p_demux->out, tk->p_es );
1647             }
1648             if ( tk->p_fmt )
1649             {
1650                 es_format_Clean( tk->p_fmt );
1651                 free( tk->p_fmt );
1652             }
1653             free( tk );
1654         }
1655         p_sys->track[i] = 0;
1656     }
1657 }
1658