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)
4112 symbol_bits(&cdf::INTRA_INTER[ctx], usize::from(inter))4112 symbol_bits(&cdf::INTRA_INTER[ctx], usize::from(inter))
4113 }4113 }
4114 4114
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+
4115 /// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol,4129 /// 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-derived4130+/// each at the context that symbol's own `single_ref_p*_ctx` derives from the
4117-/// symbol (the writer contexts each symbol off its above/left neighbours'4131+/// block's above/left neighbours -- plus the `comp_mode = 0` symbol
4118-/// references; the search does not have them). LAST is p1=0,p3=0,p4=0,4132+/// `write_comp_mode` codes ahead of it on a `reference_select` frame.
4133+/// LAST is p1=0,p3=0,p4=0,
4119 /// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1,4134 /// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1,
4120 /// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1.4135 /// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1.
4121 /// Before lane-pricer every BACKWARD reference was priced as LAST -- three4136 /// Before lane-pricer every BACKWARD reference was priced as LAST -- three
4122 /// symbols off the wrong CDFs -- which is what made the search believe an4137 /// 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.
4124 pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> f64 {4141 pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) -> f64 {
4125 use crate::mvstack::{4142 use crate::mvstack::{
4126 ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,4143 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) ->
4129 };4146 };
4130 let (a, a1) = stack.above_refs;4147 let (a, a1) = stack.above_refs;
4131 let (l, l1) = stack.left_refs;4148 let (l, l1) = stack.left_refs;
4149+ let comp_mode = comp_mode_single_bits(stack);
4132 let backward = ref_frame >= BWDREF_FRAME;4150 let backward = ref_frame >= BWDREF_FRAME;
4133 let p1 = symbol_bits(4151 let p1 = symbol_bits(
4134 &cdf::SINGLE_REF[single_ref_p1_ctx(a, a1, l, l1)][0],4152 &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) ->
4140 &cdf::SINGLE_REF[single_ref_p2_ctx(a, a1, l, l1)][1],4158 &cdf::SINGLE_REF[single_ref_p2_ctx(a, a1, l, l1)][1],
4141 usize::from(is_altref),4159 usize::from(is_altref),
4142 );4160 );
4143- return p14161+ return comp_mode
4162+ + p1
4144 + p24163 + p2
4145 + if is_altref {4164 + if is_altref {
4146 0.04165 0.0
@@ -4152,7 +4171,7 @@ pub(crate) fn single_ref_bits(ref_frame: i8, stack: &crate::mvstack::MvStack) ->
4152 };4171 };
4153 }4172 }
4154 let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME;4173 let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME;
4155- p1 + symbol_bits(4174+ comp_mode + p1 + symbol_bits(
4156 &cdf::SINGLE_REF[single_ref_p3_ctx(a, a1, l, l1)][2],4175 &cdf::SINGLE_REF[single_ref_p3_ctx(a, a1, l, l1)][2],
4157 usize::from(far),4176 usize::from(far),
4158 ) + if far {4177 ) + if far {
@@ -12349,11 +12368,16 @@ mod tests {
12349 LAST_FRAME, MvStack, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx,12368 LAST_FRAME, MvStack, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx,
12350 single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx,12369 single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx,
12351 };12370 };
12352- let stack_with = |above_refs, left_refs| MvStack {12371+ let stack_with = |above_refs, left_refs, above_nbr, left_nbr| MvStack {
12353 above_refs,12372 above_refs,
12354 left_refs,12373 left_refs,
12374+ above_nbr,
12375+ left_nbr,
12355 ..bare_stack()12376 ..bare_stack()
12356 };12377 };
12378+ let nbr = |ref0, ref1| {
12379+ Some(crate::mvstack::NeighbourRef { is_inter: true, ref0, ref1, uni: false })
12380+ };
12357 // (reference, the symbols `write_single_ref` emits as (p index, value))12381 // (reference, the symbols `write_single_ref` emits as (p index, value))
12358 let cases: [(i8, &[(usize, usize)]); 7] = [12382 let cases: [(i8, &[(usize, usize)]); 7] = [
12359 (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]),12383 (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]),
@@ -12364,13 +12388,20 @@ mod tests {
12364 (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]),12388 (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]),
12365 (ALTREF_FRAME, &[(0, 1), (1, 1)]),12389 (ALTREF_FRAME, &[(0, 1), (1, 1)]),
12366 ];12390 ];
12367- // Both neighbours intra, and the mixed LAST-above/ALTREF-left pair12391+ // Both neighbours intra, then the mixed LAST-above/ALTREF-left pair
12368- // that puts every p* context on a different row.12392+ // that puts every p* context on a different row -- the second with a
12369- for (above_refs, left_refs) in [12393+ // COMPOUND left neighbour, which is what takes `reference_mode_ctx`
12370- ((None, None), (None, None)),12394+ // off row 0.
12371- ((Some(LAST_FRAME), None), (Some(ALTREF_FRAME), Some(LAST_FRAME))),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+ ),
12372 ] {12403 ] {
12373- let stack = stack_with(above_refs, left_refs);12404+ let stack = stack_with(above_refs, left_refs, above_nbr, left_nbr);
12374 let (a, a1) = above_refs;12405 let (a, a1) = above_refs;
12375 let (l, l1) = left_refs;12406 let (l, l1) = left_refs;
12376 let ctx_of = |p: usize| match p {12407 let ctx_of = |p: usize| match p {
@@ -12382,7 +12413,17 @@ mod tests {
12382 _ => single_ref_p6_ctx(a, a1, l, l1),12413 _ => single_ref_p6_ctx(a, a1, l, l1),
12383 };12414 };
12384 for (ref_frame, symbols) in cases {12415 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+ }
12386 let got = super::single_ref_bits(ref_frame, &stack);12427 let got = super::single_ref_bits(ref_frame, &stack);
12387 assert!(12428 assert!(
12388 (got - want).abs() < 1e-9,12429 (got - want).abs() < 1e-9,
@@ -16075,9 +16116,12 @@ mod tests {
16075 // for `skip`/`is_inter` and for the whole compound reference tree, so16116 // for `skip`/`is_inter` and for the whole compound reference tree, so
16076 // every inter block's cost -- and with it the skip and reference16117 // every inter block's cost -- and with it the skip and reference
16077 // decisions -- moved: 8535 -> 8311 bytes at q=150 and 33221 -> 3308716118 // 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.
16079 let pins: [(u8, usize, u64); 2] =16123 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)];
16081 let coded: Vec<(u8, usize, u64)> = pins16125 let coded: Vec<(u8, usize, u64)> = pins
16082 .iter()16126 .iter()
16083 .map(|&(q, _, _)| {16127 .map(|&(q, _, _)| {