diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 90ddec38..a8f7bba6 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -18853,7 +18853,17 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( frame_width as usize, frame_height as usize, ); - let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); + // lane-perf4: loop restoration is the only reader of the pre-CDEF planes, + // and a frame that restores nothing copied every plane twice for nothing + // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside + // [`crate::restoration::apply_loop_restoration_plane`], which returns its + // input unchanged when the plane's type is `None`). Clone only when some + // plane actually restores. + let lr_active = (0..3).any(|p| { + lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None + && lr.loop_restoration_size[p] != 0 + }); + let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone())); // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter // dump above these bisect WHICH filter stage introduced a mismatch. @@ -18877,8 +18887,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // boundaries are upscaled (`save_deblock_boundary_lines` runs // `av1_upscale_normative_rows` over its saved rows when the frame is // scaled), so every LR coordinate from here on is in upscaled space. - let (mut deblocked_y, mut deblocked_u, mut deblocked_v) = - (deblocked_y, deblocked_u, deblocked_v); + let mut deblocked = deblocked; let mut lr_w = frame_width as usize; if let Some((upscaled_w, _)) = superres() { let bd = u32::from(bit_depth()); @@ -18900,16 +18909,18 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( y = up(&y, fw, fh, uw); u = up(&u, cw, ch, cuw); v = up(&v, cw, ch, cuw); - deblocked_y = up(&deblocked_y, fw, fh, uw); - deblocked_u = up(&deblocked_u, cw, ch, cuw); - deblocked_v = up(&deblocked_v, cw, ch, cuw); + if let Some((dy, du, dv)) = deblocked.take() { + deblocked = Some((up(&dy, fw, fh, uw), up(&du, cw, ch, cuw), up(&dv, cw, ch, cuw))); + } lr_w = uw; crate::superres::note_upscaled_picture(); } - apply_loop_restoration( - &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, - (lr_w, frame_height as usize), - ); + if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { + apply_loop_restoration( + &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, + (lr_w, frame_height as usize), + ); + } let (fw, fh) = (lr_w, frame_height as usize); // An upscaled frame is already at its final size with no mi-aligned @@ -19290,11 +19301,19 @@ impl PlaneBuf { // 768..832). libaom reconstructs those rows into the frame buffer's // border; nothing reads them back, so they are simply dropped here. let (h, w) = (h.min(self.height.saturating_sub(y)), w.min(self.width.saturating_sub(x))); + // lane-perf4: one row of each operand at a time. `side` is the + // prediction/residual stride and `self.width` the plane's, so the + // three row bases are computed once per row instead of a + // multiply-add per sample (the samples themselves are unchanged). + let max = crate::decode::sample_max(); for row in 0..h { - for col in 0..w { - let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col]) - .clamp(0, crate::decode::sample_max()) as u16; - self.data[(y + row) * self.width + x + col] = sample; + let src = row * side; + let dst = (y + row) * self.width + x; + let pred = &prediction[src..src + w]; + let res = &residual[src..src + w]; + let out = &mut self.data[dst..dst + w]; + for ((o, &p), &r) in out.iter_mut().zip(pred).zip(res) { + *o = (i32::from(p) + r).clamp(0, max) as u16; } } } @@ -30862,7 +30881,17 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( frame_width as usize, frame_height as usize, ); - let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); + // lane-perf4: loop restoration is the only reader of the pre-CDEF planes, + // and a frame that restores nothing copied every plane twice for nothing + // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside + // [`crate::restoration::apply_loop_restoration_plane`], which returns its + // input unchanged when the plane's type is `None`). Clone only when some + // plane actually restores. + let lr_active = (0..3).any(|p| { + lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None + && lr.loop_restoration_size[p] != 0 + }); + let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone())); // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter // dump above these bisect WHICH filter stage introduced a mismatch. @@ -30886,8 +30915,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // boundaries are upscaled (`save_deblock_boundary_lines` runs // `av1_upscale_normative_rows` over its saved rows when the frame is // scaled), so every LR coordinate from here on is in upscaled space. - let (mut deblocked_y, mut deblocked_u, mut deblocked_v) = - (deblocked_y, deblocked_u, deblocked_v); + let mut deblocked = deblocked; let mut lr_w = frame_width as usize; if let Some((upscaled_w, _)) = superres() { let bd = u32::from(bit_depth()); @@ -30909,16 +30937,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( y = up(&y, fw, fh, uw); u = up(&u, cw, ch, cuw); v = up(&v, cw, ch, cuw); - deblocked_y = up(&deblocked_y, fw, fh, uw); - deblocked_u = up(&deblocked_u, cw, ch, cuw); - deblocked_v = up(&deblocked_v, cw, ch, cuw); + if let Some((dy, du, dv)) = deblocked.take() { + deblocked = Some((up(&dy, fw, fh, uw), up(&du, cw, ch, cuw), up(&dv, cw, ch, cuw))); + } lr_w = uw; crate::superres::note_upscaled_picture(); } - apply_loop_restoration( - &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, - (lr_w, frame_height as usize), - ); + if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { + apply_loop_restoration( + &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, + (lr_w, frame_height as usize), + ); + } let motion_field = build_motion_field( &grid, diff --git a/crates/ec-av1/src/film_grain.rs b/crates/ec-av1/src/film_grain.rs index 8efafb88..d41c8e85 100644 --- a/crates/ec-av1/src/film_grain.rs +++ b/crates/ec-av1/src/film_grain.rs @@ -387,35 +387,51 @@ fn add_noise_to_block( let cy0 = py0 / 2; let cx0 = px0 / 2; - for i in 0..half_h { - for j in 0..half_w { - let ly = py0 + i * 2; - let lx = px0 + j * 2; - let l0 = (ly * y_stride + lx) as usize; - let average_luma = (picture.y[l0] as i32 + picture.y[l0 + 1] as i32 + 1) >> 1; - let cidx = ((cy0 + i) * c_stride + (cx0 + j)) as usize; - let gidx = ((cg_row0 + i) * cg_stride + (cg_col0 + j)) as usize; - if apply_cb { - let scaled = (((average_luma * cb_luma_mult + cb_mult * picture.u[cidx] as i32) - >> 6) - + cb_offset) - .clamp(0, index_max); - let delta = - (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset) - >> scaling_shift; - picture.u[cidx] = - ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; - } - if apply_cr { - let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32) - >> 6) - + cr_offset) - .clamp(0, index_max); - let delta = - (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset) - >> scaling_shift; - picture.v[cidx] = - ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; + if apply_cb || apply_cr { + // lane-perf4: one row of each operand at a time. The 4:2:0 luma + // average (spec 7.18.3.5) reads a horizontally adjacent PAIR of the + // block's own luma row, so `chunks_exact(2)` walks exactly the pairs + // the per-sample `(ly * y_stride + lx)` form used to re-derive, and + // the U/V/grain rows advance in lockstep with it -- same integers, + // same rounding, no per-sample index arithmetic or bounds check. + let cw = half_w as usize; + // U and V are distinct fields, so a disjoint borrow of each is + // needed alongside the immutable read of Y. + let Picture { y: ref luma_plane, u: ref mut u_plane, v: ref mut v_plane, .. } = + *picture; + for i in 0..half_h { + let lrow = ((py0 + i * 2) * y_stride + px0) as usize; + let crow = ((cy0 + i) * c_stride + cx0) as usize; + let grow = ((cg_row0 + i) * cg_stride + cg_col0) as usize; + let luma = &luma_plane[lrow..lrow + cw * 2]; + let gcb = &cb_grain[grow..grow + cw]; + let gcr = &cr_grain[grow..grow + cw]; + let urow = &mut u_plane[crow..crow + cw]; + let vrow = &mut v_plane[crow..crow + cw]; + for ((((lpair, u), v), &gb), &gr) in luma + .chunks_exact(2) + .zip(urow.iter_mut()) + .zip(vrow.iter_mut()) + .zip(gcb.iter()) + .zip(gcr.iter()) + { + let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1; + if apply_cb { + let scaled = (((average_luma * cb_luma_mult + cb_mult * *u as i32) >> 6) + + cb_offset) + .clamp(0, index_max); + let delta = + (scaling_cb[scaled as usize] * gb + rounding_offset) >> scaling_shift; + *u = ((*u as i32 + delta).clamp(min_chroma, max_chroma)) as u16; + } + if apply_cr { + let scaled = (((average_luma * cr_luma_mult + cr_mult * *v as i32) >> 6) + + cr_offset) + .clamp(0, index_max); + let delta = + (scaling_cr[scaled as usize] * gr + rounding_offset) >> scaling_shift; + *v = ((*v as i32 + delta).clamp(min_chroma, max_chroma)) as u16; + } } } } diff --git a/crates/ec-av1/src/intra.rs b/crates/ec-av1/src/intra.rs index 13e5c218..6d82879e 100644 --- a/crates/ec-av1/src/intra.rs +++ b/crates/ec-av1/src/intra.rs @@ -623,31 +623,33 @@ fn directional( /// frame edge must be extended by repetition here too, not averaged over its /// own shorter length. fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 { - let extend = |samples: &[u16], want: usize| -> Vec { - if samples.len() >= want { - samples[..want].to_vec() + // lane-perf4: the replication is only ever *summed*, never indexed, so + // the extended edge is a sum rather than the `Vec` it used to be + // built as (two allocations per intra block, 0.9% of a 1080p decode's + // self time between them). Same integers, same order of magnitude, no + // rounding difference: `sum(samples[..n]) + last * (want - n)`. + let extend = |samples: &[u16], want: usize| -> u32 { + let n = samples.len().min(want); + let sum: u32 = samples[..n].iter().map(|&s| u32::from(s)).sum(); + if n < want { + let last = u32::from(*samples.last().expect("an edge that exists has samples")); + sum + last * (want - n) as u32 } else { - let last = *samples.last().expect("an edge that exists has samples"); - let mut v = samples.to_vec(); - v.resize(want, last); - v + sum } }; let above = above.map(|a| extend(a, bw)); let left = left.map(|l| extend(l, bh)); - let average = |samples: &[u16]| { - let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); - ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 - }; - match (&above, &left) { + let average = |sum: u32, count: u32| ((sum + (count >> 1)) / count) as i32; + match (above, left) { (None, None) => 1i32 << (crate::decode::bit_depth() - 1), - (Some(a), None) => average(a), - (None, Some(l)) => average(l), + (Some(a), None) => average(a, bw as u32), + (None, Some(l)) => average(l, bh as u32), (Some(a), Some(l)) if bw == bh => { // `dc_predictor` (libaom `intrapred.c`): exact division, the // square path this whole lane must leave bit-identical. - let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum(); - let count = (a.len() + l.len()) as u32; + let sum: u32 = a + l; + let count = (bw + bh) as u32; ((sum + (count >> 1)) / count) as i32 } (Some(a), Some(l)) => { @@ -657,7 +659,7 @@ fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 // every 1:2 ratio, `d == 5` for every 1:4 one; libaom comments // this exact derivation in `intrapred.c` rather than tabulating // it, so this ports the derivation, not a lookup). - let sum: u32 = a.iter().chain(l).map(|&s| u32::from(s)).sum(); + let sum: u32 = a + l; let (shift1, multiplier) = dc_rect_multiplier(bw, bh); let num = u64::from(sum + ((bw + bh) as u32 >> 1)); (((num >> shift1) * multiplier) >> 16) as i32