forge
Explore
Log in

Don't you dare go Hollow

Clamp envelope and noise rows to their field's own width

Browse files at this commit

d1237c2da69be35b4b9f1582a1f9c7f12a250ac4Ahmet Kaan GümüşParent: 407561c
  • fix
    • clamp envelope and noise rows to their field's own width
Show full message
fix(aac-sbr): clamp envelope and noise rows to their field's own width

Defensive: a corrupt DT delta chain could previously snowball without
bound inside one frame. Measured zero effect on the real-file bar --
the implausible values there are legal within the field width, a
separate open defect.
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) {
7272
7373 /// Dequantizes one channel's envelopes and noise floors for one frame,
7474 /// 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]`.
7976 pub fn dequantize_frame(
8077 header: &SbrHeader,
8178 channels: &[SbrChannel],
@@ -111,6 +108,12 @@ pub fn dequantize_frame(
111108 env.push(row.iter().map(|&v| dequant_env(v, amp_res)).collect());
112109 }
113110 }
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.
114117 let noise = c
115118 .q_q
116119 .iter()
crates/ec-aac/src/sbr_payload.rsmodified+20 -7
@@ -336,19 +336,31 @@ impl SbrParser {
336336 prev: &[i32],
337337 ) -> Result<Vec<i32>> {
338338 let book = env_book(dt, balance, amp_res);
339+ let width = raw_env_width(balance, amp_res);
340+ let cap = (1i32 << width) - 1;
339341 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.
340353 if dt {
341354 for (b, slot) in row.iter_mut().enumerate() {
342355 let delta = read_huffman(r, book)?;
343356 let base = prev.get(b).copied().unwrap_or(0);
344- *slot = base + delta;
357+ *slot = (base + delta).clamp(0, cap);
345358 }
346359 } 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);
349361 for b in 1..bands {
350362 let delta = read_huffman(r, book)?;
351- row[b] = row[b - 1] + delta;
363+ row[b] = (row[b - 1] + delta).clamp(0, cap);
352364 }
353365 }
354366 if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() {
@@ -368,18 +380,19 @@ impl SbrParser {
368380 prev: &[i32],
369381 ) -> Result<Vec<i32>> {
370382 let book = noise_book(dt, balance);
383+ let cap = (1i32 << RAW_NOISE_WIDTH) - 1;
371384 let mut row = vec![0i32; bands];
372385 if dt {
373386 for (b, slot) in row.iter_mut().enumerate() {
374387 let delta = read_huffman(r, book)?;
375388 let base = prev.get(b).copied().unwrap_or(0);
376- *slot = base + delta;
389+ *slot = (base + delta).clamp(0, cap);
377390 }
378391 } 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);
380393 for b in 1..bands {
381394 let delta = read_huffman(r, book)?;
382- row[b] = row[b - 1] + delta;
395+ row[b] = (row[b - 1] + delta).clamp(0, cap);
383396 }
384397 }
385398 Ok(row)