forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-t900' (78e0079b): palette reconstruction inside inter frames (square intra-in-inter + rect strips), variable-alphabet CDF counter reset; screen witnesses gated byte-exact at 8 and 10 bit

Browse files at this commit

2b9468da29ce2782250fedf3a48e301bf40a2b88Ahmet Kaan GümüşParent: b0b1b476a16223
crates/ec-av1/examples/decode_probe.rsmodified+2 -0
@@ -34,6 +34,8 @@ fn main() {
3434 let (h, v, c) = ec_av1::stream::rect4_32_counters();
3535 println!("rect4_32: horz={h} vert={v} coded={c}");
3636 println!("rect_intrabc_reads: {}", ec_av1::stream::rect_intrabc_reads());
37+ let (pal_y, pal_uv) = ec_av1::stream::intra_in_inter_palette_hits();
38+ println!("intra_in_inter_palette: y={pal_y} uv={pal_uv}");
3739 println!(
3840 "rect64_corner_tu: 64x32={} 32x64={}",
3941 ec_av1::stream::rect64_corner_tu_hits(0),
crates/ec-av1/fixtures/palette_screen_strip16_witness_10bit.obuadded+0 -0

Binary file changed

crates/ec-av1/fixtures/palette_screen_witness.obuadded+0 -0

Binary file changed

crates/ec-av1/fixtures/palette_screen_witness_10bit.obuadded+0 -0

Binary file changed

crates/ec-av1/src/cdf_state.rsmodified+25 -2
@@ -759,6 +759,19 @@ fn reset3<const N: usize, const M: usize, const K: usize>(a: &mut [[[u16; N]; M]
759759 a.iter_mut().for_each(reset2);
760760 }
761761
762+/// [`reset3`] for a table whose ALPHABET grows with its first index: the
763+/// palette colour-index CDFs are `[u16; 9]` rows read as `[..=n]` with
764+/// `n = idx + 2`, so their symbol counter sits at index `n`, not `N - 1`.
765+fn reset_var_alphabet<const N: usize, const M: usize, const K: usize>(
766+ a: &mut [[[u16; N]; M]; K],
767+) {
768+ for (idx, rows) in a.iter_mut().enumerate() {
769+ for row in rows.iter_mut() {
770+ row[idx + 2] = 0;
771+ }
772+ }
773+}
774+
762775 impl MvComponentCdfs {
763776 fn reset_counts(&mut self) {
764777 reset1(&mut self.class);
@@ -926,8 +939,18 @@ impl Cdfs {
926939 reset2(&mut self.palette_y_size);
927940 reset2(&mut self.palette_uv_mode);
928941 reset2(&mut self.palette_uv_size);
929- reset3(&mut self.palette_y_color_index);
930- reset3(&mut self.palette_uv_color_index);
942+ // lane-t900 r22 (class cdf-counter-not-reset): the two colour-index
943+ // tables are the only VARIABLE-ALPHABET ones here -- the reader takes
944+ // `[..=n]` of a `[u16; 9]` row, so `update_cdf`'s symbol counter lives
945+ // at index `n` (= table index + 2), not at the array's last entry.
946+ // `reset3` zeroed slot 8, which no `n < 8` row ever uses, so every
947+ // palette colour-index row crossed a frame boundary with a SATURATED
948+ // count (32) and adapted at rate 6 where libaom, counting from 0,
949+ // adapts at rate 4: the first inter-frame palette block's map desynced
950+ // at its third colour-index symbol (witness `palette_screen_witness.obu`,
951+ // decode-order frame 3, mi(40,0), Y map (0,2)).
952+ reset_var_alphabet(&mut self.palette_y_color_index);
953+ reset_var_alphabet(&mut self.palette_uv_color_index);
931954 reset1(&mut self.intrabc);
932955 reset1(&mut self.restore_wiener);
933956 reset1(&mut self.restore_sgrproj);
crates/ec-av1/src/decode.rsmodified+210 -30
@@ -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
@@ -6692,7 +6710,12 @@ fn decode_color_index_map_wh(
66926710 let (ctx, color_order) = palette_color_index_context(&map, bw, row, col, n);
66936711 if trace {
66946712 let (rng, _) = dec.debug_state();
6695- eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} rng={rng}");
6713+ let row_cdf = if uv {
6714+ &cdfs.palette_uv_color_index[n - 2][ctx]
6715+ } else {
6716+ &cdfs.palette_y_color_index[n - 2][ctx]
6717+ };
6718+ eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} cdf0={} cnt={} rng={rng}", row_cdf[0], row_cdf[n]);
66966719 }
66976720 let symbol = if uv {
66986721 dec.symbol(&mut cdfs.palette_uv_color_index[n - 2][ctx][..=n])
@@ -7715,12 +7738,13 @@ fn decode_intra_rect_in_inter(
77157738 // [`INTRA_IN_INTER_MODE`] switches to `read_intra_block_mode_info`'s
77167739 // `y_mode[0]` plus the `skip` this caller already read.
77177740 if let Some((horz, has_chroma)) = strip16 {
7718- if allow_screen_content_tools {
7719- return Err(unsupported(
7720- "a HORZ/VERT intra strip in a screen-content frame (palette syntax \
7721- is consumed for square blocks only)",
7722- ));
7723- }
7741+ // lane-t900 r22: the screen-content refusal here was STALE (class
7742+ // stale-premise): [`decode_rect4_16_strip`] takes
7743+ // `allow_screen_content_tools` and reads the whole palette syntax
7744+ // (mode/size/colours/colour-index map, its own `palette_ctx_and_cache_mi`
7745+ // pair) -- the key frame proves that body. Lifted with the 10-bit
7746+ // witness `palette_screen_witness_10bit.obu`, whose inter frames carry
7747+ // 16x4/4x16 intra strips inside a `--tune-content=screen` frame.
77247748 debug_assert_eq!((bw, bh), if horz { (16, 4) } else { (4, 16) });
77257749 INTRA_IN_INTER_MODE.with(|c| c.set(Some((0, skip))));
77267750 let res = decode_rect4_16_strip(
@@ -8015,24 +8039,35 @@ fn decode_intra_rect_in_inter(
80158039 // frame writes `palette_y_mode`/`palette_uv_mode` for every block whose
80168040 // shape [`palette_bsize_ctx_wh`] admits, intra strips included, so
80178041 // 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.
8042+ // lane-t900 r22: reconstructed, not refused -- the strip's real mode
8043+ // context and colour cache at its own mi position, and the two
8044+ // colour-index maps decoded after `filter_intra` (`av1_visit_palette`),
8045+ // exactly as [`read_intra_mode_rect`] does on the key-frame path.
8046+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((mi_r, mi_c));
8047+ let palette_uv_cache = neighbours.palette_uv_cache_mi((mi_r, mi_c));
8048+ let mut palette_y_pending: Option<(usize, [u16; 8])> = None;
8049+ let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None;
80218050 if allow_screen_content_tools
80228051 && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh)
80238052 {
80248053 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- ));
8054+ let use_palette_y =
8055+ mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0;
8056+ if use_palette_y {
8057+ let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]);
8058+ palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache)));
80298059 }
80308060 // A 2:1 strip of at least 8x8 is always a chroma reference, so
80318061 // `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- ));
8062+ let use_palette_uv =
8063+ uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0;
8064+ if crate::envflags::env_flag!("EC_PALSYN") {
8065+ 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);
8066+ }
8067+ if use_palette_uv {
8068+ let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]);
8069+ let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache);
8070+ palette_uv_pending = Some((n, u_colors, v_colors));
80368071 }
80378072 }
80388073 // lane-fiinter (merged on main for the square intra-in-inter arm):
@@ -8044,6 +8079,8 @@ fn decode_intra_rect_in_inter(
80448079 let mut filter_intra = None;
80458080 if mode == DC_PRED
80468081 && ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get)
8082+ // `av1_filter_intra_allowed` excludes a palette-Y block.
8083+ && palette_y_pending.is_none()
80478084 && let Some(class) = filter_intra_size_class_rect(bw, bh)
80488085 && dec.symbol(&mut cdfs.filter_intra[class]) != 0
80498086 {
@@ -8051,6 +8088,21 @@ fn decode_intra_rect_in_inter(
80518088 INTRA_IN_INTER_FILTER_INTRA_HITS.with(|h| h.set(h.get() + 1));
80528089 filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode));
80538090 }
8091+ // `av1_visit_palette`: Y's colour-index map then the shared chroma one.
8092+ let palette_y = palette_y_pending.map(|(n, colors)| {
8093+ let map = decode_color_index_map_wh(dec, cdfs, n, bw, bh, false);
8094+ PALETTE_HITS.with(|c| c.set(c.get() + 1));
8095+ PALETTE_RECT_HITS.with(|c| c.set(c.get() + 1));
8096+ INTRA_IN_INTER_PALETTE_Y_HITS.with(|c| c.set(c.get() + 1));
8097+ PaletteY { size: n, colors, map }
8098+ });
8099+ let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
8100+ let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4));
8101+ let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true);
8102+ PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
8103+ INTRA_IN_INTER_PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
8104+ PaletteUv { size: n, u_colors, v_colors, map }
8105+ });
80548106 // The intra-edge filter type is the NEIGHBOUR's mode (libaom
80558107 // `get_filt_type`), luma's off the luma neighbours and chroma's off the
80568108 // chroma ones -- [`decode_block_rect`]'s own pair.
@@ -8127,10 +8179,15 @@ fn decode_intra_rect_in_inter(
81278179 filter_intra,
81288180 smooth_neighbor,
81298181 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,
8182+ palette_y: palette_y
8183+ .as_ref()
8184+ .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()),
8185+ palette_uv: palette_uv.as_ref().map(|p| {
8186+ (
8187+ p.map.iter().map(|&i| p.u_colors[i as usize]).collect(),
8188+ p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
8189+ )
8190+ }),
81348191 };
81358192 decode_rect_split(
81368193 dec,
@@ -8164,6 +8221,18 @@ fn decode_intra_rect_in_inter(
81648221 *cell = tx_h as u8;
81658222 }
81668223 }
8224+ // The strip's own palette state into the mi-granular neighbour bands, so
8225+ // the next block's colour cache and `palette_y_mode` context are libaom's.
8226+ record_strip_palette(
8227+ neighbours,
8228+ (mi_r, mi_c),
8229+ bw,
8230+ bh,
8231+ palette_y.as_ref().map_or(0, |p| p.size),
8232+ palette_y.as_ref().map_or([0u16; 8], |p| p.colors),
8233+ palette_uv.as_ref().map_or(0, |p| p.size),
8234+ palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors),
8235+ );
81678236 let class = match bw.max(bh) {
81688237 64 => 0,
81698238 32 => 1,
@@ -20691,6 +20760,13 @@ fn decode_inter_block(
2069120760 // than one luma transform -- read back by the residual loop, the
2069220761 // coefficient-context write-back and the loop-filter grid fill below.
2069320762 let mut vartx_leaves: Option<Vec<(usize, usize, usize, usize)>> = None;
20763+ // lane-t900 r22: this block's own palette state, stamped into the
20764+ // mi-granular neighbour bands at the tail below for EVERY block --
20765+ // libaom's `MB_MODE_INFO.palette_size` is 0 on an inter block, and
20766+ // `av1_get_palette_cache`/`av1_get_palette_mode_ctx` read those cells, so
20767+ // leaving a previous palette block's colours standing here would feed the
20768+ // next intra block a cache libaom never builds.
20769+ let mut palette_record: (usize, [u16; 8], usize, [u16; 8]) = (0, [0u16; 8], 0, [0u16; 8]);
2069420770 let at_mi = at;
2069520771 // lane-vert46 r1: set when this block's chroma was coded as one 32x32 unit
2069620772 // per 64x64 "mu" chunk (any side above 64), so the end-of-block record
@@ -23376,18 +23452,39 @@ fn decode_inter_block(
2337623452 // by the block's TRUE footprint -- a rect strip's row is not its
2337723453 // enclosing square's, and a 16x4 strip (64 pixels) reads no palette
2337823454 // symbol at all.
23455+ // lane-t900 r22: reconstructed, not refused. Same syntax and same
23456+ // deferral of the colour-index maps past `filter_intra`
23457+ // (`av1_visit_palette` runs from `parse_decode_block`, after the whole
23458+ // mode-info read) as the key frame's [`read_intra_mode`]; the mode
23459+ // context and colour cache are this block's real above/left ones, at
23460+ // its own mi position (`av1_get_palette_mode_ctx`/
23461+ // `av1_get_palette_cache`), not the hardcoded 0/empty pair the refusal
23462+ // could get away with while no neighbour could ever be a palette.
23463+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((rmi, cmi));
23464+ let palette_uv_cache = neighbours.palette_uv_cache_mi((rmi, cmi));
23465+ let mut palette_y_pending: Option<(usize, [u16; 8])> = None;
23466+ let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None;
2337923467 if allow_screen_content_tools
2338023468 && let Some(bsize_ctx) = palette_bsize_ctx_wh(write_w, write_h)
2338123469 {
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- ));
23470+ let use_palette_y =
23471+ mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0;
23472+ if use_palette_y {
23473+ let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]);
23474+ palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache)));
2338623475 }
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- ));
23476+ if crate::envflags::env_flag!("EC_PALSYN") {
23477+ eprintln!("EC_PALSYN_PRE sq mi={rmi},{cmi} bw={write_w} bh={write_h} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} rng={}", palette_y_pending.map_or(0, |(n, _)| n), dec.debug_state().0);
23478+ }
23479+ let use_palette_uv =
23480+ uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0;
23481+ if crate::envflags::env_flag!("EC_PALSYN") {
23482+ 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);
23483+ }
23484+ if use_palette_uv {
23485+ let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]);
23486+ let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache);
23487+ palette_uv_pending = Some((n, u_colors, v_colors));
2339123488 }
2339223489 }
2339323490 // lane-fiinter r1: `read_filter_intra_mode_info` (libaom `decodemv.c`,
@@ -23401,6 +23498,9 @@ fn decode_inter_block(
2340123498 let mut filter_intra = None;
2340223499 if mode == DC_PRED
2340323500 && ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get)
23501+ // `av1_filter_intra_allowed` (reconintra.h:77) excludes a
23502+ // palette-Y block, so no `use_filter_intra` symbol exists there.
23503+ && palette_y_pending.is_none()
2340423504 // lane-inter16ab r4 (class sweep): `cdf::FILTER_INTRA`'s row is the
2340523505 // true `BLOCK_SIZES_ALL` class of this strip, not its square side's.
2340623506 && let Some(class) = filter_intra_size_class_rect(write_w, write_h)
@@ -23410,6 +23510,41 @@ fn decode_inter_block(
2341023510 INTRA_IN_INTER_FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1));
2341123511 filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode));
2341223512 }
23513+ // `av1_visit_palette`: the two colour-index maps, Y then the shared
23514+ // chroma one, after the WHOLE mode-info read.
23515+ let palette_y = palette_y_pending.map(|(n, colors)| {
23516+ let map = decode_color_index_map_wh(dec, cdfs, n, write_w, write_h, false);
23517+ PALETTE_HITS.with(|c| c.set(c.get() + 1));
23518+ INTRA_IN_INTER_PALETTE_Y_HITS.with(|c| c.set(c.get() + 1));
23519+ PaletteY { size: n, colors, map }
23520+ });
23521+ let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
23522+ // `av1_get_plane_block_size(bsize, 1, 1)` floors at the 4-px
23523+ // transform, as the rect reader's own copy.
23524+ let (cw, ch) = ((write_w / 2).max(4), (write_h / 2).max(4));
23525+ let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true);
23526+ PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
23527+ INTRA_IN_INTER_PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
23528+ PaletteUv { size: n, u_colors, v_colors, map }
23529+ });
23530+ palette_record = (
23531+ palette_y.as_ref().map_or(0, |p| p.size),
23532+ palette_y.as_ref().map_or([0u16; 8], |p| p.colors),
23533+ palette_uv.as_ref().map_or(0, |p| p.size),
23534+ palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors),
23535+ );
23536+ // A palette block predicts from its own colour-index map, never from
23537+ // the block edge (spec 7.11.2/7.11.4) -- these ride the [`PALETTE_PRED`]
23538+ // slot into each `reconstruct`/`read_plane` call below, exactly as the
23539+ // key-frame paths do.
23540+ let palette_y_buf: Option<Vec<u16>> =
23541+ palette_y.as_ref().map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect());
23542+ let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| {
23543+ (
23544+ p.map.iter().map(|&i| p.u_colors[i as usize]).collect(),
23545+ p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
23546+ )
23547+ });
2341323548 mode_for_tx = mode;
2341423549 block_filter = [3, 3];
2341523550 ref_frame_for_lf = 0;
@@ -23473,6 +23608,19 @@ fn decode_inter_block(
2347323608 // bottom-left availability is the block's, narrowed by the
2347423609 // unit's own offset inside it.
2347523610 let tu_reach = Reach::of_tu(side, side, col * MI, row * MI, tx_px, tx_px, reach);
23611+ // This unit's own window on the block's palette prediction
23612+ // (the whole-block buffer would be indexed at the wrong
23613+ // stride by the first unit).
23614+ if let Some(buf) = &palette_y_buf {
23615+ PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
23616+ let window: Vec<u16> = (0..tx_px)
23617+ .flat_map(|i| {
23618+ let base = (row * MI + i) * side + col * MI;
23619+ buf[base..base + tx_px].iter().copied()
23620+ })
23621+ .collect();
23622+ set_palette_pred(window);
23623+ }
2347623624 let tu_grid = if skip {
2347723625 y.reconstruct(
2347823626 tu_px,
@@ -23611,6 +23759,9 @@ fn decode_inter_block(
2361123759 INTRA_128_IN_INTER_HITS.with(|c| c.set(c.get() + 1));
2361223760 } else if skip {
2361323761 if !split_luma {
23762+ if let Some(buf) = &palette_y_buf {
23763+ set_palette_pred(buf.clone());
23764+ }
2361423765 y.reconstruct(
2361523766 px,
2361623767 py,
@@ -23625,6 +23776,9 @@ fn decode_inter_block(
2362523776 );
2362623777 }
2362723778 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
23779+ if let Some((ub, _)) = &palette_uv_bufs {
23780+ set_palette_pred(ub.clone());
23781+ }
2362823782 u.reconstruct(
2362923783 cpx,
2363023784 cpy,
@@ -23637,6 +23791,9 @@ fn decode_inter_block(
2363723791 None,
2363823792 smooth_neighbor_uv,
2363923793 );
23794+ if let Some((_, vb)) = &palette_uv_bufs {
23795+ set_palette_pred(vb.clone());
23796+ }
2364023797 v.reconstruct(
2364123798 cpx,
2364223799 cpy,
@@ -23660,6 +23817,9 @@ fn decode_inter_block(
2366023817 // which `record_split_luma` ignores for plane 0.
2366123818 vec![0i32; side * side]
2366223819 } else {
23820+ if let Some(buf) = &palette_y_buf {
23821+ set_palette_pred(buf.clone());
23822+ }
2366323823 read_plane(
2366423824 dec,
2366523825 cdfs,
@@ -23684,6 +23844,9 @@ fn decode_inter_block(
2368423844 )?
2368523845 };
2368623846 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
23847+ if let Some((ub, _)) = &palette_uv_bufs {
23848+ set_palette_pred(ub.clone());
23849+ }
2368723850 u_grid = read_plane(
2368823851 dec,
2368923852 cdfs,
@@ -23706,6 +23869,9 @@ fn decode_inter_block(
2370623869 None,
2370723870 smooth_neighbor_uv,
2370823871 )?;
23872+ if let Some((_, vb)) = &palette_uv_bufs {
23873+ set_palette_pred(vb.clone());
23874+ }
2370923875 v_grid = read_plane(
2371023876 dec,
2371123877 cdfs,
@@ -23791,6 +23957,20 @@ fn decode_inter_block(
2379123957 // mi(40,220): base 1 where libaom gathers 2). Snapshot and restore is
2379223958 // equivalent to not writing them, without threading a flag through both
2379323959 // recorders.
23960+ // lane-t900 r22: every block stamps its palette state (all-zero for an
23961+ // inter block, real for a palette one) into the mi-granular neighbour
23962+ // bands, so the next intra block's `av1_get_palette_mode_ctx` and colour
23963+ // cache see exactly what libaom's mode-info grid holds.
23964+ record_strip_palette(
23965+ neighbours,
23966+ at,
23967+ write_w,
23968+ write_h,
23969+ palette_record.0,
23970+ palette_record.1,
23971+ palette_record.2,
23972+ palette_record.3,
23973+ );
2379423974 let saved_chroma_ctx = (!has_chroma).then(|| {
2379523975 NON_CHROMA_REF_CTX_SKIP_HITS.with(|c| c.set(c.get() + 1));
2379623976 let (w_mi, h_mi) = (write_w / MI, write_h / MI);
crates/ec-av1/src/refusal_inventory.rsmodified+13 -3
@@ -46,6 +46,12 @@ const REFUSALS: &[&str] = &[
4646 "CfL, filter intra or a palette on a 128-root HORZ/VERT intra block (every one of their size gates caps at 64x64 or below, so none of these symbols exists there)",
4747 "intrabc under a 128x128 superblock (libaom's av1_is_dv_valid derives the block-vector delay from sb_size, which this decoder hardcodes to 64)",
4848 "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)",
49+ // lane-t900 r22: palette RECONSTRUCTION landed for the key-frame paths
50+ // (earlier lanes) and for the intra-in-inter square and rect paths (this
51+ // one). These two strings survive at exactly two kinds of site: the
52+ // `palette.is_none()` guards in `read_intra_mode`/`read_intra_mode_rect`
53+ // (defensive -- every caller now passes a real ctx/cache pair), and the
54+ // sub-8x8 `decode_inter_block8` leaf, which has no witness stream.
4955 "a block that actually uses a palette (UV) -- reconstruction is out of scope",
5056 "a block that actually uses a palette (Y) -- reconstruction is out of scope",
5157 "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)",
@@ -111,9 +117,13 @@ const REFUSALS: &[&str] = &[
111117 // both arms of gate
112118 // `a_real_aomenc_screen_inter_sequence_codes_palette_syntax_on_rect_intra_strips`
113119 // decode ten frames pixel-exact with the strips counted. What still
114- // refuses is a 16x4/4x16 strip and a 128-root half, neither of which is
115- // palette-eligible (`palette_bsize_ctx_wh` wants `8 <= min`, `max <= 64`)
116- // and neither of which has a witness stream yet.
120+ // lane-t900 r22 NARROWED it again: the 16x4/4x16 arm is LIFTED (that arm
121+ // routes through `decode_rect4_16_strip`, which reads the palette syntax
122+ // itself -- the premise was stale), gated by
123+ // `a_screen_stream_with_16x4_intra_strips_in_inter_frames_decodes_pixel_exact`.
124+ // What still refuses is the 128-root half of a screen-content frame, whose
125+ // body (`decode_block_128rect`) reads no palette syntax and for which no
126+ // `--sb-size=128` screen witness exists yet.
117127 "a HORZ/VERT intra strip in a screen-content frame (palette syntax is consumed for square blocks only)",
118128 "warp prediction with a scaled reference (superres, unimplemented)",
119129 "an 8x8 partition leaf under a scaled reference (superres, unimplemented)",
crates/ec-av1/src/stream.rsmodified+124 -0
@@ -119,6 +119,13 @@ pub fn rect_intrabc_reads() -> usize {
119119 crate::decode::rect_intrabc_reads()
120120 }
121121
122+/// `(y, uv)` palette blocks RECONSTRUCTED inside an inter frame (lane-t900
123+/// r22) -- the counter a screen-content witness gate reads to prove it
124+/// exercises the inter-frame palette path, not the key frame's.
125+pub fn intra_in_inter_palette_hits() -> (usize, usize) {
126+ crate::decode::intra_in_inter_palette_hits()
127+}
128+
122129 /// lane-rect64port r1: coded 64-level HORZ/VERT intra strips whose luma
123130 /// transform unit is the un-split TX_64X32 (orient 0) / TX_32X64 (orient 1).
124131 pub fn rect64_corner_tu_hits(orient: usize) -> usize {
@@ -30922,6 +30929,123 @@ mod tests {
3092230929 );
3092330930 }
3092430931
30932+ /// lane-t900 r22 GATE: a real `aomenc --tune-content=screen
30933+ /// --enable-palette=1` stream whose INTER frames reconstruct palette
30934+ /// blocks (Y and UV), at both bit depths.
30935+ ///
30936+ /// Recipe (both fixtures, 15 frames, key + 14 inter, 256x192, one tile):
30937+ /// `aomenc --codec=av1 --passes=1 --end-usage=q --cq-level=20 --cpu-used=1
30938+ /// --threads=1 --row-mt=0 --lag-in-frames=0 --kf-max-dist=9999
30939+ /// --sb-size=64 --tune-content=screen --enable-palette=1
30940+ /// --enable-intrabc=0 --obu` (plus `--bit-depth=10 --profile=0` for the
30941+ /// twin) over a synthetic screen-like source: two luma levels on a 16x16
30942+ /// grid, four chroma levels on an 8x8 one, both re-keyed per frame so the
30943+ /// encoder cannot predict the new blocks and codes them INTRA inside the
30944+ /// inter frames. `--enable-palette=0` on the same source is 13% larger,
30945+ /// which is what says the tool is really used.
30946+ ///
30947+ /// What this pins that no key-frame palette gate can: the palette
30948+ /// colour-index CDFs are this decoder's only VARIABLE-ALPHABET tables (the
30949+ /// reader takes `[..=n]` of a `[u16; 9]` row, so `update_cdf`'s counter
30950+ /// lives at index `n`), and `Cdfs::reset_counts` zeroed slot 8 instead --
30951+ /// so every colour-index row crossed a frame boundary with a SATURATED
30952+ /// count and adapted at rate 6 where libaom, counting from 0, adapts at 4.
30953+ /// The first inter-frame palette block's map desynced at its THIRD
30954+ /// colour-index symbol (decode-order frame 3, mi(40,0), map cell (0,2):
30955+ /// ours cdf0 = 32768-1071, aomdec's = 32768-971, same ctx, same value,
30956+ /// different range).
30957+ #[test]
30958+ fn a_real_aomenc_screen_stream_with_palette_blocks_decodes_pixel_exact() {
30959+ const NAME: &str =
30960+ "a_real_aomenc_screen_stream_with_palette_blocks_decodes_pixel_exact";
30961+ if !have_ffmpeg() {
30962+ eprintln!("SKIP {NAME}: no ffmpeg");
30963+ return;
30964+ }
30965+ for (fixture, ten_bit) in [
30966+ ("fixtures/palette_screen_witness.obu", false),
30967+ ("fixtures/palette_screen_witness_10bit.obu", true),
30968+ ] {
30969+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(fixture);
30970+ let stream = std::fs::read(&path)
30971+ .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display()));
30972+ let (width, height) = (256usize, 192usize);
30973+ let before = intra_in_inter_palette_hits();
30974+ let frames = match decode_stream(&stream) {
30975+ Ok(frames) => frames,
30976+ Err(e) => panic!("{NAME} [{fixture}]: decode_stream refused: {e}"),
30977+ };
30978+ let after = intra_in_inter_palette_hits();
30979+ let (fired_y, fired_uv) = (after.0 - before.0, after.1 - before.1);
30980+ assert!(
30981+ fired_y > 0 && fired_uv > 0,
30982+ "{NAME} [{fixture}]: inter-frame palette fired y={fired_y} uv={fired_uv} \
30983+ -- both planes must fire or the gate is vacuous for the one that did not"
30984+ );
30985+ assert_eq!(frames.len(), 15, "{NAME} [{fixture}]: 15 shown frames");
30986+ let ffmpeg_frames = if ten_bit {
30987+ ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len())
30988+ } else {
30989+ ffmpeg_decode_sequence(&stream, width, height, frames.len())
30990+ };
30991+ assert_eq!(
30992+ ffmpeg_frames.len(),
30993+ frames.len(),
30994+ "{NAME} [{fixture}]: frame count"
30995+ );
30996+ for (i, (ours, theirs)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
30997+ assert_eq!(ours.y, theirs.y, "{NAME} [{fixture}]: frame {i} luma vs ffmpeg");
30998+ assert_eq!(ours.u, theirs.u, "{NAME} [{fixture}]: frame {i} U vs ffmpeg");
30999+ assert_eq!(ours.v, theirs.v, "{NAME} [{fixture}]: frame {i} V vs ffmpeg");
31000+ }
31001+ eprintln!(
31002+ "{NAME} [{fixture}]: 15 frames pixel-exact on every plane; \
31003+ inter-frame palette y={fired_y} uv={fired_uv}"
31004+ );
31005+ }
31006+ }
31007+
31008+ /// lane-t900 r22 GATE for the LIFTED refusal `"a HORZ/VERT intra strip in
31009+ /// a screen-content frame (palette syntax is consumed for square blocks
31010+ /// only)"` on its 16x4/4x16 arm: that arm routes through
31011+ /// `decode_rect4_16_strip`, which takes `allow_screen_content_tools` and
31012+ /// reads the whole palette syntax itself, so the refusal was a stale
31013+ /// premise (the key-frame path proves that body). This 10-bit
31014+ /// `--tune-content=screen` stream reached that exact refusal before the
31015+ /// lift and now decodes all 15 frames pixel-exact while 8 inter-frame
31016+ /// palette blocks reconstruct.
31017+ #[test]
31018+ fn a_screen_stream_with_16x4_intra_strips_in_inter_frames_decodes_pixel_exact() {
31019+ const NAME: &str =
31020+ "a_screen_stream_with_16x4_intra_strips_in_inter_frames_decodes_pixel_exact";
31021+ if !have_ffmpeg() {
31022+ eprintln!("SKIP {NAME}: no ffmpeg");
31023+ return;
31024+ }
31025+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
31026+ .join("fixtures/palette_screen_strip16_witness_10bit.obu");
31027+ let stream = std::fs::read(&path)
31028+ .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display()));
31029+ let before = intra_in_inter_palette_hits();
31030+ let frames = match decode_stream(&stream) {
31031+ Ok(frames) => frames,
31032+ Err(e) => panic!("{NAME}: decode_stream refused: {e}"),
31033+ };
31034+ let fired_y = intra_in_inter_palette_hits().0 - before.0;
31035+ assert!(
31036+ fired_y > 0,
31037+ "{NAME}: no inter-frame palette block -- gate is vacuous"
31038+ );
31039+ assert_eq!(frames.len(), 15, "{NAME}: 15 shown frames");
31040+ let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, 256, 192, frames.len());
31041+ for (i, (ours, theirs)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
31042+ assert_eq!(ours.y, theirs.y, "{NAME}: frame {i} luma vs ffmpeg");
31043+ assert_eq!(ours.u, theirs.u, "{NAME}: frame {i} U vs ffmpeg");
31044+ assert_eq!(ours.v, theirs.v, "{NAME}: frame {i} V vs ffmpeg");
31045+ }
31046+ eprintln!("{NAME}: 15 frames pixel-exact; inter-frame palette y={fired_y}");
31047+ }
31048+
3092531049 /// lane-t900 r20 GATE, refusal A -- `"a non-skip rectangular
3092631050 /// (HORZ/VERT/HORZ_B) strip needs rectangular residual coding"`.
3092731051 ///