SBR limiter caps per limiter-band aggregate, not per empty cell
Browse files at this commit0a60b22082eccb67979e64829d76335b38d5c4d7Parent: 854ea37- fix
- SBR limiter caps per limiter-band aggregate, not per empty cell
3 changed files +248 -27
- crates/ec-aac/src/sbr_chain.rs +10 -0
- crates/ec-aac/src/sbr_env.rs +184 -27
- crates/ec-aac/src/sbr_hf.rs +54 -0
crates/ec-aac/src/sbr_chain.rsmodified+10 -0
| @@ -319,6 +319,15 @@ impl SbrChain { | |||
| 319 | t_noise: sbr_ch.t_noise.iter().map(|&t| t * RATE).collect(), | 319 | t_noise: sbr_ch.t_noise.iter().map(|&t| t * RATE).collect(), |
| 320 | ..sbr_ch.clone() | 320 | ..sbr_ch.clone() |
| 321 | }; | 321 | }; |
| 322 | + // Same deterministic `build_patches(&tables)` call `generate` | ||
| 323 | + // makes internally -- cheap to recompute here so the limiter | ||
| 324 | + // band table (patch boundaries + f_low, Sec 4.6.18.7.2) can be | ||
| 325 | + // built without threading patches out of `generate`'s signature. | ||
| 326 | + let limiter_table = sbr_hf::limiter_band_table( | ||
| 327 | + &tables, | ||
| 328 | + &sbr_hf::build_patches(&tables), | ||
| 329 | + header.limiter_bands, | ||
| 330 | + ); | ||
| 322 | sbr_env::adjust( | 331 | sbr_env::adjust( |
| 323 | &mut hf, | 332 | &mut hf, |
| 324 | &tables, | 333 | &tables, |
| @@ -327,6 +336,7 @@ impl SbrChain { | |||
| 327 | &env_energy, | 336 | &env_energy, |
| 328 | &noise_energy, | 337 | &noise_energy, |
| 329 | &mut state.noise, | 338 | &mut state.noise, |
| 339 | + &limiter_table, | ||
| 330 | ); | 340 | ); |
| 331 | 341 | ||
| 332 | let kx = (tables.kx as usize).min(ANALYSIS_BANDS); | 342 | let kx = (tables.kx as usize).min(ANALYSIS_BANDS); |
crates/ec-aac/src/sbr_env.rsmodified+184 -27
| @@ -34,10 +34,14 @@ | |||
| 34 | //! `Q_div = dequant_noise(q_q)` ratio (`signal = target/(1+Q_div)`, | 34 | //! `Q_div = dequant_noise(q_q)` ratio (`signal = target/(1+Q_div)`, |
| 35 | //! `noise = target*Q_div/(1+Q_div)`, round-21 -- `Q_div` is a ratio | 35 | //! `noise = target*Q_div/(1+Q_div)`, round-21 -- `Q_div` is a ratio |
| 36 | //! relative to the envelope target, not a standalone absolute energy), the | 36 | //! relative to the envelope target, not a standalone absolute energy), the |
| 37 | -//! HF estimate is scaled toward the signal share, the limiter caps how far | 37 | +//! HF estimate is scaled toward the signal share; the limiter then caps |
| 38 | -//! above the cell's own noise floor that gain is allowed to reach | 38 | +//! every cell's gain against its LIMITER BAND's aggregate headroom (Sec |
| 39 | -//! (`bs_limiter_gains`), the noise share is mixed in at that level, and | 39 | +//! 4.6.18.7.2/.7.5, `bs_limiter_gains`, `crate::sbr_hf::limiter_band_table`) |
| 40 | -//! flagged bands get an added tone. | 40 | +//! rather than each cell's own -- a content-empty cell has zero raw energy, |
| 41 | +//! so a per-cell cap is infinite exactly where it should bite; the | ||
| 42 | +//! aggregate cap and its gain-boost compensation (Sec 4.6.18.7.4) are | ||
| 43 | +//! applied to gains, injected noise and sinusoids alike. The noise share is | ||
| 44 | +//! mixed in at that level, and flagged bands get an added tone. | ||
| 41 | #![allow(clippy::needless_range_loop)] | 45 | #![allow(clippy::needless_range_loop)] |
| 42 | 46 | ||
| 43 | use crate::sbr_bands::BandTables; | 47 | use crate::sbr_bands::BandTables; |
| @@ -152,7 +156,17 @@ impl NoiseGen { | |||
| 152 | } | 156 | } |
| 153 | } | 157 | } |
| 154 | 158 | ||
| 155 | -const LIMITER_FACTOR: [f64; 4] = [1.4125, 2.0, 4.0, 1.0e6]; // 1.5/3/6 dB, "no limit" | 159 | +// (Round-33) Per-limiter-band aggregate gain factor, ISO/IEC 14496-3 |
| 160 | +// §4.6.18.7.2's `bs_limiter_gains` table: -3/0/+3 dB, and index 3 | ||
| 161 | +// ("no limiting") given a very large but finite ceiling so a limiter band | ||
| 162 | +// with zero measured HF energy still can't blow up to a literal infinity. | ||
| 163 | +const LIMITER_FACTOR: [f64; 4] = [0.707_945_78, 1.0, 1.412_537_5, 1.0e10]; | ||
| 164 | + | ||
| 165 | +/// Gain-boost compensation ceiling (+4 dB, §4.6.18.7.4): after the | ||
| 166 | +/// per-limiter-band cap, a band whose achieved energy fell short of its | ||
| 167 | +/// transmitted envelope total is boosted back up, capped here so a band | ||
| 168 | +/// that was capped hard doesn't get fully "un-capped" by the boost step. | ||
| 169 | +const BOOST_MAX: f64 = 1.584_893_2; | ||
| 156 | 170 | ||
| 157 | /// (Round-17, Task 1 conviction check) Accumulates transmitted signal vs. | 171 | /// (Round-17, Task 1 conviction check) Accumulates transmitted signal vs. |
| 158 | /// noise energy per absolute QMF band across every [`adjust`] call in the | 172 | /// noise energy per absolute QMF band across every [`adjust`] call in the |
| @@ -209,8 +223,10 @@ pub fn adjust( | |||
| 209 | env_energy: &[Vec<f64>], | 223 | env_energy: &[Vec<f64>], |
| 210 | noise_energy: &[Vec<f64>], | 224 | noise_energy: &[Vec<f64>], |
| 211 | rng: &mut NoiseGen, | 225 | rng: &mut NoiseGen, |
| 226 | + limiter_table: &[i64], | ||
| 212 | ) { | 227 | ) { |
| 213 | let kx = tables.kx as usize; | 228 | let kx = tables.kx as usize; |
| 229 | + let n_lim = limiter_table.len().saturating_sub(1).max(1); | ||
| 214 | // (Round-22 sweep instrumentation, zero cost unset) `EC_AAC_SBR_LIMITER_OFF` | 230 | // (Round-22 sweep instrumentation, zero cost unset) `EC_AAC_SBR_LIMITER_OFF` |
| 215 | // forces the "no limit" factor regardless of the transmitted | 231 | // forces the "no limit" factor regardless of the transmitted |
| 216 | // `bs_limiter_gains`, to probe whether the limiter itself is clipping | 232 | // `bs_limiter_gains`, to probe whether the limiter itself is clipping |
| @@ -259,12 +275,31 @@ pub fn adjust( | |||
| 259 | // linearly interpolated across subbands between sfb centre | 275 | // linearly interpolated across subbands between sfb centre |
| 260 | // frequencies) so both application modes share the exact same gain | 276 | // frequencies) so both application modes share the exact same gain |
| 261 | // values. | 277 | // values. |
| 278 | + // | ||
| 279 | + // (Round-33) The limiter no longer caps each cell against its OWN | ||
| 280 | + // near-zero raw HF estimate (that cap is infinite exactly where a | ||
| 281 | + // patch left a target band content-empty, Sec 4.6.18.7.5's known | ||
| 282 | + // failure mode for cell-local caps). Instead this is a two-pass | ||
| 283 | + // per-LIMITER-BAND aggregate: pass 1 computes each cell's raw | ||
| 284 | + // (uncapped) gain and accumulates that cell's transmitted/measured | ||
| 285 | + // energy into its limiter band (`limiter_table`, built from | ||
| 286 | + // f_low/patch borders in sbr_hf::limiter_band_table); pass 2 caps | ||
| 287 | + // every cell in a limiter band at that band's AGGREGATE | ||
| 288 | + // `sqrt(E_orig/E_curr)*limgain` ratio, so an empty cell inherits its | ||
| 289 | + // band's cap rather than blowing up alone, then boosts the whole | ||
| 290 | + // band back up (capped at `BOOST_MAX`) to still meet the band's | ||
| 291 | + // transmitted total. | ||
| 262 | let mut gains = Vec::with_capacity(table.len().saturating_sub(1)); | 292 | let mut gains = Vec::with_capacity(table.len().saturating_sub(1)); |
| 263 | // (Round-21) Actual per-cell noise energy this envelope/band injects, | 293 | // (Round-21) Actual per-cell noise energy this envelope/band injects, |
| 264 | // parallel to `gains` -- filled below and consumed by the | 294 | // parallel to `gains` -- filled below and consumed by the |
| 265 | // application loop that follows instead of the old separate | 295 | // application loop that follows instead of the old separate |
| 266 | // noise-grid pass (see round-21 module doc note). | 296 | // noise-grid pass (see round-21 module doc note). |
| 267 | let mut noise_amps = Vec::with_capacity(table.len().saturating_sub(1)); | 297 | let mut noise_amps = Vec::with_capacity(table.len().saturating_sub(1)); |
| 298 | + let mut cell_current = Vec::with_capacity(table.len().saturating_sub(1)); | ||
| 299 | + let mut cell_noise_here = Vec::with_capacity(table.len().saturating_sub(1)); | ||
| 300 | + let mut cell_lim = Vec::with_capacity(table.len().saturating_sub(1)); | ||
| 301 | + let mut lim_e_orig = vec![0.0f64; n_lim]; | ||
| 302 | + let mut lim_e_curr = vec![0.0f64; n_lim]; | ||
| 268 | for b in 0..table.len().saturating_sub(1) { | 303 | for b in 0..table.len().saturating_sub(1) { |
| 269 | let lo = table[b] as usize; | 304 | let lo = table[b] as usize; |
| 270 | let hi = table[b + 1] as usize; | 305 | let hi = table[b + 1] as usize; |
| @@ -301,23 +336,16 @@ pub fn adjust( | |||
| 301 | .max(0.0); | 336 | .max(0.0); |
| 302 | let signal_target = target / (1.0 + noise_div); | 337 | let signal_target = target / (1.0 + noise_div); |
| 303 | let noise_here = target - signal_target; // = target*noise_div/(1+noise_div) | 338 | let noise_here = target - signal_target; // = target*noise_div/(1+noise_div) |
| 304 | - let mut gain = (signal_target / current.max(1e-12)).sqrt(); | ||
| 305 | - // Cap gain at `limiter_max` times the cell's own | ||
| 306 | - // noise-plus-signal headroom over its raw HF estimate (Sec | ||
| 307 | - // 4.6.18.7.5's `limGain * sqrt((Q_M+E_curr)/E_curr)` shape): a | ||
| 308 | - // cell with no transmitted noise floor still gets the plain | ||
| 309 | - // `limiter_max` ceiling (the `current` term alone gives | ||
| 310 | - // `sqrt(current/current)=1`), while a cell whose noise floor | ||
| 311 | - // dominates its raw estimate gets extra headroom proportional to | ||
| 312 | - // that ratio. The previous `.max(gain.min(limiter_max))` here | ||
| 313 | - // floored the cap at `min(gain, limiter_max)`, which is always | ||
| 314 | - // >= the intended cap and so never actually restricted | ||
| 315 | - // anything. | ||
| 316 | let cur = current.max(1e-12); | 339 | let cur = current.max(1e-12); |
| 317 | - gain = gain.min(limiter_max * ((noise_here + cur) / cur).sqrt()); | 340 | + let raw_gain = (signal_target / cur).sqrt(); |
| 318 | - gain = gain.min(limiter_max * 64.0); // absolute ceiling: never amplify a silent cell to infinity | 341 | + let lim_idx = limiter_band_index(limiter_table, lo, hi).min(n_lim - 1); |
| 319 | - gains.push(gain); | 342 | + let width = (hi - lo).max(1) as f64; |
| 320 | - noise_amps.push((noise_here / (hi - lo).max(1) as f64).sqrt()); | 343 | + lim_e_orig[lim_idx] += target * width; |
| 344 | + lim_e_curr[lim_idx] += cur * width; | ||
| 345 | + cell_current.push(cur); | ||
| 346 | + cell_noise_here.push(noise_here); | ||
| 347 | + cell_lim.push(lim_idx); | ||
| 348 | + gains.push(raw_gain); | ||
| 321 | if track_fraction { | 349 | if track_fraction { |
| 322 | let slots = (t1.max(0) - t0.max(0)).max(0) as usize; | 350 | let slots = (t1.max(0) - t0.max(0)).max(0) as usize; |
| 323 | for band in lo..hi { | 351 | for band in lo..hi { |
| @@ -328,6 +356,38 @@ pub fn adjust( | |||
| 328 | } | 356 | } |
| 329 | } | 357 | } |
| 330 | } | 358 | } |
| 359 | + // Pass 2: per-limiter-band aggregate cap, then per-band boost | ||
| 360 | + // compensation so the band still meets its transmitted total. | ||
| 361 | + let lim_cap: Vec<f64> = (0..n_lim) | ||
| 362 | + .map(|j| limiter_max * (lim_e_orig[j] / lim_e_curr[j].max(1e-12)).sqrt()) | ||
| 363 | + .collect(); | ||
| 364 | + let mut lim_e_actual = vec![0.0f64; n_lim]; | ||
| 365 | + for b in 0..gains.len() { | ||
| 366 | + let j = cell_lim[b]; | ||
| 367 | + gains[b] = gains[b].min(lim_cap[j]); | ||
| 368 | + let lo = table[b] as usize; | ||
| 369 | + let hi = table[b + 1] as usize; | ||
| 370 | + let width = (hi - lo).max(1) as f64; | ||
| 371 | + lim_e_actual[j] += cell_current[b] * gains[b] * gains[b] * width; | ||
| 372 | + } | ||
| 373 | + let lim_boost: Vec<f64> = (0..n_lim) | ||
| 374 | + .map(|j| { | ||
| 375 | + if lim_e_actual[j] > 1e-12 { | ||
| 376 | + (lim_e_orig[j] / lim_e_actual[j]) | ||
| 377 | + .sqrt() | ||
| 378 | + .clamp(1.0, BOOST_MAX) | ||
| 379 | + } else { | ||
| 380 | + 1.0 | ||
| 381 | + } | ||
| 382 | + }) | ||
| 383 | + .collect(); | ||
| 384 | + for b in 0..gains.len() { | ||
| 385 | + let j = cell_lim[b]; | ||
| 386 | + gains[b] *= lim_boost[j]; | ||
| 387 | + let lo = table[b] as usize; | ||
| 388 | + let hi = table[b + 1] as usize; | ||
| 389 | + noise_amps.push((cell_noise_here[b] / (hi - lo).max(1) as f64).sqrt() * lim_boost[j]); | ||
| 390 | + } | ||
| 331 | // (Round-22 sweep instrumentation) the next envelope's gains, only | 391 | // (Round-22 sweep instrumentation) the next envelope's gains, only |
| 332 | // when `EC_AAC_SBR_GAIN_LERP` is set and the band layouts match -- | 392 | // 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 | 393 | // `hf` in the next envelope's time range is still untouched at this |
| @@ -429,7 +489,8 @@ pub fn adjust( | |||
| 429 | .unwrap_or(0); | 489 | .unwrap_or(0); |
| 430 | env_energy[ei].get(lb).copied().unwrap_or(0.0) | 490 | env_energy[ei].get(lb).copied().unwrap_or(0.0) |
| 431 | }; | 491 | }; |
| 432 | - let amp = (2.0 * e).sqrt(); | 492 | + let boost = lim_boost[limiter_band_index(limiter_table, lo, hi).min(n_lim - 1)]; |
| 493 | + let amp = (2.0 * e).sqrt() * boost; | ||
| 433 | for band in lo..hi { | 494 | for band in lo..hi { |
| 434 | if band < kx || band - kx >= hf.len() { | 495 | if band < kx || band - kx >= hf.len() { |
| 435 | continue; | 496 | continue; |
| @@ -501,6 +562,21 @@ fn compute_gains_only( | |||
| 501 | gains | 562 | gains |
| 502 | } | 563 | } |
| 503 | 564 | ||
| 565 | +/// Finds which limiter band (from [`crate::sbr_hf::limiter_band_table`]) | ||
| 566 | +/// a `[lo, hi)` QMF-band cell falls into, by its midpoint. | ||
| 567 | +fn limiter_band_index(limiter_table: &[i64], lo: usize, hi: usize) -> usize { | ||
| 568 | + if limiter_table.len() < 2 { | ||
| 569 | + return 0; | ||
| 570 | + } | ||
| 571 | + let mid = (lo + hi) as i64 / 2; | ||
| 572 | + for j in 0..limiter_table.len() - 1 { | ||
| 573 | + if mid < limiter_table[j + 1] { | ||
| 574 | + return j; | ||
| 575 | + } | ||
| 576 | + } | ||
| 577 | + limiter_table.len() - 2 | ||
| 578 | +} | ||
| 579 | + | ||
| 504 | fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { | 580 | fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { |
| 505 | let mut q = 0usize; | 581 | let mut q = 0usize; |
| 506 | for i in 0..tables.n_q { | 582 | for i in 0..tables.n_q { |
| @@ -515,6 +591,7 @@ fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { | |||
| 515 | mod tests { | 591 | mod tests { |
| 516 | use super::*; | 592 | use super::*; |
| 517 | use crate::sbr_bands::freq_tables; | 593 | use crate::sbr_bands::freq_tables; |
| 594 | + use crate::sbr_hf::{build_patches, limiter_band_table}; | ||
| 518 | use crate::sbr_payload::SbrChannel; | 595 | use crate::sbr_payload::SbrChannel; |
| 519 | 596 | ||
| 520 | fn flat_tables() -> BandTables { | 597 | fn flat_tables() -> BandTables { |
| @@ -557,6 +634,8 @@ mod tests { | |||
| 557 | let env_energy = vec![vec![target; tables.n_low]]; | 634 | let env_energy = vec![vec![target; tables.n_low]]; |
| 558 | let noise_energy = vec![vec![0.0f64; tables.n_q]]; | 635 | let noise_energy = vec![vec![0.0f64; tables.n_q]]; |
| 559 | let mut rng = NoiseGen::new(1); | 636 | let mut rng = NoiseGen::new(1); |
| 637 | + let lim_table = | ||
| 638 | + limiter_band_table(&tables, &build_patches(&tables), header(1, 3).limiter_bands); | ||
| 560 | adjust( | 639 | adjust( |
| 561 | &mut hf, | 640 | &mut hf, |
| 562 | &tables, | 641 | &tables, |
| @@ -565,6 +644,7 @@ mod tests { | |||
| 565 | &env_energy, | 644 | &env_energy, |
| 566 | &noise_energy, | 645 | &noise_energy, |
| 567 | &mut rng, | 646 | &mut rng, |
| 647 | + &lim_table, | ||
| 568 | ); | 648 | ); |
| 569 | 649 | ||
| 570 | // Every low-res band's average energy should land close to target. | 650 | // Every low-res band's average energy should land close to target. |
| @@ -594,13 +674,24 @@ mod tests { | |||
| 594 | } | 674 | } |
| 595 | 675 | ||
| 596 | #[test] | 676 | #[test] |
| 597 | - fn limiter_caps_a_pathological_gain() { | 677 | + fn limiter_stays_finite_on_a_uniformly_starved_band() { |
| 678 | + // (Round-33 semantics change) A UNIFORMLY near-silent HF estimate | ||
| 679 | + // against a huge uniform target is no longer expected to be capped | ||
| 680 | + // down near the raw HF amplitude -- the aggregate cap and its | ||
| 681 | + // gain-boost compensation are computed from the SAME population, so | ||
| 682 | + // when every cell in a limiter band is equally starved (no | ||
| 683 | + // disproportion for the limiter to actually correct), full recovery | ||
| 684 | + // toward the transmitted target is the spec-correct outcome (boost | ||
| 685 | + // undoes exactly what the cap took away, as long as the needed | ||
| 686 | + // `1/limiter_max` stays under `BOOST_MAX`). What must NOT happen | ||
| 687 | + // (the actual pre-fix bug) is a literal unbounded/non-finite blow-up | ||
| 688 | + // -- this asserts finiteness and a generous bound around the target | ||
| 689 | + // amplitude, not the old (now-wrong) "stays near the raw estimate" | ||
| 690 | + // shape. | ||
| 598 | let tables = flat_tables(); | 691 | let tables = flat_tables(); |
| 599 | let kx = tables.kx as usize; | 692 | let kx = tables.kx as usize; |
| 600 | let k2 = tables.k2 as usize; | 693 | let k2 = tables.k2 as usize; |
| 601 | let slots = 16usize; | 694 | let slots = 16usize; |
| 602 | - // Near-silent HF estimate against a huge target energy: an | ||
| 603 | - // unlimited gain would blow this up by many orders of magnitude. | ||
| 604 | let mut hf = vec![vec![Complex::new(1e-9, 0.0); slots]; k2 - kx]; | 695 | let mut hf = vec![vec![Complex::new(1e-9, 0.0); slots]; k2 - kx]; |
| 605 | let ch = SbrChannel { | 696 | let ch = SbrChannel { |
| 606 | t_env: vec![0, 16], | 697 | t_env: vec![0, 16], |
| @@ -618,6 +709,8 @@ mod tests { | |||
| 618 | // noise injection. | 709 | // noise injection. |
| 619 | let noise_energy = vec![vec![0.0f64; tables.n_q]]; | 710 | let noise_energy = vec![vec![0.0f64; tables.n_q]]; |
| 620 | let mut rng = NoiseGen::new(1); | 711 | let mut rng = NoiseGen::new(1); |
| 712 | + let lim_table = | ||
| 713 | + limiter_band_table(&tables, &build_patches(&tables), header(1, 0).limiter_bands); | ||
| 621 | adjust( | 714 | adjust( |
| 622 | &mut hf, | 715 | &mut hf, |
| 623 | &tables, | 716 | &tables, |
| @@ -626,18 +719,82 @@ mod tests { | |||
| 626 | &env_energy, | 719 | &env_energy, |
| 627 | &noise_energy, | 720 | &noise_energy, |
| 628 | &mut rng, | 721 | &mut rng, |
| 722 | + &lim_table, | ||
| 629 | ); | 723 | ); |
| 724 | + let target_amp = 1.0e6f64.sqrt(); // ~1000, the envelope's own amplitude scale | ||
| 630 | for band in 0..hf.len() { | 725 | for band in 0..hf.len() { |
| 631 | for slot in 0..slots { | 726 | for slot in 0..slots { |
| 727 | + let amp = hf[band][slot].norm_sqr().sqrt(); | ||
| 632 | assert!( | 728 | assert!( |
| 633 | - hf[band][slot].norm_sqr().sqrt() < 1.0, | 729 | + amp.is_finite() && amp < 10.0 * target_amp, |
| 634 | - "limiter failed to cap band {band} slot {slot}: {:?}", | 730 | + "gain escaped to a non-finite/unbounded value at band {band} slot {slot}: {:?}", |
| 635 | hf[band][slot] | 731 | hf[band][slot] |
| 636 | ); | 732 | ); |
| 637 | } | 733 | } |
| 638 | } | 734 | } |
| 639 | } | 735 | } |
| 640 | 736 | ||
| 737 | + #[test] | ||
| 738 | + fn a_content_empty_cell_inherits_its_limiter_bands_aggregate_cap() { | ||
| 739 | + // (Round-33) One low-res band has real HF content, its neighbour is | ||
| 740 | + // perfectly silent (current=0, the exact case a per-cell cap makes | ||
| 741 | + // infinite -- round-33's convicted mechanism). Both fall in the | ||
| 742 | + // same limiter_bands=0 (single aggregate band) table here, so the | ||
| 743 | + // silent cell must be capped by the AGGREGATE ratio (dominated by | ||
| 744 | + // the loud cell's huge current), not blown up on its own. | ||
| 745 | + let tables = flat_tables(); | ||
| 746 | + let kx = tables.kx as usize; | ||
| 747 | + let k2 = tables.k2 as usize; | ||
| 748 | + let slots = 16usize; | ||
| 749 | + let mut hf = vec![vec![Complex::new(0.1, 0.0); slots]; k2 - kx]; | ||
| 750 | + // Silence the top half of the HF region (the empty target cell). | ||
| 751 | + let mid = hf.len() / 2; | ||
| 752 | + for row in &mut hf[mid..] { | ||
| 753 | + for c in row.iter_mut() { | ||
| 754 | + *c = Complex::ZERO; | ||
| 755 | + } | ||
| 756 | + } | ||
| 757 | + let ch = SbrChannel { | ||
| 758 | + t_env: vec![0, 16], | ||
| 759 | + freq_res: vec![0], | ||
| 760 | + t_noise: vec![0, 16], | ||
| 761 | + e_q: vec![vec![]], | ||
| 762 | + q_q: vec![vec![]], | ||
| 763 | + invf_mode: vec![], | ||
| 764 | + add_harmonic: None, | ||
| 765 | + }; | ||
| 766 | + let target = 4.0f64; | ||
| 767 | + let env_energy = vec![vec![target; tables.n_low]]; | ||
| 768 | + let noise_energy = vec![vec![0.0f64; tables.n_q]]; | ||
| 769 | + let mut rng = NoiseGen::new(1); | ||
| 770 | + let mut h = header(1, 1); | ||
| 771 | + h.limiter_bands = 0; // single aggregate band spanning the whole HF region | ||
| 772 | + let lim_table = limiter_band_table(&tables, &build_patches(&tables), h.limiter_bands); | ||
| 773 | + assert_eq!(lim_table, vec![tables.kx, tables.k2]); | ||
| 774 | + adjust( | ||
| 775 | + &mut hf, | ||
| 776 | + &tables, | ||
| 777 | + &h, | ||
| 778 | + &ch, | ||
| 779 | + &env_energy, | ||
| 780 | + &noise_energy, | ||
| 781 | + &mut rng, | ||
| 782 | + &lim_table, | ||
| 783 | + ); | ||
| 784 | + // The previously-silent cells must stay bounded (not blown up to | ||
| 785 | + // the old per-cell `limiter_max*64` absolute ceiling on a target of | ||
| 786 | + // 4.0 -- they should land well under it, inheriting the band's | ||
| 787 | + // aggregate cap instead). | ||
| 788 | + for row in &hf[mid..] { | ||
| 789 | + for c in row { | ||
| 790 | + assert!( | ||
| 791 | + c.norm_sqr() < target * 4.0, | ||
| 792 | + "empty cell escaped its limiter band's aggregate cap: {c:?}" | ||
| 793 | + ); | ||
| 794 | + } | ||
| 795 | + } | ||
| 796 | + } | ||
| 797 | + | ||
| 641 | #[test] | 798 | #[test] |
| 642 | fn coupled_balance_round_trips_at_a_centred_ratio() { | 799 | fn coupled_balance_round_trips_at_a_centred_ratio() { |
| 643 | // The ratio's exponent is `2*alpha*balance_raw - 12` (pinned against | 800 | // The ratio's exponent is `2*alpha*balance_raw - 12` (pinned against |
crates/ec-aac/src/sbr_hf.rsmodified+54 -0
| @@ -88,6 +88,60 @@ pub fn build_patches(tables: &BandTables) -> Vec<Patch> { | |||
| 88 | patches | 88 | patches |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | +/// Builds the limiter band table (ISO/IEC 14496-3 §4.6.18.7.2): the union | ||
| 92 | +/// of the low-resolution envelope band borders (`tables.f_low`) and the | ||
| 93 | +/// patch target boundaries, thinned so no band is narrower than one | ||
| 94 | +/// limiter band's worth of octaves at the transmitted `bs_limiter_bands` | ||
| 95 | +/// density (`0` => a single band spanning the whole HF region; `1`/`2`/`3` | ||
| 96 | +/// => ~1/2/3 bands per octave, log2-spaced the same way [`f_master`]'s own | ||
| 97 | +/// band construction thins its candidate borders). The limiter's gain cap | ||
| 98 | +/// is applied per band on this table, aggregated over every cell it | ||
| 99 | +/// covers, so a content-empty target cell inherits its band's aggregate | ||
| 100 | +/// cap instead of amplifying numeric dust toward an unbounded per-cell | ||
| 101 | +/// ratio. | ||
| 102 | +pub fn limiter_band_table(tables: &BandTables, patches: &[Patch], limiter_bands: u8) -> Vec<i64> { | ||
| 103 | + let kx = tables.kx; | ||
| 104 | + let k2 = tables.k2; | ||
| 105 | + if kx >= k2 { | ||
| 106 | + return vec![kx, k2.max(kx)]; | ||
| 107 | + } | ||
| 108 | + if limiter_bands == 0 { | ||
| 109 | + return vec![kx, k2]; | ||
| 110 | + } | ||
| 111 | + let bands_per_octave = match limiter_bands { | ||
| 112 | + 1 => 1.0, | ||
| 113 | + 2 => 2.0, | ||
| 114 | + _ => 3.0, | ||
| 115 | + }; | ||
| 116 | + let mut borders: Vec<i64> = tables | ||
| 117 | + .f_low | ||
| 118 | + .iter() | ||
| 119 | + .copied() | ||
| 120 | + .chain(patches.iter().map(|p| p.target_start as i64)) | ||
| 121 | + .chain(std::iter::once(k2)) | ||
| 122 | + .chain(std::iter::once(kx)) | ||
| 123 | + .filter(|&b| b >= kx && b <= k2) | ||
| 124 | + .collect(); | ||
| 125 | + borders.sort_unstable(); | ||
| 126 | + borders.dedup(); | ||
| 127 | + let mut out = vec![borders[0]]; | ||
| 128 | + for &b in &borders[1..] { | ||
| 129 | + let last = *out.last().unwrap(); | ||
| 130 | + if b <= last { | ||
| 131 | + continue; | ||
| 132 | + } | ||
| 133 | + let octaves = (b as f64 / last as f64).log2(); | ||
| 134 | + if octaves * bands_per_octave < 1.0 && b != k2 { | ||
| 135 | + continue; // too narrow on its own: merges into the current band | ||
| 136 | + } | ||
| 137 | + out.push(b); | ||
| 138 | + } | ||
| 139 | + if *out.last().unwrap() != k2 { | ||
| 140 | + out.push(k2); | ||
| 141 | + } | ||
| 142 | + out | ||
| 143 | +} | ||
| 144 | + | ||
| 91 | /// `bs_invf_mode` bandwidth-expansion targets (OFF, LOW, MID, HIGH), the | 145 | /// `bs_invf_mode` bandwidth-expansion targets (OFF, LOW, MID, HIGH), the |
| 92 | /// four inverse-filtering strengths §4.6.18.6.2 defines. | 146 | /// four inverse-filtering strengths §4.6.18.6.2 defines. |
| 93 | const BW_TABLE: [f64; 4] = [0.0, 0.6, 0.9, 0.98]; | 147 | const BW_TABLE: [f64; 4] = [0.0, 0.6, 0.9, 0.98]; |