1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/scummsys.h"
24 #include "common/endian.h"
25 #include "common/util.h"
26 #include "scumm/imuse_digi/dimuse_codecs.h"
27 
28 #include "audio/decoders/adpcm_intern.h"
29 
30 namespace Scumm {
31 
32 namespace BundleCodecs {
33 
decode12BitsSample(const byte * src,byte ** dst,uint32 size)34 uint32 decode12BitsSample(const byte *src, byte **dst, uint32 size) {
35 	uint32 loop_size = size / 3;
36 	uint32 s_size = loop_size * 4;
37 	byte *ptr = *dst = (byte *)malloc(s_size);
38 	assert(ptr);
39 
40 	uint32 tmp;
41 	while (loop_size--) {
42 		byte v1 = *src++;
43 		byte v2 = *src++;
44 		byte v3 = *src++;
45 		tmp = ((((v2 & 0x0f) << 8) | v1) << 4) - 0x8000;
46 		WRITE_BE_UINT16(ptr, tmp); ptr += 2;
47 		tmp = ((((v2 & 0xf0) << 4) | v3) << 4) - 0x8000;
48 		WRITE_BE_UINT16(ptr, tmp); ptr += 2;
49 	}
50 	return s_size;
51 }
52 
53 /*
54  * The "IMC" codec below (see cases 13 & 15 in decompressCodec) is actually a
55  * variant of the IMA codec, see also
56  *   <http://www.multimedia.cx/simpleaudio.html>
57  *
58  * It is somewhat different, though: the standard ADPCM codecs use a fixed
59  * size for their data packets (4 bits), while the codec implemented here
60  * varies the size of each "packet" between 2 and 7 bits.
61  */
62 
63 static byte *_destImcTable = NULL;
64 static uint32 *_destImcTable2 = NULL;
65 
66 // This table is the "big brother" of Audio::ADPCMStream::_stepAdjustTable.
67 static const byte imxOtherTable[6][64] = {
68 	{
69 		0xFF,
70 		4
71 	},
72 
73 	{
74 		0xFF, 0xFF,
75 		   2,    8
76 	},
77 
78 	{
79 		0xFF, 0xFF, 0xFF, 0xFF,
80 		   1,    2,    4,    6
81 	},
82 
83 	{
84 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
85 		   1,    2,    4,    6,    8,   12,   16,   32
86 	},
87 
88 	{
89 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
90 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
91 		   1,    2,    4,    6,    8,   10,   12,   14,
92 		  16,   18,   20,   22,   24,   26,   28,   32
93 	},
94 
95 	{
96 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
97 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
98 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
99 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
100 		   1,    2,    3,    4,    5,    6,    7,    8,
101 		   9,   10,   11,   12,   13,   14,   15,   16,
102 		  17,   18,   19,   20,   21,   22,   23,   24,
103 		  25,   26,   27,   28,   29,   30,   31,   32
104 	}
105 };
106 
releaseImcTables()107 void releaseImcTables() {
108 	free(_destImcTable);
109 	_destImcTable = NULL;
110 	free(_destImcTable2);
111 	_destImcTable2 = NULL;
112 }
113 
initializeImcTables()114 void initializeImcTables() {
115 	int pos;
116 
117 	if (!_destImcTable) _destImcTable = (byte *)calloc(89, sizeof(byte));
118 	if (!_destImcTable2) _destImcTable2 = (uint32 *)calloc(89 * 64, sizeof(uint32));
119 
120 	for (pos = 0; pos <= 88; ++pos) {
121 		byte put = 1;
122 		int32 tableValue = ((Audio::Ima_ADPCMStream::_imaTable[pos] * 4) / 7) / 2;
123 		while (tableValue != 0) {
124 			tableValue /= 2;
125 			put++;
126 		}
127 		if (put < 3) {
128 			put = 3;
129 		}
130 		if (put > 8) {
131 			put = 8;
132 		}
133 		_destImcTable[pos] = put - 1;
134 	}
135 
136 	for (int n = 0; n < 64; n++) {
137 		for (pos = 0; pos <= 88; ++pos) {
138 			int32 count = 32;
139 			int32 put = 0;
140 			int32 tableValue = Audio::Ima_ADPCMStream::_imaTable[pos];
141 			do {
142 				if ((count & n) != 0) {
143 					put += tableValue;
144 				}
145 				count /= 2;
146 				tableValue /= 2;
147 			} while (count != 0);
148 			_destImcTable2[n + pos * 64] = put;
149 		}
150 	}
151 }
152 
153 #define NextBit                            \
154 	do {                                   \
155 		bit = mask & 1;                    \
156 		mask >>= 1;                        \
157 		if (!--bitsleft) {                 \
158 			mask = READ_LE_UINT16(srcptr); \
159 			srcptr += 2;                   \
160 			bitsleft = 16;                 \
161 		}                                  \
162 	} while (0)
163 
compDecode(byte * src,byte * dst)164 static int32 compDecode(byte *src, byte *dst) {
165 	byte *result, *srcptr = src, *dstptr = dst;
166 	int data, size, bit, bitsleft = 16, mask = READ_LE_UINT16(srcptr);
167 	srcptr += 2;
168 
169 	for (;;) {
170 		NextBit;
171 		if (bit) {
172 			*dstptr++ = *srcptr++;
173 		} else {
174 			NextBit;
175 			if (!bit) {
176 				NextBit;
177 				size = bit << 1;
178 				NextBit;
179 				size = (size | bit) + 3;
180 				data = *srcptr++ | 0xffffff00;
181 			} else {
182 				data = *srcptr++;
183 				size = *srcptr++;
184 
185 				data |= 0xfffff000 + ((size & 0xf0) << 4);
186 				size = (size & 0x0f) + 3;
187 
188 				if (size == 3)
189 					if (((*srcptr++) + 1) == 1)
190 						return dstptr - dst;
191 			}
192 			result = dstptr + data;
193 			while (size--)
194 				*dstptr++ = *result++;
195 		}
196 	}
197 }
198 #undef NextBit
199 
decompressADPCM(byte * compInput,byte * compOutput,int channels)200 int32 decompressADPCM(byte *compInput, byte *compOutput, int channels) {
201 	byte *src;
202 
203 	// Decoder for the the IMA ADPCM variants used in COMI.
204 	// Contrary to regular IMA ADPCM, this codec uses a variable
205 	// bitsize for the encoded data.
206 
207 	const int MAX_CHANNELS = 2;
208 	int32 outputSamplesLeft;
209 	int32 destPos;
210 	int16 firstWord;
211 	byte initialTablePos[MAX_CHANNELS] = {0, 0};
212 	//int32 initialimcTableEntry[MAX_CHANNELS] = {7, 7};
213 	int32 initialOutputWord[MAX_CHANNELS] = {0, 0};
214 	int32 totalBitOffset, curTablePos, outputWord;
215 	byte *dst;
216 	int i;
217 
218 	// We only support mono and stereo
219 	assert(channels == 1 || channels == 2);
220 
221 	src = compInput;
222 	dst = compOutput;
223 	outputSamplesLeft = 0x1000;
224 
225 	// Every data packet contains 0x2000 bytes of audio data
226 	// when extracted. In order to encode bigger data sets,
227 	// one has to split the data into multiple blocks.
228 	//
229 	// Every block starts with a 2 byte word. If that word is
230 	// non-zero, it indicates the size of a block of raw audio
231 	// data (not encoded) following it. That data we simply copy
232 	// to the output buffer and then proceed by decoding the
233 	// remaining data.
234 	//
235 	// If on the other hand the word is zero, then what follows
236 	// are 7*channels bytes containing seed data for the decoder.
237 	firstWord = READ_BE_UINT16(src);
238 	src += 2;
239 	if (firstWord != 0) {
240 		// Copy raw data
241 		memcpy(dst, src, firstWord);
242 		dst += firstWord;
243 		src += firstWord;
244 		assert((firstWord & 1) == 0);
245 		outputSamplesLeft -= firstWord / 2;
246 	} else {
247 		// Read the seed values for the decoder.
248 		for (i = 0; i < channels; i++) {
249 			initialTablePos[i] = *src;
250 			src += 1;
251 			//initialimcTableEntry[i] = READ_BE_UINT32(src);
252 			src += 4;
253 			initialOutputWord[i] = READ_BE_UINT32(src);
254 			src += 4;
255 		}
256 	}
257 
258 	totalBitOffset = 0;
259 	// The channels are encoded separately.
260 	for (int chan = 0; chan < channels; chan++) {
261 		// Read initial state (this makes it possible for the data stream
262 		// to be split & spread across multiple data chunks.
263 		curTablePos = initialTablePos[chan];
264 		//imcTableEntry = initialimcTableEntry[chan];
265 		outputWord = initialOutputWord[chan];
266 
267 		// We need to interleave the channels in the output; we achieve
268 		// that by using a variables dest offset:
269 		destPos = chan * 2;
270 
271 		const int bound = (channels == 1)
272 							? outputSamplesLeft
273 							: ((chan == 0)
274 								? (outputSamplesLeft+1) / 2
275 								: outputSamplesLeft / 2);
276 		for (i = 0; i < bound; ++i) {
277 			// Determine the size (in bits) of the next data packet
278 			const int32 curTableEntryBitCount = _destImcTable[curTablePos];
279 			assert(2 <= curTableEntryBitCount && curTableEntryBitCount <= 7);
280 
281 			// Read the next data packet
282 			const byte *readPos = src + (totalBitOffset >> 3);
283 			const uint16 readWord = (uint16)(READ_BE_UINT16(readPos) << (totalBitOffset & 7));
284 			const byte packet = (byte)(readWord >> (16 - curTableEntryBitCount));
285 
286 			// Advance read position to the next data packet
287 			totalBitOffset += curTableEntryBitCount;
288 
289 			// Decode the data packet into a delta value for the output signal.
290 			const byte signBitMask = (1 << (curTableEntryBitCount - 1));
291 			const byte dataBitMask = (signBitMask - 1);
292 			const byte data = (packet & dataBitMask);
293 
294 			const int32 tmpA = (data << (7 - curTableEntryBitCount));
295 			const int32 imcTableEntry = Audio::Ima_ADPCMStream::_imaTable[curTablePos] >> (curTableEntryBitCount - 1);
296 			int32 delta = imcTableEntry + _destImcTable2[tmpA + (curTablePos * 64)];
297 
298 			// The topmost bit in the data packet tells is a sign bit
299 			if ((packet & signBitMask) != 0) {
300 				delta = -delta;
301 			}
302 
303 			// Accumulate the delta onto the output data
304 			outputWord += delta;
305 
306 			// Clip outputWord to 16 bit signed, and write it into the destination stream
307 			outputWord = CLIP<int32>(outputWord, -0x8000, 0x7fff);
308 			WRITE_BE_UINT16(dst + destPos, outputWord);
309 			destPos += channels << 1;
310 
311 			// Adjust the curTablePos
312 			curTablePos += (int8)imxOtherTable[curTableEntryBitCount - 2][data];
313 			curTablePos = CLIP<int32>(curTablePos, 0, ARRAYSIZE(Audio::Ima_ADPCMStream::_imaTable) - 1);
314 		}
315 	}
316 
317 	return 0x2000;
318 }
319 
decompressCodec(int32 codec,byte * compInput,byte * compOutput,int32 inputSize)320 int32 decompressCodec(int32 codec, byte *compInput, byte *compOutput, int32 inputSize) {
321 	int32 outputSize;
322 	int32 offset1, offset2, offset3, length, k, c, s, j, r, t, z;
323 	byte *src, *t_table, *p, *ptr;
324 	byte t_tmp1, t_tmp2;
325 
326 	switch (codec) {
327 	case 0:
328 		memcpy(compOutput, compInput, inputSize);
329 		outputSize = inputSize;
330 		break;
331 
332 	case 1:
333 		outputSize = compDecode(compInput, compOutput);
334 		break;
335 
336 	case 2:
337 		outputSize = compDecode(compInput, compOutput);
338 		p = compOutput;
339 		for (z = 1; z < outputSize; z++)
340 			p[z] += p[z - 1];
341 		break;
342 
343 	case 3:
344 		outputSize = compDecode(compInput, compOutput);
345 		p = compOutput;
346 		for (z = 2; z < outputSize; z++)
347 			p[z] += p[z - 1];
348 		for (z = 1; z < outputSize; z++)
349 			p[z] += p[z - 1];
350 		break;
351 
352 	case 4:
353 		outputSize = compDecode(compInput, compOutput);
354 		p = compOutput;
355 		for (z = 2; z < outputSize; z++)
356 			p[z] += p[z - 1];
357 		for (z = 1; z < outputSize; z++)
358 			p[z] += p[z - 1];
359 
360 		t_table = (byte *)malloc(outputSize);
361 		assert(t_table);
362 
363 		src = compOutput;
364 		length = (outputSize << 3) / 12;
365 		k = 0;
366 		if (length > 0) {
367 			c = -12;
368 			s = 0;
369 			j = 0;
370 			do {
371 				ptr = src + length + (k >> 1);
372 				t_tmp2 = src[j];
373 				if (k & 1) {
374 					r = c >> 3;
375 					t_table[r + 2] = ((t_tmp2 & 0x0f) << 4) | (ptr[1] >> 4);
376 					t_table[r + 1] = (t_tmp2 & 0xf0) | (t_table[r + 1]);
377 				} else {
378 					r = s >> 3;
379 					t_table[r + 0] = ((t_tmp2 & 0x0f) << 4) | (ptr[0] & 0x0f);
380 					t_table[r + 1] = t_tmp2 >> 4;
381 				}
382 				s += 12;
383 				c += 12;
384 				k++;
385 				j++;
386 			} while (k < length);
387 		}
388 		offset1 = ((length - 1) * 3) >> 1;
389 		t_table[offset1 + 1] = (t_table[offset1 + 1]) | (src[length - 1] & 0xf0);
390 		memcpy(src, t_table, outputSize);
391 		free(t_table);
392 		break;
393 
394 	case 5:
395 		outputSize = compDecode(compInput, compOutput);
396 		p = compOutput;
397 		for (z = 2; z < outputSize; z++)
398 			p[z] += p[z - 1];
399 		for (z = 1; z < outputSize; z++)
400 			p[z] += p[z - 1];
401 
402 		t_table = (byte *)malloc(outputSize);
403 		assert(t_table);
404 
405 		src = compOutput;
406 		length = (outputSize << 3) / 12;
407 		k = 1;
408 		c = 0;
409 		s = 12;
410 		t_table[0] = src[length] >> 4;
411 		t = length + k;
412 		j = 1;
413 		if (t > k) {
414 			do {
415 				t_tmp1 = *(src + length + (k >> 1));
416 				t_tmp2 = src[j - 1];
417 				if (k & 1) {
418 					r = c >> 3;
419 					t_table[r + 0] = (t_tmp2 & 0xf0) | t_table[r];
420 					t_table[r + 1] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 & 0x0f);
421 				} else {
422 					r = s >> 3;
423 					t_table[r + 0] = t_tmp2 >> 4;
424 					t_table[r - 1] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 >> 4);
425 				}
426 				s += 12;
427 				c += 12;
428 				k++;
429 				j++;
430 			} while (k < t);
431 		}
432 		memcpy(src, t_table, outputSize);
433 		free(t_table);
434 		break;
435 
436 	case 6:
437 		outputSize = compDecode(compInput, compOutput);
438 		p = compOutput;
439 		for (z = 2; z < outputSize; z++)
440 			p[z] += p[z - 1];
441 		for (z = 1; z < outputSize; z++)
442 			p[z] += p[z - 1];
443 
444 		t_table = (byte *)malloc(outputSize);
445 		assert(t_table);
446 
447 		src = compOutput;
448 		length = (outputSize << 3) / 12;
449 		k = 0;
450 		c = 0;
451 		j = 0;
452 		s = -12;
453 		t_table[0] = src[outputSize - 1];
454 		t_table[outputSize - 1] = src[length - 1];
455 		t = length - 1;
456 		if (t > 0) {
457 			do {
458 				t_tmp1 = *(src + length + (k >> 1));
459 				t_tmp2 = src[j];
460 				if (k & 1) {
461 					r = s >> 3;
462 					t_table[r + 2] = (t_tmp2 & 0xf0) | t_table[r + 2];
463 					t_table[r + 3] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 >> 4);
464 				} else {
465 					r = c >> 3;
466 					t_table[r + 2] = t_tmp2 >> 4;
467 					t_table[r + 1] = ((t_tmp2 & 0x0f) << 4) | (t_tmp1 & 0x0f);
468 				}
469 				s += 12;
470 				c += 12;
471 				k++;
472 				j++;
473 			} while (k < t);
474 		}
475 		memcpy(src, t_table, outputSize);
476 		free(t_table);
477 		break;
478 
479 	case 10:
480 		outputSize = compDecode(compInput, compOutput);
481 		p = compOutput;
482 		for (z = 2; z < outputSize; z++)
483 			p[z] += p[z - 1];
484 		for (z = 1; z < outputSize; z++)
485 			p[z] += p[z - 1];
486 
487 		t_table = (byte *)malloc(outputSize);
488 		assert(t_table);
489 		memcpy(t_table, p, outputSize);
490 
491 		offset1 = outputSize / 3;
492 		offset2 = offset1 << 1;
493 		offset3 = offset2;
494 		src = compOutput;
495 
496 		while (offset1--) {
497 			offset2 -= 2;
498 			offset3--;
499 			t_table[offset2 + 0] = src[offset1];
500 			t_table[offset2 + 1] = src[offset3];
501 		}
502 
503 		src = compOutput;
504 		length = (outputSize << 3) / 12;
505 		k = 0;
506 		if (length > 0) {
507 			c = -12;
508 			s = 0;
509 			do {
510 				j = length + (k >> 1);
511 				t_tmp1 = t_table[k];
512 				if (k & 1) {
513 					r = c >> 3;
514 					t_tmp2 = t_table[j + 1];
515 					src[r + 2] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 >> 4);
516 					src[r + 1] = (src[r + 1]) | (t_tmp1 & 0xf0);
517 				} else {
518 					r = s >> 3;
519 					t_tmp2 = t_table[j];
520 					src[r + 0] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 & 0x0f);
521 					src[r + 1] = t_tmp1 >> 4;
522 				}
523 				s += 12;
524 				c += 12;
525 				k++;
526 			} while (k < length);
527 		}
528 		offset1 = ((length - 1) * 3) >> 1;
529 		src[offset1 + 1] = (t_table[length] & 0xf0) | src[offset1 + 1];
530 		free(t_table);
531 		break;
532 
533 	case 11:
534 		outputSize = compDecode(compInput, compOutput);
535 		p = compOutput;
536 		for (z = 2; z < outputSize; z++)
537 			p[z] += p[z - 1];
538 		for (z = 1; z < outputSize; z++)
539 			p[z] += p[z - 1];
540 
541 		t_table = (byte *)malloc(outputSize);
542 		assert(t_table);
543 		memcpy(t_table, p, outputSize);
544 
545 		offset1 = outputSize / 3;
546 		offset2 = offset1 << 1;
547 		offset3 = offset2;
548 		src = compOutput;
549 
550 		while (offset1--) {
551 			offset2 -= 2;
552 			offset3--;
553 			t_table[offset2 + 0] = src[offset1];
554 			t_table[offset2 + 1] = src[offset3];
555 		}
556 
557 		src = compOutput;
558 		length = (outputSize << 3) / 12;
559 		k = 1;
560 		c = 0;
561 		s = 12;
562 		t_tmp1 = t_table[length] >> 4;
563 		src[0] = t_tmp1;
564 		t = length + k;
565 		if (t > k) {
566 			do {
567 				j = length + (k >> 1);
568 				t_tmp1 = t_table[k - 1];
569 				t_tmp2 = t_table[j];
570 				if (k & 1) {
571 					r = c >> 3;
572 					src[r + 0] = (src[r]) | (t_tmp1 & 0xf0);
573 					src[r + 1] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 & 0x0f);
574 				} else {
575 					r = s >> 3;
576 					src[r + 0] = t_tmp1 >> 4;
577 					src[r - 1] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 >> 4);
578 				}
579 				s += 12;
580 				c += 12;
581 				k++;
582 			} while (k < t);
583 		}
584 		free(t_table);
585 		break;
586 
587 	case 12:
588 		outputSize = compDecode(compInput, compOutput);
589 		p = compOutput;
590 		for (z = 2; z < outputSize; z++)
591 			p[z] += p[z - 1];
592 		for (z = 1; z < outputSize; z++)
593 			p[z] += p[z - 1];
594 
595 		t_table = (byte *)malloc(outputSize);
596 		assert(t_table);
597 		memcpy(t_table, p, outputSize);
598 
599 		offset1 = outputSize / 3;
600 		offset2 = offset1 << 1;
601 		offset3 = offset2;
602 		src = compOutput;
603 
604 		while (offset1--) {
605 			offset2 -= 2;
606 			offset3--;
607 			t_table[offset2 + 0] = src[offset1];
608 			t_table[offset2 + 1] = src[offset3];
609 		}
610 
611 		src = compOutput;
612 		length = (outputSize << 3) / 12;
613 		k = 0;
614 		c = 0;
615 		s = -12;
616 		src[0] = t_table[outputSize - 1];
617 		src[outputSize - 1] = t_table[length - 1];
618 		t = length - 1;
619 		if (t > 0) {
620 			do {
621 				j = length + (k >> 1);
622 				t_tmp1 = t_table[k];
623 				t_tmp2 = t_table[j];
624 				if (k & 1) {
625 					r = s >> 3;
626 					src[r + 2] = (src[r + 2]) | (t_tmp1 & 0xf0);
627 					src[r + 3] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 >> 4);
628 				} else {
629 					r = c >> 3;
630 					src[r + 2] = t_tmp1 >> 4;
631 					src[r + 1] = ((t_tmp1 & 0x0f) << 4) | (t_tmp2 & 0x0f);
632 				}
633 				s += 12;
634 				c += 12;
635 				k++;
636 			} while (k < t);
637 		}
638 		free(t_table);
639 		break;
640 
641 	case 13:
642 	case 15:
643 		outputSize = decompressADPCM(compInput, compOutput, (codec == 13) ? 1 : 2);
644 		break;
645 
646 	default:
647 		error("BundleCodecs::decompressCodec() Unknown codec %d", (int)codec);
648 		outputSize = 0;
649 		break;
650 	}
651 
652 	return outputSize;
653 }
654 
655 } // End of namespace BundleCodecs
656 
657 } // End of namespace Scumm
658