| 1 |
///! Optional module containing `portable_simd` versions of the most |
| 2 |
///! important unfiltering algorithms. Enable using the `unstable` feature. |
| 3 |
use core::convert::TryInto; |
| 4 |
use core::simd::prelude::*; |
| 5 |
use core::simd::Select; |
| 6 |
// Import the fastest arch-specific scalar implementations from the outer crate. |
| 7 |
#[cfg(not(target_arch = "x86_64"))] |
| 8 |
use super::paeth::filter_paeth as filter_paeth_chosen; |
| 9 |
#[cfg(target_arch = "x86_64")] |
| 10 |
use super::paeth::filter_paeth_stbi as filter_paeth_chosen; |
| 11 |
|
| 12 |
/// Paeth predictor specialized for AArch64 systems. Ported from the libpng |
| 13 |
/// implementation at |
| 14 |
/// https://github.com/pnggroup/libpng/blob/master/arm/filter_neon_intrinsics.c |
| 15 |
#[cfg(target_arch = "aarch64")] |
| 16 |
#[inline(always)] |
| 17 |
fn paeth_predictor_simd<const SIZE: usize>( |
| 18 |
a_i16: Simd<i16, SIZE>, |
| 19 |
b_i16: Simd<i16, SIZE>, |
| 20 |
c_i16: Simd<i16, SIZE>, |
| 21 |
) -> Simd<u8, SIZE> { |
| 22 |
let pa = (b_i16 - c_i16).abs(); |
| 23 |
let pb = (a_i16 - c_i16).abs(); |
| 24 |
let pc = (a_i16 + b_i16 - c_i16 - c_i16).abs(); |
| 25 |
|
| 26 |
let mut nearest = a_i16; |
| 27 |
let mut min_dist = pa; |
| 28 |
|
| 29 |
// Tie-breaking: left, then above, then upper-left. |
| 30 |
let pb_lt_min = pb.simd_lt(min_dist); |
| 31 |
nearest = pb_lt_min.select(b_i16, nearest); |
| 32 |
min_dist = pb_lt_min.select(pb, min_dist); |
| 33 |
|
| 34 |
let pc_lt_min = pc.simd_lt(min_dist); |
| 35 |
nearest = pc_lt_min.select(c_i16, nearest); |
| 36 |
|
| 37 |
nearest.cast::<u8>() |
| 38 |
} |
| 39 |
|
| 40 |
/// Paeth predictor based on the `filter_paeth_stbi` formulation, which |
| 41 |
/// performs better on x86_64 systems. |
| 42 |
#[cfg(not(target_arch = "aarch64"))] |
| 43 |
#[inline(always)] |
| 44 |
fn paeth_predictor_simd<const SIZE: usize>( |
| 45 |
a_i16: Simd<i16, SIZE>, |
| 46 |
b_i16: Simd<i16, SIZE>, |
| 47 |
c_i16: Simd<i16, SIZE>, |
| 48 |
) -> Simd<u8, SIZE> { |
| 49 |
let thresh = c_i16 * Simd::splat(3) - (a_i16 + b_i16); |
| 50 |
let lo = a_i16.simd_min(b_i16); |
| 51 |
let hi = a_i16.simd_max(b_i16); |
| 52 |
let t0 = hi.simd_le(thresh).select(lo, c_i16); |
| 53 |
thresh.simd_le(lo).select(hi, t0).cast::<u8>() |
| 54 |
} |
| 55 |
|
| 56 |
/// Core kernel for 3bpp Paeth unfiltering. Takes a `b_vec` consisting of |
| 57 |
/// 16 RGB above the starting offset, a `c_vec_initial` which is the |
| 58 |
/// previous pixel from before the starting offset, and a `current_a` |
| 59 |
/// which is the previous pixel to the left of `x_out`, which is the 16 |
| 60 |
/// pixels we're unfiltering. Returns the next `a` pixel and also updates |
| 61 |
/// `x_out`. |
| 62 |
#[inline(always)] |
| 63 |
fn process_paeth_chunk_bpp3_s48( |
| 64 |
// Previous `a` pixel from the last pump, or zero if this is the first. |
| 65 |
mut current_a: Simd<u8, 3>, |
| 66 |
// 48 bytes from the row above. |
| 67 |
b_vec: &Simd<u8, 48>, |
| 68 |
// The last three bytes of the previous `b_vec`, or zero if this if the first. |
| 69 |
c_vec_initial: Simd<u8, 3>, |
| 70 |
// 48 bytes from the current row (filtered) also re-used as the output. |
| 71 |
x_out: &mut Simd<u8, 48>, |
| 72 |
) -> Simd<u8, 3> { |
| 73 |
let x_in = *x_out; |
| 74 |
|
| 75 |
// Store the outputs of the caluation as we go along, then we can do a |
| 76 |
// a wide vectorized add at the end of the loop. |
| 77 |
let mut preds = [0u8; 48]; |
| 78 |
|
| 79 |
// Shift b and sift in the lowest 3 elements from the previous pump |
| 80 |
// to form c. |
| 81 |
let mut c_vec = b_vec.shift_elements_right::<3>(0u8); |
| 82 |
c_vec.as_mut_array()[0..3].copy_from_slice(c_vec_initial.as_array()); |
| 83 |
|
| 84 |
// For each RGB pixel in the 48-byte window, we a) extract the relevant |
| 85 |
// parts of a, b, c and input x, then b) apply the paeth predictor to |
| 86 |
// what's extracted, then c) merge them to form the 'a' pixel for the |
| 87 |
// next part of the calculation, and update the predictpr buffer. |
| 88 |
macro_rules! process_pixel { |
| 89 |
($shift:expr) => { |
| 90 |
let a_i16 = current_a.cast::<i16>(); |
| 91 |
let b_i16 = b_vec.extract::<$shift, 3>().cast::<i16>(); |
| 92 |
let c_i16 = c_vec.extract::<$shift, 3>().cast::<i16>(); |
| 93 |
let pred = paeth_predictor_simd(a_i16, b_i16, c_i16); |
| 94 |
current_a = x_in.extract::<$shift, 3>() + pred; |
| 95 |
// This is necessary to break the data dependency between the |
| 96 |
// output and the next pixel as much as possible. |
| 97 |
preds[$shift..$shift + 3].copy_from_slice(pred.as_array()); |
| 98 |
}; |
| 99 |
} |
| 100 |
|
| 101 |
process_pixel!(0); |
| 102 |
process_pixel!(3); |
| 103 |
process_pixel!(6); |
| 104 |
process_pixel!(9); |
| 105 |
process_pixel!(12); |
| 106 |
process_pixel!(15); |
| 107 |
process_pixel!(18); |
| 108 |
process_pixel!(21); |
| 109 |
process_pixel!(24); |
| 110 |
process_pixel!(27); |
| 111 |
process_pixel!(30); |
| 112 |
process_pixel!(33); |
| 113 |
process_pixel!(36); |
| 114 |
process_pixel!(39); |
| 115 |
process_pixel!(42); |
| 116 |
process_pixel!(45); |
| 117 |
|
| 118 |
// Commit the unfiltered result and return the next 'a' pixel. |
| 119 |
*x_out += Simd::from_array(preds); |
| 120 |
current_a |
| 121 |
} |
| 122 |
|
| 123 |
/// Applies Paeth unfiltering on the `current` pixel row using `prev` row, |
| 124 |
/// interpreting the input data as RGB. |
| 125 |
pub fn paeth_unfilter_3bpp(current: &mut [u8], prev: &[u8]) { |
| 126 |
const BPP: usize = 3; |
| 127 |
const STRIDE_BYTES: usize = 48; // 16 pixels * 3 bytes/pixel. |
| 128 |
|
| 129 |
// Use the standard convention of [c] [b] |
| 130 |
// [a] [x] |
| 131 |
// We load 48 bytes of each and use a sliding window approach to minimize loads/stores. |
| 132 |
// Whilst we cannot break the strict data dependency on [a], we can agressively unroll |
| 133 |
// the calculation and allow independent computation of the `pa` prediction variables. |
| 134 |
// Initially set these to zero. |
| 135 |
let mut a: Simd<u8, BPP> = Default::default(); // Left pixel (unfiltered) |
| 136 |
let mut c: Simd<u8, BPP> = Default::default(); // Upper-left pixel (unfiltered) |
| 137 |
|
| 138 |
// Decide the number of chunks and setup iterators for the SIMD body and scalar fallback. |
| 139 |
let mut current_iter = current.chunks_exact_mut(STRIDE_BYTES); |
| 140 |
let mut previous_iter = prev.chunks_exact(STRIDE_BYTES); |
| 141 |
let combined_iter = (&mut current_iter).zip(&mut previous_iter); |
| 142 |
|
| 143 |
for (chunk, prev_chunk) in combined_iter { |
| 144 |
let mut x: Simd<u8, STRIDE_BYTES> = Simd::<u8, STRIDE_BYTES>::from_slice(chunk); |
| 145 |
let b: Simd<u8, STRIDE_BYTES> = Simd::<u8, STRIDE_BYTES>::from_slice(prev_chunk); |
| 146 |
|
| 147 |
// Process the chunk using the SIMD helper, passing the initial `c`. |
| 148 |
a = process_paeth_chunk_bpp3_s48(a, &b, c, &mut x); |
| 149 |
|
| 150 |
// Update `c` for the next chunk: it's the upper-left of the next chunk, |
| 151 |
// which corresponds to the upper-right of the current chunk's `b` vector. |
| 152 |
c = b.extract::<{ STRIDE_BYTES - BPP }, BPP>(); |
| 153 |
|
| 154 |
// `TryInto` and `copy_to_slice` have similar performance here. |
| 155 |
x.copy_to_slice(chunk); |
| 156 |
} |
| 157 |
|
| 158 |
// Scalar remainder. |
| 159 |
let mut a_bpp = a.to_array(); |
| 160 |
let mut c_bpp = c.to_array(); |
| 161 |
for (chunk, b_bpp) in current_iter |
| 162 |
.into_remainder() |
| 163 |
.chunks_exact_mut(BPP) |
| 164 |
.zip(previous_iter.remainder().chunks_exact(BPP)) |
| 165 |
{ |
| 166 |
let new_chunk = [ |
| 167 |
chunk[0].wrapping_add(filter_paeth_chosen( |
| 168 |
a_bpp[0].into(), |
| 169 |
b_bpp[0].into(), |
| 170 |
c_bpp[0].into(), |
| 171 |
)), |
| 172 |
chunk[1].wrapping_add(filter_paeth_chosen( |
| 173 |
a_bpp[1].into(), |
| 174 |
b_bpp[1].into(), |
| 175 |
c_bpp[1].into(), |
| 176 |
)), |
| 177 |
chunk[2].wrapping_add(filter_paeth_chosen( |
| 178 |
a_bpp[2].into(), |
| 179 |
b_bpp[2].into(), |
| 180 |
c_bpp[2].into(), |
| 181 |
)), |
| 182 |
]; |
| 183 |
*TryInto::<&mut [u8; BPP]>::try_into(chunk).unwrap() = new_chunk; |
| 184 |
a_bpp = new_chunk; |
| 185 |
c_bpp = b_bpp.try_into().unwrap(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/// Core kernel for 4bpp Paeth unfiltering. Takes a `b_vec` consisting of |
| 190 |
/// 16 RGB above the starting offset, a `c_vec_initial` which is the |
| 191 |
/// previous pixel from before the starting offset, and a `current_a` |
| 192 |
/// which is the previous pixel to the left of `x_out`, which is the 16 |
| 193 |
/// pixels we're unfiltering. Returns the next `a` pixel and also updates |
| 194 |
/// `x_out`. |
| 195 |
#[inline(always)] |
| 196 |
fn process_paeth_chunk_bpp4_s64( |
| 197 |
mut current_a: Simd<u8, 4>, |
| 198 |
b_vec: &Simd<u8, 64>, |
| 199 |
c_vec_initial: Simd<u8, 4>, |
| 200 |
x_out: &mut Simd<u8, 64>, |
| 201 |
) -> Simd<u8, 4> { |
| 202 |
let x_in = *x_out; |
| 203 |
let mut preds = [0u8; 64]; |
| 204 |
|
| 205 |
// Mix and shift the previous pixel into b to form c |
| 206 |
let mut c_vec = b_vec.shift_elements_right::<4>(0u8); |
| 207 |
c_vec.as_mut_array()[0..4].copy_from_slice(c_vec_initial.as_array()); |
| 208 |
|
| 209 |
// For each RGB pixel in the 48-byte window, we a) extract the relevant |
| 210 |
// parts of a, b, c and input x, then b) apply the paeth predictor to |
| 211 |
// what's extracted, then c) merge them to form the 'a' pixel for the |
| 212 |
// next part of the calculation, and update the predictor buffer. |
| 213 |
macro_rules! process_pixel { |
| 214 |
($shift:expr) => { |
| 215 |
let a_i16 = current_a.cast::<i16>(); |
| 216 |
let b_i16 = b_vec.extract::<$shift, 4>().cast::<i16>(); |
| 217 |
let c_i16 = c_vec.extract::<$shift, 4>().cast::<i16>(); |
| 218 |
let pred = paeth_predictor_simd(a_i16, b_i16, c_i16); |
| 219 |
current_a = x_in.extract::<$shift, 4>() + pred; |
| 220 |
preds[$shift..$shift + 4].copy_from_slice(pred.as_array()); |
| 221 |
}; |
| 222 |
} |
| 223 |
|
| 224 |
process_pixel!(0); |
| 225 |
process_pixel!(4); |
| 226 |
process_pixel!(8); |
| 227 |
process_pixel!(12); |
| 228 |
process_pixel!(16); |
| 229 |
process_pixel!(20); |
| 230 |
process_pixel!(24); |
| 231 |
process_pixel!(28); |
| 232 |
process_pixel!(32); |
| 233 |
process_pixel!(36); |
| 234 |
process_pixel!(40); |
| 235 |
process_pixel!(44); |
| 236 |
process_pixel!(48); |
| 237 |
process_pixel!(52); |
| 238 |
process_pixel!(56); |
| 239 |
process_pixel!(60); |
| 240 |
|
| 241 |
// Do a wide vectorized add to commit the predictions and return the next `a` pixel. |
| 242 |
*x_out += Simd::from_array(preds); |
| 243 |
current_a |
| 244 |
} |
| 245 |
|
| 246 |
/// Applies Paeth unfiltering on the `current` pixel row using `prev` row, |
| 247 |
/// interpreting the input data as RGBA. |
| 248 |
pub fn paeth_unfilter_4bpp(row: &mut [u8], prev_row: &[u8]) { |
| 249 |
const BPP: usize = 4; |
| 250 |
const STRIDE_BYTES: usize = 64; // 16 pixels * 4 bytes/pixel |
| 251 |
|
| 252 |
let mut a: Simd<u8, BPP> = Default::default(); // Left pixel (unfiltered) |
| 253 |
let mut c: Simd<u8, BPP> = Default::default(); // Upper-left pixel (unfiltered) |
| 254 |
|
| 255 |
// Set up iterators for SIMD body and scalar remainder. |
| 256 |
let chunks = row.len() / STRIDE_BYTES; |
| 257 |
let (simd_row, remainder_row) = row.split_at_mut(chunks * STRIDE_BYTES); |
| 258 |
let (simd_prev_row, remainder_prev_row) = prev_row.split_at(chunks * STRIDE_BYTES); |
| 259 |
let row_iter = simd_row.chunks_exact_mut(STRIDE_BYTES); |
| 260 |
let prev_iter = simd_prev_row.chunks_exact(STRIDE_BYTES); |
| 261 |
let combined_iter = row_iter.zip(prev_iter); |
| 262 |
|
| 263 |
for (chunk, prev_chunk) in combined_iter { |
| 264 |
let mut x: Simd<u8, STRIDE_BYTES> = Simd::<u8, STRIDE_BYTES>::from_slice(chunk); |
| 265 |
let b: Simd<u8, STRIDE_BYTES> = Simd::<u8, STRIDE_BYTES>::from_slice(prev_chunk); |
| 266 |
|
| 267 |
a = process_paeth_chunk_bpp4_s64(a, &b, c, &mut x); |
| 268 |
|
| 269 |
// Update `vlast` for the next chunk: last BPP bytes of `b` |
| 270 |
c = b.extract::<{ STRIDE_BYTES - BPP }, BPP>(); |
| 271 |
|
| 272 |
x.copy_to_slice(chunk); |
| 273 |
} |
| 274 |
|
| 275 |
// Scalar remainder. |
| 276 |
let mut a_bpp = a.to_array(); |
| 277 |
let mut c_bpp = c.to_array(); |
| 278 |
for (chunk, b_bpp) in remainder_row |
| 279 |
.chunks_exact_mut(BPP) |
| 280 |
.zip(remainder_prev_row.chunks_exact(BPP)) |
| 281 |
{ |
| 282 |
let new_chunk = [ |
| 283 |
chunk[0].wrapping_add(filter_paeth_chosen( |
| 284 |
a_bpp[0].into(), |
| 285 |
b_bpp[0].into(), |
| 286 |
c_bpp[0].into(), |
| 287 |
)), |
| 288 |
chunk[1].wrapping_add(filter_paeth_chosen( |
| 289 |
a_bpp[1].into(), |
| 290 |
b_bpp[1].into(), |
| 291 |
c_bpp[1].into(), |
| 292 |
)), |
| 293 |
chunk[2].wrapping_add(filter_paeth_chosen( |
| 294 |
a_bpp[2].into(), |
| 295 |
b_bpp[2].into(), |
| 296 |
c_bpp[2].into(), |
| 297 |
)), |
| 298 |
chunk[3].wrapping_add(filter_paeth_chosen( |
| 299 |
a_bpp[3].into(), |
| 300 |
b_bpp[3].into(), |
| 301 |
c_bpp[3].into(), |
| 302 |
)), |
| 303 |
]; |
| 304 |
*TryInto::<&mut [u8; BPP]>::try_into(chunk).unwrap() = new_chunk; |
| 305 |
a_bpp = new_chunk; |
| 306 |
c_bpp = b_bpp.try_into().unwrap(); |
| 307 |
} |
| 308 |
} |