PR #24835 opened by dhalsimax
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24835
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24835.patch

util_mem holds the peak detection state and the kernel expects it to start at 
zero, but it is created without a host pointer, so its contents are undefined. 
With Intel's compute-runtime on a Gen9 iGPU a whole film came out black, three 
runs out of three; with the buffer zeroed it is correct (retested on current 
master).

🤖 Generated with [Claude Code](https://claude.com/claude-code)


From 138a8ef558ac151d89e8a83061e70a899816d1dd Mon Sep 17 00:00:00 2001
From: dhalsimax <[email protected]>
Date: Thu, 1 Oct 2026 08:38:55 +0200
Subject: [PATCH] avfilter/vf_tonemap_opencl: zero the peak detection buffer

The kernel keeps its peak detection state in util_mem (frame index,
frame count, running totals of peak and average) and expects it to
start at zero. The buffer is created without a host pointer, so per the
OpenCL spec its contents are undefined. Intel's compute-runtime (NEO
24.35, legacy branch, on a Gen9 Apollo Lake) hands out recycled memory,
and the totals can start as garbage: the detected peak is then huge and
the whole stream comes out black (Y=16), or with the wrong brightness.
The same command gave black video every time on one film and a correct
picture on another; with the buffer zeroed both are correct.

Create the buffer from a zeroed array with CL_MEM_COPY_HOST_PTR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VB8Q6zwZGmj1CET8B15HLg
---
 libavfilter/vf_tonemap_opencl.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/libavfilter/vf_tonemap_opencl.c b/libavfilter/vf_tonemap_opencl.c
index 2f47cfd49f..aa8f50b6d6 100644
--- a/libavfilter/vf_tonemap_opencl.c
+++ b/libavfilter/vf_tonemap_opencl.c
@@ -116,6 +116,7 @@ static const float sdr_avg = 0.25f;
 
 static int tonemap_opencl_init(AVFilterContext *avctx)
 {
+    unsigned util_init[2 * DETECTION_FRAMES + 7] = { 0 };
     TonemapOpenCLContext *ctx = avctx->priv;
     int rgb2rgb_passthrough = 1;
     double rgb2rgb[3][3], rgb2yuv[3][3], yuv2rgb[3][3];
@@ -258,10 +259,12 @@ static int tonemap_opencl_init(AVFilterContext *avctx)
     ctx->kernel = clCreateKernel(ctx->ocf.program, "tonemap", &cle);
     CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
 
+    // The kernel keeps its peak detection state here and expects it to
+    // start at zero, but a buffer created without a host pointer has
+    // undefined contents.
     ctx->util_mem =
-        clCreateBuffer(ctx->ocf.hwctx->context, 0,
-                       (2 * DETECTION_FRAMES + 7) * sizeof(unsigned),
-                       NULL, &cle);
+        clCreateBuffer(ctx->ocf.hwctx->context, CL_MEM_COPY_HOST_PTR,
+                       sizeof(util_init), util_init, &cle);
     CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create util buffer: %d.\n", cle);
 
     ctx->initialised = 1;
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to