forge
Explore
Log in

Don't you dare go Hollow

Lossless chroma units read a windowed palette prediction, one entropy cell, and stop at the frame edge

Browse files at this commit

6f56c949e523645262bbc6298b28d67157e4e550Ahmet Kaan GümüşParent: adb743e
  • fix
    • lossless chroma units read a windowed palette prediction, one entropy cell, and stop at the frame edge
Show full message
fix(av1): lossless chroma units read a windowed palette prediction, one entropy cell, and stop at the frame edge

Three defects the synthetic (non-screen) fixtures could not see, all found by
a tune-content=screen -crf 0 stream:

- a 4x4 chroma unit was handed the WHOLE-BLOCK palette prediction buffer and
  read it at its own stride, so every unit reconstructed the block's top-left
  corner (the luma split-transform paths already window; palette_window() is
  now shared by decode_block, decode_rect_split and the new intra helper).
  A 320x192 screen key frame decoded 9600/15360 chroma samples wrong.
- a unit whose block hangs off the frame edge is still coded by libaom
  (max_blocks_high clips in LUMA mi, then rounds UP for chroma), and our
  two-cell neighbour read then indexed past the band. libaom reads exactly
  ONE chroma entropy entry for a TX_4X4 chroma unit (get_txb_ctx's a[0]/l[0]);
  our bands carry that entry replicated over the unit's two luma-mi cells, so
  one cell is the same answer and never runs off.
- units whose ORIGIN is past the frame edge are not coded at all.

The 320x192 and 640x384 screen key frames are now sample-exact.
crates/ec-av1/src/decode.rsmodified+51 -8
@@ -2099,6 +2099,18 @@ pub(crate) fn intra_in_inter_split_tx_hits() -> [usize; 4] {
2099 /// (`set`) / consumes it (`take`). Set and take MUST pair: a buffer armed at a2099 /// (`set`) / consumes it (`take`). Set and take MUST pair: a buffer armed at a
2100 /// site whose reconstruct never runs would be picked up by the NEXT block's2100 /// site whose reconstruct never runs would be picked up by the NEXT block's
2101 /// reconstruct (class [[refusal-hides-a-defect]] / stale thread-local), so2101 /// reconstruct (class [[refusal-hides-a-defect]] / stale thread-local), so
2102+/// lane-lossless2: one transform unit's window on a block-sized palette
2103+/// prediction buffer. A unit reads its prediction at ITS OWN stride, so
2104+/// handing it the whole-block buffer makes every unit reconstruct the block's
2105+/// top-left corner (the luma split-transform paths already window; the chroma
2106+/// ones did not, which is invisible until a lossless frame codes a palette
2107+/// block as 4x4 chroma units).
2108+fn palette_window(buf: &[u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize) -> Vec<u16> {
2109+ (0..h)
2110+ .flat_map(|r| buf[(oy + r) * stride + ox..][..w].iter().copied())
2111+ .collect()
2112+}
2113+
2102 /// `EC_DEBUG_PAL=1` traces every arm and every consumption with its call site.2114 /// `EC_DEBUG_PAL=1` traces every arm and every consumption with its call site.
2103 #[track_caller]2115 #[track_caller]
2104 fn set_palette_pred(buf: Vec<u16>, fctx: &crate::decode::FrameCtx) {2116 fn set_palette_pred(buf: Vec<u16>, fctx: &crate::decode::FrameCtx) {
@@ -9601,8 +9613,9 @@ fn decode_rect_split(
9601 );9613 );
9602 let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4);9614 let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4);
9603 if let Some((ub, vb)) = &m.palette_uv {9615 if let Some((ub, vb)) = &m.palette_uv {
9616+ let pbuf = if plane_idx == 1 { ub } else { vb };
9604 set_palette_pred(9617 set_palette_pred(
9605- if plane_idx == 1 { ub.clone() } else { vb.clone() },9618+ palette_window(pbuf, chroma_w, cu_col * 4, cu_row * 4, 4, 4),
9606 fctx,9619 fctx,
9607 );9620 );
9608 }9621 }
@@ -14498,8 +14511,12 @@ fn decode_block(
14498 };14511 };
14499 let (cu_x, cu_y) = (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx);14512 let (cu_x, cu_y) = (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx);
14500 if let Some((ub, vb)) = &palette_uv_bufs {14513 if let Some((ub, vb)) = &palette_uv_bufs {
14514+ let pbuf = if plane_pass == 1 { ub } else { vb };
14501 set_palette_pred(14515 set_palette_pred(
14502- if plane_pass == 1 { ub.clone() } else { vb.clone() },14516+ palette_window(
14517+ pbuf, chroma_side, cu_col * chroma_tx, cu_row * chroma_tx,
14518+ chroma_tx, chroma_tx,
14519+ ),
14503 fctx,14520 fctx,
14504 );14521 );
14505 }14522 }
@@ -24493,16 +24510,28 @@ fn read_intra_chroma_lossless(
24493 for rr in 0..reg_h / 4 {24510 for rr in 0..reg_h / 4 {
24494 for cc in 0..reg_w / 4 {24511 for cc in 0..reg_w / 4 {
24495 let (ox, oy) = (org_x + cc * 4, org_y + rr * 4);24512 let (ox, oy) = (org_x + cc * 4, org_y + rr * 4);
24513+ // libaom's `max_blocks_wide/high` clip: a unit whose origin is
24514+ // past the frame edge is not coded at all
24515+ // (`decode_token_recon_block`), same test `decode_block`'s
24516+ // luma loop already runs.
24517+ if cpx + ox >= u.true_width || cpy + oy >= u.true_height {
24518+ continue;
24519+ }
24496 let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2);24520 let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2);
24497- let cu_around = neighbours.around_mi(cu_mi, 8);24521+ // libaom's chroma entropy-context arrays are SUBSAMPLED:
24522+ // `get_txb_ctx` reads exactly one entry per plane for a
24523+ // TX_4X4 chroma unit (`a[0]`/`l[0]`). Our bands are luma-mi
24524+ // indexed and carry that one entry replicated over the unit's
24525+ // two cells, so one cell is the same answer -- and the second
24526+ // cell is past the band on a unit whose block hangs off the
24527+ // frame edge, which libaom still codes.
24528+ let cu_around = neighbours.around_mi(cu_mi, MI);
24498 let cu_reach = tu_reach(24529 let cu_reach = tu_reach(
24499 side, side, ox * 2, oy * 2, 8, reach, px, py, frame_w, frame_h, fctx,24530 side, side, ox * 2, oy * 2, 8, reach, px, py, frame_w, frame_h, fctx,
24500 );24531 );
24501 if let Some((ub, vb)) = palette_uv_bufs {24532 if let Some((ub, vb)) = palette_uv_bufs {
24502- set_palette_pred(24533+ let buf = if plane_idx == 1 { ub } else { vb };
24503- if plane_idx == 1 { ub.to_vec() } else { vb.to_vec() },24534+ set_palette_pred(palette_window(buf, stride, ox, oy, 4, 4), fctx);
24504- fctx,
24505- );
24506 }24535 }
24507 let buf: &mut PlaneBuf<'static> =24536 let buf: &mut PlaneBuf<'static> =
24508 if plane_idx == 1 { &mut *u } else { &mut *v };24537 if plane_idx == 1 { &mut *u } else { &mut *v };
@@ -24589,10 +24618,24 @@ fn read_inter_chroma_lossless(
24589 for rr in 0..reg_h / 4 {24618 for rr in 0..reg_h / 4 {
24590 for cc in 0..reg_w / 4 {24619 for cc in 0..reg_w / 4 {
24591 let (ox, oy) = (org_x + cc * 4, org_y + rr * 4);24620 let (ox, oy) = (org_x + cc * 4, org_y + rr * 4);
24621+ // libaom's `max_blocks_wide/high` clip: a unit whose origin is
24622+ // past the frame edge is not coded at all
24623+ // (`decode_token_recon_block`), same test `decode_block`'s
24624+ // luma loop already runs.
24625+ if cpx + ox >= u.true_width || cpy + oy >= u.true_height {
24626+ continue;
24627+ }
24592 // The chroma neighbour bands are indexed in LUMA mi: 4 chroma24628 // The chroma neighbour bands are indexed in LUMA mi: 4 chroma
24593 // pixels span 8 luma ones, i.e. 2 mi.24629 // pixels span 8 luma ones, i.e. 2 mi.
24594 let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2);24630 let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2);
24595- let cu_around = neighbours.around_mi(cu_mi, 8);24631+ // libaom's chroma entropy-context arrays are SUBSAMPLED:
24632+ // `get_txb_ctx` reads exactly one entry per plane for a
24633+ // TX_4X4 chroma unit (`a[0]`/`l[0]`). Our bands are luma-mi
24634+ // indexed and carry that one entry replicated over the unit's
24635+ // two cells, so one cell is the same answer -- and the second
24636+ // cell is past the band on a unit whose block hangs off the
24637+ // frame edge, which libaom still codes.
24638+ let cu_around = neighbours.around_mi(cu_mi, MI);
24596 let (src, buf) = if plane_idx == 1 {24639 let (src, buf) = if plane_idx == 1 {
24597 (su, &mut *u)24640 (su, &mut *u)
24598 } else {24641 } else {