Coverage Report

Created: 2025-11-09 06:39

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
735
#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
42.8k
{
84
42.8k
    const struct wav_chunk_id_key *id = b;
85
42.8k
    return memcmp(a, id->key, 4);
86
42.8k
}
87
88
static int Demux( demux_t *p_demux )
89
6.39M
{
90
6.39M
    demux_sys_t *p_sys = p_demux->p_sys;
91
6.39M
    block_t     *p_block;
92
6.39M
    const int64_t i_pos = vlc_stream_Tell( p_demux->s );
93
6.39M
    unsigned int i_read_size = p_sys->i_frame_size;
94
6.39M
    uint32_t i_read_samples = p_sys->i_frame_samples;
95
96
97
6.39M
    if( p_sys->i_data_size > 0 )
98
6.39M
    {
99
6.39M
        int64_t i_end = p_sys->i_data_pos + p_sys->i_data_size;
100
6.39M
        if ( i_pos >= i_end )
101
590
            return VLC_DEMUXER_EOF;  /* EOF */
102
103
        /* Don't read past data chunk boundary */
104
6.39M
        if ( i_end < i_pos + i_read_size )
105
438
        {
106
438
            i_read_size = i_end - i_pos;
107
438
            i_read_samples = ( p_sys->i_frame_size - i_read_size )
108
438
                           * p_sys->i_frame_samples / p_sys->i_frame_size;
109
438
        }
110
6.39M
    }
111
112
6.39M
    if( ( p_block = vlc_stream_Block( p_demux->s, i_read_size ) ) == NULL )
113
584
    {
114
584
        msg_Warn( p_demux, "cannot read data" );
115
584
        return VLC_DEMUXER_EOF;
116
584
    }
117
118
6.39M
    p_block->i_dts =
119
6.39M
    p_block->i_pts = date_Get( &p_sys->pts );
120
121
    /* set PCR */
122
6.39M
    es_out_SetPCR( p_demux->out, p_block->i_pts );
123
124
    /* Do the channel reordering */
125
6.39M
    if( p_sys->i_chans_to_reorder )
126
2.63k
        aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
127
2.63k
                             p_sys->i_chans_to_reorder,
128
2.63k
                             p_sys->pi_chan_table, p_sys->fmt.i_codec );
129
130
6.39M
    es_out_Send( p_demux->out, p_sys->p_es, p_block );
131
132
6.39M
    date_Increment( &p_sys->pts, i_read_samples );
133
134
6.39M
    return VLC_DEMUXER_SUCCESS;
135
6.39M
}
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 =
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
19.4k
{
176
19.4k
    i_size += i_size & 1;
177
178
19.4k
    if( unlikely( i_size >= 65536 ) )
179
343
    {
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
343
        return vlc_stream_Seek( p_demux->s,
185
343
                                vlc_stream_Tell( p_demux->s ) + i_size );
186
343
    }
187
188
19.1k
    return vlc_stream_Read( p_demux->s, NULL, i_size ) != i_size ? VLC_EGENERIC : VLC_SUCCESS;
189
19.4k
}
190
191
static int ChunkGetNext( demux_t *p_demux, enum wav_chunk_id *p_id,
192
                         uint32_t *pi_size )
193
7.02k
{
194
7.02k
#ifndef NDEBUG
195
    /* assert that keys are in alphabetical order */
196
21.0k
    for( size_t i = 0; i < wav_chunk_id_key_count - 1; ++i )
197
14.0k
        assert( strcmp( wav_chunk_id_key_list[i].key,
198
7.02k
                        wav_chunk_id_key_list[i + 1].key ) < 0 );
199
7.02k
#endif
200
201
7.02k
    for( ;; )
202
23.0k
    {
203
23.0k
        const uint8_t *p_peek;
204
23.0k
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
205
520
            return VLC_EGENERIC;
206
207
22.5k
        const struct wav_chunk_id_key *id =
208
22.5k
            bsearch( p_peek, wav_chunk_id_key_list, wav_chunk_id_key_count,
209
22.5k
                     sizeof(*wav_chunk_id_key_list), wav_chunk_CompareCb );
210
22.5k
        uint32_t i_size = GetDWLE( p_peek + 4 );
211
212
22.5k
        if( id == NULL )
213
16.1k
        {
214
16.1k
            msg_Warn( p_demux, "unknown chunk '%4.4s' of size: %u",
215
16.1k
                      p_peek, i_size );
216
217
16.1k
            if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 )
218
0
                return VLC_EGENERIC;
219
220
16.1k
            if( ChunkSkip( p_demux, i_size ) != VLC_SUCCESS )
221
87
                return VLC_EGENERIC;
222
16.0k
            continue;
223
16.1k
        }
224
225
6.41k
        if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 )
226
0
            return VLC_EGENERIC;
227
228
6.41k
        *p_id = id->id;
229
6.41k
        *pi_size = i_size;
230
231
6.41k
        return VLC_SUCCESS;
232
6.41k
    }
233
7.02k
}
234
235
static int FrameInfo_PCM( unsigned int *pi_size, unsigned *pi_samples,
236
                          const es_format_t *p_fmt )
237
784
{
238
784
    int i_bytes;
239
240
784
    if( p_fmt->audio.i_rate > 352800
241
757
     || p_fmt->audio.i_bitspersample > 64
242
735
     || p_fmt->audio.i_channels > WAV_CHAN_MAX )
243
52
        return VLC_EGENERIC;
244
245
    /* read samples for 50ms of */
246
732
    *pi_samples = __MAX( p_fmt->audio.i_rate / 20, 1 );
247
248
732
    i_bytes = *pi_samples * p_fmt->audio.i_channels *
249
732
        ( (p_fmt->audio.i_bitspersample + 7) / 8 );
250
251
732
    if( p_fmt->audio.i_blockalign > 0 )
252
501
    {
253
501
        const int i_modulo = i_bytes % p_fmt->audio.i_blockalign;
254
501
        if( i_modulo > 0 )
255
343
            i_bytes += p_fmt->audio.i_blockalign - i_modulo;
256
501
    }
257
258
732
    *pi_size = i_bytes;
259
732
    return VLC_SUCCESS;
260
784
}
261
262
static int FrameInfo_MS_ADPCM( unsigned int *pi_size, unsigned *pi_samples,
263
                               const es_format_t *p_fmt )
264
278
{
265
278
    if( p_fmt->audio.i_channels == 0 )
266
2
        return VLC_EGENERIC;
267
268
276
    if( p_fmt->audio.i_blockalign < 7 * p_fmt->audio.i_channels )
269
2
        return VLC_EGENERIC;
270
271
274
    *pi_samples = 2 + 2 * ( p_fmt->audio.i_blockalign -
272
274
        7 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
273
274
    *pi_size = p_fmt->audio.i_blockalign;
274
275
274
    return VLC_SUCCESS;
276
276
}
277
278
static int FrameInfo_IMA_ADPCM( unsigned int *pi_size, unsigned *pi_samples,
279
                                const es_format_t *p_fmt )
280
168
{
281
168
    if( p_fmt->audio.i_channels == 0 )
282
1
        return VLC_EGENERIC;
283
284
167
    if( p_fmt->audio.i_blockalign < 4 * p_fmt->audio.i_channels )
285
3
        return VLC_EGENERIC;
286
287
164
    *pi_samples = 2 * ( p_fmt->audio.i_blockalign -
288
164
        4 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
289
164
    *pi_size = p_fmt->audio.i_blockalign;
290
291
164
    return VLC_SUCCESS;
292
167
}
293
294
static int FrameInfo_Creative_ADPCM( unsigned int *pi_size, unsigned *pi_samples,
295
                                     const es_format_t *p_fmt )
296
39
{
297
39
    if( p_fmt->audio.i_channels == 0 )
298
2
        return VLC_EGENERIC;
299
300
    /* 4 bits / sample */
301
37
    *pi_samples = p_fmt->audio.i_blockalign * 2 / p_fmt->audio.i_channels;
302
37
    *pi_size = p_fmt->audio.i_blockalign;
303
304
37
    return VLC_SUCCESS;
305
39
}
306
307
static int FrameInfo_MSGSM( unsigned int *pi_size, unsigned *pi_samples,
308
                            const es_format_t *p_fmt )
309
239
{
310
239
    if( p_fmt->i_bitrate <= 0 )
311
2
        return VLC_EGENERIC;
312
313
237
    *pi_samples = ( p_fmt->audio.i_blockalign * p_fmt->audio.i_rate * 8)
314
237
                    / p_fmt->i_bitrate;
315
237
    *pi_size = p_fmt->audio.i_blockalign;
316
317
237
    return VLC_SUCCESS;
318
239
}
319
static void Close ( vlc_object_t * p_this )
320
1.17k
{
321
1.17k
    demux_t     *p_demux = (demux_t*)p_this;
322
1.17k
    demux_sys_t *p_sys   = p_demux->p_sys;
323
324
1.17k
    es_format_Clean( &p_sys->fmt );
325
1.17k
    free( p_sys );
326
1.17k
}
327
328
static int ChunkParseDS64( demux_t *p_demux, uint32_t i_size )
329
2.23k
{
330
2.23k
    demux_sys_t *p_sys = p_demux->p_sys;
331
2.23k
    const uint8_t *p_peek;
332
333
2.23k
    if( i_size < 24 )
334
6
    {
335
6
        msg_Err( p_demux, "invalid 'ds64' chunk" );
336
6
        return VLC_EGENERIC;
337
6
    }
338
339
2.22k
    if( vlc_stream_Peek( p_demux->s, &p_peek, 24 ) < 24 )
340
70
        return VLC_EGENERIC;
341
342
2.15k
    p_sys->i_data_size = GetQWLE( &p_peek[8] );
343
344
2.15k
    return ChunkSkip( p_demux, i_size );
345
2.22k
}
346
347
static void InitFmt( demux_t *p_demux )
348
2.86k
{
349
2.86k
    demux_sys_t *p_sys = p_demux->p_sys;
350
351
2.86k
    es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
352
2.86k
    p_sys->i_frame_size = 0;
353
2.86k
    p_sys->i_frame_samples = 0;
354
2.86k
    p_sys->i_chans_to_reorder = 0;
355
2.86k
    p_sys->i_channel_mask = 0;
356
2.86k
}
357
358
static int ChunkParseFmt( demux_t *p_demux, uint32_t i_size )
359
1.87k
{
360
1.87k
    demux_sys_t *p_sys = p_demux->p_sys;
361
1.87k
    WAVEFORMATEXTENSIBLE *p_wf_ext = NULL;
362
1.87k
    WAVEFORMATEX         *p_wf = NULL;
363
1.87k
    const char *psz_name;
364
1.87k
    unsigned int i_extended;
365
366
1.87k
    i_size += 2;
367
1.87k
    if( i_size < sizeof( WAVEFORMATEX ) || i_size > (sizeof( WAVEFORMATEX ) + UINT16_MAX ) )
368
32
    {
369
32
        msg_Err( p_demux, "invalid 'fmt ' chunk of size %" PRIu32, i_size );
370
32
        goto error;
371
32
    }
372
373
    /* load waveformatex */
374
1.83k
    p_wf_ext = malloc( i_size );
375
1.83k
    if( unlikely( !p_wf_ext ) )
376
0
         goto error;
377
378
1.83k
    p_wf         = &p_wf_ext->Format;
379
1.83k
    p_wf->cbSize = 0;
380
1.83k
    i_size      -= 2;
381
1.83k
    if( vlc_stream_Read( p_demux->s, p_wf, i_size ) != (int)i_size ||
382
1.79k
        ( ( i_size & 1 ) && vlc_stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
383
46
    {
384
46
        msg_Err( p_demux, "cannot load 'fmt ' chunk" );
385
46
        goto error;
386
46
    }
387
388
1.79k
    wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
389
1.79k
                      &psz_name );
390
1.79k
    p_sys->fmt.audio.i_channels      = GetWLE ( &p_wf->nChannels );
391
1.79k
    p_sys->fmt.audio.i_rate          = GetDWLE( &p_wf->nSamplesPerSec );
392
1.79k
    p_sys->fmt.audio.i_blockalign    = GetWLE( &p_wf->nBlockAlign );
393
1.79k
    p_sys->fmt.i_bitrate             = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
394
1.79k
    p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
395
1.79k
    if( i_size >= sizeof(WAVEFORMATEX) )
396
287
        p_sys->fmt.i_extra = __MIN( GetWLE( &p_wf->cbSize ), i_size - sizeof(WAVEFORMATEX) );
397
1.79k
    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.79k
    if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
403
227
        i_size >= sizeof( WAVEFORMATEXTENSIBLE ) &&
404
216
        ( p_sys->fmt.i_extra + sizeof( WAVEFORMATEX )
405
216
            >= sizeof( WAVEFORMATEXTENSIBLE ) ) )
406
207
    {
407
207
        unsigned i_channel_mask;
408
207
        GUID guid_subformat;
409
410
207
        guid_subformat = p_wf_ext->SubFormat;
411
207
        guid_subformat.Data1 = GetDWLE( &p_wf_ext->SubFormat.Data1 );
412
207
        guid_subformat.Data2 = GetWLE( &p_wf_ext->SubFormat.Data2 );
413
207
        guid_subformat.Data3 = GetWLE( &p_wf_ext->SubFormat.Data3 );
414
415
207
        sf_tag_to_fourcc( &guid_subformat, &p_sys->fmt.i_codec, &psz_name );
416
417
207
        msg_Dbg( p_demux, "extensible format guid " GUID_FMT, GUID_PRINT(guid_subformat) );
418
419
207
        i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
420
207
        p_sys->fmt.i_extra -= i_extended;
421
422
207
        i_channel_mask = GetDWLE( &p_wf_ext->dwChannelMask );
423
207
        if( i_channel_mask )
424
203
        {
425
203
            int i_match = 0;
426
203
            p_sys->i_channel_mask = getChannelMask( &i_channel_mask, p_sys->fmt.audio.i_channels, &i_match );
427
203
            if( i_channel_mask )
428
203
                msg_Warn( p_demux, "Some channels are unrecognized or uselessly specified (0x%x)", i_channel_mask );
429
203
            if( i_match < p_sys->fmt.audio.i_channels )
430
117
            {
431
117
                int i_missing = p_sys->fmt.audio.i_channels - i_match;
432
117
                msg_Warn( p_demux, "Trying to fill up unspecified position for %d channels", p_sys->fmt.audio.i_channels - i_match );
433
434
117
                static const uint32_t pi_pair[] = { AOUT_CHAN_REARLEFT|AOUT_CHAN_REARRIGHT,
435
117
                                                    AOUT_CHAN_MIDDLELEFT|AOUT_CHAN_MIDDLERIGHT,
436
117
                                                    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
468
                for( unsigned i = 0; i < sizeof(pi_pair)/sizeof(*pi_pair); i++ )
444
351
                {
445
351
                    if( i_missing >= 2 && !(p_sys->i_channel_mask & pi_pair[i] ) )
446
95
                    {
447
95
                        i_missing -= 2;
448
95
                        p_sys->i_channel_mask |= pi_pair[i];
449
95
                    }
450
351
                }
451
                /* Well fill up with what we can */
452
834
                for( unsigned i = 0; i < sizeof(pi_channels_aout)/sizeof(*pi_channels_aout) && i_missing > 0; i++ )
453
764
                {
454
764
                    if( !( p_sys->i_channel_mask & pi_channels_aout[i] ) )
455
319
                    {
456
319
                        p_sys->i_channel_mask |= pi_channels_aout[i];
457
319
                        i_missing--;
458
459
319
                        if( i_missing <= 0 )
460
47
                            break;
461
319
                    }
462
764
                }
463
464
117
                i_match = p_sys->fmt.audio.i_channels - i_missing;
465
117
            }
466
203
            if( i_match < p_sys->fmt.audio.i_channels )
467
64
            {
468
64
                msg_Err( p_demux, "Invalid/unsupported channel mask" );
469
64
                p_sys->i_channel_mask = 0;
470
64
            }
471
203
        }
472
207
    }
473
1.79k
    if( p_sys->i_channel_mask == 0 && p_sys->fmt.audio.i_channels > 2
474
1.00k
     && p_sys->fmt.audio.i_channels <= AOUT_CHAN_MAX )
475
545
    {
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
545
        static const uint32_t pi_default_channels[] = {
484
545
            AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
485
545
            AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
486
545
            AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_REARCENTER };
487
488
4.06k
        for( unsigned i = 0; i < p_sys->fmt.audio.i_channels &&
489
3.52k
             i < (sizeof(pi_default_channels) / sizeof(*pi_default_channels));
490
3.52k
             i++ )
491
3.52k
            p_sys->i_channel_mask |= pi_default_channels[i];
492
545
    }
493
494
1.79k
    if( p_sys->i_channel_mask )
495
674
    {
496
674
        if( p_sys->fmt.i_codec == VLC_FOURCC('a','r','a','w') ||
497
340
            p_sys->fmt.i_codec == VLC_FOURCC('a','f','l','t') )
498
480
            p_sys->i_chans_to_reorder =
499
480
                aout_CheckChannelReorder( pi_channels_aout, NULL,
500
480
                                          p_sys->i_channel_mask,
501
480
                                          p_sys->pi_chan_table );
502
503
674
        msg_Dbg( p_demux, "channel mask: %x, reordering: %u",
504
674
                 p_sys->i_channel_mask, p_sys->i_chans_to_reorder );
505
674
    }
506
507
1.79k
    p_sys->fmt.audio.i_physical_channels = p_sys->i_channel_mask;
508
509
1.79k
    if( p_sys->fmt.i_extra > 0 )
510
70
    {
511
70
        p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
512
70
        if( unlikely(!p_sys->fmt.p_extra) )
513
0
        {
514
0
            p_sys->fmt.i_extra = 0;
515
0
            goto error;
516
0
        }
517
70
        memcpy( p_sys->fmt.p_extra, (uint8_t *)p_wf + sizeof( WAVEFORMATEX ) + i_extended,
518
70
                p_sys->fmt.i_extra );
519
70
    }
520
521
1.79k
    msg_Dbg( p_demux, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
522
1.79k
             "freq: %u Hz, bitrate: %uKo/s, blockalign: %d, bits/samples: %d, "
523
1.79k
             "extra size: %zu",
524
1.79k
             GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
525
1.79k
             p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
526
1.79k
             p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
527
1.79k
             p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
528
529
1.79k
    free( p_wf );
530
1.79k
    p_wf = NULL;
531
532
1.79k
    switch( p_sys->fmt.i_codec )
533
1.79k
    {
534
476
    case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
535
647
    case VLC_FOURCC( 'a', 'f', 'l', 't' ):
536
647
    case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
537
735
    case VLC_CODEC_ALAW:
538
784
    case VLC_CODEC_MULAW:
539
784
        if( FrameInfo_PCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
540
784
                           &p_sys->fmt ) )
541
52
            goto error;
542
732
        p_sys->fmt.i_codec =
543
732
            vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec,
544
732
                                      p_sys->fmt.audio.i_bitspersample );
545
732
        if( p_sys->fmt.i_codec == 0 ) {
546
23
            msg_Err( p_demux, "Unrecognized codec" );
547
23
            goto error;
548
23
        }
549
709
        break;
550
709
    case VLC_CODEC_ADPCM_MS:
551
    /* FIXME not sure at all FIXME */
552
211
    case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
553
278
    case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
554
278
        if( FrameInfo_MS_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
555
278
                                &p_sys->fmt ) )
556
4
            goto error;
557
274
        break;
558
274
    case VLC_CODEC_ADPCM_IMA_WAV:
559
168
        if( FrameInfo_IMA_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
560
168
                                 &p_sys->fmt ) )
561
4
            goto error;
562
164
        break;
563
164
    case VLC_CODEC_ADPCM_CREATIVE:
564
39
        if( FrameInfo_Creative_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
565
39
                                      &p_sys->fmt ) )
566
2
            goto error;
567
37
        break;
568
37
    case VLC_CODEC_MPGA:
569
3
    case VLC_CODEC_A52:
570
        /* FIXME set end of area FIXME */
571
3
        goto error;
572
23
    case VLC_CODEC_GSM_MS:
573
30
    case VLC_CODEC_ADPCM_G726:
574
49
    case VLC_CODEC_TRUESPEECH:
575
49
    case VLC_CODEC_ATRAC3P:
576
53
    case VLC_CODEC_ATRAC3:
577
81
    case VLC_CODEC_G723_1:
578
239
    case VLC_CODEC_WMA2:
579
239
        if( FrameInfo_MSGSM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
580
239
                             &p_sys->fmt ) )
581
2
            goto error;
582
237
        break;
583
281
    default:
584
281
        msg_Err( p_demux, "unsupported codec (%4.4s)",
585
281
                 (char*)&p_sys->fmt.i_codec );
586
281
        goto error;
587
1.79k
    }
588
589
1.42k
    if( p_sys->i_frame_size == 0 || p_sys->i_frame_samples == 0 )
590
44
    {
591
44
        msg_Dbg( p_demux, "invalid frame size: 0 0" );
592
44
        goto error;
593
44
    }
594
1.37k
    if( p_sys->fmt.audio.i_rate == 0 )
595
5
    {
596
5
        msg_Dbg( p_demux, "invalid sample rate: 0" );
597
5
        goto error;
598
5
    }
599
600
1.37k
    msg_Dbg( p_demux, "found %s audio format", psz_name );
601
602
1.37k
    return VLC_SUCCESS;
603
604
498
error:
605
498
    es_format_Clean( &p_sys->fmt );
606
498
    InitFmt( p_demux );
607
498
    free( p_wf );
608
498
    return VLC_EGENERIC;
609
1.37k
}
610
611
static int Open( vlc_object_t * p_this )
612
3.39k
{
613
3.39k
    demux_t     *p_demux = (demux_t*)p_this;
614
3.39k
    demux_sys_t *p_sys;
615
616
3.39k
    const uint8_t *p_peek;
617
3.39k
    bool           b_is_rf64;
618
3.39k
    uint32_t       i_size;
619
620
    /* Is it a wav file ? */
621
3.39k
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
622
8
        return VLC_EGENERIC;
623
624
3.38k
    b_is_rf64 = ( memcmp( p_peek, "RF64", 4 ) == 0 );
625
3.38k
    if( ( !b_is_rf64 && memcmp( p_peek, "RIFF", 4 ) ) ||
626
2.38k
          memcmp( &p_peek[8], "WAVE", 4 ) )
627
1.02k
    {
628
1.02k
        return VLC_EGENERIC;
629
1.02k
    }
630
631
2.36k
    p_demux->pf_demux     = Demux;
632
2.36k
    p_demux->pf_control   = Control;
633
2.36k
    p_demux->p_sys        = p_sys = malloc( sizeof( *p_sys ) );
634
2.36k
    if( unlikely(!p_sys) )
635
0
        return VLC_ENOMEM;
636
637
2.36k
    p_sys->p_es           = NULL;
638
2.36k
    p_sys->i_data_pos = p_sys->i_data_size = 0;
639
2.36k
    InitFmt( p_demux );
640
641
    /* skip riff header */
642
2.36k
    if( vlc_stream_Read( p_demux->s, NULL, 12 ) != 12 )
643
0
        goto error;
644
645
2.36k
    bool eof = false;
646
2.36k
    enum wav_chunk_id id;
647
8.14k
    while( !eof && ( ChunkGetNext( p_demux, &id, &i_size ) ) == VLC_SUCCESS )
648
6.41k
    {
649
6.41k
        if( i_size == 0 )
650
1
        {
651
1
            msg_Err( p_demux, "invalid chunk with a size 0");
652
1
            goto error;
653
1
        }
654
655
6.41k
        switch( id )
656
6.41k
        {
657
2.06k
            case wav_chunk_id_data:
658
2.06k
            {
659
2.06k
                uint64_t i_stream_size;
660
2.06k
                if( vlc_stream_GetSize( p_demux->s, &i_stream_size ) != VLC_SUCCESS )
661
0
                    goto error;
662
2.06k
                p_sys->i_data_pos = vlc_stream_Tell( p_demux->s );
663
664
2.06k
                if( !b_is_rf64 && i_stream_size >= i_size + p_sys->i_data_pos )
665
983
                    p_sys->i_data_size = i_size;
666
667
2.06k
                if( likely( b_is_rf64
668
2.06k
                 || p_sys->i_data_pos + i_size == i_stream_size ) )
669
1.11k
                {
670
                    /* Bypass the final ChunkGetNext() to avoid a read+seek
671
                     * since this chunk is the last one */
672
1.11k
                    eof = true;
673
1.11k
                }   /* Unlikely case where there is a chunk after 'data' */
674
943
                else if( ChunkSkip( p_demux, i_size ) != VLC_SUCCESS )
675
37
                    goto error;
676
2.02k
                break;
677
2.06k
            }
678
2.23k
            case wav_chunk_id_ds64:
679
2.23k
                if( b_is_rf64 )
680
2.23k
                {
681
2.23k
                    if( ChunkParseDS64( p_demux, i_size ) != VLC_SUCCESS )
682
103
                        goto error;
683
2.23k
                }
684
1
                else
685
1
                {
686
1
                    msg_Err( p_demux, "'ds64' chunk found but format not RF64" );
687
1
                    goto error;
688
1
                }
689
2.13k
                break;
690
2.13k
            case wav_chunk_id_fmt:
691
2.12k
                if( p_sys->i_frame_samples != 0 )
692
251
                {
693
251
                    ChunkSkip( p_demux, i_size );
694
251
                    msg_Warn( p_demux, "fmt chunk already parsed" );
695
251
                    break;
696
251
                }
697
1.87k
                if( ChunkParseFmt( p_demux, i_size ) != VLC_SUCCESS )
698
498
                    goto error;
699
1.37k
                break;
700
6.41k
        }
701
6.41k
    }
702
703
1.72k
    if( p_sys->i_data_pos == 0 || p_sys->i_data_size == 0
704
1.24k
     || p_sys->i_frame_samples == 0 )
705
550
    {
706
550
        msg_Err( p_demux, "'%s' chunk not found",
707
550
                 p_sys->i_data_pos == 0 ? "data" :
708
550
                 p_sys->i_frame_samples == 0 ? "fmt " :
709
550
                 b_is_rf64 ? "ds64" : "data" );
710
550
        goto error;
711
550
    }
712
713
    /* Seek back to data position if needed */
714
1.17k
    if( unlikely( vlc_stream_Tell( p_demux->s ) != p_sys->i_data_pos )
715
105
     && vlc_stream_Seek( p_demux->s, p_sys->i_data_pos ) != VLC_SUCCESS )
716
0
        goto error;
717
718
1.17k
    if( p_sys->fmt.i_bitrate <= 0 )
719
126
    {
720
126
        p_sys->fmt.i_bitrate = (int64_t)p_sys->i_frame_size *
721
126
            p_sys->fmt.audio.i_rate * 8 / p_sys->i_frame_samples;
722
126
    }
723
724
1.17k
    p_sys->fmt.i_id = 0;
725
1.17k
    p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
726
1.17k
    if( unlikely(p_sys->p_es == NULL) )
727
0
        goto error;
728
729
1.17k
    date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
730
1.17k
    date_Set( &p_sys->pts, VLC_TICK_0 );
731
732
1.17k
    return VLC_SUCCESS;
733
734
1.19k
error:
735
1.19k
    es_format_Clean( &p_sys->fmt );
736
1.19k
    free( p_sys );
737
1.19k
    return VLC_EGENERIC;
738
1.17k
}
739
740
108
vlc_module_begin ()
741
54
    set_description( N_("WAV demuxer") )
742
54
    set_subcategory( SUBCAT_INPUT_DEMUX )
743
54
    set_capability( "demux", 142 )
744
108
    set_callbacks( Open, Close )
745
54
vlc_module_end ()