diff --git a/crates/ec-opus/Cargo.toml b/crates/ec-opus/Cargo.toml index efd566cd..c6b36b12 100644 --- a/crates/ec-opus/Cargo.toml +++ b/crates/ec-opus/Cargo.toml @@ -9,3 +9,6 @@ authors.workspace = true [dependencies] ec-core.workspace = true ec-dsp.workspace = true + +[dev-dependencies] +ec-dsp.workspace = true diff --git a/crates/ec-opus/src/celt.rs b/crates/ec-opus/src/celt.rs new file mode 100644 index 00000000..74fece79 --- /dev/null +++ b/crates/ec-opus/src/celt.rs @@ -0,0 +1,2687 @@ +//! The CELT (MDCT) layer of Opus — RFC 6716 Section 4.3. +//! +//! CELT codes a frame as a per-band energy envelope plus a unit-norm shape per +//! band, the shape being a PVQ codeword. Decoding is therefore: coarse and fine +//! energy, an implicit bit allocation that both sides must derive identically, +//! PVQ shapes with spreading/folding, denormalisation by the energy, and an +//! inverse MDCT with overlap-add, post-filter and de-emphasis. +//! +//! Two notes on where the numbers come from. RFC 6716 Section 6 makes the +//! reference decoder in its Appendix A normative — "should the description +//! contradict the source code of the reference implementation, the latter shall +//! take precedence" — and Section 4.3 defers several tables to it by name +//! (`e_prob_model`, `cache_caps50`, `tf_select_table`, `LOG2_FRAC_TABLE`, +//! band_allocation). Those constants are reproduced here from that appendix, +//! because a decoder that invents them does not decode Opus. Everything with a +//! closed form (the window, `eband` boundaries, PVQ combinatorics) is computed +//! or written from the prose instead. +//! +//! The MDCT is *not* a DFT: sizes here are 15*2^k (120, 240, 480, 960 +//! coefficients), so the transform runs as a 15-point stage over +//! [`ec_dsp::Fft`] power-of-two sub-transforms — an `N/4`-point complex FFT +//! exactly as the reference does it, never an O(N^2) direct evaluation. + +use ec_core::{Error, Result}; +use ec_dsp::Fft; + +use crate::range::RangeDecoder; + +/// Bands in the 48 kHz mode. +pub const NB_BANDS: usize = 21; +/// Band boundaries in units of 2.5 ms MDCT bins (Table 55). +const E_BANDS: [usize; NB_BANDS + 1] = [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100, +]; +/// MDCT bins in a 2.5 ms frame; the window overlap is the same length. +const SHORT_MDCT: usize = 120; +/// Samples of overlap between consecutive frames. +const OVERLAP: usize = 120; +/// Longest post-filter period, and the history the comb filter needs. +#[allow(dead_code)] +const MAX_PERIOD: usize = 1024; +/// Shortest post-filter period. +const MIN_PERIOD: usize = 15; +/// Synthesis history kept per channel. +const DECODE_BUFFER: usize = 2048; +/// Allocation resolution: 1/8 bit. +const BITRES: u32 = 3; +/// Most fine-energy bits a band can get. +const MAX_FINE_BITS: i32 = 8; +const FINE_OFFSET: i32 = 21; +const QTHETA_OFFSET: i32 = 4; +const QTHETA_OFFSET_TWOPHASE: i32 = 16; + +/// `logN400`: `log2(N)` per band in 1/8 bits, for the fine-energy offset. +const LOG_N: [i32; NB_BANDS] = [ + 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36, +]; + +/// Mean energy per band, subtracted before quantisation (`eMeans`, in log2). +const E_MEANS: [f32; NB_BANDS] = [ + 6.4375, 6.25, 5.75, 5.3125, 5.0625, 4.8125, 4.5, 4.375, 4.875, 4.6875, 4.5625, 4.4375, 4.875, + 4.625, 4.3125, 4.5, 4.375, 4.625, 4.75, 4.4375, 3.75, +]; + +/// Static allocation table (Table 57), `[quality][band]`, 1/32 bit per bin. +const BAND_ALLOCATION: [[u8; NB_BANDS]; 11] = [ + [0; NB_BANDS], + [ + 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0, + ], + [ + 110, 100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0, + ], + [ + 118, 110, 103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0, + ], + [ + 126, 119, 112, 104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0, + ], + [ + 134, 127, 120, 114, 103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1, + ], + [ + 144, 137, 130, 124, 113, 107, 101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1, + ], + [ + 152, 145, 138, 132, 123, 117, 111, 105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1, + ], + [ + 162, 155, 148, 142, 133, 127, 121, 115, 108, 102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1, + ], + [ + 172, 165, 158, 152, 143, 137, 131, 125, 118, 112, 106, 100, 94, 87, 81, 75, 69, 63, 56, 45, + 20, + ], + [ + 200, 200, 200, 200, 200, 200, 200, 200, 198, 193, 188, 183, 178, 173, 168, 163, 158, 153, + 148, 129, 104, + ], +]; + +/// Laplace parameters for coarse energy, `[LM][intra][2*band]` (`e_prob_model`). +const E_PROB_MODEL: [[[u8; 42]; 2]; 4] = [ + [ + [ + 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 64, 128, 92, 78, 92, 79, + 92, 78, 90, 79, 116, 41, 115, 40, 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, + 177, 11, + ], + [ + 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, 55, 132, 61, 114, 70, + 96, 74, 88, 75, 88, 87, 74, 89, 66, 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, + 78, 50, + ], + ], + [ + [ + 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, 93, 74, 109, 40, 114, 36, 117, + 34, 117, 34, 143, 17, 145, 18, 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, + 9, + ], + [ + 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 73, 91, 78, 89, 86, 80, 92, + 66, 93, 64, 102, 59, 103, 60, 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, + 45, + ], + ], + [ + [ + 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 112, 38, 124, 26, 132, 27, + 136, 19, 140, 20, 155, 14, 159, 16, 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, + 159, 10, + ], + [ + 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, 87, 72, 92, 75, 98, 72, 105, + 58, 107, 54, 115, 52, 114, 55, 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, + 42, + ], + ], + [ + [ + 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, 119, 33, 127, 33, 134, + 34, 139, 21, 147, 23, 152, 20, 158, 25, 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, + 150, 13, 139, 15, + ], + [ + 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 96, 67, 101, 73, 107, 72, + 113, 55, 118, 52, 125, 52, 118, 52, 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, + 33, 77, 40, + ], + ], +]; + +/// Inter-frame prediction gain for coarse energy, per LM (`pred_coef`). +const PRED_COEF: [f32; 4] = [ + 29440.0 / 32768.0, + 26112.0 / 32768.0, + 21248.0 / 32768.0, + 16384.0 / 32768.0, +]; +/// Intra-frame (across bands) prediction gain, per LM (`beta_coef`). +const BETA_COEF: [f32; 4] = [ + 30147.0 / 32768.0, + 22282.0 / 32768.0, + 12124.0 / 32768.0, + 6554.0 / 32768.0, +]; +/// The same for an intra frame. +const BETA_INTRA: f32 = 4915.0 / 32768.0; + +/// TF resolution adjustment, `[LM][4*transient + 2*tf_select + tf_change]` +/// (Tables 60-63). +const TF_SELECT: [[i32; 8]; 4] = [ + [0, -1, 0, -1, 0, -1, 0, -1], + [0, -1, 0, -2, 1, 0, 1, -1], + [0, -2, 0, -3, 2, 0, 1, -1], + [0, -2, 0, -3, 3, 0, 1, -1], +]; + +/// Conservative `log2` in 1/8 bits, used to reserve the intensity parameter. +const LOG2_FRAC: [i32; 24] = [ + 0, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37, +]; + +/// Index into [`CACHE_BITS`] per `[LM+1][band]` (`cache_index50`). +const CACHE_INDEX: [i16; 105] = [ + -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, 82, 82, 123, 164, 200, 222, 0, 0, 0, 0, + 0, 0, 0, 0, 41, 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41, 41, 41, + 41, 41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, 318, 328, 336, 123, 123, 123, + 123, 123, 123, 123, 123, 240, 240, 240, 240, 305, 305, 305, 318, 318, 343, 351, 358, 364, 240, + 240, 240, 240, 240, 240, 240, 240, 305, 305, 305, 305, 343, 343, 343, 351, 351, 370, 376, 382, + 387, +]; + +/// Bits (minus one) to code `k` pulses in each band size (`cache_bits50`). +const CACHE_BITS: [u8; 392] = [ + 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 31, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, + 47, 49, 50, 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, 66, 67, 68, 69, 70, 71, + 71, 40, 20, 33, 41, 48, 53, 57, 61, 64, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92, + 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123, 124, 126, 128, 40, 23, + 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, 97, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126, + 129, 131, 135, 139, 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, + 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, 153, 159, 165, 171, 176, + 180, 185, 189, 192, 199, 205, 211, 216, 220, 225, 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, + 112, 125, 137, 148, 157, 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35, + 63, 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250, 25, 31, 55, 75, + 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 185, 190, 200, 208, 215, 222, 229, 235, + 240, 245, 255, 16, 36, 65, 89, 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, + 11, 41, 74, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 163, 186, 207, + 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, 228, 241, 253, 9, 44, 81, 113, 142, + 168, 192, 214, 235, 255, 7, 49, 90, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7, + 47, 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57, 106, 151, 192, 231, 5, + 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, 224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175, + 224, 4, 67, 127, 182, 234, +]; + +/// Per-band allocation ceiling, `[2*LM + stereo][band]` (`cache_caps50`). +const CACHE_CAPS: [u8; 168] = [ + 224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134, + 61, 37, 224, 224, 224, 224, 224, 224, 224, 224, 240, 240, 240, 240, 207, 207, 207, 198, 198, + 183, 144, 66, 40, 160, 160, 160, 160, 160, 160, 160, 160, 185, 185, 185, 185, 193, 193, 193, + 183, 183, 172, 138, 64, 38, 240, 240, 240, 240, 240, 240, 240, 240, 207, 207, 207, 207, 204, + 204, 204, 193, 193, 180, 143, 66, 40, 185, 185, 185, 185, 185, 185, 185, 185, 193, 193, 193, + 193, 193, 193, 193, 183, 183, 172, 138, 65, 39, 207, 207, 207, 207, 207, 207, 207, 207, 204, + 204, 204, 204, 201, 201, 201, 188, 188, 176, 141, 66, 40, 193, 193, 193, 193, 193, 193, 193, + 193, 193, 193, 193, 193, 194, 194, 194, 184, 184, 173, 139, 65, 39, 204, 204, 204, 204, 204, + 204, 204, 204, 201, 201, 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40, +]; + +/// Hadamard interleave order for 2, 4, 8 and 16 blocks (`ordery_table`). +const ORDERY: [usize; 30] = [ + 1, 0, // + 3, 0, 2, 1, // + 7, 0, 4, 3, 6, 1, 5, 2, // + 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5, +]; + +const TRIM_ICDF: [u8; 11] = [126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0]; +const SPREAD_ICDF: [u8; 4] = [25, 23, 2, 0]; +const TAPSET_ICDF: [u8; 3] = [2, 1, 0]; +const SMALL_ENERGY_ICDF: [u8; 3] = [2, 1, 0]; + +/// Post-filter tap sets (Section 4.3.7.1). +const POSTFILTER_GAINS: [[f32; 3]; 3] = [ + [0.306_640_63, 0.217_041_02, 0.129_638_67], + [0.463_867_2, 0.268_066_4, 0.0], + [0.799_804_7, 0.100_097_66, 0.0], +]; + +/// De-emphasis coefficient, `1/(1 - 0.85 z^-1)` at 48 kHz. +const PREEMPH: f32 = 0.850_006_1; +/// Internal signal scale; the output is divided by this. +const SIG_SCALE: f32 = 32768.0; + +const SPREAD_NONE: usize = 0; +const SPREAD_NORMAL: usize = 2; +const SPREAD_AGGRESSIVE: usize = 3; + +/// `ilog(x)`: index of the highest set bit plus one. +#[inline] +fn ilog(x: u32) -> i32 { + (32 - x.leading_zeros()) as i32 +} + +/// Integer square root, `floor(sqrt(x))`. +fn isqrt32(x: u32) -> u32 { + if x == 0 { + return 0; + } + let mut r = (x as f64).sqrt() as u32; + // Correct the float estimate; never trust it at the boundary. + while r > 0 && (r as u64) * (r as u64) > x as u64 { + r -= 1; + } + while ((r + 1) as u64) * ((r + 1) as u64) <= x as u64 { + r += 1; + } + r +} + +/// The CELT low-overlap window (Section 4.3.7), computed rather than tabulated. +fn overlap_window() -> Vec { + (0..OVERLAP) + .map(|i| { + let inner = (core::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / OVERLAP as f64).sin(); + (core::f64::consts::FRAC_PI_2 * inner * inner).sin() as f32 + }) + .collect() +} + +// --------------------------------------------------------------------------- +// The N/4 FFT and the inverse MDCT +// --------------------------------------------------------------------------- + +/// A complex FFT of size `15 * 2^k`, the only sizes CELT needs. +/// +/// [`ec_dsp::Fft`] covers the `2^k` factor; the factor of 15 is one extra +/// decimation stage, so the whole transform stays `O(n log n)` without a +/// mixed-radix kernel in the DSP crate. +#[derive(Clone, Debug)] +struct Fft15 { + n: usize, + /// Sub-transform of length `n/15`. + sub: Fft, + /// `exp(2 pi i * k1 * n2 / n)` twiddles, split. + tw_re: Vec, + tw_im: Vec, + /// 15-point DFT twiddles. + w15_re: [f32; 15], + w15_im: [f32; 15], + scratch_re: Vec, + scratch_im: Vec, +} + +impl Fft15 { + fn new(n: usize) -> Fft15 { + assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); + let l = n / 15; + let mut tw_re = vec![0.0; n]; + let mut tw_im = vec![0.0; n]; + for k1 in 0..15 { + for n2 in 0..l { + let a = 2.0 * core::f64::consts::PI * (k1 * n2) as f64 / n as f64; + tw_re[k1 * l + n2] = a.cos() as f32; + tw_im[k1 * l + n2] = a.sin() as f32; + } + } + let mut w15_re = [0.0f32; 15]; + let mut w15_im = [0.0f32; 15]; + for (j, (re, im)) in w15_re.iter_mut().zip(w15_im.iter_mut()).enumerate() { + let a = 2.0 * core::f64::consts::PI * j as f64 / 15.0; + *re = a.cos() as f32; + *im = a.sin() as f32; + } + Fft15 { + n, + sub: Fft::new(l), + tw_re, + tw_im, + w15_re, + w15_im, + scratch_re: vec![0.0; n], + scratch_im: vec![0.0; n], + } + } + + /// Unscaled inverse transform: `X[k] = sum x[n] exp(+2 pi i n k / N)`. + fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) { + let n = self.n; + let l = n / 15; + debug_assert_eq!(re.len(), n); + // Stage 1: 15-point DFTs over n1, input stride L. + for n2 in 0..l { + let mut xr = [0.0f32; 15]; + let mut xi = [0.0f32; 15]; + for n1 in 0..15 { + xr[n1] = re[l * n1 + n2]; + xi[n1] = im[l * n1 + n2]; + } + for k1 in 0..15 { + let (mut sr, mut si) = (0.0f32, 0.0f32); + for n1 in 0..15 { + let w = (k1 * n1) % 15; + let (wr, wi) = (self.w15_re[w], self.w15_im[w]); + sr += xr[n1] * wr - xi[n1] * wi; + si += xr[n1] * wi + xi[n1] * wr; + } + // Stage 2 twiddle, folded in here to avoid another pass. + let (tr, ti) = (self.tw_re[k1 * l + n2], self.tw_im[k1 * l + n2]); + self.scratch_re[k1 * l + n2] = sr * tr - si * ti; + self.scratch_im[k1 * l + n2] = sr * ti + si * tr; + } + } + // Stage 3: L-point transforms, one per k1, written out at stride 15. + for k1 in 0..15 { + let rr = &mut self.scratch_re[k1 * l..(k1 + 1) * l]; + let ii = &mut self.scratch_im[k1 * l..(k1 + 1) * l]; + // ec-dsp scales its inverse by 1/L; undo that to keep the + // transform unscaled, which is what the MDCT expects. + self.sub.inverse_split(rr, ii); + for (k2, (&r, &i)) in rr.iter().zip(ii.iter()).enumerate() { + re[k1 + 15 * k2] = r * l as f32; + im[k1 + 15 * k2] = i * l as f32; + } + } + } +} + +/// One inverse-MDCT plan: spectrum length `l = 15 * 2^k`, window `2*l`. +#[derive(Clone, Debug)] +struct ImdctPlan { + /// Spectrum length. + l: usize, + fft: Fft15, + /// `cos(2 pi (i + 1/8) / (2 l))` and its sine, for the pre/post rotation. + rot_re: Vec, + rot_im: Vec, + fre: Vec, + fim: Vec, + out: Vec, +} + +impl ImdctPlan { + fn new(l: usize) -> ImdctPlan { + let n = 2 * l; + let quarter = l / 2; + let mut rot_re = vec![0.0; quarter]; + let mut rot_im = vec![0.0; quarter]; + for i in 0..quarter { + let a = 2.0 * core::f64::consts::PI * (i as f64 + 0.125) / n as f64; + rot_re[i] = a.cos() as f32; + rot_im[i] = a.sin() as f32; + } + ImdctPlan { + l, + fft: Fft15::new(quarter), + rot_re, + rot_im, + fre: vec![0.0; quarter], + fim: vec![0.0; quarter], + out: vec![0.0; 2 * l], + } + } + + /// Transforms `l` coefficients read at `stride` into `l + OVERLAP` windowed + /// time samples, added into `out` starting at `out_off`. + /// + /// The window is the low-overlap one: zero outside a `l + OVERLAP` span, + /// which is why the output is that long and not `2*l`. + fn inverse( + &mut self, + spectrum: &[f32], + stride: usize, + window: &[f32], + out: &mut [f32], + out_off: usize, + ) { + let l = self.l; + let quarter = l / 2; + // Pre-rotation: z[i] = -(x[l-1-2i] + j x[2i]) * exp(j 2pi (i+1/8)/2l). + for i in 0..quarter { + let a = spectrum[(2 * i) * stride]; + let b = spectrum[(l - 1 - 2 * i) * stride]; + let (c, s) = (self.rot_re[i], self.rot_im[i]); + self.fre[i] = -(b * c - a * s); + self.fim[i] = -(b * s + a * c); + } + self.fft.inverse(&mut self.fre, &mut self.fim); + // Post-rotation by the same angles. + for i in 0..quarter { + let (re, im) = (self.fre[i], self.fim[i]); + let (c, s) = (self.rot_re[i], self.rot_im[i]); + self.fre[i] = re * c - im * s; + self.fim[i] = im * c + re * s; + } + // De-shuffle into the time-domain half-window, then mirror for TDAC. + // t[] holds the l samples that the window's two halves fold onto. + let t = &mut self.out[..l]; + for i in 0..quarter { + t[2 * i] = -self.fre[i]; + t[2 * i + 1] = self.fim[quarter - 1 - i]; + } + // out is indexed from `out_off - (l - OVERLAP)/2` in the reference; + // shift it so the first written sample lands on out_off. + // The window is zero outside a span of `l + OVERLAP`, so that is all + // that is written: `flat` samples straight through at each end, and + // `OVERLAP/2` windowed samples mirrored onto the frame's own tails. + let base = out_off; + let flat = quarter - OVERLAP / 2; + let mirror = quarter + OVERLAP / 2; // index of t[quarter-1] in `out` + for i in 0..flat { + out[base + mirror - 1 - i] = t[quarter - 1 - i]; + } + for i in flat..quarter { + let x = t[quarter - 1 - i]; + let w = i - flat; + out[base + w] -= window[w] * x; + out[base + mirror - 1 - i] += window[OVERLAP - 1 - w] * x; + } + for i in 0..flat { + out[base + mirror + i] = t[quarter + i]; + } + for i in flat..quarter { + let x = t[quarter + i]; + let w = i - flat; + out[base + l + OVERLAP - 1 - w] = window[w] * x; + out[base + mirror + i] = window[OVERLAP - 1 - w] * x; + } + } +} + +// --------------------------------------------------------------------------- +// PVQ +// --------------------------------------------------------------------------- + +/// Builds the `U(n, .)` row used to count PVQ codewords, returning `V(n, k)`. +/// +/// `V(n,k) = U(n,k) + U(n,k+1)` with `U` obeying +/// `u[n][k] = u[n-1][k] + u[n][k-1] + u[n-1][k-1]` (Section 4.3.4.2). +fn pvq_urow(n: usize, k: usize, u: &mut [u32]) -> u32 { + u[0] = 0; + u[1] = 1; + for (j, slot) in u.iter_mut().enumerate().take(k + 2).skip(2) { + *slot = (2 * j - 1) as u32; + } + for _ in 2..n { + unext(&mut u[1..k + 2], 1); + } + u[k].wrapping_add(u[k + 1]) +} + +/// Steps a `U` row up one dimension in place. +fn unext(u: &mut [u32], mut ui0: u32) { + for j in 1..u.len() { + let ui1 = u[j].wrapping_add(u[j - 1]).wrapping_add(ui0); + u[j - 1] = ui0; + ui0 = ui1; + } + let last = u.len() - 1; + u[last] = ui0; +} + +/// Steps a `U` row down one dimension in place. +fn uprev(u: &mut [u32], mut ui0: u32) { + for j in 1..u.len() { + let ui1 = u[j].wrapping_sub(u[j - 1]).wrapping_sub(ui0); + u[j - 1] = ui0; + ui0 = ui1; + } + let last = u.len() - 1; + u[last] = ui0; +} + +/// Decodes one PVQ codeword of `k` pulses in `n` dimensions. +fn decode_pulses(dec: &mut RangeDecoder, n: usize, k: usize, y: &mut [i32], u: &mut [u32]) { + let v = pvq_urow(n, k, u); + let mut i = dec.dec_uint(v.max(2)); + let mut k = k; + for slot in y.iter_mut().take(n) { + let p = u[k + 1]; + let neg = i >= p; + if neg { + i -= p; + } + let k0 = k; + let mut p = u[k]; + while p > i { + k -= 1; + p = u[k]; + } + i -= p; + let mag = (k0 - k) as i32; + *slot = if neg { -mag } else { mag }; + uprev(&mut u[..k + 2], 0); + } +} + +// --------------------------------------------------------------------------- +// Decoder +// --------------------------------------------------------------------------- + +/// Where a band's folding source lives: in the running normalised spectrum, or +/// in the scratch copy taken before that spectrum is rewritten in place. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Lowband { + None, + Norm(usize), + Scratch(usize), +} + +impl Lowband { + fn offset(self, n: usize) -> Lowband { + match self { + Lowband::None => Lowband::None, + Lowband::Norm(l) => Lowband::Norm(l + n), + Lowband::Scratch(o) => Lowband::Scratch(o + n), + } + } +} + +/// Everything one band decode needs; a struct because the recursion changes +/// only two or three fields at a time. +#[derive(Clone, Copy, Debug)] +struct BandArgs { + /// Band index. + i: usize, + /// Offset of the band in the coefficient buffer. + x: usize, + /// Offset of the second channel, when this is a stereo band. + y: Option, + /// Coefficients in the band. + n: usize, + /// Bits allocated, in 1/8th bits. + b: i32, + spread: usize, + /// Short blocks the band is split over. + blocks: usize, + intensity: usize, + tf_change: i32, + lowband: Lowband, + /// Recursion depth of the split. + level: i32, + lm: i32, + /// Where to write the normalised band for later folding. + lowband_out: Option, + gain: f32, + /// Which blocks may be folded into, one bit each. + fill: u32, +} + +/// The folding PRNG (`celt_lcg_rand`). +#[inline] +fn celt_lcg_rand(seed: u32) -> u32 { + seed.wrapping_mul(1664525).wrapping_add(1013904223) +} + +/// Start of a band's pulse-cache row. `lm` is `-1` for a twice-split band. +fn cache_index(band: usize, lm: i32) -> usize { + let row = (lm + 1) as usize; + let idx = CACHE_INDEX[row * NB_BANDS + band]; + debug_assert!(idx >= 0, "no pulse cache for band {band} at LM {lm}"); + idx.max(0) as usize +} + +/// A CELT layer decoder, one per Opus stream. +#[derive(Clone, Debug)] +pub struct CeltDecoder { + /// Channels this decoder outputs. + channels: usize, + /// Output decimation: 1 at 48 kHz, 2 at 24 kHz, and so on. + downsample: usize, + window: Vec, + plans: Vec, + /// Synthesis history, `channels * DECODE_BUFFER`. + decode_mem: Vec, + /// MDCT overlap tail, `channels * OVERLAP`. + overlap_mem: Vec, + preemph_mem: [f32; 2], + old_band_e: Vec, + old_log_e: Vec, + old_log_e2: Vec, + background_log_e: Vec, + postfilter_period: usize, + postfilter_period_old: usize, + postfilter_gain: f32, + postfilter_gain_old: f32, + postfilter_tapset: usize, + postfilter_tapset_old: usize, + /// The folding PRNG state, carried between frames. + rng: u32, + // Per-frame scratch, allocated once. + x: Vec, + freq: Vec, + norm: Vec, + lowband_scratch: Vec, + band_e: Vec, + pulses: [i32; NB_BANDS], + fine_quant: [i32; NB_BANDS], + fine_priority: [i32; NB_BANDS], + offsets: [i32; NB_BANDS], + caps: [i32; NB_BANDS], + tf_res: [i32; NB_BANDS], + collapse_masks: [u8; 2 * NB_BANDS], + iy: Vec, + urow: Vec, + mdct_buf: Vec, +} + +impl CeltDecoder { + /// A decoder producing `channels` channels at `48000/downsample` Hz. + pub fn new(channels: usize, downsample: usize) -> CeltDecoder { + assert!((1..=2).contains(&channels)); + let max_n = SHORT_MDCT * 8; + CeltDecoder { + channels, + downsample, + window: overlap_window(), + plans: (0..4).map(|lm| ImdctPlan::new(SHORT_MDCT << lm)).collect(), + decode_mem: vec![0.0; channels * DECODE_BUFFER], + overlap_mem: vec![0.0; channels * OVERLAP], + preemph_mem: [0.0; 2], + old_band_e: vec![0.0; 2 * NB_BANDS], + old_log_e: vec![-28.0; 2 * NB_BANDS], + old_log_e2: vec![-28.0; 2 * NB_BANDS], + background_log_e: vec![-28.0; 2 * NB_BANDS], + postfilter_period: 0, + postfilter_period_old: 0, + postfilter_gain: 0.0, + postfilter_gain_old: 0.0, + postfilter_tapset: 0, + postfilter_tapset_old: 0, + rng: 0, + x: vec![0.0; 2 * max_n], + freq: vec![0.0; 2 * max_n], + norm: vec![0.0; 2 * max_n], + lowband_scratch: vec![0.0; max_n], + band_e: vec![0.0; 2 * NB_BANDS], + pulses: [0; NB_BANDS], + fine_quant: [0; NB_BANDS], + fine_priority: [0; NB_BANDS], + offsets: [0; NB_BANDS], + caps: [0; NB_BANDS], + tf_res: [0; NB_BANDS], + collapse_masks: [0; 2 * NB_BANDS], + iy: vec![0; max_n], + urow: vec![0; 256], + mdct_buf: vec![0.0; max_n + OVERLAP], + } + } + + /// Drops all inter-frame state (after a seek, or on a mode switch). + pub fn reset(&mut self) { + self.decode_mem.fill(0.0); + self.overlap_mem.fill(0.0); + self.preemph_mem = [0.0; 2]; + self.old_band_e.fill(0.0); + self.old_log_e.fill(-28.0); + self.old_log_e2.fill(-28.0); + self.background_log_e.fill(-28.0); + self.postfilter_period = 0; + self.postfilter_period_old = 0; + self.postfilter_gain = 0.0; + self.postfilter_gain_old = 0.0; + self.postfilter_tapset = 0; + self.postfilter_tapset_old = 0; + self.rng = 0; + } + + /// The range decoder state after the last frame — the test vectors' oracle. + pub fn rng(&self) -> u32 { + self.rng + } + + /// Decodes one CELT frame. + /// + /// `frame_size` is in 48 kHz samples; `start`/`end` bound the coded bands + /// (17..21 for the CELT half of a hybrid frame); `stream_channels` is what + /// the bitstream codes, which may be 1 while the decoder outputs 2. + /// `out` receives `channels * frame_size / downsample` interleaved samples. + #[allow(clippy::too_many_arguments)] + pub fn decode( + &mut self, + dec: &mut RangeDecoder, + out: &mut [f32], + frame_size: usize, + start: usize, + end: usize, + stream_channels: usize, + ) -> Result<()> { + let lm = match frame_size { + 120 => 0, + 240 => 1, + 480 => 2, + 960 => 3, + _ => { + return Err(Error::corrupt(format!( + "celt: frame size {frame_size} is not 120, 240, 480 or 960" + ))); + } + }; + let m = 1usize << lm; + let n = m * SHORT_MDCT; + let cc = self.channels; + let c = stream_channels; + let eff_end = end.min(NB_BANDS); + let len = dec.len(); + let total_bits = (len * 8) as i32; + + // A mono stream fed to a stereo decoder inherits the louder history. + if c == 1 { + for i in 0..NB_BANDS { + self.old_band_e[i] = self.old_band_e[i].max(self.old_band_e[NB_BANDS + i]); + } + } + + for ch in 0..c { + self.x[ch * n..ch * n + m * E_BANDS[start]].fill(0.0); + self.x[ch * n + m * E_BANDS[eff_end]..(ch + 1) * n].fill(0.0); + } + + let mut tell = dec.tell() as i32; + let silence = if tell >= total_bits { + true + } else if tell == 1 { + dec.dec_bit_logp(15) + } else { + false + }; + if silence { + // Pretend the frame was fully consumed (Section 4.3). + dec.skip_to_end(); + tell = total_bits; + } + + let mut postfilter_gain = 0.0f32; + let mut postfilter_pitch = 0usize; + let mut postfilter_tapset = 0usize; + if start == 0 && tell + 16 <= total_bits { + if dec.dec_bit_logp(1) { + let octave = dec.dec_uint(6) as usize; + postfilter_pitch = (16 << octave) + dec.dec_bits(4 + octave as u32) as usize - 1; + let qg = dec.dec_bits(3); + if dec.tell() as i32 + 2 <= total_bits { + postfilter_tapset = dec.dec_icdf(&TAPSET_ICDF, 2); + } + postfilter_gain = 0.09375 * (qg + 1) as f32; + } + tell = dec.tell() as i32; + } + + let transient = if lm > 0 && tell + 3 <= total_bits { + let t = dec.dec_bit_logp(3); + tell = dec.tell() as i32; + t + } else { + false + }; + let short_blocks = if transient { m } else { 0 }; + + let intra = if tell + 3 <= total_bits { + dec.dec_bit_logp(3) + } else { + false + }; + self.unquant_coarse_energy(dec, start, end, intra, c, lm, len); + self.tf_decode(dec, start, end, transient, lm, len); + + tell = dec.tell() as i32; + let spread = if tell + 4 <= total_bits { + dec.dec_icdf(&SPREAD_ICDF, 5) + } else { + SPREAD_NORMAL + }; + + // Per-band allocation ceiling. + for i in 0..NB_BANDS { + let bw = (E_BANDS[i + 1] - E_BANDS[i]) << lm; + self.caps[i] = + ((CACHE_CAPS[NB_BANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * bw as i32) + >> 2; + } + + // Band boosts (dynalloc). + let mut dynalloc_logp = 6u32; + let mut total_bits_frac = total_bits << BITRES; + let mut tell_frac = dec.tell_frac() as i32; + self.offsets.fill(0); + for i in start..end { + let width = ((c * (E_BANDS[i + 1] - E_BANDS[i])) << lm) as i32; + let quanta = (width << BITRES).min((6 << BITRES).max(width)); + let mut loop_logp = dynalloc_logp; + let mut boost = 0; + while tell_frac + ((loop_logp as i32) << BITRES) < total_bits_frac + && boost < self.caps[i] + { + let flag = dec.dec_bit_logp(loop_logp); + tell_frac = dec.tell_frac() as i32; + if !flag { + break; + } + boost += quanta; + total_bits_frac -= quanta; + loop_logp = 1; + } + self.offsets[i] = boost; + if boost > 0 { + dynalloc_logp = dynalloc_logp.saturating_sub(1).max(2); + } + } + + let alloc_trim = if tell_frac + (6 << BITRES) <= total_bits_frac { + dec.dec_icdf(&TRIM_ICDF, 7) as i32 + } else { + 5 + }; + + let mut bits = ((len as i32 * 8) << BITRES) - dec.tell_frac() as i32 - 1; + let anti_collapse_rsv = if transient && lm >= 2 && bits >= ((lm as i32 + 2) << BITRES) { + 1 << BITRES + } else { + 0 + }; + bits -= anti_collapse_rsv; + + let mut intensity = 0usize; + let mut dual_stereo = false; + let mut balance = 0i32; + let coded_bands = self.compute_allocation( + dec, + start, + end, + alloc_trim, + &mut intensity, + &mut dual_stereo, + bits, + &mut balance, + c, + lm, + ); + + self.unquant_fine_energy(dec, start, end, c); + + self.quant_all_bands( + dec, + start, + end, + short_blocks, + spread, + dual_stereo, + intensity, + (len as i32 * (8 << BITRES)) - anti_collapse_rsv, + balance, + lm, + coded_bands, + c, + n, + ); + + let anti_collapse_on = if anti_collapse_rsv > 0 { + dec.dec_bits(1) != 0 + } else { + false + }; + + let bits_left = len as i32 * 8 - dec.tell() as i32; + self.unquant_energy_finalise(dec, start, end, bits_left, c); + + if anti_collapse_on { + self.anti_collapse(lm, c, n, start, end); + } + + // log2 energy back to linear amplitude. + for ch in 0..c { + for (i, mean) in E_MEANS.iter().enumerate() { + self.band_e[ch * NB_BANDS + i] = if i < start || i >= end { + 0.0 + } else { + ((self.old_band_e[ch * NB_BANDS + i] + mean) as f64).exp2() as f32 + }; + } + } + if silence { + self.band_e.fill(0.0); + self.old_band_e.fill(-28.0); + } + + self.denormalise_bands(eff_end, c, m, n); + + // Slide the synthesis history and run the inverse MDCT into it. + for ch in 0..cc { + let base = ch * DECODE_BUFFER; + self.decode_mem + .copy_within(base + n..base + DECODE_BUFFER, base); + } + for ch in 0..c { + self.freq[ch * n..ch * n + m * E_BANDS[start]].fill(0.0); + let bound = (m * E_BANDS[eff_end]).min(n / self.downsample); + self.freq[ch * n + bound..(ch + 1) * n].fill(0.0); + } + if cc == 2 && c == 1 { + let (a, b) = self.freq.split_at_mut(n); + b[..n].copy_from_slice(&a[..n]); + } + if cc == 1 && c == 2 { + for i in 0..n { + self.freq[i] = 0.5 * (self.freq[i] + self.freq[n + i]); + } + } + self.inverse_mdcts(short_blocks, lm, n, cc); + + // Post-filter, then de-emphasis into the caller's buffer. + self.postfilter_period = self.postfilter_period.max(MIN_PERIOD); + self.postfilter_period_old = self.postfilter_period_old.max(MIN_PERIOD); + for ch in 0..cc { + let base = ch * DECODE_BUFFER + DECODE_BUFFER - n; + self.comb_filter( + base, + SHORT_MDCT.min(n), + self.postfilter_period_old, + self.postfilter_period, + self.postfilter_gain_old, + self.postfilter_gain, + self.postfilter_tapset_old, + self.postfilter_tapset, + true, + ); + if lm != 0 { + self.comb_filter( + base + SHORT_MDCT, + n - SHORT_MDCT, + self.postfilter_period, + postfilter_pitch.max(MIN_PERIOD), + self.postfilter_gain, + postfilter_gain, + self.postfilter_tapset, + postfilter_tapset, + true, + ); + } + } + self.postfilter_period_old = self.postfilter_period; + self.postfilter_gain_old = self.postfilter_gain; + self.postfilter_tapset_old = self.postfilter_tapset; + self.postfilter_period = postfilter_pitch; + self.postfilter_gain = postfilter_gain; + self.postfilter_tapset = postfilter_tapset; + if lm != 0 { + self.postfilter_period_old = self.postfilter_period; + self.postfilter_gain_old = self.postfilter_gain; + self.postfilter_tapset_old = self.postfilter_tapset; + } + + // Energy history for the next frame's prediction and anti-collapse. + if c == 1 { + for i in 0..NB_BANDS { + self.old_band_e[NB_BANDS + i] = self.old_band_e[i]; + } + } + if !transient { + self.old_log_e2.copy_from_slice(&self.old_log_e); + self.old_log_e.copy_from_slice(&self.old_band_e); + for i in 0..2 * NB_BANDS { + self.background_log_e[i] = + (self.background_log_e[i] + m as f32 * 0.001).min(self.old_band_e[i]); + } + } else { + for i in 0..2 * NB_BANDS { + self.old_log_e[i] = self.old_log_e[i].min(self.old_band_e[i]); + } + } + for ch in 0..2 { + for i in (0..start).chain(end..NB_BANDS) { + self.old_band_e[ch * NB_BANDS + i] = 0.0; + self.old_log_e[ch * NB_BANDS + i] = -28.0; + self.old_log_e2[ch * NB_BANDS + i] = -28.0; + } + } + self.rng = dec.range(); + + self.deemphasis(out, n, cc); + Ok(()) + } + + #[allow(clippy::too_many_arguments)] + fn unquant_coarse_energy( + &mut self, + dec: &mut RangeDecoder, + start: usize, + end: usize, + intra: bool, + c: usize, + lm: usize, + len: usize, + ) { + let model = &E_PROB_MODEL[lm][usize::from(intra)]; + let (coef, beta) = if intra { + (0.0, BETA_INTRA) + } else { + (PRED_COEF[lm], BETA_COEF[lm]) + }; + let budget = (len * 8) as i32; + let mut prev = [0.0f32; 2]; + for i in start..end { + for (ch, prev) in prev.iter_mut().enumerate().take(c) { + let tell = dec.tell() as i32; + let qi = if budget - tell >= 15 { + let pi = 2 * i.min(20); + laplace_decode(dec, (model[pi] as u32) << 7, (model[pi + 1] as i32) << 6) + } else if budget - tell >= 2 { + let qi = dec.dec_icdf(&SMALL_ENERGY_ICDF, 2) as i32; + (qi >> 1) ^ -(qi & 1) + } else if budget - tell >= 1 { + -i32::from(dec.dec_bit_logp(1)) + } else { + -1 + }; + let q = qi as f32; + let old = &mut self.old_band_e[i + ch * NB_BANDS]; + *old = old.max(-9.0); + *old = coef * *old + *prev + q; + *prev += q - beta * q; + } + } + } + + fn unquant_fine_energy(&mut self, dec: &mut RangeDecoder, start: usize, end: usize, c: usize) { + for i in start..end { + if self.fine_quant[i] <= 0 { + continue; + } + for ch in 0..c { + let q2 = dec.dec_bits(self.fine_quant[i] as u32); + let offset = (q2 as f32 + 0.5) / (1 << self.fine_quant[i]) as f32 - 0.5; + self.old_band_e[i + ch * NB_BANDS] += offset; + } + } + } + + fn unquant_energy_finalise( + &mut self, + dec: &mut RangeDecoder, + start: usize, + end: usize, + mut bits_left: i32, + c: usize, + ) { + for prio in 0..2 { + for i in start..end { + if bits_left < c as i32 { + break; + } + if self.fine_quant[i] >= MAX_FINE_BITS || self.fine_priority[i] != prio { + continue; + } + for ch in 0..c { + let q2 = dec.dec_bits(1); + let offset = (q2 as f32 - 0.5) / (1 << (self.fine_quant[i] + 1)) as f32; + self.old_band_e[i + ch * NB_BANDS] += offset; + bits_left -= 1; + } + } + } + } + + #[allow(clippy::too_many_arguments)] + fn tf_decode( + &mut self, + dec: &mut RangeDecoder, + start: usize, + end: usize, + transient: bool, + lm: usize, + len: usize, + ) { + let mut budget = (len * 8) as i32; + let mut tell = dec.tell() as i32; + let mut logp: u32 = if transient { 2 } else { 4 }; + let tf_select_rsv = lm > 0 && tell + (logp as i32) < budget; + budget -= i32::from(tf_select_rsv); + let mut curr = 0i32; + let mut changed = false; + for i in start..end { + if tell + logp as i32 <= budget { + curr ^= i32::from(dec.dec_bit_logp(logp)); + tell = dec.tell() as i32; + changed |= curr != 0; + } + self.tf_res[i] = curr; + logp = if transient { 4 } else { 5 }; + } + let t = usize::from(transient); + let ch = usize::from(changed); + let mut tf_select = 0usize; + if tf_select_rsv && TF_SELECT[lm][4 * t + ch] != TF_SELECT[lm][4 * t + 2 + ch] { + tf_select = usize::from(dec.dec_bit_logp(1)); + } + for i in start..end { + self.tf_res[i] = TF_SELECT[lm][4 * t + 2 * tf_select + self.tf_res[i] as usize]; + } + } + + #[allow(clippy::too_many_arguments)] + fn compute_allocation( + &mut self, + dec: &mut RangeDecoder, + start: usize, + end: usize, + alloc_trim: i32, + intensity: &mut usize, + dual_stereo: &mut bool, + total: i32, + balance: &mut i32, + c: usize, + lm: usize, + ) -> usize { + let mut total = total.max(0); + let mut skip_start = start; + let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; + total -= skip_rsv; + let mut intensity_rsv = 0; + let mut dual_stereo_rsv = 0; + if c == 2 { + intensity_rsv = LOG2_FRAC[end - start]; + if intensity_rsv > total { + intensity_rsv = 0; + } else { + total -= intensity_rsv; + dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; + total -= dual_stereo_rsv; + } + } + + let mut thresh = [0i32; NB_BANDS]; + let mut trim_offset = [0i32; NB_BANDS]; + for j in start..end { + let width = (E_BANDS[j + 1] - E_BANDS[j]) as i32; + thresh[j] = ((c as i32) << BITRES).max((((3 * width) << lm) << BITRES) >> 4); + trim_offset[j] = (c as i32 + * width + * (alloc_trim - 5 - lm as i32) + * (end - j - 1) as i32 + * (1 << (lm as u32 + BITRES))) + >> 6; + if (width << lm) == 1 { + trim_offset[j] -= (c as i32) << BITRES; + } + } + + // Bisect the static allocation table for the highest quality that fits. + let mut lo = 1i32; + let mut hi = BAND_ALLOCATION.len() as i32 - 1; + while lo <= hi { + let mid = (lo + hi) >> 1; + let mut psum = 0; + let mut done = false; + for j in (start..end).rev() { + let width = (E_BANDS[j + 1] - E_BANDS[j]) as i32; + let mut bitsj = + (c as i32 * width * BAND_ALLOCATION[mid as usize][j] as i32) << lm >> 2; + if bitsj > 0 { + bitsj = 0.max(bitsj + trim_offset[j]); + } + bitsj += self.offsets[j]; + if bitsj >= thresh[j] || done { + done = true; + psum += bitsj.min(self.caps[j]); + } else if bitsj >= (c as i32) << BITRES { + psum += (c as i32) << BITRES; + } + } + if psum > total { + hi = mid - 1; + } else { + lo = mid + 1; + } + } + let hi = lo; + let lo = lo - 1; + + let mut bits1 = [0i32; NB_BANDS]; + let mut bits2 = [0i32; NB_BANDS]; + for j in start..end { + let width = (E_BANDS[j + 1] - E_BANDS[j]) as i32; + let mut b1 = (c as i32 * width * BAND_ALLOCATION[lo as usize][j] as i32) << lm >> 2; + let mut b2 = if hi as usize >= BAND_ALLOCATION.len() { + self.caps[j] + } else { + (c as i32 * width * BAND_ALLOCATION[hi as usize][j] as i32) << lm >> 2 + }; + if b1 > 0 { + b1 = 0.max(b1 + trim_offset[j]); + } + if b2 > 0 { + b2 = 0.max(b2 + trim_offset[j]); + } + if lo > 0 { + b1 += self.offsets[j]; + } + b2 += self.offsets[j]; + if self.offsets[j] > 0 { + skip_start = j; + } + bits1[j] = b1; + bits2[j] = 0.max(b2 - b1); + } + + self.interp_bits2pulses( + dec, + start, + end, + skip_start, + &bits1, + &bits2, + &thresh, + total, + balance, + skip_rsv, + intensity, + intensity_rsv, + dual_stereo, + dual_stereo_rsv, + c, + lm, + ) + } + + #[allow(clippy::too_many_arguments)] + fn interp_bits2pulses( + &mut self, + dec: &mut RangeDecoder, + start: usize, + end: usize, + skip_start: usize, + bits1: &[i32; NB_BANDS], + bits2: &[i32; NB_BANDS], + thresh: &[i32; NB_BANDS], + mut total: i32, + balance_out: &mut i32, + skip_rsv: i32, + intensity: &mut usize, + mut intensity_rsv: i32, + dual_stereo: &mut bool, + mut dual_stereo_rsv: i32, + c: usize, + lm: usize, + ) -> usize { + const ALLOC_STEPS: u32 = 6; + let alloc_floor = (c as i32) << BITRES; + let stereo = c > 1; + let log_m = (lm as i32) << BITRES; + + let mut lo = 0i32; + let mut hi = 1i32 << ALLOC_STEPS; + for _ in 0..ALLOC_STEPS { + let mid = (lo + hi) >> 1; + let mut psum = 0; + let mut done = false; + for j in (start..end).rev() { + let tmp = bits1[j] + ((mid * bits2[j]) >> ALLOC_STEPS); + if tmp >= thresh[j] || done { + done = true; + psum += tmp.min(self.caps[j]); + } else if tmp >= alloc_floor { + psum += alloc_floor; + } + } + if psum > total { + hi = mid; + } else { + lo = mid; + } + } + + let mut psum = 0; + let mut done = false; + let bits = &mut self.pulses; + for j in (start..end).rev() { + let mut tmp = bits1[j] + ((lo * bits2[j]) >> ALLOC_STEPS); + if tmp < thresh[j] && !done { + tmp = if tmp >= alloc_floor { alloc_floor } else { 0 }; + } else { + done = true; + } + tmp = tmp.min(self.caps[j]); + bits[j] = tmp; + psum += tmp; + } + + // Skip decoding, working back from the top band. + let mut coded_bands = end; + loop { + let j = coded_bands - 1; + if j <= skip_start { + total += skip_rsv; + break; + } + let mut left = total - psum; + let percoeff = left / (E_BANDS[coded_bands] - E_BANDS[start]) as i32; + left -= (E_BANDS[coded_bands] - E_BANDS[start]) as i32 * percoeff; + let rem = 0.max(left - (E_BANDS[j] - E_BANDS[start]) as i32); + let band_width = (E_BANDS[coded_bands] - E_BANDS[j]) as i32; + let mut band_bits = self.pulses[j] + percoeff * band_width + rem; + if band_bits >= thresh[j].max(alloc_floor + (1 << BITRES)) { + if dec.dec_bit_logp(1) { + break; + } + psum += 1 << BITRES; + band_bits -= 1 << BITRES; + } + psum -= self.pulses[j] + intensity_rsv; + if intensity_rsv > 0 { + intensity_rsv = LOG2_FRAC[j - start]; + } + psum += intensity_rsv; + if band_bits >= alloc_floor { + psum += alloc_floor; + self.pulses[j] = alloc_floor; + } else { + self.pulses[j] = 0; + } + coded_bands -= 1; + } + + if intensity_rsv > 0 { + *intensity = start + dec.dec_uint((coded_bands + 1 - start) as u32) as usize; + } else { + *intensity = 0; + } + if *intensity <= start { + total += dual_stereo_rsv; + dual_stereo_rsv = 0; + } + *dual_stereo = if dual_stereo_rsv > 0 { + dec.dec_bit_logp(1) + } else { + false + }; + + // Hand out what is left, then split each band into shape and fine bits. + let mut left = total - psum; + let percoeff = left / (E_BANDS[coded_bands] - E_BANDS[start]) as i32; + left -= (E_BANDS[coded_bands] - E_BANDS[start]) as i32 * percoeff; + for j in start..coded_bands { + self.pulses[j] += percoeff * (E_BANDS[j + 1] - E_BANDS[j]) as i32; + } + for j in start..coded_bands { + let tmp = left.min((E_BANDS[j + 1] - E_BANDS[j]) as i32); + self.pulses[j] += tmp; + left -= tmp; + } + + let mut balance = 0i32; + for j in start..coded_bands { + let n0 = (E_BANDS[j + 1] - E_BANDS[j]) as i32; + let n = n0 << lm; + self.pulses[j] += balance; + let mut excess; + if n > 1 { + excess = 0.max(self.pulses[j] - self.caps[j]); + self.pulses[j] -= excess; + let den = + c as i32 * n + i32::from(c == 2 && n > 2 && !*dual_stereo && j < *intensity); + let nc_log_n = den * (LOG_N[j] + log_m); + let mut offset = (nc_log_n >> 1) - den * FINE_OFFSET; + if n == 2 { + offset += den << BITRES >> 2; + } + if self.pulses[j] + offset < (den * 2) << BITRES { + offset += nc_log_n >> 2; + } else if self.pulses[j] + offset < (den * 3) << BITRES { + offset += nc_log_n >> 3; + } + let mut eb = + 0.max((self.pulses[j] + offset + (den << (BITRES - 1))) / (den << BITRES)); + if c as i32 * eb > (self.pulses[j] >> BITRES) { + eb = self.pulses[j] >> u32::from(stereo) >> BITRES; + } + eb = eb.min(MAX_FINE_BITS); + self.fine_quant[j] = eb; + self.fine_priority[j] = i32::from(eb * (den << BITRES) >= self.pulses[j] + offset); + self.pulses[j] -= (c as i32 * eb) << BITRES; + } else { + excess = 0.max(self.pulses[j] - ((c as i32) << BITRES)); + self.pulses[j] -= excess; + self.fine_quant[j] = 0; + self.fine_priority[j] = 1; + } + if excess > 0 { + let extra_fine = (excess >> (u32::from(stereo) + BITRES)) + .min(MAX_FINE_BITS - self.fine_quant[j]); + self.fine_quant[j] += extra_fine; + let extra_bits = (extra_fine * c as i32) << BITRES; + self.fine_priority[j] = i32::from(extra_bits >= excess - balance); + excess -= extra_bits; + } + balance = excess; + } + *balance_out = balance; + for j in coded_bands..end { + self.fine_quant[j] = self.pulses[j] >> u32::from(stereo) >> BITRES; + self.pulses[j] = 0; + self.fine_priority[j] = i32::from(self.fine_quant[j] < 1); + } + coded_bands + } + + #[allow(clippy::too_many_arguments)] + fn quant_all_bands( + &mut self, + dec: &mut RangeDecoder, + start: usize, + end: usize, + short_blocks: usize, + spread: usize, + mut dual_stereo: bool, + intensity: usize, + total_bits: i32, + mut balance: i32, + lm: usize, + coded_bands: usize, + c: usize, + n: usize, + ) { + let m = 1usize << lm; + let blocks = if short_blocks != 0 { m } else { 1 }; + let norm_len = m * E_BANDS[NB_BANDS]; + let mut lowband_offset = 0usize; + let mut update_lowband = true; + self.collapse_masks.fill(0); + + for i in start..end { + let n_band = m * E_BANDS[i + 1] - m * E_BANDS[i]; + let tell = dec.tell_frac() as i32; + if i != start { + balance -= tell; + } + let mut remaining_bits = total_bits - tell - 1; + let b = if i < coded_bands { + let curr_balance = balance / 3.min(coded_bands - i) as i32; + 0.max(16383.min((remaining_bits + 1).min(self.pulses[i] + curr_balance))) + } else { + 0 + }; + + if m * E_BANDS[i] >= m * E_BANDS[start] + n_band + && (update_lowband || lowband_offset == 0) + { + lowband_offset = i; + } + + let tf_change = self.tf_res[i]; + // Which already-decoded bands this one may fold from, and whether + // they had any pulses at all (the collapse mask). + let (mut x_cm, mut y_cm); + let mut effective_lowband: Option = None; + if lowband_offset != 0 && (spread != SPREAD_AGGRESSIVE || blocks > 1 || tf_change < 0) { + let low = + (m * E_BANDS[start]).max((m * E_BANDS[lowband_offset]).saturating_sub(n_band)); + effective_lowband = Some(low); + let mut fold_start = lowband_offset; + loop { + fold_start -= 1; + if m * E_BANDS[fold_start] <= low { + break; + } + } + let mut fold_end = lowband_offset - 1; + loop { + fold_end += 1; + if m * E_BANDS[fold_end] >= low + n_band { + break; + } + } + x_cm = 0; + y_cm = 0; + let mut fi = fold_start; + loop { + x_cm |= self.collapse_masks[fi * c] as u32; + y_cm |= self.collapse_masks[fi * c + c - 1] as u32; + fi += 1; + if fi >= fold_end { + break; + } + } + } else { + x_cm = (1u32 << blocks) - 1; + y_cm = x_cm; + } + + if dual_stereo && i == intensity { + // Switch off dual stereo to do intensity from here up. + dual_stereo = false; + for j in m * E_BANDS[start]..m * E_BANDS[i] { + self.norm[j] = 0.5 * (self.norm[j] + self.norm[norm_len + j]); + } + } + + let x_off = m * E_BANDS[i]; + let base = BandArgs { + i, + x: x_off, + y: None, + n: n_band, + b, + spread, + blocks, + intensity, + tf_change, + lowband: Lowband::None, + level: 0, + lm: lm as i32, + lowband_out: Some(x_off), + gain: 1.0, + fill: 0, + }; + if dual_stereo { + x_cm = self.quant_band( + dec, + BandArgs { + b: b / 2, + lowband: effective_lowband.map_or(Lowband::None, Lowband::Norm), + fill: x_cm, + ..base + }, + &mut remaining_bits, + ); + y_cm = self.quant_band( + dec, + BandArgs { + x: n + x_off, + b: b / 2, + lowband: effective_lowband + .map_or(Lowband::None, |l| Lowband::Norm(norm_len + l)), + lowband_out: Some(norm_len + x_off), + fill: y_cm, + ..base + }, + &mut remaining_bits, + ); + } else { + x_cm = self.quant_band( + dec, + BandArgs { + y: if c == 2 { Some(n + x_off) } else { None }, + lowband: effective_lowband.map_or(Lowband::None, Lowband::Norm), + fill: x_cm | y_cm, + ..base + }, + &mut remaining_bits, + ); + y_cm = x_cm; + } + self.collapse_masks[i * c] = x_cm as u8; + self.collapse_masks[i * c + c - 1] = y_cm as u8; + balance += self.pulses[i] + tell; + // Keep folding from this band only while it has a bit per sample. + update_lowband = b > (n_band as i32) << BITRES; + } + } + + /// Decodes one band (Section 4.3.4), recursing for splits and for stereo. + fn quant_band(&mut self, dec: &mut RangeDecoder, a: BandArgs, remaining_bits: &mut i32) -> u32 { + let BandArgs { + i, + x, + mut y, + mut n, + mut b, + spread, + mut blocks, + intensity, + mut tf_change, + mut lowband, + level, + mut lm, + lowband_out, + gain, + mut fill, + } = a; + let n0 = n; + let mut n_b = n; + let stereo = y.is_some(); + let mut split = stereo; + let mut inv = false; + let mut cm: u32 = 0; + let mut time_divide = 0; + let mut recombine = 0usize; + let long_blocks = blocks == 1; + + n_b /= blocks; + let mut n_b0 = n_b; + let mut b0 = blocks; + + if n == 1 { + // A single coefficient carries nothing but its sign. + for offs in [Some(x), y].into_iter().flatten() { + let mut sign = false; + if *remaining_bits >= 1 << BITRES { + sign = dec.dec_bits(1) != 0; + *remaining_bits -= 1 << BITRES; + } + self.x[offs] = if sign { -1.0 } else { 1.0 }; + } + if let Some(o) = lowband_out { + self.norm[o] = self.x[x]; + } + return 1; + } + + if !stereo && level == 0 { + if tf_change > 0 { + recombine = tf_change as usize; + } + // The folding source is about to be rewritten in place, so take a + // copy first — the band it belongs to is still needed as decoded. + if lowband != Lowband::None + && (recombine > 0 || (n_b & 1) == 0 && tf_change < 0 || b0 > 1) + { + for j in 0..n { + self.lowband_scratch[j] = self.lowband(lowband, j); + } + lowband = Lowband::Scratch(0); + } + for k in 0..recombine { + const BIT_INTERLEAVE: [u8; 16] = [0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3]; + haar1(&mut self.x[x..], n >> k, 1 << k); + if let Some(l) = self.lowband_mut(lowband) { + haar1(l, n >> k, 1 << k); + } + fill = BIT_INTERLEAVE[(fill & 0xF) as usize] as u32 + | (BIT_INTERLEAVE[(fill >> 4) as usize] as u32) << 2; + } + blocks >>= recombine; + n_b <<= recombine; + // Increasing the time resolution. + while (n_b & 1) == 0 && tf_change < 0 { + haar1(&mut self.x[x..], n_b, blocks); + if let Some(l) = self.lowband_mut(lowband) { + haar1(l, n_b, blocks); + } + fill |= fill << blocks; + blocks <<= 1; + n_b >>= 1; + time_divide += 1; + tf_change += 1; + } + b0 = blocks; + n_b0 = n_b; + // Reorder the samples in time order instead of frequency order. + if b0 > 1 { + deinterleave_hadamard( + &mut self.x[x..x + n], + n_b >> recombine, + b0 << recombine, + long_blocks, + ); + if let Some(l) = self.lowband_mut(lowband) { + deinterleave_hadamard( + &mut l[..n], + n_b >> recombine, + b0 << recombine, + long_blocks, + ); + } + } + } + + // Split the band when one codebook would need more than 32 bits. + let cache_start = cache_index(i, lm); + let cache0 = CACHE_BITS[cache_start] as usize; + if !stereo && lm != -1 && b > CACHE_BITS[cache_start + cache0] as i32 + 12 && n > 2 { + n >>= 1; + y = Some(x + n); + split = true; + lm -= 1; + if blocks == 1 { + fill = (fill & 1) | (fill << 1); + } + blocks = (blocks + 1) >> 1; + } + + let mut mid = 0.0f32; + if split { + let pulse_cap = LOG_N[i] + lm * (1 << BITRES); + let offset = (pulse_cap >> 1) + - if stereo && n == 2 { + QTHETA_OFFSET_TWOPHASE + } else { + QTHETA_OFFSET + }; + let mut qn = compute_qn(n as i32, b, offset, pulse_cap, stereo); + if stereo && i >= intensity { + qn = 1; + } + let tell = dec.tell_frac() as i32; + let mut itheta: i32 = 0; + if qn != 1 { + if stereo && n > 2 { + // A step pdf: probability 3 up to itheta=qn/2, then 1. + let p0 = 3i32; + let x0 = qn / 2; + let ft = p0 * (x0 + 1) + x0; + let fs = dec.decode(ft as u32) as i32; + let xv = if fs < (x0 + 1) * p0 { + fs / p0 + } else { + x0 + 1 + (fs - (x0 + 1) * p0) + }; + let (fl, fh) = if xv <= x0 { + (p0 * xv, p0 * (xv + 1)) + } else { + ((xv - 1 - x0) + (x0 + 1) * p0, (xv - x0) + (x0 + 1) * p0) + }; + dec.update(fl as u32, fh as u32, ft as u32); + itheta = xv; + } else if b0 > 1 || stereo { + itheta = dec.dec_uint((qn + 1) as u32) as i32; + } else { + // Triangular pdf. + let ft = ((qn >> 1) + 1) * ((qn >> 1) + 1); + let fm = dec.decode(ft as u32) as i32; + let (fl, fs); + if fm < (((qn >> 1) * ((qn >> 1) + 1)) >> 1) { + itheta = ((isqrt32(8 * fm as u32 + 1) - 1) >> 1) as i32; + fs = itheta + 1; + fl = (itheta * (itheta + 1)) >> 1; + } else { + itheta = (2 * (qn + 1) - isqrt32(8 * (ft - fm - 1) as u32 + 1) as i32) >> 1; + fs = qn + 1 - itheta; + fl = ft - (((qn + 1 - itheta) * (qn + 2 - itheta)) >> 1); + } + dec.update(fl as u32, (fl + fs) as u32, ft as u32); + } + itheta = (itheta * 16384) / qn; + } else if stereo { + inv = if b > 2 << BITRES && *remaining_bits > 2 << BITRES { + dec.dec_bit_logp(2) + } else { + false + }; + itheta = 0; + } + let qalloc = dec.tell_frac() as i32 - tell; + b -= qalloc; + + let orig_fill = fill; + let (imid, iside, mut delta); + if itheta == 0 { + imid = 32767; + iside = 0; + fill &= (1 << blocks) - 1; + delta = -16384; + } else if itheta == 16384 { + imid = 0; + iside = 32767; + fill &= ((1u32 << blocks) - 1) << blocks; + delta = 16384; + } else { + imid = bitexact_cos(itheta as i16) as i32; + iside = bitexact_cos((16384 - itheta) as i16) as i32; + // The mid/side split that minimises the squared error. + delta = frac_mul16( + ((n as i32 - 1) << 7) as i16, + bitexact_log2tan(iside, imid) as i16, + ); + } + mid = imid as f32 / 32768.0; + let side = iside as f32 / 32768.0; + + if n == 2 && stereo { + // With two coefficients the side needs only a sign. + let mut mbits = b; + let mut sbits = 0; + if itheta != 0 && itheta != 16384 { + sbits = 1 << BITRES; + } + mbits -= sbits; + let yy = y.unwrap(); + let swap = itheta > 8192; + *remaining_bits -= qalloc + sbits; + let (x2, y2) = if swap { (yy, x) } else { (x, yy) }; + let mut sign = 0i32; + if sbits != 0 { + sign = dec.dec_bits(1) as i32; + } + let sign = 1 - 2 * sign; + cm = self.quant_band( + dec, + BandArgs { + x: x2, + y: None, + n, + b: mbits, + blocks, + tf_change, + lowband, + lm, + fill: orig_fill, + ..a + }, + remaining_bits, + ); + self.x[y2] = -(sign as f32) * self.x[x2 + 1]; + self.x[y2 + 1] = sign as f32 * self.x[x2]; + let x0 = self.x[x] * mid; + let x1 = self.x[x + 1] * mid; + let y0 = self.x[yy] * side; + let y1 = self.x[yy + 1] * side; + self.x[x] = x0 - y0; + self.x[yy] = x0 + y0; + self.x[x + 1] = x1 - y1; + self.x[yy + 1] = x1 + y1; + } else { + // "Normal" split. + if b0 > 1 && !stereo && (itheta & 0x3fff) != 0 { + if itheta > 8192 { + // Rough approximation for pre-echo masking. + delta -= delta >> (4 - lm); + } else { + delta = 0.min(delta + ((n as i32) << BITRES >> (5 - lm))); + } + } + let mut mbits = 0.max(b.min((b - delta) / 2)); + let mut sbits = b - mbits; + *remaining_bits -= qalloc; + let next_lowband2 = if lowband != Lowband::None && !stereo { + lowband.offset(n) + } else { + Lowband::None + }; + let next_lowband_out1 = if stereo { lowband_out } else { None }; + let next_level = if stereo { level } else { level + 1 }; + let mid_args = BandArgs { + x, + y: None, + n, + blocks, + tf_change, + lowband, + level: next_level, + lm, + lowband_out: next_lowband_out1, + gain: if stereo { 1.0 } else { gain * mid }, + fill, + ..a + }; + let side_args = BandArgs { + x: y.unwrap(), + y: None, + n, + blocks, + tf_change, + lowband: next_lowband2, + level: next_level, + lm, + lowband_out: None, + gain: gain * side, + fill: fill >> blocks, + ..a + }; + // A mono split interleaves the two halves' collapse masks; a + // stereo split keeps the side's mask in place. + let shift = if stereo { 0 } else { b0 >> 1 }; + let mut rebalance = *remaining_bits; + if mbits >= sbits { + cm = self.quant_band( + dec, + BandArgs { + b: mbits, + ..mid_args + }, + remaining_bits, + ); + rebalance = mbits - (rebalance - *remaining_bits); + if rebalance > 3 << BITRES && itheta != 0 { + sbits += rebalance - (3 << BITRES); + } + cm |= self.quant_band( + dec, + BandArgs { + b: sbits, + ..side_args + }, + remaining_bits, + ) << shift; + } else { + cm = self.quant_band( + dec, + BandArgs { + b: sbits, + ..side_args + }, + remaining_bits, + ) << shift; + rebalance = sbits - (rebalance - *remaining_bits); + if rebalance > 3 << BITRES && itheta != 16384 { + mbits += rebalance - (3 << BITRES); + } + cm |= self.quant_band( + dec, + BandArgs { + b: mbits, + ..mid_args + }, + remaining_bits, + ); + } + } + } else { + // No split: straight PVQ, or folding when there are no pulses. + let mut q = bits2pulses(i, lm, b); + let mut curr_bits = pulses2bits(i, lm, q); + *remaining_bits -= curr_bits; + // Never bust the budget. + while *remaining_bits < 0 && q > 0 { + *remaining_bits += curr_bits; + q -= 1; + curr_bits = pulses2bits(i, lm, q); + *remaining_bits -= curr_bits; + } + if q != 0 { + let k = get_pulses(q) as usize; + cm = self.alg_unquant(dec, x, n, k, spread, blocks, gain); + } else { + let cm_mask = (1u32 << blocks) - 1; + fill &= cm_mask; + if fill == 0 { + self.x[x..x + n].fill(0.0); + } else if lowband == Lowband::None { + // Noise. + for j in 0..n { + self.rng = celt_lcg_rand(self.rng); + self.x[x + j] = ((self.rng as i32) >> 20) as f32; + } + cm = cm_mask; + renormalise(&mut self.x[x..x + n], gain); + } else { + // Folded spectrum, about 48 dB below the normal level. + for j in 0..n { + self.rng = celt_lcg_rand(self.rng); + let tmp = if self.rng & 0x8000 != 0 { + 1.0 / 256.0 + } else { + -1.0 / 256.0 + }; + self.x[x + j] = self.lowband(lowband, j) + tmp; + } + cm = fill; + renormalise(&mut self.x[x..x + n], gain); + } + } + } + + // Undo the reorganisations, and pass the band on for later folding. + if stereo { + if n != 2 { + stereo_merge(&mut self.x, x, y.unwrap(), mid, n); + } + if inv { + for j in 0..n { + self.x[y.unwrap() + j] = -self.x[y.unwrap() + j]; + } + } + } else if level == 0 { + if b0 > 1 { + interleave_hadamard( + &mut self.x[x..x + n0], + n_b >> recombine, + b0 << recombine, + long_blocks, + ); + } + n_b = n_b0; + let mut bb = b0; + for _ in 0..time_divide { + bb >>= 1; + n_b <<= 1; + cm |= cm >> bb; + haar1(&mut self.x[x..], n_b, bb); + } + for k in 0..recombine { + const BIT_DEINTERLEAVE: [u8; 16] = [ + 0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33, 0x3C, 0x3F, 0xC0, 0xC3, 0xCC, 0xCF, 0xF0, + 0xF3, 0xFC, 0xFF, + ]; + cm = BIT_DEINTERLEAVE[(cm & 0xF) as usize] as u32; + haar1(&mut self.x[x..], n0 >> k, 1 << k); + } + bb <<= recombine; + // Scale for use as a folding source. + if let Some(o) = lowband_out { + let nrm = (n0 as f32).sqrt(); + for j in 0..n0 { + self.norm[o + j] = nrm * self.x[x + j]; + } + } + cm &= (1 << bb) - 1; + } + cm + } + + /// Reads one sample of the folding source. + fn lowband(&self, lb: Lowband, j: usize) -> f32 { + match lb { + Lowband::None => 0.0, + Lowband::Norm(l) => self.norm[l + j], + Lowband::Scratch(o) => self.lowband_scratch[o + j], + } + } + + /// The folding source as a mutable slice, for the in-place Haar passes. + fn lowband_mut(&mut self, lb: Lowband) -> Option<&mut [f32]> { + match lb { + Lowband::None => None, + Lowband::Norm(l) => Some(&mut self.norm[l..]), + Lowband::Scratch(o) => Some(&mut self.lowband_scratch[o..]), + } + } + #[allow(clippy::too_many_arguments)] + fn alg_unquant( + &mut self, + dec: &mut RangeDecoder, + x: usize, + n: usize, + k: usize, + spread: usize, + b_blocks: usize, + gain: f32, + ) -> u32 { + if self.urow.len() < k + 2 { + self.urow.resize(k + 2, 0); + } + decode_pulses(dec, n, k, &mut self.iy, &mut self.urow); + let mut ryy = 0.0f32; + for j in 0..n { + ryy += (self.iy[j] * self.iy[j]) as f32; + } + let g = gain / ryy.sqrt(); + for j in 0..n { + self.x[x + j] = g * self.iy[j] as f32; + } + exp_rotation(&mut self.x[x..x + n], n, -1, b_blocks, k, spread); + // Collapse mask: which of the B short blocks got any pulse. + if b_blocks <= 1 { + return 1; + } + let n0 = n / b_blocks; + let mut mask = 0u32; + for bi in 0..b_blocks { + for j in 0..n0 { + mask |= u32::from(self.iy[bi * n0 + j] != 0) << bi; + } + } + mask + } + + fn anti_collapse(&mut self, lm: usize, c: usize, size: usize, start: usize, end: usize) { + for i in start..end { + let n0 = E_BANDS[i + 1] - E_BANDS[i]; + let depth = (1 + self.pulses[i]) / (n0 << lm) as i32; + let thresh = 0.5 * (-0.125 * depth as f32).exp2(); + let sqrt_1 = 1.0 / ((n0 << lm) as f32).sqrt(); + for ch in 0..c { + let mut prev1 = self.old_log_e[ch * NB_BANDS + i]; + let mut prev2 = self.old_log_e2[ch * NB_BANDS + i]; + if c == 1 { + prev1 = prev1.max(self.old_log_e[NB_BANDS + i]); + prev2 = prev2.max(self.old_log_e2[NB_BANDS + i]); + } + let ediff = (self.old_band_e[ch * NB_BANDS + i] - prev1.min(prev2)).max(0.0); + let mut r = 2.0 * (-ediff).exp2(); + if lm == 3 { + r *= core::f32::consts::SQRT_2; + } + r = r.min(thresh) * sqrt_1; + let base = ch * size + (E_BANDS[i] << lm); + let mut renorm = false; + for k in 0..(1usize << lm) { + if self.collapse_masks[i * c + ch] & (1 << k) == 0 { + for j in 0..n0 { + self.rng = celt_lcg_rand(self.rng); + self.x[base + (j << lm) + k] = + if self.rng & 0x8000 != 0 { r } else { -r }; + } + renorm = true; + } + } + if renorm { + let len = n0 << lm; + renormalise(&mut self.x[base..base + len], 1.0); + } + } + } + } + + fn denormalise_bands(&mut self, eff_end: usize, c: usize, m: usize, n: usize) { + for ch in 0..c { + for i in 0..eff_end { + let g = self.band_e[i + ch * NB_BANDS]; + for j in m * E_BANDS[i]..m * E_BANDS[i + 1] { + self.freq[ch * n + j] = self.x[ch * n + j] * g; + } + } + for j in m * E_BANDS[eff_end]..n { + self.freq[ch * n + j] = 0.0; + } + } + } + + fn inverse_mdcts(&mut self, short_blocks: usize, lm: usize, n: usize, cc: usize) { + let (n2, blocks, plan_lm) = if short_blocks != 0 { + (SHORT_MDCT, short_blocks, 0) + } else { + (n, 1, lm) + }; + for ch in 0..cc { + self.mdct_buf[..n + OVERLAP].fill(0.0); + for b in 0..blocks { + let spec_off = ch * n + b; + let plan = &mut self.plans[plan_lm]; + plan.inverse( + &self.freq[spec_off..], + blocks, + &self.window, + &mut self.mdct_buf, + n2 * b, + ); + } + let out_base = ch * DECODE_BUFFER + DECODE_BUFFER - n; + let ov_base = ch * OVERLAP; + for j in 0..OVERLAP { + self.decode_mem[out_base + j] = self.mdct_buf[j] + self.overlap_mem[ov_base + j]; + } + for j in OVERLAP..n { + self.decode_mem[out_base + j] = self.mdct_buf[j]; + } + for j in 0..OVERLAP { + self.overlap_mem[ov_base + j] = self.mdct_buf[n + j]; + } + } + } + + #[allow(clippy::too_many_arguments)] + #[allow(clippy::too_many_arguments)] + fn comb_filter( + &mut self, + base: usize, + n: usize, + t0: usize, + t1: usize, + g0: f32, + g1: f32, + tapset0: usize, + tapset1: usize, + _in_place: bool, + ) { + if g0 == 0.0 && g1 == 0.0 { + return; + } + let g00 = g0 * POSTFILTER_GAINS[tapset0][0]; + let g01 = g0 * POSTFILTER_GAINS[tapset0][1]; + let g02 = g0 * POSTFILTER_GAINS[tapset0][2]; + let g10 = g1 * POSTFILTER_GAINS[tapset1][0]; + let g11 = g1 * POSTFILTER_GAINS[tapset1][1]; + let g12 = g1 * POSTFILTER_GAINS[tapset1][2]; + // The overlap region cross-fades between the old and new pitch. + let ov = OVERLAP.min(n); + for i in 0..ov { + let f = self.window[i] * self.window[i]; + let x = |k: usize| self.decode_mem[base + i - k]; + let v = self.decode_mem[base + i] + + (1.0 - f) * g00 * x(t0) + + (1.0 - f) * g01 * (x(t0 + 1) + x(t0.wrapping_sub(1))) + + (1.0 - f) * g02 * (x(t0 + 2) + x(t0.wrapping_sub(2))) + + f * g10 * x(t1) + + f * g11 * (x(t1 + 1) + x(t1.wrapping_sub(1))) + + f * g12 * (x(t1 + 2) + x(t1.wrapping_sub(2))); + self.decode_mem[base + i] = v; + } + for i in ov..n { + let x = |k: usize| self.decode_mem[base + i - k]; + let v = self.decode_mem[base + i] + + g10 * x(t1) + + g11 * (x(t1 + 1) + x(t1.wrapping_sub(1))) + + g12 * (x(t1 + 2) + x(t1.wrapping_sub(2))); + self.decode_mem[base + i] = v; + } + } + + fn deemphasis(&mut self, pcm: &mut [f32], n: usize, cc: usize) { + for ch in 0..cc { + let base = ch * DECODE_BUFFER + DECODE_BUFFER - n; + let mut mem = self.preemph_mem[ch]; + let mut count = 0usize; + let mut y = ch; + for j in 0..n { + let x = self.decode_mem[base + j]; + let tmp = x + mem; + mem = PREEMPH * tmp; + if count == 0 { + pcm[y] = tmp / SIG_SCALE; + } + count += 1; + if count == self.downsample { + y += cc; + count = 0; + } + } + self.preemph_mem[ch] = mem; + } + } +} + +/// `ec_laplace_decode()`: the coarse-energy prediction error. +fn laplace_decode(dec: &mut RangeDecoder, fs0: u32, decay: i32) -> i32 { + const MINP: u32 = 1; + const NMIN: u32 = 16; + let mut val = 0i32; + let mut fs = fs0; + let fm = dec.decode_bin(15); + let mut fl = 0u32; + if fm >= fs { + val += 1; + fl = fs; + let ft = 32768 - MINP * (2 * NMIN) - fs0; + fs = ((ft * (16384 - decay as u32)) >> 15) + MINP; + while fs > MINP && fm >= fl + 2 * fs { + fs *= 2; + fl += fs; + fs = (((fs - 2 * MINP) * decay as u32) >> 15) + MINP; + val += 1; + } + if fs <= MINP { + let di = (fm - fl) >> 1; + val += di as i32; + fl += 2 * di * MINP; + } + if fm < fl + fs { + val = -val; + } else { + fl += fs; + } + } + dec.update(fl, (fl + fs).min(32768), 32768); + val +} + +/// `bitexact_cos()`: a cosine that must round identically everywhere, because +/// the bit allocation depends on it. +fn bitexact_cos(x: i16) -> i16 { + let tmp = (4096 + (x as i32) * (x as i32)) >> 13; + let mut x2 = tmp as i16; + x2 = (32767 - x2 as i32 + + frac_mul16( + x2, + (-7651 + frac_mul16(x2, 8277 + frac_mul16(-626, x2) as i16)) as i16, + )) as i16; + 1 + x2 +} + +fn frac_mul16(a: i16, b: i16) -> i32 { + (16384 + (a as i32) * (b as i32)) >> 15 +} + +fn bitexact_log2tan(isin: i32, icos: i32) -> i32 { + let lc = ilog(icos as u32); + let ls = ilog(isin as u32); + let isin = (isin << (15 - ls)) as i16; + let icos = (icos << (15 - lc)) as i16; + (ls - lc) * (1 << 11) + frac_mul16(isin, (frac_mul16(isin, -2597) + 7932) as i16) + - frac_mul16(icos, (frac_mul16(icos, -2597) + 7932) as i16) +} + +fn compute_qn(n: i32, b: i32, offset: i32, pulse_cap: i32, stereo: bool) -> i32 { + const EXP2_TABLE8: [i32; 8] = [16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048]; + let mut n2 = 2 * n - 1; + if stereo && n == 2 { + n2 -= 1; + } + let mut qb = (b - pulse_cap - (4 << BITRES)).min((b + n2 * offset) / n2); + qb = qb.min(8 << BITRES); + if qb < (1 << BITRES >> 1) { + 1 + } else { + let qn = EXP2_TABLE8[(qb & 0x7) as usize] >> (14 - (qb >> BITRES)); + (qn + 1) >> 1 << 1 + } +} + +fn get_pulses(i: i32) -> i32 { + if i < 8 { + i + } else { + (8 + (i & 7)) << ((i >> 3) - 1) + } +} + +fn bits2pulses(band: usize, lm: i32, bits: i32) -> i32 { + let cache = &CACHE_BITS[cache_index(band, lm)..]; + let mut lo = 0i32; + let mut hi = cache[0] as i32; + let bits = bits - 1; + for _ in 0..6 { + let mid = (lo + hi + 1) >> 1; + if cache[mid as usize] as i32 >= bits { + hi = mid; + } else { + lo = mid; + } + } + let lo_cost = if lo == 0 { + -1 + } else { + cache[lo as usize] as i32 + }; + if bits - lo_cost <= cache[hi as usize] as i32 - bits { + lo + } else { + hi + } +} + +fn pulses2bits(band: usize, lm: i32, pulses: i32) -> i32 { + if pulses == 0 { + return 0; + } + let cache = &CACHE_BITS[cache_index(band, lm)..]; + cache[pulses as usize] as i32 + 1 +} + +/// One level of the Haar transform, used for the time/frequency changes. +fn haar1(x: &mut [f32], n0: usize, stride: usize) { + let n0 = n0 >> 1; + const S: f32 = core::f32::consts::FRAC_1_SQRT_2; + for i in 0..stride { + for j in 0..n0 { + let a = S * x[stride * 2 * j + i]; + let b = S * x[stride * (2 * j + 1) + i]; + x[stride * 2 * j + i] = a + b; + x[stride * (2 * j + 1) + i] = a - b; + } + } +} + +fn deinterleave_hadamard(x: &mut [f32], n0: usize, stride: usize, hadamard: bool) { + let n = n0 * stride; + let mut tmp = vec![0.0f32; n]; + if hadamard { + let order = &ORDERY[stride - 2..]; + for i in 0..stride { + for j in 0..n0 { + tmp[order[i] * n0 + j] = x[j * stride + i]; + } + } + } else { + for i in 0..stride { + for j in 0..n0 { + tmp[i * n0 + j] = x[j * stride + i]; + } + } + } + x[..n].copy_from_slice(&tmp); +} + +fn interleave_hadamard(x: &mut [f32], n0: usize, stride: usize, hadamard: bool) { + let n = n0 * stride; + let mut tmp = vec![0.0f32; n]; + if hadamard { + let order = &ORDERY[stride - 2..]; + for i in 0..stride { + for j in 0..n0 { + tmp[j * stride + i] = x[order[i] * n0 + j]; + } + } + } else { + for i in 0..stride { + for j in 0..n0 { + tmp[j * stride + i] = x[i * n0 + j]; + } + } + } + x[..n].copy_from_slice(&tmp); +} + +/// Scales a vector to `gain` in the L2 sense. +fn renormalise(x: &mut [f32], gain: f32) { + let mut e = 1e-15f32; + for v in x.iter() { + e += v * v; + } + let g = gain / e.sqrt(); + for v in x.iter_mut() { + *v *= g; + } +} + +/// Recombines the mid/side pair back into left/right (`stereo_merge`). +fn stereo_merge(buf: &mut [f32], x: usize, y: usize, mid: f32, n: usize) { + let mut xp = 0.0f32; + let mut side = 0.0f32; + for j in 0..n { + xp += buf[x + j] * buf[y + j]; + side += buf[y + j] * buf[y + j]; + } + xp *= mid; + let mid2 = mid; + let el = mid2 * mid2 + side - 2.0 * xp; + let er = mid2 * mid2 + side + 2.0 * xp; + if er < 6e-4 || el < 6e-4 { + for j in 0..n { + buf[y + j] = buf[x + j]; + } + return; + } + let lgain = 1.0 / el.sqrt(); + let rgain = 1.0 / er.sqrt(); + for j in 0..n { + let l = mid * buf[x + j]; + let r = buf[y + j]; + buf[x + j] = lgain * (l - r); + buf[y + j] = rgain * (l + r); + } +} + +/// The spreading rotation (Section 4.3.4.3), applied in the decode direction. +fn exp_rotation(x: &mut [f32], len: usize, dir: i32, stride: usize, k: usize, spread: usize) { + const SPREAD_FACTOR: [usize; 3] = [15, 10, 5]; + if 2 * k >= len || spread == SPREAD_NONE { + return; + } + let factor = SPREAD_FACTOR[spread - 1]; + let gain = len as f32 / (len + factor * k) as f32; + let theta = 0.5 * gain * gain; + let c = (core::f32::consts::FRAC_PI_2 * theta).cos(); + let s = (core::f32::consts::FRAC_PI_2 * (1.0 - theta)).cos(); + let mut stride2 = 0usize; + if len >= 8 * stride { + stride2 = 1; + while (stride2 * stride2 + stride2) * stride + (stride >> 2) < len { + stride2 += 1; + } + } + let sub = len / stride; + for i in 0..stride { + let seg = &mut x[i * sub..(i + 1) * sub]; + if dir < 0 { + if stride2 != 0 { + exp_rotation1(seg, sub, stride2, s, c); + } + exp_rotation1(seg, sub, 1, c, s); + } else { + exp_rotation1(seg, sub, 1, c, -s); + if stride2 != 0 { + exp_rotation1(seg, sub, stride2, s, -c); + } + } + } +} + +fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { + for i in 0..len.saturating_sub(stride) { + let x1 = x[i]; + let x2 = x[i + stride]; + x[i + stride] = c * x2 + s * x1; + x[i] = c * x1 - s * x2; + } + if len > 2 * stride { + for i in (0..=len - 2 * stride - 1).rev() { + let x1 = x[i]; + let x2 = x[i + stride]; + x[i + stride] = c * x2 + s * x1; + x[i] = c * x1 - s * x2; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn window_is_power_complementary() { + // Princen-Bradley: w[n]^2 + w[L-1-n]^2 == 1, which is what makes the + // overlap-add reconstruct exactly. + let w = overlap_window(); + for i in 0..OVERLAP { + let s = w[i] * w[i] + w[OVERLAP - 1 - i] * w[OVERLAP - 1 - i]; + assert!((s - 1.0).abs() < 1e-6, "w[{i}]^2 + w[..]^2 = {s}"); + } + } + + #[test] + fn fft15_matches_a_direct_transform() { + for &n in &[60usize, 120, 240, 480] { + let mut re: Vec = (0..n).map(|i| ((i * 37 % 19) as f32) - 9.0).collect(); + let mut im: Vec = (0..n).map(|i| ((i * 11 % 7) as f32) - 3.0).collect(); + let (re0, im0) = (re.clone(), im.clone()); + let mut fft = Fft15::new(n); + fft.inverse(&mut re, &mut im); + for k in 0..n { + let (mut sr, mut si) = (0.0f64, 0.0f64); + for j in 0..n { + let a = 2.0 * core::f64::consts::PI * (j * k % n) as f64 / n as f64; + sr += re0[j] as f64 * a.cos() - im0[j] as f64 * a.sin(); + si += re0[j] as f64 * a.sin() + im0[j] as f64 * a.cos(); + } + assert!( + (sr - re[k] as f64).abs() < 1e-2 * n as f64, + "n={n} k={k} re {sr} vs {}", + re[k] + ); + assert!( + (si - im[k] as f64).abs() < 1e-2 * n as f64, + "n={n} k={k} im" + ); + } + } + } + + /// The MDCT of a `2L` window, straight from the definition — the slow + /// reference the fast inverse is checked against. + fn direct_mdct(x: &[f32], l: usize) -> Vec { + (0..l) + .map(|k| { + let mut sum = 0.0f64; + for (n, &v) in x.iter().enumerate().take(2 * l) { + let a = core::f64::consts::PI / l as f64 + * (n as f64 + 0.5 + l as f64 / 2.0) + * (k as f64 + 0.5); + sum += v as f64 * a.cos(); + } + sum as f32 + }) + .collect() + } + + #[test] + fn imdct_overlap_add_reconstructs_the_signal() { + // Forward by definition, inverse through the FFT path, overlap-add at + // hop L: the middle of the signal must come back. This pins down every + // index, sign and window position in the inverse; the one free + // parameter left is the overall gain, which the test reports. + for &l in &[120usize, 240, 480] { + let frames = 4; + let total = (frames + 1) * l; + let sig: Vec = (0..total) + .map(|i| ((i * 7919 % 1000) as f32 / 500.0) - 1.0) + .collect(); + // The 2L analysis window: zero, rise, ones, fall, zero. + let w = overlap_window(); + let flat = (l - OVERLAP) / 2; + let mut win = vec![0.0f32; 2 * l]; + for n in 0..l { + win[n] = if n < flat { + 0.0 + } else if n < flat + OVERLAP { + w[n - flat] + } else { + 1.0 + }; + win[2 * l - 1 - n] = win[n]; + } + let mut plan = ImdctPlan::new(l); + let mut out = vec![0.0f32; total + 2 * l]; + for t in 0..frames { + let start = t * l; + let mut block = vec![0.0f32; 2 * l]; + for n in 0..2 * l { + block[n] = win[n] * sig.get(start + n).copied().unwrap_or(0.0); + } + let spec = direct_mdct(&block, l); + plan.inverse(&spec, 1, &w, &mut out, start + flat); + } + // Compare where two windows fully overlap. + let lo = l + OVERLAP; + let hi = frames * l - OVERLAP; + let mut num = 0.0f64; + let mut den = 0.0f64; + for i in lo..hi { + num += out[i] as f64 * sig[i] as f64; + den += sig[i] as f64 * sig[i] as f64; + } + let gain = num / den; + let mut err = 0.0f64; + for i in lo..hi { + let d = out[i] as f64 - gain * sig[i] as f64; + err += d * d; + } + let rel = (err / den).sqrt(); + assert!( + rel < 1e-3, + "L={l}: overlap-add residual {rel} at gain {gain}" + ); + // CELT puts the 2/N normalisation on the *forward* transform, so + // this inverse is the plain sum and the loop above, whose forward + // has no scaling at all, reconstructs at L/2. + assert!( + (gain - l as f64 / 2.0).abs() < 1e-3 * gain, + "L={l}: gain {gain}, expected {}", + l as f64 / 2.0 + ); + } + } + + #[test] + fn pvq_index_round_trips_within_v() { + // V(N,K) from the recursion in Section 4.3.4.2, checked against the + // closed forms the RFC gives for small N. + let mut u = vec![0u32; 300]; + assert_eq!(pvq_urow(2, 1, &mut u), 4); + assert_eq!(pvq_urow(2, 3, &mut u), 12); + assert_eq!(pvq_urow(3, 2, &mut u), 18); + assert_eq!(pvq_urow(4, 2, &mut u), 32); + // V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1). + for n in 3..8usize { + for k in 2..8usize { + let mut a = vec![0u32; 300]; + let v = pvq_urow(n, k, &mut a); + let v1 = pvq_urow(n - 1, k, &mut a); + let v2 = pvq_urow(n, k - 1, &mut a); + let v3 = pvq_urow(n - 1, k - 1, &mut a); + assert_eq!(v, v1 + v2 + v3, "V({n},{k})"); + } + } + } +} diff --git a/crates/ec-opus/src/lib.rs b/crates/ec-opus/src/lib.rs index 64cc22c6..644e1989 100644 --- a/crates/ec-opus/src/lib.rs +++ b/crates/ec-opus/src/lib.rs @@ -26,8 +26,173 @@ #![forbid(unsafe_code)] #![warn(missing_docs)] +pub mod celt; pub mod packet; pub mod range; +pub use celt::CeltDecoder; pub use packet::{Bandwidth, Mode, Packet, Toc}; pub use range::RangeDecoder; + +use ec_core::{Error, Result}; + +/// One Opus stream: mono or stereo, any mode, any frame size. +/// +/// Feed whole Opus packets to [`Decoder::decode_float`]; the decoder tracks the +/// inter-frame state (energy history, MDCT overlap, LPC memory) that Opus needs +/// across packets, so packets must be delivered in order. +#[derive(Clone, Debug)] +pub struct Decoder { + sample_rate: u32, + /// Output channels, which may exceed what a given packet codes. + channels: usize, + downsample: usize, + celt: CeltDecoder, + prev_mode: Option, + prev_redundancy: bool, + range_final: u32, +} + +impl Decoder { + /// A decoder for `channels` channels at `sample_rate`, which must be one of + /// 8000, 12000, 16000, 24000 or 48000 Hz. + pub fn new(sample_rate: u32, channels: usize) -> Result { + let downsample = match sample_rate { + 48000 => 1, + 24000 => 2, + 16000 => 3, + 12000 => 4, + 8000 => 6, + _ => { + return Err(Error::unsupported( + format!("opus output rate {sample_rate}"), + "Opus decodes to 8, 12, 16, 24 or 48 kHz only", + )); + } + }; + if !(1..=2).contains(&channels) { + return Err(Error::unsupported( + format!("{channels}-channel Opus stream"), + "one stream carries at most two channels; use MultistreamDecoder", + )); + } + Ok(Decoder { + sample_rate, + channels, + downsample, + celt: CeltDecoder::new(channels, downsample), + prev_mode: None, + prev_redundancy: false, + range_final: 0, + }) + } + + /// Output sample rate. + pub fn sample_rate(&self) -> u32 { + self.sample_rate + } + + /// Output channels. + pub fn channels(&self) -> usize { + self.channels + } + + /// The range coder state after the last packet. The RFC 6716 test vectors + /// carry this value per packet, which makes it a bit-exact conformance + /// check on everything the decoder read. + pub fn final_range(&self) -> u32 { + self.range_final + } + + /// Drops all inter-packet state; call after a seek. + pub fn reset(&mut self) { + self.celt.reset(); + self.prev_mode = None; + self.prev_redundancy = false; + self.range_final = 0; + } + + /// Decodes one packet into interleaved `f32`, returning samples per channel. + /// + /// `out` must hold `channels * 120 ms` worth of samples for the general + /// case (5760 per channel at 48 kHz). + pub fn decode_float(&mut self, packet: &[u8], out: &mut [f32]) -> Result { + let (samples, _) = self.decode_impl(packet, out, false)?; + Ok(samples) + } + + /// Decodes one self-delimited packet (RFC 6716, Appendix B), returning the + /// samples per channel and the bytes the packet occupied. This is what the + /// multistream framing needs for every stream but the last. + pub fn decode_self_delimited( + &mut self, + data: &[u8], + out: &mut [f32], + ) -> Result<(usize, usize)> { + self.decode_impl(data, out, true) + } + + fn decode_impl( + &mut self, + data: &[u8], + out: &mut [f32], + self_delimited: bool, + ) -> Result<(usize, usize)> { + let parsed = Packet::parse(data, self_delimited)?; + let toc = parsed.toc; + let frame_48k = toc.frame_size_48k(); + let frame_out = frame_48k / self.downsample; + let needed = parsed.frames.len() * frame_out * self.channels; + if out.len() < needed { + return Err(Error::corrupt(format!( + "opus: output buffer holds {} samples, packet needs {needed}", + out.len() + ))); + } + let mut done = 0usize; + let mut range = 0u32; + for frame in &parsed.frames { + let dst = &mut out[done * self.channels..(done + frame_out) * self.channels]; + range = self.decode_frame(frame, dst, toc, frame_48k)?; + done += frame_out; + } + self.range_final = range; + Ok((done, parsed.consumed)) + } + + /// Decodes one Opus frame (one entropy-coded unit) of a packet. + fn decode_frame( + &mut self, + data: &[u8], + out: &mut [f32], + toc: Toc, + frame_48k: usize, + ) -> Result { + let mode = toc.mode(); + let stream_channels = toc.channels(); + if data.len() <= 1 { + // DTX or a dropped frame: no PLC in this decoder, so emit silence. + out.fill(0.0); + return Ok(0); + } + let mut dec = RangeDecoder::new(data); + + if mode != Mode::Celt { + return Err(Error::unsupported( + "Opus SILK layer", + "only the CELT layer is implemented in this build", + )); + } + + // A mode switch invalidates the MDCT overlap and energy history. + if self.prev_mode.is_some_and(|p| p != mode) && !self.prev_redundancy { + self.celt.reset(); + } + let end_band = toc.bandwidth().celt_end_band(); + self.celt + .decode(&mut dec, out, frame_48k, 0, end_band, stream_channels)?; + self.prev_mode = Some(mode); + self.prev_redundancy = false; + Ok(dec.range()) + } +} diff --git a/crates/ec-opus/src/range.rs b/crates/ec-opus/src/range.rs index eb56864e..72b6156f 100644 --- a/crates/ec-opus/src/range.rs +++ b/crates/ec-opus/src/range.rs @@ -224,6 +224,13 @@ impl<'a> RangeDecoder<'a> { } } + /// Declares the rest of the frame consumed, so [`RangeDecoder::tell`] + /// reports the whole frame. CELT does this for a silent frame, whose + /// remaining bits carry nothing (RFC 6716, Section 4.3). + pub fn skip_to_end(&mut self) { + self.nbits_total = (self.buf.len() as u32) * 8 + ilog(self.rng); + } + /// `ec_tell()`: a conservative upper bound on whole bits used so far. #[inline] pub fn tell(&self) -> u32 { diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs new file mode 100644 index 00000000..434745e6 --- /dev/null +++ b/crates/ec-opus/tests/conformance.rs @@ -0,0 +1,351 @@ +//! RFC 6716 conformance: the official test vectors, the `opus_compare` quality +//! metric, real-file oracles against ffmpeg, and the decode-speed floor. +//! +//! All of it lives in one test binary on purpose — a file per feature costs a +//! serial link-and-run for every build. + +use std::fs; +use std::path::{Path, PathBuf}; + +use ec_opus::Decoder; + +/// One packet of an `opus_demo` test vector file. +struct VectorPacket { + payload: Vec, + /// The reference decoder's range state after this packet. + final_range: u32, +} + +/// The `.bit` files are `opus_demo` output: per packet, a big-endian length, a +/// big-endian final range, then the packet itself. +fn read_vector(path: &Path) -> Vec { + let data = fs::read(path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); + let mut out = Vec::new(); + let mut i = 0; + while i + 8 <= data.len() { + let len = u32::from_be_bytes(data[i..i + 4].try_into().unwrap()) as usize; + let rng = u32::from_be_bytes(data[i + 4..i + 8].try_into().unwrap()); + i += 8; + if i + len > data.len() { + break; + } + out.push(VectorPacket { + payload: data[i..i + len].to_vec(), + final_range: rng, + }); + i += len; + } + out +} + +fn vectors_dir() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../fixtures/vectors/opus-rfc6716/opus_testvectors") +} + +/// Decodes a vector at 48 kHz stereo, returning the samples and how many +/// packets ended with the reference range state. +fn decode_vector(packets: &[VectorPacket]) -> (Vec, usize, usize) { + let mut dec = Decoder::new(48000, 2).unwrap(); + let mut pcm = Vec::new(); + let mut buf = vec![0.0f32; 5760 * 2]; + let mut matched = 0; + let mut decoded = 0; + for p in packets { + match dec.decode_float(&p.payload, &mut buf) { + Ok(n) => { + pcm.extend_from_slice(&buf[..n * 2]); + decoded += 1; + if dec.final_range() == p.final_range { + matched += 1; + } + } + Err(e) => { + panic!("packet {decoded} failed: {e}"); + } + } + } + (pcm, matched, decoded) +} + +// --------------------------------------------------------------------------- +// The conformance metric +// --------------------------------------------------------------------------- +// +// RFC 6716 Section 6 defines a compliant decoder as one whose output stays +// "within the thresholds specified by the opus_compare.c tool" against the +// reference decoder's output for each test vector. That tool is a weighted +// spectral comparison, reimplemented here so the check needs no C toolchain: +// +// 1. Both signals are cut into 480-sample Hann windows at a 120-sample hop +// and turned into per-bin power spectra (bins 0..200 of 240). +// 2. The reference's per-band energy is spread by a masking model: 10 dB per +// Bark upwards, 15 dB downwards, -3 dB per 2.5 ms in time, 1 % crosstalk +// between channels. A tenth of that masking energy is added to both +// spectra, so error under a strong signal counts for less. +// 3. Consecutive frames are averaged pairwise, then per bin the ratio +// r = Y/X contributes r - log(r) - 1 (zero when equal, growing either +// way); bins 79..81 are damped because the SILK/CELT crossover is free. +// 4. The per-band mean is squared, averaged over bands, squared again, +// raised to the fourth power per frame, averaged, and the 16th root taken. +// Quality is 100*(1 - 0.5*ln(1+err)/ln(1.13)); 0 is the pass threshold and +// 100 means identical output. +// +// A quality of 0 was calibrated to additive white noise at 48 dB SNR. + +const NBANDS: usize = 21; +const BANDS: [usize; NBANDS + 1] = [ + 0, 2, 4, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 68, 80, 96, 120, 156, 200, +]; +const WIN_SIZE: usize = 480; +const WIN_STEP: usize = 120; +const NFREQS: usize = 240; + +/// A complex FFT of size `15 * 2^k`, which is what a 480-sample window needs. +/// Same decomposition the decoder uses; duplicated here so the test measures +/// the reference metric rather than the decoder's own code. +struct Fft15 { + n: usize, + sub: ec_dsp::Fft, +} + +impl Fft15 { + fn new(n: usize) -> Fft15 { + assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); + Fft15 { + n, + sub: ec_dsp::Fft::new(n / 15), + } + } + + /// Power spectrum of a real signal, bins `0..n/2`. + fn power(&mut self, x: &[f64], out: &mut [f64]) { + let n = self.n; + let l = n / 15; + let mut re = vec![0.0f64; n]; + let mut im = vec![0.0f64; n]; + for n2 in 0..l { + for k1 in 0..15 { + let (mut sr, mut si) = (0.0, 0.0); + for n1 in 0..15 { + let a = 2.0 * std::f64::consts::PI * ((k1 * n1) % 15) as f64 / 15.0; + sr += x[l * n1 + n2] * a.cos(); + si += x[l * n1 + n2] * a.sin(); + } + let a = 2.0 * std::f64::consts::PI * (k1 * n2) as f64 / n as f64; + let (c, s) = (a.cos(), a.sin()); + re[k1 * l + n2] = sr * c - si * s; + im[k1 * l + n2] = sr * s + si * c; + } + } + for k1 in 0..15 { + let rr = &mut re[k1 * l..(k1 + 1) * l]; + let ii = &mut im[k1 * l..(k1 + 1) * l]; + self.sub.inverse_split(rr, ii); + for k2 in 0..l { + let idx = k1 + 15 * k2; + if idx < out.len() { + let (r, i) = (rr[k2] * l as f64, ii[k2] * l as f64); + out[idx] = r * r + i * i; + } + } + } + } +} + +/// Per-bin power spectra and per-band energies, `band_energy()` in the tool. +fn band_energy( + signal: &[f64], + channels: usize, + nframes: usize, + bands: &mut Option<&mut Vec>, + ps: &mut Vec, +) { + let mut fft = Fft15::new(WIN_SIZE); + let window: Vec = (0..WIN_SIZE) + .map(|j| 0.5 - 0.5 * (2.0 * std::f64::consts::PI / (WIN_SIZE - 1) as f64 * j as f64).cos()) + .collect(); + ps.resize(nframes * NFREQS * channels, 0.0); + if let Some(b) = bands { + b.resize(nframes * NBANDS * channels, 0.0); + } + let mut frame = vec![0.0f64; WIN_SIZE]; + let mut spec = vec![0.0f64; NFREQS]; + for xi in 0..nframes { + for ci in 0..channels { + for k in 0..WIN_SIZE { + frame[k] = window[k] * signal[(xi * WIN_STEP + k) * channels + ci]; + } + spec.fill(0.0); + fft.power(&frame, &mut spec); + for bi in 0..NBANDS { + let mut p = 0.0; + for xj in BANDS[bi]..BANDS[bi + 1] { + let v = spec[xj] + 100000.0; + ps[(xi * NFREQS + xj) * channels + ci] = v; + p += v; + } + if let Some(b) = bands { + b[(xi * NBANDS + bi) * channels + ci] = p / (BANDS[bi + 1] - BANDS[bi]) as f64; + } + } + } + } +} + +/// The `opus_compare` quality metric: >= 0 passes, 100 is identical output. +fn opus_compare(reference: &[i16], test: &[i16], channels: usize) -> f64 { + assert_eq!(reference.len(), test.len(), "sample counts must match"); + let x: Vec = reference.iter().map(|&v| v as f64).collect(); + let y: Vec = test.iter().map(|&v| v as f64).collect(); + let xlength = x.len() / channels; + assert!(xlength >= WIN_SIZE, "not enough samples to compare"); + let nframes = (xlength - WIN_SIZE + WIN_STEP) / WIN_STEP; + + let mut xb = Vec::new(); + let mut big_x = Vec::new(); + let mut big_y = Vec::new(); + band_energy(&x, channels, nframes, &mut Some(&mut xb), &mut big_x); + band_energy(&y, channels, nframes, &mut None, &mut big_y); + + for xi in 0..nframes { + // Frequency masking, low to high then high to low. + for bi in 1..NBANDS { + for ci in 0..channels { + xb[(xi * NBANDS + bi) * channels + ci] += + 0.1 * xb[(xi * NBANDS + bi - 1) * channels + ci]; + } + } + for bi in (0..NBANDS - 1).rev() { + for ci in 0..channels { + xb[(xi * NBANDS + bi) * channels + ci] += + 0.03 * xb[(xi * NBANDS + bi + 1) * channels + ci]; + } + } + // Temporal masking. + if xi > 0 { + for bi in 0..NBANDS { + for ci in 0..channels { + xb[(xi * NBANDS + bi) * channels + ci] += + 0.5 * xb[((xi - 1) * NBANDS + bi) * channels + ci]; + } + } + } + // Crosstalk between channels. + if channels == 2 { + for bi in 0..NBANDS { + let l = xb[(xi * NBANDS + bi) * channels]; + let r = xb[(xi * NBANDS + bi) * channels + 1]; + xb[(xi * NBANDS + bi) * channels] += 0.01 * r; + xb[(xi * NBANDS + bi) * channels + 1] += 0.01 * l; + } + } + // Apply the masking to both spectra. + for bi in 0..NBANDS { + for xj in BANDS[bi]..BANDS[bi + 1] { + for ci in 0..channels { + let m = 0.1 * xb[(xi * NBANDS + bi) * channels + ci]; + big_x[(xi * NFREQS + xj) * channels + ci] += m; + big_y[(xi * NFREQS + xj) * channels + ci] += m; + } + } + } + } + + // Average consecutive frames to make the comparison less twitchy. + for bi in 0..NBANDS { + for xj in BANDS[bi]..BANDS[bi + 1] { + for ci in 0..channels { + let mut xtmp = big_x[xj * channels + ci]; + let mut ytmp = big_y[xj * channels + ci]; + for xi in 1..nframes { + let xtmp2 = big_x[(xi * NFREQS + xj) * channels + ci]; + let ytmp2 = big_y[(xi * NFREQS + xj) * channels + ci]; + big_x[(xi * NFREQS + xj) * channels + ci] += xtmp; + big_y[(xi * NFREQS + xj) * channels + ci] += ytmp; + xtmp = xtmp2; + ytmp = ytmp2; + } + } + } + } + + let max_compare = BANDS[NBANDS]; + let mut err = 0.0f64; + for xi in 0..nframes { + let mut ef = 0.0f64; + for bi in 0..NBANDS { + let mut eb = 0.0f64; + for xj in BANDS[bi]..BANDS[bi + 1].min(max_compare) { + for ci in 0..channels { + let re = big_y[(xi * NFREQS + xj) * channels + ci] + / big_x[(xi * NFREQS + xj) * channels + ci]; + let mut im = re - re.ln() - 1.0; + // The SILK/CELT crossover may differ in its filters. + if (79..=81).contains(&xj) { + im *= 0.1; + } + if xj == 80 { + im *= 0.1; + } + eb += im; + } + } + eb /= ((BANDS[bi + 1] - BANDS[bi]) * channels) as f64; + ef += eb * eb; + } + ef /= NBANDS as f64; + ef *= ef; + err += ef * ef; + } + err = (err / nframes as f64).powf(1.0 / 16.0); + 100.0 * (1.0 - 0.5 * (1.0 + err).ln() / 1.13f64.ln()) +} + +/// `FLOAT2INT16`: what `opus_demo` writes, so the metric sees the same samples. +fn to_i16(pcm: &[f32]) -> Vec { + pcm.iter() + .map(|&v| (v * 32768.0).round().clamp(-32768.0, 32767.0) as i16) + .collect() +} + +fn read_i16(path: &Path) -> Vec { + let d = fs::read(path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); + d.chunks_exact(2) + .map(|c| i16::from_le_bytes([c[0], c[1]])) + .collect() +} + +/// The CELT-only vectors, which need no SILK layer. +const CELT_VECTORS: [&str; 3] = ["testvector01", "testvector07", "testvector11"]; + +#[test] +fn celt_vectors_match_the_reference_range_state() { + for name in CELT_VECTORS { + let path = vectors_dir().join(format!("{name}.bit")); + if !path.exists() { + eprintln!("{name}: missing, skipped (run scripts/fetch-vectors.sh)"); + continue; + } + let packets = read_vector(&path); + let (pcm, matched, decoded) = decode_vector(&packets); + println!( + "{name}: {matched}/{decoded} packets bit-exact, {} samples", + pcm.len() / 2 + ); + assert_eq!( + matched, decoded, + "{name}: range state diverged from the reference" + ); + + let reference = read_i16(&vectors_dir().join(format!("{name}.dec"))); + let mine = to_i16(&pcm); + let n = reference.len().min(mine.len()); + let q = opus_compare(&reference[..n], &mine[..n], 2); + println!("{name}: opus_compare quality {q:.1} %"); + assert!( + q >= 0.0, + "{name}: opus_compare quality {q:.1} % (must be >= 0)" + ); + } +}