ec-aac SBR: downsampled SBR (ext rate < 2*core, al_sbr_sr_48_2_fsaac48) ran the 64-band chain with tables at the 48 kHz ext rate (2048/AU). ffmpeg aacsbr_template.c: tables at 2*core, 32-band synthesis with decimated window c[2n] when ext < 2*core. Fix: parser rate always 2*core, SbrChain::set_downsampled, Synthesis::downsampled (32 bands, 64-band path numerically unchanged), k2 clamped to 32, LFE bypass same bank. QMF_WINDOW untouched (d8db6262 sign flips dropped). The 'missing' 170 AUs are the test MAX_SAMPLES=1_000_000 cap (977*1024=1000448), not decoder loss. cargo check -p ec-aac --all-targets clean; tests not run
Browse files at this commit780c4a9016142df90e3f21daa86bd8a4f093e596Parent: b3f487f4 changed files +82 -57
- crates/ec-aac/src/decode.rs +6 -0
- crates/ec-aac/src/lib.rs +4 -2
- crates/ec-aac/src/sbr_chain.rs +33 -14
- crates/ec-aac/src/sbr_qmf.rs +39 -41
crates/ec-aac/src/decode.rsmodified+6 -0
| @@ -582,6 +582,12 @@ impl BlockDecoder { | ||
| 582 | 582 | self.sbr = rate.map(crate::sbr_chain::SbrChain::new); |
| 583 | 583 | } |
| 584 | 584 | |
| 585 | + pub fn set_sbr_downsampled(&mut self, on: bool) { | |
| 586 | + if let Some(sbr) = self.sbr.as_mut() { | |
| 587 | + sbr.set_downsampled(on); | |
| 588 | + } | |
| 589 | + } | |
| 590 | + | |
| 585 | 591 | /// Marks Parametric Stereo as signalled by the config, for the SBR chain |
| 586 | 592 | /// (if one is armed): its elements then decode to two channels. |
| 587 | 593 | pub fn set_sbr_ps(&mut self, on: bool) { |
crates/ec-aac/src/lib.rsmodified+4 -2
| @@ -144,8 +144,10 @@ 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 rate = cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2); | |
| 148 | - d.block.set_sbr_rate(Some(rate)); | |
| 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); | |
| 149 | 151 | d.block.set_sbr_ps(cfg.ps_present); |
| 150 | 152 | } else if !cfg.ps_present { |
| 151 | 153 | // Plain AAC-LC config: an SBR payload may still follow (implicit signalling). |
crates/ec-aac/src/sbr_chain.rsmodified+33 -14
| @@ -237,10 +237,14 @@ struct Element { | ||
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | impl Element { |
| 240 | - fn ensure_channels(&mut self, need: usize) { | |
| 240 | + fn ensure_channels(&mut self, need: usize, downsampled: bool) { | |
| 241 | 241 | while self.channels.len() < need { |
| 242 | 242 | let seed = 0x1234_5678u32.wrapping_add(self.channels.len() as u32 * 0x9e37_79b9); |
| 243 | - self.channels.push(ChannelState::new(1, seed)); | |
| 243 | + let mut channel = ChannelState::new(1, seed); | |
| 244 | + if downsampled { | |
| 245 | + channel.synthesis = Synthesis::downsampled(); | |
| 246 | + } | |
| 247 | + self.channels.push(channel); | |
| 244 | 248 | } |
| 245 | 249 | } |
| 246 | 250 | } |
| @@ -254,6 +258,7 @@ const PS_SLOTS: usize = 32; | ||
| 254 | 258 | /// encoder in practice emits it). |
| 255 | 259 | pub struct SbrChain { |
| 256 | 260 | rate: u32, |
| 261 | + downsampled: bool, | |
| 257 | 262 | /// PS is signalled by the config (explicit HE-AACv2). |
| 258 | 263 | ps_config: bool, |
| 259 | 264 | // Keyed by (element_instance_tag, is_cpe): SCE/LFE and CPE instance |
| @@ -272,12 +277,17 @@ impl SbrChain { | ||
| 272 | 277 | pub fn new(rate: u32) -> SbrChain { |
| 273 | 278 | SbrChain { |
| 274 | 279 | rate, |
| 280 | + downsampled: false, | |
| 275 | 281 | ps_config: false, |
| 276 | 282 | elements: HashMap::new(), |
| 277 | 283 | bypass: HashMap::new(), |
| 278 | 284 | } |
| 279 | 285 | } |
| 280 | 286 | |
| 287 | + pub fn set_downsampled(&mut self, on: bool) { | |
| 288 | + self.downsampled = on; | |
| 289 | + } | |
| 290 | + | |
| 281 | 291 | /// Marks PS as signalled by the config: the element's output is stereo. |
| 282 | 292 | pub fn set_ps_config(&mut self, on: bool) { |
| 283 | 293 | self.ps_config = on; |
| @@ -289,13 +299,13 @@ impl SbrChain { | ||
| 289 | 299 | /// Parses one FIL element's `sbr_extension_data` (the reader |
| 290 | 300 | /// positioned right past `bs_extension_type`) for element `tag`. |
| 291 | 301 | pub fn parse(&mut self, r: &mut BitReader<'_>, tag: u8, is_cpe: bool) -> Option<SbrData> { |
| 292 | - let ps_config = self.ps_config; | |
| 302 | + let (ps_config, downsampled) = (self.ps_config, self.downsampled); | |
| 293 | 303 | let elem = self.element(tag, is_cpe); |
| 294 | 304 | let data = elem.parser.parse(r, is_cpe).ok()?; |
| 295 | 305 | let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1); |
| 296 | 306 | let ps_out = data.ps.is_some() || (ps_config && !is_cpe); |
| 297 | 307 | let need = data.channels.len().max(if ps_out { 2 } else { 1 }); |
| 298 | - elem.ensure_channels(need); | |
| 308 | + elem.ensure_channels(need, downsampled); | |
| 299 | 309 | for ch in &mut elem.channels { |
| 300 | 310 | if ch.chirp.bands() != n_q { |
| 301 | 311 | ch.chirp = ChirpState::new(n_q); |
| @@ -321,8 +331,9 @@ impl SbrChain { | ||
| 321 | 331 | |
| 322 | 332 | fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec<f32>]) { |
| 323 | 333 | let dup = !is_cpe && planes.len() == 2; |
| 334 | + let downsampled = self.downsampled; | |
| 324 | 335 | let elem = self.element(tag, is_cpe); |
| 325 | - elem.ensure_channels(planes.len()); | |
| 336 | + elem.ensure_channels(planes.len(), downsampled); | |
| 326 | 337 | let mut right_synth = if dup { |
| 327 | 338 | Some(std::mem::take(&mut elem.channels[1].synthesis)) |
| 328 | 339 | } else { |
| @@ -375,13 +386,19 @@ impl SbrChain { | ||
| 375 | 386 | /// planes, with the high bands left empty, which is what ffmpeg's output |
| 376 | 387 | /// does for the LFE channel. |
| 377 | 388 | pub fn bypass_plane(&mut self, tag: u8, plane: &mut Vec<f32>) { |
| 378 | - let (state, delay) = self | |
| 379 | - .bypass | |
| 380 | - .entry(tag) | |
| 381 | - .or_insert_with(|| (ChannelState::new(1, 0), vec![0.0; BYPASS_DELAY])); | |
| 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 | + }); | |
| 382 | 399 | let num_slots = plane.len() / ANALYSIS_BANDS; |
| 383 | 400 | let mut out = std::mem::take(delay); |
| 384 | - out.reserve(num_slots * SYNTHESIS_BANDS); | |
| 401 | + out.reserve(num_slots * state.synthesis.bands()); | |
| 385 | 402 | for slot in 0..num_slots { |
| 386 | 403 | let mut chunk = [0f32; ANALYSIS_BANDS]; |
| 387 | 404 | chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]); |
| @@ -397,7 +414,7 @@ impl SbrChain { | ||
| 397 | 414 | } |
| 398 | 415 | // The reconstructed planes lag their raw analysis by `HF_ADJ` slots; |
| 399 | 416 | // hold the bypass output back by the same amount so they line up. |
| 400 | - let produced = num_slots * SYNTHESIS_BANDS; | |
| 417 | + let produced = num_slots * state.synthesis.bands(); | |
| 401 | 418 | *plane = out[..produced].to_vec(); |
| 402 | 419 | *delay = out[produced..].to_vec(); |
| 403 | 420 | } |
| @@ -488,6 +505,7 @@ impl SbrChain { | ||
| 488 | 505 | if source == "hold" { |
| 489 | 506 | HOLD_CALLS.fetch_add(1, Ordering::Relaxed); |
| 490 | 507 | } |
| 508 | + let band_limit = if self.downsampled { ANALYSIS_BANDS } else { SYNTHESIS_BANDS }; | |
| 491 | 509 | let Some(elem) = self.elements.get_mut(&(tag, is_cpe)) else { |
| 492 | 510 | return Ok(()); |
| 493 | 511 | }; |
| @@ -705,7 +723,7 @@ impl SbrChain { | ||
| 705 | 723 | } |
| 706 | 724 | |
| 707 | 725 | let kx = (tables.kx as usize).min(ANALYSIS_BANDS); |
| 708 | - let k2 = (tables.k2 as usize).min(SYNTHESIS_BANDS); | |
| 726 | + let k2 = (tables.k2 as usize).min(band_limit); | |
| 709 | 727 | let mut vs: Vec<[Complex<f64>; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots); |
| 710 | 728 | for slot in 0..num_slots { |
| 711 | 729 | let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; |
| @@ -843,7 +861,8 @@ fn render_ps( | ||
| 843 | 861 | |
| 844 | 862 | /// Runs `slots` through a synthesis bank, at the SBR output scale. |
| 845 | 863 | fn synth_slots(synth: &mut Synthesis, slots: &[[Complex<f64>; SYNTHESIS_BANDS]]) -> Vec<f32> { |
| 846 | - let mut out = Vec::with_capacity(slots.len() * SYNTHESIS_BANDS); | |
| 864 | + let bands = synth.bands(); | |
| 865 | + let mut out = Vec::with_capacity(slots.len() * bands); | |
| 847 | 866 | for v in slots { |
| 848 | 867 | let mut pcm = synth.process_slot(v); |
| 849 | 868 | for s in &mut pcm { |
| @@ -852,7 +871,7 @@ fn synth_slots(synth: &mut Synthesis, slots: &[[Complex<f64>; SYNTHESIS_BANDS]]) | ||
| 852 | 871 | // core's amplitude; the 2x is the upsampler's gain. |
| 853 | 872 | *s *= 2.0 * crate::decode::OUTPUT_SCALE; |
| 854 | 873 | } |
| 855 | - out.extend_from_slice(&pcm); | |
| 874 | + out.extend_from_slice(&pcm[..bands]); | |
| 856 | 875 | } |
| 857 | 876 | out |
| 858 | 877 | } |
crates/ec-aac/src/sbr_qmf.rsmodified+39 -41
| @@ -218,70 +218,68 @@ impl Default for HfAnalysis { | ||
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 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 | 221 | pub struct Synthesis { |
| 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], | |
| 222 | + bands: usize, | |
| 223 | + v: Vec<f64>, | |
| 229 | 224 | cos_tab: Vec<f64>, |
| 230 | 225 | sin_tab: Vec<f64>, |
| 231 | 226 | } |
| 232 | 227 | |
| 233 | 228 | impl Synthesis { |
| 234 | - /// A fresh bank with a silent history buffer. | |
| 235 | 229 | pub fn new() -> Synthesis { |
| 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 | |
| 230 | + Synthesis::with_bands(SYNTHESIS_BANDS, 0.0) | |
| 231 | + } | |
| 232 | + | |
| 233 | + pub fn downsampled() -> Synthesis { | |
| 234 | + Synthesis::with_bands(SYNTHESIS_BANDS / 2, -0.5) | |
| 235 | + } | |
| 236 | + | |
| 237 | + fn with_bands(bands: usize, shift: f64) -> Synthesis { | |
| 238 | + let len = 2 * bands; | |
| 239 | + let mut cos_tab = vec![0.0f64; bands * len]; | |
| 240 | + let mut sin_tab = vec![0.0f64; bands * len]; | |
| 241 | + for k in 0..bands { | |
| 242 | + for n in 0..len { | |
| 243 | + let phi = std::f64::consts::PI / len as f64 | |
| 242 | 244 | * (k as f64 + 0.5) |
| 243 | - * (2.0 * n as f64 - 255.0); | |
| 244 | - cos_tab[k * 128 + n] = phi.cos(); | |
| 245 | - sin_tab[k * 128 + n] = phi.sin(); | |
| 245 | + * (2.0 * n as f64 - (2 * len - 1) as f64 + shift); | |
| 246 | + cos_tab[k * len + n] = phi.cos(); | |
| 247 | + sin_tab[k * len + n] = phi.sin(); | |
| 246 | 248 | } |
| 247 | 249 | } |
| 248 | 250 | Synthesis { |
| 249 | - v: [0.0; 1280], | |
| 251 | + bands, | |
| 252 | + v: vec![0.0; 10 * len], | |
| 250 | 253 | cos_tab, |
| 251 | 254 | sin_tab, |
| 252 | 255 | } |
| 253 | 256 | } |
| 254 | 257 | |
| 255 | - /// Feeds one slot of [`SYNTHESIS_BANDS`] complex subband samples and | |
| 256 | - /// returns [`SYNTHESIS_BANDS`] output PCM samples. | |
| 258 | + pub fn bands(&self) -> usize { | |
| 259 | + self.bands | |
| 260 | + } | |
| 261 | + | |
| 257 | 262 | pub fn process_slot(&mut self, x: &[Complex<f64>; SYNTHESIS_BANDS]) -> [f32; SYNTHESIS_BANDS] { |
| 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 { | |
| 263 | + let bands = self.bands; | |
| 264 | + let len = 2 * bands; | |
| 265 | + self.v.copy_within(0..9 * len, len); | |
| 266 | + for n in 0..len { | |
| 262 | 267 | let mut re = 0.0f64; |
| 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]; | |
| 268 | + for (k, xk) in x.iter().take(bands).enumerate() { | |
| 269 | + let at = k * len + n; | |
| 270 | + re += xk.re * self.cos_tab[at] - xk.im * self.sin_tab[at]; | |
| 267 | 271 | } |
| 268 | 272 | self.v[n] = re / SYNTHESIS_BANDS as f64; |
| 269 | 273 | } |
| 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 | |
| 274 | + let step = SYNTHESIS_BANDS / bands; | |
| 279 | 275 | let mut out = [0.0f32; SYNTHESIS_BANDS]; |
| 280 | - for (n, o) in out.iter_mut().enumerate() { | |
| 276 | + for (n, o) in out.iter_mut().take(bands).enumerate() { | |
| 281 | 277 | let mut acc = 0.0f64; |
| 282 | - for k in 0..10 { | |
| 283 | - let idx = 64 * k + n; | |
| 284 | - acc += g[idx] * QMF_WINDOW[idx]; | |
| 278 | + for i in 0..5 { | |
| 279 | + let near = self.v[2 * len * i + n]; | |
| 280 | + let far = self.v[2 * len * i + 3 * bands + n]; | |
| 281 | + acc += near * QMF_WINDOW[step * (len * i + n)]; | |
| 282 | + acc += far * QMF_WINDOW[step * (len * i + bands + n)]; | |
| 285 | 283 | } |
| 286 | 284 | *o = acc as f32; |
| 287 | 285 | } |