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) {
72 72
73 /// Dequantizes one channel's envelopes and noise floors for one frame,73 /// Dequantizes one channel's envelopes and noise floors for one frame,
74 /// de-mixing through [`dequant_pair`] when `coupling` makes `channels[1]`74 /// de-mixing through [`dequant_pair`] when `coupling` makes `channels[1]`
75-/// a balance channel against `channels[0]`. Noise floors dequantize75+/// a balance channel against `channels[0]`.
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).
79 pub fn dequantize_frame(76 pub fn dequantize_frame(
80 header: &SbrHeader,77 header: &SbrHeader,
81 channels: &[SbrChannel],78 channels: &[SbrChannel],
@@ -111,6 +108,12 @@ pub fn dequantize_frame(
111 env.push(row.iter().map(|&v| dequant_env(v, amp_res)).collect());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 let noise = c117 let noise = c
115 .q_q118 .q_q
116 .iter()119 .iter()
crates/ec-aac/src/sbr_payload.rsmodified+20 -7
@@ -336,19 +336,31 @@ impl SbrParser {
336 prev: &[i32],336 prev: &[i32],
337 ) -> Result<Vec<i32>> {337 ) -> Result<Vec<i32>> {
338 let book = env_book(dt, balance, amp_res);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 let mut row = vec![0i32; bands];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 if dt {353 if dt {
341 for (b, slot) in row.iter_mut().enumerate() {354 for (b, slot) in row.iter_mut().enumerate() {
342 let delta = read_huffman(r, book)?;355 let delta = read_huffman(r, book)?;
343 let base = prev.get(b).copied().unwrap_or(0);356 let base = prev.get(b).copied().unwrap_or(0);
344- *slot = base + delta;357+ *slot = (base + delta).clamp(0, cap);
345 }358 }
346 } else {359 } else {
347- let width = raw_env_width(balance, amp_res);360+ row[0] = (r.read_bits(width)? as i32).clamp(0, cap);
348- row[0] = r.read_bits(width)? as i32;
349 for b in 1..bands {361 for b in 1..bands {
350 let delta = read_huffman(r, book)?;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 if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() {366 if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() {
@@ -368,18 +380,19 @@ impl SbrParser {
368 prev: &[i32],380 prev: &[i32],
369 ) -> Result<Vec<i32>> {381 ) -> Result<Vec<i32>> {
370 let book = noise_book(dt, balance);382 let book = noise_book(dt, balance);
383+ let cap = (1i32 << RAW_NOISE_WIDTH) - 1;
371 let mut row = vec![0i32; bands];384 let mut row = vec![0i32; bands];
372 if dt {385 if dt {
373 for (b, slot) in row.iter_mut().enumerate() {386 for (b, slot) in row.iter_mut().enumerate() {
374 let delta = read_huffman(r, book)?;387 let delta = read_huffman(r, book)?;
375 let base = prev.get(b).copied().unwrap_or(0);388 let base = prev.get(b).copied().unwrap_or(0);
376- *slot = base + delta;389+ *slot = (base + delta).clamp(0, cap);
377 }390 }
378 } else {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 for b in 1..bands {393 for b in 1..bands {
381 let delta = read_huffman(r, book)?;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 Ok(row)398 Ok(row)