Debian Codegraph @

test/frei0r-run.c

raw symbols SCIP
1 /* This file is part of frei0r (https://frei0r.dyne.org)
2 *
3 * Copyright (C) 2024-2025 Dyne.org foundation
4 * designed, written and maintained by Denis Roio <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include <unistd.h>
22 #include <libgen.h>
23 #include <dlfcn.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28
29 #include <frei0r.h>
30 #include "test-pattern.h"
31
32 #if defined(__unix__) && defined(GUI)
33 #pragma message "Compiling GUI test"
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #endif
37
38 // frei0r function prototypes
39 typedef int (*f0r_init_f)(void);
40 typedef void (*f0r_deinit_f)(void);
41 typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
42 typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
43 typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
44 typedef void (*f0r_update_f)(f0r_instance_t instance,
45 double time, const uint32_t* inframe, uint32_t* outframe);
46 typedef void (*f0r_update2_f)(f0r_instance_t instance, double time,
47 const uint32_t* inframe1, const uint32_t* inframe2,
48 const uint32_t* inframe3, uint32_t* outframe);
49 typedef void (*f0r_destruct_f)(f0r_instance_t instance);
50 typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
51 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
52
53
54 // Generate a simple color bar test pattern
55 void generate_test_pattern(uint32_t* frame, int width, int height, int color_model, int pattern_variant) {
56 // Create color bars: red, green, blue, white, black, cyan, magenta, yellow
57 int bar_width = width / 8;
58
59 for (int y = 0; y < height; y++) {
60 for (int x = 0; x < width; x++) {
61 int bar_index = x / bar_width;
62 if (bar_index >= 8) bar_index = 7;
63
64 // Rotate pattern based on variant
65 if (pattern_variant == 1) {
66 // Vertical bars instead
67 bar_index = y / (height / 8);
68 if (bar_index >= 8) bar_index = 7;
69 } else if (pattern_variant == 2) {
70 // Checkerboard
71 bar_index = ((x / bar_width) + (y / (height / 8))) % 8;
72 }
73
74 uint8_t r, g, b, a = 255;
75
76 switch (bar_index) {
77 case 0: // Red
78 r = 255; g = 0; b = 0;
79 break;
80 case 1: // Green
81 r = 0; g = 255; b = 0;
82 break;
83 case 2: // Blue
84 r = 0; g = 0; b = 255;
85 break;
86 case 3: // White
87 r = 255; g = 255; b = 255;
88 break;
89 case 4: // Black
90 r = 0; g = 0; b = 0;
91 break;
92 case 5: // Cyan
93 r = 0; g = 255; b = 255;
94 break;
95 case 6: // Magenta
96 r = 255; g = 0; b = 255;
97 break;
98 case 7: // Yellow
99 r = 255; g = 255; b = 0;
100 break;
101 default:
102 r = 128; g = 128; b = 128;
103 break;
104 }
105
106 // Add some vertical variation for visual interest
107 if (y < height / 4) {
108 // Top quarter: keep original colors
109 } else if (y < height / 2) {
110 // Second quarter: darker
111 r = r * 0.7;
112 g = g * 0.7;
113 b = b * 0.7;
114 } else if (y < 3 * height / 4) {
115 // Third quarter: even darker
116 r = r * 0.4;
117 g = g * 0.4;
118 b = b * 0.4;
119 } else {
120 // Bottom quarter: much darker
121 r = r * 0.2;
122 g = g * 0.2;
123 b = b * 0.2;
124 }
125
126 if (color_model == F0R_COLOR_MODEL_BGRA8888) {
127 frame[y * width + x] = (a << 24) | (r << 16) | (g << 8) | b;
128 } else {
129 frame[y * width + x] = (a << 24) | (b << 16) | (g << 8) | r;
130 }
131 }
132 }
133 }
134
135 // Test parameters by cycling through different values
136 void test_parameters(f0r_instance_t instance, f0r_set_param_value_f f0r_set_param_value,
137 f0r_get_param_info_f f0r_get_param_info, int num_params, int frame_count) {
138 f0r_param_info_t param_info;
139 double double_val;
140 f0r_param_color_t color_val;
141 f0r_param_position_t position_val;
142
143 for (int i = 0; i < num_params; i++) {
144 f0r_get_param_info(&param_info, i);
145
146 switch (param_info.type) {
147 case F0R_PARAM_BOOL:
148 // Alternate between 0.0 and 1.0 every 30 frames
149 double_val = (frame_count / 30) % 2;
150 f0r_set_param_value(instance, (f0r_param_t)&double_val, i);
151 break;
152
153 case F0R_PARAM_DOUBLE:
154 // Cycle through values 0.0 to 1.0 over 60 frames
155 double_val = (frame_count % 60) / 60.0;
156 f0r_set_param_value(instance, (f0r_param_t)&double_val, i);
157 break;
158
159 case F0R_PARAM_COLOR:
160 // Cycle through different colors
161 switch ((frame_count / 20) % 4) {
162 case 0: // Red
163 color_val.r = 1.0; color_val.g = 0.0; color_val.b = 0.0;
164 break;
165 case 1: // Green
166 color_val.r = 0.0; color_val.g = 1.0; color_val.b = 0.0;
167 break;
168 case 2: // Blue
169 color_val.r = 0.0; color_val.g = 0.0; color_val.b = 1.0;
170 break;
171 case 3: // White
172 color_val.r = 1.0; color_val.g = 1.0; color_val.b = 1.0;
173 break;
174 }
175 f0r_set_param_value(instance, (f0r_param_t)&color_val, i);
176 break;
177
178 case F0R_PARAM_POSITION:
179 // Move position in a circular pattern
180 position_val.x = 0.5 + 0.4 * sin(frame_count * 0.1);
181 position_val.y = 0.5 + 0.4 * cos(frame_count * 0.1);
182 f0r_set_param_value(instance, (f0r_param_t)&position_val, i);
183 break;
184
185 case F0R_PARAM_STRING:
186 {
187 // For string parameters, use "0" as default
188 char *string_val = "0";
189 f0r_set_param_value(instance, (f0r_param_t)&string_val, i);
190 break;
191 }
192
193 default:
194 // For unknown parameter types, set to middle value
195 double_val = 0.5;
196 f0r_set_param_value(instance, (f0r_param_t)&double_val, i);
197 break;
198 }
199 }
200 }
201
202 int main(int argc, char* argv[]) {
203 // instance frei0r pointers
204 static void *dl_handle;
205 static f0r_init_f f0r_init;
206 static f0r_deinit_f f0r_deinit;
207 static f0r_plugin_info_t pi;
208 static f0r_get_plugin_info_f f0r_get_plugin_info;
209 static f0r_get_param_info_f f0r_get_param_info;
210 static f0r_param_info_t param;
211 static f0r_instance_t instance;
212 static f0r_construct_f f0r_construct;
213 static f0r_update_f f0r_update;
214 static f0r_destruct_f f0r_destruct;
215 static f0r_set_param_value_f f0r_set_param_value;
216 static f0r_get_param_value_f f0r_get_param_value;
217
218 const char *usage = "Usage: frei0r-run [-tdg] [-f frames] -p <frei0r_plugin_file>\n"
219 " -d debug mode\n"
220 " -g graphical display mode (Linux/WSL)\n"
221 " -f frames number of frames to process (default: 100)\n"
222 " -p plugin path to frei0r plugin file";
223 if (argc < 2) {
224 fprintf(stderr,"%s\n",usage);
225 return -1;
226 }
227
228 int opt;
229 int graphical = 0;
230 int debug = 0;
231 int frames = 100; // Number of frames to test
232 char plugin_file[512];
233 plugin_file[0] = '\0';
234 while((opt = getopt(argc, argv, "tdgf:p:")) != -1) {
235 switch(opt) {
236 case 'd':
237 debug = 1;
238 break;
239 case 'g':
240 graphical = 1;
241 break;
242 case 'f':
243 frames = atoi(optarg);
244 break;
245 case 'p':
246 snprintf(plugin_file, 511, "%s", optarg);
247 break;
248 }
249 }
250
251 if (plugin_file[0] == '\0') {
252 fprintf(stderr, "Error: plugin file required (-p option)\n%s\n", usage);
253 return -1;
254 }
255
256 // Set fixed video properties for test pattern
257 int frame_width = 640;
258 int frame_height = 480;
259 int fps = 30;
260
261 const char *file = basename(plugin_file);
262 const char *dir = dirname(plugin_file);
263 char path[256];;
264 snprintf(path, 255,"%s/%s",dir,file);
265 // fprintf(stderr,"%s %s\n",argv[0], file);
266 // load shared library
267 dl_handle = dlopen(path, RTLD_NOW|RTLD_LOCAL);
268 if(!dl_handle) {
269 fprintf(stderr,"error: %s\n",dlerror());
270 exit(1);
271 }
272 // get plugin function calls
273 f0r_init = (f0r_init_f) dlsym(dl_handle,"f0r_init");
274 f0r_deinit = (f0r_deinit_f) dlsym(dl_handle,"f0r_deinit");
275 f0r_get_plugin_info = (f0r_get_plugin_info_f) dlsym(dl_handle,"f0r_get_plugin_info");
276 f0r_get_param_info = (f0r_get_param_info_f) dlsym(dl_handle,"f0r_get_param_info");
277 f0r_construct = (f0r_construct_f) dlsym(dl_handle,"f0r_construct");
278 f0r_update = (f0r_update_f) dlsym(dl_handle,"f0r_update");
279 f0r_destruct = (f0r_destruct_f) dlsym(dl_handle,"f0r_destruct");
280 f0r_set_param_value = (f0r_set_param_value_f) dlsym(dl_handle,"f0r_set_param_value");
281 f0r_get_param_value = (f0r_get_param_value_f) dlsym(dl_handle,"f0r_get_param_value");
282
283 // always initialize plugin first
284 f0r_init();
285 // get info about plugin
286 f0r_get_plugin_info(&pi);
287 const char *frei0r_color_model = (pi.color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
288 pi.color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
289 pi.color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown");
290
291 if(debug) {
292 fprintf(stderr,"{\n \"name\":\"%s\",\n \"type\":\"%s\",\n \"color_model\":\"%s\",\n \"num_params\":\"%d\"\n}",
293 pi.name,
294 pi.plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
295 pi.plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
296 pi.plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
297 pi.plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown",
298 frei0r_color_model,
299 pi.num_params);
300 // Print parameter information
301 if (pi.num_params > 0) {
302 fprintf(stderr,",\n \"parameters\":[\n");
303 for (int i = 0; i < pi.num_params; i++) {
304 f0r_get_param_info(&param, i);
305 const char* param_type =
306 param.type == F0R_PARAM_BOOL ? "bool" :
307 param.type == F0R_PARAM_DOUBLE ? "double" :
308 param.type == F0R_PARAM_COLOR ? "color" :
309 param.type == F0R_PARAM_POSITION ? "position" :
310 param.type == F0R_PARAM_STRING ? "string" : "unknown";
311 fprintf(stderr," {\"name\":\"%s\",\"type\":\"%s\",\"explanation\":\"%s\"}",
312 param.name, param_type, param.explanation);
313 if (i < pi.num_params - 1) fprintf(stderr,",\n");
314 }
315 fprintf(stderr,"\n ]\n");
316 }
317 fprintf(stderr,"}\n");
318 }
319
320 instance = f0r_construct(frame_width, frame_height);
321
322 uint32_t *input_buffer = NULL;
323 uint32_t *input_buffer2 = NULL;
324 uint32_t *input_buffer3 = NULL;
325 uint32_t *output_buffer;
326
327 // Allocate buffers based on plugin type
328 if (pi.plugin_type == F0R_PLUGIN_TYPE_FILTER) {
329 input_buffer = (uint32_t*)calloc(4, frame_width * frame_height);
330 } else if (pi.plugin_type == F0R_PLUGIN_TYPE_MIXER2) {
331 input_buffer = (uint32_t*)calloc(4, frame_width * frame_height);
332 input_buffer2 = (uint32_t*)calloc(4, frame_width * frame_height);
333 } else if (pi.plugin_type == F0R_PLUGIN_TYPE_MIXER3) {
334 input_buffer = (uint32_t*)calloc(4, frame_width * frame_height);
335 input_buffer2 = (uint32_t*)calloc(4, frame_width * frame_height);
336 input_buffer3 = (uint32_t*)calloc(4, frame_width * frame_height);
337 }
338 // SOURCE type needs no input buffer
339
340 output_buffer = (uint32_t*)calloc(4, frame_width * frame_height);
341
342 #if defined(GUI)
343 // Generate initial test patterns
344 if (input_buffer)
345 generate_animated_test_pattern(input_buffer, frame_width, frame_height, 0, pi.color_model);
346 if (input_buffer2)
347 generate_animated_test_pattern(input_buffer2, frame_width, frame_height, 0, pi.color_model);
348 if (input_buffer3)
349 generate_animated_test_pattern(input_buffer3, frame_width, frame_height, 0, pi.color_model);
350 #else
351 if (input_buffer)
352 generate_test_pattern(input_buffer, frame_width, frame_height, pi.color_model, 0);
353 if (input_buffer2)
354 generate_test_pattern(input_buffer2, frame_width, frame_height, pi.color_model, 1);
355 if (input_buffer3)
356 generate_test_pattern(input_buffer3, frame_width, frame_height, pi.color_model, 2);
357 #endif
358
359 #if defined(__unix__) && defined(GUI)
360 Display *display = NULL;
361 Window window;
362 GC gc;
363 XImage *ximage = NULL;
364
365 if (graphical) {
366 display = XOpenDisplay(NULL);
367 if (!display) {
368 fprintf(stderr, "Warning: Cannot open X display, falling back to headless mode\n");
369 graphical = 0;
370 } else {
371 int screen = DefaultScreen(display);
372 window = XCreateSimpleWindow(display, RootWindow(display, screen),
373 0, 0, frame_width, frame_height, 1,
374 BlackPixel(display, screen),
375 WhitePixel(display, screen));
376
377 XStoreName(display, window, pi.name);
378 XSelectInput(display, window, ExposureMask | KeyPressMask);
379 XMapWindow(display, window);
380 gc = XCreateGC(display, window, 0, NULL);
381
382 Visual *visual = DefaultVisual(display, screen);
383 ximage = XCreateImage(display, visual, 24, ZPixmap, 0,
384 (char*)output_buffer, frame_width, frame_height, 32, 0);
385
386 XFlush(display);
387 }
388 }
389 #endif
390
391 // Load f0r_update2 for mixers
392 f0r_update2_f f0r_update2 = NULL;
393 if (pi.plugin_type == F0R_PLUGIN_TYPE_MIXER2 || pi.plugin_type == F0R_PLUGIN_TYPE_MIXER3) {
394 f0r_update2 = (f0r_update2_f)dlsym(dl_handle, "f0r_update2");
395 if (!f0r_update2) {
396 fprintf(stderr, "Error: Cannot load f0r_update2 for mixer plugin\n");
397 if (input_buffer) free(input_buffer);
398 if (input_buffer2) free(input_buffer2);
399 if (input_buffer3) free(input_buffer3);
400 free(output_buffer);
401 f0r_destruct(instance);
402 f0r_deinit();
403 dlclose(dl_handle);
404 return 1;
405 }
406 }
407
408 // Test the plugin with different parameter values
409 for (int frame = 0; frame < frames; frame++) {
410 #if defined(GUI)
411 // Generate animated test patterns for this frame
412 if (input_buffer)
413 generate_animated_test_pattern(input_buffer, frame_width, frame_height, frame, pi.color_model);
414 if (input_buffer2)
415 generate_animated_test_pattern(input_buffer2, frame_width, frame_height, frame + 10, pi.color_model);
416 if (input_buffer3)
417 generate_animated_test_pattern(input_buffer3, frame_width, frame_height, frame + 20, pi.color_model);
418 #else
419 if (input_buffer)
420 generate_test_pattern(input_buffer, frame_width, frame_height, pi.color_model, 0);
421 if (input_buffer2)
422 generate_test_pattern(input_buffer2, frame_width, frame_height, pi.color_model, 1);
423 if (input_buffer3)
424 generate_test_pattern(input_buffer3, frame_width, frame_height, pi.color_model, 2);
425 #endif
426
427 // Update parameters if the plugin has any
428 if (pi.num_params > 0 && f0r_set_param_value) {
429 test_parameters(instance, f0r_set_param_value, f0r_get_param_info, pi.num_params, frame);
430 }
431
432 // Apply plugin based on type
433 double time = (double)frame / (double)fps;
434
435 switch (pi.plugin_type) {
436 case F0R_PLUGIN_TYPE_SOURCE:
437 f0r_update(instance, time, NULL, output_buffer);
438 break;
439 case F0R_PLUGIN_TYPE_FILTER:
440 f0r_update(instance, time, (const uint32_t*)input_buffer, output_buffer);
441 break;
442 case F0R_PLUGIN_TYPE_MIXER2:
443 f0r_update2(instance, time, (const uint32_t*)input_buffer,
444 (const uint32_t*)input_buffer2, NULL, output_buffer);
445 break;
446 case F0R_PLUGIN_TYPE_MIXER3:
447 f0r_update2(instance, time, (const uint32_t*)input_buffer,
448 (const uint32_t*)input_buffer2, (const uint32_t*)input_buffer3,
449 output_buffer);
450 break;
451 default:
452 fprintf(stderr, "Unknown plugin type: %d\n", pi.plugin_type);
453 break;
454 }
455
456 #if defined(__unix__) && defined(GUI)
457 if (graphical && display) {
458 ximage->data = (char*)output_buffer;
459 XPutImage(display, window, gc, ximage, 0, 0, 0, 0, frame_width, frame_height);
460 XFlush(display);
461
462 // Check for key press to exit early
463 while (XPending(display)) {
464 XEvent event;
465 XNextEvent(display, &event);
466 if (event.type == KeyPress) {
467 fprintf(stderr, "\nInterrupted by user at frame %d\n", frame);
468 frame = frames; // Exit loop
469 break;
470 }
471 }
472
473 usleep(1000000 / fps); // Frame delay
474 }
475 #endif
476
477 if (!graphical && frame % 10 == 0 && debug) {
478 printf("Frame %d processed\n", frame);
479 }
480 }
481
482 #if defined(__unix__) && defined(GUI)
483 if (graphical && display) {
484 ximage->data = NULL; // Prevent XDestroyImage from freeing our buffer
485 XDestroyImage(ximage);
486 XFreeGC(display, gc);
487 XDestroyWindow(display, window);
488 XCloseDisplay(display);
489 }
490 #endif
491
492 if(debug) {
493 const char *plugin_type_name = "unknown";
494 switch (pi.plugin_type) {
495 case F0R_PLUGIN_TYPE_SOURCE: plugin_type_name = "source"; break;
496 case F0R_PLUGIN_TYPE_FILTER: plugin_type_name = "filter"; break;
497 case F0R_PLUGIN_TYPE_MIXER2: plugin_type_name = "mixer2"; break;
498 case F0R_PLUGIN_TYPE_MIXER3: plugin_type_name = "mixer3"; break;
499 }
500 printf("Test completed successfully. Plugin: %s (type: %s)\n", pi.name, plugin_type_name);
501 printf("Tested %d frames with %d parameters\n", frames, pi.num_params);
502 if (pi.plugin_type != F0R_PLUGIN_TYPE_SOURCE) {
503 printf("Input: %dx%d test pattern(s)\n", frame_width, frame_height);
504 }
505 printf("Output: %dx%d processed frames\n", frame_width, frame_height);
506 }
507
508 if (input_buffer) free(input_buffer);
509 if (input_buffer2) free(input_buffer2);
510 if (input_buffer3) free(input_buffer3);
511 free(output_buffer);
512
513 f0r_destruct(instance);
514 f0r_deinit();
515
516 dlclose(dl_handle);
517
518 return 0;
519 }