summaryrefslogtreecommitdiffstats
path: root/hostTools/lzma/compress/LZMAEncoder.h
blob: 636873837b91a58ce2275d796553c59311a6c282 (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#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;

  UINT32 PosPrev2;
  UINT32 BackPrev2;     

  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 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 UINT32 GetPosSlot2(UINT32 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;
  // CBitTreeEncoder2<kNumMoveBitsForPosCoders> m_PosEncoders[kNumPosModels];
  // CBitTreeEncoder2<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;
  UINT32 m_NumFastBytes;
  UINT32 m_LongestMatchLength;    

  UINT32 m_AdditionalOffset;

  UINT32 m_OptimumEndIndex;
  UINT32 m_OptimumCurrentIndex;

  bool m_LongestMatchWasFound;

  UINT32 m_PosSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
  
  UINT32 m_DistancesPrices[kNumLenToPosStates][kNumFullDistances];

  UINT32 m_AlignPrices[kAlignTableSize];
  UINT32 m_AlignPriceCount;

  UINT32 m_DistTableSize;

  UINT32 m_PosStateBits;
  UINT32 m_PosStateMask;
  UINT32 m_LiteralPosStateBits;
  UINT32 m_LiteralContextBits;

  UINT32 m_DictionarySize;


  UINT32 m_DictionarySizePrev;
  UINT32 m_NumFastBytesPrev;

  
  UINT32 ReadMatchDistances()
  {
    UINT32 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(UINT32 aNum);
  UINT32 GetRepLen1Price(CState aState, UINT32 aPosState) const
  {
    return m_MatchRepChoiceEncoders[aState.m_Index].GetPrice(0) +
        m_MatchRepShortChoiceEncoders[aState.m_Index][aPosState].GetPrice(0);
  }
  UINT32 GetRepPrice(UINT32 aRepIndex, UINT32 aLen, CState aState, UINT32 aPosState) const
  {
    UINT32 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;
  }
  /*
  UINT32 GetPosLen2Price(UINT32 aPos, UINT32 aPosState) const
  {
    if (aPos >= kNumFullDistances)
      return kIfinityPrice;
    return m_DistancesPrices[0][aPos] + m_LenEncoder.GetPrice(0, aPosState);
  }
  UINT32 GetPosLen3Price(UINT32 aPos, UINT32 aLen, UINT32 aPosState) const
  {
    UINT32 aPrice;
    UINT32 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);
  }
  */
  UINT32 GetPosLenPrice(UINT32 aPos, UINT32 aLen, UINT32 aPosState) const
  {
    if (aLen == 2 && aPos >= 0x80)
      return kIfinityPrice;
    UINT32 aPrice;
    UINT32 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);
  }

  UINT32 Backward(UINT32 &aBackRes, UINT32 aCur);
  UINT32 GetOptimum(UINT32 &aBackRes, UINT32 aPosition);
  UINT32 GetOptimumFast(UINT32 &aBackRes, UINT32 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(UINT32 A);
  HRESULT SetEncoderNumFastBytes(UINT32 A);
  HRESULT SetDictionarySize(UINT32 aDictionarySize);
  HRESULT SetLiteralProperties(UINT32 aLiteralPosStateBits, UINT32 aLiteralContextBits);
  HRESULT SetPosBitsProperties(UINT32 aNumPosStateBits);
  HRESULT Code(ISequentialInStream *anInStream, ISequentialOutStream *anOutStream, const UINT64 *anInSize);
  HRESULT WriteCoderProperties(ISequentialOutStream *anOutStream);
};

}}

#endif