type CodebookResult<T> = Result<T, CodebookError>;
pub struct FullCodebookDesc<S> {
- code: u32,
- bits: u8,
- sym: S,
+ pub code: u32,
+ pub bits: u8,
+ pub sym: S,
}
pub struct ShortCodebookDesc {
- code: u32,
- bits: u8,
+ pub code: u32,
+ pub bits: u8,
}
pub trait CodebookDescReader<S> {
let fill_len = lut_bits - bits;
let fill_size = 1 << fill_len;
let fill_code = code << (lut_bits - bits);
- let lut_value = (symidx << 8) | (bits as u32);
+ let lut_value = (symidx << 8) | u32::from(bits);
for j in 0..fill_size {
let idx = (fill_code + j) as usize;
table[idx + off] = lut_value;
}
} else {
let idx = (code as usize) + off;
- table[idx] = (symidx << 8) | 0x80 | (bits as u32);
+ table[idx] = (symidx << 8) | 0x80 | u32::from(bits);
}
}
let step = lut_bits - fill_len;
for j in 0..fill_size {
let idx = (fill_code + (j << step)) as usize;
- table[idx + off] = (symidx << 8) | (bits as u32);
+ table[idx + off] = (symidx << 8) | u32::from(bits);
}
} else {
let idx = (code as usize) + off;
- table[idx] = (symidx << 8) | 0x80 | (bits as u32);
+ table[idx] = (symidx << 8) | 0x80 | u32::from(bits);
}
}
type EscapeCodes = HashMap<u32, CodeBucket>;
fn add_esc_code(cc: &mut EscapeCodes, key: u32, code: u32, bits: u8, idx: usize) {
- if !cc.contains_key(&key) { cc.insert(key, CodeBucket::new()); }
+ cc.entry(key).or_insert_with(CodeBucket::new);
let b = cc.get_mut(&key);
if let Some(bucket) = b {
- bucket.add_code(Code {code: code, bits: bits, idx: idx });
+ bucket.add_code(Code {code, bits, idx });
} else { panic!("no bucket when expected!"); }
}
sec_bucket.offset = new_off as usize;
}
- for (_, sec_bucket) in &escape_list {
+ for sec_bucket in escape_list.values() {
build_esc_lut(table, mode, sec_bucket)?;
}
for i in 0..cb.len() {
let bits = cb.bits(i);
if bits > 0 {
- nnz = nnz + 1;
+ nnz += 1;
if cb.code(i) >= (1 << bits) {
return Err(CodebookError::InvalidCodebook);
}
let cval = extract_esc_part(code, bits, MAX_LUT_BITS, mode);
add_esc_code(&mut escape_list, ckey, cval, bits - MAX_LUT_BITS, symidx);
}
- if bits > 0 { symidx = symidx + 1; }
+ if bits > 0 { symidx += 1; }
}
if maxbits == 0 { return Err(CodebookError::InvalidCodebook); }
}
}
}
- symidx = symidx + 1;
+ symidx += 1;
}
- for (_, bucket) in &escape_list {
+ for bucket in escape_list.values() {
build_esc_lut(&mut table, mode, &bucket)?;
}
}
}
- Ok(Codebook { table: table, syms: syms, lut_bits: maxbits })
+ Ok(Codebook { table, syms, lut_bits: maxbits })
}
}
if (bits as isize) > self.left() {
return Err(CodebookError::InvalidCode);
}
- let skip_bits = if esc { lut_bits as u32 } else { bits };
- if let Err(_) = self.skip(skip_bits as u32) {}
+ let skip_bits = if esc { u32::from(lut_bits) } else { bits };
+ self.skip(skip_bits as u32).unwrap();
lut_bits = bits as u8;
}
Ok(cb.syms[idx])
impl<S> FullCodebookDescReader<S> {
pub fn new(data: Vec<FullCodebookDesc<S>>) -> Self {
- FullCodebookDescReader { data: data }
+ FullCodebookDescReader { data }
}
}
impl ShortCodebookDescReader {
pub fn new(data: Vec<ShortCodebookDesc<>>) -> Self {
- ShortCodebookDescReader { data: data }
+ ShortCodebookDescReader { data }
}
}
fn len(&mut self) -> usize { self.data.len() }
}
-pub struct TableCodebookDescReader<CodeType:'static, IndexType:'static> {
- bits: &'static [u8],
- codes: &'static [CodeType],
+pub struct TableCodebookDescReader<'a, CodeType:'static, IndexType:'static> {
+ bits: &'a [u8],
+ codes: &'a [CodeType],
idx_map: fn(usize) -> IndexType,
}
-impl<'a, CodeType, IndexType> TableCodebookDescReader<CodeType, IndexType> {
- pub fn new(codes: &'static [CodeType], bits: &'static [u8], idx_map: fn(usize) -> IndexType) -> Self {
+impl<'a, CodeType, IndexType> TableCodebookDescReader<'a, CodeType, IndexType> {
+ pub fn new(codes: &'a [CodeType], bits: &'a [u8], idx_map: fn(usize) -> IndexType) -> Self {
Self { bits, codes, idx_map }
}
}
-impl<CodeType: Copy+Into<u32>, IndexType> CodebookDescReader<IndexType> for TableCodebookDescReader<CodeType, IndexType>
+impl<'a, CodeType: Copy+Into<u32>, IndexType> CodebookDescReader<IndexType> for TableCodebookDescReader<'a, CodeType, IndexType>
{
fn bits(&mut self, idx: usize) -> u8 { self.bits[idx] }
fn code(&mut self, idx: usize) -> u32 { self.codes[idx].into() }