~ubuntu-branches/ubuntu/raring/scummvm/raring

« back to all changes in this revision

Viewing changes to audio/decoders/adpcm.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Moritz Muehlenhoff
  • Date: 2011-05-25 19:02:23 UTC
  • mto: (21.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20110525190223-fiqm0oaec714xk31
Tags: upstream-1.3.0
ImportĀ upstreamĀ versionĀ 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * $URL$
 
22
 * $Id$
 
23
 *
 
24
 */
 
25
 
 
26
#include "common/stream.h"
 
27
#include "common/textconsole.h"
 
28
#include "common/util.h"
 
29
 
 
30
#include "audio/decoders/adpcm.h"
 
31
#include "audio/decoders/adpcm_intern.h"
 
32
 
 
33
 
 
34
namespace Audio {
 
35
 
 
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>
 
39
//
 
40
// IMA ADPCM support is based on
 
41
//   <http://wiki.multimedia.cx/index.php?title=IMA_ADPCM>
 
42
//
 
43
// In addition, also MS IMA ADPCM is supported. See
 
44
//   <http://wiki.multimedia.cx/index.php?title=Microsoft_IMA_ADPCM>.
 
45
 
 
46
ADPCMStream::ADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 size, int rate, int channels, uint32 blockAlign)
 
47
        : _stream(stream),
 
48
                _disposeAfterUse(disposeAfterUse),
 
49
                _startpos(stream->pos()),
 
50
                _endpos(_startpos + size),
 
51
                _channels(channels),
 
52
                _blockAlign(blockAlign),
 
53
                _rate(rate) {
 
54
 
 
55
        reset();
 
56
}
 
57
 
 
58
ADPCMStream::~ADPCMStream() {
 
59
        if (_disposeAfterUse == DisposeAfterUse::YES)
 
60
                delete _stream;
 
61
}
 
62
 
 
63
void ADPCMStream::reset() {
 
64
        memset(&_status, 0, sizeof(_status));
 
65
        _blockPos[0] = _blockPos[1] = _blockAlign; // To make sure first header is read
 
66
}
 
67
 
 
68
bool ADPCMStream::rewind() {
 
69
        // TODO: Error checking.
 
70
        reset();
 
71
        _stream->seek(_startpos);
 
72
        return true;
 
73
}
 
74
 
 
75
 
 
76
#pragma mark -
 
77
 
 
78
 
 
79
int Oki_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
 
80
        int samples;
 
81
        byte data;
 
82
 
 
83
        assert(numSamples % 2 == 0);
 
84
 
 
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);
 
89
        }
 
90
        return samples;
 
91
}
 
92
 
 
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,
 
100
         1552
 
101
};
 
102
 
 
103
// Decode Linear to ADPCM
 
104
int16 Oki_ADPCMStream::decodeOKI(byte code) {
 
105
        int16 diff, E, samp;
 
106
 
 
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);
 
112
 
 
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);
 
116
 
 
117
        // * 16 effectively converts 12-bit input to 16-bit output
 
118
        return samp * 16;
 
119
}
 
120
 
 
121
 
 
122
#pragma mark -
 
123
 
 
124
 
 
125
int DVI_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
 
126
        int samples;
 
127
        byte data;
 
128
 
 
129
        assert(numSamples % 2 == 0);
 
130
 
 
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);
 
135
        }
 
136
        return samples;
 
137
}
 
138
 
 
139
#pragma mark -
 
140
 
 
141
 
 
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);
 
145
 
 
146
        // Current sample positions
 
147
        int samples[2] = { 0, 0};
 
148
 
 
149
        // Number of samples per channel
 
150
        int chanSamples = numSamples / _channels;
 
151
 
 
152
        for (int i = 0; i < _channels; i++) {
 
153
                _stream->seek(_streamPos[i]);
 
154
 
 
155
                while ((samples[i] < chanSamples) &&
 
156
                       // Last byte read and a new one needed
 
157
                       !((_stream->eos() || (_stream->pos() >= _endpos)) && (_chunkPos[i] == 0))) {
 
158
 
 
159
                        if (_blockPos[i] == _blockAlign) {
 
160
                                // 2 byte header per block
 
161
                                uint16 temp = _stream->readUint16BE();
 
162
 
 
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;
 
167
 
 
168
                                // Clip the step index
 
169
                                _status.ima_ch[i].stepIndex = CLIP<int32>(_status.ima_ch[i].stepIndex, 0, 88);
 
170
 
 
171
                                _blockPos[i] = 2;
 
172
                        }
 
173
 
 
174
                        if (_chunkPos[i] == 0) {
 
175
                                // Decode data
 
176
                                byte data = _stream->readByte();
 
177
                                _buffer[i][0] = decodeIMA(data &  0x0F, i);
 
178
                                _buffer[i][1] = decodeIMA(data >>    4, i);
 
179
                        }
 
180
 
 
181
                        // The original is interleaved block-wise, we want it sample-wise
 
182
                        buffer[_channels * samples[i] + i] = _buffer[i][_chunkPos[i]];
 
183
 
 
184
                        if (++_chunkPos[i] > 1) {
 
185
                                // We're about to decode the next byte, so advance the block position
 
186
                                _chunkPos[i] = 0;
 
187
                                _blockPos[i]++;
 
188
                        }
 
189
 
 
190
                        samples[i]++;
 
191
 
 
192
                        if (_channels == 2)
 
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()));
 
197
 
 
198
                        _streamPos[i] = _stream->pos();
 
199
                }
 
200
        }
 
201
 
 
202
        return samples[0] + samples[1];
 
203
}
 
204
 
 
205
 
 
206
#pragma mark -
 
207
 
 
208
 
 
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);
 
212
 
 
213
        int samples = 0;
 
214
 
 
215
        while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
 
216
                if (_blockPos[0] == _blockAlign) {
 
217
                        for (int i = 0; i < _channels; i++) {
 
218
                                // read block header
 
219
                                _status.ima_ch[i].last = _stream->readSint16LE();
 
220
                                _status.ima_ch[i].stepIndex = _stream->readSint16LE();
 
221
                        }
 
222
 
 
223
                        _blockPos[0] = _channels * 4;
 
224
                }
 
225
 
 
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();
 
231
                                _blockPos[0]++;
 
232
                                _buffer[i][j * 2] = decodeIMA(data & 0x0f, i);
 
233
                                _buffer[i][j * 2 + 1] = decodeIMA((data >> 4) & 0x0f, i);
 
234
                                _samplesLeft[i] += 2;
 
235
                        }
 
236
                }
 
237
 
 
238
                while (samples < numSamples && _samplesLeft[0] != 0) {
 
239
                        for (int i = 0; i < _channels; i++) {
 
240
                                buffer[samples] = _buffer[i][8 - _samplesLeft[i]];
 
241
                                _samplesLeft[i]--;
 
242
                        }
 
243
 
 
244
                        samples += _channels;
 
245
                }
 
246
        }
 
247
 
 
248
        return samples;
 
249
}
 
250
 
 
251
 
 
252
#pragma mark -
 
253
 
 
254
 
 
255
static const int MSADPCMAdaptCoeff1[] = {
 
256
        256, 512, 0, 192, 240, 460, 392
 
257
};
 
258
 
 
259
static const int MSADPCMAdaptCoeff2[] = {
 
260
        0, -256, 0, 64, 0, -208, -232
 
261
};
 
262
 
 
263
static const int MSADPCMAdaptationTable[] = {
 
264
        230, 230, 230, 230, 307, 409, 512, 614,
 
265
        768, 614, 512, 409, 307, 230, 230, 230
 
266
};
 
267
 
 
268
 
 
269
int16 MS_ADPCMStream::decodeMS(ADPCMChannelStatus *c, byte code) {
 
270
        int32 predictor;
 
271
 
 
272
        predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
 
273
        predictor += (signed)((code & 0x08) ? (code - 0x10) : (code)) * c->delta;
 
274
 
 
275
        predictor = CLIP<int32>(predictor, -32768, 32767);
 
276
 
 
277
        c->sample2 = c->sample1;
 
278
        c->sample1 = predictor;
 
279
        c->delta = (MSADPCMAdaptationTable[(int)code] * c->delta) >> 8;
 
280
 
 
281
        if (c->delta < 16)
 
282
                c->delta = 16;
 
283
 
 
284
        return (int16)predictor;
 
285
}
 
286
 
 
287
int MS_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
 
288
        int samples;
 
289
        byte data;
 
290
        int i = 0;
 
291
 
 
292
        samples = 0;
 
293
 
 
294
        while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
 
295
                if (_blockPos[0] == _blockAlign) {
 
296
                        // read block header
 
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];
 
301
                        }
 
302
 
 
303
                        for (i = 0; i < _channels; i++)
 
304
                                _status.ch[i].delta = _stream->readSint16LE();
 
305
 
 
306
                        for (i = 0; i < _channels; i++)
 
307
                                _status.ch[i].sample1 = _stream->readSint16LE();
 
308
 
 
309
                        for (i = 0; i < _channels; i++)
 
310
                                buffer[samples++] = _status.ch[i].sample2 = _stream->readSint16LE();
 
311
 
 
312
                        for (i = 0; i < _channels; i++)
 
313
                                buffer[samples++] = _status.ch[i].sample1;
 
314
 
 
315
                        _blockPos[0] = _channels * 7;
 
316
                }
 
317
 
 
318
                for (; samples < numSamples && _blockPos[0] < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
 
319
                        data = _stream->readByte();
 
320
                        _blockPos[0]++;
 
321
                        buffer[samples] = decodeMS(&_status.ch[0], (data >> 4) & 0x0f);
 
322
                        buffer[samples + 1] = decodeMS(&_status.ch[_channels - 1], data & 0x0f);
 
323
                }
 
324
        }
 
325
 
 
326
        return samples;
 
327
}
 
328
 
 
329
 
 
330
#pragma mark -
 
331
 
 
332
 
 
333
#define DK3_READ_NIBBLE() \
 
334
do { \
 
335
        if (_topNibble) { \
 
336
                _nibble = _lastByte >> 4; \
 
337
                _topNibble = false; \
 
338
        } else { \
 
339
                if (_stream->pos() >= _endpos) \
 
340
                        break; \
 
341
                if ((_stream->pos() % _blockAlign) == 0) \
 
342
                        continue; \
 
343
                _lastByte = _stream->readByte(); \
 
344
                _nibble = _lastByte & 0xf; \
 
345
                _topNibble = true; \
 
346
        } \
 
347
} while (0)
 
348
                
 
349
 
 
350
int DK3_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
 
351
        int samples = 0;
 
352
 
 
353
        assert((numSamples % 4) == 0);
 
354
 
 
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();
 
366
 
 
367
                        if (_stream->eos())
 
368
                                break;
 
369
 
 
370
                        // Sanity check
 
371
                        assert(rate == getRate());
 
372
                }
 
373
 
 
374
                DK3_READ_NIBBLE();
 
375
                decodeIMA(_nibble, 0);
 
376
 
 
377
                DK3_READ_NIBBLE();
 
378
                decodeIMA(_nibble, 1);
 
379
 
 
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;
 
382
 
 
383
                DK3_READ_NIBBLE();
 
384
                decodeIMA(_nibble, 0);
 
385
 
 
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;
 
388
        }
 
389
 
 
390
        return samples;
 
391
}
 
392
 
 
393
 
 
394
#pragma mark -
 
395
 
 
396
 
 
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
 
403
};
 
404
 
 
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,
 
417
        32767
 
418
};
 
419
 
 
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);
 
424
 
 
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);
 
428
 
 
429
        return samp;
 
430
}
 
431
 
 
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
 
434
        if (!size)
 
435
                size = stream->size();
 
436
 
 
437
        switch (type) {
 
438
        case kADPCMOki:
 
439
                return new Oki_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
 
440
        case kADPCMMSIma:
 
441
                return new MSIma_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
 
442
        case kADPCMMS:
 
443
                return new MS_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
 
444
        case kADPCMDVI:
 
445
                return new DVI_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
 
446
        case kADPCMApple:
 
447
                return new Apple_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
 
448
        case kADPCMDK3:
 
449
                return new DK3_ADPCMStream(stream, disposeAfterUse, size, rate, channels, blockAlign);
 
450
        default:
 
451
                error("Unsupported ADPCM encoding");
 
452
                break;
 
453
        }
 
454
}
 
455
 
 
456
} // End of namespace Audio