forked from dbry/adpcm-xq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadpcm-lib.c
575 lines (446 loc) · 18 KB
/
adpcm-lib.c
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
////////////////////////////////////////////////////////////////////////////
// **** ADPCM-XQ **** //
// Xtreme Quality ADPCM Encoder/Decoder //
// Copyright (c) 2022 David Bryant. //
// All Rights Reserved. //
// Distributed under the BSD Software License (see license.txt) //
////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <string.h>
#include "adpcm-lib.h"
/* This module encodes and decodes 4-bit ADPCM (DVI/IMA varient). ADPCM data is divided
* into independently decodable blocks that can be relatively small. The most common
* configuration is to store 505 samples into a 256 byte block, although other sizes are
* permitted as long as the number of samples is one greater than a multiple of 8. When
* multiple channels are present, they are interleaved in the data with an 8-sample
* interval.
*/
/********************************* 4-bit ADPCM encoder ********************************/
#define CLIP(data, min, max) \
if ((data) > (max)) data = max; \
else if ((data) < (min)) data = min;
/* step table */
static const uint16_t 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
};
/* step index tables */
static const int index_table[] = {
/* adpcm data size is 4 */
-1, -1, -1, -1, 2, 4, 6, 8
};
static const int sb4_index_table[8] = {-1, 0, 0, 0, 0, 1, 1, 1};
static const int32_t sb4_step_table[4] = {0x100, 0x200, 0x400, 0x800};
struct adpcm_channel {
int32_t pcmdata; // current PCM value
int32_t error, weight, history [2]; // for noise shaping
int8_t index; // current index into step size table
uint8_t (*encode_sample) (struct adpcm_channel *, int32_t);
void (*decode_sample) (struct adpcm_channel *, int);
};
struct adpcm_context {
struct adpcm_channel channels [2];
int num_channels, lookahead, noise_shaping;
void (*init_block) (struct adpcm_context *, uint8_t *, const int32_t , const int8_t );
};
static void init_ima_block (struct adpcm_context *pcnxt, uint8_t *outbuf, const int32_t init_pcmdata, const int8_t init_index);
static void decode_ima_sample (struct adpcm_channel *pchan, int nibble);
static uint8_t encode_ima_sample (struct adpcm_channel *pchan, int32_t csample);
static void init_sb4_block (struct adpcm_context *pcnxt, uint8_t *outbuf, const int32_t init_pcmdata, const int8_t init_index);
static void decode_sb4_sample (struct adpcm_channel *pchan, int code);
static uint8_t encode_sb4_sample (struct adpcm_channel *pchan, int32_t csample);
/* Create ADPCM encoder context with given number of channels.
* The returned pointer is used for subsequent calls. Note that
* even though an ADPCM encoder could be set up to encode frames
* independently, we use a context so that we can use previous
* data to improve quality (this encoder might not be optimal
* for encoding independent frames).
*/
void *adpcm_create_ima_context (int num_channels, int lookahead, int noise_shaping, int32_t initial_deltas [2])
{
struct adpcm_context *pcnxt = malloc (sizeof (struct adpcm_context));
int ch, i;
memset (pcnxt, 0, sizeof (struct adpcm_context));
pcnxt->noise_shaping = noise_shaping;
pcnxt->num_channels = num_channels;
pcnxt->lookahead = lookahead;
pcnxt->init_block = init_ima_block;
// given the supplied initial deltas, search for and store the closest index
for (ch = 0; ch < num_channels; ++ch)
{
pcnxt->channels[ch].encode_sample = encode_ima_sample;
pcnxt->channels[ch].decode_sample = decode_ima_sample;
for (i = 0; i <= 88; i++)
if (i == 88 || initial_deltas [ch] < ((int32_t) step_table [i] + step_table [i+1]) / 2) {
pcnxt->channels [ch].index = i;
break;
}
}
return pcnxt;
}
void *adpcm_create_sb4_context (int num_channels, int lookahead, int noise_shaping, int32_t initial_deltas [2])
{
struct adpcm_context *pcnxt = malloc (sizeof (struct adpcm_context));
int ch, i;
memset (pcnxt, 0, sizeof (struct adpcm_context));
pcnxt->noise_shaping = noise_shaping;
pcnxt->num_channels = num_channels;
pcnxt->lookahead = lookahead;
pcnxt->init_block = init_sb4_block;
// given the supplied initial deltas, search for and store the closest index
for (ch = 0; ch < num_channels; ++ch)
{
pcnxt->channels[ch].encode_sample = encode_sb4_sample;
pcnxt->channels[ch].decode_sample = decode_sb4_sample;
for (i = 0; i <= 2; i++)
if (i == 2 || initial_deltas [ch] < ((int32_t) sb4_step_table [i] + sb4_step_table [i+1]) / 2) {
pcnxt->channels [ch].index = i;
break;
}
}
return pcnxt;
}
/* Free the ADPCM encoder context.
*/
void adpcm_free_context (void *p)
{
struct adpcm_context *pcnxt = (struct adpcm_context *) p;
free (pcnxt);
}
static void set_decode_parameters (struct adpcm_context *pcnxt, int32_t *init_pcmdata, int8_t *init_index)
{
int ch;
for (ch = 0; ch < pcnxt->num_channels; ch++) {
pcnxt->channels[ch].pcmdata = init_pcmdata[ch];
pcnxt->channels[ch].index = init_index[ch];
}
}
static void get_decode_parameters (struct adpcm_context *pcnxt, int32_t *init_pcmdata, int8_t *init_index)
{
int ch;
for (ch = 0; ch < pcnxt->num_channels; ch++) {
init_pcmdata[ch] = pcnxt->channels[ch].pcmdata;
init_index[ch] = pcnxt->channels[ch].index;
}
}
static double minimum_error (const struct adpcm_channel *pchan, int nch, int32_t csample, const int16_t *sample, int depth, int *best_nibble)
{
struct adpcm_channel chan = *pchan;
int nibble, nibble2;
double min_error;
nibble = chan.encode_sample(&chan, csample);
if (best_nibble) *best_nibble = nibble;
min_error = (double) (chan.pcmdata - csample) * (chan.pcmdata - csample);
if (depth)
min_error += minimum_error (&chan, nch, sample [nch], sample + nch, depth - 1, NULL);
else
return min_error;
for (nibble2 = 0; nibble2 <= 0xF; ++nibble2) {
double error;
if (nibble2 == nibble)
continue;
chan = *pchan;
chan.decode_sample(&chan, nibble2);
error = (double) (chan.pcmdata - csample) * (chan.pcmdata - csample);
if (error < min_error) {
error += minimum_error (&chan, nch, sample [nch], sample + nch, depth - 1, NULL);
if (error < min_error) {
if (best_nibble) *best_nibble = nibble2;
min_error = error;
}
}
}
return min_error;
}
static void init_ima_block (struct adpcm_context *pcnxt, uint8_t *outbuf, const int32_t init_pcmdata, const int8_t init_index)
{
outbuf[0] = init_pcmdata;
outbuf[1] = init_pcmdata >> 8;
outbuf[2] = init_index;
outbuf[3] = 0;
}
static void decode_ima_sample (struct adpcm_channel *pchan, int nibble)
{
uint16_t step = step_table[pchan->index];
uint16_t trial_delta = (step >> 3);
if (nibble & 1) trial_delta += (step >> 2);
if (nibble & 2) trial_delta += (step >> 1);
if (nibble & 4) trial_delta += step;
if (nibble & 8)
pchan->pcmdata -= trial_delta;
else
pchan->pcmdata += trial_delta;
pchan->index += index_table[nibble & 0x07];
CLIP(pchan->index, 0, 88);
CLIP(pchan->pcmdata, -32768, 32767);
}
static uint8_t encode_ima_sample (struct adpcm_channel *pchan, int32_t csample)
{
int32_t delta = csample - pchan->pcmdata;
uint16_t step = step_table[pchan->index];
int nibble;
if (delta < 0) {
int mag = (-delta << 2) / step;
nibble = 0x8 | (mag > 7 ? 7 : mag);
}
else {
int mag = (delta << 2) / step;
nibble = mag > 7 ? 7 : mag;
}
decode_ima_sample(pchan, nibble);
return nibble;
}
static void init_sb4_block (struct adpcm_context *pcnxt, uint8_t *outbuf, const int32_t init_pcmdata, const int8_t init_index)
{
outbuf[0] = (init_pcmdata + 32768)/256; // convert signed 16-bit sample to unsigned 8-bit
outbuf[1] = 0;
outbuf[2] = init_index;
outbuf[3] = 0;
}
static void decode_sb4_sample (struct adpcm_channel *pchan, int code)
{
int sample;
int32_t delta, diff;
delta = sb4_step_table[pchan->index];
diff = (code & 0x07) * delta;
if (code & 0x08) {
diff = -diff;
}
sample = pchan->pcmdata + diff;
CLIP(sample, -32768, 32767);
pchan->index += sb4_index_table[code & 7];
CLIP(pchan->index, 0, 3);
pchan->pcmdata = sample;
}
static uint8_t encode_sb4_sample (struct adpcm_channel *pchan, int32_t csample)
{
int32_t delta;
int sign, code;
sign = 0;
delta = csample - pchan->pcmdata;
if (delta < 0) {
sign = 0x8;
delta = -delta;
}
CLIP(delta, 0, 0xffff);
code = delta / sb4_step_table[pchan->index];
code = sign | (code > 0x7 ? 0x7 : code);
decode_sb4_sample(pchan, code);
return code;
}
static uint8_t encode_sample (struct adpcm_context *pcnxt, int ch, const int16_t *sample, int num_samples)
{
struct adpcm_channel *pchan = pcnxt->channels + ch;
int depth, nibble;
int32_t csample = *sample;
if (pcnxt->noise_shaping == NOISE_SHAPING_DYNAMIC) {
int32_t sam = (3 * pchan->history [0] - pchan->history [1]) >> 1;
int32_t temp = csample - (((pchan->weight * sam) + 512) >> 10);
int32_t shaping_weight;
if (sam && temp) pchan->weight -= (((sam ^ temp) >> 29) & 4) - 2;
pchan->history [1] = pchan->history [0];
pchan->history [0] = csample;
shaping_weight = (pchan->weight < 256) ? 1024 : 1536 - (pchan->weight * 2);
temp = -((shaping_weight * pchan->error + 512) >> 10);
if (shaping_weight < 0 && temp) {
if (temp == pchan->error)
temp = (temp < 0) ? temp + 1 : temp - 1;
pchan->error = -csample;
csample += temp;
}
else
pchan->error = -(csample += temp);
}
else if (pcnxt->noise_shaping == NOISE_SHAPING_STATIC)
pchan->error = -(csample -= pchan->error);
depth = num_samples - 1;
if (depth > pcnxt->lookahead)
depth = pcnxt->lookahead;
minimum_error (pchan, pcnxt->num_channels, csample, sample, depth, &nibble);
pchan->decode_sample (pchan, nibble);
if (pcnxt->noise_shaping)
pchan->error += pchan->pcmdata;
return nibble;
}
static void encode_chunks (struct adpcm_context *pcnxt, uint8_t **outbuf, size_t *outbufsize, const int16_t **inbuf, int inbufcount)
{
const int16_t *pcmbuf;
int chunks, ch, i;
chunks = (inbufcount - 1) / 8;
*outbufsize += (chunks * 4) * pcnxt->num_channels;
while (chunks--)
{
for (ch = 0; ch < pcnxt->num_channels; ch++)
{
pcmbuf = *inbuf + ch;
for (i = 0; i < 4; i++) {
**outbuf = encode_sample (pcnxt, ch, pcmbuf, chunks * 8 + (3 - i) * 2 + 2);
pcmbuf += pcnxt->num_channels;
**outbuf |= encode_sample (pcnxt, ch, pcmbuf, chunks * 8 + (3 - i) * 2 + 1) << 4;
pcmbuf += pcnxt->num_channels;
(*outbuf)++;
}
}
*inbuf += 8 * pcnxt->num_channels;
}
}
/* Encode a block of 16-bit PCM data into 4-bit ADPCM.
*
* Parameters:
* p the context returned by adpcm_begin()
* outbuf destination buffer
* outbufsize pointer to variable where the number of bytes written
* will be stored
* inbuf source PCM samples
* inbufcount number of composite PCM samples provided (note: this is
* the total number of 16-bit samples divided by the number
* of channels)
*
* Returns 1 (for success as there is no error checking)
*/
int adpcm_encode_block (void *p, uint8_t *outbuf, size_t *outbufsize, const int16_t *inbuf, int inbufcount)
{
struct adpcm_context *pcnxt = (struct adpcm_context *) p;
int32_t init_pcmdata[2];
int8_t init_index[2];
int ch;
*outbufsize = 0;
if (!inbufcount)
return 1;
get_decode_parameters(pcnxt, init_pcmdata, init_index);
for (ch = 0; ch < pcnxt->num_channels; ch++) {
init_pcmdata[ch] = *inbuf++;
pcnxt->init_block(pcnxt, outbuf, init_pcmdata[ch], init_index[ch]);
outbuf += 4;
*outbufsize += 4;
}
set_decode_parameters(pcnxt, init_pcmdata, init_index);
encode_chunks (pcnxt, &outbuf, outbufsize, &inbuf, inbufcount);
return 1;
}
/********************************* 4-bit ADPCM decoder ********************************/
/* Decode the block of ADPCM data into PCM. This requires no context because ADPCM blocks
* are indeppendently decodable. This assumes that a single entire block is always decoded;
* it must be called multiple times for multiple blocks and cannot resume in the middle of a
* block.
*
* Parameters:
* outbuf destination for interleaved PCM samples
* inbuf source ADPCM block
* inbufsize size of source ADPCM block
* channels number of channels in block (must be determined from other context)
*
* Returns number of converted composite samples (total samples divided by number of channels)
*/
int adpcm_decode_ima_block (int16_t *outbuf, const uint8_t *inbuf, size_t inbufsize, int channels)
{
int ch, samples = 1, chunks;
int32_t pcmdata[2];
int8_t index[2];
if (inbufsize < (uint32_t) channels * 4)
return 0;
for (ch = 0; ch < channels; ch++) {
*outbuf++ = pcmdata[ch] = (int16_t) (inbuf [0] | (inbuf [1] << 8));
index[ch] = inbuf [2];
if (index [ch] < 0 || index [ch] > 88 || inbuf [3]) // sanitize the input a little...
return 0;
inbufsize -= 4;
inbuf += 4;
}
chunks = inbufsize / (channels * 4);
samples += chunks * 8;
while (chunks--) {
int ch, i;
for (ch = 0; ch < channels; ++ch) {
for (i = 0; i < 4; ++i) {
uint16_t step = step_table [index [ch]], delta = step >> 3;
if (*inbuf & 1) delta += (step >> 2);
if (*inbuf & 2) delta += (step >> 1);
if (*inbuf & 4) delta += step;
if (*inbuf & 8)
pcmdata[ch] -= delta;
else
pcmdata[ch] += delta;
index[ch] += index_table [*inbuf & 0x7];
CLIP(index[ch], 0, 88);
CLIP(pcmdata[ch], -32768, 32767);
outbuf [i * 2 * channels] = pcmdata[ch];
step = step_table [index [ch]]; delta = step >> 3;
if (*inbuf & 0x10) delta += (step >> 2);
if (*inbuf & 0x20) delta += (step >> 1);
if (*inbuf & 0x40) delta += step;
if (*inbuf & 0x80)
pcmdata[ch] -= delta;
else
pcmdata[ch] += delta;
index[ch] += index_table [(*inbuf >> 4) & 0x7];
CLIP(index[ch], 0, 88);
CLIP(pcmdata[ch], -32768, 32767);
outbuf [(i * 2 + 1) * channels] = pcmdata[ch];
inbuf++;
}
outbuf++;
}
outbuf += channels * 7;
}
return samples;
}
int adpcm_decode_sb4_block (int16_t *outbuf, const uint8_t *inbuf, size_t inbufsize, int channels)
{
int ch, samples = 1, chunks;
int32_t pcmdata[2];
int8_t index[2];
if (inbufsize < (uint32_t) channels * 4)
return 0;
for (ch = 0; ch < channels; ch++) {
*outbuf++ = pcmdata[ch] = (int16_t)((int)inbuf[0] * 256 - 32768);
index[ch] = inbuf [2];
if (index [ch] < 0 || index [ch] > 3 || inbuf [3]) // sanitize the input a little...
return 0;
inbufsize -= 4;
inbuf += 4;
}
chunks = inbufsize / (channels * 4);
samples += chunks * 8;
while (chunks--) {
int ch, i;
for (ch = 0; ch < channels; ++ch) {
for (i = 0; i < 4; ++i) {
int delta;
int in = *inbuf++;
delta = (in & 0x07) * sb4_step_table[index[ch]];
if (in & 0x8)
pcmdata[ch] -= delta;
else
pcmdata[ch] += delta;
index[ch] += sb4_index_table[in & 0x7];
CLIP(index[ch], 0, 3);
CLIP(pcmdata[ch], -32768, 32767);
outbuf [i * 2 * channels] = pcmdata[ch];
in = in >> 4;
delta = (in & 0x07) * sb4_step_table[index[ch]];
if (in & 0x8)
pcmdata[ch] -= delta;
else
pcmdata[ch] += delta;
index[ch] += sb4_index_table[in & 0x7];
CLIP(index[ch], 0, 3);
CLIP(pcmdata[ch], -32768, 32767);
outbuf [(i * 2 + 1) * channels] = pcmdata[ch];
}
outbuf++;
}
outbuf += channels * 7;
}
return samples;
}