forge
Explore
Log in

Don't you dare go Hollow

Price a single reference by the tree the writer codes

Browse files at this commit

5d678d588bcd6f3ff02748fcfddf88670b4be7bdAhmet Kaan GümüşParent: 778c3d3
  • fix
    • price a single reference by the tree the writer codes
Show full message
fix(av1): price a single reference by the tree the writer codes

The inter RD pricer charged every BACKWARD single reference (BWDREF,
ALTREF2, ALTREF) as if it were LAST -- p1=0,p3=0,p4=0 instead of the
p1/p2/p6 chain tile.rs write_single_ref emits -- so an ALTREF block read
1.34 bits of reference syntax where it really costs 4.79, and LAST3 was
priced as LAST2. One shared single_ref_bits() now mirrors the writer's
whole tree at context 0; the two fixed LAST constants call it too.
crates/ec-av1/src/encode.rsmodified+80 -25
@@ -4004,6 +4004,50 @@ fn warp_prediction(
4004 Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)])4004 Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)])
4005 }4005 }
4006 4006
4007+/// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol,
4008+/// at the fixed context this RD pricer uses for every neighbour-derived
4009+/// symbol (the writer contexts each symbol off its above/left neighbours'
4010+/// references; the search does not have them). LAST is p1=0,p3=0,p4=0,
4011+/// LAST2 p1=0,p3=0,p4=1, LAST3 p1=0,p3=1,p5=0, GOLDEN p1=0,p3=1,p5=1,
4012+/// BWDREF p1=1,p2=0,p6=0, ALTREF2 p1=1,p2=0,p6=1 and ALTREF p1=1,p2=1.
4013+/// Before lane-pricer every BACKWARD reference was priced as LAST -- three
4014+/// symbols off the wrong CDFs -- which is what made the search believe an
4015+/// ALTREF block cost the same syntax as a LAST one.
4016+pub(crate) fn single_ref_bits(ref_frame: i8) -> f64 {
4017+ use crate::mvstack::{
4018+ ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
4019+ };
4020+ let backward = ref_frame >= BWDREF_FRAME;
4021+ let p1 = symbol_bits(&cdf::SINGLE_REF[0][0], usize::from(backward));
4022+ if backward {
4023+ let is_altref = ref_frame == ALTREF_FRAME;
4024+ let p2 = symbol_bits(&cdf::SINGLE_REF[0][1], usize::from(is_altref));
4025+ return p1
4026+ + p2
4027+ + if is_altref {
4028+ 0.0
4029+ } else {
4030+ symbol_bits(
4031+ &cdf::SINGLE_REF[0][5],
4032+ usize::from(ref_frame == ALTREF2_FRAME),
4033+ )
4034+ };
4035+ }
4036+ let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME;
4037+ p1 + symbol_bits(&cdf::SINGLE_REF[0][2], usize::from(far))
4038+ + if far {
4039+ symbol_bits(
4040+ &cdf::SINGLE_REF[0][4],
4041+ usize::from(ref_frame == GOLDEN_FRAME),
4042+ )
4043+ } else {
4044+ symbol_bits(
4045+ &cdf::SINGLE_REF[0][3],
4046+ usize::from(ref_frame == LAST2_FRAME),
4047+ )
4048+ }
4049+}
4050+
4007 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {4051 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
4008 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };4052 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };
4009 let width = cdf[symbol] - low;4053 let width = cdf[symbol] - low;
@@ -5949,9 +5993,7 @@ fn code_square_inter(
5949 5993
5950 let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip));5994 let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip));
5951 let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter));5995 let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter));
5952- let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0)5996+ let single_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME);
5953- + symbol_bits(&cdf::SINGLE_REF[0][2], 0)
5954- + symbol_bits(&cdf::SINGLE_REF[0][3], 0);
5955 let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV5997 let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV
5956 + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero5998 + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero
5957 + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV5999 + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV
@@ -8736,9 +8778,7 @@ fn search_skip_64(
8736 let side = SUPERBLOCK;8778 let side = SUPERBLOCK;
8737 let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1);8779 let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1);
8738 let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1);8780 let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1);
8739- let ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0)8781+ let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME);
8740- + symbol_bits(&cdf::SINGLE_REF[0][2], 0)
8741- + symbol_bits(&cdf::SINGLE_REF[0][3], 0);
8742 // What every candidate pays whatever its vector: `skip` coded 1, then8782 // What every candidate pays whatever its vector: `skip` coded 1, then
8743 // `is_inter` coded 1, then the LAST chain above.8783 // `is_inter` coded 1, then the LAST chain above.
8744 let fixed = symbol_bits(&cdf::SKIP[0], 1) + symbol_bits(&cdf::INTRA_INTER[0], 1) + ref_bits;8784 let fixed = symbol_bits(&cdf::SKIP[0], 1) + symbol_bits(&cdf::INTRA_INTER[0], 1) + ref_bits;
@@ -9121,25 +9161,6 @@ fn search_inter_block(
9121 // function's doc comment.9161 // function's doc comment.
9122 let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip));9162 let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip));
9123 let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter));9163 let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter));
9124- // `write_single_ref`'s own tree, at the fixed context this pricer uses
9125- // for every neighbour-derived symbol: LAST is p1=0,p3=0,p4=0, LAST2
9126- // p1=0,p3=0,p4=1 and GOLDEN p1=0,p3=1,p5=1. A BACKWARD reference
9127- // (`ALTREF`, p1=1,p2=1) is priced as LAST here and always has been --
9128- // wrong by two symbols, but fixing it moves every shipped stream, so it
9129- // is a lever of its own and not this lane's (lanes/last2.report.md).
9130- let single_ref_bits = |ref_frame: i8| {
9131- use crate::mvstack::{GOLDEN_FRAME, LAST2_FRAME};
9132- symbol_bits(&cdf::SINGLE_REF[0][0], 0)
9133- + if ref_frame == GOLDEN_FRAME {
9134- symbol_bits(&cdf::SINGLE_REF[0][2], 1) + symbol_bits(&cdf::SINGLE_REF[0][4], 1)
9135- } else {
9136- symbol_bits(&cdf::SINGLE_REF[0][2], 0)
9137- + symbol_bits(
9138- &cdf::SINGLE_REF[0][3],
9139- usize::from(ref_frame == LAST2_FRAME),
9140- )
9141- }
9142- };
9143 9164
9144 struct Candidate {9165 struct Candidate {
9145 cost: f64,9166 cost: f64,
@@ -12028,6 +12049,40 @@ pub(crate) fn encode_sequence_with_ctx(
12028 #[cfg(test)]12049 #[cfg(test)]
12029 mod tests {12050 mod tests {
12030 12051
12052+ /// [`super::single_ref_bits`] pays exactly what tile.rs'
12053+ /// `write_single_ref` codes, symbol for symbol, at context 0: the p1/p2/
12054+ /// p6 chain for a BACKWARD reference and the p1/p3/p4/p5 chain for a
12055+ /// forward one. Red before lane-pricer, where every backward reference
12056+ /// was priced as LAST (p1=0,p3=0,p4=0) -- ALTREF read 1.34 bits instead
12057+ /// of its true 4.79.
12058+ #[test]
12059+ fn the_single_ref_pricer_pays_the_writers_own_symbol_sequence() {
12060+ use crate::cdf::SINGLE_REF as SR;
12061+ use crate::encode::symbol_bits as b;
12062+ use crate::mvstack::{
12063+ ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
12064+ LAST_FRAME,
12065+ };
12066+ // (reference, the symbols `write_single_ref` emits as (p index, value))
12067+ let cases: [(i8, &[(usize, usize)]); 7] = [
12068+ (LAST_FRAME, &[(0, 0), (2, 0), (3, 0)]),
12069+ (LAST2_FRAME, &[(0, 0), (2, 0), (3, 1)]),
12070+ (LAST3_FRAME, &[(0, 0), (2, 1), (4, 0)]),
12071+ (GOLDEN_FRAME, &[(0, 0), (2, 1), (4, 1)]),
12072+ (BWDREF_FRAME, &[(0, 1), (1, 0), (5, 0)]),
12073+ (ALTREF2_FRAME, &[(0, 1), (1, 0), (5, 1)]),
12074+ (ALTREF_FRAME, &[(0, 1), (1, 1)]),
12075+ ];
12076+ for (ref_frame, symbols) in cases {
12077+ let want: f64 = symbols.iter().map(|&(p, v)| b(&SR[0][p], v)).sum();
12078+ let got = super::single_ref_bits(ref_frame);
12079+ assert!(
12080+ (got - want).abs() < 1e-9,
12081+ "ref {ref_frame}: priced {got} bits, writer codes {want}"
12082+ );
12083+ }
12084+ }
12085+
12031 /// lane-av1tpl2's propagated map, on the two properties every decision12086 /// lane-av1tpl2's propagated map, on the two properties every decision
12032 /// under it rests on: a frame the whole lookahead window repeats exactly12087 /// under it rests on: a frame the whole lookahead window repeats exactly
12033 /// (uniform texture) leans on every cell alike, so every factor is 1 and12088 /// (uniform texture) leans on every cell alike, so every factor is 1 and