sbr: env-gated parameter-sensitivity sweep instruments (round-22)
Browse files at this commit0fb1dc203ca0700b187f9b722122a288d51d3687Parent: 5537fae3 changed files +173 -8
- crates/ec-aac/src/sbr_env.rs +109 -2
- crates/ec-aac/src/sbr_hf.rs +53 -5
- crates/ec-aac/tests/sbr_real_library.rs +11 -1
crates/ec-aac/src/sbr_env.rsmodified+109 -2
| @@ -211,7 +211,15 @@ pub fn adjust( | |||
| 211 | rng: &mut NoiseGen, | 211 | rng: &mut NoiseGen, |
| 212 | ) { | 212 | ) { |
| 213 | let kx = tables.kx as usize; | 213 | let kx = tables.kx as usize; |
| 214 | - let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)]; | 214 | + // (Round-22 sweep instrumentation, zero cost unset) `EC_AAC_SBR_LIMITER_OFF` |
| 215 | + // forces the "no limit" factor regardless of the transmitted | ||
| 216 | + // `bs_limiter_gains`, to probe whether the limiter itself is clipping | ||
| 217 | + // correlation on real content. | ||
| 218 | + let limiter_max = if std::env::var("EC_AAC_SBR_LIMITER_OFF").is_ok() { | ||
| 219 | + LIMITER_FACTOR[3] | ||
| 220 | + } else { | ||
| 221 | + LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)] | ||
| 222 | + }; | ||
| 215 | let track_fraction = noise_fraction_debug(); | 223 | let track_fraction = noise_fraction_debug(); |
| 216 | // (Round-17, Task 1 corroboration) zeroes our injected noise so the | 224 | // (Round-17, Task 1 corroboration) zeroes our injected noise so the |
| 217 | // sweep's measured correlation can be checked against the | 225 | // sweep's measured correlation can be checked against the |
| @@ -219,6 +227,12 @@ pub fn adjust( | |||
| 219 | // touching the transmitted noise ENERGY bookkeeping above (only the | 227 | // touching the transmitted noise ENERGY bookkeeping above (only the |
| 220 | // actual PCM addition is skipped). | 228 | // actual PCM addition is skipped). |
| 221 | let zero_noise = std::env::var("EC_AAC_SBR_NOISE_ZERO").is_ok(); | 229 | let zero_noise = std::env::var("EC_AAC_SBR_NOISE_ZERO").is_ok(); |
| 230 | + // (Round-22 sweep instrumentation, zero cost unset) `EC_AAC_SBR_GAIN_LERP` | ||
| 231 | + // linearly interpolates each envelope's per-band gain toward the NEXT | ||
| 232 | + // envelope's gain across the current envelope's time slots, instead of | ||
| 233 | + // holding it flat -- probing whether our constant-per-cell gain shape | ||
| 234 | + // (vs. the spec's smoother per-slot shape) is a live suspect. | ||
| 235 | + let gain_lerp = std::env::var("EC_AAC_SBR_GAIN_LERP").is_ok(); | ||
| 222 | 236 | ||
| 223 | // Gain toward the transmitted envelope, per (envelope, band) cell, | 237 | // Gain toward the transmitted envelope, per (envelope, band) cell, |
| 224 | // limited relative to that cell's own noise floor. | 238 | // limited relative to that cell's own noise floor. |
| @@ -314,6 +328,43 @@ pub fn adjust( | |||
| 314 | } | 328 | } |
| 315 | } | 329 | } |
| 316 | } | 330 | } |
| 331 | + // (Round-22 sweep instrumentation) the next envelope's gains, only | ||
| 332 | + // when `EC_AAC_SBR_GAIN_LERP` is set and the band layouts match -- | ||
| 333 | + // `hf` in the next envelope's time range is still untouched at this | ||
| 334 | + // point (envelopes are processed and applied in time order), so | ||
| 335 | + // this reads the same raw HF estimate the next iteration would. | ||
| 336 | + let next_gains: Option<Vec<f64>> = | ||
| 337 | + if gain_lerp && ei + 1 < env_energy.len() && ei + 2 < ch.t_env.len() { | ||
| 338 | + let (t0n, t1n) = (ch.t_env[ei + 1], ch.t_env[ei + 2]); | ||
| 339 | + let high_res_n = ch.freq_res.get(ei + 1).copied().unwrap_or(0) != 0; | ||
| 340 | + let table_n: &[i64] = if high_res_n { | ||
| 341 | + &tables.f_high | ||
| 342 | + } else { | ||
| 343 | + &tables.f_low | ||
| 344 | + }; | ||
| 345 | + if table_n.len() == table.len() { | ||
| 346 | + let noise_row_n: Option<&Vec<f64>> = ch | ||
| 347 | + .t_noise | ||
| 348 | + .windows(2) | ||
| 349 | + .position(|w| t0n >= w[0] && t0n < w[1]) | ||
| 350 | + .and_then(|ni| noise_energy.get(ni)); | ||
| 351 | + Some(compute_gains_only( | ||
| 352 | + hf, | ||
| 353 | + tables, | ||
| 354 | + kx, | ||
| 355 | + table_n, | ||
| 356 | + t0n, | ||
| 357 | + t1n, | ||
| 358 | + &env_energy[ei + 1], | ||
| 359 | + noise_row_n, | ||
| 360 | + limiter_max, | ||
| 361 | + )) | ||
| 362 | + } else { | ||
| 363 | + None | ||
| 364 | + } | ||
| 365 | + } else { | ||
| 366 | + None | ||
| 367 | + }; | ||
| 317 | // `bs_interpol_freq` (per-subband linear interpolation of these sfb | 368 | // `bs_interpol_freq` (per-subband linear interpolation of these sfb |
| 318 | // gains, Sec 4.6.18.7.6) was tried here: a centre-frequency lerp | 369 | // gains, Sec 4.6.18.7.6) was tried here: a centre-frequency lerp |
| 319 | // between neighbouring sfbs' flat gain values, applied whenever | 370 | // between neighbouring sfbs' flat gain values, applied whenever |
| @@ -327,10 +378,12 @@ pub fn adjust( | |||
| 327 | // support, ceiling ~0.999 full-band bar; needs the reference | 378 | // support, ceiling ~0.999 full-band bar; needs the reference |
| 328 | // decoder's exact per-subband gain-interpolation formula, not a | 379 | // decoder's exact per-subband gain-interpolation formula, not a |
| 329 | // plausible reconstruction of it. | 380 | // plausible reconstruction of it. |
| 381 | + let span = (t1 - t0.max(0)).max(1) as f64; | ||
| 330 | for b in 0..table.len().saturating_sub(1) { | 382 | for b in 0..table.len().saturating_sub(1) { |
| 331 | let lo = table[b] as usize; | 383 | let lo = table[b] as usize; |
| 332 | let hi = table[b + 1] as usize; | 384 | let hi = table[b + 1] as usize; |
| 333 | - let gain = gains[b]; | 385 | + let base_gain = gains[b]; |
| 386 | + let end_gain = next_gains.as_ref().and_then(|ng| ng.get(b).copied()); | ||
| 334 | let namp = noise_amps[b]; | 387 | let namp = noise_amps[b]; |
| 335 | for band in lo..hi { | 388 | for band in lo..hi { |
| 336 | if band < kx || band - kx >= hf.len() { | 389 | if band < kx || band - kx >= hf.len() { |
| @@ -338,6 +391,12 @@ pub fn adjust( | |||
| 338 | } | 391 | } |
| 339 | let row = &mut hf[band - kx]; | 392 | let row = &mut hf[band - kx]; |
| 340 | for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { | 393 | for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { |
| 394 | + let gain = if let Some(end) = end_gain { | ||
| 395 | + let w = (slot as f64 - t0.max(0) as f64) / span; | ||
| 396 | + base_gain + (end - base_gain) * w.clamp(0.0, 1.0) | ||
| 397 | + } else { | ||
| 398 | + base_gain | ||
| 399 | + }; | ||
| 341 | row[slot] = row[slot].scale(gain); | 400 | row[slot] = row[slot].scale(gain); |
| 342 | // Consume the RNG unconditionally (matches the pre-round-21 | 401 | // Consume the RNG unconditionally (matches the pre-round-21 |
| 343 | // stream shape) even under EC_AAC_SBR_NOISE_ZERO, which | 402 | // stream shape) even under EC_AAC_SBR_NOISE_ZERO, which |
| @@ -394,6 +453,54 @@ pub fn adjust( | |||
| 394 | // unrelated unit scales; see the comment at `noise_div` above). | 453 | // unrelated unit scales; see the comment at `noise_div` above). |
| 395 | } | 454 | } |
| 396 | 455 | ||
| 456 | +/// (Round-22 sweep instrumentation) The same gain math the main loop in | ||
| 457 | +/// [`adjust`] computes, minus the tracking/noise-amplitude side effects -- | ||
| 458 | +/// used to look ahead at the NEXT envelope's gain for `EC_AAC_SBR_GAIN_LERP`. | ||
| 459 | +#[allow(clippy::too_many_arguments)] | ||
| 460 | +fn compute_gains_only( | ||
| 461 | + hf: &[Vec<Complex<f64>>], | ||
| 462 | + tables: &BandTables, | ||
| 463 | + kx: usize, | ||
| 464 | + table: &[i64], | ||
| 465 | + t0: i64, | ||
| 466 | + t1: i64, | ||
| 467 | + env_row: &[f64], | ||
| 468 | + noise_row: Option<&Vec<f64>>, | ||
| 469 | + limiter_max: f64, | ||
| 470 | +) -> Vec<f64> { | ||
| 471 | + let mut gains = Vec::with_capacity(table.len().saturating_sub(1)); | ||
| 472 | + for b in 0..table.len().saturating_sub(1) { | ||
| 473 | + let lo = table[b] as usize; | ||
| 474 | + let hi = table[b + 1] as usize; | ||
| 475 | + let target = env_row.get(b).copied().unwrap_or(0.0); | ||
| 476 | + let (mut sum, mut count) = (0.0f64, 0usize); | ||
| 477 | + for band in lo..hi { | ||
| 478 | + if band < kx || band - kx >= hf.len() { | ||
| 479 | + continue; | ||
| 480 | + } | ||
| 481 | + let row = &hf[band - kx]; | ||
| 482 | + for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { | ||
| 483 | + sum += row[slot].norm_sqr(); | ||
| 484 | + count += 1; | ||
| 485 | + } | ||
| 486 | + } | ||
| 487 | + let current = if count > 0 { sum / count as f64 } else { 0.0 }; | ||
| 488 | + let q = noise_band_for(tables, lo); | ||
| 489 | + let noise_div = noise_row | ||
| 490 | + .and_then(|r| r.get(q).copied()) | ||
| 491 | + .unwrap_or(0.0) | ||
| 492 | + .max(0.0); | ||
| 493 | + let signal_target = target / (1.0 + noise_div); | ||
| 494 | + let noise_here = target - signal_target; | ||
| 495 | + let cur = current.max(1e-12); | ||
| 496 | + let mut gain = (signal_target / cur).sqrt(); | ||
| 497 | + gain = gain.min(limiter_max * ((noise_here + cur) / cur).sqrt()); | ||
| 498 | + gain = gain.min(limiter_max * 64.0); | ||
| 499 | + gains.push(gain); | ||
| 500 | + } | ||
| 501 | + gains | ||
| 502 | +} | ||
| 503 | + | ||
| 397 | fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { | 504 | fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { |
| 398 | let mut q = 0usize; | 505 | let mut q = 0usize; |
| 399 | for i in 0..tables.n_q { | 506 | for i in 0..tables.n_q { |
crates/ec-aac/src/sbr_hf.rsmodified+53 -5
| @@ -107,19 +107,67 @@ impl ChirpState { | |||
| 107 | 107 | ||
| 108 | /// Advances the state one frame given this frame's `bs_invf_mode` per | 108 | /// Advances the state one frame given this frame's `bs_invf_mode` per |
| 109 | /// noise band, returning the smoothed bw to use for HF generation. | 109 | /// noise band, returning the smoothed bw to use for HF generation. |
| 110 | + /// | ||
| 111 | + /// (Round-22 sweep instrumentation, zero cost unset) `EC_AAC_SBR_BW_SCALE` | ||
| 112 | + /// scales `BW_TABLE`'s target globally (clamped below 1.0) before | ||
| 113 | + /// smoothing, to probe whether the chirp/inverse-filter strength is | ||
| 114 | + /// miscalibrated on real content. `EC_AAC_SBR_CHIRP_SMOOTH=none` skips | ||
| 115 | + /// the attack/release smoothing entirely (target used immediately); | ||
| 116 | + /// `=swap` swaps which curve (0.75/0.25 vs 0.90625/0.09375) applies to | ||
| 117 | + /// rising vs falling bw, to probe whether the asymmetry is backwards. | ||
| 110 | pub fn update(&mut self, invf_mode: &[u8]) -> &[f64] { | 118 | pub fn update(&mut self, invf_mode: &[u8]) -> &[f64] { |
| 119 | + let scale = bw_scale_override(); | ||
| 120 | + let smooth = chirp_smooth_mode(); | ||
| 111 | for (slot, &mode) in self.bw.iter_mut().zip(invf_mode) { | 121 | for (slot, &mode) in self.bw.iter_mut().zip(invf_mode) { |
| 112 | - let target = BW_TABLE[usize::from(mode).min(3)]; | 122 | + let target = (BW_TABLE[usize::from(mode).min(3)] * scale).min(0.999_999); |
| 113 | - *slot = if target < *slot { | 123 | + *slot = match smooth { |
| 114 | - 0.75 * target + 0.25 * *slot | 124 | + ChirpSmooth::None => target, |
| 115 | - } else { | 125 | + ChirpSmooth::Current => { |
| 116 | - 0.90625 * *slot + 0.09375 * target | 126 | + if target < *slot { |
| 127 | + 0.75 * target + 0.25 * *slot | ||
| 128 | + } else { | ||
| 129 | + 0.90625 * *slot + 0.09375 * target | ||
| 130 | + } | ||
| 131 | + } | ||
| 132 | + ChirpSmooth::Swap => { | ||
| 133 | + if target < *slot { | ||
| 134 | + 0.90625 * target + 0.09375 * *slot | ||
| 135 | + } else { | ||
| 136 | + 0.75 * *slot + 0.25 * target | ||
| 137 | + } | ||
| 138 | + } | ||
| 117 | }; | 139 | }; |
| 118 | } | 140 | } |
| 119 | &self.bw | 141 | &self.bw |
| 120 | } | 142 | } |
| 121 | } | 143 | } |
| 122 | 144 | ||
| 145 | +/// `EC_AAC_SBR_BW_SCALE` global multiplier on `BW_TABLE`, clamped below 1.0 | ||
| 146 | +/// (a chirp target at or past 1.0 is an undamped all-pole resonance, not a | ||
| 147 | +/// meaningful "stronger" whitening). `1.0` (no-op) when unset or unparsable. | ||
| 148 | +fn bw_scale_override() -> f64 { | ||
| 149 | + std::env::var("EC_AAC_SBR_BW_SCALE") | ||
| 150 | + .ok() | ||
| 151 | + .and_then(|s| s.parse::<f64>().ok()) | ||
| 152 | + .map(|s| s.max(0.0)) | ||
| 153 | + .unwrap_or(1.0) | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +#[derive(Clone, Copy, PartialEq, Eq)] | ||
| 157 | +enum ChirpSmooth { | ||
| 158 | + None, | ||
| 159 | + Current, | ||
| 160 | + Swap, | ||
| 161 | +} | ||
| 162 | + | ||
| 163 | +fn chirp_smooth_mode() -> ChirpSmooth { | ||
| 164 | + match std::env::var("EC_AAC_SBR_CHIRP_SMOOTH").as_deref() { | ||
| 165 | + Ok("none") => ChirpSmooth::None, | ||
| 166 | + Ok("swap") => ChirpSmooth::Swap, | ||
| 167 | + _ => ChirpSmooth::Current, | ||
| 168 | + } | ||
| 169 | +} | ||
| 170 | + | ||
| 123 | /// Order-2 covariance-method LPC over `x` (index 0 is the oldest sample): | 171 | /// Order-2 covariance-method LPC over `x` (index 0 is the oldest sample): |
| 124 | /// solves the 2x2 normal equations minimizing `sum |x[n] - a1*x[n-1] - | 172 | /// solves the 2x2 normal equations minimizing `sum |x[n] - a1*x[n-1] - |
| 125 | /// a2*x[n-2]|^2` over `n = 2..x.len()`. Returns `(a1, a2)`; `(0, 0)` if `x` | 173 | /// a2*x[n-2]|^2` over `n = 2..x.len()`. Returns `(a1, a2)`; `(0, 0)` if `x` |
crates/ec-aac/tests/sbr_real_library.rsmodified+11 -1
| @@ -1053,7 +1053,17 @@ fn sbr_real_library_matches_reference() { | |||
| 1053 | correlation_at_lag(&ours[0], &ours[1], 0), | 1053 | correlation_at_lag(&ours[0], &ours[1], 0), |
| 1054 | ); | 1054 | ); |
| 1055 | } | 1055 | } |
| 1056 | - for (ch, (o, t)) in ours.iter().zip(&theirs).enumerate() { | 1056 | + for (ch, (o_full, t_full)) in ours.iter().zip(&theirs).enumerate() { |
| 1057 | + // (Round-22 stability check) EC_AAC_SBR_SWEEP_SEGMENT=1 measures | ||
| 1058 | + // the second half of the file instead of the first, so a | ||
| 1059 | + // calibration candidate can be checked on two disjoint segments | ||
| 1060 | + // rather than just the file's start. | ||
| 1061 | + let (o, t): (&[f32], &[f32]) = | ||
| 1062 | + if std::env::var("EC_AAC_SBR_SWEEP_SEGMENT").as_deref() == Ok("1") { | ||
| 1063 | + (&o_full[o_full.len() / 2..], &t_full[t_full.len() / 2..]) | ||
| 1064 | + } else { | ||
| 1065 | + (&o_full[..], &t_full[..]) | ||
| 1066 | + }; | ||
| 1057 | let rms = |s: &[f32]| { | 1067 | let rms = |s: &[f32]| { |
| 1058 | (s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::<f64>() | 1068 | (s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::<f64>() |
| 1059 | / s.len().max(1) as f64) | 1069 | / s.len().max(1) as f64) |