forge
Explore
Log in

Don't you dare go Hollow

Filter intra as an intra-search candidate

Browse files at this commit

b978eaaa330c18a8b9c8cac7bbfdea0053c37dd2Ahmet Kaan GümüşParent: c9c94fc
  • feat
    • filter intra as an intra-search candidate
Show full message
feat(av1): filter intra as an intra-search candidate

Threads the sequence header's `enable_filter_intra` through the raw inter
tile decode entry points and the gates that decode the encoder's own tiles
(stale-header class: five hard-coded `false`s), writes
`filter_intra_mode_info` at all five intra write sites between the palette
colour lists and the colour-index maps, and offers the five recursive
filter-intra modes as RD-priced candidates after the mode/tx-depth/palette
searches. `EC_AV1_FILTER_INTRA=0` restores the previous streams.
crates/ec-av1/src/decode.rsmodified+41 -12
@@ -8274,7 +8274,7 @@ impl Neighbours {
82748274 /// `intra_ext_tx_cdf` row a luma transform reads. A filter-intra block's
82758275 /// `mode` is always `DC_PRED`, so the row comes from the FILTER-INTRA mode
82768276 /// instead (the defect commit 1a89ed9 fixed at the square sites).
8277-fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize {
8277+pub(crate) fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize {
82788278 const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED];
82798279 filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi])
82808280 }
@@ -8282,7 +8282,7 @@ fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize {
82828282 /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`,
82838283 /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32
82848284 /// bound (spec `filter_intra_mode_info` never reads a symbol there).
8285-fn filter_intra_size_class(side: usize) -> Option<usize> {
8285+pub(crate) fn filter_intra_size_class(side: usize) -> Option<usize> {
82868286 match side {
82878287 4 => Some(0),
82888288 8 => Some(1),
@@ -33466,7 +33466,9 @@ pub(crate) fn decode_inter_frame_tile(
3346633466 loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed,
3346733467 enable_dual_filter, reference_select, tx_select,
3346833468 &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false,
33469- switchable_motion_mode, false, fctx,
33469+ // `enable_filter_intra`: as the `false`s beside it, this convenience
33470+ // entry names a stream written without the sequence bit (lane-fintra).
33471+ switchable_motion_mode, false, false, fctx,
3347033472 )
3347133473 }
3347233474
@@ -33518,6 +33520,8 @@ pub(crate) fn decode_inter_frame_tile_lr(
3351833520 // `false` here desynced every stream this crate's encoder writes with the
3351933521 // warp knob on: the same stale-header class as the parameter above.
3352033522 allow_warped_motion: bool,
33523+ // lane-fintra: see [`decode_inter_frame_tiles_lr`]'s own parameter.
33524+ enable_filter_intra: bool,
3352133525 fctx: &crate::decode::FrameCtx,
3352233526 ) -> Result<Picture> {
3352333527 let single_tile = TileInfo {
@@ -33548,6 +33552,7 @@ pub(crate) fn decode_inter_frame_tile_lr(
3354833552 allow_screen_content_tools,
3354933553 switchable_motion_mode,
3355033554 allow_warped_motion,
33555+ enable_filter_intra,
3355133556 fctx,
3355233557 )
3355333558 }
@@ -33589,6 +33594,12 @@ pub(crate) fn decode_inter_frame_tiles_lr(
3358933594 // `false` here desynced every stream this crate's encoder writes with the
3359033595 // warp knob on: the same stale-header class as the parameter above.
3359133596 allow_warped_motion: bool,
33597+ // lane-fintra: this sequence header's own `enable_filter_intra` bit (spec
33598+ // 5.5.2). Hard-coded `false` here until this lane -- the same stale-header
33599+ // class as `switchable_motion_mode`/`allow_warped_motion` above: the
33600+ // encoder's own trial decode desyncs at the first intra-in-inter DC_PRED
33601+ // block the moment its sequence header sets the bit.
33602+ enable_filter_intra: bool,
3359233603 fctx: &crate::decode::FrameCtx,
3359333604 ) -> Result<Picture> {
3359433605 decode_inter_frame_tile_with_cdfs(
@@ -33627,10 +33638,7 @@ pub(crate) fn decode_inter_frame_tiles_lr(
3362733638 allow_warped_motion,
3362833639 allow_screen_content_tools,
3362933640 DeltaParams::default(),
33630- // `enable_filter_intra`: this crate's own encoder never writes the
33631- // sequence bit (`encode.rs`), so no intra-in-inter block of a stream
33632- // reaching this entry point reads a `use_filter_intra` symbol.
33633- false,
33641+ enable_filter_intra,
3363433642 None,
3363533643 fctx,
3363633644 )
@@ -37618,7 +37626,10 @@ mod tests {
3761837626 base_q_idx,
3761937627 w as u32,
3762037628 h as u32,
37621- false,
37629+ // lane-fintra: the sequence bit the encoder wrote this tile under
37630+ // (stale-header class -- a hard-coded `false` here reads one symbol
37631+ // short at the first DC_PRED block once the search offers filter intra).
37632+ crate::encode::filter_intra_on(),
3762237633 &cdef,
3762337634 &loop_filter,
3762437635 tx_select,
@@ -37988,7 +37999,10 @@ mod tests {
3798837999 encoded.base_q_idx,
3798938000 coded_w,
3799038001 coded_h,
37991- false,
38002+ // lane-fintra: the sequence bit the encoder wrote this tile under
38003+ // (stale-header class -- a hard-coded `false` here reads one symbol
38004+ // short at the first DC_PRED block once the search offers filter intra).
38005+ crate::encode::filter_intra_on(),
3799238006 &encoded.cdef,
3799338007 &encoded.loop_filter,
3799438008 encoded.tx_select,
@@ -38032,7 +38046,10 @@ mod tests {
3803238046 encoded.base_q_idx,
3803338047 width as u32,
3803438048 height as u32,
38035- false,
38049+ // lane-fintra: the sequence bit the encoder wrote this tile under
38050+ // (stale-header class -- a hard-coded `false` here reads one symbol
38051+ // short at the first DC_PRED block once the search offers filter intra).
38052+ crate::encode::filter_intra_on(),
3803638053 &encoded.cdef,
3803738054 &encoded.loop_filter,
3803838055 tx_select,
@@ -38117,7 +38134,10 @@ mod tests {
3811738134 key.base_q_idx,
3811838135 width as u32,
3811938136 height as u32,
38120- false,
38137+ // lane-fintra: the sequence bit the encoder wrote this tile under
38138+ // (stale-header class -- a hard-coded `false` here reads one symbol
38139+ // short at the first DC_PRED block once the search offers filter intra).
38140+ crate::encode::filter_intra_on(),
3812138141 &key.cdef,
3812238142 &key.loop_filter,
3812338143 key.tx_select,
@@ -38183,6 +38203,9 @@ mod tests {
3818338203 // the warp bit must be the one it wrote too (lane-av1obmc2);
3818438204 // the encoder only sets it under `EC_AV1_WARP`.
3818538205 crate::encode::warp_on(),
38206+ // lane-fintra: the sequence bit the encoder wrote this tile
38207+ // under -- same stale-header class as the warp bit above.
38208+ crate::encode::filter_intra_on(),
3818638209 fctx,
3818738210 )
3818838211 .unwrap();
@@ -38466,7 +38489,10 @@ mod tests {
3846638489 key.base_q_idx,
3846738490 coded_w,
3846838491 coded_h,
38469- false,
38492+ // lane-fintra: the sequence bit the encoder wrote this tile under
38493+ // (stale-header class -- a hard-coded `false` here reads one symbol
38494+ // short at the first DC_PRED block once the search offers filter intra).
38495+ crate::encode::filter_intra_on(),
3847038496 &key.cdef,
3847138497 &key.loop_filter,
3847238498 key.tx_select,
@@ -38521,6 +38547,9 @@ mod tests {
3852138547 // the warp bit must be the one it wrote too (lane-av1obmc2);
3852238548 // the encoder only sets it under `EC_AV1_WARP`.
3852338549 crate::encode::warp_on(),
38550+ // lane-fintra: the sequence bit the encoder wrote this tile
38551+ // under -- same stale-header class as the warp bit above.
38552+ crate::encode::filter_intra_on(),
3852438553 fctx,
3852538554 )
3852638555 .unwrap();
crates/ec-av1/src/encode.rsmodified+130 -1
@@ -756,6 +756,46 @@ fn intra_predict_u8(
756756 }
757757 }
758758
759+/// [`intra_predict_u8`] for recursive filter intra (spec 7.11.2.3): the same
760+/// `u8` box around [`crate::intra::predict_filter_intra`], whose `mode` is one
761+/// of the five `FILTER_INTRA_MODES` rather than an ordinary intra mode.
762+fn filter_intra_predict_u8(
763+ mode: usize,
764+ above: Option<&[u8]>,
765+ left: Option<&[u8]>,
766+ corner: Option<u8>,
767+ side: usize,
768+ dst: &mut [u8], fctx: &crate::decode::FrameCtx,
769+) {
770+ let mut above_buf = [0u16; 2 * BLOCK];
771+ let mut left_buf = [0u16; 2 * BLOCK];
772+ let widen = |src: &[u8], buf: &mut [u16; 2 * BLOCK]| {
773+ for (d, &v) in buf[..src.len()].iter_mut().zip(src) {
774+ *d = u16::from(v);
775+ }
776+ };
777+ if let Some(a) = above {
778+ widen(a, &mut above_buf);
779+ }
780+ if let Some(l) = left {
781+ widen(l, &mut left_buf);
782+ }
783+ let mut dst16 = [0u16; BLOCK * BLOCK];
784+ let n = side * side;
785+ crate::intra::predict_filter_intra(
786+ mode,
787+ above.map(|a| &above_buf[..a.len()]),
788+ left.map(|l| &left_buf[..l.len()]),
789+ corner.map(u16::from),
790+ side,
791+ side,
792+ &mut dst16[..n], fctx,
793+ );
794+ for (d, &v) in dst.iter_mut().zip(dst16[..n].iter()) {
795+ *d = v as u8;
796+ }
797+}
798+
759799 /// Pads one plane to `(padded_width, padded_height)` by repeating its last
760800 /// row and column, so the block coder always sees a whole number of blocks.
761801 /// [`crop_plane`] undoes this on the way back out.
@@ -1183,7 +1223,11 @@ pub(crate) fn key_frame_headers_colour(
11831223 max_frame_width: w,
11841224 max_frame_height: h,
11851225 use_128x128_superblock: false,
1186- enable_filter_intra: false,
1226+ // lane-fintra: the encoder's intra search offers the five recursive
1227+ // filter-intra modes on eligible blocks, so the sequence bit is set
1228+ // whenever that search is on (`EC_AV1_FILTER_INTRA=0` switches both
1229+ // off and restores the streams this encoder wrote before the lane).
1230+ enable_filter_intra: filter_intra_on(),
11871231 enable_intra_edge_filter: false,
11881232 enable_order_hint: true,
11891233 order_hint_bits: 7,
@@ -3860,6 +3904,73 @@ fn code_square(
38603904 }
38613905 }
38623906
3907+ // The five recursive filter-intra modes (spec 5.11.14), priced against
3908+ // whatever the mode, transform-depth and palette searches settled on --
3909+ // the same "one candidate loop after the incumbent is known" shape the
3910+ // palette above uses. A block that takes one is `DC_PRED` with `tx_depth`
3911+ // 0 and no palette by construction (`av1_filter_intra_allowed`), and its
3912+ // luma transform types are coded from `fimode_to_intradir`'s row
3913+ // (`crate::tile::tx_row`).
3914+ let mut filter_intra: Option<u8> = None;
3915+ if filter_intra_on()
3916+ && palette.is_none()
3917+ && let Some(class) = crate::decode::filter_intra_size_class(side)
3918+ {
3919+ let row = &cdf::FILTER_INTRA[class];
3920+ // With the sequence bit on, EVERY `DC_PRED` block without a luma
3921+ // palette carries the flag, so the incumbent pays its zero before the
3922+ // comparison -- otherwise the candidate is charged a bit the block
3923+ // spends either way (class `price-the-narrowing-not-the-table`).
3924+ if mode == DC_PRED {
3925+ cost += search.lambda * symbol_bits(row, 0);
3926+ }
3927+ let one_bits = symbol_bits(row, 1);
3928+ let kept = luma.snapshot(x, y, side);
3929+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
3930+ let (above, left, corner) =
3931+ luma.edges_into(x, y, side, at.reach, &mut above_buf, &mut left_buf);
3932+ let mut prediction = vec![0u8; side * side];
3933+ for fi in 0..5u8 {
3934+ filter_intra_predict_u8(
3935+ usize::from(fi),
3936+ above,
3937+ left,
3938+ corner,
3939+ side,
3940+ &mut prediction, fctx,
3941+ );
3942+ let trial = luma.code_from_prediction(
3943+ x,
3944+ y,
3945+ side,
3946+ &prediction,
3947+ false,
3948+ search.base_q_idx,
3949+ search.deadzone,
3950+ luma_set,
3951+ );
3952+ let cost_f = trial.sse
3953+ + search.lambda
3954+ * (trial.bits
3955+ + mode_bits[DC_PRED as usize]
3956+ + one_bits
3957+ + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))
3958+ + if tx_select { tx_depth_bits(side, 0) } else { 0.0 });
3959+ if cost_f < cost {
3960+ cost = cost_f;
3961+ mode = DC_PRED;
3962+ angle_delta = 0;
3963+ tx_depth = 0;
3964+ luma_coeffs = coeffs(&trial.levels, side);
3965+ luma.commit(x, y, side, &trial);
3966+ filter_intra = Some(fi);
3967+ }
3968+ }
3969+ if filter_intra.is_none() {
3970+ luma.restore(x, y, side, &kept);
3971+ }
3972+ }
3973+
38633974 // Chroma searches its own mode over [`CHROMA_MODES`], one symbol for both
38643975 // planes; what it costs still counts towards the partition decision.
38653976 let ([u, v], uv_mode, chroma_cost, palette_uv, cfl_alphas) =
@@ -3898,6 +4009,7 @@ fn code_square(
38984009 tx_depth,
38994010 palette,
39004011 palette_uv,
4012+ filter_intra,
39014013 cfl_alphas,
39024014 angle_delta_y: angle_delta as i8,
39034015 ..BlockCoeffs::default()
@@ -3975,6 +4087,16 @@ fn cfl_on() -> bool {
39754087 *ON.get_or_init(|| std::env::var("EC_AV1_CFL").as_deref() != Ok("0"))
39764088 }
39774089
4090+/// Whether the intra search offers the five recursive filter-intra modes at
4091+/// all, and with them whether the sequence header sets `enable_filter_intra`
4092+/// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured
4093+/// its own on/off pair -- and how every byte pin written before it still
4094+/// reproduces).
4095+pub(crate) fn filter_intra_on() -> bool {
4096+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4097+ *ON.get_or_init(|| std::env::var("EC_AV1_FILTER_INTRA").as_deref() != Ok("0"))
4098+}
4099+
39784100 /// Reads [`UV_MODE_HITS`] and zeroes it, so a gate can attribute the counts
39794101 /// to its own encode.
39804102 #[cfg(test)]
@@ -5086,6 +5208,7 @@ fn code_square_inter(
50865208 BlockCoeffs {
50875209 angle_delta_y: 0,
50885210 cfl_alphas: None,
5211+ filter_intra: None,
50895212 luma: coeffs(&levels, side),
50905213 u: coeffs(&u_new.levels, side / 2),
50915214 v: coeffs(&v_new.levels, side / 2),
@@ -5121,6 +5244,7 @@ fn code_square_inter(
51215244 (BlockCoeffs {
51225245 angle_delta_y: 0,
51235246 cfl_alphas: None,
5247+ filter_intra: None,
51245248 luma: coeffs(&levels, side),
51255249 u: coeffs(&u.levels, side / 2),
51265250 v: coeffs(&v.levels, side / 2),
@@ -6223,6 +6347,7 @@ pub(crate) fn encode_key_frame_inner(
62236347 crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec());
62246348 let mut cdfs = start_cdfs.0.clone();
62256349 crate::tile::arm_screen(screen);
6350+ crate::tile::arm_filter_intra(filter_intra_on());
62266351 crate::tile::arm_intrabc(allow_intrabc);
62276352 crate::tile::arm_pricing_cdfs(None, false);
62286353 let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs(
@@ -7829,6 +7954,7 @@ fn search_inter_block(
78297954 BlockCoeffs {
78307955 angle_delta_y: 0,
78317956 cfl_alphas: None,
7957+ filter_intra: None,
78327958 luma: coeffs(&levels, BLOCK),
78337959 u: coeffs(&best.u.levels, BLOCK / 2),
78347960 v: coeffs(&best.v.levels, BLOCK / 2),
@@ -9079,6 +9205,7 @@ pub(crate) fn encode_inter_frame(
90799205 crate::tile::arm_warped_motion(warped_motion);
90809206 crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints);
90819207 crate::tile::arm_screen(screen);
9208+ crate::tile::arm_filter_intra(filter_intra_on());
90829209 let mut cdfs = start_cdfs.0.clone();
90839210 crate::tile::arm_pricing_cdfs(Some(&cdfs), screen);
90849211 // Each tile's own MV grid: a candidate scan never reaches past
@@ -9205,6 +9332,7 @@ pub(crate) fn encode_inter_frame(
92059332 h.allow_screen_content_tools,
92069333 switchable_motion_mode,
92079334 h.allow_warped_motion,
9335+ seq.enable_filter_intra,
92089336 fctx,
92099337 )
92109338 },
@@ -10083,6 +10211,7 @@ mod tests {
1008310211 let residual_block = BlockCoeffs {
1008410212 angle_delta_y: 0,
1008510213 cfl_alphas: None,
10214+ filter_intra: None,
1008610215 luma: vec![Coeff {
1008710216 row: 0,
1008810217 col: 0,
crates/ec-av1/src/stream.rsmodified+4 -1
@@ -2993,7 +2993,10 @@ pub(crate) mod tests {
29932993 encoded.base_q_idx,
29942994 width as u32,
29952995 height as u32,
2996- false,
2996+ // lane-fintra: the sequence bit the encoder wrote this tile under
2997+ // (stale-header class -- a hard-coded `false` here reads one symbol
2998+ // short at the first DC_PRED block once the search offers filter intra).
2999+ crate::encode::filter_intra_on(),
29973000 &encoded.cdef,
29983001 &encoded.loop_filter,
29993002 encoded.tx_select,
crates/ec-av1/src/tile.rsmodified+112 -7
@@ -343,6 +343,80 @@ fn intrabc_armed() -> bool {
343343 INTRABC.with(std::cell::Cell::get)
344344 }
345345
346+thread_local! {
347+ /// This SEQUENCE header's `enable_filter_intra` (spec 5.5.2), armed
348+ /// exactly like [`SCREEN`]: with it every DC_PRED intra block of at most
349+ /// 32x32 that took no luma palette carries a `use_filter_intra` symbol
350+ /// after the palette colours and before the colour-index maps (decode.rs
351+ /// `read_intra_mode`), so a tile written without it and read with it
352+ /// desyncs at the first such block.
353+ static FILTER_INTRA: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
354+}
355+
356+/// Arms the sequence's `enable_filter_intra` bit for the next tile written on
357+/// this thread; cleared by [`CdefIdxGuard`] when that writer returns.
358+pub(crate) fn arm_filter_intra(on: bool) {
359+ FILTER_INTRA.with(|c| c.set(on));
360+}
361+
362+fn filter_intra_armed() -> bool {
363+ FILTER_INTRA.with(std::cell::Cell::get)
364+}
365+
366+/// How many blocks this writer coded `use_filter_intra == 1`, and the
367+/// histogram of the five `filter_intra_mode`s (index 1..=5) -- the fire count
368+/// a gate prints so that "filter intra is on" is a measurement rather than a
369+/// claim (class `gate-blind-to-feature`). Index 0 is the block count.
370+static FILTER_INTRA_HITS: [std::sync::atomic::AtomicUsize; 6] =
371+ [const { std::sync::atomic::AtomicUsize::new(0) }; 6];
372+
373+/// Reads [`FILTER_INTRA_HITS`] and zeroes it, so a gate can attribute the
374+/// counts to its own encode.
375+pub(crate) fn take_filter_intra_hits() -> [usize; 6] {
376+ std::array::from_fn(|i| FILTER_INTRA_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))
377+}
378+
379+/// `filter_intra_mode_info` (spec 5.11.14) written out, mirrored symbol for
380+/// symbol from decode.rs `read_intra_mode`: a `use_filter_intra` flag on every
381+/// DC_PRED block whose sides `av1_filter_intra_allowed_bsize` admits (both <=
382+/// 32, [`crate::decode::filter_intra_size_class`]) and that took no luma
383+/// palette (`av1_filter_intra_allowed`, reconintra.h:77), then the chosen mode
384+/// of five. Sits between the palette COLOUR lists and the colour-index maps,
385+/// where libaom reads it (`av1_visit_palette` runs after
386+/// `read_filter_intra_mode_info`).
387+fn write_filter_intra(
388+ enc: &mut SymbolEncoder,
389+ cdfs: &mut Cdfs,
390+ mode: usize,
391+ side: usize,
392+ has_palette_y: bool,
393+ filter_intra: Option<u8>,
394+) {
395+ if !filter_intra_armed() || mode != DC_PRED || has_palette_y {
396+ debug_assert!(filter_intra.is_none(), "no `use_filter_intra` symbol exists here");
397+ return;
398+ }
399+ let Some(class) = crate::decode::filter_intra_size_class(side) else {
400+ debug_assert!(filter_intra.is_none(), "past av1_filter_intra_allowed_bsize");
401+ return;
402+ };
403+ enc.symbol(usize::from(filter_intra.is_some()), &mut cdfs.filter_intra[class]);
404+ if let Some(fi) = filter_intra {
405+ enc.symbol(usize::from(fi), &mut cdfs.filter_intra_mode);
406+ FILTER_INTRA_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
407+ FILTER_INTRA_HITS[usize::from(fi) + 1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
408+ }
409+}
410+
411+/// The coefficient-table row a block's luma transform types are coded from:
412+/// `fimode_to_intradir[filter_intra_mode]` for a filter-intra block, its own
413+/// luma mode otherwise (class `filter-intra tx_type row`, decode.rs
414+/// [`crate::decode::fi_tx_row`]). The block still PUBLISHES `DC_PRED` as its
415+/// neighbour mode -- libaom's `mbmi->mode` is unchanged by filter intra.
416+fn tx_row(mode: usize, filter_intra: Option<u8>) -> usize {
417+ crate::decode::fi_tx_row(mode, filter_intra.map(usize::from))
418+}
419+
346420 /// How many blocks this writer coded `use_intrabc == 1`, and the histogram of
347421 /// their DV magnitudes in full pels (index 1..=6 = 1, 2, 4, 8, 16, 32 pels
348422 /// and up, by the larger component) -- the fire count a gate prints so that
@@ -935,6 +1009,7 @@ impl Drop for CdefIdxGuard {
9351009 REFERENCE_SELECT.with(|c| c.set(false));
9361010 MOTION_MODE.with(|c| c.set(false));
9371011 INTRABC.with(|c| c.set(false));
1012+ FILTER_INTRA.with(|c| c.set(false));
9381013 INTRABC_GRID.with(|c| *c.borrow_mut() = None);
9391014 ORDER_HINTS.with(|c| c.set((7, 0, [0; 7])));
9401015 }
@@ -1452,6 +1527,14 @@ pub struct BlockCoeffs {
14521527 /// [`UV_CFL_PRED`]; at least one of the pair is nonzero, since the joint
14531528 /// sign symbol has no (ZERO, ZERO) value.
14541529 pub cfl_alphas: Option<(i32, i32)>,
1530+ /// This block's `filter_intra_mode` (spec 5.11.14, 0..=4) when it is
1531+ /// coded with recursive filter intra, or `None`. `Some` only on a DC_PRED
1532+ /// luma block of at most 32x32 with no luma palette, on a sequence whose
1533+ /// header set `enable_filter_intra` ([`arm_filter_intra`]); the block's
1534+ /// luma transform types are then coded from
1535+ /// `fimode_to_intradir[filter_intra_mode]`'s row ([`tx_row`]) while its
1536+ /// neighbour mode stays `DC_PRED`.
1537+ pub filter_intra: Option<u8>,
14551538 /// This block's `angle_delta_y` (spec `read_intra_angle_info`, -3..=3),
14561539 /// zero for every non-directional luma mode -- which is what
14571540 /// [`Default`] gives, and what every writer coded before lane-av1cfl.
@@ -3446,6 +3529,7 @@ fn write_intra_mode(
34463529 neighbours,
34473530 block.palette.as_ref(),
34483531 block.palette_uv.as_ref(),
3532+ block.filter_intra,
34493533 mode,
34503534 uv_mode,
34513535 mi,
@@ -3474,12 +3558,18 @@ fn write_palette_syntax(
34743558 neighbours: &mut Neighbours,
34753559 palette: Option<&PaletteY>,
34763560 palette_uv: Option<&PaletteUv>,
3561+ // lane-fintra: this block's chosen `filter_intra_mode`, or `None`. Written
3562+ // here rather than by the caller because its symbol sits BETWEEN the
3563+ // palette colour lists and the colour-index maps (decode.rs
3564+ // `read_intra_mode`), which is inside this function.
3565+ filter_intra: Option<u8>,
34773566 mode: usize,
34783567 uv_mode: usize,
34793568 mi: (usize, usize),
34803569 side: usize,
34813570 ) {
34823571 if !screen_armed() {
3572+ write_filter_intra(enc, cdfs, mode, side, false, filter_intra);
34833573 return;
34843574 }
34853575 let palette = palette.filter(|_| mode == DC_PRED);
@@ -3509,6 +3599,7 @@ fn write_palette_syntax(
35093599 enc.symbol(n - 2, &mut cdfs.palette_uv_size[bsize_ctx]);
35103600 write_palette_colors_uv(enc, &pal.u_colors[..n], &pal.v_colors[..n], &uv_cache);
35113601 }
3602+ write_filter_intra(enc, cdfs, mode, side, palette.is_some(), filter_intra);
35123603 // Both colour-index maps come after BOTH colour lists, in plane order
35133604 // (`av1_visit_palette` runs from the caller, past `filter_intra`).
35143605 if let Some(pal) = palette {
@@ -3527,6 +3618,10 @@ fn write_palette_syntax(
35273618 );
35283619 note_palette_uv(n);
35293620 }
3621+ } else {
3622+ // Below `av1_allow_palette`'s size bound there is no palette syntax at
3623+ // all, but `av1_filter_intra_allowed_bsize` still admits the block.
3624+ write_filter_intra(enc, cdfs, mode, side, false, filter_intra);
35303625 }
35313626 neighbours.pending_palette = palette.map(|p| (p.size, p.colors));
35323627 neighbours.pending_palette_uv = palette_uv.map(|p| (p.size, p.u_colors));
@@ -3629,6 +3724,10 @@ fn write_block(
36293724 at_mi,
36303725 side,
36313726 );
3727+ // The coefficient tables read `fimode_to_intradir[filter_intra_mode]`'s
3728+ // row on a filter-intra block; the neighbour publication below still gets
3729+ // the block's own `DC_PRED` ([`tx_row`]).
3730+ let tx_mode = tx_row(mode, block.filter_intra);
36323731 let split = if tx_select {
36333732 let tx = write_luma_select(
36343733 enc,
@@ -3638,7 +3737,7 @@ fn write_block(
36383737 side,
36393738 usize::from(block.tx_depth),
36403739 &grids[0],
3641- mode,
3740+ tx_mode,
36423741 all_scans,
36433742 )?;
36443743 tx < side
@@ -3652,7 +3751,7 @@ fn write_block(
36523751 grids,
36533752 &scans,
36543753 &neighbours.around(at, side),
3655- mode,
3754+ tx_mode,
36563755 tx_select,
36573756 );
36583757 neighbours.record_planes(at, side, mode, grids, !split);
@@ -3724,6 +3823,9 @@ fn write_leaf8(
37243823 // unchanged).
37253824 let mode = write_intra_mode(enc, cdfs, neighbours, block, above_mode, left_mode, true, leaf_mi, 8);
37263825 let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4];
3826+ // As [`write_block`]: the coefficient row follows the filter-intra mode,
3827+ // the mode this leaf publishes (and returns to the next leaf) does not.
3828+ let tx_mode = tx_row(mode, block.filter_intra);
37273829 let split = if tx_select {
37283830 let tx = write_luma_select(
37293831 enc,
@@ -3733,7 +3835,7 @@ fn write_leaf8(
37333835 8,
37343836 usize::from(block.tx_depth),
37353837 &grids[0],
3736- mode,
3838+ tx_mode,
37373839 all_scans,
37383840 )?;
37393841 tx < 8
@@ -3747,7 +3849,7 @@ fn write_leaf8(
37473849 grids,
37483850 &scans,
37493851 &neighbours.around_mi(leaf_mi, 8),
3750- mode,
3852+ tx_mode,
37513853 tx_select,
37523854 );
37533855 neighbours.record_mi_planes(leaf_mi, 8, grids, !split);
@@ -5811,7 +5913,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
58115913 if (V_PRED..=D67_PRED).contains(&uv_mode) {
58125914 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
58135915 }
5814- mode_for_tx = mode;
5916+ mode_for_tx = tx_row(mode, block.filter_intra);
58155917 // Intra: no vote, but still a coded cell -- mvstack's
58165918 // extended-scan coverage must see it (module doc).
58175919 let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8);
@@ -5821,6 +5923,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
58215923 &mut neighbours,
58225924 block.palette.as_ref(),
58235925 block.palette_uv.as_ref(),
5926+ block.filter_intra,
58245927 mode,
58255928 uv_mode,
58265929 (mi_row, mi_col),
@@ -6052,7 +6155,7 @@ fn write_inter_frame_leaf(
60526155 if (V_PRED..=D67_PRED).contains(&uv_mode) {
60536156 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
60546157 }
6055- mode_for_tx = mode;
6158+ mode_for_tx = tx_row(mode, block.filter_intra);
60566159 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
60576160 // coverage must see it (module doc).
60586161 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
@@ -6062,6 +6165,7 @@ fn write_inter_frame_leaf(
60626165 neighbours,
60636166 block.palette.as_ref(),
60646167 block.palette_uv.as_ref(),
6168+ block.filter_intra,
60656169 mode,
60666170 uv_mode,
60676171 (mi_row, mi_col),
@@ -6299,7 +6403,7 @@ fn write_inter_frame_leaf8(
62996403 if (V_PRED..=D67_PRED).contains(&uv_mode) {
63006404 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
63016405 }
6302- mode_for_tx = mode;
6406+ mode_for_tx = tx_row(mode, block.filter_intra);
63036407 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
63046408 // coverage must see it (module doc).
63056409 let (mi_row, mi_col) = leaf_mi;
@@ -6309,6 +6413,7 @@ fn write_inter_frame_leaf8(
63096413 neighbours,
63106414 block.palette.as_ref(),
63116415 block.palette_uv.as_ref(),
6416+ block.filter_intra,
63126417 mode,
63136418 uv_mode,
63146419 leaf_mi,