forge
Explore
Log in

Don't you dare go Hollow

Filter intra predicts per transform unit, depths searched on screen frames

Browse files at this commit

235add97870eb53db5ef903c9cad3fc816a23ee7Ahmet Kaan GümüşParent: db67894
  • feat
    • filter intra predicts per transform unit, depths searched on screen frames
Show full message
feat(av1): filter intra predicts per transform unit, depths searched on screen frames

The filter-intra candidate used to be priced at tx_depth 0 only: a block
whose ordinary intra winner wanted a split transform was compared against
a handicapped filter arm, and only 19-44% of intra blocks code depth 0.

`Plane::code_tx_depth` now takes the block's `filter_intra_mode` and
predicts each transform unit recursively off the units before it -- what
decode.rs already does per unit -- so the arm can be priced at every
depth through the same pricer, neighbour context and `fimode_to_intradir`
tx row.

Measured on the native BD table: searching the depths on EVERY frame
moves screen -1.0/-0.4 and the bars rows -0.4/-0.7, but leaves film A
flat and film B +0.1/+0.5 WORSE at +30..35% encoder wall -- the extra
filter blocks are a locally cheaper reconstruction the film rows then
predict from. So the depth search is armed on screen frames only: every
non-screen frame keeps the depth-0-only arm and codes byte-identical
streams (film A/B and both bars rows reproduce the baseline table byte
for byte), while the screen row goes +49.2 -> +48.2 vs libaom and
-16.0 -> -16.4 vs rav1e for +21% wall on that row.
crates/ec-av1/src/encode.rsmodified+87 -48
@@ -1868,12 +1868,19 @@ impl Plane<'_> {
18681868 /// reconstructed on its own. Commits the result and hands back the
18691869 /// block-coordinate levels, the block's squared error and what its
18701870 /// coefficients cost.
1871+ #[allow(clippy::too_many_arguments)]
18711872 fn code_tx_depth(
18721873 &mut self,
18731874 at: At,
18741875 mode: u8,
18751876 angle_delta: i32,
18761877 depth: usize,
1878+ // This block's `filter_intra_mode` when it is coded with one, in
1879+ // which case every transform unit predicts recursively off the
1880+ // reconstruction the units before it left -- what the decoder does
1881+ // per unit (decode.rs `decode_block`, `push_intra_rect`'s
1882+ // `filter_intra`), not once over the whole block.
1883+ filter_intra: Option<u8>,
18771884 search: &Search, fctx: &crate::decode::FrameCtx,
18781885 ) -> (Vec<i32>, f64, f64) {
18791886 let At { x, y, side, reach, .. } = at;
@@ -1901,7 +1908,36 @@ impl Plane<'_> {
19011908 reach: Reach::of_tu(side, side, col_off, row_off, tx, tx, reach),
19021909 set,
19031910 };
1904- let trial = self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx);
1911+ let trial = match filter_intra {
1912+ Some(fi) => {
1913+ let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
1914+ let (above, left, corner) =
1915+ self.edges_into(tu.x, tu.y, tx, tu.reach, &mut above_buf, &mut left_buf);
1916+ let mut prediction = vec![0u8; tx * tx];
1917+ filter_intra_predict_u8(
1918+ usize::from(fi),
1919+ above,
1920+ left,
1921+ corner,
1922+ tx,
1923+ &mut prediction,
1924+ fctx,
1925+ );
1926+ self.code_from_prediction(
1927+ tu.x,
1928+ tu.y,
1929+ tx,
1930+ &prediction,
1931+ false,
1932+ search.base_q_idx,
1933+ search.deadzone,
1934+ set,
1935+ )
1936+ }
1937+ None => {
1938+ self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx)
1939+ }
1940+ };
19051941 sse += trial.sse;
19061942 bits += trial.bits;
19071943 self.commit(tu.x, tu.y, tx, &trial);
@@ -3848,7 +3884,7 @@ fn code_square(
38483884 luma_coeffs,
38493885 );
38503886 for depth in 1..=max_tx_depth(side) {
3851- let (levels, sse, bits) = luma.code_tx_depth(at, mode, angle_delta, depth, search, fctx);
3887+ let (levels, sse, bits) = luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);
38523888 let cost_d = sse
38533889 + search.lambda
38543890 * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth));
@@ -3920,14 +3956,19 @@ fn code_square(
39203956 // by construction (`av1_filter_intra_allowed`), and its luma transform
39213957 // types are coded from `fimode_to_intradir`'s row (`crate::tile::tx_row`).
39223958 //
3923- // corner-cut, ceiling named: the candidate is only ever priced at
3924- // `tx_depth` 0 (one transform over the whole block), so a block whose
3925- // ordinary intra winner wanted a SPLIT transform is compared against a
3926- // handicapped filter-intra arm -- and only 19-44% of this encoder's intra
3927- // blocks code depth 0. Upgrade path: predict per transform unit inside
3928- // [`Plane::code_tx_depth`] (the decoder already reads `filter_intra` per
3929- // unit, decode.rs:11835) and run the depth search under the winning
3930- // filter mode.
3959+ // lane-fitu: on a SCREEN frame the candidate is priced at every transform
3960+ // depth the ordinary intra search runs, each unit predicting recursively
3961+ // off the units before it ([`Plane::code_tx_depth`]'s `filter_intra`
3962+ // arm), so a block whose ordinary winner wanted a split transform is
3963+ // compared against a filter arm that may split too.
3964+ //
3965+ // Measured (native BD table, lane-fitu): searching the depths on EVERY
3966+ // frame moves screen -1.0/-0.4 and the bars rows -0.4/-0.7 but leaves
3967+ // film A flat and film B +0.1/+0.5 WORSE -- the extra filter blocks are a
3968+ // locally cheaper reconstruction that the film rows go on to predict from
3969+ // (class `local-rd-on-references`) -- at +30..35% encoder wall. Screen
3970+ // content is where it pays, and a non-screen frame keeps the depth-0-only
3971+ // arm, byte for byte.
39313972 let mut filter_intra: Option<u8> = None;
39323973 if filter_intra_on()
39333974 && palette.is_none()
@@ -3943,47 +3984,39 @@ fn code_square(
39433984 }
39443985 let one_bits = symbol_bits(row, 1);
39453986 let kept = luma.snapshot(x, y, side);
3946- let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]);
3947- let (above, left, corner) =
3948- luma.edges_into(x, y, side, at.reach, &mut above_buf, &mut left_buf);
3949- let mut prediction = vec![0u8; side * side];
3987+ let max_depth = if tx_select && search.screen { max_tx_depth(side) } else { 0 };
3988+ let mut best: Option<(f64, u8, usize, (Vec<u8>, Vec<CoefCtx>), Vec<Coeff>)> = None;
39503989 for fi in 0..5u8 {
3951- filter_intra_predict_u8(
3952- usize::from(fi),
3953- above,
3954- left,
3955- corner,
3956- side,
3957- &mut prediction, fctx,
3958- );
3959- let trial = luma.code_from_prediction(
3960- x,
3961- y,
3962- side,
3963- &prediction,
3964- false,
3965- search.base_q_idx,
3966- search.deadzone,
3967- luma_set,
3968- );
3969- let cost_f = trial.sse
3970- + search.lambda
3971- * (trial.bits
3972- + mode_bits[DC_PRED as usize]
3973- + one_bits
3974- + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))
3975- + if tx_select { tx_depth_bits(side, 0) } else { 0.0 });
3976- if cost_f < cost {
3977- cost = cost_f;
3978- mode = DC_PRED;
3979- angle_delta = 0;
3980- tx_depth = 0;
3981- luma_coeffs = coeffs(&trial.levels, side);
3982- luma.commit(x, y, side, &trial);
3983- filter_intra = Some(fi);
3990+ for depth in 0..=max_depth {
3991+ let (levels, sse, bits) =
3992+ luma.code_tx_depth(at, DC_PRED, 0, depth, Some(fi), search, fctx);
3993+ let cost_f = sse
3994+ + search.lambda
3995+ * (bits
3996+ + mode_bits[DC_PRED as usize]
3997+ + one_bits
3998+ + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))
3999+ + if tx_select { tx_depth_bits(side, depth) } else { 0.0 });
4000+ if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) {
4001+ best = Some((
4002+ cost_f,
4003+ fi,
4004+ depth,
4005+ luma.snapshot(x, y, side),
4006+ coeffs(&levels, side),
4007+ ));
4008+ }
39844009 }
39854010 }
3986- if filter_intra.is_none() {
4011+ if let Some((cost_f, fi, depth, snapshot, levels)) = best {
4012+ cost = cost_f;
4013+ mode = DC_PRED;
4014+ angle_delta = 0;
4015+ tx_depth = depth as u8;
4016+ luma_coeffs = levels;
4017+ luma.restore(x, y, side, &snapshot);
4018+ filter_intra = Some(fi);
4019+ } else {
39874020 luma.restore(x, y, side, &kept);
39884021 }
39894022 }
@@ -11078,6 +11111,12 @@ mod tests {
1107811111 #[test]
1107911112 fn the_search_picks_the_direction_the_picture_runs() {
1108011113 let fctx = &crate::decode::FrameCtx::new();
11114+ // Same confound as [`every_mode_decodes_to_what_the_encoder_predicted`]:
11115+ // a filter-intra block is coded `DC_PRED` whatever mode the arm
11116+ // offers, and these stripes are SCREEN content, where the filter arm
11117+ // now searches its own transform depth (lane-fitu) and wins outright
11118+ // -- which says nothing about the mode search this gate measures.
11119+ force_filter_intra(Some(false));
1108111120 for (vertical, want) in [(true, V_PRED), (false, H_PRED)] {
1108211121 let picture = stripes(128, 96, vertical);
1108311122 // Only the pair, because on stripes a third mode predicts exactly