summaryrefslogtreecommitdiff
path: root/other-licenses/7zstub/src/7zip/Compress/RangeCoder/RangeCoderBitTree.h
blob: 1fa023f3b8f5d539a3b57dc929079b3ed1e13143 (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
// Compress/RangeCoder/RangeCoderBitTree.h

#ifndef __COMPRESS_RANGECODER_BIT_TREE_H
#define __COMPRESS_RANGECODER_BIT_TREE_H

#include "RangeCoderBit.h"
#include "RangeCoderOpt.h"

namespace NCompress {
namespace NRangeCoder {

template <int numMoveBits, int NumBitLevels>
class CBitTreeEncoder
{
  CBitEncoder<numMoveBits> Models[1 << NumBitLevels];
public:
  void Init()
  {
    for(UInt32 i = 1; i < (1 << NumBitLevels); i++)
      Models[i].Init();
  }
  void Encode(CEncoder *rangeEncoder, UInt32 symbol)
  {
    UInt32 modelIndex = 1;
    for (int bitIndex = NumBitLevels; bitIndex != 0 ;)
    {
      bitIndex--;
      UInt32 bit = (symbol >> bitIndex) & 1;
      Models[modelIndex].Encode(rangeEncoder, bit);
      modelIndex = (modelIndex << 1) | bit;
    }
  };
  void ReverseEncode(CEncoder *rangeEncoder, UInt32 symbol)
  {
    UInt32 modelIndex = 1;
    for (int 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
  {
    symbol |= (1 << NumBitLevels);
    UInt32 price = 0;
    while (symbol != 1)
    {
      price += Models[symbol >> 1].GetPrice(symbol & 1);
      symbol >>= 1;
    }
    return price;
  }
  UInt32 ReverseGetPrice(UInt32 symbol) const
  {
    UInt32 price = 0;
    UInt32 modelIndex = 1;
    for (int 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 CBitTreeDecoder
{
  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;
    RC_INIT_VAR
    for(int bitIndex = NumBitLevels; bitIndex != 0; bitIndex--)
    {
      // modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder);
      RC_GETBIT(numMoveBits, Models[modelIndex].Prob, modelIndex)
    }
    RC_FLUSH_VAR
    return modelIndex - (1 << NumBitLevels);
  };
  UInt32 ReverseDecode(CDecoder *rangeDecoder)
  {
    UInt32 modelIndex = 1;
    UInt32 symbol = 0;
    RC_INIT_VAR
    for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
    {
      // UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
      // modelIndex <<= 1;
      // modelIndex += bit;
      // symbol |= (bit << bitIndex);
      RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex))
    }
    RC_FLUSH_VAR
    return symbol;
  }
};

template <int numMoveBits>
void ReverseBitTreeEncode(CBitEncoder<numMoveBits> *Models, 
    CEncoder *rangeEncoder, int NumBitLevels, UInt32 symbol)
{
  UInt32 modelIndex = 1;
  for (int i = 0; i < NumBitLevels; i++)
  {
    UInt32 bit = symbol & 1;
    Models[modelIndex].Encode(rangeEncoder, bit);
    modelIndex = (modelIndex << 1) | bit;
    symbol >>= 1;
  }
}

template <int numMoveBits>
UInt32 ReverseBitTreeGetPrice(CBitEncoder<numMoveBits> *Models, 
    UInt32 NumBitLevels, UInt32 symbol)
{
  UInt32 price = 0;
  UInt32 modelIndex = 1;
  for (int 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>
UInt32 ReverseBitTreeDecode(CBitDecoder<numMoveBits> *Models, 
    CDecoder *rangeDecoder, int NumBitLevels)
{
  UInt32 modelIndex = 1;
  UInt32 symbol = 0;
  RC_INIT_VAR
  for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
  {
    // UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
    // modelIndex <<= 1;
    // modelIndex += bit;
    // symbol |= (bit << bitIndex);
    RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex))
  }
  RC_FLUSH_VAR
  return symbol;
}

}}

#endif