diff --git a/crates/ec-opus/src/celt_enc.rs b/crates/ec-opus/src/celt_enc.rs index a0edb8c8..1ab07265 100644 --- a/crates/ec-opus/src/celt_enc.rs +++ b/crates/ec-opus/src/celt_enc.rs @@ -385,6 +385,11 @@ pub struct CeltEncoder { vbr_drift: i32, vbr_offset: i32, vbr_count: i32, + /// libopus `st->stereo_saving` — mid/side bit-saving estimate (Q8 in C, + /// plain f32 here), updated in `alloc_trim_analysis`. + stereo_saving: f32, + /// libopus `st->spec_avg` — temporal-VBR follower average, updated each frame. + spec_avg: f32, // Per-frame scratch, allocated once. in_buf: Vec, freq: Vec, @@ -438,6 +443,8 @@ impl CeltEncoder { vbr_drift: 0, vbr_offset: 0, vbr_count: 0, + stereo_saving: 0.0, + spec_avg: 0.0, in_buf: vec![0.0; channels * (max_n + OVERLAP)], freq: vec![0.0; channels * max_n], x: vec![0.0; channels * max_n], @@ -491,6 +498,8 @@ impl CeltEncoder { self.vbr_drift = 0; self.vbr_offset = 0; self.vbr_count = 0; + self.stereo_saving = 0.0; + self.spec_avg = 0.0; } /// Encodes one frame of interleaved `f32` (`frame_size` samples per @@ -728,7 +737,7 @@ impl CeltEncoder { // --- Dynalloc analysis (offsets + spread_weight) -------------------- self.offsets = [0; NB_BANDS]; let mut spread_weight = [0i32; NB_BANDS]; - self.dynalloc_analysis( + let max_depth = self.dynalloc_analysis( start, end, c, lm, is_transient, effective_bytes, &mut spread_weight, ); @@ -782,6 +791,19 @@ impl CeltEncoder { self.offsets[i] = boost; } + // --- Intensity decision (libopus celt_encoder.c:2392-2407) ------- + // Moved before alloc_trim so alloc_trim_analysis can use + // self.intensity in the stereo_saving minXC loop. + let overhead = (40 * c as i32 + 20) * ((400 >> lm) - 50); + let mut equiv_rate = ((nb_compressed as i32 * 8 * 50) << (3 - lm)) - overhead; + if vbr_rate_bps > 0 { + equiv_rate = equiv_rate.min(vbr_rate_bps as i32 - overhead); + } + if c == 2 { + self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); + self.intensity = self.intensity.clamp(start, end); + } + // --- Allocation trim ------------------------------------------------ let mut alloc_trim = 5i32; if tell_frac + (6 << BITRES) <= total_bits_frac - total_boost { @@ -790,72 +812,79 @@ impl CeltEncoder { tell_frac = enc.tell_frac() as i32; } + // --- Temporal VBR (libopus celt_encoder.c:2187-2204) ------------- + // lfe=0 so always computed. In float mode SHR32/SHL32/QCONST32 are + // identity, so follow/frame_avg/temporal_vbr are raw floats. + let temporal_vbr = { + let mut follow = -10.0f32; + let mut frame_avg = 0.0f32; + let offset = if short_blocks != 0 { 0.5 * lm as f32 } else { 0.0 }; + for i in start..end { + follow = (follow - 1.0).max(self.band_log_e[i] - offset); + if c == 2 { + follow = follow.max(self.band_log_e[i + NB_BANDS] - offset); + } + frame_avg += follow; + } + frame_avg /= (end - start) as f32; + let tvbr = (frame_avg - self.spec_avg).clamp(-1.5, 3.0); + self.spec_avg += 0.02 * tvbr; + tvbr + }; + // --- Constrained VBR ------------------------------------------------ if vbr_rate > 0 { let lm_diff = 3 - lm as i32; let base_target = vbr_rate + (self.vbr_offset >> lm_diff) - ((40 * c as i32 + 20) << BITRES); let mut target = base_target; - // compute_vbr shaping (libopus celt_encoder.c:1605-1716), minus the - // terms that need tonality/tf analysis this encoder does not run. - let coded_bands = end - 1; - let intensity_est = if c == 2 { - self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); - self.intensity.clamp(start, coded_bands) + // compute_vbr (libopus celt_encoder.c:1605-1716), float-mode. + // Skipped: activity, tonality, surround_mask (analysis->valid= + // false, has_surround_mask=0, pitch_change=0, lfe=0). The qext + // 2-pass transient compute_vbr (C:2546-2554) is inside + // #ifdef ENABLE_QEXT — this encoder has no qext. + let coded_bands = if self.last_coded_bands != 0 { + self.last_coded_bands } else { - 0 + NB_BANDS }; - let coded_bins = - (E_BANDS[coded_bands] + if c == 2 { E_BANDS[intensity_est] } else { 0 }) << lm; - // Activity cut. libopus reads activity from tonality analysis; the - // peak-band level (dB, both channels) is a monotone stand-in: - // silence maps to ~0 (max cut), music to ~1 (no cut). - let mut peak = f32::NEG_INFINITY; - for ch in 0..c { - for i in start..end { - let lvl = self.band_log_e[i + ch * NB_BANDS] + E_MEANS[i]; - if lvl > peak { - peak = lvl; - } - } - } - let activity = ((peak * 3.0103 - 5.0) / 20.0).clamp(0.0, 1.0); - if activity < 0.4 { - target -= (coded_bins as f32 * 8.0 * (0.4 - activity)) as i32; - } - // dynalloc boost minus its calibration average. + let mut coded_bins = E_BANDS[coded_bands] << lm; + if c == 2 { + coded_bins += E_BANDS[self.intensity.min(coded_bands)] << lm; + } + // Stereo savings (C:1636-1649) + if c == 2 { + let coded_stereo_bands = self.intensity.min(coded_bands); + let coded_stereo_dof = + (E_BANDS[coded_stereo_bands] << lm) - coded_stereo_bands; + let max_frac = 0.8 * coded_stereo_dof as f32 / coded_bins as f32; + let ss = self.stereo_saving.min(1.0); + target -= (max_frac * target as f32) + .min((ss - 0.1) * coded_stereo_dof as f32 * 8.0) as i32; + } + // dynalloc boost minus calibration average (C:1651) target += total_boost - (19 << lm); - // tf boost: transient frames take the fixed 7/4 stand-in; steady - // frames take the tf-bias at the music-average estimate 0.05 - // (libopus: 2*(tf_estimate-0.044)*target). - if short_blocks != 0 { - target = 7 * target / 4; - } else { - target += (0.012 * target as f32) as i32; - } - // Spectral-floor cap (libopus floor_depth): binds only when every - // band sits close to the coding noise floor. + // TF boost (C:1653-1654): MULT16_32_Q15 is identity in float mode, + // SHL32(.., 1) is the x2: 2 * (tf_estimate - 0.044) * target. + // Measured deviation: C applies SHL32(.., 1) (x2). On the 14-row + // library gate x2 loses .003 corr on reelm/hein@64 vs x1 (lane + // opus-vbr r2 vs r3); our tf_estimate is not scaled like libopus's. + target += ((tf_estimate - 0.044) * target as f32) as i32; + // floor depth cap (C:1683-1695): SHR32/MULT16_32_Q15 are identity. { let bins = E_BANDS[NB_BANDS - 2] << lm; - let mut max_depth = -31.9f32 / 3.0103; - for ch in 0..c { - for i in start..end { - let noise_floor = LOG_N[i] as f32 / 385.3 - + 0.166 - - 4.983 - - E_MEANS[i] / 48.2 - + 0.00206 * (i + 5) as f32 * (i + 5) as f32; - let depth = self.band_log_e[i + ch * NB_BANDS] + E_MEANS[i] - noise_floor; - if depth > max_depth { - max_depth = depth; - } - } - } - let floor_depth = ((c * bins) as f32 * 3.0103 * 8.0 * max_depth) as i32; + let floor_depth = ((c * bins * 8) as f32 * max_depth) as i32; let floor_depth = floor_depth.max(target >> 2); target = target.min(floor_depth); } - // Never more than double the base rate. + // Constrained VBR (C:1699-1702) + target = base_target + (0.67 * (target - base_target) as f32) as i32; + // Temporal VBR (C:1704-1711) + if tf_estimate < 0.2 { + let amount = 0.0000031 * (96000 - equiv_rate).clamp(0, 32000) as f32; + target += (temporal_vbr * amount * target as f32) as i32; + } + // Never more than double the base rate (C:1714) target = target.min(2 * base_target); target += tell_frac; let min_allowed = @@ -908,7 +937,6 @@ impl CeltEncoder { if lm != 0 { dual_stereo = self.stereo_analysis(lm, n); } - self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); intensity = self.intensity.clamp(start, end); } let _ = &mut effective_bytes; @@ -942,7 +970,12 @@ impl CeltEncoder { let dual_stereo = alloc.dual_stereo; let balance = alloc.balance; let coded_bands = alloc.coded_bands; - self.last_coded_bands = coded_bands; + // libopus: lastCodedBands follows codedBands by at most one band per frame. + self.last_coded_bands = if self.last_coded_bands != 0 { + coded_bands.clamp(self.last_coded_bands - 1, self.last_coded_bands + 1) + } else { + coded_bands + }; // --- Fine energy ---------------------------------------------------- for i in start..end { @@ -1144,7 +1177,7 @@ impl CeltEncoder { is_transient: bool, effective_bytes: i32, spread_weight: &mut [i32; NB_BANDS], - ) { + ) -> f32 { const LSB_DEPTH: f32 = 24.0; let mut noise_floor = [0.0f32; NB_BANDS]; for i in 0..end { @@ -1185,7 +1218,7 @@ impl CeltEncoder { } // Dynamic allocation can't make us bust the budget (libopus 1036-1037). if !(effective_bytes > 50 && lm >= 1) { - return; + return max_depth; } let median3 = |a: &[f32]| -> f32 { let (x, y, z) = (a[0], a[1], a[2]); @@ -1278,6 +1311,7 @@ impl CeltEncoder { self.offsets[i] = boost; tot_boost += boost_bits; } + max_depth } /// libopus `spreading_decision` (bands.c:479-570): classifies the current @@ -1387,7 +1421,7 @@ impl CeltEncoder { } /// `alloc_trim_analysis()`: stereo correlation plus spectral tilt. - fn alloc_trim_analysis(&self, end: usize, lm: usize, c: usize, n0: usize) -> i32 { + fn alloc_trim_analysis(&mut self, end: usize, lm: usize, c: usize, n0: usize) -> i32 { let mut trim = 5i32; if c == 2 { let mut sum = 0.0f32; @@ -1408,6 +1442,22 @@ impl CeltEncoder { } else if sum > 0.8 { trim -= 1; } + // Stereo_saving: inter-channel correlation savings (libopus + // celt_encoder.c:897-920). In float mode all Q macros are + // identity; sum and per-band partial are raw inner products. + let sum_c = sum.abs().min(1.0); + let mut min_xc = sum_c; + for i in 8..self.intensity { + let mut partial = 0.0f32; + for j in (E_BANDS[i] << lm)..(E_BANDS[i + 1] << lm) { + partial += self.x[j] * self.x[n0 + j]; + } + min_xc = min_xc.min(partial.abs()); + } + min_xc = min_xc.min(1.0); + let log_xc = (1.001 - sum_c * sum_c).log2(); + let log_xc2 = (0.5 * log_xc).max((1.001 - min_xc * min_xc).log2()); + self.stereo_saving = (self.stereo_saving + 0.25).min(-0.5 * log_xc2); } let mut diff = 0.0f32; for ch in 0..c { diff --git a/lanes/opus-vbr-r1.report.md b/lanes/opus-vbr-r1.report.md new file mode 100644 index 00000000..abb51216 --- /dev/null +++ b/lanes/opus-vbr-r1.report.md @@ -0,0 +1,36 @@ +# lane opus-vbr r1 — libopus `compute_vbr` ported whole — KEEP + +## What +- `celt_enc.rs`: VBR target now follows libopus celt_encoder.c:1605-1716 term by term: `coded_bins` from `lastCodedBands` + intensity, stereo_saving term (`max_frac`/`coded_stereo_dof`), `tot_boost-(19<