Merge branch 'lane-av1comp' (1300db86): compound reference prediction (NEAREST_NEAREST / GLOBAL_GLOBAL / NEW_NEW, LAST+GOLDEN and LAST+ALTREF) on by default
Browse files at this commit6 changed files +944 -32
- crates/ec-av1/src/decode.rs +21 -2
- crates/ec-av1/src/encode.rs +312 -5
- crates/ec-av1/src/encoder.rs +20 -5
- crates/ec-av1/src/frame.rs +57 -8
- crates/ec-av1/src/mvstack.rs +8 -0
- crates/ec-av1/src/tile.rs +526 -12
crates/ec-av1/src/decode.rsmodified+21 -2
| @@ -25016,6 +25016,15 @@ fn read_comp_mode( | |||
| 25016 | ) -> bool { | 25016 | ) -> bool { |
| 25017 | let ctx = reference_mode_ctx(above, left); | 25017 | let ctx = reference_mode_ctx(above, left); |
| 25018 | let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1; | 25018 | let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1; |
| 25019 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 25020 | + eprintln!( | ||
| 25021 | + "EC_RCM ctx={ctx} val={} a={:?} l={:?} rng={}", | ||
| 25022 | + usize::from(compound), | ||
| 25023 | + above.map(|n| (n.is_inter, n.ref0, n.ref1)), | ||
| 25024 | + left.map(|n| (n.is_inter, n.ref0, n.ref1)), | ||
| 25025 | + dec.debug_state().0 | ||
| 25026 | + ); | ||
| 25027 | + } | ||
| 25019 | if compound { | 25028 | if compound { |
| 25020 | hit!(COMP_MODE_HITS); | 25029 | hit!(COMP_MODE_HITS); |
| 25021 | } | 25030 | } |
| @@ -37498,7 +37507,12 @@ mod tests { | |||
| 37498 | false, | 37507 | false, |
| 37499 | Some(mc::InterpFilterKind::Regular), | 37508 | Some(mc::InterpFilterKind::Regular), |
| 37500 | false, | 37509 | false, |
| 37501 | - false, | 37510 | + // The encoder's own `reference_select` (spec 5.9.22): these |
| 37511 | + // GOP round trips decode a tile the ENCODER wrote, so the | ||
| 37512 | + // header bit must be the one it wrote, not a hard-coded | ||
| 37513 | + // `false` (stale-header class -- a `reference_select` stream | ||
| 37514 | + // reads a `comp_mode` symbol this decode would skip). | ||
| 37515 | + crate::encode::reference_select(), | ||
| 37502 | frame.tx_select, | 37516 | frame.tx_select, |
| 37503 | &frame.loop_restoration, | 37517 | &frame.loop_restoration, |
| 37504 | Some(frame.start_cdfs.0.clone()), | 37518 | Some(frame.start_cdfs.0.clone()), |
| @@ -37817,7 +37831,12 @@ mod tests { | |||
| 37817 | false, | 37831 | false, |
| 37818 | Some(mc::InterpFilterKind::Regular), | 37832 | Some(mc::InterpFilterKind::Regular), |
| 37819 | false, | 37833 | false, |
| 37820 | - false, | 37834 | + // The encoder's own `reference_select` (spec 5.9.22): these |
| 37835 | + // GOP round trips decode a tile the ENCODER wrote, so the | ||
| 37836 | + // header bit must be the one it wrote, not a hard-coded | ||
| 37837 | + // `false` (stale-header class -- a `reference_select` stream | ||
| 37838 | + // reads a `comp_mode` symbol this decode would skip). | ||
| 37839 | + crate::encode::reference_select(), | ||
| 37821 | frame.tx_select, | 37840 | frame.tx_select, |
| 37822 | &frame.loop_restoration, | 37841 | &frame.loop_restoration, |
| 37823 | Some(frame.start_cdfs.0.clone()), | 37842 | Some(frame.start_cdfs.0.clone()), |
crates/ec-av1/src/encode.rsmodified+312 -5
| @@ -2653,6 +2653,8 @@ fn code_square_inter( | |||
| 2653 | eight: None, | 2653 | eight: None, |
| 2654 | tx_depth, | 2654 | tx_depth, |
| 2655 | inter: Some(InterInfo { | 2655 | inter: Some(InterInfo { |
| 2656 | + ref1: None, | ||
| 2657 | + mv1: (0, 0), | ||
| 2656 | ref_frame: crate::mvstack::LAST_FRAME, | 2658 | ref_frame: crate::mvstack::LAST_FRAME, |
| 2657 | mode: InterMode::NewMv, | 2659 | mode: InterMode::NewMv, |
| 2658 | mv: new_mv, | 2660 | mv: new_mv, |
| @@ -2687,6 +2689,8 @@ fn code_square_inter( | |||
| 2687 | eight: None, | 2689 | eight: None, |
| 2688 | tx_depth, | 2690 | tx_depth, |
| 2689 | inter: Some(InterInfo { | 2691 | inter: Some(InterInfo { |
| 2692 | + ref1: None, | ||
| 2693 | + mv1: (0, 0), | ||
| 2690 | ref_frame: crate::mvstack::LAST_FRAME, | 2694 | ref_frame: crate::mvstack::LAST_FRAME, |
| 2691 | mode: InterMode::NearestMv, | 2695 | mode: InterMode::NearestMv, |
| 2692 | mv, | 2696 | mv, |
| @@ -3698,6 +3702,38 @@ fn extra_ref_new_mv() -> bool { | |||
| 3698 | } | 3702 | } |
| 3699 | } | 3703 | } |
| 3700 | 3704 | ||
| 3705 | +/// The seven `ref_frame_idx` order hints of a frame coded on this crate's own | ||
| 3706 | +/// flat slot plan (`inter_frame_headers_slots`): every reference but | ||
| 3707 | +/// `GOLDEN_FRAME` and `ALTREF_FRAME` reads the frame just coded, | ||
| 3708 | +/// `GOLDEN_FRAME` reads the GOP's key frame, and `ALTREF_FRAME` reads the | ||
| 3709 | +/// frame two back when there is one (otherwise it, too, names the key frame's | ||
| 3710 | +/// slot). Hints are 7-bit, the only `order_hint_bits` this encoder writes. | ||
| 3711 | +pub(crate) fn flat_order_hints(order_hint: u32, key_hint: u32, has_altref: bool) -> [u32; 7] { | ||
| 3712 | + let last = order_hint.wrapping_sub(1) & 0x7f; | ||
| 3713 | + let alt = if has_altref { | ||
| 3714 | + order_hint.wrapping_sub(2) & 0x7f | ||
| 3715 | + } else { | ||
| 3716 | + key_hint | ||
| 3717 | + }; | ||
| 3718 | + [last, last, last, key_hint, last, last, alt] | ||
| 3719 | +} | ||
| 3720 | + | ||
| 3721 | +/// Whether an inter frame carries `reference_select` (spec 5.9.22): the | ||
| 3722 | +/// header bit that lets each block choose between a single and a compound | ||
| 3723 | +/// reference. Off by default until compound prediction is measured to pay; | ||
| 3724 | +/// `EC_AV1_COMPOUND=1` arms it in a test build (which is what the BD gate | ||
| 3725 | +/// runs as), and every block still codes SINGLE, so the only cost is one | ||
| 3726 | +/// `comp_mode` symbol per inter block. | ||
| 3727 | +const REFERENCE_SELECT: bool = true; | ||
| 3728 | + | ||
| 3729 | +/// [`REFERENCE_SELECT`], with the test-build environment override. | ||
| 3730 | +pub(crate) fn reference_select() -> bool { | ||
| 3731 | + match std::env::var("EC_AV1_COMPOUND").ok() { | ||
| 3732 | + Some(v) if cfg!(test) => v != "0", | ||
| 3733 | + _ => REFERENCE_SELECT, | ||
| 3734 | + } | ||
| 3735 | +} | ||
| 3736 | + | ||
| 3701 | /// [`tile::write_drl_idx`]'s cost against one stack: one `drl_mode` symbol | 3737 | /// [`tile::write_drl_idx`]'s cost against one stack: one `drl_mode` symbol |
| 3702 | /// per entry past `start`, `1` to advance and `0` to stop, at most two. | 3738 | /// per entry past `start`, `1` to advance and `0` to stop, at most two. |
| 3703 | fn drl_bits_for(stack: &MvStack, start: usize, target: usize) -> f64 { | 3739 | fn drl_bits_for(stack: &MvStack, start: usize, target: usize) -> f64 { |
| @@ -3842,6 +3878,56 @@ fn mc_trial( | |||
| 3842 | plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set) | 3878 | plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set) |
| 3843 | } | 3879 | } |
| 3844 | 3880 | ||
| 3881 | +/// [`mc_trial`]'s compound counterpart (spec 7.11.3.1's compound path): both | ||
| 3882 | +/// references predict into the `CONV_BUF` intermediate domain | ||
| 3883 | +/// ([`mc::predict_compound_intermediate`]) and are blended by | ||
| 3884 | +/// [`mc::combine_compound`] at the simple-average split `(8, 8)` -- exactly | ||
| 3885 | +/// what a decoder computes for a `comp_group_idx == 0`, `compound_idx == 1` | ||
| 3886 | +/// block, so the encoder's reconstruction is the decoder's. | ||
| 3887 | +#[allow(clippy::too_many_arguments)] | ||
| 3888 | +fn mc_trial_compound( | ||
| 3889 | + plane: &Plane, | ||
| 3890 | + x: usize, | ||
| 3891 | + y: usize, | ||
| 3892 | + side: usize, | ||
| 3893 | + (mv0, mv1): ((i32, i32), (i32, i32)), | ||
| 3894 | + luma: bool, | ||
| 3895 | + ref0: (&[u16], usize, usize, usize), | ||
| 3896 | + ref1: (&[u16], usize, usize, usize), | ||
| 3897 | + base_q_idx: u8, | ||
| 3898 | + deadzone: f64, | ||
| 3899 | + set: TxbSet, fctx: &crate::decode::FrameCtx, | ||
| 3900 | +) -> Trial { | ||
| 3901 | + let mut inter0 = [0i32; BLOCK * BLOCK]; | ||
| 3902 | + let mut inter1 = [0i32; BLOCK * BLOCK]; | ||
| 3903 | + let (inter0, inter1) = (&mut inter0[..side * side], &mut inter1[..side * side]); | ||
| 3904 | + for (mv, r, dst) in [(mv0, ref0, &mut *inter0), (mv1, ref1, &mut *inter1)] { | ||
| 3905 | + mc::predict_compound_intermediate( | ||
| 3906 | + r.0, | ||
| 3907 | + r.1, | ||
| 3908 | + r.2, | ||
| 3909 | + r.3, | ||
| 3910 | + mv_to_q4(x, mv.1, luma), | ||
| 3911 | + mv_to_q4(y, mv.0, luma), | ||
| 3912 | + mc::REF_NO_SCALE, | ||
| 3913 | + side, | ||
| 3914 | + side, | ||
| 3915 | + mc::InterpFilterKind::Regular, | ||
| 3916 | + mc::InterpFilterKind::Regular, | ||
| 3917 | + dst, | ||
| 3918 | + ); | ||
| 3919 | + } | ||
| 3920 | + let mut blended16 = [0u16; BLOCK * BLOCK]; | ||
| 3921 | + let blended16 = &mut blended16[..side * side]; | ||
| 3922 | + mc::combine_compound(inter0, inter1, 8, 8, blended16, fctx); | ||
| 3923 | + let mut prediction = [0u8; BLOCK * BLOCK]; | ||
| 3924 | + let prediction = &mut prediction[..side * side]; | ||
| 3925 | + for (dst, &src) in prediction.iter_mut().zip(blended16.iter()) { | ||
| 3926 | + *dst = src as u8; | ||
| 3927 | + } | ||
| 3928 | + plane.code_from_prediction(x, y, side, prediction, false, base_q_idx, deadzone, set) | ||
| 3929 | +} | ||
| 3930 | + | ||
| 3845 | /// Codes one 32x32 block of an inter frame as whichever costs least of: each | 3931 | /// Codes one 32x32 block of an inter frame as whichever costs least of: each |
| 3846 | /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's | 3932 | /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's |
| 3847 | /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by | 3933 | /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by |
| @@ -3874,7 +3960,11 @@ fn search_inter_block( | |||
| 3874 | // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a | 3960 | // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a |
| 3875 | // second motion search would double the wall of the stage that already | 3961 | // second motion search would double the wall of the stage that already |
| 3876 | // owns most of it. | 3962 | // owns most of it. |
| 3877 | - extra: &[(i8, &Picture, &MvStack)], fctx: &crate::decode::FrameCtx, | 3963 | + extra: &[(i8, &Picture, &MvStack)], |
| 3964 | + // The COMPOUND stack of each `LAST_FRAME` + extra-reference pair, built by | ||
| 3965 | + // the caller off the same MI grid the tile writer will (empty unless the | ||
| 3966 | + // frame carries `reference_select`). | ||
| 3967 | + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], fctx: &crate::decode::FrameCtx, | ||
| 3878 | ) -> (BlockCoeffs, f64) { | 3968 | ) -> (BlockCoeffs, f64) { |
| 3879 | // `luma_set` is the INTRA candidates' table; the two inter candidates | 3969 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 3880 | // below price their coefficients through `TxbSet::Luma32Inter`, which is | 3970 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| @@ -4051,6 +4141,8 @@ fn search_inter_block( | |||
| 4051 | stack.nearest_mv, | 4141 | stack.nearest_mv, |
| 4052 | last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), | 4142 | last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), |
| 4053 | InterInfo { | 4143 | InterInfo { |
| 4144 | + ref1: None, | ||
| 4145 | + mv1: (0, 0), | ||
| 4054 | ref_frame: crate::mvstack::LAST_FRAME, | 4146 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4055 | mode: InterMode::NearestMv, | 4147 | mode: InterMode::NearestMv, |
| 4056 | mv: stack.nearest_mv, | 4148 | mv: stack.nearest_mv, |
| @@ -4064,6 +4156,8 @@ fn search_inter_block( | |||
| 4064 | (0, 0), | 4156 | (0, 0), |
| 4065 | last_ref_bits + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), | 4157 | last_ref_bits + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), |
| 4066 | InterInfo { | 4158 | InterInfo { |
| 4159 | + ref1: None, | ||
| 4160 | + mv1: (0, 0), | ||
| 4067 | ref_frame: crate::mvstack::LAST_FRAME, | 4161 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4068 | mode: InterMode::GlobalMv, | 4162 | mode: InterMode::GlobalMv, |
| 4069 | mv: (0, 0), | 4163 | mv: (0, 0), |
| @@ -4079,6 +4173,8 @@ fn search_inter_block( | |||
| 4079 | e.mv, | 4173 | e.mv, |
| 4080 | near_bits + drl_bits(1, idx), | 4174 | near_bits + drl_bits(1, idx), |
| 4081 | InterInfo { | 4175 | InterInfo { |
| 4176 | + ref1: None, | ||
| 4177 | + mv1: (0, 0), | ||
| 4082 | ref_frame: crate::mvstack::LAST_FRAME, | 4178 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4083 | mode: InterMode::NearMv, | 4179 | mode: InterMode::NearMv, |
| 4084 | mv: e.mv, | 4180 | mv: e.mv, |
| @@ -4117,6 +4213,8 @@ fn search_inter_block( | |||
| 4117 | mv, | 4213 | mv, |
| 4118 | last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, | 4214 | last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, |
| 4119 | InterInfo { | 4215 | InterInfo { |
| 4216 | + ref1: None, | ||
| 4217 | + mv1: (0, 0), | ||
| 4120 | ref_frame: crate::mvstack::LAST_FRAME, | 4218 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4121 | mode: InterMode::NewMv, | 4219 | mode: InterMode::NewMv, |
| 4122 | mv, | 4220 | mv, |
| @@ -4125,6 +4223,9 @@ fn search_inter_block( | |||
| 4125 | )); | 4223 | )); |
| 4126 | } | 4224 | } |
| 4127 | 4225 | ||
| 4226 | + // Each extra reference's own `NEWMV` vector, for the compound | ||
| 4227 | + // `NEW_NEWMV` candidate below to pair with `LAST`'s. | ||
| 4228 | + let mut extra_new_mvs: Vec<(i8, (i32, i32))> = Vec::new(); | ||
| 4128 | for &(ref_frame, g, gstack) in extra { | 4229 | for &(ref_frame, g, gstack) in extra { |
| 4129 | let g_ref_bits = single_ref_bits(ref_frame); | 4230 | let g_ref_bits = single_ref_bits(ref_frame); |
| 4130 | let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); | 4231 | let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); |
| @@ -4135,6 +4236,8 @@ fn search_inter_block( | |||
| 4135 | + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) | 4236 | + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) |
| 4136 | + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), | 4237 | + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), |
| 4137 | InterInfo { | 4238 | InterInfo { |
| 4239 | + ref1: None, | ||
| 4240 | + mv1: (0, 0), | ||
| 4138 | ref_frame, | 4241 | ref_frame, |
| 4139 | mode: InterMode::NearestMv, | 4242 | mode: InterMode::NearestMv, |
| 4140 | mv: gstack.nearest_mv, | 4243 | mv: gstack.nearest_mv, |
| @@ -4145,6 +4248,8 @@ fn search_inter_block( | |||
| 4145 | (0, 0), | 4248 | (0, 0), |
| 4146 | g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), | 4249 | g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), |
| 4147 | InterInfo { | 4250 | InterInfo { |
| 4251 | + ref1: None, | ||
| 4252 | + mv1: (0, 0), | ||
| 4148 | ref_frame, | 4253 | ref_frame, |
| 4149 | mode: InterMode::GlobalMv, | 4254 | mode: InterMode::GlobalMv, |
| 4150 | mv: (0, 0), | 4255 | mv: (0, 0), |
| @@ -4199,11 +4304,14 @@ fn search_inter_block( | |||
| 4199 | #[cfg(test)] | 4304 | #[cfg(test)] |
| 4200 | stage_add(0, t.elapsed()); | 4305 | stage_add(0, t.elapsed()); |
| 4201 | let gmv = round_to_valid_mv(gfound.mv, gstack.pred_mv); | 4306 | let gmv = round_to_valid_mv(gfound.mv, gstack.pred_mv); |
| 4307 | + extra_new_mvs.push((ref_frame, gmv)); | ||
| 4202 | if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(gstack, gmv) { | 4308 | if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(gstack, gmv) { |
| 4203 | cands.push(( | 4309 | cands.push(( |
| 4204 | gmv, | 4310 | gmv, |
| 4205 | g_ref_bits + symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 0) + bits, | 4311 | g_ref_bits + symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 0) + bits, |
| 4206 | InterInfo { | 4312 | InterInfo { |
| 4313 | + ref1: None, | ||
| 4314 | + mv1: (0, 0), | ||
| 4207 | ref_frame, | 4315 | ref_frame, |
| 4208 | mode: InterMode::NewMv, | 4316 | mode: InterMode::NewMv, |
| 4209 | mv: gmv, | 4317 | mv: gmv, |
| @@ -4214,6 +4322,130 @@ fn search_inter_block( | |||
| 4214 | } | 4322 | } |
| 4215 | } | 4323 | } |
| 4216 | 4324 | ||
| 4325 | + // The compound candidates (spec 5.11.24's `INTER_COMPOUND_MODES`), one | ||
| 4326 | + // trial set each: both references predict the block and the two | ||
| 4327 | + // predictions are averaged, which is what pays on content where neither | ||
| 4328 | + // reference alone is right. Priced with the same static-CDF approximation | ||
| 4329 | + // the single-reference candidates use, plus the compound-only symbols | ||
| 4330 | + // (`comp_mode`, the reference-pair tree, `comp_group_idx`/`compound_idx`). | ||
| 4331 | + for (ref1, g, cstack) in compound { | ||
| 4332 | + let (ref1, g) = (*ref1, *g); | ||
| 4333 | + let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME) | ||
| 4334 | + .contains(&ref1); | ||
| 4335 | + let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) | ||
| 4336 | + + if uni { | ||
| 4337 | + // LAST + a backward reference: `comp_ref_type` = bidirectional, | ||
| 4338 | + // `comp_ref` LAST, `comp_bwdref` ALTREF. | ||
| 4339 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 1) | ||
| 4340 | + + symbol_bits(&cdf::COMP_REF[0][0], 0) | ||
| 4341 | + + symbol_bits(&cdf::COMP_REF[0][1], 0) | ||
| 4342 | + + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) | ||
| 4343 | + } else { | ||
| 4344 | + // LAST + GOLDEN: unidirectional, `uni_comp_ref` p0/p1/p2. | ||
| 4345 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 0) | ||
| 4346 | + + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) | ||
| 4347 | + + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) | ||
| 4348 | + + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) | ||
| 4349 | + }; | ||
| 4350 | + let mode_ctx = | ||
| 4351 | + cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; | ||
| 4352 | + let mode_bits_of = | ||
| 4353 | + |mode: usize| pair_bits + symbol_bits(&cdf::INTER_COMPOUND_MODE[mode_ctx], mode); | ||
| 4354 | + let mut ccands: Vec<(((i32, i32), (i32, i32)), f64, InterInfo)> = vec![ | ||
| 4355 | + ( | ||
| 4356 | + cstack.nearest_mv, | ||
| 4357 | + mode_bits_of(0), | ||
| 4358 | + InterInfo { | ||
| 4359 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 4360 | + mode: InterMode::NearestNearestMv, | ||
| 4361 | + mv: cstack.nearest_mv.0, | ||
| 4362 | + mv1: cstack.nearest_mv.1, | ||
| 4363 | + ref1: Some(ref1), | ||
| 4364 | + ref_mv_idx: 0, | ||
| 4365 | + }, | ||
| 4366 | + ), | ||
| 4367 | + ( | ||
| 4368 | + ((0, 0), (0, 0)), | ||
| 4369 | + mode_bits_of(6), | ||
| 4370 | + InterInfo { | ||
| 4371 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 4372 | + mode: InterMode::GlobalGlobalMv, | ||
| 4373 | + mv: (0, 0), | ||
| 4374 | + mv1: (0, 0), | ||
| 4375 | + ref1: Some(ref1), | ||
| 4376 | + ref_mv_idx: 0, | ||
| 4377 | + }, | ||
| 4378 | + ), | ||
| 4379 | + ]; | ||
| 4380 | + // `NEW_NEWMV` seeded from the two single-reference searches this | ||
| 4381 | + // block already ran -- no joint search of its own. | ||
| 4382 | + if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) { | ||
| 4383 | + let base = cstack | ||
| 4384 | + .entries | ||
| 4385 | + .first() | ||
| 4386 | + .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1)); | ||
| 4387 | + if let (Some(b0), Some(b1)) = ( | ||
| 4388 | + mv_residual_bits(mv, base.0), | ||
| 4389 | + mv_residual_bits(mv1, base.1), | ||
| 4390 | + ) { | ||
| 4391 | + ccands.push(( | ||
| 4392 | + (mv, mv1), | ||
| 4393 | + mode_bits_of(7) + b0 + b1, | ||
| 4394 | + InterInfo { | ||
| 4395 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 4396 | + mode: InterMode::NewNewMv, | ||
| 4397 | + mv, | ||
| 4398 | + mv1, | ||
| 4399 | + ref1: Some(ref1), | ||
| 4400 | + ref_mv_idx: 0, | ||
| 4401 | + }, | ||
| 4402 | + )); | ||
| 4403 | + } | ||
| 4404 | + } | ||
| 4405 | + ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); | ||
| 4406 | + ccands.dedup_by_key(|c| c.0); | ||
| 4407 | + let g_luma = (g.y.as_slice(), g.width, luma.true_width, luma.true_height); | ||
| 4408 | + let g_u = (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height); | ||
| 4409 | + let g_v = (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height); | ||
| 4410 | + for (mvs, bits, info) in ccands { | ||
| 4411 | + let luma_trial = mc_trial_compound( | ||
| 4412 | + luma, x, y, BLOCK, mvs, true, | ||
| 4413 | + (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), g_luma, | ||
| 4414 | + search.base_q_idx, search.deadzone, TxbSet::Luma32Inter, fctx, | ||
| 4415 | + ); | ||
| 4416 | + let u = mc_trial_compound( | ||
| 4417 | + &chroma[0], x / 2, y / 2, BLOCK / 2, mvs, false, | ||
| 4418 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), g_u, | ||
| 4419 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 4420 | + ); | ||
| 4421 | + let v = mc_trial_compound( | ||
| 4422 | + &chroma[1], x / 2, y / 2, BLOCK / 2, mvs, false, | ||
| 4423 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), g_v, | ||
| 4424 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 4425 | + ); | ||
| 4426 | + let skip = luma_trial.levels.iter().all(|&l| l == 0) | ||
| 4427 | + && u.levels.iter().all(|&l| l == 0) | ||
| 4428 | + && v.levels.iter().all(|&l| l == 0); | ||
| 4429 | + let cost = luma_trial.sse | ||
| 4430 | + + u.sse | ||
| 4431 | + + v.sse | ||
| 4432 | + + search.lambda | ||
| 4433 | + * (skip_bits(skip) | ||
| 4434 | + + intra_inter_bits(true) | ||
| 4435 | + + bits | ||
| 4436 | + + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits }); | ||
| 4437 | + consider(Candidate { | ||
| 4438 | + cost, | ||
| 4439 | + luma: luma_trial, | ||
| 4440 | + u, | ||
| 4441 | + v, | ||
| 4442 | + mode: DC_PRED, | ||
| 4443 | + skip, | ||
| 4444 | + inter: Some(info), | ||
| 4445 | + }); | ||
| 4446 | + } | ||
| 4447 | + } | ||
| 4448 | + | ||
| 4217 | // One trial set per distinct (reference, vector) pair: sort, cheapest | 4449 | // One trial set per distinct (reference, vector) pair: sort, cheapest |
| 4218 | // syntax first, and drop the rest of each pair's run. | 4450 | // syntax first, and drop the rest of each pair's run. |
| 4219 | cands.sort_by(|a, b| { | 4451 | cands.sort_by(|a, b| { |
| @@ -4367,6 +4599,12 @@ pub(crate) fn encode_inter_frame( | |||
| 4367 | base_q_idx: u8, | 4599 | base_q_idx: u8, |
| 4368 | deadzone: f64, | 4600 | deadzone: f64, |
| 4369 | order_hint: u32, | 4601 | order_hint: u32, |
| 4602 | + // The order hint each of the seven `ref_frame_idx` entries names, which a | ||
| 4603 | + // decoder reads out of its own reference state -- only spec 5.9.22's | ||
| 4604 | + // `skipModeAllowed` (in `frame::write_frame_header`) reads it, and only | ||
| 4605 | + // on a `reference_select` frame. [`flat_order_hints`] builds it for the | ||
| 4606 | + // callers that follow this crate's own flat slot plan. | ||
| 4607 | + order_hints: [u32; 7], | ||
| 4370 | render: (usize, usize), | 4608 | render: (usize, usize), |
| 4371 | // The tables this frame's writer starts from: what the previous frame | 4609 | // The tables this frame's writer starts from: what the previous frame |
| 4372 | // stored into the slot this one's `primary_ref_frame` names. `None` | 4610 | // stored into the slot this one's `primary_ref_frame` names. `None` |
| @@ -4443,6 +4681,14 @@ pub(crate) fn encode_inter_frame( | |||
| 4443 | header.render_width = render.0 as u32; | 4681 | header.render_width = render.0 as u32; |
| 4444 | header.render_height = render.1 as u32; | 4682 | header.render_height = render.1 as u32; |
| 4445 | let sign_bias = pyramid.map_or(crate::mvstack::NO_SIGN_BIAS, |p| p.sign_bias); | 4683 | let sign_bias = pyramid.map_or(crate::mvstack::NO_SIGN_BIAS, |p| p.sign_bias); |
| 4684 | + // The order hint behind each `ref_frame_idx` entry. The flat path's own | ||
| 4685 | + // slot plan (`inter_frame_headers_slots`) is: every reference but | ||
| 4686 | + // `GOLDEN_FRAME` reads `last_slot` (the frame just coded) except | ||
| 4687 | + // `ALTREF_FRAME`, which reads the frame two back when there is one; | ||
| 4688 | + // `GOLDEN_FRAME` is the key frame, order hint 0. Only | ||
| 4689 | + // `frame::write_frame_header`'s `skipModeAllowed` reads this. | ||
| 4690 | + header.order_hints = order_hints; | ||
| 4691 | + header.reference_select = reference_select(); | ||
| 4446 | if let Some(p) = pyramid { | 4692 | if let Some(p) = pyramid { |
| 4447 | header.show_frame = p.show_frame; | 4693 | header.show_frame = p.show_frame; |
| 4448 | header.showable_frame = !p.show_frame; | 4694 | header.showable_frame = !p.show_frame; |
| @@ -4570,6 +4816,37 @@ pub(crate) fn encode_inter_frame( | |||
| 4570 | .iter() | 4816 | .iter() |
| 4571 | .map(|(r, p, st)| (*r, *p, st)) | 4817 | .map(|(r, p, st)| (*r, *p, st)) |
| 4572 | .collect(); | 4818 | .collect(); |
| 4819 | + // The COMPOUND stack of each `LAST` + extra pair, off the | ||
| 4820 | + // same grid (and the same armed sign bias) the tile writer | ||
| 4821 | + // rebuilds it from. Empty unless this frame codes | ||
| 4822 | + // `reference_select`, which is what gates the syntax. | ||
| 4823 | + let compound: Vec<(i8, &Picture, crate::mvstack::CompoundMvStack)> = | ||
| 4824 | + if header.reference_select { | ||
| 4825 | + extra_stacks | ||
| 4826 | + .iter() | ||
| 4827 | + .map(|&(r, p, _)| { | ||
| 4828 | + ( | ||
| 4829 | + r, | ||
| 4830 | + p, | ||
| 4831 | + crate::mvstack::find_mv_stack_compound( | ||
| 4832 | + &grid, | ||
| 4833 | + mi_row, | ||
| 4834 | + mi_col, | ||
| 4835 | + 8, | ||
| 4836 | + 8, | ||
| 4837 | + (crate::mvstack::LAST_FRAME, r), | ||
| 4838 | + mi_cols, | ||
| 4839 | + mi_rows, | ||
| 4840 | + grid.sign_bias_table(), | ||
| 4841 | + &[(0, 0); 7], | ||
| 4842 | + None, | ||
| 4843 | + ), | ||
| 4844 | + ) | ||
| 4845 | + }) | ||
| 4846 | + .collect() | ||
| 4847 | + } else { | ||
| 4848 | + Vec::new() | ||
| 4849 | + }; | ||
| 4573 | 4850 | ||
| 4574 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); | 4851 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 4575 | let (block, mut cost_whole) = search_inter_block( | 4852 | let (block, mut cost_whole) = search_inter_block( |
| @@ -4580,7 +4857,8 @@ pub(crate) fn encode_inter_frame( | |||
| 4580 | &mode_bits_table, | 4857 | &mode_bits_table, |
| 4581 | reference, | 4858 | reference, |
| 4582 | &stack, | 4859 | &stack, |
| 4583 | - &extra, fctx, | 4860 | + &extra, |
| 4861 | + &compound, fctx, | ||
| 4584 | ); | 4862 | ); |
| 4585 | cost_whole += search.lambda * partition_bits(BLOCK, false); | 4863 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 4586 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); | 4864 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -4918,6 +5196,8 @@ pub(crate) fn encode_inter_frame( | |||
| 4918 | .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); | 5196 | .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); |
| 4919 | let start_cdfs = CdfSnapshot(cdfs.clone()); | 5197 | let start_cdfs = CdfSnapshot(cdfs.clone()); |
| 4920 | crate::tile::arm_sign_bias(sign_bias); | 5198 | crate::tile::arm_sign_bias(sign_bias); |
| 5199 | + crate::tile::arm_reference_select(header.reference_select); | ||
| 5200 | + crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); | ||
| 4921 | let mut tile = crate::tile::sb_coeff_inter_frame_tile_cdfs( | 5201 | let mut tile = crate::tile::sb_coeff_inter_frame_tile_cdfs( |
| 4922 | mi_cols, | 5202 | mi_cols, |
| 4923 | mi_rows, | 5203 | mi_rows, |
| @@ -5008,6 +5288,8 @@ pub(crate) fn encode_inter_frame( | |||
| 5008 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); | 5288 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); |
| 5009 | crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); | 5289 | crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); |
| 5010 | crate::tile::arm_sign_bias(sign_bias); | 5290 | crate::tile::arm_sign_bias(sign_bias); |
| 5291 | + crate::tile::arm_reference_select(reference_select()); | ||
| 5292 | + crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); | ||
| 5011 | let mut cdfs = start_cdfs.0.clone(); | 5293 | let mut cdfs = start_cdfs.0.clone(); |
| 5012 | let coded = crate::tile::sb_coeff_inter_frame_tile_cdfs( | 5294 | let coded = crate::tile::sb_coeff_inter_frame_tile_cdfs( |
| 5013 | mi_cols, | 5295 | mi_cols, |
| @@ -5160,6 +5442,7 @@ pub(crate) fn encode_sequence_with_ctx( | |||
| 5160 | base_q_idx, | 5442 | base_q_idx, |
| 5161 | deadzone, | 5443 | deadzone, |
| 5162 | order_hint, | 5444 | order_hint, |
| 5445 | + flat_order_hints(order_hint, 0, prev2.is_some()), | ||
| 5163 | render, | 5446 | render, |
| 5164 | Some(&carried.0), | 5447 | Some(&carried.0), |
| 5165 | Some(&golden), | 5448 | Some(&golden), |
| @@ -5581,6 +5864,8 @@ mod tests { | |||
| 5581 | eight: None, | 5864 | eight: None, |
| 5582 | tx_depth: 0, | 5865 | tx_depth: 0, |
| 5583 | inter: Some(InterInfo { | 5866 | inter: Some(InterInfo { |
| 5867 | + ref1: None, | ||
| 5868 | + mv1: (0, 0), | ||
| 5584 | ref_frame: crate::mvstack::LAST_FRAME, | 5869 | ref_frame: crate::mvstack::LAST_FRAME, |
| 5585 | mode: InterMode::NearestMv, | 5870 | mode: InterMode::NearestMv, |
| 5586 | mv: (0, 0), | 5871 | mv: (0, 0), |
| @@ -5590,6 +5875,8 @@ mod tests { | |||
| 5590 | let skipped_block = BlockCoeffs { | 5875 | let skipped_block = BlockCoeffs { |
| 5591 | skip: true, | 5876 | skip: true, |
| 5592 | inter: Some(InterInfo { | 5877 | inter: Some(InterInfo { |
| 5878 | + ref1: None, | ||
| 5879 | + mv1: (0, 0), | ||
| 5593 | ref_frame: crate::mvstack::LAST_FRAME, | 5880 | ref_frame: crate::mvstack::LAST_FRAME, |
| 5594 | mode: InterMode::NearestMv, | 5881 | mode: InterMode::NearestMv, |
| 5595 | mv: (0, 0), | 5882 | mv: (0, 0), |
| @@ -7220,7 +7507,7 @@ mod tests { | |||
| 7220 | let padded_pic = picture.padded_to(SUPERBLOCK); | 7507 | let padded_pic = picture.padded_to(SUPERBLOCK); |
| 7221 | let t = Instant::now(); | 7508 | let t = Instant::now(); |
| 7222 | let inter = | 7509 | let inter = |
| 7223 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx, None).unwrap(); | 7510 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, flat_order_hints(1, 0, false), (width, height), None, None, None, fctx, None).unwrap(); |
| 7224 | let inter_t = t.elapsed(); | 7511 | let inter_t = t.elapsed(); |
| 7225 | eprintln!( | 7512 | eprintln!( |
| 7226 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ | 7513 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ |
| @@ -7318,7 +7605,7 @@ mod tests { | |||
| 7318 | stage_reset(); | 7605 | stage_reset(); |
| 7319 | let t = Instant::now(); | 7606 | let t = Instant::now(); |
| 7320 | let inter = | 7607 | let inter = |
| 7321 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx, None).unwrap(); | 7608 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, flat_order_hints(1, 0, false), (width, height), None, None, None, fctx, None).unwrap(); |
| 7322 | let total_t = t.elapsed(); | 7609 | let total_t = t.elapsed(); |
| 7323 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); | 7610 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); |
| 7324 | let accounted = motion_search + transform_quant + entropy; | 7611 | let accounted = motion_search + transform_quant + entropy; |
| @@ -7495,6 +7782,7 @@ mod tests { | |||
| 7495 | q, | 7782 | q, |
| 7496 | 0.5, | 7783 | 0.5, |
| 7497 | 1, | 7784 | 1, |
| 7785 | + flat_order_hints(1, 0, false), | ||
| 7498 | (width, height), None, None, None, fctx, None, | 7786 | (width, height), None, None, None, fctx, None, |
| 7499 | ) | 7787 | ) |
| 7500 | .unwrap(); | 7788 | .unwrap(); |
| @@ -7512,6 +7800,7 @@ mod tests { | |||
| 7512 | q, | 7800 | q, |
| 7513 | 0.5, | 7801 | 0.5, |
| 7514 | 1, | 7802 | 1, |
| 7803 | + flat_order_hints(1, 0, false), | ||
| 7515 | (width, height), None, None, None, fctx, None, | 7804 | (width, height), None, None, None, fctx, None, |
| 7516 | ) | 7805 | ) |
| 7517 | .unwrap(); | 7806 | .unwrap(); |
| @@ -8005,7 +8294,7 @@ mod tests { | |||
| 8005 | }) | 8294 | }) |
| 8006 | }; | 8295 | }; |
| 8007 | let pins: [(u8, usize, u64); 2] = | 8296 | let pins: [(u8, usize, u64); 2] = |
| 8008 | - [(150, 7444, 0x76eb980832d719f1), (60, 27692, 0x8c88f7e73455d439)]; | 8297 | + [(150, 7209, 0xacdd_6952_78c1_49f2), (60, 27353, 0x9be8_7898_f08d_3b77)]; |
| 8009 | for (q, bytes, hash) in pins { | 8298 | for (q, bytes, hash) in pins { |
| 8010 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); | 8299 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 8011 | assert_eq!( | 8300 | assert_eq!( |
| @@ -8169,6 +8458,8 @@ mod tests { | |||
| 8169 | let _ = crate::tile::take_inter_mode_hits(); | 8458 | let _ = crate::tile::take_inter_mode_hits(); |
| 8170 | let _ = crate::tile::take_drl_hits(); | 8459 | let _ = crate::tile::take_drl_hits(); |
| 8171 | let _ = crate::tile::take_ref_hits(); | 8460 | let _ = crate::tile::take_ref_hits(); |
| 8461 | + let _ = crate::tile::take_compound_mode_hits(); | ||
| 8462 | + let _ = crate::tile::take_compound_pair_hits(); | ||
| 8172 | let _ = crate::motion::take_census(); | 8463 | let _ = crate::motion::take_census(); |
| 8173 | let (ours, ours_wall) = match pyramid_from_env() { | 8464 | let (ours, ours_wall) = match pyramid_from_env() { |
| 8174 | None => our_ladder(&source, width, height, fctx), | 8465 | None => our_ladder(&source, width, height, fctx), |
| @@ -8204,6 +8495,22 @@ mod tests { | |||
| 8204 | 100.0 * global as f64 / modes_total as f64, | 8495 | 100.0 * global as f64 / modes_total as f64, |
| 8205 | 100.0 * new as f64 / modes_total as f64, | 8496 | 100.0 * new as f64 / modes_total as f64, |
| 8206 | ); | 8497 | ); |
| 8498 | + // The compound census: what share of the coded inter blocks took | ||
| 8499 | + // a compound reference at all, which compound mode they took, and | ||
| 8500 | + // which pair (gate-blind-to-feature -- a compound mode nobody | ||
| 8501 | + // picks cannot be what a BD number credits). | ||
| 8502 | + let comp_modes = crate::tile::take_compound_mode_hits(); | ||
| 8503 | + let comp_pairs = crate::tile::take_compound_pair_hits(); | ||
| 8504 | + let comp_total: usize = comp_modes.iter().sum(); | ||
| 8505 | + eprintln!( | ||
| 8506 | + "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \ | ||
| 8507 | + NEAREST_NEAREST {} NEAR_NEAR {} GLOBAL_GLOBAL {} NEW_NEW {}; pairs \ | ||
| 8508 | + LAST+GOLDEN {} LAST+ALTREF {}", | ||
| 8509 | + modes_total + comp_total, | ||
| 8510 | + 100.0 * comp_total as f64 / (modes_total + comp_total) as f64, | ||
| 8511 | + comp_modes[0], comp_modes[1], comp_modes[6], comp_modes[7], | ||
| 8512 | + comp_pairs[3], comp_pairs[6], | ||
| 8513 | + ); | ||
| 8207 | // Where the wall-owning stage actually goes: how many candidate | 8514 | // Where the wall-owning stage actually goes: how many candidate |
| 8208 | // evaluations (one `mc::predict` + SAD each) a search spends, how | 8515 | // evaluations (one `mc::predict` + SAD each) a search spends, how |
| 8209 | // many of them are subpel, and how often the winner is the seed | 8516 | // many of them are subpel, and how often the winner is the seed |
crates/ec-av1/src/encoder.rsmodified+20 -5
| @@ -708,6 +708,15 @@ impl Av1Encoder { | |||
| 708 | base_q_idx, | 708 | base_q_idx, |
| 709 | DEADZONE, | 709 | DEADZONE, |
| 710 | order as u32, | 710 | order as u32, |
| 711 | + // The flat path's own slot plan, in order hints: the key frame | ||
| 712 | + // of the GOP this picture sits in is what `GOLDEN_FRAME` | ||
| 713 | + // names, and every reference but `ALTREF_FRAME` is the frame | ||
| 714 | + // just coded. | ||
| 715 | + crate::encode::flat_order_hints( | ||
| 716 | + order as u32 & 0x7f, | ||
| 717 | + (order - order % self.config.gop as u64) as u32 & 0x7f, | ||
| 718 | + self.prev2.is_some(), | ||
| 719 | + ), | ||
| 711 | render, | 720 | render, |
| 712 | self.carried_cdfs.as_ref().map(|c| &c.0), | 721 | self.carried_cdfs.as_ref().map(|c| &c.0), |
| 713 | self.golden.as_ref(), | 722 | self.golden.as_ref(), |
| @@ -827,14 +836,19 @@ impl Av1Encoder { | |||
| 827 | .as_ref() | 836 | .as_ref() |
| 828 | .is_some_and(|s| s.order_hint > order_hint) | 837 | .is_some_and(|s| s.order_hint > order_hint) |
| 829 | }; | 838 | }; |
| 830 | - for (i, slot) in [ | 839 | + let slots = [ |
| 831 | last_slot, last_slot, last_slot, GOLDEN_SLOT, last_slot, last_slot, altref_slot, | 840 | last_slot, last_slot, last_slot, GOLDEN_SLOT, last_slot, last_slot, altref_slot, |
| 832 | - ] | 841 | + ]; |
| 833 | - .into_iter() | 842 | + for (i, slot) in slots.into_iter().enumerate() { |
| 834 | - .enumerate() | ||
| 835 | - { | ||
| 836 | sign_bias[i] = ahead(slot, &self.dpb); | 843 | sign_bias[i] = ahead(slot, &self.dpb); |
| 837 | } | 844 | } |
| 845 | + // The same seven slots' order hints, for `skipModeAllowed` (spec | ||
| 846 | + // 5.9.22) in the frame-header writer. | ||
| 847 | + let order_hints: [u32; 7] = slots.map(|slot| { | ||
| 848 | + self.dpb[slot as usize] | ||
| 849 | + .as_ref() | ||
| 850 | + .map_or(0, |s| (s.order_hint & 0x7f) as u32) | ||
| 851 | + }); | ||
| 838 | let reference = self.dpb[last_slot as usize] | 852 | let reference = self.dpb[last_slot as usize] |
| 839 | .as_ref() | 853 | .as_ref() |
| 840 | .map(|s| s.picture.clone()) | 854 | .map(|s| s.picture.clone()) |
| @@ -852,6 +866,7 @@ impl Av1Encoder { | |||
| 852 | base_q_idx, | 866 | base_q_idx, |
| 853 | DEADZONE, | 867 | DEADZONE, |
| 854 | order_hint, | 868 | order_hint, |
| 869 | + order_hints, | ||
| 855 | render, | 870 | render, |
| 856 | start_cdfs.as_ref(), | 871 | start_cdfs.as_ref(), |
| 857 | golden.as_ref(), | 872 | golden.as_ref(), |
crates/ec-av1/src/frame.rsmodified+57 -8
| @@ -359,6 +359,45 @@ pub fn write_show_existing_frame_header( | |||
| 359 | Ok(()) | 359 | Ok(()) |
| 360 | } | 360 | } |
| 361 | 361 | ||
| 362 | +/// Spec 5.9.22's `skipModeAllowed`, the gate on whether a | ||
| 363 | +/// `skip_mode_present` bit is coded at all -- the write side of | ||
| 364 | +/// `ec_av1_syntax::frame`'s `read_skip_mode_params`, reading the per-reference | ||
| 365 | +/// order hints out of `h.order_hints` (what that reader takes from its own | ||
| 366 | +/// reference state). Returns `false` for every frame that codes no | ||
| 367 | +/// `reference_select`, which is every frame this crate wrote before compound | ||
| 368 | +/// references existed. | ||
| 369 | +fn skip_mode_allowed(seq: &SequenceHeader, h: &FrameHeader) -> bool { | ||
| 370 | + if h.frame_is_intra || !h.reference_select || !seq.enable_order_hint { | ||
| 371 | + return false; | ||
| 372 | + } | ||
| 373 | + let bits = seq.order_hint_bits; | ||
| 374 | + let dist = |a: u32, b: u32| crate::motion_field::get_relative_dist(bits, a, b); | ||
| 375 | + let (mut forward_idx, mut backward_idx) = (-1i32, -1i32); | ||
| 376 | + let (mut forward_hint, mut backward_hint) = (0u32, 0u32); | ||
| 377 | + for (i, &hint) in h.order_hints.iter().enumerate() { | ||
| 378 | + if dist(hint, h.order_hint) < 0 { | ||
| 379 | + if forward_idx < 0 || dist(hint, forward_hint) > 0 { | ||
| 380 | + forward_idx = i as i32; | ||
| 381 | + forward_hint = hint; | ||
| 382 | + } | ||
| 383 | + } else if dist(hint, h.order_hint) > 0 | ||
| 384 | + && (backward_idx < 0 || dist(hint, backward_hint) < 0) | ||
| 385 | + { | ||
| 386 | + backward_idx = i as i32; | ||
| 387 | + backward_hint = hint; | ||
| 388 | + } | ||
| 389 | + } | ||
| 390 | + if forward_idx < 0 { | ||
| 391 | + return false; | ||
| 392 | + } | ||
| 393 | + if backward_idx >= 0 { | ||
| 394 | + return true; | ||
| 395 | + } | ||
| 396 | + h.order_hints | ||
| 397 | + .iter() | ||
| 398 | + .any(|&hint| dist(hint, forward_hint) < 0) | ||
| 399 | +} | ||
| 400 | + | ||
| 362 | /// `uncompressed_header()` (spec 5.9.2) for a shown key or inter frame: no | 401 | /// `uncompressed_header()` (spec 5.9.2) for a shown key or inter frame: no |
| 363 | /// decoder model, no frame ids, no still-picture shortcut, and (for an inter | 402 | /// decoder model, no frame ids, no still-picture shortcut, and (for an inter |
| 364 | /// frame) short reference signalling, non-identity global motion, compound | 403 | /// frame) short reference signalling, non-identity global motion, compound |
| @@ -530,15 +569,25 @@ pub fn write_frame_header(w: &mut BitWriter, seq: &SequenceHeader, h: &FrameHead | |||
| 530 | } | 569 | } |
| 531 | if h.frame_is_intra { | 570 | if h.frame_is_intra { |
| 532 | // reference_select is forced false and not coded. | 571 | // reference_select is forced false and not coded. |
| 533 | - } else if h.reference_select { | ||
| 534 | - return Err(Error::unsupported( | ||
| 535 | - "AV1 frame header", | ||
| 536 | - "reference_select (and the skip mode it gates) is not written", | ||
| 537 | - )); | ||
| 538 | } else { | 572 | } else { |
| 539 | - w.write_bit(false); | 573 | + w.write_bit(h.reference_select); |
| 540 | - // skip_mode_present is forced false when reference_select is false | 574 | + // spec 5.9.22 `skip_mode_params`: `skip_mode_present` is coded only |
| 541 | - // (spec 5.9.22), and not coded. | 575 | + // when `skipModeAllowed`, which needs `reference_select` and either a |
| 576 | + // forward and a backward reference or two distinct forward ones. | ||
| 577 | + // Mirrors `ec_av1_syntax::frame`'s own `read_skip_mode_params` off | ||
| 578 | + // `h.order_hints` (the per-reference order hints the ENCODER fills | ||
| 579 | + // from its DPB, which is what a decoder reads out of its own | ||
| 580 | + // `state.refs`); a `false` bit here would desync every stream whose | ||
| 581 | + // references make skip mode allowed. | ||
| 582 | + if skip_mode_allowed(seq, h) { | ||
| 583 | + if h.skip_mode_present { | ||
| 584 | + return Err(Error::unsupported( | ||
| 585 | + "AV1 frame header", | ||
| 586 | + "skip_mode_present is not written -- no writer codes a skip-mode block", | ||
| 587 | + )); | ||
| 588 | + } | ||
| 589 | + w.write_bit(false); | ||
| 590 | + } | ||
| 542 | } | 591 | } |
| 543 | if h.frame_is_intra || h.error_resilient_mode || !seq.enable_warped_motion { | 592 | if h.frame_is_intra || h.error_resilient_mode || !seq.enable_warped_motion { |
| 544 | // allow_warped_motion is forced false and not coded. | 593 | // allow_warped_motion is forced false and not coded. |
crates/ec-av1/src/mvstack.rsmodified+8 -0
| @@ -284,6 +284,14 @@ impl MiGrid { | |||
| 284 | } | 284 | } |
| 285 | 285 | ||
| 286 | /// Sets this frame's `ref_frame_sign_bias` (see the field). | 286 | /// Sets this frame's `ref_frame_sign_bias` (see the field). |
| 287 | + /// This grid's armed `ref_frame_sign_bias`, for the callers that build a | ||
| 288 | + /// COMPOUND stack (which takes the table explicitly, unlike | ||
| 289 | + /// [`find_mv_stack`]). | ||
| 290 | + pub fn sign_bias_table(&self) -> &SignBiasTable { | ||
| 291 | + &self.sign_bias | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + /// Arms this grid's `ref_frame_sign_bias` for every stack built off it. | ||
| 287 | pub fn set_sign_bias(&mut self, sign_bias: SignBiasTable) { | 295 | pub fn set_sign_bias(&mut self, sign_bias: SignBiasTable) { |
| 288 | self.sign_bias = sign_bias; | 296 | self.sign_bias = sign_bias; |
| 289 | } | 297 | } |
crates/ec-av1/src/tile.rsmodified+526 -12
| @@ -160,6 +160,300 @@ pub(crate) fn arm_sign_bias(sign_bias: crate::mvstack::SignBiasTable) { | |||
| 160 | SIGN_BIAS.with(|c| c.set(sign_bias)); | 160 | SIGN_BIAS.with(|c| c.set(sign_bias)); |
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | +thread_local! { | ||
| 164 | + /// This frame header's own `reference_select` bit (spec 5.9.22), armed | ||
| 165 | + /// the same way `arm_sign_bias` arms the sign-bias table rather than | ||
| 166 | + /// threaded through the five nested writer helpers between here and the | ||
| 167 | + /// per-block mode syntax. `true` makes every inter block of size at least | ||
| 168 | + /// 8x8 code a `comp_mode` symbol, exactly where decode.rs `read_comp_mode` | ||
| 169 | + /// reads one. | ||
| 170 | + static REFERENCE_SELECT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | ||
| 171 | +} | ||
| 172 | + | ||
| 173 | +/// Arms the frame's `reference_select` bit for the next tile written on this | ||
| 174 | +/// thread; cleared by [`CdefIdxGuard`] when that writer returns. | ||
| 175 | +pub(crate) fn arm_reference_select(on: bool) { | ||
| 176 | + REFERENCE_SELECT.with(|c| c.set(on)); | ||
| 177 | +} | ||
| 178 | + | ||
| 179 | +/// The compound half of a block's end-of-block neighbour record: nothing for | ||
| 180 | +/// a single-reference or intra block, and `record_compound_mi`'s overwrite of | ||
| 181 | +/// the ALTREF default for a compound one (this writer only codes | ||
| 182 | +/// `comp_group_idx == 0`, `compound_idx == 1`). | ||
| 183 | +fn record_block_compound(neighbours: &mut Neighbours, at_mi: (usize, usize), side: usize, block: &BlockCoeffs) { | ||
| 184 | + if let Some(r1) = block.inter.and_then(|i| i.ref1) { | ||
| 185 | + neighbours.record_compound_mi(at_mi, side, r1, 0, 1); | ||
| 186 | + } | ||
| 187 | +} | ||
| 188 | + | ||
| 189 | +/// decode.rs' own `is_uni_comp_ref`: a pair is `UNIDIR_COMP_REFERENCE` | ||
| 190 | +/// exactly when both references sit on the same temporal side. | ||
| 191 | +fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { | ||
| 192 | + let backward = | ||
| 193 | + |r: i8| (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&r); | ||
| 194 | + backward(ref0) == backward(ref1) | ||
| 195 | +} | ||
| 196 | + | ||
| 197 | +thread_local! { | ||
| 198 | + /// `(order_hint_bits, this frame's order hint, each reference's order | ||
| 199 | + /// hint)` -- what `get_comp_index_context` (decode.rs) needs to context | ||
| 200 | + /// the `compound_idx` symbol, armed the same way the sign-bias table is. | ||
| 201 | + static ORDER_HINTS: std::cell::Cell<(u32, u32, [u32; 7])> = | ||
| 202 | + const { std::cell::Cell::new((7, 0, [0; 7])) }; | ||
| 203 | +} | ||
| 204 | + | ||
| 205 | +/// Arms this frame's order hints for the next tile written on this thread. | ||
| 206 | +pub(crate) fn arm_order_hints(bits: u32, order_hint: u32, hints: [u32; 7]) { | ||
| 207 | + ORDER_HINTS.with(|c| c.set((bits, order_hint, hints))); | ||
| 208 | +} | ||
| 209 | + | ||
| 210 | +/// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec | ||
| 211 | +/// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref` | ||
| 212 | +/// trees), symbol for symbol at the same contexts -- which are, as that | ||
| 213 | +/// function's own doc records, `single_ref_p1_ctx`..`single_ref_p6_ctx`/ | ||
| 214 | +/// `uni_comp_ref_p1_ctx` under libaom's compound names. | ||
| 215 | +/// | ||
| 216 | +/// # Errors | ||
| 217 | +/// A pair this writer never forms (its `ref0` is not `LAST_FRAME`). | ||
| 218 | +fn write_compound_ref_frames( | ||
| 219 | + enc: &mut SymbolEncoder, | ||
| 220 | + cdfs: &mut Cdfs, | ||
| 221 | + above: Option<crate::mvstack::NeighbourRef>, | ||
| 222 | + left: Option<crate::mvstack::NeighbourRef>, | ||
| 223 | + above_ref: i8, | ||
| 224 | + left_ref: i8, | ||
| 225 | + (ref0, ref1): (i8, i8), | ||
| 226 | +) -> Result<()> { | ||
| 227 | + use crate::mvstack::{ | ||
| 228 | + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, | ||
| 229 | + LAST_FRAME, comp_reference_type_ctx, single_ref_p1_ctx, single_ref_p2_ctx, | ||
| 230 | + single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, | ||
| 231 | + uni_comp_ref_p1_ctx, | ||
| 232 | + }; | ||
| 233 | + let a = (above_ref > 0).then_some(above_ref); | ||
| 234 | + let l = (left_ref > 0).then_some(left_ref); | ||
| 235 | + let a1 = above.and_then(|n| n.ref1); | ||
| 236 | + let l1 = left.and_then(|n| n.ref1); | ||
| 237 | + let unidir = is_uni_comp_ref(ref0, ref1); | ||
| 238 | + enc.symbol( | ||
| 239 | + usize::from(!unidir), | ||
| 240 | + &mut cdfs.comp_ref_type[comp_reference_type_ctx(above, left)], | ||
| 241 | + ); | ||
| 242 | + if unidir { | ||
| 243 | + let both_backward = (ref0, ref1) == (BWDREF_FRAME, ALTREF_FRAME); | ||
| 244 | + enc.symbol( | ||
| 245 | + usize::from(both_backward), | ||
| 246 | + &mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0], | ||
| 247 | + ); | ||
| 248 | + if both_backward { | ||
| 249 | + return Ok(()); | ||
| 250 | + } | ||
| 251 | + if ref0 != LAST_FRAME { | ||
| 252 | + return Err(Error::unsupported( | ||
| 253 | + "AV1 tile", | ||
| 254 | + "a unidirectional compound pair this writer does not form", | ||
| 255 | + )); | ||
| 256 | + } | ||
| 257 | + let past_last2 = ref1 != LAST2_FRAME; | ||
| 258 | + enc.symbol( | ||
| 259 | + usize::from(past_last2), | ||
| 260 | + &mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1], | ||
| 261 | + ); | ||
| 262 | + if past_last2 { | ||
| 263 | + enc.symbol( | ||
| 264 | + usize::from(ref1 == GOLDEN_FRAME), | ||
| 265 | + &mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2], | ||
| 266 | + ); | ||
| 267 | + } | ||
| 268 | + return Ok(()); | ||
| 269 | + } | ||
| 270 | + let far = ref0 == LAST3_FRAME || ref0 == GOLDEN_FRAME; | ||
| 271 | + enc.symbol( | ||
| 272 | + usize::from(far), | ||
| 273 | + &mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0], | ||
| 274 | + ); | ||
| 275 | + if far { | ||
| 276 | + enc.symbol( | ||
| 277 | + usize::from(ref0 == GOLDEN_FRAME), | ||
| 278 | + &mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2], | ||
| 279 | + ); | ||
| 280 | + } else { | ||
| 281 | + enc.symbol( | ||
| 282 | + usize::from(ref0 == LAST2_FRAME), | ||
| 283 | + &mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1], | ||
| 284 | + ); | ||
| 285 | + } | ||
| 286 | + let is_altref = ref1 == ALTREF_FRAME; | ||
| 287 | + enc.symbol( | ||
| 288 | + usize::from(is_altref), | ||
| 289 | + &mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0], | ||
| 290 | + ); | ||
| 291 | + if !is_altref { | ||
| 292 | + enc.symbol( | ||
| 293 | + usize::from(ref1 == ALTREF2_FRAME), | ||
| 294 | + &mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1], | ||
| 295 | + ); | ||
| 296 | + } | ||
| 297 | + Ok(()) | ||
| 298 | +} | ||
| 299 | + | ||
| 300 | +/// Per-mode and per-pair fire counts of [`write_compound_block`], for the | ||
| 301 | +/// gate's own census (gate-blind-to-feature): modes indexed by the spec's | ||
| 302 | +/// `INTER_COMPOUND_MODES`, pairs by `ref1` (`LAST`..`ALTREF`). | ||
| 303 | +static COMPOUND_MODE_HITS: [std::sync::atomic::AtomicUsize; 8] = | ||
| 304 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 8]; | ||
| 305 | +static COMPOUND_PAIR_HITS: [std::sync::atomic::AtomicUsize; 7] = | ||
| 306 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 7]; | ||
| 307 | + | ||
| 308 | +/// Takes and clears the compound mode histogram. | ||
| 309 | +#[cfg(test)] | ||
| 310 | +pub(crate) fn take_compound_mode_hits() -> [usize; 8] { | ||
| 311 | + std::array::from_fn(|i| COMPOUND_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 312 | +} | ||
| 313 | + | ||
| 314 | +/// Takes and clears the compound pair histogram, indexed by the SECOND | ||
| 315 | +/// reference (`LAST` = 0 .. `ALTREF` = 6). | ||
| 316 | +#[cfg(test)] | ||
| 317 | +pub(crate) fn take_compound_pair_hits() -> [usize; 7] { | ||
| 318 | + std::array::from_fn(|i| COMPOUND_PAIR_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 319 | +} | ||
| 320 | + | ||
| 321 | +/// Writes one COMPOUND_REFERENCE block's whole mode chain -- the writer side | ||
| 322 | +/// of decode.rs' `read_compound_ref_frames` / `read_inter_compound_mode` / | ||
| 323 | +/// `assign_compound_mv` / `comp_group_idx` / `compound_idx` sequence, in that | ||
| 324 | +/// order. Returns the two motion vectors the decoder derives (what the MI | ||
| 325 | +/// grid must record) and whether the mode was a `NEW*` one. | ||
| 326 | +/// | ||
| 327 | +/// Only the three modes [`InterMode::compound_index`] names are written, and | ||
| 328 | +/// only the `comp_group_idx == 0`, `compound_idx == 1` blend (spec | ||
| 329 | +/// `COMPOUND_AVERAGE`) -- masked compound and the distance-weighted blend are | ||
| 330 | +/// not searched. | ||
| 331 | +/// | ||
| 332 | +/// # Errors | ||
| 333 | +/// As [`write_mv`], or a pair/mode this writer does not form. | ||
| 334 | +#[allow(clippy::too_many_arguments)] | ||
| 335 | +fn write_compound_block( | ||
| 336 | + enc: &mut SymbolEncoder, | ||
| 337 | + cdfs: &mut Cdfs, | ||
| 338 | + neighbours: &Neighbours, | ||
| 339 | + grid: &crate::mvstack::MiGrid, | ||
| 340 | + (mi_row, mi_col): (usize, usize), | ||
| 341 | + (bw4, bh4): (usize, usize), | ||
| 342 | + (has_above, has_left): (bool, bool), | ||
| 343 | + info: InterInfo, | ||
| 344 | + (mi_cols, mi_rows): (usize, usize), | ||
| 345 | +) -> Result<((i32, i32), (i32, i32), bool)> { | ||
| 346 | + let (Some(ref1), Some(mode)) = (info.ref1, info.mode.compound_index()) else { | ||
| 347 | + return Err(Error::unsupported( | ||
| 348 | + "AV1 tile", | ||
| 349 | + "a compound block needs a second reference and a compound mode", | ||
| 350 | + )); | ||
| 351 | + }; | ||
| 352 | + let above = has_above.then(|| neighbours.above_nbr(mi_col)); | ||
| 353 | + let left = has_left.then(|| neighbours.left_nbr(mi_row)); | ||
| 354 | + write_compound_ref_frames( | ||
| 355 | + enc, | ||
| 356 | + cdfs, | ||
| 357 | + above, | ||
| 358 | + left, | ||
| 359 | + neighbours.above_ref[mi_col], | ||
| 360 | + neighbours.left_ref[mi_row], | ||
| 361 | + (info.ref_frame, ref1), | ||
| 362 | + )?; | ||
| 363 | + let stack = crate::mvstack::find_mv_stack_compound( | ||
| 364 | + grid, | ||
| 365 | + mi_row, | ||
| 366 | + mi_col, | ||
| 367 | + bw4, | ||
| 368 | + bh4, | ||
| 369 | + (info.ref_frame, ref1), | ||
| 370 | + mi_cols, | ||
| 371 | + mi_rows, | ||
| 372 | + grid.sign_bias_table(), | ||
| 373 | + &[(0, 0); 7], | ||
| 374 | + None, | ||
| 375 | + ); | ||
| 376 | + let ctx = crate::cdf::COMPOUND_MODE_CTX_MAP[stack.ref_mv_ctx >> 1][stack.new_mv_ctx.min(4)]; | ||
| 377 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 378 | + eprintln!( | ||
| 379 | + "EC_WCOMP mi_row={mi_row} mi_col={mi_col} mode={mode} ref0={} ref1={ref1} ctx={ctx} new_mv_ctx={} ref_mv_ctx={} stack={}", | ||
| 380 | + info.ref_frame, stack.new_mv_ctx, stack.ref_mv_ctx, stack.entries.len() | ||
| 381 | + ); | ||
| 382 | + } | ||
| 383 | + enc.symbol(mode, &mut cdfs.inter_compound_mode[ctx]); | ||
| 384 | + // `assign_compound_mv`'s own DRL half: `NEW_NEWMV` walks from index 0, | ||
| 385 | + // the two derived modes code no index at all. | ||
| 386 | + let mvs = match info.mode { | ||
| 387 | + InterMode::NearestNearestMv => stack.nearest_mv, | ||
| 388 | + InterMode::GlobalGlobalMv => ((0, 0), (0, 0)), | ||
| 389 | + _ => { | ||
| 390 | + let idx = usize::from(info.ref_mv_idx); | ||
| 391 | + let mut walk = 0usize; | ||
| 392 | + while walk < 2 && stack.entries.len() > walk + 1 { | ||
| 393 | + let advance = walk < idx; | ||
| 394 | + enc.symbol(usize::from(advance), &mut cdfs.drl_mode[stack.drl_ctx[walk]]); | ||
| 395 | + if !advance { | ||
| 396 | + break; | ||
| 397 | + } | ||
| 398 | + walk += 1; | ||
| 399 | + } | ||
| 400 | + let base = stack | ||
| 401 | + .entries | ||
| 402 | + .get(idx) | ||
| 403 | + .map_or(stack.nearest_mv, |e| (e.mv0, e.mv1)); | ||
| 404 | + write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv, base.0)?; | ||
| 405 | + write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv1, base.1)?; | ||
| 406 | + (info.mv, info.mv1) | ||
| 407 | + } | ||
| 408 | + }; | ||
| 409 | + // spec 5.11.25's `comp_group_idx`/`compound_idx` are NOT coded: this | ||
| 410 | + // crate's sequence header carries `enable_masked_compound = false` and | ||
| 411 | + // `enable_jnt_comp = false` (encode.rs), and decode.rs reads neither | ||
| 412 | + // symbol under those bits -- it infers `comp_group_idx = 0`, | ||
| 413 | + // `compound_idx = 1` (the simple average), which is the only blend this | ||
| 414 | + // writer forms anyway. A lane that turns either sequence bit on has to | ||
| 415 | + // write the matching symbol here, at | ||
| 416 | + // `get_comp_group_idx_context`/`get_comp_index_context`'s contexts (which | ||
| 417 | + // is what `Neighbours::above_comp_group_idx`/`above_compound_idx` and the | ||
| 418 | + // armed order hints below are already kept for). | ||
| 419 | + let _ = ORDER_HINTS.with(std::cell::Cell::get); | ||
| 420 | + COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 421 | + COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7] | ||
| 422 | + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 423 | + Ok((mvs.0, mvs.1, matches!(info.mode, InterMode::NewNewMv))) | ||
| 424 | +} | ||
| 425 | + | ||
| 426 | +/// Writer-side counterpart of decode.rs `read_comp_mode` (spec 5.11.25's | ||
| 427 | +/// `comp_mode`): on a `reference_select` frame every inter block whose size | ||
| 428 | +/// allows compound (`is_comp_ref_allowed`, always true here -- this writer's | ||
| 429 | +/// smallest leaf is 8x8) names SINGLE or COMPOUND, at | ||
| 430 | +/// `mvstack::reference_mode_ctx`'s own context off the same two neighbour | ||
| 431 | +/// bands decode.rs builds its `NeighbourRef` pair from. | ||
| 432 | +fn write_comp_mode( | ||
| 433 | + enc: &mut SymbolEncoder, | ||
| 434 | + cdfs: &mut Cdfs, | ||
| 435 | + neighbours: &Neighbours, | ||
| 436 | + (mi_r, mi_c): (usize, usize), | ||
| 437 | + (has_above, has_left): (bool, bool), | ||
| 438 | + compound: bool, | ||
| 439 | +) { | ||
| 440 | + if !REFERENCE_SELECT.with(std::cell::Cell::get) { | ||
| 441 | + return; | ||
| 442 | + } | ||
| 443 | + let above = has_above.then(|| neighbours.above_nbr(mi_c)); | ||
| 444 | + let left = has_left.then(|| neighbours.left_nbr(mi_r)); | ||
| 445 | + let ctx = crate::mvstack::reference_mode_ctx(above, left); | ||
| 446 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 447 | + eprintln!( | ||
| 448 | + "EC_WCM mi=({mi_r},{mi_c}) ctx={ctx} val={} a={:?} l={:?}", | ||
| 449 | + usize::from(compound), | ||
| 450 | + above.map(|n| (n.is_inter, n.ref0, n.ref1)), | ||
| 451 | + left.map(|n| (n.is_inter, n.ref0, n.ref1)) | ||
| 452 | + ); | ||
| 453 | + } | ||
| 454 | + enc.symbol(usize::from(compound), &mut cdfs.comp_mode[ctx]); | ||
| 455 | +} | ||
| 456 | + | ||
| 163 | /// Clears the armed plan when the tile writer that consumed it returns. | 457 | /// Clears the armed plan when the tile writer that consumed it returns. |
| 164 | struct CdefIdxGuard; | 458 | struct CdefIdxGuard; |
| 165 | 459 | ||
| @@ -168,6 +462,8 @@ impl Drop for CdefIdxGuard { | |||
| 168 | CDEF_IDX.with(|c| *c.borrow_mut() = None); | 462 | CDEF_IDX.with(|c| *c.borrow_mut() = None); |
| 169 | LR_PLAN.with(|c| *c.borrow_mut() = None); | 463 | LR_PLAN.with(|c| *c.borrow_mut() = None); |
| 170 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); | 464 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); |
| 465 | + REFERENCE_SELECT.with(|c| c.set(false)); | ||
| 466 | + ORDER_HINTS.with(|c| c.set((7, 0, [0; 7]))); | ||
| 171 | } | 467 | } |
| 172 | } | 468 | } |
| 173 | 469 | ||
| @@ -678,6 +974,29 @@ pub enum InterMode { | |||
| 678 | /// `NEWMV`: codes `mv` as a residual against the stack entry | 974 | /// `NEWMV`: codes `mv` as a residual against the stack entry |
| 679 | /// [`InterInfo::ref_mv_idx`] names. | 975 | /// [`InterInfo::ref_mv_idx`] names. |
| 680 | NewMv, | 976 | NewMv, |
| 977 | + /// `NEAREST_NEARESTMV`: both sides take the compound stack's own nearest | ||
| 978 | + /// pair, no residual, no DRL. | ||
| 979 | + NearestNearestMv, | ||
| 980 | + /// `GLOBAL_GLOBALMV`: both sides take the identity global model's zero | ||
| 981 | + /// vector. | ||
| 982 | + GlobalGlobalMv, | ||
| 983 | + /// `NEW_NEWMV`: both sides code a residual against the compound stack | ||
| 984 | + /// entry [`InterInfo::ref_mv_idx`] names. | ||
| 985 | + NewNewMv, | ||
| 986 | +} | ||
| 987 | + | ||
| 988 | +impl InterMode { | ||
| 989 | + /// The spec's `INTER_COMPOUND_MODES` index (libaom `enums.h` order, | ||
| 990 | + /// `NEAREST_NEARESTMV = 0`..`NEW_NEWMV = 7`) for a compound mode, `None` | ||
| 991 | + /// for a single-reference one. | ||
| 992 | + fn compound_index(self) -> Option<usize> { | ||
| 993 | + match self { | ||
| 994 | + InterMode::NearestNearestMv => Some(0), | ||
| 995 | + InterMode::GlobalGlobalMv => Some(6), | ||
| 996 | + InterMode::NewNewMv => Some(7), | ||
| 997 | + _ => None, | ||
| 998 | + } | ||
| 999 | + } | ||
| 681 | } | 1000 | } |
| 682 | 1001 | ||
| 683 | /// An inter-coded block's mode and motion vector, `mv` in the spec's 1/8-pel | 1002 | /// An inter-coded block's mode and motion vector, `mv` in the spec's 1/8-pel |
| @@ -698,6 +1017,13 @@ pub struct InterInfo { | |||
| 698 | /// `NEARMV` takes outright. Ignored by the two modes that code no DRL | 1017 | /// `NEARMV` takes outright. Ignored by the two modes that code no DRL |
| 699 | /// index (`NEARESTMV`, `GLOBALMV`). | 1018 | /// index (`NEARESTMV`, `GLOBALMV`). |
| 700 | pub ref_mv_idx: u8, | 1019 | pub ref_mv_idx: u8, |
| 1020 | + /// The block's second reference (spec `RefFrames[1]`), `None` for a | ||
| 1021 | + /// single-reference block. `Some` only on a `reference_select` frame, and | ||
| 1022 | + /// only beside one of [`InterMode`]'s compound modes. | ||
| 1023 | + pub ref1: Option<i8>, | ||
| 1024 | + /// The motion vector into `ref1`, for the compound modes that code one | ||
| 1025 | + /// (`NEW_NEWMV`); the other two derive both sides. | ||
| 1026 | + pub mv1: (i32, i32), | ||
| 701 | } | 1027 | } |
| 702 | 1028 | ||
| 703 | impl From<Vec<Coeff>> for BlockCoeffs { | 1029 | impl From<Vec<Coeff>> for BlockCoeffs { |
| @@ -869,6 +1195,23 @@ struct Neighbours { | |||
| 869 | above_ref: Vec<i8>, | 1195 | above_ref: Vec<i8>, |
| 870 | /// The same, down the left edge. | 1196 | /// The same, down the left edge. |
| 871 | left_ref: Vec<i8>, | 1197 | left_ref: Vec<i8>, |
| 1198 | + /// Each neighbour's SECOND reference, `None` for a single-reference or | ||
| 1199 | + /// intra one -- decode.rs `Neighbours::above_ref1`, read by every compound | ||
| 1200 | + /// context function (`reference_mode_ctx`, `comp_reference_type_ctx`, the | ||
| 1201 | + /// `comp_ref`/`comp_bwdref` votes). | ||
| 1202 | + above_ref1: Vec<Option<i8>>, | ||
| 1203 | + /// The same, down the left edge. | ||
| 1204 | + left_ref1: Vec<Option<i8>>, | ||
| 1205 | + /// What decode.rs `Neighbours::above_comp_group_idx` holds: a compound | ||
| 1206 | + /// neighbour's own `comp_group_idx`, or libaom's single-reference special | ||
| 1207 | + /// case (`3` for an ALTREF block, `0` otherwise). | ||
| 1208 | + above_comp_group_idx: Vec<u8>, | ||
| 1209 | + /// The same, down the left edge. | ||
| 1210 | + left_comp_group_idx: Vec<u8>, | ||
| 1211 | + /// The same for `compound_idx` (`1` for a single-ref ALTREF block). | ||
| 1212 | + above_compound_idx: Vec<u8>, | ||
| 1213 | + /// The same, down the left edge. | ||
| 1214 | + left_compound_idx: Vec<u8>, | ||
| 872 | /// The frame's true (unpadded) width and height, in 4x4 mode-info units, | 1215 | /// The frame's true (unpadded) width and height, in 4x4 mode-info units, |
| 873 | /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec | 1216 | /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec |
| 874 | /// `av1_set_entropy_contexts`): a block whose row or column run spills | 1217 | /// `av1_set_entropy_contexts`): a block whose row or column run spills |
| @@ -914,6 +1257,12 @@ impl Neighbours { | |||
| 914 | left_inter: vec![false; rows * (SUB / MI)], | 1257 | left_inter: vec![false; rows * (SUB / MI)], |
| 915 | above_ref: vec![-1; cols * (SUB / MI)], | 1258 | above_ref: vec![-1; cols * (SUB / MI)], |
| 916 | left_ref: vec![-1; rows * (SUB / MI)], | 1259 | left_ref: vec![-1; rows * (SUB / MI)], |
| 1260 | + above_ref1: vec![None; cols * (SUB / MI)], | ||
| 1261 | + left_ref1: vec![None; rows * (SUB / MI)], | ||
| 1262 | + above_comp_group_idx: vec![0; cols * (SUB / MI)], | ||
| 1263 | + left_comp_group_idx: vec![0; rows * (SUB / MI)], | ||
| 1264 | + above_compound_idx: vec![0; cols * (SUB / MI)], | ||
| 1265 | + left_compound_idx: vec![0; rows * (SUB / MI)], | ||
| 917 | above_tx: vec![0; cols * (SUB / MI)], | 1266 | above_tx: vec![0; cols * (SUB / MI)], |
| 918 | left_tx: vec![0; rows * (SUB / MI)], | 1267 | left_tx: vec![0; rows * (SUB / MI)], |
| 919 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], | 1268 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| @@ -932,6 +1281,9 @@ impl Neighbours { | |||
| 932 | self.left_skip.iter_mut().for_each(|s| *s = false); | 1281 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 933 | self.left_inter.iter_mut().for_each(|i| *i = false); | 1282 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 934 | self.left_ref.iter_mut().for_each(|r| *r = -1); | 1283 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 1284 | + self.left_ref1.iter_mut().for_each(|r| *r = None); | ||
| 1285 | + self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); | ||
| 1286 | + self.left_compound_idx.iter_mut().for_each(|r| *r = 0); | ||
| 935 | self.left_tx.iter_mut().for_each(|t| *t = 0); | 1287 | self.left_tx.iter_mut().for_each(|t| *t = 0); |
| 936 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); | 1288 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 937 | } | 1289 | } |
| @@ -1136,6 +1488,29 @@ impl Neighbours { | |||
| 1136 | self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame); | 1488 | self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame); |
| 1137 | } | 1489 | } |
| 1138 | 1490 | ||
| 1491 | + /// [`Self::record_inter_mi`]'s compound half (decode.rs | ||
| 1492 | + /// `Neighbours::record_compound_ctx_rect_mi`): overwrites the ALTREF | ||
| 1493 | + /// single-reference default with a real compound block's own second | ||
| 1494 | + /// reference and `comp_group_idx`/`compound_idx` bits, called right after | ||
| 1495 | + /// it for every compound-coded block. | ||
| 1496 | + fn record_compound_mi( | ||
| 1497 | + &mut self, | ||
| 1498 | + (mi_r, mi_c): (usize, usize), | ||
| 1499 | + side: usize, | ||
| 1500 | + ref1: i8, | ||
| 1501 | + comp_group_idx: u8, | ||
| 1502 | + compound_idx: u8, | ||
| 1503 | + ) { | ||
| 1504 | + for cell in 0..side / MI { | ||
| 1505 | + self.above_ref1[mi_c + cell] = Some(ref1); | ||
| 1506 | + self.left_ref1[mi_r + cell] = Some(ref1); | ||
| 1507 | + self.above_comp_group_idx[mi_c + cell] = comp_group_idx; | ||
| 1508 | + self.left_comp_group_idx[mi_r + cell] = comp_group_idx; | ||
| 1509 | + self.above_compound_idx[mi_c + cell] = compound_idx; | ||
| 1510 | + self.left_compound_idx[mi_r + cell] = compound_idx; | ||
| 1511 | + } | ||
| 1512 | + } | ||
| 1513 | + | ||
| 1139 | /// [`Self::record_inter`] taking the block's position directly in 4x4 | 1514 | /// [`Self::record_inter`] taking the block's position directly in 4x4 |
| 1140 | /// mode-info units, for a block finer than one [`SUB`] slot. | 1515 | /// mode-info units, for a block finer than one [`SUB`] slot. |
| 1141 | fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) { | 1516 | fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) { |
| @@ -1146,6 +1521,45 @@ impl Neighbours { | |||
| 1146 | self.left_inter[mi_r + cell] = is_inter; | 1521 | self.left_inter[mi_r + cell] = is_inter; |
| 1147 | self.above_ref[mi_c + cell] = ref_frame; | 1522 | self.above_ref[mi_c + cell] = ref_frame; |
| 1148 | self.left_ref[mi_r + cell] = ref_frame; | 1523 | self.left_ref[mi_r + cell] = ref_frame; |
| 1524 | + // decode.rs `record_inter`'s single-reference default: no second | ||
| 1525 | + // reference, and libaom's ALTREF special case in the two compound | ||
| 1526 | + // context bands. `record_compound_mi` overwrites both for a real | ||
| 1527 | + // compound block. | ||
| 1528 | + let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; | ||
| 1529 | + self.above_ref1[mi_c + cell] = None; | ||
| 1530 | + self.left_ref1[mi_r + cell] = None; | ||
| 1531 | + self.above_comp_group_idx[mi_c + cell] = if altref { 3 } else { 0 }; | ||
| 1532 | + self.left_comp_group_idx[mi_r + cell] = if altref { 3 } else { 0 }; | ||
| 1533 | + self.above_compound_idx[mi_c + cell] = u8::from(altref); | ||
| 1534 | + self.left_compound_idx[mi_r + cell] = u8::from(altref); | ||
| 1535 | + } | ||
| 1536 | + } | ||
| 1537 | + | ||
| 1538 | + /// The above neighbour as decode.rs' own `NeighbourRef`, for every | ||
| 1539 | + /// compound context function (`reference_mode_ctx`, | ||
| 1540 | + /// `comp_reference_type_ctx`, the `comp_ref`/`comp_bwdref` votes). | ||
| 1541 | + fn above_nbr(&self, mi_c: usize) -> crate::mvstack::NeighbourRef { | ||
| 1542 | + let is_inter = self.above_inter[mi_c]; | ||
| 1543 | + let ref0 = if is_inter { self.above_ref[mi_c] } else { 0 }; | ||
| 1544 | + let ref1 = self.above_ref1[mi_c]; | ||
| 1545 | + crate::mvstack::NeighbourRef { | ||
| 1546 | + is_inter, | ||
| 1547 | + ref0, | ||
| 1548 | + ref1, | ||
| 1549 | + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), | ||
| 1550 | + } | ||
| 1551 | + } | ||
| 1552 | + | ||
| 1553 | + /// [`Self::above_nbr`], down the left edge. | ||
| 1554 | + fn left_nbr(&self, mi_r: usize) -> crate::mvstack::NeighbourRef { | ||
| 1555 | + let is_inter = self.left_inter[mi_r]; | ||
| 1556 | + let ref0 = if is_inter { self.left_ref[mi_r] } else { 0 }; | ||
| 1557 | + let ref1 = self.left_ref1[mi_r]; | ||
| 1558 | + crate::mvstack::NeighbourRef { | ||
| 1559 | + is_inter, | ||
| 1560 | + ref0, | ||
| 1561 | + ref1, | ||
| 1562 | + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), | ||
| 1149 | } | 1563 | } |
| 1150 | } | 1564 | } |
| 1151 | 1565 | ||
| @@ -2934,7 +3348,9 @@ fn write_single_ref( | |||
| 2934 | cdfs: &mut Cdfs, | 3348 | cdfs: &mut Cdfs, |
| 2935 | ref_frame: i8, | 3349 | ref_frame: i8, |
| 2936 | above_ref: i8, | 3350 | above_ref: i8, |
| 3351 | + above_ref1: Option<i8>, | ||
| 2937 | left_ref: i8, | 3352 | left_ref: i8, |
| 3353 | + left_ref1: Option<i8>, | ||
| 2938 | ) { | 3354 | ) { |
| 2939 | REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 3355 | REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 2940 | use crate::mvstack::{ | 3356 | use crate::mvstack::{ |
| @@ -2944,21 +3360,26 @@ fn write_single_ref( | |||
| 2944 | }; | 3360 | }; |
| 2945 | let above = (above_ref > 0).then_some(above_ref); | 3361 | let above = (above_ref > 0).then_some(above_ref); |
| 2946 | let left = (left_ref > 0).then_some(left_ref); | 3362 | let left = (left_ref > 0).then_some(left_ref); |
| 3363 | + // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound | ||
| 3364 | + // neighbour's references (decode.rs `read_single_ref`'s own | ||
| 3365 | + // `above_ref1`/`left_ref1`): dropping the second one reads a different | ||
| 3366 | + // context row for every block sitting under a compound neighbour, and so | ||
| 3367 | + // decodes a different reference NAME. | ||
| 2947 | let backward = ref_frame >= BWDREF_FRAME; | 3368 | let backward = ref_frame >= BWDREF_FRAME; |
| 2948 | enc.symbol( | 3369 | enc.symbol( |
| 2949 | usize::from(backward), | 3370 | usize::from(backward), |
| 2950 | - &mut cdfs.single_ref[single_ref_p1_ctx(above, None, left, None)][0], | 3371 | + &mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0], |
| 2951 | ); | 3372 | ); |
| 2952 | if backward { | 3373 | if backward { |
| 2953 | let is_altref = ref_frame == ALTREF_FRAME; | 3374 | let is_altref = ref_frame == ALTREF_FRAME; |
| 2954 | enc.symbol( | 3375 | enc.symbol( |
| 2955 | usize::from(is_altref), | 3376 | usize::from(is_altref), |
| 2956 | - &mut cdfs.single_ref[single_ref_p2_ctx(above, None, left, None)][1], | 3377 | + &mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1], |
| 2957 | ); | 3378 | ); |
| 2958 | if !is_altref { | 3379 | if !is_altref { |
| 2959 | enc.symbol( | 3380 | enc.symbol( |
| 2960 | usize::from(ref_frame == ALTREF2_FRAME), | 3381 | usize::from(ref_frame == ALTREF2_FRAME), |
| 2961 | - &mut cdfs.single_ref[single_ref_p6_ctx(above, None, left, None)][5], | 3382 | + &mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5], |
| 2962 | ); | 3383 | ); |
| 2963 | } | 3384 | } |
| 2964 | return; | 3385 | return; |
| @@ -2966,17 +3387,17 @@ fn write_single_ref( | |||
| 2966 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; | 3387 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; |
| 2967 | enc.symbol( | 3388 | enc.symbol( |
| 2968 | usize::from(far), | 3389 | usize::from(far), |
| 2969 | - &mut cdfs.single_ref[single_ref_p3_ctx(above, None, left, None)][2], | 3390 | + &mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2], |
| 2970 | ); | 3391 | ); |
| 2971 | if far { | 3392 | if far { |
| 2972 | enc.symbol( | 3393 | enc.symbol( |
| 2973 | usize::from(ref_frame == GOLDEN_FRAME), | 3394 | usize::from(ref_frame == GOLDEN_FRAME), |
| 2974 | - &mut cdfs.single_ref[single_ref_p5_ctx(above, None, left, None)][4], | 3395 | + &mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4], |
| 2975 | ); | 3396 | ); |
| 2976 | } else { | 3397 | } else { |
| 2977 | enc.symbol( | 3398 | enc.symbol( |
| 2978 | usize::from(ref_frame == LAST2_FRAME), | 3399 | usize::from(ref_frame == LAST2_FRAME), |
| 2979 | - &mut cdfs.single_ref[single_ref_p4_ctx(above, None, left, None)][3], | 3400 | + &mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3], |
| 2980 | ); | 3401 | ); |
| 2981 | } | 3402 | } |
| 2982 | } | 3403 | } |
| @@ -3080,6 +3501,12 @@ fn write_inter_mode( | |||
| 3080 | stack: &crate::mvstack::MvStack, | 3501 | stack: &crate::mvstack::MvStack, |
| 3081 | ) -> Result<((i32, i32), bool)> { | 3502 | ) -> Result<((i32, i32), bool)> { |
| 3082 | let idx = usize::from(info.ref_mv_idx); | 3503 | let idx = usize::from(info.ref_mv_idx); |
| 3504 | + if info.mode.compound_index().is_some() { | ||
| 3505 | + return Err(Error::unsupported( | ||
| 3506 | + "AV1 tile", | ||
| 3507 | + "a compound mode is written by `write_compound_block`, not here", | ||
| 3508 | + )); | ||
| 3509 | + } | ||
| 3083 | match info.mode { | 3510 | match info.mode { |
| 3084 | InterMode::NewMv => { | 3511 | InterMode::NewMv => { |
| 3085 | enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); | 3512 | enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); |
| @@ -3113,6 +3540,7 @@ fn write_inter_mode( | |||
| 3113 | note_inter_mode(1, idx); | 3540 | note_inter_mode(1, idx); |
| 3114 | Ok((mv, false)) | 3541 | Ok((mv, false)) |
| 3115 | } | 3542 | } |
| 3543 | + _ => unreachable!("compound modes are refused above"), | ||
| 3116 | } | 3544 | } |
| 3117 | } | 3545 | } |
| 3118 | 3546 | ||
| @@ -3514,9 +3942,33 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 3514 | 3942 | ||
| 3515 | let mode_for_tx; | 3943 | let mode_for_tx; |
| 3516 | if let Some(info) = block.inter { | 3944 | if let Some(info) = block.inter { |
| 3517 | - write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | 3945 | + write_comp_mode(&mut enc, &mut cdfs, &neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some()); |
| 3518 | - | ||
| 3519 | let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); | 3946 | let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); |
| 3947 | + if info.ref1.is_some() { | ||
| 3948 | + let (mv, mv1, is_new_mv) = write_compound_block( | ||
| 3949 | + &mut enc, &mut cdfs, &neighbours, &grid, (mi_row, mi_col), (8, 8), | ||
| 3950 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | ||
| 3951 | + )?; | ||
| 3952 | + for dr in 0..8 { | ||
| 3953 | + for dc in 0..8 { | ||
| 3954 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | ||
| 3955 | + is_inter: true, | ||
| 3956 | + ref_frame: info.ref_frame, | ||
| 3957 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | ||
| 3958 | + mv1: mv16(mv1), | ||
| 3959 | + mv: mv16(mv), | ||
| 3960 | + is_new_mv, | ||
| 3961 | + size: 8, | ||
| 3962 | + size_h: 8, | ||
| 3963 | + is_global_mv0: false, | ||
| 3964 | + is_global_mv1: false, | ||
| 3965 | + }); | ||
| 3966 | + } | ||
| 3967 | + } | ||
| 3968 | + mode_for_tx = 0; | ||
| 3969 | + } else { | ||
| 3970 | + write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]); | ||
| 3971 | + | ||
| 3520 | let stack = find_mv_stack( | 3972 | let stack = find_mv_stack( |
| 3521 | &grid, | 3973 | &grid, |
| 3522 | mi_row, | 3974 | mi_row, |
| @@ -3569,6 +4021,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 3569 | } | 4021 | } |
| 3570 | } | 4022 | } |
| 3571 | mode_for_tx = 0; | 4023 | mode_for_tx = 0; |
| 4024 | + } | ||
| 3572 | } else { | 4025 | } else { |
| 3573 | let mode = usize::from(block.mode); | 4026 | let mode = usize::from(block.mode); |
| 3574 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]); | 4027 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]); |
| @@ -3661,6 +4114,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 3661 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); | 4114 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); |
| 3662 | } | 4115 | } |
| 3663 | neighbours.record_inter(at, BLOCK, block.skip, is_inter, block_ref(block)); | 4116 | neighbours.record_inter(at, BLOCK, block.skip, is_inter, block_ref(block)); |
| 4117 | + record_block_compound(&mut neighbours, (mi_r, mi_c), BLOCK, block); | ||
| 3664 | } | 4118 | } |
| 3665 | } | 4119 | } |
| 3666 | } | 4120 | } |
| @@ -3719,9 +4173,34 @@ fn write_inter_frame_leaf( | |||
| 3719 | 4173 | ||
| 3720 | let mode_for_tx; | 4174 | let mode_for_tx; |
| 3721 | if let Some(info) = block.inter { | 4175 | if let Some(info) = block.inter { |
| 3722 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | 4176 | + write_comp_mode(enc, cdfs, neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some()); |
| 3723 | - | ||
| 3724 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | 4177 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 4178 | + if info.ref1.is_some() { | ||
| 4179 | + let n = SUB_MI as usize; | ||
| 4180 | + let (mv, mv1, is_new_mv) = write_compound_block( | ||
| 4181 | + enc, cdfs, neighbours, grid, (mi_row, mi_col), (n, n), | ||
| 4182 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | ||
| 4183 | + )?; | ||
| 4184 | + for dr in 0..n { | ||
| 4185 | + for dc in 0..n { | ||
| 4186 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | ||
| 4187 | + is_inter: true, | ||
| 4188 | + ref_frame: info.ref_frame, | ||
| 4189 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | ||
| 4190 | + mv1: mv16(mv1), | ||
| 4191 | + mv: mv16(mv), | ||
| 4192 | + is_new_mv, | ||
| 4193 | + size: n as u8, | ||
| 4194 | + size_h: n as u8, | ||
| 4195 | + is_global_mv0: false, | ||
| 4196 | + is_global_mv1: false, | ||
| 4197 | + }); | ||
| 4198 | + } | ||
| 4199 | + } | ||
| 4200 | + mode_for_tx = 0; | ||
| 4201 | + } else { | ||
| 4202 | + write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]); | ||
| 4203 | + | ||
| 3725 | let stack = find_mv_stack( | 4204 | let stack = find_mv_stack( |
| 3726 | grid, | 4205 | grid, |
| 3727 | mi_row, | 4206 | mi_row, |
| @@ -3755,6 +4234,7 @@ fn write_inter_frame_leaf( | |||
| 3755 | } | 4234 | } |
| 3756 | } | 4235 | } |
| 3757 | mode_for_tx = 0; | 4236 | mode_for_tx = 0; |
| 4237 | + } | ||
| 3758 | } else { | 4238 | } else { |
| 3759 | let mode = usize::from(block.mode); | 4239 | let mode = usize::from(block.mode); |
| 3760 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); | 4240 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); |
| @@ -3843,6 +4323,7 @@ fn write_inter_frame_leaf( | |||
| 3843 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); | 4323 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); |
| 3844 | } | 4324 | } |
| 3845 | neighbours.record_inter(at, SUB, block.skip, is_inter, block_ref(block)); | 4325 | neighbours.record_inter(at, SUB, block.skip, is_inter, block_ref(block)); |
| 4326 | + record_block_compound(neighbours, (mi_r, mi_c), SUB, block); | ||
| 3846 | Ok(()) | 4327 | Ok(()) |
| 3847 | } | 4328 | } |
| 3848 | 4329 | ||
| @@ -3912,9 +4393,33 @@ fn write_inter_frame_leaf8( | |||
| 3912 | 4393 | ||
| 3913 | let mode_for_tx; | 4394 | let mode_for_tx; |
| 3914 | if let Some(info) = block.inter { | 4395 | if let Some(info) = block.inter { |
| 3915 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.left_ref[leaf_mi.0]); | 4396 | + write_comp_mode(enc, cdfs, neighbours, leaf_mi, (has_above, has_left), info.ref1.is_some()); |
| 3916 | - | ||
| 3917 | let (mi_row, mi_col) = leaf_mi; | 4397 | let (mi_row, mi_col) = leaf_mi; |
| 4398 | + if info.ref1.is_some() { | ||
| 4399 | + let (mv, mv1, is_new_mv) = write_compound_block( | ||
| 4400 | + enc, cdfs, neighbours, grid, (mi_row, mi_col), (2, 2), | ||
| 4401 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | ||
| 4402 | + )?; | ||
| 4403 | + for dr in 0..2 { | ||
| 4404 | + for dc in 0..2 { | ||
| 4405 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | ||
| 4406 | + is_inter: true, | ||
| 4407 | + ref_frame: info.ref_frame, | ||
| 4408 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | ||
| 4409 | + mv1: mv16(mv1), | ||
| 4410 | + mv: mv16(mv), | ||
| 4411 | + is_new_mv, | ||
| 4412 | + size: 2, | ||
| 4413 | + size_h: 2, | ||
| 4414 | + is_global_mv0: false, | ||
| 4415 | + is_global_mv1: false, | ||
| 4416 | + }); | ||
| 4417 | + } | ||
| 4418 | + } | ||
| 4419 | + mode_for_tx = 0; | ||
| 4420 | + } else { | ||
| 4421 | + write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.above_ref1[leaf_mi.1], neighbours.left_ref[leaf_mi.0], neighbours.left_ref1[leaf_mi.0]); | ||
| 4422 | + | ||
| 3918 | let stack = find_mv_stack( | 4423 | let stack = find_mv_stack( |
| 3919 | grid, | 4424 | grid, |
| 3920 | mi_row, | 4425 | mi_row, |
| @@ -3948,6 +4453,7 @@ fn write_inter_frame_leaf8( | |||
| 3948 | } | 4453 | } |
| 3949 | } | 4454 | } |
| 3950 | mode_for_tx = 0; | 4455 | mode_for_tx = 0; |
| 4456 | + } | ||
| 3951 | } else { | 4457 | } else { |
| 3952 | let mode = usize::from(block.mode); | 4458 | let mode = usize::from(block.mode); |
| 3953 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]); | 4459 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]); |
| @@ -4013,6 +4519,7 @@ fn write_inter_frame_leaf8( | |||
| 4013 | ]; | 4519 | ]; |
| 4014 | neighbours.record_mi(leaf_mi, 8, &zero_grids); | 4520 | neighbours.record_mi(leaf_mi, 8, &zero_grids); |
| 4015 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some(), block_ref(block)); | 4521 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some(), block_ref(block)); |
| 4522 | + record_block_compound(neighbours, leaf_mi, 8, block); | ||
| 4016 | } else { | 4523 | } else { |
| 4017 | let grids = [ | 4524 | let grids = [ |
| 4018 | level_grid(&block.luma, TX8)?, | 4525 | level_grid(&block.luma, TX8)?, |
| @@ -4036,6 +4543,7 @@ fn write_inter_frame_leaf8( | |||
| 4036 | ); | 4543 | ); |
| 4037 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); | 4544 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); |
| 4038 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); | 4545 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); |
| 4546 | + record_block_compound(neighbours, leaf_mi, 8, block); | ||
| 4039 | } | 4547 | } |
| 4040 | Ok((block.skip, is_inter)) | 4548 | Ok((block.skip, is_inter)) |
| 4041 | } | 4549 | } |
| @@ -6317,6 +6825,8 @@ mod tests { | |||
| 6317 | fn an_all_skip_inter_superblock_is_small_and_reads_back() { | 6825 | fn an_all_skip_inter_superblock_is_small_and_reads_back() { |
| 6318 | let block = BlockCoeffs { | 6826 | let block = BlockCoeffs { |
| 6319 | inter: Some(InterInfo { | 6827 | inter: Some(InterInfo { |
| 6828 | + ref1: None, | ||
| 6829 | + mv1: (0, 0), | ||
| 6320 | ref_frame: crate::mvstack::LAST_FRAME, | 6830 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6321 | mode: InterMode::NearestMv, | 6831 | mode: InterMode::NearestMv, |
| 6322 | mv: (0, 0), | 6832 | mv: (0, 0), |
| @@ -6360,6 +6870,8 @@ mod tests { | |||
| 6360 | fn a_newmv_block_reads_back_the_exact_symbol_sequence_it_was_written_with() { | 6870 | fn a_newmv_block_reads_back_the_exact_symbol_sequence_it_was_written_with() { |
| 6361 | let nearest = BlockCoeffs { | 6871 | let nearest = BlockCoeffs { |
| 6362 | inter: Some(InterInfo { | 6872 | inter: Some(InterInfo { |
| 6873 | + ref1: None, | ||
| 6874 | + mv1: (0, 0), | ||
| 6363 | ref_frame: crate::mvstack::LAST_FRAME, | 6875 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6364 | mode: InterMode::NearestMv, | 6876 | mode: InterMode::NearestMv, |
| 6365 | mv: (0, 0), | 6877 | mv: (0, 0), |
| @@ -6370,6 +6882,8 @@ mod tests { | |||
| 6370 | }; | 6882 | }; |
| 6371 | let newmv = BlockCoeffs { | 6883 | let newmv = BlockCoeffs { |
| 6372 | inter: Some(InterInfo { | 6884 | inter: Some(InterInfo { |
| 6885 | + ref1: None, | ||
| 6886 | + mv1: (0, 0), | ||
| 6373 | ref_frame: crate::mvstack::LAST_FRAME, | 6887 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6374 | mode: InterMode::NewMv, | 6888 | mode: InterMode::NewMv, |
| 6375 | mv: (0, 2), | 6889 | mv: (0, 2), |