Coverage Report

Created: 2026-07-12 08:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/wav.c
Line
Count
Source
1
/*****************************************************************************
2
 * wav.c : wav file input module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2001-2008 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
/*****************************************************************************
24
 * Preamble
25
 *****************************************************************************/
26
27
#ifdef HAVE_CONFIG_H
28
# include "config.h"
29
#endif
30
31
#include <assert.h>
32
#include <limits.h>
33
34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
36
#include <vlc_demux.h>
37
#include <vlc_aout.h>
38
#include <vlc_codecs.h>
39
40
#include "windows_audio_commons.h"
41
42
670
#define WAV_CHAN_MAX 32
43
static_assert( INPUT_CHAN_MAX >= WAV_CHAN_MAX, "channel count mismatch" );
44
45
typedef struct
46
{
47
    es_format_t     fmt;
48
    es_out_id_t     *p_es;
49
50
    uint64_t        i_data_pos;
51
    uint64_t        i_data_size;
52
53
    unsigned int    i_frame_size;
54
    unsigned int    i_frame_samples;
55
56
    date_t          pts;
57
58
    uint32_t i_channel_mask;
59
    uint8_t i_chans_to_reorder;            /* do we need channel reordering */
60
    uint8_t pi_chan_table[AOUT_CHAN_MAX];
61
} demux_sys_t;
62
63
enum wav_chunk_id {
64
    wav_chunk_id_data,
65
    wav_chunk_id_ds64,
66
    wav_chunk_id_fmt,
67
};
68
69
static const struct wav_chunk_id_key
70
{
71
    enum wav_chunk_id id;
72
    char key[5];
73
} wav_chunk_id_key_list[] =  {
74
    /* Alphabetical order */
75
    { wav_chunk_id_data, "data" },
76
    { wav_chunk_id_ds64, "ds64" },
77
    { wav_chunk_id_fmt,  "fmt " },
78
};
79
static const size_t wav_chunk_id_key_count = ARRAY_SIZE(wav_chunk_id_key_list);
80
81
static int
82
wav_chunk_CompareCb(const void *a, const void *b)
83
16.4k
{
84
16.4k
    const struct wav_chunk_id_key *id = b;
85
16.4k
    return memcmp(a, id->key, 4);
86
16.4k
}
87
88
static int Demux( demux_t *p_demux )
89
7.52M
{
90
7.52M
    demux_sys_t *p_sys = p_demux->p_sys;
91
7.52M
    block_t     *p_block;
92
7.52M
    const int64_t i_pos = vlc_stream_Tell( p_demux->s );
93
7.52M
    unsigned int i_read_size = p_sys->i_frame_size;
94
7.52M
    uint32_t i_read_samples = p_sys->i_frame_samples;
95
96
97
7.52M
    if( p_sys->i_data_size > 0 )
98
7.52M
    {
99
7.52M
        int64_t i_end = p_sys->i_data_pos + p_sys->i_data_size;
100
7.52M
        if ( i_pos >= i_end )
101
723
            return VLC_DEMUXER_EOF;  /* EOF */
102
103
        /* Don't read past data chunk boundary */
104
7.52M
        if ( i_end < i_pos + i_read_size )
105
491
        {
106
491
            i_read_size = i_end - i_pos;
107
491
            i_read_samples = ( p_sys->i_frame_size - i_read_size )
108
491
                           * p_sys->i_frame_samples / p_sys->i_frame_size;
109
491
        }
110
7.52M
    }
111
112
7.52M
    if( ( p_block = vlc_stream_Block( p_demux->s, i_read_size ) ) == NULL )
113
564
    {
114
564
        msg_Warn( p_demux, "cannot read data" );
115
564
        return VLC_DEMUXER_EOF;
116
564
    }
117
118
7.52M
    p_block->i_dts =
119
7.52M
    p_block->i_pts = date_Get( &p_sys->pts );
120
121
    /* set PCR */
122
7.52M
    es_out_SetPCR( p_demux->out, p_block->i_pts );
123
124
    /* Do the channel reordering */
125
7.52M
    if( p_sys->i_chans_to_reorder )
126
2.10k
        aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
127
2.10k
                             p_sys->i_chans_to_reorder,
128
2.10k
                             p_sys->pi_chan_table, p_sys->fmt.i_codec );
129
130
7.52M
    es_out_Send( p_demux->out, p_sys->p_es, p_block );
131
132
7.52M
    date_Increment( &p_sys->pts, i_read_samples );
133
134
7.52M
    return VLC_DEMUXER_SUCCESS;
135
7.52M
}
136
137
static int Control( demux_t *p_demux, int i_query, va_list args )
138
0
{
139
0
    demux_sys_t *p_sys  = p_demux->p_sys;
140
0
    int64_t i_end = -1;
141
142
0
    if( p_sys->i_data_size > 0 )
143
0
        i_end = p_sys->i_data_pos + p_sys->i_data_size;
144
145
0
    int ret = demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, i_end,
146
0
                                     p_sys->fmt.i_bitrate,
147
0
                                     p_sys->fmt.audio.i_blockalign,
148
0
                                     i_query, args );
149
0
    if( ret != VLC_SUCCESS )
150
0
        return ret;
151
152
    /* Update the date to the new seek point */
153
0
    switch( i_query )
154
0
    {
155
0
        case DEMUX_SET_POSITION:
156
0
        case DEMUX_SET_TIME:
157
0
        {
158
0
            uint64_t ofs = vlc_stream_Tell( p_demux->s );
159
0
            if( unlikely( ofs < p_sys->i_data_pos ) )
160
0
                return VLC_SUCCESS;
161
162
0
            ofs -= p_sys->i_data_pos;
163
0
            vlc_tick_t pts = VLC_TICK_0 +
164
0
                vlc_tick_from_samples( ofs * 8, p_sys->fmt.i_bitrate );
165
0
            date_Set( &p_sys->pts, pts );
166
0
            break;
167
0
        }
168
0
        default:
169
0
            break;
170
0
    }
171
0
    return VLC_SUCCESS;
172
0
}
173
174
static int ChunkSkip( demux_t *p_demux, uint32_t i_size )
175
5.92k
{
176
5.92k
    i_size += i_size & 1;
177
178
5.92k
    if( unlikely( i_size >= 65536 ) )
179
268
    {
180
        /* Arbitrary size where a seek should be performed instead of skipping
181
         * by reading NULL. Non data chunks are generally smaller than this.
182
         * This seek may be used to skip the data chunk if there is an other
183
         * chunk after it (unlikely). */
184
268
        return vlc_stream_Seek( p_demux->s,
185
268
                                vlc_stream_Tell( p_demux->s ) + i_size );
186
268
    }
187
188
5.65k
    return vlc_stream_Read( p_demux->s, NULL, i_size ) != i_size ? VLC_EGENERIC : VLC_SUCCESS;
189
5.92k
}
190
191
static int ChunkGetNext( demux_t *p_demux, enum wav_chunk_id *p_id,
192
                         uint32_t *pi_size )
193
6.74k
{
194
6.74k
#ifndef NDEBUG
195
    /* assert that keys are in alphabetical order */
196
20.2k
    for( size_t i = 0; i < wav_chunk_id_key_count - 1; ++i )
197
13.4k
        assert( strcmp( wav_chunk_id_key_list[i].key,
198
6.74k
                        wav_chunk_id_key_list[i + 1].key ) < 0 );
199
6.74k
#endif
200
201
6.74k
    for( ;; )
202
9.59k
    {
203
9.59k
        const uint8_t *p_peek;
204
9.59k
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
205
436
            return VLC_EGENERIC;
206
207
9.15k
        const struct wav_chunk_id_key *id =
208
9.15k
            bsearch( p_peek, wav_chunk_id_key_list, wav_chunk_id_key_count,
209
9.15k
                     sizeof(*wav_chunk_id_key_list), wav_chunk_CompareCb );
210
9.15k
        uint32_t i_size = GetDWLE( p_peek + 4 );
211
212
9.15k
        if( id == NULL )
213
2.92k
        {
214
2.92k
            msg_Warn( p_demux, "unknown chunk '%4.4s' of size: %u",
215
2.92k
                      p_peek, i_size );
216
217
2.92k
            if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 )
218
0
                return VLC_EGENERIC;
219
220
2.92k
            if( ChunkSkip( p_demux, i_size ) != VLC_SUCCESS )
221
77
                return VLC_EGENERIC;
222
2.85k
            continue;
223
2.92k
        }
224
225
6.22k
        if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 )
226
0
            return VLC_EGENERIC;
227
228
6.22k
        *p_id = id->id;
229
6.22k
        *pi_size = i_size;
230
231
6.22k
        return VLC_SUCCESS;
232
6.22k
    }
233
6.74k
}
234
235
static int FrameInfo_PCM( unsigned int *pi_size, unsigned *pi_samples,
236
                          const es_format_t *p_fmt )
237
709
{
238
709
    int i_bytes;
239
240
709
    if( p_fmt->audio.i_rate > 352800
241
686
     || p_fmt->audio.i_bitspersample > 64
242
670
     || p_fmt->audio.i_channels > WAV_CHAN_MAX )
243
42
        return VLC_EGENERIC;
244
245
    /* read samples for 50ms of */
246
667
    *pi_samples = __MAX( p_fmt->audio.i_rate / 20, 1 );
247
248
667
    i_bytes = *pi_samples * p_fmt->audio.i_channels *
249
667
        ( (p_fmt->audio.i_bitspersample + 7) / 8 );
250
251
667
    if( p_fmt->audio.i_blockalign > 0 )
252
446
    {
253
446
        const int i_modulo = i_bytes % p_fmt->audio.i_blockalign;
254
446
        if( i_modulo > 0 )
255
331
            i_bytes += p_fmt->audio.i_blockalign - i_modulo;
256
446
    }
257
258
667
    *pi_size = i_bytes;
259
667
    return VLC_SUCCESS;
260
709
}
261
262
static int FrameInfo_MS_ADPCM( unsigned int *pi_size, unsigned *pi_samples,
263
                               const es_format_t *p_fmt )
264
202
{
265
202
    if( p_fmt->audio.i_channels == 0 )
266
1
        return VLC_EGENERIC;
267
268
201
    if( p_fmt->audio.i_blockalign < 7 * p_fmt->audio.i_channels )
269
2
        return VLC_EGENERIC;
270
271
199
    *pi_samples = 2 + 2 * ( p_fmt->audio.i_blockalign -
272
199
        7 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
273
199
    *pi_size = p_fmt->audio.i_blockalign;
274
275
199
    return VLC_SUCCESS;
276
201
}
277
278
static int FrameInfo_IMA_ADPCM( unsigned int *pi_size, unsigned *pi_samples,
279
                                const es_format_t *p_fmt )
280
230
{
281
230
    if( p_fmt->audio.i_channels == 0 )
282
3
        return VLC_EGENERIC;
283
284
227
    if( p_fmt->audio.i_blockalign < 4 * p_fmt->audio.i_channels )
285
4
        return VLC_EGENERIC;
286
287
223
    *pi_samples = 2 * ( p_fmt->audio.i_blockalign -
288
223
        4 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
289
223
    *pi_size = p_fmt->audio.i_blockalign;
290
291
223
    return VLC_SUCCESS;
292
227
}
293
294
static int FrameInfo_Creative_ADPCM( unsigned int *pi_size, unsigned *pi_samples,
295
                                     const es_format_t *p_fmt )
296
104
{
297
104
    if( p_fmt->audio.i_channels == 0 )
298
2
        return VLC_EGENERIC;
299
300
    /* 4 bits / sample */
301
102
    *pi_samples = p_fmt->audio.i_blockalign * 2 / p_fmt->audio.i_channels;
302
102
    *pi_size = p_fmt->audio.i_blockalign;
303
304
102
    return VLC_SUCCESS;
305
104
}
306
307
static int FrameInfo_MSGSM( unsigned int *pi_size, unsigned *pi_samples,
308
                            const es_format_t *p_fmt )
309
343
{
310
343
    if( p_fmt->i_bitrate <= 0 )
311
3
        return VLC_EGENERIC;
312
313
340
    *pi_samples = ( p_fmt->audio.i_blockalign * p_fmt->audio.i_rate * 8)
314
340
                    / p_fmt->i_bitrate;
315
340
    *pi_size = p_fmt->audio.i_blockalign;
316
317
340
    return VLC_SUCCESS;
318
343
}
319
static void Close ( vlc_object_t * p_this )
320
1.28k
{
321
1.28k
    demux_t     *p_demux = (demux_t*)p_this;
322
1.28k
    demux_sys_t *p_sys   = p_demux->p_sys;
323
324
1.28k
    es_format_Clean( &p_sys->fmt );
325
1.28k
    free( p_sys );
326
1.28k
}
327
328
static int ChunkParseDS64( demux_t *p_demux, uint32_t i_size )
329
1.82k
{
330
1.82k
    demux_sys_t *p_sys = p_demux->p_sys;
331
1.82k
    const uint8_t *p_peek;
332
333
1.82k
    if( i_size < 24 )
334
5
    {
335
5
        msg_Err( p_demux, "invalid 'ds64' chunk" );
336
5
        return VLC_EGENERIC;
337
5
    }
338
339
1.81k
    if( vlc_stream_Peek( p_demux->s, &p_peek, 24 ) < 24 )
340
62
        return VLC_EGENERIC;
341
342
1.75k
    p_sys->i_data_size = GetQWLE( &p_peek[8] );
343
344
1.75k
    return ChunkSkip( p_demux, i_size );
345
1.81k
}
346
347
static void InitFmt( demux_t *p_demux )
348
2.77k
{
349
2.77k
    demux_sys_t *p_sys = p_demux->p_sys;
350
351
2.77k
    es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
352
2.77k
    p_sys->i_frame_size = 0;
353
2.77k
    p_sys->i_frame_samples = 0;
354
2.77k
    p_sys->i_chans_to_reorder = 0;
355
2.77k
    p_sys->i_channel_mask = 0;
356
2.77k
}
357
358
static int ChunkParseFmt( demux_t *p_demux, uint32_t i_size )
359
1.89k
{
360
1.89k
    demux_sys_t *p_sys = p_demux->p_sys;
361
1.89k
    WAVEFORMATEXTENSIBLE *p_wf_ext = NULL;
362
1.89k
    WAVEFORMATEX         *p_wf = NULL;
363
1.89k
    const char *psz_name;
364
1.89k
    unsigned int i_extended;
365
366
1.89k
    i_size += 2;
367
1.89k
    if( i_size < sizeof( WAVEFORMATEX ) || i_size > (sizeof( WAVEFORMATEX ) + UINT16_MAX ) )
368
36
    {
369
36
        msg_Err( p_demux, "invalid 'fmt ' chunk of size %" PRIu32, i_size );
370
36
        goto error;
371
36
    }
372
373
    /* load waveformatex */
374
1.85k
    p_wf_ext = malloc( i_size );
375
1.85k
    if( unlikely( !p_wf_ext ) )
376
0
         goto error;
377
378
1.85k
    p_wf         = &p_wf_ext->Format;
379
1.85k
    p_wf->cbSize = 0;
380
1.85k
    i_size      -= 2;
381
1.85k
    if( vlc_stream_Read( p_demux->s, p_wf, i_size ) != (int)i_size ||
382
1.81k
        ( ( i_size & 1 ) && vlc_stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
383
43
    {
384
43
        msg_Err( p_demux, "cannot load 'fmt ' chunk" );
385
43
        goto error;
386
43
    }
387
388
1.81k
    wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
389
1.81k
                      &psz_name );
390
1.81k
    p_sys->fmt.audio.i_channels      = GetWLE ( &p_wf->nChannels );
391
1.81k
    p_sys->fmt.audio.i_rate          = GetDWLE( &p_wf->nSamplesPerSec );
392
1.81k
    p_sys->fmt.audio.i_blockalign    = GetWLE( &p_wf->nBlockAlign );
393
1.81k
    p_sys->fmt.i_bitrate             = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
394
1.81k
    p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
395
1.81k
    if( i_size >= sizeof(WAVEFORMATEX) )
396
446
        p_sys->fmt.i_extra = __MIN( GetWLE( &p_wf->cbSize ), i_size - sizeof(WAVEFORMATEX) );
397
1.81k
    i_extended = 0;
398
399
    /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
400
    /* see the following link for more information:
401
     * http://www.microsoft.com/whdc/device/audio/multichaud.mspx#EFAA */
402
1.81k
    if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
403
346
        i_size >= sizeof( WAVEFORMATEXTENSIBLE ) &&
404
339
        ( p_sys->fmt.i_extra + sizeof( WAVEFORMATEX )
405
339
            >= sizeof( WAVEFORMATEXTENSIBLE ) ) )
406
331
    {
407
331
        unsigned i_channel_mask;
408
331
        GUID guid_subformat;
409
410
331
        guid_subformat = p_wf_ext->SubFormat;
411
331
        guid_subformat.Data1 = GetDWLE( &p_wf_ext->SubFormat.Data1 );
412
331
        guid_subformat.Data2 = GetWLE( &p_wf_ext->SubFormat.Data2 );
413
331
        guid_subformat.Data3 = GetWLE( &p_wf_ext->SubFormat.Data3 );
414
415
331
        sf_tag_to_fourcc( &guid_subformat, &p_sys->fmt.i_codec, &psz_name );
416
417
331
        msg_Dbg( p_demux, "extensible format guid " GUID_FMT, GUID_PRINT(guid_subformat) );
418
419
331
        i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
420
331
        p_sys->fmt.i_extra -= i_extended;
421
422
331
        i_channel_mask = GetDWLE( &p_wf_ext->dwChannelMask );
423
331
        if( i_channel_mask )
424
329
        {
425
329
            int i_match = 0;
426
329
            p_sys->i_channel_mask = getChannelMask( &i_channel_mask, p_sys->fmt.audio.i_channels, &i_match );
427
329
            if( i_channel_mask )
428
329
                msg_Warn( p_demux, "Some channels are unrecognized or uselessly specified (0x%x)", i_channel_mask );
429
329
            if( i_match < p_sys->fmt.audio.i_channels )
430
119
            {
431
119
                int i_missing = p_sys->fmt.audio.i_channels - i_match;
432
119
                msg_Warn( p_demux, "Trying to fill up unspecified position for %d channels", p_sys->fmt.audio.i_channels - i_match );
433
434
119
                static const uint32_t pi_pair[] = { AOUT_CHAN_REARLEFT|AOUT_CHAN_REARRIGHT,
435
119
                                                    AOUT_CHAN_MIDDLELEFT|AOUT_CHAN_MIDDLERIGHT,
436
119
                                                    AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT };
437
                /* FIXME: Unused yet
438
                static const uint32_t pi_center[] = { AOUT_CHAN_REARCENTER,
439
                                                      0,
440
                                                      AOUT_CHAN_CENTER }; */
441
442
                /* Try to complete with pair */
443
476
                for( unsigned i = 0; i < ARRAY_SIZE(pi_pair); i++ )
444
357
                {
445
357
                    if( i_missing >= 2 && !(p_sys->i_channel_mask & pi_pair[i] ) )
446
112
                    {
447
112
                        i_missing -= 2;
448
112
                        p_sys->i_channel_mask |= pi_pair[i];
449
112
                    }
450
357
                }
451
                /* Well fill up with what we can */
452
610
                for( unsigned i = 0; i < ARRAY_SIZE(pi_channels_aout) && i_missing > 0; i++ )
453
533
                {
454
533
                    if( !( p_sys->i_channel_mask & pi_channels_aout[i] ) )
455
229
                    {
456
229
                        p_sys->i_channel_mask |= pi_channels_aout[i];
457
229
                        i_missing--;
458
459
229
                        if( i_missing <= 0 )
460
42
                            break;
461
229
                    }
462
533
                }
463
464
119
                i_match = p_sys->fmt.audio.i_channels - i_missing;
465
119
            }
466
329
            if( i_match < p_sys->fmt.audio.i_channels )
467
44
            {
468
44
                msg_Err( p_demux, "Invalid/unsupported channel mask" );
469
44
                p_sys->i_channel_mask = 0;
470
44
            }
471
329
        }
472
331
    }
473
1.81k
    if( p_sys->i_channel_mask == 0 && p_sys->fmt.audio.i_channels > 2
474
797
     && p_sys->fmt.audio.i_channels <= AOUT_CHAN_MAX )
475
364
    {
476
        /* A dwChannelMask of 0 tells the audio device to render the first
477
         * channel to the first port on the device, the second channel to the
478
         * second port on the device, and so on. pi_default_channels is
479
         * different than pi_channels_aout. Indeed FLC/FRC must be treated a
480
         * SL/SR in that case. See "Default Channel Ordering" and "Details
481
         * about dwChannelMask" from msdn */
482
483
364
        static const uint32_t pi_default_channels[] = {
484
364
            AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
485
364
            AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
486
364
            AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_REARCENTER };
487
488
2.73k
        for( unsigned i = 0; i < p_sys->fmt.audio.i_channels &&
489
2.37k
             i < ARRAY_SIZE(pi_default_channels);
490
2.37k
             i++ )
491
2.37k
            p_sys->i_channel_mask |= pi_default_channels[i];
492
364
    }
493
494
1.81k
    if( p_sys->i_channel_mask )
495
637
    {
496
637
        if( p_sys->fmt.i_codec == VLC_FOURCC('a','r','a','w') ||
497
327
            p_sys->fmt.i_codec == VLC_FOURCC('a','f','l','t') )
498
450
            p_sys->i_chans_to_reorder =
499
450
                aout_CheckChannelReorder( pi_channels_aout, NULL,
500
450
                                          p_sys->i_channel_mask,
501
450
                                          p_sys->pi_chan_table );
502
503
637
        msg_Dbg( p_demux, "channel mask: %x, reordering: %u",
504
637
                 p_sys->i_channel_mask, p_sys->i_chans_to_reorder );
505
637
    }
506
507
1.81k
    p_sys->fmt.audio.i_physical_channels = p_sys->i_channel_mask;
508
509
1.81k
    if( p_sys->fmt.i_extra > 0 )
510
107
    {
511
107
        p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
512
107
        if( unlikely(!p_sys->fmt.p_extra) )
513
0
        {
514
0
            p_sys->fmt.i_extra = 0;
515
0
            goto error;
516
0
        }
517
107
        memcpy( p_sys->fmt.p_extra, (uint8_t *)p_wf + sizeof( WAVEFORMATEX ) + i_extended,
518
107
                p_sys->fmt.i_extra );
519
107
    }
520
521
1.81k
    msg_Dbg( p_demux, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
522
1.81k
             "freq: %u Hz, bitrate: %uKo/s, blockalign: %d, bits/samples: %d, "
523
1.81k
             "extra size: %zu",
524
1.81k
             GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
525
1.81k
             p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
526
1.81k
             p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
527
1.81k
             p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
528
529
1.81k
    free( p_wf );
530
1.81k
    p_wf = NULL;
531
532
1.81k
    switch( p_sys->fmt.i_codec )
533
1.81k
    {
534
407
    case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
535
563
    case VLC_FOURCC( 'a', 'f', 'l', 't' ):
536
563
    case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
537
643
    case VLC_CODEC_ALAW:
538
709
    case VLC_CODEC_MULAW:
539
709
        if( FrameInfo_PCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
540
709
                           &p_sys->fmt ) )
541
42
            goto error;
542
667
        p_sys->fmt.i_codec =
543
667
            vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec,
544
667
                                      p_sys->fmt.audio.i_bitspersample );
545
667
        if( p_sys->fmt.i_codec == 0 ) {
546
26
            msg_Err( p_demux, "Unrecognized codec" );
547
26
            goto error;
548
26
        }
549
641
        break;
550
641
    case VLC_CODEC_ADPCM_MS:
551
    /* FIXME not sure at all FIXME */
552
166
    case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
553
202
    case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
554
202
        if( FrameInfo_MS_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
555
202
                                &p_sys->fmt ) )
556
3
            goto error;
557
199
        break;
558
230
    case VLC_CODEC_ADPCM_IMA_WAV:
559
230
        if( FrameInfo_IMA_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
560
230
                                 &p_sys->fmt ) )
561
7
            goto error;
562
223
        break;
563
223
    case VLC_CODEC_ADPCM_CREATIVE:
564
104
        if( FrameInfo_Creative_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
565
104
                                      &p_sys->fmt ) )
566
2
            goto error;
567
102
        break;
568
102
    case VLC_CODEC_MPGA:
569
3
    case VLC_CODEC_A52:
570
        /* FIXME set end of area FIXME */
571
3
        goto error;
572
33
    case VLC_CODEC_GSM_MS:
573
131
    case VLC_CODEC_ADPCM_G726:
574
211
    case VLC_CODEC_TRUESPEECH:
575
216
    case VLC_CODEC_ATRAC3P:
576
222
    case VLC_CODEC_ATRAC3:
577
222
    case VLC_CODEC_DFPWM:
578
275
    case VLC_CODEC_G723_1:
579
343
    case VLC_CODEC_WMA2:
580
343
        if( FrameInfo_MSGSM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
581
343
                             &p_sys->fmt ) )
582
3
            goto error;
583
340
        break;
584
340
    default:
585
221
        msg_Err( p_demux, "unsupported codec (%4.4s)",
586
221
                 (char*)&p_sys->fmt.i_codec );
587
221
        goto error;
588
1.81k
    }
589
590
1.50k
    if( p_sys->i_frame_size == 0 || p_sys->i_frame_samples == 0 )
591
42
    {
592
42
        msg_Dbg( p_demux, "invalid frame size: 0 0" );
593
42
        goto error;
594
42
    }
595
1.46k
    if( p_sys->fmt.audio.i_rate == 0 )
596
5
    {
597
5
        msg_Dbg( p_demux, "invalid sample rate: 0" );
598
5
        goto error;
599
5
    }
600
601
1.45k
    msg_Dbg( p_demux, "found %s audio format", psz_name );
602
603
1.45k
    return VLC_SUCCESS;
604
605
433
error:
606
433
    es_format_Clean( &p_sys->fmt );
607
433
    InitFmt( p_demux );
608
433
    free( p_wf );
609
433
    return VLC_EGENERIC;
610
1.46k
}
611
612
static int Open( vlc_object_t * p_this )
613
2.40k
{
614
2.40k
    demux_t     *p_demux = (demux_t*)p_this;
615
2.40k
    demux_sys_t *p_sys;
616
617
2.40k
    const uint8_t *p_peek;
618
2.40k
    bool           b_is_rf64;
619
2.40k
    uint32_t       i_size;
620
621
    /* Is it a wav file ? */
622
2.40k
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
623
9
        return VLC_EGENERIC;
624
625
2.39k
    b_is_rf64 = ( memcmp( p_peek, "RF64", 4 ) == 0 );
626
2.39k
    if( ( !b_is_rf64 && memcmp( p_peek, "RIFF", 4 ) ) ||
627
2.35k
          memcmp( &p_peek[8], "WAVE", 4 ) )
628
48
    {
629
48
        return VLC_EGENERIC;
630
48
    }
631
632
2.34k
    p_demux->pf_demux     = Demux;
633
2.34k
    p_demux->pf_control   = Control;
634
2.34k
    p_demux->p_sys        = p_sys = malloc( sizeof( *p_sys ) );
635
2.34k
    if( unlikely(!p_sys) )
636
0
        return VLC_ENOMEM;
637
638
2.34k
    p_sys->p_es           = NULL;
639
2.34k
    p_sys->i_data_pos = p_sys->i_data_size = 0;
640
2.34k
    InitFmt( p_demux );
641
642
    /* skip riff header */
643
2.34k
    if( vlc_stream_Read( p_demux->s, NULL, 12 ) != 12 )
644
0
        goto error;
645
646
2.34k
    bool eof = false;
647
2.34k
    enum wav_chunk_id id;
648
8.01k
    while( !eof && ( ChunkGetNext( p_demux, &id, &i_size ) ) == VLC_SUCCESS )
649
6.22k
    {
650
6.22k
        if( i_size == 0 )
651
1
        {
652
1
            msg_Err( p_demux, "invalid chunk with a size 0");
653
1
            goto error;
654
1
        }
655
656
6.22k
        switch( id )
657
6.22k
        {
658
2.26k
            case wav_chunk_id_data:
659
2.26k
            {
660
2.26k
                uint64_t i_stream_size;
661
2.26k
                if( vlc_stream_GetSize( p_demux->s, &i_stream_size ) != VLC_SUCCESS )
662
0
                    goto error;
663
2.26k
                p_sys->i_data_pos = vlc_stream_Tell( p_demux->s );
664
665
2.26k
                if( !b_is_rf64 && i_stream_size >= i_size + p_sys->i_data_pos )
666
1.06k
                    p_sys->i_data_size = i_size;
667
668
2.26k
                if( likely( b_is_rf64
669
2.26k
                 || p_sys->i_data_pos + i_size == i_stream_size ) )
670
1.27k
                {
671
                    /* Bypass the final ChunkGetNext() to avoid a read+seek
672
                     * since this chunk is the last one */
673
1.27k
                    eof = true;
674
1.27k
                }   /* Unlikely case where there is a chunk after 'data' */
675
995
                else if( ChunkSkip( p_demux, i_size ) != VLC_SUCCESS )
676
35
                    goto error;
677
2.23k
                break;
678
2.26k
            }
679
2.23k
            case wav_chunk_id_ds64:
680
1.82k
                if( b_is_rf64 )
681
1.82k
                {
682
1.82k
                    if( ChunkParseDS64( p_demux, i_size ) != VLC_SUCCESS )
683
89
                        goto error;
684
1.82k
                }
685
1
                else
686
1
                {
687
1
                    msg_Err( p_demux, "'ds64' chunk found but format not RF64" );
688
1
                    goto error;
689
1
                }
690
1.73k
                break;
691
2.13k
            case wav_chunk_id_fmt:
692
2.13k
                if( p_sys->i_frame_samples != 0 )
693
248
                {
694
248
                    ChunkSkip( p_demux, i_size );
695
248
                    msg_Warn( p_demux, "fmt chunk already parsed" );
696
248
                    break;
697
248
                }
698
1.89k
                if( ChunkParseFmt( p_demux, i_size ) != VLC_SUCCESS )
699
433
                    goto error;
700
1.45k
                break;
701
6.22k
        }
702
6.22k
    }
703
704
1.78k
    if( p_sys->i_data_pos == 0 || p_sys->i_data_size == 0
705
1.38k
     || p_sys->i_frame_samples == 0 )
706
497
    {
707
497
        msg_Err( p_demux, "'%s' chunk not found",
708
497
                 p_sys->i_data_pos == 0 ? "data" :
709
497
                 p_sys->i_frame_samples == 0 ? "fmt " :
710
497
                 b_is_rf64 ? "ds64" : "data" );
711
497
        goto error;
712
497
    }
713
714
    /* Seek back to data position if needed */
715
1.28k
    if( unlikely( vlc_stream_Tell( p_demux->s ) != p_sys->i_data_pos )
716
101
     && vlc_stream_Seek( p_demux->s, p_sys->i_data_pos ) != VLC_SUCCESS )
717
0
        goto error;
718
719
1.28k
    if( p_sys->fmt.i_bitrate <= 0 )
720
101
    {
721
101
        p_sys->fmt.i_bitrate = (int64_t)p_sys->i_frame_size *
722
101
            p_sys->fmt.audio.i_rate * 8 / p_sys->i_frame_samples;
723
101
    }
724
725
1.28k
    p_sys->fmt.i_id = 0;
726
1.28k
    p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
727
1.28k
    if( unlikely(p_sys->p_es == NULL) )
728
0
        goto error;
729
730
1.28k
    date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
731
1.28k
    date_Set( &p_sys->pts, VLC_TICK_0 );
732
733
1.28k
    return VLC_SUCCESS;
734
735
1.05k
error:
736
1.05k
    es_format_Clean( &p_sys->fmt );
737
1.05k
    free( p_sys );
738
1.05k
    return VLC_EGENERIC;
739
1.28k
}
740
741
168
vlc_module_begin ()
742
84
    set_description( N_("WAV demuxer") )
743
84
    set_subcategory( SUBCAT_INPUT_DEMUX )
744
84
    set_capability( "demux", 142 )
745
168
    set_callbacks( Open, Close )
746
84
vlc_module_end ()