diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 6cb769cc..fae5ee61 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2521,6 +2521,25 @@ thread_local! { const { std::cell::Cell::new([0; 3]) }; } +// lane-t900 r21: rect intra strips inside an inter frame that read the +// screen-content palette syntax (`allow_screen_content_tools` set and the +// strip's own shape admitted by `palette_bsize_ctx_wh`) -- the site a blanket +// refusal used to guard, so this counter is what keeps its witness gate +// non-vacuous. +thread_local! { + static INTRA_RECT_IN_INTER_SCREEN_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`INTRA_RECT_IN_INTER_SCREEN_HITS`]. +pub(crate) fn intra_rect_in_inter_screen_hits() -> usize { + INTRA_RECT_IN_INTER_SCREEN_HITS.with(std::cell::Cell::get) +} + +/// Resets [`INTRA_RECT_IN_INTER_SCREEN_HITS`] before a decode. +pub(crate) fn reset_intra_rect_in_inter_screen_hits() { + INTRA_RECT_IN_INTER_SCREEN_HITS.with(|c| c.set(0)); +} + /// Current value of [`INTRA_RECT_IN_INTER_SPLIT_TX_HITS`], indexed by tx depth. pub(crate) fn intra_rect_in_inter_split_tx_hits() -> [usize; 3] { INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) @@ -7852,15 +7871,14 @@ fn decode_intra_rect_in_inter( "an intra-coded {bw}x{bh} block on the inter block path (no size-group/tx-category row for that shape here)" ))); }; - // [`read_intra_mode_rect`]'s own refusal: palette/intrabc syntax is - // consumed for square blocks only, so a strip in a screen-content frame - // would skip symbols the encoder wrote. - if allow_screen_content_tools { - return Err(unsupported( - "a HORZ/VERT intra strip in a screen-content frame (palette syntax \ - is consumed for square blocks only)", - )); - } + // lane-t900 r21: this arm used to refuse EVERY rect strip of a + // screen-content frame ("palette syntax is consumed for square blocks + // only"). It is not: [`read_intra_mode_rect`] generalised the palette read + // to rect blocks on the key-frame path, and the 1:4 arm below goes through + // it, while the 2:1 reader inlined further down now codes the same syntax + // itself. A real `--tune-content=screen` inter stream reached this refusal + // on 8x16 / 16x8 strips (gate + // `a_real_aomenc_screen_inter_sequence_codes_palette_syntax_on_rect_intra_strips`). // lane-intra14 r1: a 1:4 strip decodes through the very readers the KEY // FRAME path already proves for this shape ([`decode_block_rect4`] at the // 32 level, [`decode_block_rect64`] at 64 -- both already handle the 1:4 @@ -7991,6 +8009,32 @@ fn decode_intra_rect_in_inter( UV_ANGLE_DELTA_HITS.with(|h| h.set(h.get() + 1)); INTRA_IN_INTER_ANGLE_DELTA_UV_HITS.with(|h| h.set(h.get() + 1)); } + // `read_palette_mode_info` (spec 5.11.13), in the position + // `read_intra_block_mode_info` writes it: after the chroma mode and + // before `filter_intra`. lane-t900 r21: a `--tune-content=screen` inter + // frame writes `palette_y_mode`/`palette_uv_mode` for every block whose + // shape [`palette_bsize_ctx_wh`] admits, intra strips included, so + // skipping the syntax here desynced the tile at the strip's own symbol. + // No block this decoder produces can USE a palette (the arms below refuse + // by name), so the neighbour mode context is 0 and there is no cache -- + // the same stand-in [`read_intra_mode_rect`] uses when its caller has none. + if allow_screen_content_tools + && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) + { + INTRA_RECT_IN_INTER_SCREEN_HITS.with(|c| c.set(c.get() + 1)); + if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][0]) != 0 { + return Err(unsupported( + "a block that actually uses a palette (Y) -- reconstruction is out of scope", + )); + } + // A 2:1 strip of at least 8x8 is always a chroma reference, so + // `read_palette_mode_info`'s `xd->is_chroma_ref` guard holds here. + if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 { + return Err(unsupported( + "a block that actually uses a palette (UV) -- reconstruction is out of scope", + )); + } + } // lane-fiinter (merged on main for the square intra-in-inter arm): // `read_filter_intra_mode_info` is the tail of // `read_intra_block_mode_info`, after the palette syntax (refused whole diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 30d6a059..8935410c 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -103,8 +103,17 @@ const REFUSALS: &[&str] = &[ // what is left is the 1:4 shape, whose `bsize_to_tx_size_cat` breaks the // size-group/category diagonal the 2:1 shapes share. "an intra-coded {bw}x{bh} block on the inter block path (no size-group/tx-category row for that shape here)", - // The same arm's screen-content gate: palette/intrabc syntax is consumed - // for square blocks only, so a strip in such a frame would skip symbols. + // lane-t900 r21 NARROWED this: the string used to guard EVERY rect intra + // strip of a screen-content inter frame, and a real + // `--tune-content=screen` stream reached it on an 8x16 (8-bit) / 16x8 + // (10-bit) strip. The 2:1 reader now codes the palette syntax itself and + // the 1:4 arm goes through `read_intra_mode_rect`, which already did -- + // both arms of gate + // `a_real_aomenc_screen_inter_sequence_codes_palette_syntax_on_rect_intra_strips` + // decode ten frames pixel-exact with the strips counted. What still + // refuses is a 16x4/4x16 strip and a 128-root half, neither of which is + // palette-eligible (`palette_bsize_ctx_wh` wants `8 <= min`, `max <= 64`) + // and neither of which has a witness stream yet. "a HORZ/VERT intra strip in a screen-content frame (palette syntax is consumed for square blocks only)", "warp prediction with a scaled reference (superres, unimplemented)", "an 8x8 partition leaf under a scaled reference (superres, unimplemented)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index d23a281b..3553e151 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -30179,6 +30179,143 @@ mod tests { ); } + /// lane-t900 r21: the witness for the palette SYNTAX on a rect intra strip + /// inside a screen-content INTER frame. + /// + /// `read_palette_mode_info` is gated by the frame's + /// `allow_screen_content_tools` and by the block's own shape + /// (`palette_bsize_ctx_wh`: `min(w,h) >= 8`, `max(w,h) <= 64`), + /// with no square-only term -- so a `--tune-content=screen` inter frame + /// writes `palette_y_mode` (and `palette_uv_mode`) for an 8x16 or 16x8 + /// intra strip exactly as for a square block. This decoder's inter-path + /// intra arm skipped both symbols and refused the whole case by name ("a + /// HORZ/VERT intra strip in a screen-content frame (palette syntax is + /// consumed for square blocks only)"), which a real stream reached: + /// measured with `decode_probe`, `testsrc2` 256x192 at + /// `--tune-content=screen --enable-palette=0 --enable-intrabc=0` stops on + /// that refusal at an 8x16 strip (8-bit) / a 16x8 one (10-bit). + /// + /// Both arms now decode all ten frames pixel-exact against ffmpeg, and + /// [`decode::intra_rect_in_inter_screen_hits`] counts the strips that read + /// the syntax -- hard-asserted, because an attempt with no such strip + /// proves nothing about the case the refusal named. + /// + /// Note `--enable-palette=0`: the encoder's palette SEARCH is what this + /// gate must switch off, not the syntax. With it on, the key frame codes + /// a block that really uses a palette and stops on the separate, + /// still-live "a block that actually uses a palette (Y)" refusal before + /// any inter frame is reached (class `refusal-hides-a-defect`, measured on + /// six such encodes). + #[test] + fn a_real_aomenc_screen_inter_sequence_codes_palette_syntax_on_rect_intra_strips() { + screen_rect_intra_palette_syntax_gate(8); + } + + /// The 10-bit arm (both of the user's films are `yuv420p10le`). + #[test] + fn a_real_aomenc_10bit_screen_inter_sequence_codes_palette_syntax_on_rect_intra_strips() { + screen_rect_intra_palette_syntax_gate(10); + } + + fn screen_rect_intra_palette_syntax_gate(bit_depth: u32) { + let name = format!("screen_rect_intra_palette_syntax_gate({bit_depth})"); + 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, frame_count) = (256usize, 192usize, 10usize); + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", "testsrc2=s=256x192:r=25", "-t", "0.4", + "-pix_fmt", "yuv420p", "-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: {}", String::from_utf8_lossy(&y4m.stderr)); + // The source stays 8-bit for both arms: `--bit-depth=10 + // --input-bit-depth=8` is the recipe that was measured to reach the + // refusal, and the arm's point is the coded syntax, not the input. + let depth_arg = format!("--bit-depth={bit_depth}"); + let cq_arg = format!("--cq-level={}", if bit_depth == 10 { 45 } else { 40 }); + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cpu-used=0", + "--lag-in-frames=0", + "--kf-max-dist=30", + "--limit=10", + "--threads=1", + "--tile-columns=0", + "--enable-rect-partitions=1", + "--enable-1to4-partitions=1", + "--min-partition-size=8", + "--sb-size=64", + "--input-bit-depth=8", + // `allow_screen_content_tools` in every frame header comes + // from here; the two searches below stay off so the KEY frame + // does not stop on a palette/intrabc RECONSTRUCTION refusal + // before an inter frame exists. + "--tune-content=screen", + "--enable-palette=0", + "--enable-intrabc=0", + ]) + .args([&depth_arg, &cq_arg]) + .args(["--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) + .or_else(|e| match e.kind() { + std::io::ErrorKind::BrokenPipe => Ok(()), + _ => Err(e), + }) + .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; + decode::reset_intra_rect_in_inter_screen_hits(); + let frames = decode_stream(&stream).unwrap_or_else(|e| { + panic!("{name}: decode failed -- the case this gate exists for: {e}") + }); + let strips = decode::intra_rect_in_inter_screen_hits(); + eprintln!("{name}: {} frames, {strips} rect intra strip(s) read the palette syntax", frames.len()); + assert_eq!(frames.len(), frame_count, "{name}: frame count"); + let ffmpeg_frames = if bit_depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) + } else { + ffmpeg_decode_sequence(&stream, width, height, frame_count) + }; + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{name}: frame {i} luma vs ffmpeg"); + assert_eq!(got.u, want.u, "{name}: frame {i} U vs ffmpeg"); + assert_eq!(got.v, want.v, "{name}: frame {i} V vs ffmpeg"); + } + assert!( + strips > 0, + "{name}: no rect intra strip of a screen-content frame read the palette syntax -- \ + the gate is vacuous, the recipe stopped producing the shape" + ); + } + /// lane-intra14 r4: the FILM witness for an intra block on a 1:4 partition /// inside an INTER frame -- `crates/ec-av1/fixtures/reelb_intra14_witness.obu`, /// 163057 bytes, sha256