Port libopus compute_vbr whole (stereo_saving, temporal VBR, tf x2, floor depth)
Browse files at this commit567ea1d0d7dc3604cbc1c3fc1371985d666fda8eParent: 27e5266- feat
- port libopus compute_vbr whole (stereo_saving, temporal VBR, tf x2, floor depth)
1 changed file +86 -54
- crates/ec-opus/src/celt_enc.rs +86 -54
crates/ec-opus/src/celt_enc.rsmodified+86 -54
| @@ -791,6 +791,19 @@ impl CeltEncoder { | |||
| 791 | self.offsets[i] = boost; | 791 | self.offsets[i] = boost; |
| 792 | } | 792 | } |
| 793 | 793 | ||
| 794 | + // --- Intensity decision (libopus celt_encoder.c:2392-2407) ------- | ||
| 795 | + // Moved before alloc_trim so alloc_trim_analysis can use | ||
| 796 | + // self.intensity in the stereo_saving minXC loop. | ||
| 797 | + let overhead = (40 * c as i32 + 20) * ((400 >> lm) - 50); | ||
| 798 | + let mut equiv_rate = ((nb_compressed as i32 * 8 * 50) << (3 - lm)) - overhead; | ||
| 799 | + if vbr_rate_bps > 0 { | ||
| 800 | + equiv_rate = equiv_rate.min(vbr_rate_bps as i32 - overhead); | ||
| 801 | + } | ||
| 802 | + if c == 2 { | ||
| 803 | + self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); | ||
| 804 | + self.intensity = self.intensity.clamp(start, end); | ||
| 805 | + } | ||
| 806 | + | ||
| 794 | // --- Allocation trim ------------------------------------------------ | 807 | // --- Allocation trim ------------------------------------------------ |
| 795 | let mut alloc_trim = 5i32; | 808 | let mut alloc_trim = 5i32; |
| 796 | if tell_frac + (6 << BITRES) <= total_bits_frac - total_boost { | 809 | if tell_frac + (6 << BITRES) <= total_bits_frac - total_boost { |
| @@ -799,72 +812,76 @@ impl CeltEncoder { | |||
| 799 | tell_frac = enc.tell_frac() as i32; | 812 | tell_frac = enc.tell_frac() as i32; |
| 800 | } | 813 | } |
| 801 | 814 | ||
| 815 | + // --- Temporal VBR (libopus celt_encoder.c:2187-2204) ------------- | ||
| 816 | + // lfe=0 so always computed. In float mode SHR32/SHL32/QCONST32 are | ||
| 817 | + // identity, so follow/frame_avg/temporal_vbr are raw floats. | ||
| 818 | + let temporal_vbr = { | ||
| 819 | + let mut follow = -10.0f32; | ||
| 820 | + let mut frame_avg = 0.0f32; | ||
| 821 | + let offset = if short_blocks != 0 { 0.5 * lm as f32 } else { 0.0 }; | ||
| 822 | + for i in start..end { | ||
| 823 | + follow = (follow - 1.0).max(self.band_log_e[i] - offset); | ||
| 824 | + if c == 2 { | ||
| 825 | + follow = follow.max(self.band_log_e[i + NB_BANDS] - offset); | ||
| 826 | + } | ||
| 827 | + frame_avg += follow; | ||
| 828 | + } | ||
| 829 | + frame_avg /= (end - start) as f32; | ||
| 830 | + let tvbr = (frame_avg - self.spec_avg).clamp(-1.5, 3.0); | ||
| 831 | + self.spec_avg += 0.02 * tvbr; | ||
| 832 | + tvbr | ||
| 833 | + }; | ||
| 834 | + | ||
| 802 | // --- Constrained VBR ------------------------------------------------ | 835 | // --- Constrained VBR ------------------------------------------------ |
| 803 | if vbr_rate > 0 { | 836 | if vbr_rate > 0 { |
| 804 | let lm_diff = 3 - lm as i32; | 837 | let lm_diff = 3 - lm as i32; |
| 805 | let base_target = | 838 | let base_target = |
| 806 | vbr_rate + (self.vbr_offset >> lm_diff) - ((40 * c as i32 + 20) << BITRES); | 839 | vbr_rate + (self.vbr_offset >> lm_diff) - ((40 * c as i32 + 20) << BITRES); |
| 807 | let mut target = base_target; | 840 | let mut target = base_target; |
| 808 | - // compute_vbr shaping (libopus celt_encoder.c:1605-1716), minus the | 841 | + // compute_vbr (libopus celt_encoder.c:1605-1716), float-mode. |
| 809 | - // terms that need tonality/tf analysis this encoder does not run. | 842 | + // Skipped: activity, tonality, surround_mask (analysis->valid= |
| 810 | - let coded_bands = end - 1; | 843 | + // false, has_surround_mask=0, pitch_change=0, lfe=0). The qext |
| 811 | - let intensity_est = if c == 2 { | 844 | + // 2-pass transient compute_vbr (C:2546-2554) is inside |
| 812 | - self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); | 845 | + // #ifdef ENABLE_QEXT — this encoder has no qext. |
| 813 | - self.intensity.clamp(start, coded_bands) | 846 | + let coded_bands = if self.last_coded_bands != 0 { |
| 847 | + self.last_coded_bands | ||
| 814 | } else { | 848 | } else { |
| 815 | - 0 | 849 | + NB_BANDS |
| 816 | }; | 850 | }; |
| 817 | - let coded_bins = | 851 | + let mut coded_bins = E_BANDS[coded_bands] << lm; |
| 818 | - (E_BANDS[coded_bands] + if c == 2 { E_BANDS[intensity_est] } else { 0 }) << lm; | 852 | + if c == 2 { |
| 819 | - // Activity cut. libopus reads activity from tonality analysis; the | 853 | + coded_bins += E_BANDS[self.intensity.min(coded_bands)] << lm; |
| 820 | - // peak-band level (dB, both channels) is a monotone stand-in: | ||
| 821 | - // silence maps to ~0 (max cut), music to ~1 (no cut). | ||
| 822 | - let mut peak = f32::NEG_INFINITY; | ||
| 823 | - for ch in 0..c { | ||
| 824 | - for i in start..end { | ||
| 825 | - let lvl = self.band_log_e[i + ch * NB_BANDS] + E_MEANS[i]; | ||
| 826 | - if lvl > peak { | ||
| 827 | - peak = lvl; | ||
| 828 | - } | ||
| 829 | - } | ||
| 830 | - } | ||
| 831 | - let activity = ((peak * 3.0103 - 5.0) / 20.0).clamp(0.0, 1.0); | ||
| 832 | - if activity < 0.4 { | ||
| 833 | - target -= (coded_bins as f32 * 8.0 * (0.4 - activity)) as i32; | ||
| 834 | } | 854 | } |
| 835 | - // dynalloc boost minus its calibration average. | 855 | + // Stereo savings (C:1636-1649) |
| 856 | + if c == 2 { | ||
| 857 | + let coded_stereo_bands = self.intensity.min(coded_bands); | ||
| 858 | + let coded_stereo_dof = | ||
| 859 | + (E_BANDS[coded_stereo_bands] << lm) - coded_stereo_bands; | ||
| 860 | + let max_frac = 0.8 * coded_stereo_dof as f32 / coded_bins as f32; | ||
| 861 | + let ss = self.stereo_saving.min(1.0); | ||
| 862 | + target -= (max_frac * target as f32) | ||
| 863 | + .min((ss - 0.1) * coded_stereo_dof as f32 * 8.0) as i32; | ||
| 864 | + } | ||
| 865 | + // dynalloc boost minus calibration average (C:1651) | ||
| 836 | target += total_boost - (19 << lm); | 866 | target += total_boost - (19 << lm); |
| 837 | - // tf boost: transient frames take the fixed 7/4 stand-in; steady | 867 | + // TF boost (C:1653-1654): MULT16_32_Q15 is identity in float mode, |
| 838 | - // frames take the tf-bias at the music-average estimate 0.05 | 868 | + // SHL32(.., 1) is the x2: 2 * (tf_estimate - 0.044) * target. |
| 839 | - // (libopus: 2*(tf_estimate-0.044)*target). | 869 | + target += (2.0 * (tf_estimate - 0.044) * target as f32) as i32; |
| 840 | - if short_blocks != 0 { | 870 | + // floor depth cap (C:1683-1695): SHR32/MULT16_32_Q15 are identity. |
| 841 | - target = 7 * target / 4; | ||
| 842 | - } else { | ||
| 843 | - target += (0.012 * target as f32) as i32; | ||
| 844 | - } | ||
| 845 | - // Spectral-floor cap (libopus floor_depth): binds only when every | ||
| 846 | - // band sits close to the coding noise floor. | ||
| 847 | { | 871 | { |
| 848 | let bins = E_BANDS[NB_BANDS - 2] << lm; | 872 | let bins = E_BANDS[NB_BANDS - 2] << lm; |
| 849 | - let mut max_depth = -31.9f32 / 3.0103; | 873 | + let floor_depth = ((c * bins * 8) as f32 * max_depth) as i32; |
| 850 | - for ch in 0..c { | ||
| 851 | - for i in start..end { | ||
| 852 | - let noise_floor = LOG_N[i] as f32 / 385.3 | ||
| 853 | - + 0.166 | ||
| 854 | - - 4.983 | ||
| 855 | - - E_MEANS[i] / 48.2 | ||
| 856 | - + 0.00206 * (i + 5) as f32 * (i + 5) as f32; | ||
| 857 | - let depth = self.band_log_e[i + ch * NB_BANDS] + E_MEANS[i] - noise_floor; | ||
| 858 | - if depth > max_depth { | ||
| 859 | - max_depth = depth; | ||
| 860 | - } | ||
| 861 | - } | ||
| 862 | - } | ||
| 863 | - let floor_depth = ((c * bins) as f32 * 3.0103 * 8.0 * max_depth) as i32; | ||
| 864 | let floor_depth = floor_depth.max(target >> 2); | 874 | let floor_depth = floor_depth.max(target >> 2); |
| 865 | target = target.min(floor_depth); | 875 | target = target.min(floor_depth); |
| 866 | } | 876 | } |
| 867 | - // Never more than double the base rate. | 877 | + // Constrained VBR (C:1699-1702) |
| 878 | + target = base_target + (0.67 * (target - base_target) as f32) as i32; | ||
| 879 | + // Temporal VBR (C:1704-1711) | ||
| 880 | + if tf_estimate < 0.2 { | ||
| 881 | + let amount = 0.0000031 * (96000 - equiv_rate).clamp(0, 32000) as f32; | ||
| 882 | + target += (temporal_vbr * amount * target as f32) as i32; | ||
| 883 | + } | ||
| 884 | + // Never more than double the base rate (C:1714) | ||
| 868 | target = target.min(2 * base_target); | 885 | target = target.min(2 * base_target); |
| 869 | target += tell_frac; | 886 | target += tell_frac; |
| 870 | let min_allowed = | 887 | let min_allowed = |
| @@ -917,7 +934,6 @@ impl CeltEncoder { | |||
| 917 | if lm != 0 { | 934 | if lm != 0 { |
| 918 | dual_stereo = self.stereo_analysis(lm, n); | 935 | dual_stereo = self.stereo_analysis(lm, n); |
| 919 | } | 936 | } |
| 920 | - self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); | ||
| 921 | intensity = self.intensity.clamp(start, end); | 937 | intensity = self.intensity.clamp(start, end); |
| 922 | } | 938 | } |
| 923 | let _ = &mut effective_bytes; | 939 | let _ = &mut effective_bytes; |
| @@ -1194,7 +1210,7 @@ impl CeltEncoder { | |||
| 1194 | } | 1210 | } |
| 1195 | // Dynamic allocation can't make us bust the budget (libopus 1036-1037). | 1211 | // Dynamic allocation can't make us bust the budget (libopus 1036-1037). |
| 1196 | if !(effective_bytes > 50 && lm >= 1) { | 1212 | if !(effective_bytes > 50 && lm >= 1) { |
| 1197 | - return; | 1213 | + return max_depth; |
| 1198 | } | 1214 | } |
| 1199 | let median3 = |a: &[f32]| -> f32 { | 1215 | let median3 = |a: &[f32]| -> f32 { |
| 1200 | let (x, y, z) = (a[0], a[1], a[2]); | 1216 | let (x, y, z) = (a[0], a[1], a[2]); |
| @@ -1397,7 +1413,7 @@ impl CeltEncoder { | |||
| 1397 | } | 1413 | } |
| 1398 | 1414 | ||
| 1399 | /// `alloc_trim_analysis()`: stereo correlation plus spectral tilt. | 1415 | /// `alloc_trim_analysis()`: stereo correlation plus spectral tilt. |
| 1400 | - fn alloc_trim_analysis(&self, end: usize, lm: usize, c: usize, n0: usize) -> i32 { | 1416 | + fn alloc_trim_analysis(&mut self, end: usize, lm: usize, c: usize, n0: usize) -> i32 { |
| 1401 | let mut trim = 5i32; | 1417 | let mut trim = 5i32; |
| 1402 | if c == 2 { | 1418 | if c == 2 { |
| 1403 | let mut sum = 0.0f32; | 1419 | let mut sum = 0.0f32; |
| @@ -1418,6 +1434,22 @@ impl CeltEncoder { | |||
| 1418 | } else if sum > 0.8 { | 1434 | } else if sum > 0.8 { |
| 1419 | trim -= 1; | 1435 | trim -= 1; |
| 1420 | } | 1436 | } |
| 1437 | + // Stereo_saving: inter-channel correlation savings (libopus | ||
| 1438 | + // celt_encoder.c:897-920). In float mode all Q macros are | ||
| 1439 | + // identity; sum and per-band partial are raw inner products. | ||
| 1440 | + let sum_c = sum.abs().min(1.0); | ||
| 1441 | + let mut min_xc = sum_c; | ||
| 1442 | + for i in 8..self.intensity { | ||
| 1443 | + let mut partial = 0.0f32; | ||
| 1444 | + for j in (E_BANDS[i] << lm)..(E_BANDS[i + 1] << lm) { | ||
| 1445 | + partial += self.x[j] * self.x[n0 + j]; | ||
| 1446 | + } | ||
| 1447 | + min_xc = min_xc.min(partial.abs()); | ||
| 1448 | + } | ||
| 1449 | + min_xc = min_xc.min(1.0); | ||
| 1450 | + let log_xc = (1.001 - sum_c * sum_c).log2(); | ||
| 1451 | + let log_xc2 = (0.5 * log_xc).max((1.001 - min_xc * min_xc).log2()); | ||
| 1452 | + self.stereo_saving = (self.stereo_saving + 0.25).min(-0.5 * log_xc2); | ||
| 1421 | } | 1453 | } |
| 1422 | let mut diff = 0.0f32; | 1454 | let mut diff = 0.0f32; |
| 1423 | for ch in 0..c { | 1455 | for ch in 0..c { |