From 570d0991b35d3db5ee965dbe9293343bdffc76b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 08:33:44 +0300 Subject: [PATCH] fix(av1): a same-size reference at a height that is not a multiple of 8 was refused, and loop restoration ran over the mi-rounded plane instead of the cropped one Two true-size-vs-mi-rounded defects (class av1-truesize-lane), both found by the (192,68)/(68,192) straddling arms lane-golomb r7 measured but could not add. 1. decode.rs `decode_inter_frame_tile_with_cdfs` compared the reference picture's height against `true_height = mi_rows * 4` (the mi-rounded size). A decoded `Picture` is cropped to the header's `fh`, so for any height that is not a multiple of 8 (68 -> mi_rows 18 -> 72) this compared 68 against 72 and refused EVERY same-size reference by name ("a reference picture whose height does not match this frame's own true size"). libaom compares references on the CROPPED dimensions -- `av1_setup_scale_factors_for_frame(&sf, ref->y_crop_width, ref->y_crop_height, cm->width, cm->height)` -- and calls a reference scaled only when those differ. Height must still match: AV1 superres never scales height. MEASURED: with `arms.push((192, 68, 5, false))` all eight cq attempts refused before this commit; after it none do (they now stop at another lane's inter SB-level AB partition, or reach a compare). 2. `apply_loop_restoration` passed each plane's `true_width`/`true_height`. LR is the one post-decode filter that does NOT walk the mi grid: libaom sizes every plane with `av1_get_upsampled_plane_size` (av1/common/restoration.c:47), asserts it equals the buffer's crop width (restoration.c:1106) and extends the border from that cropped edge (restoration.c:1109) before filtering. Deblock and CDEF do walk the mi grid, which is why this went unnoticed until a non-multiple-of-8 size. NOT exercised by any gate here: every straddle gate recipe runs `--enable-restoration=0`. Source-cited only. edge32 gate stays green on the merged tree: 38 pixel-exact attempts, 64-level edge bits [horz_or_vert=7 split=197] right-VERT=7, 2 named refusals, all eight cq levels at both depths. --- crates/ec-av1/src/decode.rs | 52 ++++++++++++++++++++++++++++++------- crates/ec-av1/src/stream.rs | 28 ++++++++++++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index e1a80dad..7bfb8d18 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -12798,13 +12798,30 @@ fn apply_loop_restoration( deblocked_v: &PlaneBuf, lr: &LoopRestorationParams, grid: &crate::restoration::RestorationGrid, + // lane-golomb r8: this frame header's own (cropped) luma size. Loop + // restoration is the ONE post-decode filter that does NOT run over the + // mi-rounded plane: libaom's `av1_loop_restoration_filter_frame` sizes + // every plane with `av1_get_upsampled_plane_size` -- `plane_w = + // ROUND_POWER_OF_TWO(cm->superres_upscaled_width, ss_x)`, `plane_h = + // ROUND_POWER_OF_TWO(cm->height, ss_y)` (`av1/common/restoration.c:47`) + // -- and asserts it equals the buffer's CROP width + // (`restoration.c:1106`), then `av1_extend_frame(..., plane_w, plane_h, + // ...)` (`restoration.c:1109`) replicates the right/bottom border from + // that cropped edge before any unit is filtered. Deblock and CDEF DO + // walk the mi grid, which is why passing `true_width`/`true_height` + // here went unnoticed until a frame whose size is not a multiple of 8 + // (68 -> mi-rounded 72): the LR taps at the last four real columns then + // read this decoder's own decoded samples at columns 68..71 where + // libaom reads column 67 replicated -- +-1 over the straddling band + // (class av1-truesize-lane: true frame size vs mi-rounded). + (frame_w, frame_h): (usize, usize), ) { y.data = crate::restoration::apply_loop_restoration_plane( &y.data, &deblocked_y.data, y.width, - y.true_width, - y.true_height, + frame_w, + frame_h, 0, lr.frame_restoration_type[0], lr.loop_restoration_size[0], @@ -12815,8 +12832,8 @@ fn apply_loop_restoration( &u.data, &deblocked_u.data, u.width, - u.true_width, - u.true_height, + frame_w.div_ceil(2), + frame_h.div_ceil(2), 1, lr.frame_restoration_type[1], lr.loop_restoration_size[1], @@ -12827,8 +12844,8 @@ fn apply_loop_restoration( &v.data, &deblocked_v.data, v.width, - v.true_width, - v.true_height, + frame_w.div_ceil(2), + frame_h.div_ceil(2), 1, lr.frame_restoration_type[2], lr.loop_restoration_size[2], @@ -14837,7 +14854,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); - apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); + apply_loop_restoration( + &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, + (frame_width as usize, frame_height as usize), + ); let (fw, fh) = (frame_width as usize, frame_height as usize); if fw == width && fh == height { @@ -20713,7 +20733,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // true size. Height must still match: AV1 superres never scales height // (r8's derivation, `mc.rs`'s own doc), and nothing in this decode path // has a vertical scaling pass. - if reference.height != true_height { + // lane-golomb r8: the comparison is against the header's own frame + // height, NOT the mi-rounded `true_height`. libaom compares references + // on the CROPPED dimensions -- `av1_setup_scale_factors_for_frame(&sf, + // ref->y_crop_width, ref->y_crop_height, cm->width, cm->height)` + // (`av1/common/reconinter.c`), and a reference is "scaled" only when + // those differ (`av1_is_scaled`). A decoded `Picture` is cropped to + // `fw`/`fh`, so for any height that is not a multiple of 8 (68 -> + // mi_rows 18 -> `true_height` 72) this test compared 68 against 72 and + // refused every same-size reference (class av1-truesize-lane: true + // frame size vs mi-rounded). Height must still MATCH: AV1 superres + // never scales height, and no path here has a vertical scaling pass. + if reference.height != frame_height as usize { return Err(unsupported( "a reference picture whose height does not match this frame's own true size", )); @@ -22663,7 +22694,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); - apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); + apply_loop_restoration( + &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, + (frame_width as usize, frame_height as usize), + ); let motion_field = build_motion_field( &grid, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 319e4d72..dced19d4 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -18888,6 +18888,28 @@ mod tests { // The inter arm: >= 4 frames so the 32-level edge path in the INTER // tile walk is exercised too. arms.push((192, 80, 5, false)); + // lane-golomb r8 MEASURED, arm STILL NOT added (every cq level is + // vacuous, panicking or red -- each measured this round with + // `arms.push((192, 68, 5, false))`, log + // `$HOME/.cache/golomb-gate-r8{b,e,f,g}.log`): + // * the r7 blocker IS fixed -- no attempt refuses "a reference + // picture whose height does not match this frame's own true + // size" any more (`decode.rs`'s comparison now uses the header's + // frame height, per libaom's crop-dimension reference test). + // * cq35 now refuses LATER, by another lane's name: "an inter + // SB-level AB partition (HORZ_A/HORZ_B/VERT_A/VERT_B)" -- so the + // arm compares nothing (class `counter-from-refused-stream`). + // * cq40 PANICS in `decode_inter_block8` + // (`from_switchable_symbol` handed a 4th symbol, `mc.rs:203`) = + // lane-sub8's own open below-8x8 desync; a panic cannot be + // tolerated as a named refusal. + // * cq59 8-bit is RED: `frame 1 plane Y: 217 pixels differ, first + // at row 60 col 170 (ours 172 vs ffmpeg 173)`, and the cq35 + // 10-bit twin is RED at `row 56 col 48 (ours 530 vs ffmpeg + // 531)`. Both are +-1 bands in rows 56..63, i.e. the deblock/CDEF + // bleed ABOVE the 4-px straddling row band (64..67) -- the same + // shape as the `(68, 192, 5, false)` column defect below, so the + // two are very likely ONE straddling-band reconstruction defect. // lane-golomb r7 MEASURED, arm NOT added (it would be vacuous or red): // a 68-px axis leaves the last superblock straddling by 4 px, the // shape `read_var_tx_size`'s `blk_row >= max_h_mi || blk_col >= @@ -19010,6 +19032,12 @@ mod tests { } let tag = format!("{width}x{height} cq{cq} frames={frame_count} 10bit={ten_bit}"); let before = crate::decode::edge32_hits(); + // lane-golomb r8: name the attempt BEFORE decoding it -- a + // desync surfaces as a panic deep in mc.rs with no arm in the + // message, and r8 spent a run bisecting arms by hand. + if std::env::var_os("EC_GATE_VERBOSE").is_some() { + eprintln!("{NAME}: attempting {tag}"); + } let decoded = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => {