Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precalculate Non-zero divisors for lak #89

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/structs/lepton_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ fn decode_edge<R: Read, const ALL_PRESENT: bool>(
decode_one_edge::<R, ALL_PRESENT, true>(
model_per_color,
bool_reader,
&curr_horiz_pred.to_array(),
&curr_horiz_pred,
here_mut,
qt,
pt,
Expand All @@ -486,7 +486,7 @@ fn decode_edge<R: Read, const ALL_PRESENT: bool>(
decode_one_edge::<R, ALL_PRESENT, false>(
model_per_color,
bool_reader,
&curr_vert_pred.to_array(),
&curr_vert_pred,
here_mut,
qt,
pt,
Expand All @@ -504,7 +504,7 @@ fn decode_edge<R: Read, const ALL_PRESENT: bool>(
fn decode_one_edge<R: Read, const ALL_PRESENT: bool, const HORIZONTAL: bool>(
model_per_color: &mut ModelPerColor,
bool_reader: &mut VPXBoolReader<R>,
pred: &[i32; 8],
pred: &i32x8,
here_mut: &mut AlignedBlock,
qt: &QuantizationTables,
pt: &ProbabilityTables,
Expand All @@ -529,13 +529,13 @@ fn decode_one_edge<R: Read, const ALL_PRESENT: bool, const HORIZONTAL: bool>(

let mut coord_tr = delta;

for _lane in 0..7 {
for lane in 1..8 {
if num_non_zeros_edge == 0 {
break;
}

let best_prior =
pt.calc_coefficient_context8_lak::<ALL_PRESENT, HORIZONTAL>(qt, coord_tr, pred);
pt.calc_coefficient_context8_lak::<ALL_PRESENT, HORIZONTAL>(qt, lane, pred);

let coef = model_per_color.read_edge_coefficient(
bool_reader,
Expand Down
10 changes: 5 additions & 5 deletions src/structs/lepton_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ fn encode_edge<W: Write, const ALL_PRESENT: bool>(
here_tr,
model_per_color,
bool_writer,
&curr_horiz_pred.to_array(),
&curr_horiz_pred,
qt,
pt,
num_non_zeros_bin,
Expand All @@ -512,7 +512,7 @@ fn encode_edge<W: Write, const ALL_PRESENT: bool>(
here_tr,
model_per_color,
bool_writer,
&curr_vert_pred.to_array(),
&curr_vert_pred,
qt,
pt,
num_non_zeros_bin,
Expand All @@ -538,7 +538,7 @@ fn encode_one_edge<W: Write, const ALL_PRESENT: bool, const HORIZONTAL: bool>(
block: &AlignedBlock,
model_per_color: &mut ModelPerColor,
bool_writer: &mut VPXBoolWriter<W>,
pred: &[i32; 8],
pred: &i32x8,
qt: &QuantizationTables,
pt: &ProbabilityTables,
num_non_zeros_bin: u8,
Expand Down Expand Up @@ -586,13 +586,13 @@ fn encode_one_edge<W: Write, const ALL_PRESENT: bool, const HORIZONTAL: bool>(

let mut coord_tr = delta;

for _lane in 0..7 {
for lane in 1..8 {
if num_non_zeros_edge == 0 {
break;
}

let best_prior =
pt.calc_coefficient_context8_lak::<ALL_PRESENT, HORIZONTAL>(qt, coord_tr, pred);
pt.calc_coefficient_context8_lak::<ALL_PRESENT, HORIZONTAL>(qt, lane, pred);

let coef = block.get_coefficient(coord_tr);

Expand Down
13 changes: 3 additions & 10 deletions src/structs/probability_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,16 @@ impl ProbabilityTables {
pub fn calc_coefficient_context8_lak<const ALL_PRESENT: bool, const HORIZONTAL: bool>(
&self,
qt: &QuantizationTables,
coefficient_tr: usize,
pred: &[i32; 8],
lane: usize,
pred: &i32x8,
) -> i32 {
if !ALL_PRESENT
&& ((HORIZONTAL && !self.above_present) || (!HORIZONTAL && !self.left_present))
{
return 0;
}

let mut best_prior: i32 = pred[if HORIZONTAL {
coefficient_tr >> 3
} else {
coefficient_tr
}];
best_prior /= (qt.get_quantization_table_transposed()[coefficient_tr] as i32) << 13;

best_prior
pred.as_array_ref()[lane] / qt.get_quantization_table_divisors::<HORIZONTAL>()[lane].get()
}

pub fn adv_predict_dc_pix<const ALL_PRESENT: bool>(
Expand Down
32 changes: 32 additions & 0 deletions src/structs/quantization_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@
* This software incorporates material from third parties. See NOTICE.txt for details.
*--------------------------------------------------------------------------------------------*/

use std::num::NonZeroI32;

use crate::consts::*;
use crate::helpers::*;

use super::jpeg_header::JPegHeader;

pub struct QuantizationTables {
quantization_table: [u16; 64],

/// transposed version of quantization table
quantization_table_transposed: [u16; 64],

/// precalculated divisors * 8192 for the top row of the quantization table for final step of lak calculation
/// compiler sees non-zero to avoid having to check for division-by-zero
quantization_table_divisors_horiz: [NonZeroI32; 8],

/// precalculated divisors * 8192 for the left column of the quantization table for final step of lak calculation
/// compiler sees non-zero to avoid having to check for division-by-zero
quantization_table_divisors_vert: [NonZeroI32; 8],

// Values for discrimination between "regular" and "noise" part of
// edge AC coefficients, used in `read/write_edge_coefficient`.
// Calculated using approximate maximal magnitudes
Expand All @@ -31,6 +44,8 @@ impl QuantizationTables {
quantization_table: [0; 64],
quantization_table_transposed: [0; 64],
min_noise_threshold: [0; 14],
quantization_table_divisors_horiz: [NonZeroI32::new(1).unwrap(); 8],
quantization_table_divisors_vert: [NonZeroI32::new(1).unwrap(); 8],
};

for pixel_row in 0..8 {
Expand All @@ -41,6 +56,16 @@ impl QuantizationTables {

retval.quantization_table[coord] = q;
retval.quantization_table_transposed[coord_tr] = q;

if pixel_row == 0 {
retval.quantization_table_divisors_horiz[pixel_column] =
NonZeroI32::new(i32::from(q) << 13).unwrap();
}

if pixel_column == 0 {
retval.quantization_table_divisors_vert[pixel_row] =
NonZeroI32::new(i32::from(q) << 13).unwrap();
}
}
}

Expand Down Expand Up @@ -70,6 +95,13 @@ impl QuantizationTables {
&self.quantization_table_transposed
}

pub fn get_quantization_table_divisors<const HORIZONTAL: bool>(&self) -> &[NonZeroI32; 8] {
if HORIZONTAL {
&self.quantization_table_divisors_horiz
} else {
&self.quantization_table_divisors_vert
}
}
pub fn get_min_noise_threshold(&self, coef: usize) -> u8 {
self.min_noise_threshold[coef]
}
Expand Down
Loading