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