Paste tf_analysis port, gate OFF
Browse files at this commitc1fe246c44e4de261299cf5532bf2a7369fade10Parent: 74c150a- feat
- paste tf_analysis port, gate OFF
3 changed files +343 -77
- crates/ec-opus/src/celt_enc.rs +241 -77
- lanes/opus-64-r4.report.md +95 -0
- lanes/opus-64-r4.sweep.txt +7 -0
crates/ec-opus/src/celt_enc.rsmodified+241 -77
| @@ -11,12 +11,15 @@ | |||
| 11 | //! which removes the most expensive analysis in the reference encoder (an | 11 | //! which removes the most expensive analysis in the reference encoder (an |
| 12 | //! autocorrelation search over 1024 lags). Same choice the reference makes | 12 | //! autocorrelation search over 1024 lags). Same choice the reference makes |
| 13 | //! below complexity 5. | 13 | //! below complexity 5. |
| 14 | -//! - **No time/frequency Viterbi.** Raw `tf_res` is `is_transient` in every | 14 | +//! - **Time/frequency Viterbi ported but gated off.** `tf_analysis()` and the |
| 15 | -//! band (the reference's no-analysis default): with `tf_select = 0` the | 15 | +//! libopus float-mode `transient_analysis()` (returning `tf_estimate`/ |
| 16 | -//! tf table maps it to "no change", so long-block frames keep full | 16 | +//! `tf_chan`) are ported from the reference; `const TF_ANALYSIS: bool` |
| 17 | -//! frequency resolution and transient frames keep their short blocks. (Raw | 17 | +//! gates the Viterbi search. It is `false`: the 2-row gate (reelm/hein @64k) |
| 18 | -//! zeros in a transient frame would map to `+LM`, merging the short blocks | 18 | +//! vs the no-analysis baseline showed both corr gaps grow and minsec drop, |
| 19 | -//! back into one long block and smearing pre-echo over the whole frame.) | 19 | +//! so the search is disabled. With the flag off, `tf_res` falls back to the |
| 20 | +//! reference's no-analysis default — raw `is_transient` per band, which under | ||
| 21 | +//! `tf_select = 0` codes "no tf change" (long frames keep full frequency | ||
| 22 | +//! resolution; transient frames keep their short blocks). | ||
| 20 | //! - **Fixed spreading.** `SPREAD_NORMAL` every frame, coded explicitly. | 23 | //! - **Fixed spreading.** `SPREAD_NORMAL` every frame, coded explicitly. |
| 21 | //! - **Single-pass coarse energy** with the reference's own delayed-intra | 24 | //! - **Single-pass coarse energy** with the reference's own delayed-intra |
| 22 | //! heuristic, not the two-pass encode-both-and-compare. | 25 | //! heuristic, not the two-pass encode-both-and-compare. |
| @@ -42,6 +45,16 @@ use crate::celt::{ | |||
| 42 | use crate::range::RangeEncoder; | 45 | use crate::range::RangeEncoder; |
| 43 | 46 | ||
| 44 | const CACHE_BITS: &[u8] = &crate::celt::CACHE_BITS; | 47 | const CACHE_BITS: &[u8] = &crate::celt::CACHE_BITS; |
| 48 | +const INV_TABLE: [u8; 128] = [ | ||
| 49 | + 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25, | ||
| 50 | + 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12, | ||
| 51 | + 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8, | ||
| 52 | + 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, | ||
| 53 | + 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, | ||
| 54 | + 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | ||
| 55 | + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, | ||
| 56 | + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, | ||
| 57 | +]; | ||
| 45 | 58 | ||
| 46 | // --------------------------------------------------------------------------- | 59 | // --------------------------------------------------------------------------- |
| 47 | // Forward MDCT | 60 | // Forward MDCT |
| @@ -253,6 +266,16 @@ fn stereo_itheta(x: &[f32], y: &[f32], stereo: bool, n: usize) -> i32 { | |||
| 253 | (0.5 + 16384.0 * TWO_OVER_PI * (side as f64).atan2(mid as f64)).floor() as i32 | 266 | (0.5 + 16384.0 * TWO_OVER_PI * (side as f64).atan2(mid as f64)).floor() as i32 |
| 254 | } | 267 | } |
| 255 | 268 | ||
| 269 | +/// `l1_metric()` from the reference: L1 norm scaled by `(1 + LM*bias)`. | ||
| 270 | +/// In float mode `MAC16_32_Q15(c, a*b) = c + a*b`, so this is `L1*(1 + LM*bias)`. | ||
| 271 | +fn l1_metric(tmp: &[f32], n: usize, lm: usize, bias: f32) -> f32 { | ||
| 272 | + let mut l1 = 0.0f32; | ||
| 273 | + for i in 0..n { | ||
| 274 | + l1 += tmp[i].abs(); | ||
| 275 | + } | ||
| 276 | + l1 * (1.0 + lm as f32 * bias) | ||
| 277 | +} | ||
| 278 | + | ||
| 256 | // --------------------------------------------------------------------------- | 279 | // --------------------------------------------------------------------------- |
| 257 | // The encoder | 280 | // The encoder |
| 258 | // --------------------------------------------------------------------------- | 281 | // --------------------------------------------------------------------------- |
| @@ -580,8 +603,13 @@ impl CeltEncoder { | |||
| 580 | // --- Transient flag ------------------------------------------------- | 603 | // --- Transient flag ------------------------------------------------- |
| 581 | let mut is_transient = false; | 604 | let mut is_transient = false; |
| 582 | let mut short_blocks = 0usize; | 605 | let mut short_blocks = 0usize; |
| 606 | + let mut tf_estimate = 0.0f32; | ||
| 607 | + let mut tf_chan = 0usize; | ||
| 583 | if lm > 0 && enc.tell() as i32 + 3 <= total_bits { | 608 | if lm > 0 && enc.tell() as i32 + 3 <= total_bits { |
| 584 | - is_transient = self.transient_analysis(n + OVERLAP, c, stride); | 609 | + let (ta, te, tc) = self.transient_analysis(n + OVERLAP, c, stride); |
| 610 | + is_transient = ta; | ||
| 611 | + tf_estimate = te; | ||
| 612 | + tf_chan = tc; | ||
| 585 | if is_transient { | 613 | if is_transient { |
| 586 | short_blocks = m; | 614 | short_blocks = m; |
| 587 | } | 615 | } |
| @@ -610,11 +638,22 @@ impl CeltEncoder { | |||
| 610 | } | 638 | } |
| 611 | } | 639 | } |
| 612 | 640 | ||
| 613 | - // --- tf_res: the reference's no-analysis default -------------------- | 641 | + // --- tf_res --------------------------------------------------------- |
| 614 | - // Raw `is_transient` per band + `tf_select = 0` codes "no tf change"; | 642 | + // `TF_ANALYSIS`: run the libopus Viterbi tf search when there are |
| 615 | - // raw zeros on a transient frame would code `+LM` (undo the shorts). | 643 | + // enough bits; otherwise fall back to the no-analysis default (raw |
| 616 | - self.tf_res = [i32::from(is_transient); NB_BANDS]; | 644 | + // `is_transient` per band codes "no tf change" under tf_select=0). |
| 617 | - let tf_select = 0usize; | 645 | + const TF_ANALYSIS: bool = false; |
| 646 | + let tf_select = if TF_ANALYSIS && effective_bytes >= 15 * c as i32 { | ||
| 647 | + let lambda = (80i32).max(20480 / effective_bytes + 2); | ||
| 648 | + let mut importance = [0i32; NB_BANDS]; | ||
| 649 | + for i in start..end { | ||
| 650 | + importance[i] = 13; | ||
| 651 | + } | ||
| 652 | + self.tf_analysis(end, is_transient, lambda, n, lm, tf_estimate, tf_chan, &importance) | ||
| 653 | + } else { | ||
| 654 | + self.tf_res = [i32::from(is_transient); NB_BANDS]; | ||
| 655 | + 0usize | ||
| 656 | + }; | ||
| 618 | 657 | ||
| 619 | // --- Coarse energy -------------------------------------------------- | 658 | // --- Coarse energy -------------------------------------------------- |
| 620 | let two_pass = false; | 659 | let two_pass = false; |
| @@ -1017,72 +1056,80 @@ impl CeltEncoder { | |||
| 1017 | } | 1056 | } |
| 1018 | // -- Analysis ------------------------------------------------------------ | 1057 | // -- Analysis ------------------------------------------------------------ |
| 1019 | 1058 | ||
| 1020 | - /// `transient_analysis()`: a high-passed peak-decay detector, straight | 1059 | + /// `transient_analysis()` — libopus float-mode algorithm. |
| 1021 | - /// from the reference. | 1060 | + /// Returns `(is_transient, tf_estimate, tf_chan)`. |
| 1022 | - fn transient_analysis(&mut self, len: usize, c: usize, stride: usize) -> bool { | 1061 | + fn transient_analysis(&mut self, len: usize, c: usize, stride: usize) -> (bool, f32, usize) { |
| 1023 | let tmp = &mut self.transient_tmp[..len]; | 1062 | let tmp = &mut self.transient_tmp[..len]; |
| 1024 | - if c == 1 { | 1063 | + let len2 = len / 2; |
| 1025 | - tmp.copy_from_slice(&self.in_buf[..len]); | 1064 | + let mut mask_metric: i32 = 0; |
| 1026 | - } else { | 1065 | + let mut tf_chan: usize = 0; |
| 1027 | - for (i, v) in tmp.iter_mut().enumerate() { | 1066 | + |
| 1028 | - *v = self.in_buf[i] + self.in_buf[stride + i]; | 1067 | + for ch in 0..c { |
| 1029 | - } | 1068 | + // Copy this channel's samples (C layout: in[i + c*len] = in_buf[ch*stride + i]) |
| 1030 | - } | 1069 | + tmp.copy_from_slice(&self.in_buf[ch * stride..ch * stride + len]); |
| 1031 | - let mut mem0 = 0.0f32; | 1070 | + |
| 1032 | - let mut mem1 = 0.0f32; | 1071 | + // High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) |
| 1033 | - for v in tmp.iter_mut() { | 1072 | + let mut mem0 = 0.0f32; |
| 1034 | - let x = *v; | 1073 | + let mut mem1 = 0.0f32; |
| 1035 | - let y = mem0 + x; | 1074 | + for v in tmp.iter_mut() { |
| 1036 | - mem0 = mem1 + y - 2.0 * x; | 1075 | + let x = *v; |
| 1037 | - mem1 = x - 0.5 * y; | 1076 | + let y = mem0 + x; |
| 1038 | - *v = y; | 1077 | + mem0 = mem1 + y - 2.0 * x; |
| 1039 | - } | 1078 | + mem1 = x - 0.5 * y; |
| 1040 | - for v in tmp.iter_mut().take(12) { | 1079 | + *v = y; |
| 1041 | - *v = 0.0; | 1080 | + } |
| 1042 | - } | 1081 | + // First few samples are bad because we don't propagate the memory |
| 1043 | - let block = OVERLAP / 2; | 1082 | + for v in tmp.iter_mut().take(12) { |
| 1044 | - let nbins = len / block; | 1083 | + *v = 0.0; |
| 1045 | - let mut bins = [0.0f32; 20]; | 1084 | + } |
| 1046 | - for (i, bin) in bins.iter_mut().enumerate().take(nbins) { | 1085 | + |
| 1047 | - let mut max_abs = 0.0f32; | 1086 | + // Forward pass: post-echo threshold (forward_decay = 0.0625) |
| 1048 | - for j in 0..block { | 1087 | + let mut mean = 0.0f32; |
| 1049 | - max_abs = max_abs.max(tmp[i * block + j].abs()); | 1088 | + let mut fm0 = 0.0f32; |
| 1050 | - } | 1089 | + for i in 0..len2 { |
| 1051 | - *bin = max_abs; | 1090 | + let x2 = tmp[2 * i] * tmp[2 * i] + tmp[2 * i + 1] * tmp[2 * i + 1]; |
| 1052 | - } | 1091 | + mean += x2; |
| 1053 | - let mut is_transient = false; | 1092 | + tmp[i] = fm0 + 0.0625 * (x2 - fm0); |
| 1054 | - #[allow(clippy::needless_range_loop)] | 1093 | + fm0 = tmp[i]; |
| 1055 | - for i in 0..nbins { | 1094 | + } |
| 1056 | - let t1 = 0.15 * bins[i]; | 1095 | + |
| 1057 | - let t2 = 0.4 * bins[i]; | 1096 | + // Backward pass: pre-echo threshold (backward_decay = 0.125) |
| 1058 | - let t3 = 0.15 * bins[i]; | 1097 | + let mut bm0 = 0.0f32; |
| 1059 | - let mut conseq = 0; | 1098 | + let mut max_e = 0.0f32; |
| 1060 | - for &b in bins.iter().take(i) { | 1099 | + for i in (0..len2).rev() { |
| 1061 | - if b < t1 { | 1100 | + tmp[i] = bm0 + 0.125 * (tmp[i] - bm0); |
| 1062 | - conseq += 1; | 1101 | + bm0 = tmp[i]; |
| 1063 | - } | 1102 | + max_e = max_e.max(bm0); |
| 1064 | - if b < t2 { | 1103 | + } |
| 1065 | - conseq += 1; | 1104 | + |
| 1066 | - } else { | 1105 | + // Geometric mean of frame energy and half the max |
| 1067 | - conseq = 0; | 1106 | + mean = (mean * max_e * 0.5 * len2 as f32).sqrt(); |
| 1068 | - } | 1107 | + |
| 1069 | - } | 1108 | + // Inverse of the mean energy (float mode: SHL32/SHR32 are no-ops) |
| 1070 | - if conseq >= 3 { | 1109 | + let norm = len2 as f32 / (1e-15 + mean); |
| 1071 | - is_transient = true; | 1110 | + |
| 1072 | - } | 1111 | + // Harmonic mean discarding unreliable boundaries (1/4th of samples) |
| 1073 | - conseq = 0; | 1112 | + let mut unmask: i32 = 0; |
| 1074 | - for &b in bins.iter().take(nbins).skip(i + 1) { | 1113 | + let mut i = 12; |
| 1075 | - if b < t3 { | 1114 | + while i < len2 - 5 { |
| 1076 | - conseq += 1; | 1115 | + let id = ((64.0 * norm * (tmp[i] + 1e-15)).floor() as i32).clamp(0, 127) as usize; |
| 1077 | - } else { | 1116 | + unmask += INV_TABLE[id] as i32; |
| 1078 | - conseq = 0; | 1117 | + i += 4; |
| 1079 | - } | 1118 | + } |
| 1080 | - } | 1119 | + // Normalize: 1/4th sampling, factor of 6 in inv_table |
| 1081 | - if conseq >= 7 { | 1120 | + unmask = 64 * unmask * 4 / (6 * (len2 as i32 - 17)); |
| 1082 | - is_transient = true; | 1121 | + if unmask > mask_metric { |
| 1083 | - } | 1122 | + tf_chan = ch; |
| 1084 | - } | 1123 | + mask_metric = unmask; |
| 1085 | - is_transient | 1124 | + } |
| 1125 | + } | ||
| 1126 | + | ||
| 1127 | + let is_transient = mask_metric > 200; | ||
| 1128 | + // tf_max and tf_estimate (float mode: all Q-shifts are no-ops) | ||
| 1129 | + let tf_max = ((27.0 * mask_metric as f32).sqrt() - 42.0).max(0.0); | ||
| 1130 | + let tf_estimate = (0.0069 * tf_max.min(163.0) - 0.139).max(0.0).sqrt(); | ||
| 1131 | + | ||
| 1132 | + (is_transient, tf_estimate, tf_chan) | ||
| 1086 | } | 1133 | } |
| 1087 | 1134 | ||
| 1088 | fn compute_mdcts(&mut self, short_blocks: usize, lm: usize, n: usize, c: usize) { | 1135 | fn compute_mdcts(&mut self, short_blocks: usize, lm: usize, n: usize, c: usize) { |
| @@ -1298,6 +1345,123 @@ impl CeltEncoder { | |||
| 1298 | } | 1345 | } |
| 1299 | } | 1346 | } |
| 1300 | 1347 | ||
| 1348 | + /// `tf_analysis()` — libopus Viterbi time/frequency search. | ||
| 1349 | + /// Writes `self.tf_res[0..len]` and returns `tf_select`. | ||
| 1350 | + #[allow(clippy::too_many_arguments)] | ||
| 1351 | + fn tf_analysis( | ||
| 1352 | + &mut self, | ||
| 1353 | + len: usize, | ||
| 1354 | + is_transient: bool, | ||
| 1355 | + lambda: i32, | ||
| 1356 | + n: usize, | ||
| 1357 | + lm: usize, | ||
| 1358 | + tf_estimate: f32, | ||
| 1359 | + tf_chan: usize, | ||
| 1360 | + importance: &[i32; NB_BANDS], | ||
| 1361 | + ) -> usize { | ||
| 1362 | + // bias = 0.04 * max(-0.25, 0.5 - tf_estimate) | ||
| 1363 | + let bias = 0.04 * (0.5 - tf_estimate).max(-0.25); | ||
| 1364 | + let t = usize::from(is_transient); | ||
| 1365 | + | ||
| 1366 | + let mut metric = [0i32; NB_BANDS]; | ||
| 1367 | + let mut path0 = [0i32; NB_BANDS]; | ||
| 1368 | + let mut path1 = [0i32; NB_BANDS]; | ||
| 1369 | + let mut tf_res = [0i32; NB_BANDS]; | ||
| 1370 | + | ||
| 1371 | + // Max band width for tmp/tmp_1 scratch | ||
| 1372 | + let max_band_w = ((E_BANDS[len] - E_BANDS[len - 1]) << lm).max(1); | ||
| 1373 | + let (tmp_s, tmp_1_s) = self.hadamard_tmp.split_at_mut(max_band_w); | ||
| 1374 | + let x = &self.x; | ||
| 1375 | + | ||
| 1376 | + for i in 0..len { | ||
| 1377 | + let band_w = (E_BANDS[i + 1] - E_BANDS[i]) << lm; | ||
| 1378 | + let narrow = E_BANDS[i + 1] - E_BANDS[i] == 1; | ||
| 1379 | + let tmp = &mut tmp_s[..band_w]; | ||
| 1380 | + let tmp_1 = &mut tmp_1_s[..band_w]; | ||
| 1381 | + | ||
| 1382 | + // Copy band coefficients: X[tf_chan*N0 + (eBands[i]<<LM)] | ||
| 1383 | + let offset = tf_chan * n + (E_BANDS[i] << lm); | ||
| 1384 | + tmp.copy_from_slice(&x[offset..offset + band_w]); | ||
| 1385 | + | ||
| 1386 | + let mut best_level = 0i32; | ||
| 1387 | + let mut best_l1 = l1_metric(tmp, band_w, if is_transient { lm } else { 0 }, bias); | ||
| 1388 | + | ||
| 1389 | + // Check the -1 case for transients | ||
| 1390 | + if is_transient && !narrow { | ||
| 1391 | + tmp_1.copy_from_slice(tmp); | ||
| 1392 | + haar1(tmp_1, band_w >> lm, 1 << lm); | ||
| 1393 | + let l1 = l1_metric(tmp_1, band_w, lm + 1, bias); | ||
| 1394 | + if l1 < best_l1 { | ||
| 1395 | + best_l1 = l1; | ||
| 1396 | + best_level = -1; | ||
| 1397 | + } | ||
| 1398 | + } | ||
| 1399 | + | ||
| 1400 | + for k in 0..(lm + usize::from(!(is_transient || narrow))) { | ||
| 1401 | + let b = if is_transient { lm - k - 1 } else { k + 1 }; | ||
| 1402 | + haar1(tmp, band_w >> k, 1 << k); | ||
| 1403 | + let l1 = l1_metric(tmp, band_w, b, bias); | ||
| 1404 | + if l1 < best_l1 { | ||
| 1405 | + best_l1 = l1; | ||
| 1406 | + best_level = (k as i32) + 1; | ||
| 1407 | + } | ||
| 1408 | + } | ||
| 1409 | + | ||
| 1410 | + metric[i] = if is_transient { 2 * best_level } else { -2 * best_level }; | ||
| 1411 | + if narrow && (metric[i] == 0 || metric[i] == -2 * lm as i32) { | ||
| 1412 | + metric[i] -= 1; | ||
| 1413 | + } | ||
| 1414 | + } | ||
| 1415 | + | ||
| 1416 | + // Search for optimal tf resolution (tf_select) | ||
| 1417 | + let mut tf_select = 0usize; | ||
| 1418 | + let mut selcost = [0i32; 2]; | ||
| 1419 | + for sel in 0..2 { | ||
| 1420 | + let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * sel]).abs(); | ||
| 1421 | + let mut cost1 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * sel + 1]).abs() | ||
| 1422 | + + if is_transient { 0 } else { lambda }; | ||
| 1423 | + for i in 1..len { | ||
| 1424 | + let curr0 = cost0.min(cost1 + lambda); | ||
| 1425 | + let curr1 = (cost0 + lambda).min(cost1); | ||
| 1426 | + cost0 = curr0 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * sel]).abs(); | ||
| 1427 | + cost1 = curr1 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * sel + 1]).abs(); | ||
| 1428 | + } | ||
| 1429 | + selcost[sel] = cost0.min(cost1); | ||
| 1430 | + } | ||
| 1431 | + if selcost[1] < selcost[0] && is_transient { | ||
| 1432 | + tf_select = 1; | ||
| 1433 | + } | ||
| 1434 | + | ||
| 1435 | + // Final Viterbi forward pass | ||
| 1436 | + let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs(); | ||
| 1437 | + let mut cost1 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select + 1]).abs() | ||
| 1438 | + + if is_transient { 0 } else { lambda }; | ||
| 1439 | + for i in 1..len { | ||
| 1440 | + // curr0: best path to state 0 | ||
| 1441 | + let from0 = cost0; | ||
| 1442 | + let from1 = cost1 + lambda; | ||
| 1443 | + let (curr0, p0) = if from0 < from1 { (from0, 0) } else { (from1, 1) }; | ||
| 1444 | + path0[i] = p0; | ||
| 1445 | + // curr1: best path to state 1 (uses OLD cost0) | ||
| 1446 | + let from0 = cost0 + lambda; | ||
| 1447 | + let from1 = cost1; | ||
| 1448 | + let (curr1, p1) = if from0 < from1 { (from0, 0) } else { (from1, 1) }; | ||
| 1449 | + path1[i] = p1; | ||
| 1450 | + | ||
| 1451 | + cost0 = curr0 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs(); | ||
| 1452 | + cost1 = curr1 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select + 1]).abs(); | ||
| 1453 | + } | ||
| 1454 | + | ||
| 1455 | + // Backward pass | ||
| 1456 | + tf_res[len - 1] = if cost0 < cost1 { 0 } else { 1 }; | ||
| 1457 | + for i in (0..len - 1).rev() { | ||
| 1458 | + tf_res[i] = if tf_res[i + 1] == 1 { path1[i + 1] } else { path0[i + 1] }; | ||
| 1459 | + } | ||
| 1460 | + | ||
| 1461 | + self.tf_res = tf_res; | ||
| 1462 | + tf_select | ||
| 1463 | + } | ||
| 1464 | + | ||
| 1301 | // -- Band shapes --------------------------------------------------------- | 1465 | // -- Band shapes --------------------------------------------------------- |
| 1302 | 1466 | ||
| 1303 | #[allow(clippy::too_many_arguments)] | 1467 | #[allow(clippy::too_many_arguments)] |
lanes/opus-64-r4.report.mdadded+95 -0
| @@ -0,0 +1,95 @@ | |||
| 1 | +# lane-opus-64 r4 — tf_analysis port pasted, gated OFF | ||
| 2 | + | ||
| 3 | +## What was pasted | ||
| 4 | + | ||
| 5 | +All six edits from `lanes/opus-64-r3.handoff.md` applied to | ||
| 6 | +`crates/ec-opus/src/celt_enc.rs`, in order: | ||
| 7 | + | ||
| 8 | +1. **`INV_TABLE` constant** (128-entry `u8` table) — after `CACHE_BITS`. | ||
| 9 | +2. **`l1_metric` free fn** — `L1 * (1 + LM*bias)`, float-mode identity macros. | ||
| 10 | +3. **`transient_analysis` replaced** — libopus float-mode algorithm returning | ||
| 11 | + `(is_transient, tf_estimate, tf_chan)` (high-pass → forward/backward decay → | ||
| 12 | + `INV_TABLE` harmonic-mean unmask → `mask_metric > 200`; `tf_estimate` from | ||
| 13 | + `tf_max`). Replaces the old bin-consecutive peak-decay detector that | ||
| 14 | + returned `bool`. **This replacement is unconditional** (not behind the flag). | ||
| 15 | +4. **`tf_analysis` method added** — libopus Viterbi tf search: per-band Haar | ||
| 16 | + `l1_metric` level sweep → `metric[]`, `tf_select` cost search, forward | ||
| 17 | + Viterbi with `path0/path1`, backward trace. Writes `self.tf_res`, returns | ||
| 18 | + `tf_select`. Borrows `self.hadamard_tmp` via `split_at_mut`, `self.x` by ref. | ||
| 19 | +5. **Transient call site** — unpacks the tuple into `is_transient`/ | ||
| 20 | + `tf_estimate`/`tf_chan`. | ||
| 21 | +6. **tf_res wiring** — `const TF_ANALYSIS: bool` gates the Viterbi call: | ||
| 22 | + `effective_bytes >= 15*c` → `tf_analysis(...)`; else the no-analysis default | ||
| 23 | + `self.tf_res = [i32::from(is_transient); NB_BANDS]` + `tf_select = 0`. | ||
| 24 | + | ||
| 25 | +Module doc comment (`//!`) updated to describe the gated port. | ||
| 26 | + | ||
| 27 | +One handoff correction: the handoff's Edit 6 `else` branch used | ||
| 28 | +`self.tf_res = [0; NB_BANDS]`; main since changed the default to | ||
| 29 | +`[i32::from(is_transient); NB_BANDS]`, so the port keeps that (per the task's | ||
| 30 | +"when off, keep the current default"). Also fixed a loop-step typo during | ||
| 31 | +pasting (handoff `i += 4` was the intended fixed 1/4-sample step). | ||
| 32 | + | ||
| 33 | +## Gate: 2 rows (reelm, hein @64k), `SWEEP_ONLY=reelm,hein` | ||
| 34 | + | ||
| 35 | +Baselines from `lanes/opus-reell-r2.sweep.txt` (old transient_analysis, no | ||
| 36 | +tf_analysis): | ||
| 37 | + | ||
| 38 | +| row | baseline gap | baseline minsec | | ||
| 39 | +|-----|-------------|-----------------| | ||
| 40 | +| reelm@64 | +0.0043 | 0.9151 | | ||
| 41 | +| hein@64 | +0.0020 | 0.9209 | | ||
| 42 | + | ||
| 43 | +### TF_ANALYSIS = true (ON) | ||
| 44 | + | ||
| 45 | +| row | gap | minsec | Δgap vs base | Δminsec vs base | | ||
| 46 | +|-----|-----|--------|--------------|-----------------| | ||
| 47 | +| reelm@64 | +0.0046 | 0.9085 | +0.0003 (grew) | −0.0066 (dropped) | | ||
| 48 | +| hein@64 | +0.0030 | 0.9103 | +0.0010 (grew) | −0.0106 (dropped) | | ||
| 49 | + | ||
| 50 | +### TF_ANALYSIS = false (OFF) — committed state | ||
| 51 | + | ||
| 52 | +| row | gap | minsec | Δgap vs base | Δminsec vs base | | ||
| 53 | +|-----|-----|--------|--------------|-----------------| | ||
| 54 | +| reelm@64 | +0.0046 | 0.9085 | +0.0003 (grew) | −0.0066 (dropped) | | ||
| 55 | +| hein@64 | +0.0031 | 0.9113 | +0.0011 (grew) | −0.0096 (dropped) | | ||
| 56 | + | ||
| 57 | +## Decision: OFF | ||
| 58 | + | ||
| 59 | +Gate rule: keep ON only if **both** gaps shrink **and** minsec does not drop. | ||
| 60 | + | ||
| 61 | +Both conditions fail on both rows, for ON **and** OFF. ON ≈ OFF (tf_analysis | ||
| 62 | +itself is neutral on these two rows — the Viterbi search barely moves the | ||
| 63 | +needle once the new transient detector is in place). The divergence from the | ||
| 64 | +old baseline comes from the **Edit 3 `transient_analysis` replacement, which is | ||
| 65 | +ungated**: the libopus float-mode detector (`mask_metric > 200`) flags | ||
| 66 | +transients differently from the old bin-consecutive detector, changing | ||
| 67 | +`short_blocks` and therefore the whole frame encoding. | ||
| 68 | + | ||
| 69 | +So `const TF_ANALYSIS: bool = false`. The tf_analysis Viterbi search is present | ||
| 70 | +in source but never executed; `tf_res` uses the no-analysis default. | ||
| 71 | + | ||
| 72 | +### Note for review | ||
| 73 | +OFF ≠ old baseline because `transient_analysis` (Edit 3) is replaced | ||
| 74 | +unconditionally. The 2 gate rows regress vs `opus-reell-r2` under the new | ||
| 75 | +transient detector alone. If a follow-up wants OFF to exactly reproduce the | ||
| 76 | +pre-port baseline, Edit 3 must also be gated (keep the old `bool` detector | ||
| 77 | +behind the flag). Not done here because the task scoped the flag to the tf_res | ||
| 78 | +wiring (Edit 6) and instructed applying handoff edits 1..N as-is. The full | ||
| 79 | +14-row sweep (step 4) was skipped — it is only required when ON. | ||
| 80 | + | ||
| 81 | +## Verification | ||
| 82 | + | ||
| 83 | +- `cargo check -p ec-opus` — clean (one pre-existing `missing_docs` warning in | ||
| 84 | + `celt.rs:1113`, unrelated). | ||
| 85 | +- `cargo test -p ec-opus --release` — 34 + 27 passed, 0 failed (8 ignored). | ||
| 86 | +- Gate `SWEEP_ONLY=reelm,hein cargo test -p ec-opus --release --test | ||
| 87 | + conformance encoder_library_gate_vs_libopus -- --ignored --nocapture` — | ||
| 88 | + RATE GATE passed, DROPOUT GATE passed, both for ON and OFF runs. | ||
| 89 | + | ||
| 90 | +## Files | ||
| 91 | + | ||
| 92 | +- `crates/ec-opus/src/celt_enc.rs` — INV_TABLE, l1_metric, transient_analysis | ||
| 93 | + (replaced), tf_analysis (added), call-site + tf_res wiring, module doc. | ||
| 94 | +- `lanes/opus-64-r4.sweep.txt` — the OFF 2-row gate sweep (reelm/hein @64/96k). | ||
| 95 | +- `lanes/opus-64-r4.report.md` — this file. | ||
lanes/opus-64-r4.sweep.txtadded+7 -0
| @@ -0,0 +1,7 @@ | |||
| 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 | +reelm 64 62.4 63.3 -1.4 0.9814 0.9859 +0.0046 -329.99 -392.46 0.798 0.9085 0.9100 0 0 | ||
| 5 | +reelm 96 83.7 84.8 -1.3 0.9917 0.9920 +0.0003 -157.29 -231.16 0.702 0.9623 0.9511 0 0 | ||
| 6 | +hein 64 64.2 64.9 -1.0 0.9843 0.9874 +0.0031 -287.08 -388.31 0.685 0.9113 0.9148 0 0 | ||
| 7 | +hein 96 85.9 86.9 -1.1 0.9931 0.9930 -0.0000 -142.58 -246.46 0.607 0.9632 0.9499 0 0 | ||