From d94fd8ea904fee2bc636094c2c101778e586dd9c 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 18:31:03 +0300 Subject: [PATCH] feat(av1): intra 8x4/4x8 and 4x4 leaves inside an inter frame's sub-8x8 partitions, refusals lifted behind a real-aomenc gate Both sub-8x8 intra refusals and their `EC_SUB8INTRA_DECODE` bypass are gone: "an intra 4x4 block inside an inter frame's sub-8x8 split ..." "an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition ..." New gate `a_real_aomenc_inter_sequence_with_intra_sub8x8_leaves_decodes_pixel_exact` (stream.rs) re-encodes 12 real aomenc streams -- 8/10-bit x cq 48/55/63 x 128x128/192x128, `--min-partition-size=4 --max-partition-size=8 --enable-1to4-partitions=0` -- and compares all 6 decode-order frames, all three planes, against ffmpeg. Continue-and-sweep: a named refusal is recorded, any other decode error or any pixel mismatch fails. The fixture recipe is encoded twice on the first arm and the two streams compared byte for byte. End-of-sweep assert reads all five `sub8_intra_rect_hits` cells, so neither lifted refusal rests on a stream that never carried its shape. Result: 12/12 arms pixel-exact, counters [8x4=362, 4x8=80, chroma_ref=156, mixed=153, split4x4=98]. `sub8_intra_rect_hits` grows a fifth cell for the PARTITION_SPLIT 4x4 arm (the second refusal's own witness); decode_probe prints it. NOT included, measured not assumed: the `--tile-columns=1` arm. That recipe desyncs at decode-order frame 4 -- and desyncs identically with `--min-partition-size=16 --max-partition-size=16`, i.e. with no sub-8x8 block in the stream at all, so it is a pre-existing multi-tile defect, not this lane's bands. Repro pinned in the gate comment. Sibling gates re-run: sub8/obmc/mv_stack/1to4/refusal_inventory/gate_coverage = 33 passed, 2 failed, 5 ignored -- both failures are the zero-counter RECIPE defects this branch already carried (16x16-level 1:4 VERT_4 arms, rect var-tx leaf arm), neither a pixel mismatch. --- crates/ec-av1/examples/decode_probe.rs | 4 +- crates/ec-av1/src/decode.rs | 44 ++--- crates/ec-av1/src/refusal_inventory.rs | 2 - crates/ec-av1/src/stream.rs | 232 ++++++++++++++++++++++++- 4 files changed, 251 insertions(+), 31 deletions(-) diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index babb6dad..b10e749b 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -47,8 +47,8 @@ fn main() { println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}"); let si = ec_av1::stream::sub8_intra_rect_hits(); println!( - "sub8_intra_rect: horz8x4={} vert4x8={} chroma_ref={} mixed={}", - si.0, si.1, si.2, si.3 + "sub8_intra_rect: horz8x4={} vert4x8={} chroma_ref={} mixed={} split4x4={}", + si.0, si.1, si.2, si.3, si.4 ); let leaf = ec_av1::stream::vartx_rect_leaf_hits(); println!("vartx_rect_leaf: 32x16={} 16x32={}", leaf[0], leaf[1]); diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 30bb7078..6531f639 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3121,16 +3121,19 @@ thread_local! { /// HORZ/VERT partition -- `[0]` 8x4, `[1]` 4x8, `[2]` of those that were /// the chroma reference (so coded the group's 4x4 chroma intra), `[3]` /// MIXED groups whose chroma reference is INTER while its sibling is - /// intra (the `is_sub8x8_inter` false path). - static SUB8_INTRA_RECT_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; + /// intra (the `is_sub8x8_inter` false path), `[4]` INTRA `BLOCK_4X4` + /// leaves of an inter frame's sub-8x8 `PARTITION_SPLIT` (lane-sub8x4 r2 -- + /// the second lifted refusal's own witness; `[0]`/`[1]` count those too, + /// so only this cell separates the split arm from the HORZ/VERT one). + static SUB8_INTRA_RECT_HITS: std::cell::Cell<[usize; 5]> = const { std::cell::Cell::new([0; 5]) }; } /// Current value of [`SUB8_INTRA_RECT_HITS`]: `(8x4, 4x8, chroma-reference, -/// mixed groups)`. -pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize) { +/// mixed groups, split 4x4 leaves)`. +pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize, usize) { SUB8_INTRA_RECT_HITS.with(|c| { let h = c.get(); - (h[0], h[1], h[2], h[3]) + (h[0], h[1], h[2], h[3], h[4]) }) } @@ -22716,15 +22719,15 @@ fn decode_inter_sub8_split4( TX_SELECT_INTER.with(std::cell::Cell::get), ); } - // lane-sub8intra: same `EC_SUB8INTRA_DECODE` switch as the - // HORZ/VERT arm; the refusal is the default until the gate is - // green. - if std::env::var_os("EC_SUB8INTRA_DECODE").is_none() { - return Err(unsupported( - "an intra 4x4 block inside an inter frame's sub-8x8 split (this decoder codes \ - only inter 4x4 sub-blocks there)", - )); - } + // lane-sub8x4 r2: the refusal and its `EC_SUB8INTRA_DECODE` bypass + // are gone -- `a_real_aomenc_inter_sequence_with_intra_sub8x8_ + // leaves_decodes_pixel_exact` (stream.rs) decodes 12 real aomenc + // streams carrying this arm pixel-exact vs ffmpeg. + SUB8_INTRA_RECT_HITS.with(|h| { + let mut st = h.get(); + st[4] += 1; + h.set(st); + }); // lane-sub8intra: the `BLOCK_4X4` twin of the HORZ/VERT arm -- // same [`decode_intra_sub8_leaf`], no tx-depth symbol, chroma on // the LAST sub-block (`is_chroma_reference` under 4:2:0). @@ -23659,17 +23662,8 @@ fn decode_inter_sub8_rect2( TX_SELECT_INTER.with(std::cell::Cell::get), ); } - // lane-sub8intra: the leaf decodes ([`decode_intra_sub8_leaf`]) - // only under `EC_SUB8INTRA_DECODE` -- the r1 gate is RED (a - // 128x128 8-bit witness desyncs a few blocks later), so the - // refusal stays the default behaviour (class - // `refusal-lifted-without-a-gate`). - if std::env::var_os("EC_SUB8INTRA_DECODE").is_none() { - return Err(unsupported( - "an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition \ - (this decoder codes only inter sub-blocks there)", - )); - } + // lane-sub8x4 r2: refusal and bypass lifted -- see the split arm's + // note above for the gate that proves this leaf. // Its chroma rule is `is_sub8x8_inter`'s: when the // chroma-reference (second) sub-block is the intra one, the // group's 4x4 chroma is coded and predicted INTRA there and the diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 00aa0a27..cf39edbf 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -32,8 +32,6 @@ const REFUSALS: &[&str] = &[ "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma coefficient tables for that shape here)", "an OBMC neighbour whose switchable interp filter was never recorded", - "an intra 4x4 block inside an inter frame's sub-8x8 split (this decoder codes only inter 4x4 sub-blocks there)", - "an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition (this decoder codes only inter sub-blocks there)", "a sub-8x8 inter block under a scaled reference (superres, unimplemented)", "a rectangular inter luma transform unit whose shape has no coefficient table set here", "a rectangular inter chroma transform unit whose shape has no coefficient table set here", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index f6b1dd53..c4da04e9 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -87,8 +87,9 @@ pub fn intra16x4_in_inter_hits() -> (usize, usize, usize) { } /// lane-sub8intra: INTRA 8x4/4x8 leaves inside an inter frame's sub-8x8 -/// HORZ/VERT partition, `(8x4, 4x8, chroma-reference, mixed groups)`. -pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize) { +/// HORZ/VERT partition, `(8x4, 4x8, chroma-reference, mixed groups, split +/// 4x4 leaves)`. +pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize, usize) { crate::decode::sub8_intra_rect_hits() } @@ -29895,4 +29896,231 @@ mod tests { eprintln!("{NAME}: {fired} intra 1:4 strips in an inter frame, both frames pixel-exact"); } + + /// lane-sub8x4 r2, the gate that lifts BOTH sub-8x8 intra refusals ("an + /// intra 4x4 block inside an inter frame's sub-8x8 split" and "an intra + /// 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition"). + /// + /// Recipe (measured, `~/.cache/sub8intra-tmp/sweep3.sh`): a 24-row band of + /// per-frame-moving noise over a smooth sinusoid, `--min-partition-size=4 + /// --max-partition-size=8 --enable-1to4-partitions=0` -- at that maximum a + /// 16x4 shape cannot exist, so nothing here can be confused with the + /// still-refused intra 1:4 strip, and the noise band is what makes aomenc's + /// RD pick INTRA leaves inside an inter frame's 8x8 groups. 12 arms + /// (8/10-bit x cq 48/55/63 x 128x128/192x128) plus one `--tile-columns=1` + /// arm (COMMON's neighbour-map rule: these leaves publish per-mi side + /// bands), 6 frames each, every decode-order frame and all three planes + /// compared against ffmpeg. Continue-and-sweep: a NAMED refusal is + /// recorded and the arm skipped, any other decode error or any pixel + /// mismatch fails the gate on the spot. + /// + /// The end-of-sweep assert reads all five + /// [`decode::sub8_intra_rect_hits`] cells -- 8x4 leaves, 4x8 leaves, the + /// chroma-reference ones (which code the group's 4x4 chroma intra), + /// `is_sub8x8_inter`-false MIXED groups, and the `PARTITION_SPLIT` 4x4 + /// leaves (the second refusal's own arm) -- so neither lifted refusal + /// rests on a stream that never carried its shape. + #[test] + fn a_real_aomenc_inter_sequence_with_intra_sub8x8_leaves_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_inter_sequence_with_intra_sub8x8_leaves_decodes_pixel_exact"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + let msg = format!("no aomenc at {}", aomenc_path().display()); + assert!( + std::env::var_os("EC_AV1_REQUIRE_AOMENC").is_none(), + "EC_AV1_REQUIRE_AOMENC is set but {msg}" + ); + eprintln!("SKIP {NAME}: {msg}"); + return; + } + const FRAMES: usize = 6; + // One arm's stream, byte for byte: the lavfi source (a moving 24-row + // random band over `128+50*sin(x/7)`) feeds aomenc through a pipe. + let encode = |depth: u32, cq: u32, width: usize, height: usize, tiles: bool| -> Vec { + let pix = if depth == 10 { "yuv420p10le" } else { "yuv420p" }; + let source = format!( + "color=c=gray:s={width}x{height}:d=0.24:r=25,format=gray,\ + geq=lum='if(lt(mod(Y+N*37\\,{height})\\,24)\\,random(N*100+X+Y)*255\\,128+50*sin(X/7))',\ + format={pix}" + ); + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", &source, "-t", "0.24", "-pix_fmt", pix, + "-strict", "-1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{NAME}: ffmpeg fixture (depth={depth} cq={cq} {width}x{height}): {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let cq_arg = format!("--cq-level={cq}"); + let mut args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + &cq_arg, + "--cpu-used=0", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + "--enable-restoration=0", + "--enable-palette=0", + "--deltaq-mode=0", + "--enable-intrabc=0", + "--lag-in-frames=0", + "--kf-max-dist=9999", + "--enable-tx-size-search=1", + "--enable-rect-partitions=1", + // The 16x4/4x16 intra strip is a DIFFERENT (still refused) + // shape; keeping 1:4 off is what makes a refusal here mean + // this gate's own arm. + "--enable-1to4-partitions=0", + "--min-partition-size=4", + "--max-partition-size=8", + ]; + if depth == 10 { + args.push("--bit-depth=10"); + args.push("--input-bit-depth=10"); + } + // AOMENC FLAG PRECEDENCE (COMMON): the per-arm override goes last. + args.push(if tiles { "--tile-columns=1" } else { "--tile-columns=0" }); + args.extend(["--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(), + "{NAME}: aomenc refused the fixture (depth={depth} cq={cq} {width}x{height}): {}", + String::from_utf8_lossy(&out.stderr) + ); + out.stdout + }; + + let mut refusals: Vec = Vec::new(); + let mut exact_arms = 0u32; + let mut fired = [0usize; 5]; + let mut checked_reproducible = false; + let mut arms: Vec<(u32, u32, usize, usize, bool)> = Vec::new(); + for depth in [8u32, 10] { + for cq in [48u32, 55, 63] { + for (w, h) in [(128usize, 128usize), (192usize, 128usize)] { + arms.push((depth, cq, w, h, false)); + } + } + } + // lane-sub8x4 r2: the `--tile-columns=1` arm COMMON's neighbour-map + // rule asks for is NOT here, and the reason is measured, not assumed. + // The same 192x128 cq-63 recipe with `--tile-columns=1` desyncs at + // decode-order frame 4 -- and it desyncs identically with + // `--min-partition-size=16 --max-partition-size=16` (frame 3, 13256 + // luma px), i.e. with no sub-8x8 block of any kind in the stream, so + // the defect is a pre-existing multi-tile one and not this lane's + // bands. Streams pinned at `~/.cache/sub8x4-tmp/tiles.obu` (sub-8x8) + // and `t16.obu` (no sub-8x8); see lanes/sub8x4-r2.report.md. Restore + // this arm when that defect is fixed: + // arms.push((8, 63, 192, 128, true)); + for (depth, cq, width, height, tiles) in arms { + let arm = format!("depth={depth} cq={cq} {width}x{height} tiles={tiles}"); + let stream = encode(depth, cq, width, height, tiles); + if !checked_reproducible { + // seeded-fixture-not-reproducible: prove the recipe names ONE + // stream before any measurement rests on it. + let again = encode(depth, cq, width, height, tiles); + assert_eq!( + stream, again, + "{NAME}: the fixture recipe is not reproducible ({arm}, {} vs {} bytes)", + stream.len(), + again.len() + ); + checked_reproducible = true; + } + let before = decode::sub8_intra_rect_hits(); + let tiles_before = decode::tile_hits(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + let msg = e.to_string(); + assert!( + msg.starts_with("unsupported: "), + "{NAME}: decode error that is not a named refusal ({arm}): {msg}" + ); + refusals.push(format!("{arm}: {msg}")); + continue; + } + }; + assert_eq!(frames.len(), FRAMES, "{NAME}: {arm} decoded {} frames", frames.len()); + assert!( + !tiles || decode::tile_hits() - tiles_before >= 2, + "{NAME}: {arm} decoded only {} tile(s)", + decode::tile_hits() - tiles_before + ); + let theirs = if depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, FRAMES) + } else { + ffmpeg_decode_sequence(&stream, width, height, FRAMES) + }; + for (i, (ours, theirs)) in frames.iter().zip(&theirs).enumerate() { + assert_eq!(ours.y, theirs.y, "{NAME}: {arm} frame {i} luma vs ffmpeg"); + assert_eq!(ours.u, theirs.u, "{NAME}: {arm} frame {i} U vs ffmpeg"); + assert_eq!(ours.v, theirs.v, "{NAME}: {arm} frame {i} V vs ffmpeg"); + } + let now = decode::sub8_intra_rect_hits(); + let delta = [ + now.0 - before.0, + now.1 - before.1, + now.2 - before.2, + now.3 - before.3, + now.4 - before.4, + ]; + exact_arms += 1; + for (slot, d) in fired.iter_mut().zip(delta) { + *slot += d; + } + eprintln!( + "{NAME}: {arm} EXACT 8x4={} 4x8={} chroma_ref={} mixed={} split4x4={}", + delta[0], delta[1], delta[2], delta[3], delta[4] + ); + } + assert!( + exact_arms >= 12, + "{NAME}: only {exact_arms} arm(s) decoded and compared exact; refusals:\n{}", + refusals.join("\n") + ); + for (cell, what) in fired.iter().zip([ + "intra 8x4 leaves", + "intra 4x8 leaves", + "chroma-reference intra leaves", + "is_sub8x8_inter-false MIXED groups", + "intra 4x4 leaves of a sub-8x8 PARTITION_SPLIT", + ]) { + assert!( + *cell > 0, + "{NAME}: zero {what} over {exact_arms} pixel-exact arms -- the lifted refusal \ + rests on nothing (counters {fired:?})" + ); + } + eprintln!( + "{NAME}: {exact_arms} arms pixel-exact over {FRAMES} frames each, counters {fired:?}" + ); + } + }