Segmentation.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Graphics.Video;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Graphics.Nvdec.Vp9.Types
  7. {
  8. internal struct Segmentation
  9. {
  10. public const int SegmentDeltadata = 0;
  11. public const int SegmentAbsdata = 1;
  12. public const int MaxSegments = 8;
  13. public const int SegTreeProbs = MaxSegments - 1;
  14. public const int PredictionProbs = 3;
  15. private static readonly int[] SegFeatureDataSigned = { 1, 1, 0, 0 };
  16. private static readonly int[] SegFeatureDataMax = { QuantCommon.MaxQ, Vp9.LoopFilter.MaxLoopFilter, 3, 0 };
  17. public bool Enabled;
  18. public bool UpdateMap;
  19. public byte UpdateData;
  20. public byte AbsDelta;
  21. public bool TemporalUpdate;
  22. public Array8<Array4<short>> FeatureData;
  23. public Array8<uint> FeatureMask;
  24. public int AqAvOffset;
  25. public static byte GetPredProbSegId(ref Array3<byte> segPredProbs, ref MacroBlockD xd)
  26. {
  27. return segPredProbs[xd.GetPredContextSegId()];
  28. }
  29. public void ClearAllSegFeatures()
  30. {
  31. MemoryMarshal.CreateSpan(ref FeatureData[0][0], 8 * 4).Clear();
  32. MemoryMarshal.CreateSpan(ref FeatureMask[0], 8).Clear();
  33. AqAvOffset = 0;
  34. }
  35. internal void EnableSegFeature(int segmentId, SegLvlFeatures featureId)
  36. {
  37. FeatureMask[segmentId] |= 1u << (int)featureId;
  38. }
  39. internal static int FeatureDataMax(SegLvlFeatures featureId)
  40. {
  41. return SegFeatureDataMax[(int)featureId];
  42. }
  43. internal static int IsSegFeatureSigned(SegLvlFeatures featureId)
  44. {
  45. return SegFeatureDataSigned[(int)featureId];
  46. }
  47. internal void SetSegData(int segmentId, SegLvlFeatures featureId, int segData)
  48. {
  49. Debug.Assert(segData <= SegFeatureDataMax[(int)featureId]);
  50. if (segData < 0)
  51. {
  52. Debug.Assert(SegFeatureDataSigned[(int)featureId] != 0);
  53. Debug.Assert(-segData <= SegFeatureDataMax[(int)featureId]);
  54. }
  55. FeatureData[segmentId][(int)featureId] = (short)segData;
  56. }
  57. internal int IsSegFeatureActive(int segmentId, SegLvlFeatures featureId)
  58. {
  59. return Enabled && (FeatureMask[segmentId] & (1 << (int)featureId)) != 0 ? 1 : 0;
  60. }
  61. internal short GetSegData(int segmentId, SegLvlFeatures featureId)
  62. {
  63. return FeatureData[segmentId][(int)featureId];
  64. }
  65. public int GetQIndex(int segmentId, int baseQIndex)
  66. {
  67. if (IsSegFeatureActive(segmentId, SegLvlFeatures.AltQ) != 0)
  68. {
  69. int data = GetSegData(segmentId, SegLvlFeatures.AltQ);
  70. int segQIndex = AbsDelta == Constants.SegmentAbsData ? data : baseQIndex + data;
  71. return Math.Clamp(segQIndex, 0, QuantCommon.MaxQ);
  72. }
  73. return baseQIndex;
  74. }
  75. public void SetupSegmentation(ref Vp9EntropyProbs fc, ref ReadBitBuffer rb)
  76. {
  77. UpdateMap = false;
  78. UpdateData = 0;
  79. Enabled = rb.ReadBit() != 0;
  80. if (!Enabled)
  81. {
  82. return;
  83. }
  84. // Segmentation map update
  85. UpdateMap = rb.ReadBit() != 0;
  86. if (UpdateMap)
  87. {
  88. for (int i = 0; i < SegTreeProbs; i++)
  89. {
  90. fc.SegTreeProb[i] = rb.ReadBit() != 0
  91. ? (byte)rb.ReadLiteral(8)
  92. : (byte)Prob.MaxProb;
  93. }
  94. TemporalUpdate = rb.ReadBit() != 0;
  95. if (TemporalUpdate)
  96. {
  97. for (int i = 0; i < PredictionProbs; i++)
  98. {
  99. fc.SegPredProb[i] = rb.ReadBit() != 0
  100. ? (byte)rb.ReadLiteral(8)
  101. : (byte)Prob.MaxProb;
  102. }
  103. }
  104. else
  105. {
  106. for (int i = 0; i < PredictionProbs; i++)
  107. {
  108. fc.SegPredProb[i] = Prob.MaxProb;
  109. }
  110. }
  111. }
  112. // Segmentation data update
  113. UpdateData = (byte)rb.ReadBit();
  114. if (UpdateData != 0)
  115. {
  116. AbsDelta = (byte)rb.ReadBit();
  117. ClearAllSegFeatures();
  118. for (int i = 0; i < Constants.MaxSegments; i++)
  119. {
  120. for (int j = 0; j < (int)SegLvlFeatures.Max; j++)
  121. {
  122. int data = 0;
  123. int featureEnabled = rb.ReadBit();
  124. if (featureEnabled != 0)
  125. {
  126. EnableSegFeature(i, (SegLvlFeatures)j);
  127. data = rb.DecodeUnsignedMax(FeatureDataMax((SegLvlFeatures)j));
  128. if (IsSegFeatureSigned((SegLvlFeatures)j) != 0)
  129. {
  130. data = rb.ReadBit() != 0 ? -data : data;
  131. }
  132. }
  133. SetSegData(i, (SegLvlFeatures)j, data);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }