2018-09-08 17:51:50 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Texture
|
|
|
|
|
{
|
|
|
|
|
public struct IntegerEncoded
|
|
|
|
|
{
|
|
|
|
|
public enum EIntegerEncoding
|
|
|
|
|
{
|
|
|
|
|
JustBits,
|
|
|
|
|
Quint,
|
|
|
|
|
Trit
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
EIntegerEncoding _encoding;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
public int NumberBits { get; private set; }
|
|
|
|
|
public int BitValue { get; private set; }
|
|
|
|
|
public int TritValue { get; private set; }
|
|
|
|
|
public int QuintValue { get; private set; }
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public IntegerEncoded(EIntegerEncoding encoding, int numBits)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
_encoding = encoding;
|
|
|
|
|
NumberBits = numBits;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
BitValue = 0;
|
|
|
|
|
TritValue = 0;
|
|
|
|
|
QuintValue = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public bool MatchesEncoding(IntegerEncoded other)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return _encoding == other._encoding && NumberBits == other.NumberBits;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EIntegerEncoding GetEncoding()
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return _encoding;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public int GetBitLength(int numberVals)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int totalBits = NumberBits * numberVals;
|
|
|
|
|
if (_encoding == EIntegerEncoding.Trit)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
totalBits += (numberVals * 8 + 4) / 5;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
|
else if (_encoding == EIntegerEncoding.Quint)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
totalBits += (numberVals * 7 + 2) / 3;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return totalBits;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
public static IntegerEncoded CreateEncoding(int maxVal)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
while (maxVal > 0)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int check = maxVal + 1;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
|
|
|
|
// Is maxVal a power of two?
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if ((check & (check - 1)) == 0)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return new IntegerEncoded(EIntegerEncoding.JustBits, BitArrayStream.PopCnt(maxVal));
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Is maxVal of the type 3*2^n - 1?
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if ((check % 3 == 0) && ((check / 3) & ((check / 3) - 1)) == 0)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return new IntegerEncoded(EIntegerEncoding.Trit, BitArrayStream.PopCnt(check / 3 - 1));
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Is maxVal of the type 5*2^n - 1?
|
2019-03-04 01:45:25 +00:00
|
|
|
|
if ((check % 5 == 0) && ((check / 5) & ((check / 5) - 1)) == 0)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
return new IntegerEncoded(EIntegerEncoding.Quint, BitArrayStream.PopCnt(check / 5 - 1));
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apparently it can't be represented with a bounded integer sequence...
|
|
|
|
|
// just iterate.
|
2019-03-04 01:45:25 +00:00
|
|
|
|
maxVal--;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new IntegerEncoded(EIntegerEncoding.JustBits, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DecodeTritBlock(
|
2019-03-04 01:45:25 +00:00
|
|
|
|
BitArrayStream bitStream,
|
|
|
|
|
List<IntegerEncoded> listIntegerEncoded,
|
|
|
|
|
int numberBitsPerValue)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
// Implement the algorithm in section C.2.12
|
|
|
|
|
int[] m = new int[5];
|
|
|
|
|
int[] t = new int[5];
|
|
|
|
|
int T;
|
|
|
|
|
|
|
|
|
|
// Read the trit encoded block according to
|
|
|
|
|
// table C.2.14
|
2019-03-04 01:45:25 +00:00
|
|
|
|
m[0] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
T = bitStream.ReadBits(2);
|
|
|
|
|
m[1] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
T |= bitStream.ReadBits(2) << 2;
|
|
|
|
|
m[2] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
T |= bitStream.ReadBits(1) << 4;
|
|
|
|
|
m[3] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
T |= bitStream.ReadBits(2) << 5;
|
|
|
|
|
m[4] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
T |= bitStream.ReadBits(1) << 7;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int c = 0;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
BitArrayStream tb = new BitArrayStream(new BitArray(new int[] { T }));
|
|
|
|
|
if (tb.ReadBits(2, 4) == 7)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
c = (tb.ReadBits(5, 7) << 2) | tb.ReadBits(0, 1);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
t[4] = t[3] = 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
c = tb.ReadBits(0, 4);
|
|
|
|
|
if (tb.ReadBits(5, 6) == 3)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
t[4] = 2;
|
2019-03-04 01:45:25 +00:00
|
|
|
|
t[3] = tb.ReadBit(7);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
t[4] = tb.ReadBit(7);
|
|
|
|
|
t[3] = tb.ReadBits(5, 6);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
BitArrayStream cb = new BitArrayStream(new BitArray(new int[] { c }));
|
|
|
|
|
if (cb.ReadBits(0, 1) == 3)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
t[2] = 2;
|
2019-03-04 01:45:25 +00:00
|
|
|
|
t[1] = cb.ReadBit(4);
|
|
|
|
|
t[0] = (cb.ReadBit(3) << 1) | (cb.ReadBit(2) & ~cb.ReadBit(3));
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
|
else if (cb.ReadBits(2, 3) == 3)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
t[2] = 2;
|
|
|
|
|
t[1] = 2;
|
2019-03-04 01:45:25 +00:00
|
|
|
|
t[0] = cb.ReadBits(0, 1);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
t[2] = cb.ReadBit(4);
|
|
|
|
|
t[1] = cb.ReadBits(2, 3);
|
|
|
|
|
t[0] = (cb.ReadBit(1) << 1) | (cb.ReadBit(0) & ~cb.ReadBit(1));
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Trit, numberBitsPerValue)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
BitValue = m[i],
|
|
|
|
|
TritValue = t[i]
|
|
|
|
|
};
|
2019-03-04 01:45:25 +00:00
|
|
|
|
listIntegerEncoded.Add(intEncoded);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DecodeQuintBlock(
|
2019-03-04 01:45:25 +00:00
|
|
|
|
BitArrayStream bitStream,
|
|
|
|
|
List<IntegerEncoded> listIntegerEncoded,
|
|
|
|
|
int numberBitsPerValue)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
// Implement the algorithm in section C.2.12
|
|
|
|
|
int[] m = new int[3];
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int[] qa = new int[3];
|
|
|
|
|
int q;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
|
|
|
|
// Read the trit encoded block according to
|
|
|
|
|
// table C.2.15
|
2019-03-04 01:45:25 +00:00
|
|
|
|
m[0] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
q = bitStream.ReadBits(3);
|
|
|
|
|
m[1] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
q |= bitStream.ReadBits(2) << 3;
|
|
|
|
|
m[2] = bitStream.ReadBits(numberBitsPerValue);
|
|
|
|
|
q |= bitStream.ReadBits(2) << 5;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
BitArrayStream qb = new BitArrayStream(new BitArray(new int[] { q }));
|
|
|
|
|
if (qb.ReadBits(1, 2) == 3 && qb.ReadBits(5, 6) == 0)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
qa[0] = qa[1] = 4;
|
|
|
|
|
qa[2] = (qb.ReadBit(0) << 2) | ((qb.ReadBit(4) & ~qb.ReadBit(0)) << 1) | (qb.ReadBit(3) & ~qb.ReadBit(0));
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int c = 0;
|
|
|
|
|
if (qb.ReadBits(1, 2) == 3)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
qa[2] = 4;
|
|
|
|
|
c = (qb.ReadBits(3, 4) << 3) | ((~qb.ReadBits(5, 6) & 3) << 1) | qb.ReadBit(0);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
qa[2] = qb.ReadBits(5, 6);
|
|
|
|
|
c = qb.ReadBits(0, 4);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
|
BitArrayStream cb = new BitArrayStream(new BitArray(new int[] { c }));
|
|
|
|
|
if (cb.ReadBits(0, 2) == 5)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
qa[1] = 4;
|
|
|
|
|
qa[0] = cb.ReadBits(3, 4);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
qa[1] = cb.ReadBits(3, 4);
|
|
|
|
|
qa[0] = cb.ReadBits(0, 2);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Quint, numberBitsPerValue)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
BitValue = m[i],
|
2019-03-04 01:45:25 +00:00
|
|
|
|
QuintValue = qa[i]
|
2018-09-08 17:51:50 +00:00
|
|
|
|
};
|
2019-03-04 01:45:25 +00:00
|
|
|
|
listIntegerEncoded.Add(intEncoded);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DecodeIntegerSequence(
|
2019-03-04 01:45:25 +00:00
|
|
|
|
List<IntegerEncoded> decodeIntegerSequence,
|
|
|
|
|
BitArrayStream bitStream,
|
|
|
|
|
int maxRange,
|
|
|
|
|
int numberValues)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
// Determine encoding parameters
|
2019-03-04 01:45:25 +00:00
|
|
|
|
IntegerEncoded intEncoded = CreateEncoding(maxRange);
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
|
|
|
|
// Start decoding
|
2019-03-04 01:45:25 +00:00
|
|
|
|
int numberValuesDecoded = 0;
|
|
|
|
|
while (numberValuesDecoded < numberValues)
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
switch (intEncoded.GetEncoding())
|
2018-09-08 17:51:50 +00:00
|
|
|
|
{
|
|
|
|
|
case EIntegerEncoding.Quint:
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
DecodeQuintBlock(bitStream, decodeIntegerSequence, intEncoded.NumberBits);
|
|
|
|
|
numberValuesDecoded += 3;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case EIntegerEncoding.Trit:
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
DecodeTritBlock(bitStream, decodeIntegerSequence, intEncoded.NumberBits);
|
|
|
|
|
numberValuesDecoded += 5;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case EIntegerEncoding.JustBits:
|
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
|
intEncoded.BitValue = bitStream.ReadBits(intEncoded.NumberBits);
|
|
|
|
|
decodeIntegerSequence.Add(intEncoded);
|
|
|
|
|
numberValuesDecoded++;
|
2018-09-08 17:51:50 +00:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|