2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../SDL_internal.h"
27 /* Make a lucky guess. */
28 #define INT_MAX SDL_MAX_SINT32
31 #define SIZE_MAX ((size_t)-1)
34 /* Microsoft WAVE file loading routines */
36 #include "SDL_hints.h"
37 #include "SDL_audio.h"
39 #include "SDL_audio_c.h"
41 /* Reads the value stored at the location of the f1 pointer, multiplies it
42 * with the second argument and then stores the result to f1.
43 * Returns 0 on success, or -1 if the multiplication overflows, in which case f1
44 * does not get modified.
47 SafeMult(size_t *f1, size_t f2)
49 if (*f1 > 0 && SIZE_MAX / *f1 <= f2) {
56 typedef struct ADPCM_DecoderState
58 Uint32 channels; /* Number of channels. */
59 size_t blocksize; /* Size of an ADPCM block in bytes. */
60 size_t blockheadersize; /* Size of an ADPCM block header in bytes. */
61 size_t samplesperblock; /* Number of samples per channel in an ADPCM block. */
62 size_t framesize; /* Size of a sample frame (16-bit PCM) in bytes. */
63 Sint64 framestotal; /* Total number of sample frames. */
64 Sint64 framesleft; /* Number of sample frames still to be decoded. */
65 void *ddata; /* Decoder data from initialization. */
66 void *cstate; /* Decoding state for each channel. */
75 /* Current ADPCM block in the ADPCM data above. */
82 /* Decoded 16-bit PCM data. */
90 typedef struct MS_ADPCM_CoeffData
94 Sint16 aligndummy; /* Has to be last member. */
97 typedef struct MS_ADPCM_ChannelState
102 } MS_ADPCM_ChannelState;
104 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
106 WaveDebugLogFormat(WaveFile *file)
108 WaveFormat *format = &file->format;
109 const char *fmtstr = "WAVE file: %s, %u Hz, %s, %u bits, %u %s/s";
110 const char *waveformat, *wavechannel, *wavebpsunit = "B";
111 Uint32 wavebps = format->byterate;
114 SDL_zeroa(channelstr);
116 switch (format->encoding) {
120 case IEEE_FLOAT_CODE:
121 waveformat = "IEEE Float";
124 waveformat = "A-law";
127 waveformat = "\xc2\xb5-law";
130 waveformat = "MS ADPCM";
133 waveformat = "IMA ADPCM";
136 waveformat = "Unknown";
140 #define SDL_WAVE_DEBUG_CHANNELCFG(STR, CODE) case CODE: wavechannel = STR; break;
141 #define SDL_WAVE_DEBUG_CHANNELSTR(STR, CODE) if (format->channelmask & CODE) { \
142 SDL_strlcat(channelstr, channelstr[0] ? "-" STR : STR, sizeof(channelstr));}
144 if (format->formattag == EXTENSIBLE_CODE && format->channelmask > 0) {
145 switch (format->channelmask) {
146 SDL_WAVE_DEBUG_CHANNELCFG("1.0 Mono", 0x4)
147 SDL_WAVE_DEBUG_CHANNELCFG("1.1 Mono", 0xc)
148 SDL_WAVE_DEBUG_CHANNELCFG("2.0 Stereo", 0x3)
149 SDL_WAVE_DEBUG_CHANNELCFG("2.1 Stereo", 0xb)
150 SDL_WAVE_DEBUG_CHANNELCFG("3.0 Stereo", 0x7)
151 SDL_WAVE_DEBUG_CHANNELCFG("3.1 Stereo", 0xf)
152 SDL_WAVE_DEBUG_CHANNELCFG("3.0 Surround", 0x103)
153 SDL_WAVE_DEBUG_CHANNELCFG("3.1 Surround", 0x10b)
154 SDL_WAVE_DEBUG_CHANNELCFG("4.0 Quad", 0x33)
155 SDL_WAVE_DEBUG_CHANNELCFG("4.1 Quad", 0x3b)
156 SDL_WAVE_DEBUG_CHANNELCFG("4.0 Surround", 0x107)
157 SDL_WAVE_DEBUG_CHANNELCFG("4.1 Surround", 0x10f)
158 SDL_WAVE_DEBUG_CHANNELCFG("5.0", 0x37)
159 SDL_WAVE_DEBUG_CHANNELCFG("5.1", 0x3f)
160 SDL_WAVE_DEBUG_CHANNELCFG("5.0 Side", 0x607)
161 SDL_WAVE_DEBUG_CHANNELCFG("5.1 Side", 0x60f)
162 SDL_WAVE_DEBUG_CHANNELCFG("6.0", 0x137)
163 SDL_WAVE_DEBUG_CHANNELCFG("6.1", 0x13f)
164 SDL_WAVE_DEBUG_CHANNELCFG("6.0 Side", 0x707)
165 SDL_WAVE_DEBUG_CHANNELCFG("6.1 Side", 0x70f)
166 SDL_WAVE_DEBUG_CHANNELCFG("7.0", 0xf7)
167 SDL_WAVE_DEBUG_CHANNELCFG("7.1", 0xff)
168 SDL_WAVE_DEBUG_CHANNELCFG("7.0 Side", 0x6c7)
169 SDL_WAVE_DEBUG_CHANNELCFG("7.1 Side", 0x6cf)
170 SDL_WAVE_DEBUG_CHANNELCFG("7.0 Surround", 0x637)
171 SDL_WAVE_DEBUG_CHANNELCFG("7.1 Surround", 0x63f)
172 SDL_WAVE_DEBUG_CHANNELCFG("9.0 Surround", 0x5637)
173 SDL_WAVE_DEBUG_CHANNELCFG("9.1 Surround", 0x563f)
174 SDL_WAVE_DEBUG_CHANNELCFG("11.0 Surround", 0x56f7)
175 SDL_WAVE_DEBUG_CHANNELCFG("11.1 Surround", 0x56ff)
177 SDL_WAVE_DEBUG_CHANNELSTR("FL", 0x1)
178 SDL_WAVE_DEBUG_CHANNELSTR("FR", 0x2)
179 SDL_WAVE_DEBUG_CHANNELSTR("FC", 0x4)
180 SDL_WAVE_DEBUG_CHANNELSTR("LF", 0x8)
181 SDL_WAVE_DEBUG_CHANNELSTR("BL", 0x10)
182 SDL_WAVE_DEBUG_CHANNELSTR("BR", 0x20)
183 SDL_WAVE_DEBUG_CHANNELSTR("FLC", 0x40)
184 SDL_WAVE_DEBUG_CHANNELSTR("FRC", 0x80)
185 SDL_WAVE_DEBUG_CHANNELSTR("BC", 0x100)
186 SDL_WAVE_DEBUG_CHANNELSTR("SL", 0x200)
187 SDL_WAVE_DEBUG_CHANNELSTR("SR", 0x400)
188 SDL_WAVE_DEBUG_CHANNELSTR("TC", 0x800)
189 SDL_WAVE_DEBUG_CHANNELSTR("TFL", 0x1000)
190 SDL_WAVE_DEBUG_CHANNELSTR("TFC", 0x2000)
191 SDL_WAVE_DEBUG_CHANNELSTR("TFR", 0x4000)
192 SDL_WAVE_DEBUG_CHANNELSTR("TBL", 0x8000)
193 SDL_WAVE_DEBUG_CHANNELSTR("TBC", 0x10000)
194 SDL_WAVE_DEBUG_CHANNELSTR("TBR", 0x20000)
198 switch (format->channels) {
200 if (SDL_snprintf(channelstr, sizeof(channelstr), "%u channels", format->channels) >= 0) {
201 wavechannel = channelstr;
205 wavechannel = "Unknown";
208 wavechannel = "Mono";
211 wavechannel = "Setero";
216 #undef SDL_WAVE_DEBUG_CHANNELCFG
217 #undef SDL_WAVE_DEBUG_CHANNELSTR
219 if (wavebps >= 1024) {
221 wavebps = wavebps / 1024 + (wavebps & 0x3ff ? 1 : 0);
224 SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, fmtstr, waveformat, format->frequency, wavechannel, format->bitspersample, wavebps, wavebpsunit);
228 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
230 WaveDebugDumpFormat(WaveFile *file, Uint32 rifflen, Uint32 fmtlen, Uint32 datalen)
232 WaveFormat *format = &file->format;
233 const char *fmtstr1 = "WAVE chunk dump:\n"
234 "-------------------------------------------\n"
236 "-------------------------------------------\n"
238 " wFormatTag 0x%04x\n"
240 " nSamplesPerSec %11u\n"
241 " nAvgBytesPerSec %11u\n"
242 " nBlockAlign %11u\n";
243 const char *fmtstr2 = " wBitsPerSample %11u\n";
244 const char *fmtstr3 = " cbSize %11u\n";
245 const char *fmtstr4a = " wValidBitsPerSample %11u\n";
246 const char *fmtstr4b = " wSamplesPerBlock %11u\n";
247 const char *fmtstr5 = " dwChannelMask 0x%08x\n"
249 " %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n";
250 const char *fmtstr6 = "-------------------------------------------\n"
252 " dwSampleLength %11u\n";
253 const char *fmtstr7 = "-------------------------------------------\n"
255 "-------------------------------------------\n";
258 const size_t bufsize = 1024;
261 dumpstr = SDL_malloc(bufsize);
262 if (dumpstr == NULL) {
267 res = SDL_snprintf(dumpstr, bufsize, fmtstr1, rifflen, fmtlen, format->formattag, format->channels, format->frequency, format->byterate, format->blockalign);
268 dumppos += res > 0 ? res : 0;
270 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr2, format->bitspersample);
271 dumppos += res > 0 ? res : 0;
274 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr3, format->extsize);
275 dumppos += res > 0 ? res : 0;
277 if (format->formattag == EXTENSIBLE_CODE && fmtlen >= 40 && format->extsize >= 22) {
278 const Uint8 *g = format->subformat;
279 const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
280 const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
281 const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
283 switch (format->encoding) {
285 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4a, format->validsamplebits);
286 dumppos += res > 0 ? res : 0;
290 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
291 dumppos += res > 0 ? res : 0;
294 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]);
295 dumppos += res > 0 ? res : 0;
297 switch (format->encoding) {
300 if (fmtlen >= 20 && format->extsize >= 2) {
301 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
302 dumppos += res > 0 ? res : 0;
307 if (file->fact.status >= 1) {
308 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr6, file->fact.samplelength);
309 dumppos += res > 0 ? res : 0;
311 res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr7, datalen);
312 dumppos += res > 0 ? res : 0;
314 SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", dumpstr);
321 WaveAdjustToFactValue(WaveFile *file, Sint64 sampleframes)
323 if (file->fact.status == 2) {
324 if (file->facthint == FactStrict && sampleframes < file->fact.samplelength) {
325 return SDL_SetError("Invalid number of sample frames in WAVE fact chunk (too many)");
326 } else if (sampleframes > file->fact.samplelength) {
327 return file->fact.samplelength;
335 MS_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
337 WaveFormat *format = &file->format;
338 const size_t blockheadersize = (size_t)file->format.channels * 7;
339 const size_t availableblocks = datalength / file->format.blockalign;
340 const size_t blockframebitsize = (size_t)file->format.bitspersample * file->format.channels;
341 const size_t trailingdata = datalength % file->format.blockalign;
343 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
344 /* The size of the data chunk must be a multiple of the block size. */
345 if (datalength < blockheadersize || trailingdata > 0) {
346 return SDL_SetError("Truncated MS ADPCM block");
350 /* Calculate number of sample frames that will be decoded. */
351 file->sampleframes = (Sint64)availableblocks * format->samplesperblock;
352 if (trailingdata > 0) {
353 /* The last block is truncated. Check if we can get any samples out of it. */
354 if (file->trunchint == TruncDropFrame) {
355 /* Drop incomplete sample frame. */
356 if (trailingdata >= blockheadersize) {
357 size_t trailingsamples = 2 + (trailingdata - blockheadersize) * 8 / blockframebitsize;
358 if (trailingsamples > format->samplesperblock) {
359 trailingsamples = format->samplesperblock;
361 file->sampleframes += trailingsamples;
366 file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
367 if (file->sampleframes < 0) {
375 MS_ADPCM_Init(WaveFile *file, size_t datalength)
377 WaveFormat *format = &file->format;
378 WaveChunk *chunk = &file->chunk;
379 const size_t blockheadersize = (size_t)format->channels * 7;
380 const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
381 const size_t blockframebitsize = (size_t)format->bitspersample * format->channels;
382 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
383 const Sint16 presetcoeffs[14] = {256, 0, 512, -256, 0, 0, 192, 64, 240, 0, 460, -208, 392, -232};
384 size_t i, coeffcount;
385 MS_ADPCM_CoeffData *coeffdata;
389 /* While it's clear how IMA ADPCM handles more than two channels, the nibble
390 * order of MS ADPCM makes it awkward. The Standards Update does not talk
391 * about supporting more than stereo anyway.
393 if (format->channels > 2) {
394 return SDL_SetError("Invalid number of channels");
397 if (format->bitspersample != 4) {
398 return SDL_SetError("Invalid MS ADPCM bits per sample of %u", (unsigned int)format->bitspersample);
401 /* The block size must be big enough to contain the block header. */
402 if (format->blockalign < blockheadersize) {
403 return SDL_SetError("Invalid MS ADPCM block size (nBlockAlign)");
406 if (format->formattag == EXTENSIBLE_CODE) {
407 /* Does have a GUID (like all format tags), but there's no specification
408 * for how the data is packed into the extensible header. Making
409 * assumptions here could lead to new formats nobody wants to support.
411 return SDL_SetError("MS ADPCM with the extensible header is not supported");
414 /* There are wSamplesPerBlock, wNumCoef, and at least 7 coefficient pairs in
415 * the extended part of the header.
417 if (chunk->size < 22) {
418 return SDL_SetError("Could not read MS ADPCM format header");
421 format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
422 /* Number of coefficient pairs. A pair has two 16-bit integers. */
423 coeffcount = chunk->data[20] | ((size_t)chunk->data[21] << 8);
424 /* bPredictor, the integer offset into the coefficients array, is only
425 * 8 bits. It can only address the first 256 coefficients. Let's limit
426 * the count number here.
428 if (coeffcount > 256) {
432 if (chunk->size < 22 + coeffcount * 4) {
433 return SDL_SetError("Could not read custom coefficients in MS ADPCM format header");
434 } else if (format->extsize < 4 + coeffcount * 4) {
435 return SDL_SetError("Invalid MS ADPCM format header (too small)");
436 } else if (coeffcount < 7) {
437 return SDL_SetError("Missing required coefficients in MS ADPCM format header");
440 coeffdata = (MS_ADPCM_CoeffData *)SDL_malloc(sizeof(MS_ADPCM_CoeffData) + coeffcount * 4);
441 file->decoderdata = coeffdata; /* Freed in cleanup. */
442 if (coeffdata == NULL) {
443 return SDL_OutOfMemory();
445 coeffdata->coeff = &coeffdata->aligndummy;
446 coeffdata->coeffcount = (Uint16)coeffcount;
448 /* Copy the 16-bit pairs. */
449 for (i = 0; i < coeffcount * 2; i++) {
450 Sint32 c = chunk->data[22 + i * 2] | ((Sint32)chunk->data[23 + i * 2] << 8);
454 if (i < 14 && c != presetcoeffs[i]) {
455 return SDL_SetError("Wrong preset coefficients in MS ADPCM format header");
457 coeffdata->coeff[i] = (Sint16)c;
460 /* Technically, wSamplesPerBlock is required, but we have all the
461 * information in the other fields to calculate it, if it's zero.
463 if (format->samplesperblock == 0) {
464 /* Let's be nice to the encoders that didn't know how to fill this.
465 * The Standards Update calculates it this way:
467 * x = Block size (in bits) minus header size (in bits)
468 * y = Bit depth multiplied by channel count
469 * z = Number of samples per channel in block header
470 * wSamplesPerBlock = x / y + z
472 format->samplesperblock = (Uint32)blockdatasamples + 2;
475 /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
476 * the number of samples doesn't fit into the block. The Standards Update
477 * also describes wSamplesPerBlock with a formula that makes it necessary to
478 * always fill the block with the maximum amount of samples, but this is not
479 * enforced here as there are no compatibility issues.
480 * A truncated block header with just one sample is not supported.
482 if (format->samplesperblock == 1 || blockdatasamples < format->samplesperblock - 2) {
483 return SDL_SetError("Invalid number of samples per MS ADPCM block (wSamplesPerBlock)");
486 if (MS_ADPCM_CalculateSampleFrames(file, datalength) < 0) {
494 MS_ADPCM_ProcessNibble(MS_ADPCM_ChannelState *cstate, Sint32 sample1, Sint32 sample2, Uint8 nybble)
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
505 Uint32 delta = cstate->delta;
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;
516 delta = (delta * adaptive[nybble]) / 256;
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.
523 delta = max_deltaval;
526 cstate->delta = (Uint16)delta;
527 return (Sint16)new_sample;
531 MS_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
534 const Uint32 channels = state->channels;
537 MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
538 MS_ADPCM_CoeffData *ddata = (MS_ADPCM_CoeffData *)state->ddata;
540 for (c = 0; c < channels; c++) {
543 /* Load the coefficient pair into the channel state. */
544 coeffindex = state->block.data[o];
545 if (coeffindex > ddata->coeffcount) {
546 return SDL_SetError("Invalid MS ADPCM coefficient index in block header");
548 cstate[c].coeff1 = ddata->coeff[coeffindex * 2];
549 cstate[c].coeff2 = ddata->coeff[coeffindex * 2 + 1];
551 /* Initial delta value. */
552 o = channels + c * 2;
553 cstate[c].delta = state->block.data[o] | ((Uint16)state->block.data[o + 1] << 8);
555 /* Load the samples from the header. Interestingly, the sample later in
556 * the output stream comes first.
558 o = channels * 3 + c * 2;
559 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
560 if (sample >= 0x8000) {
563 state->output.data[state->output.pos + channels] = (Sint16)sample;
565 o = channels * 5 + c * 2;
566 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
567 if (sample >= 0x8000) {
570 state->output.data[state->output.pos] = (Sint16)sample;
575 state->block.pos += state->blockheadersize;
577 /* Skip second sample frame that came from the header. */
578 state->output.pos += state->channels;
580 /* Header provided two sample frames. */
581 state->framesleft -= 2;
586 /* Decodes the data of the MS ADPCM block. Decoding will stop if a block is too
587 * short, returning with none or partially decoded data. The partial data
588 * will always contain full sample frames (same sample count for each channel).
589 * Incomplete sample frames are discarded.
592 MS_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
595 Sint16 sample1, sample2;
596 const Uint32 channels = state->channels;
598 MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
600 size_t blockpos = state->block.pos;
601 size_t blocksize = state->block.size;
603 size_t outpos = state->output.pos;
605 Sint64 blockframesleft = state->samplesperblock - 2;
606 if (blockframesleft > state->framesleft) {
607 blockframesleft = state->framesleft;
610 while (blockframesleft > 0) {
611 for (c = 0; c < channels; c++) {
612 if (nybble & 0x4000) {
614 } else if (blockpos < blocksize) {
615 nybble = state->block.data[blockpos++] | 0x4000;
617 /* Out of input data. Drop the incomplete frame and return. */
618 state->output.pos = outpos - c;
622 /* Load previous samples which may come from the block header. */
623 sample1 = state->output.data[outpos - channels];
624 sample2 = state->output.data[outpos - channels * 2];
626 sample1 = MS_ADPCM_ProcessNibble(cstate + c, sample1, sample2, (nybble >> 4) & 0x0f);
627 state->output.data[outpos++] = sample1;
634 state->output.pos = outpos;
640 MS_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
643 size_t bytesleft, outputsize;
644 WaveChunk *chunk = &file->chunk;
645 ADPCM_DecoderState state;
646 MS_ADPCM_ChannelState cstate[2];
651 if (chunk->size != chunk->length) {
652 /* Could not read everything. Recalculate number of sample frames. */
653 if (MS_ADPCM_CalculateSampleFrames(file, chunk->size) < 0) {
658 /* Nothing to decode, nothing to return. */
659 if (file->sampleframes == 0) {
665 state.blocksize = file->format.blockalign;
666 state.channels = file->format.channels;
667 state.blockheadersize = (size_t)state.channels * 7;
668 state.samplesperblock = file->format.samplesperblock;
669 state.framesize = state.channels * sizeof(Sint16);
670 state.ddata = file->decoderdata;
671 state.framestotal = file->sampleframes;
672 state.framesleft = state.framestotal;
674 state.input.data = chunk->data;
675 state.input.size = chunk->size;
678 /* The output size in bytes. May get modified if data is truncated. */
679 outputsize = (size_t)state.framestotal;
680 if (SafeMult(&outputsize, state.framesize)) {
681 return SDL_OutOfMemory();
682 } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
683 return SDL_SetError("WAVE file too big");
686 state.output.pos = 0;
687 state.output.size = outputsize / sizeof(Sint16);
688 state.output.data = (Sint16 *)SDL_malloc(outputsize);
689 if (state.output.data == NULL) {
690 return SDL_OutOfMemory();
693 state.cstate = cstate;
695 /* Decode block by block. A truncated block will stop the decoding. */
696 bytesleft = state.input.size - state.input.pos;
697 while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
698 state.block.data = state.input.data + state.input.pos;
699 state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
702 if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
703 /* Somehow didn't allocate enough space for the output. */
704 SDL_free(state.output.data);
705 return SDL_SetError("Unexpected overflow in MS ADPCM decoder");
708 /* Initialize decoder with the values from the block header. */
709 result = MS_ADPCM_DecodeBlockHeader(&state);
711 SDL_free(state.output.data);
715 /* Decode the block data. It stores the samples directly in the output. */
716 result = MS_ADPCM_DecodeBlockData(&state);
718 /* Unexpected end. Stop decoding and return partial data if necessary. */
719 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
720 SDL_free(state.output.data);
721 return SDL_SetError("Truncated data chunk");
722 } else if (file->trunchint != TruncDropFrame) {
723 state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
725 outputsize = state.output.pos * sizeof(Sint16); /* Can't overflow, is always smaller. */
729 state.input.pos += state.block.size;
730 bytesleft = state.input.size - state.input.pos;
733 *audio_buf = (Uint8 *)state.output.data;
734 *audio_len = (Uint32)outputsize;
740 IMA_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
742 WaveFormat *format = &file->format;
743 const size_t blockheadersize = (size_t)format->channels * 4;
744 const size_t subblockframesize = (size_t)format->channels * 4;
745 const size_t availableblocks = datalength / format->blockalign;
746 const size_t trailingdata = datalength % format->blockalign;
748 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
749 /* The size of the data chunk must be a multiple of the block size. */
750 if (datalength < blockheadersize || trailingdata > 0) {
751 return SDL_SetError("Truncated IMA ADPCM block");
755 /* Calculate number of sample frames that will be decoded. */
756 file->sampleframes = (Uint64)availableblocks * format->samplesperblock;
757 if (trailingdata > 0) {
758 /* The last block is truncated. Check if we can get any samples out of it. */
759 if (file->trunchint == TruncDropFrame && trailingdata > blockheadersize - 2) {
760 /* The sample frame in the header of the truncated block is present.
761 * Drop incomplete sample frames.
763 size_t trailingsamples = 1;
765 if (trailingdata > blockheadersize) {
766 /* More data following after the header. */
767 const size_t trailingblockdata = trailingdata - blockheadersize;
768 const size_t trailingsubblockdata = trailingblockdata % subblockframesize;
769 trailingsamples += (trailingblockdata / subblockframesize) * 8;
770 /* Due to the interleaved sub-blocks, the last 4 bytes determine
771 * how many samples of the truncated sub-block are lost.
773 if (trailingsubblockdata > subblockframesize - 4) {
774 trailingsamples += (trailingsubblockdata % 4) * 2;
778 if (trailingsamples > format->samplesperblock) {
779 trailingsamples = format->samplesperblock;
781 file->sampleframes += trailingsamples;
785 file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
786 if (file->sampleframes < 0) {
794 IMA_ADPCM_Init(WaveFile *file, size_t datalength)
796 WaveFormat *format = &file->format;
797 WaveChunk *chunk = &file->chunk;
798 const size_t blockheadersize = (size_t)format->channels * 4;
799 const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
800 const size_t blockframebitsize = (size_t)format->bitspersample * format->channels;
801 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
805 /* IMA ADPCM can also have 3-bit samples, but it's not supported by SDL at this time. */
806 if (format->bitspersample == 3) {
807 return SDL_SetError("3-bit IMA ADPCM currently not supported");
808 } else if (format->bitspersample != 4) {
809 return SDL_SetError("Invalid IMA ADPCM bits per sample of %u", (unsigned int)format->bitspersample);
812 /* The block size is required to be a multiple of 4 and it must be able to
813 * hold a block header.
815 if (format->blockalign < blockheadersize || format->blockalign % 4) {
816 return SDL_SetError("Invalid IMA ADPCM block size (nBlockAlign)");
819 if (format->formattag == EXTENSIBLE_CODE) {
820 /* There's no specification for this, but it's basically the same
821 * format because the extensible header has wSampePerBlocks too.
824 /* The Standards Update says there 'should' be 2 bytes for wSamplesPerBlock. */
825 if (chunk->size >= 20 && format->extsize >= 2) {
826 format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
830 if (format->samplesperblock == 0) {
831 /* Field zero? No problem. We just assume the encoder packed the block.
832 * The specification calculates it this way:
834 * x = Block size (in bits) minus header size (in bits)
835 * y = Bit depth multiplied by channel count
836 * z = Number of samples per channel in header
837 * wSamplesPerBlock = x / y + z
839 format->samplesperblock = (Uint32)blockdatasamples + 1;
842 /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
843 * the number of samples doesn't fit into the block. The Standards Update
844 * also describes wSamplesPerBlock with a formula that makes it necessary
845 * to always fill the block with the maximum amount of samples, but this is
846 * not enforced here as there are no compatibility issues.
848 if (blockdatasamples < format->samplesperblock - 1) {
849 return SDL_SetError("Invalid number of samples per IMA ADPCM block (wSamplesPerBlock)");
852 if (IMA_ADPCM_CalculateSampleFrames(file, datalength) < 0) {
860 IMA_ADPCM_ProcessNibble(Sint8 *cindex, Sint16 lastsample, Uint8 nybble)
862 const Sint32 max_audioval = 32767;
863 const Sint32 min_audioval = -32768;
864 const Sint8 index_table_4b[16] = {
870 const Uint16 step_table[89] = {
871 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
872 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
873 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
874 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
875 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
876 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
877 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
878 22385, 24623, 27086, 29794, 32767
881 Sint32 sample, delta;
882 Sint8 index = *cindex;
884 /* Clamp index into valid range. */
887 } else if (index < 0) {
891 /* explicit cast to avoid gcc warning about using 'char' as array index */
892 step = step_table[(size_t)index];
894 /* Update index value */
895 *cindex = index + index_table_4b[nybble];
897 /* This calculation uses shifts and additions because multiplications were
898 * much slower back then. Sadly, this can't just be replaced with an actual
899 * multiplication now as the old algorithm drops some bits. The closest
900 * approximation I could find is something like this:
901 * (nybble & 0x8 ? -1 : 1) * ((nybble & 0x7) * step / 4 + step / 8)
913 sample = lastsample + delta;
915 /* Clamp output sample */
916 if (sample > max_audioval) {
917 sample = max_audioval;
918 } else if (sample < min_audioval) {
919 sample = min_audioval;
922 return (Sint16)sample;
926 IMA_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
930 Uint8 *cstate = (Uint8 *) state->cstate;
932 for (c = 0; c < state->channels; c++) {
933 size_t o = state->block.pos + c * 4;
935 /* Extract the sample from the header. */
936 Sint32 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
937 if (sample >= 0x8000) {
940 state->output.data[state->output.pos++] = (Sint16)sample;
942 /* Channel step index. */
943 step = (Sint16)state->block.data[o + 2];
944 cstate[c] = (Sint8)(step > 0x80 ? step - 0x100 : step);
946 /* Reserved byte in block header, should be 0. */
947 if (state->block.data[o + 3] != 0) {
948 /* Uh oh, corrupt data? Buggy code? */ ;
952 state->block.pos += state->blockheadersize;
954 /* Header provided one sample frame. */
960 /* Decodes the data of the IMA ADPCM block. Decoding will stop if a block is too
961 * short, returning with none or partially decoded data. The partial data always
962 * contains full sample frames (same sample count for each channel).
963 * Incomplete sample frames are discarded.
966 IMA_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
970 const Uint32 channels = state->channels;
971 const size_t subblockframesize = channels * 4;
972 Uint64 bytesrequired;
975 size_t blockpos = state->block.pos;
976 size_t blocksize = state->block.size;
977 size_t blockleft = blocksize - blockpos;
979 size_t outpos = state->output.pos;
981 Sint64 blockframesleft = state->samplesperblock - 1;
982 if (blockframesleft > state->framesleft) {
983 blockframesleft = state->framesleft;
986 bytesrequired = (blockframesleft + 7) / 8 * subblockframesize;
987 if (blockleft < bytesrequired) {
988 /* Data truncated. Calculate how many samples we can get out if it. */
989 const size_t guaranteedframes = blockleft / subblockframesize;
990 const size_t remainingbytes = blockleft % subblockframesize;
991 blockframesleft = guaranteedframes;
992 if (remainingbytes > subblockframesize - 4) {
993 blockframesleft += (remainingbytes % 4) * 2;
995 /* Signal the truncation. */
999 /* Each channel has their nibbles packed into 32-bit blocks. These blocks
1000 * are interleaved and make up the data part of the ADPCM block. This loop
1001 * decodes the samples as they come from the input data and puts them at
1002 * the appropriate places in the output data.
1004 while (blockframesleft > 0) {
1005 const size_t subblocksamples = blockframesleft < 8 ? (size_t)blockframesleft : 8;
1007 for (c = 0; c < channels; c++) {
1009 /* Load previous sample which may come from the block header. */
1010 Sint16 sample = state->output.data[outpos + c - channels];
1012 for (i = 0; i < subblocksamples; i++) {
1016 nybble = state->block.data[blockpos++];
1019 sample = IMA_ADPCM_ProcessNibble((Sint8 *)state->cstate + c, sample, nybble & 0x0f);
1020 state->output.data[outpos + c + i * channels] = sample;
1024 outpos += channels * subblocksamples;
1025 state->framesleft -= subblocksamples;
1026 blockframesleft -= subblocksamples;
1029 state->block.pos = blockpos;
1030 state->output.pos = outpos;
1036 IMA_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1039 size_t bytesleft, outputsize;
1040 WaveChunk *chunk = &file->chunk;
1041 ADPCM_DecoderState state;
1044 if (chunk->size != chunk->length) {
1045 /* Could not read everything. Recalculate number of sample frames. */
1046 if (IMA_ADPCM_CalculateSampleFrames(file, chunk->size) < 0) {
1051 /* Nothing to decode, nothing to return. */
1052 if (file->sampleframes == 0) {
1059 state.channels = file->format.channels;
1060 state.blocksize = file->format.blockalign;
1061 state.blockheadersize = (size_t)state.channels * 4;
1062 state.samplesperblock = file->format.samplesperblock;
1063 state.framesize = state.channels * sizeof(Sint16);
1064 state.framestotal = file->sampleframes;
1065 state.framesleft = state.framestotal;
1067 state.input.data = chunk->data;
1068 state.input.size = chunk->size;
1069 state.input.pos = 0;
1071 /* The output size in bytes. May get modified if data is truncated. */
1072 outputsize = (size_t)state.framestotal;
1073 if (SafeMult(&outputsize, state.framesize)) {
1074 return SDL_OutOfMemory();
1075 } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
1076 return SDL_SetError("WAVE file too big");
1079 state.output.pos = 0;
1080 state.output.size = outputsize / sizeof(Sint16);
1081 state.output.data = (Sint16 *)SDL_malloc(outputsize);
1082 if (state.output.data == NULL) {
1083 return SDL_OutOfMemory();
1086 cstate = (Sint8 *)SDL_calloc(state.channels, sizeof(Sint8));
1087 if (cstate == NULL) {
1088 SDL_free(state.output.data);
1089 return SDL_OutOfMemory();
1091 state.cstate = cstate;
1093 /* Decode block by block. A truncated block will stop the decoding. */
1094 bytesleft = state.input.size - state.input.pos;
1095 while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
1096 state.block.data = state.input.data + state.input.pos;
1097 state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
1098 state.block.pos = 0;
1100 if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
1101 /* Somehow didn't allocate enough space for the output. */
1102 SDL_free(state.output.data);
1104 return SDL_SetError("Unexpected overflow in IMA ADPCM decoder");
1107 /* Initialize decoder with the values from the block header. */
1108 result = IMA_ADPCM_DecodeBlockHeader(&state);
1110 /* Decode the block data. It stores the samples directly in the output. */
1111 result = IMA_ADPCM_DecodeBlockData(&state);
1115 /* Unexpected end. Stop decoding and return partial data if necessary. */
1116 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
1117 SDL_free(state.output.data);
1119 return SDL_SetError("Truncated data chunk");
1120 } else if (file->trunchint != TruncDropFrame) {
1121 state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
1123 outputsize = state.output.pos * sizeof(Sint16); /* Can't overflow, is always smaller. */
1127 state.input.pos += state.block.size;
1128 bytesleft = state.input.size - state.input.pos;
1131 *audio_buf = (Uint8 *)state.output.data;
1132 *audio_len = (Uint32)outputsize;
1140 LAW_Init(WaveFile *file, size_t datalength)
1142 WaveFormat *format = &file->format;
1144 /* Standards Update requires this to be 8. */
1145 if (format->bitspersample != 8) {
1146 return SDL_SetError("Invalid companded bits per sample of %u", (unsigned int)format->bitspersample);
1149 /* Not going to bother with weird padding. */
1150 if (format->blockalign != format->channels) {
1151 return SDL_SetError("Unsupported block alignment");
1154 if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
1155 if (format->blockalign > 1 && datalength % format->blockalign) {
1156 return SDL_SetError("Truncated data chunk in WAVE file");
1160 file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
1161 if (file->sampleframes < 0) {
1169 LAW_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1171 #ifdef SDL_WAVE_LAW_LUT
1172 const Sint16 alaw_lut[256] = {
1173 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
1174 -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
1175 -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
1176 -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344,
1177 -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88,
1178 -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
1179 -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
1180 -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504,
1181 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752,
1182 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016,
1183 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008,
1184 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344,
1185 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88,
1186 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376,
1187 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688,
1188 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848
1190 const Sint16 mulaw_lut[256] = {
1191 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996,
1192 -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
1193 -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
1194 -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
1195 -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876,
1196 -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372,
1197 -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120,
1198 -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124,
1199 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996,
1200 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932,
1201 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
1202 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884,
1203 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876,
1204 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,
1205 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120,
1206 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0
1210 WaveFormat *format = &file->format;
1211 WaveChunk *chunk = &file->chunk;
1212 size_t i, sample_count, expanded_len;
1216 if (chunk->length != chunk->size) {
1217 file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
1218 if (file->sampleframes < 0) {
1223 /* Nothing to decode, nothing to return. */
1224 if (file->sampleframes == 0) {
1230 sample_count = (size_t)file->sampleframes;
1231 if (SafeMult(&sample_count, format->channels)) {
1232 return SDL_OutOfMemory();
1235 expanded_len = sample_count;
1236 if (SafeMult(&expanded_len, sizeof(Sint16))) {
1237 return SDL_OutOfMemory();
1238 } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
1239 return SDL_SetError("WAVE file too big");
1242 /* 1 to avoid allocating zero bytes, to keep static analysis happy. */
1243 src = (Uint8 *)SDL_realloc(chunk->data, expanded_len ? expanded_len : 1);
1245 return SDL_OutOfMemory();
1250 dst = (Sint16 *)src;
1252 /* Work backwards, since we're expanding in-place. SDL_AudioSpec.format will
1253 * inform the caller about the byte order.
1256 switch (file->format.encoding) {
1257 #ifdef SDL_WAVE_LAW_LUT
1260 dst[i] = alaw_lut[src[i]];
1265 dst[i] = mulaw_lut[src[i]];
1271 Uint8 nibble = src[i];
1272 Uint8 exponent = (nibble & 0x7f) ^ 0x55;
1273 Sint16 mantissa = exponent & 0xf;
1279 mantissa = (mantissa << 4) | 0x8;
1281 mantissa <<= exponent - 1;
1284 dst[i] = nibble & 0x80 ? mantissa : -mantissa;
1289 Uint8 nibble = ~src[i];
1290 Sint16 mantissa = nibble & 0xf;
1291 Uint8 exponent = (nibble >> 4) & 0x7;
1292 Sint16 step = 4 << (exponent + 1);
1294 mantissa = (0x80 << exponent) + step * mantissa + step / 2 - 132;
1296 dst[i] = nibble & 0x80 ? -mantissa : mantissa;
1302 return SDL_SetError("Unknown companded encoding");
1306 *audio_len = (Uint32)expanded_len;
1312 PCM_Init(WaveFile *file, size_t datalength)
1314 WaveFormat *format = &file->format;
1316 if (format->encoding == PCM_CODE) {
1317 switch (format->bitspersample) {
1322 /* These are supported. */
1325 return SDL_SetError("%u-bit PCM format not supported", (unsigned int)format->bitspersample);
1327 } else if (format->encoding == IEEE_FLOAT_CODE) {
1328 if (format->bitspersample != 32) {
1329 return SDL_SetError("%u-bit IEEE floating-point format not supported", (unsigned int)format->bitspersample);
1333 /* It wouldn't be that hard to support more exotic block sizes, but
1334 * the most common formats should do for now.
1336 /* Make sure we're a multiple of the blockalign, at least. */
1337 if ((format->channels * format->bitspersample) % (format->blockalign * 8)) {
1338 return SDL_SetError("Unsupported block alignment");
1341 if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
1342 if (format->blockalign > 1 && datalength % format->blockalign) {
1343 return SDL_SetError("Truncated data chunk in WAVE file");
1347 file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
1348 if (file->sampleframes < 0) {
1356 PCM_ConvertSint24ToSint32(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1358 WaveFormat *format = &file->format;
1359 WaveChunk *chunk = &file->chunk;
1360 size_t i, expanded_len, sample_count;
1363 sample_count = (size_t)file->sampleframes;
1364 if (SafeMult(&sample_count, format->channels)) {
1365 return SDL_OutOfMemory();
1368 expanded_len = sample_count;
1369 if (SafeMult(&expanded_len, sizeof(Sint32))) {
1370 return SDL_OutOfMemory();
1371 } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
1372 return SDL_SetError("WAVE file too big");
1375 /* 1 to avoid allocating zero bytes, to keep static analysis happy. */
1376 ptr = (Uint8 *)SDL_realloc(chunk->data, expanded_len ? expanded_len : 1);
1378 return SDL_OutOfMemory();
1381 /* This pointer is now invalid. */
1386 *audio_len = (Uint32)expanded_len;
1388 /* work from end to start, since we're expanding in-place. */
1389 for (i = sample_count; i > 0; i--) {
1390 const size_t o = i - 1;
1395 b[2] = ptr[o * 3 + 1];
1396 b[3] = ptr[o * 3 + 2];
1398 ptr[o * 4 + 0] = b[0];
1399 ptr[o * 4 + 1] = b[1];
1400 ptr[o * 4 + 2] = b[2];
1401 ptr[o * 4 + 3] = b[3];
1408 PCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
1410 WaveFormat *format = &file->format;
1411 WaveChunk *chunk = &file->chunk;
1414 if (chunk->length != chunk->size) {
1415 file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
1416 if (file->sampleframes < 0) {
1421 /* Nothing to decode, nothing to return. */
1422 if (file->sampleframes == 0) {
1428 /* 24-bit samples get shifted to 32 bits. */
1429 if (format->encoding == PCM_CODE && format->bitspersample == 24) {
1430 return PCM_ConvertSint24ToSint32(file, audio_buf, audio_len);
1433 outputsize = (size_t)file->sampleframes;
1434 if (SafeMult(&outputsize, format->blockalign)) {
1435 return SDL_OutOfMemory();
1436 } else if (outputsize > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
1437 return SDL_SetError("WAVE file too big");
1440 *audio_buf = chunk->data;
1441 *audio_len = (Uint32)outputsize;
1443 /* This pointer is going to be returned to the caller. Prevent free in cleanup. */
1450 static WaveRiffSizeHint
1451 WaveGetRiffSizeHint()
1453 const char *hint = SDL_GetHint(SDL_HINT_WAVE_RIFF_CHUNK_SIZE);
1456 if (SDL_strcmp(hint, "force") == 0) {
1457 return RiffSizeForce;
1458 } else if (SDL_strcmp(hint, "ignore") == 0) {
1459 return RiffSizeIgnore;
1460 } else if (SDL_strcmp(hint, "ignorezero") == 0) {
1461 return RiffSizeIgnoreZero;
1462 } else if (SDL_strcmp(hint, "maximum") == 0) {
1463 return RiffSizeMaximum;
1467 return RiffSizeNoHint;
1470 static WaveTruncationHint
1471 WaveGetTruncationHint()
1473 const char *hint = SDL_GetHint(SDL_HINT_WAVE_TRUNCATION);
1476 if (SDL_strcmp(hint, "verystrict") == 0) {
1477 return TruncVeryStrict;
1478 } else if (SDL_strcmp(hint, "strict") == 0) {
1480 } else if (SDL_strcmp(hint, "dropframe") == 0) {
1481 return TruncDropFrame;
1482 } else if (SDL_strcmp(hint, "dropblock") == 0) {
1483 return TruncDropBlock;
1490 static WaveFactChunkHint
1491 WaveGetFactChunkHint()
1493 const char *hint = SDL_GetHint(SDL_HINT_WAVE_FACT_CHUNK);
1496 if (SDL_strcmp(hint, "truncate") == 0) {
1497 return FactTruncate;
1498 } else if (SDL_strcmp(hint, "strict") == 0) {
1500 } else if (SDL_strcmp(hint, "ignorezero") == 0) {
1501 return FactIgnoreZero;
1502 } else if (SDL_strcmp(hint, "ignore") == 0) {
1511 WaveFreeChunkData(WaveChunk *chunk)
1513 if (chunk->data != NULL) {
1514 SDL_free(chunk->data);
1521 WaveNextChunk(SDL_RWops *src, WaveChunk *chunk)
1523 Uint32 chunkheader[2];
1524 Sint64 nextposition = chunk->position + chunk->length;
1526 /* Data is no longer valid after this function returns. */
1527 WaveFreeChunkData(chunk);
1529 /* Error on overflows. */
1530 if (SDL_MAX_SINT64 - chunk->length < chunk->position || SDL_MAX_SINT64 - 8 < nextposition) {
1534 /* RIFF chunks have a 2-byte alignment. Skip padding byte. */
1535 if (chunk->length & 1) {
1539 if (SDL_RWseek(src, nextposition, RW_SEEK_SET) != nextposition) {
1540 /* Not sure how we ended up here. Just abort. */
1542 } else if (SDL_RWread(src, chunkheader, 4, 2) != 2) {
1546 chunk->fourcc = SDL_SwapLE32(chunkheader[0]);
1547 chunk->length = SDL_SwapLE32(chunkheader[1]);
1548 chunk->position = nextposition + 8;
1554 WaveReadPartialChunkData(SDL_RWops *src, WaveChunk *chunk, size_t length)
1556 WaveFreeChunkData(chunk);
1558 if (length > chunk->length) {
1559 length = chunk->length;
1563 chunk->data = (Uint8 *) SDL_malloc(length);
1564 if (chunk->data == NULL) {
1565 return SDL_OutOfMemory();
1568 if (SDL_RWseek(src, chunk->position, RW_SEEK_SET) != chunk->position) {
1569 /* Not sure how we ended up here. Just abort. */
1573 chunk->size = SDL_RWread(src, chunk->data, 1, length);
1574 if (chunk->size != length) {
1575 /* Expected to be handled by the caller. */
1583 WaveReadChunkData(SDL_RWops *src, WaveChunk *chunk)
1585 return WaveReadPartialChunkData(src, chunk, chunk->length);
1588 typedef struct WaveExtensibleGUID {
1591 } WaveExtensibleGUID;
1593 /* Some of the GUIDs that are used by WAVEFORMATEXTENSIBLE. */
1594 #define WAVE_FORMATTAG_GUID(tag) {(tag) & 0xff, (tag) >> 8, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113}
1595 static WaveExtensibleGUID extensible_guids[] = {
1596 {PCM_CODE, WAVE_FORMATTAG_GUID(PCM_CODE)},
1597 {MS_ADPCM_CODE, WAVE_FORMATTAG_GUID(MS_ADPCM_CODE)},
1598 {IEEE_FLOAT_CODE, WAVE_FORMATTAG_GUID(IEEE_FLOAT_CODE)},
1599 {ALAW_CODE, WAVE_FORMATTAG_GUID(ALAW_CODE)},
1600 {MULAW_CODE, WAVE_FORMATTAG_GUID(MULAW_CODE)},
1601 {IMA_ADPCM_CODE, WAVE_FORMATTAG_GUID(IMA_ADPCM_CODE)}
1605 WaveGetFormatGUIDEncoding(WaveFormat *format)
1608 for (i = 0; i < SDL_arraysize(extensible_guids); i++) {
1609 if (SDL_memcmp(format->subformat, extensible_guids[i].guid, 16) == 0) {
1610 return extensible_guids[i].encoding;
1613 return UNKNOWN_CODE;
1617 WaveReadFormat(WaveFile *file)
1619 WaveChunk *chunk = &file->chunk;
1620 WaveFormat *format = &file->format;
1622 size_t fmtlen = chunk->size;
1624 if (fmtlen > SDL_MAX_SINT32) {
1625 /* Limit given by SDL_RWFromConstMem. */
1626 return SDL_SetError("Data of WAVE fmt chunk too big");
1628 fmtsrc = SDL_RWFromConstMem(chunk->data, (int)chunk->size);
1629 if (fmtsrc == NULL) {
1630 return SDL_OutOfMemory();
1633 format->formattag = SDL_ReadLE16(fmtsrc);
1634 format->encoding = format->formattag;
1635 format->channels = SDL_ReadLE16(fmtsrc);
1636 format->frequency = SDL_ReadLE32(fmtsrc);
1637 format->byterate = SDL_ReadLE32(fmtsrc);
1638 format->blockalign = SDL_ReadLE16(fmtsrc);
1640 /* This is PCM specific in the first version of the specification. */
1642 format->bitspersample = SDL_ReadLE16(fmtsrc);
1643 } else if (format->encoding == PCM_CODE) {
1644 SDL_RWclose(fmtsrc);
1645 return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
1648 /* The earlier versions also don't have this field. */
1650 format->extsize = SDL_ReadLE16(fmtsrc);
1653 if (format->formattag == EXTENSIBLE_CODE) {
1654 /* note that this ignores channel masks, smaller valid bit counts
1655 * inside a larger container, and most subtypes. This is just enough
1656 * to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE
1657 * to be useful working when they use this format flag.
1660 /* Extensible header must be at least 22 bytes. */
1661 if (fmtlen < 40 || format->extsize < 22) {
1662 SDL_RWclose(fmtsrc);
1663 return SDL_SetError("Extensible WAVE header too small");
1666 format->validsamplebits = SDL_ReadLE16(fmtsrc);
1667 format->samplesperblock = format->validsamplebits;
1668 format->channelmask = SDL_ReadLE32(fmtsrc);
1669 SDL_RWread(fmtsrc, format->subformat, 1, 16);
1670 format->encoding = WaveGetFormatGUIDEncoding(format);
1673 SDL_RWclose(fmtsrc);
1679 WaveCheckFormat(WaveFile *file, size_t datalength)
1681 WaveFormat *format = &file->format;
1683 /* Check for some obvious issues. */
1685 if (format->channels == 0) {
1686 return SDL_SetError("Invalid number of channels");
1687 } else if (format->channels > 255) {
1688 /* Limit given by SDL_AudioSpec.channels. */
1689 return SDL_SetError("Number of channels exceeds limit of 255");
1692 if (format->frequency == 0) {
1693 return SDL_SetError("Invalid sample rate");
1694 } else if (format->frequency > INT_MAX) {
1695 /* Limit given by SDL_AudioSpec.freq. */
1696 return SDL_SetError("Sample rate exceeds limit of %d", INT_MAX);
1699 /* Reject invalid fact chunks in strict mode. */
1700 if (file->facthint == FactStrict && file->fact.status == -1) {
1701 return SDL_SetError("Invalid fact chunk in WAVE file");
1704 /* Check for issues common to all encodings. Some unsupported formats set
1705 * the bits per sample to zero. These fall through to the 'unsupported
1708 switch (format->encoding) {
1709 case IEEE_FLOAT_CODE:
1713 case IMA_ADPCM_CODE:
1714 /* These formats require a fact chunk. */
1715 if (file->facthint == FactStrict && file->fact.status <= 0) {
1716 return SDL_SetError("Missing fact chunk in WAVE file");
1720 /* All supported formats require a non-zero bit depth. */
1721 if (file->chunk.size < 16) {
1722 return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
1723 } else if (format->bitspersample == 0) {
1724 return SDL_SetError("Invalid bits per sample");
1727 /* All supported formats must have a proper block size. */
1728 if (format->blockalign == 0) {
1729 return SDL_SetError("Invalid block alignment");
1732 /* If the fact chunk is valid and the appropriate hint is set, the
1733 * decoders will use the number of sample frames from the fact chunk.
1735 if (file->fact.status == 1) {
1736 WaveFactChunkHint hint = file->facthint;
1737 Uint32 samples = file->fact.samplelength;
1738 if (hint == FactTruncate || hint == FactStrict || (hint == FactIgnoreZero && samples > 0)) {
1739 file->fact.status = 2;
1744 /* Check the format for encoding specific issues and initialize decoders. */
1745 switch (format->encoding) {
1747 case IEEE_FLOAT_CODE:
1748 if (PCM_Init(file, datalength) < 0) {
1754 if (LAW_Init(file, datalength) < 0) {
1759 if (MS_ADPCM_Init(file, datalength) < 0) {
1763 case IMA_ADPCM_CODE:
1764 if (IMA_ADPCM_Init(file, datalength) < 0) {
1769 case MPEGLAYER3_CODE:
1770 return SDL_SetError("MPEG formats not supported");
1772 if (format->formattag == EXTENSIBLE_CODE) {
1773 const char *errstr = "Unknown WAVE format GUID: %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x";
1774 const Uint8 *g = format->subformat;
1775 const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
1776 const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
1777 const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
1778 return SDL_SetError(errstr, g1, g2, g3, g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]);
1780 return SDL_SetError("Unknown WAVE format tag: 0x%04x", (unsigned int)format->encoding);
1787 WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
1790 Uint32 chunkcount = 0;
1791 Uint32 chunkcountlimit = 10000;
1792 char *envchunkcountlimit;
1793 Sint64 RIFFstart, RIFFend, lastchunkpos;
1794 SDL_bool RIFFlengthknown = SDL_FALSE;
1795 WaveFormat *format = &file->format;
1796 WaveChunk *chunk = &file->chunk;
1797 WaveChunk RIFFchunk;
1799 WaveChunk datachunk;
1801 SDL_zero(RIFFchunk);
1803 SDL_zero(datachunk);
1805 envchunkcountlimit = SDL_getenv("SDL_WAVE_CHUNK_LIMIT");
1806 if (envchunkcountlimit != NULL) {
1808 if (SDL_sscanf(envchunkcountlimit, "%u", &count) == 1) {
1809 chunkcountlimit = count <= SDL_MAX_UINT32 ? count : SDL_MAX_UINT32;
1813 RIFFstart = SDL_RWtell(src);
1814 if (RIFFstart < 0) {
1815 return SDL_SetError("Could not seek in file");
1818 RIFFchunk.position = RIFFstart;
1819 if (WaveNextChunk(src, &RIFFchunk) < 0) {
1820 return SDL_SetError("Could not read RIFF header");
1823 /* Check main WAVE file identifiers. */
1824 if (RIFFchunk.fourcc == RIFF) {
1826 /* Read the form type. "WAVE" expected. */
1827 if (SDL_RWread(src, &formtype, sizeof(Uint32), 1) != 1) {
1828 return SDL_SetError("Could not read RIFF form type");
1829 } else if (SDL_SwapLE32(formtype) != WAVE) {
1830 return SDL_SetError("RIFF form type is not WAVE (not a Waveform file)");
1832 } else if (RIFFchunk.fourcc == WAVE) {
1833 /* RIFF chunk missing or skipped. Length unknown. */
1834 RIFFchunk.position = 0;
1835 RIFFchunk.length = 0;
1837 return SDL_SetError("Could not find RIFF or WAVE identifiers (not a Waveform file)");
1840 /* The 4-byte form type is immediately followed by the first chunk.*/
1841 chunk->position = RIFFchunk.position + 4;
1843 /* Use the RIFF chunk size to limit the search for the chunks. This is not
1844 * always reliable and the hint can be used to tune the behavior. By
1845 * default, it will never search past 4 GiB.
1847 switch (file->riffhint) {
1848 case RiffSizeIgnore:
1849 RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
1852 case RiffSizeIgnoreZero:
1853 if (RIFFchunk.length == 0) {
1854 RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
1859 RIFFend = RIFFchunk.position + RIFFchunk.length;
1860 RIFFlengthknown = SDL_TRUE;
1862 case RiffSizeMaximum:
1863 RIFFend = SDL_MAX_SINT64;
1867 /* Step through all chunks and save information on the fmt, data, and fact
1868 * chunks. Ignore the chunks we don't know as per specification. This
1869 * currently also ignores cue, list, and slnt chunks.
1871 while ((Uint64)RIFFend > (Uint64)chunk->position + chunk->length + (chunk->length & 1)) {
1872 /* Abort after too many chunks or else corrupt files may waste time. */
1873 if (chunkcount++ >= chunkcountlimit) {
1874 return SDL_SetError("Chunk count in WAVE file exceeds limit of %u", chunkcountlimit);
1877 result = WaveNextChunk(src, chunk);
1879 /* Unexpected EOF. Corrupt file or I/O issues. */
1880 if (file->trunchint == TruncVeryStrict) {
1881 return SDL_SetError("Unexpected end of WAVE file");
1883 /* Let the checks after this loop sort this issue out. */
1885 } else if (result == -2) {
1886 return SDL_SetError("Could not seek to WAVE chunk header");
1889 if (chunk->fourcc == FMT) {
1890 if (fmtchunk.fourcc == FMT) {
1891 /* Multiple fmt chunks. Ignore or error? */
1893 /* The fmt chunk must occur before the data chunk. */
1894 if (datachunk.fourcc == DATA) {
1895 return SDL_SetError("fmt chunk after data chunk in WAVE file");
1899 } else if (chunk->fourcc == DATA) {
1900 /* Only use the first data chunk. Handling the wavl list madness
1901 * may require a different approach.
1903 if (datachunk.fourcc != DATA) {
1906 } else if (chunk->fourcc == FACT) {
1907 /* The fact chunk data must be at least 4 bytes for the
1908 * dwSampleLength field. Ignore all fact chunks after the first one.
1910 if (file->fact.status == 0) {
1911 if (chunk->length < 4) {
1912 file->fact.status = -1;
1914 /* Let's use src directly, it's just too convenient. */
1915 Sint64 position = SDL_RWseek(src, chunk->position, RW_SEEK_SET);
1916 Uint32 samplelength;
1917 if (position == chunk->position && SDL_RWread(src, &samplelength, sizeof(Uint32), 1) == 1) {
1918 file->fact.status = 1;
1919 file->fact.samplelength = SDL_SwapLE32(samplelength);
1921 file->fact.status = -1;
1927 /* Go through all chunks in verystrict mode or stop the search early if
1928 * all required chunks were found.
1930 if (file->trunchint == TruncVeryStrict) {
1931 if ((Uint64)RIFFend < (Uint64)chunk->position + chunk->length) {
1932 return SDL_SetError("RIFF size truncates chunk");
1934 } else if (fmtchunk.fourcc == FMT && datachunk.fourcc == DATA) {
1935 if (file->fact.status == 1 || file->facthint == FactIgnore || file->facthint == FactNoHint) {
1941 /* Save the position after the last chunk. This position will be used if the
1942 * RIFF length is unknown.
1944 lastchunkpos = chunk->position + chunk->length;
1946 /* The fmt chunk is mandatory. */
1947 if (fmtchunk.fourcc != FMT) {
1948 return SDL_SetError("Missing fmt chunk in WAVE file");
1950 /* A data chunk must be present. */
1951 if (datachunk.fourcc != DATA) {
1952 return SDL_SetError("Missing data chunk in WAVE file");
1954 /* Check if the last chunk has all of its data in verystrict mode. */
1955 if (file->trunchint == TruncVeryStrict) {
1956 /* data chunk is handled later. */
1957 if (chunk->fourcc != DATA && chunk->length > 0) {
1959 Uint64 position = (Uint64)chunk->position + chunk->length - 1;
1960 if (position > SDL_MAX_SINT64 || SDL_RWseek(src, (Sint64)position, RW_SEEK_SET) != (Sint64)position) {
1961 return SDL_SetError("Could not seek to WAVE chunk data");
1962 } else if (SDL_RWread(src, &tmp, 1, 1) != 1) {
1963 return SDL_SetError("RIFF size truncates chunk");
1968 /* Process fmt chunk. */
1971 /* No need to read more than 1046 bytes of the fmt chunk data with the
1972 * formats that are currently supported. (1046 because of MS ADPCM coefficients)
1974 if (WaveReadPartialChunkData(src, chunk, 1046) < 0) {
1975 return SDL_SetError("Could not read data of WAVE fmt chunk");
1978 /* The fmt chunk data must be at least 14 bytes to include all common fields.
1979 * It usually is 16 and larger depending on the header and encoding.
1981 if (chunk->length < 14) {
1982 return SDL_SetError("Invalid WAVE fmt chunk length (too small)");
1983 } else if (chunk->size < 14) {
1984 return SDL_SetError("Could not read data of WAVE fmt chunk");
1985 } else if (WaveReadFormat(file) < 0) {
1987 } else if (WaveCheckFormat(file, (size_t)datachunk.length) < 0) {
1991 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
1992 WaveDebugLogFormat(file);
1994 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
1995 WaveDebugDumpFormat(file, RIFFchunk.length, fmtchunk.length, datachunk.length);
1998 WaveFreeChunkData(chunk);
2000 /* Process data chunk. */
2003 if (chunk->length > 0) {
2004 result = WaveReadChunkData(src, chunk);
2007 } else if (result == -2) {
2008 return SDL_SetError("Could not seek data of WAVE data chunk");
2012 if (chunk->length != chunk->size) {
2013 /* I/O issues or corrupt file. */
2014 if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
2015 return SDL_SetError("Could not read data of WAVE data chunk");
2017 /* The decoders handle this truncation. */
2020 /* Decode or convert the data if necessary. */
2021 switch (format->encoding) {
2023 case IEEE_FLOAT_CODE:
2024 if (PCM_Decode(file, audio_buf, audio_len) < 0) {
2030 if (LAW_Decode(file, audio_buf, audio_len) < 0) {
2035 if (MS_ADPCM_Decode(file, audio_buf, audio_len) < 0) {
2039 case IMA_ADPCM_CODE:
2040 if (IMA_ADPCM_Decode(file, audio_buf, audio_len) < 0) {
2046 /* Setting up the SDL_AudioSpec. All unsupported formats were filtered out
2047 * by checks earlier in this function.
2050 spec->freq = format->frequency;
2051 spec->channels = (Uint8)format->channels;
2052 spec->samples = 4096; /* Good default buffer size */
2054 switch (format->encoding) {
2056 case IMA_ADPCM_CODE:
2059 /* These can be easily stored in the byte order of the system. */
2060 spec->format = AUDIO_S16SYS;
2062 case IEEE_FLOAT_CODE:
2063 spec->format = AUDIO_F32LSB;
2066 switch (format->bitspersample) {
2068 spec->format = AUDIO_U8;
2071 spec->format = AUDIO_S16LSB;
2073 case 24: /* Has been shifted to 32 bits. */
2075 spec->format = AUDIO_S32LSB;
2078 /* Just in case something unexpected happened in the checks. */
2079 return SDL_SetError("Unexpected %u-bit PCM data format", (unsigned int)format->bitspersample);
2084 spec->silence = SDL_SilenceValueForFormat(spec->format);
2086 /* Report the end position back to the cleanup code. */
2087 if (RIFFlengthknown) {
2088 chunk->position = RIFFend;
2090 chunk->position = lastchunkpos;
2097 SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
2104 /* Make sure we are passed a valid data source */
2106 /* Error may come from RWops. */
2108 } else if (spec == NULL) {
2109 SDL_InvalidParamError("spec");
2111 } else if (audio_buf == NULL) {
2112 SDL_InvalidParamError("audio_buf");
2114 } else if (audio_len == NULL) {
2115 SDL_InvalidParamError("audio_len");
2122 file.riffhint = WaveGetRiffSizeHint();
2123 file.trunchint = WaveGetTruncationHint();
2124 file.facthint = WaveGetFactChunkHint();
2126 result = WaveLoad(src, &file, spec, audio_buf, audio_len);
2128 SDL_free(*audio_buf);
2138 SDL_RWseek(src, file.chunk.position, RW_SEEK_SET);
2140 WaveFreeChunkData(&file.chunk);
2141 SDL_free(file.decoderdata);
2146 /* Since the WAV memory is allocated in the shared library, it must also
2147 be freed here. (Necessary under Win32, VC++)
2150 SDL_FreeWAV(Uint8 *audio_buf)
2152 SDL_free(audio_buf);
2155 /* vi: set ts=4 sw=4 expandtab: */