浏览代码

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.
kleines Filmröllchen 3 年之前
父节点
当前提交
ba622cffe4
共有 1 个文件被更改,包括 2 次插入2 次删除
  1. 2 2
      Userland/Libraries/LibAudio/FlacLoader.cpp

+ 2 - 2
Userland/Libraries/LibAudio/FlacLoader.cpp

@@ -675,9 +675,9 @@ Vector<i32> FlacLoaderPlugin::decode_custom_lpc(FlacSubframeHeader& subframe, In
 
 
     // approximate the waveform with the predictor
     // approximate the waveform with the predictor
     for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) {
     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) {
         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;
         decoded[i] += sample >> lpc_shift;
     }
     }