2024-04-08 03:17:36 +00:00
|
|
|
use crate::constants::TILE_SZ2;
|
2024-04-08 01:42:52 +00:00
|
|
|
|
2024-04-08 03:17:36 +00:00
|
|
|
const ZIGZAG: [u8; TILE_SZ2] = [
|
2024-04-08 01:42:52 +00:00
|
|
|
0 , 1 , 5 , 6 , 14, 15, 27, 28,
|
|
|
|
2 , 4 , 7 , 13, 16, 26, 29, 42,
|
|
|
|
3 , 8 , 12, 17, 25, 30, 41, 43,
|
|
|
|
9 , 11, 18, 24, 31, 40, 44, 53,
|
|
|
|
10, 19, 23, 32, 39, 45, 52, 54,
|
|
|
|
20, 22, 33, 38, 46, 51, 55, 60,
|
|
|
|
21, 34, 37, 47, 50, 56, 59, 61,
|
|
|
|
35, 36, 48, 49, 57, 58, 62, 63,
|
|
|
|
];
|
|
|
|
|
|
|
|
pub fn to_quantized(
|
2024-04-08 03:17:36 +00:00
|
|
|
coefs: [i32; TILE_SZ2]
|
|
|
|
) -> [i32; TILE_SZ2] {
|
|
|
|
let mut quant: [i32; TILE_SZ2] = [0; TILE_SZ2];
|
|
|
|
|
|
|
|
for cf_ix in 0..TILE_SZ2 {
|
|
|
|
let div = divisor(cf_ix);
|
|
|
|
let qval = (coefs[cf_ix] + div / 2) / div;
|
|
|
|
quant[ZIGZAG[cf_ix] as usize] = qval
|
|
|
|
}
|
2024-04-08 01:42:52 +00:00
|
|
|
|
2024-04-08 03:17:36 +00:00
|
|
|
let mut indices = [0; TILE_SZ2];
|
|
|
|
for i in 0..indices.len() {
|
|
|
|
indices[i] = i;
|
|
|
|
}
|
|
|
|
indices.sort_by_key(|i| -quant[*i].abs());
|
|
|
|
for i in 4..indices.len() {
|
|
|
|
quant[indices[i]] = 0;
|
2024-04-08 01:42:52 +00:00
|
|
|
}
|
2024-04-08 03:17:36 +00:00
|
|
|
|
2024-04-08 01:42:52 +00:00
|
|
|
quant
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_quantized(
|
2024-04-08 03:17:36 +00:00
|
|
|
quant: [i32; TILE_SZ2]
|
|
|
|
) -> [i32; TILE_SZ2] {
|
|
|
|
let mut coefs: [i32; TILE_SZ2] = [0; TILE_SZ2];
|
|
|
|
for cf_ix in 0..TILE_SZ2 {
|
|
|
|
let div = divisor(cf_ix);
|
|
|
|
coefs[cf_ix] = quant[ZIGZAG[cf_ix] as usize] * div;
|
2024-04-08 01:42:52 +00:00
|
|
|
}
|
|
|
|
coefs
|
2024-04-08 03:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn divisor(cf_ix: usize) -> i32 {
|
|
|
|
if cf_ix == 0 { return 1; }
|
|
|
|
let x = cf_ix % 8;
|
|
|
|
let y = cf_ix / 8;
|
|
|
|
let div = 32 + (x as i32 + y as i32) * 12;
|
|
|
|
if div==32 && cf_ix != 0 {
|
|
|
|
dbg!(cf_ix, x, y, div);
|
|
|
|
}
|
|
|
|
return div;
|
2024-04-08 01:42:52 +00:00
|
|
|
}
|