forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-fintra' (88c5ef0f): filter intra as an intra-search candidate, default on; enable_filter_intra threaded through every raw decode of the encoder's own tiles

Browse files at this commit

9b5376d7bbf7ddbfcd274cfcaf2a7dabf7ed77f7Ahmet Kaan GümüşParent: f60b30248bcb5c
Native table: film A +53.2/+23.4 -> +52.8/+23.3, film B +81.5/+46.5 ->
+81.0/+46.1, screen flat. Pins 7373 / 27074. Six gate decodes passed a
literal false for the header bit (stale-header class, swept).
crates/ec-av1/src/decode.rsmodified+103 -12
@@ -8288,7 +8288,7 @@ impl Neighbours {
82888288 /// `intra_ext_tx_cdf` row a luma transform reads. A filter-intra block's
82898289 /// `mode` is always `DC_PRED`, so the row comes from the FILTER-INTRA mode
82908290 /// instead (the defect commit 1a89ed9 fixed at the square sites).
8291-fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize {
8291+pub(crate) fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize {
82928292 const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED];
82938293 filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi])
82948294 }
@@ -8296,7 +8296,7 @@ fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize {
82968296 /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`,
82978297 /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32
82988298 /// bound (spec `filter_intra_mode_info` never reads a symbol there).
8299-fn filter_intra_size_class(side: usize) -> Option<usize> {
8299+pub(crate) fn filter_intra_size_class(side: usize) -> Option<usize> {
83008300 match side {
83018301 4 => Some(0),
83028302 8 => Some(1),
@@ -33480,7 +33480,9 @@ pub(crate) fn decode_inter_frame_tile(
3348033480 loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed,
3348133481 enable_dual_filter, reference_select, tx_select,
3348233482 &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false,
33483- switchable_motion_mode, false, fctx,
33483+ // `enable_filter_intra`: as the `false`s beside it, this convenience
33484+ // entry names a stream written without the sequence bit (lane-fintra).
33485+ switchable_motion_mode, false, false, fctx,
3348433486 )
3348533487 }
3348633488
@@ -33532,6 +33534,8 @@ pub(crate) fn decode_inter_frame_tile_lr(
3353233534 // `false` here desynced every stream this crate's encoder writes with the
3353333535 // warp knob on: the same stale-header class as the parameter above.
3353433536 allow_warped_motion: bool,
33537+ // lane-fintra: see [`decode_inter_frame_tiles_lr`]'s own parameter.
33538+ enable_filter_intra: bool,
3353533539 fctx: &crate::decode::FrameCtx,
3353633540 ) -> Result<Picture> {
3353733541 let single_tile = TileInfo {
@@ -33562,6 +33566,7 @@ pub(crate) fn decode_inter_frame_tile_lr(
3356233566 allow_screen_content_tools,
3356333567 switchable_motion_mode,
3356433568 allow_warped_motion,
33569+ enable_filter_intra,
3356533570 fctx,
3356633571 )
3356733572 }
@@ -33603,6 +33608,12 @@ pub(crate) fn decode_inter_frame_tiles_lr(
3360333608 // `false` here desynced every stream this crate's encoder writes with the
3360433609 // warp knob on: the same stale-header class as the parameter above.
3360533610 allow_warped_motion: bool,
33611+ // lane-fintra: this sequence header's own `enable_filter_intra` bit (spec
33612+ // 5.5.2). Hard-coded `false` here until this lane -- the same stale-header
33613+ // class as `switchable_motion_mode`/`allow_warped_motion` above: the
33614+ // encoder's own trial decode desyncs at the first intra-in-inter DC_PRED
33615+ // block the moment its sequence header sets the bit.
33616+ enable_filter_intra: bool,
3360633617 fctx: &crate::decode::FrameCtx,
3360733618 ) -> Result<Picture> {
3360833619 decode_inter_frame_tile_with_cdfs(
@@ -33641,10 +33652,7 @@ pub(crate) fn decode_inter_frame_tiles_lr(
3364133652 allow_warped_motion,
3364233653 allow_screen_content_tools,
3364333654 DeltaParams::default(),
33644- // `enable_filter_intra`: this crate's own encoder never writes the
33645- // sequence bit (`encode.rs`), so no intra-in-inter block of a stream
33646- // reaching this entry point reads a `use_filter_intra` symbol.
33647- false,
33655+ enable_filter_intra,
3364833656 None,
3364933657 fctx,
3365033658 )
@@ -37632,7 +37640,10 @@ mod tests {
3763237640 base_q_idx,
3763337641 w as u32,
3763437642 h as u32,
37635- false,
37643+ // lane-fintra: the sequence bit the encoder wrote this tile under
37644+ // (stale-header class -- a hard-coded `false` here reads one symbol
37645+ // short at the first DC_PRED block once the search offers filter intra).
37646+ crate::encode::filter_intra_on(),
3763637647 &cdef,
3763737648 &loop_filter,
3763837649 tx_select,
@@ -38002,7 +38013,10 @@ mod tests {
3800238013 encoded.base_q_idx,
3800338014 coded_w,
3800438015 coded_h,
38005- false,
38016+ // lane-fintra: the sequence bit the encoder wrote this tile under
38017+ // (stale-header class -- a hard-coded `false` here reads one symbol
38018+ // short at the first DC_PRED block once the search offers filter intra).
38019+ crate::encode::filter_intra_on(),
3800638020 &encoded.cdef,
3800738021 &encoded.loop_filter,
3800838022 encoded.tx_select,
@@ -38018,6 +38032,68 @@ mod tests {
3801838032 }
3801938033 }
3802038034
38035+ /// lane-fintra: the encoder's own filter-intra blocks, read back by BOTH
38036+ /// decoders. Two claims in one gate: the stream really exercises the tool
38037+ /// (the decoder's own `use_filter_intra == 1` counter moves, class
38038+ /// `gate-blind-to-feature` -- a feature that never fires is not measured),
38039+ /// and every sample of the frame that carries them matches ffmpeg's
38040+ /// (class `fixture-proves-symbol-not-signal`: the symbol alone would not
38041+ /// prove the recursive predictor, the tx_type row or the syntax position
38042+ /// between the palette colours and the colour-index maps).
38043+ #[test]
38044+ fn a_filter_intra_key_frame_decodes_pixel_exact_through_ffmpeg() {
38045+ let fctx = &crate::decode::FrameCtx::new();
38046+ if !crate::encode::filter_intra_on() {
38047+ eprintln!("SKIP a_filter_intra_key_frame...: EC_AV1_FILTER_INTRA=0");
38048+ return;
38049+ }
38050+ if !have_ffmpeg() {
38051+ eprintln!("SKIP a_filter_intra_key_frame...: no ffmpeg");
38052+ return;
38053+ }
38054+ let (width, height) = (128usize, 128usize);
38055+ // Smooth, slowly curving content: what the recursive filter modes are
38056+ // for, and what a plain directional prediction leaves residual on.
38057+ let mut picture = crate::encode::Picture::grey(width, height);
38058+ for row in 0..height {
38059+ for col in 0..width {
38060+ let v = 128.0
38061+ + 60.0 * ((row as f64) / 9.0).sin() * ((col as f64) / 11.0).cos()
38062+ + 8.0 * ((row + col) as f64 / 3.0).sin();
38063+ picture.y[row * width + col] = v.clamp(0.0, 255.0) as u16;
38064+ }
38065+ }
38066+ let encoded = crate::encode::encode_key_frame_with_ctx(&picture, 100, 0.5, fctx).unwrap();
38067+ let (coded_w, coded_h) = ffprobe_size(&encoded.stream);
38068+ let before = filter_intra_hits();
38069+ let ours = decode_key_frame_tile_lr(
38070+ &encoded.tile,
38071+ encoded.mi_cols,
38072+ encoded.mi_rows,
38073+ encoded.base_q_idx,
38074+ coded_w,
38075+ coded_h,
38076+ crate::encode::filter_intra_on(),
38077+ &encoded.cdef,
38078+ &encoded.loop_filter,
38079+ encoded.tx_select,
38080+ true,
38081+ encoded.screen,
38082+ encoded.allow_intrabc,
38083+ &encoded.loop_restoration,
38084+ fctx,
38085+ )
38086+ .unwrap();
38087+ let fired = filter_intra_hits() - before;
38088+ assert!(fired > 0, "no block took filter intra -- the gate is blind to the feature");
38089+ eprintln!("a_filter_intra_key_frame: {fired} filter-intra blocks");
38090+ let ffmpeg_decoded = ffmpeg_decode(&encoded.stream, coded_w as usize, coded_h as usize);
38091+ assert_eq!(ours.y, ffmpeg_decoded.y, "luma vs ffmpeg");
38092+ assert_eq!(ours.u, ffmpeg_decoded.u, "U vs ffmpeg");
38093+ assert_eq!(ours.v, ffmpeg_decoded.v, "V vs ffmpeg");
38094+ assert_eq!(ours.y, encoded.reconstruction.y, "luma vs the encoder's own reconstruction");
38095+ }
38096+
3802138097 /// The key frame the encoder writes carries `tx_mode == TxMode::Select`,
3802238098 /// and that header bit is load-bearing for a decode of its tile: read as a
3802338099 /// `TxMode::Largest` frame the very first block misses its `tx_depth`
@@ -38046,7 +38122,10 @@ mod tests {
3804638122 encoded.base_q_idx,
3804738123 width as u32,
3804838124 height as u32,
38049- false,
38125+ // lane-fintra: the sequence bit the encoder wrote this tile under
38126+ // (stale-header class -- a hard-coded `false` here reads one symbol
38127+ // short at the first DC_PRED block once the search offers filter intra).
38128+ crate::encode::filter_intra_on(),
3805038129 &encoded.cdef,
3805138130 &encoded.loop_filter,
3805238131 tx_select,
@@ -38131,7 +38210,10 @@ mod tests {
3813138210 key.base_q_idx,
3813238211 width as u32,
3813338212 height as u32,
38134- false,
38213+ // lane-fintra: the sequence bit the encoder wrote this tile under
38214+ // (stale-header class -- a hard-coded `false` here reads one symbol
38215+ // short at the first DC_PRED block once the search offers filter intra).
38216+ crate::encode::filter_intra_on(),
3813538217 &key.cdef,
3813638218 &key.loop_filter,
3813738219 key.tx_select,
@@ -38197,6 +38279,9 @@ mod tests {
3819738279 // the warp bit must be the one it wrote too (lane-av1obmc2);
3819838280 // the encoder only sets it under `EC_AV1_WARP`.
3819938281 crate::encode::warp_on(),
38282+ // lane-fintra: the sequence bit the encoder wrote this tile
38283+ // under -- same stale-header class as the warp bit above.
38284+ crate::encode::filter_intra_on(),
3820038285 fctx,
3820138286 )
3820238287 .unwrap();
@@ -38480,7 +38565,10 @@ mod tests {
3848038565 key.base_q_idx,
3848138566 coded_w,
3848238567 coded_h,
38483- false,
38568+ // lane-fintra: the sequence bit the encoder wrote this tile under
38569+ // (stale-header class -- a hard-coded `false` here reads one symbol
38570+ // short at the first DC_PRED block once the search offers filter intra).
38571+ crate::encode::filter_intra_on(),
3848438572 &key.cdef,
3848538573 &key.loop_filter,
3848638574 key.tx_select,
@@ -38535,6 +38623,9 @@ mod tests {
3853538623 // the warp bit must be the one it wrote too (lane-av1obmc2);
3853638624 // the encoder only sets it under `EC_AV1_WARP`.
3853738625 crate::encode::warp_on(),
38626+ // lane-fintra: the sequence bit the encoder wrote this tile
38627+ // under -- same stale-header class as the warp bit above.
38628+ crate::encode::filter_intra_on(),
3853838629 fctx,
3853938630 )
3854038631 .unwrap();
crates/ec-av1/src/encode.rsmodified+187 -2
@@ -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.
@@ -982,6 +1022,20 @@ pub(crate) fn force_screen(value: Option<bool>) {
9821022 SCREEN_FORCE.with(|c| c.set(value));
9831023 }
9841024
1025+// lane-fintra: [`force_screen`] for the filter-intra candidate, so a MODE
1026+// ablation can measure the mode search alone. Setting it also clears the
1027+// sequence header's own `enable_filter_intra` bit, so the stream the arm
1028+// writes is exactly the one this encoder wrote before the lane.
1029+#[cfg(test)]
1030+thread_local! {
1031+ static FILTER_INTRA_FORCE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
1032+}
1033+
1034+#[cfg(test)]
1035+pub(crate) fn force_filter_intra(value: Option<bool>) {
1036+ FILTER_INTRA_FORCE.with(|c| c.set(value));
1037+}
1038+
9851039 pub(crate) fn arm_seq_screen(on: bool) {
9861040 SEQ_SCREEN.with(|c| c.set(on));
9871041 }
@@ -1183,7 +1237,11 @@ pub(crate) fn key_frame_headers_colour(
11831237 max_frame_width: w,
11841238 max_frame_height: h,
11851239 use_128x128_superblock: false,
1186- enable_filter_intra: false,
1240+ // lane-fintra: the encoder's intra search offers the five recursive
1241+ // filter-intra modes on eligible blocks, so the sequence bit is set
1242+ // whenever that search is on (`EC_AV1_FILTER_INTRA=0` switches both
1243+ // off and restores the streams this encoder wrote before the lane).
1244+ enable_filter_intra: filter_intra_on(),
11871245 enable_intra_edge_filter: false,
11881246 enable_order_hint: true,
11891247 order_hint_bits: 7,
@@ -3860,6 +3918,81 @@ fn code_square(
38603918 }
38613919 }
38623920
3921+ // The five recursive filter-intra modes (spec 5.11.14), priced against
3922+ // whatever the mode, transform-depth and palette searches settled on --
3923+ // the same "one candidate loop after the incumbent is known" shape the
3924+ // palette above uses. A block that takes one is `DC_PRED` with no palette
3925+ // by construction (`av1_filter_intra_allowed`), and its luma transform
3926+ // types are coded from `fimode_to_intradir`'s row (`crate::tile::tx_row`).
3927+ //
3928+ // corner-cut, ceiling named: the candidate is only ever priced at
3929+ // `tx_depth` 0 (one transform over the whole block), so a block whose
3930+ // ordinary intra winner wanted a SPLIT transform is compared against a
3931+ // handicapped filter-intra arm -- and only 19-44% of this encoder's intra
3932+ // blocks code depth 0. Upgrade path: predict per transform unit inside
3933+ // [`Plane::code_tx_depth`] (the decoder already reads `filter_intra` per
3934+ // unit, decode.rs:11835) and run the depth search under the winning
3935+ // filter mode.
3936+ let mut filter_intra: Option<u8> = None;
3937+ if filter_intra_on()
3938+ && palette.is_none()
3939+ && let Some(class) = crate::decode::filter_intra_size_class(side)
3940+ {
3941+ let row = &cdf::FILTER_INTRA[class];
3942+ // With the sequence bit on, EVERY `DC_PRED` block without a luma
3943+ // palette carries the flag, so the incumbent pays its zero before the
3944+ // comparison -- otherwise the candidate is charged a bit the block
3945+ // spends either way (class `price-the-narrowing-not-the-table`).
3946+ if mode == DC_PRED {
3947+ cost += search.lambda * symbol_bits(row, 0);
3948+ }
3949+ let one_bits = symbol_bits(row, 1);
3950+ let kept = luma.snapshot(x, y, side);
3951+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
3952+ let (above, left, corner) =
3953+ luma.edges_into(x, y, side, at.reach, &mut above_buf, &mut left_buf);
3954+ let mut prediction = vec![0u8; side * side];
3955+ for fi in 0..5u8 {
3956+ filter_intra_predict_u8(
3957+ usize::from(fi),
3958+ above,
3959+ left,
3960+ corner,
3961+ side,
3962+ &mut prediction, fctx,
3963+ );
3964+ let trial = luma.code_from_prediction(
3965+ x,
3966+ y,
3967+ side,
3968+ &prediction,
3969+ false,
3970+ search.base_q_idx,
3971+ search.deadzone,
3972+ luma_set,
3973+ );
3974+ let cost_f = trial.sse
3975+ + search.lambda
3976+ * (trial.bits
3977+ + mode_bits[DC_PRED as usize]
3978+ + one_bits
3979+ + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))
3980+ + if tx_select { tx_depth_bits(side, 0) } else { 0.0 });
3981+ if cost_f < cost {
3982+ cost = cost_f;
3983+ mode = DC_PRED;
3984+ angle_delta = 0;
3985+ tx_depth = 0;
3986+ luma_coeffs = coeffs(&trial.levels, side);
3987+ luma.commit(x, y, side, &trial);
3988+ filter_intra = Some(fi);
3989+ }
3990+ }
3991+ if filter_intra.is_none() {
3992+ luma.restore(x, y, side, &kept);
3993+ }
3994+ }
3995+
38633996 // Chroma searches its own mode over [`CHROMA_MODES`], one symbol for both
38643997 // planes; what it costs still counts towards the partition decision.
38653998 let ([u, v], uv_mode, chroma_cost, palette_uv, cfl_alphas) =
@@ -3898,6 +4031,7 @@ fn code_square(
38984031 tx_depth,
38994032 palette,
39004033 palette_uv,
4034+ filter_intra,
39014035 cfl_alphas,
39024036 angle_delta_y: angle_delta as i8,
39034037 ..BlockCoeffs::default()
@@ -3975,6 +4109,20 @@ fn cfl_on() -> bool {
39754109 *ON.get_or_init(|| std::env::var("EC_AV1_CFL").as_deref() != Ok("0"))
39764110 }
39774111
4112+/// Whether the intra search offers the five recursive filter-intra modes at
4113+/// all, and with them whether the sequence header sets `enable_filter_intra`
4114+/// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured
4115+/// its own on/off pair -- and how every byte pin written before it still
4116+/// reproduces).
4117+pub(crate) fn filter_intra_on() -> bool {
4118+ #[cfg(test)]
4119+ if let Some(forced) = FILTER_INTRA_FORCE.with(std::cell::Cell::get) {
4120+ return forced;
4121+ }
4122+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
4123+ *ON.get_or_init(|| std::env::var("EC_AV1_FILTER_INTRA").as_deref() != Ok("0"))
4124+}
4125+
39784126 /// Reads [`UV_MODE_HITS`] and zeroes it, so a gate can attribute the counts
39794127 /// to its own encode.
39804128 #[cfg(test)]
@@ -5086,6 +5234,7 @@ fn code_square_inter(
50865234 BlockCoeffs {
50875235 angle_delta_y: 0,
50885236 cfl_alphas: None,
5237+ filter_intra: None,
50895238 luma: coeffs(&levels, side),
50905239 u: coeffs(&u_new.levels, side / 2),
50915240 v: coeffs(&v_new.levels, side / 2),
@@ -5121,6 +5270,7 @@ fn code_square_inter(
51215270 (BlockCoeffs {
51225271 angle_delta_y: 0,
51235272 cfl_alphas: None,
5273+ filter_intra: None,
51245274 luma: coeffs(&levels, side),
51255275 u: coeffs(&u.levels, side / 2),
51265276 v: coeffs(&v.levels, side / 2),
@@ -6223,6 +6373,7 @@ pub(crate) fn encode_key_frame_inner(
62236373 crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec());
62246374 let mut cdfs = start_cdfs.0.clone();
62256375 crate::tile::arm_screen(screen);
6376+ crate::tile::arm_filter_intra(filter_intra_on());
62266377 crate::tile::arm_intrabc(allow_intrabc);
62276378 crate::tile::arm_pricing_cdfs(None, false);
62286379 let bytes = crate::tile::sb_coeff_key_frame_tile_cdfs(
@@ -7829,6 +7980,7 @@ fn search_inter_block(
78297980 BlockCoeffs {
78307981 angle_delta_y: 0,
78317982 cfl_alphas: None,
7983+ filter_intra: None,
78327984 luma: coeffs(&levels, BLOCK),
78337985 u: coeffs(&best.u.levels, BLOCK / 2),
78347986 v: coeffs(&best.v.levels, BLOCK / 2),
@@ -9079,6 +9231,7 @@ pub(crate) fn encode_inter_frame(
90799231 crate::tile::arm_warped_motion(warped_motion);
90809232 crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints);
90819233 crate::tile::arm_screen(screen);
9234+ crate::tile::arm_filter_intra(filter_intra_on());
90829235 let mut cdfs = start_cdfs.0.clone();
90839236 crate::tile::arm_pricing_cdfs(Some(&cdfs), screen);
90849237 // Each tile's own MV grid: a candidate scan never reaches past
@@ -9205,6 +9358,7 @@ pub(crate) fn encode_inter_frame(
92059358 h.allow_screen_content_tools,
92069359 switchable_motion_mode,
92079360 h.allow_warped_motion,
9361+ seq.enable_filter_intra,
92089362 fctx,
92099363 )
92109364 },
@@ -10083,6 +10237,7 @@ mod tests {
1008310237 let residual_block = BlockCoeffs {
1008410238 angle_delta_y: 0,
1008510239 cfl_alphas: None,
10240+ filter_intra: None,
1008610241 luma: vec![Coeff {
1008710242 row: 0,
1008810243 col: 0,
@@ -10882,6 +11037,11 @@ mod tests {
1088211037 eprintln!("SKIP every_mode_decodes_to_what_the_encoder_predicted: no ffmpeg");
1088311038 return;
1088411039 }
11040+ // A mode ablation measures the MODE search: a filter-intra block is
11041+ // coded `DC_PRED` whatever mode the arm forces, so it would fail the
11042+ // "the encoder coded something else" check for a reason that is not
11043+ // about the mode search at all.
11044+ force_filter_intra(Some(false));
1088511045 // 128 wide is a whole number of superblocks and 160 is not: the last
1088611046 // superblock of a 160-wide row is half a one, whose blocks have no
1088711047 // above-right samples inside the frame at all.
@@ -10992,6 +11152,10 @@ mod tests {
1099211152 // these synthetic two-colour pictures losslessly under every arm and
1099311153 // leave nothing to compare (see [`force_screen`]).
1099411154 force_screen(Some(false));
11155+ // Same reason, for the filter-intra candidate: it wins blocks under
11156+ // BOTH arms of a mode ablation and so cancels the difference the
11157+ // ablation measures (class `gate-recipe-confound`).
11158+ force_filter_intra(Some(false));
1099511159 let mut points: Vec<(f64, f64)> = [110u8, 90, 70]
1099611160 .iter()
1099711161 .map(|&q| {
@@ -12840,8 +13004,14 @@ mod tests {
1284013004 // so every block taking either codes different syntax and different
1284113005 // coefficients -- 8194 -> 7321 at q=150 and 28285 -> 27285 at q=60.
1284213006 // `EC_AV1_CFL=0` / `EC_AV1_ANGLE=0` restore each half.
13007+ // Re-taken on lane-fintra: the sequence header sets
13008+ // `enable_filter_intra`, so every DC_PRED intra block of at most
13009+ // 32x32 without a luma palette carries a `use_filter_intra` flag and
13010+ // the blocks that take one code a filter-intra mode, a different
13011+ // prediction and different coefficients -- 7321 -> 7373 at q=150 and
13012+ // 27285 -> 27074 at q=60. `EC_AV1_FILTER_INTRA=0` restores these.
1284313013 let pins: [(u8, usize, u64); 2] =
12844- [(150, 7321, 0x34e2_e4e0_d7c5_09f3), (60, 27285, 0xbfd2_41af_dcac_a0c6)];
13014+ [(150, 7373, 0xd0af_b890_28bc_cfbf), (60, 27074, 0x8324_0e9f_5971_e4e7)];
1284513015 for (q, bytes, hash) in pins {
1284613016 let encoded = encode_sequence(&source, q, 0.5).unwrap();
1284713017 assert_eq!(
@@ -13963,6 +14133,7 @@ mod tests {
1396314133 let _ = crate::tile::take_palette_uv_hits();
1396414134 let _ = crate::tile::take_intrabc_hits();
1396514135 let _ = take_intrabc_search();
14136+ let _ = crate::tile::take_filter_intra_hits();
1396614137 // The sequence path itself codes the pyramid now
1396714138 // (lane-av1pyrdef), and prints what it requested and what the
1396814139 // content gate left effective.
@@ -14009,6 +14180,20 @@ mod tests {
1400914180 search[1],
1401014181 search[2],
1401114182 );
14183+ // gate-blind-to-feature: how often the five recursive filter-intra
14184+ // modes actually won a block over this clip's four encodes -- a
14185+ // tool that never fires is not measured by the row above it.
14186+ let fi = crate::tile::take_filter_intra_hits();
14187+ eprintln!(
14188+ "{name}: filter intra on={} blocks {} (modes DC={} V={} H={} D157={} PAETH={})",
14189+ filter_intra_on(),
14190+ fi[0],
14191+ fi[1],
14192+ fi[2],
14193+ fi[3],
14194+ fi[4],
14195+ fi[5],
14196+ );
1401214197 let (aom, aom_wall) = external_ladder(&source, cw, ch, "libaom-av1", &aom_points);
1401314198 let (rav1e, rav1e_wall) = external_ladder(&source, cw, ch, "librav1e", &rav1e_points);
1401414199 assert_monotone(&format!("{name}: ours"), &ours);
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+113 -7
@@ -343,6 +343,81 @@ 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+#[cfg(test)]
376+pub(crate) fn take_filter_intra_hits() -> [usize; 6] {
377+ std::array::from_fn(|i| FILTER_INTRA_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))
378+}
379+
380+/// `filter_intra_mode_info` (spec 5.11.14) written out, mirrored symbol for
381+/// symbol from decode.rs `read_intra_mode`: a `use_filter_intra` flag on every
382+/// DC_PRED block whose sides `av1_filter_intra_allowed_bsize` admits (both <=
383+/// 32, [`crate::decode::filter_intra_size_class`]) and that took no luma
384+/// palette (`av1_filter_intra_allowed`, reconintra.h:77), then the chosen mode
385+/// of five. Sits between the palette COLOUR lists and the colour-index maps,
386+/// where libaom reads it (`av1_visit_palette` runs after
387+/// `read_filter_intra_mode_info`).
388+fn write_filter_intra(
389+ enc: &mut SymbolEncoder,
390+ cdfs: &mut Cdfs,
391+ mode: usize,
392+ side: usize,
393+ has_palette_y: bool,
394+ filter_intra: Option<u8>,
395+) {
396+ if !filter_intra_armed() || mode != DC_PRED || has_palette_y {
397+ debug_assert!(filter_intra.is_none(), "no `use_filter_intra` symbol exists here");
398+ return;
399+ }
400+ let Some(class) = crate::decode::filter_intra_size_class(side) else {
401+ debug_assert!(filter_intra.is_none(), "past av1_filter_intra_allowed_bsize");
402+ return;
403+ };
404+ enc.symbol(usize::from(filter_intra.is_some()), &mut cdfs.filter_intra[class]);
405+ if let Some(fi) = filter_intra {
406+ enc.symbol(usize::from(fi), &mut cdfs.filter_intra_mode);
407+ FILTER_INTRA_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
408+ FILTER_INTRA_HITS[usize::from(fi) + 1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
409+ }
410+}
411+
412+/// The coefficient-table row a block's luma transform types are coded from:
413+/// `fimode_to_intradir[filter_intra_mode]` for a filter-intra block, its own
414+/// luma mode otherwise (class `filter-intra tx_type row`, decode.rs
415+/// [`crate::decode::fi_tx_row`]). The block still PUBLISHES `DC_PRED` as its
416+/// neighbour mode -- libaom's `mbmi->mode` is unchanged by filter intra.
417+fn tx_row(mode: usize, filter_intra: Option<u8>) -> usize {
418+ crate::decode::fi_tx_row(mode, filter_intra.map(usize::from))
419+}
420+
346421 /// How many blocks this writer coded `use_intrabc == 1`, and the histogram of
347422 /// their DV magnitudes in full pels (index 1..=6 = 1, 2, 4, 8, 16, 32 pels
348423 /// and up, by the larger component) -- the fire count a gate prints so that
@@ -935,6 +1010,7 @@ impl Drop for CdefIdxGuard {
9351010 REFERENCE_SELECT.with(|c| c.set(false));
9361011 MOTION_MODE.with(|c| c.set(false));
9371012 INTRABC.with(|c| c.set(false));
1013+ FILTER_INTRA.with(|c| c.set(false));
9381014 INTRABC_GRID.with(|c| *c.borrow_mut() = None);
9391015 ORDER_HINTS.with(|c| c.set((7, 0, [0; 7])));
9401016 }
@@ -1452,6 +1528,14 @@ pub struct BlockCoeffs {
14521528 /// [`UV_CFL_PRED`]; at least one of the pair is nonzero, since the joint
14531529 /// sign symbol has no (ZERO, ZERO) value.
14541530 pub cfl_alphas: Option<(i32, i32)>,
1531+ /// This block's `filter_intra_mode` (spec 5.11.14, 0..=4) when it is
1532+ /// coded with recursive filter intra, or `None`. `Some` only on a DC_PRED
1533+ /// luma block of at most 32x32 with no luma palette, on a sequence whose
1534+ /// header set `enable_filter_intra` ([`arm_filter_intra`]); the block's
1535+ /// luma transform types are then coded from
1536+ /// `fimode_to_intradir[filter_intra_mode]`'s row ([`tx_row`]) while its
1537+ /// neighbour mode stays `DC_PRED`.
1538+ pub filter_intra: Option<u8>,
14551539 /// This block's `angle_delta_y` (spec `read_intra_angle_info`, -3..=3),
14561540 /// zero for every non-directional luma mode -- which is what
14571541 /// [`Default`] gives, and what every writer coded before lane-av1cfl.
@@ -3446,6 +3530,7 @@ fn write_intra_mode(
34463530 neighbours,
34473531 block.palette.as_ref(),
34483532 block.palette_uv.as_ref(),
3533+ block.filter_intra,
34493534 mode,
34503535 uv_mode,
34513536 mi,
@@ -3474,12 +3559,18 @@ fn write_palette_syntax(
34743559 neighbours: &mut Neighbours,
34753560 palette: Option<&PaletteY>,
34763561 palette_uv: Option<&PaletteUv>,
3562+ // lane-fintra: this block's chosen `filter_intra_mode`, or `None`. Written
3563+ // here rather than by the caller because its symbol sits BETWEEN the
3564+ // palette colour lists and the colour-index maps (decode.rs
3565+ // `read_intra_mode`), which is inside this function.
3566+ filter_intra: Option<u8>,
34773567 mode: usize,
34783568 uv_mode: usize,
34793569 mi: (usize, usize),
34803570 side: usize,
34813571 ) {
34823572 if !screen_armed() {
3573+ write_filter_intra(enc, cdfs, mode, side, false, filter_intra);
34833574 return;
34843575 }
34853576 let palette = palette.filter(|_| mode == DC_PRED);
@@ -3509,6 +3600,7 @@ fn write_palette_syntax(
35093600 enc.symbol(n - 2, &mut cdfs.palette_uv_size[bsize_ctx]);
35103601 write_palette_colors_uv(enc, &pal.u_colors[..n], &pal.v_colors[..n], &uv_cache);
35113602 }
3603+ write_filter_intra(enc, cdfs, mode, side, palette.is_some(), filter_intra);
35123604 // Both colour-index maps come after BOTH colour lists, in plane order
35133605 // (`av1_visit_palette` runs from the caller, past `filter_intra`).
35143606 if let Some(pal) = palette {
@@ -3527,6 +3619,10 @@ fn write_palette_syntax(
35273619 );
35283620 note_palette_uv(n);
35293621 }
3622+ } else {
3623+ // Below `av1_allow_palette`'s size bound there is no palette syntax at
3624+ // all, but `av1_filter_intra_allowed_bsize` still admits the block.
3625+ write_filter_intra(enc, cdfs, mode, side, false, filter_intra);
35303626 }
35313627 neighbours.pending_palette = palette.map(|p| (p.size, p.colors));
35323628 neighbours.pending_palette_uv = palette_uv.map(|p| (p.size, p.u_colors));
@@ -3629,6 +3725,10 @@ fn write_block(
36293725 at_mi,
36303726 side,
36313727 );
3728+ // The coefficient tables read `fimode_to_intradir[filter_intra_mode]`'s
3729+ // row on a filter-intra block; the neighbour publication below still gets
3730+ // the block's own `DC_PRED` ([`tx_row`]).
3731+ let tx_mode = tx_row(mode, block.filter_intra);
36323732 let split = if tx_select {
36333733 let tx = write_luma_select(
36343734 enc,
@@ -3638,7 +3738,7 @@ fn write_block(
36383738 side,
36393739 usize::from(block.tx_depth),
36403740 &grids[0],
3641- mode,
3741+ tx_mode,
36423742 all_scans,
36433743 )?;
36443744 tx < side
@@ -3652,7 +3752,7 @@ fn write_block(
36523752 grids,
36533753 &scans,
36543754 &neighbours.around(at, side),
3655- mode,
3755+ tx_mode,
36563756 tx_select,
36573757 );
36583758 neighbours.record_planes(at, side, mode, grids, !split);
@@ -3724,6 +3824,9 @@ fn write_leaf8(
37243824 // unchanged).
37253825 let mode = write_intra_mode(enc, cdfs, neighbours, block, above_mode, left_mode, true, leaf_mi, 8);
37263826 let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4];
3827+ // As [`write_block`]: the coefficient row follows the filter-intra mode,
3828+ // the mode this leaf publishes (and returns to the next leaf) does not.
3829+ let tx_mode = tx_row(mode, block.filter_intra);
37273830 let split = if tx_select {
37283831 let tx = write_luma_select(
37293832 enc,
@@ -3733,7 +3836,7 @@ fn write_leaf8(
37333836 8,
37343837 usize::from(block.tx_depth),
37353838 &grids[0],
3736- mode,
3839+ tx_mode,
37373840 all_scans,
37383841 )?;
37393842 tx < 8
@@ -3747,7 +3850,7 @@ fn write_leaf8(
37473850 grids,
37483851 &scans,
37493852 &neighbours.around_mi(leaf_mi, 8),
3750- mode,
3853+ tx_mode,
37513854 tx_select,
37523855 );
37533856 neighbours.record_mi_planes(leaf_mi, 8, grids, !split);
@@ -5811,7 +5914,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
58115914 if (V_PRED..=D67_PRED).contains(&uv_mode) {
58125915 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
58135916 }
5814- mode_for_tx = mode;
5917+ mode_for_tx = tx_row(mode, block.filter_intra);
58155918 // Intra: no vote, but still a coded cell -- mvstack's
58165919 // extended-scan coverage must see it (module doc).
58175920 let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8);
@@ -5821,6 +5924,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
58215924 &mut neighbours,
58225925 block.palette.as_ref(),
58235926 block.palette_uv.as_ref(),
5927+ block.filter_intra,
58245928 mode,
58255929 uv_mode,
58265930 (mi_row, mi_col),
@@ -6052,7 +6156,7 @@ fn write_inter_frame_leaf(
60526156 if (V_PRED..=D67_PRED).contains(&uv_mode) {
60536157 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
60546158 }
6055- mode_for_tx = mode;
6159+ mode_for_tx = tx_row(mode, block.filter_intra);
60566160 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
60576161 // coverage must see it (module doc).
60586162 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
@@ -6062,6 +6166,7 @@ fn write_inter_frame_leaf(
60626166 neighbours,
60636167 block.palette.as_ref(),
60646168 block.palette_uv.as_ref(),
6169+ block.filter_intra,
60656170 mode,
60666171 uv_mode,
60676172 (mi_row, mi_col),
@@ -6299,7 +6404,7 @@ fn write_inter_frame_leaf8(
62996404 if (V_PRED..=D67_PRED).contains(&uv_mode) {
63006405 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
63016406 }
6302- mode_for_tx = mode;
6407+ mode_for_tx = tx_row(mode, block.filter_intra);
63036408 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
63046409 // coverage must see it (module doc).
63056410 let (mi_row, mi_col) = leaf_mi;
@@ -6309,6 +6414,7 @@ fn write_inter_frame_leaf8(
63096414 neighbours,
63106415 block.palette.as_ref(),
63116416 block.palette_uv.as_ref(),
6417+ block.filter_intra,
63126418 mode,
63136419 uv_mode,
63146420 leaf_mi,