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