Lane-lr report -- CDF tables landed, read_lr wiring design worked out, not yet coded
Browse files at this commited0393739a4cc20de85a60cc97bfbb22e3f7f8bcParent: ed590d6- docs
- lane-lr report -- CDF tables landed, read_lr wiring design worked out, not yet coded
1 changed file +120 -0
- lanes/lr.report.md +120 -0
lanes/lr.report.mdadded+120 -0
| @@ -0,0 +1,120 @@ | ||
| 1 | +# lane-lr report | |
| 2 | + | |
| 3 | +## What landed (committed on `lane-lr`, 14a0273) | |
| 4 | +- Added the three loop-restoration CDF tables this decoder was missing | |
| 5 | + (`crates/ec-av1/src/cdf.rs`): `RESTORE_WIENER`, `RESTORE_SGRPROJ`, | |
| 6 | + `RESTORE_SWITCHABLE`, copied exactly from libaom | |
| 7 | + `av1/common/entropymode.c`'s `default_{wiener,sgrproj,switchable}_restore_cdf` | |
| 8 | + (`AOM_CDF2(11570)`, `AOM_CDF2(16855)`, `AOM_CDF3(9413, 22581)` — converted | |
| 9 | + to this codebase's `[a0, (a1,) 32768, 0]` convention, matching the existing | |
| 10 | + `INTRABC` table's own conversion of `AOM_CDF2(30531)`). | |
| 11 | +- Wired all three into `Cdfs` (`crates/ec-av1/src/cdf_state.rs`): struct | |
| 12 | + fields, `Cdfs::new`'s defaults, and `reset_counts` (all three are | |
| 13 | + unindexed `[u16; N]`, same shape as `intrabc`, so `reset1` — the | |
| 14 | + 4-site wiring checklist's counter-reset site is the only hand-checked | |
| 15 | + one; save/restore and the other two sites are generic over any shape). | |
| 16 | +- `cargo check -p ec-av1` clean. | |
| 17 | + | |
| 18 | +## What still refuses, and by what string | |
| 19 | +The whole-frame refusal is UNCHANGED: `crates/ec-av1/src/stream.rs:195-198` | |
| 20 | +still returns `Error::unsupported("AV1 decode_stream", "a frame with loop | |
| 21 | +restoration enabled (this decoder never reads the per-unit lr symbols)")` | |
| 22 | +for any `header.loop_restoration.uses_lr` frame. The CDF tables added this | |
| 23 | +round are not read from the bitstream anywhere yet — this is groundwork | |
| 24 | +for stage 1 (charter's "syntax only" milestone), not stage 1 itself. | |
| 25 | + | |
| 26 | +## Design worked out but not yet coded (for the next round) | |
| 27 | +Traced the full spec/libaom shape so the next round can go straight to | |
| 28 | +code instead of re-reading source: | |
| 29 | + | |
| 30 | +- `av1_loop_restoration_corners_in_sb` (`restoration.c:1277`) only returns | |
| 31 | + non-zero corners when `bsize == sb_size` — i.e. despite `decode_partition` | |
| 32 | + calling it recursively at every partition level in libaom, only the | |
| 33 | + top-of-superblock call ever does anything, because every restoration | |
| 34 | + unit is >= 64px (SB size). The charter's "once per superblock" plan is | |
| 35 | + therefore exactly spec-equivalent, not a simplification with a gap. | |
| 36 | +- Per-plane unit grid: `horz_units = max(1, round(plane_w / unit_size))`, | |
| 37 | + `vert_units` likewise (`av1_lr_count_units`, `restoration.c:63`, note | |
| 38 | + ROUND not ceil). `plane_w`/`plane_h` are `frame_width`/`frame_height` for | |
| 39 | + luma; this decoder is 4:2:0-only in the tile-decode functions already | |
| 40 | + (`u`/`v` are hardcoded half-size), so chroma is | |
| 41 | + `(frame_width+1)/2, (frame_height+1)/2` (`ROUND_POWER_OF_TWO`). | |
| 42 | + `unit_size` = `header.loop_restoration.loop_restoration_size[plane]` | |
| 43 | + directly (already computed per-plane in | |
| 44 | + `ec-av1-syntax/src/frame.rs:1626-1637`). | |
| 45 | +- Per-SB corner math (`restoration.c:1277-1335`, no superres in this | |
| 46 | + decoder so the superres-scaled branches are dead code here): | |
| 47 | + `rcol0 = ceil_div(mi_col0 * mi_size_x, unit_size)`, | |
| 48 | + `rcol1 = min(ceil_div((mi_col0+sb_mi_w) * mi_size_x, unit_size), horz_units)`, | |
| 49 | + same for rows; `mi_size_x = 4` luma, `2` chroma (4:2:0). Then | |
| 50 | + `runit_idx = rcol + rrow * horz_units`, and `read_lr_unit` is called once | |
| 51 | + per `(rrow, rcol)` in that rectangle, per plane, in plane order. | |
| 52 | +- `read_lr_unit` symbol shape (`decodeframe.c:1687-1723`): if | |
| 53 | + `frame_restoration_type[plane] == Switchable`, read one | |
| 54 | + `restore_switchable` (3-way) symbol and dispatch; if `== Wiener`, read one | |
| 55 | + `restore_wiener` binary symbol, Wiener filter only if 1; if `== | |
| 56 | + Sgrproj`, same with `restore_sgrproj` and the SGR filter. `None` never | |
| 57 | + calls this at all (loop skipped by `frame_restoration_type[plane] == | |
| 58 | + RESTORE_NONE` — i.e. it's legal for the switchable-frame case per plane | |
| 59 | + even when only some planes use LR, since `frame_restoration_type` is | |
| 60 | + per-plane). | |
| 61 | +- Wiener coefficients: 3 taps per direction (vfilter/hfilter), each via | |
| 62 | + `aom_read_primitive_refsubexpfin(n, k, ref)` — SAME algorithm this crate | |
| 63 | + already has for global-motion params | |
| 64 | + (`ec-av1-syntax/src/frame.rs:1797-1839`, | |
| 65 | + `decode_signed/unsigned_subexp_with_ref`/`decode_subexp`/`inverse_recenter`) | |
| 66 | + but that existing implementation runs on a raw `BitReader` (frame header, | |
| 67 | + uncompressed bits); the LR coefficients are read through the arithmetic | |
| 68 | + decoder (`aom_reader`, i.e. this crate's `SymbolDecoder`), using | |
| 69 | + `aom_read_literal`/`aom_read_bit` as the raw-bit primitives — | |
| 70 | + `SymbolDecoder::literal(bits)` (`msac.rs:348`, already exists, built on | |
| 71 | + `symbol_fixed(&EQUIPROBABLE)`) is the exact equivalent. The subexp/recenter | |
| 72 | + math needs a second, msac-flavoured port of those four functions (same | |
| 73 | + arithmetic, `dec.literal(1)`/`dec.literal(n)` in place of `r.read_bit()`/ | |
| 74 | + `r.read_bits(n)`) — not reusable as-is because the reader types differ. | |
| 75 | + Per-tap `(min, max, k)`, all computed from libaom `restoration.h`: | |
| 76 | + tap0 `(-5, 10, k=1)`, tap1 `(-23, 8, k=2)`, tap2 `(-17, 46, k=3)` | |
| 77 | + (`WIENER_FILT_TAPn_MIDV` 3/-7/15, bits 4/5/6). Reference state | |
| 78 | + (`ref_wiener_info`) starts each tile at the tap midpoints (3, -7, 15) and | |
| 79 | + updates to the just-decoded value after each unit — same per-plane | |
| 80 | + running-reference shape as `PrevSegmentIds`/other adapting-per-tile state | |
| 81 | + already in this crate. | |
| 82 | +- SGR coefficients: one 4-bit literal `ep` (`SGRPROJ_PARAMS_BITS`) indexing | |
| 83 | + `av1_sgr_params`/`sgr_params` (a small fixed table this crate does not | |
| 84 | + have yet — needed even for stage 1 since `ep` selects which of `xqd[0]`/ | |
| 85 | + `xqd[1]` is forced to 0 vs subexp-coded, spec 7.17.3's `Sgr_Params`), then | |
| 86 | + 1-2 subexp-coded `xqd` values, ranges `(-96, 31, k=4)` and | |
| 87 | + `(-32, 95, k=4)` (`SGRPROJ_PRJ_MIN/MAX{0,1}`, `SGRPROJ_PRJ_BITS=7`). | |
| 88 | + | |
| 89 | +## Next lever | |
| 90 | +Port the msac-flavoured subexp/recenter quartet into `decode.rs` (or a new | |
| 91 | +`restoration.rs`), add the `Sgr_Params` table (8 entries, `restoration.c`'s | |
| 92 | +`av1_sgr_params`, not yet located — grep `sgr_params\[` there), write | |
| 93 | +`read_lr_unit` using the corner math above, thread | |
| 94 | +`&header.loop_restoration` into `decode_key_frame_tile_with_cdfs` and | |
| 95 | +`decode_inter_frame_tile_with_cdfs` (both already take a long positional | |
| 96 | +arg list matching this pattern — see `cdef`/`loop_filter` params; the two | |
| 97 | +public one-star wrappers `decode_key_frame_tile`/`decode_inter_frame_tile` | |
| 98 | +can keep their old signatures by passing `&LoopRestorationParams::default()` | |
| 99 | +internally, so the ~10 test call sites across `decode.rs` do not need | |
| 100 | +touching), call `read_lr` once per SB in both superblock loops | |
| 101 | +(`decode.rs` ~4547 key, ~9275 inter) BEFORE `decode_partition`/its | |
| 102 | +equivalent, and only then flip `stream.rs`'s refusal from unconditional to | |
| 103 | +"decode succeeded, still refuse by name because filters are not applied" | |
| 104 | +(stage 1's own exit criterion: prove the partition walk survives past a | |
| 105 | +real LR stream). Stage 1 gate: generate an aomenc stream with | |
| 106 | +`--enable-restoration=1` (default on) via the existing `gradients_source` | |
| 107 | +helper, decode it, assert the *new* refusal string appears (not the old | |
| 108 | +desync-shaped one / not an `Err` from deep inside a block reader), and | |
| 109 | +self-pin firing counts for wiener/sgrproj/switchable via thread-local | |
| 110 | +`Cell<usize>` hit counters the way every other gate in this lane's charter | |
| 111 | +requires. | |
| 112 | + | |
| 113 | +## Turn budget | |
| 114 | +Spent ~55 of 75 turns on recon (spec/libaom read_lr shape, corner math, | |
| 115 | +subexp/recenter porting requirements, CDF value derivation) plus the CDF | |
| 116 | +landing above; stopped short of wiring `read_lr` itself to avoid leaving a | |
| 117 | +half-edited, untested threading change across two large decode functions | |
| 118 | +uncommitted at the cap. Everything above is proven correct in principle | |
| 119 | +(SB-only firing is a real spec property, not skipped-and-hoped) but wiring | |
| 120 | ++ the gate itself are unstarted. |