diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 44090bbe..83f64d01 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -4467,6 +4467,23 @@ pub fn decode_key_frame_tile( .map(|(picture, _)| picture) } +thread_local! { + /// lane-superres r3: the real decoded margin beyond `frame_width` up to + /// the mi-aligned `true_width`, set by the most recent + /// [`decode_key_frame_tile_with_cdfs`] call. See that function's own + /// comment at the write site. `None` when there was no margin to save. + static LAST_KEY_FRAME_WIDE_MARGIN: std::cell::RefCell> = + const { std::cell::RefCell::new(None) }; +} + +/// The margin [`decode_key_frame_tile_with_cdfs`] most recently stashed +/// (see [`LAST_KEY_FRAME_WIDE_MARGIN`]) -- `stream.rs`'s superres path +/// reads this immediately after the call, before any other key frame +/// decode can overwrite it. +pub(crate) fn take_last_key_frame_wide_margin() -> Option { + LAST_KEY_FRAME_WIDE_MARGIN.with(|m| m.borrow_mut().take()) +} + /// [`decode_key_frame_tile`], threading a cross-frame CDF forward (spec /// 7.20's `load_cdfs`): `initial_cdfs` is the caller's forwarded state (from /// a prior frame's saved reference slot) when set, or the spec 8.4 defaults @@ -4967,6 +4984,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let (fw, fh) = (frame_width as usize, frame_height as usize); if fw == width && fh == height { + LAST_KEY_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); return Ok(( Picture { width, @@ -4985,6 +5003,33 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( } out }; + // lane-superres r3: `true_width`/`true_height` (`mi_cols`/`mi_rows` * 4) + // is the real reconstructed extent -- columns `[fw, true_width)` hold + // genuine decoded samples (the last coding block straddles the frame + // edge whenever `frame_width` isn't 8-sample aligned), not padding. + // libaom's superres upscale runs on that buffer's own border-extended + // margin, so it reads those real columns, not a synthetic replicate of + // column `fw - 1`. Stash them for `stream.rs`'s superres path (a real + // aomenc 43->64 stream pinned column-by-column against libaom's own + // `av1_convolve_horiz_rs_c` via `scripts/superres-pin-harness.c` -- + // `row6-realedgeval` proved the replicate-of-`fw-1` padding this + // decoder used before was off by 1 at the columns the tap window + // reaches into that real margin; `None` when there is no margin (this + // frame's `true_width`/`true_height` already equal `fw`/`fh`, e.g. a + // non-superres frame whose width just isn't 32-block aligned). + LAST_KEY_FRAME_WIDE_MARGIN.with(|m| { + *m.borrow_mut() = if true_width > fw || true_height > fh { + Some(Picture { + width: true_width, + height: true_height, + y: crop(&y, true_width, true_height), + u: crop(&u, true_width.div_ceil(2), true_height.div_ceil(2)), + v: crop(&v, true_width.div_ceil(2), true_height.div_ceil(2)), + }) + } else { + None + } + }); Ok(( Picture { width: fw, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 02cb041c..db4b1e22 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -401,8 +401,16 @@ pub fn decode_stream(data: &[u8]) -> Result> { // and BEFORE this picture is stored as a reference or handed to // the caller -- loop restoration is not yet ported, so there is // no LR step here for the upscale to precede. + // r3: the real decoded margin beyond `frame_width` (set by the + // call above, right before anything else can overwrite it) -- + // see `decode::take_last_key_frame_wide_margin`'s doc. + let wide_margin = crate::decode::take_last_key_frame_wide_margin(); let picture = if header.use_superres { - crate::superres::upscale_picture(&picture, header.upscaled_width as usize) + crate::superres::upscale_picture( + &picture, + header.upscaled_width as usize, + wide_margin.as_ref(), + ) } else { picture }; diff --git a/crates/ec-av1/src/superres.rs b/crates/ec-av1/src/superres.rs index 1a1cd5c6..25d0bd8a 100644 --- a/crates/ec-av1/src/superres.rs +++ b/crates/ec-av1/src/superres.rs @@ -89,24 +89,33 @@ fn round_power_of_two(sum: i32, bits: u32) -> i32 { /// true, replicating the row's own edge pixels rather than sampling a /// neighbour tile). `in_width` and `out_width` must both be positive; /// `row.len() == in_width` and `out.len() == out_width`. -pub(crate) fn upscale_row(row: &[u8], out_width: usize, out: &mut [u8]) { +pub(crate) fn upscale_row(row: &[u8], real_right_margin: &[u8], out_width: usize, out: &mut [u8]) { let in_width = row.len(); debug_assert!(in_width > 0 && out_width > 0); debug_assert_eq!(out.len(), out_width); let x_step_qn = upscale_convolve_step(in_width as i64, out_width as i64); let x0_qn = upscale_convolve_x0(in_width as i64, out_width as i64, x_step_qn); - // Edge-replicated padding: the real decoder samples out-of-frame - // columns from the reference frame buffer's own border margin; - // `upscale_normative_rect` fills that margin with `input[0]`/ - // `input[width-1]` for a single-tile column before calling the - // convolver. `UPSCALE_NORMATIVE_TAPS/2 + 1` columns are provably - // enough (the C source's own `border_cols`); this pads generously. + // Right-edge padding: r1 assumed a pure edge-replicate of `row`'s own + // last column (matching `upscale_normative_rect`'s single-tile + // in-memory fill), but the real decoder's reconstructed buffer already + // holds genuine decoded samples past `frame_width` out to the + // mi-aligned `true_width` (the coding block straddling the frame edge) + // -- libaom's border extension replicates from THAT last real column, + // not from `frame_width - 1`. `real_right_margin` (from + // `decode::take_last_key_frame_wide_margin`) supplies those real + // pixels first; only once it runs out (or is empty, e.g. `fw` was + // already mi-aligned) does this fall back to r1's replicate. Pinned + // column-by-column against real libaom via `scripts/superres-pin- + // harness.c`'s `row6-realedgeval` case (r3). let pad = UPSCALE_NORMATIVE_TAPS + 2; let mut padded = vec![0u8; in_width + 2 * pad]; padded[..pad].fill(row[0]); padded[pad..pad + in_width].copy_from_slice(row); - padded[pad + in_width..].fill(row[in_width - 1]); + let real_n = real_right_margin.len().min(pad); + padded[pad + in_width..pad + in_width + real_n].copy_from_slice(&real_right_margin[..real_n]); + let replicate_from = real_right_margin.last().copied().unwrap_or(row[in_width - 1]); + padded[pad + in_width + real_n..].fill(replicate_from); // `av1_convolve_horiz_rs_c` is called with `input - 1` and itself // subtracts `UPSCALE_NORMATIVE_TAPS/2 - 1` more; net offset from the @@ -131,13 +140,25 @@ pub(crate) fn upscale_row(row: &[u8], out_width: usize, out: &mut [u8]) { /// widened to every row (`height2 == height`, no vertical scaling -- the /// spec only widens columns). `rows` is the plane's row-major byte buffer /// at `in_width`; returns a new buffer at `out_width`. -pub(crate) fn upscale_plane(rows: &[u8], height: usize, in_width: usize, out_width: usize) -> Vec { +pub(crate) fn upscale_plane( + rows: &[u8], + height: usize, + in_width: usize, + out_width: usize, + margin: Option<(&[u8], usize)>, +) -> Vec { debug_assert_eq!(rows.len(), height * in_width); let mut out = vec![0u8; height * out_width]; for r in 0..height { let src = &rows[r * in_width..(r + 1) * in_width]; let dst = &mut out[r * out_width..(r + 1) * out_width]; - upscale_row(src, out_width, dst); + let real_right = match margin { + Some((margin_rows, margin_width)) if margin_width > in_width => { + &margin_rows[r * margin_width + in_width..(r + 1) * margin_width] + } + _ => &[], + }; + upscale_row(src, real_right, out_width, dst); } out } @@ -156,8 +177,15 @@ pub(crate) fn superres_hits() -> usize { /// A whole [`crate::encode::Picture`]'s Y/U/V planes, upscaled from /// `frame_width`/`frame_height` (4:2:0 chroma) to `upscaled_width` at the /// same height -- spec 7.16 is horizontal-only. Increments -/// [`SUPERRES_HITS`] once per call. -pub(crate) fn upscale_picture(picture: &crate::encode::Picture, upscaled_width: usize) -> crate::encode::Picture { +/// [`SUPERRES_HITS`] once per call. `margin`, when set (r3, from +/// `decode::take_last_key_frame_wide_margin`), is the real decoded pixels +/// beyond `picture`'s own width out to the mi-aligned `true_width` -- see +/// [`upscale_row`]'s doc for why the right-edge padding needs it. +pub(crate) fn upscale_picture( + picture: &crate::encode::Picture, + upscaled_width: usize, + margin: Option<&crate::encode::Picture>, +) -> crate::encode::Picture { SUPERRES_HITS.with(|c| c.set(c.get() + 1)); let height = picture.height; let chroma_in_w = picture.width.div_ceil(2); @@ -166,9 +194,27 @@ pub(crate) fn upscale_picture(picture: &crate::encode::Picture, upscaled_width: crate::encode::Picture { width: upscaled_width, height, - y: upscale_plane(&picture.y, height, picture.width, upscaled_width), - u: upscale_plane(&picture.u, chroma_h, chroma_in_w, chroma_out_w), - v: upscale_plane(&picture.v, chroma_h, chroma_in_w, chroma_out_w), + y: upscale_plane( + &picture.y, + height, + picture.width, + upscaled_width, + margin.map(|m| (m.y.as_slice(), m.width)), + ), + u: upscale_plane( + &picture.u, + chroma_h, + chroma_in_w, + chroma_out_w, + margin.map(|m| (m.u.as_slice(), m.width.div_ceil(2))), + ), + v: upscale_plane( + &picture.v, + chroma_h, + chroma_in_w, + chroma_out_w, + margin.map(|m| (m.v.as_slice(), m.width.div_ceil(2))), + ), } } @@ -190,7 +236,7 @@ mod tests { fn upscale_row_matches_libaom_in8_out12() { let row = [10u8, 20, 30, 40, 50, 60, 70, 80]; let mut out = [0u8; 12]; - upscale_row(&row, 12, &mut out); + upscale_row(&row, &[], 12, &mut out); assert_eq!(out, [9, 14, 22, 29, 35, 42, 48, 55, 61, 68, 76, 81]); } @@ -198,7 +244,7 @@ mod tests { fn upscale_row_matches_libaom_in8_out16() { let row = [10u8, 20, 30, 40, 50, 60, 70, 80]; let mut out = [0u8; 16]; - upscale_row(&row, 16, &mut out); + upscale_row(&row, &[], 16, &mut out); assert_eq!( out, [9, 12, 17, 23, 28, 32, 37, 43, 48, 53, 58, 62, 67, 73, 78, 81] @@ -214,7 +260,26 @@ mod tests { fn upscale_row_of_a_flat_input_is_flat() { let row = [200u8; 8]; let mut out = [0u8; 12]; - upscale_row(&row, 12, &mut out); + upscale_row(&row, &[], 12, &mut out); assert_eq!(out, [200u8; 12]); } + + /// r3: the real 43->64 failing case, pinned against real libaom + /// including the right-edge margin (`scripts/superres-pin-harness.c`'s + /// `row6-realedgeval` case -- input row + real trailing decoded pixel + /// 140, not a replicate of the frame-edge pixel 141). Column 62 (0 + /// indexed) is exactly the pixel this gate mismatched by 1 before the + /// margin fix. + #[test] + fn upscale_row_with_real_margin_matches_libaom_in43_out64() { + let row: [u8; 43] = [ + 102, 102, 103, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 127, 128, 129, 130, 130, 131, 131, + 133, 134, 136, 137, 138, 139, 140, 140, 141, + ]; + let real_margin = [140u8]; + let mut out = [0u8; 64]; + upscale_row(&row, &real_margin, 64, &mut out); + assert_eq!(out[62], 141, "column 62 must match libaom, not the old replicate-padded 140"); + } } diff --git a/scripts/superres-pin-harness.c b/scripts/superres-pin-harness.c index 77aed05b..e8a57cc8 100644 --- a/scripts/superres-pin-harness.c +++ b/scripts/superres-pin-harness.c @@ -32,27 +32,113 @@ static int32_t get_upscale_convolve_x0(int in_length, int out_length, return (int32_t)((uint32_t)x0 & RS_SCALE_SUBPEL_MASK); } -int main(void) { - const int in_len = 8, out_len = 16; - uint8_t input[8] = { 10, 20, 30, 40, 50, 60, 70, 80 }; - const int border = UPSCALE_NORMATIVE_TAPS / 2 + 1; // 5 - uint8_t padded[8 + 2 * 16]; - uint8_t *row = padded + 16; // row[0..7] == input - memset(padded, input[0], 16); - memcpy(row, input, 8); - memset(row + 8, input[7], 16); +static void run_case_margin(const char *label, int in_len, int out_len, + const uint8_t *input, const uint8_t *margin, + int margin_len) { + uint8_t padded[512]; + const int pad = 32; + uint8_t *row = padded + pad; + memset(padded, input[0], pad); + memcpy(row, input, in_len); + memcpy(row + in_len, margin, margin_len); + memset(row + in_len + margin_len, margin[margin_len - 1], pad); + + int32_t x_step_qn = av1_get_upscale_convolve_step(in_len, out_len); + int32_t x0_qn = get_upscale_convolve_x0(in_len, out_len, x_step_qn); + printf("%s: x_step_qn=%d x0_qn=%d\n", label, x_step_qn, x0_qn); + uint8_t out[256]; + av1_convolve_horiz_rs_c(row - 1, 0, out, 0, out_len, 1, + &av1_resize_filter_normative[0][0], x0_qn, + x_step_qn); + printf("%s out:", label); + for (int i = 0; i < out_len; i++) printf(" %d", out[i]); + printf("\n"); +} + +// Runs one row through the real compiled libaom convolver, edge-replicated +// padding on both sides (the single-tile case, matching this decoder's +// `superres::upscale_row`). `right_pad_col` lets the caller feed a +// right-edge pad value that differs from `input[in_len-1]`, to test +// r3's charter question (does the padding value change the output at +// all near the right edge, i.e. does the failing case even depend on +// what's beyond column `in_len-1`). +static void run_case(const char *label, int in_len, int out_len, + const uint8_t *input, int right_pad_val) { + uint8_t padded[512]; + const int pad = 32; + uint8_t *row = padded + pad; + memset(padded, input[0], pad); + memcpy(row, input, in_len); + memset(row + in_len, right_pad_val, pad); int32_t x_step_qn = av1_get_upscale_convolve_step(in_len, out_len); int32_t x0_qn = get_upscale_convolve_x0(in_len, out_len, x_step_qn); - printf("x_step_qn=%d x0_qn=%d\n", x_step_qn, x0_qn); + printf("%s: in_len=%d out_len=%d x_step_qn=%d x0_qn=%d right_pad=%d\n", + label, in_len, out_len, x_step_qn, x0_qn, right_pad_val); - uint8_t out[12]; + uint8_t out[256]; av1_convolve_horiz_rs_c(row - 1, 0, out, 0, out_len, 1, &av1_resize_filter_normative[0][0], x0_qn, x_step_qn); - printf("out:"); + printf("%s out:", label); for (int i = 0; i < out_len; i++) printf(" %d", out[i]); printf("\n"); - (void)border; +} + +int main(void) { + uint8_t input8[8] = { 10, 20, 30, 40, 50, 60, 70, 80 }; + run_case("in8->out12", 8, 12, input8, input8[7]); + run_case("in8->out16", 8, 16, input8, input8[7]); + + // r3: the exact failing ratio (43 -> 64), row 6 of the real aomenc + // superres key-frame gate fixture (`EC_SUPERRES_DEBUG=1` dump of the + // input row this decoder feeds `upscale_row`, matched against the + // `DIFF frame=0 row=6 col=62 got=140 want=141` mismatch). Run once + // with the replicate-from-column-42 padding this decoder uses, and + // once with a right pad value that visibly differs, to settle whether + // the failing columns even read past the real input at all. + uint8_t row6[43] = { 102, 102, 103, 103, 104, 105, 106, 107, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 125, 126, 127, 127, 128, 129, + 130, 130, 131, 131, 133, 134, 136, 137, 138, 139, + 140, 140, 141 }; + run_case("row6-replicate", 43, 64, row6, row6[42]); + run_case("row6-farpad", 43, 64, row6, 255); + + // r3 decisive check: `CROPDBG` dumped the pre-crop reconstructed row at + // the real (mi-aligned) width 48 -- columns 43..47 are genuine decoded + // pixels (140,140,140,140,140), NOT a replicate of column 42 (141, this + // decoder's current padding value). Feed the real trailing pixels + // instead of a flat replicate and see if the failing columns change. + uint8_t row6_real48[48] = { 102, 102, 103, 103, 104, 105, 106, 107, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 125, 126, 127, 127, 128, 129, + 130, 130, 131, 131, 133, 134, 136, 137, 138, 139, + 140, 140, 141, 140, 140, 140, 140, 140 }; + run_case("row6-realpad48", 48, 64, row6_real48, row6_real48[47]); + // Same real trailing pixels but in_len still 43 (matches frame_width), + // right_pad taken from the first real trailing pixel (140) rather than + // the frame-edge pixel (141) -- the actual libaom border-extend source. + run_case("row6-realedgeval", 43, 64, row6, 140); + + // r3 round 2: frame 2's rows 17/18 still mismatched by 1 at col 62 with + // the flat-margin fix landed. Their real margin is NOT flat (a gradient + // slope continues past the frame edge), so this checks the fix's + // n-real-pixels-then-replicate strategy against the true 5-pixel margin. + uint8_t row17[43] = { 109, 109, 109, 110, 111, 112, 112, 113, 114, 115, + 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, + 126, 127, 128, 129, 130, 131, 131, 132, 133, 134, + 135, 136, 137, 139, 139, 140, 140, 141, 142, 142, + 143, 144, 145 }; + uint8_t margin17[5] = { 146, 146, 147, 147, 147 }; + run_case_margin("row17-margin", 43, 64, row17, margin17, 5); + + uint8_t row18[43] = { 109, 110, 110, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 122, 123, 124, 125, + 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, + 134, 136, 138, 140, 139, 140, 140, 141, 142, 142, + 143, 144, 145 }; + uint8_t margin18[5] = { 146, 147, 147, 148, 148 }; + run_case_margin("row18-margin", 43, 64, row18, margin18, 5); return 0; }