From 689d3e06c0c3de84f2b61bf36812d9dc64f039ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 28 Sep 2026 22:18:01 +0300 Subject: [PATCH] ec-av1: lane-av1444edge -- the inter-frame pre-filter rung did not exist, and its counter restarted Two defects in the same measurement rung, both of which report success while measuring nothing useful. `EC_AV1_PREFILT_WIDE_DUMP` is the only rung that writes the pre-loop-filter reconstruction cropped to each plane's mi-ALIGNED extent, i.e. the shape aomdec's own rung 7 writes (`y_width`/`y_height`), so it is the rung that separates "already wrong in reconstruction" from "introduced by a loop filter". 1. It was inlined in the KEY-frame tile path only (`decode_key_frame_tile_with_cdfs`). `decode_inter_frame_tile_with_cdfs` has no wide prefilter dump at all, so every divergence localized to an inter frame had no pre-filter rung on our side to compare against the oracle's, and the nearest usable rung (the padded `EC_AV1_PREFILT_DUMP`) has a different byte shape from every oracle rung. Measured cost on the 4:4:4 lossy cells this lane was handed: `EC_AV1_POSTDEBLOCK_DUMP` / `EC_AV1_POSTCDEF_DUMP` came out 61440 B against the oracle's 52224 B (our 160x128 padded surface vs its 136x128 aligned buffer) and the oracle's POSTDEBLOCK wrote 3 files for a 4-frame stream, so the stage ladder could not be read at all. With the rung on the inter path the same two cells' pre-filter dumps are 52224 B and 4 files, and the verdict is immediate: reconstruction already diverges at the same first sample as the final output, so no filter is involved. 2. Its counter was a FUNCTION-LOCAL `static IDX2`. Rust gives each monomorphisation of the enclosing function its own copy, so the index restarted and every frame after the first overwrote `.f0` -- a per-frame bisection silently compared the last decoded frame and reported it as frame 0. Measured directly: before this change the rung wrote exactly one file for a four-frame stream. The counter is now the shared per-var `dump_stage_idx` map the other stage dumps already use, and the block is a shared `dump_prefilter_wide` called from both tile paths, beside each path's existing padded `EC_AV1_PREFILT_DUMP` so the two agree on the point they sample. No decode-path behaviour changes: the only writes are inside the existing `if let Ok(path) = crate::envflags::var(...)` arms, which are off unless the env var is set. (cherry picked from commit b2148831481f741a05830ac7014876dfc67f8d07) --- crates/ec-av1/src/decode.rs | 70 ++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 38015002..abafb00f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -19155,6 +19155,48 @@ fn dump_stage(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) { } } +/// `EC_AV1_PREFILT_WIDE_DUMP=` -> `.f`, the pre-loop-filter +/// reconstruction cropped to each plane's mi-ALIGNED true extent, byte for +/// byte the shape aomdec's own rung 7 writes (`y_width`/`y_height`, +/// `uv_width`/`uv_height`), so the two can be diffed directly. +/// +/// lane-av1444edge: this used to be an inline block in the KEY-frame tile path +/// only, with a FUNCTION-LOCAL `static IDX2` as its counter. Both facts were +/// measurement traps, and the second one is why the first went unnoticed: +/// the inter-frame path (`decode_inter_frame_tile_with_cdfs`) had no wide +/// prefilter dump at all, so every 4:4:4 lossy divergence localized to an +/// inter frame had no pre-filter rung to compare against and got attributed to +/// reconstruction when the oracle could not see it. The `static` also restarted +/// per monomorphisation, so every frame after the first overwrote `.f0` -- a +/// per-frame bisection silently compared the LAST decoded frame. The counter +/// is now the shared per-var [`dump_stage_idx`] map, and the call also sits on +/// the inter path (skipped when the filter pipeline is active, which has +/// already deblocked by the time the tile tail runs -- same condition +/// `capture_filter_replay` guards itself with). +fn dump_prefilter_wide(y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) { + let Ok(path) = crate::envflags::var("EC_AV1_PREFILT_WIDE_DUMP") else { + return; + }; + use std::io::Write; + let idx = dump_stage_idx("EC_AV1_PREFILT_WIDE_DUMP"); + let crop = |plane: &PlaneBuf<'_>| -> Vec { + let mut out = Vec::with_capacity(plane.true_width * plane.true_height); + for row in 0..plane.true_height { + out.extend( + plane.data[row * plane.width..][..plane.true_width] + .iter() + .map(|&s| s as u8), + ); + } + out + }; + if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { + let _ = f.write_all(&crop(y)); + let _ = f.write_all(&crop(u)); + let _ = f.write_all(&crop(v)); + } +} + /// The value [`fresh_plane`] fills with under `EC_AV1_PLANE_SENTINEL`: no /// legal sample can equal it (reconstruction clamps to `(1 << bit_depth) - 1`, /// at most 4095), so a decode that hashes identically under the flag proves @@ -34513,27 +34555,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( } else { PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); } - if let Ok(path) = crate::envflags::var("EC_AV1_PREFILT_WIDE_DUMP") { - use std::io::Write; - static IDX2: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); - let idx = IDX2.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - let crop_wide = |plane: &PlaneBuf<'_>| -> Vec { - let mut out = Vec::with_capacity(plane.true_width * plane.true_height); - for row in 0..plane.true_height { - out.extend( - plane.data[row * plane.width..][..plane.true_width] - .iter() - .map(|&s| s as u8), - ); - } - out - }; - if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { - let _ = f.write_all(&crop_wide(&y)); - let _ = f.write_all(&crop_wide(&u)); - let _ = f.write_all(&crop_wide(&v)); - } - } + dump_prefilter_wide(&y, &u, &v); // lane-av1fwd: the encoder's filter search re-decodes this same key // frame tile per candidate -- capture it once (see [`FilterReplay`]), // on the shape whose tail is the two filter calls and nothing else. @@ -52079,6 +52101,12 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( } else { PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); } + // lane-av1444edge: the inter-frame path's mi-aligned pre-loop-filter + // dump, the rung the key-frame path has always had (see + // `dump_prefilter_wide`). Placed beside the padded `EC_AV1_PREFILT_DUMP` + // above, i.e. before the filter pipeline is released, so the two agree on + // the point in the sequence they sample. + dump_prefilter_wide(&y, &u, &v); // lane-pipefilt: release the rows still pending and wait the pipeline // out; from here the picture is deblocked and CDEF-filtered exactly as // the whole-frame path leaves it.