common_utils_linux.cpp 9.3 KB

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