diff --git a/crates/ec-av1/src/decode/block.rs b/crates/ec-av1/src/decode/block.rs index dc728614..f3bc81b2 100644 --- a/crates/ec-av1/src/decode/block.rs +++ b/crates/ec-av1/src/decode/block.rs @@ -418,111 +418,6 @@ pub(crate) fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { } } -/// One plane's reconstruction buffer, `width * height` samples, and the -/// prediction/residual reads and writes into it — the decode-side twin of -/// [`crate::encode`]'s private `Plane`, whose `edges` this mirrors exactly -/// (own-edge and reach clamps both), passing what it gathers straight to -/// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. -/// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written -/// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. -/// lane-hgkf r2: 16-bit cropped stage dump, the high-bitdepth twin of -/// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects). -/// lane-unwritten-dep: this doc previously said the dump "matches aomdec's own -/// `ec_dump16` rung byte for byte (crop dims, u16 LE)", which was true only of -/// the 4:2:0 crop it then used. The crop is now PER-AXIS, which is what the -/// oracle's rung 1 actually does -- it writes `y_crop_width` x `y_crop_height`, -/// `uv_crop_width` x `uv_crop_height`, `uv_crop_width` x `uv_crop_height` (the -/// libaom `YV12_BUFFER_CONFIG` fields, each already per-axis). Byte shape per -/// subsampling, `fw x fh` luma then both chroma planes, u16 LE, so: -/// -/// ```text -/// ss_x ss_y format luma chroma (each) -/// 0 0 4:4:4 fw x fh fw x fh fw*fh*3 samples -/// 1 1 4:2:0 fw x fh fw/2 x fh/2 fw*fh*3/2 samples -/// 1 0 4:2:2 fw x fh fw/2 x fh fw*fh*2 samples -/// 0 1 4:4:0 fw x fh fw x fh/2 fw*fh*3/2 samples -/// ``` -/// -/// (`round_ss` is the rounding the decoder's own plane extents use; at ss = 1 it -/// is `div_ceil(2)`, so every 4:2:0 dump is byte-for-byte unchanged by this.) -/// lane-mc64 r1: the file name is keyed on [`dump_stage_idx`], a per-var -/// decode-order counter -- both dumps used to write `.f0` unconditionally, so -/// every frame overwrote the previous one and only the LAST frame survived, -/// which silently defeats any per-frame filter-stage bisection (ledger, class -/// stale-output-faked-measurement). A sibling accessor `stage_dump_idx()`, which -/// derived the same number from `PREFILT_PICTURE_IDX`, had no caller and was -/// deleted: `PREFILT_PICTURE_IDX` is bumped after this point, not before it, so -/// the counter that names these files is the per-var map. -// Signature mirrors the spec's per-call inputs; a parameter struct would only move them. -/// Unwritten samples [`census_unwritten`] has counted since the last -/// [`take_unwritten_samples`] -- process-global (not thread-local) because the -/// gate reads it after the decode, from a different thread than the one that -/// scanned the plane. -pub(super) static UNWRITTEN_SAMPLES: std::sync::atomic::AtomicUsize = - std::sync::atomic::AtomicUsize::new(0); - -/// Turns [`PLANE_SENTINEL`] filling on for the CALLING thread, independently -/// of `EC_AV1_PLANE_SENTINEL`. A gate cannot set a process-constant env var -/// (see [`crate::envflags`]), and the census is the only honest way to ask -/// "did the tile walk write every sample it was handed" -- a wrong-sample diff -/// cannot tell an unwritten sample from a mis-predicted one. -pub fn set_plane_sentinel(on: bool) { - PLANE_SENTINEL_ON.with(|c| c.set(on)); -} - -/// Reads and clears [`UNWRITTEN_SAMPLES`]: the number of samples the decode -/// just handed to the tile walk never wrote, summed over every frame and -/// plane. Zero is the only acceptable answer for a decoder whose output must -/// not depend on its allocator. -pub fn take_unwritten_samples() -> usize { - UNWRITTEN_SAMPLES.swap(0, std::sync::atomic::Ordering::SeqCst) -} - -/// Samples in the PADDED TAIL -- every sample of each plane outside its own -/// `true_width` x `true_height` extent -- that still holds [`plane_poison`] at -/// the pre-deblock point. [`census_unwritten`] scans that region too, because -/// "past the true extent nothing is ever read" was an ASSUMPTION the tail's -/// census was built to avoid checking; this counter is the measurement. -pub(super) static PAD_UNWRITTEN_SAMPLES: std::sync::atomic::AtomicUsize = - std::sync::atomic::AtomicUsize::new(0); - -/// Reads and clears [`PAD_UNWRITTEN_SAMPLES`]. -pub fn take_pad_unwritten_samples() -> usize { - PAD_UNWRITTEN_SAMPLES.swap(0, std::sync::atomic::Ordering::SeqCst) -} - -/// The value [`fresh_plane`] fills with when the sentinel is on. -/// -/// [`PLANE_SENTINEL`] unless `EC_AV1_PLANE_SENTINEL` names a different one in -/// hex. Two different poisons are what turns the sentinel from "did a hole -/// survive into the output" into "did the output depend on the buffer's -/// initial content": a hole whose value reaches the picture changes the hash -/// when the poison changes, and one that does not cannot have influenced -/// anything. A value at or below [`MAX_LEGAL_SAMPLE`] is REFUSED (falls back -/// to [`PLANE_SENTINEL`]) because the census's soundness rests on the sentinel -/// being impossible. `EC_AV1_PLANE_SENTINEL=1` -- the spelling every existing -/// caller and gate uses, and `env_flag!`'s mere presence test -- keeps -/// [`PLANE_SENTINEL`]. -/// `pub(crate)` for lane-av1graincensus: `film_grain::uninit_plane` fills its -/// destination with the SAME poison `fresh_plane` does, and -/// [`census_unwritten_grained`] counts the same value. The fill and the census -/// must agree on both the value and the predicate, or a "0" means nothing. -pub(crate) fn plane_poison() -> u16 { - PLANE_SENTINEL -} - -thread_local! { - /// The [`set_plane_sentinel`] override; `EC_AV1_PLANE_SENTINEL` is the - /// process-wide default. - static PLANE_SENTINEL_ON: std::cell::Cell = const { std::cell::Cell::new(false) }; -} - -/// Whether freshly allocated planes are [`PLANE_SENTINEL`]-filled. -/// `pub(crate)` for the same reason as [`plane_poison`]. -pub(crate) fn plane_sentinel_on() -> bool { - PLANE_SENTINEL_ON.with(std::cell::Cell::get) -} - /// lane-av1unwritten: whether a subsize has a chroma PLANE BLOCK at this /// frame's subsampling -- libaom `av1_ss_size_lookup[bsize][ss_x][ss_y] != /// BLOCK_INVALID` (`common_data.c`), which `decode_partition` checks on every @@ -717,84 +612,8 @@ pub(super) fn refuse_invalid_plane_block( ) } -/// 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 -/// the output does not depend on the plane buffer's initial content. -pub(super) const PLANE_SENTINEL: u16 = 0xDEAD; - -/// lane-unwritten-dep: the census count at the LAST point of the pipeline, on the -/// exact bytes `EC_AV1_FINAL_DUMP` writes. -/// -/// WHY A SECOND COUNT, and what it is actually for -- the justification this -/// comment originally carried was WRONG and is corrected here, because a reader -/// would otherwise re-derive the wrong conclusion. An earlier revision of this -/// lane claimed the pre-deblock plane was byte-identical plain-vs-sentinel and -/// that "deblock creates the dependence". That reading came from -/// [`dump_stage16`], which cropped chroma as `(fw/2, fh/2)` -- a 4:2:0 -/// assumption. On a 64x64 4:2:2 frame (ss_x=1, ss_y=0) that wrote 32x32 chroma -/// where the frame carries 32x64, so 2048 chroma samples appeared in NO stage -/// dump, and they are exactly the region where the difference lives. -/// -/// With per-axis extents the corrected measurement on `440_request_is_422` is: -/// the PRE-DEBLOCK dump already DIFFERS by 2158 bytes plain-vs-sentinel, and an -/// exact u16 whole-plane census finds 224 unwritten samples (U 112, V 112, -/// rows 36..63, whole 4x4 groups) -- the same number -/// `lanes/av1unwritten.report.md` reports. **The hole is present before any -/// filter runs, and the pre-deblock census sees it.** This comment previously -/// said the opposite; that was the instrument's blind spot read as a property of -/// the pipeline. -/// -/// So what this second count is FOR, stated honestly: it is not there because the -/// pre-deblock census misses holes. It is there because it is the measurement -/// taken where the OUTPUT is taken -- the bytes stored into the reference slots -/// and handed to the caller. A hole can be created after reconstruction (a -/// filter stage, a superres step, a second pass) and be invisible to a scan taken -/// before it; this one cannot. It is a check on the whole span from the tile walk -/// to the caller's frame, not a claim that the pre-deblock scan is insufficient. -pub(super) static FINAL_UNWRITTEN_SAMPLES: std::sync::atomic::AtomicUsize = - std::sync::atomic::AtomicUsize::new(0); - -/// lane-unwritten-dep: reads and clears [`FINAL_UNWRITTEN_SAMPLES`] -- unwritten -/// samples still present in the frame as handed to the caller, summed over every -/// frame and plane. Process-global, like the pre-deblock census's counter, so a -/// gate can read it after the decode from a different thread than the one that -/// scanned. -pub fn take_final_unwritten_samples() -> usize { - FINAL_UNWRITTEN_SAMPLES.swap(0, std::sync::atomic::Ordering::SeqCst) -} - -/// lane-av1graincensus: samples still holding the poison after film grain, over -/// the picture [`crate::film_grain::apply_grain`] RETURNS -- i.e. the picture the -/// caller receives on a grained frame. -pub(super) static GRAINED_UNWRITTEN_SAMPLES: std::sync::atomic::AtomicUsize = - std::sync::atomic::AtomicUsize::new(0); - -/// Reads and clears [`GRAINED_UNWRITTEN_SAMPLES`]. -pub fn take_grained_unwritten_samples() -> usize { - GRAINED_UNWRITTEN_SAMPLES.swap(0, std::sync::atomic::Ordering::SeqCst) -} - /// A frame plane's own backing store, `n` samples, zero-filled. -/// -/// The `true_width`/`true_height`/`width`/`height` arguments are kept for the -/// call sites' shape and the sentinel's poison fill; the zero fill itself reads -/// none of them. The former `EC_AV1_PLANE_ALLOC=uninit|padzero|extzero` arms -/// (uninitialised allocation for memcheck census) are gone: they needed -/// `set_len` over uninitialised memory, which the crate no longer contains. The -/// shipped default was already the zeroed arm (`lanes/av1uballoc.report.md` §1); -/// the poison sentinel (`EC_AV1_PLANE_SENTINEL`) still catches any read of the -/// initial content. -pub(super) fn fresh_plane( - n: usize, - _true_width: usize, - _true_height: usize, - _width: usize, - _height: usize, -) -> Vec { - if plane_sentinel_on() { - return vec![plane_poison(); n]; - } +pub(super) fn fresh_plane(n: usize) -> Vec { vec![0u16; n] } diff --git a/crates/ec-av1/src/decode/frame_tile.rs b/crates/ec-av1/src/decode/frame_tile.rs index 3a7920f2..5113eced 100644 --- a/crates/ec-av1/src/decode/frame_tile.rs +++ b/crates/ec-av1/src/decode/frame_tile.rs @@ -417,13 +417,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK); let mut y = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane( - width * height, - true_width, - true_height, - width, - height, - )), + data: std::borrow::Cow::Owned(fresh_plane(width * height)), width, height, true_width, @@ -437,7 +431,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx)); let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx)); let mut u = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(cw * ch, ctw, cth, cw, ch)), + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), width: cw, height: ch, true_width: ctw, @@ -449,7 +443,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( plane: 1, }; let mut v = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(cw * ch, ctw, cth, cw, ch)), + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), width: cw, height: ch, true_width: ctw, diff --git a/crates/ec-av1/src/decode/key_tile.rs b/crates/ec-av1/src/decode/key_tile.rs index 660e93af..f86f4a5b 100644 --- a/crates/ec-av1/src/decode/key_tile.rs +++ b/crates/ec-av1/src/decode/key_tile.rs @@ -410,13 +410,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK); let mut y = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane( - width * height, - true_width, - true_height, - width, - height, - )), + data: std::borrow::Cow::Owned(fresh_plane(width * height)), width, height, true_width, @@ -430,7 +424,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx)); let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx)); let mut u = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(cw * ch, ctw, cth, cw, ch)), + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), width: cw, height: ch, true_width: ctw, @@ -442,7 +436,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( plane: 1, }; let mut v = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(cw * ch, ctw, cth, cw, ch)), + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), width: cw, height: ch, true_width: ctw, diff --git a/crates/ec-av1/src/decode/mod.rs b/crates/ec-av1/src/decode/mod.rs index 7054b6ab..80a8c7fe 100644 --- a/crates/ec-av1/src/decode/mod.rs +++ b/crates/ec-av1/src/decode/mod.rs @@ -67,7 +67,7 @@ use rect4::*; mod block; mod wave; -pub use block::*; +pub(crate) use block::*; mod leaf8; use leaf8::*; diff --git a/crates/ec-av1/src/film_grain.rs b/crates/ec-av1/src/film_grain.rs index 1f091c23..1718a1b7 100644 --- a/crates/ec-av1/src/film_grain.rs +++ b/crates/ec-av1/src/film_grain.rs @@ -1348,28 +1348,8 @@ struct GrainBand<'a> { /// the disjoint band slices' split does; the byte-exact 1-vs-4-thread /// gate is what checks it. Upgrade path: `Box<[MaybeUninit]>` plumbed /// through the kernels. -/// -/// # lane-av1graincensus: the fill under the census arm -/// -/// That corner-cut is also why the buffer must be POISONED, not merely -/// uninitialised, whenever the sentinel is live. Counting `== plane_poison()` -/// over ambient garbage is a census that can only ever report ~0: a hole -/// survives with probability ~1/65536 rather than always, so -/// [`crate::decode::census_unwritten_grained`] would report 0 whether or not -/// the band copy covered every row. Filling here makes "the band copy wrote -/// every sample" a measurement. It is the same predicate -/// [`crate::decode::fresh_plane`] honours, and with the sentinel off -- every -/// shipped build -- this is the same zeroed buffer, so nothing changes where it -/// is paid for. fn uninit_plane(n: usize) -> Vec { - // `vec![0; n]` is `alloc_zeroed`, which for a plane-sized buffer is a - // fresh `calloc`/mmap: the zero pages are not touched until the band copy - // writes them, so no extra full-buffer pass is paid. Safe, and no `set_len`. - let mut v: Vec = vec![0; n]; - if crate::decode::plane_sentinel_on() { - v.fill(crate::decode::plane_poison()); - } - v + vec![0; n] } /// `av1_add_film_grain`/`add_film_grain_run` (spec 7.18.3.5): synthesize and