Skip to content

Commit

Permalink
refator quantization
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed Jun 3, 2024
1 parent d337e6f commit f14ed75
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
4 changes: 4 additions & 0 deletions src/huffman_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ pub(crate) type NPtr = Option<NonNull<HuffmanNode>>;

#[derive(Debug)]
pub struct HuffmanTree {
/// Table class - 0 = DC table or lossless table, 1 = AC table.
pub(crate) class: HuffmanClass,

/// Specifies one of four possible destinations where the huffman table will be used.
pub(crate) destination_id: u8,

root: NPtr,
_woof: PhantomData<HuffmanNode>,
}
Expand Down
39 changes: 20 additions & 19 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::frame_header::{Component, ComponentType, FrameHeader};
use crate::huffman_tree::HuffmanTree;
use crate::marker::Marker;
use crate::quantization_table::QuantTable;
use crate::quantization_table::{QuantizationTable};
use crate::sample_precision::SamplePrecision;
use crate::scan_header::{ScanComponentSelector, ScanHeader};
use crate::EncodingProcess;
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Parser {
Ok((qt_ids, qt_precisions))
}

pub(crate) fn parse_quant_table(&self) -> Result<Vec<QuantTable>> {
pub(crate) fn parse_quant_table(&self) -> Result<Vec<QuantizationTable>> {
let mut tables = vec![];

let (qt_ids, qt_precisions) = self.parse_quant_table_information()?;
Expand All @@ -93,7 +93,7 @@ impl Parser {
);

let (qt_id, qt_precision) = (qt_ids[idx], qt_precisions[idx]);
tables.push(QuantTable::from(qt_id, qt_precision, qt_data))
tables.push(QuantizationTable::from(qt_id, qt_precision, qt_data))
}

Ok(tables)
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Parser {
"as of now assume only dealing with color components is 3"
);

let mut scan_component_selectors= vec![];
let mut scan_component_selectors = vec![];

let component_ids = Simd::from([
self.buffer[current_offset],
Expand Down Expand Up @@ -206,19 +206,20 @@ impl Parser {
let approx_bit_chunk = self.buffer[current_offset];
current_offset += 1;

let (successive_approx_bit_position_high, point_transform) = (
approx_bit_chunk >> 4,
approx_bit_chunk & 0b1111
);

Ok((ScanHeader {
component_type,
scan_component_selectors,
predictor_selection,
end_of_spectral_selection,
successive_approx_bit_position_high,
point_transform
}, current_offset))
let (successive_approx_bit_position_high, point_transform) =
(approx_bit_chunk >> 4, approx_bit_chunk & 0b1111);

Ok((
ScanHeader {
component_type,
scan_component_selectors,
predictor_selection,
end_of_spectral_selection,
successive_approx_bit_position_high,
point_transform,
},
current_offset,
))
}

pub(crate) fn parse_start_of_frame(&self) -> Result<FrameHeader> {
Expand Down Expand Up @@ -430,8 +431,8 @@ mod tests {
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x07, 0xB8, 0x09, 0x38, 0x39, 0x76,
0x78, // 28
0xFF, 0xDA, // START OF SCAN
0x00, 0x08, 0x03, 0x01, 0x10,
0x01, 0x3F, 0x10, // three bytes that we skip in sos
0x00, 0x08, 0x03, 0x01, 0x10, 0x01, 0x3F,
0x10, // three bytes that we skip in sos
0xFF, // this should be the start of image data
0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x02, 0x04, b'h', 0x02, 0xFF, 0xD9, // EOI
];
Expand Down
30 changes: 19 additions & 11 deletions src/quantization_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ enum TableType {
Chrominance = 1,
}


/// The set of 64 quantization values used to quantize the DCT coefficients
#[derive(Debug)]
pub struct QuantTable {
table_type: TableType,
pub struct QuantizationTable {

/// Specifies the precision of the qk values. Value 0 indicates 8-bit Qk values; value 1
/// indicates 16-bit Qk values. Pq shall be zero for 8 bit sample precision P.
precision: SamplePrecision,
data: Simd<u8, 64>, // 8x8

/// Specifies one of four possible destinations at the decoder into which the quantization
/// shall be used.
table_destination_id: u8,

/// Specifies the kth element out of 64 elements, where k is the index in the zig-zag ordering
/// of the DCT coefficients. The quantization elements shall be specified in zig-zag scan order.
quantization_table_element: Simd<u8, 64>,
}

impl QuantTable {
impl QuantizationTable {
pub(crate) fn from(qt_id: u8, qt_precision: u8, qt_data: Simd<u8, 64>) -> Self {
QuantTable {
table_type: match qt_id {
0 => TableType::Luminance,
1 => TableType::Chrominance,
_ => unreachable!(),
},
QuantizationTable {
table_destination_id: qt_id,
precision: SamplePrecision::decode(qt_precision),
data: qt_data,
quantization_table_element: qt_data
}
}
}

3 changes: 1 addition & 2 deletions src/scan_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ impl ScanComponentSelector {
Self {
component_id,
dc_destination_id,
ac_destination_id
ac_destination_id,
}
}
}

0 comments on commit f14ed75

Please sign in to comment.