ec-av1: clip a lossless block's TX grid at the frame edge
Browse files at this commit589db238f942dff20f3c5c0552bd1b7b4ea7e5c4Parent: a460dca2 changed files +197 -6
- crates/ec-av1/src/decode.rs +75 -6
- crates/ec-av1/src/stream.rs +122 -0
crates/ec-av1/src/decode.rsmodified+75 -6
| @@ -27565,9 +27565,37 @@ fn read_block_tx_size( | ||
| 27565 | 27565 | // `read_block_tx_size` (`decodeframe.c`) sends a lossless block down the |
| 27566 | 27566 | // `read_tx_size` arm, which returns TX_4X4 before it reads anything. |
| 27567 | 27567 | if lossless(fctx) { |
| 27568 | - let mut leaves = Vec::new(); | |
| 27569 | - for row in 0..side / MI { | |
| 27570 | - for col in 0..side / MI { | |
| 27568 | + // lane-av1lm444loss: libaom CLIPS the transform-unit grid at the frame | |
| 27569 | + // edge -- `max_block_high`/`max_block_wide` (`av1_common_int.h:1565`, | |
| 27570 | + // `:1579`) fold `mb_to_bottom_edge`/`mb_to_right_edge` into | |
| 27571 | + // `block_size_high/wide` before `>> MI_SIZE_LOG2`, so a block that | |
| 27572 | + // hangs off the bottom/right codes only the units INSIDE the frame. | |
| 27573 | + // `decode_token_recon_block`'s mu walk then bounds every chunk with | |
| 27574 | + // `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + row, | |
| 27575 | + // max_blocks_high), ss_y)`, so the out-of-frame units are never read. | |
| 27576 | + // | |
| 27577 | + // The unclipped `side / MI` grid is correct arithmetic for a block that | |
| 27578 | + // FITS, which is every block of a frame whose height and width are | |
| 27579 | + // multiples of the superblock -- so this reads identically on all of | |
| 27580 | + // those, and only differs on the partial edge block. Measured on the | |
| 27581 | + // 128x96 4:4:4 lossless pin (one 128 root per inter frame, frame 32 | |
| 27582 | + // mi = 24 tx rows tall): the unclipped grid read 32 rows where the | |
| 27583 | + // oracle read 24 -- 256 extra luma units per inter frame (1024 vs | |
| 27584 | + // the oracle's 768), the `EC_SYMR` ladder's first fork landing exactly | |
| 27585 | + // at that row-24 boundary (`plane=0` luma where the oracle had moved | |
| 27586 | + // to `plane=1`), and the pixel diff growing 5596 -> 33561 wrong | |
| 27587 | + // samples over the six frames. | |
| 27588 | + let side_mi = side / MI; | |
| 27589 | + let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); | |
| 27590 | + let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); | |
| 27591 | + if max_w_mi != side_mi || max_h_mi != side_mi { | |
| 27592 | + LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1)); | |
| 27593 | + LOSSLESS_EDGE_CLIP_UNITS | |
| 27594 | + .with(|c| c.set(c.get() + (side_mi * side_mi - max_w_mi * max_h_mi))); | |
| 27595 | + } | |
| 27596 | + let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi); | |
| 27597 | + for row in 0..max_h_mi { | |
| 27598 | + for col in 0..max_w_mi { | |
| 27571 | 27599 | leaves.push((row, col, 4, 4)); |
| 27572 | 27600 | } |
| 27573 | 27601 | } |
| @@ -28047,9 +28075,24 @@ fn read_block_tx_size_rect( | ||
| 28047 | 28075 | fctx: &crate::decode::FrameCtx, |
| 28048 | 28076 | ) -> Result<Option<Vec<(usize, usize, usize, usize)>>> { |
| 28049 | 28077 | if lossless(fctx) { |
| 28050 | - let mut leaves = Vec::new(); | |
| 28051 | - for row in 0..bh / MI { | |
| 28052 | - for col in 0..bw / MI { | |
| 28078 | + // lane-av1lm444loss: the same frame-edge clip as the square builder | |
| 28079 | + // above -- `max_block_high`/`max_block_wide` bound a rect block's | |
| 28080 | + // transform grid by the part of it inside the frame, so a 1:4 strip | |
| 28081 | + // hanging off the bottom codes only its in-frame rows. Identical | |
| 28082 | + // arithmetic for a block that fits. | |
| 28083 | + let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(at_mi.1)); | |
| 28084 | + let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(at_mi.0)); | |
| 28085 | + let full_w_mi = bw / MI; | |
| 28086 | + let full_h_mi = bh / MI; | |
| 28087 | + if max_w_mi != full_w_mi || max_h_mi != full_h_mi { | |
| 28088 | + LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1)); | |
| 28089 | + LOSSLESS_EDGE_CLIP_UNITS.with(|c| { | |
| 28090 | + c.set(c.get() + (full_w_mi * full_h_mi - max_w_mi * max_h_mi)) | |
| 28091 | + }); | |
| 28092 | + } | |
| 28093 | + let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi); | |
| 28094 | + for row in 0..max_h_mi { | |
| 28095 | + for col in 0..max_w_mi { | |
| 28053 | 28096 | leaves.push((row, col, 4, 4)); |
| 28054 | 28097 | } |
| 28055 | 28098 | } |
| @@ -32310,6 +32353,19 @@ thread_local! { | ||
| 32310 | 32353 | /// 128x96 4:4:4 lossless stream, 1536 per inter frame (768 per plane), |
| 32311 | 32354 | /// against the oracle's 1536. |
| 32312 | 32355 | pub(crate) static MU_CHUNK_WALK_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 32356 | + /// lane-av1lm444loss: lossless blocks whose transform-unit grid was | |
| 32357 | + /// CLIPPED at the frame edge -- `max_block_high`/`max_block_wide` cut the | |
| 32358 | + /// grid short of the block's own `side / MI`, so libaom codes fewer TX_4X4 | |
| 32359 | + /// units than the block is wide/tall. Zero on any frame whose height and | |
| 32360 | + /// width are multiples of the superblock, so a gate cannot look armed by | |
| 32361 | + /// merely decoding. | |
| 32362 | + pub(crate) static LOSSLESS_EDGE_CLIPPED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 32363 | + /// lane-av1lm444loss: the transform units that clip REMOVED -- the | |
| 32364 | + /// `(side / MI - clipped) * (side / MI)` overflow the pre-fix walk read | |
| 32365 | + /// past the frame bottom/right edge. On the 128x96 4:4:4 lossless pin this | |
| 32366 | + /// is 256 per inter frame (8 unclipped luma rows x 32 columns), and 0 on a | |
| 32367 | + /// frame that fits. | |
| 32368 | + pub(crate) static LOSSLESS_EDGE_CLIP_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 32313 | 32369 | } |
| 32314 | 32370 | |
| 32315 | 32371 | /// [`MU_CHUNK_WALKS`], for a gate's own before/after delta. |
| @@ -32329,6 +32385,19 @@ pub(crate) fn mu_chunk_walk_units() -> usize { | ||
| 32329 | 32385 | pub(crate) fn mu_chunk_order_hits() -> usize { |
| 32330 | 32386 | MU_CHUNK_ORDER_HITS.with(std::cell::Cell::get) |
| 32331 | 32387 | } |
| 32388 | +/// [`LOSSLESS_EDGE_CLIPPED`] -- lossless blocks whose TX grid the frame-edge | |
| 32389 | +/// clip shortened, for a gate's own before/after delta. | |
| 32390 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 32391 | +pub(crate) fn lossless_edge_clipped() -> usize { | |
| 32392 | + LOSSLESS_EDGE_CLIPPED.with(std::cell::Cell::get) | |
| 32393 | +} | |
| 32394 | + | |
| 32395 | +/// [`LOSSLESS_EDGE_CLIP_UNITS`] -- transform units the clip removed, i.e. the | |
| 32396 | +/// ones the unclipped pre-fix walk read past the frame edge. | |
| 32397 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 32398 | +pub(crate) fn lossless_edge_clip_units() -> usize { | |
| 32399 | + LOSSLESS_EDGE_CLIP_UNITS.with(std::cell::Cell::get) | |
| 32400 | +} | |
| 32332 | 32401 | |
| 32333 | 32402 | /// [`CHROMA_SPLIT_TX_HITS`], for a gate's own before/after delta. |
| 32334 | 32403 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
crates/ec-av1/src/stream.rsmodified+122 -0
| @@ -6858,6 +6858,128 @@ pub(crate) mod tests { | ||
| 6858 | 6858 | } |
| 6859 | 6859 | } |
| 6860 | 6860 | |
| 6861 | + /// lane-av1lm444loss: a LOSSLESS block's transform-unit grid is clipped at | |
| 6862 | + /// the FRAME edge, exactly as libaom's `max_block_high`/`max_block_wide` | |
| 6863 | + /// clip it (`av1_common_int.h:1565`, `:1579` fold `mb_to_bottom_edge` / | |
| 6864 | + /// `mb_to_right_edge` into `block_size_high/wide` before `>> | |
| 6865 | + /// MI_SIZE_LOG2`), so a block hanging off the bottom/right codes only the | |
| 6866 | + /// units inside the frame. `decode_token_recon_block` then bounds every | |
| 6867 | + /// mu chunk with `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high | |
| 6868 | + /// + row, max_blocks_high), ss_y)`, so the overflow units are never read | |
| 6869 | + /// there either. | |
| 6870 | + /// | |
| 6871 | + /// Fixture is the sibling [`a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major`] | |
| 6872 | + /// pin -- 128x96 yuv444p 4:4:4 lossless, 6 frames, 63429 bytes -- because | |
| 6873 | + /// 96 is NOT a multiple of the 128 superblock: its five inter frames each | |
| 6874 | + /// carry one unsplit 128 root that overhangs the bottom by 32 px. That is | |
| 6875 | + /// the whole reason this stream is the witness. The frame is 24 mi tall, | |
| 6876 | + /// so `max_blocks_high >> 2` is 24 transform rows where the block's own | |
| 6877 | + /// `side / MI` is 32. | |
| 6878 | + /// | |
| 6879 | + /// The three assertions are all non-vacuous and all EXACT: | |
| 6880 | + /// | |
| 6881 | + /// 1. `lossless_edge_clipped()` fired -- 5, one per inter frame's 128 | |
| 6882 | + /// root. It is 0 on any frame whose height and width are multiples of | |
| 6883 | + /// the superblock, so a stream that merely decodes cannot satisfy it. | |
| 6884 | + /// 2. `lossless_edge_clip_units()` is 1280 = 5 x 256: the 8 transform rows | |
| 6885 | + /// x 32 columns the unclipped walk read past the frame bottom, per | |
| 6886 | + /// inter frame. The pre-fix walk read 2560 units per inter frame | |
| 6887 | + /// against the oracle's 2304, and this is that 256. | |
| 6888 | + /// 3. every sample of every frame matches the oracle `aomdec | |
| 6889 | + /// --rawvideo` in decode order -- all six frames, hidden alt-ref | |
| 6890 | + /// included, which the pre-fix tree failed from frame 1 on | |
| 6891 | + /// (5596 wrong samples there, growing to 33561 by frame 5). | |
| 6892 | + /// | |
| 6893 | + /// The control arm pins the other direction of the invariant: the | |
| 6894 | + /// `ll444_minp64_128root_control` stream has no overhanging root, so its | |
| 6895 | + /// clip counters must both read 0 -- the clip cannot fire on a block that | |
| 6896 | + /// fits, which is what keeps every other lossless pin byte-identical. | |
| 6897 | + #[test] | |
| 6898 | + fn a_lossless_block_clips_its_transform_grid_at_the_frame_edge() { | |
| 6899 | + const NAME: &str = "a_lossless_block_clips_its_transform_grid_at_the_frame_edge"; | |
| 6900 | + const W: usize = 128; | |
| 6901 | + const H: usize = 96; | |
| 6902 | + const FRAMES: usize = 6; | |
| 6903 | + /// 5 inter frames, one unsplit 128 root each; frame 0 splits. | |
| 6904 | + const ROOTS: usize = 5; | |
| 6905 | + /// `max_blocks_high >> 2` = 24 rows (96 px / 4), against the block's | |
| 6906 | + /// own `side / MI` = 32 -- so 8 rows x 32 columns = 256 units per root | |
| 6907 | + /// were read past the frame bottom before the clip. | |
| 6908 | + const CLIP_UNITS_PER_ROOT: usize = 256; | |
| 6909 | + | |
| 6910 | + let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 6911 | + .join("fixtures/ll444_128root_lossless.obu"); | |
| 6912 | + let stream = read_pin(&obu, 63429, 0xf07d_47fc_fd51_2658, NAME); | |
| 6913 | + let _guard = lock_gate_counters(); | |
| 6914 | + let (c0, u0) = ( | |
| 6915 | + crate::decode::lossless_edge_clipped(), | |
| 6916 | + crate::decode::lossless_edge_clip_units(), | |
| 6917 | + ); | |
| 6918 | + let frames = decode_stream(&stream) | |
| 6919 | + .unwrap_or_else(|e| panic!("{NAME}: the pinned stream no longer decodes cleanly: {e}")); | |
| 6920 | + let (c1, u1) = ( | |
| 6921 | + crate::decode::lossless_edge_clipped(), | |
| 6922 | + crate::decode::lossless_edge_clip_units(), | |
| 6923 | + ); | |
| 6924 | + assert_eq!( | |
| 6925 | + c1 - c0, | |
| 6926 | + ROOTS, | |
| 6927 | + "{NAME}: the frame-edge clip fired on {}/{} of the 128 roots -- it must be 0 \ | |
| 6928 | + on a frame that fits, so this is what makes the gate non-vacuous", | |
| 6929 | + c1 - c0, | |
| 6930 | + ROOTS | |
| 6931 | + ); | |
| 6932 | + assert_eq!( | |
| 6933 | + u1 - u0, | |
| 6934 | + ROOTS * CLIP_UNITS_PER_ROOT, | |
| 6935 | + "{NAME}: the clip removed {}/{} transform units; the unclipped walk read {} \ | |
| 6936 | + per inter frame past the 96 px frame bottom (8 rows x 32 columns)", | |
| 6937 | + u1 - u0, | |
| 6938 | + ROOTS * CLIP_UNITS_PER_ROOT, | |
| 6939 | + CLIP_UNITS_PER_ROOT | |
| 6940 | + ); | |
| 6941 | + assert_eq!(frames.len(), FRAMES, "{NAME}: frame count"); | |
| 6942 | + for f in &frames { | |
| 6943 | + assert_eq!((f.width, f.height), (W, H), "{NAME}: dimensions"); | |
| 6944 | + } | |
| 6945 | + if aomdec_path().is_file() { | |
| 6946 | + assert_rawvideo_matches(&obu, &stream, NAME, FRAMES); | |
| 6947 | + } else { | |
| 6948 | + eprintln!( | |
| 6949 | + "SKIP {NAME} aomdec arm: no oracle aomdec at {}", | |
| 6950 | + aomdec_path().display() | |
| 6951 | + ); | |
| 6952 | + } | |
| 6953 | + | |
| 6954 | + // Control: a stream whose roots all fit must not clip at all. | |
| 6955 | + let ctrl = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 6956 | + .join("fixtures/ll444_minp64_128root_control.obu"); | |
| 6957 | + let ctrl_stream = read_pin( | |
| 6958 | + &ctrl, | |
| 6959 | + 61494, | |
| 6960 | + 0xf6f5_a3cb_efc7_eb10, | |
| 6961 | + &format!("{NAME} control"), | |
| 6962 | + ); | |
| 6963 | + let (c2, u2) = ( | |
| 6964 | + crate::decode::lossless_edge_clipped(), | |
| 6965 | + crate::decode::lossless_edge_clip_units(), | |
| 6966 | + ); | |
| 6967 | + let _ = decode_stream(&ctrl_stream) | |
| 6968 | + .unwrap_or_else(|e| panic!("{NAME}: the control arm no longer decodes cleanly: {e}")); | |
| 6969 | + assert_eq!( | |
| 6970 | + crate::decode::lossless_edge_clipped() - c2, | |
| 6971 | + 0, | |
| 6972 | + "{NAME} control: the clip fired on a stream whose blocks all fit -- the clip \ | |
| 6973 | + must be a no-op there, which is what keeps every other lossless pin \ | |
| 6974 | + byte-identical" | |
| 6975 | + ); | |
| 6976 | + assert_eq!( | |
| 6977 | + crate::decode::lossless_edge_clip_units() - u2, | |
| 6978 | + 0, | |
| 6979 | + "{NAME} control: the clip removed units from a stream whose blocks all fit" | |
| 6980 | + ); | |
| 6981 | + } | |
| 6982 | + | |
| 6861 | 6983 | /// lane-av1llpredgate2: the debug-exact pin for the ss-aware |
| 6862 | 6984 | /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the |
| 6863 | 6985 | /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc2 |