forge
Explore
Log in

Don't you dare go Hollow

ec-av1: step the intrabc-rect lossless chroma replay by the unit's own mi footprint

Browse files at this commit

30d94ca0237b4315fdf93f9afb9c86f160ce2013Ahmet Kaan GümüşParent: 6a556d8
lane-whtshape (f541d062, merged afa13bcf) gave `decode_intrabc_rect` the
per-TX_4X4 lossless chroma walk and the tail replay that re-stamps each
unit's own coefficient context over the whole-block record. The replay
stepped the mi origin by `4 >> ss_y(fctx)` cells, which is 4:2:0-only
arithmetic.

A TX_4X4 chroma unit covers `4 << ss` LUMA pixels, so its origin in the
luma-mi grid advances by `(4 << ss) / MI`. That is the form
`decode_inter_block`'s reference `mu_chroma_units` construction already
writes (decode.rs:43688, `at.0 + cr * ((cu << ss_y(fctx)) / MI)` with
cu = 4 on a lossless frame); this site was the outlier.

  ss (1,1) 4:2:0: `4 >> 1` = 2 mi, and `(4 << 1) / 4` = 2 mi -- the SAME
              number, so 4:2:0 is unchanged BY CONSTRUCTION, not by
              measurement alone.
  ss (0,0) 4:4:4: `4 >> 0` = 4 mi = 16 luma px per 4x4 chroma unit, where
              the unit covers `4 << 0` = 4 luma px = ONE mi. Every replayed
              unit was stamped at 4x its own mi row/column, writing over
              cells belonging to other blocks.

Measured on `ll444_intrabc_rect_l2.obu` (testsrc2 512x128 yuv444p,
aomenc --lossless=1 --cpu-used=2 --lag-in-frames=0 --kf-max-dist=100
--limit=6, 121844 bytes, sha256 f0de080edce35d34cdd628b71f63dc452
f946e8328352b69d3c1da090a54ec67), which reaches it: before, 10604 samples
wrong in the KEY frame, first at byte 33216 (Y(64,448)) -- the corrupted
chroma context desyncs the following luma, which is why the first
divergence is luma at all; frames 1-5 123987..171988 wrong. After: all
six frames byte-exact against the oracle aomdec in decode order, and the
EC_SYMR ladder is 540833 reads on both sides with zero fork.

Gate `a_lossless_444_intrabc_rect_replay_steps_by_the_units_own_mi_footprint`
asserts the 4:4:4 shape from the stream's OWN parsed sequence header (at
(1,1) the two steps are the same number, so a 4:2:0 stream cannot fail
this gate), that lane-whtshape's own INTRABC_RECT_LOSSLESS_CHROMA4_HITS
counter fired so the corrected expression was evaluated, luma-sized chroma
extents, and decode-order byte-exactness. Control arm: the 128x96
4:4:4 lossless pin must read that counter 0 -- whole square blocks have no
multi-unit chroma plane block to step.

Mutation proof, run: reverting only the step expression and nothing else
turns the gate RED at "decode-order frame 0 of 6 differs from the oracle
at byte 33216 (ours 101 vs 100), 10604 bytes differ"; restored from a
saved copy with cp, verified with diff -q, LIVE run ok.
crates/ec-av1/fixtures/ll444_intrabc_rect_l2.obuadded+0 -0

Binary file changed

crates/ec-av1/src/decode.rsmodified+20 -1
@@ -13892,7 +13892,26 @@ fn decode_intrabc_rect(
13892 unit.extend_from_slice(&comp[(ur * 4 + r) * cside + uc * 4..][..4]);13892 unit.extend_from_slice(&comp[(ur * 4 + r) * cside + uc * 4..][..4]);
13893 }13893 }
13894 neighbours.record_mi_chroma(13894 neighbours.record_mi_chroma(
13895- (mi_r + ur * (4 >> ss_y(fctx)), mi_c + uc * (4 >> ss_x(fctx))),13895+ // lane-av1lm444loss-corr: the MI STEP between chroma
13896+ // units is that same footprint expressed in luma mi
13897+ // cells, `(4 << ss) / MI` -- which is exactly what
13898+ // `decode_inter_block`'s reference `mu_chroma_units`
13899+ // construction writes (decode.rs:43688,
13900+ // `at.0 + cr * ((cu << ss_y(fctx)) / MI)` with
13901+ // `cu = 4` on a lossless frame). The `4 >> ss_y` form
13902+ // this replaces is 4:2:0-only arithmetic that happens
13903+ // to agree there: at ss (1,1) it is `4 >> 1` = 2 mi
13904+ // and the footprint form is `(4 << 1) / 4` = 2 mi, the
13905+ // SAME value, so 4:2:0 is unchanged by construction.
13906+ // At ss (0,0) it is `4 >> 0` = 4 mi -- four luma mi
13907+ // (16 luma px) per 4x4 chroma unit, where a TX_4X4
13908+ // chroma unit covers `4 << 0` = 4 luma px = ONE mi.
13909+ // The step therefore lands every replayed unit at 4x
13910+ // its own mi row/column on a 4:4:4 lossless frame, and
13911+ // the whole block's per-unit states are stamped over
13912+ // cells that belong to other blocks (or to nothing).
13913+ (mi_r + ur * ((4 << ss_y(fctx)) / MI),
13914+ mi_c + uc * ((4 << ss_x(fctx)) / MI)),
13896 4 << ss_x(fctx),13915 4 << ss_x(fctx),
13897 4 << ss_y(fctx),13916 4 << ss_y(fctx),
13898 plane_idx,13917 plane_idx,
crates/ec-av1/src/stream.rsmodified+146 -0
@@ -7184,6 +7184,152 @@ pub(crate) mod tests {
7184 );7184 );
7185 }7185 }
7186 7186
7187+ /// lane-av1lm444loss-corr: the MI STEP at which `decode_intrabc_rect`'s
7188+ /// lossless chroma replay walks the plane block.
7189+ ///
7190+ /// `lane-whtshape` (`f541d062`, merged `afa13bcf`) gave this route the
7191+ /// per-TX_4X4 lossless chroma walk and the tail replay that re-stamps
7192+ /// each unit's own coefficient context over the whole-block record. The
7193+ /// replay stepped by `4 >> ss_y(fctx)` mi cells, which is 4:2:0-only
7194+ /// arithmetic: at ss (1,1) it reads 2 and the ss-aware form
7195+ /// `(4 << ss_y) / MI` also reads 2, so 4:2:0 agrees by construction; at
7196+ /// ss (0,0) it reads 4 mi, i.e. 16 luma px per 4x4 chroma unit, where the
7197+ /// unit covers `4 << 0` = 4 luma px = ONE mi. Every replayed unit was
7198+ /// therefore stamped at 4x its own mi origin on a 4:4:4 lossless frame,
7199+ /// writing over cells that belong to other blocks.
7200+ ///
7201+ /// This gate is the WIRNESS stream, not a vacuous one. The 4:2:0
7202+ /// 4x4-identity is stated in the step's own comment and measured by the
7203+ /// `4:2:0` arm of the lane's invariant battery, not asserted here --
7204+ /// a 4:2:0 stream cannot fail this gate, by construction, and pretending
7205+ /// otherwise would be a floor on wrong output.
7206+ ///
7207+ /// Fixture `ll444_intrabc_rect_l2.obu`, 121844 bytes, sha256
7208+ /// `f0de080edce35d34cdd628b71f63dc452f946e8328352b69d3c1da090a54ec67`:
7209+ /// `testsrc2 512x128 yuv444p`, aomenc `--lossless=1 --cpu-used=2
7210+ /// --lag-in-frames=0 --kf-max-dist=100 --limit=6`, 6 frames. On main's
7211+ /// step this stream is 10604 samples wrong in the KEY frame, first at
7212+ /// byte 33216 (Y(64,448)) -- the corrupted chroma context desyncs the
7213+ /// following luma, which is why the first divergence is luma at all.
7214+ ///
7215+ /// Assertions:
7216+ ///
7217+ /// 1. `(subsampling_x, subsampling_y, bit_depth) == (0, 0, 8)` from the
7218+ /// stream's OWN parsed sequence header. At (1,1) the corrected step
7219+ /// and the old one are the same number, so without this every
7220+ /// assertion below would pass on a stream that cannot fail.
7221+ /// 2. `intrabc_rect_lossless_chroma4_hits()` (lane-whtshape's own
7222+ /// counter) is nonzero -- the replayed arm really ran, so the
7223+ /// corrected expression was evaluated.
7224+ /// 3. the U and V plane extents are luma-sized, which is the geometry
7225+ /// the step indexes into.
7226+ /// 4. `decode_all_frames_vs_oracle`: decode-order frame-count parity,
7227+ /// then byte-exactness on every frame.
7228+ ///
7229+ /// Control: the sibling 128x96 4:4:4 lossless pin, whose blocks are
7230+ /// whole and square, must not take the lossless chroma walk at all --
7231+ /// its chroma plane block IS one TX_4X4, so there is no step to replay.
7232+ #[test]
7233+ fn a_lossless_444_intrabc_rect_replay_steps_by_the_units_own_mi_footprint() {
7234+ const NAME: &str = "a_lossless_444_intrabc_rect_replay_steps_by_the_units_own_mi_footprint";
7235+ const W: usize = 512;
7236+ const H: usize = 128;
7237+ const FRAMES: usize = 6;
7238+
7239+ let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
7240+ .join("fixtures/ll444_intrabc_rect_l2.obu");
7241+ let stream = read_pin(&obu, 121844, 0x38bf_968e_979c_7a0a, NAME);
7242+ assert_eq!(
7243+ subsampled_header_shape(&stream, NAME),
7244+ (0, 0, 8),
7245+ "{NAME}: the stream is not 8-bit 4:4:4 -- at ss (1,1) the corrected \
7246+ step and the 4:2:0-only one are the SAME number (2 mi), so this gate \
7247+ cannot fail on a 4:2:0 stream and would prove nothing there"
7248+ );
7249+ let _guard = lock_gate_counters();
7250+ crate::decode::reset_intrabc_rect_lossless_chroma4_hits();
7251+ let h0 = crate::decode::intrabc_rect_lossless_chroma4_hits();
7252+ let frames = decode_stream(&stream)
7253+ .unwrap_or_else(|e| panic!("{NAME}: the pinned stream no longer decodes cleanly: {e}"));
7254+ let walked = crate::decode::intrabc_rect_lossless_chroma4_hits() - h0;
7255+ assert!(
7256+ walked > 0,
7257+ "{NAME}: lane-whtshape's lossless per-TX_4X4 chroma walk never ran, so the \
7258+ replayed MI step -- the thing this gate exists for -- was never evaluated"
7259+ );
7260+ assert_eq!(frames.len(), FRAMES, "{NAME}: frame count");
7261+ for f in &frames {
7262+ assert_eq!((f.width, f.height), (W, H), "{NAME}: dimensions");
7263+ assert_eq!(
7264+ f.u.len(),
7265+ W * H,
7266+ "{NAME}: U extent must be luma-sized at 4:4:4 -- the replay steps mi \
7267+ cells through this grid"
7268+ );
7269+ assert_eq!(f.v.len(), W * H, "{NAME}: V extent must be luma-sized at 4:4:4");
7270+ }
7271+ if aomdec_path().is_file() {
7272+ let (decoded, hidden) = decode_all_frames_vs_oracle(&stream, NAME);
7273+ assert_eq!(
7274+ decoded, FRAMES,
7275+ "{NAME}: decoded {decoded} frames in decode order, expected {FRAMES}"
7276+ );
7277+ eprintln!(
7278+ "{NAME}: {decoded} frames byte-exact vs aomdec ({hidden} hidden), \
7279+ {walked} lossless chroma replays"
7280+ );
7281+ } else {
7282+ eprintln!(
7283+ "SKIP {NAME} aomdec arm: no oracle aomdec at {}",
7284+ aomdec_path().display()
7285+ );
7286+ }
7287+
7288+ // Control: whole square blocks have no multi-unit plane block to step.
7289+ let ctrl = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
7290+ .join("fixtures/ll444_128root_lossless.obu");
7291+ let ctrl_stream = read_pin(&ctrl, 63429, 0xf07d_47fc_fd51_2658, &format!("{NAME} control"));
7292+ crate::decode::reset_intrabc_rect_lossless_chroma4_hits();
7293+ let _ = decode_stream(&ctrl_stream)
7294+ .unwrap_or_else(|e| panic!("{NAME}: the control arm no longer decodes cleanly: {e}"));
7295+ assert_eq!(
7296+ crate::decode::intrabc_rect_lossless_chroma4_hits(),
7297+ 0,
7298+ "{NAME} control: the lossless chroma walk ran on a stream of whole square \
7299+ blocks -- there is no multi-unit chroma plane block to replay, so the \
7300+ corrected step is not exercised there at all"
7301+ );
7302+ }
7303+
7304+ /// `(subsampling_x, subsampling_y, bit_depth)` out of the stream's OWN
7305+ /// sequence header. A gate that claims to exercise a subsampling-specific
7306+ /// path must assert this from the parsed header, not from the recipe that
7307+ /// produced the fixture: the recipe can drift (or a wrong `--pix_fmt`
7308+ /// argument can silently produce a different subsampling) and every other
7309+ /// assertion would then pass vacuously.
7310+ fn subsampled_header_shape(stream: &[u8], name: &str) -> (u8, u8, usize) {
7311+ let mut parser = Av1Parser::new();
7312+ let mut pos = 0usize;
7313+ let mut shape = None;
7314+ while pos < stream.len() {
7315+ let obu = parser
7316+ .parse_obu(&stream[pos..])
7317+ .unwrap_or_else(|e| panic!("{name}: OBU does not parse: {e:?}"));
7318+ if obu.total_size == 0 {
7319+ break;
7320+ }
7321+ pos += obu.total_size;
7322+ if let ObuKind::SequenceHeader(seq) = obu.kind {
7323+ shape = Some((
7324+ seq.color_config.subsampling_x,
7325+ seq.color_config.subsampling_y,
7326+ seq.color_config.bit_depth as usize,
7327+ ));
7328+ }
7329+ }
7330+ shape.unwrap_or_else(|| panic!("{name}: no sequence header parsed"))
7331+ }
7332+
7187 /// lane-av1llpredgate2: the debug-exact pin for the ss-aware7333 /// lane-av1llpredgate2: the debug-exact pin for the ss-aware
7188 /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the7334 /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the
7189 /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc27335 /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc2