fixed bug #1005296 - NSIS build error on Linux with g++ 3.4.0
- upgraded to the latest LZMA SDK git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@3637 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
parent
98caca8be1
commit
ec6957f356
52 changed files with 1715 additions and 2540 deletions
|
@ -1,48 +1,14 @@
|
|||
// BinTree.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
// #ifndef __BINTREE_H
|
||||
// #define __BINTREE_H
|
||||
|
||||
#include "../LZInWindow.h"
|
||||
// #include "Common/Types.h"
|
||||
// #include "Windows/Defs.h"
|
||||
|
||||
namespace BT_NAMESPACE {
|
||||
|
||||
// #define __USE_3_BYTES
|
||||
|
||||
#ifdef __USE_3_BYTES
|
||||
|
||||
//#pragma pack(push, PragmaBinTree, 1)
|
||||
#pragma pack(1)
|
||||
|
||||
struct CIndex
|
||||
{
|
||||
BYTE Data[3];
|
||||
CIndex(){}
|
||||
CIndex(UINT32 value)
|
||||
{
|
||||
Data[0] = value & 0xFF;
|
||||
Data[1] = (value >> 8) & 0xFF;
|
||||
Data[2] = (value >> 16) & 0xFF;
|
||||
}
|
||||
operator UINT32() const { return (*((const UINT32 *)Data)) & 0xFFFFFF; }
|
||||
};
|
||||
const UINT32 kMaxValForNormalize = CIndex(-1);
|
||||
|
||||
//#pragma pack(pop, PragmaBinTree)
|
||||
#pragma pack()
|
||||
|
||||
#else
|
||||
|
||||
typedef UINT32 CIndex;
|
||||
const UINT32 kMaxValForNormalize = (UINT32(1) << 31) - 1;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
typedef UInt32 CIndex;
|
||||
const UInt32 kMaxValForNormalize = (UInt32(1) << 31) - 1;
|
||||
|
||||
// #define HASH_ARRAY_2
|
||||
|
||||
|
@ -56,26 +22,31 @@ const UINT32 kMaxValForNormalize = (UINT32(1) << 31) - 1;
|
|||
|
||||
// #endif
|
||||
|
||||
//#pragma pack(push, PragmaBinTreePair, 1)
|
||||
#pragma pack(1)
|
||||
// #pragma pack(push, 1)
|
||||
|
||||
struct CPair
|
||||
{
|
||||
CIndex Left;
|
||||
CIndex Right;
|
||||
};
|
||||
|
||||
// #pragma pack(pop)
|
||||
//#pragma pack(pop, PragmaBinTreePair)
|
||||
#pragma pack()
|
||||
/*
|
||||
const int kNumBundleBits = 2;
|
||||
const UInt32 kNumPairsInBundle = 1 << kNumBundleBits;
|
||||
const UInt32 kBundleMask = kNumPairsInBundle - 1;
|
||||
const UInt32 kNumBundleBytes = kNumPairsInBundle * sizeof(CPair);
|
||||
|
||||
struct CBundle
|
||||
{
|
||||
CPair Pairs[kNumPairsInBundle];
|
||||
Byte Bytes[kNumBundleBytes];
|
||||
};
|
||||
*/
|
||||
|
||||
class CInTree: public CLZInWindow
|
||||
{
|
||||
UINT32 _cyclicBufferPos;
|
||||
UINT32 _cyclicBufferSize;
|
||||
UINT32 _historySize;
|
||||
UINT32 _matchMaxLen;
|
||||
UInt32 _cyclicBufferPos;
|
||||
UInt32 _cyclicBufferSize;
|
||||
UInt32 _historySize;
|
||||
UInt32 _matchMaxLen;
|
||||
|
||||
CIndex *_hash;
|
||||
|
||||
|
@ -86,22 +57,23 @@ class CInTree: public CLZInWindow
|
|||
#endif
|
||||
#endif
|
||||
|
||||
// CBundle *_son;
|
||||
CPair *_son;
|
||||
|
||||
UINT32 _cutValue;
|
||||
UInt32 _cutValue;
|
||||
|
||||
void NormalizeLinks(CIndex *array, UINT32 numItems, UINT32 subValue);
|
||||
void NormalizeLinks(CIndex *items, UInt32 numItems, UInt32 subValue);
|
||||
void Normalize();
|
||||
void FreeMemory();
|
||||
|
||||
public:
|
||||
CInTree();
|
||||
virtual ~CInTree();
|
||||
HRESULT Create(UINT32 sizeHistory, UINT32 keepAddBufferBefore, UINT32 matchMaxLen,
|
||||
UINT32 keepAddBufferAfter, UINT32 sizeReserv = (1<<17));
|
||||
~CInTree();
|
||||
HRESULT Create(UInt32 sizeHistory, UInt32 keepAddBufferBefore, UInt32 matchMaxLen,
|
||||
UInt32 keepAddBufferAfter, UInt32 sizeReserv = (1<<17));
|
||||
HRESULT Init(ISequentialInStream *stream);
|
||||
void SetCutValue(UINT32 cutValue) { _cutValue = cutValue; }
|
||||
UINT32 GetLongestMatch(UINT32 *distances);
|
||||
void SetCutValue(UInt32 cutValue) { _cutValue = cutValue; }
|
||||
UInt32 GetLongestMatch(UInt32 *distances);
|
||||
void DummyLongestMatch();
|
||||
HRESULT MovePos()
|
||||
{
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// BinTree2.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __BINTREE2__H
|
||||
#define __BINTREE2__H
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// BinTree3.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __BINTREE3__H
|
||||
#define __BINTREE3__H
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// BinTree3Z.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __BINTREE3Z__H
|
||||
#define __BINTREE3Z__H
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// BinTree3ZMain.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __BINTREE3ZMAIN__H
|
||||
#define __BINTREE3ZMAIN__H
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// BinTree4.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __BINTREE4__H
|
||||
#define __BINTREE4__H
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// BinTree4b.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __BINTREE4B__H
|
||||
#define __BINTREE4B__H
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// BinTreeMF.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
// #ifndef __BINTREEMF_H
|
||||
// #define __BINTREEMF_H
|
||||
|
||||
|
@ -39,13 +37,8 @@ namespace BT_NAMESPACE {
|
|||
#undef kIDUse3BytesByte
|
||||
#undef kIDUse3BytesString
|
||||
|
||||
#ifdef __USE_3_BYTES
|
||||
#define kIDUse3BytesByte 0x80
|
||||
#define kIDUse3BytesString TEXT("T")
|
||||
#else
|
||||
#define kIDUse3BytesByte 0x00
|
||||
#define kIDUse3BytesString TEXT("")
|
||||
#endif
|
||||
#define kIDUse3BytesByte 0x00
|
||||
#define kIDUse3BytesString TEXT("")
|
||||
|
||||
// #undef kIDStringFull
|
||||
|
||||
|
@ -62,7 +55,6 @@ class CInTree2: public CInTree
|
|||
virtual void BeforeMoveBlock();
|
||||
virtual void AfterMoveBlock();
|
||||
public:
|
||||
virtual ~CInTree2() {}
|
||||
void SetCallback(IMatchFinderCallback *callback)
|
||||
{
|
||||
_callback = callback;
|
||||
|
@ -79,28 +71,28 @@ class CMatchFinderBinTree:
|
|||
STDMETHOD(Init)(ISequentialInStream *stream);
|
||||
STDMETHOD_(void, ReleaseStream)();
|
||||
STDMETHOD(MovePos)();
|
||||
STDMETHOD_(BYTE, GetIndexByte)(UINT32 index);
|
||||
STDMETHOD_(UINT32, GetMatchLen)(UINT32 index, UINT32 back, UINT32 limit);
|
||||
STDMETHOD_(UINT32, GetNumAvailableBytes)();
|
||||
STDMETHOD_(const BYTE *, GetPointerToCurrentPos)();
|
||||
STDMETHOD(Create)(UINT32 sizeHistory,
|
||||
UINT32 keepAddBufferBefore, UINT32 matchMaxLen,
|
||||
UINT32 keepAddBufferAfter);
|
||||
STDMETHOD_(UINT32, GetLongestMatch)(UINT32 *distances);
|
||||
STDMETHOD_(Byte, GetIndexByte)(Int32 index);
|
||||
STDMETHOD_(UInt32, GetMatchLen)(Int32 index, UInt32 back, UInt32 limit);
|
||||
STDMETHOD_(UInt32, GetNumAvailableBytes)();
|
||||
STDMETHOD_(const Byte *, GetPointerToCurrentPos)();
|
||||
STDMETHOD(Create)(UInt32 sizeHistory,
|
||||
UInt32 keepAddBufferBefore, UInt32 matchMaxLen,
|
||||
UInt32 keepAddBufferAfter);
|
||||
STDMETHOD_(UInt32, GetLongestMatch)(UInt32 *distances);
|
||||
STDMETHOD_(void, DummyLongestMatch)();
|
||||
|
||||
// IMatchFinderSetCallback
|
||||
STDMETHOD(SetCallback)(IMatchFinderCallback *callback);
|
||||
|
||||
private:
|
||||
// UINT32 m_WindowReservSize;
|
||||
// UInt32 m_WindowReservSize;
|
||||
CInTree2 _matchFinder;
|
||||
public:
|
||||
// CMatchFinderBinTree(): m_WindowReservSize((1 << 19) + 256) {};
|
||||
void SetCutValue(UINT32 cutValue)
|
||||
void SetCutValue(UInt32 cutValue)
|
||||
{ _matchFinder.SetCutValue(cutValue); }
|
||||
/*
|
||||
void SetWindowReservSize(UINT32 reservWindowSize)
|
||||
void SetWindowReservSize(UInt32 reservWindowSize)
|
||||
{ m_WindowReservSize = reservWindowSize; }
|
||||
*/
|
||||
virtual ~CMatchFinderBinTree() {}
|
||||
|
|
|
@ -32,40 +32,42 @@ STDMETHODIMP_(void) CMatchFinderBinTree::ReleaseStream()
|
|||
STDMETHODIMP CMatchFinderBinTree::MovePos()
|
||||
{ return _matchFinder.MovePos(); }
|
||||
|
||||
STDMETHODIMP_(BYTE) CMatchFinderBinTree::GetIndexByte(UINT32 index)
|
||||
STDMETHODIMP_(Byte) CMatchFinderBinTree::GetIndexByte(Int32 index)
|
||||
{ return _matchFinder.GetIndexByte(index); }
|
||||
|
||||
STDMETHODIMP_(UINT32) CMatchFinderBinTree::GetMatchLen(UINT32 index,
|
||||
UINT32 back, UINT32 limit)
|
||||
STDMETHODIMP_(UInt32) CMatchFinderBinTree::GetMatchLen(Int32 index,
|
||||
UInt32 back, UInt32 limit)
|
||||
{ return _matchFinder.GetMatchLen(index, back, limit); }
|
||||
|
||||
STDMETHODIMP_(UINT32) CMatchFinderBinTree::GetNumAvailableBytes()
|
||||
STDMETHODIMP_(UInt32) CMatchFinderBinTree::GetNumAvailableBytes()
|
||||
{ return _matchFinder.GetNumAvailableBytes(); }
|
||||
|
||||
STDMETHODIMP CMatchFinderBinTree::Create(UINT32 sizeHistory,
|
||||
UINT32 keepAddBufferBefore, UINT32 matchMaxLen,
|
||||
UINT32 keepAddBufferAfter)
|
||||
STDMETHODIMP CMatchFinderBinTree::Create(UInt32 sizeHistory,
|
||||
UInt32 keepAddBufferBefore, UInt32 matchMaxLen,
|
||||
UInt32 keepAddBufferAfter)
|
||||
{
|
||||
UINT32 windowReservSize = (sizeHistory + keepAddBufferBefore +
|
||||
UInt32 windowReservSize = (sizeHistory + keepAddBufferBefore +
|
||||
matchMaxLen + keepAddBufferAfter) / 2 + 256;
|
||||
try
|
||||
// try
|
||||
{
|
||||
return _matchFinder.Create(sizeHistory, keepAddBufferBefore,
|
||||
matchMaxLen, keepAddBufferAfter, windowReservSize);
|
||||
}
|
||||
/*
|
||||
catch(...)
|
||||
{
|
||||
return E_OUTOFMEMORY;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
STDMETHODIMP_(UINT32) CMatchFinderBinTree::GetLongestMatch(UINT32 *distances)
|
||||
STDMETHODIMP_(UInt32) CMatchFinderBinTree::GetLongestMatch(UInt32 *distances)
|
||||
{ return _matchFinder.GetLongestMatch(distances); }
|
||||
|
||||
STDMETHODIMP_(void) CMatchFinderBinTree::DummyLongestMatch()
|
||||
{ _matchFinder.DummyLongestMatch(); }
|
||||
|
||||
STDMETHODIMP_(const BYTE *) CMatchFinderBinTree::GetPointerToCurrentPos()
|
||||
STDMETHODIMP_(const Byte *) CMatchFinderBinTree::GetPointerToCurrentPos()
|
||||
{
|
||||
return _matchFinder.GetPointerToCurrentPos();
|
||||
}
|
||||
|
|
|
@ -1,40 +1,36 @@
|
|||
// BinTreemain.h
|
||||
|
||||
// #include "StdAfx.h"
|
||||
|
||||
// #include "BinTree.h"
|
||||
// #include "Common/NewHandler.h"
|
||||
// BinTreeMain.h
|
||||
|
||||
#include "../../../../Common/Defs.h"
|
||||
#include "../../../../Common/CRC.h"
|
||||
#include "../../../../Common/Alloc.h"
|
||||
|
||||
namespace BT_NAMESPACE {
|
||||
|
||||
#ifdef HASH_ARRAY_2
|
||||
static const UINT32 kHash2Size = 1 << 10;
|
||||
static const UInt32 kHash2Size = 1 << 10;
|
||||
#ifdef HASH_ARRAY_3
|
||||
static const UINT32 kNumHashDirectBytes = 0;
|
||||
static const UINT32 kNumHashBytes = 4;
|
||||
static const UINT32 kHash3Size = 1 << 18;
|
||||
static const UInt32 kNumHashDirectBytes = 0;
|
||||
static const UInt32 kNumHashBytes = 4;
|
||||
static const UInt32 kHash3Size = 1 << 18;
|
||||
#ifdef HASH_BIG
|
||||
static const UINT32 kHashSize = 1 << 23;
|
||||
static const UInt32 kHashSize = 1 << 23;
|
||||
#else
|
||||
static const UINT32 kHashSize = 1 << 20;
|
||||
static const UInt32 kHashSize = 1 << 20;
|
||||
#endif
|
||||
#else
|
||||
static const UINT32 kNumHashDirectBytes = 3;
|
||||
static const UINT32 kNumHashBytes = 3;
|
||||
static const UINT32 kHashSize = 1 << (8 * kNumHashBytes);
|
||||
static const UInt32 kNumHashDirectBytes = 3;
|
||||
static const UInt32 kNumHashBytes = 3;
|
||||
static const UInt32 kHashSize = 1 << (8 * kNumHashBytes);
|
||||
#endif
|
||||
#else
|
||||
#ifdef HASH_ZIP
|
||||
static const UINT32 kNumHashDirectBytes = 0;
|
||||
static const UINT32 kNumHashBytes = 3;
|
||||
static const UINT32 kHashSize = 1 << 16;
|
||||
static const UInt32 kNumHashDirectBytes = 0;
|
||||
static const UInt32 kNumHashBytes = 3;
|
||||
static const UInt32 kHashSize = 1 << 16;
|
||||
#else
|
||||
static const UINT32 kNumHashDirectBytes = 2;
|
||||
static const UINT32 kNumHashBytes = 2;
|
||||
static const UINT32 kHashSize = 1 << (8 * kNumHashBytes);
|
||||
static const UInt32 kNumHashDirectBytes = 2;
|
||||
static const UInt32 kNumHashBytes = 2;
|
||||
static const UInt32 kHashSize = 1 << (8 * kNumHashBytes);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
@ -54,16 +50,9 @@ CInTree::CInTree():
|
|||
|
||||
void CInTree::FreeMemory()
|
||||
{
|
||||
#ifdef WIN32
|
||||
if (_son != 0)
|
||||
VirtualFree(_son, 0, MEM_RELEASE);
|
||||
if (_hash != 0)
|
||||
VirtualFree(_hash, 0, MEM_RELEASE);
|
||||
#else
|
||||
delete []_son;
|
||||
delete []_hash;
|
||||
#endif
|
||||
BigFree(_son);
|
||||
_son = 0;
|
||||
BigFree(_hash);
|
||||
_hash = 0;
|
||||
CLZInWindow::Free();
|
||||
}
|
||||
|
@ -73,68 +62,63 @@ CInTree::~CInTree()
|
|||
FreeMemory();
|
||||
}
|
||||
|
||||
HRESULT CInTree::Create(UINT32 sizeHistory, UINT32 keepAddBufferBefore,
|
||||
UINT32 matchMaxLen, UINT32 keepAddBufferAfter, UINT32 sizeReserv)
|
||||
HRESULT CInTree::Create(UInt32 sizeHistory, UInt32 keepAddBufferBefore,
|
||||
UInt32 matchMaxLen, UInt32 keepAddBufferAfter, UInt32 sizeReserv)
|
||||
{
|
||||
FreeMemory();
|
||||
try
|
||||
{
|
||||
CLZInWindow::Create(sizeHistory + keepAddBufferBefore,
|
||||
matchMaxLen + keepAddBufferAfter, sizeReserv);
|
||||
|
||||
if (_blockSize + 256 > kMaxValForNormalize)
|
||||
return E_INVALIDARG;
|
||||
|
||||
_historySize = sizeHistory;
|
||||
_matchMaxLen = matchMaxLen;
|
||||
|
||||
_cyclicBufferSize = sizeHistory + 1;
|
||||
|
||||
|
||||
UINT32 size = kHashSize;
|
||||
#ifdef HASH_ARRAY_2
|
||||
size += kHash2Size;
|
||||
#ifdef HASH_ARRAY_3
|
||||
size += kHash3Size;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
_son = (CPair *)::VirtualAlloc(0, (_cyclicBufferSize + 1) * sizeof(CPair), MEM_COMMIT, PAGE_READWRITE);
|
||||
if (_son == 0)
|
||||
throw CMemoryException();
|
||||
_hash = (CIndex *)::VirtualAlloc(0, (size + 1) * sizeof(CIndex), MEM_COMMIT, PAGE_READWRITE);
|
||||
if (_hash == 0)
|
||||
throw CMemoryException();
|
||||
#else
|
||||
_son = new CPair[_cyclicBufferSize + 1];
|
||||
_hash = new CIndex[size + 1];
|
||||
#endif
|
||||
|
||||
// m_RightBase = &m_LeftBase[_blockSize];
|
||||
|
||||
// _hash = &m_RightBase[_blockSize];
|
||||
#ifdef HASH_ARRAY_2
|
||||
_hash2 = &_hash[kHashSize];
|
||||
#ifdef HASH_ARRAY_3
|
||||
_hash3 = &_hash2[kHash2Size];
|
||||
#endif
|
||||
#endif
|
||||
return S_OK;
|
||||
}
|
||||
catch(...)
|
||||
if (!CLZInWindow::Create(sizeHistory + keepAddBufferBefore,
|
||||
matchMaxLen + keepAddBufferAfter, sizeReserv))
|
||||
return E_OUTOFMEMORY;
|
||||
|
||||
if (_blockSize + 256 > kMaxValForNormalize)
|
||||
return E_INVALIDARG;
|
||||
|
||||
_historySize = sizeHistory;
|
||||
_matchMaxLen = matchMaxLen;
|
||||
|
||||
_cyclicBufferSize = sizeHistory + 1;
|
||||
|
||||
|
||||
UInt32 size = kHashSize;
|
||||
#ifdef HASH_ARRAY_2
|
||||
size += kHash2Size;
|
||||
#ifdef HASH_ARRAY_3
|
||||
size += kHash3Size;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
_son = (CPair *)BigAlloc((_cyclicBufferSize + 1) * sizeof(CPair));
|
||||
if (_son == 0)
|
||||
{
|
||||
FreeMemory();
|
||||
return E_OUTOFMEMORY;
|
||||
}
|
||||
|
||||
// UInt32 numBundles = (_cyclicBufferSize + kNumPairsInBundle) >> kNumBundleBits;
|
||||
// _son = (CBundle *)::VirtualAlloc(0, numBundles * sizeof(CBundle), MEM_COMMIT, PAGE_READWRITE);
|
||||
_hash = (CIndex *)BigAlloc((size + 1) * sizeof(CIndex));
|
||||
if (_hash == 0)
|
||||
{
|
||||
FreeMemory();
|
||||
return E_OUTOFMEMORY;
|
||||
}
|
||||
|
||||
// _hash = &m_RightBase[_blockSize];
|
||||
#ifdef HASH_ARRAY_2
|
||||
_hash2 = &_hash[kHashSize];
|
||||
#ifdef HASH_ARRAY_3
|
||||
_hash3 = &_hash2[kHash2Size];
|
||||
#endif
|
||||
#endif
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
static const UINT32 kEmptyHashValue = 0;
|
||||
static const UInt32 kEmptyHashValue = 0;
|
||||
|
||||
HRESULT CInTree::Init(ISequentialInStream *stream)
|
||||
{
|
||||
RINOK(CLZInWindow::Init(stream));
|
||||
unsigned int i;
|
||||
UInt32 i;
|
||||
for(i = 0; i < kHashSize; i++)
|
||||
_hash[i] = kEmptyHashValue;
|
||||
|
||||
|
@ -149,81 +133,93 @@ HRESULT CInTree::Init(ISequentialInStream *stream)
|
|||
|
||||
_cyclicBufferPos = 0;
|
||||
|
||||
ReduceOffsets(0 - 1);
|
||||
ReduceOffsets(-1);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
|
||||
#ifdef HASH_ARRAY_2
|
||||
#ifdef HASH_ARRAY_3
|
||||
inline UINT32 Hash(const BYTE *pointer, UINT32 &hash2Value, UINT32 &hash3Value)
|
||||
inline UInt32 Hash(const Byte *pointer, UInt32 &hash2Value, UInt32 &hash3Value)
|
||||
{
|
||||
UINT32 temp = CCRC::Table[pointer[0]] ^ pointer[1];
|
||||
UInt32 temp = CCRC::Table[pointer[0]] ^ pointer[1];
|
||||
hash2Value = temp & (kHash2Size - 1);
|
||||
hash3Value = (temp ^ (UINT32(pointer[2]) << 8)) & (kHash3Size - 1);
|
||||
return (temp ^ (UINT32(pointer[2]) << 8) ^ (CCRC::Table[pointer[3]] << 5)) &
|
||||
hash3Value = (temp ^ (UInt32(pointer[2]) << 8)) & (kHash3Size - 1);
|
||||
return (temp ^ (UInt32(pointer[2]) << 8) ^ (CCRC::Table[pointer[3]] << 5)) &
|
||||
(kHashSize - 1);
|
||||
}
|
||||
#else // no HASH_ARRAY_3
|
||||
inline UINT32 Hash(const BYTE *pointer, UINT32 &hash2Value)
|
||||
inline UInt32 Hash(const Byte *pointer, UInt32 &hash2Value)
|
||||
{
|
||||
hash2Value = (CCRC::Table[pointer[0]] ^ pointer[1]) & (kHash2Size - 1);
|
||||
return (*((const UINT32 *)pointer)) & 0xFFFFFF;
|
||||
return ((UInt32(pointer[0]) << 16)) | ((UInt32(pointer[1]) << 8)) | pointer[2];
|
||||
}
|
||||
#endif // HASH_ARRAY_3
|
||||
#else // no HASH_ARRAY_2
|
||||
#ifdef HASH_ZIP
|
||||
inline UINT32 Hash(const BYTE *pointer)
|
||||
inline UInt32 Hash(const Byte *pointer)
|
||||
{
|
||||
return ((UINT32(pointer[0]) << 8) ^
|
||||
return ((UInt32(pointer[0]) << 8) ^
|
||||
CCRC::Table[pointer[1]] ^ pointer[2]) & (kHashSize - 1);
|
||||
}
|
||||
#else // no HASH_ZIP
|
||||
inline UINT32 Hash(const BYTE *pointer)
|
||||
inline UInt32 Hash(const Byte *pointer)
|
||||
{
|
||||
return pointer[0] ^ (UINT32(pointer[1]) << 8);
|
||||
return pointer[0] ^ (UInt32(pointer[1]) << 8);
|
||||
}
|
||||
#endif // HASH_ZIP
|
||||
#endif // HASH_ARRAY_2
|
||||
|
||||
UINT32 CInTree::GetLongestMatch(UINT32 *distances)
|
||||
UInt32 CInTree::GetLongestMatch(UInt32 *distances)
|
||||
{
|
||||
UINT32 currentLimit;
|
||||
UInt32 lenLimit;
|
||||
if (_pos + _matchMaxLen <= _streamPos)
|
||||
currentLimit = _matchMaxLen;
|
||||
lenLimit = _matchMaxLen;
|
||||
else
|
||||
{
|
||||
currentLimit = _streamPos - _pos;
|
||||
if(currentLimit < kNumHashBytes)
|
||||
lenLimit = _streamPos - _pos;
|
||||
if(lenLimit < kNumHashBytes)
|
||||
return 0;
|
||||
}
|
||||
|
||||
UINT32 matchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
|
||||
BYTE *cur = _buffer + _pos;
|
||||
UInt32 matchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
|
||||
Byte *cur = _buffer + _pos;
|
||||
|
||||
/*
|
||||
if ((_cyclicBufferPos & kBundleMask) == 0)
|
||||
{
|
||||
Byte *bytes = _son[_cyclicBufferPos >> kNumBundleBits].Bytes;
|
||||
UInt32 bundleLimit = kNumBundleBytes;
|
||||
if (bundleLimit > lenLimit)
|
||||
bundleLimit = lenLimit;
|
||||
for (UInt32 i = 0; i < bundleLimit; i++)
|
||||
bytes[i] = cur[i];
|
||||
}
|
||||
*/
|
||||
|
||||
UINT32 matchHashLenMax = 0;
|
||||
UInt32 matchHashLenMax = 0;
|
||||
|
||||
#ifdef HASH_ARRAY_2
|
||||
UINT32 hash2Value;
|
||||
UInt32 hash2Value;
|
||||
#ifdef HASH_ARRAY_3
|
||||
UINT32 hash3Value;
|
||||
UINT32 hashValue = Hash(cur, hash2Value, hash3Value);
|
||||
UInt32 hash3Value;
|
||||
UInt32 hashValue = Hash(cur, hash2Value, hash3Value);
|
||||
#else
|
||||
UINT32 hashValue = Hash(cur, hash2Value);
|
||||
UInt32 hashValue = Hash(cur, hash2Value);
|
||||
#endif
|
||||
#else
|
||||
UINT32 hashValue = Hash(cur);
|
||||
UInt32 hashValue = Hash(cur);
|
||||
#endif
|
||||
|
||||
UINT32 curMatch = _hash[hashValue];
|
||||
UInt32 curMatch = _hash[hashValue];
|
||||
#ifdef HASH_ARRAY_2
|
||||
UINT32 curMatch2 = _hash2[hash2Value];
|
||||
UInt32 curMatch2 = _hash2[hash2Value];
|
||||
#ifdef HASH_ARRAY_3
|
||||
UINT32 curMatch3 = _hash3[hash3Value];
|
||||
UInt32 curMatch3 = _hash3[hash3Value];
|
||||
#endif
|
||||
_hash2[hash2Value] = _pos;
|
||||
bool matchLen2Exist = false;
|
||||
UINT32 len2Distance = 0;
|
||||
UInt32 len2Distance = 0;
|
||||
if(curMatch2 >= matchMinPos)
|
||||
{
|
||||
if (_buffer[curMatch2] == cur[0])
|
||||
|
@ -236,8 +232,8 @@ UINT32 CInTree::GetLongestMatch(UINT32 *distances)
|
|||
|
||||
#ifdef HASH_ARRAY_3
|
||||
_hash3[hash3Value] = _pos;
|
||||
UINT32 matchLen3Exist = false;
|
||||
UINT32 len3Distance = 0;
|
||||
UInt32 matchLen3Exist = false;
|
||||
UInt32 len3Distance = 0;
|
||||
if(curMatch3 >= matchMinPos)
|
||||
{
|
||||
if (_buffer[curMatch3] == cur[0])
|
||||
|
@ -262,10 +258,14 @@ UINT32 CInTree::GetLongestMatch(UINT32 *distances)
|
|||
|
||||
_hash[hashValue] = _pos;
|
||||
|
||||
// UInt32 bi = _cyclicBufferPos >> kNumBundleBits;
|
||||
// UInt32 bo = _cyclicBufferPos & kBundleMask;
|
||||
// CPair &pair = _son[bi].Pairs[bo];
|
||||
CPair &pair = _son[_cyclicBufferPos];
|
||||
if(curMatch < matchMinPos)
|
||||
{
|
||||
_son[_cyclicBufferPos].Left = kEmptyHashValue;
|
||||
_son[_cyclicBufferPos].Right = kEmptyHashValue;
|
||||
pair.Left = kEmptyHashValue;
|
||||
pair.Right = kEmptyHashValue;
|
||||
|
||||
#ifdef HASH_ARRAY_2
|
||||
distances[2] = len2Distance;
|
||||
|
@ -276,11 +276,11 @@ UINT32 CInTree::GetLongestMatch(UINT32 *distances)
|
|||
|
||||
return matchHashLenMax;
|
||||
}
|
||||
CIndex *ptrLeft = &_son[_cyclicBufferPos].Right;
|
||||
CIndex *ptrRight = &_son[_cyclicBufferPos].Left;
|
||||
CIndex *ptrLeft = &pair.Right;
|
||||
CIndex *ptrRight = &pair.Left;
|
||||
|
||||
UINT32 maxLen, minSameLeft, minSameRight, minSame;
|
||||
maxLen = minSameLeft = minSameRight = minSame = kNumHashDirectBytes;
|
||||
UInt32 maxLen, minLeft, minRight;
|
||||
maxLen = minLeft = minRight = kNumHashDirectBytes;
|
||||
|
||||
#ifdef HASH_ARRAY_2
|
||||
#ifndef HASH_ARRAY_3
|
||||
|
@ -294,46 +294,66 @@ UINT32 CInTree::GetLongestMatch(UINT32 *distances)
|
|||
|
||||
distances[maxLen] = _pos - curMatch - 1;
|
||||
|
||||
for(UINT32 count = _cutValue; count > 0; count--)
|
||||
for(UInt32 count = _cutValue; count > 0; count--)
|
||||
{
|
||||
BYTE *pby1 = _buffer + curMatch;
|
||||
// CIndex left = _son[curMatch].Left; // it's prefetch
|
||||
UINT32 currentLen;
|
||||
for(currentLen = minSame; currentLen < currentLimit; currentLen++/*, dwComps++*/)
|
||||
if (pby1[currentLen] != cur[currentLen])
|
||||
break;
|
||||
while (currentLen > maxLen)
|
||||
distances[++maxLen] = _pos - curMatch - 1;
|
||||
|
||||
UINT32 delta = _pos - curMatch;
|
||||
UINT32 cyclicPos = (delta <= _cyclicBufferPos) ?
|
||||
/*
|
||||
UInt32 delta = _pos - curMatch;
|
||||
UInt32 cyclicPos = (delta <= _cyclicBufferPos) ?
|
||||
(_cyclicBufferPos - delta):
|
||||
(_cyclicBufferPos - delta + _cyclicBufferSize);
|
||||
|
||||
if (currentLen != currentLimit)
|
||||
CBundle &bundle = _son[cyclicPos >> kNumBundleBits];
|
||||
UInt32 bo = cyclicPos & kBundleMask;
|
||||
CPair &pair = bundle.Pairs[bo];
|
||||
|
||||
Byte *pby1 = bundle.Bytes + bo;
|
||||
UInt32 bundleLimit = kNumBundleBytes - bo;
|
||||
UInt32 currentLen = minSame;
|
||||
if (bundleLimit > lenLimit)
|
||||
bundleLimit = lenLimit;
|
||||
for(; currentLen < bundleLimit; currentLen++)
|
||||
if (pby1[currentLen] != cur[currentLen])
|
||||
break;
|
||||
if (currentLen >= bundleLimit)
|
||||
{
|
||||
pby1 = _buffer + curMatch;
|
||||
for(; currentLen < lenLimit; currentLen++)
|
||||
if (pby1[currentLen] != cur[currentLen])
|
||||
break;
|
||||
}
|
||||
*/
|
||||
Byte *pby1 = _buffer + curMatch;
|
||||
// CIndex left = pair.Left; // it's prefetch
|
||||
UInt32 currentLen = MyMin(minLeft, minRight);
|
||||
for(; currentLen < lenLimit; currentLen++)
|
||||
if (pby1[currentLen] != cur[currentLen])
|
||||
break;
|
||||
UInt32 delta = _pos - curMatch;
|
||||
while (currentLen > maxLen)
|
||||
distances[++maxLen] = delta - 1;
|
||||
|
||||
UInt32 cyclicPos = (delta <= _cyclicBufferPos) ?
|
||||
(_cyclicBufferPos - delta):
|
||||
(_cyclicBufferPos - delta + _cyclicBufferSize);
|
||||
CPair &pair = _son[cyclicPos];
|
||||
|
||||
if (currentLen != lenLimit)
|
||||
{
|
||||
if (pby1[currentLen] < cur[currentLen])
|
||||
{
|
||||
*ptrRight = curMatch;
|
||||
ptrRight = &_son[cyclicPos].Right;
|
||||
curMatch = _son[cyclicPos].Right;
|
||||
if(currentLen > minSameLeft)
|
||||
{
|
||||
minSameLeft = currentLen;
|
||||
minSame = MyMin(minSameLeft, minSameRight);
|
||||
}
|
||||
ptrRight = &pair.Right;
|
||||
curMatch = pair.Right;
|
||||
if(currentLen > minLeft)
|
||||
minLeft = currentLen;
|
||||
}
|
||||
else
|
||||
{
|
||||
*ptrLeft = curMatch;
|
||||
ptrLeft = &_son[cyclicPos].Left;
|
||||
// curMatch = left;
|
||||
curMatch = _son[cyclicPos].Left;
|
||||
if(currentLen > minSameRight)
|
||||
{
|
||||
minSameRight = currentLen;
|
||||
minSame = MyMin(minSameLeft, minSameRight);
|
||||
}
|
||||
ptrLeft = &pair.Left;
|
||||
curMatch = pair.Left;
|
||||
if(currentLen > minRight)
|
||||
minRight = currentLen;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -341,18 +361,15 @@ UINT32 CInTree::GetLongestMatch(UINT32 *distances)
|
|||
if(currentLen < _matchMaxLen)
|
||||
{
|
||||
*ptrLeft = curMatch;
|
||||
ptrLeft = &_son[cyclicPos].Left;
|
||||
curMatch = _son[cyclicPos].Left;
|
||||
if(currentLen > minSameRight)
|
||||
{
|
||||
minSameRight = currentLen;
|
||||
minSame = MyMin(minSameLeft, minSameRight);
|
||||
}
|
||||
ptrLeft = &pair.Left;
|
||||
curMatch = pair.Left;
|
||||
if(currentLen > minRight)
|
||||
minRight = currentLen;
|
||||
}
|
||||
else
|
||||
{
|
||||
*ptrLeft = _son[cyclicPos].Right;
|
||||
*ptrRight = _son[cyclicPos].Left;
|
||||
*ptrLeft = pair.Right;
|
||||
*ptrRight = pair.Left;
|
||||
|
||||
#ifdef HASH_ARRAY_2
|
||||
if (matchLen2Exist && len2Distance < distances[2])
|
||||
|
@ -400,84 +417,79 @@ UINT32 CInTree::GetLongestMatch(UINT32 *distances)
|
|||
|
||||
void CInTree::DummyLongestMatch()
|
||||
{
|
||||
UINT32 currentLimit;
|
||||
UInt32 lenLimit;
|
||||
if (_pos + _matchMaxLen <= _streamPos)
|
||||
currentLimit = _matchMaxLen;
|
||||
lenLimit = _matchMaxLen;
|
||||
else
|
||||
{
|
||||
currentLimit = _streamPos - _pos;
|
||||
if(currentLimit < kNumHashBytes)
|
||||
lenLimit = _streamPos - _pos;
|
||||
if(lenLimit < kNumHashBytes)
|
||||
return;
|
||||
}
|
||||
UINT32 matchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
|
||||
BYTE *cur = _buffer + _pos;
|
||||
|
||||
UInt32 matchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
|
||||
Byte *cur = _buffer + _pos;
|
||||
|
||||
#ifdef HASH_ARRAY_2
|
||||
UINT32 hash2Value;
|
||||
UInt32 hash2Value;
|
||||
#ifdef HASH_ARRAY_3
|
||||
UINT32 hash3Value;
|
||||
UINT32 hashValue = Hash(cur, hash2Value, hash3Value);
|
||||
UInt32 hash3Value;
|
||||
UInt32 hashValue = Hash(cur, hash2Value, hash3Value);
|
||||
_hash3[hash3Value] = _pos;
|
||||
#else
|
||||
UINT32 hashValue = Hash(cur, hash2Value);
|
||||
UInt32 hashValue = Hash(cur, hash2Value);
|
||||
#endif
|
||||
_hash2[hash2Value] = _pos;
|
||||
#else
|
||||
UINT32 hashValue = Hash(cur);
|
||||
UInt32 hashValue = Hash(cur);
|
||||
#endif
|
||||
|
||||
UINT32 curMatch = _hash[hashValue];
|
||||
UInt32 curMatch = _hash[hashValue];
|
||||
_hash[hashValue] = _pos;
|
||||
|
||||
CPair &pair = _son[_cyclicBufferPos];
|
||||
|
||||
if(curMatch < matchMinPos)
|
||||
{
|
||||
_son[_cyclicBufferPos].Left = kEmptyHashValue;
|
||||
_son[_cyclicBufferPos].Right = kEmptyHashValue;
|
||||
pair.Left = kEmptyHashValue;
|
||||
pair.Right = kEmptyHashValue;
|
||||
return;
|
||||
}
|
||||
CIndex *ptrLeft = &_son[_cyclicBufferPos].Right;
|
||||
CIndex *ptrRight = &_son[_cyclicBufferPos].Left;
|
||||
CIndex *ptrLeft = &pair.Right;
|
||||
CIndex *ptrRight = &pair.Left;
|
||||
|
||||
UINT32 maxLen, minSameLeft, minSameRight, minSame;
|
||||
maxLen = minSameLeft = minSameRight = minSame = kNumHashDirectBytes;
|
||||
for(UINT32 count = _cutValue; count > 0; count--)
|
||||
UInt32 maxLen, minLeft, minRight;
|
||||
maxLen = minLeft = minRight = kNumHashDirectBytes;
|
||||
for(UInt32 count = _cutValue; count > 0; count--)
|
||||
{
|
||||
BYTE *pby1 = _buffer + curMatch;
|
||||
// CIndex left = _son[curMatch].Left; // it's prefetch
|
||||
UINT32 currentLen;
|
||||
for(currentLen = minSame; currentLen < currentLimit; currentLen++/*, dwComps++*/)
|
||||
Byte *pby1 = _buffer + curMatch;
|
||||
UInt32 currentLen = MyMin(minLeft, minRight);
|
||||
for(; currentLen < lenLimit; currentLen++)
|
||||
if (pby1[currentLen] != cur[currentLen])
|
||||
break;
|
||||
|
||||
UINT32 delta = _pos - curMatch;
|
||||
UINT32 cyclicPos = (delta <= _cyclicBufferPos) ?
|
||||
UInt32 delta = _pos - curMatch;
|
||||
UInt32 cyclicPos = (delta <= _cyclicBufferPos) ?
|
||||
(_cyclicBufferPos - delta):
|
||||
(_cyclicBufferPos - delta + _cyclicBufferSize);
|
||||
|
||||
if (currentLen != currentLimit)
|
||||
CPair &pair = _son[cyclicPos];
|
||||
|
||||
if (currentLen != lenLimit)
|
||||
{
|
||||
if (pby1[currentLen] < cur[currentLen])
|
||||
{
|
||||
*ptrRight = curMatch;
|
||||
ptrRight = &_son[cyclicPos].Right;
|
||||
curMatch = _son[cyclicPos].Right;
|
||||
if(currentLen > minSameLeft)
|
||||
{
|
||||
minSameLeft = currentLen;
|
||||
minSame = MyMin(minSameLeft, minSameRight);
|
||||
}
|
||||
ptrRight = &pair.Right;
|
||||
curMatch = pair.Right;
|
||||
if(currentLen > minLeft)
|
||||
minLeft = currentLen;
|
||||
}
|
||||
else
|
||||
{
|
||||
*ptrLeft = curMatch;
|
||||
ptrLeft = &_son[cyclicPos].Left;
|
||||
curMatch = _son[cyclicPos].Left;
|
||||
// curMatch = left;
|
||||
if(currentLen > minSameRight)
|
||||
{
|
||||
minSameRight = currentLen;
|
||||
minSame = MyMin(minSameLeft, minSameRight);
|
||||
}
|
||||
ptrLeft = &pair.Left;
|
||||
curMatch = pair.Left;
|
||||
if(currentLen > minRight)
|
||||
minRight = currentLen;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -485,18 +497,15 @@ void CInTree::DummyLongestMatch()
|
|||
if(currentLen < _matchMaxLen)
|
||||
{
|
||||
*ptrLeft = curMatch;
|
||||
ptrLeft = &_son[cyclicPos].Left;
|
||||
curMatch = _son[cyclicPos].Left;
|
||||
if(currentLen > minSameRight)
|
||||
{
|
||||
minSameRight = currentLen;
|
||||
minSame = MyMin(minSameLeft, minSameRight);
|
||||
}
|
||||
ptrLeft = &pair.Left;
|
||||
curMatch = pair.Left;
|
||||
if(currentLen > minRight)
|
||||
minRight = currentLen;
|
||||
}
|
||||
else
|
||||
{
|
||||
*ptrLeft = _son[cyclicPos].Right;
|
||||
*ptrRight = _son[cyclicPos].Left;
|
||||
*ptrLeft = pair.Right;
|
||||
*ptrRight = pair.Left;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -507,23 +516,23 @@ void CInTree::DummyLongestMatch()
|
|||
*ptrRight = kEmptyHashValue;
|
||||
}
|
||||
|
||||
void CInTree::NormalizeLinks(CIndex *array, UINT32 numItems, UINT32 subValue)
|
||||
void CInTree::NormalizeLinks(CIndex *items, UInt32 numItems, UInt32 subValue)
|
||||
{
|
||||
for (UINT32 i = 0; i < numItems; i++)
|
||||
for (UInt32 i = 0; i < numItems; i++)
|
||||
{
|
||||
UINT32 value = array[i];
|
||||
UInt32 value = items[i];
|
||||
if (value <= subValue)
|
||||
value = kEmptyHashValue;
|
||||
else
|
||||
value -= subValue;
|
||||
array[i] = value;
|
||||
items[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void CInTree::Normalize()
|
||||
{
|
||||
UINT32 startItem = _pos - _historySize;
|
||||
UINT32 subValue = startItem - 1;
|
||||
UInt32 startItem = _pos - _historySize;
|
||||
UInt32 subValue = startItem - 1;
|
||||
// NormalizeLinks((CIndex *)(_son + startItem), _historySize * 2, subValue);
|
||||
NormalizeLinks((CIndex *)_son, _cyclicBufferSize * 2, subValue);
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// MatchFinders/IMatchFinder.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __IMATCHFINDER_H
|
||||
#define __IMATCHFINDER_H
|
||||
|
||||
|
@ -14,10 +12,10 @@ IInWindowStream: public IUnknown
|
|||
STDMETHOD(Init)(ISequentialInStream *inStream) PURE;
|
||||
STDMETHOD_(void, ReleaseStream)() PURE;
|
||||
STDMETHOD(MovePos)() PURE;
|
||||
STDMETHOD_(BYTE, GetIndexByte)(UINT32 index) PURE;
|
||||
STDMETHOD_(UINT32, GetMatchLen)(UINT32 index, UINT32 distance, UINT32 limit) PURE;
|
||||
STDMETHOD_(UINT32, GetNumAvailableBytes)() PURE;
|
||||
STDMETHOD_(const BYTE *, GetPointerToCurrentPos)() PURE;
|
||||
STDMETHOD_(Byte, GetIndexByte)(Int32 index) PURE;
|
||||
STDMETHOD_(UInt32, GetMatchLen)(Int32 index, UInt32 distance, UInt32 limit) PURE;
|
||||
STDMETHOD_(UInt32, GetNumAvailableBytes)() PURE;
|
||||
STDMETHOD_(const Byte *, GetPointerToCurrentPos)() PURE;
|
||||
};
|
||||
|
||||
// {23170F69-40C1-278A-0000-000200020000}
|
||||
|
@ -26,9 +24,9 @@ DEFINE_GUID(IID_IMatchFinder,
|
|||
MIDL_INTERFACE("23170F69-40C1-278A-0000-000200020000")
|
||||
IMatchFinder: public IInWindowStream
|
||||
{
|
||||
STDMETHOD(Create)(UINT32 historySize, UINT32 keepAddBufferBefore,
|
||||
UINT32 matchMaxLen, UINT32 keepAddBufferAfter) PURE;
|
||||
STDMETHOD_(UINT32, GetLongestMatch)(UINT32 *distances) PURE;
|
||||
STDMETHOD(Create)(UInt32 historySize, UInt32 keepAddBufferBefore,
|
||||
UInt32 matchMaxLen, UInt32 keepAddBufferAfter) PURE;
|
||||
STDMETHOD_(UInt32, GetLongestMatch)(UInt32 *distances) PURE;
|
||||
STDMETHOD_(void, DummyLongestMatch)() PURE;
|
||||
};
|
||||
|
||||
|
|
|
@ -4,27 +4,28 @@
|
|||
|
||||
#include "LZInWindow.h"
|
||||
#include "../../../Common/MyCom.h"
|
||||
|
||||
CLZInWindow::~CLZInWindow()
|
||||
{
|
||||
Free();
|
||||
}
|
||||
#include "../../../Common/Alloc.h"
|
||||
|
||||
void CLZInWindow::Free()
|
||||
{
|
||||
delete []_bufferBase;
|
||||
::BigFree(_bufferBase);
|
||||
_bufferBase = 0;
|
||||
}
|
||||
|
||||
void CLZInWindow::Create(UINT32 keepSizeBefore, UINT32 keepSizeAfter, UINT32 keepSizeReserv)
|
||||
bool CLZInWindow::Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv)
|
||||
{
|
||||
_keepSizeBefore = keepSizeBefore;
|
||||
_keepSizeAfter = keepSizeAfter;
|
||||
_keepSizeReserv = keepSizeReserv;
|
||||
_blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
|
||||
Free();
|
||||
_bufferBase = new BYTE[_blockSize];
|
||||
UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
|
||||
if (_bufferBase == 0 || _blockSize != blockSize)
|
||||
{
|
||||
Free();
|
||||
_blockSize = blockSize;
|
||||
_bufferBase = (Byte *)::BigAlloc(_blockSize);
|
||||
}
|
||||
_pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter;
|
||||
return (_bufferBase != 0);
|
||||
}
|
||||
|
||||
|
||||
|
@ -64,17 +65,17 @@ HRESULT CLZInWindow::ReadBlock()
|
|||
return S_OK;
|
||||
while(true)
|
||||
{
|
||||
UINT32 size = (_bufferBase + _blockSize) - (_buffer + _streamPos);
|
||||
UInt32 size = UInt32(_bufferBase - _buffer) + _blockSize - _streamPos;
|
||||
if(size == 0)
|
||||
return S_OK;
|
||||
UINT32 numReadBytes;
|
||||
UInt32 numReadBytes;
|
||||
RINOK(_stream->ReadPart(_buffer + _streamPos, size, &numReadBytes));
|
||||
if(numReadBytes == 0)
|
||||
{
|
||||
_posLimit = _streamPos;
|
||||
const BYTE *pointerToPostion = _buffer + _posLimit;
|
||||
const Byte *pointerToPostion = _buffer + _posLimit;
|
||||
if(pointerToPostion > _pointerToLastSafePosition)
|
||||
_posLimit = _pointerToLastSafePosition - _buffer;
|
||||
_posLimit = (UInt32)(_pointerToLastSafePosition - _buffer);
|
||||
_streamEndWasReached = true;
|
||||
return S_OK;
|
||||
}
|
||||
|
@ -90,8 +91,8 @@ HRESULT CLZInWindow::ReadBlock()
|
|||
void CLZInWindow::MoveBlock()
|
||||
{
|
||||
BeforeMoveBlock();
|
||||
UINT32 offset = (_buffer + _pos - _keepSizeBefore) - _bufferBase;
|
||||
UINT32 numBytes = (_buffer + _streamPos) - (_bufferBase + offset);
|
||||
UInt32 offset = UInt32(_buffer - _bufferBase) + _pos - _keepSizeBefore;
|
||||
UInt32 numBytes = UInt32(_buffer - _bufferBase) + _streamPos - offset;
|
||||
memmove(_bufferBase, _bufferBase + offset, numBytes);
|
||||
_buffer -= offset;
|
||||
AfterMoveBlock();
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// LZInWindow.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __LZ_IN_WINDOW_H
|
||||
#define __LZ_IN_WINDOW_H
|
||||
|
||||
|
@ -9,19 +7,19 @@
|
|||
|
||||
class CLZInWindow
|
||||
{
|
||||
BYTE *_bufferBase; // pointer to buffer with data
|
||||
Byte *_bufferBase; // pointer to buffer with data
|
||||
ISequentialInStream *_stream;
|
||||
UINT32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done
|
||||
UInt32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done
|
||||
bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
|
||||
const BYTE *_pointerToLastSafePosition;
|
||||
const Byte *_pointerToLastSafePosition;
|
||||
protected:
|
||||
BYTE *_buffer; // Pointer to virtual Buffer begin
|
||||
UINT32 _blockSize; // Size of Allocated memory block
|
||||
UINT32 _pos; // offset (from _buffer) of curent byte
|
||||
UINT32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
|
||||
UINT32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
|
||||
UINT32 _keepSizeReserv; // how many BYTEs must be kept as reserv
|
||||
UINT32 _streamPos; // offset (from _buffer) of first not read byte from Stream
|
||||
Byte *_buffer; // Pointer to virtual Buffer begin
|
||||
UInt32 _blockSize; // Size of Allocated memory block
|
||||
UInt32 _pos; // offset (from _buffer) of curent byte
|
||||
UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
|
||||
UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
|
||||
UInt32 _keepSizeReserv; // how many BYTEs must be kept as reserv
|
||||
UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream
|
||||
|
||||
virtual void BeforeMoveBlock() {};
|
||||
virtual void AfterMoveBlock() {};
|
||||
|
@ -30,23 +28,24 @@ protected:
|
|||
void Free();
|
||||
public:
|
||||
CLZInWindow(): _bufferBase(0) {}
|
||||
virtual ~CLZInWindow();
|
||||
void Create(UINT32 keepSizeBefore, UINT32 keepSizeAfter,
|
||||
UINT32 keepSizeReserv = (1<<17));
|
||||
virtual ~CLZInWindow() { Free(); }
|
||||
|
||||
bool Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter,
|
||||
UInt32 keepSizeReserv = (1<<17));
|
||||
|
||||
HRESULT Init(ISequentialInStream *stream);
|
||||
// void ReleaseStream();
|
||||
|
||||
BYTE *GetBuffer() const { return _buffer; }
|
||||
Byte *GetBuffer() const { return _buffer; }
|
||||
|
||||
const BYTE *GetPointerToCurrentPos() const { return _buffer + _pos; }
|
||||
const Byte *GetPointerToCurrentPos() const { return _buffer + _pos; }
|
||||
|
||||
HRESULT MovePos()
|
||||
{
|
||||
_pos++;
|
||||
if (_pos > _posLimit)
|
||||
{
|
||||
const BYTE *pointerToPostion = _buffer + _pos;
|
||||
const Byte *pointerToPostion = _buffer + _pos;
|
||||
if(pointerToPostion > _pointerToLastSafePosition)
|
||||
MoveBlock();
|
||||
return ReadBlock();
|
||||
|
@ -54,29 +53,25 @@ public:
|
|||
else
|
||||
return S_OK;
|
||||
}
|
||||
// BYTE GetCurrentByte()const;
|
||||
BYTE GetIndexByte(UINT32 index)const
|
||||
{ return _buffer[_pos + index]; }
|
||||
|
||||
// UINT32 GetCurPos()const { return _pos;};
|
||||
// BYTE *GetBufferBeg()const { return _buffer;};
|
||||
Byte GetIndexByte(Int32 index)const
|
||||
{ return _buffer[(size_t)_pos + index]; }
|
||||
|
||||
// index + limit have not to exceed _keepSizeAfter;
|
||||
UINT32 GetMatchLen(UINT32 index, UINT32 back, UINT32 limit) const
|
||||
UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit) const
|
||||
{
|
||||
if(_streamEndWasReached)
|
||||
if ((_pos + index) + limit > _streamPos)
|
||||
limit = _streamPos - (_pos + index);
|
||||
back++;
|
||||
BYTE *pby = _buffer + _pos + index;
|
||||
UINT32 i;
|
||||
for(i = 0; i < limit && pby[i] == pby[i - back]; i++);
|
||||
distance++;
|
||||
Byte *pby = _buffer + (size_t)_pos + index;
|
||||
UInt32 i;
|
||||
for(i = 0; i < limit && pby[i] == pby[(size_t)i - distance]; i++);
|
||||
return i;
|
||||
}
|
||||
|
||||
UINT32 GetNumAvailableBytes() const { return _streamPos - _pos; }
|
||||
UInt32 GetNumAvailableBytes() const { return _streamPos - _pos; }
|
||||
|
||||
void ReduceOffsets(UINT32 subValue)
|
||||
void ReduceOffsets(Int32 subValue)
|
||||
{
|
||||
_buffer += subValue;
|
||||
_posLimit -= subValue;
|
||||
|
|
|
@ -2,36 +2,29 @@
|
|||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "../../../Common/Alloc.h"
|
||||
#include "LZOutWindow.h"
|
||||
|
||||
void CLZOutWindow::Create(UINT32 windowSize)
|
||||
bool CLZOutWindow::Create(UInt32 windowSize)
|
||||
{
|
||||
_pos = 0;
|
||||
_streamPos = 0;
|
||||
UINT32 newBlockSize = windowSize;
|
||||
const UINT32 kMinBlockSize = 1;
|
||||
if (newBlockSize < kMinBlockSize)
|
||||
newBlockSize = kMinBlockSize;
|
||||
if (_buffer != 0 && _windowSize == newBlockSize)
|
||||
return;
|
||||
delete []_buffer;
|
||||
_buffer = 0;
|
||||
_windowSize = newBlockSize;
|
||||
_buffer = new BYTE[_windowSize];
|
||||
}
|
||||
|
||||
CLZOutWindow::~CLZOutWindow()
|
||||
{
|
||||
// ReleaseStream();
|
||||
delete []_buffer;
|
||||
}
|
||||
|
||||
/*
|
||||
void CLZOutWindow::SetWindowSize(UINT32 windowSize)
|
||||
{
|
||||
const UInt32 kMinBlockSize = 1;
|
||||
if (windowSize < kMinBlockSize)
|
||||
windowSize = kMinBlockSize;
|
||||
if (_buffer != 0 && _windowSize == windowSize)
|
||||
return true;
|
||||
Free();
|
||||
_windowSize = windowSize;
|
||||
_buffer = (Byte *)::BigAlloc(windowSize);
|
||||
return (_buffer != 0);
|
||||
}
|
||||
|
||||
void CLZOutWindow::Free()
|
||||
{
|
||||
::BigFree(_buffer);
|
||||
_buffer = 0;
|
||||
}
|
||||
*/
|
||||
|
||||
void CLZOutWindow::Init(ISequentialOutStream *stream, bool solid)
|
||||
{
|
||||
|
@ -44,6 +37,9 @@ void CLZOutWindow::Init(ISequentialOutStream *stream, bool solid)
|
|||
_streamPos = 0;
|
||||
_pos = 0;
|
||||
}
|
||||
#ifdef _NO_EXCEPTIONS
|
||||
ErrorCode = S_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -61,16 +57,25 @@ void CLZOutWindow::ReleaseStream()
|
|||
void CLZOutWindow::FlushWithCheck()
|
||||
{
|
||||
HRESULT result = Flush();
|
||||
#ifdef _NO_EXCEPTIONS
|
||||
ErrorCode = result;
|
||||
#else
|
||||
if (result != S_OK)
|
||||
throw CLZOutWindowException(result);
|
||||
#endif
|
||||
}
|
||||
|
||||
HRESULT CLZOutWindow::Flush()
|
||||
{
|
||||
UINT32 size = _pos - _streamPos;
|
||||
UInt32 size = _pos - _streamPos;
|
||||
if(size == 0)
|
||||
return S_OK;
|
||||
UINT32 processedSize;
|
||||
#ifdef _NO_EXCEPTIONS
|
||||
if (ErrorCode != S_OK)
|
||||
return ErrorCode;
|
||||
#endif
|
||||
|
||||
UInt32 processedSize;
|
||||
HRESULT result = _stream->Write(_buffer + _streamPos, size, &processedSize);
|
||||
if (result != S_OK)
|
||||
return result;
|
||||
|
|
|
@ -1,78 +1,71 @@
|
|||
// LZOutWindow.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __LZ_OUT_WINDOW_H
|
||||
#define __LZ_OUT_WINDOW_H
|
||||
|
||||
#include "../../IStream.h"
|
||||
|
||||
// m_KeepSizeBefore: how mach BYTEs must be in buffer before _pos;
|
||||
// m_KeepSizeAfter: how mach BYTEs must be in buffer after _pos;
|
||||
// m_KeepSizeReserv: how mach BYTEs must be in buffer for Moving Reserv;
|
||||
// must be >= aKeepSizeAfter; // test it
|
||||
|
||||
#ifndef _NO_EXCEPTIONS
|
||||
class CLZOutWindowException
|
||||
{
|
||||
public:
|
||||
HRESULT ErrorCode;
|
||||
CLZOutWindowException(HRESULT errorCode): ErrorCode(errorCode) {}
|
||||
};
|
||||
#endif
|
||||
|
||||
class CLZOutWindow
|
||||
{
|
||||
BYTE *_buffer;
|
||||
UINT32 _pos;
|
||||
UINT32 _windowSize;
|
||||
UINT32 _streamPos;
|
||||
Byte *_buffer;
|
||||
UInt32 _pos;
|
||||
UInt32 _windowSize;
|
||||
UInt32 _streamPos;
|
||||
ISequentialOutStream *_stream;
|
||||
void FlushWithCheck();
|
||||
|
||||
public:
|
||||
CLZOutWindow(): _buffer(0), _stream(0) {}
|
||||
~CLZOutWindow();
|
||||
void Create(UINT32 windowSize);
|
||||
bool IsCreated() const { return _buffer != 0; }
|
||||
#ifdef _NO_EXCEPTIONS
|
||||
HRESULT ErrorCode;
|
||||
#endif
|
||||
|
||||
void Free();
|
||||
CLZOutWindow(): _buffer(0), _stream(0) {}
|
||||
~CLZOutWindow() { Free(); /* ReleaseStream(); */ }
|
||||
bool Create(UInt32 windowSize);
|
||||
|
||||
void Init(ISequentialOutStream *stream, bool solid = false);
|
||||
HRESULT Flush();
|
||||
// void ReleaseStream();
|
||||
|
||||
// UINT32 GetCurPos() const { return _pos; }
|
||||
// const BYTE *GetPointerToCurrentPos() const { return _buffer + _pos;};
|
||||
|
||||
void CopyBackBlock(UINT32 distance, UINT32 len)
|
||||
void CopyBlock(UInt32 distance, UInt32 len)
|
||||
{
|
||||
UINT32 pos = _pos - distance - 1;
|
||||
if (pos >= _windowSize)
|
||||
pos += _windowSize;
|
||||
for(; len > 0; len--)
|
||||
{
|
||||
if (pos >= _windowSize)
|
||||
pos = 0;
|
||||
_buffer[_pos++] = _buffer[pos++];
|
||||
if (_pos >= _windowSize)
|
||||
FlushWithCheck();
|
||||
// PutOneByte(GetOneByte(0 - distance));
|
||||
}
|
||||
UInt32 pos = _pos - distance - 1;
|
||||
if (pos >= _windowSize)
|
||||
pos += _windowSize;
|
||||
for(; len > 0; len--)
|
||||
{
|
||||
if (pos >= _windowSize)
|
||||
pos = 0;
|
||||
_buffer[_pos++] = _buffer[pos++];
|
||||
if (_pos >= _windowSize)
|
||||
FlushWithCheck();
|
||||
// PutOneByte(GetOneByte(distance));
|
||||
}
|
||||
}
|
||||
|
||||
void PutOneByte(BYTE b)
|
||||
|
||||
void PutByte(Byte b)
|
||||
{
|
||||
_buffer[_pos++] = b;
|
||||
if (_pos >= _windowSize)
|
||||
FlushWithCheck();
|
||||
_buffer[_pos++] = b;
|
||||
if (_pos >= _windowSize)
|
||||
FlushWithCheck();
|
||||
}
|
||||
|
||||
BYTE GetOneByte(UINT32 index) const
|
||||
|
||||
Byte GetByte(UInt32 distance) const
|
||||
{
|
||||
UINT32 pos = _pos + index;
|
||||
if (pos >= _windowSize)
|
||||
pos += _windowSize;
|
||||
return _buffer[pos];
|
||||
UInt32 pos = _pos - distance - 1;
|
||||
if (pos >= _windowSize)
|
||||
pos += _windowSize;
|
||||
return _buffer[pos];
|
||||
}
|
||||
|
||||
// BYTE *GetBuffer() const { return _buffer; }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
// stdafx.h
|
||||
// StdAfx.h
|
||||
|
||||
#ifndef __STDAFX_H
|
||||
#define __STDAFX_H
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include "../../../../Platform.h"
|
||||
#endif
|
||||
#include "../../../../Platform.h"
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,55 +1,29 @@
|
|||
// LZMA.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#include "LZMALen.h"
|
||||
|
||||
#ifndef __LZMA_H
|
||||
#define __LZMA_H
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
|
||||
const UINT32 kNumRepDistances = 4;
|
||||
const UInt32 kNumRepDistances = 4;
|
||||
|
||||
const BYTE kNumStates = 12;
|
||||
const int kNumStates = 12;
|
||||
|
||||
const BYTE kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
|
||||
const BYTE kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
|
||||
const BYTE kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
|
||||
const BYTE kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
|
||||
const Byte kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
|
||||
const Byte kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
|
||||
const Byte kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
|
||||
const Byte kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
|
||||
|
||||
class CState
|
||||
{
|
||||
public:
|
||||
BYTE Index;
|
||||
void Init()
|
||||
{ Index = 0; }
|
||||
void UpdateChar()
|
||||
{ Index = kLiteralNextStates[Index]; }
|
||||
void UpdateMatch()
|
||||
{ Index = kMatchNextStates[Index]; }
|
||||
void UpdateRep()
|
||||
{ Index = kRepNextStates[Index]; }
|
||||
void UpdateShortRep()
|
||||
{ Index = kShortRepNextStates[Index]; }
|
||||
};
|
||||
|
||||
class CBaseCoder
|
||||
{
|
||||
protected:
|
||||
CState _state;
|
||||
BYTE _previousByte;
|
||||
bool _peviousIsMatch;
|
||||
UINT32 _repDistances[kNumRepDistances];
|
||||
void Init()
|
||||
{
|
||||
_state.Init();
|
||||
_previousByte = 0;
|
||||
_peviousIsMatch = false;
|
||||
for(unsigned int i = 0 ; i < kNumRepDistances; i++)
|
||||
_repDistances[i] = 0;
|
||||
}
|
||||
Byte Index;
|
||||
void Init() { Index = 0; }
|
||||
void UpdateChar() { Index = kLiteralNextStates[Index]; }
|
||||
void UpdateMatch() { Index = kMatchNextStates[Index]; }
|
||||
void UpdateRep() { Index = kRepNextStates[Index]; }
|
||||
void UpdateShortRep() { Index = kShortRepNextStates[Index]; }
|
||||
};
|
||||
|
||||
const int kNumPosSlotBits = 6;
|
||||
|
@ -57,8 +31,9 @@ const int kDicLogSizeMin = 0;
|
|||
const int kDicLogSizeMax = 32;
|
||||
const int kDistTableSizeMax = kDicLogSizeMax * 2;
|
||||
|
||||
const UINT32 kNumLenToPosStates = 4;
|
||||
inline UINT32 GetLenToPosState(UINT32 len)
|
||||
const UInt32 kNumLenToPosStates = 4;
|
||||
|
||||
inline UInt32 GetLenToPosState(UInt32 len)
|
||||
{
|
||||
len -= 2;
|
||||
if (len < kNumLenToPosStates)
|
||||
|
@ -66,31 +41,41 @@ inline UINT32 GetLenToPosState(UINT32 len)
|
|||
return kNumLenToPosStates - 1;
|
||||
}
|
||||
|
||||
const UINT32 kMatchMinLen = 2;
|
||||
namespace NLength {
|
||||
|
||||
const UINT32 kMatchMaxLen = kMatchMinLen + NLength::kNumSymbolsTotal - 1;
|
||||
const int kNumPosStatesBitsMax = 4;
|
||||
const UInt32 kNumPosStatesMax = (1 << kNumPosStatesBitsMax);
|
||||
|
||||
const int kNumPosStatesBitsEncodingMax = 4;
|
||||
const UInt32 kNumPosStatesEncodingMax = (1 << kNumPosStatesBitsEncodingMax);
|
||||
|
||||
const int kNumLowBits = 3;
|
||||
const int kNumMidBits = 3;
|
||||
const int kNumHighBits = 8;
|
||||
const UInt32 kNumLowSymbols = 1 << kNumLowBits;
|
||||
const UInt32 kNumMidSymbols = 1 << kNumMidBits;
|
||||
const UInt32 kNumSymbolsTotal = kNumLowSymbols + kNumMidSymbols + (1 << kNumHighBits);
|
||||
|
||||
}
|
||||
|
||||
const UInt32 kMatchMinLen = 2;
|
||||
const UInt32 kMatchMaxLen = kMatchMinLen + NLength::kNumSymbolsTotal - 1;
|
||||
|
||||
const int kNumAlignBits = 4;
|
||||
const UINT32 kAlignTableSize = 1 << kNumAlignBits;
|
||||
const UINT32 kAlignMask = (kAlignTableSize - 1);
|
||||
const UInt32 kAlignTableSize = 1 << kNumAlignBits;
|
||||
const UInt32 kAlignMask = (kAlignTableSize - 1);
|
||||
|
||||
const UINT32 kStartPosModelIndex = 4;
|
||||
const UINT32 kEndPosModelIndex = 14;
|
||||
const UINT32 kNumPosModels = kEndPosModelIndex - kStartPosModelIndex;
|
||||
const UInt32 kStartPosModelIndex = 4;
|
||||
const UInt32 kEndPosModelIndex = 14;
|
||||
const UInt32 kNumPosModels = kEndPosModelIndex - kStartPosModelIndex;
|
||||
|
||||
const UINT32 kNumFullDistances = 1 << (kEndPosModelIndex / 2);
|
||||
|
||||
const UINT32 kMainChoiceLiteralIndex = 0;
|
||||
const UINT32 kMainChoiceMatchIndex = 1;
|
||||
|
||||
const UINT32 kMatchChoiceDistanceIndex= 0;
|
||||
const UINT32 kMatchChoiceRepetitionIndex = 1;
|
||||
|
||||
const int kNumMoveBits = 5;
|
||||
const UInt32 kNumFullDistances = 1 << (kEndPosModelIndex / 2);
|
||||
|
||||
const int kNumLitPosStatesBitsEncodingMax = 4;
|
||||
const int kNumLitContextBitsMax = 8;
|
||||
|
||||
const int kNumMoveBits = 5;
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,356 +0,0 @@
|
|||
// LZMADecoder.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "LZMADecoder.h"
|
||||
#include "../../../Common/Defs.h"
|
||||
#include "../../../Common/ComTry.h"
|
||||
|
||||
/*
|
||||
#include "fstream.h"
|
||||
#include "iomanip.h"
|
||||
|
||||
ofstream ofs("res.dat");
|
||||
|
||||
const kNumCounters = 3;
|
||||
UINT32 g_Counter[kNumCounters];
|
||||
class C1
|
||||
{
|
||||
public:
|
||||
~C1()
|
||||
{
|
||||
for (int i = 0; i < kNumCounters; i++)
|
||||
ofs << setw(10) << g_Counter[i] << endl;
|
||||
}
|
||||
} g_C1;
|
||||
*/
|
||||
|
||||
/*
|
||||
const UINT32 kLenTableMax = 20;
|
||||
const UINT32 kNumDists = NCompress::NLZMA::kDistTableSizeMax / 2;
|
||||
UINT32 g_Counts[kLenTableMax][kNumDists];
|
||||
class C1
|
||||
{
|
||||
public:
|
||||
~C1 ()
|
||||
{
|
||||
UINT32 sums[kLenTableMax];
|
||||
for (int len = 2; len < kLenTableMax; len++)
|
||||
{
|
||||
sums[len] = 0;
|
||||
for (int dist = 0; dist < kNumDists; dist++)
|
||||
sums[len] += g_Counts[len][dist];
|
||||
if (sums[len] == 0)
|
||||
sums[len] = 1;
|
||||
}
|
||||
for (int dist = 0; dist < kNumDists; dist++)
|
||||
{
|
||||
ofs << setw(4) << dist << " ";
|
||||
for (int len = 2; len < kLenTableMax; len++)
|
||||
{
|
||||
ofs << setw(4) << g_Counts[len][dist] * 1000 / sums[len];
|
||||
}
|
||||
ofs << endl;
|
||||
}
|
||||
}
|
||||
} g_Class;
|
||||
|
||||
void UpdateStat(UINT32 len, UINT32 dist)
|
||||
{
|
||||
if (len >= kLenTableMax)
|
||||
len = kLenTableMax - 1;
|
||||
g_Counts[len][dist / 2]++;
|
||||
}
|
||||
*/
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
|
||||
HRESULT CDecoder::SetDictionarySize(UINT32 dictionarySize)
|
||||
{
|
||||
if (_dictionarySize != dictionarySize)
|
||||
{
|
||||
_dictionarySize = dictionarySize;
|
||||
_dictionarySizeCheck = MyMax(_dictionarySize, UINT32(1));
|
||||
UINT32 blockSize = MyMax(_dictionarySizeCheck, UINT32(1 << 12));
|
||||
try
|
||||
{
|
||||
_outWindowStream.Create(blockSize /*, kMatchMaxLen */);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
return E_OUTOFMEMORY;
|
||||
}
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CDecoder::SetLiteralProperties(
|
||||
UINT32 numLiteralPosStateBits, UINT32 numLiteralContextBits)
|
||||
{
|
||||
if (numLiteralPosStateBits > 8)
|
||||
return E_INVALIDARG;
|
||||
if (numLiteralContextBits > 8)
|
||||
return E_INVALIDARG;
|
||||
_literalDecoder.Create(numLiteralPosStateBits, numLiteralContextBits);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CDecoder::SetPosBitsProperties(UINT32 numPosStateBits)
|
||||
{
|
||||
if (numPosStateBits > NLength::kNumPosStatesBitsMax)
|
||||
return E_INVALIDARG;
|
||||
UINT32 numPosStates = 1 << numPosStateBits;
|
||||
_lenDecoder.Create(numPosStates);
|
||||
_repMatchLenDecoder.Create(numPosStates);
|
||||
_posStateMask = numPosStates - 1;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
CDecoder::CDecoder():
|
||||
_dictionarySize((UINT32)-1)
|
||||
{
|
||||
Create();
|
||||
}
|
||||
|
||||
HRESULT CDecoder::Create()
|
||||
{
|
||||
COM_TRY_BEGIN
|
||||
for(int i = 0; i < kNumPosModels; i++)
|
||||
_posDecoders[i].Create(((kStartPosModelIndex + i) >> 1) - 1);
|
||||
COM_TRY_END
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
|
||||
HRESULT CDecoder::Init(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream)
|
||||
{
|
||||
_rangeDecoder.Init(inStream);
|
||||
|
||||
_outWindowStream.Init(outStream);
|
||||
|
||||
int i;
|
||||
for(i = 0; i < kNumStates; i++)
|
||||
{
|
||||
for (UINT32 j = 0; j <= _posStateMask; j++)
|
||||
{
|
||||
_mainChoiceDecoders[i][j].Init();
|
||||
_matchRepShortChoiceDecoders[i][j].Init();
|
||||
}
|
||||
_matchChoiceDecoders[i].Init();
|
||||
_matchRepChoiceDecoders[i].Init();
|
||||
_matchRep1ChoiceDecoders[i].Init();
|
||||
_matchRep2ChoiceDecoders[i].Init();
|
||||
}
|
||||
|
||||
_literalDecoder.Init();
|
||||
|
||||
// _repMatchLenDecoder.Init();
|
||||
|
||||
for (i = 0; i < kNumLenToPosStates; i++)
|
||||
_posSlotDecoder[i].Init();
|
||||
|
||||
for(i = 0; i < kNumPosModels; i++)
|
||||
_posDecoders[i].Init();
|
||||
|
||||
_lenDecoder.Init();
|
||||
_repMatchLenDecoder.Init();
|
||||
|
||||
_posAlignDecoder.Init();
|
||||
return S_OK;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
STDMETHODIMP CDecoder::CodeReal(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream,
|
||||
const UINT64 *inSize, const UINT64 *outSize,
|
||||
ICompressProgressInfo *progress)
|
||||
{
|
||||
/*
|
||||
if (outSize == NULL)
|
||||
return E_INVALIDARG;
|
||||
*/
|
||||
|
||||
Init(inStream, outStream);
|
||||
CDecoderFlusher flusher(this);
|
||||
|
||||
CState state;
|
||||
state.Init();
|
||||
bool peviousIsMatch = false;
|
||||
BYTE previousByte = 0;
|
||||
UINT32 repDistances[kNumRepDistances];
|
||||
for(int i = 0 ; i < kNumRepDistances; i++)
|
||||
repDistances[i] = 0;
|
||||
|
||||
UINT64 nowPos64 = 0;
|
||||
UINT64 size = (outSize == NULL) ? (UINT64)(INT64)(-1) : *outSize;
|
||||
while(nowPos64 < size)
|
||||
{
|
||||
UINT64 nextPos = MyMin(nowPos64 + (1 << 18), size);
|
||||
while(nowPos64 < nextPos)
|
||||
{
|
||||
UINT32 posState = UINT32(nowPos64) & _posStateMask;
|
||||
if (_mainChoiceDecoders[state.Index][posState].Decode(&_rangeDecoder) == kMainChoiceLiteralIndex)
|
||||
{
|
||||
state.UpdateChar();
|
||||
if(peviousIsMatch)
|
||||
{
|
||||
BYTE matchByte = _outWindowStream.GetOneByte(0 - repDistances[0] - 1);
|
||||
previousByte = _literalDecoder.DecodeWithMatchByte(&_rangeDecoder,
|
||||
UINT32(nowPos64), previousByte, matchByte);
|
||||
peviousIsMatch = false;
|
||||
}
|
||||
else
|
||||
previousByte = _literalDecoder.DecodeNormal(&_rangeDecoder,
|
||||
UINT32(nowPos64), previousByte);
|
||||
_outWindowStream.PutOneByte(previousByte);
|
||||
nowPos64++;
|
||||
}
|
||||
else
|
||||
{
|
||||
peviousIsMatch = true;
|
||||
UINT32 distance, len;
|
||||
if(_matchChoiceDecoders[state.Index].Decode(&_rangeDecoder) ==
|
||||
kMatchChoiceRepetitionIndex)
|
||||
{
|
||||
if(_matchRepChoiceDecoders[state.Index].Decode(&_rangeDecoder) == 0)
|
||||
{
|
||||
if(_matchRepShortChoiceDecoders[state.Index][posState].Decode(&_rangeDecoder) == 0)
|
||||
{
|
||||
state.UpdateShortRep();
|
||||
previousByte = _outWindowStream.GetOneByte(0 - repDistances[0] - 1);
|
||||
_outWindowStream.PutOneByte(previousByte);
|
||||
nowPos64++;
|
||||
continue;
|
||||
}
|
||||
distance = repDistances[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_matchRep1ChoiceDecoders[state.Index].Decode(&_rangeDecoder) == 0)
|
||||
distance = repDistances[1];
|
||||
else
|
||||
{
|
||||
if (_matchRep2ChoiceDecoders[state.Index].Decode(&_rangeDecoder) == 0)
|
||||
distance = repDistances[2];
|
||||
else
|
||||
{
|
||||
distance = repDistances[3];
|
||||
repDistances[3] = repDistances[2];
|
||||
}
|
||||
repDistances[2] = repDistances[1];
|
||||
}
|
||||
repDistances[1] = repDistances[0];
|
||||
repDistances[0] = distance;
|
||||
}
|
||||
len = _repMatchLenDecoder.Decode(&_rangeDecoder, posState) + kMatchMinLen;
|
||||
state.UpdateRep();
|
||||
}
|
||||
else
|
||||
{
|
||||
len = kMatchMinLen + _lenDecoder.Decode(&_rangeDecoder, posState);
|
||||
state.UpdateMatch();
|
||||
UINT32 posSlot = _posSlotDecoder[GetLenToPosState(len)].Decode(&_rangeDecoder);
|
||||
if (posSlot >= kStartPosModelIndex)
|
||||
{
|
||||
UINT32 numDirectBits = (posSlot >> 1) - 1;
|
||||
distance = ((2 | (posSlot & 1)) << numDirectBits);
|
||||
|
||||
if (posSlot < kEndPosModelIndex)
|
||||
distance += _posDecoders[posSlot - kStartPosModelIndex].Decode(&_rangeDecoder);
|
||||
else
|
||||
{
|
||||
distance += (_rangeDecoder.DecodeDirectBits(
|
||||
numDirectBits - kNumAlignBits) << kNumAlignBits);
|
||||
distance += _posAlignDecoder.Decode(&_rangeDecoder);
|
||||
}
|
||||
}
|
||||
else
|
||||
distance = posSlot;
|
||||
|
||||
repDistances[3] = repDistances[2];
|
||||
repDistances[2] = repDistances[1];
|
||||
repDistances[1] = repDistances[0];
|
||||
repDistances[0] = distance;
|
||||
// UpdateStat(len, posSlot);
|
||||
}
|
||||
if (distance >= nowPos64 || distance >= _dictionarySizeCheck)
|
||||
{
|
||||
if (distance == (UINT32)(-1) && size == (UINT64)(INT64)(-1))
|
||||
{
|
||||
flusher.NeedFlush = false;
|
||||
return Flush();
|
||||
}
|
||||
throw "data error";
|
||||
}
|
||||
_outWindowStream.CopyBackBlock(distance, len);
|
||||
nowPos64 += len;
|
||||
previousByte = _outWindowStream.GetOneByte(0 - 1);
|
||||
}
|
||||
}
|
||||
if (progress != NULL)
|
||||
{
|
||||
UINT64 inSize = _rangeDecoder.GetProcessedSize();
|
||||
RINOK(progress->SetRatioInfo(&inSize, &nowPos64));
|
||||
}
|
||||
}
|
||||
flusher.NeedFlush = false;
|
||||
return Flush();
|
||||
}
|
||||
|
||||
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
|
||||
ICompressProgressInfo *progress)
|
||||
{
|
||||
try { return CodeReal(inStream, outStream, inSize, outSize, progress); }
|
||||
catch(const CInBufferException &e) { return e.ErrorCode; }
|
||||
catch(const CLZOutWindowException &e) { return e.ErrorCode; }
|
||||
catch(...) { return S_FALSE; }
|
||||
}
|
||||
|
||||
|
||||
static HRESULT DecodeProperties(ISequentialInStream *inStream,
|
||||
UINT32 &numPosStateBits,
|
||||
UINT32 &numLiteralPosStateBits,
|
||||
UINT32 &numLiteralContextBits,
|
||||
UINT32 &dictionarySize)
|
||||
{
|
||||
UINT32 processesedSize;
|
||||
|
||||
BYTE firstByte;
|
||||
RINOK(inStream->Read(&firstByte, sizeof(firstByte), &processesedSize));
|
||||
if (processesedSize != sizeof(firstByte))
|
||||
return E_INVALIDARG;
|
||||
|
||||
numLiteralContextBits = firstByte % 9;
|
||||
BYTE remainder = firstByte / 9;
|
||||
numLiteralPosStateBits = remainder % 5;
|
||||
numPosStateBits = remainder / 5;
|
||||
|
||||
RINOK(inStream->Read(&dictionarySize, sizeof(dictionarySize), &processesedSize));
|
||||
if (processesedSize != sizeof(dictionarySize))
|
||||
return E_INVALIDARG;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CDecoder::SetDecoderProperties(ISequentialInStream *inStream)
|
||||
{
|
||||
UINT32 numPosStateBits;
|
||||
UINT32 numLiteralPosStateBits;
|
||||
UINT32 numLiteralContextBits;
|
||||
UINT32 dictionarySize;
|
||||
RINOK(DecodeProperties(inStream,
|
||||
numPosStateBits,
|
||||
numLiteralPosStateBits,
|
||||
numLiteralContextBits,
|
||||
dictionarySize));
|
||||
RINOK(SetDictionarySize(dictionarySize));
|
||||
RINOK(SetLiteralProperties(numLiteralPosStateBits, numLiteralContextBits));
|
||||
RINOK(SetPosBitsProperties(numPosStateBits));
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
}}
|
|
@ -1,107 +0,0 @@
|
|||
// LZMA/Decoder.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __LZMA_DECODER_H
|
||||
#define __LZMA_DECODER_H
|
||||
|
||||
#include "../../ICoder.h"
|
||||
#include "../../../Common/MyCom.h"
|
||||
#include "../LZ/LZOutWindow.h"
|
||||
|
||||
#include "LZMA.h"
|
||||
#include "LZMALen.h"
|
||||
#include "LZMALiteral.h"
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
|
||||
typedef NRangeCoder::CBitDecoder<kNumMoveBits> CMyBitDecoder;
|
||||
|
||||
class CDecoder:
|
||||
public ICompressCoder,
|
||||
public ICompressSetDecoderProperties,
|
||||
public CMyUnknownImp
|
||||
{
|
||||
CLZOutWindow _outWindowStream;
|
||||
NRangeCoder::CDecoder _rangeDecoder;
|
||||
|
||||
CMyBitDecoder _mainChoiceDecoders[kNumStates][NLength::kNumPosStatesMax];
|
||||
CMyBitDecoder _matchChoiceDecoders[kNumStates];
|
||||
CMyBitDecoder _matchRepChoiceDecoders[kNumStates];
|
||||
CMyBitDecoder _matchRep1ChoiceDecoders[kNumStates];
|
||||
CMyBitDecoder _matchRep2ChoiceDecoders[kNumStates];
|
||||
CMyBitDecoder _matchRepShortChoiceDecoders[kNumStates][NLength::kNumPosStatesMax];
|
||||
|
||||
NRangeCoder::CBitTreeDecoder<kNumMoveBits, kNumPosSlotBits> _posSlotDecoder[kNumLenToPosStates];
|
||||
|
||||
NRangeCoder::CReverseBitTreeDecoder2<kNumMoveBits> _posDecoders[kNumPosModels];
|
||||
NRangeCoder::CReverseBitTreeDecoder<kNumMoveBits, kNumAlignBits> _posAlignDecoder;
|
||||
|
||||
NLength::CDecoder _lenDecoder;
|
||||
NLength::CDecoder _repMatchLenDecoder;
|
||||
|
||||
NLiteral::CDecoder _literalDecoder;
|
||||
|
||||
UINT32 _dictionarySize;
|
||||
UINT32 _dictionarySizeCheck;
|
||||
|
||||
UINT32 _posStateMask;
|
||||
|
||||
public:
|
||||
|
||||
MY_UNKNOWN_IMP1(ICompressSetDecoderProperties)
|
||||
|
||||
CDecoder();
|
||||
HRESULT Create();
|
||||
|
||||
|
||||
HRESULT Init(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream);
|
||||
/*
|
||||
void ReleaseStreams()
|
||||
{
|
||||
_outWindowStream.ReleaseStream();
|
||||
_rangeDecoder.ReleaseStream();
|
||||
}
|
||||
*/
|
||||
|
||||
class CDecoderFlusher
|
||||
{
|
||||
CDecoder *_decoder;
|
||||
public:
|
||||
bool NeedFlush;
|
||||
CDecoderFlusher(CDecoder *decoder):
|
||||
_decoder(decoder), NeedFlush(true) {}
|
||||
~CDecoderFlusher()
|
||||
{
|
||||
if (NeedFlush)
|
||||
_decoder->Flush();
|
||||
// _decoder->ReleaseStreams();
|
||||
}
|
||||
};
|
||||
|
||||
HRESULT Flush()
|
||||
{ return _outWindowStream.Flush(); }
|
||||
|
||||
// ICompressCoder interface
|
||||
STDMETHOD(CodeReal)(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
|
||||
ICompressProgressInfo *progress);
|
||||
|
||||
STDMETHOD(Code)(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
|
||||
ICompressProgressInfo *progress);
|
||||
|
||||
// ICompressSetDecoderProperties
|
||||
STDMETHOD(SetDecoderProperties)(ISequentialInStream *inStream);
|
||||
|
||||
HRESULT SetDictionarySize(UINT32 dictionarySize);
|
||||
HRESULT SetLiteralProperties(UINT32 numLiteralPosStateBits,
|
||||
UINT32 numLiteralContextBits);
|
||||
HRESULT SetPosBitsProperties(UINT32 numPosStateBits);
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load diff
|
@ -1,26 +1,36 @@
|
|||
// LZMA/Encoder.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __LZMA_ENCODER_H
|
||||
#define __LZMA_ENCODER_H
|
||||
|
||||
#include "../../ICoder.h"
|
||||
#include "../../../Common/MyCom.h"
|
||||
#include "../../../Common/Alloc.h"
|
||||
#include "../../ICoder.h"
|
||||
#include "../LZ/IMatchFinder.h"
|
||||
#include "../RangeCoder/RangeCoderBitTree.h"
|
||||
|
||||
#include "LZMA.h"
|
||||
#include "LZMALen.h"
|
||||
#include "LZMALiteral.h"
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
|
||||
class CMatchFinderException
|
||||
typedef NRangeCoder::CBitEncoder<kNumMoveBits> CMyBitEncoder;
|
||||
|
||||
class CBaseState
|
||||
{
|
||||
public:
|
||||
HRESULT ErrorCode;
|
||||
CMatchFinderException(HRESULT errorCode): ErrorCode(errorCode) {}
|
||||
protected:
|
||||
CState _state;
|
||||
Byte _previousByte;
|
||||
bool _peviousIsMatch;
|
||||
UInt32 _repDistances[kNumRepDistances];
|
||||
void Init()
|
||||
{
|
||||
_state.Init();
|
||||
_previousByte = 0;
|
||||
_peviousIsMatch = false;
|
||||
for(UInt32 i = 0 ; i < kNumRepDistances; i++)
|
||||
_repDistances[i] = 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct COptimal
|
||||
|
@ -30,21 +40,21 @@ struct COptimal
|
|||
bool Prev1IsChar;
|
||||
bool Prev2;
|
||||
|
||||
UINT32 PosPrev2;
|
||||
UINT32 BackPrev2;
|
||||
UInt32 PosPrev2;
|
||||
UInt32 BackPrev2;
|
||||
|
||||
UINT32 Price;
|
||||
UINT32 PosPrev; // posNext;
|
||||
UINT32 BackPrev;
|
||||
UINT32 Backs[kNumRepDistances];
|
||||
void MakeAsChar() { BackPrev = UINT32(-1); Prev1IsChar = false; }
|
||||
UInt32 Price;
|
||||
UInt32 PosPrev; // posNext;
|
||||
UInt32 BackPrev;
|
||||
UInt32 Backs[kNumRepDistances];
|
||||
void MakeAsChar() { BackPrev = UInt32(-1); Prev1IsChar = false; }
|
||||
void MakeAsShortRep() { BackPrev = 0; ; Prev1IsChar = false; }
|
||||
bool IsShortRep() { return (BackPrev == 0); }
|
||||
};
|
||||
|
||||
|
||||
extern BYTE g_FastPos[1024];
|
||||
inline UINT32 GetPosSlot(UINT32 pos)
|
||||
extern Byte g_FastPos[1024];
|
||||
inline UInt32 GetPosSlot(UInt32 pos)
|
||||
{
|
||||
if (pos < (1 << 10))
|
||||
return g_FastPos[pos];
|
||||
|
@ -53,7 +63,7 @@ inline UINT32 GetPosSlot(UINT32 pos)
|
|||
return g_FastPos[pos >> 18] + 36;
|
||||
}
|
||||
|
||||
inline UINT32 GetPosSlot2(UINT32 pos)
|
||||
inline UInt32 GetPosSlot2(UInt32 pos)
|
||||
{
|
||||
if (pos < (1 << 16))
|
||||
return g_FastPos[pos >> 6] + 12;
|
||||
|
@ -62,79 +72,190 @@ inline UINT32 GetPosSlot2(UINT32 pos)
|
|||
return g_FastPos[pos >> 24] + 48;
|
||||
}
|
||||
|
||||
const UINT32 kIfinityPrice = 0xFFFFFFF;
|
||||
const UInt32 kIfinityPrice = 0xFFFFFFF;
|
||||
|
||||
typedef NRangeCoder::CBitEncoder<kNumMoveBits> CMyBitEncoder;
|
||||
const UInt32 kNumOpts = 1 << 12;
|
||||
|
||||
|
||||
class CLiteralEncoder2
|
||||
{
|
||||
CMyBitEncoder _encoders[0x300];
|
||||
public:
|
||||
void Init()
|
||||
{
|
||||
for (int i = 0; i < 0x300; i++)
|
||||
_encoders[i].Init();
|
||||
}
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, Byte symbol);
|
||||
void EncodeMatched(NRangeCoder::CEncoder *rangeEncoder, Byte matchByte, Byte symbol);
|
||||
UInt32 GetPrice(bool matchMode, Byte matchByte, Byte symbol) const;
|
||||
};
|
||||
|
||||
class CLiteralEncoder
|
||||
{
|
||||
CLiteralEncoder2 *_coders;
|
||||
int _numPrevBits;
|
||||
int _numPosBits;
|
||||
UInt32 _posMask;
|
||||
public:
|
||||
CLiteralEncoder(): _coders(0) {}
|
||||
~CLiteralEncoder() { Free(); }
|
||||
void Free()
|
||||
{
|
||||
MyFree(_coders);
|
||||
_coders = 0;
|
||||
}
|
||||
bool Create(int numPosBits, int numPrevBits)
|
||||
{
|
||||
if (_coders == 0 || (numPosBits + numPrevBits) !=
|
||||
(_numPrevBits + _numPosBits) )
|
||||
{
|
||||
Free();
|
||||
UInt32 numStates = 1 << (numPosBits + numPrevBits);
|
||||
_coders = (CLiteralEncoder2 *)MyAlloc(numStates * sizeof(CLiteralEncoder2));
|
||||
}
|
||||
_numPosBits = numPosBits;
|
||||
_posMask = (1 << numPosBits) - 1;
|
||||
_numPrevBits = numPrevBits;
|
||||
return (_coders != 0);
|
||||
}
|
||||
void Init()
|
||||
{
|
||||
UInt32 numStates = 1 << (_numPrevBits + _numPosBits);
|
||||
for (UInt32 i = 0; i < numStates; i++)
|
||||
_coders[i].Init();
|
||||
}
|
||||
UInt32 GetState(UInt32 pos, Byte prevByte) const
|
||||
{ return ((pos & _posMask) << _numPrevBits) + (prevByte >> (8 - _numPrevBits)); }
|
||||
CLiteralEncoder2 *GetSubCoder(UInt32 pos, Byte prevByte)
|
||||
{ return &_coders[GetState(pos, prevByte)]; }
|
||||
/*
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, UInt32 pos, Byte prevByte,
|
||||
Byte symbol)
|
||||
{ _coders[GetState(pos, prevByte)].Encode(rangeEncoder, symbol); }
|
||||
void EncodeMatched(NRangeCoder::CEncoder *rangeEncoder, UInt32 pos, Byte prevByte,
|
||||
Byte matchByte, Byte symbol)
|
||||
{ _coders[GetState(pos, prevByte)].Encode(rangeEncoder,
|
||||
matchByte, symbol); }
|
||||
*/
|
||||
UInt32 GetPrice(UInt32 pos, Byte prevByte, bool matchMode, Byte matchByte, Byte symbol) const
|
||||
{ return _coders[GetState(pos, prevByte)].GetPrice(matchMode, matchByte, symbol); }
|
||||
};
|
||||
|
||||
namespace NLength {
|
||||
|
||||
class CEncoder
|
||||
{
|
||||
CMyBitEncoder _choice;
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumLowBits> _lowCoder[kNumPosStatesEncodingMax];
|
||||
CMyBitEncoder _choice2;
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumMidBits> _midCoder[kNumPosStatesEncodingMax];
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumHighBits> _highCoder;
|
||||
public:
|
||||
void Init(UInt32 numPosStates);
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, UInt32 symbol, UInt32 posState);
|
||||
UInt32 GetPrice(UInt32 symbol, UInt32 posState) const;
|
||||
};
|
||||
|
||||
const UInt32 kNumSpecSymbols = kNumLowSymbols + kNumMidSymbols;
|
||||
|
||||
class CPriceTableEncoder: public CEncoder
|
||||
{
|
||||
UInt32 _prices[kNumSymbolsTotal][kNumPosStatesEncodingMax];
|
||||
UInt32 _tableSize;
|
||||
UInt32 _counters[kNumPosStatesEncodingMax];
|
||||
public:
|
||||
void SetTableSize(UInt32 tableSize) { _tableSize = tableSize; }
|
||||
UInt32 GetPrice(UInt32 symbol, UInt32 posState) const
|
||||
{ return _prices[symbol][posState]; }
|
||||
void UpdateTable(UInt32 posState)
|
||||
{
|
||||
for (UInt32 len = 0; len < _tableSize; len++)
|
||||
_prices[len][posState] = CEncoder::GetPrice(len, posState);
|
||||
_counters[posState] = _tableSize;
|
||||
}
|
||||
void UpdateTables(UInt32 numPosStates)
|
||||
{
|
||||
for (UInt32 posState = 0; posState < numPosStates; posState++)
|
||||
UpdateTable(posState);
|
||||
}
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, UInt32 symbol, UInt32 posState)
|
||||
{
|
||||
CEncoder::Encode(rangeEncoder, symbol, posState);
|
||||
if (--_counters[posState] == 0)
|
||||
UpdateTable(posState);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
const UINT32 kNumOpts = 1 << 12;
|
||||
|
||||
class CEncoder :
|
||||
public ICompressCoder,
|
||||
// public IInitMatchFinder,
|
||||
public ICompressSetCoderProperties,
|
||||
public ICompressWriteCoderProperties,
|
||||
public CBaseCoder,
|
||||
public CBaseState,
|
||||
public CMyUnknownImp
|
||||
{
|
||||
COptimal _optimum[kNumOpts];
|
||||
public:
|
||||
CMyComPtr<IMatchFinder> _matchFinder; // test it
|
||||
NRangeCoder::CEncoder _rangeEncoder;
|
||||
public:
|
||||
private:
|
||||
|
||||
CMyBitEncoder _mainChoiceEncoders[kNumStates][NLength::kNumPosStatesEncodingMax];
|
||||
CMyBitEncoder _matchChoiceEncoders[kNumStates];
|
||||
CMyBitEncoder _matchRepChoiceEncoders[kNumStates];
|
||||
CMyBitEncoder _matchRep1ChoiceEncoders[kNumStates];
|
||||
CMyBitEncoder _matchRep2ChoiceEncoders[kNumStates];
|
||||
CMyBitEncoder _matchRepShortChoiceEncoders[kNumStates][NLength::kNumPosStatesEncodingMax];
|
||||
CMyBitEncoder _isMatch[kNumStates][NLength::kNumPosStatesEncodingMax];
|
||||
CMyBitEncoder _isRep[kNumStates];
|
||||
CMyBitEncoder _isRepG0[kNumStates];
|
||||
CMyBitEncoder _isRepG1[kNumStates];
|
||||
CMyBitEncoder _isRepG2[kNumStates];
|
||||
CMyBitEncoder _isRep0Long[kNumStates][NLength::kNumPosStatesEncodingMax];
|
||||
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumPosSlotBits> _posSlotEncoder[kNumLenToPosStates];
|
||||
|
||||
NRangeCoder::CReverseBitTreeEncoder2<kNumMoveBits> _posEncoders[kNumPosModels];
|
||||
NRangeCoder::CReverseBitTreeEncoder2<kNumMoveBits> _posAlignEncoder;
|
||||
CMyBitEncoder _posEncoders[kNumFullDistances - kEndPosModelIndex];
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumAlignBits> _posAlignEncoder;
|
||||
|
||||
NLength::CPriceTableEncoder _lenEncoder;
|
||||
NLength::CPriceTableEncoder _repMatchLenEncoder;
|
||||
|
||||
NLiteral::CEncoder _literalEncoder;
|
||||
CLiteralEncoder _literalEncoder;
|
||||
|
||||
UINT32 _matchDistances[kMatchMaxLen + 1];
|
||||
UInt32 _matchDistances[kMatchMaxLen + 1];
|
||||
|
||||
bool _fastMode;
|
||||
bool _maxMode;
|
||||
UInt32 _numFastBytes;
|
||||
UInt32 _longestMatchLength;
|
||||
|
||||
UINT32 _numFastBytes;
|
||||
UINT32 _numFastBytesPrev;
|
||||
UInt32 _additionalOffset;
|
||||
|
||||
UINT32 _longestMatchLength;
|
||||
|
||||
UINT32 _additionalOffset;
|
||||
|
||||
UINT32 _optimumEndIndex;
|
||||
UINT32 _optimumCurrentIndex;
|
||||
UInt32 _optimumEndIndex;
|
||||
UInt32 _optimumCurrentIndex;
|
||||
|
||||
bool _longestMatchWasFound;
|
||||
|
||||
UINT32 _posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
|
||||
UInt32 _posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
|
||||
|
||||
UINT32 _distancesPrices[kNumLenToPosStates][kNumFullDistances];
|
||||
UInt32 _distancesPrices[kNumLenToPosStates][kNumFullDistances];
|
||||
|
||||
UINT32 _alignPrices[kAlignTableSize];
|
||||
UINT32 _alignPriceCount;
|
||||
UInt32 _alignPrices[kAlignTableSize];
|
||||
UInt32 _alignPriceCount;
|
||||
|
||||
UINT32 _dictionarySize;
|
||||
UINT32 _dictionarySizePrev;
|
||||
UInt32 _distTableSize;
|
||||
|
||||
UINT32 _distTableSize;
|
||||
UInt32 _posStateBits;
|
||||
UInt32 _posStateMask;
|
||||
UInt32 _numLiteralPosStateBits;
|
||||
UInt32 _numLiteralContextBits;
|
||||
|
||||
UINT32 _posStateBits;
|
||||
UINT32 _posStateMask;
|
||||
UINT32 _numLiteralPosStateBits;
|
||||
UINT32 _numLiteralContextBits;
|
||||
UInt32 _dictionarySize;
|
||||
|
||||
UINT64 lastPosSlotFillingPos;
|
||||
UINT64 nowPos64;
|
||||
UInt32 _dictionarySizePrev;
|
||||
UInt32 _numFastBytesPrev;
|
||||
|
||||
UInt64 lastPosSlotFillingPos;
|
||||
UInt64 nowPos64;
|
||||
bool _finished;
|
||||
ISequentialInStream *_inStream;
|
||||
|
||||
|
@ -144,58 +265,49 @@ private:
|
|||
#endif
|
||||
|
||||
bool _writeEndMark;
|
||||
|
||||
UINT32 ReadMatchDistances()
|
||||
{
|
||||
UINT32 len = _matchFinder->GetLongestMatch(_matchDistances);
|
||||
if (len == _numFastBytes)
|
||||
len += _matchFinder->GetMatchLen(len, _matchDistances[len],
|
||||
kMatchMaxLen - len);
|
||||
_additionalOffset++;
|
||||
HRESULT result = _matchFinder->MovePos();
|
||||
if (result != S_OK)
|
||||
throw CMatchFinderException(result);
|
||||
return len;
|
||||
}
|
||||
|
||||
void MovePos(UINT32 num);
|
||||
UINT32 GetRepLen1Price(CState state, UINT32 posState) const
|
||||
bool _needReleaseMFStream;
|
||||
|
||||
HRESULT ReadMatchDistances(UInt32 &len);
|
||||
|
||||
HRESULT MovePos(UInt32 num);
|
||||
UInt32 GetRepLen1Price(CState state, UInt32 posState) const
|
||||
{
|
||||
return _matchRepChoiceEncoders[state.Index].GetPrice(0) +
|
||||
_matchRepShortChoiceEncoders[state.Index][posState].GetPrice(0);
|
||||
return _isRepG0[state.Index].GetPrice(0) +
|
||||
_isRep0Long[state.Index][posState].GetPrice(0);
|
||||
}
|
||||
UINT32 GetRepPrice(UINT32 repIndex, UINT32 len, CState state, UINT32 posState) const
|
||||
UInt32 GetRepPrice(UInt32 repIndex, UInt32 len, CState state, UInt32 posState) const
|
||||
{
|
||||
UINT32 price = _repMatchLenEncoder.GetPrice(len - kMatchMinLen, posState);
|
||||
UInt32 price = _repMatchLenEncoder.GetPrice(len - kMatchMinLen, posState);
|
||||
if(repIndex == 0)
|
||||
{
|
||||
price += _matchRepChoiceEncoders[state.Index].GetPrice(0);
|
||||
price += _matchRepShortChoiceEncoders[state.Index][posState].GetPrice(1);
|
||||
price += _isRepG0[state.Index].GetPrice(0);
|
||||
price += _isRep0Long[state.Index][posState].GetPrice(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
price += _matchRepChoiceEncoders[state.Index].GetPrice(1);
|
||||
price += _isRepG0[state.Index].GetPrice(1);
|
||||
if (repIndex == 1)
|
||||
price += _matchRep1ChoiceEncoders[state.Index].GetPrice(0);
|
||||
price += _isRepG1[state.Index].GetPrice(0);
|
||||
else
|
||||
{
|
||||
price += _matchRep1ChoiceEncoders[state.Index].GetPrice(1);
|
||||
price += _matchRep2ChoiceEncoders[state.Index].GetPrice(repIndex - 2);
|
||||
price += _isRepG1[state.Index].GetPrice(1);
|
||||
price += _isRepG2[state.Index].GetPrice(repIndex - 2);
|
||||
}
|
||||
}
|
||||
return price;
|
||||
}
|
||||
/*
|
||||
UINT32 GetPosLen2Price(UINT32 pos, UINT32 posState) const
|
||||
UInt32 GetPosLen2Price(UInt32 pos, UInt32 posState) const
|
||||
{
|
||||
if (pos >= kNumFullDistances)
|
||||
return kIfinityPrice;
|
||||
return _distancesPrices[0][pos] + _lenEncoder.GetPrice(0, posState);
|
||||
}
|
||||
UINT32 GetPosLen3Price(UINT32 pos, UINT32 len, UINT32 posState) const
|
||||
UInt32 GetPosLen3Price(UInt32 pos, UInt32 len, UInt32 posState) const
|
||||
{
|
||||
UINT32 price;
|
||||
UINT32 lenToPosState = GetLenToPosState(len);
|
||||
UInt32 price;
|
||||
UInt32 lenToPosState = GetLenToPosState(len);
|
||||
if (pos < kNumFullDistances)
|
||||
price = _distancesPrices[lenToPosState][pos];
|
||||
else
|
||||
|
@ -204,12 +316,12 @@ private:
|
|||
return price + _lenEncoder.GetPrice(len - kMatchMinLen, posState);
|
||||
}
|
||||
*/
|
||||
UINT32 GetPosLenPrice(UINT32 pos, UINT32 len, UINT32 posState) const
|
||||
UInt32 GetPosLenPrice(UInt32 pos, UInt32 len, UInt32 posState) const
|
||||
{
|
||||
if (len == 2 && pos >= 0x80)
|
||||
return kIfinityPrice;
|
||||
UINT32 price;
|
||||
UINT32 lenToPosState = GetLenToPosState(len);
|
||||
UInt32 price;
|
||||
UInt32 lenToPosState = GetLenToPosState(len);
|
||||
if (pos < kNumFullDistances)
|
||||
price = _distancesPrices[lenToPosState][pos];
|
||||
else
|
||||
|
@ -218,9 +330,9 @@ private:
|
|||
return price + _lenEncoder.GetPrice(len - kMatchMinLen, posState);
|
||||
}
|
||||
|
||||
UINT32 Backward(UINT32 &backRes, UINT32 cur);
|
||||
UINT32 GetOptimum(UINT32 &backRes, UINT32 position);
|
||||
UINT32 GetOptimumFast(UINT32 &backRes, UINT32 position);
|
||||
UInt32 Backward(UInt32 &backRes, UInt32 cur);
|
||||
HRESULT GetOptimum(UInt32 position, UInt32 &backRes, UInt32 &lenRes);
|
||||
HRESULT GetOptimumFast(UInt32 position, UInt32 &backRes, UInt32 &lenRes);
|
||||
|
||||
void FillPosSlotPrices();
|
||||
void FillDistancesPrices();
|
||||
|
@ -228,7 +340,11 @@ private:
|
|||
|
||||
void ReleaseStreams()
|
||||
{
|
||||
_matchFinder->ReleaseStream();
|
||||
if (_matchFinder && _needReleaseMFStream)
|
||||
{
|
||||
_matchFinder->ReleaseStream();
|
||||
_needReleaseMFStream = false;
|
||||
}
|
||||
// _rangeEncoder.ReleaseStream();
|
||||
}
|
||||
|
||||
|
@ -245,21 +361,19 @@ private:
|
|||
};
|
||||
friend class CCoderReleaser;
|
||||
|
||||
void WriteEndMarker(UINT32 posState);
|
||||
void WriteEndMarker(UInt32 posState);
|
||||
|
||||
public:
|
||||
CEncoder();
|
||||
virtual ~CEncoder() {}
|
||||
void SetWriteEndMarkerMode(bool writeEndMarker)
|
||||
{ _writeEndMark= writeEndMarker; }
|
||||
|
||||
HRESULT Create();
|
||||
|
||||
/*MY_UNKNOWN_IMP2(
|
||||
MY_UNKNOWN_IMP2(
|
||||
ICompressSetCoderProperties,
|
||||
ICompressWriteCoderProperties
|
||||
)*/
|
||||
MY_UNKNOWN_IMP
|
||||
)
|
||||
|
||||
STDMETHOD(Init)(
|
||||
ISequentialOutStream *outStream);
|
||||
|
@ -267,18 +381,18 @@ public:
|
|||
// ICompressCoder interface
|
||||
HRESULT SetStreams(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream,
|
||||
const UINT64 *inSize, const UINT64 *outSize);
|
||||
HRESULT CodeOneBlock(UINT64 *inSize, UINT64 *outSize, INT32 *finished);
|
||||
const UInt64 *inSize, const UInt64 *outSize);
|
||||
HRESULT CodeOneBlock(UInt64 *inSize, UInt64 *outSize, Int32 *finished);
|
||||
|
||||
HRESULT CodeReal(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream,
|
||||
const UINT64 *inSize, const UINT64 *outSize,
|
||||
const UInt64 *inSize, const UInt64 *outSize,
|
||||
ICompressProgressInfo *progress);
|
||||
|
||||
// ICompressCoder interface
|
||||
STDMETHOD(Code)(ISequentialInStream *inStream,
|
||||
ISequentialOutStream *outStream,
|
||||
const UINT64 *inSize, const UINT64 *outSize,
|
||||
const UInt64 *inSize, const UInt64 *outSize,
|
||||
ICompressProgressInfo *progress);
|
||||
|
||||
// IInitMatchFinder interface
|
||||
|
@ -286,10 +400,12 @@ public:
|
|||
|
||||
// ICompressSetCoderProperties2
|
||||
STDMETHOD(SetCoderProperties)(const PROPID *propIDs,
|
||||
const PROPVARIANT *properties, UINT32 numProperties);
|
||||
const PROPVARIANT *properties, UInt32 numProperties);
|
||||
|
||||
// ICompressWriteCoderProperties
|
||||
STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
|
||||
|
||||
virtual ~CEncoder() {}
|
||||
};
|
||||
|
||||
}}
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
// LZMALen.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "LZMALen.h"
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
namespace NLength {
|
||||
|
||||
void CEncoder::Init()
|
||||
{
|
||||
_choice.Init();
|
||||
for (UINT32 posState = 0; posState < _numPosStates; posState++)
|
||||
{
|
||||
_lowCoder[posState].Init();
|
||||
_midCoder[posState].Init();
|
||||
}
|
||||
_choice2.Init();
|
||||
_highCoder.Init();
|
||||
}
|
||||
|
||||
void CEncoder::Encode(NRangeCoder::CEncoder *rangeEncoder, UINT32 symbol, UINT32 posState)
|
||||
{
|
||||
if(symbol < kNumLowSymbols)
|
||||
{
|
||||
_choice.Encode(rangeEncoder, 0);
|
||||
_lowCoder[posState].Encode(rangeEncoder, symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
symbol -= kNumLowSymbols;
|
||||
_choice.Encode(rangeEncoder, 1);
|
||||
if(symbol < kNumMidSymbols)
|
||||
{
|
||||
_choice2.Encode(rangeEncoder, 0);
|
||||
_midCoder[posState].Encode(rangeEncoder, symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
_choice2.Encode(rangeEncoder, 1);
|
||||
_highCoder.Encode(rangeEncoder, symbol - kNumMidSymbols);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UINT32 CEncoder::GetPrice(UINT32 symbol, UINT32 posState) const
|
||||
{
|
||||
UINT32 price = 0;
|
||||
if(symbol < kNumLowSymbols)
|
||||
{
|
||||
price += _choice.GetPrice(0);
|
||||
price += _lowCoder[posState].GetPrice(symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
symbol -= kNumLowSymbols;
|
||||
price += _choice.GetPrice(1);
|
||||
if(symbol < kNumMidSymbols)
|
||||
{
|
||||
price += _choice2.GetPrice(0);
|
||||
price += _midCoder[posState].GetPrice(symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
price += _choice2.GetPrice(1);
|
||||
price += _highCoder.GetPrice(symbol - kNumMidSymbols);
|
||||
}
|
||||
}
|
||||
return price;
|
||||
}
|
||||
|
||||
}}}
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
// LenCoder.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __LENCODER_H
|
||||
#define __LENCODER_H
|
||||
|
||||
#include "../RangeCoder/RangeCoderBitTree.h"
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
namespace NLength {
|
||||
|
||||
const int kNumMoveBits = 5;
|
||||
|
||||
const int kNumPosStatesBitsMax = 4;
|
||||
const UINT32 kNumPosStatesMax = (1 << kNumPosStatesBitsMax);
|
||||
|
||||
const int kNumPosStatesBitsEncodingMax = 4;
|
||||
const UINT32 kNumPosStatesEncodingMax = (1 << kNumPosStatesBitsEncodingMax);
|
||||
|
||||
const int kNumLenBits = 3;
|
||||
const UINT32 kNumLowSymbols = 1 << kNumLenBits;
|
||||
const int kNumMidBits = 3;
|
||||
const UINT32 kNumMidSymbols = 1 << kNumMidBits;
|
||||
|
||||
const int kNumHighBits = 8;
|
||||
|
||||
const UINT32 kNumSymbolsTotal = kNumLowSymbols + kNumMidSymbols + (1 << kNumHighBits);
|
||||
|
||||
class CEncoder
|
||||
{
|
||||
NRangeCoder::CBitEncoder<kNumMoveBits> _choice;
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumLenBits> _lowCoder[kNumPosStatesEncodingMax];
|
||||
NRangeCoder::CBitEncoder<kNumMoveBits> _choice2;
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumMidBits> _midCoder[kNumPosStatesEncodingMax];
|
||||
NRangeCoder::CBitTreeEncoder<kNumMoveBits, kNumHighBits> _highCoder;
|
||||
protected:
|
||||
UINT32 _numPosStates;
|
||||
public:
|
||||
void Create(UINT32 numPosStates)
|
||||
{ _numPosStates = numPosStates; }
|
||||
void Init();
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, UINT32 symbol, UINT32 posState);
|
||||
UINT32 GetPrice(UINT32 symbol, UINT32 posState) const;
|
||||
};
|
||||
|
||||
const UINT32 kNumSpecSymbols = kNumLowSymbols + kNumMidSymbols;
|
||||
|
||||
class CPriceTableEncoder: public CEncoder
|
||||
{
|
||||
UINT32 _prices[kNumSymbolsTotal][kNumPosStatesEncodingMax];
|
||||
UINT32 _tableSize;
|
||||
UINT32 _counters[kNumPosStatesEncodingMax];
|
||||
public:
|
||||
void SetTableSize(UINT32 tableSize)
|
||||
{ _tableSize = tableSize; }
|
||||
UINT32 GetPrice(UINT32 symbol, UINT32 posState) const
|
||||
{ return _prices[symbol][posState]; }
|
||||
void UpdateTable(UINT32 posState)
|
||||
{
|
||||
for (UINT32 len = 0; len < _tableSize; len++)
|
||||
_prices[len][posState] = CEncoder::GetPrice(len , posState);
|
||||
_counters[posState] = _tableSize;
|
||||
}
|
||||
void UpdateTables()
|
||||
{
|
||||
for (UINT32 posState = 0; posState < _numPosStates; posState++)
|
||||
UpdateTable(posState);
|
||||
}
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, UINT32 symbol, UINT32 posState)
|
||||
{
|
||||
CEncoder::Encode(rangeEncoder, symbol, posState);
|
||||
if (--_counters[posState] == 0)
|
||||
UpdateTable(posState);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class CDecoder
|
||||
{
|
||||
NRangeCoder::CBitDecoder<kNumMoveBits> _choice;
|
||||
NRangeCoder::CBitTreeDecoder<kNumMoveBits, kNumLenBits> _lowCoder[kNumPosStatesMax];
|
||||
NRangeCoder::CBitDecoder<kNumMoveBits> _choice2;
|
||||
NRangeCoder::CBitTreeDecoder<kNumMoveBits, kNumMidBits> _midCoder[kNumPosStatesMax];
|
||||
NRangeCoder::CBitTreeDecoder<kNumMoveBits, kNumHighBits> _highCoder;
|
||||
UINT32 _numPosStates;
|
||||
public:
|
||||
void Create(UINT32 numPosStates)
|
||||
{ _numPosStates = numPosStates; }
|
||||
void Init()
|
||||
{
|
||||
_choice.Init();
|
||||
for (UINT32 posState = 0; posState < _numPosStates; posState++)
|
||||
{
|
||||
_lowCoder[posState].Init();
|
||||
_midCoder[posState].Init();
|
||||
}
|
||||
_choice2.Init();
|
||||
_highCoder.Init();
|
||||
}
|
||||
UINT32 Decode(NRangeCoder::CDecoder *rangeDecoder, UINT32 posState)
|
||||
{
|
||||
if(_choice.Decode(rangeDecoder) == 0)
|
||||
return _lowCoder[posState].Decode(rangeDecoder);
|
||||
if(_choice2.Decode(rangeDecoder) == 0)
|
||||
return kNumLowSymbols + _midCoder[posState].Decode(rangeDecoder);
|
||||
return kNumLowSymbols + kNumMidSymbols + _highCoder.Decode(rangeDecoder);
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif
|
|
@ -1,69 +0,0 @@
|
|||
// LZMALiteral.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "LZMALiteral.h"
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
namespace NLiteral {
|
||||
|
||||
void CEncoder2::Init()
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
for (int j = 1; j < (1 << 8); j++)
|
||||
_encoders[i][j].Init();
|
||||
}
|
||||
|
||||
void CEncoder2::Encode(NRangeCoder::CEncoder *rangeEncoder,
|
||||
bool matchMode, BYTE matchByte, BYTE symbol)
|
||||
{
|
||||
UINT32 context = 1;
|
||||
bool same = true;
|
||||
for (int i = 7; i >= 0; i--)
|
||||
{
|
||||
UINT32 bit = (symbol >> i) & 1;
|
||||
UINT state;
|
||||
if (matchMode && same)
|
||||
{
|
||||
UINT32 matchBit = (matchByte >> i) & 1;
|
||||
state = 1 + matchBit;
|
||||
same = (matchBit == bit);
|
||||
}
|
||||
else
|
||||
state = 0;
|
||||
_encoders[state][context].Encode(rangeEncoder, bit);
|
||||
context = (context << 1) | bit;
|
||||
}
|
||||
}
|
||||
|
||||
UINT32 CEncoder2::GetPrice(bool matchMode, BYTE matchByte, BYTE symbol) const
|
||||
{
|
||||
UINT32 price = 0;
|
||||
UINT32 context = 1;
|
||||
int i = 7;
|
||||
if (matchMode)
|
||||
{
|
||||
for (; i >= 0; i--)
|
||||
{
|
||||
UINT32 matchBit = (matchByte >> i) & 1;
|
||||
UINT32 bit = (symbol >> i) & 1;
|
||||
price += _encoders[1 + matchBit][context].GetPrice(bit);
|
||||
context = (context << 1) | bit;
|
||||
if (matchBit != bit)
|
||||
{
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (; i >= 0; i--)
|
||||
{
|
||||
UINT32 bit = (symbol >> i) & 1;
|
||||
price += _encoders[0][context].GetPrice(bit);
|
||||
context = (context << 1) | bit;
|
||||
}
|
||||
return price;
|
||||
};
|
||||
|
||||
}}}
|
|
@ -1,166 +0,0 @@
|
|||
// LiteralCoder.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __LITERALCODER_H
|
||||
#define __LITERALCODER_H
|
||||
|
||||
#include "../RangeCoder/RangeCoderBit.h"
|
||||
#include "../RangeCoder/RangeCoderOpt.h"
|
||||
|
||||
namespace NCompress {
|
||||
namespace NLZMA {
|
||||
namespace NLiteral {
|
||||
|
||||
const int kNumMoveBits = 5;
|
||||
|
||||
class CEncoder2
|
||||
{
|
||||
NRangeCoder::CBitEncoder<kNumMoveBits> _encoders[3][1 << 8];
|
||||
public:
|
||||
void Init();
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, bool matchMode, BYTE matchByte, BYTE symbol);
|
||||
UINT32 GetPrice(bool matchMode, BYTE matchByte, BYTE symbol) const;
|
||||
};
|
||||
|
||||
class CDecoder2
|
||||
{
|
||||
NRangeCoder::CBitDecoder<kNumMoveBits> _decoders[3][1 << 8];
|
||||
public:
|
||||
void Init()
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
for (int j = 1; j < (1 << 8); j++)
|
||||
_decoders[i][j].Init();
|
||||
}
|
||||
|
||||
BYTE DecodeNormal(NRangeCoder::CDecoder *rangeDecoder)
|
||||
{
|
||||
UINT32 symbol = 1;
|
||||
RC_INIT_VAR
|
||||
do
|
||||
{
|
||||
// symbol = (symbol << 1) | _decoders[0][symbol].Decode(rangeDecoder);
|
||||
RC_GETBIT(kNumMoveBits, _decoders[0][symbol].Probability, symbol)
|
||||
}
|
||||
while (symbol < 0x100);
|
||||
RC_FLUSH_VAR
|
||||
return symbol;
|
||||
}
|
||||
|
||||
BYTE DecodeWithMatchByte(NRangeCoder::CDecoder *rangeDecoder, BYTE matchByte)
|
||||
{
|
||||
UINT32 symbol = 1;
|
||||
RC_INIT_VAR
|
||||
do
|
||||
{
|
||||
UINT32 matchBit = (matchByte >> 7) & 1;
|
||||
matchByte <<= 1;
|
||||
// UINT32 bit = _decoders[1 + matchBit][symbol].Decode(rangeDecoder);
|
||||
// symbol = (symbol << 1) | bit;
|
||||
UINT32 bit;
|
||||
RC_GETBIT2(kNumMoveBits, _decoders[1 + matchBit][symbol].Probability, symbol,
|
||||
bit = 0, bit = 1)
|
||||
if (matchBit != bit)
|
||||
{
|
||||
while (symbol < 0x100)
|
||||
{
|
||||
// symbol = (symbol << 1) | _decoders[0][symbol].Decode(rangeDecoder);
|
||||
RC_GETBIT(kNumMoveBits, _decoders[0][symbol].Probability, symbol)
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (symbol < 0x100);
|
||||
RC_FLUSH_VAR
|
||||
return symbol;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
const UINT32 kNumPrevByteBits = 1;
|
||||
const UINT32 kNumPrevByteStates = (1 << kNumPrevByteBits);
|
||||
|
||||
inline UINT32 GetLiteralState(BYTE prevByte)
|
||||
{ return (prevByte >> (8 - kNumPrevByteBits)); }
|
||||
*/
|
||||
|
||||
class CEncoder
|
||||
{
|
||||
CEncoder2 *_coders;
|
||||
UINT32 _numPrevBits;
|
||||
UINT32 _numPosBits;
|
||||
UINT32 _posMask;
|
||||
public:
|
||||
CEncoder(): _coders(0) {}
|
||||
~CEncoder() { Free(); }
|
||||
void Free()
|
||||
{
|
||||
delete []_coders;
|
||||
_coders = 0;
|
||||
}
|
||||
void Create(UINT32 numPosBits, UINT32 numPrevBits)
|
||||
{
|
||||
Free();
|
||||
_numPosBits = numPosBits;
|
||||
_posMask = (1 << numPosBits) - 1;
|
||||
_numPrevBits = numPrevBits;
|
||||
UINT32 numStates = 1 << (_numPrevBits + _numPosBits);
|
||||
_coders = new CEncoder2[numStates];
|
||||
}
|
||||
void Init()
|
||||
{
|
||||
UINT32 numStates = 1 << (_numPrevBits + _numPosBits);
|
||||
for (UINT32 i = 0; i < numStates; i++)
|
||||
_coders[i].Init();
|
||||
}
|
||||
UINT32 GetState(UINT32 pos, BYTE prevByte) const
|
||||
{ return ((pos & _posMask) << _numPrevBits) + (prevByte >> (8 - _numPrevBits)); }
|
||||
void Encode(NRangeCoder::CEncoder *rangeEncoder, UINT32 pos, BYTE prevByte,
|
||||
bool matchMode, BYTE matchByte, BYTE symbol)
|
||||
{ _coders[GetState(pos, prevByte)].Encode(rangeEncoder, matchMode,
|
||||
matchByte, symbol); }
|
||||
UINT32 GetPrice(UINT32 pos, BYTE prevByte, bool matchMode, BYTE matchByte, BYTE symbol) const
|
||||
{ return _coders[GetState(pos, prevByte)].GetPrice(matchMode, matchByte, symbol); }
|
||||
};
|
||||
|
||||
class CDecoder
|
||||
{
|
||||
CDecoder2 *_coders;
|
||||
UINT32 _numPrevBits;
|
||||
UINT32 _numPosBits;
|
||||
UINT32 _posMask;
|
||||
public:
|
||||
CDecoder(): _coders(0) {}
|
||||
~CDecoder() { Free(); }
|
||||
void Free()
|
||||
{
|
||||
delete []_coders;
|
||||
_coders = 0;
|
||||
}
|
||||
void Create(UINT32 numPosBits, UINT32 numPrevBits)
|
||||
{
|
||||
Free();
|
||||
_numPosBits = numPosBits;
|
||||
_posMask = (1 << numPosBits) - 1;
|
||||
_numPrevBits = numPrevBits;
|
||||
UINT32 numStates = 1 << (_numPrevBits + _numPosBits);
|
||||
_coders = new CDecoder2[numStates];
|
||||
}
|
||||
void Init()
|
||||
{
|
||||
UINT32 numStates = 1 << (_numPrevBits + _numPosBits);
|
||||
for (UINT32 i = 0; i < numStates; i++)
|
||||
_coders[i].Init();
|
||||
}
|
||||
UINT32 GetState(UINT32 pos, BYTE prevByte) const
|
||||
{ return ((pos & _posMask) << _numPrevBits) + (prevByte >> (8 - _numPrevBits)); }
|
||||
BYTE DecodeNormal(NRangeCoder::CDecoder *rangeDecoder, UINT32 pos, BYTE prevByte)
|
||||
{ return _coders[GetState(pos, prevByte)].DecodeNormal(rangeDecoder); }
|
||||
BYTE DecodeWithMatchByte(NRangeCoder::CDecoder *rangeDecoder, UINT32 pos, BYTE prevByte, BYTE matchByte)
|
||||
{ return _coders[GetState(pos, prevByte)].DecodeWithMatchByte(rangeDecoder, matchByte); }
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif
|
|
@ -1,12 +1,8 @@
|
|||
// stdafx.h
|
||||
// StdAfx.h
|
||||
|
||||
#ifndef __STDAFX_H
|
||||
#define __STDAFX_H
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include "../../../../Platform.h"
|
||||
#endif
|
||||
#include "../../../../Platform.h"
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
// Compress/RangeCoder.h
|
||||
// This code is based on Eugene Shelwien's Rangecoder code
|
||||
|
||||
// #pragma once
|
||||
// Compress/RangeCoder/RangeCoder.h
|
||||
|
||||
#ifndef __COMPRESS_RANGECODER_H
|
||||
#define __COMPRESS_RANGECODER_H
|
||||
|
@ -12,23 +9,24 @@
|
|||
namespace NCompress {
|
||||
namespace NRangeCoder {
|
||||
|
||||
const UINT32 kNumTopBits = 24;
|
||||
const UINT32 kTopValue = (1 << kNumTopBits);
|
||||
const int kNumTopBits = 24;
|
||||
const UInt32 kTopValue = (1 << kNumTopBits);
|
||||
|
||||
class CEncoder
|
||||
{
|
||||
COutBuffer Stream;
|
||||
UINT64 Low;
|
||||
UINT32 Range;
|
||||
UINT32 _ffNum;
|
||||
BYTE _cache;
|
||||
|
||||
UInt64 Low;
|
||||
UInt32 Range;
|
||||
UInt32 _ffNum;
|
||||
Byte _cache;
|
||||
public:
|
||||
COutBuffer Stream;
|
||||
bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
|
||||
|
||||
void Init(ISequentialOutStream *stream)
|
||||
{
|
||||
Stream.Init(stream);
|
||||
Low = 0;
|
||||
Range = UINT32(-1);
|
||||
Range = 0xFFFFFFFF;
|
||||
_ffNum = 0;
|
||||
_cache = 0;
|
||||
}
|
||||
|
@ -40,15 +38,14 @@ public:
|
|||
ShiftLow();
|
||||
}
|
||||
|
||||
HRESULT FlushStream()
|
||||
{ return Stream.Flush(); }
|
||||
HRESULT FlushStream() { return Stream.Flush(); }
|
||||
|
||||
/*
|
||||
void ReleaseStream()
|
||||
{ Stream.ReleaseStream(); }
|
||||
*/
|
||||
|
||||
void Encode(UINT32 start, UINT32 size, UINT32 total)
|
||||
void Encode(UInt32 start, UInt32 size, UInt32 total)
|
||||
{
|
||||
Low += start * (Range /= total);
|
||||
Range *= size;
|
||||
|
@ -60,13 +57,13 @@ public:
|
|||
}
|
||||
|
||||
/*
|
||||
void EncodeDirectBitsDiv(UINT32 value, UINT32 numTotalBits)
|
||||
void EncodeDirectBitsDiv(UInt32 value, UInt32 numTotalBits)
|
||||
{
|
||||
Low += value * (Range >>= numTotalBits);
|
||||
Normalize();
|
||||
}
|
||||
|
||||
void EncodeDirectBitsDiv2(UINT32 value, UINT32 numTotalBits)
|
||||
void EncodeDirectBitsDiv2(UInt32 value, UInt32 numTotalBits)
|
||||
{
|
||||
if (numTotalBits <= kNumBottomBits)
|
||||
EncodeDirectBitsDiv(value, numTotalBits);
|
||||
|
@ -79,19 +76,19 @@ public:
|
|||
*/
|
||||
void ShiftLow()
|
||||
{
|
||||
if (Low < (UINT32)0xFF000000 || UINT32(Low >> 32) == 1)
|
||||
if (Low < (UInt32)0xFF000000 || UInt32(Low >> 32) == 1)
|
||||
{
|
||||
Stream.WriteByte(_cache + BYTE(Low >> 32));
|
||||
Stream.WriteByte(Byte(_cache + Byte(Low >> 32)));
|
||||
for (;_ffNum != 0; _ffNum--)
|
||||
Stream.WriteByte(0xFF + BYTE(Low >> 32));
|
||||
_cache = BYTE(UINT32(Low) >> 24);
|
||||
Stream.WriteByte(Byte(0xFF + Byte(Low >> 32)));
|
||||
_cache = Byte(UInt32(Low) >> 24);
|
||||
}
|
||||
else
|
||||
_ffNum++;
|
||||
Low = UINT32(Low) << 8;
|
||||
Low = UInt32(Low) << 8;
|
||||
}
|
||||
|
||||
void EncodeDirectBits(UINT32 value, UINT32 numTotalBits)
|
||||
void EncodeDirectBits(UInt32 value, int numTotalBits)
|
||||
{
|
||||
for (int i = numTotalBits - 1; i >= 0; i--)
|
||||
{
|
||||
|
@ -106,9 +103,9 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void EncodeBit(UINT32 size0, UINT32 numTotalBits, UINT32 symbol)
|
||||
void EncodeBit(UInt32 size0, UInt32 numTotalBits, UInt32 symbol)
|
||||
{
|
||||
UINT32 newBound = (Range >> numTotalBits) * size0;
|
||||
UInt32 newBound = (Range >> numTotalBits) * size0;
|
||||
if (symbol == 0)
|
||||
Range = newBound;
|
||||
else
|
||||
|
@ -123,16 +120,17 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
UINT64 GetProcessedSize() { return Stream.GetProcessedSize() + _ffNum; }
|
||||
UInt64 GetProcessedSize() { return Stream.GetProcessedSize() + _ffNum; }
|
||||
};
|
||||
|
||||
class CDecoder
|
||||
{
|
||||
public:
|
||||
CInBuffer Stream;
|
||||
UINT32 Range;
|
||||
UINT32 Code;
|
||||
// UINT32 m_Word;
|
||||
UInt32 Range;
|
||||
UInt32 Code;
|
||||
bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
|
||||
|
||||
void Normalize()
|
||||
{
|
||||
while (Range < kTopValue)
|
||||
|
@ -146,19 +144,19 @@ public:
|
|||
{
|
||||
Stream.Init(stream);
|
||||
Code = 0;
|
||||
Range = UINT32(-1);
|
||||
Range = 0xFFFFFFFF;
|
||||
for(int i = 0; i < 5; i++)
|
||||
Code = (Code << 8) | Stream.ReadByte();
|
||||
}
|
||||
|
||||
// void ReleaseStream() { Stream.ReleaseStream(); }
|
||||
|
||||
UINT32 GetThreshold(UINT32 total)
|
||||
UInt32 GetThreshold(UInt32 total)
|
||||
{
|
||||
return (Code) / ( Range /= total);
|
||||
}
|
||||
|
||||
void Decode(UINT32 start, UINT32 size, UINT32 total)
|
||||
void Decode(UInt32 start, UInt32 size)
|
||||
{
|
||||
Code -= start * Range;
|
||||
Range *= size;
|
||||
|
@ -166,31 +164,31 @@ public:
|
|||
}
|
||||
|
||||
/*
|
||||
UINT32 DecodeDirectBitsDiv(UINT32 numTotalBits)
|
||||
UInt32 DecodeDirectBitsDiv(UInt32 numTotalBits)
|
||||
{
|
||||
Range >>= numTotalBits;
|
||||
UINT32 threshold = Code / Range;
|
||||
UInt32 threshold = Code / Range;
|
||||
Code -= threshold * Range;
|
||||
|
||||
Normalize();
|
||||
return threshold;
|
||||
}
|
||||
|
||||
UINT32 DecodeDirectBitsDiv2(UINT32 numTotalBits)
|
||||
UInt32 DecodeDirectBitsDiv2(UInt32 numTotalBits)
|
||||
{
|
||||
if (numTotalBits <= kNumBottomBits)
|
||||
return DecodeDirectBitsDiv(numTotalBits);
|
||||
UINT32 result = DecodeDirectBitsDiv(numTotalBits - kNumBottomBits) << kNumBottomBits;
|
||||
UInt32 result = DecodeDirectBitsDiv(numTotalBits - kNumBottomBits) << kNumBottomBits;
|
||||
return (result | DecodeDirectBitsDiv(kNumBottomBits));
|
||||
}
|
||||
*/
|
||||
|
||||
UINT32 DecodeDirectBits(UINT32 numTotalBits)
|
||||
UInt32 DecodeDirectBits(UInt32 numTotalBits)
|
||||
{
|
||||
UINT32 range = Range;
|
||||
UINT32 code = Code;
|
||||
UINT32 result = 0;
|
||||
for (UINT32 i = numTotalBits; i > 0; i--)
|
||||
UInt32 range = Range;
|
||||
UInt32 code = Code;
|
||||
UInt32 result = 0;
|
||||
for (UInt32 i = numTotalBits; i > 0; i--)
|
||||
{
|
||||
range >>= 1;
|
||||
/*
|
||||
|
@ -201,7 +199,7 @@ public:
|
|||
result |= 1;
|
||||
}
|
||||
*/
|
||||
UINT32 t = (code - range) >> 31;
|
||||
UInt32 t = (code - range) >> 31;
|
||||
code -= range & (t - 1);
|
||||
// range = rangeTmp + ((range & 1) & (1 - t));
|
||||
result = (result << 1) | (1 - t);
|
||||
|
@ -217,10 +215,10 @@ public:
|
|||
return result;
|
||||
}
|
||||
|
||||
UINT32 DecodeBit(UINT32 size0, UINT32 numTotalBits)
|
||||
UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits)
|
||||
{
|
||||
UINT32 newBound = (Range >> numTotalBits) * size0;
|
||||
UINT32 symbol;
|
||||
UInt32 newBound = (Range >> numTotalBits) * size0;
|
||||
UInt32 symbol;
|
||||
if (Code < newBound)
|
||||
{
|
||||
symbol = 0;
|
||||
|
@ -236,7 +234,7 @@ public:
|
|||
return symbol;
|
||||
}
|
||||
|
||||
UINT64 GetProcessedSize() {return Stream.GetProcessedSize(); }
|
||||
UInt64 GetProcessedSize() {return Stream.GetProcessedSize(); }
|
||||
};
|
||||
|
||||
}}
|
||||
|
|
|
@ -7,12 +7,27 @@
|
|||
namespace NCompress {
|
||||
namespace NRangeCoder {
|
||||
|
||||
CPriceTables::CPriceTables()
|
||||
UInt32 CPriceTables::ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
|
||||
static CPriceTables g_PriceTables;
|
||||
|
||||
CPriceTables::CPriceTables() { Init(); }
|
||||
|
||||
void CPriceTables::Init()
|
||||
{
|
||||
const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
|
||||
for(int i = kNumBits - 1; i >= 0; i--)
|
||||
{
|
||||
UInt32 start = 1 << (kNumBits - i - 1);
|
||||
UInt32 end = 1 << (kNumBits - i);
|
||||
for (UInt32 j = start; j < end; j++)
|
||||
ProbPrices[j] = (i << kNumBitPriceShiftBits) +
|
||||
(((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1));
|
||||
}
|
||||
|
||||
/*
|
||||
// simplest: bad solution
|
||||
for(UINT32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
|
||||
StatePrices[i] = kBitPrice;
|
||||
for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
|
||||
ProbPrices[i] = kBitPrice;
|
||||
*/
|
||||
|
||||
/*
|
||||
|
@ -21,20 +36,20 @@ CPriceTables::CPriceTables()
|
|||
// float solution
|
||||
double ln2 = log(double(2));
|
||||
double lnAll = log(double(kBitModelTotal >> kNumMoveReducingBits));
|
||||
for(UINT32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
|
||||
StatePrices[i] = UINT32((fabs(lnAll - log(double(i))) / ln2 + kDummyMultMid) * kBitPrice);
|
||||
for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
|
||||
ProbPrices[i] = UInt32((fabs(lnAll - log(double(i))) / ln2 + kDummyMultMid) * kBitPrice);
|
||||
*/
|
||||
|
||||
/*
|
||||
// experimental, slow, solution:
|
||||
for(UINT32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
|
||||
for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
|
||||
{
|
||||
const int kCyclesBits = 5;
|
||||
const UINT32 kCycles = (1 << kCyclesBits);
|
||||
const UInt32 kCycles = (1 << kCyclesBits);
|
||||
|
||||
UINT32 range = UINT32(-1);
|
||||
UINT32 bitCount = 0;
|
||||
for (UINT32 j = 0; j < kCycles; j++)
|
||||
UInt32 range = UInt32(-1);
|
||||
UInt32 bitCount = 0;
|
||||
for (UInt32 j = 0; j < kCycles; j++)
|
||||
{
|
||||
range >>= (kNumBitModelTotalBits - kNumMoveReducingBits);
|
||||
range *= i;
|
||||
|
@ -55,23 +70,11 @@ CPriceTables::CPriceTables()
|
|||
range -= (1 << 31);
|
||||
}
|
||||
}
|
||||
StatePrices[i] = (bitCount
|
||||
ProbPrices[i] = (bitCount
|
||||
// + (1 << (kCyclesBits - 1))
|
||||
) >> kCyclesBits;
|
||||
}
|
||||
*/
|
||||
|
||||
const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
|
||||
for(int i = kNumBits - 1; i >= 0; i--)
|
||||
{
|
||||
UINT32 start = 1 << (kNumBits - i - 1);
|
||||
UINT32 end = 1 << (kNumBits - i);
|
||||
for (UINT32 j = start; j < end; j++)
|
||||
StatePrices[j] = (i << kNumBitPriceShiftBits) +
|
||||
(((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1));
|
||||
}
|
||||
}
|
||||
|
||||
CPriceTables g_PriceTables;
|
||||
|
||||
}}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
// Compress/RangeCoder/RangeCoderBit.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __COMPRESS_RANGECODER_BIT_TREE_H
|
||||
#define __COMPRESS_RANGECODER_BIT_TREE_H
|
||||
#ifndef __COMPRESS_RANGECODER_BIT_H
|
||||
#define __COMPRESS_RANGECODER_BIT_H
|
||||
|
||||
#include "RangeCoder.h"
|
||||
|
||||
|
@ -11,74 +9,88 @@ namespace NCompress {
|
|||
namespace NRangeCoder {
|
||||
|
||||
const int kNumBitModelTotalBits = 11;
|
||||
const UINT32 kBitModelTotal = (1 << kNumBitModelTotalBits);
|
||||
const UInt32 kBitModelTotal = (1 << kNumBitModelTotalBits);
|
||||
|
||||
const int kNumMoveReducingBits = 2;
|
||||
|
||||
const int kNumBitPriceShiftBits = 6;
|
||||
const UINT32 kBitPrice = 1 << kNumBitPriceShiftBits;
|
||||
const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits;
|
||||
|
||||
class CPriceTables
|
||||
{
|
||||
public:
|
||||
UINT32 StatePrices[kBitModelTotal >> kNumMoveReducingBits];
|
||||
static UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
|
||||
static void Init();
|
||||
CPriceTables();
|
||||
};
|
||||
|
||||
extern CPriceTables g_PriceTables;
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// CBitModel
|
||||
|
||||
template <int aNumMoveBits>
|
||||
template <int numMoveBits>
|
||||
class CBitModel
|
||||
{
|
||||
public:
|
||||
UINT32 Probability;
|
||||
void UpdateModel(UINT32 symbol)
|
||||
UInt32 Prob;
|
||||
void UpdateModel(UInt32 symbol)
|
||||
{
|
||||
/*
|
||||
Probability -= (Probability + ((symbol - 1) & ((1 << aNumMoveBits) - 1))) >> aNumMoveBits;
|
||||
Probability += (1 - symbol) << (kNumBitModelTotalBits - aNumMoveBits);
|
||||
Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits;
|
||||
Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits);
|
||||
*/
|
||||
if (symbol == 0)
|
||||
Probability += (kBitModelTotal - Probability) >> aNumMoveBits;
|
||||
Prob += (kBitModelTotal - Prob) >> numMoveBits;
|
||||
else
|
||||
Probability -= (Probability) >> aNumMoveBits;
|
||||
Prob -= (Prob) >> numMoveBits;
|
||||
}
|
||||
public:
|
||||
void Init() { Probability = kBitModelTotal / 2; }
|
||||
void Init() { Prob = kBitModelTotal / 2; }
|
||||
};
|
||||
|
||||
template <int aNumMoveBits>
|
||||
class CBitEncoder: public CBitModel<aNumMoveBits>
|
||||
template <int numMoveBits>
|
||||
class CBitEncoder: public CBitModel<numMoveBits>
|
||||
{
|
||||
public:
|
||||
void Encode(CEncoder *encoder, UINT32 symbol)
|
||||
void Encode(CEncoder *encoder, UInt32 symbol)
|
||||
{
|
||||
encoder->EncodeBit(Probability, kNumBitModelTotalBits, symbol);
|
||||
UpdateModel(symbol);
|
||||
encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol);
|
||||
this->UpdateModel(symbol);
|
||||
/*
|
||||
UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob;
|
||||
if (symbol == 0)
|
||||
{
|
||||
encoder->Range = newBound;
|
||||
this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
|
||||
}
|
||||
else
|
||||
{
|
||||
encoder->Low += newBound;
|
||||
encoder->Range -= newBound;
|
||||
this->Prob -= (this->Prob) >> numMoveBits;
|
||||
}
|
||||
while (encoder->Range < kTopValue)
|
||||
{
|
||||
encoder->Range <<= 8;
|
||||
encoder->ShiftLow();
|
||||
}
|
||||
*/
|
||||
}
|
||||
UINT32 GetPrice(UINT32 symbol) const
|
||||
UInt32 GetPrice(UInt32 symbol) const
|
||||
{
|
||||
return g_PriceTables.StatePrices[
|
||||
(((Probability - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
|
||||
return CPriceTables::ProbPrices[
|
||||
(((this->Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <int aNumMoveBits>
|
||||
class CBitDecoder: public CBitModel<aNumMoveBits>
|
||||
template <int numMoveBits>
|
||||
class CBitDecoder: public CBitModel<numMoveBits>
|
||||
{
|
||||
public:
|
||||
UINT32 Decode(CDecoder *decoder)
|
||||
UInt32 Decode(CDecoder *decoder)
|
||||
{
|
||||
UINT32 newBound = (decoder->Range >> kNumBitModelTotalBits) * Probability;
|
||||
UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob;
|
||||
if (decoder->Code < newBound)
|
||||
{
|
||||
decoder->Range = newBound;
|
||||
Probability += (kBitModelTotal - Probability) >> aNumMoveBits;
|
||||
this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
|
||||
if (decoder->Range < kTopValue)
|
||||
{
|
||||
decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
|
||||
|
@ -90,7 +102,7 @@ public:
|
|||
{
|
||||
decoder->Range -= newBound;
|
||||
decoder->Code -= newBound;
|
||||
Probability -= (Probability) >> aNumMoveBits;
|
||||
this->Prob -= (this->Prob) >> numMoveBits;
|
||||
if (decoder->Range < kTopValue)
|
||||
{
|
||||
decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
|
||||
|
@ -103,5 +115,4 @@ public:
|
|||
|
||||
}}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
// Compress/RangeCoder/RangeCoderBitTree.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __COMPRESS_RANGECODER_BIT_H
|
||||
#define __COMPRESS_RANGECODER_BIT_H
|
||||
#ifndef __COMPRESS_RANGECODER_BIT_TREE_H
|
||||
#define __COMPRESS_RANGECODER_BIT_TREE_H
|
||||
|
||||
#include "RangeCoderBit.h"
|
||||
#include "RangeCoderOpt.h"
|
||||
|
@ -11,292 +9,154 @@
|
|||
namespace NCompress {
|
||||
namespace NRangeCoder {
|
||||
|
||||
/*
|
||||
template <int numMoveBits> class CMyBitEncoder:
|
||||
public NCompression::NArithmetic::CBitEncoder<numMoveBits> {};
|
||||
template <int numMoveBits> class CMyBitDecoder:
|
||||
public NCompression::NArithmetic::CBitDecoder<numMoveBits> {};
|
||||
*/
|
||||
|
||||
//////////////////////////
|
||||
// CBitTreeEncoder
|
||||
|
||||
template <int numMoveBits, UINT32 NumBitLevels>
|
||||
template <int numMoveBits, UInt32 NumBitLevels>
|
||||
class CBitTreeEncoder
|
||||
{
|
||||
CBitEncoder<numMoveBits> Models[1 << NumBitLevels];
|
||||
public:
|
||||
void Init()
|
||||
{
|
||||
for(UINT32 i = 1; i < (1 << NumBitLevels); i++)
|
||||
for(UInt32 i = 1; i < (1 << NumBitLevels); i++)
|
||||
Models[i].Init();
|
||||
}
|
||||
void Encode(CEncoder *rangeEncoder, UINT32 symbol)
|
||||
void Encode(CEncoder *rangeEncoder, UInt32 symbol)
|
||||
{
|
||||
UINT32 modelIndex = 1;
|
||||
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
|
||||
UInt32 modelIndex = 1;
|
||||
for (UInt32 bitIndex = NumBitLevels; bitIndex > 0 ;)
|
||||
{
|
||||
bitIndex--;
|
||||
UINT32 bit = (symbol >> bitIndex ) & 1;
|
||||
UInt32 bit = (symbol >> bitIndex) & 1;
|
||||
Models[modelIndex].Encode(rangeEncoder, bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
}
|
||||
};
|
||||
UINT32 GetPrice(UINT32 symbol) const
|
||||
void ReverseEncode(CEncoder *rangeEncoder, UInt32 symbol)
|
||||
{
|
||||
UINT32 price = 0;
|
||||
UINT32 modelIndex = 1;
|
||||
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
|
||||
UInt32 modelIndex = 1;
|
||||
for (UInt32 i = 0; i < NumBitLevels; i++)
|
||||
{
|
||||
UInt32 bit = symbol & 1;
|
||||
Models[modelIndex].Encode(rangeEncoder, bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
symbol >>= 1;
|
||||
}
|
||||
}
|
||||
UInt32 GetPrice(UInt32 symbol) const
|
||||
{
|
||||
UInt32 price = 0;
|
||||
UInt32 modelIndex = 1;
|
||||
for (UInt32 bitIndex = NumBitLevels; bitIndex > 0 ;)
|
||||
{
|
||||
bitIndex--;
|
||||
UINT32 bit = (symbol >> bitIndex ) & 1;
|
||||
UInt32 bit = (symbol >> bitIndex) & 1;
|
||||
price += Models[modelIndex].GetPrice(bit);
|
||||
modelIndex = (modelIndex << 1) + bit;
|
||||
}
|
||||
return price;
|
||||
}
|
||||
UInt32 ReverseGetPrice(UInt32 symbol) const
|
||||
{
|
||||
UInt32 price = 0;
|
||||
UInt32 modelIndex = 1;
|
||||
for (UInt32 i = NumBitLevels; i > 0; i--)
|
||||
{
|
||||
UInt32 bit = symbol & 1;
|
||||
symbol >>= 1;
|
||||
price += Models[modelIndex].GetPrice(bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
}
|
||||
return price;
|
||||
}
|
||||
};
|
||||
|
||||
//////////////////////////
|
||||
// CBitTreeDecoder
|
||||
|
||||
template <int numMoveBits, UINT32 NumBitLevels>
|
||||
template <int numMoveBits, UInt32 NumBitLevels>
|
||||
class CBitTreeDecoder
|
||||
{
|
||||
CBitDecoder<numMoveBits> Models[1 << NumBitLevels];
|
||||
public:
|
||||
void Init()
|
||||
{
|
||||
for(UINT32 i = 1; i < (1 << NumBitLevels); i++)
|
||||
for(UInt32 i = 1; i < (1 << NumBitLevels); i++)
|
||||
Models[i].Init();
|
||||
}
|
||||
UINT32 Decode(CDecoder *rangeDecoder)
|
||||
UInt32 Decode(CDecoder *rangeDecoder)
|
||||
{
|
||||
UINT32 modelIndex = 1;
|
||||
UInt32 modelIndex = 1;
|
||||
RC_INIT_VAR
|
||||
for(UINT32 bitIndex = NumBitLevels; bitIndex > 0; bitIndex--)
|
||||
for(UInt32 bitIndex = NumBitLevels; bitIndex > 0; bitIndex--)
|
||||
{
|
||||
// modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder);
|
||||
RC_GETBIT(numMoveBits, Models[modelIndex].Probability, modelIndex)
|
||||
RC_GETBIT(numMoveBits, Models[modelIndex].Prob, modelIndex)
|
||||
}
|
||||
RC_FLUSH_VAR
|
||||
return modelIndex - (1 << NumBitLevels);
|
||||
};
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
// CReverseBitTreeEncoder
|
||||
|
||||
template <int numMoveBits>
|
||||
class CReverseBitTreeEncoder2
|
||||
{
|
||||
CBitEncoder<numMoveBits> *Models;
|
||||
UINT32 NumBitLevels;
|
||||
public:
|
||||
CReverseBitTreeEncoder2(): Models(0) { }
|
||||
~CReverseBitTreeEncoder2() { delete []Models; }
|
||||
void Create(UINT32 numBitLevels)
|
||||
UInt32 ReverseDecode(CDecoder *rangeDecoder)
|
||||
{
|
||||
NumBitLevels = numBitLevels;
|
||||
Models = new CBitEncoder<numMoveBits>[1 << numBitLevels];
|
||||
// return (Models != 0);
|
||||
}
|
||||
void Init()
|
||||
{
|
||||
UINT32 numModels = 1 << NumBitLevels;
|
||||
for(UINT32 i = 1; i < numModels; i++)
|
||||
Models[i].Init();
|
||||
}
|
||||
void Encode(CEncoder *rangeEncoder, UINT32 symbol)
|
||||
{
|
||||
UINT32 modelIndex = 1;
|
||||
for (UINT32 i = 0; i < NumBitLevels; i++)
|
||||
{
|
||||
UINT32 bit = symbol & 1;
|
||||
Models[modelIndex].Encode(rangeEncoder, bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
symbol >>= 1;
|
||||
}
|
||||
}
|
||||
UINT32 GetPrice(UINT32 symbol) const
|
||||
{
|
||||
UINT32 price = 0;
|
||||
UINT32 modelIndex = 1;
|
||||
for (UINT32 i = NumBitLevels; i > 0; i--)
|
||||
{
|
||||
UINT32 bit = symbol & 1;
|
||||
symbol >>= 1;
|
||||
price += Models[modelIndex].GetPrice(bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
}
|
||||
return price;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
template <int numMoveBits, int numBitLevels>
|
||||
class CReverseBitTreeEncoder: public CReverseBitTreeEncoder2<numMoveBits>
|
||||
{
|
||||
public:
|
||||
CReverseBitTreeEncoder()
|
||||
{ Create(numBitLevels); }
|
||||
};
|
||||
*/
|
||||
////////////////////////////////
|
||||
// CReverseBitTreeDecoder
|
||||
|
||||
template <int numMoveBits>
|
||||
class CReverseBitTreeDecoder2
|
||||
{
|
||||
CBitDecoder<numMoveBits> *Models;
|
||||
UINT32 NumBitLevels;
|
||||
public:
|
||||
CReverseBitTreeDecoder2(): Models(0) { }
|
||||
~CReverseBitTreeDecoder2() { delete []Models; }
|
||||
void Create(UINT32 numBitLevels)
|
||||
{
|
||||
NumBitLevels = numBitLevels;
|
||||
Models = new CBitDecoder<numMoveBits>[1 << numBitLevels];
|
||||
// return (Models != 0);
|
||||
}
|
||||
void Init()
|
||||
{
|
||||
UINT32 numModels = 1 << NumBitLevels;
|
||||
for(UINT32 i = 1; i < numModels; i++)
|
||||
Models[i].Init();
|
||||
}
|
||||
UINT32 Decode(CDecoder *rangeDecoder)
|
||||
{
|
||||
UINT32 modelIndex = 1;
|
||||
UINT32 symbol = 0;
|
||||
UInt32 modelIndex = 1;
|
||||
UInt32 symbol = 0;
|
||||
RC_INIT_VAR
|
||||
for(UINT32 bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
|
||||
for(UInt32 bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
|
||||
{
|
||||
// UINT32 bit = Models[modelIndex].Decode(rangeDecoder);
|
||||
// UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
|
||||
// modelIndex <<= 1;
|
||||
// modelIndex += bit;
|
||||
// symbol |= (bit << bitIndex);
|
||||
RC_GETBIT2(numMoveBits, Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << bitIndex))
|
||||
}
|
||||
RC_FLUSH_VAR
|
||||
return symbol;
|
||||
};
|
||||
};
|
||||
////////////////////////////
|
||||
// CReverseBitTreeDecoder2
|
||||
|
||||
template <int numMoveBits, UINT32 NumBitLevels>
|
||||
class CReverseBitTreeDecoder
|
||||
{
|
||||
CBitDecoder<numMoveBits> Models[1 << NumBitLevels];
|
||||
public:
|
||||
void Init()
|
||||
{
|
||||
for(UINT32 i = 1; i < (1 << NumBitLevels); i++)
|
||||
Models[i].Init();
|
||||
}
|
||||
UINT32 Decode(CDecoder *rangeDecoder)
|
||||
{
|
||||
UINT32 modelIndex = 1;
|
||||
UINT32 symbol = 0;
|
||||
RC_INIT_VAR
|
||||
for(UINT32 bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
|
||||
{
|
||||
// UINT32 bit = Models[modelIndex].Decode(rangeDecoder);
|
||||
// modelIndex <<= 1;
|
||||
// modelIndex += bit;
|
||||
// symbol |= (bit << bitIndex);
|
||||
RC_GETBIT2(numMoveBits, Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << bitIndex))
|
||||
RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex))
|
||||
}
|
||||
RC_FLUSH_VAR
|
||||
return symbol;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
//////////////////////////
|
||||
// CBitTreeEncoder2
|
||||
template <int numMoveBits>
|
||||
void ReverseBitTreeEncode(CBitEncoder<numMoveBits> *Models,
|
||||
CEncoder *rangeEncoder, UInt32 NumBitLevels, UInt32 symbol)
|
||||
{
|
||||
UInt32 modelIndex = 1;
|
||||
for (UInt32 i = 0; i < NumBitLevels; i++)
|
||||
{
|
||||
UInt32 bit = symbol & 1;
|
||||
Models[modelIndex].Encode(rangeEncoder, bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
symbol >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
template <int numMoveBits>
|
||||
class CBitTreeEncoder2
|
||||
UInt32 ReverseBitTreeGetPrice(CBitEncoder<numMoveBits> *Models,
|
||||
UInt32 NumBitLevels, UInt32 symbol)
|
||||
{
|
||||
NCompression::NArithmetic::CBitEncoder<numMoveBits> *Models;
|
||||
UINT32 NumBitLevels;
|
||||
public:
|
||||
bool Create(UINT32 numBitLevels)
|
||||
{
|
||||
NumBitLevels = numBitLevels;
|
||||
Models = new NCompression::NArithmetic::CBitEncoder<numMoveBits>[1 << numBitLevels];
|
||||
return (Models != 0);
|
||||
}
|
||||
void Init()
|
||||
UInt32 price = 0;
|
||||
UInt32 modelIndex = 1;
|
||||
for (UInt32 i = NumBitLevels; i > 0; i--)
|
||||
{
|
||||
UINT32 numModels = 1 << NumBitLevels;
|
||||
for(UINT32 i = 1; i < numModels; i++)
|
||||
Models[i].Init();
|
||||
UInt32 bit = symbol & 1;
|
||||
symbol >>= 1;
|
||||
price += Models[modelIndex].GetPrice(bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
}
|
||||
void Encode(CMyRangeEncoder *rangeEncoder, UINT32 symbol)
|
||||
{
|
||||
UINT32 modelIndex = 1;
|
||||
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
|
||||
{
|
||||
bitIndex--;
|
||||
UINT32 bit = (symbol >> bitIndex ) & 1;
|
||||
Models[modelIndex].Encode(rangeEncoder, bit);
|
||||
modelIndex = (modelIndex << 1) | bit;
|
||||
}
|
||||
}
|
||||
UINT32 GetPrice(UINT32 symbol) const
|
||||
{
|
||||
UINT32 price = 0;
|
||||
UINT32 modelIndex = 1;
|
||||
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
|
||||
{
|
||||
bitIndex--;
|
||||
UINT32 bit = (symbol >> bitIndex ) & 1;
|
||||
price += Models[modelIndex].GetPrice(bit);
|
||||
modelIndex = (modelIndex << 1) + bit;
|
||||
}
|
||||
return price;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//////////////////////////
|
||||
// CBitTreeDecoder2
|
||||
return price;
|
||||
}
|
||||
|
||||
template <int numMoveBits>
|
||||
class CBitTreeDecoder2
|
||||
UInt32 ReverseBitTreeDecode(CBitDecoder<numMoveBits> *Models,
|
||||
CDecoder *rangeDecoder, UInt32 NumBitLevels)
|
||||
{
|
||||
NCompression::NArithmetic::CBitDecoder<numMoveBits> *Models;
|
||||
UINT32 NumBitLevels;
|
||||
public:
|
||||
bool Create(UINT32 numBitLevels)
|
||||
{
|
||||
NumBitLevels = numBitLevels;
|
||||
Models = new NCompression::NArithmetic::CBitDecoder<numMoveBits>[1 << numBitLevels];
|
||||
return (Models != 0);
|
||||
}
|
||||
void Init()
|
||||
UInt32 modelIndex = 1;
|
||||
UInt32 symbol = 0;
|
||||
RC_INIT_VAR
|
||||
for(UInt32 bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
|
||||
{
|
||||
UINT32 numModels = 1 << NumBitLevels;
|
||||
for(UINT32 i = 1; i < numModels; i++)
|
||||
Models[i].Init();
|
||||
// UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
|
||||
// modelIndex <<= 1;
|
||||
// modelIndex += bit;
|
||||
// symbol |= (bit << bitIndex);
|
||||
RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex))
|
||||
}
|
||||
UINT32 Decode(CMyRangeDecoder *rangeDecoder)
|
||||
{
|
||||
UINT32 modelIndex = 1;
|
||||
RC_INIT_VAR
|
||||
for(UINT32 bitIndex = NumBitLevels; bitIndex > 0; bitIndex--)
|
||||
{
|
||||
// modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder);
|
||||
RC_GETBIT(numMoveBits, Models[modelIndex].Probability, modelIndex)
|
||||
}
|
||||
RC_FLUSH_VAR
|
||||
return modelIndex - (1 << NumBitLevels);
|
||||
}
|
||||
};
|
||||
*/
|
||||
RC_FLUSH_VAR
|
||||
return symbol;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
|
|
|
@ -1,43 +1,31 @@
|
|||
// Compress/RangeCoder/RangeCoderOpt.h
|
||||
|
||||
// #pragma once
|
||||
|
||||
#ifndef __COMPRESS_RANGECODER_OPT_H
|
||||
#define __COMPRESS_RANGECODER_OPT_H
|
||||
|
||||
#define RC_INIT_VAR \
|
||||
UINT32 range = rangeDecoder->Range; \
|
||||
UINT32 code = rangeDecoder->Code;
|
||||
#define RC_INIT_VAR \
|
||||
UInt32 range = rangeDecoder->Range; \
|
||||
UInt32 code = rangeDecoder->Code;
|
||||
|
||||
#define RC_FLUSH_VAR \
|
||||
rangeDecoder->Range = range; \
|
||||
#define RC_FLUSH_VAR \
|
||||
rangeDecoder->Range = range; \
|
||||
rangeDecoder->Code = code;
|
||||
|
||||
#define RC_NORMALIZE \
|
||||
if (range < NCompress::NRangeCoder::kTopValue) \
|
||||
{ \
|
||||
code = (code << 8) | rangeDecoder->Stream.ReadByte(); \
|
||||
range <<= 8; }
|
||||
#define RC_NORMALIZE \
|
||||
if (range < NCompress::NRangeCoder::kTopValue) \
|
||||
{ code = (code << 8) | rangeDecoder->Stream.ReadByte(); range <<= 8; }
|
||||
|
||||
#define RC_GETBIT2(numMoveBits, prob, modelIndex, Action0, Action1) \
|
||||
{UINT32 newBound = (range >> NCompress::NRangeCoder::kNumBitModelTotalBits) * prob; \
|
||||
if (code < newBound) \
|
||||
{ \
|
||||
Action0; \
|
||||
range = newBound; \
|
||||
prob += (NCompress::NRangeCoder::kBitModelTotal - prob) >> numMoveBits; \
|
||||
modelIndex <<= 1; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
Action1; \
|
||||
range -= newBound; \
|
||||
code -= newBound; \
|
||||
prob -= (prob) >> numMoveBits; \
|
||||
modelIndex = (modelIndex << 1) + 1; \
|
||||
}} \
|
||||
RC_NORMALIZE
|
||||
#define RC_GETBIT2(numMoveBits, prob, mi, A0, A1) \
|
||||
{ UInt32 bound = (range >> NCompress::NRangeCoder::kNumBitModelTotalBits) * prob; \
|
||||
if (code < bound) \
|
||||
{ A0; range = bound; \
|
||||
prob += (NCompress::NRangeCoder::kBitModelTotal - prob) >> numMoveBits; \
|
||||
mi <<= 1; } \
|
||||
else \
|
||||
{ A1; range -= bound; code -= bound; prob -= (prob) >> numMoveBits; \
|
||||
mi = (mi + mi) + 1; }} \
|
||||
RC_NORMALIZE
|
||||
|
||||
#define RC_GETBIT(numMoveBits, prob, modelIndex) RC_GETBIT2(numMoveBits, prob, modelIndex, ; , ;)
|
||||
#define RC_GETBIT(numMoveBits, prob, mi) RC_GETBIT2(numMoveBits, prob, mi, ; , ;)
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
// stdafx.h
|
||||
// StdAfx.h
|
||||
|
||||
#ifndef __STDAFX_H
|
||||
#define __STDAFX_H
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include "../../../../Platform.h"
|
||||
#endif
|
||||
#include "../../../../Platform.h"
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue