forge
Explore
Log in

Don't you dare go Hollow

The RD pricer priced every compound pair as LAST+GOLDEN or LAST+ALTREF at context 0

Browse files at this commit

628f49e62fade91db84d3157c5b926a25910ab6bAhmet Kaan GümüşParent: b527fb1
  • fix
    • the RD pricer priced every compound pair as LAST+GOLDEN or LAST+ALTREF at context 0
Show full message
fix(av1): the RD pricer priced every compound pair as LAST+GOLDEN or LAST+ALTREF at context 0

The compound half of the reference tree was four constants: comp_bwdref[0][0]
coded 1 (ALTREF) whatever the real partner was, dropping the p6 symbol a
BWDREF or ALTREF2 pair also codes, and the uni_comp_ref leaves coded GOLDEN
for every forward partner. All of it at CDF row 0 while the writer contexts
every one of those symbols off its above/left neighbours.

compound_ref_bits now walks tile.rs' write_compound_ref_frames symbol for
symbol for the real pair, at comp_reference_type_ctx / single_ref_p*_ctx /
uni_comp_ref_p1_ctx, and prices comp_mode at reference_mode_ctx.
crates/ec-av1/src/encode.rsmodified+204 -44
@@ -4004,6 +4004,91 @@ fn warp_prediction(
40044004 Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)])
40054005 }
40064006
4007+/// What tile.rs' `write_compound_ref_frames` pays for the pair
4008+/// `(ref0, ref1)`, symbol for symbol, each at the context its own
4009+/// `single_ref_p*_ctx`/`uni_comp_ref_p1_ctx`/`comp_reference_type_ctx`
4010+/// derives from the block's above/left neighbours -- plus the `comp_mode`
4011+/// symbol itself at `reference_mode_ctx`.
4012+///
4013+/// Before lane-ctx this was four constants at CDF row 0 that assumed the pair
4014+/// was LAST+GOLDEN or LAST+ALTREF: `comp_bwdref[0][0] = 1` prices a BWDREF or
4015+/// ALTREF2 partner as ALTREF and drops its `p6` symbol entirely, and the
4016+/// `uni_comp_ref` leaves price a LAST+LAST2 pair as LAST+GOLDEN.
4017+pub(crate) fn compound_ref_bits(
4018+ (ref0, ref1): (i8, i8),
4019+ stack: &crate::mvstack::MvStack,
4020+) -> f64 {
4021+ use crate::mvstack::{
4022+ ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
4023+ comp_reference_type_ctx, is_uni_comp_ref, single_ref_p1_ctx, single_ref_p2_ctx,
4024+ single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx,
4025+ uni_comp_ref_p1_ctx,
4026+ };
4027+ let (a, a1) = stack.above_refs;
4028+ let (l, l1) = stack.left_refs;
4029+ let unidir = is_uni_comp_ref(ref0, ref1);
4030+ let comp_mode = symbol_bits(
4031+ &cdf::COMP_MODE[crate::mvstack::reference_mode_ctx(stack.above_nbr, stack.left_nbr)],
4032+ 1,
4033+ );
4034+ let ref_type = symbol_bits(
4035+ &cdf::COMP_REF_TYPE[comp_reference_type_ctx(stack.above_nbr, stack.left_nbr)],
4036+ usize::from(!unidir),
4037+ );
4038+ if unidir {
4039+ let both_backward = (ref0, ref1) == (BWDREF_FRAME, ALTREF_FRAME);
4040+ let p0 = symbol_bits(
4041+ &cdf::UNI_COMP_REF[single_ref_p1_ctx(a, a1, l, l1)][0],
4042+ usize::from(both_backward),
4043+ );
4044+ if both_backward {
4045+ return comp_mode + ref_type + p0;
4046+ }
4047+ let past_last2 = ref1 != LAST2_FRAME;
4048+ let p1 = symbol_bits(
4049+ &cdf::UNI_COMP_REF[uni_comp_ref_p1_ctx(a, a1, l, l1)][1],
4050+ usize::from(past_last2),
4051+ );
4052+ let p2 = if past_last2 {
4053+ symbol_bits(
4054+ &cdf::UNI_COMP_REF[single_ref_p5_ctx(a, a1, l, l1)][2],
4055+ usize::from(ref1 == GOLDEN_FRAME),
4056+ )
4057+ } else {
4058+ 0.0
4059+ };
4060+ return comp_mode + ref_type + p0 + p1 + p2;
4061+ }
4062+ let far = ref0 == LAST3_FRAME || ref0 == GOLDEN_FRAME;
4063+ let fwd = symbol_bits(
4064+ &cdf::COMP_REF[single_ref_p3_ctx(a, a1, l, l1)][0],
4065+ usize::from(far),
4066+ ) + if far {
4067+ symbol_bits(
4068+ &cdf::COMP_REF[single_ref_p5_ctx(a, a1, l, l1)][2],
4069+ usize::from(ref0 == GOLDEN_FRAME),
4070+ )
4071+ } else {
4072+ symbol_bits(
4073+ &cdf::COMP_REF[single_ref_p4_ctx(a, a1, l, l1)][1],
4074+ usize::from(ref0 == LAST2_FRAME),
4075+ )
4076+ };
4077+ let is_altref = ref1 == ALTREF_FRAME;
4078+ let bwd = symbol_bits(
4079+ &cdf::COMP_BWDREF[single_ref_p2_ctx(a, a1, l, l1)][0],
4080+ usize::from(is_altref),
4081+ ) + if is_altref {
4082+ 0.0
4083+ } else {
4084+ symbol_bits(
4085+ &cdf::COMP_BWDREF[single_ref_p6_ctx(a, a1, l, l1)][1],
4086+ usize::from(ref1 == ALTREF2_FRAME),
4087+ )
4088+ };
4089+ comp_mode + ref_type + fwd + bwd
4090+}
4091+
40074092 /// What the `skip` symbol costs at the context tile.rs really codes it with
40084093 /// (`above_skip[mi_c] + left_skip[mi_r]`, the two cells the MV stack now
40094094 /// carries) instead of row 0 -- the pricer's skip-versus-residual margin is
@@ -6264,20 +6349,9 @@ fn code_square_inter(
62646349 // compound-only syntax the writer emits.
62656350 for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) {
62666351 let (ref1, g) = (*ref1, *g);
6267- let uni =
6268- (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&ref1);
6269- let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1)
6270- + if uni {
6271- symbol_bits(&cdf::COMP_REF_TYPE[0], 1)
6272- + symbol_bits(&cdf::COMP_REF[0][0], 0)
6273- + symbol_bits(&cdf::COMP_REF[0][1], 0)
6274- + symbol_bits(&cdf::COMP_BWDREF[0][0], 1)
6275- } else {
6276- symbol_bits(&cdf::COMP_REF_TYPE[0], 0)
6277- + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0)
6278- + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1)
6279- + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1)
6280- };
6352+ // The whole `comp_mode` + reference-pair tree at the writer's own
6353+ // contexts, for THIS pair (lane-ctx).
6354+ let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack);
62816355 let mode_ctx =
62826356 cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)];
62836357 let mode_bits_of =
@@ -8941,19 +9015,9 @@ fn search_skip_64(
89419015 // reference's tree, plus the pair tree and the compound mode.
89429016 for (ref1, g, cstack) in compound.iter().filter(|_| b64_compound()) {
89439017 let (ref1, g) = (*ref1, *g);
8944- let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&ref1);
8945- let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1)
8946- + if uni {
8947- symbol_bits(&cdf::COMP_REF_TYPE[0], 1)
8948- + symbol_bits(&cdf::COMP_REF[0][0], 0)
8949- + symbol_bits(&cdf::COMP_REF[0][1], 0)
8950- + symbol_bits(&cdf::COMP_BWDREF[0][0], 1)
8951- } else {
8952- symbol_bits(&cdf::COMP_REF_TYPE[0], 0)
8953- + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0)
8954- + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1)
8955- + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1)
8956- };
9018+ // The whole `comp_mode` + reference-pair tree at the writer's own
9019+ // contexts, for THIS pair (lane-ctx).
9020+ let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack);
89579021 let mode_ctx =
89589022 cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)];
89599023 let mode_bits_of =
@@ -9576,23 +9640,9 @@ fn search_inter_block(
95769640 let half_new = true;
95779641 for (ref1, g, cstack) in compound {
95789642 let (ref1, g) = (*ref1, *g);
9579- let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME)
9580- .contains(&ref1);
9581- let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1)
9582- + if uni {
9583- // LAST + a backward reference: `comp_ref_type` = bidirectional,
9584- // `comp_ref` LAST, `comp_bwdref` ALTREF.
9585- symbol_bits(&cdf::COMP_REF_TYPE[0], 1)
9586- + symbol_bits(&cdf::COMP_REF[0][0], 0)
9587- + symbol_bits(&cdf::COMP_REF[0][1], 0)
9588- + symbol_bits(&cdf::COMP_BWDREF[0][0], 1)
9589- } else {
9590- // LAST + GOLDEN: unidirectional, `uni_comp_ref` p0/p1/p2.
9591- symbol_bits(&cdf::COMP_REF_TYPE[0], 0)
9592- + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0)
9593- + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1)
9594- + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1)
9595- };
9643+ // The whole `comp_mode` + reference-pair tree at the writer's own
9644+ // contexts, for THIS pair (lane-ctx).
9645+ let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack);
95969646 let mode_ctx =
95979647 cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)];
95989648 let mode_bits_of =
@@ -12117,6 +12167,116 @@ mod tests {
1211712167 }
1211812168 }
1211912169
12170+ /// [`super::compound_ref_bits`] pays exactly the symbols tile.rs'
12171+ /// `write_compound_ref_frames` codes for a pair, each at that symbol's
12172+ /// own neighbour-derived context. Red before lane-ctx, where the pricer
12173+ /// spent four constants at CDF row 0 that assumed the pair was
12174+ /// LAST+GOLDEN or LAST+ALTREF -- so a LAST+BWDREF pair was priced as
12175+ /// LAST+ALTREF with its `comp_bwdref` p6 symbol missing entirely.
12176+ #[test]
12177+ fn the_compound_ref_pricer_pays_the_writers_own_symbol_sequence() {
12178+ use crate::cdf::{COMP_BWDREF, COMP_MODE, COMP_REF, COMP_REF_TYPE, UNI_COMP_REF};
12179+ use crate::encode::symbol_bits as b;
12180+ use crate::mvstack::{
12181+ ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
12182+ LAST_FRAME, NeighbourRef, comp_reference_type_ctx, reference_mode_ctx,
12183+ single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx,
12184+ single_ref_p5_ctx, single_ref_p6_ctx, uni_comp_ref_p1_ctx,
12185+ };
12186+ // (pair, unidirectional?, the tree's symbols as (table, p index,
12187+ // context selector, value)) -- transcribed from the writer.
12188+ // selector: 1..=6 are `single_ref_p*_ctx`, 7 is `uni_comp_ref_p1_ctx`.
12189+ type Sym = (char, usize, usize, usize);
12190+ let cases: [((i8, i8), bool, &[Sym]); 7] = [
12191+ ((LAST_FRAME, LAST2_FRAME), true, &[('u', 0, 1, 0), ('u', 1, 7, 0)]),
12192+ (
12193+ (LAST_FRAME, LAST3_FRAME),
12194+ true,
12195+ &[('u', 0, 1, 0), ('u', 1, 7, 1), ('u', 2, 5, 0)],
12196+ ),
12197+ (
12198+ (LAST_FRAME, GOLDEN_FRAME),
12199+ true,
12200+ &[('u', 0, 1, 0), ('u', 1, 7, 1), ('u', 2, 5, 1)],
12201+ ),
12202+ ((BWDREF_FRAME, ALTREF_FRAME), true, &[('u', 0, 1, 1)]),
12203+ (
12204+ (LAST_FRAME, ALTREF_FRAME),
12205+ false,
12206+ &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 1)],
12207+ ),
12208+ (
12209+ (LAST_FRAME, BWDREF_FRAME),
12210+ false,
12211+ &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 0), ('b', 1, 6, 0)],
12212+ ),
12213+ (
12214+ (LAST_FRAME, ALTREF2_FRAME),
12215+ false,
12216+ &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 0), ('b', 1, 6, 1)],
12217+ ),
12218+ ];
12219+ let nbr = |ref0, ref1: Option<i8>| {
12220+ Some(NeighbourRef {
12221+ is_inter: true,
12222+ ref0,
12223+ ref1,
12224+ uni: ref1.is_some_and(|r1| crate::mvstack::is_uni_comp_ref(ref0, r1)),
12225+ })
12226+ };
12227+ // No neighbours (every context zero), then a mixed pair that puts the
12228+ // p* rows, `comp_reference_type_ctx` and `reference_mode_ctx` off 0.
12229+ for (above_nbr, left_nbr, above_refs, left_refs) in [
12230+ (None, None, (None, None), (None, None)),
12231+ (
12232+ nbr(LAST_FRAME, None),
12233+ nbr(ALTREF_FRAME, Some(GOLDEN_FRAME)),
12234+ (Some(LAST_FRAME), None),
12235+ (Some(ALTREF_FRAME), Some(GOLDEN_FRAME)),
12236+ ),
12237+ ] {
12238+ let stack = crate::mvstack::MvStack {
12239+ above_nbr,
12240+ left_nbr,
12241+ above_refs,
12242+ left_refs,
12243+ ..bare_stack()
12244+ };
12245+ let (a, a1) = above_refs;
12246+ let (l, l1) = left_refs;
12247+ let ctx_of = |sel: usize| match sel {
12248+ 1 => single_ref_p1_ctx(a, a1, l, l1),
12249+ 2 => single_ref_p2_ctx(a, a1, l, l1),
12250+ 3 => single_ref_p3_ctx(a, a1, l, l1),
12251+ 4 => single_ref_p4_ctx(a, a1, l, l1),
12252+ 5 => single_ref_p5_ctx(a, a1, l, l1),
12253+ 6 => single_ref_p6_ctx(a, a1, l, l1),
12254+ _ => uni_comp_ref_p1_ctx(a, a1, l, l1),
12255+ };
12256+ for (pair, unidir, symbols) in cases {
12257+ let mut want = b(&COMP_MODE[reference_mode_ctx(above_nbr, left_nbr)], 1)
12258+ + b(
12259+ &COMP_REF_TYPE[comp_reference_type_ctx(above_nbr, left_nbr)],
12260+ usize::from(!unidir),
12261+ );
12262+ for &(table, p, sel, v) in symbols {
12263+ let row = ctx_of(sel);
12264+ want += match table {
12265+ 'u' => b(&UNI_COMP_REF[row][p], v),
12266+ 'c' => b(&COMP_REF[row][p], v),
12267+ _ => b(&COMP_BWDREF[row][p], v),
12268+ };
12269+ }
12270+ let got = super::compound_ref_bits(pair, &stack);
12271+ assert!(
12272+ (got - want).abs() < 1e-9,
12273+ "pair {pair:?} above {above_refs:?} left {left_refs:?}: \
12274+ priced {got} bits, writer codes {want}"
12275+ );
12276+ }
12277+ }
12278+ }
12279+
1212012280 /// The `skip` and `is_inter` symbols are priced at the SAME context
1212112281 /// tile.rs codes them with -- `above_skip + left_skip` for the first and
1212212282 /// `intra_inter_ctx` for the second -- not at row 0. Red before lane-ctx