Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
mcroomp committed Sep 27, 2024
1 parent a9874a0 commit 95aaddf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/structs/idct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ use bytemuck::cast_ref;

#[cfg(test)]
#[inline(always)]
pub fn get_q(offset: usize, q_transposed: &AlignedBlock) -> i32x8 {
fn get_q(offset: usize, q_transposed: &AlignedBlock) -> i32x8 {
use wide::u16x8;

let rows: &[u16x8; 8] = cast_ref(q_transposed.get_block());
Expand All @@ -142,7 +142,7 @@ pub fn get_q(offset: usize, q_transposed: &AlignedBlock) -> i32x8 {

#[cfg(test)]
#[inline(always)]
pub fn get_c(offset: usize, q_transposed: &AlignedBlock) -> i32x8 {
fn get_c(offset: usize, q_transposed: &AlignedBlock) -> i32x8 {
let rows: &[i16x8; 8] = cast_ref(q_transposed.get_block());
i32x8::from_i16x8(rows[offset])
}
Expand Down
14 changes: 6 additions & 8 deletions src/structs/lepton_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ pub fn read_coefficient_block<const ALL_PRESENT: bool, R: Read>(
// read how many of these are non-zero, which is used both
// to terminate the loop early and as a predictor for the model
let num_non_zeros_7x7 = model_per_color
.read_non_zero_7x7_count(bool_reader, num_non_zeros_7x7_context_bin)
.context(here!())?;
.read_non_zero_7x7_count(bool_reader, num_non_zeros_7x7_context_bin)?;

if num_non_zeros_7x7 > 49 {
// most likely a stream or model synchronization error
Expand Down Expand Up @@ -285,8 +284,7 @@ pub fn read_coefficient_block<const ALL_PRESENT: bool, R: Read>(
zig49,
num_non_zeros_bin,
best_prior_bit_length as usize,
)
.context(here!())?;
)?;

if coef != 0 {
// here we calculate the furthest x and y coordinates that have non-zero coefficients
Expand Down Expand Up @@ -348,8 +346,8 @@ pub fn read_coefficient_block<const ALL_PRESENT: bool, R: Read>(
color_index,
predicted_dc.uncertainty,
predicted_dc.uncertainty2,
)
.context(here!())?;
)?;

output.set_dc(ProbabilityTables::adv_predict_or_unpredict_dc(
coef,
true,
Expand All @@ -358,8 +356,8 @@ pub fn read_coefficient_block<const ALL_PRESENT: bool, R: Read>(

// neighbor summary is used as a predictor for the next block
let neighbor_summary = NeighborSummary::new(
predicted_dc.edge_pixels_h,
predicted_dc.edge_pixels_v,
predicted_dc.next_edge_pixels_h,
predicted_dc.next_edge_pixels_v,
output.get_dc() as i32 * q0,
num_non_zeros_7x7,
horiz_pred,
Expand Down
4 changes: 2 additions & 2 deletions src/structs/lepton_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ pub fn write_coefficient_block<const ALL_PRESENT: bool, W: Write>(

// neighbor summary is used as a predictor for the next block
let neighbor_summary = NeighborSummary::new(
predicted_val.edge_pixels_h,
predicted_val.edge_pixels_v,
predicted_val.next_edge_pixels_h,
predicted_val.next_edge_pixels_v,
here_tr.get_dc() as i32 * q0,
num_non_zeros_7x7,
horiz_pred,
Expand Down
51 changes: 26 additions & 25 deletions src/structs/probability_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub struct PredictDCResult {
pub predicted_dc: i32,
pub uncertainty: i16,
pub uncertainty2: i16,
pub edge_pixels_h: i16x8,
pub edge_pixels_v: i16x8,
pub next_edge_pixels_h: i16x8,
pub next_edge_pixels_v: i16x8,
}

impl ProbabilityTables {
Expand Down Expand Up @@ -241,8 +241,8 @@ impl ProbabilityTables {
// here DC in raster_cols should be 0
let pixels_sans_dc = run_idct(raster_cols);

// helper functions to avoid code duplication that calculate the left and above prediction values

// helper functions to avoid code duplication that calculate prediction values
#[inline]
fn calc_pred(a1: i16x8, a2: i16x8, is_16_bit: bool) -> i16x8 {
if is_16_bit {
let pixel_delta = a1 - a2;
Expand All @@ -260,25 +260,26 @@ impl ProbabilityTables {
}
}

// transpose so we can get the vertical rows as single vectors
let transposed = pixels_sans_dc.transpose();

let a1 = pixels_sans_dc.as_i16x8(0);
let a2 = pixels_sans_dc.as_i16x8(1);
let v =
let v_pred =
calc_pred(a1, a2, enabled_features.use_16bit_adv_predict) + 128 * X_IDCT_SCALE as i16;

let prev = pixels_sans_dc.as_i16x8(6);
let curr = pixels_sans_dc.as_i16x8(7);
let edge_pixels_h = calc_pred(curr, prev, enabled_features.use_16bit_dc_estimate);

let t = pixels_sans_dc.transpose();

let a1 = t.as_i16x8(0);
let a2 = t.as_i16x8(1);
let h =
let a1 = transposed.as_i16x8(0);
let a2 = transposed.as_i16x8(1);
let h_pred =
calc_pred(a1, a2, enabled_features.use_16bit_adv_predict) + 128 * X_IDCT_SCALE as i16;

let prev = t.as_i16x8(6);
let curr = t.as_i16x8(7);
let edge_pixels_v = calc_pred(curr, prev, enabled_features.use_16bit_dc_estimate);
let a1 = pixels_sans_dc.as_i16x8(7);
let a2 = pixels_sans_dc.as_i16x8(6);
let next_edge_pixels_v = calc_pred(a1, a2, enabled_features.use_16bit_dc_estimate);

let a1 = transposed.as_i16x8(7);
let a2 = transposed.as_i16x8(6);
let next_edge_pixels_h = calc_pred(a1, a2, enabled_features.use_16bit_dc_estimate);

let min_dc;
let max_dc;
Expand All @@ -287,23 +288,23 @@ impl ProbabilityTables {

if ALL_PRESENT {
// most common case where we have both left and above
let horiz = neighbor_data.neighbor_context_left.get_vertical_pix() - h;
let vert = neighbor_data.neighbor_context_above.get_horizontal_pix() - v;
let horiz = neighbor_data.neighbor_context_left.get_horizontal_pix() - h_pred;
let vert = neighbor_data.neighbor_context_above.get_vertical_pix() - v_pred;

min_dc = horiz.min(vert).reduce_min();
max_dc = horiz.max(vert).reduce_max();

avg_horizontal = i32x8::from_i16x8(horiz).reduce_add();
avg_vertical = i32x8::from_i16x8(vert).reduce_add();
} else if self.left_present {
let horiz = neighbor_data.neighbor_context_left.get_vertical_pix() - h;
let horiz = neighbor_data.neighbor_context_left.get_horizontal_pix() - h_pred;
min_dc = horiz.reduce_min();
max_dc = horiz.reduce_max();

avg_horizontal = i32x8::from_i16x8(horiz).reduce_add();
avg_vertical = avg_horizontal;
} else if self.above_present {
let vert = neighbor_data.neighbor_context_above.get_horizontal_pix() - v;
let vert = neighbor_data.neighbor_context_above.get_vertical_pix() - v_pred;
min_dc = vert.reduce_min();
max_dc = vert.reduce_max();

Expand All @@ -314,8 +315,8 @@ impl ProbabilityTables {
predicted_dc: 0,
uncertainty: 0,
uncertainty2: 0,
edge_pixels_h,
edge_pixels_v,
next_edge_pixels_h,
next_edge_pixels_v,
};
}

Expand All @@ -335,8 +336,8 @@ impl ProbabilityTables {
predicted_dc: (avgmed / q0 + 4) >> 3,
uncertainty: uncertainty_val,
uncertainty2: uncertainty2_val,
edge_pixels_h,
edge_pixels_v,
next_edge_pixels_h,
next_edge_pixels_v,
};
}
}

0 comments on commit 95aaddf

Please sign in to comment.