1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
Audio File Library
Copyright (C) 2010-2013, Michael Pruett <michael@68k.org>
Copyright (C) 2001, Silicon Graphics, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA
*/
/*
This module implements IMA ADPCM compression.
*/
#include "config.h"
#include "IMA.h"
#include <assert.h>
#include <audiofile.h>
#include "BlockCodec.h"
#include "Compiler.h"
#include "File.h"
#include "Track.h"
#include "afinternal.h"
#include "byteorder.h"
#include "util.h"
#include "../pcm.h"
struct adpcmState
{
int previousValue; // previous output value
int index; // index into step table
adpcmState()
{
previousValue = 0;
index = 0;
}
};
class IMA : public BlockCodec
{
public:
static IMA *createDecompress(Track *track, File *fh, bool canSeek,
bool headerless, AFframecount *chunkFrames);
static IMA *createCompress(Track *track, File *fh, bool canSeek,
bool headerless, AFframecount *chunkFrames);
virtual ~IMA();
virtual const char *name() const OVERRIDE
{
return mode() == Compress ?
"ima_adpcm_compress" : "ima_adpcm_decompress";
}
virtual void describe() OVERRIDE;
private:
int m_imaType;
adpcmState *m_adpcmState;
IMA(Mode, Track *, File *fh, bool canSeek);
int decodeBlock(const uint8_t *encoded, int16_t *decoded) OVERRIDE;
int decodeBlockWAVE(const uint8_t *encoded, int16_t *decoded);
int decodeBlockQT(const uint8_t *encoded, int16_t *decoded);
int encodeBlock(const int16_t *input, uint8_t *output) OVERRIDE;
int encodeBlockWAVE(const int16_t *input, uint8_t *output);
int encodeBlockQT(const int16_t *input, uint8_t *output);
};
IMA::IMA(Mode mode, Track *track, File *fh, bool canSeek) :
BlockCodec(mode, track, fh, canSeek),
m_imaType(0)
{
AUpvlist pv = (AUpvlist) track->f.compressionParams;
m_framesPerPacket = track->f.framesPerPacket;
m_bytesPerPacket = track->f.bytesPerPacket;
long l;
if (_af_pv_getlong(pv, _AF_IMA_ADPCM_TYPE, &l))
m_imaType = l;
m_adpcmState = new adpcmState[track->f.channelCount];
}
IMA::~IMA()
{
delete [] m_adpcmState;
}
int IMA::decodeBlock(const uint8_t *encoded, int16_t *decoded)
{
if (m_imaType == _AF_IMA_ADPCM_TYPE_WAVE)
return decodeBlockWAVE(encoded, decoded);
else if (m_imaType == _AF_IMA_ADPCM_TYPE_QT)
return decodeBlockQT(encoded, decoded);
return 0;
}
static const int8_t indexTable[16] =
{
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8,
};
static const int16_t stepTable[89] =
{
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
static inline int clamp(int x, int low, int high)
{
if (x < low) return low;
if (x > high) return high;
return x;
}
static inline int16_t decodeSample(adpcmState &state, uint8_t code)
{
int step = stepTable[state.index];
int diff = step >> 3;
if (code & 4) diff += step;
if (code & 2) diff += step>>1;
if (code & 1) diff += step>>2;
int predictor = state.previousValue;
if (code & 8)
predictor -= diff;
else
predictor += diff;
state.previousValue = clamp(predictor, MIN_INT16, MAX_INT16);
state.index = clamp(state.index + indexTable[code], 0, 88);
return state.previousValue;
}
int IMA::decodeBlockWAVE(const uint8_t *encoded, int16_t *decoded)
{
int channelCount = m_track->f.channelCount;
for (int c=0; c<channelCount; c++)
{
m_adpcmState[c].previousValue = (encoded[1]<<8) | encoded[0];
if (encoded[1] & 0x80)
m_adpcmState[c].previousValue -= 0x10000;
m_adpcmState[c].index = clamp(encoded[2], 0, 88);
*decoded++ = m_adpcmState[c].previousValue;
encoded += 4;
}
for (int n=0; n<m_framesPerPacket - 1; n += 8)
{
for (int c=0; c<channelCount; c++)
{
int16_t *output = decoded + c;
for (int s=0; s<4; s++)
{
*output = decodeSample(m_adpcmState[c], *encoded & 0xf);
output += channelCount;
*output = decodeSample(m_adpcmState[c], *encoded >> 4);
output += channelCount;
encoded++;
}
}
decoded += channelCount * 8;
}
return m_framesPerPacket * channelCount * sizeof (int16_t);
}
int IMA::decodeBlockQT(const uint8_t *encoded, int16_t *decoded)
{
int channelCount = m_track->f.channelCount;
for (int c=0; c<channelCount; c++)
{
adpcmState state;
int predictor = (encoded[0] << 8) | (encoded[1] & 0x80);
if (predictor & 0x8000)
predictor -= 0x10000;
state.previousValue = clamp(predictor, MIN_INT16, MAX_INT16);
state.index = clamp(encoded[1] & 0x7f, 0, 88);
encoded += 2;
for (int n=0; n<m_framesPerPacket; n+=2)
{
uint8_t e = *encoded;
decoded[n*channelCount + c] = decodeSample(state, e & 0xf);
decoded[(n+1)*channelCount + c] = decodeSample(state, e >> 4);
encoded++;
}
}
return m_framesPerPacket * channelCount * sizeof (int16_t);
}
int IMA::encodeBlock(const int16_t *input, uint8_t *output)
{
if (m_imaType == _AF_IMA_ADPCM_TYPE_WAVE)
return encodeBlockWAVE(input, output);
else if (m_imaType == _AF_IMA_ADPCM_TYPE_QT)
return encodeBlockQT(input, output);
return 0;
}
static inline uint8_t encodeSample(adpcmState &state, int16_t sample)
{
int step = stepTable[state.index];
int diff = sample - state.previousValue;
int vpdiff = step >> 3;
uint8_t code = 0;
if (diff < 0)
{
code = 8;
diff = -diff;
}
if (diff >= step)
{
code |= 4;
diff -= step;
vpdiff += step;
}
step >>= 1;
if (diff >= step)
{
code |= 2;
diff -= step;
vpdiff += step;
}
step >>= 1;
if (diff >= step)
{
code |= 1;
vpdiff += step;
}
if (code & 8)
vpdiff = -vpdiff;
state.previousValue = clamp(state.previousValue + vpdiff,
MIN_INT16, MAX_INT16);
state.index = clamp(state.index + indexTable[code], 0, 88);
return code & 0xf;
}
int IMA::encodeBlockWAVE(const int16_t *input, uint8_t *output)
{
int channelCount = m_track->f.channelCount;
for (int c=0; c<channelCount; c++)
{
output[0] = m_adpcmState[c].previousValue & 0xff;
output[1] = m_adpcmState[c].previousValue >> 8;
output[2] = m_adpcmState[c].index;
output[3] = 0;
output += 4;
}
for (int n=0; n<m_framesPerPacket - 1; n += 8)
{
for (int c=0; c<channelCount; c++)
{
const int16_t *currentInput = input + c;
for (int s=0; s<4; s++)
{
uint8_t encodedValue = encodeSample(m_adpcmState[c], *currentInput);
currentInput += channelCount;
encodedValue |= encodeSample(m_adpcmState[c], *currentInput) << 4;
currentInput += channelCount;
*output++ = encodedValue;
}
}
input += channelCount * 8;
}
return m_bytesPerPacket;
}
int IMA::encodeBlockQT(const int16_t *input, uint8_t *output)
{
int channelCount = m_track->f.channelCount;
for (int c=0; c<channelCount; c++)
{
adpcmState state = m_adpcmState[c];
state.previousValue &= ~0x7f;
output[0] = (state.previousValue >> 8) & 0xff;
output[1] = (state.previousValue & 0x80) | (state.index & 0x7f);
output += 2;
for (int n=0; n<m_framesPerPacket; n+=2)
{
uint8_t encoded = encodeSample(state, input[n*channelCount + c]);
encoded |= encodeSample(state, input[(n+1)*channelCount + c]) << 4;
*output++ = encoded;
}
m_adpcmState[c] = state;
}
return m_bytesPerPacket;
}
bool _af_ima_adpcm_format_ok (AudioFormat *f)
{
if (f->channelCount != 1 && f->channelCount != 2)
{
_af_error(AF_BAD_COMPRESSION,
"IMA ADPCM compression requires 1 or 2 channels");
return false;
}
if (f->sampleFormat != AF_SAMPFMT_TWOSCOMP || f->sampleWidth != 16)
{
_af_error(AF_BAD_COMPRESSION,
"IMA ADPCM compression requires 16-bit signed integer format");
return false;
}
if (f->byteOrder != _AF_BYTEORDER_NATIVE)
{
_af_error(AF_BAD_COMPRESSION,
"IMA ADPCM compression requires native byte order");
return false;
}
return true;
}
void IMA::describe()
{
m_outChunk->f.byteOrder = _AF_BYTEORDER_NATIVE;
m_outChunk->f.compressionType = AF_COMPRESSION_NONE;
m_outChunk->f.compressionParams = AU_NULL_PVLIST;
}
IMA *IMA::createDecompress(Track *track, File *fh, bool canSeek,
bool headerless, AFframecount *chunkFrames)
{
assert(fh->tell() == track->fpos_first_frame);
IMA *ima = new IMA(Decompress, track, fh, canSeek);
if (!ima->m_imaType)
{
_af_error(AF_BAD_CODEC_CONFIG, "IMA type not set");
delete ima;
return NULL;
}
*chunkFrames = ima->m_framesPerPacket;
return ima;
}
IMA *IMA::createCompress(Track *track, File *fh, bool canSeek,
bool headerless, AFframecount *chunkFrames)
{
assert(fh->tell() == track->fpos_first_frame);
IMA *ima = new IMA(Compress, track, fh, canSeek);
if (!ima->m_imaType)
{
_af_error(AF_BAD_CODEC_CONFIG, "IMA type not set");
delete ima;
return NULL;
}
*chunkFrames = ima->m_framesPerPacket;
return ima;
}
FileModule *_af_ima_adpcm_init_decompress(Track *track, File *fh,
bool canSeek, bool headerless, AFframecount *chunkFrames)
{
return IMA::createDecompress(track, fh, canSeek, headerless, chunkFrames);
}
FileModule *_af_ima_adpcm_init_compress(Track *track, File *fh,
bool canSeek, bool headerless, AFframecount *chunkFrames)
{
return IMA::createCompress(track, fh, canSeek, headerless, chunkFrames);
}
|