diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 13ec4312..1bdc7812 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -4004,6 +4004,62 @@ fn warp_prediction( Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)]) } +/// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol, +/// at the fixed context this RD pricer uses for every neighbour-derived +/// symbol (the writer contexts each symbol off its above/left neighbours' +/// references; the search does not have them). LAST is p1=0,p3=0,p4=0, +/// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1, +/// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1. +/// Before lane-pricer every BACKWARD reference was priced as LAST -- three +/// symbols off the wrong CDFs -- which is what made the search believe an +/// ALTREF block cost the same syntax as a LAST one. +pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> f64 { + use crate::mvstack::{ + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, + single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, + single_ref_p5_ctx, single_ref_p6_ctx, + }; + let (a, a1) = stack.above_refs; + let (l, l1) = stack.left_refs; + let backward = ref_frame >= BWDREF_FRAME; + let p1 = symbol_bits( + &cdf::SINGLE_REF[single_ref_p1_ctx(a, a1, l, l1)][0], + usize::from(backward), + ); + if backward { + let is_altref = ref_frame == ALTREF_FRAME; + let p2 = symbol_bits( + &cdf::SINGLE_REF[single_ref_p2_ctx(a, a1, l, l1)][1], + usize::from(is_altref), + ); + return p1 + + p2 + + if is_altref { + 0.0 + } else { + symbol_bits( + &cdf::SINGLE_REF[single_ref_p6_ctx(a, a1, l, l1)][5], + usize::from(ref_frame == ALTREF2_FRAME), + ) + }; + } + let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; + p1 + symbol_bits( + &cdf::SINGLE_REF[single_ref_p3_ctx(a, a1, l, l1)][2], + usize::from(far), + ) + if far { + symbol_bits( + &cdf::SINGLE_REF[single_ref_p5_ctx(a, a1, l, l1)][4], + usize::from(ref_frame == GOLDEN_FRAME), + ) + } else { + symbol_bits( + &cdf::SINGLE_REF[single_ref_p4_ctx(a, a1, l, l1)][3], + usize::from(ref_frame == LAST2_FRAME), + ) + } +} + pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { let low = if symbol == 0 { 0 } else { cdf[symbol - 1] }; let width = cdf[symbol] - low; @@ -5949,9 +6005,7 @@ fn code_square_inter( let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); - let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) - + symbol_bits(&cdf::SINGLE_REF[0][2], 0) - + symbol_bits(&cdf::SINGLE_REF[0][3], 0); + let single_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV @@ -8736,9 +8790,7 @@ fn search_skip_64( let side = SUPERBLOCK; let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1); let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1); - let ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) - + symbol_bits(&cdf::SINGLE_REF[0][2], 0) - + symbol_bits(&cdf::SINGLE_REF[0][3], 0); + let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); // What every candidate pays whatever its vector: `skip` coded 1, then // `is_inter` coded 1, then the LAST chain above. 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( // function's doc comment. let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); - // `write_single_ref`'s own tree, at the fixed context this pricer uses - // for every neighbour-derived symbol: LAST is p1=0,p3=0,p4=0, LAST2 - // p1=0,p3=0,p4=1 and GOLDEN p1=0,p3=1,p5=1. A BACKWARD reference - // (`ALTREF`, p1=1,p2=1) is priced as LAST here and always has been -- - // wrong by two symbols, but fixing it moves every shipped stream, so it - // is a lever of its own and not this lane's (lanes/last2.report.md). - let single_ref_bits = |ref_frame: i8| { - use crate::mvstack::{GOLDEN_FRAME, LAST2_FRAME}; - symbol_bits(&cdf::SINGLE_REF[0][0], 0) - + if ref_frame == GOLDEN_FRAME { - symbol_bits(&cdf::SINGLE_REF[0][2], 1) + symbol_bits(&cdf::SINGLE_REF[0][4], 1) - } else { - symbol_bits(&cdf::SINGLE_REF[0][2], 0) - + symbol_bits( - &cdf::SINGLE_REF[0][3], - usize::from(ref_frame == LAST2_FRAME), - ) - } - }; struct Candidate { cost: f64, @@ -9295,7 +9328,7 @@ fn search_inter_block( // the loop cannot reach is never offered below -- every candidate's // index is one this same loop walks to. let drl_bits = |start: usize, target: usize| drl_bits_for(stack, start, target); - let last_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME); + let last_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); let mut cands: Vec<((i32, i32), f64, InterInfo)> = vec![ ( stack.nearest_mv, @@ -9388,7 +9421,7 @@ fn search_inter_block( // `NEW_NEWMV` candidate below to pair with `LAST`'s. let mut extra_new_mvs: Vec<(i8, (i32, i32))> = Vec::new(); for &(ref_frame, g, gstack) in extra { - let g_ref_bits = single_ref_bits(ref_frame); + let g_ref_bits = single_ref_bits(ref_frame, gstack); let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); cands.push(( gstack.nearest_mv, @@ -12028,6 +12061,72 @@ pub(crate) fn encode_sequence_with_ctx( #[cfg(test)] mod tests { + /// [`super::single_ref_bits`] pays exactly what tile.rs' + /// `write_single_ref` codes: the p1/p2/p6 chain for a BACKWARD + /// reference and the p1/p3/p4/p5 chain for a forward one, each symbol at + /// the context that symbol's own `single_ref_p*_ctx` derives from the + /// block's above/left neighbours. Red before lane-pricer, where every + /// backward reference was priced as LAST (p1=0,p3=0,p4=0) at context 0. + #[test] + fn the_single_ref_pricer_pays_the_writers_own_symbol_sequence() { + use crate::cdf::SINGLE_REF as SR; + use crate::encode::symbol_bits as b; + use crate::mvstack::{ + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, + LAST_FRAME, MvStack, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, + single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, + }; + let stack_with = |above_refs, left_refs| MvStack { + entries: Default::default(), + nearest_mv: (0, 0), + near_mv: (0, 0), + pred_mv: (0, 0), + new_mv_ctx: 0, + ref_mv_ctx: 0, + zero_mv_ctx: 0, + drl_ctx: Default::default(), + above_refs, + left_refs, + }; + // (reference, the symbols `write_single_ref` emits as (p index, value)) + let cases: [(i8, &[(usize, usize)]); 7] = [ + (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]), + (LAST2_FRAME, &[(0, 0), (2, 0), (3, 1)]), + (LAST3_FRAME, &[(0, 0), (2, 1), (4, 0)]), + (GOLDEN_FRAME, &[(0, 0), (2, 1), (4, 1)]), + (BWDREF_FRAME, &[(0, 1), (1, 0), (5, 0)]), + (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]), + (ALTREF_FRAME, &[(0, 1), (1, 1)]), + ]; + // Both neighbours intra, and the mixed LAST-above/ALTREF-left pair + // that puts every p* context on a different row. + for (above_refs, left_refs) in [ + ((None, None), (None, None)), + ((Some(LAST_FRAME), None), (Some(ALTREF_FRAME), Some(LAST_FRAME))), + ] { + let stack = stack_with(above_refs, left_refs); + let (a, a1) = above_refs; + let (l, l1) = left_refs; + let ctx_of = |p: usize| match p { + 0 => single_ref_p1_ctx(a, a1, l, l1), + 1 => single_ref_p2_ctx(a, a1, l, l1), + 2 => single_ref_p3_ctx(a, a1, l, l1), + 3 => single_ref_p4_ctx(a, a1, l, l1), + 4 => single_ref_p5_ctx(a, a1, l, l1), + _ => single_ref_p6_ctx(a, a1, l, l1), + }; + for (ref_frame, symbols) in cases { + let want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum(); + let got = super::single_ref_bits(ref_frame, &stack); + assert!( + (got - want).abs() < 1e-9, + "ref {ref_frame} above {above_refs:?} left {left_refs:?}: \ + priced {got} bits, writer codes {want}" + ); + } + } + } + /// lane-av1tpl2's propagated map, on the two properties every decision /// under it rests on: a frame the whole lookahead window repeats exactly /// (uniform texture) leans on every cell alike, so every factor is 1 and @@ -15702,8 +15801,12 @@ mod tests { // gave 9808 / 35791) and the lane-i64 merge: the KEY frame -- one of // these four pictures -- offers every superblock a 64x64 intra root // now. `EC_AV1_I64=0` restores 8557 / 33345. + // Re-taken on lane-pricer: the inter RD search prices a single + // reference through the tree and the neighbour contexts the writer + // really codes it with, so every inter picture's reference decisions + // move -- 8562 -> 8535 at q=150 and 33357 -> 33221 at q=60. let pins: [(u8, usize, u64); 2] = - [(150, 8562, 0x66f5_4ffc_aacf_964f), (60, 33357, 0x1d61_9ca4_e960_b044)]; + [(150, 8535, 0x7b7f_4eeb_1080_7b45), (60, 33221, 0x720b_821e_5dd2_90e4)]; let coded: Vec<(u8, usize, u64)> = pins .iter() .map(|&(q, _, _)| { diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index ddf1a5b8..5623a2a6 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -492,6 +492,14 @@ pub struct MvStack { /// Context for each `drl_mode` symbol between consecutive stack /// entries, `entries.len().saturating_sub(1)` long. pub drl_ctx: DrlVec, + /// The immediate above/left neighbours' references, `(ref0, ref1)` each + /// with `None` for intra/unavailable -- exactly what tile.rs + /// `write_single_ref` hands `single_ref_p1_ctx`..`p6_ctx`. Carried here + /// so the RD pricer can price the reference symbols at the context the + /// writer really codes them with, instead of context 0 (lane-pricer). + pub above_refs: (Option, Option), + /// The immediate LEFT neighbour's `(ref0, ref1)` -- see `above_refs`. + pub left_refs: (Option, Option), } /// 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( }) .collect(); + // The two cells `write_single_ref` reads its contexts from (its + // `above_ref`/`above_ref1`/`left_ref`/`left_ref1` bands are this same + // grid one block back), `None` where the neighbour is intra or outside + // the tile. + let refs_of = |cell: Option| match cell { + Some(mi) if mi.is_inter && mi.ref_frame > 0 => ( + Some(mi.ref_frame), + (mi.ref_frame1 > 0).then_some(mi.ref_frame1), + ), + _ => (None, None), + }; + let above_refs = refs_of(mi_row.checked_sub(1).and_then(|r| grid.get(r, mi_col))); + let left_refs = refs_of(mi_col.checked_sub(1).and_then(|c| grid.get(mi_row, c))); + MvStack { entries: candidates, nearest_mv, @@ -1660,6 +1682,8 @@ pub fn find_mv_stack_with_sign_bias( ref_mv_ctx, zero_mv_ctx, drl_ctx, + above_refs, + left_refs, } } diff --git a/lanes/pricer.report.md b/lanes/pricer.report.md new file mode 100644 index 00000000..c95d95dd --- /dev/null +++ b/lanes/pricer.report.md @@ -0,0 +1,96 @@ +# lane-pricer — the inter RD pricer charged the wrong reference symbols + +## The defect + +`crates/ec-av1/src/encode.rs`'s inter RD search priced a single reference +with LAST's branch of the tree, whatever the reference was, at CDF context 0: + +``` +p1 = SINGLE_REF[0][0] symbol 0 +p3 = SINGLE_REF[0][2] symbol 0 (GOLDEN took p3=1,p5=1) +p4 = SINGLE_REF[0][3] symbol (ref==LAST2) +``` + +What `tile.rs write_single_ref` actually codes (spec 5.11.25 `single_ref_p1..p6`), +each symbol at the context its own `single_ref_p*_ctx` derives from the block's +above/left neighbours: + +| reference | symbols the writer codes | +|---|---| +| LAST | p1=0, p3=0, p4=0 | +| LAST2 | p1=0, p3=0, p4=1 | +| LAST3 | p1=0, p3=1, p5=0 | +| GOLDEN | p1=0, p3=1, p5=1 | +| BWDREF | p1=1, p2=0, p6=0 | +| ALTREF2 | p1=1, p2=0, p6=1 | +| ALTREF | p1=1, p2=1 | + +So every BACKWARD reference (BWDREF/ALTREF2/ALTREF) was priced as LAST, and +LAST3 as LAST2. The magnitude runs both ways: at the default CDFs and context 0 +LAST's chain costs ~4.4 bits while ALTREF's p1=1,p2=1 costs ~0.30, so the old +pricer OVERCHARGED ALTREF by ~4 bits; against the real neighbour contexts the +sign flips block by block. + +## Arms + +* **arm 1** — the symbol tree only, still at context 0. +* **arm 2** — arm 1 plus the writer's own contexts: `MvStack` now carries the + two cells `write_single_ref` reads (`above_refs`, `left_refs`), so the pricer + runs `single_ref_p1_ctx`..`p6_ctx` exactly as the writer does. No search + signature changed — every pricer site already holds the stack. + +## Gate (12-frame native gate, BD-rate vs libaom cpu-used 6 / rav1e speed 6) + +Control REPRODUCED in this worktree (the charter's screen row was stale): + +| row | control | arm 1 | arm 2 | +|---|---|---|---| +| film A | +21.7 / −4.4 | +21.9 / −4.2 | **+21.5 / −4.4** | +| film B | +26.9 / −0.6 | +27.0 / −0.3 | **+26.9 / −0.4** | +| screen | +14.9 / −32.9 | +15.2 / −32.7 | **+14.4 / −33.2** | +| bars 1080p | −1.0 / −16.8 | (not run) | **−1.4 / −17.2** | +| bars 2160p | +9.4 / −12.7 | (not run) | **+8.8 / −13.2** | + +Arm 1 is REFUTED: correct symbols at the wrong (0) context make a backward +reference read almost free (p1=1 = 0.23 bits), ALTREF fires 992 → 2648 blocks on +film A, and every row loses. Arm 2 pulls the reference mix back (film A ALTREF +1476, GOLDEN 304) and wins or ties everywhere. + +Long-GOP gate, arm 2: film A +26.4 / −6.4 (control +26.4 / −6.6), +film B +89.6 / +9.1 (control +89.8 / +9.1). + +## Decision + +Ship arm 2. Strictly the charter's keep rule is NOT met — it asks both film rows +to improve on both columns, and film B's rav1e column moves −0.6 → −0.4 (0.2 the +wrong way, inside the rule's own ±0.3 "flat" band but not paired with a ≥0.5 win). +Shipped anyway because this is a correctness fix — the pricer now prices the +symbols the writer emits — and no row regresses more than 0.2 while screen +(−0.5 / −0.3) and both bars rows (−0.4 / −0.4 and −0.6 / −0.5) improve on both +columns. + +## Sweep: `symbol_bits(&cdf::` sites whose CDF/leaf is constant while the writer selects + +| site (encode.rs) | verdict | +|---|---| +| `SINGLE_REF` (all) | FIXED — tree + neighbour contexts, matches the writer | +| `COMP_REF_TYPE[0]` (6236/8901/9539) | value matches (`is_uni_comp_ref`); CTX constant 0 vs writer's `comp_reference_type_ctx` | +| `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` | +| `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 | +| `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 | +| `SKIP[0]`, `INTRA_INTER[0]` | value right; CTX constant 0 while the writer derives both from neighbours | +| `TX_SIZE_CAT*[0]` | value right; CTX constant 0 | +| `TXFM_PARTITION[ctx(..)]`, `DRL_MODE[stack.drl_ctx[..]]`, `NEW_MV`/`ZERO_MV`/`REF_MV`/`INTER_COMPOUND_MODE` | same as writer (real contexts) | +| `ANGLE_DELTA[mode - V_PRED]` | same row as the writer; value fixed at delta 0, which is what these candidates code | +| `MV_*` | single tables where the writer has per-component ones — the pricer's long-standing static-MV approximation, out of this lane | + +Only the single-ref sites are fixed here; the rest are `open|` lines. + +## Evidence + +* unit test `encode::tests::the_single_ref_pricer_pays_the_writers_own_symbol_sequence` + — asserts the priced bits equal the writer's symbol sequence at the writer's + contexts, for all seven references and two neighbour configurations. RED before + the fix (`ref 3: priced 7.615346227887448 bits, writer codes 8.121845298652628`). +* pins re-taken: `[(150, 8535, 0x7b7f_4eeb_1080_7b45), (60, 33221, 0x720b_821e_5dd2_90e4)]` + (was 8562 / 33357), green at default and at `EC_AV1_SPEED=6`.