forge
Explore
Log in

Don't you dare go Hollow

Intra DC edge extension without allocation

Browse files at this commit

bea828cf85f6264becd8806fd3eeff8704f5d195Ahmet Kaan GümüşParent: 1d06737
  • perf
    • intra DC edge extension without allocation
    • pre-CDEF planes cloned only when loop restoration runs
Show full message
perf(av1): intra DC edge extension without allocation; pre-CDEF planes cloned only when loop restoration runs

Two allocation sites the 1080p profile put at ~1.3% of self time between
them, neither of which any pixel depends on:

* `intra::dc` built one `Vec<u16>` per existing edge per intra block only
  to sum it. The replication libaom's `build_intra_predictors` performs is
  never indexed here, so the extended edge becomes a sum:
  `sum(samples[..n]) + last * (want - n)`, the same integers in the same
  order of magnitude, and the square/rect divisors come from `bw`/`bh`
  directly rather than from the (equal) vector lengths.
* Both frame paths cloned all three planes before CDEF as the loop
  restoration source, and `apply_loop_restoration_plane` then copied each
  plane again to return it unchanged when its restoration type is `None`.
  The clone is now taken only when some plane's type is not `None` with a
  non-zero unit size, i.e. exactly when a restored sample can differ.

Byte-exact: EC_PROBE_OUT16 sha256 unchanged on a 1080p and a 4K segment
and on `palette_screen_witness.obu`. Interleaved medians (3 pairs 1080p,
one pair 4K): 11.82 s -> 11.65 s at 1080p, 64.91 s -> 60.86 s at 4K.
crates/ec-av1/src/decode.rsmodified+34 -10
@@ -18819,7 +18819,17 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1881918819 frame_width as usize,
1882018820 frame_height as usize,
1882118821 );
18822- let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone());
18822+ // lane-perf4: loop restoration is the only reader of the pre-CDEF planes,
18823+ // and a frame that restores nothing copied every plane twice for nothing
18824+ // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside
18825+ // [`crate::restoration::apply_loop_restoration_plane`], which returns its
18826+ // input unchanged when the plane's type is `None`). Clone only when some
18827+ // plane actually restores.
18828+ let lr_active = (0..3).any(|p| {
18829+ lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
18830+ && lr.loop_restoration_size[p] != 0
18831+ });
18832+ let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone()));
1882318833 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
1882418834 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
1882518835 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -18835,10 +18845,12 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1883518845 );
1883618846 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
1883718847 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
18838- apply_loop_restoration(
18839- &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid,
18840- (frame_width as usize, frame_height as usize),
18841- );
18848+ if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked {
18849+ apply_loop_restoration(
18850+ &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid,
18851+ (frame_width as usize, frame_height as usize),
18852+ );
18853+ }
1884218854
1884318855 let (fw, fh) = (frame_width as usize, frame_height as usize);
1884418856 if fw == width && fh == height {
@@ -30776,7 +30788,17 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3077630788 frame_width as usize,
3077730789 frame_height as usize,
3077830790 );
30779- let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone());
30791+ // lane-perf4: loop restoration is the only reader of the pre-CDEF planes,
30792+ // and a frame that restores nothing copied every plane twice for nothing
30793+ // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside
30794+ // [`crate::restoration::apply_loop_restoration_plane`], which returns its
30795+ // input unchanged when the plane's type is `None`). Clone only when some
30796+ // plane actually restores.
30797+ let lr_active = (0..3).any(|p| {
30798+ lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
30799+ && lr.loop_restoration_size[p] != 0
30800+ });
30801+ let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone()));
3078030802 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
3078130803 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
3078230804 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -30792,10 +30814,12 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3079230814 );
3079330815 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
3079430816 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
30795- apply_loop_restoration(
30796- &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid,
30797- (frame_width as usize, frame_height as usize),
30798- );
30817+ if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked {
30818+ apply_loop_restoration(
30819+ &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid,
30820+ (frame_width as usize, frame_height as usize),
30821+ );
30822+ }
3079930823
3080030824 let motion_field = build_motion_field(
3080130825 &grid,
crates/ec-av1/src/intra.rsmodified+19 -17
@@ -623,31 +623,33 @@ fn directional(
623623 /// frame edge must be extended by repetition here too, not averaged over its
624624 /// own shorter length.
625625 fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 {
626- let extend = |samples: &[u16], want: usize| -> Vec<u16> {
627- if samples.len() >= want {
628- samples[..want].to_vec()
626+ // lane-perf4: the replication is only ever *summed*, never indexed, so
627+ // the extended edge is a sum rather than the `Vec<u16>` it used to be
628+ // built as (two allocations per intra block, 0.9% of a 1080p decode's
629+ // self time between them). Same integers, same order of magnitude, no
630+ // rounding difference: `sum(samples[..n]) + last * (want - n)`.
631+ let extend = |samples: &[u16], want: usize| -> u32 {
632+ let n = samples.len().min(want);
633+ let sum: u32 = samples[..n].iter().map(|&s| u32::from(s)).sum();
634+ if n < want {
635+ let last = u32::from(*samples.last().expect("an edge that exists has samples"));
636+ sum + last * (want - n) as u32
629637 } else {
630- let last = *samples.last().expect("an edge that exists has samples");
631- let mut v = samples.to_vec();
632- v.resize(want, last);
633- v
638+ sum
634639 }
635640 };
636641 let above = above.map(|a| extend(a, bw));
637642 let left = left.map(|l| extend(l, bh));
638- let average = |samples: &[u16]| {
639- let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum();
640- ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32
641- };
642- match (&above, &left) {
643+ let average = |sum: u32, count: u32| ((sum + (count >> 1)) / count) as i32;
644+ match (above, left) {
643645 (None, None) => 1i32 << (crate::decode::bit_depth() - 1),
644- (Some(a), None) => average(a),
645- (None, Some(l)) => average(l),
646+ (Some(a), None) => average(a, bw as u32),
647+ (None, Some(l)) => average(l, bh as u32),
646648 (Some(a), Some(l)) if bw == bh => {
647649 // `dc_predictor` (libaom `intrapred.c`): exact division, the
648650 // square path this whole lane must leave bit-identical.
649- let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum();
650- let count = (a.len() + l.len()) as u32;
651+ let sum: u32 = a + l;
652+ let count = (bw + bh) as u32;
651653 ((sum + (count >> 1)) / count) as i32
652654 }
653655 (Some(a), Some(l)) => {
@@ -657,7 +659,7 @@ fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32
657659 // every 1:2 ratio, `d == 5` for every 1:4 one; libaom comments
658660 // this exact derivation in `intrapred.c` rather than tabulating
659661 // it, so this ports the derivation, not a lookup).
660- let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum();
662+ let sum: u32 = a + l;
661663 let (shift1, multiplier) = dc_rect_multiplier(bw, bh);
662664 let num = u64::from(sum + ((bw + bh) as u32 >> 1));
663665 (((num >> shift1) * multiplier) >> 16) as i32