Skip to content

Commit

Permalink
fn sgr_{5x5,3x5,mix}: Deduplicate w/ generics (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison authored Jul 25, 2023
2 parents 682ed4c + ff71f18 commit c207227
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 428 deletions.
211 changes: 211 additions & 0 deletions src/looprestoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,214 @@ pub(crate) unsafe extern "C" fn selfguided_filter<BD: BitDepth>(
}
};
}

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

unsafe fn sgr_5x5_rust<BD: BitDepth>(
mut p: *mut BD::Pixel,
stride: ptrdiff_t,
left: *const [BD::Pixel; 4],
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,
25 as libc::c_int,
(*params).sgr.s0,
bd,
);
let w0 = (*params).sgr.w0 as libc::c_int;
let mut j = 0;
while j < h {
let mut i = 0;
while i < w {
let v = w0 * dst[(j * 384 + i) as usize].as_::<libc::c_int>();
*p.offset(i as isize) = bd.iclip_pixel(
(*p.offset(i as isize)).as_::<libc::c_int>()
+ (v + ((1 as libc::c_int) << 10) >> 11),
);
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_erased<BD: BitDepth>(
p: *mut libc::c_void,
stride: ptrdiff_t,
left: *const libc::c_void,
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],
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) = bd.iclip_pixel(
(*p.offset(i as isize)).as_::<libc::c_int>()
+ (v + ((1 as libc::c_int) << 10) >> 11),
);
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_mix_c_erased<BD: BitDepth>(
p: *mut libc::c_void,
stride: ptrdiff_t,
left: *const libc::c_void,
lpf: *const libc::c_void,
w: libc::c_int,
h: libc::c_int,
params: *const LooprestorationParams,
edges: LrEdgeFlags,
bitdepth_max: libc::c_int,
) {
sgr_mix_rust(
p.cast(),
stride,
left.cast(),
lpf.cast(),
w,
h,
params,
edges,
BD::from_c(bitdepth_max),
)
}

unsafe fn sgr_mix_rust<BD: BitDepth>(
mut p: *mut BD::Pixel,
stride: ptrdiff_t,
left: *const [BD::Pixel; 4],
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 dst0: [BD::Coef; 24576] = [0.as_(); 24576];
let mut dst1: [BD::Coef; 24576] = [0.as_(); 24576];
padding::<BD>(&mut tmp, p, stride, left, lpf, w, h, edges);
selfguided_filter(
dst0.as_mut_ptr(),
tmp.as_mut_ptr(),
390 as libc::c_int as ptrdiff_t,
w,
h,
25 as libc::c_int,
(*params).sgr.s0,
bd,
);
selfguided_filter(
dst1.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 w0 = (*params).sgr.w0 as libc::c_int;
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 = w0 * dst0[(j * 384 + i) as usize].as_::<libc::c_int>()
+ w1 * dst1[(j * 384 + i) as usize].as_::<libc::c_int>();
*p.offset(i as isize) = bd.iclip_pixel(
(*p.offset(i as isize)).as_::<libc::c_int>()
+ (v + ((1 as libc::c_int) << 10) >> 11),
);
i += 1;
}
p = p.offset(BD::pxstride(stride as usize) as isize);
j += 1;
}
}
Loading

0 comments on commit c207227

Please sign in to comment.