forge
Explore
Log in

Don't you dare go Hollow

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 commit

e08b4af0cbced709852733d86cc766df70044de4Ahmet Kaan GümüşParent: 02aa508
crates/ec-h265/src/enc_p.rsmodified+86 -82
@@ -71,8 +71,22 @@ const EARLY_SKIP_COST: f64 = 4.0;
7171 /// Mean absolute difference per sample below which a motion-searched block is not split.
7272 const EARLY_SPLIT_SAD: f64 = 0.25;
7373
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.
7690 #[derive(Clone, Copy)]
7791 pub(crate) struct PKnobs {
7892 /// Whole-sample scan half-width for blocks of 32 and up.
@@ -87,80 +101,74 @@ pub(crate) struct PKnobs {
87101 full_gate: f64,
88102 /// Cost per sample below which the best inter candidate ends the intra search.
89103 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,
103104 /// Most distinct merge candidates costed in full.
104105 merge_max: usize,
105106 /// Cost both AMVP predictor flags (otherwise only the one with the smaller vector difference).
106107 amvp_both: bool,
107108 /// log2 of the smallest inter coding unit the analysis searches (3 = 8x8).
108109 min_log2: u32,
109- /// Smallest square (samples) whose horizontal and vertical halves are searched and costed.
110- halves_min: usize,
111110 /// Most intra luma modes considered (MPMs first) before the SAD ranking.
112111 intra_modes: usize,
113112 /// Cost per sample below which the sub-sample refinement is skipped.
114113 sub_gate: f64,
115114 /// Skip the quarter-sample refinement step.
116115 no_quarter: bool,
116+ /// Smallest square (samples) whose horizontal and vertical halves are searched and costed.
117+ halves_min: usize,
117118 }
118119
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+
119168 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.
122170 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]
164172 }
165173 }
166174
@@ -728,14 +736,14 @@ impl<'a> PCoder<'a> {
728736 self.k.full_small
729737 },
730738 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,
733741 sub_gate: self.k.sub_gate,
734742 no_quarter: self.k.no_quarter,
735743 first_step: match n {
736744 32.. => 16,
737- 16.. => self.k.step16,
738- _ => self.k.step8,
745+ 16.. => STEP16,
746+ _ => STEP8,
739747 },
740748 };
741749 let best = search.run((x, y), (n, n), mvp, &seeds);
@@ -800,10 +808,10 @@ impl<'a> PCoder<'a> {
800808 full_range: 0,
801809 full_gate: 0.0,
802810 full_stride: 1,
803- sub_passes: self.k.sub_passes,
811+ sub_passes: SUB_PASSES,
804812 sub_gate: self.k.sub_gate,
805813 no_quarter: self.k.no_quarter,
806- first_step: self.k.step_part,
814+ first_step: STEP_PART,
807815 };
808816 let best = search.run((rx, ry), (rw, rh), mvp, &seeds);
809817 total += best.cost;
@@ -1002,7 +1010,7 @@ impl<'a> PCoder<'a> {
10021010 let inter_best = best.as_ref().map_or(f64::MAX, |(c, _, _)| *c);
10031011 if inter_best < self.k.intra_gate * (n * n) as f64 {
10041012 modes.clear();
1005- } else if self.k.intra_k < modes.len() {
1013+ } else if INTRA_K < modes.len() {
10061014 let mut ranked: Vec<(u64, u8)> = modes
10071015 .iter()
10081016 .map(|&m| {
@@ -1011,11 +1019,7 @@ impl<'a> PCoder<'a> {
10111019 })
10121020 .collect();
10131021 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();
10191023 }
10201024 let no_motion = PbMotion::new([false, false], [-1, -1], [[0, 0], [0, 0]]);
10211025 for mode in modes {
@@ -1175,7 +1179,7 @@ impl<'a> PCoder<'a> {
11751179 self.bd,
11761180 sh,
11771181 (0, false),
1178- self.k.zero_alpha,
1182+ ZERO_ALPHA,
11791183 );
11801184 let cb = code_tb(
11811185 &src.cb,
@@ -1188,7 +1192,7 @@ impl<'a> PCoder<'a> {
11881192 self.bd,
11891193 sh,
11901194 (0, false),
1191- self.k.zero_alpha,
1195+ ZERO_ALPHA,
11921196 );
11931197 let cr = code_tb(
11941198 &src.cr,
@@ -1201,7 +1205,7 @@ impl<'a> PCoder<'a> {
12011205 self.bd,
12021206 sh,
12031207 (0, false),
1204- self.k.zero_alpha,
1208+ ZERO_ALPHA,
12051209 );
12061210 ssd += luma.ssd + cb.ssd + cr.ssd;
12071211 for r in 0..half {
@@ -1271,7 +1275,7 @@ impl<'a> PCoder<'a> {
12711275 self.bd,
12721276 sh,
12731277 (0, false),
1274- self.k.zero_alpha,
1278+ ZERO_ALPHA,
12751279 );
12761280 let cb = code_tb(
12771281 &src.cb,
@@ -1284,7 +1288,7 @@ impl<'a> PCoder<'a> {
12841288 self.bd,
12851289 sh,
12861290 (0, false),
1287- self.k.zero_alpha,
1291+ ZERO_ALPHA,
12881292 );
12891293 let cr = code_tb(
12901294 &src.cr,
@@ -1297,7 +1301,7 @@ impl<'a> PCoder<'a> {
12971301 self.bd,
12981302 sh,
12991303 (0, false),
1300- self.k.zero_alpha,
1304+ ZERO_ALPHA,
13011305 );
13021306 let cbf = [luma.cbf, cb.cbf, cr.cbf];
13031307 let any = cbf.iter().any(|&c| c);
@@ -1423,7 +1427,7 @@ impl<'a> PCoder<'a> {
14231427 self.bd,
14241428 sh,
14251429 (scan, n == 4),
1426- self.k.zero_alpha,
1430+ ZERO_ALPHA,
14271431 );
14281432 let cb = code_tb(
14291433 &src.cb,
@@ -1436,7 +1440,7 @@ impl<'a> PCoder<'a> {
14361440 self.bd,
14371441 sh,
14381442 (cscan, false),
1439- self.k.zero_alpha,
1443+ ZERO_ALPHA,
14401444 );
14411445 let cr = code_tb(
14421446 &src.cr,
@@ -1449,7 +1453,7 @@ impl<'a> PCoder<'a> {
14491453 self.bd,
14501454 sh,
14511455 (cscan, false),
1452- self.k.zero_alpha,
1456+ ZERO_ALPHA,
14531457 );
14541458 let syntax = CuSyntax {
14551459 skip: false,
@@ -1582,7 +1586,7 @@ impl<'a> PCoder<'a> {
15821586 c_idx: usize,
15831587 scan: usize,
15841588 ) {
1585- if e.is_estimating() && self.k.res_proxy {
1589+ if e.is_estimating() {
15861590 e.add_estimated_bits(estimate_residual_bits(levels, log2, scan));
15871591 } else {
15881592 encode_residual(e, levels, log2, c_idx, scan, self.sign_hiding, None);