Vp9Common.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Graphics.Nvdec.Vp9.Common;
  3. using Ryujinx.Graphics.Video;
  4. namespace Ryujinx.Graphics.Nvdec.Vp9.Types
  5. {
  6. internal struct Vp9Common
  7. {
  8. public MacroBlockD Mb;
  9. public ArrayPtr<TileWorkerData> TileWorkerData;
  10. public InternalErrorInfo Error;
  11. public int Width;
  12. public int Height;
  13. public int SubsamplingX;
  14. public int SubsamplingY;
  15. public ArrayPtr<MvRef> PrevFrameMvs;
  16. public ArrayPtr<MvRef> CurFrameMvs;
  17. public Array3<RefBuffer> FrameRefs;
  18. public FrameType FrameType;
  19. // Flag signaling that the frame is encoded using only Intra modes.
  20. public bool IntraOnly;
  21. public bool AllowHighPrecisionMv;
  22. // MBs, MbRows/Cols is in 16-pixel units; MiRows/Cols is in
  23. // ModeInfo (8-pixel) units.
  24. public int MBs;
  25. public int MbRows, MiRows;
  26. public int MbCols, MiCols;
  27. public int MiStride;
  28. /* Profile settings */
  29. public TxMode TxMode;
  30. public int BaseQindex;
  31. public int YDcDeltaQ;
  32. public int UvDcDeltaQ;
  33. public int UvAcDeltaQ;
  34. public Array8<Array2<short>> YDequant;
  35. public Array8<Array2<short>> UvDequant;
  36. /* We allocate a ModeInfo struct for each macroblock, together with
  37. an extra row on top and column on the left to simplify prediction. */
  38. public ArrayPtr<ModeInfo> Mip; /* Base of allocated array */
  39. public ArrayPtr<ModeInfo> Mi; /* Corresponds to upper left visible macroblock */
  40. public ArrayPtr<Ptr<ModeInfo>> MiGridBase;
  41. public ArrayPtr<Ptr<ModeInfo>> MiGridVisible;
  42. // Whether to use previous frame's motion vectors for prediction.
  43. public bool UsePrevFrameMvs;
  44. // Persistent mb segment id map used in prediction.
  45. public int SegMapIdx;
  46. public int PrevSegMapIdx;
  47. public Array2<ArrayPtr<byte>> SegMapArray;
  48. public ArrayPtr<byte> LastFrameSegMap;
  49. public ArrayPtr<byte> CurrentFrameSegMap;
  50. public byte InterpFilter;
  51. public LoopFilterInfoN LfInfo;
  52. public Array4<sbyte> RefFrameSignBias; /* Two state 0, 1 */
  53. public LoopFilter Lf;
  54. public Segmentation Seg;
  55. // Context probabilities for reference frame prediction
  56. public sbyte CompFixedRef;
  57. public Array2<sbyte> CompVarRef;
  58. public ReferenceMode ReferenceMode;
  59. public Ptr<Vp9EntropyProbs> Fc;
  60. public Ptr<Vp9BackwardUpdates> Counts;
  61. public bool FrameParallelDecodingMode;
  62. public int Log2TileCols, Log2TileRows;
  63. public ArrayPtr<sbyte> AboveSegContext;
  64. public ArrayPtr<sbyte> AboveContext;
  65. public bool FrameIsIntraOnly()
  66. {
  67. return FrameType == FrameType.KeyFrame || IntraOnly;
  68. }
  69. public bool CompoundReferenceAllowed()
  70. {
  71. int i;
  72. for (i = 1; i < Constants.RefsPerFrame; ++i)
  73. {
  74. if (RefFrameSignBias[i + 1] != RefFrameSignBias[1])
  75. {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. private static int CalcMiSize(int len)
  82. {
  83. // Len is in mi units.
  84. return len + Constants.MiBlockSize;
  85. }
  86. public void SetMbMi(int width, int height)
  87. {
  88. int alignedWidth = BitUtils.AlignPowerOfTwo(width, Constants.MiSizeLog2);
  89. int alignedHeight = BitUtils.AlignPowerOfTwo(height, Constants.MiSizeLog2);
  90. MiCols = alignedWidth >> Constants.MiSizeLog2;
  91. MiRows = alignedHeight >> Constants.MiSizeLog2;
  92. MiStride = CalcMiSize(MiCols);
  93. MbCols = (MiCols + 1) >> 1;
  94. MbRows = (MiRows + 1) >> 1;
  95. MBs = MbRows * MbCols;
  96. }
  97. public void AllocTileWorkerData(MemoryAllocator allocator, int tileCols, int tileRows)
  98. {
  99. TileWorkerData = allocator.Allocate<TileWorkerData>(tileCols * tileRows);
  100. }
  101. public void FreeTileWorkerData(MemoryAllocator allocator)
  102. {
  103. allocator.Free(TileWorkerData);
  104. }
  105. private void AllocSegMap(MemoryAllocator allocator, int segMapSize)
  106. {
  107. int i;
  108. for (i = 0; i < Constants.NumPingPongBuffers; ++i)
  109. {
  110. SegMapArray[i] = allocator.Allocate<byte>(segMapSize);
  111. }
  112. // Init the index.
  113. SegMapIdx = 0;
  114. PrevSegMapIdx = 1;
  115. CurrentFrameSegMap = SegMapArray[SegMapIdx];
  116. LastFrameSegMap = SegMapArray[PrevSegMapIdx];
  117. }
  118. private void FreeSegMap(MemoryAllocator allocator)
  119. {
  120. int i;
  121. for (i = 0; i < Constants.NumPingPongBuffers; ++i)
  122. {
  123. allocator.Free(SegMapArray[i]);
  124. SegMapArray[i] = ArrayPtr<byte>.Null;
  125. }
  126. CurrentFrameSegMap = ArrayPtr<byte>.Null;
  127. LastFrameSegMap = ArrayPtr<byte>.Null;
  128. }
  129. private void DecAllocMi(MemoryAllocator allocator, int miSize)
  130. {
  131. Mip = allocator.Allocate<ModeInfo>(miSize);
  132. MiGridBase = allocator.Allocate<Ptr<ModeInfo>>(miSize);
  133. }
  134. private void DecFreeMi(MemoryAllocator allocator)
  135. {
  136. allocator.Free(Mip);
  137. Mip = ArrayPtr<ModeInfo>.Null;
  138. allocator.Free(MiGridBase);
  139. MiGridBase = ArrayPtr<Ptr<ModeInfo>>.Null;
  140. }
  141. public void FreeContextBuffers(MemoryAllocator allocator)
  142. {
  143. DecFreeMi(allocator);
  144. FreeSegMap(allocator);
  145. allocator.Free(AboveContext);
  146. AboveContext = ArrayPtr<sbyte>.Null;
  147. allocator.Free(AboveSegContext);
  148. AboveSegContext = ArrayPtr<sbyte>.Null;
  149. allocator.Free(Lf.Lfm);
  150. Lf.Lfm = ArrayPtr<LoopFilterMask>.Null;
  151. allocator.Free(CurFrameMvs);
  152. CurFrameMvs = ArrayPtr<MvRef>.Null;
  153. if (UsePrevFrameMvs)
  154. {
  155. allocator.Free(PrevFrameMvs);
  156. PrevFrameMvs = ArrayPtr<MvRef>.Null;
  157. }
  158. }
  159. private void AllocLoopFilter(MemoryAllocator allocator)
  160. {
  161. // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region. The
  162. // stride and rows are rounded up / truncated to a multiple of 8.
  163. Lf.LfmStride = (MiCols + (Constants.MiBlockSize - 1)) >> 3;
  164. Lf.Lfm = allocator.Allocate<LoopFilterMask>(((MiRows + (Constants.MiBlockSize - 1)) >> 3) * Lf.LfmStride);
  165. }
  166. public void AllocContextBuffers(MemoryAllocator allocator, int width, int height)
  167. {
  168. SetMbMi(width, height);
  169. int newMiSize = MiStride * CalcMiSize(MiRows);
  170. if (newMiSize != 0)
  171. {
  172. DecAllocMi(allocator, newMiSize);
  173. }
  174. if (MiRows * MiCols != 0)
  175. {
  176. // Create the segmentation map structure and set to 0.
  177. AllocSegMap(allocator, MiRows * MiCols);
  178. }
  179. if (MiCols != 0)
  180. {
  181. AboveContext = allocator.Allocate<sbyte>(2 * TileInfo.MiColsAlignedToSb(MiCols) * Constants.MaxMbPlane);
  182. AboveSegContext = allocator.Allocate<sbyte>(TileInfo.MiColsAlignedToSb(MiCols));
  183. }
  184. AllocLoopFilter(allocator);
  185. CurFrameMvs = allocator.Allocate<MvRef>(MiRows * MiCols);
  186. // Using the same size as the current frame is fine here,
  187. // as this is never true when we have a resolution change.
  188. if (UsePrevFrameMvs)
  189. {
  190. PrevFrameMvs = allocator.Allocate<MvRef>(MiRows * MiCols);
  191. }
  192. }
  193. private unsafe void DecSetupMi()
  194. {
  195. Mi = Mip.Slice(MiStride + 1);
  196. MiGridVisible = MiGridBase.Slice(MiStride + 1);
  197. MemoryUtil.Fill(MiGridBase.ToPointer(), Ptr<ModeInfo>.Null, MiStride * (MiRows + 1));
  198. }
  199. public unsafe void InitContextBuffers()
  200. {
  201. DecSetupMi();
  202. if (!LastFrameSegMap.IsNull)
  203. {
  204. MemoryUtil.Fill(LastFrameSegMap.ToPointer(), (byte)0, MiRows * MiCols);
  205. }
  206. }
  207. private void SetPartitionProbs(ref MacroBlockD xd)
  208. {
  209. xd.PartitionProbs = FrameIsIntraOnly()
  210. ? new ArrayPtr<Array3<byte>>(ref Fc.Value.KfPartitionProb[0], 16)
  211. : new ArrayPtr<Array3<byte>>(ref Fc.Value.PartitionProb[0], 16);
  212. }
  213. internal void InitMacroBlockD(ref MacroBlockD xd, ArrayPtr<int> dqcoeff)
  214. {
  215. int i;
  216. for (i = 0; i < Constants.MaxMbPlane; ++i)
  217. {
  218. xd.Plane[i].DqCoeff = dqcoeff;
  219. xd.AboveContext[i] = AboveContext.Slice(i * 2 * TileInfo.MiColsAlignedToSb(MiCols));
  220. if (i == 0)
  221. {
  222. MemoryUtil.Copy(ref xd.Plane[i].SegDequant, ref YDequant);
  223. }
  224. else
  225. {
  226. MemoryUtil.Copy(ref xd.Plane[i].SegDequant, ref UvDequant);
  227. }
  228. xd.Fc = new Ptr<Vp9EntropyProbs>(ref Fc.Value);
  229. }
  230. xd.AboveSegContext = AboveSegContext;
  231. xd.MiStride = MiStride;
  232. xd.ErrorInfo = new Ptr<InternalErrorInfo>(ref Error);
  233. SetPartitionProbs(ref xd);
  234. }
  235. public void SetupSegmentationDequant()
  236. {
  237. const BitDepth bitDepth = BitDepth.Bits8; // TODO: Configurable
  238. // Build y/uv dequant values based on segmentation.
  239. if (Seg.Enabled)
  240. {
  241. int i;
  242. for (i = 0; i < Constants.MaxSegments; ++i)
  243. {
  244. int qIndex = QuantCommon.GetQIndex(ref Seg, i, BaseQindex);
  245. YDequant[i][0] = QuantCommon.DcQuant(qIndex, YDcDeltaQ, bitDepth);
  246. YDequant[i][1] = QuantCommon.AcQuant(qIndex, 0, bitDepth);
  247. UvDequant[i][0] = QuantCommon.DcQuant(qIndex, UvDcDeltaQ, bitDepth);
  248. UvDequant[i][1] = QuantCommon.AcQuant(qIndex, UvAcDeltaQ, bitDepth);
  249. }
  250. }
  251. else
  252. {
  253. int qIndex = BaseQindex;
  254. // When segmentation is disabled, only the first value is used. The
  255. // remaining are don't cares.
  256. YDequant[0][0] = QuantCommon.DcQuant(qIndex, YDcDeltaQ, bitDepth);
  257. YDequant[0][1] = QuantCommon.AcQuant(qIndex, 0, bitDepth);
  258. UvDequant[0][0] = QuantCommon.DcQuant(qIndex, UvDcDeltaQ, bitDepth);
  259. UvDequant[0][1] = QuantCommon.AcQuant(qIndex, UvAcDeltaQ, bitDepth);
  260. }
  261. }
  262. public void SetupScaleFactors()
  263. {
  264. for (int i = 0; i < Constants.RefsPerFrame; ++i)
  265. {
  266. ref RefBuffer refBuf = ref FrameRefs[i];
  267. refBuf.Sf.SetupScaleFactorsForFrame(refBuf.Buf.Width, refBuf.Buf.Height, Width, Height);
  268. }
  269. }
  270. }
  271. }