From 5f35cce379c5779ee184b0897472dee744a68063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 05:41:05 +0300 Subject: [PATCH] fix(av1): a 16x8/8x16 intra leaf reads tx_size_cdf[cat 1], not cat 2 -- wrong-row tx_depth desynced every 16-level rect strip under --enable-tx-size-search lane-rect1d r1. `decode_leaf_rect` read its `tx_depth` symbol from `cdfs.tx_size_cat2`, the table the 32-level strips (BLOCK_32X16/16X32) use. libaom's `bsize_to_tx_size_cat` reads `bsize_to_tx_size_depth_table` and subtracts one: BLOCK_16X8/8X16 (and the 1:4 BLOCK_16X4/4X16 this function also decodes) sit at depth 2, so they read `tx_size_cdf[1]` -- the same table a 16x16 square block reads. Both tables carry three symbols, so the wrong one still decodes a plausible depth, from the wrong probabilities: the range diverges silently at the first such leaf and every later symbol in the tile comes out of the wrong place. Found by copying lane-rectclass's 1D-tx-class gate onto this tree (main's `read_coeffs_rect` no longer refuses a 1D class, so the gate now DECODES those TUs and the merge exposed the mismatch). Bisection: pinned stream (sha256 5b3c8fc9... twice), coefficient range ladder ours vs instrumented aomdec matched for 105 transform units and diverged at the 106th; the mode ladder matched through `uv_mode` (rng 50640 both) and the next symbol was aomdec's `name=tx_depth val=0 ctx=2 cat=1 rng=60682` against our cat-2 read. Block: mi_row=24 mi_col=44, BLOCK_16X8, 10-bit seed 42. Gate: `a_real_aomenc_stream_with_a_1d_tx_class_on_a_rect_transform_decodes_pixel_exact` (copied from lane-rectclass ef7a476, minus its two now-nonexistent refusal strings and the vacuous `class1_refusals == 0` assert), plus a new `rect_class1_hits` counter so the gate pins the 1D class on a RECTANGULAR transform rather than settling for the square reader's own 1D types: 12/12 pixel-exact (6 of them 10-bit), 219 rect coefficient TUs, 10 of them a 1D tx class. `enable-flip-idtx` leaves the 10-bit coverage-hole list. `examples/dump_yuv.rs`: raw u16 plane dump -- both prefilt dumps narrow to `u8`, which is blind to exactly this defect at 10 bits. --- crates/ec-av1/examples/dump_yuv.rs | 46 +++++ crates/ec-av1/src/decode.rs | 37 +++- crates/ec-av1/src/gate_coverage.rs | 10 +- crates/ec-av1/src/stream.rs | 263 +++++++++++++++++++++++++++++ 4 files changed, 351 insertions(+), 5 deletions(-) create mode 100644 crates/ec-av1/examples/dump_yuv.rs diff --git a/crates/ec-av1/examples/dump_yuv.rs b/crates/ec-av1/examples/dump_yuv.rs new file mode 100644 index 00000000..2892c620 --- /dev/null +++ b/crates/ec-av1/examples/dump_yuv.rs @@ -0,0 +1,46 @@ +//! Decode one OBU stream and write every frame's planes as raw little-endian +//! `u16` (`.f.yuv`) — the high-bit-depth companion to +//! `EC_AV1_PREFILT_DUMP`, whose `as u8` narrowing throws away exactly the bits +//! a 10-bit mismatch lives in (lane-rect1d r1 found its defect by diffing this +//! against `ffmpeg -pix_fmt yuv420p10le -f rawvideo`). +//! +//! ```text +//! cargo run -p ec-av1 --example dump_yuv -- s.obu /tmp/ours +//! ffmpeg -v error -i s.obu -pix_fmt yuv420p10le -f rawvideo /tmp/ref.yuv +//! cmp /tmp/ours.f0.yuv /tmp/ref.yuv +//! ``` +fn main() { + let mut args = std::env::args().skip(1); + let (Some(path), Some(out)) = (args.next(), args.next()) else { + eprintln!("usage: dump_yuv "); + std::process::exit(2); + }; + let data = match std::fs::read(&path) { + Ok(data) => data, + Err(e) => { + eprintln!("{path}: {e}"); + std::process::exit(2); + } + }; + let frames = match ec_av1::stream::decode_stream(&data) { + Ok(frames) => frames, + Err(e) => { + eprintln!("{path}: {e}"); + std::process::exit(1); + } + }; + for (i, f) in frames.iter().enumerate() { + let mut buf = Vec::with_capacity((f.y.len() + f.u.len() + f.v.len()) * 2); + for plane in [&f.y, &f.u, &f.v] { + for &s in plane.iter() { + buf.extend_from_slice(&s.to_le_bytes()); + } + } + let name = format!("{out}.f{i}.yuv"); + if let Err(e) = std::fs::write(&name, &buf) { + eprintln!("{name}: {e}"); + std::process::exit(2); + } + println!("frame {i}: {}x{} -> {name}", f.width, f.height); + } +} diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 45cd45b5..e591cb00 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -972,6 +972,28 @@ pub(crate) fn filter_intra_rect_hits() -> usize { FILTER_INTRA_RECT_HITS.with(|c| c.get()) } +// How many rectangular transform units actually coded coefficients through +// [`read_coeffs_rect`] (`all_zero == 0`) -- lane-rect1d's measure that a gate +// exercised the rect coefficient reader at all rather than passing on +// all-skip blocks. +thread_local! { + static RECT_COEFF_TU_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static RECT_CLASS1_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`RECT_COEFF_TU_HITS`]. +pub(crate) fn rect_coeff_tu_hits() -> usize { + RECT_COEFF_TU_HITS.with(|c| c.get()) +} + +/// Rectangular transform units whose resolved `tx_type` is a 1D class +/// (`V_DCT`/`H_DCT`) -- [`TX_CLASS1_HITS`]'s rect-only half, so a gate can +/// pin that the 1D-class-on-a-RECT-transform path fired rather than settling +/// for the square reader's own 1D types. +pub(crate) fn rect_class1_hits() -> usize { + RECT_CLASS1_HITS.with(|c| c.get()) +} + // As [`FILTER_INTRA_RECT_HITS`], counting only the strips BELOW 16x16 // (8x16/16x8 leaves of a 16x16 HORZ/VERT partition, lane-fistrip r1) -- the // shapes whose `use_filter_intra` symbol had no CDF class before this round. @@ -2777,6 +2799,7 @@ fn read_coeffs( let class = TxClass::of(tx_type); if class != TxClass::TwoD { TX_CLASS1_HITS.with(|c| c.set(c.get() + 1)); + RECT_CLASS1_HITS.with(|c| c.set(c.get() + 1)); } let eob = read_eob(dec, coding, class); if trace { @@ -2927,6 +2950,7 @@ fn read_coeffs_rect( if all_zero { return Ok((grid, TxType::DctDct)); } + RECT_COEFF_TU_HITS.with(|c| c.set(c.get() + 1)); // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including // the `V_DCT`/`H_DCT` members whose 1D tx class needs its own scan and @@ -5757,7 +5781,18 @@ fn decode_leaf_rect( let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); let depth = if tx_select { let ctx = tx_size_context_rect(neighbours, leaf_mi, bw, bh); - dec.symbol(&mut cdfs.tx_size_cat2[ctx]) + // lane-rect1d r1: the CATEGORY of this leaf is 1, not 2. libaom's + // `bsize_to_tx_size_cat` reads `bsize_to_tx_size_depth_table` and + // subtracts one: BLOCK_16X8/8X16 (and the 1:4 BLOCK_16X4/4X16 this + // function also decodes) sit at depth 2, so they read + // `tx_size_cdf[1]` -- the same table a 16x16 square block reads -- + // while `tx_size_cat2` belongs to the 32-level shapes + // ([`decode_block_rect`]). Both tables carry three symbols, so the + // wrong one decodes a plausible depth from the wrong probabilities: + // the range diverges silently at the first such leaf and every later + // symbol in the tile is read from the wrong place (class + // `wrong-alphabet-same-value`, here same alphabet / wrong row). + dec.symbol(&mut cdfs.tx_size_cat1[ctx]) } else { 0 }; diff --git a/crates/ec-av1/src/gate_coverage.rs b/crates/ec-av1/src/gate_coverage.rs index b6805291..396ceda0 100644 --- a/crates/ec-av1/src/gate_coverage.rs +++ b/crates/ec-av1/src/gate_coverage.rs @@ -226,10 +226,12 @@ const NEVER_EXERCISED_10BIT: &[(&str, &str)] = &[ // lane-cwarp's 10-bit comp "enable-dual-filter", "hole at both depths, see the 8-bit list", ), - ( - "enable-flip-idtx", - "hole at both depths, see the 8-bit list", - ), + // `enable-flip-idtx` LEFT this list on 2026-09-02 (lane-rect1d r1): + // `a_real_aomenc_stream_with_a_1d_tx_class_on_a_rect_transform_decodes_pixel_exact` + // passes `=1` at both depths and pixel-compares six 10-bit decodes, so the + // flip/identity/1D transform types are proven to reach a real 10-bit stream + // this decoder reconstructs exactly (10 of its rect coefficient TUs carry a + // 1D tx class). ("enable-intrabc", "hole at both depths, see the 8-bit list"), ("enable-rect-tx", "hole at both depths, see the 8-bit list"), ( diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 73be302e..87138506 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -15567,6 +15567,269 @@ mod tests { ); } + /// lane-rect1d r1 (copied from lane-rectclass r1): the class-correctness measurement for every + /// RECTANGULAR transform unit this decoder reads. [`crate::decode::read_coeffs_rect`] + /// handles `TxClass::TwoD` only -- it refuses by name both a coded + /// `tx_type` symbol and a non-2D default -- so the open question a + /// verifier raised was whether a real encoder ever puts a + /// `V_DCT`/`H_DCT` (`TX_CLASS_HORIZ`/`VERT`) type on a rect TU that + /// reaches this reader. The recipe deliberately maximises the chance: + /// `--enable-flip-idtx=1` (the flip/identity/1D types are exactly what + /// `EXT_TX_SET_DTT4_IDTX_1DDCT` and the wider inter sets add), + /// `--enable-tx-size-search=1`, rect partitions on, small minimum + /// partition size, both bit depths. Every successful decode is + /// pixel-compared; the rect-TU counter is folded in PER ATTEMPT and only + /// after that attempt compared (class `counter-from-refused-stream`), and + /// the class-1 refusal counter is summed over EVERY attempt including the + /// refused ones -- a refused attempt is exactly where a 1D class on a + /// rect TU would show up. + /// + /// lane-rect1d: on THIS tree `read_coeffs_rect` no longer refuses a 1D + /// class -- lane-tx4x8 gave it the mrow/mcol scan and the class-aware + /// base/br contexts -- so the live pins are `compared_class1 > 0` (the + /// flag arrived and 1D types were coded) plus the pixel compare itself. + #[test] + fn a_real_aomenc_stream_with_a_1d_tx_class_on_a_rect_transform_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_stream_with_a_1d_tx_class_on_a_rect_transform_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height) = (192usize, 128usize); + let mut named_refusals = 0u32; + let mut matched = 0u32; + let mut matched_10bit = 0u32; + let mut compared_rect_tus = 0usize; + let mut compared_rect_parts = 0usize; + let mut compared_tx_depths = 0usize; + let mut compared_class1 = 0usize; + let mut compared_rect_class1 = 0usize; + let n_attempts: u32 = std::env::var("EC_RECTCLASS_GATE_ATTEMPTS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(12); + // Both bit depths in one sweep: his two films are yuv420p10le, and a + // class claim that only holds at 8 bits is worth nothing to them. + for attempt in 0..n_attempts { + let seed = 42 + attempt / 2; + let ten_bit = attempt % 2 == 1; + let pix_fmt = if ten_bit { "yuv420p10le" } else { "yuv420p" }; + // Content + quantiser sweep: the 1D (`V_DCT`/`H_DCT`) types RD can + // only pick on strongly one-directional detail, and only when the + // quantiser leaves that detail in the residual -- smooth gradients + // at cq 45 produced exactly zero of them (measured this round), + // which would have made the `--enable-flip-idtx=1` arrival assert + // fail rather than the gate pass vacuously. + let period = [4u32, 6, 8, 12, 16][(attempt as usize / 2) % 5]; + let source = format!("testsrc2=size={width}x{height}:rate=25"); + let filter = format!( + "geq=lum='128+80*sin(2*PI*Y/{period})':cb='128+60*sin(2*PI*X/{period})':cr=128,\ + noise=alls=6:allf=t" + ); + let cq = if attempt % 4 < 2 { "--cq-level=20" } else { "--cq-level=40" }; + let y4m = Command::new("ffmpeg") + .args([ + // `-strict -1`: y4m calls 10-bit an unofficial pixel + // format and refuses to write the header otherwise (no-op + // at 8 bit). + "-v", "error", "-f", "lavfi", "-i", &source, "-vf", &filter, "-t", "0.04", + "-pix_fmt", pix_fmt, "-strict", "-1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + // aomenc keeps the LAST occurrence of a repeated `--enable-*` + // flag (measured 2026-09-02 by md5 of four orderings), so this + // lane's overrides come AFTER the base recipe. They are also + // proven to have ARRIVED rather than assumed: the asserts below + // pin a nonzero rect-partition, tx-depth and 1D-tx-class counter + // on pixel-compared attempts, one per override. + let mut args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + cq, + "--cpu-used=0", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-restoration=0", + "--enable-palette=0", + "--deltaq-mode=0", + "--enable-filter-intra=0", + "--enable-cfl-intra=0", + "--enable-intrabc=0", + ]; + if ten_bit { + args.extend_from_slice(&["--input-bit-depth=10", "--bit-depth=10"]); + } + args.extend_from_slice(&[ + "--enable-rect-partitions=1", + "--enable-tx-size-search=1", + "--enable-flip-idtx=1", + // Without this aomenc's reduced tx set narrows every block to + // DCT_DCT/IDTX and no 1D type can be coded at all -- the flag + // that actually gates the class, as the arrival assert below + // proved by staying at 0 without it. + "--reduced-tx-type-set=0", + "--min-partition-size=8", + "--max-partition-size=32", + "--obu", + "-o", + "-", + "-", + ]); + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + assert!(!stream.is_empty(), "{NAME}: aomenc wrote an empty stream (seed {seed})"); + if ten_bit { + let mut probe = Av1Parser::new(); + let mut pos = 0usize; + while pos < stream.len() && probe.sequence_header().is_none() { + let obu = probe.parse_obu(&stream[pos..]).unwrap(); + pos += obu.total_size; + } + assert_eq!( + probe + .sequence_header() + .expect("stream has a sequence header OBU") + .color_config + .bit_depth, + 10, + "{NAME}: aomenc did not actually write a 10-bit sequence header" + ); + } + let attempt_before = crate::decode::rect_coeff_tu_hits(); + let rect_before = crate::decode::rect_partition_hits(); + let depth_before = crate::decode::tx_depth_hits(); + let class1_before = crate::decode::tx_class1_hits(); + let rect_class1_before = crate::decode::rect_class1_hits(); + let frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal (seed {seed}, 10bit \ + {ten_bit}): {msg}" + ); + named_refusals += 1; + continue; + } + Ok(frames) => frames, + }; + let ffmpeg_frames = if ten_bit { + ffmpeg_decode_sequence_10bit(&stream, width, height, 1) + } else { + ffmpeg_decode_sequence(&stream, width, height, 1) + }; + assert_eq!(frames.len(), 1, "{NAME}: expected one frame (seed {seed})"); + assert_eq!( + frames[0].y, ffmpeg_frames[0].y, + "{NAME}: luma vs ffmpeg (seed {seed}, 10bit {ten_bit})" + ); + assert_eq!( + frames[0].u, ffmpeg_frames[0].u, + "{NAME}: U vs ffmpeg (seed {seed}, 10bit {ten_bit})" + ); + assert_eq!( + frames[0].v, ffmpeg_frames[0].v, + "{NAME}: V vs ffmpeg (seed {seed}, 10bit {ten_bit})" + ); + compared_rect_tus += crate::decode::rect_coeff_tu_hits() - attempt_before; + compared_rect_parts += crate::decode::rect_partition_hits() - rect_before; + compared_tx_depths += crate::decode::tx_depth_hits() - depth_before; + compared_class1 += crate::decode::tx_class1_hits() - class1_before; + compared_rect_class1 += crate::decode::rect_class1_hits() - rect_class1_before; + matched += 1; + matched_10bit += u32::from(ten_bit); + } + assert!( + matched > 0, + "{NAME}: every attempt refused ({named_refusals} named refusals) -- gate decoded \ + nothing" + ); + assert!( + matched_10bit > 0, + "{NAME}: every 10-bit attempt refused -- his films are yuv420p10le, so a gate that \ + only compared 8-bit streams proves nothing for them" + ); + assert!( + compared_rect_tus > 0, + "{NAME}: no pixel-exact attempt coded a single rectangular transform unit \ + ({matched} matches, {named_refusals} refusals out of {n_attempts}) -- the gate \ + measured nothing about rect coefficient reading" + ); + // FLAG ARRIVAL, one assert per override (a flag whose counter stays + // at 0 is an inert flag, not a passing gate). + assert!( + compared_rect_parts > 0, + "{NAME}: --enable-rect-partitions=1 never produced a rect partition on a compared \ + attempt ({matched} matches) -- the flag did not arrive" + ); + assert!( + compared_tx_depths > 0, + "{NAME}: --enable-tx-size-search=1 never produced a nonzero tx depth on a compared \ + attempt ({matched} matches) -- the flag did not arrive" + ); + assert!( + compared_class1 > 0, + "{NAME}: --enable-flip-idtx=1 never produced a V_DCT/H_DCT transform on a compared \ + attempt ({matched} matches) -- the 1D types live only in the extended ext-tx sets \ + this flag enables, so the flag did not arrive" + ); + // lane-rect1d r1: the pin of the LANE's own capability -- a V_DCT/H_DCT + // transform on a RECTANGULAR transform unit, read through + // `read_coeffs_rect`'s own 1D scan and contexts. `compared_class1` + // above counts square TUs too, so it can be nonzero while this path + // never fires. + assert!( + compared_rect_class1 > 0, + "{NAME}: no pixel-exact attempt put a 1D tx class on a RECTANGULAR transform \ + ({compared_class1} 1D transforms overall, {compared_rect_tus} rect coefficient \ + TUs) -- the gate proves nothing about the rect 1D path" + ); + eprintln!( + "{NAME}: {matched} pixel-exact decodes ({named_refusals} named refusals out of \ + {n_attempts} attempts, {matched_10bit} of the matches 10-bit), rect coefficient \ + TUs on compared attempts: {compared_rect_tus} ({compared_rect_class1} of them a \ + 1D tx class); flag arrival: rect partitions {compared_rect_parts}, tx \ + depths {compared_tx_depths}, 1D-class (square) transforms {compared_class1}" + ); + } + /// lane-rectsplit r1 gate (c): gate (a)'s recipe with /// `--enable-filter-intra=1` (every sibling gate here pins it to 0), so /// aomenc's RD is free to pick `use_filter_intra` on a 32x16/16x32 strip