Codebase list frei0r / f89ec56 test / frei0r-run.c
f89ec56

Tree @f89ec56 (Download .tar.gz)

frei0r-run.c @f89ec56raw · history · blame

  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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* This file is part of frei0r (https://frei0r.dyne.org)
 *
 * Copyright (C) 2024-2025 Dyne.org foundation
 * designed, written and maintained by Denis Roio <jaromil@dyne.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include <unistd.h>
#include <libgen.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <frei0r.h>
#include "test-pattern.h"

#if defined(__linux__) && defined(GUI)
#pragma message "Compiling GUI test"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif

// frei0r function prototypes
typedef int (*f0r_init_f)(void);
typedef void (*f0r_deinit_f)(void);
typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
typedef void (*f0r_update_f)(f0r_instance_t instance,
    double time, const uint32_t* inframe, uint32_t* outframe);
typedef void (*f0r_update2_f)(f0r_instance_t instance, double time,
    const uint32_t* inframe1, const uint32_t* inframe2,
    const uint32_t* inframe3, uint32_t* outframe);
typedef void (*f0r_destruct_f)(f0r_instance_t instance);
typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);


// Generate a simple color bar test pattern
void generate_test_pattern(uint32_t* frame, int width, int height, int color_model, int pattern_variant) {
    // Create color bars: red, green, blue, white, black, cyan, magenta, yellow
    int bar_width = width / 8;

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int bar_index = x / bar_width;
            if (bar_index >= 8) bar_index = 7;

            // Rotate pattern based on variant
            if (pattern_variant == 1) {
                // Vertical bars instead
                bar_index = y / (height / 8);
                if (bar_index >= 8) bar_index = 7;
            } else if (pattern_variant == 2) {
                // Checkerboard
                bar_index = ((x / bar_width) + (y / (height / 8))) % 8;
            }

            uint8_t r, g, b, a = 255;

            switch (bar_index) {
                case 0: // Red
                    r = 255; g = 0; b = 0;
                    break;
                case 1: // Green
                    r = 0; g = 255; b = 0;
                    break;
                case 2: // Blue
                    r = 0; g = 0; b = 255;
                    break;
                case 3: // White
                    r = 255; g = 255; b = 255;
                    break;
                case 4: // Black
                    r = 0; g = 0; b = 0;
                    break;
                case 5: // Cyan
                    r = 0; g = 255; b = 255;
                    break;
                case 6: // Magenta
                    r = 255; g = 0; b = 255;
                    break;
                case 7: // Yellow
                    r = 255; g = 255; b = 0;
                    break;
                default:
                    r = 128; g = 128; b = 128;
                    break;
            }

            // Add some vertical variation for visual interest
            if (y < height / 4) {
                // Top quarter: keep original colors
            } else if (y < height / 2) {
                // Second quarter: darker
                r = r * 0.7;
                g = g * 0.7;
                b = b * 0.7;
            } else if (y < 3 * height / 4) {
                // Third quarter: even darker
                r = r * 0.4;
                g = g * 0.4;
                b = b * 0.4;
            } else {
                // Bottom quarter: much darker
                r = r * 0.2;
                g = g * 0.2;
                b = b * 0.2;
            }

            if (color_model == F0R_COLOR_MODEL_BGRA8888) {
                frame[y * width + x] = (a << 24) | (r << 16) | (g << 8) | b;
            } else {
                frame[y * width + x] = (a << 24) | (b << 16) | (g << 8) | r;
            }
        }
    }
}

// Test parameters by cycling through different values
void test_parameters(f0r_instance_t instance, f0r_set_param_value_f f0r_set_param_value,
                     f0r_get_param_info_f f0r_get_param_info, int num_params, int frame_count) {
    f0r_param_info_t param_info;
    double double_val;
    f0r_param_color_t color_val;
    f0r_param_position_t position_val;

    for (int i = 0; i < num_params; i++) {
        f0r_get_param_info(&param_info, i);

        switch (param_info.type) {
            case F0R_PARAM_BOOL:
                // Alternate between 0.0 and 1.0 every 30 frames
                double_val = (frame_count / 30) % 2;
                f0r_set_param_value(instance, (f0r_param_t)&double_val, i);
                break;

            case F0R_PARAM_DOUBLE:
                // Cycle through values 0.0 to 1.0 over 60 frames
                double_val = (frame_count % 60) / 60.0;
                f0r_set_param_value(instance, (f0r_param_t)&double_val, i);
                break;

            case F0R_PARAM_COLOR:
                // Cycle through different colors
                switch ((frame_count / 20) % 4) {
                    case 0: // Red
                        color_val.r = 1.0; color_val.g = 0.0; color_val.b = 0.0;
                        break;
                    case 1: // Green
                        color_val.r = 0.0; color_val.g = 1.0; color_val.b = 0.0;
                        break;
                    case 2: // Blue
                        color_val.r = 0.0; color_val.g = 0.0; color_val.b = 1.0;
                        break;
                    case 3: // White
                        color_val.r = 1.0; color_val.g = 1.0; color_val.b = 1.0;
                        break;
                }
                f0r_set_param_value(instance, (f0r_param_t)&color_val, i);
                break;

            case F0R_PARAM_POSITION:
                // Move position in a circular pattern
                position_val.x = 0.5 + 0.4 * sin(frame_count * 0.1);
                position_val.y = 0.5 + 0.4 * cos(frame_count * 0.1);
                f0r_set_param_value(instance, (f0r_param_t)&position_val, i);
                break;

            case F0R_PARAM_STRING:
            {
                // For string parameters, use "0" as default
                char *string_val = "0";
                f0r_set_param_value(instance, (f0r_param_t)&string_val, i);
                break;
            }

            default:
                // For unknown parameter types, set to middle value
                double_val = 0.5;
                f0r_set_param_value(instance, (f0r_param_t)&double_val, i);
                break;
        }
    }
}

int main(int argc, char* argv[]) {
  // instance frei0r pointers
  static void *dl_handle;
  static f0r_init_f f0r_init;
  static f0r_deinit_f f0r_deinit;
  static f0r_plugin_info_t pi;
  static f0r_get_plugin_info_f f0r_get_plugin_info;
  static f0r_get_param_info_f f0r_get_param_info;
  static f0r_param_info_t param;
  static f0r_instance_t instance;
  static f0r_construct_f f0r_construct;
  static f0r_update_f f0r_update;
  static f0r_destruct_f f0r_destruct;
  static f0r_set_param_value_f f0r_set_param_value;
  static f0r_get_param_value_f f0r_get_param_value;

  const char *usage = "Usage: frei0r-run [-tdg] [-f frames] -p <frei0r_plugin_file>\n"
                      "  -d         debug mode\n"
                      "  -g         graphical display mode (Linux/WSL)\n"
                      "  -f frames  number of frames to process (default: 100)\n"
                      "  -p plugin  path to frei0r plugin file";
  if (argc < 2) {
  fprintf(stderr,"%s\n",usage);
  return -1;
  }

  int opt;
#if defined(GUI)
  int graphical = 1;
#else
  int graphical = 0;
#endif
  int debug = 0;
  int frames = 100; // Number of frames to test
  char plugin_file[512];
  plugin_file[0] = '\0';
  while((opt =  getopt(argc, argv, "tdgf:p:")) != -1) {
  switch(opt) {
  case 'd':
    debug = 1;
    break;
  case 'g':
    graphical = 1;
    break;
  case 'f':
    frames = atoi(optarg);
    break;
  case 'p':
    snprintf(plugin_file, 511, "%s", optarg);
    break;
  }
  }

  if (plugin_file[0] == '\0') {
    fprintf(stderr, "Error: plugin file required (-p option)\n%s\n", usage);
    return -1;
  }

  // Set fixed video properties for test pattern
  int frame_width = 640;
  int frame_height = 480;
  int fps = 30;

  const char *file = basename(plugin_file);
  const char *dir = dirname(plugin_file);
  char path[256];;
  snprintf(path, 255,"%s/%s",dir,file);
  // fprintf(stderr,"%s %s\n",argv[0], file);
  // load shared library
  dl_handle = dlopen(path, RTLD_NOW|RTLD_LOCAL|RTLD_NODELETE);
  if(!dl_handle) {
  fprintf(stderr,"error: %s\n",dlerror());
  exit(1);
  }
  // get plugin function calls
  f0r_init = (f0r_init_f) dlsym(dl_handle,"f0r_init");
  f0r_deinit = (f0r_deinit_f) dlsym(dl_handle,"f0r_deinit");
  f0r_get_plugin_info = (f0r_get_plugin_info_f) dlsym(dl_handle,"f0r_get_plugin_info");
  f0r_get_param_info = (f0r_get_param_info_f) dlsym(dl_handle,"f0r_get_param_info");
  f0r_construct = (f0r_construct_f) dlsym(dl_handle,"f0r_construct");
  f0r_update = (f0r_update_f) dlsym(dl_handle,"f0r_update");
  f0r_destruct = (f0r_destruct_f) dlsym(dl_handle,"f0r_destruct");
  f0r_set_param_value = (f0r_set_param_value_f) dlsym(dl_handle,"f0r_set_param_value");
  f0r_get_param_value = (f0r_get_param_value_f) dlsym(dl_handle,"f0r_get_param_value");

  // always initialize plugin first
  f0r_init();
  // get info about plugin
  f0r_get_plugin_info(&pi);
  const char *frei0r_color_model = (pi.color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
  pi.color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
  pi.color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown");

  if(debug) {
    fprintf(stderr,"{\n \"name\":\"%s\",\n \"type\":\"%s\",\n \"color_model\":\"%s\",\n \"num_params\":\"%d\"\n}",
            pi.name,
            pi.plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
            pi.plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
            pi.plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
            pi.plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown",
            frei0r_color_model,
            pi.num_params);
    // Print parameter information
    if (pi.num_params > 0) {
      fprintf(stderr,",\n \"parameters\":[\n");
      for (int i = 0; i < pi.num_params; i++) {
        f0r_get_param_info(&param, i);
        const char* param_type =
          param.type == F0R_PARAM_BOOL ? "bool" :
          param.type == F0R_PARAM_DOUBLE ? "double" :
          param.type == F0R_PARAM_COLOR ? "color" :
          param.type == F0R_PARAM_POSITION ? "position" :
          param.type == F0R_PARAM_STRING ? "string" : "unknown";
        fprintf(stderr,"  {\"name\":\"%s\",\"type\":\"%s\",\"explanation\":\"%s\"}",
                param.name, param_type, param.explanation);
        if (i < pi.num_params - 1) fprintf(stderr,",\n");
      }
      fprintf(stderr,"\n ]\n");
    }
    fprintf(stderr,"}\n");
  }

  instance = f0r_construct(frame_width, frame_height);

  uint32_t *input_buffer = NULL;
  uint32_t *input_buffer2 = NULL;
  uint32_t *input_buffer3 = NULL;
  uint32_t *output_buffer;

  // Allocate buffers based on plugin type
  if (pi.plugin_type == F0R_PLUGIN_TYPE_FILTER) {
      input_buffer = (uint32_t*)calloc(4, frame_width * frame_height);
  } else if (pi.plugin_type == F0R_PLUGIN_TYPE_MIXER2) {
      input_buffer = (uint32_t*)calloc(4, frame_width * frame_height);
      input_buffer2 = (uint32_t*)calloc(4, frame_width * frame_height);
  } else if (pi.plugin_type == F0R_PLUGIN_TYPE_MIXER3) {
      input_buffer = (uint32_t*)calloc(4, frame_width * frame_height);
      input_buffer2 = (uint32_t*)calloc(4, frame_width * frame_height);
      input_buffer3 = (uint32_t*)calloc(4, frame_width * frame_height);
  }
  // SOURCE type needs no input buffer

  output_buffer = (uint32_t*)calloc(4, frame_width * frame_height);

#if defined(GUI)
  // Generate initial test patterns
  if (input_buffer)
      generate_animated_test_pattern(input_buffer, frame_width, frame_height, 0, pi.color_model);
  if (input_buffer2)
      generate_animated_test_pattern(input_buffer2, frame_width, frame_height, 0, pi.color_model);
  if (input_buffer3)
      generate_animated_test_pattern(input_buffer3, frame_width, frame_height, 0, pi.color_model);
#else
  if (input_buffer)
      generate_test_pattern(input_buffer, frame_width, frame_height, pi.color_model, 0);
  if (input_buffer2)
      generate_test_pattern(input_buffer2, frame_width, frame_height, pi.color_model, 1);
  if (input_buffer3)
      generate_test_pattern(input_buffer3, frame_width, frame_height, pi.color_model, 2);
#endif

#if defined(__linux__) && defined(GUI)
  Display *display = NULL;
  Window window;
  GC gc;
  XImage *ximage = NULL;

  if (graphical) {
      display = XOpenDisplay(NULL);
      if (!display) {
          fprintf(stderr, "Warning: Cannot open X display, falling back to headless mode\n");
          graphical = 0;
      } else {
          int screen = DefaultScreen(display);
          window = XCreateSimpleWindow(display, RootWindow(display, screen),
                                       0, 0, frame_width, frame_height, 1,
                                       BlackPixel(display, screen),
                                       WhitePixel(display, screen));

          XStoreName(display, window, pi.name);
          XSelectInput(display, window, ExposureMask | KeyPressMask);
          XMapWindow(display, window);
          gc = XCreateGC(display, window, 0, NULL);

          Visual *visual = DefaultVisual(display, screen);
          ximage = XCreateImage(display, visual, 24, ZPixmap, 0,
                               (char*)output_buffer, frame_width, frame_height, 32, 0);

          XFlush(display);
      }
  }
#endif

  // Load f0r_update2 for mixers
  f0r_update2_f f0r_update2 = NULL;
  if (pi.plugin_type == F0R_PLUGIN_TYPE_MIXER2 || pi.plugin_type == F0R_PLUGIN_TYPE_MIXER3) {
      f0r_update2 = (f0r_update2_f)dlsym(dl_handle, "f0r_update2");
      if (!f0r_update2) {
          fprintf(stderr, "Error: Cannot load f0r_update2 for mixer plugin\n");
          if (input_buffer) free(input_buffer);
          if (input_buffer2) free(input_buffer2);
          if (input_buffer3) free(input_buffer3);
          free(output_buffer);
          f0r_destruct(instance);
          f0r_deinit();
          dlclose(dl_handle);
          return 1;
      }
  }

  // Test the plugin with different parameter values
  for (int frame = 0; frame < frames; frame++) {
#if defined(GUI)
      // Generate animated test patterns for this frame
      if (input_buffer)
          generate_animated_test_pattern(input_buffer, frame_width, frame_height, frame, pi.color_model);
      if (input_buffer2)
          generate_animated_test_pattern(input_buffer2, frame_width, frame_height, frame + 10, pi.color_model);
      if (input_buffer3)
          generate_animated_test_pattern(input_buffer3, frame_width, frame_height, frame + 20, pi.color_model);
#else
      if (input_buffer)
          generate_test_pattern(input_buffer, frame_width, frame_height, pi.color_model, 0);
      if (input_buffer2)
          generate_test_pattern(input_buffer2, frame_width, frame_height, pi.color_model, 1);
      if (input_buffer3)
          generate_test_pattern(input_buffer3, frame_width, frame_height, pi.color_model, 2);
#endif

      // Update parameters if the plugin has any
      if (pi.num_params > 0 && f0r_set_param_value) {
          test_parameters(instance, f0r_set_param_value, f0r_get_param_info, pi.num_params, frame);
      }

      // Apply plugin based on type
      double time = (double)frame / (double)fps;

      switch (pi.plugin_type) {
          case F0R_PLUGIN_TYPE_SOURCE:
              f0r_update(instance, time, NULL, output_buffer);
              break;
          case F0R_PLUGIN_TYPE_FILTER:
              f0r_update(instance, time, (const uint32_t*)input_buffer, output_buffer);
              break;
          case F0R_PLUGIN_TYPE_MIXER2:
              f0r_update2(instance, time, (const uint32_t*)input_buffer,
                         (const uint32_t*)input_buffer2, NULL, output_buffer);
              break;
          case F0R_PLUGIN_TYPE_MIXER3:
              f0r_update2(instance, time, (const uint32_t*)input_buffer,
                         (const uint32_t*)input_buffer2, (const uint32_t*)input_buffer3,
                         output_buffer);
              break;
          default:
              fprintf(stderr, "Unknown plugin type: %d\n", pi.plugin_type);
              break;
      }

#if defined(__linux__) && defined(GUI)
      if (graphical && display) {
          ximage->data = (char*)output_buffer;
          XPutImage(display, window, gc, ximage, 0, 0, 0, 0, frame_width, frame_height);
          XFlush(display);

          // Check for key press to exit early
          while (XPending(display)) {
              XEvent event;
              XNextEvent(display, &event);
              if (event.type == KeyPress) {
                  fprintf(stderr, "\nInterrupted by user at frame %d\n", frame);
                  frame = frames; // Exit loop
                  break;
              }
          }

          usleep(1000000 / fps); // Frame delay
      }
#endif

      if (!graphical && frame % 10 == 0 && debug) {
          printf("Frame %d processed\n", frame);
      }
  }

#if defined(__linux__) && defined(GUI)
  if (graphical && display) {
      ximage->data = NULL; // Prevent XDestroyImage from freeing our buffer
      XDestroyImage(ximage);
      XFreeGC(display, gc);
      XDestroyWindow(display, window);
      XCloseDisplay(display);
  }
#endif

  if(debug) {
    const char *plugin_type_name = "unknown";
    switch (pi.plugin_type) {
        case F0R_PLUGIN_TYPE_SOURCE: plugin_type_name = "source"; break;
        case F0R_PLUGIN_TYPE_FILTER: plugin_type_name = "filter"; break;
        case F0R_PLUGIN_TYPE_MIXER2: plugin_type_name = "mixer2"; break;
        case F0R_PLUGIN_TYPE_MIXER3: plugin_type_name = "mixer3"; break;
    }
    printf("Test completed successfully. Plugin: %s (type: %s)\n", pi.name, plugin_type_name);
    printf("Tested %d frames with %d parameters\n", frames, pi.num_params);
    if (pi.plugin_type != F0R_PLUGIN_TYPE_SOURCE) {
        printf("Input: %dx%d test pattern(s)\n", frame_width, frame_height);
    }
    printf("Output: %dx%d processed frames\n", frame_width, frame_height);
  }

  if (input_buffer) free(input_buffer);
  if (input_buffer2) free(input_buffer2);
  if (input_buffer3) free(input_buffer3);
  free(output_buffer);

  f0r_destruct(instance);
  f0r_deinit();

  dlclose(dl_handle);

  return 0;
}