From 06abb60130ad79422c9d9513ec26e7085224110b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 05:22:59 +0300 Subject: [PATCH] perf(av1): the motion search reads the reference picture's own plane Every call to `motion::search` allocated and converted the WHOLE reference luma plane from the encoder's `u8` shadow copy to `u16`, once per searched block. The DPB picture already holds that plane as `u16`, so the search now takes it directly and the shadow copy is gone. Output is byte-identical (the BD gate's four points per clip and the sequence bench's 64931 bytes are unchanged); instructions:u on the 24-frame 1280x720 sequence bench fall 384.4G -> 283.6G (-26.2%). --- crates/ec-av1/src/encode.rs | 20 ++++++-------------- crates/ec-av1/src/motion.rs | 30 +++++++++++++++++------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index abe3ed0b..713a1aa8 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1711,7 +1711,6 @@ fn code_square_inter( search: &Search, mode_bits: &[f64; 13], reference: &Picture, - ref_y8: &[u8], stack: &MvStack, fctx: &crate::decode::FrameCtx, ) -> (BlockCoeffs, f64) { let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx); @@ -1843,7 +1842,7 @@ fn code_square_inter( if leaf_new_mv() { let source_block = luma.source_block(x, y, side); let found = motion::search( - ref_y8, + ref_luma.0, ref_luma.1, ref_luma.2, ref_luma.3, @@ -2809,7 +2808,6 @@ fn search_inter_block( search: &Search, mode_bits: &[f64; 13], reference: &Picture, - ref_y8: &[u8], stack: &MvStack, fctx: &crate::decode::FrameCtx, ) -> (BlockCoeffs, f64) { // `luma_set` is the INTRA candidates' table; the two inter candidates @@ -3049,7 +3047,7 @@ fn search_inter_block( #[cfg(test)] let t = std::time::Instant::now(); let found = motion::search( - ref_y8, + ref_luma.0, ref_luma.1, ref_luma.2, ref_luma.3, @@ -3267,7 +3265,6 @@ pub(crate) fn encode_inter_frame( // integer-`u8`-scale), so the DPB reference plane is narrowed for it -- // once per frame (lane-av1rd2), not once per block, which is a whole // plane's map per 32x32 block and now per leaf too. - let ref_y8: Vec = reference.y.iter().map(|&v| v as u8).collect(); let mut grid = MiGrid::new(mi_cols, mi_rows); let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; @@ -3314,7 +3311,6 @@ pub(crate) fn encode_inter_frame( &search, &mode_bits_table, reference, - &ref_y8, &stack, fctx, ); cost_whole += search.lambda * partition_bits(BLOCK, false); @@ -3366,8 +3362,7 @@ pub(crate) fn encode_inter_frame( &search, &mode_bits_table, reference, - &ref_y8, - &stack, fctx, + &stack, fctx, ); let leaf_cost = leaf_cost + search.lambda * partition_bits(SUB, false); @@ -3401,8 +3396,7 @@ pub(crate) fn encode_inter_frame( &search, &mode_bits_table, reference, - &ref_y8, - &stack8, fctx, + &stack8, fctx, ); cost8 += cost; record_mi(&mut grid, mr, mc, 2, leaf8.inter); @@ -3503,8 +3497,7 @@ pub(crate) fn encode_inter_frame( &search, &mode_bits_table, reference, - &ref_y8, - &stack, fctx, + &stack, fctx, ); for dr in 0..4 { for dc in 0..4 { @@ -3564,8 +3557,7 @@ pub(crate) fn encode_inter_frame( &search, &mode_bits_table, reference, - &ref_y8, - &stack, fctx, + &stack, fctx, ); for dr in 0..2 { for dc in 0..2 { diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index 5f2b586f..57515a25 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -190,7 +190,7 @@ pub struct MotionSearch { /// empty (the same contracts [`predict`] has). #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor pub(crate) fn search( - reference: &[u8], + reference: &[u16], stride: usize, ref_width: usize, ref_height: usize, @@ -214,7 +214,7 @@ pub(crate) fn search( /// checks. #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor fn search_traced( - reference: &[u8], + reference: &[u16], stride: usize, ref_width: usize, ref_height: usize, @@ -247,7 +247,7 @@ fn search_traced( /// [`SEARCH_INITIAL_STEP_PEL`]. #[allow(clippy::too_many_arguments)] fn search_traced_from_step( - reference: &[u8], + reference: &[u16], stride: usize, ref_width: usize, ref_height: usize, @@ -263,17 +263,17 @@ fn search_traced_from_step( assert_eq!(source.len(), block_w * block_h, "source is one block"); assert!(!reference.is_empty(), "a reference plane has samples"); - // lane-hbd r2: this encoder-side search stays `u8` (see - // `encode::intra_predict_u8`'s doc comment) -- `reference` is converted - // once here, outside the per-candidate closure, since `mc::predict` now - // takes `u16`. - let reference16: Vec = reference.iter().map(|&v| u16::from(v)).collect(); + // lane-av1speed: `reference` arrives as the DPB picture's own `u16` + // plane. It used to be handed the encoder's `u8` shadow copy and + // converted here, whole-plane, on EVERY call -- one 245k-element + // allocate-and-convert per searched block, which profiled as the single + // largest symbol in the encoder (30% of `sequence_bench_sanity`). let mut dst16 = vec![0u16; block_w * block_h]; let mut cost_of = |mv: (i32, i32)| -> f64 { let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; predict( - &reference16, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, + reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst16, fctx, ); let sad: f64 = source @@ -405,6 +405,7 @@ mod tests { let width = 64; let height = 64; let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1); + let plane16: Vec = plane.iter().map(|&v| u16::from(v)).collect(); let block_w = 8; let block_h = 8; // Anchor the source block away from the plane's edges so every sign @@ -433,7 +434,7 @@ mod tests { } } let result = search( - &plane, + &plane16, width, width, height, @@ -463,6 +464,7 @@ mod tests { let plane: Vec = (0..width * height) .map(|i| (2 * (i % width)) as u8) .collect(); + let plane16: Vec = plane.iter().map(|&v| u16::from(v)).collect(); let block_w = 8; let block_h = 6; let anchor_x = 10; @@ -481,7 +483,7 @@ mod tests { } } let result = search( - &plane, + &plane16, width, width, height, @@ -506,6 +508,7 @@ mod tests { let width = 48; let height = 48; let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1); + let plane16: Vec = plane.iter().map(|&v| u16::from(v)).collect(); let block_w = 8; let block_h = 8; let anchor_x = 20; @@ -518,7 +521,7 @@ mod tests { } } let (_result, trace) = search_traced( - &plane, + &plane16, width, width, height, @@ -566,6 +569,7 @@ mod tests { let width = 96; let height = 96; let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1); + let plane16: Vec = plane.iter().map(|&v| u16::from(v)).collect(); let block_w = 8; let block_h = 8; let anchor_x = 48; @@ -586,7 +590,7 @@ mod tests { } } let (result, trace) = search_traced_from_step( - &plane, + &plane16, width, width, height,