VideoDecoder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using ARMeilleure.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using Ryujinx.Graphics.Memory;
  4. using Ryujinx.Graphics.Texture;
  5. using Ryujinx.Graphics.Vic;
  6. using System;
  7. namespace Ryujinx.Graphics.VDec
  8. {
  9. unsafe class VideoDecoder
  10. {
  11. private NvGpu _gpu;
  12. private H264Decoder _h264Decoder;
  13. private Vp9Decoder _vp9Decoder;
  14. private VideoCodec _currentVideoCodec;
  15. private long _decoderContextAddress;
  16. private long _frameDataAddress;
  17. private long _vpxCurrLumaAddress;
  18. private long _vpxRef0LumaAddress;
  19. private long _vpxRef1LumaAddress;
  20. private long _vpxRef2LumaAddress;
  21. private long _vpxCurrChromaAddress;
  22. private long _vpxRef0ChromaAddress;
  23. private long _vpxRef1ChromaAddress;
  24. private long _vpxRef2ChromaAddress;
  25. private long _vpxProbTablesAddress;
  26. public VideoDecoder(NvGpu gpu)
  27. {
  28. _gpu = gpu;
  29. _h264Decoder = new H264Decoder();
  30. _vp9Decoder = new Vp9Decoder();
  31. }
  32. public void Process(NvGpuVmm vmm, int methodOffset, int[] arguments)
  33. {
  34. VideoDecoderMeth method = (VideoDecoderMeth)methodOffset;
  35. switch (method)
  36. {
  37. case VideoDecoderMeth.SetVideoCodec: SetVideoCodec (vmm, arguments); break;
  38. case VideoDecoderMeth.Execute: Execute (vmm, arguments); break;
  39. case VideoDecoderMeth.SetDecoderCtxAddr: SetDecoderCtxAddr (vmm, arguments); break;
  40. case VideoDecoderMeth.SetFrameDataAddr: SetFrameDataAddr (vmm, arguments); break;
  41. case VideoDecoderMeth.SetVpxCurrLumaAddr: SetVpxCurrLumaAddr (vmm, arguments); break;
  42. case VideoDecoderMeth.SetVpxRef0LumaAddr: SetVpxRef0LumaAddr (vmm, arguments); break;
  43. case VideoDecoderMeth.SetVpxRef1LumaAddr: SetVpxRef1LumaAddr (vmm, arguments); break;
  44. case VideoDecoderMeth.SetVpxRef2LumaAddr: SetVpxRef2LumaAddr (vmm, arguments); break;
  45. case VideoDecoderMeth.SetVpxCurrChromaAddr: SetVpxCurrChromaAddr(vmm, arguments); break;
  46. case VideoDecoderMeth.SetVpxRef0ChromaAddr: SetVpxRef0ChromaAddr(vmm, arguments); break;
  47. case VideoDecoderMeth.SetVpxRef1ChromaAddr: SetVpxRef1ChromaAddr(vmm, arguments); break;
  48. case VideoDecoderMeth.SetVpxRef2ChromaAddr: SetVpxRef2ChromaAddr(vmm, arguments); break;
  49. case VideoDecoderMeth.SetVpxProbTablesAddr: SetVpxProbTablesAddr(vmm, arguments); break;
  50. }
  51. }
  52. private void SetVideoCodec(NvGpuVmm vmm, int[] arguments)
  53. {
  54. _currentVideoCodec = (VideoCodec)arguments[0];
  55. }
  56. private void Execute(NvGpuVmm vmm, int[] arguments)
  57. {
  58. if (_currentVideoCodec == VideoCodec.H264)
  59. {
  60. int frameDataSize = vmm.ReadInt32(_decoderContextAddress + 0x48);
  61. H264ParameterSets Params = MemoryHelper.Read<H264ParameterSets>(vmm.Memory, vmm.GetPhysicalAddress(_decoderContextAddress + 0x58));
  62. H264Matrices matrices = new H264Matrices()
  63. {
  64. ScalingMatrix4 = vmm.ReadBytes(_decoderContextAddress + 0x1c0, 6 * 16),
  65. ScalingMatrix8 = vmm.ReadBytes(_decoderContextAddress + 0x220, 2 * 64)
  66. };
  67. byte[] frameData = vmm.ReadBytes(_frameDataAddress, frameDataSize);
  68. _h264Decoder.Decode(Params, matrices, frameData);
  69. }
  70. else if (_currentVideoCodec == VideoCodec.Vp9)
  71. {
  72. int frameDataSize = vmm.ReadInt32(_decoderContextAddress + 0x30);
  73. Vp9FrameKeys keys = new Vp9FrameKeys()
  74. {
  75. CurrKey = vmm.GetPhysicalAddress(_vpxCurrLumaAddress),
  76. Ref0Key = vmm.GetPhysicalAddress(_vpxRef0LumaAddress),
  77. Ref1Key = vmm.GetPhysicalAddress(_vpxRef1LumaAddress),
  78. Ref2Key = vmm.GetPhysicalAddress(_vpxRef2LumaAddress)
  79. };
  80. Vp9FrameHeader header = MemoryHelper.Read<Vp9FrameHeader>(vmm.Memory, vmm.GetPhysicalAddress(_decoderContextAddress + 0x48));
  81. Vp9ProbabilityTables probs = new Vp9ProbabilityTables()
  82. {
  83. SegmentationTreeProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x387, 0x7),
  84. SegmentationPredProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x38e, 0x3),
  85. Tx8x8Probs = vmm.ReadBytes(_vpxProbTablesAddress + 0x470, 0x2),
  86. Tx16x16Probs = vmm.ReadBytes(_vpxProbTablesAddress + 0x472, 0x4),
  87. Tx32x32Probs = vmm.ReadBytes(_vpxProbTablesAddress + 0x476, 0x6),
  88. CoefProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x5a0, 0x900),
  89. SkipProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x537, 0x3),
  90. InterModeProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x400, 0x1c),
  91. InterpFilterProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x52a, 0x8),
  92. IsInterProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x41c, 0x4),
  93. CompModeProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x532, 0x5),
  94. SingleRefProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x580, 0xa),
  95. CompRefProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x58a, 0x5),
  96. YModeProbs0 = vmm.ReadBytes(_vpxProbTablesAddress + 0x480, 0x20),
  97. YModeProbs1 = vmm.ReadBytes(_vpxProbTablesAddress + 0x47c, 0x4),
  98. PartitionProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x4e0, 0x40),
  99. MvJointProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x53b, 0x3),
  100. MvSignProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x53e, 0x3),
  101. MvClassProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x54c, 0x14),
  102. MvClass0BitProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x540, 0x3),
  103. MvBitsProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x56c, 0x14),
  104. MvClass0FrProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x560, 0xc),
  105. MvFrProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x542, 0x6),
  106. MvClass0HpProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x548, 0x2),
  107. MvHpProbs = vmm.ReadBytes(_vpxProbTablesAddress + 0x54a, 0x2)
  108. };
  109. byte[] frameData = vmm.ReadBytes(_frameDataAddress, frameDataSize);
  110. _vp9Decoder.Decode(keys, header, probs, frameData);
  111. }
  112. else
  113. {
  114. ThrowUnimplementedCodec();
  115. }
  116. }
  117. private void SetDecoderCtxAddr(NvGpuVmm vmm, int[] arguments)
  118. {
  119. _decoderContextAddress = GetAddress(arguments);
  120. }
  121. private void SetFrameDataAddr(NvGpuVmm vmm, int[] arguments)
  122. {
  123. _frameDataAddress = GetAddress(arguments);
  124. }
  125. private void SetVpxCurrLumaAddr(NvGpuVmm vmm, int[] arguments)
  126. {
  127. _vpxCurrLumaAddress = GetAddress(arguments);
  128. }
  129. private void SetVpxRef0LumaAddr(NvGpuVmm vmm, int[] arguments)
  130. {
  131. _vpxRef0LumaAddress = GetAddress(arguments);
  132. }
  133. private void SetVpxRef1LumaAddr(NvGpuVmm vmm, int[] arguments)
  134. {
  135. _vpxRef1LumaAddress = GetAddress(arguments);
  136. }
  137. private void SetVpxRef2LumaAddr(NvGpuVmm vmm, int[] arguments)
  138. {
  139. _vpxRef2LumaAddress = GetAddress(arguments);
  140. }
  141. private void SetVpxCurrChromaAddr(NvGpuVmm vmm, int[] arguments)
  142. {
  143. _vpxCurrChromaAddress = GetAddress(arguments);
  144. }
  145. private void SetVpxRef0ChromaAddr(NvGpuVmm vmm, int[] arguments)
  146. {
  147. _vpxRef0ChromaAddress = GetAddress(arguments);
  148. }
  149. private void SetVpxRef1ChromaAddr(NvGpuVmm vmm, int[] arguments)
  150. {
  151. _vpxRef1ChromaAddress = GetAddress(arguments);
  152. }
  153. private void SetVpxRef2ChromaAddr(NvGpuVmm vmm, int[] arguments)
  154. {
  155. _vpxRef2ChromaAddress = GetAddress(arguments);
  156. }
  157. private void SetVpxProbTablesAddr(NvGpuVmm vmm, int[] arguments)
  158. {
  159. _vpxProbTablesAddress = GetAddress(arguments);
  160. }
  161. private static long GetAddress(int[] arguments)
  162. {
  163. return (long)(uint)arguments[0] << 8;
  164. }
  165. internal void CopyPlanes(NvGpuVmm vmm, SurfaceOutputConfig outputConfig)
  166. {
  167. switch (outputConfig.PixelFormat)
  168. {
  169. case SurfacePixelFormat.Rgba8: CopyPlanesRgba8 (vmm, outputConfig); break;
  170. case SurfacePixelFormat.Yuv420P: CopyPlanesYuv420P(vmm, outputConfig); break;
  171. default: ThrowUnimplementedPixelFormat(outputConfig.PixelFormat); break;
  172. }
  173. }
  174. private void CopyPlanesRgba8(NvGpuVmm vmm, SurfaceOutputConfig outputConfig)
  175. {
  176. FFmpegFrame frame = FFmpegWrapper.GetFrameRgba();
  177. if ((frame.Width | frame.Height) == 0)
  178. {
  179. return;
  180. }
  181. GalImage image = new GalImage(
  182. outputConfig.SurfaceWidth,
  183. outputConfig.SurfaceHeight, 1, 1, 1,
  184. outputConfig.GobBlockHeight, 1,
  185. GalMemoryLayout.BlockLinear,
  186. GalImageFormat.Rgba8 | GalImageFormat.Unorm,
  187. GalTextureTarget.TwoD);
  188. ImageUtils.WriteTexture(vmm, image, vmm.GetPhysicalAddress(outputConfig.SurfaceLumaAddress), frame.Data);
  189. }
  190. private void CopyPlanesYuv420P(NvGpuVmm vmm, SurfaceOutputConfig outputConfig)
  191. {
  192. FFmpegFrame frame = FFmpegWrapper.GetFrame();
  193. if ((frame.Width | frame.Height) == 0)
  194. {
  195. return;
  196. }
  197. int halfSrcWidth = frame.Width / 2;
  198. int halfWidth = frame.Width / 2;
  199. int halfHeight = frame.Height / 2;
  200. int alignedWidth = (outputConfig.SurfaceWidth + 0xff) & ~0xff;
  201. for (int y = 0; y < frame.Height; y++)
  202. {
  203. int src = y * frame.Width;
  204. int dst = y * alignedWidth;
  205. int size = frame.Width;
  206. for (int offset = 0; offset < size; offset++)
  207. {
  208. vmm.WriteByte(outputConfig.SurfaceLumaAddress + dst + offset, *(frame.LumaPtr + src + offset));
  209. }
  210. }
  211. // Copy chroma data from both channels with interleaving.
  212. for (int y = 0; y < halfHeight; y++)
  213. {
  214. int src = y * halfSrcWidth;
  215. int dst = y * alignedWidth;
  216. for (int x = 0; x < halfWidth; x++)
  217. {
  218. vmm.WriteByte(outputConfig.SurfaceChromaUAddress + dst + x * 2 + 0, *(frame.ChromaBPtr + src + x));
  219. vmm.WriteByte(outputConfig.SurfaceChromaUAddress + dst + x * 2 + 1, *(frame.ChromaRPtr + src + x));
  220. }
  221. }
  222. }
  223. private void ThrowUnimplementedCodec()
  224. {
  225. throw new NotImplementedException("Codec \"" + _currentVideoCodec + "\" is not supported!");
  226. }
  227. private void ThrowUnimplementedPixelFormat(SurfacePixelFormat pixelFormat)
  228. {
  229. throw new NotImplementedException("Pixel format \"" + pixelFormat + "\" is not supported!");
  230. }
  231. }
  232. }