From e08b4af0cbced709852733d86cc766df70044de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:41:48 +0300 Subject: [PATCH] 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 --- crates/ec-h265/src/enc_p.rs | 168 ++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 82 deletions(-) diff --git a/crates/ec-h265/src/enc_p.rs b/crates/ec-h265/src/enc_p.rs index fac5106d..6241ccde 100644 --- a/crates/ec-h265/src/enc_p.rs +++ b/crates/ec-h265/src/enc_p.rs @@ -71,8 +71,22 @@ const EARLY_SKIP_COST: f64 = 4.0; /// Mean absolute difference per sample below which a motion-searched block is not split. const EARLY_SPLIT_SAD: f64 = 0.25; -/// Search thresholds of the P-picture decision. The defaults are the measured -/// values; the `EC_H265_*` overrides exist for the sweep in the lane report. +/// Intra modes costed in full, the best by SAD of their prediction. +const INTRA_K: usize = 1; +/// Residual energy bound below which a transform block is taken to quantise to zero. +const ZERO_ALPHA: f64 = 24.0; +/// First pattern step (quarter samples) of the search for 16x16 blocks. +const STEP16: i16 = 16; +/// First pattern step (quarter samples) of the search for 8x8 blocks. +const STEP8: i16 = 16; +/// First pattern step (quarter samples) of the search for split partitions. +const STEP_PART: i16 = 16; +/// Whole-sample scan stride. +const FULL_STRIDE: i16 = 4; +/// Sub-sample refinement passes. +const SUB_PASSES: u32 = 1; + +/// Search thresholds of the P-picture decision, per speed preset. #[derive(Clone, Copy)] pub(crate) struct PKnobs { /// Whole-sample scan half-width for blocks of 32 and up. @@ -87,80 +101,74 @@ pub(crate) struct PKnobs { full_gate: f64, /// Cost per sample below which the best inter candidate ends the intra search. intra_gate: f64, - /// Intra luma modes costed in full, the best by SAD of their prediction. - intra_k: usize, - /// A transform block whose residual energy times the transform gain is below this many - /// squares of the smallest non-zero coefficient is taken to quantise to zero without - /// transforming it (1 is the bound that is exact; larger trades a little accuracy). - zero_alpha: f64, - /// First pattern step (quarter samples) of the search for 16x16 and 8x8 blocks, and halves. - step16: i16, - step8: i16, - step_part: i16, - full_stride: i16, - res_proxy: bool, - sub_passes: u32, /// Most distinct merge candidates costed in full. merge_max: usize, /// Cost both AMVP predictor flags (otherwise only the one with the smaller vector difference). amvp_both: bool, /// log2 of the smallest inter coding unit the analysis searches (3 = 8x8). min_log2: u32, - /// Smallest square (samples) whose horizontal and vertical halves are searched and costed. - halves_min: usize, /// Most intra luma modes considered (MPMs first) before the SAD ranking. intra_modes: usize, /// Cost per sample below which the sub-sample refinement is skipped. sub_gate: f64, /// Skip the quarter-sample refinement step. no_quarter: bool, + /// Smallest square (samples) whose horizontal and vertical halves are searched and costed. + halves_min: usize, } +const PKNOBS: [PKnobs; 3] = [ + PKnobs { + full_large: FULL_RANGE_LARGE, + full_small: FULL_RANGE_SMALL, + split_sad: EARLY_SPLIT_SAD, + skip_cost: EARLY_SKIP_COST, + full_gate: 4.0, + intra_gate: 0.0, + merge_max: 5, + amvp_both: true, + min_log2: 3, + intra_modes: 7, + sub_gate: 0.0, + no_quarter: false, + halves_min: 16, + }, + PKnobs { + full_large: 8, + full_small: 4, + split_sad: 1.0, + skip_cost: 16.0, + full_gate: 12.0, + intra_gate: 6.0, + merge_max: 3, + amvp_both: false, + min_log2: 3, + intra_modes: 3, + sub_gate: 1.0, + no_quarter: false, + halves_min: 32, + }, + PKnobs { + full_large: 4, + full_small: 0, + split_sad: 2.0, + skip_cost: 32.0, + full_gate: 16.0, + intra_gate: 12.0, + merge_max: 2, + amvp_both: false, + min_log2: 4, + intra_modes: 2, + sub_gate: 2.0, + no_quarter: true, + halves_min: 1024, + }, +]; + impl PKnobs { - /// The knobs of a speed preset. `Default` is the measured set; `EC_H265_*` - /// environment variables override any of them (sweeps only). + /// The knobs of a speed preset. pub(crate) fn for_preset(p: Preset) -> PKnobs { - let f = |name: &str, d: f64| { - std::env::var(name) - .ok() - .and_then(|v| v.parse::().ok()) - .unwrap_or(d) - }; - // (fast, faster) values against the default. - let pick = |d: f64, fast: f64, faster: f64| match p { - Preset::Default => d, - Preset::Fast => fast, - Preset::Faster => faster, - }; - PKnobs { - full_large: f( - "EC_H265_FULL_LARGE", - pick(f64::from(FULL_RANGE_LARGE), 8.0, 4.0), - ) as i16, - full_small: f( - "EC_H265_FULL_SMALL", - pick(f64::from(FULL_RANGE_SMALL), 4.0, 0.0), - ) as i16, - split_sad: f("EC_H265_SPLIT_SAD", pick(EARLY_SPLIT_SAD, 1.0, 2.0)), - skip_cost: f("EC_H265_SKIP_COST", pick(EARLY_SKIP_COST, 16.0, 32.0)), - full_gate: f("EC_H265_FULL_GATE", pick(4.0, 12.0, 16.0)), - intra_gate: f("EC_H265_INTRA_GATE", pick(0.0, 6.0, 12.0)), - intra_k: f("EC_H265_INTRA_K", 1.0) as usize, - zero_alpha: f("EC_H265_ZERO_ALPHA", pick(24.0, 24.0, 24.0)), - step16: f("EC_H265_STEP16", 16.0) as i16, - step8: f("EC_H265_STEP8", 16.0) as i16, - step_part: f("EC_H265_STEPP", 16.0) as i16, - full_stride: f("EC_H265_FULL_STRIDE", 4.0) as i16, - sub_passes: f("EC_H265_SUB_PASSES", pick(1.0, 1.0, 1.0)) as u32, - res_proxy: f("EC_H265_RES_PROXY", 1.0) != 0.0, - merge_max: f("EC_H265_MERGE_MAX", pick(5.0, 3.0, 2.0)) as usize, - amvp_both: f("EC_H265_AMVP_BOTH", pick(1.0, 0.0, 0.0)) != 0.0, - min_log2: f("EC_H265_MIN_LOG2", pick(3.0, 3.0, 4.0)) as u32, - intra_modes: f("EC_H265_INTRA_MODES", pick(7.0, 3.0, 2.0)) as usize, - sub_gate: f("EC_H265_SUB_GATE", pick(0.0, 1.0, 2.0)), - no_quarter: f("EC_H265_NO_QUARTER", pick(0.0, 0.0, 1.0)) != 0.0, - halves_min: f("EC_H265_HALVES_MIN", pick(16.0, 32.0, 1024.0)) as usize, - } + PKNOBS[p as usize] } } @@ -728,14 +736,14 @@ impl<'a> PCoder<'a> { self.k.full_small }, full_gate: self.k.full_gate, - full_stride: self.k.full_stride, - sub_passes: self.k.sub_passes, + full_stride: FULL_STRIDE, + sub_passes: SUB_PASSES, sub_gate: self.k.sub_gate, no_quarter: self.k.no_quarter, first_step: match n { 32.. => 16, - 16.. => self.k.step16, - _ => self.k.step8, + 16.. => STEP16, + _ => STEP8, }, }; let best = search.run((x, y), (n, n), mvp, &seeds); @@ -800,10 +808,10 @@ impl<'a> PCoder<'a> { full_range: 0, full_gate: 0.0, full_stride: 1, - sub_passes: self.k.sub_passes, + sub_passes: SUB_PASSES, sub_gate: self.k.sub_gate, no_quarter: self.k.no_quarter, - first_step: self.k.step_part, + first_step: STEP_PART, }; let best = search.run((rx, ry), (rw, rh), mvp, &seeds); total += best.cost; @@ -1002,7 +1010,7 @@ impl<'a> PCoder<'a> { let inter_best = best.as_ref().map_or(f64::MAX, |(c, _, _)| *c); if inter_best < self.k.intra_gate * (n * n) as f64 { modes.clear(); - } else if self.k.intra_k < modes.len() { + } else if INTRA_K < modes.len() { let mut ranked: Vec<(u64, u8)> = modes .iter() .map(|&m| { @@ -1011,11 +1019,7 @@ impl<'a> PCoder<'a> { }) .collect(); ranked.sort_unstable(); - modes = ranked - .into_iter() - .take(self.k.intra_k) - .map(|(_, m)| m) - .collect(); + modes = ranked.into_iter().take(INTRA_K).map(|(_, m)| m).collect(); } let no_motion = PbMotion::new([false, false], [-1, -1], [[0, 0], [0, 0]]); for mode in modes { @@ -1175,7 +1179,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (0, false), - self.k.zero_alpha, + ZERO_ALPHA, ); let cb = code_tb( &src.cb, @@ -1188,7 +1192,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (0, false), - self.k.zero_alpha, + ZERO_ALPHA, ); let cr = code_tb( &src.cr, @@ -1201,7 +1205,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (0, false), - self.k.zero_alpha, + ZERO_ALPHA, ); ssd += luma.ssd + cb.ssd + cr.ssd; for r in 0..half { @@ -1271,7 +1275,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (0, false), - self.k.zero_alpha, + ZERO_ALPHA, ); let cb = code_tb( &src.cb, @@ -1284,7 +1288,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (0, false), - self.k.zero_alpha, + ZERO_ALPHA, ); let cr = code_tb( &src.cr, @@ -1297,7 +1301,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (0, false), - self.k.zero_alpha, + ZERO_ALPHA, ); let cbf = [luma.cbf, cb.cbf, cr.cbf]; let any = cbf.iter().any(|&c| c); @@ -1423,7 +1427,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (scan, n == 4), - self.k.zero_alpha, + ZERO_ALPHA, ); let cb = code_tb( &src.cb, @@ -1436,7 +1440,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (cscan, false), - self.k.zero_alpha, + ZERO_ALPHA, ); let cr = code_tb( &src.cr, @@ -1449,7 +1453,7 @@ impl<'a> PCoder<'a> { self.bd, sh, (cscan, false), - self.k.zero_alpha, + ZERO_ALPHA, ); let syntax = CuSyntax { skip: false, @@ -1582,7 +1586,7 @@ impl<'a> PCoder<'a> { c_idx: usize, scan: usize, ) { - if e.is_estimating() && self.k.res_proxy { + if e.is_estimating() { e.add_estimated_bits(estimate_residual_bits(levels, log2, scan)); } else { encode_residual(e, levels, log2, c_idx, scan, self.sign_hiding, None);