forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-perf4' (486d8fd8): loop-restoration source planes cloned only when LR is active, intra DC sums the edge without materialising it, film-grain chroma and MC reconstruction on row slices; byte-exact, 4K 64.9 s to 58.4 s (conflict with r29's superres LR tail resolved: the optional deblocked planes are upscaled when present)

Browse files at this commit

ea4deedd37f606548b92000e969ffe1989c998c2Ahmet Kaan GümüşParent: cdcd10c180e079
crates/ec-av1/src/decode.rsmodified+54 -24
@@ -18853,7 +18853,17 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1885318853 frame_width as usize,
1885418854 frame_height as usize,
1885518855 );
18856- let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone());
18856+ // lane-perf4: loop restoration is the only reader of the pre-CDEF planes,
18857+ // and a frame that restores nothing copied every plane twice for nothing
18858+ // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside
18859+ // [`crate::restoration::apply_loop_restoration_plane`], which returns its
18860+ // input unchanged when the plane's type is `None`). Clone only when some
18861+ // plane actually restores.
18862+ let lr_active = (0..3).any(|p| {
18863+ lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
18864+ && lr.loop_restoration_size[p] != 0
18865+ });
18866+ let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone()));
1885718867 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
1885818868 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
1885918869 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -18877,8 +18887,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1887718887 // boundaries are upscaled (`save_deblock_boundary_lines` runs
1887818888 // `av1_upscale_normative_rows` over its saved rows when the frame is
1887918889 // scaled), so every LR coordinate from here on is in upscaled space.
18880- let (mut deblocked_y, mut deblocked_u, mut deblocked_v) =
18881- (deblocked_y, deblocked_u, deblocked_v);
18890+ let mut deblocked = deblocked;
1888218891 let mut lr_w = frame_width as usize;
1888318892 if let Some((upscaled_w, _)) = superres() {
1888418893 let bd = u32::from(bit_depth());
@@ -18900,16 +18909,18 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1890018909 y = up(&y, fw, fh, uw);
1890118910 u = up(&u, cw, ch, cuw);
1890218911 v = up(&v, cw, ch, cuw);
18903- deblocked_y = up(&deblocked_y, fw, fh, uw);
18904- deblocked_u = up(&deblocked_u, cw, ch, cuw);
18905- deblocked_v = up(&deblocked_v, cw, ch, cuw);
18912+ if let Some((dy, du, dv)) = deblocked.take() {
18913+ deblocked = Some((up(&dy, fw, fh, uw), up(&du, cw, ch, cuw), up(&dv, cw, ch, cuw)));
18914+ }
1890618915 lr_w = uw;
1890718916 crate::superres::note_upscaled_picture();
1890818917 }
18909- apply_loop_restoration(
18910- &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid,
18911- (lr_w, frame_height as usize),
18912- );
18918+ if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked {
18919+ apply_loop_restoration(
18920+ &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid,
18921+ (lr_w, frame_height as usize),
18922+ );
18923+ }
1891318924
1891418925 let (fw, fh) = (lr_w, frame_height as usize);
1891518926 // An upscaled frame is already at its final size with no mi-aligned
@@ -19290,11 +19301,19 @@ impl PlaneBuf {
1929019301 // 768..832). libaom reconstructs those rows into the frame buffer's
1929119302 // border; nothing reads them back, so they are simply dropped here.
1929219303 let (h, w) = (h.min(self.height.saturating_sub(y)), w.min(self.width.saturating_sub(x)));
19304+ // lane-perf4: one row of each operand at a time. `side` is the
19305+ // prediction/residual stride and `self.width` the plane's, so the
19306+ // three row bases are computed once per row instead of a
19307+ // multiply-add per sample (the samples themselves are unchanged).
19308+ let max = crate::decode::sample_max();
1929319309 for row in 0..h {
19294- for col in 0..w {
19295- let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col])
19296- .clamp(0, crate::decode::sample_max()) as u16;
19297- self.data[(y + row) * self.width + x + col] = sample;
19310+ let src = row * side;
19311+ let dst = (y + row) * self.width + x;
19312+ let pred = &prediction[src..src + w];
19313+ let res = &residual[src..src + w];
19314+ let out = &mut self.data[dst..dst + w];
19315+ for ((o, &p), &r) in out.iter_mut().zip(pred).zip(res) {
19316+ *o = (i32::from(p) + r).clamp(0, max) as u16;
1929819317 }
1929919318 }
1930019319 }
@@ -30862,7 +30881,17 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3086230881 frame_width as usize,
3086330882 frame_height as usize,
3086430883 );
30865- let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone());
30884+ // lane-perf4: loop restoration is the only reader of the pre-CDEF planes,
30885+ // and a frame that restores nothing copied every plane twice for nothing
30886+ // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside
30887+ // [`crate::restoration::apply_loop_restoration_plane`], which returns its
30888+ // input unchanged when the plane's type is `None`). Clone only when some
30889+ // plane actually restores.
30890+ let lr_active = (0..3).any(|p| {
30891+ lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
30892+ && lr.loop_restoration_size[p] != 0
30893+ });
30894+ let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone()));
3086630895 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
3086730896 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
3086830897 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -30886,8 +30915,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3088630915 // boundaries are upscaled (`save_deblock_boundary_lines` runs
3088730916 // `av1_upscale_normative_rows` over its saved rows when the frame is
3088830917 // scaled), so every LR coordinate from here on is in upscaled space.
30889- let (mut deblocked_y, mut deblocked_u, mut deblocked_v) =
30890- (deblocked_y, deblocked_u, deblocked_v);
30918+ let mut deblocked = deblocked;
3089130919 let mut lr_w = frame_width as usize;
3089230920 if let Some((upscaled_w, _)) = superres() {
3089330921 let bd = u32::from(bit_depth());
@@ -30909,16 +30937,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3090930937 y = up(&y, fw, fh, uw);
3091030938 u = up(&u, cw, ch, cuw);
3091130939 v = up(&v, cw, ch, cuw);
30912- deblocked_y = up(&deblocked_y, fw, fh, uw);
30913- deblocked_u = up(&deblocked_u, cw, ch, cuw);
30914- deblocked_v = up(&deblocked_v, cw, ch, cuw);
30940+ if let Some((dy, du, dv)) = deblocked.take() {
30941+ deblocked = Some((up(&dy, fw, fh, uw), up(&du, cw, ch, cuw), up(&dv, cw, ch, cuw)));
30942+ }
3091530943 lr_w = uw;
3091630944 crate::superres::note_upscaled_picture();
3091730945 }
30918- apply_loop_restoration(
30919- &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid,
30920- (lr_w, frame_height as usize),
30921- );
30946+ if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked {
30947+ apply_loop_restoration(
30948+ &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid,
30949+ (lr_w, frame_height as usize),
30950+ );
30951+ }
3092230952
3092330953 let motion_field = build_motion_field(
3092430954 &grid,
crates/ec-av1/src/film_grain.rsmodified+45 -29
@@ -387,35 +387,51 @@ fn add_noise_to_block(
387387
388388 let cy0 = py0 / 2;
389389 let cx0 = px0 / 2;
390- for i in 0..half_h {
391- for j in 0..half_w {
392- let ly = py0 + i * 2;
393- let lx = px0 + j * 2;
394- let l0 = (ly * y_stride + lx) as usize;
395- let average_luma = (picture.y[l0] as i32 + picture.y[l0 + 1] as i32 + 1) >> 1;
396- let cidx = ((cy0 + i) * c_stride + (cx0 + j)) as usize;
397- let gidx = ((cg_row0 + i) * cg_stride + (cg_col0 + j)) as usize;
398- if apply_cb {
399- let scaled = (((average_luma * cb_luma_mult + cb_mult * picture.u[cidx] as i32)
400- >> 6)
401- + cb_offset)
402- .clamp(0, index_max);
403- let delta =
404- (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset)
405- >> scaling_shift;
406- picture.u[cidx] =
407- ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
408- }
409- if apply_cr {
410- let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32)
411- >> 6)
412- + cr_offset)
413- .clamp(0, index_max);
414- let delta =
415- (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset)
416- >> scaling_shift;
417- picture.v[cidx] =
418- ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
390+ if apply_cb || apply_cr {
391+ // lane-perf4: one row of each operand at a time. The 4:2:0 luma
392+ // average (spec 7.18.3.5) reads a horizontally adjacent PAIR of the
393+ // block's own luma row, so `chunks_exact(2)` walks exactly the pairs
394+ // the per-sample `(ly * y_stride + lx)` form used to re-derive, and
395+ // the U/V/grain rows advance in lockstep with it -- same integers,
396+ // same rounding, no per-sample index arithmetic or bounds check.
397+ let cw = half_w as usize;
398+ // U and V are distinct fields, so a disjoint borrow of each is
399+ // needed alongside the immutable read of Y.
400+ let Picture { y: ref luma_plane, u: ref mut u_plane, v: ref mut v_plane, .. } =
401+ *picture;
402+ for i in 0..half_h {
403+ let lrow = ((py0 + i * 2) * y_stride + px0) as usize;
404+ let crow = ((cy0 + i) * c_stride + cx0) as usize;
405+ let grow = ((cg_row0 + i) * cg_stride + cg_col0) as usize;
406+ let luma = &luma_plane[lrow..lrow + cw * 2];
407+ let gcb = &cb_grain[grow..grow + cw];
408+ let gcr = &cr_grain[grow..grow + cw];
409+ let urow = &mut u_plane[crow..crow + cw];
410+ let vrow = &mut v_plane[crow..crow + cw];
411+ for ((((lpair, u), v), &gb), &gr) in luma
412+ .chunks_exact(2)
413+ .zip(urow.iter_mut())
414+ .zip(vrow.iter_mut())
415+ .zip(gcb.iter())
416+ .zip(gcr.iter())
417+ {
418+ let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1;
419+ if apply_cb {
420+ let scaled = (((average_luma * cb_luma_mult + cb_mult * *u as i32) >> 6)
421+ + cb_offset)
422+ .clamp(0, index_max);
423+ let delta =
424+ (scaling_cb[scaled as usize] * gb + rounding_offset) >> scaling_shift;
425+ *u = ((*u as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
426+ }
427+ if apply_cr {
428+ let scaled = (((average_luma * cr_luma_mult + cr_mult * *v as i32) >> 6)
429+ + cr_offset)
430+ .clamp(0, index_max);
431+ let delta =
432+ (scaling_cr[scaled as usize] * gr + rounding_offset) >> scaling_shift;
433+ *v = ((*v as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
434+ }
419435 }
420436 }
421437 }
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