From d50b1465c352fdb472638b196db7644f231deb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Mon, 15 Nov 2021 23:00:09 +0100 Subject: [PATCH] LibAudio: Add explanatory comments to the FlacLoader Some nuances in the FLAC loading code can do well with an explanation, as these non-obvious insights are often the result of long and painful debugging and nobody should touch the affected code without careful deliberation. (Of course, secretly I just want people to maintain my loader code.) :^) --- Userland/Libraries/LibAudio/FlacLoader.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index af463fbebc..90b6f0ad1c 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -682,13 +682,17 @@ Vector FlacLoaderPlugin::decode_custom_lpc(FlacSubframeHeader& subframe, In dbgln_if(AFLACLOADER_DEBUG, "{}-bit {} shift coefficients: {}", lpc_precision, lpc_shift, coefficients); // decode residual - // FIXME: This order may be incorrect, the LPC is applied to the residual, probably leading to incorrect results. decoded = decode_residual(decoded, subframe, bit_input); // approximate the waveform with the predictor for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) { + // (see below) i64 sample = 0; for (size_t t = 0; t < subframe.order; ++t) { + // It's really important that we compute in 64-bit land here. + // Even though FLAC operates at a maximum bit depth of 32 bits, modern encoders use super-large coefficients for maximum compression. + // These will easily overflow 32 bits and cause strange white noise that apruptly stops intermittently (at the end of a frame). + // The simple fix of course is to do intermediate computations in 64 bits. sample += static_cast(coefficients[t]) * static_cast(decoded[i - t - 1]); } decoded[i] += sample >> lpc_shift;