common_utils_windows.cpp 6.8 KB

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