view src/win32/7zip/7z/CPP/7zip/Compress/RangeCoderBit.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // Compress/RangeCoderBit.h
3 #ifndef __COMPRESS_RANGE_CODER_BIT_H
4 #define __COMPRESS_RANGE_CODER_BIT_H
6 #include "RangeCoder.h"
8 namespace NCompress {
9 namespace NRangeCoder {
11 const int kNumBitModelTotalBits = 11;
12 const UInt32 kBitModelTotal = (1 << kNumBitModelTotalBits);
14 const int kNumMoveReducingBits = 4;
16 const int kNumBitPriceShiftBits = 4;
17 const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits;
19 extern UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
21 template <int numMoveBits>
22 class CBitModel
23 {
24 public:
25 UInt32 Prob;
26 void UpdateModel(UInt32 symbol)
27 {
28 /*
29 Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits;
30 Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits);
31 */
32 if (symbol == 0)
33 Prob += (kBitModelTotal - Prob) >> numMoveBits;
34 else
35 Prob -= (Prob) >> numMoveBits;
36 }
37 public:
38 void Init() { Prob = kBitModelTotal / 2; }
39 };
41 template <int numMoveBits>
42 class CBitEncoder: public CBitModel<numMoveBits>
43 {
44 public:
45 void Encode(CEncoder *encoder, UInt32 symbol)
46 {
47 /*
48 encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol);
49 this->UpdateModel(symbol);
50 */
51 UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob;
52 if (symbol == 0)
53 {
54 encoder->Range = newBound;
55 this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
56 }
57 else
58 {
59 encoder->Low += newBound;
60 encoder->Range -= newBound;
61 this->Prob -= (this->Prob) >> numMoveBits;
62 }
63 if (encoder->Range < kTopValue)
64 {
65 encoder->Range <<= 8;
66 encoder->ShiftLow();
67 }
68 }
69 UInt32 GetPrice(UInt32 symbol) const
70 {
71 return ProbPrices[(this->Prob ^ ((-(int)symbol)) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
72 }
73 UInt32 GetPrice0() const { return ProbPrices[this->Prob >> kNumMoveReducingBits]; }
74 UInt32 GetPrice1() const { return ProbPrices[(this->Prob ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]; }
75 };
78 template <int numMoveBits>
79 class CBitDecoder: public CBitModel<numMoveBits>
80 {
81 public:
82 UInt32 Decode(CDecoder *decoder)
83 {
84 UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob;
85 if (decoder->Code < newBound)
86 {
87 decoder->Range = newBound;
88 this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits;
89 if (decoder->Range < kTopValue)
90 {
91 decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
92 decoder->Range <<= 8;
93 }
94 return 0;
95 }
96 else
97 {
98 decoder->Range -= newBound;
99 decoder->Code -= newBound;
100 this->Prob -= (this->Prob) >> numMoveBits;
101 if (decoder->Range < kTopValue)
102 {
103 decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte();
104 decoder->Range <<= 8;
105 }
106 return 1;
107 }
108 }
109 };
111 }}
113 #endif