Merge branch 'lane-opus-silkq4': libopus stereo SILK mid/side rate split
Browse files at this commit3 changed files +214 -48
- crates/ec-opus/src/silk_enc_write.rs +143 -43
- crates/ec-opus/tests/conformance.rs +3 -5
- lanes/opus-silkq-r4.report.md +68 -0
crates/ec-opus/src/silk_enc_write.rsmodified+143 -43
| @@ -806,6 +806,11 @@ pub struct SilkStereoEncoder { | ||
| 806 | 806 | mirror_range: u32, |
| 807 | 807 | mirror_out: Vec<i16>, |
| 808 | 808 | target_bps: Option<u32>, |
| 809 | + /// libopus `smth_width_Q14`: smoothed stereo width. A fresh mono->stereo | |
| 810 | + /// transition starts it at 1.0 (`enc_API.c` line 191). | |
| 811 | + smth_width: f32, | |
| 812 | + /// libopus `width_prev_Q14 == 0`: the previous frame had zero width. | |
| 813 | + width_prev_zero: bool, | |
| 809 | 814 | prev_mid_only: bool, |
| 810 | 815 | voiced_frames: usize, |
| 811 | 816 | } |
| @@ -832,6 +837,8 @@ impl SilkStereoEncoder { | ||
| 832 | 837 | mirror_range: 0, |
| 833 | 838 | mirror_out: vec![0; 2 * 20 * fs_khz], |
| 834 | 839 | target_bps: None, |
| 840 | + smth_width: 1.0, | |
| 841 | + width_prev_zero: true, | |
| 835 | 842 | prev_mid_only: false, |
| 836 | 843 | voiced_frames: 0, |
| 837 | 844 | } |
| @@ -847,16 +854,68 @@ impl SilkStereoEncoder { | ||
| 847 | 854 | self.voiced_frames |
| 848 | 855 | } |
| 849 | 856 | |
| 850 | - /// Target bit rate in bits per second for the whole stereo packet. | |
| 857 | + /// Target bit rate in bits per second for the whole stereo packet. The | |
| 858 | + /// mid/side split is applied per frame by `stereo_rates`, mirroring | |
| 859 | + /// libopus `silk_stereo_LR_to_MS`. | |
| 851 | 860 | pub fn set_bitrate(&mut self, bps: u32) { |
| 852 | 861 | self.target_bps = Some(bps); |
| 853 | - // libopus (stereo_LR_to_MS) gives the side channel a fraction of | |
| 854 | - // the total that shrinks with its energy; a flat half starved the | |
| 855 | - // mid at 16 kbps stereo (libopus-heard corr .34 on the rate table). | |
| 856 | - let mid = (bps * 2 / 3).max(500); | |
| 857 | - let side = (bps - mid).max(500); | |
| 858 | - self.mid.set_bitrate(mid); | |
| 859 | - self.side.set_bitrate(side); | |
| 862 | + } | |
| 863 | + | |
| 864 | + /// libopus `silk_stereo_LR_to_MS` rate split and panned-mono rule. | |
| 865 | + /// `frac` is the sqrt of the side/mid energy ratio, standing in for | |
| 866 | + /// libopus's LP/HP residual ratios. Simplifications: no LP/HP band split, | |
| 867 | + /// the width smoother runs at the full 0.01/frame coefficient (libopus | |
| 868 | + /// scales it by speech activity), and the silent-side taper is skipped | |
| 869 | + /// (a no-op for 20 ms frames). Width never enters the bitstream directly | |
| 870 | + /// (our packets carry zero stereo predictors); it acts only through | |
| 871 | + /// these rates and the mid-only flag. | |
| 872 | + fn stereo_rates(&mut self, frame_ms: usize, frac: f32) -> (u32, u32, bool) { | |
| 873 | + let bps = self.target_bps.expect("stereo rate split needs a target"); | |
| 874 | + // Approximate bitrate spent coding the stereo parameters. | |
| 875 | + let total = bps | |
| 876 | + .saturating_sub(if frame_ms == 10 { 1200 } else { 600 }) | |
| 877 | + .max(1); | |
| 878 | + let min_mid = 2000 + 600 * self.fs_khz as u32; | |
| 879 | + // 8 parts mid, 5 + 3*frac parts side; below the mid minimum the | |
| 880 | + // stereo width is reduced instead. | |
| 881 | + let mut mid = (8.0 * total as f32) / (13.0 + 3.0 * frac); | |
| 882 | + let mut width = if mid < min_mid as f32 { | |
| 883 | + mid = min_mid as f32; | |
| 884 | + let side = total as f32 - mid; | |
| 885 | + (4.0 * (2.0 * side - min_mid as f32) | |
| 886 | + / ((1.0 + 3.0 * frac) * min_mid as f32)) | |
| 887 | + .clamp(0.0, 1.0) | |
| 888 | + } else { | |
| 889 | + 1.0 | |
| 890 | + }; | |
| 891 | + self.smth_width += (width - self.smth_width) * 0.01; | |
| 892 | + let mid_only = if self.width_prev_zero | |
| 893 | + && (8 * total < 13 * min_mid || frac * self.smth_width < 0.05) | |
| 894 | + { | |
| 895 | + // Panned mono: the previous frame already had zero width. | |
| 896 | + width = 0.0; | |
| 897 | + true | |
| 898 | + } else if !self.width_prev_zero | |
| 899 | + && (8 * total < 11 * min_mid || frac * self.smth_width < 0.02) | |
| 900 | + { | |
| 901 | + // Transition to zero width; the side is still coded this frame. | |
| 902 | + width = 0.0; | |
| 903 | + false | |
| 904 | + } else if self.smth_width > 0.95 { | |
| 905 | + width = 1.0; | |
| 906 | + false | |
| 907 | + } else { | |
| 908 | + width = self.smth_width; | |
| 909 | + false | |
| 910 | + }; | |
| 911 | + self.width_prev_zero = width == 0.0; | |
| 912 | + if mid_only { | |
| 913 | + (total, 0, true) | |
| 914 | + } else { | |
| 915 | + // Keep at least 1 bps on the side (libopus tail). | |
| 916 | + let side = (total as f32 - mid).max(1.0); | |
| 917 | + ((total - side as u32).max(1), side as u32, false) | |
| 918 | + } | |
| 860 | 919 | } |
| 861 | 920 | |
| 862 | 921 | /// Algorithmic delay in 48 kHz samples. |
| @@ -883,45 +942,64 @@ impl SilkStereoEncoder { | ||
| 883 | 942 | ); |
| 884 | 943 | if self.prev_mid_only { |
| 885 | 944 | self.side.reset_state(); |
| 886 | - if let Some(bps) = self.target_bps { | |
| 887 | - self.side.set_bitrate((bps / 2).max(500)); | |
| 888 | - } | |
| 889 | 945 | } |
| 890 | 946 | let samples = 48 * frame_ms; |
| 891 | 947 | let mut mid = Vec::with_capacity(samples); |
| 892 | 948 | let mut side = Vec::with_capacity(samples); |
| 949 | + let mut mid_energy = 0.0f32; | |
| 893 | 950 | let mut side_energy = 0.0f32; |
| 894 | 951 | for lr in pcm48.chunks_exact(2) { |
| 895 | 952 | let m = 0.5 * (lr[0] + lr[1]); |
| 896 | 953 | let s = 0.5 * (lr[0] - lr[1]); |
| 897 | 954 | mid.push(m); |
| 898 | 955 | side.push(s); |
| 956 | + mid_energy += m * m; | |
| 899 | 957 | side_energy += s * s; |
| 900 | 958 | } |
| 959 | + let frac = if mid_energy > 0.0 { | |
| 960 | + (side_energy / mid_energy).sqrt().min(1.0) | |
| 961 | + } else { | |
| 962 | + f32::from(side_energy > 0.0) | |
| 963 | + }; | |
| 964 | + let mid_only = if self.target_bps.is_some() { | |
| 965 | + let (mid_bps, side_bps, mid_only) = self.stereo_rates(frame_ms, frac); | |
| 966 | + self.mid.set_bitrate(mid_bps.max(500)); | |
| 967 | + if !mid_only { | |
| 968 | + self.side.set_bitrate(side_bps.max(500)); | |
| 969 | + } | |
| 970 | + mid_only | |
| 971 | + } else { | |
| 972 | + side_energy < 1.0e-10 | |
| 973 | + }; | |
| 901 | 974 | let mid_trial = self |
| 902 | 975 | .mid |
| 903 | 976 | .encode_inner(&mid, frame_ms, None, false, 0, true)?; |
| 904 | - let side_trial = self | |
| 905 | - .side | |
| 906 | - .encode_inner(&side, frame_ms, None, false, 0, true)?; | |
| 907 | - let mid_only = side_energy < 1.0e-10; | |
| 908 | - write_stereo_packet_header(enc, &mid_trial, &side_trial, mid_only); | |
| 977 | + let side_trial = if mid_only { | |
| 978 | + None | |
| 979 | + } else { | |
| 980 | + Some( | |
| 981 | + self.side | |
| 982 | + .encode_inner(&side, frame_ms, None, false, 0, true)?, | |
| 983 | + ) | |
| 984 | + }; | |
| 985 | + let side_vad = side_trial.as_ref().map(|t| t.vad_flag).unwrap_or(false); | |
| 986 | + write_stereo_packet_header(enc, mid_trial.vad_flag, side_vad); | |
| 909 | 987 | write_stereo_zero_pred(enc); |
| 910 | - if !side_trial.vad_flag { | |
| 988 | + if !side_vad { | |
| 911 | 989 | enc.enc_icdf(usize::from(mid_only), &STEREO_ONLY_CODE_MID_ICDF, 8); |
| 912 | 990 | } |
| 913 | 991 | write_frame_data(enc, &mid_trial, false, 0); |
| 914 | - if !mid_only { | |
| 915 | - write_frame_data(enc, &side_trial, false, 0); | |
| 992 | + if let Some(trial) = &side_trial { | |
| 993 | + write_frame_data(enc, trial, false, 0); | |
| 916 | 994 | } |
| 917 | 995 | self.mid.replay_ms(&mid_trial.bytes, frame_ms)?; |
| 918 | - if mid_only { | |
| 919 | - self.side.reset_state(); | |
| 920 | - } else { | |
| 921 | - self.side.replay_ms(&side_trial.bytes, frame_ms)?; | |
| 996 | + match &side_trial { | |
| 997 | + Some(trial) => self.side.replay_ms(&trial.bytes, frame_ms)?, | |
| 998 | + None => self.side.reset_state(), | |
| 922 | 999 | } |
| 923 | 1000 | self.prev_mid_only = mid_only; |
| 924 | - self.voiced_frames += usize::from(mid_trial.voiced || side_trial.voiced); | |
| 1001 | + self.voiced_frames += | |
| 1002 | + usize::from(mid_trial.voiced || side_trial.as_ref().is_some_and(|t| t.voiced)); | |
| 925 | 1003 | Ok(()) |
| 926 | 1004 | } |
| 927 | 1005 | |
| @@ -943,39 +1021,61 @@ impl SilkStereoEncoder { | ||
| 943 | 1021 | ); |
| 944 | 1022 | if self.prev_mid_only { |
| 945 | 1023 | self.side.reset_state(); |
| 946 | - if let Some(bps) = self.target_bps { | |
| 947 | - self.side.set_bitrate((bps / 2).max(500)); | |
| 948 | - } | |
| 949 | 1024 | } |
| 950 | 1025 | let samples = 48 * frame_ms; |
| 951 | 1026 | let mut mid = Vec::with_capacity(samples); |
| 952 | 1027 | let mut side = Vec::with_capacity(samples); |
| 1028 | + let mut mid_energy = 0.0f32; | |
| 953 | 1029 | let mut side_energy = 0.0f32; |
| 954 | 1030 | for lr in pcm48.chunks_exact(2) { |
| 955 | 1031 | let m = 0.5 * (lr[0] + lr[1]); |
| 956 | 1032 | let s = 0.5 * (lr[0] - lr[1]); |
| 957 | 1033 | mid.push(m); |
| 958 | 1034 | side.push(s); |
| 1035 | + mid_energy += m * m; | |
| 959 | 1036 | side_energy += s * s; |
| 960 | 1037 | } |
| 1038 | + let frac = if mid_energy > 0.0 { | |
| 1039 | + (side_energy / mid_energy).sqrt().min(1.0) | |
| 1040 | + } else { | |
| 1041 | + f32::from(side_energy > 0.0) | |
| 1042 | + }; | |
| 1043 | + let mid_only = if self.target_bps.is_some() { | |
| 1044 | + let (mid_bps, side_bps, mid_only) = self.stereo_rates(frame_ms, frac); | |
| 1045 | + self.mid.set_bitrate(mid_bps.max(500)); | |
| 1046 | + if !mid_only { | |
| 1047 | + self.side.set_bitrate(side_bps.max(500)); | |
| 1048 | + } | |
| 1049 | + mid_only | |
| 1050 | + } else { | |
| 1051 | + side_energy < 1.0e-10 | |
| 1052 | + }; | |
| 961 | 1053 | let mid_trial = self |
| 962 | 1054 | .mid |
| 963 | 1055 | .encode_inner(&mid, frame_ms, None, false, 0, true)?; |
| 964 | - let side_trial = self | |
| 965 | - .side | |
| 966 | - .encode_inner(&side, frame_ms, None, false, 0, true)?; | |
| 967 | - let mid_only = side_energy < 1.0e-10; | |
| 1056 | + let side_trial = if mid_only { | |
| 1057 | + None | |
| 1058 | + } else { | |
| 1059 | + Some( | |
| 1060 | + self.side | |
| 1061 | + .encode_inner(&side, frame_ms, None, false, 0, true)?, | |
| 1062 | + ) | |
| 1063 | + }; | |
| 968 | 1064 | |
| 969 | 1065 | self.range.reset(1275); |
| 970 | - write_stereo_packet_header(&mut self.range, &mid_trial, &side_trial, mid_only); | |
| 1066 | + write_stereo_packet_header( | |
| 1067 | + &mut self.range, | |
| 1068 | + mid_trial.vad_flag, | |
| 1069 | + side_trial.as_ref().map(|t| t.vad_flag).unwrap_or(false), | |
| 1070 | + ); | |
| 971 | 1071 | write_stereo_zero_pred(&mut self.range); |
| 972 | - if !side_trial.vad_flag { | |
| 1072 | + if !side_trial.as_ref().map(|t| t.vad_flag).unwrap_or(false) { | |
| 973 | 1073 | self.range |
| 974 | 1074 | .enc_icdf(usize::from(mid_only), &STEREO_ONLY_CODE_MID_ICDF, 8); |
| 975 | 1075 | } |
| 976 | 1076 | write_frame_data(&mut self.range, &mid_trial, false, 0); |
| 977 | - if !mid_only { | |
| 978 | - write_frame_data(&mut self.range, &side_trial, false, 0); | |
| 1077 | + if let Some(trial) = &side_trial { | |
| 1078 | + write_frame_data(&mut self.range, trial, false, 0); | |
| 979 | 1079 | } |
| 980 | 1080 | self.range.done(); |
| 981 | 1081 | debug_assert!(!self.range.error()); |
| @@ -998,13 +1098,13 @@ impl SilkStereoEncoder { | ||
| 998 | 1098 | out[1..1 + len].copy_from_slice(&self.range.data()[..len]); |
| 999 | 1099 | self.replay_ms(&out[1..1 + len], frame_ms)?; |
| 1000 | 1100 | self.mid.replay_ms(&mid_trial.bytes, frame_ms)?; |
| 1001 | - if mid_only { | |
| 1002 | - self.side.reset_state(); | |
| 1003 | - } else { | |
| 1004 | - self.side.replay_ms(&side_trial.bytes, frame_ms)?; | |
| 1101 | + match &side_trial { | |
| 1102 | + Some(trial) => self.side.replay_ms(&trial.bytes, frame_ms)?, | |
| 1103 | + None => self.side.reset_state(), | |
| 1005 | 1104 | } |
| 1006 | 1105 | self.prev_mid_only = mid_only; |
| 1007 | - self.voiced_frames += usize::from(mid_trial.voiced || side_trial.voiced); | |
| 1106 | + self.voiced_frames += | |
| 1107 | + usize::from(mid_trial.voiced || side_trial.as_ref().is_some_and(|t| t.voiced)); | |
| 1008 | 1108 | debug_assert_eq!(self.mirror_range, self.final_range); |
| 1009 | 1109 | Ok(1 + len) |
| 1010 | 1110 | } |
| @@ -1036,10 +1136,10 @@ impl SilkStereoEncoder { | ||
| 1036 | 1136 | } |
| 1037 | 1137 | } |
| 1038 | 1138 | |
| 1039 | -fn write_stereo_packet_header(enc: &mut RangeEncoder, mid: &Trial, side: &Trial, mid_only: bool) { | |
| 1040 | - enc.enc_bit_logp(mid.vad_flag, 1); | |
| 1139 | +fn write_stereo_packet_header(enc: &mut RangeEncoder, mid_vad: bool, side_vad: bool) { | |
| 1140 | + enc.enc_bit_logp(mid_vad, 1); | |
| 1041 | 1141 | enc.enc_bit_logp(false, 1); |
| 1042 | - enc.enc_bit_logp(if mid_only { false } else { side.vad_flag }, 1); | |
| 1142 | + enc.enc_bit_logp(side_vad, 1); | |
| 1043 | 1143 | enc.enc_bit_logp(false, 1); |
| 1044 | 1144 | } |
| 1045 | 1145 |
crates/ec-opus/tests/conformance.rsmodified+3 -5
| @@ -1441,11 +1441,9 @@ fn oracle_decodes_our_packets_across_the_rate_table() { | ||
| 1441 | 1441 | if channels == 1 { |
| 1442 | 1442 | 0.6 |
| 1443 | 1443 | } else { |
| 1444 | - // .4164 before the libopus SILK reservoir port | |
| 1445 | - // (lane opus-silkq r3), .34 after: stereo SILK at | |
| 1446 | - // 8 kbps per channel with a flat mid/side split. | |
| 1447 | - // Open: lanes/opus-silkq-r3.report.md residue. | |
| 1448 | - 0.33 | |
| 1444 | + // Stereo SILK with the libopus LR_to_MS rate split | |
| 1445 | + // and panned-mono rule (lane opus-silkq r4): .4534. | |
| 1446 | + 0.44 | |
| 1449 | 1447 | } |
| 1450 | 1448 | } |
| 1451 | 1449 | 20..=39 => 0.6, |
lanes/opus-silkq-r4.report.mdadded+68 -0
| @@ -0,0 +1,68 @@ | ||
| 1 | +# opus-silkq r4 — stereo SILK mid/side rate split | |
| 2 | + | |
| 3 | +Ported the libopus 1.5.2 `silk/stereo_LR_to_MS.c` rate logic into | |
| 4 | +`SilkStereoEncoder`, replacing the static mid = 2/3 bps split that r3's | |
| 5 | +bit-reservoir port had left regressing the synthetic stereo row. | |
| 6 | + | |
| 7 | +## What was ported (`SilkStereoEncoder::stereo_rates`, silk_enc_write.rs) | |
| 8 | + | |
| 9 | +- Overhead subtraction: `total = bps − (is10ms ? 1200 : 600)`, clamped ≥ 1. | |
| 10 | +- `min_mid = 2000 + 600·fs_kHz` (NB 6800, MB 9200, WB 11600). | |
| 11 | +- Split: `mid = 8·total/(13 + 3·frac)`; below `min_mid` the mid is clamped and | |
| 12 | + the width drops as `4(2·side − min_mid)/((1 + 3·frac)·min_mid)`, clamped [0, 1]. | |
| 13 | +- Width smoother: `smth += (width − smth)·0.01` each frame, state kept across | |
| 14 | + packets (`smth_width` init 1.0, `width_prev_zero` init true — the | |
| 15 | + mono→stereo transition semantics of `enc_API.c` 181–196, which fire at | |
| 16 | + stream start since `nChannelsInternal` starts 0). | |
| 17 | +- Branches, in libopus order: panned mono (`width_prev_zero && (8·total < | |
| 18 | + 13·min_mid || frac·smth < 0.05)` → rates (total, 0), mid-only flag set, | |
| 19 | + width 0); transition (same with `width_prev != 0`, thresholds 11·min_mid / | |
| 20 | + 0.02 → width 0, side still coded one frame); `smth > 0.95` → width 1; | |
| 21 | + else width = smth. Tail: side ≥ 1 bps. | |
| 22 | +- Bitstream contract kept decoder-true: when mid-only is rate-driven (side | |
| 23 | + trial not run), the header writes the side VAD as 0 and the mid-only icdf | |
| 24 | + bit follows, matching libopus's forced `VAD_flags[1] = 0` (`enc_API.c` 456, | |
| 25 | + 460–462). `write_stereo_packet_header` now takes the two VAD bools; | |
| 26 | + `prev_mid_only` still resets side-encoder state on the first side-coded | |
| 27 | + frame after a mid-only run (`prev_decode_only_middle` analog). | |
| 28 | +- The old `side_energy < 1e-10` immediate mid-only and the | |
| 29 | + `(bps/2).max(500)` side-rate reset in the `prev_mid_only` prologs are | |
| 30 | + subsumed by the panned-mono rule and deleted. `set_bitrate` is now | |
| 31 | + store-only; the split runs per frame in both `encode_frame_ms` and | |
| 32 | + `encode_hybrid_ms` (mid energy is now accumulated alongside side energy). | |
| 33 | + | |
| 34 | +## Named simplifications | |
| 35 | + | |
| 36 | +1. `frac = sqrt(E_side/E_mid)` full-band — no LP/HP band split (libopus runs | |
| 37 | + the ratio per band through the stereo predictors; ours are always zero). | |
| 38 | +2. Smoother coefficient fixed at 0.01/frame — no speech-activity scaling | |
| 39 | + (our encoder does not track speech activity; test material is always | |
| 40 | + active, where libopus's coefficient also lands near 0.01). | |
| 41 | +3. `silent_side_len` taper dropped — a no-op at 20 ms (320 − 128 = 192 ≥ | |
| 42 | + LA_SHAPE_MS·fs_kHz = 80); it only affects 10 ms frames, which this | |
| 43 | + encoder's stereo paths never code. | |
| 44 | + | |
| 45 | +## Measurements | |
| 46 | + | |
| 47 | +Oracle rate table (`oracle_decodes_our_packets_across_the_rate_table`), | |
| 48 | +2 ch 16 kbps worst-channel corr: **.3407 → .4534** (was .4164 before r3). | |
| 49 | +Floor raised 0.33 → 0.44; r3 regression comment deleted. | |
| 50 | +Other 2 ch rows: 32 kbps .8943, 64 kbps .9773 — floors untouched, pass. | |
| 51 | +`silk_stereo_speech_roundtrip`: NB20 .9803/.9922, MB20 .9855/.9942, | |
| 52 | +WB20 .9906/.9956 (mono floors .9791/.9873/.9902/.9943; worst gap −.0018, | |
| 53 | +assert corr + 0.01 ≥ mono holds); NB40/WB60 identical per-channel corr. | |
| 54 | + | |
| 55 | +Gate 2 (`silk_library_gate_vs_libopus`, mono, unchanged code path): | |
| 56 | +12k .8526, 16k .8646, 24k .9146, 32k .9391 — identical to r3 baseline; | |
| 57 | +RATE/LAG/DROPOUT gates pass. | |
| 58 | + | |
| 59 | +## Variants measured and rejected | |
| 60 | + | |
| 61 | +None — the libopus rule passed gate 1 on the first run (.4534 ≥ .40). | |
| 62 | + | |
| 63 | +## Why 16 kbps lands at panned mono | |
| 64 | + | |
| 65 | +16 kbps stereo WB: total = 15400, min_mid = 11600, frac ≈ .92 → | |
| 66 | +8·total = 123200 < 13·min_mid = 150800 → panned mono from frame 1, | |
| 67 | +permanent — exactly what libopus does at this rate; the mid keeps the full | |
| 68 | +15400 bps instead of starving between two channels. |