}
}
+ #[inline(always)]
fn refill(&mut self) -> BitReaderResult<()> {
if self.pos >= self.end { return Err(BitstreamEnd) }
while self.bits <= 32 {
Ok(())
}
+ #[inline(always)]
fn read_cache(&mut self, nbits: u8) -> u32 {
let res = match self.mode {
BitReaderMode::LE => ((1u64 << nbits) - 1) & self.cache,
res as i32
}
+ #[inline(always)]
fn skip_cache(&mut self, nbits: u8) {
match self.mode {
BitReaderMode::LE => self.cache >>= nbits,
self.bits -= nbits;
}
+ #[inline(always)]
fn reset_cache(&mut self) {
self.bits = 0;
self.cache = 0;
}
+ #[inline(always)]
pub fn read(&mut self, nbits: u8) -> BitReaderResult<u32> {
if nbits == 0 { return Ok(0) }
if nbits > 32 { return Err(TooManyBitsRequested) }
Ok(res)
}
+ #[inline(always)]
pub fn read_bool(&mut self) -> BitReaderResult<bool> {
if self.bits < 1 {
if let Err(err) = self.refill() { return Err(err) }
Ok(res == 1)
}
+ #[inline(always)]
pub fn peek(&mut self, nbits: u8) -> u32 {
if nbits > 32 { return 0 }
if self.bits < nbits { let _ = self.refill(); }
self.read_cache(nbits)
}
+ #[inline(always)]
pub fn skip(&mut self, nbits: u32) -> BitReaderResult<()> {
if self.bits as u32 >= nbits {
self.skip_cache(nbits as u8);
self.pos = ((nbits / 32) * 4) as usize;
self.skip(nbits & 0x1F)
}
+
+ pub fn align(&mut self) {
+ let pos = self.bits & 7;
+ if pos != 0 {
+ self.skip_cache(pos);
+ }
+ }
+}
+
+pub fn reverse_bits(inval: u32, len: u8) -> u32 {
+ if len == 0 { return 0; }
+ const REV_TAB: [u8; 16] = [
+ 0b0000, 0b1000, 0b0100, 0b1100, 0b0010, 0b1010, 0b0110, 0b1110,
+ 0b0001, 0b1001, 0b0101, 0b1101, 0b0011, 0b1011, 0b0111, 0b1111,
+ ];
+
+ let mut ret = 0;
+ let mut val = inval;
+ for _ in 0..8 {
+ ret = (ret << 4) | (REV_TAB[(val & 0xF) as usize] as u32);
+ val = val >> 4;
+ }
+ ret >> (32 - len)
}
#[cfg(test)]