mirror of
https://github.com/GreemDev/Ryujinx
synced 2024-11-22 09:53:35 +01:00
1f554c1093
* Renaming part 1 * Renaming part 2 * Renaming part 3 * Renaming part 4 * Renaming part 5 * Renaming part 6 * Renaming part 7 * Renaming part 8 * Renaming part 9 * Renaming part 10 * General cleanup * Thought I got all of these * Apply #595 * Additional renaming * Tweaks from feedback * Rename files
75 lines
No EOL
1.6 KiB
C#
75 lines
No EOL
1.6 KiB
C#
using System.IO;
|
|
|
|
namespace Ryujinx.Graphics.VDec
|
|
{
|
|
class BitStreamWriter
|
|
{
|
|
private const int BufferSize = 8;
|
|
|
|
private Stream _baseStream;
|
|
|
|
private int _buffer;
|
|
private int _bufferPos;
|
|
|
|
public BitStreamWriter(Stream baseStream)
|
|
{
|
|
_baseStream = baseStream;
|
|
}
|
|
|
|
public void WriteBit(bool value)
|
|
{
|
|
WriteBits(value ? 1 : 0, 1);
|
|
}
|
|
|
|
public void WriteBits(int value, int valueSize)
|
|
{
|
|
int valuePos = 0;
|
|
|
|
int remaining = valueSize;
|
|
|
|
while (remaining > 0)
|
|
{
|
|
int copySize = remaining;
|
|
|
|
int free = GetFreeBufferBits();
|
|
|
|
if (copySize > free)
|
|
{
|
|
copySize = free;
|
|
}
|
|
|
|
int mask = (1 << copySize) - 1;
|
|
|
|
int srcShift = (valueSize - valuePos) - copySize;
|
|
int dstShift = (BufferSize - _bufferPos) - copySize;
|
|
|
|
_buffer |= ((value >> srcShift) & mask) << dstShift;
|
|
|
|
valuePos += copySize;
|
|
_bufferPos += copySize;
|
|
remaining -= copySize;
|
|
}
|
|
}
|
|
|
|
private int GetFreeBufferBits()
|
|
{
|
|
if (_bufferPos == BufferSize)
|
|
{
|
|
Flush();
|
|
}
|
|
|
|
return BufferSize - _bufferPos;
|
|
}
|
|
|
|
public void Flush()
|
|
{
|
|
if (_bufferPos != 0)
|
|
{
|
|
_baseStream.WriteByte((byte)_buffer);
|
|
|
|
_buffer = 0;
|
|
_bufferPos = 0;
|
|
}
|
|
}
|
|
}
|
|
} |