ec-ac3 E-AC-3 SPX noise: match ffmpeg eac3dec.c. Root cause: noise came from a separate LFG (decode.rs spx_noise) and was the integer dither mantissa scaled by sqrt(3); ffmpeg draws the raw word from the shared dith_state as (int32)w * nblend*rms*coord/INT32_MIN with nblend=sqrt(3*nratio) (ac3dec.c spx_coordinates, eac3dec.c apply_spectral_extension). Fix: nblend=sqrt(3*nratio), noise from core.dither.next_word(). Evidence: csi_miami_5.1_256_spx_small.eac3 was ch0 rms 6.2e-5, ch2 1.6e-3, ch3 corr 0.9978; unrun, orchestrator to verify
Browse files at this commit5555e532904f76cee98f4e3edd56ba19b4f40c28Parent: 61f297e3 changed files +3 -17
crates/ec-ac3/src/decode.rsmodified+0 -4
| @@ -118,9 +118,6 @@ pub(crate) struct Core { | |||
| 118 | pub(crate) first_spxco: [bool; MAX_FBW], | 118 | pub(crate) first_spxco: [bool; MAX_FBW], |
| 119 | pub(crate) chinspxatten: [bool; MAX_FBW], | 119 | pub(crate) chinspxatten: [bool; MAX_FBW], |
| 120 | pub(crate) spxattencod: [u8; MAX_FBW], | 120 | pub(crate) spxattencod: [u8; MAX_FBW], |
| 121 | - /// The noise source of §3.6.4.2.4, kept apart from the dither source so | ||
| 122 | - /// neither one's sequence depends on the other's use. | ||
| 123 | - pub(crate) spx_noise: Dither, | ||
| 124 | // Adaptive hybrid transform (Annex E §3.4). All false under AC-3. | 121 | // Adaptive hybrid transform (Annex E §3.4). All false under AC-3. |
| 125 | /// Which channels are AHT-coded this frame. | 122 | /// Which channels are AHT-coded this frame. |
| 126 | pub(crate) aht: [bool; CHANNELS], | 123 | pub(crate) aht: [bool; CHANNELS], |
| @@ -190,7 +187,6 @@ impl Core { | |||
| 190 | first_spxco: [true; MAX_FBW], | 187 | first_spxco: [true; MAX_FBW], |
| 191 | chinspxatten: [false; MAX_FBW], | 188 | chinspxatten: [false; MAX_FBW], |
| 192 | spxattencod: [0; MAX_FBW], | 189 | spxattencod: [0; MAX_FBW], |
| 193 | - spx_noise: Dither::default(), | ||
| 194 | aht: [false; CHANNELS], | 190 | aht: [false; CHANNELS], |
| 195 | aht_read: [false; CHANNELS], | 191 | aht_read: [false; CHANNELS], |
| 196 | aht_mant: vec![0.0; CHANNELS * crate::aht::AHT_BLOCKS * COEFFS], | 192 | aht_mant: vec![0.0; CHANNELS * crate::aht::AHT_BLOCKS * COEFFS], |
crates/ec-ac3/src/eac3.rsmodified+2 -5
| @@ -781,7 +781,7 @@ fn parse_block( | |||
| 781 | // §3.6.4.2.1: the blend follows the band's frequency midpoint. | 781 | // §3.6.4.2.1: the blend follows the band's frequency midpoint. |
| 782 | let bandsize = core.spxbndsz[bnd] as f32; | 782 | let bandsize = core.spxbndsz[bnd] as f32; |
| 783 | let nratio = ((spxmant + 0.5 * bandsize) / end - spxblnd).clamp(0.0, 1.0); | 783 | let nratio = ((spxmant + 0.5 * bandsize) / end - spxblnd).clamp(0.0, 1.0); |
| 784 | - core.nblend[ch][bnd] = nratio.sqrt(); | 784 | + core.nblend[ch][bnd] = (3.0 * nratio).sqrt(); |
| 785 | core.sblend[ch][bnd] = (1.0 - nratio).sqrt(); | 785 | core.sblend[ch][bnd] = (1.0 - nratio).sqrt(); |
| 786 | spxmant += bandsize; | 786 | spxmant += bandsize; |
| 787 | } | 787 | } |
| @@ -1191,11 +1191,8 @@ fn spectral_extension(core: &mut Core, ch: usize) { | |||
| 1191 | if spxmant >= COEFFS { | 1191 | if spxmant >= COEFFS { |
| 1192 | break; | 1192 | break; |
| 1193 | } | 1193 | } |
| 1194 | - // §E3.6.4.2.4's noise(): a zero-mean unit-variance source the | ||
| 1195 | - // standard leaves to the implementation, so it is switched by the | ||
| 1196 | - // same option as §7.3.4's dither. | ||
| 1197 | let noise = if core.dither_on { | 1194 | let noise = if core.dither_on { |
| 1198 | - core.spx_noise.unit_variance() | 1195 | + (core.dither.next_word() as i32) as f32 / -2_147_483_648.0 |
| 1199 | } else { | 1196 | } else { |
| 1200 | 0.0 | 1197 | 0.0 |
| 1201 | }; | 1198 | }; |
crates/ec-ac3/src/mantissa.rsmodified+1 -8
| @@ -55,13 +55,6 @@ impl Dither { | |||
| 55 | self.mantissa() as f32 / 8_388_608.0 | 55 | self.mantissa() as f32 / 8_388_608.0 |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | - /// Next value from a zero-mean, unit-variance source — what Annex E | ||
| 59 | - /// §3.6.4.2.4 calls `noise()`. A uniform distribution on `[-1, 1]` has | ||
| 60 | - /// variance 1/3, so it is scaled by `sqrt(3)`. | ||
| 61 | - pub fn unit_variance(&mut self) -> f32 { | ||
| 62 | - self.mantissa() as f32 / 5_931_008.0 * 1.732_050_8 | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | /// ffmpeg's integer dither mantissa: `((x >> 8) * 181 >> 8) - 5931008`. | 58 | /// ffmpeg's integer dither mantissa: `((x >> 8) * 181 >> 8) - 5931008`. |
| 66 | pub fn mantissa(&mut self) -> i32 { | 59 | pub fn mantissa(&mut self) -> i32 { |
| 67 | let x = self.next_word(); | 60 | let x = self.next_word(); |
| @@ -70,7 +63,7 @@ impl Dither { | |||
| 70 | } | 63 | } |
| 71 | 64 | ||
| 72 | /// `av_lfg_get`: `state[i] = state[i-24] + state[i-55]`, returning the new word. | 65 | /// `av_lfg_get`: `state[i] = state[i-24] + state[i-55]`, returning the new word. |
| 73 | - fn next_word(&mut self) -> u32 { | 66 | + pub fn next_word(&mut self) -> u32 { |
| 74 | let i = self.index & 63; | 67 | let i = self.index & 63; |
| 75 | let v = self.state[(i + 40) & 63].wrapping_add(self.state[(i + 9) & 63]); | 68 | let v = self.state[(i + 40) & 63].wrapping_add(self.state[(i + 9) & 63]); |
| 76 | self.state[i] = v; | 69 | self.state[i] = v; |