common_utils_windows.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include "common_utils.h"
  2. // ATTENTION: If D3D surfaces are used, DX9_D3D or DX11_D3D must be set in project settings or hardcoded here
  3. #ifdef DX9_D3D
  4. #include "common_directx.h"
  5. #elif DX11_D3D
  6. #include "common_directx11.h"
  7. #include "common_directx9.h"
  8. #endif
  9. #include <util/windows/device-enum.h>
  10. #include <util/config-file.h>
  11. #include <util/platform.h>
  12. #include <util/pipe.h>
  13. #include <util/dstr.h>
  14. #include <intrin.h>
  15. #include <inttypes.h>
  16. /* =======================================================
  17. * Windows implementation of OS-specific utility functions
  18. */
  19. mfxStatus Initialize(mfxVersion ver, mfxSession *pSession,
  20. mfxFrameAllocator *pmfxAllocator, mfxHDL *deviceHandle,
  21. bool bCreateSharedHandles, bool dx9hack,
  22. enum qsv_codec codec, void **data)
  23. {
  24. UNUSED_PARAMETER(codec);
  25. UNUSED_PARAMETER(data);
  26. mfxStatus sts = MFX_ERR_NONE;
  27. mfxVariant impl;
  28. // If mfxFrameAllocator is provided it means we need to setup DirectX device and memory allocator
  29. if (pmfxAllocator && !dx9hack) {
  30. // Initialize Intel VPL Session
  31. mfxLoader loader = MFXLoad();
  32. mfxConfig cfg = MFXCreateConfig(loader);
  33. impl.Type = MFX_VARIANT_TYPE_U32;
  34. impl.Data.U32 = MFX_IMPL_TYPE_HARDWARE;
  35. MFXSetConfigFilterProperty(
  36. cfg, (const mfxU8 *)"mfxImplDescription.Impl", impl);
  37. impl.Type = MFX_VARIANT_TYPE_U32;
  38. impl.Data.U32 = INTEL_VENDOR_ID;
  39. MFXSetConfigFilterProperty(
  40. cfg, (const mfxU8 *)"mfxImplDescription.VendorID",
  41. impl);
  42. impl.Type = MFX_VARIANT_TYPE_U32;
  43. impl.Data.U32 = MFX_ACCEL_MODE_VIA_D3D11;
  44. MFXSetConfigFilterProperty(
  45. cfg,
  46. (const mfxU8 *)"mfxImplDescription.AccelerationMode",
  47. impl);
  48. sts = MFXCreateSession(loader, 0, pSession);
  49. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  50. // Create DirectX device context
  51. if (deviceHandle == NULL || *deviceHandle == NULL) {
  52. sts = CreateHWDevice(*pSession, deviceHandle, NULL,
  53. bCreateSharedHandles);
  54. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  55. }
  56. if (deviceHandle == NULL || *deviceHandle == NULL)
  57. return MFX_ERR_DEVICE_FAILED;
  58. // Provide device manager to VPL
  59. sts = MFXVideoCORE_SetHandle(*pSession, DEVICE_MGR_TYPE,
  60. *deviceHandle);
  61. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  62. pmfxAllocator->pthis =
  63. *pSession; // We use VPL session ID as the allocation identifier
  64. pmfxAllocator->Alloc = simple_alloc;
  65. pmfxAllocator->Free = simple_free;
  66. pmfxAllocator->Lock = simple_lock;
  67. pmfxAllocator->Unlock = simple_unlock;
  68. pmfxAllocator->GetHDL = simple_gethdl;
  69. // Since we are using video memory we must provide VPL with an external allocator
  70. sts = MFXVideoCORE_SetFrameAllocator(*pSession, pmfxAllocator);
  71. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  72. } else if (pmfxAllocator && dx9hack) {
  73. // Initialize Intel VPL Session
  74. mfxLoader loader = MFXLoad();
  75. mfxConfig cfg = MFXCreateConfig(loader);
  76. impl.Type = MFX_VARIANT_TYPE_U32;
  77. impl.Data.U32 = MFX_IMPL_TYPE_HARDWARE;
  78. MFXSetConfigFilterProperty(
  79. cfg, (const mfxU8 *)"mfxImplDescription.Impl", impl);
  80. impl.Type = MFX_VARIANT_TYPE_U32;
  81. impl.Data.U32 = INTEL_VENDOR_ID;
  82. MFXSetConfigFilterProperty(
  83. cfg, (const mfxU8 *)"mfxImplDescription.VendorID",
  84. impl);
  85. impl.Type = MFX_VARIANT_TYPE_U32;
  86. impl.Data.U32 = MFX_ACCEL_MODE_VIA_D3D9;
  87. MFXSetConfigFilterProperty(
  88. cfg,
  89. (const mfxU8 *)"mfxImplDescription.AccelerationMode",
  90. impl);
  91. sts = MFXCreateSession(loader, 0, pSession);
  92. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  93. // Create DirectX device context
  94. if (deviceHandle == NULL || *deviceHandle == NULL) {
  95. sts = DX9_CreateHWDevice(*pSession, deviceHandle, NULL,
  96. false);
  97. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  98. }
  99. if (*deviceHandle == NULL)
  100. return MFX_ERR_DEVICE_FAILED;
  101. // Provide device manager to VPL
  102. sts = MFXVideoCORE_SetHandle(*pSession, DEVICE_MGR_TYPE,
  103. *deviceHandle);
  104. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  105. pmfxAllocator->pthis =
  106. *pSession; // We use VPL session ID as the allocation identifier
  107. pmfxAllocator->Alloc = dx9_simple_alloc;
  108. pmfxAllocator->Free = dx9_simple_free;
  109. pmfxAllocator->Lock = dx9_simple_lock;
  110. pmfxAllocator->Unlock = dx9_simple_unlock;
  111. pmfxAllocator->GetHDL = dx9_simple_gethdl;
  112. // Since we are using video memory we must provide VPL with an external allocator
  113. sts = MFXVideoCORE_SetFrameAllocator(*pSession, pmfxAllocator);
  114. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  115. } else {
  116. // Initialize Intel VPL Session
  117. mfxLoader loader = MFXLoad();
  118. mfxConfig cfg = MFXCreateConfig(loader);
  119. impl.Type = MFX_VARIANT_TYPE_U32;
  120. impl.Data.U32 = MFX_IMPL_TYPE_HARDWARE;
  121. MFXSetConfigFilterProperty(
  122. cfg, (const mfxU8 *)"mfxImplDescription.Impl", impl);
  123. impl.Type = MFX_VARIANT_TYPE_U32;
  124. impl.Data.U32 = INTEL_VENDOR_ID;
  125. MFXSetConfigFilterProperty(
  126. cfg, (const mfxU8 *)"mfxImplDescription.VendorID",
  127. impl);
  128. impl.Type = MFX_VARIANT_TYPE_U32;
  129. impl.Data.U32 = MFX_ACCEL_MODE_VIA_D3D9;
  130. MFXSetConfigFilterProperty(
  131. cfg,
  132. (const mfxU8 *)"mfxImplDescription.AccelerationMode",
  133. impl);
  134. sts = MFXCreateSession(loader, 0, pSession);
  135. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  136. }
  137. return sts;
  138. }
  139. void Release()
  140. {
  141. #if defined(DX9_D3D) || defined(DX11_D3D)
  142. CleanupHWDevice();
  143. DX9_CleanupHWDevice();
  144. #endif
  145. }
  146. void ReleaseSessionData(void *) {}
  147. void mfxGetTime(mfxTime *timestamp)
  148. {
  149. QueryPerformanceCounter(timestamp);
  150. }
  151. double TimeDiffMsec(mfxTime tfinish, mfxTime tstart)
  152. {
  153. static LARGE_INTEGER tFreq = {0};
  154. if (!tFreq.QuadPart)
  155. QueryPerformanceFrequency(&tFreq);
  156. double freq = (double)tFreq.QuadPart;
  157. return 1000.0 * ((double)tfinish.QuadPart - (double)tstart.QuadPart) /
  158. freq;
  159. }
  160. void util_cpuid(int cpuinfo[4], int flags)
  161. {
  162. return __cpuid(cpuinfo, flags);
  163. }
  164. static bool enum_luids(void *param, uint32_t idx, uint64_t luid)
  165. {
  166. struct dstr *cmd = (struct dstr *)param;
  167. dstr_catf(cmd, " %" PRIx64, luid);
  168. UNUSED_PARAMETER(idx);
  169. return true;
  170. }
  171. void check_adapters(struct adapter_info *adapters, size_t *adapter_count)
  172. {
  173. char *test_exe = os_get_executable_path_ptr("obs-qsv-test.exe");
  174. struct dstr cmd = {0};
  175. struct dstr caps_str = {0};
  176. os_process_pipe_t *pp = nullptr;
  177. config_t *config = nullptr;
  178. const char *error = nullptr;
  179. size_t config_adapter_count;
  180. dstr_init_move_array(&cmd, test_exe);
  181. dstr_insert_ch(&cmd, 0, '\"');
  182. dstr_cat(&cmd, "\"");
  183. enum_graphics_device_luids(enum_luids, &cmd);
  184. pp = os_process_pipe_create(cmd.array, "r");
  185. if (!pp) {
  186. blog(LOG_INFO, "Failed to launch the QSV test process I guess");
  187. goto fail;
  188. }
  189. for (;;) {
  190. char data[2048];
  191. size_t len =
  192. os_process_pipe_read(pp, (uint8_t *)data, sizeof(data));
  193. if (!len)
  194. break;
  195. dstr_ncat(&caps_str, data, len);
  196. }
  197. if (dstr_is_empty(&caps_str)) {
  198. blog(LOG_INFO, "Seems the QSV test subprocess crashed. "
  199. "Better there than here I guess. "
  200. "Let's just skip loading QSV then I suppose.");
  201. goto fail;
  202. }
  203. if (config_open_string(&config, caps_str.array) != 0) {
  204. blog(LOG_INFO, "Couldn't open QSV configuration string");
  205. goto fail;
  206. }
  207. error = config_get_string(config, "error", "string");
  208. if (error) {
  209. blog(LOG_INFO, "Error querying QSV support: %s", error);
  210. goto fail;
  211. }
  212. config_adapter_count = config_num_sections(config);
  213. if (config_adapter_count < *adapter_count)
  214. *adapter_count = config_adapter_count;
  215. for (size_t i = 0; i < *adapter_count; i++) {
  216. char section[16];
  217. snprintf(section, sizeof(section), "%d", (int)i);
  218. struct adapter_info *adapter = &adapters[i];
  219. adapter->is_intel =
  220. config_get_bool(config, section, "is_intel");
  221. adapter->is_dgpu = config_get_bool(config, section, "is_dgpu");
  222. adapter->supports_av1 =
  223. config_get_bool(config, section, "supports_av1");
  224. adapter->supports_hevc =
  225. config_get_bool(config, section, "supports_hevc");
  226. }
  227. fail:
  228. config_close(config);
  229. dstr_free(&caps_str);
  230. dstr_free(&cmd);
  231. os_process_pipe_destroy(pp);
  232. }
  233. /* (Lain) Functions currently unused */
  234. #if 0
  235. void ClearYUVSurfaceVMem(mfxMemId memId)
  236. {
  237. #if defined(DX9_D3D) || defined(DX11_D3D)
  238. ClearYUVSurfaceD3D(memId);
  239. #endif
  240. }
  241. void ClearRGBSurfaceVMem(mfxMemId memId)
  242. {
  243. #if defined(DX9_D3D) || defined(DX11_D3D)
  244. ClearRGBSurfaceD3D(memId);
  245. #endif
  246. }
  247. #endif