From 3e65e491ea6b8acc8317c5e550fabef532d7fe01 Mon Sep 17 00:00:00 2001 From: Folkert Date: Tue, 5 Mar 2024 18:09:01 +0100 Subject: [PATCH] refactor filter logic --- src/mc.rs | 50 +++++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/src/mc.rs b/src/mc.rs index dbaf18f95..e72b35dc9 100644 --- a/src/mc.rs +++ b/src/mc.rs @@ -125,37 +125,14 @@ unsafe fn rav1d_filter_8tap_clip2>( bd.iclip_pixel(rav1d_filter_8tap_rnd2(src, x, f, stride, rnd, sh)) } -fn get_h_filter(mx: usize, w: usize, filter_type: Rav1dFilterMode) -> Option<&'static [i8; 8]> { - let mx = mx.checked_sub(1)?; - let i = if w > 4 { +fn get_filter(m: usize, d: usize, filter_type: Rav1dFilterMode) -> Option<&'static [i8; 8]> { + let m = m.checked_sub(1)?; + let i = if d > 4 { filter_type as u8 } else { 3 + (filter_type as u8 & 1) }; - Some(&dav1d_mc_subpel_filters[i as usize][mx]) -} - -fn get_v_filter(my: usize, h: usize, filter_type: Rav1dFilterMode) -> Option<&'static [i8; 8]> { - let my = my.checked_sub(1)?; - let i = if h > 4 { - filter_type as u8 - } else { - 3 + (filter_type as u8 & 1) - }; - Some(&dav1d_mc_subpel_filters[i as usize][my]) -} - -fn get_filters( - mx: usize, - my: usize, - w: usize, - h: usize, - (h_filter_type, v_filter_type): (Rav1dFilterMode, Rav1dFilterMode), -) -> (Option<&'static [i8; 8]>, Option<&'static [i8; 8]>) { - ( - get_h_filter(mx, w, h_filter_type), - get_v_filter(my, h, v_filter_type), - ) + Some(&dav1d_mc_subpel_filters[i as usize][m]) } #[inline(never)] @@ -168,13 +145,15 @@ unsafe fn put_8tap_rust( h: usize, mx: usize, my: usize, - filter_type: (Rav1dFilterMode, Rav1dFilterMode), + (h_filter_type, v_filter_type): (Rav1dFilterMode, Rav1dFilterMode), bd: BD, ) { let intermediate_bits = bd.get_intermediate_bits(); let intermediate_rnd = 32 + (1 << 6 - intermediate_bits >> 1); - let (fh, fv) = get_filters(mx, my, w, h, filter_type); + let fh = get_filter(mx, w, h_filter_type); + let fv = get_filter(my, h, v_filter_type); + let [dst_stride, src_stride] = [dst_stride, src_stride].map(BD::pxstride); let mut dst = std::slice::from_raw_parts_mut(dst, dst_stride * h); @@ -268,7 +247,7 @@ unsafe fn put_8tap_scaled_rust( let mut ioff = 0; for x in 0..w { - let fh = get_h_filter(imx >> 6, w, h_filter_type); + let fh = get_filter(imx >> 6, w, h_filter_type); mid_ptr[x] = match fh { Some(fh) => rav1d_filter_8tap_rnd(src, ioff, fh, 1, 6 - intermediate_bits) as i16, None => ((*src.offset(ioff as isize)).as_::() as i16) << intermediate_bits, @@ -283,7 +262,7 @@ unsafe fn put_8tap_scaled_rust( } mid_ptr = &mut mid[128 * 3..]; for _ in 0..h { - let fv = get_v_filter(my >> 6, h, v_filter_type); + let fv = get_filter(my >> 6, h, v_filter_type); for x in 0..w { dst[x] = match fv { @@ -312,11 +291,12 @@ unsafe fn prep_8tap_rust( h: usize, mx: usize, my: usize, - filter_type: (Rav1dFilterMode, Rav1dFilterMode), + (h_filter_type, v_filter_type): (Rav1dFilterMode, Rav1dFilterMode), bd: BD, ) { let intermediate_bits = bd.get_intermediate_bits(); - let (fh, fv) = get_filters(mx, my, w, h, filter_type); + let fh = get_filter(mx, w, h_filter_type); + let fv = get_filter(my, h, v_filter_type); let src_stride = BD::pxstride(src_stride); if let Some(fh) = fh { @@ -401,7 +381,7 @@ unsafe fn prep_8tap_scaled_rust( let mut imx = mx; let mut ioff = 0; for x in 0..w { - let fh = get_h_filter(imx >> 6, w, h_filter_type); + let fh = get_filter(imx >> 6, w, h_filter_type); mid_ptr[x] = match fh { Some(fh) => rav1d_filter_8tap_rnd(src, ioff, fh, 1, 6 - intermediate_bits) as i16, None => ((*src.offset(ioff as isize)).as_::() as i16) << intermediate_bits, @@ -417,7 +397,7 @@ unsafe fn prep_8tap_scaled_rust( mid_ptr = &mut mid[128 * 3..]; for _ in 0..h { - let fv = get_v_filter(my >> 6, h, v_filter_type); + let fv = get_filter(my >> 6, h, v_filter_type); for x in 0..w { *tmp.offset(x as isize) = ((match fv { Some(fv) => rav1d_filter_8tap_rnd(mid_ptr.as_ptr(), x, fv, 128, 6),