diff --git a/src/memory/bitstreamcache/bitstreamcache/high_in_low_out/mod.rs b/src/memory/bitstreamcache/bitstreamcache/high_in_low_out/mod.rs index 54d05c7..ed7ef7b 100644 --- a/src/memory/bitstreamcache/bitstreamcache/high_in_low_out/mod.rs +++ b/src/memory/bitstreamcache/bitstreamcache/high_in_low_out/mod.rs @@ -28,6 +28,11 @@ impl BitStreamCache for BitStreamCacheHighInLowOut { } } + #[inline] + fn size(&self) -> usize { + Self::SIZE + } + #[inline] fn fill_level(&self) -> usize { self.fill_level as usize diff --git a/src/memory/bitstreamcache/bitstreamcache/low_in_high_out/mod.rs b/src/memory/bitstreamcache/bitstreamcache/low_in_high_out/mod.rs index f019f49..9c89cee 100644 --- a/src/memory/bitstreamcache/bitstreamcache/low_in_high_out/mod.rs +++ b/src/memory/bitstreamcache/bitstreamcache/low_in_high_out/mod.rs @@ -28,6 +28,11 @@ impl BitStreamCache for BitStreamCacheLowInHighOut { } } + #[inline] + fn size(&self) -> usize { + Self::SIZE + } + #[inline] fn fill_level(&self) -> usize { self.fill_level as usize diff --git a/src/memory/bitstreamcache/bitstreamcache/mod.rs b/src/memory/bitstreamcache/bitstreamcache/mod.rs index e53ce51..2bae1a5 100644 --- a/src/memory/bitstreamcache/bitstreamcache/mod.rs +++ b/src/memory/bitstreamcache/bitstreamcache/mod.rs @@ -36,6 +36,7 @@ pub trait BitStreamCache { #[must_use] fn new() -> Self; + fn size(&self) -> usize; fn fill_level(&self) -> usize; fn push(&mut self, bits: Self::Storage, count: usize); diff --git a/src/memory/bitvacuumer/bitvacuumer/mod.rs b/src/memory/bitvacuumer/bitvacuumer/mod.rs index 4baf6ee..b907e5e 100644 --- a/src/memory/bitvacuumer/bitvacuumer/mod.rs +++ b/src/memory/bitvacuumer/bitvacuumer/mod.rs @@ -2,10 +2,12 @@ use core::marker::PhantomData; use rawspeed_common::common::Bitwidth; use rawspeed_memory_bitstream::bitstream::BitOrderTrait; use rawspeed_memory_bitstream::bitstream::BitStreamTraits; -use rawspeed_memory_bitstreamcache::bitstreamcache::BitStreamCache; use rawspeed_memory_endianness::endianness::SwapBytes; use rawspeed_memory_endianness::endianness::get_host_endianness; +#[allow(clippy::wildcard_imports)] +use rawspeed_memory_bitstreamcache::bitstreamcache::*; + pub trait BitVacuumerDefaultDrainImpl { fn drain_impl(&mut self) -> std::io::Result<()>; } @@ -21,17 +23,7 @@ pub struct BitVacuumerBase<'a, T, W> where T: BitOrderTrait + BitStreamTraits, W: std::io::Write, - u32: From - + Bitwidth - + From - + core::ops::Shl - + core::ops::ShlAssign - + core::ops::BitOrAssign, T::StreamFlow: BitStreamCache, - T::ChunkType: Bitwidth - + SwapBytes - + TryFrom<::Storage>, - ::Storage: From, { cache: T::StreamFlow, writer: &'a mut W, @@ -42,33 +34,34 @@ impl BitVacuumerDefaultDrainImpl for BitVacuumerBase<'_, T, W> where T: BitOrderTrait + BitStreamTraits, W: std::io::Write, - u32: From - + Bitwidth - + From - + core::ops::Shl - + core::ops::ShlAssign - + core::ops::BitOrAssign, T::StreamFlow: BitStreamCache, T::ChunkType: Bitwidth - + SwapBytes - + TryFrom<::Storage>, - ::Storage: From, + + TryFrom<::Storage> + + SwapBytes, + u32: From, { fn drain_impl(&mut self) -> std::io::Result<()> { - type WritebackCache = u32; - - assert!(self.cache.fill_level() >= WritebackCache::BITWIDTH); + let mut cache: BitStreamCacheBase<_, u32> = { + #[cfg(target_endian = "little")] + { + BitStreamCacheHighInLowOut::::new() + } + #[cfg(not(target_endian = "little"))] + { + BitStreamCacheLowInHighOut::::new() + } + }; + + assert!(self.cache.fill_level() >= cache.size()); let stream_chunk_bitwidth: usize = T::ChunkType::BITWIDTH; - assert!(WritebackCache::BITWIDTH >= stream_chunk_bitwidth); - assert!(WritebackCache::BITWIDTH.is_multiple_of(stream_chunk_bitwidth)); - let num_chunks_needed: usize = - WritebackCache::BITWIDTH / stream_chunk_bitwidth; + assert!(cache.size() >= stream_chunk_bitwidth); + assert!(cache.size().is_multiple_of(stream_chunk_bitwidth)); + let num_chunks_needed: usize = cache.size() / stream_chunk_bitwidth; assert!(num_chunks_needed >= 1); - let mut cache: WritebackCache = Default::default(); - for i in 0..num_chunks_needed { + for _i in 0..num_chunks_needed { let Ok(chunk) = ::try_from( self.cache.peek(stream_chunk_bitwidth), ) else { @@ -77,27 +70,9 @@ where self.cache.skip(stream_chunk_bitwidth); let chunk = chunk .get_byte_swapped(T::CHUNK_ENDIANNESS != get_host_endianness()); - let chunk: WritebackCache = chunk.into(); - let chunk: WritebackCache = { - #[cfg(target_endian = "little")] - { - chunk << (i * stream_chunk_bitwidth) - } - #[cfg(not(target_endian = "little"))] - { - #[allow(clippy::no_effect_underscore_binding)] - { - let _i = i; // i is only used in little-endian block. - } - if num_chunks_needed != 1 { - cache <<= stream_chunk_bitwidth; - } - chunk - } - }; - cache |= chunk; + cache.push(chunk.into(), stream_chunk_bitwidth); } - let bytes = cache.to_ne_bytes(); + let bytes = cache.peek(cache.size()).to_ne_bytes(); self.writer.write_all(&bytes) } } @@ -106,17 +81,11 @@ impl BitVacuumerDrainImpl for BitVacuumerBase<'_, T, W> where T: BitOrderTrait + BitStreamTraits + BitVacuumerUseDefaultDrainImpl, W: std::io::Write, - u32: From - + Bitwidth - + From - + core::ops::Shl - + core::ops::ShlAssign - + core::ops::BitOrAssign, T::StreamFlow: BitStreamCache, T::ChunkType: Bitwidth - + SwapBytes - + TryFrom<::Storage>, - ::Storage: From, + + TryFrom<::Storage> + + SwapBytes, + u32: From, { fn drain_impl(&mut self) -> std::io::Result<()> { BitVacuumerDefaultDrainImpl::drain_impl(self) @@ -128,17 +97,12 @@ where T: BitOrderTrait + BitStreamTraits, Self: BitVacuumerDrainImpl, W: std::io::Write, - u32: From - + Bitwidth - + From - + core::ops::Shl - + core::ops::ShlAssign - + core::ops::BitOrAssign, T::StreamFlow: BitStreamCache, - ::Storage: From + From, T::ChunkType: Bitwidth - + SwapBytes - + TryFrom<::Storage>, + + TryFrom<::Storage> + + SwapBytes, + u32: From, + ::Storage: From, { #[allow(dead_code)] pub fn new(writer: &'a mut W) -> Self @@ -195,17 +159,7 @@ impl Drop for BitVacuumerBase<'_, T, W> where T: BitOrderTrait + BitStreamTraits, W: std::io::Write, - u32: From - + Bitwidth - + From - + core::ops::Shl - + core::ops::ShlAssign - + core::ops::BitOrAssign, T::StreamFlow: BitStreamCache, - T::ChunkType: Bitwidth - + SwapBytes - + TryFrom<::Storage>, - ::Storage: From, { fn drop(&mut self) { const ERR: &str = "Unrecoverable Error: trying to drop \