Limiter clamp actually caps gain instead of always no-opping
Browse files at this commit407561c6c27819f53f76d8cd1abc9f929aa085a8Parent: 226d633- fix
- limiter clamp actually caps gain instead of always no-opping
1 changed file +38 -5
- crates/ec-aac/src/sbr_env.rs +38 -5
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 infinity | 222 | 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; |