forge
Explore
Log in

Don't you dare go Hollow

Limiter clamp actually caps gain instead of always no-opping

Browse files at this commit

407561c6c27819f53f76d8cd1abc9f929aa085a8Ahmet Kaan GümüşParent: 226d633
  • fix
    • limiter clamp actually caps gain instead of always no-opping
Show full message
fix(sbr): limiter clamp actually caps gain instead of always no-opping

sbr_env::adjust's limiter cap used `.max(gain.min(limiter_max))`, which is
always >= the intended cap and so the .min() around it never restricted
anything -- flagged in the ledger as likely backwards, now confirmed and
fixed: the cap is limiter_max * sqrt((noise+current)/current), matching
Sec 4.6.18.7.5's headroom shape (a cell with zero transmitted noise floor
still gets the plain limiter_max ceiling). Zero measured effect on the two
real-file fixtures (limiter rarely the binding constraint there), but
lib tests 38/38 and this closes a genuine correctness gap flagged for two
rounds.

Also tried and reverted in this same pass: per-subband bs_interpol_freq
gain interpolation (a centre-frequency lerp across sfb gains) regressed
reeln ch0 full-band 0.972756 -> 0.960561, so it's left as a documented
corner-cut rather than shipped -- header.interpol_freq/smoothing_mode stay
parsed but unapplied.
crates/ec-aac/src/sbr_env.rsmodified+38 -5
@@ -178,6 +178,12 @@ pub fn adjust(
178 .windows(2)178 .windows(2)
179 .position(|w| t0 >= w[0] && t0 < w[1])179 .position(|w| t0 >= w[0] && t0 < w[1])
180 .and_then(|ni| noise_energy.get(ni));180 .and_then(|ni| noise_energy.get(ni));
181+ // Per-sfb gain, computed first and applied afterward (as either a
182+ // flat step per band, or -- Sec 4.6.18.7.6, `bs_interpol_freq` --
183+ // linearly interpolated across subbands between sfb centre
184+ // frequencies) so both application modes share the exact same gain
185+ // values.
186+ let mut gains = Vec::with_capacity(table.len().saturating_sub(1));
181 for b in 0..table.len().saturating_sub(1) {187 for b in 0..table.len().saturating_sub(1) {
182 let lo = table[b] as usize;188 let lo = table[b] as usize;
183 let hi = table[b + 1] as usize;189 let hi = table[b + 1] as usize;
@@ -200,12 +206,39 @@ pub fn adjust(
200 .and_then(|r| r.get(q).copied())206 .and_then(|r| r.get(q).copied())
201 .unwrap_or(0.0)207 .unwrap_or(0.0)
202 .max(1e-12);208 .max(1e-12);
203- gain = gain.min(209+ // Cap gain at `limiter_max` times the cell's own
204- (noise_here * limiter_max / current.max(1e-12))210+ // noise-plus-signal headroom over its raw HF estimate (Sec
205- .sqrt()211+ // 4.6.18.7.5's `limGain * sqrt((Q_M+E_curr)/E_curr)` shape): a
206- .max(gain.min(limiter_max)),212+ // cell with no transmitted noise floor still gets the plain
207- );213+ // `limiter_max` ceiling (the `current` term alone gives
214+ // `sqrt(current/current)=1`), while a cell whose noise floor
215+ // dominates its raw estimate gets extra headroom proportional to
216+ // that ratio. The previous `.max(gain.min(limiter_max))` here
217+ // floored the cap at `min(gain, limiter_max)`, which is always
218+ // >= the intended cap and so never actually restricted
219+ // anything.
220+ let cur = current.max(1e-12);
221+ gain = gain.min(limiter_max * ((noise_here + cur) / cur).sqrt());
208 gain = gain.min(limiter_max * 64.0); // absolute ceiling: never amplify a silent cell to infinity222 gain = gain.min(limiter_max * 64.0); // absolute ceiling: never amplify a silent cell to infinity
223+ gains.push(gain);
224+ }
225+ // `bs_interpol_freq` (per-subband linear interpolation of these sfb
226+ // gains, Sec 4.6.18.7.6) was tried here: a centre-frequency lerp
227+ // between neighbouring sfbs' flat gain values, applied whenever
228+ // `header.interpol_freq != 0`. Measured effect on the real-file
229+ // sweep was NEGATIVE (reeln ch0 full-band 0.972756 -> 0.960561)
230+ // -- the reference decoder's actual interpolation shape (weighting,
231+ // node placement, or which quantity gets interpolated) differs from
232+ // this naive lerp, so it was reverted rather than shipped as a
233+ // regression; `header.interpol_freq`/`smoothing_mode` remain parsed
234+ // but unapplied. corner-cut: real bs_interpol_freq/smoothing_mode
235+ // support, ceiling ~0.999 full-band bar; needs the reference
236+ // decoder's exact per-subband gain-interpolation formula, not a
237+ // plausible reconstruction of it.
238+ for b in 0..table.len().saturating_sub(1) {
239+ let lo = table[b] as usize;
240+ let hi = table[b + 1] as usize;
241+ let gain = gains[b];
209 for band in lo..hi {242 for band in lo..hi {
210 if band < kx || band - kx >= hf.len() {243 if band < kx || band - kx >= hf.len() {
211 continue;244 continue;