forked from haasn/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plconv.c
266 lines (227 loc) · 8.12 KB
/
plconv.c
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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <libplacebo/context.h>
#include <libplacebo/vulkan.h>
#include <libplacebo/dispatch.h>
#include <libplacebo/shaders/av1.h>
#include <libplacebo/shaders/sampling.h>
#include <libplacebo/shaders/colorspace.h>
#include <libplacebo/gpu.h>
int main()
{
int width, height;
char type;
if (scanf("P%c\n%d %d\n-1.0\n", &type, &width, &height) != 3 ||
toupper(type) != 'F')
{
fprintf(stderr, "Got unexpected file header? Input must be PF format\n");
return 1;
}
// basic sanitization
if (width <= 0 || height <= 0 || width > 1<<20 || height > 1<<20) {
fprintf(stderr, "Got nonsensical dimensions %dx%d!\n", width, height);
return 1;
}
struct pl_context *ctx;
ctx = pl_context_create(PL_API_VER, &(struct pl_context_params) {
.log_cb = pl_log_color,
.log_level = PL_LOG_DEBUG,
.log_priv = stderr,
});
struct pl_vulkan_params params = pl_vulkan_default_params;
const struct pl_vulkan *vk = pl_vulkan_create(ctx, ¶ms);
if (!vk) {
fprintf(stderr, "Failed initializing vulkan context\n");
return 3;
}
const struct pl_gpu *gpu = vk->gpu;
const struct pl_fmt *fmt;
fmt = pl_find_fmt(gpu, PL_FMT_FLOAT, 4, 16, 32, PL_FMT_CAP_SAMPLEABLE |
PL_FMT_CAP_RENDERABLE | PL_FMT_CAP_STORABLE);
if (!fmt) {
fprintf(stderr, "GPU does not support 32-bit RGBA textures!\n");
return 3;
}
// Compute the optimal stride, simply because we can
int stride_w = pl_optimal_transfer_stride(gpu, width);
char *buf = malloc(stride_w * height * fmt->texel_size);
if (!buf) {
fprintf(stderr, "Failed allocating memory for image buffer!\n");
return 2;
}
// Fill the buffer with the input data
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int idx = y * stride_w + x;
float *texel = (float *) &buf[idx * fmt->texel_size];
int num = 0;
switch (type) {
case 'F': num = 3; break; // RGB
case 'f': num = 1; break; // grayscale
}
if (fread(texel, sizeof(float), num, stdin) != num) {
fprintf(stderr, "Expected %d floats, got fewer!\n", num);
return 1;
}
if (type == 'f') // grayscale
texel[2] = texel[1] = texel[0];
// Explicitly clear the alpha channel
texel[3] = 1.0;
}
}
// Create the source and destination textures
const struct pl_tex *src, *dst;
src = pl_tex_create(gpu, &(struct pl_tex_params) {
.format = fmt,
.w = width,
.h = height,
.sampleable = true,
.host_writable = true,
});
dst = pl_tex_create(gpu, &(struct pl_tex_params) {
.format = fmt,
.w = width,
.h = height,
.renderable = true,
.storable = true,
.host_readable = true,
});
if (!src || !dst) {
fprintf(stderr, "Failed creating render textures!\n");
return 3;
}
// Upload the source data
struct pl_tex_transfer_params transfer = {
.tex = src,
.stride_w = stride_w,
.ptr = buf,
};
if (!pl_tex_upload(gpu, &transfer)) {
fprintf(stderr, "Failed uploading source data!\n");
return 4;
}
struct pl_dispatch *dp = pl_dispatch_create(ctx, gpu);
if (!dp)
return 3;
struct pl_shader_obj *state = NULL, *dither_state = NULL;
// Repeat this twice to force the peak detection to work
for (int i = 0; i < 2; i++) {
struct pl_shader *sh = pl_dispatch_begin(dp);
if (!sh)
return 3;
pl_shader_sample_direct(sh, &(struct pl_sample_src) {
.tex = src,
.components = 3,
});
pl_shader_decode_color(sh, &(struct pl_color_repr){
.sys = PL_COLOR_SYSTEM_RGB,
.levels = PL_COLOR_LEVELS_PC,
}, &(struct pl_color_adjustment) {
.brightness = 0.0,
.contrast = 2.0,
.saturation = 1.0,
.hue = 0.0,
.gamma = 1.0,
});
struct pl_color_map_params params = pl_color_map_default_params;
/*
struct pl_color_map_params params = {
.tone_mapping_algo = PL_TONE_MAPPING_MOBIUS,
//.tone_mapping_param = 2,
//.tone_mapping_desaturate = 0.3,
.gamut_warning = false,
.peak_detect_frames = 63,
};
*/
struct pl_color_space src_space = {
.primaries = PL_COLOR_PRIM_BT_709,
.transfer = PL_COLOR_TRC_BT_1886,
.light = PL_COLOR_LIGHT_DISPLAY,
};
/*
struct pl_color_space src_space = {
.primaries = PL_COLOR_PRIM_BT_709,
.transfer = PL_COLOR_TRC_LINEAR,
.light = PL_COLOR_LIGHT_SCENE_1_2,
.sig_peak = 537.059,
.sig_scale = 5.0,
};
*/
struct pl_color_space dst_space = pl_color_space_srgb;
//dst_space.sig_scale = 3.0;
pl_shader_color_map(sh, ¶ms,
src_space, dst_space,
&state, false);
/*
pl_shader_cone_distort(sh, pl_color_space_srgb, &(struct pl_cone_params) {
.cones = PL_CONE_L,
.strength = 0.0,
});
*/
/*
static const enum pl_channel channels[3] = {0, 0, 0};
pl_shader_av1_grain(sh, &state, channels, NULL, &(struct pl_grain_params) {
.width = width,
.height = height,
.repr = {
.levels = PL_COLOR_LEVELS_TV,
.sys = PL_COLOR_SYSTEM_BT_709,
.bits = { .color_depth = 10, .sample_depth = 10 },
},
.grain_seed = 4316,
.overlap = false,
.num_points_y = 6,
.points_y = {{0, 4}, {27, 33}, {54, 55}, {67, 61}, {108, 71}, {-1, 72}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
.chroma_scaling_from_luma = false,
.num_points_uv = {2, 2},
.points_uv = {{{0, 0}, {255, 0}}, {{0, 0}, {255, 0}}},
.scaling_shift = 11,
.ar_coeff_lag = 3,
.ar_coeffs_y = {0, 0, -1, 0, 0, 1, 1, 2, 1, -3, -8, -1, 2, 1, 4, 0, -5, -10, -4, 0, 2, 9, 1, 1},
.ar_coeffs_uv = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128},
},
.ar_coeff_shift = 6,
.grain_scale_shift = 0,
.uv_mult = {0, 0},
.uv_mult_luma = {64, 64},
.uv_offset = {0, 0},
});
pl_shader_cone_distort(sh, pl_color_space_srgb, &(struct pl_cone_params) {
.cones = PL_CONE_M,
.strength = 0.5,
});
*/
pl_shader_dither(sh, 8, &dither_state, NULL);
if (!pl_dispatch_finish(dp, &sh, dst, NULL, NULL)) {
fprintf(stderr, "Failed dispatching shader!\n");
return 4;
}
}
// Download the result data
transfer.tex = dst;
if (!pl_tex_download(gpu, &transfer)) {
fprintf(stderr, "Failed downloading result data!\n");
return 4;
}
// Write the result to stdout
printf("PF\n%d %d\n-1.0\n", width, height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int idx = y * stride_w + x;
float *texel = (float *) &buf[idx * fmt->texel_size];
//fprintf(stderr, "%f %f %f\n", texel[0], texel[1], texel[2]);
fwrite(texel, sizeof(float), 3, stdout);
}
}
// Clean up state and exit
pl_shader_obj_destroy(&state);
pl_shader_obj_destroy(&dither_state);
pl_dispatch_destroy(&dp);
pl_tex_destroy(gpu, &src);
pl_tex_destroy(gpu, &dst);
pl_vulkan_destroy(&vk);
pl_context_destroy(&ctx);
}