common_utils_linux.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "common_utils.h"
  2. #include <time.h>
  3. #include <cpuid.h>
  4. #include <util/c99defs.h>
  5. #include <util/dstr.h>
  6. #include <va/va_drm.h>
  7. #include <va/va_str.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <string.h>
  13. #include <dirent.h>
  14. #include <obs.h>
  15. #include <obs-nix-platform.h>
  16. // Set during check_adapters to work-around VPL dispatcher not setting a VADisplay
  17. // for the MSDK runtime.
  18. static const char *default_h264_device = nullptr;
  19. static const char *default_hevc_device = nullptr;
  20. static const char *default_av1_device = nullptr;
  21. static int default_fd = -1;
  22. mfxStatus simple_alloc(mfxHDL pthis, mfxFrameAllocRequest *request,
  23. mfxFrameAllocResponse *response)
  24. {
  25. UNUSED_PARAMETER(pthis);
  26. UNUSED_PARAMETER(request);
  27. UNUSED_PARAMETER(response);
  28. return MFX_ERR_UNSUPPORTED;
  29. }
  30. mfxStatus simple_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  31. {
  32. UNUSED_PARAMETER(pthis);
  33. UNUSED_PARAMETER(mid);
  34. UNUSED_PARAMETER(ptr);
  35. return MFX_ERR_UNSUPPORTED;
  36. }
  37. mfxStatus simple_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  38. {
  39. UNUSED_PARAMETER(pthis);
  40. UNUSED_PARAMETER(mid);
  41. UNUSED_PARAMETER(ptr);
  42. return MFX_ERR_UNSUPPORTED;
  43. }
  44. mfxStatus simple_gethdl(mfxHDL pthis, mfxMemId mid, mfxHDL *handle)
  45. {
  46. UNUSED_PARAMETER(pthis);
  47. UNUSED_PARAMETER(mid);
  48. UNUSED_PARAMETER(handle);
  49. return MFX_ERR_UNSUPPORTED;
  50. }
  51. mfxStatus simple_free(mfxHDL pthis, mfxFrameAllocResponse *response)
  52. {
  53. UNUSED_PARAMETER(pthis);
  54. UNUSED_PARAMETER(response);
  55. return MFX_ERR_UNSUPPORTED;
  56. }
  57. mfxStatus simple_copytex(mfxHDL pthis, mfxMemId mid, mfxU32 tex_handle,
  58. mfxU64 lock_key, mfxU64 *next_key)
  59. {
  60. UNUSED_PARAMETER(pthis);
  61. UNUSED_PARAMETER(mid);
  62. UNUSED_PARAMETER(tex_handle);
  63. UNUSED_PARAMETER(lock_key);
  64. UNUSED_PARAMETER(next_key);
  65. return MFX_ERR_UNSUPPORTED;
  66. }
  67. #if 0
  68. void ClearYUVSurfaceVMem(mfxMemId memId);
  69. void ClearRGBSurfaceVMem(mfxMemId memId);
  70. #endif
  71. // Initialize Intel VPL Session, device/display and memory manager
  72. mfxStatus Initialize(mfxVersion ver, mfxSession *pSession,
  73. mfxFrameAllocator *pmfxAllocator, mfxHDL *deviceHandle,
  74. bool bCreateSharedHandles, bool dx9hack,
  75. enum qsv_codec codec)
  76. {
  77. UNUSED_PARAMETER(ver);
  78. UNUSED_PARAMETER(pmfxAllocator);
  79. UNUSED_PARAMETER(deviceHandle);
  80. UNUSED_PARAMETER(bCreateSharedHandles);
  81. UNUSED_PARAMETER(dx9hack);
  82. mfxStatus sts = MFX_ERR_NONE;
  83. mfxVariant impl;
  84. // Initialize Intel VPL Session
  85. mfxLoader loader = MFXLoad();
  86. mfxConfig cfg = MFXCreateConfig(loader);
  87. impl.Type = MFX_VARIANT_TYPE_U32;
  88. impl.Data.U32 = MFX_IMPL_TYPE_HARDWARE;
  89. MFXSetConfigFilterProperty(
  90. cfg, (const mfxU8 *)"mfxImplDescription.Impl", impl);
  91. impl.Type = MFX_VARIANT_TYPE_U32;
  92. impl.Data.U32 = INTEL_VENDOR_ID;
  93. MFXSetConfigFilterProperty(
  94. cfg, (const mfxU8 *)"mfxImplDescription.VendorID", impl);
  95. impl.Type = MFX_VARIANT_TYPE_U32;
  96. impl.Data.U32 = MFX_ACCEL_MODE_VIA_VAAPI_DRM_RENDER_NODE;
  97. MFXSetConfigFilterProperty(
  98. cfg, (const mfxU8 *)"mfxImplDescription.AccelerationMode",
  99. impl);
  100. if (codec == QSV_CODEC_AVC && default_h264_device)
  101. default_fd = open(default_h264_device, O_RDWR);
  102. if (codec == QSV_CODEC_HEVC && default_hevc_device)
  103. default_fd = open(default_hevc_device, O_RDWR);
  104. if (codec == QSV_CODEC_AV1 && default_av1_device)
  105. default_fd = open(default_av1_device, O_RDWR);
  106. if (default_fd < 0) {
  107. blog(LOG_ERROR, "Failed to open device '%s'",
  108. default_h264_device);
  109. return MFX_ERR_DEVICE_FAILED;
  110. }
  111. mfxHDL vaDisplay = vaGetDisplayDRM(default_fd);
  112. if (!vaDisplay) {
  113. return MFX_ERR_DEVICE_FAILED;
  114. }
  115. sts = MFXCreateSession(loader, 0, pSession);
  116. if (MFX_ERR_NONE > sts) {
  117. blog(LOG_ERROR, "Failed to initialize MFX");
  118. MSDK_PRINT_RET_MSG(sts);
  119. close(default_fd);
  120. default_fd = -1;
  121. return sts;
  122. }
  123. // VPL expects the VADisplay to be initialized.
  124. int major;
  125. int minor;
  126. if (vaInitialize(vaDisplay, &major, &minor) != VA_STATUS_SUCCESS) {
  127. blog(LOG_ERROR, "Failed to initialize VA-API");
  128. vaTerminate(vaDisplay);
  129. close(default_fd);
  130. default_fd = -1;
  131. return MFX_ERR_DEVICE_FAILED;
  132. }
  133. sts = MFXVideoCORE_SetHandle(*pSession, MFX_HANDLE_VA_DISPLAY,
  134. vaDisplay);
  135. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  136. return sts;
  137. }
  138. // Release resources (device/display)
  139. void Release()
  140. {
  141. if (default_fd > 0)
  142. close(default_fd);
  143. default_fd = -1;
  144. }
  145. void mfxGetTime(mfxTime *timestamp)
  146. {
  147. clock_gettime(CLOCK_MONOTONIC, timestamp);
  148. }
  149. double TimeDiffMsec(mfxTime tfinish, mfxTime tstart)
  150. {
  151. UNUSED_PARAMETER(tfinish);
  152. UNUSED_PARAMETER(tstart);
  153. //TODO, unused so far it seems
  154. return 0.0;
  155. }
  156. extern "C" void util_cpuid(int cpuinfo[4], int level)
  157. {
  158. __get_cpuid(level, (unsigned int *)&cpuinfo[0],
  159. (unsigned int *)&cpuinfo[1], (unsigned int *)&cpuinfo[2],
  160. (unsigned int *)&cpuinfo[3]);
  161. }
  162. struct vaapi_device {
  163. int fd;
  164. VADisplay display;
  165. const char *driver;
  166. };
  167. static void vaapi_open(char *device_path, struct vaapi_device *device)
  168. {
  169. int fd = open(device_path, O_RDWR);
  170. if (fd < 0) {
  171. return;
  172. }
  173. VADisplay display = vaGetDisplayDRM(fd);
  174. if (!display) {
  175. close(fd);
  176. return;
  177. }
  178. // VA-API is noisy by default.
  179. vaSetInfoCallback(display, nullptr, nullptr);
  180. vaSetErrorCallback(display, nullptr, nullptr);
  181. int major;
  182. int minor;
  183. if (vaInitialize(display, &major, &minor) != VA_STATUS_SUCCESS) {
  184. vaTerminate(display);
  185. close(fd);
  186. return;
  187. }
  188. const char *driver = vaQueryVendorString(display);
  189. if (strstr(driver, "Intel i965 driver") != nullptr) {
  190. blog(LOG_WARNING,
  191. "Legacy intel-vaapi-driver detected, incompatible with QSV");
  192. vaTerminate(display);
  193. close(fd);
  194. return;
  195. }
  196. device->fd = fd;
  197. device->display = display;
  198. device->driver = driver;
  199. }
  200. static void vaapi_close(struct vaapi_device *device)
  201. {
  202. vaTerminate(device->display);
  203. close(device->fd);
  204. }
  205. static uint32_t vaapi_check_support(VADisplay display, VAProfile profile,
  206. VAEntrypoint entrypoint)
  207. {
  208. bool ret = false;
  209. VAConfigAttrib attrib[1];
  210. attrib->type = VAConfigAttribRateControl;
  211. VAStatus va_status =
  212. vaGetConfigAttributes(display, profile, entrypoint, attrib, 1);
  213. uint32_t rc = 0;
  214. switch (va_status) {
  215. case VA_STATUS_SUCCESS:
  216. rc = attrib->value;
  217. break;
  218. case VA_STATUS_ERROR_UNSUPPORTED_PROFILE:
  219. case VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT:
  220. default:
  221. break;
  222. }
  223. return (rc & VA_RC_CBR || rc & VA_RC_CQP || rc & VA_RC_VBR);
  224. }
  225. static bool vaapi_supports_h264(VADisplay display)
  226. {
  227. bool ret = false;
  228. ret |= vaapi_check_support(display, VAProfileH264ConstrainedBaseline,
  229. VAEntrypointEncSlice);
  230. ret |= vaapi_check_support(display, VAProfileH264Main,
  231. VAEntrypointEncSlice);
  232. ret |= vaapi_check_support(display, VAProfileH264High,
  233. VAEntrypointEncSlice);
  234. if (!ret) {
  235. ret |= vaapi_check_support(display,
  236. VAProfileH264ConstrainedBaseline,
  237. VAEntrypointEncSliceLP);
  238. ret |= vaapi_check_support(display, VAProfileH264Main,
  239. VAEntrypointEncSliceLP);
  240. ret |= vaapi_check_support(display, VAProfileH264High,
  241. VAEntrypointEncSliceLP);
  242. }
  243. return ret;
  244. }
  245. static bool vaapi_supports_av1(VADisplay display)
  246. {
  247. bool ret = false;
  248. // Are there any devices with non-LowPower entrypoints?
  249. ret |= vaapi_check_support(display, VAProfileAV1Profile0,
  250. VAEntrypointEncSlice);
  251. ret |= vaapi_check_support(display, VAProfileAV1Profile0,
  252. VAEntrypointEncSliceLP);
  253. return ret;
  254. }
  255. static bool vaapi_supports_hevc(VADisplay display)
  256. {
  257. bool ret = false;
  258. ret |= vaapi_check_support(display, VAProfileHEVCMain,
  259. VAEntrypointEncSlice);
  260. ret |= vaapi_check_support(display, VAProfileHEVCMain,
  261. VAEntrypointEncSliceLP);
  262. return ret;
  263. }
  264. void check_adapters(struct adapter_info *adapters, size_t *adapter_count)
  265. {
  266. struct dstr full_path;
  267. struct dirent **namelist;
  268. int no;
  269. int adapter_idx;
  270. const char *base_dir = "/dev/dri/";
  271. dstr_init(&full_path);
  272. if ((no = scandir(base_dir, &namelist, 0, alphasort)) > 0) {
  273. for (int i = 0; i < no; i++) {
  274. struct adapter_info *adapter;
  275. struct dirent *dp;
  276. struct vaapi_device device = {0};
  277. dp = namelist[i];
  278. if (strstr(dp->d_name, "renderD") == nullptr)
  279. goto next_entry;
  280. adapter_idx = atoi(&dp->d_name[7]) - 128;
  281. if (adapter_idx >= (ssize_t)*adapter_count ||
  282. adapter_idx < 0)
  283. goto next_entry;
  284. *adapter_count = adapter_idx + 1;
  285. dstr_copy(&full_path, base_dir);
  286. dstr_cat(&full_path, dp->d_name);
  287. vaapi_open(full_path.array, &device);
  288. if (!device.display)
  289. goto next_entry;
  290. adapter = &adapters[adapter_idx];
  291. adapter->is_intel = strstr(device.driver, "Intel") !=
  292. nullptr;
  293. // This is currently only used for LowPower coding which is busted on VA-API anyway.
  294. adapter->is_dgpu = false;
  295. adapter->supports_av1 =
  296. vaapi_supports_av1(device.display);
  297. adapter->supports_hevc =
  298. vaapi_supports_hevc(device.display);
  299. if (adapter->is_intel && default_h264_device == nullptr)
  300. default_h264_device = strdup(full_path.array);
  301. if (adapter->is_intel && adapter->supports_av1 &&
  302. default_av1_device == nullptr)
  303. default_av1_device = strdup(full_path.array);
  304. if (adapter->is_intel && adapter->supports_hevc &&
  305. default_hevc_device == nullptr)
  306. default_hevc_device = strdup(full_path.array);
  307. vaapi_close(&device);
  308. next_entry:
  309. free(dp);
  310. }
  311. free(namelist);
  312. }
  313. dstr_free(&full_path);
  314. }