Skip to content

Commit

Permalink
fn sgr_3x3: Deduplicate w/ generics
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison committed Jul 24, 2023
1 parent 96d6814 commit 8e5428f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 134 deletions.
75 changes: 73 additions & 2 deletions src/looprestoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,81 @@ unsafe fn sgr_5x5_rust<BD: BitDepth>(
while i < w {
let v = w0 * dst[(j * 384 + i) as usize].as_::<libc::c_int>();
*p.offset(i as isize) = iclip(
(*p.offset(i as isize)).as_::<libc::c_int>() + (v + ((1 as libc::c_int) << 10) >> 11),
(*p.offset(i as isize)).as_::<libc::c_int>()
+ (v + ((1 as libc::c_int) << 10) >> 11),
0 as libc::c_int,
bd.bitdepth_max().as_(),
).as_();
)
.as_();
i += 1;
}
p = p.offset(BD::pxstride(stride as usize) as isize);
j += 1;
}
}

// TODO(randomPoison): Temporarily pub until init logic is deduplicated.
pub(crate) unsafe extern "C" fn sgr_3x3_c<BD: BitDepth>(
mut p: *mut libc::c_void,
stride: ptrdiff_t,
left: *const libc::c_void,
mut lpf: *const libc::c_void,
w: libc::c_int,
h: libc::c_int,
params: *const LooprestorationParams,
edges: LrEdgeFlags,
bitdepth_max: libc::c_int,
) {
sgr_3x3_rust(
p.cast(),
stride,
left.cast(),
lpf.cast(),
w,
h,
params,
edges,
BD::from_c(bitdepth_max),
)
}

unsafe fn sgr_3x3_rust<BD: BitDepth>(
mut p: *mut BD::Pixel,
stride: ptrdiff_t,
left: *const [BD::Pixel; 4],
mut lpf: *const BD::Pixel,
w: libc::c_int,
h: libc::c_int,
params: *const LooprestorationParams,
edges: LrEdgeFlags,
bd: BD,
) {
let mut tmp: [BD::Pixel; 27300] = [0.as_(); 27300];
let mut dst: [BD::Coef; 24576] = [0.as_(); 24576];
padding::<BD>(&mut tmp, p, stride, left, lpf, w, h, edges);
selfguided_filter(
dst.as_mut_ptr(),
tmp.as_mut_ptr(),
390 as libc::c_int as ptrdiff_t,
w,
h,
9 as libc::c_int,
(*params).sgr.s1,
bd,
);
let w1 = (*params).sgr.w1 as libc::c_int;
let mut j = 0;
while j < h {
let mut i = 0;
while i < w {
let v = w1 * dst[(j * 384 + i) as usize].as_::<libc::c_int>();
*p.offset(i as isize) = iclip(
(*p.offset(i as isize)).as_::<libc::c_int>()
+ (v + ((1 as libc::c_int) << 10) >> 11),
0 as libc::c_int,
bd.bitdepth_max().as_(),
)
.as_();
i += 1;
}
p = p.offset(BD::pxstride(stride as usize) as isize);
Expand Down
70 changes: 2 additions & 68 deletions src/looprestoration_tmpl_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ use crate::src::looprestoration::padding;
use crate::src::looprestoration::selfguided_filter;
use crate::src::looprestoration::wiener_c_erased;
use crate::src::looprestoration::Dav1dLoopRestorationDSPContext;
use crate::src::looprestoration::sgr_5x5_c;
use crate::src::looprestoration::{sgr_3x3_c, sgr_5x5_c};

#[inline]
unsafe extern "C" fn PXSTRIDE(x: ptrdiff_t) -> ptrdiff_t {
Expand All @@ -149,72 +149,6 @@ unsafe extern "C" fn PXSTRIDE(x: ptrdiff_t) -> ptrdiff_t {
return x >> 1;
}

unsafe extern "C" fn sgr_3x3_c(
mut p: *mut libc::c_void,
stride: ptrdiff_t,
left: *const libc::c_void,
mut lpf: *const libc::c_void,
w: libc::c_int,
h: libc::c_int,
params: *const LooprestorationParams,
edges: LrEdgeFlags,
bitdepth_max: libc::c_int,
) {
sgr_3x3_rust(
p.cast(),
stride,
left.cast(),
lpf.cast(),
w,
h,
params,
edges,
bitdepth_max,
)
}

unsafe extern "C" fn sgr_3x3_rust(
mut p: *mut pixel,
stride: ptrdiff_t,
left: *const [pixel; 4],
mut lpf: *const pixel,
w: libc::c_int,
h: libc::c_int,
params: *const LooprestorationParams,
edges: LrEdgeFlags,
bitdepth_max: libc::c_int,
) {
let mut tmp: [pixel; 27300] = [0; 27300];
let mut dst: [coef; 24576] = [0; 24576];
padding::<BitDepth16>(&mut tmp, p, stride, left, lpf, w, h, edges);
selfguided_filter(
dst.as_mut_ptr(),
tmp.as_mut_ptr(),
390 as libc::c_int as ptrdiff_t,
w,
h,
9 as libc::c_int,
(*params).sgr.s1,
BitDepth16::from_c(bitdepth_max),
);
let w1 = (*params).sgr.w1 as libc::c_int;
let mut j = 0;
while j < h {
let mut i = 0;
while i < w {
let v = w1 * dst[(j * 384 + i) as usize];
*p.offset(i as isize) = iclip(
*p.offset(i as isize) as libc::c_int + (v + ((1 as libc::c_int) << 10) >> 11),
0 as libc::c_int,
bitdepth_max,
) as pixel;
i += 1;
}
p = p.offset(PXSTRIDE(stride) as isize);
j += 1;
}
}

unsafe extern "C" fn sgr_mix_c(
mut p: *mut libc::c_void,
stride: ptrdiff_t,
Expand Down Expand Up @@ -816,7 +750,7 @@ pub unsafe extern "C" fn dav1d_loop_restoration_dsp_init_16bpc(
(*c).wiener[1] = wiener_c_erased::<BitDepth16>;
(*c).wiener[0] = (*c).wiener[1];
(*c).sgr[0] = sgr_5x5_c::<BitDepth16>;
(*c).sgr[1] = sgr_3x3_c;
(*c).sgr[1] = sgr_3x3_c::<BitDepth16>;
(*c).sgr[2] = sgr_mix_c;

#[cfg(feature = "asm")]
Expand Down
66 changes: 2 additions & 64 deletions src/looprestoration_tmpl_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,72 +134,10 @@ use crate::src::looprestoration::selfguided_filter;
use crate::src::looprestoration::wiener_c_erased;
use crate::src::looprestoration::Dav1dLoopRestorationDSPContext;
use crate::src::looprestoration::LooprestorationParams;
use crate::src::looprestoration::sgr_5x5_c;
use crate::src::looprestoration::{sgr_3x3_c, sgr_5x5_c};

use crate::include::common::intops::iclip_u8;

unsafe extern "C" fn sgr_3x3_c(
mut p: *mut libc::c_void,
stride: ptrdiff_t,
left: *const libc::c_void,
mut lpf: *const libc::c_void,
w: libc::c_int,
h: libc::c_int,
params: *const LooprestorationParams,
edges: LrEdgeFlags,
_bitdepth_max: libc::c_int,
) {
sgr_3x3_rust(
p.cast(),
stride,
left.cast(),
lpf.cast(),
w,
h,
params,
edges,
)
}

unsafe extern "C" fn sgr_3x3_rust(
mut p: *mut pixel,
stride: ptrdiff_t,
left: *const [pixel; 4],
mut lpf: *const pixel,
w: libc::c_int,
h: libc::c_int,
params: *const LooprestorationParams,
edges: LrEdgeFlags,
) {
let mut tmp: [pixel; 27300] = [0; 27300];
let mut dst: [coef; 24576] = [0; 24576];
padding::<BitDepth8>(&mut tmp, p, stride, left, lpf, w, h, edges);
selfguided_filter(
dst.as_mut_ptr(),
tmp.as_mut_ptr(),
390 as libc::c_int as ptrdiff_t,
w,
h,
9 as libc::c_int,
(*params).sgr.s1,
BitDepth8::new(()),
);
let w1 = (*params).sgr.w1 as libc::c_int;
let mut j = 0;
while j < h {
let mut i = 0;
while i < w {
let v = w1 * dst[(j * 384 + i) as usize] as libc::c_int;
*p.offset(i as isize) = iclip_u8(
*p.offset(i as isize) as libc::c_int + (v + ((1 as libc::c_int) << 10) >> 11),
) as pixel;
i += 1;
}
p = p.offset(stride as isize);
j += 1;
}
}

unsafe extern "C" fn sgr_mix_c(
mut p: *mut libc::c_void,
stride: ptrdiff_t,
Expand Down Expand Up @@ -771,7 +709,7 @@ pub unsafe extern "C" fn dav1d_loop_restoration_dsp_init_8bpc(
(*c).wiener[1] = wiener_c_erased::<BitDepth8>;
(*c).wiener[0] = (*c).wiener[1];
(*c).sgr[0] = sgr_5x5_c::<BitDepth8>;
(*c).sgr[1] = sgr_3x3_c;
(*c).sgr[1] = sgr_3x3_c::<BitDepth8>;
(*c).sgr[2] = sgr_mix_c;

#[cfg(feature = "asm")]
Expand Down

0 comments on commit 8e5428f

Please sign in to comment.