Merge branch 'lane-opustonal' (974dbe05): libopus 1.6 tonality analysis ported whole; its bitstream consumers ship off
Browse files at this commit9 changed files +1966 -0
- crates/ec-opus/src/analysis.rs +1673 -0
- crates/ec-opus/src/celt_enc.rs +122 -0
- crates/ec-opus/src/encoder.rs +9 -0
- crates/ec-opus/src/lib.rs +1 -0
- crates/ec-opus/tests/conformance.rs +101 -0
- lanes/opus-opustonal-r1.sweep.txt +15 -0
- lanes/opus-opustonal-r2.sweep.txt +15 -0
- lanes/opus-opustonal-r3.sweep.txt +15 -0
- lanes/opus-opustonal-r4.sweep.txt +15 -0
crates/ec-opus/src/analysis.rsadded+1673 -0
| @@ -0,0 +1,1673 @@ | ||
| 1 | +//! libopus's tonality analysis (`src/analysis.c`, `src/mlp.c`, `src/mlp_data.c` | |
| 2 | +//! at v1.6, the release ffmpeg's reference build links) ported to float Rust. | |
| 3 | +//! | |
| 4 | +//! The encoder feeds every API frame here *undelayed* — the same input | |
| 5 | +//! `opus_encode()` hands `run_analysis()`, before the CELT layer's | |
| 6 | +//! `delay_compensation` — and the analysis maintains its own 30 ms window at | |
| 7 | +//! 24 kHz (720 samples, [`ANALYSIS_BUF_SIZE`]). Each window yields one | |
| 8 | +//! [`AnalysisInfo`]: a tonality estimate and its spectral slope, a noisiness | |
| 9 | +//! and an activity measure, the detected bandwidth, and the music/activity | |
| 10 | +//! probabilities of a 25-input dense → 24-unit GRU → 2-output MLP whose | |
| 11 | +//! `i8` weights are copied verbatim from `mlp_data.c` (they are generated from | |
| 12 | +//! a Keras model upstream and have no closed form). | |
| 13 | +//! | |
| 14 | +//! **What is verbatim.** `tonality_analysis()`, `tonality_get_info()`, | |
| 15 | +//! `run_analysis()`, the 480-point analysis window and DCT tables, the band | |
| 16 | +//! edges, `silk_resampler_down2_hp()`, `fast_atan2f()`, the `tansig`/`sigmoid` | |
| 17 | +//! rational approximations, and all seven weight tables. The float build's Q | |
| 18 | +//! macros (`SHR32`, `MULT16_32_Q15`, `QCONST16`, `SHR64`) are identities and | |
| 19 | +//! are dropped. | |
| 20 | +//! | |
| 21 | +//! **What is omitted.** The fixed-point arms; `MLP_TRAINING`; the | |
| 22 | +//! `application`/`arch` fields (we have one code path); and libopus's | |
| 23 | +//! `CELT_SIG_SCALE` (32768) input scaling together with the matching | |
| 24 | +//! `SCALE_ENER` division by 32768² — they cancel exactly, so this port works | |
| 25 | +//! in the ±1 sample units our encoder already uses, and every energy-domain | |
| 26 | +//! constant (`noise_floor`, the `hp_ener` term) is in those same units. | |
| 27 | +//! `run_analysis()` only runs at 16, 24 and 48 kHz, exactly as libopus's | |
| 28 | +//! `st->Fs >= 16000` guard arranges; below that `AnalysisInfo::valid` stays | |
| 29 | +//! false and every consumer takes its no-analysis branch. | |
| 30 | + | |
| 31 | +use crate::celt::Fft15; | |
| 32 | + | |
| 33 | +/// Number of past frames the band-energy history keeps (`NB_FRAMES`). | |
| 34 | +const NB_FRAMES: usize = 8; | |
| 35 | +/// Number of tonality bands (`NB_TBANDS`). | |
| 36 | +const NB_TBANDS: usize = 18; | |
| 37 | +/// Analysis buffer: 30 ms at 24 kHz (`ANALYSIS_BUF_SIZE`). | |
| 38 | +const ANALYSIS_BUF_SIZE: usize = 720; | |
| 39 | +/// Ring size of the per-window info history (`DETECT_SIZE`). | |
| 40 | +const DETECT_SIZE: usize = 100; | |
| 41 | +/// libopus stops counting frames here (`ANALYSIS_COUNT_MAX`). | |
| 42 | +const ANALYSIS_COUNT_MAX: i32 = 10000; | |
| 43 | +/// Bands skipped at the top of the tonality sum (`NB_TONAL_SKIP_BANDS`). | |
| 44 | +const NB_TONAL_SKIP_BANDS: usize = 9; | |
| 45 | +/// Penalty for switching speech/music during active audio (`TRANSITION_PENALTY`). | |
| 46 | +const TRANSITION_PENALTY: f32 = 10.0; | |
| 47 | +/// Bands of the leakage boost vector (`LEAK_BANDS`, celt.h). | |
| 48 | +pub(crate) const LEAK_BANDS: usize = 19; | |
| 49 | +/// Analysis MDCT/FFT length (`N` in `tonality_analysis`). | |
| 50 | +const N: usize = 480; | |
| 51 | +/// Half of [`N`] (`N2`). | |
| 52 | +const N2: usize = 240; | |
| 53 | +const LEAKAGE_OFFSET: f32 = 2.5; | |
| 54 | +const LEAKAGE_SLOPE: f32 = 2.0; | |
| 55 | +/// Neurons of the widest MLP layer (`MAX_NEURONS`). | |
| 56 | +const MAX_NEURONS: usize = 32; | |
| 57 | +/// `1/128`, the `i8` weight scale (`WEIGHTS_SCALE`). | |
| 58 | +const WEIGHTS_SCALE: f32 = 1.0 / 128.0; | |
| 59 | + | |
| 60 | +/// Tonality band edges in 480-point FFT bins (`tbands`). | |
| 61 | +const TBANDS: [usize; NB_TBANDS + 1] = [ | |
| 62 | + 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 136, 160, 192, 240, | |
| 63 | +]; | |
| 64 | + | |
| 65 | +/// One analysis window's result — libopus's `AnalysisInfo`. | |
| 66 | +/// | |
| 67 | +/// Every field is meaningless unless [`AnalysisInfo::valid`] is set; the | |
| 68 | +/// [`Default`] value is libopus's "analysis did not run" state. | |
| 69 | +// `noisiness`, `max_pitch_ratio` and `leak_boost` are computed by the port but | |
| 70 | +// have no consumer in this encoder yet: their libopus readers are | |
| 71 | +// `dynalloc_analysis`'s leakage boost and `run_prefilter`'s gain scaling, | |
| 72 | +// neither of which is wired. They stay so the port is whole. | |
| 73 | +#[allow(dead_code)] | |
| 74 | +#[derive(Clone, Copy, Debug)] | |
| 75 | +pub(crate) struct AnalysisInfo { | |
| 76 | + /// Whether the rest of this struct may be read at all. | |
| 77 | + pub valid: bool, | |
| 78 | + /// Tonality of the frame, roughly 0 (noise) to 1 (pure tones). | |
| 79 | + pub tonality: f32, | |
| 80 | + /// Spectral slope of the per-band tonality, `alloc_trim`'s input. | |
| 81 | + pub tonality_slope: f32, | |
| 82 | + /// Mean per-band noisiness. | |
| 83 | + pub noisiness: f32, | |
| 84 | + /// Perceptual activity measure, `compute_vbr`'s input. | |
| 85 | + pub activity: f32, | |
| 86 | + /// Music probability, look-ahead-smoothed by `tonality_get_info`. | |
| 87 | + pub music_prob: f32, | |
| 88 | + /// Lower switching threshold for speech → music. | |
| 89 | + pub music_prob_min: f32, | |
| 90 | + /// Upper switching threshold for music → speech. | |
| 91 | + pub music_prob_max: f32, | |
| 92 | + /// Raw MLP activity (voice-activity) probability of this window. | |
| 93 | + pub activity_probability: f32, | |
| 94 | + /// Energy ratio below/above the maximum pitch bin. | |
| 95 | + pub max_pitch_ratio: f32, | |
| 96 | + /// Detected bandwidth as a tonality-band index (0..=20). | |
| 97 | + pub bandwidth: i32, | |
| 98 | + /// Per-band spectral-leakage boost, in 1/64 log2 units. | |
| 99 | + pub leak_boost: [u8; LEAK_BANDS], | |
| 100 | +} | |
| 101 | + | |
| 102 | +impl Default for AnalysisInfo { | |
| 103 | + fn default() -> Self { | |
| 104 | + AnalysisInfo { | |
| 105 | + valid: false, | |
| 106 | + tonality: 0.0, | |
| 107 | + tonality_slope: 0.0, | |
| 108 | + noisiness: 0.0, | |
| 109 | + activity: 0.0, | |
| 110 | + music_prob: 0.0, | |
| 111 | + music_prob_min: 0.0, | |
| 112 | + music_prob_max: 0.0, | |
| 113 | + activity_probability: 0.0, | |
| 114 | + max_pitch_ratio: 0.0, | |
| 115 | + bandwidth: 0, | |
| 116 | + leak_boost: [0; LEAK_BANDS], | |
| 117 | + } | |
| 118 | + } | |
| 119 | +} | |
| 120 | + | |
| 121 | +/// libopus `fast_atan2f()` — the rational approximation `analysis.c` uses for | |
| 122 | +/// the per-bin phase, kept because the tonality features are calibrated on it. | |
| 123 | +fn fast_atan2f(y: f32, x: f32) -> f32 { | |
| 124 | + const CA: f32 = 0.431_579_74; | |
| 125 | + const CB: f32 = 0.678_484_04; | |
| 126 | + const CC: f32 = 0.085_955_42; | |
| 127 | + const CE: f32 = core::f32::consts::FRAC_PI_2; | |
| 128 | + let x2 = x * x; | |
| 129 | + let y2 = y * y; | |
| 130 | + if x2 + y2 < 1e-18 { | |
| 131 | + return 0.0; | |
| 132 | + } | |
| 133 | + if x2 < y2 { | |
| 134 | + let den = (y2 + CB * x2) * (y2 + CC * x2); | |
| 135 | + -x * y * (y2 + CA * x2) / den + if y < 0.0 { -CE } else { CE } | |
| 136 | + } else { | |
| 137 | + let den = (x2 + CB * y2) * (x2 + CC * y2); | |
| 138 | + x * y * (x2 + CA * y2) / den + if y < 0.0 { -CE } else { CE } | |
| 139 | + - if x * y < 0.0 { -CE } else { CE } | |
| 140 | + } | |
| 141 | +} | |
| 142 | + | |
| 143 | +/// libopus `tansig_approx()`. | |
| 144 | +fn tansig_approx(x: f32) -> f32 { | |
| 145 | + const N0: f32 = 952.528_02; | |
| 146 | + const N1: f32 = 96.392_357; | |
| 147 | + const N2C: f32 = 0.608_630_42; | |
| 148 | + const D0: f32 = 952.723_99; | |
| 149 | + const D1: f32 = 413.368_01; | |
| 150 | + const D2: f32 = 11.886_009; | |
| 151 | + let x2 = x * x; | |
| 152 | + let num = (N2C * x2 + N1) * x2 + N0; | |
| 153 | + let den = (D2 * x2 + D1) * x2 + D0; | |
| 154 | + (num * x / den).clamp(-1.0, 1.0) | |
| 155 | +} | |
| 156 | + | |
| 157 | +/// libopus `sigmoid_approx()`. | |
| 158 | +fn sigmoid_approx(x: f32) -> f32 { | |
| 159 | + 0.5 + 0.5 * tansig_approx(0.5 * x) | |
| 160 | +} | |
| 161 | + | |
| 162 | +/// libopus `gemm_accum()`: `out[i] += weights[j*col_stride + i] * x[j]`. | |
| 163 | +fn gemm_accum(out: &mut [f32], weights: &[i8], rows: usize, cols: usize, col_stride: usize, x: &[f32]) { | |
| 164 | + for (i, o) in out.iter_mut().take(rows).enumerate() { | |
| 165 | + for (j, &xj) in x.iter().take(cols).enumerate() { | |
| 166 | + *o += f32::from(weights[j * col_stride + i]) * xj; | |
| 167 | + } | |
| 168 | + } | |
| 169 | +} | |
| 170 | + | |
| 171 | +/// libopus `analysis_compute_dense()`. | |
| 172 | +fn compute_dense(bias: &[i8], weights: &[i8], nb_inputs: usize, nb_neurons: usize, sigmoid: bool, output: &mut [f32], input: &[f32]) { | |
| 173 | + for (i, o) in output.iter_mut().take(nb_neurons).enumerate() { | |
| 174 | + *o = f32::from(bias[i]); | |
| 175 | + } | |
| 176 | + gemm_accum(output, weights, nb_neurons, nb_inputs, nb_neurons, input); | |
| 177 | + for o in output.iter_mut().take(nb_neurons) { | |
| 178 | + *o *= WEIGHTS_SCALE; | |
| 179 | + *o = if sigmoid { sigmoid_approx(*o) } else { tansig_approx(*o) }; | |
| 180 | + } | |
| 181 | +} | |
| 182 | + | |
| 183 | +/// libopus `analysis_compute_gru()`. | |
| 184 | +fn compute_gru(bias: &[i8], weights: &[i8], recur: &[i8], nb_inputs: usize, nb_neurons: usize, state: &mut [f32], input: &[f32]) { | |
| 185 | + let n = nb_neurons; | |
| 186 | + let stride = 3 * n; | |
| 187 | + let mut z = [0.0f32; MAX_NEURONS]; | |
| 188 | + let mut r = [0.0f32; MAX_NEURONS]; | |
| 189 | + let mut h = [0.0f32; MAX_NEURONS]; | |
| 190 | + let mut tmp = [0.0f32; MAX_NEURONS]; | |
| 191 | + for i in 0..n { | |
| 192 | + z[i] = f32::from(bias[i]); | |
| 193 | + } | |
| 194 | + gemm_accum(&mut z, weights, n, nb_inputs, stride, input); | |
| 195 | + gemm_accum(&mut z, recur, n, n, stride, state); | |
| 196 | + for zi in z.iter_mut().take(n) { | |
| 197 | + *zi = sigmoid_approx(WEIGHTS_SCALE * *zi); | |
| 198 | + } | |
| 199 | + for i in 0..n { | |
| 200 | + r[i] = f32::from(bias[n + i]); | |
| 201 | + } | |
| 202 | + gemm_accum(&mut r, &weights[n..], n, nb_inputs, stride, input); | |
| 203 | + gemm_accum(&mut r, &recur[n..], n, n, stride, state); | |
| 204 | + for ri in r.iter_mut().take(n) { | |
| 205 | + *ri = sigmoid_approx(WEIGHTS_SCALE * *ri); | |
| 206 | + } | |
| 207 | + for i in 0..n { | |
| 208 | + h[i] = f32::from(bias[2 * n + i]); | |
| 209 | + tmp[i] = state[i] * r[i]; | |
| 210 | + } | |
| 211 | + gemm_accum(&mut h, &weights[2 * n..], n, nb_inputs, stride, input); | |
| 212 | + gemm_accum(&mut h, &recur[2 * n..], n, n, stride, &tmp); | |
| 213 | + for i in 0..n { | |
| 214 | + state[i] = z[i] * state[i] + (1.0 - z[i]) * tansig_approx(WEIGHTS_SCALE * h[i]); | |
| 215 | + } | |
| 216 | +} | |
| 217 | + | |
| 218 | +/// libopus `silk_resampler_down2_hp()` in float: halves the rate and returns | |
| 219 | +/// the energy of the discarded high band. | |
| 220 | +fn resampler_down2_hp(s: &mut [f32; 3], out: &mut [f32], input: &[f32]) -> f32 { | |
| 221 | + let len2 = input.len() / 2; | |
| 222 | + let mut hp_ener = 0.0f32; | |
| 223 | + for k in 0..len2 { | |
| 224 | + let in32 = input[2 * k]; | |
| 225 | + let y = in32 - s[0]; | |
| 226 | + let x = 0.607_437_1 * y; | |
| 227 | + let mut out32 = s[0] + x; | |
| 228 | + s[0] = in32 + x; | |
| 229 | + let mut out32_hp = out32; | |
| 230 | + | |
| 231 | + let in32 = input[2 * k + 1]; | |
| 232 | + let y = in32 - s[1]; | |
| 233 | + let x = 0.150_63 * y; | |
| 234 | + out32 = out32 + s[1] + x; | |
| 235 | + s[1] = in32 + x; | |
| 236 | + | |
| 237 | + let y = -in32 - s[2]; | |
| 238 | + let x = 0.150_63 * y; | |
| 239 | + out32_hp = out32_hp + s[2] + x; | |
| 240 | + s[2] = -in32 + x; | |
| 241 | + | |
| 242 | + hp_ener += out32_hp * out32_hp; | |
| 243 | + out[k] = 0.5 * out32; | |
| 244 | + } | |
| 245 | + hp_ener | |
| 246 | +} | |
| 247 | + | |
| 248 | +/// The rolling state of the tonality analysis — libopus's | |
| 249 | +/// `TonalityAnalysisState`, minus the fixed-point and `arch` fields. | |
| 250 | +#[derive(Clone, Debug)] | |
| 251 | +pub(crate) struct TonalityAnalysis { | |
| 252 | + /// Native input rate in Hz; 16000, 24000 or 48000 when enabled. | |
| 253 | + fs: u32, | |
| 254 | + /// False for rates libopus never analyses (below 16 kHz). | |
| 255 | + enabled: bool, | |
| 256 | + fft: Fft15, | |
| 257 | + angle: Vec<f32>, | |
| 258 | + d_angle: Vec<f32>, | |
| 259 | + d2_angle: Vec<f32>, | |
| 260 | + inmem: Vec<f32>, | |
| 261 | + mem_fill: usize, | |
| 262 | + prev_band_tonality: [f32; NB_TBANDS], | |
| 263 | + prev_tonality: f32, | |
| 264 | + prev_bandwidth: i32, | |
| 265 | + e: [[f32; NB_TBANDS]; NB_FRAMES], | |
| 266 | + log_e: [[f32; NB_TBANDS]; NB_FRAMES], | |
| 267 | + low_e: [f32; NB_TBANDS], | |
| 268 | + high_e: [f32; NB_TBANDS], | |
| 269 | + mean_e: [f32; NB_TBANDS + 1], | |
| 270 | + mem: [f32; 32], | |
| 271 | + cmean: [f32; 8], | |
| 272 | + std: [f32; 9], | |
| 273 | + e_tracker: f32, | |
| 274 | + low_e_count: f32, | |
| 275 | + e_count: usize, | |
| 276 | + count: i32, | |
| 277 | + analysis_offset: i32, | |
| 278 | + write_pos: usize, | |
| 279 | + read_pos: usize, | |
| 280 | + read_subframe: i32, | |
| 281 | + hp_ener_accum: f32, | |
| 282 | + initialized: bool, | |
| 283 | + rnn_state: [f32; MAX_NEURONS], | |
| 284 | + downmix_state: [f32; 3], | |
| 285 | + info: Vec<AnalysisInfo>, | |
| 286 | + /// Per-frame scratch, sized once so the steady-state encode loop stays | |
| 287 | + /// allocation-free (`steady_state_encode_loop_zero_alloc`). | |
| 288 | + dm_tmp: Vec<f32>, | |
| 289 | + dm_tmp3x: Vec<f32>, | |
| 290 | + chunk: Vec<f32>, | |
| 291 | + fft_re: Vec<f32>, | |
| 292 | + fft_im: Vec<f32>, | |
| 293 | +} | |
| 294 | + | |
| 295 | +impl TonalityAnalysis { | |
| 296 | + /// A state for `sample_rate` Hz input. Rates below 16 kHz are accepted but | |
| 297 | + /// disabled, mirroring libopus's `st->Fs >= 16000` guard on `run_analysis`. | |
| 298 | + pub(crate) fn new(sample_rate: u32) -> TonalityAnalysis { | |
| 299 | + TonalityAnalysis { | |
| 300 | + fs: sample_rate, | |
| 301 | + enabled: matches!(sample_rate, 16000 | 24000 | 48000), | |
| 302 | + fft: Fft15::new(N), | |
| 303 | + angle: vec![0.0; N2], | |
| 304 | + d_angle: vec![0.0; N2], | |
| 305 | + d2_angle: vec![0.0; N2], | |
| 306 | + inmem: vec![0.0; ANALYSIS_BUF_SIZE], | |
| 307 | + mem_fill: 0, | |
| 308 | + prev_band_tonality: [0.0; NB_TBANDS], | |
| 309 | + prev_tonality: 0.0, | |
| 310 | + prev_bandwidth: 0, | |
| 311 | + e: [[0.0; NB_TBANDS]; NB_FRAMES], | |
| 312 | + log_e: [[0.0; NB_TBANDS]; NB_FRAMES], | |
| 313 | + low_e: [0.0; NB_TBANDS], | |
| 314 | + high_e: [0.0; NB_TBANDS], | |
| 315 | + mean_e: [0.0; NB_TBANDS + 1], | |
| 316 | + mem: [0.0; 32], | |
| 317 | + cmean: [0.0; 8], | |
| 318 | + std: [0.0; 9], | |
| 319 | + e_tracker: 0.0, | |
| 320 | + low_e_count: 0.0, | |
| 321 | + e_count: 0, | |
| 322 | + count: 0, | |
| 323 | + analysis_offset: 0, | |
| 324 | + write_pos: 0, | |
| 325 | + read_pos: 0, | |
| 326 | + read_subframe: 0, | |
| 327 | + hp_ener_accum: 0.0, | |
| 328 | + initialized: false, | |
| 329 | + rnn_state: [0.0; MAX_NEURONS], | |
| 330 | + downmix_state: [0.0; 3], | |
| 331 | + info: vec![AnalysisInfo::default(); DETECT_SIZE], | |
| 332 | + dm_tmp: Vec::with_capacity(2 * ANALYSIS_BUF_SIZE), | |
| 333 | + dm_tmp3x: Vec::new(), | |
| 334 | + chunk: Vec::with_capacity(ANALYSIS_BUF_SIZE), | |
| 335 | + fft_re: vec![0.0; N], | |
| 336 | + fft_im: vec![0.0; N], | |
| 337 | + } | |
| 338 | + } | |
| 339 | + | |
| 340 | + /// Clears every non-reusable field — libopus `tonality_analysis_reset()`. | |
| 341 | + pub(crate) fn reset(&mut self) { | |
| 342 | + let fs = self.fs; | |
| 343 | + let fft = std::mem::replace(&mut self.fft, Fft15::new(15)); | |
| 344 | + *self = TonalityAnalysis::new(fs); | |
| 345 | + self.fft = fft; | |
| 346 | + } | |
| 347 | + | |
| 348 | + /// libopus `run_analysis()`: consumes one API frame of interleaved input | |
| 349 | + /// (`frame_size` samples per channel at the native rate) and returns the | |
| 350 | + /// [`AnalysisInfo`] that applies to it. | |
| 351 | + pub(crate) fn run(&mut self, pcm: &[f32], frame_size: usize, channels: usize, lsb_depth: i32) -> AnalysisInfo { | |
| 352 | + let mut info = AnalysisInfo::default(); | |
| 353 | + if !self.enabled || frame_size == 0 { | |
| 354 | + return info; | |
| 355 | + } | |
| 356 | + let fs = self.fs as i32; | |
| 357 | + let mut analysis_frame_size = frame_size as i32; | |
| 358 | + analysis_frame_size -= analysis_frame_size & 1; | |
| 359 | + // Avoid overflow/wrap-around of the analysis buffer. | |
| 360 | + analysis_frame_size = analysis_frame_size.min((DETECT_SIZE as i32 - 5) * fs / 50); | |
| 361 | + let mut pcm_len = analysis_frame_size - self.analysis_offset; | |
| 362 | + let mut offset = self.analysis_offset; | |
| 363 | + while pcm_len > 0 { | |
| 364 | + self.tonality_analysis(pcm, (fs / 50).min(pcm_len), offset, channels, lsb_depth); | |
| 365 | + offset += fs / 50; | |
| 366 | + pcm_len -= fs / 50; | |
| 367 | + } | |
| 368 | + self.analysis_offset = analysis_frame_size - frame_size as i32; | |
| 369 | + self.get_info(&mut info, frame_size as i32); | |
| 370 | + info | |
| 371 | + } | |
| 372 | + | |
| 373 | + /// libopus `downmix_and_resample()` for the float API: mono-downmixes | |
| 374 | + /// `subframe` native samples starting at `offset` into 24 kHz `y`, | |
| 375 | + /// returning the high-band energy the 48 kHz decimator throws away. | |
| 376 | + fn downmix_and_resample(&mut self, pcm: &[f32], y: &mut [f32], subframe: usize, offset: usize, channels: usize) -> f32 { | |
| 377 | + if subframe == 0 { | |
| 378 | + return 0.0; | |
| 379 | + } | |
| 380 | + let (subframe, offset) = match self.fs { | |
| 381 | + 48000 => (subframe * 2, offset * 2), | |
| 382 | + 16000 => (subframe * 2 / 3, offset * 2 / 3), | |
| 383 | + _ => (subframe, offset), | |
| 384 | + }; | |
| 385 | + let mut tmp = std::mem::take(&mut self.dm_tmp); | |
| 386 | + tmp.clear(); | |
| 387 | + tmp.resize(subframe, 0.0); | |
| 388 | + for (j, t) in tmp.iter_mut().enumerate() { | |
| 389 | + let base = (j + offset) * channels; | |
| 390 | + let mut acc = pcm[base]; | |
| 391 | + for c in 1..channels { | |
| 392 | + acc += pcm[base + c]; | |
| 393 | + } | |
| 394 | + *t = if channels == 2 { 0.5 * acc } else { acc }; | |
| 395 | + } | |
| 396 | + let mut st = self.downmix_state; | |
| 397 | + let ret = match self.fs { | |
| 398 | + 48000 => resampler_down2_hp(&mut st, y, &tmp), | |
| 399 | + 16000 => { | |
| 400 | + let mut tmp3x = std::mem::take(&mut self.dm_tmp3x); | |
| 401 | + tmp3x.clear(); | |
| 402 | + tmp3x.resize(3 * subframe, 0.0); | |
| 403 | + for j in 0..subframe { | |
| 404 | + tmp3x[3 * j] = tmp[j]; | |
| 405 | + tmp3x[3 * j + 1] = tmp[j]; | |
| 406 | + tmp3x[3 * j + 2] = tmp[j]; | |
| 407 | + } | |
| 408 | + resampler_down2_hp(&mut st, y, &tmp3x); | |
| 409 | + self.dm_tmp3x = tmp3x; | |
| 410 | + 0.0 | |
| 411 | + } | |
| 412 | + _ => { | |
| 413 | + y[..subframe].copy_from_slice(&tmp); | |
| 414 | + 0.0 | |
| 415 | + } | |
| 416 | + }; | |
| 417 | + self.downmix_state = st; | |
| 418 | + self.dm_tmp = tmp; | |
| 419 | + ret | |
| 420 | + } | |
| 421 | + | |
| 422 | + /// libopus `tonality_analysis()`: appends one 20 ms chunk of input and, | |
| 423 | + /// once 30 ms have accumulated, emits one window's [`AnalysisInfo`]. | |
| 424 | + #[allow(clippy::needless_range_loop)] | |
| 425 | + fn tonality_analysis(&mut self, pcm: &[f32], len: i32, offset: i32, channels: usize, lsb_depth: i32) { | |
| 426 | + if !self.initialized { | |
| 427 | + self.mem_fill = 240; | |
| 428 | + self.initialized = true; | |
| 429 | + } | |
| 430 | + let alpha = 1.0 / (10.min(1 + self.count)) as f32; | |
| 431 | + let alpha_e = 1.0 / (25.min(1 + self.count)) as f32; | |
| 432 | + // Noise floor related decay for bandwidth detection: -2.2 dB/second. | |
| 433 | + let mut alpha_e2 = 1.0 / (100.min(1 + self.count)) as f32; | |
| 434 | + if self.count <= 1 { | |
| 435 | + alpha_e2 = 1.0; | |
| 436 | + } | |
| 437 | + // len and offset are converted to the 24 kHz analysis domain. | |
| 438 | + let (len, offset) = match self.fs { | |
| 439 | + 48000 => (len / 2, offset / 2), | |
| 440 | + 16000 => (3 * len / 2, 3 * offset / 2), | |
| 441 | + _ => (len, offset), | |
| 442 | + }; | |
| 443 | + let (len, offset) = (len as usize, offset as usize); | |
| 444 | + | |
| 445 | + let take = len.min(ANALYSIS_BUF_SIZE - self.mem_fill); | |
| 446 | + let mut chunk = std::mem::take(&mut self.chunk); | |
| 447 | + chunk.clear(); | |
| 448 | + chunk.resize(take, 0.0); | |
| 449 | + let ener = self.downmix_and_resample(pcm, &mut chunk, take, offset, channels); | |
| 450 | + self.inmem[self.mem_fill..self.mem_fill + take].copy_from_slice(&chunk); | |
| 451 | + self.chunk = chunk; | |
| 452 | + self.hp_ener_accum += ener; | |
| 453 | + if self.mem_fill + len < ANALYSIS_BUF_SIZE { | |
| 454 | + self.mem_fill += len; | |
| 455 | + return; | |
| 456 | + } | |
| 457 | + let hp_ener = self.hp_ener_accum; | |
| 458 | + let write = self.write_pos; | |
| 459 | + self.write_pos += 1; | |
| 460 | + if self.write_pos >= DETECT_SIZE { | |
| 461 | + self.write_pos -= DETECT_SIZE; | |
| 462 | + } | |
| 463 | + | |
| 464 | + let is_silence = self.inmem.iter().all(|&v| v == 0.0); | |
| 465 | + | |
| 466 | + let mut re = std::mem::take(&mut self.fft_re); | |
| 467 | + let mut im = std::mem::take(&mut self.fft_im); | |
| 468 | + for i in 0..N2 { | |
| 469 | + let w = ANALYSIS_WINDOW[i]; | |
| 470 | + re[i] = w * self.inmem[i]; | |
| 471 | + im[i] = w * self.inmem[N2 + i]; | |
| 472 | + re[N - i - 1] = w * self.inmem[N - i - 1]; | |
| 473 | + im[N - i - 1] = w * self.inmem[N + N2 - i - 1]; | |
| 474 | + } | |
| 475 | + self.inmem.copy_within(ANALYSIS_BUF_SIZE - 240.., 0); | |
| 476 | + let remaining = len - (ANALYSIS_BUF_SIZE - self.mem_fill); | |
| 477 | + let mut chunk = std::mem::take(&mut self.chunk); | |
| 478 | + chunk.clear(); | |
| 479 | + chunk.resize(remaining, 0.0); | |
| 480 | + let ener = self.downmix_and_resample(pcm, &mut chunk, remaining, offset + ANALYSIS_BUF_SIZE - self.mem_fill, channels); | |
| 481 | + self.inmem[240..240 + remaining].copy_from_slice(&chunk); | |
| 482 | + self.chunk = chunk; | |
| 483 | + self.hp_ener_accum = ener; | |
| 484 | + self.mem_fill = 240 + remaining; | |
| 485 | + if is_silence { | |
| 486 | + // On silence, copy the previous analysis. | |
| 487 | + let prev = (write + DETECT_SIZE - 1) % DETECT_SIZE; | |
| 488 | + self.info[write] = self.info[prev]; | |
| 489 | + self.fft_re = re; | |
| 490 | + self.fft_im = im; | |
| 491 | + return; | |
| 492 | + } | |
| 493 | + // libopus takes `opus_fft` (forward, scaled by 1/N); ours is the | |
| 494 | + // unscaled conjugate transform, so conjugate in and out and scale. | |
| 495 | + for v in im.iter_mut() { | |
| 496 | + *v = -*v; | |
| 497 | + } | |
| 498 | + self.fft.inverse(&mut re, &mut im); | |
| 499 | + let s = 1.0 / N as f32; | |
| 500 | + for i in 0..N { | |
| 501 | + re[i] *= s; | |
| 502 | + im[i] *= -s; | |
| 503 | + } | |
| 504 | + if re[0].is_nan() { | |
| 505 | + self.info[write].valid = false; | |
| 506 | + self.fft_re = re; | |
| 507 | + self.fft_im = im; | |
| 508 | + return; | |
| 509 | + } | |
| 510 | + | |
| 511 | + let mut tonality = [0.0f32; N2]; | |
| 512 | + let mut tonality2 = [0.0f32; N2]; | |
| 513 | + let mut noisiness = [0.0f32; N2]; | |
| 514 | + let pi4 = (core::f64::consts::PI.powi(4)) as f32; | |
| 515 | + for i in 1..N2 { | |
| 516 | + let x1r = re[i] + re[N - i]; | |
| 517 | + let x1i = im[i] - im[N - i]; | |
| 518 | + let x2r = im[i] + im[N - i]; | |
| 519 | + let x2i = re[N - i] - re[i]; | |
| 520 | + | |
| 521 | + let angle = (0.5 / core::f32::consts::PI) * fast_atan2f(x1i, x1r); | |
| 522 | + let d_angle = angle - self.angle[i]; | |
| 523 | + let d2_angle = d_angle - self.d_angle[i]; | |
| 524 | + | |
| 525 | + let angle2 = (0.5 / core::f32::consts::PI) * fast_atan2f(x2i, x2r); | |
| 526 | + let d_angle2 = angle2 - angle; | |
| 527 | + let d2_angle2 = d_angle2 - d_angle; | |
| 528 | + | |
| 529 | + let mut mod1 = d2_angle - d2_angle.round_ties_even(); | |
| 530 | + noisiness[i] = mod1.abs(); | |
| 531 | + mod1 *= mod1; | |
| 532 | + mod1 *= mod1; | |
| 533 | + | |
| 534 | + let mut mod2 = d2_angle2 - d2_angle2.round_ties_even(); | |
| 535 | + noisiness[i] += mod2.abs(); | |
| 536 | + mod2 *= mod2; | |
| 537 | + mod2 *= mod2; | |
| 538 | + | |
| 539 | + let avg_mod = 0.25 * (self.d2_angle[i] + mod1 + 2.0 * mod2); | |
| 540 | + // This introduces an extra delay of 2 frames in the detection. | |
| 541 | + tonality[i] = 1.0 / (1.0 + 40.0 * 16.0 * pi4 * avg_mod) - 0.015; | |
| 542 | + // No delay on this detection, but it's less reliable. | |
| 543 | + tonality2[i] = 1.0 / (1.0 + 40.0 * 16.0 * pi4 * mod2) - 0.015; | |
| 544 | + | |
| 545 | + self.angle[i] = angle2; | |
| 546 | + self.d_angle[i] = d_angle2; | |
| 547 | + self.d2_angle[i] = mod2; | |
| 548 | + } | |
| 549 | + for i in 2..N2 - 1 { | |
| 550 | + let tt = tonality2[i].min(tonality2[i - 1].max(tonality2[i + 1])); | |
| 551 | + tonality[i] = 0.9 * tonality[i].max(tt - 0.1); | |
| 552 | + } | |
| 553 | + let mut frame_tonality = 0.0f32; | |
| 554 | + let mut max_frame_tonality = 0.0f32; | |
| 555 | + let activity; | |
| 556 | + let mut frame_noisiness = 0.0f32; | |
| 557 | + let mut frame_stationarity = 0.0f32; | |
| 558 | + if self.count == 0 { | |
| 559 | + for b in 0..NB_TBANDS { | |
| 560 | + self.low_e[b] = 1e10; | |
| 561 | + self.high_e[b] = -1e10; | |
| 562 | + } | |
| 563 | + } | |
| 564 | + let mut relative_e = 0.0f32; | |
| 565 | + let mut frame_loudness = 0.0f32; | |
| 566 | + let mut band_log2 = [0.0f32; NB_TBANDS + 1]; | |
| 567 | + let mut log_e = [0.0f32; NB_TBANDS]; | |
| 568 | + let bin_e = |re: &[f32], im: &[f32], i: usize| { | |
| 569 | + re[i] * re[i] + re[N - i] * re[N - i] + im[i] * im[i] + im[N - i] * im[N - i] | |
| 570 | + }; | |
| 571 | + // The energy of the very first band is special because of DC. | |
| 572 | + { | |
| 573 | + let x1r = 2.0 * re[0]; | |
| 574 | + let x2r = 2.0 * im[0]; | |
| 575 | + let mut e = x1r * x1r + x2r * x2r; | |
| 576 | + for i in 1..4 { | |
| 577 | + e += bin_e(&re, &im, i); | |
| 578 | + } | |
| 579 | + band_log2[0] = 0.5 * 1.442_695 * (e + 1e-10).ln(); | |
| 580 | + } | |
| 581 | + let mut band_tonality = [0.0f32; NB_TBANDS]; | |
| 582 | + let mut slope = 0.0f32; | |
| 583 | + for b in 0..NB_TBANDS { | |
| 584 | + let (mut e, mut te, mut ne) = (0.0f32, 0.0f32, 0.0f32); | |
| 585 | + for i in TBANDS[b]..TBANDS[b + 1] { | |
| 586 | + let be = bin_e(&re, &im, i); | |
| 587 | + e += be; | |
| 588 | + te += be * tonality[i].max(0.0); | |
| 589 | + ne += be * 2.0 * (0.5 - noisiness[i]); | |
| 590 | + } | |
| 591 | + // Check for extreme band energies that could cause NaNs later. | |
| 592 | + if !(e < 1e9) || e.is_nan() { | |
| 593 | + self.info[write].valid = false; | |
| 594 | + self.fft_re = re; | |
| 595 | + self.fft_im = im; | |
| 596 | + return; | |
| 597 | + } | |
| 598 | + self.e[self.e_count][b] = e; | |
| 599 | + frame_noisiness += ne / (1e-15 + e); | |
| 600 | + frame_loudness += (e + 1e-10).sqrt(); | |
| 601 | + log_e[b] = (e + 1e-10).ln(); | |
| 602 | + band_log2[b + 1] = 0.5 * 1.442_695 * (e + 1e-10).ln(); | |
| 603 | + self.log_e[self.e_count][b] = log_e[b]; | |
| 604 | + if self.count == 0 { | |
| 605 | + self.high_e[b] = log_e[b]; | |
| 606 | + self.low_e[b] = log_e[b]; | |
| 607 | + } | |
| 608 | + if self.high_e[b] > self.low_e[b] + 7.5 { | |
| 609 | + if self.high_e[b] - log_e[b] > log_e[b] - self.low_e[b] { | |
| 610 | + self.high_e[b] -= 0.01; | |
| 611 | + } else { | |
| 612 | + self.low_e[b] += 0.01; | |
| 613 | + } | |
| 614 | + } | |
| 615 | + if log_e[b] > self.high_e[b] { | |
| 616 | + self.high_e[b] = log_e[b]; | |
| 617 | + self.low_e[b] = (self.high_e[b] - 15.0).max(self.low_e[b]); | |
| 618 | + } else if log_e[b] < self.low_e[b] { | |
| 619 | + self.low_e[b] = log_e[b]; | |
| 620 | + self.high_e[b] = (self.low_e[b] + 15.0).min(self.high_e[b]); | |
| 621 | + } | |
| 622 | + relative_e += (log_e[b] - self.low_e[b]) / (1e-5 + (self.high_e[b] - self.low_e[b])); | |
| 623 | + | |
| 624 | + let mut l1 = 0.0f32; | |
| 625 | + let mut l2 = 0.0f32; | |
| 626 | + for i in 0..NB_FRAMES { | |
| 627 | + l1 += self.e[i][b].sqrt(); | |
| 628 | + l2 += self.e[i][b]; | |
| 629 | + } | |
| 630 | + let mut stationarity = 0.99f32.min(l1 / (1e-15 + NB_FRAMES as f32 * l2).sqrt()); | |
| 631 | + stationarity *= stationarity; | |
| 632 | + stationarity *= stationarity; | |
| 633 | + frame_stationarity += stationarity; | |
| 634 | + band_tonality[b] = (te / (1e-15 + e)).max(stationarity * self.prev_band_tonality[b]); | |
| 635 | + frame_tonality += band_tonality[b]; | |
| 636 | + if b >= NB_TBANDS - NB_TONAL_SKIP_BANDS { | |
| 637 | + frame_tonality -= band_tonality[b - NB_TBANDS + NB_TONAL_SKIP_BANDS]; | |
| 638 | + } | |
| 639 | + max_frame_tonality = | |
| 640 | + max_frame_tonality.max((1.0 + 0.03 * (b as f32 - NB_TBANDS as f32)) * frame_tonality); | |
| 641 | + slope += band_tonality[b] * (b as f32 - 8.0); | |
| 642 | + self.prev_band_tonality[b] = band_tonality[b]; | |
| 643 | + } | |
| 644 | + | |
| 645 | + let mut leakage_from = [0.0f32; NB_TBANDS + 1]; | |
| 646 | + let mut leakage_to = [0.0f32; NB_TBANDS + 1]; | |
| 647 | + leakage_from[0] = band_log2[0]; | |
| 648 | + leakage_to[0] = band_log2[0] - LEAKAGE_OFFSET; | |
| 649 | + for b in 1..NB_TBANDS + 1 { | |
| 650 | + let leak_slope = LEAKAGE_SLOPE * (TBANDS[b] - TBANDS[b - 1]) as f32 / 4.0; | |
| 651 | + leakage_from[b] = (leakage_from[b - 1] + leak_slope).min(band_log2[b]); | |
| 652 | + leakage_to[b] = (leakage_to[b - 1] - leak_slope).max(band_log2[b] - LEAKAGE_OFFSET); | |
| 653 | + } | |
| 654 | + for b in (0..NB_TBANDS - 1).rev() { | |
| 655 | + let leak_slope = LEAKAGE_SLOPE * (TBANDS[b + 1] - TBANDS[b]) as f32 / 4.0; | |
| 656 | + leakage_from[b] = (leakage_from[b + 1] + leak_slope).min(leakage_from[b]); | |
| 657 | + leakage_to[b] = (leakage_to[b + 1] - leak_slope).max(leakage_to[b]); | |
| 658 | + } | |
| 659 | + let mut leak_boost = [0u8; LEAK_BANDS]; | |
| 660 | + for b in 0..NB_TBANDS + 1 { | |
| 661 | + let boost = (leakage_to[b] - band_log2[b]).max(0.0) | |
| 662 | + + (band_log2[b] - (leakage_from[b] + LEAKAGE_OFFSET)).max(0.0); | |
| 663 | + leak_boost[b] = (0.5 + 64.0 * boost).floor().min(255.0) as u8; | |
| 664 | + } | |
| 665 | + | |
| 666 | + let mut spec_variability = 0.0f32; | |
| 667 | + for i in 0..NB_FRAMES { | |
| 668 | + let mut mindist = 1e15f32; | |
| 669 | + for j in 0..NB_FRAMES { | |
| 670 | + let mut dist = 0.0f32; | |
| 671 | + for k in 0..NB_TBANDS { | |
| 672 | + let tmp = self.log_e[i][k] - self.log_e[j][k]; | |
| 673 | + dist += tmp * tmp; | |
| 674 | + } | |
| 675 | + if j != i { | |
| 676 | + mindist = mindist.min(dist); | |
| 677 | + } | |
| 678 | + } | |
| 679 | + spec_variability += mindist; | |
| 680 | + } | |
| 681 | + spec_variability = (spec_variability / NB_FRAMES as f32 / NB_TBANDS as f32).sqrt(); | |
| 682 | + let mut bandwidth_mask = 0.0f32; | |
| 683 | + let mut bandwidth = 0i32; | |
| 684 | + let mut max_e = 0.0f32; | |
| 685 | + let mut noise_floor = 5.7e-4 / (1 << 0.max(lsb_depth - 8)) as f32; | |
| 686 | + noise_floor *= noise_floor; | |
| 687 | + let mut below_max_pitch = 0.0f32; | |
| 688 | + let mut above_max_pitch = 0.0f32; | |
| 689 | + let mut is_masked = [false; NB_TBANDS + 1]; | |
| 690 | + for b in 0..NB_TBANDS { | |
| 691 | + let band_start = TBANDS[b]; | |
| 692 | + let band_end = TBANDS[b + 1]; | |
| 693 | + let mut e = 0.0f32; | |
| 694 | + for i in band_start..band_end { | |
| 695 | + e += bin_e(&re, &im, i); | |
| 696 | + } | |
| 697 | + max_e = max_e.max(e); | |
| 698 | + if band_start < 64 { | |
| 699 | + below_max_pitch += e; | |
| 700 | + } else { | |
| 701 | + above_max_pitch += e; | |
| 702 | + } | |
| 703 | + self.mean_e[b] = ((1.0 - alpha_e2) * self.mean_e[b]).max(e); | |
| 704 | + let em = e.max(self.mean_e[b]); | |
| 705 | + let width = (band_end - band_start) as f32; | |
| 706 | + if e * 1e9 > max_e && (em > 3.0 * noise_floor * width || e > noise_floor * width) { | |
| 707 | + bandwidth = b as i32 + 1; | |
| 708 | + } | |
| 709 | + is_masked[b] = e < if self.prev_bandwidth >= b as i32 + 1 { 0.01 } else { 0.05 } * bandwidth_mask; | |
| 710 | + bandwidth_mask = (0.05 * bandwidth_mask).max(e); | |
| 711 | + } | |
| 712 | + // The last two bands have no spectrum, only the energy above 12 kHz. | |
| 713 | + if self.fs == 48000 { | |
| 714 | + let b = NB_TBANDS; | |
| 715 | + let e = hp_ener * (1.0 / (60.0 * 60.0)); | |
| 716 | + let noise_ratio = if self.prev_bandwidth == 20 { 10.0 } else { 30.0 }; | |
| 717 | + above_max_pitch += e; | |
| 718 | + self.mean_e[b] = ((1.0 - alpha_e2) * self.mean_e[b]).max(e); | |
| 719 | + let em = e.max(self.mean_e[b]); | |
| 720 | + if em > 3.0 * noise_ratio * noise_floor * 160.0 || e > noise_ratio * noise_floor * 160.0 { | |
| 721 | + bandwidth = 20; | |
| 722 | + } | |
| 723 | + is_masked[b] = e < if self.prev_bandwidth == 20 { 0.01 } else { 0.05 } * bandwidth_mask; | |
| 724 | + } | |
| 725 | + let max_pitch_ratio = if above_max_pitch > below_max_pitch { | |
| 726 | + below_max_pitch / above_max_pitch | |
| 727 | + } else { | |
| 728 | + 1.0 | |
| 729 | + }; | |
| 730 | + // Resampling aliasing can leave energy in the first band being cut. | |
| 731 | + if bandwidth == 20 && is_masked[NB_TBANDS] { | |
| 732 | + bandwidth -= 2; | |
| 733 | + } else if bandwidth > 0 && bandwidth <= NB_TBANDS as i32 && is_masked[bandwidth as usize - 1] { | |
| 734 | + bandwidth -= 1; | |
| 735 | + } | |
| 736 | + if self.count <= 2 { | |
| 737 | + bandwidth = 20; | |
| 738 | + } | |
| 739 | + frame_loudness = 20.0 * frame_loudness.log10(); | |
| 740 | + self.e_tracker = (self.e_tracker - 0.003).max(frame_loudness); | |
| 741 | + self.low_e_count *= 1.0 - alpha_e; | |
| 742 | + if frame_loudness < self.e_tracker - 30.0 { | |
| 743 | + self.low_e_count += alpha_e; | |
| 744 | + } | |
| 745 | + | |
| 746 | + let mut bfcc = [0.0f32; 8]; | |
| 747 | + let mut mid_e = [0.0f32; 8]; | |
| 748 | + for i in 0..8 { | |
| 749 | + let mut sum = 0.0f32; | |
| 750 | + for b in 0..16 { | |
| 751 | + sum += DCT_TABLE[i * 16 + b] * log_e[b]; | |
| 752 | + } | |
| 753 | + bfcc[i] = sum; | |
| 754 | + } | |
| 755 | + for i in 0..8 { | |
| 756 | + let mut sum = 0.0f32; | |
| 757 | + for b in 0..16 { | |
| 758 | + sum += DCT_TABLE[i * 16 + b] * 0.5 * (self.high_e[b] + self.low_e[b]); | |
| 759 | + } | |
| 760 | + mid_e[i] = sum; | |
| 761 | + } | |
| 762 | + | |
| 763 | + frame_stationarity /= NB_TBANDS as f32; | |
| 764 | + relative_e /= NB_TBANDS as f32; | |
| 765 | + if self.count < 10 { | |
| 766 | + relative_e = 0.5; | |
| 767 | + } | |
| 768 | + frame_noisiness /= NB_TBANDS as f32; | |
| 769 | + activity = frame_noisiness + (1.0 - frame_noisiness) * relative_e; | |
| 770 | + let mut frame_tonality = max_frame_tonality / (NB_TBANDS - NB_TONAL_SKIP_BANDS) as f32; | |
| 771 | + frame_tonality = frame_tonality.max(self.prev_tonality * 0.8); | |
| 772 | + self.prev_tonality = frame_tonality; | |
| 773 | + slope /= 8.0 * 8.0; | |
| 774 | + let tonality_slope = slope; | |
| 775 | + | |
| 776 | + self.e_count = (self.e_count + 1) % NB_FRAMES; | |
| 777 | + self.count = (self.count + 1).min(ANALYSIS_COUNT_MAX); | |
| 778 | + | |
| 779 | + let mut features = [0.0f32; 25]; | |
| 780 | + for i in 0..4 { | |
| 781 | + features[i] = -0.122_99 * (bfcc[i] + self.mem[i + 24]) | |
| 782 | + + 0.491_95 * (self.mem[i] + self.mem[i + 16]) | |
| 783 | + + 0.696_93 * self.mem[i + 8] | |
| 784 | + - 1.434_9 * self.cmean[i]; | |
| 785 | + } | |
| 786 | + for i in 0..4 { | |
| 787 | + self.cmean[i] = (1.0 - alpha) * self.cmean[i] + alpha * bfcc[i]; | |
| 788 | + } | |
| 789 | + for i in 0..4 { | |
| 790 | + features[4 + i] = | |
| 791 | + 0.632_46 * (bfcc[i] - self.mem[i + 24]) + 0.316_23 * (self.mem[i] - self.mem[i + 16]); | |
| 792 | + } | |
| 793 | + for i in 0..3 { | |
| 794 | + features[8 + i] = 0.534_52 * (bfcc[i] + self.mem[i + 24]) | |
| 795 | + - 0.267_26 * (self.mem[i] + self.mem[i + 16]) | |
| 796 | + - 0.534_52 * self.mem[i + 8]; | |
| 797 | + } | |
| 798 | + if self.count > 5 { | |
| 799 | + for i in 0..9 { | |
| 800 | + self.std[i] = (1.0 - alpha) * self.std[i] + alpha * features[i] * features[i]; | |
| 801 | + } | |
| 802 | + } | |
| 803 | + for i in 0..4 { | |
| 804 | + features[i] = bfcc[i] - mid_e[i]; | |
| 805 | + } | |
| 806 | + for i in 0..8 { | |
| 807 | + self.mem[i + 24] = self.mem[i + 16]; | |
| 808 | + self.mem[i + 16] = self.mem[i + 8]; | |
| 809 | + self.mem[i + 8] = self.mem[i]; | |
| 810 | + self.mem[i] = bfcc[i]; | |
| 811 | + } | |
| 812 | + for i in 0..9 { | |
| 813 | + features[11 + i] = self.std[i].sqrt() - STD_FEATURE_BIAS[i]; | |
| 814 | + } | |
| 815 | + features[18] = spec_variability - 0.78; | |
| 816 | + features[20] = frame_tonality - 0.154_723; | |
| 817 | + features[21] = activity - 0.724_643; | |
| 818 | + features[22] = frame_stationarity - 0.743_717; | |
| 819 | + features[23] = tonality_slope + 0.069_216; | |
| 820 | + features[24] = self.low_e_count - 0.067_930; | |
| 821 | + | |
| 822 | + let mut layer_out = [0.0f32; MAX_NEURONS]; | |
| 823 | + let mut frame_probs = [0.0f32; 2]; | |
| 824 | + compute_dense(&LAYER0_BIAS, &LAYER0_WEIGHTS, 25, 32, false, &mut layer_out, &features); | |
| 825 | + let mut state = self.rnn_state; | |
| 826 | + compute_gru(&LAYER1_BIAS, &LAYER1_WEIGHTS, &LAYER1_RECUR_WEIGHTS, 32, 24, &mut state, &layer_out); | |
| 827 | + self.rnn_state = state; | |
| 828 | + compute_dense(&LAYER2_BIAS, &LAYER2_WEIGHTS, 24, 2, true, &mut frame_probs, &self.rnn_state); | |
| 829 | + | |
| 830 | + self.prev_bandwidth = bandwidth; | |
| 831 | + self.info[write] = AnalysisInfo { | |
| 832 | + valid: true, | |
| 833 | + tonality: frame_tonality, | |
| 834 | + tonality_slope, | |
| 835 | + noisiness: frame_noisiness, | |
| 836 | + activity, | |
| 837 | + music_prob: frame_probs[0], | |
| 838 | + music_prob_min: 0.0, | |
| 839 | + music_prob_max: 0.0, | |
| 840 | + activity_probability: frame_probs[1], | |
| 841 | + max_pitch_ratio, | |
| 842 | + bandwidth, | |
| 843 | + leak_boost, | |
| 844 | + }; | |
| 845 | + self.fft_re = re; | |
| 846 | + self.fft_im = im; | |
| 847 | + } | |
| 848 | + | |
| 849 | + /// libopus `tonality_get_info()`: picks the window that covers the frame | |
| 850 | + /// about to be coded and smooths `music_prob` over the look-ahead. | |
| 851 | + fn get_info(&mut self, info_out: &mut AnalysisInfo, len: i32) { | |
| 852 | + let mut pos = self.read_pos; | |
| 853 | + let mut curr_lookahead = self.write_pos as i32 - self.read_pos as i32; | |
| 854 | + if curr_lookahead < 0 { | |
| 855 | + curr_lookahead += DETECT_SIZE as i32; | |
| 856 | + } | |
| 857 | + self.read_subframe += len / (self.fs as i32 / 400); | |
| 858 | + while self.read_subframe >= 8 { | |
| 859 | + self.read_subframe -= 8; | |
| 860 | + self.read_pos += 1; | |
| 861 | + } | |
| 862 | + if self.read_pos >= DETECT_SIZE { | |
| 863 | + self.read_pos -= DETECT_SIZE; | |
| 864 | + } | |
| 865 | + // On long frames, look at the second analysis window rather than the first. | |
| 866 | + if len > self.fs as i32 / 50 && pos != self.write_pos { | |
| 867 | + pos += 1; | |
| 868 | + if pos == DETECT_SIZE { | |
| 869 | + pos = 0; | |
| 870 | + } | |
| 871 | + } | |
| 872 | + if pos == self.write_pos { | |
| 873 | + pos = (pos + DETECT_SIZE - 1) % DETECT_SIZE; | |
| 874 | + } | |
| 875 | + let pos0 = pos; | |
| 876 | + *info_out = self.info[pos]; | |
| 877 | + if !info_out.valid { | |
| 878 | + return; | |
| 879 | + } | |
| 880 | + let mut tonality_max = info_out.tonality; | |
| 881 | + let mut tonality_avg = info_out.tonality; | |
| 882 | + let mut tonality_count = 1; | |
| 883 | + // Look at the neighbouring frames and pick the largest bandwidth found. | |
| 884 | + let mut bandwidth_span = 6; | |
| 885 | + for _ in 0..3 { | |
| 886 | + pos += 1; | |
| 887 | + if pos == DETECT_SIZE { | |
| 888 | + pos = 0; | |
| 889 | + } | |
| 890 | + if pos == self.write_pos { | |
| 891 | + break; | |
| 892 | + } | |
| 893 | + tonality_max = tonality_max.max(self.info[pos].tonality); | |
| 894 | + tonality_avg += self.info[pos].tonality; | |
| 895 | + tonality_count += 1; | |
| 896 | + info_out.bandwidth = info_out.bandwidth.max(self.info[pos].bandwidth); | |
| 897 | + bandwidth_span -= 1; | |
| 898 | + } | |
| 899 | + pos = pos0; | |
| 900 | + for _ in 0..bandwidth_span { | |
| 901 | + pos = (pos + DETECT_SIZE - 1) % DETECT_SIZE; | |
| 902 | + if pos == self.write_pos { | |
| 903 | + break; | |
| 904 | + } | |
| 905 | + info_out.bandwidth = info_out.bandwidth.max(self.info[pos].bandwidth); | |
| 906 | + } | |
| 907 | + info_out.tonality = (tonality_avg / tonality_count as f32).max(tonality_max - 0.2); | |
| 908 | + | |
| 909 | + let mut mpos = pos0; | |
| 910 | + let mut vpos = pos0; | |
| 911 | + // With enough look-ahead, compensate for the ~5-frame delay in the | |
| 912 | + // music prob and the ~1 frame delay in the VAD prob. | |
| 913 | + if curr_lookahead > 15 { | |
| 914 | + mpos = (mpos + 5) % DETECT_SIZE; | |
| 915 | + vpos = (vpos + 1) % DETECT_SIZE; | |
| 916 | + } | |
| 917 | + // The "badness function" minimisation of libopus's comment block: what | |
| 918 | + // is computed is the threshold at which switching now is optimal. | |
| 919 | + let mut prob_min = 1.0f32; | |
| 920 | + let mut prob_max = 0.0f32; | |
| 921 | + let vad_prob = self.info[vpos].activity_probability; | |
| 922 | + let mut prob_count = 0.1f32.max(vad_prob); | |
| 923 | + let mut prob_avg = 0.1f32.max(vad_prob) * self.info[mpos].music_prob; | |
| 924 | + loop { | |
| 925 | + mpos = (mpos + 1) % DETECT_SIZE; | |
| 926 | + if mpos == self.write_pos { | |
| 927 | + break; | |
| 928 | + } | |
| 929 | + vpos = (vpos + 1) % DETECT_SIZE; | |
| 930 | + if vpos == self.write_pos { | |
| 931 | + break; | |
| 932 | + } | |
| 933 | + let pos_vad = self.info[vpos].activity_probability; | |
| 934 | + prob_min = ((prob_avg - TRANSITION_PENALTY * (vad_prob - pos_vad)) / prob_count).min(prob_min); | |
| 935 | + prob_max = ((prob_avg + TRANSITION_PENALTY * (vad_prob - pos_vad)) / prob_count).max(prob_max); | |
| 936 | + prob_count += 0.1f32.max(pos_vad); | |
| 937 | + prob_avg += 0.1f32.max(pos_vad) * self.info[mpos].music_prob; | |
| 938 | + } | |
| 939 | + info_out.music_prob = prob_avg / prob_count; | |
| 940 | + prob_min = (prob_avg / prob_count).min(prob_min); | |
| 941 | + prob_max = (prob_avg / prob_count).max(prob_max); | |
| 942 | + prob_min = prob_min.max(0.0); | |
| 943 | + prob_max = prob_max.min(1.0); | |
| 944 | + | |
| 945 | + // Without enough look-ahead, do our best to make a decent decision. | |
| 946 | + if curr_lookahead < 10 { | |
| 947 | + let mut pmin = prob_min; | |
| 948 | + let mut pmax = prob_max; | |
| 949 | + pos = pos0; | |
| 950 | + for _ in 0..(self.count - 1).min(15) { | |
| 951 | + pos = (pos + DETECT_SIZE - 1) % DETECT_SIZE; | |
| 952 | + pmin = pmin.min(self.info[pos].music_prob); | |
| 953 | + pmax = pmax.max(self.info[pos].music_prob); | |
| 954 | + } | |
| 955 | + // Bias against switching on active audio. | |
| 956 | + pmin = 0.0f32.max(pmin - 0.1 * vad_prob); | |
| 957 | + pmax = 1.0f32.min(pmax + 0.1 * vad_prob); | |
| 958 | + prob_min += (1.0 - 0.1 * curr_lookahead as f32) * (pmin - prob_min); | |
| 959 | + prob_max += (1.0 - 0.1 * curr_lookahead as f32) * (pmax - prob_max); | |
| 960 | + } | |
| 961 | + info_out.music_prob_min = prob_min; | |
| 962 | + info_out.music_prob_max = prob_max; | |
| 963 | + } | |
| 964 | +} | |
| 965 | + | |
| 966 | +// --------------------------------------------------------------------------- | |
| 967 | +// Tables copied verbatim from libopus v1.6: `src/analysis.c` (`dct_table`, | |
| 968 | +// `analysis_window`, `std_feature_bias`) and `src/mlp_data.c` (the Keras- | |
| 969 | +// generated `i8` weights of the 25x32 dense layer, the 24-unit GRU and the | |
| 970 | +// 2-output sigmoid layer). They have no closed form; do not "simplify" them. | |
| 971 | +// --------------------------------------------------------------------------- | |
| 972 | + | |
| 973 | +static DCT_TABLE: [f32; 128] = [ | |
| 974 | + 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, | |
| 975 | + 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, 0.250000, | |
| 976 | + 0.351851, 0.338330, 0.311806, 0.273300, 0.224292, 0.166664, 0.102631, 0.034654, | |
| 977 | + -0.034654, -0.102631, -0.166664, -0.224292, -0.273300, -0.311806, -0.338330, -0.351851, | |
| 978 | + 0.346760, 0.293969, 0.196424, 0.068975, -0.068975, -0.196424, -0.293969, -0.346760, | |
| 979 | + -0.346760, -0.293969, -0.196424, -0.068975, 0.068975, 0.196424, 0.293969, 0.346760, | |
| 980 | + 0.338330, 0.224292, 0.034654, -0.166664, -0.311806, -0.351851, -0.273300, -0.102631, | |
| 981 | + 0.102631, 0.273300, 0.351851, 0.311806, 0.166664, -0.034654, -0.224292, -0.338330, | |
| 982 | + 0.326641, 0.135299, -0.135299, -0.326641, -0.326641, -0.135299, 0.135299, 0.326641, | |
| 983 | + 0.326641, 0.135299, -0.135299, -0.326641, -0.326641, -0.135299, 0.135299, 0.326641, | |
| 984 | + 0.311806, 0.034654, -0.273300, -0.338330, -0.102631, 0.224292, 0.351851, 0.166664, | |
| 985 | + -0.166664, -0.351851, -0.224292, 0.102631, 0.338330, 0.273300, -0.034654, -0.311806, | |
| 986 | + 0.293969, -0.068975, -0.346760, -0.196424, 0.196424, 0.346760, 0.068975, -0.293969, | |
| 987 | + -0.293969, 0.068975, 0.346760, 0.196424, -0.196424, -0.346760, -0.068975, 0.293969, | |
| 988 | + 0.273300, -0.166664, -0.338330, 0.034654, 0.351851, 0.102631, -0.311806, -0.224292, | |
| 989 | + 0.224292, 0.311806, -0.102631, -0.351851, -0.034654, 0.338330, 0.166664, -0.273300, | |
| 990 | +]; | |
| 991 | + | |
| 992 | +static ANALYSIS_WINDOW: [f32; 240] = [ | |
| 993 | + 0.000043, 0.000171, 0.000385, 0.000685, 0.001071, 0.001541, 0.002098, 0.002739, | |
| 994 | + 0.003466, 0.004278, 0.005174, 0.006156, 0.007222, 0.008373, 0.009607, 0.010926, | |
| 995 | + 0.012329, 0.013815, 0.015385, 0.017037, 0.018772, 0.020590, 0.022490, 0.024472, | |
| 996 | + 0.026535, 0.028679, 0.030904, 0.033210, 0.035595, 0.038060, 0.040604, 0.043227, | |
| 997 | + 0.045928, 0.048707, 0.051564, 0.054497, 0.057506, 0.060591, 0.063752, 0.066987, | |
| 998 | + 0.070297, 0.073680, 0.077136, 0.080665, 0.084265, 0.087937, 0.091679, 0.095492, | |
| 999 | + 0.099373, 0.103323, 0.107342, 0.111427, 0.115579, 0.119797, 0.124080, 0.128428, | |
| 1000 | + 0.132839, 0.137313, 0.141849, 0.146447, 0.151105, 0.155823, 0.160600, 0.165435, | |
| 1001 | + 0.170327, 0.175276, 0.180280, 0.185340, 0.190453, 0.195619, 0.200838, 0.206107, | |
| 1002 | + 0.211427, 0.216797, 0.222215, 0.227680, 0.233193, 0.238751, 0.244353, 0.250000, | |
| 1003 | + 0.255689, 0.261421, 0.267193, 0.273005, 0.278856, 0.284744, 0.290670, 0.296632, | |
| 1004 | + 0.302628, 0.308658, 0.314721, 0.320816, 0.326941, 0.333097, 0.339280, 0.345492, | |
| 1005 | + 0.351729, 0.357992, 0.364280, 0.370590, 0.376923, 0.383277, 0.389651, 0.396044, | |
| 1006 | + 0.402455, 0.408882, 0.415325, 0.421783, 0.428254, 0.434737, 0.441231, 0.447736, | |
| 1007 | + 0.454249, 0.460770, 0.467298, 0.473832, 0.480370, 0.486912, 0.493455, 0.500000, | |
| 1008 | + 0.506545, 0.513088, 0.519630, 0.526168, 0.532702, 0.539230, 0.545751, 0.552264, | |
| 1009 | + 0.558769, 0.565263, 0.571746, 0.578217, 0.584675, 0.591118, 0.597545, 0.603956, | |
| 1010 | + 0.610349, 0.616723, 0.623077, 0.629410, 0.635720, 0.642008, 0.648271, 0.654508, | |
| 1011 | + 0.660720, 0.666903, 0.673059, 0.679184, 0.685279, 0.691342, 0.697372, 0.703368, | |
| 1012 | + 0.709330, 0.715256, 0.721144, 0.726995, 0.732807, 0.738579, 0.744311, 0.750000, | |
| 1013 | + 0.755647, 0.761249, 0.766807, 0.772320, 0.777785, 0.783203, 0.788573, 0.793893, | |
| 1014 | + 0.799162, 0.804381, 0.809547, 0.814660, 0.819720, 0.824724, 0.829673, 0.834565, | |
| 1015 | + 0.839400, 0.844177, 0.848895, 0.853553, 0.858151, 0.862687, 0.867161, 0.871572, | |
| 1016 | + 0.875920, 0.880203, 0.884421, 0.888573, 0.892658, 0.896677, 0.900627, 0.904508, | |
| 1017 | + 0.908321, 0.912063, 0.915735, 0.919335, 0.922864, 0.926320, 0.929703, 0.933013, | |
| 1018 | + 0.936248, 0.939409, 0.942494, 0.945503, 0.948436, 0.951293, 0.954072, 0.956773, | |
| 1019 | + 0.959396, 0.961940, 0.964405, 0.966790, 0.969096, 0.971321, 0.973465, 0.975528, | |
| 1020 | + 0.977510, 0.979410, 0.981228, 0.982963, 0.984615, 0.986185, 0.987671, 0.989074, | |
| 1021 | + 0.990393, 0.991627, 0.992778, 0.993844, 0.994826, 0.995722, 0.996534, 0.997261, | |
| 1022 | + 0.997902, 0.998459, 0.998929, 0.999315, 0.999615, 0.999829, 0.999957, 1.000000, | |
| 1023 | +]; | |
| 1024 | + | |
| 1025 | +static STD_FEATURE_BIAS: [f32; 9] = [ | |
| 1026 | + 5.684947, 3.475288, 1.770634, 1.599784, 3.773215, | |
| 1027 | + 2.163313, 1.260756, 1.116868, 1.918795, | |
| 1028 | +]; | |
| 1029 | + | |
| 1030 | +static LAYER0_WEIGHTS: [i8; 800] = [ | |
| 1031 | + -30, -9, 2, -12, 5, -1, 8, 9, | |
| 1032 | + 9, 8, -13, 18, -17, -34, -5, 17, | |
| 1033 | + -11, 0, -4, 10, 2, 10, 15, -8, | |
| 1034 | + 2, -1, 0, 5, 13, -3, -16, 1, | |
| 1035 | + -5, 3, 7, -28, -13, 6, 36, -3, | |
| 1036 | + 19, -60, -17, -28, 7, -11, -30, -7, | |
| 1037 | + 2, -42, -21, -3, 6, -22, 33, -9, | |
| 1038 | + 7, -30, 21, -14, 24, -11, -20, -18, | |
| 1039 | + -5, -12, 12, -49, -50, -49, 16, 9, | |
| 1040 | + -37, -1, 9, 34, -13, -31, -31, 12, | |
| 1041 | + 16, 44, -42, 2, -9, 8, -18, -6, | |
| 1042 | + 9, 36, 19, 11, 13, 12, -21, 3, | |
| 1043 | + -28, -12, 3, 33, 25, -14, 11, 1, | |
| 1044 | + -94, -39, 18, -12, -11, -15, -7, 49, | |
| 1045 | + 52, 10, -43, 9, 57, 8, 21, -6, | |
| 1046 | + 14, -15, 44, -8, 7, -30, -13, -2, | |
| 1047 | + -9, 25, -2, -127, 18, -11, -52, 26, | |
| 1048 | + -27, 27, 10, -10, 7, 43, 6, -24, | |
| 1049 | + 41, 10, -18, -27, 10, 17, 9, 10, | |
| 1050 | + -17, -10, 20, -6, 22, 55, 35, -80, | |
| 1051 | + 36, 25, -24, -36, 15, 9, -19, 88, | |
| 1052 | + 19, 64, -51, -35, 17, 0, -7, 41, | |
| 1053 | + -16, 27, 4, 15, -1, 18, -16, 47, | |
| 1054 | + -39, -54, -8, 13, -25, -20, 102, -18, | |
| 1055 | + -5, 44, 11, -28, 71, 2, -51, -5, | |
| 1056 | + 5, 2, -83, -9, -29, 8, 21, -53, | |
| 1057 | + 58, -37, -7, 13, 38, 9, 34, -1, | |
| 1058 | + -41, 21, 4, -24, -36, -33, -21, 32, | |
| 1059 | + 75, -2, 1, -68, -1, 47, -29, 32, | |
| 1060 | + 20, 12, -65, -87, 5, 16, -12, 24, | |
| 1061 | + 40, 15, 7, 19, -26, -17, 17, 6, | |
| 1062 | + -2, -37, -30, -9, 32, -127, -39, 0, | |
| 1063 | + -31, -27, 4, -22, 23, -6, -77, 35, | |
| 1064 | + -61, 32, -37, -24, 13, -11, -1, -40, | |
| 1065 | + -3, 17, -7, 13, 11, 59, -19, 10, | |
| 1066 | + 6, -18, 0, 13, 3, -6, -23, 19, | |
| 1067 | + 11, -17, 13, -1, -80, 40, -53, 69, | |
| 1068 | + -29, -54, 0, -4, 33, -25, -2, 38, | |
| 1069 | + 35, 36, -15, 46, 2, -13, -16, -8, | |
| 1070 | + -8, 12, -24, -9, -55, -5, -9, 32, | |
| 1071 | + 11, 7, 12, -18, -10, -86, -38, 54, | |
| 1072 | + 37, -25, 18, -43, 7, -27, -27, -54, | |
| 1073 | + 13, 9, 22, 70, 6, 35, -7, 23, | |
| 1074 | + -15, -44, -6, 7, -66, -85, 32, 40, | |
| 1075 | + -19, -9, -7, 12, -15, 7, 2, 6, | |
| 1076 | + -35, 11, 28, 0, 26, 14, 1, 1, | |
| 1077 | + 4, 12, 18, 35, 22, -18, -3, 14, | |
| 1078 | + -1, 7, 14, -8, -14, -3, 4, -3, | |
| 1079 | + -19, -7, -1, -25, -27, 25, -26, -2, | |
| 1080 | + 33, -22, -27, -25, 4, -9, 7, 21, | |
| 1081 | + 26, -30, 10, -9, -20, 11, 27, 10, | |
| 1082 | + 5, -18, 14, -4, 2, -17, -5, -7, | |
| 1083 | + -9, -13, 15, 29, 1, -10, -16, -10, | |
| 1084 | + 35, 36, -7, -22, -44, 17, 30, 22, | |
| 1085 | + 21, -1, 22, -11, 32, -8, -7, 5, | |
| 1086 | + -10, 5, 30, -20, 29, -20, -34, 12, | |
| 1087 | + -4, -6, 6, -13, 10, -5, -68, -1, | |
| 1088 | + 24, 9, 19, -24, -64, 31, 19, 27, | |
| 1089 | + -26, 75, -45, 41, 39, -42, 8, 6, | |
| 1090 | + 23, -30, 16, -25, 30, 34, 8, -38, | |
| 1091 | + -3, 18, 16, -31, 22, -4, -9, 1, | |
| 1092 | + 20, 9, 38, -32, 0, -45, 0, -6, | |
| 1093 | + -13, 11, -25, -32, -22, 31, -24, -11, | |
| 1094 | + -11, -4, -4, 20, -34, 22, 20, 9, | |
| 1095 | + -25, 27, -5, 28, -29, 29, 6, 21, | |
| 1096 | + -6, -18, 54, 4, -46, 23, 21, -14, | |
| 1097 | + -31, 36, -41, -24, 4, 22, 10, 11, | |
| 1098 | + 7, 36, -32, -13, -52, -17, 24, 28, | |
| 1099 | + -37, -36, -1, 24, 9, -38, 35, 48, | |
| 1100 | + 18, 2, -1, 45, 10, 39, 24, -38, | |
| 1101 | + 13, 8, -16, 8, 25, 11, 7, -29, | |
| 1102 | + -11, 7, 20, -30, -38, -45, 14, -18, | |
| 1103 | + -28, -9, 65, 61, 22, -53, -38, -16, | |
| 1104 | + 36, 46, 20, -39, 32, -61, -6, -6, | |
| 1105 | + -36, -33, -18, -28, 56, 101, 45, 11, | |
| 1106 | + -28, -23, -29, -61, 20, -47, 2, 48, | |
| 1107 | + 27, -17, 1, 40, 1, 3, -51, 15, | |
| 1108 | + 35, 28, 22, 35, 53, -61, -29, 12, | |
| 1109 | + -6, -21, 10, 3, -20, 2, -25, 1, | |
| 1110 | + -6, 31, 11, -3, 1, -10, -52, 6, | |
| 1111 | + 126, -105, 122, 127, -128, 127, 127, -128, | |
| 1112 | + 127, 108, 12, 127, 48, -128, -36, -128, | |
| 1113 | + 127, 127, -128, -128, 127, 89, -128, 127, | |
| 1114 | + -128, -128, -128, 127, 127, -128, -128, -93, | |
| 1115 | + -82, 20, 125, 65, -82, 127, 38, -74, | |
| 1116 | + 81, 88, -88, 79, 51, -47, -111, -26, | |
| 1117 | + 14, 83, -88, -112, 24, 35, -101, 98, | |
| 1118 | + -99, -48, -45, 46, 83, -60, -79, 45, | |
| 1119 | + -20, -41, 9, 4, 52, 54, 93, -10, | |
| 1120 | + 4, 13, 3, 123, 6, 94, -111, -69, | |
| 1121 | + -14, -31, 10, 12, 53, -79, -11, -21, | |
| 1122 | + -2, -44, -72, 92, 65, -57, 56, -38, | |
| 1123 | + 127, -56, -128, 127, 127, -128, 86, 117, | |
| 1124 | + -75, -128, 127, -19, -99, -112, 127, -128, | |
| 1125 | + 127, -48, 114, 118, -128, -128, 117, -17, | |
| 1126 | + -6, 121, -128, 127, -128, 82, 54, -106, | |
| 1127 | + 127, 127, -33, 100, -39, -23, 18, -78, | |
| 1128 | + -34, -29, -1, -30, 127, -26, 127, -128, | |
| 1129 | + 126, -128, 27, -23, -79, -120, -127, 127, | |
| 1130 | + 72, 66, 29, 7, -66, -56, -117, -128, | |
| 1131 | +]; | |
| 1132 | + | |
| 1133 | +static LAYER0_BIAS: [i8; 32] = [ | |
| 1134 | + 51, -16, 1, 13, -5, -6, -16, -7, | |
| 1135 | + 11, -6, 106, 26, 28, -14, 21, -29, | |
| 1136 | + 7, 18, -18, -17, 21, -17, -9, 20, | |
| 1137 | + -25, -3, -34, 48, 11, -13, -31, -20, | |
| 1138 | +]; | |
| 1139 | + | |
| 1140 | +static LAYER1_WEIGHTS: [i8; 2304] = [ | |
| 1141 | + 22, -1, -7, 7, 29, -27, -31, -17, | |
| 1142 | + -13, 33, 44, -8, 11, 33, 24, 78, | |
| 1143 | + 15, 19, 30, -2, -24, 5, 49, 5, | |
| 1144 | + 36, 29, -14, -11, -48, -33, 21, -42, | |
| 1145 | + -38, -12, 55, -37, 54, -8, 1, 36, | |
| 1146 | + 17, 0, 51, 31, 59, 7, -12, 53, | |
| 1147 | + 4, 32, -14, 48, 5, -10, -16, -8, | |
| 1148 | + 1, -16, -56, -24, -6, 18, -2, 23, | |
| 1149 | + 6, 46, -6, -10, 20, 35, -44, -15, | |
| 1150 | + -49, 36, 16, 5, -7, -79, -67, 12, | |
| 1151 | + 70, -3, -79, -54, -85, -24, 47, -22, | |
| 1152 | + 33, 21, 69, -1, 11, 22, 14, -16, | |
| 1153 | + -16, -22, -28, -11, 11, -41, 31, -26, | |
| 1154 | + -33, -19, -4, 27, 32, -50, 5, -10, | |
| 1155 | + -38, -22, -8, 35, -31, 1, -41, -15, | |
| 1156 | + -11, 44, 28, -17, -41, -23, 17, 2, | |
| 1157 | + -23, -26, -13, -13, -17, 6, 14, -31, | |
| 1158 | + -25, 9, -19, 39, -8, 4, 31, -1, | |
| 1159 | + -45, -11, -28, -92, -46, -15, 21, 118, | |
| 1160 | + -22, 45, -51, 11, -20, -20, -15, 13, | |
| 1161 | + -21, -97, -29, -32, -23, -42, 94, 1, | |
| 1162 | + 23, -8, 63, -3, -46, 19, -26, 32, | |
| 1163 | + -40, -74, -26, 26, -4, -13, 30, -20, | |
| 1164 | + -30, -25, -14, -31, -45, -43, 4, -60, | |
| 1165 | + -48, -12, -34, 2, 2, 3, 13, 15, | |
| 1166 | + 11, 16, 5, 46, -9, -55, -16, -57, | |
| 1167 | + 29, 14, 38, -50, -2, -44, -11, -8, | |
| 1168 | + 52, -27, -38, -7, 20, 47, 17, -59, | |
| 1169 | + 0, 47, 46, -63, 35, -17, 19, 33, | |
| 1170 | + 68, -19, 2, 15, -16, 28, -16, -103, | |
| 1171 | + 26, -35, 47, -39, -60, 30, 31, -23, | |
| 1172 | + -52, -13, 116, 47, -25, 30, 40, 30, | |
| 1173 | + -22, 2, 12, -27, -18, 31, -10, 27, | |
| 1174 | + -8, -66, 12, 14, 4, -26, -28, -13, | |
| 1175 | + 3, 13, -26, -51, 37, 5, 2, -21, | |
| 1176 | + 47, 3, 13, 25, -41, -27, -8, -4, | |
| 1177 | + 5, -76, -33, 28, 10, 9, -46, -74, | |
| 1178 | + 19, 28, 25, 31, 54, -55, 68, 38, | |
| 1179 | + -24, -32, 2, 4, 68, 11, -1, 99, | |
| 1180 | + 5, 16, -2, -74, 40, 26, -26, 33, | |
| 1181 | + 31, -1, -68, 14, -6, 25, 9, 29, | |
| 1182 | + 60, 61, 7, -7, 0, -24, 7, 77, | |
| 1183 | + 4, -1, 16, -7, 13, -15, -19, 28, | |
| 1184 | + -31, -24, -16, 37, 24, 13, 30, 10, | |
| 1185 | + -30, 11, 11, -10, 22, 60, 28, 45, | |
| 1186 | + -3, -40, -62, -5, -102, 9, -32, -27, | |
| 1187 | + -54, 21, 15, -5, 37, -43, -11, 37, | |
| 1188 | + -19, 47, -64, -128, -27, -114, 21, -66, | |
| 1189 | + 59, 46, -3, -12, -87, -9, 4, 19, | |
| 1190 | + -113, -36, 78, 57, -26, -38, -77, -10, | |
| 1191 | + 6, 6, -75, 25, -97, -11, 33, -46, | |
| 1192 | + 1, 13, -21, -33, -20, 16, -6, -3, | |
| 1193 | + -11, -4, -27, 38, 8, -41, -2, -33, | |
| 1194 | + 18, 19, -26, 1, -29, -22, -4, -14, | |
| 1195 | + -55, -11, -80, -3, 11, 34, 90, 51, | |
| 1196 | + 11, 17, 43, 36, 127, -32, 29, 103, | |
| 1197 | + 9, 27, 13, 64, 56, 70, -14, 3, | |
| 1198 | + -12, 10, 37, 3, 12, -22, -10, 46, | |
| 1199 | + 28, 10, 20, 26, -24, 18, 9, 7, | |
| 1200 | + 14, 34, -5, -7, 31, -14, -56, 11, | |
| 1201 | + -18, -8, -17, -7, -10, -40, 10, -33, | |
| 1202 | + -32, -43, 5, 9, 11, -4, 10, 50, | |
| 1203 | + -12, -5, 46, 9, 7, 1, 11, 15, | |
| 1204 | + 91, -17, 7, -50, 23, 6, -30, -99, | |
| 1205 | + 0, -17, 14, 8, -10, -25, -30, -69, | |
| 1206 | + -62, 31, 127, 114, -23, 101, -5, -54, | |
| 1207 | + -6, -22, 7, -56, 39, 18, -29, 0, | |
| 1208 | + 46, 8, -79, 4, -21, 18, -32, 62, | |
| 1209 | + -12, -8, -12, -58, 31, -32, 17, 6, | |
| 1210 | + -24, 25, 24, 9, -4, -19, 45, 6, | |
| 1211 | + 17, -14, 5, -27, 16, -4, -41, 25, | |
| 1212 | + -36, 5, 15, 12, 50, 27, 25, 23, | |
| 1213 | + -44, -69, -9, -19, -48, -8, 4, 12, | |
| 1214 | + -6, 13, -19, -30, -36, 26, 37, -1, | |
| 1215 | + -3, -30, -42, -14, -10, -20, 26, -54, | |
| 1216 | + -27, -44, 4, 73, -26, 90, 32, -69, | |
| 1217 | + -29, -16, 3, 103, 15, -17, 37, 24, | |
| 1218 | + -23, -31, 33, -37, -64, 25, 13, -81, | |
| 1219 | + -28, -32, 27, 5, -35, -23, 15, -22, | |
| 1220 | + 19, -7, 9, 30, 19, -23, 27, -13, | |
| 1221 | + 43, 29, -29, -6, 9, -40, -33, -33, | |
| 1222 | + -32, 9, 11, -48, -8, -23, -52, 46, | |
| 1223 | + 17, -22, -42, 35, -15, -41, 16, 34, | |
| 1224 | + 31, -42, -19, -11, 55, 7, -39, 89, | |
| 1225 | + -11, -33, 20, -14, 22, 32, 3, -17, | |
| 1226 | + -6, 14, 34, 1, 55, -21, -90, -8, | |
| 1227 | + 18, 27, 13, -29, 21, 15, -33, -51, | |
| 1228 | + -9, -11, 4, -16, -18, 23, -4, -4, | |
| 1229 | + 48, 1, 7, 29, -14, -12, -16, 17, | |
| 1230 | + 35, 8, 0, -7, -2, 9, 8, 17, | |
| 1231 | + -6, 53, -32, -21, -50, 5, 99, -60, | |
| 1232 | + -5, -53, 10, -31, 12, -5, 7, 80, | |
| 1233 | + 36, 18, -31, 9, 98, 36, -63, -35, | |
| 1234 | + 4, -13, -28, -24, 28, -13, 18, 16, | |
| 1235 | + -1, -18, -34, 10, 20, 7, 4, 29, | |
| 1236 | + 11, 25, -7, 36, 14, 45, 24, 1, | |
| 1237 | + -16, 30, 6, 35, -6, -11, -24, 13, | |
| 1238 | + -1, 27, 39, 20, 48, -11, -4, -13, | |
| 1239 | + 28, 11, -31, -18, 31, -29, 22, -2, | |
| 1240 | + -20, -16, 5, 30, -12, -28, -3, 93, | |
| 1241 | + -16, 23, 18, -29, 6, -54, -37, 28, | |
| 1242 | + -3, -3, -47, -3, -36, -55, -3, 41, | |
| 1243 | + -10, 47, -2, 23, 42, -7, -71, -27, | |
| 1244 | + 83, -64, 7, -24, 8, 26, -17, 15, | |
| 1245 | + 12, 31, -30, -38, -13, -33, -56, 4, | |
| 1246 | + -17, 20, 18, 1, -30, -5, -6, -31, | |
| 1247 | + -14, -37, 0, 22, 10, -30, 37, -17, | |
| 1248 | + 18, 6, 5, 23, -36, -32, 14, 18, | |
| 1249 | + -13, -61, -52, -69, 44, -30, 16, 18, | |
| 1250 | + -4, -25, 14, 81, 26, -8, -23, -59, | |
| 1251 | + 52, -104, 17, 119, -32, 26, 17, 1, | |
| 1252 | + 23, 45, 29, -64, -57, -14, 73, 21, | |
| 1253 | + -13, -13, 9, -68, -7, -52, 3, 24, | |
| 1254 | + -39, 44, -15, 27, 14, 19, -9, -28, | |
| 1255 | + -11, 5, 3, -34, -2, 2, 22, -6, | |
| 1256 | + -23, 4, 3, 13, -22, -13, -10, -18, | |
| 1257 | + 29, 6, 44, -13, -24, -8, 2, 30, | |
| 1258 | + 14, 43, 6, 17, -73, -6, -7, 20, | |
| 1259 | + -80, -7, -7, -28, 15, -69, -38, -5, | |
| 1260 | + -100, -35, 15, -79, 23, 29, -18, -27, | |
| 1261 | + 21, -66, -37, 8, -22, -39, 48, 4, | |
| 1262 | + -13, 1, -9, 11, -29, 22, 6, -49, | |
| 1263 | + 32, -14, 47, -18, -4, 44, -52, -74, | |
| 1264 | + 43, 30, 23, -14, 5, 0, -27, 4, | |
| 1265 | + -7, 10, -4, 10, 1, -16, 11, -18, | |
| 1266 | + -2, -5, 2, -11, 0, -20, -4, 38, | |
| 1267 | + 74, 59, 39, 64, -10, 26, -3, -40, | |
| 1268 | + -68, 3, -30, -51, 8, -19, -27, -46, | |
| 1269 | + 51, 52, 54, 36, 90, 92, 14, 13, | |
| 1270 | + -5, 0, 16, -62, 16, 11, -47, -37, | |
| 1271 | + -6, -5, 21, 54, -57, 32, 42, -6, | |
| 1272 | + 62, -9, 16, 21, 24, 9, -10, -4, | |
| 1273 | + 33, 50, 13, -15, 1, -35, -48, 18, | |
| 1274 | + -11, -17, -67, -13, 21, 38, -44, 36, | |
| 1275 | + -16, 29, 17, 5, -10, 18, 17, -32, | |
| 1276 | + 2, 8, 22, -56, -15, -32, 40, 43, | |
| 1277 | + 19, 46, -7, -100, -96, 19, 53, 24, | |
| 1278 | + 21, -26, -48, -101, -82, 61, 38, -85, | |
| 1279 | + -28, -34, -1, 63, -5, -5, 39, 39, | |
| 1280 | + -38, 32, -12, -28, 20, 40, -8, 2, | |
| 1281 | + 31, 12, -35, -13, 20, -25, 30, 8, | |
| 1282 | + 3, -13, -9, -20, 2, -13, 24, 37, | |
| 1283 | + -10, 33, 6, 20, -16, -24, -6, -6, | |
| 1284 | + -19, -5, 22, 21, 10, 11, -4, -39, | |
| 1285 | + -1, 6, 49, 41, -15, -57, 21, -62, | |
| 1286 | + 77, -69, -13, 0, -74, 1, -7, -38, | |
| 1287 | + -8, 6, 63, 28, 4, 26, -52, 82, | |
| 1288 | + 63, 13, 45, -33, 44, -52, -65, -21, | |
| 1289 | + -46, -49, 64, -17, 32, 24, 68, -39, | |
| 1290 | + -16, -5, -26, 28, 5, -61, -28, 2, | |
| 1291 | + 24, 11, -12, -33, 9, -37, -3, -28, | |
| 1292 | + 22, -37, -12, 19, 0, -18, -2, 14, | |
| 1293 | + 1, 4, 8, -9, -2, 43, -17, -2, | |
| 1294 | + -66, -31, 56, -40, -87, -36, -2, -4, | |
| 1295 | + -42, -45, -1, 31, -43, -15, 27, 63, | |
| 1296 | + -11, 32, -10, -33, 27, -19, 4, 15, | |
| 1297 | + -26, -34, 29, -4, -39, -65, 14, -20, | |
| 1298 | + -21, -17, -36, 13, 59, 47, -38, -33, | |
| 1299 | + 13, -37, -8, -37, -7, -6, -76, -31, | |
| 1300 | + -12, -46, 7, 24, -21, -30, -14, 9, | |
| 1301 | + 15, -12, -13, 47, -27, -25, -1, -39, | |
| 1302 | + 0, 20, -9, 6, 7, 4, 3, 7, | |
| 1303 | + 39, 50, 22, -7, 14, -20, 1, 70, | |
| 1304 | + -28, 29, -41, 10, -16, -5, -28, -2, | |
| 1305 | + -37, 32, -18, 17, 62, -11, -20, -50, | |
| 1306 | + 36, 21, -62, -12, -56, 52, 50, 17, | |
| 1307 | + 3, 48, 44, -41, -25, 3, 16, -3, | |
| 1308 | + 0, 33, -6, 15, 27, 34, -25, 22, | |
| 1309 | + 9, 17, -11, 36, 16, -2, 12, 21, | |
| 1310 | + -52, 45, -2, -10, 46, 21, -18, 67, | |
| 1311 | + -28, -13, 30, 37, 42, 16, -9, 11, | |
| 1312 | + 75, 7, -64, -40, -10, 29, 57, -23, | |
| 1313 | + 5, 53, -77, 3, -17, -5, 47, -55, | |
| 1314 | + -35, -36, -13, 52, -53, -71, 52, -111, | |
| 1315 | + -23, -26, -28, 29, -43, 55, -19, 43, | |
| 1316 | + -19, 54, -12, -33, -44, -39, -19, -10, | |
| 1317 | + -31, -10, 21, 38, -57, -20, 2, -25, | |
| 1318 | + 8, -6, 50, 12, 15, 25, -25, 15, | |
| 1319 | + -30, -6, 9, 25, 37, 19, -4, 31, | |
| 1320 | + -22, 2, 4, 2, 36, 7, 3, -34, | |
| 1321 | + -80, 36, -10, -2, -5, 31, -36, 49, | |
| 1322 | + -70, 20, -36, 21, 24, 25, -46, -51, | |
| 1323 | + 36, -58, -48, -40, -10, 55, 71, 47, | |
| 1324 | + 10, -1, 1, 2, -46, -68, 16, 13, | |
| 1325 | + 0, -74, -29, 73, -52, -18, -11, 7, | |
| 1326 | + -44, -82, -32, -70, -28, -1, -39, -68, | |
| 1327 | + -6, -41, 12, -22, -16, 40, -11, -25, | |
| 1328 | + 51, -9, 21, 4, 4, -34, 7, -78, | |
| 1329 | + 16, 6, -38, -30, -2, -44, 32, 0, | |
| 1330 | + 22, 64, 5, -72, -2, -14, -10, -16, | |
| 1331 | + -8, -25, 12, 102, -58, 37, -10, -23, | |
| 1332 | + 15, 49, 7, -7, 2, -20, -32, 45, | |
| 1333 | + -6, 48, 28, 30, 33, -1, 22, -6, | |
| 1334 | + 30, 65, -17, 29, 74, 37, -26, -10, | |
| 1335 | + 15, -24, 19, -66, 22, -10, -31, -1, | |
| 1336 | + -18, -9, 11, 37, -4, 45, 5, 41, | |
| 1337 | + 17, 1, 1, 24, -58, 41, 5, -51, | |
| 1338 | + 14, 8, 43, 16, -10, -1, 45, 32, | |
| 1339 | + -64, 3, -33, -25, -3, -27, -68, 12, | |
| 1340 | + 23, -11, -13, -37, -40, 4, -21, -12, | |
| 1341 | + 32, -23, -19, 76, 41, -23, -24, -44, | |
| 1342 | + -65, -1, -15, 1, 71, 63, 5, 20, | |
| 1343 | + -3, 21, -23, 31, -32, 18, -2, 27, | |
| 1344 | + 31, 46, -5, -39, -5, -35, 18, -18, | |
| 1345 | + -40, -10, 3, 12, 2, -2, -22, 40, | |
| 1346 | + 5, -6, 60, 36, 3, 29, -27, 10, | |
| 1347 | + 25, -54, 5, 26, 39, 35, -24, -37, | |
| 1348 | + 30, -91, 28, -4, -21, -27, -39, -6, | |
| 1349 | + 5, 12, -128, 38, -16, 29, -95, -29, | |
| 1350 | + 82, -2, 35, 2, 12, 8, -22, 10, | |
| 1351 | + 80, -47, 2, -25, -73, -79, 16, -30, | |
| 1352 | + -32, -66, 48, 21, -45, -11, -47, 14, | |
| 1353 | + -27, -17, -7, 15, -44, -14, -44, -26, | |
| 1354 | + -32, 26, -23, 17, -7, -28, 26, -6, | |
| 1355 | + 28, 6, -26, 2, 13, -14, -23, -14, | |
| 1356 | + 19, 46, 16, 2, -33, -21, 28, -17, | |
| 1357 | + -42, 44, -37, 1, -39, 28, 84, -46, | |
| 1358 | + 15, 10, 13, -44, 72, -26, 26, 32, | |
| 1359 | + -28, -12, -83, 2, 10, -30, -44, -10, | |
| 1360 | + -28, 53, 45, 65, 0, -25, 57, 36, | |
| 1361 | + -33, 6, 29, 44, -53, 11, 19, -2, | |
| 1362 | + -27, 35, 32, 49, 4, 23, 38, 36, | |
| 1363 | + 24, 10, 51, -39, 4, -7, 26, 37, | |
| 1364 | + -35, 11, -47, -18, 28, 16, -35, 42, | |
| 1365 | + 17, -21, -41, 28, 14, -12, 11, -45, | |
| 1366 | + 7, -43, -15, 18, -5, 38, -40, -50, | |
| 1367 | + -30, -21, 9, -98, 13, 12, 23, 75, | |
| 1368 | + -56, -7, -3, -4, -1, -34, 12, -49, | |
| 1369 | + 11, 26, -18, -28, -17, 33, 13, -14, | |
| 1370 | + 40, 24, -72, -37, 10, 17, -6, 22, | |
| 1371 | + 16, 16, -6, -12, -30, -14, 10, 40, | |
| 1372 | + -23, 12, 15, -3, -15, 13, -56, -4, | |
| 1373 | + -30, 1, -3, -17, 27, 50, -5, 64, | |
| 1374 | + -36, -19, 7, 29, 22, 25, 9, -16, | |
| 1375 | + -58, -69, -40, -61, -71, -14, 42, 93, | |
| 1376 | + 26, 11, -6, -58, -11, 70, -52, 19, | |
| 1377 | + 9, -30, -33, 11, -37, -47, -21, -22, | |
| 1378 | + -40, 10, 47, 4, -23, 17, 48, 41, | |
| 1379 | + -48, 14, 10, 15, 34, -23, -2, -47, | |
| 1380 | + 23, -32, -13, -10, -26, -26, -4, 16, | |
| 1381 | + 38, -14, 0, -12, -7, -7, 20, 44, | |
| 1382 | + -1, -32, -27, -16, 4, -6, -18, 14, | |
| 1383 | + 5, 4, -29, 28, 7, -7, 15, -11, | |
| 1384 | + -20, -45, -36, 16, 84, 34, -59, -30, | |
| 1385 | + 22, 126, 8, 68, 79, -17, 21, -68, | |
| 1386 | + 37, 5, 15, 63, 49, 127, -90, 85, | |
| 1387 | + 43, 7, 16, 9, 6, -45, -57, -43, | |
| 1388 | + 57, 11, -23, -11, -29, 60, -26, 0, | |
| 1389 | + 7, 42, -24, 10, 23, -25, 8, -7, | |
| 1390 | + -40, 19, -17, 35, 4, 27, -39, -91, | |
| 1391 | + 27, -36, 34, 2, 16, -24, 25, 7, | |
| 1392 | + -21, 5, 17, 10, -22, -30, 9, -17, | |
| 1393 | + -61, -26, 33, 21, 58, -51, -14, 69, | |
| 1394 | + -38, 20, 7, 80, -4, -65, -6, -27, | |
| 1395 | + 53, -12, 47, -1, -15, 1, 60, 102, | |
| 1396 | + -79, -4, 12, 9, 22, 37, -8, -4, | |
| 1397 | + 37, 2, -3, -15, -16, -11, -5, 19, | |
| 1398 | + -6, -43, 20, -25, -18, 10, -27, 0, | |
| 1399 | + -28, -27, -11, 10, -18, -2, -4, -16, | |
| 1400 | + 26, 14, -6, 7, -6, 1, 53, -2, | |
| 1401 | + -29, 23, 9, -30, -6, -4, -6, 56, | |
| 1402 | + 70, 0, -33, -20, -17, -9, -24, 46, | |
| 1403 | + -5, -105, 47, -46, -51, 20, 20, -53, | |
| 1404 | + -81, -1, -7, 75, -5, -21, -65, 12, | |
| 1405 | + -52, 22, -50, -12, 49, 54, 76, -81, | |
| 1406 | + 10, 45, -41, -59, 18, -19, 25, 14, | |
| 1407 | + -31, -53, -5, 12, 31, 84, -23, 2, | |
| 1408 | + 7, 2, 10, -32, 39, -2, -12, 1, | |
| 1409 | + -9, 0, -10, -11, 9, 15, -8, -2, | |
| 1410 | + 2, -1, 10, 14, -5, -40, 19, -7, | |
| 1411 | + -7, 26, -4, 2, 1, -27, 35, 32, | |
| 1412 | + 21, -31, 26, 43, -9, 4, -32, 40, | |
| 1413 | + -62, -52, 36, 22, 38, 22, 36, -96, | |
| 1414 | + 6, -10, -23, -49, 15, -33, -18, -3, | |
| 1415 | + 0, 41, 21, -19, 21, 23, -39, -23, | |
| 1416 | + -6, 6, 47, 56, 4, 74, 0, -98, | |
| 1417 | + 29, -47, -14, -36, 21, -22, 22, 16, | |
| 1418 | + 13, 12, 16, -5, 13, 17, -13, -15, | |
| 1419 | + 1, -34, -26, 26, 12, 32, 27, 13, | |
| 1420 | + -67, 27, 2, 8, 10, 18, 16, 20, | |
| 1421 | + -17, -17, 57, -64, 5, 14, 19, 31, | |
| 1422 | + -18, -44, -46, -16, 4, -25, 17, -126, | |
| 1423 | + -24, 39, 4, 8, 55, -25, -34, 39, | |
| 1424 | + -16, 3, 9, 71, 72, -31, -55, 6, | |
| 1425 | + 10, -25, 32, -85, -21, 18, -8, 15, | |
| 1426 | + 12, -27, -7, 1, -21, -2, -5, 48, | |
| 1427 | + -16, 18, 1, -22, -26, 16, 14, -31, | |
| 1428 | + 27, -6, -15, -21, 4, -14, 18, -36, | |
| 1429 | +]; | |
| 1430 | + | |
| 1431 | +static LAYER1_RECUR_WEIGHTS: [i8; 1728] = [ | |
| 1432 | + 20, 67, -99, 12, 41, -25, 49, -44, | |
| 1433 | + 35, 81, 110, 47, 34, -66, -14, 14, | |
| 1434 | + -60, 34, 29, -73, 10, 41, 35, 89, | |
| 1435 | + 7, -35, 22, 7, 27, -20, -6, 56, | |
| 1436 | + 26, 66, 6, 33, -55, 53, 1, -21, | |
| 1437 | + 14, 17, 68, 55, 59, 0, 18, -9, | |
| 1438 | + 5, -41, 6, -5, -114, -12, 29, 42, | |
| 1439 | + -23, 10, 81, -27, 20, -53, -30, -62, | |
| 1440 | + 40, 95, 25, -4, 3, 18, -8, -15, | |
| 1441 | + -29, -82, 2, -57, -3, -61, -29, -29, | |
| 1442 | + 49, 2, -55, 5, -69, -99, -49, -51, | |
| 1443 | + 6, -25, 12, 89, 44, -33, 5, 41, | |
| 1444 | + 1, 23, -37, -37, -28, -48, 3, 4, | |
| 1445 | + -41, -30, -57, -35, -39, -1, -13, -56, | |
| 1446 | + -5, 50, 49, 41, -4, -4, 33, -22, | |
| 1447 | + -1, 33, 34, 18, 40, -42, 12, 1, | |
| 1448 | + -6, -2, 18, 17, 39, 44, 11, 65, | |
| 1449 | + -60, -45, 10, 91, 21, 9, -62, -11, | |
| 1450 | + 8, 69, 37, 24, -30, 21, 26, -27, | |
| 1451 | + 1, -28, 24, 66, -8, 6, -71, 34, | |
| 1452 | + 24, 44, 58, -78, -19, 57, 17, -60, | |
| 1453 | + 1, 12, -3, -1, -40, 22, 11, -5, | |
| 1454 | + 25, 12, 1, 72, 79, 7, -50, 23, | |
| 1455 | + 18, 13, 21, -11, -20, 5, 77, -94, | |
| 1456 | + 24, 15, 57, -51, 3, 36, 53, -1, | |
| 1457 | + 4, 14, 30, -31, 22, 40, 32, -11, | |
| 1458 | + -34, -36, -59, 58, 25, 21, -54, -23, | |
| 1459 | + 40, 46, 18, 0, 12, 54, -96, -99, | |
| 1460 | + -59, 5, 119, -38, 50, 55, 12, -16, | |
| 1461 | + 67, 0, 34, 35, 39, 35, -1, 69, | |
| 1462 | + 24, 27, -30, -35, -4, -70, 2, -44, | |
| 1463 | + -7, -6, 19, -9, 60, 44, -21, -10, | |
| 1464 | + 37, 43, -16, -3, 30, -15, -65, 31, | |
| 1465 | + -55, 18, -98, 76, 64, 25, 24, -18, | |
| 1466 | + -7, -68, -10, 38, 27, -60, 36, 33, | |
| 1467 | + 16, 30, 34, -39, -37, 31, 12, 53, | |
| 1468 | + -54, 14, -26, -49, -128, -13, -5, -22, | |
| 1469 | + -11, -85, 55, -8, -51, -11, -33, -10, | |
| 1470 | + -31, -76, -41, 23, 44, -40, -54, -127, | |
| 1471 | + -101, 19, -23, -15, 15, 27, 58, -60, | |
| 1472 | + 8, 14, -33, 1, 48, -9, -11, -123, | |
| 1473 | + 3, 53, 23, 4, -28, 22, 2, -29, | |
| 1474 | + -67, 36, 12, 7, 55, -21, 88, 20, | |
| 1475 | + -1, -21, -17, 3, 41, 32, -10, -14, | |
| 1476 | + -5, -57, 67, 57, 21, 23, -2, -27, | |
| 1477 | + -73, -24, 120, 21, 18, -35, 42, -7, | |
| 1478 | + 3, -45, -25, 76, -34, 50, 11, -54, | |
| 1479 | + -91, 3, -113, -20, -5, 47, 15, -47, | |
| 1480 | + 17, 27, -3, -26, -7, 10, 7, 74, | |
| 1481 | + -40, 64, -7, -5, -24, -49, -24, -3, | |
| 1482 | + -10, 27, -17, -8, -3, 14, -27, 33, | |
| 1483 | + 13, 39, 28, -7, -38, 29, 16, 44, | |
| 1484 | + 19, 55, -3, 9, -13, -57, 43, 43, | |
| 1485 | + 31, 0, -93, -17, 19, -56, 4, -12, | |
| 1486 | + -25, 37, -85, -13, -118, 33, -17, 56, | |
| 1487 | + 71, -80, -4, 6, -11, -18, 47, -52, | |
| 1488 | + 25, 9, 48, -107, 1, 21, 20, -3, | |
| 1489 | + 10, -16, -4, 24, 17, 31, -61, -18, | |
| 1490 | + -50, 24, -10, 12, 71, 26, 11, -3, | |
| 1491 | + 4, 1, 0, -7, -40, 18, 38, -34, | |
| 1492 | + 38, 17, 8, -34, 2, 21, 123, -32, | |
| 1493 | + -26, 43, 14, -34, -1, -9, 37, -16, | |
| 1494 | + 6, -17, -62, 68, 22, 17, 11, -75, | |
| 1495 | + 33, -80, 62, -9, -75, 76, 36, -41, | |
| 1496 | + -8, -40, -11, -71, 40, -39, 62, -49, | |
| 1497 | + -81, 16, -9, -52, 52, 61, 17, -103, | |
| 1498 | + -27, -10, -8, -54, -57, 21, 23, -16, | |
| 1499 | + -52, 36, 18, 10, -5, 8, 15, -29, | |
| 1500 | + 5, -19, -37, 8, -53, 6, 19, -37, | |
| 1501 | + 38, -17, 48, 10, 0, 81, 46, 70, | |
| 1502 | + -29, 101, 11, 44, -44, -3, 24, 11, | |
| 1503 | + 3, 14, -9, 11, 14, -45, 13, 46, | |
| 1504 | + -3, -57, 68, 44, 63, 98, 25, -28, | |
| 1505 | + -23, 15, 32, -10, 53, -6, -2, -9, | |
| 1506 | + -6, 16, -107, -11, -11, -28, 59, 57, | |
| 1507 | + -22, 38, 42, 83, 27, 5, 29, -30, | |
| 1508 | + 12, -21, -13, 31, 38, -21, 58, -10, | |
| 1509 | + -10, -15, -2, -5, 11, 12, -73, -28, | |
| 1510 | + -38, 22, 2, -25, 73, -52, -12, -55, | |
| 1511 | + 32, -63, 21, 51, 33, 52, -26, 55, | |
| 1512 | + -26, -26, 57, -32, -4, -52, -61, 21, | |
| 1513 | + -33, -91, -51, 69, -90, -53, -38, -44, | |
| 1514 | + 12, -76, -20, 77, -45, -7, 86, 43, | |
| 1515 | + -109, -33, -105, -40, -121, -10, 0, -72, | |
| 1516 | + 45, -51, -75, -49, -38, -1, -62, 18, | |
| 1517 | + -1, 30, -44, -14, -10, -67, 40, -10, | |
| 1518 | + -34, 46, -64, -32, 29, -13, 33, 3, | |
| 1519 | + -32, -5, 28, -27, -25, 93, 24, 68, | |
| 1520 | + -40, 57, 23, -3, -21, -58, 17, -39, | |
| 1521 | + -17, -22, -89, 11, 18, -46, 27, 24, | |
| 1522 | + 46, 127, 61, 87, 31, 127, -36, 47, | |
| 1523 | + -23, 47, 127, -24, 110, 122, 30, 100, | |
| 1524 | + 0, 96, -12, 6, 50, 44, -13, 73, | |
| 1525 | + 4, 55, -11, -15, 49, 42, -6, 20, | |
| 1526 | + -35, 58, 18, 38, 42, 72, 19, -21, | |
| 1527 | + 11, 9, -37, 7, 29, 31, 16, -17, | |
| 1528 | + 13, -50, 19, 5, -23, 51, -16, -5, | |
| 1529 | + 4, -24, 76, 10, -53, -28, -7, -65, | |
| 1530 | + 74, 40, -16, -29, 32, -16, -49, -35, | |
| 1531 | + -3, 59, -96, -50, -43, -43, -61, -15, | |
| 1532 | + -8, -36, -34, -33, -14, 11, -3, -39, | |
| 1533 | + 4, -114, -123, -11, -49, -21, 14, -56, | |
| 1534 | + 1, 43, -63, 26, 40, 18, -10, -26, | |
| 1535 | + -14, -15, -35, -35, -11, 32, -44, -67, | |
| 1536 | + 2, 22, 7, 3, -9, -30, -51, -28, | |
| 1537 | + 28, 6, -22, 16, 34, -25, -52, -54, | |
| 1538 | + -8, -6, 5, 8, 20, -16, -17, -44, | |
| 1539 | + 27, 3, 31, -5, -48, -1, -3, 116, | |
| 1540 | + 11, 71, -31, -47, 109, 50, -22, -12, | |
| 1541 | + -57, 32, 66, 8, -25, -93, -54, -10, | |
| 1542 | + 19, -76, -34, 97, 48, -36, -18, -30, | |
| 1543 | + -39, -26, -12, 28, 14, 12, -12, -31, | |
| 1544 | + 38, 2, 10, 4, -40, 20, 16, -61, | |
| 1545 | + 2, 64, 39, 5, 15, 33, 40, -61, | |
| 1546 | + -49, 93, -10, 33, 28, -11, -27, -18, | |
| 1547 | + 39, -62, -6, -6, 62, 11, -8, 38, | |
| 1548 | + -67, 12, 27, 39, -27, 123, -18, -6, | |
| 1549 | + -65, 83, -64, 20, 19, -11, 33, 24, | |
| 1550 | + 17, 56, 78, 7, -15, 54, -101, -9, | |
| 1551 | + 115, -96, 50, 51, 35, 34, 27, 37, | |
| 1552 | + -40, -11, 8, -36, 42, -45, 2, -23, | |
| 1553 | + 0, 67, -8, -9, -13, 50, -14, -27, | |
| 1554 | + 4, 0, -8, -14, 30, -9, 29, 15, | |
| 1555 | + 9, -38, 37, -8, 50, -46, 54, 41, | |
| 1556 | + -11, -8, -11, -26, 39, 45, 14, -26, | |
| 1557 | + -17, -27, 69, 38, 39, 98, 66, 0, | |
| 1558 | + 42, 123, -101, -19, -83, 117, -32, 56, | |
| 1559 | + 10, 12, -88, 79, -53, 56, 63, 95, | |
| 1560 | + -62, 9, 36, -13, -79, -16, 37, -46, | |
| 1561 | + 35, -34, 14, 17, -54, 5, 21, -7, | |
| 1562 | + 7, 63, 56, 15, 27, -76, -25, 4, | |
| 1563 | + -26, -63, 28, -67, -52, 43, -47, -70, | |
| 1564 | + 40, -12, 40, -66, -37, 0, 35, 37, | |
| 1565 | + -53, 4, -17, -51, 11, 21, 14, -34, | |
| 1566 | + -4, 24, -42, 29, 22, 7, 28, 12, | |
| 1567 | + 37, 39, -39, -19, 65, -60, -50, -2, | |
| 1568 | + 1, 82, 39, 19, -23, -43, -22, -67, | |
| 1569 | + -35, -34, 32, 102, 81, 127, 36, 67, | |
| 1570 | + -45, 1, -67, -52, -4, 35, 20, 28, | |
| 1571 | + 71, 86, -35, -9, -83, -34, 12, 9, | |
| 1572 | + -23, 2, 14, 28, -23, 7, -25, 45, | |
| 1573 | + 7, 17, -37, 0, -19, 31, 26, 40, | |
| 1574 | + -27, -16, 17, 5, -21, 23, 24, 96, | |
| 1575 | + -55, 52, -19, -14, -6, 1, 50, -34, | |
| 1576 | + 86, -53, 38, 2, -52, -36, -13, 60, | |
| 1577 | + -85, -120, 32, 7, -12, 22, 70, -7, | |
| 1578 | + -94, 38, -76, -31, -20, 15, -28, 7, | |
| 1579 | + 6, 40, 53, 88, 3, 38, 18, -8, | |
| 1580 | + -22, -23, 51, 37, -9, 13, -32, 25, | |
| 1581 | + -21, 27, 31, 20, 18, -9, -13, 1, | |
| 1582 | + 21, -24, -13, 39, 15, -11, -29, -36, | |
| 1583 | + 18, 15, 8, 27, 21, -94, -1, -22, | |
| 1584 | + 49, 66, -1, 6, -3, -40, -18, 6, | |
| 1585 | + 28, 12, 33, -59, 62, 60, -48, 90, | |
| 1586 | + -1, 108, 9, 18, -2, 27, 77, -65, | |
| 1587 | + 82, -48, -38, -19, -11, 127, 50, 66, | |
| 1588 | + 18, -13, -22, 60, -38, 40, -14, -26, | |
| 1589 | + -13, 38, 67, 57, 30, 33, 26, 36, | |
| 1590 | + 38, -17, 27, -28, 20, 12, -64, 18, | |
| 1591 | + 5, -33, -27, 13, -26, 32, 35, -5, | |
| 1592 | + -48, -14, 92, 43, -47, -14, 40, 11, | |
| 1593 | + 51, 66, 22, -63, -16, -61, 4, -28, | |
| 1594 | + 27, 20, -33, -30, -21, -29, -53, 31, | |
| 1595 | + -40, 24, 43, -4, -19, 21, 67, 20, | |
| 1596 | + 100, -16, -93, 78, -6, -18, -52, -37, | |
| 1597 | + -9, 66, -31, -8, 26, 18, 4, 24, | |
| 1598 | + -22, 17, -2, -13, 27, 0, 8, -18, | |
| 1599 | + -25, 5, -21, -24, -7, 18, -93, 21, | |
| 1600 | + 7, 2, -75, 69, 50, -5, -15, -17, | |
| 1601 | + 60, -42, 55, 1, -4, 3, 10, 46, | |
| 1602 | + 16, -13, 45, -7, -10, -44, -108, 49, | |
| 1603 | + 2, -15, -64, -12, -72, 32, -38, -45, | |
| 1604 | + 10, -54, 13, -13, -27, -36, -64, 58, | |
| 1605 | + -62, -101, 88, -86, -71, -39, -9, -128, | |
| 1606 | + 32, 15, -4, 54, -16, -39, -26, -36, | |
| 1607 | + 46, 48, -64, -10, 19, 30, -13, 34, | |
| 1608 | + -8, 50, 60, -22, -6, -11, -30, 5, | |
| 1609 | + 50, 32, 56, 0, 25, 6, 68, 11, | |
| 1610 | + -29, 45, -9, -12, 4, 1, 18, -49, | |
| 1611 | + 0, -38, -19, 90, 29, 35, 51, 8, | |
| 1612 | + -48, 96, -1, -12, -9, -32, -63, -65, | |
| 1613 | + -7, 38, 89, 28, -85, -28, -23, -25, | |
| 1614 | + -128, 56, 79, -36, 99, -6, -37, 7, | |
| 1615 | + -13, -69, -46, -29, 25, 64, -21, 17, | |
| 1616 | + 1, 42, -66, 1, 80, 26, -32, 21, | |
| 1617 | + 15, 15, 6, 6, -10, 15, 127, 5, | |
| 1618 | + 38, 27, 87, -57, -25, 11, 72, -21, | |
| 1619 | + -5, 11, -13, -66, 78, 36, -3, 41, | |
| 1620 | + -21, 8, -33, 23, 73, 28, 57, -25, | |
| 1621 | + -5, 4, -22, -47, 15, 4, -57, -72, | |
| 1622 | + 33, 1, 18, 2, 53, -71, -99, -21, | |
| 1623 | + -3, -111, 108, 71, -14, 82, 25, 61, | |
| 1624 | + -48, 5, 9, -51, -20, -25, -3, 14, | |
| 1625 | + -33, 14, -3, -34, 22, 12, -19, -38, | |
| 1626 | + -16, 2, 21, 16, 26, -31, 75, 44, | |
| 1627 | + -31, 16, 26, 66, 17, -9, -22, -22, | |
| 1628 | + 22, -44, 22, 27, 2, 58, -14, 10, | |
| 1629 | + -73, -42, 55, -25, -61, 72, -1, 30, | |
| 1630 | + -58, -25, 63, 26, -48, -40, 26, -30, | |
| 1631 | + 60, 8, -17, -1, -18, -20, 43, -20, | |
| 1632 | + -4, -28, 127, -106, 29, 70, 64, -27, | |
| 1633 | + 39, -33, -5, -88, -40, -52, 26, 44, | |
| 1634 | + -17, 23, 2, -49, 22, -9, -8, 86, | |
| 1635 | + 49, -43, -60, 1, 10, 45, 36, -53, | |
| 1636 | + -4, 33, 38, 48, -72, 1, 19, 21, | |
| 1637 | + -65, 4, -5, -62, 27, -25, 17, -6, | |
| 1638 | + 6, -45, -39, -46, 4, 26, 127, -9, | |
| 1639 | + 18, -33, -18, -3, 33, 2, -5, 15, | |
| 1640 | + -26, -22, -117, -63, -17, -59, 61, -74, | |
| 1641 | + 7, -47, -58, -128, -67, 15, -16, -128, | |
| 1642 | + 12, 2, 20, 9, -48, -40, 43, 3, | |
| 1643 | + -40, -16, -38, -6, -22, -28, -16, -59, | |
| 1644 | + -22, 6, -5, 11, -12, -66, -40, 27, | |
| 1645 | + -62, -44, -19, 38, -3, 39, -8, 40, | |
| 1646 | + -24, 13, 21, 50, -60, -22, 53, -29, | |
| 1647 | + -6, 1, 22, -59, 0, 17, -39, 115, | |
| 1648 | +]; | |
| 1649 | + | |
| 1650 | +static LAYER1_BIAS: [i8; 72] = [ | |
| 1651 | + -42, 20, 16, 0, 105, 60, 1, -97, | |
| 1652 | + 24, 60, 18, 13, 62, 25, 127, 34, | |
| 1653 | + 79, 55, 118, 127, 95, 31, -4, 87, | |
| 1654 | + 21, 12, 2, -14, 18, 23, 8, 17, | |
| 1655 | + -1, -8, 5, 4, 24, 37, 21, 13, | |
| 1656 | + 36, 13, 17, 18, 37, 30, 33, 1, | |
| 1657 | + 8, -16, -11, -5, -31, -3, -5, 0, | |
| 1658 | + 6, 3, 58, -7, -1, -16, 5, -13, | |
| 1659 | + 16, 10, -2, -14, 11, -4, 3, -11, | |
| 1660 | +]; | |
| 1661 | + | |
| 1662 | +static LAYER2_WEIGHTS: [i8; 48] = [ | |
| 1663 | + -113, -88, 31, -128, -126, -61, 85, -35, | |
| 1664 | + 118, -128, -61, 127, -128, -17, -128, 127, | |
| 1665 | + 104, -9, -128, 33, 45, 127, 5, 83, | |
| 1666 | + 84, -128, -85, -128, -45, 48, -53, -128, | |
| 1667 | + 46, 127, -17, 125, 117, -41, -117, -91, | |
| 1668 | + -127, -68, -1, -89, -80, 32, 106, 7, | |
| 1669 | +]; | |
| 1670 | + | |
| 1671 | +static LAYER2_BIAS: [i8; 2] = [ | |
| 1672 | + 14, 117, | |
| 1673 | +]; |
crates/ec-opus/src/celt_enc.rsmodified+122 -0
| @@ -32,6 +32,7 @@ | ||
| 32 | 32 | |
| 33 | 33 | use ec_core::{Error, Result}; |
| 34 | 34 | |
| 35 | +use crate::analysis::{AnalysisInfo, TonalityAnalysis}; | |
| 35 | 36 | use crate::celt::{ |
| 36 | 37 | self, BETA_COEF, BETA_INTRA, BITRES, CACHE_CAPS, E_BANDS, E_MEANS, E_PROB_MODEL, LOG_N, |
| 37 | 38 | MAX_FINE_BITS, NB_BANDS, OVERLAP, PRED_COEF, PREEMPH, QTHETA_OFFSET, QTHETA_OFFSET_TWOPHASE, |
| @@ -59,6 +60,55 @@ fn tf_boost_params() -> (f32, f32, bool, f32) { | ||
| 59 | 60 | }) |
| 60 | 61 | } |
| 61 | 62 | |
| 63 | +/// Which consumers of the [`crate::analysis`] tonality analysis are live. | |
| 64 | +/// | |
| 65 | +/// The analysis itself runs whenever `EC_OPUS_ANALYSIS` is not `0`; each | |
| 66 | +/// downstream term was measured on the 12-row library gate on its own, so | |
| 67 | +/// each keeps its own switch (`1`/`0`) around the shipped default: | |
| 68 | +/// `EC_OPUS_TRIM_TONAL` (`alloc_trim`'s `tonality_slope` term), | |
| 69 | +/// `EC_OPUS_TRIM13` (the continuous 1.3 trim the term belongs to), | |
| 70 | +/// `EC_OPUS_VBR_TONAL` (`compute_vbr`'s tonality boost) and | |
| 71 | +/// `EC_OPUS_VBR_ACT` (`compute_vbr`'s low-activity cut). | |
| 72 | +/// | |
| 73 | +/// MEASURED (lane opustonal, four detached 12-row library-gate runs against | |
| 74 | +/// the shipped rows `lanes/opus-opustr2-r1.sweep.txt`). The analysis itself is | |
| 75 | +/// byte-neutral -- no consumer reads it unless its own switch is on -- and | |
| 76 | +/// **every consumer arm is rejected by the KEEP rule**, so all three default | |
| 77 | +/// off. err_ratio, better rows first, blocking rows named: | |
| 78 | +/// | |
| 79 | +/// | arm | better | blocking | | |
| 80 | +/// |---|---|---| | |
| 81 | +/// | 1.3 trim + tonality term (`-r1`) | nik@64 .708→.647, reelo@64 1.510→1.071, reelo@96 .940→.898, reell@96 4.499→2.458, dl8a .552/.374→.512/.336 | nik@96 .985→1.006, her@64 1.103→1.109, her@96 1.051→1.085, reell@64 2.697→2.872 | | |
| 82 | +/// | vbr tonality + activity (`-r2`) | hein@64 .853→.327, hein@96 .528→.426, nik@64 →.672, dl8a@64 →.541 | nik@96 →1.061, reelo@96 →1.165, her@64 →1.104, her@96 →1.060, dl8a@96 →.414 | | |
| 83 | +/// | vbr activity alone (`-r3`) | hein@64 →.430, hein@96 →.408, nik@64 →.657, dl8a@64 →.511 | nik@96 →.990, her@64 →1.104, her@96 →1.071, dl8a@96 →.382 | | |
| 84 | +/// | all four + `EC_OPUS_ALIGN=1` (`-r4`) | reell@64 →1.865, reell@96 →3.986, reelo@64 →1.123, reelo@96 →.891, her@64 →.952 | nik@64 →1.014, nik@96 →1.842, her@96 →1.217, dl8a →1.162/.988, hein →.971/1.514 | | |
| 85 | +/// | |
| 86 | +/// `corr_ours` is never worse by more than .0005 on any unaligned arm (it is | |
| 87 | +/// *better* on every row of `-r2`/`-r3`), so corr is not what rejects them. | |
| 88 | +/// The `alloc_trim` term is small by construction -- `tonality_slope` averages | |
| 89 | +/// -.09 (speech) to -.17 (music), so the term is ±.08 of a trim step and | |
| 90 | +/// rounds away entirely on the shipped discrete ladder, which is why no | |
| 91 | +/// discrete-ladder arm was run. The activity cut is the nearest miss ever | |
| 92 | +/// measured on this gate for a speech source (hein halves) and is blocked by | |
| 93 | +/// her@96 +.020 and nik@96 +.005 -- the same two rows that have blocked the | |
| 94 | +/// tf boost. Aligned, the analysis moves reell/reelo/her@64 a long way but leaves | |
| 95 | +/// the aligned blocking set (nik, her@96, dl8a, hein) exactly where the | |
| 96 | +/// analysis-free aligned arms left it, so the alignment default stays off. | |
| 97 | +fn analysis_params() -> (bool, bool, bool, bool) { | |
| 98 | + static P: std::sync::OnceLock<(bool, bool, bool, bool)> = std::sync::OnceLock::new(); | |
| 99 | + *P.get_or_init(|| { | |
| 100 | + let b = |name: &str, dflt: bool| { | |
| 101 | + std::env::var(name).map(|v| v != "0").unwrap_or(dflt) | |
| 102 | + }; | |
| 103 | + ( | |
| 104 | + b("EC_OPUS_ANALYSIS", true), | |
| 105 | + b("EC_OPUS_TRIM_TONAL", false), | |
| 106 | + b("EC_OPUS_VBR_TONAL", false), | |
| 107 | + b("EC_OPUS_VBR_ACT", false), | |
| 108 | + ) | |
| 109 | + }) | |
| 110 | +} | |
| 111 | + | |
| 62 | 112 | const CACHE_BITS: &[u8] = &crate::celt::CACHE_BITS; |
| 63 | 113 | const INV_TABLE: [u8; 128] = [ |
| 64 | 114 | 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25, |
| @@ -351,6 +401,18 @@ pub struct CeltFrameDiag { | ||
| 351 | 401 | pub pulses: [i32; NB_BANDS], |
| 352 | 402 | /// Per-band fine-energy precision bits. |
| 353 | 403 | pub fine_quant: [i32; NB_BANDS], |
| 404 | + /// Whether the tonality analysis produced a usable window for this frame. | |
| 405 | + pub analysis_valid: bool, | |
| 406 | + /// `AnalysisInfo::music_prob` — 0 is speech, 1 is music. | |
| 407 | + pub music_prob: f32, | |
| 408 | + /// `AnalysisInfo::tonality`. | |
| 409 | + pub tonality: f32, | |
| 410 | + /// `AnalysisInfo::tonality_slope`, `alloc_trim`'s input. | |
| 411 | + pub tonality_slope: f32, | |
| 412 | + /// `AnalysisInfo::activity`, `compute_vbr`'s input. | |
| 413 | + pub activity: f32, | |
| 414 | + /// `AnalysisInfo::bandwidth`, the detected bandwidth band index. | |
| 415 | + pub analysis_bandwidth: i32, | |
| 354 | 416 | } |
| 355 | 417 | |
| 356 | 418 | impl Default for CeltFrameDiag { |
| @@ -371,6 +433,12 @@ impl Default for CeltFrameDiag { | ||
| 371 | 433 | band_log_e: [0.0; 2 * NB_BANDS], |
| 372 | 434 | pulses: [0; NB_BANDS], |
| 373 | 435 | fine_quant: [0; NB_BANDS], |
| 436 | + analysis_valid: false, | |
| 437 | + music_prob: 0.0, | |
| 438 | + tonality: 0.0, | |
| 439 | + tonality_slope: 0.0, | |
| 440 | + activity: 0.0, | |
| 441 | + analysis_bandwidth: 0, | |
| 374 | 442 | } |
| 375 | 443 | } |
| 376 | 444 | } |
| @@ -448,6 +516,13 @@ pub struct CeltEncoder { | ||
| 448 | 516 | urow: Vec<u32>, |
| 449 | 517 | /// Per-frame diagnostics captured at the end of the last `encode` call. |
| 450 | 518 | last_diag: CeltFrameDiag, |
| 519 | + /// libopus `st->analysis`: the tonality/music-probability estimator, run | |
| 520 | + /// on the *undelayed* API input at the top of every `encode` call exactly | |
| 521 | + /// where `opus_encode()` runs `run_analysis()`. | |
| 522 | + analysis: TonalityAnalysis, | |
| 523 | + /// The window that covers the frame being coded, or the default | |
| 524 | + /// (`valid == false`) state when the analysis is off. | |
| 525 | + info: AnalysisInfo, | |
| 451 | 526 | } |
| 452 | 527 | |
| 453 | 528 | impl CeltEncoder { |
| @@ -505,6 +580,8 @@ impl CeltEncoder { | ||
| 505 | 580 | pvq_sign: vec![0; max_n], |
| 506 | 581 | urow: vec![0; 1280], |
| 507 | 582 | last_diag: CeltFrameDiag::default(), |
| 583 | + analysis: TonalityAnalysis::new((48000 / upsample) as u32), | |
| 584 | + info: AnalysisInfo::default(), | |
| 508 | 585 | } |
| 509 | 586 | } |
| 510 | 587 | |
| @@ -547,6 +624,8 @@ impl CeltEncoder { | ||
| 547 | 624 | self.vbr_count = 0; |
| 548 | 625 | self.stereo_saving = 0.0; |
| 549 | 626 | self.spec_avg = 0.0; |
| 627 | + self.analysis.reset(); | |
| 628 | + self.info = AnalysisInfo::default(); | |
| 550 | 629 | } |
| 551 | 630 | |
| 552 | 631 | /// Encodes one frame of interleaved `f32` (`frame_size` samples per |
| @@ -592,6 +671,16 @@ impl CeltEncoder { | ||
| 592 | 671 | } |
| 593 | 672 | let end = end.clamp(start + 1, NB_BANDS); |
| 594 | 673 | |
| 674 | + // libopus `opus_encode_native` runs the tonality analysis on the raw | |
| 675 | + // API frame before anything else; the CELT layer's own delay does not | |
| 676 | + // apply to it, so this reads `pcm` and not the delayed buffer. 24 is | |
| 677 | + // libopus's `lsb_depth` default for the float API. | |
| 678 | + self.info = if analysis_params().0 { | |
| 679 | + self.analysis.run(pcm, in_n, c, 24) | |
| 680 | + } else { | |
| 681 | + AnalysisInfo::default() | |
| 682 | + }; | |
| 683 | + | |
| 595 | 684 | let mut nb_compressed = enc.storage(); |
| 596 | 685 | let mut nb_available = nb_compressed as i32; |
| 597 | 686 | // The target rate in 1/8-bit units per frame, VBR only. |
| @@ -942,6 +1031,7 @@ impl CeltEncoder { | ||
| 942 | 1031 | let base_target = |
| 943 | 1032 | vbr_rate + (self.vbr_offset >> lm_diff) - ((40 * c as i32 + 20) << BITRES); |
| 944 | 1033 | let mut target = base_target; |
| 1034 | + let (_, _, vbr_tonal, vbr_act) = analysis_params(); | |
| 945 | 1035 | // compute_vbr (libopus celt_encoder.c:1605-1716), float-mode. |
| 946 | 1036 | // Skipped: activity, tonality, surround_mask (analysis->valid= |
| 947 | 1037 | // false, has_surround_mask=0, pitch_change=0, lfe=0). The qext |
| @@ -956,6 +1046,12 @@ impl CeltEncoder { | ||
| 956 | 1046 | if c == 2 { |
| 957 | 1047 | coded_bins += E_BANDS[self.intensity.min(coded_bands)] << lm; |
| 958 | 1048 | } |
| 1049 | + // Low-activity cut (C:1632-1633). REJECTED on the 12-row gate | |
| 1050 | + // (lane opustonal r2/r3): halves hein's err_ratio but costs | |
| 1051 | + // her@96 +.020 and nik@96 +.005 -- see `analysis_params`. | |
| 1052 | + if vbr_act && self.info.valid && self.info.activity < 0.4 { | |
| 1053 | + target -= ((coded_bins << BITRES) as f32 * (0.4 - self.info.activity)) as i32; | |
| 1054 | + } | |
| 959 | 1055 | // Stereo savings (C:1636-1649) |
| 960 | 1056 | if c == 2 { |
| 961 | 1057 | let coded_stereo_bands = self.intensity.min(coded_bands); |
| @@ -1010,6 +1106,14 @@ impl CeltEncoder { | ||
| 1010 | 1106 | let k_eff = if fires { k } else { 1.0 }; |
| 1011 | 1107 | target += (k_eff * (tf_estimate - off) * target as f32) as i32; |
| 1012 | 1108 | } |
| 1109 | + // Tonality boost (C:1658-1669), compensating for the average. | |
| 1110 | + // REJECTED on the 12-row gate (lane opustonal r2): it is what | |
| 1111 | + // pushes reelo@96 .940->1.165 -- see `analysis_params`. | |
| 1112 | + // `pitch_change` is 0 here (no prefilter), so its term is absent. | |
| 1113 | + if vbr_tonal && self.info.valid { | |
| 1114 | + let tonal = (self.info.tonality - 0.15).max(0.0) - 0.12; | |
| 1115 | + target += ((coded_bins << BITRES) as f32 * 1.2 * tonal) as i32; | |
| 1116 | + } | |
| 1013 | 1117 | // floor depth cap (C:1683-1695): SHR32/MULT16_32_Q15 are identity. |
| 1014 | 1118 | { |
| 1015 | 1119 | let bins = E_BANDS[NB_BANDS - 2] << lm; |
| @@ -1228,6 +1332,12 @@ impl CeltEncoder { | ||
| 1228 | 1332 | band_log_e, |
| 1229 | 1333 | pulses: self.pulses, |
| 1230 | 1334 | fine_quant: self.fine_quant, |
| 1335 | + analysis_valid: self.info.valid, | |
| 1336 | + music_prob: self.info.music_prob, | |
| 1337 | + tonality: self.info.tonality, | |
| 1338 | + tonality_slope: self.info.tonality_slope, | |
| 1339 | + activity: self.info.activity, | |
| 1340 | + analysis_bandwidth: self.info.bandwidth, | |
| 1231 | 1341 | }; |
| 1232 | 1342 | Ok(nb_compressed) |
| 1233 | 1343 | } |
| @@ -1651,14 +1761,26 @@ impl CeltEncoder { | ||
| 1651 | 1761 | * (2 + 2 * i as i32 - NB_BANDS as i32) as f32; |
| 1652 | 1762 | } |
| 1653 | 1763 | } |
| 1764 | + // libopus's `analysis->valid` term (celt_encoder.c:935-939): a tonal | |
| 1765 | + // spectrum wants a lower trim. It is only defined on the continuous | |
| 1766 | + // 1.3 trim; on the discrete ladder it can only be applied rounded, | |
| 1767 | + // where it is inert (|term| <= .08). REJECTED with the 1.3 trim it | |
| 1768 | + // belongs to -- see `analysis_params`, lane opustonal r1. | |
| 1769 | + let tonal_trim = if analysis_params().1 && self.info.valid { | |
| 1770 | + (2.0 * (self.info.tonality_slope + 0.05)).clamp(-2.0, 2.0) | |
| 1771 | + } else { | |
| 1772 | + 0.0 | |
| 1773 | + }; | |
| 1654 | 1774 | if cont { |
| 1655 | 1775 | // libopus divides the same sum by `C*(end-1)`; the ladder above |
| 1656 | 1776 | // carries an extra factor of two in its thresholds. |
| 1657 | 1777 | let diff = diff / (c * (end - 1)) as f32; |
| 1658 | 1778 | trim_f -= ((diff + 1.0) / 6.0).clamp(-2.0, 2.0); |
| 1659 | 1779 | trim_f -= 2.0 * tf_estimate; |
| 1780 | + trim_f -= tonal_trim; | |
| 1660 | 1781 | return (trim_f + 0.5).floor().clamp(0.0, 10.0) as i32; |
| 1661 | 1782 | } |
| 1783 | + trim -= tonal_trim.round_ties_even() as i32; | |
| 1662 | 1784 | diff /= (2 * c * (end - 1)) as f32; |
| 1663 | 1785 | if diff > 2.0 { |
| 1664 | 1786 | trim -= 1; |
crates/ec-opus/src/encoder.rsmodified+9 -0
| @@ -273,11 +273,20 @@ impl Encoder { | ||
| 273 | 273 | /// | + tf boost k=1.25 (`-r4`) | 5 | same 7 | |
| 274 | 274 | /// | + tf boost k=1.5 (`-r5`) | 5 | same 7 | |
| 275 | 275 | /// | + two-pass *and* k=1.25, the combined best (`-r7`) | 5 | same 7: nik@64 .773, nik@96 1.801, her@96 1.114, dl8a@64 1.181, dl8a@96 1.014, hein@64 .977, hein@96 1.398 | |
| 276 | + /// | + the whole libopus tonality analysis and its `alloc_trim`/`compute_vbr` consumers (`opus-opustonal-r4`) | 5 | same 7: nik@64 1.014, nik@96 1.842, her@96 1.217, dl8a@64 1.162, dl8a@96 .988, hein@64 .971, hein@96 1.514 | | |
| 276 | 277 | /// |
| 277 | 278 | /// The best aligned arm on the KEEP rule is the delay alone without |
| 278 | 279 | /// `dc_reject` (5 rows worse); every arm that adds `dc_reject` puts dl8a |
| 279 | 280 | /// in the blocking set too. |
| 280 | 281 | /// |
| 282 | + /// The last row is the one that was expected to rescue this: libopus's | |
| 283 | + /// actual operating point is the aligned spectrum *with* `analysis->valid` | |
| 284 | + /// true, and running the real analysis (`crates/ec-opus/src/analysis.rs`, | |
| 285 | + /// `music_prob` matching a patched-libopus trace to .001) moves reell@64 | |
| 286 | + /// 2.697→1.865, reell@96 4.499→3.986, reelo@64 1.510→1.123 and her@64 | |
| 287 | + /// 1.103→.952 — the largest movement any arm has produced on those rows — | |
| 288 | + /// and still leaves the same seven rows worse. | |
| 289 | + /// | |
| 281 | 290 | /// The blocking set (nik, her@96, dl8a, hein) is the same in every arm and |
| 282 | 291 | /// no arm moves any of those rows back towards its shipped value, so the |
| 283 | 292 | /// misfit is not one of the swept constants. Turning this on also costs |
crates/ec-opus/src/lib.rsmodified+1 -0
| @@ -61,6 +61,7 @@ | ||
| 61 | 61 | #![forbid(unsafe_code)] |
| 62 | 62 | #![warn(missing_docs)] |
| 63 | 63 | |
| 64 | +pub(crate) mod analysis; | |
| 64 | 65 | pub mod celt; |
| 65 | 66 | pub mod celt_enc; |
| 66 | 67 | pub mod encoder; |
crates/ec-opus/tests/conformance.rsmodified+101 -0
| @@ -5716,3 +5716,104 @@ fn frame_decisions_vs_libopus() { | ||
| 5716 | 5716 | } |
| 5717 | 5717 | |
| 5718 | 5718 | const NB_BANDS_DIAG: usize = ec_opus::celt::NB_BANDS; |
| 5719 | + | |
| 5720 | +/// The ported tonality analysis (`crates/ec-opus/src/analysis.rs`) must | |
| 5721 | +/// actually tell speech from music, since `music_prob` is what libopus's mode | |
| 5722 | +/// and bandwidth decisions read. Drives the real entry surface | |
| 5723 | +/// (`Encoder::encode_float`) over 30 s of each gate source and reads the | |
| 5724 | +/// per-frame value back through [`CeltFrameDiag`]. Speech (hein) must land | |
| 5725 | +/// clearly below the two music sources (reell, reelo). | |
| 5726 | +#[test] | |
| 5727 | +#[ignore = "needs the local library sources; run with --ignored"] | |
| 5728 | +fn analysis_music_prob_separates_speech_from_music() { | |
| 5729 | + const SECS: f64 = 30.0; | |
| 5730 | + const FRAME: usize = 960; | |
| 5731 | + const CHANNELS: usize = 2; | |
| 5732 | + let sources: &[(&str, &str, bool)] = &[ | |
| 5733 | + ("hein", "~/Downloads/reelm.mp3", false), | |
| 5734 | + ("reell", "~/Music/reell.mp4", true), | |
| 5735 | + ("reelo", "~/Music/reelo.mp3", true), | |
| 5736 | + ]; | |
| 5737 | + // Oracle: libopus v1.6 built from source with one fprintf of `analysis_info` | |
| 5738 | + // after `run_analysis()` in `opus_encode_native`, fed the same 30 s of | |
| 5739 | + // ffmpeg-decoded f32 at the same settings, 1498-1499 valid windows each. | |
| 5740 | + // Means of (music_prob, tonality, tonality_slope, activity). | |
| 5741 | + let oracle = |tag: &str| -> Option<[f64; 4]> { | |
| 5742 | + match tag { | |
| 5743 | + "hein" => Some([0.160, 0.155, -0.0876, 0.567]), | |
| 5744 | + "reell" => Some([0.991, 0.362, -0.1680, 0.701]), | |
| 5745 | + "reelo" => Some([0.977, 0.313, -0.1333, 0.735]), | |
| 5746 | + _ => None, | |
| 5747 | + } | |
| 5748 | + }; | |
| 5749 | + let mut means: Vec<(String, bool, f64)> = Vec::new(); | |
| 5750 | + for (tag, path, is_music) in sources { | |
| 5751 | + let src = shellexpand(path); | |
| 5752 | + if !src.exists() { | |
| 5753 | + eprintln!("SKIP {tag}: missing {}", src.display()); | |
| 5754 | + continue; | |
| 5755 | + } | |
| 5756 | + let pcm = ffmpeg_decode_pcm(&src, SECS); | |
| 5757 | + let mut enc = Encoder::new(48000, CHANNELS, Application::Audio).expect("encoder"); | |
| 5758 | + enc.set_bitrate(96_000); | |
| 5759 | + enc.set_vbr_constrained(true); | |
| 5760 | + let mut out = vec![0u8; 4000]; | |
| 5761 | + let mut sum = [0.0f64; 4]; | |
| 5762 | + let mut n = 0usize; | |
| 5763 | + let mut valid = 0usize; | |
| 5764 | + for chunk in pcm.chunks_exact(FRAME * CHANNELS) { | |
| 5765 | + enc.encode_float(chunk, FRAME, &mut out).expect("encode"); | |
| 5766 | + let d = enc.last_celt_diag(); | |
| 5767 | + n += 1; | |
| 5768 | + if d.analysis_valid { | |
| 5769 | + valid += 1; | |
| 5770 | + sum[0] += f64::from(d.music_prob); | |
| 5771 | + sum[1] += f64::from(d.tonality); | |
| 5772 | + sum[2] += f64::from(d.tonality_slope); | |
| 5773 | + sum[3] += f64::from(d.activity); | |
| 5774 | + } | |
| 5775 | + } | |
| 5776 | + assert!(valid * 10 > n * 9, "{tag}: analysis valid on only {valid}/{n} frames"); | |
| 5777 | + let m = sum.map(|v| v / valid as f64); | |
| 5778 | + println!( | |
| 5779 | + "analysis {tag}: music {:.3} tonality {:.3} slope {:.4} activity {:.3} over {valid}/{n} frames (music={is_music})", | |
| 5780 | + m[0], m[1], m[2], m[3] | |
| 5781 | + ); | |
| 5782 | + if let Some(o) = oracle(tag) { | |
| 5783 | + for (i, name) in ["music_prob", "tonality", "tonality_slope", "activity"] | |
| 5784 | + .iter() | |
| 5785 | + .enumerate() | |
| 5786 | + { | |
| 5787 | + assert!( | |
| 5788 | + (m[i] - o[i]).abs() < 0.02, | |
| 5789 | + "{tag} {name}: ours {:.4} vs libopus {:.4}", | |
| 5790 | + m[i], | |
| 5791 | + o[i] | |
| 5792 | + ); | |
| 5793 | + } | |
| 5794 | + } | |
| 5795 | + means.push(((*tag).to_string(), *is_music, m[0])); | |
| 5796 | + } | |
| 5797 | + if means.len() < 2 { | |
| 5798 | + eprintln!("SKIP: fewer than two sources present"); | |
| 5799 | + return; | |
| 5800 | + } | |
| 5801 | + let speech: Vec<_> = means.iter().filter(|m| !m.1).collect(); | |
| 5802 | + let music: Vec<_> = means.iter().filter(|m| m.1).collect(); | |
| 5803 | + for s in &speech { | |
| 5804 | + assert!(s.2 < 0.5, "{}: speech music_prob {:.3} should be < 0.5", s.0, s.2); | |
| 5805 | + for m in &music { | |
| 5806 | + assert!( | |
| 5807 | + m.2 - s.2 > 0.2, | |
| 5808 | + "{} ({:.3}) does not separate from {} ({:.3}) by 0.2", | |
| 5809 | + m.0, | |
| 5810 | + m.2, | |
| 5811 | + s.0, | |
| 5812 | + s.2 | |
| 5813 | + ); | |
| 5814 | + } | |
| 5815 | + } | |
| 5816 | + for m in &music { | |
| 5817 | + assert!(m.2 > 0.5, "{}: music music_prob {:.3} should be > 0.5", m.0, m.2); | |
| 5818 | + } | |
| 5819 | +} |
lanes/opus-opustonal-r1.sweep.txtadded+15 -0
| @@ -0,0 +1,15 @@ | ||
| 1 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | |
| 2 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | |
| 3 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | |
| 4 | +nik 64 69.1 69.2 -0.1 0.9880 0.9864 -0.0017 -81.62 -154.78 0.647 0.9762 0.9745 0 0 | |
| 5 | +nik 96 100.9 101.0 -0.1 0.9943 0.9940 -0.0002 -32.19 -31.55 1.006 0.9882 0.9888 0 0 | |
| 6 | +reelo 64 63.6 63.6 -0.0 0.9892 0.9866 -0.0026 -135.40 -123.36 1.071 0.9701 0.9686 0 0 | |
| 7 | +reelo 96 93.4 93.9 -0.5 0.9946 0.9937 -0.0008 -107.07 -125.18 0.898 0.9861 0.9854 0 0 | |
| 8 | +her 64 67.7 67.9 -0.4 0.9829 0.9794 -0.0035 -367.34 -339.00 1.109 0.9468 0.9406 0 0 | |
| 9 | +her 96 101.0 101.5 -0.5 0.9911 0.9902 -0.0009 -208.79 -191.50 1.085 0.9720 0.9728 0 0 | |
| 10 | +reell 64 71.3 71.3 -0.0 0.9903 0.9880 -0.0023 -369.03 -128.39 2.872 0.9712 0.9644 0 0 | |
| 11 | +reell 96 105.3 105.4 -0.0 0.9953 0.9948 -0.0005 -165.27 -29.16 2.458 0.9847 0.9804 0 0 | |
| 12 | +dl8a 64 65.7 65.8 -0.1 0.9882 0.9865 -0.0017 -210.74 -378.84 0.512 0.9781 0.9723 0 0 | |
| 13 | +dl8a 96 96.5 96.8 -0.3 0.9938 0.9934 -0.0004 -156.76 -423.47 0.336 0.9874 0.9864 0 0 | |
| 14 | +hein 64 64.8 64.9 -0.2 0.9888 0.9874 -0.0014 -344.06 -388.31 0.853 0.9318 0.9148 0 0 | |
| 15 | +hein 96 86.8 86.9 -0.2 0.9946 0.9930 -0.0016 -117.93 -246.46 0.528 0.9689 0.9499 0 0 |
lanes/opus-opustonal-r2.sweep.txtadded+15 -0
| @@ -0,0 +1,15 @@ | ||
| 1 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | |
| 2 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | |
| 3 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | |
| 4 | +nik 64 69.0 69.2 -0.3 0.9887 0.9864 -0.0023 -87.41 -154.78 0.672 0.9772 0.9745 0 0 | |
| 5 | +nik 96 100.8 101.0 -0.2 0.9947 0.9940 -0.0007 -38.39 -31.55 1.061 0.9891 0.9888 0 0 | |
| 6 | +reelo 64 63.3 63.6 -0.4 0.9892 0.9866 -0.0026 -202.87 -123.36 1.510 0.9678 0.9686 0 0 | |
| 7 | +reelo 96 92.8 93.9 -1.1 0.9945 0.9937 -0.0008 -152.87 -125.18 1.165 0.9845 0.9854 0 0 | |
| 8 | +her 64 67.4 67.9 -0.7 0.9833 0.9794 -0.0039 -366.15 -339.00 1.104 0.9477 0.9406 0 0 | |
| 9 | +her 96 100.3 101.5 -1.2 0.9912 0.9902 -0.0010 -203.77 -191.50 1.060 0.9729 0.9728 0 0 | |
| 10 | +reell 64 71.3 71.3 -0.0 0.9908 0.9880 -0.0028 -351.70 -128.39 2.697 0.9716 0.9644 0 0 | |
| 11 | +reell 96 105.3 105.4 -0.0 0.9956 0.9948 -0.0008 -301.83 -29.16 4.499 0.9850 0.9804 0 0 | |
| 12 | +dl8a 64 65.6 65.8 -0.3 0.9888 0.9865 -0.0024 -223.26 -378.84 0.541 0.9796 0.9723 0 0 | |
| 13 | +dl8a 96 96.5 96.8 -0.4 0.9942 0.9934 -0.0008 -198.74 -423.47 0.414 0.9882 0.9864 0 0 | |
| 14 | +hein 64 64.6 64.9 -0.4 0.9887 0.9874 -0.0014 -129.55 -388.31 0.327 0.9287 0.9148 0 0 | |
| 15 | +hein 96 86.7 86.9 -0.2 0.9946 0.9930 -0.0016 -84.08 -246.46 0.426 0.9680 0.9499 0 0 |
lanes/opus-opustonal-r3.sweep.txtadded+15 -0
| @@ -0,0 +1,15 @@ | ||
| 1 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | |
| 2 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | |
| 3 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | |
| 4 | +nik 64 69.1 69.2 -0.1 0.9887 0.9864 -0.0024 -83.85 -154.78 0.657 0.9766 0.9745 0 0 | |
| 5 | +nik 96 101.0 101.0 -0.0 0.9947 0.9940 -0.0007 -30.46 -31.55 0.990 0.9889 0.9888 0 0 | |
| 6 | +reelo 64 63.6 63.6 -0.0 0.9893 0.9866 -0.0027 -202.82 -123.36 1.510 0.9702 0.9686 0 0 | |
| 7 | +reelo 96 93.4 93.9 -0.5 0.9947 0.9937 -0.0009 -114.74 -125.18 0.940 0.9862 0.9854 0 0 | |
| 8 | +her 64 67.7 67.9 -0.4 0.9835 0.9794 -0.0040 -366.02 -339.00 1.104 0.9480 0.9406 0 0 | |
| 9 | +her 96 101.1 101.5 -0.4 0.9914 0.9902 -0.0012 -206.12 -191.50 1.071 0.9729 0.9728 0 0 | |
| 10 | +reell 64 71.3 71.3 -0.0 0.9908 0.9880 -0.0029 -351.70 -128.39 2.697 0.9721 0.9644 0 0 | |
| 11 | +reell 96 105.3 105.4 -0.0 0.9956 0.9948 -0.0008 -301.83 -29.16 4.499 0.9855 0.9804 0 0 | |
| 12 | +dl8a 64 65.7 65.8 -0.2 0.9890 0.9865 -0.0025 -210.46 -378.84 0.511 0.9788 0.9723 0 0 | |
| 13 | +dl8a 96 96.6 96.8 -0.3 0.9943 0.9934 -0.0009 -181.52 -423.47 0.382 0.9882 0.9864 0 0 | |
| 14 | +hein 64 64.7 64.9 -0.3 0.9893 0.9874 -0.0020 -181.35 -388.31 0.430 0.9348 0.9148 0 0 | |
| 15 | +hein 96 86.7 86.9 -0.3 0.9949 0.9930 -0.0018 -77.72 -246.46 0.408 0.9707 0.9499 0 0 |
lanes/opus-opustonal-r4.sweep.txtadded+15 -0
| @@ -0,0 +1,15 @@ | ||
| 1 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | |
| 2 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | |
| 3 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | |
| 4 | +nik 64 69.0 69.2 -0.3 0.9875 0.9864 -0.0012 -157.46 -154.78 1.014 0.9763 0.9745 0 0 | |
| 5 | +nik 96 100.8 101.0 -0.2 0.9939 0.9940 +0.0002 -116.75 -31.55 1.842 0.9883 0.9888 0 0 | |
| 6 | +reelo 64 63.3 63.6 -0.4 0.9883 0.9866 -0.0017 -143.92 -123.36 1.123 0.9695 0.9686 0 0 | |
| 7 | +reelo 96 92.8 93.9 -1.1 0.9940 0.9937 -0.0002 -105.85 -125.18 0.891 0.9858 0.9854 0 0 | |
| 8 | +her 64 67.4 67.9 -0.8 0.9821 0.9794 -0.0026 -325.83 -339.00 0.952 0.9464 0.9406 0 0 | |
| 9 | +her 96 100.3 101.5 -1.2 0.9910 0.9902 -0.0007 -234.41 -191.50 1.217 0.9754 0.9728 0 0 | |
| 10 | +reell 64 71.3 71.3 -0.0 0.9898 0.9880 -0.0019 -257.24 -128.39 1.865 0.9697 0.9644 0 0 | |
| 11 | +reell 96 105.3 105.4 -0.0 0.9948 0.9948 -0.0001 -271.56 -29.16 3.986 0.9842 0.9804 0 0 | |
| 12 | +dl8a 64 65.6 65.8 -0.3 0.9876 0.9865 -0.0011 -422.17 -378.84 1.162 0.9778 0.9723 0 0 | |
| 13 | +dl8a 96 96.5 96.8 -0.4 0.9932 0.9934 +0.0002 -420.05 -423.47 0.988 0.9870 0.9864 0 0 | |
| 14 | +hein 64 64.6 64.9 -0.4 0.9886 0.9874 -0.0012 -380.03 -388.31 0.971 0.9307 0.9148 0 0 | |
| 15 | +hein 96 86.7 86.9 -0.2 0.9945 0.9930 -0.0015 -351.77 -246.46 1.514 0.9683 0.9499 0 0 |