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