summarylogtreecommitdiffstats
path: root/sdl_sound-ima-adpcm.patch
blob: bf6ba73838101472539f1fac479e4fff528718cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
diff -r 719dade41745 decoders/wav.c
--- a/decoders/wav.c	Wed Aug 15 23:52:18 2012 -0400
+++ b/decoders/wav.c	Fri Sep 12 06:50:35 2014 +0200
@@ -113,8 +113,9 @@
 
 #define fmtID  0x20746D66  /* "fmt ", in ascii. */
 
-#define FMT_NORMAL 0x0001    /* Uncompressed waveform data.     */
-#define FMT_ADPCM  0x0002    /* ADPCM compressed waveform data. */
+#define FMT_NORMAL 0x0001    /* Uncompressed waveform data.         */
+#define FMT_ADPCM  0x0002    /* ADPCM compressed waveform data.     */
+#define FMT_IMA    0x0011    /* IMA ADPCM compressed waveform data. */
 
 typedef struct
 {
@@ -130,6 +131,12 @@
     Sint16 iSamp2;
 } ADPCMBLOCKHEADER;
 
+typedef struct
+{
+    Sint16 iPrevSamp;
+    Uint8 iStepIndex;
+} IMAADPCMDATA;
+
 typedef struct S_WAV_FMT_T
 {
     Uint32 chunkID;
@@ -166,6 +173,25 @@
             Sint8 nibble;
         } adpcm;
 
+        struct
+        {
+            /* per channel decoder state */
+            IMAADPCMDATA *d;
+            /* auxiliary buffer */
+            Uint8 *buf;
+
+            Uint16 block_frames;
+            Uint16 block_framesets;
+            Uint16 enc_frameset_size;
+            Uint16 headerset_size;
+            Uint16 dec_frame_size;
+            Uint16 dec_frameset_size;
+            Uint16 rem_block_framesets;
+
+            /* whether the next word(s) are the start of a new block */
+            int read_header;
+        } ima;
+
         /* put other format-specific data here... */
     } fmt;
 } fmt_t;
@@ -614,6 +640,296 @@
 
 
 /*****************************************************************************
+ * IMA ADPCM compression handler...                                          *
+ *****************************************************************************/
+
+static const int ima_index_table[16] =
+{
+    -1, -1, -1, -1, 2, 4, 6, 8,
+    -1, -1, -1, -1, 2, 4, 6, 8
+};
+
+static const int ima_step_table[89] =
+{
+    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
+    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
+    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
+    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
+    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
+    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
+    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
+    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
+    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
+};
+
+/* 1 frameset = 4 bytes (per channel) = 8 nibbles = 8 frames */
+#define FRAMESET_FRAMES 8
+
+
+static int read_ima_block_headers(SDL_RWops *rw, IMAADPCMDATA *d,
+                                  Uint16 chan_count, Sint16 *out)
+{
+    Uint16 i;
+    Uint8 dummy;
+
+    for (i = 0; i < chan_count; i++)
+    {
+        BAIL_IF_MACRO(!read_le16(rw, &d[i].iPrevSamp), NULL, 0);
+        BAIL_IF_MACRO(!read_uint8(rw, &d[i].iStepIndex), NULL, 0);
+        BAIL_IF_MACRO(!read_uint8(rw, &dummy), NULL, 0);
+
+        out[i] = d[i].iPrevSamp;
+    } /* for */
+
+    return(1);
+} /* read_ima_block_headers */
+
+
+static Sint16 decode_ima_nibble(Uint8 nibble, IMAADPCMDATA *state)
+{
+    int step = ima_step_table[state->iStepIndex];
+    int diff = 0;
+    int samp, index;
+
+    if (nibble & 0x4)
+        diff += step >> 0;
+    if (nibble & 0x2)
+        diff += step >> 1;
+    if (nibble & 0x1)
+        diff += step >> 2;
+
+    diff += step >> 3;
+
+    if (nibble & 0x8)
+        diff = -diff;
+
+    samp = state->iPrevSamp + diff;
+    samp = SDL_max(SDL_min(samp, 32767), -32768);
+    state->iPrevSamp = samp;
+
+    index = state->iStepIndex + ima_index_table[nibble];
+    state->iStepIndex = SDL_max(SDL_min(index, 88), 0);
+
+    return samp;
+} /* decode_ima_nibble */
+
+
+static int read_ima_frameset(SDL_RWops *rw, wav_t *wav, Sint16 *out)
+{
+    fmt_t *fmt = wav->fmt;
+    Uint16 i, j;
+    Uint8 *fs_buf = fmt->fmt.ima.buf;
+    Uint32 fs_buf_size = fmt->fmt.ima.enc_frameset_size;
+    Uint16 chan_count = fmt->wChannels;
+    IMAADPCMDATA *data = fmt->fmt.ima.d;
+
+    /* read encoded frameset into temp buffer */
+    BAIL_IF_MACRO(!SDL_RWread(rw, fs_buf, fs_buf_size, 1), NULL, 0);
+
+    for (i = 0; i < chan_count; i++)
+    {
+        for (j = 0; j < 4; j++)
+        {
+            Uint8 byte = fs_buf[i*4+j];
+            Sint16 *_out = out + j*chan_count*2+i;
+
+            /* low nibble */
+            *_out = decode_ima_nibble(byte & 0xF, &data[i]);
+
+            _out += chan_count;
+
+            /* high nibble */
+            *_out = decode_ima_nibble(byte >> 4, &data[i]);
+        }
+    } /* for */
+
+    return(1);
+} /* read_ima_frameset */
+
+
+static Uint32 read_sample_fmt_ima(Sound_Sample *sample)
+{
+    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
+    wav_t *w = (wav_t *) internal->decoder_private;
+    fmt_t *fmt = w->fmt;
+    void *const out_buf = internal->buffer;
+    Uint32 bw = 0;
+    int rc;
+
+    while (1)
+    {
+        if (fmt->fmt.ima.read_header)
+        {
+            if (w->bytesLeft < fmt->fmt.ima.headerset_size)
+            {
+                sample->flags |= SOUND_SAMPLEFLAG_EOF;
+                break;
+            } /* if */
+
+            if (bw+fmt->fmt.ima.dec_frame_size > internal->buffer_size)
+                break;
+
+            rc = read_ima_block_headers(internal->rw, fmt->fmt.ima.d,
+                                        fmt->wChannels, (Sint16*) (out_buf+bw));
+                                        
+            if (!rc)
+            {
+                sample->flags |= SOUND_SAMPLEFLAG_ERROR;
+                return 0;
+            } /* if */
+
+            w->bytesLeft -= fmt->fmt.ima.headerset_size;
+            bw += fmt->fmt.ima.dec_frame_size;
+
+            fmt->fmt.ima.read_header = 0;
+            fmt->fmt.ima.rem_block_framesets = fmt->fmt.ima.block_framesets;
+        } /* if */
+
+        if (w->bytesLeft < fmt->fmt.ima.enc_frameset_size)
+        {
+            sample->flags |= SOUND_SAMPLEFLAG_EOF;
+            break;
+        } /* if */
+
+        if (bw+fmt->fmt.ima.dec_frameset_size > internal->buffer_size)
+            break;
+
+        rc = read_ima_frameset(internal->rw, w, (Sint16*) (out_buf+bw));
+
+        if (!rc)
+        {
+            sample->flags |= SOUND_SAMPLEFLAG_ERROR;
+            return 0;
+        } /* if */
+
+        bw += fmt->fmt.ima.dec_frameset_size;
+        w->bytesLeft -= fmt->fmt.ima.enc_frameset_size;
+
+        if (--fmt->fmt.ima.rem_block_framesets == 0)
+            fmt->fmt.ima.read_header = 1;
+    } /* while */
+
+    return(bw);
+} /* read_sample_fmt_ima */
+
+
+static void free_fmt_ima(fmt_t *fmt)
+{
+    free(fmt->fmt.ima.d);
+    free(fmt->fmt.ima.buf);
+} /* free_fmt_ima */
+
+
+static int rewind_sample_fmt_ima(Sound_Sample *sample)
+{
+    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
+    wav_t *w = (wav_t *) internal->decoder_private;
+    fmt_t *fmt = w->fmt;
+
+    fmt->fmt.ima.read_header = 1;
+
+    return(1);
+} /* rewind_sample_fmt_ima */
+
+
+static int seek_sample_fmt_ima(Sound_Sample *sample, Uint32 ms)
+{
+    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
+    wav_t *w = (wav_t *) internal->decoder_private;
+    fmt_t *fmt = w->fmt;
+    Sint16 *dummy_buf = (Sint16*) (fmt->fmt.ima.buf +
+                                   fmt->fmt.ima.enc_frameset_size);
+    Uint32 i;
+    int rc, pos;
+
+    Uint32 seek_frames = (fmt->dwSamplesPerSec * ms) / 1000;
+    Uint32 seek_blocks = seek_frames / fmt->fmt.ima.block_frames;
+    Uint32 seek_framesets;
+    seek_frames %= fmt->fmt.ima.block_frames;
+
+    w->bytesLeft = fmt->total_bytes;
+    pos = seek_blocks * fmt->wBlockAlign + fmt->data_starting_offset;
+    rc = SDL_RWseek(internal->rw, pos, SEEK_SET);
+    BAIL_IF_MACRO(rc != pos, ERR_IO_ERROR, 0);
+    w->bytesLeft -= seek_blocks * fmt->wBlockAlign;
+
+    fmt->fmt.ima.read_header = 0;
+
+    if (seek_frames == 0)
+    {
+        fmt->fmt.ima.read_header = 1;
+        return(1);
+    } /* if */
+
+    rc = read_ima_block_headers(internal->rw, fmt->fmt.ima.d,
+                                fmt->wChannels, dummy_buf);
+    BAIL_IF_MACRO(!rc, NULL, 0);
+    w->bytesLeft -= fmt->fmt.ima.headerset_size;
+
+    if (w->bytesLeft < fmt->fmt.ima.headerset_size)
+    {
+        sample->flags |= SOUND_SAMPLEFLAG_EOF;
+        return(1);
+    } /* if */
+
+    seek_frames -= 1;
+    seek_framesets = seek_frames / FRAMESET_FRAMES;
+
+    for (i = 0; i < seek_framesets; i++)
+    {
+        rc = read_ima_frameset(internal->rw, w, dummy_buf);
+        BAIL_IF_MACRO(!rc, NULL, 0);
+        w->bytesLeft -= fmt->fmt.ima.enc_frameset_size;
+    } /* for */
+
+    fmt->fmt.ima.rem_block_framesets =
+        fmt->fmt.ima.block_framesets - seek_framesets;
+
+    if (w->bytesLeft < fmt->fmt.ima.enc_frameset_size)
+        sample->flags |= SOUND_SAMPLEFLAG_EOF;
+
+    return(1);  /* success. */
+} /* seek_sample_fmt_ima */
+
+
+static int read_fmt_ima(SDL_RWops *rw, fmt_t *fmt)
+{
+    Uint16 chan = fmt->wChannels;
+    Sint16 extraBytes;
+    int rc;
+
+    /* setup function pointers */
+    fmt->free = free_fmt_ima;
+    fmt->read_sample = read_sample_fmt_ima;
+    fmt->rewind_sample = rewind_sample_fmt_ima;
+    fmt->seek_sample = seek_sample_fmt_ima;
+
+    BAIL_IF_MACRO(!read_le16(rw, &extraBytes), NULL, 0);
+    BAIL_IF_MACRO(!read_le16(rw, &fmt->fmt.ima.block_frames), NULL, 0);
+
+    /* skip to end of fmt chunk */
+    rc = SDL_RWseek(rw, extraBytes-2, SEEK_CUR);
+    BAIL_IF_MACRO(!rc, NULL, 0);
+
+    fmt->fmt.ima.block_framesets = (fmt->fmt.ima.block_frames-1) / FRAMESET_FRAMES;
+    fmt->fmt.ima.enc_frameset_size = 4 * chan;
+    fmt->fmt.ima.headerset_size = 4 * chan;
+    fmt->fmt.ima.dec_frame_size = sizeof(Uint16) * chan;
+    fmt->fmt.ima.dec_frameset_size = fmt->fmt.ima.dec_frame_size * FRAMESET_FRAMES;
+    fmt->fmt.ima.read_header = 1;
+
+    fmt->fmt.ima.d = malloc(sizeof(IMAADPCMDATA) * chan);
+
+    size_t buf_size = fmt->fmt.ima.enc_frameset_size +
+                      fmt->fmt.ima.dec_frameset_size;
+    fmt->fmt.ima.buf = malloc(buf_size);
+
+    return(1);
+} /* read_fmt_ima */
+
+
+
+/*****************************************************************************
  * Everything else...                                                        *
  *****************************************************************************/
 
@@ -642,6 +958,13 @@
             SNDDBG(("WAV: Appears to be ADPCM compressed audio.\n"));
             return(read_fmt_adpcm(rw, fmt));
 
+        case FMT_IMA:
+            if (fmt->wBitsPerSample == 4)
+            {
+                SNDDBG(("WAV: Appears to be 4bit IMA ADPCM compressed audio.\n"));
+                return(read_fmt_ima(rw, fmt));
+            }
+
         /* add other types here. */
 
         default: