From ba622cffe4f736501791ddaa7f333f87e11ff3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Mon, 16 Aug 2021 22:04:34 +0200 Subject: [PATCH] LibAudio: Fix overflow on 24-bit FLAC LPC data When computing sample values from a linear predictor, the repeated multiplication and addition can lead to very large values that may overflow a 32-bit integer. This was never discovered with 16-bit FLAC test files used to create and validate the first version of the FLAC loader. However, 24-bit audio, especially with large LPC shifts, will regularly exceed and overflow i32. Therefore, we now use 64 bits temporarily. If the resulting value is too large for 32 bits, something else has gone wrong :^) This fixes playback noise on 24-bit FLACs. --- Userland/Libraries/LibAudio/FlacLoader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index 0e21ad035a..a2baf1aaa7 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -675,9 +675,9 @@ Vector FlacLoaderPlugin::decode_custom_lpc(FlacSubframeHeader& subframe, In // approximate the waveform with the predictor for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) { - i32 sample = 0; + i64 sample = 0; for (size_t t = 0; t < subframe.order; ++t) { - sample += coefficients[t] * decoded[i - t - 1]; + sample += static_cast(coefficients[t]) * static_cast(decoded[i - t - 1]); } decoded[i] += sample >> lpc_shift; }