Load_mo3.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. /*
  2. * Load_mo3.cpp
  3. * ------------
  4. * Purpose: MO3 module loader.
  5. * Notes : (currently none)
  6. * Authors: Johannes Schultz / OpenMPT Devs
  7. * Based on documentation and the decompression routines from the
  8. * open-source UNMO3 project (https://github.com/lclevy/unmo3).
  9. * The modified decompression code has been relicensed to the BSD
  10. * license with permission from Laurent Clévy.
  11. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  12. */
  13. #include "stdafx.h"
  14. #include "Loaders.h"
  15. #include "../common/ComponentManager.h"
  16. #include "mpt/io/base.hpp"
  17. #include "mpt/io/io.hpp"
  18. #include "mpt/io/io_stdstream.hpp"
  19. #include "Tables.h"
  20. #include "../common/version.h"
  21. #include "mpt/audio/span.hpp"
  22. #include "MPEGFrame.h"
  23. #include "OggStream.h"
  24. #if defined(MPT_WITH_VORBIS) && defined(MPT_WITH_VORBISFILE)
  25. #include <sstream>
  26. #endif
  27. #if defined(MPT_WITH_VORBIS)
  28. #if MPT_COMPILER_CLANG
  29. #pragma clang diagnostic push
  30. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  31. #endif // MPT_COMPILER_CLANG
  32. #include <vorbis/codec.h>
  33. #if MPT_COMPILER_CLANG
  34. #pragma clang diagnostic pop
  35. #endif // MPT_COMPILER_CLANG
  36. #endif
  37. #if defined(MPT_WITH_VORBISFILE)
  38. #if MPT_COMPILER_CLANG
  39. #pragma clang diagnostic push
  40. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  41. #endif // MPT_COMPILER_CLANG
  42. #include <vorbis/vorbisfile.h>
  43. #if MPT_COMPILER_CLANG
  44. #pragma clang diagnostic pop
  45. #endif // MPT_COMPILER_CLANG
  46. #include "openmpt/soundbase/Copy.hpp"
  47. #endif
  48. #ifdef MPT_WITH_STBVORBIS
  49. #include <stb_vorbis/stb_vorbis.c>
  50. #include "openmpt/soundbase/Copy.hpp"
  51. #endif // MPT_WITH_STBVORBIS
  52. OPENMPT_NAMESPACE_BEGIN
  53. struct MO3FileHeader
  54. {
  55. enum MO3HeaderFlags
  56. {
  57. linearSlides = 0x0001,
  58. isS3M = 0x0002,
  59. s3mFastSlides = 0x0004,
  60. isMTM = 0x0008, // Actually this is simply "not XM". But if none of the S3M, MOD and IT flags are set, it's an MTM.
  61. s3mAmigaLimits = 0x0010,
  62. // 0x20 and 0x40 have been used in old versions for things that can be inferred from the file format anyway.
  63. // The official UNMO3 ignores them.
  64. isMOD = 0x0080,
  65. isIT = 0x0100,
  66. instrumentMode = 0x0200,
  67. itCompatGxx = 0x0400,
  68. itOldFX = 0x0800,
  69. modplugMode = 0x10000,
  70. unknown = 0x20000, // Always set (internal BASS flag to designate modules)
  71. modVBlank = 0x80000,
  72. hasPlugins = 0x100000,
  73. extFilterRange = 0x200000,
  74. };
  75. uint8le numChannels; // 1...64 (limited by channel panning and volume)
  76. uint16le numOrders;
  77. uint16le restartPos;
  78. uint16le numPatterns;
  79. uint16le numTracks;
  80. uint16le numInstruments;
  81. uint16le numSamples;
  82. uint8le defaultSpeed;
  83. uint8le defaultTempo;
  84. uint32le flags; // See MO3HeaderFlags
  85. uint8le globalVol; // 0...128 in IT, 0...64 in S3M
  86. uint8le panSeparation; // 0...128 in IT
  87. int8le sampleVolume; // Only used in IT
  88. uint8le chnVolume[64]; // 0...64
  89. uint8le chnPan[64]; // 0...256, 127 = surround
  90. uint8le sfxMacros[16];
  91. uint8le fixedMacros[128][2];
  92. };
  93. MPT_BINARY_STRUCT(MO3FileHeader, 422)
  94. struct MO3Envelope
  95. {
  96. enum MO3EnvelopeFlags
  97. {
  98. envEnabled = 0x01,
  99. envSustain = 0x02,
  100. envLoop = 0x04,
  101. envFilter = 0x10,
  102. envCarry = 0x20,
  103. };
  104. uint8le flags; // See MO3EnvelopeFlags
  105. uint8le numNodes;
  106. uint8le sustainStart;
  107. uint8le sustainEnd;
  108. uint8le loopStart;
  109. uint8le loopEnd;
  110. int16le points[25][2];
  111. // Convert MO3 envelope data into OpenMPT's internal envelope format
  112. void ConvertToMPT(InstrumentEnvelope &mptEnv, uint8 envShift) const
  113. {
  114. if(flags & envEnabled) mptEnv.dwFlags.set(ENV_ENABLED);
  115. if(flags & envSustain) mptEnv.dwFlags.set(ENV_SUSTAIN);
  116. if(flags & envLoop) mptEnv.dwFlags.set(ENV_LOOP);
  117. if(flags & envFilter) mptEnv.dwFlags.set(ENV_FILTER);
  118. if(flags & envCarry) mptEnv.dwFlags.set(ENV_CARRY);
  119. mptEnv.resize(std::min(numNodes.get(), uint8(25)));
  120. mptEnv.nSustainStart = sustainStart;
  121. mptEnv.nSustainEnd = sustainEnd;
  122. mptEnv.nLoopStart = loopStart;
  123. mptEnv.nLoopEnd = loopEnd;
  124. for(uint32 ev = 0; ev < mptEnv.size(); ev++)
  125. {
  126. mptEnv[ev].tick = points[ev][0];
  127. if(ev > 0 && mptEnv[ev].tick < mptEnv[ev - 1].tick)
  128. mptEnv[ev].tick = mptEnv[ev - 1].tick + 1;
  129. mptEnv[ev].value = static_cast<uint8>(Clamp(points[ev][1] >> envShift, 0, 64));
  130. }
  131. }
  132. };
  133. MPT_BINARY_STRUCT(MO3Envelope, 106)
  134. struct MO3Instrument
  135. {
  136. enum MO3InstrumentFlags
  137. {
  138. playOnMIDI = 0x01,
  139. mute = 0x02,
  140. };
  141. uint32le flags; // See MO3InstrumentFlags
  142. uint16le sampleMap[120][2];
  143. MO3Envelope volEnv;
  144. MO3Envelope panEnv;
  145. MO3Envelope pitchEnv;
  146. struct XMVibratoSettings
  147. {
  148. uint8le type;
  149. uint8le sweep;
  150. uint8le depth;
  151. uint8le rate;
  152. } vibrato; // Applies to all samples of this instrument (XM)
  153. uint16le fadeOut;
  154. uint8le midiChannel;
  155. uint8le midiBank;
  156. uint8le midiPatch;
  157. uint8le midiBend;
  158. uint8le globalVol; // 0...128
  159. uint16le panning; // 0...256 if enabled, 0xFFFF otherwise
  160. uint8le nna;
  161. uint8le pps;
  162. uint8le ppc;
  163. uint8le dct;
  164. uint8le dca;
  165. uint16le volSwing; // 0...100
  166. uint16le panSwing; // 0...256
  167. uint8le cutoff; // 0...127, + 128 if enabled
  168. uint8le resonance; // 0...127, + 128 if enabled
  169. // Convert MO3 instrument data into OpenMPT's internal instrument format
  170. void ConvertToMPT(ModInstrument &mptIns, MODTYPE type) const
  171. {
  172. if(type == MOD_TYPE_XM)
  173. {
  174. for(size_t i = 0; i < 96; i++)
  175. {
  176. mptIns.Keyboard[i + 12] = sampleMap[i][1] + 1;
  177. }
  178. } else
  179. {
  180. for(size_t i = 0; i < 120; i++)
  181. {
  182. mptIns.NoteMap[i] = static_cast<uint8>(sampleMap[i][0] + NOTE_MIN);
  183. mptIns.Keyboard[i] = sampleMap[i][1] + 1;
  184. }
  185. }
  186. volEnv.ConvertToMPT(mptIns.VolEnv, 0);
  187. panEnv.ConvertToMPT(mptIns.PanEnv, 0);
  188. pitchEnv.ConvertToMPT(mptIns.PitchEnv, 5);
  189. mptIns.nFadeOut = fadeOut;
  190. if(midiChannel >= 128)
  191. {
  192. // Plugin
  193. mptIns.nMixPlug = midiChannel - 127;
  194. } else if(midiChannel < 17 && (flags & playOnMIDI))
  195. {
  196. // XM, or IT with recent encoder
  197. mptIns.nMidiChannel = midiChannel + MidiFirstChannel;
  198. } else if(midiChannel > 0 && midiChannel < 17)
  199. {
  200. // IT encoded with MO3 version prior to 2.4.1 (yes, channel 0 is represented the same way as "no channel")
  201. mptIns.nMidiChannel = midiChannel + MidiFirstChannel;
  202. }
  203. if(mptIns.nMidiChannel != MidiNoChannel)
  204. {
  205. if(type == MOD_TYPE_XM)
  206. {
  207. mptIns.nMidiProgram = midiPatch + 1;
  208. } else
  209. {
  210. if(midiBank < 128)
  211. mptIns.wMidiBank = midiBank + 1;
  212. if(midiPatch < 128)
  213. mptIns.nMidiProgram = midiPatch + 1;
  214. }
  215. mptIns.midiPWD = midiBend;
  216. }
  217. if(type == MOD_TYPE_IT)
  218. mptIns.nGlobalVol = std::min(static_cast<uint8>(globalVol), uint8(128)) / 2u;
  219. if(panning <= 256)
  220. {
  221. mptIns.nPan = panning;
  222. mptIns.dwFlags.set(INS_SETPANNING);
  223. }
  224. mptIns.nNNA = static_cast<NewNoteAction>(nna.get());
  225. mptIns.nPPS = pps;
  226. mptIns.nPPC = ppc;
  227. mptIns.nDCT = static_cast<DuplicateCheckType>(dct.get());
  228. mptIns.nDNA = static_cast<DuplicateNoteAction>(dca.get());
  229. mptIns.nVolSwing = static_cast<uint8>(std::min(volSwing.get(), uint16(100)));
  230. mptIns.nPanSwing = static_cast<uint8>(std::min(panSwing.get(), uint16(256)) / 4u);
  231. mptIns.SetCutoff(cutoff & 0x7F, (cutoff & 0x80) != 0);
  232. mptIns.SetResonance(resonance & 0x7F, (resonance & 0x80) != 0);
  233. }
  234. };
  235. MPT_BINARY_STRUCT(MO3Instrument, 826)
  236. struct MO3Sample
  237. {
  238. enum MO3SampleFlags
  239. {
  240. smp16Bit = 0x01,
  241. smpLoop = 0x10,
  242. smpPingPongLoop = 0x20,
  243. smpSustain = 0x100,
  244. smpSustainPingPong = 0x200,
  245. smpStereo = 0x400,
  246. smpCompressionMPEG = 0x1000, // MPEG 1.0 / 2.0 / 2.5 sample
  247. smpCompressionOgg = 0x1000 | 0x2000, // Ogg sample
  248. smpSharedOgg = 0x1000 | 0x2000 | 0x4000, // Ogg sample with shared vorbis header
  249. smpDeltaCompression = 0x2000, // Deltas + compression
  250. smpDeltaPrediction = 0x4000, // Delta prediction + compression
  251. smpOPLInstrument = 0x8000, // OPL patch data
  252. smpCompressionMask = 0x1000 | 0x2000 | 0x4000 | 0x8000
  253. };
  254. uint32le freqFinetune; // Frequency in S3M and IT, finetune (0...255) in MOD, MTM, XM
  255. int8le transpose;
  256. uint8le defaultVolume; // 0...64
  257. uint16le panning; // 0...256 if enabled, 0xFFFF otherwise
  258. uint32le length;
  259. uint32le loopStart;
  260. uint32le loopEnd;
  261. uint16le flags; // See MO3SampleFlags
  262. uint8le vibType;
  263. uint8le vibSweep;
  264. uint8le vibDepth;
  265. uint8le vibRate;
  266. uint8le globalVol; // 0...64 in IT, in XM it represents the instrument number
  267. uint32le sustainStart;
  268. uint32le sustainEnd;
  269. int32le compressedSize;
  270. uint16le encoderDelay; // MP3: Ignore first n bytes of decoded output. Ogg: Shared Ogg header size
  271. // Convert MO3 sample data into OpenMPT's internal instrument format
  272. void ConvertToMPT(ModSample &mptSmp, MODTYPE type, bool frequencyIsHertz) const
  273. {
  274. mptSmp.Initialize();
  275. mptSmp.SetDefaultCuePoints();
  276. if(type & (MOD_TYPE_IT | MOD_TYPE_S3M))
  277. {
  278. if(frequencyIsHertz)
  279. mptSmp.nC5Speed = freqFinetune;
  280. else
  281. mptSmp.nC5Speed = mpt::saturate_round<uint32>(8363.0 * std::pow(2.0, static_cast<int32>(freqFinetune + 1408) / 1536.0));
  282. } else
  283. {
  284. mptSmp.nFineTune = static_cast<int8>(freqFinetune);
  285. if(type != MOD_TYPE_MTM)
  286. mptSmp.nFineTune -= 128;
  287. mptSmp.RelativeTone = transpose;
  288. }
  289. mptSmp.nVolume = std::min(defaultVolume.get(), uint8(64)) * 4u;
  290. if(panning <= 256)
  291. {
  292. mptSmp.nPan = panning;
  293. mptSmp.uFlags.set(CHN_PANNING);
  294. }
  295. mptSmp.nLength = length;
  296. mptSmp.nLoopStart = loopStart;
  297. mptSmp.nLoopEnd = loopEnd;
  298. if(flags & smpLoop)
  299. mptSmp.uFlags.set(CHN_LOOP);
  300. if(flags & smpPingPongLoop)
  301. mptSmp.uFlags.set(CHN_PINGPONGLOOP);
  302. if(flags & smpSustain)
  303. mptSmp.uFlags.set(CHN_SUSTAINLOOP);
  304. if(flags & smpSustainPingPong)
  305. mptSmp.uFlags.set(CHN_PINGPONGSUSTAIN);
  306. mptSmp.nVibType = static_cast<VibratoType>(AutoVibratoIT2XM[vibType & 7]);
  307. mptSmp.nVibSweep = vibSweep;
  308. mptSmp.nVibDepth = vibDepth;
  309. mptSmp.nVibRate = vibRate;
  310. if(type == MOD_TYPE_IT)
  311. mptSmp.nGlobalVol = std::min(static_cast<uint8>(globalVol), uint8(64));
  312. mptSmp.nSustainStart = sustainStart;
  313. mptSmp.nSustainEnd = sustainEnd;
  314. }
  315. };
  316. MPT_BINARY_STRUCT(MO3Sample, 41)
  317. // We need all this information for Ogg-compressed samples with shared headers:
  318. // A shared header can be taken from a sample that has not been read yet, so
  319. // we first need to read all headers, and then load the Ogg samples afterwards.
  320. struct MO3SampleChunk
  321. {
  322. FileReader chunk;
  323. uint16 headerSize;
  324. int16 sharedHeader;
  325. MO3SampleChunk(const FileReader &chunk_ = FileReader(), uint16 headerSize_ = 0, int16 sharedHeader_ = 0)
  326. : chunk(chunk_), headerSize(headerSize_), sharedHeader(sharedHeader_) {}
  327. };
  328. // Unpack macros
  329. // shift control bits until it is empty:
  330. // a 0 bit means literal : the next data byte is copied
  331. // a 1 means compressed data
  332. // then the next 2 bits determines what is the LZ ptr
  333. // ('00' same as previous, else stored in stream)
  334. #define READ_CTRL_BIT \
  335. data <<= 1; \
  336. carry = (data > 0xFF); \
  337. data &= 0xFF; \
  338. if(data == 0) \
  339. { \
  340. uint8 nextByte; \
  341. if(!file.Read(nextByte)) \
  342. break; \
  343. data = nextByte; \
  344. data = (data << 1) + 1; \
  345. carry = (data > 0xFF); \
  346. data &= 0xFF; \
  347. }
  348. // length coded within control stream:
  349. // most significant bit is 1
  350. // then the first bit of each bits pair (noted n1),
  351. // until second bit is 0 (noted n0)
  352. #define DECODE_CTRL_BITS \
  353. { \
  354. strLen++; \
  355. do \
  356. { \
  357. READ_CTRL_BIT; \
  358. strLen = mpt::lshift_signed(strLen, 1) + carry; \
  359. READ_CTRL_BIT; \
  360. } while(carry); \
  361. }
  362. static bool UnpackMO3Data(FileReader &file, std::vector<uint8> &uncompressed, const uint32 size)
  363. {
  364. if(!size)
  365. return false;
  366. uint16 data = 0;
  367. int8 carry = 0; // x86 carry (used to propagate the most significant bit from one byte to another)
  368. int32 strLen = 0; // length of previous string
  369. int32 strOffset; // string offset
  370. uint32 previousPtr = 0;
  371. // Read first uncompressed byte
  372. uncompressed.push_back(file.ReadUint8());
  373. uint32 remain = size - 1;
  374. while(remain > 0)
  375. {
  376. READ_CTRL_BIT;
  377. if(!carry)
  378. {
  379. // a 0 ctrl bit means 'copy', not compressed byte
  380. if(uint8 b; file.Read(b))
  381. uncompressed.push_back(b);
  382. else
  383. break;
  384. remain--;
  385. } else
  386. {
  387. // a 1 ctrl bit means compressed bytes are following
  388. uint8 lengthAdjust = 0; // length adjustment
  389. DECODE_CTRL_BITS; // read length, and if strLen > 3 (coded using more than 1 bits pair) also part of the offset value
  390. strLen -= 3;
  391. if(strLen < 0)
  392. {
  393. // means LZ ptr with same previous relative LZ ptr (saved one)
  394. strOffset = previousPtr; // restore previous Ptr
  395. strLen++;
  396. } else
  397. {
  398. // LZ ptr in ctrl stream
  399. if(uint8 b; file.Read(b))
  400. strOffset = mpt::lshift_signed(strLen, 8) | b; // read less significant offset byte from stream
  401. else
  402. break;
  403. strLen = 0;
  404. strOffset = ~strOffset;
  405. if(strOffset < -1280)
  406. lengthAdjust++;
  407. lengthAdjust++; // length is always at least 1
  408. if(strOffset < -32000)
  409. lengthAdjust++;
  410. previousPtr = strOffset; // save current Ptr
  411. }
  412. // read the next 2 bits as part of strLen
  413. READ_CTRL_BIT;
  414. strLen = mpt::lshift_signed(strLen, 1) + carry;
  415. READ_CTRL_BIT;
  416. strLen = mpt::lshift_signed(strLen, 1) + carry;
  417. if(strLen == 0)
  418. {
  419. // length does not fit in 2 bits
  420. DECODE_CTRL_BITS; // decode length: 1 is the most significant bit,
  421. strLen += 2; // then first bit of each bits pairs (noted n1), until n0.
  422. }
  423. strLen += lengthAdjust; // length adjustment
  424. if(remain < static_cast<uint32>(strLen) || strLen <= 0)
  425. break;
  426. if(strOffset >= 0 || -static_cast<ptrdiff_t>(uncompressed.size()) > strOffset)
  427. break;
  428. // Copy previous string
  429. // Need to do this in two steps as source and destination may overlap (e.g. strOffset = -1, strLen = 2 repeats last character twice)
  430. uncompressed.insert(uncompressed.end(), strLen, 0);
  431. remain -= strLen;
  432. auto src = uncompressed.cend() - strLen + strOffset;
  433. auto dst = uncompressed.end() - strLen;
  434. do
  435. {
  436. strLen--;
  437. *dst++ = *src++;
  438. } while(strLen > 0);
  439. }
  440. }
  441. #ifdef MPT_BUILD_FUZZER
  442. // When using a fuzzer, we should not care if the decompressed buffer has the correct size.
  443. // This makes finding new interesting test cases much easier.
  444. return true;
  445. #else
  446. return remain == 0;
  447. #endif // MPT_BUILD_FUZZER
  448. }
  449. struct MO3Delta8BitParams
  450. {
  451. using sample_t = int8;
  452. using unsigned_t = uint8;
  453. static constexpr int shift = 7;
  454. static constexpr uint8 dhInit = 4;
  455. static inline void Decode(FileReader &file, int8 &carry, uint16 &data, uint8 & /*dh*/, unsigned_t &val)
  456. {
  457. do
  458. {
  459. READ_CTRL_BIT;
  460. val = (val << 1) + carry;
  461. READ_CTRL_BIT;
  462. } while(carry);
  463. }
  464. };
  465. struct MO3Delta16BitParams
  466. {
  467. using sample_t = int16;
  468. using unsigned_t = uint16;
  469. static constexpr int shift = 15;
  470. static constexpr uint8 dhInit = 8;
  471. static inline void Decode(FileReader &file, int8 &carry, uint16 &data, uint8 &dh, unsigned_t &val)
  472. {
  473. if(dh < 5)
  474. {
  475. do
  476. {
  477. READ_CTRL_BIT;
  478. val = (val << 1) + carry;
  479. READ_CTRL_BIT;
  480. val = (val << 1) + carry;
  481. READ_CTRL_BIT;
  482. } while(carry);
  483. } else
  484. {
  485. do
  486. {
  487. READ_CTRL_BIT;
  488. val = (val << 1) + carry;
  489. READ_CTRL_BIT;
  490. } while(carry);
  491. }
  492. }
  493. };
  494. template <typename Properties>
  495. static void UnpackMO3DeltaSample(FileReader &file, typename Properties::sample_t *dst, uint32 length, uint8 numChannels)
  496. {
  497. uint8 dh = Properties::dhInit, cl = 0;
  498. int8 carry = 0;
  499. uint16 data = 0;
  500. typename Properties::unsigned_t val;
  501. typename Properties::sample_t previous = 0;
  502. for(uint8 chn = 0; chn < numChannels; chn++)
  503. {
  504. typename Properties::sample_t *p = dst + chn;
  505. const typename Properties::sample_t *const pEnd = p + length * numChannels;
  506. while(p < pEnd)
  507. {
  508. val = 0;
  509. Properties::Decode(file, carry, data, dh, val);
  510. cl = dh;
  511. while(cl > 0)
  512. {
  513. READ_CTRL_BIT;
  514. val = (val << 1) + carry;
  515. cl--;
  516. }
  517. cl = 1;
  518. if(val >= 4)
  519. {
  520. cl = Properties::shift;
  521. while(((1 << cl) & val) == 0 && cl > 1)
  522. cl--;
  523. }
  524. dh = dh + cl;
  525. dh >>= 1; // next length in bits of encoded delta second part
  526. carry = val & 1; // sign of delta 1=+, 0=not
  527. val >>= 1;
  528. if(carry == 0)
  529. val = ~val; // negative delta
  530. val += previous; // previous value + delta
  531. *p = val;
  532. p += numChannels;
  533. previous = val;
  534. }
  535. }
  536. }
  537. template <typename Properties>
  538. static void UnpackMO3DeltaPredictionSample(FileReader &file, typename Properties::sample_t *dst, uint32 length, uint8 numChannels)
  539. {
  540. uint8 dh = Properties::dhInit, cl = 0;
  541. int8 carry;
  542. uint16 data = 0;
  543. int32 next = 0;
  544. typename Properties::unsigned_t val = 0;
  545. typename Properties::sample_t sval = 0, delta = 0, previous = 0;
  546. for(uint8 chn = 0; chn < numChannels; chn++)
  547. {
  548. typename Properties::sample_t *p = dst + chn;
  549. const typename Properties::sample_t *const pEnd = p + length * numChannels;
  550. while(p < pEnd)
  551. {
  552. val = 0;
  553. Properties::Decode(file, carry, data, dh, val);
  554. cl = dh; // length in bits of: delta second part (right most bits of delta) and sign bit
  555. while(cl > 0)
  556. {
  557. READ_CTRL_BIT;
  558. val = (val << 1) + carry;
  559. cl--;
  560. }
  561. cl = 1;
  562. if(val >= 4)
  563. {
  564. cl = Properties::shift;
  565. while(((1 << cl) & val) == 0 && cl > 1)
  566. cl--;
  567. }
  568. dh = dh + cl;
  569. dh >>= 1; // next length in bits of encoded delta second part
  570. carry = val & 1; // sign of delta 1=+, 0=not
  571. val >>= 1;
  572. if(carry == 0)
  573. val = ~val; // negative delta
  574. delta = static_cast<typename Properties::sample_t>(val);
  575. val = val + static_cast<typename Properties::unsigned_t>(next); // predicted value + delta
  576. *p = val;
  577. p += numChannels;
  578. sval = static_cast<typename Properties::sample_t>(val);
  579. next = (sval * (1 << 1)) + (delta >> 1) - previous; // corrected next value
  580. Limit(next, std::numeric_limits<typename Properties::sample_t>::min(), std::numeric_limits<typename Properties::sample_t>::max());
  581. previous = sval;
  582. }
  583. }
  584. }
  585. #undef READ_CTRL_BIT
  586. #undef DECODE_CTRL_BITS
  587. #if defined(MPT_WITH_VORBIS) && defined(MPT_WITH_VORBISFILE)
  588. static size_t VorbisfileFilereaderRead(void *ptr, size_t size, size_t nmemb, void *datasource)
  589. {
  590. FileReader &file = *reinterpret_cast<FileReader *>(datasource);
  591. return file.ReadRaw(mpt::span(mpt::void_cast<std::byte *>(ptr), size * nmemb)).size() / size;
  592. }
  593. static int VorbisfileFilereaderSeek(void *datasource, ogg_int64_t offset, int whence)
  594. {
  595. FileReader &file = *reinterpret_cast<FileReader *>(datasource);
  596. switch(whence)
  597. {
  598. case SEEK_SET:
  599. if(!mpt::in_range<FileReader::off_t>(offset))
  600. {
  601. return -1;
  602. }
  603. return file.Seek(mpt::saturate_cast<FileReader::off_t>(offset)) ? 0 : -1;
  604. case SEEK_CUR:
  605. if(offset < 0)
  606. {
  607. if(offset == std::numeric_limits<ogg_int64_t>::min())
  608. {
  609. return -1;
  610. }
  611. if(!mpt::in_range<FileReader::off_t>(0 - offset))
  612. {
  613. return -1;
  614. }
  615. return file.SkipBack(mpt::saturate_cast<FileReader::off_t>(0 - offset)) ? 0 : -1;
  616. } else
  617. {
  618. if(!mpt::in_range<FileReader::off_t>(offset))
  619. {
  620. return -1;
  621. }
  622. return file.Skip(mpt::saturate_cast<FileReader::off_t>(offset)) ? 0 : -1;
  623. }
  624. break;
  625. case SEEK_END:
  626. if(!mpt::in_range<FileReader::off_t>(offset))
  627. {
  628. return -1;
  629. }
  630. if(!mpt::in_range<FileReader::off_t>(file.GetLength() + offset))
  631. {
  632. return -1;
  633. }
  634. return file.Seek(mpt::saturate_cast<FileReader::off_t>(file.GetLength() + offset)) ? 0 : -1;
  635. default:
  636. return -1;
  637. }
  638. }
  639. static long VorbisfileFilereaderTell(void *datasource)
  640. {
  641. FileReader &file = *reinterpret_cast<FileReader *>(datasource);
  642. FileReader::off_t result = file.GetPosition();
  643. if(!mpt::in_range<long>(result))
  644. {
  645. return -1;
  646. }
  647. return static_cast<long>(result);
  648. }
  649. #endif // MPT_WITH_VORBIS && MPT_WITH_VORBISFILE
  650. struct MO3ContainerHeader
  651. {
  652. char magic[3]; // MO3
  653. uint8le version;
  654. uint32le musicSize;
  655. };
  656. MPT_BINARY_STRUCT(MO3ContainerHeader, 8)
  657. static bool ValidateHeader(const MO3ContainerHeader &containerHeader)
  658. {
  659. if(std::memcmp(containerHeader.magic, "MO3", 3))
  660. {
  661. return false;
  662. }
  663. if(containerHeader.musicSize <= sizeof(MO3FileHeader) || containerHeader.musicSize >= uint32_max / 2u)
  664. {
  665. return false;
  666. }
  667. if(containerHeader.version > 5)
  668. {
  669. return false;
  670. }
  671. return true;
  672. }
  673. CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderMO3(MemoryFileReader file, const uint64 *pfilesize)
  674. {
  675. MO3ContainerHeader containerHeader;
  676. if(!file.ReadStruct(containerHeader))
  677. {
  678. return ProbeWantMoreData;
  679. }
  680. if(!ValidateHeader(containerHeader))
  681. {
  682. return ProbeFailure;
  683. }
  684. MPT_UNREFERENCED_PARAMETER(pfilesize);
  685. return ProbeSuccess;
  686. }
  687. bool CSoundFile::ReadMO3(FileReader &file, ModLoadingFlags loadFlags)
  688. {
  689. file.Rewind();
  690. MO3ContainerHeader containerHeader;
  691. if(!file.ReadStruct(containerHeader))
  692. {
  693. return false;
  694. }
  695. if(!ValidateHeader(containerHeader))
  696. {
  697. return false;
  698. }
  699. if(loadFlags == onlyVerifyHeader)
  700. {
  701. return true;
  702. }
  703. const uint8 version = containerHeader.version;
  704. uint32 compressedSize = uint32_max, reserveSize = 1024 * 1024; // Generous estimate based on biggest pre-v5 MO3s found in the wild (~350K music data)
  705. if(version >= 5)
  706. {
  707. // Size of compressed music chunk
  708. compressedSize = file.ReadUint32LE();
  709. if(!file.CanRead(compressedSize))
  710. return false;
  711. // Generous estimate based on highest real-world compression ratio I found in a module (~20:1)
  712. reserveSize = std::min(Util::MaxValueOfType(reserveSize) / 32u, compressedSize) * 32u;
  713. }
  714. std::vector<uint8> musicData;
  715. // We don't always reserve the whole uncompressed size as claimed by the module to guard against broken files
  716. // that e.g. claim that the uncompressed size is 1GB while the MO3 file itself is only 100 bytes.
  717. // As the LZ compression used in MO3 doesn't allow for establishing a clear upper bound for the maximum size,
  718. // this is probably the only sensible way we can prevent DoS due to huge allocations.
  719. musicData.reserve(std::min(reserveSize, containerHeader.musicSize.get()));
  720. if(!UnpackMO3Data(file, musicData, containerHeader.musicSize))
  721. {
  722. return false;
  723. }
  724. if(version >= 5)
  725. {
  726. file.Seek(12 + compressedSize);
  727. }
  728. InitializeGlobals();
  729. InitializeChannels();
  730. FileReader musicChunk(mpt::as_span(musicData));
  731. musicChunk.ReadNullString(m_songName);
  732. musicChunk.ReadNullString(m_songMessage);
  733. MO3FileHeader fileHeader;
  734. if(!musicChunk.ReadStruct(fileHeader)
  735. || fileHeader.numChannels == 0 || fileHeader.numChannels > MAX_BASECHANNELS
  736. || fileHeader.numInstruments >= MAX_INSTRUMENTS
  737. || fileHeader.numSamples >= MAX_SAMPLES)
  738. {
  739. return false;
  740. }
  741. m_nChannels = fileHeader.numChannels;
  742. Order().SetRestartPos(fileHeader.restartPos);
  743. m_nInstruments = fileHeader.numInstruments;
  744. m_nSamples = fileHeader.numSamples;
  745. m_nDefaultSpeed = fileHeader.defaultSpeed ? fileHeader.defaultSpeed : 6;
  746. m_nDefaultTempo.Set(fileHeader.defaultTempo ? fileHeader.defaultTempo : 125, 0);
  747. if(fileHeader.flags & MO3FileHeader::isIT)
  748. SetType(MOD_TYPE_IT);
  749. else if(fileHeader.flags & MO3FileHeader::isS3M)
  750. SetType(MOD_TYPE_S3M);
  751. else if(fileHeader.flags & MO3FileHeader::isMOD)
  752. SetType(MOD_TYPE_MOD);
  753. else if(fileHeader.flags & MO3FileHeader::isMTM)
  754. SetType(MOD_TYPE_MTM);
  755. else
  756. SetType(MOD_TYPE_XM);
  757. m_SongFlags.set(SONG_IMPORTED);
  758. if(fileHeader.flags & MO3FileHeader::linearSlides)
  759. m_SongFlags.set(SONG_LINEARSLIDES);
  760. if((fileHeader.flags & MO3FileHeader::s3mAmigaLimits) && m_nType == MOD_TYPE_S3M)
  761. m_SongFlags.set(SONG_AMIGALIMITS);
  762. if((fileHeader.flags & MO3FileHeader::s3mFastSlides) && m_nType == MOD_TYPE_S3M)
  763. m_SongFlags.set(SONG_FASTVOLSLIDES);
  764. if(!(fileHeader.flags & MO3FileHeader::itOldFX) && m_nType == MOD_TYPE_IT)
  765. m_SongFlags.set(SONG_ITOLDEFFECTS);
  766. if(!(fileHeader.flags & MO3FileHeader::itCompatGxx) && m_nType == MOD_TYPE_IT)
  767. m_SongFlags.set(SONG_ITCOMPATGXX);
  768. if(fileHeader.flags & MO3FileHeader::extFilterRange)
  769. m_SongFlags.set(SONG_EXFILTERRANGE);
  770. if(fileHeader.flags & MO3FileHeader::modVBlank)
  771. m_playBehaviour.set(kMODVBlankTiming);
  772. if(m_nType == MOD_TYPE_IT)
  773. m_nDefaultGlobalVolume = std::min(fileHeader.globalVol.get(), uint8(128)) * 2;
  774. else if(m_nType == MOD_TYPE_S3M)
  775. m_nDefaultGlobalVolume = std::min(fileHeader.globalVol.get(), uint8(64)) * 4;
  776. if(fileHeader.sampleVolume < 0)
  777. m_nSamplePreAmp = fileHeader.sampleVolume + 52;
  778. else
  779. m_nSamplePreAmp = static_cast<uint32>(std::exp(fileHeader.sampleVolume * 3.1 / 20.0)) + 51;
  780. // Header only has room for 64 channels, like in IT
  781. const CHANNELINDEX headerChannels = std::min(m_nChannels, CHANNELINDEX(64));
  782. for(CHANNELINDEX i = 0; i < headerChannels; i++)
  783. {
  784. if(m_nType == MOD_TYPE_IT)
  785. ChnSettings[i].nVolume = std::min(fileHeader.chnVolume[i].get(), uint8(64));
  786. if(m_nType != MOD_TYPE_XM)
  787. {
  788. if(fileHeader.chnPan[i] == 127)
  789. ChnSettings[i].dwFlags = CHN_SURROUND;
  790. else if(fileHeader.chnPan[i] == 255)
  791. ChnSettings[i].nPan = 256;
  792. else
  793. ChnSettings[i].nPan = fileHeader.chnPan[i];
  794. }
  795. }
  796. bool anyMacros = false;
  797. for(uint32 i = 0; i < 16; i++)
  798. {
  799. if(fileHeader.sfxMacros[i])
  800. anyMacros = true;
  801. }
  802. for(uint32 i = 0; i < 128; i++)
  803. {
  804. if(fileHeader.fixedMacros[i][1])
  805. anyMacros = true;
  806. }
  807. if(anyMacros)
  808. {
  809. for(uint32 i = 0; i < 16; i++)
  810. {
  811. if(fileHeader.sfxMacros[i])
  812. m_MidiCfg.SFx[i] = MPT_AFORMAT("F0F0{}z")(mpt::afmt::HEX0<2>(fileHeader.sfxMacros[i] - 1));
  813. else
  814. m_MidiCfg.SFx[i] = "";
  815. }
  816. for(uint32 i = 0; i < 128; i++)
  817. {
  818. if(fileHeader.fixedMacros[i][1])
  819. m_MidiCfg.Zxx[i] = MPT_AFORMAT("F0F0{}{}")(mpt::afmt::HEX0<2>(fileHeader.fixedMacros[i][1] - 1), mpt::afmt::HEX0<2>(fileHeader.fixedMacros[i][0].get()));
  820. else
  821. m_MidiCfg.Zxx[i] = "";
  822. }
  823. }
  824. const bool hasOrderSeparators = !(m_nType & (MOD_TYPE_MOD | MOD_TYPE_XM));
  825. ReadOrderFromFile<uint8>(Order(), musicChunk, fileHeader.numOrders, hasOrderSeparators ? 0xFF : uint16_max, hasOrderSeparators ? 0xFE : uint16_max);
  826. // Track assignments for all patterns
  827. FileReader trackChunk = musicChunk.ReadChunk(fileHeader.numPatterns * fileHeader.numChannels * sizeof(uint16));
  828. FileReader patLengthChunk = musicChunk.ReadChunk(fileHeader.numPatterns * sizeof(uint16));
  829. std::vector<FileReader> tracks(fileHeader.numTracks);
  830. for(auto &track : tracks)
  831. {
  832. uint32 len = musicChunk.ReadUint32LE();
  833. track = musicChunk.ReadChunk(len);
  834. }
  835. /*
  836. MO3 pattern commands:
  837. 01 = Note
  838. 02 = Instrument
  839. 03 = CMD_ARPEGGIO (IT, XM, S3M, MOD, MTM)
  840. 04 = CMD_PORTAMENTOUP (XM, MOD, MTM) [for formats with separate fine slides]
  841. 05 = CMD_PORTAMENTODOWN (XM, MOD, MTM) [for formats with separate fine slides]
  842. 06 = CMD_TONEPORTAMENTO (IT, XM, S3M, MOD, MTM) / VOLCMD_TONEPORTA (IT, XM)
  843. 07 = CMD_VIBRATO (IT, XM, S3M, MOD, MTM) / VOLCMD_VIBRATODEPTH (IT)
  844. 08 = CMD_TONEPORTAVOL (XM, MOD, MTM)
  845. 09 = CMD_VIBRATOVOL (XM, MOD, MTM)
  846. 0A = CMD_TREMOLO (IT, XM, S3M, MOD, MTM)
  847. 0B = CMD_PANNING8 (IT, XM, S3M, MOD, MTM) / VOLCMD_PANNING (IT, XM)
  848. 0C = CMD_OFFSET (IT, XM, S3M, MOD, MTM)
  849. 0D = CMD_VOLUMESLIDE (XM, MOD, MTM)
  850. 0E = CMD_POSITIONJUMP (IT, XM, S3M, MOD, MTM)
  851. 0F = CMD_VOLUME (XM, MOD, MTM) / VOLCMD_VOLUME (IT, XM, S3M)
  852. 10 = CMD_PATTERNBREAK (IT, XM, MOD, MTM) - BCD-encoded in MOD/XM/S3M/MTM!
  853. 11 = CMD_MODCMDEX (XM, MOD, MTM)
  854. 12 = CMD_TEMPO (XM, MOD, MTM) / CMD_SPEED (XM, MOD, MTM)
  855. 13 = CMD_TREMOR (XM)
  856. 14 = VOLCMD_VOLSLIDEUP x=X0 (XM) / VOLCMD_VOLSLIDEDOWN x=0X (XM)
  857. 15 = VOLCMD_FINEVOLUP x=X0 (XM) / VOLCMD_FINEVOLDOWN x=0X (XM)
  858. 16 = CMD_GLOBALVOLUME (IT, XM, S3M)
  859. 17 = CMD_GLOBALVOLSLIDE (XM)
  860. 18 = CMD_KEYOFF (XM)
  861. 19 = CMD_SETENVPOSITION (XM)
  862. 1A = CMD_PANNINGSLIDE (XM)
  863. 1B = VOLCMD_PANSLIDELEFT x=0X (XM) / VOLCMD_PANSLIDERIGHT x=X0 (XM)
  864. 1C = CMD_RETRIG (XM)
  865. 1D = CMD_XFINEPORTAUPDOWN X1x (XM)
  866. 1E = CMD_XFINEPORTAUPDOWN X2x (XM)
  867. 1F = VOLCMD_VIBRATOSPEED (XM)
  868. 20 = VOLCMD_VIBRATODEPTH (XM)
  869. 21 = CMD_SPEED (IT, S3M)
  870. 22 = CMD_VOLUMESLIDE (IT, S3M)
  871. 23 = CMD_PORTAMENTODOWN (IT, S3M) [for formats without separate fine slides]
  872. 24 = CMD_PORTAMENTOUP (IT, S3M) [for formats without separate fine slides]
  873. 25 = CMD_TREMOR (IT, S3M)
  874. 26 = CMD_RETRIG (IT, S3M)
  875. 27 = CMD_FINEVIBRATO (IT, S3M)
  876. 28 = CMD_CHANNELVOLUME (IT, S3M)
  877. 29 = CMD_CHANNELVOLSLIDE (IT, S3M)
  878. 2A = CMD_PANNINGSLIDE (IT, S3M)
  879. 2B = CMD_S3MCMDEX (IT, S3M)
  880. 2C = CMD_TEMPO (IT, S3M)
  881. 2D = CMD_GLOBALVOLSLIDE (IT, S3M)
  882. 2E = CMD_PANBRELLO (IT, XM, S3M)
  883. 2F = CMD_MIDI (IT, XM, S3M)
  884. 30 = VOLCMD_FINEVOLUP x=0...9 (IT) / VOLCMD_FINEVOLDOWN x=10...19 (IT) / VOLCMD_VOLSLIDEUP x=20...29 (IT) / VOLCMD_VOLSLIDEDOWN x=30...39 (IT)
  885. 31 = VOLCMD_PORTADOWN (IT)
  886. 32 = VOLCMD_PORTAUP (IT)
  887. 33 = Unused XM command "W" (XM)
  888. 34 = Any other IT volume column command to support OpenMPT extensions (IT)
  889. 35 = CMD_XPARAM (IT)
  890. 36 = CMD_SMOOTHMIDI (IT)
  891. 37 = CMD_DELAYCUT (IT)
  892. 38 = CMD_FINETUNE (MPTM)
  893. 39 = CMD_FINETUNE_SMOOTH (MPTM)
  894. Note: S3M/IT CMD_TONEPORTAVOL / CMD_VIBRATOVOL are encoded as two commands:
  895. K= 07 00 22 x
  896. L= 06 00 22 x
  897. */
  898. static constexpr ModCommand::COMMAND effTrans[] =
  899. {
  900. CMD_NONE, CMD_NONE, CMD_NONE, CMD_ARPEGGIO,
  901. CMD_PORTAMENTOUP, CMD_PORTAMENTODOWN, CMD_TONEPORTAMENTO, CMD_VIBRATO,
  902. CMD_TONEPORTAVOL, CMD_VIBRATOVOL, CMD_TREMOLO, CMD_PANNING8,
  903. CMD_OFFSET, CMD_VOLUMESLIDE, CMD_POSITIONJUMP, CMD_VOLUME,
  904. CMD_PATTERNBREAK, CMD_MODCMDEX, CMD_TEMPO, CMD_TREMOR,
  905. VOLCMD_VOLSLIDEUP, VOLCMD_FINEVOLUP, CMD_GLOBALVOLUME, CMD_GLOBALVOLSLIDE,
  906. CMD_KEYOFF, CMD_SETENVPOSITION, CMD_PANNINGSLIDE, VOLCMD_PANSLIDELEFT,
  907. CMD_RETRIG, CMD_XFINEPORTAUPDOWN, CMD_XFINEPORTAUPDOWN, VOLCMD_VIBRATOSPEED,
  908. VOLCMD_VIBRATODEPTH, CMD_SPEED, CMD_VOLUMESLIDE, CMD_PORTAMENTODOWN,
  909. CMD_PORTAMENTOUP, CMD_TREMOR, CMD_RETRIG, CMD_FINEVIBRATO,
  910. CMD_CHANNELVOLUME, CMD_CHANNELVOLSLIDE, CMD_PANNINGSLIDE, CMD_S3MCMDEX,
  911. CMD_TEMPO, CMD_GLOBALVOLSLIDE, CMD_PANBRELLO, CMD_MIDI,
  912. VOLCMD_FINEVOLUP, VOLCMD_PORTADOWN, VOLCMD_PORTAUP, CMD_NONE,
  913. VOLCMD_OFFSET, CMD_XPARAM, CMD_SMOOTHMIDI, CMD_DELAYCUT,
  914. CMD_FINETUNE, CMD_FINETUNE_SMOOTH,
  915. };
  916. uint8 noteOffset = NOTE_MIN;
  917. if(m_nType == MOD_TYPE_MTM)
  918. noteOffset = 13 + NOTE_MIN;
  919. else if(m_nType != MOD_TYPE_IT)
  920. noteOffset = 12 + NOTE_MIN;
  921. bool onlyAmigaNotes = true;
  922. if(loadFlags & loadPatternData)
  923. Patterns.ResizeArray(fileHeader.numPatterns);
  924. for(PATTERNINDEX pat = 0; pat < fileHeader.numPatterns; pat++)
  925. {
  926. const ROWINDEX numRows = patLengthChunk.ReadUint16LE();
  927. if(!(loadFlags & loadPatternData) || !Patterns.Insert(pat, numRows))
  928. continue;
  929. for(CHANNELINDEX chn = 0; chn < fileHeader.numChannels; chn++)
  930. {
  931. uint16 trackIndex = trackChunk.ReadUint16LE();
  932. if(trackIndex >= tracks.size())
  933. continue;
  934. FileReader &track = tracks[trackIndex];
  935. track.Rewind();
  936. ROWINDEX row = 0;
  937. ModCommand *patData = Patterns[pat].GetpModCommand(0, chn);
  938. while(row < numRows)
  939. {
  940. const uint8 b = track.ReadUint8();
  941. if(!b)
  942. break;
  943. const uint8 numCommands = (b & 0x0F), rep = (b >> 4);
  944. ModCommand m = ModCommand::Empty();
  945. for(uint8 c = 0; c < numCommands; c++)
  946. {
  947. uint8 cmd[2];
  948. track.ReadArray(cmd);
  949. // Import pattern commands
  950. switch(cmd[0])
  951. {
  952. case 0x01:
  953. // Note
  954. m.note = cmd[1];
  955. if(m.note < 120)
  956. m.note += noteOffset;
  957. else if(m.note == 0xFF)
  958. m.note = NOTE_KEYOFF;
  959. else if(m.note == 0xFE)
  960. m.note = NOTE_NOTECUT;
  961. else
  962. m.note = NOTE_FADE;
  963. if(!m.IsAmigaNote())
  964. onlyAmigaNotes = false;
  965. break;
  966. case 0x02:
  967. // Instrument
  968. m.instr = cmd[1] + 1;
  969. break;
  970. case 0x06:
  971. // Tone portamento
  972. if(m.volcmd == VOLCMD_NONE && m_nType == MOD_TYPE_XM && !(cmd[1] & 0x0F))
  973. {
  974. m.volcmd = VOLCMD_TONEPORTAMENTO;
  975. m.vol = cmd[1] >> 4;
  976. break;
  977. } else if(m.volcmd == VOLCMD_NONE && m_nType == MOD_TYPE_IT)
  978. {
  979. for(uint8 i = 0; i < 10; i++)
  980. {
  981. if(ImpulseTrackerPortaVolCmd[i] == cmd[1])
  982. {
  983. m.volcmd = VOLCMD_TONEPORTAMENTO;
  984. m.vol = i;
  985. break;
  986. }
  987. }
  988. if(m.volcmd != VOLCMD_NONE)
  989. break;
  990. }
  991. m.command = CMD_TONEPORTAMENTO;
  992. m.param = cmd[1];
  993. break;
  994. case 0x07:
  995. // Vibrato
  996. if(m.volcmd == VOLCMD_NONE && cmd[1] < 10 && m_nType == MOD_TYPE_IT)
  997. {
  998. m.volcmd = VOLCMD_VIBRATODEPTH;
  999. m.vol = cmd[1];
  1000. } else
  1001. {
  1002. m.command = CMD_VIBRATO;
  1003. m.param = cmd[1];
  1004. }
  1005. break;
  1006. case 0x0B:
  1007. // Panning
  1008. if(m.volcmd == VOLCMD_NONE)
  1009. {
  1010. if(m_nType == MOD_TYPE_IT && cmd[1] == 0xFF)
  1011. {
  1012. m.volcmd = VOLCMD_PANNING;
  1013. m.vol = 64;
  1014. break;
  1015. }
  1016. if((m_nType == MOD_TYPE_IT && !(cmd[1] & 0x03))
  1017. || (m_nType == MOD_TYPE_XM && !(cmd[1] & 0x0F)))
  1018. {
  1019. m.volcmd = VOLCMD_PANNING;
  1020. m.vol = cmd[1] / 4;
  1021. break;
  1022. }
  1023. }
  1024. m.command = CMD_PANNING8;
  1025. m.param = cmd[1];
  1026. break;
  1027. case 0x0F:
  1028. // Volume
  1029. if(m_nType != MOD_TYPE_MOD && m.volcmd == VOLCMD_NONE && cmd[1] <= 64)
  1030. {
  1031. m.volcmd = VOLCMD_VOLUME;
  1032. m.vol = cmd[1];
  1033. } else
  1034. {
  1035. m.command = CMD_VOLUME;
  1036. m.param = cmd[1];
  1037. }
  1038. break;
  1039. case 0x10:
  1040. // Pattern break
  1041. m.command = CMD_PATTERNBREAK;
  1042. m.param = cmd[1];
  1043. if(m_nType != MOD_TYPE_IT)
  1044. m.param = ((m.param >> 4) * 10) + (m.param & 0x0F);
  1045. break;
  1046. case 0x12:
  1047. // Combined Tempo / Speed command
  1048. m.param = cmd[1];
  1049. if(m.param < 0x20)
  1050. m.command = CMD_SPEED;
  1051. else
  1052. m.command = CMD_TEMPO;
  1053. break;
  1054. case 0x14:
  1055. case 0x15:
  1056. // XM volume column volume slides
  1057. if(cmd[1] & 0xF0)
  1058. {
  1059. m.volcmd = static_cast<ModCommand::VOLCMD>((cmd[0] == 0x14) ? VOLCMD_VOLSLIDEUP : VOLCMD_FINEVOLUP);
  1060. m.vol = cmd[1] >> 4;
  1061. } else
  1062. {
  1063. m.volcmd = static_cast<ModCommand::VOLCMD>((cmd[0] == 0x14) ? VOLCMD_VOLSLIDEDOWN : VOLCMD_FINEVOLDOWN);
  1064. m.vol = cmd[1] & 0x0F;
  1065. }
  1066. break;
  1067. case 0x1B:
  1068. // XM volume column panning slides
  1069. if(cmd[1] & 0xF0)
  1070. {
  1071. m.volcmd = VOLCMD_PANSLIDERIGHT;
  1072. m.vol = cmd[1] >> 4;
  1073. } else
  1074. {
  1075. m.volcmd = VOLCMD_PANSLIDELEFT;
  1076. m.vol = cmd[1] & 0x0F;
  1077. }
  1078. break;
  1079. case 0x1D:
  1080. // XM extra fine porta up
  1081. m.command = CMD_XFINEPORTAUPDOWN;
  1082. m.param = 0x10 | cmd[1];
  1083. break;
  1084. case 0x1E:
  1085. // XM extra fine porta down
  1086. m.command = CMD_XFINEPORTAUPDOWN;
  1087. m.param = 0x20 | cmd[1];
  1088. break;
  1089. case 0x1F:
  1090. case 0x20:
  1091. // XM volume column vibrato
  1092. m.volcmd = effTrans[cmd[0]];
  1093. m.vol = cmd[1];
  1094. break;
  1095. case 0x22:
  1096. // IT / S3M volume slide
  1097. if(m.command == CMD_TONEPORTAMENTO)
  1098. m.command = CMD_TONEPORTAVOL;
  1099. else if(m.command == CMD_VIBRATO)
  1100. m.command = CMD_VIBRATOVOL;
  1101. else
  1102. m.command = CMD_VOLUMESLIDE;
  1103. m.param = cmd[1];
  1104. break;
  1105. case 0x30:
  1106. // IT volume column volume slides
  1107. m.vol = cmd[1] % 10;
  1108. if(cmd[1] < 10)
  1109. m.volcmd = VOLCMD_FINEVOLUP;
  1110. else if(cmd[1] < 20)
  1111. m.volcmd = VOLCMD_FINEVOLDOWN;
  1112. else if(cmd[1] < 30)
  1113. m.volcmd = VOLCMD_VOLSLIDEUP;
  1114. else if(cmd[1] < 40)
  1115. m.volcmd = VOLCMD_VOLSLIDEDOWN;
  1116. break;
  1117. case 0x31:
  1118. case 0x32:
  1119. // IT volume column portamento
  1120. m.volcmd = effTrans[cmd[0]];
  1121. m.vol = cmd[1];
  1122. break;
  1123. case 0x34:
  1124. // Any unrecognized IT volume command
  1125. if(cmd[1] >= 223 && cmd[1] <= 232)
  1126. {
  1127. m.volcmd = VOLCMD_OFFSET;
  1128. m.vol = cmd[1] - 223;
  1129. }
  1130. break;
  1131. default:
  1132. if(cmd[0] < std::size(effTrans))
  1133. {
  1134. m.command = effTrans[cmd[0]];
  1135. m.param = cmd[1];
  1136. }
  1137. break;
  1138. }
  1139. }
  1140. #ifdef MODPLUG_TRACKER
  1141. if(m_nType == MOD_TYPE_MTM)
  1142. m.Convert(MOD_TYPE_MTM, MOD_TYPE_S3M, *this);
  1143. #endif
  1144. ROWINDEX targetRow = std::min(row + rep, numRows);
  1145. while(row < targetRow)
  1146. {
  1147. *patData = m;
  1148. patData += fileHeader.numChannels;
  1149. row++;
  1150. }
  1151. }
  1152. }
  1153. }
  1154. if(GetType() == MOD_TYPE_MOD && GetNumChannels() == 4 && onlyAmigaNotes)
  1155. {
  1156. m_SongFlags.set(SONG_AMIGALIMITS | SONG_ISAMIGA);
  1157. }
  1158. const bool isSampleMode = (m_nType != MOD_TYPE_XM && !(fileHeader.flags & MO3FileHeader::instrumentMode));
  1159. std::vector<MO3Instrument::XMVibratoSettings> instrVibrato(m_nType == MOD_TYPE_XM ? m_nInstruments : 0);
  1160. for(INSTRUMENTINDEX ins = 1; ins <= m_nInstruments; ins++)
  1161. {
  1162. ModInstrument *pIns = nullptr;
  1163. if(isSampleMode || (pIns = AllocateInstrument(ins)) == nullptr)
  1164. {
  1165. // Even in IT sample mode, instrument headers are still stored....
  1166. while(musicChunk.ReadUint8() != 0)
  1167. ;
  1168. if(version >= 5)
  1169. {
  1170. while(musicChunk.ReadUint8() != 0)
  1171. ;
  1172. }
  1173. musicChunk.Skip(sizeof(MO3Instrument));
  1174. continue;
  1175. }
  1176. std::string name;
  1177. musicChunk.ReadNullString(name);
  1178. pIns->name = name;
  1179. if(version >= 5)
  1180. {
  1181. musicChunk.ReadNullString(name);
  1182. pIns->filename = name;
  1183. }
  1184. MO3Instrument insHeader;
  1185. if(!musicChunk.ReadStruct(insHeader))
  1186. break;
  1187. insHeader.ConvertToMPT(*pIns, m_nType);
  1188. if(m_nType == MOD_TYPE_XM)
  1189. instrVibrato[ins - 1] = insHeader.vibrato;
  1190. }
  1191. if(isSampleMode)
  1192. m_nInstruments = 0;
  1193. std::vector<MO3SampleChunk> sampleChunks(m_nSamples);
  1194. const bool frequencyIsHertz = (version >= 5 || !(fileHeader.flags & MO3FileHeader::linearSlides));
  1195. bool unsupportedSamples = false;
  1196. for(SAMPLEINDEX smp = 1; smp <= m_nSamples; smp++)
  1197. {
  1198. ModSample &sample = Samples[smp];
  1199. std::string name;
  1200. musicChunk.ReadNullString(name);
  1201. m_szNames[smp] = name;
  1202. if(version >= 5)
  1203. {
  1204. musicChunk.ReadNullString(name);
  1205. sample.filename = name;
  1206. }
  1207. MO3Sample smpHeader;
  1208. if(!musicChunk.ReadStruct(smpHeader))
  1209. break;
  1210. smpHeader.ConvertToMPT(sample, m_nType, frequencyIsHertz);
  1211. int16 sharedOggHeader = 0;
  1212. if(version >= 5 && (smpHeader.flags & MO3Sample::smpCompressionMask) == MO3Sample::smpSharedOgg)
  1213. {
  1214. sharedOggHeader = musicChunk.ReadInt16LE();
  1215. }
  1216. if(!(loadFlags & loadSampleData))
  1217. continue;
  1218. const uint32 compression = (smpHeader.flags & MO3Sample::smpCompressionMask);
  1219. if(!compression && smpHeader.compressedSize == 0)
  1220. {
  1221. // Uncompressed sample
  1222. SampleIO(
  1223. (smpHeader.flags & MO3Sample::smp16Bit) ? SampleIO::_16bit : SampleIO::_8bit,
  1224. (smpHeader.flags & MO3Sample::smpStereo) ? SampleIO::stereoSplit : SampleIO::mono,
  1225. SampleIO::littleEndian,
  1226. SampleIO::signedPCM)
  1227. .ReadSample(Samples[smp], file);
  1228. } else if(smpHeader.compressedSize < 0 && (smp + smpHeader.compressedSize) > 0)
  1229. {
  1230. // Duplicate sample
  1231. sample.CopyWaveform(Samples[smp + smpHeader.compressedSize]);
  1232. } else if(smpHeader.compressedSize > 0)
  1233. {
  1234. if(smpHeader.flags & MO3Sample::smp16Bit)
  1235. sample.uFlags.set(CHN_16BIT);
  1236. if(smpHeader.flags & MO3Sample::smpStereo)
  1237. sample.uFlags.set(CHN_STEREO);
  1238. FileReader sampleData = file.ReadChunk(smpHeader.compressedSize);
  1239. const uint8 numChannels = sample.GetNumChannels();
  1240. if(compression == MO3Sample::smpDeltaCompression || compression == MO3Sample::smpDeltaPrediction)
  1241. {
  1242. // In the best case, MO3 compression represents each sample point as two bits.
  1243. // As a result, if we have a file length of n, we know that the sample can be at most n*4 sample points long.
  1244. auto maxLength = sampleData.GetLength();
  1245. uint8 maxSamplesPerByte = 4 / numChannels;
  1246. if(Util::MaxValueOfType(maxLength) / maxSamplesPerByte >= maxLength)
  1247. maxLength *= maxSamplesPerByte;
  1248. else
  1249. maxLength = Util::MaxValueOfType(maxLength);
  1250. LimitMax(sample.nLength, mpt::saturate_cast<SmpLength>(maxLength));
  1251. }
  1252. if(compression == MO3Sample::smpDeltaCompression)
  1253. {
  1254. if(sample.AllocateSample())
  1255. {
  1256. if(smpHeader.flags & MO3Sample::smp16Bit)
  1257. UnpackMO3DeltaSample<MO3Delta16BitParams>(sampleData, sample.sample16(), sample.nLength, numChannels);
  1258. else
  1259. UnpackMO3DeltaSample<MO3Delta8BitParams>(sampleData, sample.sample8(), sample.nLength, numChannels);
  1260. }
  1261. } else if(compression == MO3Sample::smpDeltaPrediction)
  1262. {
  1263. if(sample.AllocateSample())
  1264. {
  1265. if(smpHeader.flags & MO3Sample::smp16Bit)
  1266. UnpackMO3DeltaPredictionSample<MO3Delta16BitParams>(sampleData, sample.sample16(), sample.nLength, numChannels);
  1267. else
  1268. UnpackMO3DeltaPredictionSample<MO3Delta8BitParams>(sampleData, sample.sample8(), sample.nLength, numChannels);
  1269. }
  1270. } else if(compression == MO3Sample::smpCompressionOgg || compression == MO3Sample::smpSharedOgg)
  1271. {
  1272. // Since shared Ogg headers can stem from a sample that has not been read yet, postpone Ogg import.
  1273. sampleChunks[smp - 1] = MO3SampleChunk(sampleData, smpHeader.encoderDelay, sharedOggHeader);
  1274. } else if(compression == MO3Sample::smpCompressionMPEG)
  1275. {
  1276. // Old MO3 encoders didn't remove LAME info frames. This is unfortunate since the encoder delay
  1277. // specified in the sample header does not take the gapless information from the LAME info frame
  1278. // into account. We should not depend on the MP3 decoder's capabilities to read or ignore such frames:
  1279. // - libmpg123 has MPG123_IGNORE_INFOFRAME but that requires API version 31 (mpg123 v1.14) or higher
  1280. // - Media Foundation does (currently) not read LAME gapless information at all
  1281. // So we just play safe and remove such frames.
  1282. FileReader mpegData(sampleData);
  1283. MPEGFrame frame(sampleData);
  1284. uint16 frameDelay = frame.numSamples * 2;
  1285. if(frame.isLAME && smpHeader.encoderDelay >= frameDelay)
  1286. {
  1287. // The info frame does not produce any output, but still counts towards the encoder delay.
  1288. smpHeader.encoderDelay -= frameDelay;
  1289. sampleData.Seek(frame.frameSize);
  1290. mpegData = sampleData.ReadChunk(sampleData.BytesLeft());
  1291. }
  1292. if(ReadMP3Sample(smp, mpegData, true, true) || ReadMediaFoundationSample(smp, mpegData, true))
  1293. {
  1294. if(smpHeader.encoderDelay > 0 && smpHeader.encoderDelay < sample.GetSampleSizeInBytes())
  1295. {
  1296. SmpLength delay = smpHeader.encoderDelay / sample.GetBytesPerSample();
  1297. memmove(sample.sampleb(), sample.sampleb() + smpHeader.encoderDelay, sample.GetSampleSizeInBytes() - smpHeader.encoderDelay);
  1298. sample.nLength -= delay;
  1299. }
  1300. LimitMax(sample.nLength, smpHeader.length);
  1301. } else
  1302. {
  1303. unsupportedSamples = true;
  1304. }
  1305. } else if(compression == MO3Sample::smpOPLInstrument)
  1306. {
  1307. OPLPatch patch;
  1308. if(sampleData.ReadArray(patch))
  1309. {
  1310. sample.SetAdlib(true, patch);
  1311. }
  1312. } else
  1313. {
  1314. unsupportedSamples = true;
  1315. }
  1316. }
  1317. }
  1318. // Now we can load Ogg samples with shared headers.
  1319. if(loadFlags & loadSampleData)
  1320. {
  1321. for(SAMPLEINDEX smp = 1; smp <= m_nSamples; smp++)
  1322. {
  1323. MO3SampleChunk &sampleChunk = sampleChunks[smp - 1];
  1324. // Is this an Ogg sample?
  1325. if(!sampleChunk.chunk.IsValid())
  1326. continue;
  1327. SAMPLEINDEX sharedOggHeader = smp + sampleChunk.sharedHeader;
  1328. // Which chunk are we going to read the header from?
  1329. // Note: Every Ogg stream has a unique serial number.
  1330. // stb_vorbis (currently) ignores this serial number so we can just stitch
  1331. // together our sample without adjusting the shared header's serial number.
  1332. const bool sharedHeader = sharedOggHeader != smp && sharedOggHeader > 0 && sharedOggHeader <= m_nSamples && sampleChunk.headerSize > 0;
  1333. #if defined(MPT_WITH_VORBIS) && defined(MPT_WITH_VORBISFILE)
  1334. std::vector<char> mergedData;
  1335. if(sharedHeader)
  1336. {
  1337. // Prepend the shared header to the actual sample data and adjust bitstream serial numbers.
  1338. // We do not handle multiple muxed logical streams as they do not exist in practice in mo3.
  1339. // We assume sequence numbers are consecutive at the end of the headers.
  1340. // Corrupted pages get dropped as required by Ogg spec. We cannot do any further sane parsing on them anyway.
  1341. // We do not match up multiple muxed stream properly as this would need parsing of actual packet data to determine or guess the codec.
  1342. // Ogg Vorbis files may contain at least an additional Ogg Skeleton stream. It is not clear whether these actually exist in MO3.
  1343. // We do not validate packet structure or logical bitstream structure (i.e. sequence numbers and granule positions).
  1344. // TODO: At least handle Skeleton streams here, as they violate our stream ordering assumptions here.
  1345. #if 0
  1346. // This block may still turn out to be useful as it does a more thourough validation of the stream than the optimized version below.
  1347. // We copy the whole data into a single consecutive buffer in order to keep things simple when interfacing libvorbisfile.
  1348. // We could in theory only adjust the header and pass 2 chunks to libvorbisfile.
  1349. // Another option would be to demux both chunks on our own (or using libogg) and pass the raw packet data to libvorbis directly.
  1350. std::ostringstream mergedStream(std::ios::binary);
  1351. mergedStream.imbue(std::locale::classic());
  1352. sampleChunks[sharedOggHeader - 1].chunk.Rewind();
  1353. FileReader sharedChunk = sampleChunks[sharedOggHeader - 1].chunk.ReadChunk(sampleChunk.headerSize);
  1354. sharedChunk.Rewind();
  1355. std::vector<uint32> streamSerials;
  1356. Ogg::PageInfo oggPageInfo;
  1357. std::vector<uint8> oggPageData;
  1358. streamSerials.clear();
  1359. while(Ogg::ReadPageAndSkipJunk(sharedChunk, oggPageInfo, oggPageData))
  1360. {
  1361. auto it = std::find(streamSerials.begin(), streamSerials.end(), oggPageInfo.header.bitstream_serial_number);
  1362. if(it == streamSerials.end())
  1363. {
  1364. streamSerials.push_back(oggPageInfo.header.bitstream_serial_number);
  1365. it = streamSerials.begin() + (streamSerials.size() - 1);
  1366. }
  1367. uint32 newSerial = it - streamSerials.begin() + 1;
  1368. oggPageInfo.header.bitstream_serial_number = newSerial;
  1369. Ogg::UpdatePageCRC(oggPageInfo, oggPageData);
  1370. Ogg::WritePage(mergedStream, oggPageInfo, oggPageData);
  1371. }
  1372. streamSerials.clear();
  1373. while(Ogg::ReadPageAndSkipJunk(sampleChunk.chunk, oggPageInfo, oggPageData))
  1374. {
  1375. auto it = std::find(streamSerials.begin(), streamSerials.end(), oggPageInfo.header.bitstream_serial_number);
  1376. if(it == streamSerials.end())
  1377. {
  1378. streamSerials.push_back(oggPageInfo.header.bitstream_serial_number);
  1379. it = streamSerials.begin() + (streamSerials.size() - 1);
  1380. }
  1381. uint32 newSerial = it - streamSerials.begin() + 1;
  1382. oggPageInfo.header.bitstream_serial_number = newSerial;
  1383. Ogg::UpdatePageCRC(oggPageInfo, oggPageData);
  1384. Ogg::WritePage(mergedStream, oggPageInfo, oggPageData);
  1385. }
  1386. std::string mergedStreamData = mergedStream.str();
  1387. mergedData.insert(mergedData.end(), mergedStreamData.begin(), mergedStreamData.end());
  1388. #else
  1389. // We assume same ordering of streams in both header and data if
  1390. // multiple streams are present.
  1391. std::ostringstream mergedStream(std::ios::binary);
  1392. mergedStream.imbue(std::locale::classic());
  1393. sampleChunks[sharedOggHeader - 1].chunk.Rewind();
  1394. FileReader sharedChunk = sampleChunks[sharedOggHeader - 1].chunk.ReadChunk(sampleChunk.headerSize);
  1395. sharedChunk.Rewind();
  1396. std::vector<uint32> dataStreamSerials;
  1397. std::vector<uint32> headStreamSerials;
  1398. Ogg::PageInfo oggPageInfo;
  1399. std::vector<uint8> oggPageData;
  1400. // Gather bitstream serial numbers form sample data chunk
  1401. dataStreamSerials.clear();
  1402. while(Ogg::ReadPageAndSkipJunk(sampleChunk.chunk, oggPageInfo, oggPageData))
  1403. {
  1404. if(!mpt::contains(dataStreamSerials, oggPageInfo.header.bitstream_serial_number))
  1405. {
  1406. dataStreamSerials.push_back(oggPageInfo.header.bitstream_serial_number);
  1407. }
  1408. }
  1409. // Apply the data bitstream serial numbers to the header
  1410. headStreamSerials.clear();
  1411. while(Ogg::ReadPageAndSkipJunk(sharedChunk, oggPageInfo, oggPageData))
  1412. {
  1413. auto it = std::find(headStreamSerials.begin(), headStreamSerials.end(), oggPageInfo.header.bitstream_serial_number);
  1414. if(it == headStreamSerials.end())
  1415. {
  1416. headStreamSerials.push_back(oggPageInfo.header.bitstream_serial_number);
  1417. it = headStreamSerials.begin() + (headStreamSerials.size() - 1);
  1418. }
  1419. uint32 newSerial = 0;
  1420. if(dataStreamSerials.size() >= static_cast<std::size_t>(it - headStreamSerials.begin()))
  1421. {
  1422. // Found corresponding stream in data chunk.
  1423. newSerial = dataStreamSerials[it - headStreamSerials.begin()];
  1424. } else
  1425. {
  1426. // No corresponding stream in data chunk. Find a free serialno.
  1427. std::size_t extraIndex = (it - headStreamSerials.begin()) - dataStreamSerials.size();
  1428. for(newSerial = 1; newSerial < 0xffffffffu; ++newSerial)
  1429. {
  1430. if(!mpt::contains(dataStreamSerials, newSerial))
  1431. {
  1432. extraIndex -= 1;
  1433. }
  1434. if(extraIndex == 0)
  1435. {
  1436. break;
  1437. }
  1438. }
  1439. }
  1440. oggPageInfo.header.bitstream_serial_number = newSerial;
  1441. Ogg::UpdatePageCRC(oggPageInfo, oggPageData);
  1442. Ogg::WritePage(mergedStream, oggPageInfo, oggPageData);
  1443. }
  1444. if(headStreamSerials.size() > 1)
  1445. {
  1446. AddToLog(LogWarning, MPT_UFORMAT("Sample {}: Ogg Vorbis data with shared header and multiple logical bitstreams in header chunk found. This may be handled incorrectly.")(smp));
  1447. } else if(dataStreamSerials.size() > 1)
  1448. {
  1449. AddToLog(LogWarning, MPT_UFORMAT("Sample {}: Ogg Vorbis sample with shared header and multiple logical bitstreams found. This may be handled incorrectly.")(smp));
  1450. } else if((dataStreamSerials.size() == 1) && (headStreamSerials.size() == 1) && (dataStreamSerials[0] != headStreamSerials[0]))
  1451. {
  1452. AddToLog(LogInformation, MPT_UFORMAT("Sample {}: Ogg Vorbis data with shared header and different logical bitstream serials found.")(smp));
  1453. }
  1454. std::string mergedStreamData = mergedStream.str();
  1455. mergedData.insert(mergedData.end(), mergedStreamData.begin(), mergedStreamData.end());
  1456. sampleChunk.chunk.Rewind();
  1457. FileReader::PinnedView sampleChunkView = sampleChunk.chunk.GetPinnedView();
  1458. mpt::span<const char> sampleChunkViewSpan = mpt::byte_cast<mpt::span<const char>>(sampleChunkView.span());
  1459. mergedData.insert(mergedData.end(), sampleChunkViewSpan.begin(), sampleChunkViewSpan.end());
  1460. #endif
  1461. }
  1462. FileReader mergedDataChunk(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(mergedData)));
  1463. FileReader &sampleData = sharedHeader ? mergedDataChunk : sampleChunk.chunk;
  1464. FileReader &headerChunk = sampleData;
  1465. #else // !(MPT_WITH_VORBIS && MPT_WITH_VORBISFILE)
  1466. FileReader &sampleData = sampleChunk.chunk;
  1467. FileReader &headerChunk = sharedHeader ? sampleChunks[sharedOggHeader - 1].chunk : sampleData;
  1468. #if defined(MPT_WITH_STBVORBIS)
  1469. std::size_t initialRead = sharedHeader ? sampleChunk.headerSize : headerChunk.GetLength();
  1470. #endif // MPT_WITH_STBVORBIS
  1471. #endif // MPT_WITH_VORBIS && MPT_WITH_VORBISFILE
  1472. headerChunk.Rewind();
  1473. if(sharedHeader && !headerChunk.CanRead(sampleChunk.headerSize))
  1474. continue;
  1475. #if defined(MPT_WITH_VORBIS) && defined(MPT_WITH_VORBISFILE)
  1476. ov_callbacks callbacks = {
  1477. &VorbisfileFilereaderRead,
  1478. &VorbisfileFilereaderSeek,
  1479. nullptr,
  1480. &VorbisfileFilereaderTell};
  1481. OggVorbis_File vf;
  1482. MemsetZero(vf);
  1483. if(ov_open_callbacks(&sampleData, &vf, nullptr, 0, callbacks) == 0)
  1484. {
  1485. if(ov_streams(&vf) == 1)
  1486. { // we do not support chained vorbis samples
  1487. vorbis_info *vi = ov_info(&vf, -1);
  1488. if(vi && vi->rate > 0 && vi->channels > 0)
  1489. {
  1490. ModSample &sample = Samples[smp];
  1491. sample.AllocateSample();
  1492. SmpLength offset = 0;
  1493. int channels = vi->channels;
  1494. int current_section = 0;
  1495. long decodedSamples = 0;
  1496. bool eof = false;
  1497. while(!eof && offset < sample.nLength && sample.HasSampleData())
  1498. {
  1499. float **output = nullptr;
  1500. long ret = ov_read_float(&vf, &output, 1024, &current_section);
  1501. if(ret == 0)
  1502. {
  1503. eof = true;
  1504. } else if(ret < 0)
  1505. {
  1506. // stream error, just try to continue
  1507. } else
  1508. {
  1509. decodedSamples = ret;
  1510. LimitMax(decodedSamples, mpt::saturate_cast<long>(sample.nLength - offset));
  1511. if(decodedSamples > 0 && channels == sample.GetNumChannels())
  1512. {
  1513. if(sample.uFlags[CHN_16BIT])
  1514. {
  1515. CopyAudio(mpt::audio_span_interleaved(sample.sample16() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  1516. } else
  1517. {
  1518. CopyAudio(mpt::audio_span_interleaved(sample.sample8() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  1519. }
  1520. }
  1521. offset += decodedSamples;
  1522. }
  1523. }
  1524. } else
  1525. {
  1526. unsupportedSamples = true;
  1527. }
  1528. } else
  1529. {
  1530. AddToLog(LogWarning, MPT_UFORMAT("Sample {}: Unsupported Ogg Vorbis chained stream found.")(smp));
  1531. unsupportedSamples = true;
  1532. }
  1533. ov_clear(&vf);
  1534. } else
  1535. {
  1536. unsupportedSamples = true;
  1537. }
  1538. #elif defined(MPT_WITH_STBVORBIS)
  1539. // NOTE/TODO: stb_vorbis does not handle inferred negative PCM sample
  1540. // position at stream start. (See
  1541. // <https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-132000A.2>).
  1542. // This means that, for remuxed and re-aligned/cutted (at stream start)
  1543. // Vorbis files, stb_vorbis will include superfluous samples at the
  1544. // beginning. MO3 files with this property are yet to be spotted in the
  1545. // wild, thus, this behaviour is currently not problematic.
  1546. int consumed = 0, error = 0;
  1547. stb_vorbis *vorb = nullptr;
  1548. if(sharedHeader)
  1549. {
  1550. FileReader::PinnedView headChunkView = headerChunk.GetPinnedView(initialRead);
  1551. vorb = stb_vorbis_open_pushdata(mpt::byte_cast<const unsigned char *>(headChunkView.data()), mpt::saturate_cast<int>(headChunkView.size()), &consumed, &error, nullptr);
  1552. headerChunk.Skip(consumed);
  1553. }
  1554. FileReader::PinnedView sampleDataView = sampleData.GetPinnedView();
  1555. const std::byte *data = sampleDataView.data();
  1556. std::size_t dataLeft = sampleDataView.size();
  1557. if(!sharedHeader)
  1558. {
  1559. vorb = stb_vorbis_open_pushdata(mpt::byte_cast<const unsigned char *>(data), mpt::saturate_cast<int>(dataLeft), &consumed, &error, nullptr);
  1560. sampleData.Skip(consumed);
  1561. data += consumed;
  1562. dataLeft -= consumed;
  1563. }
  1564. if(vorb)
  1565. {
  1566. // Header has been read, proceed to reading the sample data
  1567. ModSample &sample = Samples[smp];
  1568. sample.AllocateSample();
  1569. SmpLength offset = 0;
  1570. while((error == VORBIS__no_error || (error == VORBIS_need_more_data && dataLeft > 0))
  1571. && offset < sample.nLength && sample.HasSampleData())
  1572. {
  1573. int channels = 0, decodedSamples = 0;
  1574. float **output;
  1575. consumed = stb_vorbis_decode_frame_pushdata(vorb, mpt::byte_cast<const unsigned char *>(data), mpt::saturate_cast<int>(dataLeft), &channels, &output, &decodedSamples);
  1576. sampleData.Skip(consumed);
  1577. data += consumed;
  1578. dataLeft -= consumed;
  1579. LimitMax(decodedSamples, mpt::saturate_cast<int>(sample.nLength - offset));
  1580. if(decodedSamples > 0 && channels == sample.GetNumChannels())
  1581. {
  1582. if(sample.uFlags[CHN_16BIT])
  1583. {
  1584. CopyAudio(mpt::audio_span_interleaved(sample.sample16() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  1585. } else
  1586. {
  1587. CopyAudio(mpt::audio_span_interleaved(sample.sample8() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  1588. }
  1589. }
  1590. offset += decodedSamples;
  1591. error = stb_vorbis_get_error(vorb);
  1592. }
  1593. stb_vorbis_close(vorb);
  1594. } else
  1595. {
  1596. unsupportedSamples = true;
  1597. }
  1598. #else // !VORBIS
  1599. unsupportedSamples = true;
  1600. #endif // VORBIS
  1601. }
  1602. }
  1603. if(m_nType == MOD_TYPE_XM)
  1604. {
  1605. // Transfer XM instrument vibrato to samples
  1606. for(INSTRUMENTINDEX ins = 0; ins < m_nInstruments; ins++)
  1607. {
  1608. PropagateXMAutoVibrato(ins + 1, static_cast<VibratoType>(instrVibrato[ins].type.get()), instrVibrato[ins].sweep, instrVibrato[ins].depth, instrVibrato[ins].rate);
  1609. }
  1610. }
  1611. if((fileHeader.flags & MO3FileHeader::hasPlugins) && musicChunk.CanRead(1))
  1612. {
  1613. // Plugin data
  1614. uint8 pluginFlags = musicChunk.ReadUint8();
  1615. if(pluginFlags & 1)
  1616. {
  1617. // Channel plugins
  1618. for(CHANNELINDEX chn = 0; chn < m_nChannels; chn++)
  1619. {
  1620. ChnSettings[chn].nMixPlugin = static_cast<PLUGINDEX>(musicChunk.ReadUint32LE());
  1621. }
  1622. }
  1623. while(musicChunk.CanRead(1))
  1624. {
  1625. PLUGINDEX plug = musicChunk.ReadUint8();
  1626. if(!plug)
  1627. break;
  1628. FileReader pluginChunk = musicChunk.ReadChunk(musicChunk.ReadUint32LE());
  1629. #ifndef NO_PLUGINS
  1630. if(plug <= MAX_MIXPLUGINS)
  1631. {
  1632. ReadMixPluginChunk(pluginChunk, m_MixPlugins[plug - 1]);
  1633. }
  1634. #endif // NO_PLUGINS
  1635. }
  1636. }
  1637. mpt::ustring madeWithTracker;
  1638. uint16 cwtv = 0;
  1639. uint16 cmwt = 0;
  1640. while(musicChunk.CanRead(8))
  1641. {
  1642. uint32 id = musicChunk.ReadUint32LE();
  1643. uint32 len = musicChunk.ReadUint32LE();
  1644. FileReader chunk = musicChunk.ReadChunk(len);
  1645. switch(id)
  1646. {
  1647. case MagicLE("VERS"):
  1648. // Tracker magic bytes (depending on format)
  1649. switch(m_nType)
  1650. {
  1651. case MOD_TYPE_IT:
  1652. cwtv = chunk.ReadUint16LE();
  1653. cmwt = chunk.ReadUint16LE();
  1654. /*switch(cwtv >> 12)
  1655. {
  1656. }*/
  1657. break;
  1658. case MOD_TYPE_S3M:
  1659. cwtv = chunk.ReadUint16LE();
  1660. break;
  1661. case MOD_TYPE_XM:
  1662. chunk.ReadString<mpt::String::spacePadded>(madeWithTracker, mpt::Charset::CP437, std::min(FileReader::off_t(32), chunk.GetLength()));
  1663. break;
  1664. case MOD_TYPE_MTM:
  1665. {
  1666. uint8 mtmVersion = chunk.ReadUint8();
  1667. madeWithTracker = MPT_UFORMAT("MultiTracker {}.{}")(mtmVersion >> 4, mtmVersion & 0x0F);
  1668. }
  1669. break;
  1670. default:
  1671. break;
  1672. }
  1673. break;
  1674. case MagicLE("PRHI"):
  1675. m_nDefaultRowsPerBeat = chunk.ReadUint8();
  1676. m_nDefaultRowsPerMeasure = chunk.ReadUint8();
  1677. break;
  1678. case MagicLE("MIDI"):
  1679. // Full MIDI config
  1680. chunk.ReadStruct<MIDIMacroConfigData>(m_MidiCfg);
  1681. m_MidiCfg.Sanitize();
  1682. break;
  1683. case MagicLE("OMPT"):
  1684. // Read pattern names: "PNAM"
  1685. if(chunk.ReadMagic("PNAM"))
  1686. {
  1687. FileReader patterns = chunk.ReadChunk(chunk.ReadUint32LE());
  1688. const PATTERNINDEX namedPats = std::min(static_cast<PATTERNINDEX>(patterns.GetLength() / MAX_PATTERNNAME), Patterns.Size());
  1689. for(PATTERNINDEX pat = 0; pat < namedPats; pat++)
  1690. {
  1691. char patName[MAX_PATTERNNAME];
  1692. patterns.ReadString<mpt::String::maybeNullTerminated>(patName, MAX_PATTERNNAME);
  1693. Patterns[pat].SetName(patName);
  1694. }
  1695. }
  1696. // Read channel names: "CNAM"
  1697. if(chunk.ReadMagic("CNAM"))
  1698. {
  1699. FileReader channels = chunk.ReadChunk(chunk.ReadUint32LE());
  1700. const CHANNELINDEX namedChans = std::min(static_cast<CHANNELINDEX>(channels.GetLength() / MAX_CHANNELNAME), GetNumChannels());
  1701. for(CHANNELINDEX chn = 0; chn < namedChans; chn++)
  1702. {
  1703. channels.ReadString<mpt::String::maybeNullTerminated>(ChnSettings[chn].szName, MAX_CHANNELNAME);
  1704. }
  1705. }
  1706. LoadExtendedInstrumentProperties(chunk);
  1707. LoadExtendedSongProperties(chunk, true);
  1708. if(cwtv > 0x0889 && cwtv <= 0x8FF)
  1709. {
  1710. m_nType = MOD_TYPE_MPT;
  1711. LoadMPTMProperties(chunk, cwtv);
  1712. }
  1713. if(m_dwLastSavedWithVersion)
  1714. {
  1715. madeWithTracker = U_("OpenMPT ") + mpt::ufmt::val(m_dwLastSavedWithVersion);
  1716. }
  1717. break;
  1718. }
  1719. }
  1720. if((GetType() == MOD_TYPE_IT && cwtv >= 0x0100 && cwtv < 0x0214)
  1721. || (GetType() == MOD_TYPE_S3M && cwtv >= 0x3100 && cwtv < 0x3214)
  1722. || (GetType() == MOD_TYPE_S3M && cwtv >= 0x1300 && cwtv < 0x1320))
  1723. {
  1724. // Ignore MIDI data in files made with IT older than version 2.14 and old ST3 versions.
  1725. m_MidiCfg.ClearZxxMacros();
  1726. }
  1727. if(fileHeader.flags & MO3FileHeader::modplugMode)
  1728. {
  1729. // Apply some old ModPlug (mis-)behaviour
  1730. if(!m_dwLastSavedWithVersion)
  1731. {
  1732. // These fixes are only applied when the OpenMPT version number is not known, as otherwise the song upgrade feature will take care of it.
  1733. for(INSTRUMENTINDEX i = 1; i <= GetNumInstruments(); i++)
  1734. {
  1735. if(ModInstrument *ins = Instruments[i])
  1736. {
  1737. // Fix pitch / filter envelope being shortened by one tick (for files before v1.20)
  1738. ins->GetEnvelope(ENV_PITCH).Convert(MOD_TYPE_XM, GetType());
  1739. // Fix excessive pan swing range (for files before v1.26)
  1740. ins->nPanSwing = (ins->nPanSwing + 3) / 4u;
  1741. }
  1742. }
  1743. }
  1744. if(m_dwLastSavedWithVersion < MPT_V("1.18.00.00"))
  1745. {
  1746. m_playBehaviour.reset(kITOffset);
  1747. m_playBehaviour.reset(kFT2ST3OffsetOutOfRange);
  1748. }
  1749. if(m_dwLastSavedWithVersion < MPT_V("1.23.00.00"))
  1750. m_playBehaviour.reset(kFT2Periods);
  1751. if(m_dwLastSavedWithVersion < MPT_V("1.26.00.00"))
  1752. m_playBehaviour.reset(kITInstrWithNoteOff);
  1753. }
  1754. if(madeWithTracker.empty())
  1755. madeWithTracker = MPT_UFORMAT("MO3 v{}")(version);
  1756. else
  1757. madeWithTracker = MPT_UFORMAT("MO3 v{} ({})")(version, madeWithTracker);
  1758. m_modFormat.formatName = MPT_UFORMAT("Un4seen MO3 v{}")(version);
  1759. m_modFormat.type = U_("mo3");
  1760. switch(GetType())
  1761. {
  1762. case MOD_TYPE_MTM:
  1763. m_modFormat.originalType = U_("mtm");
  1764. m_modFormat.originalFormatName = U_("MultiTracker");
  1765. break;
  1766. case MOD_TYPE_MOD:
  1767. m_modFormat.originalType = U_("mod");
  1768. m_modFormat.originalFormatName = U_("Generic MOD");
  1769. break;
  1770. case MOD_TYPE_XM:
  1771. m_modFormat.originalType = U_("xm");
  1772. m_modFormat.originalFormatName = U_("FastTracker 2");
  1773. break;
  1774. case MOD_TYPE_S3M:
  1775. m_modFormat.originalType = U_("s3m");
  1776. m_modFormat.originalFormatName = U_("Scream Tracker 3");
  1777. break;
  1778. case MOD_TYPE_IT:
  1779. m_modFormat.originalType = U_("it");
  1780. if(cmwt)
  1781. m_modFormat.originalFormatName = MPT_UFORMAT("Impulse Tracker {}.{}")(cmwt >> 8, mpt::ufmt::hex0<2>(cmwt & 0xFF));
  1782. else
  1783. m_modFormat.originalFormatName = U_("Impulse Tracker");
  1784. break;
  1785. case MOD_TYPE_MPT:
  1786. m_modFormat.originalType = U_("mptm");
  1787. m_modFormat.originalFormatName = U_("OpenMPT MPTM");
  1788. break;
  1789. default:
  1790. MPT_ASSERT_NOTREACHED();
  1791. }
  1792. m_modFormat.madeWithTracker = std::move(madeWithTracker);
  1793. if(m_dwLastSavedWithVersion)
  1794. m_modFormat.charset = mpt::Charset::Windows1252;
  1795. else if(GetType() == MOD_TYPE_MOD)
  1796. m_modFormat.charset = mpt::Charset::Amiga_no_C1;
  1797. else
  1798. m_modFormat.charset = mpt::Charset::CP437;
  1799. if(unsupportedSamples)
  1800. {
  1801. AddToLog(LogWarning, U_("Some compressed samples could not be loaded because they use an unsupported codec."));
  1802. }
  1803. return true;
  1804. }
  1805. OPENMPT_NAMESPACE_END