forge
Explore
Log in

Don't you dare go Hollow

Read the unread use_filter_intra flag

Browse files at this commit

beeeec5972c4e7264be4f38a4a97b46b902af65aAhmet Kaan GümüşParent: 120854a
  • fix
    • read the unread use_filter_intra flag
    • audit the same blindness class
Show full message
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.rsmodified+14 -0
@@ -312,6 +312,20 @@ pub const UV_MODE_CFL: [[u16; 15]; 13] = [
312312 /// the spec's own table, is `AOM_ICDF`-inverted).
313313 pub const CFL_SIGN: [u16; 9] = [1418, 2123, 13340, 18405, 26972, 28343, 32294, 32768, 0];
314314
315+/// `default_filter_intra_cdfs` (entropymode.c), the `use_filter_intra` flag's
316+/// prior, indexed by block-size class: `[0]`=4x4, `[1]`=8x8, `[2]`=16x16,
317+/// `[3]`=32x32 -- the only square sizes `av1_filter_intra_allowed_bsize`
318+/// (width and height both <=32) ever offers this decoder's square blocks.
319+pub const FILTER_INTRA: [[u16; 3]; 4] = [
320+ [4621, 32768, 0],
321+ [7866, 32768, 0],
322+ [12408, 32768, 0],
323+ [22343, 32768, 0],
324+];
325+/// `default_filter_intra_mode_cdf` (entropymode.c): which of the five
326+/// `FILTER_INTRA_MODES` a `use_filter_intra` block picks.
327+pub const FILTER_INTRA_MODE: [u16; 6] = [8949, 12776, 17211, 29558, 32768, 0];
328+
315329 /// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane
316330 /// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by
317331 /// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`,
crates/ec-av1/src/cdf_state.rsmodified+7 -0
@@ -239,6 +239,11 @@ pub(crate) struct Cdfs {
239239 /// The two motion vector components' own tables: separate owned state,
240240 /// since spec 8.3.2 adapts one component without touching the other.
241241 pub mv_comp: [MvComponentCdfs; 2],
242+ /// The `use_filter_intra` flag, indexed by block-size class (`[0]`=4x4,
243+ /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32) -- see [`cdf::FILTER_INTRA`].
244+ pub filter_intra: [[u16; 3]; 4],
245+ /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks.
246+ pub filter_intra_mode: [u16; 6],
242247 }
243248
244249 /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`,
@@ -643,6 +648,8 @@ impl Cdfs {
643648 drl_mode: cdf::DRL_MODE,
644649 mv_joint: cdf::MV_JOINT,
645650 mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()],
651+ filter_intra: cdf::FILTER_INTRA,
652+ filter_intra_mode: cdf::FILTER_INTRA_MODE,
646653 }
647654 }
648655
crates/ec-av1/src/decode.rsmodified+73 -5
@@ -557,12 +557,27 @@ impl Neighbours {
557557 /// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the
558558 /// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for
559559 /// plain DC chroma.
560+/// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`,
561+/// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32
562+/// bound (spec `filter_intra_mode_info` never reads a symbol there).
563+fn filter_intra_size_class(side: usize) -> Option<usize> {
564+ match side {
565+ 4 => Some(0),
566+ 8 => Some(1),
567+ 16 => Some(2),
568+ 32 => Some(3),
569+ _ => None,
570+ }
571+}
572+
560573 fn read_intra_mode(
561574 dec: &mut SymbolDecoder,
562575 cdfs: &mut Cdfs,
563576 above_mode: usize,
564577 left_mode: usize,
565578 cfl: bool,
579+ side: usize,
580+ enable_filter_intra: bool,
566581 ) -> Result<(bool, usize, Option<(i32, i32)>)> {
567582 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
568583 let skip = dec.symbol(&mut cdfs.skip[0]) != 0;
@@ -601,6 +616,36 @@ fn read_intra_mode(
601616 } else {
602617 return Err(unsupported("a directional chroma mode (round 2)"));
603618 };
619+ // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a
620+ // `use_filter_intra` symbol this decoder never read at all until
621+ // lane-av1real r10 -- silently skipping it left every bit past a
622+ // DC_PRED block at <=32x32 read one symbol short of libaom for the rest
623+ // of the tile (the pinned CfL stream's actual bug: partition/skip/mode
624+ // matched bit-exact, then `eob_pt` read 5 instead of 6, because this
625+ // symbol's bits were never consumed).
626+ if mode == DC_PRED
627+ && enable_filter_intra
628+ && let Some(class) = filter_intra_size_class(side)
629+ {
630+ let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0;
631+ if trace {
632+ eprintln!(
633+ "TRACE use_filter_intra side={side} value={}",
634+ use_filter_intra as i32
635+ );
636+ }
637+ if use_filter_intra {
638+ // libaom still reads `filter_intra_mode` to stay in sync, but
639+ // this decoder does not implement filter-intra prediction
640+ // (spec 7.11.2.3's 5-tap recursive filter) -- refuse rather
641+ // than silently miscode the reconstruction, the same pattern
642+ // as this crate's other `Error::unsupported` refusals.
643+ let _ = dec.symbol(&mut cdfs.filter_intra_mode);
644+ return Err(unsupported(
645+ "a filter-intra block (this decoder does not implement the 5.11.14 recursive predictor)",
646+ ));
647+ }
648+ }
604649 Ok((skip, mode, alpha))
605650 }
606651
@@ -856,6 +901,7 @@ fn decode_block(
856901 u: &mut PlaneBuf,
857902 v: &mut PlaneBuf,
858903 base_q_idx: u8,
904+ enable_filter_intra: bool,
859905 ) -> Result<()> {
860906 let (r, c) = at;
861907 let (px, py) = (c * SUB, r * SUB);
@@ -865,6 +911,8 @@ fn decode_block(
865911 neighbours.above_mode[c],
866912 neighbours.left_mode[r],
867913 cfl,
914+ side,
915+ enable_filter_intra,
868916 )?;
869917 let reach = Reach::of(side, px, py, y.width, y.height);
870918 let (luma_grid, u_grid, v_grid);
@@ -966,6 +1014,7 @@ fn decode_leaf8(
9661014 u: &mut PlaneBuf,
9671015 v: &mut PlaneBuf,
9681016 base_q_idx: u8,
1017+ enable_filter_intra: bool,
9691018 ) -> Result<usize> {
9701019 let (r, c) = outer_at;
9711020 let mut above_mode = neighbours.above_mode[c];
@@ -980,7 +1029,15 @@ fn decode_leaf8(
9801029 // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec
9811030 // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other
9821031 // `decode_block` caller at 16x16 and up.
983- let (skip, mode, alpha) = read_intra_mode(dec, cdfs, above_mode, left_mode, true)?;
1032+ let (skip, mode, alpha) = read_intra_mode(
1033+ dec,
1034+ cdfs,
1035+ above_mode,
1036+ left_mode,
1037+ true,
1038+ 8,
1039+ enable_filter_intra,
1040+ )?;
9841041 let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
9851042 let reach = Reach::of(8, px, py, y.width, y.height);
9861043 let (cpx, cpy) = (px / 2, py / 2);
@@ -1096,6 +1153,7 @@ pub fn decode_key_frame_tile(
10961153 base_q_idx: u8,
10971154 frame_width: u32,
10981155 frame_height: u32,
1156+ enable_filter_intra: bool,
10991157 ) -> Result<Picture> {
11001158 if mi_cols == 0 || mi_rows == 0 {
11011159 return Err(unsupported("a frame with no mode-info grid"));
@@ -1200,6 +1258,7 @@ pub fn decode_key_frame_tile(
12001258 &mut u,
12011259 &mut v,
12021260 base_q_idx,
1261+ enable_filter_intra,
12031262 )?;
12041263 }
12051264 PARTITION_SPLIT => {
@@ -1256,6 +1315,7 @@ pub fn decode_key_frame_tile(
12561315 &mut u,
12571316 &mut v,
12581317 base_q_idx,
1318+ enable_filter_intra,
12591319 )?;
12601320 }
12611321 PARTITION_SPLIT => {
@@ -1292,6 +1352,7 @@ pub fn decode_key_frame_tile(
12921352 &mut u,
12931353 &mut v,
12941354 base_q_idx,
1355+ enable_filter_intra,
12951356 )?;
12961357 continue;
12971358 }
@@ -1334,6 +1395,7 @@ pub fn decode_key_frame_tile(
13341395 &mut u,
13351396 &mut v,
13361397 base_q_idx,
1398+ enable_filter_intra,
13371399 )?;
13381400 prev_leaf = Some((leaf_mi, leaf_mode));
13391401 }
@@ -1395,6 +1457,7 @@ pub fn decode_key_frame_tile(
13951457 &mut u,
13961458 &mut v,
13971459 base_q_idx,
1460+ enable_filter_intra,
13981461 )?;
13991462 prev_leaf = Some((leaf_mi, leaf_mode));
14001463 }
@@ -2216,7 +2279,7 @@ mod tests {
22162279
22172280 #[test]
22182281 fn a_frame_with_no_mode_info_grid_is_refused() {
2219- assert!(decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128).is_err());
2282+ assert!(decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128, false).is_err());
22202283 }
22212284
22222285 /// Encodes and decodes one picture with `modes`, asserting the decoder's
@@ -2244,8 +2307,10 @@ mod tests {
22442307 base_q_idx,
22452308 ..
22462309 }: Encoded = crate::encode::encode_key_frame_with_modes(&picture, 40, 0.0, modes).unwrap();
2247- let decoded =
2248- decode_key_frame_tile(&tile, mi_cols, mi_rows, base_q_idx, w as u32, h as u32).unwrap();
2310+ let decoded = decode_key_frame_tile(
2311+ &tile, mi_cols, mi_rows, base_q_idx, w as u32, h as u32, false,
2312+ )
2313+ .unwrap();
22492314 assert_eq!(decoded.width, w, "{w}x{h}: decoded width");
22502315 assert_eq!(decoded.height, h, "{w}x{h}: decoded height");
22512316 assert_eq!(decoded.y, reconstruction.y, "{w}x{h} luma mismatch");
@@ -2315,7 +2380,7 @@ mod tests {
23152380 );
23162381 enc.symbol(DC_PRED, &mut cdfs.uv_mode_no_cfl[DC_PRED]);
23172382 let data = enc.finish();
2318- let decoded = decode_key_frame_tile(&data, 16, 16, 40, 64, 64).unwrap();
2383+ let decoded = decode_key_frame_tile(&data, 16, 16, 40, 64, 64, false).unwrap();
23192384 assert!(
23202385 decoded.y.iter().all(|&s| s == 128),
23212386 "a skipped DC_PRED block with no neighbours predicts flat mid-grey"
@@ -2494,6 +2559,7 @@ mod tests {
24942559 encoded.base_q_idx,
24952560 coded_w,
24962561 coded_h,
2562+ false,
24972563 )
24982564 .unwrap();
24992565 assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg");
@@ -2561,6 +2627,7 @@ mod tests {
25612627 key.base_q_idx,
25622628 width as u32,
25632629 height as u32,
2630+ false,
25642631 )
25652632 .unwrap();
25662633 assert_eq!(
@@ -2827,6 +2894,7 @@ mod tests {
28272894 key.base_q_idx,
28282895 coded_w,
28292896 coded_h,
2897+ false,
28302898 )
28312899 .unwrap();
28322900 assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg");
crates/ec-av1/src/stream.rsmodified+33 -1
@@ -93,11 +93,42 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
9393 "a frame using TxMode::Select (this decoder never reads a tx_depth symbol, so it desyncs after the first intra block's mode)",
9494 ));
9595 }
96+ // lane-av1real r11's blindness-audit sweep (`read_intra_frame_mode_info`,
97+ // libaom decodemv.c): three more per-block/per-SB symbols this crate
98+ // never reads at all, each gated by a header field this decoder
99+ // otherwise accepts uncomplaining -- the same silent-desync shape
100+ // `use_filter_intra` was. `allow_screen_content_tools` gates both
101+ // `read_intrabc_info` and `read_palette_mode_info`; `delta.q_present`/
102+ // `delta.lf_present` gate `read_delta_qindex`/`read_delta_lflevel`
103+ // (spec 5.11.10/5.11.11, one symbol group per superblock). Refuse
104+ // before desyncing rather than silently miscode, the same pattern as
105+ // the CDEF/TxMode::Select refusals above.
106+ if header.allow_screen_content_tools {
107+ return Err(Error::unsupported(
108+ "AV1 decode_stream",
109+ "a frame with allow_screen_content_tools set (this decoder never reads intrabc/palette_mode_info)",
110+ ));
111+ }
112+ if header.delta.q_present || header.delta.lf_present {
113+ return Err(Error::unsupported(
114+ "AV1 decode_stream",
115+ "a frame with delta_q_present or delta_lf_present set (this decoder never reads the per-superblock delta symbols)",
116+ ));
117+ }
118+ if header.segmentation.enabled {
119+ return Err(Error::unsupported(
120+ "AV1 decode_stream",
121+ "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)",
122+ ));
123+ }
96124 // `Tile::offset` is relative to the buffer `parse_obu` was handed
97125 // (`&data[pos..]` at the time this OBU was parsed), so it is relative
98126 // to `obu_offset`, not to `data` as a whole.
99127 let start = obu_offset + tile.offset;
100128 let tile_bytes = data.get(start..start + tile.size).ok_or(Error::NeedMore)?;
129+ let enable_filter_intra = parser
130+ .sequence_header()
131+ .is_some_and(|seq| seq.enable_filter_intra);
101132
102133 let picture = if header.frame_type == FrameType::Key {
103134 decode_key_frame_tile(
@@ -107,6 +138,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
107138 header.quantization.base_q_idx,
108139 header.frame_width,
109140 header.frame_height,
141+ enable_filter_intra,
110142 )?
111143 } else {
112144 let reference = reference.as_ref().ok_or_else(|| {
@@ -188,6 +220,7 @@ mod tests {
188220 encoded.base_q_idx,
189221 width as u32,
190222 height as u32,
223+ false,
191224 )
192225 .unwrap();
193226 let via_stream = decode_stream(&encoded.stream).unwrap();
@@ -513,7 +546,6 @@ mod tests {
513546 /// decode: pixel-exact against ffmpeg's own decode, not just "decoded".
514547 ///
515548 #[test]
516- #[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."]
517549 fn a_real_libaom_cfl_stream_decodes_pixel_exact() {
518550 if !have_ffmpeg() {
519551 eprintln!("SKIP a_real_libaom_cfl_stream_decodes_pixel_exact: no ffmpeg");