Skip to content

Commit

Permalink
fn selfguided_filter: Deduplicate w/ generics (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison authored Jul 25, 2023
2 parents ee3e717 + a5696f9 commit 682ed4c
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 353 deletions.
194 changes: 190 additions & 4 deletions src/looprestoration.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::include::common::bitdepth::AsPrimitive;
use crate::include::common::bitdepth::BitDepth;
use crate::include::common::intops::iclip;
use crate::include::common::intops::imax;
use crate::include::common::intops::umin;
use crate::include::stddef::ptrdiff_t;
use crate::include::stdint::int16_t;
use crate::include::stdint::int32_t;
use crate::include::stdint::uint16_t;
use crate::include::stdint::uint32_t;
use crate::src::align::Align16;
use crate::src::tables::dav1d_sgr_x_by_x;

pub type LrEdgeFlags = libc::c_uint;
pub const LR_HAVE_BOTTOM: LrEdgeFlags = 8;
Expand Down Expand Up @@ -401,8 +404,7 @@ unsafe fn wiener_rust<BD: BitDepth>(
}
}

// TODO(randomPoison): Temporarily pub until callers are deduplicated.
pub(crate) unsafe fn boxsum3<BD: BitDepth>(
unsafe fn boxsum3<BD: BitDepth>(
mut sumsq: *mut int32_t,
mut sum: *mut BD::Coef,
mut src: *const BD::Pixel,
Expand Down Expand Up @@ -462,8 +464,7 @@ pub(crate) unsafe fn boxsum3<BD: BitDepth>(
}
}

// TODO(randomPoison): Temporarily pub until callers are deduplicated.
pub(crate) unsafe fn boxsum5<BD: BitDepth>(
unsafe fn boxsum5<BD: BitDepth>(
mut sumsq: *mut int32_t,
mut sum: *mut BD::Coef,
src: *const BD::Pixel,
Expand Down Expand Up @@ -537,3 +538,188 @@ pub(crate) unsafe fn boxsum5<BD: BitDepth>(
y_0 += 1;
}
}

// TODO(randomPoison): Temporarily pub until callers are deduplicated.
#[inline(never)]
pub(crate) unsafe extern "C" fn selfguided_filter<BD: BitDepth>(
mut dst: *mut BD::Coef,
mut src: *const BD::Pixel,
_src_stride: ptrdiff_t,
w: libc::c_int,
h: libc::c_int,
n: libc::c_int,
s: libc::c_uint,
bd: BD,
) {
let sgr_one_by_x: libc::c_uint = (if n == 25 {
164 as libc::c_int
} else {
455 as libc::c_int
}) as libc::c_uint;
let mut sumsq: [int32_t; 26520] = [0; 26520];
let mut A: *mut int32_t = sumsq.as_mut_ptr().offset((2 * 390) as isize).offset(3);
let mut sum: [BD::Coef; 26520] = [0.as_(); 26520];
let mut B: *mut BD::Coef = sum.as_mut_ptr().offset((2 * 390) as isize).offset(3);
let step = (n == 25) as libc::c_int + 1;
if n == 25 {
boxsum5::<BD>(sumsq.as_mut_ptr(), sum.as_mut_ptr(), src, w + 6, h + 6);
} else {
boxsum3::<BD>(sumsq.as_mut_ptr(), sum.as_mut_ptr(), src, w + 6, h + 6);
}
let bitdepth_min_8 = bd.bitdepth() - 8;
let mut AA: *mut int32_t = A.offset(-(390 as libc::c_int as isize));
let mut BB: *mut BD::Coef = B.offset(-(390 as libc::c_int as isize));
let mut j = -(1 as libc::c_int);
while j < h + 1 {
let mut i = -(1 as libc::c_int);
while i < w + 1 {
let a = *AA.offset(i as isize) + ((1 as libc::c_int) << 2 * bitdepth_min_8 >> 1)
>> 2 * bitdepth_min_8;
let b = (*BB.offset(i as isize)).as_::<libc::c_int>()
+ ((1 as libc::c_int) << bitdepth_min_8 >> 1)
>> bitdepth_min_8;
let p: libc::c_uint = imax(a * n - b * b, 0 as libc::c_int) as libc::c_uint;
let z: libc::c_uint = p
.wrapping_mul(s)
.wrapping_add(((1 as libc::c_int) << 19) as libc::c_uint)
>> 20;
let x: libc::c_uint = dav1d_sgr_x_by_x
[umin(z, 255 as libc::c_int as libc::c_uint) as usize]
as libc::c_uint;
*AA.offset(i as isize) = (x
.wrapping_mul((*BB.offset(i as isize)).as_::<libc::c_uint>())
.wrapping_mul(sgr_one_by_x)
.wrapping_add(((1 as libc::c_int) << 11) as libc::c_uint)
>> 12) as int32_t;
*BB.offset(i as isize) = x.as_::<BD::Coef>();
i += 1;
}
AA = AA.offset((step * 390) as isize);
BB = BB.offset((step * 390) as isize);
j += step;
}
src = src.offset((3 * 390 + 3) as isize);
if n == 25 {
let mut j_0 = 0;
while j_0 < h - 1 {
let mut i_0 = 0;
while i_0 < w {
let a_0: libc::c_int = (*B.offset((i_0 - 390) as isize)
+ (*B.offset((i_0 + 390) as isize)))
.as_::<libc::c_int>()
* 6
+ (*B.offset((i_0 - 1 - 390) as isize)
+ *B.offset((i_0 - 1 + 390) as isize)
+ *B.offset((i_0 + 1 - 390) as isize)
+ *B.offset((i_0 + 1 + 390) as isize))
.as_::<libc::c_int>()
* 5;
let b_0 = (*A.offset((i_0 - 390) as isize) + *A.offset((i_0 + 390) as isize)) * 6
+ (*A.offset((i_0 - 1 - 390) as isize)
+ *A.offset((i_0 - 1 + 390) as isize)
+ *A.offset((i_0 + 1 - 390) as isize)
+ *A.offset((i_0 + 1 + 390) as isize))
* 5;
*dst.offset(i_0 as isize) = (b_0
- a_0 * (*src.offset(i_0 as isize)).as_::<libc::c_int>()
+ ((1 as libc::c_int) << 8)
>> 9)
.as_();
i_0 += 1;
}
dst = dst.offset(384);
src = src.offset(390);
B = B.offset(390);
A = A.offset(390);
let mut i_1 = 0;
while i_1 < w {
let a_1: libc::c_int = (*B.offset(i_1 as isize)).as_::<libc::c_int>() * 6
+ (*B.offset((i_1 - 1) as isize) + *B.offset((i_1 + 1) as isize))
.as_::<libc::c_int>()
* 5;
let b_1 = *A.offset(i_1 as isize) * 6
+ (*A.offset((i_1 - 1) as isize) + *A.offset((i_1 + 1) as isize)) * 5;
*dst.offset(i_1 as isize) = (b_1
- a_1 * (*src.offset(i_1 as isize)).as_::<libc::c_int>()
+ ((1 as libc::c_int) << 7)
>> 8)
.as_();
i_1 += 1;
}
dst = dst.offset(384);
src = src.offset(390);
B = B.offset(390);
A = A.offset(390);
j_0 += 2 as libc::c_int;
}
if j_0 + 1 == h {
let mut i_2 = 0;
while i_2 < w {
let a_2: libc::c_int = (*B.offset((i_2 - 390) as isize)
+ *B.offset((i_2 + 390) as isize))
.as_::<libc::c_int>()
* 6
+ (*B.offset((i_2 - 1 - 390) as isize)
+ *B.offset((i_2 - 1 + 390) as isize)
+ *B.offset((i_2 + 1 - 390) as isize)
+ *B.offset((i_2 + 1 + 390) as isize))
.as_::<libc::c_int>()
* 5;
let b_2 = (*A.offset((i_2 - 390) as isize) + *A.offset((i_2 + 390) as isize)) * 6
+ (*A.offset((i_2 - 1 - 390) as isize)
+ *A.offset((i_2 - 1 + 390) as isize)
+ *A.offset((i_2 + 1 - 390) as isize)
+ *A.offset((i_2 + 1 + 390) as isize))
* 5;
*dst.offset(i_2 as isize) = (b_2
- a_2 * (*src.offset(i_2 as isize)).as_::<libc::c_int>()
+ ((1 as libc::c_int) << 8)
>> 9)
.as_();
i_2 += 1;
}
}
} else {
let mut j_1 = 0;
while j_1 < h {
let mut i_3 = 0;
while i_3 < w {
let a_3: libc::c_int = (*B.offset(i_3 as isize)
+ *B.offset((i_3 - 1) as isize)
+ *B.offset((i_3 + 1) as isize)
+ *B.offset((i_3 - 390) as isize)
+ *B.offset((i_3 + 390) as isize))
.as_::<libc::c_int>()
* 4
+ (*B.offset((i_3 - 1 - 390) as isize)
+ *B.offset((i_3 - 1 + 390) as isize)
+ *B.offset((i_3 + 1 - 390) as isize)
+ *B.offset((i_3 + 1 + 390) as isize))
.as_::<libc::c_int>()
* 3;
let b_3 = (*A.offset(i_3 as isize)
+ *A.offset((i_3 - 1) as isize)
+ *A.offset((i_3 + 1) as isize)
+ *A.offset((i_3 - 390) as isize)
+ *A.offset((i_3 + 390) as isize))
* 4
+ (*A.offset((i_3 - 1 - 390) as isize)
+ *A.offset((i_3 - 1 + 390) as isize)
+ *A.offset((i_3 + 1 - 390) as isize)
+ *A.offset((i_3 + 1 + 390) as isize))
* 3;
*dst.offset(i_3 as isize) = (b_3
- a_3 * (*src.offset(i_3 as isize)).as_::<libc::c_int>()
+ ((1 as libc::c_int) << 8)
>> 9)
.as_();
i_3 += 1;
}
dst = dst.offset(384);
src = src.offset(390);
B = B.offset(390);
A = A.offset(390);
j_1 += 1;
}
};
}
Loading

0 comments on commit 682ed4c

Please sign in to comment.