diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 2b644c65..bca10580 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -37,6 +37,11 @@ fn main() { println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); let leaf = ec_av1::stream::vartx_rect_leaf_hits(); println!("vartx_rect_leaf: 32x16={} 16x32={}", leaf[0], leaf[1]); + let sb = ec_av1::stream::sb128_rect_counters(); + println!( + "sb128_rect: edge_horz={} edge_vert={} inter_128x64={} inter_64x128={}", + sb.0, sb.1, sb.2, sb.3 + ); let ir = ec_av1::stream::inter_rect_counters(); println!( "inter_rect: 32x8={} 8x32={} 64x32={} 32x64={} 64x16={} 16x64={}", diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 997bda47..35d8f9e3 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3946,6 +3946,18 @@ impl Neighbours { /// `cols`/`rows` are in [`SUB`] units; `mi_cols`/`mi_rows` are the frame's /// true (unpadded) size in 4x4 mode-info units. fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self { + // lane-sb128c r7: a 128x64 block at the bottom frame edge records its + // neighbour state over the whole 64-row extent, past the frame's last + // mi row (mi_rows 198, block rows 192..208). libaom's own context + // buffers are superblock-aligned for exactly that reason + // (`av1_alloc_context_buffers` rounds to `MAX_MIB_SIZE`), so pad the + // side arrays to a superblock multiple and leave those cells unread -- + // every availability check uses the true `mi_rows`/`mi_cols` below. + let sb_sub = (sb_mi_cur() as usize) / (SUB / MI); + let (cols, rows) = ( + cols.next_multiple_of(sb_sub), + rows.next_multiple_of(sb_sub), + ); Self { above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], @@ -5015,7 +5027,14 @@ impl Neighbours { round_up_even(self.mi_cols), ]; for cell in 0..h_mi { - self.left[mi_r + cell] = std::array::from_fn(|plane| { + // lane-sb128c r7: a 128x64 at the bottom frame edge names mi rows + // past the padded side arrays (mi_rows 198, block rows 192..208); + // libaom's own arrays are superblock-sized, so those cells simply + // have no reader. Same guard the `left_side_mi` loop above uses. + let Some(left_slot) = self.left.get_mut(mi_r + cell) else { + break; + }; + *left_slot = std::array::from_fn(|plane| { if cell < h_mi.min(bound_h[plane].saturating_sub(mi_r)) { states[plane] } else { @@ -5024,7 +5043,10 @@ impl Neighbours { }); } for cell in 0..w_mi { - self.above[mi_c + cell] = std::array::from_fn(|plane| { + let Some(above_slot) = self.above.get_mut(mi_c + cell) else { + break; + }; + *above_slot = std::array::from_fn(|plane| { if cell < w_mi.min(bound_w[plane].saturating_sub(mi_c)) { states[plane] } else { @@ -13675,6 +13697,16 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( mi_cols, mi_rows, )?; + if matches!(part128, PARTITION_HORZ | PARTITION_VERT) { + // lane-sb128c r7: the INTER path decodes these (two 128x64 + // / two 64x128 blocks, the film's own bottom-edge shape); + // the key path's `decode_block` is square-only, so an + // intra 128x64 would silently decode as four 64x64 + // quadrants. Refuse by name instead of corrupting. + return Err(unsupported( + "a 128x128 superblock HORZ/VERT partition on an intra frame (the inter path decodes it; the intra one is square-block only)", + )); + } if part128 == PARTITION_NONE { // One 128x128 intra block: four TX_64X64 luma units and a // single 64x64 chroma unit per plane (`av1_get_max_uv_txsize` diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 7ae4a7a0..f8fb077e 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -43,7 +43,8 @@ const REFUSALS: &[&str] = &[ "a tx_type symbol outside its CDF's own set: {t}", "an INTER 32x32 partition type this decoder does not code (value={part32})", "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", - "a 128x128 superblock HORZ/VERT or AB partition (only SPLIT and NONE are decoded at the 128 root)", + "a 128x128 superblock AB partition (HORZ_A/HORZ_B/VERT_A/VERT_B at the 128 root)", + "a 128x128 superblock HORZ/VERT partition on an intra frame (the inter path decodes it; the intra one is square-block only)", "intrabc under a 128x128 superblock (libaom's av1_is_dv_valid derives the block-vector delay from sb_size, which this decoder hardcodes to 64)", "a palette block on a HORZ/VERT intra strip below 16x16 (reconstruction not ported)", "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index af66e3de..189876f2 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -81,6 +81,17 @@ pub fn vartx_rect_leaf_hits() -> [usize; 2] { crate::decode::vartx_rect_leaf_hits() } +/// lane-sb128c r7: the 128 root's rect arms -- (gathered edge HORZ, gathered +/// edge VERT, inter 128x64 blocks, inter 64x128 blocks). +pub fn sb128_rect_counters() -> (usize, usize, usize, usize) { + ( + crate::decode::part128_edge_horz_hits(), + crate::decode::part128_edge_vert_hits(), + crate::decode::inter_sb128_horz_hits(), + crate::decode::inter_sb128_vert_hits(), + ) +} + pub fn rect4_32_counters() -> (usize, usize, usize) { ( crate::decode::rect4_32_horz_hits(), @@ -18953,6 +18964,173 @@ mod tests { ); } + /// lane-sb128c r7 GATE: the 128 root's GATHERED bottom/right-edge arm -- + /// the only 128-partition shape the user's 1080p AV1 film reaches + /// (`mi_rows = 270`, so every bottom 128 row has `has_rows128 == false`, + /// one gathered bit, and the film codes `PARTITION_HORZ`: a 128x64 block + /// whose bottom half is outside the frame and never decoded). Frame sizes + /// here are chosen so the LAST superblock row (or column, for the VERT + /// twin at 192x256) is partial, which is what forces the gathered symbol. + /// Both the key frame and the inter frames of each stream carry the arm; + /// `part128_edge_*_hits` and `inter_sb128_*_hits` HARD-assert it fired, + /// and `decode_all_frames_vs_oracle` compares EVERY decode-order frame + /// (hidden alt-refs included) against the instrumented aomdec. + #[test] + fn a_real_aomenc_sb128_gathered_edge_horz_partition_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_sb128_gathered_edge_horz_partition_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; + } + // (seed, cq, width, height, 10-bit, vertical twin). Every size has a + // PARTIAL last superblock row (or column, for the VERT twin): 320 px + // = 80 mi rows, two full 128 rows and a 64-px remainder; 256 px = 64 + // mi = two full rows and none, so the VERT arm uses 192 px = 48 mi on + // the column axis. A smooth `gradients` source makes the KEY frame + // take the arm too, which is still refused by name (square-block + // intra), so this gate's source is `mandelbrot`: its key frames split + // and its inter frames keep the 128x64/64x128 edge block. + let arms: Vec<(u32, &str, usize, usize, bool, bool)> = vec![ + (42, "--cq-level=55", 384, 320, false, false), + (43, "--cq-level=55", 384, 320, true, false), + (44, "--cq-level=32", 192, 256, false, true), + (45, "--cq-level=45", 192, 256, true, true), + ]; + let (mut matched, mut named_refusals) = (0u32, 0u32); + let (mut edge_arms, mut inter_arms) = (0u32, 0u32); + for (seed, cq, width, height, ten_bit, vert) in arms { + let pix = if ten_bit { "yuv420p10le" } else { "yuv420p" }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", + &format!( + "mandelbrot=size={width}x{height}:rate=25:start_x=-0.6:start_y=-0.4" + ), + "-pix_fmt", pix, "-strict", "-1", "-t", "0.2", + "-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, + "--cpu-used=0", "--threads=1", "--row-mt=0", + "--sb-size=128", "--min-partition-size=64", + "--enable-rect-partitions=1", "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", "--enable-palette=0", + "--enable-intrabc=0", "--enable-tx-size-search=0", + "--deltaq-mode=0", "--limit=5", + ]; + 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 (before_edge, before_inter) = if vert { + ( + crate::decode::part128_edge_vert_hits(), + crate::decode::inter_sb128_vert_hits(), + ) + } else { + ( + crate::decode::part128_edge_horz_hits(), + crate::decode::inter_sb128_horz_hits(), + ) + }; + // A refusal is still a legal outcome for an arm whose stream hits + // some OTHER lane's gap, but never for all of them (asserted + // below), and never as a silent skip. + if let Err(e) = decode_stream(&stream) { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal \ + (seed {seed} {width}x{height}): {msg}" + ); + named_refusals += 1; + eprintln!("{NAME}: seed {seed} {width}x{height} refusal: {msg}"); + continue; + } + let (frames, hidden) = decode_all_frames_vs_oracle( + &stream, + &format!("sb128c-edge-{seed}-{width}x{height}"), + ); + assert!(frames > 0, "{NAME}: no frames decoded at {width}x{height}"); + let (edge, inter) = if vert { + ( + crate::decode::part128_edge_vert_hits() - before_edge, + crate::decode::inter_sb128_vert_hits() - before_inter, + ) + } else { + ( + crate::decode::part128_edge_horz_hits() - before_edge, + crate::decode::inter_sb128_horz_hits() - before_inter, + ) + }; + eprintln!( + "{NAME}: seed {seed} cq {cq} {width}x{height} {}bit {}: \ + {frames} frames pixel-exact ({hidden} hidden), \ + edge_hits +{edge} inter_128rect_blocks +{inter}", + if ten_bit { 10 } else { 8 }, + if vert { "VERT" } else { "HORZ" }, + ); + if edge > 0 { + edge_arms += 1; + } + if inter > 0 { + inter_arms += 1; + } + matched += 1; + } + assert!( + matched > 0, + "{NAME}: every attempt refused ({named_refusals}); the gate never \ + decoded a stream" + ); + assert!( + edge_arms > 0, + "{NAME}: {matched} stream(s) decoded pixel-exact but NOT ONE 128 root \ + took the gathered frame-edge HORZ/VERT arm this gate exists for" + ); + assert!( + inter_arms > 0, + "{NAME}: the gathered arm fired only on key frames -- no INTER \ + 128x64/64x128 block was decoded, which is the film's own shape" + ); + } + #[test] #[ignore = "lane-sb128 r2 diagnostic: is the frame-6 mismatch sb128-specific?"] fn sb128_r2_control_sb64() {