Coverage Report

Created: 2026-12-01 08:07

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