summaryrefslogtreecommitdiffstats
path: root/hostTools/lzma/compress/LiteralCoder.h
blob: 147bf03e3bba788300b05bc964f2f1b87bd3ebee (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
#ifndef __LITERALCODER_H
#define __LITERALCODER_H

#include "AriBitCoder.h"
#include "RCDefs.h"

namespace NLiteral {

const int kNumMoveBits = 5;

class CEncoder2
{
  CMyBitEncoder<kNumMoveBits> m_Encoders[3][1 << 8];
public:
  void Init();
  void Encode(CMyRangeEncoder *aRangeEncoder, bool aMatchMode, BYTE aMatchByte, BYTE aSymbol);
  UINT32 GetPrice(bool aMatchMode, BYTE aMatchByte, BYTE aSymbol) const;
};

class CDecoder2
{
  CMyBitDecoder<kNumMoveBits> m_Decoders[3][1 << 8];
public:
  void Init()
  {
    for (int i = 0; i < 3; i++)
      for (int j = 1; j < (1 << 8); j++)
        m_Decoders[i][j].Init();
  }

  BYTE DecodeNormal(CMyRangeDecoder *aRangeDecoder)
  {
    UINT32 aSymbol = 1;
    RC_INIT_VAR
    do
    {
      // aSymbol = (aSymbol << 1) | m_Decoders[0][aSymbol].Decode(aRangeDecoder);
      RC_GETBIT(kNumMoveBits, m_Decoders[0][aSymbol].m_Probability, aSymbol)
    }
    while (aSymbol < 0x100);
    RC_FLUSH_VAR
    return aSymbol;
  }

  BYTE DecodeWithMatchByte(CMyRangeDecoder *aRangeDecoder, BYTE aMatchByte)
  {
    UINT32 aSymbol = 1;
    RC_INIT_VAR
    do
    {
      UINT32 aMatchBit = (aMatchByte >> 7) & 1;
      aMatchByte <<= 1;
      // UINT32 aBit = m_Decoders[1 + aMatchBit][aSymbol].Decode(aRangeDecoder);
      // aSymbol = (aSymbol << 1) | aBit;
      UINT32 aBit;
      RC_GETBIT2(kNumMoveBits, m_Decoders[1 + aMatchBit][aSymbol].m_Probability, aSymbol, 
          aBit = 0, aBit = 1)
      if (aMatchBit != aBit)
      {
        while (aSymbol < 0x100)
        {
          // aSymbol = (aSymbol << 1) | m_Decoders[0][aSymbol].Decode(aRangeDecoder);
          RC_GETBIT(kNumMoveBits, m_Decoders[0][aSymbol].m_Probability, aSymbol)
        }
        break;
      }
    }
    while (aSymbol < 0x100);
    RC_FLUSH_VAR
    return aSymbol;
  }
};

/*
const UINT32 kNumPrevByteBits = 1;
const UINT32 kNumPrevByteStates =  (1 << kNumPrevByteBits);

inline UINT32 GetLiteralState(BYTE aPrevByte)
  { return (aPrevByte >> (8 - kNumPrevByteBits)); }
*/

class CEncoder
{
  CEncoder2 *m_Coders;
  UINT32 m_NumPrevBits;
  UINT32 m_NumPosBits;
  UINT32 m_PosMask;
public:
  CEncoder(): m_Coders(0) {}
  ~CEncoder()  { Free(); }
  void Free()
  { 
    delete []m_Coders;
    m_Coders = 0;
  }
  void Create(UINT32 aNumPosBits, UINT32 aNumPrevBits)
  {
    Free();
    m_NumPosBits = aNumPosBits;
    m_PosMask = (1 << aNumPosBits) - 1;
    m_NumPrevBits = aNumPrevBits;
    UINT32 aNumStates = 1 << (m_NumPrevBits + m_NumPosBits);
    m_Coders = new CEncoder2[aNumStates];
  }
  void Init()
  {
    UINT32 aNumStates = 1 << (m_NumPrevBits + m_NumPosBits);
    for (UINT32 i = 0; i < aNumStates; i++)
      m_Coders[i].Init();
  }
  UINT32 GetState(UINT32 aPos, BYTE aPrevByte) const
    { return ((aPos & m_PosMask) << m_NumPrevBits) + (aPrevByte >> (8 - m_NumPrevBits)); }
  void Encode(CMyRangeEncoder *aRangeEncoder, UINT32 aPos, BYTE aPrevByte, 
      bool aMatchMode, BYTE aMatchByte, BYTE aSymbol)
    { m_Coders[GetState(aPos, aPrevByte)].Encode(aRangeEncoder, aMatchMode, 
          aMatchByte, aSymbol); }
  UINT32 GetPrice(UINT32 aPos, BYTE aPrevByte, bool aMatchMode, BYTE aMatchByte, BYTE aSymbol) const
    { return m_Coders[GetState(aPos, aPrevByte)].GetPrice(aMatchMode, aMatchByte, aSymbol); }
};

class CDecoder
{
  CDecoder2 *m_Coders;
  UINT32 m_NumPrevBits;
  UINT32 m_NumPosBits;
  UINT32 m_PosMask;
public:
  CDecoder(): m_Coders(0) {}
  ~CDecoder()  { Free(); }
  void Free()
  { 
    delete []m_Coders;
    m_Coders = 0;
  }
  void Create(UINT32 aNumPosBits, UINT32 aNumPrevBits)
  {
    Free();
    m_NumPosBits = aNumPosBits;
    m_PosMask = (1 << aNumPosBits) - 1;
    m_NumPrevBits = aNumPrevBits;
    UINT32 aNumStates = 1 << (m_NumPrevBits + m_NumPosBits);
    m_Coders = new CDecoder2[aNumStates];
  }
  void Init()
  {
    UINT32 aNumStates = 1 << (m_NumPrevBits + m_NumPosBits);
    for (UINT32 i = 0; i < aNumStates; i++)
      m_Coders[i].Init();
  }
  UINT32 GetState(UINT32 aPos, BYTE aPrevByte) const
    { return ((aPos & m_PosMask) << m_NumPrevBits) + (aPrevByte >> (8 - m_NumPrevBits)); }
  BYTE DecodeNormal(CMyRangeDecoder *aRangeDecoder, UINT32 aPos, BYTE aPrevByte)
    { return m_Coders[GetState(aPos, aPrevByte)].DecodeNormal(aRangeDecoder); }
  BYTE DecodeWithMatchByte(CMyRangeDecoder *aRangeDecoder, UINT32 aPos, BYTE aPrevByte, BYTE aMatchByte)
    { return m_Coders[GetState(aPos, aPrevByte)].DecodeWithMatchByte(aRangeDecoder, aMatchByte); }
};

}

#endif