forge
Explore
Log in

Don't you dare go Hollow

Lane-lr charter -- loop restoration decode

Browse files at this commit

d3d4001b42cb81a4caf129a3db48893d3520858cAhmet Kaan GümüşParent: 94033af
  • docs
    • lane-lr charter -- loop restoration decode
Show full message
docs(av1): lane-lr charter -- loop restoration decode
lanes/lr.charter.mdadded+100 -0
@@ -0,0 +1,100 @@
1+# lane-lr — loop restoration decode (read_lr + Wiener + self-guided)
2+
3+Worktree `/home/user/Documents/Code/Rust/edith_codecs-lr`, branch `lane-lr`, off main 87a1e34.
4+
5+## Goal
6+Remove the whole-frame refusal at `crates/ec-av1/src/stream.rs:195`
7+("a frame with loop restoration enabled") by actually decoding it:
8+1. the per-restoration-unit symbols read from the tile data during the
9+ superblock walk (spec 5.11.57 `read_lr` / `read_lr_unit`), and
10+2. the two restoration filters themselves (spec 7.17: Wiener 7.17.4,
11+ self-guided 7.17.2/7.17.3), applied to the CDEF-filtered frame.
12+
13+## Anchors
14+- Spec: 5.11.57 `read_lr`, 5.11.58 `read_lr_unit`, 6.10.15, 7.17 `loop restoration process`.
15+- libaom (oracle source, `~/.cache/aom-oracle` build tree — find the src dir under it):
16+ `av1/decoder/decodeframe.c` `read_lr()` / `read_lr_unit()`;
17+ `av1/common/restoration.c` `av1_loop_restoration_filter_frame`,
18+ `wiener_filter_stripe`, `selfguided_restoration`, `av1_selfguided_restoration_c`,
19+ and the `sgr_params` table.
20+- Existing syntax: `crates/ec-av1-syntax/src/frame.rs:341` `LoopRestorationParams`
21+ (`frame_restoration_type`, `loop_restoration_size`, `uses_lr`).
22+- The superblock walk in the decoder is `crates/ec-av1/src/decode.rs` around
23+ lines 4547 and 9275 (`for sb_r in 0..sb_rows { for sb_c ... }`). `read_lr` is
24+ called once per superblock, BEFORE `decode_partition`, and loops over the LR
25+ units whose top-left falls in this superblock, per plane.
26+- CDF tables live in `crates/ec-av1/src/cdf.rs`. There is currently NO
27+ restoration CDF (`grep -i restoration crates/ec-av1/src/cdf.rs` is empty) —
28+ you must add `restore_wiener`, `restore_sgrproj`, `restore_switchable`
29+ (the switchable one is a 3-symbol CDF) with libaom's `default_*_cdf` values
30+ copied EXACTLY from `av1/common/token_cdfs.h` / `entropymode.c`. Note that
31+ the Wiener/SGR *coefficients* are NOT CDF-coded: they use
32+ `decode_signed_subexp_with_ref` (literal + subexp bits), spec 5.9.27-ish.
33+- CDF WIRING IS FOUR SITES in this codebase but in practice only the defaults
34+ array needs hand-checking (reset2/reset3 are length-generic, save/restore is
35+ a whole-struct Clone). Still verify the counter reset covers new tables.
36+
37+## Method (this is a range-ladder lane)
38+CLASS `compare-range-not-tell`: never compare `tell()` across decoders —
39+compare the msac RANGE after each element. The instrumented oracle at
40+`~/.cache/aom-oracle` has env-gated rungs: `EC_TRACE=1` (EC_PART),
41+`EC_TRACE_COEFF=1` (EC_COEFF), `EC_TRACE_MODE=1` (EC_MODE/EC_IMODE).
42+If you need an LR rung, ADD one to `scripts/instrument-aom-oracle.sh`
43+following the exact shape of the existing rungs (env-gated, silent when unset,
44+idempotent, wrapper-around-impl) and rebuild the oracle with
45+`scripts/build-aom-oracle.sh`. That is an expected part of this lane.
46+CLASS `equal-range-means-unread`: if the reference's range is UNCHANGED where
47+ours moves, we read a symbol it never wrote; if theirs moves and ours doesn't,
48+we skipped one. Only when both move by different amounts is a table implicated.
49+
50+## Staging — commit after EVERY green milestone
51+1. **Syntax only.** Read the lr symbols correctly and then still refuse the
52+ frame by name ("loop restoration symbols are read but the filters are not
53+ applied"). Prove correctness by the partition walk surviving: a stream that
54+ previously desynced into out-of-alphabet garbage now decodes its partitions
55+ and only fails the pixel compare. COMMIT.
56+2. **Wiener.** Implement `wiener_filter_stripe` (64-row stripes with the
57+ 3-pixel stripe boundary handling — that boundary handling is the classic
58+ trap; libaom saves/restores `rlbs` boundary lines). Gate a stream forced to
59+ Wiener only. COMMIT.
60+3. **Self-guided.** `selfguided_restoration` incl. the box-sum radii and the
61+ `sgr_params` table. COMMIT.
62+4. **Switchable.** Remove the last refusal. COMMIT.
63+
64+## Gate (mandatory, in `crates/ec-av1/src/stream.rs` next to the existing gates)
65+- Follow the existing gate shape exactly. `EC_AV1_REQUIRE_AOMENC=1` must be set
66+ when you run tests, so a missing oracle FAILS rather than SKIPs.
67+- Bound every ffmpeg `generate` with `-t <seconds>` (an unbounded source
68+ deadlocked a gate for an hour; class `gate-loader-slurps-whole-file`).
69+- Build the fixture through the existing `gradients_source(seed, w, h, tail)`
70+ helper — do NOT hand-write a `gradients=size=` string; ffmpeg's gradients
71+ source ignores its seed and the helper derives colours from the seed instead.
72+- aomenc flags: `--threads=1 --row-mt=0`, and whatever turns LR on
73+ (it is on by default; `--enable-restoration=1`). To force a single filter
74+ type, encode content that provokes it, or use the aomenc knob if one exists —
75+ if not, assert per-type firing counts instead.
76+- The gate MUST hard-assert a firing count (e.g. `wiener_hits > 0`), a
77+ thread-local `Cell<usize>` counter in decode.rs like the existing `*_HITS`
78+ (they are thread-local now, NOT atomics — process-global atomics made one
79+ gate count another test's work). CLASS `gate-blind-to-feature`: a gate that
80+ cannot prove its feature fired is vacuous.
81+- Refusals inside the gate are FORBIDDEN once the stage that removes them
82+ lands: assert the decode returns Ok, do not tolerate an Err.
83+
84+## Hard rules
85+- Own build dir: `export CARGO_TARGET_DIR=$HOME/.cache/cargo-target-lr`.
86+ Foreground builds only, `nice -n 19 cargo ... -j4`. Never `cargo` in another
87+ worktree, never touch a sibling worktree's files.
88+- Suite scope: `nice -n 19 cargo test -p ec-av1 --lib` (67 s baseline on main,
89+ 232 passed / 0 failed). It must stay green.
90+- NEVER push, never merge, never touch `main`. Commit on `lane-lr` only.
91+- You have a 75-turn cap. **Commit work-in-progress after every green
92+ milestone** — five builders have lost complete implementations at the cap.
93+ If you are near the cap, commit whatever compiles as `wip(av1): ...` and
94+ write `lanes/lr.report.md` before you stop.
95+- Refuse-by-name rather than desync: any case you do not implement must return
96+ `Error::unsupported` with an accurate string. CLASS
97+ `refusal-claim-disproved-by-its-own-gate`: never write a refusal string that
98+ claims the encoder never emits the case unless you have proved it.
99+- Write `lanes/lr.report.md` at the end: what landed, gate name + firing count,
100+ what still refuses and by what exact string, and the next lever.