forge
Explore
Log in

Don't you dare go Hollow

The motion search reads the reference picture's own plane

Browse files at this commit

06abb60130ad79422c9d9513ec26e7085224110bAhmet Kaan GümüşParent: 1dcee8a
  • perf
    • the motion search reads the reference picture's own plane
Show full message
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.rsmodified+6 -14
@@ -1711,7 +1711,6 @@ fn code_square_inter(
1711 search: &Search,1711 search: &Search,
1712 mode_bits: &[f64; 13],1712 mode_bits: &[f64; 13],
1713 reference: &Picture,1713 reference: &Picture,
1714- ref_y8: &[u8],
1715 stack: &MvStack, fctx: &crate::decode::FrameCtx,1714 stack: &MvStack, fctx: &crate::decode::FrameCtx,
1716 ) -> (BlockCoeffs, f64) {1715 ) -> (BlockCoeffs, f64) {
1717 let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx);1716 let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx);
@@ -1843,7 +1842,7 @@ fn code_square_inter(
1843 if leaf_new_mv() {1842 if leaf_new_mv() {
1844 let source_block = luma.source_block(x, y, side);1843 let source_block = luma.source_block(x, y, side);
1845 let found = motion::search(1844 let found = motion::search(
1846- ref_y8,1845+ ref_luma.0,
1847 ref_luma.1,1846 ref_luma.1,
1848 ref_luma.2,1847 ref_luma.2,
1849 ref_luma.3,1848 ref_luma.3,
@@ -2809,7 +2808,6 @@ fn search_inter_block(
2809 search: &Search,2808 search: &Search,
2810 mode_bits: &[f64; 13],2809 mode_bits: &[f64; 13],
2811 reference: &Picture,2810 reference: &Picture,
2812- ref_y8: &[u8],
2813 stack: &MvStack, fctx: &crate::decode::FrameCtx,2811 stack: &MvStack, fctx: &crate::decode::FrameCtx,
2814 ) -> (BlockCoeffs, f64) {2812 ) -> (BlockCoeffs, f64) {
2815 // `luma_set` is the INTRA candidates' table; the two inter candidates2813 // `luma_set` is the INTRA candidates' table; the two inter candidates
@@ -3049,7 +3047,7 @@ fn search_inter_block(
3049 #[cfg(test)]3047 #[cfg(test)]
3050 let t = std::time::Instant::now();3048 let t = std::time::Instant::now();
3051 let found = motion::search(3049 let found = motion::search(
3052- ref_y8,3050+ ref_luma.0,
3053 ref_luma.1,3051 ref_luma.1,
3054 ref_luma.2,3052 ref_luma.2,
3055 ref_luma.3,3053 ref_luma.3,
@@ -3267,7 +3265,6 @@ pub(crate) fn encode_inter_frame(
3267 // integer-`u8`-scale), so the DPB reference plane is narrowed for it --3265 // integer-`u8`-scale), so the DPB reference plane is narrowed for it --
3268 // once per frame (lane-av1rd2), not once per block, which is a whole3266 // once per frame (lane-av1rd2), not once per block, which is a whole
3269 // plane's map per 32x32 block and now per leaf too.3267 // plane's map per 32x32 block and now per leaf too.
3270- let ref_y8: Vec<u8> = reference.y.iter().map(|&v| v as u8).collect();
3271 let mut grid = MiGrid::new(mi_cols, mi_rows);3268 let mut grid = MiGrid::new(mi_cols, mi_rows);
3272 let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows];3269 let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows];
3273 3270
@@ -3314,7 +3311,6 @@ pub(crate) fn encode_inter_frame(
3314 &search,3311 &search,
3315 &mode_bits_table,3312 &mode_bits_table,
3316 reference,3313 reference,
3317- &ref_y8,
3318 &stack, fctx,3314 &stack, fctx,
3319 );3315 );
3320 cost_whole += search.lambda * partition_bits(BLOCK, false);3316 cost_whole += search.lambda * partition_bits(BLOCK, false);
@@ -3366,8 +3362,7 @@ pub(crate) fn encode_inter_frame(
3366 &search,3362 &search,
3367 &mode_bits_table,3363 &mode_bits_table,
3368 reference,3364 reference,
3369- &ref_y8,3365+ &stack, fctx,
3370- &stack, fctx,
3371 );3366 );
3372 let leaf_cost = leaf_cost3367 let leaf_cost = leaf_cost
3373 + search.lambda * partition_bits(SUB, false);3368 + search.lambda * partition_bits(SUB, false);
@@ -3401,8 +3396,7 @@ pub(crate) fn encode_inter_frame(
3401 &search,3396 &search,
3402 &mode_bits_table,3397 &mode_bits_table,
3403 reference,3398 reference,
3404- &ref_y8,3399+ &stack8, fctx,
3405- &stack8, fctx,
3406 );3400 );
3407 cost8 += cost;3401 cost8 += cost;
3408 record_mi(&mut grid, mr, mc, 2, leaf8.inter);3402 record_mi(&mut grid, mr, mc, 2, leaf8.inter);
@@ -3503,8 +3497,7 @@ pub(crate) fn encode_inter_frame(
3503 &search,3497 &search,
3504 &mode_bits_table,3498 &mode_bits_table,
3505 reference,3499 reference,
3506- &ref_y8,3500+ &stack, fctx,
3507- &stack, fctx,
3508 );3501 );
3509 for dr in 0..4 {3502 for dr in 0..4 {
3510 for dc in 0..4 {3503 for dc in 0..4 {
@@ -3564,8 +3557,7 @@ pub(crate) fn encode_inter_frame(
3564 &search,3557 &search,
3565 &mode_bits_table,3558 &mode_bits_table,
3566 reference,3559 reference,
3567- &ref_y8,3560+ &stack, fctx,
3568- &stack, fctx,
3569 );3561 );
3570 for dr in 0..2 {3562 for dr in 0..2 {
3571 for dc in 0..2 {3563 for dc in 0..2 {
crates/ec-av1/src/motion.rsmodified+17 -13
@@ -190,7 +190,7 @@ pub struct MotionSearch {
190 /// empty (the same contracts [`predict`] has).190 /// empty (the same contracts [`predict`] has).
191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
192 pub(crate) fn search(192 pub(crate) fn search(
193- reference: &[u8],193+ reference: &[u16],
194 stride: usize,194 stride: usize,
195 ref_width: usize,195 ref_width: usize,
196 ref_height: usize,196 ref_height: usize,
@@ -214,7 +214,7 @@ pub(crate) fn search(
214 /// checks.214 /// checks.
215 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor215 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
216 fn search_traced(216 fn search_traced(
217- reference: &[u8],217+ reference: &[u16],
218 stride: usize,218 stride: usize,
219 ref_width: usize,219 ref_width: usize,
220 ref_height: usize,220 ref_height: usize,
@@ -247,7 +247,7 @@ fn search_traced(
247 /// [`SEARCH_INITIAL_STEP_PEL`].247 /// [`SEARCH_INITIAL_STEP_PEL`].
248 #[allow(clippy::too_many_arguments)]248 #[allow(clippy::too_many_arguments)]
249 fn search_traced_from_step(249 fn search_traced_from_step(
250- reference: &[u8],250+ reference: &[u16],
251 stride: usize,251 stride: usize,
252 ref_width: usize,252 ref_width: usize,
253 ref_height: usize,253 ref_height: usize,
@@ -263,17 +263,17 @@ fn search_traced_from_step(
263 assert_eq!(source.len(), block_w * block_h, "source is one block");263 assert_eq!(source.len(), block_w * block_h, "source is one block");
264 assert!(!reference.is_empty(), "a reference plane has samples");264 assert!(!reference.is_empty(), "a reference plane has samples");
265 265
266- // lane-hbd r2: this encoder-side search stays `u8` (see266+ // lane-av1speed: `reference` arrives as the DPB picture's own `u16`
267- // `encode::intra_predict_u8`'s doc comment) -- `reference` is converted267+ // plane. It used to be handed the encoder's `u8` shadow copy and
268- // once here, outside the per-candidate closure, since `mc::predict` now268+ // converted here, whole-plane, on EVERY call -- one 245k-element
269- // takes `u16`.269+ // allocate-and-convert per searched block, which profiled as the single
270- let reference16: Vec<u16> = reference.iter().map(|&v| u16::from(v)).collect();270+ // largest symbol in the encoder (30% of `sequence_bench_sanity`).
271 let mut dst16 = vec![0u16; block_w * block_h];271 let mut dst16 = vec![0u16; block_w * block_h];
272 let mut cost_of = |mv: (i32, i32)| -> f64 {272 let mut cost_of = |mv: (i32, i32)| -> f64 {
273 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;273 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;
274 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;274 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;
275 predict(275 predict(
276- &reference16, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h,276+ reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h,
277 &mut dst16, fctx,277 &mut dst16, fctx,
278 );278 );
279 let sad: f64 = source279 let sad: f64 = source
@@ -405,6 +405,7 @@ mod tests {
405 let width = 64;405 let width = 64;
406 let height = 64;406 let height = 64;
407 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);407 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);
408+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
408 let block_w = 8;409 let block_w = 8;
409 let block_h = 8;410 let block_h = 8;
410 // Anchor the source block away from the plane's edges so every sign411 // Anchor the source block away from the plane's edges so every sign
@@ -433,7 +434,7 @@ mod tests {
433 }434 }
434 }435 }
435 let result = search(436 let result = search(
436- &plane,437+ &plane16,
437 width,438 width,
438 width,439 width,
439 height,440 height,
@@ -463,6 +464,7 @@ mod tests {
463 let plane: Vec<u8> = (0..width * height)464 let plane: Vec<u8> = (0..width * height)
464 .map(|i| (2 * (i % width)) as u8)465 .map(|i| (2 * (i % width)) as u8)
465 .collect();466 .collect();
467+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
466 let block_w = 8;468 let block_w = 8;
467 let block_h = 6;469 let block_h = 6;
468 let anchor_x = 10;470 let anchor_x = 10;
@@ -481,7 +483,7 @@ mod tests {
481 }483 }
482 }484 }
483 let result = search(485 let result = search(
484- &plane,486+ &plane16,
485 width,487 width,
486 width,488 width,
487 height,489 height,
@@ -506,6 +508,7 @@ mod tests {
506 let width = 48;508 let width = 48;
507 let height = 48;509 let height = 48;
508 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);510 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);
511+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
509 let block_w = 8;512 let block_w = 8;
510 let block_h = 8;513 let block_h = 8;
511 let anchor_x = 20;514 let anchor_x = 20;
@@ -518,7 +521,7 @@ mod tests {
518 }521 }
519 }522 }
520 let (_result, trace) = search_traced(523 let (_result, trace) = search_traced(
521- &plane,524+ &plane16,
522 width,525 width,
523 width,526 width,
524 height,527 height,
@@ -566,6 +569,7 @@ mod tests {
566 let width = 96;569 let width = 96;
567 let height = 96;570 let height = 96;
568 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);571 let (plane, _unused) = plane_and_block(width, height, 0, 0, 1, 1);
572+ let plane16: Vec<u16> = plane.iter().map(|&v| u16::from(v)).collect();
569 let block_w = 8;573 let block_w = 8;
570 let block_h = 8;574 let block_h = 8;
571 let anchor_x = 48;575 let anchor_x = 48;
@@ -586,7 +590,7 @@ mod tests {
586 }590 }
587 }591 }
588 let (result, trace) = search_traced_from_step(592 let (result, trace) = search_traced_from_step(
589- &plane,593+ &plane16,
590 width,594 width,
591 width,595 width,
592 height,596 height,