mirror of
https://github.com/exoticorn/upkr.git
synced 2026-01-20 19:46:42 +01:00
implement optional parity contexts
This commit is contained in:
52
src/lz.rs
52
src/lz.rs
@@ -9,41 +9,49 @@ pub enum Op {
|
||||
|
||||
impl Op {
|
||||
pub fn encode(&self, coder: &mut dyn EntropyCoder, state: &mut CoderState) {
|
||||
let literal_base = state.pos % state.parity_contexts * 256;
|
||||
match self {
|
||||
&Op::Literal(lit) => {
|
||||
encode_bit(coder, state, 0, false);
|
||||
encode_bit(coder, state, literal_base, false);
|
||||
let mut context_index = 1;
|
||||
for i in (0..8).rev() {
|
||||
let bit = (lit >> i) & 1 != 0;
|
||||
encode_bit(coder, state, context_index, bit);
|
||||
encode_bit(coder, state, literal_base + context_index, bit);
|
||||
context_index = (context_index << 1) | bit as usize;
|
||||
}
|
||||
state.prev_was_match = false;
|
||||
state.pos += 1;
|
||||
}
|
||||
&Op::Match { offset, len } => {
|
||||
encode_bit(coder, state, 0, true);
|
||||
encode_bit(coder, state, literal_base, true);
|
||||
if !state.prev_was_match {
|
||||
encode_bit(coder, state, 256, offset != state.last_offset);
|
||||
encode_bit(
|
||||
coder,
|
||||
state,
|
||||
256 * state.parity_contexts,
|
||||
offset != state.last_offset,
|
||||
);
|
||||
} else {
|
||||
assert!(offset != state.last_offset);
|
||||
}
|
||||
if offset != state.last_offset {
|
||||
encode_length(coder, state, 257, offset + 1);
|
||||
encode_length(coder, state, 256 * state.parity_contexts + 1, offset + 1);
|
||||
state.last_offset = offset;
|
||||
}
|
||||
encode_length(coder, state, 257 + 64, len);
|
||||
encode_length(coder, state, 256 * state.parity_contexts + 65, len);
|
||||
state.prev_was_match = true;
|
||||
state.pos += len as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_eof(coder: &mut dyn EntropyCoder, state: &mut CoderState) {
|
||||
encode_bit(coder, state, 0, true);
|
||||
encode_bit(coder, state, state.pos % state.parity_contexts * 256, true);
|
||||
if !state.prev_was_match {
|
||||
encode_bit(coder, state, 256, true);
|
||||
encode_bit(coder, state, 256 * state.parity_contexts, true);
|
||||
}
|
||||
encode_length(coder, state, 257, 1);
|
||||
encode_length(coder, state, 256 * state.parity_contexts + 1, 1);
|
||||
}
|
||||
|
||||
fn encode_bit(
|
||||
@@ -76,16 +84,20 @@ fn encode_length(
|
||||
#[derive(Clone)]
|
||||
pub struct CoderState {
|
||||
contexts: ContextState,
|
||||
parity_contexts: usize,
|
||||
last_offset: u32,
|
||||
prev_was_match: bool,
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl CoderState {
|
||||
pub fn new() -> CoderState {
|
||||
pub fn new(parity_contexts: usize) -> CoderState {
|
||||
CoderState {
|
||||
contexts: ContextState::new(1 + 255 + 1 + 64 + 64),
|
||||
contexts: ContextState::new((1 + 255) * parity_contexts + 1 + 64 + 64),
|
||||
last_offset: 0,
|
||||
parity_contexts,
|
||||
prev_was_match: false,
|
||||
pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,9 +106,9 @@ impl CoderState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unpack(packed_data: &[u8], use_bitstream: bool) -> Vec<u8> {
|
||||
pub fn unpack(packed_data: &[u8], use_bitstream: bool, parity_contexts: usize) -> Vec<u8> {
|
||||
let mut decoder = RansDecoder::new(packed_data, use_bitstream);
|
||||
let mut contexts = ContextState::new(1 + 255 + 1 + 64 + 64);
|
||||
let mut contexts = ContextState::new((1 + 255) * parity_contexts + 1 + 64 + 64);
|
||||
let mut result = vec![];
|
||||
let mut offset = 0;
|
||||
let mut prev_was_match = false;
|
||||
@@ -119,14 +131,17 @@ pub fn unpack(packed_data: &[u8], use_bitstream: bool) -> Vec<u8> {
|
||||
}
|
||||
|
||||
loop {
|
||||
if decoder.decode_with_context(&mut contexts.context_mut(0)) {
|
||||
if prev_was_match || decoder.decode_with_context(&mut contexts.context_mut(256)) {
|
||||
offset = decode_length(&mut decoder, &mut contexts, 257) - 1;
|
||||
let literal_base = result.len() % parity_contexts * 256;
|
||||
if decoder.decode_with_context(&mut contexts.context_mut(literal_base)) {
|
||||
if prev_was_match
|
||||
|| decoder.decode_with_context(&mut contexts.context_mut(256 * parity_contexts))
|
||||
{
|
||||
offset = decode_length(&mut decoder, &mut contexts, 256 * parity_contexts + 1) - 1;
|
||||
if offset == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let length = decode_length(&mut decoder, &mut contexts, 257 + 64);
|
||||
let length = decode_length(&mut decoder, &mut contexts, 256 * parity_contexts + 65);
|
||||
for _ in 0..length {
|
||||
result.push(result[result.len() - offset]);
|
||||
}
|
||||
@@ -135,7 +150,8 @@ pub fn unpack(packed_data: &[u8], use_bitstream: bool) -> Vec<u8> {
|
||||
let mut context_index = 1;
|
||||
let mut byte = 0;
|
||||
for i in (0..8).rev() {
|
||||
let bit = decoder.decode_with_context(&mut contexts.context_mut(context_index));
|
||||
let bit = decoder
|
||||
.decode_with_context(&mut contexts.context_mut(literal_base + context_index));
|
||||
context_index = (context_index << 1) | bit as usize;
|
||||
byte |= (bit as u8) << i;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user