use crate::encoding_context::EncodingContext;
const ITERATIONS: usize = 3;
const MIN_SATURATED_SHARE: f32 = 0.02;
const Y_TOLERANCE: f32 = 0.002;
const MAX_COVERED_BLOCKS: usize = 16;
const NORMAL: u8 = 0;
const WHITE: u8 = 1;
const BLACK: u8 = 2;
#[derive(Default)]
pub(crate) struct ClipScratch {
source: Vec<f32>,
target: Vec<f32>,
shift: Vec<f32>,
coefficients: Vec<f32>,
levels: Vec<i32>,
pixels: Vec<f32>,
class: Vec<u8>,
shifted: Vec<f32>,
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn continue_saturated(
ctx: &EncodingContext,
raw_strategy: u8,
cx: usize,
cy: usize,
inv_qm: &[f32],
qm: &[f32],
quant_ac: i32,
scale: f32,
distance: f32,
input: &[f32],
stride: usize,
scratch: &mut ClipScratch,
) -> bool {
let covered = cx * cy;
if covered > MAX_COVERED_BLOCKS {
return false;
}
let n = covered * 64;
let (pw, ph) = crate::inflated_cost::strategy_pixel_dims(raw_strategy);
debug_assert_eq!(pw * ph, n);
let (_, white, _) = crate::xyb::rgb_to_xyb_pixel_f32(&ctx.xyb, 1.0, 1.0, 1.0);
let (_, black, _) = crate::xyb::rgb_to_xyb_pixel_f32(&ctx.xyb, 0.0, 0.0, 0.0);
scratch.class.clear();
let mut saturated = 0;
let (mut any_white, mut any_black, mut any_normal) = (false, false, false);
for row in input.chunks(stride).take(ph) {
for &v in &row[..pw] {
let class = if v >= white - Y_TOLERANCE {
any_white = true;
WHITE
} else if v <= black + Y_TOLERANCE {
any_black = true;
BLACK
} else {
any_normal = true;
NORMAL
};
saturated += usize::from(class != NORMAL);
scratch.class.push(class);
}
}
let single_class = !any_normal && (any_white != any_black);
if single_class || (saturated as f32) < MIN_SATURATED_SHARE * n as f32 {
return false;
}
scratch.source.clear();
scratch.source.reserve(n);
for row in input.chunks(stride).take(ph) {
scratch.source.extend_from_slice(&row[..pw]);
}
scratch.target.clone_from(&scratch.source);
scratch.coefficients.resize(n, 0.0);
scratch.levels.resize(n, 0);
scratch.pixels.resize(n, 0.0);
let bias = ctx.dequant_bias(1);
let inv_qac = 1.0 / (scale * quant_ac as f32);
let wc = cx * 8;
for _ in 0..ITERATIONS {
crate::inflated_cost::forward_for(raw_strategy, &scratch.target, &mut scratch.coefficients);
(ctx.quantize_block_ac)(
&scratch.coefficients,
1,
inv_qm,
quant_ac,
scale,
1.0,
distance,
cx,
cy,
&mut scratch.levels,
);
for (y, ((coefficients, levels), weights)) in scratch
.coefficients
.chunks_exact_mut(wc)
.zip(scratch.levels.chunks_exact(wc))
.zip(qm.chunks_exact(wc))
.enumerate()
{
let skip = if y < cy { cx } else { 0 };
for ((coefficient, &level), &weight) in
coefficients.iter_mut().zip(levels).zip(weights).skip(skip)
{
*coefficient = bias.level(level) * weight * inv_qac;
}
}
crate::inflated_cost::reconstruct_error(
ctx.idct,
raw_strategy,
&scratch.coefficients,
&mut scratch.pixels,
);
for ((target, &recon), &class) in scratch
.target
.iter_mut()
.zip(&scratch.pixels)
.zip(&scratch.class)
{
match class {
WHITE => *target = recon.max(white),
BLACK => *target = recon.min(black),
_ => {}
}
}
}
scratch.shift.clear();
scratch.shift.extend(
scratch
.target
.iter()
.zip(&scratch.source)
.map(|(t, s)| t - s),
);
scratch.shift.iter().any(|&d| d != 0.0)
}
pub(crate) fn shifted_input<'a>(
scratch: &'a mut ClipScratch,
input: &[f32],
stride: usize,
pw: usize,
ph: usize,
) -> &'a [f32] {
scratch.shifted.clear();
for y in 0..ph {
let row = &input[y * stride..y * stride + pw];
let shift = &scratch.shift[y * pw..y * pw + pw];
scratch
.shifted
.extend(row.iter().zip(shift).map(|(a, b)| a + b));
}
&scratch.shifted
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Speed;
use crate::dc_group_data::STRATEGY_DCT;
use crate::image::Image3F;
use crate::xyb::{XybMatrix, rgb_to_xyb_pixel_f32};
fn context() -> EncodingContext {
EncodingContext::new(
Speed::Slow,
crate::Arbiter::Ssimulacra2,
XybMatrix::SPEC,
2.0,
1,
)
}
fn image(left_value: f32, right_value: f32, left: usize) -> Image3F {
let mut xyb = Image3F::new(8, 8);
for y in 0..8 {
let row = xyb.plane_row_mut(1, y);
for (x, v) in row.iter_mut().enumerate() {
*v = if x < left { left_value } else { right_value };
}
}
xyb
}
fn run(ctx: &EncodingContext, xyb: &Image3F, scratch: &mut ClipScratch) -> bool {
let matrices = ctx.matrices();
let plane = xyb.plane(1);
continue_saturated(
ctx,
STRATEGY_DCT,
1,
1,
&matrices.inv_matrix(1)[..],
&matrices.matrix(1)[..],
8,
1.0,
2.0,
plane.as_slice(),
plane.xsize(),
scratch,
)
}
#[test]
fn saturated_pixels_only_move_beyond_their_limit() {
let ctx = context();
let (_, white, _) = rgb_to_xyb_pixel_f32(&ctx.xyb, 1.0, 1.0, 1.0);
let (_, black, _) = rgb_to_xyb_pixel_f32(&ctx.xyb, 0.0, 0.0, 0.0);
let xyb = image(black, white, 3);
let mut scratch = ClipScratch::default();
assert!(run(&ctx, &xyb, &mut scratch));
for y in 0..8 {
for x in 0..8 {
let shift = scratch.shift[y * 8 + x];
if x < 3 {
assert!(shift <= 0.0, "black pixel moved up at {x},{y}");
} else {
assert!(shift >= 0.0, "white pixel moved down at {x},{y}");
}
}
}
let shifted = shifted_input(&mut scratch, xyb.plane(1).as_slice(), 8, 8, 8).to_vec();
assert!(
shifted
.iter()
.zip(xyb.plane(1).as_slice())
.any(|(a, b)| a != b)
);
let mut xyb = image(black, white, 3);
for y in 0..8 {
xyb.plane_row_mut(1, y)[3] = 0.5 * (black + white);
}
let mut scratch = ClipScratch::default();
assert!(run(&ctx, &xyb, &mut scratch));
for y in 0..8 {
assert_eq!(scratch.shift[y * 8 + 3], 0.0);
}
}
#[test]
fn unsaturated_and_single_limit_footprints_are_untouched() {
let ctx = context();
let (_, white, _) = rgb_to_xyb_pixel_f32(&ctx.xyb, 1.0, 1.0, 1.0);
let mut scratch = ClipScratch::default();
assert!(!run(&ctx, &image(0.2, 0.6, 4), &mut scratch));
assert!(!run(&ctx, &image(white, white, 4), &mut scratch));
}
}