Correct §4.6.18.7.2 coupled-CPE pan-offset/exponent math
Browse files at this commitc1d01e5623c1965e976bc87dc1744d4767ddbe69Parent: 9630027- fix
- correct §4.6.18.7.2 coupled-CPE pan-offset/exponent math
2 changed files +74 -54
crates/ec-aac/src/sbr_env.rsmodified+63 -51
| @@ -10,21 +10,20 @@ | |||
| 10 | //! with `alpha = 1.0` at `bs_amp_res = 1` (3 dB steps) and `0.5` at | 10 | //! with `alpha = 1.0` at `bs_amp_res = 1` (3 dB steps) and `0.5` at |
| 11 | //! `bs_amp_res = 0` (1.5 dB steps); the noise floor is `2^(6 - q_q)` | 11 | //! `bs_amp_res = 0` (1.5 dB steps); the noise floor is `2^(6 - q_q)` |
| 12 | //! (`NOISE_FLOOR_OFFSET = 6`). A coupled CPE's balance channel (`e_q`'s | 12 | //! (`NOISE_FLOOR_OFFSET = 6`). A coupled CPE's balance channel (`e_q`'s |
| 13 | -//! second channel) carries a log-ratio rather than an absolute | 13 | +//! second channel) carries a log-ratio (`pan`) rather than an absolute |
| 14 | //! scalefactor; `dequant_pair` de-mixes it against channel 0's raw value | 14 | //! 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's | 15 | +//! into `(env0, env1)` energies that are equal (and each equal to channel |
| 16 | -//! energy`) when the ratio is centred. The ratio's exponent is | 16 | +//! 0's plain energy) when the ratio is centred (ISO/IEC 14496-3 |
| 17 | -//! `2 * alpha * balance_raw - 12`: pinned empirically (not from spec | 17 | +//! §4.6.18.7.2): `ratio = 2^(pan - panOffset)`, `panOffset = 24` at |
| 18 | -//! text) by writing coupled CPE payloads with known asymmetric | 18 | +//! `bs_amp_res=0` and `12` at `bs_amp_res=1`, `env0 = 2*Ecurr/(1+ratio)`, |
| 19 | -//! `(channel 0 raw, balance raw)` pairs through a decodable HE-AAC | 19 | +//! `env1 = 2*Ecurr*ratio/(1+ratio)`. A real coupled-CPE fixture |
| 20 | -//! stream, decoding both channels' PCM with the reference decoder, and | 20 | +//! (`heaac_44100_48k.m4a`, `coupled_cpe_channel_swap_probe`) settled this: |
| 21 | -//! fitting the left/right energy ratio against the balance raw value -- | 21 | +//! its transmitted balance raw values cluster at 12 under `bs_amp_res=1`, |
| 22 | -//! done separately for `bs_amp_res=0` (offset 12 in raw units, one | 22 | +//! matching a centred pan at `panOffset=12` exactly, and only this |
| 23 | -//! exponent step per raw unit) and `bs_amp_res=1` (offset 6, two | 23 | +//! `ratio` shape (not the previous `2*alpha*raw - 12` exponent) plus |
| 24 | -//! exponent steps per raw unit); both collapse to the same | 24 | +//! keeping `env0`/`env1` on their own physical channel (not swapped |
| 25 | -//! `2*alpha*raw - 12` form (`scripts/aac-tables/sbrpayload_fixtures.py` | 25 | +//! into a `left`/`right` naming) reproduces the reference decoder's L/R |
| 26 | -//! carries no permanent copy of the probe -- the fit is recorded here | 26 | +//! assignment on that file. |
| 27 | -//! and in the project ledger). | ||
| 28 | //! | 27 | //! |
| 29 | //! # Gain, limiter, noise, sinusoids | 28 | //! # Gain, limiter, noise, sinusoids |
| 30 | //! | 29 | //! |
| @@ -75,13 +74,33 @@ pub fn dequant_noise(q_q: i32) -> f64 { | |||
| 75 | } | 74 | } |
| 76 | 75 | ||
| 77 | /// De-mixes a coupled CPE's `(channel 0 raw, balance raw)` pair into | 76 | /// De-mixes a coupled CPE's `(channel 0 raw, balance raw)` pair into |
| 78 | -/// `(left, right)` linear energies. See the module doc for the convention. | 77 | +/// `(env0, env1)` linear energies (ISO/IEC 14496-3 §4.6.18.7.2, |
| 79 | -pub fn dequant_pair(e0_raw: i32, e1_raw: i32, amp_res: u8) -> (f64, f64) { | 78 | +/// `bs_coupling`): `Ecurr = dequant_env(e0_raw, amp_res)`, |
| 80 | - let e0 = dequant_env(e0_raw, amp_res); | 79 | +/// `ratio = 2^(pan - panOffset)` where `pan` is the RAW balance value |
| 81 | - let ratio = 2f64.powf(2.0 * alpha(amp_res) * f64::from(e1_raw) - 12.0); | 80 | +/// (channels[1]'s `e_q`, not run through `alpha`/`amp_res` scaling) and |
| 82 | - let left = 2.0 * e0 * ratio / (1.0 + ratio); | 81 | +/// `panOffset` is 24 at `bs_amp_res=0` (1.5 dB steps) or 12 at |
| 83 | - let right = 2.0 * e0 / (1.0 + ratio); | 82 | +/// `bs_amp_res=1` (3 dB steps); `env0 = 2*Ecurr/(1+ratio)`, |
| 84 | - (left, right) | 83 | +/// `env1 = 2*Ecurr*ratio/(1+ratio)`. The previous `2*alpha*raw - 12` |
| 84 | +/// exponent (doubling the step at `amp_res=1` and using the wrong offset | ||
| 85 | +/// at `amp_res=0`), combined with a `left`/`right` naming below that | ||
| 86 | +/// handed physical channel 0 the `env1` shape and channel 1 the `env0` | ||
| 87 | +/// shape, together produced a real-file coupled-CPE L/R swap | ||
| 88 | +/// (`coupled_cpe_channel_swap_probe`, ch0 correlated 0.98 against the | ||
| 89 | +/// REFERENCE's right channel, not its left). This stream's live balance | ||
| 90 | +/// raw values cluster at 12 (`amp_res=1`), matching a centred pan at | ||
| 91 | +/// `panOffset=12` exactly, confirming the offset direction empirically. | ||
| 92 | +/// `env_amp_res` scales `e0_raw` (forced to 0/1.5 dB on a single-envelope | ||
| 93 | +/// frame, mirroring `sbr_payload`'s own `amp_res_of`); `header_amp_res` is | ||
| 94 | +/// the SBR header's transmitted `bs_amp_res` field UNCONDITIONALLY, which | ||
| 95 | +/// governs `panOffset` regardless of the single-envelope override -- the | ||
| 96 | +/// two diverge on single-envelope coupled frames. | ||
| 97 | +pub fn dequant_pair(e0_raw: i32, pan_raw: i32, env_amp_res: u8, header_amp_res: u8) -> (f64, f64) { | ||
| 98 | + let e_curr = dequant_env(e0_raw, env_amp_res); | ||
| 99 | + let pan_offset = if header_amp_res != 0 { 12.0 } else { 24.0 }; | ||
| 100 | + let ratio = 2f64.powf(f64::from(pan_raw) - pan_offset); | ||
| 101 | + let env0 = 2.0 * e_curr / (1.0 + ratio); | ||
| 102 | + let env1 = 2.0 * e_curr * ratio / (1.0 + ratio); | ||
| 103 | + (env0, env1) | ||
| 85 | } | 104 | } |
| 86 | 105 | ||
| 87 | /// Dequantizes one channel's envelopes and noise floors for one frame, | 106 | /// Dequantizes one channel's envelopes and noise floors for one frame, |
| @@ -99,21 +118,16 @@ pub fn dequantize_frame( | |||
| 99 | let amp_res = if c.e_q.len() == 1 { 0 } else { header.amp_res }; | 118 | let amp_res = if c.e_q.len() == 1 { 0 } else { header.amp_res }; |
| 100 | let mut env = Vec::with_capacity(c.e_q.len()); | 119 | let mut env = Vec::with_capacity(c.e_q.len()); |
| 101 | if coupling && channels.len() == 2 { | 120 | if coupling && channels.len() == 2 { |
| 102 | - let (row0_src, row1_src, want_left) = if ch == 0 { | ||
| 103 | - (0usize, 1usize, true) | ||
| 104 | - } else { | ||
| 105 | - (0usize, 1usize, false) | ||
| 106 | - }; | ||
| 107 | for i in 0..c.e_q.len() { | 121 | for i in 0..c.e_q.len() { |
| 108 | - let row0 = channels[row0_src].e_q.get(i); | 122 | + let row0 = channels[0].e_q.get(i); |
| 109 | - let row1 = channels[row1_src].e_q.get(i); | 123 | + let row1 = channels[1].e_q.get(i); |
| 110 | let len = c.e_q[i].len(); | 124 | let len = c.e_q[i].len(); |
| 111 | let mut r = Vec::with_capacity(len); | 125 | let mut r = Vec::with_capacity(len); |
| 112 | for b in 0..len { | 126 | for b in 0..len { |
| 113 | let e0 = row0.and_then(|r| r.get(b)).copied().unwrap_or(0); | 127 | let e0 = row0.and_then(|r| r.get(b)).copied().unwrap_or(0); |
| 114 | - let e1 = row1.and_then(|r| r.get(b)).copied().unwrap_or(0); | 128 | + let pan = row1.and_then(|r| r.get(b)).copied().unwrap_or(0); |
| 115 | - let (left, right) = dequant_pair(e0, e1, amp_res); | 129 | + let (env0, env1) = dequant_pair(e0, pan, amp_res, header.amp_res); |
| 116 | - r.push(if want_left { left } else { right }); | 130 | + r.push(if ch == 0 { env0 } else { env1 }); |
| 117 | } | 131 | } |
| 118 | env.push(r); | 132 | env.push(r); |
| 119 | } | 133 | } |
| @@ -1042,33 +1056,31 @@ mod tests { | |||
| 1042 | 1056 | ||
| 1043 | #[test] | 1057 | #[test] |
| 1044 | fn coupled_balance_round_trips_at_a_centred_ratio() { | 1058 | fn coupled_balance_round_trips_at_a_centred_ratio() { |
| 1045 | - // The ratio's exponent is `2*alpha*balance_raw - 12` (pinned against | 1059 | + // The ratio's exponent is `pan_raw - panOffset` (ISO/IEC 14496-3 |
| 1046 | - // a reference decoder, see the module doc): it's centred at | 1060 | + // §4.6.18.7.2, see the module doc): it's centred (ratio=1) at |
| 1047 | - // balance_raw=6 for amp_res=1 (alpha=1) and balance_raw=12 for | 1061 | + // pan_raw=12 for amp_res=1 and pan_raw=24 for amp_res=0. |
| 1048 | - // amp_res=0 (alpha=0.5). | 1062 | + let (e0, e1) = dequant_pair(10, 12, 1, 1); |
| 1049 | - let (l, r) = dequant_pair(10, 6, 1); | 1063 | + let e0_plain = dequant_env(10, 1); |
| 1050 | - let e0 = dequant_env(10, 1); | ||
| 1051 | assert!( | 1064 | assert!( |
| 1052 | - (l - e0).abs() < 1e-6 && (r - e0).abs() < 1e-6, | 1065 | + (e0 - e0_plain).abs() < 1e-6 && (e1 - e0_plain).abs() < 1e-6, |
| 1053 | - "{l} {r} vs {e0}" | 1066 | + "{e0} {e1} vs {e0_plain}" |
| 1054 | ); | 1067 | ); |
| 1055 | - let (l0, r0) = dequant_pair(10, 12, 0); | 1068 | + let (e00, e10) = dequant_pair(10, 24, 0, 0); |
| 1056 | - let e00 = dequant_env(10, 0); | 1069 | + let e00_plain = dequant_env(10, 0); |
| 1057 | assert!( | 1070 | assert!( |
| 1058 | - (l0 - e00).abs() < 1e-6 && (r0 - e00).abs() < 1e-6, | 1071 | + (e00 - e00_plain).abs() < 1e-6 && (e10 - e00_plain).abs() < 1e-6, |
| 1059 | - "{l0} {r0} vs {e00}" | 1072 | + "{e00} {e10} vs {e00_plain}" |
| 1060 | ); | 1073 | ); |
| 1061 | } | 1074 | } |
| 1062 | 1075 | ||
| 1063 | #[test] | 1076 | #[test] |
| 1064 | fn coupled_balance_shifts_energy_toward_the_channel_with_a_higher_raw_value() { | 1077 | fn coupled_balance_shifts_energy_toward_the_channel_with_a_higher_raw_value() { |
| 1065 | - // Fit against the reference decoder's own PCM output (not spec | 1078 | + // §4.6.18.7.2: as the raw balance value grows past the centre |
| 1066 | - // text): as balance_raw grows past the centre, left grows toward | 1079 | + // (panOffset), env1 grows toward `2*Ecurr` and env0 shrinks toward |
| 1067 | - // `2*e0` and right shrinks toward 0, matching the measured PCM | 1080 | + // 0 (env1's formula carries the `ratio` factor, env0's doesn't). |
| 1068 | - // energy split for a real coupled CPE. | 1081 | + let (e0_low, e1_low) = dequant_pair(10, 0, 1, 1); |
| 1069 | - let (l_low, r_low) = dequant_pair(10, 0, 1); | 1082 | + let (e0_high, e1_high) = dequant_pair(10, 24, 1, 1); |
| 1070 | - let (l_high, r_high) = dequant_pair(10, 20, 1); | 1083 | + assert!(e0_low > e1_low, "{e0_low} vs {e1_low}"); |
| 1071 | - assert!(l_low < r_low, "{l_low} vs {r_low}"); | 1084 | + assert!(e0_high < e1_high, "{e0_high} vs {e1_high}"); |
| 1072 | - assert!(l_high > r_high, "{l_high} vs {r_high}"); | ||
| 1073 | } | 1085 | } |
| 1074 | } | 1086 | } |
crates/ec-aac/tests/sbr_real_library.rsmodified+11 -3
| @@ -3870,9 +3870,17 @@ fn reelq_multichannel_lc_matches_reference() { | |||
| 3870 | /// Coupled-CPE swap probe: the `heaac_44100_48k` fixture's SBR side info | 3870 | /// Coupled-CPE swap probe: the `heaac_44100_48k` fixture's SBR side info |
| 3871 | /// shows `coupling: true` on nearly every AU (reeln/FMJ, both passing, | 3871 | /// shows `coupling: true` on nearly every AU (reeln/FMJ, both passing, |
| 3872 | /// show `coupling: false`) -- checks whether our decoded ch0 actually | 3872 | /// show `coupling: false`) -- checks whether our decoded ch0 actually |
| 3873 | -/// correlates better against the REFERENCE's R channel than its own L, | 3873 | +/// correlates better against the REFERENCE's R channel than its own L. |
| 3874 | -/// which would mean `dequant_pair`'s left/right assignment in | 3874 | +/// RULED OUT as the cause (this round): `sbr_env::dequant_pair`'s |
| 3875 | -/// `sbr_env::dequantize_frame` is swapped for coupled CPEs. | 3875 | +/// `panOffset`/exponent formula had two real spec bugs (fixed, see its |
| 3876 | +/// doc comment) but neither the fix nor `EC_AAC_SBR_HF_BYPASS=1` (which | ||
| 3877 | +/// reduces SBR to a per-channel low-band QMF round trip with NO | ||
| 3878 | +/// envelope/coupling/HF-generation code running at all) changes this | ||
| 3879 | +/// probe's correlation numbers -- the swap survives with the entire | ||
| 3880 | +/// `sbr_env`/`sbr_hf` machinery out of the picture, so it lives | ||
| 3881 | +/// elsewhere (plane/channel threading in `sbr_chain::apply_data` or | ||
| 3882 | +/// `decode.rs`'s per-AU channel assembly, or this test's own | ||
| 3883 | +/// interleave-to-plane de-mux -- unexplored this round). | ||
| 3876 | #[test] | 3884 | #[test] |
| 3877 | fn coupled_cpe_channel_swap_probe() { | 3885 | fn coupled_cpe_channel_swap_probe() { |
| 3878 | if !have_ffmpeg() { | 3886 | if !have_ffmpeg() { |