diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 7481b423..96cc895f 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -3345,6 +3345,103 @@ where Ok((out, stored)) } +/// Runs `job(tile_index, ctx)` once per tile, on `EC_AV1_TILE_THREADS` +/// workers, and returns the results in tile order. The counterpart of +/// [`write_tiles`] for the RD/motion SEARCH: a tile's job owns every piece of +/// state the search mutates (its own reconstruction planes, its own `MiGrid`, +/// its own neighbour-mode bands and its own [`crate::decode::FrameCtx`]), and +/// availability is already tile-relative (`clip_planes_to_tile`, +/// `MiGrid::set_tile_bounds`), so what a tile decides cannot depend on which +/// other tiles have run -- gated by +/// `encoder::tests::tile_bytes_do_not_depend_on_the_thread_count`, which now +/// covers the search as well as the write. +/// +/// The `FrameCtx` copy is made on the calling thread (`&FrameCtx` is `!Sync` +/// by design) and moved into the worker, exactly as [`crate::par::run_bands`] +/// does it for the decoder's filter bands. +fn search_tiles(tiles: usize, fctx: &crate::decode::FrameCtx, job: F) -> Result> +where + T: Send, + F: Fn(usize, &crate::decode::FrameCtx) -> Result + Sync, +{ + let threads = crate::par::tile_threads().min(tiles); + if threads <= 1 { + return (0..tiles).map(|index| job(index, fctx)).collect(); + } + type Slot = Option>; + let done: std::sync::Mutex>> = + std::sync::Mutex::new((0..tiles).map(|_| None).collect()); + { + let batch = crate::par::Batch::new("ec-av1-tile-search"); + for (from, to) in crate::par::bands(tiles, threads) { + let done = &done; + let job = &job; + let ctx = crate::decode::filter_ctx_copy(fctx); + batch.submit(move || { + for index in from..to { + let searched = job(index, &ctx); + done.lock().expect("tile slots")[index] = Some(searched); + } + }); + } + } + done.into_inner() + .expect("tile slots") + .into_iter() + .map(|slot| slot.expect("every tile was searched")) + .collect() +} + +/// One tile job's own copy of a plane: the frame's source, a blank +/// reconstruction of the frame's size, and the frame's own bounds -- narrowed +/// to the tile by `clip_planes_to_tile` right after. Blank rather than a copy +/// of the frame's reconstruction because a tile's prediction never reads a +/// sample outside its own rectangle, and every sample inside it is written +/// before it is read. +fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, true_height: usize) -> Plane<'_> { + Plane { + source, + reconstruction: vec![128; source.len()], + width, + height, + true_width, + true_height, + tile_x0: 0, + tile_y0: 0, + tile_x1: width, + tile_y1: height, + } +} + +/// Copies the rectangle `[x0, x1) x [y0, y1)` of a tile job's own plane into +/// the frame's plane, through the [`crate::par::Shared`] every job holds. +/// +/// corner-cut: the disjointness that makes the aliasing sound is the caller's +/// -- `x0..x1`/`y0..y1` are the job's own `TileRect`, and a frame's tile +/// rectangles partition it -- checked end-to-end by +/// `tile_bytes_do_not_depend_on_the_thread_count` rather than by the borrow +/// checker, exactly the contract `Shared`'s own doc names. Upgrade path is +/// row-range `split_at_mut` plumbing through `Plane`, which would have to +/// thread an origin offset through every prediction and transform call site +/// for the same bytes. +#[allow(unsafe_code)] +fn copy_rect( + dst: crate::par::Shared<'_, Vec>, + src: &[u8], + stride: usize, + x0: usize, + y0: usize, + x1: usize, + y1: usize, +) { + // SAFETY: this job touches only its own tile rectangle (see above). + let dst = unsafe { dst.get() }; + for y in y0..y1 { + let (a, b) = (y * stride + x0, y * stride + x1); + dst[a..b].copy_from_slice(&src[a..b]); + } +} + /// The frame header's `tile_info` (spec 5.9.15) for `layout`: /// `context_update_tile_id` names the largest tile, whose end-of-tile CDF /// tables the frame stores (spec 7.20), which is what @@ -3612,8 +3709,6 @@ pub(crate) fn encode_key_frame_inner( // symbol the writer will actually write. // The bookkeeping is kept on the 16x16 grid the tile writer keeps it on, // because a 32x32 block may be split into four 16x16 ones. - let mut above_mode = vec![DC_PRED; cols * 2]; - let mut left_mode = vec![DC_PRED; rows * 2]; let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); // This frame's tile grid (one tile unless the facade armed more). let layout = @@ -3624,15 +3719,36 @@ pub(crate) fn encode_key_frame_inner( // `mi_row_starts`), which is what makes the writer's rects and the // reader's the same rects. let header_tile_info = header.tile_info.clone(); - let mut superblocks = Vec::with_capacity(sb_cols * sb_rows); - let mut modes = Vec::with_capacity(cols * rows); - for sb_row in 0..sb_rows { - for sb_col in 0..sb_cols { - clip_planes_to_tile( - &mut luma, - &mut chroma, - layout.rect(layout.index_of_sb(sb_row as u32, sb_col as u32)), - ); + // lane-av1tsearch: the search runs once per TILE (`search_tiles`), not + // once over the whole frame. Each tile job builds its own reconstruction + // planes and its own `above_mode`/`left_mode` bands, which is also what + // the tile writer reads -- a tile resets its neighbour contexts (spec + // 5.11.1), so a block at a tile's left edge must cost its mode against + // the default context, not against whatever the tile to its west left in + // that row. At one tile the bands are the frame's, exactly as before. + let (frame_width, frame_height) = (picture.width, picture.height); + let luma_out = crate::par::Shared::new(&mut luma.reconstruction); + let [cb, cr] = &mut chroma; + let chroma_out = [ + crate::par::Shared::new(&mut cb.reconstruction), + crate::par::Shared::new(&mut cr.reconstruction), + ]; + let search_tile = |index: usize, + fctx: &crate::decode::FrameCtx| + -> Result)>> { + let rect = layout.rect(index); + let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); + let mut chroma = [ + fresh_plane(&picture_u8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), + fresh_plane(&picture_v8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), + ]; + clip_planes_to_tile(&mut luma, &mut chroma, rect); + let mut above_mode = vec![DC_PRED; cols * 2]; + let mut left_mode = vec![DC_PRED; rows * 2]; + let mut coded: Vec<(usize, Superblock, Vec)> = Vec::new(); + for sb_row in rect.sb_row0 as usize..rect.sb_row1 as usize { + for sb_col in rect.sb_col0 as usize..rect.sb_col1 as usize { + let mut modes = Vec::with_capacity(4); // The quadrants of a superblock are coded in the order the decoder // walks them, which for a 64x64 split into 32x32 blocks is raster // order among the quadrants that are inside the frame. @@ -3816,8 +3932,39 @@ pub(crate) fn encode_key_frame_inner( blocks.push(Quadrant::Whole(whole)); } } - superblocks.push(Superblock::Split(blocks)); + coded.push((sb_row * sb_cols + sb_col, Superblock::Split(blocks), modes)); + } + } + let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); + let (x1, y1) = (rect.mi_col1 as usize * 4, rect.mi_row1 as usize * 4); + copy_rect(luma_out, &luma.reconstruction, frame_width, x0, y0, x1, y1); + for (&out, tile) in chroma_out.iter().zip(&chroma) { + copy_rect( + out, + &tile.reconstruction, + frame_width / 2, + x0 / 2, + y0 / 2, + x1.div_ceil(2), + y1.div_ceil(2), + ); } + Ok(coded) + }; + let searched = search_tiles(layout.count(), fctx, search_tile)?; + // Back into frame raster order: the tile writer indexes `superblocks` by + // the frame's own superblock index, and `modes` is the coding order the + // ablation drivers read. + let mut in_order: Vec)>> = (0..sb_cols * sb_rows).map(|_| None).collect(); + for (at, superblock, block_modes) in searched.into_iter().flatten() { + in_order[at] = Some((superblock, block_modes)); + } + let mut superblocks = Vec::with_capacity(sb_cols * sb_rows); + let mut modes = Vec::with_capacity(cols * rows); + for slot in in_order { + let (superblock, block_modes) = slot.expect("every superblock belongs to a tile"); + superblocks.push(superblock); + modes.extend_from_slice(&block_modes); } #[cfg(test)] @@ -5262,7 +5409,7 @@ pub(crate) fn encode_inter_frame( // whenever the true size is not a 64x64 multiple. let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); let (cols, rows) = (cols as usize, rows as usize); - let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); + let (sb_cols, _sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); // This frame's tile grid, as in `encode_key_frame_inner`. let (tile_cols_log2, tile_rows_log2) = armed_tiles(); @@ -5274,24 +5421,42 @@ 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 mut grid = MiGrid::new(mi_cols, mi_rows); - grid.set_sign_bias(sign_bias); let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; - - for sb_r in 0..sb_rows { - for sb_c in 0..sb_cols { - // Neighbour availability -- intra edges and the MV stack's - // scans alike -- stops at this superblock's own tile, exactly - // where the decoder stops it (`PlaneBuf::set_tile_origin`, - // `MiGrid::set_tile_bounds`). - let rect = layout.rect(layout.index_of_sb(sb_r as u32, sb_c as u32)); - clip_planes_to_tile(&mut luma, &mut chroma, rect); - grid.set_tile_bounds( - rect.mi_row0 as usize, - rect.mi_col0 as usize, - rect.mi_row1 as usize, - rect.mi_col1 as usize, - ); + // lane-av1tsearch: one search job per TILE (`search_tiles`). Neighbour + // availability -- intra edges and the MV stack's scans alike -- already + // stopped at the superblock's own tile (`PlaneBuf::set_tile_origin`, + // `MiGrid::set_tile_bounds`), so a tile reads nothing another tile wrote; + // giving each job its own planes and its own `MiGrid` therefore leaves + // every decision, and so every coded bit, exactly where the whole-frame + // loop left it, at any tile count and any thread count. + let (frame_width, frame_height) = (picture.width, picture.height); + let luma_out = crate::par::Shared::new(&mut luma.reconstruction); + let [cb, cr] = &mut chroma; + let chroma_out = [ + crate::par::Shared::new(&mut cb.reconstruction), + crate::par::Shared::new(&mut cr.reconstruction), + ]; + let search_tile = |index: usize, + fctx: &crate::decode::FrameCtx| + -> Result> { + let rect = layout.rect(index); + let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); + let mut chroma = [ + fresh_plane(&picture_u8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), + fresh_plane(&picture_v8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), + ]; + clip_planes_to_tile(&mut luma, &mut chroma, rect); + let mut grid = MiGrid::new(mi_cols, mi_rows); + grid.set_sign_bias(sign_bias); + grid.set_tile_bounds( + rect.mi_row0 as usize, + rect.mi_col0 as usize, + rect.mi_row1 as usize, + rect.mi_col1 as usize, + ); + let mut blocks: Vec<(usize, Quadrant)> = Vec::new(); + for sb_r in rect.sb_row0 as usize..rect.sb_row1 as usize { + for sb_c in rect.sb_col0 as usize..rect.sb_col1 as usize { for quadrant in 0..4 { let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); // Same filter as the tile writer's: a quadrant whose own mi @@ -5516,14 +5681,14 @@ pub(crate) fn encode_inter_frame( // grid, and their reconstruction is what the // planes hold. partition_hit(1); - blocks[r32 * cols + c32] = Quadrant::Split(leaves); + blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); continue; } restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); } partition_hit(0); record_mi(&mut grid, mi_row, mi_col, 8, block.inter); - blocks[r32 * cols + c32] = Quadrant::Whole(block); + blocks.push((r32 * cols + c32, Quadrant::Whole(block))); } else { // The sub-positions actually inside the true frame, same // filter as `sb_coeff_inter_frame_tile`'s `sub_positions`. @@ -5688,11 +5853,33 @@ pub(crate) fn encode_inter_frame( }); } } - blocks[r32 * cols + c32] = Quadrant::Split(leaves); + blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); } } } } + let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); + let (x1, y1) = (rect.mi_col1 as usize * 4, rect.mi_row1 as usize * 4); + copy_rect(luma_out, &luma.reconstruction, frame_width, x0, y0, x1, y1); + for (&out, tile) in chroma_out.iter().zip(&chroma) { + copy_rect( + out, + &tile.reconstruction, + frame_width / 2, + x0 / 2, + y0 / 2, + x1.div_ceil(2), + y1.div_ceil(2), + ); + } + Ok(blocks) + }; + for (at, quadrant) in search_tiles(layout.count(), fctx, search_tile)? + .into_iter() + .flatten() + { + blocks[at] = quadrant; + } let modes = blocks .iter() diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 459ea325..b31d2980 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1839,6 +1839,171 @@ mod tests { crate::par::set_tile_threads(1); } + /// What the per-tile search (lane-av1tsearch) is worth in wall and in + /// frames per second at the editor's own export sizes: every tile layout + /// crossed with every tile-thread count, plus librav1e at the matching + /// `tile_cols`/`tile_rows`/`threads` as the reference point. Two passes + /// over the whole table (A,B,A,B... rather than AA,BB) with the best of + /// the two kept, so a thermal drift cannot order the rows. + /// + /// Measurement, not a pass/fail gate: it asserts only that every cell + /// produced a stream. + #[test] + #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] + fn tile_search_wall_1080p() { + tile_search_wall(1920, 1080, 12, &[(0, 0), (1, 0), (1, 1), (2, 1)], &[1, 2, 4, 8]); + } + + /// The same table at the 4K frame size the editor exports (3840x1608), + /// at the two layouts with enough tiles to fill this box. + #[test] + #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] + fn tile_search_wall_4k() { + tile_search_wall(3840, 1608, 6, &[(2, 1), (3, 2)], &[1, 8, 12]); + } + + fn tile_search_wall( + width: usize, + height: usize, + frames: usize, + layouts: &[(u32, u32)], + threads: &[usize], + ) { + let _gate_lock = crate::stream::tests::lock_gate_counters(); + let Some(sources) = h264_clip_frames(width, height, frames) else { + eprintln!("SKIP the tile-search wall table: no fixture or no ffmpeg"); + return; + }; + let run = |cols_log2: u32, rows_log2: u32, t: usize| -> (std::time::Duration, usize) { + crate::par::set_tile_threads(t); + let config = EncoderConfig { + width, + height, + base_q_idx: 120, + gop: frames, + colour: Colour::Bt709Limited, + tile_cols_log2: cols_log2, + tile_rows_log2: rows_log2, + }; + let mut enc = Av1Encoder::new(config).unwrap(); + let start = std::time::Instant::now(); + let mut bytes = 0usize; + for picture in &sources { + bytes += enc.encode(picture).unwrap().data.len(); + } + (start.elapsed(), bytes) + }; + let cells: Vec<((u32, u32), usize)> = layouts + .iter() + .flat_map(|&l| threads.iter().map(move |&t| (l, t))) + .collect(); + let mut best: Vec> = vec![None; cells.len()]; + for _pass in 0..2 { + for (i, &((c, r), t)) in cells.iter().enumerate() { + let got = run(c, r, t); + assert!(got.1 > 0, "{}x{} tiles at {t} threads: empty stream", 1 << c, 1 << r); + if best[i].is_none_or(|(w, _)| got.0 < w) { + best[i] = Some(got); + } + } + } + crate::par::set_tile_threads(1); + eprintln!("\n{width}x{height}, {frames} frames, base_q_idx 120 -- best of two passes"); + eprintln!("| layout | tiles | threads | wall | fps | speedup vs 1 tile 1 thread | bytes |"); + let base = best[0].expect("the first cell ran").0.as_secs_f64(); + for (i, &((c, r), t)) in cells.iter().enumerate() { + let (wall, bytes) = best[i].expect("every cell ran"); + let secs = wall.as_secs_f64(); + eprintln!( + "| {}x{} | {} | {t} | {secs:.2}s | {:.2} | {:.2}x | {bytes} |", + 1 << c, + 1 << r, + 1 << (c + r), + frames as f64 / secs, + base / secs, + ); + } + // Parallel efficiency per layout: what the extra threads actually + // took off that layout's own single-threaded wall, and so what share + // of the added cores sat idle. + for &(c, r) in layouts { + let one = cells + .iter() + .position(|&(l, t)| l == (c, r) && t == threads[0]) + .map(|i| best[i].expect("cell").0.as_secs_f64()); + let Some(one) = one else { continue }; + for &t in &threads[1..] { + let i = cells.iter().position(|&(l, tt)| l == (c, r) && tt == t).expect("cell"); + let secs = best[i].expect("cell").0.as_secs_f64(); + let workers = (t.min(1 << (c + r))) as f64 / threads[0] as f64; + let speedup = one / secs; + eprintln!( + "{}x{} tiles, {} -> {t} threads: {speedup:.2}x of a possible {workers:.2}x, \ + idle share {:.0}%", + 1 << c, + 1 << r, + threads[0], + 100.0 * (1.0 - speedup / workers).max(0.0), + ); + } + } + rav1e_wall_reference(width, height, frames, layouts, threads); + } + + /// librav1e at the same sizes, layouts and thread counts, through ffmpeg + /// -- the reference the wall table is read against. + fn rav1e_wall_reference( + width: usize, + height: usize, + frames: usize, + layouts: &[(u32, u32)], + threads: &[usize], + ) { + let clip = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../fixtures/video/h264-1080p-23.976-8bit.mp4"); + let listed = Command::new("ffmpeg").args(["-hide_banner", "-encoders"]).output(); + let has_rav1e = listed + .map(|o| String::from_utf8_lossy(&o.stdout).contains("librav1e")) + .unwrap_or(false); + if !has_rav1e || !clip.exists() { + eprintln!("SKIP the rav1e reference: no librav1e or no fixture"); + return; + } + eprintln!("| rav1e layout | threads | wall | fps | bytes |"); + for &(c, r) in layouts { + for &t in threads { + let out = std::env::temp_dir().join(format!("ec-av1-rav1e-{}.ivf", std::process::id())); + let params = format!( + "speed=6:quantizer=100:tile_cols={}:tile_rows={}:threads={t}", + 1 << c, + 1 << r + ); + let start = std::time::Instant::now(); + let status = Command::new("ffmpeg") + .args(["-v", "error", "-y", "-i", clip.to_str().unwrap()]) + .args(["-frames:v", &frames.to_string()]) + .args(["-vf", &format!("scale={width}:{height}")]) + .args(["-c:v", "librav1e", "-rav1e-params", ¶ms]) + .arg(out.to_str().unwrap()) + .output() + .expect("ffmpeg failed to run"); + let secs = start.elapsed().as_secs_f64(); + if !status.status.success() { + eprintln!("| {}x{} | {t} | FAILED: {} |", 1 << c, 1 << r, String::from_utf8_lossy(&status.stderr).lines().last().unwrap_or("")); + continue; + } + let bytes = std::fs::metadata(&out).map(|m| m.len()).unwrap_or(0); + let _ = std::fs::remove_file(&out); + eprintln!( + "| {}x{} | {t} | {secs:.2}s | {:.2} | {bytes} |", + 1 << c, + 1 << r, + frames as f64 / secs + ); + } + } + } + /// The same round trip at a real 1920x1080 crop of the gate's own clip /// -- the size the editor's export actually runs at, where a tile grid /// is worth having. Ignored by default only for its wall (a 1080p diff --git a/crates/ec-av1/src/par.rs b/crates/ec-av1/src/par.rs index ae9681c6..3ad04f74 100644 --- a/crates/ec-av1/src/par.rs +++ b/crates/ec-av1/src/par.rs @@ -56,11 +56,21 @@ pub(crate) fn set_filter_threads(n: usize) { } /// `EC_AV1_TILE_THREADS`, how many workers the ENCODER spreads a frame's -/// tiles across (default 1 = the shipped single-threaded write). Tiles are -/// entropy-independent by construction (each starts from the frame's own -/// tables and resets its own contexts), so the bytes a frame comes out as do -/// not depend on this -- `encoder::tests::tile_bytes_do_not_depend_on_the_ +/// tiles across -- both the RD/motion SEARCH (`encode::search_tiles`) and the +/// entropy WRITE (`encode::write_tiles`); default 1 = the shipped +/// single-threaded encode. Tiles are independent by construction (each starts +/// from the frame's own tables, resets its own contexts, and reads no sample +/// and no mode outside its own rectangle), so the bytes a frame comes out as +/// do not depend on this -- `encoder::tests::tile_bytes_do_not_depend_on_the_ /// thread_count` is the pin. +/// +/// Measured (lane-av1tsearch, 12-core box, best of two interleaved passes): +/// 1920x1080, 12 frames -- 11.49 s at one tile (any thread count), 6.98 s at +/// 4x2 tiles and 8 threads (1.65x, 1.04 -> 1.72 fps); 3840x1608, 6 frames -- +/// 17.12 s at 4x2 tiles and one thread, 8.64 s at 8 (1.98x, 0.35 -> 0.69 +/// fps). Past ~8 tiles the extra tiles cost bytes without buying wall: what +/// is left is the per-frame serial tail (the filter search re-decodes the +/// whole frame), ~43% of an inter frame by Amdahl on those numbers. static TILE_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); pub(crate) fn tile_threads() -> usize { diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 431df26a..509caf21 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -238,15 +238,6 @@ impl TileLayout { } } - /// Which tile the superblock at `(sb_r, sb_c)` belongs to -- what the - /// encoder's frame-raster search consults to clip a block's neighbour - /// availability to its own tile. - pub(crate) fn index_of_sb(&self, sb_r: u32, sb_c: u32) -> usize { - let col = self.col_starts_sb[1..].partition_point(|&s| s <= sb_c); - let row = self.row_starts_sb[1..].partition_point(|&s| s <= sb_r); - row * self.cols() as usize + col - } - /// The largest tile by mi area, `context_update_tile_id`'s own rule /// (spec 6.8.14 leaves the choice to the encoder; libaom and rav1e both /// name the biggest tile, whose end-of-tile tables the frame stores).