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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*/
#include "common/scummsys.h"
#include "common/endian.h"
#include "common/util.h"
#include "scumm/imuse_digi/dimuse_codecs.h"
namespace Scumm {
namespace BundleCodecs {
uint32 decode12BitsSample(const byte *src, byte **dst, uint32 size) {
uint32 loop_size = size / 3;
uint32 s_size = loop_size * 4;
byte *ptr = *dst = (byte *)malloc(s_size);
assert(ptr);
uint32 tmp;
while (loop_size--) {
byte v1 = *src++;
byte v2 = *src++;
byte v3 = *src++;
tmp = ((((v2 & 0x0f) << 8) | v1) << 4) - 0x8000;
WRITE_BE_UINT16(ptr, tmp); ptr += 2;
tmp = ((((v2 & 0xf0) << 4) | v3) << 4) - 0x8000;
WRITE_BE_UINT16(ptr, tmp); ptr += 2;
}
return s_size;
}
/*
* The "IMC" codec below (see cases 13 & 15 in decompressCodec) is actually a
* variant of the IMA codec, see also
* <http://www.multimedia.cx/simpleaudio.html>
*
* It is somewhat different, though: the standard ADPCM codecs use a fixed
* size for their data packets (4 bits), while the codec implemented here
* varies the size of each "packet" between 2 and 7 bits.
*/
static byte _imcTableEntryBitCount[89];
static const int16 imcTable[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
};
static const byte imxOtherTable[6][64] = {
{
0xFF,
4
},
{
0xFF, 0xFF,
2, 8
},
{
0xFF, 0xFF, 0xFF, 0xFF,
1, 2, 4, 6
},
{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1, 2, 4, 6, 8, 12, 16, 32
},
{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1, 2, 4, 6, 8, 10, 12, 14,
16, 18, 20, 22, 24, 26, 28, 32
},
{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
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
}
};
void initializeImcTables() {
int pos;
for (pos = 0; pos < ARRAYSIZE(imcTable); ++pos) {
byte put = 0;
int32 tableValue = ((imcTable[pos] * 4) / 7) / 2;
while (tableValue != 0) {
tableValue /= 2;
put++;
}
if (put < 2) {
put = 2;
}
if (put > 7) {
put = 7;
}
_imcTableEntryBitCount[pos] = put;
}
}
#define NextBit \
do { \
bit = mask & 1; \
mask >>= 1; \
if (!--bitsleft) { \
mask = READ_LE_UINT16(srcptr); \
srcptr += 2; \
bitsleft = 16; \
} \
} while (0)
static int32 compDecode(byte *src, byte *dst) {
byte *result, *srcptr = src, *dstptr = dst;
int data, size, bit, bitsleft = 16, mask = READ_LE_UINT16(srcptr);
srcptr += 2;
for (;;) {
NextBit;
if (bit) {
*dstptr++ = *srcptr++;
} else {
NextBit;
if (!bit) {
NextBit;
size = bit << 1;
NextBit;
size = (size | bit) + 3;
data = *srcptr++ | 0xffffff00;
} else {
data = *srcptr++;
size = *srcptr++;
data |= 0xfffff000 + ((size & 0xf0) << 4);
size = (size & 0x0f) + 3;
if (size == 3)
if (((*srcptr++) + 1) == 1)
return dstptr - dst;
}
result = dstptr + data;
while (size--)
*dstptr++ = *result++;
}
}
}
#undef NextBit
int32 decompressADPCM(byte *compInput, byte *compOutput, int channels) {
byte *src;
// Decoder for the the IMA ADPCM variants used in COMI.
// Contrary to regular IMA ADPCM, this codec uses a variable
// bitsize for the encoded data.
const int MAX_CHANNELS = 2;
int32 outputSamplesLeft;
int32 destPos;
int16 firstWord;
byte initialTablePos[MAX_CHANNELS] = {0, 0};
//int32 initialimcTableEntry[MAX_CHANNELS] = {7, 7};
int32 initialOutputWord[MAX_CHANNELS] = {0, 0};
int32 totalBitOffset, curTablePos, outputWord;
byte *dst;
int i;
// We only support mono and stereo
assert(channels == 1 || channels == 2);
src = compInput;
dst = compOutput;
outputSamplesLeft = 0x1000;
// Every data packet contains 0x2000 bytes of audio data
// when extracted. In order to encode bigger data sets,
// one has to split the data into multiple blocks.
//
// Every block starts with a 2 byte word. If that word is
// non-zero, it indicates the size of a block of raw audio
// data (not encoded) following it. That data we simply copy
// to the output buffer and the proceed by decoding the
// remaining data.
//
// If on the other hand the word is zero, then what follows
// are 7*channels bytes containing seed data for the decoder.
firstWord = READ_BE_UINT16(src);
src += 2;
if (firstWord != 0) {
// Copy raw data
memcpy(dst, src, firstWord);
dst += firstWord;
src += firstWord;
assert((firstWord & 1) == 0);
outputSamplesLeft -= firstWord / 2;
} else {
// Read the seed values for the decoder.
for (i = 0; i < channels; i++) {
initialTablePos[i] = *src;
src += 1;
//initialimcTableEntry[i] = READ_BE_UINT32(src);
src += 4;
initialOutputWord[i] = READ_BE_UINT32(src);
src += 4;
}
}
totalBitOffset = 0;
// The channels are encoded separately.
for (int chan = 0; chan < channels; chan++) {
// Read initial state (this makes it possible for the data stream
// to be split & spread across multiple data chunks.
curTablePos = initialTablePos[chan];
//imcTableEntry = initialimcTableEntry[chan];
outputWord = initialOutputWord[chan];
// We need to interleave the channels in the output; we achieve
// that by using a variables dest offset:
destPos = chan * 2;
const int bound = (channels == 1)
? outputSamplesLeft
: ((chan == 0)
? (outputSamplesLeft+1) / 2
: outputSamplesLeft / 2);
for (i = 0; i < bound; ++i) {
// Determine the size (in bits) of the next data packet
const int32 curTableEntryBitCount = _imcTableEntryBitCount[curTablePos];
assert(2 <= curTableEntryBitCount && curTableEntryBitCount <= 7);
// Read the next data packet
const byte *readPos = src + (totalBitOffset >> 3);
const uint16 readWord = (uint16)(READ_BE_UINT16(readPos) << (totalBitOffset & 7));
const byte packet = (byte)(readWord >> (16 - curTableEntryBitCount));
// Advance read position to the next data packet
totalBitOffset += curTableEntryBitCount;
// Decode the data packet into a delta value for the output signal.
const byte signBitMask = (1 << (curTableEntryBitCount - 1));
const byte dataBitMask = (signBitMask - 1);
const byte data = (packet & dataBitMask);
int32 delta = imcTable[curTablePos] * (2 * data + 1) >> (curTableEntryBitCount - 1);
// The topmost bit in the data packet tells is a sign bit
if ((packet & signBitMask) != 0) {
delta = -delta;
}
// Accumulate the delta onto the output data
outputWord += delta;
// Clip outputWord to 16 bit signed, and write it into the destination stream
if (outputWord > 0x7fff)
outputWord = 0x7fff;
if (outputWord < -0x8000)
outputWord = -0x8000;
WRITE_BE_UINT16(dst + destPos, outputWord);
destPos += channels << 1;
// Adjust the curTablePos
curTablePos += (int8)imxOtherTable[curTableEntryBitCount - 2][data];
if (curTablePos < 0)
curTablePos = 0;
else if (curTablePos >= ARRAYSIZE(imcTable))
curTablePos = ARRAYSIZE(imcTable) - 1;
}
}
return 0x2000;
}
int32 decompressCodec(int32 codec, byte *compInput, byte *compOutput, int32 inputSize) {
int32 outputSize;
int32 offset1, offset2, offset3, length, k, c, s, j, r, t, z;
byte *src, *t_table, *p, *ptr;
byte t_tmp1, t_tmp2;
switch (codec) {
case 0:
memcpy(compOutput, compInput, inputSize);
outputSize = inputSize;
break;
case 1:
outputSize = compDecode(compInput, compOutput);
break;
case 2:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
break;
case 3:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 2; z < outputSize; z++)
p[z] += p[z - 1];
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
break;
case 4:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 2; z < outputSize; z++)
p[z] += p[z - 1];
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
t_table = (byte *)malloc(outputSize);
assert(t_table);
src = compOutput;
length = (outputSize << 3) / 12;
k = 0;
if (length > 0) {
c = -12;
s = 0;
j = 0;
do {
ptr = src + length + (k >> 1);
t_tmp2 = src[j];
if (k & 1) {
r = c >> 3;
t_table[r + 2] = ((t_tmp2 & 0x0f) << 4) | (ptr[1] >> 4);
t_table[r + 1] = (t_tmp2 & 0xf0) | (t_table[r + 1]);
} else {
r = s >> 3;
t_table[r + 0] = ((t_tmp2 & 0x0f) << 4) | (ptr[0] & 0x0f);
t_table[r + 1] = t_tmp2 >> 4;
}
s += 12;
c += 12;
k++;
j++;
} while (k < length);
}
offset1 = ((length - 1) * 3) >> 1;
t_table[offset1 + 1] = (t_table[offset1 + 1]) | (src[length - 1] & 0xf0);
memcpy(src, t_table, outputSize);
free(t_table);
break;
case 5:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 2; z < outputSize; z++)
p[z] += p[z - 1];
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
t_table = (byte *)malloc(outputSize);
assert(t_table);
src = compOutput;
length = (outputSize << 3) / 12;
k = 1;
c = 0;
s = 12;
t_table[0] = src[length] >> 4;
t = length + k;
j = 1;
if (t > k) {
do {
t_tmp1 = *(src + length + (k >> 1));
t_tmp2 = src[j - 1];
if (k & 1) {
r = c >> 3;
t_table[r + 0] = (t_tmp2 & 0xf0) | t_table[r];
t_table[r + 1] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 & 0x0f);
} else {
r = s >> 3;
t_table[r + 0] = t_tmp2 >> 4;
t_table[r - 1] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 >> 4);
}
s += 12;
c += 12;
k++;
j++;
} while (k < t);
}
memcpy(src, t_table, outputSize);
free(t_table);
break;
case 6:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 2; z < outputSize; z++)
p[z] += p[z - 1];
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
t_table = (byte *)malloc(outputSize);
assert(t_table);
src = compOutput;
length = (outputSize << 3) / 12;
k = 0;
c = 0;
j = 0;
s = -12;
t_table[0] = src[outputSize - 1];
t_table[outputSize - 1] = src[length - 1];
t = length - 1;
if (t > 0) {
do {
t_tmp1 = *(src + length + (k >> 1));
t_tmp2 = src[j];
if (k & 1) {
r = s >> 3;
t_table[r + 2] = (t_tmp2 & 0xf0) | t_table[r + 2];
t_table[r + 3] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 >> 4);
} else {
r = c >> 3;
t_table[r + 2] = t_tmp2 >> 4;
t_table[r + 1] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 & 0x0f);
}
s += 12;
c += 12;
k++;
j++;
} while (k < t);
}
memcpy(src, t_table, outputSize);
free(t_table);
break;
case 10:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 2; z < outputSize; z++)
p[z] += p[z - 1];
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
t_table = (byte *)malloc(outputSize);
assert(t_table);
memcpy(t_table, p, outputSize);
offset1 = outputSize / 3;
offset2 = offset1 << 1;
offset3 = offset2;
src = compOutput;
while (offset1--) {
offset2 -= 2;
offset3--;
t_table[offset2 + 0] = src[offset1];
t_table[offset2 + 1] = src[offset3];
}
src = compOutput;
length = (outputSize << 3) / 12;
k = 0;
if (length > 0) {
c = -12;
s = 0;
do {
j = length + (k >> 1);
t_tmp1 = t_table[k];
if (k & 1) {
r = c >> 3;
t_tmp2 = t_table[j + 1];
src[r + 2] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 >> 4);
src[r + 1] = (src[r + 1]) | (t_tmp1 & 0xf0);
} else {
r = s >> 3;
t_tmp2 = t_table[j];
src[r + 0] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 & 0x0f);
src[r + 1] = t_tmp1 >> 4;
}
s += 12;
c += 12;
k++;
} while (k < length);
}
offset1 = ((length - 1) * 3) >> 1;
src[offset1 + 1] = (t_table[length] & 0xf0) | src[offset1 + 1];
free(t_table);
break;
case 11:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 2; z < outputSize; z++)
p[z] += p[z - 1];
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
t_table = (byte *)malloc(outputSize);
assert(t_table);
memcpy(t_table, p, outputSize);
offset1 = outputSize / 3;
offset2 = offset1 << 1;
offset3 = offset2;
src = compOutput;
while (offset1--) {
offset2 -= 2;
offset3--;
t_table[offset2 + 0] = src[offset1];
t_table[offset2 + 1] = src[offset3];
}
src = compOutput;
length = (outputSize << 3) / 12;
k = 1;
c = 0;
s = 12;
t_tmp1 = t_table[length] >> 4;
src[0] = t_tmp1;
t = length + k;
if (t > k) {
do {
j = length + (k >> 1);
t_tmp1 = t_table[k - 1];
t_tmp2 = t_table[j];
if (k & 1) {
r = c >> 3;
src[r + 0] = (src[r]) | (t_tmp1 & 0xf0);
src[r + 1] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 & 0x0f);
} else {
r = s >> 3;
src[r + 0] = t_tmp1 >> 4;
src[r - 1] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 >> 4);
}
s += 12;
c += 12;
k++;
} while (k < t);
}
free(t_table);
break;
case 12:
outputSize = compDecode(compInput, compOutput);
p = compOutput;
for (z = 2; z < outputSize; z++)
p[z] += p[z - 1];
for (z = 1; z < outputSize; z++)
p[z] += p[z - 1];
t_table = (byte *)malloc(outputSize);
assert(t_table);
memcpy(t_table, p, outputSize);
offset1 = outputSize / 3;
offset2 = offset1 << 1;
offset3 = offset2;
src = compOutput;
while (offset1--) {
offset2 -= 2;
offset3--;
t_table[offset2 + 0] = src[offset1];
t_table[offset2 + 1] = src[offset3];
}
src = compOutput;
length = (outputSize << 3) / 12;
k = 0;
c = 0;
s = -12;
src[0] = t_table[outputSize - 1];
src[outputSize - 1] = t_table[length - 1];
t = length - 1;
if (t > 0) {
do {
j = length + (k >> 1);
t_tmp1 = t_table[k];
t_tmp2 = t_table[j];
if (k & 1) {
r = s >> 3;
src[r + 2] = (src[r + 2]) | (t_tmp1 & 0xf0);
src[r + 3] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 >> 4);
} else {
r = c >> 3;
src[r + 2] = t_tmp1 >> 4;
src[r + 1] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 & 0x0f);
}
s += 12;
c += 12;
k++;
} while (k < t);
}
free(t_table);
break;
case 13:
case 15:
outputSize = decompressADPCM(compInput, compOutput, (codec == 13) ? 1 : 2);
break;
default:
error("BundleCodecs::decompressCodec() Unknown codec %d", (int)codec);
outputSize = 0;
break;
}
return outputSize;
}
} // End of namespace BundleCodecs
} // End of namespace Scumm
|