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