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.
This commit is contained in:
kleines Filmröllchen 2021-08-16 22:04:34 +02:00 committed by Andreas Kling
parent c974be91ab
commit ba622cffe4
Notes: sideshowbarker 2024-07-18 05:37:25 +09:00

View file

@ -675,9 +675,9 @@ Vector<i32> 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<i64>(coefficients[t]) * static_cast<i64>(decoded[i - t - 1]);
}
decoded[i] += sample >> lpc_shift;
}