21 #include "../SDL_internal.h" 50 Uint8 *rogue_feel, *rogue_feel_end;
54 if (length <
sizeof(*format))
goto too_short;
62 rogue_feel = (
Uint8 *) format +
sizeof(*format);
63 rogue_feel_end = (
Uint8 *)format + length;
64 if (
sizeof(*format) == 16) {
66 rogue_feel +=
sizeof(
Uint16);
68 if (rogue_feel + 4 > rogue_feel_end)
goto too_short;
70 rogue_feel +=
sizeof(
Uint16);
72 rogue_feel +=
sizeof(
Uint16);
78 if (rogue_feel + 4 > rogue_feel_end)
goto too_short;
80 rogue_feel +=
sizeof(
Uint16);
82 rogue_feel +=
sizeof(
Uint16);
86 SDL_SetError(
"Unexpected length of a chunk with a MS ADPCM format");
94 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
95 const Sint32 min_audioval = -(1 << (16 - 1));
96 const Sint32 adaptive[] = {
97 230, 230, 230, 230, 307, 409, 512, 614,
98 768, 614, 512, 409, 307, 230, 230, 230
102 new_sample = ((state->
iSamp1 * coeff[0]) +
103 (state->
iSamp2 * coeff[1])) / 256;
105 new_sample += state->
iDelta * (nybble - 0x10);
107 new_sample += state->
iDelta * nybble;
109 if (new_sample < min_audioval) {
110 new_sample = min_audioval;
111 }
else if (new_sample > max_audioval) {
112 new_sample = max_audioval;
114 delta = ((
Sint32) state->
iDelta * adaptive[nybble]) / 256;
128 Uint8 *freeable, *encoded, *decoded;
129 Sint32 encoded_len, samplesleft;
136 encoded_len = *audio_len;
137 encoded = *audio_buf;
138 freeable = *audio_buf;
143 if (*audio_buf ==
NULL) {
146 decoded = *audio_buf;
158 state[0]->
iDelta = ((encoded[1] << 8) | encoded[0]);
159 encoded +=
sizeof(
Sint16);
161 state[1]->
iDelta = ((encoded[1] << 8) | encoded[0]);
162 encoded +=
sizeof(
Sint16);
164 state[0]->
iSamp1 = ((encoded[1] << 8) | encoded[0]);
165 encoded +=
sizeof(
Sint16);
167 state[1]->
iSamp1 = ((encoded[1] << 8) | encoded[0]);
168 encoded +=
sizeof(
Sint16);
170 state[0]->
iSamp2 = ((encoded[1] << 8) | encoded[0]);
171 encoded +=
sizeof(
Sint16);
173 state[1]->
iSamp2 = ((encoded[1] << 8) | encoded[0]);
174 encoded +=
sizeof(
Sint16);
180 decoded[0] = state[0]->
iSamp2 & 0xFF;
181 decoded[1] = state[0]->
iSamp2 >> 8;
184 decoded[0] = state[1]->
iSamp2 & 0xFF;
185 decoded[1] = state[1]->
iSamp2 >> 8;
188 decoded[0] = state[0]->
iSamp1 & 0xFF;
189 decoded[1] = state[0]->
iSamp1 >> 8;
192 decoded[0] = state[1]->
iSamp1 & 0xFF;
193 decoded[1] = state[1]->
iSamp1 >> 8;
200 while (samplesleft > 0) {
201 nybble = (*encoded) >> 4;
203 decoded[0] = new_sample & 0xFF;
205 decoded[1] = new_sample & 0xFF;
208 nybble = (*encoded) & 0x0F;
210 decoded[0] = new_sample & 0xFF;
212 decoded[1] = new_sample & 0xFF;
240 Uint8 *rogue_feel, *rogue_feel_end;
250 rogue_feel = (
Uint8 *) format +
sizeof(*format);
251 rogue_feel_end = (
Uint8 *) format + length;
252 if (
sizeof(*format) == 16) {
254 rogue_feel +=
sizeof(
Uint16);
256 if (rogue_feel + 2 > rogue_feel_end)
goto too_short;
260 SDL_SetError(
"Unexpected length of a chunk with an IMA ADPCM format");
267 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
268 const Sint32 min_audioval = -(1 << (16 - 1));
269 const int index_table[16] = {
275 const Sint32 step_table[89] = {
276 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
277 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
278 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
279 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
280 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
281 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
282 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
283 22385, 24623, 27086, 29794, 32767
288 if (state->
index > 88) {
290 }
else if (state->
index < 0) {
294 step = step_table[(int)state->
index];
299 delta += (step >> 1);
301 delta += (step >> 2);
307 state->
index += index_table[nybble];
310 if (state->
sample > max_audioval) {
311 state->
sample = max_audioval;
312 }
else if (state->
sample < min_audioval) {
313 state->
sample = min_audioval;
321 int channel,
int numchannels,
328 decoded += (channel * 2);
329 for (i = 0; i < 4; ++
i) {
330 nybble = (*encoded) & 0x0F;
332 decoded[0] = new_sample & 0xFF;
334 decoded[1] = new_sample & 0xFF;
335 decoded += 2 * numchannels;
337 nybble = (*encoded) >> 4;
339 decoded[0] = new_sample & 0xFF;
341 decoded[1] = new_sample & 0xFF;
342 decoded += 2 * numchannels;
352 Uint8 *freeable, *encoded, *decoded;
353 Sint32 encoded_len, samplesleft;
354 unsigned int c, channels;
359 SDL_SetError(
"IMA ADPCM decoder can only handle %u channels",
366 encoded_len = *audio_len;
367 encoded = *audio_buf;
368 freeable = *audio_buf;
373 if (*audio_buf ==
NULL) {
376 decoded = *audio_buf;
381 for (c = 0; c < channels; ++
c) {
383 state[
c].
sample = ((encoded[1] << 8) | encoded[0]);
385 if (state[c].
sample & 0x8000) {
388 state[
c].
index = *encoded++;
390 if (*encoded++ != 0) {
402 while (samplesleft > 0) {
403 for (c = 0; c < channels; ++
c) {
405 c, channels, &state[c]);
409 decoded += (channels * 8 * 2);
421 const double DIVBY8388608 = 0.00000011920928955078125;
422 const Uint32 original_len = *audio_len;
435 *audio_len = expanded_len;
438 src = (ptr + original_len) - 3;
439 dst = ((
Uint32 *) (ptr + expanded_len)) - 1;
440 for (i = 0; i <
samples; i++) {
443 (((
Uint32) src[1]) << 16) |
444 (((
Uint32) src[0]) << 8) )) >> 8;
445 const double scaled = (((double) converted) * DIVBY8388608);
447 *(dst--) = (
Sint32) (scaled * 2147483647.0);
455 static const Uint8 extensible_pcm_guid[16] = { 1, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 };
456 static const Uint8 extensible_ieee_guid[16] = { 3, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 };
465 int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded;
490 if (wavelen ==
WAVE) {
497 if ((RIFFchunk !=
RIFF) || (WAVEmagic !=
WAVE)) {
502 headerDiff +=
sizeof(
Uint32);
515 headerDiff += lenread + 2 *
sizeof(
Uint32);
525 IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
531 IEEE_float_encoded = 1;
540 MS_ADPCM_encoded = 1;
548 IMA_ADPCM_encoded = 1;
564 IEEE_float_encoded = 1;
581 if (IEEE_float_encoded) {
590 if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
632 *audio_len = lenread;
633 *audio_buf = chunk.
data;
635 headerDiff += lenread + 2 *
sizeof(
Uint32);
637 headerDiff += 2 *
sizeof(
Uint32);
639 if (MS_ADPCM_encoded) {
645 if (IMA_ADPCM_encoded) {
661 *audio_len &= ~(samplesize - 1);
static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded, int channel, int numchannels, struct IMA_ADPCM_decodestate *state)
static int InitMS_ADPCM(WaveFMT *format, int length)
void SDL_FreeWAV(Uint8 *audio_buf)
#define SDL_RWread(ctx, ptr, size, n)
static int InitIMA_ADPCM(WaveFMT *format, int length)
static struct IMA_ADPCM_decoder IMA_ADPCM_state
SDL_AudioSpec * SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
struct MS_ADPCM_decodestate state[2]
static Sint32 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state, Uint8 nybble, Sint16 *coeff)
static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
#define SDL_RWseek(ctx, offset, whence)
static const Uint8 extensible_ieee_guid[16]
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
#define SDL_AUDIO_BITSIZE(x)
static int ConvertSint24ToSint32(Uint8 **audio_buf, Uint32 *audio_len)
static const Uint8 extensible_pcm_guid[16]
struct IMA_ADPCM_decodestate state[2]
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
static Sint32 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state, Uint8 nybble)
#define SDL_OutOfMemory()
#define SDL_arraysize(array)
GLuint GLsizei GLsizei * length
static int ReadChunk(SDL_RWops *src, Chunk *chunk)
static struct MS_ADPCM_decoder MS_ADPCM_state