U7: PKnobs as a const table indexed by Preset; the EC_H265_* env overrides go, knobs that do not vary by preset become consts (res_proxy always on, its branch removed); no output change at any preset
Browse files at this commite08b4af0cbced709852733d86cc766df70044de4Parent: 02aa5081 changed file +86 -82
- crates/ec-h265/src/enc_p.rs +86 -82
crates/ec-h265/src/enc_p.rsmodified+86 -82
| @@ -71,8 +71,22 @@ const EARLY_SKIP_COST: f64 = 4.0; | ||
| 71 | 71 | /// Mean absolute difference per sample below which a motion-searched block is not split. |
| 72 | 72 | const EARLY_SPLIT_SAD: f64 = 0.25; |
| 73 | 73 | |
| 74 | -/// Search thresholds of the P-picture decision. The defaults are the measured | |
| 75 | -/// values; the `EC_H265_*` overrides exist for the sweep in the lane report. | |
| 74 | +/// Intra modes costed in full, the best by SAD of their prediction. | |
| 75 | +const INTRA_K: usize = 1; | |
| 76 | +/// Residual energy bound below which a transform block is taken to quantise to zero. | |
| 77 | +const ZERO_ALPHA: f64 = 24.0; | |
| 78 | +/// First pattern step (quarter samples) of the search for 16x16 blocks. | |
| 79 | +const STEP16: i16 = 16; | |
| 80 | +/// First pattern step (quarter samples) of the search for 8x8 blocks. | |
| 81 | +const STEP8: i16 = 16; | |
| 82 | +/// First pattern step (quarter samples) of the search for split partitions. | |
| 83 | +const STEP_PART: i16 = 16; | |
| 84 | +/// Whole-sample scan stride. | |
| 85 | +const FULL_STRIDE: i16 = 4; | |
| 86 | +/// Sub-sample refinement passes. | |
| 87 | +const SUB_PASSES: u32 = 1; | |
| 88 | + | |
| 89 | +/// Search thresholds of the P-picture decision, per speed preset. | |
| 76 | 90 | #[derive(Clone, Copy)] |
| 77 | 91 | pub(crate) struct PKnobs { |
| 78 | 92 | /// Whole-sample scan half-width for blocks of 32 and up. |
| @@ -87,80 +101,74 @@ pub(crate) struct PKnobs { | ||
| 87 | 101 | full_gate: f64, |
| 88 | 102 | /// Cost per sample below which the best inter candidate ends the intra search. |
| 89 | 103 | intra_gate: f64, |
| 90 | - /// Intra luma modes costed in full, the best by SAD of their prediction. | |
| 91 | - intra_k: usize, | |
| 92 | - /// A transform block whose residual energy times the transform gain is below this many | |
| 93 | - /// squares of the smallest non-zero coefficient is taken to quantise to zero without | |
| 94 | - /// transforming it (1 is the bound that is exact; larger trades a little accuracy). | |
| 95 | - zero_alpha: f64, | |
| 96 | - /// First pattern step (quarter samples) of the search for 16x16 and 8x8 blocks, and halves. | |
| 97 | - step16: i16, | |
| 98 | - step8: i16, | |
| 99 | - step_part: i16, | |
| 100 | - full_stride: i16, | |
| 101 | - res_proxy: bool, | |
| 102 | - sub_passes: u32, | |
| 103 | 104 | /// Most distinct merge candidates costed in full. |
| 104 | 105 | merge_max: usize, |
| 105 | 106 | /// Cost both AMVP predictor flags (otherwise only the one with the smaller vector difference). |
| 106 | 107 | amvp_both: bool, |
| 107 | 108 | /// log2 of the smallest inter coding unit the analysis searches (3 = 8x8). |
| 108 | 109 | min_log2: u32, |
| 109 | - /// Smallest square (samples) whose horizontal and vertical halves are searched and costed. | |
| 110 | - halves_min: usize, | |
| 111 | 110 | /// Most intra luma modes considered (MPMs first) before the SAD ranking. |
| 112 | 111 | intra_modes: usize, |
| 113 | 112 | /// Cost per sample below which the sub-sample refinement is skipped. |
| 114 | 113 | sub_gate: f64, |
| 115 | 114 | /// Skip the quarter-sample refinement step. |
| 116 | 115 | no_quarter: bool, |
| 116 | + /// Smallest square (samples) whose horizontal and vertical halves are searched and costed. | |
| 117 | + halves_min: usize, | |
| 117 | 118 | } |
| 118 | 119 | |
| 120 | +const PKNOBS: [PKnobs; 3] = [ | |
| 121 | + PKnobs { | |
| 122 | + full_large: FULL_RANGE_LARGE, | |
| 123 | + full_small: FULL_RANGE_SMALL, | |
| 124 | + split_sad: EARLY_SPLIT_SAD, | |
| 125 | + skip_cost: EARLY_SKIP_COST, | |
| 126 | + full_gate: 4.0, | |
| 127 | + intra_gate: 0.0, | |
| 128 | + merge_max: 5, | |
| 129 | + amvp_both: true, | |
| 130 | + min_log2: 3, | |
| 131 | + intra_modes: 7, | |
| 132 | + sub_gate: 0.0, | |
| 133 | + no_quarter: false, | |
| 134 | + halves_min: 16, | |
| 135 | + }, | |
| 136 | + PKnobs { | |
| 137 | + full_large: 8, | |
| 138 | + full_small: 4, | |
| 139 | + split_sad: 1.0, | |
| 140 | + skip_cost: 16.0, | |
| 141 | + full_gate: 12.0, | |
| 142 | + intra_gate: 6.0, | |
| 143 | + merge_max: 3, | |
| 144 | + amvp_both: false, | |
| 145 | + min_log2: 3, | |
| 146 | + intra_modes: 3, | |
| 147 | + sub_gate: 1.0, | |
| 148 | + no_quarter: false, | |
| 149 | + halves_min: 32, | |
| 150 | + }, | |
| 151 | + PKnobs { | |
| 152 | + full_large: 4, | |
| 153 | + full_small: 0, | |
| 154 | + split_sad: 2.0, | |
| 155 | + skip_cost: 32.0, | |
| 156 | + full_gate: 16.0, | |
| 157 | + intra_gate: 12.0, | |
| 158 | + merge_max: 2, | |
| 159 | + amvp_both: false, | |
| 160 | + min_log2: 4, | |
| 161 | + intra_modes: 2, | |
| 162 | + sub_gate: 2.0, | |
| 163 | + no_quarter: true, | |
| 164 | + halves_min: 1024, | |
| 165 | + }, | |
| 166 | +]; | |
| 167 | + | |
| 119 | 168 | impl PKnobs { |
| 120 | - /// The knobs of a speed preset. `Default` is the measured set; `EC_H265_*` | |
| 121 | - /// environment variables override any of them (sweeps only). | |
| 169 | + /// The knobs of a speed preset. | |
| 122 | 170 | pub(crate) fn for_preset(p: Preset) -> PKnobs { |
| 123 | - let f = |name: &str, d: f64| { | |
| 124 | - std::env::var(name) | |
| 125 | - .ok() | |
| 126 | - .and_then(|v| v.parse::<f64>().ok()) | |
| 127 | - .unwrap_or(d) | |
| 128 | - }; | |
| 129 | - // (fast, faster) values against the default. | |
| 130 | - let pick = |d: f64, fast: f64, faster: f64| match p { | |
| 131 | - Preset::Default => d, | |
| 132 | - Preset::Fast => fast, | |
| 133 | - Preset::Faster => faster, | |
| 134 | - }; | |
| 135 | - PKnobs { | |
| 136 | - full_large: f( | |
| 137 | - "EC_H265_FULL_LARGE", | |
| 138 | - pick(f64::from(FULL_RANGE_LARGE), 8.0, 4.0), | |
| 139 | - ) as i16, | |
| 140 | - full_small: f( | |
| 141 | - "EC_H265_FULL_SMALL", | |
| 142 | - pick(f64::from(FULL_RANGE_SMALL), 4.0, 0.0), | |
| 143 | - ) as i16, | |
| 144 | - split_sad: f("EC_H265_SPLIT_SAD", pick(EARLY_SPLIT_SAD, 1.0, 2.0)), | |
| 145 | - skip_cost: f("EC_H265_SKIP_COST", pick(EARLY_SKIP_COST, 16.0, 32.0)), | |
| 146 | - full_gate: f("EC_H265_FULL_GATE", pick(4.0, 12.0, 16.0)), | |
| 147 | - intra_gate: f("EC_H265_INTRA_GATE", pick(0.0, 6.0, 12.0)), | |
| 148 | - intra_k: f("EC_H265_INTRA_K", 1.0) as usize, | |
| 149 | - zero_alpha: f("EC_H265_ZERO_ALPHA", pick(24.0, 24.0, 24.0)), | |
| 150 | - step16: f("EC_H265_STEP16", 16.0) as i16, | |
| 151 | - step8: f("EC_H265_STEP8", 16.0) as i16, | |
| 152 | - step_part: f("EC_H265_STEPP", 16.0) as i16, | |
| 153 | - full_stride: f("EC_H265_FULL_STRIDE", 4.0) as i16, | |
| 154 | - sub_passes: f("EC_H265_SUB_PASSES", pick(1.0, 1.0, 1.0)) as u32, | |
| 155 | - res_proxy: f("EC_H265_RES_PROXY", 1.0) != 0.0, | |
| 156 | - merge_max: f("EC_H265_MERGE_MAX", pick(5.0, 3.0, 2.0)) as usize, | |
| 157 | - amvp_both: f("EC_H265_AMVP_BOTH", pick(1.0, 0.0, 0.0)) != 0.0, | |
| 158 | - min_log2: f("EC_H265_MIN_LOG2", pick(3.0, 3.0, 4.0)) as u32, | |
| 159 | - intra_modes: f("EC_H265_INTRA_MODES", pick(7.0, 3.0, 2.0)) as usize, | |
| 160 | - sub_gate: f("EC_H265_SUB_GATE", pick(0.0, 1.0, 2.0)), | |
| 161 | - no_quarter: f("EC_H265_NO_QUARTER", pick(0.0, 0.0, 1.0)) != 0.0, | |
| 162 | - halves_min: f("EC_H265_HALVES_MIN", pick(16.0, 32.0, 1024.0)) as usize, | |
| 163 | - } | |
| 171 | + PKNOBS[p as usize] | |
| 164 | 172 | } |
| 165 | 173 | } |
| 166 | 174 | |
| @@ -728,14 +736,14 @@ impl<'a> PCoder<'a> { | ||
| 728 | 736 | self.k.full_small |
| 729 | 737 | }, |
| 730 | 738 | full_gate: self.k.full_gate, |
| 731 | - full_stride: self.k.full_stride, | |
| 732 | - sub_passes: self.k.sub_passes, | |
| 739 | + full_stride: FULL_STRIDE, | |
| 740 | + sub_passes: SUB_PASSES, | |
| 733 | 741 | sub_gate: self.k.sub_gate, |
| 734 | 742 | no_quarter: self.k.no_quarter, |
| 735 | 743 | first_step: match n { |
| 736 | 744 | 32.. => 16, |
| 737 | - 16.. => self.k.step16, | |
| 738 | - _ => self.k.step8, | |
| 745 | + 16.. => STEP16, | |
| 746 | + _ => STEP8, | |
| 739 | 747 | }, |
| 740 | 748 | }; |
| 741 | 749 | let best = search.run((x, y), (n, n), mvp, &seeds); |
| @@ -800,10 +808,10 @@ impl<'a> PCoder<'a> { | ||
| 800 | 808 | full_range: 0, |
| 801 | 809 | full_gate: 0.0, |
| 802 | 810 | full_stride: 1, |
| 803 | - sub_passes: self.k.sub_passes, | |
| 811 | + sub_passes: SUB_PASSES, | |
| 804 | 812 | sub_gate: self.k.sub_gate, |
| 805 | 813 | no_quarter: self.k.no_quarter, |
| 806 | - first_step: self.k.step_part, | |
| 814 | + first_step: STEP_PART, | |
| 807 | 815 | }; |
| 808 | 816 | let best = search.run((rx, ry), (rw, rh), mvp, &seeds); |
| 809 | 817 | total += best.cost; |
| @@ -1002,7 +1010,7 @@ impl<'a> PCoder<'a> { | ||
| 1002 | 1010 | let inter_best = best.as_ref().map_or(f64::MAX, |(c, _, _)| *c); |
| 1003 | 1011 | if inter_best < self.k.intra_gate * (n * n) as f64 { |
| 1004 | 1012 | modes.clear(); |
| 1005 | - } else if self.k.intra_k < modes.len() { | |
| 1013 | + } else if INTRA_K < modes.len() { | |
| 1006 | 1014 | let mut ranked: Vec<(u64, u8)> = modes |
| 1007 | 1015 | .iter() |
| 1008 | 1016 | .map(|&m| { |
| @@ -1011,11 +1019,7 @@ impl<'a> PCoder<'a> { | ||
| 1011 | 1019 | }) |
| 1012 | 1020 | .collect(); |
| 1013 | 1021 | ranked.sort_unstable(); |
| 1014 | - modes = ranked | |
| 1015 | - .into_iter() | |
| 1016 | - .take(self.k.intra_k) | |
| 1017 | - .map(|(_, m)| m) | |
| 1018 | - .collect(); | |
| 1022 | + modes = ranked.into_iter().take(INTRA_K).map(|(_, m)| m).collect(); | |
| 1019 | 1023 | } |
| 1020 | 1024 | let no_motion = PbMotion::new([false, false], [-1, -1], [[0, 0], [0, 0]]); |
| 1021 | 1025 | for mode in modes { |
| @@ -1175,7 +1179,7 @@ impl<'a> PCoder<'a> { | ||
| 1175 | 1179 | self.bd, |
| 1176 | 1180 | sh, |
| 1177 | 1181 | (0, false), |
| 1178 | - self.k.zero_alpha, | |
| 1182 | + ZERO_ALPHA, | |
| 1179 | 1183 | ); |
| 1180 | 1184 | let cb = code_tb( |
| 1181 | 1185 | &src.cb, |
| @@ -1188,7 +1192,7 @@ impl<'a> PCoder<'a> { | ||
| 1188 | 1192 | self.bd, |
| 1189 | 1193 | sh, |
| 1190 | 1194 | (0, false), |
| 1191 | - self.k.zero_alpha, | |
| 1195 | + ZERO_ALPHA, | |
| 1192 | 1196 | ); |
| 1193 | 1197 | let cr = code_tb( |
| 1194 | 1198 | &src.cr, |
| @@ -1201,7 +1205,7 @@ impl<'a> PCoder<'a> { | ||
| 1201 | 1205 | self.bd, |
| 1202 | 1206 | sh, |
| 1203 | 1207 | (0, false), |
| 1204 | - self.k.zero_alpha, | |
| 1208 | + ZERO_ALPHA, | |
| 1205 | 1209 | ); |
| 1206 | 1210 | ssd += luma.ssd + cb.ssd + cr.ssd; |
| 1207 | 1211 | for r in 0..half { |
| @@ -1271,7 +1275,7 @@ impl<'a> PCoder<'a> { | ||
| 1271 | 1275 | self.bd, |
| 1272 | 1276 | sh, |
| 1273 | 1277 | (0, false), |
| 1274 | - self.k.zero_alpha, | |
| 1278 | + ZERO_ALPHA, | |
| 1275 | 1279 | ); |
| 1276 | 1280 | let cb = code_tb( |
| 1277 | 1281 | &src.cb, |
| @@ -1284,7 +1288,7 @@ impl<'a> PCoder<'a> { | ||
| 1284 | 1288 | self.bd, |
| 1285 | 1289 | sh, |
| 1286 | 1290 | (0, false), |
| 1287 | - self.k.zero_alpha, | |
| 1291 | + ZERO_ALPHA, | |
| 1288 | 1292 | ); |
| 1289 | 1293 | let cr = code_tb( |
| 1290 | 1294 | &src.cr, |
| @@ -1297,7 +1301,7 @@ impl<'a> PCoder<'a> { | ||
| 1297 | 1301 | self.bd, |
| 1298 | 1302 | sh, |
| 1299 | 1303 | (0, false), |
| 1300 | - self.k.zero_alpha, | |
| 1304 | + ZERO_ALPHA, | |
| 1301 | 1305 | ); |
| 1302 | 1306 | let cbf = [luma.cbf, cb.cbf, cr.cbf]; |
| 1303 | 1307 | let any = cbf.iter().any(|&c| c); |
| @@ -1423,7 +1427,7 @@ impl<'a> PCoder<'a> { | ||
| 1423 | 1427 | self.bd, |
| 1424 | 1428 | sh, |
| 1425 | 1429 | (scan, n == 4), |
| 1426 | - self.k.zero_alpha, | |
| 1430 | + ZERO_ALPHA, | |
| 1427 | 1431 | ); |
| 1428 | 1432 | let cb = code_tb( |
| 1429 | 1433 | &src.cb, |
| @@ -1436,7 +1440,7 @@ impl<'a> PCoder<'a> { | ||
| 1436 | 1440 | self.bd, |
| 1437 | 1441 | sh, |
| 1438 | 1442 | (cscan, false), |
| 1439 | - self.k.zero_alpha, | |
| 1443 | + ZERO_ALPHA, | |
| 1440 | 1444 | ); |
| 1441 | 1445 | let cr = code_tb( |
| 1442 | 1446 | &src.cr, |
| @@ -1449,7 +1453,7 @@ impl<'a> PCoder<'a> { | ||
| 1449 | 1453 | self.bd, |
| 1450 | 1454 | sh, |
| 1451 | 1455 | (cscan, false), |
| 1452 | - self.k.zero_alpha, | |
| 1456 | + ZERO_ALPHA, | |
| 1453 | 1457 | ); |
| 1454 | 1458 | let syntax = CuSyntax { |
| 1455 | 1459 | skip: false, |
| @@ -1582,7 +1586,7 @@ impl<'a> PCoder<'a> { | ||
| 1582 | 1586 | c_idx: usize, |
| 1583 | 1587 | scan: usize, |
| 1584 | 1588 | ) { |
| 1585 | - if e.is_estimating() && self.k.res_proxy { | |
| 1589 | + if e.is_estimating() { | |
| 1586 | 1590 | e.add_estimated_bits(estimate_residual_bits(levels, log2, scan)); |
| 1587 | 1591 | } else { |
| 1588 | 1592 | encode_residual(e, levels, log2, c_idx, scan, self.sign_hiding, None); |