File: vaapi-utils.c

package info (click to toggle)
obs-studio 30.2.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 47,852 kB
  • sloc: ansic: 202,137; cpp: 112,402; makefile: 868; python: 599; sh: 275; javascript: 19
file content (409 lines) | stat: -rw-r--r-- 9,402 bytes parent folder | download | duplicates (2)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// 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);
		vaapi_close_device(fd, va_dpy);
		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)
{
	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;
}

static bool vaapi_display_ep_bframe_supported(VAProfile profile,
					      VAEntrypoint entrypoint,
					      VADisplay dpy)
{
	VAStatus va_status;
	VAConfigAttrib attrib[1];
	attrib->type = VAConfigAttribEncMaxRefFrames;

	va_status = vaGetConfigAttributes(dpy, profile, entrypoint, attrib, 1);

	if (va_status == VA_STATUS_SUCCESS &&
	    attrib->value != VA_ATTRIB_NOT_SUPPORTED)
		return attrib->value >> 16;

	return false;
}

bool vaapi_device_bframe_supported(VAProfile profile, VADisplay dpy)
{
	bool ret = vaapi_display_ep_bframe_supported(profile,
						     VAEntrypointEncSlice, dpy);
	if (ret)
		return true;
	ret = vaapi_display_ep_bframe_supported(profile, VAEntrypointEncSliceLP,
						dpy);
	if (ret)
		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;
}

bool vaapi_display_av1_supported(VADisplay dpy, const char *device_path)
{
	bool ret = false;

	CHECK_PROFILE(ret, VAProfileAV1Profile0, dpy, device_path);

	if (!ret) {
		CHECK_PROFILE_LP(ret, VAProfileAV1Profile0, dpy, device_path);
	}

	return ret;
}

bool vaapi_device_av1_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_av1_supported");
	if (!va_dpy)
		return false;

	ret = vaapi_display_av1_supported(va_dpy, device_path);

	vaapi_close_device(&drm_fd, va_dpy);

	return ret;
}

const char *vaapi_get_av1_default_device()
{
	static const char *default_av1_device = NULL;

	if (!default_av1_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_av1_supported(path);
			if (ret) {
				default_av1_device = strdup(path);
				break;
			}
		}
	}

	return default_av1_device;
}

#ifdef ENABLE_HEVC

bool vaapi_display_hevc_supported(VADisplay dpy, const char *device_path)
{
	bool ret = false;

	CHECK_PROFILE(ret, VAProfileHEVCMain, dpy, device_path);
	CHECK_PROFILE(ret, VAProfileHEVCMain10, dpy, device_path);

	if (!ret) {
		CHECK_PROFILE_LP(ret, VAProfileHEVCMain, dpy, device_path);
		CHECK_PROFILE_LP(ret, VAProfileHEVCMain10, dpy, device_path);
	}

	return ret;
}

bool vaapi_device_hevc_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_hevc_supported");
	if (!va_dpy)
		return false;

	ret = vaapi_display_hevc_supported(va_dpy, device_path);

	vaapi_close_device(&drm_fd, va_dpy);

	return ret;
}

const char *vaapi_get_hevc_default_device()
{
	static const char *default_hevc_device = NULL;

	if (!default_hevc_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_hevc_supported(path);
			if (ret) {
				default_hevc_device = strdup(path);
				break;
			}
		}
	}

	return default_hevc_device;
}

#endif // #ifdef ENABLE_HEVC