forge
Explore
Log in

Don't you dare go Hollow

The RD pricer pays comp_mode=0 for a single-reference candidate

Browse files at this commit

ef9e91a60948ff1933ea945d68e50baeeb6024c6Ahmet Kaan GümüşParent: 55a7370
  • fix
    • the RD pricer pays comp_mode=0 for a single-reference candidate
Show full message
fix(av1): the RD pricer pays comp_mode=0 for a single-reference candidate

On a reference_select frame the writer codes a comp_mode symbol ahead of
every inter block's reference syntax, but the pricer charged only the
compound side (comp_mode=1 inside compound_ref_bits) and the single side
nothing at all, so compound carried the whole symbol as a handicap.

single_ref_bits now adds comp_mode=0 at the writer's own
reference_mode_ctx, and pays nothing on a frame that carries no
reference_select bit -- exactly where write_comp_mode returns early.

Pins re-taken: 8325 / 33014.
crates/ec-av1/src/encode.rsmodified+60 -16
@@ -4112,15 +4112,32 @@ pub(crate) fn intra_inter_bits_at(stack: &crate::mvstack::MvStack, inter: bool)
41124112 symbol_bits(&cdf::INTRA_INTER[ctx], usize::from(inter))
41134113 }
41144114
4115+/// The `comp_mode = 0` symbol tile.rs' `write_comp_mode` codes ahead of a
4116+/// SINGLE-reference block's reference syntax, at `reference_mode_ctx`'s own
4117+/// context -- zero on a frame that carries no `reference_select` bit, where
4118+/// the writer codes no symbol at all.
4119+fn comp_mode_single_bits(stack: &crate::mvstack::MvStack) -> f64 {
4120+ if !reference_select() {
4121+ return 0.0;
4122+ }
4123+ symbol_bits(
4124+ &cdf::COMP_MODE[crate::mvstack::reference_mode_ctx(stack.above_nbr, stack.left_nbr)],
4125+ 0,
4126+ )
4127+}
4128+
41154129 /// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol,
4116-/// at the fixed context this RD pricer uses for every neighbour-derived
4117-/// symbol (the writer contexts each symbol off its above/left neighbours'
4118-/// references; the search does not have them). LAST is p1=0,p3=0,p4=0,
4130+/// each at the context that symbol's own `single_ref_p*_ctx` derives from the
4131+/// block's above/left neighbours -- plus the `comp_mode = 0` symbol
4132+/// `write_comp_mode` codes ahead of it on a `reference_select` frame.
4133+/// LAST is p1=0,p3=0,p4=0,
41194134 /// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1,
41204135 /// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1.
41214136 /// Before lane-pricer every BACKWARD reference was priced as LAST -- three
41224137 /// symbols off the wrong CDFs -- which is what made the search believe an
4123-/// ALTREF block cost the same syntax as a LAST one.
4138+/// ALTREF block cost the same syntax as a LAST one. The `comp_mode` term is
4139+/// lane-ctx2's: a compound candidate paid `comp_mode = 1` while a single one
4140+/// paid nothing at all, so compound was overcharged by the whole symbol.
41244141 pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> f64 {
41254142 use crate::mvstack::{
41264143 ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
@@ -4129,6 +4146,7 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) ->
41294146 };
41304147 let (a, a1) = stack.above_refs;
41314148 let (l, l1) = stack.left_refs;
4149+ let comp_mode = comp_mode_single_bits(stack);
41324150 let backward = ref_frame >= BWDREF_FRAME;
41334151 let p1 = symbol_bits(
41344152 &cdf::SINGLE_REF[single_ref_p1_ctx(a, a1, l, l1)][0],
@@ -4140,7 +4158,8 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) ->
41404158 &cdf::SINGLE_REF[single_ref_p2_ctx(a, a1, l, l1)][1],
41414159 usize::from(is_altref),
41424160 );
4143- return p1
4161+ return comp_mode
4162+ + p1
41444163 + p2
41454164 + if is_altref {
41464165 0.0
@@ -4152,7 +4171,7 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) ->
41524171 };
41534172 }
41544173 let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME;
4155- p1 + symbol_bits(
4174+ comp_mode + p1 + symbol_bits(
41564175 &cdf::SINGLE_REF[single_ref_p3_ctx(a, a1, l, l1)][2],
41574176 usize::from(far),
41584177 ) + if far {
@@ -12349,11 +12368,16 @@ mod tests {
1234912368 LAST_FRAME, MvStack, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx,
1235012369 single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx,
1235112370 };
12352- let stack_with = |above_refs, left_refs| MvStack {
12371+ let stack_with = |above_refs, left_refs, above_nbr, left_nbr| MvStack {
1235312372 above_refs,
1235412373 left_refs,
12374+ above_nbr,
12375+ left_nbr,
1235512376 ..bare_stack()
1235612377 };
12378+ let nbr = |ref0, ref1| {
12379+ Some(crate::mvstack::NeighbourRef { is_inter: true, ref0, ref1, uni: false })
12380+ };
1235712381 // (reference, the symbols `write_single_ref` emits as (p index, value))
1235812382 let cases: [(i8, &[(usize, usize)]); 7] = [
1235912383 (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]),
@@ -12364,13 +12388,20 @@ mod tests {
1236412388 (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]),
1236512389 (ALTREF_FRAME, &[(0, 1), (1, 1)]),
1236612390 ];
12367- // Both neighbours intra, and the mixed LAST-above/ALTREF-left pair
12368- // that puts every p* context on a different row.
12369- for (above_refs, left_refs) in [
12370- ((None, None), (None, None)),
12371- ((Some(LAST_FRAME), None), (Some(ALTREF_FRAME), Some(LAST_FRAME))),
12391+ // Both neighbours intra, then the mixed LAST-above/ALTREF-left pair
12392+ // that puts every p* context on a different row -- the second with a
12393+ // COMPOUND left neighbour, which is what takes `reference_mode_ctx`
12394+ // off row 0.
12395+ for (above_refs, left_refs, above_nbr, left_nbr) in [
12396+ ((None, None), (None, None), None, None),
12397+ (
12398+ (Some(LAST_FRAME), None),
12399+ (Some(ALTREF_FRAME), Some(LAST_FRAME)),
12400+ nbr(LAST_FRAME, None),
12401+ nbr(ALTREF_FRAME, Some(LAST_FRAME)),
12402+ ),
1237212403 ] {
12373- let stack = stack_with(above_refs, left_refs);
12404+ let stack = stack_with(above_refs, left_refs, above_nbr, left_nbr);
1237412405 let (a, a1) = above_refs;
1237512406 let (l, l1) = left_refs;
1237612407 let ctx_of = |p: usize| match p {
@@ -12382,7 +12413,17 @@ mod tests {
1238212413 _ => single_ref_p6_ctx(a, a1, l, l1),
1238312414 };
1238412415 for (ref_frame, symbols) in cases {
12385- let want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum();
12416+ // Plus the `comp_mode = 0` symbol the writer codes ahead of
12417+ // the reference syntax on a `reference_select` frame.
12418+ let mut want: f64 = symbols.iter().map(|&(p, v)| b(&SR[ctx_of(p)][p], v)).sum();
12419+ if super::reference_select() {
12420+ want += b(
12421+ &crate::cdf::COMP_MODE[crate::mvstack::reference_mode_ctx(
12422+ above_nbr, left_nbr,
12423+ )],
12424+ 0,
12425+ );
12426+ }
1238612427 let got = super::single_ref_bits(ref_frame, &stack);
1238712428 assert!(
1238812429 (got - want).abs() < 1e-9,
@@ -16075,9 +16116,12 @@ mod tests {
1607516116 // for `skip`/`is_inter` and for the whole compound reference tree, so
1607616117 // every inter block's cost -- and with it the skip and reference
1607716118 // decisions -- moved: 8535 -> 8311 bytes at q=150 and 33221 -> 33087
16078- // at q=60.
16119+ // at q=60. Re-taken on lane-ctx2: a single-reference candidate now
16120+ // pays the `comp_mode = 0` symbol the writer codes ahead of it, so
16121+ // single and compound are priced against the same syntax -- 8311 ->
16122+ // 8325 bytes at q=150 and 33087 -> 33014 at q=60.
1607916123 let pins: [(u8, usize, u64); 2] =
16080- [(150, 8311, 0xfb6d_75a4_5d13_3833), (60, 33087, 0x4d11_a03c_bdc6_e2d5)];
16124+ [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)];
1608116125 let coded: Vec<(u8, usize, u64)> = pins
1608216126 .iter()
1608316127 .map(|&(q, _, _)| {