From a4831bd49c1459ae317d2455ff601d4cfb92be10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:15:07 +0300 Subject: [PATCH] S11: ac3 fold split: downmix_gains builds the per-channel matrix; fold applies it (same ops, same order) --- crates/ec-ac3/src/decoder.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/crates/ec-ac3/src/decoder.rs b/crates/ec-ac3/src/decoder.rs index 3c69d80c..f30751a7 100644 --- a/crates/ec-ac3/src/decoder.rs +++ b/crates/ec-ac3/src/decoder.rs @@ -413,20 +413,10 @@ impl Ac3Decoder { } } - /// Apply the requested downmix, if any (§7.8.2). - fn fold(&self, samples: Vec, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec { - if self.options.downmix == Downmix::Native { - return samples; - } - let channels = positions.len(); - if channels == 0 { - return samples; - } + /// Per-channel gains into the left and right downmix sums (§7.8.2). + fn downmix_gains(info: &FrameInfo, positions: &[ChannelPosition]) -> (Vec, Vec) { let clev = info.center_mix_level.unwrap_or(0.707); let slev = info.surround_mix_level.unwrap_or(0.707); - // §7.8.2 with the standard's own coefficients, then one scaling so a - // full-scale input cannot overflow the downmix — the standard's fixed - // -7.65 dB would quieten every stereo listener instead. let (mut left, mut right) = (Vec::new(), Vec::new()); for &p in positions { let (l, r) = match p { @@ -447,6 +437,22 @@ impl Ac3Decoder { left.push(l); right.push(r); } + (left, right) + } + + /// Apply the requested downmix, if any (§7.8.2). + fn fold(&self, samples: Vec, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec { + if self.options.downmix == Downmix::Native { + return samples; + } + let channels = positions.len(); + if channels == 0 { + return samples; + } + // The standard's own coefficients, then one scaling so a full-scale + // input cannot overflow the downmix — the standard's fixed -7.65 dB + // would quieten every stereo listener instead. + let (left, right) = Self::downmix_gains(info, positions); let peak = left .iter() .sum::()