Revert "ec-aac SBR: downsampled SBR (ext rate < 2*core, e.g. al_sbr_sr_48_2_fsaac48 ASC 11 90 56 e5 98) ran the 64-band doubling chain with band tables at the 48 kHz ext rate: ours 2048/AU and band-table errors (no sideinfo rows, ours 1001472 = cap at 489 AUs x 2048 vs ffmpeg 1148 x 1024). ffmpeg (aacsbr_template.c 1147/1683/1418): sbr->sample_rate = 2*core for tables, downsampled = ext_sample_rate < sbr->sample_rate -> 32-band synthesis with the decimated window (ds[n]=c[2n]), 32 output samples/slot. Fix: parser rate always 2*core, SbrChain::set_downsampled, Synthesis::downsampled (N=32, v 640, shift -0.5 matching our analysis phase; python replica of analysis->synth32 corr 0.995 at lag 289 vs 0.9967 for the 64-band pair), k2 clamped to 32, LFE bypass uses the same bank. Also QMF_WINDOW[384] and [512] had the wrong sign vs ffmpeg us/ds tables (ffmpeg flips at 385/513, mirror-symmetric with 128/256; ours at 384/512; table now equals ffmpeg's us and ds to 1e-10), pinned sum updated 81.46300943655164 -> 82.21187106118578. cargo check -p ec-aac --all-targets clean; tests not run"
Browse files at this commitb3f487fceb15f7831f0ca37924b8e98e02287d6aParent: 0c091c35 changed files +61 -92
- crates/ec-aac/src/decode.rs +0 -8
- crates/ec-aac/src/lib.rs +2 -4
- crates/ec-aac/src/sbr_chain.rs +14 -33
- crates/ec-aac/src/sbr_qmf.rs +42 -44
- crates/ec-aac/src/sbr_qmf_window.rs +3 -3
crates/ec-aac/src/decode.rsmodified+0 -8
| @@ -582,14 +582,6 @@ impl BlockDecoder { | ||
| 582 | 582 | self.sbr = rate.map(crate::sbr_chain::SbrChain::new); |
| 583 | 583 | } |
| 584 | 584 | |
| 585 | - /// Marks the SBR output as downsampled: the extension rate is below twice | |
| 586 | - /// the core rate, so the synthesis stays at 32 bands. | |
| 587 | - pub fn set_sbr_downsampled(&mut self, on: bool) { | |
| 588 | - if let Some(sbr) = self.sbr.as_mut() { | |
| 589 | - sbr.set_downsampled(on); | |
| 590 | - } | |
| 591 | - } | |
| 592 | - | |
| 593 | 585 | /// Marks Parametric Stereo as signalled by the config, for the SBR chain |
| 594 | 586 | /// (if one is armed): its elements then decode to two channels. |
| 595 | 587 | pub fn set_sbr_ps(&mut self, on: bool) { |
crates/ec-aac/src/lib.rsmodified+2 -4
| @@ -144,10 +144,8 @@ impl AacDecoder { | ||
| 144 | 144 | // SBR (and Parametric Stereo, for HE-AACv2) is reconstructed: the |
| 145 | 145 | // core is decoded first, then the extension-rate chain runs on it. |
| 146 | 146 | if cfg.sbr_present { |
| 147 | - let sbr_rate = cfg.sample_rate * 2; | |
| 148 | - let ext_rate = cfg.extension_sample_rate.unwrap_or(sbr_rate); | |
| 149 | - d.block.set_sbr_rate(Some(sbr_rate)); | |
| 150 | - d.block.set_sbr_downsampled(ext_rate < sbr_rate); | |
| 147 | + let rate = cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2); | |
| 148 | + d.block.set_sbr_rate(Some(rate)); | |
| 151 | 149 | d.block.set_sbr_ps(cfg.ps_present); |
| 152 | 150 | } else if !cfg.ps_present { |
| 153 | 151 | // Plain AAC-LC config: an SBR payload may still follow (implicit signalling). |
crates/ec-aac/src/sbr_chain.rsmodified+14 -33
| @@ -237,14 +237,10 @@ struct Element { | ||
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | impl Element { |
| 240 | - fn ensure_channels(&mut self, need: usize, downsampled: bool) { | |
| 240 | + fn ensure_channels(&mut self, need: usize) { | |
| 241 | 241 | while self.channels.len() < need { |
| 242 | 242 | let seed = 0x1234_5678u32.wrapping_add(self.channels.len() as u32 * 0x9e37_79b9); |
| 243 | - let mut channel = ChannelState::new(1, seed); | |
| 244 | - if downsampled { | |
| 245 | - channel.synthesis = Synthesis::downsampled(); | |
| 246 | - } | |
| 247 | - self.channels.push(channel); | |
| 243 | + self.channels.push(ChannelState::new(1, seed)); | |
| 248 | 244 | } |
| 249 | 245 | } |
| 250 | 246 | } |
| @@ -258,7 +254,6 @@ const PS_SLOTS: usize = 32; | ||
| 258 | 254 | /// encoder in practice emits it). |
| 259 | 255 | pub struct SbrChain { |
| 260 | 256 | rate: u32, |
| 261 | - downsampled: bool, | |
| 262 | 257 | /// PS is signalled by the config (explicit HE-AACv2). |
| 263 | 258 | ps_config: bool, |
| 264 | 259 | // Keyed by (element_instance_tag, is_cpe): SCE/LFE and CPE instance |
| @@ -277,7 +272,6 @@ impl SbrChain { | ||
| 277 | 272 | pub fn new(rate: u32) -> SbrChain { |
| 278 | 273 | SbrChain { |
| 279 | 274 | rate, |
| 280 | - downsampled: false, | |
| 281 | 275 | ps_config: false, |
| 282 | 276 | elements: HashMap::new(), |
| 283 | 277 | bypass: HashMap::new(), |
| @@ -285,10 +279,6 @@ impl SbrChain { | ||
| 285 | 279 | } |
| 286 | 280 | |
| 287 | 281 | /// Marks PS as signalled by the config: the element's output is stereo. |
| 288 | - pub fn set_downsampled(&mut self, on: bool) { | |
| 289 | - self.downsampled = on; | |
| 290 | - } | |
| 291 | - | |
| 292 | 282 | pub fn set_ps_config(&mut self, on: bool) { |
| 293 | 283 | self.ps_config = on; |
| 294 | 284 | for elem in self.elements.values_mut() { |
| @@ -299,13 +289,13 @@ impl SbrChain { | ||
| 299 | 289 | /// Parses one FIL element's `sbr_extension_data` (the reader |
| 300 | 290 | /// positioned right past `bs_extension_type`) for element `tag`. |
| 301 | 291 | pub fn parse(&mut self, r: &mut BitReader<'_>, tag: u8, is_cpe: bool) -> Option<SbrData> { |
| 302 | - let (ps_config, downsampled) = (self.ps_config, self.downsampled); | |
| 292 | + let ps_config = self.ps_config; | |
| 303 | 293 | let elem = self.element(tag, is_cpe); |
| 304 | 294 | let data = elem.parser.parse(r, is_cpe).ok()?; |
| 305 | 295 | let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1); |
| 306 | 296 | let ps_out = data.ps.is_some() || (ps_config && !is_cpe); |
| 307 | 297 | let need = data.channels.len().max(if ps_out { 2 } else { 1 }); |
| 308 | - elem.ensure_channels(need, downsampled); | |
| 298 | + elem.ensure_channels(need); | |
| 309 | 299 | for ch in &mut elem.channels { |
| 310 | 300 | if ch.chirp.bands() != n_q { |
| 311 | 301 | ch.chirp = ChirpState::new(n_q); |
| @@ -331,9 +321,8 @@ impl SbrChain { | ||
| 331 | 321 | |
| 332 | 322 | fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec<f32>]) { |
| 333 | 323 | let dup = !is_cpe && planes.len() == 2; |
| 334 | - let downsampled = self.downsampled; | |
| 335 | 324 | let elem = self.element(tag, is_cpe); |
| 336 | - elem.ensure_channels(planes.len(), downsampled); | |
| 325 | + elem.ensure_channels(planes.len()); | |
| 337 | 326 | let mut right_synth = if dup { |
| 338 | 327 | Some(std::mem::take(&mut elem.channels[1].synthesis)) |
| 339 | 328 | } else { |
| @@ -386,19 +375,13 @@ impl SbrChain { | ||
| 386 | 375 | /// planes, with the high bands left empty, which is what ffmpeg's output |
| 387 | 376 | /// does for the LFE channel. |
| 388 | 377 | pub fn bypass_plane(&mut self, tag: u8, plane: &mut Vec<f32>) { |
| 389 | - let downsampled = self.downsampled; | |
| 390 | - let (state, delay) = self.bypass.entry(tag).or_insert_with(|| { | |
| 391 | - let mut state = ChannelState::new(1, 0); | |
| 392 | - let mut delay = BYPASS_DELAY; | |
| 393 | - if downsampled { | |
| 394 | - state.synthesis = Synthesis::downsampled(); | |
| 395 | - delay /= 2; | |
| 396 | - } | |
| 397 | - (state, vec![0.0; delay]) | |
| 398 | - }); | |
| 378 | + let (state, delay) = self | |
| 379 | + .bypass | |
| 380 | + .entry(tag) | |
| 381 | + .or_insert_with(|| (ChannelState::new(1, 0), vec![0.0; BYPASS_DELAY])); | |
| 399 | 382 | let num_slots = plane.len() / ANALYSIS_BANDS; |
| 400 | 383 | let mut out = std::mem::take(delay); |
| 401 | - out.reserve(num_slots * state.synthesis.bands()); | |
| 384 | + out.reserve(num_slots * SYNTHESIS_BANDS); | |
| 402 | 385 | for slot in 0..num_slots { |
| 403 | 386 | let mut chunk = [0f32; ANALYSIS_BANDS]; |
| 404 | 387 | chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]); |
| @@ -414,7 +397,7 @@ impl SbrChain { | ||
| 414 | 397 | } |
| 415 | 398 | // The reconstructed planes lag their raw analysis by `HF_ADJ` slots; |
| 416 | 399 | // hold the bypass output back by the same amount so they line up. |
| 417 | - let produced = num_slots * state.synthesis.bands(); | |
| 400 | + let produced = num_slots * SYNTHESIS_BANDS; | |
| 418 | 401 | *plane = out[..produced].to_vec(); |
| 419 | 402 | *delay = out[produced..].to_vec(); |
| 420 | 403 | } |
| @@ -505,7 +488,6 @@ impl SbrChain { | ||
| 505 | 488 | if source == "hold" { |
| 506 | 489 | HOLD_CALLS.fetch_add(1, Ordering::Relaxed); |
| 507 | 490 | } |
| 508 | - let band_limit = if self.downsampled { ANALYSIS_BANDS } else { SYNTHESIS_BANDS }; | |
| 509 | 491 | let Some(elem) = self.elements.get_mut(&(tag, is_cpe)) else { |
| 510 | 492 | return Ok(()); |
| 511 | 493 | }; |
| @@ -723,7 +705,7 @@ impl SbrChain { | ||
| 723 | 705 | } |
| 724 | 706 | |
| 725 | 707 | let kx = (tables.kx as usize).min(ANALYSIS_BANDS); |
| 726 | - let k2 = (tables.k2 as usize).min(band_limit); | |
| 708 | + let k2 = (tables.k2 as usize).min(SYNTHESIS_BANDS); | |
| 727 | 709 | let mut vs: Vec<[Complex<f64>; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots); |
| 728 | 710 | for slot in 0..num_slots { |
| 729 | 711 | let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; |
| @@ -861,8 +843,7 @@ fn render_ps( | ||
| 861 | 843 | |
| 862 | 844 | /// Runs `slots` through a synthesis bank, at the SBR output scale. |
| 863 | 845 | fn synth_slots(synth: &mut Synthesis, slots: &[[Complex<f64>; SYNTHESIS_BANDS]]) -> Vec<f32> { |
| 864 | - let bands = synth.bands(); | |
| 865 | - let mut out = Vec::with_capacity(slots.len() * bands); | |
| 846 | + let mut out = Vec::with_capacity(slots.len() * SYNTHESIS_BANDS); | |
| 866 | 847 | for v in slots { |
| 867 | 848 | let mut pcm = synth.process_slot(v); |
| 868 | 849 | for s in &mut pcm { |
| @@ -871,7 +852,7 @@ fn synth_slots(synth: &mut Synthesis, slots: &[[Complex<f64>; SYNTHESIS_BANDS]]) | ||
| 871 | 852 | // core's amplitude; the 2x is the upsampler's gain. |
| 872 | 853 | *s *= 2.0 * crate::decode::OUTPUT_SCALE; |
| 873 | 854 | } |
| 874 | - out.extend_from_slice(&pcm[..bands]); | |
| 855 | + out.extend_from_slice(&pcm); | |
| 875 | 856 | } |
| 876 | 857 | out |
| 877 | 858 | } |
crates/ec-aac/src/sbr_qmf.rsmodified+42 -44
| @@ -218,72 +218,70 @@ impl Default for HfAnalysis { | ||
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | -/// The ISO/IEC 14496-3 4.6.18.8.2 complex synthesis QMF bank: complex subband | |
| 222 | -/// slots in, PCM out. The 64-band form doubles the rate; the 32-band | |
| 223 | -/// downsampled form (decimated window, band shift of half a sample like the | |
| 224 | -/// analysis bank) keeps the core rate. | |
| 221 | +/// The ISO/IEC 14496-3 4.6.18.8.2 64-band complex synthesis QMF bank: | |
| 222 | +/// complex subband slots in, doubled-rate PCM out, [`SYNTHESIS_BANDS`] | |
| 223 | +/// output samples per slot. Verified against the literal spec equations at | |
| 224 | +/// `corr=0.999999` self-consistency (see the module doc). | |
| 225 | 225 | pub struct Synthesis { |
| 226 | - bands: usize, | |
| 227 | - v: Vec<f64>, | |
| 226 | + /// History buffer `v`, `10 * PROTO_LEN / SYNTHESIS_BANDS * SYNTHESIS_BANDS` | |
| 227 | + /// = `1280` samples (`4.6.18.8.2`'s own length, not `PROTO_LEN`). | |
| 228 | + v: [f64; 1280], | |
| 228 | 229 | cos_tab: Vec<f64>, |
| 229 | 230 | sin_tab: Vec<f64>, |
| 230 | 231 | } |
| 231 | 232 | |
| 232 | 233 | impl Synthesis { |
| 234 | + /// A fresh bank with a silent history buffer. | |
| 233 | 235 | pub fn new() -> Synthesis { |
| 234 | - Synthesis::with_bands(SYNTHESIS_BANDS, 0.0) | |
| 235 | - } | |
| 236 | - | |
| 237 | - pub fn downsampled() -> Synthesis { | |
| 238 | - Synthesis::with_bands(SYNTHESIS_BANDS / 2, -0.5) | |
| 239 | - } | |
| 240 | - | |
| 241 | - fn with_bands(bands: usize, shift: f64) -> Synthesis { | |
| 242 | - let len = 2 * bands; | |
| 243 | - let mut cos_tab = vec![0.0f64; bands * len]; | |
| 244 | - let mut sin_tab = vec![0.0f64; bands * len]; | |
| 245 | - for k in 0..bands { | |
| 246 | - for n in 0..len { | |
| 247 | - let phi = std::f64::consts::PI / len as f64 | |
| 236 | + let mut cos_tab = vec![0.0f64; SYNTHESIS_BANDS * 128]; | |
| 237 | + let mut sin_tab = vec![0.0f64; SYNTHESIS_BANDS * 128]; | |
| 238 | + for k in 0..SYNTHESIS_BANDS { | |
| 239 | + for n in 0..128 { | |
| 240 | + // phi = pi/128*(k+0.5)*(2n-255) | |
| 241 | + let phi = std::f64::consts::PI / 128.0 | |
| 248 | 242 | * (k as f64 + 0.5) |
| 249 | - * (2.0 * n as f64 - (2 * len - 1) as f64 + shift); | |
| 250 | - cos_tab[k * len + n] = phi.cos(); | |
| 251 | - sin_tab[k * len + n] = phi.sin(); | |
| 243 | + * (2.0 * n as f64 - 255.0); | |
| 244 | + cos_tab[k * 128 + n] = phi.cos(); | |
| 245 | + sin_tab[k * 128 + n] = phi.sin(); | |
| 252 | 246 | } |
| 253 | 247 | } |
| 254 | 248 | Synthesis { |
| 255 | - bands, | |
| 256 | - v: vec![0.0; 10 * len], | |
| 249 | + v: [0.0; 1280], | |
| 257 | 250 | cos_tab, |
| 258 | 251 | sin_tab, |
| 259 | 252 | } |
| 260 | 253 | } |
| 261 | 254 | |
| 262 | - pub fn bands(&self) -> usize { | |
| 263 | - self.bands | |
| 264 | - } | |
| 265 | - | |
| 255 | + /// Feeds one slot of [`SYNTHESIS_BANDS`] complex subband samples and | |
| 256 | + /// returns [`SYNTHESIS_BANDS`] output PCM samples. | |
| 266 | 257 | pub fn process_slot(&mut self, x: &[Complex<f64>; SYNTHESIS_BANDS]) -> [f32; SYNTHESIS_BANDS] { |
| 267 | - let bands = self.bands; | |
| 268 | - let len = 2 * bands; | |
| 269 | - self.v.copy_within(0..9 * len, len); | |
| 270 | - for n in 0..len { | |
| 258 | + // v[n] = v[n-128] for n=1279..128 (shift), then v[0..128] filled with | |
| 259 | + // v[n] = Re{ (1/64) sum_k X[k]*exp(j*phi) }, n=0..127. | |
| 260 | + self.v.copy_within(0..1152, 128); | |
| 261 | + for n in 0..128 { | |
| 271 | 262 | let mut re = 0.0f64; |
| 272 | - for (k, xk) in x.iter().take(bands).enumerate() { | |
| 273 | - let at = k * len + n; | |
| 274 | - re += xk.re * self.cos_tab[at] - xk.im * self.sin_tab[at]; | |
| 263 | + for (k, xk) in x.iter().enumerate() { | |
| 264 | + let row = k * 128; | |
| 265 | + // Re{X[k]*exp(j*phi)} = Xre*cos(phi) - Xim*sin(phi) | |
| 266 | + re += xk.re * self.cos_tab[row + n] - xk.im * self.sin_tab[row + n]; | |
| 275 | 267 | } |
| 276 | 268 | self.v[n] = re / SYNTHESIS_BANDS as f64; |
| 277 | 269 | } |
| 278 | - let step = SYNTHESIS_BANDS / bands; | |
| 270 | + // g[128i+n] = v[256i+n], g[128i+64+n] = v[256i+192+n], i=0..4, n=0..63 | |
| 271 | + let mut g = [0.0f64; PROTO_LEN]; | |
| 272 | + for i in 0..5 { | |
| 273 | + for n in 0..64 { | |
| 274 | + g[128 * i + n] = self.v[256 * i + n]; | |
| 275 | + g[128 * i + 64 + n] = self.v[256 * i + 192 + n]; | |
| 276 | + } | |
| 277 | + } | |
| 278 | + // w[n] = g[n]*c[n], n=0..639; y[n] = sum_{k=0}^{9} w[64k+n], n=0..63 | |
| 279 | 279 | let mut out = [0.0f32; SYNTHESIS_BANDS]; |
| 280 | - for (n, o) in out.iter_mut().take(bands).enumerate() { | |
| 280 | + for (n, o) in out.iter_mut().enumerate() { | |
| 281 | 281 | let mut acc = 0.0f64; |
| 282 | - for i in 0..5 { | |
| 283 | - let near = self.v[2 * len * i + n]; | |
| 284 | - let far = self.v[2 * len * i + 3 * bands + n]; | |
| 285 | - acc += near * QMF_WINDOW[step * (len * i + n)]; | |
| 286 | - acc += far * QMF_WINDOW[step * (len * i + bands + n)]; | |
| 282 | + for k in 0..10 { | |
| 283 | + let idx = 64 * k + n; | |
| 284 | + acc += g[idx] * QMF_WINDOW[idx]; | |
| 287 | 285 | } |
| 288 | 286 | *o = acc as f32; |
| 289 | 287 | } |
| @@ -472,7 +470,7 @@ mod tests { | ||
| 472 | 470 | assert_eq!(QMF_WINDOW.len(), PROTO_LEN); |
| 473 | 471 | let sum: f64 = QMF_WINDOW.iter().sum(); |
| 474 | 472 | let sum_sq: f64 = QMF_WINDOW.iter().map(|v| v * v).sum(); |
| 475 | - assert!((sum - 82.21187106118578).abs() < 1e-9, "sum={sum}"); | |
| 473 | + assert!((sum - 81.46300943655164).abs() < 1e-9, "sum={sum}"); | |
| 476 | 474 | assert!((sum_sq - 64.00000000000003).abs() < 1e-9, "sum_sq={sum_sq}"); |
| 477 | 475 | } |
| 478 | 476 |
crates/ec-aac/src/sbr_qmf_window.rsmodified+3 -3
| @@ -114,7 +114,7 @@ pub const QMF_WINDOW: [f64; 640] = [ | ||
| 114 | 114 | 0.49677082545707, 0.48552530911099, 0.47424532146115, 0.46293080852757, |
| 115 | 115 | 0.45159965356824, 0.44025537543665, 0.42891199207373, 0.41756968968409, |
| 116 | 116 | 0.40623176767625, 0.39492117615675, 0.38363500139043, 0.37237955463061, |
| 117 | - 0.36115899031355, -0.3499914122931, -0.33887226938665, -0.32781137272105, | |
| 117 | + -0.36115899031355, -0.3499914122931, -0.33887226938665, -0.32781137272105, | |
| 118 | 118 | -0.31682789136456, -0.30590985751916, -0.29507167170646, -0.28432141891085, |
| 119 | 119 | -0.27366340405625, -0.26310532994603, -0.25264803095722, -0.24230168845974, |
| 120 | 120 | -0.23206908706791, -0.22196526964149, -0.21197358538056, -0.20212501768103, |
| @@ -146,7 +146,7 @@ pub const QMF_WINDOW: [f64; 640] = [ | ||
| 146 | 146 | 0.02860721736385, 0.02718594296329, 0.02578758475467, 0.02441609920285, |
| 147 | 147 | 0.02306801692862, 0.02174675502535, 0.02045317933555, 0.01918724313698, |
| 148 | 148 | 0.01794333813443, 0.01673247129989, 0.01554055533423, 0.01439046660792, |
| 149 | - 0.01327182200351, -0.01218499959508, -0.01113155480321, -0.01011502154986, | |
| 149 | + -0.01327182200351, -0.01218499959508, -0.01113155480321, -0.01011502154986, | |
| 150 | 150 | -0.00913253296085, -0.00817982333726, -0.00726158168517, -0.00637922932685, |
| 151 | 151 | -0.00553372111088, -0.004722259624, -0.00394011240522, -0.003193377839, |
| 152 | 152 | -0.00248267236449, -0.00180394725893, -0.00115681355227, -0.00054642808664, |
| @@ -196,7 +196,7 @@ mod tests { | ||
| 196 | 196 | argmax = i; |
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | - assert!((sum - 82.21187106118578).abs() < 1e-9, "sum={sum}"); | |
| 199 | + assert!((sum - 81.46300943655164).abs() < 1e-9, "sum={sum}"); | |
| 200 | 200 | assert!((sum_sq - 64.00000000000003).abs() < 1e-9, "sum_sq={sum_sq}"); |
| 201 | 201 | assert!((max - 0.85373856005937).abs() < 1e-12, "max={max}"); |
| 202 | 202 | assert_eq!(argmax, 320); |