forge
Explore
Log in

Don't you dare go Hollow

Port comp_mode + compound ref-pair reads, not yet wired

Browse files at this commit

a75a2e1cab9a061ee3bd22cef4e0b0e4c716311cAhmet Kaan GümüşParent: ac83457
  • feat
    • port comp_mode + compound ref-pair reads, not yet wired
Show full message
feat(av1): port comp_mode + compound ref-pair reads, not yet wired

read_comp_mode (spec 5.11.25 comp_mode) and read_compound_ref_frames
(comp_reference_type + uni_comp_ref/comp_ref/comp_bwdref trees) mirror
libaom decodemv.c's read_ref_frames compound arm bit-for-bit. Audited
every libaom pred_common.c compound context function against the
existing single_ref_p1..p6_ctx/uni_comp_ref_p1_ctx helpers: each one
(uni_comp_ref_p/p2, comp_ref_p/p1/p2, comp_bwdref_p/p1) is the same
vote-the-neighbours shape under a different libaom name, so no new
context function was needed.

Not yet called from decode_inter_block/decode_inter_block8 -- the
frame-level reference_select refusal in stream.rs still gates every
caller, so this is dead code today (#[allow(dead_code)]), kept
compiling and reviewable for the round that wires it into the block
decode path and adds the inter_compound_mode/DRL/MV/MC chain.
crates/ec-av1/src/decode.rsmodified+97 -3
@@ -32,9 +32,10 @@ use crate::mc;
3232 use crate::msac::SymbolDecoder;
3333 use crate::mvstack::{
3434 ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME, LAST3_FRAME,
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,
35+ MiGrid, MiInfo, NO_SIGN_BIAS, NeighbourRef, SignBiasTable, comp_reference_type_ctx,
36+ find_mv_stack, find_mv_stack_with_sign_bias, reference_mode_ctx, single_ref_ctx,
37+ single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx,
38+ single_ref_p6_ctx, uni_comp_ref_p1_ctx,
3839 };
3940 use crate::tile::{INTRA_MODE_CTX, block_grid, has_half};
4041 use crate::transform::{TxType, dequant_and_inverse_typed};
@@ -3793,6 +3794,99 @@ fn read_single_ref(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, above_ref: i8, left
37933794 ref_frame
37943795 }
37953796
3797+/// Spec 5.11.25's `comp_mode` (lane-av1comp): whether an inter block reads
3798+/// `SINGLE_REFERENCE` or `COMPOUND_REFERENCE`, only reached once a frame's
3799+/// `reference_select` header bit lets a block choose per-block instead of
3800+/// fixing the mode. `above`/`left` are `None` for an intra/unavailable
3801+/// neighbour, [`NeighbourRef`] for a coded one -- [`reference_mode_ctx`]'s
3802+/// own doc. Not yet called from [`decode_inter_block`]/
3803+/// [`decode_inter_block8`]: `decode_stream`'s frame-level `reference_select`
3804+/// refusal still gates every caller, so this and
3805+/// [`read_compound_ref_frames`] are dead code today, kept compiling and
3806+/// reviewable for the round that wires them.
3807+#[allow(dead_code)]
3808+fn read_comp_mode(
3809+ dec: &mut SymbolDecoder,
3810+ cdfs: &mut Cdfs,
3811+ above: Option<NeighbourRef>,
3812+ left: Option<NeighbourRef>,
3813+) -> bool {
3814+ let ctx = reference_mode_ctx(above, left);
3815+ dec.symbol(&mut cdfs.comp_mode[ctx]) == 1
3816+}
3817+
3818+/// Spec 5.11.25's `comp_reference_type` + the `uni_comp_ref`/`comp_ref`/
3819+/// `comp_bwdref` trees (libaom `decodemv.c`'s `read_ref_frames` compound
3820+/// arm), reached once [`read_comp_mode`] has already returned
3821+/// `COMPOUND_REFERENCE`. Returns `(RefFrame[0], RefFrame[1])`.
3822+/// `above`/`left` are [`comp_reference_type_ctx`]'s own `NeighbourRef`
3823+/// convention; `above_ref`/`left_ref` are the scalar `1..=7`/`-1`
3824+/// convention [`read_single_ref`] already uses, for every downstream binary
3825+/// decision. libaom `pred_common.c`'s own compound context functions
3826+/// (`av1_get_pred_context_uni_comp_ref_p[1|2]`, `comp_ref_p[1|2]`,
3827+/// `comp_bwdref_p[1]`) turned out to be exactly [`single_ref_p1_ctx`]..
3828+/// [`single_ref_p6_ctx`]/[`uni_comp_ref_p1_ctx`] under a different name
3829+/// (same vote-the-neighbours-above-left shape, same forward/backward and
3830+/// LAST/LAST2/LAST3/GOLDEN groupings) -- lane-av1comp audited each one
3831+/// against the C source rather than porting six near-duplicate functions.
3832+#[allow(dead_code)]
3833+fn read_compound_ref_frames(
3834+ dec: &mut SymbolDecoder,
3835+ cdfs: &mut Cdfs,
3836+ above: Option<NeighbourRef>,
3837+ left: Option<NeighbourRef>,
3838+ above_ref: i8,
3839+ left_ref: i8,
3840+) -> (i8, i8) {
3841+ let a = (above_ref > 0).then_some(above_ref);
3842+ let l = (left_ref > 0).then_some(left_ref);
3843+ let type_ctx = comp_reference_type_ctx(above, left);
3844+ let unidir = dec.symbol(&mut cdfs.comp_ref_type[type_ctx]) == 0;
3845+ if unidir {
3846+ // uni_comp_ref (p0): forward vs. backward -- av1_get_pred_context_-
3847+ // uni_comp_ref_p is single_ref_p1's own forward/backward vote.
3848+ let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, l)][0]);
3849+ if bit == 1 {
3850+ (BWDREF_FRAME, ALTREF_FRAME)
3851+ } else {
3852+ let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, l)][1]);
3853+ if p1 == 1 {
3854+ // uni_comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5.
3855+ let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, l)][2]);
3856+ if p2 == 1 {
3857+ (LAST_FRAME, GOLDEN_FRAME)
3858+ } else {
3859+ (LAST_FRAME, LAST3_FRAME)
3860+ }
3861+ } else {
3862+ (LAST_FRAME, LAST2_FRAME)
3863+ }
3864+ }
3865+ } else {
3866+ // comp_ref (p0): LAST/LAST2 vs. LAST3/GOLDEN, same vote as single_ref_p3.
3867+ let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, l)][0]);
3868+ let ref0 = if bit0 == 0 {
3869+ // comp_ref_p1: LAST vs. LAST2, same vote as single_ref_p4.
3870+ let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, l)][1]);
3871+ if p1 == 1 { LAST2_FRAME } else { LAST_FRAME }
3872+ } else {
3873+ // comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5.
3874+ let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, l)][2]);
3875+ if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME }
3876+ };
3877+ // comp_bwdref (p0): BWDREF/ALTREF2 vs. ALTREF, same vote as single_ref_p2.
3878+ let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, l)][0]);
3879+ let ref1 = if bit1 == 0 {
3880+ // comp_bwdref_p1: BWDREF vs. ALTREF2, same vote as single_ref_p6.
3881+ let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, l)][1]);
3882+ if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME }
3883+ } else {
3884+ ALTREF_FRAME
3885+ };
3886+ (ref0, ref1)
3887+ }
3888+}
3889+
37963890 /// Decodes one square inter-frame block (32x32 whole or 16x16 leaf): skip,
37973891 /// `is_inter`, then either the single-reference/MV-stack/motion-vector chain
37983892 /// and motion-compensated reconstruction, or the inter frame's own intra