forge
Explore
Log in

Don't you dare go Hollow

A non-chroma-reference inter block wiped its neighbours' chroma coefficient contexts

Browse files at this commit

8b5425d6ae2e67b1bbc61f6756549d35c9ea20b9Ahmet Kaan GümüşParent: e049d65
  • fix
    • a non-chroma-reference inter block wiped its neighbours' chroma coefficient contexts
Show full message
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.rsmodified+40 -4
@@ -5401,14 +5401,14 @@ impl Neighbours {
54015401 left_coded |= left.level != 0;
54025402 vote += dc_vote(left.dc);
54035403 }
5404- if plane == 0 && std::env::var_os("EC_DCDUMP").is_some() {
5404+ if std::env::var_os("EC_DCDUMP").is_some() {
54055405 let ab: Vec<String> = (0..w_mi)
5406- .map(|k| format!("{:?}/{}", self.above[mi_c + k][0].dc, self.above[mi_c + k][0].level))
5406+ .map(|k| format!("{:?}/{}", self.above[mi_c + k][plane].dc, self.above[mi_c + k][plane].level))
54075407 .collect();
54085408 let lf: Vec<String> = (0..h_mi)
5409- .map(|k| format!("{:?}/{}", self.left[mi_r + k][0].dc, self.left[mi_r + k][0].level))
5409+ .map(|k| format!("{:?}/{}", self.left[mi_r + k][plane].dc, self.left[mi_r + k][plane].level))
54105410 .collect();
5411- eprintln!("EC_DCDUMP mi=({mi_r},{mi_c}) wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(","));
5411+ eprintln!("EC_DCDUMP mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(","));
54125412 }
54135413 (above_coded, left_coded, vote)
54145414 })
@@ -22927,6 +22927,28 @@ fn decode_inter_block(
2292722927 } else {
2292822928 Vec::new()
2292922929 };
22930+ // libaom `av1_reset_entropy_context` / `av1_set_entropy_contexts`:
22931+ // `nplanes = 1 + (num_planes - 1) * xd->is_chroma_ref` -- a block that is
22932+ // NOT the chroma reference of its 4:2:0 pair codes no chroma transform at
22933+ // all, so it leaves its neighbours' CHROMA coefficient contexts exactly as
22934+ // it found them. `record_rect_mi`/`record_split_luma_rect_mi` below write
22935+ // all three planes unconditionally, and for such a strip the chroma grids
22936+ // are all-zero (see the `!has_chroma` arm above) -- that wiped the real
22937+ // left-neighbour chroma state, so the PAIR's own `all_zero` then read
22938+ // `txb_skip_ctx` one row low (lane-t900 r3, 10-bit 1920x792 stream,
22939+ // mi(40,220): base 1 where libaom gathers 2). Snapshot and restore is
22940+ // equivalent to not writing them, without threading a flag through both
22941+ // recorders.
22942+ let saved_chroma_ctx = (!has_chroma).then(|| {
22943+ let (w_mi, h_mi) = (write_w / MI, write_h / MI);
22944+ let left: Vec<[Neighbour; 2]> = (0..h_mi)
22945+ .filter_map(|k| neighbours.left.get(at.0 + k).map(|s| [s[1], s[2]]))
22946+ .collect();
22947+ let above: Vec<[Neighbour; 2]> = (0..w_mi)
22948+ .filter_map(|k| neighbours.above.get(at.1 + k).map(|s| [s[1], s[2]]))
22949+ .collect();
22950+ (left, above)
22951+ });
2293022952 if vartx_leaves.is_some() {
2293122953 // Plane 0 is already correct per transform unit
2293222954 // ([`Neighbours::record_mi_luma`] above); this writes everything else
@@ -22960,6 +22982,20 @@ fn decode_inter_block(
2296022982 &[luma_grid, u_grid, v_grid],
2296122983 );
2296222984 }
22985+ if let Some((left, above)) = saved_chroma_ctx {
22986+ for (k, v) in left.into_iter().enumerate() {
22987+ if let Some(slot) = neighbours.left.get_mut(at.0 + k) {
22988+ slot[1] = v[0];
22989+ slot[2] = v[1];
22990+ }
22991+ }
22992+ for (k, v) in above.into_iter().enumerate() {
22993+ if let Some(slot) = neighbours.above.get_mut(at.1 + k) {
22994+ slot[1] = v[0];
22995+ slot[2] = v[1];
22996+ }
22997+ }
22998+ }
2296322999 if let Some((s, u_state, v_state)) = pair_chroma {
2296423000 let (pw, ph) = if s.horz { (16usize, 8usize) } else { (8, 16) };
2296523001 let round_up_even = |n: usize| n.div_ceil(2) * 2;