1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#ifndef __LZARITHMETIC_ENCODER_H
#define __LZARITHMETIC_ENCODER_H
#include "Portable.h"
#include "AriPrice.h"
#include "LZMA.h"
#include "LenCoder.h"
#include "LiteralCoder.h"
#include "AriConst.h"
// NOTE Here is choosen the MatchFinder
#include "BinTree2.h"
#define MATCH_FINDER NBT2::CMatchFinderBinTree
namespace NCompress {
namespace NLZMA {
struct COptimal
{
CState State;
bool Prev1IsChar;
bool Prev2;
INT PosPrev2;
INT BackPrev2;
INT Price;
INT PosPrev; // posNext;
INT BackPrev;
INT Backs[kNumRepDistances];
void MakeAsChar() { BackPrev = INT(-1); Prev1IsChar = false; }
void MakeAsShortRep() { BackPrev = 0; ; Prev1IsChar = false; }
bool IsShortRep() { return (BackPrev == 0); }
};
extern BYTE g_FastPos[1024];
inline INT GetPosSlot(INT aPos)
{
if (aPos < (1 << 10))
return g_FastPos[aPos];
if (aPos < (1 << 19))
return g_FastPos[aPos >> 9] + 18;
return g_FastPos[aPos >> 18] + 36;
}
inline INT GetPosSlot2(INT aPos)
{
if (aPos < (1 << 16))
return g_FastPos[aPos >> 6] + 12;
if (aPos < (1 << 25))
return g_FastPos[aPos >> 15] + 30;
return g_FastPos[aPos >> 24] + 48;
}
const int kIfinityPrice = 0xFFFFFFF;
typedef CMyBitEncoder<kNumMoveBitsForMainChoice> CMyBitEncoder2;
const int kNumOpts = 1 << 12;
class CEncoder : public CBaseCoder
{
COptimal m_Optimum[kNumOpts];
public:
MATCH_FINDER m_MatchFinder;
CMyRangeEncoder m_RangeEncoder;
private:
CMyBitEncoder2 m_MainChoiceEncoders[kNumStates][NLength::kNumPosStatesEncodingMax];
CMyBitEncoder2 m_MatchChoiceEncoders[kNumStates];
CMyBitEncoder2 m_MatchRepChoiceEncoders[kNumStates];
CMyBitEncoder2 m_MatchRep1ChoiceEncoders[kNumStates];
CMyBitEncoder2 m_MatchRep2ChoiceEncoders[kNumStates];
CMyBitEncoder2 m_MatchRepShortChoiceEncoders[kNumStates][NLength::kNumPosStatesEncodingMax];
CBitTreeEncoder<kNumMoveBitsForPosSlotCoder, kNumPosSlotBits> m_PosSlotEncoder[kNumLenToPosStates];
CReverseBitTreeEncoder2<kNumMoveBitsForPosCoders> m_PosEncoders[kNumPosModels];
CReverseBitTreeEncoder2<kNumMoveBitsForAlignCoders> m_PosAlignEncoder;
NLength::CPriceTableEncoder m_LenEncoder;
NLength::CPriceTableEncoder m_RepMatchLenEncoder;
NLiteral::CEncoder m_LiteralEncoder;
UINT32 m_MatchDistances[kMatchMaxLen + 1];
bool m_FastMode;
bool m_MaxMode;
INT m_NumFastBytes;
INT m_LongestMatchLength;
INT m_AdditionalOffset;
INT m_OptimumEndIndex;
INT m_OptimumCurrentIndex;
bool m_LongestMatchWasFound;
INT m_PosSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
INT m_DistancesPrices[kNumLenToPosStates][kNumFullDistances];
INT m_AlignPrices[kAlignTableSize];
INT m_AlignPriceCount;
INT m_DistTableSize;
INT m_PosStateBits;
INT m_PosStateMask;
INT m_LiteralPosStateBits;
INT m_LiteralContextBits;
INT m_DictionarySize;
INT m_DictionarySizePrev;
INT m_NumFastBytesPrev;
INT ReadMatchDistances()
{
INT aLen = m_MatchFinder.GetLongestMatch(m_MatchDistances);
if (aLen == m_NumFastBytes)
aLen += m_MatchFinder.GetMatchLen(aLen, m_MatchDistances[aLen],
kMatchMaxLen - aLen);
m_AdditionalOffset++;
HRESULT aResult = m_MatchFinder.MovePos();
if (aResult != S_OK)
throw aResult;
return aLen;
}
void MovePos(INT aNum);
INT GetRepLen1Price(CState aState, INT aPosState) const
{
return m_MatchRepChoiceEncoders[aState.m_Index].GetPrice(0) +
m_MatchRepShortChoiceEncoders[aState.m_Index][aPosState].GetPrice(0);
}
INT GetRepPrice(INT aRepIndex, INT aLen, CState aState, INT aPosState) const
{
INT aPrice = m_RepMatchLenEncoder.GetPrice(aLen - kMatchMinLen, aPosState);
if(aRepIndex == 0)
{
aPrice += m_MatchRepChoiceEncoders[aState.m_Index].GetPrice(0);
aPrice += m_MatchRepShortChoiceEncoders[aState.m_Index][aPosState].GetPrice(1);
}
else
{
aPrice += m_MatchRepChoiceEncoders[aState.m_Index].GetPrice(1);
if (aRepIndex == 1)
aPrice += m_MatchRep1ChoiceEncoders[aState.m_Index].GetPrice(0);
else
{
aPrice += m_MatchRep1ChoiceEncoders[aState.m_Index].GetPrice(1);
aPrice += m_MatchRep2ChoiceEncoders[aState.m_Index].GetPrice(aRepIndex - 2);
}
}
return aPrice;
}
INT GetPosLenPrice(INT aPos, INT aLen, INT aPosState) const
{
if (aLen == 2 && aPos >= 0x80)
return kIfinityPrice;
INT aPrice;
INT aLenToPosState = GetLenToPosState(aLen);
if (aPos < kNumFullDistances)
aPrice = m_DistancesPrices[aLenToPosState][aPos];
else
aPrice = m_PosSlotPrices[aLenToPosState][GetPosSlot2(aPos)] +
m_AlignPrices[aPos & kAlignMask];
return aPrice + m_LenEncoder.GetPrice(aLen - kMatchMinLen, aPosState);
}
INT Backward(INT &aBackRes, INT aCur);
INT GetOptimum(INT &aBackRes, INT aPosition);
INT GetOptimumFast(INT &aBackRes, INT aPosition);
void FillPosSlotPrices();
void FillDistancesPrices();
void FillAlignPrices();
HRESULT Flush();
HRESULT Create();
HRESULT CodeReal(ISequentialInStream *anInStream, ISequentialOutStream *anOutStream, const UINT64 *anInSize);
HRESULT Init(ISequentialInStream *anInStream, ISequentialOutStream *anOutStream);
public:
CEncoder();
HRESULT SetEncoderAlgorithm(INT A);
HRESULT SetEncoderNumFastBytes(INT A);
HRESULT SetDictionarySize(INT aDictionarySize);
HRESULT SetLiteralProperties(INT aLiteralPosStateBits, INT aLiteralContextBits);
HRESULT SetPosBitsProperties(INT aNumPosStateBits);
HRESULT Code(ISequentialInStream *anInStream, ISequentialOutStream *anOutStream, const UINT64 *anInSize);
HRESULT WriteCoderProperties(ISequentialOutStream *anOutStream);
};
}}
#endif
|