| 1 |
/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 |
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */ |
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include "openmpt/all/BuildSettings.hpp" |
| 8 |
|
| 9 |
#include "mpt/base/float.hpp" |
| 10 |
#include "mpt/base/macros.hpp" |
| 11 |
#include "mpt/base/memory.hpp" |
| 12 |
#include "openmpt/base/Endian.hpp" |
| 13 |
#include "openmpt/base/Types.hpp" |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
|
| 17 |
#include <cmath> |
| 18 |
#include <cstddef> |
| 19 |
|
| 20 |
|
| 21 |
OPENMPT_NAMESPACE_BEGIN |
| 22 |
|
| 23 |
|
| 24 |
// Byte offsets, from lowest significant to highest significant byte (for various functor template parameters) |
| 25 |
#define littleEndian64 0, 1, 2, 3, 4, 5, 6, 7 |
| 26 |
#define littleEndian32 0, 1, 2, 3 |
| 27 |
#define littleEndian24 0, 1, 2 |
| 28 |
#define littleEndian16 0, 1 |
| 29 |
|
| 30 |
#define bigEndian64 7, 6, 5, 4, 3, 2, 1, 0 |
| 31 |
#define bigEndian32 3, 2, 1, 0 |
| 32 |
#define bigEndian24 2, 1, 0 |
| 33 |
#define bigEndian16 1, 0 |
| 34 |
|
| 35 |
|
| 36 |
namespace SC |
| 37 |
{ // SC = _S_ample_C_onversion |
| 38 |
|
| 39 |
|
| 40 |
// Every sample decoding functor has to typedef its input_t and output_t |
| 41 |
// and has to provide a static constexpr input_inc member |
| 42 |
// which describes by how many input_t elements inBuf has to be incremented between invocations. |
| 43 |
// input_inc is normally 1 except when decoding e.g. bigger sample values |
| 44 |
// from multiple std::byte values. |
| 45 |
|
| 46 |
|
| 47 |
struct DecodeInt8 |
| 48 |
{ |
| 49 |
using input_t = std::byte; |
| 50 |
using output_t = int8; |
| 51 |
static constexpr std::size_t input_inc = 1; |
| 52 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 53 |
{ |
| 54 |
return mpt::byte_cast<int8>(*inBuf); |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
struct DecodeUint8 |
| 59 |
{ |
| 60 |
using input_t = std::byte; |
| 61 |
using output_t = int8; |
| 62 |
static constexpr std::size_t input_inc = 1; |
| 63 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 64 |
{ |
| 65 |
return static_cast<int8>(static_cast<int>(mpt::byte_cast<uint8>(*inBuf)) - 128); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
struct DecodeInt8Delta |
| 70 |
{ |
| 71 |
using input_t = std::byte; |
| 72 |
using output_t = int8; |
| 73 |
static constexpr std::size_t input_inc = 1; |
| 74 |
uint8 delta; |
| 75 |
DecodeInt8Delta() |
| 76 |
: delta(0) |
| 77 |
{ |
| 78 |
} |
| 79 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 80 |
{ |
| 81 |
delta += mpt::byte_cast<uint8>(*inBuf); |
| 82 |
return static_cast<int8>(delta); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
struct DecodeInt16uLaw |
| 87 |
{ |
| 88 |
using input_t = std::byte; |
| 89 |
using output_t = int16; |
| 90 |
static constexpr std::size_t input_inc = 1; |
| 91 |
// clang-format off |
| 92 |
static constexpr std::array<int16, 256> uLawTable = |
| 93 |
{ |
| 94 |
-32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956, |
| 95 |
-23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764, |
| 96 |
-15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412, |
| 97 |
-11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316, |
| 98 |
-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, |
| 99 |
-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, |
| 100 |
-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, |
| 101 |
-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, |
| 102 |
-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, |
| 103 |
-1372, -1308, -1244, -1180, -1116, -1052, -988, -924, |
| 104 |
-876, -844, -812, -780, -748, -716, -684, -652, |
| 105 |
-620, -588, -556, -524, -492, -460, -428, -396, |
| 106 |
-372, -356, -340, -324, -308, -292, -276, -260, |
| 107 |
-244, -228, -212, -196, -180, -164, -148, -132, |
| 108 |
-120, -112, -104, -96, -88, -80, -72, -64, |
| 109 |
-56, -48, -40, -32, -24, -16, -8, -1, |
| 110 |
32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, |
| 111 |
23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, |
| 112 |
15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, |
| 113 |
11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, |
| 114 |
7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, |
| 115 |
5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, |
| 116 |
3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, |
| 117 |
2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, |
| 118 |
1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, |
| 119 |
1372, 1308, 1244, 1180, 1116, 1052, 988, 924, |
| 120 |
876, 844, 812, 780, 748, 716, 684, 652, |
| 121 |
620, 588, 556, 524, 492, 460, 428, 396, |
| 122 |
372, 356, 340, 324, 308, 292, 276, 260, |
| 123 |
244, 228, 212, 196, 180, 164, 148, 132, |
| 124 |
120, 112, 104, 96, 88, 80, 72, 64, |
| 125 |
56, 48, 40, 32, 24, 16, 8, 0 |
| 126 |
}; |
| 127 |
// clang-format on |
| 128 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 129 |
{ |
| 130 |
return uLawTable[mpt::byte_cast<uint8>(*inBuf)]; |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
struct DecodeInt16ALaw |
| 135 |
{ |
| 136 |
using input_t = std::byte; |
| 137 |
using output_t = int16; |
| 138 |
static constexpr std::size_t input_inc = 1; |
| 139 |
// clang-format off |
| 140 |
static constexpr std::array<int16, 256> ALawTable = |
| 141 |
{ |
| 142 |
-5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, |
| 143 |
-7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, |
| 144 |
-2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, |
| 145 |
-3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, |
| 146 |
-22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944, |
| 147 |
-30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136, |
| 148 |
-11008,-10496,-12032,-11520, -8960, -8448, -9984, -9472, |
| 149 |
-15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568, |
| 150 |
-344, -328, -376, -360, -280, -264, -312, -296, |
| 151 |
-472, -456, -504, -488, -408, -392, -440, -424, |
| 152 |
-88, -72, -120, -104, -24, -8, -56, -40, |
| 153 |
-216, -200, -248, -232, -152, -136, -184, -168, |
| 154 |
-1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184, |
| 155 |
-1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, |
| 156 |
-688, -656, -752, -720, -560, -528, -624, -592, |
| 157 |
-944, -912, -1008, -976, -816, -784, -880, -848, |
| 158 |
5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736, |
| 159 |
7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, |
| 160 |
2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368, |
| 161 |
3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, |
| 162 |
22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944, |
| 163 |
30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, |
| 164 |
11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472, |
| 165 |
15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, |
| 166 |
344, 328, 376, 360, 280, 264, 312, 296, |
| 167 |
472, 456, 504, 488, 408, 392, 440, 424, |
| 168 |
88, 72, 120, 104, 24, 8, 56, 40, |
| 169 |
216, 200, 248, 232, 152, 136, 184, 168, |
| 170 |
1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184, |
| 171 |
1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, |
| 172 |
688, 656, 752, 720, 560, 528, 624, 592, |
| 173 |
944, 912, 1008, 976, 816, 784, 880, 848 |
| 174 |
}; |
| 175 |
// clang-format on |
| 176 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 177 |
{ |
| 178 |
return ALawTable[mpt::byte_cast<uint8>(*inBuf)]; |
| 179 |
} |
| 180 |
}; |
| 181 |
|
| 182 |
template <uint16 offset, std::size_t loByteIndex, std::size_t hiByteIndex> |
| 183 |
struct DecodeInt16 |
| 184 |
{ |
| 185 |
using input_t = std::byte; |
| 186 |
using output_t = int16; |
| 187 |
static constexpr std::size_t input_inc = 2; |
| 188 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 189 |
{ |
| 190 |
return static_cast<int16>((mpt::byte_cast<uint8>(inBuf[loByteIndex]) | (mpt::byte_cast<uint8>(inBuf[hiByteIndex]) << 8)) - offset); |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
template <std::size_t loByteIndex, std::size_t hiByteIndex> |
| 195 |
struct DecodeInt16Delta |
| 196 |
{ |
| 197 |
using input_t = std::byte; |
| 198 |
using output_t = int16; |
| 199 |
static constexpr std::size_t input_inc = 2; |
| 200 |
uint16 delta; |
| 201 |
DecodeInt16Delta() |
| 202 |
: delta(0) |
| 203 |
{ |
| 204 |
} |
| 205 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 206 |
{ |
| 207 |
delta += static_cast<uint16>(static_cast<uint16>(mpt::byte_cast<uint8>(inBuf[loByteIndex])) | static_cast<uint16>(mpt::byte_cast<uint8>(inBuf[hiByteIndex]) << 8)); |
| 208 |
return static_cast<int16>(delta); |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
struct DecodeInt16Delta8 |
| 213 |
{ |
| 214 |
using input_t = std::byte; |
| 215 |
using output_t = int16; |
| 216 |
static constexpr std::size_t input_inc = 2; |
| 217 |
uint16 delta; |
| 218 |
DecodeInt16Delta8() |
| 219 |
: delta(0) |
| 220 |
{ |
| 221 |
} |
| 222 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 223 |
{ |
| 224 |
delta += mpt::byte_cast<uint8>(inBuf[0]); |
| 225 |
int16 result = delta & 0xFF; |
| 226 |
delta += mpt::byte_cast<uint8>(inBuf[1]); |
| 227 |
result |= static_cast<uint16>(delta << 8); |
| 228 |
return result; |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
template <uint32 offset, std::size_t loByteIndex, std::size_t midByteIndex, std::size_t hiByteIndex> |
| 233 |
struct DecodeInt24 |
| 234 |
{ |
| 235 |
using input_t = std::byte; |
| 236 |
using output_t = int32; |
| 237 |
static constexpr std::size_t input_inc = 3; |
| 238 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 239 |
{ |
| 240 |
const uint32 tmp = (uint32(0) |
| 241 |
| (static_cast<uint32>(mpt::byte_cast<uint8>(inBuf[loByteIndex])) << 8) |
| 242 |
| (static_cast<uint32>(mpt::byte_cast<uint8>(inBuf[midByteIndex])) << 16) |
| 243 |
| (static_cast<uint32>(mpt::byte_cast<uint8>(inBuf[hiByteIndex])) << 24)) |
| 244 |
- offset; |
| 245 |
return static_cast<int32>(tmp); |
| 246 |
} |
| 247 |
}; |
| 248 |
|
| 249 |
template <uint32 offset, std::size_t loLoByteIndex, std::size_t loHiByteIndex, std::size_t hiLoByteIndex, std::size_t hiHiByteIndex> |
| 250 |
struct DecodeInt32 |
| 251 |
{ |
| 252 |
using input_t = std::byte; |
| 253 |
using output_t = int32; |
| 254 |
static constexpr std::size_t input_inc = 4; |
| 255 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 256 |
{ |
| 257 |
const uint32 tmp = (uint32(0) |
| 258 |
| (static_cast<uint32>(mpt::byte_cast<uint8>(inBuf[loLoByteIndex])) << 0) |
| 259 |
| (static_cast<uint32>(mpt::byte_cast<uint8>(inBuf[loHiByteIndex])) << 8) |
| 260 |
| (static_cast<uint32>(mpt::byte_cast<uint8>(inBuf[hiLoByteIndex])) << 16) |
| 261 |
| (static_cast<uint32>(mpt::byte_cast<uint8>(inBuf[hiHiByteIndex])) << 24)) |
| 262 |
- offset; |
| 263 |
return static_cast<int32>(tmp); |
| 264 |
} |
| 265 |
}; |
| 266 |
|
| 267 |
template <uint64 offset, std::size_t b0, std::size_t b1, std::size_t b2, std::size_t b3, std::size_t b4, std::size_t b5, std::size_t b6, std::size_t b7> |
| 268 |
struct DecodeInt64 |
| 269 |
{ |
| 270 |
using input_t = std::byte; |
| 271 |
using output_t = int64; |
| 272 |
static constexpr std::size_t input_inc = 8; |
| 273 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 274 |
{ |
| 275 |
const uint64 tmp = (uint64(0) |
| 276 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b0])) << 0) |
| 277 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b1])) << 8) |
| 278 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b2])) << 16) |
| 279 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b3])) << 24) |
| 280 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b4])) << 32) |
| 281 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b5])) << 40) |
| 282 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b6])) << 48) |
| 283 |
| (static_cast<uint64>(mpt::byte_cast<uint8>(inBuf[b7])) << 56)) |
| 284 |
- offset; |
| 285 |
return static_cast<int64>(tmp); |
| 286 |
} |
| 287 |
}; |
| 288 |
|
| 289 |
template <std::size_t loLoByteIndex, std::size_t loHiByteIndex, std::size_t hiLoByteIndex, std::size_t hiHiByteIndex> |
| 290 |
struct DecodeFloat32 |
| 291 |
{ |
| 292 |
using input_t = std::byte; |
| 293 |
using output_t = somefloat32; |
| 294 |
static constexpr std::size_t input_inc = 4; |
| 295 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 296 |
{ |
| 297 |
somefloat32 val = IEEE754binary32LE(inBuf[loLoByteIndex], inBuf[loHiByteIndex], inBuf[hiLoByteIndex], inBuf[hiHiByteIndex]); |
| 298 |
val = mpt::sanitize_nan(val); |
| 299 |
if(std::isinf(val)) |
| 300 |
{ |
| 301 |
if(val >= 0.0f) |
| 302 |
{ |
| 303 |
val = 1.0f; |
| 304 |
} else |
| 305 |
{ |
| 306 |
val = -1.0f; |
| 307 |
} |
| 308 |
} |
| 309 |
return val; |
| 310 |
} |
| 311 |
}; |
| 312 |
|
| 313 |
template <std::size_t loLoByteIndex, std::size_t loHiByteIndex, std::size_t hiLoByteIndex, std::size_t hiHiByteIndex> |
| 314 |
struct DecodeScaledFloat32 |
| 315 |
{ |
| 316 |
using input_t = std::byte; |
| 317 |
using output_t = somefloat32; |
| 318 |
static constexpr std::size_t input_inc = 4; |
| 319 |
float factor; |
| 320 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 321 |
{ |
| 322 |
somefloat32 val = IEEE754binary32LE(inBuf[loLoByteIndex], inBuf[loHiByteIndex], inBuf[hiLoByteIndex], inBuf[hiHiByteIndex]); |
| 323 |
val = mpt::sanitize_nan(val); |
| 324 |
if(std::isinf(val)) |
| 325 |
{ |
| 326 |
if(val >= 0.0f) |
| 327 |
{ |
| 328 |
val = 1.0f; |
| 329 |
} else |
| 330 |
{ |
| 331 |
val = -1.0f; |
| 332 |
} |
| 333 |
} |
| 334 |
return factor * val; |
| 335 |
} |
| 336 |
MPT_FORCEINLINE DecodeScaledFloat32(float scaleFactor) |
| 337 |
: factor(scaleFactor) |
| 338 |
{ |
| 339 |
return; |
| 340 |
} |
| 341 |
}; |
| 342 |
|
| 343 |
template <std::size_t b0, std::size_t b1, std::size_t b2, std::size_t b3, std::size_t b4, std::size_t b5, std::size_t b6, std::size_t b7> |
| 344 |
struct DecodeFloat64 |
| 345 |
{ |
| 346 |
using input_t = std::byte; |
| 347 |
using output_t = somefloat64; |
| 348 |
static constexpr std::size_t input_inc = 8; |
| 349 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 350 |
{ |
| 351 |
somefloat64 val = IEEE754binary64LE(inBuf[b0], inBuf[b1], inBuf[b2], inBuf[b3], inBuf[b4], inBuf[b5], inBuf[b6], inBuf[b7]); |
| 352 |
val = mpt::sanitize_nan(val); |
| 353 |
if(std::isinf(val)) |
| 354 |
{ |
| 355 |
if(val >= 0.0) |
| 356 |
{ |
| 357 |
val = 1.0; |
| 358 |
} else |
| 359 |
{ |
| 360 |
val = -1.0; |
| 361 |
} |
| 362 |
} |
| 363 |
return val; |
| 364 |
} |
| 365 |
}; |
| 366 |
|
| 367 |
template <typename Tsample> |
| 368 |
struct DecodeIdentity |
| 369 |
{ |
| 370 |
using input_t = Tsample; |
| 371 |
using output_t = Tsample; |
| 372 |
static constexpr std::size_t input_inc = 1; |
| 373 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 374 |
{ |
| 375 |
return *inBuf; |
| 376 |
} |
| 377 |
}; |
| 378 |
|
| 379 |
|
| 380 |
// Reads sample data with Func and passes it directly to Func2. |
| 381 |
// Func1::output_t and Func2::input_t must be identical |
| 382 |
template <typename Func2, typename Func1> |
| 383 |
struct ConversionChain |
| 384 |
{ |
| 385 |
using input_t = typename Func1::input_t; |
| 386 |
using output_t = typename Func2::output_t; |
| 387 |
static constexpr std::size_t input_inc = Func1::input_inc; |
| 388 |
Func1 func1; |
| 389 |
Func2 func2; |
| 390 |
MPT_FORCEINLINE output_t operator()(const input_t *inBuf) |
| 391 |
{ |
| 392 |
return func2(func1(inBuf)); |
| 393 |
} |
| 394 |
MPT_FORCEINLINE ConversionChain(Func2 f2 = Func2(), Func1 f1 = Func1()) |
| 395 |
: func1(f1) |
| 396 |
, func2(f2) |
| 397 |
{ |
| 398 |
return; |
| 399 |
} |
| 400 |
}; |
| 401 |
|
| 402 |
|
| 403 |
} // namespace SC |
| 404 |
|
| 405 |
|
| 406 |
OPENMPT_NAMESPACE_END |