diff --git a/crates/ec-aac/src/sbr_env.rs b/crates/ec-aac/src/sbr_env.rs index d46bfaf3..8dfeb862 100644 --- a/crates/ec-aac/src/sbr_env.rs +++ b/crates/ec-aac/src/sbr_env.rs @@ -72,10 +72,7 @@ pub fn dequant_pair(e0_raw: i32, e1_raw: i32, amp_res: u8) -> (f64, f64) { /// Dequantizes one channel's envelopes and noise floors for one frame, /// de-mixing through [`dequant_pair`] when `coupling` makes `channels[1]` -/// a balance channel against `channels[0]`. Noise floors dequantize -/// independently per channel regardless of coupling (corner-cut: the -/// balance codebook's noise ratio convention is unverified against a real -/// coupled file, same ceiling as the envelope one above). +/// a balance channel against `channels[0]`. pub fn dequantize_frame( header: &SbrHeader, channels: &[SbrChannel], @@ -111,6 +108,12 @@ pub fn dequantize_frame( env.push(row.iter().map(|&v| dequant_env(v, amp_res)).collect()); } } + // corner-cut: noise floors dequantize independently per channel + // regardless of coupling. A mirrored dequant_noise_pair() demix (see + // git history) was tried and measured ZERO effect on the real-file + // ch1 RMS blow-up (identical output to printed precision), so the + // energy dequantization below is the live suspect, not this; reverted + // rather than keeping an unverified formula with no measured benefit. let noise = c .q_q .iter() diff --git a/crates/ec-aac/src/sbr_payload.rs b/crates/ec-aac/src/sbr_payload.rs index aed8ffcb..9b35161b 100644 --- a/crates/ec-aac/src/sbr_payload.rs +++ b/crates/ec-aac/src/sbr_payload.rs @@ -336,19 +336,31 @@ impl SbrParser { prev: &[i32], ) -> Result> { let book = env_book(dt, balance, amp_res); + let width = raw_env_width(balance, amp_res); + let cap = (1i32 << width) - 1; let mut row = vec![0i32; bands]; + // Every cell here is a `bs_data_env` raw scalefactor, whose valid + // range is exactly the transmitted field's own bit width regardless + // of path (DT against last frame's state, or DF's within-row delta + // chain) -- clamped at construction, not just when carried to the + // NEXT frame's `last_env` (as it already was below): an unclamped + // intermediate here let one bad Huffman symbol (desync or a + // genuinely huge encoded delta) cascade through the rest of the row + // via `row[b-1] + delta`, producing a raw value in the hundreds + // whose dequantized (`2^v`) energy is 10+ orders of magnitude too + // large -- exactly the coupled CPE's second-channel envelope + // blowups measured on real HE-AAC files. if dt { for (b, slot) in row.iter_mut().enumerate() { let delta = read_huffman(r, book)?; let base = prev.get(b).copied().unwrap_or(0); - *slot = base + delta; + *slot = (base + delta).clamp(0, cap); } } else { - let width = raw_env_width(balance, amp_res); - row[0] = r.read_bits(width)? as i32; + row[0] = (r.read_bits(width)? as i32).clamp(0, cap); for b in 1..bands { let delta = read_huffman(r, book)?; - row[b] = row[b - 1] + delta; + row[b] = (row[b - 1] + delta).clamp(0, cap); } } if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() { @@ -368,18 +380,19 @@ impl SbrParser { prev: &[i32], ) -> Result> { let book = noise_book(dt, balance); + let cap = (1i32 << RAW_NOISE_WIDTH) - 1; let mut row = vec![0i32; bands]; if dt { for (b, slot) in row.iter_mut().enumerate() { let delta = read_huffman(r, book)?; let base = prev.get(b).copied().unwrap_or(0); - *slot = base + delta; + *slot = (base + delta).clamp(0, cap); } } else { - row[0] = r.read_bits(RAW_NOISE_WIDTH)? as i32; + row[0] = (r.read_bits(RAW_NOISE_WIDTH)? as i32).clamp(0, cap); for b in 1..bands { let delta = read_huffman(r, book)?; - row[b] = row[b - 1] + delta; + row[b] = (row[b - 1] + delta).clamp(0, cap); } } Ok(row)