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
|
/*
* ITCompression.cpp
* -----------------
* Purpose: Code for IT sample compression and decompression.
* Notes : The original Python compression code was written by GreaseMonkey and has been released into the public domain.
* Authors: OpenMPT Devs
* Ben "GreaseMonkey" Russell
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include <ostream>
#include "ITCompression.h"
#include "mpt/io/base.hpp"
#include "mpt/io/io.hpp"
#include "mpt/io/io_stdstream.hpp"
#include "../common/misc_util.h"
#include "ModSample.h"
#include "SampleCopy.h"
OPENMPT_NAMESPACE_BEGIN
// Algorithm parameters for 16-Bit samples
struct IT16BitParams
{
using sample_t = int16;
static constexpr int16 lowerTab[] = {0, -1, -3, -7, -15, -31, -56, -120, -248, -504, -1016, -2040, -4088, -8184, -16376, -32760, -32768};
static constexpr int16 upperTab[] = {0, 1, 3, 7, 15, 31, 55, 119, 247, 503, 1015, 2039, 4087, 8183, 16375, 32759, 32767};
static constexpr int8 fetchA = 4;
static constexpr int8 lowerB = -8;
static constexpr int8 upperB = 7;
static constexpr int8 defWidth = 17;
static constexpr int mask = 0xFFFF;
};
// Algorithm parameters for 8-Bit samples
struct IT8BitParams
{
using sample_t = int8;
static constexpr int8 lowerTab[] = {0, -1, -3, -7, -15, -31, -60, -124, -128};
static constexpr int8 upperTab[] = {0, 1, 3, 7, 15, 31, 59, 123, 127};
static constexpr int8 fetchA = 3;
static constexpr int8 lowerB = -4;
static constexpr int8 upperB = 3;
static constexpr int8 defWidth = 9;
static constexpr int mask = 0xFF;
};
static constexpr int8 ITWidthChangeSize[] = { 4, 5, 6, 7, 8, 9, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
//////////////////////////////////////////////////////////////////////////////
// IT 2.14 compression
ITCompression::ITCompression(const ModSample &sample, bool it215, std::ostream *f, SmpLength maxLength)
: file(f)
, mptSample(sample)
, is215(it215)
{
if(mptSample.GetElementarySampleSize() > 1)
Compress<IT16BitParams>(mptSample.sample16(), maxLength);
else
Compress<IT8BitParams>(mptSample.sample8(), maxLength);
}
template<typename Properties>
void ITCompression::Compress(const typename Properties::sample_t *mptSampleData, SmpLength maxLength)
{
packedData.resize(bufferSize);
std::vector<typename Properties::sample_t> sampleData;
sampleData.resize(blockSize / sizeof(typename Properties::sample_t));
if(maxLength == 0 || maxLength > mptSample.nLength)
maxLength = mptSample.nLength;
for(uint8 chn = 0; chn < mptSample.GetNumChannels(); chn++)
{
SmpLength offset = 0;
SmpLength remain = maxLength;
while(remain > 0)
{
// Initialise output buffer and bit writer positions
packedLength = 2;
bitPos = 0;
remBits = 8;
byteVal = 0;
CompressBlock<Properties>(mptSampleData + chn, offset, remain, sampleData.data());
if(file) mpt::IO::WriteRaw(*file, packedData.data(), packedLength);
packedTotalLength += packedLength;
offset += baseLength;
remain -= baseLength;
}
}
packedData.resize(0);
packedData.shrink_to_fit();
}
template<typename T>
void ITCompression::CopySample(T *target, const T *source, SmpLength offset, SmpLength length, SmpLength skip)
{
T *out = target;
const T *in = source + offset * skip;
for(SmpLength i = 0, j = 0; j < length; i += skip, j++)
{
out[j] = in[i];
}
}
// Convert sample to delta values.
template<typename T>
void ITCompression::Deltafy(T *sampleData)
{
T *p = sampleData;
int oldVal = 0;
for(SmpLength i = 0; i < baseLength; i++)
{
int newVal = p[i];
p[i] = static_cast<T>(newVal - oldVal);
oldVal = newVal;
}
}
template<typename Properties>
void ITCompression::CompressBlock(const typename Properties::sample_t *data, SmpLength offset, SmpLength actualLength, typename Properties::sample_t *sampleData)
{
baseLength = std::min(actualLength, SmpLength(blockSize / sizeof(typename Properties::sample_t)));
CopySample<typename Properties::sample_t>(sampleData, data, offset, baseLength, mptSample.GetNumChannels());
Deltafy(sampleData);
if(is215)
{
Deltafy(sampleData);
}
// Initialise bit width table with initial values
bwt.assign(baseLength, Properties::defWidth);
// Recurse!
SquishRecurse<Properties>(Properties::defWidth, Properties::defWidth, Properties::defWidth, Properties::defWidth - 2, 0, baseLength, sampleData);
// Write those bits!
const typename Properties::sample_t *p = sampleData;
int8 width = Properties::defWidth;
for(size_t i = 0; i < baseLength; i++)
{
if(bwt[i] != width)
{
if(width <= 6)
{
// Mode A: 1 to 6 bits
MPT_ASSERT(width);
WriteBits(width, (1 << (width - 1)));
WriteBits(Properties::fetchA, ConvertWidth(width, bwt[i]));
} else if(width < Properties::defWidth)
{
// Mode B: 7 to 8 / 16 bits
int xv = (1 << (width - 1)) + Properties::lowerB + ConvertWidth(width, bwt[i]);
WriteBits(width, xv);
} else
{
// Mode C: 9 / 17 bits
MPT_ASSERT((bwt[i] - 1) >= 0);
WriteBits(width, (1 << (width - 1)) + bwt[i] - 1);
}
width = bwt[i];
}
WriteBits(width, static_cast<int>(p[i]) & Properties::mask);
}
// Write last byte and update block length
WriteByte(byteVal);
packedData[0] = static_cast<uint8>((packedLength - 2) & 0xFF);
packedData[1] = static_cast<uint8>((packedLength - 2) >> 8);
}
int8 ITCompression::GetWidthChangeSize(int8 w, bool is16)
{
MPT_ASSERT(w > 0 && static_cast<unsigned int>(w) <= std::size(ITWidthChangeSize));
int8 wcs = ITWidthChangeSize[w - 1];
if(w <= 6 && is16)
wcs++;
return wcs;
}
template<typename Properties>
void ITCompression::SquishRecurse(int8 sWidth, int8 lWidth, int8 rWidth, int8 width, SmpLength offset, SmpLength length, const typename Properties::sample_t *sampleData)
{
if(width + 1 < 1)
{
for(SmpLength i = offset; i < offset + length; i++)
bwt[i] = sWidth;
return;
}
MPT_ASSERT(width >= 0 && static_cast<unsigned int>(width) < std::size(Properties::lowerTab));
SmpLength i = offset;
SmpLength end = offset + length;
const typename Properties::sample_t *p = sampleData;
while(i < end)
{
if(p[i] >= Properties::lowerTab[width] && p[i] <= Properties::upperTab[width])
{
SmpLength start = i;
// Check for how long we can keep this bit width
while(i < end && p[i] >= Properties::lowerTab[width] && p[i] <= Properties::upperTab[width])
{
i++;
}
const SmpLength blockLength = i - start;
const int8 xlwidth = start == offset ? lWidth : sWidth;
const int8 xrwidth = i == end ? rWidth : sWidth;
const bool is16 = sizeof(typename Properties::sample_t) > 1;
const int8 wcsl = GetWidthChangeSize(xlwidth, is16);
const int8 wcss = GetWidthChangeSize(sWidth, is16);
const int8 wcsw = GetWidthChangeSize(width + 1, is16);
bool comparison;
if(i == baseLength)
{
SmpLength keepDown = wcsl + (width + 1) * blockLength;
SmpLength levelLeft = wcsl + sWidth * blockLength;
if(xlwidth == sWidth)
levelLeft -= wcsl;
comparison = (keepDown <= levelLeft);
} else
{
SmpLength keepDown = wcsl + (width + 1) * blockLength + wcsw;
SmpLength levelLeft = wcsl + sWidth * blockLength + wcss;
if(xlwidth == sWidth)
levelLeft -= wcsl;
if(xrwidth == sWidth)
levelLeft -= wcss;
comparison = (keepDown <= levelLeft);
}
SquishRecurse<Properties>(comparison ? (width + 1) : sWidth, xlwidth, xrwidth, width - 1, start, blockLength, sampleData);
} else
{
bwt[i] = sWidth;
i++;
}
}
}
int8 ITCompression::ConvertWidth(int8 curWidth, int8 newWidth)
{
curWidth--;
newWidth--;
MPT_ASSERT(newWidth != curWidth);
if(newWidth > curWidth)
newWidth--;
return newWidth;
}
void ITCompression::WriteBits(int8 width, int v)
{
while(width > remBits)
{
byteVal |= (v << bitPos);
width -= remBits;
v >>= remBits;
bitPos = 0;
remBits = 8;
WriteByte(byteVal);
byteVal = 0;
}
if(width > 0)
{
byteVal |= (v & ((1 << width) - 1)) << bitPos;
remBits -= width;
bitPos += width;
}
}
void ITCompression::WriteByte(uint8 v)
{
if(packedLength < bufferSize)
{
packedData[packedLength++] = v;
} else
{
// How could this happen, anyway?
MPT_ASSERT_NOTREACHED();
}
}
//////////////////////////////////////////////////////////////////////////////
// IT 2.14 decompression
ITDecompression::ITDecompression(FileReader &file, ModSample &sample, bool it215)
: mptSample(sample)
, is215(it215)
{
for(uint8 chn = 0; chn < mptSample.GetNumChannels(); chn++)
{
writtenSamples = writePos = 0;
while(writtenSamples < sample.nLength && file.CanRead(sizeof(uint16)))
{
uint16 compressedSize = file.ReadUint16LE();
if(!compressedSize)
continue; // Malformed sample?
bitFile = file.ReadChunk(compressedSize);
// Initialise bit reader
mem1 = mem2 = 0;
try
{
if(mptSample.GetElementarySampleSize() > 1)
Uncompress<IT16BitParams>(mptSample.sample16() + chn);
else
Uncompress<IT8BitParams>(mptSample.sample8() + chn);
} catch(const BitReader::eof &)
{
// Data is not sufficient to decode the block
//AddToLog(LogWarning, "Truncated IT sample block");
}
}
}
}
template<typename Properties>
void ITDecompression::Uncompress(typename Properties::sample_t *target)
{
curLength = std::min(mptSample.nLength - writtenSamples, SmpLength(ITCompression::blockSize / sizeof(typename Properties::sample_t)));
int width = Properties::defWidth;
while(curLength > 0)
{
if(width > Properties::defWidth)
{
// Error!
return;
}
int v = bitFile.ReadBits(width);
const int topBit = (1 << (width - 1));
if(width <= 6)
{
// Mode A: 1 to 6 bits
if(v == topBit)
ChangeWidth(width, bitFile.ReadBits(Properties::fetchA));
else
Write<Properties>(v, topBit, target);
} else if(width < Properties::defWidth)
{
// Mode B: 7 to 8 / 16 bits
if(v >= topBit + Properties::lowerB && v <= topBit + Properties::upperB)
ChangeWidth(width, v - (topBit + Properties::lowerB));
else
Write<Properties>(v, topBit, target);
} else
{
// Mode C: 9 / 17 bits
if(v & topBit)
width = (v & ~topBit) + 1;
else
Write<Properties>((v & ~topBit), 0, target);
}
}
}
void ITDecompression::ChangeWidth(int &curWidth, int width)
{
width++;
if(width >= curWidth)
width++;
curWidth = width;
}
template<typename Properties>
void ITDecompression::Write(int v, int topBit, typename Properties::sample_t *target)
{
if(v & topBit)
v -= (topBit << 1);
mem1 += v;
mem2 += mem1;
target[writePos] = static_cast<typename Properties::sample_t>(static_cast<int>(is215 ? mem2 : mem1));
writtenSamples++;
writePos += mptSample.GetNumChannels();
curLength--;
}
OPENMPT_NAMESPACE_END
|