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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private Stream _baseStream;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private uint _lowValue;
|
|
|
|
private uint _range;
|
|
|
|
private int _count;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public VpxRangeEncoder(Stream baseStream)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream = baseStream;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_range = 0xff;
|
|
|
|
_count = -24;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
|
|
|
Write(false);
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public void WriteByte(byte value)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
Write(value, 8);
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public void Write(int value, int valueSize)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
for (int bit = valueSize - 1; bit >= 0; bit--)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
Write(((value >> bit) & 1) != 0);
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public void Write(bool bit)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
Write(bit, HalfProbability);
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public void Write(bool bit, int probability)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
uint range = _range;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
uint split = 1 + (((range - 1) * (uint)probability) >> 8);
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
range = split;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (bit)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_lowValue += split;
|
|
|
|
range = _range - split;
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int shift = NormLut[range];
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
range <<= shift;
|
|
|
|
_count += shift;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_count >= 0)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
int offset = shift - _count;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (((_lowValue << (offset - 1)) >> 31) != 0)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
long currentPos = _baseStream.Position;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream.Seek(-1, SeekOrigin.Current);
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
while (_baseStream.Position >= 0 && PeekByte() == 0xff)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream.WriteByte(0);
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream.Seek(-2, SeekOrigin.Current);
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream.WriteByte((byte)(PeekByte() + 1));
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream.Seek(currentPos, SeekOrigin.Begin);
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream.WriteByte((byte)(_lowValue >> (24 - offset)));
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_lowValue <<= offset;
|
|
|
|
shift = _count;
|
|
|
|
_lowValue &= 0xffffff;
|
|
|
|
_count -= 8;
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_lowValue <<= shift;
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_range = range;
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private byte PeekByte()
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
byte value = (byte)_baseStream.ReadByte();
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_baseStream.Seek(-1, SeekOrigin.Current);
|
2018-12-03 03:38:47 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
return value;
|
2018-12-03 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void End()
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
for (int index = 0; index < 32; index++)
|
2018-12-03 03:38:47 +01:00
|
|
|
{
|
|
|
|
Write(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|