21 #include "../SDL_internal.h" 62 rogue_feel = (
Uint8 *) format +
sizeof(*format);
63 if (
sizeof(*format) == 16) {
65 rogue_feel +=
sizeof(
Uint16);
68 rogue_feel +=
sizeof(
Uint16);
70 rogue_feel +=
sizeof(
Uint16);
77 rogue_feel +=
sizeof(
Uint16);
79 rogue_feel +=
sizeof(
Uint16);
88 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
89 const Sint32 min_audioval = -(1 << (16 - 1));
90 const Sint32 adaptive[] = {
91 230, 230, 230, 230, 307, 409, 512, 614,
92 768, 614, 512, 409, 307, 230, 230, 230
96 new_sample = ((state->
iSamp1 * coeff[0]) +
97 (state->
iSamp2 * coeff[1])) / 256;
99 new_sample += state->
iDelta * (nybble - 0x10);
101 new_sample += state->
iDelta * nybble;
103 if (new_sample < min_audioval) {
104 new_sample = min_audioval;
105 }
else if (new_sample > max_audioval) {
106 new_sample = max_audioval;
108 delta = ((
Sint32) state->
iDelta * adaptive[nybble]) / 256;
122 Uint8 *freeable, *encoded, *encoded_end, *decoded, *decoded_end;
123 Sint32 encoded_len, samplesleft;
130 encoded_len = *audio_len;
131 encoded = *audio_buf;
132 encoded_end = encoded + encoded_len;
133 freeable = *audio_buf;
138 if (*audio_buf ==
NULL) {
141 decoded = *audio_buf;
142 decoded_end = decoded + *audio_len;
150 if (encoded + 7 + (stereo ? 7 : 0) > encoded_end)
goto invalid_size;
156 goto invalid_predictor;
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 if (decoded + 4 + (stereo ? 4 : 0) > decoded_end)
goto invalid_size;
181 decoded[0] = state[0]->
iSamp2 & 0xFF;
182 decoded[1] = state[0]->
iSamp2 >> 8;
185 decoded[0] = state[1]->
iSamp2 & 0xFF;
186 decoded[1] = state[1]->
iSamp2 >> 8;
189 decoded[0] = state[0]->
iSamp1 & 0xFF;
190 decoded[1] = state[0]->
iSamp1 >> 8;
193 decoded[0] = state[1]->
iSamp1 & 0xFF;
194 decoded[1] = state[1]->
iSamp1 >> 8;
201 while (samplesleft > 0) {
202 if (encoded + 1 > encoded_end)
goto invalid_size;
203 if (decoded + 4 > decoded_end)
goto invalid_size;
205 nybble = (*encoded) >> 4;
207 decoded[0] = new_sample & 0xFF;
209 decoded[1] = new_sample & 0xFF;
212 nybble = (*encoded) & 0x0F;
214 decoded[0] = new_sample & 0xFF;
216 decoded[1] = new_sample & 0xFF;
227 SDL_SetError(
"Unexpected chunk length for a MS ADPCM decoder");
231 SDL_SetError(
"Invalid predictor value for a MS ADPCM decoder");
252 Uint8 *rogue_feel, *rogue_feel_end;
255 if (length <
sizeof(*format))
goto too_short;
263 rogue_feel = (
Uint8 *) format +
sizeof(*format);
264 rogue_feel_end = (
Uint8 *)format + length;
265 if (
sizeof(*format) == 16) {
267 rogue_feel +=
sizeof(
Uint16);
269 if (rogue_feel + 2 > rogue_feel_end)
goto too_short;
273 SDL_SetError(
"Unexpected length of a chunk with an IMA ADPCM format");
280 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
281 const Sint32 min_audioval = -(1 << (16 - 1));
282 const int index_table[16] = {
288 const Sint32 step_table[89] = {
289 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
290 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
291 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
292 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
293 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
294 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
295 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
296 22385, 24623, 27086, 29794, 32767
301 if (state->
index > 88) {
303 }
else if (state->
index < 0) {
307 step = step_table[(int)state->
index];
312 delta += (step >> 1);
314 delta += (step >> 2);
320 state->
index += index_table[nybble];
323 if (state->
sample > max_audioval) {
324 state->
sample = max_audioval;
325 }
else if (state->
sample < min_audioval) {
326 state->
sample = min_audioval;
334 int channel,
int numchannels,
341 decoded += (channel * 2);
342 for (i = 0; i < 4; ++
i) {
343 nybble = (*encoded) & 0x0F;
345 decoded[0] = new_sample & 0xFF;
347 decoded[1] = new_sample & 0xFF;
348 decoded += 2 * numchannels;
350 nybble = (*encoded) >> 4;
352 decoded[0] = new_sample & 0xFF;
354 decoded[1] = new_sample & 0xFF;
355 decoded += 2 * numchannels;
365 Uint8 *freeable, *encoded, *decoded;
366 Sint32 encoded_len, samplesleft;
367 unsigned int c, channels;
372 SDL_SetError(
"IMA ADPCM decoder can only handle %u channels",
379 encoded_len = *audio_len;
380 encoded = *audio_buf;
381 freeable = *audio_buf;
386 if (*audio_buf ==
NULL) {
389 decoded = *audio_buf;
394 for (c = 0; c < channels; ++
c) {
396 state[
c].
sample = ((encoded[1] << 8) | encoded[0]);
398 if (state[c].
sample & 0x8000) {
401 state[
c].
index = *encoded++;
403 if (*encoded++ != 0) {
415 while (samplesleft > 0) {
416 for (c = 0; c < channels; ++
c) {
418 c, channels, &state[c]);
422 decoded += (channels * 8 * 2);
437 int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded;
461 if (wavelen ==
WAVE) {
468 if ((RIFFchunk !=
RIFF) || (WAVEmagic !=
WAVE)) {
473 headerDiff +=
sizeof(
Uint32);
486 headerDiff += lenread + 2 *
sizeof(
Uint32);
496 IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
502 IEEE_float_encoded = 1;
511 MS_ADPCM_encoded = 1;
519 IMA_ADPCM_encoded = 1;
534 if (IEEE_float_encoded) {
543 if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
582 *audio_len = lenread;
583 *audio_buf = chunk.
data;
585 headerDiff += lenread + 2 *
sizeof(
Uint32);
587 headerDiff += 2 *
sizeof(
Uint32);
589 if (MS_ADPCM_encoded) {
595 if (IMA_ADPCM_encoded) {
604 *audio_len &= ~(samplesize - 1);
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded, int channel, int numchannels, struct IMA_ADPCM_decodestate *state)
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
uint32_t Uint32
An unsigned 32-bit integer type.
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)
int8_t Sint8
A signed 8-bit integer type.
static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
uint8_t Uint8
An unsigned 8-bit integer type.
#define SDL_AUDIO_BITSIZE(x)
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()
static int InitMS_ADPCM(WaveFMT *format)
uint16_t Uint16
An unsigned 16-bit integer type.
#define SDL_arraysize(array)
GLuint GLsizei GLsizei * length
static int ReadChunk(SDL_RWops *src, Chunk *chunk)
int16_t Sint16
A signed 16-bit integer type.
static struct MS_ADPCM_decoder MS_ADPCM_state