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