From 590e4b811eeb42f87d04c8f16fd32ba3cae64211 Mon Sep 17 00:00:00 2001 From: FreezyLemon Date: Sun, 31 Mar 2024 23:35:53 +0200 Subject: [PATCH] Simplify NoiseStatus - Remove anyhow::Error field from Error variant - Rename/move to solver::NoiseModelStatus --- src/diff.rs | 20 ++------------------ src/diff/solver.rs | 37 +++++++++++++++++-------------------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index a85759f..dc461c9 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -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; @@ -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!( @@ -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, denoised: &Frame) -> 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); diff --git a/src/diff/solver.rs b/src/diff/solver.rs index 4697a52..7b59191 100644 --- a/src/diff/solver.rs +++ b/src/diff/solver.rs @@ -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, @@ -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 { @@ -402,7 +408,7 @@ impl NoiseModel { source: &Frame, denoised: &Frame, 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; @@ -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); @@ -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( @@ -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 @@ -489,10 +491,7 @@ 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; } } @@ -500,17 +499,15 @@ impl NoiseModel { &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)]