vaapi-utils.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-FileCopyrightText: 2022 tytan652 <[email protected]>
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include "vaapi-utils.h"
  5. #include <util/bmem.h>
  6. #include <util/dstr.h>
  7. #include <va/va_drm.h>
  8. #include <va/va_str.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. static bool version_logged = false;
  13. inline static VADisplay vaapi_open_display_drm(int *fd, const char *device_path)
  14. {
  15. VADisplay va_dpy;
  16. if (!device_path)
  17. return NULL;
  18. *fd = open(device_path, O_RDWR);
  19. if (*fd < 0) {
  20. blog(LOG_ERROR, "VAAPI: Failed to open device '%s'",
  21. device_path);
  22. return NULL;
  23. }
  24. va_dpy = vaGetDisplayDRM(*fd);
  25. if (!va_dpy) {
  26. blog(LOG_ERROR, "VAAPI: Failed to initialize DRM display");
  27. return NULL;
  28. }
  29. return va_dpy;
  30. }
  31. inline static void vaapi_close_display_drm(int *fd)
  32. {
  33. if (*fd < 0)
  34. return;
  35. close(*fd);
  36. *fd = -1;
  37. }
  38. static void vaapi_log_info_cb(void *user_context, const char *message)
  39. {
  40. UNUSED_PARAMETER(user_context);
  41. // Libva message always ends with a newline
  42. struct dstr m;
  43. dstr_init_copy(&m, message);
  44. dstr_depad(&m);
  45. blog(LOG_DEBUG, "Libva: %s", m.array);
  46. dstr_free(&m);
  47. }
  48. static void vaapi_log_error_cb(void *user_context, const char *message)
  49. {
  50. UNUSED_PARAMETER(user_context);
  51. // Libva message always ends with a newline
  52. struct dstr m;
  53. dstr_init_copy(&m, message);
  54. dstr_depad(&m);
  55. blog(LOG_DEBUG, "Libva error: %s", m.array);
  56. dstr_free(&m);
  57. }
  58. VADisplay vaapi_open_device(int *fd, const char *device_path,
  59. const char *func_name)
  60. {
  61. VADisplay va_dpy;
  62. VAStatus va_status;
  63. int major, minor;
  64. const char *driver;
  65. va_dpy = vaapi_open_display_drm(fd, device_path);
  66. if (!va_dpy)
  67. return NULL;
  68. blog(LOG_DEBUG, "VAAPI: Initializing display in %s", func_name);
  69. vaSetInfoCallback(va_dpy, vaapi_log_info_cb, NULL);
  70. vaSetErrorCallback(va_dpy, vaapi_log_error_cb, NULL);
  71. va_status = vaInitialize(va_dpy, &major, &minor);
  72. if (va_status != VA_STATUS_SUCCESS) {
  73. blog(LOG_ERROR, "VAAPI: Failed to initialize display in %s",
  74. func_name);
  75. return NULL;
  76. }
  77. blog(LOG_DEBUG, "VAAPI: Display initialized");
  78. if (!version_logged) {
  79. blog(LOG_INFO, "VAAPI: API version %d.%d", major, minor);
  80. version_logged = true;
  81. }
  82. driver = vaQueryVendorString(va_dpy);
  83. blog(LOG_DEBUG, "VAAPI: '%s' in use for device '%s'", driver,
  84. device_path);
  85. return va_dpy;
  86. }
  87. void vaapi_close_device(int *fd, VADisplay dpy)
  88. {
  89. vaTerminate(dpy);
  90. vaapi_close_display_drm(fd);
  91. }
  92. static uint32_t vaapi_display_ep_combo_rate_controls(VAProfile profile,
  93. VAEntrypoint entrypoint,
  94. VADisplay dpy,
  95. const char *device_path)
  96. {
  97. VAStatus va_status;
  98. VAConfigAttrib attrib[1];
  99. attrib->type = VAConfigAttribRateControl;
  100. va_status = vaGetConfigAttributes(dpy, profile, entrypoint, attrib, 1);
  101. switch (va_status) {
  102. case VA_STATUS_SUCCESS:
  103. return attrib->value;
  104. case VA_STATUS_ERROR_UNSUPPORTED_PROFILE:
  105. blog(LOG_DEBUG, "VAAPI: %s is not supported by the device '%s'",
  106. vaProfileStr(profile), device_path);
  107. return 0;
  108. case VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT:
  109. blog(LOG_DEBUG,
  110. "VAAPI: %s %s is not supported by the device '%s'",
  111. vaProfileStr(profile), vaEntrypointStr(entrypoint),
  112. device_path);
  113. return 0;
  114. default:
  115. blog(LOG_ERROR,
  116. "VAAPI: Fail to get RC attribute from the %s %s of the device '%s'",
  117. vaProfileStr(profile), vaEntrypointStr(entrypoint),
  118. device_path);
  119. return 0;
  120. }
  121. }
  122. static bool vaapi_display_ep_combo_supported(VAProfile profile,
  123. VAEntrypoint entrypoint,
  124. VADisplay dpy,
  125. const char *device_path)
  126. {
  127. uint32_t ret = vaapi_display_ep_combo_rate_controls(profile, entrypoint,
  128. dpy, device_path);
  129. if (ret & VA_RC_CBR || ret & VA_RC_CQP || ret & VA_RC_VBR)
  130. return true;
  131. return false;
  132. }
  133. bool vaapi_device_rc_supported(VAProfile profile, VADisplay dpy, uint32_t rc,
  134. const char *device_path)
  135. {
  136. uint32_t ret = vaapi_display_ep_combo_rate_controls(
  137. profile, VAEntrypointEncSlice, dpy, device_path);
  138. if (ret & rc)
  139. return true;
  140. ret = vaapi_display_ep_combo_rate_controls(
  141. profile, VAEntrypointEncSliceLP, dpy, device_path);
  142. if (ret & rc)
  143. return true;
  144. return false;
  145. }
  146. static bool vaapi_display_ep_bframe_supported(VAProfile profile,
  147. VAEntrypoint entrypoint,
  148. VADisplay dpy)
  149. {
  150. VAStatus va_status;
  151. VAConfigAttrib attrib[1];
  152. attrib->type = VAConfigAttribEncMaxRefFrames;
  153. va_status = vaGetConfigAttributes(dpy, profile, entrypoint, attrib, 1);
  154. if (va_status == VA_STATUS_SUCCESS &&
  155. attrib->value != VA_ATTRIB_NOT_SUPPORTED)
  156. return attrib->value >> 16;
  157. return false;
  158. }
  159. bool vaapi_device_bframe_supported(VAProfile profile, VADisplay dpy)
  160. {
  161. bool ret = vaapi_display_ep_bframe_supported(profile,
  162. VAEntrypointEncSlice, dpy);
  163. if (ret)
  164. return true;
  165. ret = vaapi_display_ep_bframe_supported(profile, VAEntrypointEncSliceLP,
  166. dpy);
  167. if (ret)
  168. return true;
  169. return false;
  170. }
  171. #define CHECK_PROFILE(ret, profile, va_dpy, device_path) \
  172. if (vaapi_display_ep_combo_supported(profile, VAEntrypointEncSlice, \
  173. va_dpy, device_path)) { \
  174. blog(LOG_DEBUG, "'%s' support encoding with %s", device_path, \
  175. vaProfileStr(profile)); \
  176. ret |= true; \
  177. }
  178. #define CHECK_PROFILE_LP(ret, profile, va_dpy, device_path) \
  179. if (vaapi_display_ep_combo_supported(profile, VAEntrypointEncSliceLP, \
  180. va_dpy, device_path)) { \
  181. blog(LOG_DEBUG, "'%s' support low power encoding with %s", \
  182. device_path, vaProfileStr(profile)); \
  183. ret |= true; \
  184. }
  185. bool vaapi_display_h264_supported(VADisplay dpy, const char *device_path)
  186. {
  187. bool ret = false;
  188. CHECK_PROFILE(ret, VAProfileH264ConstrainedBaseline, dpy, device_path);
  189. CHECK_PROFILE(ret, VAProfileH264Main, dpy, device_path);
  190. CHECK_PROFILE(ret, VAProfileH264High, dpy, device_path);
  191. if (!ret) {
  192. CHECK_PROFILE_LP(ret, VAProfileH264ConstrainedBaseline, dpy,
  193. device_path);
  194. CHECK_PROFILE_LP(ret, VAProfileH264Main, dpy, device_path);
  195. CHECK_PROFILE_LP(ret, VAProfileH264High, dpy, device_path);
  196. }
  197. return ret;
  198. }
  199. bool vaapi_device_h264_supported(const char *device_path)
  200. {
  201. bool ret = false;
  202. VADisplay va_dpy;
  203. int drm_fd = -1;
  204. va_dpy = vaapi_open_device(&drm_fd, device_path,
  205. "vaapi_device_h264_supported");
  206. if (!va_dpy)
  207. return false;
  208. ret = vaapi_display_h264_supported(va_dpy, device_path);
  209. vaapi_close_device(&drm_fd, va_dpy);
  210. return ret;
  211. }
  212. const char *vaapi_get_h264_default_device()
  213. {
  214. static const char *default_h264_device = NULL;
  215. if (!default_h264_device) {
  216. bool ret = false;
  217. char path[32] = "/dev/dri/renderD1";
  218. for (int i = 28;; i++) {
  219. sprintf(path, "/dev/dri/renderD1%d", i);
  220. if (access(path, F_OK) != 0)
  221. break;
  222. ret = vaapi_device_h264_supported(path);
  223. if (ret) {
  224. default_h264_device = strdup(path);
  225. break;
  226. }
  227. }
  228. }
  229. return default_h264_device;
  230. }
  231. bool vaapi_display_av1_supported(VADisplay dpy, const char *device_path)
  232. {
  233. bool ret = false;
  234. CHECK_PROFILE(ret, VAProfileAV1Profile0, dpy, device_path);
  235. if (!ret) {
  236. CHECK_PROFILE_LP(ret, VAProfileAV1Profile0, dpy, device_path);
  237. }
  238. return ret;
  239. }
  240. bool vaapi_device_av1_supported(const char *device_path)
  241. {
  242. bool ret = false;
  243. VADisplay va_dpy;
  244. int drm_fd = -1;
  245. va_dpy = vaapi_open_device(&drm_fd, device_path,
  246. "vaapi_device_av1_supported");
  247. if (!va_dpy)
  248. return false;
  249. ret = vaapi_display_av1_supported(va_dpy, device_path);
  250. vaapi_close_device(&drm_fd, va_dpy);
  251. return ret;
  252. }
  253. const char *vaapi_get_av1_default_device()
  254. {
  255. static const char *default_av1_device = NULL;
  256. if (!default_av1_device) {
  257. bool ret = false;
  258. char path[32] = "/dev/dri/renderD1";
  259. for (int i = 28;; i++) {
  260. sprintf(path, "/dev/dri/renderD1%d", i);
  261. if (access(path, F_OK) != 0)
  262. break;
  263. ret = vaapi_device_av1_supported(path);
  264. if (ret) {
  265. default_av1_device = strdup(path);
  266. break;
  267. }
  268. }
  269. }
  270. return default_av1_device;
  271. }
  272. #ifdef ENABLE_HEVC
  273. bool vaapi_display_hevc_supported(VADisplay dpy, const char *device_path)
  274. {
  275. bool ret = false;
  276. CHECK_PROFILE(ret, VAProfileHEVCMain, dpy, device_path);
  277. CHECK_PROFILE(ret, VAProfileHEVCMain10, dpy, device_path);
  278. if (!ret) {
  279. CHECK_PROFILE_LP(ret, VAProfileHEVCMain, dpy, device_path);
  280. CHECK_PROFILE_LP(ret, VAProfileHEVCMain10, dpy, device_path);
  281. }
  282. return ret;
  283. }
  284. bool vaapi_device_hevc_supported(const char *device_path)
  285. {
  286. bool ret = false;
  287. VADisplay va_dpy;
  288. int drm_fd = -1;
  289. va_dpy = vaapi_open_device(&drm_fd, device_path,
  290. "vaapi_device_hevc_supported");
  291. if (!va_dpy)
  292. return false;
  293. ret = vaapi_display_hevc_supported(va_dpy, device_path);
  294. vaapi_close_device(&drm_fd, va_dpy);
  295. return ret;
  296. }
  297. const char *vaapi_get_hevc_default_device()
  298. {
  299. static const char *default_hevc_device = NULL;
  300. if (!default_hevc_device) {
  301. bool ret = false;
  302. char path[32] = "/dev/dri/renderD1";
  303. for (int i = 28;; i++) {
  304. sprintf(path, "/dev/dri/renderD1%d", i);
  305. if (access(path, F_OK) != 0)
  306. break;
  307. ret = vaapi_device_hevc_supported(path);
  308. if (ret) {
  309. default_hevc_device = strdup(path);
  310. break;
  311. }
  312. }
  313. }
  314. return default_hevc_device;
  315. }
  316. #endif // #ifdef ENABLE_HEVC