forge
Explore
Log in

Don't you dare go Hollow

lane-av1unwritten: a 4:2:2 chroma block the walk never covered

Browse files at this commit

24bb8e90ec40991a50dee2a8ea8c3795ee27282eAhmet Kaan GümüşParent: 05c4451
sub8_leaf_chroma422 wrote ONE square TX_4X4 where libaom writes ONE TX_4X8
(av1_get_max_uv_txsize(BLOCK_4X8, 1, 0) == max_txsize_rect_lookup[BLOCK_4X8],
through get_plane_block_size's per-axis MIN), so every 4:2:2 8x8 block coded
PARTITION_HORZ left the bottom four chroma rows of its plane block unwritten.
The uninitialised plane memory reached the output: the pinned
440_request_is_422.obu hashed five different ways over five runs while every
census count read identical.

The fix routes that one shape to leaf8_chroma422_unit -- the port that already
exists for a BLOCK_8X8 4:2:2 leaf, same TxbSet/scan/inverse-transform/push --
with the unit's reach, entropy context and CfL AC span following the taller
unit. The two intrabc shapes hand their frame copy over through that function's
existing pred_override. The square arms stay for leaf=(4,4) (the
decode_leaf_split4 route already tiles 4x8 correctly) and for lossless, where
av1_get_tx_size returns TX_4X4 on every plane and libaom walks two stacked
4x4 units.

Evidence: sentinel census of all 127 committed fixtures is empty on every
frame before AND after except the witness, which goes 112 -> 0 per chroma
plane; every fixture's EC_AV1_FINAL_DUMP hashes byte-identically pre/post
except the witness; the defective leaf=(4,8) shape fires 0 times in 3528 real
sub8_leaf_chroma422 invocations across the 4:2:2 corpus (7 on the witness), so
no committed valid stream regresses. New instrument EC_AV1_SENTINEL_CENSUS
plus decode::set_plane_sentinel / take_unwritten_samples / take_census_scanned,
and the gate a_422_header_over_a_420_tile_leaves_no_sample_unwritten (reds at
224 unwritten samples with the fix disabled).
crates/ec-av1/src/decode.rsmodified+232 -5
@@ -21235,6 +21235,113 @@ fn dump_prefilter_wide(y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) {
2123521235 }
2123621236 }
2123721237
21238+/// `EC_AV1_SENTINEL_CENSUS=1`: at each frame's pre-deblock point, print an
21239+/// EXACT census of the samples the tile walk never wrote -- with
21240+/// `EC_AV1_PLANE_SENTINEL=1` every plane still holding [`PLANE_SENTINEL`] at
21241+/// this point is a sample no block reconstructed, and no legal sample can
21242+/// equal `0xDEAD` (reconstruction clamps to `(1 << bit_depth) - 1`).
21243+///
21244+/// This is the shipped form of the four-line temporary scan the
21245+/// `lane/av1422seed` census used: it replaces a wrong-sample diff (which says
21246+/// where the OUTPUT is wrong, not whether the block was written at all) with a
21247+/// direct count plus the coordinates of every hole, grouped into per-row
21248+/// spans so a large hole stays readable. Scans each plane over its own
21249+/// `true_width` x `true_height` extent: past that is the padded coding
21250+/// surface's invented tail, which no decoder ever reads.
21251+fn census_unwritten(y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) {
21252+ if !plane_sentinel_on() {
21253+ return;
21254+ }
21255+ let idx = PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst);
21256+ for (name, p) in [("Y", y), ("U", u), ("V", v)] {
21257+ let (w, h) = (p.true_width.min(p.width), p.true_height);
21258+ let mut total = 0usize;
21259+ let mut spans: Vec<(usize, usize, usize)> = Vec::new();
21260+ for row in 0..h {
21261+ let mut run: Option<(usize, usize)> = None;
21262+ for col in 0..w {
21263+ if p.data[row * p.width + col] == PLANE_SENTINEL {
21264+ total += 1;
21265+ run = Some(match run {
21266+ None => (col, col),
21267+ Some((a, _)) => (a, col),
21268+ });
21269+ }
21270+ if run.is_some()
21271+ && (col + 1 == w || p.data[row * p.width + col + 1] != PLANE_SENTINEL)
21272+ {
21273+ let (a, b) = run.take().expect("a run is open here");
21274+ spans.push((row, a, b));
21275+ }
21276+ }
21277+ }
21278+ UNWRITTEN_SAMPLES.fetch_add(total, std::sync::atomic::Ordering::SeqCst);
21279+ CENSUS_SCANNED.fetch_add(w * h, std::sync::atomic::Ordering::SeqCst);
21280+ if total == 0 {
21281+ eprintln!("SENTINEL_CENSUS idx={idx} plane={name} extent={w}x{h} unwritten=0");
21282+ continue;
21283+ }
21284+ let body: Vec<String> = spans
21285+ .iter()
21286+ .map(|&(r, a, b)| format!("y{r} x{a}..={b}"))
21287+ .collect();
21288+ eprintln!(
21289+ "SENTINEL_CENSUS idx={idx} plane={name} extent={w}x{h} unwritten={total} \
21290+ spans={}: {}",
21291+ spans.len(),
21292+ body.join(" ")
21293+ );
21294+ }
21295+}
21296+
21297+/// Unwritten samples [`census_unwritten`] has counted since the last
21298+/// [`take_unwritten_samples`] -- process-global (not thread-local) because the
21299+/// gate reads it after the decode, from a different thread than the one that
21300+/// scanned the plane.
21301+static UNWRITTEN_SAMPLES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
21302+
21303+/// Turns [`PLANE_SENTINEL`] filling on for the CALLING thread, independently
21304+/// of `EC_AV1_PLANE_SENTINEL`. A gate cannot set a process-constant env var
21305+/// (see [`crate::envflags`]), and the census is the only honest way to ask
21306+/// "did the tile walk write every sample it was handed" -- a wrong-sample diff
21307+/// cannot tell an unwritten sample from a mis-predicted one.
21308+pub fn set_plane_sentinel(on: bool) {
21309+ PLANE_SENTINEL_ON.with(|c| c.set(on));
21310+}
21311+
21312+/// Reads and clears [`UNWRITTEN_SAMPLES`]: the number of samples the decode
21313+/// just handed to the tile walk never wrote, summed over every frame and
21314+/// plane. Zero is the only acceptable answer for a decoder whose output must
21315+/// not depend on its allocator.
21316+pub fn take_unwritten_samples() -> usize {
21317+ UNWRITTEN_SAMPLES.swap(0, std::sync::atomic::Ordering::SeqCst)
21318+}
21319+
21320+/// Samples [`census_unwritten`] has looked at since the last
21321+/// [`take_census_scanned`]. A gate that only asserts
21322+/// [`take_unwritten_samples`] == 0 passes on a decode that never scanned
21323+/// anything, so it reads THIS too: a census that scanned nothing has proved
21324+/// nothing.
21325+static CENSUS_SCANNED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
21326+
21327+/// Reads and clears [`CENSUS_SCANNED`]; see [`take_unwritten_samples`] for the
21328+/// pairing.
21329+pub fn take_census_scanned() -> usize {
21330+ CENSUS_SCANNED.swap(0, std::sync::atomic::Ordering::SeqCst)
21331+}
21332+
21333+thread_local! {
21334+ /// The [`set_plane_sentinel`] override; `EC_AV1_PLANE_SENTINEL` is the
21335+ /// process-wide default.
21336+ static PLANE_SENTINEL_ON: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
21337+}
21338+
21339+/// Whether freshly allocated planes are [`PLANE_SENTINEL`]-filled.
21340+fn plane_sentinel_on() -> bool {
21341+ crate::envflags::env_flag!("EC_AV1_PLANE_SENTINEL")
21342+ || PLANE_SENTINEL_ON.with(std::cell::Cell::get)
21343+}
21344+
2123821345 /// The value [`fresh_plane`] fills with under `EC_AV1_PLANE_SENTINEL`: no
2123921346 /// legal sample can equal it (reconstruction clamps to `(1 << bit_depth) - 1`,
2124021347 /// at most 4095), so a decode that hashes identically under the flag proves
@@ -21252,7 +21359,7 @@ const PLANE_SENTINEL: u16 = 0xDEAD;
2125221359 /// ways (see the lane report) and the two agree, which is the proof that no
2125321360 /// unwritten sample reaches the output.
2125421361 fn fresh_plane(n: usize) -> Vec<u16> {
21255- if crate::envflags::env_flag!("EC_AV1_PLANE_SENTINEL") {
21362+ if plane_sentinel_on() {
2125621363 return vec![PLANE_SENTINEL; n];
2125721364 }
2125821365 let mut v: Vec<u16> = Vec::with_capacity(n);
@@ -21294,6 +21401,8 @@ struct PlaneBuf<'a> {
2129421401 /// old unclipped behaviour exactly.
2129521402 tile_x1: usize,
2129621403 tile_y1: usize,
21404+ /// Which plane this buffer IS (0 luma, 1 U, 2 V) -- TEMPORARY probe.
21405+ plane: u8,
2129721406 }
2129821407
2129921408 /// One entry per `MV_REFERENCE_FRAME` (index 0/1 unused -- `NONE`/
@@ -26322,8 +26431,32 @@ fn sub8_leaf_chroma422(
2632226431 // (`reconintra.c:1804-1806`, `:1815-1817`); the tile end is the frame end
2632326432 // for every leaf this reader owns, and the unit is one TX_4X4 chroma
2632426433 // transform wide and tall (`4 << ss` luma px per axis).
26434+ // lane-av1unwritten: at ss (1,0) a BLOCK_4X8 chroma reference's plane
26435+ // block is 4x8 -- `get_plane_block_size(BLOCK_4X8, 1, 0)` is BLOCK_4X8
26436+ // (`common_data.c`'s `av1_ss_size_lookup[BLOCK_4X8][1][0]` is
26437+ // BLOCK_INVALID, but `blockd.h:1372` takes the per-axis MIN of the two
26438+ // lookups) -- and `av1_get_max_uv_txsize` (blockd.h:1372-1379) resolves
26439+ // it through `max_txsize_rect_lookup[BLOCK_4X8]` == TX_4X8
26440+ // (`common_data.h:126`). So away from lossless the WHOLE 4x8 plane block
26441+ // is ONE rect transform unit, exactly the one [`leaf8_chroma422_unit`]
26442+ // already reads for a BLOCK_8X8 leaf. Emitting the single square TX_4X4
26443+ // this arm used to wrote left the block's bottom four chroma rows
26444+ // UNWRITTEN: on the pinned `440_request_is_422.obu` (a 4:2:2 header over
26445+ // a 4:2:0 tile, whose legal 8x8 `PARTITION_HORZ` groups make this shape
26446+ // legal syntax) the sentinel census found 112 unwritten samples per
26447+ // chroma plane, one 4x4 block per group, and the decode's output hash
26448+ // CHANGED RUN TO RUN because the unwritten samples were uninitialised
26449+ // memory (class `writer-gap-reaches-output`).
26450+ //
26451+ // At LOSSLESS the single-unit reading is wrong the other way:
26452+ // `av1_get_tx_size`'s first line returns TX_4X4 on every plane
26453+ // (blockd.h:1383), so the 4x8 plane block is walked as TWO stacked 4x4
26454+ // units -- which is exactly what the two chroma-reference leaves of the
26455+ // `decode_leaf_split4` route below already emit, so the square arms stay
26456+ // for lossless.
26457+ let rect48 = leaf_shape == (4, 8) && !lossless(fctx);
2632526458 let tx_w = 4 << ss_x(fctx);
26326- let tx_h = 4 << ss_y(fctx);
26459+ let tx_h = (if rect48 { 8 } else { 4 }) << ss_y(fctx);
2632726460 let reach = Reach::of_tu_chroma(
2632826461 mi_bw,
2632926462 mi_bh,
@@ -26339,15 +26472,95 @@ fn sub8_leaf_chroma422(
2633926472 py > 0 && px + (tx_w << ss_x(fctx)) < y.width,
2634026473 px > 0 && py + (tx_h << ss_y(fctx)) < y.height,
2634126474 );
26342- let chroma_around = neighbours.around_mi_422_chroma(ctx_mi, 8, 4);
26475+ let chroma_around = neighbours.around_mi_422_chroma(ctx_mi, 8, if rect48 { 8 } else { 4 });
2634326476 // lane-av1422sub8pix: the CfL AC signal spans the UNIT's luma footprint,
2634426477 // anchored at the same FLOORED luma x the write/prediction anchor below
2634526478 // uses -- `px` (the odd leaf's own luma x) steps half the span into the
2634626479 // RIGHT neighbour group (t422 frame-1 leaf (1,23): the unit reconstructs
2634726480 // chroma px 44..47 from luma 88..95, while a `px`-anchored source
2634826481 // averaged luma 92..99 and every CFL unit of the group mismatched).
26349- let ac = alpha.map(|_| cfl_src_rect((lmi.1 & !1) * MI, py, 8, 4));
26350- let grids: (Grid, Grid) = if let Some(dv) = intrabc_dv.filter(|_| skip) {
26482+ let ac = alpha.map(|_| cfl_src_rect((lmi.1 & !1) * MI, py, 8, if rect48 { 8 } else { 4 }));
26483+ let grids: (Grid, Grid) = if rect48 {
26484+ // The 4:2:2 vert pair's ONE chroma reference codes the PAIR's whole
26485+ // 4x8 chroma plane block. libaom reaches the same unit through
26486+ // `decode_token_recon_block`'s plane/row/col loop with
26487+ // `tx_size = av1_get_tx_size(1, xd)` == TX_4X8 (stepr/stepc = 2/1 over
26488+ // `unit_height = ROUND_POWER_OF_TWO(min(mu_blocks_high + row,
26489+ // max_blocks_high), ss_y)` = 2 and `unit_width` = 1, i.e. one
26490+ // iteration), so [`leaf8_chroma422_unit`] -- the port of that walk for
26491+ // a BLOCK_8X8 leaf -- is the unit, unchanged. The DV arms supply the
26492+ // frame copy through `pred_override`, which is how the BLOCK_8X8 leaf
26493+ // hands the same two intrabc shapes over.
26494+ let (ub, vb) = match intrabc_dv {
26495+ Some(dv) => {
26496+ let mut ub = vec![0u16; 4 * 8];
26497+ mc::predict_with_filter(
26498+ &u.data,
26499+ u.width,
26500+ u.true_width,
26501+ u.true_height,
26502+ mv_to_q4(cpx, dv.1, ss_x(fctx)),
26503+ mv_to_q4(cpy, dv.0, ss_y(fctx)),
26504+ 4,
26505+ 8,
26506+ mc::InterpFilterKind::Bilinear,
26507+ &mut ub,
26508+ fctx,
26509+ );
26510+ let mut vb = vec![0u16; 4 * 8];
26511+ mc::predict_with_filter(
26512+ &v.data,
26513+ v.width,
26514+ v.true_width,
26515+ v.true_height,
26516+ mv_to_q4(cpx, dv.1, ss_x(fctx)),
26517+ mv_to_q4(cpy, dv.0, ss_y(fctx)),
26518+ 4,
26519+ 8,
26520+ mc::InterpFilterKind::Bilinear,
26521+ &mut vb,
26522+ fctx,
26523+ );
26524+ (Some(ub), Some(vb))
26525+ }
26526+ None => (None, None),
26527+ };
26528+ let u_grid = leaf8_chroma422_unit(
26529+ dec,
26530+ cdfs,
26531+ chroma_around[1],
26532+ uv_predict_mode,
26533+ angle_delta_uv,
26534+ reach,
26535+ 1,
26536+ alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
26537+ skip,
26538+ ub,
26539+ u,
26540+ cpx,
26541+ cpy,
26542+ smooth_neighbor_uv,
26543+ fctx,
26544+ )?;
26545+ let v_grid = leaf8_chroma422_unit(
26546+ dec,
26547+ cdfs,
26548+ chroma_around[2],
26549+ uv_predict_mode,
26550+ angle_delta_uv,
26551+ reach,
26552+ 2,
26553+ alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
26554+ skip,
26555+ vb,
26556+ v,
26557+ cpx,
26558+ cpy,
26559+ smooth_neighbor_uv,
26560+ fctx,
26561+ )?;
26562+ (u_grid, v_grid)
26563+ } else if let Some(dv) = intrabc_dv.filter(|_| skip) {
2635126564 // lane-av1422ctattr: a SKIPPED intrabc chroma leaf still predicts the
2635226565 // frame copy at its DV. libaom's `decode_token_recon_block` takes the
2635326566 // INTER arm for an intrabc block (`is_inter_block` counts it,
@@ -35287,6 +35500,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
3528735500 tile_y0: 0,
3528835501 tile_x1: width,
3528935502 tile_y1: height,
35503+ plane: 0,
3529035504 };
3529135505 let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx));
3529235506 let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx));
@@ -35300,6 +35514,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
3530035514 tile_y0: 0,
3530135515 tile_x1: cw,
3530235516 tile_y1: ch,
35517+ plane: 1,
3530335518 };
3530435519 let mut v = PlaneBuf {
3530535520 data: std::borrow::Cow::Owned(fresh_plane(cw * ch)),
@@ -35311,6 +35526,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
3531135526 tile_y0: 0,
3531235527 tile_x1: cw,
3531335528 tile_y1: ch,
35529+ plane: 2,
3531435530 };
3531535531
3531635532 let scan32 = default_scan(TX32);
@@ -37459,6 +37675,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
3745937675 frame_width as usize,
3746037676 frame_height as usize,
3746137677 );
37678+ census_unwritten(&y, &u, &v);
3746237679 if let Ok(path) = crate::envflags::var("EC_AV1_PREFILT_DUMP") {
3746337680 use std::io::Write;
3746437681 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
@@ -37597,6 +37814,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
3759737814 tile_y0: 0,
3759837815 tile_x1: out_w,
3759937816 tile_y1: in_h,
37817+ plane: p.plane,
3760037818 };
3760137819 let (cw, ch, cuw) = (
3760237820 round_ss(fw, ss_x(fctx)),
@@ -38944,6 +39162,7 @@ fn whole_plane(data: &[u16], width: usize, height: usize) -> PlaneBuf<'_> {
3894439162 tile_y0: 0,
3894539163 tile_x1: width,
3894639164 tile_y1: height,
39165+ plane: 0,
3894739166 }
3894839167 }
3894939168
@@ -52902,6 +53121,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
5290253121 tile_y0: 0,
5290353122 tile_x1: width,
5290453123 tile_y1: height,
53124+ plane: 0,
5290553125 };
5290653126 let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx));
5290753127 let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx));
@@ -52915,6 +53135,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
5291553135 tile_y0: 0,
5291653136 tile_x1: cw,
5291753137 tile_y1: ch,
53138+ plane: 1,
5291853139 };
5291953140 let mut v = PlaneBuf {
5292053141 data: std::borrow::Cow::Owned(fresh_plane(cw * ch)),
@@ -52926,6 +53147,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
5292653147 tile_y0: 0,
5292753148 tile_x1: cw,
5292853149 tile_y1: ch,
53150+ plane: 2,
5292953151 };
5293053152
5293153153 let scan32 = default_scan(TX32);
@@ -55743,6 +55965,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
5574355965 frame_width as usize,
5574455966 frame_height as usize,
5574555967 );
55968+ census_unwritten(&y, &u, &v);
5574655969 if let Ok(path) = crate::envflags::var("EC_AV1_PREFILT_DUMP") {
5574755970 use std::io::Write;
5574855971 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
@@ -55903,6 +56126,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
5590356126 tile_y0: 0,
5590456127 tile_x1: out_w,
5590556128 tile_y1: in_h,
56129+ plane: p.plane,
5590656130 };
5590756131 let (cw, ch, cuw) = (
5590856132 round_ss(fw, ss_x(fctx)),
@@ -58297,6 +58521,7 @@ mod tests {
5829758521 tile_y0: 0,
5829858522 tile_x1: width,
5829958523 tile_y1: height,
58524+ plane: 0,
5830058525 };
5830158526 let mut u = PlaneBuf {
5830258527 data: std::borrow::Cow::Owned(vec![0u16; (width / 2) * (height / 2)]),
@@ -58308,6 +58533,7 @@ mod tests {
5830858533 tile_y0: 0,
5830958534 tile_x1: width / 2,
5831058535 tile_y1: height / 2,
58536+ plane: 1,
5831158537 };
5831258538 let mut v = PlaneBuf {
5831358539 data: std::borrow::Cow::Owned(vec![0u16; (width / 2) * (height / 2)]),
@@ -58319,6 +58545,7 @@ mod tests {
5831958545 tile_y0: 0,
5832058546 tile_x1: width / 2,
5832158547 tile_y1: height / 2,
58548+ plane: 2,
5832258549 };
5832358550
5832458551 let mut cdfs = Cdfs::new(q_ctx_of(100));
crates/ec-av1/src/stream.rsmodified+53 -0
@@ -2863,6 +2863,59 @@ pub(crate) mod tests {
28632863 ran on it"
28642864 );
28652865 }
2866+
2867+ /// lane-av1unwritten: the same pin decoded NONDETERMINISTICALLY. Its
2868+ /// 4:2:2 header hands the chroma planes 4x8 blocks (libaom's
2869+ /// `av1_get_max_uv_txsize(BLOCK_4X8, 1, 0)` == TX_4X8), and this
2870+ /// decoder's `sub8_leaf_chroma422` wrote a single square TX_4X4 per
2871+ /// chroma reference -- so the bottom four chroma rows of every 8x8
2872+ /// `PARTITION_HORZ` group were never written at all and the uninitialised
2873+ /// plane memory reached the output: five runs of the one file gave five
2874+ /// different `EC_AV1_FINAL_DUMP` hashes while every census count read
2875+ /// identical.
2876+ ///
2877+ /// The property pinned here is the one that stops that: with freshly
2878+ /// allocated planes filled with `PLANE_SENTINEL` (a value no legal sample
2879+ /// can equal), the decode must leave NO sample still holding it. A
2880+ /// wrong-sample diff cannot assert this -- it cannot tell an unwritten
2881+ /// sample from a mis-predicted one -- and `take_census_scanned` is read
2882+ /// alongside so a census that scanned nothing cannot pass as a census
2883+ /// that found nothing.
2884+ #[test]
2885+ fn a_422_header_over_a_420_tile_leaves_no_sample_unwritten() {
2886+ const NAME: &str = "a_422_header_over_a_420_tile_leaves_no_sample_unwritten";
2887+ struct Sentinel(bool);
2888+ impl Drop for Sentinel {
2889+ fn drop(&mut self) {
2890+ crate::decode::set_plane_sentinel(self.0);
2891+ }
2892+ }
2893+ let _gate_lock = lock_gate_counters();
2894+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
2895+ .join("fixtures")
2896+ .join("440_request_is_422.obu");
2897+ let pinned = read_pin(&path, 2014, 0x98a1378df976253d, NAME);
2898+ crate::decode::set_plane_sentinel(true);
2899+ let _sentinel = Sentinel(false);
2900+ crate::decode::take_unwritten_samples();
2901+ crate::decode::take_census_scanned();
2902+ let frames =
2903+ decode_stream(&pinned).unwrap_or_else(|e| panic!("{NAME}: the pin must decode: {e}"));
2904+ assert_eq!(frames.len(), 1, "{NAME}: the pin decodes exactly one frame");
2905+ let scanned = crate::decode::take_census_scanned();
2906+ let unwritten = crate::decode::take_unwritten_samples();
2907+ assert!(
2908+ scanned >= 64 * 64 + 2 * 32 * 64,
2909+ "{NAME}: the sentinel census scanned only {scanned} samples -- a census that \
2910+ looked at less than the whole frame proves nothing"
2911+ );
2912+ assert_eq!(
2913+ unwritten, 0,
2914+ "{NAME}: {unwritten} samples of the frame were handed to the tile walk and never \
2915+ written, so they reach the output as whatever the allocator left there (the five \
2916+ different dump hashes this pin used to give)"
2917+ );
2918+ }
28662919 /// lane-av1-422bigblock: the two witnessed 4:2:2 big-block defects, pinned
28672920 /// as fixtures (`git add -f`; `fixtures/` is gitignored).
28682921 ///
lanes/av1unwritten.report.mdadded+325 -0
@@ -0,0 +1,325 @@
1+# lane/av1unwritten — a 4:2:2 chroma block the walk never covered
2+
3+**Outcome in one line: `sub8_leaf_chroma422` wrote ONE square `TX_4X4` where libaom
4+writes ONE `TX_4X8` (`av1_get_max_uv_txsize(BLOCK_4X8, 1, 0)`), so every 4:2:2
5+8x8 block coded `PARTITION_HORZ` left the bottom four chroma rows unwritten and
6+the uninitialised plane memory reached the output; the fix routes that shape to
7+the port that already exists for a `BLOCK_8X8` leaf (`leaf8_chroma422_unit`), and
8+a new sentinel-census gate pins "the walk wrote every sample it was handed".**
9+
10+Branch `lane/av1unwritten`, base `main` = `8d6998d7`. No push.
11+
12+---
13+
14+## 1. Reproduction: five runs, five hashes
15+
16+```
17+$ cd /home/user/.cache/wt/av1unwritten
18+$ cargo build -p ec-av1 --example decode_probe --features gate-counters
19+$ mkdir -p /tmp/wt_aw
20+$ for i in 1 2 3 4 5; do EC_AV1_FINAL_DUMP=/tmp/wt_aw/u$i \
21+ ./target/debug/examples/decode_probe crates/ec-av1/fixtures/440_request_is_422.obu \
22+ >/dev/null 2>&1; sha256sum /tmp/wt_aw/u$i.f0; done
23+de5051156b7cfd00dc7ed5f2c6ad404e2a49822a8a728bc027a938aac00219f1 /tmp/wt_aw/u1.f0
24+eb0e513ff07d9780b03e70861d1a7f507f540e4724f62384511a71e06c386aa9 /tmp/wt_aw/u2.f0
25+2985b82874579807efb8bc88136d1b8f5f1f0121410cde26723d5be7e6ba8715 /tmp/wt_aw/u3.f0
26+204645ad20d7e557d7b04f0027d9aafd5790fe685dc39bdf7cfb117d9dd1f0fc /tmp/wt_aw/u4.f0
27+80908a1712c4fe78070a6a3ad57464e8bd6a9d0b8d73e86e8f9cf7ea7cbd3496 /tmp/wt_aw/u5.f0
28+```
29+
30+Five hashes from one file. Under `EC_AV1_PLANE_SENTINEL=1` the same five runs give
31+ONE hash — the output difference is exactly the samples the decode never wrote.
32+
33+### 1.1 The instrument (now committed)
34+
35+`EC_AV1_SENTINEL_CENSUS=1` (new, `decode::census_unwritten`, called at each
36+frame's pre-deblock point beside the existing `EC_AV1_PREFILT_DUMP16` rung, on
37+both the key-frame and inter-frame paths) scans each plane over its own
38+`true_width` x `true_height` extent and prints, per plane, the number of samples
39+still holding `PLANE_SENTINEL` plus every hole as per-row column spans. This is
40+the shipped form of the four-line temporary scan `lanes/av1422seed.report.md` §3
41+used; the report explicitly noted it was *not* committed, which is why this lane
42+had to re-derive it.
43+
44+```
45+$ EC_AV1_PLANE_SENTINEL=1 EC_AV1_SENTINEL_CENSUS=1 \
46+ ./target/debug/examples/decode_probe crates/ec-av1/fixtures/440_request_is_422.obu 2>&1 >/dev/null
47+SENTINEL_CENSUS idx=0 plane=Y extent=64x64 unwritten=0
48+SENTINEL_CENSUS idx=0 plane=U extent=32x64 unwritten=112 spans=24:
49+ y36 x0..=3 y36 x12..=15 y37 x0..=3 y37 x12..=15 y38 x0..=3 y38 x12..=15 y39 x0..=3 y39 x12..=15
50+ y44 x4..=7 y44 x12..=15 y45 x4..=7 y45 x12..=15 y46 x4..=7 y46 x12..=15 y47 x4..=7 y47 x12..=15
51+ y52 x0..=7 y53 x0..=7 y54 x0..=7 y55 x0..=7 y60 x0..=3 y61 x0..=3 y62 x0..=3 y63 x0..=3
52+SENTINEL_CENSUS idx=0 plane=V extent=32x64 unwritten=112 spans=24: (identical)
53+```
54+
55+**112 samples per chroma plane, 0 luma.** Both chroma planes, so the census is
56+not a one-sided artefact.
57+
58+### 1.2 The holes are whole 4x4 chroma blocks, and each is a whole block's bottom half
59+
60+Grouped as blocks, the six spans are six 4x4 chroma blocks (two of the spans are
61+8 wide = two blocks):
62+
63+| chroma block (x, y) | luma extent (2x, y..y+3) | owning luma group |
64+|---|---|---|
65+| (0..3, 36..39) | (0..7, 36..39) | 8x8 group at luma (0, 32), `PARTITION_HORZ` -> two `BLOCK_4X8` |
66+| (12..15, 36..39) | (24..31, 36..39) | 8x8 group at luma (24, 32), `PARTITION_HORZ` |
67+| (4..7, 44..47) | (8..15, 44..47) | 8x8 group at luma (8, 40), `PARTITION_HORZ` |
68+| (12..15, 44..47) | (24..31, 44..47) | 8x8 group at luma (24, 40), `PARTITION_HORZ` |
69+| (0..3, 52..55) + (4..7, 52..55) | (0..15, 52..55) | 8x8 groups at luma (0, 48) and (8, 48), `PARTITION_HORZ` |
70+| (0..3, 60..63) | (0..7, 60..63) | 8x8 group at luma (0, 56), `PARTITION_HORZ` |
71+
72+The blocks that *should* have written each hole did write their luma and their
73+top four chroma rows; the hole is always the **lower half of a 4x8 chroma plane
74+block**, never anything else.
75+
76+### 1.3 Which arm, and what the tile walk does not code
77+
78+Localised with a temporary probe (since removed) on the three
79+`PlaneBuf` sinks — every reconstruction store was recorded with its plane and
80+extent and the census's holes were attributed to owners:
81+
82+```
83+SUB8_C422 lmi=(8,1) leaf=(4, 8) cpx=0 cpy=32
84+SUB8_C422 lmi=(10,3) leaf=(4, 8) cpx=4 cpy=40
85+SUB8_C422 lmi=(8,7) leaf=(4, 8) cpx=12 cpy=32
86+SUB8_C422 lmi=(10,7) leaf=(4, 8) cpx=12 cpy=40
87+SUB8_C422 lmi=(12,1) leaf=(4, 8) cpx=0 cpy=48
88+SUB8_C422 lmi=(12,3) leaf=(4, 8) cpx=4 cpy=48
89+SUB8_C422 lmi=(14,1) leaf=(4, 8) cpx=0 cpy=56
90+SUB8_C422 lmi=(14,3) leaf=(4, 4) cpx=4 cpy=56
91+SUB8_C422 lmi=(15,3) leaf=(4, 4) cpx=4 cpy=60
92+```
93+
94+Every arm is `sub8_leaf_chroma422` (`decode.rs`, reached from
95+`decode_leaf_rect8`'s three chroma-reference call sites and from
96+`decode_leaf_split4`). Each writes one **4x4** chroma unit at `(cpx, cpy)`. The
97+seven `leaf=(4, 8)` entries are the chroma references of the seven
98+`PARTITION_HORZ` groups in §1.2, and `cpy` is the group's TOP row — the bottom
99+row is never visited, by any arm, on any plane.
100+
101+---
102+
103+## 2. Is this reachable on a stream this decoder is supposed to accept?
104+
105+**Yes — the shape is legal AV1 syntax and a 4:2:2 stream can carry it. The
106+committed corpus happens to contain none.**
107+
108+### 2.1 Corpus sentinel sweep (all 127 committed fixtures)
109+
110+```
111+$ for f in crates/ec-av1/fixtures/*.obu; do \
112+ out=$(EC_AV1_PLANE_SENTINEL=1 EC_AV1_SENTINEL_CENSUS=1 timeout 120 \
113+ ./target/debug/examples/decode_probe "$f" 2>&1 >/dev/null | grep SENTINEL_CENSUS); \
114+ if echo "$out" | grep -qv 'unwritten=0'; then echo "=== $(basename $f)"; \
115+ echo "$out" | grep -v 'unwritten=0'; fi; done
116+=== 440_request_is_422.obu
117+SENTINEL_CENSUS idx=0 plane=U ... unwritten=112 ...
118+SENTINEL_CENSUS idx=0 plane=V ... unwritten=112 ...
119+```
120+
121+126 of 127 fixtures — 4:2:0 (lossless, mixed-lossless, tall 8x16/16x8, odd
122+65x65, odd heights, altref, intrabc), 4:4:4 (lossless 128-root mu-chroma,
123+palette, rect4 444 at 8/10-bit), and 4:2:2 at 8/10/12-bit including
124+`Y_intrabc_10b.obu`, `X_intrabc_tiled.obu`, `W_intrabc.obu`, the `s422_*`
125+slices, grain and superres cells — have a **fully written** census on every
126+frame of every plane. Only the hand-built witness does not.
127+
128+### 2.2 Why the corpus cannot see it: the defective shape has zero reach on valid streams
129+
130+Same sweep, counting arm entries and how many carry the `leaf=(4, 8)` shape:
131+
132+```
133+$ for f in crates/ec-av1/fixtures/*.obu; do \
134+ n=$(EC_RECON_TRACE=1 ./target/debug/examples/decode_probe "$f" 2>&1 >/dev/null | grep -c SUB8_C422); \
135+ if [ "$n" != 0 ]; then v=$(... grep -c 'SUB8_C422.*leaf=(4, 8)'); \
136+ echo "$(basename $f) c422=$n vert48=$v"; fi; done
137+422_inter_160x128_3f.obu c422=29 vert48=0
138+422_key_64x64.obu c422=2 vert48=0
139+422_palette_intra_in_inter_384x240_17f c422=158 vert48=0
140+422_residual_compound_warp_16f.obu c422=257 vert48=0
141+422_residual_compound_warp_nolr_16f.obu c422=256 vert48=0
142+440_request_is_422.obu c422=9 vert48=7 <-- the only one
143+W_intrabc.obu c422=391 vert48=0
144+X_intrabc_tiled.obu c422=333 vert48=0
145+Y_intrabc_10b.obu c422=539 vert48=0
146+s422_12bit_160x128.obu c422=185 vert48=0
147+s422_320x246.obu c422=162 vert48=0
148+s422_322x240.obu c422=195 vert48=0
149+s422_322x246.obu c422=155 vert48=0
150+s422_352x242_10b.obu c422=169 vert48=0
151+s422_416x242_10b.obu c422=176 vert48=0
152+s422_416x250_10b.obu c422=198 vert48=0
153+s422_grain_160x128.obu c422=45 vert48=0
154+s422_superres_160x128.obu c422=45 vert48=0
155+```
156+
157+3528 real `sub8_leaf_chroma422` invocations across the 4:2:2 corpus, **zero** of
158+the `leaf=(4, 8)` shape. So:
159+
160+* the DEFECT is not corpus-faked — the arm is heavily used and every other shape
161+ is right;
162+* the defect is latent, not observed, on valid streams, because no committed
163+ 4:2:2 stream codes an 8x8 block with `PARTITION_HORZ`.
164+
165+**Latent does not mean absent.** AV1's 8x8 partition set is the full
166+`PARTITION_TYPES` (`partition_cdf_length`, `av1_common_int.h:1555-1560`: `bsize
167+<= BLOCK_8X8` gets all eight), so `PARTITION_HORZ` at 8x8 is codable, and libaom
168+decodes it through exactly the path below. The witness's tile is a genuine 4:2:0
169+key frame's tile, so the `PARTITION_HORZ` groups it carries are ordinary legal
170+partition symbols — the header/tile chroma mismatch changes only the geometry
171+they are decoded against.
172+
173+### 2.3 What libaom does here (the derivation, from source)
174+
175+1. `av1/decoder/decodeframe.c`, `decode_partition` -> `parse_decode_block`
176+ (`decodeframe.c:1229`) runs once **per leaf**, so a `PARTITION_HORZ` 8x8 group
177+ reaches `decode_token_recon_block(pbi, td, r, BLOCK_4X8)` once per leaf.
178+2. `decode_token_recon_block` (`:993-1041`) skips the chroma planes when
179+ `!xd->is_chroma_ref`. `is_chroma_reference` (`av1_common_int.h:1454-1461`) is
180+ `((mi_row & 1) || !(bh & 1) || !ss_y) && ((mi_col & 1) || !(bw & 1) || !ss_x)`.
181+ For `BLOCK_4X8` at ss (1,0): `bw=1` -> the column clause is `mi_col & 1`, so
182+ **only the odd-column leaf codes chroma**; the row clause is `true` because
183+ `!ss_y`. That is the crate's `!vert || i == 1` in `decode_leaf_rect8`, and it
184+ is right.
185+3. For the chroma reference leaf, `tx_size = av1_get_tx_size(1, xd)`
186+ (`blockd.h:1381-1388`) = `av1_get_max_uv_txsize(BLOCK_4X8, 1, 0)`
187+ (`blockd.h:1372-1379`) = `max_txsize_rect_lookup[get_plane_block_size(
188+ BLOCK_4X8, 1, 0)]`. `get_plane_block_size` takes the **per-axis MIN** of the
189+ two `av1_ss_size_lookup` entries, so `min(BLOCK_INVALID, BLOCK_4X8)` =
190+ `BLOCK_4X8`, and `max_txsize_rect_lookup[BLOCK_4X8]` (`common_data.h:126-127`)
191+ is **`TX_4X8`**. (At lossless, `av1_get_tx_size`'s first line returns `TX_4X4`
192+ on every plane, `blockd.h:1383`.)
193+4. The walk loop at `:1011-1033` with `tx_size = TX_4X8`:
194+ `max_block_wide/high` are in MI units (`blockd.h:1565-1594`) = 1 and 2;
195+ `tx_size_high_unit[TX_4X8] = 2`, `stepc = tx_size_wide_unit[TX_4X8] = 1`
196+ (`common_data.h:246-253`). `blk_row = 0` only, `blk_col = 0` only:
197+ **one `TX_4X8` unit covering the whole 4x8 chroma plane block.**
198+
199+This decoder instead emitted a single `TX_4X4` (`read_plane(..., 4, TX4, ...)`
200+then `push_intra(..., 4, ...)`) — half the plane block. At lossless the existing
201+square model is the *correct* one (`TX_4X4` per unit, two stacked units), which
202+is why the arm's `leaf=(4, 4)` path and the `decode_leaf_split4` route are
203+untouched by this fix.
204+
205+---
206+
207+## 3. The fix
208+
209+`crates/ec-av1/src/decode.rs`, `sub8_leaf_chroma422`, one new branch:
210+
211+```rust
212+let rect48 = leaf_shape == (4, 8) && !lossless(fctx);
213+```
214+
215+When it holds, the arm hands the PAIR's whole 4x8 chroma plane block to
216+`leaf8_chroma422_unit` — the port that already exists for a `BLOCK_8X8` 4:2:2
217+leaf (`TxbSet::ChromaRect8x4` + `SCAN_4X8` + `read_coeffs_rect(4, 8, ..)` +
218+`dequant_and_inverse_typed_wh(4, 8, ..)` + `push_intra_rect(.., 4, 8, ..)`, whose
219+own doc cites the identical `av1_get_max_uv_txsize` derivation). The two intrabc
220+shapes hand their frame copy over through that function's existing
221+`pred_override` parameter, which is how the `BLOCK_8X8` leaf passes the same two
222+cases. Alongside it, three sub-8-specific inputs follow the unit's new height:
223+the reach (`tx_h = 8 << ss_y`), the entropy context
224+(`around_mi_422_chroma(ctx_mi, 8, 8)`) and the CfL AC span
225+(`cfl_src_rect(floored_x, py, 8, 8)`).
226+
227+The three existing square arms stay for `leaf=(4, 4)` (the `decode_leaf_split4`
228+route, one `TX_4X4` per odd-column leaf, which tiles 4x8 correctly) and for
229+lossless (two stacked `TX_4X4`, which is what libaom walks there).
230+
231+**Why a real fix and not a detection/padding.** Refusal is not available: the
232+sequence header is self-consistent AV1 and the partition/mode syntax is
233+subsampling-independent, so the tile parses with no signal that the header and
234+the tile disagree — there is nothing to detect. Pre-filling the plane with 0 or
235+with a mid-grey would hide the class and still emit wrong pixels. Making the
236+walk cover the block it was handed is the only real answer, and the walk's own
237+libaom derivation says what "cover" is: one `TX_4X8`.
238+
239+### 3.1 After the fix
240+
241+```
242+$ EC_AV1_PLANE_SENTINEL=1 EC_AV1_SENTINEL_CENSUS=1 \
243+ ./target/debug/examples/decode_probe crates/ec-av1/fixtures/440_request_is_422.obu 2>&1 >/dev/null
244+SENTINEL_CENSUS idx=0 plane=Y extent=64x64 unwritten=0
245+SENTINEL_CENSUS idx=0 plane=U extent=32x64 unwritten=0
246+SENTINEL_CENSUS idx=0 plane=V extent=32x64 unwritten=0
247+
248+$ for i in 1 2 3 4 5; do EC_AV1_FINAL_DUMP=/tmp/wt_aw/w$i \
249+ ./target/debug/examples/decode_probe crates/ec-av1/fixtures/440_request_is_422.obu >/dev/null 2>&1; \
250+ sha256sum /tmp/wt_aw/w$i.f0; done
251+3823d219ba99c28c904d3a4f6f695fe56fa6cd5b7f26da5919e525b36478d7df ... (x5, identical)
252+```
253+
254+Five runs, one hash.
255+
256+---
257+
258+## 4. The gate
259+
260+`crates/ec-av1/src/stream.rs`, `a_422_header_over_a_420_tile_leaves_no_sample_unwritten`:
261+
262+* pins the same fixture through the existing `read_pin` (2014 bytes,
263+ fnv1a64 `0x98a1378df976253d`);
264+* turns the sentinel on through the new `decode::set_plane_sentinel` (a thread
265+ local, because this crate snapshots the environment once per process and a
266+ gate cannot set a process-constant env var) and turns it off again through a
267+ `Drop` guard;
268+* decodes it and asserts `decode::take_unwritten_samples() == 0`;
269+* asserts `decode::take_census_scanned() >= 64*64 + 2*32*64`, so a census that
270+ scanned nothing cannot pass as a census that found nothing
271+ (`vacuous-test-assertions`).
272+
273+```
274+$ cargo test -p ec-av1 --features gate-counters --lib a_422_header_over_a_420_tile_leaves_no_sample_unwritten
275+test stream::tests::a_422_header_over_a_420_tile_leaves_no_sample_unwritten ... ok
276+test result: ok. 1 passed; 0 failed; ...
277+```
278+
279+### 4.1 Mutation: the gate bites
280+
281+`let rect48 = false && leaf_shape == (4, 8) && !lossless(fctx);` — the fix
282+disabled, nothing else:
283+
284+```
285+thread 'stream::tests::a_422_header_over_a_420_tile_leaves_no_sample_unwritten' panicked at
286+ crates/ec-av1/src/stream.rs:2912:9:
287+assertion `left == right` failed: a_422_header_over_a_420_tile_leaves_no_sample_unwritten:
288+224 samples of the frame were handed to the tile walk and never written, so they reach the output
289+as whatever the allocator left there (the five different dump hashes this pin used to give)
290+test result: FAILED. 0 passed; 1 failed
291+```
292+
293+224 = 112 (U) + 112 (V), the §1.1 census exactly. Reverted, gate green again.
294+
295+---
296+
297+## 5. Regression evidence
298+
299+* **Whole-corpus output equality, pre-fix vs post-fix.** Every fixture decoded
300+ with `EC_AV1_FINAL_DUMP` on both trees, all `.f*` files hashed: the two hash
301+ lists differ **only** for `440_request_is_422.obu`. Every 4:2:0 / 4:4:4 /
302+ 4:2:2 committed stream produces byte-identical output, which is what §2.2's
303+ reach measurement predicts (the `leaf=(4, 8)` shape never fires on them).
304+* **Whole-corpus sentinel sweep re-run after the fix**: `non-clean fixtures:
305+ 0 / 127`. The witness's 112+112 is gone and the other 126 are unchanged,
306+ which is what §2.2's reach measurement predicts.
307+
308+---
309+
310+## 6. What this lane did NOT do, and why
311+
312+* No oracle comparison for the fix's pixels. The `TX_4X8` chroma unit is
313+ derived from libaom's source (§2.3) and reuses the crate's already-pinned
314+ port of the identical unit, but **no committed valid stream reaches the
315+ shape**, so there is no byte-exact claim to make and none is made. A 4:2:2
316+ stream whose 8x8 blocks code `PARTITION_HORZ` is the fixture this fix still
317+ wants; building one needs an encoder that emits that partition, which this
318+ crate's encoder does not.
319+* `film_grain.rs`, the reserved 4:2:0 group-tail chroma SKIP arm and
320+ `decode_rect4_16_intrabc` were not touched. The cause is not in them.
321+* The census instrument (`census_unwritten`, `set_plane_sentinel`,
322+ `take_unwritten_samples`, `take_census_scanned`) and the
323+ `PlaneBuf::plane` field are additions; the field is what lets the census and
324+ any future write-trace name the plane from the buffer instead of from the
325+ call site.
\ No newline at end of file