merge: lane-av1tiles with main ac6e60b4 (compound prediction)
Browse files at this commit6 changed files +945 -32
- crates/ec-av1/src/decode.rs +21 -2
- crates/ec-av1/src/encode.rs +313 -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
| @@ -25059,6 +25059,15 @@ fn read_comp_mode( | ||
| 25059 | 25059 | ) -> bool { |
| 25060 | 25060 | let ctx = reference_mode_ctx(above, left); |
| 25061 | 25061 | let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1; |
| 25062 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | |
| 25063 | + eprintln!( | |
| 25064 | + "EC_RCM ctx={ctx} val={} a={:?} l={:?} rng={}", | |
| 25065 | + usize::from(compound), | |
| 25066 | + above.map(|n| (n.is_inter, n.ref0, n.ref1)), | |
| 25067 | + left.map(|n| (n.is_inter, n.ref0, n.ref1)), | |
| 25068 | + dec.debug_state().0 | |
| 25069 | + ); | |
| 25070 | + } | |
| 25062 | 25071 | if compound { |
| 25063 | 25072 | hit!(COMP_MODE_HITS); |
| 25064 | 25073 | } |
| @@ -37591,7 +37600,12 @@ mod tests { | ||
| 37591 | 37600 | false, |
| 37592 | 37601 | Some(mc::InterpFilterKind::Regular), |
| 37593 | 37602 | false, |
| 37594 | - false, | |
| 37603 | + // The encoder's own `reference_select` (spec 5.9.22): these | |
| 37604 | + // GOP round trips decode a tile the ENCODER wrote, so the | |
| 37605 | + // header bit must be the one it wrote, not a hard-coded | |
| 37606 | + // `false` (stale-header class -- a `reference_select` stream | |
| 37607 | + // reads a `comp_mode` symbol this decode would skip). | |
| 37608 | + crate::encode::reference_select(), | |
| 37595 | 37609 | frame.tx_select, |
| 37596 | 37610 | &frame.loop_restoration, |
| 37597 | 37611 | Some(frame.start_cdfs.0.clone()), |
| @@ -37910,7 +37924,12 @@ mod tests { | ||
| 37910 | 37924 | false, |
| 37911 | 37925 | Some(mc::InterpFilterKind::Regular), |
| 37912 | 37926 | false, |
| 37913 | - false, | |
| 37927 | + // The encoder's own `reference_select` (spec 5.9.22): these | |
| 37928 | + // GOP round trips decode a tile the ENCODER wrote, so the | |
| 37929 | + // header bit must be the one it wrote, not a hard-coded | |
| 37930 | + // `false` (stale-header class -- a `reference_select` stream | |
| 37931 | + // reads a `comp_mode` symbol this decode would skip). | |
| 37932 | + crate::encode::reference_select(), | |
| 37914 | 37933 | frame.tx_select, |
| 37915 | 37934 | &frame.loop_restoration, |
| 37916 | 37935 | Some(frame.start_cdfs.0.clone()), |
crates/ec-av1/src/encode.rsmodified+313 -5
| @@ -2675,6 +2675,8 @@ fn code_square_inter( | ||
| 2675 | 2675 | eight: None, |
| 2676 | 2676 | tx_depth, |
| 2677 | 2677 | inter: Some(InterInfo { |
| 2678 | + ref1: None, | |
| 2679 | + mv1: (0, 0), | |
| 2678 | 2680 | ref_frame: crate::mvstack::LAST_FRAME, |
| 2679 | 2681 | mode: InterMode::NewMv, |
| 2680 | 2682 | mv: new_mv, |
| @@ -2709,6 +2711,8 @@ fn code_square_inter( | ||
| 2709 | 2711 | eight: None, |
| 2710 | 2712 | tx_depth, |
| 2711 | 2713 | inter: Some(InterInfo { |
| 2714 | + ref1: None, | |
| 2715 | + mv1: (0, 0), | |
| 2712 | 2716 | ref_frame: crate::mvstack::LAST_FRAME, |
| 2713 | 2717 | mode: InterMode::NearestMv, |
| 2714 | 2718 | mv, |
| @@ -3885,6 +3889,38 @@ fn extra_ref_new_mv() -> bool { | ||
| 3885 | 3889 | } |
| 3886 | 3890 | } |
| 3887 | 3891 | |
| 3892 | +/// The seven `ref_frame_idx` order hints of a frame coded on this crate's own | |
| 3893 | +/// flat slot plan (`inter_frame_headers_slots`): every reference but | |
| 3894 | +/// `GOLDEN_FRAME` and `ALTREF_FRAME` reads the frame just coded, | |
| 3895 | +/// `GOLDEN_FRAME` reads the GOP's key frame, and `ALTREF_FRAME` reads the | |
| 3896 | +/// frame two back when there is one (otherwise it, too, names the key frame's | |
| 3897 | +/// slot). Hints are 7-bit, the only `order_hint_bits` this encoder writes. | |
| 3898 | +pub(crate) fn flat_order_hints(order_hint: u32, key_hint: u32, has_altref: bool) -> [u32; 7] { | |
| 3899 | + let last = order_hint.wrapping_sub(1) & 0x7f; | |
| 3900 | + let alt = if has_altref { | |
| 3901 | + order_hint.wrapping_sub(2) & 0x7f | |
| 3902 | + } else { | |
| 3903 | + key_hint | |
| 3904 | + }; | |
| 3905 | + [last, last, last, key_hint, last, last, alt] | |
| 3906 | +} | |
| 3907 | + | |
| 3908 | +/// Whether an inter frame carries `reference_select` (spec 5.9.22): the | |
| 3909 | +/// header bit that lets each block choose between a single and a compound | |
| 3910 | +/// reference. Off by default until compound prediction is measured to pay; | |
| 3911 | +/// `EC_AV1_COMPOUND=1` arms it in a test build (which is what the BD gate | |
| 3912 | +/// runs as), and every block still codes SINGLE, so the only cost is one | |
| 3913 | +/// `comp_mode` symbol per inter block. | |
| 3914 | +const REFERENCE_SELECT: bool = true; | |
| 3915 | + | |
| 3916 | +/// [`REFERENCE_SELECT`], with the test-build environment override. | |
| 3917 | +pub(crate) fn reference_select() -> bool { | |
| 3918 | + match std::env::var("EC_AV1_COMPOUND").ok() { | |
| 3919 | + Some(v) if cfg!(test) => v != "0", | |
| 3920 | + _ => REFERENCE_SELECT, | |
| 3921 | + } | |
| 3922 | +} | |
| 3923 | + | |
| 3888 | 3924 | /// [`tile::write_drl_idx`]'s cost against one stack: one `drl_mode` symbol |
| 3889 | 3925 | /// per entry past `start`, `1` to advance and `0` to stop, at most two. |
| 3890 | 3926 | fn drl_bits_for(stack: &MvStack, start: usize, target: usize) -> f64 { |
| @@ -4029,6 +4065,56 @@ fn mc_trial( | ||
| 4029 | 4065 | plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set) |
| 4030 | 4066 | } |
| 4031 | 4067 | |
| 4068 | +/// [`mc_trial`]'s compound counterpart (spec 7.11.3.1's compound path): both | |
| 4069 | +/// references predict into the `CONV_BUF` intermediate domain | |
| 4070 | +/// ([`mc::predict_compound_intermediate`]) and are blended by | |
| 4071 | +/// [`mc::combine_compound`] at the simple-average split `(8, 8)` -- exactly | |
| 4072 | +/// what a decoder computes for a `comp_group_idx == 0`, `compound_idx == 1` | |
| 4073 | +/// block, so the encoder's reconstruction is the decoder's. | |
| 4074 | +#[allow(clippy::too_many_arguments)] | |
| 4075 | +fn mc_trial_compound( | |
| 4076 | + plane: &Plane, | |
| 4077 | + x: usize, | |
| 4078 | + y: usize, | |
| 4079 | + side: usize, | |
| 4080 | + (mv0, mv1): ((i32, i32), (i32, i32)), | |
| 4081 | + luma: bool, | |
| 4082 | + ref0: (&[u16], usize, usize, usize), | |
| 4083 | + ref1: (&[u16], usize, usize, usize), | |
| 4084 | + base_q_idx: u8, | |
| 4085 | + deadzone: f64, | |
| 4086 | + set: TxbSet, fctx: &crate::decode::FrameCtx, | |
| 4087 | +) -> Trial { | |
| 4088 | + let mut inter0 = [0i32; BLOCK * BLOCK]; | |
| 4089 | + let mut inter1 = [0i32; BLOCK * BLOCK]; | |
| 4090 | + let (inter0, inter1) = (&mut inter0[..side * side], &mut inter1[..side * side]); | |
| 4091 | + for (mv, r, dst) in [(mv0, ref0, &mut *inter0), (mv1, ref1, &mut *inter1)] { | |
| 4092 | + mc::predict_compound_intermediate( | |
| 4093 | + r.0, | |
| 4094 | + r.1, | |
| 4095 | + r.2, | |
| 4096 | + r.3, | |
| 4097 | + mv_to_q4(x, mv.1, luma), | |
| 4098 | + mv_to_q4(y, mv.0, luma), | |
| 4099 | + mc::REF_NO_SCALE, | |
| 4100 | + side, | |
| 4101 | + side, | |
| 4102 | + mc::InterpFilterKind::Regular, | |
| 4103 | + mc::InterpFilterKind::Regular, | |
| 4104 | + dst, | |
| 4105 | + ); | |
| 4106 | + } | |
| 4107 | + let mut blended16 = [0u16; BLOCK * BLOCK]; | |
| 4108 | + let blended16 = &mut blended16[..side * side]; | |
| 4109 | + mc::combine_compound(inter0, inter1, 8, 8, blended16, fctx); | |
| 4110 | + let mut prediction = [0u8; BLOCK * BLOCK]; | |
| 4111 | + let prediction = &mut prediction[..side * side]; | |
| 4112 | + for (dst, &src) in prediction.iter_mut().zip(blended16.iter()) { | |
| 4113 | + *dst = src as u8; | |
| 4114 | + } | |
| 4115 | + plane.code_from_prediction(x, y, side, prediction, false, base_q_idx, deadzone, set) | |
| 4116 | +} | |
| 4117 | + | |
| 4032 | 4118 | /// Codes one 32x32 block of an inter frame as whichever costs least of: each |
| 4033 | 4119 | /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's |
| 4034 | 4120 | /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by |
| @@ -4061,7 +4147,11 @@ fn search_inter_block( | ||
| 4061 | 4147 | // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a |
| 4062 | 4148 | // second motion search would double the wall of the stage that already |
| 4063 | 4149 | // owns most of it. |
| 4064 | - extra: &[(i8, &Picture, &MvStack)], fctx: &crate::decode::FrameCtx, | |
| 4150 | + extra: &[(i8, &Picture, &MvStack)], | |
| 4151 | + // The COMPOUND stack of each `LAST_FRAME` + extra-reference pair, built by | |
| 4152 | + // the caller off the same MI grid the tile writer will (empty unless the | |
| 4153 | + // frame carries `reference_select`). | |
| 4154 | + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], fctx: &crate::decode::FrameCtx, | |
| 4065 | 4155 | ) -> (BlockCoeffs, f64) { |
| 4066 | 4156 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 4067 | 4157 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| @@ -4238,6 +4328,8 @@ fn search_inter_block( | ||
| 4238 | 4328 | stack.nearest_mv, |
| 4239 | 4329 | last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), |
| 4240 | 4330 | InterInfo { |
| 4331 | + ref1: None, | |
| 4332 | + mv1: (0, 0), | |
| 4241 | 4333 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4242 | 4334 | mode: InterMode::NearestMv, |
| 4243 | 4335 | mv: stack.nearest_mv, |
| @@ -4251,6 +4343,8 @@ fn search_inter_block( | ||
| 4251 | 4343 | (0, 0), |
| 4252 | 4344 | last_ref_bits + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), |
| 4253 | 4345 | InterInfo { |
| 4346 | + ref1: None, | |
| 4347 | + mv1: (0, 0), | |
| 4254 | 4348 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4255 | 4349 | mode: InterMode::GlobalMv, |
| 4256 | 4350 | mv: (0, 0), |
| @@ -4266,6 +4360,8 @@ fn search_inter_block( | ||
| 4266 | 4360 | e.mv, |
| 4267 | 4361 | near_bits + drl_bits(1, idx), |
| 4268 | 4362 | InterInfo { |
| 4363 | + ref1: None, | |
| 4364 | + mv1: (0, 0), | |
| 4269 | 4365 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4270 | 4366 | mode: InterMode::NearMv, |
| 4271 | 4367 | mv: e.mv, |
| @@ -4304,6 +4400,8 @@ fn search_inter_block( | ||
| 4304 | 4400 | mv, |
| 4305 | 4401 | last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, |
| 4306 | 4402 | InterInfo { |
| 4403 | + ref1: None, | |
| 4404 | + mv1: (0, 0), | |
| 4307 | 4405 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4308 | 4406 | mode: InterMode::NewMv, |
| 4309 | 4407 | mv, |
| @@ -4312,6 +4410,9 @@ fn search_inter_block( | ||
| 4312 | 4410 | )); |
| 4313 | 4411 | } |
| 4314 | 4412 | |
| 4413 | + // Each extra reference's own `NEWMV` vector, for the compound | |
| 4414 | + // `NEW_NEWMV` candidate below to pair with `LAST`'s. | |
| 4415 | + let mut extra_new_mvs: Vec<(i8, (i32, i32))> = Vec::new(); | |
| 4315 | 4416 | for &(ref_frame, g, gstack) in extra { |
| 4316 | 4417 | let g_ref_bits = single_ref_bits(ref_frame); |
| 4317 | 4418 | let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); |
| @@ -4322,6 +4423,8 @@ fn search_inter_block( | ||
| 4322 | 4423 | + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) |
| 4323 | 4424 | + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), |
| 4324 | 4425 | InterInfo { |
| 4426 | + ref1: None, | |
| 4427 | + mv1: (0, 0), | |
| 4325 | 4428 | ref_frame, |
| 4326 | 4429 | mode: InterMode::NearestMv, |
| 4327 | 4430 | mv: gstack.nearest_mv, |
| @@ -4332,6 +4435,8 @@ fn search_inter_block( | ||
| 4332 | 4435 | (0, 0), |
| 4333 | 4436 | g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), |
| 4334 | 4437 | InterInfo { |
| 4438 | + ref1: None, | |
| 4439 | + mv1: (0, 0), | |
| 4335 | 4440 | ref_frame, |
| 4336 | 4441 | mode: InterMode::GlobalMv, |
| 4337 | 4442 | mv: (0, 0), |
| @@ -4386,11 +4491,14 @@ fn search_inter_block( | ||
| 4386 | 4491 | #[cfg(test)] |
| 4387 | 4492 | stage_add(0, t.elapsed()); |
| 4388 | 4493 | let gmv = round_to_valid_mv(gfound.mv, gstack.pred_mv); |
| 4494 | + extra_new_mvs.push((ref_frame, gmv)); | |
| 4389 | 4495 | if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(gstack, gmv) { |
| 4390 | 4496 | cands.push(( |
| 4391 | 4497 | gmv, |
| 4392 | 4498 | g_ref_bits + symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 0) + bits, |
| 4393 | 4499 | InterInfo { |
| 4500 | + ref1: None, | |
| 4501 | + mv1: (0, 0), | |
| 4394 | 4502 | ref_frame, |
| 4395 | 4503 | mode: InterMode::NewMv, |
| 4396 | 4504 | mv: gmv, |
| @@ -4401,6 +4509,130 @@ fn search_inter_block( | ||
| 4401 | 4509 | } |
| 4402 | 4510 | } |
| 4403 | 4511 | |
| 4512 | + // The compound candidates (spec 5.11.24's `INTER_COMPOUND_MODES`), one | |
| 4513 | + // trial set each: both references predict the block and the two | |
| 4514 | + // predictions are averaged, which is what pays on content where neither | |
| 4515 | + // reference alone is right. Priced with the same static-CDF approximation | |
| 4516 | + // the single-reference candidates use, plus the compound-only symbols | |
| 4517 | + // (`comp_mode`, the reference-pair tree, `comp_group_idx`/`compound_idx`). | |
| 4518 | + for (ref1, g, cstack) in compound { | |
| 4519 | + let (ref1, g) = (*ref1, *g); | |
| 4520 | + let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME) | |
| 4521 | + .contains(&ref1); | |
| 4522 | + let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) | |
| 4523 | + + if uni { | |
| 4524 | + // LAST + a backward reference: `comp_ref_type` = bidirectional, | |
| 4525 | + // `comp_ref` LAST, `comp_bwdref` ALTREF. | |
| 4526 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 1) | |
| 4527 | + + symbol_bits(&cdf::COMP_REF[0][0], 0) | |
| 4528 | + + symbol_bits(&cdf::COMP_REF[0][1], 0) | |
| 4529 | + + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) | |
| 4530 | + } else { | |
| 4531 | + // LAST + GOLDEN: unidirectional, `uni_comp_ref` p0/p1/p2. | |
| 4532 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 0) | |
| 4533 | + + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) | |
| 4534 | + + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) | |
| 4535 | + + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) | |
| 4536 | + }; | |
| 4537 | + let mode_ctx = | |
| 4538 | + cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; | |
| 4539 | + let mode_bits_of = | |
| 4540 | + |mode: usize| pair_bits + symbol_bits(&cdf::INTER_COMPOUND_MODE[mode_ctx], mode); | |
| 4541 | + let mut ccands: Vec<(((i32, i32), (i32, i32)), f64, InterInfo)> = vec![ | |
| 4542 | + ( | |
| 4543 | + cstack.nearest_mv, | |
| 4544 | + mode_bits_of(0), | |
| 4545 | + InterInfo { | |
| 4546 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 4547 | + mode: InterMode::NearestNearestMv, | |
| 4548 | + mv: cstack.nearest_mv.0, | |
| 4549 | + mv1: cstack.nearest_mv.1, | |
| 4550 | + ref1: Some(ref1), | |
| 4551 | + ref_mv_idx: 0, | |
| 4552 | + }, | |
| 4553 | + ), | |
| 4554 | + ( | |
| 4555 | + ((0, 0), (0, 0)), | |
| 4556 | + mode_bits_of(6), | |
| 4557 | + InterInfo { | |
| 4558 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 4559 | + mode: InterMode::GlobalGlobalMv, | |
| 4560 | + mv: (0, 0), | |
| 4561 | + mv1: (0, 0), | |
| 4562 | + ref1: Some(ref1), | |
| 4563 | + ref_mv_idx: 0, | |
| 4564 | + }, | |
| 4565 | + ), | |
| 4566 | + ]; | |
| 4567 | + // `NEW_NEWMV` seeded from the two single-reference searches this | |
| 4568 | + // block already ran -- no joint search of its own. | |
| 4569 | + if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) { | |
| 4570 | + let base = cstack | |
| 4571 | + .entries | |
| 4572 | + .first() | |
| 4573 | + .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1)); | |
| 4574 | + if let (Some(b0), Some(b1)) = ( | |
| 4575 | + mv_residual_bits(mv, base.0), | |
| 4576 | + mv_residual_bits(mv1, base.1), | |
| 4577 | + ) { | |
| 4578 | + ccands.push(( | |
| 4579 | + (mv, mv1), | |
| 4580 | + mode_bits_of(7) + b0 + b1, | |
| 4581 | + InterInfo { | |
| 4582 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 4583 | + mode: InterMode::NewNewMv, | |
| 4584 | + mv, | |
| 4585 | + mv1, | |
| 4586 | + ref1: Some(ref1), | |
| 4587 | + ref_mv_idx: 0, | |
| 4588 | + }, | |
| 4589 | + )); | |
| 4590 | + } | |
| 4591 | + } | |
| 4592 | + ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); | |
| 4593 | + ccands.dedup_by_key(|c| c.0); | |
| 4594 | + let g_luma = (g.y.as_slice(), g.width, luma.true_width, luma.true_height); | |
| 4595 | + let g_u = (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height); | |
| 4596 | + let g_v = (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height); | |
| 4597 | + for (mvs, bits, info) in ccands { | |
| 4598 | + let luma_trial = mc_trial_compound( | |
| 4599 | + luma, x, y, BLOCK, mvs, true, | |
| 4600 | + (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), g_luma, | |
| 4601 | + search.base_q_idx, search.deadzone, TxbSet::Luma32Inter, fctx, | |
| 4602 | + ); | |
| 4603 | + let u = mc_trial_compound( | |
| 4604 | + &chroma[0], x / 2, y / 2, BLOCK / 2, mvs, false, | |
| 4605 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), g_u, | |
| 4606 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | |
| 4607 | + ); | |
| 4608 | + let v = mc_trial_compound( | |
| 4609 | + &chroma[1], x / 2, y / 2, BLOCK / 2, mvs, false, | |
| 4610 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), g_v, | |
| 4611 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | |
| 4612 | + ); | |
| 4613 | + let skip = luma_trial.levels.iter().all(|&l| l == 0) | |
| 4614 | + && u.levels.iter().all(|&l| l == 0) | |
| 4615 | + && v.levels.iter().all(|&l| l == 0); | |
| 4616 | + let cost = luma_trial.sse | |
| 4617 | + + u.sse | |
| 4618 | + + v.sse | |
| 4619 | + + search.lambda | |
| 4620 | + * (skip_bits(skip) | |
| 4621 | + + intra_inter_bits(true) | |
| 4622 | + + bits | |
| 4623 | + + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits }); | |
| 4624 | + consider(Candidate { | |
| 4625 | + cost, | |
| 4626 | + luma: luma_trial, | |
| 4627 | + u, | |
| 4628 | + v, | |
| 4629 | + mode: DC_PRED, | |
| 4630 | + skip, | |
| 4631 | + inter: Some(info), | |
| 4632 | + }); | |
| 4633 | + } | |
| 4634 | + } | |
| 4635 | + | |
| 4404 | 4636 | // One trial set per distinct (reference, vector) pair: sort, cheapest |
| 4405 | 4637 | // syntax first, and drop the rest of each pair's run. |
| 4406 | 4638 | cands.sort_by(|a, b| { |
| @@ -4554,6 +4786,12 @@ pub(crate) fn encode_inter_frame( | ||
| 4554 | 4786 | base_q_idx: u8, |
| 4555 | 4787 | deadzone: f64, |
| 4556 | 4788 | order_hint: u32, |
| 4789 | + // The order hint each of the seven `ref_frame_idx` entries names, which a | |
| 4790 | + // decoder reads out of its own reference state -- only spec 5.9.22's | |
| 4791 | + // `skipModeAllowed` (in `frame::write_frame_header`) reads it, and only | |
| 4792 | + // on a `reference_select` frame. [`flat_order_hints`] builds it for the | |
| 4793 | + // callers that follow this crate's own flat slot plan. | |
| 4794 | + order_hints: [u32; 7], | |
| 4557 | 4795 | render: (usize, usize), |
| 4558 | 4796 | // The tables this frame's writer starts from: what the previous frame |
| 4559 | 4797 | // stored into the slot this one's `primary_ref_frame` names. `None` |
| @@ -4630,6 +4868,14 @@ pub(crate) fn encode_inter_frame( | ||
| 4630 | 4868 | header.render_width = render.0 as u32; |
| 4631 | 4869 | header.render_height = render.1 as u32; |
| 4632 | 4870 | let sign_bias = pyramid.map_or(crate::mvstack::NO_SIGN_BIAS, |p| p.sign_bias); |
| 4871 | + // The order hint behind each `ref_frame_idx` entry. The flat path's own | |
| 4872 | + // slot plan (`inter_frame_headers_slots`) is: every reference but | |
| 4873 | + // `GOLDEN_FRAME` reads `last_slot` (the frame just coded) except | |
| 4874 | + // `ALTREF_FRAME`, which reads the frame two back when there is one; | |
| 4875 | + // `GOLDEN_FRAME` is the key frame, order hint 0. Only | |
| 4876 | + // `frame::write_frame_header`'s `skipModeAllowed` reads this. | |
| 4877 | + header.order_hints = order_hints; | |
| 4878 | + header.reference_select = reference_select(); | |
| 4633 | 4879 | if let Some(p) = pyramid { |
| 4634 | 4880 | header.show_frame = p.show_frame; |
| 4635 | 4881 | header.showable_frame = !p.show_frame; |
| @@ -4787,6 +5033,37 @@ pub(crate) fn encode_inter_frame( | ||
| 4787 | 5033 | .iter() |
| 4788 | 5034 | .map(|(r, p, st)| (*r, *p, st)) |
| 4789 | 5035 | .collect(); |
| 5036 | + // The COMPOUND stack of each `LAST` + extra pair, off the | |
| 5037 | + // same grid (and the same armed sign bias) the tile writer | |
| 5038 | + // rebuilds it from. Empty unless this frame codes | |
| 5039 | + // `reference_select`, which is what gates the syntax. | |
| 5040 | + let compound: Vec<(i8, &Picture, crate::mvstack::CompoundMvStack)> = | |
| 5041 | + if header.reference_select { | |
| 5042 | + extra_stacks | |
| 5043 | + .iter() | |
| 5044 | + .map(|&(r, p, _)| { | |
| 5045 | + ( | |
| 5046 | + r, | |
| 5047 | + p, | |
| 5048 | + crate::mvstack::find_mv_stack_compound( | |
| 5049 | + &grid, | |
| 5050 | + mi_row, | |
| 5051 | + mi_col, | |
| 5052 | + 8, | |
| 5053 | + 8, | |
| 5054 | + (crate::mvstack::LAST_FRAME, r), | |
| 5055 | + mi_cols, | |
| 5056 | + mi_rows, | |
| 5057 | + grid.sign_bias_table(), | |
| 5058 | + &[(0, 0); 7], | |
| 5059 | + None, | |
| 5060 | + ), | |
| 5061 | + ) | |
| 5062 | + }) | |
| 5063 | + .collect() | |
| 5064 | + } else { | |
| 5065 | + Vec::new() | |
| 5066 | + }; | |
| 4790 | 5067 | |
| 4791 | 5068 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 4792 | 5069 | let (block, mut cost_whole) = search_inter_block( |
| @@ -4797,7 +5074,8 @@ pub(crate) fn encode_inter_frame( | ||
| 4797 | 5074 | &mode_bits_table, |
| 4798 | 5075 | reference, |
| 4799 | 5076 | &stack, |
| 4800 | - &extra, fctx, | |
| 5077 | + &extra, | |
| 5078 | + &compound, fctx, | |
| 4801 | 5079 | ); |
| 4802 | 5080 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 4803 | 5081 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -5134,6 +5412,8 @@ pub(crate) fn encode_inter_frame( | ||
| 5134 | 5412 | .cloned() |
| 5135 | 5413 | .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); |
| 5136 | 5414 | let start_cdfs = CdfSnapshot(cdfs); |
| 5415 | + let reference_select_bit = header.reference_select; | |
| 5416 | + let order_hint_bits = seq.order_hint_bits; | |
| 5137 | 5417 | let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; |
| 5138 | 5418 | let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; |
| 5139 | 5419 | // What this frame stores is the LARGEST tile's end-of-tile tables -- |
| @@ -5152,6 +5432,9 @@ pub(crate) fn encode_inter_frame( | ||
| 5152 | 5432 | crate::tile::arm_cdef_idx(bits, sb_cols, cdef_grid.to_vec()); |
| 5153 | 5433 | crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); |
| 5154 | 5434 | crate::tile::arm_sign_bias(sign_bias); |
| 5435 | + // Thread-locals: every tile job arms its own worker. | |
| 5436 | + crate::tile::arm_reference_select(reference_select_bit); | |
| 5437 | + crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); | |
| 5155 | 5438 | let mut cdfs = start_cdfs.0.clone(); |
| 5156 | 5439 | // Each tile's own MV grid: a candidate scan never reaches past |
| 5157 | 5440 | // the tile's bounds, so a grid holding only this tile's own |
| @@ -5394,6 +5677,7 @@ pub(crate) fn encode_sequence_with_ctx( | ||
| 5394 | 5677 | base_q_idx, |
| 5395 | 5678 | deadzone, |
| 5396 | 5679 | order_hint, |
| 5680 | + flat_order_hints(order_hint, 0, prev2.is_some()), | |
| 5397 | 5681 | render, |
| 5398 | 5682 | Some(&carried.0), |
| 5399 | 5683 | Some(&golden), |
| @@ -5815,6 +6099,8 @@ mod tests { | ||
| 5815 | 6099 | eight: None, |
| 5816 | 6100 | tx_depth: 0, |
| 5817 | 6101 | inter: Some(InterInfo { |
| 6102 | + ref1: None, | |
| 6103 | + mv1: (0, 0), | |
| 5818 | 6104 | ref_frame: crate::mvstack::LAST_FRAME, |
| 5819 | 6105 | mode: InterMode::NearestMv, |
| 5820 | 6106 | mv: (0, 0), |
| @@ -5824,6 +6110,8 @@ mod tests { | ||
| 5824 | 6110 | let skipped_block = BlockCoeffs { |
| 5825 | 6111 | skip: true, |
| 5826 | 6112 | inter: Some(InterInfo { |
| 6113 | + ref1: None, | |
| 6114 | + mv1: (0, 0), | |
| 5827 | 6115 | ref_frame: crate::mvstack::LAST_FRAME, |
| 5828 | 6116 | mode: InterMode::NearestMv, |
| 5829 | 6117 | mv: (0, 0), |
| @@ -7454,7 +7742,7 @@ mod tests { | ||
| 7454 | 7742 | let padded_pic = picture.padded_to(SUPERBLOCK); |
| 7455 | 7743 | let t = Instant::now(); |
| 7456 | 7744 | let inter = |
| 7457 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx, None).unwrap(); | |
| 7745 | + 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(); | |
| 7458 | 7746 | let inter_t = t.elapsed(); |
| 7459 | 7747 | eprintln!( |
| 7460 | 7748 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ |
| @@ -7552,7 +7840,7 @@ mod tests { | ||
| 7552 | 7840 | stage_reset(); |
| 7553 | 7841 | let t = Instant::now(); |
| 7554 | 7842 | let inter = |
| 7555 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx, None).unwrap(); | |
| 7843 | + 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(); | |
| 7556 | 7844 | let total_t = t.elapsed(); |
| 7557 | 7845 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); |
| 7558 | 7846 | let accounted = motion_search + transform_quant + entropy; |
| @@ -7729,6 +8017,7 @@ mod tests { | ||
| 7729 | 8017 | q, |
| 7730 | 8018 | 0.5, |
| 7731 | 8019 | 1, |
| 8020 | + flat_order_hints(1, 0, false), | |
| 7732 | 8021 | (width, height), None, None, None, fctx, None, |
| 7733 | 8022 | ) |
| 7734 | 8023 | .unwrap(); |
| @@ -7746,6 +8035,7 @@ mod tests { | ||
| 7746 | 8035 | q, |
| 7747 | 8036 | 0.5, |
| 7748 | 8037 | 1, |
| 8038 | + flat_order_hints(1, 0, false), | |
| 7749 | 8039 | (width, height), None, None, None, fctx, None, |
| 7750 | 8040 | ) |
| 7751 | 8041 | .unwrap(); |
| @@ -8241,7 +8531,7 @@ mod tests { | ||
| 8241 | 8531 | }) |
| 8242 | 8532 | }; |
| 8243 | 8533 | let pins: [(u8, usize, u64); 2] = |
| 8244 | - [(150, 7444, 0x76eb980832d719f1), (60, 27692, 0x8c88f7e73455d439)]; | |
| 8534 | + [(150, 7209, 0xacdd_6952_78c1_49f2), (60, 27353, 0x9be8_7898_f08d_3b77)]; | |
| 8245 | 8535 | for (q, bytes, hash) in pins { |
| 8246 | 8536 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 8247 | 8537 | assert_eq!( |
| @@ -8405,6 +8695,8 @@ mod tests { | ||
| 8405 | 8695 | let _ = crate::tile::take_inter_mode_hits(); |
| 8406 | 8696 | let _ = crate::tile::take_drl_hits(); |
| 8407 | 8697 | let _ = crate::tile::take_ref_hits(); |
| 8698 | + let _ = crate::tile::take_compound_mode_hits(); | |
| 8699 | + let _ = crate::tile::take_compound_pair_hits(); | |
| 8408 | 8700 | let _ = crate::motion::take_census(); |
| 8409 | 8701 | let (ours, ours_wall) = match pyramid_from_env() { |
| 8410 | 8702 | None => our_ladder(&source, width, height, fctx), |
| @@ -8440,6 +8732,22 @@ mod tests { | ||
| 8440 | 8732 | 100.0 * global as f64 / modes_total as f64, |
| 8441 | 8733 | 100.0 * new as f64 / modes_total as f64, |
| 8442 | 8734 | ); |
| 8735 | + // The compound census: what share of the coded inter blocks took | |
| 8736 | + // a compound reference at all, which compound mode they took, and | |
| 8737 | + // which pair (gate-blind-to-feature -- a compound mode nobody | |
| 8738 | + // picks cannot be what a BD number credits). | |
| 8739 | + let comp_modes = crate::tile::take_compound_mode_hits(); | |
| 8740 | + let comp_pairs = crate::tile::take_compound_pair_hits(); | |
| 8741 | + let comp_total: usize = comp_modes.iter().sum(); | |
| 8742 | + eprintln!( | |
| 8743 | + "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \ | |
| 8744 | + NEAREST_NEAREST {} NEAR_NEAR {} GLOBAL_GLOBAL {} NEW_NEW {}; pairs \ | |
| 8745 | + LAST+GOLDEN {} LAST+ALTREF {}", | |
| 8746 | + modes_total + comp_total, | |
| 8747 | + 100.0 * comp_total as f64 / (modes_total + comp_total) as f64, | |
| 8748 | + comp_modes[0], comp_modes[1], comp_modes[6], comp_modes[7], | |
| 8749 | + comp_pairs[3], comp_pairs[6], | |
| 8750 | + ); | |
| 8443 | 8751 | // Where the wall-owning stage actually goes: how many candidate |
| 8444 | 8752 | // evaluations (one `mc::predict` + SAD each) a search spends, how |
| 8445 | 8753 | // many of them are subpel, and how often the winner is the seed |
crates/ec-av1/src/encoder.rsmodified+20 -5
| @@ -752,6 +752,15 @@ impl Av1Encoder { | ||
| 752 | 752 | base_q_idx, |
| 753 | 753 | DEADZONE, |
| 754 | 754 | order as u32, |
| 755 | + // The flat path's own slot plan, in order hints: the key frame | |
| 756 | + // of the GOP this picture sits in is what `GOLDEN_FRAME` | |
| 757 | + // names, and every reference but `ALTREF_FRAME` is the frame | |
| 758 | + // just coded. | |
| 759 | + crate::encode::flat_order_hints( | |
| 760 | + order as u32 & 0x7f, | |
| 761 | + (order - order % self.config.gop as u64) as u32 & 0x7f, | |
| 762 | + self.prev2.is_some(), | |
| 763 | + ), | |
| 755 | 764 | render, |
| 756 | 765 | self.carried_cdfs.as_ref().map(|c| &c.0), |
| 757 | 766 | self.golden.as_ref(), |
| @@ -871,14 +880,19 @@ impl Av1Encoder { | ||
| 871 | 880 | .as_ref() |
| 872 | 881 | .is_some_and(|s| s.order_hint > order_hint) |
| 873 | 882 | }; |
| 874 | - for (i, slot) in [ | |
| 883 | + let slots = [ | |
| 875 | 884 | last_slot, last_slot, last_slot, GOLDEN_SLOT, last_slot, last_slot, altref_slot, |
| 876 | - ] | |
| 877 | - .into_iter() | |
| 878 | - .enumerate() | |
| 879 | - { | |
| 885 | + ]; | |
| 886 | + for (i, slot) in slots.into_iter().enumerate() { | |
| 880 | 887 | sign_bias[i] = ahead(slot, &self.dpb); |
| 881 | 888 | } |
| 889 | + // The same seven slots' order hints, for `skipModeAllowed` (spec | |
| 890 | + // 5.9.22) in the frame-header writer. | |
| 891 | + let order_hints: [u32; 7] = slots.map(|slot| { | |
| 892 | + self.dpb[slot as usize] | |
| 893 | + .as_ref() | |
| 894 | + .map_or(0, |s| (s.order_hint & 0x7f) as u32) | |
| 895 | + }); | |
| 882 | 896 | let reference = self.dpb[last_slot as usize] |
| 883 | 897 | .as_ref() |
| 884 | 898 | .map(|s| s.picture.clone()) |
| @@ -896,6 +910,7 @@ impl Av1Encoder { | ||
| 896 | 910 | base_q_idx, |
| 897 | 911 | DEADZONE, |
| 898 | 912 | order_hint, |
| 913 | + order_hints, | |
| 899 | 914 | render, |
| 900 | 915 | start_cdfs.as_ref(), |
| 901 | 916 | golden.as_ref(), |
crates/ec-av1/src/frame.rsmodified+57 -8
| @@ -359,6 +359,45 @@ pub fn write_show_existing_frame_header( | ||
| 359 | 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 | 401 | /// `uncompressed_header()` (spec 5.9.2) for a shown key or inter frame: no |
| 363 | 402 | /// decoder model, no frame ids, no still-picture shortcut, and (for an inter |
| 364 | 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 | 570 | if h.frame_is_intra { |
| 532 | 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 | 572 | } else { |
| 539 | - w.write_bit(false); | |
| 540 | - // skip_mode_present is forced false when reference_select is false | |
| 541 | - // (spec 5.9.22), and not coded. | |
| 573 | + w.write_bit(h.reference_select); | |
| 574 | + // spec 5.9.22 `skip_mode_params`: `skip_mode_present` is coded only | |
| 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 | 592 | if h.frame_is_intra || h.error_resilient_mode || !seq.enable_warped_motion { |
| 544 | 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 | 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 | 295 | pub fn set_sign_bias(&mut self, sign_bias: SignBiasTable) { |
| 288 | 296 | self.sign_bias = sign_bias; |
| 289 | 297 | } |
crates/ec-av1/src/tile.rsmodified+526 -12
| @@ -304,6 +304,300 @@ pub(crate) fn arm_sign_bias(sign_bias: crate::mvstack::SignBiasTable) { | ||
| 304 | 304 | SIGN_BIAS.with(|c| c.set(sign_bias)); |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | +thread_local! { | |
| 308 | + /// This frame header's own `reference_select` bit (spec 5.9.22), armed | |
| 309 | + /// the same way `arm_sign_bias` arms the sign-bias table rather than | |
| 310 | + /// threaded through the five nested writer helpers between here and the | |
| 311 | + /// per-block mode syntax. `true` makes every inter block of size at least | |
| 312 | + /// 8x8 code a `comp_mode` symbol, exactly where decode.rs `read_comp_mode` | |
| 313 | + /// reads one. | |
| 314 | + static REFERENCE_SELECT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | |
| 315 | +} | |
| 316 | + | |
| 317 | +/// Arms the frame's `reference_select` bit for the next tile written on this | |
| 318 | +/// thread; cleared by [`CdefIdxGuard`] when that writer returns. | |
| 319 | +pub(crate) fn arm_reference_select(on: bool) { | |
| 320 | + REFERENCE_SELECT.with(|c| c.set(on)); | |
| 321 | +} | |
| 322 | + | |
| 323 | +/// The compound half of a block's end-of-block neighbour record: nothing for | |
| 324 | +/// a single-reference or intra block, and `record_compound_mi`'s overwrite of | |
| 325 | +/// the ALTREF default for a compound one (this writer only codes | |
| 326 | +/// `comp_group_idx == 0`, `compound_idx == 1`). | |
| 327 | +fn record_block_compound(neighbours: &mut Neighbours, at_mi: (usize, usize), side: usize, block: &BlockCoeffs) { | |
| 328 | + if let Some(r1) = block.inter.and_then(|i| i.ref1) { | |
| 329 | + neighbours.record_compound_mi(at_mi, side, r1, 0, 1); | |
| 330 | + } | |
| 331 | +} | |
| 332 | + | |
| 333 | +/// decode.rs' own `is_uni_comp_ref`: a pair is `UNIDIR_COMP_REFERENCE` | |
| 334 | +/// exactly when both references sit on the same temporal side. | |
| 335 | +fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { | |
| 336 | + let backward = | |
| 337 | + |r: i8| (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&r); | |
| 338 | + backward(ref0) == backward(ref1) | |
| 339 | +} | |
| 340 | + | |
| 341 | +thread_local! { | |
| 342 | + /// `(order_hint_bits, this frame's order hint, each reference's order | |
| 343 | + /// hint)` -- what `get_comp_index_context` (decode.rs) needs to context | |
| 344 | + /// the `compound_idx` symbol, armed the same way the sign-bias table is. | |
| 345 | + static ORDER_HINTS: std::cell::Cell<(u32, u32, [u32; 7])> = | |
| 346 | + const { std::cell::Cell::new((7, 0, [0; 7])) }; | |
| 347 | +} | |
| 348 | + | |
| 349 | +/// Arms this frame's order hints for the next tile written on this thread. | |
| 350 | +pub(crate) fn arm_order_hints(bits: u32, order_hint: u32, hints: [u32; 7]) { | |
| 351 | + ORDER_HINTS.with(|c| c.set((bits, order_hint, hints))); | |
| 352 | +} | |
| 353 | + | |
| 354 | +/// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec | |
| 355 | +/// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref` | |
| 356 | +/// trees), symbol for symbol at the same contexts -- which are, as that | |
| 357 | +/// function's own doc records, `single_ref_p1_ctx`..`single_ref_p6_ctx`/ | |
| 358 | +/// `uni_comp_ref_p1_ctx` under libaom's compound names. | |
| 359 | +/// | |
| 360 | +/// # Errors | |
| 361 | +/// A pair this writer never forms (its `ref0` is not `LAST_FRAME`). | |
| 362 | +fn write_compound_ref_frames( | |
| 363 | + enc: &mut SymbolEncoder, | |
| 364 | + cdfs: &mut Cdfs, | |
| 365 | + above: Option<crate::mvstack::NeighbourRef>, | |
| 366 | + left: Option<crate::mvstack::NeighbourRef>, | |
| 367 | + above_ref: i8, | |
| 368 | + left_ref: i8, | |
| 369 | + (ref0, ref1): (i8, i8), | |
| 370 | +) -> Result<()> { | |
| 371 | + use crate::mvstack::{ | |
| 372 | + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, | |
| 373 | + LAST_FRAME, comp_reference_type_ctx, single_ref_p1_ctx, single_ref_p2_ctx, | |
| 374 | + single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, | |
| 375 | + uni_comp_ref_p1_ctx, | |
| 376 | + }; | |
| 377 | + let a = (above_ref > 0).then_some(above_ref); | |
| 378 | + let l = (left_ref > 0).then_some(left_ref); | |
| 379 | + let a1 = above.and_then(|n| n.ref1); | |
| 380 | + let l1 = left.and_then(|n| n.ref1); | |
| 381 | + let unidir = is_uni_comp_ref(ref0, ref1); | |
| 382 | + enc.symbol( | |
| 383 | + usize::from(!unidir), | |
| 384 | + &mut cdfs.comp_ref_type[comp_reference_type_ctx(above, left)], | |
| 385 | + ); | |
| 386 | + if unidir { | |
| 387 | + let both_backward = (ref0, ref1) == (BWDREF_FRAME, ALTREF_FRAME); | |
| 388 | + enc.symbol( | |
| 389 | + usize::from(both_backward), | |
| 390 | + &mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0], | |
| 391 | + ); | |
| 392 | + if both_backward { | |
| 393 | + return Ok(()); | |
| 394 | + } | |
| 395 | + if ref0 != LAST_FRAME { | |
| 396 | + return Err(Error::unsupported( | |
| 397 | + "AV1 tile", | |
| 398 | + "a unidirectional compound pair this writer does not form", | |
| 399 | + )); | |
| 400 | + } | |
| 401 | + let past_last2 = ref1 != LAST2_FRAME; | |
| 402 | + enc.symbol( | |
| 403 | + usize::from(past_last2), | |
| 404 | + &mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1], | |
| 405 | + ); | |
| 406 | + if past_last2 { | |
| 407 | + enc.symbol( | |
| 408 | + usize::from(ref1 == GOLDEN_FRAME), | |
| 409 | + &mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2], | |
| 410 | + ); | |
| 411 | + } | |
| 412 | + return Ok(()); | |
| 413 | + } | |
| 414 | + let far = ref0 == LAST3_FRAME || ref0 == GOLDEN_FRAME; | |
| 415 | + enc.symbol( | |
| 416 | + usize::from(far), | |
| 417 | + &mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0], | |
| 418 | + ); | |
| 419 | + if far { | |
| 420 | + enc.symbol( | |
| 421 | + usize::from(ref0 == GOLDEN_FRAME), | |
| 422 | + &mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2], | |
| 423 | + ); | |
| 424 | + } else { | |
| 425 | + enc.symbol( | |
| 426 | + usize::from(ref0 == LAST2_FRAME), | |
| 427 | + &mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1], | |
| 428 | + ); | |
| 429 | + } | |
| 430 | + let is_altref = ref1 == ALTREF_FRAME; | |
| 431 | + enc.symbol( | |
| 432 | + usize::from(is_altref), | |
| 433 | + &mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0], | |
| 434 | + ); | |
| 435 | + if !is_altref { | |
| 436 | + enc.symbol( | |
| 437 | + usize::from(ref1 == ALTREF2_FRAME), | |
| 438 | + &mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1], | |
| 439 | + ); | |
| 440 | + } | |
| 441 | + Ok(()) | |
| 442 | +} | |
| 443 | + | |
| 444 | +/// Per-mode and per-pair fire counts of [`write_compound_block`], for the | |
| 445 | +/// gate's own census (gate-blind-to-feature): modes indexed by the spec's | |
| 446 | +/// `INTER_COMPOUND_MODES`, pairs by `ref1` (`LAST`..`ALTREF`). | |
| 447 | +static COMPOUND_MODE_HITS: [std::sync::atomic::AtomicUsize; 8] = | |
| 448 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 8]; | |
| 449 | +static COMPOUND_PAIR_HITS: [std::sync::atomic::AtomicUsize; 7] = | |
| 450 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 7]; | |
| 451 | + | |
| 452 | +/// Takes and clears the compound mode histogram. | |
| 453 | +#[cfg(test)] | |
| 454 | +pub(crate) fn take_compound_mode_hits() -> [usize; 8] { | |
| 455 | + std::array::from_fn(|i| COMPOUND_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 456 | +} | |
| 457 | + | |
| 458 | +/// Takes and clears the compound pair histogram, indexed by the SECOND | |
| 459 | +/// reference (`LAST` = 0 .. `ALTREF` = 6). | |
| 460 | +#[cfg(test)] | |
| 461 | +pub(crate) fn take_compound_pair_hits() -> [usize; 7] { | |
| 462 | + std::array::from_fn(|i| COMPOUND_PAIR_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 463 | +} | |
| 464 | + | |
| 465 | +/// Writes one COMPOUND_REFERENCE block's whole mode chain -- the writer side | |
| 466 | +/// of decode.rs' `read_compound_ref_frames` / `read_inter_compound_mode` / | |
| 467 | +/// `assign_compound_mv` / `comp_group_idx` / `compound_idx` sequence, in that | |
| 468 | +/// order. Returns the two motion vectors the decoder derives (what the MI | |
| 469 | +/// grid must record) and whether the mode was a `NEW*` one. | |
| 470 | +/// | |
| 471 | +/// Only the three modes [`InterMode::compound_index`] names are written, and | |
| 472 | +/// only the `comp_group_idx == 0`, `compound_idx == 1` blend (spec | |
| 473 | +/// `COMPOUND_AVERAGE`) -- masked compound and the distance-weighted blend are | |
| 474 | +/// not searched. | |
| 475 | +/// | |
| 476 | +/// # Errors | |
| 477 | +/// As [`write_mv`], or a pair/mode this writer does not form. | |
| 478 | +#[allow(clippy::too_many_arguments)] | |
| 479 | +fn write_compound_block( | |
| 480 | + enc: &mut SymbolEncoder, | |
| 481 | + cdfs: &mut Cdfs, | |
| 482 | + neighbours: &Neighbours, | |
| 483 | + grid: &crate::mvstack::MiGrid, | |
| 484 | + (mi_row, mi_col): (usize, usize), | |
| 485 | + (bw4, bh4): (usize, usize), | |
| 486 | + (has_above, has_left): (bool, bool), | |
| 487 | + info: InterInfo, | |
| 488 | + (mi_cols, mi_rows): (usize, usize), | |
| 489 | +) -> Result<((i32, i32), (i32, i32), bool)> { | |
| 490 | + let (Some(ref1), Some(mode)) = (info.ref1, info.mode.compound_index()) else { | |
| 491 | + return Err(Error::unsupported( | |
| 492 | + "AV1 tile", | |
| 493 | + "a compound block needs a second reference and a compound mode", | |
| 494 | + )); | |
| 495 | + }; | |
| 496 | + let above = has_above.then(|| neighbours.above_nbr(mi_col)); | |
| 497 | + let left = has_left.then(|| neighbours.left_nbr(mi_row)); | |
| 498 | + write_compound_ref_frames( | |
| 499 | + enc, | |
| 500 | + cdfs, | |
| 501 | + above, | |
| 502 | + left, | |
| 503 | + neighbours.above_ref[mi_col], | |
| 504 | + neighbours.left_ref[mi_row], | |
| 505 | + (info.ref_frame, ref1), | |
| 506 | + )?; | |
| 507 | + let stack = crate::mvstack::find_mv_stack_compound( | |
| 508 | + grid, | |
| 509 | + mi_row, | |
| 510 | + mi_col, | |
| 511 | + bw4, | |
| 512 | + bh4, | |
| 513 | + (info.ref_frame, ref1), | |
| 514 | + mi_cols, | |
| 515 | + mi_rows, | |
| 516 | + grid.sign_bias_table(), | |
| 517 | + &[(0, 0); 7], | |
| 518 | + None, | |
| 519 | + ); | |
| 520 | + let ctx = crate::cdf::COMPOUND_MODE_CTX_MAP[stack.ref_mv_ctx >> 1][stack.new_mv_ctx.min(4)]; | |
| 521 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | |
| 522 | + eprintln!( | |
| 523 | + "EC_WCOMP mi_row={mi_row} mi_col={mi_col} mode={mode} ref0={} ref1={ref1} ctx={ctx} new_mv_ctx={} ref_mv_ctx={} stack={}", | |
| 524 | + info.ref_frame, stack.new_mv_ctx, stack.ref_mv_ctx, stack.entries.len() | |
| 525 | + ); | |
| 526 | + } | |
| 527 | + enc.symbol(mode, &mut cdfs.inter_compound_mode[ctx]); | |
| 528 | + // `assign_compound_mv`'s own DRL half: `NEW_NEWMV` walks from index 0, | |
| 529 | + // the two derived modes code no index at all. | |
| 530 | + let mvs = match info.mode { | |
| 531 | + InterMode::NearestNearestMv => stack.nearest_mv, | |
| 532 | + InterMode::GlobalGlobalMv => ((0, 0), (0, 0)), | |
| 533 | + _ => { | |
| 534 | + let idx = usize::from(info.ref_mv_idx); | |
| 535 | + let mut walk = 0usize; | |
| 536 | + while walk < 2 && stack.entries.len() > walk + 1 { | |
| 537 | + let advance = walk < idx; | |
| 538 | + enc.symbol(usize::from(advance), &mut cdfs.drl_mode[stack.drl_ctx[walk]]); | |
| 539 | + if !advance { | |
| 540 | + break; | |
| 541 | + } | |
| 542 | + walk += 1; | |
| 543 | + } | |
| 544 | + let base = stack | |
| 545 | + .entries | |
| 546 | + .get(idx) | |
| 547 | + .map_or(stack.nearest_mv, |e| (e.mv0, e.mv1)); | |
| 548 | + write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv, base.0)?; | |
| 549 | + write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv1, base.1)?; | |
| 550 | + (info.mv, info.mv1) | |
| 551 | + } | |
| 552 | + }; | |
| 553 | + // spec 5.11.25's `comp_group_idx`/`compound_idx` are NOT coded: this | |
| 554 | + // crate's sequence header carries `enable_masked_compound = false` and | |
| 555 | + // `enable_jnt_comp = false` (encode.rs), and decode.rs reads neither | |
| 556 | + // symbol under those bits -- it infers `comp_group_idx = 0`, | |
| 557 | + // `compound_idx = 1` (the simple average), which is the only blend this | |
| 558 | + // writer forms anyway. A lane that turns either sequence bit on has to | |
| 559 | + // write the matching symbol here, at | |
| 560 | + // `get_comp_group_idx_context`/`get_comp_index_context`'s contexts (which | |
| 561 | + // is what `Neighbours::above_comp_group_idx`/`above_compound_idx` and the | |
| 562 | + // armed order hints below are already kept for). | |
| 563 | + let _ = ORDER_HINTS.with(std::cell::Cell::get); | |
| 564 | + COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 565 | + COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7] | |
| 566 | + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 567 | + Ok((mvs.0, mvs.1, matches!(info.mode, InterMode::NewNewMv))) | |
| 568 | +} | |
| 569 | + | |
| 570 | +/// Writer-side counterpart of decode.rs `read_comp_mode` (spec 5.11.25's | |
| 571 | +/// `comp_mode`): on a `reference_select` frame every inter block whose size | |
| 572 | +/// allows compound (`is_comp_ref_allowed`, always true here -- this writer's | |
| 573 | +/// smallest leaf is 8x8) names SINGLE or COMPOUND, at | |
| 574 | +/// `mvstack::reference_mode_ctx`'s own context off the same two neighbour | |
| 575 | +/// bands decode.rs builds its `NeighbourRef` pair from. | |
| 576 | +fn write_comp_mode( | |
| 577 | + enc: &mut SymbolEncoder, | |
| 578 | + cdfs: &mut Cdfs, | |
| 579 | + neighbours: &Neighbours, | |
| 580 | + (mi_r, mi_c): (usize, usize), | |
| 581 | + (has_above, has_left): (bool, bool), | |
| 582 | + compound: bool, | |
| 583 | +) { | |
| 584 | + if !REFERENCE_SELECT.with(std::cell::Cell::get) { | |
| 585 | + return; | |
| 586 | + } | |
| 587 | + let above = has_above.then(|| neighbours.above_nbr(mi_c)); | |
| 588 | + let left = has_left.then(|| neighbours.left_nbr(mi_r)); | |
| 589 | + let ctx = crate::mvstack::reference_mode_ctx(above, left); | |
| 590 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | |
| 591 | + eprintln!( | |
| 592 | + "EC_WCM mi=({mi_r},{mi_c}) ctx={ctx} val={} a={:?} l={:?}", | |
| 593 | + usize::from(compound), | |
| 594 | + above.map(|n| (n.is_inter, n.ref0, n.ref1)), | |
| 595 | + left.map(|n| (n.is_inter, n.ref0, n.ref1)) | |
| 596 | + ); | |
| 597 | + } | |
| 598 | + enc.symbol(usize::from(compound), &mut cdfs.comp_mode[ctx]); | |
| 599 | +} | |
| 600 | + | |
| 307 | 601 | /// Clears the armed plan when the tile writer that consumed it returns. |
| 308 | 602 | struct CdefIdxGuard; |
| 309 | 603 | |
| @@ -312,6 +606,8 @@ impl Drop for CdefIdxGuard { | ||
| 312 | 606 | CDEF_IDX.with(|c| *c.borrow_mut() = None); |
| 313 | 607 | LR_PLAN.with(|c| *c.borrow_mut() = None); |
| 314 | 608 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); |
| 609 | + REFERENCE_SELECT.with(|c| c.set(false)); | |
| 610 | + ORDER_HINTS.with(|c| c.set((7, 0, [0; 7]))); | |
| 315 | 611 | } |
| 316 | 612 | } |
| 317 | 613 | |
| @@ -822,6 +1118,29 @@ pub enum InterMode { | ||
| 822 | 1118 | /// `NEWMV`: codes `mv` as a residual against the stack entry |
| 823 | 1119 | /// [`InterInfo::ref_mv_idx`] names. |
| 824 | 1120 | NewMv, |
| 1121 | + /// `NEAREST_NEARESTMV`: both sides take the compound stack's own nearest | |
| 1122 | + /// pair, no residual, no DRL. | |
| 1123 | + NearestNearestMv, | |
| 1124 | + /// `GLOBAL_GLOBALMV`: both sides take the identity global model's zero | |
| 1125 | + /// vector. | |
| 1126 | + GlobalGlobalMv, | |
| 1127 | + /// `NEW_NEWMV`: both sides code a residual against the compound stack | |
| 1128 | + /// entry [`InterInfo::ref_mv_idx`] names. | |
| 1129 | + NewNewMv, | |
| 1130 | +} | |
| 1131 | + | |
| 1132 | +impl InterMode { | |
| 1133 | + /// The spec's `INTER_COMPOUND_MODES` index (libaom `enums.h` order, | |
| 1134 | + /// `NEAREST_NEARESTMV = 0`..`NEW_NEWMV = 7`) for a compound mode, `None` | |
| 1135 | + /// for a single-reference one. | |
| 1136 | + fn compound_index(self) -> Option<usize> { | |
| 1137 | + match self { | |
| 1138 | + InterMode::NearestNearestMv => Some(0), | |
| 1139 | + InterMode::GlobalGlobalMv => Some(6), | |
| 1140 | + InterMode::NewNewMv => Some(7), | |
| 1141 | + _ => None, | |
| 1142 | + } | |
| 1143 | + } | |
| 825 | 1144 | } |
| 826 | 1145 | |
| 827 | 1146 | /// An inter-coded block's mode and motion vector, `mv` in the spec's 1/8-pel |
| @@ -842,6 +1161,13 @@ pub struct InterInfo { | ||
| 842 | 1161 | /// `NEARMV` takes outright. Ignored by the two modes that code no DRL |
| 843 | 1162 | /// index (`NEARESTMV`, `GLOBALMV`). |
| 844 | 1163 | pub ref_mv_idx: u8, |
| 1164 | + /// The block's second reference (spec `RefFrames[1]`), `None` for a | |
| 1165 | + /// single-reference block. `Some` only on a `reference_select` frame, and | |
| 1166 | + /// only beside one of [`InterMode`]'s compound modes. | |
| 1167 | + pub ref1: Option<i8>, | |
| 1168 | + /// The motion vector into `ref1`, for the compound modes that code one | |
| 1169 | + /// (`NEW_NEWMV`); the other two derive both sides. | |
| 1170 | + pub mv1: (i32, i32), | |
| 845 | 1171 | } |
| 846 | 1172 | |
| 847 | 1173 | impl From<Vec<Coeff>> for BlockCoeffs { |
| @@ -1021,6 +1347,23 @@ struct Neighbours { | ||
| 1021 | 1347 | above_ref: Vec<i8>, |
| 1022 | 1348 | /// The same, down the left edge. |
| 1023 | 1349 | left_ref: Vec<i8>, |
| 1350 | + /// Each neighbour's SECOND reference, `None` for a single-reference or | |
| 1351 | + /// intra one -- decode.rs `Neighbours::above_ref1`, read by every compound | |
| 1352 | + /// context function (`reference_mode_ctx`, `comp_reference_type_ctx`, the | |
| 1353 | + /// `comp_ref`/`comp_bwdref` votes). | |
| 1354 | + above_ref1: Vec<Option<i8>>, | |
| 1355 | + /// The same, down the left edge. | |
| 1356 | + left_ref1: Vec<Option<i8>>, | |
| 1357 | + /// What decode.rs `Neighbours::above_comp_group_idx` holds: a compound | |
| 1358 | + /// neighbour's own `comp_group_idx`, or libaom's single-reference special | |
| 1359 | + /// case (`3` for an ALTREF block, `0` otherwise). | |
| 1360 | + above_comp_group_idx: Vec<u8>, | |
| 1361 | + /// The same, down the left edge. | |
| 1362 | + left_comp_group_idx: Vec<u8>, | |
| 1363 | + /// The same for `compound_idx` (`1` for a single-ref ALTREF block). | |
| 1364 | + above_compound_idx: Vec<u8>, | |
| 1365 | + /// The same, down the left edge. | |
| 1366 | + left_compound_idx: Vec<u8>, | |
| 1024 | 1367 | /// The frame's true (unpadded) width and height, in 4x4 mode-info units, |
| 1025 | 1368 | /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec |
| 1026 | 1369 | /// `av1_set_entropy_contexts`): a block whose row or column run spills |
| @@ -1068,6 +1411,12 @@ impl Neighbours { | ||
| 1068 | 1411 | left_inter: vec![false; rows * (SUB / MI)], |
| 1069 | 1412 | above_ref: vec![-1; cols * (SUB / MI)], |
| 1070 | 1413 | left_ref: vec![-1; rows * (SUB / MI)], |
| 1414 | + above_ref1: vec![None; cols * (SUB / MI)], | |
| 1415 | + left_ref1: vec![None; rows * (SUB / MI)], | |
| 1416 | + above_comp_group_idx: vec![0; cols * (SUB / MI)], | |
| 1417 | + left_comp_group_idx: vec![0; rows * (SUB / MI)], | |
| 1418 | + above_compound_idx: vec![0; cols * (SUB / MI)], | |
| 1419 | + left_compound_idx: vec![0; rows * (SUB / MI)], | |
| 1071 | 1420 | above_tx: vec![0; cols * (SUB / MI)], |
| 1072 | 1421 | left_tx: vec![0; rows * (SUB / MI)], |
| 1073 | 1422 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| @@ -1103,6 +1452,9 @@ impl Neighbours { | ||
| 1103 | 1452 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 1104 | 1453 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 1105 | 1454 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 1455 | + self.left_ref1.iter_mut().for_each(|r| *r = None); | |
| 1456 | + self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); | |
| 1457 | + self.left_compound_idx.iter_mut().for_each(|r| *r = 0); | |
| 1106 | 1458 | self.left_tx.iter_mut().for_each(|t| *t = 0); |
| 1107 | 1459 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 1108 | 1460 | } |
| @@ -1307,6 +1659,29 @@ impl Neighbours { | ||
| 1307 | 1659 | self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame); |
| 1308 | 1660 | } |
| 1309 | 1661 | |
| 1662 | + /// [`Self::record_inter_mi`]'s compound half (decode.rs | |
| 1663 | + /// `Neighbours::record_compound_ctx_rect_mi`): overwrites the ALTREF | |
| 1664 | + /// single-reference default with a real compound block's own second | |
| 1665 | + /// reference and `comp_group_idx`/`compound_idx` bits, called right after | |
| 1666 | + /// it for every compound-coded block. | |
| 1667 | + fn record_compound_mi( | |
| 1668 | + &mut self, | |
| 1669 | + (mi_r, mi_c): (usize, usize), | |
| 1670 | + side: usize, | |
| 1671 | + ref1: i8, | |
| 1672 | + comp_group_idx: u8, | |
| 1673 | + compound_idx: u8, | |
| 1674 | + ) { | |
| 1675 | + for cell in 0..side / MI { | |
| 1676 | + self.above_ref1[mi_c + cell] = Some(ref1); | |
| 1677 | + self.left_ref1[mi_r + cell] = Some(ref1); | |
| 1678 | + self.above_comp_group_idx[mi_c + cell] = comp_group_idx; | |
| 1679 | + self.left_comp_group_idx[mi_r + cell] = comp_group_idx; | |
| 1680 | + self.above_compound_idx[mi_c + cell] = compound_idx; | |
| 1681 | + self.left_compound_idx[mi_r + cell] = compound_idx; | |
| 1682 | + } | |
| 1683 | + } | |
| 1684 | + | |
| 1310 | 1685 | /// [`Self::record_inter`] taking the block's position directly in 4x4 |
| 1311 | 1686 | /// mode-info units, for a block finer than one [`SUB`] slot. |
| 1312 | 1687 | fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) { |
| @@ -1317,6 +1692,45 @@ impl Neighbours { | ||
| 1317 | 1692 | self.left_inter[mi_r + cell] = is_inter; |
| 1318 | 1693 | self.above_ref[mi_c + cell] = ref_frame; |
| 1319 | 1694 | self.left_ref[mi_r + cell] = ref_frame; |
| 1695 | + // decode.rs `record_inter`'s single-reference default: no second | |
| 1696 | + // reference, and libaom's ALTREF special case in the two compound | |
| 1697 | + // context bands. `record_compound_mi` overwrites both for a real | |
| 1698 | + // compound block. | |
| 1699 | + let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; | |
| 1700 | + self.above_ref1[mi_c + cell] = None; | |
| 1701 | + self.left_ref1[mi_r + cell] = None; | |
| 1702 | + self.above_comp_group_idx[mi_c + cell] = if altref { 3 } else { 0 }; | |
| 1703 | + self.left_comp_group_idx[mi_r + cell] = if altref { 3 } else { 0 }; | |
| 1704 | + self.above_compound_idx[mi_c + cell] = u8::from(altref); | |
| 1705 | + self.left_compound_idx[mi_r + cell] = u8::from(altref); | |
| 1706 | + } | |
| 1707 | + } | |
| 1708 | + | |
| 1709 | + /// The above neighbour as decode.rs' own `NeighbourRef`, for every | |
| 1710 | + /// compound context function (`reference_mode_ctx`, | |
| 1711 | + /// `comp_reference_type_ctx`, the `comp_ref`/`comp_bwdref` votes). | |
| 1712 | + fn above_nbr(&self, mi_c: usize) -> crate::mvstack::NeighbourRef { | |
| 1713 | + let is_inter = self.above_inter[mi_c]; | |
| 1714 | + let ref0 = if is_inter { self.above_ref[mi_c] } else { 0 }; | |
| 1715 | + let ref1 = self.above_ref1[mi_c]; | |
| 1716 | + crate::mvstack::NeighbourRef { | |
| 1717 | + is_inter, | |
| 1718 | + ref0, | |
| 1719 | + ref1, | |
| 1720 | + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), | |
| 1721 | + } | |
| 1722 | + } | |
| 1723 | + | |
| 1724 | + /// [`Self::above_nbr`], down the left edge. | |
| 1725 | + fn left_nbr(&self, mi_r: usize) -> crate::mvstack::NeighbourRef { | |
| 1726 | + let is_inter = self.left_inter[mi_r]; | |
| 1727 | + let ref0 = if is_inter { self.left_ref[mi_r] } else { 0 }; | |
| 1728 | + let ref1 = self.left_ref1[mi_r]; | |
| 1729 | + crate::mvstack::NeighbourRef { | |
| 1730 | + is_inter, | |
| 1731 | + ref0, | |
| 1732 | + ref1, | |
| 1733 | + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), | |
| 1320 | 1734 | } |
| 1321 | 1735 | } |
| 1322 | 1736 | |
| @@ -3119,7 +3533,9 @@ fn write_single_ref( | ||
| 3119 | 3533 | cdfs: &mut Cdfs, |
| 3120 | 3534 | ref_frame: i8, |
| 3121 | 3535 | above_ref: i8, |
| 3536 | + above_ref1: Option<i8>, | |
| 3122 | 3537 | left_ref: i8, |
| 3538 | + left_ref1: Option<i8>, | |
| 3123 | 3539 | ) { |
| 3124 | 3540 | REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 3125 | 3541 | use crate::mvstack::{ |
| @@ -3129,21 +3545,26 @@ fn write_single_ref( | ||
| 3129 | 3545 | }; |
| 3130 | 3546 | let above = (above_ref > 0).then_some(above_ref); |
| 3131 | 3547 | let left = (left_ref > 0).then_some(left_ref); |
| 3548 | + // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound | |
| 3549 | + // neighbour's references (decode.rs `read_single_ref`'s own | |
| 3550 | + // `above_ref1`/`left_ref1`): dropping the second one reads a different | |
| 3551 | + // context row for every block sitting under a compound neighbour, and so | |
| 3552 | + // decodes a different reference NAME. | |
| 3132 | 3553 | let backward = ref_frame >= BWDREF_FRAME; |
| 3133 | 3554 | enc.symbol( |
| 3134 | 3555 | usize::from(backward), |
| 3135 | - &mut cdfs.single_ref[single_ref_p1_ctx(above, None, left, None)][0], | |
| 3556 | + &mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0], | |
| 3136 | 3557 | ); |
| 3137 | 3558 | if backward { |
| 3138 | 3559 | let is_altref = ref_frame == ALTREF_FRAME; |
| 3139 | 3560 | enc.symbol( |
| 3140 | 3561 | usize::from(is_altref), |
| 3141 | - &mut cdfs.single_ref[single_ref_p2_ctx(above, None, left, None)][1], | |
| 3562 | + &mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1], | |
| 3142 | 3563 | ); |
| 3143 | 3564 | if !is_altref { |
| 3144 | 3565 | enc.symbol( |
| 3145 | 3566 | usize::from(ref_frame == ALTREF2_FRAME), |
| 3146 | - &mut cdfs.single_ref[single_ref_p6_ctx(above, None, left, None)][5], | |
| 3567 | + &mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5], | |
| 3147 | 3568 | ); |
| 3148 | 3569 | } |
| 3149 | 3570 | return; |
| @@ -3151,17 +3572,17 @@ fn write_single_ref( | ||
| 3151 | 3572 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; |
| 3152 | 3573 | enc.symbol( |
| 3153 | 3574 | usize::from(far), |
| 3154 | - &mut cdfs.single_ref[single_ref_p3_ctx(above, None, left, None)][2], | |
| 3575 | + &mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2], | |
| 3155 | 3576 | ); |
| 3156 | 3577 | if far { |
| 3157 | 3578 | enc.symbol( |
| 3158 | 3579 | usize::from(ref_frame == GOLDEN_FRAME), |
| 3159 | - &mut cdfs.single_ref[single_ref_p5_ctx(above, None, left, None)][4], | |
| 3580 | + &mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4], | |
| 3160 | 3581 | ); |
| 3161 | 3582 | } else { |
| 3162 | 3583 | enc.symbol( |
| 3163 | 3584 | usize::from(ref_frame == LAST2_FRAME), |
| 3164 | - &mut cdfs.single_ref[single_ref_p4_ctx(above, None, left, None)][3], | |
| 3585 | + &mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3], | |
| 3165 | 3586 | ); |
| 3166 | 3587 | } |
| 3167 | 3588 | } |
| @@ -3265,6 +3686,12 @@ fn write_inter_mode( | ||
| 3265 | 3686 | stack: &crate::mvstack::MvStack, |
| 3266 | 3687 | ) -> Result<((i32, i32), bool)> { |
| 3267 | 3688 | let idx = usize::from(info.ref_mv_idx); |
| 3689 | + if info.mode.compound_index().is_some() { | |
| 3690 | + return Err(Error::unsupported( | |
| 3691 | + "AV1 tile", | |
| 3692 | + "a compound mode is written by `write_compound_block`, not here", | |
| 3693 | + )); | |
| 3694 | + } | |
| 3268 | 3695 | match info.mode { |
| 3269 | 3696 | InterMode::NewMv => { |
| 3270 | 3697 | enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); |
| @@ -3298,6 +3725,7 @@ fn write_inter_mode( | ||
| 3298 | 3725 | note_inter_mode(1, idx); |
| 3299 | 3726 | Ok((mv, false)) |
| 3300 | 3727 | } |
| 3728 | + _ => unreachable!("compound modes are refused above"), | |
| 3301 | 3729 | } |
| 3302 | 3730 | } |
| 3303 | 3731 | |
| @@ -3727,9 +4155,33 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | ||
| 3727 | 4155 | |
| 3728 | 4156 | let mode_for_tx; |
| 3729 | 4157 | if let Some(info) = block.inter { |
| 3730 | - write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | |
| 3731 | - | |
| 4158 | + write_comp_mode(&mut enc, &mut cdfs, &neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some()); | |
| 3732 | 4159 | let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); |
| 4160 | + if info.ref1.is_some() { | |
| 4161 | + let (mv, mv1, is_new_mv) = write_compound_block( | |
| 4162 | + &mut enc, &mut cdfs, &neighbours, &grid, (mi_row, mi_col), (8, 8), | |
| 4163 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | |
| 4164 | + )?; | |
| 4165 | + for dr in 0..8 { | |
| 4166 | + for dc in 0..8 { | |
| 4167 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | |
| 4168 | + is_inter: true, | |
| 4169 | + ref_frame: info.ref_frame, | |
| 4170 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | |
| 4171 | + mv1: mv16(mv1), | |
| 4172 | + mv: mv16(mv), | |
| 4173 | + is_new_mv, | |
| 4174 | + size: 8, | |
| 4175 | + size_h: 8, | |
| 4176 | + is_global_mv0: false, | |
| 4177 | + is_global_mv1: false, | |
| 4178 | + }); | |
| 4179 | + } | |
| 4180 | + } | |
| 4181 | + mode_for_tx = 0; | |
| 4182 | + } else { | |
| 4183 | + 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]); | |
| 4184 | + | |
| 3733 | 4185 | let stack = find_mv_stack( |
| 3734 | 4186 | &grid, |
| 3735 | 4187 | mi_row, |
| @@ -3782,6 +4234,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | ||
| 3782 | 4234 | } |
| 3783 | 4235 | } |
| 3784 | 4236 | mode_for_tx = 0; |
| 4237 | + } | |
| 3785 | 4238 | } else { |
| 3786 | 4239 | let mode = usize::from(block.mode); |
| 3787 | 4240 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]); |
| @@ -3874,6 +4327,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | ||
| 3874 | 4327 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); |
| 3875 | 4328 | } |
| 3876 | 4329 | neighbours.record_inter(at, BLOCK, block.skip, is_inter, block_ref(block)); |
| 4330 | + record_block_compound(&mut neighbours, (mi_r, mi_c), BLOCK, block); | |
| 3877 | 4331 | } |
| 3878 | 4332 | } |
| 3879 | 4333 | } |
| @@ -3932,9 +4386,34 @@ fn write_inter_frame_leaf( | ||
| 3932 | 4386 | |
| 3933 | 4387 | let mode_for_tx; |
| 3934 | 4388 | if let Some(info) = block.inter { |
| 3935 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | |
| 3936 | - | |
| 4389 | + write_comp_mode(enc, cdfs, neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some()); | |
| 3937 | 4390 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 4391 | + if info.ref1.is_some() { | |
| 4392 | + let n = SUB_MI as usize; | |
| 4393 | + let (mv, mv1, is_new_mv) = write_compound_block( | |
| 4394 | + enc, cdfs, neighbours, grid, (mi_row, mi_col), (n, n), | |
| 4395 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | |
| 4396 | + )?; | |
| 4397 | + for dr in 0..n { | |
| 4398 | + for dc in 0..n { | |
| 4399 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | |
| 4400 | + is_inter: true, | |
| 4401 | + ref_frame: info.ref_frame, | |
| 4402 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | |
| 4403 | + mv1: mv16(mv1), | |
| 4404 | + mv: mv16(mv), | |
| 4405 | + is_new_mv, | |
| 4406 | + size: n as u8, | |
| 4407 | + size_h: n as u8, | |
| 4408 | + is_global_mv0: false, | |
| 4409 | + is_global_mv1: false, | |
| 4410 | + }); | |
| 4411 | + } | |
| 4412 | + } | |
| 4413 | + mode_for_tx = 0; | |
| 4414 | + } else { | |
| 4415 | + 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]); | |
| 4416 | + | |
| 3938 | 4417 | let stack = find_mv_stack( |
| 3939 | 4418 | grid, |
| 3940 | 4419 | mi_row, |
| @@ -3968,6 +4447,7 @@ fn write_inter_frame_leaf( | ||
| 3968 | 4447 | } |
| 3969 | 4448 | } |
| 3970 | 4449 | mode_for_tx = 0; |
| 4450 | + } | |
| 3971 | 4451 | } else { |
| 3972 | 4452 | let mode = usize::from(block.mode); |
| 3973 | 4453 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); |
| @@ -4056,6 +4536,7 @@ fn write_inter_frame_leaf( | ||
| 4056 | 4536 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); |
| 4057 | 4537 | } |
| 4058 | 4538 | neighbours.record_inter(at, SUB, block.skip, is_inter, block_ref(block)); |
| 4539 | + record_block_compound(neighbours, (mi_r, mi_c), SUB, block); | |
| 4059 | 4540 | Ok(()) |
| 4060 | 4541 | } |
| 4061 | 4542 | |
| @@ -4125,9 +4606,33 @@ fn write_inter_frame_leaf8( | ||
| 4125 | 4606 | |
| 4126 | 4607 | let mode_for_tx; |
| 4127 | 4608 | if let Some(info) = block.inter { |
| 4128 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.left_ref[leaf_mi.0]); | |
| 4129 | - | |
| 4609 | + write_comp_mode(enc, cdfs, neighbours, leaf_mi, (has_above, has_left), info.ref1.is_some()); | |
| 4130 | 4610 | let (mi_row, mi_col) = leaf_mi; |
| 4611 | + if info.ref1.is_some() { | |
| 4612 | + let (mv, mv1, is_new_mv) = write_compound_block( | |
| 4613 | + enc, cdfs, neighbours, grid, (mi_row, mi_col), (2, 2), | |
| 4614 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | |
| 4615 | + )?; | |
| 4616 | + for dr in 0..2 { | |
| 4617 | + for dc in 0..2 { | |
| 4618 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | |
| 4619 | + is_inter: true, | |
| 4620 | + ref_frame: info.ref_frame, | |
| 4621 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | |
| 4622 | + mv1: mv16(mv1), | |
| 4623 | + mv: mv16(mv), | |
| 4624 | + is_new_mv, | |
| 4625 | + size: 2, | |
| 4626 | + size_h: 2, | |
| 4627 | + is_global_mv0: false, | |
| 4628 | + is_global_mv1: false, | |
| 4629 | + }); | |
| 4630 | + } | |
| 4631 | + } | |
| 4632 | + mode_for_tx = 0; | |
| 4633 | + } else { | |
| 4634 | + 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]); | |
| 4635 | + | |
| 4131 | 4636 | let stack = find_mv_stack( |
| 4132 | 4637 | grid, |
| 4133 | 4638 | mi_row, |
| @@ -4161,6 +4666,7 @@ fn write_inter_frame_leaf8( | ||
| 4161 | 4666 | } |
| 4162 | 4667 | } |
| 4163 | 4668 | mode_for_tx = 0; |
| 4669 | + } | |
| 4164 | 4670 | } else { |
| 4165 | 4671 | let mode = usize::from(block.mode); |
| 4166 | 4672 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]); |
| @@ -4226,6 +4732,7 @@ fn write_inter_frame_leaf8( | ||
| 4226 | 4732 | ]; |
| 4227 | 4733 | neighbours.record_mi(leaf_mi, 8, &zero_grids); |
| 4228 | 4734 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some(), block_ref(block)); |
| 4735 | + record_block_compound(neighbours, leaf_mi, 8, block); | |
| 4229 | 4736 | } else { |
| 4230 | 4737 | let grids = [ |
| 4231 | 4738 | level_grid(&block.luma, TX8)?, |
| @@ -4249,6 +4756,7 @@ fn write_inter_frame_leaf8( | ||
| 4249 | 4756 | ); |
| 4250 | 4757 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); |
| 4251 | 4758 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); |
| 4759 | + record_block_compound(neighbours, leaf_mi, 8, block); | |
| 4252 | 4760 | } |
| 4253 | 4761 | Ok((block.skip, is_inter)) |
| 4254 | 4762 | } |
| @@ -6530,6 +7038,8 @@ mod tests { | ||
| 6530 | 7038 | fn an_all_skip_inter_superblock_is_small_and_reads_back() { |
| 6531 | 7039 | let block = BlockCoeffs { |
| 6532 | 7040 | inter: Some(InterInfo { |
| 7041 | + ref1: None, | |
| 7042 | + mv1: (0, 0), | |
| 6533 | 7043 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6534 | 7044 | mode: InterMode::NearestMv, |
| 6535 | 7045 | mv: (0, 0), |
| @@ -6573,6 +7083,8 @@ mod tests { | ||
| 6573 | 7083 | fn a_newmv_block_reads_back_the_exact_symbol_sequence_it_was_written_with() { |
| 6574 | 7084 | let nearest = BlockCoeffs { |
| 6575 | 7085 | inter: Some(InterInfo { |
| 7086 | + ref1: None, | |
| 7087 | + mv1: (0, 0), | |
| 6576 | 7088 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6577 | 7089 | mode: InterMode::NearestMv, |
| 6578 | 7090 | mv: (0, 0), |
| @@ -6583,6 +7095,8 @@ mod tests { | ||
| 6583 | 7095 | }; |
| 6584 | 7096 | let newmv = BlockCoeffs { |
| 6585 | 7097 | inter: Some(InterInfo { |
| 7098 | + ref1: None, | |
| 7099 | + mv1: (0, 0), | |
| 6586 | 7100 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6587 | 7101 | mode: InterMode::NewMv, |
| 6588 | 7102 | mv: (0, 2), |