forge
Explore
Log in

Don't you dare go Hollow

The encoder searches loop restoration, one Wiener unit at a time

Browse files at this commit

2b5ba17e557f739e8bba4b788146e7c56ecee622Ahmet Kaan GümüşParent: c3b0776
  • feat
    • the encoder searches loop restoration, one Wiener unit at a time
Show full message
feat(av1): the encoder searches loop restoration, one Wiener unit at a time

Loop restoration was the half of the in-loop filter chain the frame-level
search could not reach: a unit's filter is coded in the TILE, so no
re-decode under candidate header parameters can choose it, and the
decoder never handed back the pictures spec 7.17 filters from.

It does now, on request: `FrameCtx::capture_stages` leaves the
post-deblock and post-CDEF planes behind between the two stages, and off
(every decode but this search) it costs one bool read and not a copy.
The search prices four Wiener candidates per 64x64 unit against
RESTORE_NONE, running the DECODER's own kernel over the captured
picture, and the winner is written through the tile writers' new
per-unit syntax -- `restore_wiener` plus the reference-relative subexp
taps, whose writers now sit beside the readers they invert (the
test-only transcription of them is gone, and a roundtrip pins the pair).

A frame no unit restores keeps RESTORE_NONE on every plane instead of
paying a bit per unit for a tile full of zero symbols.
crates/ec-av1/src/encode.rsmodified+43 -37
@@ -3188,60 +3188,66 @@ fn pick_and_apply_filters(
3188 )?;3188 )?;
3189 header.loop_filter = lf;3189 header.loop_filter = lf;
3190 header.cdef = cdef;3190 header.cdef = cdef;
3191- // spec 7.17: loop restoration. Its per-unit filters live in the TILE, so3191+ // spec 7.17: loop restoration. Its per-unit filters live in the TILE, not
3192- // this stage codes the tile a second time no matter what -- first with3192+ // in the frame header, so a re-decode under candidate parameters cannot
3193- // every unit at `RESTORE_NONE`, which is the shape whose decode hands3193+ // search them the way deblocking and CDEF are searched above. What the
3194- // back the post-deblock and post-CDEF pictures the search needs (and3194+ // decoder hands back instead is its own filter chain's intermediates
3195- // whose output IS the post-CDEF picture), then again with the filters it3195+ // (`crate::decode::FrameCtx::capture_stages`), and the search runs on
3196- // picked.3196+ // those, through the decoder's own Wiener kernel.
3197- let mut lr = LoopRestorationParams::default();3197+ //
3198- if restoration_enabled() {3198+ // The capture rides the decode this stage was already going to do: the
3199- lr.frame_restoration_type[0] = ec_av1_syntax::RestorationType::Wiener;3199+ // re-decode of the `cdef_idx` tile when there is one, and otherwise one
3200- lr.loop_restoration_size = [64, 64, 64];3200+ // decode of the tile as it stands. That decode's header still says
3201- lr.uses_lr = true;3201+ // `RESTORE_NONE`, so the decoder takes no pre-CDEF snapshot and the
3202- }3202+ // post-CDEF picture stands in for it at LR's 3-row stripe borders --
3203- let grid_dims = crate::restoration::RestorationGrid::new(&lr, fw as u32, fh as u32);3203+ // an approximation inside the SEARCH only; the reconstruction this
3204- let n_units = if lr.uses_lr {3204+ // function ends up splicing is always a real decode of the real tile.
3205- grid_dims.horz_units[0] * grid_dims.vert_units[0]3205+ let lr_on = restoration_enabled();
3206+ fctx.capture_stages.set(lr_on);
3207+ let mut filtered = if idx_grid.is_empty() {
3208+ if lr_on {
3209+ decode(&lf, &cdef, &hdr, tile, &none_lr)?
3210+ } else {
3211+ picture
3212+ }
3206 } else {3213 } else {
3207- 03214+ let coded = recode(cdef.bits, sb_cols, &idx_grid, &[])?;
3208- };3215+ let picture = decode(&lf, &cdef, &hdr, &coded, &none_lr)?;
3209- let filtered = if idx_grid.is_empty() && n_units == 0 {3216+ *tile = coded;
3210 picture3217 picture
3211- } else {3218+ };
3212- let probe = recode(cdef.bits, sb_cols, &idx_grid, &vec![None; n_units])?;3219+ fctx.capture_stages.set(false);
3213- fctx.capture_stages.set(n_units > 0);3220+ let mut lr = LoopRestorationParams::default();
3214- let probe_picture = decode(&lf, &cdef, &hdr, &probe, &lr)?;3221+ if lr_on {
3215- fctx.capture_stages.set(false);3222+ let mut want = LoopRestorationParams {
3223+ loop_restoration_size: [64, 64, 64],
3224+ uses_lr: true,
3225+ ..LoopRestorationParams::default()
3226+ };
3227+ want.frame_restoration_type[0] = ec_av1_syntax::RestorationType::Wiener;
3216 let units = match crate::decode::take_filter_stages(fctx) {3228 let units = match crate::decode::take_filter_stages(fctx) {
3217 Some(stages) => crate::filter_search::pick_restoration(3229 Some(stages) => crate::filter_search::pick_restoration(
3218 &stages,3230 &stages,
3219 source[0],3231 source[0],
3220 luma.width,3232 luma.width,
3221 (fw, fh),3233 (fw, fh),
3222- &lr,3234+ &want,
3223 lambda,3235 lambda,
3224 fctx,3236 fctx,
3225 ),3237 ),
3226 None => Vec::new(),3238 None => Vec::new(),
3227 };3239 };
3240+ // A frame no unit restores keeps `RESTORE_NONE` on every plane
3241+ // rather than paying ~1 bit per 64x64 for a tile full of
3242+ // `restore_wiener = 0` symbols (the BD gate saw exactly that on the
3243+ // clip whose units never take a filter).
3228 if units.iter().any(Option::is_some) {3244 if units.iter().any(Option::is_some) {
3229 let coded = recode(cdef.bits, sb_cols, &idx_grid, &units)?;3245 let coded = recode(cdef.bits, sb_cols, &idx_grid, &units)?;
3230- let picture = decode(&lf, &cdef, &hdr, &coded, &lr)?;3246+ filtered = decode(&lf, &cdef, &hdr, &coded, &want)?;
3231 *tile = coded;3247 *tile = coded;
3232- picture3248+ lr = want;
3233- } else {
3234- // Nothing to restore: code the frame with `RESTORE_NONE` on every
3235- // plane instead of a tile full of `restore_wiener = 0` symbols
3236- // (worth ~1 bit per 64x64, which the BD gate saw on a clip whose
3237- // units never take a filter). The probe's picture already IS the
3238- // unrestored one -- a grid of `RESTORE_NONE` units restores
3239- // nothing -- so this costs one tile write and no decode.
3240- lr = LoopRestorationParams::default();
3241- *tile = recode(cdef.bits, sb_cols, &idx_grid, &[])?;
3242- probe_picture
3243 }3249 }
3244- };3250+ }
3245 header.loop_restoration = lr;3251 header.loop_restoration = lr;
3246 let dec_cw = filtered.width.div_ceil(2);3252 let dec_cw = filtered.width.div_ceil(2);
3247 let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));3253 let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));