21 #include "../SDL_internal.h"
27 #define SIZE_MAX ((size_t)-1)
31 #define INT_MAX (SDL_MAX_SINT32)
49 if (*f1 > 0 &&
SIZE_MAX / *f1 <= f2) {
104 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
109 const char *fmtstr =
"WAVE file: %s, %u Hz, %s, %u bits, %u %s/s";
110 const char *waveformat, *wavechannel, *wavebpsunit =
"B";
112 char channelstr[64] = {0};
114 switch (
format->encoding) {
119 waveformat =
"IEEE Float";
122 waveformat =
"A-law";
125 waveformat =
"\xc2\xb5-law";
128 waveformat =
"MS ADPCM";
131 waveformat =
"IMA ADPCM";
134 waveformat =
"Unknown";
138 #define SDL_WAVE_DEBUG_CHANNELCFG(STR, CODE) case CODE: wavechannel = STR; break;
139 #define SDL_WAVE_DEBUG_CHANNELSTR(STR, CODE) if (format->channelmask & CODE) { \
140 SDL_strlcat(channelstr, channelstr[0] ? "-" STR : STR, sizeof(channelstr));}
143 switch (
format->channelmask) {
144 SDL_WAVE_DEBUG_CHANNELCFG(
"1.0 Mono", 0x4)
145 SDL_WAVE_DEBUG_CHANNELCFG(
"1.1 Mono", 0xc)
146 SDL_WAVE_DEBUG_CHANNELCFG(
"2.0 Stereo", 0x3)
147 SDL_WAVE_DEBUG_CHANNELCFG(
"2.1 Stereo", 0xb)
148 SDL_WAVE_DEBUG_CHANNELCFG(
"3.0 Stereo", 0x7)
149 SDL_WAVE_DEBUG_CHANNELCFG(
"3.1 Stereo", 0xf)
150 SDL_WAVE_DEBUG_CHANNELCFG(
"3.0 Surround", 0x103)
151 SDL_WAVE_DEBUG_CHANNELCFG(
"3.1 Surround", 0x10b)
152 SDL_WAVE_DEBUG_CHANNELCFG(
"4.0 Quad", 0x33)
153 SDL_WAVE_DEBUG_CHANNELCFG(
"4.1 Quad", 0x3b)
154 SDL_WAVE_DEBUG_CHANNELCFG(
"4.0 Surround", 0x107)
155 SDL_WAVE_DEBUG_CHANNELCFG(
"4.1 Surround", 0x10f)
156 SDL_WAVE_DEBUG_CHANNELCFG(
"5.0", 0x37)
157 SDL_WAVE_DEBUG_CHANNELCFG(
"5.1", 0x3f)
158 SDL_WAVE_DEBUG_CHANNELCFG(
"5.0 Side", 0x607)
159 SDL_WAVE_DEBUG_CHANNELCFG(
"5.1 Side", 0x60f)
160 SDL_WAVE_DEBUG_CHANNELCFG(
"6.0", 0x137)
161 SDL_WAVE_DEBUG_CHANNELCFG(
"6.1", 0x13f)
162 SDL_WAVE_DEBUG_CHANNELCFG(
"6.0 Side", 0x707)
163 SDL_WAVE_DEBUG_CHANNELCFG(
"6.1 Side", 0x70f)
164 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0", 0xf7)
165 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1", 0xff)
166 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0 Side", 0x6c7)
167 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1 Side", 0x6cf)
168 SDL_WAVE_DEBUG_CHANNELCFG(
"7.0 Surround", 0x637)
169 SDL_WAVE_DEBUG_CHANNELCFG(
"7.1 Surround", 0x63f)
170 SDL_WAVE_DEBUG_CHANNELCFG(
"9.0 Surround", 0x5637)
171 SDL_WAVE_DEBUG_CHANNELCFG(
"9.1 Surround", 0x563f)
172 SDL_WAVE_DEBUG_CHANNELCFG(
"11.0 Surround", 0x56f7)
173 SDL_WAVE_DEBUG_CHANNELCFG(
"11.1 Surround", 0x56ff)
175 SDL_WAVE_DEBUG_CHANNELSTR(
"FL", 0
x1)
176 SDL_WAVE_DEBUG_CHANNELSTR(
"FR", 0
x2)
177 SDL_WAVE_DEBUG_CHANNELSTR(
"FC", 0x4)
178 SDL_WAVE_DEBUG_CHANNELSTR(
"LF", 0x8)
179 SDL_WAVE_DEBUG_CHANNELSTR(
"BL", 0x10)
180 SDL_WAVE_DEBUG_CHANNELSTR(
"BR", 0x20)
181 SDL_WAVE_DEBUG_CHANNELSTR(
"FLC", 0x40)
182 SDL_WAVE_DEBUG_CHANNELSTR(
"FRC", 0x80)
183 SDL_WAVE_DEBUG_CHANNELSTR(
"BC", 0x100)
184 SDL_WAVE_DEBUG_CHANNELSTR(
"SL", 0x200)
185 SDL_WAVE_DEBUG_CHANNELSTR(
"SR", 0x400)
186 SDL_WAVE_DEBUG_CHANNELSTR(
"TC", 0x800)
187 SDL_WAVE_DEBUG_CHANNELSTR(
"TFL", 0x1000)
188 SDL_WAVE_DEBUG_CHANNELSTR(
"TFC", 0x2000)
189 SDL_WAVE_DEBUG_CHANNELSTR(
"TFR", 0x4000)
190 SDL_WAVE_DEBUG_CHANNELSTR(
"TBL", 0x8000)
191 SDL_WAVE_DEBUG_CHANNELSTR(
"TBC", 0x10000)
192 SDL_WAVE_DEBUG_CHANNELSTR(
"TBR", 0x20000)
196 switch (
format->channels) {
198 if (
SDL_snprintf(channelstr,
sizeof(channelstr),
"%u channels",
format->channels) >= 0) {
199 wavechannel = channelstr;
203 wavechannel =
"Unknown";
206 wavechannel =
"Mono";
209 wavechannel =
"Setero";
214 #undef SDL_WAVE_DEBUG_CHANNELCFG
215 #undef SDL_WAVE_DEBUG_CHANNELSTR
217 if (wavebps >= 1024) {
219 wavebps = wavebps / 1024 + (wavebps & 0x3ff ? 1 : 0);
226 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
231 const char *fmtstr1 =
"WAVE chunk dump:\n"
232 "-------------------------------------------\n"
234 "-------------------------------------------\n"
236 " wFormatTag 0x%04x\n"
238 " nSamplesPerSec %11u\n"
239 " nAvgBytesPerSec %11u\n"
240 " nBlockAlign %11u\n";
241 const char *fmtstr2 =
" wBitsPerSample %11u\n";
242 const char *fmtstr3 =
" cbSize %11u\n";
243 const char *fmtstr4a =
" wValidBitsPerSample %11u\n";
244 const char *fmtstr4b =
" wSamplesPerBlock %11u\n";
245 const char *fmtstr5 =
" dwChannelMask 0x%08x\n"
247 " %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n";
248 const char *fmtstr6 =
"-------------------------------------------\n"
250 " dwSampleLength %11u\n";
251 const char *fmtstr7 =
"-------------------------------------------\n"
253 "-------------------------------------------\n";
260 if (dumpstr ==
NULL) {
266 dumppos +=
res > 0 ?
res : 0;
269 dumppos +=
res > 0 ?
res : 0;
273 dumppos +=
res > 0 ?
res : 0;
281 switch (
format->encoding) {
284 dumppos +=
res > 0 ?
res : 0;
289 dumppos +=
res > 0 ?
res : 0;
292 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]);
293 dumppos +=
res > 0 ?
res : 0;
295 switch (
format->encoding) {
298 if (fmtlen >= 20 &&
format->extsize >= 2) {
300 dumppos +=
res > 0 ?
res : 0;
307 dumppos +=
res > 0 ?
res : 0;
310 dumppos +=
res > 0 ?
res : 0;
323 return SDL_SetError(
"Invalid number of sample frames in WAVE fact chunk (too many)");
343 if (datalength < blockheadersize || trailingdata > 0) {
350 if (trailingdata > 0) {
354 if (trailingdata >= blockheadersize) {
355 size_t trailingsamples = 2 + (trailingdata - blockheadersize) * 8 / blockframebitsize;
356 if (trailingsamples >
format->samplesperblock) {
357 trailingsamples =
format->samplesperblock;
377 const size_t blockheadersize =
format->channels * 7;
378 const size_t blockdatasize = (
size_t)
format->blockalign - blockheadersize;
379 const size_t blockframebitsize =
format->bitspersample *
format->channels;
380 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
381 const Sint16 presetcoeffs[14] = {256, 0, 512, -256, 0, 0, 192, 64, 240, 0, 460, -208, 392, -232};
382 size_t i, coeffcount;
391 if (
format->channels > 2) {
395 if (
format->bitspersample != 4) {
396 return SDL_SetError(
"Invalid MS ADPCM bits per sample of %d", (
int)
format->bitspersample);
400 if (
format->blockalign < blockheadersize) {
401 return SDL_SetError(
"Invalid MS ADPCM block size (nBlockAlign)");
409 return SDL_SetError(
"MS ADPCM with the extensible header is not supported");
415 if (chunk->
size < 22) {
416 return SDL_SetError(
"Could not read MS ADPCM format header");
426 if (coeffcount > 256) {
430 if (chunk->
size < 22 + coeffcount * 4) {
431 return SDL_SetError(
"Could not read custom coefficients in MS ADPCM format header");
432 }
else if (
format->extsize < 4 + coeffcount * 4) {
433 return SDL_SetError(
"Invalid MS ADPCM format header (too small)");
434 }
else if (coeffcount < 7) {
435 return SDL_SetError(
"Missing required coefficients in MS ADPCM format header");
440 if (coeffdata ==
NULL) {
447 for (
i = 0;
i < coeffcount * 2;
i++) {
452 if (
i < 14 &&
c != presetcoeffs[
i]) {
453 return SDL_SetError(
"Wrong preset coefficients in MS ADPCM format header");
461 if (
format->samplesperblock == 0) {
480 if (
format->samplesperblock == 1 || blockdatasamples < format->samplesperblock - 2) {
481 return SDL_SetError(
"Invalid number of samples per MS ADPCM block (wSamplesPerBlock)");
494 const Sint32 max_audioval = 32767;
495 const Sint32 min_audioval = -32768;
496 const Uint16 max_deltaval = 65535;
497 const Uint16 adaptive[] = {
498 230, 230, 230, 230, 307, 409, 512, 614,
499 768, 614, 512, 409, 307, 230, 230, 230
505 new_sample = (sample1 * cstate->
coeff1 + sample2 * cstate->
coeff2) / 256;
507 errordelta = (
Sint32)nybble - (nybble >= 0x08 ? 0x10 : 0);
508 new_sample += (
Sint32)delta * errordelta;
509 if (new_sample < min_audioval) {
510 new_sample = min_audioval;
511 }
else if (new_sample > max_audioval) {
512 new_sample = max_audioval;
514 delta = (delta * adaptive[nybble]) / 256;
517 }
else if (delta > max_deltaval) {
521 delta = max_deltaval;
525 return (
Sint16)new_sample;
538 for (
c = 0;
c < channels;
c++) {
542 coeffindex =
state->block.data[o];
543 if (coeffindex > ddata->coeffcount) {
544 return SDL_SetError(
"Invalid MS ADPCM coefficient index in block header");
546 cstate[
c].
coeff1 = ddata->coeff[coeffindex * 2];
547 cstate[
c].
coeff2 = ddata->coeff[coeffindex * 2 + 1];
550 o = channels +
c * 2;
556 o = channels * 3 +
c * 2;
558 if (sample >= 0x8000) {
563 o = channels * 5 +
c * 2;
565 if (sample >= 0x8000) {
579 state->framesleft -= 2;
598 size_t blockpos =
state->block.pos;
599 size_t blocksize =
state->block.size;
601 size_t outpos =
state->output.pos;
603 Sint64 blockframesleft =
state->samplesperblock - 2;
604 if (blockframesleft >
state->framesleft) {
605 blockframesleft =
state->framesleft;
608 while (blockframesleft > 0) {
609 for (
c = 0;
c < channels;
c++) {
610 if (nybble & 0x8000) {
612 }
else if (blockpos < blocksize) {
613 nybble =
state->block.data[blockpos++] | 0x8000;
616 state->output.pos = outpos -
c;
621 sample1 =
state->output.data[outpos - channels];
622 sample2 =
state->output.data[outpos - channels * 2];
625 state->output.data[outpos++] = sample1;
632 state->output.pos = outpos;
641 size_t bytesleft, outputsize;
681 state.output.pos = 0;
688 state.cstate = &cstate;
691 bytesleft =
state.input.size -
state.input.pos;
692 while (
state.framesleft > 0 && bytesleft >=
state.blockheadersize) {
694 state.block.size = bytesleft <
state.blocksize ? bytesleft :
state.blocksize;
700 return SDL_SetError(
"Unexpected overflow in MS ADPCM decoder");
725 bytesleft =
state.input.size -
state.input.pos;
729 *audio_len = (
Uint32)outputsize;
739 const size_t subblockframesize =
format->channels * 4;
740 const size_t availableblocks = datalength /
format->blockalign;
741 const size_t trailingdata = datalength %
format->blockalign;
745 if (datalength < blockheadersize || trailingdata > 0) {
752 if (trailingdata > 0) {
758 size_t trailingsamples = 1;
760 if (trailingdata > blockheadersize) {
762 const size_t trailingblockdata = trailingdata - blockheadersize;
763 const size_t trailingsubblockdata = trailingblockdata % subblockframesize;
764 trailingsamples += (trailingblockdata / subblockframesize) * 8;
768 if (trailingsubblockdata > subblockframesize - 4) {
769 trailingsamples += (trailingsubblockdata % 4) * 2;
773 if (trailingsamples >
format->samplesperblock) {
774 trailingsamples =
format->samplesperblock;
793 const size_t blockheadersize =
format->channels * 4;
794 const size_t blockdatasize = (
size_t)
format->blockalign - blockheadersize;
795 const size_t blockframebitsize =
format->bitspersample *
format->channels;
796 const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
801 if (
format->bitspersample == 3) {
802 return SDL_SetError(
"3-bit IMA ADPCM currently not supported");
803 }
else if (
format->bitspersample != 4) {
804 return SDL_SetError(
"Invalid IMA ADPCM bits per sample of %d", (
int)
format->bitspersample);
810 if (
format->blockalign < blockheadersize ||
format->blockalign % 4) {
811 return SDL_SetError(
"Invalid IMA ADPCM block size (nBlockAlign)");
820 if (chunk->
size >= 20 &&
format->extsize >= 2) {
825 if (
format->samplesperblock == 0) {
843 if (blockdatasamples < format->samplesperblock - 1) {
844 return SDL_SetError(
"Invalid number of samples per IMA ADPCM block (wSamplesPerBlock)");
857 const Sint32 max_audioval = 32767;
858 const Sint32 min_audioval = -32768;
859 const Sint8 index_table_4b[16] = {
865 const Uint16 step_table[89] = {
866 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
867 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
868 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
869 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
870 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
871 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
872 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
873 22385, 24623, 27086, 29794, 32767
882 }
else if (
index < 0) {
890 *cindex =
index + index_table_4b[nybble];
908 sample = lastsample + delta;
911 if (sample > max_audioval) {
912 sample = max_audioval;
913 }
else if (sample < min_audioval) {
914 sample = min_audioval;
927 for (
c = 0;
c <
state->channels;
c++) {
928 size_t o =
state->block.pos +
c * 4;
932 if (sample >= 0x8000) {
939 cstate[
c] = (
Sint8)(step > 0x80 ? step - 0x100 : step);
942 if (
state->block.data[o + 3] != 0) {
966 const size_t subblockframesize = channels * 4;
970 size_t blockpos =
state->block.pos;
971 size_t blocksize =
state->block.size;
972 size_t blockleft = blocksize - blockpos;
974 size_t outpos =
state->output.pos;
976 Sint64 blockframesleft =
state->samplesperblock - 1;
977 if (blockframesleft >
state->framesleft) {
978 blockframesleft =
state->framesleft;
981 bytesrequired = (blockframesleft + 7) / 8 * subblockframesize;
982 if (blockleft < bytesrequired) {
984 const size_t guaranteedframes = blockleft / subblockframesize;
985 const size_t remainingbytes = blockleft % subblockframesize;
986 blockframesleft = guaranteedframes;
987 if (remainingbytes > subblockframesize - 4) {
988 blockframesleft += (remainingbytes % 4) * 2;
999 while (blockframesleft > 0) {
1000 const size_t subblocksamples = blockframesleft < 8 ? (
size_t)blockframesleft : 8;
1002 for (
c = 0;
c < channels;
c++) {
1005 Sint16 sample =
state->output.data[outpos +
c - channels];
1007 for (
i = 0;
i < subblocksamples;
i++) {
1011 nybble =
state->block.data[blockpos++];
1015 state->output.data[outpos +
c +
i * channels] = sample;
1019 outpos += channels * subblocksamples;
1020 state->framesleft -= subblocksamples;
1021 blockframesleft -= subblocksamples;
1024 state->block.pos = blockpos;
1025 state->output.pos = outpos;
1034 size_t bytesleft, outputsize;
1063 state.input.pos = 0;
1073 state.output.pos = 0;
1081 if (cstate ==
NULL) {
1085 state.cstate = cstate;
1088 bytesleft =
state.input.size -
state.input.pos;
1089 while (
state.framesleft > 0 && bytesleft >=
state.blockheadersize) {
1091 state.block.size = bytesleft <
state.blocksize ? bytesleft :
state.blocksize;
1092 state.block.pos = 0;
1098 return SDL_SetError(
"Unexpected overflow in IMA ADPCM decoder");
1120 bytesleft =
state.input.size -
state.input.pos;
1124 *audio_len = (
Uint32)outputsize;
1137 if (
format->bitspersample != 8) {
1138 return SDL_SetError(
"Invalid companded bits per sample of %d", (
int)
format->bitspersample);
1147 if (
format->blockalign > 1 && datalength %
format->blockalign) {
1148 return SDL_SetError(
"Truncated data chunk in WAVE file");
1163 #ifdef SDL_WAVE_LAW_LUT
1164 const Sint16 alaw_lut[256] = {
1165 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
1166 -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
1167 -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
1168 -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344,
1169 -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88,
1170 -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
1171 -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
1172 -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504,
1173 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
1182 const Sint16 mulaw_lut[256] = {
1183 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996,
1184 -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
1185 -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
1186 -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
1187 -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876,
1188 -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372,
1189 -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120,
1190 -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124,
1191 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
1204 size_t i, sample_count, expanded_len;
1227 expanded_len = sample_count;
1248 #ifdef SDL_WAVE_LAW_LUT
1270 mantissa = mantissa << 4 | 0x8;
1275 dst[
i] =
nibble & 0x80 ? mantissa : -mantissa;
1285 mantissa = (0x80 <<
exponent) + step * mantissa + step / 2 - 132;
1287 dst[
i] =
nibble & 0x80 ? -mantissa : mantissa;
1297 *audio_len = (
Uint32)expanded_len;
1308 switch (
format->bitspersample) {
1319 if (
format->bitspersample != 32) {
1320 return SDL_SetError(
"%d-bit IEEE floating-point format not supported", (
int)
format->bitspersample);
1332 if (
format->blockalign > 1 && datalength %
format->blockalign) {
1333 return SDL_SetError(
"Truncated data chunk in WAVE file");
1350 size_t i, expanded_len, sample_count;
1358 expanded_len = sample_count;
1375 *audio_len = (
Uint32)expanded_len;
1378 for (
i = sample_count;
i > 0;
i--) {
1379 const size_t o =
i - 1;
1384 b[2] = ptr[o * 3 + 1];
1385 b[3] = ptr[o * 3 + 2];
1387 ptr[o * 4 + 0] =
b[0];
1388 ptr[o * 4 + 1] =
b[1];
1389 ptr[o * 4 + 2] =
b[2];
1390 ptr[o * 4 + 3] =
b[3];
1429 *audio_buf = chunk->
data;
1430 *audio_len = (
Uint32)outputsize;
1447 }
else if (
SDL_strcmp(hint,
"ignore") == 0) {
1449 }
else if (
SDL_strcmp(hint,
"ignorezero") == 0) {
1451 }
else if (
SDL_strcmp(hint,
"maximum") == 0) {
1467 }
else if (
SDL_strcmp(hint,
"strict") == 0) {
1469 }
else if (
SDL_strcmp(hint,
"dropframe") == 0) {
1471 }
else if (
SDL_strcmp(hint,
"dropblock") == 0) {
1487 }
else if (
SDL_strcmp(hint,
"strict") == 0) {
1489 }
else if (
SDL_strcmp(hint,
"ignorezero") == 0) {
1491 }
else if (
SDL_strcmp(hint,
"ignore") == 0) {
1532 chunk->
position = nextposition + 8;
1578 #define WAVE_FORMATTAG_GUID(tag) {(tag) & 0xff, (tag) >> 8, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113}
1606 size_t fmtlen = chunk->
size;
1613 if (fmtsrc ==
NULL) {
1629 return SDL_SetError(
"Missing wBitsPerSample field in WAVE fmt chunk");
1645 if (fmtlen < 40 || format->extsize < 22) {
1647 return SDL_SetError(
"Extensible WAVE header too small");
1669 if (
format->channels == 0) {
1671 }
else if (
format->channels > 255) {
1673 return SDL_SetError(
"Number of channels exceeds limit of 255");
1676 if (
format->frequency == 0) {
1685 return SDL_SetError(
"Invalid fact chunk in WAVE file");
1692 switch (
format->encoding) {
1700 return SDL_SetError(
"Missing fact chunk in WAVE file");
1706 return SDL_SetError(
"Missing wBitsPerSample field in WAVE fmt chunk");
1707 }
else if (
format->bitspersample == 0) {
1712 if (
format->blockalign == 0) {
1729 switch (
format->encoding) {
1732 if (
PCM_Init(file, datalength) < 0) {
1738 if (
LAW_Init(file, datalength) < 0) {
1757 const char *errstr =
"Unknown WAVE format GUID: %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x";
1762 return SDL_SetError(errstr, g1, g2, g3,
g[8],
g[9],
g[10],
g[11],
g[12],
g[13],
g[14],
g[15]);
1775 Uint32 chunkcountlimit = 10000;
1776 char *envchunkcountlimit;
1777 Sint64 RIFFstart, RIFFend, lastchunkpos;
1785 envchunkcountlimit =
SDL_getenv(
"SDL_WAVE_CHUNK_LIMIT");
1786 if (envchunkcountlimit !=
NULL) {
1794 if (RIFFstart < 0) {
1810 return SDL_SetError(
"RIFF form type is not WAVE (not a Waveform file)");
1817 return SDL_SetError(
"Could not find RIFF or WAVE identifiers (not a Waveform file)");
1833 if (RIFFchunk.
length == 0) {
1853 if (chunkcount++ >= chunkcountlimit) {
1854 return SDL_SetError(
"Chunk count in WAVE file exceeds limit of %u", chunkcountlimit);
1865 }
else if (
result == -2) {
1866 return SDL_SetError(
"Could not seek to WAVE chunk header");
1875 return SDL_SetError(
"fmt chunk after data chunk in WAVE file");
1911 if (RIFFend < chunk->position + chunk->
length) {
1932 return SDL_SetError(
"Missing data chunk in WAVE file");
1941 return SDL_SetError(
"Could not seek to WAVE chunk data");
1955 return SDL_SetError(
"Could not read data of WAVE fmt chunk");
1961 if (chunk->
length < 14) {
1962 return SDL_SetError(
"Invalid WAVE fmt chunk length (too small)");
1963 }
else if (chunk->
size < 14) {
1964 return SDL_SetError(
"Could not read data of WAVE fmt chunk");
1971 #ifdef SDL_WAVE_DEBUG_LOG_FORMAT
1972 WaveDebugLogFormat(file);
1974 #ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
1987 }
else if (
result == -2) {
1988 return SDL_SetError(
"Could not seek data of WAVE data chunk");
1995 return SDL_SetError(
"Could not read data of WAVE data chunk");
2001 switch (
format->encoding) {
2004 if (
PCM_Decode(file, audio_buf, audio_len) < 0) {
2010 if (
LAW_Decode(file, audio_buf, audio_len) < 0) {
2034 switch (
format->encoding) {
2046 switch (
format->bitspersample) {
2065 if (RIFFlengthknown) {
2087 }
else if (audio_buf ==
NULL) {
2090 }
else if (audio_len ==
NULL) {