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)
Browse files at this commit7ed859cf1d5f147fe4e9f6af85743f334c2d9736Parent: 76c8a776 changed files +289 -101
- crates/ec-vp9/src/decode.rs +53 -15
- crates/ec-vp9/src/intra.rs +64 -1
- crates/ec-vp9/src/loopfilter.rs +93 -79
- crates/ec-vp9/src/modes.rs +7 -6
- crates/ec-vp9/tests/keyframe_exact.rs +23 -0
- lanes/vp9kf.report.md +49 -0
crates/ec-vp9/src/decode.rsmodified+53 -15
| @@ -242,10 +242,12 @@ impl Decoder { | ||
| 242 | 242 | let mut ti = 0usize; |
| 243 | 243 | for tr in 0..tile_rows { |
| 244 | 244 | let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; |
| 245 | - let row_hi = ((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2).min(mi_rows); | |
| 245 | + let row_hi = | |
| 246 | + (((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2) * SB_MI).min(mi_rows); | |
| 246 | 247 | for tc in 0..tile_cols { |
| 247 | 248 | let col_lo = ((sb64_cols * tc) >> hdr.tile_info.cols_log2) * SB_MI; |
| 248 | - let col_hi = ((sb64_cols * (tc + 1)) >> hdr.tile_info.cols_log2).min(mi_cols); | |
| 249 | + let col_hi = | |
| 250 | + (((sb64_cols * (tc + 1)) >> hdr.tile_info.cols_log2) * SB_MI).min(mi_cols); | |
| 249 | 251 | if crate::trace_enabled() { |
| 250 | 252 | eprintln!( |
| 251 | 253 | "TILE {} bytes first={:02x?}", |
| @@ -263,12 +265,12 @@ impl Decoder { | ||
| 263 | 265 | let mut mi = MiState::new(mi_cols, mi_rows); |
| 264 | 266 | ectx.iter_mut().for_each(|p| p.above.fill(0)); |
| 265 | 267 | mi.above_seg.fill(0); |
| 266 | - for sb_row in row_lo..row_hi { | |
| 267 | - if sb_row % SB_MI == 0 { | |
| 268 | + for sb_row in (row_lo..row_hi).step_by(SB_MI) { | |
| 269 | + if sb_row == row_lo { | |
| 268 | 270 | ectx.iter_mut().for_each(|p| p.left = [0; 32]); |
| 269 | 271 | mi.left_seg = [0; 32]; |
| 270 | 272 | } |
| 271 | - for sb_col in col_lo..col_hi { | |
| 273 | + for sb_col in (col_lo..col_hi).step_by(SB_MI) { | |
| 272 | 274 | self.decode_partition( |
| 273 | 275 | &mut r, |
| 274 | 276 | &mut mi, |
| @@ -698,6 +700,11 @@ impl Decoder { | ||
| 698 | 700 | let y0 = ((mi_row * 8) >> s) + tx_row * 4; |
| 699 | 701 | let up = tx_row != 0 || mi_row > tile_row_start; |
| 700 | 702 | let lft = tx_col != 0 || mi_col > tile_col_start; |
| 703 | + // vp9_predict_intra_block `have_right`: the tx block is not in the | |
| 704 | + // last tx column of ITS OWN block (pd->n4_w units) — above-right | |
| 705 | + // past the block's right edge belongs to a not-yet-decoded | |
| 706 | + // neighbour, so it must stage as unavailable. | |
| 707 | + let rgt = tx_col + (1usize << tx_size) < bw * 2 >> s; | |
| 701 | 708 | |
| 702 | 709 | // 1. Intra prediction (reads reconstructed neighbours). |
| 703 | 710 | { |
| @@ -713,7 +720,7 @@ impl Decoder { | ||
| 713 | 720 | bs, |
| 714 | 721 | up, |
| 715 | 722 | lft, |
| 716 | - x0 + bs < pw, | |
| 723 | + rgt, | |
| 717 | 724 | ); |
| 718 | 725 | } |
| 719 | 726 | |
| @@ -739,8 +746,24 @@ impl Decoder { | ||
| 739 | 746 | let ax = ((mi_col << 1) >> s) + tx_col; |
| 740 | 747 | let lrows = 32usize >> s; |
| 741 | 748 | let ay = (((mi_row << 1) >> s) + tx_row) % lrows; |
| 742 | - let ctx_in = | |
| 743 | - usize::from(ectx[plane].above[ax] != 0) + usize::from(ectx[plane].left[ay] != 0); | |
| 749 | + // libvpx reads the context as !!*(uintN_t *)a / l over the WHOLE | |
| 750 | + // tx-block span (uint16 for 8x8, uint32 for 16x16, uint64 for | |
| 751 | + // 32x32), not just the first entry. | |
| 752 | + let nblocks = 1usize << tx_size; | |
| 753 | + let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0)) | |
| 754 | + + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0)); | |
| 755 | + if crate::trace_enabled() { | |
| 756 | + eprintln!( | |
| 757 | + "CTX p={} prob={:?} ax={} ay={} a={} l={} ctx={}", | |
| 758 | + plane, | |
| 759 | + self.ctx.coef[tx_size][ptype][0][0][ctx_in], | |
| 760 | + ax, | |
| 761 | + ay, | |
| 762 | + ectx[plane].above[ax], | |
| 763 | + ectx[plane].left[ay], | |
| 764 | + ctx_in | |
| 765 | + ); | |
| 766 | + } | |
| 744 | 767 | let coef = decode_coefs( |
| 745 | 768 | r, |
| 746 | 769 | ptype, |
| @@ -806,8 +829,21 @@ impl Decoder { | ||
| 806 | 829 | if coef.eob > 0 { |
| 807 | 830 | let dst_off = y0 * stride + x0; |
| 808 | 831 | let (data, _) = planes.plane(plane); |
| 809 | - let t = if crate::trace_enabled() && plane == 0 && mi_row == 0 && mi_col == 0 { | |
| 810 | - Some(data[0]) | |
| 832 | + let probe = crate::trace_enabled() | |
| 833 | + && plane == 0 | |
| 834 | + && std::env::var("EC_VP9_PROBE_TU") | |
| 835 | + .ok() | |
| 836 | + .and_then(|s| { | |
| 837 | + let mut it = s.split(','); | |
| 838 | + Some((it.next()?.parse::<usize>().ok()?, it.next()?.parse().ok()?)) | |
| 839 | + }) | |
| 840 | + .is_some_and(|(px, py)| x0 == px && y0 == py); | |
| 841 | + let t: Option<Vec<Vec<u8>>> = if probe { | |
| 842 | + Some( | |
| 843 | + (0..4usize) | |
| 844 | + .map(|rr| data[dst_off + rr * stride..dst_off + rr * stride + 4].to_vec()) | |
| 845 | + .collect(), | |
| 846 | + ) | |
| 811 | 847 | } else { |
| 812 | 848 | None |
| 813 | 849 | }; |
| @@ -821,11 +857,13 @@ impl Decoder { | ||
| 821 | 857 | lossless, |
| 822 | 858 | ); |
| 823 | 859 | if let Some(before) = t { |
| 824 | - let row: Vec<u8> = data[0..8].to_vec(); | |
| 825 | - eprintln!( | |
| 826 | - "RC x0={x0} y0={y0} tx={tx_size} before={before} after={} row0={row:?}", | |
| 827 | - data[0] | |
| 828 | - ); | |
| 860 | + let after: Vec<Vec<u8>> = (0..4usize) | |
| 861 | + .map(|rr| data[dst_off + rr * stride..dst_off + rr * stride + 4].to_vec()) | |
| 862 | + .collect(); | |
| 863 | + eprintln!("RC x0={x0} y0={y0} tx={tx_size} mode={mode}"); | |
| 864 | + for rr in 0..4 { | |
| 865 | + eprintln!(" pred{rr}={:?} after{rr}={:?}", before[rr], after[rr]); | |
| 866 | + } | |
| 829 | 867 | } |
| 830 | 868 | } |
| 831 | 869 | Ok(()) |
crates/ec-vp9/src/intra.rsmodified+64 -1
| @@ -145,6 +145,22 @@ pub(crate) fn build_intra_predictors( | ||
| 145 | 145 | |
| 146 | 146 | // The C's `const_above_row = above_ref` shortcut for bs==4 && |
| 147 | 147 | // right&&left stages the same bytes, so above_data is equivalent. |
| 148 | + if crate::trace_enabled() | |
| 149 | + && std::env::var("EC_VP9_PROBE_TU") | |
| 150 | + .ok() | |
| 151 | + .and_then(|s| { | |
| 152 | + let mut it = s.split(','); | |
| 153 | + Some((it.next()?.parse::<usize>().ok()?, it.next()?.parse().ok()?)) | |
| 154 | + }) | |
| 155 | + .is_some_and(|(px, py)| x0 == px && y0 == py) | |
| 156 | + { | |
| 157 | + eprintln!( | |
| 158 | + "STAGE x0={x0} y0={y0} mode={mode} bs={bs} above={:?} cor={} left={:?}", | |
| 159 | + &above_data[A..A + 8], | |
| 160 | + above_data[A - 1], | |
| 161 | + &left_col[..4] | |
| 162 | + ); | |
| 163 | + } | |
| 148 | 164 | let dst = &mut data[y0 * stride + x0..]; |
| 149 | 165 | dispatch( |
| 150 | 166 | mode, bs, dst, stride, &above_data, &left_col, up_available, left_available, |
| @@ -192,6 +208,51 @@ fn dispatch( | ||
| 192 | 208 | dst[r * stride..r * stride + bs].fill(left[r]); |
| 193 | 209 | } |
| 194 | 210 | } |
| 211 | + // The 4x4 kernels in intrapred.c are specialized implementations, | |
| 212 | + // NOT the shared `d45_predictor`/`d63_predictor`/`d153_predictor` | |
| 213 | + // INLINE loops: they continue the diagonal with the staged | |
| 214 | + // above-right samples (vpx_d45/d63_predictor_4x4_c, and d153's | |
| 215 | + // DST(2,3)). Only 8x8+ use the generic loops below. | |
| 216 | + 3 if bs == 4 => { | |
| 217 | + // vpx_d45_predictor_4x4_c | |
| 218 | + for r in 0..4usize { | |
| 219 | + for c in 0..4usize { | |
| 220 | + dst[r * stride + c] = if r + c < 6 { | |
| 221 | + avg3(ab[r + c], ab[r + c + 1], ab[r + c + 2]) | |
| 222 | + } else { | |
| 223 | + ab[7] | |
| 224 | + }; | |
| 225 | + } | |
| 226 | + } | |
| 227 | + } | |
| 228 | + 6 if bs == 4 => { | |
| 229 | + // vpx_d153_predictor_4x4_c | |
| 230 | + dst[0] = avg2(left[0], cor); | |
| 231 | + dst[stride] = avg2(left[1], left[0]); | |
| 232 | + dst[2 * stride] = avg2(left[2], left[1]); | |
| 233 | + dst[3 * stride] = avg2(left[3], left[2]); | |
| 234 | + dst[1] = avg3(left[0], cor, ab[0]); | |
| 235 | + dst[stride + 1] = avg3(left[1], left[0], cor); | |
| 236 | + dst[2 * stride + 1] = avg3(left[2], left[1], left[0]); | |
| 237 | + dst[3 * stride + 1] = avg3(left[3], left[2], left[1]); | |
| 238 | + dst[2] = avg3(cor, ab[0], ab[1]); | |
| 239 | + dst[stride + 2] = dst[0]; | |
| 240 | + dst[2 * stride + 2] = dst[stride]; | |
| 241 | + dst[3 * stride + 2] = dst[2 * stride]; | |
| 242 | + dst[3] = avg3(ab[0], ab[1], ab[2]); | |
| 243 | + dst[stride + 3] = dst[1]; | |
| 244 | + dst[2 * stride + 3] = avg3(left[1], left[0], cor); // DST(3, 2) | |
| 245 | + dst[3 * stride + 3] = avg3(left[2], left[1], left[0]); | |
| 246 | + } | |
| 247 | + 8 if bs == 4 => { | |
| 248 | + // vpx_d63_predictor_4x4_c | |
| 249 | + for c in 0..4usize { | |
| 250 | + dst[c] = avg2(ab[c], ab[c + 1]); | |
| 251 | + dst[stride + c] = avg3(ab[c], ab[c + 1], ab[c + 2]); | |
| 252 | + dst[2 * stride + c] = avg2(ab[c + 1], ab[c + 2]); | |
| 253 | + dst[3 * stride + c] = avg3(ab[c + 1], ab[c + 2], ab[c + 3]); | |
| 254 | + } | |
| 255 | + } | |
| 195 | 256 | 3 => { |
| 196 | 257 | // d45 |
| 197 | 258 | let above_right = ab[bs - 1]; |
| @@ -237,8 +298,10 @@ fn dispatch( | ||
| 237 | 298 | dst[stride + c] = avg3(above[A + c - 2], ab[c - 1], ab[c]); |
| 238 | 299 | } |
| 239 | 300 | dst[2 * stride] = avg3(cor, left[0], left[1]); |
| 301 | + // libvpx anchors this loop at row 2 (`dst` walked twice); | |
| 302 | + // our `dst` is the block base, so the index is absolute. | |
| 240 | 303 | for r in 3..bs { |
| 241 | - dst[(r - 2) * stride] = avg3(left[r - 3], left[r - 2], left[r - 1]); | |
| 304 | + dst[r * stride] = avg3(left[r - 3], left[r - 2], left[r - 1]); | |
| 242 | 305 | } |
| 243 | 306 | for r in 2..bs { |
| 244 | 307 | for c in 1..bs { |
crates/ec-vp9/src/loopfilter.rsmodified+93 -79
| @@ -26,11 +26,14 @@ fn sc(x: i32) -> i32 { | ||
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /// `filter4` kernel; `idx` = [op1, op0, oq0, oq1]. |
| 29 | +/// `vpx_dsp filter4`: updates p1/p0/q0/q1 in place. `mask` gates the | |
| 30 | +/// whole filter, `hev` the outer taps (vp9_loopfilter.c:81). | |
| 29 | 31 | fn filter4(s: &mut [u8], idx: [usize; 4], thresh: i32, mask: bool) { |
| 30 | 32 | if !mask { |
| 31 | 33 | return; |
| 32 | 34 | } |
| 33 | - let (p1, p0, q0, q1) = (s[idx[0]], s[idx[1]], s[idx[2]], s[idx[3]]); | |
| 35 | + let (op1, op0, oq0, oq1) = (idx[0], idx[1], idx[2], idx[3]); | |
| 36 | + let (p1, p0, q0, q1) = (s[op1], s[op0], s[oq0], s[oq1]); | |
| 34 | 37 | let hev = (p1 as i32 - p0 as i32).abs() > thresh || (q1 as i32 - q0 as i32).abs() > thresh; |
| 35 | 38 | let (ps1, ps0, qs0, qs1) = ( |
| 36 | 39 | (p1 ^ 0x80) as i8 as i32, |
| @@ -39,60 +42,60 @@ fn filter4(s: &mut [u8], idx: [usize; 4], thresh: i32, mask: bool) { | ||
| 39 | 42 | (q1 ^ 0x80) as i8 as i32, |
| 40 | 43 | ); |
| 41 | 44 | let mut filter = sc(ps1 - qs1); |
| 42 | - if !hev { | |
| 45 | + if hev { | |
| 46 | + // add outer taps if we have high edge variance (& hev mask) | |
| 47 | + } else { | |
| 43 | 48 | filter = 0; |
| 44 | 49 | } |
| 50 | + // inner taps | |
| 45 | 51 | filter = sc(filter + 3 * (qs0 - ps0)); |
| 52 | + // round one side +4 and the other +3 | |
| 46 | 53 | let filter1 = sc(filter + 4) >> 3; |
| 47 | 54 | let filter2 = sc(filter + 3) >> 3; |
| 48 | - s[idx[3]] = (sc(qs0 - filter1) ^ 0x80) as u8; | |
| 49 | - s[idx[2]] = (sc(ps0 + filter2) ^ 0x80) as u8; | |
| 50 | - let mut f = (filter1 + 1) >> 1; | |
| 51 | - if hev { | |
| 52 | - f = 0; | |
| 53 | - } | |
| 54 | - s[idx[1]] = (sc(ps1 + f) ^ 0x80) as u8; | |
| 55 | - s[idx[0]] = (sc(qs1 - f) ^ 0x80) as u8; | |
| 55 | + s[oq0] = (sc(qs0 - filter1) ^ 0x80) as u8; | |
| 56 | + s[op0] = (sc(ps0 + filter2) ^ 0x80) as u8; | |
| 57 | + // outer tap adjustments (& ~hev) | |
| 58 | + let f = if hev { 0 } else { (filter1 + 1) >> 1 }; | |
| 59 | + s[oq1] = (sc(qs1 - f) ^ 0x80) as u8; | |
| 60 | + s[op1] = (sc(ps1 + f) ^ 0x80) as u8; | |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | -/// The tapped edge filter: `dir` 0 = horizontal edges (rows step by | |
| 59 | -/// stride), 1 = vertical (columns step by 1). `taps` in {4, 8, 16}. | |
| 60 | -fn lpf_edge(data: &mut [u8], off: usize, stride: usize, dir: u8, taps: u8, blimit: i32, limit: i32) { | |
| 63 | +fn lpf_edge( | |
| 64 | + data: &mut [u8], | |
| 65 | + off: usize, | |
| 66 | + stride: usize, | |
| 67 | + dir: u8, | |
| 68 | + taps: u8, | |
| 69 | + blimit: i32, | |
| 70 | + limit: i32, | |
| 71 | + thresh: i32, | |
| 72 | +) { | |
| 73 | + // `dir` 0 = horizontal edges (rows step by stride), 1 = vertical | |
| 74 | + // (columns step by 1). `taps` in {4, 8, 16}. | |
| 61 | 75 | let step: isize = if dir == 0 { stride as isize } else { 1 }; |
| 62 | 76 | let edge = off as isize; |
| 63 | - if std::env::var_os("EC_VP9_DBG").is_some() { | |
| 64 | - let need = if taps == 16 { 8 } else if taps == 8 { 4 } else { 2 }; | |
| 65 | - let lo = edge - need * step; | |
| 66 | - let hi = edge + need * step; | |
| 67 | - if lo < 0 || hi >= data.len() as isize { | |
| 68 | - eprintln!("DBG lpf OOB off={off} dir={dir} taps={taps} len={}", data.len()); | |
| 69 | - } | |
| 70 | - } | |
| 71 | 77 | macro_rules! rd { ($k:expr) => { data[(edge + $k * step) as usize] as i32 } } |
| 72 | 78 | macro_rules! put { ($k:expr, $v:expr) => { data[(edge + $k * step) as usize] = $v; } } |
| 73 | - // filter_mask (loopfilter.c:30): p3..q3 gradient limits + blimit. | |
| 74 | - let mask8 = (rd!(-4) - rd!(-3)).abs() <= limit | |
| 79 | + // filter_mask (vpx_dsp/loopfilter.c:34): p3..q3 gradient limits + | |
| 80 | + // blimit; shared by the 4-, 8- and 16-tap entry points. | |
| 81 | + let mask = (rd!(-4) - rd!(-3)).abs() <= limit | |
| 75 | 82 | && (rd!(-3) - rd!(-2)).abs() <= limit |
| 76 | 83 | && (rd!(-2) - rd!(-1)).abs() <= limit |
| 77 | 84 | && (rd!(1) - rd!(0)).abs() <= limit |
| 78 | 85 | && (rd!(2) - rd!(1)).abs() <= limit |
| 79 | 86 | && (rd!(3) - rd!(2)).abs() <= limit |
| 80 | 87 | && 2 * (rd!(-1) - rd!(0)).abs() + (rd!(-2) - rd!(1)).abs() / 2 <= blimit; |
| 88 | + let idx4 = [ | |
| 89 | + (edge - 2 * step) as usize, | |
| 90 | + (edge - step) as usize, | |
| 91 | + edge as usize, | |
| 92 | + (edge + step) as usize, | |
| 93 | + ]; | |
| 81 | 94 | if taps == 4 { |
| 82 | - // lpf4 uses the p1/p0/q0/q1 form of the mask. | |
| 83 | - let mask = (rd!(-1) - rd!(0)).abs() <= limit | |
| 84 | - && (rd!(1) - rd!(0)).abs() <= limit | |
| 85 | - && 2 * (rd!(-1) - rd!(0)).abs() + (rd!(-2) - rd!(1)).abs() / 2 <= blimit; | |
| 86 | - let idx = [ | |
| 87 | - (edge - 2 * step) as usize, | |
| 88 | - (edge - step) as usize, | |
| 89 | - edge as usize, | |
| 90 | - (edge + step) as usize, | |
| 91 | - ]; | |
| 92 | - filter4(data, idx, limit, mask); | |
| 95 | + filter4(data, idx4, thresh, mask); | |
| 93 | 96 | return; |
| 94 | 97 | } |
| 95 | - if !mask8 { | |
| 98 | + if !mask { | |
| 96 | 99 | return; |
| 97 | 100 | } |
| 98 | 101 | // flat_mask4(1, p3..q3) |
| @@ -104,13 +107,8 @@ fn lpf_edge(data: &mut [u8], off: usize, stride: usize, dir: u8, taps: u8, blimi | ||
| 104 | 107 | && (rd!(3) - rd!(0)).abs() <= 1; |
| 105 | 108 | if taps == 8 || !flat { |
| 106 | 109 | if !flat { |
| 107 | - let idx = [ | |
| 108 | - (edge - 2 * step) as usize, | |
| 109 | - (edge - step) as usize, | |
| 110 | - edge as usize, | |
| 111 | - (edge + step) as usize, | |
| 112 | - ]; | |
| 113 | - filter4(data, idx, limit, true); | |
| 110 | + // plain filter4 with the real mask and hev threshold | |
| 111 | + filter4(data, idx4, thresh, mask); | |
| 114 | 112 | return; |
| 115 | 113 | } |
| 116 | 114 | // 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 | ||
| 124 | 122 | put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8); |
| 125 | 123 | return; |
| 126 | 124 | } |
| 127 | - // 16-tap: flat2 via flat_mask5(1, p7..q7) | |
| 128 | - let flat2 = flat | |
| 129 | - && (rd!(-8) - rd!(-1)).abs() <= 1 | |
| 130 | - && (rd!(8) - rd!(0)).abs() <= 1 | |
| 125 | + // 16-tap: flat2 = flat_mask5(1, p7..p4, p0, q0, q4..q7) — every | |
| 126 | + // sample compared against p0 (rd!(-1)). | |
| 127 | + let flat2 = (rd!(-8) - rd!(-1)).abs() <= 1 | |
| 131 | 128 | && (rd!(-7) - rd!(-1)).abs() <= 1 |
| 132 | - && (rd!(7) - rd!(0)).abs() <= 1 | |
| 133 | 129 | && (rd!(-6) - rd!(-1)).abs() <= 1 |
| 134 | - && (rd!(6) - rd!(0)).abs() <= 1 | |
| 135 | 130 | && (rd!(-5) - rd!(-1)).abs() <= 1 |
| 136 | - && (rd!(5) - rd!(0)).abs() <= 1; | |
| 131 | + && (rd!(0) - rd!(-1)).abs() <= 1 | |
| 132 | + && (rd!(4) - rd!(-1)).abs() <= 1 | |
| 133 | + && (rd!(5) - rd!(-1)).abs() <= 1 | |
| 134 | + && (rd!(6) - rd!(-1)).abs() <= 1 | |
| 135 | + && (rd!(7) - rd!(-1)).abs() <= 1; | |
| 137 | 136 | if !flat2 { |
| 138 | 137 | let (p3, p2, p1, p0, q0, q1, q2, q3) = |
| 139 | 138 | (rd!(-4), rd!(-3), rd!(-2), rd!(-1), rd!(0), rd!(1), rd!(2), rd!(3)); |
| @@ -180,8 +179,9 @@ pub(crate) struct LfGrids { | ||
| 180 | 179 | pub mi_rows: usize, |
| 181 | 180 | } |
| 182 | 181 | |
| 183 | -/// limits for a level (spec 8.8.1, `update_sharpness`). | |
| 184 | -fn limits(level: u8, sharpness: u8) -> (i32, i32) { | |
| 182 | +/// limits for a level (spec 8.8.1, `update_sharpness` + | |
| 183 | +/// `vp9_loop_filter_init`): (mblim, lim, hev_thr). | |
| 184 | +fn limits(level: u8, sharpness: u8) -> (i32, i32, i32) { | |
| 185 | 185 | let mut bil = level as i32 >> ((usize::from(sharpness > 0)) + (usize::from(sharpness > 4))); |
| 186 | 186 | if sharpness > 0 && bil > 9 - sharpness as i32 { |
| 187 | 187 | bil = 9 - sharpness as i32; |
| @@ -189,7 +189,7 @@ fn limits(level: u8, sharpness: u8) -> (i32, i32) { | ||
| 189 | 189 | if bil < 1 { |
| 190 | 190 | bil = 1; |
| 191 | 191 | } |
| 192 | - (2 * (level as i32 + 2) + bil, bil) | |
| 192 | + (2 * (level as i32 + 2) + bil, bil, level as i32 >> 4) | |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | impl LfGrids { |
| @@ -236,10 +236,10 @@ impl LfGrids { | ||
| 236 | 236 | if taps == 16 && (c4 * 4 < 8 || c4 * 4 + 8 > y_w) { |
| 237 | 237 | taps = 8; |
| 238 | 238 | } |
| 239 | - let (bl, li) = limits(level, sharpness); | |
| 239 | + let (bl, li, th) = limits(level, sharpness); | |
| 240 | 240 | let off = r4 * 4 * stride + c4 * 4; |
| 241 | 241 | if y_w >= c4 * 4 + 4 { |
| 242 | - lpf_edge(y, off, stride, 1, taps, bl, li); | |
| 242 | + lpf_edge(y, off, stride, 1, taps, bl, li, th); | |
| 243 | 243 | } |
| 244 | 244 | } |
| 245 | 245 | } |
| @@ -268,47 +268,61 @@ impl LfGrids { | ||
| 268 | 268 | if taps == 16 && (r4 * 4 < 8 || r4 * 4 + 8 > y_h) { |
| 269 | 269 | taps = 8; |
| 270 | 270 | } |
| 271 | - let (bl, li) = limits(level, sharpness); | |
| 271 | + let (bl, li, th) = limits(level, sharpness); | |
| 272 | 272 | let off = r4 * 4 * stride + c4 * 4; |
| 273 | 273 | if y_h >= r4 * 4 + 4 { |
| 274 | - lpf_edge(y, off, stride, 0, taps, bl, li); | |
| 274 | + lpf_edge(y, off, stride, 0, taps, bl, li, th); | |
| 275 | 275 | } |
| 276 | 276 | } |
| 277 | 277 | } |
| 278 | - // Chroma (uv 4x4 grid = mi grid). | |
| 278 | + // Chroma (4:2:0): one 4x4 chroma TU per MI, so the only filtered | |
| 279 | + // edges are the MI boundaries (vp9_filter_block_plane_ss11): | |
| 280 | + // vertical on every MI column boundary, horizontal on every MI | |
| 281 | + // row boundary, taps from the plane's uv transform size. | |
| 279 | 282 | for data in [&mut *u, &mut *v] { |
| 283 | + // Vertical: left edge of each MI (except the leftmost column). | |
| 280 | 284 | for r in 0..self.mi_rows { |
| 281 | 285 | for c in 1..self.mi_cols { |
| 282 | 286 | let cell = r * self.mi_cols + c; |
| 283 | - let (bsize, _bor, boc) = self.blk[cell]; | |
| 287 | + let (bsize, _bor, _boc) = self.blk[cell]; | |
| 284 | 288 | let level = self.level8[cell]; |
| 285 | 289 | if level == 0 { |
| 286 | 290 | continue; |
| 287 | 291 | } |
| 288 | 292 | let uv_tx = uv_txsize_lookup(bsize as usize, self.otx[cell] as usize); |
| 289 | - let taps = if bsize as usize >= 6 { | |
| 290 | - if c == boc as usize { | |
| 291 | - match uv_tx { | |
| 292 | - TX_16X16 if c * 8 >= 8 && c * 8 + 8 <= uv_w => 16, | |
| 293 | - TX_16X16 => 8, | |
| 294 | - TX_8X8 => 8, | |
| 295 | - _ if c % 4 == 0 => 8, | |
| 296 | - _ => 4, | |
| 297 | - } | |
| 298 | - } else if uv_tx == TX_4X4 { | |
| 299 | - 4 | |
| 300 | - } else { | |
| 301 | - continue; | |
| 302 | - } | |
| 303 | - } else if c % 2 == 0 && r % 2 == 0 { | |
| 304 | - if c % 4 == 0 { 8 } else { 4 } | |
| 305 | - } else { | |
| 293 | + let taps = match uv_tx { | |
| 294 | + TX_16X16 if c * 4 >= 8 && c * 4 + 8 <= uv_w => 16, | |
| 295 | + TX_16X16 => 8, | |
| 296 | + TX_8X8 => 8, | |
| 297 | + _ => 4, | |
| 298 | + }; | |
| 299 | + let (bl, li, th) = limits(level, sharpness); | |
| 300 | + let off = r * 4 * uv_stride + c * 4; | |
| 301 | + if uv_w >= c * 4 + 4 { | |
| 302 | + lpf_edge(data, off, uv_stride, 1, taps, bl, li, th); | |
| 303 | + } | |
| 304 | + } | |
| 305 | + } | |
| 306 | + // Horizontal: top edge of each MI (except the top row). | |
| 307 | + for r in 1..self.mi_rows { | |
| 308 | + for c in 0..self.mi_cols { | |
| 309 | + let cell = r * self.mi_cols + c; | |
| 310 | + let (bsize, _bor, _boc) = self.blk[cell]; | |
| 311 | + let level = self.level8[cell]; | |
| 312 | + if level == 0 { | |
| 306 | 313 | continue; |
| 314 | + } | |
| 315 | + let uv_tx = uv_txsize_lookup(bsize as usize, self.otx[cell] as usize); | |
| 316 | + let taps = match uv_tx { | |
| 317 | + TX_16X16 if r * 4 >= 8 && r * 4 + 8 <= uv_h => 16, | |
| 318 | + TX_16X16 => 8, | |
| 319 | + TX_8X8 => 8, | |
| 320 | + _ => 4, | |
| 307 | 321 | }; |
| 308 | - let (bl, li) = limits(level, sharpness); | |
| 309 | - let off = r * 8 * uv_stride + c * 8; | |
| 310 | - if uv_w >= c * 8 + 8 { | |
| 311 | - lpf_edge(data, off, uv_stride, 1, taps, bl, li); | |
| 322 | + let (bl, li, th) = limits(level, sharpness); | |
| 323 | + let off = r * 4 * uv_stride + c * 4; | |
| 324 | + if uv_h >= r * 4 + 4 { | |
| 325 | + lpf_edge(data, off, uv_stride, 0, taps, bl, li, th); | |
| 312 | 326 | } |
| 313 | 327 | } |
| 314 | 328 | } |
crates/ec-vp9/src/modes.rsmodified+7 -6
| @@ -225,29 +225,30 @@ pub(crate) fn read_intra_frame_mode_info( | ||
| 225 | 225 | mi.mode = mi.bmi[3]; |
| 226 | 226 | } |
| 227 | 227 | 1 => { |
| 228 | - // BLOCK_4X8 | |
| 228 | + // BLOCK_4X8; decodemv.c commits bmi[0]/bmi[2] BEFORE the | |
| 229 | + // second mode's probs are derived from the current bmi. | |
| 229 | 230 | let a0 = above_block_mode(&mi.bmi, above_pair, 0); |
| 230 | 231 | let l0 = left_block_mode(&mi.bmi, left_pair, 0); |
| 231 | 232 | let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0)); |
| 233 | + mi.bmi[0] = m0; | |
| 234 | + mi.bmi[2] = m0; | |
| 232 | 235 | let a1 = above_block_mode(&mi.bmi, above_pair, 1); |
| 233 | 236 | let l1 = left_block_mode(&mi.bmi, left_pair, 1); |
| 234 | 237 | let m1 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a1, l1)); |
| 235 | - mi.bmi[0] = m0; | |
| 236 | - mi.bmi[2] = m0; | |
| 237 | 238 | mi.bmi[1] = m1; |
| 238 | 239 | mi.bmi[3] = m1; |
| 239 | 240 | mi.mode = m1; |
| 240 | 241 | } |
| 241 | 242 | 2 => { |
| 242 | - // BLOCK_8X4 | |
| 243 | + // BLOCK_8X4; same commit-before-read ordering. | |
| 243 | 244 | let a0 = above_block_mode(&mi.bmi, above_pair, 0); |
| 244 | 245 | let l0 = left_block_mode(&mi.bmi, left_pair, 0); |
| 245 | 246 | let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0)); |
| 247 | + mi.bmi[0] = m0; | |
| 248 | + mi.bmi[1] = m0; | |
| 246 | 249 | let a2 = above_block_mode(&mi.bmi, above_pair, 2); |
| 247 | 250 | let l2 = left_block_mode(&mi.bmi, left_pair, 2); |
| 248 | 251 | let m2 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a2, l2)); |
| 249 | - mi.bmi[0] = m0; | |
| 250 | - mi.bmi[1] = m0; | |
| 251 | 252 | mi.bmi[2] = m2; |
| 252 | 253 | mi.bmi[3] = m2; |
| 253 | 254 | mi.mode = m2; |
crates/ec-vp9/tests/keyframe_exact.rsmodified+23 -0
| @@ -93,6 +93,29 @@ fn keyframes_match_ffmpeg() { | ||
| 93 | 93 | compare(&path); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | +#[test] | |
| 97 | +fn lossless_64_matches_ffmpeg() { | |
| 98 | + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); | |
| 99 | + let path = dir.join("fixtures/vp9/lossless-64.ivf"); | |
| 100 | + let bytes = std::fs::read(&path).unwrap(); | |
| 101 | + let (_fourcc, w, h, frames) = ivf::parse_ivf(&bytes); | |
| 102 | + let reference = ffmpeg_raw_yuv(&path); | |
| 103 | + let mut decoder = Decoder::new(); | |
| 104 | + let pic = decoder | |
| 105 | + .decode(&frames[0].data) | |
| 106 | + .expect("decode must succeed") | |
| 107 | + .expect("frame is shown"); | |
| 108 | + let (w, h) = (w as usize, h as usize); | |
| 109 | + let uv = (w / 2) * (h / 2); | |
| 110 | + let want_y = &reference[..w * h]; | |
| 111 | + let want_u = &reference[w * h..w * h + uv]; | |
| 112 | + let want_v = &reference[w * h + uv..]; | |
| 113 | + assert!(pic.y.iter().eq(want_y.iter()), "lossless Y must be byte-exact"); | |
| 114 | + assert!(pic.u.iter().eq(want_u.iter()), "lossless U must be byte-exact"); | |
| 115 | + assert!(pic.v.iter().eq(want_v.iter()), "lossless V must be byte-exact"); | |
| 116 | + assert_eq!((pic.width, pic.height), (w as u16, h as u16)); | |
| 117 | +} | |
| 118 | + | |
| 96 | 119 | #[test] |
| 97 | 120 | fn inter_is_named_unsupported() { |
| 98 | 121 | let Some(dir) = ivf::fixture_dir() else { return }; |
lanes/vp9kf.report.mdmodified+49 -0
| @@ -189,3 +189,52 @@ ours with a TOK-style trace in tokens.rs decode_coefs (oracle TOK print is in | ||
| 189 | 189 | /tmp/vp9loss/libvpx-src/vp9/decoder/vp9_detokenize.c decode_coefs loop top); |
| 190 | 190 | (2) re-check `lossless_64_matches_ffmpeg` after; keep scratch files until green. |
| 191 | 191 | Oracle: /tmp/vp9loss/{libvpx-src,drv} (see above; TOK c/band/ctx/p0 print per symbol). |
| 192 | + | |
| 193 | +## HANDOFF #4 (Vp9Sub8, 2026-09-14) — lossless 4096/4096 GREEN; key-320 decode matches, loop-filter 4x4-int edge remains | |
| 194 | + | |
| 195 | +1. **Sub8x8 second y-mode ctx (FIXED, ec-vp9/src/modes.rs).** decodemv.c | |
| 196 | + commits bmi[0]/bmi[2] (4X8) or bmi[0]/bmi[1] (8X4) BEFORE deriving the | |
| 197 | + second mode's probs from the current bmi; we read both modes first, so | |
| 198 | + the second mode always saw DC/DC. | |
| 199 | +2. **D117 first-column anchor (FIXED, ec-vp9/src/intra.rs).** libvpx's | |
| 200 | + loop is anchored at row 2 (its `dst` walks twice); our base-relative | |
| 201 | + `dst[(r-2)*stride]` wrote row 1 instead of row r. | |
| 202 | +3. **Specialized 4x4 predictors (FIXED, intra.rs).** intrapred.c's | |
| 203 | + vpx_d45/d63/d153_predictor_4x4_c are NOT the shared INLINE loops: they | |
| 204 | + continue the diagonal with the staged above-right (d153 also differs | |
| 205 | + at DST(3,2)). d117/d135/d207 4x4 equal the generic loops; d45/d63/ | |
| 206 | + d153 do not. | |
| 207 | +4. **`have_right` decode-order semantics (FIXED, decode.rs).** | |
| 208 | + vp9_predict_intra_block's have_right = (aoff + txw) < n4_w — not a | |
| 209 | + frame-geometry test. Above-right past the block's own edge reads | |
| 210 | + not-yet-decoded neighbours and must stage as unavailable. | |
| 211 | +5. **Entropy-ctx span read (FIXED, decode.rs).** vp9_decode_block_tokens | |
| 212 | + computes ctx as !!*(uintN_t *)a over the WHOLE tx span; we read only | |
| 213 | + above[ax]/left[ay]. Wrong ctx on TX_8X8+ desynced key-320 tokens. | |
| 214 | +6. **Tile SB walk units (FIXED, decode.rs).** row_hi/col_hi are SB-unit | |
| 215 | + boundaries but the loop stepped per-MI — for 320x240 the 64x64 | |
| 216 | + decode_partition ran at every MI (overlapping re-decodes); lossless-64 | |
| 217 | + only survived because its single-tile boundary collapses to 1. This | |
| 218 | + was the altref f0 "tile bool decoder desync" class. Loop now steps by | |
| 219 | + SB_MI and clamps to mi_rows/mi_cols. | |
| 220 | +7. **Loop filter rewrite (loopfilter.rs).** filter4 slot mapping was | |
| 221 | + scrambled (p0/q0 updates written into q1/p1), lpf4 used a VP8-style | |
| 222 | + mask instead of the shared p3..q3 filter_mask, the lpf8 non-flat | |
| 223 | + fallback passed mask=true with limit as thresh, flat2 compared the q | |
| 224 | + side against q0 instead of p0, hev_thr (lvl>>4) was missing, and the | |
| 225 | + chroma pass used an 8-px MI stride with no horizontal edges (OOB on | |
| 226 | + 320x240). All re-derived from vpx_dsp/loopfilter.c + | |
| 227 | + vp9_loopfilter.c. | |
| 228 | + | |
| 229 | +**State at yield:** lossless-64 4096/4096 vs ffmpeg (Y/U/V byte-exact; | |
| 230 | +`lossless_64_matches_ffmpeg` added). key-320: tokens + prediction match | |
| 231 | +the oracle (inter_is_named_unsupported PASSES — the desync is gone); | |
| 232 | +keyframes_match_ffmpeg first diff (7,1) ours 75 ref 76 — remaining class | |
| 233 | +is the luma 4x4-internal-edge filter (libvpx filter_selectively_* | |
| 234 | +mask_4x4_int half-position variant), NOT tokens or prediction. Next: | |
| 235 | +(1) port mask_4x4_int shifted filtering (and the frame bottom/right tap | |
| 236 | +rules), then re-run keyframes_match_ffmpeg; (2) after green remove | |
| 237 | +EC_VP9_FORCE_UH/HSZ, scratch_* tests, and the EC_VP9_PROBE_TU/STAGE/CTX | |
| 238 | +debug prints in decode.rs/intra.rs. | |
| 239 | +Oracle: /tmp/vp9loss/{libvpx-src,drv}; /tmp/vp9loss/d45probe2.c proves | |
| 240 | +SIMD==C for the specialized 4x4 kernels. |