diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index a022a274..35c49af2 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -326,6 +326,52 @@ pub const FILTER_INTRA: [[u16; 3]; 4] = [ /// `FILTER_INTRA_MODES` a `use_filter_intra` block picks. pub const FILTER_INTRA_MODE: [u16; 6] = [8949, 12776, 17211, 29558, 32768, 0]; +/// `default_palette_y_size_cdf` (entropymode.c): `PALETTE_SIZES` (2..=8), +/// indexed by `av1_get_palette_bsize_ctx` (0..=6, `PALATTE_BSIZE_CTXS`). +pub const PALETTE_Y_SIZE: [[u16; 8]; 7] = [ + [7952, 13000, 18149, 21478, 25527, 29241, 32768, 0], + [7139, 11421, 16195, 19544, 23666, 28073, 32768, 0], + [7788, 12741, 17325, 20500, 24315, 28530, 32768, 0], + [8271, 14064, 18246, 21564, 25071, 28533, 32768, 0], + [12725, 19180, 21863, 24839, 27535, 30120, 32768, 0], + [9711, 14888, 16923, 21052, 25661, 27875, 32768, 0], + [14940, 20797, 21678, 24186, 27033, 28999, 32768, 0], +]; +/// `default_palette_uv_size_cdf` (entropymode.c): as [`PALETTE_Y_SIZE`], for +/// the chroma plane's own palette size. +pub const PALETTE_UV_SIZE: [[u16; 8]; 7] = [ + [8713, 19979, 27128, 29609, 31331, 32272, 32768, 0], + [5839, 15573, 23581, 26947, 29848, 31700, 32768, 0], + [4426, 11260, 17999, 21483, 25863, 29430, 32768, 0], + [3228, 9464, 14993, 18089, 22523, 27420, 32768, 0], + [3768, 8886, 13091, 17852, 22495, 27207, 32768, 0], + [2464, 8451, 12861, 21632, 25525, 28555, 32768, 0], + [1269, 5435, 10433, 18963, 21700, 25865, 32768, 0], +]; +/// `default_palette_y_mode_cdf` (entropymode.c): whether a `DC_PRED` block +/// uses a palette, indexed `[bsize_ctx][palette_mode_ctx]` +/// (`palette_mode_ctx` from `av1_get_palette_mode_ctx` -- this decoder never +/// completes a decode with a nonzero neighbour `palette_size`, so only row +/// `[.][0]` is ever read, but the whole table is here to match libaom's +/// adaptation shape exactly). +pub const PALETTE_Y_MODE: [[[u16; 3]; 3]; 7] = [ + [[31676, 32768, 0], [3419, 32768, 0], [1261, 32768, 0]], + [[31912, 32768, 0], [2859, 32768, 0], [980, 32768, 0]], + [[31823, 32768, 0], [3400, 32768, 0], [781, 32768, 0]], + [[32030, 32768, 0], [3561, 32768, 0], [904, 32768, 0]], + [[32309, 32768, 0], [7337, 32768, 0], [1462, 32768, 0]], + [[32265, 32768, 0], [4015, 32768, 0], [1521, 32768, 0]], + [[32450, 32768, 0], [7946, 32768, 0], [129, 32768, 0]], +]; +/// `default_palette_uv_mode_cdf` (entropymode.c): whether a `UV_DC_PRED` +/// block uses a chroma palette, indexed by `palette_uv_mode_ctx` (whether +/// this block's own luma palette was used -- always 0 here, see +/// [`PALETTE_Y_MODE`]'s doc). +pub const PALETTE_UV_MODE: [[u16; 3]; 2] = [[32461, 32768, 0], [21488, 32768, 0]]; +/// `default_intrabc_cdf` (entropymode.c): the `use_intrabc` flag on an +/// intra frame with `allow_intrabc` set. +pub const INTRABC: [u16; 3] = [30531, 32768, 0]; + /// `default_tx_size_cdf[0]` (entropymode.c): an 8x8 block's `tx_depth` flag /// (`TX8` vs `TX4`), indexed by `tx_size_context` (0..=2). pub const TX_SIZE_CAT0: [[u16; 3]; 3] = [[19968, 32768, 0], [19968, 32768, 0], [24320, 32768, 0]]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index a28cbda7..feaee959 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -401,6 +401,17 @@ pub(crate) struct Cdfs { /// A `SWITCHABLE`-filter inter block's own `interp_filter[dir]` -- see /// [`cdf::SWITCHABLE_INTERP`]. pub switchable_interp: [[u16; 4]; 16], + /// A `DC_PRED` block's own palette-use flag -- see [`cdf::PALETTE_Y_MODE`]. + pub palette_y_mode: [[[u16; 3]; 3]; 7], + /// A palette block's own size (2..=8) -- see [`cdf::PALETTE_Y_SIZE`]. + pub palette_y_size: [[u16; 8]; 7], + /// A `UV_DC_PRED` block's own chroma palette-use flag -- see + /// [`cdf::PALETTE_UV_MODE`]. + pub palette_uv_mode: [[u16; 3]; 2], + /// A chroma palette block's own size -- see [`cdf::PALETTE_UV_SIZE`]. + pub palette_uv_size: [[u16; 8]; 7], + /// The `use_intrabc` flag -- see [`cdf::INTRABC`]. + pub intrabc: [u16; 3], } /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`, @@ -615,6 +626,11 @@ impl Cdfs { reset2(&mut self.tx_size_cat1); reset2(&mut self.tx_size_cat2); reset2(&mut self.tx_size_cat3); + reset3(&mut self.palette_y_mode); + reset2(&mut self.palette_y_size); + reset2(&mut self.palette_uv_mode); + reset2(&mut self.palette_uv_size); + reset1(&mut self.intrabc); } /// The defaults a key frame starts from (spec 8.4, `init_coeff_cdfs` and @@ -1056,6 +1072,11 @@ impl Cdfs { tx_size_cat2: cdf::TX_SIZE_CAT2, tx_size_cat3: cdf::TX_SIZE_CAT3, switchable_interp: cdf::SWITCHABLE_INTERP, + palette_y_mode: cdf::PALETTE_Y_MODE, + palette_y_size: cdf::PALETTE_Y_SIZE, + palette_uv_mode: cdf::PALETTE_UV_MODE, + palette_uv_size: cdf::PALETTE_UV_SIZE, + intrabc: cdf::INTRABC, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 4d9c3d4f..abae5647 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1533,6 +1533,22 @@ fn filter_intra_size_class(side: usize) -> Option { } } +/// `av1_allow_palette`'s size bound (blockd.h: `block_size_wide/high <= +/// MAX_PALETTE_BLOCK_WIDTH/HEIGHT` (64) and `sb_type >= BLOCK_8X8`) plus +/// `av1_get_palette_bsize_ctx` (pred_common.h: `num_pels_log2_lookup[bsize] +/// - num_pels_log2_lookup[BLOCK_8X8]`, i.e. `2*log2(side) - 6` for this +/// decoder's square-only blocks) -- `None` past the bound (never gates a +/// palette read), `Some(bsize_ctx)` otherwise. +fn palette_bsize_ctx(side: usize) -> Option { + match side { + 8 => Some(0), + 16 => Some(2), + 32 => Some(4), + 64 => Some(6), + _ => None, + } +} + /// `read_angle_delta`/`Angle_Delta` (spec 5.11.42 + 9.3): the symbol minus /// [`ANGLE_DELTA_ZERO`] gives the signed `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA` /// delta [`crate::intra::predict`] wants; bumps [`ANGLE_DELTA_HITS`] whenever @@ -1553,6 +1569,8 @@ fn read_intra_mode( cfl: bool, side: usize, enable_filter_intra: bool, + allow_screen_content_tools: bool, + allow_intrabc: bool, ) -> Result<( bool, usize, @@ -1567,6 +1585,26 @@ fn read_intra_mode( if trace { eprintln!("TRACE skip value={}", skip as i32); } + // `read_intrabc_info` (spec 5.11.13, libaom decodemv.c:693, called at + // :811 right after `skip_txfm`/`segment_id`/`cdef`/`delta_q` and before + // `mbmi->mode` is ever read): a `use_intrabc` symbol only present on an + // intra frame whose header set `allow_intrabc`. Actual intra block copy + // needs `assign_dv`'s own motion-vector-prediction machinery (`av1_find_mv_refs`, + // `av1_find_ref_dv`) this decoder does not carry -- lane-screen scope + // draws the line at consuming this one flag symbol and refusing by name + // when it fires, so the arithmetic decoder still resyncs for a stream + // that merely allows (but never uses) intrabc. + if allow_intrabc { + let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; + if trace { + eprintln!("TRACE use_intrabc value={}", use_intrabc as i32); + } + if use_intrabc { + return Err(unsupported( + "a block that actually uses intrabc (this decoder never reconstructs one)", + )); + } + } let above_ctx = INTRA_MODE_CTX[above_mode]; let left_ctx = INTRA_MODE_CTX[left_mode]; let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); @@ -1619,6 +1657,32 @@ fn read_intra_mode( } else { 0 }; + // `read_palette_mode_info` (spec 5.11.13, libaom decodemv.c:567, called + // at :840 right after `xd->cfl.store_y` and before `read_filter_intra_mode_info`): + // gated by `av1_allow_palette` (blockd.h -- size bound only, + // [`palette_bsize_ctx`]). `palette_mode_ctx`/`palette_uv_mode_ctx` + // (`av1_get_palette_mode_ctx`, pred_common.h) read the neighbours' own + // `palette_size[0] > 0` -- always 0 here since a nonzero size refuses + // the whole decode below, so no successfully-completed decode ever has + // such a neighbour (corner-cut: hardcoded ctx 0 instead of tracking it, + // ceiling = real palette reconstruction, out of this lane's scope). + // A nonzero `palette_size` needs the color-cache/delta-coded-colors + // syntax this decoder does not reconstruct -- refuse by name rather + // than silently drop pixels. + if allow_screen_content_tools + && let Some(bsize_ctx) = palette_bsize_ctx(side) + { + if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][0]) != 0 { + return Err(unsupported( + "a block that actually uses a palette (Y) -- reconstruction is out of scope", + )); + } + if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 { + return Err(unsupported( + "a block that actually uses a palette (UV) -- reconstruction is out of scope", + )); + } + } // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a // `use_filter_intra` symbol this decoder never read at all until // lane-av1real r10 -- silently skipping it left every bit past a @@ -1991,6 +2055,8 @@ fn decode_block( v: &mut PlaneBuf, base_q_idx: u8, enable_filter_intra: bool, + allow_screen_content_tools: bool, + allow_intrabc: bool, scan32: &[u16], scan16: &[u16], scan8: &[u16], @@ -2009,6 +2075,8 @@ fn decode_block( cfl, side, enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, )?; // `predict` panics outside `DC_PRED..=PAETH_PRED` (0..=12); `UV_CFL_PRED` // (13) predicts as `DC_PRED` with the [`cfl_scaled`] nudge carrying the @@ -2297,6 +2365,8 @@ fn decode_leaf8( v: &mut PlaneBuf, base_q_idx: u8, enable_filter_intra: bool, + allow_screen_content_tools: bool, + allow_intrabc: bool, tx_select: bool, reduced_tx_set: bool, ) -> Result { @@ -2322,6 +2392,8 @@ fn decode_leaf8( true, 8, enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, )?; let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED @@ -3614,6 +3686,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, &scan32, &scan16, &scan8, @@ -3681,6 +3755,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, &scan32, &scan16, &scan8, @@ -3730,6 +3806,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, &scan32, &scan16, &scan8, @@ -3784,6 +3862,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, tx_select, reduced_tx_set, )?; @@ -3853,6 +3933,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, tx_select, reduced_tx_set, )?;