Coverage Report

Created: 2026-04-12 07:27

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
116
vlc_module_begin ()
63
58
    set_subcategory( SUBCAT_INPUT_DEMUX )
64
58
    set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
65
58
    set_shortname( N_("Audio ES") )
66
58
    set_capability( "demux", 155 )
67
116
    set_callbacks( OpenAudio, Close )
68
69
58
    add_shortcut( "mpga", "mp3",
70
58
                  "m4a", "mp4a", "aac",
71
58
                  "ac3", "a52",
72
58
                  "eac3",
73
58
                  "dts",
74
58
                  "mlp", "thd" )
75
76
58
    add_submodule()
77
58
    set_description( N_("MPEG-4 video" ) )
78
58
    set_capability( "demux", 7 )
79
116
    set_callbacks( OpenVideo, Close )
80
58
    add_float( "es-fps", 25, FPS_TEXT, FPS_LONGTEXT )
81
82
58
    add_shortcut( "m4v" )
83
58
    add_shortcut( "mp4v" )
84
58
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.67M
#define WAV_PROBE_SIZE (512*1024)
93
5.18k
#define BASE_PROBE_SIZE (8000)
94
18.5k
#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
108
#define XING_FIELD_STREAMFRAMES    (1 << 0)
132
108
#define XING_FIELD_STREAMBYTES     (1 << 1)
133
108
#define XING_FIELD_TOC             (1 << 2)
134
108
#define XING_FIELD_QUALITY         (1 << 3)
135
1.04k
#define XING_MIN_TAG_SIZE           42
136
96
#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
940
{
178
940
    if( i_buf < XING_MIN_TAG_SIZE )
179
0
        return VLC_EGENERIC;
180
181
940
    vlc_fourcc_t infotag = VLC_FOURCC(p_buf[0], p_buf[1], p_buf[2], p_buf[3]);
182
183
    /* L3Enc VBR info */
184
940
    if( infotag == VLC_FOURCC('V','B','R','I') )
185
38
    {
186
38
        if( GetWBE( &p_buf[4] ) != 0x0001 )
187
24
            return VLC_EGENERIC;
188
14
        xing->i_bytes = GetDWBE( &p_buf[10] );
189
14
        xing->i_frames = GetDWBE( &p_buf[14] );
190
14
        xing->infotag = infotag;
191
14
        return VLC_SUCCESS;
192
38
    }
193
    /* Xing VBR/CBR tags */
194
902
    else if( infotag != VLC_FOURCC('X','i','n','g') &&
195
855
             infotag != VLC_FOURCC('I','n','f','o') )
196
794
    {
197
794
        return VLC_EGENERIC;
198
794
    }
199
200
108
    xing->infotag = infotag;
201
202
108
    const uint32_t i_flags = GetDWBE( &p_buf[4] );
203
    /* compute our variable struct size for early checks */
204
108
    const unsigned varsz[4] = { ((i_flags & 0x01) ? 4 : 0),
205
108
                                ((i_flags & 0x02) ? 4 : 0),
206
108
                                ((i_flags & 0x04) ? XING_TOC_COUNTBYTES : 0),
207
108
                                ((i_flags & 0x08) ? 4 : 0) };
208
108
    const unsigned i_varallsz = varsz[0] + varsz[1] + varsz[2] + varsz[3];
209
108
    const unsigned i_tag_total = XING_MIN_TAG_SIZE + i_varallsz;
210
211
108
    if( i_buf < i_tag_total )
212
0
        return VLC_EGENERIC;
213
214
108
    if( i_flags & XING_FIELD_STREAMFRAMES )
215
74
        xing->i_frames = GetDWBE( &p_buf[8] );
216
108
    if( i_flags & XING_FIELD_STREAMBYTES )
217
92
        xing->i_bytes = GetDWBE( &p_buf[8 + varsz[0]] );
218
108
    if( i_flags & XING_FIELD_TOC )
219
48
        memcpy( xing->rgi_toc, &p_buf[8 + varsz[0] + varsz[1]], XING_TOC_COUNTBYTES );
220
108
    if( i_flags & XING_FIELD_QUALITY )
221
55
        xing->i_quality = GetDWBE( &p_buf[8 + varsz[0] + varsz[1] + varsz[2]] );
222
223
    /* pointer past optional members */
224
108
    const uint8_t *p_fixed = &p_buf[8 + i_varallsz];
225
226
    /* Original Xing encoder header stops here */
227
228
108
    xing->encoder = VLC_FOURCC(p_fixed[0], p_fixed[1], p_fixed[2], p_fixed[3]); /* char version[9] start */
229
230
108
    if( xing->encoder != VLC_FOURCC('L','A','M','E') &&
231
105
        xing->encoder != VLC_FOURCC('L','a','v','c') &&
232
105
        xing->encoder != VLC_FOURCC('L','a','v','f') )
233
103
        return VLC_SUCCESS;
234
235
5
    xing->brmode  = p_fixed[9] & 0x0f; /* version upper / mode lower */
236
5
    uint32_t peak_signal  = GetDWBE( &p_fixed[11] );
237
5
    xing->f_peak_signal = peak_signal / 8388608.0; /* pow(2, 23) */
238
5
    uint16_t gain = GetWBE( &p_fixed[15] );
239
5
    xing->f_radio_replay_gain = (gain & 0x1FF) / /* 9bits val stored x10 */
240
5
                                ((gain & 0x200) ? -10.0 : 10.0); /* -sign bit on bit 6 */
241
5
    gain = GetWBE( &p_fixed[17] );
242
5
    xing->f_radio_replay_gain = (gain & 0x1FF) / /* 9bits val stored x10 */
243
5
                                ((gain & 0x200) ? -10.0 : 10.0); /* -sign bit on bit 6 */
244
    /* flags @19 */
245
5
    xing->bitrate_avg = (p_fixed[20] != 0xFF) ? p_fixed[20] : 0; /* clipped to 255, so it's unknown from there */
246
5
    xing->i_delay_samples = (p_fixed[21] << 4) | (p_fixed[22] >> 4); /* upper 12bits */
247
5
    xing->i_padding_samples = ((p_fixed[22] & 0x0F) << 8) | p_fixed[23]; /* lower 12bits */
248
5
    xing->audiomode = (p_fixed[26] >> 3) & 0x07; /* over 16bits, 2b unused / 3b mode / 11b preset */
249
5
    xing->i_music_length  = GetDWBE( &p_fixed[28] );
250
251
5
    return VLC_SUCCESS;
252
108
}
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
3.62k
{
423
3.62k
    demux_sys_t *p_sys;
424
425
3.62k
    es_format_t fmt;
426
427
3.62k
    DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
428
3.62k
    memset( p_sys, 0, sizeof( demux_sys_t ) );
429
3.62k
    p_sys->codec = *p_codec;
430
3.62k
    p_sys->p_es = NULL;
431
3.62k
    p_sys->b_start = true;
432
3.62k
    p_sys->i_stream_offset = i_bs_offset;
433
3.62k
    p_sys->b_estimate_bitrate = true;
434
3.62k
    p_sys->i_bitrate = 0;
435
3.62k
    p_sys->i_duration = 0;
436
3.62k
    p_sys->b_big_endian = false;
437
3.62k
    p_sys->f_fps = var_InheritFloat( p_demux, "es-fps" );
438
3.62k
    p_sys->p_packetized_data = NULL;
439
3.62k
    p_sys->chapters.i_current = 0;
440
3.62k
    p_sys->xing.f_peak_signal = NAN;
441
3.62k
    p_sys->xing.f_radio_replay_gain = NAN;
442
3.62k
    p_sys->xing.f_audiophile_replay_gain = NAN;
443
3.62k
    TAB_INIT(p_sys->chapters.i_count, p_sys->chapters.p_entry);
444
445
3.62k
    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
3.62k
    if( p_sys->codec.pf_init( p_demux ) )
452
0
    {
453
0
        free( p_sys );
454
0
        return VLC_EGENERIC;
455
0
    }
456
457
3.62k
    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
3.62k
    msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
464
465
    /* Load the audio packetizer */
466
3.62k
    es_format_Init( &fmt, i_cat, p_sys->codec.i_codec );
467
3.62k
    fmt.i_original_fourcc = p_sys->i_original;
468
3.62k
    p_sys->p_packetizer = demux_PacketizerNew( VLC_OBJECT(p_demux), &fmt, p_sys->codec.psz_name );
469
3.62k
    if( !p_sys->p_packetizer )
470
0
    {
471
0
        free( p_sys );
472
0
        return VLC_EGENERIC;
473
0
    }
474
475
3.62k
    es_format_t *p_fmt = &p_sys->p_packetizer->fmt_out;
476
3.62k
    replay_gain_Merge( &p_fmt->audio_replay_gain, &p_sys->audio_replay_gain );
477
478
3.62k
    for( ;; )
479
56.6k
    {
480
56.6k
        if( Parse( p_demux, &p_sys->p_packetized_data ) )
481
1.14k
            break;
482
55.5k
        if( p_sys->p_packetized_data )
483
2.48k
            break;
484
55.5k
    }
485
486
3.62k
    return VLC_SUCCESS;
487
3.62k
}
488
static int OpenAudio( vlc_object_t *p_this )
489
5.91k
{
490
5.91k
    demux_t *p_demux = (demux_t*)p_this;
491
32.6k
    for( int i = 0; p_codecs[i].i_codec != 0; i++ )
492
30.3k
    {
493
30.3k
        uint64_t i_offset;
494
30.3k
        if( !p_codecs[i].pf_probe( p_demux, &i_offset ) )
495
3.62k
            return OpenCommon( p_demux, AUDIO_ES, &p_codecs[i], i_offset );
496
30.3k
    }
497
2.28k
    return VLC_EGENERIC;
498
5.91k
}
499
static int OpenVideo( vlc_object_t *p_this )
500
979
{
501
979
    demux_t *p_demux = (demux_t*)p_this;
502
503
    /* Only m4v is supported for the moment */
504
979
    bool b_m4v_ext    = demux_IsPathExtension( p_demux, ".m4v" );
505
979
    bool b_re4_ext    = !b_m4v_ext && demux_IsPathExtension( p_demux, ".re4" );
506
979
    bool b_m4v_forced = demux_IsForced( p_demux, "m4v" ) ||
507
979
                        demux_IsForced( p_demux, "mp4v" );
508
509
979
    if( !b_m4v_ext && !b_m4v_forced && !b_re4_ext )
510
979
        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
55.2k
{
528
55.2k
    demux_sys_t *p_sys = p_demux->p_sys;
529
55.2k
    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
55.2k
}
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
55.2k
{
547
55.2k
    int ret = 1;
548
55.2k
    demux_sys_t *p_sys = p_demux->p_sys;
549
550
55.2k
    block_t *p_block_out = p_sys->p_packetized_data;
551
55.2k
    if( p_block_out )
552
2.66k
        p_sys->p_packetized_data = NULL;
553
52.5k
    else
554
52.5k
        ret = Parse( p_demux, &p_block_out ) ? 0 : 1;
555
556
    /* Update chapter if any */
557
55.2k
    IncreaseChapter( p_demux,
558
55.2k
                     p_block_out ? p_sys->i_time_offset + p_block_out->i_dts
559
55.2k
                                 : VLC_TICK_INVALID );
560
561
1.51M
    while( p_block_out )
562
1.46M
    {
563
1.46M
        block_t *p_next = p_block_out->p_next;
564
565
        /* Correct timestamp */
566
1.46M
        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.46M
        else
575
1.46M
        {
576
1.46M
            p_sys->i_pts = p_block_out->i_pts - VLC_TICK_0;
577
1.46M
        }
578
579
1.46M
        if( p_block_out->i_pts != VLC_TICK_INVALID )
580
1.45M
        {
581
1.45M
            p_block_out->i_pts += p_sys->i_time_offset;
582
1.45M
        }
583
1.46M
        if( p_block_out->i_dts != VLC_TICK_INVALID )
584
1.45M
        {
585
1.45M
            p_block_out->i_dts += p_sys->i_time_offset;
586
1.45M
            es_out_SetPCR( p_demux->out, p_block_out->i_dts );
587
1.45M
        }
588
        /* Re-estimate bitrate */
589
1.46M
        if( p_sys->b_estimate_bitrate && p_sys->i_pts > VLC_TICK_FROM_MS(500) )
590
1.41M
            p_sys->i_bitrate = 8 * CLOCK_FREQ * p_sys->i_bytes
591
1.41M
                                   / (p_sys->i_pts - 1);
592
1.46M
        p_sys->i_bytes += p_block_out->i_buffer;
593
594
595
1.46M
        p_block_out->p_next = NULL;
596
1.46M
        if( likely(p_sys->p_es) )
597
1.46M
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
598
0
        else
599
0
            block_Release( p_block_out );
600
601
1.46M
        p_block_out = p_next;
602
1.46M
    }
603
55.2k
    return ret;
604
55.2k
}
605
606
/*****************************************************************************
607
 * Close: frees unused data
608
 *****************************************************************************/
609
static void Close( vlc_object_t * p_this )
610
3.62k
{
611
3.62k
    demux_t     *p_demux = (demux_t*)p_this;
612
3.62k
    demux_sys_t *p_sys = p_demux->p_sys;
613
614
3.62k
    if( p_sys->p_packetized_data )
615
0
        block_ChainRelease( p_sys->p_packetized_data );
616
3.62k
    for( size_t i=0; i< p_sys->chapters.i_count; i++ )
617
0
        vlc_seekpoint_Delete( p_sys->chapters.p_entry[i].p_seekpoint );
618
3.62k
    TAB_CLEAN( p_sys->chapters.i_count, p_sys->chapters.p_entry );
619
3.62k
    if( p_sys->mllt.p_bits )
620
0
        free( p_sys->mllt.p_bits );
621
3.62k
    demux_PacketizerDestroy( p_sys->p_packetizer );
622
3.62k
    free( p_sys );
623
3.62k
}
624
625
/*****************************************************************************
626
 * Time seek:
627
 *****************************************************************************/
628
static void PostSeekCleanup( demux_sys_t *p_sys, vlc_tick_t i_time )
629
0
{
630
    /* Fix time_offset */
631
0
    if( i_time >= 0 )
632
0
        p_sys->i_time_offset = i_time - p_sys->i_pts;
633
    /* And reset buffered data */
634
0
    if( p_sys->p_packetized_data )
635
0
        block_ChainRelease( p_sys->p_packetized_data );
636
0
    p_sys->p_packetized_data = NULL;
637
    /* Reset chapter if any */
638
0
    p_sys->chapters.i_current = 0;
639
0
    p_sys->i_demux_flags |= INPUT_UPDATE_SEEKPOINT;
640
0
}
641
642
static int MovetoTimePos( demux_t *p_demux, vlc_tick_t i_time, uint64_t i_pos )
643
0
{
644
0
    demux_sys_t *p_sys  = p_demux->p_sys;
645
0
    int i_ret = vlc_stream_Seek( p_demux->s, p_sys->i_stream_offset + i_pos );
646
0
    if( i_ret != VLC_SUCCESS )
647
0
        return i_ret;
648
0
    PostSeekCleanup( p_sys, i_time );
649
0
    return VLC_SUCCESS;
650
0
}
651
652
/*****************************************************************************
653
 * Control:
654
 *****************************************************************************/
655
static int Control( demux_t *p_demux, int i_query, va_list args )
656
0
{
657
0
    demux_sys_t *p_sys  = p_demux->p_sys;
658
0
    bool *pb_bool;
659
660
0
    switch( i_query )
661
0
    {
662
0
        case DEMUX_HAS_UNSUPPORTED_META:
663
0
            pb_bool = va_arg( args, bool * );
664
0
            *pb_bool = true;
665
0
            return VLC_SUCCESS;
666
667
0
        case DEMUX_GET_TIME:
668
0
            *va_arg( args, vlc_tick_t * ) = p_sys->i_pts + p_sys->i_time_offset;
669
0
            return VLC_SUCCESS;
670
671
0
        case DEMUX_GET_LENGTH:
672
0
        {
673
0
            if( p_sys->i_duration > 0 )
674
0
            {
675
0
                *va_arg( args, vlc_tick_t * ) = p_sys->i_duration;
676
0
                return VLC_SUCCESS;
677
0
            }
678
            /* compute length from bitrate */
679
0
            va_list ap;
680
0
            int i_ret;
681
682
0
            va_copy ( ap, args );
683
0
            i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset,
684
0
                                    -1, p_sys->i_bitrate, 1, i_query, ap );
685
0
            va_end( ap );
686
687
            /* No bitrate, we can't have it precisely, but we can compute
688
             * a raw approximation with time/position */
689
0
            if( i_ret && !p_sys->i_bitrate )
690
0
            {
691
0
                uint64_t i_stream_size;
692
0
                if( vlc_stream_GetSize( p_demux->s, &i_stream_size ) != VLC_SUCCESS )
693
0
                    return VLC_EGENERIC;
694
695
0
                uint64_t i_pos = vlc_stream_Tell( p_demux->s );
696
0
                float f_pos = (double)i_pos / (double)i_stream_size;
697
                /* The first few seconds are guaranteed to be very whacky,
698
                 * don't bother trying ... Too bad */
699
0
                if( f_pos < 0.01f ||
700
0
                    (p_sys->i_pts + p_sys->i_time_offset) < VLC_TICK_FROM_SEC(8) )
701
0
                {
702
0
                    return VLC_EGENERIC;
703
0
                }
704
705
0
                *va_arg( args, vlc_tick_t * ) =
706
0
                    (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
707
0
                return VLC_SUCCESS;
708
0
            }
709
0
            return i_ret;
710
0
        }
711
712
0
        case DEMUX_SET_TIME:
713
0
        case DEMUX_SET_POSITION:
714
0
        {
715
0
            vlc_tick_t i_time;
716
0
            double f_pos;
717
0
            uint64_t i_offset;
718
719
0
            va_list ap;
720
0
            va_copy ( ap, args ); /* don't break args for helper fallback */
721
0
            if( i_query == DEMUX_SET_TIME )
722
0
            {
723
0
                i_time = va_arg(ap, vlc_tick_t);
724
0
                f_pos = p_sys->i_duration ? i_time / (double) p_sys->i_duration : -1.0;
725
0
                if( f_pos > 1.0 )
726
0
                    f_pos = 1.0;
727
0
            }
728
0
            else
729
0
            {
730
0
                f_pos = va_arg(ap, double);
731
0
                i_time = p_sys->i_duration ? p_sys->i_duration * f_pos : VLC_TICK_INVALID;
732
0
            }
733
0
            va_end( ap );
734
735
            /* Try to use ID3 table */
736
0
            if( !SeekByMlltTable( &p_sys->mllt, &i_time, &i_offset ) )
737
0
                return MovetoTimePos( p_demux, i_time, i_offset );
738
739
0
            if( p_sys->codec.i_codec == VLC_CODEC_MPGA )
740
0
            {
741
0
                uint64_t streamsize;
742
0
                if( !vlc_stream_GetSize( p_demux->s, &streamsize ) &&
743
0
                    streamsize > p_sys->i_stream_offset )
744
0
                    streamsize -= p_sys->i_stream_offset;
745
0
                else
746
0
                    streamsize = 0;
747
748
0
                if( !MpgaSeek( &p_sys->mpgah, &p_sys->xing,
749
0
                           streamsize, f_pos, &i_time, &i_offset ) )
750
0
                {
751
0
                    return MovetoTimePos( p_demux, i_time, i_offset );
752
0
                }
753
0
            }
754
755
            /* fallback on bitrate / file position seeking */
756
0
            i_time = VLC_TICK_INVALID;
757
0
            int ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
758
0
                                             p_sys->i_bitrate, 1, i_query, args );
759
0
            if( ret != VLC_SUCCESS )
760
0
                return ret; /* not much we can do */
761
762
0
            if( p_sys->i_bitrate > 0 )
763
0
            {
764
0
                i_offset = vlc_stream_Tell( p_demux->s ); /* new pos */
765
0
                i_time = VLC_TICK_0;
766
0
                if( likely(i_offset > p_sys->i_stream_offset) )
767
0
                {
768
0
                    i_offset -= p_sys->i_stream_offset;
769
0
                    i_time += vlc_tick_from_samples( i_offset * 8, p_sys->i_bitrate );
770
0
                }
771
0
            }
772
0
            PostSeekCleanup( p_sys, i_time );
773
774
            /* FIXME TODO: implement a high precision seek (with mp3 parsing)
775
             * needed for multi-input */
776
0
            return VLC_SUCCESS;
777
0
        }
778
779
0
        case DEMUX_GET_TITLE_INFO:
780
0
        {
781
0
            if( p_sys->chapters.i_count == 0 )
782
0
                return VLC_EGENERIC;
783
0
            input_title_t **pp_title = malloc( sizeof(*pp_title) );
784
0
            if( !pp_title )
785
0
                return VLC_EGENERIC;
786
0
            *pp_title = vlc_input_title_New();
787
0
            if( !*pp_title )
788
0
            {
789
0
                free( pp_title );
790
0
                return VLC_EGENERIC;
791
0
            }
792
0
            (*pp_title)->seekpoint = vlc_alloc( p_sys->chapters.i_count, sizeof(seekpoint_t *) );
793
0
            if( !(*pp_title)->seekpoint )
794
0
            {
795
0
                free( *pp_title );
796
0
                free( pp_title );
797
0
                return VLC_EGENERIC;
798
0
            }
799
0
            for( size_t i=0; i<p_sys->chapters.i_count; i++ )
800
0
            {
801
0
                seekpoint_t *s = vlc_seekpoint_Duplicate( p_sys->chapters.p_entry[i].p_seekpoint );
802
0
                if( s )
803
0
                    (*pp_title)->seekpoint[(*pp_title)->i_seekpoint++] = s;
804
0
            }
805
0
            *(va_arg( args, input_title_t *** )) = pp_title;
806
0
            *(va_arg( args, int* )) = 1;
807
0
            *(va_arg( args, int* )) = 0;
808
0
            *(va_arg( args, int* )) = 0;
809
0
            return VLC_SUCCESS;
810
0
        }
811
0
        break;
812
813
0
        case DEMUX_GET_TITLE:
814
0
            if( p_sys->chapters.i_count == 0 )
815
0
                return VLC_EGENERIC;
816
0
            *(va_arg( args, int* )) = 0;
817
0
            return VLC_SUCCESS;
818
819
0
        case DEMUX_GET_SEEKPOINT:
820
0
            if( p_sys->chapters.i_count == 0 )
821
0
                return VLC_EGENERIC;
822
0
            *(va_arg( args, int* )) = p_sys->chapters.i_current;
823
0
            return VLC_SUCCESS;
824
825
0
        case DEMUX_SET_TITLE:
826
0
            return va_arg( args, int) == 0 ? VLC_SUCCESS : VLC_EGENERIC;
827
828
0
        case DEMUX_SET_SEEKPOINT:
829
0
        {
830
0
            int i = va_arg( args, int );
831
0
            if( (size_t)i>=p_sys->chapters.i_count )
832
0
                return VLC_EGENERIC;
833
0
            const chap_entry_t *p = &p_sys->chapters.p_entry[i];
834
0
            if( p_sys->chapters.p_entry[i].i_offset == UINT32_MAX )
835
0
                return demux_Control( p_demux, DEMUX_SET_TIME, p->p_seekpoint->i_time_offset );
836
0
            int i_ret= MovetoTimePos( p_demux, p->p_seekpoint->i_time_offset, p->i_offset );
837
0
            if( i_ret == VLC_SUCCESS )
838
0
                p_sys->chapters.i_current = i;
839
0
            return i_ret;
840
0
        }
841
842
0
        case DEMUX_TEST_AND_CLEAR_FLAGS:
843
0
        {
844
0
            unsigned *restrict flags = va_arg( args, unsigned * );
845
0
            *flags &= p_sys->i_demux_flags;
846
0
            p_sys->i_demux_flags &= ~*flags;
847
0
            return VLC_SUCCESS;
848
0
        }
849
0
    }
850
851
0
    return demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
852
0
                                  p_sys->i_bitrate, 1, i_query, args );
853
0
}
854
855
/*****************************************************************************
856
 * Makes a link list of buffer of parsed data
857
 * Returns true if EOF
858
 *****************************************************************************/
859
static bool Parse( demux_t *p_demux, block_t **pp_output )
860
109k
{
861
109k
    demux_sys_t *p_sys = p_demux->p_sys;
862
109k
    block_t *p_block_in, *p_block_out;
863
864
109k
    *pp_output = NULL;
865
866
109k
    if( p_sys->codec.b_use_word )
867
49.8k
    {
868
        /* Make sure we are word aligned */
869
49.8k
        int64_t i_pos = vlc_stream_Tell( p_demux->s );
870
49.8k
        if( (i_pos & 1) && vlc_stream_Read( p_demux->s, NULL, 1 ) != 1 )
871
606
            return true;
872
49.8k
    }
873
874
108k
    p_block_in = vlc_stream_Block( p_demux->s, p_sys->i_packet_size );
875
108k
    bool b_eof = p_block_in == NULL;
876
877
108k
    if( p_block_in )
878
104k
    {
879
104k
        if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
880
14.5k
        {
881
            /* Convert to big endian */
882
14.5k
            block_t *old = p_block_in;
883
14.5k
            p_block_in = block_Alloc( p_block_in->i_buffer );
884
14.5k
            if( p_block_in )
885
14.5k
            {
886
14.5k
                block_CopyProperties( p_block_in, old );
887
14.5k
                swab( old->p_buffer, p_block_in->p_buffer, old->i_buffer );
888
14.5k
            }
889
14.5k
            block_Release( old );
890
14.5k
        }
891
892
104k
        if( p_block_in )
893
104k
        {
894
104k
            p_block_in->i_pts =
895
104k
            p_block_in->i_dts = (p_sys->b_start || p_sys->b_initial_sync_failed) ?
896
104k
                                 VLC_TICK_0 : VLC_TICK_INVALID;
897
104k
        }
898
104k
    }
899
108k
    p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
900
901
108k
    decoder_t *p_packetizer = p_sys->p_packetizer;
902
1.56M
    while( ( p_block_out = p_packetizer->pf_packetize( p_packetizer, p_block_in ? &p_block_in : NULL ) ) )
903
1.46M
    {
904
1.46M
        p_sys->b_initial_sync_failed = false;
905
2.92M
        while( p_block_out )
906
1.46M
        {
907
1.46M
            if( !p_sys->p_es )
908
2.66k
            {
909
2.66k
                es_format_t fmt;
910
2.66k
                es_format_Init( &fmt, p_packetizer->fmt_out.i_cat, p_packetizer->fmt_out.i_codec );
911
2.66k
                es_format_Copy( &fmt, &p_packetizer->fmt_out );
912
913
2.66k
                switch( p_sys->xing.audiomode )
914
2.66k
                {
915
1
                    case XING_AUDIOMODE_AMBISONICS:
916
1
                        fmt.audio.channel_type = AUDIO_CHANNEL_TYPE_AMBISONICS;
917
1
                        break;
918
2.66k
                    default:
919
2.66k
                        break;
920
2.66k
                }
921
922
2.66k
                fmt.b_packetized = true;
923
2.66k
                fmt.i_id = 0;
924
2.66k
                p_sys->p_es = es_out_Add( p_demux->out, &fmt );
925
926
                /* Use the bitrate as initual value */
927
2.66k
                if( p_sys->b_estimate_bitrate )
928
2.62k
                    p_sys->i_bitrate = fmt.i_bitrate;
929
930
2.66k
                es_format_Clean( &fmt );
931
2.66k
            }
932
933
1.46M
            block_t *p_next = p_block_out->p_next;
934
1.46M
            p_block_out->p_next = NULL;
935
936
1.46M
            block_ChainLastAppend( &pp_output, p_block_out );
937
938
1.46M
            p_block_out = p_next;
939
1.46M
        }
940
1.46M
    }
941
942
108k
    if( p_sys->b_initial_sync_failed )
943
108k
        msg_Dbg( p_demux, "did not sync on first block" );
944
108k
    p_sys->b_start = false;
945
946
108k
    return b_eof;
947
108k
}
948
949
/* Check to apply to WAVE fmt header */
950
static int GenericFormatCheck( int i_format, const uint8_t *p_head )
951
2.95k
{
952
2.95k
    if ( i_format == WAVE_FORMAT_PCM )
953
2.22k
    {
954
2.22k
        if( GetWLE( p_head /* nChannels */ ) != 2 )
955
534
            return VLC_EGENERIC;
956
1.69k
        if( GetDWLE( p_head + 2 /* nSamplesPerSec */ ) != 44100 )
957
282
            return VLC_EGENERIC;
958
1.69k
    }
959
2.13k
    return VLC_SUCCESS;
960
2.95k
}
961
962
/*****************************************************************************
963
 * Wav header skipper
964
 *****************************************************************************/
965
static int WavSkipHeader( demux_t *p_demux, uint64_t *pi_skip,
966
                          const uint16_t rgi_twocc[],
967
                          int (*pf_format_check)( int, const uint8_t * ) )
968
24.4k
{
969
24.4k
    const uint8_t *p_peek;
970
24.4k
    size_t i_peek = 0;
971
24.4k
    uint32_t i_len;
972
973
    /* */
974
24.4k
    *pi_skip = 0;
975
976
    /* Check if we are dealing with a WAV file */
977
24.4k
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
978
0
        return VLC_SUCCESS;
979
980
24.4k
    if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
981
19.0k
        return VLC_SUCCESS;
982
983
    /* Find the wave format header */
984
5.40k
    i_peek = 12 + 8;
985
365k
    while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
986
360k
    {
987
360k
        i_len = GetDWLE( p_peek + i_peek - 4 );
988
360k
        if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
989
192
            return VLC_EGENERIC;
990
991
359k
        i_peek += i_len + 8;
992
359k
        if( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) != (ssize_t) i_peek )
993
78
            return VLC_EGENERIC;
994
359k
    }
995
996
    /* Sanity check the wave format header */
997
5.13k
    i_len = GetDWLE( p_peek + i_peek - 4 );
998
5.13k
    if( i_len > WAV_PROBE_SIZE )
999
0
        return VLC_EGENERIC;
1000
1001
5.13k
    i_peek += i_len + 8;
1002
5.13k
    if( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) != (ssize_t) i_peek )
1003
122
        return VLC_EGENERIC;
1004
5.01k
    const uint16_t i_twocc = GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ );
1005
5.01k
    int i_format_idx;
1006
8.30k
    for( i_format_idx = 0; rgi_twocc[i_format_idx] != WAVE_FORMAT_UNKNOWN; i_format_idx++ )
1007
7.02k
    {
1008
7.02k
        if( i_twocc == rgi_twocc[i_format_idx] )
1009
3.73k
            break;
1010
7.02k
    }
1011
5.01k
    if( rgi_twocc[i_format_idx] == WAVE_FORMAT_UNKNOWN )
1012
1.28k
        return VLC_EGENERIC;
1013
1014
3.73k
    if( pf_format_check &&
1015
2.95k
        pf_format_check( i_twocc, p_peek + i_peek - i_len - 6 ) != VLC_SUCCESS )
1016
816
            return VLC_EGENERIC;
1017
1018
    /* Skip the wave header */
1019
200k
    while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
1020
197k
    {
1021
197k
        i_len = GetDWLE( p_peek + i_peek - 4 );
1022
197k
        if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
1023
18
            return VLC_EGENERIC;
1024
1025
197k
        i_peek += i_len + 8;
1026
197k
        if( vlc_stream_Peek( p_demux->s, &p_peek, i_peek ) != (ssize_t) i_peek )
1027
27
            return VLC_EGENERIC;
1028
197k
    }
1029
2.87k
    *pi_skip = i_peek;
1030
2.87k
    return VLC_SUCCESS;
1031
2.91k
}
1032
1033
static int GenericProbe( demux_t *p_demux, uint64_t *pi_offset,
1034
                         const char * ppsz_name[],
1035
                         int (*pf_check)( const uint8_t *, unsigned * ),
1036
                         unsigned i_check_size,
1037
                         unsigned i_base_probing,
1038
                         unsigned i_wav_extra_probing,
1039
                         bool b_use_word,
1040
                         const uint16_t pi_twocc[],
1041
                         int (*pf_format_check)( int, const uint8_t * ) )
1042
18.5k
{
1043
18.5k
    bool   b_forced_demux;
1044
1045
18.5k
    uint64_t i_offset;
1046
18.5k
    const uint8_t *p_peek;
1047
18.5k
    uint64_t i_skip;
1048
1049
18.5k
    b_forced_demux = false;
1050
42.0k
    for( size_t i = 0; ppsz_name[i] != NULL; i++ )
1051
23.5k
    {
1052
23.5k
        b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
1053
23.5k
    }
1054
1055
18.5k
    i_offset = vlc_stream_Tell( p_demux->s );
1056
1057
18.5k
    if( WavSkipHeader( p_demux, &i_skip, pi_twocc, pf_format_check ) )
1058
1.21k
    {
1059
1.21k
        if( !b_forced_demux )
1060
1.12k
            return VLC_EGENERIC;
1061
1.21k
    }
1062
17.4k
    const bool b_wav = i_skip > 0;
1063
1064
    /* peek the beginning
1065
     * It is common that wav files have some sort of garbage at the beginning
1066
     * We will accept probing 0.5s of data in this case.
1067
     */
1068
17.4k
    const size_t i_probe = i_skip + i_check_size + i_base_probing + ( b_wav ? i_wav_extra_probing : 0);
1069
17.4k
    const ssize_t i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_probe );
1070
1071
17.4k
    if( i_peek < 0 || (size_t)i_peek < i_skip + i_check_size )
1072
0
        return VLC_EGENERIC;
1073
1074
17.4k
    for( ;; )
1075
32.4M
    {
1076
        /* i_peek > 0 so we can cast into size_t. */
1077
32.4M
        if( i_skip + i_check_size > (size_t)i_peek )
1078
1.81k
        {
1079
1.81k
            if( !b_forced_demux )
1080
1.69k
                return VLC_EGENERIC;
1081
121
            break;
1082
1.81k
        }
1083
32.4M
        unsigned i_samples = 0;
1084
32.4M
        int i_size = pf_check( &p_peek[i_skip], &i_samples );
1085
32.4M
        if( i_size >= 0 )
1086
37.8k
        {
1087
37.8k
            if( i_size == 0 || /* 0 sized frame ?? */
1088
37.4k
                i_skip == 0 /* exact match from start, we're not WAVE either, so skip multiple checks (would break if padding) */ )
1089
1.55k
                break;
1090
1091
            /* If we have the frame size, check the next frame for
1092
             * extra robustness
1093
             * The second test is because some .wav have paddings
1094
             */
1095
37.8k
            bool b_ok = false;
1096
99.9k
            for( int t = 0; t < 1 + !!b_wav; t++ )
1097
66.8k
            {
1098
66.8k
                if( t == 1 )
1099
30.5k
                {
1100
30.5k
                    if(!i_samples)
1101
2.18k
                        break;
1102
28.3k
                    i_size = i_samples * 2 * 2;
1103
28.3k
                }
1104
1105
64.6k
                if( i_skip + i_check_size + i_size <= (size_t)i_peek )
1106
53.5k
                {
1107
53.5k
                    b_ok = pf_check( &p_peek[i_skip+i_size], NULL ) >= 0;
1108
53.5k
                    if( b_ok )
1109
977
                        break;
1110
53.5k
                }
1111
64.6k
            }
1112
36.2k
            if( b_ok )
1113
977
                break;
1114
36.2k
        }
1115
32.4M
        if( b_use_word )
1116
14.7M
            i_skip += ((i_offset + i_skip) % 2 == 0) ? 2 : 1;
1117
17.6M
        else
1118
17.6M
            i_skip++;
1119
32.4M
        if( !b_wav && !b_forced_demux )
1120
13.1k
            return VLC_EGENERIC;
1121
32.4M
    }
1122
1123
2.65k
    *pi_offset = i_offset + i_skip;
1124
2.65k
    return VLC_SUCCESS;
1125
17.4k
}
1126
1127
/*****************************************************************************
1128
 * Mpeg I/II Audio
1129
 *****************************************************************************/
1130
static int MpgaCheckSync( const uint8_t *p_peek )
1131
284k
{
1132
284k
    uint32_t h = GetDWBE( p_peek );
1133
1134
284k
    if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
1135
42.6k
        || (((h >> 19)&0x03) == 1 )         /* valid version ID ? */
1136
42.4k
        || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
1137
32.9k
        || (((h >> 12)&0x0F) == 0x0F )      /* valid bitrate ?*/
1138
4.08k
        || (((h >> 10) & 0x03) == 0x03 )    /* valid sampling freq ? */
1139
2.00k
        || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
1140
282k
    {
1141
282k
        return false;
1142
282k
    }
1143
1.88k
    return true;
1144
284k
}
1145
1146
940
#define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
1147
940
#define MPGA_MODE(h)        (((h)>> 6)&0x03)
1148
1149
static int MpgaProbe( demux_t *p_demux, uint64_t *pi_offset )
1150
5.91k
{
1151
5.91k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_MPEG, WAVE_FORMAT_MPEGLAYER3, WAVE_FORMAT_UNKNOWN };
1152
5.91k
    bool   b_forced;
1153
5.91k
    bool   b_forced_demux;
1154
5.91k
    uint64_t i_offset;
1155
1156
5.91k
    const uint8_t *p_peek;
1157
5.91k
    uint64_t i_skip;
1158
5.91k
    ssize_t i_peek;
1159
1160
5.91k
    b_forced = demux_IsPathExtension( p_demux, ".mp3" );
1161
5.91k
    b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
1162
4.46k
                     demux_IsForced( p_demux, "mpga" );
1163
1164
5.91k
    i_offset = vlc_stream_Tell( p_demux->s );
1165
1166
5.91k
    if( WavSkipHeader( p_demux, &i_skip, rgi_twocc, NULL ) )
1167
1.32k
    {
1168
1.32k
        if( !b_forced_demux )
1169
258
            return VLC_EGENERIC;
1170
1171
1.06k
        return VLC_EGENERIC;
1172
1.32k
    }
1173
1174
4.59k
    i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_skip + 4 );
1175
4.59k
    if( i_peek <= 0 || (uint64_t) i_peek < i_skip + 4 )
1176
0
        return VLC_EGENERIC;
1177
1178
4.59k
    if( !MpgaCheckSync( &p_peek[i_skip] ) )
1179
3.82k
    {
1180
3.82k
        bool b_ok = false;
1181
1182
3.82k
        if( !b_forced_demux && !b_forced )
1183
3.61k
            return VLC_EGENERIC;
1184
1185
213
        i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_skip + 8096 );
1186
279k
        while( i_peek > 0 && i_skip + 4 < (uint64_t) i_peek )
1187
279k
        {
1188
279k
            if( MpgaCheckSync( &p_peek[i_skip] ) )
1189
177
            {
1190
177
                b_ok = true;
1191
177
                break;
1192
177
            }
1193
278k
            i_skip++;
1194
278k
        }
1195
213
        if( !b_ok && !b_forced_demux )
1196
0
            return VLC_EGENERIC;
1197
213
    }
1198
976
    *pi_offset = i_offset + i_skip;
1199
976
    return VLC_SUCCESS;
1200
4.59k
}
1201
1202
static int SeekByMlltTable( sync_table_t *mllt, vlc_tick_t *pi_time, uint64_t *pi_offset )
1203
0
{
1204
0
    if( !mllt->p_bits )
1205
0
        return -1;
1206
1207
0
    sync_table_ctx_t *p_cur = &mllt->current;
1208
1209
    /* reset or init context */
1210
0
    if( *pi_time < p_cur->i_time || !p_cur->br.p )
1211
0
    {
1212
0
        p_cur->i_time = 0;
1213
0
        p_cur->i_pos = 0;
1214
0
        bs_init(&p_cur->br, mllt->p_bits, mllt->i_bits);
1215
0
    }
1216
1217
0
    while(!bs_eof(&p_cur->br))
1218
0
    {
1219
0
        const uint32_t i_bytesdev = bs_read(&p_cur->br, mllt->i_bits_per_bytes_dev);
1220
0
        const uint32_t i_msdev = bs_read(&p_cur->br, mllt->i_bits_per_ms_dev);
1221
0
        if(bs_error(&p_cur->br))
1222
0
            break;
1223
0
        const vlc_tick_t i_deltatime = VLC_TICK_FROM_MS(mllt->i_ms_btw_refs + i_msdev);
1224
0
        if( p_cur->i_time + i_deltatime > *pi_time )
1225
0
            break;
1226
0
        p_cur->i_time += i_deltatime;
1227
0
        p_cur->i_pos += mllt->i_bytes_btw_refs + i_bytesdev;
1228
0
    }
1229
0
    *pi_time = p_cur->i_time;
1230
0
    *pi_offset = p_cur->i_pos;
1231
1232
0
    return 0;
1233
0
}
1234
1235
static int ID3TAG_Parse_Handler( uint32_t i_tag, const uint8_t *p_payload, size_t i_payload, void *p_priv )
1236
0
{
1237
0
    demux_t *p_demux = (demux_t *) p_priv;
1238
0
    demux_sys_t *p_sys = p_demux->p_sys;
1239
1240
0
    if( i_tag == VLC_FOURCC('M', 'L', 'L', 'T') )
1241
0
    {
1242
0
        if( i_payload > 20 )
1243
0
        {
1244
0
            p_sys->mllt.i_frames_btw_refs = GetWBE(p_payload);
1245
0
            p_sys->mllt.i_bytes_btw_refs = GetDWBE(&p_payload[1]) & 0x00FFFFFF;
1246
0
            p_sys->mllt.i_ms_btw_refs = GetDWBE(&p_payload[4]) & 0x00FFFFFF;
1247
0
            if( !p_sys->mllt.i_frames_btw_refs || !p_sys->mllt.i_bytes_btw_refs ||
1248
0
                    !p_sys->mllt.i_ms_btw_refs ||
1249
0
                    p_payload[8] > 31 || p_payload[9] > 31 || /* bits length sanity check */
1250
0
                    ((p_payload[8] + p_payload[9]) % 4) || p_payload[8] + p_payload[9] < 4 )
1251
0
                return VLC_EGENERIC;
1252
0
            p_sys->mllt.i_bits_per_bytes_dev = p_payload[8];
1253
0
            p_sys->mllt.i_bits_per_ms_dev = p_payload[9];
1254
0
            p_sys->mllt.p_bits = malloc(i_payload - 10);
1255
0
            if( likely(p_sys->mllt.p_bits) )
1256
0
            {
1257
0
                p_sys->mllt.i_bits = i_payload - 10;
1258
0
                memcpy(p_sys->mllt.p_bits, &p_payload[10], p_sys->mllt.i_bits);
1259
0
                msg_Dbg(p_demux, "read MLLT sync table with %zu entries",
1260
0
                        (p_sys->mllt.i_bits * 8) / (p_sys->mllt.i_bits_per_bytes_dev + p_sys->mllt.i_bits_per_ms_dev) );
1261
0
            }
1262
0
        }
1263
0
        return VLC_EGENERIC;
1264
0
    }
1265
0
    else if( i_tag == VLC_FOURCC('T', 'X', 'X', 'X') )
1266
0
    {
1267
0
        vlc_meta_t *p_meta = vlc_meta_New();
1268
0
        if( p_meta )
1269
0
        {
1270
0
            bool b_updated;
1271
0
            if( ID3HandleTag( p_payload, i_payload, i_tag, p_meta, &b_updated ) )
1272
0
            {
1273
0
                vlc_replay_gain_CopyFromMeta( &p_sys->audio_replay_gain, p_meta );
1274
0
            }
1275
0
            vlc_meta_Delete( p_meta );
1276
0
        }
1277
0
    }
1278
0
    else if ( i_tag == VLC_FOURCC('C', 'H', 'A', 'P') && i_payload >= 17 )
1279
0
    {
1280
0
        char *psz_title = strndup( (const char *)p_payload, i_payload - 16 );
1281
0
        size_t i_offset = psz_title ? strlen( psz_title ) : 0;
1282
0
        if( p_payload[i_offset] != 0 )
1283
0
        {
1284
0
            free( psz_title );
1285
0
            return VLC_EGENERIC;
1286
0
        }
1287
0
        chap_entry_t e;
1288
0
        e.p_seekpoint = vlc_seekpoint_New();
1289
0
        if( e.p_seekpoint )
1290
0
        {
1291
0
            e.p_seekpoint->psz_name = psz_title;
1292
0
            e.p_seekpoint->i_time_offset = VLC_TICK_FROM_MS(GetDWBE(&p_payload[1 + i_offset]));
1293
0
            e.i_offset = GetDWBE(&p_payload[1 + i_offset + 8]);
1294
0
            p_payload += i_offset + 1 + 16;
1295
0
            i_payload -= i_offset + 1 + 16;
1296
0
            if( 12 < i_payload && !memcmp("TIT2", p_payload, 4) )
1297
0
            {
1298
0
                psz_title = NULL; /* optional alloc */
1299
0
                const char *psz = ID3TextConvert(&p_payload[10], i_payload-12, &psz_title);
1300
0
                if( psz ) /* replace with TIT2 */
1301
0
                {
1302
0
                    free( e.p_seekpoint->psz_name );
1303
0
                    e.p_seekpoint->psz_name = (psz_title) ? psz_title : strdup( psz );
1304
0
                }
1305
0
            }
1306
0
            TAB_APPEND(p_sys->chapters.i_count, p_sys->chapters.p_entry, e);
1307
0
        } else free( psz_title );
1308
0
    }
1309
1310
0
    return VLC_SUCCESS;
1311
0
}
1312
1313
static int ID3Parse( demux_t *p_demux,
1314
                     int (*pf_callback)(uint32_t, const uint8_t *, size_t, void *) )
1315
976
{
1316
976
    const block_t *p_tags = NULL;
1317
1318
976
    if( vlc_stream_Control( p_demux->s, STREAM_GET_TAGS, &p_tags ) != VLC_SUCCESS )
1319
976
        return VLC_EGENERIC;
1320
1321
0
    for( ; p_tags; p_tags = p_tags->p_next )
1322
0
        ID3TAG_Parse( p_tags->p_buffer, p_tags->i_buffer, pf_callback, (void *) p_demux );
1323
1324
0
    return VLC_SUCCESS;
1325
976
}
1326
1327
static int MpgaInit( demux_t *p_demux )
1328
976
{
1329
976
    demux_sys_t *p_sys = p_demux->p_sys;
1330
1331
976
    const uint8_t *p_peek;
1332
976
    int i_peek;
1333
1334
    /* */
1335
976
    p_sys->i_packet_size = 1024;
1336
1337
976
    ID3Parse( p_demux, ID3TAG_Parse_Handler );
1338
1339
    /* Load a potential xing header */
1340
976
    i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
1341
976
    if( i_peek < 4 + 21 )
1342
7
        return VLC_SUCCESS;
1343
1344
969
    const uint32_t header = GetDWBE( p_peek );
1345
969
    if( !MpgaCheckSync( p_peek ) || mpga_decode_frameheader( header, &p_sys->mpgah ) )
1346
29
        return VLC_SUCCESS;
1347
1348
940
    if( p_sys->mpgah.i_layer == 3 )
1349
343
        p_sys->codec.i_codec = VLC_CODEC_MP3;
1350
1351
    /* Xing header */
1352
940
    int i_skip;
1353
1354
940
    if( MPGA_VERSION( header ) == 0 )
1355
542
        i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
1356
398
    else
1357
398
        i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
1358
1359
940
    if( i_skip >= i_peek )
1360
0
        return VLC_SUCCESS;
1361
1362
940
    struct xing_info_s *xing = &p_sys->xing;
1363
940
    if( ParseXing( &p_peek[i_skip], i_peek - i_skip, xing ) == VLC_SUCCESS )
1364
122
    {
1365
122
        const uint64_t i_total_samples = xing->i_frames * (uint64_t) p_sys->mpgah.i_samples_per_frame;
1366
122
        const uint64_t i_dropped_samples = xing->i_delay_samples + xing->i_padding_samples;
1367
1368
122
        if( p_sys->mpgah.i_sample_rate && i_dropped_samples < i_total_samples )
1369
88
        {
1370
88
            uint64_t i_stream_size;
1371
            /* We only set duration if we can't check (then compute it using bitrate/size)
1372
               or if we verified the file isn't truncated */
1373
88
            if( xing->i_bytes == 0 ||
1374
84
                vlc_stream_GetSize( p_demux->s, &i_stream_size ) ||
1375
84
               (i_stream_size >= xing->i_bytes &&
1376
51
                i_stream_size <= xing->i_bytes + p_sys->mpgah.i_frame_size) )
1377
4
            {
1378
4
                p_sys->i_duration = vlc_tick_from_samples( i_total_samples - i_dropped_samples,
1379
4
                                                           p_sys->mpgah.i_sample_rate );
1380
4
            }
1381
88
        }
1382
1383
122
        unsigned i_bitrate = 0;
1384
122
        if( xing->brmode == XING_MODE_ABR || xing->brmode == XING_MODE_ABR_2PASS )
1385
1
            i_bitrate = xing->bitrate_avg * 1000;
1386
1387
122
        if( !i_bitrate && p_sys->mpgah.i_sample_rate &&
1388
121
            xing->i_bytes > p_sys->mpgah.i_frame_size )
1389
92
        {
1390
92
            unsigned d = vlc_tick_from_samples( i_total_samples, p_sys->mpgah.i_sample_rate );
1391
92
            if( d )
1392
71
                i_bitrate = (xing->i_bytes - p_sys->mpgah.i_frame_size) * 8 * CLOCK_FREQ / d;
1393
92
        }
1394
1395
122
        if( i_bitrate )
1396
43
        {
1397
43
            p_sys->i_bitrate = i_bitrate;
1398
43
            p_sys->b_estimate_bitrate = false;
1399
43
        }
1400
1401
122
        if( isfinite(xing->f_radio_replay_gain) )
1402
5
        {
1403
5
            p_sys->audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
1404
5
            p_sys->audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = xing->f_radio_replay_gain;
1405
5
        }
1406
1407
122
        if( isfinite(xing->f_peak_signal) )
1408
5
        {
1409
5
            p_sys->audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
1410
5
            p_sys->audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = xing->f_peak_signal;
1411
5
        }
1412
1413
122
        if( isfinite(xing->f_audiophile_replay_gain) )
1414
0
        {
1415
0
            p_sys->audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
1416
0
            p_sys->audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = xing->f_audiophile_replay_gain;
1417
0
        }
1418
1419
122
        msg_Dbg( p_demux, "Using '%4.4s' infotag"
1420
122
                          "(%"PRIu32" bytes, %"PRIu32" frames, %u samples/frame)",
1421
122
                          (char *) &xing->infotag,
1422
122
                          xing->i_bytes, xing->i_frames,
1423
122
                          p_sys->mpgah.i_samples_per_frame );
1424
1425
        /* We'll need to skip that part for playback
1426
         * and avoid using it as container frame could be different rate/size */
1427
122
        p_sys->i_stream_offset += p_sys->mpgah.i_frame_size;
1428
122
    }
1429
1430
940
    return VLC_SUCCESS;
1431
940
}
1432
1433
/*****************************************************************************
1434
 * AAC
1435
 *****************************************************************************/
1436
static int AacProbe( demux_t *p_demux, uint64_t *pi_offset )
1437
5.91k
{
1438
5.91k
    bool   b_forced;
1439
5.91k
    bool   b_forced_demux;
1440
1441
5.91k
    uint64_t i_offset;
1442
5.91k
    const uint8_t *p_peek;
1443
1444
5.91k
    b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
1445
5.91k
               demux_IsPathExtension( p_demux, ".aacp" );
1446
5.91k
    b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
1447
5.91k
                     demux_IsForced( p_demux, "aac" ) ||
1448
5.91k
                     demux_IsForced( p_demux, "mp4a" );
1449
1450
5.91k
    if( !b_forced_demux && !b_forced )
1451
5.91k
        return VLC_EGENERIC;
1452
1453
0
    i_offset = vlc_stream_Tell( p_demux->s );
1454
1455
    /* peek the beginning (10 is for adts header) */
1456
0
    if( vlc_stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
1457
0
        return VLC_EGENERIC;
1458
1459
0
    if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
1460
0
    {
1461
0
        msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
1462
0
        return VLC_EGENERIC;
1463
0
    }
1464
1465
0
    *pi_offset = i_offset;
1466
0
    return VLC_SUCCESS;
1467
0
}
1468
static int AacInit( demux_t *p_demux )
1469
0
{
1470
0
    demux_sys_t *p_sys = p_demux->p_sys;
1471
1472
0
    p_sys->i_packet_size = 4096;
1473
0
    p_sys->i_original = VLC_FOURCC('H','E','A','D');
1474
1475
0
    return VLC_SUCCESS;
1476
0
}
1477
1478
1479
/*****************************************************************************
1480
 * A52
1481
 *****************************************************************************/
1482
static int A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, unsigned *pi_samples, bool b_eac3 )
1483
14.8M
{
1484
14.8M
    vlc_a52_header_t header;
1485
14.8M
    uint8_t p_tmp[VLC_A52_MIN_HEADER_SIZE];
1486
1487
14.8M
    *p_big_endian =  p_peek[0] == 0x0b && p_peek[1] == 0x77;
1488
14.8M
    if( !*p_big_endian )
1489
14.7M
    {
1490
14.7M
        swab( p_peek, p_tmp, VLC_A52_MIN_HEADER_SIZE );
1491
14.7M
        p_peek = p_tmp;
1492
14.7M
    }
1493
1494
14.8M
    if( vlc_a52_header_Parse( &header, p_peek, VLC_A52_MIN_HEADER_SIZE ) )
1495
14.5M
        return VLC_EGENERIC;
1496
1497
342k
    if( !header.b_eac3 != !b_eac3 )
1498
313k
        return VLC_EGENERIC;
1499
28.8k
    if( pi_samples )
1500
27.6k
        *pi_samples = header.i_samples;
1501
28.8k
    return header.i_size;
1502
342k
}
1503
static int EA52CheckSyncProbe( const uint8_t *p_peek, unsigned *pi_samples )
1504
5.44M
{
1505
5.44M
    bool b_dummy;
1506
5.44M
    return A52CheckSync( p_peek, &b_dummy, pi_samples, true );
1507
5.44M
}
1508
1509
static int EA52Probe( demux_t *p_demux, uint64_t *pi_offset )
1510
4.59k
{
1511
4.59k
    const char *ppsz_name[] = { "eac3", NULL };
1512
4.59k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
1513
1514
4.59k
    return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe,
1515
4.59k
                         VLC_A52_MIN_HEADER_SIZE,
1516
4.59k
                         1920 + VLC_A52_MIN_HEADER_SIZE + 1,
1517
4.59k
                         WAV_EXTRA_PROBE_SIZE,
1518
4.59k
                         true, rgi_twocc, GenericFormatCheck );
1519
4.59k
}
1520
1521
static int A52CheckSyncProbe( const uint8_t *p_peek, unsigned *pi_samples )
1522
9.39M
{
1523
9.39M
    bool b_dummy;
1524
9.39M
    return A52CheckSync( p_peek, &b_dummy, pi_samples, false );
1525
9.39M
}
1526
1527
static int A52Probe( demux_t *p_demux, uint64_t *pi_offset )
1528
4.93k
{
1529
4.93k
    const char *ppsz_name[] = { "a52", "ac3", NULL };
1530
4.93k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
1531
1532
4.93k
    return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe,
1533
4.93k
                         VLC_A52_MIN_HEADER_SIZE,
1534
4.93k
                         1920 + VLC_A52_MIN_HEADER_SIZE + 1,
1535
4.93k
                         WAV_EXTRA_PROBE_SIZE,
1536
4.93k
                         true, rgi_twocc, GenericFormatCheck );
1537
4.93k
}
1538
1539
static int A52Init( demux_t *p_demux )
1540
1.07k
{
1541
1.07k
    demux_sys_t *p_sys = p_demux->p_sys;
1542
1543
1.07k
    p_sys->b_big_endian = false;
1544
1.07k
    p_sys->i_packet_size = 1024;
1545
1546
1.07k
    const uint8_t *p_peek;
1547
1548
    /* peek the beginning */
1549
1.07k
    if( vlc_stream_Peek( p_demux->s, &p_peek, VLC_A52_MIN_HEADER_SIZE ) >= VLC_A52_MIN_HEADER_SIZE )
1550
1.07k
    {
1551
1.07k
        A52CheckSync( p_peek, &p_sys->b_big_endian, NULL, true );
1552
1.07k
    }
1553
1.07k
    return VLC_SUCCESS;
1554
1.07k
}
1555
1556
/*****************************************************************************
1557
 * DTS
1558
 *****************************************************************************/
1559
static int DtsCheckSync( const uint8_t *p_peek, unsigned *pi_samples )
1560
10.3M
{
1561
10.3M
    VLC_UNUSED(pi_samples);
1562
1563
10.3M
    vlc_dts_header_t dts;
1564
10.3M
    if( vlc_dts_header_Parse( &dts, p_peek, VLC_DTS_HEADER_SIZE ) == VLC_SUCCESS
1565
20.6k
     && dts.i_frame_size > 0 && dts.i_frame_size <= 8192 )
1566
10.3k
    {
1567
10.3k
        if( pi_samples )
1568
9.79k
            *pi_samples = dts.i_frame_length;
1569
10.3k
        return dts.i_frame_size;
1570
10.3k
    }
1571
10.3M
    else
1572
10.3M
        return VLC_EGENERIC;
1573
10.3M
}
1574
1575
static int DtsProbe( demux_t *p_demux, uint64_t *pi_offset )
1576
3.86k
{
1577
3.86k
    const char *ppsz_name[] = { "dts", NULL };
1578
3.86k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_DTSINC_DTS, WAVE_FORMAT_UNKNOWN };
1579
1580
3.86k
    return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync,
1581
3.86k
                         VLC_DTS_HEADER_SIZE,
1582
3.86k
                         16384 + VLC_DTS_HEADER_SIZE + 1,
1583
3.86k
                         WAV_EXTRA_PROBE_SIZE,
1584
3.86k
                         false, rgi_twocc, NULL );
1585
3.86k
}
1586
static int DtsInit( demux_t *p_demux )
1587
1.25k
{
1588
1.25k
    demux_sys_t *p_sys = p_demux->p_sys;
1589
1590
1.25k
    p_sys->i_packet_size = 16384;
1591
1592
1.25k
    return VLC_SUCCESS;
1593
1.25k
}
1594
1595
/*****************************************************************************
1596
 * MLP
1597
 *****************************************************************************/
1598
static int MlpCheckSync( const uint8_t *p_peek, unsigned *pi_samples )
1599
6.83M
{
1600
6.83M
    if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
1601
6.67M
        return -1;
1602
1603
154k
    if( p_peek[4+3] != 0xbb )
1604
154k
        return -1;
1605
1606
    /* TODO checksum and real size for robustness */
1607
40
    VLC_UNUSED(pi_samples);
1608
40
    return 0;
1609
154k
}
1610
static int ThdCheckSync( const uint8_t *p_peek, unsigned *pi_samples )
1611
424k
{
1612
424k
    if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
1613
423k
        return -1;
1614
1615
671
    if( p_peek[4+3] != 0xba )
1616
378
        return -1;
1617
1618
    /* TODO checksum and real size for robustness */
1619
293
    VLC_UNUSED(pi_samples);
1620
293
    return 0;
1621
671
}
1622
static int MlpProbe( demux_t *p_demux, uint64_t *pi_offset )
1623
2.61k
{
1624
2.61k
    const char *ppsz_name[] = { "mlp", NULL };
1625
2.61k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
1626
1627
2.61k
    return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync,
1628
2.61k
                         4+28+16*4, BASE_PROBE_SIZE, WAV_EXTRA_PROBE_SIZE,
1629
2.61k
                         false, rgi_twocc, GenericFormatCheck );
1630
2.61k
}
1631
static int ThdProbe( demux_t *p_demux, uint64_t *pi_offset )
1632
2.57k
{
1633
2.57k
    const char *ppsz_name[] = { "thd", NULL };
1634
2.57k
    const uint16_t rgi_twocc[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
1635
1636
2.57k
    return GenericProbe( p_demux, pi_offset, ppsz_name, ThdCheckSync,
1637
2.57k
                         4+28+16*4, BASE_PROBE_SIZE, WAV_EXTRA_PROBE_SIZE,
1638
2.57k
                         false, rgi_twocc, GenericFormatCheck );
1639
2.57k
}
1640
static int MlpInit( demux_t *p_demux )
1641
1642
333
{
1643
333
    demux_sys_t *p_sys = p_demux->p_sys;
1644
1645
333
    p_sys->i_packet_size = 4096;
1646
1647
333
    return VLC_SUCCESS;
1648
333
}
1649
1650
/*****************************************************************************
1651
 * Video
1652
 *****************************************************************************/
1653
static int VideoInit( demux_t *p_demux )
1654
0
{
1655
0
    demux_sys_t *p_sys = p_demux->p_sys;
1656
1657
0
    p_sys->i_packet_size = 4096;
1658
1659
0
    return VLC_SUCCESS;
1660
0
}