Skip to content

Commit

Permalink
Simplify NoiseStatus
Browse files Browse the repository at this point in the history
- Remove anyhow::Error field from Error variant
- Rename/move to solver::NoiseModelStatus
  • Loading branch information
FreezyLemon committed Mar 31, 2024
1 parent f0292b0 commit 02ab2ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
20 changes: 2 additions & 18 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{ensure, Result};
use num_rational::Rational64;
use v_frame::{frame::Frame, pixel::Pixel};

use self::solver::{FlatBlockFinder, NoiseModel};
use self::solver::{FlatBlockFinder, NoiseModel, NoiseModelStatus};
use crate::{util::frame_into_u8, GrainTableSegment};

mod solver;
Expand Down Expand Up @@ -75,7 +75,7 @@ impl DiffGenerator {
log::debug!("Updating noise model");
let status = self.noise_model.update(source, denoised, &flat_blocks);

if status == NoiseStatus::DifferentType {
if status == NoiseModelStatus::DifferentType {
let cur_timestamp = self.frame_count as u64 * 10_000_000u64 * *self.fps.denom() as u64
/ *self.fps.numer() as u64;
log::debug!(
Expand All @@ -97,22 +97,6 @@ impl DiffGenerator {
}
}

#[derive(Debug)]
enum NoiseStatus {
Ok,
DifferentType,
Error(anyhow::Error),
}

impl PartialEq for NoiseStatus {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(&Self::Error(_), &Self::Error(_)) => true,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}

fn verify_dimensions_match(source: &Frame<u8>, denoised: &Frame<u8>) -> Result<()> {
let res_1 = (source.planes[0].cfg.width, source.planes[0].cfg.height);
let res_2 = (denoised.planes[0].cfg.width, denoised.planes[0].cfg.height);
Expand Down
37 changes: 17 additions & 20 deletions src/diff/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ mod util;

use std::ops::{Add, AddAssign};

use anyhow::anyhow;
use arrayvec::ArrayVec;
use v_frame::{frame::Frame, math::clamp, plane::Plane};

use self::util::{extract_ar_row, get_block_mean, get_noise_var, linsolve, multiply_mat};
use super::{NoiseStatus, BLOCK_SIZE, BLOCK_SIZE_SQUARED};
use super::{BLOCK_SIZE, BLOCK_SIZE_SQUARED};
use crate::{
diff::solver::util::normalized_cross_correlation, GrainTableSegment, DEFAULT_GRAIN_SEED,
NUM_UV_COEFFS, NUM_UV_POINTS, NUM_Y_COEFFS, NUM_Y_POINTS,
Expand Down Expand Up @@ -360,6 +359,13 @@ pub(super) struct NoiseModel {
coords: Vec<[isize; 2]>,
}

#[derive(PartialEq, Eq)]
pub(super) enum NoiseModelStatus {
Ok,
DifferentType,
Error,
}

impl NoiseModel {
#[must_use]
pub fn new() -> Self {
Expand Down Expand Up @@ -402,7 +408,7 @@ impl NoiseModel {
source: &Frame<u8>,
denoised: &Frame<u8>,
flat_blocks: &[u8],
) -> NoiseStatus {
) -> NoiseModelStatus {
let num_blocks_w = (source.planes[0].cfg.width + BLOCK_SIZE - 1) / BLOCK_SIZE;
let num_blocks_h = (source.planes[0].cfg.height + BLOCK_SIZE - 1) / BLOCK_SIZE;
let mut y_model_different = false;
Expand All @@ -417,7 +423,8 @@ impl NoiseModel {
// Check that we have enough flat blocks
let num_blocks = flat_blocks.iter().filter(|b| **b > 0).count();
if num_blocks <= 1 {
return NoiseStatus::Error(anyhow!("Not enough flat blocks to update noise estimate"));
// need more than one flat block to update estimate
return NoiseModelStatus::Error;
}

let frame_dims = (source.planes[0].cfg.width, source.planes[0].cfg.height);
Expand Down Expand Up @@ -446,10 +453,7 @@ impl NoiseModel {
.eqns
.set_chroma_coefficient_fallback_solution();
} else {
return NoiseStatus::Error(anyhow!(
"Solving latest noise equation system failed on plane {}",
channel
));
return NoiseModelStatus::Error;
}
}
self.add_noise_std_observations(
Expand All @@ -463,9 +467,7 @@ impl NoiseModel {
num_blocks_h,
);
if !self.latest_state[channel].strength_solver.solve() {
return NoiseStatus::Error(anyhow!(
"Failed to solve strength solver for latest state"
));
return NoiseModelStatus::Error;
}

// Check noise characteristics and return if error
Expand All @@ -489,28 +491,23 @@ impl NoiseModel {
.eqns
.set_chroma_coefficient_fallback_solution();
} else {
return NoiseStatus::Error(anyhow!(
"Solving combined noise equation system failed on plane {}",
channel
));
return NoiseModelStatus::Error;
}
}

self.combined_state[channel].strength_solver +=
&self.latest_state[channel].strength_solver;

if !self.combined_state[channel].strength_solver.solve() {
return NoiseStatus::Error(anyhow!(
"Failed to solve strength solver for combined state"
));
return NoiseModelStatus::Error;
};
}

if y_model_different {
return NoiseStatus::DifferentType;
return NoiseModelStatus::DifferentType;
}

NoiseStatus::Ok
NoiseModelStatus::Ok
}

#[allow(clippy::too_many_lines)]
Expand Down

0 comments on commit 02ab2ea

Please sign in to comment.