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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
/*
* Load_dsym.cpp
* -------------
* Purpose: Digital Symphony module loader
* Notes : Based on information from the DSym_Info file and sigma-delta decompression code from TimPlayer.
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "Loaders.h"
#include "BitReader.h"
#include "mpt/endian/int24.hpp"
OPENMPT_NAMESPACE_BEGIN
struct DSymFileHeader
{
using uint24le = mpt::uint24le;
char magic[8];
uint8le version; // 0 / 1
uint8le numChannels; // 1...8
uint16le numOrders; // 0...4096
uint16le numTracks; // 0...4096
uint24le infoLen;
bool Validate() const
{
return !std::memcmp(magic, "\x02\x01\x13\x13\x14\x12\x01\x0B", 8)
&& version <= 1
&& numChannels >= 1 && numChannels <= 8
&& numOrders <= 4096
&& numTracks <= 4096;
}
uint64 GetHeaderMinimumAdditionalSize() const
{
return 72u;
}
};
MPT_BINARY_STRUCT(DSymFileHeader, 17)
static std::vector<std::byte> DecompressDSymLZW(FileReader &file, uint32 size)
{
BitReader bitFile(file);
const auto startPos = bitFile.GetPosition();
// In the best case, 13 bits decode 8192 bytes, a ratio of approximately 1:5042.
// Too much for reserving memory in case of malformed files, just choose an arbitrary but realistic upper limit.
std::vector<std::byte> output;
output.reserve(std::min(size, std::min(mpt::saturate_cast<uint32>(file.BytesLeft()), Util::MaxValueOfType(size) / 50u) * 50u));
static constexpr uint16 lzwBits = 13, MaxNodes = 1 << lzwBits;
static constexpr uint16 ResetDict = 256, EndOfStream = 257;
struct LZWEntry
{
uint16 prev;
std::byte value;
};
std::vector<LZWEntry> dictionary(MaxNodes);
std::vector<std::byte> match(MaxNodes);
// Initialize dictionary
for(int i = 0; i < 256; i++)
{
dictionary[i].prev = MaxNodes;
dictionary[i].value = static_cast<std::byte>(i);
}
uint8 codeSize = 9;
uint16 prevCode = 0;
uint16 nextIndex = 257;
while(true)
{
// Read next code
const auto newCode = static_cast<uint16>(bitFile.ReadBits(codeSize));
if(newCode == EndOfStream || newCode > nextIndex || output.size() >= size)
break;
// Reset dictionary
if(newCode == ResetDict)
{
codeSize = 9;
prevCode = 0;
nextIndex = 257;
continue;
}
// Output
auto code = (newCode < nextIndex) ? newCode : prevCode;
auto writeOffset = MaxNodes;
do
{
match[--writeOffset] = dictionary[code].value;
code = dictionary[code].prev;
} while(code < MaxNodes);
output.insert(output.end(), match.begin() + writeOffset, match.end());
// Handling for KwKwK problem
if(newCode == nextIndex)
output.push_back(match[writeOffset]);
// Add to dictionary
if(nextIndex < MaxNodes)
{
// Special case for FULLEFFECT, NARCOSIS and NEWDANCE, which end with a dictionary size of 512
// right before the end-of-stream token, but the code size is expected to be 9
if(output.size() >= size)
continue;
dictionary[nextIndex].value = match[writeOffset];
dictionary[nextIndex].prev = prevCode;
nextIndex++;
if(nextIndex != MaxNodes && nextIndex == (1u << codeSize))
codeSize++;
}
prevCode = newCode;
}
MPT_ASSERT(output.size() == size);
// Align length to 4 bytes
file.Seek(startPos + ((bitFile.GetPosition() - startPos + 3u) & ~FileReader::pos_type(3)));
// cppcheck false-positive
// cppcheck-suppress returnDanglingLifetime
return output;
}
static std::vector<std::byte> DecompressDSymSigmaDelta(FileReader &file, uint32 size)
{
const uint8 maxRunLength = std::max(file.ReadUint8(), uint8(1));
BitReader bitFile(file);
const auto startPos = bitFile.GetPosition();
// In the best case, sigma-delta compression represents each sample point as one bit.
// As a result, if we have a file length of n, we know that the sample can be at most n*8 sample points long.
LimitMax(size, std::min(mpt::saturate_cast<uint32>(file.BytesLeft()), Util::MaxValueOfType(size) / 8u) * 8u);
std::vector<std::byte> output(size);
uint32 pos = 0;
uint8 runLength = maxRunLength;
uint8 numBits = 8;
uint8 accum = static_cast<uint8>(bitFile.ReadBits(numBits));
output[pos++] = mpt::byte_cast<std::byte>(accum);
while(pos < size)
{
const uint32 value = bitFile.ReadBits(numBits);
// Increase bit width
if(value == 0)
{
if(numBits >= 9)
break;
numBits++;
runLength = maxRunLength;
continue;
}
if(value & 1)
accum -= static_cast<uint8>(value >> 1);
else
accum += static_cast<uint8>(value >> 1);
output[pos++] = mpt::byte_cast<std::byte>(accum);
// Reset run length if high bit is set
if((value >> (numBits - 1u)) != 0)
{
runLength = maxRunLength;
continue;
}
// Decrease bit width
if(--runLength == 0)
{
if(numBits > 1)
numBits--;
runLength = maxRunLength;
}
}
// Align length to 4 bytes
file.Seek(startPos + ((bitFile.GetPosition() - startPos + 3u) & ~FileReader::pos_type(3)));
return output;
}
static bool ReadDSymChunk(FileReader &file, std::vector<std::byte> &data, uint32 size)
{
const uint8 packingType = file.ReadUint8();
if(packingType > 1)
return false;
if(packingType)
{
try
{
data = DecompressDSymLZW(file, size);
} catch(const BitReader::eof &)
{
return false;
}
} else
{
if(!file.CanRead(size))
return false;
file.ReadVector(data, size);
}
return data.size() >= size;
}
CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderDSym(MemoryFileReader file, const uint64 *pfilesize)
{
DSymFileHeader fileHeader;
if(!file.ReadStruct(fileHeader))
return ProbeWantMoreData;
if(!fileHeader.Validate())
return ProbeFailure;
return ProbeAdditionalSize(file, pfilesize, fileHeader.GetHeaderMinimumAdditionalSize());
}
bool CSoundFile::ReadDSym(FileReader &file, ModLoadingFlags loadFlags)
{
DSymFileHeader fileHeader;
file.Rewind();
if(!file.ReadStruct(fileHeader) || !fileHeader.Validate())
return false;
if(!file.CanRead(mpt::saturate_cast<FileReader::pos_type>(fileHeader.GetHeaderMinimumAdditionalSize())))
return false;
if(loadFlags == onlyVerifyHeader)
return true;
InitializeGlobals(MOD_TYPE_MOD, fileHeader.numChannels);
m_SongFlags.set(SONG_IMPORTED | SONG_AMIGALIMITS);
m_SongFlags.reset(SONG_ISAMIGA);
m_nSamples = 63;
for(CHANNELINDEX chn = 0; chn < GetNumChannels(); chn++)
{
ChnSettings[chn].nPan = (((chn & 3) == 1) || ((chn & 3) == 2)) ? 64 : 192;
}
uint8 sampleNameLength[64] = {};
for(SAMPLEINDEX smp = 1; smp <= m_nSamples; smp++)
{
Samples[smp].Initialize(MOD_TYPE_MOD);
sampleNameLength[smp] = file.ReadUint8();
if(!(sampleNameLength[smp] & 0x80))
Samples[smp].nLength = file.ReadUint24LE() << 1;
}
file.ReadSizedString<uint8le, mpt::String::spacePadded>(m_songName);
const auto allowedCommands = file.ReadArray<uint8, 8>();
std::vector<uint16le> sequence;
if(fileHeader.numOrders)
{
std::vector<std::byte> sequenceData;
const uint32 sequenceSize = fileHeader.numOrders * fileHeader.numChannels * 2u;
if(!ReadDSymChunk(file, sequenceData, sequenceSize))
return false;
FileReader sequenceChunk = FileReader(mpt::as_span(sequenceData));
sequenceChunk.ReadVector(sequence, sequenceData.size() / 2u);
}
std::vector<std::byte> trackData;
trackData.reserve(fileHeader.numTracks * 256u);
// For some reason, patterns are stored in 512K chunks
for(uint16 offset = 0; offset < fileHeader.numTracks; offset += 2000)
{
const uint32 chunkSize = std::min(fileHeader.numTracks - offset, 2000) * 256;
std::vector<std::byte> chunk;
if(!ReadDSymChunk(file, chunk, chunkSize))
return false;
trackData.insert(trackData.end(), chunk.begin(), chunk.end());
}
const auto tracks = mpt::byte_cast<mpt::span<uint8>>(mpt::as_span(trackData));
Order().resize(fileHeader.numOrders);
for(ORDERINDEX pat = 0; pat < fileHeader.numOrders; pat++)
{
Order()[pat] = pat;
if(!(loadFlags & loadPatternData) || !Patterns.Insert(pat, 64))
continue;
for(CHANNELINDEX chn = 0; chn < GetNumChannels(); chn++)
{
const uint16 track = sequence[pat * GetNumChannels() + chn];
if(track >= fileHeader.numTracks)
continue;
ModCommand *m = Patterns[pat].GetpModCommand(0, chn);
for(ROWINDEX row = 0; row < 64; row++, m += GetNumChannels())
{
const auto data = tracks.subspan(track * 256 + row * 4, 4);
m->note = data[0] & 0x3F;
if(m->note)
m->note += 47 + NOTE_MIN;
else
m->note = NOTE_NONE;
m->instr = (data[0] >> 6) | ((data[1] & 0x0F) << 2);
const uint8 command = (data[1] >> 6) | ((data[2] & 0x0F) << 2);
const uint16 param = (data[2] >> 4) | (data[3] << 4);
if(!(allowedCommands[command >> 3u] & (1u << (command & 7u))))
continue;
if(command == 0 && param == 0)
continue;
m->param = static_cast<uint8>(param);
m->vol = static_cast<ModCommand::VOL>(param >> 8);
switch(command)
{
case 0x00: // 00 xyz Normal play or Arpeggio + Volume Slide Up
case 0x01: // 01 xyy Slide Up + Volume Slide Up
case 0x02: // 01 xyy Slide Up + Volume Slide Up
case 0x20: // 20 xyz Normal play or Arpeggio + Volume Slide Down
case 0x21: // 21 xyy Slide Up + Volume Slide Down
case 0x22: // 22 xyy Slide Down + Volume Slide Down
ConvertModCommand(*m, command & 0x0F, m->param);
if(m->vol)
m->volcmd = (command < 0x20) ? VOLCMD_VOLSLIDEUP : VOLCMD_VOLSLIDEDOWN;
break;
case 0x03: // 03 xyy Tone Portamento
case 0x04: // 04 xyz Vibrato
case 0x05: // 05 xyz Tone Portamento + Volume Slide
case 0x06: // 06 xyz Vibrato + Volume Slide
case 0x07: // 07 xyz Tremolo
case 0x0C: // 0C xyy Set Volume
ConvertModCommand(*m, command, m->param);
break;
case 0x09: // 09 xxx Set Sample Offset
m->command = CMD_OFFSET;
m->param = static_cast<ModCommand::PARAM>(param >> 1);
if(param >= 0x200)
{
m->volcmd = VOLCMD_OFFSET;
m->vol >>= 1;
}
break;
case 0x0A: // 0A xyz Volume Slide + Fine Slide Up
case 0x2A: // 2A xyz Volume Slide + Fine Slide Down
if(param < 0xFF)
{
ConvertModCommand(*m, command & 0x0F, m->param);
} else
{
m->command = CMD_MODCMDEX;
m->param = static_cast<ModCommand::PARAM>(((command < 0x20) ? 0x10 : 0x20) | (param >> 8));
if(param & 0xF0)
{
m->volcmd = VOLCMD_VOLSLIDEUP;
m->vol = static_cast<ModCommand::VOL>((param >> 4) & 0x0F);
} else
{
m->volcmd = VOLCMD_VOLSLIDEDOWN;
m->vol = static_cast<ModCommand::VOL>(param & 0x0F);
}
}
break;
case 0x0B: // 0B xxx Position Jump
case 0x0F: // 0F xxx Set Speed
m->command = (command == 0x0B) ? CMD_POSITIONJUMP : CMD_SPEED;
m->param = mpt::saturate_cast<ModCommand::PARAM>(param);
break;
case 0x0D: // 0D xyy Pattern Break (not BCD-encoded like in MOD)
m->command = CMD_PATTERNBREAK;
if(m->param > 63)
m->param = 0;
break;
case 0x10: // 10 xxy Filter Control (not implemented in Digital Symphony)
case 0x13: // 13 xxy Glissando Control
case 0x14: // 14 xxy Set Vibrato Waveform
case 0x15: // 15 xxy Set Fine Tune
case 0x17: // 17 xxy Set Tremolo Waveform
case 0x1F: // 1F xxy Invert Loop
m->command = CMD_MODCMDEX;
m->param = (command << 4) | (m->param & 0x0F);
break;
case 0x16: // 16 xxx Jump to Loop
case 0x19: // 19 xxx Retrig Note
case 0x1C: // 1C xxx Note Cut
case 0x1D: // 1D xxx Note Delay
case 0x1E: // 1E xxx Pattern Delay
m->command = CMD_MODCMDEX;
m->param = (command << 4) | static_cast<ModCommand::PARAM>(std::min(param, uint16(0x0F)));
break;
case 0x11: // 11 xyy Fine Slide Up + Fine Volume Slide Up
case 0x12: // 12 xyy Fine Slide Down + Fine Volume Slide Up
case 0x1A: // 1A xyy Fine Slide Up + Fine Volume Slide Down
case 0x1B: // 1B xyy Fine Slide Down + Fine Volume Slide Down
m->command = CMD_MODCMDEX;
if(m->param & 0xFF)
{
m->param = static_cast<ModCommand::PARAM>(((command == 0x11 || command == 0x1A) ? 0x10 : 0x20) | (param & 0x0F));
if(param & 0xF00)
m->volcmd = (command >= 0x1A) ? VOLCMD_FINEVOLDOWN : VOLCMD_FINEVOLUP;
} else
{
m->param = static_cast<ModCommand::PARAM>(((command >= 0x1A) ? 0xB0 : 0xA0) | (param >> 8));
}
break;
case 0x2F: // 2F xxx Set Tempo
if(param > 0)
{
m->command = CMD_TEMPO;
m->param = mpt::saturate_cast<ModCommand::PARAM>(std::max(8, param + 4) / 8);
#ifdef MODPLUG_TRACKER
m->param = std::max(m->param, ModCommand::PARAM(0x20));
#endif
} else
{
m->command = CMD_NONE;
}
break;
case 0x2B: // 2B xyy Line Jump
m->command = CMD_PATTERNBREAK;
for(CHANNELINDEX brkChn = 0; brkChn < GetNumChannels(); brkChn++)
{
ModCommand &cmd = *(m - chn + brkChn);
if(cmd.command != CMD_NONE)
continue;
cmd.command = CMD_POSITIONJUMP;
cmd.param = mpt::saturate_cast<ModCommand::PARAM>(pat);
}
break;
case 0x30: // 30 xxy Set Stereo
m->command = CMD_PANNING8;
if(param & 7)
{
static constexpr uint8 panning[8] = {0x00, 0x00, 0x2B, 0x56, 0x80, 0xAA, 0xD4, 0xFF};
m->param = panning[param & 7];
} else if((param >> 4) != 0x80)
{
m->param = static_cast<ModCommand::PARAM>(param >> 4);
if(m->param < 0x80)
m->param += 0x80;
else
m->param = 0xFF - m->param;
} else
{
m->command = CMD_NONE;
}
break;
case 0x32: // 32 xxx Unset Sample Repeat
m->command = CMD_NONE;
m->param = 0;
if(m->note == NOTE_NONE)
m->note = NOTE_KEYOFF;
else
m->command = CMD_KEYOFF;
break;
case 0x31: // 31 xxx Song Upcall
default:
m->command = CMD_NONE;
break;
}
}
}
}
for(SAMPLEINDEX smp = 1; smp <= m_nSamples; smp++)
{
file.ReadString<mpt::String::maybeNullTerminated>(m_szNames[smp], sampleNameLength[smp] & 0x3F);
if(sampleNameLength[smp] & 0x80)
continue;
ModSample &mptSmp = Samples[smp];
mptSmp.nSustainStart = file.ReadUint24LE() << 1;
if(const auto loopLen = file.ReadUint24LE() << 1; loopLen > 2)
{
mptSmp.nSustainEnd = mptSmp.nSustainStart + loopLen;
mptSmp.uFlags.set(CHN_SUSTAINLOOP);
}
mptSmp.nVolume = std::min(file.ReadUint8(), uint8(64)) * 4u;
mptSmp.nFineTune = MOD2XMFineTune(file.ReadUint8());
mptSmp.Set16BitCuePoints();
if(!mptSmp.nLength)
continue;
const uint8 packingType = file.ReadUint8();
switch(packingType)
{
case 0: // Modified u-Law
if(loadFlags & loadSampleData)
{
std::vector<std::byte> sampleData;
if(!file.CanRead(mptSmp.nLength))
return false;
file.ReadVector(sampleData, mptSmp.nLength);
for(auto &b : sampleData)
{
uint8 v = mpt::byte_cast<uint8>(b);
v = (v << 7) | (static_cast<uint8>(~v) >> 1);
b = mpt::byte_cast<std::byte>(v);
}
FileReader sampleDataFile = FileReader(mpt::as_span(sampleData));
SampleIO(
SampleIO::_16bit,
SampleIO::mono,
SampleIO::littleEndian,
SampleIO::uLaw)
.ReadSample(mptSmp, sampleDataFile);
} else
{
file.Skip(mptSmp.nLength);
}
break;
case 1: // 13-bit LZW applied to linear sample data differences
{
std::vector<std::byte> sampleData;
try
{
sampleData = DecompressDSymLZW(file, mptSmp.nLength);
} catch(const BitReader::eof &)
{
return false;
}
if(!(loadFlags & loadSampleData))
break;
FileReader sampleDataFile = FileReader(mpt::as_span(sampleData));
SampleIO(
SampleIO::_8bit,
SampleIO::mono,
SampleIO::littleEndian,
SampleIO::deltaPCM)
.ReadSample(mptSmp, sampleDataFile);
}
break;
case 2: // 8-bit signed
case 3: // 16-bit signed
if(loadFlags & loadSampleData)
{
SampleIO(
(packingType == 2) ? SampleIO::_8bit : SampleIO::_16bit,
SampleIO::mono,
SampleIO::littleEndian,
SampleIO::signedPCM)
.ReadSample(mptSmp, file);
} else
{
file.Skip(mptSmp.nLength * (packingType - 1));
}
break;
case 4: // Sigma-Delta compression applied to linear sample differences
case 5: // Sigma-Delta compression applied to logarithmic sample differences
{
std::vector<std::byte> sampleData;
try
{
sampleData = DecompressDSymSigmaDelta(file, mptSmp.nLength);
} catch(const BitReader::eof &)
{
return false;
}
if(!(loadFlags & loadSampleData))
break;
if(packingType == 5)
{
static constexpr uint8 xorMask[] = {0x00, 0x7F};
for(auto &b : sampleData)
{
uint8 v = mpt::byte_cast<uint8>(b);
v ^= xorMask[v >> 7];
b = mpt::byte_cast<std::byte>(v);
}
}
FileReader sampleDataFile = FileReader(mpt::as_span(sampleData));
SampleIO(
(packingType == 5) ? SampleIO::_16bit : SampleIO::_8bit,
SampleIO::mono,
SampleIO::littleEndian,
(packingType == 5) ? SampleIO::uLaw : SampleIO::unsignedPCM)
.ReadSample(mptSmp, sampleDataFile);
}
break;
default:
return false;
}
}
if(const uint32 infoLen = fileHeader.infoLen.get(); infoLen > 0)
{
std::vector<std::byte> infoData;
if(!ReadDSymChunk(file, infoData, infoLen))
return false;
FileReader infoChunk = FileReader(mpt::as_span(infoData));
m_songMessage.Read(infoChunk, infoLen, SongMessage::leLF);
}
m_modFormat.formatName = MPT_UFORMAT("Digital Symphony v{}")(fileHeader.version);
m_modFormat.type = UL_("dsym"); // RISC OS doesn't use file extensions but this is a common abbreviation used for this tracker
m_modFormat.madeWithTracker = UL_("Digital Symphony");
m_modFormat.charset = mpt::Charset::RISC_OS;
return true;
}
OPENMPT_NAMESPACE_END
|