ReconInter.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Graphics.Nvdec.Vp9.Types;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using static Ryujinx.Graphics.Nvdec.Vp9.Dsp.Filter;
  7. namespace Ryujinx.Graphics.Nvdec.Vp9
  8. {
  9. internal static class ReconInter
  10. {
  11. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  12. public static unsafe void InterPredictor(
  13. byte* src,
  14. int srcStride,
  15. byte* dst,
  16. int dstStride,
  17. int subpelX,
  18. int subpelY,
  19. ref ScaleFactors sf,
  20. int w,
  21. int h,
  22. int refr,
  23. Array8<short>[] kernel,
  24. int xs,
  25. int ys)
  26. {
  27. sf.InterPredict(
  28. subpelX != 0 ? 1 : 0,
  29. subpelY != 0 ? 1 : 0,
  30. refr,
  31. src,
  32. srcStride,
  33. dst,
  34. dstStride,
  35. subpelX,
  36. subpelY,
  37. w,
  38. h,
  39. kernel,
  40. xs,
  41. ys);
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public static unsafe void HighbdInterPredictor(
  45. ushort* src,
  46. int srcStride,
  47. ushort* dst,
  48. int dstStride,
  49. int subpelX,
  50. int subpelY,
  51. ref ScaleFactors sf,
  52. int w,
  53. int h,
  54. int refr,
  55. Array8<short>[] kernel,
  56. int xs,
  57. int ys,
  58. int bd)
  59. {
  60. sf.HighbdInterPredict(
  61. subpelX != 0 ? 1 : 0,
  62. subpelY != 0 ? 1 : 0,
  63. refr,
  64. src,
  65. srcStride,
  66. dst,
  67. dstStride,
  68. subpelX,
  69. subpelY,
  70. w,
  71. h,
  72. kernel,
  73. xs,
  74. ys,
  75. bd);
  76. }
  77. private static int RoundMvCompQ4(int value)
  78. {
  79. return (value < 0 ? value - 2 : value + 2) / 4;
  80. }
  81. private static Mv MiMvPredQ4(ref ModeInfo mi, int idx)
  82. {
  83. return new Mv
  84. {
  85. Row = (short)RoundMvCompQ4(
  86. mi.Bmi[0].Mv[idx].Row + mi.Bmi[1].Mv[idx].Row +
  87. mi.Bmi[2].Mv[idx].Row + mi.Bmi[3].Mv[idx].Row),
  88. Col = (short)RoundMvCompQ4(
  89. mi.Bmi[0].Mv[idx].Col + mi.Bmi[1].Mv[idx].Col +
  90. mi.Bmi[2].Mv[idx].Col + mi.Bmi[3].Mv[idx].Col),
  91. };
  92. }
  93. private static int RoundMvCompQ2(int value)
  94. {
  95. return (value < 0 ? value - 1 : value + 1) / 2;
  96. }
  97. private static Mv MiMvPredQ2(ref ModeInfo mi, int idx, int block0, int block1)
  98. {
  99. return new Mv
  100. {
  101. Row = (short)RoundMvCompQ2(
  102. mi.Bmi[block0].Mv[idx].Row +
  103. mi.Bmi[block1].Mv[idx].Row),
  104. Col = (short)RoundMvCompQ2(
  105. mi.Bmi[block0].Mv[idx].Col +
  106. mi.Bmi[block1].Mv[idx].Col),
  107. };
  108. }
  109. public static Mv ClampMvToUmvBorderSb(ref MacroBlockD xd, ref Mv srcMv, int bw, int bh, int ssX, int ssY)
  110. {
  111. // If the MV points so far into the UMV border that no visible pixels
  112. // are used for reconstruction, the subpel part of the MV can be
  113. // discarded and the MV limited to 16 pixels with equivalent results.
  114. int spelLeft = (Constants.Vp9InterpExtend + bw) << SubpelBits;
  115. int spelRight = spelLeft - SubpelShifts;
  116. int spelTop = (Constants.Vp9InterpExtend + bh) << SubpelBits;
  117. int spelBottom = spelTop - SubpelShifts;
  118. Mv clampedMv = new()
  119. {
  120. Row = (short)(srcMv.Row * (1 << (1 - ssY))),
  121. Col = (short)(srcMv.Col * (1 << (1 - ssX))),
  122. };
  123. Debug.Assert(ssX <= 1);
  124. Debug.Assert(ssY <= 1);
  125. clampedMv.ClampMv(
  126. xd.MbToLeftEdge * (1 << (1 - ssX)) - spelLeft,
  127. xd.MbToRightEdge * (1 << (1 - ssX)) + spelRight,
  128. xd.MbToTopEdge * (1 << (1 - ssY)) - spelTop,
  129. xd.MbToBottomEdge * (1 << (1 - ssY)) + spelBottom);
  130. return clampedMv;
  131. }
  132. public static Mv AverageSplitMvs(ref MacroBlockDPlane pd, ref ModeInfo mi, int refr, int block)
  133. {
  134. int ssIdx = ((pd.SubsamplingX > 0 ? 1 : 0) << 1) | (pd.SubsamplingY > 0 ? 1 : 0);
  135. Mv res = new();
  136. switch (ssIdx)
  137. {
  138. case 0:
  139. res = mi.Bmi[block].Mv[refr];
  140. break;
  141. case 1:
  142. res = MiMvPredQ2(ref mi, refr, block, block + 2);
  143. break;
  144. case 2:
  145. res = MiMvPredQ2(ref mi, refr, block, block + 1);
  146. break;
  147. case 3:
  148. res = MiMvPredQ4(ref mi, refr);
  149. break;
  150. default:
  151. Debug.Assert(ssIdx <= 3 && ssIdx >= 0);
  152. break;
  153. }
  154. return res;
  155. }
  156. private static int ScaledBufferOffset(int xOffset, int yOffset, int stride, Ptr<ScaleFactors> sf)
  157. {
  158. int x = !sf.IsNull ? sf.Value.ScaleValueX(xOffset) : xOffset;
  159. int y = !sf.IsNull ? sf.Value.ScaleValueY(yOffset) : yOffset;
  160. return y * stride + x;
  161. }
  162. private static void SetupPredPlanes(
  163. ref Buf2D dst,
  164. ArrayPtr<byte> src,
  165. int stride,
  166. int miRow,
  167. int miCol,
  168. Ptr<ScaleFactors> scale,
  169. int subsamplingX,
  170. int subsamplingY)
  171. {
  172. int x = (Constants.MiSize * miCol) >> subsamplingX;
  173. int y = (Constants.MiSize * miRow) >> subsamplingY;
  174. dst.Buf = src.Slice(ScaledBufferOffset(x, y, stride, scale));
  175. dst.Stride = stride;
  176. }
  177. public static void SetupDstPlanes(
  178. ref Array3<MacroBlockDPlane> planes,
  179. ref Surface src,
  180. int miRow,
  181. int miCol)
  182. {
  183. Span<ArrayPtr<byte>> buffers = stackalloc ArrayPtr<byte>[Constants.MaxMbPlane];
  184. buffers[0] = src.YBuffer;
  185. buffers[1] = src.UBuffer;
  186. buffers[2] = src.VBuffer;
  187. Span<int> strides = stackalloc int[Constants.MaxMbPlane];
  188. strides[0] = src.Stride;
  189. strides[1] = src.UvStride;
  190. strides[2] = src.UvStride;
  191. int i;
  192. for (i = 0; i < Constants.MaxMbPlane; ++i)
  193. {
  194. ref MacroBlockDPlane pd = ref planes[i];
  195. SetupPredPlanes(ref pd.Dst, buffers[i], strides[i], miRow, miCol, Ptr<ScaleFactors>.Null, pd.SubsamplingX, pd.SubsamplingY);
  196. }
  197. }
  198. public static void SetupPrePlanes(
  199. ref MacroBlockD xd,
  200. int idx,
  201. ref Surface src,
  202. int miRow,
  203. int miCol,
  204. Ptr<ScaleFactors> sf)
  205. {
  206. if (!src.YBuffer.IsNull && !src.UBuffer.IsNull && !src.VBuffer.IsNull)
  207. {
  208. Span<ArrayPtr<byte>> buffers = stackalloc ArrayPtr<byte>[Constants.MaxMbPlane];
  209. buffers[0] = src.YBuffer;
  210. buffers[1] = src.UBuffer;
  211. buffers[2] = src.VBuffer;
  212. Span<int> strides = stackalloc int[Constants.MaxMbPlane];
  213. strides[0] = src.Stride;
  214. strides[1] = src.UvStride;
  215. strides[2] = src.UvStride;
  216. int i;
  217. for (i = 0; i < Constants.MaxMbPlane; ++i)
  218. {
  219. ref MacroBlockDPlane pd = ref xd.Plane[i];
  220. SetupPredPlanes(ref pd.Pre[idx], buffers[i], strides[i], miRow, miCol, sf, pd.SubsamplingX, pd.SubsamplingY);
  221. }
  222. }
  223. }
  224. }
  225. }