IntegerEncoded.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Numerics;
  3. namespace Ryujinx.Graphics.Texture.Astc
  4. {
  5. internal struct IntegerEncoded
  6. {
  7. internal const int StructSize = 8;
  8. private static readonly IntegerEncoded[] Encodings;
  9. public enum EIntegerEncoding : byte
  10. {
  11. JustBits,
  12. Quint,
  13. Trit
  14. }
  15. EIntegerEncoding _encoding;
  16. public byte NumberBits { get; private set; }
  17. public byte TritValue { get; private set; }
  18. public byte QuintValue { get; private set; }
  19. public int BitValue { get; private set; }
  20. static IntegerEncoded()
  21. {
  22. Encodings = new IntegerEncoded[0x100];
  23. for (int i = 0; i < Encodings.Length; i++)
  24. {
  25. Encodings[i] = CreateEncodingCalc(i);
  26. }
  27. }
  28. public IntegerEncoded(EIntegerEncoding encoding, int numBits)
  29. {
  30. _encoding = encoding;
  31. NumberBits = (byte)numBits;
  32. BitValue = 0;
  33. TritValue = 0;
  34. QuintValue = 0;
  35. }
  36. public bool MatchesEncoding(IntegerEncoded other)
  37. {
  38. return _encoding == other._encoding && NumberBits == other.NumberBits;
  39. }
  40. public EIntegerEncoding GetEncoding()
  41. {
  42. return _encoding;
  43. }
  44. public int GetBitLength(int numberVals)
  45. {
  46. int totalBits = NumberBits * numberVals;
  47. if (_encoding == EIntegerEncoding.Trit)
  48. {
  49. totalBits += (numberVals * 8 + 4) / 5;
  50. }
  51. else if (_encoding == EIntegerEncoding.Quint)
  52. {
  53. totalBits += (numberVals * 7 + 2) / 3;
  54. }
  55. return totalBits;
  56. }
  57. public static IntegerEncoded CreateEncoding(int maxVal)
  58. {
  59. return Encodings[maxVal];
  60. }
  61. private static IntegerEncoded CreateEncodingCalc(int maxVal)
  62. {
  63. while (maxVal > 0)
  64. {
  65. int check = maxVal + 1;
  66. // Is maxVal a power of two?
  67. if ((check & (check - 1)) == 0)
  68. {
  69. return new IntegerEncoded(EIntegerEncoding.JustBits, BitOperations.PopCount((uint)maxVal));
  70. }
  71. // Is maxVal of the type 3*2^n - 1?
  72. if ((check % 3 == 0) && ((check / 3) & ((check / 3) - 1)) == 0)
  73. {
  74. return new IntegerEncoded(EIntegerEncoding.Trit, BitOperations.PopCount((uint)(check / 3 - 1)));
  75. }
  76. // Is maxVal of the type 5*2^n - 1?
  77. if ((check % 5 == 0) && ((check / 5) & ((check / 5) - 1)) == 0)
  78. {
  79. return new IntegerEncoded(EIntegerEncoding.Quint, BitOperations.PopCount((uint)(check / 5 - 1)));
  80. }
  81. // Apparently it can't be represented with a bounded integer sequence...
  82. // just iterate.
  83. maxVal--;
  84. }
  85. return new IntegerEncoded(EIntegerEncoding.JustBits, 0);
  86. }
  87. public static void DecodeTritBlock(
  88. ref BitStream128 bitStream,
  89. ref IntegerSequence listIntegerEncoded,
  90. int numberBitsPerValue)
  91. {
  92. // Implement the algorithm in section C.2.12
  93. Span<int> m = stackalloc int[5];
  94. m[0] = bitStream.ReadBits(numberBitsPerValue);
  95. int encoded = bitStream.ReadBits(2);
  96. m[1] = bitStream.ReadBits(numberBitsPerValue);
  97. encoded |= bitStream.ReadBits(2) << 2;
  98. m[2] = bitStream.ReadBits(numberBitsPerValue);
  99. encoded |= bitStream.ReadBits(1) << 4;
  100. m[3] = bitStream.ReadBits(numberBitsPerValue);
  101. encoded |= bitStream.ReadBits(2) << 5;
  102. m[4] = bitStream.ReadBits(numberBitsPerValue);
  103. encoded |= bitStream.ReadBits(1) << 7;
  104. ReadOnlySpan<byte> encodings = GetTritEncoding(encoded);
  105. IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Trit, numberBitsPerValue);
  106. for (int i = 0; i < 5; i++)
  107. {
  108. intEncoded.BitValue = m[i];
  109. intEncoded.TritValue = encodings[i];
  110. listIntegerEncoded.Add(ref intEncoded);
  111. }
  112. }
  113. public static void DecodeQuintBlock(
  114. ref BitStream128 bitStream,
  115. ref IntegerSequence listIntegerEncoded,
  116. int numberBitsPerValue)
  117. {
  118. ReadOnlySpan<byte> interleavedBits = new byte[] { 3, 2, 2 };
  119. // Implement the algorithm in section C.2.12
  120. Span<int> m = stackalloc int[3];
  121. ulong encoded = 0;
  122. int encodedBitsRead = 0;
  123. for (int i = 0; i < m.Length; i++)
  124. {
  125. m[i] = bitStream.ReadBits(numberBitsPerValue);
  126. uint encodedBits = (uint)bitStream.ReadBits(interleavedBits[i]);
  127. encoded |= encodedBits << encodedBitsRead;
  128. encodedBitsRead += interleavedBits[i];
  129. }
  130. ReadOnlySpan<byte> encodings = GetQuintEncoding((int)encoded);
  131. for (int i = 0; i < 3; i++)
  132. {
  133. IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Quint, numberBitsPerValue)
  134. {
  135. BitValue = m[i],
  136. QuintValue = encodings[i]
  137. };
  138. listIntegerEncoded.Add(ref intEncoded);
  139. }
  140. }
  141. public static void DecodeIntegerSequence(
  142. ref IntegerSequence decodeIntegerSequence,
  143. ref BitStream128 bitStream,
  144. int maxRange,
  145. int numberValues)
  146. {
  147. // Determine encoding parameters
  148. IntegerEncoded intEncoded = CreateEncoding(maxRange);
  149. // Start decoding
  150. int numberValuesDecoded = 0;
  151. while (numberValuesDecoded < numberValues)
  152. {
  153. switch (intEncoded.GetEncoding())
  154. {
  155. case EIntegerEncoding.Quint:
  156. {
  157. DecodeQuintBlock(ref bitStream, ref decodeIntegerSequence, intEncoded.NumberBits);
  158. numberValuesDecoded += 3;
  159. break;
  160. }
  161. case EIntegerEncoding.Trit:
  162. {
  163. DecodeTritBlock(ref bitStream, ref decodeIntegerSequence, intEncoded.NumberBits);
  164. numberValuesDecoded += 5;
  165. break;
  166. }
  167. case EIntegerEncoding.JustBits:
  168. {
  169. intEncoded.BitValue = bitStream.ReadBits(intEncoded.NumberBits);
  170. decodeIntegerSequence.Add(ref intEncoded);
  171. numberValuesDecoded++;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. private static ReadOnlySpan<byte> GetTritEncoding(int index)
  178. {
  179. return TritEncodings.Slice(index * 5, 5);
  180. }
  181. private static ReadOnlySpan<byte> GetQuintEncoding(int index)
  182. {
  183. return QuintEncodings.Slice(index * 3, 3);
  184. }
  185. private static ReadOnlySpan<byte> TritEncodings => new byte[]
  186. {
  187. 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
  188. 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0,
  189. 2, 1, 0, 0, 0, 1, 0, 2, 0, 0, 0, 2, 0, 0, 0,
  190. 1, 2, 0, 0, 0, 2, 2, 0, 0, 0, 2, 0, 2, 0, 0,
  191. 0, 2, 2, 0, 0, 1, 2, 2, 0, 0, 2, 2, 2, 0, 0,
  192. 2, 0, 2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0,
  193. 2, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0, 1, 1, 0, 0,
  194. 1, 1, 1, 0, 0, 2, 1, 1, 0, 0, 1, 1, 2, 0, 0,
  195. 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 2, 2, 1, 0, 0,
  196. 2, 1, 2, 0, 0, 0, 0, 0, 2, 2, 1, 0, 0, 2, 2,
  197. 2, 0, 0, 2, 2, 0, 0, 2, 2, 2, 0, 0, 0, 1, 0,
  198. 1, 0, 0, 1, 0, 2, 0, 0, 1, 0, 0, 0, 2, 1, 0,
  199. 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 2, 1, 0, 1, 0,
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0, 0, 2, 1, 2,
  263. 0, 1, 0, 1, 2, 1, 1, 0, 1, 2, 2, 1, 0, 1, 2,
  264. 1, 0, 2, 1, 2, 0, 2, 0, 1, 2, 1, 2, 0, 1, 2,
  265. 2, 2, 0, 1, 2, 2, 0, 2, 1, 2, 0, 2, 2, 1, 2,
  266. 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 0, 2, 1, 2,
  267. 0, 0, 1, 1, 2, 1, 0, 1, 1, 2, 2, 0, 1, 1, 2,
  268. 0, 1, 2, 1, 2, 0, 1, 1, 1, 2, 1, 1, 1, 1, 2,
  269. 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 0, 2, 1, 1, 2,
  270. 1, 2, 1, 1, 2, 2, 2, 1, 1, 2, 2, 1, 2, 1, 2,
  271. 0, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  272. 2, 1, 2, 2, 2
  273. };
  274. private static ReadOnlySpan<byte> QuintEncodings => new byte[]
  275. {
  276. 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0,
  277. 0, 4, 0, 4, 4, 0, 4, 4, 4, 0, 1, 0, 1, 1, 0,
  278. 2, 1, 0, 3, 1, 0, 4, 1, 0, 1, 4, 0, 4, 4, 1,
  279. 4, 4, 4, 0, 2, 0, 1, 2, 0, 2, 2, 0, 3, 2, 0,
  280. 4, 2, 0, 2, 4, 0, 4, 4, 2, 4, 4, 4, 0, 3, 0,
  281. 1, 3, 0, 2, 3, 0, 3, 3, 0, 4, 3, 0, 3, 4, 0,
  282. 4, 4, 3, 4, 4, 4, 0, 0, 1, 1, 0, 1, 2, 0, 1,
  283. 3, 0, 1, 4, 0, 1, 0, 4, 1, 4, 0, 4, 0, 4, 4,
  284. 0, 1, 1, 1, 1, 1, 2, 1, 1, 3, 1, 1, 4, 1, 1,
  285. 1, 4, 1, 4, 1, 4, 1, 4, 4, 0, 2, 1, 1, 2, 1,
  286. 2, 2, 1, 3, 2,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