summaryrefslogtreecommitdiffstats
path: root/hostTools/lzma/compress/LiteralCoder.cpp
blob: fdb4fe3d027e2d15429d5472c22553d2fe0bf3e4 (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
#include "LiteralCoder.h"

using namespace NCompression;
using namespace NArithmetic;

namespace NLiteral {

void CEncoder2::Init()
{
  for (int i = 0; i < 3; i++)
    for (int j = 1; j < (1 << 8); j++)
      m_Encoders[i][j].Init();
}

void CEncoder2::Encode(CMyRangeEncoder *aRangeEncoder, 
    bool aMatchMode, BYTE aMatchByte, BYTE aSymbol)
{
  UINT32 aContext = 1;
  bool aSame = true;
  for (int i = 7; i >= 0; i--)
  {
    UINT32 aBit = (aSymbol >> i) & 1;
    unsigned aState;
    if (aMatchMode && aSame)
    {
      UINT32 aMatchBit = (aMatchByte >> i) & 1;
      aState = 1 + aMatchBit;
      aSame = (aMatchBit == aBit);
    }
    else
      aState = 0;
    m_Encoders[aState][aContext].Encode(aRangeEncoder, aBit);
    aContext = (aContext << 1) | aBit;
  }
}

UINT32 CEncoder2::GetPrice(bool aMatchMode, BYTE aMatchByte, BYTE aSymbol) const
{
  UINT32 aPrice = 0;
  UINT32 aContext = 1;
  int i = 7;
  if (aMatchMode)
  {
    for (; i >= 0; i--)
    {
      UINT32 aMatchBit = (aMatchByte >> i) & 1;
      UINT32 aBit = (aSymbol >> i) & 1;
      aPrice += m_Encoders[1 + aMatchBit][aContext].GetPrice(aBit);
      aContext = (aContext << 1) | aBit;
      if (aMatchBit != aBit)
      {
        i--;
        break;
      }
    }
  }
  for (; i >= 0; i--)
  {
    UINT32 aBit = (aSymbol >> i) & 1;
    aPrice += m_Encoders[0][aContext].GetPrice(aBit);
    aContext = (aContext << 1) | aBit;
  }
  return aPrice;
};

}