VpxRangeEncoder.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.IO;
  2. namespace Ryujinx.Graphics.VDec
  3. {
  4. class VpxRangeEncoder
  5. {
  6. private const int HalfProbability = 128;
  7. private static readonly int[] NormLut = new int[]
  8. {
  9. 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  10. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  11. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  12. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  13. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  14. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  15. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  16. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  25. };
  26. private Stream BaseStream;
  27. private uint LowValue;
  28. private uint Range;
  29. private int Count;
  30. public VpxRangeEncoder(Stream BaseStream)
  31. {
  32. this.BaseStream = BaseStream;
  33. Range = 0xff;
  34. Count = -24;
  35. Write(false);
  36. }
  37. public void WriteByte(byte Value)
  38. {
  39. Write(Value, 8);
  40. }
  41. public void Write(int Value, int ValueSize)
  42. {
  43. for (int Bit = ValueSize - 1; Bit >= 0; Bit--)
  44. {
  45. Write(((Value >> Bit) & 1) != 0);
  46. }
  47. }
  48. public void Write(bool Bit)
  49. {
  50. Write(Bit, HalfProbability);
  51. }
  52. public void Write(bool Bit, int Probability)
  53. {
  54. uint Range = this.Range;
  55. uint Split = 1 + (((Range - 1) * (uint)Probability) >> 8);
  56. Range = Split;
  57. if (Bit)
  58. {
  59. LowValue += Split;
  60. Range = this.Range - Split;
  61. }
  62. int Shift = NormLut[Range];
  63. Range <<= Shift;
  64. Count += Shift;
  65. if (Count >= 0)
  66. {
  67. int Offset = Shift - Count;
  68. if (((LowValue << (Offset - 1)) >> 31) != 0)
  69. {
  70. long CurrentPos = BaseStream.Position;
  71. BaseStream.Seek(-1, SeekOrigin.Current);
  72. while (BaseStream.Position >= 0 && PeekByte() == 0xff)
  73. {
  74. BaseStream.WriteByte(0);
  75. BaseStream.Seek(-2, SeekOrigin.Current);
  76. }
  77. BaseStream.WriteByte((byte)(PeekByte() + 1));
  78. BaseStream.Seek(CurrentPos, SeekOrigin.Begin);
  79. }
  80. BaseStream.WriteByte((byte)(LowValue >> (24 - Offset)));
  81. LowValue <<= Offset;
  82. Shift = Count;
  83. LowValue &= 0xffffff;
  84. Count -= 8;
  85. }
  86. LowValue <<= Shift;
  87. this.Range = Range;
  88. }
  89. private byte PeekByte()
  90. {
  91. byte Value = (byte)BaseStream.ReadByte();
  92. BaseStream.Seek(-1, SeekOrigin.Current);
  93. return Value;
  94. }
  95. public void End()
  96. {
  97. for (int Index = 0; Index < 32; Index++)
  98. {
  99. Write(false);
  100. }
  101. }
  102. }
  103. }