| 1 |
/* |
| 2 |
* Schism Tracker - a cross-platform Impulse Tracker clone |
| 3 |
* copyright (c) 2003-2005 Storlek <[email protected]> |
| 4 |
* copyright (c) 2005-2008 Mrs. Brisby <[email protected]> |
| 5 |
* copyright (c) 2009 Storlek & Mrs. Brisby |
| 6 |
* copyright (c) 2010-2012 Storlek |
| 7 |
* URL: http://schismtracker.org/ |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
// slightly modified for Furnace by tildearrow |
| 25 |
|
| 26 |
#include "compression.h" |
| 27 |
|
| 28 |
#define MIN(a,b) (((a)<(b))?(a):(b)) |
| 29 |
#define MAX(a,b) (((a)>(b))?(a):(b)) |
| 30 |
#define CLAMP(x,xMin,xMax) (MIN(MAX((x),(xMin)),(xMax))) |
| 31 |
|
| 32 |
// ------------------------------------------------------------------------------------------------------------ |
| 33 |
// IT decompression code from itsex.c (Cubic Player) and load_it.cpp (Modplug) |
| 34 |
// (I suppose this could be considered a merge between the two.) |
| 35 |
|
| 36 |
static uint32_t it_readbits(int8_t n, uint32_t *bitbuf, uint32_t *bitnum, const uint8_t **ibuf) |
| 37 |
{ |
| 38 |
uint32_t value = 0; |
| 39 |
uint32_t i = n; |
| 40 |
|
| 41 |
// this could be better |
| 42 |
while (i--) { |
| 43 |
if (!*bitnum) { |
| 44 |
*bitbuf = *(*ibuf)++; |
| 45 |
*bitnum = 8; |
| 46 |
} |
| 47 |
value >>= 1; |
| 48 |
value |= (*bitbuf) << 31; |
| 49 |
(*bitbuf) >>= 1; |
| 50 |
(*bitnum)--; |
| 51 |
} |
| 52 |
return value >> (32 - n); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
uint32_t it_decompress8(void *dest, uint32_t len, const void *file, uint32_t filelen, int it215, int channels) |
| 57 |
{ |
| 58 |
const uint8_t *filebuf; // source buffer containing compressed sample data |
| 59 |
const uint8_t *srcbuf; // current position in source buffer |
| 60 |
int8_t *destpos; // position in destination buffer which will be returned |
| 61 |
uint16_t blklen; // length of compressed data block in samples |
| 62 |
uint16_t blkpos; // position in block |
| 63 |
uint8_t width; // actual "bit width" |
| 64 |
uint16_t value; // value read from file to be processed |
| 65 |
int8_t d1, d2; // integrator buffers (d2 for it2.15) |
| 66 |
int8_t v; // sample value |
| 67 |
uint32_t bitbuf, bitnum; // state for it_readbits |
| 68 |
|
| 69 |
filebuf = srcbuf = (const uint8_t *) file; |
| 70 |
destpos = (int8_t *) dest; |
| 71 |
|
| 72 |
// now unpack data till the dest buffer is full |
| 73 |
while (len) { |
| 74 |
// read a new block of compressed data and reset variables |
| 75 |
// block layout: word size, <size> bytes data |
| 76 |
if (srcbuf + 2 > filebuf + filelen |
| 77 |
|| srcbuf + 2 + (srcbuf[0] | (srcbuf[1] << 8)) > filebuf + filelen) { |
| 78 |
// truncated! |
| 79 |
return srcbuf - filebuf; |
| 80 |
} |
| 81 |
srcbuf += 2; |
| 82 |
bitbuf = bitnum = 0; |
| 83 |
|
| 84 |
blklen = MIN(0x8000, len); |
| 85 |
blkpos = 0; |
| 86 |
|
| 87 |
width = 9; // start with width of 9 bits |
| 88 |
d1 = d2 = 0; // reset integrator buffers |
| 89 |
|
| 90 |
// now uncompress the data block |
| 91 |
while (blkpos < blklen) { |
| 92 |
if (width > 9) { |
| 93 |
// illegal width, abort |
| 94 |
printf("Illegal bit width %d for 8-bit sample\n", width); |
| 95 |
return srcbuf - filebuf; |
| 96 |
} |
| 97 |
value = it_readbits(width, &bitbuf, &bitnum, &srcbuf); |
| 98 |
|
| 99 |
if (width < 7) { |
| 100 |
// method 1 (1-6 bits) |
| 101 |
// check for "100..." |
| 102 |
if (value == 1 << (width - 1)) { |
| 103 |
// yes! |
| 104 |
value = it_readbits(3, &bitbuf, &bitnum, &srcbuf) + 1; // read new width |
| 105 |
width = (value < width) ? value : value + 1; // and expand it |
| 106 |
continue; // ... next value |
| 107 |
} |
| 108 |
} else if (width < 9) { |
| 109 |
// method 2 (7-8 bits) |
| 110 |
uint8_t border = (0xFF >> (9 - width)) - 4; // lower border for width chg |
| 111 |
if (value > border && value <= (border + 8)) { |
| 112 |
value -= border; // convert width to 1-8 |
| 113 |
width = (value < width) ? value : value + 1; // and expand it |
| 114 |
continue; // ... next value |
| 115 |
} |
| 116 |
} else { |
| 117 |
// method 3 (9 bits) |
| 118 |
// bit 8 set? |
| 119 |
if (value & 0x100) { |
| 120 |
width = (value + 1) & 0xff; // new width... |
| 121 |
continue; // ... and next value |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// now expand value to signed byte |
| 126 |
if (width < 8) { |
| 127 |
uint8_t shift = 8 - width; |
| 128 |
v = (value << shift); |
| 129 |
v >>= shift; |
| 130 |
} else { |
| 131 |
v = (int8_t) value; |
| 132 |
} |
| 133 |
|
| 134 |
// integrate upon the sample values |
| 135 |
d1 += v; |
| 136 |
d2 += d1; |
| 137 |
|
| 138 |
// .. and store it into the buffer |
| 139 |
*destpos = it215 ? d2 : d1; |
| 140 |
destpos += channels; |
| 141 |
blkpos++; |
| 142 |
} |
| 143 |
|
| 144 |
// now subtract block length from total length and go on |
| 145 |
len -= blklen; |
| 146 |
} |
| 147 |
return srcbuf - filebuf; |
| 148 |
} |
| 149 |
|
| 150 |
// Mostly the same as above. |
| 151 |
uint32_t it_decompress16(void *dest, uint32_t len, const void *file, uint32_t filelen, int it215, int channels) |
| 152 |
{ |
| 153 |
const uint8_t *filebuf; // source buffer containing compressed sample data |
| 154 |
const uint8_t *srcbuf; // current position in source buffer |
| 155 |
int16_t *destpos; // position in destination buffer which will be returned |
| 156 |
uint16_t blklen; // length of compressed data block in samples |
| 157 |
uint16_t blkpos; // position in block |
| 158 |
uint8_t width; // actual "bit width" |
| 159 |
uint32_t value; // value read from file to be processed |
| 160 |
int16_t d1, d2; // integrator buffers (d2 for it2.15) |
| 161 |
int16_t v; // sample value |
| 162 |
uint32_t bitbuf, bitnum; // state for it_readbits |
| 163 |
|
| 164 |
filebuf = srcbuf = (const uint8_t *) file; |
| 165 |
destpos = (int16_t *) dest; |
| 166 |
|
| 167 |
// now unpack data till the dest buffer is full |
| 168 |
while (len) { |
| 169 |
// read a new block of compressed data and reset variables |
| 170 |
// block layout: word size, <size> bytes data |
| 171 |
if (srcbuf + 2 > filebuf + filelen |
| 172 |
|| srcbuf + 2 + (srcbuf[0] | (srcbuf[1] << 8)) > filebuf + filelen) { |
| 173 |
// truncated! |
| 174 |
return srcbuf - filebuf; |
| 175 |
} |
| 176 |
srcbuf += 2; |
| 177 |
|
| 178 |
bitbuf = bitnum = 0; |
| 179 |
|
| 180 |
blklen = MIN(0x4000, len); // 0x4000 samples => 0x8000 bytes again |
| 181 |
blkpos = 0; |
| 182 |
|
| 183 |
width = 17; // start with width of 17 bits |
| 184 |
d1 = d2 = 0; // reset integrator buffers |
| 185 |
|
| 186 |
// now uncompress the data block |
| 187 |
while (blkpos < blklen) { |
| 188 |
if (width > 17) { |
| 189 |
// illegal width, abort |
| 190 |
printf("Illegal bit width %d for 16-bit sample\n", width); |
| 191 |
return srcbuf - filebuf; |
| 192 |
} |
| 193 |
value = it_readbits(width, &bitbuf, &bitnum, &srcbuf); |
| 194 |
|
| 195 |
if (width < 7) { |
| 196 |
// method 1 (1-6 bits) |
| 197 |
// check for "100..." |
| 198 |
if (value == (uint32_t) 1 << (width - 1)) { |
| 199 |
// yes! |
| 200 |
value = it_readbits(4, &bitbuf, &bitnum, &srcbuf) + 1; // read new width |
| 201 |
width = (value < width) ? value : value + 1; // and expand it |
| 202 |
continue; // ... next value |
| 203 |
} |
| 204 |
} else if (width < 17) { |
| 205 |
// method 2 (7-16 bits) |
| 206 |
uint16_t border = (0xFFFF >> (17 - width)) - 8; // lower border for width chg |
| 207 |
if (value > border && value <= (uint32_t) (border + 16)) { |
| 208 |
value -= border; // convert width to 1-8 |
| 209 |
width = (value < width) ? value : value + 1; // and expand it |
| 210 |
continue; // ... next value |
| 211 |
} |
| 212 |
} else { |
| 213 |
// method 3 (17 bits) |
| 214 |
// bit 16 set? |
| 215 |
if (value & 0x10000) { |
| 216 |
width = (value + 1) & 0xff; // new width... |
| 217 |
continue; // ... and next value |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// now expand value to signed word |
| 222 |
if (width < 16) { |
| 223 |
uint8_t shift = 16 - width; |
| 224 |
v = (value << shift); |
| 225 |
v >>= shift; |
| 226 |
} else { |
| 227 |
v = (int16_t) value; |
| 228 |
} |
| 229 |
|
| 230 |
// integrate upon the sample values |
| 231 |
d1 += v; |
| 232 |
d2 += d1; |
| 233 |
|
| 234 |
// .. and store it into the buffer |
| 235 |
*destpos = it215 ? d2 : d1; |
| 236 |
destpos += channels; |
| 237 |
blkpos++; |
| 238 |
} |
| 239 |
|
| 240 |
// now subtract block length from total length and go on |
| 241 |
len -= blklen; |
| 242 |
} |
| 243 |
return srcbuf - filebuf; |
| 244 |
} |