1
// Compress/RangeCoder.h
3
#ifndef __COMPRESS_RANGE_CODER_H
4
#define __COMPRESS_RANGE_CODER_H
6
#include "../Common/InBuffer.h"
7
#include "../Common/OutBuffer.h"
10
namespace NRangeCoder {
12
const int kNumTopBits = 24;
13
const UInt32 kTopValue = (1 << kNumTopBits);
23
bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
25
void SetStream(ISequentialOutStream *stream) { Stream.SetStream(stream); }
38
for(int i = 0; i < 5; i++)
42
HRESULT FlushStream() { return Stream.Flush(); }
44
void ReleaseStream() { Stream.ReleaseStream(); }
46
void Encode(UInt32 start, UInt32 size, UInt32 total)
48
Low += start * (Range /= total);
50
while (Range < kTopValue)
59
if ((UInt32)Low < (UInt32)0xFF000000 || (int)(Low >> 32) != 0)
64
Stream.WriteByte((Byte)(temp + (Byte)(Low >> 32)));
67
while(--_cacheSize != 0);
68
_cache = (Byte)((UInt32)Low >> 24);
71
Low = (UInt32)Low << 8;
74
void EncodeDirectBits(UInt32 value, int numBits)
76
for (numBits--; numBits >= 0; numBits--)
79
Low += Range & (0 - ((value >> numBits) & 1));
80
if (Range < kTopValue)
88
void EncodeBit(UInt32 size0, UInt32 numTotalBits, UInt32 symbol)
90
UInt32 newBound = (Range >> numTotalBits) * size0;
98
while (Range < kTopValue)
105
UInt64 GetProcessedSize() { return Stream.GetProcessedSize() + _cacheSize + 4; }
114
bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); }
118
while (Range < kTopValue)
120
Code = (Code << 8) | Stream.ReadByte();
125
void SetStream(ISequentialInStream *stream) { Stream.SetStream(stream); }
131
for(int i = 0; i < 5; i++)
132
Code = (Code << 8) | Stream.ReadByte();
135
void ReleaseStream() { Stream.ReleaseStream(); }
137
UInt32 GetThreshold(UInt32 total)
139
return (Code) / ( Range /= total);
142
void Decode(UInt32 start, UInt32 size)
144
Code -= start * Range;
149
UInt32 DecodeDirectBits(int numTotalBits)
151
UInt32 range = Range;
154
for (int i = numTotalBits; i != 0; i--)
165
UInt32 t = (code - range) >> 31;
166
code -= range & (t - 1);
167
result = (result << 1) | (1 - t);
169
if (range < kTopValue)
171
code = (code << 8) | Stream.ReadByte();
180
UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits)
182
UInt32 newBound = (Range >> numTotalBits) * size0;
199
UInt64 GetProcessedSize() {return Stream.GetProcessedSize(); }