| 1 |
/* |
| 2 |
* Copyright (c) 2023. |
| 3 |
* |
| 4 |
* This software is free software; |
| 5 |
* |
| 6 |
* You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license |
| 7 |
*/ |
| 8 |
|
| 9 |
#![cfg(target_arch = "aarch64")] |
| 10 |
//! AVX optimised IDCT. |
| 11 |
//! |
| 12 |
//! Okay not thaat optimised. |
| 13 |
//! |
| 14 |
//! |
| 15 |
//! # The implementation |
| 16 |
//! The implementation is neatly broken down into two operations. |
| 17 |
//! |
| 18 |
//! 1. Test for zeroes |
| 19 |
//! > There is a shortcut method for idct where when all AC values are zero, we can get the answer really quickly. |
| 20 |
//! by scaling the 1/8th of the DCT coefficient of the block to the whole block and level shifting. |
| 21 |
//! |
| 22 |
//! 2. If above fails, we proceed to carry out IDCT as a two pass one dimensional algorithm. |
| 23 |
//! IT does two whole scans where it carries out IDCT on all items |
| 24 |
//! After each successive scan, data is transposed in register(thank you x86 SIMD powers). and the second |
| 25 |
//! pass is carried out. |
| 26 |
//! |
| 27 |
//! The code is not super optimized, it produces bit identical results with scalar code hence it's |
| 28 |
//! `mm256_add_epi16` |
| 29 |
//! and it also has the advantage of making this implementation easy to maintain. |
| 30 |
|
| 31 |
#![cfg(feature = "neon")] |
| 32 |
|
| 33 |
use core::arch::aarch64::*; |
| 34 |
|
| 35 |
use crate::unsafe_utils::{transpose, YmmRegister}; |
| 36 |
|
| 37 |
const SCALE_BITS: i32 = 512 + 65536 + (128 << 17); |
| 38 |
|
| 39 |
|
| 40 |
#[inline] |
| 41 |
#[target_feature(enable = "neon")] |
| 42 |
unsafe fn pack_16(a: int32x4x2_t) -> int16x8_t { |
| 43 |
vcombine_s16(vqmovn_s32(a.0), vqmovn_s32(a.1)) |
| 44 |
} |
| 45 |
|
| 46 |
#[inline] |
| 47 |
#[target_feature(enable = "neon")] |
| 48 |
unsafe fn condense_bottom_16(a: int32x4x2_t, b: int32x4x2_t) -> int16x8x2_t { |
| 49 |
int16x8x2_t(pack_16(a), pack_16(b)) |
| 50 |
} |
| 51 |
|
| 52 |
#[target_feature(enable = "neon")] |
| 53 |
#[allow( |
| 54 |
clippy::too_many_lines, |
| 55 |
clippy::cast_possible_truncation, |
| 56 |
clippy::similar_names, |
| 57 |
clippy::op_ref, |
| 58 |
unused_assignments, |
| 59 |
clippy::zero_prefixed_literal |
| 60 |
)] |
| 61 |
pub unsafe fn idct_neon( |
| 62 |
in_vector: &mut [i32; 64], out_vector: &mut [i16], stride: usize |
| 63 |
) { |
| 64 |
let mut pos = 0; |
| 65 |
|
| 66 |
// load into registers |
| 67 |
// |
| 68 |
// We sign extend i16's to i32's and calculate them with extended precision and |
| 69 |
// later reduce them to i16's when we are done carrying out IDCT |
| 70 |
|
| 71 |
let mut row0 = YmmRegister::load(in_vector[00..].as_ptr().cast()); |
| 72 |
let mut row1 = YmmRegister::load(in_vector[08..].as_ptr().cast()); |
| 73 |
let mut row2 = YmmRegister::load(in_vector[16..].as_ptr().cast()); |
| 74 |
let mut row3 = YmmRegister::load(in_vector[24..].as_ptr().cast()); |
| 75 |
let mut row4 = YmmRegister::load(in_vector[32..].as_ptr().cast()); |
| 76 |
let mut row5 = YmmRegister::load(in_vector[40..].as_ptr().cast()); |
| 77 |
let mut row6 = YmmRegister::load(in_vector[48..].as_ptr().cast()); |
| 78 |
let mut row7 = YmmRegister::load(in_vector[56..].as_ptr().cast()); |
| 79 |
|
| 80 |
// Forward DCT and quantization may cause all the AC terms to be zero, for such |
| 81 |
// cases we can try to accelerate it |
| 82 |
|
| 83 |
// Basically the poop is that whenever the array has 63 zeroes, its idct is |
| 84 |
// (arr[0]>>3)or (arr[0]/8) propagated to all the elements. |
| 85 |
// We first test to see if the array contains zero elements and if it does, we go the |
| 86 |
// short way. |
| 87 |
// |
| 88 |
// This reduces IDCT overhead from about 39% to 18 %, almost half |
| 89 |
|
| 90 |
// Do another load for the first row, we don't want to check DC value, because |
| 91 |
// we only care about AC terms |
| 92 |
// TODO this should be a shift/shuffle, not a likely unaligned load |
| 93 |
let row8 = YmmRegister::load(in_vector[1..].as_ptr().cast()); |
| 94 |
|
| 95 |
let or_tree = (((row1 | row8) | (row2 | row3)) | ((row4 | row5) | (row6 | row7))); |
| 96 |
|
| 97 |
if or_tree.all_zero() { |
| 98 |
// AC terms all zero, idct of the block is ( coeff[0] * qt[0] )/8 + 128 (bias) |
| 99 |
// (and clamped to 255) |
| 100 |
// Round by adding 0.5 * (1 << 3) and offset by adding (128 << 3) before scaling |
| 101 |
let coeff = ((in_vector[0] + 4 + 1024) >> 3).clamp(0, 255) as i16; |
| 102 |
let idct_value = vdupq_n_s16(coeff); |
| 103 |
|
| 104 |
macro_rules! store { |
| 105 |
($pos:tt,$value:tt) => { |
| 106 |
// store |
| 107 |
vst1q_s16( |
| 108 |
out_vector |
| 109 |
.get_mut($pos..$pos + 8) |
| 110 |
.unwrap() |
| 111 |
.as_mut_ptr() |
| 112 |
.cast(), |
| 113 |
$value |
| 114 |
); |
| 115 |
$pos += stride; |
| 116 |
}; |
| 117 |
} |
| 118 |
store!(pos, idct_value); |
| 119 |
store!(pos, idct_value); |
| 120 |
store!(pos, idct_value); |
| 121 |
store!(pos, idct_value); |
| 122 |
|
| 123 |
store!(pos, idct_value); |
| 124 |
store!(pos, idct_value); |
| 125 |
store!(pos, idct_value); |
| 126 |
store!(pos, idct_value); |
| 127 |
|
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
macro_rules! dct_pass { |
| 132 |
($SCALE_BITS:tt,$scale:tt) => { |
| 133 |
// There are a lot of ways to do this |
| 134 |
// but to keep it simple(and beautiful), ill make a direct translation of the |
| 135 |
// scalar code to also make this code fully transparent(this version and the non |
| 136 |
// avx one should produce identical code.) |
| 137 |
|
| 138 |
// Compiler does a pretty good job of optimizing add + mul pairs |
| 139 |
// into multiply-acumulate pairs |
| 140 |
|
| 141 |
// even part |
| 142 |
let p1 = (row2 + row6) * 2217; |
| 143 |
|
| 144 |
let mut t2 = p1 + row6 * -7567; |
| 145 |
let mut t3 = p1 + row2 * 3135; |
| 146 |
|
| 147 |
let mut t0 = (row0 + row4).const_shl::<12>(); |
| 148 |
let mut t1 = (row0 - row4).const_shl::<12>(); |
| 149 |
|
| 150 |
let x0 = t0 + t3 + $SCALE_BITS; |
| 151 |
let x3 = t0 - t3 + $SCALE_BITS; |
| 152 |
let x1 = t1 + t2 + $SCALE_BITS; |
| 153 |
let x2 = t1 - t2 + $SCALE_BITS; |
| 154 |
|
| 155 |
let p3 = row7 + row3; |
| 156 |
let p4 = row5 + row1; |
| 157 |
let p1 = row7 + row1; |
| 158 |
let p2 = row5 + row3; |
| 159 |
let p5 = (p3 + p4) * 4816; |
| 160 |
|
| 161 |
t0 = row7 * 1223; |
| 162 |
t1 = row5 * 8410; |
| 163 |
t2 = row3 * 12586; |
| 164 |
t3 = row1 * 6149; |
| 165 |
|
| 166 |
let p1 = p5 + p1 * -3685; |
| 167 |
let p2 = p5 + (p2 * -10497); |
| 168 |
let p3 = p3 * -8034; |
| 169 |
let p4 = p4 * -1597; |
| 170 |
|
| 171 |
t3 += p1 + p4; |
| 172 |
t2 += p2 + p3; |
| 173 |
t1 += p2 + p4; |
| 174 |
t0 += p1 + p3; |
| 175 |
|
| 176 |
row0 = (x0 + t3).const_shra::<$scale>(); |
| 177 |
row1 = (x1 + t2).const_shra::<$scale>(); |
| 178 |
row2 = (x2 + t1).const_shra::<$scale>(); |
| 179 |
row3 = (x3 + t0).const_shra::<$scale>(); |
| 180 |
|
| 181 |
row4 = (x3 - t0).const_shra::<$scale>(); |
| 182 |
row5 = (x2 - t1).const_shra::<$scale>(); |
| 183 |
row6 = (x1 - t2).const_shra::<$scale>(); |
| 184 |
row7 = (x0 - t3).const_shra::<$scale>(); |
| 185 |
}; |
| 186 |
} |
| 187 |
|
| 188 |
// Process rows |
| 189 |
dct_pass!(512, 10); |
| 190 |
transpose( |
| 191 |
&mut row0, &mut row1, &mut row2, &mut row3, &mut row4, &mut row5, &mut row6, &mut row7 |
| 192 |
); |
| 193 |
|
| 194 |
// process columns |
| 195 |
dct_pass!(SCALE_BITS, 17); |
| 196 |
transpose( |
| 197 |
&mut row0, &mut row1, &mut row2, &mut row3, &mut row4, &mut row5, &mut row6, &mut row7 |
| 198 |
); |
| 199 |
|
| 200 |
// Pack i32 to i16's, |
| 201 |
// clamp them to be between 0-255 |
| 202 |
// Undo shuffling |
| 203 |
// Store back to array |
| 204 |
|
| 205 |
// This could potentially be reorganized to take advantage of the multi-register stores |
| 206 |
macro_rules! permute_store { |
| 207 |
($x:tt,$y:tt,$index:tt,$out:tt) => { |
| 208 |
let a = condense_bottom_16($x, $y); |
| 209 |
|
| 210 |
// Clamp the values after packing, we can clamp more values at once |
| 211 |
let b = clamp256_neon(a); |
| 212 |
|
| 213 |
// store first vector |
| 214 |
vst1q_s16( |
| 215 |
($out) |
| 216 |
.get_mut($index..$index + 8) |
| 217 |
.unwrap() |
| 218 |
.as_mut_ptr() |
| 219 |
.cast(), |
| 220 |
b.0 |
| 221 |
); |
| 222 |
$index += stride; |
| 223 |
// second vector |
| 224 |
vst1q_s16( |
| 225 |
($out) |
| 226 |
.get_mut($index..$index + 8) |
| 227 |
.unwrap() |
| 228 |
.as_mut_ptr() |
| 229 |
.cast(), |
| 230 |
b.1 |
| 231 |
); |
| 232 |
$index += stride; |
| 233 |
}; |
| 234 |
} |
| 235 |
// Pack and write the values back to the array |
| 236 |
permute_store!((row0.mm256), (row1.mm256), pos, out_vector); |
| 237 |
permute_store!((row2.mm256), (row3.mm256), pos, out_vector); |
| 238 |
permute_store!((row4.mm256), (row5.mm256), pos, out_vector); |
| 239 |
permute_store!((row6.mm256), (row7.mm256), pos, out_vector); |
| 240 |
} |
| 241 |
|
| 242 |
#[inline] |
| 243 |
#[target_feature(enable = "neon")] |
| 244 |
unsafe fn clamp_neon(reg: int16x8_t) -> int16x8_t { |
| 245 |
let min_s = vdupq_n_s16(0); |
| 246 |
let max_s = vdupq_n_s16(255); |
| 247 |
|
| 248 |
let max_v = vmaxq_s16(reg, min_s); //max(a,0) |
| 249 |
let min_v = vminq_s16(max_v, max_s); //min(max(a,0),255) |
| 250 |
min_v |
| 251 |
} |
| 252 |
|
| 253 |
#[inline] |
| 254 |
#[target_feature(enable = "neon")] |
| 255 |
unsafe fn clamp256_neon(reg: int16x8x2_t) -> int16x8x2_t { |
| 256 |
int16x8x2_t(clamp_neon(reg.0), clamp_neon(reg.1)) |
| 257 |
} |
| 258 |
|
| 259 |
#[cfg(test)] |
| 260 |
mod test { |
| 261 |
use super::*; |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn test_neon_clamp_256() { |
| 265 |
unsafe { |
| 266 |
let vals: [i16; 16] = [-1, -2, -3, 4, 256, 257, 258, 240, -1, 290, 2, 3, 4, 5, 6, 7]; |
| 267 |
let loaded = vld1q_s16_x2(vals.as_ptr().cast()); |
| 268 |
let shuffled = clamp256_neon(loaded); |
| 269 |
|
| 270 |
let mut result: [i16; 16] = [0; 16]; |
| 271 |
|
| 272 |
vst1q_s16_x2(result.as_mut_ptr().cast(), shuffled); |
| 273 |
|
| 274 |
assert_eq!( |
| 275 |
result, |
| 276 |
[0, 0, 0, 4, 255, 255, 255, 240, 0, 255, 2, 3, 4, 5, 6, 7] |
| 277 |
) |
| 278 |
} |
| 279 |
} |
| 280 |
} |