Skip to content

Commit

Permalink
refactor filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Mar 5, 2024
1 parent ee5274a commit 3e65e49
Showing 1 changed file with 15 additions and 35 deletions.
50 changes: 15 additions & 35 deletions src/mc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,37 +125,14 @@ unsafe fn rav1d_filter_8tap_clip2<BD: BitDepth, T: Into<i32>>(
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)]
Expand All @@ -168,13 +145,15 @@ unsafe fn put_8tap_rust<BD: BitDepth>(
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);
Expand Down Expand Up @@ -268,7 +247,7 @@ unsafe fn put_8tap_scaled_rust<BD: BitDepth>(
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_::<i32>() as i16) << intermediate_bits,
Expand All @@ -283,7 +262,7 @@ unsafe fn put_8tap_scaled_rust<BD: BitDepth>(
}
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 {
Expand Down Expand Up @@ -312,11 +291,12 @@ unsafe fn prep_8tap_rust<BD: BitDepth>(
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 {
Expand Down Expand Up @@ -401,7 +381,7 @@ unsafe fn prep_8tap_scaled_rust<BD: BitDepth>(
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_::<i32>() as i16) << intermediate_bits,
Expand All @@ -417,7 +397,7 @@ unsafe fn prep_8tap_scaled_rust<BD: BitDepth>(

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),
Expand Down

0 comments on commit 3e65e49

Please sign in to comment.