forge
Explore
Log in

Don't you dare go Hollow

Coupled envelope de-mix ratio needed a -12 offset, not raw balance

Browse files at this commit

81915ceb9417e3b9a74cc26a8bec83b9c4136164Ahmet Kaan GümüşParent: 7dabfff
  • fix
    • coupled envelope de-mix ratio needed a -12 offset, not raw balance
Show full message
fix(aac-sbr): coupled envelope de-mix ratio needed a -12 offset, not raw balance

Pinned empirically by writing coupled CPE payloads with known
(channel0, balance) raw pairs through a decodable HE-AAC stream and
fitting the left/right PCM energy split against the balance raw value
for both bs_amp_res=0 and 1. Both collapse to the same
`2*alpha*raw - 12` exponent (previously used `alpha*raw`, uncentred).

Real-file sweep still misses the bar: below-crossover (pure core,
no SBR) correlation for the second channel is already near zero on
both real files, meaning the dominant remaining fault is upstream of
sbr_env, in core-level channel-pair decode -- not a de-mix convention
issue. See ledger for the swap/cross-channel correlation evidence.
crates/ec-aac/src/sbr_env.rsmodified+37 -9
@@ -13,12 +13,18 @@
13 //! second channel) carries a log-ratio rather than an absolute13 //! second channel) carries a log-ratio rather than an absolute
14 //! scalefactor; `dequant_pair` de-mixes it against channel 0's raw value14 //! scalefactor; `dequant_pair` de-mixes it against channel 0's raw value
15 //! into left/right energies that are equal (and sum to `2 * channel 0's15 //! into left/right energies that are equal (and sum to `2 * channel 0's
16-//! energy`) when the ratio is centred, which is the symmetric convention16+//! energy`) when the ratio is centred. The ratio's exponent is
17-//! this module uses in the absence of an exercised real-file fixture for17+//! `2 * alpha * balance_raw - 12`: pinned empirically (not from spec
18-//! it (corner-cut: exact reference offset unverified, ceiling = a18+//! text) by writing coupled CPE payloads with known asymmetric
19-//! panning-accuracy-only artifact on files whose SBR CPE actually19+//! `(channel 0 raw, balance raw)` pairs through a decodable HE-AAC
20-//! couples, upgrade path = pin against a captured coupled real-file20+//! stream, decoding both channels' PCM with the reference decoder, and
21-//! trace).21+//! fitting the left/right energy ratio against the balance raw value --
22+//! done separately for `bs_amp_res=0` (offset 12 in raw units, one
23+//! exponent step per raw unit) and `bs_amp_res=1` (offset 6, two
24+//! exponent steps per raw unit); both collapse to the same
25+//! `2*alpha*raw - 12` form (`scripts/aac-tables/sbrpayload_fixtures.py`
26+//! carries no permanent copy of the probe -- the fit is recorded here
27+//! and in the project ledger).
22 //!28 //!
23 //! # Gain, limiter, noise, sinusoids29 //! # Gain, limiter, noise, sinusoids
24 //!30 //!
@@ -58,7 +64,7 @@ pub fn dequant_noise(q_q: i32) -> f64 {
58 /// `(left, right)` linear energies. See the module doc for the convention.64 /// `(left, right)` linear energies. See the module doc for the convention.
59 pub fn dequant_pair(e0_raw: i32, e1_raw: i32, amp_res: u8) -> (f64, f64) {65 pub fn dequant_pair(e0_raw: i32, e1_raw: i32, amp_res: u8) -> (f64, f64) {
60 let e0 = dequant_env(e0_raw, amp_res);66 let e0 = dequant_env(e0_raw, amp_res);
61- let ratio = 2f64.powf(alpha(amp_res) * f64::from(e1_raw));67+ let ratio = 2f64.powf(2.0 * alpha(amp_res) * f64::from(e1_raw) - 12.0);
62 let left = 2.0 * e0 * ratio / (1.0 + ratio);68 let left = 2.0 * e0 * ratio / (1.0 + ratio);
63 let right = 2.0 * e0 / (1.0 + ratio);69 let right = 2.0 * e0 / (1.0 + ratio);
64 (left, right)70 (left, right)
@@ -413,11 +419,33 @@ mod tests {
413 419
414 #[test]420 #[test]
415 fn coupled_balance_round_trips_at_a_centred_ratio() {421 fn coupled_balance_round_trips_at_a_centred_ratio() {
416- let (l, r) = dequant_pair(10, 0, 1);422+ // The ratio's exponent is `2*alpha*balance_raw - 12` (pinned against
423+ // a reference decoder, see the module doc): it's centred at
424+ // balance_raw=6 for amp_res=1 (alpha=1) and balance_raw=12 for
425+ // amp_res=0 (alpha=0.5).
426+ let (l, r) = dequant_pair(10, 6, 1);
417 let e0 = dequant_env(10, 1);427 let e0 = dequant_env(10, 1);
418 assert!(428 assert!(
419- (l - e0).abs() < 1e-9 && (r - e0).abs() < 1e-9,429+ (l - e0).abs() < 1e-6 && (r - e0).abs() < 1e-6,
420 "{l} {r} vs {e0}"430 "{l} {r} vs {e0}"
421 );431 );
432+ let (l0, r0) = dequant_pair(10, 12, 0);
433+ let e00 = dequant_env(10, 0);
434+ assert!(
435+ (l0 - e00).abs() < 1e-6 && (r0 - e00).abs() < 1e-6,
436+ "{l0} {r0} vs {e00}"
437+ );
438+ }
439+
440+ #[test]
441+ fn coupled_balance_shifts_energy_toward_the_channel_with_a_higher_raw_value() {
442+ // Fit against the reference decoder's own PCM output (not spec
443+ // text): as balance_raw grows past the centre, left grows toward
444+ // `2*e0` and right shrinks toward 0, matching the measured PCM
445+ // energy split for a real coupled CPE.
446+ let (l_low, r_low) = dequant_pair(10, 0, 1);
447+ let (l_high, r_high) = dequant_pair(10, 20, 1);
448+ assert!(l_low < r_low, "{l_low} vs {r_low}");
449+ assert!(l_high > r_high, "{l_high} vs {r_high}");
422 }450 }
423 }451 }