21 #include "../SDL_internal.h"
27 #define SIZE_MAX ((size_t)-1)
31 #define INT_MAX SDL_MAX_SINT32
50 if (*f1 > 0 &&
SIZE_MAX / *f1 <= f2) {
105 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
110 const char *fmtstr =
"WAVE file: %s, %u Hz, %s, %u bits, %u %s/s";
111 const char *waveformat, *wavechannel, *wavebpsunit =
"B";
117 switch (
format->encoding) {
122 waveformat =
"IEEE Float";
125 waveformat =
"A-law";
128 waveformat =
"\xc2\xb5-law";
131 waveformat =
"MS ADPCM";
134 waveformat =
"IMA ADPCM";
137 waveformat =
"Unknown";
141 #define SDL_WAVE_DEBUG_CHANNELCFG(STR, CODE) case CODE: wavechannel = STR; break;
142 #define SDL_WAVE_DEBUG_CHANNELSTR(STR, CODE) if (format->channelmask & CODE) { \
143 SDL_strlcat(channelstr, channelstr[0] ? "-" STR : STR, sizeof(channelstr));}
146 switch (
format->channelmask) {
147 SDL_WAVE_DEBUG_CHANNELCFG(
"1.0 Mono", 0x4)
148 SDL_WAVE_DEBUG_CHANNELCFG(
"1.1 Mono", 0xc)
149 SDL_WAVE_DEBUG_CHANNELCFG(
"2.0 Stereo", 0x3)
150 SDL_WAVE_DEBUG_CHANNELCFG(
"2.1 Stereo", 0xb)
151 SDL_WAVE_DEBUG_CHANNELCFG(
"3.0 Stereo", 0x7)
152 SDL_WAVE_DEBUG_CHANNELCFG(
"3.1 Stereo", 0xf)
153 SDL_WAVE_DEBUG_CHANNELCFG(
"3.0 Surround", 0x103)
154 SDL_WAVE_DEBUG_CHANNELCFG(
"3.1 Surround", 0x10b)
155 SDL_WAVE_DEBUG_CHANNELCFG(
"4.0 Quad", 0x33)
156 SDL_WAVE_DEBUG_CHANNELCFG(
"4.1 Quad", 0x3b)
157 SDL_WAVE_DEBUG_CHANNELCFG(
"4.0 Surround", 0x107)
158 SDL_WAVE_DEBUG_CHANNELCFG(
"4.1 Surround", 0x10f)
159 SDL_WAVE_DEBUG_CHANNELCFG(
"5.0", 0x37)
160 SDL_WAVE_DEBUG_CHANNELCFG(
"5.1", 0x3f)
161 SDL_WAVE_DEBUG_CHANNELCFG(
"5.0 Side", 0x607)
162 SDL_WAVE_DEBUG_CHANNELCFG(
"5.1 Side", 0x60f)
163 SDL_WAVE_DEBUG_CHANNELCFG(
"6.0", 0x137)
164 SDL_WAVE_DEBUG_CHANNELCFG(
"6.1", 0x13f)
165 SDL_WAVE_DEBUG_CHANNELCFG(
"6.0 Side", 0x707)
166 SDL_WAVE_DEBUG_CHANNELCFG(
"6.1 Side", 0x70f)
167 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0", 0xf7)
168 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1", 0xff)
169 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0 Side", 0x6c7)
170 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1 Side", 0x6cf)
171 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0 Surround", 0x637)
172 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1 Surround", 0x63f)
173 SDL_WAVE_DEBUG_CHANNELCFG(
"9.0 Surround", 0x5637)
174 SDL_WAVE_DEBUG_CHANNELCFG(
"9.1 Surround", 0x563f)
175 SDL_WAVE_DEBUG_CHANNELCFG(
"11.0 Surround", 0x56f7)
176 SDL_WAVE_DEBUG_CHANNELCFG(
"11.1 Surround", 0x56ff)
178 SDL_WAVE_DEBUG_CHANNELSTR(
"FL", 0
x1)
179 SDL_WAVE_DEBUG_CHANNELSTR(
"FR", 0
x2)
180 SDL_WAVE_DEBUG_CHANNELSTR(
"FC", 0x4)
181 SDL_WAVE_DEBUG_CHANNELSTR(
"LF", 0x8)
182 SDL_WAVE_DEBUG_CHANNELSTR(
"BL", 0x10)
183 SDL_WAVE_DEBUG_CHANNELSTR(
"BR", 0x20)
184 SDL_WAVE_DEBUG_CHANNELSTR(
"FLC", 0x40)
185 SDL_WAVE_DEBUG_CHANNELSTR(
"FRC", 0x80)
186 SDL_WAVE_DEBUG_CHANNELSTR(
"BC", 0x100)
187 SDL_WAVE_DEBUG_CHANNELSTR(
"SL", 0x200)
188 SDL_WAVE_DEBUG_CHANNELSTR(
"SR", 0x400)
189 SDL_WAVE_DEBUG_CHANNELSTR(
"TC", 0x800)
190 SDL_WAVE_DEBUG_CHANNELSTR(
"TFL", 0x1000)
191 SDL_WAVE_DEBUG_CHANNELSTR(
"TFC", 0x2000)
192 SDL_WAVE_DEBUG_CHANNELSTR(
"TFR", 0x4000)
193 SDL_WAVE_DEBUG_CHANNELSTR(
"TBL", 0x8000)
194 SDL_WAVE_DEBUG_CHANNELSTR(
"TBC", 0x10000)
195 SDL_WAVE_DEBUG_CHANNELSTR(
"TBR", 0x20000)
199 switch (
format->channels) {
201 if (
SDL_snprintf(channelstr,
sizeof(channelstr),
"%u channels",
format->channels) >= 0) {
202 wavechannel = channelstr;
206 wavechannel =
"Unknown";
209 wavechannel =
"Mono";
212 wavechannel =
"Setero";
217 #undef SDL_WAVE_DEBUG_CHANNELCFG
218 #undef SDL_WAVE_DEBUG_CHANNELSTR
220 if (wavebps >= 1024) {
222 wavebps = wavebps / 1024 + (wavebps & 0x3ff ? 1 : 0);
229 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
234 const char *fmtstr1 =
"WAVE chunk dump:\n"
235 "-------------------------------------------\n"
237 "-------------------------------------------\n"
239 " wFormatTag 0x%04x\n"
241 " nSamplesPerSec %11u\n"
242 " nAvgBytesPerSec %11u\n"
243 " nBlockAlign %11u\n";
244 const char *fmtstr2 =
" wBitsPerSample %11u\n";
245 const char *fmtstr3 =
" cbSize %11u\n";
246 const char *fmtstr4a =
" wValidBitsPerSample %11u\n";
247 const char *fmtstr4b =
" wSamplesPerBlock %11u\n";
248 const char *fmtstr5 =
" dwChannelMask 0x%08x\n"
250 " %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n";
251 const char *fmtstr6 =
"-------------------------------------------\n"
253 " dwSampleLength %11u\n";
254 const char *fmtstr7 =
"-------------------------------------------\n"
256 "-------------------------------------------\n";
263 if (dumpstr ==
NULL) {
269 dumppos +=
res > 0 ?
res : 0;
272 dumppos +=
res > 0 ?
res : 0;
276 dumppos +=
res > 0 ?
res : 0;
284 switch (
format->encoding) {
287 dumppos +=
res > 0 ?
res : 0;
292 dumppos +=
res > 0 ?
res : 0;
295 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]);
296 dumppos +=
res > 0 ?
res : 0;
298 switch (
format->encoding) {
301 if (fmtlen >= 20 &&
format->extsize >= 2) {
303 dumppos +=
res > 0 ?
res : 0;
310 dumppos +=
res > 0 ?
res : 0;
313 dumppos +=
res > 0 ?
res : 0;
326 return SDL_SetError(
"Invalid number of sample frames in WAVE fact chunk (too many)");
346 if (datalength < blockheadersize || trailingdata > 0) {
353 if (trailingdata > 0) {
357 if (trailingdata >= blockheadersize) {
358 size_t trailingsamples = 2 + (trailingdata - blockheadersize) * 8 / blockframebitsize;
359 if (trailingsamples >
format->samplesperblock) {
360 trailingsamples =
format->samplesperblock;
380 const size_t blockheadersize = (
size_t)
format->channels * 7;
381 const size_t blockdatasize = (
size_t)
format->blockalign - blockheadersize;
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;
394 if (
format->channels > 2) {
398 if (
format->bitspersample != 4) {
399 return SDL_SetError(
"Invalid MS ADPCM bits per sample of %u", (
unsigned int)
format->bitspersample);
403 if (
format->blockalign < blockheadersize) {
404 return SDL_SetError(
"Invalid MS ADPCM block size (nBlockAlign)");
412 return SDL_SetError(
"MS ADPCM with the extensible header is not supported");
418 if (chunk->
size < 22) {
419 return SDL_SetError(
"Could not read MS ADPCM format header");
429 if (coeffcount > 256) {
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");
443 if (coeffdata ==
NULL) {
450 for (
i = 0;
i < coeffcount * 2;
i++) {
455 if (
i < 14 &&
c != presetcoeffs[
i]) {
456 return SDL_SetError(
"Wrong preset coefficients in MS ADPCM format header");
464 if (
format->samplesperblock == 0) {
483 if (
format->samplesperblock == 1 || blockdatasamples < format->samplesperblock - 2) {
484 return SDL_SetError(
"Invalid number of samples per MS ADPCM block (wSamplesPerBlock)");
497 const Sint32 max_audioval = 32767;
498 const Sint32 min_audioval = -32768;
499 const Uint16 max_deltaval = 65535;
500 const Uint16 adaptive[] = {
501 230, 230, 230, 230, 307, 409, 512, 614,
502 768, 614, 512, 409, 307, 230, 230, 230
508 new_sample = (sample1 * cstate->
coeff1 + sample2 * cstate->
coeff2) / 256;
510 errordelta = (
Sint32)nybble - (nybble >= 0x08 ? 0x10 : 0);
511 new_sample += (
Sint32)delta * errordelta;
512 if (new_sample < min_audioval) {
513 new_sample = min_audioval;
514 }
else if (new_sample > max_audioval) {
515 new_sample = max_audioval;
517 delta = (delta * adaptive[nybble]) / 256;
520 }
else if (delta > max_deltaval) {
524 delta = max_deltaval;
528 return (
Sint16)new_sample;
541 for (
c = 0;
c < channels;
c++) {
545 coeffindex =
state->block.data[o];
546 if (coeffindex > ddata->coeffcount) {
547 return SDL_SetError(
"Invalid MS ADPCM coefficient index in block header");
549 cstate[
c].
coeff1 = ddata->coeff[coeffindex * 2];
550 cstate[
c].
coeff2 = ddata->coeff[coeffindex * 2 + 1];
553 o = channels +
c * 2;
559 o = channels * 3 +
c * 2;
561 if (sample >= 0x8000) {
566 o = channels * 5 +
c * 2;
568 if (sample >= 0x8000) {
582 state->framesleft -= 2;
601 size_t blockpos =
state->block.pos;
602 size_t blocksize =
state->block.size;
604 size_t outpos =
state->output.pos;
606 Sint64 blockframesleft =
state->samplesperblock - 2;
607 if (blockframesleft >
state->framesleft) {
608 blockframesleft =
state->framesleft;
611 while (blockframesleft > 0) {
612 for (
c = 0;
c < channels;
c++) {
613 if (nybble & 0x4000) {
615 }
else if (blockpos < blocksize) {
616 nybble =
state->block.data[blockpos++] | 0x4000;
619 state->output.pos = outpos -
c;
624 sample1 =
state->output.data[outpos - channels];
625 sample2 =
state->output.data[outpos - channels * 2];
628 state->output.data[outpos++] = sample1;
635 state->output.pos = outpos;
644 size_t bytesleft, outputsize;
687 state.output.pos = 0;
694 state.cstate = &cstate;
697 bytesleft =
state.input.size -
state.input.pos;
698 while (
state.framesleft > 0 && bytesleft >=
state.blockheadersize) {
700 state.block.size = bytesleft <
state.blocksize ? bytesleft :
state.blocksize;
706 return SDL_SetError(
"Unexpected overflow in MS ADPCM decoder");
731 bytesleft =
state.input.size -
state.input.pos;
735 *audio_len = (
Uint32)outputsize;
745 const size_t subblockframesize = (
size_t)
format->channels * 4;
746 const size_t availableblocks = datalength /
format->blockalign;
747 const size_t trailingdata = datalength %
format->blockalign;
751 if (datalength < blockheadersize || trailingdata > 0) {
758 if (trailingdata > 0) {
764 size_t trailingsamples = 1;
766 if (trailingdata > blockheadersize) {
768 const size_t trailingblockdata = trailingdata - blockheadersize;
769 const size_t trailingsubblockdata = trailingblockdata % subblockframesize;
770 trailingsamples += (trailingblockdata / subblockframesize) * 8;
774 if (trailingsubblockdata > subblockframesize - 4) {
775 trailingsamples += (trailingsubblockdata % 4) * 2;
779 if (trailingsamples >
format->samplesperblock) {
780 trailingsamples =
format->samplesperblock;
799 const size_t blockheadersize = (
size_t)
format->channels * 4;
800 const size_t blockdatasize = (
size_t)
format->blockalign - blockheadersize;
802 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
807 if (
format->bitspersample == 3) {
808 return SDL_SetError(
"3-bit IMA ADPCM currently not supported");
809 }
else if (
format->bitspersample != 4) {
810 return SDL_SetError(
"Invalid IMA ADPCM bits per sample of %u", (
unsigned int)
format->bitspersample);
816 if (
format->blockalign < blockheadersize ||
format->blockalign % 4) {
817 return SDL_SetError(
"Invalid IMA ADPCM block size (nBlockAlign)");
826 if (chunk->
size >= 20 &&
format->extsize >= 2) {
831 if (
format->samplesperblock == 0) {
849 if (blockdatasamples < format->samplesperblock - 1) {
850 return SDL_SetError(
"Invalid number of samples per IMA ADPCM block (wSamplesPerBlock)");
863 const Sint32 max_audioval = 32767;
864 const Sint32 min_audioval = -32768;
865 const Sint8 index_table_4b[16] = {
871 const Uint16 step_table[89] = {
872 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
873 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
874 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
875 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
876 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
877 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
878 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
879 22385, 24623, 27086, 29794, 32767
888 }
else if (
index < 0) {
896 *cindex =
index + index_table_4b[nybble];
914 sample = lastsample + delta;
917 if (sample > max_audioval) {
918 sample = max_audioval;
919 }
else if (sample < min_audioval) {
920 sample = min_audioval;
933 for (
c = 0;
c <
state->channels;
c++) {
934 size_t o =
state->block.pos +
c * 4;
938 if (sample >= 0x8000) {
945 cstate[
c] = (
Sint8)(step > 0x80 ? step - 0x100 : step);
948 if (
state->block.data[o + 3] != 0) {
972 const size_t subblockframesize = channels * 4;
976 size_t blockpos =
state->block.pos;
977 size_t blocksize =
state->block.size;
978 size_t blockleft = blocksize - blockpos;
980 size_t outpos =
state->output.pos;
982 Sint64 blockframesleft =
state->samplesperblock - 1;
983 if (blockframesleft >
state->framesleft) {
984 blockframesleft =
state->framesleft;
987 bytesrequired = (blockframesleft + 7) / 8 * subblockframesize;
988 if (blockleft < bytesrequired) {
990 const size_t guaranteedframes = blockleft / subblockframesize;
991 const size_t remainingbytes = blockleft % subblockframesize;
992 blockframesleft = guaranteedframes;
993 if (remainingbytes > subblockframesize - 4) {
994 blockframesleft += (remainingbytes % 4) * 2;
1005 while (blockframesleft > 0) {
1006 const size_t subblocksamples = blockframesleft < 8 ? (
size_t)blockframesleft : 8;
1008 for (
c = 0;
c < channels;
c++) {
1011 Sint16 sample =
state->output.data[outpos +
c - channels];
1013 for (
i = 0;
i < subblocksamples;
i++) {
1017 nybble =
state->block.data[blockpos++];
1021 state->output.data[outpos +
c +
i * channels] = sample;
1025 outpos += channels * subblocksamples;
1026 state->framesleft -= subblocksamples;
1027 blockframesleft -= subblocksamples;
1030 state->block.pos = blockpos;
1031 state->output.pos = outpos;
1040 size_t bytesleft, outputsize;
1070 state.input.pos = 0;
1080 state.output.pos = 0;
1088 if (cstate ==
NULL) {
1092 state.cstate = cstate;
1095 bytesleft =
state.input.size -
state.input.pos;
1096 while (
state.framesleft > 0 && bytesleft >=
state.blockheadersize) {
1098 state.block.size = bytesleft <
state.blocksize ? bytesleft :
state.blocksize;
1099 state.block.pos = 0;
1105 return SDL_SetError(
"Unexpected overflow in IMA ADPCM decoder");
1129 bytesleft =
state.input.size -
state.input.pos;
1133 *audio_len = (
Uint32)outputsize;
1146 if (
format->bitspersample != 8) {
1147 return SDL_SetError(
"Invalid companded bits per sample of %u", (
unsigned int)
format->bitspersample);
1156 if (
format->blockalign > 1 && datalength %
format->blockalign) {
1157 return SDL_SetError(
"Truncated data chunk in WAVE file");
1172 #ifdef SDL_WAVE_LAW_LUT
1173 const Sint16 alaw_lut[256] = {
1174 -5504, -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, 5504,
1182 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752,
1183 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016,
1184 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008,
1185 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344,
1186 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88,
1187 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376,
1188 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688,
1189 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848
1191 const Sint16 mulaw_lut[256] = {
1192 -32124, -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, 32124,
1200 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996,
1201 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932,
1202 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
1203 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884,
1204 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876,
1205 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,
1206 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120,
1207 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0
1213 size_t i, sample_count, expanded_len;
1236 expanded_len = sample_count;
1258 #ifdef SDL_WAVE_LAW_LUT
1280 mantissa = (mantissa << 4) | 0x8;
1285 dst[
i] =
nibble & 0x80 ? mantissa : -mantissa;
1295 mantissa = (0x80 <<
exponent) + step * mantissa + step / 2 - 132;
1297 dst[
i] =
nibble & 0x80 ? -mantissa : mantissa;
1307 *audio_len = (
Uint32)expanded_len;
1318 switch (
format->bitspersample) {
1326 return SDL_SetError(
"%u-bit PCM format not supported", (
unsigned int)
format->bitspersample);
1329 if (
format->bitspersample != 32) {
1330 return SDL_SetError(
"%u-bit IEEE floating-point format not supported", (
unsigned int)
format->bitspersample);
1342 if (
format->blockalign > 1 && datalength %
format->blockalign) {
1343 return SDL_SetError(
"Truncated data chunk in WAVE file");
1360 size_t i, expanded_len, sample_count;
1368 expanded_len = sample_count;
1386 *audio_len = (
Uint32)expanded_len;
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];
1440 *audio_buf = chunk->
data;
1441 *audio_len = (
Uint32)outputsize;
1458 }
else if (
SDL_strcmp(hint,
"ignore") == 0) {
1460 }
else if (
SDL_strcmp(hint,
"ignorezero") == 0) {
1462 }
else if (
SDL_strcmp(hint,
"maximum") == 0) {
1478 }
else if (
SDL_strcmp(hint,
"strict") == 0) {
1480 }
else if (
SDL_strcmp(hint,
"dropframe") == 0) {
1482 }
else if (
SDL_strcmp(hint,
"dropblock") == 0) {
1498 }
else if (
SDL_strcmp(hint,
"strict") == 0) {
1500 }
else if (
SDL_strcmp(hint,
"ignorezero") == 0) {
1502 }
else if (
SDL_strcmp(hint,
"ignore") == 0) {
1548 chunk->
position = nextposition + 8;
1594 #define WAVE_FORMATTAG_GUID(tag) {(tag) & 0xff, (tag) >> 8, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113}
1622 size_t fmtlen = chunk->
size;
1629 if (fmtsrc ==
NULL) {
1645 return SDL_SetError(
"Missing wBitsPerSample field in WAVE fmt chunk");
1661 if (fmtlen < 40 || format->extsize < 22) {
1663 return SDL_SetError(
"Extensible WAVE header too small");
1685 if (
format->channels == 0) {
1687 }
else if (
format->channels > 255) {
1689 return SDL_SetError(
"Number of channels exceeds limit of 255");
1692 if (
format->frequency == 0) {
1701 return SDL_SetError(
"Invalid fact chunk in WAVE file");
1708 switch (
format->encoding) {
1716 return SDL_SetError(
"Missing fact chunk in WAVE file");
1722 return SDL_SetError(
"Missing wBitsPerSample field in WAVE fmt chunk");
1723 }
else if (
format->bitspersample == 0) {
1728 if (
format->blockalign == 0) {
1745 switch (
format->encoding) {
1748 if (
PCM_Init(file, datalength) < 0) {
1754 if (
LAW_Init(file, datalength) < 0) {
1773 const char *errstr =
"Unknown WAVE format GUID: %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x";
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);
1791 Uint32 chunkcountlimit = 10000;
1792 char *envchunkcountlimit;
1793 Sint64 RIFFstart, RIFFend, lastchunkpos;
1805 envchunkcountlimit =
SDL_getenv(
"SDL_WAVE_CHUNK_LIMIT");
1806 if (envchunkcountlimit !=
NULL) {
1814 if (RIFFstart < 0) {
1830 return SDL_SetError(
"RIFF form type is not WAVE (not a Waveform file)");
1837 return SDL_SetError(
"Could not find RIFF or WAVE identifiers (not a Waveform file)");
1853 if (RIFFchunk.
length == 0) {
1873 if (chunkcount++ >= chunkcountlimit) {
1874 return SDL_SetError(
"Chunk count in WAVE file exceeds limit of %u", chunkcountlimit);
1885 }
else if (
result == -2) {
1886 return SDL_SetError(
"Could not seek to WAVE chunk header");
1895 return SDL_SetError(
"fmt chunk after data chunk in WAVE file");
1952 return SDL_SetError(
"Missing data chunk in WAVE file");
1961 return SDL_SetError(
"Could not seek to WAVE chunk data");
1975 return SDL_SetError(
"Could not read data of WAVE fmt chunk");
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");
1991 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
1992 WaveDebugLogFormat(file);
1994 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
2007 }
else if (
result == -2) {
2008 return SDL_SetError(
"Could not seek data of WAVE data chunk");
2015 return SDL_SetError(
"Could not read data of WAVE data chunk");
2021 switch (
format->encoding) {
2024 if (
PCM_Decode(file, audio_buf, audio_len) < 0) {
2030 if (
LAW_Decode(file, audio_buf, audio_len) < 0) {
2054 switch (
format->encoding) {
2066 switch (
format->bitspersample) {
2079 return SDL_SetError(
"Unexpected %u-bit PCM data format", (
unsigned int)
format->bitspersample);
2085 if (RIFFlengthknown) {
2109 }
else if (audio_buf ==
NULL) {
2112 }
else if (audio_len ==
NULL) {