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/endian.h"
28 #include "sound/adpcm.h"
29 #include "sound/audiostream.h"
34 class ADPCMInputStream
: public AudioStream
{
36 Common::SeekableReadStream
*_stream
;
37 bool _disposeAfterUse
;
51 struct ADPCMChannelStatus
{
68 ADPCMChannelStatus ch
[2];
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);
84 ADPCMInputStream(Common::SeekableReadStream
*stream
, bool disposeAfterUse
, uint32 size
, typesADPCM type
, int rate
, int channels
= 2, uint32 blockAlign
= 0, uint numLoops
= 1);
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
;
142 ADPCMInputStream::~ADPCMInputStream() {
143 if (_disposeAfterUse
)
147 void ADPCMInputStream::reset() {
148 memset(&_status
, 0, sizeof(_status
));
150 _blockPos
= _blockAlign
; // To make sure first header is read
154 int ADPCMInputStream::readBuffer(int16
*buffer
, const int numSamples
) {
155 int samplesDecoded
= 0;
158 samplesDecoded
= readBufferOKI(buffer
, numSamples
);
162 samplesDecoded
= readBufferMSIMA1(buffer
, numSamples
);
164 samplesDecoded
= readBufferMSIMA2(buffer
, numSamples
);
167 samplesDecoded
= readBufferMS(_channels
, buffer
, numSamples
);
170 samplesDecoded
= readBufferTinsel4(_channels
, buffer
, numSamples
);
173 samplesDecoded
= readBufferTinsel6(_channels
, buffer
, numSamples
);
176 samplesDecoded
= readBufferTinsel8(_channels
, buffer
, numSamples
);
179 samplesDecoded
= readBufferIMA(buffer
, numSamples
);
182 error("Unsupported ADPCM encoding");
187 if (samplesDecoded
< numSamples
|| _stream
->pos() == _endpos
) {
189 if (_numLoops
== 0 || _curLoop
< _numLoops
) {
191 _stream
->seek(_startpos
);
192 return samplesDecoded
+ readBuffer(buffer
+ samplesDecoded
, numSamples
- samplesDecoded
);
196 return samplesDecoded
;
199 int ADPCMInputStream::readBufferOKI(int16
*buffer
, const int numSamples
) {
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));
213 int ADPCMInputStream::readBufferIMA(int16
*buffer
, const int numSamples
) {
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);
227 int ADPCMInputStream::readBufferMSIMA1(int16
*buffer
, const int numSamples
) {
231 assert(numSamples
% 2 == 0);
233 while (samples
< numSamples
&& !_stream
->eos() && _stream
->pos() < _endpos
) {
234 if (_blockPos
== _blockAlign
) {
236 _status
.ima_ch
[0].last
= _stream
->readSint16LE();
237 _status
.ima_ch
[0].stepIndex
= _stream
->readSint16LE();
241 for (; samples
< numSamples
&& _blockPos
< _blockAlign
&& !_stream
->eos() && _stream
->pos() < _endpos
; samples
+= 2) {
242 data
= _stream
->readByte();
244 buffer
[samples
] = TO_LE_16(decodeIMA(data
& 0x0f));
245 buffer
[samples
+ 1] = TO_LE_16(decodeIMA((data
>> 4) & 0x0f));
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
) {
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
));
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
) {
290 while (samples
< numSamples
&& !_stream
->eos() && _stream
->pos() < _endpos
) {
291 if (_blockPos
== _blockAlign
) {
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();
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));
325 static const double TinselFilterTable
[4][2] = {
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
340 start
= ~(start
| 0xC0) + 1;
342 _status
.predictor
= 1 << start
;
344 // Lower 6 bit are positive
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
) {
359 const double eVal
= 1.142822265;
363 assert(numSamples
% 2 == 0);
365 while (samples
< numSamples
&& !_stream
->eos() && _stream
->pos() < _endpos
) {
366 if (_blockPos
== _blockAlign
) {
367 readBufferTinselHeader();
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
);
382 int ADPCMInputStream::readBufferTinsel6(int channels
, int16
*buffer
, const int numSamples
) {
384 const double eVal
= 1.032226562;
388 while (samples
< numSamples
&& !_stream
->eos() && _stream
->pos() < _endpos
) {
389 if (_blockPos
== _blockAlign
) {
390 readBufferTinselHeader();
395 for (; samples
< numSamples
&& _blockPos
< _blockAlign
&& !_stream
->eos() && _stream
->pos() < _endpos
; samples
++, _chunkPos
= (_chunkPos
+ 1) % 4) {
399 _chunkData
= _stream
->readByte();
400 buffer
[samples
] = decodeTinsel((_chunkData
<< 8) & 0xFC00, eVal
);
403 _chunkData
= (_chunkData
<< 8) | (_stream
->readByte());
404 buffer
[samples
] = decodeTinsel((_chunkData
<< 6) & 0xFC00, eVal
);
408 _chunkData
= (_chunkData
<< 8) | (_stream
->readByte());
409 buffer
[samples
] = decodeTinsel((_chunkData
<< 4) & 0xFC00, eVal
);
413 _chunkData
= (_chunkData
<< 8);
414 buffer
[samples
] = decodeTinsel((_chunkData
<< 2) & 0xFC00, eVal
);
426 int ADPCMInputStream::readBufferTinsel8(int channels
, int16
*buffer
, const int numSamples
) {
429 const double eVal
= 1.007843258;
433 while (samples
< numSamples
&& !_stream
->eos() && _stream
->pos() < _endpos
) {
434 if (_blockPos
== _blockAlign
) {
435 readBufferTinselHeader();
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
);
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
) {
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;
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,
490 // Decode Linear to ADPCM
491 int16
ADPCMInputStream::decodeOKI(byte code
) {
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
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,
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);
535 int16
ADPCMInputStream::decodeTinsel(int16 code
, double eVal
) {
538 sample
= (double) code
;
539 sample
*= eVal
* _status
.predictor
;
540 sample
+= (_status
.d0
* _status
.K0
) + (_status
.d1
* _status
.K1
);
542 _status
.d1
= _status
.d0
;
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