Clamp envelope and noise rows to their field's own width
Browse files at this commitd1237c2da69be35b4b9f1582a1f9c7f12a250ac4Parent: 407561c- fix
- clamp envelope and noise rows to their field's own width
2 changed files +27 -11
crates/ec-aac/src/sbr_env.rsmodified+7 -4
| @@ -72,10 +72,7 @@ pub fn dequant_pair(e0_raw: i32, e1_raw: i32, amp_res: u8) -> (f64, f64) { | ||
| 72 | 72 | |
| 73 | 73 | /// Dequantizes one channel's envelopes and noise floors for one frame, |
| 74 | 74 | /// de-mixing through [`dequant_pair`] when `coupling` makes `channels[1]` |
| 75 | -/// a balance channel against `channels[0]`. Noise floors dequantize | |
| 76 | -/// independently per channel regardless of coupling (corner-cut: the | |
| 77 | -/// balance codebook's noise ratio convention is unverified against a real | |
| 78 | -/// coupled file, same ceiling as the envelope one above). | |
| 75 | +/// a balance channel against `channels[0]`. | |
| 79 | 76 | pub fn dequantize_frame( |
| 80 | 77 | header: &SbrHeader, |
| 81 | 78 | channels: &[SbrChannel], |
| @@ -111,6 +108,12 @@ pub fn dequantize_frame( | ||
| 111 | 108 | env.push(row.iter().map(|&v| dequant_env(v, amp_res)).collect()); |
| 112 | 109 | } |
| 113 | 110 | } |
| 111 | + // corner-cut: noise floors dequantize independently per channel | |
| 112 | + // regardless of coupling. A mirrored dequant_noise_pair() demix (see | |
| 113 | + // git history) was tried and measured ZERO effect on the real-file | |
| 114 | + // ch1 RMS blow-up (identical output to printed precision), so the | |
| 115 | + // energy dequantization below is the live suspect, not this; reverted | |
| 116 | + // rather than keeping an unverified formula with no measured benefit. | |
| 114 | 117 | let noise = c |
| 115 | 118 | .q_q |
| 116 | 119 | .iter() |
crates/ec-aac/src/sbr_payload.rsmodified+20 -7
| @@ -336,19 +336,31 @@ impl SbrParser { | ||
| 336 | 336 | prev: &[i32], |
| 337 | 337 | ) -> Result<Vec<i32>> { |
| 338 | 338 | let book = env_book(dt, balance, amp_res); |
| 339 | + let width = raw_env_width(balance, amp_res); | |
| 340 | + let cap = (1i32 << width) - 1; | |
| 339 | 341 | let mut row = vec![0i32; bands]; |
| 342 | + // Every cell here is a `bs_data_env` raw scalefactor, whose valid | |
| 343 | + // range is exactly the transmitted field's own bit width regardless | |
| 344 | + // of path (DT against last frame's state, or DF's within-row delta | |
| 345 | + // chain) -- clamped at construction, not just when carried to the | |
| 346 | + // NEXT frame's `last_env` (as it already was below): an unclamped | |
| 347 | + // intermediate here let one bad Huffman symbol (desync or a | |
| 348 | + // genuinely huge encoded delta) cascade through the rest of the row | |
| 349 | + // via `row[b-1] + delta`, producing a raw value in the hundreds | |
| 350 | + // whose dequantized (`2^v`) energy is 10+ orders of magnitude too | |
| 351 | + // large -- exactly the coupled CPE's second-channel envelope | |
| 352 | + // blowups measured on real HE-AAC files. | |
| 340 | 353 | if dt { |
| 341 | 354 | for (b, slot) in row.iter_mut().enumerate() { |
| 342 | 355 | let delta = read_huffman(r, book)?; |
| 343 | 356 | let base = prev.get(b).copied().unwrap_or(0); |
| 344 | - *slot = base + delta; | |
| 357 | + *slot = (base + delta).clamp(0, cap); | |
| 345 | 358 | } |
| 346 | 359 | } else { |
| 347 | - let width = raw_env_width(balance, amp_res); | |
| 348 | - row[0] = r.read_bits(width)? as i32; | |
| 360 | + row[0] = (r.read_bits(width)? as i32).clamp(0, cap); | |
| 349 | 361 | for b in 1..bands { |
| 350 | 362 | let delta = read_huffman(r, book)?; |
| 351 | - row[b] = row[b - 1] + delta; | |
| 363 | + row[b] = (row[b - 1] + delta).clamp(0, cap); | |
| 352 | 364 | } |
| 353 | 365 | } |
| 354 | 366 | if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() { |
| @@ -368,18 +380,19 @@ impl SbrParser { | ||
| 368 | 380 | prev: &[i32], |
| 369 | 381 | ) -> Result<Vec<i32>> { |
| 370 | 382 | let book = noise_book(dt, balance); |
| 383 | + let cap = (1i32 << RAW_NOISE_WIDTH) - 1; | |
| 371 | 384 | let mut row = vec![0i32; bands]; |
| 372 | 385 | if dt { |
| 373 | 386 | for (b, slot) in row.iter_mut().enumerate() { |
| 374 | 387 | let delta = read_huffman(r, book)?; |
| 375 | 388 | let base = prev.get(b).copied().unwrap_or(0); |
| 376 | - *slot = base + delta; | |
| 389 | + *slot = (base + delta).clamp(0, cap); | |
| 377 | 390 | } |
| 378 | 391 | } else { |
| 379 | - row[0] = r.read_bits(RAW_NOISE_WIDTH)? as i32; | |
| 392 | + row[0] = (r.read_bits(RAW_NOISE_WIDTH)? as i32).clamp(0, cap); | |
| 380 | 393 | for b in 1..bands { |
| 381 | 394 | let delta = read_huffman(r, book)?; |
| 382 | - row[b] = row[b - 1] + delta; | |
| 395 | + row[b] = (row[b - 1] + delta).clamp(0, cap); | |
| 383 | 396 | } |
| 384 | 397 | } |
| 385 | 398 | Ok(row) |