Uncoupled SBR CPEs read envelope+noise as two channel passes
Browse files at this commit3745d4569047978e395d1a83b2153a11b3a70722Parent: d1237c2- fix
- uncoupled SBR CPEs read envelope+noise as two channel passes
2 changed files +76 -12
crates/ec-aac/src/sbr_payload.rsmodified+54 -10
| @@ -512,13 +512,25 @@ impl SbrParser { | |||
| 512 | } | 512 | } |
| 513 | } | 513 | } |
| 514 | 514 | ||
| 515 | - let mut channels = Vec::with_capacity(n_channels); | 515 | + // sbr_channel_pair_element's envelope/noise interleave depends on |
| 516 | - for ch in 0..n_channels { | 516 | + // bs_coupling: the COUPLED form reads sbr_envelope(ch)/sbr_noise(ch) |
| 517 | + // interleaved per channel (env0,noise0,env1,noise1), but the | ||
| 518 | + // UNCOUPLED form reads two separate per-channel passes -- | ||
| 519 | + // sbr_envelope(ch=0), sbr_envelope(ch=1), sbr_noise(ch=0), | ||
| 520 | + // sbr_noise(ch=1). Reading the uncoupled form interleaved (as a | ||
| 521 | + // single per-channel loop) is order-invariant in total bit count (so | ||
| 522 | + // a byte-accounting oracle can't see the mistake) but desyncs every | ||
| 523 | + // field after the first channel's envelopes whenever the two | ||
| 524 | + // channels' envelope (6/7-bit) and noise (5-bit) field counts | ||
| 525 | + // differ, which round-11 pinned to ch1's second envelope. | ||
| 526 | + let separated = is_cpe && !coupling && n_channels == 2; | ||
| 527 | + let mut e_q_all: Vec<Vec<Vec<i32>>> = vec![Vec::new(); n_channels]; | ||
| 528 | + let mut q_q_all: Vec<Vec<Vec<i32>>> = vec![Vec::new(); n_channels]; | ||
| 529 | + | ||
| 530 | + let mut read_env = |this: &mut Self, r: &mut BitReader, ch: usize| -> Result<()> { | ||
| 517 | let balance = is_cpe && coupling && ch == 1; | 531 | let balance = is_cpe && coupling && ch == 1; |
| 518 | let num_env = grids[ch].num_env; | 532 | let num_env = grids[ch].num_env; |
| 519 | - let num_noise = if num_env == 1 { 1 } else { 2 }; | ||
| 520 | let amp_res = amp_res_of(header.amp_res, num_env); | 533 | let amp_res = amp_res_of(header.amp_res, num_env); |
| 521 | - | ||
| 522 | let mut e_q: Vec<Vec<i32>> = Vec::with_capacity(num_env); | 534 | let mut e_q: Vec<Vec<i32>> = Vec::with_capacity(num_env); |
| 523 | for i in 0..num_env { | 535 | for i in 0..num_env { |
| 524 | let bands = if grids[ch].freq_res[i] != 0 { | 536 | let bands = if grids[ch].freq_res[i] != 0 { |
| @@ -530,23 +542,55 @@ impl SbrParser { | |||
| 530 | let prev: &[i32] = if i > 0 { | 542 | let prev: &[i32] = if i > 0 { |
| 531 | &e_q[i - 1] | 543 | &e_q[i - 1] |
| 532 | } else { | 544 | } else { |
| 533 | - &self.state[ch].last_env | 545 | + &this.state[ch].last_env |
| 534 | }; | 546 | }; |
| 535 | - let row = self.decode_envelope(r, bands, dt, balance, amp_res, prev)?; | 547 | + let row = this.decode_envelope(r, bands, dt, balance, amp_res, prev)?; |
| 536 | e_q.push(row); | 548 | e_q.push(row); |
| 537 | } | 549 | } |
| 538 | - | 550 | + e_q_all[ch] = e_q; |
| 551 | + Ok(()) | ||
| 552 | + }; | ||
| 553 | + if separated { | ||
| 554 | + for ch in 0..n_channels { | ||
| 555 | + read_env(self, r, ch)?; | ||
| 556 | + } | ||
| 557 | + } | ||
| 558 | + let mut read_noise = |this: &mut Self, r: &mut BitReader, ch: usize| -> Result<()> { | ||
| 559 | + let balance = is_cpe && coupling && ch == 1; | ||
| 560 | + let num_env = grids[ch].num_env; | ||
| 561 | + let num_noise = if num_env == 1 { 1 } else { 2 }; | ||
| 539 | let mut q_q: Vec<Vec<i32>> = Vec::with_capacity(num_noise); | 562 | let mut q_q: Vec<Vec<i32>> = Vec::with_capacity(num_noise); |
| 540 | for i in 0..num_noise { | 563 | for i in 0..num_noise { |
| 541 | let dt = df_noise[ch][i]; | 564 | let dt = df_noise[ch][i]; |
| 542 | let prev: &[i32] = if i > 0 { | 565 | let prev: &[i32] = if i > 0 { |
| 543 | &q_q[i - 1] | 566 | &q_q[i - 1] |
| 544 | } else { | 567 | } else { |
| 545 | - &self.state[ch].last_noise | 568 | + &this.state[ch].last_noise |
| 546 | }; | 569 | }; |
| 547 | - let row = self.decode_noise(r, tables.n_q, dt, balance, prev)?; | 570 | + let row = this.decode_noise(r, tables.n_q, dt, balance, prev)?; |
| 548 | q_q.push(row); | 571 | q_q.push(row); |
| 549 | } | 572 | } |
| 573 | + q_q_all[ch] = q_q; | ||
| 574 | + Ok(()) | ||
| 575 | + }; | ||
| 576 | + if separated { | ||
| 577 | + for ch in 0..n_channels { | ||
| 578 | + read_noise(self, r, ch)?; | ||
| 579 | + } | ||
| 580 | + } else { | ||
| 581 | + for ch in 0..n_channels { | ||
| 582 | + read_env(self, r, ch)?; | ||
| 583 | + read_noise(self, r, ch)?; | ||
| 584 | + } | ||
| 585 | + } | ||
| 586 | + | ||
| 587 | + let mut channels = Vec::with_capacity(n_channels); | ||
| 588 | + for ch in 0..n_channels { | ||
| 589 | + let balance = is_cpe && coupling && ch == 1; | ||
| 590 | + let num_env = grids[ch].num_env; | ||
| 591 | + let amp_res = amp_res_of(header.amp_res, num_env); | ||
| 592 | + let e_q = e_q_all[ch].clone(); | ||
| 593 | + let q_q = q_q_all[ch].clone(); | ||
| 550 | 594 | ||
| 551 | // Containment, not the fix: a still-undiscovered desync could | 595 | // Containment, not the fix: a still-undiscovered desync could |
| 552 | // make a DT delta huge, and since a following frame's DT reads | 596 | // make a DT delta huge, and since a following frame's DT reads |
| @@ -623,7 +667,7 @@ mod tests { | |||
| 623 | const VARVAR_DF_BITS: u64 = 77; | 667 | const VARVAR_DF_BITS: u64 = 77; |
| 624 | const FIXFIX_DT_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x95, 0x78, 0x00, 0x00]; | 668 | const FIXFIX_DT_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x95, 0x78, 0x00, 0x00]; |
| 625 | const FIXFIX_DT_BITS: u64 = 57; | 669 | const FIXFIX_DT_BITS: u64 = 57; |
| 626 | - const CPE_UNCOUPLED_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x00, 0x05, 0x78, 0x07, 0x80, 0x00]; | 670 | + const CPE_UNCOUPLED_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x00, 0x05, 0x78, 0xf0, 0x00, 0x00]; |
| 627 | const CPE_UNCOUPLED_BITS: u64 = 75; | 671 | const CPE_UNCOUPLED_BITS: u64 = 75; |
| 628 | const EXTENSION_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x05, 0xe0, 0x11, 0x00]; | 672 | const EXTENSION_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x05, 0xe0, 0x11, 0x00]; |
| 629 | const EXTENSION_BITS: u64 = 64; | 673 | const EXTENSION_BITS: u64 = 64; |
scripts/aac-tables/sbrpayload_fixtures.pymodified+22 -2
| @@ -152,10 +152,14 @@ def sbr_bits_full(cfg, tables, grid_cfg, words=()): | |||
| 152 | for _ in channels: | 152 | for _ in channels: |
| 153 | for _ in range(tables["n_q"]): | 153 | for _ in range(tables["n_q"]): |
| 154 | s.w(cfg["invf"], 2) | 154 | s.w(cfg["invf"], 2) |
| 155 | - for idx, ch in enumerate(channels): | 155 | + # sbr_channel_pair_element's envelope/noise interleave depends on |
| 156 | + # bs_coupling: COUPLED reads sbr_envelope(ch)/sbr_noise(ch) interleaved | ||
| 157 | + # per channel; UNCOUPLED reads two separate passes -- both channels' | ||
| 158 | + # sbr_envelope(), then both channels' sbr_noise() (matches the Rust | ||
| 159 | + # parser's `separated` split in sbr_payload.rs). | ||
| 160 | + def write_env(idx, ch): | ||
| 156 | balance = 1 if (cpe and coupling and ch == 1) else 0 | 161 | balance = 1 if (cpe and coupling and ch == 1) else 0 |
| 157 | num_env = num_env_list[idx] | 162 | num_env = num_env_list[idx] |
| 158 | - num_noise = 1 if num_env == 1 else 2 | ||
| 159 | freq_res_list = freq_res_lists[idx] | 163 | freq_res_list = freq_res_lists[idx] |
| 160 | amp_res_eff = 0 if num_env == 1 else cfg["amp_res"] | 164 | amp_res_eff = 0 if num_env == 1 else cfg["amp_res"] |
| 161 | for i in range(num_env): | 165 | for i in range(num_env): |
| @@ -168,6 +172,11 @@ def sbr_bits_full(cfg, tables, grid_cfg, words=()): | |||
| 168 | else: | 172 | else: |
| 169 | for _ in range(bands): | 173 | for _ in range(bands): |
| 170 | slot() | 174 | slot() |
| 175 | + | ||
| 176 | + def write_noise(idx, ch): | ||
| 177 | + balance = 1 if (cpe and coupling and ch == 1) else 0 | ||
| 178 | + num_env = num_env_list[idx] | ||
| 179 | + num_noise = 1 if num_env == 1 else 2 | ||
| 171 | for i in range(num_noise): | 180 | for i in range(num_noise): |
| 172 | if cfg["df_noise"][i] == 0: | 181 | if cfg["df_noise"][i] == 0: |
| 173 | key = "noise0b" if balance else "noise0" | 182 | key = "noise0b" if balance else "noise0" |
| @@ -177,6 +186,17 @@ def sbr_bits_full(cfg, tables, grid_cfg, words=()): | |||
| 177 | else: | 186 | else: |
| 178 | for _ in range(tables["n_q"]): | 187 | for _ in range(tables["n_q"]): |
| 179 | slot() | 188 | slot() |
| 189 | + | ||
| 190 | + separated = cpe and not coupling and len(channels) == 2 | ||
| 191 | + if separated: | ||
| 192 | + for idx, ch in enumerate(channels): | ||
| 193 | + write_env(idx, ch) | ||
| 194 | + for idx, ch in enumerate(channels): | ||
| 195 | + write_noise(idx, ch) | ||
| 196 | + else: | ||
| 197 | + for idx, ch in enumerate(channels): | ||
| 198 | + write_env(idx, ch) | ||
| 199 | + write_noise(idx, ch) | ||
| 180 | for ch in channels: | 200 | for ch in channels: |
| 181 | on = cfg.get("add_harmonic", [0, 0])[ch] | 201 | on = cfg.get("add_harmonic", [0, 0])[ch] |
| 182 | s.w(1 if on else 0, 1) | 202 | s.w(1 if on else 0, 1) |