vaapi-utils.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. static bool vaapi_display_ep_bframe_supported(VAProfile profile,
  148. VAEntrypoint entrypoint,
  149. VADisplay dpy)
  150. {
  151. bool ret = false;
  152. VAStatus va_status;
  153. VAConfigAttrib attrib[1];
  154. attrib->type = VAConfigAttribEncMaxRefFrames;
  155. va_status = vaGetConfigAttributes(dpy, profile, entrypoint, attrib, 1);
  156. if (va_status == VA_STATUS_SUCCESS &&
  157. attrib->value != VA_ATTRIB_NOT_SUPPORTED)
  158. return attrib->value >> 16;
  159. return false;
  160. }
  161. bool vaapi_device_bframe_supported(VAProfile profile, VADisplay dpy)
  162. {
  163. bool ret = vaapi_display_ep_bframe_supported(profile,
  164. VAEntrypointEncSlice, dpy);
  165. if (ret)
  166. return true;
  167. ret = vaapi_display_ep_bframe_supported(profile, VAEntrypointEncSliceLP,
  168. dpy);
  169. if (ret)
  170. return true;
  171. return false;
  172. }
  173. #define CHECK_PROFILE(ret, profile, va_dpy, device_path) \
  174. if (vaapi_display_ep_combo_supported(profile, VAEntrypointEncSlice, \
  175. va_dpy, device_path)) { \
  176. blog(LOG_DEBUG, "'%s' support encoding with %s", device_path, \
  177. vaProfileStr(profile)); \
  178. ret |= true; \
  179. }
  180. #define CHECK_PROFILE_LP(ret, profile, va_dpy, device_path) \
  181. if (vaapi_display_ep_combo_supported(profile, VAEntrypointEncSliceLP, \
  182. va_dpy, device_path)) { \
  183. blog(LOG_DEBUG, "'%s' support low power encoding with %s", \
  184. device_path, vaProfileStr(profile)); \
  185. ret |= true; \
  186. }
  187. bool vaapi_display_h264_supported(VADisplay dpy, const char *device_path)
  188. {
  189. bool ret = false;
  190. CHECK_PROFILE(ret, VAProfileH264ConstrainedBaseline, dpy, device_path);
  191. CHECK_PROFILE(ret, VAProfileH264Main, dpy, device_path);
  192. CHECK_PROFILE(ret, VAProfileH264High, dpy, device_path);
  193. if (!ret) {
  194. CHECK_PROFILE_LP(ret, VAProfileH264ConstrainedBaseline, dpy,
  195. device_path);
  196. CHECK_PROFILE_LP(ret, VAProfileH264Main, dpy, device_path);
  197. CHECK_PROFILE_LP(ret, VAProfileH264High, dpy, device_path);
  198. }
  199. return ret;
  200. }
  201. bool vaapi_device_h264_supported(const char *device_path)
  202. {
  203. bool ret = false;
  204. VADisplay va_dpy;
  205. int drm_fd = -1;
  206. va_dpy = vaapi_open_device(&drm_fd, device_path,
  207. "vaapi_device_h264_supported");
  208. if (!va_dpy)
  209. return false;
  210. ret = vaapi_display_h264_supported(va_dpy, device_path);
  211. vaapi_close_device(&drm_fd, va_dpy);
  212. return ret;
  213. }
  214. const char *vaapi_get_h264_default_device()
  215. {
  216. static const char *default_h264_device = NULL;
  217. if (!default_h264_device) {
  218. bool ret = false;
  219. char path[32] = "/dev/dri/renderD1";
  220. for (int i = 28;; i++) {
  221. sprintf(path, "/dev/dri/renderD1%d", i);
  222. if (access(path, F_OK) != 0)
  223. break;
  224. ret = vaapi_device_h264_supported(path);
  225. if (ret) {
  226. default_h264_device = strdup(path);
  227. break;
  228. }
  229. }
  230. }
  231. return default_h264_device;
  232. }
  233. #ifdef ENABLE_HEVC
  234. bool vaapi_display_hevc_supported(VADisplay dpy, const char *device_path)
  235. {
  236. bool ret = false;
  237. CHECK_PROFILE(ret, VAProfileHEVCMain, dpy, device_path);
  238. CHECK_PROFILE(ret, VAProfileHEVCMain10, dpy, device_path);
  239. if (!ret) {
  240. CHECK_PROFILE_LP(ret, VAProfileHEVCMain, dpy, device_path);
  241. CHECK_PROFILE_LP(ret, VAProfileHEVCMain10, dpy, device_path);
  242. }
  243. return ret;
  244. }
  245. bool vaapi_device_hevc_supported(const char *device_path)
  246. {
  247. bool ret = false;
  248. VADisplay va_dpy;
  249. int drm_fd = -1;
  250. va_dpy = vaapi_open_device(&drm_fd, device_path,
  251. "vaapi_device_hevc_supported");
  252. if (!va_dpy)
  253. return false;
  254. ret = vaapi_display_hevc_supported(va_dpy, device_path);
  255. vaapi_close_device(&drm_fd, va_dpy);
  256. return ret;
  257. }
  258. const char *vaapi_get_hevc_default_device()
  259. {
  260. static const char *default_hevc_device = NULL;
  261. if (!default_hevc_device) {
  262. bool ret = false;
  263. char path[32] = "/dev/dri/renderD1";
  264. for (int i = 28;; i++) {
  265. sprintf(path, "/dev/dri/renderD1%d", i);
  266. if (access(path, F_OK) != 0)
  267. break;
  268. ret = vaapi_device_hevc_supported(path);
  269. if (ret) {
  270. default_hevc_device = strdup(path);
  271. break;
  272. }
  273. }
  274. }
  275. return default_hevc_device;
  276. }
  277. #endif // #ifdef ENABLE_HEVC