Skip to content

Commit

Permalink
fn sgr_mix: Deduplicate w/ generics
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison committed Jul 24, 2023
1 parent 8e5428f commit 759a302
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 168 deletions.
82 changes: 82 additions & 0 deletions src/looprestoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,85 @@ unsafe fn sgr_3x3_rust<BD: BitDepth>(
j += 1;
}
}

// TODO(randomPoison): Temporarily pub until init logic is deduplicated.
pub(crate) unsafe extern "C" fn sgr_mix_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_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],
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 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) = 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);
j += 1;
}
}
96 changes: 10 additions & 86 deletions src/looprestoration_tmpl_16.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::include::common::bitdepth::BitDepth;
use crate::include::common::bitdepth::BitDepth16;
use crate::include::stddef::*;
use crate::include::stdint::*;
Expand Down Expand Up @@ -130,16 +129,19 @@ extern "C" {

pub type pixel = uint16_t;
pub type coef = int32_t;
use crate::src::looprestoration::LrEdgeFlags;
pub type const_left_pixel_row = *const [pixel; 4];
use crate::src::looprestoration::LooprestorationParams;

use crate::include::common::intops::iclip;
use crate::src::looprestoration::padding;
use crate::src::looprestoration::selfguided_filter;
#[cfg(feature = "asm")]
cfg_if! {
if #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] {
use crate::src::looprestoration::LrEdgeFlags;
use crate::src::looprestoration::LooprestorationParams;
}
}

use crate::src::looprestoration::wiener_c_erased;
use crate::src::looprestoration::Dav1dLoopRestorationDSPContext;
use crate::src::looprestoration::{sgr_3x3_c, sgr_5x5_c};
use crate::src::looprestoration::{sgr_3x3_c, sgr_5x5_c, sgr_mix_c};

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

unsafe extern "C" fn sgr_mix_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_mix_rust(
p.cast(),
stride,
left.cast(),
lpf.cast(),
w,
h,
params,
edges,
bitdepth_max,
)
}

unsafe extern "C" fn sgr_mix_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 dst0: [coef; 24576] = [0; 24576];
let mut dst1: [coef; 24576] = [0; 24576];
padding::<BitDepth16>(&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,
BitDepth16::from_c(bitdepth_max),
);
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,
BitDepth16::from_c(bitdepth_max),
);
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] + w1 * dst1[(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;
}
}

#[cfg(all(feature = "asm", any(target_arch = "x86", target_arch = "x86_64")))]
#[inline(always)]
unsafe extern "C" fn loop_restoration_dsp_init_x86(
Expand Down Expand Up @@ -751,7 +675,7 @@ pub unsafe extern "C" fn dav1d_loop_restoration_dsp_init_16bpc(
(*c).wiener[0] = (*c).wiener[1];
(*c).sgr[0] = sgr_5x5_c::<BitDepth16>;
(*c).sgr[1] = sgr_3x3_c::<BitDepth16>;
(*c).sgr[2] = sgr_mix_c;
(*c).sgr[2] = sgr_mix_c::<BitDepth16>;

#[cfg(feature = "asm")]
cfg_if! {
Expand Down
90 changes: 8 additions & 82 deletions src/looprestoration_tmpl_8.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::include::common::bitdepth::BitDepth;
use crate::include::common::bitdepth::BitDepth8;
use crate::include::stddef::*;
use crate::include::stdint::*;
#[cfg(all(feature = "asm", any(target_arch = "arm", target_arch = "aarch64"),))]
use crate::src::align::Align16;
Expand Down Expand Up @@ -127,89 +125,17 @@ extern "C" {

pub type pixel = uint8_t;
pub type coef = int16_t;
use crate::src::looprestoration::LrEdgeFlags;
pub type const_left_pixel_row = *const [pixel; 4];
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::LooprestorationParams;
use crate::src::looprestoration::{sgr_3x3_c, sgr_5x5_c};
use crate::src::looprestoration::{sgr_3x3_c, sgr_5x5_c, sgr_mix_c};

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

unsafe extern "C" fn sgr_mix_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_mix_rust(
p.cast(),
stride,
left.cast(),
lpf.cast(),
w,
h,
params,
edges,
)
}

unsafe extern "C" fn sgr_mix_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 dst0: [coef; 24576] = [0; 24576];
let mut dst1: [coef; 24576] = [0; 24576];
padding::<BitDepth8>(&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,
BitDepth8::new(()),
);
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,
BitDepth8::new(()),
);
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) = 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;
#[cfg(feature = "asm")]
cfg_if! {
if #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] {
use crate::include::stddef::*;
use crate::src::looprestoration::LrEdgeFlags;
use crate::src::looprestoration::LooprestorationParams;
}
}

Expand Down Expand Up @@ -710,7 +636,7 @@ pub unsafe extern "C" fn dav1d_loop_restoration_dsp_init_8bpc(
(*c).wiener[0] = (*c).wiener[1];
(*c).sgr[0] = sgr_5x5_c::<BitDepth8>;
(*c).sgr[1] = sgr_3x3_c::<BitDepth8>;
(*c).sgr[2] = sgr_mix_c;
(*c).sgr[2] = sgr_mix_c::<BitDepth8>;

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

0 comments on commit 759a302

Please sign in to comment.