diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 1bdc7812..e33ccdbc 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -4004,6 +4004,114 @@ fn warp_prediction( Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)]) } +/// What tile.rs' `write_compound_ref_frames` pays for the pair +/// `(ref0, ref1)`, symbol for symbol, each at the context its own +/// `single_ref_p*_ctx`/`uni_comp_ref_p1_ctx`/`comp_reference_type_ctx` +/// derives from the block's above/left neighbours -- plus the `comp_mode` +/// symbol itself at `reference_mode_ctx`. +/// +/// Before lane-ctx this was four constants at CDF row 0 that assumed the pair +/// was LAST+GOLDEN or LAST+ALTREF: `comp_bwdref[0][0] = 1` prices a BWDREF or +/// ALTREF2 partner as ALTREF and drops its `p6` symbol entirely, and the +/// `uni_comp_ref` leaves price a LAST+LAST2 pair as LAST+GOLDEN. +pub(crate) fn compound_ref_bits( + (ref0, ref1): (i8, i8), + stack: &crate::mvstack::MvStack, +) -> f64 { + use crate::mvstack::{ + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, + comp_reference_type_ctx, is_uni_comp_ref, single_ref_p1_ctx, single_ref_p2_ctx, + single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, + uni_comp_ref_p1_ctx, + }; + let (a, a1) = stack.above_refs; + let (l, l1) = stack.left_refs; + let unidir = is_uni_comp_ref(ref0, ref1); + let comp_mode = symbol_bits( + &cdf::COMP_MODE[crate::mvstack::reference_mode_ctx(stack.above_nbr, stack.left_nbr)], + 1, + ); + let ref_type = symbol_bits( + &cdf::COMP_REF_TYPE[comp_reference_type_ctx(stack.above_nbr, stack.left_nbr)], + usize::from(!unidir), + ); + if unidir { + let both_backward = (ref0, ref1) == (BWDREF_FRAME, ALTREF_FRAME); + let p0 = symbol_bits( + &cdf::UNI_COMP_REF[single_ref_p1_ctx(a, a1, l, l1)][0], + usize::from(both_backward), + ); + if both_backward { + return comp_mode + ref_type + p0; + } + let past_last2 = ref1 != LAST2_FRAME; + let p1 = symbol_bits( + &cdf::UNI_COMP_REF[uni_comp_ref_p1_ctx(a, a1, l, l1)][1], + usize::from(past_last2), + ); + let p2 = if past_last2 { + symbol_bits( + &cdf::UNI_COMP_REF[single_ref_p5_ctx(a, a1, l, l1)][2], + usize::from(ref1 == GOLDEN_FRAME), + ) + } else { + 0.0 + }; + return comp_mode + ref_type + p0 + p1 + p2; + } + let far = ref0 == LAST3_FRAME || ref0 == GOLDEN_FRAME; + let fwd = symbol_bits( + &cdf::COMP_REF[single_ref_p3_ctx(a, a1, l, l1)][0], + usize::from(far), + ) + if far { + symbol_bits( + &cdf::COMP_REF[single_ref_p5_ctx(a, a1, l, l1)][2], + usize::from(ref0 == GOLDEN_FRAME), + ) + } else { + symbol_bits( + &cdf::COMP_REF[single_ref_p4_ctx(a, a1, l, l1)][1], + usize::from(ref0 == LAST2_FRAME), + ) + }; + let is_altref = ref1 == ALTREF_FRAME; + let bwd = symbol_bits( + &cdf::COMP_BWDREF[single_ref_p2_ctx(a, a1, l, l1)][0], + usize::from(is_altref), + ) + if is_altref { + 0.0 + } else { + symbol_bits( + &cdf::COMP_BWDREF[single_ref_p6_ctx(a, a1, l, l1)][1], + usize::from(ref1 == ALTREF2_FRAME), + ) + }; + comp_mode + ref_type + fwd + bwd +} + +/// What the `skip` symbol costs at the context tile.rs really codes it with +/// (`above_skip[mi_c] + left_skip[mi_r]`, the two cells the MV stack now +/// carries) instead of row 0 -- the pricer's skip-versus-residual margin is +/// exactly the difference between this symbol's two values, so the row it is +/// read from moves that decision (class pricer-context-zero, lane-ctx). +pub(crate) fn skip_bits_at(stack: &crate::mvstack::MvStack, skip: bool) -> f64 { + let ctx = usize::from(stack.above_skip) + usize::from(stack.left_skip); + symbol_bits(&cdf::SKIP[ctx], usize::from(skip)) +} + +/// What the `is_inter` symbol costs at tile.rs' own `intra_inter_ctx` -- the +/// neighbours' intra/inter state, with an out-of-tile side contributing +/// nothing, off the same two cells (lane-ctx). +pub(crate) fn intra_inter_bits_at(stack: &crate::mvstack::MvStack, inter: bool) -> f64 { + let ctx = crate::tile::intra_inter_ctx( + stack.above_nbr.is_some(), + stack.left_nbr.is_some(), + stack.above_nbr.is_some_and(|n| n.is_inter), + stack.left_nbr.is_some_and(|n| n.is_inter), + ); + symbol_bits(&cdf::INTRA_INTER[ctx], usize::from(inter)) +} + /// 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' @@ -6003,8 +6111,8 @@ fn code_square_inter( TxbSet::Chroma8 }; - 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 skip_bits = |skip: bool| skip_bits_at(stack, skip); + let intra_inter_bits = |inter: bool| intra_inter_bits_at(stack, inter); 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 @@ -6241,20 +6349,9 @@ fn code_square_inter( // compound-only syntax the writer emits. for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) { let (ref1, g) = (*ref1, *g); - let uni = - (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&ref1); - let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) - + if uni { - symbol_bits(&cdf::COMP_REF_TYPE[0], 1) - + symbol_bits(&cdf::COMP_REF[0][0], 0) - + symbol_bits(&cdf::COMP_REF[0][1], 0) - + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) - } else { - symbol_bits(&cdf::COMP_REF_TYPE[0], 0) - + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) - + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) - + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) - }; + // The whole `comp_mode` + reference-pair tree at the writer's own + // contexts, for THIS pair (lane-ctx). + let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack); let mode_ctx = cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; let mode_bits_of = @@ -6704,7 +6801,16 @@ fn code_square_inter( /// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra /// block casts no vote but is still a coded cell (see `code_square_inter`'s /// note on `processed_rows`/`processed_cols`). -fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: Option) { +fn record_mi( + grid: &mut MiGrid, + mi_row: usize, + mi_col: usize, + size: u8, + inter: Option, + // lane-ctx: the block's own `skip`, so the next block's MV stack can hand + // the RD pricer the two cells the writer's `skip_ctx` sums. + skip: bool, +) { let info = match inter { Some(info) => MiInfo { is_inter: true, @@ -6748,6 +6854,7 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O for dr in 0..usize::from(size) { for dc in 0..usize::from(size) { grid.set(mi_row + dr, mi_col + dc, info); + grid.set_skip(mi_row + dr, mi_col + dc, skip); } } } @@ -8793,7 +8900,7 @@ fn search_skip_64( 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; + let fixed = skip_bits_at(stack, true) + intra_inter_bits_at(stack, true) + ref_bits; let info_of = |mode: InterMode, mv: (i32, i32), ref_mv_idx: u8| InterInfo { ref1: None, mv1: (0, 0), @@ -8907,19 +9014,9 @@ fn search_skip_64( // reference's tree, plus the pair tree and the compound mode. for (ref1, g, cstack) in compound.iter().filter(|_| b64_compound()) { let (ref1, g) = (*ref1, *g); - let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&ref1); - let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) - + if uni { - symbol_bits(&cdf::COMP_REF_TYPE[0], 1) - + symbol_bits(&cdf::COMP_REF[0][0], 0) - + symbol_bits(&cdf::COMP_REF[0][1], 0) - + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) - } else { - symbol_bits(&cdf::COMP_REF_TYPE[0], 0) - + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) - + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) - + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) - }; + // The whole `comp_mode` + reference-pair tree at the writer's own + // contexts, for THIS pair (lane-ctx). + let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack); let mode_ctx = cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; let mode_bits_of = @@ -9057,7 +9154,7 @@ fn search_skip_64( sets[plane], ) }); - let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); + let unskip = skip_bits_at(stack, false) - skip_bits_at(stack, true); let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); // lane-b64b: the root's own var-tx decision -- one TX_64X64 or the // four TX_32X32 units of `txfm_split = 1`, taken by the same @@ -9171,8 +9268,8 @@ fn search_inter_block( // skip / intra_inter symbol costs at a fixed context -- see this // 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)); + let skip_bits = |skip: bool| skip_bits_at(stack, skip); + let intra_inter_bits = |inter: bool| intra_inter_bits_at(stack, inter); struct Candidate { cost: f64, @@ -9542,23 +9639,9 @@ fn search_inter_block( let half_new = true; for (ref1, g, cstack) in compound { let (ref1, g) = (*ref1, *g); - let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME) - .contains(&ref1); - let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) - + if uni { - // LAST + a backward reference: `comp_ref_type` = bidirectional, - // `comp_ref` LAST, `comp_bwdref` ALTREF. - symbol_bits(&cdf::COMP_REF_TYPE[0], 1) - + symbol_bits(&cdf::COMP_REF[0][0], 0) - + symbol_bits(&cdf::COMP_REF[0][1], 0) - + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) - } else { - // LAST + GOLDEN: unidirectional, `uni_comp_ref` p0/p1/p2. - symbol_bits(&cdf::COMP_REF_TYPE[0], 0) - + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) - + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) - + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) - }; + // The whole `comp_mode` + reference-pair tree at the writer's own + // contexts, for THIS pair (lane-ctx). + let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack); let mode_ctx = cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; let mode_bits_of = @@ -11393,7 +11476,7 @@ pub(crate) fn encode_inter_frame( fctx, ); cost8 += cost; - record_mi(&mut grid, mr, mc, 2, leaf8.inter); + record_mi(&mut grid, mr, mc, 2, leaf8.inter, leaf8.skip); eight.push(leaf8); } (eight, cost8) @@ -11415,7 +11498,7 @@ pub(crate) fn encode_inter_frame( } partition_hit(2); cost += leaf_cost; - record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter); + record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter, leaf.skip); leaves.push(leaf); } (leaves, cost) @@ -11434,7 +11517,7 @@ pub(crate) fn encode_inter_frame( } partition_hit(0); sb_cost += cost_whole; - record_mi(&mut grid, mi_row, mi_col, 8, block.inter); + record_mi(&mut grid, mi_row, mi_col, 8, block.inter, block.skip); blocks.push((r32 * cols + c32, Quadrant::Whole(block))); } else { // The sub-positions actually inside the true frame, same @@ -11501,7 +11584,7 @@ pub(crate) fn encode_inter_frame( // to (0,0), which is the encoder-grid-drift class // -- correct only for as long as a leaf can never // be compound. - record_mi(&mut grid, mi_row, mi_col, 4, block.inter); + record_mi(&mut grid, mi_row, mi_col, 4, block.inter, block.skip); leaves.push(block); } else { let (x_sub, y_sub) = (sc * SUB, sr * SUB); @@ -11540,7 +11623,7 @@ pub(crate) fn encode_inter_frame( ); // Same publication point as the 16x16 // straddling leaf above (`record_mi`). - record_mi(&mut grid, mi_row, mi_col, 2, leaf.inter); + record_mi(&mut grid, mi_row, mi_col, 2, leaf.inter, leaf.skip); eight.push(leaf); } leaves.push(BlockCoeffs { @@ -11571,7 +11654,7 @@ pub(crate) fn encode_inter_frame( // Overwrites every 16x16 mi cell the quadrant searches // published under this superblock, so the next block's stack // is built off the block that was really coded. - record_mi(&mut grid, sb_r * 16, sb_c * 16, 16, block.inter); + record_mi(&mut grid, sb_r * 16, sb_c * 16, 16, block.inter, block.skip); blocks.push(((sb_r * 2) * cols + sb_c * 2, Quadrant::Whole64(block))); for q in 1..4 { let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); @@ -12061,6 +12144,196 @@ pub(crate) fn encode_sequence_with_ctx( #[cfg(test)] mod tests { + /// An [`crate::mvstack::MvStack`] with every context at zero and no + /// neighbours, for the pricer tests below to fill the one band each of + /// them is about. + fn bare_stack() -> crate::mvstack::MvStack { + crate::mvstack::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: (None, None), + left_refs: (None, None), + above_nbr: None, + left_nbr: None, + above_skip: false, + left_skip: false, + } + } + + /// [`super::compound_ref_bits`] pays exactly the symbols tile.rs' + /// `write_compound_ref_frames` codes for a pair, each at that symbol's + /// own neighbour-derived context. Red before lane-ctx, where the pricer + /// spent four constants at CDF row 0 that assumed the pair was + /// LAST+GOLDEN or LAST+ALTREF -- so a LAST+BWDREF pair was priced as + /// LAST+ALTREF with its `comp_bwdref` p6 symbol missing entirely. + #[test] + fn the_compound_ref_pricer_pays_the_writers_own_symbol_sequence() { + use crate::cdf::{COMP_BWDREF, COMP_MODE, COMP_REF, COMP_REF_TYPE, UNI_COMP_REF}; + use crate::encode::symbol_bits as b; + use crate::mvstack::{ + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, + LAST_FRAME, NeighbourRef, comp_reference_type_ctx, reference_mode_ctx, + single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, + single_ref_p5_ctx, single_ref_p6_ctx, uni_comp_ref_p1_ctx, + }; + // (pair, unidirectional?, the tree's symbols as (table, p index, + // context selector, value)) -- transcribed from the writer. + // selector: 1..=6 are `single_ref_p*_ctx`, 7 is `uni_comp_ref_p1_ctx`. + type Sym = (char, usize, usize, usize); + let cases: [((i8, i8), bool, &[Sym]); 7] = [ + ((LAST_FRAME, LAST2_FRAME), true, &[('u', 0, 1, 0), ('u', 1, 7, 0)]), + ( + (LAST_FRAME, LAST3_FRAME), + true, + &[('u', 0, 1, 0), ('u', 1, 7, 1), ('u', 2, 5, 0)], + ), + ( + (LAST_FRAME, GOLDEN_FRAME), + true, + &[('u', 0, 1, 0), ('u', 1, 7, 1), ('u', 2, 5, 1)], + ), + ((BWDREF_FRAME, ALTREF_FRAME), true, &[('u', 0, 1, 1)]), + ( + (LAST_FRAME, ALTREF_FRAME), + false, + &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 1)], + ), + ( + (LAST_FRAME, BWDREF_FRAME), + false, + &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 0), ('b', 1, 6, 0)], + ), + ( + (LAST_FRAME, ALTREF2_FRAME), + false, + &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 0), ('b', 1, 6, 1)], + ), + ]; + let nbr = |ref0, ref1: Option| { + Some(NeighbourRef { + is_inter: true, + ref0, + ref1, + uni: ref1.is_some_and(|r1| crate::mvstack::is_uni_comp_ref(ref0, r1)), + }) + }; + // No neighbours (every context zero), then a mixed pair that puts the + // p* rows, `comp_reference_type_ctx` and `reference_mode_ctx` off 0. + for (above_nbr, left_nbr, above_refs, left_refs) in [ + (None, None, (None, None), (None, None)), + ( + nbr(LAST_FRAME, None), + nbr(ALTREF_FRAME, Some(GOLDEN_FRAME)), + (Some(LAST_FRAME), None), + (Some(ALTREF_FRAME), Some(GOLDEN_FRAME)), + ), + ] { + let stack = crate::mvstack::MvStack { + above_nbr, + left_nbr, + above_refs, + left_refs, + ..bare_stack() + }; + let (a, a1) = above_refs; + let (l, l1) = left_refs; + let ctx_of = |sel: usize| match sel { + 1 => single_ref_p1_ctx(a, a1, l, l1), + 2 => single_ref_p2_ctx(a, a1, l, l1), + 3 => single_ref_p3_ctx(a, a1, l, l1), + 4 => single_ref_p4_ctx(a, a1, l, l1), + 5 => single_ref_p5_ctx(a, a1, l, l1), + 6 => single_ref_p6_ctx(a, a1, l, l1), + _ => uni_comp_ref_p1_ctx(a, a1, l, l1), + }; + for (pair, unidir, symbols) in cases { + let mut want = b(&COMP_MODE[reference_mode_ctx(above_nbr, left_nbr)], 1) + + b( + &COMP_REF_TYPE[comp_reference_type_ctx(above_nbr, left_nbr)], + usize::from(!unidir), + ); + for &(table, p, sel, v) in symbols { + let row = ctx_of(sel); + want += match table { + 'u' => b(&UNI_COMP_REF[row][p], v), + 'c' => b(&COMP_REF[row][p], v), + _ => b(&COMP_BWDREF[row][p], v), + }; + } + let got = super::compound_ref_bits(pair, &stack); + assert!( + (got - want).abs() < 1e-9, + "pair {pair:?} above {above_refs:?} left {left_refs:?}: \ + priced {got} bits, writer codes {want}" + ); + } + } + } + + /// The `skip` and `is_inter` symbols are priced at the SAME context + /// tile.rs codes them with -- `above_skip + left_skip` for the first and + /// `intra_inter_ctx` for the second -- not at row 0. Red before lane-ctx + /// on every neighbour configuration whose context is not zero (class + /// pricer-context-zero). + #[test] + fn the_skip_and_intra_inter_pricers_use_the_writers_own_contexts() { + use crate::cdf::{INTRA_INTER, SKIP}; + use crate::encode::symbol_bits as b; + use crate::mvstack::NeighbourRef; + let nbr = |is_inter| NeighbourRef { + is_inter, + ref0: if is_inter { crate::mvstack::LAST_FRAME } else { 0 }, + ref1: None, + uni: false, + }; + for (above, left, above_skip, left_skip) in [ + (None, None, false, false), + (Some(nbr(true)), Some(nbr(true)), true, false), + (Some(nbr(false)), Some(nbr(false)), true, true), + (Some(nbr(false)), None, false, true), + (None, Some(nbr(true)), true, true), + ] { + let stack = crate::mvstack::MvStack { + above_nbr: above, + left_nbr: left, + above_skip, + left_skip, + ..bare_stack() + }; + let skip_ctx = usize::from(above_skip) + usize::from(left_skip); + let ii_ctx = crate::tile::intra_inter_ctx( + above.is_some(), + left.is_some(), + above.is_some_and(|n| n.is_inter), + left.is_some_and(|n| n.is_inter), + ); + for v in [false, true] { + let (got, want) = ( + super::skip_bits_at(&stack, v), + b(&SKIP[skip_ctx], usize::from(v)), + ); + assert!( + (got - want).abs() < 1e-9, + "skip {v} at ctx {skip_ctx}: priced {got}, writer codes {want}" + ); + let (got, want) = ( + super::intra_inter_bits_at(&stack, v), + b(&INTRA_INTER[ii_ctx], usize::from(v)), + ); + assert!( + (got - want).abs() < 1e-9, + "is_inter {v} at ctx {ii_ctx}: priced {got}, writer codes {want}" + ); + } + } + } + /// [`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 @@ -12077,16 +12350,9 @@ mod tests { 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, + ..bare_stack() }; // (reference, the symbols `write_single_ref` emits as (p index, value)) let cases: [(i8, &[(usize, usize)]); 7] = [ @@ -15805,8 +16071,13 @@ mod tests { // 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. + // Re-taken on lane-ctx: the RD pricer reads the writer's own contexts + // for `skip`/`is_inter` and for the whole compound reference tree, so + // every inter block's cost -- and with it the skip and reference + // decisions -- moved: 8535 -> 8311 bytes at q=150 and 33221 -> 33087 + // at q=60. let pins: [(u8, usize, u64); 2] = - [(150, 8535, 0x7b7f_4eeb_1080_7b45), (60, 33221, 0x720b_821e_5dd2_90e4)]; + [(150, 8311, 0xfb6d_75a4_5d13_3833), (60, 33087, 0x4d11_a03c_bdc6_e2d5)]; 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 5623a2a6..f6ac1631 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -251,6 +251,14 @@ pub struct MiGrid { cols: usize, rows: usize, cells: Vec>, + /// One `skip` flag per cell, beside `cells` rather than inside `MiInfo` + /// -- the mv-stack scan walks `cells` for every inter block, so the CELL + /// must stay 16 bytes (`mi_info_cell_stays_compact`), and nothing on the + /// scan path reads this. Only the ENCODER's own grid fills it + /// (`encode::record_mi`), for the RD pricer's `skip` context; the tile + /// writer and the decoder read their `skip` neighbours from their own + /// bands and leave this `false` (lane-ctx). + skips: Vec, /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV /// candidate scan never reaches across a tile boundary, mirroring /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction). @@ -279,6 +287,7 @@ impl MiGrid { cols, rows, cells: vec![None; cols * rows], + skips: vec![false; cols * rows], tile_row0: 0, tile_col0: 0, tile_row1: rows, @@ -327,6 +336,25 @@ impl MiGrid { } } + /// Records the `skip` flag of the unit at `(row, col)` -- see `skips`. + pub fn set_skip(&mut self, row: usize, col: usize, skip: bool) { + if row < self.rows && col < self.cols { + self.skips[row * self.cols + col] = skip; + } + } + + /// The `skip` flag of the unit at `(row, col)`, `false` outside this + /// tile's own window (the same clamp [`Self::get`] applies). + pub fn skip_at(&self, row: usize, col: usize) -> bool { + row < self.rows + && col < self.cols + && row >= self.tile_row0 + && row < self.tile_row1 + && col >= self.tile_col0 + && col < self.tile_col1 + && self.skips[row * self.cols + col] + } + /// Records one block's whole `bh4` x `bw4` footprint at once. Every /// caller published a block cell-by-cell through [`Self::set`]; the rows /// are contiguous, so this fills each of them with one `slice::fill` of @@ -500,6 +528,20 @@ pub struct MvStack { pub above_refs: (Option, Option), /// The immediate LEFT neighbour's `(ref0, ref1)` -- see `above_refs`. pub left_refs: (Option, Option), + /// The immediate above/left neighbours exactly as tile.rs' `above_nbr`/ + /// `left_nbr` build them for `reference_mode_ctx`/ + /// `comp_reference_type_ctx`, `None` outside the tile (which is what the + /// writer's `has_above`/`has_left` gate says). Carried so the RD pricer + /// prices `comp_mode` and the compound reference tree at the writer's own + /// contexts instead of context 0 (lane-ctx). + pub above_nbr: Option, + /// The immediate LEFT neighbour -- see `above_nbr`. + pub left_nbr: Option, + /// The two `skip` flags the writer's `skip_ctx` sums (its + /// `above_skip[mi_c]` + `left_skip[mi_r]`), `false` outside the tile. + pub above_skip: bool, + /// The immediate LEFT neighbour's `skip` -- see `above_skip`. + pub left_skip: bool, } /// Spec 7.10.2.8's threshold a candidate's weight is compared against to @@ -1673,6 +1715,33 @@ pub fn find_mv_stack_with_sign_bias( 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))); + // The same two cells again, in the shape tile.rs `above_nbr`/`left_nbr` + // hand `reference_mode_ctx`/`comp_reference_type_ctx`, plus each side's + // `skip` -- what the pricer's own `comp_mode`/compound-tree/`skip`/ + // `is_inter` contexts read (lane-ctx). + let above_cell = mi_row.checked_sub(1).and_then(|r| grid.get(r, mi_col)); + let left_cell = mi_col.checked_sub(1).and_then(|c| grid.get(mi_row, c)); + let nbr_of = |cell: Option| { + cell.map(|mi| { + let ref0 = if mi.is_inter { mi.ref_frame } else { 0 }; + let ref1 = (mi.is_inter && mi.ref_frame1 > 0).then_some(mi.ref_frame1); + NeighbourRef { + is_inter: mi.is_inter, + ref0, + ref1, + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), + } + }) + }; + let above_nbr = nbr_of(above_cell); + let left_nbr = nbr_of(left_cell); + let above_skip = mi_row + .checked_sub(1) + .is_some_and(|r| grid.skip_at(r, mi_col)); + let left_skip = mi_col + .checked_sub(1) + .is_some_and(|c| grid.skip_at(mi_row, c)); + MvStack { entries: candidates, nearest_mv, @@ -1684,6 +1753,10 @@ pub fn find_mv_stack_with_sign_bias( drl_ctx, above_refs, left_refs, + above_nbr, + left_nbr, + above_skip, + left_skip, } } @@ -2636,14 +2709,25 @@ pub(crate) fn uni_comp_ref_p1_ctx( /// `ref1`'s meaning only when compound: whether the pair libaom's /// `has_uni_comp_refs` would call unidirectional (both refs on the same /// side of the current frame). -#[derive(Clone, Copy)] -pub(crate) struct NeighbourRef { +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct NeighbourRef { + /// Whether the neighbour was coded inter. pub is_inter: bool, + /// Its `RefFrame[0]`, `0` (`INTRA_FRAME`) when it is intra. pub ref0: i8, + /// Its `RefFrame[1]`, `None` unless it was coded compound. pub ref1: Option, + /// Whether that compound pair is unidirectional. pub uni: bool, } +/// libaom `has_uni_comp_refs`: a compound pair is unidirectional when both +/// of its references sit on the same side of the current frame. One copy, +/// shared by the tile writer and the RD pricer (lane-ctx). +pub(crate) fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { + is_backward(ref0) == is_backward(ref1) +} + fn is_backward(r: i8) -> bool { (BWDREF_FRAME..=ALTREF_FRAME).contains(&r) } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 09c05856..db28f7ca 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -798,11 +798,7 @@ fn record_block_compound(neighbours: &mut Neighbours, at_mi: (usize, usize), sid /// decode.rs' own `is_uni_comp_ref`: a pair is `UNIDIR_COMP_REFERENCE` /// exactly when both references sit on the same temporal side. -fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { - let backward = - |r: i8| (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&r); - backward(ref0) == backward(ref1) -} +use crate::mvstack::is_uni_comp_ref; thread_local! { /// `(order_hint_bits, this frame's order hint, each reference's order @@ -5723,7 +5719,12 @@ fn dc_sign_ctx(vote: i32) -> usize { /// `pred_common.c`): both neighbours' intra/inter state when both are /// available, one neighbour's when only one is, and zero at a tile's own /// top-left corner. -fn intra_inter_ctx(has_above: bool, has_left: bool, above_inter: bool, left_inter: bool) -> usize { +pub(crate) fn intra_inter_ctx( + has_above: bool, + has_left: bool, + above_inter: bool, + left_inter: bool, +) -> usize { match (has_above, has_left) { (true, true) => { let (above_intra, left_intra) = (!above_inter, !left_inter); diff --git a/lanes/ctx.report.md b/lanes/ctx.report.md new file mode 100644 index 00000000..7a8657f9 --- /dev/null +++ b/lanes/ctx.report.md @@ -0,0 +1,92 @@ +# lane-ctx — the RD pricer's remaining context-zero sites + +Class: `pricer-context-zero`. lane-pricer fixed `single_ref_bits`; the sweep it +left named the rest. This lane took them. + +## 1. Enumeration — every `symbol_bits(&cdf::` site in `crates/ec-av1/src/encode.rs` + +| pricer site (encode.rs) | writer (tile.rs) | how the writer picks the context | verdict | +|---|---|---|---| +| `SKIP[0]` — `code_square_inter`, `search_skip_64`'s `fixed`, the b64 `unskip` term, `search_inter_block` | `enc.symbol(block.skip, &mut cdfs.skip[skip_ctx])` (4 sites: 6275/6673/6936/7189) | `above_skip[mi_c] + left_skip[mi_r]` | **arm A — FIXED** (`skip_bits_at`) | +| `INTRA_INTER[0]` — same four functions | `cdfs.intra_inter[ii_ctx]` (6284/6681/6944/7196) | `intra_inter_ctx(has_above, has_left, above_inter, left_inter)` | **arm A — FIXED** (`intra_inter_bits_at`) | +| `TX_SIZE_CAT0..3[0]` — `tx_depth_bits` (5 call sites in `code_square`) | `write_luma_select` / var-tx twin (4343/4436) | `Neighbours::tx_size_ctx(at_mi, max_tx)` = above/left published transform side >= this block's `max_tx`, and `tx_size_ctx_txfm` on the var-tx path | **arm B — DEFERRED** (see §5) | +| `COMP_MODE[0]` (3 sites) | `write_comp_mode` (1154) | `reference_mode_ctx(above_nbr, left_nbr)` | **arm C — FIXED** | +| `COMP_REF_TYPE[0]` (3) | `write_compound_ref_frames` (856) | `comp_reference_type_ctx(above, left)` | **arm C — FIXED** | +| `COMP_REF[0][0]=0`, `[0][1]=0` (3) | 889 / 899 | `single_ref_p3_ctx` / `single_ref_p4_ctx`; value right only because compound `ref0` is always LAST | **arm C — FIXED** (real pair) | +| `COMP_BWDREF[0][0]=1` (3) | 905 / 910 | `single_ref_p2_ctx`, then `single_ref_p6_ctx` when `ref1 != ALTREF` | **arm C — FIXED**: the constant priced BWDREF/ALTREF2 as ALTREF and dropped p6 | +| `UNI_COMP_REF[0][0]=0,[0][1]=1,[0][2]=1` (3) | 862 / 876 / 881 | `single_ref_p1_ctx` / `uni_comp_ref_p1_ctx` / `single_ref_p5_ctx` | **arm C — FIXED**: the constants priced every forward partner as GOLDEN | +| `SINGLE_REF[p*_ctx]` | `write_single_ref` | its own `single_ref_p*_ctx` | already fixed (lane-pricer) | +| `TXFM_PARTITION[ctx(..)]`, `DRL_MODE[stack.drl_ctx[..]]`, `NEW_MV`/`ZERO_MV`/`REF_MV`/`INTER_COMPOUND_MODE`, `ANGLE_DELTA[mode - V_PRED]`, `UV_MODE_CFL[mode]`, `Y_MODE[SIZE_GROUP_32]`, `FILTER_INTRA_MODE` | — | already the writer's own context/row | no change | +| `MV_JOINT`/`MV_SIGN`/`MV_CLASS`/`MV_BIT`/`MV_FR`/`MV_CLASS0_*` | `write_mv` | writer has PER-COMPONENT tables where the pricer has one shared set | out of this lane (long-standing static-MV approximation) | + +Open, found while enumerating: the single-reference candidates pay **no** +`comp_mode = 0` term at all while the compound ones pay `comp_mode = 1`, so +compound is still overcharged by `comp_mode(0)` relative to single. Left alone +here so arm C measures one thing. + +## 2. Arms and gates (12-frame native gate, BD-rate vs libaom cpu-used 6 / rav1e speed 6) + +Control reproduced in this worktree at `e74e7229`, exactly the charter's numbers. + +| row | control | arm A (skip + is_inter) | arm C (cumulative, + compound tree) | +|---|---|---|---| +| film A | +21.5 / −4.4 | +21.5 / −4.5 | **+21.0 / −4.8** | +| film B | +26.9 / −0.4 | +26.4 / −0.7 | **+25.0 / −1.8** | +| screen | +14.4 / −33.2 | +14.5 / −33.2 | **+14.5 / −33.2** | +| bars 1080p | −1.4 / −17.2 | −1.8 / −17.4 | −1.8 / −17.5 | +| bars 2160p | +8.8 / −13.2 | +8.5 / −13.5 | +8.8 / −13.3 | + +**arm A ships** under the second keep rule: film B is 0.5 down on libaom and +0.3 down on rav1e, film A flat on both (0.0 / −0.1), screen 0.1 up on libaom +(inside the 0.3 the rule allows). + +**arm C ships** under the first keep rule: both film rows improve on both +columns (−0.5/−0.3 and −1.4/−1.1), screen flat. + +Cumulative long-GOP gate (`bd_rate_film_long_gop`), both arms in: + +| row | control | shipped | +|---|---|---| +| film A | +26.4 / −6.4 | **+25.2 / −7.1** | +| film B | +89.6 / +9.1 | **+87.1 / +7.7** | + +## 3. Unit tests + +* `encode::tests::the_skip_and_intra_inter_pricers_use_the_writers_own_contexts` + — RED at row 0: `skip false at ctx 1: priced 0.049125199808023, writer codes + 0.9885106508271125`. Green after arm A. +* `encode::tests::the_compound_ref_pricer_pays_the_writers_own_symbol_sequence` + — RED against the old constant pricer: `pair (1, 2) ... priced + 10.19573638784928 bits, writer codes 5.455955052750427` (a LAST+LAST2 pair + priced as LAST+GOLDEN). Green after arm C. + +## 4. Pins and suite + +* Stream pins re-taken: `[(150, 8311, 0xfb6d_75a4_5d13_3833), (60, 33087, + 0x4d11_a03c_bdc6_e2d5)]` (was 8535 / 33221), green at the default preset and + at `EC_AV1_SPEED=6`. +* `mi_info_cell_stays_compact` / `mi_info_stays_small_enough_for_a_4k_grid` + went RED mid-lane: `skip` as a `MiInfo` field took the cell 16 → 18 bytes, + and that struct is the mv-stack scan's memory traffic. The flag moved to a + `Vec` beside `MiGrid::cells`, filled only by the encoder's `record_mi` + and read only by the stack. Both pins unchanged by that move, so the gate + numbers above stand. +* Split suite, release lib, all RC=0: s1 (`--skip stream::`) 338 passed / 0 + failed / 32 ignored; s2 (`stream:: --skip 10bit`) 42 / 0 / 1; s3 (`10bit`) + 208 / 0 / 16. `--ignored + encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders` + passed. +* `timeout 900 cargo check --workspace --all-targets -j4`: 0 errors, 0 ec-av1 + warnings (the workspace's 22 warnings are pre-existing, in ec-opus and + ec-vorbis). + +## 5. Deferred + +`deferred: arm B (tx_size depth category context) — the pricer's +tx_depth_bits lives in code_square, which takes neither the mi grid nor the +block's mi coordinates, and the writer's tx_size_ctx/tx_size_ctx_txfm read a +PER-TRANSFORM-UNIT published side (Neighbours::record_tx) that MiGrid does not +carry at all; reproducing it needs a new published tx band plus a parameter +through every code_square caller — what unblocks it: publish the resolved +transform side per mi cell beside MiGrid::cells the way this lane's skip band +now rides, then thread (grid, mi) into code_square.`