]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
demux/subtitle: Fix wrong ASS header parsing
[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 "asfpacket.h"
45 #include "libasf.h"
46 #include "assert.h"
47
48 /* TODO
49  *  - add support for the newly added object: language, bitrate,
50  *                                            extended stream properties.
51  */
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 static int  Open  ( vlc_object_t * );
57 static void Close ( vlc_object_t * );
58
59 vlc_module_begin ()
60     set_category( CAT_INPUT )
61     set_subcategory( SUBCAT_INPUT_DEMUX )
62     set_description( N_("ASF/WMV demuxer") )
63     set_capability( "demux", 200 )
64     set_callbacks( Open, Close )
65     add_shortcut( "asf", "wmv" )
66 vlc_module_end ()
67
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int Demux  ( demux_t * );
73 static int Control( demux_t *, int i_query, va_list args );
74 static void FlushRemainingPackets( demux_t *p_demux );
75
76 #define MAX_ASF_TRACKS (ASF_MAX_STREAMNUMBER + 1)
77 #define ASF_PREROLL_FROM_CURRENT -1
78
79 /* callbacks for packet parser */
80 static void Packet_UpdateTime( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
81                                mtime_t i_time );
82 static asf_track_info_t * Packet_GetTrackInfo( asf_packet_sys_t *p_packetsys,
83                                                uint8_t i_stream_number );
84 static bool Packet_DoSkip( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, bool b_packet_keyframe );
85 static void Packet_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame);
86 static void Packet_SetAR( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
87                           uint8_t i_ratio_x, uint8_t i_ratio_y );
88
89 typedef struct
90 {
91     int i_cat;
92
93     es_out_id_t     *p_es;
94     es_format_t     *p_fmt; /* format backup for video changes */
95     bool             b_selected;
96
97     mtime_t          i_time; /* track time*/
98
99     asf_track_info_t info;
100
101 } asf_track_t;
102
103 struct demux_sys_t
104 {
105     mtime_t             i_time;     /* s */
106     mtime_t             i_length;   /* length of file file */
107     uint64_t            i_bitrate;  /* global file bitrate */
108
109     asf_object_root_t            *p_root;
110     asf_object_file_properties_t *p_fp;
111
112     unsigned int        i_track;
113     asf_track_t         *track[MAX_ASF_TRACKS]; /* track number is stored on 7 bits */
114
115     uint64_t            i_data_begin;
116     uint64_t            i_data_end;
117
118     bool                b_index;
119     bool                b_canfastseek;
120     uint8_t             i_seek_track;
121     uint8_t             i_access_selected_track[ES_CATEGORY_COUNT]; /* mms, depends on access algorithm */
122     unsigned int        i_wait_keyframe;
123
124     mtime_t             i_preroll_start;
125
126     asf_packet_sys_t    packet_sys;
127
128     vlc_meta_t          *meta;
129 };
130
131 static mtime_t  GetMoviePTS( demux_sys_t * );
132 static int      DemuxInit( demux_t * );
133 static void     DemuxEnd( demux_t * );
134
135 /*****************************************************************************
136  * Open: check file and initializes ASF structures
137  *****************************************************************************/
138 static int Open( vlc_object_t * p_this )
139 {
140     demux_t     *p_demux = (demux_t *)p_this;
141     demux_sys_t *p_sys;
142     guid_t      guid;
143     const uint8_t     *p_peek;
144
145     /* A little test to see if it could be a asf stream */
146     if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
147
148     ASF_GetGUID( &guid, p_peek );
149     if( !guidcmp( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
150
151     /* Set p_demux fields */
152     p_demux->pf_demux = Demux;
153     p_demux->pf_control = Control;
154     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
155
156     /* Load the headers */
157     if( DemuxInit( p_demux ) )
158     {
159         free( p_sys );
160         return VLC_EGENERIC;
161     }
162
163     p_sys->packet_sys.p_demux = p_demux;
164     p_sys->packet_sys.pf_doskip = Packet_DoSkip;
165     p_sys->packet_sys.pf_send = Packet_Send;
166     p_sys->packet_sys.pf_gettrackinfo = Packet_GetTrackInfo;
167     p_sys->packet_sys.pf_updatetime = Packet_UpdateTime;
168     p_sys->packet_sys.pf_setaspectratio = Packet_SetAR;
169
170     return VLC_SUCCESS;
171 }
172
173 /*****************************************************************************
174  * Demux: read packet and send them to decoders
175  *****************************************************************************/
176 static int Demux( demux_t *p_demux )
177 {
178     demux_sys_t *p_sys = p_demux->p_sys;
179
180     for( int i=0; i<ES_CATEGORY_COUNT; i++ )
181     {
182         if ( p_sys->i_access_selected_track[i] > 0 )
183         {
184             es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
185                             p_sys->track[p_sys->i_access_selected_track[i]]->p_es, true );
186             p_sys->i_access_selected_track[i] = 0;
187         }
188     }
189
190     /* Get selected tracks, especially for computing PCR */
191     for( int i=0; i<MAX_ASF_TRACKS; i++ )
192     {
193         asf_track_t *tk = p_sys->track[i];
194         if ( !tk ) continue;
195         if ( tk->p_es )
196             es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, & tk->b_selected );
197         else
198             tk->b_selected = false;
199     }
200
201     for( ;; )
202     {
203         const uint8_t *p_peek;
204         mtime_t i_length;
205         mtime_t i_time_begin = GetMoviePTS( p_sys );
206         int i_result;
207
208         if( !vlc_object_alive (p_demux) )
209             break;
210 #if 0
211         /* FIXME: returns EOF too early for some mms streams */
212         if( p_sys->i_data_end >= 0 &&
213                 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
214             return 0; /* EOF */
215 #endif
216
217         /* Check if we have concatenated files */
218         if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
219         {
220             guid_t guid;
221
222             ASF_GetGUID( &guid, p_peek );
223             if( guidcmp( &guid, &asf_object_header_guid ) )
224             {
225                 msg_Warn( p_demux, "found a new ASF header" );
226                 /* We end this stream */
227                 DemuxEnd( p_demux );
228
229                 /* And we prepare to read the next one */
230                 if( DemuxInit( p_demux ) )
231                 {
232                     msg_Err( p_demux, "failed to load the new header" );
233                     dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
234                                     _("VLC failed to load the ASF header.") );
235                     return 0;
236                 }
237                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
238                 continue;
239             }
240         }
241
242         /* Read and demux a packet */
243         if( ( i_result = DemuxASFPacket( &p_sys->packet_sys,
244                                       p_sys->p_fp->i_min_data_packet_size,
245                                       p_sys->p_fp->i_max_data_packet_size ) ) <= 0 )
246         {
247             FlushRemainingPackets( p_demux );
248             return i_result;
249         }
250         if( i_time_begin == -1 )
251         {
252             i_time_begin = GetMoviePTS( p_sys );
253         }
254         else
255         {
256             i_length = GetMoviePTS( p_sys ) - i_time_begin;
257             if( i_length < 0 || i_length >= 40 * 1000 ) break;
258         }
259     }
260
261     /* Set the PCR */
262     /* WARN: Don't move it before the end of the whole chunk */
263     p_sys->i_time = GetMoviePTS( p_sys );
264     if( p_sys->i_time >= 0 )
265     {
266 #ifdef ASF_DEBUG
267         msg_Dbg( p_demux, "Demux Loop Setting PCR to %"PRId64, VLC_TS_0 + p_sys->i_time );
268 #endif
269         es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
270     }
271
272     return 1;
273 }
274
275 /*****************************************************************************
276  * Close: frees unused data
277  *****************************************************************************/
278 static void Close( vlc_object_t * p_this )
279 {
280     demux_t     *p_demux = (demux_t *)p_this;
281
282     DemuxEnd( p_demux );
283
284     free( p_demux->p_sys );
285 }
286
287 /*****************************************************************************
288  * WaitKeyframe: computes the number of frames to wait for a keyframe
289  *****************************************************************************/
290 static void WaitKeyframe( demux_t *p_demux )
291 {
292     demux_sys_t *p_sys = p_demux->p_sys;
293     if ( ! p_sys->i_seek_track )
294     {
295         for ( int i=0; i<MAX_ASF_TRACKS; i++ )
296         {
297             asf_track_t *tk = p_sys->track[i];
298             if ( tk && tk->info.p_sp && tk->i_cat == VIDEO_ES && tk->b_selected )
299             {
300                 p_sys->i_seek_track = tk->info.p_sp->i_stream_number;
301                 break;
302             }
303         }
304     }
305
306     if ( p_sys->i_seek_track )
307     {
308         /* Skip forward at least 1 min */
309         asf_track_t *tk = p_sys->track[p_sys->i_seek_track];
310         if ( tk->info.p_esp && tk->info.p_esp->i_average_time_per_frame )
311         {
312             /* 1 min if fastseek, otherwise 5 sec */
313             /* That's a guess for bandwidth */
314             uint64_t i_maxwaittime = ( p_sys->b_canfastseek ) ? 600000000 : 50000000;
315             i_maxwaittime /= tk->info.p_esp->i_average_time_per_frame;
316             p_sys->i_wait_keyframe = __MIN( i_maxwaittime, UINT_MAX );
317         }
318         else
319         {
320             p_sys->i_wait_keyframe = ( p_sys->b_canfastseek ) ? 25 * 30 : 25 * 5;
321         }
322     }
323     else
324     {
325         p_sys->i_wait_keyframe = 0;
326     }
327
328 }
329
330 /*****************************************************************************
331  * SeekIndex: goto to i_date or i_percent
332  *****************************************************************************/
333 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
334 {
335     demux_sys_t *p_sys = p_demux->p_sys;
336
337     WaitKeyframe( p_demux );
338
339     msg_Dbg( p_demux, "seek with percent: waiting %i frames", p_sys->i_wait_keyframe );
340     return demux_vaControlHelper( p_demux->s, __MIN( INT64_MAX, p_sys->i_data_begin ),
341                                    __MIN( INT64_MAX, p_sys->i_data_end ),
342                                    __MIN( INT64_MAX, p_sys->i_bitrate ),
343                                    __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
344                                    i_query, args );
345 }
346
347 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
348 {
349     demux_sys_t *p_sys = p_demux->p_sys;
350     asf_object_index_t *p_index;
351
352     msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
353              i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
354
355     if( i_date < 0 )
356         i_date = p_sys->i_length * f_pos;
357
358     p_sys->i_preroll_start = i_date - (int64_t) p_sys->p_fp->i_preroll;
359     if ( p_sys->i_preroll_start < 0 ) p_sys->i_preroll_start = 0;
360
361     p_index = ASF_FindObject( p_sys->p_root, &asf_object_simple_index_guid, 0 );
362
363     uint64_t i_entry = p_sys->i_preroll_start * 10 / p_index->i_index_entry_time_interval;
364     if( i_entry >= p_index->i_index_entry_count )
365     {
366         msg_Warn( p_demux, "Incomplete index" );
367         return VLC_EGENERIC;
368     }
369
370     WaitKeyframe( p_demux );
371
372     uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
373                         p_sys->p_fp->i_min_data_packet_size;
374
375     if ( stream_Seek( p_demux->s, i_offset + p_sys->i_data_begin ) == VLC_SUCCESS )
376     {
377         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_date );
378         return VLC_SUCCESS;
379     }
380     else return VLC_EGENERIC;
381 }
382
383 static void SeekPrepare( demux_t *p_demux )
384 {
385     demux_sys_t *p_sys = p_demux->p_sys;
386
387     p_sys->i_time = VLC_TS_INVALID;
388     p_sys->i_preroll_start = ASFPACKET_PREROLL_FROM_CURRENT;
389     for( int i = 0; i < MAX_ASF_TRACKS ; i++ )
390     {
391         asf_track_t *tk = p_sys->track[i];
392         if( !tk )
393             continue;
394
395         tk->i_time = -1;
396         if( tk->info.p_frame )
397             block_ChainRelease( tk->info.p_frame );
398         tk->info.p_frame = NULL;
399     }
400
401     es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
402 }
403
404 /*****************************************************************************
405  * Control:
406  *****************************************************************************/
407 static int Control( demux_t *p_demux, int i_query, va_list args )
408 {
409     demux_sys_t *p_sys = p_demux->p_sys;
410     vlc_meta_t  *p_meta;
411     int64_t     i64, *pi64;
412     int         i;
413     double      f, *pf;
414
415     switch( i_query )
416     {
417     case DEMUX_GET_LENGTH:
418         pi64 = (int64_t*)va_arg( args, int64_t * );
419         *pi64 = p_sys->i_length;
420         return VLC_SUCCESS;
421
422     case DEMUX_GET_TIME:
423         pi64 = (int64_t*)va_arg( args, int64_t * );
424         if( p_sys->i_time < 0 ) return VLC_EGENERIC;
425         *pi64 = p_sys->i_time;
426         return VLC_SUCCESS;
427
428     case DEMUX_SET_TIME:
429         if ( p_sys->p_fp &&
430              ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
431             return VLC_EGENERIC;
432
433         SeekPrepare( p_demux );
434
435         if( p_sys->b_index && p_sys->i_length > 0 )
436         {
437             va_list acpy;
438             va_copy( acpy, args );
439             i64 = (int64_t)va_arg( acpy, int64_t );
440             va_end( acpy );
441
442             if( !SeekIndex( p_demux, i64, -1 ) )
443                 return VLC_SUCCESS;
444         }
445         return SeekPercent( p_demux, i_query, args );
446
447     case DEMUX_SET_ES:
448     {
449         i = (int)va_arg( args, int );
450         int i_ret;
451         if ( i >= 0 )
452         {
453             i++; /* video/audio-es variable starts 0 */
454             msg_Dbg( p_demux, "Requesting access to enable stream %d", i );
455             i_ret = stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE, i, true );
456         }
457         else
458         {  /* i contains -1 * es_category */
459             msg_Dbg( p_demux, "Requesting access to disable stream %d", i );
460             i_ret = stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE, i, false );
461         }
462
463         if ( i_ret == VLC_SUCCESS )
464         {
465             SeekPrepare( p_demux );
466             p_sys->i_seek_track = 0;
467             WaitKeyframe( p_demux );
468         }
469         return i_ret;
470     }
471
472     case DEMUX_GET_POSITION:
473         if( p_sys->i_time < 0 ) return VLC_EGENERIC;
474         if( p_sys->i_length > 0 )
475         {
476             pf = (double*)va_arg( args, double * );
477             *pf = p_sys->i_time / (double)p_sys->i_length;
478             return VLC_SUCCESS;
479         }
480         return demux_vaControlHelper( p_demux->s,
481                                        __MIN( INT64_MAX, p_sys->i_data_begin ),
482                                        __MIN( INT64_MAX, p_sys->i_data_end ),
483                                        __MIN( INT64_MAX, p_sys->i_bitrate ),
484                                        __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
485                                        i_query, args );
486
487     case DEMUX_SET_POSITION:
488         if ( p_sys->p_fp &&
489              ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
490             return VLC_EGENERIC;
491
492         SeekPrepare( p_demux );
493
494         if( p_sys->b_index && p_sys->i_length > 0 )
495         {
496             va_list acpy;
497             va_copy( acpy, args );
498             f = (double)va_arg( acpy, double );
499             va_end( acpy );
500
501             if( !SeekIndex( p_demux, -1, f ) )
502                 return VLC_SUCCESS;
503         }
504         return SeekPercent( p_demux, i_query, args );
505
506     case DEMUX_GET_META:
507         p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
508         vlc_meta_Merge( p_meta, p_sys->meta );
509         return VLC_SUCCESS;
510
511     case DEMUX_CAN_SEEK:
512         if ( p_sys->p_fp &&
513              ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
514         {
515             bool *pb_bool = (bool*)va_arg( args, bool * );
516             *pb_bool = false;
517             return VLC_SUCCESS;
518         }
519         // ft
520
521     default:
522         return demux_vaControlHelper( p_demux->s,
523                                       __MIN( INT64_MAX, p_sys->i_data_begin ),
524                                       __MIN( INT64_MAX, p_sys->i_data_end),
525                                       __MIN( INT64_MAX, p_sys->i_bitrate ),
526                     ( p_sys->p_fp ) ? __MIN( INT_MAX, p_sys->p_fp->i_min_data_packet_size ) : 1,
527                     i_query, args );
528     }
529 }
530
531 /*****************************************************************************
532  *
533  *****************************************************************************/
534 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
535 {
536     mtime_t i_time = -1;
537     int     i;
538     /* As some tracks might have been deselected by access, the PCR might
539      * stop updating */
540     for( i = 0; i < MAX_ASF_TRACKS ; i++ )
541     {
542         const asf_track_t *tk = p_sys->track[i];
543
544         if( tk && tk->p_es && tk->b_selected )
545         {
546             /* Skip discrete tracks */
547             if ( tk->i_cat != VIDEO_ES && tk->i_cat != AUDIO_ES )
548                 continue;
549
550             /* We need to have all ES seen once, as they might have lower DTS */
551             if ( tk->i_time + (int64_t)p_sys->p_fp->i_preroll * 1000 < 0 )
552             {
553                 /* early fail */
554                 return -1;
555             }
556             else if ( tk->i_time > -1 && ( i_time == -1 || i_time > tk->i_time ) )
557             {
558                 i_time = tk->i_time;
559             }
560         }
561     }
562
563     return i_time;
564 }
565
566 static void Packet_SetAR( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
567                           uint8_t i_ratio_x, uint8_t i_ratio_y )
568 {
569     demux_t *p_demux = p_packetsys->p_demux;
570     asf_track_t *tk = p_demux->p_sys->track[i_stream_number];
571     if ( tk->p_fmt->video.i_sar_num == i_ratio_x && tk->p_fmt->video.i_sar_den == i_ratio_y )
572         return;
573
574     /* Only apply if origin pixel size >= 1x1, due to broken yacast */
575     if ( tk->p_fmt->video.i_height * i_ratio_x > tk->p_fmt->video.i_width * i_ratio_y )
576     {
577         vout_thread_t *p_vout = input_GetVout( p_demux->p_input );
578         if ( p_vout )
579         {
580             msg_Info( p_demux, "Changing aspect ratio to %i/%i", i_ratio_x, i_ratio_y );
581             vout_ChangeAspectRatio( p_vout, i_ratio_x, i_ratio_y );
582             vlc_object_release( p_vout );
583         }
584     }
585     tk->p_fmt->video.i_sar_num = i_ratio_x;
586     tk->p_fmt->video.i_sar_den = i_ratio_y;
587 }
588
589 static void Packet_UpdateTime( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
590                                mtime_t i_time )
591 {
592     asf_track_t *tk = p_packetsys->p_demux->p_sys->track[i_stream_number];
593     if ( tk )
594         tk->i_time = i_time;
595 }
596
597 static asf_track_info_t * Packet_GetTrackInfo( asf_packet_sys_t *p_packetsys,
598                                                uint8_t i_stream_number )
599 {
600     asf_track_t *tk = p_packetsys->p_demux->p_sys->track[i_stream_number];
601     if (!tk)
602         return NULL;
603     else
604         return & tk->info;
605 }
606
607 static bool Packet_DoSkip( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, bool b_packet_keyframe )
608 {
609     demux_t *p_demux = p_packetsys->p_demux;
610     demux_sys_t *p_sys = p_demux->p_sys;
611     const asf_track_t *tk = p_sys->track[i_stream_number];
612
613     if( tk == NULL )
614     {
615         msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
616         return true;
617     }
618
619     if( p_sys->i_wait_keyframe )
620     {
621         if ( i_stream_number == p_sys->i_seek_track )
622         {
623             if ( !b_packet_keyframe )
624             {
625                 p_sys->i_wait_keyframe--;
626                 return true;
627             }
628             else
629                 p_sys->i_wait_keyframe = 0;
630         }
631         else
632             return true;
633     }
634
635     if( !tk->p_es )
636         return true;
637
638     return false;
639 }
640
641 static void Packet_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame)
642 {
643     demux_t *p_demux = p_packetsys->p_demux;
644     demux_sys_t *p_sys = p_demux->p_sys;
645     const asf_track_t *tk = p_sys->track[i_stream_number];
646     if ( !tk )
647         return;
648
649     block_t *p_gather = block_ChainGather( *pp_frame );
650
651     if( p_sys->i_time < VLC_TS_0 && tk->i_time > VLC_TS_INVALID )
652     {
653         p_sys->i_time = tk->i_time;
654         es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
655 #ifdef ASF_DEBUG
656         msg_Dbg( p_demux, "    setting PCR to %"PRId64, VLC_TS_0 + p_sys->i_time );
657 #endif
658     }
659
660 #ifdef ASF_DEBUG
661     msg_Dbg( p_demux, "    sending packet dts %"PRId64" pts %"PRId64" pcr %"PRId64, p_gather->i_dts, p_gather->i_pts, p_sys->i_time );
662 #endif
663     es_out_Send( p_demux->out, tk->p_es, p_gather );
664     *pp_frame = NULL;
665 }
666
667 /*****************************************************************************
668  *
669  *****************************************************************************/
670 typedef struct asf_es_priorities_t
671 {
672     uint16_t *pi_stream_numbers;
673     uint16_t i_count;
674 } asf_es_priorities_t;
675
676 /* Fills up our exclusion list */
677 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
678                                          asf_es_priorities_t *p_prios )
679 {
680     /* Find stream exclusions */
681     asf_object_advanced_mutual_exclusion_t *p_mutex =
682             ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
683     if (! p_mutex ) return;
684
685 #if ( UINT_MAX > SIZE_MAX / 2 )
686     if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
687         return;
688 #endif
689     p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof(uint16_t) );
690     if ( !p_prios->pi_stream_numbers ) return;
691
692     if ( p_mutex->i_stream_number_count )
693     {
694         /* Just set highest prio on highest in the group */
695         for ( uint16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
696         {
697             if ( p_prios->i_count > p_sys->i_track || i > p_sys->i_track ) break;
698             p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
699         }
700     }
701 }
702
703 /* Fills up our bitrate exclusion list */
704 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
705                                                  asf_es_priorities_t *p_prios )
706 {
707     /* Find bitrate exclusions */
708     asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
709             ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
710     if (! p_bitrate_mutex ) return;
711
712 #if ( UINT_MAX > SIZE_MAX / 2 )
713     if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
714         return;
715 #endif
716     p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof( uint16_t ) );
717     if ( !p_prios->pi_stream_numbers ) return;
718
719     if ( p_bitrate_mutex->i_stream_number_count )
720     {
721         /* Just remove < highest */
722         for ( uint16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
723         {
724             if ( p_prios->i_count > p_sys->i_track || i > p_sys->i_track ) break;
725             p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
726         }
727     }
728
729 }
730
731 #define GET_CHECKED( target, getter, maxtarget, temp ) \
732 {\
733     temp i_temp = getter;\
734     if ( i_temp > maxtarget ) {\
735         msg_Warn( p_demux, "rejecting stream %u : " #target " overflow", i_stream );\
736         es_format_Clean( &fmt );\
737         goto error;\
738     } else {\
739         target = i_temp;\
740     }\
741 }
742
743 static int DemuxInit( demux_t *p_demux )
744 {
745     demux_sys_t *p_sys = p_demux->p_sys;
746
747     /* init context */
748     p_sys->i_time   = -1;
749     p_sys->i_length = 0;
750     p_sys->i_bitrate = 0;
751     p_sys->p_root   = NULL;
752     p_sys->p_fp     = NULL;
753     p_sys->b_index  = 0;
754     p_sys->i_track  = 0;
755     p_sys->i_seek_track = 0;
756     p_sys->i_wait_keyframe = 0;
757     for( int i = 0; i < MAX_ASF_TRACKS; i++ )
758     {
759         p_sys->track[i] = NULL;
760     }
761     p_sys->i_data_begin = 0;
762     p_sys->i_data_end   = 0;
763     p_sys->i_preroll_start = 0;
764     p_sys->meta         = NULL;
765
766     /* Now load all object ( except raw data ) */
767     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
768     if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, p_sys->b_canfastseek)) )
769     {
770         msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
771         return VLC_EGENERIC;
772     }
773     p_sys->p_fp = p_sys->p_root->p_fp;
774
775     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
776     {
777         msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
778         goto error;
779     }
780
781     if ( ASF_FindObject( p_sys->p_root->p_hdr,
782                          &asf_object_content_encryption_guid, 0 ) != NULL
783          || ASF_FindObject( p_sys->p_root->p_hdr,
784                             &asf_object_extended_content_encryption_guid, 0 ) != NULL
785          || ASF_FindObject( p_sys->p_root->p_hdr,
786                          &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
787     {
788         dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
789                         _("DRM protected streams are not supported.") );
790         goto error;
791     }
792
793     p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
794                                       &asf_object_stream_properties_guid );
795     if( p_sys->i_track == 0 )
796     {
797         msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
798         goto error;
799     }
800     msg_Dbg( p_demux, "found %u streams", p_sys->i_track );
801
802     /* check if index is available */
803     asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
804                                                   &asf_object_simple_index_guid, 0 );
805     const bool b_index = p_index && p_index->i_index_entry_count;
806
807     /* Find the extended header if any */
808     asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
809                                               &asf_object_header_extension_guid, 0 );
810
811     asf_object_language_list_t *p_languages = NULL;
812     asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
813     asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
814
815     if( p_hdr_ext )
816     {
817         p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
818
819         ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
820         ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
821     }
822
823     for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
824     {
825         asf_track_t    *tk;
826         asf_object_stream_properties_t *p_sp;
827         asf_object_extended_stream_properties_t *p_esp;
828         bool b_access_selected;
829
830         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
831                                &asf_object_stream_properties_guid,
832                                i_stream );
833         p_esp = NULL;
834
835         tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
836         memset( tk, 0, sizeof( asf_track_t ) );
837
838         tk->i_time = -1;
839         tk->info.p_sp = p_sp;
840         tk->p_es = NULL;
841         tk->info.p_esp = NULL;
842         tk->info.p_frame = NULL;
843
844         if ( strncmp( p_demux->psz_access, "mms", 3 ) )
845         {
846             /* Check (not mms) if this track is selected (ie will receive data) */
847             if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
848                                  (int) p_sp->i_stream_number, &b_access_selected ) &&
849                 !b_access_selected )
850             {
851                 tk->i_cat = UNKNOWN_ES;
852                 msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
853                          p_sp->i_stream_number );
854                 continue;
855             }
856         }
857
858         /* Find the associated extended_stream_properties if any */
859         if( p_hdr_ext )
860         {
861             int i_ext_stream = ASF_CountObject( p_hdr_ext,
862                                                 &asf_object_extended_stream_properties_guid );
863             for( int i = 0; i < i_ext_stream; i++ )
864             {
865                 asf_object_t *p_tmp =
866                     ASF_FindObject( p_hdr_ext,
867                                     &asf_object_extended_stream_properties_guid, i );
868                 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
869                 {
870                     p_esp = &p_tmp->ext_stream;
871                     tk->info.p_esp = p_esp;
872                     break;
873                 }
874             }
875         }
876
877         es_format_t fmt;
878
879         if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
880             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
881         {
882             uint8_t *p_data = p_sp->p_type_specific_data;
883             int i_format;
884
885             es_format_Init( &fmt, AUDIO_ES, 0 );
886             i_format = GetWLE( &p_data[0] );
887             wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
888
889             GET_CHECKED( fmt.audio.i_channels,      GetWLE( &p_data[2] ),
890                                                         255, uint16_t );
891             GET_CHECKED( fmt.audio.i_rate,          GetDWLE( &p_data[4] ),
892                                                         UINT_MAX, uint32_t );
893             GET_CHECKED( fmt.i_bitrate,             GetDWLE( &p_data[8] ) * 8,
894                                                         UINT_MAX, uint32_t );
895             fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
896             fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
897
898             if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
899                 i_format != WAVE_FORMAT_MPEGLAYER3 &&
900                 i_format != WAVE_FORMAT_MPEG )
901             {
902                 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
903                                      p_sp->i_type_specific_data_length -
904                                      sizeof( WAVEFORMATEX ) ),
905                              INT_MAX, uint32_t );
906                 fmt.p_extra = malloc( fmt.i_extra );
907                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
908                         fmt.i_extra );
909             }
910
911             msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
912                     GetWLE( p_data ), p_sp->i_stream_number );
913         }
914         else if( guidcmp( &p_sp->i_stream_type,
915                               &asf_object_stream_type_video ) &&
916                  p_sp->i_type_specific_data_length >= 11 +
917                  sizeof( VLC_BITMAPINFOHEADER ) )
918         {
919             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
920
921             es_format_Init( &fmt, VIDEO_ES,
922                             VLC_FOURCC( p_data[16], p_data[17],
923                                         p_data[18], p_data[19] ) );
924
925             GET_CHECKED( fmt.video.i_width,      GetDWLE( p_data + 4 ),
926                                                      UINT_MAX, uint32_t );
927             GET_CHECKED( fmt.video.i_height,     GetDWLE( p_data + 8 ),
928                                                      UINT_MAX, uint32_t );
929
930             if( p_esp && p_esp->i_average_time_per_frame > 0 )
931             {
932                 fmt.video.i_frame_rate = 10000000;
933                 GET_CHECKED( fmt.video.i_frame_rate_base,
934                              p_esp->i_average_time_per_frame,
935                              UINT_MAX, uint64_t );
936             }
937
938             if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
939             {
940                 /* DVR-MS special ASF */
941                 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
942                 fmt.b_packetized = false;
943             }
944
945             if( p_sp->i_type_specific_data_length > 11 +
946                 sizeof( VLC_BITMAPINFOHEADER ) )
947             {
948                 GET_CHECKED( fmt.i_extra, __MIN( GetDWLE( p_data ),
949                                      p_sp->i_type_specific_data_length - 11 -
950                                      sizeof( VLC_BITMAPINFOHEADER ) ),
951                              UINT_MAX, uint32_t );
952                 fmt.p_extra = malloc( fmt.i_extra );
953                 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
954                         fmt.i_extra );
955             }
956
957             /* Look for an aspect ratio */
958             if( p_sys->p_root->p_metadata )
959             {
960                 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
961                 unsigned int i_aspect_x = 0, i_aspect_y = 0;
962                 uint32_t i;
963                 for( i = 0; i < p_meta->i_record_entries_count; i++ )
964                 {
965                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
966                     {
967                         if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
968                             p_meta->record[i].i_stream ==
969                             p_sp->i_stream_number )
970                             GET_CHECKED( i_aspect_x, p_meta->record[i].i_val,
971                                          UINT_MAX, uint64_t );
972                     }
973                     if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
974                     {
975                         if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
976                             p_meta->record[i].i_stream ==
977                             p_sp->i_stream_number )
978                             GET_CHECKED( i_aspect_y, p_meta->record[i].i_val,
979                                          UINT_MAX, uint64_t );
980                     }
981                 }
982
983                 if( i_aspect_x && i_aspect_y )
984                 {
985                     fmt.video.i_sar_num = i_aspect_x;
986                     fmt.video.i_sar_den = i_aspect_y;
987                 }
988             }
989
990             /* If there is a video track then use the index for seeking */
991             p_sys->b_index = b_index;
992
993             msg_Dbg( p_demux, "added new video stream(ID:%d)",
994                      p_sp->i_stream_number );
995         }
996         else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
997             p_sp->i_type_specific_data_length >= 64 )
998         {
999             /* Now follows a 64 byte header of which we don't know much */
1000             guid_t  *p_ref  = (guid_t *)p_sp->p_type_specific_data;
1001             uint8_t *p_data = p_sp->p_type_specific_data + 64;
1002             unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1003
1004             msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1005             if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1006                 i_data >= sizeof( WAVEFORMATEX ) - 2)
1007             {
1008                 uint16_t i_format;
1009                 es_format_Init( &fmt, AUDIO_ES, 0 );
1010                 i_format = GetWLE( &p_data[0] );
1011                 if( i_format == 0 )
1012                     fmt.i_codec = VLC_CODEC_A52;
1013                 else
1014                     wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1015                 GET_CHECKED( fmt.audio.i_channels,      GetWLE( &p_data[2] ),
1016                                                             255, uint16_t );
1017                 GET_CHECKED( fmt.audio.i_rate,          GetDWLE( &p_data[4] ),
1018                                                             UINT_MAX, uint32_t );
1019                 GET_CHECKED( fmt.i_bitrate,             GetDWLE( &p_data[8] ) * 8,
1020                                                             UINT_MAX, uint32_t );
1021                 fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
1022                 fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
1023                 fmt.b_packetized = true;
1024
1025                 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1026                     i_format != WAVE_FORMAT_MPEGLAYER3 &&
1027                     i_format != WAVE_FORMAT_MPEG && i_data >= 19 )
1028                 {
1029                     GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1030                                          p_sp->i_type_specific_data_length -
1031                                          sizeof( WAVEFORMATEX ) ),
1032                                  INT_MAX, uint32_t );
1033                     fmt.p_extra = malloc( fmt.i_extra );
1034                     memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1035                         fmt.i_extra );
1036                 }
1037
1038                 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1039                     i_format, p_sp->i_stream_number );
1040             }
1041             else
1042             {
1043                 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1044             }
1045         }
1046         else
1047         {
1048             es_format_Init( &fmt, UNKNOWN_ES, 0 );
1049         }
1050
1051         tk->i_cat = fmt.i_cat;
1052         if( fmt.i_cat != UNKNOWN_ES )
1053         {
1054             if( p_esp && p_languages &&
1055                 p_esp->i_language_index < p_languages->i_language )
1056             {
1057                 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1058                 char *p;
1059                 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1060                     *p = '\0';
1061             }
1062
1063             /* Set our priority so we won't get multiple videos */
1064             int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1065             for( uint16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1066             {
1067                 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1068                 {
1069                     i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1070                     break;
1071                 }
1072             }
1073             for( uint16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1074             {
1075                 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1076                 {
1077                     i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1078                     break;
1079                 }
1080             }
1081             fmt.i_priority = i_priority;
1082
1083             if ( i_stream <= INT_MAX )
1084                 fmt.i_id = i_stream;
1085             else
1086                 msg_Warn( p_demux, "Can't set fmt.i_id to match stream id %u", i_stream );
1087
1088             if ( fmt.i_cat == VIDEO_ES )
1089             {
1090                 /* Backup our video format */
1091                 tk->p_fmt = malloc( sizeof( es_format_t ) );
1092                 if ( tk->p_fmt )
1093                     es_format_Copy( tk->p_fmt, &fmt );
1094             }
1095
1096             tk->p_es = es_out_Add( p_demux->out, &fmt );
1097
1098             if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1099                                  (int) p_sp->i_stream_number, &b_access_selected ) &&
1100                 b_access_selected )
1101             {
1102                 p_sys->i_access_selected_track[fmt.i_cat] = p_sp->i_stream_number;
1103             }
1104
1105         }
1106         else
1107         {
1108             msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1109                      p_sp->i_stream_number );
1110         }
1111
1112         es_format_Clean( &fmt );
1113     }
1114
1115     free( fmt_priorities_ex.pi_stream_numbers );
1116     free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1117
1118     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1119     if( p_sys->p_root->p_data->i_object_size != 0 )
1120     { /* local file */
1121         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1122                                     p_sys->p_root->p_data->i_object_size;
1123         p_sys->i_data_end = __MIN( (uint64_t)stream_Size( p_demux->s ), p_sys->i_data_end );
1124     }
1125     else
1126     { /* live/broacast */
1127         p_sys->i_data_end = 0;
1128     }
1129
1130     /* go to first packet */
1131     stream_Seek( p_demux->s, p_sys->i_data_begin );
1132
1133     /* try to calculate movie time */
1134     if( p_sys->p_fp->i_data_packets_count > 0 )
1135     {
1136         uint64_t i_count;
1137         uint64_t i_size = stream_Size( p_demux->s );
1138
1139         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1140         {
1141             i_size = p_sys->i_data_end;
1142         }
1143
1144         /* real number of packets */
1145         i_count = ( i_size - p_sys->i_data_begin ) /
1146                   p_sys->p_fp->i_min_data_packet_size;
1147
1148         /* calculate the time duration in micro-s */
1149         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1150                    (mtime_t)i_count /
1151                    (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1152         if( p_sys->i_length < 0 )
1153             p_sys->i_length = 0;
1154
1155         if( p_sys->i_length > 0 )
1156         {
1157             p_sys->i_bitrate = 8 * i_size * 1000000 / p_sys->i_length;
1158         }
1159     }
1160
1161     /* Create meta information */
1162     p_sys->meta = vlc_meta_New();
1163
1164     asf_object_content_description_t *p_cd;
1165     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1166                                  &asf_object_content_description_guid, 0 ) ) )
1167     {
1168         if( p_cd->psz_title && *p_cd->psz_title )
1169         {
1170             vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1171         }
1172         if( p_cd->psz_artist && *p_cd->psz_artist )
1173         {
1174              vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1175         }
1176         if( p_cd->psz_copyright && *p_cd->psz_copyright )
1177         {
1178             vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1179         }
1180         if( p_cd->psz_description && *p_cd->psz_description )
1181         {
1182             vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1183         }
1184         if( p_cd->psz_rating && *p_cd->psz_rating )
1185         {
1186             vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1187         }
1188     }
1189     /// \tood Fix Child meta for ASF tracks
1190 #if 0
1191     for( i_stream = 0, i = 0; i < MAX_ASF_TRACKS; i++ )
1192     {
1193         asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1194                                                         &asf_object_codec_list_guid, 0 );
1195
1196         if( p_sys->track[i] )
1197         {
1198             vlc_meta_t *tk = vlc_meta_New();
1199             TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1200
1201             if( p_cl && i_stream < p_cl->i_codec_entries_count )
1202             {
1203                 if( p_cl->codec[i_stream].psz_name &&
1204                     *p_cl->codec[i_stream].psz_name )
1205                 {
1206                     vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1207                                   p_cl->codec[i_stream].psz_name );
1208                 }
1209                 if( p_cl->codec[i_stream].psz_description &&
1210                     *p_cl->codec[i_stream].psz_description )
1211                 {
1212                     vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1213                                   p_cl->codec[i_stream].psz_description );
1214                 }
1215             }
1216             i_stream++;
1217         }
1218     }
1219 #endif
1220
1221     p_sys->packet_sys.pi_preroll = &p_sys->p_fp->i_preroll;
1222     p_sys->packet_sys.pi_preroll_start = &p_sys->i_preroll_start;
1223
1224     return VLC_SUCCESS;
1225
1226 error:
1227     ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1228     return VLC_EGENERIC;
1229 }
1230
1231 /*****************************************************************************
1232  * FlushRemainingPackets: flushes tail packets
1233  *****************************************************************************/
1234
1235 static void FlushRemainingPackets( demux_t *p_demux )
1236 {
1237     demux_sys_t *p_sys = p_demux->p_sys;
1238     for ( unsigned int i = 0; i < MAX_ASF_TRACKS; i++ )
1239     {
1240         asf_track_t *tk = p_sys->track[i];
1241         if( tk && tk->info.p_frame )
1242             Packet_Send( &p_sys->packet_sys, i, &tk->info.p_frame );
1243     }
1244 }
1245
1246 /*****************************************************************************
1247  *
1248  *****************************************************************************/
1249 static void DemuxEnd( demux_t *p_demux )
1250 {
1251     demux_sys_t *p_sys = p_demux->p_sys;
1252
1253     if( p_sys->p_root )
1254     {
1255         ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1256         p_sys->p_root = NULL;
1257     }
1258     if( p_sys->meta )
1259     {
1260         vlc_meta_Delete( p_sys->meta );
1261         p_sys->meta = NULL;
1262     }
1263
1264     for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1265     {
1266         asf_track_t *tk = p_sys->track[i];
1267
1268         if( tk )
1269         {
1270             if( tk->info.p_frame )
1271                 block_ChainRelease( tk->info.p_frame );
1272
1273             if( tk->p_es )
1274             {
1275                 es_out_Del( p_demux->out, tk->p_es );
1276             }
1277             if ( tk->p_fmt )
1278             {
1279                 es_format_Clean( tk->p_fmt );
1280                 free( tk->p_fmt );
1281             }
1282             free( tk );
1283         }
1284         p_sys->track[i] = 0;
1285     }
1286 }
1287