From 71bf8da70361c94d9106a0bc83f1af0e96f87f84 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Sat, 29 Mar 2025 11:34:57 +0100
Subject: [PATCH 1/2] avfilter/opencl: Add wrapper to load exactly one program

ff_opencl_filter_load_program() can create a program
from an array of strings, yet almost all users only
want to use a single one. This commit adds a specialization
for this common case.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavfilter/opencl.c                | 14 ++++++++++----
 libavfilter/opencl.h                | 12 ++++++++++--
 libavfilter/vf_avgblur_opencl.c     |  2 +-
 libavfilter/vf_colorkey_opencl.c    |  2 +-
 libavfilter/vf_convolution_opencl.c |  2 +-
 libavfilter/vf_deshake_opencl.c     |  2 +-
 libavfilter/vf_neighbor_opencl.c    |  2 +-
 libavfilter/vf_nlmeans_opencl.c     |  2 +-
 libavfilter/vf_overlay_opencl.c     |  2 +-
 libavfilter/vf_pad_opencl.c         |  2 +-
 libavfilter/vf_remap_opencl.c       |  2 +-
 libavfilter/vf_tonemap_opencl.c     |  2 +-
 libavfilter/vf_transpose_opencl.c   |  2 +-
 libavfilter/vf_unsharp_opencl.c     |  2 +-
 libavfilter/vf_xfade_opencl.c       |  2 +-
 15 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/libavfilter/opencl.c b/libavfilter/opencl.c
index e91610a10e..4726d0b111 100644
--- a/libavfilter/opencl.c
+++ b/libavfilter/opencl.c
@@ -156,9 +156,9 @@ void ff_opencl_filter_uninit(AVFilterContext *avctx)
     av_buffer_unref(&ctx->device_ref);
 }
 
-int ff_opencl_filter_load_program(AVFilterContext *avctx,
-                                  const char **program_source_array,
-                                  int nb_strings)
+int ff_opencl_filter_load_programs(AVFilterContext *avctx,
+                                   const char **program_source_array,
+                                   int nb_strings)
 {
     OpenCLFilterContext *ctx = avctx->priv;
     cl_int cle;
@@ -204,6 +204,12 @@ int ff_opencl_filter_load_program(AVFilterContext *avctx,
     return 0;
 }
 
+int ff_opencl_filter_load_program(AVFilterContext *avctx,
+                                  const char *program_source)
+{
+    return ff_opencl_filter_load_programs(avctx, &program_source, 1);
+}
+
 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
                                             const char *filename)
 {
@@ -256,7 +262,7 @@ int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
 
     src_const = src;
 
-    err = ff_opencl_filter_load_program(avctx, &src_const, 1);
+    err = ff_opencl_filter_load_program(avctx, src_const);
 fail:
     fclose(file);
     av_freep(&src);
diff --git a/libavfilter/opencl.h b/libavfilter/opencl.h
index dec376766e..1448d0a3eb 100644
--- a/libavfilter/opencl.h
+++ b/libavfilter/opencl.h
@@ -263,9 +263,17 @@ void ff_opencl_filter_uninit(AVFilterContext *avctx);
  * Creates a new program and compiles it for the current device.
  * Will log any build errors if compilation fails.
  */
+int ff_opencl_filter_load_programs(AVFilterContext *avctx,
+                                   const char **program_source_array,
+                                   int nb_strings);
+
+/**
+ * Load a new OpenCL program from a string in memory.
+ * This is a wrapper around ff_opencl_filter_load_programs()
+ * for the case of a single string.
+ */
 int ff_opencl_filter_load_program(AVFilterContext *avctx,
-                                  const char **program_source_array,
-                                  int nb_strings);
+                                  const char *program_source);
 
 /**
  * Load a new OpenCL program from a file.
diff --git a/libavfilter/vf_avgblur_opencl.c b/libavfilter/vf_avgblur_opencl.c
index 790a51ea80..edd66748d8 100644
--- a/libavfilter/vf_avgblur_opencl.c
+++ b/libavfilter/vf_avgblur_opencl.c
@@ -59,7 +59,7 @@ static int avgblur_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_avgblur_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_avgblur_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_colorkey_opencl.c b/libavfilter/vf_colorkey_opencl.c
index 403e763503..3b9830525f 100644
--- a/libavfilter/vf_colorkey_opencl.c
+++ b/libavfilter/vf_colorkey_opencl.c
@@ -50,7 +50,7 @@ static int colorkey_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_colorkey_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_colorkey_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_convolution_opencl.c b/libavfilter/vf_convolution_opencl.c
index 7193a2b0d9..ec4dd426ba 100644
--- a/libavfilter/vf_convolution_opencl.c
+++ b/libavfilter/vf_convolution_opencl.c
@@ -63,7 +63,7 @@ static int convolution_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_convolution_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_convolution_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_deshake_opencl.c b/libavfilter/vf_deshake_opencl.c
index dc3df0e989..0026f650c5 100644
--- a/libavfilter/vf_deshake_opencl.c
+++ b/libavfilter/vf_deshake_opencl.c
@@ -1250,7 +1250,7 @@ static int deshake_opencl_init(AVFilterContext *avctx)
     }
     ctx->sw_format = hw_frames_ctx->sw_format;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_deshake_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_deshake_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_neighbor_opencl.c b/libavfilter/vf_neighbor_opencl.c
index 38a1b7821e..5eb7caeb6a 100644
--- a/libavfilter/vf_neighbor_opencl.c
+++ b/libavfilter/vf_neighbor_opencl.c
@@ -55,7 +55,7 @@ static int neighbor_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_neighbor_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_neighbor_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_nlmeans_opencl.c b/libavfilter/vf_nlmeans_opencl.c
index ae64429efc..ff44125ef1 100644
--- a/libavfilter/vf_nlmeans_opencl.c
+++ b/libavfilter/vf_nlmeans_opencl.c
@@ -98,7 +98,7 @@ static int nlmeans_opencl_init(AVFilterContext *avctx, int width, int height)
     if (!ctx->patch_size_uv)
         ctx->patch_size_uv = ctx->patch_size;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_nlmeans_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_nlmeans_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_overlay_opencl.c b/libavfilter/vf_overlay_opencl.c
index e930aced04..99519c19d2 100644
--- a/libavfilter/vf_overlay_opencl.c
+++ b/libavfilter/vf_overlay_opencl.c
@@ -92,7 +92,7 @@ static int overlay_opencl_load(AVFilterContext *avctx,
 
     av_log(avctx, AV_LOG_DEBUG, "Using kernel %s.\n", kernel);
 
-    err = ff_opencl_filter_load_program(avctx, &source, 1);
+    err = ff_opencl_filter_load_program(avctx, source);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_pad_opencl.c b/libavfilter/vf_pad_opencl.c
index 9dc0290c19..55a450af76 100644
--- a/libavfilter/vf_pad_opencl.c
+++ b/libavfilter/vf_pad_opencl.c
@@ -92,7 +92,7 @@ static int pad_opencl_init(AVFilterContext *avctx, AVFrame *input_frame)
     ctx->hsub = desc->log2_chroma_w;
     ctx->vsub = desc->log2_chroma_h;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_pad_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_pad_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_remap_opencl.c b/libavfilter/vf_remap_opencl.c
index bb83944b2f..3fa4d2f723 100644
--- a/libavfilter/vf_remap_opencl.c
+++ b/libavfilter/vf_remap_opencl.c
@@ -103,7 +103,7 @@ static int remap_opencl_load(AVFilterContext *avctx,
 
     ctx->nb_planes = main_planes;
 
-    err = ff_opencl_filter_load_program(avctx, &source, 1);
+    err = ff_opencl_filter_load_program(avctx, source);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_tonemap_opencl.c b/libavfilter/vf_tonemap_opencl.c
index 758c0e5e94..056629c98b 100644
--- a/libavfilter/vf_tonemap_opencl.c
+++ b/libavfilter/vf_tonemap_opencl.c
@@ -242,7 +242,7 @@ static int tonemap_opencl_init(AVFilterContext *avctx)
     opencl_sources[0] = header.str;
     opencl_sources[1] = ff_source_tonemap_cl;
     opencl_sources[2] = ff_source_colorspace_common_cl;
-    err = ff_opencl_filter_load_program(avctx, opencl_sources, OPENCL_SOURCE_NB);
+    err = ff_opencl_filter_load_programs(avctx, opencl_sources, OPENCL_SOURCE_NB);
 
     av_bprint_finalize(&header, NULL);
     if (err < 0)
diff --git a/libavfilter/vf_transpose_opencl.c b/libavfilter/vf_transpose_opencl.c
index 51201a9a71..64af30f687 100644
--- a/libavfilter/vf_transpose_opencl.c
+++ b/libavfilter/vf_transpose_opencl.c
@@ -44,7 +44,7 @@ static int transpose_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_transpose_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_transpose_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_unsharp_opencl.c b/libavfilter/vf_unsharp_opencl.c
index 15853e8db3..aa6dcd6bc2 100644
--- a/libavfilter/vf_unsharp_opencl.c
+++ b/libavfilter/vf_unsharp_opencl.c
@@ -69,7 +69,7 @@ static int unsharp_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_source_unsharp_cl, 1);
+    err = ff_opencl_filter_load_program(avctx, ff_source_unsharp_cl);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_xfade_opencl.c b/libavfilter/vf_xfade_opencl.c
index 96dc7e4554..1952e897b0 100644
--- a/libavfilter/vf_xfade_opencl.c
+++ b/libavfilter/vf_xfade_opencl.c
@@ -92,7 +92,7 @@ static int xfade_opencl_load(AVFilterContext *avctx,
     if (ctx->transition == CUSTOM) {
         err = ff_opencl_filter_load_program_from_file(avctx, ctx->source_file);
     } else {
-        err = ff_opencl_filter_load_program(avctx, &ff_source_xfade_cl, 1);
+        err = ff_opencl_filter_load_program(avctx, ff_source_xfade_cl);
     }
     if (err < 0)
         return err;
-- 
2.45.2