Files
sharpcompress/src/SharpCompress/Compressor/LZMA/RangeCoder/RangeCoderBit.cs

140 lines
4.3 KiB
C#
Raw Normal View History

2015-12-30 11:19:42 +00:00
using System;
namespace SharpCompress.Compressor.LZMA.RangeCoder
{
internal struct BitEncoder
{
public const int kNumBitModelTotalBits = 11;
public const uint kBitModelTotal = (1 << kNumBitModelTotalBits);
private const int kNumMoveBits = 5;
private const int kNumMoveReducingBits = 2;
public const int kNumBitPriceShiftBits = 6;
private uint Prob;
public void Init()
{
Prob = kBitModelTotal >> 1;
}
public void UpdateModel(uint symbol)
{
if (symbol == 0)
{
2015-12-30 11:19:42 +00:00
Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
}
2015-12-30 11:19:42 +00:00
else
{
2015-12-30 11:19:42 +00:00
Prob -= (Prob) >> kNumMoveBits;
}
2015-12-30 11:19:42 +00:00
}
public void Encode(Encoder encoder, uint symbol)
{
// encoder.EncodeBit(Prob, kNumBitModelTotalBits, symbol);
// UpdateModel(symbol);
uint newBound = (encoder.Range >> kNumBitModelTotalBits) * Prob;
2015-12-30 11:19:42 +00:00
if (symbol == 0)
{
encoder.Range = newBound;
Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
}
else
{
encoder.Low += newBound;
encoder.Range -= newBound;
Prob -= (Prob) >> kNumMoveBits;
}
if (encoder.Range < Encoder.kTopValue)
{
encoder.Range <<= 8;
encoder.ShiftLow();
}
}
private static readonly UInt32[] ProbPrices = new UInt32[kBitModelTotal >> kNumMoveReducingBits];
2015-12-30 11:19:42 +00:00
static BitEncoder()
{
const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
for (int i = kNumBits - 1; i >= 0; i--)
{
UInt32 start = (UInt32)1 << (kNumBits - i - 1);
UInt32 end = (UInt32)1 << (kNumBits - i);
2015-12-30 11:19:42 +00:00
for (UInt32 j = start; j < end; j++)
{
ProbPrices[j] = ((UInt32)i << kNumBitPriceShiftBits) +
2015-12-30 11:19:42 +00:00
(((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1));
}
2015-12-30 11:19:42 +00:00
}
}
public uint GetPrice(uint symbol)
{
return ProbPrices[(((Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
2015-12-30 11:19:42 +00:00
}
public uint GetPrice0()
{
return ProbPrices[Prob >> kNumMoveReducingBits];
}
public uint GetPrice1()
{
return ProbPrices[(kBitModelTotal - Prob) >> kNumMoveReducingBits];
}
}
internal struct BitDecoder
{
public const int kNumBitModelTotalBits = 11;
public const uint kBitModelTotal = (1 << kNumBitModelTotalBits);
private const int kNumMoveBits = 5;
private uint Prob;
public void UpdateModel(int numMoveBits, uint symbol)
{
if (symbol == 0)
{
2015-12-30 11:19:42 +00:00
Prob += (kBitModelTotal - Prob) >> numMoveBits;
}
2015-12-30 11:19:42 +00:00
else
{
2015-12-30 11:19:42 +00:00
Prob -= (Prob) >> numMoveBits;
}
2015-12-30 11:19:42 +00:00
}
public void Init()
{
Prob = kBitModelTotal >> 1;
}
public uint Decode(Decoder rangeDecoder)
2015-12-30 11:19:42 +00:00
{
uint newBound = (rangeDecoder.Range >> kNumBitModelTotalBits) * Prob;
2015-12-30 11:19:42 +00:00
if (rangeDecoder.Code < newBound)
{
rangeDecoder.Range = newBound;
Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
if (rangeDecoder.Range < Decoder.kTopValue)
{
rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte)rangeDecoder.Stream.ReadByte();
2015-12-30 11:19:42 +00:00
rangeDecoder.Range <<= 8;
rangeDecoder.Total++;
}
return 0;
}
rangeDecoder.Range -= newBound;
rangeDecoder.Code -= newBound;
Prob -= (Prob) >> kNumMoveBits;
if (rangeDecoder.Range < Decoder.kTopValue)
2015-12-30 11:19:42 +00:00
{
rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte)rangeDecoder.Stream.ReadByte();
rangeDecoder.Range <<= 8;
rangeDecoder.Total++;
2015-12-30 11:19:42 +00:00
}
return 1;
2015-12-30 11:19:42 +00:00
}
}
2013-04-28 12:32:55 +01:00
}