64538e687948f1b8e50ab975636cac9460335609
[coreboot.git] / payloads / bayou / util / pbuilder / lzma / C / 7zip / Compress / RangeCoder / RangeCoderBit.h
1 // Compress/RangeCoder/RangeCoderBit.h\r
2 \r
3 #ifndef __COMPRESS_RANGECODER_BIT_H\r
4 #define __COMPRESS_RANGECODER_BIT_H\r
5 \r
6 #include "RangeCoder.h"\r
7 \r
8 namespace NCompress {\r
9 namespace NRangeCoder {\r
10 \r
11 const int kNumBitModelTotalBits  = 11;\r
12 const UInt32 kBitModelTotal = (1 << kNumBitModelTotalBits);\r
13 \r
14 const int kNumMoveReducingBits = 2;\r
15 \r
16 const int kNumBitPriceShiftBits = 6;\r
17 const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits;\r
18 \r
19 class CPriceTables\r
20 {\r
21 public:\r
22   static UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];\r
23   static void Init();\r
24   CPriceTables();\r
25 };\r
26 \r
27 template <int numMoveBits>\r
28 class CBitModel\r
29 {\r
30 public:\r
31   UInt32 Prob;\r
32   void UpdateModel(UInt32 symbol)\r
33   {\r
34     /*\r
35     Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits;\r
36     Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits);\r
37     */\r
38     if (symbol == 0)\r
39       Prob += (kBitModelTotal - Prob) >> numMoveBits;\r
40     else\r
41       Prob -= (Prob) >> numMoveBits;\r
42   }\r
43 public:\r
44   void Init() { Prob = kBitModelTotal / 2; }\r
45 };\r
46 \r
47 template <int numMoveBits>\r
48 class CBitEncoder: public CBitModel<numMoveBits>\r
49 {\r
50 public:\r
51   void Encode(CEncoder *encoder, UInt32 symbol)\r
52   {\r
53     /*\r
54     encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol);\r
55     this->UpdateModel(symbol);\r
56     */\r
57     UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob;\r
58     if (symbol == 0)\r
59     {\r
60       encoder->Range = newBound;\r
61       this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;\r
62     }\r
63     else\r
64     {\r
65       encoder->Low += newBound;\r
66       encoder->Range -= newBound;\r
67       this->Prob -= (this->Prob) >> numMoveBits;\r
68     }\r
69     if (encoder->Range < kTopValue)\r
70     {\r
71       encoder->Range <<= 8;\r
72       encoder->ShiftLow();\r
73     }\r
74   }\r
75   UInt32 GetPrice(UInt32 symbol) const\r
76   {\r
77     return CPriceTables::ProbPrices[\r
78       (((this->Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];\r
79   }\r
80   UInt32 GetPrice0() const { return CPriceTables::ProbPrices[this->Prob >> kNumMoveReducingBits]; }\r
81   UInt32 GetPrice1() const { return CPriceTables::ProbPrices[(kBitModelTotal - this->Prob) >> kNumMoveReducingBits]; }\r
82 };\r
83 \r
84 \r
85 template <int numMoveBits>\r
86 class CBitDecoder: public CBitModel<numMoveBits>\r
87 {\r
88 public:\r
89   UInt32 Decode(CDecoder *decoder)\r
90   {\r
91     UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob;\r
92     if (decoder->Code < newBound)\r
93     {\r
94       decoder->Range = newBound;\r
95       this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;\r
96       if (decoder->Range < kTopValue)\r
97       {\r
98         decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();\r
99         decoder->Range <<= 8;\r
100       }\r
101       return 0;\r
102     }\r
103     else\r
104     {\r
105       decoder->Range -= newBound;\r
106       decoder->Code -= newBound;\r
107       this->Prob -= (this->Prob) >> numMoveBits;\r
108       if (decoder->Range < kTopValue)\r
109       {\r
110         decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();\r
111         decoder->Range <<= 8;\r
112       }\r
113       return 1;\r
114     }\r
115   }\r
116 };\r
117 \r
118 }}\r
119 \r
120 #endif\r