Spec patch construction (4.6.18.6.3) + 64x domain scale on E_orig
Browse files at this commit40bfd304d9d6f45e32f95c9e892b11bcd167d204Parent: 363af1c- fix
- spec patch construction (4.6.18.6.3) + 64x domain scale on E_orig
- per-QMF-band ours-vs-reference instrument
4 changed files +90 -36
- crates/ec-aac/src/sbr_bands.rs +6 -0
- crates/ec-aac/src/sbr_env.rs +12 -6
- crates/ec-aac/src/sbr_hf.rs +51 -30
- crates/ec-aac/tests/sbr_real_library.rs +21 -0
crates/ec-aac/src/sbr_bands.rsmodified+6 -0
| @@ -180,6 +180,10 @@ pub struct BandTables { | ||
| 180 | 180 | pub f_noise: Vec<i64>, |
| 181 | 181 | pub kx: i64, |
| 182 | 182 | pub k2: i64, |
| 183 | + /// Master table borders (`k0 = f_master[0]`), needed by the patch builder. | |
| 184 | + pub f_master: Vec<i64>, | |
| 185 | + /// SBR (output) sample rate, the patch builder's `goalSb` input. | |
| 186 | + pub rate: u32, | |
| 183 | 187 | } |
| 184 | 188 | |
| 185 | 189 | /// `rate` is the SBR rate: twice the AAC-LC core's. |
| @@ -236,6 +240,8 @@ pub fn freq_tables( | ||
| 236 | 240 | f_noise, |
| 237 | 241 | kx, |
| 238 | 242 | k2, |
| 243 | + f_master: master.clone(), | |
| 244 | + rate, | |
| 239 | 245 | }) |
| 240 | 246 | } |
| 241 | 247 |
crates/ec-aac/src/sbr_env.rsmodified+12 -6
| @@ -21,6 +21,14 @@ use crate::sbr_tables::SBR_NOISE; | ||
| 21 | 21 | use ec_dsp::Complex; |
| 22 | 22 | |
| 23 | 23 | const NOISE_FLOOR_OFFSET: f64 = 6.0; |
| 24 | +/// corner-cut: this chain's analysis bank (`sbr_qmf::Analysis`, undone by | |
| 25 | +/// its own `Synthesis`) carries twice the spec bank's subband amplitude, so | |
| 26 | +/// the transmitted energies need x4 to compare against `|X_high|^2` here. | |
| 27 | +/// Measured on the real-file per-QMF-band instrument (`EC_AAC_SBR_QMF_WITNESS`, | |
| 28 | +/// `QMF-BAND` rows): a uniform -6 dB across every HF band with the low | |
| 29 | +/// band at 0 dB before this factor. Ceiling: none functionally; the upgrade | |
| 30 | +/// path is normalising `sbr_qmf` to the spec's gain and dropping this. | |
| 31 | +const ENV_DOMAIN_SCALE: f64 = 4.0; | |
| 24 | 32 | |
| 25 | 33 | /// Envelope amp-resolution exponent: `2.0` (`bs_amp_res=1`, 3 dB) or `1.0` |
| 26 | 34 | /// (`bs_amp_res=0`, 1.5 dB) -- expressed as the multiplier on `e_q/2` so |
| @@ -30,13 +38,11 @@ fn alpha(amp_res: u8) -> f64 { | ||
| 30 | 38 | if amp_res != 0 { 1.0 } else { 0.5 } |
| 31 | 39 | } |
| 32 | 40 | |
| 33 | -/// Plain (non-coupled) envelope dequantization: `E_Orig = 2^(E_Q * a)`. | |
| 34 | -/// §4.6.18.7.2 carries an extra `* 64` here; it is a pure scale on the | |
| 35 | -/// whole HF region (the gain `sqrt(E_orig/E_curr)` cancels our own QMF | |
| 36 | -/// scale otherwise), and this chain's QMF domain is calibrated without it | |
| 37 | -/// (see `sbr_chain`'s OUTPUT_SCALE handling). | |
| 41 | +/// Plain (non-coupled) envelope dequantization: `E_Orig = 2^(E_Q * a) * 64` | |
| 42 | +/// (§4.6.18.7.2), times `ENV_DOMAIN_SCALE` to land in this chain's QMF | |
| 43 | +/// energy domain. | |
| 38 | 44 | pub fn dequant_env(e_q: i32, amp_res: u8) -> f64 { |
| 39 | - 2f64.powf(alpha(amp_res) * f64::from(e_q)) | |
| 45 | + 2f64.powf(alpha(amp_res) * f64::from(e_q)) * 64.0 * ENV_DOMAIN_SCALE | |
| 40 | 46 | } |
| 41 | 47 | |
| 42 | 48 | /// Plain noise-floor dequantization: `Q_Orig = 2^(NOISE_FLOOR_OFFSET - Q_Q)`, |
crates/ec-aac/src/sbr_hf.rsmodified+51 -30
| @@ -124,45 +124,66 @@ pub struct Patch { | ||
| 124 | 124 | pub width: usize, |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | -/// Builds the patch list filling `[tables.kx, tables.k2)` from bands below | |
| 128 | -/// `kx`, walking the source pointer down and wrapping it back to `kx` when | |
| 129 | -/// exhausted. Deterministic: the same tables always produce the same patches. | |
| 127 | +/// Patch construction, ISO/IEC 14496-3 §4.6.18.6.3 (Figure 4.31): source | |
| 128 | +/// bands are read downward from `k0 = f_master[0]`, patches end on master | |
| 129 | +/// borders, `goalSb = round(2048 kHz / fs)` bounds the first patch, and a | |
| 130 | +/// trailing patch narrower than 3 bands is dropped. | |
| 130 | 131 | pub fn build_patches(tables: &BandTables) -> Vec<Patch> { |
| 131 | - let kx = tables.kx as usize; | |
| 132 | - let k2 = tables.k2 as usize; | |
| 133 | - let f_high: Vec<usize> = tables.f_high.iter().map(|&b| b as usize).collect(); | |
| 132 | + let kx = tables.kx; | |
| 133 | + let k2 = tables.k2; | |
| 134 | + let fm = &tables.f_master; | |
| 134 | 135 | let mut patches = Vec::new(); |
| 135 | - if kx == 0 || k2 <= kx { | |
| 136 | + if kx <= 0 || k2 <= kx || fm.len() < 2 { | |
| 136 | 137 | return patches; |
| 137 | 138 | } |
| 138 | - let mut msb = kx; // source read pointer, decreasing | |
| 139 | - let mut sb = kx; // target write pointer, increasing | |
| 140 | - while sb < k2 { | |
| 141 | - // Widest run of consecutive f_high intervals starting at `sb` whose | |
| 142 | - // total width still fits below the current source pointer. | |
| 143 | - let mut width = 0usize; | |
| 144 | - let mut probe = sb; | |
| 139 | + let k0 = fm[0]; | |
| 140 | + let n_master = fm.len() - 1; | |
| 141 | + let goal_sb = ((1000i64 << 11) + i64::from(tables.rate >> 1)) / i64::from(tables.rate.max(1)); | |
| 142 | + let mut k = if goal_sb < k2 { | |
| 143 | + fm.iter().position(|&b| b >= goal_sb).unwrap_or(n_master) | |
| 144 | + } else { | |
| 145 | + n_master | |
| 146 | + }; | |
| 147 | + let (mut msb, mut usb) = (k0, kx); | |
| 148 | + let (mut last_k, mut last_msb) = (usize::MAX, i64::MIN); | |
| 149 | + let mut sb; | |
| 150 | + loop { | |
| 151 | + if k == last_k && msb == last_msb { | |
| 152 | + break; // construction failed; keep what we have | |
| 153 | + } | |
| 154 | + last_k = k; | |
| 155 | + last_msb = msb; | |
| 156 | + let mut odd = 0i64; | |
| 157 | + let mut i = k; | |
| 145 | 158 | loop { |
| 146 | - let next_border = f_high.iter().find(|&&b| b > probe).copied(); | |
| 147 | - let Some(border) = next_border else { break }; | |
| 148 | - let iv = border - probe; | |
| 149 | - if width + iv > msb || border > k2 { | |
| 159 | + sb = fm[i]; | |
| 160 | + odd = (sb + k0) & 1; | |
| 161 | + if i == 0 || sb <= k0 - 1 + msb - odd { | |
| 150 | 162 | break; |
| 151 | 163 | } |
| 152 | - width += iv; | |
| 153 | - probe = border; | |
| 164 | + i -= 1; | |
| 165 | + } | |
| 166 | + let width = (sb - usb).max(0); | |
| 167 | + if width > 0 { | |
| 168 | + patches.push(Patch { | |
| 169 | + source_start: (k0 - odd - width).max(0) as usize, | |
| 170 | + target_start: usb as usize, | |
| 171 | + width: width as usize, | |
| 172 | + }); | |
| 173 | + usb = sb; | |
| 174 | + msb = sb; | |
| 175 | + } else { | |
| 176 | + msb = kx; | |
| 154 | 177 | } |
| 155 | - if width == 0 { | |
| 156 | - width = (k2 - sb).min(msb.max(1)); | |
| 178 | + if fm[k] - sb < 3 { | |
| 179 | + k = n_master; | |
| 157 | 180 | } |
| 158 | - let source_start = msb - width.min(msb); | |
| 159 | - patches.push(Patch { | |
| 160 | - source_start, | |
| 161 | - target_start: sb, | |
| 162 | - width, | |
| 163 | - }); | |
| 164 | - sb += width; | |
| 165 | - msb = if source_start == 0 { kx } else { source_start }; | |
| 181 | + if sb == k2 || patches.len() > 6 { | |
| 182 | + break; | |
| 183 | + } | |
| 184 | + } | |
| 185 | + if patches.len() > 1 && patches.last().is_some_and(|p| p.width < 3) { | |
| 186 | + patches.pop(); | |
| 166 | 187 | } |
| 167 | 188 | patches |
| 168 | 189 | } |
crates/ec-aac/tests/sbr_real_library.rsmodified+21 -0
| @@ -2755,6 +2755,8 @@ fn sbr_real_library_matches_reference() { | ||
| 2755 | 2755 | let high = if n >= 1024 { |
| 2756 | 2756 | let oh = highpass(&o[oa..oa + n], rate, c.crossover_hz); |
| 2757 | 2757 | let th = highpass(&t[ob..ob + n], rate, c.crossover_hz); |
| 2758 | + let rms = |v: &[f32]| (v.iter().map(|x| f64::from(*x).powi(2)).sum::<f64>() / v.len().max(1) as f64).sqrt(); | |
| 2759 | + println!(" ch{ch} above-crossover RMS ours/ref = {:.4}", rms(&oh) / rms(&th).max(1e-12)); | |
| 2758 | 2760 | correlation(&oh, &th) |
| 2759 | 2761 | } else { |
| 2760 | 2762 | 0.0 |
| @@ -3004,6 +3006,25 @@ fn sbr_real_library_matches_reference() { | ||
| 3004 | 3006 | let win = &o[oa..oa + n]; |
| 3005 | 3007 | let twin = &t[ob..ob + n]; |
| 3006 | 3008 | let (vs_ref, vs_ours, ours_vs_ref) = hf_patch_simulator_qmf(win, twin); |
| 3009 | + // Per-QMF-band energy ratio and coherence, ours vs reference | |
| 3010 | + // (the envelope-adjuster instrument: a level offset shows as | |
| 3011 | + // a constant dB ratio, a gain-shape defect as per-band dB). | |
| 3012 | + { | |
| 3013 | + let (o_spec, t_spec, _) = qmf_domain_spec(win, twin); | |
| 3014 | + println!(" ch{ch} QMF-BAND ours-vs-REFERENCE (band, ratio_dB, coherence):"); | |
| 3015 | + for band in 0..o_spec.len().min(t_spec.len()).min(48) { | |
| 3016 | + let (mut eo, mut et, mut cr, mut ci) = (0.0f64, 0.0f64, 0.0f64, 0.0f64); | |
| 3017 | + for (a, b) in o_spec[band].iter().zip(&t_spec[band]) { | |
| 3018 | + eo += a.norm_sqr(); | |
| 3019 | + et += b.norm_sqr(); | |
| 3020 | + let c = *a * b.conj(); | |
| 3021 | + cr += c.re; | |
| 3022 | + ci += c.im; | |
| 3023 | + } | |
| 3024 | + let coh = (cr * cr + ci * ci).sqrt() / (eo * et).sqrt().max(1e-30); | |
| 3025 | + println!(" band{band:>3}: {:>7.2} dB coh {coh:.4}", 10.0 * (eo / et.max(1e-30)).log10()); | |
| 3026 | + } | |
| 3027 | + } | |
| 3007 | 3028 | let mean_in = |rows: &[(usize, f64)], lo: usize, hi: usize| { |
| 3008 | 3029 | let vals: Vec<f64> = rows |
| 3009 | 3030 | .iter() |