Commit dbeafc5bdf for ffmpeg

commit dbeafc5bdf1d68a4a1f15db0e06286fab993fae5
Author: Lynne <dev@lynne.ee>
Date:   Sat Dec 5 16:13:07 2026 +0800

    avutil/hwcontext_vulkan: limit staging copies to the hardware frame size

    hwdownload allocates the software frame at the frames context size,
    which can be larger than an imported image (1088 rows vs 1080 for
    1080p H.264 decoded by VAAPI), so the copy read outside the image.

diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index d8464e2436..fff8c7530f 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -4756,7 +4756,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 width, int upload)
 {
     int err;
     VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
@@ -4765,7 +4765,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];
     }
@@ -4809,7 +4809,6 @@ static int get_plane_buf(AVHWFramesContext *hwfc, FFVkBuffer **dst,
                          AVFrame *swf, VkBufferImageCopy *region, int upload)
 {
     int err;
-    uint32_t p_w, p_h;
     VulkanFramesPriv *fp = hwfc->hwctx;
     VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
     const int planes = av_pix_fmt_count_planes(swf->format);
@@ -4818,19 +4817,11 @@ 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);
+        region[i].bufferOffset = buf_offset;
+        region[i].bufferRowLength = FFALIGN(FFABS(swf->linesize[i]),
+                                            p->props.properties.limits.optimalBufferCopyRowPitchAlignment);

-        region[i] = (VkBufferImageCopy) {
-            .bufferOffset = buf_offset,
-            .bufferRowLength = FFALIGN(FFABS(swf->linesize[i]),
-                                       p->props.properties.limits.optimalBufferCopyRowPitchAlignment),
-            .bufferImageHeight = p_h,
-            .imageSubresource.layerCount = 1,
-            .imageExtent = (VkExtent3D){ p_w, p_h, 1 },
-            /* Rest of the fields adjusted/filled in later */
-        };
-
-        buf_offset += FFALIGN(p_h*region[i].bufferRowLength,
+        buf_offset += FFALIGN(region[i].bufferImageHeight*region[i].bufferRowLength,
                               p->props.properties.limits.optimalBufferCopyOffsetAlignment);
     }

@@ -5098,9 +5089,13 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc,
     if (host_copy)
         return vulkan_transfer_host(hwfc, hwf, swf, upload);

-    for (int i = 0; i < av_pix_fmt_count_planes(swf->format); i++) {
+    /* hwdownload allocates at the frames context size, imports can be smaller */
+    int width  = FFMIN(swf->width,  hwf->width);
+    int height = FFMIN(swf->height, hwf->height);
+
+    for (int i = 0; i < planes; 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) {
@@ -5127,7 +5122,7 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc,
         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, width, 1);
             if (err < 0)
                 goto end;
         }
@@ -5207,7 +5202,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, width, 0);
     }

 end: