From 7ed859cf1d5f147fe4e9f6af85743f334c2d9736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 14 Sep 2026 19:58:53 +0300 Subject: [PATCH] vp9kf: sub8x8 mode-ctx order, D117 anchor, 4x4 specialized predictors, have_right, ctx span read, SB walk units, loop-filter rewrite; lossless 4096/4096 (HANDOFF #4) --- crates/ec-vp9/src/decode.rs | 68 +++++++--- crates/ec-vp9/src/intra.rs | 65 +++++++++- crates/ec-vp9/src/loopfilter.rs | 172 ++++++++++++++------------ crates/ec-vp9/src/modes.rs | 13 +- crates/ec-vp9/tests/keyframe_exact.rs | 23 ++++ lanes/vp9kf.report.md | 49 ++++++++ 6 files changed, 289 insertions(+), 101 deletions(-) diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index 59ed3a16..17a27910 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -242,10 +242,12 @@ impl Decoder { let mut ti = 0usize; for tr in 0..tile_rows { let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; - let row_hi = ((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2).min(mi_rows); + let row_hi = + (((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2) * SB_MI).min(mi_rows); for tc in 0..tile_cols { let col_lo = ((sb64_cols * tc) >> hdr.tile_info.cols_log2) * SB_MI; - let col_hi = ((sb64_cols * (tc + 1)) >> hdr.tile_info.cols_log2).min(mi_cols); + let col_hi = + (((sb64_cols * (tc + 1)) >> hdr.tile_info.cols_log2) * SB_MI).min(mi_cols); if crate::trace_enabled() { eprintln!( "TILE {} bytes first={:02x?}", @@ -263,12 +265,12 @@ impl Decoder { let mut mi = MiState::new(mi_cols, mi_rows); ectx.iter_mut().for_each(|p| p.above.fill(0)); mi.above_seg.fill(0); - for sb_row in row_lo..row_hi { - if sb_row % SB_MI == 0 { + for sb_row in (row_lo..row_hi).step_by(SB_MI) { + if sb_row == row_lo { ectx.iter_mut().for_each(|p| p.left = [0; 32]); mi.left_seg = [0; 32]; } - for sb_col in col_lo..col_hi { + for sb_col in (col_lo..col_hi).step_by(SB_MI) { self.decode_partition( &mut r, &mut mi, @@ -698,6 +700,11 @@ impl Decoder { let y0 = ((mi_row * 8) >> s) + tx_row * 4; let up = tx_row != 0 || mi_row > tile_row_start; let lft = tx_col != 0 || mi_col > tile_col_start; + // vp9_predict_intra_block `have_right`: the tx block is not in the + // last tx column of ITS OWN block (pd->n4_w units) — above-right + // past the block's right edge belongs to a not-yet-decoded + // neighbour, so it must stage as unavailable. + let rgt = tx_col + (1usize << tx_size) < bw * 2 >> s; // 1. Intra prediction (reads reconstructed neighbours). { @@ -713,7 +720,7 @@ impl Decoder { bs, up, lft, - x0 + bs < pw, + rgt, ); } @@ -739,8 +746,24 @@ impl Decoder { let ax = ((mi_col << 1) >> s) + tx_col; let lrows = 32usize >> s; let ay = (((mi_row << 1) >> s) + tx_row) % lrows; - let ctx_in = - usize::from(ectx[plane].above[ax] != 0) + usize::from(ectx[plane].left[ay] != 0); + // libvpx reads the context as !!*(uintN_t *)a / l over the WHOLE + // tx-block span (uint16 for 8x8, uint32 for 16x16, uint64 for + // 32x32), not just the first entry. + let nblocks = 1usize << tx_size; + let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0)) + + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0)); + if crate::trace_enabled() { + eprintln!( + "CTX p={} prob={:?} ax={} ay={} a={} l={} ctx={}", + plane, + self.ctx.coef[tx_size][ptype][0][0][ctx_in], + ax, + ay, + ectx[plane].above[ax], + ectx[plane].left[ay], + ctx_in + ); + } let coef = decode_coefs( r, ptype, @@ -806,8 +829,21 @@ impl Decoder { if coef.eob > 0 { let dst_off = y0 * stride + x0; let (data, _) = planes.plane(plane); - let t = if crate::trace_enabled() && plane == 0 && mi_row == 0 && mi_col == 0 { - Some(data[0]) + let probe = crate::trace_enabled() + && plane == 0 + && std::env::var("EC_VP9_PROBE_TU") + .ok() + .and_then(|s| { + let mut it = s.split(','); + Some((it.next()?.parse::().ok()?, it.next()?.parse().ok()?)) + }) + .is_some_and(|(px, py)| x0 == px && y0 == py); + let t: Option>> = if probe { + Some( + (0..4usize) + .map(|rr| data[dst_off + rr * stride..dst_off + rr * stride + 4].to_vec()) + .collect(), + ) } else { None }; @@ -821,11 +857,13 @@ impl Decoder { lossless, ); if let Some(before) = t { - let row: Vec = data[0..8].to_vec(); - eprintln!( - "RC x0={x0} y0={y0} tx={tx_size} before={before} after={} row0={row:?}", - data[0] - ); + let after: Vec> = (0..4usize) + .map(|rr| data[dst_off + rr * stride..dst_off + rr * stride + 4].to_vec()) + .collect(); + eprintln!("RC x0={x0} y0={y0} tx={tx_size} mode={mode}"); + for rr in 0..4 { + eprintln!(" pred{rr}={:?} after{rr}={:?}", before[rr], after[rr]); + } } } Ok(()) diff --git a/crates/ec-vp9/src/intra.rs b/crates/ec-vp9/src/intra.rs index 248e0b0c..1983c01f 100644 --- a/crates/ec-vp9/src/intra.rs +++ b/crates/ec-vp9/src/intra.rs @@ -145,6 +145,22 @@ pub(crate) fn build_intra_predictors( // The C's `const_above_row = above_ref` shortcut for bs==4 && // right&&left stages the same bytes, so above_data is equivalent. + if crate::trace_enabled() + && std::env::var("EC_VP9_PROBE_TU") + .ok() + .and_then(|s| { + let mut it = s.split(','); + Some((it.next()?.parse::().ok()?, it.next()?.parse().ok()?)) + }) + .is_some_and(|(px, py)| x0 == px && y0 == py) + { + eprintln!( + "STAGE x0={x0} y0={y0} mode={mode} bs={bs} above={:?} cor={} left={:?}", + &above_data[A..A + 8], + above_data[A - 1], + &left_col[..4] + ); + } let dst = &mut data[y0 * stride + x0..]; dispatch( mode, bs, dst, stride, &above_data, &left_col, up_available, left_available, @@ -192,6 +208,51 @@ fn dispatch( dst[r * stride..r * stride + bs].fill(left[r]); } } + // The 4x4 kernels in intrapred.c are specialized implementations, + // NOT the shared `d45_predictor`/`d63_predictor`/`d153_predictor` + // INLINE loops: they continue the diagonal with the staged + // above-right samples (vpx_d45/d63_predictor_4x4_c, and d153's + // DST(2,3)). Only 8x8+ use the generic loops below. + 3 if bs == 4 => { + // vpx_d45_predictor_4x4_c + for r in 0..4usize { + for c in 0..4usize { + dst[r * stride + c] = if r + c < 6 { + avg3(ab[r + c], ab[r + c + 1], ab[r + c + 2]) + } else { + ab[7] + }; + } + } + } + 6 if bs == 4 => { + // vpx_d153_predictor_4x4_c + dst[0] = avg2(left[0], cor); + dst[stride] = avg2(left[1], left[0]); + dst[2 * stride] = avg2(left[2], left[1]); + dst[3 * stride] = avg2(left[3], left[2]); + dst[1] = avg3(left[0], cor, ab[0]); + dst[stride + 1] = avg3(left[1], left[0], cor); + dst[2 * stride + 1] = avg3(left[2], left[1], left[0]); + dst[3 * stride + 1] = avg3(left[3], left[2], left[1]); + dst[2] = avg3(cor, ab[0], ab[1]); + dst[stride + 2] = dst[0]; + dst[2 * stride + 2] = dst[stride]; + dst[3 * stride + 2] = dst[2 * stride]; + dst[3] = avg3(ab[0], ab[1], ab[2]); + dst[stride + 3] = dst[1]; + dst[2 * stride + 3] = avg3(left[1], left[0], cor); // DST(3, 2) + dst[3 * stride + 3] = avg3(left[2], left[1], left[0]); + } + 8 if bs == 4 => { + // vpx_d63_predictor_4x4_c + for c in 0..4usize { + dst[c] = avg2(ab[c], ab[c + 1]); + dst[stride + c] = avg3(ab[c], ab[c + 1], ab[c + 2]); + dst[2 * stride + c] = avg2(ab[c + 1], ab[c + 2]); + dst[3 * stride + c] = avg3(ab[c + 1], ab[c + 2], ab[c + 3]); + } + } 3 => { // d45 let above_right = ab[bs - 1]; @@ -237,8 +298,10 @@ fn dispatch( dst[stride + c] = avg3(above[A + c - 2], ab[c - 1], ab[c]); } dst[2 * stride] = avg3(cor, left[0], left[1]); + // libvpx anchors this loop at row 2 (`dst` walked twice); + // our `dst` is the block base, so the index is absolute. for r in 3..bs { - dst[(r - 2) * stride] = avg3(left[r - 3], left[r - 2], left[r - 1]); + dst[r * stride] = avg3(left[r - 3], left[r - 2], left[r - 1]); } for r in 2..bs { for c in 1..bs { diff --git a/crates/ec-vp9/src/loopfilter.rs b/crates/ec-vp9/src/loopfilter.rs index 7538634d..3fcfa129 100644 --- a/crates/ec-vp9/src/loopfilter.rs +++ b/crates/ec-vp9/src/loopfilter.rs @@ -26,11 +26,14 @@ fn sc(x: i32) -> i32 { } /// `filter4` kernel; `idx` = [op1, op0, oq0, oq1]. +/// `vpx_dsp filter4`: updates p1/p0/q0/q1 in place. `mask` gates the +/// whole filter, `hev` the outer taps (vp9_loopfilter.c:81). fn filter4(s: &mut [u8], idx: [usize; 4], thresh: i32, mask: bool) { if !mask { return; } - let (p1, p0, q0, q1) = (s[idx[0]], s[idx[1]], s[idx[2]], s[idx[3]]); + let (op1, op0, oq0, oq1) = (idx[0], idx[1], idx[2], idx[3]); + let (p1, p0, q0, q1) = (s[op1], s[op0], s[oq0], s[oq1]); let hev = (p1 as i32 - p0 as i32).abs() > thresh || (q1 as i32 - q0 as i32).abs() > thresh; let (ps1, ps0, qs0, qs1) = ( (p1 ^ 0x80) as i8 as i32, @@ -39,60 +42,60 @@ fn filter4(s: &mut [u8], idx: [usize; 4], thresh: i32, mask: bool) { (q1 ^ 0x80) as i8 as i32, ); let mut filter = sc(ps1 - qs1); - if !hev { + if hev { + // add outer taps if we have high edge variance (& hev mask) + } else { filter = 0; } + // inner taps filter = sc(filter + 3 * (qs0 - ps0)); + // round one side +4 and the other +3 let filter1 = sc(filter + 4) >> 3; let filter2 = sc(filter + 3) >> 3; - s[idx[3]] = (sc(qs0 - filter1) ^ 0x80) as u8; - s[idx[2]] = (sc(ps0 + filter2) ^ 0x80) as u8; - let mut f = (filter1 + 1) >> 1; - if hev { - f = 0; - } - s[idx[1]] = (sc(ps1 + f) ^ 0x80) as u8; - s[idx[0]] = (sc(qs1 - f) ^ 0x80) as u8; + s[oq0] = (sc(qs0 - filter1) ^ 0x80) as u8; + s[op0] = (sc(ps0 + filter2) ^ 0x80) as u8; + // outer tap adjustments (& ~hev) + let f = if hev { 0 } else { (filter1 + 1) >> 1 }; + s[oq1] = (sc(qs1 - f) ^ 0x80) as u8; + s[op1] = (sc(ps1 + f) ^ 0x80) as u8; } -/// The tapped edge filter: `dir` 0 = horizontal edges (rows step by -/// stride), 1 = vertical (columns step by 1). `taps` in {4, 8, 16}. -fn lpf_edge(data: &mut [u8], off: usize, stride: usize, dir: u8, taps: u8, blimit: i32, limit: i32) { +fn lpf_edge( + data: &mut [u8], + off: usize, + stride: usize, + dir: u8, + taps: u8, + blimit: i32, + limit: i32, + thresh: i32, +) { + // `dir` 0 = horizontal edges (rows step by stride), 1 = vertical + // (columns step by 1). `taps` in {4, 8, 16}. let step: isize = if dir == 0 { stride as isize } else { 1 }; let edge = off as isize; - if std::env::var_os("EC_VP9_DBG").is_some() { - let need = if taps == 16 { 8 } else if taps == 8 { 4 } else { 2 }; - let lo = edge - need * step; - let hi = edge + need * step; - if lo < 0 || hi >= data.len() as isize { - eprintln!("DBG lpf OOB off={off} dir={dir} taps={taps} len={}", data.len()); - } - } macro_rules! rd { ($k:expr) => { data[(edge + $k * step) as usize] as i32 } } macro_rules! put { ($k:expr, $v:expr) => { data[(edge + $k * step) as usize] = $v; } } - // filter_mask (loopfilter.c:30): p3..q3 gradient limits + blimit. - let mask8 = (rd!(-4) - rd!(-3)).abs() <= limit + // filter_mask (vpx_dsp/loopfilter.c:34): p3..q3 gradient limits + + // blimit; shared by the 4-, 8- and 16-tap entry points. + let mask = (rd!(-4) - rd!(-3)).abs() <= limit && (rd!(-3) - rd!(-2)).abs() <= limit && (rd!(-2) - rd!(-1)).abs() <= limit && (rd!(1) - rd!(0)).abs() <= limit && (rd!(2) - rd!(1)).abs() <= limit && (rd!(3) - rd!(2)).abs() <= limit && 2 * (rd!(-1) - rd!(0)).abs() + (rd!(-2) - rd!(1)).abs() / 2 <= blimit; + let idx4 = [ + (edge - 2 * step) as usize, + (edge - step) as usize, + edge as usize, + (edge + step) as usize, + ]; if taps == 4 { - // lpf4 uses the p1/p0/q0/q1 form of the mask. - let mask = (rd!(-1) - rd!(0)).abs() <= limit - && (rd!(1) - rd!(0)).abs() <= limit - && 2 * (rd!(-1) - rd!(0)).abs() + (rd!(-2) - rd!(1)).abs() / 2 <= blimit; - let idx = [ - (edge - 2 * step) as usize, - (edge - step) as usize, - edge as usize, - (edge + step) as usize, - ]; - filter4(data, idx, limit, mask); + filter4(data, idx4, thresh, mask); return; } - if !mask8 { + if !mask { return; } // flat_mask4(1, p3..q3) @@ -104,13 +107,8 @@ fn lpf_edge(data: &mut [u8], off: usize, stride: usize, dir: u8, taps: u8, blimi && (rd!(3) - rd!(0)).abs() <= 1; if taps == 8 || !flat { if !flat { - let idx = [ - (edge - 2 * step) as usize, - (edge - step) as usize, - edge as usize, - (edge + step) as usize, - ]; - filter4(data, idx, limit, true); + // plain filter4 with the real mask and hev threshold + filter4(data, idx4, thresh, mask); return; } // 7-tap [1,1,1,2,1,1,1] outputs (filter8). @@ -124,16 +122,17 @@ fn lpf_edge(data: &mut [u8], off: usize, stride: usize, dir: u8, taps: u8, blimi put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8); return; } - // 16-tap: flat2 via flat_mask5(1, p7..q7) - let flat2 = flat - && (rd!(-8) - rd!(-1)).abs() <= 1 - && (rd!(8) - rd!(0)).abs() <= 1 + // 16-tap: flat2 = flat_mask5(1, p7..p4, p0, q0, q4..q7) — every + // sample compared against p0 (rd!(-1)). + let flat2 = (rd!(-8) - rd!(-1)).abs() <= 1 && (rd!(-7) - rd!(-1)).abs() <= 1 - && (rd!(7) - rd!(0)).abs() <= 1 && (rd!(-6) - rd!(-1)).abs() <= 1 - && (rd!(6) - rd!(0)).abs() <= 1 && (rd!(-5) - rd!(-1)).abs() <= 1 - && (rd!(5) - rd!(0)).abs() <= 1; + && (rd!(0) - rd!(-1)).abs() <= 1 + && (rd!(4) - rd!(-1)).abs() <= 1 + && (rd!(5) - rd!(-1)).abs() <= 1 + && (rd!(6) - rd!(-1)).abs() <= 1 + && (rd!(7) - rd!(-1)).abs() <= 1; if !flat2 { let (p3, p2, p1, p0, q0, q1, q2, q3) = (rd!(-4), rd!(-3), rd!(-2), rd!(-1), rd!(0), rd!(1), rd!(2), rd!(3)); @@ -180,8 +179,9 @@ pub(crate) struct LfGrids { pub mi_rows: usize, } -/// limits for a level (spec 8.8.1, `update_sharpness`). -fn limits(level: u8, sharpness: u8) -> (i32, i32) { +/// limits for a level (spec 8.8.1, `update_sharpness` + +/// `vp9_loop_filter_init`): (mblim, lim, hev_thr). +fn limits(level: u8, sharpness: u8) -> (i32, i32, i32) { let mut bil = level as i32 >> ((usize::from(sharpness > 0)) + (usize::from(sharpness > 4))); if sharpness > 0 && bil > 9 - sharpness as i32 { bil = 9 - sharpness as i32; @@ -189,7 +189,7 @@ fn limits(level: u8, sharpness: u8) -> (i32, i32) { if bil < 1 { bil = 1; } - (2 * (level as i32 + 2) + bil, bil) + (2 * (level as i32 + 2) + bil, bil, level as i32 >> 4) } impl LfGrids { @@ -236,10 +236,10 @@ impl LfGrids { if taps == 16 && (c4 * 4 < 8 || c4 * 4 + 8 > y_w) { taps = 8; } - let (bl, li) = limits(level, sharpness); + let (bl, li, th) = limits(level, sharpness); let off = r4 * 4 * stride + c4 * 4; if y_w >= c4 * 4 + 4 { - lpf_edge(y, off, stride, 1, taps, bl, li); + lpf_edge(y, off, stride, 1, taps, bl, li, th); } } } @@ -268,47 +268,61 @@ impl LfGrids { if taps == 16 && (r4 * 4 < 8 || r4 * 4 + 8 > y_h) { taps = 8; } - let (bl, li) = limits(level, sharpness); + let (bl, li, th) = limits(level, sharpness); let off = r4 * 4 * stride + c4 * 4; if y_h >= r4 * 4 + 4 { - lpf_edge(y, off, stride, 0, taps, bl, li); + lpf_edge(y, off, stride, 0, taps, bl, li, th); } } } - // Chroma (uv 4x4 grid = mi grid). + // Chroma (4:2:0): one 4x4 chroma TU per MI, so the only filtered + // edges are the MI boundaries (vp9_filter_block_plane_ss11): + // vertical on every MI column boundary, horizontal on every MI + // row boundary, taps from the plane's uv transform size. for data in [&mut *u, &mut *v] { + // Vertical: left edge of each MI (except the leftmost column). for r in 0..self.mi_rows { for c in 1..self.mi_cols { let cell = r * self.mi_cols + c; - let (bsize, _bor, boc) = self.blk[cell]; + let (bsize, _bor, _boc) = self.blk[cell]; let level = self.level8[cell]; if level == 0 { continue; } let uv_tx = uv_txsize_lookup(bsize as usize, self.otx[cell] as usize); - let taps = if bsize as usize >= 6 { - if c == boc as usize { - match uv_tx { - TX_16X16 if c * 8 >= 8 && c * 8 + 8 <= uv_w => 16, - TX_16X16 => 8, - TX_8X8 => 8, - _ if c % 4 == 0 => 8, - _ => 4, - } - } else if uv_tx == TX_4X4 { - 4 - } else { - continue; - } - } else if c % 2 == 0 && r % 2 == 0 { - if c % 4 == 0 { 8 } else { 4 } - } else { + let taps = match uv_tx { + TX_16X16 if c * 4 >= 8 && c * 4 + 8 <= uv_w => 16, + TX_16X16 => 8, + TX_8X8 => 8, + _ => 4, + }; + let (bl, li, th) = limits(level, sharpness); + let off = r * 4 * uv_stride + c * 4; + if uv_w >= c * 4 + 4 { + lpf_edge(data, off, uv_stride, 1, taps, bl, li, th); + } + } + } + // Horizontal: top edge of each MI (except the top row). + for r in 1..self.mi_rows { + for c in 0..self.mi_cols { + let cell = r * self.mi_cols + c; + let (bsize, _bor, _boc) = self.blk[cell]; + let level = self.level8[cell]; + if level == 0 { continue; + } + let uv_tx = uv_txsize_lookup(bsize as usize, self.otx[cell] as usize); + let taps = match uv_tx { + TX_16X16 if r * 4 >= 8 && r * 4 + 8 <= uv_h => 16, + TX_16X16 => 8, + TX_8X8 => 8, + _ => 4, }; - let (bl, li) = limits(level, sharpness); - let off = r * 8 * uv_stride + c * 8; - if uv_w >= c * 8 + 8 { - lpf_edge(data, off, uv_stride, 1, taps, bl, li); + let (bl, li, th) = limits(level, sharpness); + let off = r * 4 * uv_stride + c * 4; + if uv_h >= r * 4 + 4 { + lpf_edge(data, off, uv_stride, 0, taps, bl, li, th); } } } diff --git a/crates/ec-vp9/src/modes.rs b/crates/ec-vp9/src/modes.rs index 41035f46..249dd315 100644 --- a/crates/ec-vp9/src/modes.rs +++ b/crates/ec-vp9/src/modes.rs @@ -225,29 +225,30 @@ pub(crate) fn read_intra_frame_mode_info( mi.mode = mi.bmi[3]; } 1 => { - // BLOCK_4X8 + // BLOCK_4X8; decodemv.c commits bmi[0]/bmi[2] BEFORE the + // second mode's probs are derived from the current bmi. let a0 = above_block_mode(&mi.bmi, above_pair, 0); let l0 = left_block_mode(&mi.bmi, left_pair, 0); let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0)); + mi.bmi[0] = m0; + mi.bmi[2] = m0; let a1 = above_block_mode(&mi.bmi, above_pair, 1); let l1 = left_block_mode(&mi.bmi, left_pair, 1); let m1 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a1, l1)); - mi.bmi[0] = m0; - mi.bmi[2] = m0; mi.bmi[1] = m1; mi.bmi[3] = m1; mi.mode = m1; } 2 => { - // BLOCK_8X4 + // BLOCK_8X4; same commit-before-read ordering. let a0 = above_block_mode(&mi.bmi, above_pair, 0); let l0 = left_block_mode(&mi.bmi, left_pair, 0); let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0)); + mi.bmi[0] = m0; + mi.bmi[1] = m0; let a2 = above_block_mode(&mi.bmi, above_pair, 2); let l2 = left_block_mode(&mi.bmi, left_pair, 2); let m2 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a2, l2)); - mi.bmi[0] = m0; - mi.bmi[1] = m0; mi.bmi[2] = m2; mi.bmi[3] = m2; mi.mode = m2; diff --git a/crates/ec-vp9/tests/keyframe_exact.rs b/crates/ec-vp9/tests/keyframe_exact.rs index 3069ede4..40d08203 100644 --- a/crates/ec-vp9/tests/keyframe_exact.rs +++ b/crates/ec-vp9/tests/keyframe_exact.rs @@ -93,6 +93,29 @@ fn keyframes_match_ffmpeg() { compare(&path); } +#[test] +fn lossless_64_matches_ffmpeg() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let path = dir.join("fixtures/vp9/lossless-64.ivf"); + let bytes = std::fs::read(&path).unwrap(); + let (_fourcc, w, h, frames) = ivf::parse_ivf(&bytes); + let reference = ffmpeg_raw_yuv(&path); + let mut decoder = Decoder::new(); + let pic = decoder + .decode(&frames[0].data) + .expect("decode must succeed") + .expect("frame is shown"); + let (w, h) = (w as usize, h as usize); + let uv = (w / 2) * (h / 2); + let want_y = &reference[..w * h]; + let want_u = &reference[w * h..w * h + uv]; + let want_v = &reference[w * h + uv..]; + assert!(pic.y.iter().eq(want_y.iter()), "lossless Y must be byte-exact"); + assert!(pic.u.iter().eq(want_u.iter()), "lossless U must be byte-exact"); + assert!(pic.v.iter().eq(want_v.iter()), "lossless V must be byte-exact"); + assert_eq!((pic.width, pic.height), (w as u16, h as u16)); +} + #[test] fn inter_is_named_unsupported() { let Some(dir) = ivf::fixture_dir() else { return }; diff --git a/lanes/vp9kf.report.md b/lanes/vp9kf.report.md index 0484edb6..7e095f60 100644 --- a/lanes/vp9kf.report.md +++ b/lanes/vp9kf.report.md @@ -189,3 +189,52 @@ ours with a TOK-style trace in tokens.rs decode_coefs (oracle TOK print is in /tmp/vp9loss/libvpx-src/vp9/decoder/vp9_detokenize.c decode_coefs loop top); (2) re-check `lossless_64_matches_ffmpeg` after; keep scratch files until green. Oracle: /tmp/vp9loss/{libvpx-src,drv} (see above; TOK c/band/ctx/p0 print per symbol). + +## HANDOFF #4 (Vp9Sub8, 2026-09-14) — lossless 4096/4096 GREEN; key-320 decode matches, loop-filter 4x4-int edge remains + +1. **Sub8x8 second y-mode ctx (FIXED, ec-vp9/src/modes.rs).** decodemv.c + commits bmi[0]/bmi[2] (4X8) or bmi[0]/bmi[1] (8X4) BEFORE deriving the + second mode's probs from the current bmi; we read both modes first, so + the second mode always saw DC/DC. +2. **D117 first-column anchor (FIXED, ec-vp9/src/intra.rs).** libvpx's + loop is anchored at row 2 (its `dst` walks twice); our base-relative + `dst[(r-2)*stride]` wrote row 1 instead of row r. +3. **Specialized 4x4 predictors (FIXED, intra.rs).** intrapred.c's + vpx_d45/d63/d153_predictor_4x4_c are NOT the shared INLINE loops: they + continue the diagonal with the staged above-right (d153 also differs + at DST(3,2)). d117/d135/d207 4x4 equal the generic loops; d45/d63/ + d153 do not. +4. **`have_right` decode-order semantics (FIXED, decode.rs).** + vp9_predict_intra_block's have_right = (aoff + txw) < n4_w — not a + frame-geometry test. Above-right past the block's own edge reads + not-yet-decoded neighbours and must stage as unavailable. +5. **Entropy-ctx span read (FIXED, decode.rs).** vp9_decode_block_tokens + computes ctx as !!*(uintN_t *)a over the WHOLE tx span; we read only + above[ax]/left[ay]. Wrong ctx on TX_8X8+ desynced key-320 tokens. +6. **Tile SB walk units (FIXED, decode.rs).** row_hi/col_hi are SB-unit + boundaries but the loop stepped per-MI — for 320x240 the 64x64 + decode_partition ran at every MI (overlapping re-decodes); lossless-64 + only survived because its single-tile boundary collapses to 1. This + was the altref f0 "tile bool decoder desync" class. Loop now steps by + SB_MI and clamps to mi_rows/mi_cols. +7. **Loop filter rewrite (loopfilter.rs).** filter4 slot mapping was + scrambled (p0/q0 updates written into q1/p1), lpf4 used a VP8-style + mask instead of the shared p3..q3 filter_mask, the lpf8 non-flat + fallback passed mask=true with limit as thresh, flat2 compared the q + side against q0 instead of p0, hev_thr (lvl>>4) was missing, and the + chroma pass used an 8-px MI stride with no horizontal edges (OOB on + 320x240). All re-derived from vpx_dsp/loopfilter.c + + vp9_loopfilter.c. + +**State at yield:** lossless-64 4096/4096 vs ffmpeg (Y/U/V byte-exact; +`lossless_64_matches_ffmpeg` added). key-320: tokens + prediction match +the oracle (inter_is_named_unsupported PASSES — the desync is gone); +keyframes_match_ffmpeg first diff (7,1) ours 75 ref 76 — remaining class +is the luma 4x4-internal-edge filter (libvpx filter_selectively_* +mask_4x4_int half-position variant), NOT tokens or prediction. Next: +(1) port mask_4x4_int shifted filtering (and the frame bottom/right tap +rules), then re-run keyframes_match_ffmpeg; (2) after green remove +EC_VP9_FORCE_UH/HSZ, scratch_* tests, and the EC_VP9_PROBE_TU/STAGE/CTX +debug prints in decode.rs/intra.rs. +Oracle: /tmp/vp9loss/{libvpx-src,drv}; /tmp/vp9loss/d45probe2.c proves +SIMD==C for the specialized 4x4 kernels.