Vp9Common.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 int AboveContextAllocCols;
  66. public bool FrameIsIntraOnly()
  67. {
  68. return FrameType == FrameType.KeyFrame || IntraOnly;
  69. }
  70. public bool CompoundReferenceAllowed()
  71. {
  72. int i;
  73. for (i = 1; i < Constants.RefsPerFrame; ++i)
  74. {
  75. if (RefFrameSignBias[i + 1] != RefFrameSignBias[1])
  76. {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. private static int CalcMiSize(int len)
  83. {
  84. // Len is in mi units.
  85. return len + Constants.MiBlockSize;
  86. }
  87. public void SetMbMi(int width, int height)
  88. {
  89. int alignedWidth = BitUtils.AlignPowerOfTwo(width, Constants.MiSizeLog2);
  90. int alignedHeight = BitUtils.AlignPowerOfTwo(height, Constants.MiSizeLog2);
  91. MiCols = alignedWidth >> Constants.MiSizeLog2;
  92. MiRows = alignedHeight >> Constants.MiSizeLog2;
  93. MiStride = CalcMiSize(MiCols);
  94. MbCols = (MiCols + 1) >> 1;
  95. MbRows = (MiRows + 1) >> 1;
  96. MBs = MbRows * MbCols;
  97. }
  98. public void AllocTileWorkerData(MemoryAllocator allocator, int tileCols, int tileRows)
  99. {
  100. TileWorkerData = allocator.Allocate<TileWorkerData>(tileCols * tileRows);
  101. }
  102. public void FreeTileWorkerData(MemoryAllocator allocator)
  103. {
  104. allocator.Free(TileWorkerData);
  105. }
  106. private void AllocSegMap(MemoryAllocator allocator, int segMapSize)
  107. {
  108. int i;
  109. for (i = 0; i < Constants.NumPingPongBuffers; ++i)
  110. {
  111. SegMapArray[i] = allocator.Allocate<byte>(segMapSize);
  112. }
  113. // Init the index.
  114. SegMapIdx = 0;
  115. PrevSegMapIdx = 1;
  116. CurrentFrameSegMap = SegMapArray[SegMapIdx];
  117. LastFrameSegMap = SegMapArray[PrevSegMapIdx];
  118. }
  119. private void FreeSegMap(MemoryAllocator allocator)
  120. {
  121. int i;
  122. for (i = 0; i < Constants.NumPingPongBuffers; ++i)
  123. {
  124. allocator.Free(SegMapArray[i]);
  125. SegMapArray[i] = ArrayPtr<byte>.Null;
  126. }
  127. CurrentFrameSegMap = ArrayPtr<byte>.Null;
  128. LastFrameSegMap = ArrayPtr<byte>.Null;
  129. }
  130. private void DecAllocMi(MemoryAllocator allocator, int miSize)
  131. {
  132. Mip = allocator.Allocate<ModeInfo>(miSize);
  133. MiGridBase = allocator.Allocate<Ptr<ModeInfo>>(miSize);
  134. }
  135. private void DecFreeMi(MemoryAllocator allocator)
  136. {
  137. allocator.Free(Mip);
  138. Mip = ArrayPtr<ModeInfo>.Null;
  139. allocator.Free(MiGridBase);
  140. MiGridBase = ArrayPtr<Ptr<ModeInfo>>.Null;
  141. }
  142. public void FreeContextBuffers(MemoryAllocator allocator)
  143. {
  144. DecFreeMi(allocator);
  145. FreeSegMap(allocator);
  146. allocator.Free(AboveContext);
  147. AboveContext = ArrayPtr<sbyte>.Null;
  148. allocator.Free(AboveSegContext);
  149. AboveSegContext = ArrayPtr<sbyte>.Null;
  150. allocator.Free(Lf.Lfm);
  151. Lf.Lfm = ArrayPtr<LoopFilterMask>.Null;
  152. allocator.Free(CurFrameMvs);
  153. CurFrameMvs = ArrayPtr<MvRef>.Null;
  154. if (UsePrevFrameMvs)
  155. {
  156. allocator.Free(PrevFrameMvs);
  157. PrevFrameMvs = ArrayPtr<MvRef>.Null;
  158. }
  159. }
  160. private void AllocLoopFilter(MemoryAllocator allocator)
  161. {
  162. // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region. The
  163. // stride and rows are rounded up / truncated to a multiple of 8.
  164. Lf.LfmStride = (MiCols + (Constants.MiBlockSize - 1)) >> 3;
  165. Lf.Lfm = allocator.Allocate<LoopFilterMask>(((MiRows + (Constants.MiBlockSize - 1)) >> 3) * Lf.LfmStride);
  166. }
  167. public void AllocContextBuffers(MemoryAllocator allocator, int width, int height)
  168. {
  169. SetMbMi(width, height);
  170. int newMiSize = MiStride * CalcMiSize(MiRows);
  171. if (newMiSize != 0)
  172. {
  173. DecAllocMi(allocator, newMiSize);
  174. }
  175. if (MiRows * MiCols != 0)
  176. {
  177. // Create the segmentation map structure and set to 0.
  178. AllocSegMap(allocator, MiRows * MiCols);
  179. }
  180. if (MiCols != 0)
  181. {
  182. AboveContext = allocator.Allocate<sbyte>(2 * TileInfo.MiColsAlignedToSb(MiCols) * Constants.MaxMbPlane);
  183. AboveSegContext = allocator.Allocate<sbyte>(TileInfo.MiColsAlignedToSb(MiCols));
  184. }
  185. AllocLoopFilter(allocator);
  186. CurFrameMvs = allocator.Allocate<MvRef>(MiRows * MiCols);
  187. // Using the same size as the current frame is fine here,
  188. // as this is never true when we have a resolution change.
  189. if (UsePrevFrameMvs)
  190. {
  191. PrevFrameMvs = allocator.Allocate<MvRef>(MiRows * MiCols);
  192. }
  193. }
  194. private unsafe void DecSetupMi()
  195. {
  196. Mi = Mip.Slice(MiStride + 1);
  197. MiGridVisible = MiGridBase.Slice(MiStride + 1);
  198. MemoryUtil.Fill(MiGridBase.ToPointer(), Ptr<ModeInfo>.Null, MiStride * (MiRows + 1));
  199. }
  200. public unsafe void InitContextBuffers()
  201. {
  202. DecSetupMi();
  203. if (!LastFrameSegMap.IsNull)
  204. {
  205. MemoryUtil.Fill(LastFrameSegMap.ToPointer(), (byte)0, MiRows * MiCols);
  206. }
  207. }
  208. private void SetPartitionProbs(ref MacroBlockD xd)
  209. {
  210. xd.PartitionProbs = FrameIsIntraOnly()
  211. ? new ArrayPtr<Array3<byte>>(ref Fc.Value.KfPartitionProb[0], 16)
  212. : new ArrayPtr<Array3<byte>>(ref Fc.Value.PartitionProb[0], 16);
  213. }
  214. internal void InitMacroBlockD(ref MacroBlockD xd, ArrayPtr<int> dqcoeff)
  215. {
  216. int i;
  217. for (i = 0; i < Constants.MaxMbPlane; ++i)
  218. {
  219. xd.Plane[i].DqCoeff = dqcoeff;
  220. xd.AboveContext[i] = AboveContext.Slice(i * 2 * TileInfo.MiColsAlignedToSb(MiCols));
  221. if (i == 0)
  222. {
  223. MemoryUtil.Copy(ref xd.Plane[i].SegDequant, ref YDequant);
  224. }
  225. else
  226. {
  227. MemoryUtil.Copy(ref xd.Plane[i].SegDequant, ref UvDequant);
  228. }
  229. xd.Fc = new Ptr<Vp9EntropyProbs>(ref Fc.Value);
  230. }
  231. xd.AboveSegContext = AboveSegContext;
  232. xd.MiStride = MiStride;
  233. xd.ErrorInfo = new Ptr<InternalErrorInfo>(ref Error);
  234. SetPartitionProbs(ref xd);
  235. }
  236. public void SetupSegmentationDequant()
  237. {
  238. const BitDepth bitDepth = BitDepth.Bits8; // TODO: Configurable
  239. // Build y/uv dequant values based on segmentation.
  240. if (Seg.Enabled)
  241. {
  242. int i;
  243. for (i = 0; i < Constants.MaxSegments; ++i)
  244. {
  245. int qIndex = QuantCommon.GetQIndex(ref Seg, i, BaseQindex);
  246. YDequant[i][0] = QuantCommon.DcQuant(qIndex, YDcDeltaQ, bitDepth);
  247. YDequant[i][1] = QuantCommon.AcQuant(qIndex, 0, bitDepth);
  248. UvDequant[i][0] = QuantCommon.DcQuant(qIndex, UvDcDeltaQ, bitDepth);
  249. UvDequant[i][1] = QuantCommon.AcQuant(qIndex, UvAcDeltaQ, bitDepth);
  250. }
  251. }
  252. else
  253. {
  254. int qIndex = BaseQindex;
  255. // When segmentation is disabled, only the first value is used. The
  256. // remaining are don't cares.
  257. YDequant[0][0] = QuantCommon.DcQuant(qIndex, YDcDeltaQ, bitDepth);
  258. YDequant[0][1] = QuantCommon.AcQuant(qIndex, 0, bitDepth);
  259. UvDequant[0][0] = QuantCommon.DcQuant(qIndex, UvDcDeltaQ, bitDepth);
  260. UvDequant[0][1] = QuantCommon.AcQuant(qIndex, UvAcDeltaQ, bitDepth);
  261. }
  262. }
  263. public void SetupScaleFactors()
  264. {
  265. for (int i = 0; i < Constants.RefsPerFrame; ++i)
  266. {
  267. ref RefBuffer refBuf = ref FrameRefs[i];
  268. refBuf.Sf.SetupScaleFactorsForFrame(refBuf.Buf.Width, refBuf.Buf.Height, Width, Height);
  269. }
  270. }
  271. }
  272. }