Intra DC edge extension without allocation
Browse files at this commitbea828cf85f6264becd8806fd3eeff8704f5d195Parent: 1d06737- perf
- intra DC edge extension without allocation
- pre-CDEF planes cloned only when loop restoration runs
2 changed files +47 -21
- crates/ec-av1/src/decode.rs +28 -4
- crates/ec-av1/src/intra.rs +19 -17
crates/ec-av1/src/decode.rsmodified+28 -4
| @@ -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 own | 18833 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 18824 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter | 18834 | // 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); |
| 18848 | + if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { | ||
| 18838 | apply_loop_restoration( | 18849 | apply_loop_restoration( |
| 18839 | - &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, | 18850 | + &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, |
| 18840 | (frame_width as usize, frame_height as usize), | 18851 | (frame_width as usize, frame_height as usize), |
| 18841 | ); | 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 own | 30802 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 30781 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter | 30803 | // 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); |
| 30817 | + if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { | ||
| 30795 | apply_loop_restoration( | 30818 | apply_loop_restoration( |
| 30796 | - &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, | 30819 | + &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, |
| 30797 | (frame_width as usize, frame_height as usize), | 30820 | (frame_width as usize, frame_height as usize), |
| 30798 | ); | 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 its | 623 | /// 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, the | 649 | // `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 i32 | 653 | ((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 comments | 659 | // every 1:2 ratio, `d == 5` for every 1:4 one; libaom comments |
| 658 | // this exact derivation in `intrapred.c` rather than tabulating | 660 | // 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 i32 | 665 | (((num >> shift1) * multiplier) >> 16) as i32 |