forge
Explore
Log in

Don't you dare go Hollow

SAD-prescreen the 13-mode intra search, gated off by default

Browse files at this commit

7f9b971515db1142296bf2d960f65d99cf9a0c6fAhmet Kaan GümüşParent: 2e9837a
  • perf
    • SAD-prescreen the 13-mode intra search, gated off by default
Show full message
perf(av1): SAD-prescreen the 13-mode intra search, gated off by default

search_block now predicts every mode once (unchanged cost) and ranks by
a cheap SAD-plus-mode-bits pre-pass before spending the full RD trial
(forward transform, quantize, reconstruct, coeff_bits) on the winners;
Search::top_k picks how many survive plus DC, always. Default stays
None (all 13 modes, byte-identical output, 129/0/5 green) until the
quality gate sweep in the lane report picks a K to ship as default.
crates/ec-av1/src/encode.rsmodified+98 -6
@@ -742,17 +742,83 @@ impl Plane<'_> {
742742 }
743743 }
744744
745- /// Codes one block under every mode the search offers and commits the one
746- /// whose squared error and estimated rate come out cheapest.
745+ /// Codes one block under every mode the search offers (or, when
746+ /// [`Search::top_k`] prunes it, the cheapest `top_k` of them by a SAD
747+ /// pre-pass plus DC) and commits the one whose squared error and
748+ /// estimated rate come out cheapest.
749+ ///
750+ /// The prediction is built once per mode regardless -- the full RD trial
751+ /// needs it too -- so ranking modes by SAD on that same prediction before
752+ /// running the expensive part (forward transform, quantize, reconstruct,
753+ /// `coeff_bits`) on only the survivors costs nothing extra to predict,
754+ /// only to sum.
747755 fn search_block(
748756 &mut self,
749757 at: At,
750758 search: &Search,
751759 mode_bits: &[f64; 13],
752760 ) -> (Vec<Coeff>, u8, f64) {
761+ let At {
762+ x, y, side, reach, ..
763+ } = at;
764+ let (above, left, corner) = self.edges(x, y, side, reach);
765+ let mut scored: Vec<(f64, u8, Vec<u8>)> = search
766+ .modes
767+ .iter()
768+ .map(|&mode| {
769+ let mut prediction = vec![0u8; side * side];
770+ crate::intra::predict(
771+ mode,
772+ above.as_deref(),
773+ left.as_deref(),
774+ corner,
775+ side,
776+ &mut prediction,
777+ );
778+ let sad: f64 = (0..side * side)
779+ .map(|i| {
780+ let (row, col) = (i / side, i % side);
781+ f64::from(
782+ (i32::from(self.source[(y + row) * self.width + x + col])
783+ - i32::from(prediction[i]))
784+ .abs(),
785+ )
786+ })
787+ .sum();
788+ (
789+ sad + search.lambda * mode_bits[usize::from(mode)],
790+ mode,
791+ prediction,
792+ )
793+ })
794+ .collect();
795+ if let Some(k) = search.top_k {
796+ if k < scored.len() {
797+ let dc_pos = scored.iter().position(|&(_, mode, _)| mode == DC_PRED);
798+ let dc_entry = dc_pos.map(|i| scored.remove(i));
799+ scored.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("scores are finite"));
800+ scored.truncate(if dc_entry.is_some() {
801+ k.saturating_sub(1)
802+ } else {
803+ k
804+ });
805+ if let Some(dc_entry) = dc_entry {
806+ scored.push(dc_entry);
807+ }
808+ }
809+ }
753810 let mut best: Option<(f64, u8, Trial)> = None;
754- for &mode in search.modes {
755- let trial = self.trial(at, mode, search.base_q_idx, search.deadzone);
811+ for (_, mode, prediction) in scored {
812+ let trial = self.code_from_prediction(
813+ x,
814+ y,
815+ side,
816+ &prediction,
817+ false,
818+ search.base_q_idx,
819+ search.deadzone,
820+ at.set,
821+ );
756822 let cost = trial.sse + search.lambda * (trial.bits + mode_bits[usize::from(mode)]);
757823 if best
758824 .as_ref()
@@ -762,8 +828,8 @@ impl Plane<'_> {
762828 }
763829 }
764830 let (cost, mode, trial) = best.expect("the search offers at least one mode");
765- self.commit(at.x, at.y, at.side, &trial);
766- (coeffs(&trial.levels, at.side), mode, cost)
831+ self.commit(x, y, side, &trial);
832+ (coeffs(&trial.levels, side), mode, cost)
767833 }
768834 }
769835
@@ -1011,8 +1077,32 @@ struct Search<'a> {
10111077 deadzone: f64,
10121078 lambda: f64,
10131079 modes: &'a [u8],
1080+ /// How many of `modes` the full RD trial (forward transform, quantize,
1081+ /// reconstruct, `coeff_bits`) actually runs on, ranked by a cheap SAD
1082+ /// pre-pass on the prediction residual alone plus DC (always kept, since
1083+ /// libaom's own intra pruning -- `av1/encoder/speed_features.c`'s
1084+ /// `intra_pruning_with_hog` -- never prunes it away). `None` runs every
1085+ /// mode through full RD, unchanged from before this lever.
1086+ top_k: Option<usize>,
10141087 }
10151088
1089+/// [`Search::top_k`]'s value: `None` keeps every mode's search unchanged
1090+/// (the default, until a lever's quality gate justifies pruning by default);
1091+/// `EC_AV1_PRUNE_K` sweeps it in a test build without touching callers.
1092+fn prune_top_k() -> Option<usize> {
1093+ let swept = std::env::var("EC_AV1_PRUNE_K")
1094+ .ok()
1095+ .and_then(|v| v.parse::<usize>().ok());
1096+ match swept {
1097+ Some(k) if cfg!(test) => Some(k),
1098+ _ => PRUNE_TOP_K,
1099+ }
1100+}
1101+
1102+/// The default: unpruned, thirteen full trials per block, same as before this
1103+/// lever. Set from the sweep in the lane report if the quality gate clears it.
1104+const PRUNE_TOP_K: Option<usize> = None;
1105+
10161106 /// One mode's coding of one block, before it is committed.
10171107 struct Trial {
10181108 levels: Vec<i32>,
@@ -1161,6 +1251,7 @@ pub(crate) fn encode_key_frame_inner(
11611251 deadzone,
11621252 lambda: lambda_scale() * step * step,
11631253 modes,
1254+ top_k: prune_top_k(),
11641255 };
11651256
11661257 // The block grid this frame is coded over: [`crate::tile::block_grid`]'s
@@ -1923,6 +2014,7 @@ pub(crate) fn encode_inter_frame(
19232014 deadzone,
19242015 lambda: lambda_scale() * step * step,
19252016 modes: &KEY_FRAME_MODES,
2017+ top_k: prune_top_k(),
19262018 };
19272019 let mode_bits_table = inter_mode_bits();
19282020