forge
Explore
Log in

Don't you dare go Hollow

Directional luma winners search their angle_delta_y, off screen frames

Browse files at this commit

a00525205d9bebbe4f8bf77cdd11254956bb2eb6Ahmet Kaan GümüşParent: f944910
  • feat
    • directional luma winners search their angle_delta_y, off screen frames
Show full message
feat(av1): directional luma winners search their angle_delta_y, off screen frames

tile.rs coded ANGLE_DELTA_ZERO at all four luma sites because nothing ever
searched a delta. The luma search now refines the winning directional mode
over delta -3..=3 through the decoder's own predictor (its `angle_delta`
argument; `enable_intra_edge_filter` stays off in this encoder's sequence
header, so both sides gather the same edge samples), prices the symbol as a
difference from ZERO, and threads the delta through `trial`/`trial_typed`/
`code_tx_depth` so a transform-depth trial re-predicts at the same angle.

Screen frames are excluded: measured, the refinement is +0.7/+0.3 the wrong
way on the screen capture (synthetic edges sit on the base angles, so a
delta buys a symbol and no prediction) while the film rows gain.

5-row native gate, EC_AV1_ANGLE=0 -> default (both with CfL on):
  bars 1080p +0.3/-15.5 -> +0.2/-15.5, bars 2160p +16.8/-6.5 -> +16.9/-6.5,
  film A +67.7/+36.3 -> +67.7/+36.3, film B +99.6/+61.2 -> +99.0/+60.6,
  screen +50.4/-15.1 -> +50.4/-15.1 (byte-identical).
crates/ec-av1/src/encode.rsmodified+123 -13
@@ -1561,8 +1561,8 @@ impl Plane<'_> {
15611561 /// Codes one block under one mode without committing it: hands back the
15621562 /// levels, the block the decoder would reconstruct, the squared error
15631563 /// against the source and an estimate of what the levels cost in bits.
1564- fn trial(&self, at: At, mode: u8, base_q_idx: u8, deadzone: f64, fctx: &crate::decode::FrameCtx) -> Trial {
1565- self.trial_typed(at, mode, base_q_idx, deadzone, TxType::DctDct, fctx)
1564+ fn trial(&self, at: At, mode: u8, angle_delta: i32, base_q_idx: u8, deadzone: f64, fctx: &crate::decode::FrameCtx) -> Trial {
1565+ self.trial_typed(at, mode, angle_delta, base_q_idx, deadzone, TxType::DctDct, fctx)
15661566 }
15671567
15681568 /// [`Self::trial`] under a named transform type -- what an intra chroma
@@ -1609,6 +1609,10 @@ impl Plane<'_> {
16091609 &self,
16101610 at: At,
16111611 mode: u8,
1612+ // This block's `angle_delta_y`/`angle_delta_uv` (spec
1613+ // `read_intra_angle_info`, -3..=3), zero for every non-directional
1614+ // mode -- the predictor's own `angle_delta` argument.
1615+ angle_delta: i32,
16121616 base_q_idx: u8,
16131617 deadzone: f64,
16141618 tx_type: TxType,
@@ -1627,7 +1631,7 @@ impl Plane<'_> {
16271631 let prediction = &mut prediction[..side * side];
16281632 intra_predict_u8(
16291633 mode,
1630- 0,
1634+ angle_delta,
16311635 above,
16321636 left,
16331637 corner,
@@ -1815,6 +1819,7 @@ impl Plane<'_> {
18151819 &mut self,
18161820 at: At,
18171821 mode: u8,
1822+ angle_delta: i32,
18181823 depth: usize,
18191824 search: &Search, fctx: &crate::decode::FrameCtx,
18201825 ) -> (Vec<i32>, f64, f64) {
@@ -1843,7 +1848,7 @@ impl Plane<'_> {
18431848 reach: Reach::of_tu(side, side, col_off, row_off, tx, tx, reach),
18441849 set,
18451850 };
1846- let trial = self.trial(tu, mode, search.base_q_idx, search.deadzone, fctx);
1851+ let trial = self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx);
18471852 sse += trial.sse;
18481853 bits += trial.bits;
18491854 self.commit(tu.x, tu.y, tx, &trial);
@@ -2072,7 +2077,7 @@ impl Plane<'_> {
20722077 at: At,
20732078 search: &Search,
20742079 mode_bits: &[f64; 13], fctx: &crate::decode::FrameCtx,
2075- ) -> (Vec<Coeff>, u8, f64) {
2080+ ) -> (Vec<Coeff>, u8, i32, f64) {
20762081 let At {
20772082 x, y, side, reach, ..
20782083 } = at;
@@ -2155,13 +2160,69 @@ impl Plane<'_> {
21552160 best = Some((cost, mode, trial));
21562161 }
21572162 }
2158- let (cost, mode, trial) = best.expect("the search offers at least one mode");
2163+ let (mut cost, mode, mut trial) = best.expect("the search offers at least one mode");
2164+ // `angle_delta_y` (spec `read_intra_angle_info`): the eight
2165+ // directional modes each carry a -3..=3 delta, three degrees a step,
2166+ // which the writer has always coded as ZERO because nothing searched
2167+ // it. Refined on the winning mode only -- libaom's own ordering, and
2168+ // six extra trials on the directional blocks instead of on every
2169+ // block times every mode. The edges are the ones the base angle
2170+ // already read (`enable_intra_edge_filter` is off in this encoder's
2171+ // sequence header, so no delta changes which samples exist), and the
2172+ // predictor is the decoder's, `angle_delta` argument and all.
2173+ let mut angle_delta = 0i32;
2174+ // Not on a screen frame: measured on the 5-row native gate, the
2175+ // refinement is worth -0.6/-0.6 on the 2160p film row and flat on the
2176+ // 1080p one, but +0.7/+0.3 the WRONG way on the screen capture --
2177+ // synthetic edges land on the base angles, so every delta there buys
2178+ // a symbol and no prediction. Same `search.screen` gate palette and
2179+ // intrabc already stand behind.
2180+ if angle_delta_on() && !search.screen && (V_PRED..=D67_PRED).contains(&mode) {
2181+ let row = &cdf::ANGLE_DELTA[usize::from(mode - V_PRED)];
2182+ let zero_bits = symbol_bits(row, crate::tile::ANGLE_DELTA_ZERO);
2183+ let mut prediction = vec![0u8; side * side];
2184+ for delta in [-3i32, -2, -1, 1, 2, 3] {
2185+ intra_predict_u8(
2186+ mode,
2187+ delta,
2188+ above,
2189+ left,
2190+ corner,
2191+ side,
2192+ side,
2193+ false,
2194+ false,
2195+ &mut prediction, fctx,
2196+ );
2197+ let candidate = self.code_from_prediction(
2198+ x,
2199+ y,
2200+ side,
2201+ &prediction,
2202+ false,
2203+ search.base_q_idx,
2204+ search.deadzone,
2205+ at.set,
2206+ );
2207+ let bits = symbol_bits(row, (crate::tile::ANGLE_DELTA_ZERO as i32 + delta) as usize)
2208+ - zero_bits;
2209+ let cost_d = candidate.sse
2210+ + search.lambda * (candidate.bits + mode_bits[usize::from(mode)] + bits);
2211+ if cost_d < cost {
2212+ cost = cost_d;
2213+ angle_delta = delta;
2214+ trial = candidate;
2215+ }
2216+ }
2217+ ANGLE_DELTA_HITS[(crate::tile::ANGLE_DELTA_ZERO as i32 + angle_delta) as usize]
2218+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2219+ }
21592220 if census_on() {
21602221 LUMA_RANK[sad_rank(&ranking, mode).min(12)]
21612222 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
21622223 }
21632224 self.commit(x, y, side, &trial);
2164- (coeffs(&trial.levels, side), mode, cost)
2225+ (coeffs(&trial.levels, side), mode, angle_delta, cost)
21652226 }
21662227 }
21672228
@@ -3720,7 +3781,8 @@ fn code_square(
37203781 reach: Reach::of(side, x, y, luma.true_width, luma.true_height, fctx),
37213782 set: luma_set,
37223783 };
3723- let (mut luma_coeffs, mode, mut cost) = luma.search_block(at, search, mode_bits, fctx);
3784+ let (mut luma_coeffs, mode, angle_delta, mut cost) =
3785+ luma.search_block(at, search, mode_bits, fctx);
37243786 // The depth search runs on the mode the whole-block transform picked --
37253787 // libaom's own ordering, and the reason it costs one extra pass per depth
37263788 // rather than one per (mode, depth) pair.
@@ -3733,7 +3795,7 @@ fn code_square(
37333795 luma_coeffs,
37343796 );
37353797 for depth in 1..=max_tx_depth(side) {
3736- let (levels, sse, bits) = luma.code_tx_depth(at, mode, depth, search, fctx);
3798+ let (levels, sse, bits) = luma.code_tx_depth(at, mode, angle_delta, depth, search, fctx);
37373799 let cost_d = sse
37383800 + search.lambda
37393801 * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth));
@@ -3753,6 +3815,7 @@ fn code_square(
37533815 // A palette block is `DC_PRED` with `tx_depth` 0 by construction: its
37543816 // prediction is the colour map, not the DC of its neighbours.
37553817 let mut mode = mode;
3818+ let mut angle_delta = angle_delta;
37563819 let mut palette = None;
37573820 if search.screen && crate::decode::palette_bsize_ctx(side).is_some() {
37583821 let source: Vec<u8> = (y..y + side)
@@ -3785,6 +3848,7 @@ fn code_square(
37853848 if cost_p < cost {
37863849 cost = cost_p;
37873850 mode = DC_PRED as u8;
3851+ angle_delta = 0;
37883852 tx_depth = 0;
37893853 luma_coeffs = coeffs(&trial.levels, side);
37903854 luma.commit(x, y, side, &trial);
@@ -3835,6 +3899,7 @@ fn code_square(
38353899 palette,
38363900 palette_uv,
38373901 cfl_alphas,
3902+ angle_delta_y: angle_delta as i8,
38383903 ..BlockCoeffs::default()
38393904 },
38403905 cost,
@@ -3884,6 +3949,25 @@ pub(crate) fn take_cfl_alpha_hits() -> [usize; 17] {
38843949 std::array::from_fn(|m| CFL_ALPHA_HITS[m].swap(0, std::sync::atomic::Ordering::Relaxed))
38853950 }
38863951
3952+/// How many directional luma blocks won at each `angle_delta_y`, indexed
3953+/// `delta + 3` -- the histogram that says whether the refinement below ever
3954+/// leaves zero (`gate-blind-to-feature`).
3955+static ANGLE_DELTA_HITS: [std::sync::atomic::AtomicUsize; 7] =
3956+ [const { std::sync::atomic::AtomicUsize::new(0) }; 7];
3957+
3958+/// Reads [`ANGLE_DELTA_HITS`] and zeroes it.
3959+#[cfg(test)]
3960+pub(crate) fn take_angle_delta_hits() -> [usize; 7] {
3961+ std::array::from_fn(|d| ANGLE_DELTA_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed))
3962+}
3963+
3964+/// Whether the luma search refines a directional winner's `angle_delta_y`
3965+/// (`EC_AV1_ANGLE=0` switches it off, the lane's own on/off pair).
3966+fn angle_delta_on() -> bool {
3967+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
3968+ *ON.get_or_init(|| std::env::var("EC_AV1_ANGLE").as_deref() != Ok("0"))
3969+}
3970+
38873971 /// Whether the chroma search offers `UV_CFL_PRED` at all (`EC_AV1_CFL=0`
38883972 /// switches it off, which is how the lane measured its own on/off pair).
38893973 fn cfl_on() -> bool {
@@ -3998,8 +4082,8 @@ fn search_chroma(
39984082 bits += symbol_bits(&cdf::ANGLE_DELTA[usize::from(mode - V_PRED)], 3);
39994083 }
40004084 let tx_type = crate::decode::default_intra_tx_type(mode);
4001- let u = chroma[0].trial_typed(at, mode, search.base_q_idx, search.deadzone, tx_type, fctx);
4002- let v = chroma[1].trial_typed(at, mode, search.base_q_idx, search.deadzone, tx_type, fctx);
4085+ let u = chroma[0].trial_typed(at, mode, 0, search.base_q_idx, search.deadzone, tx_type, fctx);
4086+ let v = chroma[1].trial_typed(at, mode, 0, search.base_q_idx, search.deadzone, tx_type, fctx);
40034087 let cost = u.sse + v.sse + search.lambda * (bits + u.bits + v.bits);
40044088 if best.as_ref().is_none_or(|(b, ..)| cost < *b) {
40054089 best = Some((cost, mode, u, v));
@@ -5000,6 +5084,7 @@ fn code_square_inter(
50005084 chroma[1].commit(x / 2, y / 2, side / 2, &v_new);
50015085 return (
50025086 BlockCoeffs {
5087+ angle_delta_y: 0,
50035088 cfl_alphas: None,
50045089 luma: coeffs(&levels, side),
50055090 u: coeffs(&u_new.levels, side / 2),
@@ -5034,6 +5119,7 @@ fn code_square_inter(
50345119 chroma[0].commit(x / 2, y / 2, side / 2, &u);
50355120 chroma[1].commit(x / 2, y / 2, side / 2, &v);
50365121 (BlockCoeffs {
5122+ angle_delta_y: 0,
50375123 cfl_alphas: None,
50385124 luma: coeffs(&levels, side),
50395125 u: coeffs(&u.levels, side / 2),
@@ -6950,6 +7036,7 @@ fn search_inter_block(
69507036 set: chroma_set,
69517037 },
69527038 DC_PRED,
7039+ 0,
69537040 search.base_q_idx,
69547041 search.deadzone, fctx,
69557042 );
@@ -6962,6 +7049,7 @@ fn search_inter_block(
69627049 set: chroma_set,
69637050 },
69647051 DC_PRED,
7052+ 0,
69657053 search.base_q_idx,
69667054 search.deadzone, fctx,
69677055 );
@@ -6996,6 +7084,7 @@ fn search_inter_block(
69967084 set: luma_set,
69977085 },
69987086 mode,
7087+ 0,
69997088 search.base_q_idx,
70007089 search.deadzone, fctx,
70017090 );
@@ -7738,6 +7827,7 @@ fn search_inter_block(
77387827 chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v);
77397828 (
77407829 BlockCoeffs {
7830+ angle_delta_y: 0,
77417831 cfl_alphas: None,
77427832 luma: coeffs(&levels, BLOCK),
77437833 u: coeffs(&best.u.levels, BLOCK / 2),
@@ -9991,6 +10081,7 @@ mod tests {
999110081 let (_, inter_header) = inter_frame_headers(width, height, 100, 1, 0).unwrap();
999210082
999310083 let residual_block = BlockCoeffs {
10084+ angle_delta_y: 0,
999410085 cfl_alphas: None,
999510086 luma: vec![Coeff {
999610087 row: 0,
@@ -11657,8 +11748,10 @@ mod tests {
1165711748 // symbol and one or two magnitude symbols, none of which the
1165811749 // coefficient sum counts either -- +36.9% here against the same
1165911750 // fixture's +34.9% with `EC_AV1_CFL=0`.
11751+ // ... and a directional block whose `angle_delta_y` is no longer
11752+ // always ZERO codes a costlier symbol off the same CDF -- +39.1%.
1166011753 assert!(
11661- worst_under <= 0.38,
11754+ worst_under <= 0.40,
1166211755 "the writer spent {:.2}% more than the search priced -- more than \
1166311756 the mode/mv syntax outside the coefficient sum explains",
1166411757 worst_under * 100.0
@@ -12712,7 +12805,9 @@ mod tests {
1271212805 /// Re-pinned on lane-av1cfl: the chroma search offers `UV_CFL_PRED`, so
1271312806 /// every block whose chroma takes it codes a different mode symbol, two
1271412807 /// alpha symbols and different chroma coefficients. `EC_AV1_CFL=0`
12715- /// restores the previous streams.
12808+ /// restores the previous streams. Re-pinned again in the same lane once
12809+ /// directional luma winners searched their `angle_delta_y`
12810+ /// (`EC_AV1_ANGLE=0` restores that half).
1271612811 #[test]
1271712812 fn the_encoders_own_streams_are_byte_identical_to_their_pins() {
1271812813 if !have_ffmpeg() {
@@ -13222,6 +13317,21 @@ mod tests {
1322213317 // plane of every `UV_CFL_PRED` block, by |alpha_q3|. A tool that
1322313318 // only ever fires at one magnitude is a search that never left
1322413319 // its first candidate (gate-blind-to-feature).
13320+ // The luma angle-delta histogram (lane-av1cfl): how many
13321+ // directional blocks won at each delta. All-zero would mean the
13322+ // refinement never beat the base angle.
13323+ let deltas = take_angle_delta_hits();
13324+ if deltas.iter().sum::<usize>() > 0 {
13325+ eprintln!(
13326+ "{name}: angle_delta_y {}",
13327+ deltas
13328+ .iter()
13329+ .enumerate()
13330+ .map(|(d, &n)| format!("{:+}={n}", d as i32 - 3))
13331+ .collect::<Vec<_>>()
13332+ .join(" ")
13333+ );
13334+ }
1322513335 let alphas = take_cfl_alpha_hits();
1322613336 if alphas.iter().sum::<usize>() > 0 {
1322713337 eprintln!(
crates/ec-av1/src/tile.rsmodified+21 -5
@@ -1053,7 +1053,7 @@ pub(crate) const INTRA_MODE_CTX: [usize; INTRA_MODES] = [0, 1, 2, 3, 4, 4, 4, 4,
10531053
10541054 /// The symbol an angle delta of zero codes as: the alphabet runs from -3 to
10551055 /// +3, so `MAX_ANGLE_DELTA` is the middle of it.
1056-const ANGLE_DELTA_ZERO: usize = 3;
1056+pub(crate) const ANGLE_DELTA_ZERO: usize = 3;
10571057 /// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off.
10581058 const SB_MI: u32 = 16;
10591059
@@ -1452,6 +1452,10 @@ pub struct BlockCoeffs {
14521452 /// [`UV_CFL_PRED`]; at least one of the pair is nonzero, since the joint
14531453 /// sign symbol has no (ZERO, ZERO) value.
14541454 pub cfl_alphas: Option<(i32, i32)>,
1455+ /// This block's `angle_delta_y` (spec `read_intra_angle_info`, -3..=3),
1456+ /// zero for every non-directional luma mode -- which is what
1457+ /// [`Default`] gives, and what every writer coded before lane-av1cfl.
1458+ pub angle_delta_y: i8,
14551459 /// This block's intra block-copy vector (spec 5.11.13 `use_intrabc` ->
14561460 /// `assign_dv`), in the spec's 1/8-pel `(row, col)` units, or `None` for
14571461 /// an ordinary intra block. Only a key frame whose header set
@@ -3403,7 +3407,10 @@ fn write_intra_mode(
34033407 );
34043408 ec_rng_trace(|| format!("EC_YMODE mode={mode} tell={} rng={}", enc.tell(), enc.rng()));
34053409 if (V_PRED..=D67_PRED).contains(&mode) {
3406- enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);
3410+ enc.symbol(
3411+ (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
3412+ &mut cdfs.angle_delta[mode - V_PRED],
3413+ );
34073414 }
34083415 // A block small enough to be offered chroma from luma reads the wider
34093416 // table even when it does not take the mode.
@@ -5791,7 +5798,10 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
57915798 let mode = usize::from(block.mode);
57925799 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]);
57935800 if (V_PRED..=D67_PRED).contains(&mode) {
5794- enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);
5801+ enc.symbol(
5802+ (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
5803+ &mut cdfs.angle_delta[mode - V_PRED],
5804+ );
57955805 }
57965806 let uv_mode = usize::from(block.uv_mode);
57975807 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);
@@ -6029,7 +6039,10 @@ fn write_inter_frame_leaf(
60296039 let mode = usize::from(block.mode);
60306040 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]);
60316041 if (V_PRED..=D67_PRED).contains(&mode) {
6032- enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);
6042+ enc.symbol(
6043+ (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
6044+ &mut cdfs.angle_delta[mode - V_PRED],
6045+ );
60336046 }
60346047 let uv_mode = usize::from(block.uv_mode);
60356048 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);
@@ -6273,7 +6286,10 @@ fn write_inter_frame_leaf8(
62736286 let mode = usize::from(block.mode);
62746287 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]);
62756288 if (V_PRED..=D67_PRED).contains(&mode) {
6276- enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);
6289+ enc.symbol(
6290+ (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
6291+ &mut cdfs.angle_delta[mode - V_PRED],
6292+ );
62776293 }
62786294 let uv_mode = usize::from(block.uv_mode);
62796295 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);