21 #include "../SDL_internal.h"
27 #define SIZE_MAX ((size_t)-1)
31 #define INT_MAX SDL_MAX_SINT32
51 if (*f1 > 0 &&
SIZE_MAX / *f1 <= f2) {
106 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
111 const char *fmtstr =
"WAVE file: %s, %u Hz, %s, %u bits, %u %s/s";
112 const char *waveformat, *wavechannel, *wavebpsunit =
"B";
118 switch (
format->encoding) {
123 waveformat =
"IEEE Float";
126 waveformat =
"A-law";
129 waveformat =
"\xc2\xb5-law";
132 waveformat =
"MS ADPCM";
135 waveformat =
"IMA ADPCM";
138 waveformat =
"Unknown";
142 #define SDL_WAVE_DEBUG_CHANNELCFG(STR, CODE) case CODE: wavechannel = STR; break;
143 #define SDL_WAVE_DEBUG_CHANNELSTR(STR, CODE) if (format->channelmask & CODE) { \
144 SDL_strlcat(channelstr, channelstr[0] ? "-" STR : STR, sizeof(channelstr));}
147 switch (
format->channelmask) {
148 SDL_WAVE_DEBUG_CHANNELCFG(
"1.0 Mono", 0x4)
149 SDL_WAVE_DEBUG_CHANNELCFG(
"1.1 Mono", 0xc)
150 SDL_WAVE_DEBUG_CHANNELCFG(
"2.0 Stereo", 0x3)
151 SDL_WAVE_DEBUG_CHANNELCFG(
"2.1 Stereo", 0xb)
152 SDL_WAVE_DEBUG_CHANNELCFG(
"3.0 Stereo", 0x7)
153 SDL_WAVE_DEBUG_CHANNELCFG(
"3.1 Stereo", 0xf)
154 SDL_WAVE_DEBUG_CHANNELCFG(
"3.0 Surround", 0x103)
155 SDL_WAVE_DEBUG_CHANNELCFG(
"3.1 Surround", 0x10b)
156 SDL_WAVE_DEBUG_CHANNELCFG(
"4.0 Quad", 0x33)
157 SDL_WAVE_DEBUG_CHANNELCFG(
"4.1 Quad", 0x3b)
158 SDL_WAVE_DEBUG_CHANNELCFG(
"4.0 Surround", 0x107)
159 SDL_WAVE_DEBUG_CHANNELCFG(
"4.1 Surround", 0x10f)
160 SDL_WAVE_DEBUG_CHANNELCFG(
"5.0", 0x37)
161 SDL_WAVE_DEBUG_CHANNELCFG(
"5.1", 0x3f)
162 SDL_WAVE_DEBUG_CHANNELCFG(
"5.0 Side", 0x607)
163 SDL_WAVE_DEBUG_CHANNELCFG(
"5.1 Side", 0x60f)
164 SDL_WAVE_DEBUG_CHANNELCFG(
"6.0", 0x137)
165 SDL_WAVE_DEBUG_CHANNELCFG(
"6.1", 0x13f)
166 SDL_WAVE_DEBUG_CHANNELCFG(
"6.0 Side", 0x707)
167 SDL_WAVE_DEBUG_CHANNELCFG(
"6.1 Side", 0x70f)
168 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0", 0xf7)
169 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1", 0xff)
170 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0 Side", 0x6c7)
171 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1 Side", 0x6cf)
172 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0 Surround", 0x637)
173 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1 Surround", 0x63f)
174 SDL_WAVE_DEBUG_CHANNELCFG(
"9.0 Surround", 0x5637)
175 SDL_WAVE_DEBUG_CHANNELCFG(
"9.1 Surround", 0x563f)
176 SDL_WAVE_DEBUG_CHANNELCFG(
"11.0 Surround", 0x56f7)
177 SDL_WAVE_DEBUG_CHANNELCFG(
"11.1 Surround", 0x56ff)
179 SDL_WAVE_DEBUG_CHANNELSTR(
"FL", 0
x1)
180 SDL_WAVE_DEBUG_CHANNELSTR(
"FR", 0
x2)
181 SDL_WAVE_DEBUG_CHANNELSTR(
"FC", 0x4)
182 SDL_WAVE_DEBUG_CHANNELSTR(
"LF", 0x8)
183 SDL_WAVE_DEBUG_CHANNELSTR(
"BL", 0x10)
184 SDL_WAVE_DEBUG_CHANNELSTR(
"BR", 0x20)
185 SDL_WAVE_DEBUG_CHANNELSTR(
"FLC", 0x40)
186 SDL_WAVE_DEBUG_CHANNELSTR(
"FRC", 0x80)
187 SDL_WAVE_DEBUG_CHANNELSTR(
"BC", 0x100)
188 SDL_WAVE_DEBUG_CHANNELSTR(
"SL", 0x200)
189 SDL_WAVE_DEBUG_CHANNELSTR(
"SR", 0x400)
190 SDL_WAVE_DEBUG_CHANNELSTR(
"TC", 0x800)
191 SDL_WAVE_DEBUG_CHANNELSTR(
"TFL", 0x1000)
192 SDL_WAVE_DEBUG_CHANNELSTR(
"TFC", 0x2000)
193 SDL_WAVE_DEBUG_CHANNELSTR(
"TFR", 0x4000)
194 SDL_WAVE_DEBUG_CHANNELSTR(
"TBL", 0x8000)
195 SDL_WAVE_DEBUG_CHANNELSTR(
"TBC", 0x10000)
196 SDL_WAVE_DEBUG_CHANNELSTR(
"TBR", 0x20000)
200 switch (
format->channels) {
202 if (
SDL_snprintf(channelstr,
sizeof(channelstr),
"%u channels",
format->channels) >= 0) {
203 wavechannel = channelstr;
207 wavechannel =
"Unknown";
210 wavechannel =
"Mono";
213 wavechannel =
"Setero";
218 #undef SDL_WAVE_DEBUG_CHANNELCFG
219 #undef SDL_WAVE_DEBUG_CHANNELSTR
221 if (wavebps >= 1024) {
223 wavebps = wavebps / 1024 + (wavebps & 0x3ff ? 1 : 0);
230 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
235 const char *fmtstr1 =
"WAVE chunk dump:\n"
236 "-------------------------------------------\n"
238 "-------------------------------------------\n"
240 " wFormatTag 0x%04x\n"
242 " nSamplesPerSec %11u\n"
243 " nAvgBytesPerSec %11u\n"
244 " nBlockAlign %11u\n";
245 const char *fmtstr2 =
" wBitsPerSample %11u\n";
246 const char *fmtstr3 =
" cbSize %11u\n";
247 const char *fmtstr4a =
" wValidBitsPerSample %11u\n";
248 const char *fmtstr4b =
" wSamplesPerBlock %11u\n";
249 const char *fmtstr5 =
" dwChannelMask 0x%08x\n"
251 " %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n";
252 const char *fmtstr6 =
"-------------------------------------------\n"
254 " dwSampleLength %11u\n";
255 const char *fmtstr7 =
"-------------------------------------------\n"
257 "-------------------------------------------\n";
264 if (dumpstr ==
NULL) {
270 dumppos +=
res > 0 ?
res : 0;
273 dumppos +=
res > 0 ?
res : 0;
277 dumppos +=
res > 0 ?
res : 0;
285 switch (
format->encoding) {
288 dumppos +=
res > 0 ?
res : 0;
293 dumppos +=
res > 0 ?
res : 0;
296 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]);
297 dumppos +=
res > 0 ?
res : 0;
299 switch (
format->encoding) {
302 if (fmtlen >= 20 &&
format->extsize >= 2) {
304 dumppos +=
res > 0 ?
res : 0;
311 dumppos +=
res > 0 ?
res : 0;
314 dumppos +=
res > 0 ?
res : 0;
327 return SDL_SetError(
"Invalid number of sample frames in WAVE fact chunk (too many)");
347 if (datalength < blockheadersize || trailingdata > 0) {
354 if (trailingdata > 0) {
358 if (trailingdata >= blockheadersize) {
359 size_t trailingsamples = 2 + (trailingdata - blockheadersize) * 8 / blockframebitsize;
360 if (trailingsamples >
format->samplesperblock) {
361 trailingsamples =
format->samplesperblock;
381 const size_t blockheadersize = (
size_t)
format->channels * 7;
382 const size_t blockdatasize = (
size_t)
format->blockalign - blockheadersize;
384 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
385 const Sint16 presetcoeffs[14] = {256, 0, 512, -256, 0, 0, 192, 64, 240, 0, 460, -208, 392, -232};
386 size_t i, coeffcount;
395 if (
format->channels > 2) {
399 if (
format->bitspersample != 4) {
400 return SDL_SetError(
"Invalid MS ADPCM bits per sample of %u", (
unsigned int)
format->bitspersample);
404 if (
format->blockalign < blockheadersize) {
405 return SDL_SetError(
"Invalid MS ADPCM block size (nBlockAlign)");
413 return SDL_SetError(
"MS ADPCM with the extensible header is not supported");
419 if (chunk->
size < 22) {
420 return SDL_SetError(
"Could not read MS ADPCM format header");
430 if (coeffcount > 256) {
434 if (chunk->
size < 22 + coeffcount * 4) {
435 return SDL_SetError(
"Could not read custom coefficients in MS ADPCM format header");
436 }
else if (
format->extsize < 4 + coeffcount * 4) {
437 return SDL_SetError(
"Invalid MS ADPCM format header (too small)");
438 }
else if (coeffcount < 7) {
439 return SDL_SetError(
"Missing required coefficients in MS ADPCM format header");
444 if (coeffdata ==
NULL) {
451 for (
i = 0;
i < coeffcount * 2;
i++) {
456 if (
i < 14 &&
c != presetcoeffs[
i]) {
457 return SDL_SetError(
"Wrong preset coefficients in MS ADPCM format header");
465 if (
format->samplesperblock == 0) {
484 if (
format->samplesperblock == 1 || blockdatasamples < format->samplesperblock - 2) {
485 return SDL_SetError(
"Invalid number of samples per MS ADPCM block (wSamplesPerBlock)");
498 const Sint32 max_audioval = 32767;
499 const Sint32 min_audioval = -32768;
500 const Uint16 max_deltaval = 65535;
501 const Uint16 adaptive[] = {
502 230, 230, 230, 230, 307, 409, 512, 614,
503 768, 614, 512, 409, 307, 230, 230, 230
509 new_sample = (sample1 * cstate->
coeff1 + sample2 * cstate->
coeff2) / 256;
511 errordelta = (
Sint32)nybble - (nybble >= 0x08 ? 0x10 : 0);
512 new_sample += (
Sint32)delta * errordelta;
513 if (new_sample < min_audioval) {
514 new_sample = min_audioval;
515 }
else if (new_sample > max_audioval) {
516 new_sample = max_audioval;
518 delta = (delta * adaptive[nybble]) / 256;
521 }
else if (delta > max_deltaval) {
525 delta = max_deltaval;
529 return (
Sint16)new_sample;
542 for (
c = 0;
c < channels;
c++) {
546 coeffindex =
state->block.data[o];
547 if (coeffindex > ddata->coeffcount) {
548 return SDL_SetError(
"Invalid MS ADPCM coefficient index in block header");
550 cstate[
c].
coeff1 = ddata->coeff[coeffindex * 2];
551 cstate[
c].
coeff2 = ddata->coeff[coeffindex * 2 + 1];
554 o = channels +
c * 2;
560 o = channels * 3 +
c * 2;
562 if (sample >= 0x8000) {
567 o = channels * 5 +
c * 2;
569 if (sample >= 0x8000) {
583 state->framesleft -= 2;
602 size_t blockpos =
state->block.pos;
603 size_t blocksize =
state->block.size;
605 size_t outpos =
state->output.pos;
607 Sint64 blockframesleft =
state->samplesperblock - 2;
608 if (blockframesleft >
state->framesleft) {
609 blockframesleft =
state->framesleft;
612 while (blockframesleft > 0) {
613 for (
c = 0;
c < channels;
c++) {
614 if (nybble & 0x4000) {
616 }
else if (blockpos < blocksize) {
617 nybble =
state->block.data[blockpos++] | 0x4000;
620 state->output.pos = outpos -
c;
625 sample1 =
state->output.data[outpos - channels];
626 sample2 =
state->output.data[outpos - channels * 2];
629 state->output.data[outpos++] = sample1;
636 state->output.pos = outpos;
645 size_t bytesleft, outputsize;
688 state.output.pos = 0;
695 state.cstate = cstate;
698 bytesleft =
state.input.size -
state.input.pos;
699 while (
state.framesleft > 0 && bytesleft >=
state.blockheadersize) {
701 state.block.size = bytesleft <
state.blocksize ? bytesleft :
state.blocksize;
707 return SDL_SetError(
"Unexpected overflow in MS ADPCM decoder");
732 bytesleft =
state.input.size -
state.input.pos;
736 *audio_len = (
Uint32)outputsize;
746 const size_t subblockframesize = (
size_t)
format->channels * 4;
747 const size_t availableblocks = datalength /
format->blockalign;
748 const size_t trailingdata = datalength %
format->blockalign;
752 if (datalength < blockheadersize || trailingdata > 0) {
759 if (trailingdata > 0) {
765 size_t trailingsamples = 1;
767 if (trailingdata > blockheadersize) {
769 const size_t trailingblockdata = trailingdata - blockheadersize;
770 const size_t trailingsubblockdata = trailingblockdata % subblockframesize;
771 trailingsamples += (trailingblockdata / subblockframesize) * 8;
775 if (trailingsubblockdata > subblockframesize - 4) {
776 trailingsamples += (trailingsubblockdata % 4) * 2;
780 if (trailingsamples >
format->samplesperblock) {
781 trailingsamples =
format->samplesperblock;
800 const size_t blockheadersize = (
size_t)
format->channels * 4;
801 const size_t blockdatasize = (
size_t)
format->blockalign - blockheadersize;
803 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
808 if (
format->bitspersample == 3) {
809 return SDL_SetError(
"3-bit IMA ADPCM currently not supported");
810 }
else if (
format->bitspersample != 4) {
811 return SDL_SetError(
"Invalid IMA ADPCM bits per sample of %u", (
unsigned int)
format->bitspersample);
817 if (
format->blockalign < blockheadersize ||
format->blockalign % 4) {
818 return SDL_SetError(
"Invalid IMA ADPCM block size (nBlockAlign)");
827 if (chunk->
size >= 20 &&
format->extsize >= 2) {
832 if (
format->samplesperblock == 0) {
850 if (blockdatasamples < format->samplesperblock - 1) {
851 return SDL_SetError(
"Invalid number of samples per IMA ADPCM block (wSamplesPerBlock)");
864 const Sint32 max_audioval = 32767;
865 const Sint32 min_audioval = -32768;
866 const Sint8 index_table_4b[16] = {
872 const Uint16 step_table[89] = {
873 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
874 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
875 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
876 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
877 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
878 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
879 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
880 22385, 24623, 27086, 29794, 32767
889 }
else if (
index < 0) {
897 *cindex =
index + index_table_4b[nybble];
915 sample = lastsample + delta;
918 if (sample > max_audioval) {
919 sample = max_audioval;
920 }
else if (sample < min_audioval) {
921 sample = min_audioval;
934 for (
c = 0;
c <
state->channels;
c++) {
935 size_t o =
state->block.pos +
c * 4;
939 if (sample >= 0x8000) {
946 cstate[
c] = (
Sint8)(step > 0x80 ? step - 0x100 : step);
949 if (
state->block.data[o + 3] != 0) {
973 const size_t subblockframesize = channels * 4;
977 size_t blockpos =
state->block.pos;
978 size_t blocksize =
state->block.size;
979 size_t blockleft = blocksize - blockpos;
981 size_t outpos =
state->output.pos;
983 Sint64 blockframesleft =
state->samplesperblock - 1;
984 if (blockframesleft >
state->framesleft) {
985 blockframesleft =
state->framesleft;
988 bytesrequired = (blockframesleft + 7) / 8 * subblockframesize;
989 if (blockleft < bytesrequired) {
991 const size_t guaranteedframes = blockleft / subblockframesize;
992 const size_t remainingbytes = blockleft % subblockframesize;
993 blockframesleft = guaranteedframes;
994 if (remainingbytes > subblockframesize - 4) {
995 blockframesleft += (remainingbytes % 4) * 2;
1006 while (blockframesleft > 0) {
1007 const size_t subblocksamples = blockframesleft < 8 ? (
size_t)blockframesleft : 8;
1009 for (
c = 0;
c < channels;
c++) {
1012 Sint16 sample =
state->output.data[outpos +
c - channels];
1014 for (
i = 0;
i < subblocksamples;
i++) {
1018 nybble =
state->block.data[blockpos++];
1022 state->output.data[outpos +
c +
i * channels] = sample;
1026 outpos += channels * subblocksamples;
1027 state->framesleft -= subblocksamples;
1028 blockframesleft -= subblocksamples;
1031 state->block.pos = blockpos;
1032 state->output.pos = outpos;
1041 size_t bytesleft, outputsize;
1071 state.input.pos = 0;
1081 state.output.pos = 0;
1089 if (cstate ==
NULL) {
1093 state.cstate = cstate;
1096 bytesleft =
state.input.size -
state.input.pos;
1097 while (
state.framesleft > 0 && bytesleft >=
state.blockheadersize) {
1099 state.block.size = bytesleft <
state.blocksize ? bytesleft :
state.blocksize;
1100 state.block.pos = 0;
1106 return SDL_SetError(
"Unexpected overflow in IMA ADPCM decoder");
1130 bytesleft =
state.input.size -
state.input.pos;
1134 *audio_len = (
Uint32)outputsize;
1147 if (
format->bitspersample != 8) {
1148 return SDL_SetError(
"Invalid companded bits per sample of %u", (
unsigned int)
format->bitspersample);
1157 if (
format->blockalign > 1 && datalength %
format->blockalign) {
1158 return SDL_SetError(
"Truncated data chunk in WAVE file");
1173 #ifdef SDL_WAVE_LAW_LUT
1174 const Sint16 alaw_lut[256] = {
1175 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
1176 -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
1177 -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
1178 -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344,
1179 -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88,
1180 -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
1181 -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
1182 -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504,
1183 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752,
1184 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016,
1185 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008,
1186 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344,
1187 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88,
1188 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376,
1189 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688,
1190 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848
1192 const Sint16 mulaw_lut[256] = {
1193 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996,
1194 -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
1195 -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
1196 -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
1197 -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876,
1198 -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372,
1199 -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120,
1200 -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124,
1201 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996,
1202 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932,
1203 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
1204 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884,
1205 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876,
1206 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,
1207 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120,
1208 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0
1214 size_t i, sample_count, expanded_len;
1237 expanded_len = sample_count;
1259 #ifdef SDL_WAVE_LAW_LUT
1281 mantissa = (mantissa << 4) | 0x8;
1286 dst[
i] =
nibble & 0x80 ? mantissa : -mantissa;
1296 mantissa = (0x80 <<
exponent) + step * mantissa + step / 2 - 132;
1298 dst[
i] =
nibble & 0x80 ? -mantissa : mantissa;
1308 *audio_len = (
Uint32)expanded_len;
1319 switch (
format->bitspersample) {
1327 return SDL_SetError(
"%u-bit PCM format not supported", (
unsigned int)
format->bitspersample);
1330 if (
format->bitspersample != 32) {
1331 return SDL_SetError(
"%u-bit IEEE floating-point format not supported", (
unsigned int)
format->bitspersample);
1343 if (
format->blockalign > 1 && datalength %
format->blockalign) {
1344 return SDL_SetError(
"Truncated data chunk in WAVE file");
1361 size_t i, expanded_len, sample_count;
1369 expanded_len = sample_count;
1387 *audio_len = (
Uint32)expanded_len;
1390 for (
i = sample_count;
i > 0;
i--) {
1391 const size_t o =
i - 1;
1396 b[2] =
ptr[o * 3 + 1];
1397 b[3] =
ptr[o * 3 + 2];
1399 ptr[o * 4 + 0] =
b[0];
1400 ptr[o * 4 + 1] =
b[1];
1401 ptr[o * 4 + 2] =
b[2];
1402 ptr[o * 4 + 3] =
b[3];
1441 *audio_buf = chunk->
data;
1442 *audio_len = (
Uint32)outputsize;
1459 }
else if (
SDL_strcmp(hint,
"ignore") == 0) {
1461 }
else if (
SDL_strcmp(hint,
"ignorezero") == 0) {
1463 }
else if (
SDL_strcmp(hint,
"maximum") == 0) {
1479 }
else if (
SDL_strcmp(hint,
"strict") == 0) {
1481 }
else if (
SDL_strcmp(hint,
"dropframe") == 0) {
1483 }
else if (
SDL_strcmp(hint,
"dropblock") == 0) {
1499 }
else if (
SDL_strcmp(hint,
"strict") == 0) {
1501 }
else if (
SDL_strcmp(hint,
"ignorezero") == 0) {
1503 }
else if (
SDL_strcmp(hint,
"ignore") == 0) {
1549 chunk->
position = nextposition + 8;
1595 #define WAVE_FORMATTAG_GUID(tag) {(tag) & 0xff, (tag) >> 8, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113}
1623 size_t fmtlen = chunk->
size;
1630 if (fmtsrc ==
NULL) {
1646 return SDL_SetError(
"Missing wBitsPerSample field in WAVE fmt chunk");
1662 if (fmtlen < 40 || format->extsize < 22) {
1664 return SDL_SetError(
"Extensible WAVE header too small");
1686 if (
format->channels == 0) {
1688 }
else if (
format->channels > 255) {
1690 return SDL_SetError(
"Number of channels exceeds limit of 255");
1693 if (
format->frequency == 0) {
1702 return SDL_SetError(
"Invalid fact chunk in WAVE file");
1709 switch (
format->encoding) {
1717 return SDL_SetError(
"Missing fact chunk in WAVE file");
1723 return SDL_SetError(
"Missing wBitsPerSample field in WAVE fmt chunk");
1724 }
else if (
format->bitspersample == 0) {
1729 if (
format->blockalign == 0) {
1746 switch (
format->encoding) {
1749 if (
PCM_Init(file, datalength) < 0) {
1755 if (
LAW_Init(file, datalength) < 0) {
1774 const char *errstr =
"Unknown WAVE format GUID: %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x";
1779 return SDL_SetError(errstr, g1, g2, g3,
g[8],
g[9],
g[10],
g[11],
g[12],
g[13],
g[14],
g[15]);
1781 return SDL_SetError(
"Unknown WAVE format tag: 0x%04x", (
unsigned int)
format->encoding);
1792 Uint32 chunkcountlimit = 10000;
1793 char *envchunkcountlimit;
1794 Sint64 RIFFstart, RIFFend, lastchunkpos;
1806 envchunkcountlimit =
SDL_getenv(
"SDL_WAVE_CHUNK_LIMIT");
1807 if (envchunkcountlimit !=
NULL) {
1815 if (RIFFstart < 0) {
1831 return SDL_SetError(
"RIFF form type is not WAVE (not a Waveform file)");
1838 return SDL_SetError(
"Could not find RIFF or WAVE identifiers (not a Waveform file)");
1854 if (RIFFchunk.
length == 0) {
1874 if (chunkcount++ >= chunkcountlimit) {
1875 return SDL_SetError(
"Chunk count in WAVE file exceeds limit of %u", chunkcountlimit);
1886 }
else if (
result == -2) {
1887 return SDL_SetError(
"Could not seek to WAVE chunk header");
1896 return SDL_SetError(
"fmt chunk after data chunk in WAVE file");
1953 return SDL_SetError(
"Missing data chunk in WAVE file");
1962 return SDL_SetError(
"Could not seek to WAVE chunk data");
1976 return SDL_SetError(
"Could not read data of WAVE fmt chunk");
1982 if (chunk->
length < 14) {
1983 return SDL_SetError(
"Invalid WAVE fmt chunk length (too small)");
1984 }
else if (chunk->
size < 14) {
1985 return SDL_SetError(
"Could not read data of WAVE fmt chunk");
1992 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
1993 WaveDebugLogFormat(file);
1995 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
2008 }
else if (
result == -2) {
2009 return SDL_SetError(
"Could not seek data of WAVE data chunk");
2016 return SDL_SetError(
"Could not read data of WAVE data chunk");
2022 switch (
format->encoding) {
2025 if (
PCM_Decode(file, audio_buf, audio_len) < 0) {
2031 if (
LAW_Decode(file, audio_buf, audio_len) < 0) {
2055 switch (
format->encoding) {
2067 switch (
format->bitspersample) {
2080 return SDL_SetError(
"Unexpected %u-bit PCM data format", (
unsigned int)
format->bitspersample);
2088 if (RIFFlengthknown) {
2112 }
else if (audio_buf ==
NULL) {
2115 }
else if (audio_len ==
NULL) {