Commit 1503c58876 for ffmpeg
commit 1503c58876bc20926d1bffe8506f8f8a81a683c0
Author: Tymur Boiko <tboiko@nvidia.com>
Date: Thu Nov 5 17:24:51 2026 +0100
avcodec/vulkan_decode: make encode usage optional for decoding
Requiring VIDEO_ENCODE_SRC in decode format queries rejects valid
decode-only formats, such as 12-bit VP9 on NVIDIA Blackwell GPU,
and causes hardware initialization to fail.
Probe combined decode and encode-source usage explicitly, since some
drivers omit unrequested video usages from the returned properties.
Retry without VIDEO_ENCODE_SRC when unsupported or no formats are
returned. Propagate the fallback and prevent automatic usage filling
from re-adding the bit, preserving successful combined usage for DRM
modifier pools.
Tested 8/10-bit DRM transcoding and 12-bit decoding on NVIDIA Blackwell
GPU, with normal, filter, H.264, HEVC and VP9 pipeline checks.
Signed-off-by: Tymur Boiko <tboiko@nvidia.com>
diff --git a/libavcodec/vulkan_decode.c b/libavcodec/vulkan_decode.c
index 3c78c22c95..5e649e7dc5 100644
--- a/libavcodec/vulkan_decode.c
+++ b/libavcodec/vulkan_decode.c
@@ -830,7 +830,7 @@ static const char *profile_name(enum AVCodecID codec_id, int profile)
static int vulkan_decode_get_profile(AVCodecContext *avctx, AVBufferRef *frames_ref,
enum AVPixelFormat *pix_fmt, VkFormat *vk_fmt,
FFVulkanDecodeProfileData *prof,
- int *dpb_dedicate)
+ int *dpb_dedicate, int *decode_only)
{
VkResult ret;
int max_level, base_profile, cur_profile;
@@ -1055,15 +1055,32 @@ static int vulkan_decode_get_profile(AVCodecContext *avctx, AVBufferRef *frames_
VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT;
+ /* Some drivers omit unrequested video usages from the returned
+ * properties, so query optional encode-source support explicitly.
+ * An image-format probe after decode-only enumeration would always
+ * add a query; here an extra count query is needed only on fallback. */
if ((ctx->s.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
(ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_1))
fmt_info.imageUsage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
}
+ *decode_only = 0;
+
/* Get the format of the images necessary */
ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->phys_dev,
&fmt_info,
&nb_out_fmts, NULL);
+ /* Requiring encode-source usage can exclude formats usable for decoding.
+ * Retry without it so decode-only formats can still initialize. */
+ if ((fmt_info.imageUsage & VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR) &&
+ (ret == VK_ERROR_IMAGE_USAGE_NOT_SUPPORTED_KHR ||
+ (ret == VK_SUCCESS && !nb_out_fmts))) {
+ fmt_info.imageUsage &= ~VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
+ *decode_only = 1;
+ ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->phys_dev,
+ &fmt_info,
+ &nb_out_fmts, NULL);
+ }
if (ret == VK_ERROR_FORMAT_NOT_SUPPORTED ||
(!nb_out_fmts && ret == VK_SUCCESS)) {
return AVERROR(EINVAL);
@@ -1141,7 +1158,7 @@ static void free_profile_data(AVHWFramesContext *hwfc)
int ff_vk_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
{
- int err, dedicated_dpb;
+ int err, dedicated_dpb, decode_only;
AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
AVVulkanFramesContext *hwfc = frames_ctx->hwctx;
FFVulkanDecodeContext *dec = avctx->internal->hwaccel_priv_data;
@@ -1164,7 +1181,7 @@ int ff_vk_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
err = vulkan_decode_get_profile(avctx, hw_frames_ctx,
&frames_ctx->sw_format,
&hwfc->format[0],
- prof, &dedicated_dpb);
+ prof, &dedicated_dpb, &decode_only);
if (err < 0) {
av_free(prof);
return err;
@@ -1233,7 +1250,8 @@ int ff_vk_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
hwfc->usage |= VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
ctx = dec->shared_ctx;
- if ((ctx->s.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
+ if (!decode_only &&
+ (ctx->s.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
(ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_1))
hwfc->usage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
}
diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index 023b11224e..c65a197ca9 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -3344,7 +3344,8 @@ static int vulkan_frames_init(AVHWFramesContext *hwfc)
hwctx->usage |= supported_usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT;
/* Enables encoding of images, if supported by format and extensions */
- if ((supported_usage & VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR) &&
+ if (!(hwctx->usage & VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR) &&
+ (supported_usage & VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR) &&
(p->vkctx.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
(p->vkctx.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_1))
hwctx->usage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;