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 {
8274 /// `intra_ext_tx_cdf` row a luma transform reads. A filter-intra block's8274 /// `intra_ext_tx_cdf` row a luma transform reads. A filter-intra block's
8275 /// `mode` is always `DC_PRED`, so the row comes from the FILTER-INTRA mode8275 /// `mode` is always `DC_PRED`, so the row comes from the FILTER-INTRA mode
8276 /// instead (the defect commit 1a89ed9 fixed at the square sites).8276 /// 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 {
8278 const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED];8278 const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED];
8279 filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi])8279 filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi])
8280 }8280 }
@@ -8282,7 +8282,7 @@ fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize {
8282 /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`,8282 /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`,
8283 /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=328283 /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32
8284 /// bound (spec `filter_intra_mode_info` never reads a symbol there).8284 /// 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> {
8286 match side {8286 match side {
8287 4 => Some(0),8287 4 => Some(0),
8288 8 => Some(1),8288 8 => Some(1),
@@ -33466,7 +33466,9 @@ pub(crate) fn decode_inter_frame_tile(
33466 loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed,33466 loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed,
33467 enable_dual_filter, reference_select, tx_select,33467 enable_dual_filter, reference_select, tx_select,
33468 &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false,33468 &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,
33470 )33472 )
33471 }33473 }
33472 33474
@@ -33518,6 +33520,8 @@ pub(crate) fn decode_inter_frame_tile_lr(
33518 // `false` here desynced every stream this crate's encoder writes with the33520 // `false` here desynced every stream this crate's encoder writes with the
33519 // warp knob on: the same stale-header class as the parameter above.33521 // warp knob on: the same stale-header class as the parameter above.
33520 allow_warped_motion: bool,33522 allow_warped_motion: bool,
33523+ // lane-fintra: see [`decode_inter_frame_tiles_lr`]'s own parameter.
33524+ enable_filter_intra: bool,
33521 fctx: &crate::decode::FrameCtx,33525 fctx: &crate::decode::FrameCtx,
33522 ) -> Result<Picture> {33526 ) -> Result<Picture> {
33523 let single_tile = TileInfo {33527 let single_tile = TileInfo {
@@ -33548,6 +33552,7 @@ pub(crate) fn decode_inter_frame_tile_lr(
33548 allow_screen_content_tools,33552 allow_screen_content_tools,
33549 switchable_motion_mode,33553 switchable_motion_mode,
33550 allow_warped_motion,33554 allow_warped_motion,
33555+ enable_filter_intra,
33551 fctx,33556 fctx,
33552 )33557 )
33553 }33558 }
@@ -33589,6 +33594,12 @@ pub(crate) fn decode_inter_frame_tiles_lr(
33589 // `false` here desynced every stream this crate's encoder writes with the33594 // `false` here desynced every stream this crate's encoder writes with the
33590 // warp knob on: the same stale-header class as the parameter above.33595 // warp knob on: the same stale-header class as the parameter above.
33591 allow_warped_motion: bool,33596 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,
33592 fctx: &crate::decode::FrameCtx,33603 fctx: &crate::decode::FrameCtx,
33593 ) -> Result<Picture> {33604 ) -> Result<Picture> {
33594 decode_inter_frame_tile_with_cdfs(33605 decode_inter_frame_tile_with_cdfs(
@@ -33627,10 +33638,7 @@ pub(crate) fn decode_inter_frame_tiles_lr(
33627 allow_warped_motion,33638 allow_warped_motion,
33628 allow_screen_content_tools,33639 allow_screen_content_tools,
33629 DeltaParams::default(),33640 DeltaParams::default(),
33630- // `enable_filter_intra`: this crate's own encoder never writes the33641+ enable_filter_intra,
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,
33634 None,33642 None,
33635 fctx,33643 fctx,
33636 )33644 )
@@ -37618,7 +37626,10 @@ mod tests {
37618 base_q_idx,37626 base_q_idx,
37619 w as u32,37627 w as u32,
37620 h as u32,37628 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(),
37622 &cdef,37633 &cdef,
37623 &loop_filter,37634 &loop_filter,
37624 tx_select,37635 tx_select,
@@ -37988,7 +37999,10 @@ mod tests {
37988 encoded.base_q_idx,37999 encoded.base_q_idx,
37989 coded_w,38000 coded_w,
37990 coded_h,38001 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(),
37992 &encoded.cdef,38006 &encoded.cdef,
37993 &encoded.loop_filter,38007 &encoded.loop_filter,
37994 encoded.tx_select,38008 encoded.tx_select,
@@ -38032,7 +38046,10 @@ mod tests {
38032 encoded.base_q_idx,38046 encoded.base_q_idx,
38033 width as u32,38047 width as u32,
38034 height as u32,38048 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(),
38036 &encoded.cdef,38053 &encoded.cdef,
38037 &encoded.loop_filter,38054 &encoded.loop_filter,
38038 tx_select,38055 tx_select,
@@ -38117,7 +38134,10 @@ mod tests {
38117 key.base_q_idx,38134 key.base_q_idx,
38118 width as u32,38135 width as u32,
38119 height as u32,38136 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(),
38121 &key.cdef,38141 &key.cdef,
38122 &key.loop_filter,38142 &key.loop_filter,
38123 key.tx_select,38143 key.tx_select,
@@ -38183,6 +38203,9 @@ mod tests {
38183 // the warp bit must be the one it wrote too (lane-av1obmc2);38203 // the warp bit must be the one it wrote too (lane-av1obmc2);
38184 // the encoder only sets it under `EC_AV1_WARP`.38204 // the encoder only sets it under `EC_AV1_WARP`.
38185 crate::encode::warp_on(),38205 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(),
38186 fctx,38209 fctx,
38187 )38210 )
38188 .unwrap();38211 .unwrap();
@@ -38466,7 +38489,10 @@ mod tests {
38466 key.base_q_idx,38489 key.base_q_idx,
38467 coded_w,38490 coded_w,
38468 coded_h,38491 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(),
38470 &key.cdef,38496 &key.cdef,
38471 &key.loop_filter,38497 &key.loop_filter,
38472 key.tx_select,38498 key.tx_select,
@@ -38521,6 +38547,9 @@ mod tests {
38521 // the warp bit must be the one it wrote too (lane-av1obmc2);38547 // the warp bit must be the one it wrote too (lane-av1obmc2);
38522 // the encoder only sets it under `EC_AV1_WARP`.38548 // the encoder only sets it under `EC_AV1_WARP`.
38523 crate::encode::warp_on(),38549 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(),
38524 fctx,38553 fctx,
38525 )38554 )
38526 .unwrap();38555 .unwrap();
crates/ec-av1/src/encode.rsmodified+130 -1
@@ -756,6 +756,46 @@ fn intra_predict_u8(
756 }756 }
757 }757 }
758 758
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+
759 /// Pads one plane to `(padded_width, padded_height)` by repeating its last799 /// Pads one plane to `(padded_width, padded_height)` by repeating its last
760 /// row and column, so the block coder always sees a whole number of blocks.800 /// row and column, so the block coder always sees a whole number of blocks.
761 /// [`crop_plane`] undoes this on the way back out.801 /// [`crop_plane`] undoes this on the way back out.
@@ -1183,7 +1223,11 @@ pub(crate) fn key_frame_headers_colour(
1183 max_frame_width: w,1223 max_frame_width: w,
1184 max_frame_height: h,1224 max_frame_height: h,
1185 use_128x128_superblock: false,1225 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(),
1187 enable_intra_edge_filter: false,1231 enable_intra_edge_filter: false,
1188 enable_order_hint: true,1232 enable_order_hint: true,
1189 order_hint_bits: 7,1233 order_hint_bits: 7,
@@ -3860,6 +3904,73 @@ fn code_square(
3860 }3904 }
3861 }3905 }
3862 3906
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+
3863 // Chroma searches its own mode over [`CHROMA_MODES`], one symbol for both3974 // Chroma searches its own mode over [`CHROMA_MODES`], one symbol for both
3864 // planes; what it costs still counts towards the partition decision.3975 // planes; what it costs still counts towards the partition decision.
3865 let ([u, v], uv_mode, chroma_cost, palette_uv, cfl_alphas) =3976 let ([u, v], uv_mode, chroma_cost, palette_uv, cfl_alphas) =
@@ -3898,6 +4009,7 @@ fn code_square(
3898 tx_depth,4009 tx_depth,
3899 palette,4010 palette,
3900 palette_uv,4011 palette_uv,
4012+ filter_intra,
3901 cfl_alphas,4013 cfl_alphas,
3902 angle_delta_y: angle_delta as i8,4014 angle_delta_y: angle_delta as i8,
3903 ..BlockCoeffs::default()4015 ..BlockCoeffs::default()
@@ -3975,6 +4087,16 @@ fn cfl_on() -> bool {
3975 *ON.get_or_init(|| std::env::var("EC_AV1_CFL").as_deref() != Ok("0"))4087 *ON.get_or_init(|| std::env::var("EC_AV1_CFL").as_deref() != Ok("0"))
3976 }4088 }
3977 4089
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+
3978 /// Reads [`UV_MODE_HITS`] and zeroes it, so a gate can attribute the counts4100 /// Reads [`UV_MODE_HITS`] and zeroes it, so a gate can attribute the counts
3979 /// to its own encode.4101 /// to its own encode.
3980 #[cfg(test)]4102 #[cfg(test)]
@@ -5086,6 +5208,7 @@ fn code_square_inter(
5086 BlockCoeffs {5208 BlockCoeffs {
5087 angle_delta_y: 0,5209 angle_delta_y: 0,
5088 cfl_alphas: None,5210 cfl_alphas: None,
5211+ filter_intra: None,
5089 luma: coeffs(&levels, side),5212 luma: coeffs(&levels, side),
5090 u: coeffs(&u_new.levels, side / 2),5213 u: coeffs(&u_new.levels, side / 2),
5091 v: coeffs(&v_new.levels, side / 2),5214 v: coeffs(&v_new.levels, side / 2),
@@ -5121,6 +5244,7 @@ fn code_square_inter(
5121 (BlockCoeffs {5244 (BlockCoeffs {
5122 angle_delta_y: 0,5245 angle_delta_y: 0,
5123 cfl_alphas: None,5246 cfl_alphas: None,
5247+ filter_intra: None,
5124 luma: coeffs(&levels, side),5248 luma: coeffs(&levels, side),
5125 u: coeffs(&u.levels, side / 2),5249 u: coeffs(&u.levels, side / 2),
5126 v: coeffs(&v.levels, side / 2),5250 v: coeffs(&v.levels, side / 2),
@@ -6223,6 +6347,7 @@ pub(crate) fn encode_key_frame_inner(
6223 crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec());6347 crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec());
6224 let mut cdfs = start_cdfs.0.clone();6348 let mut cdfs = start_cdfs.0.clone();
6225 crate::tile::arm_screen(screen);6349 crate::tile::arm_screen(screen);
6350+ crate::tile::arm_filter_intra(filter_intra_on());
6226 crate::tile::arm_intrabc(allow_intrabc);6351 crate::tile::arm_intrabc(allow_intrabc);
6227 crate::tile::arm_pricing_cdfs(None, false);6352 crate::tile::arm_pricing_cdfs(None, false);
6228 let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs(6353 let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs(
@@ -7829,6 +7954,7 @@ fn search_inter_block(
7829 BlockCoeffs {7954 BlockCoeffs {
7830 angle_delta_y: 0,7955 angle_delta_y: 0,
7831 cfl_alphas: None,7956 cfl_alphas: None,
7957+ filter_intra: None,
7832 luma: coeffs(&levels, BLOCK),7958 luma: coeffs(&levels, BLOCK),
7833 u: coeffs(&best.u.levels, BLOCK / 2),7959 u: coeffs(&best.u.levels, BLOCK / 2),
7834 v: coeffs(&best.v.levels, BLOCK / 2),7960 v: coeffs(&best.v.levels, BLOCK / 2),
@@ -9079,6 +9205,7 @@ pub(crate) fn encode_inter_frame(
9079 crate::tile::arm_warped_motion(warped_motion);9205 crate::tile::arm_warped_motion(warped_motion);
9080 crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints);9206 crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints);
9081 crate::tile::arm_screen(screen);9207 crate::tile::arm_screen(screen);
9208+ crate::tile::arm_filter_intra(filter_intra_on());
9082 let mut cdfs = start_cdfs.0.clone();9209 let mut cdfs = start_cdfs.0.clone();
9083 crate::tile::arm_pricing_cdfs(Some(&cdfs), screen);9210 crate::tile::arm_pricing_cdfs(Some(&cdfs), screen);
9084 // Each tile's own MV grid: a candidate scan never reaches past9211 // Each tile's own MV grid: a candidate scan never reaches past
@@ -9205,6 +9332,7 @@ pub(crate) fn encode_inter_frame(
9205 h.allow_screen_content_tools,9332 h.allow_screen_content_tools,
9206 switchable_motion_mode,9333 switchable_motion_mode,
9207 h.allow_warped_motion,9334 h.allow_warped_motion,
9335+ seq.enable_filter_intra,
9208 fctx,9336 fctx,
9209 )9337 )
9210 },9338 },
@@ -10083,6 +10211,7 @@ mod tests {
10083 let residual_block = BlockCoeffs {10211 let residual_block = BlockCoeffs {
10084 angle_delta_y: 0,10212 angle_delta_y: 0,
10085 cfl_alphas: None,10213 cfl_alphas: None,
10214+ filter_intra: None,
10086 luma: vec![Coeff {10215 luma: vec![Coeff {
10087 row: 0,10216 row: 0,
10088 col: 0,10217 col: 0,
crates/ec-av1/src/stream.rsmodified+4 -1
@@ -2993,7 +2993,10 @@ pub(crate) mod tests {
2993 encoded.base_q_idx,2993 encoded.base_q_idx,
2994 width as u32,2994 width as u32,
2995 height as u32,2995 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(),
2997 &encoded.cdef,3000 &encoded.cdef,
2998 &encoded.loop_filter,3001 &encoded.loop_filter,
2999 encoded.tx_select,3002 encoded.tx_select,
crates/ec-av1/src/tile.rsmodified+112 -7
@@ -343,6 +343,80 @@ fn intrabc_armed() -> bool {
343 INTRABC.with(std::cell::Cell::get)343 INTRABC.with(std::cell::Cell::get)
344 }344 }
345 345
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+
346 /// How many blocks this writer coded `use_intrabc == 1`, and the histogram of420 /// How many blocks this writer coded `use_intrabc == 1`, and the histogram of
347 /// their DV magnitudes in full pels (index 1..=6 = 1, 2, 4, 8, 16, 32 pels421 /// their DV magnitudes in full pels (index 1..=6 = 1, 2, 4, 8, 16, 32 pels
348 /// and up, by the larger component) -- the fire count a gate prints so that422 /// and up, by the larger component) -- the fire count a gate prints so that
@@ -935,6 +1009,7 @@ impl Drop for CdefIdxGuard {
935 REFERENCE_SELECT.with(|c| c.set(false));1009 REFERENCE_SELECT.with(|c| c.set(false));
936 MOTION_MODE.with(|c| c.set(false));1010 MOTION_MODE.with(|c| c.set(false));
937 INTRABC.with(|c| c.set(false));1011 INTRABC.with(|c| c.set(false));
1012+ FILTER_INTRA.with(|c| c.set(false));
938 INTRABC_GRID.with(|c| *c.borrow_mut() = None);1013 INTRABC_GRID.with(|c| *c.borrow_mut() = None);
939 ORDER_HINTS.with(|c| c.set((7, 0, [0; 7])));1014 ORDER_HINTS.with(|c| c.set((7, 0, [0; 7])));
940 }1015 }
@@ -1452,6 +1527,14 @@ pub struct BlockCoeffs {
1452 /// [`UV_CFL_PRED`]; at least one of the pair is nonzero, since the joint1527 /// [`UV_CFL_PRED`]; at least one of the pair is nonzero, since the joint
1453 /// sign symbol has no (ZERO, ZERO) value.1528 /// sign symbol has no (ZERO, ZERO) value.
1454 pub cfl_alphas: Option<(i32, i32)>,1529 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>,
1455 /// This block's `angle_delta_y` (spec `read_intra_angle_info`, -3..=3),1538 /// This block's `angle_delta_y` (spec `read_intra_angle_info`, -3..=3),
1456 /// zero for every non-directional luma mode -- which is what1539 /// zero for every non-directional luma mode -- which is what
1457 /// [`Default`] gives, and what every writer coded before lane-av1cfl.1540 /// [`Default`] gives, and what every writer coded before lane-av1cfl.
@@ -3446,6 +3529,7 @@ fn write_intra_mode(
3446 neighbours,3529 neighbours,
3447 block.palette.as_ref(),3530 block.palette.as_ref(),
3448 block.palette_uv.as_ref(),3531 block.palette_uv.as_ref(),
3532+ block.filter_intra,
3449 mode,3533 mode,
3450 uv_mode,3534 uv_mode,
3451 mi,3535 mi,
@@ -3474,12 +3558,18 @@ fn write_palette_syntax(
3474 neighbours: &mut Neighbours,3558 neighbours: &mut Neighbours,
3475 palette: Option<&PaletteY>,3559 palette: Option<&PaletteY>,
3476 palette_uv: Option<&PaletteUv>,3560 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>,
3477 mode: usize,3566 mode: usize,
3478 uv_mode: usize,3567 uv_mode: usize,
3479 mi: (usize, usize),3568 mi: (usize, usize),
3480 side: usize,3569 side: usize,
3481 ) {3570 ) {
3482 if !screen_armed() {3571 if !screen_armed() {
3572+ write_filter_intra(enc, cdfs, mode, side, false, filter_intra);
3483 return;3573 return;
3484 }3574 }
3485 let palette = palette.filter(|_| mode == DC_PRED);3575 let palette = palette.filter(|_| mode == DC_PRED);
@@ -3509,6 +3599,7 @@ fn write_palette_syntax(
3509 enc.symbol(n - 2, &mut cdfs.palette_uv_size[bsize_ctx]);3599 enc.symbol(n - 2, &mut cdfs.palette_uv_size[bsize_ctx]);
3510 write_palette_colors_uv(enc, &pal.u_colors[..n], &pal.v_colors[..n], &uv_cache);3600 write_palette_colors_uv(enc, &pal.u_colors[..n], &pal.v_colors[..n], &uv_cache);
3511 }3601 }
3602+ write_filter_intra(enc, cdfs, mode, side, palette.is_some(), filter_intra);
3512 // Both colour-index maps come after BOTH colour lists, in plane order3603 // Both colour-index maps come after BOTH colour lists, in plane order
3513 // (`av1_visit_palette` runs from the caller, past `filter_intra`).3604 // (`av1_visit_palette` runs from the caller, past `filter_intra`).
3514 if let Some(pal) = palette {3605 if let Some(pal) = palette {
@@ -3527,6 +3618,10 @@ fn write_palette_syntax(
3527 );3618 );
3528 note_palette_uv(n);3619 note_palette_uv(n);
3529 }3620 }
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);
3530 }3625 }
3531 neighbours.pending_palette = palette.map(|p| (p.size, p.colors));3626 neighbours.pending_palette = palette.map(|p| (p.size, p.colors));
3532 neighbours.pending_palette_uv = palette_uv.map(|p| (p.size, p.u_colors));3627 neighbours.pending_palette_uv = palette_uv.map(|p| (p.size, p.u_colors));
@@ -3629,6 +3724,10 @@ fn write_block(
3629 at_mi,3724 at_mi,
3630 side,3725 side,
3631 );3726 );
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);
3632 let split = if tx_select {3731 let split = if tx_select {
3633 let tx = write_luma_select(3732 let tx = write_luma_select(
3634 enc,3733 enc,
@@ -3638,7 +3737,7 @@ fn write_block(
3638 side,3737 side,
3639 usize::from(block.tx_depth),3738 usize::from(block.tx_depth),
3640 &grids[0],3739 &grids[0],
3641- mode,3740+ tx_mode,
3642 all_scans,3741 all_scans,
3643 )?;3742 )?;
3644 tx < side3743 tx < side
@@ -3652,7 +3751,7 @@ fn write_block(
3652 grids,3751 grids,
3653 &scans,3752 &scans,
3654 &neighbours.around(at, side),3753 &neighbours.around(at, side),
3655- mode,3754+ tx_mode,
3656 tx_select,3755 tx_select,
3657 );3756 );
3658 neighbours.record_planes(at, side, mode, grids, !split);3757 neighbours.record_planes(at, side, mode, grids, !split);
@@ -3724,6 +3823,9 @@ fn write_leaf8(
3724 // unchanged).3823 // unchanged).
3725 let mode = write_intra_mode(enc, cdfs, neighbours, block, above_mode, left_mode, true, leaf_mi, 8);3824 let mode = write_intra_mode(enc, cdfs, neighbours, block, above_mode, left_mode, true, leaf_mi, 8);
3726 let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4];3825 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);
3727 let split = if tx_select {3829 let split = if tx_select {
3728 let tx = write_luma_select(3830 let tx = write_luma_select(
3729 enc,3831 enc,
@@ -3733,7 +3835,7 @@ fn write_leaf8(
3733 8,3835 8,
3734 usize::from(block.tx_depth),3836 usize::from(block.tx_depth),
3735 &grids[0],3837 &grids[0],
3736- mode,3838+ tx_mode,
3737 all_scans,3839 all_scans,
3738 )?;3840 )?;
3739 tx < 83841 tx < 8
@@ -3747,7 +3849,7 @@ fn write_leaf8(
3747 grids,3849 grids,
3748 &scans,3850 &scans,
3749 &neighbours.around_mi(leaf_mi, 8),3851 &neighbours.around_mi(leaf_mi, 8),
3750- mode,3852+ tx_mode,
3751 tx_select,3853 tx_select,
3752 );3854 );
3753 neighbours.record_mi_planes(leaf_mi, 8, grids, !split);3855 neighbours.record_mi_planes(leaf_mi, 8, grids, !split);
@@ -5811,7 +5913,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
5811 if (V_PRED..=D67_PRED).contains(&uv_mode) {5913 if (V_PRED..=D67_PRED).contains(&uv_mode) {
5812 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);5914 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
5813 }5915 }
5814- mode_for_tx = mode;5916+ mode_for_tx = tx_row(mode, block.filter_intra);
5815 // Intra: no vote, but still a coded cell -- mvstack's5917 // Intra: no vote, but still a coded cell -- mvstack's
5816 // extended-scan coverage must see it (module doc).5918 // extended-scan coverage must see it (module doc).
5817 let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8);5919 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(
5821 &mut neighbours,5923 &mut neighbours,
5822 block.palette.as_ref(),5924 block.palette.as_ref(),
5823 block.palette_uv.as_ref(),5925 block.palette_uv.as_ref(),
5926+ block.filter_intra,
5824 mode,5927 mode,
5825 uv_mode,5928 uv_mode,
5826 (mi_row, mi_col),5929 (mi_row, mi_col),
@@ -6052,7 +6155,7 @@ fn write_inter_frame_leaf(
6052 if (V_PRED..=D67_PRED).contains(&uv_mode) {6155 if (V_PRED..=D67_PRED).contains(&uv_mode) {
6053 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);6156 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
6054 }6157 }
6055- mode_for_tx = mode;6158+ mode_for_tx = tx_row(mode, block.filter_intra);
6056 // Intra: no vote, but still a coded cell -- mvstack's extended-scan6159 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
6057 // coverage must see it (module doc).6160 // coverage must see it (module doc).
6058 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);6161 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
@@ -6062,6 +6165,7 @@ fn write_inter_frame_leaf(
6062 neighbours,6165 neighbours,
6063 block.palette.as_ref(),6166 block.palette.as_ref(),
6064 block.palette_uv.as_ref(),6167 block.palette_uv.as_ref(),
6168+ block.filter_intra,
6065 mode,6169 mode,
6066 uv_mode,6170 uv_mode,
6067 (mi_row, mi_col),6171 (mi_row, mi_col),
@@ -6299,7 +6403,7 @@ fn write_inter_frame_leaf8(
6299 if (V_PRED..=D67_PRED).contains(&uv_mode) {6403 if (V_PRED..=D67_PRED).contains(&uv_mode) {
6300 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);6404 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
6301 }6405 }
6302- mode_for_tx = mode;6406+ mode_for_tx = tx_row(mode, block.filter_intra);
6303 // Intra: no vote, but still a coded cell -- mvstack's extended-scan6407 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
6304 // coverage must see it (module doc).6408 // coverage must see it (module doc).
6305 let (mi_row, mi_col) = leaf_mi;6409 let (mi_row, mi_col) = leaf_mi;
@@ -6309,6 +6413,7 @@ fn write_inter_frame_leaf8(
6309 neighbours,6413 neighbours,
6310 block.palette.as_ref(),6414 block.palette.as_ref(),
6311 block.palette_uv.as_ref(),6415 block.palette_uv.as_ref(),
6416+ block.filter_intra,
6312 mode,6417 mode,
6313 uv_mode,6418 uv_mode,
6314 leaf_mi,6419 leaf_mi,