wip(av1): palette lane snapshot at the agent cap
Browse files at this commit6 changed files +940 -29
- 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 +54 -5
- crates/ec-av1/src/mvstack.rs +8 -0
- crates/ec-av1/src/tile.rs +525 -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
| @@ -2934,6 +2934,8 @@ fn code_square_inter( | |||
| 2934 | palette: None, | 2934 | palette: None, |
| 2935 | tx_depth, | 2935 | tx_depth, |
| 2936 | inter: Some(InterInfo { | 2936 | inter: Some(InterInfo { |
| 2937 | + ref1: None, | ||
| 2938 | + mv1: (0, 0), | ||
| 2937 | ref_frame: crate::mvstack::LAST_FRAME, | 2939 | ref_frame: crate::mvstack::LAST_FRAME, |
| 2938 | mode: InterMode::NewMv, | 2940 | mode: InterMode::NewMv, |
| 2939 | mv: new_mv, | 2941 | mv: new_mv, |
| @@ -2969,6 +2971,8 @@ fn code_square_inter( | |||
| 2969 | palette: None, | 2971 | palette: None, |
| 2970 | tx_depth, | 2972 | tx_depth, |
| 2971 | inter: Some(InterInfo { | 2973 | inter: Some(InterInfo { |
| 2974 | + ref1: None, | ||
| 2975 | + mv1: (0, 0), | ||
| 2972 | ref_frame: crate::mvstack::LAST_FRAME, | 2976 | ref_frame: crate::mvstack::LAST_FRAME, |
| 2973 | mode: InterMode::NearestMv, | 2977 | mode: InterMode::NearestMv, |
| 2974 | mv, | 2978 | mv, |
| @@ -3999,6 +4003,38 @@ fn extra_ref_new_mv() -> bool { | |||
| 3999 | } | 4003 | } |
| 4000 | } | 4004 | } |
| 4001 | 4005 | ||
| 4006 | +/// The seven `ref_frame_idx` order hints of a frame coded on this crate's own | ||
| 4007 | +/// flat slot plan (`inter_frame_headers_slots`): every reference but | ||
| 4008 | +/// `GOLDEN_FRAME` and `ALTREF_FRAME` reads the frame just coded, | ||
| 4009 | +/// `GOLDEN_FRAME` reads the GOP's key frame, and `ALTREF_FRAME` reads the | ||
| 4010 | +/// frame two back when there is one (otherwise it, too, names the key frame's | ||
| 4011 | +/// slot). Hints are 7-bit, the only `order_hint_bits` this encoder writes. | ||
| 4012 | +pub(crate) fn flat_order_hints(order_hint: u32, key_hint: u32, has_altref: bool) -> [u32; 7] { | ||
| 4013 | + let last = order_hint.wrapping_sub(1) & 0x7f; | ||
| 4014 | + let alt = if has_altref { | ||
| 4015 | + order_hint.wrapping_sub(2) & 0x7f | ||
| 4016 | + } else { | ||
| 4017 | + key_hint | ||
| 4018 | + }; | ||
| 4019 | + [last, last, last, key_hint, last, last, alt] | ||
| 4020 | +} | ||
| 4021 | + | ||
| 4022 | +/// Whether an inter frame carries `reference_select` (spec 5.9.22): the | ||
| 4023 | +/// header bit that lets each block choose between a single and a compound | ||
| 4024 | +/// reference. Off by default until compound prediction is measured to pay; | ||
| 4025 | +/// `EC_AV1_COMPOUND=1` arms it in a test build (which is what the BD gate | ||
| 4026 | +/// runs as), and every block still codes SINGLE, so the only cost is one | ||
| 4027 | +/// `comp_mode` symbol per inter block. | ||
| 4028 | +const REFERENCE_SELECT: bool = true; | ||
| 4029 | + | ||
| 4030 | +/// [`REFERENCE_SELECT`], with the test-build environment override. | ||
| 4031 | +pub(crate) fn reference_select() -> bool { | ||
| 4032 | + match std::env::var("EC_AV1_COMPOUND").ok() { | ||
| 4033 | + Some(v) if cfg!(test) => v != "0", | ||
| 4034 | + _ => REFERENCE_SELECT, | ||
| 4035 | + } | ||
| 4036 | +} | ||
| 4037 | + | ||
| 4002 | /// [`tile::write_drl_idx`]'s cost against one stack: one `drl_mode` symbol | 4038 | /// [`tile::write_drl_idx`]'s cost against one stack: one `drl_mode` symbol |
| 4003 | /// per entry past `start`, `1` to advance and `0` to stop, at most two. | 4039 | /// per entry past `start`, `1` to advance and `0` to stop, at most two. |
| 4004 | fn drl_bits_for(stack: &MvStack, start: usize, target: usize) -> f64 { | 4040 | fn drl_bits_for(stack: &MvStack, start: usize, target: usize) -> f64 { |
| @@ -4143,6 +4179,56 @@ fn mc_trial( | |||
| 4143 | plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set) | 4179 | plane.code_from_prediction(x, y, side, prediction, skip, base_q_idx, deadzone, set) |
| 4144 | } | 4180 | } |
| 4145 | 4181 | ||
| 4182 | +/// [`mc_trial`]'s compound counterpart (spec 7.11.3.1's compound path): both | ||
| 4183 | +/// references predict into the `CONV_BUF` intermediate domain | ||
| 4184 | +/// ([`mc::predict_compound_intermediate`]) and are blended by | ||
| 4185 | +/// [`mc::combine_compound`] at the simple-average split `(8, 8)` -- exactly | ||
| 4186 | +/// what a decoder computes for a `comp_group_idx == 0`, `compound_idx == 1` | ||
| 4187 | +/// block, so the encoder's reconstruction is the decoder's. | ||
| 4188 | +#[allow(clippy::too_many_arguments)] | ||
| 4189 | +fn mc_trial_compound( | ||
| 4190 | + plane: &Plane, | ||
| 4191 | + x: usize, | ||
| 4192 | + y: usize, | ||
| 4193 | + side: usize, | ||
| 4194 | + (mv0, mv1): ((i32, i32), (i32, i32)), | ||
| 4195 | + luma: bool, | ||
| 4196 | + ref0: (&[u16], usize, usize, usize), | ||
| 4197 | + ref1: (&[u16], usize, usize, usize), | ||
| 4198 | + base_q_idx: u8, | ||
| 4199 | + deadzone: f64, | ||
| 4200 | + set: TxbSet, fctx: &crate::decode::FrameCtx, | ||
| 4201 | +) -> Trial { | ||
| 4202 | + let mut inter0 = [0i32; BLOCK * BLOCK]; | ||
| 4203 | + let mut inter1 = [0i32; BLOCK * BLOCK]; | ||
| 4204 | + let (inter0, inter1) = (&mut inter0[..side * side], &mut inter1[..side * side]); | ||
| 4205 | + for (mv, r, dst) in [(mv0, ref0, &mut *inter0), (mv1, ref1, &mut *inter1)] { | ||
| 4206 | + mc::predict_compound_intermediate( | ||
| 4207 | + r.0, | ||
| 4208 | + r.1, | ||
| 4209 | + r.2, | ||
| 4210 | + r.3, | ||
| 4211 | + mv_to_q4(x, mv.1, luma), | ||
| 4212 | + mv_to_q4(y, mv.0, luma), | ||
| 4213 | + mc::REF_NO_SCALE, | ||
| 4214 | + side, | ||
| 4215 | + side, | ||
| 4216 | + mc::InterpFilterKind::Regular, | ||
| 4217 | + mc::InterpFilterKind::Regular, | ||
| 4218 | + dst, | ||
| 4219 | + ); | ||
| 4220 | + } | ||
| 4221 | + let mut blended16 = [0u16; BLOCK * BLOCK]; | ||
| 4222 | + let blended16 = &mut blended16[..side * side]; | ||
| 4223 | + mc::combine_compound(inter0, inter1, 8, 8, blended16, fctx); | ||
| 4224 | + let mut prediction = [0u8; BLOCK * BLOCK]; | ||
| 4225 | + let prediction = &mut prediction[..side * side]; | ||
| 4226 | + for (dst, &src) in prediction.iter_mut().zip(blended16.iter()) { | ||
| 4227 | + *dst = src as u8; | ||
| 4228 | + } | ||
| 4229 | + plane.code_from_prediction(x, y, side, prediction, false, base_q_idx, deadzone, set) | ||
| 4230 | +} | ||
| 4231 | + | ||
| 4146 | /// Codes one 32x32 block of an inter frame as whichever costs least of: each | 4232 | /// Codes one 32x32 block of an inter frame as whichever costs least of: each |
| 4147 | /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's | 4233 | /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's |
| 4148 | /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by | 4234 | /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by |
| @@ -4175,7 +4261,11 @@ fn search_inter_block( | |||
| 4175 | // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a | 4261 | // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a |
| 4176 | // second motion search would double the wall of the stage that already | 4262 | // second motion search would double the wall of the stage that already |
| 4177 | // owns most of it. | 4263 | // owns most of it. |
| 4178 | - extra: &[(i8, &Picture, &MvStack)], fctx: &crate::decode::FrameCtx, | 4264 | + extra: &[(i8, &Picture, &MvStack)], |
| 4265 | + // The COMPOUND stack of each `LAST_FRAME` + extra-reference pair, built by | ||
| 4266 | + // the caller off the same MI grid the tile writer will (empty unless the | ||
| 4267 | + // frame carries `reference_select`). | ||
| 4268 | + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], fctx: &crate::decode::FrameCtx, | ||
| 4179 | ) -> (BlockCoeffs, f64) { | 4269 | ) -> (BlockCoeffs, f64) { |
| 4180 | // `luma_set` is the INTRA candidates' table; the two inter candidates | 4270 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 4181 | // below price their coefficients through `TxbSet::Luma32Inter`, which is | 4271 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| @@ -4352,6 +4442,8 @@ fn search_inter_block( | |||
| 4352 | stack.nearest_mv, | 4442 | stack.nearest_mv, |
| 4353 | last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), | 4443 | last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), |
| 4354 | InterInfo { | 4444 | InterInfo { |
| 4445 | + ref1: None, | ||
| 4446 | + mv1: (0, 0), | ||
| 4355 | ref_frame: crate::mvstack::LAST_FRAME, | 4447 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4356 | mode: InterMode::NearestMv, | 4448 | mode: InterMode::NearestMv, |
| 4357 | mv: stack.nearest_mv, | 4449 | mv: stack.nearest_mv, |
| @@ -4365,6 +4457,8 @@ fn search_inter_block( | |||
| 4365 | (0, 0), | 4457 | (0, 0), |
| 4366 | last_ref_bits + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), | 4458 | last_ref_bits + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), |
| 4367 | InterInfo { | 4459 | InterInfo { |
| 4460 | + ref1: None, | ||
| 4461 | + mv1: (0, 0), | ||
| 4368 | ref_frame: crate::mvstack::LAST_FRAME, | 4462 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4369 | mode: InterMode::GlobalMv, | 4463 | mode: InterMode::GlobalMv, |
| 4370 | mv: (0, 0), | 4464 | mv: (0, 0), |
| @@ -4380,6 +4474,8 @@ fn search_inter_block( | |||
| 4380 | e.mv, | 4474 | e.mv, |
| 4381 | near_bits + drl_bits(1, idx), | 4475 | near_bits + drl_bits(1, idx), |
| 4382 | InterInfo { | 4476 | InterInfo { |
| 4477 | + ref1: None, | ||
| 4478 | + mv1: (0, 0), | ||
| 4383 | ref_frame: crate::mvstack::LAST_FRAME, | 4479 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4384 | mode: InterMode::NearMv, | 4480 | mode: InterMode::NearMv, |
| 4385 | mv: e.mv, | 4481 | mv: e.mv, |
| @@ -4418,6 +4514,8 @@ fn search_inter_block( | |||
| 4418 | mv, | 4514 | mv, |
| 4419 | last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, | 4515 | last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, |
| 4420 | InterInfo { | 4516 | InterInfo { |
| 4517 | + ref1: None, | ||
| 4518 | + mv1: (0, 0), | ||
| 4421 | ref_frame: crate::mvstack::LAST_FRAME, | 4519 | ref_frame: crate::mvstack::LAST_FRAME, |
| 4422 | mode: InterMode::NewMv, | 4520 | mode: InterMode::NewMv, |
| 4423 | mv, | 4521 | mv, |
| @@ -4426,6 +4524,9 @@ fn search_inter_block( | |||
| 4426 | )); | 4524 | )); |
| 4427 | } | 4525 | } |
| 4428 | 4526 | ||
| 4527 | + // Each extra reference's own `NEWMV` vector, for the compound | ||
| 4528 | + // `NEW_NEWMV` candidate below to pair with `LAST`'s. | ||
| 4529 | + let mut extra_new_mvs: Vec<(i8, (i32, i32))> = Vec::new(); | ||
| 4429 | for &(ref_frame, g, gstack) in extra { | 4530 | for &(ref_frame, g, gstack) in extra { |
| 4430 | let g_ref_bits = single_ref_bits(ref_frame); | 4531 | let g_ref_bits = single_ref_bits(ref_frame); |
| 4431 | let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); | 4532 | let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); |
| @@ -4436,6 +4537,8 @@ fn search_inter_block( | |||
| 4436 | + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) | 4537 | + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) |
| 4437 | + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), | 4538 | + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), |
| 4438 | InterInfo { | 4539 | InterInfo { |
| 4540 | + ref1: None, | ||
| 4541 | + mv1: (0, 0), | ||
| 4439 | ref_frame, | 4542 | ref_frame, |
| 4440 | mode: InterMode::NearestMv, | 4543 | mode: InterMode::NearestMv, |
| 4441 | mv: gstack.nearest_mv, | 4544 | mv: gstack.nearest_mv, |
| @@ -4446,6 +4549,8 @@ fn search_inter_block( | |||
| 4446 | (0, 0), | 4549 | (0, 0), |
| 4447 | g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), | 4550 | g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), |
| 4448 | InterInfo { | 4551 | InterInfo { |
| 4552 | + ref1: None, | ||
| 4553 | + mv1: (0, 0), | ||
| 4449 | ref_frame, | 4554 | ref_frame, |
| 4450 | mode: InterMode::GlobalMv, | 4555 | mode: InterMode::GlobalMv, |
| 4451 | mv: (0, 0), | 4556 | mv: (0, 0), |
| @@ -4500,11 +4605,14 @@ fn search_inter_block( | |||
| 4500 | #[cfg(test)] | 4605 | #[cfg(test)] |
| 4501 | stage_add(0, t.elapsed()); | 4606 | stage_add(0, t.elapsed()); |
| 4502 | let gmv = round_to_valid_mv(gfound.mv, gstack.pred_mv); | 4607 | let gmv = round_to_valid_mv(gfound.mv, gstack.pred_mv); |
| 4608 | + extra_new_mvs.push((ref_frame, gmv)); | ||
| 4503 | if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(gstack, gmv) { | 4609 | if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(gstack, gmv) { |
| 4504 | cands.push(( | 4610 | cands.push(( |
| 4505 | gmv, | 4611 | gmv, |
| 4506 | g_ref_bits + symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 0) + bits, | 4612 | g_ref_bits + symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 0) + bits, |
| 4507 | InterInfo { | 4613 | InterInfo { |
| 4614 | + ref1: None, | ||
| 4615 | + mv1: (0, 0), | ||
| 4508 | ref_frame, | 4616 | ref_frame, |
| 4509 | mode: InterMode::NewMv, | 4617 | mode: InterMode::NewMv, |
| 4510 | mv: gmv, | 4618 | mv: gmv, |
| @@ -4515,6 +4623,130 @@ fn search_inter_block( | |||
| 4515 | } | 4623 | } |
| 4516 | } | 4624 | } |
| 4517 | 4625 | ||
| 4626 | + // The compound candidates (spec 5.11.24's `INTER_COMPOUND_MODES`), one | ||
| 4627 | + // trial set each: both references predict the block and the two | ||
| 4628 | + // predictions are averaged, which is what pays on content where neither | ||
| 4629 | + // reference alone is right. Priced with the same static-CDF approximation | ||
| 4630 | + // the single-reference candidates use, plus the compound-only symbols | ||
| 4631 | + // (`comp_mode`, the reference-pair tree, `comp_group_idx`/`compound_idx`). | ||
| 4632 | + for (ref1, g, cstack) in compound { | ||
| 4633 | + let (ref1, g) = (*ref1, *g); | ||
| 4634 | + let uni = (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME) | ||
| 4635 | + .contains(&ref1); | ||
| 4636 | + let pair_bits = symbol_bits(&cdf::COMP_MODE[0], 1) | ||
| 4637 | + + if uni { | ||
| 4638 | + // LAST + a backward reference: `comp_ref_type` = bidirectional, | ||
| 4639 | + // `comp_ref` LAST, `comp_bwdref` ALTREF. | ||
| 4640 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 1) | ||
| 4641 | + + symbol_bits(&cdf::COMP_REF[0][0], 0) | ||
| 4642 | + + symbol_bits(&cdf::COMP_REF[0][1], 0) | ||
| 4643 | + + symbol_bits(&cdf::COMP_BWDREF[0][0], 1) | ||
| 4644 | + } else { | ||
| 4645 | + // LAST + GOLDEN: unidirectional, `uni_comp_ref` p0/p1/p2. | ||
| 4646 | + symbol_bits(&cdf::COMP_REF_TYPE[0], 0) | ||
| 4647 | + + symbol_bits(&cdf::UNI_COMP_REF[0][0], 0) | ||
| 4648 | + + symbol_bits(&cdf::UNI_COMP_REF[0][1], 1) | ||
| 4649 | + + symbol_bits(&cdf::UNI_COMP_REF[0][2], 1) | ||
| 4650 | + }; | ||
| 4651 | + let mode_ctx = | ||
| 4652 | + cdf::COMPOUND_MODE_CTX_MAP[cstack.ref_mv_ctx >> 1][cstack.new_mv_ctx.min(4)]; | ||
| 4653 | + let mode_bits_of = | ||
| 4654 | + |mode: usize| pair_bits + symbol_bits(&cdf::INTER_COMPOUND_MODE[mode_ctx], mode); | ||
| 4655 | + let mut ccands: Vec<(((i32, i32), (i32, i32)), f64, InterInfo)> = vec![ | ||
| 4656 | + ( | ||
| 4657 | + cstack.nearest_mv, | ||
| 4658 | + mode_bits_of(0), | ||
| 4659 | + InterInfo { | ||
| 4660 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 4661 | + mode: InterMode::NearestNearestMv, | ||
| 4662 | + mv: cstack.nearest_mv.0, | ||
| 4663 | + mv1: cstack.nearest_mv.1, | ||
| 4664 | + ref1: Some(ref1), | ||
| 4665 | + ref_mv_idx: 0, | ||
| 4666 | + }, | ||
| 4667 | + ), | ||
| 4668 | + ( | ||
| 4669 | + ((0, 0), (0, 0)), | ||
| 4670 | + mode_bits_of(6), | ||
| 4671 | + InterInfo { | ||
| 4672 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 4673 | + mode: InterMode::GlobalGlobalMv, | ||
| 4674 | + mv: (0, 0), | ||
| 4675 | + mv1: (0, 0), | ||
| 4676 | + ref1: Some(ref1), | ||
| 4677 | + ref_mv_idx: 0, | ||
| 4678 | + }, | ||
| 4679 | + ), | ||
| 4680 | + ]; | ||
| 4681 | + // `NEW_NEWMV` seeded from the two single-reference searches this | ||
| 4682 | + // block already ran -- no joint search of its own. | ||
| 4683 | + if let Some(&(_, mv1)) = extra_new_mvs.iter().find(|(r, _)| *r == ref1) { | ||
| 4684 | + let base = cstack | ||
| 4685 | + .entries | ||
| 4686 | + .first() | ||
| 4687 | + .map_or(cstack.nearest_mv, |e| (e.mv0, e.mv1)); | ||
| 4688 | + if let (Some(b0), Some(b1)) = ( | ||
| 4689 | + mv_residual_bits(mv, base.0), | ||
| 4690 | + mv_residual_bits(mv1, base.1), | ||
| 4691 | + ) { | ||
| 4692 | + ccands.push(( | ||
| 4693 | + (mv, mv1), | ||
| 4694 | + mode_bits_of(7) + b0 + b1, | ||
| 4695 | + InterInfo { | ||
| 4696 | + ref_frame: crate::mvstack::LAST_FRAME, | ||
| 4697 | + mode: InterMode::NewNewMv, | ||
| 4698 | + mv, | ||
| 4699 | + mv1, | ||
| 4700 | + ref1: Some(ref1), | ||
| 4701 | + ref_mv_idx: 0, | ||
| 4702 | + }, | ||
| 4703 | + )); | ||
| 4704 | + } | ||
| 4705 | + } | ||
| 4706 | + ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); | ||
| 4707 | + ccands.dedup_by_key(|c| c.0); | ||
| 4708 | + let g_luma = (g.y.as_slice(), g.width, luma.true_width, luma.true_height); | ||
| 4709 | + let g_u = (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height); | ||
| 4710 | + let g_v = (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height); | ||
| 4711 | + for (mvs, bits, info) in ccands { | ||
| 4712 | + let luma_trial = mc_trial_compound( | ||
| 4713 | + luma, x, y, BLOCK, mvs, true, | ||
| 4714 | + (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), g_luma, | ||
| 4715 | + search.base_q_idx, search.deadzone, TxbSet::Luma32Inter, fctx, | ||
| 4716 | + ); | ||
| 4717 | + let u = mc_trial_compound( | ||
| 4718 | + &chroma[0], x / 2, y / 2, BLOCK / 2, mvs, false, | ||
| 4719 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), g_u, | ||
| 4720 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 4721 | + ); | ||
| 4722 | + let v = mc_trial_compound( | ||
| 4723 | + &chroma[1], x / 2, y / 2, BLOCK / 2, mvs, false, | ||
| 4724 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), g_v, | ||
| 4725 | + search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 4726 | + ); | ||
| 4727 | + let skip = luma_trial.levels.iter().all(|&l| l == 0) | ||
| 4728 | + && u.levels.iter().all(|&l| l == 0) | ||
| 4729 | + && v.levels.iter().all(|&l| l == 0); | ||
| 4730 | + let cost = luma_trial.sse | ||
| 4731 | + + u.sse | ||
| 4732 | + + v.sse | ||
| 4733 | + + search.lambda | ||
| 4734 | + * (skip_bits(skip) | ||
| 4735 | + + intra_inter_bits(true) | ||
| 4736 | + + bits | ||
| 4737 | + + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits }); | ||
| 4738 | + consider(Candidate { | ||
| 4739 | + cost, | ||
| 4740 | + luma: luma_trial, | ||
| 4741 | + u, | ||
| 4742 | + v, | ||
| 4743 | + mode: DC_PRED, | ||
| 4744 | + skip, | ||
| 4745 | + inter: Some(info), | ||
| 4746 | + }); | ||
| 4747 | + } | ||
| 4748 | + } | ||
| 4749 | + | ||
| 4518 | // One trial set per distinct (reference, vector) pair: sort, cheapest | 4750 | // One trial set per distinct (reference, vector) pair: sort, cheapest |
| 4519 | // syntax first, and drop the rest of each pair's run. | 4751 | // syntax first, and drop the rest of each pair's run. |
| 4520 | cands.sort_by(|a, b| { | 4752 | cands.sort_by(|a, b| { |
| @@ -4669,6 +4901,12 @@ pub(crate) fn encode_inter_frame( | |||
| 4669 | base_q_idx: u8, | 4901 | base_q_idx: u8, |
| 4670 | deadzone: f64, | 4902 | deadzone: f64, |
| 4671 | order_hint: u32, | 4903 | order_hint: u32, |
| 4904 | + // The order hint each of the seven `ref_frame_idx` entries names, which a | ||
| 4905 | + // decoder reads out of its own reference state -- only spec 5.9.22's | ||
| 4906 | + // `skipModeAllowed` (in `frame::write_frame_header`) reads it, and only | ||
| 4907 | + // on a `reference_select` frame. [`flat_order_hints`] builds it for the | ||
| 4908 | + // callers that follow this crate's own flat slot plan. | ||
| 4909 | + order_hints: [u32; 7], | ||
| 4672 | render: (usize, usize), | 4910 | render: (usize, usize), |
| 4673 | // The tables this frame's writer starts from: what the previous frame | 4911 | // The tables this frame's writer starts from: what the previous frame |
| 4674 | // stored into the slot this one's `primary_ref_frame` names. `None` | 4912 | // stored into the slot this one's `primary_ref_frame` names. `None` |
| @@ -4745,6 +4983,14 @@ pub(crate) fn encode_inter_frame( | |||
| 4745 | header.render_width = render.0 as u32; | 4983 | header.render_width = render.0 as u32; |
| 4746 | header.render_height = render.1 as u32; | 4984 | header.render_height = render.1 as u32; |
| 4747 | let sign_bias = pyramid.map_or(crate::mvstack::NO_SIGN_BIAS, |p| p.sign_bias); | 4985 | let sign_bias = pyramid.map_or(crate::mvstack::NO_SIGN_BIAS, |p| p.sign_bias); |
| 4986 | + // The order hint behind each `ref_frame_idx` entry. The flat path's own | ||
| 4987 | + // slot plan (`inter_frame_headers_slots`) is: every reference but | ||
| 4988 | + // `GOLDEN_FRAME` reads `last_slot` (the frame just coded) except | ||
| 4989 | + // `ALTREF_FRAME`, which reads the frame two back when there is one; | ||
| 4990 | + // `GOLDEN_FRAME` is the key frame, order hint 0. Only | ||
| 4991 | + // `frame::write_frame_header`'s `skipModeAllowed` reads this. | ||
| 4992 | + header.order_hints = order_hints; | ||
| 4993 | + header.reference_select = reference_select(); | ||
| 4748 | if let Some(p) = pyramid { | 4994 | if let Some(p) = pyramid { |
| 4749 | header.show_frame = p.show_frame; | 4995 | header.show_frame = p.show_frame; |
| 4750 | header.showable_frame = !p.show_frame; | 4996 | header.showable_frame = !p.show_frame; |
| @@ -4875,6 +5121,37 @@ pub(crate) fn encode_inter_frame( | |||
| 4875 | .iter() | 5121 | .iter() |
| 4876 | .map(|(r, p, st)| (*r, *p, st)) | 5122 | .map(|(r, p, st)| (*r, *p, st)) |
| 4877 | .collect(); | 5123 | .collect(); |
| 5124 | + // The COMPOUND stack of each `LAST` + extra pair, off the | ||
| 5125 | + // same grid (and the same armed sign bias) the tile writer | ||
| 5126 | + // rebuilds it from. Empty unless this frame codes | ||
| 5127 | + // `reference_select`, which is what gates the syntax. | ||
| 5128 | + let compound: Vec<(i8, &Picture, crate::mvstack::CompoundMvStack)> = | ||
| 5129 | + if header.reference_select { | ||
| 5130 | + extra_stacks | ||
| 5131 | + .iter() | ||
| 5132 | + .map(|&(r, p, _)| { | ||
| 5133 | + ( | ||
| 5134 | + r, | ||
| 5135 | + p, | ||
| 5136 | + crate::mvstack::find_mv_stack_compound( | ||
| 5137 | + &grid, | ||
| 5138 | + mi_row, | ||
| 5139 | + mi_col, | ||
| 5140 | + 8, | ||
| 5141 | + 8, | ||
| 5142 | + (crate::mvstack::LAST_FRAME, r), | ||
| 5143 | + mi_cols, | ||
| 5144 | + mi_rows, | ||
| 5145 | + grid.sign_bias_table(), | ||
| 5146 | + &[(0, 0); 7], | ||
| 5147 | + None, | ||
| 5148 | + ), | ||
| 5149 | + ) | ||
| 5150 | + }) | ||
| 5151 | + .collect() | ||
| 5152 | + } else { | ||
| 5153 | + Vec::new() | ||
| 5154 | + }; | ||
| 4878 | 5155 | ||
| 4879 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); | 5156 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 4880 | let (block, mut cost_whole) = search_inter_block( | 5157 | let (block, mut cost_whole) = search_inter_block( |
| @@ -4885,7 +5162,8 @@ pub(crate) fn encode_inter_frame( | |||
| 4885 | &mode_bits_table, | 5162 | &mode_bits_table, |
| 4886 | reference, | 5163 | reference, |
| 4887 | &stack, | 5164 | &stack, |
| 4888 | - &extra, fctx, | 5165 | + &extra, |
| 5166 | + &compound, fctx, | ||
| 4889 | ); | 5167 | ); |
| 4890 | cost_whole += search.lambda * partition_bits(BLOCK, false); | 5168 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 4891 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); | 5169 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -5223,6 +5501,8 @@ pub(crate) fn encode_inter_frame( | |||
| 5223 | .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); | 5501 | .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); |
| 5224 | let start_cdfs = CdfSnapshot(cdfs.clone()); | 5502 | let start_cdfs = CdfSnapshot(cdfs.clone()); |
| 5225 | crate::tile::arm_sign_bias(sign_bias); | 5503 | crate::tile::arm_sign_bias(sign_bias); |
| 5504 | + crate::tile::arm_reference_select(header.reference_select); | ||
| 5505 | + crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); | ||
| 5226 | let mut tile = crate::tile::sb_coeff_inter_frame_tile_cdfs( | 5506 | let mut tile = crate::tile::sb_coeff_inter_frame_tile_cdfs( |
| 5227 | mi_cols, | 5507 | mi_cols, |
| 5228 | mi_rows, | 5508 | mi_rows, |
| @@ -5313,6 +5593,8 @@ pub(crate) fn encode_inter_frame( | |||
| 5313 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); | 5593 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); |
| 5314 | crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); | 5594 | crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); |
| 5315 | crate::tile::arm_sign_bias(sign_bias); | 5595 | crate::tile::arm_sign_bias(sign_bias); |
| 5596 | + crate::tile::arm_reference_select(reference_select()); | ||
| 5597 | + crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); | ||
| 5316 | let mut cdfs = start_cdfs.0.clone(); | 5598 | let mut cdfs = start_cdfs.0.clone(); |
| 5317 | let coded = crate::tile::sb_coeff_inter_frame_tile_cdfs( | 5599 | let coded = crate::tile::sb_coeff_inter_frame_tile_cdfs( |
| 5318 | mi_cols, | 5600 | mi_cols, |
| @@ -5468,6 +5750,7 @@ pub(crate) fn encode_sequence_with_ctx( | |||
| 5468 | base_q_idx, | 5750 | base_q_idx, |
| 5469 | deadzone, | 5751 | deadzone, |
| 5470 | order_hint, | 5752 | order_hint, |
| 5753 | + flat_order_hints(order_hint, 0, prev2.is_some()), | ||
| 5471 | render, | 5754 | render, |
| 5472 | Some(&carried.0), | 5755 | Some(&carried.0), |
| 5473 | Some(&golden), | 5756 | Some(&golden), |
| @@ -5952,6 +6235,8 @@ mod tests { | |||
| 5952 | palette: None, | 6235 | palette: None, |
| 5953 | tx_depth: 0, | 6236 | tx_depth: 0, |
| 5954 | inter: Some(InterInfo { | 6237 | inter: Some(InterInfo { |
| 6238 | + ref1: None, | ||
| 6239 | + mv1: (0, 0), | ||
| 5955 | ref_frame: crate::mvstack::LAST_FRAME, | 6240 | ref_frame: crate::mvstack::LAST_FRAME, |
| 5956 | mode: InterMode::NearestMv, | 6241 | mode: InterMode::NearestMv, |
| 5957 | mv: (0, 0), | 6242 | mv: (0, 0), |
| @@ -5961,6 +6246,8 @@ mod tests { | |||
| 5961 | let skipped_block = BlockCoeffs { | 6246 | let skipped_block = BlockCoeffs { |
| 5962 | skip: true, | 6247 | skip: true, |
| 5963 | inter: Some(InterInfo { | 6248 | inter: Some(InterInfo { |
| 6249 | + ref1: None, | ||
| 6250 | + mv1: (0, 0), | ||
| 5964 | ref_frame: crate::mvstack::LAST_FRAME, | 6251 | ref_frame: crate::mvstack::LAST_FRAME, |
| 5965 | mode: InterMode::NearestMv, | 6252 | mode: InterMode::NearestMv, |
| 5966 | mv: (0, 0), | 6253 | mv: (0, 0), |
| @@ -7599,7 +7886,7 @@ mod tests { | |||
| 7599 | let padded_pic = picture.padded_to(SUPERBLOCK); | 7886 | let padded_pic = picture.padded_to(SUPERBLOCK); |
| 7600 | let t = Instant::now(); | 7887 | let t = Instant::now(); |
| 7601 | let inter = | 7888 | let inter = |
| 7602 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx, None).unwrap(); | 7889 | + 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(); |
| 7603 | let inter_t = t.elapsed(); | 7890 | let inter_t = t.elapsed(); |
| 7604 | eprintln!( | 7891 | eprintln!( |
| 7605 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ | 7892 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ |
| @@ -7697,7 +7984,7 @@ mod tests { | |||
| 7697 | stage_reset(); | 7984 | stage_reset(); |
| 7698 | let t = Instant::now(); | 7985 | let t = Instant::now(); |
| 7699 | let inter = | 7986 | let inter = |
| 7700 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx, None).unwrap(); | 7987 | + 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(); |
| 7701 | let total_t = t.elapsed(); | 7988 | let total_t = t.elapsed(); |
| 7702 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); | 7989 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); |
| 7703 | let accounted = motion_search + transform_quant + entropy; | 7990 | let accounted = motion_search + transform_quant + entropy; |
| @@ -7874,6 +8161,7 @@ mod tests { | |||
| 7874 | q, | 8161 | q, |
| 7875 | 0.5, | 8162 | 0.5, |
| 7876 | 1, | 8163 | 1, |
| 8164 | + flat_order_hints(1, 0, false), | ||
| 7877 | (width, height), None, None, None, fctx, None, | 8165 | (width, height), None, None, None, fctx, None, |
| 7878 | ) | 8166 | ) |
| 7879 | .unwrap(); | 8167 | .unwrap(); |
| @@ -7891,6 +8179,7 @@ mod tests { | |||
| 7891 | q, | 8179 | q, |
| 7892 | 0.5, | 8180 | 0.5, |
| 7893 | 1, | 8181 | 1, |
| 8182 | + flat_order_hints(1, 0, false), | ||
| 7894 | (width, height), None, None, None, fctx, None, | 8183 | (width, height), None, None, None, fctx, None, |
| 7895 | ) | 8184 | ) |
| 7896 | .unwrap(); | 8185 | .unwrap(); |
| @@ -8384,7 +8673,7 @@ mod tests { | |||
| 8384 | }) | 8673 | }) |
| 8385 | }; | 8674 | }; |
| 8386 | let pins: [(u8, usize, u64); 2] = | 8675 | let pins: [(u8, usize, u64); 2] = |
| 8387 | - [(150, 7444, 0x76eb980832d719f1), (60, 27692, 0x8c88f7e73455d439)]; | 8676 | + [(150, 7209, 0xacdd_6952_78c1_49f2), (60, 27353, 0x9be8_7898_f08d_3b77)]; |
| 8388 | for (q, bytes, hash) in pins { | 8677 | for (q, bytes, hash) in pins { |
| 8389 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); | 8678 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 8390 | assert_eq!( | 8679 | assert_eq!( |
| @@ -8692,6 +8981,8 @@ mod tests { | |||
| 8692 | let _ = crate::tile::take_inter_mode_hits(); | 8981 | let _ = crate::tile::take_inter_mode_hits(); |
| 8693 | let _ = crate::tile::take_drl_hits(); | 8982 | let _ = crate::tile::take_drl_hits(); |
| 8694 | let _ = crate::tile::take_ref_hits(); | 8983 | let _ = crate::tile::take_ref_hits(); |
| 8984 | + let _ = crate::tile::take_compound_mode_hits(); | ||
| 8985 | + let _ = crate::tile::take_compound_pair_hits(); | ||
| 8695 | let _ = crate::motion::take_census(); | 8986 | let _ = crate::motion::take_census(); |
| 8696 | let (ours, ours_wall) = match pyramid_from_env() { | 8987 | let (ours, ours_wall) = match pyramid_from_env() { |
| 8697 | None => our_ladder(&source, width, height, fctx), | 8988 | None => our_ladder(&source, width, height, fctx), |
| @@ -8727,6 +9018,22 @@ mod tests { | |||
| 8727 | 100.0 * global as f64 / modes_total as f64, | 9018 | 100.0 * global as f64 / modes_total as f64, |
| 8728 | 100.0 * new as f64 / modes_total as f64, | 9019 | 100.0 * new as f64 / modes_total as f64, |
| 8729 | ); | 9020 | ); |
| 9021 | + // The compound census: what share of the coded inter blocks took | ||
| 9022 | + // a compound reference at all, which compound mode they took, and | ||
| 9023 | + // which pair (gate-blind-to-feature -- a compound mode nobody | ||
| 9024 | + // picks cannot be what a BD number credits). | ||
| 9025 | + let comp_modes = crate::tile::take_compound_mode_hits(); | ||
| 9026 | + let comp_pairs = crate::tile::take_compound_pair_hits(); | ||
| 9027 | + let comp_total: usize = comp_modes.iter().sum(); | ||
| 9028 | + eprintln!( | ||
| 9029 | + "{name}: compound {comp_total} of {} inter blocks ({:.1}%); modes \ | ||
| 9030 | + NEAREST_NEAREST {} NEAR_NEAR {} GLOBAL_GLOBAL {} NEW_NEW {}; pairs \ | ||
| 9031 | + LAST+GOLDEN {} LAST+ALTREF {}", | ||
| 9032 | + modes_total + comp_total, | ||
| 9033 | + 100.0 * comp_total as f64 / (modes_total + comp_total) as f64, | ||
| 9034 | + comp_modes[0], comp_modes[1], comp_modes[6], comp_modes[7], | ||
| 9035 | + comp_pairs[3], comp_pairs[6], | ||
| 9036 | + ); | ||
| 8730 | // Where the wall-owning stage actually goes: how many candidate | 9037 | // Where the wall-owning stage actually goes: how many candidate |
| 8731 | // evaluations (one `mc::predict` + SAD each) a search spends, how | 9038 | // evaluations (one `mc::predict` + SAD each) a search spends, how |
| 8732 | // many of them are subpel, and how often the winner is the seed | 9039 | // 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+54 -5
| @@ -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 { | 572 | + } else { |
| 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 { | ||
| 534 | return Err(Error::unsupported( | 584 | return Err(Error::unsupported( |
| 535 | "AV1 frame header", | 585 | "AV1 frame header", |
| 536 | - "reference_select (and the skip mode it gates) is not written", | 586 | + "skip_mode_present is not written -- no writer codes a skip-mode block", |
| 537 | )); | 587 | )); |
| 538 | - } else { | 588 | + } |
| 539 | w.write_bit(false); | 589 | w.write_bit(false); |
| 540 | - // skip_mode_present is forced false when reference_select is false | 590 | + } |
| 541 | - // (spec 5.9.22), and not coded. | ||
| 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+525 -12
| @@ -177,6 +177,299 @@ pub(crate) fn arm_screen(on: bool) { | |||
| 177 | 177 | ||
| 178 | fn screen_armed() -> bool { | 178 | fn screen_armed() -> bool { |
| 179 | SCREEN.with(std::cell::Cell::get) | 179 | SCREEN.with(std::cell::Cell::get) |
| 180 | + | ||
| 181 | +thread_local! { | ||
| 182 | + /// This frame header's own `reference_select` bit (spec 5.9.22), armed | ||
| 183 | + /// the same way `arm_sign_bias` arms the sign-bias table rather than | ||
| 184 | + /// threaded through the five nested writer helpers between here and the | ||
| 185 | + /// per-block mode syntax. `true` makes every inter block of size at least | ||
| 186 | + /// 8x8 code a `comp_mode` symbol, exactly where decode.rs `read_comp_mode` | ||
| 187 | + /// reads one. | ||
| 188 | + static REFERENCE_SELECT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | ||
| 189 | +} | ||
| 190 | + | ||
| 191 | +/// Arms the frame's `reference_select` bit for the next tile written on this | ||
| 192 | +/// thread; cleared by [`CdefIdxGuard`] when that writer returns. | ||
| 193 | +pub(crate) fn arm_reference_select(on: bool) { | ||
| 194 | + REFERENCE_SELECT.with(|c| c.set(on)); | ||
| 195 | +} | ||
| 196 | + | ||
| 197 | +/// The compound half of a block's end-of-block neighbour record: nothing for | ||
| 198 | +/// a single-reference or intra block, and `record_compound_mi`'s overwrite of | ||
| 199 | +/// the ALTREF default for a compound one (this writer only codes | ||
| 200 | +/// `comp_group_idx == 0`, `compound_idx == 1`). | ||
| 201 | +fn record_block_compound(neighbours: &mut Neighbours, at_mi: (usize, usize), side: usize, block: &BlockCoeffs) { | ||
| 202 | + if let Some(r1) = block.inter.and_then(|i| i.ref1) { | ||
| 203 | + neighbours.record_compound_mi(at_mi, side, r1, 0, 1); | ||
| 204 | + } | ||
| 205 | +} | ||
| 206 | + | ||
| 207 | +/// decode.rs' own `is_uni_comp_ref`: a pair is `UNIDIR_COMP_REFERENCE` | ||
| 208 | +/// exactly when both references sit on the same temporal side. | ||
| 209 | +fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { | ||
| 210 | + let backward = | ||
| 211 | + |r: i8| (crate::mvstack::BWDREF_FRAME..=crate::mvstack::ALTREF_FRAME).contains(&r); | ||
| 212 | + backward(ref0) == backward(ref1) | ||
| 213 | +} | ||
| 214 | + | ||
| 215 | +thread_local! { | ||
| 216 | + /// `(order_hint_bits, this frame's order hint, each reference's order | ||
| 217 | + /// hint)` -- what `get_comp_index_context` (decode.rs) needs to context | ||
| 218 | + /// the `compound_idx` symbol, armed the same way the sign-bias table is. | ||
| 219 | + static ORDER_HINTS: std::cell::Cell<(u32, u32, [u32; 7])> = | ||
| 220 | + const { std::cell::Cell::new((7, 0, [0; 7])) }; | ||
| 221 | +} | ||
| 222 | + | ||
| 223 | +/// Arms this frame's order hints for the next tile written on this thread. | ||
| 224 | +pub(crate) fn arm_order_hints(bits: u32, order_hint: u32, hints: [u32; 7]) { | ||
| 225 | + ORDER_HINTS.with(|c| c.set((bits, order_hint, hints))); | ||
| 226 | +} | ||
| 227 | + | ||
| 228 | +/// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec | ||
| 229 | +/// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref` | ||
| 230 | +/// trees), symbol for symbol at the same contexts -- which are, as that | ||
| 231 | +/// function's own doc records, `single_ref_p1_ctx`..`single_ref_p6_ctx`/ | ||
| 232 | +/// `uni_comp_ref_p1_ctx` under libaom's compound names. | ||
| 233 | +/// | ||
| 234 | +/// # Errors | ||
| 235 | +/// A pair this writer never forms (its `ref0` is not `LAST_FRAME`). | ||
| 236 | +fn write_compound_ref_frames( | ||
| 237 | + enc: &mut SymbolEncoder, | ||
| 238 | + cdfs: &mut Cdfs, | ||
| 239 | + above: Option<crate::mvstack::NeighbourRef>, | ||
| 240 | + left: Option<crate::mvstack::NeighbourRef>, | ||
| 241 | + above_ref: i8, | ||
| 242 | + left_ref: i8, | ||
| 243 | + (ref0, ref1): (i8, i8), | ||
| 244 | +) -> Result<()> { | ||
| 245 | + use crate::mvstack::{ | ||
| 246 | + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, | ||
| 247 | + LAST_FRAME, comp_reference_type_ctx, single_ref_p1_ctx, single_ref_p2_ctx, | ||
| 248 | + single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, | ||
| 249 | + uni_comp_ref_p1_ctx, | ||
| 250 | + }; | ||
| 251 | + let a = (above_ref > 0).then_some(above_ref); | ||
| 252 | + let l = (left_ref > 0).then_some(left_ref); | ||
| 253 | + let a1 = above.and_then(|n| n.ref1); | ||
| 254 | + let l1 = left.and_then(|n| n.ref1); | ||
| 255 | + let unidir = is_uni_comp_ref(ref0, ref1); | ||
| 256 | + enc.symbol( | ||
| 257 | + usize::from(!unidir), | ||
| 258 | + &mut cdfs.comp_ref_type[comp_reference_type_ctx(above, left)], | ||
| 259 | + ); | ||
| 260 | + if unidir { | ||
| 261 | + let both_backward = (ref0, ref1) == (BWDREF_FRAME, ALTREF_FRAME); | ||
| 262 | + enc.symbol( | ||
| 263 | + usize::from(both_backward), | ||
| 264 | + &mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0], | ||
| 265 | + ); | ||
| 266 | + if both_backward { | ||
| 267 | + return Ok(()); | ||
| 268 | + } | ||
| 269 | + if ref0 != LAST_FRAME { | ||
| 270 | + return Err(Error::unsupported( | ||
| 271 | + "AV1 tile", | ||
| 272 | + "a unidirectional compound pair this writer does not form", | ||
| 273 | + )); | ||
| 274 | + } | ||
| 275 | + let past_last2 = ref1 != LAST2_FRAME; | ||
| 276 | + enc.symbol( | ||
| 277 | + usize::from(past_last2), | ||
| 278 | + &mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1], | ||
| 279 | + ); | ||
| 280 | + if past_last2 { | ||
| 281 | + enc.symbol( | ||
| 282 | + usize::from(ref1 == GOLDEN_FRAME), | ||
| 283 | + &mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2], | ||
| 284 | + ); | ||
| 285 | + } | ||
| 286 | + return Ok(()); | ||
| 287 | + } | ||
| 288 | + let far = ref0 == LAST3_FRAME || ref0 == GOLDEN_FRAME; | ||
| 289 | + enc.symbol( | ||
| 290 | + usize::from(far), | ||
| 291 | + &mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0], | ||
| 292 | + ); | ||
| 293 | + if far { | ||
| 294 | + enc.symbol( | ||
| 295 | + usize::from(ref0 == GOLDEN_FRAME), | ||
| 296 | + &mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2], | ||
| 297 | + ); | ||
| 298 | + } else { | ||
| 299 | + enc.symbol( | ||
| 300 | + usize::from(ref0 == LAST2_FRAME), | ||
| 301 | + &mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1], | ||
| 302 | + ); | ||
| 303 | + } | ||
| 304 | + let is_altref = ref1 == ALTREF_FRAME; | ||
| 305 | + enc.symbol( | ||
| 306 | + usize::from(is_altref), | ||
| 307 | + &mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0], | ||
| 308 | + ); | ||
| 309 | + if !is_altref { | ||
| 310 | + enc.symbol( | ||
| 311 | + usize::from(ref1 == ALTREF2_FRAME), | ||
| 312 | + &mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1], | ||
| 313 | + ); | ||
| 314 | + } | ||
| 315 | + Ok(()) | ||
| 316 | +} | ||
| 317 | + | ||
| 318 | +/// Per-mode and per-pair fire counts of [`write_compound_block`], for the | ||
| 319 | +/// gate's own census (gate-blind-to-feature): modes indexed by the spec's | ||
| 320 | +/// `INTER_COMPOUND_MODES`, pairs by `ref1` (`LAST`..`ALTREF`). | ||
| 321 | +static COMPOUND_MODE_HITS: [std::sync::atomic::AtomicUsize; 8] = | ||
| 322 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 8]; | ||
| 323 | +static COMPOUND_PAIR_HITS: [std::sync::atomic::AtomicUsize; 7] = | ||
| 324 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 7]; | ||
| 325 | + | ||
| 326 | +/// Takes and clears the compound mode histogram. | ||
| 327 | +#[cfg(test)] | ||
| 328 | +pub(crate) fn take_compound_mode_hits() -> [usize; 8] { | ||
| 329 | + std::array::from_fn(|i| COMPOUND_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 330 | +} | ||
| 331 | + | ||
| 332 | +/// Takes and clears the compound pair histogram, indexed by the SECOND | ||
| 333 | +/// reference (`LAST` = 0 .. `ALTREF` = 6). | ||
| 334 | +#[cfg(test)] | ||
| 335 | +pub(crate) fn take_compound_pair_hits() -> [usize; 7] { | ||
| 336 | + std::array::from_fn(|i| COMPOUND_PAIR_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 337 | +} | ||
| 338 | + | ||
| 339 | +/// Writes one COMPOUND_REFERENCE block's whole mode chain -- the writer side | ||
| 340 | +/// of decode.rs' `read_compound_ref_frames` / `read_inter_compound_mode` / | ||
| 341 | +/// `assign_compound_mv` / `comp_group_idx` / `compound_idx` sequence, in that | ||
| 342 | +/// order. Returns the two motion vectors the decoder derives (what the MI | ||
| 343 | +/// grid must record) and whether the mode was a `NEW*` one. | ||
| 344 | +/// | ||
| 345 | +/// Only the three modes [`InterMode::compound_index`] names are written, and | ||
| 346 | +/// only the `comp_group_idx == 0`, `compound_idx == 1` blend (spec | ||
| 347 | +/// `COMPOUND_AVERAGE`) -- masked compound and the distance-weighted blend are | ||
| 348 | +/// not searched. | ||
| 349 | +/// | ||
| 350 | +/// # Errors | ||
| 351 | +/// As [`write_mv`], or a pair/mode this writer does not form. | ||
| 352 | +#[allow(clippy::too_many_arguments)] | ||
| 353 | +fn write_compound_block( | ||
| 354 | + enc: &mut SymbolEncoder, | ||
| 355 | + cdfs: &mut Cdfs, | ||
| 356 | + neighbours: &Neighbours, | ||
| 357 | + grid: &crate::mvstack::MiGrid, | ||
| 358 | + (mi_row, mi_col): (usize, usize), | ||
| 359 | + (bw4, bh4): (usize, usize), | ||
| 360 | + (has_above, has_left): (bool, bool), | ||
| 361 | + info: InterInfo, | ||
| 362 | + (mi_cols, mi_rows): (usize, usize), | ||
| 363 | +) -> Result<((i32, i32), (i32, i32), bool)> { | ||
| 364 | + let (Some(ref1), Some(mode)) = (info.ref1, info.mode.compound_index()) else { | ||
| 365 | + return Err(Error::unsupported( | ||
| 366 | + "AV1 tile", | ||
| 367 | + "a compound block needs a second reference and a compound mode", | ||
| 368 | + )); | ||
| 369 | + }; | ||
| 370 | + let above = has_above.then(|| neighbours.above_nbr(mi_col)); | ||
| 371 | + let left = has_left.then(|| neighbours.left_nbr(mi_row)); | ||
| 372 | + write_compound_ref_frames( | ||
| 373 | + enc, | ||
| 374 | + cdfs, | ||
| 375 | + above, | ||
| 376 | + left, | ||
| 377 | + neighbours.above_ref[mi_col], | ||
| 378 | + neighbours.left_ref[mi_row], | ||
| 379 | + (info.ref_frame, ref1), | ||
| 380 | + )?; | ||
| 381 | + let stack = crate::mvstack::find_mv_stack_compound( | ||
| 382 | + grid, | ||
| 383 | + mi_row, | ||
| 384 | + mi_col, | ||
| 385 | + bw4, | ||
| 386 | + bh4, | ||
| 387 | + (info.ref_frame, ref1), | ||
| 388 | + mi_cols, | ||
| 389 | + mi_rows, | ||
| 390 | + grid.sign_bias_table(), | ||
| 391 | + &[(0, 0); 7], | ||
| 392 | + None, | ||
| 393 | + ); | ||
| 394 | + let ctx = crate::cdf::COMPOUND_MODE_CTX_MAP[stack.ref_mv_ctx >> 1][stack.new_mv_ctx.min(4)]; | ||
| 395 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 396 | + eprintln!( | ||
| 397 | + "EC_WCOMP mi_row={mi_row} mi_col={mi_col} mode={mode} ref0={} ref1={ref1} ctx={ctx} new_mv_ctx={} ref_mv_ctx={} stack={}", | ||
| 398 | + info.ref_frame, stack.new_mv_ctx, stack.ref_mv_ctx, stack.entries.len() | ||
| 399 | + ); | ||
| 400 | + } | ||
| 401 | + enc.symbol(mode, &mut cdfs.inter_compound_mode[ctx]); | ||
| 402 | + // `assign_compound_mv`'s own DRL half: `NEW_NEWMV` walks from index 0, | ||
| 403 | + // the two derived modes code no index at all. | ||
| 404 | + let mvs = match info.mode { | ||
| 405 | + InterMode::NearestNearestMv => stack.nearest_mv, | ||
| 406 | + InterMode::GlobalGlobalMv => ((0, 0), (0, 0)), | ||
| 407 | + _ => { | ||
| 408 | + let idx = usize::from(info.ref_mv_idx); | ||
| 409 | + let mut walk = 0usize; | ||
| 410 | + while walk < 2 && stack.entries.len() > walk + 1 { | ||
| 411 | + let advance = walk < idx; | ||
| 412 | + enc.symbol(usize::from(advance), &mut cdfs.drl_mode[stack.drl_ctx[walk]]); | ||
| 413 | + if !advance { | ||
| 414 | + break; | ||
| 415 | + } | ||
| 416 | + walk += 1; | ||
| 417 | + } | ||
| 418 | + let base = stack | ||
| 419 | + .entries | ||
| 420 | + .get(idx) | ||
| 421 | + .map_or(stack.nearest_mv, |e| (e.mv0, e.mv1)); | ||
| 422 | + write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv, base.0)?; | ||
| 423 | + write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv1, base.1)?; | ||
| 424 | + (info.mv, info.mv1) | ||
| 425 | + } | ||
| 426 | + }; | ||
| 427 | + // spec 5.11.25's `comp_group_idx`/`compound_idx` are NOT coded: this | ||
| 428 | + // crate's sequence header carries `enable_masked_compound = false` and | ||
| 429 | + // `enable_jnt_comp = false` (encode.rs), and decode.rs reads neither | ||
| 430 | + // symbol under those bits -- it infers `comp_group_idx = 0`, | ||
| 431 | + // `compound_idx = 1` (the simple average), which is the only blend this | ||
| 432 | + // writer forms anyway. A lane that turns either sequence bit on has to | ||
| 433 | + // write the matching symbol here, at | ||
| 434 | + // `get_comp_group_idx_context`/`get_comp_index_context`'s contexts (which | ||
| 435 | + // is what `Neighbours::above_comp_group_idx`/`above_compound_idx` and the | ||
| 436 | + // armed order hints below are already kept for). | ||
| 437 | + let _ = ORDER_HINTS.with(std::cell::Cell::get); | ||
| 438 | + COMPOUND_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 439 | + COMPOUND_PAIR_HITS[(ref1.max(1) - 1) as usize % 7] | ||
| 440 | + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 441 | + Ok((mvs.0, mvs.1, matches!(info.mode, InterMode::NewNewMv))) | ||
| 442 | +} | ||
| 443 | + | ||
| 444 | +/// Writer-side counterpart of decode.rs `read_comp_mode` (spec 5.11.25's | ||
| 445 | +/// `comp_mode`): on a `reference_select` frame every inter block whose size | ||
| 446 | +/// allows compound (`is_comp_ref_allowed`, always true here -- this writer's | ||
| 447 | +/// smallest leaf is 8x8) names SINGLE or COMPOUND, at | ||
| 448 | +/// `mvstack::reference_mode_ctx`'s own context off the same two neighbour | ||
| 449 | +/// bands decode.rs builds its `NeighbourRef` pair from. | ||
| 450 | +fn write_comp_mode( | ||
| 451 | + enc: &mut SymbolEncoder, | ||
| 452 | + cdfs: &mut Cdfs, | ||
| 453 | + neighbours: &Neighbours, | ||
| 454 | + (mi_r, mi_c): (usize, usize), | ||
| 455 | + (has_above, has_left): (bool, bool), | ||
| 456 | + compound: bool, | ||
| 457 | +) { | ||
| 458 | + if !REFERENCE_SELECT.with(std::cell::Cell::get) { | ||
| 459 | + return; | ||
| 460 | + } | ||
| 461 | + let above = has_above.then(|| neighbours.above_nbr(mi_c)); | ||
| 462 | + let left = has_left.then(|| neighbours.left_nbr(mi_r)); | ||
| 463 | + let ctx = crate::mvstack::reference_mode_ctx(above, left); | ||
| 464 | + if crate::envflags::env_flag!("EC_TRACE_MODE") { | ||
| 465 | + eprintln!( | ||
| 466 | + "EC_WCM mi=({mi_r},{mi_c}) ctx={ctx} val={} a={:?} l={:?}", | ||
| 467 | + usize::from(compound), | ||
| 468 | + above.map(|n| (n.is_inter, n.ref0, n.ref1)), | ||
| 469 | + left.map(|n| (n.is_inter, n.ref0, n.ref1)) | ||
| 470 | + ); | ||
| 471 | + } | ||
| 472 | + enc.symbol(usize::from(compound), &mut cdfs.comp_mode[ctx]); | ||
| 180 | } | 473 | } |
| 181 | 474 | ||
| 182 | /// Clears the armed plan when the tile writer that consumed it returns. | 475 | /// Clears the armed plan when the tile writer that consumed it returns. |
| @@ -188,6 +481,8 @@ impl Drop for CdefIdxGuard { | |||
| 188 | LR_PLAN.with(|c| *c.borrow_mut() = None); | 481 | LR_PLAN.with(|c| *c.borrow_mut() = None); |
| 189 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); | 482 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); |
| 190 | SCREEN.with(|c| c.set(false)); | 483 | SCREEN.with(|c| c.set(false)); |
| 484 | + REFERENCE_SELECT.with(|c| c.set(false)); | ||
| 485 | + ORDER_HINTS.with(|c| c.set((7, 0, [0; 7]))); | ||
| 191 | } | 486 | } |
| 192 | } | 487 | } |
| 193 | 488 | ||
| @@ -719,6 +1014,29 @@ pub enum InterMode { | |||
| 719 | /// `NEWMV`: codes `mv` as a residual against the stack entry | 1014 | /// `NEWMV`: codes `mv` as a residual against the stack entry |
| 720 | /// [`InterInfo::ref_mv_idx`] names. | 1015 | /// [`InterInfo::ref_mv_idx`] names. |
| 721 | NewMv, | 1016 | NewMv, |
| 1017 | + /// `NEAREST_NEARESTMV`: both sides take the compound stack's own nearest | ||
| 1018 | + /// pair, no residual, no DRL. | ||
| 1019 | + NearestNearestMv, | ||
| 1020 | + /// `GLOBAL_GLOBALMV`: both sides take the identity global model's zero | ||
| 1021 | + /// vector. | ||
| 1022 | + GlobalGlobalMv, | ||
| 1023 | + /// `NEW_NEWMV`: both sides code a residual against the compound stack | ||
| 1024 | + /// entry [`InterInfo::ref_mv_idx`] names. | ||
| 1025 | + NewNewMv, | ||
| 1026 | +} | ||
| 1027 | + | ||
| 1028 | +impl InterMode { | ||
| 1029 | + /// The spec's `INTER_COMPOUND_MODES` index (libaom `enums.h` order, | ||
| 1030 | + /// `NEAREST_NEARESTMV = 0`..`NEW_NEWMV = 7`) for a compound mode, `None` | ||
| 1031 | + /// for a single-reference one. | ||
| 1032 | + fn compound_index(self) -> Option<usize> { | ||
| 1033 | + match self { | ||
| 1034 | + InterMode::NearestNearestMv => Some(0), | ||
| 1035 | + InterMode::GlobalGlobalMv => Some(6), | ||
| 1036 | + InterMode::NewNewMv => Some(7), | ||
| 1037 | + _ => None, | ||
| 1038 | + } | ||
| 1039 | + } | ||
| 722 | } | 1040 | } |
| 723 | 1041 | ||
| 724 | /// An inter-coded block's mode and motion vector, `mv` in the spec's 1/8-pel | 1042 | /// An inter-coded block's mode and motion vector, `mv` in the spec's 1/8-pel |
| @@ -739,6 +1057,13 @@ pub struct InterInfo { | |||
| 739 | /// `NEARMV` takes outright. Ignored by the two modes that code no DRL | 1057 | /// `NEARMV` takes outright. Ignored by the two modes that code no DRL |
| 740 | /// index (`NEARESTMV`, `GLOBALMV`). | 1058 | /// index (`NEARESTMV`, `GLOBALMV`). |
| 741 | pub ref_mv_idx: u8, | 1059 | pub ref_mv_idx: u8, |
| 1060 | + /// The block's second reference (spec `RefFrames[1]`), `None` for a | ||
| 1061 | + /// single-reference block. `Some` only on a `reference_select` frame, and | ||
| 1062 | + /// only beside one of [`InterMode`]'s compound modes. | ||
| 1063 | + pub ref1: Option<i8>, | ||
| 1064 | + /// The motion vector into `ref1`, for the compound modes that code one | ||
| 1065 | + /// (`NEW_NEWMV`); the other two derive both sides. | ||
| 1066 | + pub mv1: (i32, i32), | ||
| 742 | } | 1067 | } |
| 743 | 1068 | ||
| 744 | impl From<Vec<Coeff>> for BlockCoeffs { | 1069 | impl From<Vec<Coeff>> for BlockCoeffs { |
| @@ -924,6 +1249,23 @@ struct Neighbours { | |||
| 924 | above_ref: Vec<i8>, | 1249 | above_ref: Vec<i8>, |
| 925 | /// The same, down the left edge. | 1250 | /// The same, down the left edge. |
| 926 | left_ref: Vec<i8>, | 1251 | left_ref: Vec<i8>, |
| 1252 | + /// Each neighbour's SECOND reference, `None` for a single-reference or | ||
| 1253 | + /// intra one -- decode.rs `Neighbours::above_ref1`, read by every compound | ||
| 1254 | + /// context function (`reference_mode_ctx`, `comp_reference_type_ctx`, the | ||
| 1255 | + /// `comp_ref`/`comp_bwdref` votes). | ||
| 1256 | + above_ref1: Vec<Option<i8>>, | ||
| 1257 | + /// The same, down the left edge. | ||
| 1258 | + left_ref1: Vec<Option<i8>>, | ||
| 1259 | + /// What decode.rs `Neighbours::above_comp_group_idx` holds: a compound | ||
| 1260 | + /// neighbour's own `comp_group_idx`, or libaom's single-reference special | ||
| 1261 | + /// case (`3` for an ALTREF block, `0` otherwise). | ||
| 1262 | + above_comp_group_idx: Vec<u8>, | ||
| 1263 | + /// The same, down the left edge. | ||
| 1264 | + left_comp_group_idx: Vec<u8>, | ||
| 1265 | + /// The same for `compound_idx` (`1` for a single-ref ALTREF block). | ||
| 1266 | + above_compound_idx: Vec<u8>, | ||
| 1267 | + /// The same, down the left edge. | ||
| 1268 | + left_compound_idx: Vec<u8>, | ||
| 927 | /// The frame's true (unpadded) width and height, in 4x4 mode-info units, | 1269 | /// The frame's true (unpadded) width and height, in 4x4 mode-info units, |
| 928 | /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec | 1270 | /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec |
| 929 | /// `av1_set_entropy_contexts`): a block whose row or column run spills | 1271 | /// `av1_set_entropy_contexts`): a block whose row or column run spills |
| @@ -969,6 +1311,12 @@ impl Neighbours { | |||
| 969 | left_inter: vec![false; rows * (SUB / MI)], | 1311 | left_inter: vec![false; rows * (SUB / MI)], |
| 970 | above_ref: vec![-1; cols * (SUB / MI)], | 1312 | above_ref: vec![-1; cols * (SUB / MI)], |
| 971 | left_ref: vec![-1; rows * (SUB / MI)], | 1313 | left_ref: vec![-1; rows * (SUB / MI)], |
| 1314 | + above_ref1: vec![None; cols * (SUB / MI)], | ||
| 1315 | + left_ref1: vec![None; rows * (SUB / MI)], | ||
| 1316 | + above_comp_group_idx: vec![0; cols * (SUB / MI)], | ||
| 1317 | + left_comp_group_idx: vec![0; rows * (SUB / MI)], | ||
| 1318 | + above_compound_idx: vec![0; cols * (SUB / MI)], | ||
| 1319 | + left_compound_idx: vec![0; rows * (SUB / MI)], | ||
| 972 | above_tx: vec![0; cols * (SUB / MI)], | 1320 | above_tx: vec![0; cols * (SUB / MI)], |
| 973 | left_tx: vec![0; rows * (SUB / MI)], | 1321 | left_tx: vec![0; rows * (SUB / MI)], |
| 974 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], | 1322 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| @@ -991,6 +1339,9 @@ impl Neighbours { | |||
| 991 | self.left_skip.iter_mut().for_each(|s| *s = false); | 1339 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 992 | self.left_inter.iter_mut().for_each(|i| *i = false); | 1340 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 993 | self.left_ref.iter_mut().for_each(|r| *r = -1); | 1341 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 1342 | + self.left_ref1.iter_mut().for_each(|r| *r = None); | ||
| 1343 | + self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); | ||
| 1344 | + self.left_compound_idx.iter_mut().for_each(|r| *r = 0); | ||
| 994 | self.left_tx.iter_mut().for_each(|t| *t = 0); | 1345 | self.left_tx.iter_mut().for_each(|t| *t = 0); |
| 995 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); | 1346 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 996 | self.left_palette_size.iter_mut().for_each(|s| *s = 0); | 1347 | self.left_palette_size.iter_mut().for_each(|s| *s = 0); |
| @@ -1271,6 +1622,29 @@ impl Neighbours { | |||
| 1271 | self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame); | 1622 | self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame); |
| 1272 | } | 1623 | } |
| 1273 | 1624 | ||
| 1625 | + /// [`Self::record_inter_mi`]'s compound half (decode.rs | ||
| 1626 | + /// `Neighbours::record_compound_ctx_rect_mi`): overwrites the ALTREF | ||
| 1627 | + /// single-reference default with a real compound block's own second | ||
| 1628 | + /// reference and `comp_group_idx`/`compound_idx` bits, called right after | ||
| 1629 | + /// it for every compound-coded block. | ||
| 1630 | + fn record_compound_mi( | ||
| 1631 | + &mut self, | ||
| 1632 | + (mi_r, mi_c): (usize, usize), | ||
| 1633 | + side: usize, | ||
| 1634 | + ref1: i8, | ||
| 1635 | + comp_group_idx: u8, | ||
| 1636 | + compound_idx: u8, | ||
| 1637 | + ) { | ||
| 1638 | + for cell in 0..side / MI { | ||
| 1639 | + self.above_ref1[mi_c + cell] = Some(ref1); | ||
| 1640 | + self.left_ref1[mi_r + cell] = Some(ref1); | ||
| 1641 | + self.above_comp_group_idx[mi_c + cell] = comp_group_idx; | ||
| 1642 | + self.left_comp_group_idx[mi_r + cell] = comp_group_idx; | ||
| 1643 | + self.above_compound_idx[mi_c + cell] = compound_idx; | ||
| 1644 | + self.left_compound_idx[mi_r + cell] = compound_idx; | ||
| 1645 | + } | ||
| 1646 | + } | ||
| 1647 | + | ||
| 1274 | /// [`Self::record_inter`] taking the block's position directly in 4x4 | 1648 | /// [`Self::record_inter`] taking the block's position directly in 4x4 |
| 1275 | /// mode-info units, for a block finer than one [`SUB`] slot. | 1649 | /// mode-info units, for a block finer than one [`SUB`] slot. |
| 1276 | fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) { | 1650 | fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) { |
| @@ -1281,6 +1655,45 @@ impl Neighbours { | |||
| 1281 | self.left_inter[mi_r + cell] = is_inter; | 1655 | self.left_inter[mi_r + cell] = is_inter; |
| 1282 | self.above_ref[mi_c + cell] = ref_frame; | 1656 | self.above_ref[mi_c + cell] = ref_frame; |
| 1283 | self.left_ref[mi_r + cell] = ref_frame; | 1657 | self.left_ref[mi_r + cell] = ref_frame; |
| 1658 | + // decode.rs `record_inter`'s single-reference default: no second | ||
| 1659 | + // reference, and libaom's ALTREF special case in the two compound | ||
| 1660 | + // context bands. `record_compound_mi` overwrites both for a real | ||
| 1661 | + // compound block. | ||
| 1662 | + let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; | ||
| 1663 | + self.above_ref1[mi_c + cell] = None; | ||
| 1664 | + self.left_ref1[mi_r + cell] = None; | ||
| 1665 | + self.above_comp_group_idx[mi_c + cell] = if altref { 3 } else { 0 }; | ||
| 1666 | + self.left_comp_group_idx[mi_r + cell] = if altref { 3 } else { 0 }; | ||
| 1667 | + self.above_compound_idx[mi_c + cell] = u8::from(altref); | ||
| 1668 | + self.left_compound_idx[mi_r + cell] = u8::from(altref); | ||
| 1669 | + } | ||
| 1670 | + } | ||
| 1671 | + | ||
| 1672 | + /// The above neighbour as decode.rs' own `NeighbourRef`, for every | ||
| 1673 | + /// compound context function (`reference_mode_ctx`, | ||
| 1674 | + /// `comp_reference_type_ctx`, the `comp_ref`/`comp_bwdref` votes). | ||
| 1675 | + fn above_nbr(&self, mi_c: usize) -> crate::mvstack::NeighbourRef { | ||
| 1676 | + let is_inter = self.above_inter[mi_c]; | ||
| 1677 | + let ref0 = if is_inter { self.above_ref[mi_c] } else { 0 }; | ||
| 1678 | + let ref1 = self.above_ref1[mi_c]; | ||
| 1679 | + crate::mvstack::NeighbourRef { | ||
| 1680 | + is_inter, | ||
| 1681 | + ref0, | ||
| 1682 | + ref1, | ||
| 1683 | + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), | ||
| 1684 | + } | ||
| 1685 | + } | ||
| 1686 | + | ||
| 1687 | + /// [`Self::above_nbr`], down the left edge. | ||
| 1688 | + fn left_nbr(&self, mi_r: usize) -> crate::mvstack::NeighbourRef { | ||
| 1689 | + let is_inter = self.left_inter[mi_r]; | ||
| 1690 | + let ref0 = if is_inter { self.left_ref[mi_r] } else { 0 }; | ||
| 1691 | + let ref1 = self.left_ref1[mi_r]; | ||
| 1692 | + crate::mvstack::NeighbourRef { | ||
| 1693 | + is_inter, | ||
| 1694 | + ref0, | ||
| 1695 | + ref1, | ||
| 1696 | + uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), | ||
| 1284 | } | 1697 | } |
| 1285 | } | 1698 | } |
| 1286 | 1699 | ||
| @@ -3265,7 +3678,9 @@ fn write_single_ref( | |||
| 3265 | cdfs: &mut Cdfs, | 3678 | cdfs: &mut Cdfs, |
| 3266 | ref_frame: i8, | 3679 | ref_frame: i8, |
| 3267 | above_ref: i8, | 3680 | above_ref: i8, |
| 3681 | + above_ref1: Option<i8>, | ||
| 3268 | left_ref: i8, | 3682 | left_ref: i8, |
| 3683 | + left_ref1: Option<i8>, | ||
| 3269 | ) { | 3684 | ) { |
| 3270 | REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 3685 | REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 3271 | use crate::mvstack::{ | 3686 | use crate::mvstack::{ |
| @@ -3275,21 +3690,26 @@ fn write_single_ref( | |||
| 3275 | }; | 3690 | }; |
| 3276 | let above = (above_ref > 0).then_some(above_ref); | 3691 | let above = (above_ref > 0).then_some(above_ref); |
| 3277 | let left = (left_ref > 0).then_some(left_ref); | 3692 | let left = (left_ref > 0).then_some(left_ref); |
| 3693 | + // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound | ||
| 3694 | + // neighbour's references (decode.rs `read_single_ref`'s own | ||
| 3695 | + // `above_ref1`/`left_ref1`): dropping the second one reads a different | ||
| 3696 | + // context row for every block sitting under a compound neighbour, and so | ||
| 3697 | + // decodes a different reference NAME. | ||
| 3278 | let backward = ref_frame >= BWDREF_FRAME; | 3698 | let backward = ref_frame >= BWDREF_FRAME; |
| 3279 | enc.symbol( | 3699 | enc.symbol( |
| 3280 | usize::from(backward), | 3700 | usize::from(backward), |
| 3281 | - &mut cdfs.single_ref[single_ref_p1_ctx(above, None, left, None)][0], | 3701 | + &mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0], |
| 3282 | ); | 3702 | ); |
| 3283 | if backward { | 3703 | if backward { |
| 3284 | let is_altref = ref_frame == ALTREF_FRAME; | 3704 | let is_altref = ref_frame == ALTREF_FRAME; |
| 3285 | enc.symbol( | 3705 | enc.symbol( |
| 3286 | usize::from(is_altref), | 3706 | usize::from(is_altref), |
| 3287 | - &mut cdfs.single_ref[single_ref_p2_ctx(above, None, left, None)][1], | 3707 | + &mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1], |
| 3288 | ); | 3708 | ); |
| 3289 | if !is_altref { | 3709 | if !is_altref { |
| 3290 | enc.symbol( | 3710 | enc.symbol( |
| 3291 | usize::from(ref_frame == ALTREF2_FRAME), | 3711 | usize::from(ref_frame == ALTREF2_FRAME), |
| 3292 | - &mut cdfs.single_ref[single_ref_p6_ctx(above, None, left, None)][5], | 3712 | + &mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5], |
| 3293 | ); | 3713 | ); |
| 3294 | } | 3714 | } |
| 3295 | return; | 3715 | return; |
| @@ -3297,17 +3717,17 @@ fn write_single_ref( | |||
| 3297 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; | 3717 | let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; |
| 3298 | enc.symbol( | 3718 | enc.symbol( |
| 3299 | usize::from(far), | 3719 | usize::from(far), |
| 3300 | - &mut cdfs.single_ref[single_ref_p3_ctx(above, None, left, None)][2], | 3720 | + &mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2], |
| 3301 | ); | 3721 | ); |
| 3302 | if far { | 3722 | if far { |
| 3303 | enc.symbol( | 3723 | enc.symbol( |
| 3304 | usize::from(ref_frame == GOLDEN_FRAME), | 3724 | usize::from(ref_frame == GOLDEN_FRAME), |
| 3305 | - &mut cdfs.single_ref[single_ref_p5_ctx(above, None, left, None)][4], | 3725 | + &mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4], |
| 3306 | ); | 3726 | ); |
| 3307 | } else { | 3727 | } else { |
| 3308 | enc.symbol( | 3728 | enc.symbol( |
| 3309 | usize::from(ref_frame == LAST2_FRAME), | 3729 | usize::from(ref_frame == LAST2_FRAME), |
| 3310 | - &mut cdfs.single_ref[single_ref_p4_ctx(above, None, left, None)][3], | 3730 | + &mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3], |
| 3311 | ); | 3731 | ); |
| 3312 | } | 3732 | } |
| 3313 | } | 3733 | } |
| @@ -3411,6 +3831,12 @@ fn write_inter_mode( | |||
| 3411 | stack: &crate::mvstack::MvStack, | 3831 | stack: &crate::mvstack::MvStack, |
| 3412 | ) -> Result<((i32, i32), bool)> { | 3832 | ) -> Result<((i32, i32), bool)> { |
| 3413 | let idx = usize::from(info.ref_mv_idx); | 3833 | let idx = usize::from(info.ref_mv_idx); |
| 3834 | + if info.mode.compound_index().is_some() { | ||
| 3835 | + return Err(Error::unsupported( | ||
| 3836 | + "AV1 tile", | ||
| 3837 | + "a compound mode is written by `write_compound_block`, not here", | ||
| 3838 | + )); | ||
| 3839 | + } | ||
| 3414 | match info.mode { | 3840 | match info.mode { |
| 3415 | InterMode::NewMv => { | 3841 | InterMode::NewMv => { |
| 3416 | enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); | 3842 | enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); |
| @@ -3444,6 +3870,7 @@ fn write_inter_mode( | |||
| 3444 | note_inter_mode(1, idx); | 3870 | note_inter_mode(1, idx); |
| 3445 | Ok((mv, false)) | 3871 | Ok((mv, false)) |
| 3446 | } | 3872 | } |
| 3873 | + _ => unreachable!("compound modes are refused above"), | ||
| 3447 | } | 3874 | } |
| 3448 | } | 3875 | } |
| 3449 | 3876 | ||
| @@ -3845,9 +4272,33 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 3845 | 4272 | ||
| 3846 | let mode_for_tx; | 4273 | let mode_for_tx; |
| 3847 | if let Some(info) = block.inter { | 4274 | if let Some(info) = block.inter { |
| 3848 | - write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | 4275 | + write_comp_mode(&mut enc, &mut cdfs, &neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some()); |
| 3849 | - | ||
| 3850 | let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); | 4276 | let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); |
| 4277 | + if info.ref1.is_some() { | ||
| 4278 | + let (mv, mv1, is_new_mv) = write_compound_block( | ||
| 4279 | + &mut enc, &mut cdfs, &neighbours, &grid, (mi_row, mi_col), (8, 8), | ||
| 4280 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | ||
| 4281 | + )?; | ||
| 4282 | + for dr in 0..8 { | ||
| 4283 | + for dc in 0..8 { | ||
| 4284 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | ||
| 4285 | + is_inter: true, | ||
| 4286 | + ref_frame: info.ref_frame, | ||
| 4287 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | ||
| 4288 | + mv1: mv16(mv1), | ||
| 4289 | + mv: mv16(mv), | ||
| 4290 | + is_new_mv, | ||
| 4291 | + size: 8, | ||
| 4292 | + size_h: 8, | ||
| 4293 | + is_global_mv0: false, | ||
| 4294 | + is_global_mv1: false, | ||
| 4295 | + }); | ||
| 4296 | + } | ||
| 4297 | + } | ||
| 4298 | + mode_for_tx = 0; | ||
| 4299 | + } else { | ||
| 4300 | + 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]); | ||
| 4301 | + | ||
| 3851 | let stack = find_mv_stack( | 4302 | let stack = find_mv_stack( |
| 3852 | &grid, | 4303 | &grid, |
| 3853 | mi_row, | 4304 | mi_row, |
| @@ -3900,6 +4351,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 3900 | } | 4351 | } |
| 3901 | } | 4352 | } |
| 3902 | mode_for_tx = 0; | 4353 | mode_for_tx = 0; |
| 4354 | + } | ||
| 3903 | } else { | 4355 | } else { |
| 3904 | let mode = usize::from(block.mode); | 4356 | let mode = usize::from(block.mode); |
| 3905 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]); | 4357 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]); |
| @@ -3992,6 +4444,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 3992 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); | 4444 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); |
| 3993 | } | 4445 | } |
| 3994 | neighbours.record_inter(at, BLOCK, block.skip, is_inter, block_ref(block)); | 4446 | neighbours.record_inter(at, BLOCK, block.skip, is_inter, block_ref(block)); |
| 4447 | + record_block_compound(&mut neighbours, (mi_r, mi_c), BLOCK, block); | ||
| 3995 | } | 4448 | } |
| 3996 | } | 4449 | } |
| 3997 | } | 4450 | } |
| @@ -4050,9 +4503,34 @@ fn write_inter_frame_leaf( | |||
| 4050 | 4503 | ||
| 4051 | let mode_for_tx; | 4504 | let mode_for_tx; |
| 4052 | if let Some(info) = block.inter { | 4505 | if let Some(info) = block.inter { |
| 4053 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | 4506 | + write_comp_mode(enc, cdfs, neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some()); |
| 4054 | - | ||
| 4055 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | 4507 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 4508 | + if info.ref1.is_some() { | ||
| 4509 | + let n = SUB_MI as usize; | ||
| 4510 | + let (mv, mv1, is_new_mv) = write_compound_block( | ||
| 4511 | + enc, cdfs, neighbours, grid, (mi_row, mi_col), (n, n), | ||
| 4512 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | ||
| 4513 | + )?; | ||
| 4514 | + for dr in 0..n { | ||
| 4515 | + for dc in 0..n { | ||
| 4516 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | ||
| 4517 | + is_inter: true, | ||
| 4518 | + ref_frame: info.ref_frame, | ||
| 4519 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | ||
| 4520 | + mv1: mv16(mv1), | ||
| 4521 | + mv: mv16(mv), | ||
| 4522 | + is_new_mv, | ||
| 4523 | + size: n as u8, | ||
| 4524 | + size_h: n as u8, | ||
| 4525 | + is_global_mv0: false, | ||
| 4526 | + is_global_mv1: false, | ||
| 4527 | + }); | ||
| 4528 | + } | ||
| 4529 | + } | ||
| 4530 | + mode_for_tx = 0; | ||
| 4531 | + } else { | ||
| 4532 | + 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]); | ||
| 4533 | + | ||
| 4056 | let stack = find_mv_stack( | 4534 | let stack = find_mv_stack( |
| 4057 | grid, | 4535 | grid, |
| 4058 | mi_row, | 4536 | mi_row, |
| @@ -4086,6 +4564,7 @@ fn write_inter_frame_leaf( | |||
| 4086 | } | 4564 | } |
| 4087 | } | 4565 | } |
| 4088 | mode_for_tx = 0; | 4566 | mode_for_tx = 0; |
| 4567 | + } | ||
| 4089 | } else { | 4568 | } else { |
| 4090 | let mode = usize::from(block.mode); | 4569 | let mode = usize::from(block.mode); |
| 4091 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); | 4570 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); |
| @@ -4174,6 +4653,7 @@ fn write_inter_frame_leaf( | |||
| 4174 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); | 4653 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); |
| 4175 | } | 4654 | } |
| 4176 | neighbours.record_inter(at, SUB, block.skip, is_inter, block_ref(block)); | 4655 | neighbours.record_inter(at, SUB, block.skip, is_inter, block_ref(block)); |
| 4656 | + record_block_compound(neighbours, (mi_r, mi_c), SUB, block); | ||
| 4177 | Ok(()) | 4657 | Ok(()) |
| 4178 | } | 4658 | } |
| 4179 | 4659 | ||
| @@ -4243,9 +4723,33 @@ fn write_inter_frame_leaf8( | |||
| 4243 | 4723 | ||
| 4244 | let mode_for_tx; | 4724 | let mode_for_tx; |
| 4245 | if let Some(info) = block.inter { | 4725 | if let Some(info) = block.inter { |
| 4246 | - write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.left_ref[leaf_mi.0]); | 4726 | + write_comp_mode(enc, cdfs, neighbours, leaf_mi, (has_above, has_left), info.ref1.is_some()); |
| 4247 | - | ||
| 4248 | let (mi_row, mi_col) = leaf_mi; | 4727 | let (mi_row, mi_col) = leaf_mi; |
| 4728 | + if info.ref1.is_some() { | ||
| 4729 | + let (mv, mv1, is_new_mv) = write_compound_block( | ||
| 4730 | + enc, cdfs, neighbours, grid, (mi_row, mi_col), (2, 2), | ||
| 4731 | + (has_above, has_left), info, (mi_cols as usize, mi_rows as usize), | ||
| 4732 | + )?; | ||
| 4733 | + for dr in 0..2 { | ||
| 4734 | + for dc in 0..2 { | ||
| 4735 | + grid.set(mi_row + dr, mi_col + dc, MiInfo { | ||
| 4736 | + is_inter: true, | ||
| 4737 | + ref_frame: info.ref_frame, | ||
| 4738 | + ref_frame1: info.ref1.unwrap_or(NO_REF1), | ||
| 4739 | + mv1: mv16(mv1), | ||
| 4740 | + mv: mv16(mv), | ||
| 4741 | + is_new_mv, | ||
| 4742 | + size: 2, | ||
| 4743 | + size_h: 2, | ||
| 4744 | + is_global_mv0: false, | ||
| 4745 | + is_global_mv1: false, | ||
| 4746 | + }); | ||
| 4747 | + } | ||
| 4748 | + } | ||
| 4749 | + mode_for_tx = 0; | ||
| 4750 | + } else { | ||
| 4751 | + 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]); | ||
| 4752 | + | ||
| 4249 | let stack = find_mv_stack( | 4753 | let stack = find_mv_stack( |
| 4250 | grid, | 4754 | grid, |
| 4251 | mi_row, | 4755 | mi_row, |
| @@ -4279,6 +4783,7 @@ fn write_inter_frame_leaf8( | |||
| 4279 | } | 4783 | } |
| 4280 | } | 4784 | } |
| 4281 | mode_for_tx = 0; | 4785 | mode_for_tx = 0; |
| 4786 | + } | ||
| 4282 | } else { | 4787 | } else { |
| 4283 | let mode = usize::from(block.mode); | 4788 | let mode = usize::from(block.mode); |
| 4284 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]); | 4789 | enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]); |
| @@ -4344,6 +4849,7 @@ fn write_inter_frame_leaf8( | |||
| 4344 | ]; | 4849 | ]; |
| 4345 | neighbours.record_mi(leaf_mi, 8, &zero_grids); | 4850 | neighbours.record_mi(leaf_mi, 8, &zero_grids); |
| 4346 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some(), block_ref(block)); | 4851 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some(), block_ref(block)); |
| 4852 | + record_block_compound(neighbours, leaf_mi, 8, block); | ||
| 4347 | } else { | 4853 | } else { |
| 4348 | let grids = [ | 4854 | let grids = [ |
| 4349 | level_grid(&block.luma, TX8)?, | 4855 | level_grid(&block.luma, TX8)?, |
| @@ -4367,6 +4873,7 @@ fn write_inter_frame_leaf8( | |||
| 4367 | ); | 4873 | ); |
| 4368 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); | 4874 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); |
| 4369 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); | 4875 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); |
| 4876 | + record_block_compound(neighbours, leaf_mi, 8, block); | ||
| 4370 | } | 4877 | } |
| 4371 | Ok((block.skip, is_inter)) | 4878 | Ok((block.skip, is_inter)) |
| 4372 | } | 4879 | } |
| @@ -6648,6 +7155,8 @@ mod tests { | |||
| 6648 | fn an_all_skip_inter_superblock_is_small_and_reads_back() { | 7155 | fn an_all_skip_inter_superblock_is_small_and_reads_back() { |
| 6649 | let block = BlockCoeffs { | 7156 | let block = BlockCoeffs { |
| 6650 | inter: Some(InterInfo { | 7157 | inter: Some(InterInfo { |
| 7158 | + ref1: None, | ||
| 7159 | + mv1: (0, 0), | ||
| 6651 | ref_frame: crate::mvstack::LAST_FRAME, | 7160 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6652 | mode: InterMode::NearestMv, | 7161 | mode: InterMode::NearestMv, |
| 6653 | mv: (0, 0), | 7162 | mv: (0, 0), |
| @@ -6691,6 +7200,8 @@ mod tests { | |||
| 6691 | fn a_newmv_block_reads_back_the_exact_symbol_sequence_it_was_written_with() { | 7200 | fn a_newmv_block_reads_back_the_exact_symbol_sequence_it_was_written_with() { |
| 6692 | let nearest = BlockCoeffs { | 7201 | let nearest = BlockCoeffs { |
| 6693 | inter: Some(InterInfo { | 7202 | inter: Some(InterInfo { |
| 7203 | + ref1: None, | ||
| 7204 | + mv1: (0, 0), | ||
| 6694 | ref_frame: crate::mvstack::LAST_FRAME, | 7205 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6695 | mode: InterMode::NearestMv, | 7206 | mode: InterMode::NearestMv, |
| 6696 | mv: (0, 0), | 7207 | mv: (0, 0), |
| @@ -6701,6 +7212,8 @@ mod tests { | |||
| 6701 | }; | 7212 | }; |
| 6702 | let newmv = BlockCoeffs { | 7213 | let newmv = BlockCoeffs { |
| 6703 | inter: Some(InterInfo { | 7214 | inter: Some(InterInfo { |
| 7215 | + ref1: None, | ||
| 7216 | + mv1: (0, 0), | ||
| 6704 | ref_frame: crate::mvstack::LAST_FRAME, | 7217 | ref_frame: crate::mvstack::LAST_FRAME, |
| 6705 | mode: InterMode::NewMv, | 7218 | mode: InterMode::NewMv, |
| 6706 | mv: (0, 2), | 7219 | mv: (0, 2), |