//! ShortCodebookDesc { code: 0b01, bits: 2 },
//! ShortCodebookDesc { code: 0b1, bits: 1 });
//! let mut cr = ShortCodebookDescReader::new(cb_desc);
-//! let cb = Codebook::new(&mut cr, CodebookMode::LSB).unwrap();
+//! let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
//! ```
//!
//! Create a codebook using more flexible [`TableCodebookDescReader`] approach.
///
/// [`ShortCodebookDescReader`]: ./struct.ShortCodebookDescReader.html
/// [`TableCodebookDescReader`]: ./struct.TableCodebookDescReader.html
+#[allow(clippy::len_without_is_empty)]
pub trait CodebookDescReader<S> {
/// Returns the codeword length for the provided index.
fn bits(&mut self, idx: usize) -> u8;
/// The codebook structure for code reading.
#[allow(dead_code)]
pub struct Codebook<S> {
- table: Vec<u32>,
- syms: Vec<S>,
- lut_bits: u8,
+ pub table: Vec<u32>,
+ pub syms: Vec<S>,
+ pub lut_bits: u8,
}
/// Trait allowing bitreader to use codebook for decoding bit sequences.
fn read_cb(&mut self, cb: &Codebook<S>) -> CodebookResult<S>;
}
-const TABLE_FILL_VALUE: u32 = 0x7F;
+pub const TABLE_FILL_VALUE: u32 = 0x7F;
const MAX_LUT_BITS: u8 = 10;
fn fill_lut_msb(table: &mut Vec<u32>, off: usize,
impl<S: Copy> Codebook<S> {
/// Constructs a new `Codebook` instance using provided codebook description and mode.
- pub fn new(cb: &mut CodebookDescReader<S>, mode: CodebookMode) -> CodebookResult<Self> {
+ pub fn new(cb: &mut dyn CodebookDescReader<S>, mode: CodebookMode) -> CodebookResult<Self> {
let mut maxbits = 0;
let mut nnz = 0;
let mut escape_list: EscapeCodes = HashMap::new();
let bits = cb.table[lut_idx] & 0x7F;
esc = (cb.table[lut_idx] & 0x80) != 0;
idx = (cb.table[lut_idx] >> 8) as usize;
- if (bits as isize) > self.left() {
+ let skip_bits = if esc { u32::from(lut_bits) } else { bits };
+ if (skip_bits as isize) > self.left() {
return Err(CodebookError::InvalidCode);
}
- let skip_bits = if esc { u32::from(lut_bits) } else { bits };
self.skip(skip_bits as u32).unwrap();
lut_bits = bits as u8;
}