forge
Explore
Log in

Don't you dare go Hollow

A skipped intra block predicts per transform unit, not per block

Browse files at this commit

5b4ff4a4a6abedbc55a839e38b8dbb47683be6f6Ahmet Kaan GümüşParent: d0545fa
  • fix
    • a skipped intra block predicts per transform unit, not per block
Show full message
fix(av1): a skipped intra block predicts per transform unit, not per block

An intra block's tx_depth is read whether or not the block is skipped (only
an INTER skip forces the largest transform), and spec 7.11.2 predicts one
transform unit at a time -- each unit's edges are the units already written
left/above of it inside the same block. Our two skip branches (decode_block,
decode_leaf8) predicted the whole block in one call, which for a 32x32 DC
block with 8x8 units gives 123 where libaom gives 124 sixteen times.

Mid-film 3840x1608 10-bit key frame: 2403 luma samples wrong (|delta| <= 2)
-> 0, all three planes bit-exact; the earlier key frame stays bit-exact.
Stage split done with new 16-bit dump rungs on both sides (the 8-bit ones
truncate the defect away) and a new EC_PREDND oracle rung (EC_PRED sat after
the highbd early return and printed nothing on a 10-bit stream).

New counter SKIP_SPLIT_TX_HITS: a skipped block codes no coefficients, so
nothing but the pixels witnesses this path. No in-repo gate landed -- see
lanes/hgkf-r2.report.md for the swept recipes and the deferral.
crates/ec-av1/src/decode.rsmodified+182 -24
@@ -1309,6 +1309,24 @@ pub(crate) fn palette_split_tx_hits() -> usize {
13091309 PALETTE_SPLIT_TX_HITS.with(|c| c.get())
13101310 }
13111311
1312+// lane-hgkf r2: SKIPPED intra blocks whose `tx_depth` split the luma transform
1313+// and which are therefore predicted one transform unit at a time (a skipped
1314+// block codes no coefficients, so nothing else in the stream witnesses this
1315+// path -- only the pixels do).
1316+thread_local! {
1317+ static SKIP_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
1318+}
1319+
1320+/// Current value of [`SKIP_SPLIT_TX_HITS`] (blocks, not units).
1321+pub fn skip_split_tx_hits() -> usize {
1322+ SKIP_SPLIT_TX_HITS.with(|c| c.get())
1323+}
1324+
1325+/// Zeroes [`SKIP_SPLIT_TX_HITS`] before a gate's own decode attempt.
1326+pub fn reset_skip_split_tx_hits() {
1327+ SKIP_SPLIT_TX_HITS.with(|c| c.set(0));
1328+}
1329+
13121330 // lane-intrainter r1: intra blocks inside an INTER frame whose `tx_depth`
13131331 // symbol split their luma transform, bucketed by block side (8, 16, 32, 64) --
13141332 // the gate's proof that the lifted path actually ran, per size.
@@ -7690,6 +7708,26 @@ fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 {
76907708 /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone.
76917709 /// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written
76927710 /// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`.
7711+/// lane-hgkf r2: 16-bit cropped stage dump, the high-bitdepth twin of
7712+/// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects) --
7713+/// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE).
7714+fn dump_stage16(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf, fw: usize, fh: usize) {
7715+ use std::io::Write;
7716+ if let Ok(path) = std::env::var(var)
7717+ && let Ok(mut f) = std::fs::File::create(format!("{path}.f0"))
7718+ {
7719+ for (p, w, h) in [(y, fw, fh), (u, fw.div_ceil(2), fh.div_ceil(2)), (v, fw.div_ceil(2), fh.div_ceil(2))] {
7720+ for row in 0..h {
7721+ let mut bytes = Vec::with_capacity(w * 2);
7722+ for &s in &p.data[row * p.width..][..w] {
7723+ bytes.extend_from_slice(&s.to_le_bytes());
7724+ }
7725+ let _ = f.write_all(&bytes);
7726+ }
7727+ }
7728+ }
7729+}
7730+
76937731 fn dump_stage(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf) {
76947732 use std::io::Write;
76957733 if let Ok(path) = std::env::var(var)
@@ -7835,6 +7873,7 @@ impl PlaneBuf {
78357873 /// via [`Self::edges_rect`] and [`crate::intra::predict`]'s own
78367874 /// already-rect-capable `(bw, bh)` pair.
78377875 #[allow(clippy::too_many_arguments)]
7876+ #[track_caller]
78387877 fn reconstruct_rect(
78397878 &mut self,
78407879 x: usize,
@@ -7893,6 +7932,16 @@ impl PlaneBuf {
78937932 }
78947933 prediction
78957934 };
7935+ // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's
7936+ // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin).
7937+ if std::env::var_os("EC_PRED").is_some() {
7938+ let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum();
7939+ let row0: Vec<u16> = prediction[..bw.min(8)].to_vec();
7940+ let col0: Vec<u16> = (0..bh.min(8)).map(|r| prediction[r * bw]).collect();
7941+ eprintln!(
7942+ "@{} OUR_PRED x={x} y={y} bw={bw} bh={bh} mode={mode} ad={angle_delta} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller()
7943+ );
7944+ }
78967945 for row in 0..bh {
78977946 for col in 0..bw {
78987947 let idx = row * bw + col;
@@ -7912,6 +7961,7 @@ impl PlaneBuf {
79127961 /// nudges the prediction by [`cfl_scaled`]'s per-sample amount before the
79137962 /// residual is added — `av1_cfl_predict_block`'s own clip-then-add-residual
79147963 /// order (cfl.c).
7964+ #[track_caller]
79157965 fn reconstruct(
79167966 &mut self,
79177967 x: usize,
@@ -7975,6 +8025,16 @@ impl PlaneBuf {
79758025 }
79768026 prediction
79778027 };
8028+ // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's
8029+ // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin).
8030+ if std::env::var_os("EC_PRED").is_some() {
8031+ let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum();
8032+ let row0: Vec<u16> = prediction[..side.min(8)].to_vec();
8033+ let col0: Vec<u16> = (0..side.min(8)).map(|r| prediction[r * side]).collect();
8034+ eprintln!(
8035+ "@{} OUR_PRED x={x} y={y} side={side} side={side} mode={mode} ad={angle_delta} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller()
8036+ );
8037+ }
79788038 for row in 0..side {
79798039 for col in 0..side {
79808040 let idx = row * side + col;
@@ -8306,18 +8366,69 @@ fn decode_block(
83068366 if skip {
83078367 // A skipped block codes no residual syntax at all (spec 5.11.34):
83088368 // straight prediction, on every plane.
8309- y.reconstruct(
8310- px,
8311- py,
8312- side,
8313- mode,
8314- angle_delta_y,
8315- reach,
8316- &vec![0i32; side * side],
8317- None,
8318- filter_intra,
8319- smooth_neighbor,
8320- );
8369+ //
8370+ // lane-hgkf r2: an intra block's `tx_depth` is read whether or not it
8371+ // is skipped (only an INTER skip forces the largest transform), and
8372+ // spec 7.11.2 / libaom `predict_and_reconstruct_intra_block` predicts
8373+ // ONE TRANSFORM UNIT AT A TIME -- each unit's edges are the units
8374+ // already written left/above of it inside this same block. Predicting
8375+ // the whole `side`x`side` at once gives a different (uniformly off by
8376+ // ~1) answer, which is exactly the residue this lane was chasing.
8377+ // The palette/intrabc override path keeps the single-call shape (its
8378+ // per-TU windowing is the non-skip loop's own `palette_y_buf` slice,
8379+ // untested for skip and unreachable in these streams).
8380+ let split_tx_skip =
8381+ tx_select && logical_tx < side && palette_y_buf.is_none() && intrabc_bufs.is_none();
8382+ if split_tx_skip {
8383+ SKIP_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
8384+ let n_axis = side / logical_tx;
8385+ for tu_row in 0..n_axis {
8386+ for tu_col in 0..n_axis {
8387+ let tu_px = px + tu_col * logical_tx;
8388+ let tu_py = py + tu_row * logical_tx;
8389+ if tu_px >= y.true_width || tu_py >= y.true_height {
8390+ continue;
8391+ }
8392+ let tu_reach = tu_reach(
8393+ side,
8394+ side,
8395+ tu_col * logical_tx,
8396+ tu_row * logical_tx,
8397+ logical_tx,
8398+ reach,
8399+ px,
8400+ py,
8401+ y.width,
8402+ y.height,
8403+ );
8404+ y.reconstruct(
8405+ tu_px,
8406+ tu_py,
8407+ logical_tx,
8408+ mode,
8409+ angle_delta_y,
8410+ tu_reach,
8411+ &vec![0i32; logical_tx * logical_tx],
8412+ None,
8413+ filter_intra,
8414+ smooth_neighbor,
8415+ );
8416+ }
8417+ }
8418+ } else {
8419+ y.reconstruct(
8420+ px,
8421+ py,
8422+ side,
8423+ mode,
8424+ angle_delta_y,
8425+ reach,
8426+ &vec![0i32; side * side],
8427+ None,
8428+ filter_intra,
8429+ smooth_neighbor,
8430+ );
8431+ }
83218432 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
83228433 if let Some((ub, _)) = &palette_uv_bufs {
83238434 set_palette_pred(ub.clone());
@@ -8735,18 +8846,59 @@ fn decode_leaf8(
87358846 let (cpx, cpy) = (px / 2, py / 2);
87368847 let (luma_grid, u_grid, v_grid);
87378848 if skip {
8738- y.reconstruct(
8739- px,
8740- py,
8741- 8,
8742- mode,
8743- angle_delta_y,
8744- reach,
8745- &vec![0i32; 64],
8746- None,
8747- filter_intra,
8748- smooth_neighbor,
8749- );
8849+ // lane-hgkf r2 (same shape as `decode_block`'s own skip branch): an
8850+ // 8x8 leaf whose `tx_depth` resolved to TX_4X4 predicts FOUR 4x4
8851+ // transform units, each off the units already written left/above of
8852+ // it inside this same leaf -- an intra block's transform size is read
8853+ // whether or not it is skipped, and spec 7.11.2 predicts per unit.
8854+ if resolved == 4 {
8855+ SKIP_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
8856+ for tu_row in 0..2 {
8857+ for tu_col in 0..2 {
8858+ let (tu_px, tu_py) = (px + tu_col * 4, py + tu_row * 4);
8859+ if tu_px >= y.true_width || tu_py >= y.true_height {
8860+ continue;
8861+ }
8862+ let tu_reach = tu_reach(
8863+ 8,
8864+ 8,
8865+ tu_col * 4,
8866+ tu_row * 4,
8867+ 4,
8868+ reach,
8869+ px,
8870+ py,
8871+ y.width,
8872+ y.height,
8873+ );
8874+ y.reconstruct(
8875+ tu_px,
8876+ tu_py,
8877+ 4,
8878+ mode,
8879+ angle_delta_y,
8880+ tu_reach,
8881+ &vec![0i32; 16],
8882+ None,
8883+ filter_intra,
8884+ smooth_neighbor,
8885+ );
8886+ }
8887+ }
8888+ } else {
8889+ y.reconstruct(
8890+ px,
8891+ py,
8892+ 8,
8893+ mode,
8894+ angle_delta_y,
8895+ reach,
8896+ &vec![0i32; 64],
8897+ None,
8898+ filter_intra,
8899+ smooth_neighbor,
8900+ );
8901+ }
87508902 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
87518903 u.reconstruct(
87528904 cpx,
@@ -12721,6 +12873,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1272112873 // that isolate whether a decode-order frame's mismatch already exists
1272212874 // in reconstruction (this dump) vs is introduced by the loop filter
1272312875 // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump).
12876+ dump_stage16("EC_AV1_PREFILT_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
1272412877 if let Ok(path) = std::env::var("EC_AV1_PREFILT_DUMP") {
1272512878 use std::io::Write;
1272612879 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
@@ -12764,8 +12917,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1276412917 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
1276512918 // dump above these bisect WHICH filter stage introduced a mismatch.
1276612919 dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v);
12920+ dump_stage16("EC_AV1_POSTDEBLOCK_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
1276712921 apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours);
1276812922 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
12923+ dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
1276912924 apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid);
1277012925
1277112926 let (fw, fh) = (frame_width as usize, frame_height as usize);
@@ -19843,6 +19998,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1984319998 // that isolate whether a decode-order frame's mismatch already exists
1984419999 // in reconstruction (this dump) vs is introduced by the loop filter
1984520000 // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump).
20001+ dump_stage16("EC_AV1_PREFILT_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
1984620002 if let Ok(path) = std::env::var("EC_AV1_PREFILT_DUMP") {
1984720003 use std::io::Write;
1984820004 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
@@ -19869,8 +20025,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1986920025 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
1987020026 // dump above these bisect WHICH filter stage introduced a mismatch.
1987120027 dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v);
20028+ dump_stage16("EC_AV1_POSTDEBLOCK_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
1987220029 apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours);
1987320030 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
20031+ dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
1987420032 apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid);
1987520033
1987620034 let motion_field = build_motion_field(
lanes/hgkf-r2.report.mdadded+90 -0
@@ -0,0 +1,90 @@
1+# lane-hgkf r2 — a skipped intra block predicts per TRANSFORM UNIT
2+
3+Branch `lane-hgkf`, on top of r1 (`4939695`). Target: the r1 residue on the
4+mid-film 3840x1608 10-bit key frame — 2403 luma samples wrong, |delta| <= 2,
5+chroma exact, entropy ladder exact frame-wide.
6+
7+## Stage split (the charter's step 1)
8+
9+Both decoders' 8-bit stage rungs truncate 10-bit samples, so a |delta| <= 2
10+defect is invisible in them. Added a 16-bit twin on both sides:
11+`ec_dump16` in the oracle (`EC_AV1_PREFILT_DUMP16` /
12+`EC_AV1_POSTDEBLOCK_DUMP16`, `av1/decoder/decodeframe.c`) and `dump_stage16`
13+in ours (same two names plus `EC_AV1_POSTCDEF_DUMP16`, `decode.rs`).
14+
15+EVIDENCE: /home/user/.cache/hgkf-work/{o,a}_pre.f0, {o,a}_pdb.f0, o_fin.f0.yuv vs ref_kf_4500.yuv | ours vs instrumented aomdec, 16-bit cropped planes at PREFILT / POSTDEBLOCK / FINAL | PREFILT Y 2372 wrong (maxabs 3, first row 416 col 3170, ours 210 vs 209), POSTDEBLOCK identical set, FINAL 2403 -> **the defect is already in reconstruction**; deblock/CDEF/LR only smear it
16+
17+## Root cause (class `decision-at-wrong-granularity`)
18+
19+An intra block's `tx_depth` is read whether or not the block is skipped (only
20+an INTER skip forces the largest transform), and spec 7.11.2 / libaom
21+`predict_and_reconstruct_intra_block` predicts **one transform unit at a
22+time** — each unit's above/left edges are the units already reconstructed
23+inside that same block. Our skip branches predicted the whole block in one
24+call:
25+
26+* `crates/ec-av1/src/decode.rs` `decode_block`'s `if skip` arm — a 32x32 block
27+ with `logical_tx == 8` was one 32x32 DC prediction (123) where libaom does 16
28+ 8x8 DC predictions (124 each).
29+* `crates/ec-av1/src/decode.rs` `decode_leaf8`'s `if skip` arm — the same shape
30+ one level down: an 8x8 leaf with `resolved == 4` is four 4x4 units.
31+
32+Both now loop the units with a zero residual, reusing the non-skip loop's own
33+`tu_reach` geometry and its out-of-frame TU-origin clip. The palette/intrabc
34+override keeps the single-call shape (its per-TU windowing is the non-skip
35+path's, untested for skip and unreachable in these streams).
36+
37+New counter `SKIP_SPLIT_TX_HITS` (`skip_split_tx_hits()` /
38+`reset_skip_split_tx_hits()`, blocks not units): the only witness this path
39+has, since a skipped block codes no coefficients at all.
40+
41+## Evidence
42+
43+EVIDENCE: /home/user/.cache/hgkf-work/aom_pred.txt, our_pred.txt | new oracle rung `EC_PREDND` (non-directional high-bitdepth prediction; the pre-existing `EC_PRED` rung sat AFTER the `is_hbd` early return and printed nothing on a 10-bit stream) vs our new `OUR_PRED` rung | at px(1088,1216) libaom prints 16 8x8 DC predictions of 124, ours one 32x32 of 123 — sum 7936 x16 vs 125952
44+EVIDENCE: /home/user/.cache/hgkf-work/fx_kf_4500.f0.yuv vs ref_kf_4500.yuv | `dump_yuv kf_4500.obu` vs `ffmpeg -pix_fmt yuv420p10le`, 16-bit compare | 2403 wrong -> **0, all three planes bit-exact**
45+EVIDENCE: /home/user/.cache/hgkf-work/fx_kf_0.f0.yuv vs ref_kf_0.yuv | same command pair | **0, still bit-exact** (r1's frame did not regress)
46+
47+Scratch pin (not a repo fixture, scratch-only as the charter requires):
48+`.../scratchpad/census2/kf_4500.obu` sha256
49+`80529979df3735bce82f9c48fb0be66ec2019f4316ad6e617def86141c2b9010` — 48 split-skip
50+TU predictions, decodes bit-exact; `kf_0.obu` sha256
51+`53c311dec790bb2413afdde92cb24de498a7c4f66f6f0c7f4f5910ab0d2c65dc` — 0 hits, bit-exact.
52+
53+## Gate: NOT LANDED — no encoder recipe reached the path
54+
55+`deferred: an in-repo gate for split-tx skipped intra prediction — no
56+reproducible encoder recipe found in this round's budget — unblocked by either
57+an aomenc flag combination that keeps a split `tx_depth` on a block it then
58+skips, or by lifting the 1:4-partition refusal so an SVT-AV1 stream can be
59+decoded whole.`
60+
61+Swept, all 10-bit, all with the decode's success checked (a refused decode's
62+counter is a `counter-from-refused-stream` artefact, so every arm below reports
63+both):
64+
65+* aomenc `--enable-tx-size-search=1`, mandelbrot/testsrc2/low-amplitude noise,
66+ 256x192..1280x720, cq 12..55, cpu-used 0..8, aq-mode 0/1, 8- and 10-bit:
67+ **0 hits on every arm that decoded**. aomenc's tx search ties on a zero
68+ residual and keeps the largest transform, so it essentially never writes this
69+ combination.
70+* ffmpeg `libsvtav1` (v3.1.2) preset 4, 1280x720 10-bit: **reaches the path**
71+ (4..24 units at crf 25..55) — but every one of those streams also uses a 1:4
72+ partition below 16x16 / intrabc / palette, all of which this decoder refuses,
73+ so nothing could be pixel-compared.
74+* `gradients=size=1280x720:n=4` + libsvtav1 crf 30 decoded whole once with 16
75+ units, but lavfi `gradients` is not reproducible (3 encodes, 3 different
76+ sha256) — the known `seeded-fixture-not-reproducible` trap, rejected as a
77+ fixture.
78+
79+## Same-shape sweep
80+
81+Every intra skip-prediction site in `decode.rs`: `decode_block` (fixed),
82+`decode_leaf8` (fixed), the 4x8/8x4 leaf at `decode.rs:9570` (already per-TU,
83+lane-palette2), the inter-frame vartx leaf at `decode.rs:16450` (already
84+per-TU), the 4x4 leaf (no split exists). Chroma skip prediction is still
85+whole-block: for 4:2:0 the chroma transform only splits above a 64x64 luma
86+block, which no gate or film frame here reaches —
87+`deferred: chroma skip prediction per TU above 64x64 luma — no stream reaches
88+it — unblocked by a 128x128-superblock 10-bit gate.`
89+
90+Suite: `cargo test -p ec-av1 --lib` (user unit, log `$HOME/.cache/hgkf-suite-r2.log`) -- **356 passed / 0 failed / 29 ignored**, 416s.