aboutsummaryrefslogtreecommitdiff
path: root/src/codec_query
diff options
context:
space:
mode:
Diffstat (limited to 'src/codec_query')
-rw-r--r--src/codec_query/nvenc.c (renamed from src/codec_query/cuda.c)16
-rw-r--r--src/codec_query/vaapi.c60
-rw-r--r--src/codec_query/vulkan.c156
3 files changed, 199 insertions, 33 deletions
diff --git a/src/codec_query/cuda.c b/src/codec_query/nvenc.c
index 690e7d4..0501851 100644
--- a/src/codec_query/cuda.c
+++ b/src/codec_query/nvenc.c
@@ -1,4 +1,4 @@
-#include "../../include/codec_query/cuda.h"
+#include "../../include/codec_query/nvenc.h"
#include "../../include/cuda.h"
#include "../../external/nvEncodeAPI.h"
@@ -101,16 +101,16 @@ static bool encoder_get_supported_profiles(const NV_ENCODE_API_FUNCTION_LIST *fu
for(uint32_t i = 0; i < profile_guid_count; ++i) {
if(profile_is_h264(&profile_guids[i])) {
- supported_video_codecs->h264 = true;
+ supported_video_codecs->h264 = (gsr_supported_video_codec){ true, false };
} else if(profile_is_hevc(&profile_guids[i])) {
- supported_video_codecs->hevc = true;
+ supported_video_codecs->hevc = (gsr_supported_video_codec){ true, false };
} else if(profile_is_hevc_10bit(&profile_guids[i])) {
- supported_video_codecs->hevc_hdr = true;
- supported_video_codecs->hevc_10bit = true;
+ supported_video_codecs->hevc_hdr = (gsr_supported_video_codec){ true, false };
+ supported_video_codecs->hevc_10bit = (gsr_supported_video_codec){ true, false };
} else if(profile_is_av1(&profile_guids[i])) {
- supported_video_codecs->av1 = true;
- supported_video_codecs->av1_hdr = true;
- supported_video_codecs->av1_10bit = true;
+ supported_video_codecs->av1 = (gsr_supported_video_codec){ true, false };
+ supported_video_codecs->av1_hdr = (gsr_supported_video_codec){ true, false };
+ supported_video_codecs->av1_10bit = (gsr_supported_video_codec){ true, false };
}
}
diff --git a/src/codec_query/vaapi.c b/src/codec_query/vaapi.c
index d9f3497..2c74d96 100644
--- a/src/codec_query/vaapi.c
+++ b/src/codec_query/vaapi.c
@@ -77,7 +77,8 @@ static bool profile_is_vp9(VAProfile profile) {
}
}
-static bool profile_supports_video_encoding(VADisplay va_dpy, VAProfile profile) {
+static bool profile_supports_video_encoding(VADisplay va_dpy, VAProfile profile, bool *low_power) {
+ *low_power = false;
int num_entrypoints = vaMaxNumEntrypoints(va_dpy);
if(num_entrypoints <= 0)
return false;
@@ -86,18 +87,22 @@ static bool profile_supports_video_encoding(VADisplay va_dpy, VAProfile profile)
if(!entrypoint_list)
return false;
- bool supported = false;
+ bool supports_encoding = false;
+ bool supports_low_power_encoding = false;
if(vaQueryConfigEntrypoints(va_dpy, profile, entrypoint_list, &num_entrypoints) == VA_STATUS_SUCCESS) {
for(int i = 0; i < num_entrypoints; ++i) {
- if(entrypoint_list[i] == VAEntrypointEncSlice) {
- supported = true;
- break;
- }
+ if(entrypoint_list[i] == VAEntrypointEncSlice)
+ supports_encoding = true;
+ else if(entrypoint_list[i] == VAEntrypointEncSliceLP)
+ supports_low_power_encoding = true;
}
}
+ if(!supports_encoding && supports_low_power_encoding)
+ *low_power = true;
+
free(entrypoint_list);
- return supported;
+ return supports_encoding || supports_low_power_encoding;
}
static bool get_supported_video_codecs(VADisplay va_dpy, gsr_supported_video_codecs *video_codecs, bool cleanup) {
@@ -123,29 +128,31 @@ static bool get_supported_video_codecs(VADisplay va_dpy, gsr_supported_video_cod
goto fail;
for(int i = 0; i < num_profiles; ++i) {
+ bool low_power = false;
if(profile_is_h264(profile_list[i])) {
- if(profile_supports_video_encoding(va_dpy, profile_list[i]))
- video_codecs->h264 = true;
+ if(profile_supports_video_encoding(va_dpy, profile_list[i], &low_power)) {
+ video_codecs->h264 = (gsr_supported_video_codec){ true, low_power };
+ }
} else if(profile_is_hevc_8bit(profile_list[i])) {
- if(profile_supports_video_encoding(va_dpy, profile_list[i]))
- video_codecs->hevc = true;
+ if(profile_supports_video_encoding(va_dpy, profile_list[i], &low_power))
+ video_codecs->hevc = (gsr_supported_video_codec){ true, low_power };
} else if(profile_is_hevc_10bit(profile_list[i])) {
- if(profile_supports_video_encoding(va_dpy, profile_list[i])) {
- video_codecs->hevc_hdr = true;
- video_codecs->hevc_10bit = true;
+ if(profile_supports_video_encoding(va_dpy, profile_list[i], &low_power)) {
+ video_codecs->hevc_hdr = (gsr_supported_video_codec){ true, low_power };
+ video_codecs->hevc_10bit = (gsr_supported_video_codec){ true, low_power };
}
} else if(profile_is_av1(profile_list[i])) {
- if(profile_supports_video_encoding(va_dpy, profile_list[i])) {
- video_codecs->av1 = true;
- video_codecs->av1_hdr = true;
- video_codecs->av1_10bit = true;
+ if(profile_supports_video_encoding(va_dpy, profile_list[i], &low_power)) {
+ video_codecs->av1 = (gsr_supported_video_codec){ true, low_power };
+ video_codecs->av1_hdr = (gsr_supported_video_codec){ true, low_power };
+ video_codecs->av1_10bit = (gsr_supported_video_codec){ true, low_power };
}
} else if(profile_is_vp8(profile_list[i])) {
- if(profile_supports_video_encoding(va_dpy, profile_list[i]))
- video_codecs->vp8 = true;
+ if(profile_supports_video_encoding(va_dpy, profile_list[i], &low_power))
+ video_codecs->vp8 = (gsr_supported_video_codec){ true, low_power };
} else if(profile_is_vp9(profile_list[i])) {
- if(profile_supports_video_encoding(va_dpy, profile_list[i]))
- video_codecs->vp9 = true;
+ if(profile_supports_video_encoding(va_dpy, profile_list[i], &low_power))
+ video_codecs->vp9 = (gsr_supported_video_codec){ true, low_power };
}
}
@@ -163,6 +170,7 @@ static bool get_supported_video_codecs(VADisplay va_dpy, gsr_supported_video_cod
bool gsr_get_supported_video_codecs_vaapi(gsr_supported_video_codecs *video_codecs, const char *card_path, bool cleanup) {
memset(video_codecs, 0, sizeof(*video_codecs));
bool success = false;
+ int drm_fd = -1;
char render_path[128];
if(!gsr_card_path_get_render_path(card_path, render_path)) {
@@ -170,7 +178,7 @@ bool gsr_get_supported_video_codecs_vaapi(gsr_supported_video_codecs *video_code
goto done;
}
- const int drm_fd = open(render_path, O_RDWR);
+ drm_fd = open(render_path, O_RDWR);
if(drm_fd == -1) {
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vaapi: failed to open device %s\n", render_path);
goto done;
@@ -186,8 +194,10 @@ bool gsr_get_supported_video_codecs_vaapi(gsr_supported_video_codecs *video_code
}
done:
- if(cleanup)
- close(drm_fd);
+ if(cleanup) {
+ if(drm_fd > 0)
+ close(drm_fd);
+ }
return success;
}
diff --git a/src/codec_query/vulkan.c b/src/codec_query/vulkan.c
new file mode 100644
index 0000000..15dd98b
--- /dev/null
+++ b/src/codec_query/vulkan.c
@@ -0,0 +1,156 @@
+#include "../../include/codec_query/vulkan.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <xf86drm.h>
+#define VK_NO_PROTOTYPES
+//#include <vulkan/vulkan.h>
+
+#define MAX_PHYSICAL_DEVICES 32
+
+static const char *required_device_extensions[] = {
+ "VK_KHR_external_memory_fd",
+ "VK_KHR_external_semaphore_fd",
+ "VK_KHR_video_encode_queue",
+ "VK_KHR_video_queue",
+ "VK_KHR_video_maintenance1",
+ "VK_EXT_external_memory_dma_buf",
+ "VK_EXT_external_memory_host",
+ "VK_EXT_image_drm_format_modifier"
+};
+static int num_required_device_extensions = 8;
+
+bool gsr_get_supported_video_codecs_vulkan(gsr_supported_video_codecs *video_codecs, const char *card_path, bool cleanup) {
+ memset(video_codecs, 0, sizeof(*video_codecs));
+#if 0
+ bool success = false;
+ VkInstance instance = NULL;
+ VkPhysicalDevice physical_devices[MAX_PHYSICAL_DEVICES];
+ VkDevice device = NULL;
+ VkExtensionProperties *device_extensions = NULL;
+
+ const VkApplicationInfo app_info = {
+ .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
+ .pApplicationName = "GPU Screen Recorder",
+ .applicationVersion = VK_MAKE_VERSION(1, 0, 0),
+ .pEngineName = "GPU Screen Recorder",
+ .engineVersion = VK_MAKE_VERSION(1, 0, 0),
+ .apiVersion = VK_API_VERSION_1_3,
+ };
+
+ const VkInstanceCreateInfo instance_create_info = {
+ .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
+ .pApplicationInfo = &app_info
+ };
+
+ if(vkCreateInstance(&instance_create_info, NULL, &instance) != VK_SUCCESS) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkCreateInstance failed\n");
+ goto done;
+ }
+
+ uint32_t num_devices = 0;
+ if(vkEnumeratePhysicalDevices(instance, &num_devices, NULL) != VK_SUCCESS) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumeratePhysicalDevices (query num devices) failed\n");
+ goto done;
+ }
+
+ if(num_devices == 0) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: no vulkan capable device found\n");
+ goto done;
+ }
+
+ if(num_devices > MAX_PHYSICAL_DEVICES)
+ num_devices = MAX_PHYSICAL_DEVICES;
+
+ if(vkEnumeratePhysicalDevices(instance, &num_devices, physical_devices) != VK_SUCCESS) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumeratePhysicalDevices (get data) failed\n");
+ goto done;
+ }
+
+ VkPhysicalDevice physical_device = NULL;
+ char device_card_path[128];
+ for(uint32_t i = 0; i < num_devices; ++i) {
+ VkPhysicalDeviceDrmPropertiesEXT device_drm_properties = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT
+ };
+
+ VkPhysicalDeviceProperties2 device_properties = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
+ .pNext = &device_drm_properties
+ };
+ vkGetPhysicalDeviceProperties2(physical_devices[i], &device_properties);
+
+ if(!device_drm_properties.hasPrimary)
+ continue;
+
+ snprintf(device_card_path, sizeof(device_card_path), DRM_DEV_NAME, DRM_DIR_NAME, (int)device_drm_properties.primaryMinor);
+ if(strcmp(device_card_path, card_path) == 0) {
+ physical_device = physical_devices[i];
+ break;
+ }
+ }
+
+ if(!physical_device) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: failed to find a vulkan device that matches opengl device %s\n", card_path);
+ goto done;
+ }
+
+ const VkDeviceCreateInfo device_create_info = {
+ .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
+ .enabledExtensionCount = num_required_device_extensions,
+ .ppEnabledExtensionNames = required_device_extensions
+ };
+
+ if(vkCreateDevice(physical_device, &device_create_info, NULL, &device) != VK_SUCCESS) {
+ //fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkCreateDevice failed. Device %s likely doesn't support vulkan video encoding\n", card_path);
+ goto done;
+ }
+
+ uint32_t num_device_extensions = 0;
+ if(vkEnumerateDeviceExtensionProperties(physical_device, NULL, &num_device_extensions, NULL) != VK_SUCCESS) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumerateDeviceExtensionProperties (query num device extensions) failed\n");
+ goto done;
+ }
+
+ device_extensions = calloc(num_device_extensions, sizeof(VkExtensionProperties));
+ if(!device_extensions) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: failed to allocate %d device extensions\n", num_device_extensions);
+ goto done;
+ }
+
+ if(vkEnumerateDeviceExtensionProperties(physical_device, NULL, &num_device_extensions, device_extensions) != VK_SUCCESS) {
+ fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumerateDeviceExtensionProperties (get data) failed\n");
+ goto done;
+ }
+
+ for(uint32_t i = 0; i < num_device_extensions; ++i) {
+ if(strcmp(device_extensions[i].extensionName, "VK_KHR_video_encode_h264") == 0) {
+ video_codecs->h264 = true;
+ } else if(strcmp(device_extensions[i].extensionName, "VK_KHR_video_encode_h265") == 0) {
+ // TODO: Verify if 10bit and hdr are actually supported
+ video_codecs->hevc = true;
+ video_codecs->hevc_10bit = true;
+ video_codecs->hevc_hdr = true;
+ }
+ }
+
+ success = true;
+
+ done:
+ if(cleanup) {
+ if(device)
+ vkDestroyDevice(device, NULL);
+ if(instance)
+ vkDestroyInstance(instance, NULL);
+ }
+ if(device_extensions)
+ free(device_extensions);
+ return success;
+#else
+ // TODO: Low power query
+ video_codecs->h264 = (gsr_supported_video_codec){ true, false };
+ video_codecs->hevc = (gsr_supported_video_codec){ true, false };
+ return true;
+#endif
+}