Commit 1341bdaf77 for ffmpeg

commit 1341bdaf77beaf8089cb8ca6ab56844b07d2ddf8
Author: Zhao Zhili <quinkblack@foxmail.com>
Date:   Wed Nov 25 10:47:37 2026 +0800

    swscale/tests/swscale: reuse uploaded Vulkan input frames

    Each runner uses the same input for multiple conversions, so it can
    share the setup and upload across those conversions. Wall time of
    the Vulkan tests with NVIDIA GB10 and GeForce RTX 3060:

                                 before   after  speedup
    NVIDIA GB10
      fate-sws-unscaled-spirv    35.8 s  19.7 s    1.81x
      fate-sws-unstable-spirv    67.0 s  43.5 s    1.54x

    NVIDIA GeForce RTX 3060
      fate-sws-unscaled-spirv    16.0 s   9.1 s    1.75x
      fate-sws-unstable-spirv    29.2 s  19.4 s    1.51x

diff --git a/libswscale/tests/swscale.c b/libswscale/tests/swscale.c
index 0e47df117f..77b986e286 100644
--- a/libswscale/tests/swscale.c
+++ b/libswscale/tests/swscale.c
@@ -102,6 +102,7 @@ struct runner {
     SwsContext *sws_src_dst;
     SwsContext *sws_dst_out;
     AVFrame *src;
+    AVFrame *hw_src;
 };

 struct test_case {
@@ -388,16 +389,49 @@ error:
     return ret;
 }

-static int scale_hw(AVFrame *dst, const AVFrame *src,
+static int alloc_hw_frame(AVFrame **frame, const AVFrame *sw)
+{
+    AVBufferRef *ref = av_hwframe_ctx_alloc(state.hw_device_ctx);
+    if (!ref)
+        return AVERROR(ENOMEM);
+
+    AVHWFramesContext *ctx = (AVHWFramesContext *)ref->data;
+    ctx->format    = AV_PIX_FMT_VULKAN;
+    ctx->sw_format = sw->format;
+    ctx->width     = sw->width;
+    ctx->height    = sw->height;
+
+    int ret = av_hwframe_ctx_init(ref);
+    if (ret < 0) {
+        if (ret != AVERROR(ENOTSUP))
+            av_log(NULL, AV_LOG_ERROR, "Failed to create HW frames context: %s\n",
+                   av_err2str(ret));
+        goto end;
+    }
+
+    *frame = av_frame_alloc();
+    if (!*frame) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    ret = av_hwframe_get_buffer(ref, *frame, 0);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Failed to allocate HW frame: %s\n",
+               av_err2str(ret));
+        av_frame_free(frame);
+    }
+
+end:
+    av_buffer_unref(&ref);
+    return ret;
+}
+
+static int scale_hw(struct runner *runner, AVFrame *dst, const AVFrame *src,
                     const struct mode *mode, const struct options *opts,
                     int64_t *out_time)
 {
     SwsContext *sws_hw = NULL;
-    AVBufferRef *in_ref = NULL;
-    AVBufferRef *out_ref = NULL;
-    AVHWFramesContext *in_ctx = NULL;
-    AVHWFramesContext *out_ctx = NULL;
-    AVFrame *in_f = NULL;
     AVFrame *out_f = NULL;
     int ret;

@@ -414,81 +448,26 @@ static int scale_hw(AVFrame *dst, const AVFrame *src,
     sws_hw->threads    = opts->threads;
     sws_hw->backends   = opts->backends;

-    in_ref = av_hwframe_ctx_alloc(state.hw_device_ctx);
-    if (!in_ref) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-
-    in_ctx = (AVHWFramesContext *)in_ref->data;
-    in_ctx->format = AV_PIX_FMT_VULKAN;
-    in_ctx->sw_format = src->format;
-    in_ctx->width = src->width;
-    in_ctx->height = src->height;
-    ret = av_hwframe_ctx_init(in_ref);
-    if (ret < 0) {
-        if (ret != AVERROR(ENOTSUP))
-            av_log(NULL, AV_LOG_ERROR, "Failed to create input HW context: %s\n",
-                   av_err2str(ret));
-        goto error;
-    }
-
-    out_ref = av_hwframe_ctx_alloc(state.hw_device_ctx);
-    if (!out_ref) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-
-    out_ctx = (AVHWFramesContext *) out_ref->data;
-    out_ctx->format = AV_PIX_FMT_VULKAN;
-    out_ctx->sw_format = dst->format;
-    out_ctx->width = dst->width;
-    out_ctx->height = dst->height;
-    ret = av_hwframe_ctx_init(out_ref);
-    if (ret < 0) {
-        if (ret != AVERROR(ENOTSUP))
-            av_log(NULL, AV_LOG_ERROR, "Failed to create output HW context: %s\n",
-                   av_err2str(ret));
-        goto error;
-    }
-
-    in_f = av_frame_alloc();
-    if (!in_f) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-    in_f->width = src->width;
-    in_f->height = src->height;
-    in_f->format = AV_PIX_FMT_VULKAN;
-    ret = av_hwframe_get_buffer(in_ref, in_f, 0);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Failed to allocate input HW frame\n");
-        goto error;
-    }
+    if (!runner->hw_src) {
+        ret = alloc_hw_frame(&runner->hw_src, src);
+        if (ret < 0)
+            goto error;

-    ret = av_hwframe_transfer_data(in_f, src, 0);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Failed to upload HW frame\n");
-        goto error;
+        ret = av_hwframe_transfer_data(runner->hw_src, src, 0);
+        if (ret < 0) {
+            av_log(NULL, AV_LOG_ERROR, "Failed to upload HW frame\n");
+            av_frame_free(&runner->hw_src);
+            goto error;
+        }
     }

-    out_f = av_frame_alloc();
-    if (!out_f) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-    out_f->width = dst->width;
-    out_f->height = dst->height;
-    out_f->format = AV_PIX_FMT_VULKAN;
-    ret = av_hwframe_get_buffer(out_ref, out_f, 0);
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Failed to allocate output HW frame\n");
+    ret = alloc_hw_frame(&out_f, dst);
+    if (ret < 0)
         goto error;
-    }

     int64_t time = av_gettime_relative();
     for (int i = 0; ret >= 0 && i < opts->iters; i++) {
-        ret = checked_sws_scale_frame(sws_hw, out_f, in_f);
+        ret = checked_sws_scale_frame(sws_hw, out_f, runner->hw_src);
         if (ret < 0)
             goto error;
     }
@@ -507,10 +486,7 @@ static int scale_hw(AVFrame *dst, const AVFrame *src,
     ret = 0;

 error:
-    av_frame_free(&in_f);
     av_frame_free(&out_f);
-    av_buffer_unref(&in_ref);
-    av_buffer_unref(&out_ref);
     sws_free_context(&sws_hw);
     return ret;
 }
@@ -797,6 +773,7 @@ static int run_test(struct runner *runner, const struct test_case *t,

     if (!src || src->format != t->src_fmt) {
         av_frame_free(&runner->src);
+        av_frame_free(&runner->hw_src);
         ret = init_frame(&src, ref, ref->width, ref->height, t->src_fmt);
         if (ret < 0)
             goto error;
@@ -821,7 +798,7 @@ static int run_test(struct runner *runner, const struct test_case *t,
         goto error;

     ret = (opts->api == IMPL_LEGACY) ? scale_legacy(dst, src, &t->mode, opts, &r.time)
-        : state.hw_device_ctx        ? scale_hw(dst, src, &t->mode, opts, &r.time)
+        : state.hw_device_ctx        ? scale_hw(runner, dst, src, &t->mode, opts, &r.time)
         :                              scale_new(runner, dst, src, &t->mode, opts, &r.time);
     if (ret < 0) {
         if (ret == AVERROR(ENOTSUP))
@@ -1109,6 +1086,7 @@ static void runner_free(struct runner *runner)
     sws_free_context(&runner->sws_ref_src);
     sws_free_context(&runner->sws_src_dst);
     sws_free_context(&runner->sws_dst_out);
+    av_frame_free(&runner->hw_src);
     av_frame_free(&runner->src);
 }