Coverage Report

Created: 2025-11-14 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/mpeg/es.c
Line
Count
Source
1
/*****************************************************************************
2
 * es.c : Generic audio ES input module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2001-2008 VLC authors and VideoLAN
5
 *               2022 VideoLabs
6
 *
7
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9
 *
10
 * This program is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU Lesser General Public License as published by
12
 * the Free Software Foundation; either version 2.1 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with this program; if not, write to the Free Software Foundation,
22
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24
25
/*****************************************************************************
26
 * Preamble
27
 *****************************************************************************/
28
29
#ifdef HAVE_CONFIG_H
30
# include "config.h"
31
#endif
32
33
#include <math.h>
34
35
#include <vlc_common.h>
36
#include <vlc_arrays.h>
37
#include <vlc_plugin.h>
38
#include <vlc_demux.h>
39
#include <vlc_codec.h>
40
#include <vlc_codecs.h>
41
#include <vlc_input.h>
42
#include <vlc_replay_gain.h>
43
44
#include "../../packetizer/a52.h"
45
#include "../../packetizer/dts_header.h"
46
#include "../../packetizer/mpegaudio.h"
47
#include "../../meta_engine/ID3Tag.h"
48
#include "../../meta_engine/ID3Text.h"
49
#include "../../meta_engine/ID3Meta.h"
50
51
/*****************************************************************************
52
 * Module descriptor
53
 *****************************************************************************/
54
static int  OpenAudio( vlc_object_t * );
55
static int  OpenVideo( vlc_object_t * );
56
static void Close    ( vlc_object_t * );
57
58
#define FPS_TEXT N_("Frames per Second")
59
#define FPS_LONGTEXT N_("This is the frame rate used as a fallback when " \
60
    "playing MPEG video elementary streams.")
61
62
108
vlc_module_begin ()
63
54
    set_subcategory( SUBCAT_INPUT_DEMUX )
64
54
    set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
65
54
    set_shortname( N_("Audio ES") )
66
54
    set_capability( "demux", 155 )
67
108
    set_callbacks( OpenAudio, Close )
68
69
54
    add_shortcut( "mpga", "mp3",
70
54
                  "m4a", "mp4a", "aac",
71
54
                  "ac3", "a52",
72
54
                  "eac3",
73
54
                  "dts",
74
54
                  "mlp", "thd" )
75
76
54
    add_submodule()
77
54
    set_description( N_("MPEG-4 video" ) )
78
54
    set_capability( "demux", 7 )
79
108
    set_callbacks( OpenVideo, Close )
80
54
    add_float( "es-fps", 25, FPS_TEXT, FPS_LONGTEXT )
81
82
54
    add_shortcut( "m4v" )
83
54
    add_shortcut( "mp4v" )
84
54
vlc_module_end ()
85
86
/*****************************************************************************
87
 * Local prototypes
88
 *****************************************************************************/
89
static int Demux  ( demux_t * );
90
static int Control( demux_t *, int, va_list );
91
92
1.49M
#define WAV_PROBE_SIZE (512*1024)
93
1.83k
#define BASE_PROBE_SIZE (8000)
94
11.1k
#define WAV_EXTRA_PROBE_SIZE (44000/2*2*2)
95
96
typedef struct
97
{
98
    vlc_fourcc_t i_codec;
99
    bool       b_use_word;
100
    const char *psz_name;
101
    int  (*pf_probe)( demux_t *p_demux, uint64_t *pi_offset );
102
    int  (*pf_init)( demux_t *p_demux );
103
} codec_t;
104
105
typedef struct
106
{
107
    vlc_tick_t i_time;
108
    uint64_t i_pos;
109
    bs_t br;
110
} sync_table_ctx_t;
111
112
typedef struct
113
{
114
    uint16_t i_frames_btw_refs;
115
    uint32_t i_bytes_btw_refs;
116
    uint32_t i_ms_btw_refs;
117
    uint8_t i_bits_per_bytes_dev;
118
    uint8_t i_bits_per_ms_dev;
119
    uint8_t *p_bits;
120
    size_t i_bits;
121
    sync_table_ctx_t current;
122
} sync_table_t;
123
124
typedef struct
125
{
126
    uint32_t i_offset;
127
    seekpoint_t *p_seekpoint;
128
} chap_entry_t;
129
130
/* Mpga specific */
131
107
#define XING_FIELD_STREAMFRAMES    (1 << 0)
132
107
#define XING_FIELD_STREAMBYTES     (1 << 1)
133
107
#define XING_FIELD_TOC             (1 << 2)
134
107
#define XING_FIELD_QUALITY         (1 << 3)
135
1.05k
#define XING_MIN_TAG_SIZE           42
136
100
#define XING_TOC_COUNTBYTES         100
137
#define XING_MAX_TAG_SIZE          (XING_MIN_TAG_SIZE + 4 + 4 + XING_TOC_COUNTBYTES + 4 + 2)
138
#define XING_SUB_ETE_NOK_OTAE       XING_MIN_TAG_SIZE
139
140
struct xing_info_s
141
{
142
    uint32_t i_frames;
143
    uint32_t i_bytes;
144
    uint32_t i_quality;
145
    vlc_fourcc_t infotag;
146
    vlc_fourcc_t encoder;
147
    uint8_t  rgi_toc[XING_TOC_COUNTBYTES];
148
    float f_peak_signal;
149
    float f_radio_replay_gain;
150
    float f_audiophile_replay_gain;
151
    enum
152
    {
153
        XING_MODE_UNKNOWN   = 0,
154
        XING_MODE_CBR       = 1,
155
        XING_MODE_ABR       = 2,
156
        XING_MODE_VBR1      = 3,
157
        XING_MODE_VBR2      = 4,
158
        XING_MODE_VBR3      = 5,
159
        XING_MODE_VBR4      = 6,
160
        XING_MODE_CBR_2PASS = 8,
161
        XING_MODE_ABR_2PASS = 9,
162
    } brmode;
163
    enum
164
    {
165
        XING_AUDIOMODE_UNKNOWN    = 0,
166
        XING_AUDIOMODE_DPL        = 1,
167
        XING_AUDIOMODE_DPL2       = 2,
168
        XING_AUDIOMODE_AMBISONICS = 3,
169
    } audiomode;
170
    uint8_t  bitrate_avg;
171
    uint16_t i_delay_samples;
172
    uint16_t i_padding_samples;
173
    uint32_t i_music_length;
174
};
175
176
static int ParseXing( const uint8_t *p_buf, size_t i_buf, struct xing_info_s *xing )
177
946
{
178
946
    if( i_buf < XING_MIN_TAG_SIZE )
179
0
        return VLC_EGENERIC;
180
181
946
    vlc_fourcc_t infotag = VLC_FOURCC(p_buf[0], p_buf[1], p_buf[2], p_buf[3]);
182
183
    /* L3Enc VBR info */
184
946
    if( infotag == VLC_FOURCC('V','B','R','I') )
185
24
    {
186
24
        if( GetWBE( &p_buf[4] ) != 0x0001 )
187
13
            return VLC_EGENERIC;
188
11
        xing->i_bytes = GetDWBE( &p_buf[10] );
189
11
        xing->i_frames = GetDWBE( &p_buf[14] );
190
11
        xing->infotag = infotag;
191
11
        return VLC_SUCCESS;
192
24
    }
193
    /* Xing VBR/CBR tags */
194
922
    else if( infotag != VLC_FOURCC('X','i','n','g') &&
195
876
             infotag != VLC_FOURCC('I','n','f','o') )
196
815
    {
197
815
        return VLC_EGENERIC;
198
815
    }
199
200
107
    xing->infotag = infotag;
201
202
107
    const uint32_t i_flags = GetDWBE( &p_buf[4] );
203
    /* compute our variable struct size for early checks */
204
107
    const unsigned varsz[4] = { ((i_flags & 0x01) ? 4 : 0),
205
107
                                ((i_flags & 0x02) ? 4 : 0),
206
107
                                ((i_flags & 0x04) ? XING_TOC_COUNTBYTES : 0),
207
107
                                ((i_flags & 0x08) ? 4 : 0) };
208
107
    const unsigned i_varallsz = varsz[0] + varsz[1] + varsz[2] + varsz[3];
209
107
    const unsigned i_tag_total = XING_MIN_TAG_SIZE + i_varallsz;
210
211
107
    if( i_buf < i_tag_total )
212
0
        return VLC_EGENERIC;
213
214
107
    if( i_flags & XING_FIELD_STREAMFRAMES )
215
91
        xing->i_frames = GetDWBE( &p_buf[8] );
216
107
    if( i_flags & XING_FIELD_STREAMBYTES )
217
95
        xing->i_bytes = GetDWBE( &p_buf[8 + varsz[0]] );
218
107
    if( i_flags & XING_FIELD_TOC )
219
50
        memcpy( xing->rgi_toc, &p_buf[8 + varsz[0] + varsz[1]], XING_TOC_COUNTBYTES );
220
107
    if( i_flags & XING_FIELD_QUALITY )
221
63
        xing->i_quality = GetDWBE( &p_buf[8 + varsz[0] + varsz[1] + varsz[2]] );
222
223
    /* pointer past optional members */
224
107
    const uint8_t *p_fixed = &p_buf[8 + i_varallsz];
225
226
    /* Original Xing encoder header stops here */
227
228
107
    xing->encoder = VLC_FOURCC(p_fixed[0], p_fixed[1], p_fixed[2], p_fixed[3]); /* char version[9] start */
229
230
107
    if( xing->encoder != VLC_FOURCC('L','A','M','E') &&
231
106
        xing->encoder != VLC_FOURCC('L','a','v','c') &&
232
106
        xing->encoder != VLC_FOURCC('L','a','v','f') )
233
101
        return VLC_SUCCESS;
234
235
6
    xing->brmode  = p_fixed[9] & 0x0f; /* version upper / mode lower */
236
6
    uint32_t peak_signal  = GetDWBE( &p_fixed[11] );
237
6
    xing->f_peak_signal = peak_signal / 8388608.0; /* pow(2, 23) */
238
6
    uint16_t gain = GetWBE( &p_fixed[15] );
239
6
    xing->f_radio_replay_gain = (gain & 0x1FF) / /* 9bits val stored x10 */
240
6
                                ((gain & 0x200) ? -10.0 : 10.0); /* -sign bit on bit 6 */
241
6
    gain = GetWBE( &p_fixed[17] );
242
6
    xing->f_radio_replay_gain = (gain & 0x1FF) / /* 9bits val stored x10 */
243
6
                                ((gain & 0x200) ? -10.0 : 10.0); /* -sign bit on bit 6 */
244
    /* flags @19 */
245
6
    xing->bitrate_avg = (p_fixed[20] != 0xFF) ? p_fixed[20] : 0; /* clipped to 255, so it's unknown from there */
246
6
    xing->i_delay_samples = (p_fixed[21] << 4) | (p_fixed[22] >> 4); /* upper 12bits */
247
6
    xing->i_padding_samples = ((p_fixed[22] & 0x0F) << 8) | p_fixed[23]; /* lower 12bits */
248
6
    xing->audiomode = (p_fixed[26] >> 3) & 0x07; /* over 16bits, 2b unused / 3b mode / 11b preset */
249
6
    xing->i_music_length  = GetDWBE( &p_fixed[28] );
250
251
6
    return VLC_SUCCESS;
252
107
}
253
254
static int MpgaGetCBRSeekpoint( const struct mpga_frameheader_s *mpgah,
255
                                const struct xing_info_s *xing,
256
                                uint64_t i_seekablesize,
257
                                double f_pos, vlc_tick_t *pi_time, uint64_t *pi_offset )
258
0
{
259
0
    if( xing->infotag != 0 &&
260
0
        xing->infotag != VLC_FOURCC('I','n','f','o') &&
261
0
        xing->brmode != XING_MODE_CBR &&
262
0
        xing->brmode != XING_MODE_CBR_2PASS )
263
0
        return -1;
264
265
0
    if( !mpgah->i_sample_rate || !mpgah->i_samples_per_frame || !mpgah->i_frame_size )
266
0
        return -1;
267
268
0
    uint32_t i_total_frames = xing->i_frames ? xing->i_frames
269
0
                                             : i_seekablesize / mpgah->i_frame_size;
270
271
    /* xing must be optional here */
272
0
    uint32_t i_frame = i_total_frames * f_pos;
273
0
    *pi_offset = i_frame * mpgah->i_frame_size;
274
0
    *pi_time = vlc_tick_from_samples( i_frame * mpgah->i_samples_per_frame,
275
0
                                      mpgah->i_sample_rate ) + VLC_TICK_0;
276
277
0
    return 0;
278
0
}
279
280
static int SeekByPosUsingXingTOC( const struct mpga_frameheader_s *mpgah,
281
                                  const struct xing_info_s *xing,double pos,
282
                                  vlc_tick_t *pi_time, uint64_t *pi_offset )
283
0
{
284
0
    if( !xing->rgi_toc[XING_TOC_COUNTBYTES - 1] ||
285
0
        !mpgah->i_sample_rate || !mpgah->i_samples_per_frame )
286
0
        return -1;
287
288
0
    unsigned syncentry = pos * XING_TOC_COUNTBYTES;
289
0
    syncentry = VLC_CLIP(syncentry, 0, XING_TOC_COUNTBYTES - 1);
290
0
    unsigned syncframe = syncentry * xing->i_frames / XING_TOC_COUNTBYTES;
291
292
0
    *pi_time = vlc_tick_from_samples( syncframe * mpgah->i_samples_per_frame,
293
0
                                      mpgah->i_sample_rate ) + VLC_TICK_0;
294
0
    *pi_offset = xing->rgi_toc[syncentry] * xing->i_bytes / 256;
295
296
0
    return 0;
297
0
}
298
299
static int SeekByTimeUsingXingTOC( const struct mpga_frameheader_s *mpgah,
300
                                   const struct xing_info_s *xing,
301
                                   vlc_tick_t *pi_time, uint64_t *pi_offset )
302
0
{
303
0
    if( !mpgah->i_sample_rate || !mpgah->i_samples_per_frame || !xing->i_frames ||
304
0
        *pi_time < VLC_TICK_0 )
305
0
        return -1;
306
307
0
    uint32_t sample = samples_from_vlc_tick( *pi_time - VLC_TICK_0, mpgah->i_sample_rate );
308
0
    uint64_t totalsamples = mpgah->i_samples_per_frame * (uint64_t) xing->i_frames;
309
310
0
    return SeekByPosUsingXingTOC( mpgah, xing, (double)sample / totalsamples,
311
0
                                  pi_time, pi_offset );
312
0
}
313
314
static int MpgaSeek( const struct mpga_frameheader_s *mpgah,
315
                     const struct xing_info_s *xing,
316
                     uint64_t i_seekablesize, double f_pos,
317
                     vlc_tick_t *pi_time, uint64_t *pi_offset )
318
0
{
319
0
    if( f_pos >= 0 ) /* Set Pos */
320
0
    {
321
0
        if( !MpgaGetCBRSeekpoint( mpgah, xing, i_seekablesize,
322
0
                                  f_pos, pi_time, pi_offset ) )
323
0
        {
324
0
             return 0;
325
0
        }
326
0
    }
327
328
0
    if( *pi_time != VLC_TICK_INVALID &&
329
0
        !SeekByTimeUsingXingTOC( mpgah, xing, pi_time, pi_offset ) )
330
0
        return 0;
331
332
0
    return SeekByPosUsingXingTOC( mpgah, xing, f_pos, pi_time, pi_offset );
333
0
}
334
335
typedef struct
336
{
337
    codec_t codec;
338
    vlc_fourcc_t i_original;
339
340
    es_out_id_t *p_es;
341
342
    bool  b_start;
343
    decoder_t   *p_packetizer;
344
    block_t     *p_packetized_data;
345
346
    vlc_tick_t  i_pts;
347
    vlc_tick_t  i_time_offset;
348
    uint64_t    i_bytes;
349
350
    bool        b_big_endian;
351
    bool        b_estimate_bitrate;
352
    unsigned    i_bitrate;  /* extracted from Xing header */
353
    vlc_tick_t  i_duration;
354
355
    bool b_initial_sync_failed;
356
357
    unsigned i_packet_size;
358
359
    uint64_t i_stream_offset;
360
    unsigned i_demux_flags;
361
362
    float   f_fps;
363
364
    /* Mpga specific */
365
    struct mpga_frameheader_s mpgah;
366
    struct xing_info_s xing;
367
368
    audio_replay_gain_t audio_replay_gain;
369
370
    sync_table_t mllt;
371
    struct
372
    {
373
        size_t i_count;
374
        size_t i_current;
375
        chap_entry_t *p_entry;
376
    } chapters;
377
} demux_sys_t;
378
379
static int MpgaProbe( demux_t *p_demux, uint64_t *pi_offset );
380
static int MpgaInit( demux_t *p_demux );
381
382
static int AacProbe( demux_t *p_demux, uint64_t *pi_offset );
383
static int AacInit( demux_t *p_demux );
384
385
static int EA52Probe( demux_t *p_demux, uint64_t *pi_offset );
386
static int A52Probe( demux_t *p_demux, uint64_t *pi_offset );
387
static int A52Init( demux_t *p_demux );
388
389
static int DtsProbe( demux_t *p_demux, uint64_t *pi_offset );
390
static int DtsInit( demux_t *p_demux );
391
392
static int MlpProbe( demux_t *p_demux, uint64_t *pi_offset );
393
static int ThdProbe( demux_t *p_demux, uint64_t *pi_offset );
394
static int MlpInit( demux_t *p_demux );
395
396
static bool Parse( demux_t *p_demux, block_t **pp_output );
397
static int SeekByMlltTable( sync_table_t *, vlc_tick_t *, uint64_t * );
398
399
static const codec_t p_codecs[] = {
400
    { VLC_CODEC_MP4A, false, "mp4 audio",  AacProbe,  AacInit },
401
    { VLC_CODEC_MPGA, false, "mpeg audio", MpgaProbe, MpgaInit },
402
    { VLC_CODEC_A52, true,  "a52 audio",  A52Probe,  A52Init },
403
    { VLC_CODEC_EAC3, true,  "eac3 audio", EA52Probe, A52Init },
404
    { VLC_CODEC_DTS, false, "dts audio",  DtsProbe,  DtsInit },
405
    { VLC_CODEC_MLP, false, "mlp audio",  MlpProbe,  MlpInit },
406
    { VLC_CODEC_TRUEHD, false, "TrueHD audio",  ThdProbe,  MlpInit },
407
408
    { 0, false, NULL, NULL, NULL }
409
};
410
411
static int VideoInit( demux_t *p_demux );
412
413
static const codec_t codec_m4v = {
414
    VLC_CODEC_MP4V, false, "mp4 video", NULL,  VideoInit
415
};
416
417
/*****************************************************************************
418
 * OpenCommon: initializes demux structures
419
 *****************************************************************************/
420
static int OpenCommon( demux_t *p_demux,
421
                       int i_cat, const codec_t *p_codec, uint64_t i_bs_offset )
422
4.11k
{
423
4.11k
    demux_sys_t *p_sys;
424
425
4.11k
    es_format_t fmt;
426
427
4.11k
    DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
428
4.11k
    memset( p_sys, 0, sizeof( demux_sys_t ) );
429
4.11k
    p_sys->codec = *p_codec;
430
4.11k
    p_sys->p_es = NULL;
431
4.11k
    p_sys->b_start = true;
432
4.11k
    p_sys->i_stream_offset = i_bs_offset;
433
4.11k
    p_sys->b_estimate_bitrate = true;
434
4.11k
    p_sys->i_bitrate = 0;
435
4.11k
    p_sys->i_duration = 0;
436
4.11k
    p_sys->b_big_endian = false;
437
4.11k
    p_sys->f_fps = var_InheritFloat( p_demux, "es-fps" );
438
4.11k
    p_sys->p_packetized_data = NULL;
439
4.11k
    p_sys->chapters.i_current = 0;
440
4.11k
    p_sys->xing.f_peak_signal = NAN;
441
4.11k
    p_sys->xing.f_radio_replay_gain = NAN;
442
4.11k
    p_sys->xing.f_audiophile_replay_gain = NAN;
443
4.11k
    TAB_INIT(p_sys->chapters.i_count, p_sys->chapters.p_entry);
444
445
4.11k
    if( vlc_stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
446
0
    {
447
0
        free( p_sys );
448
0
        return VLC_EGENERIC;
449
0
    }
450
451
4.11k
    if( p_sys->codec.pf_init( p_demux ) )
452
0
    {
453
0
        free( p_sys );
454
0
        return VLC_EGENERIC;
455
0
    }
456
457
4.11k
    if( vlc_stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
458
0
    {
459
0
        free( p_sys );
460
0
        return VLC_EGENERIC;
461
0
    }
462
463
4.11k
    msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
464
465
    /* Load the audio packetizer */
466
4.11k
    es_format_Init( &fmt, i_cat, p_sys->codec.i_codec );
467
4.11k
    fmt.i_original_fourcc = p_sys->i_original;
468
4.11k
    p_sys->p_packetizer = demux_PacketizerNew( VLC_OBJECT(p_demux), &fmt, p_sys->codec.psz_name );
469
4.11k
    if( !p_sys->p_packetizer )
470
0
    {
471
0
        free( p_sys );
472
0
        return VLC_EGENERIC;
473
0
    }
474
475
4.11k
    es_format_t *p_fmt = &p_sys->p_packetizer->fmt_out;
476
4.11k
    replay_gain_Merge( &p_fmt->audio_replay_gain, &p_sys->audio_replay_gain );
477
478
4.11k
    for( ;; )
479
77.9k
    {
480
77.9k
        if( Parse( p_demux, &p_sys->p_packetized_data ) )
481
1.58k
            break;
482
76.3k
        if( p_sys->p_packetized_data )
483
2.52k
            break;
484
76.3k
    }
485
486
4.11k
    return VLC_SUCCESS;
487
4.11k
}
488
static int OpenAudio( vlc_object_t *p_this )
489
4.62k
{
490
4.62k
    demux_t *p_demux = (demux_t*)p_this;
491
20.9k
    for( int i = 0; p_codecs[i].i_codec != 0; i++ )
492
20.4k
    {
493
20.4k
        uint64_t i_offset;
494
20.4k
        if( !p_codecs[i].pf_probe( p_demux, &i_offset ) )
495
4.11k
            return OpenCommon( p_demux, AUDIO_ES, &p_codecs[i], i_offset );
496
20.4k
    }
497
515
    return VLC_EGENERIC;
498
4.62k
}
499
static int OpenVideo( vlc_object_t *p_this )
500
176
{
501
176
    demux_t *p_demux = (demux_t*)p_this;
502
503
    /* Only m4v is supported for the moment */
504
176
    bool b_m4v_ext    = demux_IsPathExtension( p_demux, ".m4v" );
505
176
    bool b_re4_ext    = !b_m4v_ext && demux_IsPathExtension( p_demux, ".re4" );
506
176
    bool b_m4v_forced = demux_IsForced( p_demux, "m4v" ) ||
507
176
                        demux_IsForced( p_demux, "mp4v" );
508
509
176
    if( !b_m4v_ext && !b_m4v_forced && !b_re4_ext )
510
176
        return VLC_EGENERIC;
511
512
0
    ssize_t i_off = b_re4_ext ? 220 : 0;
513
0
    const uint8_t *p_peek;
514
0
    if( vlc_stream_Peek( p_demux->s, &p_peek, i_off + 4 ) < i_off + 4 )
515
0
        return VLC_EGENERIC;
516
0
    if( p_peek[i_off + 0] != 0x00 || p_peek[i_off + 1] != 0x00 || p_peek[i_off + 2] != 0x01 )
517
0
    {
518
0
        if( !b_m4v_forced)
519
0
            return VLC_EGENERIC;
520
0
        msg_Warn( p_demux,
521
0
                  "this doesn't look like an MPEG ES stream, continuing anyway" );
522
0
    }
523
0
    return OpenCommon( p_demux, VIDEO_ES, &codec_m4v, i_off );
524
0
}
525
526
static void IncreaseChapter( demux_t *p_demux, vlc_tick_t i_time )
527
63.0k
{
528
63.0k
    demux_sys_t *p_sys = p_demux->p_sys;
529
63.0k
    while( p_sys->chapters.i_current + 1 < p_sys->chapters.i_count )
530
0
    {
531
0
        const chap_entry_t *p = &p_sys->chapters.p_entry[p_sys->chapters.i_current + 1];
532
0
        if( (p->i_offset != UINT32_MAX && vlc_stream_Tell( p_demux->s ) < p->i_offset) ||
533
0
            (i_time == VLC_TICK_INVALID || p->p_seekpoint->i_time_offset + VLC_TICK_0 > i_time) )
534
0
            break;
535
0
        ++p_sys->chapters.i_current;
536
0
        p_sys->i_demux_flags |= INPUT_UPDATE_SEEKPOINT;
537
0
    }
538
63.0k
}
539
540
/*****************************************************************************
541
 * Demux: reads and demuxes data packets
542
 *****************************************************************************
543
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
544
 *****************************************************************************/
545
static int Demux( demux_t *p_demux )
546
63.0k
{
547
63.0k
    int ret = 1;
548
63.0k
    demux_sys_t *p_sys = p_demux->p_sys;
549
550
63.0k
    block_t *p_block_out = p_sys->p_packetized_data;
551
63.0k
    if( p_block_out )
552
2.76k
        p_sys->p_packetized_data = NULL;
553
60.2k
    else
554
60.2k
        ret = Parse( p_demux, &p_block_out ) ? 0 : 1;
555
556
    /* Update chapter if any */
557
63.0k
    IncreaseChapter( p_demux,
558
63.0k
                     p_block_out ? p_sys->i_time_offset + p_block_out->i_dts
559
63.0k
                                 : VLC_TICK_INVALID );
560
561
1.50M
    while( p_block_out )
562
1.44M
    {
563
1.44M
        block_t *p_next = p_block_out->p_next;
564
565
        /* Correct timestamp */
566
1.44M
        if( p_sys->p_packetizer->fmt_out.i_cat == VIDEO_ES )
567
0
        {
568
0
            if( p_block_out->i_pts == VLC_TICK_INVALID &&
569
0
                p_block_out->i_dts == VLC_TICK_INVALID )
570
0
                p_block_out->i_dts = VLC_TICK_0 + p_sys->i_pts + VLC_TICK_FROM_SEC(1) / p_sys->f_fps;
571
0
            if( p_block_out->i_dts != VLC_TICK_INVALID )
572
0
                p_sys->i_pts = p_block_out->i_dts - VLC_TICK_0;
573
0
        }
574
1.44M
        else
575
1.44M
        {
576
1.44M
            p_sys->i_pts = p_block_out->i_pts - VLC_TICK_0;
577
1.44M
        }
578
579
1.44M
        if( p_block_out->i_pts != VLC_TICK_INVALID )
580
1.43M
        {
581
1.43M
            p_block_out->i_pts += p_sys->i_time_offset;
582
1.43M
        }
583
1.44M
        if( p_block_out->i_dts != VLC_TICK_INVALID )
584
1.43M
        {
585
1.43M
            p_block_out->i_dts += p_sys->i_time_offset;
586
1.43M
            es_out_SetPCR( p_demux->out, p_block_out->i_dts );
587
1.43M
        }
588
        /* Re-estimate bitrate */
589
1.44M
        if( p_sys->b_estimate_bitrate && p_sys->i_pts > VLC_TICK_FROM_MS(500) )
590
1.39M
            p_sys->i_bitrate = 8 * CLOCK_FREQ * p_sys->i_bytes
591
1.39M
                                   / (p_sys->i_pts - 1);
592
1.44M
        p_sys->i_bytes += p_block_out->i_buffer;
593
594
595
1.44M
        p_block_out->p_next = NULL;
596
1.44M
        es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
597
598
1.44M
        p_block_out = p_next;
599
1.44M
    }
600
63.0k
    return ret;
601
63.0k
}
602
603
/*****************************************************************************
604
 * Close: frees unused data
605
 *****************************************************************************/
606
static void Close( vlc_object_t * p_this )
607
4.11k
{
608
4.11k
    demux_t     *p_demux = (demux_t*)p_this;
609
4.11k
    demux_sys_t *p_sys = p_demux->p_sys;
610
611
4.11k
    if( p_sys->p_packetized_data )
612
0
        block_ChainRelease( p_sys->p_packetized_data );
613
4.11k
    for( size_t i=0; i< p_sys->chapters.i_count; i++ )
614
0
        vlc_seekpoint_Delete( p_sys->chapters.p_entry[i].p_seekpoint );
615
4.11k
    TAB_CLEAN( p_sys->chapters.i_count, p_sys->chapters.p_entry );
616
4.11k
    if( p_sys->mllt.p_bits )
617
0
        free( p_sys->mllt.p_bits );
618
4.11k
    demux_PacketizerDestroy( p_sys->p_packetizer );
619
4.11k
    free( p_sys );
620
4.11k
}
621
622
/*****************************************************************************
623
 * Time seek:
624
 *****************************************************************************/
625
static void PostSeekCleanup( demux_sys_t *p_sys, vlc_tick_t i_time )
626
0
{
627
    /* Fix time_offset */
628
0
    if( i_time >= 0 )
629
0
        p_sys->i_time_offset = i_time - p_sys->i_pts;
630
    /* And reset buffered data */
631
0
    if( p_sys->p_packetized_data )
632
0
        block_ChainRelease( p_sys->p_packetized_data );
633
0
    p_sys->p_packetized_data = NULL;
634
    /* Reset chapter if any */
635
0
    p_sys->chapters.i_current = 0;
636
0
    p_sys->i_demux_flags |= INPUT_UPDATE_SEEKPOINT;
637
0
}
638
639
static int MovetoTimePos( demux_t *p_demux, vlc_tick_t i_time, uint64_t i_pos )
640
0
{
641
0
    demux_sys_t *p_sys  = p_demux->p_sys;
642
0
    int i_ret = vlc_stream_Seek( p_demux->s, p_sys->i_stream_offset + i_pos );
643
0
    if( i_ret != VLC_SUCCESS )
644
0
        return i_ret;
645
0
    PostSeekCleanup( p_sys, i_time );
646
0
    return VLC_SUCCESS;
647
0
}
648
649
/*****************************************************************************
650
 * Control:
651
 *****************************************************************************/
652
static int Control( demux_t *p_demux, int i_query, va_list args )
653
0
{
654
0
    demux_sys_t *p_sys  = p_demux->p_sys;
655
0
    bool *pb_bool;
656
657
0
    switch( i_query )
658
0
    {
659
0
        case DEMUX_HAS_UNSUPPORTED_META:
660
0
            pb_bool = va_arg( args, bool * );
661
0
            *pb_bool = true;
662
0
            return VLC_SUCCESS;
663
664
0
        case DEMUX_GET_TIME:
665
0
            *va_arg( args, vlc_tick_t * ) = p_sys->i_pts + p_sys->i_time_offset;
666
0
            return VLC_SUCCESS;
667
668
0
        case DEMUX_GET_LENGTH:
669
0
        {
670
0
            if( p_sys->i_duration > 0 )
671
0
            {
672
0
                *va_arg( args, vlc_tick_t * ) = p_sys->i_duration;
673
0
                return VLC_SUCCESS;
674
0
            }
675
            /* compute length from bitrate */
676
0
            va_list ap;
677
0
            int i_ret;
678
679
0
            va_copy ( ap, args );
680
0
            i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset,
681
0
                                    -1, p_sys->i_bitrate, 1, i_query, ap );
682
0
            va_end( ap );
683
684
            /* No bitrate, we can't have it precisely, but we can compute
685
             * a raw approximation with time/position */
686
0
            if( i_ret && !p_sys->i_bitrate )
687
0
            {
688
0
                uint64_t i_stream_size;
689
0
                if( vlc_stream_GetSize( p_demux->s, &i_stream_size ) != VLC_SUCCESS )
690
0
                    return VLC_EGENERIC;
691
692
0
                uint64_t i_pos = vlc_stream_Tell( p_demux->s );
693
0
                float f_pos = (double)i_pos / (double)i_stream_size;
694
                /* The first few seconds are guaranteed to be very whacky,
695
                 * don't bother trying ... Too bad */
696
0
                if( f_pos < 0.01f ||
697
0
                    (p_sys->i_pts + p_sys->i_time_offset) < VLC_TICK_FROM_SEC(8) )
698
0
                {
699
0
                    return VLC_EGENERIC;
700
0
                }
701
702
0
                *va_arg( args, vlc_tick_t * ) =
703
0
                    (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
704
0
                return VLC_SUCCESS;
705
0
            }
706
0
            return i_ret;
707
0
        }
708
709
0
        case DEMUX_SET_TIME:
710
0
        case DEMUX_SET_POSITION:
711
0
        {
712
0
            vlc_tick_t i_time;
713
0
            double f_pos;
714
0
            uint64_t i_offset;
715
716
0
            va_list ap;
717
0
            va_copy ( ap, args ); /* don't break args for helper fallback */
718
0
            if( i_query == DEMUX_SET_TIME )
719
0
            {
720
0
                i_time = va_arg(ap, vlc_tick_t);
721
0
                f_pos = p_sys->i_duration ? i_time / (double) p_sys->i_duration : -1.0;
722
0
                if( f_pos > 1.0 )
723
0
                    f_pos = 1.0;
724
0
            }
725
0
            else
726
0
            {
727
0
                f_pos = va_arg(ap, double);
728
0
                i_time = p_sys->i_duration ? p_sys->i_duration * f_pos : VLC_TICK_INVALID;
729
0
            }
730
0
            va_end( ap );
731
732
            /* Try to use ID3 table */
733
0
            if( !SeekByMlltTable( &p_sys->mllt, &i_time, &i_offset ) )
734
0
                return MovetoTimePos( p_demux, i_time, i_offset );
735
736
0
            if( p_sys->codec.i_codec == VLC_CODEC_MPGA )
737
0
            {
738
0
                uint64_t streamsize;
739
0
                if( !vlc_stream_GetSize( p_demux->s, &streamsize ) &&
740
0
                    streamsize > p_sys->i_stream_offset )
741
0
                    streamsize -= p_sys->i_stream_offset;
742
0
                else
743
0
                    streamsize = 0;
744
745
0
                if( !MpgaSeek( &p_sys->mpgah, &p_sys->xing,
746
0
                           streamsize, f_pos, &i_time, &i_offset ) )
747
0
                {
748
0
                    return MovetoTimePos( p_demux, i_time, i_offset );
749
0
                }
750
0
            }
751
752
            /* fallback on bitrate / file position seeking */
753
0
            i_time = VLC_TICK_INVALID;
754
0
            int ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
755
0
                                             p_sys->i_bitrate, 1, i_query, args );
756
0
            if( ret != VLC_SUCCESS )
757
0
                return ret; /* not much we can do */
758
759
0
            if( p_sys->i_bitrate > 0 )
760
0
            {
761
0
                i_offset = vlc_stream_Tell( p_demux->s ); /* new pos */
762
0
                i_time = VLC_TICK_0;
763
0
                if( likely(i_offset > p_sys->i_stream_offset) )
764
0
                {
765
0
                    i_offset -= p_sys->i_stream_offset;
766
0
                    i_time += vlc_tick_from_samples( i_offset * 8, p_sys->i_bitrate );
767
0
                }
768
0
            }
769
0
            PostSeekCleanup( p_sys, i_time );
770
771
            /* FIXME TODO: implement a high precision seek (with mp3 parsing)
772
             * needed for multi-input */
773
0
            return VLC_SUCCESS;
774
0
        }
775
776
0
        case DEMUX_GET_TITLE_INFO:
777
0
        {
778
0
            if( p_sys->chapters.i_count == 0 )
779
0
                return VLC_EGENERIC;
780
0
            input_title_t **pp_title = malloc( sizeof(*pp_title) );
781
0
            if( !pp_title )
782
0
                return VLC_EGENERIC;
783
0
            *pp_title = vlc_input_title_New();
784
0
            if( !*pp_title )
785
0
            {
786
0
                free( pp_title );
787
0
                return VLC_EGENERIC;
788
0
            }
789
0
            (*pp_title)->seekpoint = vlc_alloc( p_sys->chapters.i_count, sizeof(seekpoint_t) );
790
0
            if( !(*pp_title)->seekpoint )
791
0
            {
792
0
                free( *pp_title );
793
0
                free( pp_title );
794
0
                return VLC_EGENERIC;
795
0
            }
796
0
            for( size_t i=0; i<p_sys->chapters.i_count; i++ )
797
0
            {
798
0
                seekpoint_t *s = vlc_seekpoint_Duplicate( p_sys->chapters.p_entry[i].p_seekpoint );
799
0
                if( s )
800
0
                    (*pp_title)->seekpoint[(*pp_title)->i_seekpoint++] = s;
801
0
            }
802
0
            *(va_arg( args, input_title_t *** )) = pp_title;
803
0
            *(va_arg( args, int* )) = 1;
804
0
            *(va_arg( args, int* )) = 0;
805
0
            *(va_arg( args, int* )) = 0;
806
0
            return VLC_SUCCESS;
807
0
        }
808
0
        break;
809
810
0
        case DEMUX_GET_TITLE:
811
0
            if( p_sys->chapters.i_count == 0 )
812
0
                return VLC_EGENERIC;
813
0
            *(va_arg( args, int* )) = 0;
814
0
            return VLC_SUCCESS;
815
816
0
        case DEMUX_GET_SEEKPOINT:
817
0
            if( p_sys->chapters.i_count == 0 )
818
0
                return VLC_EGENERIC;
819
0
            *(va_arg( args, int* )) = p_sys->chapters.i_current;
820
0
            return VLC_SUCCESS;
821
822
0
        case DEMUX_SET_TITLE:
823
0
            return va_arg( args, int) == 0 ? VLC_SUCCESS : VLC_EGENERIC;
824
825
0
        case DEMUX_SET_SEEKPOINT:
826
0
        {
827
0
            int i = va_arg( args, int );
828
0
            if( (size_t)i>=p_sys->chapters.i_count )
829
0
                return VLC_EGENERIC;
830
0
            const chap_entry_t *p = &p_sys->chapters.p_entry[i];
831
0
            if( p_sys->chapters.p_entry[i].i_offset == UINT32_MAX )
832
0
                return demux_Control( p_demux, DEMUX_SET_TIME, p->p_seekpoint->i_time_offset );
833
0
            int i_ret= MovetoTimePos( p_demux, p->p_seekpoint->i_time_offset, p->i_offset );
834
0
            if( i_ret == VLC_SUCCESS )
835
0
                p_sys->chapters.i_current = i;
836
0
            return i_ret;
837
0
        }
838
839
0
        case DEMUX_TEST_AND_CLEAR_FLAGS:
840
0
        {
841
0
            unsigned *restrict flags = va_arg( args, unsigned * );
842
0
            *flags &= p_sys->i_demux_flags;
843
0
            p_sys->i_demux_flags &= ~*flags;
844
0
            return VLC_SUCCESS;
845
0
        }
846
0
    }
847
848
0
    return demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
849
0
                                  p_sys->i_bitrate, 1, i_query, args );
850
0
}
851
852
/*****************************************************************************
853
 * Makes a link list of buffer of parsed data
854
 * Returns true if EOF
855
 *****************************************************************************/
856
static bool Parse( demux_t *p_demux, block_t **pp_output )
857
138k
{
858
138k
    demux_sys_t *p_sys = p_demux->p_sys;
859
138k
    block_t *p_block_in, *p_block_out;
860
861
138k
    *pp_output = NULL;
862
863
138k
    if( p_sys->codec.b_use_word )
864
63.1k
    {
865
        /* Make sure we are word aligned */
866
63.1k
        int64_t i_pos = vlc_stream_Tell( p_demux->s );
867
63.1k
        if( (i_pos & 1) && vlc_stream_Read( p_demux->s, NULL, 1 ) != 1 )
868
582
            return true;
869
63.1k
    }
870
871
137k
    p_block_in = vlc_stream_Block( p_demux->s, p_sys->i_packet_size );
872
137k
    bool b_eof = p_block_in == NULL;
873
874
137k
    if( p_block_in )
875
132k
    {
876
132k
        if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
877
19.2k
        {
878
            /* Convert to big endian */
879
19.2k
            block_t *old = p_block_in;
880
19.2k
            p_block_in = block_Alloc( p_block_in->i_buffer );
881
19.2k
            if( p_block_in )
882
19.2k
            {
883
19.2k
                block_CopyProperties( p_block_in, old );
884
19.2k
                swab( old->p_buffer, p_block_in->p_buffer, old->i_buffer );
885
19.2k
            }
886
19.2k
            block_Release( old );
887
19.2k
        }
888
889
132k
        if( p_block_in )
890
132k
        {
891
132k
            p_block_in->i_pts =
892
132k
            p_block_in->i_dts = (p_sys->b_start || p_sys->b_initial_sync_failed) ?
893
132k
                                 VLC_TICK_0 : VLC_TICK_INVALID;
894
132k
        }
895
132k
    }
896
137k
    p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
897
898
137k
    decoder_t *p_packetizer = p_sys->p_packetizer;
899
1.58M
    while( ( p_block_out = p_packetizer->pf_packetize( p_packetizer, p_block_in ? &p_block_in : NULL ) ) )
900
1.44M
    {
901
1.44M
        p_sys->b_initial_sync_failed = false;
902
2.89M
        while( p_block_out )
903
1.44M
        {
904
1.44M
            if( !p_sys->p_es )
905
2.76k
            {
906
2.76k
                es_format_t fmt;
907
2.76k
                es_format_Init( &fmt, p_packetizer->fmt_out.i_cat, p_packetizer->fmt_out.i_codec );
908
2.76k
                es_format_Copy( &fmt, &p_packetizer->fmt_out );
909
910
2.76k
                switch( p_sys->xing.audiomode )
911
2.76k
                {
912
0
                    case XING_AUDIOMODE_AMBISONICS:
913
0
                        fmt.audio.channel_type = AUDIO_CHANNEL_TYPE_AMBISONICS;
914
0
                        break;
915
2.76k
                    default:
916
2.76k
                        break;
917
2.76k
                }
918
919
2.76k
                fmt.b_packetized = true;
920
2.76k
                fmt.i_id = 0;
921
2.76k
                p_sys->p_es = es_out_Add( p_demux->out, &fmt );
922
923
                /* Use the bitrate as initual value */
924
2.76k
                if( p_sys->b_estimate_bitrate )
925
2.72k
                    p_sys->i_bitrate = fmt.i_bitrate;
926
927
2.76k
                es_format_Clean( &fmt );
928
2.76k
            }
929
930
1.44M
            block_t *p_next = p_block_out->p_next;
931
1.44M
            p_block_out->p_next = NULL;
932
933
1.44M
            block_ChainLastAppend( &pp_output, p_block_out );
934
935
1.44M
            p_block_out = p_next;
936
1.44M
        }
937
1.44M
    }
938
939
137k
    if( p_sys->b_initial_sync_failed )
940
137k
        msg_Dbg( p_demux, "did not sync on first block" );
941
137k
    p_sys->b_start = false;
942
943
137k
    return b_eof;
944
137k
}
945
946
/* Check to apply to WAVE fmt header */
947
static int GenericFormatCheck( int i_format, const uint8_t *p_head )
948
3.76k
{
949
3.76k
    if ( i_format == WAVE_FORMAT_PCM )
950
2.96k
    {
951
2.96k
        if( GetWLE( p_head /* nChannels */ ) != 2 )
952
802
            return VLC_EGENERIC;
953
2.16k
        if( GetDWLE( p_head + 2 /* nSamplesPerSec */ ) != 44100 )
954
336
            return VLC_EGENERIC;
955
2.16k
    }
956
2.63k
    return VLC_SUCCESS;
957
3.76k
}
958
959
/*****************************************************************************
960
 * Wav header skipper
961
 *****************************************************************************/
962
static int WavSkipHeader( demux_t *p_demux, uint64_t *pi_skip,
963
                          const uint16_t rgi_twocc[],
964
                          int (*pf_format_check)( int, const uint8_t * ) )
965
15.8k
{
966
15.8k
    const uint8_t *p_peek;
967
15.8k
    size_t i_peek = 0;
968
15.8k
    uint32_t i_len;
969
970
    /* */
971
15.8k
    *pi_skip = 0;
972
973
    /* Check if we are dealing with a WAV file */
974
15.8k
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
975
0
        return VLC_SUCCESS;
976
977
15.8k
    if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
978
9.07k
        return VLC_SUCCESS;
979
980
    /* Find the wave format header */
981
6.74k
    i_peek = 12 + 8;
982
380k
    while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
983
374k
    {
984
374k
        i_len = GetDWLE( p_peek + i_peek - 4 );
985
374k
        if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
986
112
            return VLC_EGENERIC;
987
988
374k
        i_peek += i_len + 8;
989
374k
        if( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) != (ssize_t) i_peek )
990
92
            return VLC_EGENERIC;
991
374k
    }
992
993
    /* Sanity check the wave format header */
994
6.53k
    i_len = GetDWLE( p_peek + i_peek - 4 );
995
6.53k
    if( i_len > WAV_PROBE_SIZE )
996
0
        return VLC_EGENERIC;
997
998
6.53k
    i_peek += i_len + 8;
999
6.53k
    if( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) != (ssize_t) i_peek )
1000
112
        return VLC_EGENERIC;
1001
6.42k
    const uint16_t i_twocc = GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ );
1002
6.42k
    int i_format_idx;
1003
10.4k
    for( i_format_idx = 0; rgi_twocc[i_format_idx] != WAVE_FORMAT_UNKNOWN; i_format_idx++ )
1004
8.83k
    {
1005
8.83k
        if( i_twocc == rgi_twocc[i_format_idx] )
1006
4.82k
            break;
1007
8.83k
    }
1008
6.42k
    if( rgi_twocc[i_format_idx] == WAVE_FORMAT_UNKNOWN )
1009
1.60k
        return VLC_EGENERIC;
1010
1011
4.82k
    if( pf_format_check &&
1012
3.76k
        pf_format_check( i_twocc, p_peek + i_peek - i_len - 6 ) != VLC_SUCCESS )
1013
1.13k
            return VLC_EGENERIC;
1014
1015
    /* Skip the wave header */
1016
127k
    while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
1017
123k
    {
1018
123k
        i_len = GetDWLE( p_peek + i_peek - 4 );
1019
123k
        if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
1020
10
            return VLC_EGENERIC;
1021
1022
123k
        i_peek += i_len + 8;
1023
123k
        if( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) != (ssize_t) i_peek )
1024
16
            return VLC_EGENERIC;
1025
123k
    }
1026
3.66k
    *pi_skip = i_peek;
1027
3.66k
    return VLC_SUCCESS;
1028
3.68k
}
1029
1030
static int GenericProbe( demux_t *p_demux, uint64_t *pi_offset,
1031
                         const char * ppsz_name[],
1032
                         int (*pf_check)( const uint8_t *, unsigned * ),
1033
                         unsigned i_check_size,
1034
                         unsigned i_base_probing,
1035
                         unsigned i_wav_extra_probing,
1036
                         bool b_use_word,
1037
                         const uint16_t pi_twocc[],
1038
                         int (*pf_format_check)( int, const uint8_t * ) )
1039
11.1k
{
1040
11.1k
    bool   b_forced_demux;
1041
1042
11.1k
    uint64_t i_offset;
1043
11.1k
    const uint8_t *p_peek;
1044
11.1k
    uint64_t i_skip;
1045
1046
11.1k
    b_forced_demux = false;
1047
26.0k
    for( size_t i = 0; ppsz_name[i] != NULL; i++ )
1048
14.8k
    {
1049
14.8k
        b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
1050
14.8k
    }
1051
1052
11.1k
    i_offset = vlc_stream_Tell( p_demux->s );
1053
1054
11.1k
    if( WavSkipHeader( p_demux, &i_skip, pi_twocc, pf_format_check ) )
1055
1.45k
    {
1056
1.45k
        if( !b_forced_demux )
1057
1.36k
            return VLC_EGENERIC;
1058
1.45k
    }
1059
9.82k
    const bool b_wav = i_skip > 0;
1060
1061
    /* peek the beginning
1062
     * It is common that wav files have some sort of garbage at the beginning
1063
     * We will accept probing 0.5s of data in this case.
1064
     */
1065
9.82k
    const size_t i_probe = i_skip + i_check_size + i_base_probing + ( b_wav ? i_wav_extra_probing : 0);
1066
9.82k
    const ssize_t i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_probe );
1067
1068
9.82k
    if( i_peek < 0 || (size_t)i_peek < i_skip + i_check_size )
1069
0
        return VLC_EGENERIC;
1070
1071
9.82k
    for( ;; )
1072
40.6M
    {
1073
        /* i_peek > 0 so we can cast into size_t. */
1074
40.6M
        if( i_skip + i_check_size > (size_t)i_peek )
1075
2.27k
        {
1076
2.27k
            if( !b_forced_demux )
1077
2.14k
                return VLC_EGENERIC;
1078
128
            break;
1079
2.27k
        }
1080
40.6M
        unsigned i_samples = 0;
1081
40.6M
        int i_size = pf_check( &p_peek[i_skip], &i_samples );
1082
40.6M
        if( i_size >= 0 )
1083
45.8k
        {
1084
45.8k
            if( i_size == 0 || /* 0 sized frame ?? */
1085
45.4k
                i_skip == 0 /* exact match from start, we're not WAVE either, so skip multiple checks (would break if padding) */ )
1086
1.79k
                break;
1087
1088
            /* If we have the frame size, check the next frame for
1089
             * extra robustness
1090
             * The second test is because some .wav have paddings
1091
             */
1092
45.8k
            bool b_ok = false;
1093
122k
            for( int t = 0; t < 1 + !!b_wav; t++ )
1094
82.1k
            {
1095
82.1k
                if( t == 1 )
1096
38.1k
                {
1097
38.1k
                    if(!i_samples)
1098
2.66k
                        break;
1099
35.4k
                    i_size = i_samples * 2 * 2;
1100
35.4k
                }
1101
1102
79.5k
                if( i_skip + i_check_size + i_size <= (size_t)i_peek )
1103
67.2k
                {
1104
67.2k
                    b_ok = pf_check( &p_peek[i_skip+i_size], NULL ) >= 0;
1105
67.2k
                    if( b_ok )
1106
1.20k
                        break;
1107
67.2k
                }
1108
79.5k
            }
1109
44.0k
            if( b_ok )
1110
1.20k
                break;
1111
44.0k
        }
1112
40.6M
        if( b_use_word )
1113
19.5M
            i_skip += ((i_offset + i_skip) % 2 == 0) ? 2 : 1;
1114
21.1M
        else
1115
21.1M
            i_skip++;
1116
40.6M
        if( !b_wav && !b_forced_demux )
1117
4.55k
            return VLC_EGENERIC;
1118
40.6M
    }
1119
1120
3.12k
    *pi_offset = i_offset + i_skip;
1121
3.12k
    return VLC_SUCCESS;
1122
9.82k
}
1123
1124
/*****************************************************************************
1125
 * Mpeg I/II Audio
1126
 *****************************************************************************/
1127
static int MpgaCheckSync( const uint8_t *p_peek )
1128
328k
{
1129
328k
    uint32_t h = GetDWBE( p_peek );
1130
1131
328k
    if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
1132
41.4k
        || (((h >> 19)&0x03) == 1 )         /* valid version ID ? */
1133
41.2k
        || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
1134
25.2k
        || (((h >> 12)&0x0F) == 0x0F )      /* valid bitrate ?*/
1135
5.91k
        || (((h >> 10) & 0x03) == 0x03 )    /* valid sampling freq ? */
1136
2.01k
        || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
1137
327k
    {
1138
327k
        return false;
1139
327k
    }
1140
1.89k
    return true;
1141
328k
}
1142
1143
946
#define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
1144
946
#define MPGA_MODE(h)        (((h)>> 6)&0x03)
1145
1146
static int MpgaProbe( demux_t *p_demux, uint64_t *pi_offset )
1147
4.62k
{
1148
4.62k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_MPEG, WAVE_FORMAT_MPEGLAYER3, WAVE_FORMAT_UNKNOWN };
1149
4.62k
    bool   b_forced;
1150
4.62k
    bool   b_forced_demux;
1151
4.62k
    uint64_t i_offset;
1152
1153
4.62k
    const uint8_t *p_peek;
1154
4.62k
    uint64_t i_skip;
1155
4.62k
    ssize_t i_peek;
1156
1157
4.62k
    b_forced = demux_IsPathExtension( p_demux, ".mp3" );
1158
4.62k
    b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
1159
2.80k
                     demux_IsForced( p_demux, "mpga" );
1160
1161
4.62k
    i_offset = vlc_stream_Tell( p_demux->s );
1162
1163
4.62k
    if( WavSkipHeader( p_demux, &i_skip, rgi_twocc, NULL ) )
1164
1.63k
    {
1165
1.63k
        if( !b_forced_demux )
1166
191
            return VLC_EGENERIC;
1167
1168
1.44k
        return VLC_EGENERIC;
1169
1.63k
    }
1170
1171
2.99k
    i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_skip + 4 );
1172
2.99k
    if( i_peek <= 0 || (uint64_t) i_peek < i_skip + 4 )
1173
0
        return VLC_EGENERIC;
1174
1175
2.99k
    if( !MpgaCheckSync( &p_peek[i_skip] ) )
1176
2.22k
    {
1177
2.22k
        bool b_ok = false;
1178
1179
2.22k
        if( !b_forced_demux && !b_forced )
1180
2.00k
            return VLC_EGENERIC;
1181
1182
220
        i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_skip + 8096 );
1183
325k
        while( i_peek > 0 && i_skip + 4 < (uint64_t) i_peek )
1184
324k
        {
1185
324k
            if( MpgaCheckSync( &p_peek[i_skip] ) )
1186
179
            {
1187
179
                b_ok = true;
1188
179
                break;
1189
179
            }
1190
324k
            i_skip++;
1191
324k
        }
1192
220
        if( !b_ok && !b_forced_demux )
1193
0
            return VLC_EGENERIC;
1194
220
    }
1195
987
    *pi_offset = i_offset + i_skip;
1196
987
    return VLC_SUCCESS;
1197
2.99k
}
1198
1199
static int SeekByMlltTable( sync_table_t *mllt, vlc_tick_t *pi_time, uint64_t *pi_offset )
1200
0
{
1201
0
    if( !mllt->p_bits )
1202
0
        return -1;
1203
1204
0
    sync_table_ctx_t *p_cur = &mllt->current;
1205
1206
    /* reset or init context */
1207
0
    if( *pi_time < p_cur->i_time || !p_cur->br.p )
1208
0
    {
1209
0
        p_cur->i_time = 0;
1210
0
        p_cur->i_pos = 0;
1211
0
        bs_init(&p_cur->br, mllt->p_bits, mllt->i_bits);
1212
0
    }
1213
1214
0
    while(!bs_eof(&p_cur->br))
1215
0
    {
1216
0
        const uint32_t i_bytesdev = bs_read(&p_cur->br, mllt->i_bits_per_bytes_dev);
1217
0
        const uint32_t i_msdev = bs_read(&p_cur->br, mllt->i_bits_per_ms_dev);
1218
0
        if(bs_error(&p_cur->br))
1219
0
            break;
1220
0
        const vlc_tick_t i_deltatime = VLC_TICK_FROM_MS(mllt->i_ms_btw_refs + i_msdev);
1221
0
        if( p_cur->i_time + i_deltatime > *pi_time )
1222
0
            break;
1223
0
        p_cur->i_time += i_deltatime;
1224
0
        p_cur->i_pos += mllt->i_bytes_btw_refs + i_bytesdev;
1225
0
    }
1226
0
    *pi_time = p_cur->i_time;
1227
0
    *pi_offset = p_cur->i_pos;
1228
1229
0
    return 0;
1230
0
}
1231
1232
static int ID3TAG_Parse_Handler( uint32_t i_tag, const uint8_t *p_payload, size_t i_payload, void *p_priv )
1233
0
{
1234
0
    demux_t *p_demux = (demux_t *) p_priv;
1235
0
    demux_sys_t *p_sys = p_demux->p_sys;
1236
1237
0
    if( i_tag == VLC_FOURCC('M', 'L', 'L', 'T') )
1238
0
    {
1239
0
        if( i_payload > 20 )
1240
0
        {
1241
0
            p_sys->mllt.i_frames_btw_refs = GetWBE(p_payload);
1242
0
            p_sys->mllt.i_bytes_btw_refs = GetDWBE(&p_payload[1]) & 0x00FFFFFF;
1243
0
            p_sys->mllt.i_ms_btw_refs = GetDWBE(&p_payload[4]) & 0x00FFFFFF;
1244
0
            if( !p_sys->mllt.i_frames_btw_refs || !p_sys->mllt.i_bytes_btw_refs ||
1245
0
                    !p_sys->mllt.i_ms_btw_refs ||
1246
0
                    p_payload[8] > 31 || p_payload[9] > 31 || /* bits length sanity check */
1247
0
                    ((p_payload[8] + p_payload[9]) % 4) || p_payload[8] + p_payload[9] < 4 )
1248
0
                return VLC_EGENERIC;
1249
0
            p_sys->mllt.i_bits_per_bytes_dev = p_payload[8];
1250
0
            p_sys->mllt.i_bits_per_ms_dev = p_payload[9];
1251
0
            p_sys->mllt.p_bits = malloc(i_payload - 10);
1252
0
            if( likely(p_sys->mllt.p_bits) )
1253
0
            {
1254
0
                p_sys->mllt.i_bits = i_payload - 10;
1255
0
                memcpy(p_sys->mllt.p_bits, &p_payload[10], p_sys->mllt.i_bits);
1256
0
                msg_Dbg(p_demux, "read MLLT sync table with %zu entries",
1257
0
                        (p_sys->mllt.i_bits * 8) / (p_sys->mllt.i_bits_per_bytes_dev + p_sys->mllt.i_bits_per_ms_dev) );
1258
0
            }
1259
0
        }
1260
0
        return VLC_EGENERIC;
1261
0
    }
1262
0
    else if( i_tag == VLC_FOURCC('T', 'X', 'X', 'X') )
1263
0
    {
1264
0
        vlc_meta_t *p_meta = vlc_meta_New();
1265
0
        if( p_meta )
1266
0
        {
1267
0
            bool b_updated;
1268
0
            if( ID3HandleTag( p_payload, i_payload, i_tag, p_meta, &b_updated ) )
1269
0
            {
1270
0
                vlc_replay_gain_CopyFromMeta( &p_sys->audio_replay_gain, p_meta );
1271
0
            }
1272
0
            vlc_meta_Delete( p_meta );
1273
0
        }
1274
0
    }
1275
0
    else if ( i_tag == VLC_FOURCC('C', 'H', 'A', 'P') && i_payload >= 17 )
1276
0
    {
1277
0
        char *psz_title = strndup( (const char *)p_payload, i_payload - 16 );
1278
0
        size_t i_offset = psz_title ? strlen( psz_title ) : 0;
1279
0
        if( p_payload[i_offset] != 0 )
1280
0
        {
1281
0
            free( psz_title );
1282
0
            return VLC_EGENERIC;
1283
0
        }
1284
0
        chap_entry_t e;
1285
0
        e.p_seekpoint = vlc_seekpoint_New();
1286
0
        if( e.p_seekpoint )
1287
0
        {
1288
0
            e.p_seekpoint->psz_name = psz_title;
1289
0
            e.p_seekpoint->i_time_offset = VLC_TICK_FROM_MS(GetDWBE(&p_payload[1 + i_offset]));
1290
0
            e.i_offset = GetDWBE(&p_payload[1 + i_offset + 8]);
1291
0
            p_payload += i_offset + 1 + 16;
1292
0
            i_payload -= i_offset + 1 + 16;
1293
0
            if( 12 < i_payload && !memcmp("TIT2", p_payload, 4) )
1294
0
            {
1295
0
                psz_title = NULL; /* optional alloc */
1296
0
                const char *psz = ID3TextConvert(&p_payload[10], i_payload-12, &psz_title);
1297
0
                if( psz ) /* replace with TIT2 */
1298
0
                {
1299
0
                    free( e.p_seekpoint->psz_name );
1300
0
                    e.p_seekpoint->psz_name = (psz_title) ? psz_title : strdup( psz );
1301
0
                }
1302
0
            }
1303
0
            TAB_APPEND(p_sys->chapters.i_count, p_sys->chapters.p_entry, e);
1304
0
        } else free( psz_title );
1305
0
    }
1306
1307
0
    return VLC_SUCCESS;
1308
0
}
1309
1310
static int ID3Parse( demux_t *p_demux,
1311
                     int (*pf_callback)(uint32_t, const uint8_t *, size_t, void *) )
1312
987
{
1313
987
    const block_t *p_tags = NULL;
1314
1315
987
    if( vlc_stream_Control( p_demux->s, STREAM_GET_TAGS, &p_tags ) != VLC_SUCCESS )
1316
987
        return VLC_EGENERIC;
1317
1318
0
    for( ; p_tags; p_tags = p_tags->p_next )
1319
0
        ID3TAG_Parse( p_tags->p_buffer, p_tags->i_buffer, pf_callback, (void *) p_demux );
1320
1321
0
    return VLC_SUCCESS;
1322
987
}
1323
1324
static int MpgaInit( demux_t *p_demux )
1325
987
{
1326
987
    demux_sys_t *p_sys = p_demux->p_sys;
1327
1328
987
    const uint8_t *p_peek;
1329
987
    int i_peek;
1330
1331
    /* */
1332
987
    p_sys->i_packet_size = 1024;
1333
1334
987
    ID3Parse( p_demux, ID3TAG_Parse_Handler );
1335
1336
    /* Load a potential xing header */
1337
987
    i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
1338
987
    if( i_peek < 4 + 21 )
1339
9
        return VLC_SUCCESS;
1340
1341
978
    const uint32_t header = GetDWBE( p_peek );
1342
978
    if( !MpgaCheckSync( p_peek ) || mpga_decode_frameheader( header, &p_sys->mpgah ) )
1343
32
        return VLC_SUCCESS;
1344
1345
946
    if( p_sys->mpgah.i_layer == 3 )
1346
297
        p_sys->codec.i_codec = VLC_CODEC_MP3;
1347
1348
    /* Xing header */
1349
946
    int i_skip;
1350
1351
946
    if( MPGA_VERSION( header ) == 0 )
1352
596
        i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
1353
350
    else
1354
350
        i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
1355
1356
946
    if( i_skip >= i_peek )
1357
0
        return VLC_SUCCESS;
1358
1359
946
    struct xing_info_s *xing = &p_sys->xing;
1360
946
    if( ParseXing( &p_peek[i_skip], i_peek - i_skip, xing ) == VLC_SUCCESS )
1361
118
    {
1362
118
        const uint64_t i_total_samples = xing->i_frames * (uint64_t) p_sys->mpgah.i_samples_per_frame;
1363
118
        const uint64_t i_dropped_samples = xing->i_delay_samples + xing->i_padding_samples;
1364
1365
118
        if( p_sys->mpgah.i_sample_rate && i_dropped_samples < i_total_samples )
1366
102
        {
1367
102
            uint64_t i_stream_size;
1368
            /* We only set duration if we can't check (then compute it using bitrate/size)
1369
               or if we verified the file isn't truncated */
1370
102
            if( xing->i_bytes == 0 ||
1371
99
                vlc_stream_GetSize( p_demux->s, &i_stream_size ) ||
1372
99
               (i_stream_size >= xing->i_bytes &&
1373
61
                i_stream_size <= xing->i_bytes + p_sys->mpgah.i_frame_size) )
1374
3
            {
1375
3
                p_sys->i_duration = vlc_tick_from_samples( i_total_samples - i_dropped_samples,
1376
3
                                                           p_sys->mpgah.i_sample_rate );
1377
3
            }
1378
102
        }
1379
1380
118
        unsigned i_bitrate = 0;
1381
118
        if( xing->brmode == XING_MODE_ABR || xing->brmode == XING_MODE_ABR_2PASS )
1382
2
            i_bitrate = xing->bitrate_avg * 1000;
1383
1384
118
        if( !i_bitrate && p_sys->mpgah.i_sample_rate &&
1385
117
            xing->i_bytes > p_sys->mpgah.i_frame_size )
1386
92
        {
1387
92
            unsigned d = vlc_tick_from_samples( i_total_samples, p_sys->mpgah.i_sample_rate );
1388
92
            if( d )
1389
87
                i_bitrate = (xing->i_bytes - p_sys->mpgah.i_frame_size) * 8 * CLOCK_FREQ / d;
1390
92
        }
1391
1392
118
        if( i_bitrate )
1393
57
        {
1394
57
            p_sys->i_bitrate = i_bitrate;
1395
57
            p_sys->b_estimate_bitrate = false;
1396
57
        }
1397
1398
118
        if( isfinite(xing->f_radio_replay_gain) )
1399
6
        {
1400
6
            p_sys->audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
1401
6
            p_sys->audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = xing->f_radio_replay_gain;
1402
6
        }
1403
1404
118
        if( isfinite(xing->f_peak_signal) )
1405
6
        {
1406
6
            p_sys->audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
1407
6
            p_sys->audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = xing->f_peak_signal;
1408
6
        }
1409
1410
118
        if( isfinite(xing->f_audiophile_replay_gain) )
1411
0
        {
1412
0
            p_sys->audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
1413
0
            p_sys->audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = xing->f_audiophile_replay_gain;
1414
0
        }
1415
1416
118
        msg_Dbg( p_demux, "Using '%4.4s' infotag"
1417
118
                          "(%"PRIu32" bytes, %"PRIu32" frames, %u samples/frame)",
1418
118
                          (char *) &xing->infotag,
1419
118
                          xing->i_bytes, xing->i_frames,
1420
118
                          p_sys->mpgah.i_samples_per_frame );
1421
1422
        /* We'll need to skip that part for playback
1423
         * and avoid using it as container frame could be different rate/size */
1424
118
        p_sys->i_stream_offset += p_sys->mpgah.i_frame_size;
1425
118
    }
1426
1427
946
    return VLC_SUCCESS;
1428
946
}
1429
1430
/*****************************************************************************
1431
 * AAC
1432
 *****************************************************************************/
1433
static int AacProbe( demux_t *p_demux, uint64_t *pi_offset )
1434
4.62k
{
1435
4.62k
    bool   b_forced;
1436
4.62k
    bool   b_forced_demux;
1437
1438
4.62k
    uint64_t i_offset;
1439
4.62k
    const uint8_t *p_peek;
1440
1441
4.62k
    b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
1442
4.62k
               demux_IsPathExtension( p_demux, ".aacp" );
1443
4.62k
    b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
1444
4.62k
                     demux_IsForced( p_demux, "aac" ) ||
1445
4.62k
                     demux_IsForced( p_demux, "mp4a" );
1446
1447
4.62k
    if( !b_forced_demux && !b_forced )
1448
4.62k
        return VLC_EGENERIC;
1449
1450
0
    i_offset = vlc_stream_Tell( p_demux->s );
1451
1452
    /* peek the beginning (10 is for adts header) */
1453
0
    if( vlc_stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
1454
0
        return VLC_EGENERIC;
1455
1456
0
    if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
1457
0
    {
1458
0
        msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
1459
0
        return VLC_EGENERIC;
1460
0
    }
1461
1462
0
    *pi_offset = i_offset;
1463
0
    return VLC_SUCCESS;
1464
0
}
1465
static int AacInit( demux_t *p_demux )
1466
0
{
1467
0
    demux_sys_t *p_sys = p_demux->p_sys;
1468
1469
0
    p_sys->i_packet_size = 4096;
1470
0
    p_sys->i_original = VLC_FOURCC('H','E','A','D');
1471
1472
0
    return VLC_SUCCESS;
1473
0
}
1474
1475
1476
/*****************************************************************************
1477
 * A52
1478
 *****************************************************************************/
1479
static int A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, unsigned *pi_samples, bool b_eac3 )
1480
19.5M
{
1481
19.5M
    vlc_a52_header_t header;
1482
19.5M
    uint8_t p_tmp[VLC_A52_MIN_HEADER_SIZE];
1483
1484
19.5M
    *p_big_endian =  p_peek[0] == 0x0b && p_peek[1] == 0x77;
1485
19.5M
    if( !*p_big_endian )
1486
19.4M
    {
1487
19.4M
        swab( p_peek, p_tmp, VLC_A52_MIN_HEADER_SIZE );
1488
19.4M
        p_peek = p_tmp;
1489
19.4M
    }
1490
1491
19.5M
    if( vlc_a52_header_Parse( &header, p_peek, VLC_A52_MIN_HEADER_SIZE ) )
1492
19.2M
        return VLC_EGENERIC;
1493
1494
353k
    if( !header.b_eac3 != !b_eac3 )
1495
318k
        return VLC_EGENERIC;
1496
35.1k
    if( pi_samples )
1497
33.9k
        *pi_samples = header.i_samples;
1498
35.1k
    return header.i_size;
1499
353k
}
1500
static int EA52CheckSyncProbe( const uint8_t *p_peek, unsigned *pi_samples )
1501
7.25M
{
1502
7.25M
    bool b_dummy;
1503
7.25M
    return A52CheckSync( p_peek, &b_dummy, pi_samples, true );
1504
7.25M
}
1505
1506
static int EA52Probe( demux_t *p_demux, uint64_t *pi_offset )
1507
3.23k
{
1508
3.23k
    const char *ppsz_name[] = { "eac3", NULL };
1509
3.23k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
1510
1511
3.23k
    return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe,
1512
3.23k
                         VLC_A52_MIN_HEADER_SIZE,
1513
3.23k
                         1920 + VLC_A52_MIN_HEADER_SIZE + 1,
1514
3.23k
                         WAV_EXTRA_PROBE_SIZE,
1515
3.23k
                         true, rgi_twocc, GenericFormatCheck );
1516
3.23k
}
1517
1518
static int A52CheckSyncProbe( const uint8_t *p_peek, unsigned *pi_samples )
1519
12.3M
{
1520
12.3M
    bool b_dummy;
1521
12.3M
    return A52CheckSync( p_peek, &b_dummy, pi_samples, false );
1522
12.3M
}
1523
1524
static int A52Probe( demux_t *p_demux, uint64_t *pi_offset )
1525
3.63k
{
1526
3.63k
    const char *ppsz_name[] = { "a52", "ac3", NULL };
1527
3.63k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
1528
1529
3.63k
    return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe,
1530
3.63k
                         VLC_A52_MIN_HEADER_SIZE,
1531
3.63k
                         1920 + VLC_A52_MIN_HEADER_SIZE + 1,
1532
3.63k
                         WAV_EXTRA_PROBE_SIZE,
1533
3.63k
                         true, rgi_twocc, GenericFormatCheck );
1534
3.63k
}
1535
1536
static int A52Init( demux_t *p_demux )
1537
1.15k
{
1538
1.15k
    demux_sys_t *p_sys = p_demux->p_sys;
1539
1540
1.15k
    p_sys->b_big_endian = false;
1541
1.15k
    p_sys->i_packet_size = 1024;
1542
1543
1.15k
    const uint8_t *p_peek;
1544
1545
    /* peek the beginning */
1546
1.15k
    if( vlc_stream_Peek( p_demux->s, &p_peek, VLC_A52_MIN_HEADER_SIZE ) >= VLC_A52_MIN_HEADER_SIZE )
1547
1.15k
    {
1548
1.15k
        A52CheckSync( p_peek, &p_sys->b_big_endian, NULL, true );
1549
1.15k
    }
1550
1.15k
    return VLC_SUCCESS;
1551
1.15k
}
1552
1553
/*****************************************************************************
1554
 * DTS
1555
 *****************************************************************************/
1556
static int DtsCheckSync( const uint8_t *p_peek, unsigned *pi_samples )
1557
12.6M
{
1558
12.6M
    VLC_UNUSED(pi_samples);
1559
1560
12.6M
    vlc_dts_header_t dts;
1561
12.6M
    if( vlc_dts_header_Parse( &dts, p_peek, VLC_DTS_HEADER_SIZE ) == VLC_SUCCESS
1562
22.5k
     && dts.i_frame_size > 0 && dts.i_frame_size <= 8192 )
1563
12.2k
    {
1564
12.2k
        if( pi_samples )
1565
11.4k
            *pi_samples = dts.i_frame_length;
1566
12.2k
        return dts.i_frame_size;
1567
12.2k
    }
1568
12.6M
    else
1569
12.6M
        return VLC_EGENERIC;
1570
12.6M
}
1571
1572
static int DtsProbe( demux_t *p_demux, uint64_t *pi_offset )
1573
2.48k
{
1574
2.48k
    const char *ppsz_name[] = { "dts", NULL };
1575
2.48k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_DTSINC_DTS, WAVE_FORMAT_UNKNOWN };
1576
1577
2.48k
    return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync,
1578
2.48k
                         VLC_DTS_HEADER_SIZE,
1579
2.48k
                         16384 + VLC_DTS_HEADER_SIZE + 1,
1580
2.48k
                         WAV_EXTRA_PROBE_SIZE,
1581
2.48k
                         false, rgi_twocc, NULL );
1582
2.48k
}
1583
static int DtsInit( demux_t *p_demux )
1584
1.53k
{
1585
1.53k
    demux_sys_t *p_sys = p_demux->p_sys;
1586
1587
1.53k
    p_sys->i_packet_size = 16384;
1588
1589
1.53k
    return VLC_SUCCESS;
1590
1.53k
}
1591
1592
/*****************************************************************************
1593
 * MLP
1594
 *****************************************************************************/
1595
static int MlpCheckSync( const uint8_t *p_peek, unsigned *pi_samples )
1596
8.29M
{
1597
8.29M
    if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
1598
8.13M
        return -1;
1599
1600
160k
    if( p_peek[4+3] != 0xbb )
1601
160k
        return -1;
1602
1603
    /* TODO checksum and real size for robustness */
1604
65
    VLC_UNUSED(pi_samples);
1605
65
    return 0;
1606
160k
}
1607
static int ThdCheckSync( const uint8_t *p_peek, unsigned *pi_samples )
1608
175k
{
1609
175k
    if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
1610
174k
        return -1;
1611
1612
371
    if( p_peek[4+3] != 0xba )
1613
1
        return -1;
1614
1615
    /* TODO checksum and real size for robustness */
1616
370
    VLC_UNUSED(pi_samples);
1617
370
    return 0;
1618
371
}
1619
static int MlpProbe( demux_t *p_demux, uint64_t *pi_offset )
1620
950
{
1621
950
    const char *ppsz_name[] = { "mlp", NULL };
1622
950
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
1623
1624
950
    return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync,
1625
950
                         4+28+16*4, BASE_PROBE_SIZE, WAV_EXTRA_PROBE_SIZE,
1626
950
                         false, rgi_twocc, GenericFormatCheck );
1627
950
}
1628
static int ThdProbe( demux_t *p_demux, uint64_t *pi_offset )
1629
885
{
1630
885
    const char *ppsz_name[] = { "thd", NULL };
1631
885
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
1632
1633
885
    return GenericProbe( p_demux, pi_offset, ppsz_name, ThdCheckSync,
1634
885
                         4+28+16*4, BASE_PROBE_SIZE, WAV_EXTRA_PROBE_SIZE,
1635
885
                         false, rgi_twocc, GenericFormatCheck );
1636
885
}
1637
static int MlpInit( demux_t *p_demux )
1638
1639
435
{
1640
435
    demux_sys_t *p_sys = p_demux->p_sys;
1641
1642
435
    p_sys->i_packet_size = 4096;
1643
1644
435
    return VLC_SUCCESS;
1645
435
}
1646
1647
/*****************************************************************************
1648
 * Video
1649
 *****************************************************************************/
1650
static int VideoInit( demux_t *p_demux )
1651
0
{
1652
0
    demux_sys_t *p_sys = p_demux->p_sys;
1653
1654
0
    p_sys->i_packet_size = 4096;
1655
1656
0
    return VLC_SUCCESS;
1657
0
}