common_utils_windows.cpp 6.6 KB

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