forge
Explore
Log in

Don't you dare go Hollow

Score the filter search's two sides once each, per unit

Browse files at this commit

2b23fd69f0c904debe92d5c589cbf6e7c3a189c1Ahmet Kaan GümüşParent: 826851c
  • perf
    • score the filter search's two sides once each, per unit
Show full message
perf(av1): score the filter search's two sides once each, per unit

The filter search's per-64x64 error pass (`unit_sse`) was the encoder's
second-largest self-time symbol (10.6%), and the frame-level `plane_sse`
that ran beside it (1.7%) summed the very same terms a second time.

Two exact changes, no decision moved (byte-identical streams on the
three gate clips):

* `unit_sse` returns the frame total as well, so `plane_sse` is gone,
  and its inner loop walks unit-sized chunks with a `u32` accumulator
  instead of dividing `col / unit` per sample and accumulating straight
  into `out` -- a shape LLVM turns into a reduction.
* Each candidate's luma and chroma error is memoized under the
  parameters that side actually reads: `apply_deblock` filters luma on
  `level[0..2]` and chroma on `level[2..4]`, and CDEF's strengths are
  per plane type, so the ~11 luma-deblock candidates all share one
  chroma score and the 9 chroma-CDEF candidates all share one luma
  score. The luma deblock level is in the CHROMA key because CDEF's
  direction search runs on the post-deblock luma window and chroma
  reuses that direction -- except when chroma CDEF is off, which is the
  case that makes the deblock stages share.

1080p / 2160p / screen ladder wall 5.34/4.91/7.06 -> 4.64/4.21/4.91 s.
crates/ec-av1/src/filter_search.rsmodified+86 -45
@@ -55,24 +55,22 @@ pub(crate) fn take_cdef_presets() -> Vec<(u8, Vec<usize>)> {
55 }55 }
56 56
57 /// Squared error between a decoded plane and the source it was coded from,57 /// Squared error between a decoded plane and the source it was coded from,
58-/// over the frame's own cropped `w x h` (the two planes have different58+/// accumulated BOTH per CDEF unit and over the frame's own cropped `w x h`
59-/// strides: the source is the encoder's padded coding surface).59+/// in one pass: `out` is one entry per 64x64 SUPERBLOCK (stride `sb_cols`),
60-fn plane_sse(dec: &[u16], dec_w: usize, src: &[u8], src_w: usize, w: usize, h: usize) -> u64 {60+/// `unit` the unit side in this plane's own samples (64 for luma, 32 for
61- let mut sse = 0u64;61+/// 4:2:0 chroma) -- which is what makes the luma and chroma sums land in the
62- for row in 0..h {62+/// same slot -- and the return value the frame-level sum of the same terms.
63- let (d, s) = (&dec[row * dec_w..][..w], &src[row * src_w..][..w]);63+/// The two planes have different strides: the source is the encoder's padded
64- for (&a, &b) in d.iter().zip(s) {64+/// coding surface.
65- let diff = i64::from(a) - i64::from(b);65+///
66- sse += (diff * diff) as u64;66+/// The unit chunking is what makes this vectorise (lane-av1fsearch: this was
67- }67+/// the encoder's second-largest self-time symbol): the old shape divided
68- }68+/// `col / unit` per sample and accumulated straight into `out`, which LLVM
69- sse69+/// cannot turn into a reduction. Here every unit's row segment is its own
70-}70+/// `u32` reduction -- `unit <= 64` samples of at most `(2^12-1)^2` each, so
71-71+/// the accumulator cannot overflow -- and the `u64` sums are the same
72-/// [`plane_sse`], accumulated per CDEF unit instead of over the frame: `out`72+/// integers added in a different order.
73-/// is one entry per 64x64 SUPERBLOCK (stride `sb_cols`) and `unit` is the73+#[allow(clippy::too_many_arguments)]
74-/// unit side in this plane's own samples (64 for luma, 32 for 4:2:0 chroma),
75-/// which is what makes the luma and chroma sums land in the same slot.
76 fn unit_sse(74 fn unit_sse(
77 dec: &[u16],75 dec: &[u16],
78 dec_w: usize,76 dec_w: usize,
@@ -83,15 +81,22 @@ fn unit_sse(
83 unit: usize,81 unit: usize,
84 sb_cols: usize,82 sb_cols: usize,
85 out: &mut [u64],83 out: &mut [u64],
86-) {84+) -> u64 {
85+ let mut total = 0u64;
87 for row in 0..h {86 for row in 0..h {
88 let base = (row / unit) * sb_cols;87 let base = (row / unit) * sb_cols;
89 let (d, s) = (&dec[row * dec_w..][..w], &src[row * src_w..][..w]);88 let (d, s) = (&dec[row * dec_w..][..w], &src[row * src_w..][..w]);
90- for (col, (&a, &b)) in d.iter().zip(s).enumerate() {89+ for (i, (dc, sc)) in d.chunks(unit).zip(s.chunks(unit)).enumerate() {
91- let diff = i64::from(a) - i64::from(b);90+ let mut acc = 0u32;
92- out[base + col / unit] += (diff * diff) as u64;91+ for (&a, &b) in dc.iter().zip(sc) {
92+ let diff = i32::from(a) - i32::from(b);
93+ acc += (diff * diff) as u32;
94+ }
95+ out[base + i] += u64::from(acc);
96+ total += u64::from(acc);
93 }97 }
94 }98 }
99+ total
95 }100 }
96 101
97 /// CDEF primary strengths tried (libaom's own range is 0..15 for 8-bit102 /// CDEF primary strengths tried (libaom's own range is 0..15 for 8-bit
@@ -159,22 +164,57 @@ pub(crate) fn pick_filters(
159 ) -> Result<(LoopFilterParams, CdefParams, Vec<u8>, Picture)> {164 ) -> Result<(LoopFilterParams, CdefParams, Vec<u8>, Picture)> {
160 let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));165 let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));
161 let nsb = sb_cols * sb_rows;166 let nsb = sb_cols * sb_rows;
162- let mut trials: Vec<Trial> = Vec::new();167+ // The two sides of a candidate are scored independently: `apply_deblock`
163- let eval = |cand: Cand, trials: &mut Vec<Trial>| -> Result<usize> {168+ // filters luma on `level[0..2]` alone and each chroma plane on
164- if let Some(i) = trials.iter().position(|t| t.cand == cand) {169+ // `level[2..4]` alone, and CDEF's strengths are per plane type too (a
170+ // zero strength pair is the identity filter), so a candidate that moves
171+ // only the chroma slot leaves every luma sample -- and every luma error
172+ // term -- exactly as the previous candidate had it. Each side's error is
173+ // therefore memoized under the parameters that side actually reads,
174+ // which is what takes ~32 candidates' worth of summing down to ~17 luma
175+ // and ~12 chroma passes.
176+ struct Search {
177+ trials: Vec<Trial>,
178+ /// `(luma deblock level, luma CDEF strengths) -> (frame sse, per-unit sse)`.
179+ luma: Vec<((u8, (u8, u8)), (u64, Vec<u64>))>,
180+ /// `(luma deblock level, chroma deblock level, chroma CDEF
181+ /// strengths) -> the same`. The luma level is in the key because
182+ /// CDEF's direction search runs on the post-deblock LUMA window and
183+ /// chroma reuses that direction (`decode.rs`: `uv_dir`); it is
184+ /// pinned to 0 for a chroma-CDEF-off candidate, whose chroma planes
185+ /// CDEF never touches at all.
186+ chroma: Vec<((u8, u8, (u8, u8)), (u64, Vec<u64>))>,
187+ }
188+ let mut st = Search { trials: Vec::new(), luma: Vec::new(), chroma: Vec::new() };
189+ let eval = |cand: Cand, st: &mut Search| -> Result<usize> {
190+ if let Some(i) = st.trials.iter().position(|t| t.cand == cand) {
165 return Ok(i);191 return Ok(i);
166 }192 }
167 let picture = decode(&loop_filter(cand), &cdef(cand, damping))?;193 let picture = decode(&loop_filter(cand), &cdef(cand, damping))?;
168 let dec_cw = picture.width.div_ceil(2);194 let dec_cw = picture.width.div_ceil(2);
169- let sse_y = plane_sse(&picture.y, picture.width, source[0], src_w, fw, fh);195+ let (lkey, ckey) = (
170- let sse_uv = plane_sse(&picture.u, dec_cw, source[1], src_w / 2, cw, ch)196+ (cand.lf.0, cand.y),
171- + plane_sse(&picture.v, dec_cw, source[2], src_w / 2, cw, ch);197+ (if cand.uv == (0, 0) { 0 } else { cand.lf.0 }, cand.lf.1, cand.uv),
172- let mut sse64 = vec![0u64; nsb];198+ );
173- unit_sse(&picture.y, picture.width, source[0], src_w, fw, fh, 64, sb_cols, &mut sse64);199+ if !st.luma.iter().any(|(k, _)| *k == lkey) {
174- unit_sse(&picture.u, dec_cw, source[1], src_w / 2, cw, ch, 32, sb_cols, &mut sse64);200+ let mut units = vec![0u64; nsb];
175- unit_sse(&picture.v, dec_cw, source[2], src_w / 2, cw, ch, 32, sb_cols, &mut sse64);201+ let sse =
176- trials.push(Trial { cand, picture, sse_y, sse_uv, sse64 });202+ unit_sse(&picture.y, picture.width, source[0], src_w, fw, fh, 64, sb_cols, &mut units);
177- Ok(trials.len() - 1)203+ st.luma.push((lkey, (sse, units)));
204+ }
205+ if !st.chroma.iter().any(|(k, _)| *k == ckey) {
206+ let mut units = vec![0u64; nsb];
207+ let sse = unit_sse(&picture.u, dec_cw, source[1], src_w / 2, cw, ch, 32, sb_cols, &mut units)
208+ + unit_sse(&picture.v, dec_cw, source[2], src_w / 2, cw, ch, 32, sb_cols, &mut units);
209+ st.chroma.push((ckey, (sse, units)));
210+ }
211+ let (sse_y, sse_uv, sse64) = {
212+ let l = &st.luma.iter().find(|(k, _)| *k == lkey).expect("just inserted").1;
213+ let c = &st.chroma.iter().find(|(k, _)| *k == ckey).expect("just inserted").1;
214+ (l.0, c.0, l.1.iter().zip(&c.1).map(|(a, b)| a + b).collect())
215+ };
216+ st.trials.push(Trial { cand, picture, sse_y, sse_uv, sse64 });
217+ Ok(st.trials.len() - 1)
178 };218 };
179 // One stage of the coordinate search: try `values` in the slot `set`219 // One stage of the coordinate search: try `values` in the slot `set`
180 // moves and keep the one with the lowest error `sse` reads.220 // moves and keep the one with the lowest error `sse` reads.
@@ -182,16 +222,16 @@ pub(crate) fn pick_filters(
182 values: &[u8],222 values: &[u8],
183 set: fn(&mut Cand, u8),223 set: fn(&mut Cand, u8),
184 sse: fn(&Trial) -> u64,224 sse: fn(&Trial) -> u64,
185- trials: &mut Vec<Trial>|225+ st: &mut Search|
186 -> Result<()> {226 -> Result<()> {
187 let mut winner = *best;227 let mut winner = *best;
188 let mut lowest = u64::MAX;228 let mut lowest = u64::MAX;
189 for &v in values {229 for &v in values {
190 let mut cand = *best;230 let mut cand = *best;
191 set(&mut cand, v);231 set(&mut cand, v);
192- let i = eval(cand, trials)?;232+ let i = eval(cand, st)?;
193- if sse(&trials[i]) < lowest {233+ if sse(&st.trials[i]) < lowest {
194- lowest = sse(&trials[i]);234+ lowest = sse(&st.trials[i]);
195 winner = cand;235 winner = cand;
196 }236 }
197 }237 }
@@ -201,23 +241,24 @@ pub(crate) fn pick_filters(
201 241
202 let mut best = Cand { lf: (0, 0), y: (0, 0), uv: (0, 0) };242 let mut best = Cand { lf: (0, 0), y: (0, 0), uv: (0, 0) };
203 // Deblocking luma: the coarse ladder, then +-1/+-2 around its winner.243 // Deblocking luma: the coarse ladder, then +-1/+-2 around its winner.
204- stage(&mut best, &COARSE, |c, v| c.lf = (v, v), |t| t.sse_y, &mut trials)?;244+ stage(&mut best, &COARSE, |c, v| c.lf = (v, v), |t| t.sse_y, &mut st)?;
205 let refine: Vec<u8> = [-2i32, -1, 1, 2]245 let refine: Vec<u8> = [-2i32, -1, 1, 2]
206 .iter()246 .iter()
207 .filter_map(|d| u8::try_from(i32::from(best.lf.0) + d).ok())247 .filter_map(|d| u8::try_from(i32::from(best.lf.0) + d).ok())
208 .filter(|&l| l <= 63)248 .filter(|&l| l <= 63)
209 .collect();249 .collect();
210- stage(&mut best, &refine, |c, v| c.lf = (v, v), |t| t.sse_y, &mut trials)?;250+ stage(&mut best, &refine, |c, v| c.lf = (v, v), |t| t.sse_y, &mut st)?;
211 // Deblocking chroma, three points around the luma winner.251 // Deblocking chroma, three points around the luma winner.
212 let chroma_levels = [0, best.lf.0 / 2, best.lf.0];252 let chroma_levels = [0, best.lf.0 / 2, best.lf.0];
213- stage(&mut best, &chroma_levels, |c, v| c.lf.1 = v, |t| t.sse_uv, &mut trials)?;253+ stage(&mut best, &chroma_levels, |c, v| c.lf.1 = v, |t| t.sse_uv, &mut st)?;
214 // CDEF, primary then secondary, luma on luma error and chroma on chroma.254 // CDEF, primary then secondary, luma on luma error and chroma on chroma.
215- stage(&mut best, &CDEF_PRI, |c, v| c.y.0 = v, |t| t.sse_y, &mut trials)?;255+ stage(&mut best, &CDEF_PRI, |c, v| c.y.0 = v, |t| t.sse_y, &mut st)?;
216- stage(&mut best, &CDEF_SEC, |c, v| c.y.1 = v, |t| t.sse_y, &mut trials)?;256+ stage(&mut best, &CDEF_SEC, |c, v| c.y.1 = v, |t| t.sse_y, &mut st)?;
217- stage(&mut best, &CDEF_PRI, |c, v| c.uv.0 = v, |t| t.sse_uv, &mut trials)?;257+ stage(&mut best, &CDEF_PRI, |c, v| c.uv.0 = v, |t| t.sse_uv, &mut st)?;
218- stage(&mut best, &CDEF_SEC, |c, v| c.uv.1 = v, |t| t.sse_uv, &mut trials)?;258+ stage(&mut best, &CDEF_SEC, |c, v| c.uv.1 = v, |t| t.sse_uv, &mut st)?;
219 259
220 CHOSEN.with(|c| c.borrow_mut().push(best));260 CHOSEN.with(|c| c.borrow_mut().push(best));
261+ let trials = st.trials;
221 262
222 // libaom `av1_cdef_search`'s shape, on the candidates this coordinate263 // libaom `av1_cdef_search`'s shape, on the candidates this coordinate
223 // search already decoded: every trial that ran under the WINNING264 // search already decoded: every trial that ran under the WINNING