31 struct MS_ADPCM_decodestate
38 static struct MS_ADPCM_decoder
45 struct MS_ADPCM_decodestate state[2];
62 rogue_feel = (
Uint8 *) format +
sizeof(*format);
63 if (
sizeof(*format) == 16) {
65 rogue_feel +=
sizeof(
Uint16);
67 MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
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;
112 state->iDelta = (
Uint16) delta;
113 state->iSamp2 = state->iSamp1;
114 state->iSamp1 = (
Sint16) new_sample;
121 struct MS_ADPCM_decodestate *state[2];
122 Uint8 *freeable, *encoded, *decoded;
123 Sint32 encoded_len, samplesleft;
124 Sint8 nybble, stereo;
129 encoded_len = *audio_len;
130 encoded = *audio_buf;
131 freeable = *audio_buf;
136 if (*audio_buf ==
NULL) {
139 decoded = *audio_buf;
147 state[0]->hPredictor = *encoded++;
149 state[1]->hPredictor = *encoded++;
151 state[0]->iDelta = ((encoded[1] << 8) | encoded[0]);
152 encoded +=
sizeof(
Sint16);
154 state[1]->iDelta = ((encoded[1] << 8) | encoded[0]);
155 encoded +=
sizeof(
Sint16);
157 state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
158 encoded +=
sizeof(
Sint16);
160 state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
161 encoded +=
sizeof(
Sint16);
163 state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
164 encoded +=
sizeof(
Sint16);
166 state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
167 encoded +=
sizeof(
Sint16);
173 decoded[0] = state[0]->iSamp2 & 0xFF;
174 decoded[1] = state[0]->iSamp2 >> 8;
177 decoded[0] = state[1]->iSamp2 & 0xFF;
178 decoded[1] = state[1]->iSamp2 >> 8;
181 decoded[0] = state[0]->iSamp1 & 0xFF;
182 decoded[1] = state[0]->iSamp1 >> 8;
185 decoded[0] = state[1]->iSamp1 & 0xFF;
186 decoded[1] = state[1]->iSamp1 >> 8;
193 while (samplesleft > 0) {
194 nybble = (*encoded) >> 4;
196 decoded[0] = new_sample & 0xFF;
198 decoded[1] = new_sample & 0xFF;
201 nybble = (*encoded) & 0x0F;
203 decoded[0] = new_sample & 0xFF;
205 decoded[1] = new_sample & 0xFF;
217 struct IMA_ADPCM_decodestate
222 static struct IMA_ADPCM_decoder
227 struct IMA_ADPCM_decodestate state[2];
243 rogue_feel = (
Uint8 *) format +
sizeof(*format);
244 if (
sizeof(*format) == 16) {
246 rogue_feel +=
sizeof(
Uint16);
248 IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
255 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
256 const Sint32 min_audioval = -(1 << (16 - 1));
257 const int index_table[16] = {
263 const Sint32 step_table[89] = {
264 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
265 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
266 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
267 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
268 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
269 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
270 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
271 22385, 24623, 27086, 29794, 32767
276 if (state->index > 88) {
278 }
else if (state->index < 0) {
281 step = step_table[state->index];
286 delta += (step >> 1);
288 delta += (step >> 2);
291 state->sample += delta;
294 state->index += index_table[nybble];
297 if (state->sample > max_audioval) {
298 state->sample = max_audioval;
299 }
else if (state->sample < min_audioval) {
300 state->sample = min_audioval;
302 return (state->sample);
308 int channel,
int numchannels,
309 struct IMA_ADPCM_decodestate *state)
315 decoded += (channel * 2);
316 for (i = 0; i < 4; ++
i) {
317 nybble = (*encoded) & 0x0F;
319 decoded[0] = new_sample & 0xFF;
321 decoded[1] = new_sample & 0xFF;
322 decoded += 2 * numchannels;
324 nybble = (*encoded) >> 4;
326 decoded[0] = new_sample & 0xFF;
328 decoded[1] = new_sample & 0xFF;
329 decoded += 2 * numchannels;
338 struct IMA_ADPCM_decodestate *state;
339 Uint8 *freeable, *encoded, *decoded;
340 Sint32 encoded_len, samplesleft;
341 unsigned int c, channels;
346 SDL_SetError(
"IMA ADPCM decoder can only handle %d channels",
353 encoded_len = *audio_len;
354 encoded = *audio_buf;
355 freeable = *audio_buf;
360 if (*audio_buf ==
NULL) {
363 decoded = *audio_buf;
368 for (c = 0; c < channels; ++
c) {
370 state[
c].sample = ((encoded[1] << 8) | encoded[0]);
372 if (state[c].sample & 0x8000) {
373 state[
c].sample -= 0x10000;
375 state[
c].index = *encoded++;
377 if (*encoded++ != 0) {
382 decoded[0] = (
Uint8) (state[c].sample & 0xFF);
383 decoded[1] = (
Uint8) (state[c].sample >> 8);
389 while (samplesleft > 0) {
390 for (c = 0; c < channels; ++
c) {
392 c, channels, &state[c]);
396 decoded += (channels * 8 * 2);
411 int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded;
435 if (wavelen ==
WAVE) {
442 if ((RIFFchunk !=
RIFF) || (WAVEmagic !=
WAVE)) {
447 headerDiff +=
sizeof(
Uint32);
460 headerDiff += lenread + 2 *
sizeof(
Uint32);
470 IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
476 IEEE_float_encoded = 1;
485 MS_ADPCM_encoded = 1;
493 IMA_ADPCM_encoded = 1;
509 if (IEEE_float_encoded) {
518 if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
557 *audio_len = lenread;
558 *audio_buf = chunk.
data;
560 headerDiff += lenread + 2 *
sizeof(
Uint32);
562 headerDiff += 2 *
sizeof(
Uint32);
564 if (MS_ADPCM_encoded) {
570 if (IMA_ADPCM_encoded) {
579 *audio_len &= ~(samplesize - 1);
static int InitIMA_ADPCM(WaveFMT *format)
else Out of place iCCP chunk
int32_t Sint32
A signed 32-bit integer type.
DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 *audio_buf)
static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded, int channel, int numchannels, struct IMA_ADPCM_decodestate *state)
DECLSPEC void SDLCALL SDL_free(void *mem)
#define SDL_RWread(ctx, ptr, size, n)
static struct IMA_ADPCM_decoder IMA_ADPCM_state
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)
DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src)
#define SDL_RWseek(ctx, offset, whence)
uint32_t Uint32
An unsigned 32-bit integer type.
static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
DECLSPEC int SDLCALL SDL_Error(SDL_errorcode code)
#define SDL_AUDIO_BITSIZE(x)
DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len)
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
static Sint32 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state, Uint8 nybble)
#define SDL_OutOfMemory()
#define SDL_arraysize(array)
uint8_t Uint8
An unsigned 8-bit integer type.
static int InitMS_ADPCM(WaveFMT *format)
int16_t Sint16
A signed 16-bit integer type.
DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
uint16_t Uint16
An unsigned 16-bit integer type.
int8_t Sint8
A signed 8-bit integer type.
static int ReadChunk(SDL_RWops *src, Chunk *chunk)
static struct MS_ADPCM_decoder MS_ADPCM_state