64 "FlacFile is decode-only; FLAC encoding is not supported.";
72 [[nodiscard]]
bool openRead(
const std::filesystem::path& path)
override
76 std::ifstream input(path, std::ios::binary | std::ios::ate);
79 const std::streampos end = input.tellg();
80 if (end <= std::streampos(0))
82 const uint64_t fileSize =
static_cast<uint64_t
>(end);
84 || fileSize >
static_cast<uint64_t
>(std::numeric_limits<size_t>::max()))
88 candidate.bytes.resize(
static_cast<size_t>(fileSize));
89 input.seekg(0, std::ios::beg);
90 input.read(
reinterpret_cast<char*
>(candidate.bytes.data()),
91 static_cast<std::streamsize
>(candidate.bytes.size()));
92 if (input.gcount() !=
static_cast<std::streamsize
>(candidate.bytes.size()))
94 if (!validateOpen(candidate))
97 bytes_ = std::move(candidate.bytes);
98 frames_ = std::move(candidate.frames);
99 stream_ = candidate.stream;
100 info_ = candidate.info;
110 [[nodiscard]]
bool openWrite(
const std::filesystem::path& path,
125 if (!open_)
return false;
137 int64_t numFrames)
override
139 if (!open_ || startFrame < 0 || numFrames <= 0)
147 const int channelsToCopy = std::min(destChannels,
149 const int64_t framesToCopy = std::min<int64_t>(numFrames, destSamples);
150 if (channelsToCopy == 0 || framesToCopy == 0)
153 const uint64_t first =
static_cast<uint64_t
>(startFrame);
154 const uint64_t last = first +
static_cast<uint64_t
>(framesToCopy);
155 auto it = std::lower_bound(frames_.begin(), frames_.end(), first,
156 [](
const FrameIndex& frame, uint64_t sample) {
157 return frame.firstSample + frame.blockSize <= sample;
161 std::vector<int64_t> pcm;
162 while (it != frames_.end() && it->firstSample < last)
164 const size_t frameNumber =
static_cast<size_t>(it - frames_.begin());
165 DecodedFrame decoded;
166 if (!decodeFrame(bytes_, it->offset, stream_, frameNumber,
167 it->firstSample, pcm, decoded)
168 || decoded.endOffset != it->offset + it->size
169 || decoded.blockSize != it->blockSize)
175 const uint64_t overlapBegin = std::max(first, it->firstSample);
176 const uint64_t overlapEnd = std::min(last,
177 it->firstSample +
static_cast<uint64_t
>(it->blockSize));
178 const size_t sourceOffset =
static_cast<size_t>(overlapBegin - it->firstSample);
179 const size_t count =
static_cast<size_t>(overlapEnd - overlapBegin);
180 const size_t destinationOffset =
static_cast<size_t>(overlapBegin - first);
182 for (
int channel = 0; channel < channelsToCopy; ++channel)
184 float* output = dest.
getChannel(channel) + destinationOffset;
185 const int64_t* source = pcm.data()
186 +
static_cast<size_t>(channel) * it->blockSize + sourceOffset;
187 for (
size_t i = 0; i < count; ++i)
188 output[i] = std::ldexp(
static_cast<float>(source[i]),
189 1 -
static_cast<int>(stream_.bitsPerSample));
191 written +=
static_cast<int64_t
>(count);
194 if (written != framesToCopy)
212 std::vector<uint8_t>().swap(bytes_);
213 std::vector<FrameIndex>().swap(frames_);
220 [[nodiscard]]
bool isOpen() const noexcept
override {
return open_; }
225 uint16_t minBlockSize = 0;
226 uint16_t maxBlockSize = 0;
227 uint32_t minFrameSize = 0;
228 uint32_t maxFrameSize = 0;
229 uint32_t sampleRate = 0;
230 uint8_t channels = 0;
231 uint8_t bitsPerSample = 0;
232 uint64_t declaredSamples = 0;
233 std::array<uint8_t, 16> md5 {};
240 uint64_t firstSample = 0;
241 uint32_t blockSize = 0;
246 size_t endOffset = 0;
247 uint32_t blockSize = 0;
248 bool variableBlock =
false;
253 std::vector<uint8_t> bytes;
254 std::vector<FrameIndex> frames;
262 ByteCursor(
const std::vector<uint8_t>& bytes,
size_t begin,
size_t end)
263 : bytes_(bytes), pos_(begin), end_(end)
267 [[nodiscard]]
size_t position() const noexcept {
return pos_; }
268 [[nodiscard]]
size_t remaining() const noexcept {
return end_ - pos_; }
270 bool readU8(uint8_t& value)
noexcept
272 if (pos_ >= end_)
return false;
273 value = bytes_[pos_++];
277 bool readBE16(uint16_t& value)
noexcept
279 uint8_t a = 0, b = 0;
280 if (!readU8(a) || !readU8(b))
return false;
281 value =
static_cast<uint16_t
>((
static_cast<uint16_t
>(a) << 8)
282 |
static_cast<uint16_t
>(b));
286 bool readBE24(uint32_t& value)
noexcept
288 uint8_t a = 0, b = 0, c = 0;
289 if (!readU8(a) || !readU8(b) || !readU8(c))
return false;
290 value = (
static_cast<uint32_t
>(a) << 16)
291 | (
static_cast<uint32_t
>(b) << 8)
292 |
static_cast<uint32_t
>(c);
296 bool readSpan(
size_t length,
const uint8_t*& data)
noexcept
298 if (length > remaining())
return false;
299 data = bytes_.data() + pos_;
304 bool skip(
size_t length)
noexcept
306 const uint8_t* ignored =
nullptr;
307 return readSpan(length, ignored);
311 const std::vector<uint8_t>& bytes_;
319 BitCursor(
const std::vector<uint8_t>& bytes,
size_t beginByte,
321 : bytes_(bytes), bitPos_(beginByte * 8), endBit_(endByte * 8)
325 [[nodiscard]]
size_t bitPosition() const noexcept {
return bitPos_; }
326 [[nodiscard]]
size_t bytePosition() const noexcept {
return bitPos_ / 8; }
327 [[nodiscard]] uint64_t remainingBits() const noexcept
329 return static_cast<uint64_t
>(endBit_ - bitPos_);
332 bool readBits(
unsigned count, uint64_t& value)
noexcept
335 if (count > 64 ||
static_cast<uint64_t
>(count) > remainingBits())
337 for (
unsigned i = 0; i < count; ++i)
339 const uint8_t
byte = bytes_[bitPos_ / 8];
340 const unsigned shift = 7u -
static_cast<unsigned>(bitPos_ & 7u);
341 value = (value << 1) | ((
byte >> shift) & 1u);
347 bool readSigned(
unsigned count, int64_t& value)
noexcept
354 if (count > 63)
return false;
356 if (!readBits(count, raw))
return false;
357 const uint64_t sign = uint64_t { 1 } << (count - 1);
358 if ((raw & sign) == 0)
360 value =
static_cast<int64_t
>(raw);
363 const uint64_t magnitude = (uint64_t { 1 } << count) - raw;
364 value = -
static_cast<int64_t
>(magnitude);
368 bool readUnary(uint64_t limit, uint64_t& zeros)
noexcept
374 if (!readBits(1, bit))
return false;
375 if (bit != 0)
return true;
376 if (zeros >= limit)
return false;
381 bool alignWithZeroPadding() noexcept
383 while ((bitPos_ & 7u) != 0)
386 if (!readBits(1, bit) || bit != 0)
return false;
392 const std::vector<uint8_t>& bytes_;
400 Md5() noexcept { reset(); }
402 void update(std::span<const uint8_t> data)
noexcept
404 totalBytes_ += data.size();
405 while (!data.empty())
407 const size_t take = std::min(data.size(), buffer_.size() - bufferSize_);
408 std::memcpy(buffer_.data() + bufferSize_, data.data(), take);
410 data = data.subspan(take);
411 if (bufferSize_ == buffer_.size())
413 transform(buffer_.data());
419 [[nodiscard]] std::array<uint8_t, 16> finish() noexcept
421 const uint64_t messageBits = totalBytes_ * 8;
422 std::array<uint8_t, 120> padding {};
424 const size_t paddingSize = bufferSize_ < 56
425 ? 56 - bufferSize_ : 64 + 56 - bufferSize_;
426 update(std::span<const uint8_t>(padding.data(), paddingSize));
427 uint8_t length[8] = {};
428 for (
unsigned i = 0; i < 8; ++i)
429 length[i] =
static_cast<uint8_t
>(messageBits >> (8 * i));
432 std::array<uint8_t, 16> digest {};
433 for (
size_t word = 0; word < 4; ++word)
434 for (
unsigned byte = 0;
byte < 4; ++byte)
435 digest[word * 4 +
byte] =
static_cast<uint8_t
>(
436 state_[word] >> (8 *
byte));
441 std::array<uint32_t, 4> state_ {};
442 std::array<uint8_t, 64> buffer_ {};
443 size_t bufferSize_ = 0;
444 uint64_t totalBytes_ = 0;
446 void reset() noexcept
448 state_ = { 0x67452301u, 0xefcdab89u, 0x98badcfeu, 0x10325476u };
453 static uint32_t rotateLeft(uint32_t value,
unsigned count)
noexcept
455 return static_cast<uint32_t
>((value << count) | (value >> (32 - count)));
458 void transform(
const uint8_t* block)
noexcept
460 static constexpr uint32_t constants[64] = {
461 0xd76aa478u,0xe8c7b756u,0x242070dbu,0xc1bdceeeu,
462 0xf57c0fafu,0x4787c62au,0xa8304613u,0xfd469501u,
463 0x698098d8u,0x8b44f7afu,0xffff5bb1u,0x895cd7beu,
464 0x6b901122u,0xfd987193u,0xa679438eu,0x49b40821u,
465 0xf61e2562u,0xc040b340u,0x265e5a51u,0xe9b6c7aau,
466 0xd62f105du,0x02441453u,0xd8a1e681u,0xe7d3fbc8u,
467 0x21e1cde6u,0xc33707d6u,0xf4d50d87u,0x455a14edu,
468 0xa9e3e905u,0xfcefa3f8u,0x676f02d9u,0x8d2a4c8au,
469 0xfffa3942u,0x8771f681u,0x6d9d6122u,0xfde5380cu,
470 0xa4beea44u,0x4bdecfa9u,0xf6bb4b60u,0xbebfbc70u,
471 0x289b7ec6u,0xeaa127fau,0xd4ef3085u,0x04881d05u,
472 0xd9d4d039u,0xe6db99e5u,0x1fa27cf8u,0xc4ac5665u,
473 0xf4292244u,0x432aff97u,0xab9423a7u,0xfc93a039u,
474 0x655b59c3u,0x8f0ccc92u,0xffeff47du,0x85845dd1u,
475 0x6fa87e4fu,0xfe2ce6e0u,0xa3014314u,0x4e0811a1u,
476 0xf7537e82u,0xbd3af235u,0x2ad7d2bbu,0xeb86d391u
478 static constexpr unsigned shifts[64] = {
479 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
480 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
481 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
482 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
485 uint32_t words[16] = {};
486 for (
size_t i = 0; i < 16; ++i)
487 words[i] =
static_cast<uint32_t
>(block[i * 4])
488 | (
static_cast<uint32_t
>(block[i * 4 + 1]) << 8)
489 | (
static_cast<uint32_t
>(block[i * 4 + 2]) << 16)
490 | (
static_cast<uint32_t
>(block[i * 4 + 3]) << 24);
492 uint32_t a = state_[0], b = state_[1], c = state_[2], d = state_[3];
493 for (
unsigned i = 0; i < 64; ++i)
497 if (i < 16) { f = (b & c) | (~b & d); index = i; }
498 else if (i < 32) { f = (d & b) | (~d & c); index = (5 * i + 1) & 15u; }
499 else if (i < 48) { f = b ^ c ^ d; index = (3 * i + 5) & 15u; }
500 else { f = c ^ (b | ~d); index = (7 * i) & 15u; }
501 const uint32_t next = b + rotateLeft(a + f + constants[i]
502 + words[index], shifts[i]);
503 a = d; d = c; c = b; b = next;
512 std::vector<uint8_t> bytes_;
513 std::vector<FrameIndex> frames_;
515 AudioFileInfo info_ {};
518 static uint8_t crc8(std::span<const uint8_t> data)
noexcept
521 for (uint8_t
byte : data)
524 for (
unsigned bit = 0; bit < 8; ++bit)
525 crc =
static_cast<uint8_t
>((crc & 0x80u) != 0
526 ?
static_cast<uint8_t
>((crc << 1) ^ 0x07u)
527 : static_cast<uint8_t>(crc << 1));
532 static uint16_t crc16(std::span<const uint8_t> data)
noexcept
535 for (uint8_t
byte : data)
537 crc ^=
static_cast<uint16_t
>(byte) << 8;
538 for (
unsigned bit = 0; bit < 8; ++bit)
539 crc =
static_cast<uint16_t
>((crc & 0x8000u) != 0
540 ?
static_cast<uint16_t
>((crc << 1) ^ 0x8005u)
541 :
static_cast<uint16_t
>(crc << 1));
546 static bool checkedAdd(int64_t a, int64_t b, int64_t& result)
noexcept
548 if ((b > 0 && a > std::numeric_limits<int64_t>::max() - b)
549 || (b < 0 && a < std::numeric_limits<int64_t>::min() - b))
555 static bool checkedSubtract(int64_t a, int64_t b, int64_t& result)
noexcept
557 if (b == std::numeric_limits<int64_t>::min())
559 return checkedAdd(a, -b, result);
562 static bool checkedMultiply(int64_t a, int64_t b, int64_t& result)
noexcept
564 if (a == 0 || b == 0) { result = 0;
return true; }
565 if (a == -1 && b == std::numeric_limits<int64_t>::min())
return false;
566 if (b == -1 && a == std::numeric_limits<int64_t>::min())
return false;
569 if ((b > 0 && a > std::numeric_limits<int64_t>::max() / b)
570 || (b < 0 && b < std::numeric_limits<int64_t>::min() / a))
575 if ((b > 0 && a < std::numeric_limits<int64_t>::min() / b)
576 || (b < 0 && a < std::numeric_limits<int64_t>::max() / b))
583 static bool floorDividePowerOfTwo(int64_t value,
unsigned shift,
584 int64_t& result)
noexcept
586 if (shift > 62)
return false;
587 if (shift == 0) { result = value;
return true; }
588 const int64_t divisor = int64_t { 1 } << shift;
589 result = value / divisor;
590 if (value < 0 && value % divisor != 0)
595 static bool sampleInDepth(int64_t value,
unsigned depth)
noexcept
597 if (depth == 0 || depth > 33)
return false;
598 const int64_t magnitude = int64_t { 1 } << (depth - 1);
599 return value >= -magnitude && value <= magnitude - 1;
602 static bool parseStreamInfo(
const uint8_t* data, StreamInfo& info)
noexcept
604 info.minBlockSize =
static_cast<uint16_t
>(
605 (
static_cast<uint16_t
>(data[0]) << 8) | data[1]);
606 info.maxBlockSize =
static_cast<uint16_t
>(
607 (
static_cast<uint16_t
>(data[2]) << 8) | data[3]);
608 info.minFrameSize = (
static_cast<uint32_t
>(data[4]) << 16)
609 | (
static_cast<uint32_t
>(data[5]) << 8) | data[6];
610 info.maxFrameSize = (
static_cast<uint32_t
>(data[7]) << 16)
611 | (
static_cast<uint32_t
>(data[8]) << 8) | data[9];
612 const uint64_t packed = (
static_cast<uint64_t
>(data[10]) << 56)
613 | (
static_cast<uint64_t
>(data[11]) << 48)
614 | (
static_cast<uint64_t
>(data[12]) << 40)
615 | (
static_cast<uint64_t
>(data[13]) << 32)
616 | (
static_cast<uint64_t
>(data[14]) << 24)
617 | (
static_cast<uint64_t
>(data[15]) << 16)
618 | (
static_cast<uint64_t
>(data[16]) << 8)
619 |
static_cast<uint64_t
>(data[17]);
620 info.sampleRate =
static_cast<uint32_t
>(packed >> 44);
621 info.channels =
static_cast<uint8_t
>(((packed >> 41) & 7u) + 1u);
622 info.bitsPerSample =
static_cast<uint8_t
>(((packed >> 36) & 31u) + 1u);
623 info.declaredSamples = packed & 0xfffffffffull;
624 std::copy(data + 18, data + 34, info.md5.begin());
626 if (info.minBlockSize < 16 || info.maxBlockSize < 16
627 || info.minBlockSize > info.maxBlockSize)
629 if (info.minFrameSize != 0 && info.maxFrameSize != 0
630 && info.minFrameSize > info.maxFrameSize)
632 if (info.sampleRate == 0 || info.sampleRate > 1048575
633 || info.channels == 0 || info.channels > 8
634 || info.bitsPerSample < 4 || info.bitsPerSample > 32
640 static bool parseCodedNumber(ByteCursor& cursor,
bool variable,
641 uint64_t& value)
noexcept
644 if (!cursor.readU8(first))
return false;
646 uint64_t minimum = 0;
647 if (first < 0x80) { length = 1; value = first; }
648 else if ((first & 0xe0u) == 0xc0u) { length = 2; value = first & 0x1fu; minimum = 0x80; }
649 else if ((first & 0xf0u) == 0xe0u) { length = 3; value = first & 0x0fu; minimum = 0x800; }
650 else if ((first & 0xf8u) == 0xf0u) { length = 4; value = first & 0x07u; minimum = 0x10000; }
651 else if ((first & 0xfcu) == 0xf8u) { length = 5; value = first & 0x03u; minimum = 0x200000; }
652 else if ((first & 0xfeu) == 0xfcu) { length = 6; value = first & 0x01u; minimum = 0x4000000; }
653 else if (first == 0xfeu) { length = 7; value = 0; minimum = 0x80000000ull; }
656 for (
unsigned i = 1; i < length; ++i)
658 uint8_t continuation = 0;
659 if (!cursor.readU8(continuation) || (continuation & 0xc0u) != 0x80u)
661 value = (value << 6) | (continuation & 0x3fu);
663 if (length > 1 && value < minimum)
666 return value <= 0xfffffffffull;
667 return length <= 6 && value <= 0x7fffffffu;
670 static bool decodeBlockSize(uint8_t code, ByteCursor& cursor,
671 uint32_t& blockSize)
noexcept
673 if (code == 0)
return false;
674 if (code == 1) blockSize = 192;
675 else if (code >= 2 && code <= 5)
676 blockSize = 576u << (code - 2u);
680 if (!cursor.readU8(value))
return false;
681 blockSize =
static_cast<uint32_t
>(value) + 1u;
686 if (!cursor.readBE16(value) || value == 0xffffu)
return false;
687 blockSize =
static_cast<uint32_t
>(value) + 1u;
690 blockSize = 1u << code;
691 return blockSize > 0 && blockSize <= 65535;
694 static bool decodeSampleRate(uint8_t code, ByteCursor& cursor,
695 const StreamInfo& stream,
696 uint32_t& sampleRate)
noexcept
698 static constexpr uint32_t rates[12] = {
699 0, 88200, 176400, 192000, 8000, 16000,
700 22050, 24000, 32000, 44100, 48000, 96000
703 sampleRate = code == 0 ? stream.sampleRate : rates[code];
707 if (!cursor.readU8(value))
return false;
708 sampleRate =
static_cast<uint32_t
>(value) * 1000u;
713 if (!cursor.readBE16(value))
return false;
719 if (!cursor.readBE16(value))
return false;
720 sampleRate =
static_cast<uint32_t
>(value) * 10u;
723 return sampleRate != 0 && sampleRate == stream.sampleRate;
726 static bool decodeResidual(BitCursor& bits, uint32_t blockSize,
727 unsigned predictorOrder,
728 std::span<int64_t> output)
noexcept
730 uint64_t method = 0, partitionOrder = 0;
731 if (!bits.readBits(2, method) || method > 1
732 || !bits.readBits(4, partitionOrder))
734 const uint32_t partitions = 1u <<
static_cast<unsigned>(partitionOrder);
735 if (partitions == 0 || blockSize % partitions != 0)
737 const uint32_t partitionSize = blockSize / partitions;
738 if (partitionSize <= predictorOrder)
740 const unsigned parameterBits = method == 0 ? 4u : 5u;
741 const uint64_t escape = (uint64_t { 1 } << parameterBits) - 1u;
742 size_t outputIndex = predictorOrder;
744 for (uint32_t partition = 0; partition < partitions; ++partition)
746 uint64_t parameter = 0;
747 if (!bits.readBits(parameterBits, parameter))
return false;
748 const uint32_t count = partitionSize
749 - (partition == 0 ?
static_cast<uint32_t
>(predictorOrder) : 0u);
750 if (parameter == escape)
753 if (!bits.readBits(5, width) || width > 31)
return false;
754 for (uint32_t i = 0; i < count; ++i)
756 int64_t residual = 0;
757 if (!bits.readSigned(
static_cast<unsigned>(width), residual)
758 || residual < -2147483647ll || residual > 2147483647ll)
760 output[outputIndex++] = residual;
765 for (uint32_t i = 0; i < count; ++i)
767 uint64_t quotient = 0, remainder = 0;
769 || !bits.readBits(
static_cast<unsigned>(parameter), remainder)
770 || quotient > (std::numeric_limits<uint64_t>::max()
771 >>
static_cast<unsigned>(parameter)))
773 const uint64_t folded = (quotient << static_cast<unsigned>(parameter))
775 if (folded > 4294967294ull)
777 const int64_t residual = (folded & 1u) == 0
778 ?
static_cast<int64_t
>(folded / 2u)
779 : -
static_cast<int64_t
>(folded / 2u) - 1;
780 if (residual < -2147483647ll || residual > 2147483647ll)
782 output[outputIndex++] = residual;
786 return outputIndex == output.size();
789 static bool decodeSubframe(BitCursor& bits, uint32_t blockSize,
790 unsigned storedDepth,
791 std::span<int64_t> output)
noexcept
793 uint64_t zero = 0, type = 0, wastedFlag = 0;
794 if (!bits.readBits(1, zero) || zero != 0
795 || !bits.readBits(6, type)
796 || !bits.readBits(1, wastedFlag))
803 if (!bits.readUnary(32, zeros) || zeros >= storedDepth)
805 wasted =
static_cast<unsigned>(zeros + 1);
807 if (storedDepth <= wasted)
809 const unsigned depth = storedDepth - wasted;
811 unsigned predictorOrder = 0;
815 if (!bits.readSigned(depth, value))
return false;
816 std::fill(output.begin(), output.end(), value);
820 for (int64_t& value : output)
821 if (!bits.readSigned(depth, value)) return false;
823 else if (type >= 8 && type <= 12)
825 predictorOrder =
static_cast<unsigned>(type - 8);
826 if (predictorOrder > blockSize)
return false;
827 for (
unsigned i = 0; i < predictorOrder; ++i)
828 if (!bits.readSigned(depth, output[i]))
return false;
829 if (!decodeResidual(bits, blockSize, predictorOrder, output))
831 static constexpr int coefficients[5][4] = {
832 { 0, 0, 0, 0 }, { 1, 0, 0, 0 }, { 2, -1, 0, 0 },
833 { 3, -3, 1, 0 }, { 4, -6, 4, -1 }
835 for (
size_t i = predictorOrder; i < output.size(); ++i)
837 int64_t prediction = 0;
838 for (
unsigned j = 0; j < predictorOrder; ++j)
841 if (!checkedMultiply(output[i - j - 1], coefficients[predictorOrder][j], product)
842 || !checkedAdd(prediction, product, prediction))
845 if (!checkedAdd(prediction, output[i], output[i])
846 || !sampleInDepth(output[i], depth))
850 else if (type >= 32 && type <= 63)
852 predictorOrder =
static_cast<unsigned>(type - 31);
853 if (predictorOrder > blockSize)
return false;
854 for (
unsigned i = 0; i < predictorOrder; ++i)
855 if (!bits.readSigned(depth, output[i]))
return false;
856 uint64_t precisionMinusOne = 0;
858 if (!bits.readBits(4, precisionMinusOne) || precisionMinusOne == 15
859 || !bits.readSigned(5, shift) || shift < 0)
861 const unsigned precision =
static_cast<unsigned>(precisionMinusOne + 1);
862 std::array<int64_t, 32> coefficients {};
863 for (
unsigned i = 0; i < predictorOrder; ++i)
864 if (!bits.readSigned(precision, coefficients[i]))
return false;
865 if (!decodeResidual(bits, blockSize, predictorOrder, output))
867 for (
size_t i = predictorOrder; i < output.size(); ++i)
870 for (
unsigned j = 0; j < predictorOrder; ++j)
873 if (!checkedMultiply(coefficients[j], output[i - j - 1], product)
874 || !checkedAdd(sum, product, sum))
877 int64_t prediction = 0;
878 if (!floorDividePowerOfTwo(sum,
static_cast<unsigned>(shift), prediction)
879 || !checkedAdd(prediction, output[i], output[i])
880 || !sampleInDepth(output[i], depth))
888 for (int64_t value : output)
889 if (!sampleInDepth(value, depth)) return false;
893 const int64_t factor = int64_t { 1 } << wasted;
894 for (int64_t& value : output)
895 if (!checkedMultiply(value, factor, value)
896 || !sampleInDepth(value, storedDepth))
902 static bool restoreChannels(uint8_t channelCode, uint8_t bitsPerSample,
904 std::vector<int64_t>& pcm)
noexcept
908 for (int64_t value : pcm)
909 if (!sampleInDepth(value, bitsPerSample)) return false;
913 int64_t* first = pcm.data();
914 int64_t* second = pcm.data() + blockSize;
915 for (uint32_t i = 0; i < blockSize; ++i)
917 int64_t left = 0, right = 0;
918 if (channelCode == 8)
921 if (!checkedSubtract(left, second[i], right))
return false;
923 else if (channelCode == 9)
926 if (!checkedAdd(first[i], right, left))
return false;
930 int64_t doubledMid = 0, adjustedMid = 0;
931 if (!checkedMultiply(first[i], 2, doubledMid)
932 || !checkedAdd(doubledMid, second[i] % 2 != 0 ? 1 : 0, adjustedMid))
934 int64_t leftNumerator = 0, rightNumerator = 0;
935 if (!checkedAdd(adjustedMid, second[i], leftNumerator)
936 || !checkedSubtract(adjustedMid, second[i], rightNumerator)
937 || !floorDividePowerOfTwo(leftNumerator, 1, left)
938 || !floorDividePowerOfTwo(rightNumerator, 1, right))
941 if (!sampleInDepth(left, bitsPerSample)
942 || !sampleInDepth(right, bitsPerSample))
950 static bool decodeFrame(
const std::vector<uint8_t>& bytes,
size_t offset,
951 const StreamInfo& stream,
size_t expectedFrameNumber,
952 uint64_t expectedFirstSample,
953 std::vector<int64_t>& pcm,
954 DecodedFrame& decoded)
956 if (offset > bytes.size() || bytes.size() - offset < 8)
958 ByteCursor cursor(bytes, offset, bytes.size());
959 uint8_t first = 0, second = 0, third = 0, fourth = 0;
960 if (!cursor.readU8(first) || !cursor.readU8(second)
961 || !cursor.readU8(third) || !cursor.readU8(fourth)
962 || first != 0xff || (second & 0xfeu) != 0xf8u
963 || (fourth & 1u) != 0)
965 const bool variable = (second & 1u) != 0;
966 if (variable == (stream.minBlockSize == stream.maxBlockSize))
968 const uint8_t blockCode = third >> 4;
969 const uint8_t rateCode = third & 0x0fu;
970 const uint8_t channelCode = fourth >> 4;
971 const uint8_t depthCode = (fourth >> 1) & 7u;
972 if (channelCode > 10 || depthCode == 3)
974 const uint8_t channels = channelCode <= 7
975 ?
static_cast<uint8_t
>(channelCode + 1) : uint8_t { 2 };
976 static constexpr uint8_t depths[8] = { 0, 8, 12, 0, 16, 20, 24, 32 };
977 const uint8_t bitDepth = depthCode == 0 ? stream.bitsPerSample : depths[depthCode];
978 if (channels != stream.channels || bitDepth != stream.bitsPerSample)
981 uint64_t codedNumber = 0;
982 if (!parseCodedNumber(cursor, variable, codedNumber))
984 if ((variable && codedNumber != expectedFirstSample)
985 || (!variable && codedNumber != expectedFrameNumber))
988 uint32_t blockSize = 0, sampleRate = 0;
989 if (!decodeBlockSize(blockCode, cursor, blockSize)
990 || !decodeSampleRate(rateCode, cursor, stream, sampleRate)
991 || blockSize > stream.maxBlockSize)
994 const size_t crcPosition = cursor.position();
995 uint8_t storedCrc8 = 0;
996 if (!cursor.readU8(storedCrc8)
997 || crc8(std::span<const uint8_t>(bytes.data() + offset,
998 crcPosition - offset)) != storedCrc8)
1001 if (
static_cast<uint64_t
>(blockSize) * channels
1002 >
static_cast<uint64_t
>(std::numeric_limits<size_t>::max()))
1004 pcm.resize(
static_cast<size_t>(blockSize) * channels);
1005 BitCursor bits(bytes, cursor.position(), bytes.size());
1006 for (uint8_t channel = 0; channel < channels; ++channel)
1008 unsigned depth = bitDepth;
1009 if ((channelCode == 8 && channel == 1)
1010 || (channelCode == 9 && channel == 0)
1011 || (channelCode == 10 && channel == 1))
1013 std::span<int64_t> channelPcm(
1014 pcm.data() +
static_cast<size_t>(channel) * blockSize, blockSize);
1015 if (!decodeSubframe(bits, blockSize, depth, channelPcm))
1018 if (!bits.alignWithZeroPadding())
1020 const size_t footerPosition = bits.bytePosition();
1021 if (footerPosition > bytes.size() || bytes.size() - footerPosition < 2)
1023 const uint16_t storedCrc16 =
static_cast<uint16_t
>(
1024 (
static_cast<uint16_t
>(bytes[footerPosition]) << 8)
1025 | bytes[footerPosition + 1]);
1026 if (crc16(std::span<const uint8_t>(bytes.data() + offset,
1027 footerPosition - offset)) != storedCrc16)
1029 if (!restoreChannels(channelCode, bitDepth, blockSize, pcm))
1032 decoded.endOffset = footerPosition + 2;
1033 decoded.blockSize = blockSize;
1034 decoded.variableBlock = variable;
1038 static bool updateMd5(Md5& md5,
const std::vector<int64_t>& pcm,
1039 uint8_t channels, uint8_t bitsPerSample,
1040 uint32_t blockSize)
noexcept
1042 const unsigned bytesPerSample = (bitsPerSample + 7u) / 8u;
1043 uint8_t encoded[4] = {};
1044 for (uint32_t sample = 0; sample < blockSize; ++sample)
1046 for (uint8_t channel = 0; channel < channels; ++channel)
1048 const int64_t value = pcm[
static_cast<size_t>(channel) * blockSize + sample];
1049 const uint64_t representation =
static_cast<uint64_t
>(value);
1050 encoded[0] =
static_cast<uint8_t
>(representation);
1051 encoded[1] =
static_cast<uint8_t
>(representation >> 8);
1052 encoded[2] =
static_cast<uint8_t
>(representation >> 16);
1053 encoded[3] =
static_cast<uint8_t
>(representation >> 24);
1054 switch (bytesPerSample)
1056 case 1: md5.update(std::span<const uint8_t>(encoded, 1));
break;
1057 case 2: md5.update(std::span<const uint8_t>(encoded, 2));
break;
1058 case 3: md5.update(std::span<const uint8_t>(encoded, 3));
break;
1059 case 4: md5.update(std::span<const uint8_t>(encoded, 4));
break;
1060 default:
return false;
1067 static bool allZero(
const std::array<uint8_t, 16>& value)
noexcept
1069 for (uint8_t
byte : value) if (byte != 0) return false;
1073 static bool validateOpen(OpenState& state)
1075 if (state.bytes.size() < 4
1076 || std::memcmp(state.bytes.data(),
"fLaC", 4) != 0)
1078 ByteCursor cursor(state.bytes, 4, state.bytes.size());
1079 uint64_t metadataBlocks = 0;
1080 bool sawStreamInfo =
false;
1081 bool lastMetadata =
false;
1083 while (!lastMetadata)
1088 uint32_t length = 0;
1089 if (!cursor.readU8(header) || !cursor.readBE24(length))
1091 lastMetadata = (header & 0x80u) != 0;
1092 const uint8_t type = header & 0x7fu;
1093 if (type == 127 || length > cursor.remaining())
1095 const uint8_t* data =
nullptr;
1096 if (!cursor.readSpan(length, data))
1100 if (type != 0 || length != 34
1101 || !parseStreamInfo(data, state.stream))
1103 sawStreamInfo =
true;
1108 if (!sawStreamInfo || cursor.remaining() == 0)
1112 uint64_t coverage = 0;
1113 bool haveStrategy =
false;
1114 bool strategy =
false;
1115 uint32_t previousBlockSize = 0;
1116 std::vector<int64_t> pcm;
1117 while (cursor.remaining() > 0)
1121 if (haveStrategy && previousBlockSize < state.stream.minBlockSize)
1123 if (haveStrategy && !strategy
1124 && previousBlockSize != state.stream.minBlockSize)
1127 DecodedFrame decoded;
1128 const size_t frameOffset = cursor.position();
1129 if (!decodeFrame(state.bytes, frameOffset, state.stream,
1130 state.frames.size(), coverage, pcm, decoded)
1131 || decoded.endOffset <= frameOffset
1132 || decoded.endOffset > state.bytes.size())
1134 if (haveStrategy && decoded.variableBlock != strategy)
1136 haveStrategy =
true;
1137 strategy = decoded.variableBlock;
1138 previousBlockSize = decoded.blockSize;
1139 const size_t frameSize = decoded.endOffset - frameOffset;
1140 if ((state.stream.minFrameSize != 0
1141 && frameSize < state.stream.minFrameSize)
1142 || (state.stream.maxFrameSize != 0
1143 && frameSize > state.stream.maxFrameSize))
1147 const uint64_t firstSample = coverage;
1148 coverage += decoded.blockSize;
1149 const uint64_t bytesPerSample = (state.stream.bitsPerSample + 7u) / 8u;
1151 / (
static_cast<uint64_t
>(state.stream.channels) * bytesPerSample))
1153 if (!updateMd5(md5, pcm, state.stream.channels,
1154 state.stream.bitsPerSample, decoded.blockSize))
1156 state.frames.push_back({ frameOffset, frameSize, firstSample,
1157 decoded.blockSize });
1158 if (!cursor.skip(frameSize))
1162 if (state.frames.empty() || coverage == 0
1163 || (state.stream.declaredSamples != 0
1164 && state.stream.declaredSamples != coverage))
1166 const std::array<uint8_t, 16> digest = md5.finish();
1167 if (!allZero(state.stream.md5) && digest != state.stream.md5)
1170 state.info.sampleRate =
static_cast<double>(state.stream.sampleRate);
1171 state.info.numChannels = state.stream.channels;
1172 state.info.numSamples =
static_cast<int64_t
>(coverage);
1173 state.info.bitsPerSample = state.stream.bitsPerSample;
1174 state.info.isFloatingPoint =
false;