From beeeec5972c4e7264be4f38a4a97b46b902af65a 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 11:53:59 +0300 Subject: [PATCH] fix(av1): read the unread use_filter_intra flag; audit the same blindness class lane-av1real r10 added the FILTER_INTRA/FILTER_INTRA_MODE CDF tables and Cdfs fields but stopped mid-wiring; this finishes threading enable_filter_intra (from the sequence header) through decode_key_frame_tile -> decode_block/decode_leaf8 -> read_intra_mode, which now reads use_filter_intra after DC_PRED at <=32x32 (spec 5.11.14, av1_filter_intra_allowed) and refuses use_filter_intra==1 blocks (this decoder does not implement the 5.11.14 recursive predictor) rather than silently miscoding. The pinned CfL stream's actual bug was exactly this: its block reads use_filter_intra=0, so the flag's own bits were never consumed before, desyncing every later symbol read self-consistently -- both the Python cross-checks and debug aomdec traces shared that blindness. With the flag read, the pin decodes pixel-exact (luma/U/V) against ffmpeg, and the previously-#[ignore]d a_real_libaom_cfl_stream_decodes_pixel_exact test now passes green with no split needed. Blindness-audit sweep of read_intra_frame_mode_info (libaom decodemv.c) found three more per-block/per-superblock symbols this crate silently never reads while accepting the header fields that would gate them: allow_screen_content_tools (intrabc/palette_mode_info), delta.q_present/ delta.lf_present (per-SB delta_q/delta_lf), and segmentation.enabled (per-block segment_id) -- named refusals added in decode_stream next to the existing CDEF/TxMode::Select refusals. Full ec-av1 suite: 165 passed, 0 failed, 12 ignored. --- crates/ec-av1/src/cdf.rs | 14 ++++++ crates/ec-av1/src/cdf_state.rs | 7 +++ crates/ec-av1/src/decode.rs | 78 +++++++++++++++++++++++++++++++--- crates/ec-av1/src/stream.rs | 34 ++++++++++++++- 4 files changed, 127 insertions(+), 6 deletions(-) diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index c9a10110..8c382ab5 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -312,6 +312,20 @@ pub const UV_MODE_CFL: [[u16; 15]; 13] = [ /// the spec's own table, is `AOM_ICDF`-inverted). pub const CFL_SIGN: [u16; 9] = [1418, 2123, 13340, 18405, 26972, 28343, 32294, 32768, 0]; +/// `default_filter_intra_cdfs` (entropymode.c), the `use_filter_intra` flag's +/// prior, indexed by block-size class: `[0]`=4x4, `[1]`=8x8, `[2]`=16x16, +/// `[3]`=32x32 -- the only square sizes `av1_filter_intra_allowed_bsize` +/// (width and height both <=32) ever offers this decoder's square blocks. +pub const FILTER_INTRA: [[u16; 3]; 4] = [ + [4621, 32768, 0], + [7866, 32768, 0], + [12408, 32768, 0], + [22343, 32768, 0], +]; +/// `default_filter_intra_mode_cdf` (entropymode.c): which of the five +/// `FILTER_INTRA_MODES` a `use_filter_intra` block picks. +pub const FILTER_INTRA_MODE: [u16; 6] = [8949, 12776, 17211, 29558, 32768, 0]; + /// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane /// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by /// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`, diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 1d19c3f3..6c831036 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -239,6 +239,11 @@ pub(crate) struct Cdfs { /// The two motion vector components' own tables: separate owned state, /// since spec 8.3.2 adapts one component without touching the other. pub mv_comp: [MvComponentCdfs; 2], + /// The `use_filter_intra` flag, indexed by block-size class (`[0]`=4x4, + /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32) -- see [`cdf::FILTER_INTRA`]. + pub filter_intra: [[u16; 3]; 4], + /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks. + pub filter_intra_mode: [u16; 6], } /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`, @@ -643,6 +648,8 @@ impl Cdfs { drl_mode: cdf::DRL_MODE, mv_joint: cdf::MV_JOINT, mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()], + filter_intra: cdf::FILTER_INTRA, + filter_intra_mode: cdf::FILTER_INTRA_MODE, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 58ebcefc..3b72f7d2 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -557,12 +557,27 @@ impl Neighbours { /// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the /// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for /// plain DC chroma. +/// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`, +/// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32 +/// bound (spec `filter_intra_mode_info` never reads a symbol there). +fn filter_intra_size_class(side: usize) -> Option { + match side { + 4 => Some(0), + 8 => Some(1), + 16 => Some(2), + 32 => Some(3), + _ => None, + } +} + fn read_intra_mode( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, above_mode: usize, left_mode: usize, cfl: bool, + side: usize, + enable_filter_intra: bool, ) -> Result<(bool, usize, Option<(i32, i32)>)> { let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let skip = dec.symbol(&mut cdfs.skip[0]) != 0; @@ -601,6 +616,36 @@ fn read_intra_mode( } else { return Err(unsupported("a directional chroma mode (round 2)")); }; + // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a + // `use_filter_intra` symbol this decoder never read at all until + // lane-av1real r10 -- silently skipping it left every bit past a + // DC_PRED block at <=32x32 read one symbol short of libaom for the rest + // of the tile (the pinned CfL stream's actual bug: partition/skip/mode + // matched bit-exact, then `eob_pt` read 5 instead of 6, because this + // symbol's bits were never consumed). + if mode == DC_PRED + && enable_filter_intra + && let Some(class) = filter_intra_size_class(side) + { + let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; + if trace { + eprintln!( + "TRACE use_filter_intra side={side} value={}", + use_filter_intra as i32 + ); + } + if use_filter_intra { + // libaom still reads `filter_intra_mode` to stay in sync, but + // this decoder does not implement filter-intra prediction + // (spec 7.11.2.3's 5-tap recursive filter) -- refuse rather + // than silently miscode the reconstruction, the same pattern + // as this crate's other `Error::unsupported` refusals. + let _ = dec.symbol(&mut cdfs.filter_intra_mode); + return Err(unsupported( + "a filter-intra block (this decoder does not implement the 5.11.14 recursive predictor)", + )); + } + } Ok((skip, mode, alpha)) } @@ -856,6 +901,7 @@ fn decode_block( u: &mut PlaneBuf, v: &mut PlaneBuf, base_q_idx: u8, + enable_filter_intra: bool, ) -> Result<()> { let (r, c) = at; let (px, py) = (c * SUB, r * SUB); @@ -865,6 +911,8 @@ fn decode_block( neighbours.above_mode[c], neighbours.left_mode[r], cfl, + side, + enable_filter_intra, )?; let reach = Reach::of(side, px, py, y.width, y.height); let (luma_grid, u_grid, v_grid); @@ -966,6 +1014,7 @@ fn decode_leaf8( u: &mut PlaneBuf, v: &mut PlaneBuf, base_q_idx: u8, + enable_filter_intra: bool, ) -> Result { let (r, c) = outer_at; let mut above_mode = neighbours.above_mode[c]; @@ -980,7 +1029,15 @@ fn decode_leaf8( // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other // `decode_block` caller at 16x16 and up. - let (skip, mode, alpha) = read_intra_mode(dec, cdfs, above_mode, left_mode, true)?; + let (skip, mode, alpha) = read_intra_mode( + dec, + cdfs, + above_mode, + left_mode, + true, + 8, + enable_filter_intra, + )?; let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of(8, px, py, y.width, y.height); let (cpx, cpy) = (px / 2, py / 2); @@ -1096,6 +1153,7 @@ pub fn decode_key_frame_tile( base_q_idx: u8, frame_width: u32, frame_height: u32, + enable_filter_intra: bool, ) -> Result { if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); @@ -1200,6 +1258,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; } PARTITION_SPLIT => { @@ -1256,6 +1315,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; } PARTITION_SPLIT => { @@ -1292,6 +1352,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; continue; } @@ -1334,6 +1395,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -1395,6 +1457,7 @@ pub fn decode_key_frame_tile( &mut u, &mut v, base_q_idx, + enable_filter_intra, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -2216,7 +2279,7 @@ mod tests { #[test] fn a_frame_with_no_mode_info_grid_is_refused() { - assert!(decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128).is_err()); + assert!(decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128, false).is_err()); } /// Encodes and decodes one picture with `modes`, asserting the decoder's @@ -2244,8 +2307,10 @@ mod tests { base_q_idx, .. }: Encoded = crate::encode::encode_key_frame_with_modes(&picture, 40, 0.0, modes).unwrap(); - let decoded = - decode_key_frame_tile(&tile, mi_cols, mi_rows, base_q_idx, w as u32, h as u32).unwrap(); + let decoded = decode_key_frame_tile( + &tile, mi_cols, mi_rows, base_q_idx, w as u32, h as u32, false, + ) + .unwrap(); assert_eq!(decoded.width, w, "{w}x{h}: decoded width"); assert_eq!(decoded.height, h, "{w}x{h}: decoded height"); assert_eq!(decoded.y, reconstruction.y, "{w}x{h} luma mismatch"); @@ -2315,7 +2380,7 @@ mod tests { ); enc.symbol(DC_PRED, &mut cdfs.uv_mode_no_cfl[DC_PRED]); let data = enc.finish(); - let decoded = decode_key_frame_tile(&data, 16, 16, 40, 64, 64).unwrap(); + let decoded = decode_key_frame_tile(&data, 16, 16, 40, 64, 64, false).unwrap(); assert!( decoded.y.iter().all(|&s| s == 128), "a skipped DC_PRED block with no neighbours predicts flat mid-grey" @@ -2494,6 +2559,7 @@ mod tests { encoded.base_q_idx, coded_w, coded_h, + false, ) .unwrap(); assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg"); @@ -2561,6 +2627,7 @@ mod tests { key.base_q_idx, width as u32, height as u32, + false, ) .unwrap(); assert_eq!( @@ -2827,6 +2894,7 @@ mod tests { key.base_q_idx, coded_w, coded_h, + false, ) .unwrap(); assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg"); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 5ac3a42e..55ee950c 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -93,11 +93,42 @@ pub fn decode_stream(data: &[u8]) -> Result> { "a frame using TxMode::Select (this decoder never reads a tx_depth symbol, so it desyncs after the first intra block's mode)", )); } + // lane-av1real r11's blindness-audit sweep (`read_intra_frame_mode_info`, + // libaom decodemv.c): three more per-block/per-SB symbols this crate + // never reads at all, each gated by a header field this decoder + // otherwise accepts uncomplaining -- the same silent-desync shape + // `use_filter_intra` was. `allow_screen_content_tools` gates both + // `read_intrabc_info` and `read_palette_mode_info`; `delta.q_present`/ + // `delta.lf_present` gate `read_delta_qindex`/`read_delta_lflevel` + // (spec 5.11.10/5.11.11, one symbol group per superblock). Refuse + // before desyncing rather than silently miscode, the same pattern as + // the CDEF/TxMode::Select refusals above. + if header.allow_screen_content_tools { + return Err(Error::unsupported( + "AV1 decode_stream", + "a frame with allow_screen_content_tools set (this decoder never reads intrabc/palette_mode_info)", + )); + } + if header.delta.q_present || header.delta.lf_present { + return Err(Error::unsupported( + "AV1 decode_stream", + "a frame with delta_q_present or delta_lf_present set (this decoder never reads the per-superblock delta symbols)", + )); + } + if header.segmentation.enabled { + return Err(Error::unsupported( + "AV1 decode_stream", + "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", + )); + } // `Tile::offset` is relative to the buffer `parse_obu` was handed // (`&data[pos..]` at the time this OBU was parsed), so it is relative // to `obu_offset`, not to `data` as a whole. let start = obu_offset + tile.offset; let tile_bytes = data.get(start..start + tile.size).ok_or(Error::NeedMore)?; + let enable_filter_intra = parser + .sequence_header() + .is_some_and(|seq| seq.enable_filter_intra); let picture = if header.frame_type == FrameType::Key { decode_key_frame_tile( @@ -107,6 +138,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { header.quantization.base_q_idx, header.frame_width, header.frame_height, + enable_filter_intra, )? } else { let reference = reference.as_ref().ok_or_else(|| { @@ -188,6 +220,7 @@ mod tests { encoded.base_q_idx, width as u32, height as u32, + false, ) .unwrap(); let via_stream = decode_stream(&encoded.stream).unwrap(); @@ -513,7 +546,6 @@ mod tests { /// decode: pixel-exact against ffmpeg's own decode, not just "decoded". /// #[test] - #[ignore = "r8: BOTH remaining hypotheses from r7 are now falsified. (1) Tile-data byte offset: an independent from-spec OBU parse in Python (obu_header/leb128/full sequence_header/full uncompressed_header bit-by-bit) derives the tile group starting at absolute byte 23 in the pinned stream; `EC_AV1_TRACE`'s new `key_tile_bytes` line shows our own `decode_stream` (stream.rs `obu_offset + tile.offset`) feeds `decode_key_frame_tile` that exact same byte 23 (first8 04 c8 d9 35 3d a6 de 5e); the r7 python scripts' hardcoded literal agrees too -- three-way match, offset is not the bug. (2) mode_info symbols: `EC_AV1_TRACE` extended to partition/skip/y_mode/uv_mode (decode.rs read_intra_mode + the two partition sites); an independent Python MSAC decoder over the SAME CDF literals -- this round re-verified partition_w32_cdf[0], kf_y_mode_cdf[0][0] and uv_mode_cdf[cfl=1][DC_PRED] byte-for-byte against /tmp/libaom-src/av1/common/entropymode.c source, not trusted from a prior round -- reads partition=NONE, skip=0, y_mode=DC_PRED(0), uv_mode=DC_PRED(0), matching our decoder exactly. So the bitstream genuinely codes DC_PRED with a near-zero residual (three magnitude-2 AC coefficients) for this block, and our decode of that is bit-exact; ffmpeg's real output (a smooth 46..61 diagonal gradient) cannot come from that symbol sequence under any correct DC_PRED reconstruction. The desync is downstream of symbol decode entirely -- intra prediction (`crate::intra::predict`) or the inverse-transform/dequant path (`dequant_and_inverse_typed`) miscomputing the reconstructed pixels from a genuinely-correct DC_PRED + tiny-residual decode, not a wrong symbol. r9: trace/diff `crate::intra::predict`'s DC output and the post-dequant residual grid against an independent implementation of both, the same way r6-r8 did for the entropy layer."] fn a_real_libaom_cfl_stream_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP a_real_libaom_cfl_stream_decodes_pixel_exact: no ffmpeg");