forge
Explore
Log in

Don't you dare go Hollow

ec-av1: lane-av1444rect -- a 1:4 inter strip's chroma context was gathered at the 4:2:0 pair extent at ss (0,0), forking the coder (H1)

Browse files at this commit

84fa44cfe66f76e22181d4ff8bc06ef6ea78db07Ahmet Kaan GümüşParent: 689d3e0
At 4:4:4 `is_chroma_reference` (av1_common_int.h:1454) reduces both parity
clauses to `!subsampling_y` / `!subsampling_x`, so EVERY block is its own
chroma reference and no 1:4 pair exists. `decode_inter_block`'s `around_c`
never gated on ss, so a HORZ_4 16x4 strip read its chroma coefficient
context over the 4:2:0 pair extent (16x8 luma = left[mi_r] and
left[mi_r+1]) instead of its own 4-px one. The U unit's `get_txb_ctx` then
saw a left neighbour libaom does not have and forked the arithmetic coder.

The 4:4:4 lossy + RECT cell -- the largest correctness gap the chroma-format
sweep left (lanes/av1formatsweep.report.md H1) -- had NO exactness gate at
all: the whole committed 4:4:4 base is lossless 8-bit.

Witness: testsrc2 128x96 rate=25 yuv444p, aomenc --profile=1 --passes=1
--end-usage=q --cq-level=20 --cpu-used=2 --threads=1 --row-mt=0
--lag-in-frames=0 --kf-max-dist=100 --limit=2, 6441 B, sha256 77f727e7...
Red before: frame 1, 8884/36864 samples wrong, first at Y(91,32); the first
wrong symbol was read 26263, both sides at mi (8,16) -- ours a luma-shaped
txb_skip row (re-indexed chroma ctx 1), the oracle decodetxb.c:158 on the U
plane with txb_skip_ctx 7. Green after: 2/2 frames byte-exact vs aomdec.

Gate `a_444_lossy_rect4_inter_stream_decodes_pixel_exact` pins the fixture
(len + FNV), asserts the ss (0,0) full-resolution chroma shape, the
byte-exact compare through the oracle aomdec, and non-vacuity on the
corrected route via `rect4_inter_own_chroma444_hits` (9 gathers here, 0 on
the 4:2:0 control, which also carries 16 HORZ_4 / 28 VERT_4 strips and is
itself compared byte-for-byte so a widening past ss_x 0 goes red).
Mutation proof: restoring the old arm with the counter left in place turns
the gate red at byte 4187 (ours 35 vs 38), 8884 bytes differing.

Identity: the crate's whole 444 family (9), rect_strip family (10), the
16-level rect-leaf gate, and all three new chroma-format-sweep gates
(444 lossless 10-bit, 444 12-bit, odd coded dimensions) run green against
this decode.rs. H2 (4:4:4 lossy tx-size-search chroma +/-1, 608/36864 from
frame 2) is unchanged and still divergent -- it is reconstruction
arithmetic, not entropy, and belongs to its own lane. H3's tile-column
recipe (77847 B, 6 frames) is byte-exact here.

(cherry picked from commit f92776bab2442319e32d4f56844af6956e2fc18b)
crates/ec-av1/fixtures/444_lossy_rect4_inter_witness.obuadded+0 -0

Binary file changed

crates/ec-av1/src/decode.rsmodified+51 -8
@@ -5504,6 +5504,28 @@ pub(crate) fn intra_rect4_strip_in_inter_hits(shape: usize) -> usize {
5504 INTRA_RECT4_IN_INTER_HITS.with(|c| c.get()[shape])5504 INTRA_RECT4_IN_INTER_HITS.with(|c| c.get()[shape])
5505 }5505 }
5506 5506
5507+// lane-av1444rect: how many 1:4 inter strips at `ss_x == 0` read their chroma
5508+// coefficient context over the STRIP'S OWN extent instead of the 4:2:0 pair's
5509+// (see [`decode_inter_block`]'s `around_c`). `is_chroma_reference`
5510+// (av1_common_int.h:1454) is unconditionally true at ss (0,0), so no pair
5511+// exists there; the counter is what keeps a 4:4:4 1:4-strip gate from passing
5512+// vacuously (a 4:2:0 stream of the same content leaves it at 0).
5513+thread_local! {
5514+ static RECT4_INTER_OWN_CHROMA444_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
5515+}
5516+
5517+/// Current value of [`RECT4_INTER_OWN_CHROMA444_HITS`].
5518+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
5519+pub(crate) fn rect4_inter_own_chroma444_hits() -> usize {
5520+ RECT4_INTER_OWN_CHROMA444_HITS.with(std::cell::Cell::get)
5521+}
5522+
5523+/// Resets [`RECT4_INTER_OWN_CHROMA444_HITS`] before a decode.
5524+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
5525+pub(crate) fn reset_rect4_inter_own_chroma444_hits() {
5526+ RECT4_INTER_OWN_CHROMA444_HITS.with(|c| c.set(0));
5527+}
5528+
5507 // lane-intra14 r1: set while an INTRA block of an INTER frame is decoded5529 // lane-intra14 r1: set while an INTRA block of an INTER frame is decoded
5508 // through one of the key-frame rect readers ([`decode_block_rect4`],5530 // through one of the key-frame rect readers ([`decode_block_rect4`],
5509 // [`decode_block_rect64`]). `Some((size_group, skip))` tells5531 // [`decode_block_rect64`]). `Some((size_group, skip))` tells
@@ -40849,17 +40871,38 @@ fn decode_inter_block(
40849 // ([`Neighbours::around_mi_422_chroma`]) -- and the same40871 // ([`Neighbours::around_mi_422_chroma`]) -- and the same
40850 // reroute catches 4:2:2 VERT: its (8, 16) pair span's40872 // reroute catches 4:2:2 VERT: its (8, 16) pair span's
40851 // above side covers one chroma column per two luma mi40873 // above side covers one chroma column per two luma mi
40852- // columns, the identical double-count. The plain per-mi40874+ // columns, the identical double-count.
40853- // gather survives only where the ss gate excludes it:40875+ //
40854- // the 4:2:0 pair merge (16, 8) and every 4:4:4 shape.40876+ // lane-av1444rect (H1, MEASURED): the (16, 8) / (8, 16) pair
40877+ // extent was reached at ss (0,0) too, where no pair exists.
40878+ // At 4:4:4 `is_chroma_reference` (av1_common_int.h:1454)
40879+ // reduces BOTH parity clauses to `!subsampling_y` /
40880+ // `!subsampling_x`, so every block is its own chroma
40881+ // reference: a HORZ_4 16x4 strip codes its OWN 16x4 chroma
40882+ // and the setter above already gave it `pair_mi == at_mi`.
40883+ // Its coefficient context is therefore the strip's own,
40884+ // which is exactly the `around` gathered one screen above
40885+ // as `around_mi_rect(at_mi, write_w, write_h)`. The pair
40886+ // gather read one EXTRA luma mi row of LEFT chroma context
40887+ // (`ph = 8` covers left[8] and left[9], a 4-px strip only
40888+ // left[8]), so the U unit's `get_txb_ctx` saw a neighbour
40889+ // libaom does not have and forked the arithmetic coder: the
40890+ // 6441-byte 4:4:4 lossy cq-20 stream, frame 1, mi(8,16)
40891+ // read our `txb_skip_ctx` 1 (0 above + 1 left) where the
40892+ // oracle read offset-7 row 7 (ctx_base 0).
40855 let around_c = match strip_chroma {40893 let around_c = match strip_chroma {
40894+ // lane-av1444rect: the arm `is_chroma_reference` makes the
40895+ // ONLY correct choice at ss_x 0 -- no pair exists, the
40896+ // extent is the strip's own. Counted so a 4:4:4 1:4-strip
40897+ // gate can prove it exercised this route (a 4:2:0 stream
40898+ // of the same content reads 0).
40899+ Some(_) if ss_x(fctx) == 0 => {
40900+ hit!(RECT4_INTER_OWN_CHROMA444_HITS);
40901+ around
40902+ }
40856 Some(s) if s.has_chroma => {40903 Some(s) if s.has_chroma => {
40857 let (pw, ph) = if s.horz {40904 let (pw, ph) = if s.horz {
40858- if ss_x(fctx) == 1 && ss_y(fctx) == 0 {40905+ if ss_y(fctx) == 0 { (16, 4) } else { (16, 8) }
40859- (16, 4)
40860- } else {
40861- (16, 8)
40862- }
40863 } else {40906 } else {
40864 (8, 16)40907 (8, 16)
40865 };40908 };
crates/ec-av1/src/stream.rsmodified+181 -1
@@ -2671,7 +2671,6 @@ pub(crate) mod tests {
2671 );2671 );
2672 }2672 }
2673 2673
2674-
2675 /// lane-av1-qmatrix: WITNESS for the lifted `using_qmatrix` refusal (the2674 /// lane-av1-qmatrix: WITNESS for the lifted `using_qmatrix` refusal (the
2676 /// old gate `a_frame_using_quantisation_matrices_is_refused_by_name` is2675 /// old gate `a_frame_using_quantisation_matrices_is_refused_by_name` is
2677 /// its mirror image -- its own panic said "flip this gate to a witness"2676 /// its mirror image -- its own panic said "flip this gate to a witness"
@@ -6928,6 +6927,187 @@ pub(crate) mod tests {
6928 }6927 }
6929 }6928 }
6930 6929
6930+ /// lane-av1444rect (H1): the 4:4:4 LOSSY cell with a 1:4 (HORZ_4 /
6931+ /// VERT_4) partition -- the largest correctness gap the chroma-format
6932+ /// sweep left, and the first 4:4:4 lossy exactness gate in the crate.
6933+ ///
6934+ /// **The defect.** `decode_inter_block`'s `around_c` gathered a 1:4
6935+ /// strip's chroma coefficient context at the 4:2:0 PAIR extent
6936+ /// (`(16, 8)` HORZ / `(8, 16)` VERT) with no `ss` gate, so at ss (0,0)
6937+ /// it read one extra luma mi row of LEFT chroma context. At 4:4:4
6938+ /// `is_chroma_reference` (av1_common_int.h:1454) reduces both parity
6939+ /// clauses to `!subsampling_y` / `!subsampling_x`, so every block is its
6940+ /// own chroma reference and no pair exists: the strip codes its own
6941+ /// 16x4 chroma over its own context. Our U unit's `get_txb_ctx` then saw
6942+ /// a neighbour libaom does not have, and the arithmetic coder forked.
6943+ ///
6944+ /// **The recipe** (sha256
6945+ /// `77f727e76d81fe48cd03709f799131677c04869b163e18d78e96bc742ec69153`,
6946+ /// 6441 bytes, 2 frames, `testsrc2 128x96 rate=25` yuv444p):
6947+ /// ```text
6948+ /// ffmpeg -f lavfi -i "testsrc2=size=128x96:rate=25" -frames:v 2 \
6949+ /// -pix_fmt yuv444p -f yuv4mpegpipe - | \
6950+ /// aomenc --codec=av1 --profile=1 --passes=1 --end-usage=q --cq-level=20 \
6951+ /// --cpu-used=2 --threads=1 --row-mt=0 --lag-in-frames=0 \
6952+ /// --kf-max-dist=100 --limit=2 - out.obu
6953+ /// ```
6954+ /// (`rate=25` is load-bearing: `testsrc2`'s pattern is time-parameterised,
6955+ /// so `rate=1` produces a different, already-exact stream.)
6956+ ///
6957+ /// Red before the fix: frame 1, 8884 of 36864 samples wrong, first at
6958+ /// Y(91,32); the first wrong symbol was read 26263 of the frame, both
6959+ /// sides at mi (8,16) -- ours a luma-shaped `txb_skip` row (our
6960+ /// re-indexed chroma ctx 1), the oracle `decodetxb.c:158` on the U plane
6961+ /// with `txb_skip_ctx = 7`.
6962+ ///
6963+ /// Non-vacuity, three ways: the pinned stream must really be ss (0,0)
6964+ /// (asserted through the oracle aomdec's own sequence header, below);
6965+ /// `decode::rect4_inter_own_chroma444_hits()` must be non-zero, so the
6966+ /// 1:4-strip route the fix sits on really ran; and the SAME source and
6967+ /// flags at 4:2:0 must leave that counter at zero, which is what makes
6968+ /// it the 4:4:4-only route rather than a tally every strip bumps.
6969+ #[test]
6970+ fn a_444_lossy_rect4_inter_stream_decodes_pixel_exact() {
6971+ const NAME: &str = "a_444_lossy_rect4_inter_stream_decodes_pixel_exact";
6972+ const FIXTURE_LEN: usize = 6441;
6973+ const FIXTURE_FNV: u64 = 0x0436_8b2d_2e91_72b0;
6974+ const W: usize = 128;
6975+ const H: usize = 96;
6976+ const FRAMES: usize = 2;
6977+ let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
6978+ .join("fixtures/444_lossy_rect4_inter_witness.obu");
6979+ let stream = std::fs::read(&obu).unwrap_or_else(|e| {
6980+ panic!(
6981+ "{NAME}: pinned fixture {} is missing ({e}) -- the gate cannot run, \
6982+ which is a failure, not a skip",
6983+ obu.display()
6984+ )
6985+ });
6986+ assert_eq!(stream.len(), FIXTURE_LEN, "{NAME}: fixture length moved");
6987+ assert_eq!(fnv1a64(&stream), FIXTURE_FNV, "{NAME}: fixture bytes moved");
6988+
6989+ let _guard = lock_gate_counters();
6990+ crate::decode::reset_rect4_inter_own_chroma444_hits();
6991+ let frames = decode_stream(&stream).unwrap_or_else(|e| {
6992+ panic!("{NAME}: the pinned 4:4:4 lossy stream no longer decodes cleanly: {e}")
6993+ });
6994+ let own_chroma = crate::decode::rect4_inter_own_chroma444_hits();
6995+ assert!(
6996+ own_chroma > 0,
6997+ "{NAME}: no 1:4 inter strip read its chroma context over its own \
6998+ extent -- the corrected route never ran (class gate-blind-to-feature)"
6999+ );
7000+ assert_eq!(frames.len(), FRAMES, "{NAME}: frame count");
7001+ for (i, f) in frames.iter().enumerate() {
7002+ assert_eq!((f.width, f.height), (W, H), "{NAME}: frame {i} dimensions");
7003+ // At 4:4:4 the chroma planes are full resolution. A decoder that
7004+ // kept the 4:2:0 extent would hand quarter-size chroma planes to
7005+ // the byte compare below, which cannot pass -- but say so here
7006+ // rather than let the compare stand in for the shape claim.
7007+ assert_eq!(
7008+ f.u.len(),
7009+ W * H,
7010+ "{NAME}: frame {i} chroma plane is {} samples; at 4:4:4 it must be \
7011+ full resolution ({W}x{H})",
7012+ f.u.len()
7013+ );
7014+ }
7015+ if aomdec_path().is_file() {
7016+ let (decoded, _hidden) = decode_all_frames_vs_oracle(&stream, NAME);
7017+ eprintln!(
7018+ "{NAME}: {decoded} frame(s) byte-exact vs aomdec, {own_chroma} \
7019+ own-extent 1:4 chroma gather(s)"
7020+ );
7021+ } else {
7022+ eprintln!("{NAME}: no oracle aomdec, pixel arm skipped");
7023+ }
7024+
7025+ // The specificity arm: the SAME source, flags and 1:4 partitions at
7026+ // 4:2:0 must not touch the 4:4:4-only route. If a later change made
7027+ // the counter fire everywhere, this arm is what catches it.
7028+ if !have_aomenc() {
7029+ eprintln!("{NAME}: no aomenc, 4:2:0 specificity arm skipped");
7030+ return;
7031+ }
7032+ let source = format!("testsrc2=size={W}x{H}:rate=25");
7033+ let y4m = Command::new("ffmpeg")
7034+ .args([
7035+ "-v",
7036+ "error",
7037+ "-f",
7038+ "lavfi",
7039+ "-i",
7040+ &source,
7041+ "-frames:v",
7042+ "2",
7043+ "-pix_fmt",
7044+ "yuv420p",
7045+ "-strict",
7046+ "-1",
7047+ "-f",
7048+ "yuv4mpegpipe",
7049+ "-",
7050+ ])
7051+ .stdin(Stdio::null())
7052+ .stdout(Stdio::piped())
7053+ .stderr(Stdio::piped())
7054+ .output()
7055+ .expect("ffmpeg failed to run");
7056+ assert!(
7057+ y4m.status.success(),
7058+ "{NAME}: ffmpeg refused the 4:2:0 control source: {}",
7059+ String::from_utf8_lossy(&y4m.stderr)
7060+ );
7061+ let out = run_with_stdin(
7062+ Command::new(aomenc_path()).args([
7063+ "--codec=av1",
7064+ "--passes=1",
7065+ "--end-usage=q",
7066+ "--cq-level=20",
7067+ "--cpu-used=2",
7068+ "--threads=1",
7069+ "--row-mt=0",
7070+ "--lag-in-frames=0",
7071+ "--kf-max-dist=100",
7072+ "--limit=2",
7073+ "--obu",
7074+ "-o",
7075+ "-",
7076+ "-",
7077+ ]),
7078+ &y4m.stdout,
7079+ );
7080+ assert!(
7081+ out.status.success(),
7082+ "{NAME}: aomenc refused the 4:2:0 control recipe: {}",
7083+ String::from_utf8_lossy(&out.stderr)
7084+ );
7085+ crate::decode::reset_rect4_inter_own_chroma444_hits();
7086+ decode_stream(&out.stdout)
7087+ .unwrap_or_else(|e| panic!("{NAME}: the 4:2:0 control stream was refused: {e}"));
7088+ let (h4, v4, _, _) = crate::decode::inter16_rect4_counters();
7089+ // The control must actually code 1:4 inter strips, or it says nothing
7090+ // about the arm the fix touches. Measured: 16 HORZ_4, 28 VERT_4.
7091+ assert!(
7092+ h4 + v4 > 0,
7093+ "{NAME}: the 4:2:0 control coded NO 1:4 inter strip ({h4} HORZ_4, {v4} \
7094+ VERT_4) -- the identity arm below would say nothing about 1:4 strips"
7095+ );
7096+ assert_eq!(
7097+ crate::decode::rect4_inter_own_chroma444_hits(),
7098+ 0,
7099+ "{NAME}: the 4:2:0 control fired the ss_x-0 own-extent route -- the route \
7100+ is not specific to the 4:4:4 cell it was fixed for"
7101+ );
7102+ // The identity half: the fix adds an arm gated on `ss_x == 0`, so a
7103+ // 4:2:0 stream must stay byte-exact through it. This is the arm that
7104+ // would go red if the new arm were ever widened past ss_x 0.
7105+ if aomdec_path().is_file() {
7106+ decode_all_frames_vs_oracle(&out.stdout, &format!("{NAME}-420"));
7107+ }
7108+ eprintln!("{NAME}: 4:2:0 control {h4} HORZ_4 / {v4} VERT_4 strips, 0 own-extent gathers");
7109+ }
7110+
6931 /// lane-av1lrflush: the pipelined loop-restoration band release flushes7111 /// lane-av1lrflush: the pipelined loop-restoration band release flushes
6932 /// RU row 0 on the inter frame whose top superblock row carries a7112 /// RU row 0 on the inter frame whose top superblock row carries a
6933 /// 128-root rect -- the exact shape lanes/av1444sb.report.md r2 named as7113 /// 128-root rect -- the exact shape lanes/av1444sb.report.md r2 named as