This commit is contained in:
Igor Pavlov
2005-05-30 00:00:00 +00:00
committed by Kornel Lesiński
parent 8c1b5c7b7e
commit 3c510ba80b
926 changed files with 40559 additions and 23519 deletions

View File

@@ -1,6 +1,4 @@
// Stream/LSBFDecoder.h
#pragma once
// LSBFDecoder.h
#ifndef __STREAM_LSBFDECODER_H
#define __STREAM_LSBFDECODER_H
@@ -15,80 +13,113 @@ const int kNumBigValueBits = 8 * 4;
const int kNumValueBytes = 3;
const int kNumValueBits = 8 * kNumValueBytes;
const UINT32 kMask = (1 << kNumValueBits) - 1;
const UInt32 kMask = (1 << kNumValueBits) - 1;
extern BYTE kInvertTable[256];
extern Byte kInvertTable[256];
// the Least Significant Bit of byte is First
template<class TInByte>
class CDecoder
class CBaseDecoder
{
UINT32 m_BitPos;
UINT32 m_Value;
UINT32 m_NormalValue;
protected:
UInt32 m_BitPos;
UInt32 m_Value;
TInByte m_Stream;
public:
UINT32 NumExtraBytes;
void Init(ISequentialInStream *inStream)
{
m_Stream.Init(inStream);
Init();
}
UInt32 NumExtraBytes;
bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); }
void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream); }
void ReleaseStream() { m_Stream.ReleaseStream(); }
void Init()
{
m_Stream.Init();
m_BitPos = kNumBigValueBits;
m_NormalValue = 0;
m_Value = 0;
NumExtraBytes = 0;
}
/*
void ReleaseStream()
{
m_Stream.ReleaseStream();
}
*/
UINT64 GetProcessedSize() const
UInt64 GetProcessedSize() const
{ return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; }
UINT64 GetProcessedBitsSize() const
UInt64 GetProcessedBitsSize() const
{ return (m_Stream.GetProcessedSize() << 3) - (kNumBigValueBits - m_BitPos); }
UInt32 GetBitPosition() const { return (m_BitPos & 7); }
void Normalize()
{
for (;m_BitPos >= 8; m_BitPos -= 8)
{
BYTE b;
Byte b;
if (!m_Stream.ReadByte(b))
{
b = 0xFF; // check it
NumExtraBytes++;
m_NormalValue = (b << (kNumBigValueBits - m_BitPos)) | m_NormalValue;
m_Value = (m_Value << 8) | kInvertTable[b];
}
m_Value = (b << (kNumBigValueBits - m_BitPos)) | m_Value;
}
}
UINT32 GetValue(UINT32 numBits)
UInt32 ReadBits(UInt32 numBits)
{
Normalize();
return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits);
}
void MovePos(UINT32 numBits)
{
UInt32 res = m_Value & ((1 << numBits) - 1);
m_BitPos += numBits;
m_NormalValue >>= numBits;
}
UINT32 ReadBits(UINT32 numBits)
{
Normalize();
UINT32 res = m_NormalValue & ( (1 << numBits) - 1);
MovePos(numBits);
m_Value >>= numBits;
return res;
}
UINT32 GetBitPosition() const
bool ExtraBitsWereRead() const
{
return (m_BitPos & 7);
if (NumExtraBytes == 0)
return false;
return ((kNumBigValueBits - m_BitPos) < (NumExtraBytes << 3));
}
};
template<class TInByte>
class CDecoder: public CBaseDecoder<TInByte>
{
UInt32 m_NormalValue;
public:
void Init()
{
CBaseDecoder<TInByte>::Init();
m_NormalValue = 0;
}
void Normalize()
{
for (;this->m_BitPos >= 8; this->m_BitPos -= 8)
{
Byte b;
if (!this->m_Stream.ReadByte(b))
{
b = 0xFF; // check it
this->NumExtraBytes++;
}
m_NormalValue = (b << (kNumBigValueBits - this->m_BitPos)) | m_NormalValue;
this->m_Value = (this->m_Value << 8) | kInvertTable[b];
}
}
UInt32 GetValue(UInt32 numBits)
{
Normalize();
return ((this->m_Value >> (8 - this->m_BitPos)) & kMask) >> (kNumValueBits - numBits);
}
void MovePos(UInt32 numBits)
{
this->m_BitPos += numBits;
m_NormalValue >>= numBits;
}
UInt32 ReadBits(UInt32 numBits)
{
Normalize();
UInt32 res = m_NormalValue & ( (1 << numBits) - 1);
MovePos(numBits);
return res;
}
};
}}