1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/7zip/Compress/RangeCoder/RangeCoderBitTree.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +// Compress/RangeCoder/RangeCoderBitTree.h 1.5 + 1.6 +#ifndef __COMPRESS_RANGECODER_BIT_TREE_H 1.7 +#define __COMPRESS_RANGECODER_BIT_TREE_H 1.8 + 1.9 +#include "RangeCoderBit.h" 1.10 +#include "RangeCoderOpt.h" 1.11 + 1.12 +namespace NCompress { 1.13 +namespace NRangeCoder { 1.14 + 1.15 +template <int numMoveBits, int NumBitLevels> 1.16 +class CBitTreeEncoder 1.17 +{ 1.18 + CBitEncoder<numMoveBits> Models[1 << NumBitLevels]; 1.19 +public: 1.20 + void Init() 1.21 + { 1.22 + for(UInt32 i = 1; i < (1 << NumBitLevels); i++) 1.23 + Models[i].Init(); 1.24 + } 1.25 + void Encode(CEncoder *rangeEncoder, UInt32 symbol) 1.26 + { 1.27 + UInt32 modelIndex = 1; 1.28 + for (int bitIndex = NumBitLevels; bitIndex != 0 ;) 1.29 + { 1.30 + bitIndex--; 1.31 + UInt32 bit = (symbol >> bitIndex) & 1; 1.32 + Models[modelIndex].Encode(rangeEncoder, bit); 1.33 + modelIndex = (modelIndex << 1) | bit; 1.34 + } 1.35 + }; 1.36 + void ReverseEncode(CEncoder *rangeEncoder, UInt32 symbol) 1.37 + { 1.38 + UInt32 modelIndex = 1; 1.39 + for (int i = 0; i < NumBitLevels; i++) 1.40 + { 1.41 + UInt32 bit = symbol & 1; 1.42 + Models[modelIndex].Encode(rangeEncoder, bit); 1.43 + modelIndex = (modelIndex << 1) | bit; 1.44 + symbol >>= 1; 1.45 + } 1.46 + } 1.47 + UInt32 GetPrice(UInt32 symbol) const 1.48 + { 1.49 + symbol |= (1 << NumBitLevels); 1.50 + UInt32 price = 0; 1.51 + while (symbol != 1) 1.52 + { 1.53 + price += Models[symbol >> 1].GetPrice(symbol & 1); 1.54 + symbol >>= 1; 1.55 + } 1.56 + return price; 1.57 + } 1.58 + UInt32 ReverseGetPrice(UInt32 symbol) const 1.59 + { 1.60 + UInt32 price = 0; 1.61 + UInt32 modelIndex = 1; 1.62 + for (int i = NumBitLevels; i != 0; i--) 1.63 + { 1.64 + UInt32 bit = symbol & 1; 1.65 + symbol >>= 1; 1.66 + price += Models[modelIndex].GetPrice(bit); 1.67 + modelIndex = (modelIndex << 1) | bit; 1.68 + } 1.69 + return price; 1.70 + } 1.71 +}; 1.72 + 1.73 +template <int numMoveBits, int NumBitLevels> 1.74 +class CBitTreeDecoder 1.75 +{ 1.76 + CBitDecoder<numMoveBits> Models[1 << NumBitLevels]; 1.77 +public: 1.78 + void Init() 1.79 + { 1.80 + for(UInt32 i = 1; i < (1 << NumBitLevels); i++) 1.81 + Models[i].Init(); 1.82 + } 1.83 + UInt32 Decode(CDecoder *rangeDecoder) 1.84 + { 1.85 + UInt32 modelIndex = 1; 1.86 + RC_INIT_VAR 1.87 + for(int bitIndex = NumBitLevels; bitIndex != 0; bitIndex--) 1.88 + { 1.89 + // modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder); 1.90 + RC_GETBIT(numMoveBits, Models[modelIndex].Prob, modelIndex) 1.91 + } 1.92 + RC_FLUSH_VAR 1.93 + return modelIndex - (1 << NumBitLevels); 1.94 + }; 1.95 + UInt32 ReverseDecode(CDecoder *rangeDecoder) 1.96 + { 1.97 + UInt32 modelIndex = 1; 1.98 + UInt32 symbol = 0; 1.99 + RC_INIT_VAR 1.100 + for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++) 1.101 + { 1.102 + // UInt32 bit = Models[modelIndex].Decode(rangeDecoder); 1.103 + // modelIndex <<= 1; 1.104 + // modelIndex += bit; 1.105 + // symbol |= (bit << bitIndex); 1.106 + RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex)) 1.107 + } 1.108 + RC_FLUSH_VAR 1.109 + return symbol; 1.110 + } 1.111 +}; 1.112 + 1.113 +template <int numMoveBits> 1.114 +void ReverseBitTreeEncode(CBitEncoder<numMoveBits> *Models, 1.115 + CEncoder *rangeEncoder, int NumBitLevels, UInt32 symbol) 1.116 +{ 1.117 + UInt32 modelIndex = 1; 1.118 + for (int i = 0; i < NumBitLevels; i++) 1.119 + { 1.120 + UInt32 bit = symbol & 1; 1.121 + Models[modelIndex].Encode(rangeEncoder, bit); 1.122 + modelIndex = (modelIndex << 1) | bit; 1.123 + symbol >>= 1; 1.124 + } 1.125 +} 1.126 + 1.127 +template <int numMoveBits> 1.128 +UInt32 ReverseBitTreeGetPrice(CBitEncoder<numMoveBits> *Models, 1.129 + UInt32 NumBitLevels, UInt32 symbol) 1.130 +{ 1.131 + UInt32 price = 0; 1.132 + UInt32 modelIndex = 1; 1.133 + for (int i = NumBitLevels; i != 0; i--) 1.134 + { 1.135 + UInt32 bit = symbol & 1; 1.136 + symbol >>= 1; 1.137 + price += Models[modelIndex].GetPrice(bit); 1.138 + modelIndex = (modelIndex << 1) | bit; 1.139 + } 1.140 + return price; 1.141 +} 1.142 + 1.143 +template <int numMoveBits> 1.144 +UInt32 ReverseBitTreeDecode(CBitDecoder<numMoveBits> *Models, 1.145 + CDecoder *rangeDecoder, int NumBitLevels) 1.146 +{ 1.147 + UInt32 modelIndex = 1; 1.148 + UInt32 symbol = 0; 1.149 + RC_INIT_VAR 1.150 + for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++) 1.151 + { 1.152 + // UInt32 bit = Models[modelIndex].Decode(rangeDecoder); 1.153 + // modelIndex <<= 1; 1.154 + // modelIndex += bit; 1.155 + // symbol |= (bit << bitIndex); 1.156 + RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex)) 1.157 + } 1.158 + RC_FLUSH_VAR 1.159 + return symbol; 1.160 +} 1.161 + 1.162 +}} 1.163 + 1.164 +#endif