forge
Explore
Log in

Don't you dare go Hollow

Band the loop-restoration unit search by unit row

Browse files at this commit

63f3f62f31e8a64b039fb765de534e55f89e211fAhmet Kaan GümüşParent: 996b587
  • perf
    • band the loop-restoration unit search by unit row
Show full message
perf(av1): band the loop-restoration unit search by unit row

`pick_restoration` priced every unit of every Wiener candidate on the frame
thread while the whole pool sat idle. A unit's cost is read off its own
samples and written to its own slot, so the walk is `chunks_mut` bands of
the same `best` -- the choice cannot depend on how many bands there are.

4 frames at 3840x1608, 4x2 tiles, 8 threads: the LR search goes 31.0 ->
13.4 ms/frame; 1080p 8.4 -> 4.5 ms/frame.
crates/ec-av1/src/filter_search.rsmodified+32 -15
@@ -500,13 +500,33 @@ pub(crate) fn pick_restoration(
500 sse500 sse
501 };501 };
502 // Cost of leaving every unit alone, then one whole-plane pass per502 // Cost of leaving every unit alone, then one whole-plane pass per
503- // candidate to price taking it.503+ // candidate to price taking it. Both walks are banded by unit ROW
504- let mut best: Vec<(f64, Option<crate::restoration::WienerInfo>)> = (0..horz * vert)504+ // (lane-av1fpar): a unit's cost is read off its own samples alone and
505- .map(|i| {505+ // written to its own slot, so the bands are `chunks_mut` of the same
506- let sse = unit_sse(&stages.cdefed[0], i / horz, i % horz) as f64;506+ // `best` and the choice cannot depend on how many of them there are.
507- (sse + lambda * LR_NONE_BITS, None)507+ let rows_of = |plane: &[u16], r0: usize, slice: &mut [(f64, Option<crate::restoration::WienerInfo>)], score: &dyn Fn(u64, &mut (f64, Option<crate::restoration::WienerInfo>))| {
508- })508+ for (i, slot) in slice.iter_mut().enumerate() {
509- .collect();509+ score(unit_sse(plane, r0 + i / horz, i % horz), slot);
510+ }
511+ };
512+ let banded = |plane: &[u16], best: &mut [(f64, Option<crate::restoration::WienerInfo>)], score: &(dyn Fn(u64, &mut (f64, Option<crate::restoration::WienerInfo>)) + Sync)| {
513+ let threads = crate::par::filter_threads().min(vert);
514+ if threads <= 1 {
515+ rows_of(plane, 0, best, score);
516+ return;
517+ }
518+ let per = vert.div_ceil(threads);
519+ let batch = crate::par::Batch::new("ec-av1-filter");
520+ for (band, slice) in best.chunks_mut(per * horz).enumerate() {
521+ let rows_of = &rows_of;
522+ batch.submit(move || rows_of(plane, band * per, slice, score));
523+ }
524+ };
525+ let mut best: Vec<(f64, Option<crate::restoration::WienerInfo>)> =
526+ vec![(0.0, None); horz * vert];
527+ banded(&stages.cdefed[0], &mut best, &|sse, slot| {
528+ *slot = (sse as f64 + lambda * LR_NONE_BITS, None);
529+ });
510 let mut out = Vec::new();530 let mut out = Vec::new();
511 for taps in WIENER_CANDIDATES {531 for taps in WIENER_CANDIDATES {
512 let info = crate::restoration::wiener_from_taps(taps, taps);532 let info = crate::restoration::wiener_from_taps(taps, taps);
@@ -531,15 +551,12 @@ pub(crate) fn pick_restoration(
531 None,551 None,
532 fctx,552 fctx,
533 );553 );
534- for rrow in 0..vert {554+ banded(&out, &mut best, &|sse, slot| {
535- for rcol in 0..horz {555+ let cost = sse as f64 + lambda * LR_WIENER_BITS;
536- let cost = unit_sse(&out, rrow, rcol) as f64 + lambda * LR_WIENER_BITS;556+ if cost < slot.0 {
537- let slot = &mut best[rcol + rrow * horz];557+ *slot = (cost, Some(info));
538- if cost < slot.0 {
539- *slot = (cost, Some(info));
540- }
541 }558 }
542- }559+ });
543 }560 }
544 let chosen: Vec<Option<crate::restoration::WienerInfo>> =561 let chosen: Vec<Option<crate::restoration::WienerInfo>> =
545 best.into_iter().map(|(_, f)| f).collect();562 best.into_iter().map(|(_, f)| f).collect();