forge
Explore
Log in

Don't you dare go Hollow

wip(av1): palette reconstruction, r22 partial (committed by the orchestrator at the builder's turn cap)

Browse files at this commit

236830db1cf9528e1397c4a9ea3eeffe3373c4ccAhmet Kaan GümüşParent: b0b1b47
crates/ec-av1/src/decode.rsmodified+204 -27
@@ -1727,6 +1727,24 @@ pub(crate) fn reset_palette_sb_strip_hits() {
17271727 PALETTE_SB_STRIP_HITS.with(|c| c.set(0));
17281728 }
17291729
1730+// lane-t900 r22: palette blocks reconstructed inside an INTER frame (the
1731+// intra-in-inter path, `decode_inter_block`/`decode_inter_block8`) -- counted
1732+// apart from [`PALETTE_HITS`] so a witness gate can prove the INTER-frame
1733+// capability rather than the key frame's, which every screen stream also
1734+// carries.
1735+thread_local! {
1736+ static INTRA_IN_INTER_PALETTE_Y_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
1737+ static INTRA_IN_INTER_PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
1738+}
1739+
1740+/// Current values of the two intra-in-inter palette counters, `(y, uv)`.
1741+pub(crate) fn intra_in_inter_palette_hits() -> (usize, usize) {
1742+ (
1743+ INTRA_IN_INTER_PALETTE_Y_HITS.with(|c| c.get()),
1744+ INTRA_IN_INTER_PALETTE_UV_HITS.with(|c| c.get()),
1745+ )
1746+}
1747+
17301748 // lane-hgkf r2: SKIPPED intra blocks whose `tx_depth` split the luma transform
17311749 // and which are therefore predicted one transform unit at a time (a skipped
17321750 // block codes no coefficients, so nothing else in the stream witnesses this
@@ -6904,7 +6922,7 @@ fn read_intra_mode_rect(
69046922 if use_palette_y {
69056923 if palette.is_none() {
69066924 return Err(unsupported(
6907- "a block that actually uses a palette (Y) -- reconstruction is out of scope",
6925+ "a block that actually uses a palette (Y@rect) -- reconstruction is out of scope",
69086926 ));
69096927 }
69106928 let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]);
@@ -8015,24 +8033,35 @@ fn decode_intra_rect_in_inter(
80158033 // frame writes `palette_y_mode`/`palette_uv_mode` for every block whose
80168034 // shape [`palette_bsize_ctx_wh`] admits, intra strips included, so
80178035 // skipping the syntax here desynced the tile at the strip's own symbol.
8018- // No block this decoder produces can USE a palette (the arms below refuse
8019- // by name), so the neighbour mode context is 0 and there is no cache --
8020- // the same stand-in [`read_intra_mode_rect`] uses when its caller has none.
8036+ // lane-t900 r22: reconstructed, not refused -- the strip's real mode
8037+ // context and colour cache at its own mi position, and the two
8038+ // colour-index maps decoded after `filter_intra` (`av1_visit_palette`),
8039+ // exactly as [`read_intra_mode_rect`] does on the key-frame path.
8040+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((mi_r, mi_c));
8041+ let palette_uv_cache = neighbours.palette_uv_cache_mi((mi_r, mi_c));
8042+ let mut palette_y_pending: Option<(usize, [u16; 8])> = None;
8043+ let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None;
80218044 if allow_screen_content_tools
80228045 && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh)
80238046 {
80248047 INTRA_RECT_IN_INTER_SCREEN_HITS.with(|c| c.set(c.get() + 1));
8025- if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][0]) != 0 {
8026- return Err(unsupported(
8027- "a block that actually uses a palette (Y) -- reconstruction is out of scope",
8028- ));
8048+ let use_palette_y =
8049+ mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0;
8050+ if use_palette_y {
8051+ let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]);
8052+ palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache)));
80298053 }
80308054 // A 2:1 strip of at least 8x8 is always a chroma reference, so
80318055 // `read_palette_mode_info`'s `xd->is_chroma_ref` guard holds here.
8032- if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 {
8033- return Err(unsupported(
8034- "a block that actually uses a palette (UV) -- reconstruction is out of scope",
8035- ));
8056+ let use_palette_uv =
8057+ uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0;
8058+ if crate::envflags::env_flag!("EC_PALSYN") {
8059+ eprintln!("EC_PALSYN rect mi={mi_r},{mi_c} bw={bw} bh={bh} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} uv={} rng={}", use_palette_y as i32, use_palette_uv as i32, dec.debug_state().0);
8060+ }
8061+ if use_palette_uv {
8062+ let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]);
8063+ let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache);
8064+ palette_uv_pending = Some((n, u_colors, v_colors));
80368065 }
80378066 }
80388067 // lane-fiinter (merged on main for the square intra-in-inter arm):
@@ -8044,6 +8073,8 @@ fn decode_intra_rect_in_inter(
80448073 let mut filter_intra = None;
80458074 if mode == DC_PRED
80468075 && ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get)
8076+ // `av1_filter_intra_allowed` excludes a palette-Y block.
8077+ && palette_y_pending.is_none()
80478078 && let Some(class) = filter_intra_size_class_rect(bw, bh)
80488079 && dec.symbol(&mut cdfs.filter_intra[class]) != 0
80498080 {
@@ -8051,6 +8082,24 @@ fn decode_intra_rect_in_inter(
80518082 INTRA_IN_INTER_FILTER_INTRA_HITS.with(|h| h.set(h.get() + 1));
80528083 filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode));
80538084 }
8085+ // `av1_visit_palette`: Y's colour-index map then the shared chroma one.
8086+ let palette_y = palette_y_pending.map(|(n, colors)| {
8087+ let map = decode_color_index_map_wh(dec, cdfs, n, bw, bh, false);
8088+ PALETTE_HITS.with(|c| c.set(c.get() + 1));
8089+ PALETTE_RECT_HITS.with(|c| c.set(c.get() + 1));
8090+ INTRA_IN_INTER_PALETTE_Y_HITS.with(|c| c.set(c.get() + 1));
8091+ if crate::envflags::env_flag!("EC_PALII") {
8092+ eprintln!("EC_PALII yrect mi={mi_r},{mi_c} bw={bw} bh={bh} n={n} skip={skip}");
8093+ }
8094+ PaletteY { size: n, colors, map }
8095+ });
8096+ let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
8097+ let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4));
8098+ let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true);
8099+ PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
8100+ INTRA_IN_INTER_PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
8101+ PaletteUv { size: n, u_colors, v_colors, map }
8102+ });
80548103 // The intra-edge filter type is the NEIGHBOUR's mode (libaom
80558104 // `get_filt_type`), luma's off the luma neighbours and chroma's off the
80568105 // chroma ones -- [`decode_block_rect`]'s own pair.
@@ -8127,10 +8176,15 @@ fn decode_intra_rect_in_inter(
81278176 filter_intra,
81288177 smooth_neighbor,
81298178 smooth_neighbor_uv,
8130- // screen-content frames are refused whole above, so this strip can
8131- // carry no palette.
8132- palette_y: None,
8133- palette_uv: None,
8179+ palette_y: palette_y
8180+ .as_ref()
8181+ .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()),
8182+ palette_uv: palette_uv.as_ref().map(|p| {
8183+ (
8184+ p.map.iter().map(|&i| p.u_colors[i as usize]).collect(),
8185+ p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
8186+ )
8187+ }),
81348188 };
81358189 decode_rect_split(
81368190 dec,
@@ -8164,6 +8218,18 @@ fn decode_intra_rect_in_inter(
81648218 *cell = tx_h as u8;
81658219 }
81668220 }
8221+ // The strip's own palette state into the mi-granular neighbour bands, so
8222+ // the next block's colour cache and `palette_y_mode` context are libaom's.
8223+ record_strip_palette(
8224+ neighbours,
8225+ (mi_r, mi_c),
8226+ bw,
8227+ bh,
8228+ palette_y.as_ref().map_or(0, |p| p.size),
8229+ palette_y.as_ref().map_or([0u16; 8], |p| p.colors),
8230+ palette_uv.as_ref().map_or(0, |p| p.size),
8231+ palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors),
8232+ );
81678233 let class = match bw.max(bh) {
81688234 64 => 0,
81698235 32 => 1,
@@ -10825,7 +10891,7 @@ fn read_intra_mode(
1082510891 if use_palette_y {
1082610892 if palette.is_none() {
1082710893 return Err(unsupported(
10828- "a block that actually uses a palette (Y) -- reconstruction is out of scope",
10894+ "a block that actually uses a palette (Y@square) -- reconstruction is out of scope",
1082910895 ));
1083010896 }
1083110897 let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]);
@@ -20691,6 +20757,13 @@ fn decode_inter_block(
2069120757 // than one luma transform -- read back by the residual loop, the
2069220758 // coefficient-context write-back and the loop-filter grid fill below.
2069320759 let mut vartx_leaves: Option<Vec<(usize, usize, usize, usize)>> = None;
20760+ // lane-t900 r22: this block's own palette state, stamped into the
20761+ // mi-granular neighbour bands at the tail below for EVERY block --
20762+ // libaom's `MB_MODE_INFO.palette_size` is 0 on an inter block, and
20763+ // `av1_get_palette_cache`/`av1_get_palette_mode_ctx` read those cells, so
20764+ // leaving a previous palette block's colours standing here would feed the
20765+ // next intra block a cache libaom never builds.
20766+ let mut palette_record: (usize, [u16; 8], usize, [u16; 8]) = (0, [0u16; 8], 0, [0u16; 8]);
2069420767 let at_mi = at;
2069520768 // lane-vert46 r1: set when this block's chroma was coded as one 32x32 unit
2069620769 // per 64x64 "mu" chunk (any side above 64), so the end-of-block record
@@ -23376,18 +23449,36 @@ fn decode_inter_block(
2337623449 // by the block's TRUE footprint -- a rect strip's row is not its
2337723450 // enclosing square's, and a 16x4 strip (64 pixels) reads no palette
2337823451 // symbol at all.
23452+ // lane-t900 r22: reconstructed, not refused. Same syntax and same
23453+ // deferral of the colour-index maps past `filter_intra`
23454+ // (`av1_visit_palette` runs from `parse_decode_block`, after the whole
23455+ // mode-info read) as the key frame's [`read_intra_mode`]; the mode
23456+ // context and colour cache are this block's real above/left ones, at
23457+ // its own mi position (`av1_get_palette_mode_ctx`/
23458+ // `av1_get_palette_cache`), not the hardcoded 0/empty pair the refusal
23459+ // could get away with while no neighbour could ever be a palette.
23460+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((rmi, cmi));
23461+ let palette_uv_cache = neighbours.palette_uv_cache_mi((rmi, cmi));
23462+ let mut palette_y_pending: Option<(usize, [u16; 8])> = None;
23463+ let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None;
2337923464 if allow_screen_content_tools
2338023465 && let Some(bsize_ctx) = palette_bsize_ctx_wh(write_w, write_h)
2338123466 {
23382- if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][0]) != 0 {
23383- return Err(unsupported(
23384- "a block that actually uses a palette (Y) -- reconstruction is out of scope",
23385- ));
23467+ let use_palette_y =
23468+ mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0;
23469+ if use_palette_y {
23470+ let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]);
23471+ palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache)));
2338623472 }
23387- if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 {
23388- return Err(unsupported(
23389- "a block that actually uses a palette (UV) -- reconstruction is out of scope",
23390- ));
23473+ let use_palette_uv =
23474+ uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0;
23475+ if crate::envflags::env_flag!("EC_PALSYN") {
23476+ eprintln!("EC_PALSYN sq mi={rmi},{cmi} bw={write_w} bh={write_h} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} uv={} rng={}", use_palette_y as i32, use_palette_uv as i32, dec.debug_state().0);
23477+ }
23478+ if use_palette_uv {
23479+ let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]);
23480+ let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache);
23481+ palette_uv_pending = Some((n, u_colors, v_colors));
2339123482 }
2339223483 }
2339323484 // lane-fiinter r1: `read_filter_intra_mode_info` (libaom `decodemv.c`,
@@ -23401,6 +23492,9 @@ fn decode_inter_block(
2340123492 let mut filter_intra = None;
2340223493 if mode == DC_PRED
2340323494 && ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get)
23495+ // `av1_filter_intra_allowed` (reconintra.h:77) excludes a
23496+ // palette-Y block, so no `use_filter_intra` symbol exists there.
23497+ && palette_y_pending.is_none()
2340423498 // lane-inter16ab r4 (class sweep): `cdf::FILTER_INTRA`'s row is the
2340523499 // true `BLOCK_SIZES_ALL` class of this strip, not its square side's.
2340623500 && let Some(class) = filter_intra_size_class_rect(write_w, write_h)
@@ -23410,6 +23504,44 @@ fn decode_inter_block(
2341023504 INTRA_IN_INTER_FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1));
2341123505 filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode));
2341223506 }
23507+ // `av1_visit_palette`: the two colour-index maps, Y then the shared
23508+ // chroma one, after the WHOLE mode-info read.
23509+ let palette_y = palette_y_pending.map(|(n, colors)| {
23510+ let map = decode_color_index_map_wh(dec, cdfs, n, write_w, write_h, false);
23511+ PALETTE_HITS.with(|c| c.set(c.get() + 1));
23512+ INTRA_IN_INTER_PALETTE_Y_HITS.with(|c| c.set(c.get() + 1));
23513+ if crate::envflags::env_flag!("EC_PALII") {
23514+ eprintln!("EC_PALII y mi={rmi},{cmi} bw={write_w} bh={write_h} n={n} skip={skip}");
23515+ }
23516+ PaletteY { size: n, colors, map }
23517+ });
23518+ let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
23519+ // `av1_get_plane_block_size(bsize, 1, 1)` floors at the 4-px
23520+ // transform, as the rect reader's own copy.
23521+ let (cw, ch) = ((write_w / 2).max(4), (write_h / 2).max(4));
23522+ let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true);
23523+ PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
23524+ INTRA_IN_INTER_PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
23525+ PaletteUv { size: n, u_colors, v_colors, map }
23526+ });
23527+ palette_record = (
23528+ palette_y.as_ref().map_or(0, |p| p.size),
23529+ palette_y.as_ref().map_or([0u16; 8], |p| p.colors),
23530+ palette_uv.as_ref().map_or(0, |p| p.size),
23531+ palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors),
23532+ );
23533+ // A palette block predicts from its own colour-index map, never from
23534+ // the block edge (spec 7.11.2/7.11.4) -- these ride the [`PALETTE_PRED`]
23535+ // slot into each `reconstruct`/`read_plane` call below, exactly as the
23536+ // key-frame paths do.
23537+ let palette_y_buf: Option<Vec<u16>> =
23538+ palette_y.as_ref().map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect());
23539+ let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| {
23540+ (
23541+ p.map.iter().map(|&i| p.u_colors[i as usize]).collect(),
23542+ p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
23543+ )
23544+ });
2341323545 mode_for_tx = mode;
2341423546 block_filter = [3, 3];
2341523547 ref_frame_for_lf = 0;
@@ -23473,6 +23605,19 @@ fn decode_inter_block(
2347323605 // bottom-left availability is the block's, narrowed by the
2347423606 // unit's own offset inside it.
2347523607 let tu_reach = Reach::of_tu(side, side, col * MI, row * MI, tx_px, tx_px, reach);
23608+ // This unit's own window on the block's palette prediction
23609+ // (the whole-block buffer would be indexed at the wrong
23610+ // stride by the first unit).
23611+ if let Some(buf) = &palette_y_buf {
23612+ PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
23613+ let window: Vec<u16> = (0..tx_px)
23614+ .flat_map(|i| {
23615+ let base = (row * MI + i) * side + col * MI;
23616+ buf[base..base + tx_px].iter().copied()
23617+ })
23618+ .collect();
23619+ set_palette_pred(window);
23620+ }
2347623621 let tu_grid = if skip {
2347723622 y.reconstruct(
2347823623 tu_px,
@@ -23611,6 +23756,9 @@ fn decode_inter_block(
2361123756 INTRA_128_IN_INTER_HITS.with(|c| c.set(c.get() + 1));
2361223757 } else if skip {
2361323758 if !split_luma {
23759+ if let Some(buf) = &palette_y_buf {
23760+ set_palette_pred(buf.clone());
23761+ }
2361423762 y.reconstruct(
2361523763 px,
2361623764 py,
@@ -23625,6 +23773,9 @@ fn decode_inter_block(
2362523773 );
2362623774 }
2362723775 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
23776+ if let Some((ub, _)) = &palette_uv_bufs {
23777+ set_palette_pred(ub.clone());
23778+ }
2362823779 u.reconstruct(
2362923780 cpx,
2363023781 cpy,
@@ -23637,6 +23788,9 @@ fn decode_inter_block(
2363723788 None,
2363823789 smooth_neighbor_uv,
2363923790 );
23791+ if let Some((_, vb)) = &palette_uv_bufs {
23792+ set_palette_pred(vb.clone());
23793+ }
2364023794 v.reconstruct(
2364123795 cpx,
2364223796 cpy,
@@ -23660,6 +23814,9 @@ fn decode_inter_block(
2366023814 // which `record_split_luma` ignores for plane 0.
2366123815 vec![0i32; side * side]
2366223816 } else {
23817+ if let Some(buf) = &palette_y_buf {
23818+ set_palette_pred(buf.clone());
23819+ }
2366323820 read_plane(
2366423821 dec,
2366523822 cdfs,
@@ -23684,6 +23841,9 @@ fn decode_inter_block(
2368423841 )?
2368523842 };
2368623843 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
23844+ if let Some((ub, _)) = &palette_uv_bufs {
23845+ set_palette_pred(ub.clone());
23846+ }
2368723847 u_grid = read_plane(
2368823848 dec,
2368923849 cdfs,
@@ -23706,6 +23866,9 @@ fn decode_inter_block(
2370623866 None,
2370723867 smooth_neighbor_uv,
2370823868 )?;
23869+ if let Some((_, vb)) = &palette_uv_bufs {
23870+ set_palette_pred(vb.clone());
23871+ }
2370923872 v_grid = read_plane(
2371023873 dec,
2371123874 cdfs,
@@ -23791,6 +23954,20 @@ fn decode_inter_block(
2379123954 // mi(40,220): base 1 where libaom gathers 2). Snapshot and restore is
2379223955 // equivalent to not writing them, without threading a flag through both
2379323956 // recorders.
23957+ // lane-t900 r22: every block stamps its palette state (all-zero for an
23958+ // inter block, real for a palette one) into the mi-granular neighbour
23959+ // bands, so the next intra block's `av1_get_palette_mode_ctx` and colour
23960+ // cache see exactly what libaom's mode-info grid holds.
23961+ record_strip_palette(
23962+ neighbours,
23963+ at,
23964+ write_w,
23965+ write_h,
23966+ palette_record.0,
23967+ palette_record.1,
23968+ palette_record.2,
23969+ palette_record.3,
23970+ );
2379423971 let saved_chroma_ctx = (!has_chroma).then(|| {
2379523972 NON_CHROMA_REF_CTX_SKIP_HITS.with(|c| c.set(c.get() + 1));
2379623973 let (w_mi, h_mi) = (write_w / MI, write_h / MI);
@@ -27027,7 +27204,7 @@ fn decode_inter_block8(
2702727204 if allow_screen_content_tools {
2702827205 if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[0][0]) != 0 {
2702927206 return Err(unsupported(
27030- "a block that actually uses a palette (Y) -- reconstruction is out of scope",
27207+ "a block that actually uses a palette (Y@inter_block8) -- reconstruction is out of scope",
2703127208 ));
2703227209 }
2703327210 // lane-uv8 r1: `read_palette_mode_info` reads the UV symbol only
@@ -27035,7 +27212,7 @@ fn decode_inter_block8(
2703527212 // while every non-DC uv_mode was refused a few lines above.
2703627213 if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 {
2703727214 return Err(unsupported(
27038- "a block that actually uses a palette (UV) -- reconstruction is out of scope",
27215+ "a block that actually uses a palette (UV@inter_block8) -- reconstruction is out of scope",
2703927216 ));
2704027217 }
2704127218 }