1 // Compress/RangeCoder/RangeCoderBitTree.h
3 #ifndef __COMPRESS_RANGECODER_BIT_TREE_H
4 #define __COMPRESS_RANGECODER_BIT_TREE_H
6 #include "RangeCoderBit.h"
7 #include "RangeCoderOpt.h"
10 namespace NRangeCoder
{
12 template <int numMoveBits
, int NumBitLevels
>
15 CBitEncoder
<numMoveBits
> Models
[1 << NumBitLevels
];
19 for(UInt32 i
= 1; i
< (1 << NumBitLevels
); i
++)
22 void Encode(CEncoder
*rangeEncoder
, UInt32 symbol
)
24 UInt32 modelIndex
= 1;
25 for (int bitIndex
= NumBitLevels
; bitIndex
!= 0 ;)
28 UInt32 bit
= (symbol
>> bitIndex
) & 1;
29 Models
[modelIndex
].Encode(rangeEncoder
, bit
);
30 modelIndex
= (modelIndex
<< 1) | bit
;
33 void ReverseEncode(CEncoder
*rangeEncoder
, UInt32 symbol
)
35 UInt32 modelIndex
= 1;
36 for (int i
= 0; i
< NumBitLevels
; i
++)
38 UInt32 bit
= symbol
& 1;
39 Models
[modelIndex
].Encode(rangeEncoder
, bit
);
40 modelIndex
= (modelIndex
<< 1) | bit
;
44 UInt32
GetPrice(UInt32 symbol
) const
46 symbol
|= (1 << NumBitLevels
);
50 price
+= Models
[symbol
>> 1].GetPrice(symbol
& 1);
55 UInt32
ReverseGetPrice(UInt32 symbol
) const
58 UInt32 modelIndex
= 1;
59 for (int i
= NumBitLevels
; i
!= 0; i
--)
61 UInt32 bit
= symbol
& 1;
63 price
+= Models
[modelIndex
].GetPrice(bit
);
64 modelIndex
= (modelIndex
<< 1) | bit
;
70 template <int numMoveBits
, int NumBitLevels
>
73 CBitDecoder
<numMoveBits
> Models
[1 << NumBitLevels
];
77 for(UInt32 i
= 1; i
< (1 << NumBitLevels
); i
++)
80 UInt32
Decode(CDecoder
*rangeDecoder
)
82 UInt32 modelIndex
= 1;
84 for(int bitIndex
= NumBitLevels
; bitIndex
!= 0; bitIndex
--)
86 // modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder);
87 RC_GETBIT(numMoveBits
, Models
[modelIndex
].Prob
, modelIndex
)
90 return modelIndex
- (1 << NumBitLevels
);
92 UInt32
ReverseDecode(CDecoder
*rangeDecoder
)
94 UInt32 modelIndex
= 1;
97 for(int bitIndex
= 0; bitIndex
< NumBitLevels
; bitIndex
++)
99 // UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
101 // modelIndex += bit;
102 // symbol |= (bit << bitIndex);
103 RC_GETBIT2(numMoveBits
, Models
[modelIndex
].Prob
, modelIndex
, ; , symbol
|= (1 << bitIndex
))
110 template <int numMoveBits
>
111 void ReverseBitTreeEncode(CBitEncoder
<numMoveBits
> *Models
,
112 CEncoder
*rangeEncoder
, int NumBitLevels
, UInt32 symbol
)
114 UInt32 modelIndex
= 1;
115 for (int i
= 0; i
< NumBitLevels
; i
++)
117 UInt32 bit
= symbol
& 1;
118 Models
[modelIndex
].Encode(rangeEncoder
, bit
);
119 modelIndex
= (modelIndex
<< 1) | bit
;
124 template <int numMoveBits
>
125 UInt32
ReverseBitTreeGetPrice(CBitEncoder
<numMoveBits
> *Models
,
126 UInt32 NumBitLevels
, UInt32 symbol
)
129 UInt32 modelIndex
= 1;
130 for (int i
= NumBitLevels
; i
!= 0; i
--)
132 UInt32 bit
= symbol
& 1;
134 price
+= Models
[modelIndex
].GetPrice(bit
);
135 modelIndex
= (modelIndex
<< 1) | bit
;
140 template <int numMoveBits
>
141 UInt32
ReverseBitTreeDecode(CBitDecoder
<numMoveBits
> *Models
,
142 CDecoder
*rangeDecoder
, int NumBitLevels
)
144 UInt32 modelIndex
= 1;
147 for(int bitIndex
= 0; bitIndex
< NumBitLevels
; bitIndex
++)
149 // UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
151 // modelIndex += bit;
152 // symbol |= (bit << bitIndex);
153 RC_GETBIT2(numMoveBits
, Models
[modelIndex
].Prob
, modelIndex
, ; , symbol
|= (1 << bitIndex
))