forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-seg2' (291453c1): the sub-8x8 intra reader never read intra_segment_id and stamped a 1x1 mi footprint; film B under libaom adaptive quantization decodes exact at every ladder point

Browse files at this commit

4a5d8ea8762f44e509cc683d3deb5c76c77dfc98Ahmet Kaan GümüşParent: 03046cebcdc861
crates/ec-av1/src/decode.rsmodified+49 -5
@@ -877,6 +877,12 @@ pub(crate) fn set_segmentation(
877877 mi_cols: usize,
878878 prev: Option<&[u8]>, fctx: &crate::decode::FrameCtx,
879879 ) {
880+ if crate::envflags::env_flag!("EC_HDR") {
881+ eprintln!(
882+ "EC_SEGHDR enabled={} update_map={} temporal={} preskip={} last_active={} data={:?}",
883+ seg.enabled, seg.update_map, seg.temporal_update, seg.seg_id_pre_skip, seg.last_active_seg_id, seg.feature_data
884+ );
885+ }
880886 fctx.seg.with(|s| *s.borrow_mut() = seg);
881887 fctx.seg_mi_dims.with(|c| c.set((mi_rows, mi_cols)));
882888 fctx.seg_ids.with(|m| *m.borrow_mut() = vec![0u8; mi_rows * mi_cols]);
@@ -1076,7 +1082,13 @@ fn read_segment_id(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c:
10761082 let coded = dec.symbol(&mut cdfs.segment_id[ctx]) as i32;
10771083 hit!(SEG_ID_HITS);
10781084 let last_active = fctx.seg.with(|s| i32::from(s.borrow().last_active_seg_id));
1079- neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active) as u8
1085+ let id = neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active);
1086+ // Mirrors the oracle's own `EC_SEG mi_row=.. cdf=.. pred=.. coded=.. id=..`
1087+ // (libaom `decodemv.c:read_segment_id`), for a cross-decoder segment diff.
1088+ if crate::envflags::env_flag!("EC_TRACE_SEG") {
1089+ eprintln!("EC_SEG mi_row={mi_r} mi_col={mi_c} cdf={ctx} pred={pred} coded={coded} id={id} rng={}", dec.debug_state().0);
1090+ }
1091+ id as u8
10801092 }
10811093
10821094 /// `read_intra_segment_id` (spec 5.11.7): called twice per intra-frame block
@@ -15725,19 +15737,50 @@ fn read_intra_mode_sub8(
1572515737 skip_ctx: usize,
1572615738 allow_intrabc: bool,
1572715739 mi_r: usize,
15728- mi_c: usize, fctx: &crate::decode::FrameCtx,
15740+ mi_c: usize,
15741+ // The leaf's own mi footprint for `set_segment_id`: 1x1 for a `BLOCK_4X4`
15742+ // split leaf, 2x1/1x2 for an 8x4/4x8 one. Stamping 1x1 for a rect leaf
15743+ // left the second mi column at segment 0, which reads back as a
15744+ // neighbour's `cdf_index`/prediction two blocks later.
15745+ seg_w_mi: usize,
15746+ seg_h_mi: usize, fctx: &crate::decode::FrameCtx,
1572915747 ) -> Result<(bool, usize, i32, Option<(usize, i32, Option<(i32, i32)>)>, Option<usize>)> {
1573015748 let trace = crate::envflags::env_flag!("EC_AV1_TRACE");
15731- if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") {
15749+ let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP");
15750+ if ec_istep {
1573215751 eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sub8 rng={}", dec.debug_state().0);
1573315752 }
15753+ // Same `EC_ISTEP` ladder the square/rect readers print, so a cross-decoder
15754+ // range diff lands on the ELEMENT inside a 4x4 leaf, not just the block.
15755+ macro_rules! istep {
15756+ ($name:literal, $val:expr) => {
15757+ if ec_istep {
15758+ eprintln!("EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={}", $name, $val, dec.debug_state().0);
15759+ }
15760+ };
15761+ }
15762+ // lane-seg2: same `intra_segment_id` placement as the square and rect
15763+ // readers (spec 5.11.6) -- a `BLOCK_4X4` leaf codes its own `segment_id`
15764+ // like any other block, over its own 1x1 mi footprint. Dropping it here
15765+ // both ate a symbol (the tile desynced at the frame's first sub-8x8
15766+ // split) and left `cur_segment_id` on the previous block, so the leaf
15767+ // dequantized with a neighbour's `SEG_LVL_ALT_Q`.
15768+ if seg_id_pre_skip(fctx) {
15769+ intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx);
15770+ }
1573415771 let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0;
1573515772 if trace {
1573615773 eprintln!("TRACE sub8 skip mi=({mi_r},{mi_c}) ctx={skip_ctx} value={} rng={}", skip as i32, dec.debug_state().0);
1573715774 }
15775+ istep!("skip", skip as i32);
15776+ if !seg_id_pre_skip(fctx) {
15777+ intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx);
15778+ }
1573815779 maybe_read_cdef_idx(dec, mi_r, mi_c, skip, fctx);
15780+ istep!("cdef", 0);
1573915781 maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx);
1574015782 maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx);
15783+ istep!("dq", 0);
1574115784 if allow_intrabc {
1574215785 let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0;
1574315786 if use_intrabc {
@@ -15749,6 +15792,7 @@ fn read_intra_mode_sub8(
1574915792 let above_ctx = INTRA_MODE_CTX[above_mode];
1575015793 let left_ctx = INTRA_MODE_CTX[left_mode];
1575115794 let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]);
15795+ istep!("mode", mode);
1575215796 if trace {
1575315797 eprintln!("TRACE sub8 y_mode mi=({mi_r},{mi_c}) ctx=({above_ctx},{left_ctx}) value={mode} rng={}", dec.debug_state().0);
1575415798 }
@@ -15865,7 +15909,7 @@ fn decode_leaf_split4(
1586515909 let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8(
1586615910 dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra,
1586715911 filter_intra_size_class(4).expect("BLOCK_4X4 has its own filter_intra row"),
15868- skip_ctx, allow_intrabc, lmi.0, lmi.1, fctx,
15912+ skip_ctx, allow_intrabc, lmi.0, lmi.1, 1, 1, fctx,
1586915913 )?;
1587015914 leaf_modes[i] = mode;
1587115915 leaf_skips[i] = skip;
@@ -16091,7 +16135,7 @@ fn decode_leaf_rect8(
1609116135 dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra,
1609216136 filter_intra_size_class_rect(bw, bh)
1609316137 .expect("BLOCK_4X8/BLOCK_8X4 are inside av1_filter_intra_allowed_bsize"),
16094- skip_ctx, allow_intrabc, lmi.0, lmi.1, fctx,
16138+ skip_ctx, allow_intrabc, lmi.0, lmi.1, bw / 4, bh / 4, fctx,
1609516139 )?;
1609616140 if filter_intra.is_some() {
1609716141 // lane-fi8 r1: `predict_filter_intra` walks its 4x2 patches over
crates/ec-av1/src/stream.rsmodified+87 -0
@@ -35385,4 +35385,91 @@ pub(crate) mod tests {
3538535385 eprintln!("{NAME}: {checked} straddling reference points decoded sample-exact");
3538635386 }
3538735387
35388+ /// lane-seg2: a SEGMENTED key frame that also codes sub-8x8 INTRA leaves.
35389+ ///
35390+ /// WHY IT EXISTS: `read_intra_mode_sub8` -- the reader every `BLOCK_4X4`
35391+ /// split leaf and every 8x4/4x8 leaf goes through -- never called spec
35392+ /// 5.11.6's `intra_segment_id` at all. On a stream with segmentation on
35393+ /// it therefore (a) left the block's `segment_id` symbol unread, so the
35394+ /// tile desynced from the frame's first sub-8x8 partition on, and (b)
35395+ /// dequantized the leaf with the PREVIOUS block's `SEG_LVL_ALT_Q`.
35396+ /// Reading it with a hardcoded 1x1 mi footprint was the second half of
35397+ /// the same defect: an 8x4 leaf left its second mi cell at segment 0,
35398+ /// which the next block read back as its spatial prediction and CDF row.
35399+ ///
35400+ /// RECIPE: flat gray + heavy `noise` (the contrast is what turns libaom's
35401+ /// activity segmentation on -- a `testsrc2`/bars source codes ONE segment
35402+ /// and passes with the defect in, ledger dead-end `gate film rows are
35403+ /// colour bars`), at `-crf 5 -cpu-used 2`, which is the measured corner
35404+ /// where libaom answers with sub-8x8 intra partitions. Measured on this
35405+ /// exact stream: red before either half of the fix (134k/134k samples
35406+ /// off), green with both.
35407+ #[test]
35408+ fn a_segmented_key_frame_with_sub_8x8_intra_leaves_decodes_exactly() {
35409+ const NAME: &str = "a_segmented_key_frame_with_sub_8x8_intra_leaves_decodes_exactly";
35410+ let _gate_lock = lock_gate_counters();
35411+ if !have_ffmpeg() {
35412+ eprintln!("SKIP {NAME}: no ffmpeg");
35413+ return;
35414+ }
35415+ const FRAMES: usize = 4;
35416+ let (w, h) = (384usize, 152usize);
35417+ let src = format!(
35418+ "color=c=gray:s={w}x{h}:r=25,noise=alls=80:all_seed=7:allf=t+u"
35419+ );
35420+ let args: Vec<String> = [
35421+ "-v", "error", "-f", "lavfi", "-i", &src, "-frames:v", "4", "-pix_fmt", "yuv420p",
35422+ "-an", "-threads", "1", "-g", "4", "-c:v", "libaom-av1", "-cpu-used", "2", "-b:v",
35423+ "0", "-crf", "5", "-aq-mode", "1", "-f", "obu", "-",
35424+ ]
35425+ .iter()
35426+ .map(|s| (*s).to_string())
35427+ .collect();
35428+ let out = Command::new("ffmpeg")
35429+ .args(&args)
35430+ .stdin(Stdio::null())
35431+ .stdout(Stdio::piped())
35432+ .stderr(Stdio::piped())
35433+ .output()
35434+ .expect("ffmpeg failed to run");
35435+ if !out.status.success() {
35436+ eprintln!(
35437+ "SKIP {NAME}: ffmpeg cannot encode with libaom-av1 ({})",
35438+ String::from_utf8_lossy(&out.stderr).lines().last().unwrap_or("")
35439+ );
35440+ return;
35441+ }
35442+ let stream = out.stdout;
35443+ crate::decode::reset_segment_hits();
35444+ let want = ffmpeg_decode_sequence(&stream, w, h, FRAMES);
35445+ let got = decode_stream(&stream).unwrap_or_else(|e| panic!("{NAME}: refused: {e}"));
35446+ assert_eq!(got.len(), want.len(), "{NAME}: frame count");
35447+ for (f, (o, r)) in got.iter().zip(&want).enumerate() {
35448+ for (plane, a, b) in [("Y", &o.y, &r.y), ("U", &o.u, &r.u), ("V", &o.v, &r.v)] {
35449+ if let Some(i) = a.iter().zip(b.iter()).position(|(x, y)| x != y) {
35450+ let stride = if plane == "Y" { w } else { w / 2 };
35451+ panic!(
35452+ "{NAME}: frame {f} plane {plane} sample ({}, {}): ours {} vs ffmpeg {}",
35453+ i % stride,
35454+ i / stride,
35455+ a[i],
35456+ b[i],
35457+ );
35458+ }
35459+ }
35460+ }
35461+ // The property is asserted, not hoped for: without more than one
35462+ // segment this row would pass with the defect still in.
35463+ assert!(
35464+ crate::decode::segment_ids_seen() >= 2,
35465+ "{NAME}: vacuous run -- {} distinct segment ids",
35466+ crate::decode::segment_ids_seen()
35467+ );
35468+ eprintln!(
35469+ "{NAME}: {FRAMES} frames sample-exact, {} segment_id symbols, {} distinct ids",
35470+ crate::decode::segment_id_hits(),
35471+ crate::decode::segment_ids_seen()
35472+ );
35473+ }
35474+
3538835475 }
lanes/seg2.report.mdadded+106 -0
@@ -0,0 +1,106 @@
1+# lane-seg2 — libaom `-aq-mode 1` on film B: the sub-8x8 intra reader had no segment id
2+
3+Two defects, one reader. Class: **`symbol-consumption-gap`** (a syntax element
4+the spec codes for every block was never read on one block-shape path), with
5+the second half in the **`tx-grid-published-block-side`** shape (a per-mi map
6+stamped with the wrong footprint).
7+
8+## First divergent symbol
9+
10+`filmB_aom45.obu` (`crop=1920:1024`, 12 frames, libaom `-cpu-used 6 -crf 45
11+-aq-mode 1`), frame 0 = KEY frame, first bad luma sample (883, 888).
12+
13+Cross-decoder `EC_IMODE` range ladder (aomdec `EC_TRACE_MODE_STEP` vs our
14+`EC_TRACE_MODE_STEP`), blocks 1..1799 identical, block 1800 diverges:
15+
16+```
17+aom EC_IMODE mi_row=226 mi_col=218 bsize=0 rng=35072 (BLOCK_4X4 split leaf)
18+ours EC_IMODE mi_row=226 mi_col=218 fn=sub8 rng=35072
19+aom ... next block EC_IMODE mi_row=226 mi_col=219 rng=43528
20+ours ... next block EC_IMODE mi_row=226 mi_col=219 rng=33544
21+```
22+
23+i.e. the desync is INSIDE the frame's first sub-8x8 intra leaf.
24+
25+At `crf 5` the same ladder puts the first divergence at mi (198, 148), and the
26+new `EC_SEG` trace (added to both decoders) names the element exactly:
27+
28+```
29+aom EC_SEG mi_row=198 mi_col=148 cdf=1 pred=1 coded=1 id=2 rng=61592
30+ours EC_SEG mi_row=198 mi_col=148 cdf=0 pred=0 coded=1 id=1 rng=35672
31+```
32+
33+`cdf_index` 0 vs 1 and prediction 0 vs 1: our `av1_get_spatial_seg_pred` read
34+segment 0 out of the LEFT neighbour mi (198, 147).
35+
36+## Root cause
37+
38+`crates/ec-av1/src/decode.rs read_intra_mode_sub8` — the reader every
39+`BLOCK_4X4` split leaf AND every 8x4/4x8 leaf goes through — never called
40+spec 5.11.6's `intra_segment_id` at all:
41+
42+1. **the symbol was never read.** On a stream with `segmentation_enabled` the
43+ leaf's `segment_id` symbol stayed in the bitstream, so the tile desynced
44+ from the frame's first sub-8x8 partition on (and `cur_segment_id` kept the
45+ PREVIOUS block's value, so the leaf also dequantized with a neighbour's
46+ `SEG_LVL_ALT_Q`). Fixed at `098e5e1c`.
47+2. **the stamp was 1x1.** Reading it with a hardcoded 1x1 mi footprint is the
48+ second half: the same reader serves 8x4/4x8 leaves, whose second mi cell
49+ stayed at segment 0 and was read back by the NEXT block as its spatial
50+ prediction and CDF row. Fixed at `8e58fcc7` (footprint threaded from the
51+ leaf's own `bw`/`bh`: 1x1 for a 4x4 leaf, `bw/4 x bh/4` for a rect one).
52+
53+Both halves are needed: with only (1) the film B libaom crf 5 and 20 points are
54+still 9.2M/9.7M samples off; the witness below is red under either half alone.
55+
56+## Same-shape sweep — every site that reads or stamps the segment map
57+
58+| site (`decode.rs`) | reader | footprint passed | verdict |
59+| --- | --- | --- | --- |
60+| `read_intra_mode_rect` (8922/8927) | rect intra | `(bw/4, bh/4)` | ok |
61+| `decode_leaf8`/square (13031/13039) | square intra | `(side/4, side/4)` | ok |
62+| `read_intra_mode_sub8` (15747/15755) | 4x4 split + 8x4/4x8 intra leaf | **absent, then 1x1** | **FIXED (both halves)** |
63+| inter block reader (26625/26644) | inter | `(write_w/4, write_h/4)` | ok |
64+| `decode_inter_sub8_split4` (30394/30398) | 4x4 inter leaf | `(1, 1)` | ok (BLOCK_4X4 is 1x1 mi) |
65+| inter 4x8/8x4 leaf (31437/31441) | rect inter leaf | `(bw/MI, bh/MI)` | ok |
66+| inter 8x8 leaf (32181/32189) | 8x8 inter leaf | `(2, 2)` | ok |
67+
68+Consumers of the map, all reading through the two single points
69+`segment_id_at` / `block_q_idx` (which is why only the WRITE side was wrong):
70+`spatial_seg_pred`, `predicted_segment_id`, `block_q_idx` (`SEG_LVL_ALT_Q`),
71+the deblocker's per-segment `lf_level`. No second write path exists.
72+
73+## Witness
74+
75+`stream::tests::a_segmented_key_frame_with_sub_8x8_intra_leaves_decodes_exactly`
76+(new, in the existing `stream::` test binary, no fixture committed):
77+`color=c=gray:s=384x152 , noise=alls=80:all_seed=7:allf=t+u` -> ffmpeg
78+`libaom-av1 -cpu-used 2 -b:v 0 -crf 5 -aq-mode 1 -g 4`, 4 frames, decoded by
79+ffmpeg and by us, all three planes, plus `segment_ids_seen() >= 2` so a
80+one-segment (vacuous) run cannot pass.
81+
82+- green at HEAD: `4 frames sample-exact, 1963 segment_id symbols, 3 distinct ids`, 2.16 s
83+- red with the fix reverted: `FAILED. 0 passed; 1 failed` (measured 134464
84+ samples off pre-fix, 134082 off with only the first half of the fix)
85+
86+`-cpu-used 2` is load-bearing: at `-cpu-used 6` (the straddle gate's recipe)
87+libaom answers this source with no sub-8x8 intra partition and the row passes
88+with the defect in — measured on four probe recipes.
89+
90+## Ladder (12 frames, all planes, vs ffmpeg; streams from lane-golomb2)
91+
92+| stream | pre-fix | after `098e5e1c` | HEAD |
93+| --- | --- | --- | --- |
94+| film B 1920x1024 libaom crf 5 / 20 / 35 / 45, `-aq-mode 1` | 4x DIFF | 2 EXACT, 2 DIFF | **4/4 EXACT** |
95+| film B 1920x1024 rav1e q 50 / 100 / 150 / 200 | 4/4 EXACT | 4/4 EXACT | **4/4 EXACT** |
96+| film A 1920x792 libaom crf 5 / 20 / 35 / 45 | 4/4 EXACT | — | **4/4 EXACT** |
97+| film A 1920x792 rav1e q 50 / 100 / 150 / 200 | 4/4 EXACT | — | **4/4 EXACT** |
98+
99+## Instruments added (kept)
100+
101+- `EC_TRACE_SEG=1` on BOTH decoders: `EC_SEG mi_row= mi_col= cdf= pred= coded= id= rng=`
102+ (libaom side patched in `~/.cache/aom-oracle/src/av1/decoder/decodemv.c`,
103+ rebuilt in place).
104+- `EC_HDR=1` also prints `EC_SEGHDR enabled/update_map/temporal/preskip/last_active/data`.
105+- `EC_TRACE_MODE_STEP=1` now prints the `skip`/`cdef`/`dq`/`mode` ladder for
106+ sub-8x8 leaves too, which is what separated "segment id" from "cdef".