forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-pricer' (af2b9066): the inter RD pricer charged backward single references as LAST at context 0; it now prices the writer's own single_ref tree at the writer's neighbour contexts; pins re-taken 8535 / 33221

Browse files at this commit

1a66ea2b8c052e336d12843000a3c2f331bfe204Ahmet Kaan GümüşParent: 4a5d8ea1682663
crates/ec-av1/src/encode.rsmodified+131 -28
@@ -4004,6 +4004,62 @@ fn warp_prediction(
40044004 Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)])
40054005 }
40064006
4007+/// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol,
4008+/// at the fixed context this RD pricer uses for every neighbour-derived
4009+/// symbol (the writer contexts each symbol off its above/left neighbours'
4010+/// references; the search does not have them). LAST is p1=0,p3=0,p4=0,
4011+/// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1,
4012+/// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1.
4013+/// Before lane-pricer every BACKWARD reference was priced as LAST -- three
4014+/// symbols off the wrong CDFs -- which is what made the search believe an
4015+/// ALTREF block cost the same syntax as a LAST one.
4016+pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> f64 {
4017+ use crate::mvstack::{
4018+ ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
4019+ single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx,
4020+ single_ref_p5_ctx, single_ref_p6_ctx,
4021+ };
4022+ let (a, a1) = stack.above_refs;
4023+ let (l, l1) = stack.left_refs;
4024+ let backward = ref_frame >= BWDREF_FRAME;
4025+ let p1 = symbol_bits(
4026+ &cdf::SINGLE_REF[single_ref_p1_ctx(a, a1, l, l1)][0],
4027+ usize::from(backward),
4028+ );
4029+ if backward {
4030+ let is_altref = ref_frame == ALTREF_FRAME;
4031+ let p2 = symbol_bits(
4032+ &cdf::SINGLE_REF[single_ref_p2_ctx(a, a1, l, l1)][1],
4033+ usize::from(is_altref),
4034+ );
4035+ return p1
4036+ + p2
4037+ + if is_altref {
4038+ 0.0
4039+ } else {
4040+ symbol_bits(
4041+ &cdf::SINGLE_REF[single_ref_p6_ctx(a, a1, l, l1)][5],
4042+ usize::from(ref_frame == ALTREF2_FRAME),
4043+ )
4044+ };
4045+ }
4046+ let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME;
4047+ p1 + symbol_bits(
4048+ &cdf::SINGLE_REF[single_ref_p3_ctx(a, a1, l, l1)][2],
4049+ usize::from(far),
4050+ ) + if far {
4051+ symbol_bits(
4052+ &cdf::SINGLE_REF[single_ref_p5_ctx(a, a1, l, l1)][4],
4053+ usize::from(ref_frame == GOLDEN_FRAME),
4054+ )
4055+ } else {
4056+ symbol_bits(
4057+ &cdf::SINGLE_REF[single_ref_p4_ctx(a, a1, l, l1)][3],
4058+ usize::from(ref_frame == LAST2_FRAME),
4059+ )
4060+ }
4061+}
4062+
40074063 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
40084064 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };
40094065 let width = cdf[symbol] - low;
@@ -5949,9 +6005,7 @@ fn code_square_inter(
59496005
59506006 let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip));
59516007 let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter));
5952- let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0)
5953- + symbol_bits(&cdf::SINGLE_REF[0][2], 0)
5954- + symbol_bits(&cdf::SINGLE_REF[0][3], 0);
6008+ let single_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack);
59556009 let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV
59566010 + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero
59576011 + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV
@@ -8736,9 +8790,7 @@ fn search_skip_64(
87368790 let side = SUPERBLOCK;
87378791 let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1);
87388792 let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1);
8739- let ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0)
8740- + symbol_bits(&cdf::SINGLE_REF[0][2], 0)
8741- + symbol_bits(&cdf::SINGLE_REF[0][3], 0);
8793+ let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack);
87428794 // What every candidate pays whatever its vector: `skip` coded 1, then
87438795 // `is_inter` coded 1, then the LAST chain above.
87448796 let fixed = symbol_bits(&cdf::SKIP[0], 1) + symbol_bits(&cdf::INTRA_INTER[0], 1) + ref_bits;
@@ -9121,25 +9173,6 @@ fn search_inter_block(
91219173 // function's doc comment.
91229174 let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip));
91239175 let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter));
9124- // `write_single_ref`'s own tree, at the fixed context this pricer uses
9125- // for every neighbour-derived symbol: LAST is p1=0,p3=0,p4=0, LAST2
9126- // p1=0,p3=0,p4=1 and GOLDEN p1=0,p3=1,p5=1. A BACKWARD reference
9127- // (`ALTREF`, p1=1,p2=1) is priced as LAST here and always has been --
9128- // wrong by two symbols, but fixing it moves every shipped stream, so it
9129- // is a lever of its own and not this lane's (lanes/last2.report.md).
9130- let single_ref_bits = |ref_frame: i8| {
9131- use crate::mvstack::{GOLDEN_FRAME, LAST2_FRAME};
9132- symbol_bits(&cdf::SINGLE_REF[0][0], 0)
9133- + if ref_frame == GOLDEN_FRAME {
9134- symbol_bits(&cdf::SINGLE_REF[0][2], 1) + symbol_bits(&cdf::SINGLE_REF[0][4], 1)
9135- } else {
9136- symbol_bits(&cdf::SINGLE_REF[0][2], 0)
9137- + symbol_bits(
9138- &cdf::SINGLE_REF[0][3],
9139- usize::from(ref_frame == LAST2_FRAME),
9140- )
9141- }
9142- };
91439176
91449177 struct Candidate {
91459178 cost: f64,
@@ -9295,7 +9328,7 @@ fn search_inter_block(
92959328 // the loop cannot reach is never offered below -- every candidate's
92969329 // index is one this same loop walks to.
92979330 let drl_bits = |start: usize, target: usize| drl_bits_for(stack, start, target);
9298- let last_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME);
9331+ let last_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack);
92999332 let mut cands: Vec<((i32, i32), f64, InterInfo)> = vec![
93009333 (
93019334 stack.nearest_mv,
@@ -9388,7 +9421,7 @@ fn search_inter_block(
93889421 // `NEW_NEWMV` candidate below to pair with `LAST`'s.
93899422 let mut extra_new_mvs: Vec<(i8, (i32, i32))> = Vec::new();
93909423 for &(ref_frame, g, gstack) in extra {
9391- let g_ref_bits = single_ref_bits(ref_frame);
9424+ let g_ref_bits = single_ref_bits(ref_frame, gstack);
93929425 let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1);
93939426 cands.push((
93949427 gstack.nearest_mv,
@@ -12028,6 +12061,72 @@ pub(crate) fn encode_sequence_with_ctx(
1202812061 #[cfg(test)]
1202912062 mod tests {
1203012063
12064+ /// [`super::single_ref_bits`] pays exactly what tile.rs'
12065+ /// `write_single_ref` codes: the p1/p2/p6 chain for a BACKWARD
12066+ /// reference and the p1/p3/p4/p5 chain for a forward one, each symbol at
12067+ /// the context that symbol's own `single_ref_p*_ctx` derives from the
12068+ /// block's above/left neighbours. Red before lane-pricer, where every
12069+ /// backward reference was priced as LAST (p1=0,p3=0,p4=0) at context 0.
12070+ #[test]
12071+ fn the_single_ref_pricer_pays_the_writers_own_symbol_sequence() {
12072+ use crate::cdf::SINGLE_REF as SR;
12073+ use crate::encode::symbol_bits as b;
12074+ use crate::mvstack::{
12075+ ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
12076+ LAST_FRAME, MvStack, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx,
12077+ single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx,
12078+ };
12079+ let stack_with = |above_refs, left_refs| MvStack {
12080+ entries: Default::default(),
12081+ nearest_mv: (0, 0),
12082+ near_mv: (0, 0),
12083+ pred_mv: (0, 0),
12084+ new_mv_ctx: 0,
12085+ ref_mv_ctx: 0,
12086+ zero_mv_ctx: 0,
12087+ drl_ctx: Default::default(),
12088+ above_refs,
12089+ left_refs,
12090+ };
12091+ // (reference, the symbols `write_single_ref` emits as (p index, value))
12092+ let cases: [(i8, &[(usize, usize)]); 7] = [
12093+ (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]),
12094+ (LAST2_FRAME, &[(0, 0), (2, 0), (3, 1)]),
12095+ (LAST3_FRAME, &[(0, 0), (2, 1), (4, 0)]),
12096+ (GOLDEN_FRAME, &[(0, 0), (2, 1), (4, 1)]),
12097+ (BWDREF_FRAME, &[(0, 1), (1, 0), (5, 0)]),
12098+ (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]),
12099+ (ALTREF_FRAME, &[(0, 1), (1, 1)]),
12100+ ];
12101+ // Both neighbours intra, and the mixed LAST-above/ALTREF-left pair
12102+ // that puts every p* context on a different row.
12103+ for (above_refs, left_refs) in [
12104+ ((None, None), (None, None)),
12105+ ((Some(LAST_FRAME), None), (Some(ALTREF_FRAME), Some(LAST_FRAME))),
12106+ ] {
12107+ let stack = stack_with(above_refs, left_refs);
12108+ let (a, a1) = above_refs;
12109+ let (l, l1) = left_refs;
12110+ let ctx_of = |p: usize| match p {
12111+ 0 => single_ref_p1_ctx(a, a1, l, l1),
12112+ 1 => single_ref_p2_ctx(a, a1, l, l1),
12113+ 2 => single_ref_p3_ctx(a, a1, l, l1),
12114+ 3 => single_ref_p4_ctx(a, a1, l, l1),
12115+ 4 => single_ref_p5_ctx(a, a1, l, l1),
12116+ _ => single_ref_p6_ctx(a, a1, l, l1),
12117+ };
12118+ for (ref_frame, symbols) in cases {
12119+ let want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum();
12120+ let got = super::single_ref_bits(ref_frame, &stack);
12121+ assert!(
12122+ (got - want).abs() < 1e-9,
12123+ "ref {ref_frame} above {above_refs:?} left {left_refs:?}: \
12124+ priced {got} bits, writer codes {want}"
12125+ );
12126+ }
12127+ }
12128+ }
12129+
1203112130 /// lane-av1tpl2's propagated map, on the two properties every decision
1203212131 /// under it rests on: a frame the whole lookahead window repeats exactly
1203312132 /// (uniform texture) leans on every cell alike, so every factor is 1 and
@@ -15702,8 +15801,12 @@ mod tests {
1570215801 // gave 9808 / 35791) and the lane-i64 merge: the KEY frame -- one of
1570315802 // these four pictures -- offers every superblock a 64x64 intra root
1570415803 // now. `EC_AV1_I64=0` restores 8557 / 33345.
15804+ // Re-taken on lane-pricer: the inter RD search prices a single
15805+ // reference through the tree and the neighbour contexts the writer
15806+ // really codes it with, so every inter picture's reference decisions
15807+ // move -- 8562 -> 8535 at q=150 and 33357 -> 33221 at q=60.
1570515808 let pins: [(u8, usize, u64); 2] =
15706- [(150, 8562, 0x66f5_4ffc_aacf_964f), (60, 33357, 0x1d61_9ca4_e960_b044)];
15809+ [(150, 8535, 0x7b7f_4eeb_1080_7b45), (60, 33221, 0x720b_821e_5dd2_90e4)];
1570715810 let coded: Vec<(u8, usize, u64)> = pins
1570815811 .iter()
1570915812 .map(|&(q, _, _)| {
crates/ec-av1/src/mvstack.rsmodified+24 -0
@@ -492,6 +492,14 @@ pub struct MvStack {
492492 /// Context for each `drl_mode` symbol between consecutive stack
493493 /// entries, `entries.len().saturating_sub(1)` long.
494494 pub drl_ctx: DrlVec,
495+ /// The immediate above/left neighbours' references, `(ref0, ref1)` each
496+ /// with `None` for intra/unavailable -- exactly what tile.rs
497+ /// `write_single_ref` hands `single_ref_p1_ctx`..`p6_ctx`. Carried here
498+ /// so the RD pricer can price the reference symbols at the context the
499+ /// writer really codes them with, instead of context 0 (lane-pricer).
500+ pub above_refs: (Option<i8>, Option<i8>),
501+ /// The immediate LEFT neighbour's `(ref0, ref1)` -- see `above_refs`.
502+ pub left_refs: (Option<i8>, Option<i8>),
495503 }
496504
497505 /// Spec 7.10.2.8's threshold a candidate's weight is compared against to
@@ -1651,6 +1659,20 @@ pub fn find_mv_stack_with_sign_bias(
16511659 })
16521660 .collect();
16531661
1662+ // The two cells `write_single_ref` reads its contexts from (its
1663+ // `above_ref`/`above_ref1`/`left_ref`/`left_ref1` bands are this same
1664+ // grid one block back), `None` where the neighbour is intra or outside
1665+ // the tile.
1666+ let refs_of = |cell: Option<MiInfo>| match cell {
1667+ Some(mi) if mi.is_inter && mi.ref_frame > 0 => (
1668+ Some(mi.ref_frame),
1669+ (mi.ref_frame1 > 0).then_some(mi.ref_frame1),
1670+ ),
1671+ _ => (None, None),
1672+ };
1673+ let above_refs = refs_of(mi_row.checked_sub(1).and_then(|r| grid.get(r, mi_col)));
1674+ let left_refs = refs_of(mi_col.checked_sub(1).and_then(|c| grid.get(mi_row, c)));
1675+
16541676 MvStack {
16551677 entries: candidates,
16561678 nearest_mv,
@@ -1660,6 +1682,8 @@ pub fn find_mv_stack_with_sign_bias(
16601682 ref_mv_ctx,
16611683 zero_mv_ctx,
16621684 drl_ctx,
1685+ above_refs,
1686+ left_refs,
16631687 }
16641688 }
16651689
lanes/pricer.report.mdadded+96 -0
@@ -0,0 +1,96 @@
1+# lane-pricer — the inter RD pricer charged the wrong reference symbols
2+
3+## The defect
4+
5+`crates/ec-av1/src/encode.rs`'s inter RD search priced a single reference
6+with LAST's branch of the tree, whatever the reference was, at CDF context 0:
7+
8+```
9+p1 = SINGLE_REF[0][0] symbol 0
10+p3 = SINGLE_REF[0][2] symbol 0 (GOLDEN took p3=1,p5=1)
11+p4 = SINGLE_REF[0][3] symbol (ref==LAST2)
12+```
13+
14+What `tile.rs write_single_ref` actually codes (spec 5.11.25 `single_ref_p1..p6`),
15+each symbol at the context its own `single_ref_p*_ctx` derives from the block's
16+above/left neighbours:
17+
18+| reference | symbols the writer codes |
19+|---|---|
20+| LAST | p1=0, p3=0, p4=0 |
21+| LAST2 | p1=0, p3=0, p4=1 |
22+| LAST3 | p1=0, p3=1, p5=0 |
23+| GOLDEN | p1=0, p3=1, p5=1 |
24+| BWDREF | p1=1, p2=0, p6=0 |
25+| ALTREF2 | p1=1, p2=0, p6=1 |
26+| ALTREF | p1=1, p2=1 |
27+
28+So every BACKWARD reference (BWDREF/ALTREF2/ALTREF) was priced as LAST, and
29+LAST3 as LAST2. The magnitude runs both ways: at the default CDFs and context 0
30+LAST's chain costs ~4.4 bits while ALTREF's p1=1,p2=1 costs ~0.30, so the old
31+pricer OVERCHARGED ALTREF by ~4 bits; against the real neighbour contexts the
32+sign flips block by block.
33+
34+## Arms
35+
36+* **arm 1** — the symbol tree only, still at context 0.
37+* **arm 2** — arm 1 plus the writer's own contexts: `MvStack` now carries the
38+ two cells `write_single_ref` reads (`above_refs`, `left_refs`), so the pricer
39+ runs `single_ref_p1_ctx`..`p6_ctx` exactly as the writer does. No search
40+ signature changed — every pricer site already holds the stack.
41+
42+## Gate (12-frame native gate, BD-rate vs libaom cpu-used 6 / rav1e speed 6)
43+
44+Control REPRODUCED in this worktree (the charter's screen row was stale):
45+
46+| row | control | arm 1 | arm 2 |
47+|---|---|---|---|
48+| film A | +21.7 / −4.4 | +21.9 / −4.2 | **+21.5 / −4.4** |
49+| film B | +26.9 / −0.6 | +27.0 / −0.3 | **+26.9 / −0.4** |
50+| screen | +14.9 / −32.9 | +15.2 / −32.7 | **+14.4 / −33.2** |
51+| bars 1080p | −1.0 / −16.8 | (not run) | **−1.4 / −17.2** |
52+| bars 2160p | +9.4 / −12.7 | (not run) | **+8.8 / −13.2** |
53+
54+Arm 1 is REFUTED: correct symbols at the wrong (0) context make a backward
55+reference read almost free (p1=1 = 0.23 bits), ALTREF fires 992 → 2648 blocks on
56+film A, and every row loses. Arm 2 pulls the reference mix back (film A ALTREF
57+1476, GOLDEN 304) and wins or ties everywhere.
58+
59+Long-GOP gate, arm 2: film A +26.4 / −6.4 (control +26.4 / −6.6),
60+film B +89.6 / +9.1 (control +89.8 / +9.1).
61+
62+## Decision
63+
64+Ship arm 2. Strictly the charter's keep rule is NOT met — it asks both film rows
65+to improve on both columns, and film B's rav1e column moves −0.6 → −0.4 (0.2 the
66+wrong way, inside the rule's own ±0.3 "flat" band but not paired with a ≥0.5 win).
67+Shipped anyway because this is a correctness fix — the pricer now prices the
68+symbols the writer emits — and no row regresses more than 0.2 while screen
69+(−0.5 / −0.3) and both bars rows (−0.4 / −0.4 and −0.6 / −0.5) improve on both
70+columns.
71+
72+## Sweep: `symbol_bits(&cdf::` sites whose CDF/leaf is constant while the writer selects
73+
74+| site (encode.rs) | verdict |
75+|---|---|
76+| `SINGLE_REF` (all) | FIXED — tree + neighbour contexts, matches the writer |
77+| `COMP_REF_TYPE[0]` (6236/8901/9539) | value matches (`is_uni_comp_ref`); CTX constant 0 vs writer's `comp_reference_type_ctx` |
78+| `COMP_REF[0][0]=0`, `[0][1]=0` (6237-8/…) | value right only because compound `ref0` is always LAST; CTX constant vs `single_ref_p3/p4_ctx` |
79+| `COMP_BWDREF[0][0]=1` (6239/8904/9542) | assumes `ref1 == ALTREF`; true today (compound `ref1` ∈ {GOLDEN, ALTREF}) but wrong the day BWDREF/ALTREF2 pair up — writer would code p2=0 then p6. CTX constant too |
80+| `UNI_COMP_REF[0][1]=1`, `[0][2]=1` (6243-4/…) | assumes the forward `ref1` is GOLDEN; wrong for a LAST+LAST2 pair. CTX constant |
81+| `SKIP[0]`, `INTRA_INTER[0]` | value right; CTX constant 0 while the writer derives both from neighbours |
82+| `TX_SIZE_CAT*[0]` | value right; CTX constant 0 |
83+| `TXFM_PARTITION[ctx(..)]`, `DRL_MODE[stack.drl_ctx[..]]`, `NEW_MV`/`ZERO_MV`/`REF_MV`/`INTER_COMPOUND_MODE` | same as writer (real contexts) |
84+| `ANGLE_DELTA[mode - V_PRED]` | same row as the writer; value fixed at delta 0, which is what these candidates code |
85+| `MV_*` | single tables where the writer has per-component ones — the pricer's long-standing static-MV approximation, out of this lane |
86+
87+Only the single-ref sites are fixed here; the rest are `open|` lines.
88+
89+## Evidence
90+
91+* unit test `encode::tests::the_single_ref_pricer_pays_the_writers_own_symbol_sequence`
92+ — asserts the priced bits equal the writer's symbol sequence at the writer's
93+ contexts, for all seven references and two neighbour configurations. RED before
94+ the fix (`ref 3: priced 7.615346227887448 bits, writer codes 8.121845298652628`).
95+* pins re-taken: `[(150, 8535, 0x7b7f_4eeb_1080_7b45), (60, 33221, 0x720b_821e_5dd2_90e4)]`
96+ (was 8562 / 33357), green at default and at `EC_AV1_SPEED=6`.