From d247d4292d3c15b4e632a87361b32e4b7e141b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 11:59:10 +0300 Subject: [PATCH] feat(av1): the 32x32 inter search's intra candidate gets a palette arm The corner-cut code_square closed for an inter frame's 16/8 intra leaves but the hand-rolled 32x32 loop did not. tile.rs already writes palette_mode_info from block.palette at that side, so only the candidate was missing. --- crates/ec-av1/src/encode.rs | 63 ++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 7481b423..9e8ea91e 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -4484,6 +4484,9 @@ fn search_inter_block( mode: u8, skip: bool, inter: Option, + /// The luma palette this candidate is predicted from, `None` for + /// every ordinary intra/inter candidate (the palette arm below). + palette: Option, } let mut best: Option = None; let mut consider = |candidate: Candidate| { @@ -4578,9 +4581,65 @@ fn search_inter_block( mode, skip: false, inter: None, + palette: None, }); } + // The palette arm of this search -- the corner-cut `code_square` closed + // for an inter frame's 16/8 intra leaves but this hand-rolled 32x32 loop + // did not. A 32x32 intra block inside an inter frame codes the same + // `palette_mode_info` syntax a key frame's does (tile.rs writes it from + // `block.palette` at every one of the three sides already), so the only + // thing missing was a candidate. Priced against the same `DC_PRED` + // chroma trials the ordinary intra candidates reuse, and a palette block + // is `DC_PRED` with `tx_depth` 0 by construction (its prediction is the + // colour map, not the DC of its neighbours). + if search.screen && crate::decode::palette_bsize_ctx(BLOCK).is_some() { + let source: Vec = (y..y + BLOCK) + .flat_map(|row| luma.source[row * luma.width + x..][..BLOCK].to_vec()) + .collect(); + for pal in palette_candidates(&source) { + let n = usize::from(pal.size); + let prediction: Vec = pal + .map + .iter() + .map(|&i| pal.colors[usize::from(i).min(n - 1)] as u8) + .collect(); + let luma_trial = luma.code_from_prediction( + x, + y, + BLOCK, + &prediction, + false, + search.base_q_idx, + search.deadzone, + luma_set, + ); + let (u, v) = (u_trial.clone(), v_trial.clone()); + let cost = luma_trial.sse + + u.sse + + v.sse + + search.lambda + * (luma_trial.bits + + u.bits + + v.bits + + mode_bits[DC_PRED as usize] + + crate::tile::palette_bits(&pal, BLOCK) + + skip_bits(false) + + intra_inter_bits(false)); + consider(Candidate { + cost, + luma: luma_trial, + u, + v, + mode: DC_PRED, + skip: false, + inter: None, + palette: Some(pal), + }); + } + } + // The reference frame buffer a spec decoder holds is exactly the true // (unpadded) coded extent -- a superblock whose partition stops early at // `has_cols`/`has_rows` never codes syntax for the padding columns/rows @@ -4929,6 +4988,7 @@ fn search_inter_block( mode: DC_PRED, skip, inter: Some(info), + palette: None, }); } } @@ -5026,6 +5086,7 @@ fn search_inter_block( mode: DC_PRED, skip, inter: Some(info), + palette: None, }); } } @@ -5044,7 +5105,7 @@ fn search_inter_block( skip: best.skip, inter: best.inter, eight: None, - palette: None, + palette: best.palette, tx_depth: 0, }, best.cost,