vaapi-utils.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. bool ret = false;
  98. VAStatus va_status;
  99. VAConfigAttrib attrib[1];
  100. attrib->type = VAConfigAttribRateControl;
  101. va_status = vaGetConfigAttributes(dpy, profile, entrypoint, attrib, 1);
  102. switch (va_status) {
  103. case VA_STATUS_SUCCESS:
  104. return attrib->value;
  105. case VA_STATUS_ERROR_UNSUPPORTED_PROFILE:
  106. blog(LOG_DEBUG, "VAAPI: %s is not supported by the device '%s'",
  107. vaProfileStr(profile), device_path);
  108. return 0;
  109. case VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT:
  110. blog(LOG_DEBUG,
  111. "VAAPI: %s %s is not supported by the device '%s'",
  112. vaProfileStr(profile), vaEntrypointStr(entrypoint),
  113. device_path);
  114. return 0;
  115. default:
  116. blog(LOG_ERROR,
  117. "VAAPI: Fail to get RC attribute from the %s %s of the device '%s'",
  118. vaProfileStr(profile), vaEntrypointStr(entrypoint),
  119. device_path);
  120. return 0;
  121. }
  122. }
  123. static bool vaapi_display_ep_combo_supported(VAProfile profile,
  124. VAEntrypoint entrypoint,
  125. VADisplay dpy,
  126. const char *device_path)
  127. {
  128. uint32_t ret = vaapi_display_ep_combo_rate_controls(profile, entrypoint,
  129. dpy, device_path);
  130. if (ret & VA_RC_CBR || ret & VA_RC_CQP || ret & VA_RC_VBR)
  131. return true;
  132. return false;
  133. }
  134. bool vaapi_device_rc_supported(VAProfile profile, VADisplay dpy, uint32_t rc,
  135. const char *device_path)
  136. {
  137. uint32_t ret = vaapi_display_ep_combo_rate_controls(
  138. profile, VAEntrypointEncSlice, dpy, device_path);
  139. if (ret & rc)
  140. return true;
  141. ret = vaapi_display_ep_combo_rate_controls(
  142. profile, VAEntrypointEncSliceLP, dpy, device_path);
  143. if (ret & rc)
  144. return true;
  145. return false;
  146. }
  147. #define CHECK_PROFILE(ret, profile, va_dpy, device_path) \
  148. if (vaapi_display_ep_combo_supported(profile, VAEntrypointEncSlice, \
  149. va_dpy, device_path)) { \
  150. blog(LOG_DEBUG, "'%s' support encoding with %s", device_path, \
  151. vaProfileStr(profile)); \
  152. ret |= true; \
  153. }
  154. #define CHECK_PROFILE_LP(ret, profile, va_dpy, device_path) \
  155. if (vaapi_display_ep_combo_supported(profile, VAEntrypointEncSliceLP, \
  156. va_dpy, device_path)) { \
  157. blog(LOG_DEBUG, "'%s' support low power encoding with %s", \
  158. device_path, vaProfileStr(profile)); \
  159. ret |= true; \
  160. }
  161. bool vaapi_display_h264_supported(VADisplay dpy, const char *device_path)
  162. {
  163. bool ret = false;
  164. CHECK_PROFILE(ret, VAProfileH264ConstrainedBaseline, dpy, device_path);
  165. CHECK_PROFILE(ret, VAProfileH264Main, dpy, device_path);
  166. CHECK_PROFILE(ret, VAProfileH264High, dpy, device_path);
  167. if (!ret) {
  168. CHECK_PROFILE_LP(ret, VAProfileH264ConstrainedBaseline, dpy,
  169. device_path);
  170. CHECK_PROFILE_LP(ret, VAProfileH264Main, dpy, device_path);
  171. CHECK_PROFILE_LP(ret, VAProfileH264High, dpy, device_path);
  172. }
  173. return ret;
  174. }
  175. bool vaapi_device_h264_supported(const char *device_path)
  176. {
  177. bool ret = false;
  178. VADisplay va_dpy;
  179. int drm_fd = -1;
  180. va_dpy = vaapi_open_device(&drm_fd, device_path,
  181. "vaapi_device_h264_supported");
  182. if (!va_dpy)
  183. return false;
  184. ret = vaapi_display_h264_supported(va_dpy, device_path);
  185. vaapi_close_device(&drm_fd, va_dpy);
  186. return ret;
  187. }
  188. const char *vaapi_get_h264_default_device()
  189. {
  190. static const char *default_h264_device = NULL;
  191. if (!default_h264_device) {
  192. bool ret = false;
  193. char path[32] = "/dev/dri/renderD1";
  194. for (int i = 28;; i++) {
  195. sprintf(path, "/dev/dri/renderD1%d", i);
  196. if (access(path, F_OK) != 0)
  197. break;
  198. ret = vaapi_device_h264_supported(path);
  199. if (ret) {
  200. default_h264_device = strdup(path);
  201. break;
  202. }
  203. }
  204. }
  205. return default_h264_device;
  206. }
  207. #ifdef ENABLE_HEVC
  208. bool vaapi_display_hevc_supported(VADisplay dpy, const char *device_path)
  209. {
  210. bool ret = false;
  211. CHECK_PROFILE(ret, VAProfileHEVCMain, dpy, device_path);
  212. CHECK_PROFILE(ret, VAProfileHEVCMain10, dpy, device_path);
  213. if (!ret) {
  214. CHECK_PROFILE_LP(ret, VAProfileHEVCMain, dpy, device_path);
  215. CHECK_PROFILE_LP(ret, VAProfileHEVCMain10, dpy, device_path);
  216. }
  217. return ret;
  218. }
  219. bool vaapi_device_hevc_supported(const char *device_path)
  220. {
  221. bool ret = false;
  222. VADisplay va_dpy;
  223. int drm_fd = -1;
  224. va_dpy = vaapi_open_device(&drm_fd, device_path,
  225. "vaapi_device_hevc_supported");
  226. if (!va_dpy)
  227. return false;
  228. ret = vaapi_display_hevc_supported(va_dpy, device_path);
  229. vaapi_close_device(&drm_fd, va_dpy);
  230. return ret;
  231. }
  232. const char *vaapi_get_hevc_default_device()
  233. {
  234. static const char *default_hevc_device = NULL;
  235. if (!default_hevc_device) {
  236. bool ret = false;
  237. char path[32] = "/dev/dri/renderD1";
  238. for (int i = 28;; i++) {
  239. sprintf(path, "/dev/dri/renderD1%d", i);
  240. if (access(path, F_OK) != 0)
  241. break;
  242. ret = vaapi_device_hevc_supported(path);
  243. if (ret) {
  244. default_hevc_device = strdup(path);
  245. break;
  246. }
  247. }
  248. }
  249. return default_hevc_device;
  250. }
  251. #endif // #ifdef ENABLE_HEVC