PR #26076 opened by Forgejo_Fairy URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26076 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26076.patch
Fixes the Vulkan usage and transfer-extent violations reported in #26019. DRM and VAAPI images mapped into a Vulkan frames context can have different image usage from pool images. Record the usage supplied to `vkCreateImage` for each image and use it when selecting host copies and creating image views. Images without `HOST_TRANSFER` use the staging transfer path. Limit both host and staging transfer regions to the mapped frame dimensions, which can be smaller than the padded frames context. Built on x86-64 with Vulkan, VAAPI and libdrm enabled. On an NVIDIA GPU, NV12 and RGBA upload/download frame hashes matched software output; `hflip_vulkan` also matched software `hflip` for a 320×180 NV12 frame. The reported Intel VAAPI command could not run here because VAAPI device creation failed before decoding. An Intel-system retest of the attached 720p and 1080p reproducers is requested in #26019. From 8d21f98e0b4d8be043ebe110565edca67029daf5 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 15 Nov 2026 08:22:08 +0000 Subject: [PATCH] avutil/vulkan: use image usage and dimensions for imported frame transfers Record the usage used at image creation on each Vulkan frame image. Imported DRM images may omit HOST_TRANSFER even when their frames context includes it; use the staging path for those images and give image views their actual usage flags. Limit transfer regions to the mapped frame dimensions, which can be smaller than the padded frames context. Assisted-by: Fairy --- libavutil/hwcontext_vulkan.c | 36 ++++++++++++++++++++++-------------- libavutil/hwcontext_vulkan.h | 6 ++++++ libavutil/vulkan.c | 18 ++++++++---------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c index 023b11224ea..0404d5476d7 100644 --- a/libavutil/hwcontext_vulkan.c +++ b/libavutil/hwcontext_vulkan.c @@ -2864,6 +2864,7 @@ static int create_frame(AVHWFramesContext *hwfc, AVVkFrame **frame, goto fail; } + f->usage[i] = create_info.usage; f->queue_family[i] = p->nb_img_qfs > 1 ? VK_QUEUE_FAMILY_IGNORED : p->img_qfs[0]; f->layout[i] = create_info.initialLayout; f->access[i] = 0x0; @@ -3742,6 +3743,7 @@ static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **f goto fail; } + f->usage[i] = create_info.usage; f->queue_family[i] = VK_QUEUE_FAMILY_EXTERNAL; f->layout[i] = create_info.initialLayout; f->access[i] = 0x0; @@ -4743,7 +4745,7 @@ static int vulkan_map_from(AVHWFramesContext *hwfc, AVFrame *dst, static int copy_buffer_data(AVHWFramesContext *hwfc, FFVkBuffer *vkbuf, AVFrame *swf, VkBufferImageCopy *region, - int planes, int upload) + int planes, int upload, int width) { int err; VulkanDevicePriv *p = hwfc->device_ctx->hwctx; @@ -4752,7 +4754,7 @@ static int copy_buffer_data(AVHWFramesContext *hwfc, FFVkBuffer *vkbuf, /* Copy only the visible bytes of each row, the last row of a plane * does not have to extend to the full linesize. */ for (int i = 0; i < planes; i++) { - bytewidth[i] = av_image_get_linesize(swf->format, swf->width, i); + bytewidth[i] = av_image_get_linesize(swf->format, width, i); if (bytewidth[i] < 0) return bytewidth[i]; } @@ -4793,7 +4795,8 @@ static int copy_buffer_data(AVHWFramesContext *hwfc, FFVkBuffer *vkbuf, } static int get_plane_buf(AVHWFramesContext *hwfc, FFVkBuffer **dst, - AVFrame *swf, VkBufferImageCopy *region, int upload) + AVFrame *swf, VkBufferImageCopy *region, int upload, + int width, int height) { int err; uint32_t p_w, p_h; @@ -4805,7 +4808,7 @@ static int get_plane_buf(AVHWFramesContext *hwfc, FFVkBuffer **dst, size_t buf_offset = 0; for (int i = 0; i < planes; i++) { - get_plane_wh(&p_w, &p_h, swf->format, swf->width, swf->height, i); + get_plane_wh(&p_w, &p_h, swf->format, width, height, i); region[i] = (VkBufferImageCopy) { .bufferOffset = buf_offset, @@ -4896,7 +4899,7 @@ fail: } static int vulkan_transfer_host(AVHWFramesContext *hwfc, AVFrame *hwf, - AVFrame *swf, int upload) + AVFrame *swf, int upload, int width, int height) { VulkanFramesPriv *fp = hwfc->hwctx; AVVulkanFramesContext *hwfc_vk = &fp->p; @@ -4972,7 +4975,7 @@ static int vulkan_transfer_host(AVHWFramesContext *hwfc, AVFrame *hwf, for (int i = 0; i < planes; i++) { int img_idx = FFMIN(i, (nb_images - 1)); uint32_t p_w, p_h; - get_plane_wh(&p_w, &p_h, swf->format, swf->width, swf->height, i); + get_plane_wh(&p_w, &p_h, swf->format, width, height, i); region_info.pHostPointer = swf->data[i]; region_info.memoryRowLength = swf->linesize[i] / desc->comp[i].step; @@ -4998,7 +5001,7 @@ static int vulkan_transfer_host(AVHWFramesContext *hwfc, AVFrame *hwf, for (int i = 0; i < planes; i++) { int img_idx = FFMIN(i, (nb_images - 1)); uint32_t p_w, p_h; - get_plane_wh(&p_w, &p_h, swf->format, swf->width, swf->height, i); + get_plane_wh(&p_w, &p_h, swf->format, width, height, i); region_info.pHostPointer = swf->data[i]; region_info.memoryRowLength = swf->linesize[i] / desc->comp[i].step; @@ -5022,7 +5025,6 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc, { int err; VulkanFramesPriv *fp = hwfc->hwctx; - AVVulkanFramesContext *hwctx = &fp->p; VulkanDevicePriv *p = hwfc->device_ctx->hwctx; FFVulkanFunctions *vk = &p->vkctx.vkfn; @@ -5053,7 +5055,13 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc, if (swf->width > hwfc->width || swf->height > hwfc->height) return AVERROR(EINVAL); - int host_copy = hwctx->usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT; + /* Mapped images can be smaller than the padded frames context. */ + int width = FFMIN(swf->width, hwf->width); + int height = FFMIN(swf->height, hwf->height); + + int host_copy = 1; + for (int i = 0; i < nb_images; i++) + host_copy &= !!(hwf_vk->usage[i] & VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT); int host_map = p->vkctx.extensions & FF_VK_EXT_EXTERNAL_HOST_MEMORY && !p->avoid_host_import; @@ -5081,11 +5089,11 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc, } if (host_copy) - return vulkan_transfer_host(hwfc, hwf, swf, upload); + return vulkan_transfer_host(hwfc, hwf, swf, upload, width, height); for (int i = 0; i < av_pix_fmt_count_planes(swf->format); i++) { uint32_t p_w, p_h; - get_plane_wh(&p_w, &p_h, swf->format, swf->width, swf->height, i); + get_plane_wh(&p_w, &p_h, swf->format, width, height, i); /* Buffer region for this plane */ region[i] = (VkBufferImageCopy) { @@ -5106,13 +5114,13 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc, } if (!host_mapped) { - err = get_plane_buf(hwfc, &bufs[0], swf, region, upload); + err = get_plane_buf(hwfc, &bufs[0], swf, region, upload, width, height); if (err < 0) goto end; nb_bufs = 1; if (upload) { - err = copy_buffer_data(hwfc, bufs[0], swf, region, planes, 1); + err = copy_buffer_data(hwfc, bufs[0], swf, region, planes, 1, width); if (err < 0) goto end; } @@ -5192,7 +5200,7 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc, if (err >= 0 && !upload) { ff_vk_exec_wait(&p->vkctx, exec); if (!host_mapped) - err = copy_buffer_data(hwfc, bufs[0], swf, region, planes, 0); + err = copy_buffer_data(hwfc, bufs[0], swf, region, planes, 0, width); } end: diff --git a/libavutil/hwcontext_vulkan.h b/libavutil/hwcontext_vulkan.h index 87c2a2d28de..d9cc800bfbd 100644 --- a/libavutil/hwcontext_vulkan.h +++ b/libavutil/hwcontext_vulkan.h @@ -323,6 +323,12 @@ struct AVVkFrame { * Internal data. Not to be accessed by users in any way. */ struct AVVkFrameInternal *internal; + + /** + * Usage flags supplied when each image was created. Set this when + * supplying images not allocated or imported by this implementation. + */ + VkImageUsageFlags usage[AV_NUM_DATA_POINTERS]; }; /** diff --git a/libavutil/vulkan.c b/libavutil/vulkan.c index 76e9a773a57..eb5f88d9ddd 100644 --- a/libavutil/vulkan.c +++ b/libavutil/vulkan.c @@ -2106,20 +2106,19 @@ int ff_vk_create_imageview(FFVulkanContext *s, VkResult ret; FFVulkanFunctions *vk = &s->vkfn; AVHWFramesContext *hwfc = (AVHWFramesContext *)f->hw_frames_ctx->data; - AVVulkanFramesContext *vkfc = hwfc->hwctx; const VkFormat *rep_fmts = av_vkfmt_from_pixfmt(hwfc->sw_format); AVVkFrame *vkf = (AVVkFrame *)f->data[0]; const int nb_images = ff_vk_count_images(vkf); VkImageViewUsageCreateInfo view_usage_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO, - .usage = vkfc->usage & - (~(VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR | - VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR)), + .usage = vkf->usage[FFMIN(plane, nb_images - 1)] & + ~(VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR | + VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR), }; VkImageViewCreateInfo view_create_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - .pNext = &view_usage_info, + .pNext = view_usage_info.usage ? &view_usage_info : NULL, .image = vkf->img[FFMIN(plane, nb_images - 1)], .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = ff_vk_map_fmt_to_rep(rep_fmts[plane], rep_fmt), @@ -2159,7 +2158,6 @@ int ff_vk_create_imageviews_fmt(FFVulkanContext *s, FFVkExecContext *e, VkResult ret; FFVulkanFunctions *vk = &s->vkfn; AVHWFramesContext *hwfc = (AVHWFramesContext *)f->hw_frames_ctx->data; - AVVulkanFramesContext *vkfc = hwfc->hwctx; AVVkFrame *vkf = (AVVkFrame *)f->data[0]; const int nb_images = ff_vk_count_images(vkf); const int nb_planes = av_pix_fmt_count_planes(hwfc->sw_format); @@ -2168,13 +2166,13 @@ int ff_vk_create_imageviews_fmt(FFVulkanContext *s, FFVkExecContext *e, for (int i = 0; i < nb_planes; i++) { VkImageViewUsageCreateInfo view_usage_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO, - .usage = vkfc->usage & - (~(VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR | - VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR)), + .usage = vkf->usage[FFMIN(i, nb_images - 1)] & + ~(VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR | + VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR), }; VkImageViewCreateInfo view_create_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - .pNext = &view_usage_info, + .pNext = view_usage_info.usage ? &view_usage_info : NULL, .image = vkf->img[FFMIN(i, nb_images - 1)], .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmts[i], -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
