From 514765196a3d6f7e584453fe8e9cb9075b06a97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 19:56:33 +0300 Subject: [PATCH] feat(av1): gate the V_DCT/H_DCT class-1 path on real aomenc bytes The sub-8 refusal verdict, the fixture recipe, and a tx_class1_hits counter: a min/max-partition-8 horizontal-stripe stream fires H_DCT through the class-split eob path and decodes full-frame pixel-exact vs ffmpeg, 4/4 runs. --- crates/ec-av1/src/decode.rs | 42 +++++++++- crates/ec-av1/src/stream.rs | 156 ++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+), 2 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index d8048f81..1914c2f9 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -74,6 +74,19 @@ static TX_DEPTH_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::Atomic pub(crate) fn tx_depth_hits() -> usize { TX_DEPTH_HITS.load(std::sync::atomic::Ordering::Relaxed) } + +/// How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D` +/// (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every +/// call in the process -- the same before/after counter pattern as +/// [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split +/// `eob_pt`/1D-neighbour context path rather than every block coincidentally +/// landing on `DCT_DCT`. +static TX_CLASS1_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// Current value of [`TX_CLASS1_HITS`]. +pub(crate) fn tx_class1_hits() -> usize { + TX_CLASS1_HITS.load(std::sync::atomic::Ordering::Relaxed) +} const SUB_MI: u32 = 4; const MI: usize = 4; const SB: usize = 64; @@ -225,7 +238,15 @@ fn nz_map_ctx_offset_1d(i: usize) -> usize { } fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize { - if row == 0 && col == 0 { + // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC + // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`, + // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC + // position through the full 1D neighbour sum and offset table. Skipping + // that for every class desynced the last (DC) coefficient of a + // `TX_CLASS_HORIZ` block (lane-av1tx4 final round: real ctx 28 read as + // 0, which then read one extra `BR` symbol at the max and forced a + // spurious third Golomb call). + if class == TxClass::TwoD && row == 0 && col == 0 { return 0; } let offsets: [(usize, usize); 5] = match class { @@ -383,6 +404,9 @@ fn read_coeffs( } let class = TxClass::of(tx_type); + if class != TxClass::TwoD { + TX_CLASS1_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } let eob = read_eob(dec, coding, class); if trace { eprintln!("TRACE eob value={eob}"); @@ -2814,7 +2838,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let part32 = if has_cols32 && has_rows32 { let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); if std::env::var_os("EC_AV1_TRACE").is_some() { - eprintln!("TRACE partition_w32 ctx={ctx32} value={p}"); + eprintln!( + "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p}", + r32 * BLOCK_MI, + c32 * BLOCK_MI + ); } p } else { @@ -2934,6 +2962,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!( + "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}" + ); + } if part8 != PARTITION_NONE { return Err(unsupported( "a partition below 8x8 (this encoder never writes one)", @@ -2998,6 +3031,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let leaf_mi = (mr as usize, mc as usize); let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!( + "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}" + ); + } if part8 != PARTITION_NONE { return Err(unsupported( "a partition below 8x8 (this encoder never writes one)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index ec9afea8..31bf101e 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1551,4 +1551,160 @@ mod tests { } } } + + /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=8 + /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec + /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame + /// never triggers), so a real `aomenc` stream from this recipe can only + /// legitimately read `PARTITION_NONE` at every `bsize=3` node -- any + /// other value this decoder sees there is a genuine desync, not a + /// "partition below 8x8" the encoder actually wrote (traced live against + /// a debug `aomdec` 2026-08-27: the true bitstream's own 8x8 partition + /// reads are all `NONE`; the mismatch was `base_ctx`'s DC-position + /// short-circuit firing for `TX_CLASS_HORIZ`/`VERT` too, off by one + /// `BR` read into a spurious extra Golomb call). The sinusoidal-stripe + /// `geq` source (a smooth luma ramp, not the sharp two-tone stripes that + /// tripped `aomenc`'s screen-content-tools auto-detect) is what actually + /// resolves `V_DCT`/`H_DCT` (`TxClass::of`) on some 8x8 TUs at this + /// content's frequency; the attempt loop moves on from named refusals + /// (`allow_screen_content_tools`, angle-delta) same as every other + /// multi-seed recipe in this file, and only counts a run as a genuine + /// firing when [`decode::tx_class1_hits`] actually moved. + #[test] + fn a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact() { + if !have_ffmpeg() { + eprintln!( + "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no ffmpeg" + ); + return; + } + if !have_aomenc() { + eprintln!( + "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no aomenc at {}", + aomenc_path().display() + ); + return; + } + let (width, height) = (64usize, 64usize); + let mut refusals = Vec::new(); + let mut fired_runs = 0u32; + for attempt in 0..60u32 { + let cq = 20 + (attempt % 5) * 10; + let period = [4u32, 6, 8, 12, 16][(attempt as usize / 5) % 5]; + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + "testsrc2=size=64x64:rate=25", + "-vf", + &format!( + "geq=lum='128+80*sin(2*PI*Y/{period})':cb=128:cr=128,noise=alls=6:allf=t" + ), + "-pix_fmt", + "yuv420p", + "-t", + "0.04", + "-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", + &format!("--cq-level={cq}"), + "--cpu-used=0", + "--enable-directional-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-rect-partitions=0", + "--enable-angle-delta=0", + "--enable-palette=0", + "--reduced-tx-type-set=0", + "--enable-restoration=0", + "--enable-cdef=0", + "--loopfilter-control=0", + "--min-partition-size=8", + "--max-partition-size=8", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--tune-content=default", + "--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"); + if !out.status.success() { + refusals.push(format!( + "cq={cq} period={period}: aomenc itself refused the fixture" + )); + continue; + } + let stream = out.stdout; + let before = decode::tx_class1_hits(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + refusals.push(format!("cq={cq} period={period}: {e}")); + continue; + } + }; + if decode::tx_class1_hits() == before { + refusals.push(format!( + "cq={cq} period={period}: decoded, but no block read a V_DCT/H_DCT tx_type" + )); + continue; + } + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); + assert_eq!( + frames[0].y, ffmpeg_frames[0].y, + "luma vs ffmpeg (cq={cq} period={period})" + ); + assert_eq!( + frames[0].u, ffmpeg_frames[0].u, + "U vs ffmpeg (cq={cq} period={period})" + ); + assert_eq!( + frames[0].v, ffmpeg_frames[0].v, + "V vs ffmpeg (cq={cq} period={period})" + ); + fired_runs += 1; + if fired_runs >= 4 { + return; + } + } + panic!( + "fewer than 4 firing+pixel-exact runs out of 60 attempts:\n{}", + refusals.join("\n") + ); + } }