WIP(av1): single-ref LAST2/LAST3/BWDREF/ALTREF2/ALTREF decode — agent capped mid-verification
Browse files at this commit96e6529482bec19f8e9f7584b64ea821f61ced13Parent: 1ce9f543 changed files +168 -131
- crates/ec-av1/src/decode.rs +87 -103
- crates/ec-av1/src/mvstack.rs +56 -13
- crates/ec-av1/src/stream.rs +25 -15
crates/ec-av1/src/decode.rsmodified+87 -103
| @@ -32,8 +32,9 @@ use crate::mc; | ||
| 32 | 32 | use crate::msac::SymbolDecoder; |
| 33 | 33 | use crate::mvstack::{ |
| 34 | 34 | ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME, LAST3_FRAME, |
| 35 | - MiGrid, MiInfo, find_mv_stack, single_ref_ctx, single_ref_p1_ctx, single_ref_p2_ctx, | |
| 36 | - single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, | |
| 35 | + MiGrid, MiInfo, NO_SIGN_BIAS, SignBiasTable, find_mv_stack, find_mv_stack_with_sign_bias, | |
| 36 | + single_ref_ctx, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, | |
| 37 | + single_ref_p5_ctx, single_ref_p6_ctx, | |
| 37 | 38 | }; |
| 38 | 39 | use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; |
| 39 | 40 | use crate::transform::{TxType, dequant_and_inverse_typed}; |
| @@ -1248,6 +1249,14 @@ struct PlaneBuf { | ||
| 1248 | 1249 | true_height: usize, |
| 1249 | 1250 | } |
| 1250 | 1251 | |
| 1252 | +/// One entry per `MV_REFERENCE_FRAME` (index 0/1 unused -- `NONE`/ | |
| 1253 | +/// `INTRA_FRAME`/`LAST_FRAME` all resolve elsewhere), `Some` when this | |
| 1254 | +/// frame's own `ref_frame_idx` names a DPB slot that has a picture in it | |
| 1255 | +/// (lane-av1refs: the same "empty slot" case `GOLDEN_FRAME` already | |
| 1256 | +/// refused by name, now generic across `LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/ | |
| 1257 | +/// `ALTREF2`/`ALTREF`). | |
| 1258 | +type RefSlots<'a> = [Option<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)>; 8]; | |
| 1259 | + | |
| 1251 | 1260 | impl PlaneBuf { |
| 1252 | 1261 | fn edges( |
| 1253 | 1262 | &self, |
| @@ -3691,18 +3700,14 @@ fn decode_inter_block( | ||
| 3691 | 3700 | ref_y: &PlaneBuf, |
| 3692 | 3701 | ref_u: &PlaneBuf, |
| 3693 | 3702 | ref_v: &PlaneBuf, |
| 3694 | - // corner-cut (lane-av1refs r1): only `LAST_FRAME` (`ref_y`/`u`/`v`) and | |
| 3695 | - // `GOLDEN_FRAME` (`golden_*`, when the frame header's own | |
| 3696 | - // `ref_frame_idx` names a slot with a picture) are threaded down to MC. | |
| 3697 | - // Ceiling: `LAST2`/`LAST3`/`BWDREF`/`ALTREF2`/`ALTREF` still refuse by | |
| 3698 | - // name -- real short low-lag streams overwhelmingly pick `LAST` or | |
| 3699 | - // `GOLDEN` (the ledger's cdffwd trace). Upgrade path: widen `golden_*` | |
| 3700 | - // into a `[Option<&PlaneBuf>; REFS_PER_FRAME]` addressed by the decoded | |
| 3701 | - // reference, same as the CDF/picture DPB slots `decode_stream` already | |
| 3702 | - // keeps. | |
| 3703 | - golden_y: Option<&PlaneBuf>, | |
| 3704 | - golden_u: Option<&PlaneBuf>, | |
| 3705 | - golden_v: Option<&PlaneBuf>, | |
| 3703 | + // lane-av1refs: every non-`LAST_FRAME` reference this frame header's own | |
| 3704 | + // `ref_frame_idx` names a live DPB slot for, indexed `[ref_frame]` | |
| 3705 | + // (`LAST2_FRAME`=2 .. `ALTREF_FRAME`=7; index 0/1 unused, `LAST_FRAME` | |
| 3706 | + // stays on `ref_y`/`u`/`v` above) -- `None` at a live index means the | |
| 3707 | + // slot's still empty (an error-resilient stream, or too early in the | |
| 3708 | + // GOP), matching `GOLDEN_FRAME`'s existing empty-slot refusal below. | |
| 3709 | + other_refs: &RefSlots, | |
| 3710 | + sign_bias_table: &SignBiasTable, | |
| 3706 | 3711 | base_q_idx: u8, |
| 3707 | 3712 | luma_set_intra: TxbSet, |
| 3708 | 3713 | luma_set_inter: TxbSet, |
| @@ -3737,57 +3742,31 @@ fn decode_inter_block( | ||
| 3737 | 3742 | if is_inter { |
| 3738 | 3743 | let ref_frame = read_single_ref(dec, cdfs, neighbours.above_ref[c], neighbours.left_ref[r]); |
| 3739 | 3744 | ref_frame_for_lf = ref_frame; |
| 3740 | - // round 4-8 (lane-av1golden through lane-av1golden6): repeatedly | |
| 3741 | - // un-masked and re-masked chasing what looked like a GOLDEN_FRAME MC | |
| 3742 | - // bug -- lf_level `mode_deltas`/`ref_deltas` forwarding fixes | |
| 3743 | - // (rounds 5/6, both real and kept), and a switchable_interp ctx | |
| 3744 | - // ref-frame gate (round 8, real and kept). Every remaining | |
| 3745 | - // "GOLDEN_FRAME mismatch" chased past round 8 (including a frame-0 | |
| 3746 | - // *intra keyframe* luma mismatch that could not possibly depend on | |
| 3747 | - // this arm at all) turned out to be a single unrelated cause: round | |
| 3748 | - // 9 (lane-av1golden7) traced `fixtures/golden6-mismatch.obu`'s | |
| 3749 | - // frame-0 mismatch down to `header.film_grain.apply_grain == true` | |
| 3750 | - // (aomenc's `--tune-content=film` denoises the coded picture and | |
| 3751 | - // has the decoder re-synthesize grain per spec 7.18.3) -- this | |
| 3752 | - // decoder never implements grain synthesis at all, so it silently | |
| 3753 | - // returned the clean base picture while ffmpeg (dav1d) returned the | |
| 3754 | - // grain-synthesized one. That bug fires on *any* frame, key or | |
| 3755 | - // inter, and is orthogonal to which reference a block selects -- | |
| 3756 | - // the earlier "correlates with GOLDEN firing" pattern was survivor | |
| 3757 | - // bias: the golden gate only ever compared streams where | |
| 3758 | - // `non_last_ref_hits` fired, and `--tune-content=film` is part of | |
| 3759 | - // that same gate's own aomenc recipe. `decode_stream` now refuses | |
| 3760 | - // `apply_grain` streams by name instead of silently mismatching -- | |
| 3761 | - // but unmasking `GOLDEN_FRAME` with that fix in still shows a real, | |
| 3762 | - // separate residual MC bug: round 9 (lane-av1golden7)'s forwarding | |
| 3763 | - // gate hit 1/20 real aomenc streams mismatching (frame 3 luma, | |
| 3764 | - // apply_grain=false, non_last_ref_hits delta=2), pinned in | |
| 3765 | - // `fixtures/golden7-forwarding-mismatch.obu` for round 10. `GOLDEN_FRAME` | |
| 3766 | - // stays refused by name until that's found. | |
| 3767 | - let (py_ref, pu_ref, pv_ref) = match ref_frame { | |
| 3768 | - LAST_FRAME => (ref_y, ref_u, ref_v), | |
| 3769 | - GOLDEN_FRAME => match (golden_y, golden_u, golden_v) { | |
| 3770 | - (Some(gy), Some(gu), Some(gv)) => (gy, gu, gv), | |
| 3771 | - _ => { | |
| 3745 | + // round 4-9 (lane-av1golden..lane-av1golden7): GOLDEN_FRAME's own | |
| 3746 | + // stacked defects (lf_level ref/mode-delta forwarding, the | |
| 3747 | + // switchable_interp ref-frame ctx gate, and a film-grain synthesis | |
| 3748 | + // gap that looked like a GOLDEN MC bug by survivor bias -- see the | |
| 3749 | + // ledger) are all closed; GOLDEN_FRAME decodes live via `other_refs` | |
| 3750 | + // below same as every other non-`LAST_FRAME` reference. lane-av1refs | |
| 3751 | + // widens this from GOLDEN alone to every reference `read_single_ref` | |
| 3752 | + // can name. | |
| 3753 | + let (py_ref, pu_ref, pv_ref) = if ref_frame == LAST_FRAME { | |
| 3754 | + (ref_y, ref_u, ref_v) | |
| 3755 | + } else { | |
| 3756 | + match other_refs[ref_frame as usize] { | |
| 3757 | + Some((ry, ru, rv)) => (ry, ru, rv), | |
| 3758 | + None => { | |
| 3772 | 3759 | return Err(unsupported( |
| 3773 | - "GOLDEN_FRAME selected with no golden picture at this frame's \ | |
| 3774 | - ref_frame_idx[3] slot", | |
| 3760 | + "a reference frame selected with no picture at this frame's own \ | |
| 3761 | + ref_frame_idx slot for it", | |
| 3775 | 3762 | )); |
| 3776 | 3763 | } |
| 3777 | - }, | |
| 3778 | - _ => { | |
| 3779 | - return Err(unsupported( | |
| 3780 | - "a reference frame other than LAST_FRAME (LAST2/LAST3/GOLDEN/BWDREF/\ | |
| 3781 | - ALTREF2/ALTREF still have no MC path -- GOLDEN's residual MC bug is \ | |
| 3782 | - pinned in fixtures/golden7-forwarding-mismatch.obu, round 9 \ | |
| 3783 | - lane-av1golden7)", | |
| 3784 | - )); | |
| 3785 | 3764 | } |
| 3786 | 3765 | }; |
| 3787 | 3766 | |
| 3788 | 3767 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 3789 | 3768 | let bw4 = side / 4; |
| 3790 | - let stack = find_mv_stack( | |
| 3769 | + let stack = find_mv_stack_with_sign_bias( | |
| 3791 | 3770 | grid, |
| 3792 | 3771 | mi_row, |
| 3793 | 3772 | mi_col, |
| @@ -3796,6 +3775,7 @@ fn decode_inter_block( | ||
| 3796 | 3775 | ref_frame, |
| 3797 | 3776 | mi_cols as usize, |
| 3798 | 3777 | mi_rows as usize, |
| 3778 | + sign_bias_table, | |
| 3799 | 3779 | ); |
| 3800 | 3780 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 3801 | 3781 | let mut is_globalmv = false; |
| @@ -4615,7 +4595,7 @@ pub fn decode_inter_frame_tile( | ||
| 4615 | 4595 | frame_width: u32, |
| 4616 | 4596 | frame_height: u32, |
| 4617 | 4597 | reference: &Picture, |
| 4618 | - golden: Option<&Picture>, | |
| 4598 | + other_refs: [Option<&Picture>; 8], | |
| 4619 | 4599 | cdef: &CdefParams, |
| 4620 | 4600 | loop_filter: &LoopFilterParams, |
| 4621 | 4601 | allow_high_precision_mv: bool, |
| @@ -4630,11 +4610,12 @@ pub fn decode_inter_frame_tile( | ||
| 4630 | 4610 | frame_width, |
| 4631 | 4611 | frame_height, |
| 4632 | 4612 | reference, |
| 4633 | - golden, | |
| 4613 | + other_refs, | |
| 4634 | 4614 | cdef, |
| 4635 | 4615 | loop_filter, |
| 4636 | 4616 | None, |
| 4637 | 4617 | allow_high_precision_mv, |
| 4618 | + NO_SIGN_BIAS, | |
| 4638 | 4619 | interp_fixed, |
| 4639 | 4620 | enable_dual_filter, |
| 4640 | 4621 | ) |
| @@ -4654,11 +4635,12 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 4654 | 4635 | frame_width: u32, |
| 4655 | 4636 | frame_height: u32, |
| 4656 | 4637 | reference: &Picture, |
| 4657 | - golden: Option<&Picture>, | |
| 4638 | + other_refs: [Option<&Picture>; 8], | |
| 4658 | 4639 | cdef: &CdefParams, |
| 4659 | 4640 | loop_filter: &LoopFilterParams, |
| 4660 | 4641 | initial_cdfs: Option<Cdfs>, |
| 4661 | 4642 | allow_high_precision_mv: bool, |
| 4643 | + sign_bias_table: SignBiasTable, | |
| 4662 | 4644 | interp_fixed: Option<mc::InterpFilterKind>, |
| 4663 | 4645 | enable_dual_filter: bool, |
| 4664 | 4646 | ) -> Result<(Picture, Cdfs)> { |
| @@ -4696,38 +4678,43 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 4696 | 4678 | true_width: reference.width / 2, |
| 4697 | 4679 | true_height: reference.height / 2, |
| 4698 | 4680 | }; |
| 4699 | - // `GOLDEN_FRAME`'s own picture, when the frame header's `ref_frame_idx` | |
| 4700 | - // names a DPB slot holding one and it is this frame's own true size -- | |
| 4701 | - // `decode_inter_block`'s GOLDEN_FRAME arm refuses when it is `None` or a | |
| 4702 | - // mismatched size (spec 7.9 requires every active reference be a | |
| 4703 | - // compatible size; this decoder's single-slot-per-frame update makes | |
| 4704 | - // that always true for a stream this crate's own encoder writes). | |
| 4705 | - let (golden_y, golden_u, golden_v) = match golden { | |
| 4706 | - Some(g) if g.width == reference.width && g.height == reference.height => ( | |
| 4707 | - Some(PlaneBuf { | |
| 4708 | - data: g.y.clone(), | |
| 4709 | - width: g.width, | |
| 4710 | - height: g.height, | |
| 4711 | - true_width: g.width, | |
| 4712 | - true_height: g.height, | |
| 4713 | - }), | |
| 4714 | - Some(PlaneBuf { | |
| 4715 | - data: g.u.clone(), | |
| 4716 | - width: g.width / 2, | |
| 4717 | - height: g.height / 2, | |
| 4718 | - true_width: g.width / 2, | |
| 4719 | - true_height: g.height / 2, | |
| 4720 | - }), | |
| 4721 | - Some(PlaneBuf { | |
| 4722 | - data: g.v.clone(), | |
| 4723 | - width: g.width / 2, | |
| 4724 | - height: g.height / 2, | |
| 4725 | - true_width: g.width / 2, | |
| 4726 | - true_height: g.height / 2, | |
| 4727 | - }), | |
| 4728 | - ), | |
| 4729 | - _ => (None, None, None), | |
| 4730 | - }; | |
| 4681 | + // Every non-`LAST_FRAME` reference this frame header's own `ref_frame_idx` | |
| 4682 | + // names a live DPB slot for (lane-av1refs: generalised from the old | |
| 4683 | + // GOLDEN_FRAME-only block -- same empty-slot/size-mismatch refusal, | |
| 4684 | + // now per-reference). `decode_inter_block` refuses by name when the | |
| 4685 | + // block it is decoding selects an index that is still `None` here | |
| 4686 | + // (spec 7.9 requires every *active* reference be a compatible size; | |
| 4687 | + // an inactive one this frame never selects is simply left `None`). | |
| 4688 | + let owned_refs: [Option<(PlaneBuf, PlaneBuf, PlaneBuf)>; 8] = std::array::from_fn(|i| { | |
| 4689 | + other_refs[i] | |
| 4690 | + .filter(|g| g.width == reference.width && g.height == reference.height) | |
| 4691 | + .map(|g| { | |
| 4692 | + ( | |
| 4693 | + PlaneBuf { | |
| 4694 | + data: g.y.clone(), | |
| 4695 | + width: g.width, | |
| 4696 | + height: g.height, | |
| 4697 | + true_width: g.width, | |
| 4698 | + true_height: g.height, | |
| 4699 | + }, | |
| 4700 | + PlaneBuf { | |
| 4701 | + data: g.u.clone(), | |
| 4702 | + width: g.width / 2, | |
| 4703 | + height: g.height / 2, | |
| 4704 | + true_width: g.width / 2, | |
| 4705 | + true_height: g.height / 2, | |
| 4706 | + }, | |
| 4707 | + PlaneBuf { | |
| 4708 | + data: g.v.clone(), | |
| 4709 | + width: g.width / 2, | |
| 4710 | + height: g.height / 2, | |
| 4711 | + true_width: g.width / 2, | |
| 4712 | + true_height: g.height / 2, | |
| 4713 | + }, | |
| 4714 | + ) | |
| 4715 | + }) | |
| 4716 | + }); | |
| 4717 | + let ref_slots: RefSlots = std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); | |
| 4731 | 4718 | |
| 4732 | 4719 | let mut y = PlaneBuf { |
| 4733 | 4720 | data: vec![0u8; width * height], |
| @@ -4830,9 +4817,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 4830 | 4817 | &ref_y, |
| 4831 | 4818 | &ref_u, |
| 4832 | 4819 | &ref_v, |
| 4833 | - golden_y.as_ref(), | |
| 4834 | - golden_u.as_ref(), | |
| 4835 | - golden_v.as_ref(), | |
| 4820 | + &ref_slots, | |
| 4821 | + &sign_bias_table, | |
| 4836 | 4822 | base_q_idx, |
| 4837 | 4823 | TxbSet::Luma32, |
| 4838 | 4824 | TxbSet::Luma32Inter, |
| @@ -4888,9 +4874,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 4888 | 4874 | &ref_y, |
| 4889 | 4875 | &ref_u, |
| 4890 | 4876 | &ref_v, |
| 4891 | - golden_y.as_ref(), | |
| 4892 | - golden_u.as_ref(), | |
| 4893 | - golden_v.as_ref(), | |
| 4877 | + &ref_slots, | |
| 4878 | + &sign_bias_table, | |
| 4894 | 4879 | base_q_idx, |
| 4895 | 4880 | TxbSet::Luma16, |
| 4896 | 4881 | TxbSet::Luma16Inter, |
| @@ -5457,7 +5442,7 @@ mod tests { | ||
| 5457 | 5442 | width as u32, |
| 5458 | 5443 | height as u32, |
| 5459 | 5444 | &reference, |
| 5460 | - None, | |
| 5445 | + [None; 8], | |
| 5461 | 5446 | &CdefParams::default(), |
| 5462 | 5447 | &LoopFilterParams::default(), |
| 5463 | 5448 | false, |
| @@ -5627,9 +5612,8 @@ mod tests { | ||
| 5627 | 5612 | &ref_y, |
| 5628 | 5613 | &ref_u, |
| 5629 | 5614 | &ref_v, |
| 5630 | - None, | |
| 5631 | - None, | |
| 5632 | - None, | |
| 5615 | + &[None; 8], | |
| 5616 | + &NO_SIGN_BIAS, | |
| 5633 | 5617 | 100, |
| 5634 | 5618 | TxbSet::Luma16, |
| 5635 | 5619 | TxbSet::Luma16Inter, |
| @@ -5729,7 +5713,7 @@ mod tests { | ||
| 5729 | 5713 | coded_w, |
| 5730 | 5714 | coded_h, |
| 5731 | 5715 | &reference, |
| 5732 | - None, | |
| 5716 | + [None; 8], | |
| 5733 | 5717 | &CdefParams::default(), |
| 5734 | 5718 | &LoopFilterParams::default(), |
| 5735 | 5719 | false, |
crates/ec-av1/src/mvstack.rsmodified+56 -13
| @@ -227,18 +227,30 @@ fn add_candidate(candidates: &mut Vec<StackEntry>, mv: (i32, i32), weight: u32) | ||
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | -/// Whether `ref_frame`'s prediction runs backward in display order | |
| 231 | -/// (`cm->ref_frame_sign_bias`, libaom): flips a borrowed neighbour's MV | |
| 232 | -/// sign in [`process_single_ref_mv_candidate`] when the neighbour's own | |
| 233 | -/// reference disagrees. This decoder only ever threads forward references | |
| 234 | -/// (`LAST_FRAME`/`GOLDEN_FRAME`) to MC — never `BWDREF`/`ALTREF` — so every | |
| 235 | -/// call site compares `false != false` and the flip is always inert. Ported | |
| 236 | -/// anyway (never proved away, per this round's own bounds): a real | |
| 237 | -/// backward reference would need this to be correct on day one. | |
| 238 | -fn sign_bias(_ref_frame: i8) -> bool { | |
| 239 | - false | |
| 230 | +/// `cm->ref_frame_sign_bias[ref_frame]`, looked up from the frame header's | |
| 231 | +/// own `ref_frame_sign_bias` (spec 5.9.2's `get_relative_dist(ref_order_hint, | |
| 232 | +/// OrderHint) > 0` per active reference, already computed by | |
| 233 | +/// `ec_av1_syntax::frame::read_uncompressed_header` and indexed exactly like | |
| 234 | +/// `ref_frame_idx`: `table[ref_frame - LAST_FRAME]`). `find_mv_stack`'s | |
| 235 | +/// no-sign-bias callers (this crate's own encoder, which only ever writes | |
| 236 | +/// `LAST_FRAME`) pass an all-`false` table, matching this stub's old | |
| 237 | +/// always-`false` behaviour exactly. | |
| 238 | +fn sign_bias(table: &SignBiasTable, ref_frame: i8) -> bool { | |
| 239 | + table[(ref_frame - LAST_FRAME) as usize] | |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | +/// One `bool` per `MV_REFERENCE_FRAME` past `INTRA_FRAME`/`NONE` | |
| 243 | +/// (`LAST_FRAME`..=`ALTREF_FRAME`), indexed `[ref_frame - LAST_FRAME]` -- | |
| 244 | +/// spec `ec_av1_syntax::frame::FrameHeader::ref_frame_sign_bias`'s own | |
| 245 | +/// shape, threaded straight through. | |
| 246 | +pub type SignBiasTable = [bool; 7]; | |
| 247 | + | |
| 248 | +/// The all-`false` table every no-sign-bias caller (this crate's own | |
| 249 | +/// encoder, and every test below) passes: correct as long as no candidate | |
| 250 | +/// or query ever names a backward reference, exactly this module's old | |
| 251 | +/// hardcoded `sign_bias` stub. | |
| 252 | +pub const NO_SIGN_BIAS: SignBiasTable = [false; 7]; | |
| 253 | + | |
| 242 | 254 | /// libaom's `process_single_ref_mv_candidate` (mvref_common.c): folds one |
| 243 | 255 | /// already-coded neighbour into the stack as a low-weight (`2`, unboosted) |
| 244 | 256 | /// filler entry, used only by the single-reference extension pass below. |
| @@ -257,13 +269,14 @@ fn sign_bias(_ref_frame: i8) -> bool { | ||
| 257 | 269 | fn process_single_ref_mv_candidate( |
| 258 | 270 | candidate: &MiInfo, |
| 259 | 271 | ref_frame: i8, |
| 272 | + sign_bias_table: &SignBiasTable, | |
| 260 | 273 | candidates: &mut Vec<StackEntry>, |
| 261 | 274 | ) { |
| 262 | 275 | if !candidate.is_inter { |
| 263 | 276 | return; |
| 264 | 277 | } |
| 265 | 278 | let mut this_mv = candidate.mv; |
| 266 | - if sign_bias(candidate.ref_frame) != sign_bias(ref_frame) { | |
| 279 | + if sign_bias(sign_bias_table, candidate.ref_frame) != sign_bias(sign_bias_table, ref_frame) { | |
| 267 | 280 | this_mv = (-this_mv.0, -this_mv.1); |
| 268 | 281 | } |
| 269 | 282 | if !candidates.iter().any(|e| e.mv == this_mv) { |
| @@ -510,6 +523,36 @@ pub fn find_mv_stack( | ||
| 510 | 523 | ref_frame: i8, |
| 511 | 524 | mi_cols: usize, |
| 512 | 525 | mi_rows: usize, |
| 526 | +) -> MvStack { | |
| 527 | + find_mv_stack_with_sign_bias( | |
| 528 | + grid, | |
| 529 | + mi_row, | |
| 530 | + mi_col, | |
| 531 | + bw4, | |
| 532 | + bh4, | |
| 533 | + ref_frame, | |
| 534 | + mi_cols, | |
| 535 | + mi_rows, | |
| 536 | + &NO_SIGN_BIAS, | |
| 537 | + ) | |
| 538 | +} | |
| 539 | + | |
| 540 | +/// [`find_mv_stack`], with the frame header's real `ref_frame_sign_bias` | |
| 541 | +/// (lane-av1refs: `BWDREF_FRAME`/`ALTREF2_FRAME`/`ALTREF_FRAME` are forward | |
| 542 | +/// in display order, so the single-reference extension pass's borrowed-MV | |
| 543 | +/// flip is no longer always inert once one of them is live next to a | |
| 544 | +/// backward-biased neighbour). | |
| 545 | +#[allow(clippy::too_many_arguments)] | |
| 546 | +pub fn find_mv_stack_with_sign_bias( | |
| 547 | + grid: &MiGrid, | |
| 548 | + mi_row: usize, | |
| 549 | + mi_col: usize, | |
| 550 | + bw4: usize, | |
| 551 | + bh4: usize, | |
| 552 | + ref_frame: i8, | |
| 553 | + mi_cols: usize, | |
| 554 | + mi_rows: usize, | |
| 555 | + sign_bias_table: &SignBiasTable, | |
| 513 | 556 | ) -> MvStack { |
| 514 | 557 | let mut candidates = Vec::new(); |
| 515 | 558 | let mut newmv_count = 0u32; |
| @@ -703,7 +746,7 @@ pub fn find_mv_stack( | ||
| 703 | 746 | let cand = grid.get(mi_row - 1, mi_col + idx); |
| 704 | 747 | let step = cand.map_or(1, |c| c.size).max(1); |
| 705 | 748 | if let Some(c) = cand { |
| 706 | - process_single_ref_mv_candidate(c, ref_frame, &mut candidates); | |
| 749 | + process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates); | |
| 707 | 750 | } |
| 708 | 751 | idx += step; |
| 709 | 752 | } |
| @@ -714,7 +757,7 @@ pub fn find_mv_stack( | ||
| 714 | 757 | let cand = grid.get(mi_row + idx, mi_col - 1); |
| 715 | 758 | let step = cand.map_or(1, |c| c.size).max(1); |
| 716 | 759 | if let Some(c) = cand { |
| 717 | - process_single_ref_mv_candidate(c, ref_frame, &mut candidates); | |
| 760 | + process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates); | |
| 718 | 761 | } |
| 719 | 762 | idx += step; |
| 720 | 763 | } |
crates/ec-av1/src/stream.rsmodified+25 -15
| @@ -157,16 +157,17 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 157 | 157 | // model needs the full affine/translation MV derivation this |
| 158 | 158 | // decoder does not carry. `global_motion` is indexed `ref_frame - 1` |
| 159 | 159 | // (`read_global_motion_params`'s `i` loops from `LAST_FRAME`); |
| 160 | - // lane-av1refs widens this from `[0]` (LAST_FRAME) alone to every | |
| 161 | - // reference `decode_inter_block` can now select -- LAST_FRAME (index | |
| 162 | - // 0) and GOLDEN_FRAME (index 3). | |
| 160 | + // lane-av1refs widens this from `[0, 3]` (LAST_FRAME/GOLDEN_FRAME) | |
| 161 | + // to every one of the 7 single-reference slots `decode_inter_block` | |
| 162 | + // can now select. | |
| 163 | 163 | if header.frame_type != FrameType::Key |
| 164 | - && (header.global_motion[0].model != WarpModel::Identity | |
| 165 | - || header.global_motion[3].model != WarpModel::Identity) | |
| 164 | + && header.global_motion[..7] | |
| 165 | + .iter() | |
| 166 | + .any(|gm| gm.model != WarpModel::Identity) | |
| 166 | 167 | { |
| 167 | 168 | return Err(Error::unsupported( |
| 168 | 169 | "AV1 decode_stream", |
| 169 | - "an inter frame whose LAST_FRAME/GOLDEN_FRAME global motion is not IDENTITY (GLOBALMV needs the full warp derivation this decoder does not carry)", | |
| 170 | + "an inter frame whose global motion for a single-reference frame is not IDENTITY (GLOBALMV needs the full warp derivation this decoder does not carry)", | |
| 170 | 171 | )); |
| 171 | 172 | } |
| 172 | 173 | // `decode_inter_block`'s `single_ref_p*` chain codes only |
| @@ -255,14 +256,22 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 255 | 256 | "an inter frame with no key frame before it", |
| 256 | 257 | ) |
| 257 | 258 | })?; |
| 258 | - // `ref_frame_idx[3]` names GOLDEN_FRAME's own DPB slot (spec | |
| 259 | - // 7.8/7.20 -- `ref_frame_idx` is indexed `ref_frame - LAST_FRAME`). | |
| 260 | - // Unlike LAST_FRAME, GOLDEN_FRAME having no picture yet is not a | |
| 261 | - // stream error on its own: `decode_inter_block` only needs it | |
| 262 | - // when a block actually selects GOLDEN_FRAME, and refuses by | |
| 263 | - // name there if the slot is still empty. | |
| 264 | - let golden_slot = header.ref_frame_idx[3] as usize; | |
| 265 | - let golden = ref_slots.get(golden_slot).and_then(Option::as_ref); | |
| 259 | + // `ref_frame_idx[i]` names the DPB slot for `LAST_FRAME + i` | |
| 260 | + // (spec 7.8/7.20). Any of them having no picture yet is not a | |
| 261 | + // stream error on its own: `decode_inter_block` only needs a | |
| 262 | + // given slot when a block actually selects that reference, and | |
| 263 | + // refuses by name there if the slot is still empty -- widened | |
| 264 | + // (lane-av1refs) from `GOLDEN_FRAME`'s own slot alone to every | |
| 265 | + // one of the 7 single-reference slots. | |
| 266 | + let other_refs: [Option<&Picture>; 8] = std::array::from_fn(|ref_frame| { | |
| 267 | + if ref_frame == 0 { | |
| 268 | + None | |
| 269 | + } else { | |
| 270 | + ref_slots | |
| 271 | + .get(header.ref_frame_idx[ref_frame - 1] as usize) | |
| 272 | + .and_then(Option::as_ref) | |
| 273 | + } | |
| 274 | + }); | |
| 266 | 275 | decode_inter_frame_tile_with_cdfs( |
| 267 | 276 | tile_bytes, |
| 268 | 277 | header.mi_cols, |
| @@ -271,11 +280,12 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 271 | 280 | header.frame_width, |
| 272 | 281 | header.frame_height, |
| 273 | 282 | reference, |
| 274 | - golden, | |
| 283 | + other_refs, | |
| 275 | 284 | &header.cdef, |
| 276 | 285 | &header.loop_filter, |
| 277 | 286 | initial_cdfs, |
| 278 | 287 | header.allow_high_precision_mv, |
| 288 | + header.ref_frame_sign_bias, | |
| 279 | 289 | interp_fixed, |
| 280 | 290 | enable_dual_filter, |
| 281 | 291 | )? |