Merge branch 'main' into lane-av1pal2
Browse files at this commit4 changed files +399 -46
- crates/ec-av1/src/encode.rs +220 -33
- crates/ec-av1/src/encoder.rs +165 -0
- crates/ec-av1/src/par.rs +14 -4
- crates/ec-av1/src/tile.rs +0 -9
crates/ec-av1/src/encode.rsmodified+220 -33
| @@ -3430,6 +3430,103 @@ where | |||
| 3430 | Ok((out, stored)) | 3430 | Ok((out, stored)) |
| 3431 | } | 3431 | } |
| 3432 | 3432 | ||
| 3433 | +/// Runs `job(tile_index, ctx)` once per tile, on `EC_AV1_TILE_THREADS` | ||
| 3434 | +/// workers, and returns the results in tile order. The counterpart of | ||
| 3435 | +/// [`write_tiles`] for the RD/motion SEARCH: a tile's job owns every piece of | ||
| 3436 | +/// state the search mutates (its own reconstruction planes, its own `MiGrid`, | ||
| 3437 | +/// its own neighbour-mode bands and its own [`crate::decode::FrameCtx`]), and | ||
| 3438 | +/// availability is already tile-relative (`clip_planes_to_tile`, | ||
| 3439 | +/// `MiGrid::set_tile_bounds`), so what a tile decides cannot depend on which | ||
| 3440 | +/// other tiles have run -- gated by | ||
| 3441 | +/// `encoder::tests::tile_bytes_do_not_depend_on_the_thread_count`, which now | ||
| 3442 | +/// covers the search as well as the write. | ||
| 3443 | +/// | ||
| 3444 | +/// The `FrameCtx` copy is made on the calling thread (`&FrameCtx` is `!Sync` | ||
| 3445 | +/// by design) and moved into the worker, exactly as [`crate::par::run_bands`] | ||
| 3446 | +/// does it for the decoder's filter bands. | ||
| 3447 | +fn search_tiles<T, F>(tiles: usize, fctx: &crate::decode::FrameCtx, job: F) -> Result<Vec<T>> | ||
| 3448 | +where | ||
| 3449 | + T: Send, | ||
| 3450 | + F: Fn(usize, &crate::decode::FrameCtx) -> Result<T> + Sync, | ||
| 3451 | +{ | ||
| 3452 | + let threads = crate::par::tile_threads().min(tiles); | ||
| 3453 | + if threads <= 1 { | ||
| 3454 | + return (0..tiles).map(|index| job(index, fctx)).collect(); | ||
| 3455 | + } | ||
| 3456 | + type Slot<T> = Option<Result<T>>; | ||
| 3457 | + let done: std::sync::Mutex<Vec<Slot<T>>> = | ||
| 3458 | + std::sync::Mutex::new((0..tiles).map(|_| None).collect()); | ||
| 3459 | + { | ||
| 3460 | + let batch = crate::par::Batch::new("ec-av1-tile-search"); | ||
| 3461 | + for (from, to) in crate::par::bands(tiles, threads) { | ||
| 3462 | + let done = &done; | ||
| 3463 | + let job = &job; | ||
| 3464 | + let ctx = crate::decode::filter_ctx_copy(fctx); | ||
| 3465 | + batch.submit(move || { | ||
| 3466 | + for index in from..to { | ||
| 3467 | + let searched = job(index, &ctx); | ||
| 3468 | + done.lock().expect("tile slots")[index] = Some(searched); | ||
| 3469 | + } | ||
| 3470 | + }); | ||
| 3471 | + } | ||
| 3472 | + } | ||
| 3473 | + done.into_inner() | ||
| 3474 | + .expect("tile slots") | ||
| 3475 | + .into_iter() | ||
| 3476 | + .map(|slot| slot.expect("every tile was searched")) | ||
| 3477 | + .collect() | ||
| 3478 | +} | ||
| 3479 | + | ||
| 3480 | +/// One tile job's own copy of a plane: the frame's source, a blank | ||
| 3481 | +/// reconstruction of the frame's size, and the frame's own bounds -- narrowed | ||
| 3482 | +/// to the tile by `clip_planes_to_tile` right after. Blank rather than a copy | ||
| 3483 | +/// of the frame's reconstruction because a tile's prediction never reads a | ||
| 3484 | +/// sample outside its own rectangle, and every sample inside it is written | ||
| 3485 | +/// before it is read. | ||
| 3486 | +fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, true_height: usize) -> Plane<'_> { | ||
| 3487 | + Plane { | ||
| 3488 | + source, | ||
| 3489 | + reconstruction: vec![128; source.len()], | ||
| 3490 | + width, | ||
| 3491 | + height, | ||
| 3492 | + true_width, | ||
| 3493 | + true_height, | ||
| 3494 | + tile_x0: 0, | ||
| 3495 | + tile_y0: 0, | ||
| 3496 | + tile_x1: width, | ||
| 3497 | + tile_y1: height, | ||
| 3498 | + } | ||
| 3499 | +} | ||
| 3500 | + | ||
| 3501 | +/// Copies the rectangle `[x0, x1) x [y0, y1)` of a tile job's own plane into | ||
| 3502 | +/// the frame's plane, through the [`crate::par::Shared`] every job holds. | ||
| 3503 | +/// | ||
| 3504 | +/// corner-cut: the disjointness that makes the aliasing sound is the caller's | ||
| 3505 | +/// -- `x0..x1`/`y0..y1` are the job's own `TileRect`, and a frame's tile | ||
| 3506 | +/// rectangles partition it -- checked end-to-end by | ||
| 3507 | +/// `tile_bytes_do_not_depend_on_the_thread_count` rather than by the borrow | ||
| 3508 | +/// checker, exactly the contract `Shared`'s own doc names. Upgrade path is | ||
| 3509 | +/// row-range `split_at_mut` plumbing through `Plane`, which would have to | ||
| 3510 | +/// thread an origin offset through every prediction and transform call site | ||
| 3511 | +/// for the same bytes. | ||
| 3512 | +#[allow(unsafe_code)] | ||
| 3513 | +fn copy_rect( | ||
| 3514 | + dst: crate::par::Shared<'_, Vec<u8>>, | ||
| 3515 | + src: &[u8], | ||
| 3516 | + stride: usize, | ||
| 3517 | + x0: usize, | ||
| 3518 | + y0: usize, | ||
| 3519 | + x1: usize, | ||
| 3520 | + y1: usize, | ||
| 3521 | +) { | ||
| 3522 | + // SAFETY: this job touches only its own tile rectangle (see above). | ||
| 3523 | + let dst = unsafe { dst.get() }; | ||
| 3524 | + for y in y0..y1 { | ||
| 3525 | + let (a, b) = (y * stride + x0, y * stride + x1); | ||
| 3526 | + dst[a..b].copy_from_slice(&src[a..b]); | ||
| 3527 | + } | ||
| 3528 | +} | ||
| 3529 | + | ||
| 3433 | /// The frame header's `tile_info` (spec 5.9.15) for `layout`: | 3530 | /// The frame header's `tile_info` (spec 5.9.15) for `layout`: |
| 3434 | /// `context_update_tile_id` names the largest tile, whose end-of-tile CDF | 3531 | /// `context_update_tile_id` names the largest tile, whose end-of-tile CDF |
| 3435 | /// tables the frame stores (spec 7.20), which is what | 3532 | /// tables the frame stores (spec 7.20), which is what |
| @@ -3697,8 +3794,6 @@ pub(crate) fn encode_key_frame_inner( | |||
| 3697 | // symbol the writer will actually write. | 3794 | // symbol the writer will actually write. |
| 3698 | // The bookkeeping is kept on the 16x16 grid the tile writer keeps it on, | 3795 | // The bookkeeping is kept on the 16x16 grid the tile writer keeps it on, |
| 3699 | // because a 32x32 block may be split into four 16x16 ones. | 3796 | // because a 32x32 block may be split into four 16x16 ones. |
| 3700 | - let mut above_mode = vec![DC_PRED; cols * 2]; | ||
| 3701 | - let mut left_mode = vec![DC_PRED; rows * 2]; | ||
| 3702 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | 3797 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 3703 | // This frame's tile grid (one tile unless the facade armed more). | 3798 | // This frame's tile grid (one tile unless the facade armed more). |
| 3704 | let layout = | 3799 | let layout = |
| @@ -3709,15 +3804,36 @@ pub(crate) fn encode_key_frame_inner( | |||
| 3709 | // `mi_row_starts`), which is what makes the writer's rects and the | 3804 | // `mi_row_starts`), which is what makes the writer's rects and the |
| 3710 | // reader's the same rects. | 3805 | // reader's the same rects. |
| 3711 | let header_tile_info = header.tile_info.clone(); | 3806 | let header_tile_info = header.tile_info.clone(); |
| 3712 | - let mut superblocks = Vec::with_capacity(sb_cols * sb_rows); | 3807 | + // lane-av1tsearch: the search runs once per TILE (`search_tiles`), not |
| 3713 | - let mut modes = Vec::with_capacity(cols * rows); | 3808 | + // once over the whole frame. Each tile job builds its own reconstruction |
| 3714 | - for sb_row in 0..sb_rows { | 3809 | + // planes and its own `above_mode`/`left_mode` bands, which is also what |
| 3715 | - for sb_col in 0..sb_cols { | 3810 | + // the tile writer reads -- a tile resets its neighbour contexts (spec |
| 3716 | - clip_planes_to_tile( | 3811 | + // 5.11.1), so a block at a tile's left edge must cost its mode against |
| 3717 | - &mut luma, | 3812 | + // the default context, not against whatever the tile to its west left in |
| 3718 | - &mut chroma, | 3813 | + // that row. At one tile the bands are the frame's, exactly as before. |
| 3719 | - layout.rect(layout.index_of_sb(sb_row as u32, sb_col as u32)), | 3814 | + let (frame_width, frame_height) = (picture.width, picture.height); |
| 3720 | - ); | 3815 | + let luma_out = crate::par::Shared::new(&mut luma.reconstruction); |
| 3816 | + let [cb, cr] = &mut chroma; | ||
| 3817 | + let chroma_out = [ | ||
| 3818 | + crate::par::Shared::new(&mut cb.reconstruction), | ||
| 3819 | + crate::par::Shared::new(&mut cr.reconstruction), | ||
| 3820 | + ]; | ||
| 3821 | + let search_tile = |index: usize, | ||
| 3822 | + fctx: &crate::decode::FrameCtx| | ||
| 3823 | + -> Result<Vec<(usize, Superblock, Vec<u8>)>> { | ||
| 3824 | + let rect = layout.rect(index); | ||
| 3825 | + let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); | ||
| 3826 | + let mut chroma = [ | ||
| 3827 | + fresh_plane(&picture_u8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), | ||
| 3828 | + fresh_plane(&picture_v8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), | ||
| 3829 | + ]; | ||
| 3830 | + clip_planes_to_tile(&mut luma, &mut chroma, rect); | ||
| 3831 | + let mut above_mode = vec![DC_PRED; cols * 2]; | ||
| 3832 | + let mut left_mode = vec![DC_PRED; rows * 2]; | ||
| 3833 | + let mut coded: Vec<(usize, Superblock, Vec<u8>)> = Vec::new(); | ||
| 3834 | + for sb_row in rect.sb_row0 as usize..rect.sb_row1 as usize { | ||
| 3835 | + for sb_col in rect.sb_col0 as usize..rect.sb_col1 as usize { | ||
| 3836 | + let mut modes = Vec::with_capacity(4); | ||
| 3721 | // The quadrants of a superblock are coded in the order the decoder | 3837 | // The quadrants of a superblock are coded in the order the decoder |
| 3722 | // walks them, which for a 64x64 split into 32x32 blocks is raster | 3838 | // walks them, which for a 64x64 split into 32x32 blocks is raster |
| 3723 | // order among the quadrants that are inside the frame. | 3839 | // order among the quadrants that are inside the frame. |
| @@ -3901,8 +4017,39 @@ pub(crate) fn encode_key_frame_inner( | |||
| 3901 | blocks.push(Quadrant::Whole(whole)); | 4017 | blocks.push(Quadrant::Whole(whole)); |
| 3902 | } | 4018 | } |
| 3903 | } | 4019 | } |
| 3904 | - superblocks.push(Superblock::Split(blocks)); | 4020 | + coded.push((sb_row * sb_cols + sb_col, Superblock::Split(blocks), modes)); |
| 4021 | + } | ||
| 4022 | + } | ||
| 4023 | + let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); | ||
| 4024 | + let (x1, y1) = (rect.mi_col1 as usize * 4, rect.mi_row1 as usize * 4); | ||
| 4025 | + copy_rect(luma_out, &luma.reconstruction, frame_width, x0, y0, x1, y1); | ||
| 4026 | + for (&out, tile) in chroma_out.iter().zip(&chroma) { | ||
| 4027 | + copy_rect( | ||
| 4028 | + out, | ||
| 4029 | + &tile.reconstruction, | ||
| 4030 | + frame_width / 2, | ||
| 4031 | + x0 / 2, | ||
| 4032 | + y0 / 2, | ||
| 4033 | + x1.div_ceil(2), | ||
| 4034 | + y1.div_ceil(2), | ||
| 4035 | + ); | ||
| 3905 | } | 4036 | } |
| 4037 | + Ok(coded) | ||
| 4038 | + }; | ||
| 4039 | + let searched = search_tiles(layout.count(), fctx, search_tile)?; | ||
| 4040 | + // Back into frame raster order: the tile writer indexes `superblocks` by | ||
| 4041 | + // the frame's own superblock index, and `modes` is the coding order the | ||
| 4042 | + // ablation drivers read. | ||
| 4043 | + let mut in_order: Vec<Option<(Superblock, Vec<u8>)>> = (0..sb_cols * sb_rows).map(|_| None).collect(); | ||
| 4044 | + for (at, superblock, block_modes) in searched.into_iter().flatten() { | ||
| 4045 | + in_order[at] = Some((superblock, block_modes)); | ||
| 4046 | + } | ||
| 4047 | + let mut superblocks = Vec::with_capacity(sb_cols * sb_rows); | ||
| 4048 | + let mut modes = Vec::with_capacity(cols * rows); | ||
| 4049 | + for slot in in_order { | ||
| 4050 | + let (superblock, block_modes) = slot.expect("every superblock belongs to a tile"); | ||
| 4051 | + superblocks.push(superblock); | ||
| 4052 | + modes.extend_from_slice(&block_modes); | ||
| 3906 | } | 4053 | } |
| 3907 | 4054 | ||
| 3908 | #[cfg(test)] | 4055 | #[cfg(test)] |
| @@ -5348,7 +5495,7 @@ pub(crate) fn encode_inter_frame( | |||
| 5348 | // whenever the true size is not a 64x64 multiple. | 5495 | // whenever the true size is not a 64x64 multiple. |
| 5349 | let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); | 5496 | let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); |
| 5350 | let (cols, rows) = (cols as usize, rows as usize); | 5497 | let (cols, rows) = (cols as usize, rows as usize); |
| 5351 | - let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | 5498 | + let (sb_cols, _sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 5352 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); | 5499 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); |
| 5353 | // This frame's tile grid, as in `encode_key_frame_inner`. | 5500 | // This frame's tile grid, as in `encode_key_frame_inner`. |
| 5354 | let (tile_cols_log2, tile_rows_log2) = armed_tiles(); | 5501 | let (tile_cols_log2, tile_rows_log2) = armed_tiles(); |
| @@ -5360,24 +5507,42 @@ pub(crate) fn encode_inter_frame( | |||
| 5360 | // integer-`u8`-scale), so the DPB reference plane is narrowed for it -- | 5507 | // integer-`u8`-scale), so the DPB reference plane is narrowed for it -- |
| 5361 | // once per frame (lane-av1rd2), not once per block, which is a whole | 5508 | // once per frame (lane-av1rd2), not once per block, which is a whole |
| 5362 | // plane's map per 32x32 block and now per leaf too. | 5509 | // plane's map per 32x32 block and now per leaf too. |
| 5363 | - let mut grid = MiGrid::new(mi_cols, mi_rows); | ||
| 5364 | - grid.set_sign_bias(sign_bias); | ||
| 5365 | let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; | 5510 | let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; |
| 5366 | - | 5511 | + // lane-av1tsearch: one search job per TILE (`search_tiles`). Neighbour |
| 5367 | - for sb_r in 0..sb_rows { | 5512 | + // availability -- intra edges and the MV stack's scans alike -- already |
| 5368 | - for sb_c in 0..sb_cols { | 5513 | + // stopped at the superblock's own tile (`PlaneBuf::set_tile_origin`, |
| 5369 | - // Neighbour availability -- intra edges and the MV stack's | 5514 | + // `MiGrid::set_tile_bounds`), so a tile reads nothing another tile wrote; |
| 5370 | - // scans alike -- stops at this superblock's own tile, exactly | 5515 | + // giving each job its own planes and its own `MiGrid` therefore leaves |
| 5371 | - // where the decoder stops it (`PlaneBuf::set_tile_origin`, | 5516 | + // every decision, and so every coded bit, exactly where the whole-frame |
| 5372 | - // `MiGrid::set_tile_bounds`). | 5517 | + // loop left it, at any tile count and any thread count. |
| 5373 | - let rect = layout.rect(layout.index_of_sb(sb_r as u32, sb_c as u32)); | 5518 | + let (frame_width, frame_height) = (picture.width, picture.height); |
| 5374 | - clip_planes_to_tile(&mut luma, &mut chroma, rect); | 5519 | + let luma_out = crate::par::Shared::new(&mut luma.reconstruction); |
| 5375 | - grid.set_tile_bounds( | 5520 | + let [cb, cr] = &mut chroma; |
| 5376 | - rect.mi_row0 as usize, | 5521 | + let chroma_out = [ |
| 5377 | - rect.mi_col0 as usize, | 5522 | + crate::par::Shared::new(&mut cb.reconstruction), |
| 5378 | - rect.mi_row1 as usize, | 5523 | + crate::par::Shared::new(&mut cr.reconstruction), |
| 5379 | - rect.mi_col1 as usize, | 5524 | + ]; |
| 5380 | - ); | 5525 | + let search_tile = |index: usize, |
| 5526 | + fctx: &crate::decode::FrameCtx| | ||
| 5527 | + -> Result<Vec<(usize, Quadrant)>> { | ||
| 5528 | + let rect = layout.rect(index); | ||
| 5529 | + let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); | ||
| 5530 | + let mut chroma = [ | ||
| 5531 | + fresh_plane(&picture_u8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), | ||
| 5532 | + fresh_plane(&picture_v8, frame_width / 2, frame_height / 2, true_width / 2, true_height / 2), | ||
| 5533 | + ]; | ||
| 5534 | + clip_planes_to_tile(&mut luma, &mut chroma, rect); | ||
| 5535 | + let mut grid = MiGrid::new(mi_cols, mi_rows); | ||
| 5536 | + grid.set_sign_bias(sign_bias); | ||
| 5537 | + grid.set_tile_bounds( | ||
| 5538 | + rect.mi_row0 as usize, | ||
| 5539 | + rect.mi_col0 as usize, | ||
| 5540 | + rect.mi_row1 as usize, | ||
| 5541 | + rect.mi_col1 as usize, | ||
| 5542 | + ); | ||
| 5543 | + let mut blocks: Vec<(usize, Quadrant)> = Vec::new(); | ||
| 5544 | + for sb_r in rect.sb_row0 as usize..rect.sb_row1 as usize { | ||
| 5545 | + for sb_c in rect.sb_col0 as usize..rect.sb_col1 as usize { | ||
| 5381 | for quadrant in 0..4 { | 5546 | for quadrant in 0..4 { |
| 5382 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | 5547 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 5383 | // Same filter as the tile writer's: a quadrant whose own mi | 5548 | // Same filter as the tile writer's: a quadrant whose own mi |
| @@ -5602,14 +5767,14 @@ pub(crate) fn encode_inter_frame( | |||
| 5602 | // grid, and their reconstruction is what the | 5767 | // grid, and their reconstruction is what the |
| 5603 | // planes hold. | 5768 | // planes hold. |
| 5604 | partition_hit(1); | 5769 | partition_hit(1); |
| 5605 | - blocks[r32 * cols + c32] = Quadrant::Split(leaves); | 5770 | + blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); |
| 5606 | continue; | 5771 | continue; |
| 5607 | } | 5772 | } |
| 5608 | restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); | 5773 | restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); |
| 5609 | } | 5774 | } |
| 5610 | partition_hit(0); | 5775 | partition_hit(0); |
| 5611 | record_mi(&mut grid, mi_row, mi_col, 8, block.inter); | 5776 | record_mi(&mut grid, mi_row, mi_col, 8, block.inter); |
| 5612 | - blocks[r32 * cols + c32] = Quadrant::Whole(block); | 5777 | + blocks.push((r32 * cols + c32, Quadrant::Whole(block))); |
| 5613 | } else { | 5778 | } else { |
| 5614 | // The sub-positions actually inside the true frame, same | 5779 | // The sub-positions actually inside the true frame, same |
| 5615 | // filter as `sb_coeff_inter_frame_tile`'s `sub_positions`. | 5780 | // filter as `sb_coeff_inter_frame_tile`'s `sub_positions`. |
| @@ -5774,11 +5939,33 @@ pub(crate) fn encode_inter_frame( | |||
| 5774 | }); | 5939 | }); |
| 5775 | } | 5940 | } |
| 5776 | } | 5941 | } |
| 5777 | - blocks[r32 * cols + c32] = Quadrant::Split(leaves); | 5942 | + blocks.push((r32 * cols + c32, Quadrant::Split(leaves))); |
| 5778 | } | 5943 | } |
| 5779 | } | 5944 | } |
| 5780 | } | 5945 | } |
| 5781 | } | 5946 | } |
| 5947 | + let (x0, y0) = (rect.mi_col0 as usize * 4, rect.mi_row0 as usize * 4); | ||
| 5948 | + let (x1, y1) = (rect.mi_col1 as usize * 4, rect.mi_row1 as usize * 4); | ||
| 5949 | + copy_rect(luma_out, &luma.reconstruction, frame_width, x0, y0, x1, y1); | ||
| 5950 | + for (&out, tile) in chroma_out.iter().zip(&chroma) { | ||
| 5951 | + copy_rect( | ||
| 5952 | + out, | ||
| 5953 | + &tile.reconstruction, | ||
| 5954 | + frame_width / 2, | ||
| 5955 | + x0 / 2, | ||
| 5956 | + y0 / 2, | ||
| 5957 | + x1.div_ceil(2), | ||
| 5958 | + y1.div_ceil(2), | ||
| 5959 | + ); | ||
| 5960 | + } | ||
| 5961 | + Ok(blocks) | ||
| 5962 | + }; | ||
| 5963 | + for (at, quadrant) in search_tiles(layout.count(), fctx, search_tile)? | ||
| 5964 | + .into_iter() | ||
| 5965 | + .flatten() | ||
| 5966 | + { | ||
| 5967 | + blocks[at] = quadrant; | ||
| 5968 | + } | ||
| 5782 | 5969 | ||
| 5783 | let modes = blocks | 5970 | let modes = blocks |
| 5784 | .iter() | 5971 | .iter() |
crates/ec-av1/src/encoder.rsmodified+165 -0
| @@ -1839,6 +1839,171 @@ mod tests { | |||
| 1839 | crate::par::set_tile_threads(1); | 1839 | crate::par::set_tile_threads(1); |
| 1840 | } | 1840 | } |
| 1841 | 1841 | ||
| 1842 | + /// What the per-tile search (lane-av1tsearch) is worth in wall and in | ||
| 1843 | + /// frames per second at the editor's own export sizes: every tile layout | ||
| 1844 | + /// crossed with every tile-thread count, plus librav1e at the matching | ||
| 1845 | + /// `tile_cols`/`tile_rows`/`threads` as the reference point. Two passes | ||
| 1846 | + /// over the whole table (A,B,A,B... rather than AA,BB) with the best of | ||
| 1847 | + /// the two kept, so a thermal drift cannot order the rows. | ||
| 1848 | + /// | ||
| 1849 | + /// Measurement, not a pass/fail gate: it asserts only that every cell | ||
| 1850 | + /// produced a stream. | ||
| 1851 | + #[test] | ||
| 1852 | + #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] | ||
| 1853 | + fn tile_search_wall_1080p() { | ||
| 1854 | + tile_search_wall(1920, 1080, 12, &[(0, 0), (1, 0), (1, 1), (2, 1)], &[1, 2, 4, 8]); | ||
| 1855 | + } | ||
| 1856 | + | ||
| 1857 | + /// The same table at the 4K frame size the editor exports (3840x1608), | ||
| 1858 | + /// at the two layouts with enough tiles to fill this box. | ||
| 1859 | + #[test] | ||
| 1860 | + #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] | ||
| 1861 | + fn tile_search_wall_4k() { | ||
| 1862 | + tile_search_wall(3840, 1608, 6, &[(2, 1), (3, 2)], &[1, 8, 12]); | ||
| 1863 | + } | ||
| 1864 | + | ||
| 1865 | + fn tile_search_wall( | ||
| 1866 | + width: usize, | ||
| 1867 | + height: usize, | ||
| 1868 | + frames: usize, | ||
| 1869 | + layouts: &[(u32, u32)], | ||
| 1870 | + threads: &[usize], | ||
| 1871 | + ) { | ||
| 1872 | + let _gate_lock = crate::stream::tests::lock_gate_counters(); | ||
| 1873 | + let Some(sources) = h264_clip_frames(width, height, frames) else { | ||
| 1874 | + eprintln!("SKIP the tile-search wall table: no fixture or no ffmpeg"); | ||
| 1875 | + return; | ||
| 1876 | + }; | ||
| 1877 | + let run = |cols_log2: u32, rows_log2: u32, t: usize| -> (std::time::Duration, usize) { | ||
| 1878 | + crate::par::set_tile_threads(t); | ||
| 1879 | + let config = EncoderConfig { | ||
| 1880 | + width, | ||
| 1881 | + height, | ||
| 1882 | + base_q_idx: 120, | ||
| 1883 | + gop: frames, | ||
| 1884 | + colour: Colour::Bt709Limited, | ||
| 1885 | + tile_cols_log2: cols_log2, | ||
| 1886 | + tile_rows_log2: rows_log2, | ||
| 1887 | + }; | ||
| 1888 | + let mut enc = Av1Encoder::new(config).unwrap(); | ||
| 1889 | + let start = std::time::Instant::now(); | ||
| 1890 | + let mut bytes = 0usize; | ||
| 1891 | + for picture in &sources { | ||
| 1892 | + bytes += enc.encode(picture).unwrap().data.len(); | ||
| 1893 | + } | ||
| 1894 | + (start.elapsed(), bytes) | ||
| 1895 | + }; | ||
| 1896 | + let cells: Vec<((u32, u32), usize)> = layouts | ||
| 1897 | + .iter() | ||
| 1898 | + .flat_map(|&l| threads.iter().map(move |&t| (l, t))) | ||
| 1899 | + .collect(); | ||
| 1900 | + let mut best: Vec<Option<(std::time::Duration, usize)>> = vec![None; cells.len()]; | ||
| 1901 | + for _pass in 0..2 { | ||
| 1902 | + for (i, &((c, r), t)) in cells.iter().enumerate() { | ||
| 1903 | + let got = run(c, r, t); | ||
| 1904 | + assert!(got.1 > 0, "{}x{} tiles at {t} threads: empty stream", 1 << c, 1 << r); | ||
| 1905 | + if best[i].is_none_or(|(w, _)| got.0 < w) { | ||
| 1906 | + best[i] = Some(got); | ||
| 1907 | + } | ||
| 1908 | + } | ||
| 1909 | + } | ||
| 1910 | + crate::par::set_tile_threads(1); | ||
| 1911 | + eprintln!("\n{width}x{height}, {frames} frames, base_q_idx 120 -- best of two passes"); | ||
| 1912 | + eprintln!("| layout | tiles | threads | wall | fps | speedup vs 1 tile 1 thread | bytes |"); | ||
| 1913 | + let base = best[0].expect("the first cell ran").0.as_secs_f64(); | ||
| 1914 | + for (i, &((c, r), t)) in cells.iter().enumerate() { | ||
| 1915 | + let (wall, bytes) = best[i].expect("every cell ran"); | ||
| 1916 | + let secs = wall.as_secs_f64(); | ||
| 1917 | + eprintln!( | ||
| 1918 | + "| {}x{} | {} | {t} | {secs:.2}s | {:.2} | {:.2}x | {bytes} |", | ||
| 1919 | + 1 << c, | ||
| 1920 | + 1 << r, | ||
| 1921 | + 1 << (c + r), | ||
| 1922 | + frames as f64 / secs, | ||
| 1923 | + base / secs, | ||
| 1924 | + ); | ||
| 1925 | + } | ||
| 1926 | + // Parallel efficiency per layout: what the extra threads actually | ||
| 1927 | + // took off that layout's own single-threaded wall, and so what share | ||
| 1928 | + // of the added cores sat idle. | ||
| 1929 | + for &(c, r) in layouts { | ||
| 1930 | + let one = cells | ||
| 1931 | + .iter() | ||
| 1932 | + .position(|&(l, t)| l == (c, r) && t == threads[0]) | ||
| 1933 | + .map(|i| best[i].expect("cell").0.as_secs_f64()); | ||
| 1934 | + let Some(one) = one else { continue }; | ||
| 1935 | + for &t in &threads[1..] { | ||
| 1936 | + let i = cells.iter().position(|&(l, tt)| l == (c, r) && tt == t).expect("cell"); | ||
| 1937 | + let secs = best[i].expect("cell").0.as_secs_f64(); | ||
| 1938 | + let workers = (t.min(1 << (c + r))) as f64 / threads[0] as f64; | ||
| 1939 | + let speedup = one / secs; | ||
| 1940 | + eprintln!( | ||
| 1941 | + "{}x{} tiles, {} -> {t} threads: {speedup:.2}x of a possible {workers:.2}x, \ | ||
| 1942 | + idle share {:.0}%", | ||
| 1943 | + 1 << c, | ||
| 1944 | + 1 << r, | ||
| 1945 | + threads[0], | ||
| 1946 | + 100.0 * (1.0 - speedup / workers).max(0.0), | ||
| 1947 | + ); | ||
| 1948 | + } | ||
| 1949 | + } | ||
| 1950 | + rav1e_wall_reference(width, height, frames, layouts, threads); | ||
| 1951 | + } | ||
| 1952 | + | ||
| 1953 | + /// librav1e at the same sizes, layouts and thread counts, through ffmpeg | ||
| 1954 | + /// -- the reference the wall table is read against. | ||
| 1955 | + fn rav1e_wall_reference( | ||
| 1956 | + width: usize, | ||
| 1957 | + height: usize, | ||
| 1958 | + frames: usize, | ||
| 1959 | + layouts: &[(u32, u32)], | ||
| 1960 | + threads: &[usize], | ||
| 1961 | + ) { | ||
| 1962 | + let clip = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| 1963 | + .join("../../fixtures/video/h264-1080p-23.976-8bit.mp4"); | ||
| 1964 | + let listed = Command::new("ffmpeg").args(["-hide_banner", "-encoders"]).output(); | ||
| 1965 | + let has_rav1e = listed | ||
| 1966 | + .map(|o| String::from_utf8_lossy(&o.stdout).contains("librav1e")) | ||
| 1967 | + .unwrap_or(false); | ||
| 1968 | + if !has_rav1e || !clip.exists() { | ||
| 1969 | + eprintln!("SKIP the rav1e reference: no librav1e or no fixture"); | ||
| 1970 | + return; | ||
| 1971 | + } | ||
| 1972 | + eprintln!("| rav1e layout | threads | wall | fps | bytes |"); | ||
| 1973 | + for &(c, r) in layouts { | ||
| 1974 | + for &t in threads { | ||
| 1975 | + let out = std::env::temp_dir().join(format!("ec-av1-rav1e-{}.ivf", std::process::id())); | ||
| 1976 | + let params = format!( | ||
| 1977 | + "speed=6:quantizer=100:tile_cols={}:tile_rows={}:threads={t}", | ||
| 1978 | + 1 << c, | ||
| 1979 | + 1 << r | ||
| 1980 | + ); | ||
| 1981 | + let start = std::time::Instant::now(); | ||
| 1982 | + let status = Command::new("ffmpeg") | ||
| 1983 | + .args(["-v", "error", "-y", "-i", clip.to_str().unwrap()]) | ||
| 1984 | + .args(["-frames:v", &frames.to_string()]) | ||
| 1985 | + .args(["-vf", &format!("scale={width}:{height}")]) | ||
| 1986 | + .args(["-c:v", "librav1e", "-rav1e-params", ¶ms]) | ||
| 1987 | + .arg(out.to_str().unwrap()) | ||
| 1988 | + .output() | ||
| 1989 | + .expect("ffmpeg failed to run"); | ||
| 1990 | + let secs = start.elapsed().as_secs_f64(); | ||
| 1991 | + if !status.status.success() { | ||
| 1992 | + eprintln!("| {}x{} | {t} | FAILED: {} |", 1 << c, 1 << r, String::from_utf8_lossy(&status.stderr).lines().last().unwrap_or("")); | ||
| 1993 | + continue; | ||
| 1994 | + } | ||
| 1995 | + let bytes = std::fs::metadata(&out).map(|m| m.len()).unwrap_or(0); | ||
| 1996 | + let _ = std::fs::remove_file(&out); | ||
| 1997 | + eprintln!( | ||
| 1998 | + "| {}x{} | {t} | {secs:.2}s | {:.2} | {bytes} |", | ||
| 1999 | + 1 << c, | ||
| 2000 | + 1 << r, | ||
| 2001 | + frames as f64 / secs | ||
| 2002 | + ); | ||
| 2003 | + } | ||
| 2004 | + } | ||
| 2005 | + } | ||
| 2006 | + | ||
| 1842 | /// The same round trip at a real 1920x1080 crop of the gate's own clip | 2007 | /// The same round trip at a real 1920x1080 crop of the gate's own clip |
| 1843 | /// -- the size the editor's export actually runs at, where a tile grid | 2008 | /// -- the size the editor's export actually runs at, where a tile grid |
| 1844 | /// is worth having. Ignored by default only for its wall (a 1080p | 2009 | /// is worth having. Ignored by default only for its wall (a 1080p |
crates/ec-av1/src/par.rsmodified+14 -4
| @@ -56,11 +56,21 @@ pub(crate) fn set_filter_threads(n: usize) { | |||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | /// `EC_AV1_TILE_THREADS`, how many workers the ENCODER spreads a frame's | 58 | /// `EC_AV1_TILE_THREADS`, how many workers the ENCODER spreads a frame's |
| 59 | -/// tiles across (default 1 = the shipped single-threaded write). Tiles are | 59 | +/// tiles across -- both the RD/motion SEARCH (`encode::search_tiles`) and the |
| 60 | -/// entropy-independent by construction (each starts from the frame's own | 60 | +/// entropy WRITE (`encode::write_tiles`); default 1 = the shipped |
| 61 | -/// tables and resets its own contexts), so the bytes a frame comes out as do | 61 | +/// single-threaded encode. Tiles are independent by construction (each starts |
| 62 | -/// not depend on this -- `encoder::tests::tile_bytes_do_not_depend_on_the_ | 62 | +/// from the frame's own tables, resets its own contexts, and reads no sample |
| 63 | +/// and no mode outside its own rectangle), so the bytes a frame comes out as | ||
| 64 | +/// do not depend on this -- `encoder::tests::tile_bytes_do_not_depend_on_the_ | ||
| 63 | /// thread_count` is the pin. | 65 | /// thread_count` is the pin. |
| 66 | +/// | ||
| 67 | +/// Measured (lane-av1tsearch, 12-core box, best of two interleaved passes): | ||
| 68 | +/// 1920x1080, 12 frames -- 11.49 s at one tile (any thread count), 6.98 s at | ||
| 69 | +/// 4x2 tiles and 8 threads (1.65x, 1.04 -> 1.72 fps); 3840x1608, 6 frames -- | ||
| 70 | +/// 17.12 s at 4x2 tiles and one thread, 8.64 s at 8 (1.98x, 0.35 -> 0.69 | ||
| 71 | +/// fps). Past ~8 tiles the extra tiles cost bytes without buying wall: what | ||
| 72 | +/// is left is the per-frame serial tail (the filter search re-decodes the | ||
| 73 | +/// whole frame), ~43% of an inter frame by Amdahl on those numbers. | ||
| 64 | static TILE_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | 74 | static TILE_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 65 | 75 | ||
| 66 | pub(crate) fn tile_threads() -> usize { | 76 | pub(crate) fn tile_threads() -> usize { |
crates/ec-av1/src/tile.rsmodified+0 -9
| @@ -238,15 +238,6 @@ impl TileLayout { | |||
| 238 | } | 238 | } |
| 239 | } | 239 | } |
| 240 | 240 | ||
| 241 | - /// Which tile the superblock at `(sb_r, sb_c)` belongs to -- what the | ||
| 242 | - /// encoder's frame-raster search consults to clip a block's neighbour | ||
| 243 | - /// availability to its own tile. | ||
| 244 | - pub(crate) fn index_of_sb(&self, sb_r: u32, sb_c: u32) -> usize { | ||
| 245 | - let col = self.col_starts_sb[1..].partition_point(|&s| s <= sb_c); | ||
| 246 | - let row = self.row_starts_sb[1..].partition_point(|&s| s <= sb_r); | ||
| 247 | - row * self.cols() as usize + col | ||
| 248 | - } | ||
| 249 | - | ||
| 250 | /// The largest tile by mi area, `context_update_tile_id`'s own rule | 241 | /// The largest tile by mi area, `context_update_tile_id`'s own rule |
| 251 | /// (spec 6.8.14 leaves the choice to the encoder; libaom and rav1e both | 242 | /// (spec 6.8.14 leaves the choice to the encoder; libaom and rav1e both |
| 252 | /// name the biggest tile, whose end-of-tile tables the frame stores). | 243 | /// name the biggest tile, whose end-of-tile tables the frame stores). |