Vp9Common.cs 11 KB

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