forge
Explore
Log in

Don't you dare go Hollow

Interpolation filters beyond REGULAR, incl. per-block SWITCHABLE

Browse files at this commit

10f24e61d7f47778f46bb0b75f7a19091a926d7cAhmet Kaan GümüşParent: ddd8c16
  • feat
    • interpolation filters beyond REGULAR, incl. per-block SWITCHABLE
Show full message
feat(av1): interpolation filters beyond REGULAR, incl. per-block SWITCHABLE

The ~13% decodes-but-wrong inter flake was a whole missing filter
family: motion compensation always used the REGULAR subpel tables,
while real encoders pick SMOOTH/SHARP per frame -- or SWITCHABLE,
reading a per-block filter symbol pair this decoder never read (the
silently-unread class again; our own encoder writes one fixed filter).
All four table sets land in mc.rs with an aomdec-pinned unit test, the
switchable_interp CDFs and neighbour context follow libaom's
read_mb_interp_filter, and the filter threads from the frame header
through both inter block paths. The forwarding gate ran 10/10 with
zero pixel mismatches and no vacuous skips.
crates/ec-av1/src/cdf.rsmodified+28 -0
@@ -351,6 +351,34 @@ pub const TX_SIZE_CAT3: [[u16; 4]; 3] = [
351 [16803, 22759, 32768, 0],351 [16803, 22759, 32768, 0],
352 ];352 ];
353 353
354+/// `default_switchable_interp_cdf` (spec 9.4): a `SWITCHABLE`-filter frame's
355+/// per-block `interp_filter[dir]` (spec 5.11.20's `read_interpolation_filter`,
356+/// three symbols -- `EIGHTTAP`/`EIGHTTAP_SMOOTH`/`EIGHTTAP_SHARP`, `BILINEAR`
357+/// is never chosen this way), indexed by `av1_get_pred_context_switchable_interp`'s
358+/// `ctx_offset + dir * INTER_FILTER_DIR_OFFSET + neighbour_term`
359+/// (`INTER_FILTER_DIR_OFFSET` = 8; this decoder never codes a compound
360+/// reference, so `ctx_offset` -- `INTER_FILTER_COMP_OFFSET` on a second
361+/// reference frame -- is always 0, leaving rows 0..=3 for `dir` 0 and 8..=11
362+/// for `dir` 1).
363+pub const SWITCHABLE_INTERP: [[u16; 4]; 16] = [
364+ [31935, 32720, 32768, 0],
365+ [5568, 32719, 32768, 0],
366+ [422, 2938, 32768, 0],
367+ [28244, 32608, 32768, 0],
368+ [31206, 31953, 32768, 0],
369+ [4862, 32121, 32768, 0],
370+ [770, 1152, 32768, 0],
371+ [20889, 25637, 32768, 0],
372+ [31910, 32724, 32768, 0],
373+ [4120, 32712, 32768, 0],
374+ [305, 2247, 32768, 0],
375+ [27403, 32636, 32768, 0],
376+ [31022, 32009, 32768, 0],
377+ [2963, 32093, 32768, 0],
378+ [601, 943, 32768, 0],
379+ [14969, 21398, 32768, 0],
380+];
381+
354 /// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane382 /// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane
355 /// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by383 /// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by
356 /// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`,384 /// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`,
crates/ec-av1/src/cdf_state.rsmodified+4 -0
@@ -284,6 +284,9 @@ pub(crate) struct Cdfs {
284 pub tx_size_cat2: [[u16; 4]; 3],284 pub tx_size_cat2: [[u16; 4]; 3],
285 /// `tx_depth` at a 64x64 block -- see [`cdf::TX_SIZE_CAT3`].285 /// `tx_depth` at a 64x64 block -- see [`cdf::TX_SIZE_CAT3`].
286 pub tx_size_cat3: [[u16; 4]; 3],286 pub tx_size_cat3: [[u16; 4]; 3],
287+ /// A `SWITCHABLE`-filter inter block's own `interp_filter[dir]` -- see
288+ /// [`cdf::SWITCHABLE_INTERP`].
289+ pub switchable_interp: [[u16; 4]; 16],
287 }290 }
288 291
289 /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`,292 /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`,
@@ -827,6 +830,7 @@ impl Cdfs {
827 tx_size_cat1: cdf::TX_SIZE_CAT1,830 tx_size_cat1: cdf::TX_SIZE_CAT1,
828 tx_size_cat2: cdf::TX_SIZE_CAT2,831 tx_size_cat2: cdf::TX_SIZE_CAT2,
829 tx_size_cat3: cdf::TX_SIZE_CAT3,832 tx_size_cat3: cdf::TX_SIZE_CAT3,
833+ switchable_interp: cdf::SWITCHABLE_INTERP,
830 }834 }
831 }835 }
832 836
crates/ec-av1/src/decode.rsmodified+147 -8
@@ -455,6 +455,12 @@ struct Neighbours {
455 /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`.455 /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`.
456 above_inter: Vec<bool>,456 above_inter: Vec<bool>,
457 left_inter: Vec<bool>,457 left_inter: Vec<bool>,
458+ /// `interp_filter[0]`/`[1]` (horizontal/vertical kernel index, 0..=2, or
459+ /// `3` = "no info": an intra neighbour or the frame's own border) of the
460+ /// block above/left of this column/row -- spec
461+ /// `av1_get_pred_context_switchable_interp`'s own neighbour lookup.
462+ above_filter: Vec<[u8; 2]>,
463+ left_filter: Vec<[u8; 2]>,
458 mi_cols: usize,464 mi_cols: usize,
459 mi_rows: usize,465 mi_rows: usize,
460 /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame,466 /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame,
@@ -497,6 +503,8 @@ impl Neighbours {
497 left_skip: vec![false; rows],503 left_skip: vec![false; rows],
498 above_inter: vec![false; cols],504 above_inter: vec![false; cols],
499 left_inter: vec![false; rows],505 left_inter: vec![false; rows],
506+ above_filter: vec![[3u8; 2]; cols],
507+ left_filter: vec![[3u8; 2]; rows],
500 mi_cols,508 mi_cols,
501 mi_rows,509 mi_rows,
502 skip_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)],510 skip_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)],
@@ -577,17 +585,30 @@ impl Neighbours {
577 self.left_side_mi.iter_mut().for_each(|s| *s = SB);585 self.left_side_mi.iter_mut().for_each(|s| *s = SB);
578 self.left_skip.iter_mut().for_each(|s| *s = false);586 self.left_skip.iter_mut().for_each(|s| *s = false);
579 self.left_inter.iter_mut().for_each(|i| *i = false);587 self.left_inter.iter_mut().for_each(|i| *i = false);
588+ self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]);
580 }589 }
581 590
582- /// Records a block's `skip`/`is_inter` state for the next block that591+ /// Records a block's `skip`/`is_inter`/`interp_filter` state for the next
583- /// reads it as a neighbour -- [`crate::tile`]'s `record_inter`.592+ /// block that reads it as a neighbour -- [`crate::tile`]'s
584- fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool) {593+ /// `record_inter`. `filter` is `[3, 3]` for an intra block (spec
594+ /// `get_ref_filter_type`'s "not this ref frame" case reads the same as
595+ /// "no neighbour").
596+ fn record_inter(
597+ &mut self,
598+ at: (usize, usize),
599+ side: usize,
600+ skip: bool,
601+ is_inter: bool,
602+ filter: [u8; 2],
603+ ) {
585 let (r, c) = at;604 let (r, c) = at;
586 for cell in 0..side / SUB {605 for cell in 0..side / SUB {
587 self.above_skip[c + cell] = skip;606 self.above_skip[c + cell] = skip;
588 self.left_skip[r + cell] = skip;607 self.left_skip[r + cell] = skip;
589 self.above_inter[c + cell] = is_inter;608 self.above_inter[c + cell] = is_inter;
590 self.left_inter[r + cell] = is_inter;609 self.left_inter[r + cell] = is_inter;
610+ self.above_filter[c + cell] = filter;
611+ self.left_filter[r + cell] = filter;
591 }612 }
592 }613 }
593 614
@@ -2861,6 +2882,75 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
2861 ))2882 ))
2862 }2883 }
2863 2884
2885+/// `av1_get_pred_context_switchable_interp`'s context for `interp_filter[dir]`
2886+/// -- `above`/`left` are the neighbour's own resolved index for this
2887+/// direction (0..=2, or `3` = no info, spec `SWITCHABLE_FILTERS`). This
2888+/// decoder never codes a compound reference, so `INTER_FILTER_COMP_OFFSET`
2889+/// is always 0; only `dir`'s own `INTER_FILTER_DIR_OFFSET` (8) is folded in.
2890+fn switchable_interp_ctx(above: u8, left: u8, dir: usize) -> usize {
2891+ let base = dir * 8;
2892+ let term = if left == above {
2893+ left
2894+ } else if left == 3 {
2895+ above
2896+ } else if above == 3 {
2897+ left
2898+ } else {
2899+ 3
2900+ };
2901+ base + term as usize
2902+}
2903+
2904+/// Resolves the interpolation filter kernel(s) an inter block's motion
2905+/// compensation reads (spec 5.11.10/5.11.20): a fixed-filter frame
2906+/// (`interp_fixed = Some`) never reads a symbol; a `Switchable` frame
2907+/// (`interp_fixed = None`) reads `interp_filter[0]` and, when
2908+/// `enable_dual_filter`, a second `interp_filter[1]` -- unless
2909+/// `force_regular` (this block's own `needs_interp_filter() == 0` case: a
2910+/// `GLOBALMV` block, whose global motion is always `IDENTITY` in this
2911+/// decoder's scope, spec 5.11.10's `large && YMode == GLOBALMV` branch),
2912+/// which forces `EIGHTTAP` without reading anything.
2913+#[allow(clippy::too_many_arguments)]
2914+fn resolve_interp_filter(
2915+ dec: &mut SymbolDecoder,
2916+ cdfs: &mut Cdfs,
2917+ interp_fixed: Option<mc::InterpFilterKind>,
2918+ enable_dual_filter: bool,
2919+ force_regular: bool,
2920+ above: [u8; 2],
2921+ left: [u8; 2],
2922+) -> (mc::InterpFilterKind, mc::InterpFilterKind, [u8; 2]) {
2923+ if let Some(kind) = interp_fixed {
2924+ return (kind, kind, [3, 3]);
2925+ }
2926+ if force_regular {
2927+ return (
2928+ mc::InterpFilterKind::Regular,
2929+ mc::InterpFilterKind::Regular,
2930+ [0, 0],
2931+ );
2932+ }
2933+ // libaom `av1_extract_interp_filter`/`read_mb_interp_filter`: the
2934+ // symbol read at `dir=0` becomes the block's *vertical* (`y_filter`)
2935+ // kernel, `dir=1` the *horizontal* (`x_filter`) one -- `dir` is not
2936+ // "horizontal then vertical" despite `predict_with_filters`' own
2937+ // `h_kind`/`v_kind` argument order, which is why this function returns
2938+ // `(h, v, ..)` but reads `dir0` (`v`) before `dir1` (`h`).
2939+ let ctx0 = switchable_interp_ctx(above[0], left[0], 0);
2940+ let sym0 = dec.symbol(&mut cdfs.switchable_interp[ctx0]) as u8;
2941+ let sym1 = if enable_dual_filter {
2942+ let ctx1 = switchable_interp_ctx(above[1], left[1], 1);
2943+ dec.symbol(&mut cdfs.switchable_interp[ctx1]) as u8
2944+ } else {
2945+ sym0
2946+ };
2947+ (
2948+ mc::InterpFilterKind::from_switchable_symbol(sym1 as usize),
2949+ mc::InterpFilterKind::from_switchable_symbol(sym0 as usize),
2950+ [sym0, sym1],
2951+ )
2952+}
2953+
2864 /// The `is_inter` context (spec 5.11.16 via `av1_get_intra_inter_context`),2954 /// The `is_inter` context (spec 5.11.16 via `av1_get_intra_inter_context`),
2865 /// duplicating [`crate::tile`]'s private copy of the same rule.2955 /// duplicating [`crate::tile`]'s private copy of the same rule.
2866 fn intra_inter_ctx(has_above: bool, has_left: bool, above_inter: bool, left_inter: bool) -> usize {2956 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(
3050 scan_chroma: &[u16],3140 scan_chroma: &[u16],
3051 size_group: usize,3141 size_group: usize,
3052 allow_high_precision_mv: bool,3142 allow_high_precision_mv: bool,
3143+ interp_fixed: Option<mc::InterpFilterKind>,
3144+ enable_dual_filter: bool,
3053 ) -> Result<()> {3145 ) -> Result<()> {
3054 const LAST_FRAME: i8 = 1;3146 const LAST_FRAME: i8 = 1;
3055 3147
@@ -3068,6 +3160,7 @@ fn decode_inter_block(
3068 3160
3069 let mode_for_tx;3161 let mode_for_tx;
3070 let (luma_grid, u_grid, v_grid);3162 let (luma_grid, u_grid, v_grid);
3163+ let block_filter;
3071 if is_inter {3164 if is_inter {
3072 let sr_ctx = single_ref_ctx(above_inter || left_inter);3165 let sr_ctx = single_ref_ctx(above_inter || left_inter);
3073 let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]);3166 let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]);
@@ -3093,6 +3186,7 @@ fn decode_inter_block(
3093 );3186 );
3094 3187
3095 let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;3188 let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;
3189+ let mut is_globalmv = false;
3096 let (mv, is_new_mv) = if !not_new {3190 let (mv, is_new_mv) = if !not_new {
3097 if stack.entries.len() > 1 {3191 if stack.entries.len() > 1 {
3098 dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]);3192 dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]);
@@ -3109,6 +3203,7 @@ fn decode_inter_block(
3109 )3203 )
3110 } else {3204 } else {
3111 let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1;3205 let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1;
3206+ is_globalmv = !not_zero;
3112 let mv = if !not_zero {3207 let mv = if !not_zero {
3113 // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1) under the3208 // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1) under the
3114 // `GmType::IDENTITY` this crate's `decode_stream` requires of3209 // `GmType::IDENTITY` this crate's `decode_stream` requires of
@@ -3138,6 +3233,16 @@ fn decode_inter_block(
3138 };3233 };
3139 (mv, false)3234 (mv, false)
3140 };3235 };
3236+ let (h_filter, v_filter, resolved_filter) = resolve_interp_filter(
3237+ dec,
3238+ cdfs,
3239+ interp_fixed,
3240+ enable_dual_filter,
3241+ is_globalmv,
3242+ neighbours.above_filter[c],
3243+ neighbours.left_filter[r],
3244+ );
3245+ block_filter = resolved_filter;
3141 if std::env::var_os("EC_AV1_TRACE").is_some() {3246 if std::env::var_os("EC_AV1_TRACE").is_some() {
3142 eprintln!(3247 eprintln!(
3143 "EC_TRACE mi_row={mi_row} mi_col={mi_col} skip={} is_inter=1 mv=({},{}) is_new_mv={is_new_mv} bsize={side}",3248 "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(
3162 mode_for_tx = 0;3267 mode_for_tx = 0;
3163 3268
3164 let mut pred_y = vec![0u8; side * side];3269 let mut pred_y = vec![0u8; side * side];
3165- mc::predict(3270+ mc::predict_with_filters(
3166 &ref_y.data,3271 &ref_y.data,
3167 ref_y.width,3272 ref_y.width,
3168 ref_y.true_width,3273 ref_y.true_width,
@@ -3171,10 +3276,12 @@ fn decode_inter_block(
3171 mv_to_q4(py, mv.0, true),3276 mv_to_q4(py, mv.0, true),
3172 side,3277 side,
3173 side,3278 side,
3279+ h_filter,
3280+ v_filter,
3174 &mut pred_y,3281 &mut pred_y,
3175 );3282 );
3176 let mut pred_u = vec![0u8; chroma_side * chroma_side];3283 let mut pred_u = vec![0u8; chroma_side * chroma_side];
3177- mc::predict(3284+ mc::predict_with_filters(
3178 &ref_u.data,3285 &ref_u.data,
3179 ref_u.width,3286 ref_u.width,
3180 ref_u.true_width,3287 ref_u.true_width,
@@ -3183,10 +3290,12 @@ fn decode_inter_block(
3183 mv_to_q4(cpy, mv.0, false),3290 mv_to_q4(cpy, mv.0, false),
3184 chroma_side,3291 chroma_side,
3185 chroma_side,3292 chroma_side,
3293+ h_filter,
3294+ v_filter,
3186 &mut pred_u,3295 &mut pred_u,
3187 );3296 );
3188 let mut pred_v = vec![0u8; chroma_side * chroma_side];3297 let mut pred_v = vec![0u8; chroma_side * chroma_side];
3189- mc::predict(3298+ mc::predict_with_filters(
3190 &ref_v.data,3299 &ref_v.data,
3191 ref_v.width,3300 ref_v.width,
3192 ref_v.true_width,3301 ref_v.true_width,
@@ -3195,6 +3304,8 @@ fn decode_inter_block(
3195 mv_to_q4(cpy, mv.0, false),3304 mv_to_q4(cpy, mv.0, false),
3196 chroma_side,3305 chroma_side,
3197 chroma_side,3306 chroma_side,
3307+ h_filter,
3308+ v_filter,
3198 &mut pred_v,3309 &mut pred_v,
3199 );3310 );
3200 3311
@@ -3289,6 +3400,7 @@ fn decode_inter_block(
3289 return Err(unsupported("a directional chroma mode (round 2)"));3400 return Err(unsupported("a directional chroma mode (round 2)"));
3290 };3401 };
3291 mode_for_tx = mode;3402 mode_for_tx = mode;
3403+ block_filter = [3, 3];
3292 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);3404 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
3293 for dr in 0..side / 4 {3405 for dr in 0..side / 4 {
3294 for dc in 0..side / 4 {3406 for dc in 0..side / 4 {
@@ -3408,7 +3520,7 @@ fn decode_inter_block(
3408 }3520 }
3409 }3521 }
3410 neighbours.record(at, side, mode_for_tx, &[luma_grid, u_grid, v_grid]);3522 neighbours.record(at, side, mode_for_tx, &[luma_grid, u_grid, v_grid]);
3411- neighbours.record_inter(at, side, skip, is_inter);3523+ neighbours.record_inter(at, side, skip, is_inter, block_filter);
3412 neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip);3524 neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip);
3413 neighbours.fill_lf_grid(3525 neighbours.fill_lf_grid(
3414 (r * (SUB / MI), c * (SUB / MI)),3526 (r * (SUB / MI), c * (SUB / MI)),
@@ -3833,6 +3945,8 @@ pub fn decode_inter_frame_tile(
3833 cdef: &CdefParams,3945 cdef: &CdefParams,
3834 loop_filter: &LoopFilterParams,3946 loop_filter: &LoopFilterParams,
3835 allow_high_precision_mv: bool,3947 allow_high_precision_mv: bool,
3948+ interp_fixed: Option<mc::InterpFilterKind>,
3949+ enable_dual_filter: bool,
3836 ) -> Result<Picture> {3950 ) -> Result<Picture> {
3837 decode_inter_frame_tile_with_cdfs(3951 decode_inter_frame_tile_with_cdfs(
3838 data,3952 data,
@@ -3846,6 +3960,8 @@ pub fn decode_inter_frame_tile(
3846 loop_filter,3960 loop_filter,
3847 None,3961 None,
3848 allow_high_precision_mv,3962 allow_high_precision_mv,
3963+ interp_fixed,
3964+ enable_dual_filter,
3849 )3965 )
3850 .map(|(picture, _)| picture)3966 .map(|(picture, _)| picture)
3851 }3967 }
@@ -3867,6 +3983,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3867 loop_filter: &LoopFilterParams,3983 loop_filter: &LoopFilterParams,
3868 initial_cdfs: Option<Cdfs>,3984 initial_cdfs: Option<Cdfs>,
3869 allow_high_precision_mv: bool,3985 allow_high_precision_mv: bool,
3986+ interp_fixed: Option<mc::InterpFilterKind>,
3987+ enable_dual_filter: bool,
3870 ) -> Result<(Picture, Cdfs)> {3988 ) -> Result<(Picture, Cdfs)> {
3871 if mi_cols == 0 || mi_rows == 0 {3989 if mi_cols == 0 || mi_rows == 0 {
3872 return Err(unsupported("a frame with no mode-info grid"));3990 return Err(unsupported("a frame with no mode-info grid"));
@@ -4014,6 +4132,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
4014 &scan16,4132 &scan16,
4015 3,4133 3,
4016 allow_high_precision_mv,4134 allow_high_precision_mv,
4135+ interp_fixed,
4136+ enable_dual_filter,
4017 )?;4137 )?;
4018 }4138 }
4019 PARTITION_SPLIT => {4139 PARTITION_SPLIT => {
@@ -4067,6 +4187,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
4067 &scan8,4187 &scan8,
4068 2,4188 2,
4069 allow_high_precision_mv,4189 allow_high_precision_mv,
4190+ interp_fixed,
4191+ enable_dual_filter,
4070 )?;4192 )?;
4071 } else {4193 } else {
4072 let ctx16 = neighbours.partition_ctx(at16, SUB);4194 let ctx16 = neighbours.partition_ctx(at16, SUB);
@@ -4120,7 +4242,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
4120 prev_leaf = Some((leaf_mi, skip, is_inter));4242 prev_leaf = Some((leaf_mi, skip, is_inter));
4121 }4243 }
4122 if let Some((_, skip, is_inter)) = prev_leaf {4244 if let Some((_, skip, is_inter)) = prev_leaf {
4123- neighbours.record_inter(at16, SUB, skip, is_inter);4245+ // corner-cut: `decode_inter_block8`'s
4246+ // leaves never read a switchable filter
4247+ // (round-3 GLOBALMV refusal already
4248+ // bounds them away from the Regular-only
4249+ // gap this round closes elsewhere); `[3,
4250+ // 3]` is "no info", the same value an
4251+ // intra neighbour records. Upgrade:
4252+ // thread `interp_fixed`/`enable_dual_filter`
4253+ // into `decode_inter_block8` once a
4254+ // straddling 8x8 SWITCHABLE stream is on
4255+ // hand to pin against.
4256+ neighbours.record_inter(at16, SUB, skip, is_inter, [3, 3]);
4124 }4257 }
4125 }4258 }
4126 }4259 }
@@ -4606,6 +4739,8 @@ mod tests {
4606 &CdefParams::default(),4739 &CdefParams::default(),
4607 &LoopFilterParams::default(),4740 &LoopFilterParams::default(),
4608 false,4741 false,
4742+ Some(mc::InterpFilterKind::Regular),
4743+ false,
4609 )4744 )
4610 .unwrap();4745 .unwrap();
4611 assert_eq!(4746 assert_eq!(
@@ -4780,6 +4915,8 @@ mod tests {
4780 &[],4915 &[],
4781 0,4916 0,
4782 false,4917 false,
4918+ Some(mc::InterpFilterKind::Regular),
4919+ false,
4783 )4920 )
4784 .unwrap();4921 .unwrap();
4785 4922
@@ -4870,6 +5007,8 @@ mod tests {
4870 &CdefParams::default(),5007 &CdefParams::default(),
4871 &LoopFilterParams::default(),5008 &LoopFilterParams::default(),
4872 false,5009 false,
5010+ Some(mc::InterpFilterKind::Regular),
5011+ false,
4873 )5012 )
4874 .unwrap();5013 .unwrap();
4875 assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg");5014 assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg");
crates/ec-av1/src/mc.rsmodified+297 -5
@@ -79,6 +79,147 @@ const SUBPEL_FILTERS_4: [[i32; 8]; 16] = [
79 [0, 0, -2, 8, 126, -4, 0, 0],79 [0, 0, -2, 8, 126, -4, 0, 0],
80 ];80 ];
81 81
82+/// `Subpel_Filters[EIGHTTAP_SMOOTH]` (`filter.h`'s `av1_sub_pel_filters_8smooth`).
83+const SUBPEL_FILTERS_SMOOTH: [[i32; 8]; 16] = [
84+ [0, 0, 0, 128, 0, 0, 0, 0],
85+ [0, 2, 28, 62, 34, 2, 0, 0],
86+ [0, 0, 26, 62, 36, 4, 0, 0],
87+ [0, 0, 22, 62, 40, 4, 0, 0],
88+ [0, 0, 20, 60, 42, 6, 0, 0],
89+ [0, 0, 18, 58, 44, 8, 0, 0],
90+ [0, 0, 16, 56, 46, 10, 0, 0],
91+ [0, -2, 16, 54, 48, 12, 0, 0],
92+ [0, -2, 14, 52, 52, 14, -2, 0],
93+ [0, 0, 12, 48, 54, 16, -2, 0],
94+ [0, 0, 10, 46, 56, 16, 0, 0],
95+ [0, 0, 8, 44, 58, 18, 0, 0],
96+ [0, 0, 6, 42, 60, 20, 0, 0],
97+ [0, 0, 4, 40, 62, 22, 0, 0],
98+ [0, 0, 4, 36, 62, 26, 0, 0],
99+ [0, 0, 2, 34, 62, 28, 2, 0],
100+];
101+
102+/// `SUBPEL_FILTERS_SMOOTH`'s narrow-block counterpart (`av1_sub_pel_filters_4smooth`).
103+const SUBPEL_FILTERS_SMOOTH_4: [[i32; 8]; 16] = [
104+ [0, 0, 0, 128, 0, 0, 0, 0],
105+ [0, 0, 30, 62, 34, 2, 0, 0],
106+ [0, 0, 26, 62, 36, 4, 0, 0],
107+ [0, 0, 22, 62, 40, 4, 0, 0],
108+ [0, 0, 20, 60, 42, 6, 0, 0],
109+ [0, 0, 18, 58, 44, 8, 0, 0],
110+ [0, 0, 16, 56, 46, 10, 0, 0],
111+ [0, 0, 14, 54, 48, 12, 0, 0],
112+ [0, 0, 12, 52, 52, 12, 0, 0],
113+ [0, 0, 12, 48, 54, 14, 0, 0],
114+ [0, 0, 10, 46, 56, 16, 0, 0],
115+ [0, 0, 8, 44, 58, 18, 0, 0],
116+ [0, 0, 6, 42, 60, 20, 0, 0],
117+ [0, 0, 4, 40, 62, 22, 0, 0],
118+ [0, 0, 4, 36, 62, 26, 0, 0],
119+ [0, 0, 2, 34, 62, 30, 0, 0],
120+];
121+
122+/// `Subpel_Filters[EIGHTTAP_SHARP]` (`av1_sub_pel_filters_8sharp`) -- unlike
123+/// REGULAR/SMOOTH, spec 7.11.3.4 never swaps this for a narrow-block table.
124+const SUBPEL_FILTERS_SHARP: [[i32; 8]; 16] = [
125+ [0, 0, 0, 128, 0, 0, 0, 0],
126+ [-2, 2, -6, 126, 8, -2, 2, 0],
127+ [-2, 6, -12, 124, 16, -6, 4, -2],
128+ [-2, 8, -18, 120, 26, -10, 6, -2],
129+ [-4, 10, -22, 116, 38, -14, 6, -2],
130+ [-4, 10, -22, 108, 48, -18, 8, -2],
131+ [-4, 10, -24, 100, 60, -20, 8, -2],
132+ [-4, 10, -24, 90, 70, -22, 10, -2],
133+ [-4, 12, -24, 80, 80, -24, 12, -4],
134+ [-2, 10, -22, 70, 90, -24, 10, -4],
135+ [-2, 8, -20, 60, 100, -24, 10, -4],
136+ [-2, 8, -18, 48, 108, -22, 10, -4],
137+ [-2, 6, -14, 38, 116, -22, 10, -4],
138+ [-2, 6, -10, 26, 120, -18, 8, -2],
139+ [-2, 4, -6, 16, 124, -12, 6, -2],
140+ [0, 2, -2, 8, 126, -6, 2, -2],
141+];
142+
143+/// `Subpel_Filters[BILINEAR]` (`av1_bilinear_filters`) -- also never swapped
144+/// for a narrow-block table (it is already only 2 non-zero taps).
145+const SUBPEL_FILTERS_BILINEAR: [[i32; 8]; 16] = [
146+ [0, 0, 0, 128, 0, 0, 0, 0],
147+ [0, 0, 0, 120, 8, 0, 0, 0],
148+ [0, 0, 0, 112, 16, 0, 0, 0],
149+ [0, 0, 0, 104, 24, 0, 0, 0],
150+ [0, 0, 0, 96, 32, 0, 0, 0],
151+ [0, 0, 0, 88, 40, 0, 0, 0],
152+ [0, 0, 0, 80, 48, 0, 0, 0],
153+ [0, 0, 0, 72, 56, 0, 0, 0],
154+ [0, 0, 0, 64, 64, 0, 0, 0],
155+ [0, 0, 0, 56, 72, 0, 0, 0],
156+ [0, 0, 0, 48, 80, 0, 0, 0],
157+ [0, 0, 0, 40, 88, 0, 0, 0],
158+ [0, 0, 0, 32, 96, 0, 0, 0],
159+ [0, 0, 0, 24, 104, 0, 0, 0],
160+ [0, 0, 0, 16, 112, 0, 0, 0],
161+ [0, 0, 0, 8, 120, 0, 0, 0],
162+];
163+
164+/// Which of the spec's four `interpolation_filter` kernels a block's motion
165+/// compensation reads (spec 6.8.9 / `frame_header`'s `interpolation_filter`).
166+/// A frame codes exactly one of these when not `Switchable`; a `Switchable`
167+/// frame codes one per block per direction (spec 5.11.20's
168+/// `read_interp_filter`, see `decode::decode_inter_block`), so `predict`'s
169+/// callers resolve `Switchable` to a concrete pair of kinds themselves --
170+/// this type only ever names the resolved kernel, never `Switchable` itself.
171+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
172+pub enum InterpFilterKind {
173+ Regular,
174+ Smooth,
175+ Sharp,
176+ Bilinear,
177+}
178+
179+impl InterpFilterKind {
180+ fn tables(self) -> (&'static [[i32; 8]; 16], &'static [[i32; 8]; 16]) {
181+ match self {
182+ InterpFilterKind::Regular => (&SUBPEL_FILTERS, &SUBPEL_FILTERS_4),
183+ InterpFilterKind::Smooth => (&SUBPEL_FILTERS_SMOOTH, &SUBPEL_FILTERS_SMOOTH_4),
184+ InterpFilterKind::Sharp => (&SUBPEL_FILTERS_SHARP, &SUBPEL_FILTERS_SHARP),
185+ InterpFilterKind::Bilinear => (&SUBPEL_FILTERS_BILINEAR, &SUBPEL_FILTERS_BILINEAR),
186+ }
187+ }
188+
189+ /// The three-symbol alphabet spec 5.11.20's `interp_filter[dir]` reads
190+ /// under a `Switchable` frame decodes to (`EIGHTTAP`/`_SMOOTH`/`_SHARP`
191+ /// in that order -- `BILINEAR` is never a `switchable_interp` outcome).
192+ ///
193+ /// # Panics
194+ /// Panics when `symbol` is not `0..=2`.
195+ pub fn from_switchable_symbol(symbol: usize) -> InterpFilterKind {
196+ match symbol {
197+ 0 => InterpFilterKind::Regular,
198+ 1 => InterpFilterKind::Smooth,
199+ 2 => InterpFilterKind::Sharp,
200+ _ => panic!("switchable_interp's alphabet is exactly 3 symbols"),
201+ }
202+ }
203+
204+ /// The frame header's own `interpolation_filter` (spec 6.8.9), for the
205+ /// non-`Switchable` values a fixed-filter frame codes directly.
206+ ///
207+ /// # Panics
208+ /// Panics on `Switchable`, which is not a concrete kernel -- callers
209+ /// resolve it per block instead (spec 5.11.20).
210+ pub fn from_header(filter: ec_av1_syntax::InterpolationFilter) -> InterpFilterKind {
211+ match filter {
212+ ec_av1_syntax::InterpolationFilter::Eighttap => InterpFilterKind::Regular,
213+ ec_av1_syntax::InterpolationFilter::EighttapSmooth => InterpFilterKind::Smooth,
214+ ec_av1_syntax::InterpolationFilter::EighttapSharp => InterpFilterKind::Sharp,
215+ ec_av1_syntax::InterpolationFilter::Bilinear => InterpFilterKind::Bilinear,
216+ ec_av1_syntax::InterpolationFilter::Switchable => {
217+ panic!("Switchable is resolved per block, not as one frame-wide kernel")
218+ }
219+ }
220+ }
221+}
222+
82 /// A reference sample at `(x, y)`, clamped to the frame's true (unpadded)223 /// A reference sample at `(x, y)`, clamped to the frame's true (unpadded)
83 /// extent -- `true_width`/`true_height`, which can be narrower than224 /// extent -- `true_width`/`true_height`, which can be narrower than
84 /// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3`225 /// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3`
@@ -126,6 +267,69 @@ pub fn predict(
126 block_w: usize,267 block_w: usize,
127 block_h: usize,268 block_h: usize,
128 dst: &mut [u8],269 dst: &mut [u8],
270+) {
271+ predict_with_filter(
272+ reference,
273+ stride,
274+ true_width,
275+ true_height,
276+ x_q4,
277+ y_q4,
278+ block_w,
279+ block_h,
280+ InterpFilterKind::Regular,
281+ dst,
282+ );
283+}
284+
285+/// [`predict`], selecting the interpolation filter kernel explicitly (spec
286+/// 6.8.9's `interpolation_filter`) instead of always `Regular` -- the same
287+/// kernel both directions.
288+#[allow(clippy::too_many_arguments)]
289+pub fn predict_with_filter(
290+ reference: &[u8],
291+ stride: usize,
292+ true_width: usize,
293+ true_height: usize,
294+ x_q4: i32,
295+ y_q4: i32,
296+ block_w: usize,
297+ block_h: usize,
298+ filter_kind: InterpFilterKind,
299+ dst: &mut [u8],
300+) {
301+ predict_with_filters(
302+ reference,
303+ stride,
304+ true_width,
305+ true_height,
306+ x_q4,
307+ y_q4,
308+ block_w,
309+ block_h,
310+ filter_kind,
311+ filter_kind,
312+ dst,
313+ );
314+}
315+
316+/// [`predict_with_filter`], with the horizontal (`interp_filter[0]`) and
317+/// vertical (`interp_filter[1]`) kernels chosen independently -- spec
318+/// 5.11.20's per-block `SWITCHABLE` read is per-direction (`enable_dual_filter`),
319+/// so the two passes can genuinely differ.
320+#[allow(clippy::too_many_arguments)]
321+pub fn predict_with_filters(
322+ reference: &[u8],
323+ stride: usize,
324+ true_width: usize,
325+ true_height: usize,
326+ x_q4: i32,
327+ y_q4: i32,
328+ block_w: usize,
329+ block_h: usize,
330+ h_kind: InterpFilterKind,
331+ v_kind: InterpFilterKind,
332+ dst: &mut [u8],
129 ) {333 ) {
130 assert_eq!(dst.len(), block_w * block_h, "the destination is the block");334 assert_eq!(dst.len(), block_w * block_h, "the destination is the block");
131 assert!(!reference.is_empty(), "a reference plane has samples");335 assert!(!reference.is_empty(), "a reference plane has samples");
@@ -164,15 +368,17 @@ pub fn predict(
164 return;368 return;
165 }369 }
166 370
371+ let (h_wide, h_narrow) = h_kind.tables();
372+ let (v_wide, v_narrow) = v_kind.tables();
167 let h_filter = if block_w <= 4 {373 let h_filter = if block_w <= 4 {
168- &SUBPEL_FILTERS_4[xfrac]374+ &h_narrow[xfrac]
169 } else {375 } else {
170- &SUBPEL_FILTERS[xfrac]376+ &h_wide[xfrac]
171 };377 };
172 let v_filter = if block_h <= 4 {378 let v_filter = if block_h <= 4 {
173- &SUBPEL_FILTERS_4[yfrac]379+ &v_narrow[yfrac]
174 } else {380 } else {
175- &SUBPEL_FILTERS[yfrac]381+ &v_wide[yfrac]
176 };382 };
177 383
178 // The vertical pass reads 3 rows above and 4 below the block, so the384 // The vertical pass reads 3 rows above and 4 below the block, so the
@@ -207,7 +413,93 @@ pub fn predict(
207 413
208 #[cfg(test)]414 #[cfg(test)]
209 mod tests {415 mod tests {
210- use super::predict;416+ use super::{InterpFilterKind, predict, predict_with_filter};
417+
418+ /// Regression pin for the lane-av1flake ±1 defect: a real aomenc chroma
419+ /// inter block at the frame's top-left corner (mv q4 = (row 7, col 15),
420+ /// bw=bh=16) predicted the wrong value because this decoder always used
421+ /// the REGULAR filter table -- aomdec's own `av1_convolve_2d_sr_c` trace
422+ /// showed this block actually used `EIGHTTAP_SMOOTH`
423+ /// (`0 0 2 34 62 28 2 0` at xfrac=15, not REGULAR's `0 0 -2 8 126 -6 2
424+ /// 0`). Window and expected output dumped straight from an
425+ /// instrumented aomdec's `av1_convolve_2d_sr_c` on the pinned stream.
426+ #[test]
427+ fn smooth_filter_matches_aomdec_chroma_inter_block() {
428+ #[rustfmt::skip]
429+ let window: [[u8; 24]; 24] = [
430+ [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
431+ [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
432+ [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
433+ [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
434+ [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
435+ [114,114,114,114,114,114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116,115,115,115],
436+ [114,114,114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,116,116,115,115,115,115],
437+ [113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,116,116,116,116,115,115,115,115],
438+ [113,113,113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,115,115,115,115,115,115],
439+ [112,112,112,112,112,112,112,112,112,113,113,113,113,114,114,114,114,114,115,115,115,115,115,115],
440+ [111,111,111,111,111,111,111,111,112,112,112,112,113,113,113,113,114,114,114,114,115,115,115,115],
441+ [110,110,110,110,110,110,110,110,111,111,111,111,112,112,112,113,113,113,113,113,114,114,114,115],
442+ [109,109,109,109,109,109,109,110,110,110,110,111,111,111,111,112,112,112,112,113,113,114,114,114],
443+ [108,108,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111,111,111,112,112,113,113,113],
444+ [108,108,108,108,108,108,108,108,108,108,109,109,109,110,110,110,110,110,111,111,112,112,112,112],
445+ [107,107,107,107,107,107,107,107,107,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111],
446+ [106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,110,110,110],
447+ [106,106,106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,109],
448+ [105,105,105,105,105,105,105,105,106,106,106,107,107,107,107,108,108,108,108,108,108,108,108,109],
449+ [104,104,104,104,104,104,104,104,104,105,105,106,106,106,107,107,107,107,107,107,107,108,108,108],
450+ [102,102,102,102,102,103,103,103,103,104,104,105,105,105,106,106,107,107,107,107,107,107,107,107],
451+ [101,101,101,101,101,101,101,102,102,103,103,104,104,104,105,105,106,106,106,106,106,107,107,107],
452+ [100,100,100,100,100,100,100,101,101,102,102,103,103,104,104,105,105,105,106,106,106,106,106,106],
453+ [99,99,99,99,99,99,100,100,100,101,101,102,103,103,104,104,105,105,105,105,105,105,105,106],
454+ ];
455+ // The dumped window spans real cols/rows -4..19; the block itself
456+ // starts at real (0, 0), so pad the reference plane so col/row 4 of
457+ // this buffer is real col/row 0, and hand `predict_with_filter` the
458+ // real (0, 0) coordinates -- clamping then reproduces the identical
459+ // extended border the dump already captured.
460+ let stride = 24;
461+ let mut reference = vec![0u8; stride * 24];
462+ for (r, row) in window.iter().enumerate() {
463+ reference[r * stride..r * stride + stride].copy_from_slice(row);
464+ }
465+ // predict()'s own edge clamp starts at true (0,0); shift so the
466+ // dumped window's row/col 4 lands there by biasing x_q4/y_q4's
467+ // whole-pel part by -4 and reading from a plane whose true origin
468+ // is the window's row/col 4 -- simplest is to pass true_width /
469+ // true_height covering the whole dumped window and offset the
470+ // whole-pel part of x_q4/y_q4 by +4 to land on real (0,0).
471+ let x_q4 = (4 + 0) * 16 + 15; // real block x0=0, xfrac=15
472+ let y_q4 = (4 + 0) * 16 + 7; // real block y0=0, yfrac=7
473+ let mut dst = vec![0u8; 16 * 16];
474+ predict_with_filter(
475+ &reference,
476+ stride,
477+ 24,
478+ 24,
479+ x_q4,
480+ y_q4,
481+ 16,
482+ 16,
483+ InterpFilterKind::Smooth,
484+ &mut dst,
485+ );
486+ #[rustfmt::skip]
487+ let expected_row0: [u8; 16] =
488+ [114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116];
489+ assert_eq!(
490+ &dst[0..16],
491+ &expected_row0,
492+ "row0 vs aomdec's real av1_convolve_2d_sr_c output"
493+ );
494+ #[rustfmt::skip]
495+ let expected_row15: [u8; 16] =
496+ [103,103,104,104,104,105,105,106,106,106,107,107,107,107,107,107];
497+ assert_eq!(
498+ &dst[15 * 16..15 * 16 + 16],
499+ &expected_row15,
500+ "row15 vs aomdec"
501+ );
502+ }
211 503
212 #[test]504 #[test]
213 fn integer_mv_is_identity() {505 fn integer_mv_is_identity() {
crates/ec-av1/src/stream.rsmodified+10 -0
@@ -184,6 +184,13 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
184 let enable_filter_intra = parser184 let enable_filter_intra = parser
185 .sequence_header()185 .sequence_header()
186 .is_some_and(|seq| seq.enable_filter_intra);186 .is_some_and(|seq| seq.enable_filter_intra);
187+ let enable_dual_filter = parser
188+ .sequence_header()
189+ .is_some_and(|seq| seq.enable_dual_filter);
190+ let interp_fixed = match header.interpolation_filter {
191+ ec_av1_syntax::InterpolationFilter::Switchable => None,
192+ fixed => Some(crate::mc::InterpFilterKind::from_header(fixed)),
193+ };
187 194
188 // Spec 7.20 `load_cdfs`: a frame naming a `primary_ref_frame` resumes195 // Spec 7.20 `load_cdfs`: a frame naming a `primary_ref_frame` resumes
189 // from that reference slot's saved CDF state instead of the spec 8.4196 // 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<Vec<Picture>> {
237 &header.loop_filter,244 &header.loop_filter,
238 initial_cdfs,245 initial_cdfs,
239 header.allow_high_precision_mv,246 header.allow_high_precision_mv,
247+ interp_fixed,
248+ enable_dual_filter,
240 )?249 )?
241 };250 };
242 // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's251 // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's
@@ -1487,6 +1496,7 @@ mod tests {
1487 let obu = p.parse_obu(&stream[pos..]).expect("parse");1496 let obu = p.parse_obu(&stream[pos..]).expect("parse");
1488 pos += obu.total_size;1497 pos += obu.total_size;
1489 if let ObuKind::Frame(header, _) = obu.kind {1498 if let ObuKind::Frame(header, _) = obu.kind {
1499+ eprintln!("interpolation_filter: {:?}", header.interpolation_filter);
1490 eprintln!("cdef: {:?}", header.cdef);1500 eprintln!("cdef: {:?}", header.cdef);
1491 eprintln!("quantization: {:?}", header.quantization);1501 eprintln!("quantization: {:?}", header.quantization);
1492 eprintln!(1502 eprintln!(