diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index a8d4d1f6..62174ad2 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -351,6 +351,34 @@ pub const TX_SIZE_CAT3: [[u16; 4]; 3] = [ [16803, 22759, 32768, 0], ]; +/// `default_switchable_interp_cdf` (spec 9.4): a `SWITCHABLE`-filter frame's +/// per-block `interp_filter[dir]` (spec 5.11.20's `read_interpolation_filter`, +/// three symbols -- `EIGHTTAP`/`EIGHTTAP_SMOOTH`/`EIGHTTAP_SHARP`, `BILINEAR` +/// is never chosen this way), indexed by `av1_get_pred_context_switchable_interp`'s +/// `ctx_offset + dir * INTER_FILTER_DIR_OFFSET + neighbour_term` +/// (`INTER_FILTER_DIR_OFFSET` = 8; this decoder never codes a compound +/// reference, so `ctx_offset` -- `INTER_FILTER_COMP_OFFSET` on a second +/// reference frame -- is always 0, leaving rows 0..=3 for `dir` 0 and 8..=11 +/// for `dir` 1). +pub const SWITCHABLE_INTERP: [[u16; 4]; 16] = [ + [31935, 32720, 32768, 0], + [5568, 32719, 32768, 0], + [422, 2938, 32768, 0], + [28244, 32608, 32768, 0], + [31206, 31953, 32768, 0], + [4862, 32121, 32768, 0], + [770, 1152, 32768, 0], + [20889, 25637, 32768, 0], + [31910, 32724, 32768, 0], + [4120, 32712, 32768, 0], + [305, 2247, 32768, 0], + [27403, 32636, 32768, 0], + [31022, 32009, 32768, 0], + [2963, 32093, 32768, 0], + [601, 943, 32768, 0], + [14969, 21398, 32768, 0], +]; + /// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane /// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by /// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`, diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 9d4f5b61..aac289f0 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -284,6 +284,9 @@ pub(crate) struct Cdfs { pub tx_size_cat2: [[u16; 4]; 3], /// `tx_depth` at a 64x64 block -- see [`cdf::TX_SIZE_CAT3`]. pub tx_size_cat3: [[u16; 4]; 3], + /// A `SWITCHABLE`-filter inter block's own `interp_filter[dir]` -- see + /// [`cdf::SWITCHABLE_INTERP`]. + pub switchable_interp: [[u16; 4]; 16], } /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`, @@ -827,6 +830,7 @@ impl Cdfs { tx_size_cat1: cdf::TX_SIZE_CAT1, tx_size_cat2: cdf::TX_SIZE_CAT2, tx_size_cat3: cdf::TX_SIZE_CAT3, + switchable_interp: cdf::SWITCHABLE_INTERP, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index de893e05..e74c6855 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -455,6 +455,12 @@ struct Neighbours { /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`. above_inter: Vec, left_inter: Vec, + /// `interp_filter[0]`/`[1]` (horizontal/vertical kernel index, 0..=2, or + /// `3` = "no info": an intra neighbour or the frame's own border) of the + /// block above/left of this column/row -- spec + /// `av1_get_pred_context_switchable_interp`'s own neighbour lookup. + above_filter: Vec<[u8; 2]>, + left_filter: Vec<[u8; 2]>, mi_cols: usize, mi_rows: usize, /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame, @@ -497,6 +503,8 @@ impl Neighbours { left_skip: vec![false; rows], above_inter: vec![false; cols], left_inter: vec![false; rows], + above_filter: vec![[3u8; 2]; cols], + left_filter: vec![[3u8; 2]; rows], mi_cols, mi_rows, skip_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)], @@ -577,17 +585,30 @@ impl Neighbours { self.left_side_mi.iter_mut().for_each(|s| *s = SB); self.left_skip.iter_mut().for_each(|s| *s = false); self.left_inter.iter_mut().for_each(|i| *i = false); + self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]); } - /// Records a block's `skip`/`is_inter` state for the next block that - /// reads it as a neighbour -- [`crate::tile`]'s `record_inter`. - fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool) { + /// Records a block's `skip`/`is_inter`/`interp_filter` state for the next + /// block that reads it as a neighbour -- [`crate::tile`]'s + /// `record_inter`. `filter` is `[3, 3]` for an intra block (spec + /// `get_ref_filter_type`'s "not this ref frame" case reads the same as + /// "no neighbour"). + fn record_inter( + &mut self, + at: (usize, usize), + side: usize, + skip: bool, + is_inter: bool, + filter: [u8; 2], + ) { let (r, c) = at; for cell in 0..side / SUB { self.above_skip[c + cell] = skip; self.left_skip[r + cell] = skip; self.above_inter[c + cell] = is_inter; self.left_inter[r + cell] = is_inter; + self.above_filter[c + cell] = filter; + self.left_filter[r + cell] = filter; } } @@ -2861,6 +2882,75 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( )) } +/// `av1_get_pred_context_switchable_interp`'s context for `interp_filter[dir]` +/// -- `above`/`left` are the neighbour's own resolved index for this +/// direction (0..=2, or `3` = no info, spec `SWITCHABLE_FILTERS`). This +/// decoder never codes a compound reference, so `INTER_FILTER_COMP_OFFSET` +/// is always 0; only `dir`'s own `INTER_FILTER_DIR_OFFSET` (8) is folded in. +fn switchable_interp_ctx(above: u8, left: u8, dir: usize) -> usize { + let base = dir * 8; + let term = if left == above { + left + } else if left == 3 { + above + } else if above == 3 { + left + } else { + 3 + }; + base + term as usize +} + +/// Resolves the interpolation filter kernel(s) an inter block's motion +/// compensation reads (spec 5.11.10/5.11.20): a fixed-filter frame +/// (`interp_fixed = Some`) never reads a symbol; a `Switchable` frame +/// (`interp_fixed = None`) reads `interp_filter[0]` and, when +/// `enable_dual_filter`, a second `interp_filter[1]` -- unless +/// `force_regular` (this block's own `needs_interp_filter() == 0` case: a +/// `GLOBALMV` block, whose global motion is always `IDENTITY` in this +/// decoder's scope, spec 5.11.10's `large && YMode == GLOBALMV` branch), +/// which forces `EIGHTTAP` without reading anything. +#[allow(clippy::too_many_arguments)] +fn resolve_interp_filter( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + interp_fixed: Option, + enable_dual_filter: bool, + force_regular: bool, + above: [u8; 2], + left: [u8; 2], +) -> (mc::InterpFilterKind, mc::InterpFilterKind, [u8; 2]) { + if let Some(kind) = interp_fixed { + return (kind, kind, [3, 3]); + } + if force_regular { + return ( + mc::InterpFilterKind::Regular, + mc::InterpFilterKind::Regular, + [0, 0], + ); + } + // libaom `av1_extract_interp_filter`/`read_mb_interp_filter`: the + // symbol read at `dir=0` becomes the block's *vertical* (`y_filter`) + // kernel, `dir=1` the *horizontal* (`x_filter`) one -- `dir` is not + // "horizontal then vertical" despite `predict_with_filters`' own + // `h_kind`/`v_kind` argument order, which is why this function returns + // `(h, v, ..)` but reads `dir0` (`v`) before `dir1` (`h`). + let ctx0 = switchable_interp_ctx(above[0], left[0], 0); + let sym0 = dec.symbol(&mut cdfs.switchable_interp[ctx0]) as u8; + let sym1 = if enable_dual_filter { + let ctx1 = switchable_interp_ctx(above[1], left[1], 1); + dec.symbol(&mut cdfs.switchable_interp[ctx1]) as u8 + } else { + sym0 + }; + ( + mc::InterpFilterKind::from_switchable_symbol(sym1 as usize), + mc::InterpFilterKind::from_switchable_symbol(sym0 as usize), + [sym0, sym1], + ) +} + /// The `is_inter` context (spec 5.11.16 via `av1_get_intra_inter_context`), /// duplicating [`crate::tile`]'s private copy of the same rule. fn intra_inter_ctx(has_above: bool, has_left: bool, above_inter: bool, left_inter: bool) -> usize { @@ -3050,6 +3140,8 @@ fn decode_inter_block( scan_chroma: &[u16], size_group: usize, allow_high_precision_mv: bool, + interp_fixed: Option, + enable_dual_filter: bool, ) -> Result<()> { const LAST_FRAME: i8 = 1; @@ -3068,6 +3160,7 @@ fn decode_inter_block( let mode_for_tx; let (luma_grid, u_grid, v_grid); + let block_filter; if is_inter { let sr_ctx = single_ref_ctx(above_inter || left_inter); let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]); @@ -3093,6 +3186,7 @@ fn decode_inter_block( ); let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; + let mut is_globalmv = false; let (mv, is_new_mv) = if !not_new { if stack.entries.len() > 1 { dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); @@ -3109,6 +3203,7 @@ fn decode_inter_block( ) } else { let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; + is_globalmv = !not_zero; let mv = if !not_zero { // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1) under the // `GmType::IDENTITY` this crate's `decode_stream` requires of @@ -3138,6 +3233,16 @@ fn decode_inter_block( }; (mv, false) }; + let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( + dec, + cdfs, + interp_fixed, + enable_dual_filter, + is_globalmv, + neighbours.above_filter[c], + neighbours.left_filter[r], + ); + block_filter = resolved_filter; if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( "EC_TRACE mi_row={mi_row} mi_col={mi_col} skip={} is_inter=1 mv=({},{}) is_new_mv={is_new_mv} bsize={side}", @@ -3162,7 +3267,7 @@ fn decode_inter_block( mode_for_tx = 0; let mut pred_y = vec![0u8; side * side]; - mc::predict( + mc::predict_with_filters( &ref_y.data, ref_y.width, ref_y.true_width, @@ -3171,10 +3276,12 @@ fn decode_inter_block( mv_to_q4(py, mv.0, true), side, side, + h_filter, + v_filter, &mut pred_y, ); let mut pred_u = vec![0u8; chroma_side * chroma_side]; - mc::predict( + mc::predict_with_filters( &ref_u.data, ref_u.width, ref_u.true_width, @@ -3183,10 +3290,12 @@ fn decode_inter_block( mv_to_q4(cpy, mv.0, false), chroma_side, chroma_side, + h_filter, + v_filter, &mut pred_u, ); let mut pred_v = vec![0u8; chroma_side * chroma_side]; - mc::predict( + mc::predict_with_filters( &ref_v.data, ref_v.width, ref_v.true_width, @@ -3195,6 +3304,8 @@ fn decode_inter_block( mv_to_q4(cpy, mv.0, false), chroma_side, chroma_side, + h_filter, + v_filter, &mut pred_v, ); @@ -3289,6 +3400,7 @@ fn decode_inter_block( return Err(unsupported("a directional chroma mode (round 2)")); }; mode_for_tx = mode; + block_filter = [3, 3]; let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); for dr in 0..side / 4 { for dc in 0..side / 4 { @@ -3408,7 +3520,7 @@ fn decode_inter_block( } } neighbours.record(at, side, mode_for_tx, &[luma_grid, u_grid, v_grid]); - neighbours.record_inter(at, side, skip, is_inter); + neighbours.record_inter(at, side, skip, is_inter, block_filter); neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); neighbours.fill_lf_grid( (r * (SUB / MI), c * (SUB / MI)), @@ -3833,6 +3945,8 @@ pub fn decode_inter_frame_tile( cdef: &CdefParams, loop_filter: &LoopFilterParams, allow_high_precision_mv: bool, + interp_fixed: Option, + enable_dual_filter: bool, ) -> Result { decode_inter_frame_tile_with_cdfs( data, @@ -3846,6 +3960,8 @@ pub fn decode_inter_frame_tile( loop_filter, None, allow_high_precision_mv, + interp_fixed, + enable_dual_filter, ) .map(|(picture, _)| picture) } @@ -3867,6 +3983,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( loop_filter: &LoopFilterParams, initial_cdfs: Option, allow_high_precision_mv: bool, + interp_fixed: Option, + enable_dual_filter: bool, ) -> Result<(Picture, Cdfs)> { if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); @@ -4014,6 +4132,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &scan16, 3, allow_high_precision_mv, + interp_fixed, + enable_dual_filter, )?; } PARTITION_SPLIT => { @@ -4067,6 +4187,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &scan8, 2, allow_high_precision_mv, + interp_fixed, + enable_dual_filter, )?; } else { let ctx16 = neighbours.partition_ctx(at16, SUB); @@ -4120,7 +4242,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( prev_leaf = Some((leaf_mi, skip, is_inter)); } if let Some((_, skip, is_inter)) = prev_leaf { - neighbours.record_inter(at16, SUB, skip, is_inter); + // corner-cut: `decode_inter_block8`'s + // leaves never read a switchable filter + // (round-3 GLOBALMV refusal already + // bounds them away from the Regular-only + // gap this round closes elsewhere); `[3, + // 3]` is "no info", the same value an + // intra neighbour records. Upgrade: + // thread `interp_fixed`/`enable_dual_filter` + // into `decode_inter_block8` once a + // straddling 8x8 SWITCHABLE stream is on + // hand to pin against. + neighbours.record_inter(at16, SUB, skip, is_inter, [3, 3]); } } } @@ -4606,6 +4739,8 @@ mod tests { &CdefParams::default(), &LoopFilterParams::default(), false, + Some(mc::InterpFilterKind::Regular), + false, ) .unwrap(); assert_eq!( @@ -4780,6 +4915,8 @@ mod tests { &[], 0, false, + Some(mc::InterpFilterKind::Regular), + false, ) .unwrap(); @@ -4870,6 +5007,8 @@ mod tests { &CdefParams::default(), &LoopFilterParams::default(), false, + Some(mc::InterpFilterKind::Regular), + false, ) .unwrap(); assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg"); diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index 6122de68..2c6821a2 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -79,6 +79,147 @@ const SUBPEL_FILTERS_4: [[i32; 8]; 16] = [ [0, 0, -2, 8, 126, -4, 0, 0], ]; +/// `Subpel_Filters[EIGHTTAP_SMOOTH]` (`filter.h`'s `av1_sub_pel_filters_8smooth`). +const SUBPEL_FILTERS_SMOOTH: [[i32; 8]; 16] = [ + [0, 0, 0, 128, 0, 0, 0, 0], + [0, 2, 28, 62, 34, 2, 0, 0], + [0, 0, 26, 62, 36, 4, 0, 0], + [0, 0, 22, 62, 40, 4, 0, 0], + [0, 0, 20, 60, 42, 6, 0, 0], + [0, 0, 18, 58, 44, 8, 0, 0], + [0, 0, 16, 56, 46, 10, 0, 0], + [0, -2, 16, 54, 48, 12, 0, 0], + [0, -2, 14, 52, 52, 14, -2, 0], + [0, 0, 12, 48, 54, 16, -2, 0], + [0, 0, 10, 46, 56, 16, 0, 0], + [0, 0, 8, 44, 58, 18, 0, 0], + [0, 0, 6, 42, 60, 20, 0, 0], + [0, 0, 4, 40, 62, 22, 0, 0], + [0, 0, 4, 36, 62, 26, 0, 0], + [0, 0, 2, 34, 62, 28, 2, 0], +]; + +/// `SUBPEL_FILTERS_SMOOTH`'s narrow-block counterpart (`av1_sub_pel_filters_4smooth`). +const SUBPEL_FILTERS_SMOOTH_4: [[i32; 8]; 16] = [ + [0, 0, 0, 128, 0, 0, 0, 0], + [0, 0, 30, 62, 34, 2, 0, 0], + [0, 0, 26, 62, 36, 4, 0, 0], + [0, 0, 22, 62, 40, 4, 0, 0], + [0, 0, 20, 60, 42, 6, 0, 0], + [0, 0, 18, 58, 44, 8, 0, 0], + [0, 0, 16, 56, 46, 10, 0, 0], + [0, 0, 14, 54, 48, 12, 0, 0], + [0, 0, 12, 52, 52, 12, 0, 0], + [0, 0, 12, 48, 54, 14, 0, 0], + [0, 0, 10, 46, 56, 16, 0, 0], + [0, 0, 8, 44, 58, 18, 0, 0], + [0, 0, 6, 42, 60, 20, 0, 0], + [0, 0, 4, 40, 62, 22, 0, 0], + [0, 0, 4, 36, 62, 26, 0, 0], + [0, 0, 2, 34, 62, 30, 0, 0], +]; + +/// `Subpel_Filters[EIGHTTAP_SHARP]` (`av1_sub_pel_filters_8sharp`) -- unlike +/// REGULAR/SMOOTH, spec 7.11.3.4 never swaps this for a narrow-block table. +const SUBPEL_FILTERS_SHARP: [[i32; 8]; 16] = [ + [0, 0, 0, 128, 0, 0, 0, 0], + [-2, 2, -6, 126, 8, -2, 2, 0], + [-2, 6, -12, 124, 16, -6, 4, -2], + [-2, 8, -18, 120, 26, -10, 6, -2], + [-4, 10, -22, 116, 38, -14, 6, -2], + [-4, 10, -22, 108, 48, -18, 8, -2], + [-4, 10, -24, 100, 60, -20, 8, -2], + [-4, 10, -24, 90, 70, -22, 10, -2], + [-4, 12, -24, 80, 80, -24, 12, -4], + [-2, 10, -22, 70, 90, -24, 10, -4], + [-2, 8, -20, 60, 100, -24, 10, -4], + [-2, 8, -18, 48, 108, -22, 10, -4], + [-2, 6, -14, 38, 116, -22, 10, -4], + [-2, 6, -10, 26, 120, -18, 8, -2], + [-2, 4, -6, 16, 124, -12, 6, -2], + [0, 2, -2, 8, 126, -6, 2, -2], +]; + +/// `Subpel_Filters[BILINEAR]` (`av1_bilinear_filters`) -- also never swapped +/// for a narrow-block table (it is already only 2 non-zero taps). +const SUBPEL_FILTERS_BILINEAR: [[i32; 8]; 16] = [ + [0, 0, 0, 128, 0, 0, 0, 0], + [0, 0, 0, 120, 8, 0, 0, 0], + [0, 0, 0, 112, 16, 0, 0, 0], + [0, 0, 0, 104, 24, 0, 0, 0], + [0, 0, 0, 96, 32, 0, 0, 0], + [0, 0, 0, 88, 40, 0, 0, 0], + [0, 0, 0, 80, 48, 0, 0, 0], + [0, 0, 0, 72, 56, 0, 0, 0], + [0, 0, 0, 64, 64, 0, 0, 0], + [0, 0, 0, 56, 72, 0, 0, 0], + [0, 0, 0, 48, 80, 0, 0, 0], + [0, 0, 0, 40, 88, 0, 0, 0], + [0, 0, 0, 32, 96, 0, 0, 0], + [0, 0, 0, 24, 104, 0, 0, 0], + [0, 0, 0, 16, 112, 0, 0, 0], + [0, 0, 0, 8, 120, 0, 0, 0], +]; + +/// Which of the spec's four `interpolation_filter` kernels a block's motion +/// compensation reads (spec 6.8.9 / `frame_header`'s `interpolation_filter`). +/// A frame codes exactly one of these when not `Switchable`; a `Switchable` +/// frame codes one per block per direction (spec 5.11.20's +/// `read_interp_filter`, see `decode::decode_inter_block`), so `predict`'s +/// callers resolve `Switchable` to a concrete pair of kinds themselves -- +/// this type only ever names the resolved kernel, never `Switchable` itself. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum InterpFilterKind { + Regular, + Smooth, + Sharp, + Bilinear, +} + +impl InterpFilterKind { + fn tables(self) -> (&'static [[i32; 8]; 16], &'static [[i32; 8]; 16]) { + match self { + InterpFilterKind::Regular => (&SUBPEL_FILTERS, &SUBPEL_FILTERS_4), + InterpFilterKind::Smooth => (&SUBPEL_FILTERS_SMOOTH, &SUBPEL_FILTERS_SMOOTH_4), + InterpFilterKind::Sharp => (&SUBPEL_FILTERS_SHARP, &SUBPEL_FILTERS_SHARP), + InterpFilterKind::Bilinear => (&SUBPEL_FILTERS_BILINEAR, &SUBPEL_FILTERS_BILINEAR), + } + } + + /// The three-symbol alphabet spec 5.11.20's `interp_filter[dir]` reads + /// under a `Switchable` frame decodes to (`EIGHTTAP`/`_SMOOTH`/`_SHARP` + /// in that order -- `BILINEAR` is never a `switchable_interp` outcome). + /// + /// # Panics + /// Panics when `symbol` is not `0..=2`. + pub fn from_switchable_symbol(symbol: usize) -> InterpFilterKind { + match symbol { + 0 => InterpFilterKind::Regular, + 1 => InterpFilterKind::Smooth, + 2 => InterpFilterKind::Sharp, + _ => panic!("switchable_interp's alphabet is exactly 3 symbols"), + } + } + + /// The frame header's own `interpolation_filter` (spec 6.8.9), for the + /// non-`Switchable` values a fixed-filter frame codes directly. + /// + /// # Panics + /// Panics on `Switchable`, which is not a concrete kernel -- callers + /// resolve it per block instead (spec 5.11.20). + pub fn from_header(filter: ec_av1_syntax::InterpolationFilter) -> InterpFilterKind { + match filter { + ec_av1_syntax::InterpolationFilter::Eighttap => InterpFilterKind::Regular, + ec_av1_syntax::InterpolationFilter::EighttapSmooth => InterpFilterKind::Smooth, + ec_av1_syntax::InterpolationFilter::EighttapSharp => InterpFilterKind::Sharp, + ec_av1_syntax::InterpolationFilter::Bilinear => InterpFilterKind::Bilinear, + ec_av1_syntax::InterpolationFilter::Switchable => { + panic!("Switchable is resolved per block, not as one frame-wide kernel") + } + } + } +} + /// A reference sample at `(x, y)`, clamped to the frame's true (unpadded) /// extent -- `true_width`/`true_height`, which can be narrower than /// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3` @@ -126,6 +267,69 @@ pub fn predict( block_w: usize, block_h: usize, dst: &mut [u8], +) { + predict_with_filter( + reference, + stride, + true_width, + true_height, + x_q4, + y_q4, + block_w, + block_h, + InterpFilterKind::Regular, + dst, + ); +} + +/// [`predict`], selecting the interpolation filter kernel explicitly (spec +/// 6.8.9's `interpolation_filter`) instead of always `Regular` -- the same +/// kernel both directions. +#[allow(clippy::too_many_arguments)] +pub fn predict_with_filter( + reference: &[u8], + stride: usize, + true_width: usize, + true_height: usize, + x_q4: i32, + y_q4: i32, + block_w: usize, + block_h: usize, + filter_kind: InterpFilterKind, + dst: &mut [u8], +) { + predict_with_filters( + reference, + stride, + true_width, + true_height, + x_q4, + y_q4, + block_w, + block_h, + filter_kind, + filter_kind, + dst, + ); +} + +/// [`predict_with_filter`], with the horizontal (`interp_filter[0]`) and +/// vertical (`interp_filter[1]`) kernels chosen independently -- spec +/// 5.11.20's per-block `SWITCHABLE` read is per-direction (`enable_dual_filter`), +/// so the two passes can genuinely differ. +#[allow(clippy::too_many_arguments)] +pub fn predict_with_filters( + reference: &[u8], + stride: usize, + true_width: usize, + true_height: usize, + x_q4: i32, + y_q4: i32, + block_w: usize, + block_h: usize, + h_kind: InterpFilterKind, + v_kind: InterpFilterKind, + dst: &mut [u8], ) { assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); assert!(!reference.is_empty(), "a reference plane has samples"); @@ -164,15 +368,17 @@ pub fn predict( return; } + let (h_wide, h_narrow) = h_kind.tables(); + let (v_wide, v_narrow) = v_kind.tables(); let h_filter = if block_w <= 4 { - &SUBPEL_FILTERS_4[xfrac] + &h_narrow[xfrac] } else { - &SUBPEL_FILTERS[xfrac] + &h_wide[xfrac] }; let v_filter = if block_h <= 4 { - &SUBPEL_FILTERS_4[yfrac] + &v_narrow[yfrac] } else { - &SUBPEL_FILTERS[yfrac] + &v_wide[yfrac] }; // The vertical pass reads 3 rows above and 4 below the block, so the @@ -207,7 +413,93 @@ pub fn predict( #[cfg(test)] mod tests { - use super::predict; + use super::{InterpFilterKind, predict, predict_with_filter}; + + /// Regression pin for the lane-av1flake ±1 defect: a real aomenc chroma + /// inter block at the frame's top-left corner (mv q4 = (row 7, col 15), + /// bw=bh=16) predicted the wrong value because this decoder always used + /// the REGULAR filter table -- aomdec's own `av1_convolve_2d_sr_c` trace + /// showed this block actually used `EIGHTTAP_SMOOTH` + /// (`0 0 2 34 62 28 2 0` at xfrac=15, not REGULAR's `0 0 -2 8 126 -6 2 + /// 0`). Window and expected output dumped straight from an + /// instrumented aomdec's `av1_convolve_2d_sr_c` on the pinned stream. + #[test] + fn smooth_filter_matches_aomdec_chroma_inter_block() { + #[rustfmt::skip] + let window: [[u8; 24]; 24] = [ + [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], + [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], + [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], + [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], + [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], + [114,114,114,114,114,114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116,115,115,115], + [114,114,114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,116,116,115,115,115,115], + [113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,116,116,116,116,115,115,115,115], + [113,113,113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,115,115,115,115,115,115], + [112,112,112,112,112,112,112,112,112,113,113,113,113,114,114,114,114,114,115,115,115,115,115,115], + [111,111,111,111,111,111,111,111,112,112,112,112,113,113,113,113,114,114,114,114,115,115,115,115], + [110,110,110,110,110,110,110,110,111,111,111,111,112,112,112,113,113,113,113,113,114,114,114,115], + [109,109,109,109,109,109,109,110,110,110,110,111,111,111,111,112,112,112,112,113,113,114,114,114], + [108,108,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111,111,111,112,112,113,113,113], + [108,108,108,108,108,108,108,108,108,108,109,109,109,110,110,110,110,110,111,111,112,112,112,112], + [107,107,107,107,107,107,107,107,107,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111], + [106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,110,110,110], + [106,106,106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,109], + [105,105,105,105,105,105,105,105,106,106,106,107,107,107,107,108,108,108,108,108,108,108,108,109], + [104,104,104,104,104,104,104,104,104,105,105,106,106,106,107,107,107,107,107,107,107,108,108,108], + [102,102,102,102,102,103,103,103,103,104,104,105,105,105,106,106,107,107,107,107,107,107,107,107], + [101,101,101,101,101,101,101,102,102,103,103,104,104,104,105,105,106,106,106,106,106,107,107,107], + [100,100,100,100,100,100,100,101,101,102,102,103,103,104,104,105,105,105,106,106,106,106,106,106], + [99,99,99,99,99,99,100,100,100,101,101,102,103,103,104,104,105,105,105,105,105,105,105,106], + ]; + // The dumped window spans real cols/rows -4..19; the block itself + // starts at real (0, 0), so pad the reference plane so col/row 4 of + // this buffer is real col/row 0, and hand `predict_with_filter` the + // real (0, 0) coordinates -- clamping then reproduces the identical + // extended border the dump already captured. + let stride = 24; + let mut reference = vec![0u8; stride * 24]; + for (r, row) in window.iter().enumerate() { + reference[r * stride..r * stride + stride].copy_from_slice(row); + } + // predict()'s own edge clamp starts at true (0,0); shift so the + // dumped window's row/col 4 lands there by biasing x_q4/y_q4's + // whole-pel part by -4 and reading from a plane whose true origin + // is the window's row/col 4 -- simplest is to pass true_width / + // true_height covering the whole dumped window and offset the + // whole-pel part of x_q4/y_q4 by +4 to land on real (0,0). + let x_q4 = (4 + 0) * 16 + 15; // real block x0=0, xfrac=15 + let y_q4 = (4 + 0) * 16 + 7; // real block y0=0, yfrac=7 + let mut dst = vec![0u8; 16 * 16]; + predict_with_filter( + &reference, + stride, + 24, + 24, + x_q4, + y_q4, + 16, + 16, + InterpFilterKind::Smooth, + &mut dst, + ); + #[rustfmt::skip] + let expected_row0: [u8; 16] = + [114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116]; + assert_eq!( + &dst[0..16], + &expected_row0, + "row0 vs aomdec's real av1_convolve_2d_sr_c output" + ); + #[rustfmt::skip] + let expected_row15: [u8; 16] = + [103,103,104,104,104,105,105,106,106,106,107,107,107,107,107,107]; + assert_eq!( + &dst[15 * 16..15 * 16 + 16], + &expected_row15, + "row15 vs aomdec" + ); + } #[test] fn integer_mv_is_identity() { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 7d495def..0f7b8cf8 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -184,6 +184,13 @@ pub fn decode_stream(data: &[u8]) -> Result> { let enable_filter_intra = parser .sequence_header() .is_some_and(|seq| seq.enable_filter_intra); + let enable_dual_filter = parser + .sequence_header() + .is_some_and(|seq| seq.enable_dual_filter); + let interp_fixed = match header.interpolation_filter { + ec_av1_syntax::InterpolationFilter::Switchable => None, + fixed => Some(crate::mc::InterpFilterKind::from_header(fixed)), + }; // Spec 7.20 `load_cdfs`: a frame naming a `primary_ref_frame` resumes // from that reference slot's saved CDF state instead of the spec 8.4 @@ -237,6 +244,8 @@ pub fn decode_stream(data: &[u8]) -> Result> { &header.loop_filter, initial_cdfs, header.allow_high_precision_mv, + interp_fixed, + enable_dual_filter, )? }; // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's @@ -1487,6 +1496,7 @@ mod tests { let obu = p.parse_obu(&stream[pos..]).expect("parse"); pos += obu.total_size; if let ObuKind::Frame(header, _) = obu.kind { + eprintln!("interpolation_filter: {:?}", header.interpolation_filter); eprintln!("cdef: {:?}", header.cdef); eprintln!("quantization: {:?}", header.quantization); eprintln!(