FFmpeg
Loading...
Searching...
No Matches
tonemap.c
Go to the documentation of this file.
1// Generated from libavfilter/opencl/tonemap.cl
3"/*\n"
4" * This file is part of FFmpeg.\n"
5" *\n"
6" * FFmpeg is free software; you can redistribute it and/or\n"
7" * modify it under the terms of the GNU Lesser General Public\n"
8" * License as published by the Free Software Foundation; either\n"
9" * version 2.1 of the License, or (at your option) any later version.\n"
10" *\n"
11" * FFmpeg is distributed in the hope that it will be useful,\n"
12" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
13" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
14" * Lesser General Public License for more details.\n"
15" *\n"
16" * You should have received a copy of the GNU Lesser General Public\n"
17" * License along with FFmpeg; if not, write to the Free Software\n"
18" * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
19" */\n"
20"\n"
21"#define REFERENCE_WHITE 100.0f\n"
22"extern float3 lrgb2yuv(float3);\n"
23"extern float lrgb2y(float3);\n"
24"extern float3 yuv2lrgb(float3);\n"
25"extern float3 lrgb2lrgb(float3);\n"
26"extern float get_luma_src(float3);\n"
27"extern float get_luma_dst(float3);\n"
28"extern float3 ootf(float3 c, float peak);\n"
29"extern float3 inverse_ootf(float3 c, float peak);\n"
30"extern float3 get_chroma_sample(float3, float3, float3, float3);\n"
31"\n"
32"struct detection_result {\n"
33" float peak;\n"
34" float average;\n"
35"};\n"
36"\n"
37"float hable_f(float in) {\n"
38" float a = 0.15f, b = 0.50f, c = 0.10f, d = 0.20f, e = 0.02f, f = 0.30f;\n"
39" return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f;\n"
40"}\n"
41"\n"
42"float direct(float s, float peak) {\n"
43" return s;\n"
44"}\n"
45"\n"
46"float linear(float s, float peak) {\n"
47" return s * tone_param / peak;\n"
48"}\n"
49"\n"
50"float gamma(float s, float peak) {\n"
51" float p = s > 0.05f ? s /peak : 0.05f / peak;\n"
52" float v = powr(p, 1.0f / tone_param);\n"
53" return s > 0.05f ? v : (s * v /0.05f);\n"
54"}\n"
55"\n"
56"float clip(float s, float peak) {\n"
57" return clamp(s * tone_param, 0.0f, 1.0f);\n"
58"}\n"
59"\n"
60"float reinhard(float s, float peak) {\n"
61" return s / (s + tone_param) * (peak + tone_param) / peak;\n"
62"}\n"
63"\n"
64"float hable(float s, float peak) {\n"
65" return hable_f(s)/hable_f(peak);\n"
66"}\n"
67"\n"
68"float mobius(float s, float peak) {\n"
69" float j = tone_param;\n"
70" float a, b;\n"
71"\n"
72" if (s <= j)\n"
73" return s;\n"
74"\n"
75" a = -j * j * (peak - 1.0f) / (j * j - 2.0f * j + peak);\n"
76" b = (j * j - 2.0f * j * peak + peak) / max(peak - 1.0f, 1e-6f);\n"
77"\n"
78" return (b * b + 2.0f * b * j + j * j) / (b - a) * (s + a) / (s + b);\n"
79"}\n"
80"\n"
81"// detect peak/average signal of a frame, the algorithm was ported from:\n"
82"// libplacebo (https://github.com/haasn/libplacebo)\n"
83"struct detection_result\n"
84"detect_peak_avg(global uint *util_buf, __local uint *sum_wg,\n"
85" float signal, float peak) {\n"
86"// layout of the util buffer\n"
87"//\n"
88"// Name: : Size (units of 4-bytes)\n"
89"// average buffer : detection_frames + 1\n"
90"// peak buffer : detection_frames + 1\n"
91"// workgroup counter : 1\n"
92"// total of peak : 1\n"
93"// total of average : 1\n"
94"// frame index : 1\n"
95"// frame number : 1\n"
96" global uint *avg_buf = util_buf;\n"
97" global uint *peak_buf = avg_buf + DETECTION_FRAMES + 1;\n"
98" global uint *counter_wg_p = peak_buf + DETECTION_FRAMES + 1;\n"
99" global uint *max_total_p = counter_wg_p + 1;\n"
100" global uint *avg_total_p = max_total_p + 1;\n"
101" global uint *frame_idx_p = avg_total_p + 1;\n"
102" global uint *scene_frame_num_p = frame_idx_p + 1;\n"
103"\n"
104" uint frame_idx = *frame_idx_p;\n"
105" uint scene_frame_num = *scene_frame_num_p;\n"
106"\n"
107" size_t lidx = get_local_id(0);\n"
108" size_t lidy = get_local_id(1);\n"
109" size_t lsizex = get_local_size(0);\n"
110" size_t lsizey = get_local_size(1);\n"
111" uint num_wg = get_num_groups(0) * get_num_groups(1);\n"
112" size_t group_idx = get_group_id(0);\n"
113" size_t group_idy = get_group_id(1);\n"
114" struct detection_result r = {peak, sdr_avg};\n"
115" if (lidx == 0 && lidy == 0)\n"
116" *sum_wg = 0;\n"
117" barrier(CLK_LOCAL_MEM_FENCE);\n"
118"\n"
119" // update workgroup sum\n"
120" atomic_add(sum_wg, (uint)(signal * REFERENCE_WHITE));\n"
121" barrier(CLK_LOCAL_MEM_FENCE);\n"
122"\n"
123" // update frame peak/avg using work-group-average.\n"
124" if (lidx == 0 && lidy == 0) {\n"
125" uint avg_wg = *sum_wg / (lsizex * lsizey);\n"
126" atomic_max(&peak_buf[frame_idx], avg_wg);\n"
127" atomic_add(&avg_buf[frame_idx], avg_wg);\n"
128" }\n"
129"\n"
130" if (scene_frame_num > 0) {\n"
131" float peak = (float)*max_total_p / (REFERENCE_WHITE * scene_frame_num);\n"
132" float avg = (float)*avg_total_p / (REFERENCE_WHITE * scene_frame_num);\n"
133" r.peak = max(1.0f, peak);\n"
134" r.average = max(0.25f, avg);\n"
135" }\n"
136"\n"
137" if (lidx == 0 && lidy == 0 && atomic_add(counter_wg_p, 1) == num_wg - 1) {\n"
138" *counter_wg_p = 0;\n"
139" avg_buf[frame_idx] /= num_wg;\n"
140"\n"
141" if (scene_threshold > 0.0f) {\n"
142" uint cur_max = peak_buf[frame_idx];\n"
143" uint cur_avg = avg_buf[frame_idx];\n"
144" int diff = (int)(scene_frame_num * cur_avg) - (int)*avg_total_p;\n"
145"\n"
146" if (abs(diff) > scene_frame_num * scene_threshold * REFERENCE_WHITE) {\n"
147" for (uint i = 0; i < DETECTION_FRAMES + 1; i++)\n"
148" avg_buf[i] = 0;\n"
149" for (uint i = 0; i < DETECTION_FRAMES + 1; i++)\n"
150" peak_buf[i] = 0;\n"
151" *avg_total_p = *max_total_p = 0;\n"
152" *scene_frame_num_p = 0;\n"
153" avg_buf[frame_idx] = cur_avg;\n"
154" peak_buf[frame_idx] = cur_max;\n"
155" }\n"
156" }\n"
157" uint next = (frame_idx + 1) % (DETECTION_FRAMES + 1);\n"
158" // add current frame, subtract next frame\n"
159" *max_total_p += peak_buf[frame_idx] - peak_buf[next];\n"
160" *avg_total_p += avg_buf[frame_idx] - avg_buf[next];\n"
161" // reset next frame\n"
162" peak_buf[next] = avg_buf[next] = 0;\n"
163" *frame_idx_p = next;\n"
164" *scene_frame_num_p = min(*scene_frame_num_p + 1,\n"
165" (uint)DETECTION_FRAMES);\n"
166" }\n"
167" return r;\n"
168"}\n"
169"\n"
170"float3 map_one_pixel_rgb(float3 rgb, float peak, float average) {\n"
171" float sig = max(max(rgb.x, max(rgb.y, rgb.z)), 1e-6f);\n"
172"\n"
173" // Rescale the variables in order to bring it into a representation where\n"
174" // 1.0 represents the dst_peak. This is because all of the tone mapping\n"
175" // algorithms are defined in such a way that they map to the range [0.0, 1.0].\n"
176" if (target_peak > 1.0f) {\n"
177" sig *= 1.0f / target_peak;\n"
178" peak *= 1.0f / target_peak;\n"
179" }\n"
180"\n"
181" float sig_old = sig;\n"
182"\n"
183" // Scale the signal to compensate for differences in the average brightness\n"
184" float slope = min(1.0f, sdr_avg / average);\n"
185" sig *= slope;\n"
186" peak *= slope;\n"
187"\n"
188" // Desaturate the color using a coefficient dependent on the signal level\n"
189" if (desat_param > 0.0f) {\n"
190" float luma = get_luma_dst(rgb);\n"
191" float coeff = max(sig - 0.18f, 1e-6f) / max(sig, 1e-6f);\n"
192" coeff = native_powr(coeff, 10.0f / desat_param);\n"
193" rgb = mix(rgb, (float3)luma, (float3)coeff);\n"
194" sig = mix(sig, luma * slope, coeff);\n"
195" }\n"
196"\n"
197" sig = TONE_FUNC(sig, peak);\n"
198"\n"
199" sig = min(sig, 1.0f);\n"
200" rgb *= (sig/sig_old);\n"
201" return rgb;\n"
202"}\n"
203"// map from source space YUV to destination space RGB\n"
204"float3 map_to_dst_space_from_yuv(float3 yuv, float peak) {\n"
205" float3 c = yuv2lrgb(yuv);\n"
206" c = ootf(c, peak);\n"
207" c = lrgb2lrgb(c);\n"
208" return c;\n"
209"}\n"
210"\n"
211"__kernel void tonemap(__write_only image2d_t dst1,\n"
212" __read_only image2d_t src1,\n"
213" __write_only image2d_t dst2,\n"
214" __read_only image2d_t src2,\n"
215" global uint *util_buf,\n"
216" float peak\n"
217" )\n"
218"{\n"
219" __local uint sum_wg;\n"
220" const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |\n"
221" CLK_ADDRESS_CLAMP_TO_EDGE |\n"
222" CLK_FILTER_NEAREST);\n"
223" int xi = get_global_id(0);\n"
224" int yi = get_global_id(1);\n"
225" // each work item process four pixels\n"
226" int x = 2 * xi;\n"
227" int y = 2 * yi;\n"
228"\n"
229" float y0 = read_imagef(src1, sampler, (int2)(x, y)).x;\n"
230" float y1 = read_imagef(src1, sampler, (int2)(x + 1, y)).x;\n"
231" float y2 = read_imagef(src1, sampler, (int2)(x, y + 1)).x;\n"
232" float y3 = read_imagef(src1, sampler, (int2)(x + 1, y + 1)).x;\n"
233" float2 uv = read_imagef(src2, sampler, (int2)(xi, yi)).xy;\n"
234"\n"
235" float3 c0 = map_to_dst_space_from_yuv((float3)(y0, uv.x, uv.y), peak);\n"
236" float3 c1 = map_to_dst_space_from_yuv((float3)(y1, uv.x, uv.y), peak);\n"
237" float3 c2 = map_to_dst_space_from_yuv((float3)(y2, uv.x, uv.y), peak);\n"
238" float3 c3 = map_to_dst_space_from_yuv((float3)(y3, uv.x, uv.y), peak);\n"
239"\n"
240" float sig0 = max(c0.x, max(c0.y, c0.z));\n"
241" float sig1 = max(c1.x, max(c1.y, c1.z));\n"
242" float sig2 = max(c2.x, max(c2.y, c2.z));\n"
243" float sig3 = max(c3.x, max(c3.y, c3.z));\n"
244" float sig = max(sig0, max(sig1, max(sig2, sig3)));\n"
245"\n"
246" struct detection_result r = detect_peak_avg(util_buf, &sum_wg, sig, peak);\n"
247"\n"
248" float3 c0_old = c0, c1_old = c1, c2_old = c2;\n"
249" c0 = map_one_pixel_rgb(c0, r.peak, r.average);\n"
250" c1 = map_one_pixel_rgb(c1, r.peak, r.average);\n"
251" c2 = map_one_pixel_rgb(c2, r.peak, r.average);\n"
252" c3 = map_one_pixel_rgb(c3, r.peak, r.average);\n"
253"\n"
254" c0 = inverse_ootf(c0, target_peak);\n"
255" c1 = inverse_ootf(c1, target_peak);\n"
256" c2 = inverse_ootf(c2, target_peak);\n"
257" c3 = inverse_ootf(c3, target_peak);\n"
258"\n"
259" y0 = lrgb2y(c0);\n"
260" y1 = lrgb2y(c1);\n"
261" y2 = lrgb2y(c2);\n"
262" y3 = lrgb2y(c3);\n"
263" float3 chroma_c = get_chroma_sample(c0, c1, c2, c3);\n"
264" float3 chroma = lrgb2yuv(chroma_c);\n"
265"\n"
266" if (xi < get_image_width(dst2) && yi < get_image_height(dst2)) {\n"
267" write_imagef(dst1, (int2)(x, y), (float4)(y0, 0.0f, 0.0f, 1.0f));\n"
268" write_imagef(dst1, (int2)(x+1, y), (float4)(y1, 0.0f, 0.0f, 1.0f));\n"
269" write_imagef(dst1, (int2)(x, y+1), (float4)(y2, 0.0f, 0.0f, 1.0f));\n"
270" write_imagef(dst1, (int2)(x+1, y+1), (float4)(y3, 0.0f, 0.0f, 1.0f));\n"
271" write_imagef(dst2, (int2)(xi, yi),\n"
272" (float4)(chroma.y, chroma.z, 0.0f, 1.0f));\n"
273" }\n"
274"}\n"
275;
const char * ff_source_tonemap_cl
Definition tonemap.c:2