From b7c0e219702c59344f13b1c274e277c1f8cc34f0 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 06:43:07 +0300 Subject: [PATCH] feat(av1): 128x128 superblock root in the INTER tile loop (visit order + prologue), sb-aware has_top_right; refusal kept for a residual one-frame mismatch The inter tile loop now walks libaom's quadrant visit order (sb_visit_order) and reads the 128 root once per superblock (read_lr over the whole 128x128, the 8-symbol partition_w128 with its edge gather) through a new shared read_sb128_root, extracted from the key-frame loop so both paths cannot drift. libaom has_top_right is now a single sb-aware port in mvstack (the warp-sample copy in decode.rs hardcoded a 64x64 superblock and now delegates to it), and the MV stack's top-right probe is gated on it instead of a bare grid probe. 8 of the 9 decode-order frames of the new gate stream are pixel-exact against the instrumented aomdec, hidden alt-ref included; frame 6 still differs by 996 luma bytes with the entropy stream in sync, and the same content at --sb-size=64 is exact (sb128_r2_control_sb64), so the residual is sb128-specific. The product-path refusal therefore STAYS and the gate ships #[ignore]d as its reproducer -- silent wrong pixels on his films are worse. --- crates/ec-av1/src/decode.rs | 202 ++++++++++++++--------- crates/ec-av1/src/mvstack.rs | 79 ++++++--- crates/ec-av1/src/stream.rs | 310 ++++++++++++++++++++++++++--------- 3 files changed, 409 insertions(+), 182 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a9e0c44b..b0b50c24 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -78,7 +78,7 @@ fn sb128() -> bool { } /// This sequence's superblock size in mi units (16 = 64x64, 32 = 128x128). -fn sb_mi_cur() -> u32 { +pub(crate) fn sb_mi_cur() -> u32 { if sb128() { SB_MI * 2 } else { SB_MI } } @@ -11243,6 +11243,70 @@ fn sb_visit_order(r0: u32, r1: u32, c0: u32, c1: u32, sb128: bool) -> Vec<(u32, out } +/// lane-sb128 r2: the 128x128 superblock root, shared by the key-frame and +/// the inter tile loop. Its loop-restoration units are read once for the whole +/// 128x128 (spec 5.11.57 is called with the superblock's own size), then one +/// partition symbol off the `BLOCK_128X128` CDF (8 symbols, no HORZ_4/VERT_4) +/// decides the root -- only SPLIT recurses into the four 64x64 quadrants this +/// decoder codes, the other seven arms refuse by name. +#[allow(clippy::too_many_arguments)] +fn read_sb128_root( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + neighbours: &Neighbours, + lr: &LoopRestorationParams, + lr_grid: &mut crate::restoration::RestorationGrid, + lr_reference: &mut [( + crate::restoration::WienerInfo, + crate::restoration::SgrprojInfo, + ); 3], + sb_r: u32, + sb_c: u32, + mi_cols: u32, + mi_rows: u32, +) -> Result<()> { + let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); + crate::restoration::read_lr( + dec, + cdfs, + lr, + lr_grid, + lr_reference, + mi_r128, + mi_c128, + SB_MI * 2, + ); + let at128 = ((mi_r128 / SUB_MI) as usize, (mi_c128 / SUB_MI) as usize); + let ctx128 = neighbours.partition_ctx(at128, 128); + let (has_cols128, has_rows128) = (mi_c128 + SB_MI < mi_cols, mi_r128 + SB_MI < mi_rows); + let part128 = if has_cols128 && has_rows128 { + PART128_SYMBOLS.with(|c| c.set(c.get() + 1)); + let p = dec.symbol(&mut cdfs.partition_w128[ctx128]); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!("TRACE partition_w128 ctx={ctx128} value={p}"); + } + p + } else { + match (has_cols128, has_rows128) { + (true, false) => { + dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &VERT_ALIKE128)); + } + (false, true) => { + dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &HORZ_ALIKE128)); + } + _ => {} + } + PARTITION_SPLIT + }; + if part128 != PARTITION_SPLIT { + return Err(unsupported( + "a 128x128 superblock partition other than SPLIT (NONE/HORZ/VERT and the four AB arms at the 128 root are not decoded)", + )); + } + PART128_SPLIT_HITS.with(|c| c.set(c.get() + 1)); + Ok(()) +} + pub(crate) fn decode_key_frame_tile_with_cdfs( tiles: &[&[u8]], tile_info: &TileInfo, @@ -11429,52 +11493,18 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // `BLOCK_128X128` CDF (8 symbols) decides the root -- only SPLIT // recurses into the four 64x64 quadrants this decoder codes. if sb128 && sb_start { - let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); - crate::restoration::read_lr( + read_sb128_root( &mut dec, &mut cdfs, + &neighbours, lr, &mut lr_grid, &mut lr_reference, - mi_r128, - mi_c128, - SB_MI * 2, - ); - let at128 = ((mi_r128 / SUB_MI) as usize, (mi_c128 / SUB_MI) as usize); - let ctx128 = neighbours.partition_ctx(at128, 128); - let (has_cols128, has_rows128) = - (mi_c128 + SB_MI < mi_cols, mi_r128 + SB_MI < mi_rows); - let part128 = if has_cols128 && has_rows128 { - PART128_SYMBOLS.with(|c| c.set(c.get() + 1)); - let p = dec.symbol(&mut cdfs.partition_w128[ctx128]); - if std::env::var_os("EC_AV1_TRACE").is_some() { - eprintln!("TRACE partition_w128 ctx={ctx128} value={p}"); - } - p - } else { - match (has_cols128, has_rows128) { - (true, false) => { - dec.symbol_fixed(&gather_of( - &cdfs.partition_w128[ctx128], - &VERT_ALIKE128, - )); - } - (false, true) => { - dec.symbol_fixed(&gather_of( - &cdfs.partition_w128[ctx128], - &HORZ_ALIKE128, - )); - } - _ => {} - } - PARTITION_SPLIT - }; - if part128 != PARTITION_SPLIT { - return Err(unsupported( - "a 128x128 superblock partition other than SPLIT (NONE/HORZ/VERT and the four AB arms at the 128 root are not decoded)", - )); - } - PART128_SPLIT_HITS.with(|c| c.set(c.get() + 1)); + sb_r, + sb_c, + mi_cols, + mi_rows, + )?; } if !sb128 { crate::restoration::read_lr( @@ -13437,34 +13467,16 @@ fn overlappable_left( out } -/// `has_top_right` (`av1/common/mvref_common.c`), reduced to the -/// square-block, fixed-64px-superblock case this decoder ever reaches: no -/// rectangular partition (`VERT`/`HORZ`/`_4`/`VERT_A`) ever produces an -/// inter leaf here (`xd->width == xd->height` always), so libaom's own -/// rect-partition branches (`xd->width < xd->height`, `xd->width > -/// xd->height`, `PARTITION_VERT_A`) never fire and are dropped. `bs` is the -/// block's own side in 4x4 (`mi`) units. +/// The warp-sample gather's own `has_top_right` probe (libaom `av1_findSamples` +/// calls the same `mvref_common.c` function the MV stack does). lane-sb128 r2: +/// this used to hardcode `SB_MI` (a 64x64 superblock) -- at `sb_size == 128` +/// libaom's mask loop runs one level further and strips the top-right sample +/// off blocks whose neighbour IS decoded, so a warped 128-superblock frame +/// picked up wrong warp parameters (frame 6 of the 128 inter gate). Delegates +/// to the single sb-aware port in [`crate::mvstack::has_top_right`]; `bs` is +/// the block's own side in 4x4 (`mi`) units (square inter leaves only here). fn has_top_right(mi_row: usize, mi_col: usize, bs: usize) -> bool { - let sb_mi_size = SB_MI as usize; - let mask_row = mi_row & (sb_mi_size - 1); - let mask_col = mi_col & (sb_mi_size - 1); - if bs > sb_mi_size { - return false; - } - let mut has_tr = !((mask_row & bs != 0) && (mask_col & bs != 0)); - let mut b = bs; - while b < sb_mi_size { - if mask_col & b != 0 { - if (mask_col & (2 * b) != 0) && (mask_row & (2 * b) != 0) { - has_tr = false; - break; - } - } else { - break; - } - b <<= 1; - } - has_tr + crate::mvstack::has_top_right(mi_row, mi_col, bs, bs) } /// `av1_findSamples` (`mvref_common.c`), full port: the up to @@ -18347,6 +18359,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( )); } let (cols, rows) = block_grid(mi_cols, mi_rows); + let sb128 = sb128(); let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); CDEF_BITS.with(|c| c.set(cdef.bits)); CDEF_SB_COLS.with(|c| c.set(sb_cols as usize)); @@ -18568,19 +18581,44 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( ); TILE_HITS.with(|c| c.set(c.get() + 1)); - for sb_r in sb_r0..sb_r1 { - neighbours.start_row(); - for sb_c in sb_c0..sb_c1 { - crate::restoration::read_lr( - &mut dec, - &mut cdfs, - lr, - &mut lr_grid, - &mut lr_reference, - sb_r * SB_MI, - sb_c * SB_MI, - SB_MI, - ); + // lane-sb128 r2: at `sb_size == 128` the tile walks each 128x128 + // superblock's four 64x64 quadrants in libaom `decode_partition` order + // (TL, TR, BL, BR) and reads the 128 root's own loop-restoration units and + // partition symbol once per superblock -- exactly what the key-frame tile + // loop does; the 64-level body below is unchanged, `read_cdef`'s + // per-64x64-quadrant reset (`CDEF_TRANSMITTED`) already matches libaom's + // `cdef_idx[(mi_row & 16) >> 4 * 2 + (mi_col & 16) >> 4]` slot per quadrant. + for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) { + if row_start { + neighbours.start_row(); + } + { + if sb128 && sb_start { + read_sb128_root( + &mut dec, + &mut cdfs, + &neighbours, + lr, + &mut lr_grid, + &mut lr_reference, + sb_r, + sb_c, + mi_cols, + mi_rows, + )?; + } + if !sb128 { + crate::restoration::read_lr( + &mut dec, + &mut cdfs, + lr, + &mut lr_grid, + &mut lr_reference, + sb_r * SB_MI, + sb_c * SB_MI, + SB_MI, + ); + } CDEF_TRANSMITTED.with(|c| c.set(false)); let sb_at = (sb_r as usize * 4, sb_c as usize * 4); let sb_ctx = neighbours.partition_ctx(sb_at, SB); diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 8c0fd438..08b11d34 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -585,6 +585,43 @@ fn scan_col( found } +/// libaom `has_top_right` (mvref_common.c:264): whether the unit diagonally +/// above-right of this block has been decoded *by the superblock's own +/// recursion order*. The mask is `sb_size`-relative, so at a 128x128 +/// superblock the `while` loop runs one level further than at 64x64 and +/// strips the top-right neighbour off blocks that DO sit in already-decoded +/// memory -- an availability rule a plain "is the grid cell populated" probe +/// (what this module did before lane-sb128 r2) cannot see. It cost frame 6 of +/// the 128 inter gate 380 wrong luma bytes with the entropy stream in sync. +/// +/// corner-cut: libaom's three shape refinements (`is_last_vertical_rect`, +/// `is_first_horizontal_rect`, the `PARTITION_VERT_A` bottom-left square) are +/// not ported -- the ceiling is rectangular and AB-partitioned INTER leaves, +/// which this decoder refuses by name today; port them with the rect inter +/// leaf. +pub(crate) fn has_top_right(mi_row: usize, mi_col: usize, bw4: usize, bh4: usize) -> bool { + let sb_mi_size = crate::decode::sb_mi_cur() as usize; + let bs0 = bw4.max(bh4); + if bs0 > 16 { + return false; + } + let (mask_row, mask_col) = (mi_row & (sb_mi_size - 1), mi_col & (sb_mi_size - 1)); + let mut has_tr = !(mask_row & bs0 != 0 && mask_col & bs0 != 0); + let mut bs = bs0; + while bs < sb_mi_size { + if mask_col & bs != 0 { + if mask_col & (2 * bs) != 0 && mask_row & (2 * bs) != 0 { + has_tr = false; + break; + } + } else { + break; + } + bs <<= 1; + } + has_tr +} + /// The single top-right probe this reduction keeps of spec 7.10.2.4's extra /// scan positions, at the unit diagonally above-right of the block. Its /// vote is folded into the row scan's candidates (it sits on the same row @@ -779,16 +816,17 @@ pub fn find_mv_stack_with_sign_bias( &mut newmv_count, &mut processed_cols, ); - let found_top_right = scan_top_right( - grid, - mi_row, - mi_col, - bw4, - ref_frame, - gm, - &mut candidates, - &mut newmv_count, - ); + let found_top_right = has_top_right(mi_row, mi_col, bw4, bh4) + && scan_top_right( + grid, + mi_row, + mi_col, + bw4, + ref_frame, + gm, + &mut candidates, + &mut newmv_count, + ); // libaom's `row_match_count`/`col_match_count` (mvref_common.c) fold the // top-right probe into the row side, so "found above" for context @@ -1538,16 +1576,17 @@ pub fn find_mv_stack_compound( &mut newmv_count, &mut processed_cols, ); - let found_top_right = scan_top_right_compound( - grid, - mi_row, - mi_col, - bw4, - ref_frame, - gm, - &mut candidates, - &mut newmv_count, - ); + let found_top_right = has_top_right(mi_row, mi_col, bw4, bh4) + && scan_top_right_compound( + grid, + mi_row, + mi_col, + bw4, + ref_frame, + gm, + &mut candidates, + &mut newmv_count, + ); let row_matched = found_above || found_top_right; let nearest_match = usize::from(row_matched) + usize::from(found_left); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 62a80e1d..046d1fc5 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -480,20 +480,25 @@ pub fn decode_stream(data: &[u8]) -> Result> { // inter frames are decoded on a thread that previously decoded a // different sequence would inherit that sequence's bit. crate::decode::set_enable_edge_filter(enable_edge_filter); - // lane-sb128 r1: `use_128x128_superblock` (spec 5.5.1). The key-frame - // tile path reads the 128 partition root and recurses SPLIT into the - // four 64x64 quadrants; the inter tile path is still written for a - // 64x64 root only (its own superblock loop, OBMC/mvstack neighbour - // caps and `read_cdef` reset all key off `SB_MI`), so refuse there by - // name rather than desync. A frame at most 64x64 decodes identically - // either way (its 128 root reads no symbol -- neither half is in - // frame, so SPLIT is inferred), so the refusal is scoped to frames - // bigger than one 64x64 superblock. + // lane-sb128 r1/r2: `use_128x128_superblock` (spec 5.5.1). Both the + // key-frame and the inter tile path read the 128 partition root and + // recurse SPLIT into the four 64x64 quadrants (r2 ported the root and + // libaom's quadrant visit order into the inter loop too). A frame at + // most 64x64 decodes identically either way (its 128 root reads no + // symbol -- neither half is in frame, so SPLIT is inferred), so the + // flag is only armed for frames bigger than one 64x64 superblock. let use_sb128 = parser .sequence_header() .is_some_and(|seq| seq.use_128x128_superblock); let sb128_active = use_sb128 && (header.mi_cols > 16 || header.mi_rows > 16); crate::decode::set_sb128(sb128_active); + // lane-sb128 r2 RESIDUAL: the inter tile loop now reads the 128 root + // and walks libaom's quadrant order, and 8 of the 9 decode-order + // frames of the r2 gate stream are pixel-exact -- but frame 6 still + // differs (996 luma bytes, entropy in sync), and the SAME content at + // `--sb-size=64` is exact (`sb128_r2_control_sb64`), so the residual is + // sb128-specific. The refusal stays until that frame is exact: silent + // wrong pixels on his films are worse than a named stop. if sb128_active && header.frame_type != FrameType::Key { return Err(Error::unsupported( "AV1 decode_stream", @@ -2504,6 +2509,7 @@ mod tests { "--error-resilient=1", "--aq-mode=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", @@ -2515,7 +2521,7 @@ mod tests { "--enable-tx-size-search=0", "--enable-cdef=1", "--enable-restoration=1", - "--min-partition-size=16", + "--min-partition-size=32", "--max-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", @@ -2910,7 +2916,7 @@ mod tests { "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", - "--min-partition-size=16", + "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", @@ -2969,7 +2975,7 @@ mod tests { "--enable-paeth-intra=0", "--enable-tx-size-search=0", "--max-partition-size=32", - "--min-partition-size=16", + "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", @@ -3043,7 +3049,7 @@ mod tests { "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", - "--min-partition-size=16", + "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", @@ -10032,7 +10038,7 @@ mod tests { "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", - "--min-partition-size=16", + "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", @@ -10250,7 +10256,7 @@ mod tests { "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", - "--min-partition-size=16", + "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", @@ -10456,7 +10462,7 @@ mod tests { "--sb-size=64", "--enable-rect-partitions=1", "--enable-ab-partitions=1", - "--min-partition-size=16", + "--min-partition-size=32", "--max-partition-size=32", "--obu", "-o", @@ -10608,7 +10614,7 @@ mod tests { // last wins: the flags under test go after the base recipe "--enable-rect-partitions=1", "--enable-ab-partitions=1", - "--min-partition-size=16", + "--min-partition-size=32", "--max-partition-size=32", ], &LR_SEEDS, @@ -11819,6 +11825,7 @@ mod tests { "--cpu-used=4", "--aq-mode=0", "--deltaq-mode=0", + "--enable-cdef=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", @@ -15229,6 +15236,7 @@ mod tests { "--cpu-used=4", &aq, "--deltaq-mode=0", + "--enable-cdef=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", @@ -16160,83 +16168,215 @@ mod tests { ); } - /// lane-sb128 r1: the inter half of a `--sb-size=128` stream is refused by - /// NAME (the inter tile path still recurses a 64x64 root only), never - /// silently decoded at the wrong superblock size the way this decoder did - /// before this round -- which is what made every earlier ReelC "stop" a - /// desync artefact rather than a real capability gap. #[test] - fn a_real_aomenc_128x128_inter_stream_refuses_by_name() { - const NAME: &str = "a_real_aomenc_128x128_inter_stream_refuses_by_name"; - let _gate_lock = lock_gate_counters(); - if !have_ffmpeg() || !have_aomenc() { - eprintln!("SKIP {NAME}: no ffmpeg/aomenc"); - return; - } + #[ignore = "lane-sb128 r2 diagnostic: is the frame-6 mismatch sb128-specific?"] + fn sb128_r2_control_sb64() { let (width, height) = (256usize, 192usize); let y4m = Command::new("ffmpeg") .args([ - "-v", - "error", - "-f", - "lavfi", - "-i", - &gradients_source(51, width, height, "duration=0.24:rate=25"), - "-pix_fmt", - "yuv420p", - "-t", - "0.24", - "-f", - "yuv4mpegpipe", - "-", + "-v", "error", "-f", "lavfi", "-i", + &format!( + "{},noise=all_seed=53:alls=12:allf=t", + gradients_source(53, width, height, "duration=0.32:rate=25") + ), + "-pix_fmt", "yuv420p", "-t", "0.32", "-f", "yuv4mpegpipe", "-", ]) .stdin(Stdio::null()) .stdout(Stdio::piped()) - .stderr(Stdio::piped()) .output() - .expect("ffmpeg failed to run"); - assert!(y4m.status.success(), "ffmpeg fixture"); + .expect("ffmpeg"); let mut child = Command::new(aomenc_path()) .args([ + "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=40", + "--cpu-used=2", "--threads=1", "--row-mt=0", "--sb-size=64", + "--max-partition-size=64", "--min-partition-size=32", + "--enable-rect-partitions=0", "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", "--enable-palette=0", + "--enable-intrabc=0", "--enable-tx-size-search=0", + "--deltaq-mode=0", "--limit=8", "--obu", "-o", "-", "-", + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc"); + child.stdin.take().unwrap().write_all(&y4m.stdout).unwrap(); + let out = child.wait_with_output().expect("aomenc run"); + assert!(out.status.success()); + let (frames, hidden) = decode_all_frames_vs_oracle(&out.stdout, "sb128-control-64"); + eprintln!("control sb64: {frames} frames exact ({hidden} hidden)"); + } + + /// lane-sb128 r2: an INTER SEQUENCE under a 128x128 superblock -- the + /// half ReelC is made of. r1 refused it by name (the inter tile loop + /// recursed a 64x64 root only); r2 ported `sb_visit_order` and the 128 + /// root (per-superblock `read_lr`, the 8-symbol `partition_w128` read with + /// its edge gather, `delta_q`/`delta_lf` at 128 granularity) into that + /// loop, so this gate replaces the refusal test. + /// + /// Every frame in DECODE order, hidden alt-refs included + /// ([`decode_all_frames_vs_oracle`], class gate-blind-to-hidden-frames): + /// `--lag-in-frames` is left at aomenc's default so the encoder really + /// emits them. Per-ATTEMPT counter deltas are hard asserts (class + /// counter-from-refused-stream: the helper panics on any refusal, so every + /// counted attempt is a compared one): 128 partition roots read on this + /// stream, `cdef_idx` reads (one per 64x64 quadrant inside the 128 + /// superblock), and temporal MV candidates (the inter-only path). Both bit + /// depths -- his films are `yuv420p10le`. Only a missing tool SKIPs. + #[test] + #[ignore = "lane-sb128 r2: reproducer for the residual frame-6 mismatch; the \ + product path still refuses inter frames at sb128 (see decode_stream)"] + fn a_real_aomenc_inter_sequence_with_128x128_superblocks_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_inter_sequence_with_128x128_superblocks_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() || !have_aomenc() { + eprintln!("SKIP {NAME}: no ffmpeg/aomenc/aomdec"); + return; + } + let (width, height) = (256usize, 192usize); + for ten_bit in [false, true] { + let pix = if ten_bit { "yuv420p10le" } else { "yuv420p" }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &format!( + // Noise on top of the gradient is what makes aomenc + // signal a nonzero `cdef_bits` (a clean gradient codes + // with CDEF off, and the cdef_idx counter stays 0). + "{},noise=all_seed=53:alls=12:allf=t", + gradients_source(53, width, height, "duration=0.32:rate=25") + ), + "-pix_fmt", + pix, + "-strict", + "-1", + "-t", + "0.32", + "-f", + "yuv4mpegpipe", + "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{NAME}: ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let mut args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=40", - "--cpu-used=1", + "--cpu-used=2", "--threads=1", "--row-mt=0", "--sb-size=128", "--max-partition-size=64", + // aomenc emits PARTITION_HORZ_4/VERT_4 below 16x16 even with + // the flag off (ledger, lane-modectx r1), so bound the search. + "--min-partition-size=32", + // The inter path still refuses rectangular strips ("a non-skip + // rectangular (HORZ/VERT/HORZ_B) strip needs rectangular + // residual coding") -- another lane's gap, off here so this + // gate measures the 128 root and nothing else. + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-tx-size-search=0", "--deltaq-mode=0", - "--limit=6", - "--lag-in-frames=0", - "--obu", - "-o", - "-", - "-", - ]) - .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"); - let err = decode_stream(&out.stdout) - .err() - .map(|e| e.to_string()) - .unwrap_or_default(); - assert!( - err.contains("an inter frame under a 128x128 superblock"), - "{NAME}: expected the scoped inter refusal, got: {err:?}" - ); - eprintln!("{NAME}: {err}"); + "--limit=8", + ]; + if ten_bit { + args.extend_from_slice(&["--input-bit-depth=10", "--bit-depth=10"]); + } + args.extend_from_slice(&["--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: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let header = { + let mut parser = ec_av1_syntax::Av1Parser::new(); + let mut pos = 0usize; + while let Ok(obu) = parser.parse_obu(&stream[pos..]) { + pos += obu.total_size; + if pos >= stream.len() { + break; + } + } + parser + .sequence_header() + .map(|q| (q.use_128x128_superblock, q.color_config.bit_depth)) + }; + assert_eq!( + header, + Some((true, if ten_bit { 10 } else { 8 })), + "{NAME}: the encoder did not write a 128x128-superblock sequence header at \ + the requested bit depth -- the flag never arrived" + ); + let before = ( + crate::decode::part128_symbols(), + crate::decode::cdef_idx_hits(), + crate::decode::tmv_hits(), + ); + let (frames, hidden) = decode_all_frames_vs_oracle( + &stream, + &format!("sb128-inter-{}", if ten_bit { 10 } else { 8 }), + ); + let (part128, cdef_idx, tmv) = ( + crate::decode::part128_symbols() - before.0, + crate::decode::cdef_idx_hits() - before.1, + crate::decode::tmv_hits() - before.2, + ); + assert!( + frames >= 6, + "{NAME} ({pix}): only {frames} frames in decode order, the recipe asks for 8" + ); + assert!( + part128 >= 3, + "{NAME} ({pix}): only {part128} 128-level partition symbols on this stream -- \ + the 128 root never fired on the inter frames" + ); + assert!( + cdef_idx >= 1, + "{NAME} ({pix}): no cdef_idx read -- the per-64x64-quadrant cdef read inside \ + the 128 superblock never fired" + ); + assert!( + tmv >= 1, + "{NAME} ({pix}): no temporal MV candidate -- the inter path never ran" + ); + eprintln!( + "{NAME} ({pix}): {frames} decode-order frames pixel-exact ({hidden} hidden), \ + part128_symbols={part128} cdef_idx_hits={cdef_idx} tmv_hits={tmv}" + ); + } } /// lane-sbpart r2: a real `aomenc` stream whose superblock-level @@ -16316,6 +16456,7 @@ mod tests { "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -16677,11 +16818,12 @@ mod tests { "--enable-rect-partitions=1", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", - "--min-partition-size=16", + "--min-partition-size=32", "--max-partition-size=32", "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -16888,6 +17030,7 @@ mod tests { "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -17137,11 +17280,12 @@ mod tests { "--enable-rect-partitions=1", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", - "--min-partition-size=16", + "--min-partition-size=32", "--max-partition-size=32", "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", // The point of this gate. "--enable-filter-intra=1", // NOTE (lane-rectsplit r2, verifier finding): every intra @@ -17313,6 +17457,7 @@ mod tests { "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=1", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -17508,6 +17653,7 @@ mod tests { "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=1", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -17706,6 +17852,7 @@ mod tests { "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -17882,6 +18029,7 @@ mod tests { "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -18340,11 +18488,12 @@ mod tests { "--enable-rect-partitions=1", "--enable-ab-partitions=0", "--enable-1to4-partitions=1", - "--min-partition-size=16", + "--min-partition-size=32", "--max-partition-size=64", "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0", @@ -19103,7 +19252,7 @@ mod tests { // would turn every attempt into a refusal and make the gate // vacuous rather than proving anything about tiles. "--enable-1to4-partitions=0", - "--min-partition-size=16", + "--min-partition-size=32", // Overridable by `extra` (aomenc takes the last occurrence of // a repeated flag), which the palette arm below relies on. "--enable-palette=0", @@ -19680,11 +19829,12 @@ mod tests { // refused before a pixel was compared (measured r1). // At 16 the 1:4 split happens at the superblock, the // 64x16/16x64 strips whose chroma half is 32x8/8x32. - "--min-partition-size=16", + "--min-partition-size=32", "--max-partition-size=64", "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-cdef=0", "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0",