ReconInter.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. Mv res = 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. return res;
  93. }
  94. private static int RoundMvCompQ2(int value)
  95. {
  96. return (value < 0 ? value - 1 : value + 1) / 2;
  97. }
  98. private static Mv MiMvPredQ2(ref ModeInfo mi, int idx, int block0, int block1)
  99. {
  100. Mv res = new Mv()
  101. {
  102. Row = (short)RoundMvCompQ2(
  103. mi.Bmi[block0].Mv[idx].Row +
  104. mi.Bmi[block1].Mv[idx].Row),
  105. Col = (short)RoundMvCompQ2(
  106. mi.Bmi[block0].Mv[idx].Col +
  107. mi.Bmi[block1].Mv[idx].Col)
  108. };
  109. return res;
  110. }
  111. public static Mv ClampMvToUmvBorderSb(ref MacroBlockD xd, ref Mv srcMv, int bw, int bh, int ssX, int ssY)
  112. {
  113. // If the MV points so far into the UMV border that no visible pixels
  114. // are used for reconstruction, the subpel part of the MV can be
  115. // discarded and the MV limited to 16 pixels with equivalent results.
  116. int spelLeft = (Constants.Vp9InterpExtend + bw) << SubpelBits;
  117. int spelRight = spelLeft - SubpelShifts;
  118. int spelTop = (Constants.Vp9InterpExtend + bh) << SubpelBits;
  119. int spelBottom = spelTop - SubpelShifts;
  120. Mv clampedMv = new Mv()
  121. {
  122. Row = (short)(srcMv.Row * (1 << (1 - ssY))),
  123. Col = (short)(srcMv.Col * (1 << (1 - ssX)))
  124. };
  125. Debug.Assert(ssX <= 1);
  126. Debug.Assert(ssY <= 1);
  127. clampedMv.ClampMv(
  128. xd.MbToLeftEdge * (1 << (1 - ssX)) - spelLeft,
  129. xd.MbToRightEdge * (1 << (1 - ssX)) + spelRight,
  130. xd.MbToTopEdge * (1 << (1 - ssY)) - spelTop,
  131. xd.MbToBottomEdge * (1 << (1 - ssY)) + spelBottom);
  132. return clampedMv;
  133. }
  134. public static Mv AverageSplitMvs(ref MacroBlockDPlane pd, ref ModeInfo mi, int refr, int block)
  135. {
  136. int ssIdx = ((pd.SubsamplingX > 0 ? 1 : 0) << 1) | (pd.SubsamplingY > 0 ? 1 : 0);
  137. Mv res = new Mv();
  138. switch (ssIdx)
  139. {
  140. case 0: res = mi.Bmi[block].Mv[refr]; break;
  141. case 1: res = MiMvPredQ2(ref mi, refr, block, block + 2); break;
  142. case 2: res = MiMvPredQ2(ref mi, refr, block, block + 1); break;
  143. case 3: res = MiMvPredQ4(ref mi, refr); break;
  144. default: Debug.Assert(ssIdx <= 3 && ssIdx >= 0); break;
  145. }
  146. return res;
  147. }
  148. private static int ScaledBufferOffset(int xOffset, int yOffset, int stride, Ptr<ScaleFactors> sf)
  149. {
  150. int x = !sf.IsNull ? sf.Value.ScaleValueX(xOffset) : xOffset;
  151. int y = !sf.IsNull ? sf.Value.ScaleValueY(yOffset) : yOffset;
  152. return y * stride + x;
  153. }
  154. private static void SetupPredPlanes(
  155. ref Buf2D dst,
  156. ArrayPtr<byte> src,
  157. int stride,
  158. int miRow,
  159. int miCol,
  160. Ptr<ScaleFactors> scale,
  161. int subsamplingX,
  162. int subsamplingY)
  163. {
  164. int x = (Constants.MiSize * miCol) >> subsamplingX;
  165. int y = (Constants.MiSize * miRow) >> subsamplingY;
  166. dst.Buf = src.Slice(ScaledBufferOffset(x, y, stride, scale));
  167. dst.Stride = stride;
  168. }
  169. public static void SetupDstPlanes(
  170. ref Array3<MacroBlockDPlane> planes,
  171. ref Surface src,
  172. int miRow,
  173. int miCol)
  174. {
  175. Span<ArrayPtr<byte>> buffers = stackalloc ArrayPtr<byte>[Constants.MaxMbPlane];
  176. buffers[0] = src.YBuffer;
  177. buffers[1] = src.UBuffer;
  178. buffers[2] = src.VBuffer;
  179. Span<int> strides = stackalloc int[Constants.MaxMbPlane];
  180. strides[0] = src.Stride;
  181. strides[1] = src.UvStride;
  182. strides[2] = src.UvStride;
  183. int i;
  184. for (i = 0; i < Constants.MaxMbPlane; ++i)
  185. {
  186. ref MacroBlockDPlane pd = ref planes[i];
  187. SetupPredPlanes(ref pd.Dst, buffers[i], strides[i], miRow, miCol, Ptr<ScaleFactors>.Null, pd.SubsamplingX, pd.SubsamplingY);
  188. }
  189. }
  190. public static void SetupPrePlanes(
  191. ref MacroBlockD xd,
  192. int idx,
  193. ref Surface src,
  194. int miRow,
  195. int miCol,
  196. Ptr<ScaleFactors> sf)
  197. {
  198. if (!src.YBuffer.IsNull && !src.UBuffer.IsNull && !src.VBuffer.IsNull)
  199. {
  200. Span<ArrayPtr<byte>> buffers = stackalloc ArrayPtr<byte>[Constants.MaxMbPlane];
  201. buffers[0] = src.YBuffer;
  202. buffers[1] = src.UBuffer;
  203. buffers[2] = src.VBuffer;
  204. Span<int> strides = stackalloc int[Constants.MaxMbPlane];
  205. strides[0] = src.Stride;
  206. strides[1] = src.UvStride;
  207. strides[2] = src.UvStride;
  208. int i;
  209. for (i = 0; i < Constants.MaxMbPlane; ++i)
  210. {
  211. ref MacroBlockDPlane pd = ref xd.Plane[i];
  212. SetupPredPlanes(ref pd.Pre[idx], buffers[i], strides[i], miRow, miCol, sf, pd.SubsamplingX, pd.SubsamplingY);
  213. }
  214. }
  215. }
  216. }
  217. }