forge
Explore
Log in

Don't you dare go Hollow

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 commit

7ed859cf1d5f147fe4e9f6af85743f334c2d9736Ahmet Kaan GümüşParent: 76c8a77
crates/ec-vp9/src/decode.rsmodified+53 -15
@@ -242,10 +242,12 @@ impl Decoder {
242242 let mut ti = 0usize;
243243 for tr in 0..tile_rows {
244244 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);
246247 for tc in 0..tile_cols {
247248 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);
249251 if crate::trace_enabled() {
250252 eprintln!(
251253 "TILE {} bytes first={:02x?}",
@@ -263,12 +265,12 @@ impl Decoder {
263265 let mut mi = MiState::new(mi_cols, mi_rows);
264266 ectx.iter_mut().for_each(|p| p.above.fill(0));
265267 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 {
268270 ectx.iter_mut().for_each(|p| p.left = [0; 32]);
269271 mi.left_seg = [0; 32];
270272 }
271- for sb_col in col_lo..col_hi {
273+ for sb_col in (col_lo..col_hi).step_by(SB_MI) {
272274 self.decode_partition(
273275 &mut r,
274276 &mut mi,
@@ -698,6 +700,11 @@ impl Decoder {
698700 let y0 = ((mi_row * 8) >> s) + tx_row * 4;
699701 let up = tx_row != 0 || mi_row > tile_row_start;
700702 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;
701708
702709 // 1. Intra prediction (reads reconstructed neighbours).
703710 {
@@ -713,7 +720,7 @@ impl Decoder {
713720 bs,
714721 up,
715722 lft,
716- x0 + bs < pw,
723+ rgt,
717724 );
718725 }
719726
@@ -739,8 +746,24 @@ impl Decoder {
739746 let ax = ((mi_col << 1) >> s) + tx_col;
740747 let lrows = 32usize >> s;
741748 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+ }
744767 let coef = decode_coefs(
745768 r,
746769 ptype,
@@ -806,8 +829,21 @@ impl Decoder {
806829 if coef.eob > 0 {
807830 let dst_off = y0 * stride + x0;
808831 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+ )
811847 } else {
812848 None
813849 };
@@ -821,11 +857,13 @@ impl Decoder {
821857 lossless,
822858 );
823859 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+ }
829867 }
830868 }
831869 Ok(())
crates/ec-vp9/src/intra.rsmodified+64 -1
@@ -145,6 +145,22 @@ pub(crate) fn build_intra_predictors(
145145
146146 // The C's `const_above_row = above_ref` shortcut for bs==4 &&
147147 // 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+ }
148164 let dst = &mut data[y0 * stride + x0..];
149165 dispatch(
150166 mode, bs, dst, stride, &above_data, &left_col, up_available, left_available,
@@ -192,6 +208,51 @@ fn dispatch(
192208 dst[r * stride..r * stride + bs].fill(left[r]);
193209 }
194210 }
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+ }
195256 3 => {
196257 // d45
197258 let above_right = ab[bs - 1];
@@ -237,8 +298,10 @@ fn dispatch(
237298 dst[stride + c] = avg3(above[A + c - 2], ab[c - 1], ab[c]);
238299 }
239300 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.
240303 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]);
242305 }
243306 for r in 2..bs {
244307 for c in 1..bs {
crates/ec-vp9/src/loopfilter.rsmodified+93 -79
@@ -26,11 +26,14 @@ fn sc(x: i32) -> i32 {
2626 }
2727
2828 /// `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).
2931 fn filter4(s: &mut [u8], idx: [usize; 4], thresh: i32, mask: bool) {
3032 if !mask {
3133 return;
3234 }
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]);
3437 let hev = (p1 as i32 - p0 as i32).abs() > thresh || (q1 as i32 - q0 as i32).abs() > thresh;
3538 let (ps1, ps0, qs0, qs1) = (
3639 (p1 ^ 0x80) as i8 as i32,
@@ -39,60 +42,60 @@ fn filter4(s: &mut [u8], idx: [usize; 4], thresh: i32, mask: bool) {
3942 (q1 ^ 0x80) as i8 as i32,
4043 );
4144 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 {
4348 filter = 0;
4449 }
50+ // inner taps
4551 filter = sc(filter + 3 * (qs0 - ps0));
52+ // round one side +4 and the other +3
4653 let filter1 = sc(filter + 4) >> 3;
4754 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;
5661 }
5762
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}.
6175 let step: isize = if dir == 0 { stride as isize } else { 1 };
6276 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- }
7177 macro_rules! rd { ($k:expr) => { data[(edge + $k * step) as usize] as i32 } }
7278 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
7582 && (rd!(-3) - rd!(-2)).abs() <= limit
7683 && (rd!(-2) - rd!(-1)).abs() <= limit
7784 && (rd!(1) - rd!(0)).abs() <= limit
7885 && (rd!(2) - rd!(1)).abs() <= limit
7986 && (rd!(3) - rd!(2)).abs() <= limit
8087 && 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+ ];
8194 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);
9396 return;
9497 }
95- if !mask8 {
98+ if !mask {
9699 return;
97100 }
98101 // flat_mask4(1, p3..q3)
@@ -104,13 +107,8 @@ fn lpf_edge(data: &mut [u8], off: usize, stride: usize, dir: u8, taps: u8, blimi
104107 && (rd!(3) - rd!(0)).abs() <= 1;
105108 if taps == 8 || !flat {
106109 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);
114112 return;
115113 }
116114 // 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
124122 put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8);
125123 return;
126124 }
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
131128 && (rd!(-7) - rd!(-1)).abs() <= 1
132- && (rd!(7) - rd!(0)).abs() <= 1
133129 && (rd!(-6) - rd!(-1)).abs() <= 1
134- && (rd!(6) - rd!(0)).abs() <= 1
135130 && (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;
137136 if !flat2 {
138137 let (p3, p2, p1, p0, q0, q1, q2, q3) =
139138 (rd!(-4), rd!(-3), rd!(-2), rd!(-1), rd!(0), rd!(1), rd!(2), rd!(3));
@@ -180,8 +179,9 @@ pub(crate) struct LfGrids {
180179 pub mi_rows: usize,
181180 }
182181
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) {
185185 let mut bil = level as i32 >> ((usize::from(sharpness > 0)) + (usize::from(sharpness > 4)));
186186 if sharpness > 0 && bil > 9 - sharpness as i32 {
187187 bil = 9 - sharpness as i32;
@@ -189,7 +189,7 @@ fn limits(level: u8, sharpness: u8) -> (i32, i32) {
189189 if bil < 1 {
190190 bil = 1;
191191 }
192- (2 * (level as i32 + 2) + bil, bil)
192+ (2 * (level as i32 + 2) + bil, bil, level as i32 >> 4)
193193 }
194194
195195 impl LfGrids {
@@ -236,10 +236,10 @@ impl LfGrids {
236236 if taps == 16 && (c4 * 4 < 8 || c4 * 4 + 8 > y_w) {
237237 taps = 8;
238238 }
239- let (bl, li) = limits(level, sharpness);
239+ let (bl, li, th) = limits(level, sharpness);
240240 let off = r4 * 4 * stride + c4 * 4;
241241 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);
243243 }
244244 }
245245 }
@@ -268,47 +268,61 @@ impl LfGrids {
268268 if taps == 16 && (r4 * 4 < 8 || r4 * 4 + 8 > y_h) {
269269 taps = 8;
270270 }
271- let (bl, li) = limits(level, sharpness);
271+ let (bl, li, th) = limits(level, sharpness);
272272 let off = r4 * 4 * stride + c4 * 4;
273273 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);
275275 }
276276 }
277277 }
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.
279282 for data in [&mut *u, &mut *v] {
283+ // Vertical: left edge of each MI (except the leftmost column).
280284 for r in 0..self.mi_rows {
281285 for c in 1..self.mi_cols {
282286 let cell = r * self.mi_cols + c;
283- let (bsize, _bor, boc) = self.blk[cell];
287+ let (bsize, _bor, _boc) = self.blk[cell];
284288 let level = self.level8[cell];
285289 if level == 0 {
286290 continue;
287291 }
288292 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 {
306313 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,
307321 };
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);
312326 }
313327 }
314328 }
crates/ec-vp9/src/modes.rsmodified+7 -6
@@ -225,29 +225,30 @@ pub(crate) fn read_intra_frame_mode_info(
225225 mi.mode = mi.bmi[3];
226226 }
227227 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.
229230 let a0 = above_block_mode(&mi.bmi, above_pair, 0);
230231 let l0 = left_block_mode(&mi.bmi, left_pair, 0);
231232 let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0));
233+ mi.bmi[0] = m0;
234+ mi.bmi[2] = m0;
232235 let a1 = above_block_mode(&mi.bmi, above_pair, 1);
233236 let l1 = left_block_mode(&mi.bmi, left_pair, 1);
234237 let m1 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a1, l1));
235- mi.bmi[0] = m0;
236- mi.bmi[2] = m0;
237238 mi.bmi[1] = m1;
238239 mi.bmi[3] = m1;
239240 mi.mode = m1;
240241 }
241242 2 => {
242- // BLOCK_8X4
243+ // BLOCK_8X4; same commit-before-read ordering.
243244 let a0 = above_block_mode(&mi.bmi, above_pair, 0);
244245 let l0 = left_block_mode(&mi.bmi, left_pair, 0);
245246 let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0));
247+ mi.bmi[0] = m0;
248+ mi.bmi[1] = m0;
246249 let a2 = above_block_mode(&mi.bmi, above_pair, 2);
247250 let l2 = left_block_mode(&mi.bmi, left_pair, 2);
248251 let m2 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a2, l2));
249- mi.bmi[0] = m0;
250- mi.bmi[1] = m0;
251252 mi.bmi[2] = m2;
252253 mi.bmi[3] = m2;
253254 mi.mode = m2;
crates/ec-vp9/tests/keyframe_exact.rsmodified+23 -0
@@ -93,6 +93,29 @@ fn keyframes_match_ffmpeg() {
9393 compare(&path);
9494 }
9595
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+
96119 #[test]
97120 fn inter_is_named_unsupported() {
98121 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
189189 /tmp/vp9loss/libvpx-src/vp9/decoder/vp9_detokenize.c decode_coefs loop top);
190190 (2) re-check `lossless_64_matches_ffmpeg` after; keep scratch files until green.
191191 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.