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(
18819 frame_width as usize,18819 frame_width as usize,
18820 frame_height as usize,18820 frame_height as usize,
18821 );18821 );
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()));
18823 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own18833 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
18824 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter18834 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
18825 // dump above these bisect WHICH filter stage introduced a mismatch.18835 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -18835,10 +18845,12 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
18835 );18845 );
18836 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);18846 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
18837 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);18847 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
18838- apply_loop_restoration(18848+ if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked {
18839- &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid,18849+ apply_loop_restoration(
18840- (frame_width as usize, frame_height as usize),18850+ &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid,
18841- );18851+ (frame_width as usize, frame_height as usize),
18852+ );
18853+ }
18842 18854
18843 let (fw, fh) = (frame_width as usize, frame_height as usize);18855 let (fw, fh) = (frame_width as usize, frame_height as usize);
18844 if fw == width && fh == height {18856 if fw == width && fh == height {
@@ -30776,7 +30788,17 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
30776 frame_width as usize,30788 frame_width as usize,
30777 frame_height as usize,30789 frame_height as usize,
30778 );30790 );
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()));
30780 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own30802 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
30781 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter30803 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
30782 // dump above these bisect WHICH filter stage introduced a mismatch.30804 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -30792,10 +30814,12 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
30792 );30814 );
30793 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);30815 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
30794 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);30816 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
30795- apply_loop_restoration(30817+ if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked {
30796- &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid,30818+ apply_loop_restoration(
30797- (frame_width as usize, frame_height as usize),30819+ &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid,
30798- );30820+ (frame_width as usize, frame_height as usize),
30821+ );
30822+ }
30799 30823
30800 let motion_field = build_motion_field(30824 let motion_field = build_motion_field(
30801 &grid,30825 &grid,
crates/ec-av1/src/intra.rsmodified+19 -17
@@ -623,31 +623,33 @@ fn directional(
623 /// frame edge must be extended by repetition here too, not averaged over its623 /// frame edge must be extended by repetition here too, not averaged over its
624 /// own shorter length.624 /// own shorter length.
625 fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 {625 fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 {
626- let extend = |samples: &[u16], want: usize| -> Vec<u16> {626+ // lane-perf4: the replication is only ever *summed*, never indexed, so
627- if samples.len() >= want {627+ // the extended edge is a sum rather than the `Vec<u16>` it used to be
628- samples[..want].to_vec()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
629 } else {637 } else {
630- let last = *samples.last().expect("an edge that exists has samples");638+ sum
631- let mut v = samples.to_vec();
632- v.resize(want, last);
633- v
634 }639 }
635 };640 };
636 let above = above.map(|a| extend(a, bw));641 let above = above.map(|a| extend(a, bw));
637 let left = left.map(|l| extend(l, bh));642 let left = left.map(|l| extend(l, bh));
638- let average = |samples: &[u16]| {643+ let average = |sum: u32, count: u32| ((sum + (count >> 1)) / count) as i32;
639- let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum();644+ match (above, left) {
640- ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32
641- };
642- match (&above, &left) {
643 (None, None) => 1i32 << (crate::decode::bit_depth() - 1),645 (None, None) => 1i32 << (crate::decode::bit_depth() - 1),
644- (Some(a), None) => average(a),646+ (Some(a), None) => average(a, bw as u32),
645- (None, Some(l)) => average(l),647+ (None, Some(l)) => average(l, bh as u32),
646 (Some(a), Some(l)) if bw == bh => {648 (Some(a), Some(l)) if bw == bh => {
647 // `dc_predictor` (libaom `intrapred.c`): exact division, the649 // `dc_predictor` (libaom `intrapred.c`): exact division, the
648 // square path this whole lane must leave bit-identical.650 // square path this whole lane must leave bit-identical.
649- let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum();651+ let sum: u32 = a + l;
650- let count = (a.len() + l.len()) as u32;652+ let count = (bw + bh) as u32;
651 ((sum + (count >> 1)) / count) as i32653 ((sum + (count >> 1)) / count) as i32
652 }654 }
653 (Some(a), Some(l)) => {655 (Some(a), Some(l)) => {
@@ -657,7 +659,7 @@ fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32
657 // every 1:2 ratio, `d == 5` for every 1:4 one; libaom comments659 // every 1:2 ratio, `d == 5` for every 1:4 one; libaom comments
658 // this exact derivation in `intrapred.c` rather than tabulating660 // this exact derivation in `intrapred.c` rather than tabulating
659 // it, so this ports the derivation, not a lookup).661 // 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;
661 let (shift1, multiplier) = dc_rect_multiplier(bw, bh);663 let (shift1, multiplier) = dc_rect_multiplier(bw, bh);
662 let num = u64::from(sum + ((bw + bh) as u32 >> 1));664 let num = u64::from(sum + ((bw + bh) as u32 >> 1));
663 (((num >> shift1) * multiplier) >> 16) as i32665 (((num >> shift1) * multiplier) >> 16) as i32