forge
Explore
Log in

Don't you dare go Hollow

av1h5: read a 128 root's tokens CHUNK-major, not block-raster

Browse files at this commit

f93609f569560392a108194d7c0e5c38146a89f4Ahmet Kaan GümüşParent: 19967ac
libaom's decode_token_recon_block (decodeframe.c:972) walks 64x64 mu
chunks with the PLANE loop INSIDE the chunk loop. The var-tx leaf list
is a BLOCK-wide raster, so a chunk's members are not contiguous: leaf
0..15 is chunk (0,0) row 0, but chunk (0,0) rows 1..15 are leaves
32..47, 64..79, ... The chunk test on the given order therefore closed
the chunk every SIXTEEN leaves and coded each chunk's chroma sixteen
times -- 64 chroma walks per 128 root where libaom codes 4, and 24576
chroma units per inter frame against the oracle's 1536.

Reorder the leaf walk chunk-major at side > 64 (a no-op below 64, where
a block IS one chunk) at all four sites carrying this test: the 128x128
inter arm, the 128x64/64x128 inter arm, and the two intra arms.

Measured on the pinned 128x96 yuv444p10le lossless 10-bit stream
(sha256 bbffb979..., 63429 B), against aomdec:
  chroma walks per frame   64 -> 4
  chroma units per frame   24576 -> 1536 (oracle: 1536, exact)
  first ctx divergence      unit 1539 -> unit 2573
  first differing pixel     byte 74769 -> byte 90272

Control arm (--max-partition-size=64, no 128 root) stays byte-exact.
New gate a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major
pins the route (mu_chunk_order_hits) and the first-difference position;
mutating the chunk key back to the block raster turns it red.
crates/ec-av1/fixtures/ll444_128root_lossless.obuadded+0 -0

Binary file changed

crates/ec-av1/src/decode.rsmodified+99 -13
@@ -13540,7 +13540,18 @@ fn decode_intrabc_128rect(
13540 // top-left sample is outside the frame codes nothing.13540 // top-left sample is outside the frame codes nothing.
13541 let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c));13541 let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c));
13542 let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r));13542 let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r));
13543- for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() {13543+ // lane-av1h5: chunk-major token order at 128 (see the 128x128 arm).
13544+ let order: Vec<usize> = if side > 64 {
13545+ hit!(MU_CHUNK_ORDER_HITS);
13546+ let nchunk = (side / 64).max(1);
13547+ let mut o: Vec<usize> = (0..leaves.len()).collect();
13548+ o.sort_by_key(|&i| ((leaves[i].0 / 16) * nchunk + (leaves[i].1 / 16), i));
13549+ o
13550+ } else {
13551+ (0..leaves.len()).collect()
13552+ };
13553+ for (pos, &idx) in order.iter().enumerate() {
13554+ let (row, col, tw, th) = leaves[idx];
13544 debug_assert_eq!(13555 debug_assert_eq!(
13545 tw, th,13556 tw, th,
13546 "a square TX_64X64 tree root only yields square leaves"13557 "a square TX_64X64 tree root only yields square leaves"
@@ -13578,8 +13589,8 @@ fn decode_intrabc_128rect(
13578 // are done, its chroma is read immediately (the inter 128 path's13589 // are done, its chroma is read immediately (the inter 128 path's
13579 // interleave).13590 // interleave).
13580 let chunk = (row / 16, col / 16);13591 let chunk = (row / 16, col / 16);
13581- let done = idx + 1 == leaves.len()13592+ let done = pos + 1 == order.len()
13582- || (leaves[idx + 1].0 / 16, leaves[idx + 1].1 / 16) != chunk;13593+ || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16) != chunk;
13583 if done {13594 if done {
13584 let (cr, cc) = chunk;13595 let (cr, cc) = chunk;
13585 // `av1_get_max_uv_txsize(BLOCK_128X64)` = TX_32X32: one13596 // `av1_get_max_uv_txsize(BLOCK_128X64)` = TX_32X32: one
@@ -19579,6 +19590,7 @@ impl PlaneBuf<'_> {
19579 /// Reads one plane's transform block, reconstructs it into `plane` at19590 /// Reads one plane's transform block, reconstructs it into `plane` at
19580 /// `(x, y)`/`side`, and records what it leaves behind in `neighbours`.19591 /// `(x, y)`/`side`, and records what it leaves behind in `neighbours`.
19581 #[allow(clippy::too_many_arguments)]19592 #[allow(clippy::too_many_arguments)]
19593+
19582 fn read_plane(19594 fn read_plane(
19583 dec: &mut SymbolDecoder,19595 dec: &mut SymbolDecoder,
19584 cdfs: &mut Cdfs,19596 cdfs: &mut Cdfs,
@@ -19631,7 +19643,9 @@ fn read_plane(
19631 // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the19643 // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the
19632 // plane block is bigger than the transform. Our chroma tables hold the19644 // plane block is bigger than the transform. Our chroma tables hold the
19633 // offset-7 rows first, so the caller adds 3 for the bigger-block case19645 // offset-7 rows first, so the caller adds 3 for the bigger-block case
19634- // (only a 128x128 block's TX_32X32 chroma units, lane-sb128b r3).19646+ // (every chroma unit of a plane block above TX_4X4 -- lane-sb128b r3,
19647+ // and lane-av1h5 measured the 4:4:4 lossless 128 root's TX_4X4 chroma
19648+ // units reading the offset-10 rows too, against aomdec's `EC_DBGCTX`).
19635 usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0)19649 usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0)
19636 };19650 };
19637 // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's19651 // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's
@@ -32095,6 +32109,21 @@ thread_local! {
32095 pub(crate) static CHROMA_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };32109 pub(crate) static CHROMA_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
32096 }32110 }
32097 32111
32112+thread_local! {
32113+ /// lane-av1h5: how many blocks took the CHUNK-MAJOR token order -- a
32114+ /// block above 64 whose leaf walk was reordered into libaom's 64x64 "mu"
32115+ /// chunks (`decode_token_recon_block`'s plane loop is INSIDE the chunk
32116+ /// loop). One bump per such block. Below 64 a block IS one chunk and the
32117+ /// orders coincide, so the arm stays at 0 there.
32118+ pub(crate) static MU_CHUNK_ORDER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
32119+}
32120+
32121+/// [`MU_CHUNK_ORDER_HITS`], for a gate's own before/after delta.
32122+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
32123+pub(crate) fn mu_chunk_order_hits() -> usize {
32124+ MU_CHUNK_ORDER_HITS.with(std::cell::Cell::get)
32125+}
32126+
32098 /// [`CHROMA_SPLIT_TX_HITS`], for a gate's own before/after delta.32127 /// [`CHROMA_SPLIT_TX_HITS`], for a gate's own before/after delta.
32099 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates32128 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates
32100 pub(crate) fn chroma_split_tx_hits() -> usize {32129 pub(crate) fn chroma_split_tx_hits() -> usize {
@@ -39129,7 +39158,24 @@ fn decode_inter_block(
39129 // own slice of the block's motion-compensated predictor.39158 // own slice of the block's motion-compensated predictor.
39130 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);39159 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);
39131 let mut first_tx_type = TxType::DctDct;39160 let mut first_tx_type = TxType::DctDct;
39132- for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() {39161+ // lane-av1h5: libaom's `decode_token_recon_block` walks
39162+ // 64x64 "mu" chunks with the PLANE loop inside, so at 128
39163+ // the token order is CHUNK-major; the transform tree hands
39164+ // us a BLOCK-wide raster whose chunk members are not
39165+ // contiguous. Below 128 a block IS one chunk.
39166+ let mu_order = |leaves: &Vec<(usize, usize, usize, usize)>, side: usize| {
39167+ if side <= 64 {
39168+ return (0..leaves.len()).collect::<Vec<usize>>();
39169+ }
39170+ let nchunk = side / 64;
39171+ let mut o: Vec<usize> = (0..leaves.len()).collect();
39172+ o.sort_by_key(|&i| ((leaves[i].0 / 16) * nchunk + (leaves[i].1 / 16), i));
39173+ o
39174+ };
39175+ let order = mu_order(leaves, side);
39176+ hit!(MU_CHUNK_ORDER_HITS);
39177+ for (pos, &idx) in order.iter().enumerate() {
39178+ let (row, col, tw, th) = leaves[idx];
39133 let tu_mi = (at_mi.0 + row, at_mi.1 + col);39179 let tu_mi = (at_mi.0 + row, at_mi.1 + col);
39134 let (tu_px, tu_py) = (px + col * MI, py + row * MI);39180 let (tu_px, tu_py) = (px + col * MI, py + row * MI);
39135 let (tu_grid, tu_tx_type) = if tw == th {39181 let (tu_grid, tu_tx_type) = if tw == th {
@@ -39205,8 +39251,9 @@ fn decode_inter_block(
39205 // is a single chunk and this never fires.39251 // is a single chunk and this never fires.
39206 if side > 64 {39252 if side > 64 {
39207 let chunk = (row / 16, col / 16);39253 let chunk = (row / 16, col / 16);
39208- let done = idx + 1 == leaves.len()39254+ let done = pos + 1 == order.len()
39209- || (leaves[idx + 1].0 / 16, leaves[idx + 1].1 / 16) != chunk;39255+ || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16)
39256+ != chunk;
39210 if done {39257 if done {
39211 let (cr, cc) = chunk;39258 let (cr, cc) = chunk;
39212 // `av1_get_max_uv_txsize(BLOCK_128X128)` =39259 // `av1_get_max_uv_txsize(BLOCK_128X128)` =
@@ -40869,7 +40916,33 @@ fn decode_inter_block(
40869 // own slice of the block's motion-compensated predictor.40916 // own slice of the block's motion-compensated predictor.
40870 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);40917 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);
40871 let mut first_tx_type = TxType::DctDct;40918 let mut first_tx_type = TxType::DctDct;
40872- for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() {40919+ // lane-av1h5: libaom's `decode_token_recon_block` walks the
40920+ // block in 64x64 "mu" CHUNKS and codes EVERY PLANE of a
40921+ // chunk before the next one starts, so at 128 the token
40922+ // order is CHUNK-major. The transform tree hands us a
40923+ // BLOCK-wide raster (32 mi across here), whose chunk
40924+ // members are not contiguous: `leaves` 0..15 are chunk
40925+ // (0,0) row 0, but chunk (0,0) rows 1..15 are leaves
40926+ // 32..47, 64..79, ... Walking it as given closed the
40927+ // "chunk" every 16 leaves and coded each chunk's chroma
40928+ // sixteen times -- 64 chroma walks per 128 root where
40929+ // libaom codes 4 (measured against aomdec's `EC_DBGCTX`:
40930+ // 24576 chroma units per inter frame vs 1536). Below 128 a
40931+ // block IS one chunk and the two orders coincide.
40932+ let nchunk = (side / 64).max(1);
40933+ let order: Vec<usize> = if side > 64 {
40934+ hit!(MU_CHUNK_ORDER_HITS);
40935+ let mut o: Vec<usize> = (0..leaves.len()).collect();
40936+ o.sort_by_key(|&i| {
40937+ let (r, c) = (leaves[i].0, leaves[i].1);
40938+ ((r / 16) * nchunk + (c / 16), i)
40939+ });
40940+ o
40941+ } else {
40942+ (0..leaves.len()).collect()
40943+ };
40944+ for (pos, &idx) in order.iter().enumerate() {
40945+ let (row, col, tw, th) = leaves[idx];
40873 let tu_mi = (at_mi.0 + row, at_mi.1 + col);40946 let tu_mi = (at_mi.0 + row, at_mi.1 + col);
40874 let (tu_px, tu_py) = (px + col * MI, py + row * MI);40947 let (tu_px, tu_py) = (px + col * MI, py + row * MI);
40875 let (tu_grid, tu_tx_type) = if tw == th {40948 let (tu_grid, tu_tx_type) = if tw == th {
@@ -40945,8 +41018,9 @@ fn decode_inter_block(
40945 // is a single chunk and this never fires.41018 // is a single chunk and this never fires.
40946 if side > 64 {41019 if side > 64 {
40947 let chunk = (row / 16, col / 16);41020 let chunk = (row / 16, col / 16);
40948- let done = idx + 1 == leaves.len()41021+ let done = pos + 1 == order.len()
40949- || (leaves[idx + 1].0 / 16, leaves[idx + 1].1 / 16) != chunk;41022+ || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16)
41023+ != chunk;
40950 if done {41024 if done {
40951 let (cr, cc) = chunk;41025 let (cr, cc) = chunk;
40952 // `av1_get_max_uv_txsize(BLOCK_128X128)` =41026 // `av1_get_max_uv_txsize(BLOCK_128X128)` =
@@ -41827,7 +41901,18 @@ fn decode_inter_block(
41827 let mut v_units: Vec<i32> = Vec::new();41901 let mut v_units: Vec<i32> = Vec::new();
41828 if let Some(leaves) = vartx_leaves.as_ref() {41902 if let Some(leaves) = vartx_leaves.as_ref() {
41829 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);41903 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);
41830- for (idx, &(row, col, tx_px, _)) in leaves.iter().enumerate() {41904+ // lane-av1h5: chunk-major token order at 128 (see the 128x128 arm).
41905+ let order: Vec<usize> = if side > 64 {
41906+ hit!(MU_CHUNK_ORDER_HITS);
41907+ let nchunk = (side / 64).max(1);
41908+ let mut o: Vec<usize> = (0..leaves.len()).collect();
41909+ o.sort_by_key(|&i| ((leaves[i].0 / 16) * nchunk + (leaves[i].1 / 16), i));
41910+ o
41911+ } else {
41912+ (0..leaves.len()).collect()
41913+ };
41914+ for (pos, &idx) in order.iter().enumerate() {
41915+ let (row, col, tx_px, _) = leaves[idx];
41831 let tu_mi = (at_mi.0 + row, at_mi.1 + col);41916 let tu_mi = (at_mi.0 + row, at_mi.1 + col);
41832 let (tu_px, tu_py) = (px + col * MI, py + row * MI);41917 let (tu_px, tu_py) = (px + col * MI, py + row * MI);
41833 // A transform unit inside a block is not a block at that41918 // A transform unit inside a block is not a block at that
@@ -41900,8 +41985,9 @@ fn decode_inter_block(
41900 neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid);41985 neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid);
41901 if side > 64 {41986 if side > 64 {
41902 let chunk = (row / 16, col / 16);41987 let chunk = (row / 16, col / 16);
41903- let done = idx + 1 == leaves.len()41988+ let done = pos + 1 == order.len()
41904- || (leaves[idx + 1].0 / 16, leaves[idx + 1].1 / 16) != chunk;41989+ || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16)
41990+ != chunk;
41905 if !done {41991 if !done {
41906 continue;41992 continue;
41907 }41993 }
crates/ec-av1/src/stream.rsmodified+112 -0
@@ -6625,6 +6625,118 @@ pub(crate) mod tests {
6625 }6625 }
6626 }6626 }
6627 6627
6628+ /// lane-av1h5: the 128-ROOT lossless 4:4:4 pin. testsrc2 128x96
6629+ /// yuv444p10le, aomenc `--profile=1 --lossless=1 --enable-palette=0
6630+ /// --enable-intrabc=0 --cpu-used=0`, 6 frames; 63429 bytes, sha256
6631+ /// `bbffb979d20d581deb271aac75b7ee779dde57c024524ed6b1389356f55ca0fa`.
6632+ ///
6633+ /// This is the only committed cell where an UNSPLIT `BLOCK_128X128` root
6634+ /// meets lossless, and the only one that reaches libaom's 64x64 "mu" chunk
6635+ /// walk (`decode_token_recon_block`, decodeframe.c:972: the PLANE loop is
6636+ /// INSIDE the chunk loop). The var-tx leaf list is a BLOCK-wide raster, so
6637+ /// its chunk members are not contiguous: leaf 0..15 is chunk (0,0) row 0,
6638+ /// but chunk (0,0) rows 1..15 are leaves 32..47, 64..79, ... A chunk test
6639+ /// on the given order therefore closed the chunk every SIXTEEN leaves and
6640+ /// coded each chunk's chroma sixteen times -- 64 chroma walks per root
6641+ /// where libaom codes 4, and 24576 chroma units per inter frame against
6642+ /// the oracle's 1536.
6643+ ///
6644+ /// The gate pins the corrected route ([`crate::decode::mu_chunk_order_hits`]
6645+ /// must fire) and the first-difference position against `aomdec`'s
6646+ /// rawvideo. Reverting the reorder moves that position from 90273 back to
6647+ /// 74769, so the byte arm is the mutation witness.
6648+ #[test]
6649+ fn a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major() {
6650+ const NAME: &str = "a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major";
6651+ const FIXTURE_LEN: usize = 63429;
6652+ const FIXTURE_FNV: u64 = 0xf07d_47fc_fd51_2658;
6653+ /// First differing byte of the u16-LE YUV concatenation, measured
6654+ /// against `aomdec --rawvideo` with the chunk-major reorder in place.
6655+ /// The block-wide raster reaches 74769; the residual defect below it
6656+ /// is tracked in `lanes/av1h5.report.md` and is NOT what this gate
6657+ /// holds.
6658+ const FIRST_DIFF_FROM: usize = 90272;
6659+ const W: usize = 128;
6660+ const H: usize = 96;
6661+ const FRAMES: usize = 6;
6662+ let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
6663+ .join("fixtures/ll444_128root_lossless.obu");
6664+ let stream = std::fs::read(&obu).unwrap_or_else(|e| {
6665+ panic!(
6666+ "{NAME}: pinned fixture {} is missing ({e}) -- the gate cannot run, \
6667+ which is a failure, not a skip",
6668+ obu.display()
6669+ )
6670+ });
6671+ assert_eq!(stream.len(), FIXTURE_LEN, "{NAME}: fixture length moved");
6672+ assert_eq!(fnv1a64(&stream), FIXTURE_FNV, "{NAME}: fixture bytes moved");
6673+
6674+ let _guard = lock_gate_counters();
6675+ let before = crate::decode::mu_chunk_order_hits();
6676+ let frames = decode_stream(&stream).unwrap_or_else(|e| {
6677+ panic!("{NAME}: the pinned stream no longer decodes cleanly: {e}")
6678+ });
6679+ let hits = crate::decode::mu_chunk_order_hits();
6680+ assert!(
6681+ hits > before,
6682+ "{NAME}: gate is vacuous -- no block took the chunk-major token order \
6683+ (delta {before} -> {hits})"
6684+ );
6685+ assert_eq!(frames.len(), FRAMES, "{NAME}: frame count");
6686+ for f in &frames {
6687+ assert_eq!((f.width, f.height), (W, H), "{NAME}: dimensions");
6688+ }
6689+
6690+ if aomdec_path().is_file() {
6691+ let dir = std::env::temp_dir().join(format!("ec-av1-ll444root-{}", std::process::id()));
6692+ let _ = std::fs::remove_dir_all(&dir);
6693+ std::fs::create_dir_all(&dir).expect("scratch dir");
6694+ let raw = dir.join("aomdec.raw");
6695+ let out = Command::new(aomdec_path())
6696+ .args(["--codec=av1", "--rawvideo", "-o"])
6697+ .arg(&raw)
6698+ .arg(&obu)
6699+ .stdin(Stdio::null())
6700+ .stdout(Stdio::piped())
6701+ .stderr(Stdio::piped())
6702+ .output()
6703+ .expect("aomdec failed to run");
6704+ assert!(
6705+ out.status.success(),
6706+ "{NAME}: the oracle aomdec refused the stream: {}",
6707+ String::from_utf8_lossy(&out.stderr)
6708+ );
6709+ let ref_raw = std::fs::read(&raw).expect("aomdec rawvideo output");
6710+ let _ = std::fs::remove_dir_all(&dir);
6711+ // u16 LE, the concatenation `decode_probe` writes.
6712+ let mut ours: Vec<u8> = Vec::with_capacity(ref_raw.len());
6713+ for f in &frames {
6714+ for p in [&f.y, &f.u, &f.v] {
6715+ for s in p {
6716+ ours.extend_from_slice(&s.to_le_bytes());
6717+ }
6718+ }
6719+ }
6720+ assert_eq!(ours.len(), ref_raw.len(), "{NAME}: raw sizes differ");
6721+ let first = ours
6722+ .iter()
6723+ .zip(&ref_raw)
6724+ .position(|(a, b)| a != b)
6725+ .unwrap_or(ours.len());
6726+ assert!(
6727+ first >= FIRST_DIFF_FROM,
6728+ "{NAME}: the chunk-major token order regressed -- first differing \
6729+ byte is {first}, the block-wide raster reached {FIRST_DIFF_FROM} \
6730+ or earlier (pre-fix this stream first differed at 74769)"
6731+ );
6732+ } else {
6733+ eprintln!(
6734+ "SKIP {NAME} aomdec arm: no oracle aomdec at {}",
6735+ aomdec_path().display()
6736+ );
6737+ }
6738+ }
6739+
6628 /// lane-av1llpredgate2: the debug-exact pin for the ss-aware6740 /// lane-av1llpredgate2: the debug-exact pin for the ss-aware
6629 /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the6741 /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the
6630 /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc26742 /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc2