michael@0: // MSBFDecoder.h michael@0: // the Most Significant Bit of byte is First michael@0: michael@0: #ifndef __STREAM_MSBFDECODER_H michael@0: #define __STREAM_MSBFDECODER_H michael@0: michael@0: #include "../../Common/Types.h" michael@0: #include "../IStream.h" michael@0: michael@0: namespace NStream { michael@0: namespace NMSBF { michael@0: michael@0: const int kNumBigValueBits = 8 * 4; michael@0: const int kNumValueBytes = 3; michael@0: const int kNumValueBits = 8 * kNumValueBytes; michael@0: michael@0: const UInt32 kMask = (1 << kNumValueBits) - 1; michael@0: michael@0: template michael@0: class CDecoder michael@0: { michael@0: UInt32 m_BitPos; michael@0: UInt32 m_Value; michael@0: public: michael@0: TInByte m_Stream; michael@0: bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } michael@0: void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);} michael@0: void ReleaseStream() { m_Stream.ReleaseStream();} michael@0: michael@0: void Init() michael@0: { michael@0: m_Stream.Init(); michael@0: m_BitPos = kNumBigValueBits; michael@0: Normalize(); michael@0: } michael@0: michael@0: UInt64 GetProcessedSize() const michael@0: { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; } michael@0: UInt32 GetBitPosition() const { return (m_BitPos & 7); } michael@0: michael@0: void Normalize() michael@0: { michael@0: for (;m_BitPos >= 8; m_BitPos -= 8) michael@0: m_Value = (m_Value << 8) | m_Stream.ReadByte(); michael@0: } michael@0: michael@0: UInt32 GetValue(UInt32 numBits) const michael@0: { michael@0: // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits); michael@0: return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits); michael@0: } michael@0: michael@0: void MovePos(UInt32 numBits) michael@0: { michael@0: m_BitPos += numBits; michael@0: Normalize(); michael@0: } michael@0: michael@0: UInt32 ReadBits(UInt32 numBits) michael@0: { michael@0: UInt32 res = GetValue(numBits); michael@0: MovePos(numBits); michael@0: return res; michael@0: } michael@0: }; michael@0: michael@0: }} michael@0: michael@0: #endif