From 8b5425d6ae2e67b1bbc61f6756549d35c9ea20b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 20:50:37 +0300 Subject: [PATCH] fix(av1): a non-chroma-reference inter block wiped its neighbours' chroma coefficient contexts libaom codes no chroma transform at all for a block that is not the chroma reference of its 4:2:0 pair (`av1_reset_entropy_context`: `nplanes = 1 + (num_planes - 1) * xd->is_chroma_ref`), so such a block leaves the above/left CHROMA entropy contexts exactly as it found them. Our inter block reader recorded all three planes unconditionally, and for those strips the chroma grids are all-zero -- so the even 4x16 strip of a VERT_4 pair cleared the real left-neighbour chroma state, and the PAIR's own `all_zero` symbol then read `txb_skip_ctx` one row low (base 1 where libaom gathers 2, i.e. ctx 1 vs 9 with the chroma offset). Measured on the 2-frame cut of the 10-bit 1920x792 128-superblock stream, decode-order frame 1, mi(40,220): the chroma `all_zero` entry range 37582 now leaves 51992, aomdec's value. The whole cut's coefficient ladder is exact: 15865 `all_zero` symbols in both decoders, identical value and range in order, both frames. The frame-1 reconstruction gap drops from 1924240 to 114374 differing bytes and is now pre-loop-filter (an MC/prediction defect, no longer entropy). Also: `EC_DCDUMP` now dumps all three planes, not plane 0 only -- without the chroma cells the wipe is invisible. --- crates/ec-av1/src/decode.rs | 44 +++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 61a07bd3..74e8f70a 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -5401,14 +5401,14 @@ impl Neighbours { left_coded |= left.level != 0; vote += dc_vote(left.dc); } - if plane == 0 && std::env::var_os("EC_DCDUMP").is_some() { + if std::env::var_os("EC_DCDUMP").is_some() { let ab: Vec = (0..w_mi) - .map(|k| format!("{:?}/{}", self.above[mi_c + k][0].dc, self.above[mi_c + k][0].level)) + .map(|k| format!("{:?}/{}", self.above[mi_c + k][plane].dc, self.above[mi_c + k][plane].level)) .collect(); let lf: Vec = (0..h_mi) - .map(|k| format!("{:?}/{}", self.left[mi_r + k][0].dc, self.left[mi_r + k][0].level)) + .map(|k| format!("{:?}/{}", self.left[mi_r + k][plane].dc, self.left[mi_r + k][plane].level)) .collect(); - eprintln!("EC_DCDUMP mi=({mi_r},{mi_c}) wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(",")); + eprintln!("EC_DCDUMP mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(",")); } (above_coded, left_coded, vote) }) @@ -22927,6 +22927,28 @@ fn decode_inter_block( } else { Vec::new() }; + // libaom `av1_reset_entropy_context` / `av1_set_entropy_contexts`: + // `nplanes = 1 + (num_planes - 1) * xd->is_chroma_ref` -- a block that is + // NOT the chroma reference of its 4:2:0 pair codes no chroma transform at + // all, so it leaves its neighbours' CHROMA coefficient contexts exactly as + // it found them. `record_rect_mi`/`record_split_luma_rect_mi` below write + // all three planes unconditionally, and for such a strip the chroma grids + // are all-zero (see the `!has_chroma` arm above) -- that wiped the real + // left-neighbour chroma state, so the PAIR's own `all_zero` then read + // `txb_skip_ctx` one row low (lane-t900 r3, 10-bit 1920x792 stream, + // mi(40,220): base 1 where libaom gathers 2). Snapshot and restore is + // equivalent to not writing them, without threading a flag through both + // recorders. + let saved_chroma_ctx = (!has_chroma).then(|| { + let (w_mi, h_mi) = (write_w / MI, write_h / MI); + let left: Vec<[Neighbour; 2]> = (0..h_mi) + .filter_map(|k| neighbours.left.get(at.0 + k).map(|s| [s[1], s[2]])) + .collect(); + let above: Vec<[Neighbour; 2]> = (0..w_mi) + .filter_map(|k| neighbours.above.get(at.1 + k).map(|s| [s[1], s[2]])) + .collect(); + (left, above) + }); if vartx_leaves.is_some() { // Plane 0 is already correct per transform unit // ([`Neighbours::record_mi_luma`] above); this writes everything else @@ -22960,6 +22982,20 @@ fn decode_inter_block( &[luma_grid, u_grid, v_grid], ); } + if let Some((left, above)) = saved_chroma_ctx { + for (k, v) in left.into_iter().enumerate() { + if let Some(slot) = neighbours.left.get_mut(at.0 + k) { + slot[1] = v[0]; + slot[2] = v[1]; + } + } + for (k, v) in above.into_iter().enumerate() { + if let Some(slot) = neighbours.above.get_mut(at.1 + k) { + slot[1] = v[0]; + slot[2] = v[1]; + } + } + } if let Some((s, u_state, v_state)) = pair_chroma { let (pw, ph) = if s.horz { (16usize, 8usize) } else { (8, 16) }; let round_up_even = |n: usize| n.div_ceil(2) * 2;