From 65aa140813b4411cc5f3db440a0a0c0d9e5761fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 01:07:16 +0300 Subject: [PATCH] perf(av1): the coefficient writer splits a scan position by shifting, and a wall table for the film crop `side` is 4, 8, 16 or 32, but it reaches `write_coeffs` as a runtime value, so `pos / side` and `pos % side` compiled to a real integer division and remainder once per coefficient -- inside the encoder's largest self-time symbol. Same row and column by construction (the side is a power of two, now debug-asserted). 640x384 gate clip, 4 frames, q 150/120/90/60 and both real-film 1920x768 crops at q 150/60: byte-identical to a b7990ec1 baseline binary. 1920x1080 4 frames q=120, instructions:u ABABx2: 59.344e9 -> 59.200e9 (-0.24%), on top of the CDEF written-map commit. `filter_stage_wall_film` runs the same stage breakdown at 1920x768, and `EC_AV1_WALL_CLIP` points the test loader at a clip outside the repo, so the table can be read on a real film window as well as on the colour-bar fixture. It answers which stage grows on film: the TILE SEARCH does (285.9 -> 497.7 ms/frame, bars vs film A), while every filter stage is flat or lower -- deblock replay 15.4 -> 10.0, plane copies 28.9 -> 23.7, first decode 14.7 -> 17.2 ms/frame. Only the CDEF replay grows (13.0 -> 21.7), which is the film choosing non-zero strengths where bars do not. --- crates/ec-av1/src/encoder.rs | 22 ++++++++++++++++++++-- crates/ec-av1/src/tile.rs | 7 ++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 909c8a9c..52125ab0 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1496,8 +1496,16 @@ mod tests { if !have_ffmpeg() { return None; } - let clip = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../../fixtures/video/h264-1080p-23.976-8bit.mp4"); + // lane-av1speed3: the wall tables below are read on REAL film as + // well as on the colour-bar fixture (`bars` vs `film` in the BD + // gate's own vocabulary), and the film crop lives outside the repo -- + // `EC_AV1_WALL_CLIP` names it. Unset everywhere else, so every other + // caller still gets the fixture. + let clip = match std::env::var("EC_AV1_WALL_CLIP") { + Ok(p) => std::path::PathBuf::from(p), + Err(_) => std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../fixtures/video/h264-1080p-23.976-8bit.mp4"), + }; if !clip.exists() { return None; } @@ -2168,6 +2176,16 @@ mod tests { filter_stage_wall(3840, 1608, 4, (2, 1), &[8, 12]); } + /// The same breakdown at the real films' own crop size. With + /// `EC_AV1_WALL_CLIP` pointing at a film window this is the film row; with + /// it unset it is the colour-bar fixture at the same size, which is what + /// makes the pair a comparison rather than a number. + #[test] + #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] + fn filter_stage_wall_film() { + filter_stage_wall(1920, 768, 4, (2, 1), &[8]); + } + fn filter_stage_wall( width: usize, height: usize, diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 1234fad1..afbb022a 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -4543,9 +4543,14 @@ fn write_coeffs( write_eob(enc, coding, eob, plane); + // lane-av1speed3: `side` is 4, 8, 16 or 32, but it is a runtime value -- + // `pos / side` compiles to a real integer division, once per coefficient, + // in the encoder's largest self-time symbol. Same row and column. + debug_assert!(side.is_power_of_two(), "the scan splits a position by shifting"); + let (shift, mask) = (side.trailing_zeros(), side - 1); for scan_idx in (0..eob).rev() { let pos = scan[scan_idx] as usize; - let (row, col) = (pos / side, pos % side); + let (row, col) = (pos >> shift, pos & mask); let level = grid[pos].abs(); if scan_idx == eob - 1 { let ctx = eob_coeff_ctx(scan_idx, side * side);