1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#ifdef HAVE_LIMITS_H
24#include <limits.h>
25#endif
26#ifndef INT_MAX
27SDL_COMPILE_TIME_ASSERT(int_size, sizeof(int) == sizeof(Sint32));
28#define INT_MAX SDL_MAX_SINT32
29#endif
30#ifndef SIZE_MAX
31#define SIZE_MAX ((size_t)-1)
32#endif
33
34// Microsoft WAVE file loading routines
35
36#include "SDL_wave.h"
37#include "SDL_sysaudio.h"
38
39/* Reads the value stored at the location of the f1 pointer, multiplies it
40 * with the second argument and then stores the result to f1.
41 * Returns 0 on success, or -1 if the multiplication overflows, in which case f1
42 * does not get modified.
43 */
44static int SafeMult(size_t *f1, size_t f2)
45{
46 if (*f1 > 0 && SIZE_MAX / *f1 <= f2) {
47 return -1;
48 }
49 *f1 *= f2;
50 return 0;
51}
52
53typedef struct ADPCM_DecoderState
54{
55 Uint32 channels; // Number of channels.
56 size_t blocksize; // Size of an ADPCM block in bytes.
57 size_t blockheadersize; // Size of an ADPCM block header in bytes.
58 size_t samplesperblock; // Number of samples per channel in an ADPCM block.
59 size_t framesize; // Size of a sample frame (16-bit PCM) in bytes.
60 Sint64 framestotal; // Total number of sample frames.
61 Sint64 framesleft; // Number of sample frames still to be decoded.
62 void *ddata; // Decoder data from initialization.
63 void *cstate; // Decoding state for each channel.
64
65 // ADPCM data.
66 struct
67 {
68 Uint8 *data;
69 size_t size;
70 size_t pos;
71 } input;
72
73 // Current ADPCM block in the ADPCM data above.
74 struct
75 {
76 Uint8 *data;
77 size_t size;
78 size_t pos;
79 } block;
80
81 // Decoded 16-bit PCM data.
82 struct
83 {
84 Sint16 *data;
85 size_t size;
86 size_t pos;
87 } output;
88} ADPCM_DecoderState;
89
90typedef struct MS_ADPCM_CoeffData
91{
92 Uint16 coeffcount;
93 Sint16 *coeff;
94 Sint16 aligndummy; // Has to be last member.
95} MS_ADPCM_CoeffData;
96
97typedef struct MS_ADPCM_ChannelState
98{
99 Uint16 delta;
100 Sint16 coeff1;
101 Sint16 coeff2;
102} MS_ADPCM_ChannelState;
103
104#ifdef SDL_WAVE_DEBUG_LOG_FORMAT
105static void WaveDebugLogFormat(WaveFile *file)
106{
107 WaveFormat *format = &file->format;
108 const char *fmtstr = "WAVE file: %s, %u Hz, %s, %u bits, %u %s/s";
109 const char *waveformat, *wavechannel, *wavebpsunit = "B";
110 Uint32 wavebps = format->byterate;
111 char channelstr[64];
112
113 SDL_zeroa(channelstr);
114
115 switch (format->encoding) {
116 case PCM_CODE:
117 waveformat = "PCM";
118 break;
119 case IEEE_FLOAT_CODE:
120 waveformat = "IEEE Float";
121 break;
122 case ALAW_CODE:
123 waveformat = "A-law";
124 break;
125 case MULAW_CODE:
126 waveformat = "\xc2\xb5-law";
127 break;
128 case MS_ADPCM_CODE:
129 waveformat = "MS ADPCM";
130 break;
131 case IMA_ADPCM_CODE:
132 waveformat = "IMA ADPCM";
133 break;
134 default:
135 waveformat = "Unknown";
136 break;
137 }
138
139#define SDL_WAVE_DEBUG_CHANNELCFG(STR, CODE) \
140 case CODE: \
141 wavechannel = STR; \
142 break;
143#define SDL_WAVE_DEBUG_CHANNELSTR(STR, CODE) \
144 if (format->channelmask & CODE) { \
145 SDL_strlcat(channelstr, channelstr[0] ? "-" STR : STR, sizeof(channelstr)); \
146 }
147
148 if (format->formattag == EXTENSIBLE_CODE && format->channelmask > 0) {
149 switch (format->channelmask) {
150 SDL_WAVE_DEBUG_CHANNELCFG("1.0 Mono", 0x4)
151 SDL_WAVE_DEBUG_CHANNELCFG("1.1 Mono", 0xc)
152 SDL_WAVE_DEBUG_CHANNELCFG("2.0 Stereo", 0x3)
153 SDL_WAVE_DEBUG_CHANNELCFG("2.1 Stereo", 0xb)
154 SDL_WAVE_DEBUG_CHANNELCFG("3.0 Stereo", 0x7)
155 SDL_WAVE_DEBUG_CHANNELCFG("3.1 Stereo", 0xf)
156 SDL_WAVE_DEBUG_CHANNELCFG("3.0 Surround", 0x103)
157 SDL_WAVE_DEBUG_CHANNELCFG("3.1 Surround", 0x10b)
158 SDL_WAVE_DEBUG_CHANNELCFG("4.0 Quad", 0x33)
159 SDL_WAVE_DEBUG_CHANNELCFG("4.1 Quad", 0x3b)
160 SDL_WAVE_DEBUG_CHANNELCFG("4.0 Surround", 0x107)
161 SDL_WAVE_DEBUG_CHANNELCFG("4.1 Surround", 0x10f)
162 SDL_WAVE_DEBUG_CHANNELCFG("5.0", 0x37)
163 SDL_WAVE_DEBUG_CHANNELCFG("5.1", 0x3f)
164 SDL_WAVE_DEBUG_CHANNELCFG("5.0 Side", 0x607)
165 SDL_WAVE_DEBUG_CHANNELCFG("5.1 Side", 0x60f)
166 SDL_WAVE_DEBUG_CHANNELCFG("6.0", 0x137)
167 SDL_WAVE_DEBUG_CHANNELCFG("6.1", 0x13f)
168 SDL_WAVE_DEBUG_CHANNELCFG("6.0 Side", 0x707)
169 SDL_WAVE_DEBUG_CHANNELCFG("6.1 Side", 0x70f)
170 SDL_WAVE_DEBUG_CHANNELCFG("7.0", 0xf7)
171 SDL_WAVE_DEBUG_CHANNELCFG("7.1", 0xff)
172 SDL_WAVE_DEBUG_CHANNELCFG("7.0 Side", 0x6c7)
173 SDL_WAVE_DEBUG_CHANNELCFG("7.1 Side", 0x6cf)
174 SDL_WAVE_DEBUG_CHANNELCFG("7.0 Surround", 0x637)
175 SDL_WAVE_DEBUG_CHANNELCFG("7.1 Surround", 0x63f)
176 SDL_WAVE_DEBUG_CHANNELCFG("9.0 Surround", 0x5637)
177 SDL_WAVE_DEBUG_CHANNELCFG("9.1 Surround", 0x563f)
178 SDL_WAVE_DEBUG_CHANNELCFG("11.0 Surround", 0x56f7)
179 SDL_WAVE_DEBUG_CHANNELCFG("11.1 Surround", 0x56ff)
180 default:
181 SDL_WAVE_DEBUG_CHANNELSTR("FL", 0x1)
182 SDL_WAVE_DEBUG_CHANNELSTR("FR", 0x2)
183 SDL_WAVE_DEBUG_CHANNELSTR("FC", 0x4)
184 SDL_WAVE_DEBUG_CHANNELSTR("LF", 0x8)
185 SDL_WAVE_DEBUG_CHANNELSTR("BL", 0x10)
186 SDL_WAVE_DEBUG_CHANNELSTR("BR", 0x20)
187 SDL_WAVE_DEBUG_CHANNELSTR("FLC", 0x40)
188 SDL_WAVE_DEBUG_CHANNELSTR("FRC", 0x80)
189 SDL_WAVE_DEBUG_CHANNELSTR("BC", 0x100)
190 SDL_WAVE_DEBUG_CHANNELSTR("SL", 0x200)
191 SDL_WAVE_DEBUG_CHANNELSTR("SR", 0x400)
192 SDL_WAVE_DEBUG_CHANNELSTR("TC", 0x800)
193 SDL_WAVE_DEBUG_CHANNELSTR("TFL", 0x1000)
194 SDL_WAVE_DEBUG_CHANNELSTR("TFC", 0x2000)
195 SDL_WAVE_DEBUG_CHANNELSTR("TFR", 0x4000)
196 SDL_WAVE_DEBUG_CHANNELSTR("TBL", 0x8000)
197 SDL_WAVE_DEBUG_CHANNELSTR("TBC", 0x10000)
198 SDL_WAVE_DEBUG_CHANNELSTR("TBR", 0x20000)
199 break;
200 }
201 } else {
202 switch (format->channels) {
203 default:
204 if (SDL_snprintf(channelstr, sizeof(channelstr), "%u channels", format->channels) >= 0) {
205 wavechannel = channelstr;
206 break;
207 }
208 case 0:
209 wavechannel = "Unknown";
210 break;
211 case 1:
212 wavechannel = "Mono";
213 break;
214 case 2:
215 wavechannel = "Setero";
216 break;
217 }
218 }
219
220#undef SDL_WAVE_DEBUG_CHANNELCFG
221#undef SDL_WAVE_DEBUG_CHANNELSTR
222
223 if (wavebps >= 1024) {
224 wavebpsunit = "KiB";
225 wavebps = wavebps / 1024 + (wavebps & 0x3ff ? 1 : 0);
226 }
227
228 SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, fmtstr, waveformat, format->frequency, wavechannel, format->bitspersample, wavebps, wavebpsunit);
229}
230#endif
231
232#ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
233static void WaveDebugDumpFormat(WaveFile *file, Uint32 rifflen, Uint32 fmtlen, Uint32 datalen)
234{
235 WaveFormat *format = &file->format;
236 const char *fmtstr1 = "WAVE chunk dump:\n"
237 "-------------------------------------------\n"
238 "RIFF %11u\n"
239 "-------------------------------------------\n"
240 " fmt %11u\n"
241 " wFormatTag 0x%04x\n"
242 " nChannels %11u\n"
243 " nSamplesPerSec %11u\n"
244 " nAvgBytesPerSec %11u\n"
245 " nBlockAlign %11u\n";
246 const char *fmtstr2 = " wBitsPerSample %11u\n";
247 const char *fmtstr3 = " cbSize %11u\n";
248 const char *fmtstr4a = " wValidBitsPerSample %11u\n";
249 const char *fmtstr4b = " wSamplesPerBlock %11u\n";
250 const char *fmtstr5 = " dwChannelMask 0x%08x\n"
251 " SubFormat\n"
252 " %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n";
253 const char *fmtstr6 = "-------------------------------------------\n"
254 " fact\n"
255 " dwSampleLength %11u\n";
256 const char *fmtstr7 = "-------------------------------------------\n"
257 " data %11u\n"
258 "-------------------------------------------\n";
259 char *dumpstr;
260 size_t dumppos = 0;
261 const size_t bufsize = 1024;
262 int res;
263
264 dumpstr = SDL_malloc(bufsize);
265 if (!dumpstr) {
266 return;
267 }
268 dumpstr[0] = 0;
269
270 res = SDL_snprintf(dumpstr, bufsize, fmtstr1, rifflen, fmtlen, format->formattag, format->channels, format->frequency, format->byterate, format->blockalign);
271 dumppos += res > 0 ? res : 0;
272 if (fmtlen >= 16) {
273 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr2, format->bitspersample);
274 dumppos += res > 0 ? res : 0;
275 }
276 if (fmtlen >= 18) {
277 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr3, format->extsize);
278 dumppos += res > 0 ? res : 0;
279 }
280 if (format->formattag == EXTENSIBLE_CODE && fmtlen >= 40 && format->extsize >= 22) {
281 const Uint8 *g = format->subformat;
282 const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
283 const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
284 const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
285
286 switch (format->encoding) {
287 default:
288 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4a, format->validsamplebits);
289 dumppos += res > 0 ? res : 0;
290 break;
291 case MS_ADPCM_CODE:
292 case IMA_ADPCM_CODE:
293 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
294 dumppos += res > 0 ? res : 0;
295 break;
296 }
297 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr5, format->channelmask, g1, g2, g3, g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]);
298 dumppos += res > 0 ? res : 0;
299 } else {
300 switch (format->encoding) {
301 case MS_ADPCM_CODE:
302 case IMA_ADPCM_CODE:
303 if (fmtlen >= 20 && format->extsize >= 2) {
304 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
305 dumppos += res > 0 ? res : 0;
306 }
307 break;
308 }
309 }
310 if (file->fact.status >= 1) {
311 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr6, file->fact.samplelength);
312 dumppos += res > 0 ? res : 0;
313 }
314 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr7, datalen);
315 dumppos += res > 0 ? res : 0;
316
317 SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", dumpstr);
318
319 SDL_free(dumpstr);
320}
321#endif
322
323static Sint64 WaveAdjustToFactValue(WaveFile *file, Sint64 sampleframes)
324{
325 if (file->fact.status == 2) {
326 if (file->facthint == FactStrict && sampleframes < file->fact.samplelength) {
327 SDL_SetError("Invalid number of sample frames in WAVE fact chunk (too many)");
328 return -1;
329 } else if (sampleframes > file->fact.samplelength) {
330 return file->fact.samplelength;
331 }
332 }
333
334 return sampleframes;
335}
336
337static bool MS_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
338{
339 WaveFormat *format = &file->format;
340 const size_t blockheadersize = (size_t)file->format.channels * 7;
341 const size_t availableblocks = datalength / file->format.blockalign;
342 const size_t blockframebitsize = (size_t)file->format.bitspersample * file->format.channels;
343 const size_t trailingdata = datalength % file->format.blockalign;
344
345 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
346 // The size of the data chunk must be a multiple of the block size.
347 if (datalength < blockheadersize || trailingdata > 0) {
348 return SDL_SetError("Truncated MS ADPCM block");
349 }
350 }
351
352 // Calculate number of sample frames that will be decoded.
353 file->sampleframes = (Sint64)availableblocks * format->samplesperblock;
354 if (trailingdata > 0) {
355 // The last block is truncated. Check if we can get any samples out of it.
356 if (file->trunchint == TruncDropFrame) {
357 // Drop incomplete sample frame.
358 if (trailingdata >= blockheadersize) {
359 size_t trailingsamples = 2 + (trailingdata - blockheadersize) * 8 / blockframebitsize;
360 if (trailingsamples > format->samplesperblock) {
361 trailingsamples = format->samplesperblock;
362 }
363 file->sampleframes += trailingsamples;
364 }
365 }
366 }
367
368 file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
369 if (file->sampleframes < 0) {
370 return false;
371 }
372
373 return true;
374}
375
376static bool MS_ADPCM_Init(WaveFile *file, size_t datalength)
377{
378 WaveFormat *format = &file->format;
379 WaveChunk *chunk = &file->chunk;
380 const size_t blockheadersize = (size_t)format->channels * 7;
381 const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
382 const size_t blockframebitsize = (size_t)format->bitspersample * format->channels;
383 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
384 const Sint16 presetcoeffs[14] = { 256, 0, 512, -256, 0, 0, 192, 64, 240, 0, 460, -208, 392, -232 };
385 size_t i, coeffcount;
386 MS_ADPCM_CoeffData *coeffdata;
387
388 // Sanity checks.
389
390 /* While it's clear how IMA ADPCM handles more than two channels, the nibble
391 * order of MS ADPCM makes it awkward. The Standards Update does not talk
392 * about supporting more than stereo anyway.
393 */
394 if (format->channels > 2) {
395 return SDL_SetError("Invalid number of channels");
396 }
397
398 if (format->bitspersample != 4) {
399 return SDL_SetError("Invalid MS ADPCM bits per sample of %u", (unsigned int)format->bitspersample);
400 }
401
402 // The block size must be big enough to contain the block header.
403 if (format->blockalign < blockheadersize) {
404 return SDL_SetError("Invalid MS ADPCM block size (nBlockAlign)");
405 }
406
407 if (format->formattag == EXTENSIBLE_CODE) {
408 /* Does have a GUID (like all format tags), but there's no specification
409 * for how the data is packed into the extensible header. Making
410 * assumptions here could lead to new formats nobody wants to support.
411 */
412 return SDL_SetError("MS ADPCM with the extensible header is not supported");
413 }
414
415 /* There are wSamplesPerBlock, wNumCoef, and at least 7 coefficient pairs in
416 * the extended part of the header.
417 */
418 if (chunk->size < 22) {
419 return SDL_SetError("Could not read MS ADPCM format header");
420 }
421
422 format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
423 // Number of coefficient pairs. A pair has two 16-bit integers.
424 coeffcount = chunk->data[20] | ((size_t)chunk->data[21] << 8);
425 /* bPredictor, the integer offset into the coefficients array, is only
426 * 8 bits. It can only address the first 256 coefficients. Let's limit
427 * the count number here.
428 */
429 if (coeffcount > 256) {
430 coeffcount = 256;
431 }
432
433 if (chunk->size < 22 + coeffcount * 4) {
434 return SDL_SetError("Could not read custom coefficients in MS ADPCM format header");
435 } else if (format->extsize < 4 + coeffcount * 4) {
436 return SDL_SetError("Invalid MS ADPCM format header (too small)");
437 } else if (coeffcount < 7) {
438 return SDL_SetError("Missing required coefficients in MS ADPCM format header");
439 }
440
441 coeffdata = (MS_ADPCM_CoeffData *)SDL_malloc(sizeof(MS_ADPCM_CoeffData) + coeffcount * 4);
442 file->decoderdata = coeffdata; // Freed in cleanup.
443 if (!coeffdata) {
444 return false;
445 }
446 coeffdata->coeff = &coeffdata->aligndummy;
447 coeffdata->coeffcount = (Uint16)coeffcount;
448
449 // Copy the 16-bit pairs.
450 for (i = 0; i < coeffcount * 2; i++) {
451 Sint32 c = chunk->data[22 + i * 2] | ((Sint32)chunk->data[23 + i * 2] << 8);
452 if (c >= 0x8000) {
453 c -= 0x10000;
454 }
455 if (i < 14 && c != presetcoeffs[i]) {
456 return SDL_SetError("Wrong preset coefficients in MS ADPCM format header");
457 }
458 coeffdata->coeff[i] = (Sint16)c;
459 }
460
461 /* Technically, wSamplesPerBlock is required, but we have all the
462 * information in the other fields to calculate it, if it's zero.
463 */
464 if (format->samplesperblock == 0) {
465 /* Let's be nice to the encoders that didn't know how to fill this.
466 * The Standards Update calculates it this way:
467 *
468 * x = Block size (in bits) minus header size (in bits)
469 * y = Bit depth multiplied by channel count
470 * z = Number of samples per channel in block header
471 * wSamplesPerBlock = x / y + z
472 */
473 format->samplesperblock = (Uint32)blockdatasamples + 2;
474 }
475
476 /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
477 * the number of samples doesn't fit into the block. The Standards Update
478 * also describes wSamplesPerBlock with a formula that makes it necessary to
479 * always fill the block with the maximum amount of samples, but this is not
480 * enforced here as there are no compatibility issues.
481 * A truncated block header with just one sample is not supported.
482 */
483 if (format->samplesperblock == 1 || blockdatasamples < format->samplesperblock - 2) {
484 return SDL_SetError("Invalid number of samples per MS ADPCM block (wSamplesPerBlock)");
485 }
486
487 if (!MS_ADPCM_CalculateSampleFrames(file, datalength)) {
488 return false;
489 }
490
491 return true;
492}
493
494static Sint16 MS_ADPCM_ProcessNibble(MS_ADPCM_ChannelState *cstate, Sint32 sample1, Sint32 sample2, Uint8 nybble)
495{
496 const Sint32 max_audioval = 32767;
497 const Sint32 min_audioval = -32768;
498 const Uint16 max_deltaval = 65535;
499 const Uint16 adaptive[] = {
500 230, 230, 230, 230, 307, 409, 512, 614,
501 768, 614, 512, 409, 307, 230, 230, 230
502 };
503 Sint32 new_sample;
504 Sint32 errordelta;
505 Uint32 delta = cstate->delta;
506
507 new_sample = (sample1 * cstate->coeff1 + sample2 * cstate->coeff2) / 256;
508 // The nibble is a signed 4-bit error delta.
509 errordelta = (Sint32)nybble - (nybble >= 0x08 ? 0x10 : 0);
510 new_sample += (Sint32)delta * errordelta;
511 if (new_sample < min_audioval) {
512 new_sample = min_audioval;
513 } else if (new_sample > max_audioval) {
514 new_sample = max_audioval;
515 }
516 delta = (delta * adaptive[nybble]) / 256;
517 if (delta < 16) {
518 delta = 16;
519 } else if (delta > max_deltaval) {
520 /* This issue is not described in the Standards Update and therefore
521 * undefined. It seems sensible to prevent overflows with a limit.
522 */
523 delta = max_deltaval;
524 }
525
526 cstate->delta = (Uint16)delta;
527 return (Sint16)new_sample;
528}
529
530static bool MS_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
531{
532 Uint8 coeffindex;
533 const Uint32 channels = state->channels;
534 Sint32 sample;
535 Uint32 c;
536 MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
537 MS_ADPCM_CoeffData *ddata = (MS_ADPCM_CoeffData *)state->ddata;
538
539 for (c = 0; c < channels; c++) {
540 size_t o = c;
541
542 // Load the coefficient pair into the channel state.
543 coeffindex = state->block.data[o];
544 if (coeffindex > ddata->coeffcount) {
545 return SDL_SetError("Invalid MS ADPCM coefficient index in block header");
546 }
547 cstate[c].coeff1 = ddata->coeff[coeffindex * 2];
548 cstate[c].coeff2 = ddata->coeff[coeffindex * 2 + 1];
549
550 // Initial delta value.
551 o = (size_t)channels + c * 2;
552 cstate[c].delta = state->block.data[o] | ((Uint16)state->block.data[o + 1] << 8);
553
554 /* Load the samples from the header. Interestingly, the sample later in
555 * the output stream comes first.
556 */
557 o = (size_t)channels * 3 + c * 2;
558 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
559 if (sample >= 0x8000) {
560 sample -= 0x10000;
561 }
562 state->output.data[state->output.pos + channels] = (Sint16)sample;
563
564 o = (size_t)channels * 5 + c * 2;
565 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
566 if (sample >= 0x8000) {
567 sample -= 0x10000;
568 }
569 state->output.data[state->output.pos] = (Sint16)sample;
570
571 state->output.pos++;
572 }
573
574 state->block.pos += state->blockheadersize;
575
576 // Skip second sample frame that came from the header.
577 state->output.pos += state->channels;
578
579 // Header provided two sample frames.
580 state->framesleft -= 2;
581
582 return true;
583}
584
585/* Decodes the data of the MS ADPCM block. Decoding will stop if a block is too
586 * short, returning with none or partially decoded data. The partial data
587 * will always contain full sample frames (same sample count for each channel).
588 * Incomplete sample frames are discarded.
589 */
590static bool MS_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
591{
592 Uint16 nybble = 0;
593 Sint16 sample1, sample2;
594 const Uint32 channels = state->channels;
595 Uint32 c;
596 MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
597
598 size_t blockpos = state->block.pos;
599 size_t blocksize = state->block.size;
600
601 size_t outpos = state->output.pos;
602
603 Sint64 blockframesleft = state->samplesperblock - 2;
604 if (blockframesleft > state->framesleft) {
605 blockframesleft = state->framesleft;
606 }
607
608 while (blockframesleft > 0) {
609 for (c = 0; c < channels; c++) {
610 if (nybble & 0x4000) {
611 nybble <<= 4;
612 } else if (blockpos < blocksize) {
613 nybble = state->block.data[blockpos++] | 0x4000;
614 } else {
615 // Out of input data. Drop the incomplete frame and return.
616 state->output.pos = outpos - c;
617 return false;
618 }
619
620 // Load previous samples which may come from the block header.
621 sample1 = state->output.data[outpos - channels];
622 sample2 = state->output.data[outpos - channels * 2];
623
624 sample1 = MS_ADPCM_ProcessNibble(cstate + c, sample1, sample2, (nybble >> 4) & 0x0f);
625 state->output.data[outpos++] = sample1;
626 }
627
628 state->framesleft--;
629 blockframesleft--;
630 }
631
632 state->output.pos = outpos;
633
634 return true;
635}
636
637static bool MS_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
638{
639 bool result;
640 size_t bytesleft, outputsize;
641 WaveChunk *chunk = &file->chunk;
642 ADPCM_DecoderState state;
643 MS_ADPCM_ChannelState cstate[2];
644
645 SDL_zero(state);
646 SDL_zeroa(cstate);
647
648 if (chunk->size != chunk->length) {
649 // Could not read everything. Recalculate number of sample frames.
650 if (!MS_ADPCM_CalculateSampleFrames(file, chunk->size)) {
651 return false;
652 }
653 }
654
655 // Nothing to decode, nothing to return.
656 if (file->sampleframes == 0) {
657 *audio_buf = NULL;
658 *audio_len = 0;
659 return true;
660 }
661
662 state.blocksize = file->format.blockalign;
663 state.channels = file->format.channels;
664 state.blockheadersize = (size_t)state.channels * 7;
665 state.samplesperblock = file->format.samplesperblock;
666 state.framesize = state.channels * sizeof(Sint16);
667 state.ddata = file->decoderdata;
668 state.framestotal = file->sampleframes;
669 state.framesleft = state.framestotal;
670
671 state.input.data = chunk->data;
672 state.input.size = chunk->size;
673 state.input.pos = 0;
674
675 // The output size in bytes. May get modified if data is truncated.
676 outputsize = (size_t)state.framestotal;
677 if (SafeMult(&outputsize, state.framesize)) {
678 return SDL_SetError("WAVE file too big");
679 } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
680 return SDL_SetError("WAVE file too big");
681 }
682
683 state.output.pos = 0;
684 state.output.size = outputsize / sizeof(Sint16);
685 state.output.data = (Sint16 *)SDL_calloc(1, outputsize);
686 if (!state.output.data) {
687 return false;
688 }
689
690 state.cstate = cstate;
691
692 // Decode block by block. A truncated block will stop the decoding.
693 bytesleft = state.input.size - state.input.pos;
694 while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
695 state.block.data = state.input.data + state.input.pos;
696 state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
697 state.block.pos = 0;
698
699 if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
700 // Somehow didn't allocate enough space for the output.
701 SDL_free(state.output.data);
702 return SDL_SetError("Unexpected overflow in MS ADPCM decoder");
703 }
704
705 // Initialize decoder with the values from the block header.
706 result = MS_ADPCM_DecodeBlockHeader(&state);
707 if (!result) {
708 SDL_free(state.output.data);
709 return false;
710 }
711
712 // Decode the block data. It stores the samples directly in the output.
713 result = MS_ADPCM_DecodeBlockData(&state);
714 if (!result) {
715 // Unexpected end. Stop decoding and return partial data if necessary.
716 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
717 SDL_free(state.output.data);
718 return SDL_SetError("Truncated data chunk");
719 } else if (file->trunchint != TruncDropFrame) {
720 state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
721 }
722 outputsize = state.output.pos * sizeof(Sint16); // Can't overflow, is always smaller.
723 break;
724 }
725
726 state.input.pos += state.block.size;
727 bytesleft = state.input.size - state.input.pos;
728 }
729
730 *audio_buf = (Uint8 *)state.output.data;
731 *audio_len = (Uint32)outputsize;
732
733 return true;
734}
735
736static bool IMA_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
737{
738 WaveFormat *format = &file->format;
739 const size_t blockheadersize = (size_t)format->channels * 4;
740 const size_t subblockframesize = (size_t)format->channels * 4;
741 const size_t availableblocks = datalength / format->blockalign;
742 const size_t trailingdata = datalength % format->blockalign;
743
744 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
745 // The size of the data chunk must be a multiple of the block size.
746 if (datalength < blockheadersize || trailingdata > 0) {
747 return SDL_SetError("Truncated IMA ADPCM block");
748 }
749 }
750
751 // Calculate number of sample frames that will be decoded.
752 file->sampleframes = (Uint64)availableblocks * format->samplesperblock;
753 if (trailingdata > 0) {
754 // The last block is truncated. Check if we can get any samples out of it.
755 if (file->trunchint == TruncDropFrame && trailingdata > blockheadersize - 2) {
756 /* The sample frame in the header of the truncated block is present.
757 * Drop incomplete sample frames.
758 */
759 size_t trailingsamples = 1;
760
761 if (trailingdata > blockheadersize) {
762 // More data following after the header.
763 const size_t trailingblockdata = trailingdata - blockheadersize;
764 const size_t trailingsubblockdata = trailingblockdata % subblockframesize;
765 trailingsamples += (trailingblockdata / subblockframesize) * 8;
766 /* Due to the interleaved sub-blocks, the last 4 bytes determine
767 * how many samples of the truncated sub-block are lost.
768 */
769 if (trailingsubblockdata > subblockframesize - 4) {
770 trailingsamples += (trailingsubblockdata % 4) * 2;
771 }
772 }
773
774 if (trailingsamples > format->samplesperblock) {
775 trailingsamples = format->samplesperblock;
776 }
777 file->sampleframes += trailingsamples;
778 }
779 }
780
781 file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
782 if (file->sampleframes < 0) {
783 return false;
784 }
785
786 return true;
787}
788
789static bool IMA_ADPCM_Init(WaveFile *file, size_t datalength)
790{
791 WaveFormat *format = &file->format;
792 WaveChunk *chunk = &file->chunk;
793 const size_t blockheadersize = (size_t)format->channels * 4;
794 const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
795 const size_t blockframebitsize = (size_t)format->bitspersample * format->channels;
796 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
797
798 // Sanity checks.
799
800 // IMA ADPCM can also have 3-bit samples, but it's not supported by SDL at this time.
801 if (format->bitspersample == 3) {
802 return SDL_SetError("3-bit IMA ADPCM currently not supported");
803 } else if (format->bitspersample != 4) {
804 return SDL_SetError("Invalid IMA ADPCM bits per sample of %u", (unsigned int)format->bitspersample);
805 }
806
807 /* The block size is required to be a multiple of 4 and it must be able to
808 * hold a block header.
809 */
810 if (format->blockalign < blockheadersize || format->blockalign % 4) {
811 return SDL_SetError("Invalid IMA ADPCM block size (nBlockAlign)");
812 }
813
814 if (format->formattag == EXTENSIBLE_CODE) {
815 /* There's no specification for this, but it's basically the same
816 * format because the extensible header has wSampePerBlocks too.
817 */
818 } else {
819 // The Standards Update says there 'should' be 2 bytes for wSamplesPerBlock.
820 if (chunk->size >= 20 && format->extsize >= 2) {
821 format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
822 }
823 }
824
825 if (format->samplesperblock == 0) {
826 /* Field zero? No problem. We just assume the encoder packed the block.
827 * The specification calculates it this way:
828 *
829 * x = Block size (in bits) minus header size (in bits)
830 * y = Bit depth multiplied by channel count
831 * z = Number of samples per channel in header
832 * wSamplesPerBlock = x / y + z
833 */
834 format->samplesperblock = (Uint32)blockdatasamples + 1;
835 }
836
837 /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
838 * the number of samples doesn't fit into the block. The Standards Update
839 * also describes wSamplesPerBlock with a formula that makes it necessary
840 * to always fill the block with the maximum amount of samples, but this is
841 * not enforced here as there are no compatibility issues.
842 */
843 if (blockdatasamples < format->samplesperblock - 1) {
844 return SDL_SetError("Invalid number of samples per IMA ADPCM block (wSamplesPerBlock)");
845 }
846
847 if (!IMA_ADPCM_CalculateSampleFrames(file, datalength)) {
848 return false;
849 }
850
851 return true;
852}
853
854static Sint16 IMA_ADPCM_ProcessNibble(Sint8 *cindex, Sint16 lastsample, Uint8 nybble)
855{
856 const Sint32 max_audioval = 32767;
857 const Sint32 min_audioval = -32768;
858 const Sint8 index_table_4b[16] = {
859 -1, -1, -1, -1,
860 2, 4, 6, 8,
861 -1, -1, -1, -1,
862 2, 4, 6, 8
863 };
864 const Uint16 step_table[89] = {
865 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
866 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
867 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
868 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
869 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
870 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
871 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
872 22385, 24623, 27086, 29794, 32767
873 };
874 Uint32 step;
875 Sint32 sample, delta;
876 Sint8 index = *cindex;
877
878 // Clamp index into valid range.
879 if (index > 88) {
880 index = 88;
881 } else if (index < 0) {
882 index = 0;
883 }
884
885 // explicit cast to avoid gcc warning about using 'char' as array index
886 step = step_table[(size_t)index];
887
888 // Update index value
889 *cindex = index + index_table_4b[nybble];
890
891 /* This calculation uses shifts and additions because multiplications were
892 * much slower back then. Sadly, this can't just be replaced with an actual
893 * multiplication now as the old algorithm drops some bits. The closest
894 * approximation I could find is something like this:
895 * (nybble & 0x8 ? -1 : 1) * ((nybble & 0x7) * step / 4 + step / 8)
896 */
897 delta = step >> 3;
898 if (nybble & 0x04) {
899 delta += step;
900 }
901 if (nybble & 0x02) {
902 delta += step >> 1;
903 }
904 if (nybble & 0x01) {
905 delta += step >> 2;
906 }
907 if (nybble & 0x08) {
908 delta = -delta;
909 }
910
911 sample = lastsample + delta;
912
913 // Clamp output sample
914 if (sample > max_audioval) {
915 sample = max_audioval;
916 } else if (sample < min_audioval) {
917 sample = min_audioval;
918 }
919
920 return (Sint16)sample;
921}
922
923static bool IMA_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
924{
925 Sint16 step;
926 Uint32 c;
927 Uint8 *cstate = (Uint8 *)state->cstate;
928
929 for (c = 0; c < state->channels; c++) {
930 size_t o = state->block.pos + c * 4;
931
932 // Extract the sample from the header.
933 Sint32 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
934 if (sample >= 0x8000) {
935 sample -= 0x10000;
936 }
937 state->output.data[state->output.pos++] = (Sint16)sample;
938
939 // Channel step index.
940 step = (Sint16)state->block.data[o + 2];
941 cstate[c] = (Sint8)(step > 0x80 ? step - 0x100 : step);
942
943 // Reserved byte in block header, should be 0.
944 if (state->block.data[o + 3] != 0) {
945 /* Uh oh, corrupt data? Buggy code? */;
946 }
947 }
948
949 state->block.pos += state->blockheadersize;
950
951 // Header provided one sample frame.
952 state->framesleft--;
953
954 return true;
955}
956
957/* Decodes the data of the IMA ADPCM block. Decoding will stop if a block is too
958 * short, returning with none or partially decoded data. The partial data always
959 * contains full sample frames (same sample count for each channel).
960 * Incomplete sample frames are discarded.
961 */
962static bool IMA_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
963{
964 size_t i;
965 const Uint32 channels = state->channels;
966 const size_t subblockframesize = (size_t)channels * 4;
967 Uint64 bytesrequired;
968 Uint32 c;
969 bool result = true;
970
971 size_t blockpos = state->block.pos;
972 size_t blocksize = state->block.size;
973 size_t blockleft = blocksize - blockpos;
974
975 size_t outpos = state->output.pos;
976
977 Sint64 blockframesleft = state->samplesperblock - 1;
978 if (blockframesleft > state->framesleft) {
979 blockframesleft = state->framesleft;
980 }
981
982 bytesrequired = (blockframesleft + 7) / 8 * subblockframesize;
983 if (blockleft < bytesrequired) {
984 // Data truncated. Calculate how many samples we can get out if it.
985 const size_t guaranteedframes = blockleft / subblockframesize;
986 const size_t remainingbytes = blockleft % subblockframesize;
987 blockframesleft = guaranteedframes;
988 if (remainingbytes > subblockframesize - 4) {
989 blockframesleft += (Sint64)(remainingbytes % 4) * 2;
990 }
991 // Signal the truncation.
992 result = false;
993 }
994
995 /* Each channel has their nibbles packed into 32-bit blocks. These blocks
996 * are interleaved and make up the data part of the ADPCM block. This loop
997 * decodes the samples as they come from the input data and puts them at
998 * the appropriate places in the output data.
999 */
1000 while (blockframesleft > 0) {
1001 const size_t subblocksamples = blockframesleft < 8 ? (size_t)blockframesleft : 8;
1002
1003 for (c = 0; c < channels; c++) {
1004 Uint8 nybble = 0;
1005 // Load previous sample which may come from the block header.
1006 Sint16 sample = state->output.data[outpos + c - channels];
1007
1008 for (i = 0; i < subblocksamples; i++) {
1009 if (i & 1) {
1010 nybble >>= 4;
1011 } else {
1012 nybble = state->block.data[blockpos++];
1013 }
1014
1015 sample = IMA_ADPCM_ProcessNibble((Sint8 *)state->cstate + c, sample, nybble & 0x0f);
1016 state->output.data[outpos + c + i * channels] = sample;
1017 }
1018 }
1019
1020 outpos += channels * subblocksamples;
1021 state->framesleft -= subblocksamples;
1022 blockframesleft -= subblocksamples;
1023 }
1024
1025 state->block.pos = blockpos;
1026 state->output.pos = outpos;
1027
1028 return result;
1029}
1030
1031static bool IMA_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1032{
1033 bool result;
1034 size_t bytesleft, outputsize;
1035 WaveChunk *chunk = &file->chunk;
1036 ADPCM_DecoderState state;
1037 Sint8 *cstate;
1038
1039 if (chunk->size != chunk->length) {
1040 // Could not read everything. Recalculate number of sample frames.
1041 if (!IMA_ADPCM_CalculateSampleFrames(file, chunk->size)) {
1042 return false;
1043 }
1044 }
1045
1046 // Nothing to decode, nothing to return.
1047 if (file->sampleframes == 0) {
1048 *audio_buf = NULL;
1049 *audio_len = 0;
1050 return true;
1051 }
1052
1053 SDL_zero(state);
1054 state.channels = file->format.channels;
1055 state.blocksize = file->format.blockalign;
1056 state.blockheadersize = (size_t)state.channels * 4;
1057 state.samplesperblock = file->format.samplesperblock;
1058 state.framesize = state.channels * sizeof(Sint16);
1059 state.framestotal = file->sampleframes;
1060 state.framesleft = state.framestotal;
1061
1062 state.input.data = chunk->data;
1063 state.input.size = chunk->size;
1064 state.input.pos = 0;
1065
1066 // The output size in bytes. May get modified if data is truncated.
1067 outputsize = (size_t)state.framestotal;
1068 if (SafeMult(&outputsize, state.framesize)) {
1069 return SDL_SetError("WAVE file too big");
1070 } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
1071 return SDL_SetError("WAVE file too big");
1072 }
1073
1074 state.output.pos = 0;
1075 state.output.size = outputsize / sizeof(Sint16);
1076 state.output.data = (Sint16 *)SDL_malloc(outputsize);
1077 if (!state.output.data) {
1078 return false;
1079 }
1080
1081 cstate = (Sint8 *)SDL_calloc(state.channels, sizeof(Sint8));
1082 if (!cstate) {
1083 SDL_free(state.output.data);
1084 return false;
1085 }
1086 state.cstate = cstate;
1087
1088 // Decode block by block. A truncated block will stop the decoding.
1089 bytesleft = state.input.size - state.input.pos;
1090 while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
1091 state.block.data = state.input.data + state.input.pos;
1092 state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
1093 state.block.pos = 0;
1094
1095 if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
1096 // Somehow didn't allocate enough space for the output.
1097 SDL_free(state.output.data);
1098 SDL_free(cstate);
1099 return SDL_SetError("Unexpected overflow in IMA ADPCM decoder");
1100 }
1101
1102 // Initialize decoder with the values from the block header.
1103 result = IMA_ADPCM_DecodeBlockHeader(&state);
1104 if (result) {
1105 // Decode the block data. It stores the samples directly in the output.
1106 result = IMA_ADPCM_DecodeBlockData(&state);
1107 }
1108
1109 if (!result) {
1110 // Unexpected end. Stop decoding and return partial data if necessary.
1111 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
1112 SDL_free(state.output.data);
1113 SDL_free(cstate);
1114 return SDL_SetError("Truncated data chunk");
1115 } else if (file->trunchint != TruncDropFrame) {
1116 state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
1117 }
1118 outputsize = state.output.pos * sizeof(Sint16); // Can't overflow, is always smaller.
1119 break;
1120 }
1121
1122 state.input.pos += state.block.size;
1123 bytesleft = state.input.size - state.input.pos;
1124 }
1125
1126 *audio_buf = (Uint8 *)state.output.data;
1127 *audio_len = (Uint32)outputsize;
1128
1129 SDL_free(cstate);
1130
1131 return true;
1132}
1133
1134static bool LAW_Init(WaveFile *file, size_t datalength)
1135{
1136 WaveFormat *format = &file->format;
1137
1138 // Standards Update requires this to be 8.
1139 if (format->bitspersample != 8) {
1140 return SDL_SetError("Invalid companded bits per sample of %u", (unsigned int)format->bitspersample);
1141 }
1142
1143 // Not going to bother with weird padding.
1144 if (format->blockalign != format->channels) {
1145 return SDL_SetError("Unsupported block alignment");
1146 }
1147
1148 if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
1149 if (format->blockalign > 1 && datalength % format->blockalign) {
1150 return SDL_SetError("Truncated data chunk in WAVE file");
1151 }
1152 }
1153
1154 file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
1155 if (file->sampleframes < 0) {
1156 return false;
1157 }
1158
1159 return true;
1160}
1161
1162static bool LAW_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1163{
1164#ifdef SDL_WAVE_LAW_LUT
1165 const Sint16 alaw_lut[256] = {
1166 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
1167 -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
1168 -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
1169 -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344,
1170 -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88,
1171 -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
1172 -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
1173 -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504,
1174 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752,
1175 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016,
1176 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008,
1177 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344,
1178 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88,
1179 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376,
1180 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688,
1181 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848
1182 };
1183 const Sint16 mulaw_lut[256] = {
1184 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996,
1185 -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
1186 -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
1187 -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
1188 -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876,
1189 -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372,
1190 -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120,
1191 -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124,
1192 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996,
1193 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932,
1194 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
1195 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884,
1196 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876,
1197 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,
1198 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120,
1199 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0
1200 };
1201#endif
1202
1203 WaveFormat *format = &file->format;
1204 WaveChunk *chunk = &file->chunk;
1205 size_t i, sample_count, expanded_len;
1206 Uint8 *src;
1207 Sint16 *dst;
1208
1209 if (chunk->length != chunk->size) {
1210 file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
1211 if (file->sampleframes < 0) {
1212 return false;
1213 }
1214 }
1215
1216 // Nothing to decode, nothing to return.
1217 if (file->sampleframes == 0) {
1218 *audio_buf = NULL;
1219 *audio_len = 0;
1220 return true;
1221 }
1222
1223 sample_count = (size_t)file->sampleframes;
1224 if (SafeMult(&sample_count, format->channels)) {
1225 return SDL_SetError("WAVE file too big");
1226 }
1227
1228 expanded_len = sample_count;
1229 if (SafeMult(&expanded_len, sizeof(Sint16))) {
1230 return SDL_SetError("WAVE file too big");
1231 } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
1232 return SDL_SetError("WAVE file too big");
1233 }
1234
1235 // 1 to avoid allocating zero bytes, to keep static analysis happy.
1236 src = (Uint8 *)SDL_realloc(chunk->data, expanded_len ? expanded_len : 1);
1237 if (!src) {
1238 return false;
1239 }
1240 chunk->data = NULL;
1241 chunk->size = 0;
1242
1243 dst = (Sint16 *)src;
1244
1245 /* Work backwards, since we're expanding in-place. `format` will
1246 * inform the caller about the byte order.
1247 */
1248 i = sample_count;
1249 switch (file->format.encoding) {
1250#ifdef SDL_WAVE_LAW_LUT
1251 case ALAW_CODE:
1252 while (i--) {
1253 dst[i] = alaw_lut[src[i]];
1254 }
1255 break;
1256 case MULAW_CODE:
1257 while (i--) {
1258 dst[i] = mulaw_lut[src[i]];
1259 }
1260 break;
1261#else
1262 case ALAW_CODE:
1263 while (i--) {
1264 Uint8 nibble = src[i];
1265 Uint8 exponent = (nibble & 0x7f) ^ 0x55;
1266 Sint16 mantissa = exponent & 0xf;
1267
1268 exponent >>= 4;
1269 if (exponent > 0) {
1270 mantissa |= 0x10;
1271 }
1272 mantissa = (mantissa << 4) | 0x8;
1273 if (exponent > 1) {
1274 mantissa <<= exponent - 1;
1275 }
1276
1277 dst[i] = nibble & 0x80 ? mantissa : -mantissa;
1278 }
1279 break;
1280 case MULAW_CODE:
1281 while (i--) {
1282 Uint8 nibble = ~src[i];
1283 Sint16 mantissa = nibble & 0xf;
1284 Uint8 exponent = (nibble >> 4) & 0x7;
1285 Sint16 step = 4 << (exponent + 1);
1286
1287 mantissa = (0x80 << exponent) + step * mantissa + step / 2 - 132;
1288
1289 dst[i] = nibble & 0x80 ? -mantissa : mantissa;
1290 }
1291 break;
1292#endif
1293 default:
1294 SDL_free(src);
1295 return SDL_SetError("Unknown companded encoding");
1296 }
1297
1298 *audio_buf = src;
1299 *audio_len = (Uint32)expanded_len;
1300
1301 return true;
1302}
1303
1304static bool PCM_Init(WaveFile *file, size_t datalength)
1305{
1306 WaveFormat *format = &file->format;
1307
1308 if (format->encoding == PCM_CODE) {
1309 switch (format->bitspersample) {
1310 case 8:
1311 case 16:
1312 case 24:
1313 case 32:
1314 // These are supported.
1315 break;
1316 default:
1317 return SDL_SetError("%u-bit PCM format not supported", (unsigned int)format->bitspersample);
1318 }
1319 } else if (format->encoding == IEEE_FLOAT_CODE) {
1320 if (format->bitspersample != 32) {
1321 return SDL_SetError("%u-bit IEEE floating-point format not supported", (unsigned int)format->bitspersample);
1322 }
1323 }
1324
1325 /* It wouldn't be that hard to support more exotic block sizes, but
1326 * the most common formats should do for now.
1327 */
1328 // Make sure we're a multiple of the blockalign, at least.
1329 if ((format->channels * format->bitspersample) % (format->blockalign * 8)) {
1330 return SDL_SetError("Unsupported block alignment");
1331 }
1332
1333 if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
1334 if (format->blockalign > 1 && datalength % format->blockalign) {
1335 return SDL_SetError("Truncated data chunk in WAVE file");
1336 }
1337 }
1338
1339 file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
1340 if (file->sampleframes < 0) {
1341 return false;
1342 }
1343
1344 return true;
1345}
1346
1347static bool PCM_ConvertSint24ToSint32(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1348{
1349 WaveFormat *format = &file->format;
1350 WaveChunk *chunk = &file->chunk;
1351 size_t i, expanded_len, sample_count;
1352 Uint8 *ptr;
1353
1354 sample_count = (size_t)file->sampleframes;
1355 if (SafeMult(&sample_count, format->channels)) {
1356 return SDL_SetError("WAVE file too big");
1357 }
1358
1359 expanded_len = sample_count;
1360 if (SafeMult(&expanded_len, sizeof(Sint32))) {
1361 return SDL_SetError("WAVE file too big");
1362 } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
1363 return SDL_SetError("WAVE file too big");
1364 }
1365
1366 // 1 to avoid allocating zero bytes, to keep static analysis happy.
1367 ptr = (Uint8 *)SDL_realloc(chunk->data, expanded_len ? expanded_len : 1);
1368 if (!ptr) {
1369 return false;
1370 }
1371
1372 // This pointer is now invalid.
1373 chunk->data = NULL;
1374 chunk->size = 0;
1375
1376 *audio_buf = ptr;
1377 *audio_len = (Uint32)expanded_len;
1378
1379 // work from end to start, since we're expanding in-place.
1380 for (i = sample_count; i > 0; i--) {
1381 const size_t o = i - 1;
1382 uint8_t b[4];
1383
1384 b[0] = 0;
1385 b[1] = ptr[o * 3];
1386 b[2] = ptr[o * 3 + 1];
1387 b[3] = ptr[o * 3 + 2];
1388
1389 ptr[o * 4 + 0] = b[0];
1390 ptr[o * 4 + 1] = b[1];
1391 ptr[o * 4 + 2] = b[2];
1392 ptr[o * 4 + 3] = b[3];
1393 }
1394
1395 return true;
1396}
1397
1398static bool PCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1399{
1400 WaveFormat *format = &file->format;
1401 WaveChunk *chunk = &file->chunk;
1402 size_t outputsize;
1403
1404 if (chunk->length != chunk->size) {
1405 file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
1406 if (file->sampleframes < 0) {
1407 return false;
1408 }
1409 }
1410
1411 // Nothing to decode, nothing to return.
1412 if (file->sampleframes == 0) {
1413 *audio_buf = NULL;
1414 *audio_len = 0;
1415 return true;
1416 }
1417
1418 // 24-bit samples get shifted to 32 bits.
1419 if (format->encoding == PCM_CODE && format->bitspersample == 24) {
1420 return PCM_ConvertSint24ToSint32(file, audio_buf, audio_len);
1421 }
1422
1423 outputsize = (size_t)file->sampleframes;
1424 if (SafeMult(&outputsize, format->blockalign)) {
1425 return SDL_SetError("WAVE file too big");
1426 } else if (outputsize > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
1427 return SDL_SetError("WAVE file too big");
1428 }
1429
1430 *audio_buf = chunk->data;
1431 *audio_len = (Uint32)outputsize;
1432
1433 // This pointer is going to be returned to the caller. Prevent free in cleanup.
1434 chunk->data = NULL;
1435 chunk->size = 0;
1436
1437 return true;
1438}
1439
1440static WaveRiffSizeHint WaveGetRiffSizeHint(void)
1441{
1442 const char *hint = SDL_GetHint(SDL_HINT_WAVE_RIFF_CHUNK_SIZE);
1443
1444 if (hint) {
1445 if (SDL_strcmp(hint, "force") == 0) {
1446 return RiffSizeForce;
1447 } else if (SDL_strcmp(hint, "ignore") == 0) {
1448 return RiffSizeIgnore;
1449 } else if (SDL_strcmp(hint, "ignorezero") == 0) {
1450 return RiffSizeIgnoreZero;
1451 } else if (SDL_strcmp(hint, "maximum") == 0) {
1452 return RiffSizeMaximum;
1453 }
1454 }
1455
1456 return RiffSizeNoHint;
1457}
1458
1459static WaveTruncationHint WaveGetTruncationHint(void)
1460{
1461 const char *hint = SDL_GetHint(SDL_HINT_WAVE_TRUNCATION);
1462
1463 if (hint) {
1464 if (SDL_strcmp(hint, "verystrict") == 0) {
1465 return TruncVeryStrict;
1466 } else if (SDL_strcmp(hint, "strict") == 0) {
1467 return TruncStrict;
1468 } else if (SDL_strcmp(hint, "dropframe") == 0) {
1469 return TruncDropFrame;
1470 } else if (SDL_strcmp(hint, "dropblock") == 0) {
1471 return TruncDropBlock;
1472 }
1473 }
1474
1475 return TruncNoHint;
1476}
1477
1478static WaveFactChunkHint WaveGetFactChunkHint(void)
1479{
1480 const char *hint = SDL_GetHint(SDL_HINT_WAVE_FACT_CHUNK);
1481
1482 if (hint) {
1483 if (SDL_strcmp(hint, "truncate") == 0) {
1484 return FactTruncate;
1485 } else if (SDL_strcmp(hint, "strict") == 0) {
1486 return FactStrict;
1487 } else if (SDL_strcmp(hint, "ignorezero") == 0) {
1488 return FactIgnoreZero;
1489 } else if (SDL_strcmp(hint, "ignore") == 0) {
1490 return FactIgnore;
1491 }
1492 }
1493
1494 return FactNoHint;
1495}
1496
1497static void WaveFreeChunkData(WaveChunk *chunk)
1498{
1499 if (chunk->data) {
1500 SDL_free(chunk->data);
1501 chunk->data = NULL;
1502 }
1503 chunk->size = 0;
1504}
1505
1506static int WaveNextChunk(SDL_IOStream *src, WaveChunk *chunk)
1507{
1508 Uint32 chunkheader[2];
1509 Sint64 nextposition = chunk->position + chunk->length;
1510
1511 // Data is no longer valid after this function returns.
1512 WaveFreeChunkData(chunk);
1513
1514 // Error on overflows.
1515 if (SDL_MAX_SINT64 - chunk->length < chunk->position || SDL_MAX_SINT64 - 8 < nextposition) {
1516 return -1;
1517 }
1518
1519 // RIFF chunks have a 2-byte alignment. Skip padding byte.
1520 if (chunk->length & 1) {
1521 nextposition++;
1522 }
1523
1524 if (SDL_SeekIO(src, nextposition, SDL_IO_SEEK_SET) != nextposition) {
1525 // Not sure how we ended up here. Just abort.
1526 return -2;
1527 } else if (SDL_ReadIO(src, chunkheader, sizeof(Uint32) * 2) != (sizeof(Uint32) * 2)) {
1528 return -1;
1529 }
1530
1531 chunk->fourcc = SDL_Swap32LE(chunkheader[0]);
1532 chunk->length = SDL_Swap32LE(chunkheader[1]);
1533 chunk->position = nextposition + 8;
1534
1535 return 0;
1536}
1537
1538static int WaveReadPartialChunkData(SDL_IOStream *src, WaveChunk *chunk, size_t length)
1539{
1540 WaveFreeChunkData(chunk);
1541
1542 if (length > chunk->length) {
1543 length = chunk->length;
1544 }
1545
1546 if (length > 0) {
1547 chunk->data = (Uint8 *)SDL_malloc(length);
1548 if (!chunk->data) {
1549 return -1;
1550 }
1551
1552 if (SDL_SeekIO(src, chunk->position, SDL_IO_SEEK_SET) != chunk->position) {
1553 // Not sure how we ended up here. Just abort.
1554 return -2;
1555 }
1556
1557 chunk->size = SDL_ReadIO(src, chunk->data, length);
1558 if (chunk->size != length) {
1559 // Expected to be handled by the caller.
1560 }
1561 }
1562
1563 return 0;
1564}
1565
1566static int WaveReadChunkData(SDL_IOStream *src, WaveChunk *chunk)
1567{
1568 return WaveReadPartialChunkData(src, chunk, chunk->length);
1569}
1570
1571typedef struct WaveExtensibleGUID
1572{
1573 Uint16 encoding;
1574 Uint8 guid[16];
1575} WaveExtensibleGUID;
1576
1577// Some of the GUIDs that are used by WAVEFORMATEXTENSIBLE.
1578#define WAVE_FORMATTAG_GUID(tag) \
1579 { \
1580 (tag) & 0xff, (tag) >> 8, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 \
1581 }
1582static WaveExtensibleGUID extensible_guids[] = {
1583 { PCM_CODE, WAVE_FORMATTAG_GUID(PCM_CODE) },
1584 { MS_ADPCM_CODE, WAVE_FORMATTAG_GUID(MS_ADPCM_CODE) },
1585 { IEEE_FLOAT_CODE, WAVE_FORMATTAG_GUID(IEEE_FLOAT_CODE) },
1586 { ALAW_CODE, WAVE_FORMATTAG_GUID(ALAW_CODE) },
1587 { MULAW_CODE, WAVE_FORMATTAG_GUID(MULAW_CODE) },
1588 { IMA_ADPCM_CODE, WAVE_FORMATTAG_GUID(IMA_ADPCM_CODE) }
1589};
1590
1591static Uint16 WaveGetFormatGUIDEncoding(WaveFormat *format)
1592{
1593 size_t i;
1594 for (i = 0; i < SDL_arraysize(extensible_guids); i++) {
1595 if (SDL_memcmp(format->subformat, extensible_guids[i].guid, 16) == 0) {
1596 return extensible_guids[i].encoding;
1597 }
1598 }
1599 return UNKNOWN_CODE;
1600}
1601
1602static bool WaveReadFormat(WaveFile *file)
1603{
1604 WaveChunk *chunk = &file->chunk;
1605 WaveFormat *format = &file->format;
1606 SDL_IOStream *fmtsrc;
1607 size_t fmtlen = chunk->size;
1608
1609 if (fmtlen > SDL_MAX_SINT32) {
1610 // Limit given by SDL_IOFromConstMem.
1611 return SDL_SetError("Data of WAVE fmt chunk too big");
1612 }
1613 fmtsrc = SDL_IOFromConstMem(chunk->data, (int)chunk->size);
1614 if (!fmtsrc) {
1615 return false;
1616 }
1617
1618 if (!SDL_ReadU16LE(fmtsrc, &format->formattag) ||
1619 !SDL_ReadU16LE(fmtsrc, &format->channels) ||
1620 !SDL_ReadU32LE(fmtsrc, &format->frequency) ||
1621 !SDL_ReadU32LE(fmtsrc, &format->byterate) ||
1622 !SDL_ReadU16LE(fmtsrc, &format->blockalign)) {
1623 return false;
1624 }
1625 format->encoding = format->formattag;
1626
1627 // This is PCM specific in the first version of the specification.
1628 if (fmtlen >= 16) {
1629 if (!SDL_ReadU16LE(fmtsrc, &format->bitspersample)) {
1630 return false;
1631 }
1632 } else if (format->encoding == PCM_CODE) {
1633 SDL_CloseIO(fmtsrc);
1634 return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
1635 }
1636
1637 // The earlier versions also don't have this field.
1638 if (fmtlen >= 18) {
1639 if (!SDL_ReadU16LE(fmtsrc, &format->extsize)) {
1640 return false;
1641 }
1642 }
1643
1644 if (format->formattag == EXTENSIBLE_CODE) {
1645 /* note that this ignores channel masks, smaller valid bit counts
1646 * inside a larger container, and most subtypes. This is just enough
1647 * to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE
1648 * to be useful working when they use this format flag.
1649 */
1650
1651 // Extensible header must be at least 22 bytes.
1652 if (fmtlen < 40 || format->extsize < 22) {
1653 SDL_CloseIO(fmtsrc);
1654 return SDL_SetError("Extensible WAVE header too small");
1655 }
1656
1657 if (!SDL_ReadU16LE(fmtsrc, &format->validsamplebits) ||
1658 !SDL_ReadU32LE(fmtsrc, &format->channelmask) ||
1659 SDL_ReadIO(fmtsrc, format->subformat, 16) != 16) {
1660 }
1661 format->samplesperblock = format->validsamplebits;
1662 format->encoding = WaveGetFormatGUIDEncoding(format);
1663 }
1664
1665 SDL_CloseIO(fmtsrc);
1666
1667 return true;
1668}
1669
1670static bool WaveCheckFormat(WaveFile *file, size_t datalength)
1671{
1672 WaveFormat *format = &file->format;
1673
1674 // Check for some obvious issues.
1675
1676 if (format->channels == 0) {
1677 return SDL_SetError("Invalid number of channels");
1678 }
1679
1680 if (format->frequency == 0) {
1681 return SDL_SetError("Invalid sample rate");
1682 } else if (format->frequency > INT_MAX) {
1683 return SDL_SetError("Sample rate exceeds limit of %d", INT_MAX);
1684 }
1685
1686 // Reject invalid fact chunks in strict mode.
1687 if (file->facthint == FactStrict && file->fact.status == -1) {
1688 return SDL_SetError("Invalid fact chunk in WAVE file");
1689 }
1690
1691 /* Check for issues common to all encodings. Some unsupported formats set
1692 * the bits per sample to zero. These fall through to the 'unsupported
1693 * format' error.
1694 */
1695 switch (format->encoding) {
1696 case IEEE_FLOAT_CODE:
1697 case ALAW_CODE:
1698 case MULAW_CODE:
1699 case MS_ADPCM_CODE:
1700 case IMA_ADPCM_CODE:
1701 // These formats require a fact chunk.
1702 if (file->facthint == FactStrict && file->fact.status <= 0) {
1703 return SDL_SetError("Missing fact chunk in WAVE file");
1704 }
1705 SDL_FALLTHROUGH;
1706 case PCM_CODE:
1707 // All supported formats require a non-zero bit depth.
1708 if (file->chunk.size < 16) {
1709 return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
1710 } else if (format->bitspersample == 0) {
1711 return SDL_SetError("Invalid bits per sample");
1712 }
1713
1714 // All supported formats must have a proper block size.
1715 if (format->blockalign == 0) {
1716 format->blockalign = 1; // force it to 1 if it was unset.
1717 }
1718
1719 /* If the fact chunk is valid and the appropriate hint is set, the
1720 * decoders will use the number of sample frames from the fact chunk.
1721 */
1722 if (file->fact.status == 1) {
1723 WaveFactChunkHint hint = file->facthint;
1724 Uint32 samples = file->fact.samplelength;
1725 if (hint == FactTruncate || hint == FactStrict || (hint == FactIgnoreZero && samples > 0)) {
1726 file->fact.status = 2;
1727 }
1728 }
1729 }
1730
1731 // Check the format for encoding specific issues and initialize decoders.
1732 switch (format->encoding) {
1733 case PCM_CODE:
1734 case IEEE_FLOAT_CODE:
1735 if (!PCM_Init(file, datalength)) {
1736 return false;
1737 }
1738 break;
1739 case ALAW_CODE:
1740 case MULAW_CODE:
1741 if (!LAW_Init(file, datalength)) {
1742 return false;
1743 }
1744 break;
1745 case MS_ADPCM_CODE:
1746 if (!MS_ADPCM_Init(file, datalength)) {
1747 return false;
1748 }
1749 break;
1750 case IMA_ADPCM_CODE:
1751 if (!IMA_ADPCM_Init(file, datalength)) {
1752 return false;
1753 }
1754 break;
1755 case MPEG_CODE:
1756 case MPEGLAYER3_CODE:
1757 return SDL_SetError("MPEG formats not supported");
1758 default:
1759 if (format->formattag == EXTENSIBLE_CODE) {
1760 const char *errstr = "Unknown WAVE format GUID: %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x";
1761 const Uint8 *g = format->subformat;
1762 const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
1763 const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
1764 const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
1765 return SDL_SetError(errstr, g1, g2, g3, g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]);
1766 }
1767 return SDL_SetError("Unknown WAVE format tag: 0x%04x", (unsigned int)format->encoding);
1768 }
1769
1770 return true;
1771}
1772
1773static bool WaveLoad(SDL_IOStream *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
1774{
1775 int result;
1776 Uint32 chunkcount = 0;
1777 Uint32 chunkcountlimit = 10000;
1778 const char *hint;
1779 Sint64 RIFFstart, RIFFend, lastchunkpos;
1780 bool RIFFlengthknown = false;
1781 WaveFormat *format = &file->format;
1782 WaveChunk *chunk = &file->chunk;
1783 WaveChunk RIFFchunk;
1784 WaveChunk fmtchunk;
1785 WaveChunk datachunk;
1786
1787 SDL_zero(RIFFchunk);
1788 SDL_zero(fmtchunk);
1789 SDL_zero(datachunk);
1790
1791 hint = SDL_GetHint(SDL_HINT_WAVE_CHUNK_LIMIT);
1792 if (hint) {
1793 unsigned int count;
1794 if (SDL_sscanf(hint, "%u", &count) == 1) {
1795 chunkcountlimit = count <= SDL_MAX_UINT32 ? count : SDL_MAX_UINT32;
1796 }
1797 }
1798
1799 RIFFstart = SDL_TellIO(src);
1800 if (RIFFstart < 0) {
1801 return SDL_SetError("Could not seek in file");
1802 }
1803
1804 RIFFchunk.position = RIFFstart;
1805 if (WaveNextChunk(src, &RIFFchunk) < 0) {
1806 return SDL_SetError("Could not read RIFF header");
1807 }
1808
1809 // Check main WAVE file identifiers.
1810 if (RIFFchunk.fourcc == RIFF) {
1811 Uint32 formtype;
1812 // Read the form type. "WAVE" expected.
1813 if (!SDL_ReadU32LE(src, &formtype)) {
1814 return SDL_SetError("Could not read RIFF form type");
1815 } else if (formtype != WAVE) {
1816 return SDL_SetError("RIFF form type is not WAVE (not a Waveform file)");
1817 }
1818 } else if (RIFFchunk.fourcc == WAVE) {
1819 // RIFF chunk missing or skipped. Length unknown.
1820 RIFFchunk.position = 0;
1821 RIFFchunk.length = 0;
1822 } else {
1823 return SDL_SetError("Could not find RIFF or WAVE identifiers (not a Waveform file)");
1824 }
1825
1826 // The 4-byte form type is immediately followed by the first chunk.
1827 chunk->position = RIFFchunk.position + 4;
1828
1829 /* Use the RIFF chunk size to limit the search for the chunks. This is not
1830 * always reliable and the hint can be used to tune the behavior. By
1831 * default, it will never search past 4 GiB.
1832 */
1833 switch (file->riffhint) {
1834 case RiffSizeIgnore:
1835 RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
1836 break;
1837 default:
1838 case RiffSizeIgnoreZero:
1839 if (RIFFchunk.length == 0) {
1840 RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
1841 break;
1842 }
1843 SDL_FALLTHROUGH;
1844 case RiffSizeForce:
1845 RIFFend = RIFFchunk.position + RIFFchunk.length;
1846 RIFFlengthknown = true;
1847 break;
1848 case RiffSizeMaximum:
1849 RIFFend = SDL_MAX_SINT64;
1850 break;
1851 }
1852
1853 /* Step through all chunks and save information on the fmt, data, and fact
1854 * chunks. Ignore the chunks we don't know as per specification. This
1855 * currently also ignores cue, list, and slnt chunks.
1856 */
1857 while ((Uint64)RIFFend > (Uint64)chunk->position + chunk->length + (chunk->length & 1)) {
1858 // Abort after too many chunks or else corrupt files may waste time.
1859 if (chunkcount++ >= chunkcountlimit) {
1860 return SDL_SetError("Chunk count in WAVE file exceeds limit of %" SDL_PRIu32, chunkcountlimit);
1861 }
1862
1863 result = WaveNextChunk(src, chunk);
1864 if (result < 0) {
1865 // Unexpected EOF. Corrupt file or I/O issues.
1866 if (file->trunchint == TruncVeryStrict) {
1867 return SDL_SetError("Unexpected end of WAVE file");
1868 }
1869 // Let the checks after this loop sort this issue out.
1870 break;
1871 } else if (result == -2) {
1872 return SDL_SetError("Could not seek to WAVE chunk header");
1873 }
1874
1875 if (chunk->fourcc == FMT) {
1876 if (fmtchunk.fourcc == FMT) {
1877 // Multiple fmt chunks. Ignore or error?
1878 } else {
1879 // The fmt chunk must occur before the data chunk.
1880 if (datachunk.fourcc == DATA) {
1881 return SDL_SetError("fmt chunk after data chunk in WAVE file");
1882 }
1883 fmtchunk = *chunk;
1884 }
1885 } else if (chunk->fourcc == DATA) {
1886 /* Only use the first data chunk. Handling the wavl list madness
1887 * may require a different approach.
1888 */
1889 if (datachunk.fourcc != DATA) {
1890 datachunk = *chunk;
1891 }
1892 } else if (chunk->fourcc == FACT) {
1893 /* The fact chunk data must be at least 4 bytes for the
1894 * dwSampleLength field. Ignore all fact chunks after the first one.
1895 */
1896 if (file->fact.status == 0) {
1897 if (chunk->length < 4) {
1898 file->fact.status = -1;
1899 } else {
1900 // Let's use src directly, it's just too convenient.
1901 Sint64 position = SDL_SeekIO(src, chunk->position, SDL_IO_SEEK_SET);
1902 if (position == chunk->position && SDL_ReadU32LE(src, &file->fact.samplelength)) {
1903 file->fact.status = 1;
1904 } else {
1905 file->fact.status = -1;
1906 }
1907 }
1908 }
1909 }
1910
1911 /* Go through all chunks in verystrict mode or stop the search early if
1912 * all required chunks were found.
1913 */
1914 if (file->trunchint == TruncVeryStrict) {
1915 if ((Uint64)RIFFend < (Uint64)chunk->position + chunk->length) {
1916 return SDL_SetError("RIFF size truncates chunk");
1917 }
1918 } else if (fmtchunk.fourcc == FMT && datachunk.fourcc == DATA) {
1919 if (file->fact.status == 1 || file->facthint == FactIgnore || file->facthint == FactNoHint) {
1920 break;
1921 }
1922 }
1923 }
1924
1925 /* Save the position after the last chunk. This position will be used if the
1926 * RIFF length is unknown.
1927 */
1928 lastchunkpos = chunk->position + chunk->length;
1929
1930 // The fmt chunk is mandatory.
1931 if (fmtchunk.fourcc != FMT) {
1932 return SDL_SetError("Missing fmt chunk in WAVE file");
1933 }
1934 // A data chunk must be present.
1935 if (datachunk.fourcc != DATA) {
1936 return SDL_SetError("Missing data chunk in WAVE file");
1937 }
1938 // Check if the last chunk has all of its data in verystrict mode.
1939 if (file->trunchint == TruncVeryStrict) {
1940 // data chunk is handled later.
1941 if (chunk->fourcc != DATA && chunk->length > 0) {
1942 Uint8 tmp;
1943 Uint64 position = (Uint64)chunk->position + chunk->length - 1;
1944 if (position > SDL_MAX_SINT64 || SDL_SeekIO(src, (Sint64)position, SDL_IO_SEEK_SET) != (Sint64)position) {
1945 return SDL_SetError("Could not seek to WAVE chunk data");
1946 } else if (!SDL_ReadU8(src, &tmp)) {
1947 return SDL_SetError("RIFF size truncates chunk");
1948 }
1949 }
1950 }
1951
1952 // Process fmt chunk.
1953 *chunk = fmtchunk;
1954
1955 /* No need to read more than 1046 bytes of the fmt chunk data with the
1956 * formats that are currently supported. (1046 because of MS ADPCM coefficients)
1957 */
1958 if (WaveReadPartialChunkData(src, chunk, 1046) < 0) {
1959 return SDL_SetError("Could not read data of WAVE fmt chunk");
1960 }
1961
1962 /* The fmt chunk data must be at least 14 bytes to include all common fields.
1963 * It usually is 16 and larger depending on the header and encoding.
1964 */
1965 if (chunk->length < 14) {
1966 return SDL_SetError("Invalid WAVE fmt chunk length (too small)");
1967 } else if (chunk->size < 14) {
1968 return SDL_SetError("Could not read data of WAVE fmt chunk");
1969 } else if (!WaveReadFormat(file)) {
1970 return false;
1971 } else if (!WaveCheckFormat(file, (size_t)datachunk.length)) {
1972 return false;
1973 }
1974
1975#ifdef SDL_WAVE_DEBUG_LOG_FORMAT
1976 WaveDebugLogFormat(file);
1977#endif
1978#ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
1979 WaveDebugDumpFormat(file, RIFFchunk.length, fmtchunk.length, datachunk.length);
1980#endif
1981
1982 WaveFreeChunkData(chunk);
1983
1984 // Process data chunk.
1985 *chunk = datachunk;
1986
1987 if (chunk->length > 0) {
1988 result = WaveReadChunkData(src, chunk);
1989 if (result < 0) {
1990 return false;
1991 } else if (result == -2) {
1992 return SDL_SetError("Could not seek data of WAVE data chunk");
1993 }
1994 }
1995
1996 if (chunk->length != chunk->size) {
1997 // I/O issues or corrupt file.
1998 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
1999 return SDL_SetError("Could not read data of WAVE data chunk");
2000 }
2001 // The decoders handle this truncation.
2002 }
2003
2004 // Decode or convert the data if necessary.
2005 switch (format->encoding) {
2006 case PCM_CODE:
2007 case IEEE_FLOAT_CODE:
2008 if (!PCM_Decode(file, audio_buf, audio_len)) {
2009 return false;
2010 }
2011 break;
2012 case ALAW_CODE:
2013 case MULAW_CODE:
2014 if (!LAW_Decode(file, audio_buf, audio_len)) {
2015 return false;
2016 }
2017 break;
2018 case MS_ADPCM_CODE:
2019 if (!MS_ADPCM_Decode(file, audio_buf, audio_len)) {
2020 return false;
2021 }
2022 break;
2023 case IMA_ADPCM_CODE:
2024 if (!IMA_ADPCM_Decode(file, audio_buf, audio_len)) {
2025 return false;
2026 }
2027 break;
2028 }
2029
2030 /* Setting up the specs. All unsupported formats were filtered out
2031 * by checks earlier in this function.
2032 */
2033 spec->freq = format->frequency;
2034 spec->channels = (Uint8)format->channels;
2035 spec->format = SDL_AUDIO_UNKNOWN;
2036
2037 switch (format->encoding) {
2038 case MS_ADPCM_CODE:
2039 case IMA_ADPCM_CODE:
2040 case ALAW_CODE:
2041 case MULAW_CODE:
2042 // These can be easily stored in the byte order of the system.
2043 spec->format = SDL_AUDIO_S16;
2044 break;
2045 case IEEE_FLOAT_CODE:
2046 spec->format = SDL_AUDIO_F32LE;
2047 break;
2048 case PCM_CODE:
2049 switch (format->bitspersample) {
2050 case 8:
2051 spec->format = SDL_AUDIO_U8;
2052 break;
2053 case 16:
2054 spec->format = SDL_AUDIO_S16LE;
2055 break;
2056 case 24: // Has been shifted to 32 bits.
2057 case 32:
2058 spec->format = SDL_AUDIO_S32LE;
2059 break;
2060 default:
2061 // Just in case something unexpected happened in the checks.
2062 return SDL_SetError("Unexpected %u-bit PCM data format", (unsigned int)format->bitspersample);
2063 }
2064 break;
2065 default:
2066 return SDL_SetError("Unexpected data format");
2067 }
2068
2069 // Report the end position back to the cleanup code.
2070 if (RIFFlengthknown) {
2071 chunk->position = RIFFend;
2072 } else {
2073 chunk->position = lastchunkpos;
2074 }
2075
2076 return true;
2077}
2078
2079bool SDL_LoadWAV_IO(SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
2080{
2081 bool result = false;
2082 WaveFile file;
2083
2084 if (spec) {
2085 SDL_zerop(spec);
2086 }
2087 if (audio_buf) {
2088 *audio_buf = NULL;
2089 }
2090 if (audio_len) {
2091 *audio_len = 0;
2092 }
2093
2094 // Make sure we are passed a valid data source
2095 if (!src) {
2096 SDL_InvalidParamError("src");
2097 goto done;
2098 } else if (!spec) {
2099 SDL_InvalidParamError("spec");
2100 goto done;
2101 } else if (!audio_buf) {
2102 SDL_InvalidParamError("audio_buf");
2103 goto done;
2104 } else if (!audio_len) {
2105 SDL_InvalidParamError("audio_len");
2106 goto done;
2107 }
2108
2109 SDL_zero(file);
2110 file.riffhint = WaveGetRiffSizeHint();
2111 file.trunchint = WaveGetTruncationHint();
2112 file.facthint = WaveGetFactChunkHint();
2113
2114 result = WaveLoad(src, &file, spec, audio_buf, audio_len);
2115 if (!result) {
2116 SDL_free(*audio_buf);
2117 audio_buf = NULL;
2118 audio_len = 0;
2119 }
2120
2121 // Cleanup
2122 if (!closeio) {
2123 SDL_SeekIO(src, file.chunk.position, SDL_IO_SEEK_SET);
2124 }
2125 WaveFreeChunkData(&file.chunk);
2126 SDL_free(file.decoderdata);
2127done:
2128 if (closeio && src) {
2129 SDL_CloseIO(src);
2130 }
2131 return result;
2132}
2133
2134bool SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
2135{
2136 SDL_IOStream *stream = SDL_IOFromFile(path, "rb");
2137 if (!stream) {
2138 if (spec) {
2139 SDL_zerop(spec);
2140 }
2141 if (audio_buf) {
2142 *audio_buf = NULL;
2143 }
2144 if (audio_len) {
2145 *audio_len = 0;
2146 }
2147 return false;
2148 }
2149 return SDL_LoadWAV_IO(stream, true, spec, audio_buf, audio_len);
2150}
2151
2152