Move actors to the first frame on entering a room.
[scummvm-innocent.git] / sound / adpcm.cpp
blob37b14140b734fa5f170563aecfc70266400422e0
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.
21 * $URL$
22 * $Id$
26 #include "common/endian.h"
28 #include "sound/adpcm.h"
29 #include "sound/audiostream.h"
32 namespace Audio {
34 class ADPCMInputStream : public AudioStream {
35 private:
36 Common::SeekableReadStream *_stream;
37 bool _disposeAfterUse;
38 int32 _startpos;
39 int32 _endpos;
40 int _channels;
41 typesADPCM _type;
42 uint32 _blockAlign;
43 uint32 _blockPos;
44 uint8 _chunkPos;
45 uint16 _chunkData;
46 int _blockLen;
47 int _rate;
48 uint _numLoops;
49 uint _curLoop;
51 struct ADPCMChannelStatus {
52 byte predictor;
53 int16 delta;
54 int16 coeff1;
55 int16 coeff2;
56 int16 sample1;
57 int16 sample2;
60 struct adpcmStatus {
61 // OKI/IMA
62 struct {
63 int32 last;
64 int32 stepIndex;
65 } ima_ch[2];
67 // MS ADPCM
68 ADPCMChannelStatus ch[2];
70 // Tinsel
71 double predictor;
72 double K0, K1;
73 double d0, d1;
74 } _status;
76 void reset();
77 int16 stepAdjust(byte);
78 int16 decodeOKI(byte);
79 int16 decodeIMA(byte code, int channel = 0); // Default to using the left channel/using one channel
80 int16 decodeMS(ADPCMChannelStatus *c, byte);
81 int16 decodeTinsel(int16, double);
83 public:
84 ADPCMInputStream(Common::SeekableReadStream *stream, bool disposeAfterUse, uint32 size, typesADPCM type, int rate, int channels = 2, uint32 blockAlign = 0, uint numLoops = 1);
85 ~ADPCMInputStream();
87 int readBuffer(int16 *buffer, const int numSamples);
88 int readBufferOKI(int16 *buffer, const int numSamples);
89 int readBufferIMA(int16 *buffer, const int numSamples);
90 int readBufferMSIMA1(int16 *buffer, const int numSamples);
91 int readBufferMSIMA2(int16 *buffer, const int numSamples);
92 int readBufferMS(int channels, int16 *buffer, const int numSamples);
93 void readBufferTinselHeader();
94 int readBufferTinsel4(int channels, int16 *buffer, const int numSamples);
95 int readBufferTinsel6(int channels, int16 *buffer, const int numSamples);
96 int readBufferTinsel8(int channels, int16 *buffer, const int numSamples);
98 bool endOfData() const { return (_stream->eos() || _stream->pos() >= _endpos); }
99 bool isStereo() const { return _channels == 2; }
100 int getRate() const { return _rate; }
103 // Routines to convert 12 bit linear samples to the
104 // Dialogic or Oki ADPCM coding format aka VOX.
105 // See also <http://www.comptek.ru/telephony/tnotes/tt1-13.html>
107 // IMA ADPCM support is based on
108 // <http://wiki.multimedia.cx/index.php?title=IMA_ADPCM>
110 // In addition, also MS IMA ADPCM is supported. See
111 // <http://wiki.multimedia.cx/index.php?title=Microsoft_IMA_ADPCM>.
113 ADPCMInputStream::ADPCMInputStream(Common::SeekableReadStream *stream, bool disposeAfterUse, uint32 size, typesADPCM type, int rate, int channels, uint32 blockAlign, uint numLoops)
114 : _stream(stream), _disposeAfterUse(disposeAfterUse), _channels(channels), _type(type), _blockAlign(blockAlign), _rate(rate), _numLoops(numLoops) {
116 if (type == kADPCMMSIma && blockAlign == 0)
117 error("ADPCMInputStream(): blockAlign isn't specified for MS IMA ADPCM");
118 if (type == kADPCMMS && blockAlign == 0)
119 error("ADPCMInputStream(): blockAlign isn't specified for MS ADPCM");
121 if (type == kADPCMTinsel4 && blockAlign == 0)
122 error("ADPCMInputStream(): blockAlign isn't specified for Tinsel 4-bit ADPCM");
123 if (type == kADPCMTinsel6 && blockAlign == 0)
124 error("ADPCMInputStream(): blockAlign isn't specified for Tinsel 6-bit ADPCM");
125 if (type == kADPCMTinsel8 && blockAlign == 0)
126 error("ADPCMInputStream(): blockAlign isn't specified for Tinsel 8-bit ADPCM");
128 if (type == kADPCMTinsel4 && channels != 1)
129 error("ADPCMInputStream(): Tinsel 4-bit ADPCM only supports mono");
130 if (type == kADPCMTinsel6 && channels != 1)
131 error("ADPCMInputStream(): Tinsel 6-bit ADPCM only supports mono");
132 if (type == kADPCMTinsel8 && channels != 1)
133 error("ADPCMInputStream(): Tinsel 8-bit ADPCM only supports mono");
135 _startpos = stream->pos();
136 _endpos = _startpos + size;
137 _curLoop = 0;
138 _blockPos = 0;
139 reset();
142 ADPCMInputStream::~ADPCMInputStream() {
143 if (_disposeAfterUse)
144 delete _stream;
147 void ADPCMInputStream::reset() {
148 memset(&_status, 0, sizeof(_status));
149 _blockLen = 0;
150 _blockPos = _blockAlign; // To make sure first header is read
151 _chunkPos = 0;
154 int ADPCMInputStream::readBuffer(int16 *buffer, const int numSamples) {
155 int samplesDecoded = 0;
156 switch (_type) {
157 case kADPCMOki:
158 samplesDecoded = readBufferOKI(buffer, numSamples);
159 break;
160 case kADPCMMSIma:
161 if (_channels == 1)
162 samplesDecoded = readBufferMSIMA1(buffer, numSamples);
163 else
164 samplesDecoded = readBufferMSIMA2(buffer, numSamples);
165 break;
166 case kADPCMMS:
167 samplesDecoded = readBufferMS(_channels, buffer, numSamples);
168 break;
169 case kADPCMTinsel4:
170 samplesDecoded = readBufferTinsel4(_channels, buffer, numSamples);
171 break;
172 case kADPCMTinsel6:
173 samplesDecoded = readBufferTinsel6(_channels, buffer, numSamples);
174 break;
175 case kADPCMTinsel8:
176 samplesDecoded = readBufferTinsel8(_channels, buffer, numSamples);
177 break;
178 case kADPCMIma:
179 samplesDecoded = readBufferIMA(buffer, numSamples);
180 break;
181 default:
182 error("Unsupported ADPCM encoding");
183 break;
186 // Loop if necessary
187 if (samplesDecoded < numSamples || _stream->pos() == _endpos) {
188 _curLoop++;
189 if (_numLoops == 0 || _curLoop < _numLoops) {
190 reset();
191 _stream->seek(_startpos);
192 return samplesDecoded + readBuffer(buffer + samplesDecoded, numSamples - samplesDecoded);
196 return samplesDecoded;
199 int ADPCMInputStream::readBufferOKI(int16 *buffer, const int numSamples) {
200 int samples;
201 byte data;
203 assert(numSamples % 2 == 0);
205 for (samples = 0; samples < numSamples && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
206 data = _stream->readByte();
207 buffer[samples] = TO_LE_16(decodeOKI((data >> 4) & 0x0f));
208 buffer[samples + 1] = TO_LE_16(decodeOKI(data & 0x0f));
210 return samples;
213 int ADPCMInputStream::readBufferIMA(int16 *buffer, const int numSamples) {
214 int samples;
215 byte data;
217 assert(numSamples % 2 == 0);
219 for (samples = 0; samples < numSamples && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
220 data = _stream->readByte();
221 buffer[samples] = decodeIMA((data >> 4) & 0x0f);
222 buffer[samples + 1] = decodeIMA(data & 0x0f, _channels == 2 ? 1 : 0);
224 return samples;
227 int ADPCMInputStream::readBufferMSIMA1(int16 *buffer, const int numSamples) {
228 int samples = 0;
229 byte data;
231 assert(numSamples % 2 == 0);
233 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
234 if (_blockPos == _blockAlign) {
235 // read block header
236 _status.ima_ch[0].last = _stream->readSint16LE();
237 _status.ima_ch[0].stepIndex = _stream->readSint16LE();
238 _blockPos = 4;
241 for (; samples < numSamples && _blockPos < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
242 data = _stream->readByte();
243 _blockPos++;
244 buffer[samples] = TO_LE_16(decodeIMA(data & 0x0f));
245 buffer[samples + 1] = TO_LE_16(decodeIMA((data >> 4) & 0x0f));
248 return samples;
252 // Microsoft as usual tries to implement it differently. This method
253 // is used for stereo data.
254 int ADPCMInputStream::readBufferMSIMA2(int16 *buffer, const int numSamples) {
255 int samples;
256 uint32 data;
257 int nibble;
258 byte k;
260 for (samples = 0; samples < numSamples && !_stream->eos() && _stream->pos() < _endpos;) {
261 for (int channel = 0; channel < 2; channel++) {
262 data = _stream->readUint32LE();
264 for (nibble = 0; nibble < 8; nibble++) {
265 k = ((data & 0xf0000000) >> 28);
266 buffer[samples + channel + nibble * 2] = TO_LE_16(decodeIMA(k));
267 data <<= 4;
270 samples += 16;
272 return samples;
275 static const int MSADPCMAdaptCoeff1[] = {
276 256, 512, 0, 192, 240, 460, 392
279 static const int MSADPCMAdaptCoeff2[] = {
280 0, -256, 0, 64, 0, -208, -232
283 int ADPCMInputStream::readBufferMS(int channels, int16 *buffer, const int numSamples) {
284 int samples;
285 byte data;
286 int i = 0;
288 samples = 0;
290 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
291 if (_blockPos == _blockAlign) {
292 // read block header
293 for (i = 0; i < channels; i++) {
294 _status.ch[i].predictor = CLIP(_stream->readByte(), (byte)0, (byte)6);
295 _status.ch[i].coeff1 = MSADPCMAdaptCoeff1[_status.ch[i].predictor];
296 _status.ch[i].coeff2 = MSADPCMAdaptCoeff2[_status.ch[i].predictor];
299 for (i = 0; i < channels; i++)
300 _status.ch[i].delta = _stream->readSint16LE();
302 for (i = 0; i < channels; i++)
303 _status.ch[i].sample1 = _stream->readSint16LE();
305 for (i = 0; i < channels; i++)
306 buffer[samples++] = _status.ch[i].sample2 = _stream->readSint16LE();
308 for (i = 0; i < channels; i++)
309 buffer[samples++] = _status.ch[i].sample1;
311 _blockPos = channels * 7;
314 for (; samples < numSamples && _blockPos < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
315 data = _stream->readByte();
316 _blockPos++;
317 buffer[samples] = TO_LE_16(decodeMS(&_status.ch[0], (data >> 4) & 0x0f));
318 buffer[samples + 1] = TO_LE_16(decodeMS(&_status.ch[channels - 1], data & 0x0f));
322 return samples;
325 static const double TinselFilterTable[4][2] = {
326 {0, 0 },
327 {0.9375, 0},
328 {1.796875, -0.8125},
329 {1.53125, -0.859375}
332 void ADPCMInputStream::readBufferTinselHeader() {
333 uint8 start = _stream->readByte();
334 uint8 filterVal = (start & 0xC0) >> 6;
336 if ((start & 0x20) != 0) {
337 //Lower 6 bit are negative
339 // Negate
340 start = ~(start | 0xC0) + 1;
342 _status.predictor = 1 << start;
343 } else {
344 // Lower 6 bit are positive
346 // Truncate
347 start &= 0x1F;
349 _status.predictor = ((double) 1.0) / (1 << start);
352 _status.K0 = TinselFilterTable[filterVal][0];
353 _status.K1 = TinselFilterTable[filterVal][1];
356 int ADPCMInputStream::readBufferTinsel4(int channels, int16 *buffer, const int numSamples) {
357 int samples;
358 uint16 data;
359 const double eVal = 1.142822265;
361 samples = 0;
363 assert(numSamples % 2 == 0);
365 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
366 if (_blockPos == _blockAlign) {
367 readBufferTinselHeader();
368 _blockPos = 0;
371 for (; samples < numSamples && _blockPos < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2, _blockPos++) {
372 // Read 1 byte = 8 bits = two 4 bit blocks
373 data = _stream->readByte();
374 buffer[samples] = decodeTinsel((data << 8) & 0xF000, eVal);
375 buffer[samples+1] = decodeTinsel((data << 12) & 0xF000, eVal);
379 return samples;
382 int ADPCMInputStream::readBufferTinsel6(int channels, int16 *buffer, const int numSamples) {
383 int samples;
384 const double eVal = 1.032226562;
386 samples = 0;
388 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
389 if (_blockPos == _blockAlign) {
390 readBufferTinselHeader();
391 _blockPos = 0;
392 _chunkPos = 0;
395 for (; samples < numSamples && _blockPos < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples++, _chunkPos = (_chunkPos + 1) % 4) {
397 switch (_chunkPos) {
398 case 0:
399 _chunkData = _stream->readByte();
400 buffer[samples] = decodeTinsel((_chunkData << 8) & 0xFC00, eVal);
401 break;
402 case 1:
403 _chunkData = (_chunkData << 8) | (_stream->readByte());
404 buffer[samples] = decodeTinsel((_chunkData << 6) & 0xFC00, eVal);
405 _blockPos++;
406 break;
407 case 2:
408 _chunkData = (_chunkData << 8) | (_stream->readByte());
409 buffer[samples] = decodeTinsel((_chunkData << 4) & 0xFC00, eVal);
410 _blockPos++;
411 break;
412 case 3:
413 _chunkData = (_chunkData << 8);
414 buffer[samples] = decodeTinsel((_chunkData << 2) & 0xFC00, eVal);
415 _blockPos++;
416 break;
423 return samples;
426 int ADPCMInputStream::readBufferTinsel8(int channels, int16 *buffer, const int numSamples) {
427 int samples;
428 byte data;
429 const double eVal = 1.007843258;
431 samples = 0;
433 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
434 if (_blockPos == _blockAlign) {
435 readBufferTinselHeader();
436 _blockPos = 0;
439 for (; samples < numSamples && _blockPos < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples++, _blockPos++) {
440 // Read 1 byte = 8 bits = one 8 bit block
441 data = _stream->readByte();
442 buffer[samples] = decodeTinsel(data << 8, eVal);
446 return samples;
449 static const int MSADPCMAdaptationTable[] = {
450 230, 230, 230, 230, 307, 409, 512, 614,
451 768, 614, 512, 409, 307, 230, 230, 230
455 int16 ADPCMInputStream::decodeMS(ADPCMChannelStatus *c, byte code) {
456 int32 predictor;
458 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
459 predictor += (signed)((code & 0x08) ? (code - 0x10) : (code)) * c->delta;
461 predictor = CLIP<int32>(predictor, -32768, 32767);
463 c->sample2 = c->sample1;
464 c->sample1 = predictor;
465 c->delta = (MSADPCMAdaptationTable[(int)code] * c->delta) >> 8;
467 if (c->delta < 16)
468 c->delta = 16;
470 return (int16)predictor;
473 // adjust the step for use on the next sample.
474 int16 ADPCMInputStream::stepAdjust(byte code) {
475 static const int16 adjusts[] = {-1, -1, -1, -1, 2, 4, 6, 8};
477 return adjusts[code & 0x07];
480 static const int16 okiStepSize[49] = {
481 16, 17, 19, 21, 23, 25, 28, 31,
482 34, 37, 41, 45, 50, 55, 60, 66,
483 73, 80, 88, 97, 107, 118, 130, 143,
484 157, 173, 190, 209, 230, 253, 279, 307,
485 337, 371, 408, 449, 494, 544, 598, 658,
486 724, 796, 876, 963, 1060, 1166, 1282, 1411,
487 1552
490 // Decode Linear to ADPCM
491 int16 ADPCMInputStream::decodeOKI(byte code) {
492 int16 diff, E, samp;
494 E = (2 * (code & 0x7) + 1) * okiStepSize[_status.ima_ch[0].stepIndex] / 8;
495 diff = (code & 0x08) ? -E : E;
496 samp = _status.ima_ch[0].last + diff;
497 // Clip the values to +/- 2^11 (supposed to be 12 bits)
498 samp = CLIP<int16>(samp, -2048, 2047);
500 _status.ima_ch[0].last = samp;
501 _status.ima_ch[0].stepIndex += stepAdjust(code);
502 _status.ima_ch[0].stepIndex = CLIP<int32>(_status.ima_ch[0].stepIndex, 0, ARRAYSIZE(okiStepSize) - 1);
504 // * 16 effectively converts 12-bit input to 16-bit output
505 return samp * 16;
508 static const uint16 imaStepTable[89] = {
509 7, 8, 9, 10, 11, 12, 13, 14,
510 16, 17, 19, 21, 23, 25, 28, 31,
511 34, 37, 41, 45, 50, 55, 60, 66,
512 73, 80, 88, 97, 107, 118, 130, 143,
513 157, 173, 190, 209, 230, 253, 279, 307,
514 337, 371, 408, 449, 494, 544, 598, 658,
515 724, 796, 876, 963, 1060, 1166, 1282, 1411,
516 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
517 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
518 7132, 7845, 8630, 9493,10442,11487,12635,13899,
519 15289,16818,18500,20350,22385,24623,27086,29794,
520 32767
523 int16 ADPCMInputStream::decodeIMA(byte code, int channel) {
524 int32 E = (2 * (code & 0x7) + 1) * imaStepTable[_status.ima_ch[channel].stepIndex] / 8;
525 int32 diff = (code & 0x08) ? -E : E;
526 int32 samp = CLIP<int32>(_status.ima_ch[channel].last + diff, -32768, 32767);
528 _status.ima_ch[channel].last = samp;
529 _status.ima_ch[channel].stepIndex += stepAdjust(code);
530 _status.ima_ch[channel].stepIndex = CLIP<int32>(_status.ima_ch[channel].stepIndex, 0, ARRAYSIZE(imaStepTable) - 1);
532 return samp;
535 int16 ADPCMInputStream::decodeTinsel(int16 code, double eVal) {
536 double sample;
538 sample = (double) code;
539 sample *= eVal * _status.predictor;
540 sample += (_status.d0 * _status.K0) + (_status.d1 * _status.K1);
542 _status.d1 = _status.d0;
543 _status.d0 = sample;
545 return (int16) CLIP<double>(sample, -32768.0, 32767.0);
548 AudioStream *makeADPCMStream(Common::SeekableReadStream *stream, bool disposeAfterUse, uint32 size, typesADPCM type, int rate, int channels, uint32 blockAlign, uint numLoops) {
549 return new ADPCMInputStream(stream, disposeAfterUse, size, type, rate, channels, blockAlign, numLoops);
552 } // End of namespace Audio