From 0dbfe3c23ee072ec9dbc477f955a163107af2be1 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Tue, 31 Dec 2019 21:08:02 -0300 Subject: Re-add NVDEC project (not integrated) --- Ryujinx.Graphics.Nvdec/VDec/BitStreamWriter.cs | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Ryujinx.Graphics.Nvdec/VDec/BitStreamWriter.cs (limited to 'Ryujinx.Graphics.Nvdec/VDec/BitStreamWriter.cs') diff --git a/Ryujinx.Graphics.Nvdec/VDec/BitStreamWriter.cs b/Ryujinx.Graphics.Nvdec/VDec/BitStreamWriter.cs new file mode 100644 index 00000000..db2d39e5 --- /dev/null +++ b/Ryujinx.Graphics.Nvdec/VDec/BitStreamWriter.cs @@ -0,0 +1,75 @@ +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; + } + } + } +} \ No newline at end of file -- cgit v1.2.3