1 // BitlDecoder.h -- the Least Significant Bit of byte is First
\r
3 #ifndef __BITL_DECODER_H
\r
4 #define __BITL_DECODER_H
\r
6 #include "../IStream.h"
\r
10 const unsigned kNumBigValueBits = 8 * 4;
\r
11 const unsigned kNumValueBytes = 3;
\r
12 const unsigned kNumValueBits = 8 * kNumValueBytes;
\r
14 const UInt32 kMask = (1 << kNumValueBits) - 1;
\r
16 extern Byte kInvertTable[256];
\r
18 template<class TInByte>
\r
26 UInt32 NumExtraBytes;
\r
27 bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); }
\r
28 void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream); }
\r
29 void ReleaseStream() { m_Stream.ReleaseStream(); }
\r
33 m_BitPos = kNumBigValueBits;
\r
37 UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() + NumExtraBytes - (kNumBigValueBits - m_BitPos) / 8; }
\r
41 for (; m_BitPos >= 8; m_BitPos -= 8)
\r
44 if (!m_Stream.ReadByte(b))
\r
46 b = 0xFF; // check it
\r
49 m_Value = (b << (kNumBigValueBits - m_BitPos)) | m_Value;
\r
53 UInt32 ReadBits(unsigned numBits)
\r
56 UInt32 res = m_Value & ((1 << numBits) - 1);
\r
57 m_BitPos += numBits;
\r
58 m_Value >>= numBits;
\r
62 bool ExtraBitsWereRead() const
\r
64 if (NumExtraBytes == 0)
\r
66 return ((UInt32)(kNumBigValueBits - m_BitPos) < (NumExtraBytes << 3));
\r
70 template<class TInByte>
\r
71 class CDecoder: public CBaseDecoder<TInByte>
\r
73 UInt32 m_NormalValue;
\r
78 CBaseDecoder<TInByte>::Init();
\r
84 for (; this->m_BitPos >= 8; this->m_BitPos -= 8)
\r
87 if (!this->m_Stream.ReadByte(b))
\r
89 b = 0xFF; // check it
\r
90 this->NumExtraBytes++;
\r
92 m_NormalValue = (b << (kNumBigValueBits - this->m_BitPos)) | m_NormalValue;
\r
93 this->m_Value = (this->m_Value << 8) | kInvertTable[b];
\r
97 UInt32 GetValue(unsigned numBits)
\r
100 return ((this->m_Value >> (8 - this->m_BitPos)) & kMask) >> (kNumValueBits - numBits);
\r
103 void MovePos(unsigned numBits)
\r
105 this->m_BitPos += numBits;
\r
106 m_NormalValue >>= numBits;
\r
109 UInt32 ReadBits(unsigned numBits)
\r
112 UInt32 res = m_NormalValue & ((1 << numBits) - 1);
\r
117 void AlignToByte() { MovePos((32 - this->m_BitPos) & 7); }
\r
121 if (this->m_BitPos == kNumBigValueBits)
\r
124 if (!this->m_Stream.ReadByte(b))
\r
127 this->NumExtraBytes++;
\r
132 Byte b = (Byte)(m_NormalValue & 0xFF);
\r