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