common_utils_windows.cpp 6.9 KB

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