From b50bec7bf5d5163177211ce1e7e88f2733458314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 17:08:03 +0300 Subject: [PATCH] test(av1): gate tile rows below decode_stream's refusal (bypass) a_real_aomenc_stream_with_two_tile_rows_decodes_pixel_exact -- 64x128, --tile-rows=1 (TileRows=2), calls decode_key_frame_tile_with_cdfs directly (decode_stream still refuses tile_info.rows > 1). Loop filter left ON per the charter, not bypassed with --loopfilter-control=0 -- deblock_plane runs once over the whole picture with no tile-axis distinction, and the per-tile loop is already row/col-generic (tile_num / tile_info.cols, set_tile_origin's y0/y1 symmetric with the column gate's x0/x1). 20/20 pixel-exact vs ffmpeg, 0 named refusals, hard tile_hits > 1 assert. Proves the underlying row decode + row deblocking correct before the refusal is touched. --- crates/ec-av1/src/stream.rs | 205 ++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 94285209..7bfa18d0 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6221,6 +6221,211 @@ mod tests { ); } + /// lane-tiles r8 stage 1: the same bypass-the-refusal pattern as the + /// two-tile-column gate above, this time for tile ROWS -- `decode_stream` + /// still refuses any `tile_info.rows > 1` stream, so this gate calls + /// [`decode_key_frame_tile_with_cdfs`] directly to prove the underlying + /// per-tile loop (already row/col-generic: `tile_num / tile_info.cols` + /// gives the row, `mi_row_starts`/`set_tile_origin`'s `y0`/`y1` are + /// exactly symmetric with the column gate's `x0`/`x1`) actually decodes + /// both tile rows pixel-exact before the refusal is lifted. 64x128 is + /// one 64px superblock wide and two tall with `--tile-rows=1` + /// (`TileRows=2`, `--tile-columns=0` keeps `cols=1` so only the row axis + /// is under test). Loop filter is left ON (no + /// `--loopfilter-control=0`) per the charter -- deblocking crosses tile + /// row boundaries by spec default exactly as it crosses column ones, and + /// `deblock_plane` runs once over the whole decoded picture after every + /// tile is in, with no tile-axis distinction, so there is no reason to + /// expect the row boundary to behave differently from the column one r7 + /// already proved -- this gate is what actually proves it either way. + #[test] + fn a_real_aomenc_stream_with_two_tile_rows_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_stream_with_two_tile_rows_decodes_pixel_exact"; + 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) = (64usize, 128usize); + let mut matched = 0u32; + let mut named_refusals = 0u32; + for attempt in 0..20u32 { + let seed = 42 + attempt; + let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &source, + "-pix_fmt", + "yuv420p", + "-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) + ); + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cq-level=45", + "--cpu-used=0", + "--limit=1", + "--kf-max-dist=1000", + "--threads=1", + "--row-mt=0", + "--tile-columns=0", + "--tile-rows=1", + // This decoder hardcodes SB_MI=16 (64px) superblocks + // everywhere; without this aomenc's own default 128x128 + // superblock makes 64x128 exactly ONE superblock tall, + // so `--tile-rows=1` has nothing to split. + "--sb-size=64", + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-directional-intra=0", + "--enable-angle-delta=0", + "--enable-tx-size-search=0", + "--enable-cdef=0", + "--enable-restoration=0", + "--max-partition-size=32", + "--min-partition-size=32", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=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: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + + let mut parser = Av1Parser::new(); + let mut pos = 0usize; + let mut frame = None; + while pos < stream.len() { + let obu_offset = pos; + let obu = parser.parse_obu(&stream[pos..]).unwrap(); + pos += obu.total_size; + if let ObuKind::Frame(header, tiles) = obu.kind { + frame = Some((header, tiles, obu_offset)); + break; + } + } + let (header, tiles, obu_offset) = frame.expect("stream has a Frame OBU"); + assert!( + header.tile_info.rows > 1, + "{NAME}: aomenc did not actually split into >1 tile row (rows={}, seed {seed})", + header.tile_info.rows + ); + assert_eq!( + tiles.len(), + header.tile_info.rows as usize, + "{NAME}: expected one tile per row (cols=1, seed {seed})" + ); + let tile_bufs: Vec<&[u8]> = tiles + .iter() + .map(|t| &stream[obu_offset + t.offset..obu_offset + t.offset + t.size]) + .collect(); + + let enable_filter_intra = parser + .sequence_header() + .is_some_and(|seq| seq.enable_filter_intra); + let enable_edge_filter = parser + .sequence_header() + .is_some_and(|seq| seq.enable_intra_edge_filter); + + let before = crate::decode::tile_hits(); + let picture = match decode_key_frame_tile_with_cdfs( + &tile_bufs, + &header.tile_info, + header.mi_cols, + header.mi_rows, + header.quantization.base_q_idx, + header.frame_width, + header.frame_height, + enable_filter_intra, + enable_edge_filter, + &header.cdef, + &header.loop_filter, + None, + header.tx_mode == TxMode::Select, + header.reduced_tx_set, + header.allow_screen_content_tools, + header.allow_intrabc, + ) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" + ); + named_refusals += 1; + eprintln!("seed {seed} refusal: {msg}"); + continue; + } + Ok((picture, _end_cdfs)) => picture, + }; + let hits = crate::decode::tile_hits() - before; + assert!( + hits > 1, + "{NAME}: tile_hits delta was {hits}, expected >1 -- both tile rows must actually \ + decode, not just tile 0 (seed {seed})" + ); + + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); + assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); + assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); + assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); + matched += 1; + } + assert!( + matched > 0, + "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \ + a two-tile-row stream" + ); + eprintln!( + "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" + ); + } + /// lane-tiles r7: the `>2 tile columns` refusal's own capability, proven /// separately from the 2-column gate above -- `--tile-columns=2` gives /// aomenc `TileCols=4` (log2), a real non-last-column right-edge case the