Skip to content

Commit

Permalink
Opt-in to Vulkan FFmpeg decode when enabled on device.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Nov 21, 2023
1 parent bfae68e commit ad12cd1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
38 changes: 26 additions & 12 deletions video/ffmpeg_hw_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,16 @@ struct FFmpegHWDevice::Impl
bool init_hw_device(const AVCodec *av_codec, const char *type)
{
#ifdef HAVE_FFMPEG_VULKAN
bool use_vulkan = false;
const char *env = getenv("GRANITE_FFMPEG_VULKAN");
if (env && strtol(env, nullptr, 0) != 0)
use_vulkan = true;
bool use_vulkan = device->get_device_features().supports_video_decode_queue;
if (use_vulkan)
{
if (av_codec->id == AV_CODEC_ID_H264)
use_vulkan = device->get_device_features().supports_video_decode_h264;
else if (av_codec->id == AV_CODEC_ID_HEVC)
use_vulkan = device->get_device_features().supports_video_decode_h265;
else
use_vulkan = false;
}
#endif

for (int i = 0; !hw_device; i++)
Expand All @@ -149,21 +155,29 @@ struct FFmpegHWDevice::Impl
if (config->device_type == AV_HWDEVICE_TYPE_NONE)
continue;

#ifdef HAVE_FFMPEG_VULKAN
if (config->device_type == AV_HWDEVICE_TYPE_VULKAN && !use_vulkan)
{
LOGI("Found Vulkan HW device, but Vulkan was not enabled in device.\n");
continue;
}
#endif

if (type)
{
const char *hwdevice_name = av_hwdevice_get_type_name(config->device_type);
LOGI("Found HW device type: %s\n", hwdevice_name);
if (strcmp(type, hwdevice_name) != 0)
continue;
}
#ifdef HAVE_FFMPEG_VULKAN
else
{
// Prefer Vulkan if it exists.
if (config->device_type == AV_HWDEVICE_TYPE_VULKAN && !use_vulkan)
{
LOGI("Found Vulkan HW device, but Vulkan was not enabled in device.\n");
continue;
}
else if (config->device_type != AV_HWDEVICE_TYPE_VULKAN && use_vulkan)
{
LOGI("Vulkan video is enabled on device, skipping non-Vulkan HW device.\n");
continue;
}
}
#endif

if ((config->methods & (AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX)) != 0)
init_hw_device_ctx(config);
Expand Down
11 changes: 10 additions & 1 deletion vulkan/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,9 @@ bool Context::create_instance(const char * const *instance_ext, uint32_t instanc
if (target_instance_version < VK_API_VERSION_1_3)
target_instance_version = VK_API_VERSION_1_3;

if (volkGetInstanceVersion() < target_instance_version && target_instance_version == VK_API_VERSION_1_3)
target_instance_version = user_application_info.get_application_info().apiVersion;

if (volkGetInstanceVersion() < target_instance_version)
{
LOGE("Vulkan loader does not support target Vulkan version.\n");
Expand Down Expand Up @@ -911,7 +914,13 @@ bool Context::create_device(VkPhysicalDevice gpu_, VkSurfaceKHR surface,
LOGI("Using Vulkan GPU: %s\n", gpu_props.deviceName);

// FFmpeg integration requires Vulkan 1.3 core for physical device.
const uint32_t minimum_api_version = (flags & video_context_flags) ? VK_API_VERSION_1_3 : VK_API_VERSION_1_1;
uint32_t minimum_api_version = (flags & video_context_flags) ? VK_API_VERSION_1_3 : VK_API_VERSION_1_1;
if (gpu_props.apiVersion < minimum_api_version && (flags & video_context_flags) != 0)
{
LOGW("Requested FFmpeg-enabled context, but Vulkan 1.3 was not supported. Falling back to 1.1 without support.\n");
minimum_api_version = VK_API_VERSION_1_1;
flags &= ~video_context_flags;
}

if (gpu_props.apiVersion < minimum_api_version)
{
Expand Down

0 comments on commit ad12cd1

Please sign in to comment.