Merge branch 'lane-ctx' (ad3b6f81): the RD pricer reads the writer's contexts for skip, is_inter and the whole compound reference tree; pins re-taken 8311 / 33087
Browse files at this commit4 changed files +522 -74
- crates/ec-av1/src/encode.rs +337 -66
- crates/ec-av1/src/mvstack.rs +86 -2
- crates/ec-av1/src/tile.rs +7 -6
- lanes/ctx.report.md +92 -0
crates/ec-av1/src/encode.rsmodified+337 -66
| @@ -4004,6 +4004,114 @@ 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_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 | + | ||
| 4092 | +/// What the `skip` symbol costs at the context tile.rs really codes it with | ||
| 4093 | +/// (`above_skip[mi_c] + left_skip[mi_r]`, the two cells the MV stack now | ||
| 4094 | +/// carries) instead of row 0 -- the pricer's skip-versus-residual margin is | ||
| 4095 | +/// exactly the difference between this symbol's two values, so the row it is | ||
| 4096 | +/// read from moves that decision (class pricer-context-zero, lane-ctx). | ||
| 4097 | +pub(crate) fn skip_bits_at(stack: &crate::mvstack::MvStack, skip: bool) -> f64 { | ||
| 4098 | + let ctx = usize::from(stack.above_skip) + usize::from(stack.left_skip); | ||
| 4099 | + symbol_bits(&cdf::SKIP[ctx], usize::from(skip)) | ||
| 4100 | +} | ||
| 4101 | + | ||
| 4102 | +/// What the `is_inter` symbol costs at tile.rs' own `intra_inter_ctx` -- the | ||
| 4103 | +/// neighbours' intra/inter state, with an out-of-tile side contributing | ||
| 4104 | +/// nothing, off the same two cells (lane-ctx). | ||
| 4105 | +pub(crate) fn intra_inter_bits_at(stack: &crate::mvstack::MvStack, inter: bool) -> f64 { | ||
| 4106 | + let ctx = crate::tile::intra_inter_ctx( | ||
| 4107 | + stack.above_nbr.is_some(), | ||
| 4108 | + stack.left_nbr.is_some(), | ||
| 4109 | + stack.above_nbr.is_some_and(|n| n.is_inter), | ||
| 4110 | + stack.left_nbr.is_some_and(|n| n.is_inter), | ||
| 4111 | + ); | ||
| 4112 | + symbol_bits(&cdf::INTRA_INTER[ctx], usize::from(inter)) | ||
| 4113 | +} | ||
| 4114 | + | ||
| 4007 | /// What tile.rs' `write_single_ref` pays for `ref_frame`, symbol for symbol, | 4115 | /// 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 | 4116 | /// at the fixed context this RD pricer uses for every neighbour-derived |
| 4009 | /// symbol (the writer contexts each symbol off its above/left neighbours' | 4117 | /// symbol (the writer contexts each symbol off its above/left neighbours' |
| @@ -6003,8 +6111,8 @@ fn code_square_inter( | |||
| 6003 | TxbSet::Chroma8 | 6111 | TxbSet::Chroma8 |
| 6004 | }; | 6112 | }; |
| 6005 | 6113 | ||
| 6006 | - let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); | 6114 | + let skip_bits = |skip: bool| skip_bits_at(stack, skip); |
| 6007 | - let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); | 6115 | + let intra_inter_bits = |inter: bool| intra_inter_bits_at(stack, inter); |
| 6008 | let single_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); | 6116 | let single_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); |
| 6009 | let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV | 6117 | let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV |
| 6010 | + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero | 6118 | + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero |
| @@ -6241,20 +6349,9 @@ fn code_square_inter( | |||
| 6241 | // compound-only syntax the writer emits. | 6349 | // compound-only syntax the writer emits. |
| 6242 | for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) { | 6350 | for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) { |
| 6243 | let (ref1, g) = (*ref1, *g); | 6351 | let (ref1, g) = (*ref1, *g); |
| 6244 | - let uni = | 6352 | + // The whole `comp_mode` + reference-pair tree at the writer's own |
| 6245 | - (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&ref1); | 6353 | + // contexts, for THIS pair (lane-ctx). |
| 6246 | - let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) | 6354 | + let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack); |
| 6247 | - + if uni { | ||
| 6248 | - symbol_bits(&cdf::COMP_REF_TYPE[0], 1) | ||
| 6249 | - + symbol_bits(&cdf::COMP_REF[0][0], 0) | ||
| 6250 | - + symbol_bits(&cdf::COMP_REF[0][1], 0) | ||
| 6251 | - + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) | ||
| 6252 | - } else { | ||
| 6253 | - symbol_bits(&cdf::COMP_REF_TYPE[0], 0) | ||
| 6254 | - + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) | ||
| 6255 | - + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) | ||
| 6256 | - + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) | ||
| 6257 | - }; | ||
| 6258 | let mode_ctx = | 6355 | let mode_ctx = |
| 6259 | cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; | 6356 | cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; |
| 6260 | let mode_bits_of = | 6357 | let mode_bits_of = |
| @@ -6704,7 +6801,16 @@ fn code_square_inter( | |||
| 6704 | /// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra | 6801 | /// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra |
| 6705 | /// block casts no vote but is still a coded cell (see `code_square_inter`'s | 6802 | /// block casts no vote but is still a coded cell (see `code_square_inter`'s |
| 6706 | /// note on `processed_rows`/`processed_cols`). | 6803 | /// note on `processed_rows`/`processed_cols`). |
| 6707 | -fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: Option<InterInfo>) { | 6804 | +fn record_mi( |
| 6805 | + grid: &mut MiGrid, | ||
| 6806 | + mi_row: usize, | ||
| 6807 | + mi_col: usize, | ||
| 6808 | + size: u8, | ||
| 6809 | + inter: Option<InterInfo>, | ||
| 6810 | + // lane-ctx: the block's own `skip`, so the next block's MV stack can hand | ||
| 6811 | + // the RD pricer the two cells the writer's `skip_ctx` sums. | ||
| 6812 | + skip: bool, | ||
| 6813 | +) { | ||
| 6708 | let info = match inter { | 6814 | let info = match inter { |
| 6709 | Some(info) => MiInfo { | 6815 | Some(info) => MiInfo { |
| 6710 | is_inter: true, | 6816 | is_inter: true, |
| @@ -6748,6 +6854,7 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O | |||
| 6748 | for dr in 0..usize::from(size) { | 6854 | for dr in 0..usize::from(size) { |
| 6749 | for dc in 0..usize::from(size) { | 6855 | for dc in 0..usize::from(size) { |
| 6750 | grid.set(mi_row + dr, mi_col + dc, info); | 6856 | grid.set(mi_row + dr, mi_col + dc, info); |
| 6857 | + grid.set_skip(mi_row + dr, mi_col + dc, skip); | ||
| 6751 | } | 6858 | } |
| 6752 | } | 6859 | } |
| 6753 | } | 6860 | } |
| @@ -8793,7 +8900,7 @@ fn search_skip_64( | |||
| 8793 | let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); | 8900 | let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); |
| 8794 | // What every candidate pays whatever its vector: `skip` coded 1, then | 8901 | // What every candidate pays whatever its vector: `skip` coded 1, then |
| 8795 | // `is_inter` coded 1, then the LAST chain above. | 8902 | // `is_inter` coded 1, then the LAST chain above. |
| 8796 | - let fixed = symbol_bits(&cdf::SKIP[0], 1) + symbol_bits(&cdf::INTRA_INTER[0], 1) + ref_bits; | 8903 | + let fixed = skip_bits_at(stack, true) + intra_inter_bits_at(stack, true) + ref_bits; |
| 8797 | let info_of = |mode: InterMode, mv: (i32, i32), ref_mv_idx: u8| InterInfo { | 8904 | let info_of = |mode: InterMode, mv: (i32, i32), ref_mv_idx: u8| InterInfo { |
| 8798 | ref1: None, | 8905 | ref1: None, |
| 8799 | mv1: (0, 0), | 8906 | mv1: (0, 0), |
| @@ -8907,19 +9014,9 @@ fn search_skip_64( | |||
| 8907 | // reference's tree, plus the pair tree and the compound mode. | 9014 | // reference's tree, plus the pair tree and the compound mode. |
| 8908 | for (ref1, g, cstack) in compound.iter().filter(|_| b64_compound()) { | 9015 | for (ref1, g, cstack) in compound.iter().filter(|_| b64_compound()) { |
| 8909 | let (ref1, g) = (*ref1, *g); | 9016 | let (ref1, g) = (*ref1, *g); |
| 8910 | - let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&ref1); | 9017 | + // The whole `comp_mode` + reference-pair tree at the writer's own |
| 8911 | - let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) | 9018 | + // contexts, for THIS pair (lane-ctx). |
| 8912 | - + if uni { | 9019 | + let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack); |
| 8913 | - symbol_bits(&cdf::COMP_REF_TYPE[0], 1) | ||
| 8914 | - + symbol_bits(&cdf::COMP_REF[0][0], 0) | ||
| 8915 | - + symbol_bits(&cdf::COMP_REF[0][1], 0) | ||
| 8916 | - + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) | ||
| 8917 | - } else { | ||
| 8918 | - symbol_bits(&cdf::COMP_REF_TYPE[0], 0) | ||
| 8919 | - + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) | ||
| 8920 | - + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) | ||
| 8921 | - + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) | ||
| 8922 | - }; | ||
| 8923 | let mode_ctx = | 9020 | let mode_ctx = |
| 8924 | cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; | 9021 | cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; |
| 8925 | let mode_bits_of = | 9022 | let mode_bits_of = |
| @@ -9057,7 +9154,7 @@ fn search_skip_64( | |||
| 9057 | sets[plane], | 9154 | sets[plane], |
| 9058 | ) | 9155 | ) |
| 9059 | }); | 9156 | }); |
| 9060 | - let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); | 9157 | + let unskip = skip_bits_at(stack, false) - skip_bits_at(stack, true); |
| 9061 | let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); | 9158 | let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); |
| 9062 | // lane-b64b: the root's own var-tx decision -- one TX_64X64 or the | 9159 | // lane-b64b: the root's own var-tx decision -- one TX_64X64 or the |
| 9063 | // four TX_32X32 units of `txfm_split = 1`, taken by the same | 9160 | // four TX_32X32 units of `txfm_split = 1`, taken by the same |
| @@ -9171,8 +9268,8 @@ fn search_inter_block( | |||
| 9171 | 9268 | ||
| 9172 | // skip / intra_inter symbol costs at a fixed context -- see this | 9269 | // skip / intra_inter symbol costs at a fixed context -- see this |
| 9173 | // function's doc comment. | 9270 | // function's doc comment. |
| 9174 | - let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); | 9271 | + let skip_bits = |skip: bool| skip_bits_at(stack, skip); |
| 9175 | - let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); | 9272 | + let intra_inter_bits = |inter: bool| intra_inter_bits_at(stack, inter); |
| 9176 | 9273 | ||
| 9177 | struct Candidate { | 9274 | struct Candidate { |
| 9178 | cost: f64, | 9275 | cost: f64, |
| @@ -9542,23 +9639,9 @@ fn search_inter_block( | |||
| 9542 | let half_new = true; | 9639 | let half_new = true; |
| 9543 | for (ref1, g, cstack) in compound { | 9640 | for (ref1, g, cstack) in compound { |
| 9544 | let (ref1, g) = (*ref1, *g); | 9641 | let (ref1, g) = (*ref1, *g); |
| 9545 | - let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME) | 9642 | + // The whole `comp_mode` + reference-pair tree at the writer's own |
| 9546 | - .contains(&ref1); | 9643 | + // contexts, for THIS pair (lane-ctx). |
| 9547 | - let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) | 9644 | + let pair_bits = compound_ref_bits((crate::mvstack::LAST_FRAME, ref1), stack); |
| 9548 | - + if uni { | ||
| 9549 | - // LAST + a backward reference: `comp_ref_type` = bidirectional, | ||
| 9550 | - // `comp_ref` LAST, `comp_bwdref` ALTREF. | ||
| 9551 | - symbol_bits(&cdf::COMP_REF_TYPE[0], 1) | ||
| 9552 | - + symbol_bits(&cdf::COMP_REF[0][0], 0) | ||
| 9553 | - + symbol_bits(&cdf::COMP_REF[0][1], 0) | ||
| 9554 | - + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) | ||
| 9555 | - } else { | ||
| 9556 | - // LAST + GOLDEN: unidirectional, `uni_comp_ref` p0/p1/p2. | ||
| 9557 | - symbol_bits(&cdf::COMP_REF_TYPE[0], 0) | ||
| 9558 | - + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) | ||
| 9559 | - + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) | ||
| 9560 | - + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) | ||
| 9561 | - }; | ||
| 9562 | let mode_ctx = | 9645 | let mode_ctx = |
| 9563 | cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; | 9646 | cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; |
| 9564 | let mode_bits_of = | 9647 | let mode_bits_of = |
| @@ -11393,7 +11476,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11393 | fctx, | 11476 | fctx, |
| 11394 | ); | 11477 | ); |
| 11395 | cost8 += cost; | 11478 | cost8 += cost; |
| 11396 | - record_mi(&mut grid, mr, mc, 2, leaf8.inter); | 11479 | + record_mi(&mut grid, mr, mc, 2, leaf8.inter, leaf8.skip); |
| 11397 | eight.push(leaf8); | 11480 | eight.push(leaf8); |
| 11398 | } | 11481 | } |
| 11399 | (eight, cost8) | 11482 | (eight, cost8) |
| @@ -11415,7 +11498,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11415 | } | 11498 | } |
| 11416 | partition_hit(2); | 11499 | partition_hit(2); |
| 11417 | cost += leaf_cost; | 11500 | cost += leaf_cost; |
| 11418 | - record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter); | 11501 | + record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter, leaf.skip); |
| 11419 | leaves.push(leaf); | 11502 | leaves.push(leaf); |
| 11420 | } | 11503 | } |
| 11421 | (leaves, cost) | 11504 | (leaves, cost) |
| @@ -11434,7 +11517,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11434 | } | 11517 | } |
| 11435 | partition_hit(0); | 11518 | partition_hit(0); |
| 11436 | sb_cost += cost_whole; | 11519 | sb_cost += cost_whole; |
| 11437 | - record_mi(&mut grid, mi_row, mi_col, 8, block.inter); | 11520 | + record_mi(&mut grid, mi_row, mi_col, 8, block.inter, block.skip); |
| 11438 | blocks.push((r32 * cols + c32, Quadrant::Whole(block))); | 11521 | blocks.push((r32 * cols + c32, Quadrant::Whole(block))); |
| 11439 | } else { | 11522 | } else { |
| 11440 | // The sub-positions actually inside the true frame, same | 11523 | // The sub-positions actually inside the true frame, same |
| @@ -11501,7 +11584,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11501 | // to (0,0), which is the encoder-grid-drift class | 11584 | // to (0,0), which is the encoder-grid-drift class |
| 11502 | // -- correct only for as long as a leaf can never | 11585 | // -- correct only for as long as a leaf can never |
| 11503 | // be compound. | 11586 | // be compound. |
| 11504 | - record_mi(&mut grid, mi_row, mi_col, 4, block.inter); | 11587 | + record_mi(&mut grid, mi_row, mi_col, 4, block.inter, block.skip); |
| 11505 | leaves.push(block); | 11588 | leaves.push(block); |
| 11506 | } else { | 11589 | } else { |
| 11507 | let (x_sub, y_sub) = (sc * SUB, sr * SUB); | 11590 | let (x_sub, y_sub) = (sc * SUB, sr * SUB); |
| @@ -11540,7 +11623,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11540 | ); | 11623 | ); |
| 11541 | // Same publication point as the 16x16 | 11624 | // Same publication point as the 16x16 |
| 11542 | // straddling leaf above (`record_mi`). | 11625 | // straddling leaf above (`record_mi`). |
| 11543 | - record_mi(&mut grid, mi_row, mi_col, 2, leaf.inter); | 11626 | + record_mi(&mut grid, mi_row, mi_col, 2, leaf.inter, leaf.skip); |
| 11544 | eight.push(leaf); | 11627 | eight.push(leaf); |
| 11545 | } | 11628 | } |
| 11546 | leaves.push(BlockCoeffs { | 11629 | leaves.push(BlockCoeffs { |
| @@ -11571,7 +11654,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11571 | // Overwrites every 16x16 mi cell the quadrant searches | 11654 | // Overwrites every 16x16 mi cell the quadrant searches |
| 11572 | // published under this superblock, so the next block's stack | 11655 | // published under this superblock, so the next block's stack |
| 11573 | // is built off the block that was really coded. | 11656 | // is built off the block that was really coded. |
| 11574 | - record_mi(&mut grid, sb_r * 16, sb_c * 16, 16, block.inter); | 11657 | + record_mi(&mut grid, sb_r * 16, sb_c * 16, 16, block.inter, block.skip); |
| 11575 | blocks.push(((sb_r * 2) * cols + sb_c * 2, Quadrant::Whole64(block))); | 11658 | blocks.push(((sb_r * 2) * cols + sb_c * 2, Quadrant::Whole64(block))); |
| 11576 | for q in 1..4 { | 11659 | for q in 1..4 { |
| 11577 | let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); | 11660 | let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); |
| @@ -12061,6 +12144,196 @@ pub(crate) fn encode_sequence_with_ctx( | |||
| 12061 | #[cfg(test)] | 12144 | #[cfg(test)] |
| 12062 | mod tests { | 12145 | mod tests { |
| 12063 | 12146 | ||
| 12147 | + /// An [`crate::mvstack::MvStack`] with every context at zero and no | ||
| 12148 | + /// neighbours, for the pricer tests below to fill the one band each of | ||
| 12149 | + /// them is about. | ||
| 12150 | + fn bare_stack() -> crate::mvstack::MvStack { | ||
| 12151 | + crate::mvstack::MvStack { | ||
| 12152 | + entries: Default::default(), | ||
| 12153 | + nearest_mv: (0, 0), | ||
| 12154 | + near_mv: (0, 0), | ||
| 12155 | + pred_mv: (0, 0), | ||
| 12156 | + new_mv_ctx: 0, | ||
| 12157 | + ref_mv_ctx: 0, | ||
| 12158 | + zero_mv_ctx: 0, | ||
| 12159 | + drl_ctx: Default::default(), | ||
| 12160 | + above_refs: (None, None), | ||
| 12161 | + left_refs: (None, None), | ||
| 12162 | + above_nbr: None, | ||
| 12163 | + left_nbr: None, | ||
| 12164 | + above_skip: false, | ||
| 12165 | + left_skip: false, | ||
| 12166 | + } | ||
| 12167 | + } | ||
| 12168 | + | ||
| 12169 | + /// [`super::compound_ref_bits`] pays exactly the symbols tile.rs' | ||
| 12170 | + /// `write_compound_ref_frames` codes for a pair, each at that symbol's | ||
| 12171 | + /// own neighbour-derived context. Red before lane-ctx, where the pricer | ||
| 12172 | + /// spent four constants at CDF row 0 that assumed the pair was | ||
| 12173 | + /// LAST+GOLDEN or LAST+ALTREF -- so a LAST+BWDREF pair was priced as | ||
| 12174 | + /// LAST+ALTREF with its `comp_bwdref` p6 symbol missing entirely. | ||
| 12175 | + #[test] | ||
| 12176 | + fn the_compound_ref_pricer_pays_the_writers_own_symbol_sequence() { | ||
| 12177 | + use crate::cdf::{COMP_BWDREF, COMP_MODE, COMP_REF, COMP_REF_TYPE, UNI_COMP_REF}; | ||
| 12178 | + use crate::encode::symbol_bits as b; | ||
| 12179 | + use crate::mvstack::{ | ||
| 12180 | + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, | ||
| 12181 | + LAST_FRAME, NeighbourRef, comp_reference_type_ctx, reference_mode_ctx, | ||
| 12182 | + single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, | ||
| 12183 | + single_ref_p5_ctx, single_ref_p6_ctx, uni_comp_ref_p1_ctx, | ||
| 12184 | + }; | ||
| 12185 | + // (pair, unidirectional?, the tree's symbols as (table, p index, | ||
| 12186 | + // context selector, value)) -- transcribed from the writer. | ||
| 12187 | + // selector: 1..=6 are `single_ref_p*_ctx`, 7 is `uni_comp_ref_p1_ctx`. | ||
| 12188 | + type Sym = (char, usize, usize, usize); | ||
| 12189 | + let cases: [((i8, i8), bool, &[Sym]); 7] = [ | ||
| 12190 | + ((LAST_FRAME, LAST2_FRAME), true, &[('u', 0, 1, 0), ('u', 1, 7, 0)]), | ||
| 12191 | + ( | ||
| 12192 | + (LAST_FRAME, LAST3_FRAME), | ||
| 12193 | + true, | ||
| 12194 | + &[('u', 0, 1, 0), ('u', 1, 7, 1), ('u', 2, 5, 0)], | ||
| 12195 | + ), | ||
| 12196 | + ( | ||
| 12197 | + (LAST_FRAME, GOLDEN_FRAME), | ||
| 12198 | + true, | ||
| 12199 | + &[('u', 0, 1, 0), ('u', 1, 7, 1), ('u', 2, 5, 1)], | ||
| 12200 | + ), | ||
| 12201 | + ((BWDREF_FRAME, ALTREF_FRAME), true, &[('u', 0, 1, 1)]), | ||
| 12202 | + ( | ||
| 12203 | + (LAST_FRAME, ALTREF_FRAME), | ||
| 12204 | + false, | ||
| 12205 | + &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 1)], | ||
| 12206 | + ), | ||
| 12207 | + ( | ||
| 12208 | + (LAST_FRAME, BWDREF_FRAME), | ||
| 12209 | + false, | ||
| 12210 | + &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 0), ('b', 1, 6, 0)], | ||
| 12211 | + ), | ||
| 12212 | + ( | ||
| 12213 | + (LAST_FRAME, ALTREF2_FRAME), | ||
| 12214 | + false, | ||
| 12215 | + &[('c', 0, 3, 0), ('c', 1, 4, 0), ('b', 0, 2, 0), ('b', 1, 6, 1)], | ||
| 12216 | + ), | ||
| 12217 | + ]; | ||
| 12218 | + let nbr = |ref0, ref1: Option<i8>| { | ||
| 12219 | + Some(NeighbourRef { | ||
| 12220 | + is_inter: true, | ||
| 12221 | + ref0, | ||
| 12222 | + ref1, | ||
| 12223 | + uni: ref1.is_some_and(|r1| crate::mvstack::is_uni_comp_ref(ref0, r1)), | ||
| 12224 | + }) | ||
| 12225 | + }; | ||
| 12226 | + // No neighbours (every context zero), then a mixed pair that puts the | ||
| 12227 | + // p* rows, `comp_reference_type_ctx` and `reference_mode_ctx` off 0. | ||
| 12228 | + for (above_nbr, left_nbr, above_refs, left_refs) in [ | ||
| 12229 | + (None, None, (None, None), (None, None)), | ||
| 12230 | + ( | ||
| 12231 | + nbr(LAST_FRAME, None), | ||
| 12232 | + nbr(ALTREF_FRAME, Some(GOLDEN_FRAME)), | ||
| 12233 | + (Some(LAST_FRAME), None), | ||
| 12234 | + (Some(ALTREF_FRAME), Some(GOLDEN_FRAME)), | ||
| 12235 | + ), | ||
| 12236 | + ] { | ||
| 12237 | + let stack = crate::mvstack::MvStack { | ||
| 12238 | + above_nbr, | ||
| 12239 | + left_nbr, | ||
| 12240 | + above_refs, | ||
| 12241 | + left_refs, | ||
| 12242 | + ..bare_stack() | ||
| 12243 | + }; | ||
| 12244 | + let (a, a1) = above_refs; | ||
| 12245 | + let (l, l1) = left_refs; | ||
| 12246 | + let ctx_of = |sel: usize| match sel { | ||
| 12247 | + 1 => single_ref_p1_ctx(a, a1, l, l1), | ||
| 12248 | + 2 => single_ref_p2_ctx(a, a1, l, l1), | ||
| 12249 | + 3 => single_ref_p3_ctx(a, a1, l, l1), | ||
| 12250 | + 4 => single_ref_p4_ctx(a, a1, l, l1), | ||
| 12251 | + 5 => single_ref_p5_ctx(a, a1, l, l1), | ||
| 12252 | + 6 => single_ref_p6_ctx(a, a1, l, l1), | ||
| 12253 | + _ => uni_comp_ref_p1_ctx(a, a1, l, l1), | ||
| 12254 | + }; | ||
| 12255 | + for (pair, unidir, symbols) in cases { | ||
| 12256 | + let mut want = b(&COMP_MODE[reference_mode_ctx(above_nbr, left_nbr)], 1) | ||
| 12257 | + + b( | ||
| 12258 | + &COMP_REF_TYPE[comp_reference_type_ctx(above_nbr, left_nbr)], | ||
| 12259 | + usize::from(!unidir), | ||
| 12260 | + ); | ||
| 12261 | + for &(table, p, sel, v) in symbols { | ||
| 12262 | + let row = ctx_of(sel); | ||
| 12263 | + want += match table { | ||
| 12264 | + 'u' => b(&UNI_COMP_REF[row][p], v), | ||
| 12265 | + 'c' => b(&COMP_REF[row][p], v), | ||
| 12266 | + _ => b(&COMP_BWDREF[row][p], v), | ||
| 12267 | + }; | ||
| 12268 | + } | ||
| 12269 | + let got = super::compound_ref_bits(pair, &stack); | ||
| 12270 | + assert!( | ||
| 12271 | + (got - want).abs() < 1e-9, | ||
| 12272 | + "pair {pair:?} above {above_refs:?} left {left_refs:?}: \ | ||
| 12273 | + priced {got} bits, writer codes {want}" | ||
| 12274 | + ); | ||
| 12275 | + } | ||
| 12276 | + } | ||
| 12277 | + } | ||
| 12278 | + | ||
| 12279 | + /// The `skip` and `is_inter` symbols are priced at the SAME context | ||
| 12280 | + /// tile.rs codes them with -- `above_skip + left_skip` for the first and | ||
| 12281 | + /// `intra_inter_ctx` for the second -- not at row 0. Red before lane-ctx | ||
| 12282 | + /// on every neighbour configuration whose context is not zero (class | ||
| 12283 | + /// pricer-context-zero). | ||
| 12284 | + #[test] | ||
| 12285 | + fn the_skip_and_intra_inter_pricers_use_the_writers_own_contexts() { | ||
| 12286 | + use crate::cdf::{INTRA_INTER, SKIP}; | ||
| 12287 | + use crate::encode::symbol_bits as b; | ||
| 12288 | + use crate::mvstack::NeighbourRef; | ||
| 12289 | + let nbr = |is_inter| NeighbourRef { | ||
| 12290 | + is_inter, | ||
| 12291 | + ref0: if is_inter { crate::mvstack::LAST_FRAME } else { 0 }, | ||
| 12292 | + ref1: None, | ||
| 12293 | + uni: false, | ||
| 12294 | + }; | ||
| 12295 | + for (above, left, above_skip, left_skip) in [ | ||
| 12296 | + (None, None, false, false), | ||
| 12297 | + (Some(nbr(true)), Some(nbr(true)), true, false), | ||
| 12298 | + (Some(nbr(false)), Some(nbr(false)), true, true), | ||
| 12299 | + (Some(nbr(false)), None, false, true), | ||
| 12300 | + (None, Some(nbr(true)), true, true), | ||
| 12301 | + ] { | ||
| 12302 | + let stack = crate::mvstack::MvStack { | ||
| 12303 | + above_nbr: above, | ||
| 12304 | + left_nbr: left, | ||
| 12305 | + above_skip, | ||
| 12306 | + left_skip, | ||
| 12307 | + ..bare_stack() | ||
| 12308 | + }; | ||
| 12309 | + let skip_ctx = usize::from(above_skip) + usize::from(left_skip); | ||
| 12310 | + let ii_ctx = crate::tile::intra_inter_ctx( | ||
| 12311 | + above.is_some(), | ||
| 12312 | + left.is_some(), | ||
| 12313 | + above.is_some_and(|n| n.is_inter), | ||
| 12314 | + left.is_some_and(|n| n.is_inter), | ||
| 12315 | + ); | ||
| 12316 | + for v in [false, true] { | ||
| 12317 | + let (got, want) = ( | ||
| 12318 | + super::skip_bits_at(&stack, v), | ||
| 12319 | + b(&SKIP[skip_ctx], usize::from(v)), | ||
| 12320 | + ); | ||
| 12321 | + assert!( | ||
| 12322 | + (got - want).abs() < 1e-9, | ||
| 12323 | + "skip {v} at ctx {skip_ctx}: priced {got}, writer codes {want}" | ||
| 12324 | + ); | ||
| 12325 | + let (got, want) = ( | ||
| 12326 | + super::intra_inter_bits_at(&stack, v), | ||
| 12327 | + b(&INTRA_INTER[ii_ctx], usize::from(v)), | ||
| 12328 | + ); | ||
| 12329 | + assert!( | ||
| 12330 | + (got - want).abs() < 1e-9, | ||
| 12331 | + "is_inter {v} at ctx {ii_ctx}: priced {got}, writer codes {want}" | ||
| 12332 | + ); | ||
| 12333 | + } | ||
| 12334 | + } | ||
| 12335 | + } | ||
| 12336 | + | ||
| 12064 | /// [`super::single_ref_bits`] pays exactly what tile.rs' | 12337 | /// [`super::single_ref_bits`] pays exactly what tile.rs' |
| 12065 | /// `write_single_ref` codes: the p1/p2/p6 chain for a BACKWARD | 12338 | /// `write_single_ref` codes: the p1/p2/p6 chain for a BACKWARD |
| 12066 | /// reference and the p1/p3/p4/p5 chain for a forward one, each symbol at | 12339 | /// reference and the p1/p3/p4/p5 chain for a forward one, each symbol at |
| @@ -12077,16 +12350,9 @@ mod tests { | |||
| 12077 | single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, | 12350 | single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, |
| 12078 | }; | 12351 | }; |
| 12079 | let stack_with = |above_refs, left_refs| MvStack { | 12352 | let stack_with = |above_refs, left_refs| MvStack { |
| 12080 | - entries: Default::default(), | ||
| 12081 | - nearest_mv: (0, 0), | ||
| 12082 | - near_mv: (0, 0), | ||
| 12083 | - pred_mv: (0, 0), | ||
| 12084 | - new_mv_ctx: 0, | ||
| 12085 | - ref_mv_ctx: 0, | ||
| 12086 | - zero_mv_ctx: 0, | ||
| 12087 | - drl_ctx: Default::default(), | ||
| 12088 | above_refs, | 12353 | above_refs, |
| 12089 | left_refs, | 12354 | left_refs, |
| 12355 | + ..bare_stack() | ||
| 12090 | }; | 12356 | }; |
| 12091 | // (reference, the symbols `write_single_ref` emits as (p index, value)) | 12357 | // (reference, the symbols `write_single_ref` emits as (p index, value)) |
| 12092 | let cases: [(i8, &[(usize, usize)]); 7] = [ | 12358 | let cases: [(i8, &[(usize, usize)]); 7] = [ |
| @@ -15805,8 +16071,13 @@ mod tests { | |||
| 15805 | // reference through the tree and the neighbour contexts the writer | 16071 | // reference through the tree and the neighbour contexts the writer |
| 15806 | // really codes it with, so every inter picture's reference decisions | 16072 | // really codes it with, so every inter picture's reference decisions |
| 15807 | // move -- 8562 -> 8535 at q=150 and 33357 -> 33221 at q=60. | 16073 | // move -- 8562 -> 8535 at q=150 and 33357 -> 33221 at q=60. |
| 16074 | + // Re-taken on lane-ctx: the RD pricer reads the writer's own contexts | ||
| 16075 | + // for `skip`/`is_inter` and for the whole compound reference tree, so | ||
| 16076 | + // every inter block's cost -- and with it the skip and reference | ||
| 16077 | + // decisions -- moved: 8535 -> 8311 bytes at q=150 and 33221 -> 33087 | ||
| 16078 | + // at q=60. | ||
| 15808 | let pins: [(u8, usize, u64); 2] = | 16079 | let pins: [(u8, usize, u64); 2] = |
| 15809 | - [(150, 8535, 0x7b7f_4eeb_1080_7b45), (60, 33221, 0x720b_821e_5dd2_90e4)]; | 16080 | + [(150, 8311, 0xfb6d_75a4_5d13_3833), (60, 33087, 0x4d11_a03c_bdc6_e2d5)]; |
| 15810 | let coded: Vec<(u8, usize, u64)> = pins | 16081 | let coded: Vec<(u8, usize, u64)> = pins |
| 15811 | .iter() | 16082 | .iter() |
| 15812 | .map(|&(q, _, _)| { | 16083 | .map(|&(q, _, _)| { |
crates/ec-av1/src/mvstack.rsmodified+86 -2
| @@ -251,6 +251,14 @@ pub struct MiGrid { | |||
| 251 | cols: usize, | 251 | cols: usize, |
| 252 | rows: usize, | 252 | rows: usize, |
| 253 | cells: Vec<Option<MiInfo>>, | 253 | cells: Vec<Option<MiInfo>>, |
| 254 | + /// One `skip` flag per cell, beside `cells` rather than inside `MiInfo` | ||
| 255 | + /// -- the mv-stack scan walks `cells` for every inter block, so the CELL | ||
| 256 | + /// must stay 16 bytes (`mi_info_cell_stays_compact`), and nothing on the | ||
| 257 | + /// scan path reads this. Only the ENCODER's own grid fills it | ||
| 258 | + /// (`encode::record_mi`), for the RD pricer's `skip` context; the tile | ||
| 259 | + /// writer and the decoder read their `skip` neighbours from their own | ||
| 260 | + /// bands and leave this `false` (lane-ctx). | ||
| 261 | + skips: Vec<bool>, | ||
| 254 | /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV | 262 | /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV |
| 255 | /// candidate scan never reaches across a tile boundary, mirroring | 263 | /// candidate scan never reaches across a tile boundary, mirroring |
| 256 | /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction). | 264 | /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction). |
| @@ -279,6 +287,7 @@ impl MiGrid { | |||
| 279 | cols, | 287 | cols, |
| 280 | rows, | 288 | rows, |
| 281 | cells: vec![None; cols * rows], | 289 | cells: vec![None; cols * rows], |
| 290 | + skips: vec![false; cols * rows], | ||
| 282 | tile_row0: 0, | 291 | tile_row0: 0, |
| 283 | tile_col0: 0, | 292 | tile_col0: 0, |
| 284 | tile_row1: rows, | 293 | tile_row1: rows, |
| @@ -327,6 +336,25 @@ impl MiGrid { | |||
| 327 | } | 336 | } |
| 328 | } | 337 | } |
| 329 | 338 | ||
| 339 | + /// Records the `skip` flag of the unit at `(row, col)` -- see `skips`. | ||
| 340 | + pub fn set_skip(&mut self, row: usize, col: usize, skip: bool) { | ||
| 341 | + if row < self.rows && col < self.cols { | ||
| 342 | + self.skips[row * self.cols + col] = skip; | ||
| 343 | + } | ||
| 344 | + } | ||
| 345 | + | ||
| 346 | + /// The `skip` flag of the unit at `(row, col)`, `false` outside this | ||
| 347 | + /// tile's own window (the same clamp [`Self::get`] applies). | ||
| 348 | + pub fn skip_at(&self, row: usize, col: usize) -> bool { | ||
| 349 | + row < self.rows | ||
| 350 | + && col < self.cols | ||
| 351 | + && row >= self.tile_row0 | ||
| 352 | + && row < self.tile_row1 | ||
| 353 | + && col >= self.tile_col0 | ||
| 354 | + && col < self.tile_col1 | ||
| 355 | + && self.skips[row * self.cols + col] | ||
| 356 | + } | ||
| 357 | + | ||
| 330 | /// Records one block's whole `bh4` x `bw4` footprint at once. Every | 358 | /// Records one block's whole `bh4` x `bw4` footprint at once. Every |
| 331 | /// caller published a block cell-by-cell through [`Self::set`]; the rows | 359 | /// caller published a block cell-by-cell through [`Self::set`]; the rows |
| 332 | /// are contiguous, so this fills each of them with one `slice::fill` of | 360 | /// are contiguous, so this fills each of them with one `slice::fill` of |
| @@ -500,6 +528,20 @@ pub struct MvStack { | |||
| 500 | pub above_refs: (Option<i8>, Option<i8>), | 528 | pub above_refs: (Option<i8>, Option<i8>), |
| 501 | /// The immediate LEFT neighbour's `(ref0, ref1)` -- see `above_refs`. | 529 | /// The immediate LEFT neighbour's `(ref0, ref1)` -- see `above_refs`. |
| 502 | pub left_refs: (Option<i8>, Option<i8>), | 530 | pub left_refs: (Option<i8>, Option<i8>), |
| 531 | + /// The immediate above/left neighbours exactly as tile.rs' `above_nbr`/ | ||
| 532 | + /// `left_nbr` build them for `reference_mode_ctx`/ | ||
| 533 | + /// `comp_reference_type_ctx`, `None` outside the tile (which is what the | ||
| 534 | + /// writer's `has_above`/`has_left` gate says). Carried so the RD pricer | ||
| 535 | + /// prices `comp_mode` and the compound reference tree at the writer's own | ||
| 536 | + /// contexts instead of context 0 (lane-ctx). | ||
| 537 | + pub above_nbr: Option<NeighbourRef>, | ||
| 538 | + /// The immediate LEFT neighbour -- see `above_nbr`. | ||
| 539 | + pub left_nbr: Option<NeighbourRef>, | ||
| 540 | + /// The two `skip` flags the writer's `skip_ctx` sums (its | ||
| 541 | + /// `above_skip[mi_c]` + `left_skip[mi_r]`), `false` outside the tile. | ||
| 542 | + pub above_skip: bool, | ||
| 543 | + /// The immediate LEFT neighbour's `skip` -- see `above_skip`. | ||
| 544 | + pub left_skip: bool, | ||
| 503 | } | 545 | } |
| 504 | 546 | ||
| 505 | /// Spec 7.10.2.8's threshold a candidate's weight is compared against to | 547 | /// 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( | |||
| 1673 | let above_refs = refs_of(mi_row.checked_sub(1).and_then(|r| grid.get(r, mi_col))); | 1715 | let above_refs = refs_of(mi_row.checked_sub(1).and_then(|r| grid.get(r, mi_col))); |
| 1674 | let left_refs = refs_of(mi_col.checked_sub(1).and_then(|c| grid.get(mi_row, c))); | 1716 | let left_refs = refs_of(mi_col.checked_sub(1).and_then(|c| grid.get(mi_row, c))); |
| 1675 | 1717 | ||
| 1718 | + // The same two cells again, in the shape tile.rs `above_nbr`/`left_nbr` | ||
| 1719 | + // hand `reference_mode_ctx`/`comp_reference_type_ctx`, plus each side's | ||
| 1720 | + // `skip` -- what the pricer's own `comp_mode`/compound-tree/`skip`/ | ||
| 1721 | + // `is_inter` contexts read (lane-ctx). | ||
| 1722 | + let above_cell = mi_row.checked_sub(1).and_then(|r| grid.get(r, mi_col)); | ||
| 1723 | + let left_cell = mi_col.checked_sub(1).and_then(|c| grid.get(mi_row, c)); | ||
| 1724 | + let nbr_of = |cell: Option<MiInfo>| { | ||
| 1725 | + cell.map(|mi| { | ||
| 1726 | + let ref0 = if mi.is_inter { mi.ref_frame } else { 0 }; | ||
| 1727 | + let ref1 = (mi.is_inter && mi.ref_frame1 > 0).then_some(mi.ref_frame1); | ||
| 1728 | + NeighbourRef { | ||
| 1729 | + is_inter: mi.is_inter, | ||
| 1730 | + ref0, | ||
| 1731 | + ref1, | ||
| 1732 | + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), | ||
| 1733 | + } | ||
| 1734 | + }) | ||
| 1735 | + }; | ||
| 1736 | + let above_nbr = nbr_of(above_cell); | ||
| 1737 | + let left_nbr = nbr_of(left_cell); | ||
| 1738 | + let above_skip = mi_row | ||
| 1739 | + .checked_sub(1) | ||
| 1740 | + .is_some_and(|r| grid.skip_at(r, mi_col)); | ||
| 1741 | + let left_skip = mi_col | ||
| 1742 | + .checked_sub(1) | ||
| 1743 | + .is_some_and(|c| grid.skip_at(mi_row, c)); | ||
| 1744 | + | ||
| 1676 | MvStack { | 1745 | MvStack { |
| 1677 | entries: candidates, | 1746 | entries: candidates, |
| 1678 | nearest_mv, | 1747 | nearest_mv, |
| @@ -1684,6 +1753,10 @@ pub fn find_mv_stack_with_sign_bias( | |||
| 1684 | drl_ctx, | 1753 | drl_ctx, |
| 1685 | above_refs, | 1754 | above_refs, |
| 1686 | left_refs, | 1755 | left_refs, |
| 1756 | + above_nbr, | ||
| 1757 | + left_nbr, | ||
| 1758 | + above_skip, | ||
| 1759 | + left_skip, | ||
| 1687 | } | 1760 | } |
| 1688 | } | 1761 | } |
| 1689 | 1762 | ||
| @@ -2636,14 +2709,25 @@ pub(crate) fn uni_comp_ref_p1_ctx( | |||
| 2636 | /// `ref1`'s meaning only when compound: whether the pair libaom's | 2709 | /// `ref1`'s meaning only when compound: whether the pair libaom's |
| 2637 | /// `has_uni_comp_refs` would call unidirectional (both refs on the same | 2710 | /// `has_uni_comp_refs` would call unidirectional (both refs on the same |
| 2638 | /// side of the current frame). | 2711 | /// side of the current frame). |
| 2639 | -#[derive(Clone, Copy)] | 2712 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 2640 | -pub(crate) struct NeighbourRef { | 2713 | +pub struct NeighbourRef { |
| 2714 | + /// Whether the neighbour was coded inter. | ||
| 2641 | pub is_inter: bool, | 2715 | pub is_inter: bool, |
| 2716 | + /// Its `RefFrame[0]`, `0` (`INTRA_FRAME`) when it is intra. | ||
| 2642 | pub ref0: i8, | 2717 | pub ref0: i8, |
| 2718 | + /// Its `RefFrame[1]`, `None` unless it was coded compound. | ||
| 2643 | pub ref1: Option<i8>, | 2719 | pub ref1: Option<i8>, |
| 2720 | + /// Whether that compound pair is unidirectional. | ||
| 2644 | pub uni: bool, | 2721 | pub uni: bool, |
| 2645 | } | 2722 | } |
| 2646 | 2723 | ||
| 2724 | +/// libaom `has_uni_comp_refs`: a compound pair is unidirectional when both | ||
| 2725 | +/// of its references sit on the same side of the current frame. One copy, | ||
| 2726 | +/// shared by the tile writer and the RD pricer (lane-ctx). | ||
| 2727 | +pub(crate) fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { | ||
| 2728 | + is_backward(ref0) == is_backward(ref1) | ||
| 2729 | +} | ||
| 2730 | + | ||
| 2647 | fn is_backward(r: i8) -> bool { | 2731 | fn is_backward(r: i8) -> bool { |
| 2648 | (BWDREF_FRAME..=ALTREF_FRAME).contains(&r) | 2732 | (BWDREF_FRAME..=ALTREF_FRAME).contains(&r) |
| 2649 | } | 2733 | } |
crates/ec-av1/src/tile.rsmodified+7 -6
| @@ -798,11 +798,7 @@ fn record_block_compound(neighbours: &mut Neighbours, at_mi: (usize, usize), sid | |||
| 798 | 798 | ||
| 799 | /// decode.rs' own `is_uni_comp_ref`: a pair is `UNIDIR_COMP_REFERENCE` | 799 | /// decode.rs' own `is_uni_comp_ref`: a pair is `UNIDIR_COMP_REFERENCE` |
| 800 | /// exactly when both references sit on the same temporal side. | 800 | /// exactly when both references sit on the same temporal side. |
| 801 | -fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { | 801 | +use crate::mvstack::is_uni_comp_ref; |
| 802 | - let backward = | ||
| 803 | - |r: i8| (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&r); | ||
| 804 | - backward(ref0) == backward(ref1) | ||
| 805 | -} | ||
| 806 | 802 | ||
| 807 | thread_local! { | 803 | thread_local! { |
| 808 | /// `(order_hint_bits, this frame's order hint, each reference's order | 804 | /// `(order_hint_bits, this frame's order hint, each reference's order |
| @@ -5723,7 +5719,12 @@ fn dc_sign_ctx(vote: i32) -> usize { | |||
| 5723 | /// `pred_common.c`): both neighbours' intra/inter state when both are | 5719 | /// `pred_common.c`): both neighbours' intra/inter state when both are |
| 5724 | /// available, one neighbour's when only one is, and zero at a tile's own | 5720 | /// available, one neighbour's when only one is, and zero at a tile's own |
| 5725 | /// top-left corner. | 5721 | /// top-left corner. |
| 5726 | -fn intra_inter_ctx(has_above: bool, has_left: bool, above_inter: bool, left_inter: bool) -> usize { | 5722 | +pub(crate) fn intra_inter_ctx( |
| 5723 | + has_above: bool, | ||
| 5724 | + has_left: bool, | ||
| 5725 | + above_inter: bool, | ||
| 5726 | + left_inter: bool, | ||
| 5727 | +) -> usize { | ||
| 5727 | match (has_above, has_left) { | 5728 | match (has_above, has_left) { |
| 5728 | (true, true) => { | 5729 | (true, true) => { |
| 5729 | let (above_intra, left_intra) = (!above_inter, !left_inter); | 5730 | let (above_intra, left_intra) = (!above_inter, !left_inter); |
lanes/ctx.report.mdadded+92 -0
| @@ -0,0 +1,92 @@ | |||
| 1 | +# lane-ctx — the RD pricer's remaining context-zero sites | ||
| 2 | + | ||
| 3 | +Class: `pricer-context-zero`. lane-pricer fixed `single_ref_bits`; the sweep it | ||
| 4 | +left named the rest. This lane took them. | ||
| 5 | + | ||
| 6 | +## 1. Enumeration — every `symbol_bits(&cdf::` site in `crates/ec-av1/src/encode.rs` | ||
| 7 | + | ||
| 8 | +| pricer site (encode.rs) | writer (tile.rs) | how the writer picks the context | verdict | | ||
| 9 | +|---|---|---|---| | ||
| 10 | +| `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`) | | ||
| 11 | +| `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`) | | ||
| 12 | +| `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) | | ||
| 13 | +| `COMP_MODE[0]` (3 sites) | `write_comp_mode` (1154) | `reference_mode_ctx(above_nbr, left_nbr)` | **arm C — FIXED** | | ||
| 14 | +| `COMP_REF_TYPE[0]` (3) | `write_compound_ref_frames` (856) | `comp_reference_type_ctx(above, left)` | **arm C — FIXED** | | ||
| 15 | +| `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) | | ||
| 16 | +| `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 | | ||
| 17 | +| `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 | | ||
| 18 | +| `SINGLE_REF[p*_ctx]` | `write_single_ref` | its own `single_ref_p*_ctx` | already fixed (lane-pricer) | | ||
| 19 | +| `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 | | ||
| 20 | +| `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) | | ||
| 21 | + | ||
| 22 | +Open, found while enumerating: the single-reference candidates pay **no** | ||
| 23 | +`comp_mode = 0` term at all while the compound ones pay `comp_mode = 1`, so | ||
| 24 | +compound is still overcharged by `comp_mode(0)` relative to single. Left alone | ||
| 25 | +here so arm C measures one thing. | ||
| 26 | + | ||
| 27 | +## 2. Arms and gates (12-frame native gate, BD-rate vs libaom cpu-used 6 / rav1e speed 6) | ||
| 28 | + | ||
| 29 | +Control reproduced in this worktree at `e74e7229`, exactly the charter's numbers. | ||
| 30 | + | ||
| 31 | +| row | control | arm A (skip + is_inter) | arm C (cumulative, + compound tree) | | ||
| 32 | +|---|---|---|---| | ||
| 33 | +| film A | +21.5 / −4.4 | +21.5 / −4.5 | **+21.0 / −4.8** | | ||
| 34 | +| film B | +26.9 / −0.4 | +26.4 / −0.7 | **+25.0 / −1.8** | | ||
| 35 | +| screen | +14.4 / −33.2 | +14.5 / −33.2 | **+14.5 / −33.2** | | ||
| 36 | +| bars 1080p | −1.4 / −17.2 | −1.8 / −17.4 | −1.8 / −17.5 | | ||
| 37 | +| bars 2160p | +8.8 / −13.2 | +8.5 / −13.5 | +8.8 / −13.3 | | ||
| 38 | + | ||
| 39 | +**arm A ships** under the second keep rule: film B is 0.5 down on libaom and | ||
| 40 | +0.3 down on rav1e, film A flat on both (0.0 / −0.1), screen 0.1 up on libaom | ||
| 41 | +(inside the 0.3 the rule allows). | ||
| 42 | + | ||
| 43 | +**arm C ships** under the first keep rule: both film rows improve on both | ||
| 44 | +columns (−0.5/−0.3 and −1.4/−1.1), screen flat. | ||
| 45 | + | ||
| 46 | +Cumulative long-GOP gate (`bd_rate_film_long_gop`), both arms in: | ||
| 47 | + | ||
| 48 | +| row | control | shipped | | ||
| 49 | +|---|---|---| | ||
| 50 | +| film A | +26.4 / −6.4 | **+25.2 / −7.1** | | ||
| 51 | +| film B | +89.6 / +9.1 | **+87.1 / +7.7** | | ||
| 52 | + | ||
| 53 | +## 3. Unit tests | ||
| 54 | + | ||
| 55 | +* `encode::tests::the_skip_and_intra_inter_pricers_use_the_writers_own_contexts` | ||
| 56 | + — RED at row 0: `skip false at ctx 1: priced 0.049125199808023, writer codes | ||
| 57 | + 0.9885106508271125`. Green after arm A. | ||
| 58 | +* `encode::tests::the_compound_ref_pricer_pays_the_writers_own_symbol_sequence` | ||
| 59 | + — RED against the old constant pricer: `pair (1, 2) ... priced | ||
| 60 | + 10.19573638784928 bits, writer codes 5.455955052750427` (a LAST+LAST2 pair | ||
| 61 | + priced as LAST+GOLDEN). Green after arm C. | ||
| 62 | + | ||
| 63 | +## 4. Pins and suite | ||
| 64 | + | ||
| 65 | +* Stream pins re-taken: `[(150, 8311, 0xfb6d_75a4_5d13_3833), (60, 33087, | ||
| 66 | + 0x4d11_a03c_bdc6_e2d5)]` (was 8535 / 33221), green at the default preset and | ||
| 67 | + at `EC_AV1_SPEED=6`. | ||
| 68 | +* `mi_info_cell_stays_compact` / `mi_info_stays_small_enough_for_a_4k_grid` | ||
| 69 | + went RED mid-lane: `skip` as a `MiInfo` field took the cell 16 → 18 bytes, | ||
| 70 | + and that struct is the mv-stack scan's memory traffic. The flag moved to a | ||
| 71 | + `Vec<bool>` beside `MiGrid::cells`, filled only by the encoder's `record_mi` | ||
| 72 | + and read only by the stack. Both pins unchanged by that move, so the gate | ||
| 73 | + numbers above stand. | ||
| 74 | +* Split suite, release lib, all RC=0: s1 (`--skip stream::`) 338 passed / 0 | ||
| 75 | + failed / 32 ignored; s2 (`stream:: --skip 10bit`) 42 / 0 / 1; s3 (`10bit`) | ||
| 76 | + 208 / 0 / 16. `--ignored | ||
| 77 | + encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders` | ||
| 78 | + passed. | ||
| 79 | +* `timeout 900 cargo check --workspace --all-targets -j4`: 0 errors, 0 ec-av1 | ||
| 80 | + warnings (the workspace's 22 warnings are pre-existing, in ec-opus and | ||
| 81 | + ec-vorbis). | ||
| 82 | + | ||
| 83 | +## 5. Deferred | ||
| 84 | + | ||
| 85 | +`deferred: arm B (tx_size depth category context) — the pricer's | ||
| 86 | +tx_depth_bits lives in code_square, which takes neither the mi grid nor the | ||
| 87 | +block's mi coordinates, and the writer's tx_size_ctx/tx_size_ctx_txfm read a | ||
| 88 | +PER-TRANSFORM-UNIT published side (Neighbours::record_tx) that MiGrid does not | ||
| 89 | +carry at all; reproducing it needs a new published tx band plus a parameter | ||
| 90 | +through every code_square caller — what unblocks it: publish the resolved | ||
| 91 | +transform side per mi cell beside MiGrid::cells the way this lane's skip band | ||
| 92 | +now rides, then thread (grid, mi) into code_square.` | ||