1
/* ScummVM - Graphic Adventure Engine
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.
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.
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.
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.
26
#include "common/stream.h"
27
#include "common/textconsole.h"
28
#include "common/util.h"
30
#include "audio/decoders/adpcm.h"
31
#include "audio/decoders/adpcm_intern.h"
36
// Routines to convert 12 bit linear samples to the
37
// Dialogic or Oki ADPCM coding format aka VOX.
38
// See also <http://www.comptek.ru/telephony/tnotes/tt1-13.html>
40
// IMA ADPCM support is based on
41
// <http://wiki.multimedia.cx/index.php?title=IMA_ADPCM>
43
// In addition, also MS IMA ADPCM is supported. See
44
// <http://wiki.multimedia.cx/index.php?title=Microsoft_IMA_ADPCM>.
46
ADPCMStream::ADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 size, int rate, int channels, uint32 blockAlign)
48
_disposeAfterUse(disposeAfterUse),
49
_startpos(stream->pos()),
50
_endpos(_startpos + size),
52
_blockAlign(blockAlign),
58
ADPCMStream::~ADPCMStream() {
59
if (_disposeAfterUse == DisposeAfterUse::YES)
63
void ADPCMStream::reset() {
64
memset(&_status, 0, sizeof(_status));
65
_blockPos[0] = _blockPos[1] = _blockAlign; // To make sure first header is read
68
bool ADPCMStream::rewind() {
69
// TODO: Error checking.
71
_stream->seek(_startpos);
79
int Oki_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
83
assert(numSamples % 2 == 0);
85
for (samples = 0; samples < numSamples && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
86
data = _stream->readByte();
87
buffer[samples] = decodeOKI((data >> 4) & 0x0f);
88
buffer[samples + 1] = decodeOKI(data & 0x0f);
93
static const int16 okiStepSize[49] = {
94
16, 17, 19, 21, 23, 25, 28, 31,
95
34, 37, 41, 45, 50, 55, 60, 66,
96
73, 80, 88, 97, 107, 118, 130, 143,
97
157, 173, 190, 209, 230, 253, 279, 307,
98
337, 371, 408, 449, 494, 544, 598, 658,
99
724, 796, 876, 963, 1060, 1166, 1282, 1411,
103
// Decode Linear to ADPCM
104
int16 Oki_ADPCMStream::decodeOKI(byte code) {
107
E = (2 * (code & 0x7) + 1) * okiStepSize[_status.ima_ch[0].stepIndex] / 8;
108
diff = (code & 0x08) ? -E : E;
109
samp = _status.ima_ch[0].last + diff;
110
// Clip the values to +/- 2^11 (supposed to be 12 bits)
111
samp = CLIP<int16>(samp, -2048, 2047);
113
_status.ima_ch[0].last = samp;
114
_status.ima_ch[0].stepIndex += _stepAdjustTable[code];
115
_status.ima_ch[0].stepIndex = CLIP<int32>(_status.ima_ch[0].stepIndex, 0, ARRAYSIZE(okiStepSize) - 1);
117
// * 16 effectively converts 12-bit input to 16-bit output
125
int DVI_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
129
assert(numSamples % 2 == 0);
131
for (samples = 0; samples < numSamples && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
132
data = _stream->readByte();
133
buffer[samples] = decodeIMA((data >> 4) & 0x0f);
134
buffer[samples + 1] = decodeIMA(data & 0x0f, _channels == 2 ? 1 : 0);
142
int Apple_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
143
// Need to write at least one samples per channel
144
assert((numSamples % _channels) == 0);
146
// Current sample positions
147
int samples[2] = { 0, 0};
149
// Number of samples per channel
150
int chanSamples = numSamples / _channels;
152
for (int i = 0; i < _channels; i++) {
153
_stream->seek(_streamPos[i]);
155
while ((samples[i] < chanSamples) &&
156
// Last byte read and a new one needed
157
!((_stream->eos() || (_stream->pos() >= _endpos)) && (_chunkPos[i] == 0))) {
159
if (_blockPos[i] == _blockAlign) {
160
// 2 byte header per block
161
uint16 temp = _stream->readUint16BE();
163
// First 9 bits are the upper bits of the predictor
164
_status.ima_ch[i].last = (int16) (temp & 0xFF80);
165
// Lower 7 bits are the step index
166
_status.ima_ch[i].stepIndex = temp & 0x007F;
168
// Clip the step index
169
_status.ima_ch[i].stepIndex = CLIP<int32>(_status.ima_ch[i].stepIndex, 0, 88);
174
if (_chunkPos[i] == 0) {
176
byte data = _stream->readByte();
177
_buffer[i][0] = decodeIMA(data & 0x0F, i);
178
_buffer[i][1] = decodeIMA(data >> 4, i);
181
// The original is interleaved block-wise, we want it sample-wise
182
buffer[_channels * samples[i] + i] = _buffer[i][_chunkPos[i]];
184
if (++_chunkPos[i] > 1) {
185
// We're about to decode the next byte, so advance the block position
193
if (_blockPos[i] == _blockAlign)
194
// We're at the end of the block.
195
// Since the channels are interleaved, skip the next block
196
_stream->skip(MIN<uint32>(_blockAlign, _endpos - _stream->pos()));
198
_streamPos[i] = _stream->pos();
202
return samples[0] + samples[1];
209
int MSIma_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
210
// Need to write at least one sample per channel
211
assert((numSamples % _channels) == 0);
215
while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
216
if (_blockPos[0] == _blockAlign) {
217
for (int i = 0; i < _channels; i++) {
219
_status.ima_ch[i].last = _stream->readSint16LE();
220
_status.ima_ch[i].stepIndex = _stream->readSint16LE();
223
_blockPos[0] = _channels * 4;
226
// Decode a set of samples
227
for (int i = 0; i < _channels; i++) {
228
// The stream encodes four bytes per channel at a time
229
for (int j = 0; j < 4; j++) {
230
byte data = _stream->readByte();
232
_buffer[i][j * 2] = decodeIMA(data & 0x0f, i);
233
_buffer[i][j * 2 + 1] = decodeIMA((data >> 4) & 0x0f, i);
234
_samplesLeft[i] += 2;
238
while (samples < numSamples && _samplesLeft[0] != 0) {
239
for (int i = 0; i < _channels; i++) {
240
buffer[samples] = _buffer[i][8 - _samplesLeft[i]];
244
samples += _channels;
255
static const int MSADPCMAdaptCoeff1[] = {
256
256, 512, 0, 192, 240, 460, 392
259
static const int MSADPCMAdaptCoeff2[] = {
260
0, -256, 0, 64, 0, -208, -232
263
static const int MSADPCMAdaptationTable[] = {
264
230, 230, 230, 230, 307, 409, 512, 614,
265
768, 614, 512, 409, 307, 230, 230, 230
269
int16 MS_ADPCMStream::decodeMS(ADPCMChannelStatus *c, byte code) {
272
predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
273
predictor += (signed)((code & 0x08) ? (code - 0x10) : (code)) * c->delta;
275
predictor = CLIP<int32>(predictor, -32768, 32767);
277
c->sample2 = c->sample1;
278
c->sample1 = predictor;
279
c->delta = (MSADPCMAdaptationTable[(int)code] * c->delta) >> 8;
284
return (int16)predictor;
287
int MS_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
294
while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
295
if (_blockPos[0] == _blockAlign) {
297
for (i = 0; i < _channels; i++) {
298
_status.ch[i].predictor = CLIP(_stream->readByte(), (byte)0, (byte)6);
299
_status.ch[i].coeff1 = MSADPCMAdaptCoeff1[_status.ch[i].predictor];
300
_status.ch[i].coeff2 = MSADPCMAdaptCoeff2[_status.ch[i].predictor];
303
for (i = 0; i < _channels; i++)
304
_status.ch[i].delta = _stream->readSint16LE();
306
for (i = 0; i < _channels; i++)
307
_status.ch[i].sample1 = _stream->readSint16LE();
309
for (i = 0; i < _channels; i++)
310
buffer[samples++] = _status.ch[i].sample2 = _stream->readSint16LE();
312
for (i = 0; i < _channels; i++)
313
buffer[samples++] = _status.ch[i].sample1;
315
_blockPos[0] = _channels * 7;
318
for (; samples < numSamples && _blockPos[0] < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
319
data = _stream->readByte();
321
buffer[samples] = decodeMS(&_status.ch[0], (data >> 4) & 0x0f);
322
buffer[samples + 1] = decodeMS(&_status.ch[_channels - 1], data & 0x0f);
333
#define DK3_READ_NIBBLE() \
336
_nibble = _lastByte >> 4; \
337
_topNibble = false; \
339
if (_stream->pos() >= _endpos) \
341
if ((_stream->pos() % _blockAlign) == 0) \
343
_lastByte = _stream->readByte(); \
344
_nibble = _lastByte & 0xf; \
350
int DK3_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
353
assert((numSamples % 4) == 0);
355
while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
356
if ((_stream->pos() % _blockAlign) == 0) {
357
_stream->readUint16LE(); // Unknown
358
uint16 rate = _stream->readUint16LE(); // Copy of rate
359
_stream->skip(6); // Unknown
360
// Get predictor for both sum/diff channels
361
_status.ima_ch[0].last = _stream->readSint16LE();
362
_status.ima_ch[1].last = _stream->readSint16LE();
363
// Get index for both sum/diff channels
364
_status.ima_ch[0].stepIndex = _stream->readByte();
365
_status.ima_ch[1].stepIndex = _stream->readByte();
371
assert(rate == getRate());
375
decodeIMA(_nibble, 0);
378
decodeIMA(_nibble, 1);
380
buffer[samples++] = _status.ima_ch[0].last + _status.ima_ch[1].last;
381
buffer[samples++] = _status.ima_ch[0].last - _status.ima_ch[1].last;
384
decodeIMA(_nibble, 0);
386
buffer[samples++] = _status.ima_ch[0].last + _status.ima_ch[1].last;
387
buffer[samples++] = _status.ima_ch[0].last - _status.ima_ch[1].last;
397
// This table is used to adjust the step for use on the next sample.
398
// We could half the table, but since the lookup index used is always
399
// a 4-bit nibble, it's more efficient to just keep it as it is.
400
const int16 ADPCMStream::_stepAdjustTable[16] = {
401
-1, -1, -1, -1, 2, 4, 6, 8,
402
-1, -1, -1, -1, 2, 4, 6, 8
405
const int16 Ima_ADPCMStream::_imaTable[89] = {
406
7, 8, 9, 10, 11, 12, 13, 14,
407
16, 17, 19, 21, 23, 25, 28, 31,
408
34, 37, 41, 45, 50, 55, 60, 66,
409
73, 80, 88, 97, 107, 118, 130, 143,
410
157, 173, 190, 209, 230, 253, 279, 307,
411
337, 371, 408, 449, 494, 544, 598, 658,
412
724, 796, 876, 963, 1060, 1166, 1282, 1411,
413
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
414
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
415
7132, 7845, 8630, 9493,10442,11487,12635,13899,
416
15289,16818,18500,20350,22385,24623,27086,29794,
420
int16 Ima_ADPCMStream::decodeIMA(byte code, int channel) {
421
int32 E = (2 * (code & 0x7) + 1) * _imaTable[_status.ima_ch[channel].stepIndex] / 8;
422
int32 diff = (code & 0x08) ? -E : E;
423
int32 samp = CLIP<int32>(_status.ima_ch[channel].last + diff, -32768, 32767);
425
_status.ima_ch[channel].last = samp;
426
_status.ima_ch[channel].stepIndex += _stepAdjustTable[code];
427
_status.ima_ch[channel].stepIndex = CLIP<int32>(_status.ima_ch[channel].stepIndex, 0, ARRAYSIZE(_imaTable) - 1);
432
RewindableAudioStream *makeADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 size, typesADPCM type, int rate, int channels, uint32 blockAlign) {
433
// If size is 0, report the entire size of the stream
435
size = stream->size();
439
return new Oki_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
441
return new MSIma_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
443
return new MS_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
445
return new DVI_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
447
return new Apple_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
449
return new DK3_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
451
error("Unsupported ADPCM encoding");
456
} // End of namespace Audio