-
Notifications
You must be signed in to change notification settings - Fork 2
/
CfL_Prediction.glsl
354 lines (297 loc) · 12.1 KB
/
CfL_Prediction.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// MIT License
// Copyright (c) 2023 João Chrisóstomo
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//!PARAM chroma_offset_x
//!TYPE float
0.0
//!PARAM chroma_offset_y
//!TYPE float
0.0
//!HOOK CHROMA
//!BIND LUMA
//!BIND CHROMA
//!SAVE LUMA_LR
//!WIDTH CHROMA.w
//!HEIGHT LUMA.h
//!WHEN CHROMA.w LUMA.w <
//!DESC Chroma From Luma Prediction (Hermite 1st step, Downscaling Luma)
float comp_wd(vec2 v) {
float x = min(length(v), 1.0);
return smoothstep(0.0, 1.0, 1.0 - x);
}
vec4 hook() {
vec2 luma_pos = LUMA_pos;
luma_pos.x += chroma_offset_x / LUMA_size.x;
float start = ceil((luma_pos.x - (1.0 / CHROMA_size.x)) * LUMA_size.x - 0.5);
float end = floor((luma_pos.x + (1.0 / CHROMA_size.x)) * LUMA_size.x - 0.5);
float wt = 0.0;
float luma_sum = 0.0;
vec2 pos = luma_pos;
for (float dx = start.x; dx <= end.x; dx++) {
pos.x = LUMA_pt.x * (dx + 0.5);
vec2 dist = (pos - luma_pos) * CHROMA_size;
float wd = comp_wd(dist);
float luma_pix = LUMA_tex(pos).x;
luma_sum += wd * luma_pix;
wt += wd;
}
vec4 output_pix = vec4(luma_sum /= wt, 0.0, 0.0, 1.0);
return clamp(output_pix, 0.0, 1.0);
}
//!HOOK CHROMA
//!BIND LUMA_LR
//!BIND CHROMA
//!BIND LUMA
//!SAVE LUMA_LR
//!WIDTH CHROMA.w
//!HEIGHT CHROMA.h
//!WHEN CHROMA.w LUMA.w <
//!DESC Chroma From Luma Prediction (Hermite 2nd step, Downscaling Luma)
float comp_wd(vec2 v) {
float x = min(length(v), 1.0);
return smoothstep(0.0, 1.0, 1.0 - x);
}
vec4 hook() {
vec2 luma_pos = LUMA_LR_pos;
luma_pos.y += chroma_offset_y / LUMA_LR_size.y;
float start = ceil((luma_pos.y - (1.0 / CHROMA_size.y)) * LUMA_LR_size.y - 0.5);
float end = floor((luma_pos.y + (1.0 / CHROMA_size.y)) * LUMA_LR_size.y - 0.5);
float wt = 0.0;
float luma_sum = 0.0;
vec2 pos = luma_pos;
for (float dy = start; dy <= end; dy++) {
pos.y = LUMA_LR_pt.y * (dy + 0.5);
vec2 dist = (pos - luma_pos) * CHROMA_size;
float wd = comp_wd(dist);
float luma_pix = LUMA_LR_tex(pos).x;
luma_sum += wd * luma_pix;
wt += wd;
}
vec4 output_pix = vec4(luma_sum /= wt, 0.0, 0.0, 1.0);
return clamp(output_pix, 0.0, 1.0);
}
//!HOOK CHROMA
//!BIND HOOKED
//!BIND LUMA
//!BIND LUMA_LR
//!WHEN CHROMA.w LUMA.w <
//!WIDTH LUMA.w
//!HEIGHT LUMA.h
//!OFFSET ALIGN
//!DESC Chroma From Luma Prediction (Upscaling Chroma)
#define USE_12_TAP_REGRESSION 1
#define USE_8_TAP_REGRESSIONS 1
#define DEBUG 0
float comp_wd(vec2 v) {
float d = min(length(v), 2.0);
float d2 = d * d;
float d3 = d2 * d;
if (d < 1.0) {
return 1.25 * d3 - 2.25 * d2 + 1.0;
} else {
return -0.75 * d3 + 3.75 * d2 - 6.0 * d + 3.0;
}
}
vec4 hook() {
float ar_strength = 0.8;
vec2 mix_coeff = vec2(0.8);
vec2 corr_exponent = vec2(4.0);
vec4 output_pix = vec4(0.0, 0.0, 0.0, 1.0);
float luma_zero = LUMA_texOff(0.0).x;
vec2 pp = HOOKED_pos * HOOKED_size - vec2(0.5);
vec2 fp = floor(pp);
pp -= fp;
#ifdef HOOKED_gather
vec2 quad_idx[4] = {{0.0, 0.0}, {2.0, 0.0}, {0.0, 2.0}, {2.0, 2.0}};
vec4 luma_quads[4];
vec4 chroma_quads[4][2];
for (int i = 0; i < 4; i++) {
luma_quads[i] = LUMA_LR_gather(vec2((fp + quad_idx[i]) * HOOKED_pt), 0);
chroma_quads[i][0] = HOOKED_gather(vec2((fp + quad_idx[i]) * HOOKED_pt), 0);
chroma_quads[i][1] = HOOKED_gather(vec2((fp + quad_idx[i]) * HOOKED_pt), 1);
}
vec2 chroma_pixels[16];
chroma_pixels[0] = vec2(chroma_quads[0][0].w, chroma_quads[0][1].w);
chroma_pixels[1] = vec2(chroma_quads[0][0].z, chroma_quads[0][1].z);
chroma_pixels[2] = vec2(chroma_quads[1][0].w, chroma_quads[1][1].w);
chroma_pixels[3] = vec2(chroma_quads[1][0].z, chroma_quads[1][1].z);
chroma_pixels[4] = vec2(chroma_quads[0][0].x, chroma_quads[0][1].x);
chroma_pixels[5] = vec2(chroma_quads[0][0].y, chroma_quads[0][1].y);
chroma_pixels[6] = vec2(chroma_quads[1][0].x, chroma_quads[1][1].x);
chroma_pixels[7] = vec2(chroma_quads[1][0].y, chroma_quads[1][1].y);
chroma_pixels[8] = vec2(chroma_quads[2][0].w, chroma_quads[2][1].w);
chroma_pixels[9] = vec2(chroma_quads[2][0].z, chroma_quads[2][1].z);
chroma_pixels[10] = vec2(chroma_quads[3][0].w, chroma_quads[3][1].w);
chroma_pixels[11] = vec2(chroma_quads[3][0].z, chroma_quads[3][1].z);
chroma_pixels[12] = vec2(chroma_quads[2][0].x, chroma_quads[2][1].x);
chroma_pixels[13] = vec2(chroma_quads[2][0].y, chroma_quads[2][1].y);
chroma_pixels[14] = vec2(chroma_quads[3][0].x, chroma_quads[3][1].x);
chroma_pixels[15] = vec2(chroma_quads[3][0].y, chroma_quads[3][1].y);
float luma_pixels[16];
luma_pixels[0] = luma_quads[0].w;
luma_pixels[1] = luma_quads[0].z;
luma_pixels[2] = luma_quads[1].w;
luma_pixels[3] = luma_quads[1].z;
luma_pixels[4] = luma_quads[0].x;
luma_pixels[5] = luma_quads[0].y;
luma_pixels[6] = luma_quads[1].x;
luma_pixels[7] = luma_quads[1].y;
luma_pixels[8] = luma_quads[2].w;
luma_pixels[9] = luma_quads[2].z;
luma_pixels[10] = luma_quads[3].w;
luma_pixels[11] = luma_quads[3].z;
luma_pixels[12] = luma_quads[2].x;
luma_pixels[13] = luma_quads[2].y;
luma_pixels[14] = luma_quads[3].x;
luma_pixels[15] = luma_quads[3].y;
#else
vec2 pix_idx[16] = {{-0.5,-0.5}, {0.5,-0.5}, {1.5,-0.5}, {2.5,-0.5},
{-0.5, 0.5}, {0.5, 0.5}, {1.5, 0.5}, {2.5, 0.5},
{-0.5, 1.5}, {0.5, 1.5}, {1.5, 1.5}, {2.5, 1.5},
{-0.5, 2.5}, {0.5, 2.5}, {1.5, 2.5}, {2.5, 2.5}};
float luma_pixels[16];
vec2 chroma_pixels[16];
for (int i = 0; i < 16; i++) {
luma_pixels[i] = LUMA_LR_tex(vec2((fp + pix_idx[i]) * HOOKED_pt)).x;
chroma_pixels[i] = HOOKED_tex(vec2((fp + pix_idx[i]) * HOOKED_pt)).xy;
}
#endif
#if (DEBUG == 1)
vec2 chroma_spatial = vec2(0.5);
mix_coeff = vec2(1.0);
#else
float wd[16];
float wt = 0.0;
vec2 ct = vec2(0.0);
vec2 chroma_min = min(min(min(chroma_pixels[5], chroma_pixels[6]), chroma_pixels[9]), chroma_pixels[10]);
vec2 chroma_max = max(max(max(chroma_pixels[5], chroma_pixels[6]), chroma_pixels[9]), chroma_pixels[10]);
const int dx[16] = {-1, 0, 1, 2, -1, 0, 1, 2, -1, 0, 1, 2, -1, 0, 1, 2};
const int dy[16] = {-1, -1, -1, -1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2};
for (int i = 0; i < 16; i++) {
wd[i] = comp_wd(vec2(dx[i], dy[i]) - pp);
wt += wd[i];
ct += wd[i] * chroma_pixels[i];
}
vec2 chroma_spatial = ct / wt;
chroma_spatial = clamp(mix(chroma_spatial, clamp(chroma_spatial, chroma_min, chroma_max), ar_strength), 0.0, 1.0);
#endif
#if (USE_12_TAP_REGRESSION == 1 || USE_8_TAP_REGRESSIONS == 1)
const int i12[12] = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14};
const int i4y[4] = {1, 2, 13, 14};
const int i4x[4] = {4, 7, 8, 11};
const int i4[4] = {5, 6, 9, 10};
float luma_sum_4 = 0.0;
float luma_sum_4y = 0.0;
float luma_sum_4x = 0.0;
vec2 chroma_sum_4 = vec2(0.0);
vec2 chroma_sum_4y = vec2(0.0);
vec2 chroma_sum_4x = vec2(0.0);
for (int i = 0; i < 4; i++) {
luma_sum_4 += luma_pixels[i4[i]];
luma_sum_4y += luma_pixels[i4y[i]];
luma_sum_4x += luma_pixels[i4x[i]];
chroma_sum_4 += chroma_pixels[i4[i]];
chroma_sum_4y += chroma_pixels[i4y[i]];
chroma_sum_4x += chroma_pixels[i4x[i]];
}
float luma_avg_12 = (luma_sum_4 + luma_sum_4y + luma_sum_4x) / 12.0;
float luma_var_12 = 0.0;
vec2 chroma_avg_12 = (chroma_sum_4 + chroma_sum_4y + chroma_sum_4x) / 12.0;
vec2 chroma_var_12 = vec2(0.0);
vec2 luma_chroma_cov_12 = vec2(0.0);
for (int i = 0; i < 12; i++) {
luma_var_12 += pow(luma_pixels[i12[i]] - luma_avg_12, 2.0);
chroma_var_12 += pow(chroma_pixels[i12[i]] - chroma_avg_12, vec2(2.0));
luma_chroma_cov_12 += (luma_pixels[i12[i]] - luma_avg_12) * (chroma_pixels[i12[i]] - chroma_avg_12);
}
vec2 corr = clamp(abs(luma_chroma_cov_12 / max(sqrt(luma_var_12 * chroma_var_12), 1e-6)), 0.0, 1.0);
mix_coeff = pow(corr, corr_exponent) * mix_coeff;
#endif
#if (USE_12_TAP_REGRESSION == 1)
vec2 alpha_12 = luma_chroma_cov_12 / max(luma_var_12, 1e-6);
vec2 beta_12 = chroma_avg_12 - alpha_12 * luma_avg_12;
vec2 chroma_pred_12 = clamp(alpha_12 * luma_zero + beta_12, 0.0, 1.0);
#endif
#if (USE_8_TAP_REGRESSIONS == 1)
const int i8y[8] = {1, 2, 5, 6, 9, 10, 13, 14};
const int i8x[8] = {4, 5, 6, 7, 8, 9, 10, 11};
float luma_avg_8y = (luma_sum_4 + luma_sum_4y) / 8.0;
float luma_avg_8x = (luma_sum_4 + luma_sum_4x) / 8.0;
float luma_var_8y = 0.0;
float luma_var_8x = 0.0;
vec2 chroma_avg_8y = (chroma_sum_4 + chroma_sum_4y) / 8.0;
vec2 chroma_avg_8x = (chroma_sum_4 + chroma_sum_4x) / 8.0;
vec2 luma_chroma_cov_8y = vec2(0.0);
vec2 luma_chroma_cov_8x = vec2(0.0);
for (int i = 0; i < 8; i++) {
luma_var_8y += pow(luma_pixels[i8y[i]] - luma_avg_8y, 2.0);
luma_var_8x += pow(luma_pixels[i8x[i]] - luma_avg_8x, 2.0);
luma_chroma_cov_8y += (luma_pixels[i8y[i]] - luma_avg_8y) * (chroma_pixels[i8y[i]] - chroma_avg_8y);
luma_chroma_cov_8x += (luma_pixels[i8x[i]] - luma_avg_8x) * (chroma_pixels[i8x[i]] - chroma_avg_8x);
}
vec2 alpha_8y = luma_chroma_cov_8y / max(luma_var_8y, 1e-6);
vec2 alpha_8x = luma_chroma_cov_8x / max(luma_var_8x, 1e-6);
vec2 beta_8y = chroma_avg_8y - alpha_8y * luma_avg_8y;
vec2 beta_8x = chroma_avg_8x - alpha_8x * luma_avg_8x;
vec2 chroma_pred_8y = clamp(alpha_8y * luma_zero + beta_8y, 0.0, 1.0);
vec2 chroma_pred_8x = clamp(alpha_8x * luma_zero + beta_8x, 0.0, 1.0);
vec2 chroma_pred_8 = mix(chroma_pred_8y, chroma_pred_8x, 0.5);
#endif
#if (USE_12_TAP_REGRESSION == 1 && USE_8_TAP_REGRESSIONS == 1)
output_pix.xy = mix(chroma_spatial, mix(chroma_pred_12, chroma_pred_8, 0.5), mix_coeff);
#elif (USE_12_TAP_REGRESSION == 1 && USE_8_TAP_REGRESSIONS == 0)
output_pix.xy = mix(chroma_spatial, chroma_pred_12, mix_coeff);
#elif (USE_12_TAP_REGRESSION == 0 && USE_8_TAP_REGRESSIONS == 1)
output_pix.xy = mix(chroma_spatial, chroma_pred_8, mix_coeff);
#else
output_pix.xy = chroma_spatial;
#endif
output_pix.xy = clamp(output_pix.xy, 0.0, 1.0);
return output_pix;
}
//!PARAM distance_coeff
//!TYPE float
//!MINIMUM 0.0
2.0
//!PARAM intensity_coeff
//!TYPE float
//!MINIMUM 0.0
128.0
//!HOOK CHROMA
//!BIND CHROMA
//!BIND LUMA
//!DESC Chroma From Luma Prediction (Smoothing Chroma)
float comp_w(vec2 spatial_distance, float intensity_distance) {
return max(100.0 * exp(-distance_coeff * pow(length(spatial_distance), 2.0) - intensity_coeff * pow(intensity_distance, 2.0)), 1e-32);
}
vec4 hook() {
vec4 output_pix = vec4(0.0, 0.0, 0.0, 1.0);
float luma_zero = LUMA_texOff(0).x;
float wt = 0.0;
vec2 ct = vec2(0.0);
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
vec2 chroma_pixels = CHROMA_texOff(vec2(i, j)).xy;
float luma_pixels = LUMA_texOff(vec2(i, j)).x;
float w = comp_w(vec2(i, j), luma_zero - luma_pixels);
wt += w;
ct += w * chroma_pixels;
}
}
output_pix.xy = clamp(ct / wt, 0.0, 1.0);
return output_pix;
}