edl plugin names eng, fix segv for opengl brender, renderfarm rework strategy, perf...
[goodguy/history.git] / cinelerra-5.1 / plugins / radialblur / radialblur.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <math.h>
23 #include <stdint.h>
24 #include <string.h>
25
26
27 #include "affine.h"
28 #include "bcdisplayinfo.h"
29 #include "clip.h"
30 #include "bchash.h"
31 #include "filexml.h"
32 #include "keyframe.h"
33 #include "language.h"
34 #include "loadbalance.h"
35 #include "pluginvclient.h"
36 #include "vframe.h"
37
38
39 class RadialBlurMain;
40 class RadialBlurEngine;
41
42
43
44
45
46 class RadialBlurConfig
47 {
48 public:
49         RadialBlurConfig();
50
51         int equivalent(RadialBlurConfig &that);
52         void copy_from(RadialBlurConfig &that);
53         void interpolate(RadialBlurConfig &prev,
54                 RadialBlurConfig &next,
55                 long prev_frame,
56                 long next_frame,
57                 long current_frame);
58
59         int x;
60         int y;
61         int steps;
62         int angle;
63         int r;
64         int g;
65         int b;
66         int a;
67 };
68
69
70
71 class RadialBlurSize : public BC_ISlider
72 {
73 public:
74         RadialBlurSize(RadialBlurMain *plugin,
75                 int x,
76                 int y,
77                 int *output,
78                 int min,
79                 int max);
80         int handle_event();
81         RadialBlurMain *plugin;
82         int *output;
83 };
84
85 class RadialBlurToggle : public BC_CheckBox
86 {
87 public:
88         RadialBlurToggle(RadialBlurMain *plugin,
89                 int x,
90                 int y,
91                 int *output,
92                 char *string);
93         int handle_event();
94         RadialBlurMain *plugin;
95         int *output;
96 };
97
98 class RadialBlurWindow : public PluginClientWindow
99 {
100 public:
101         RadialBlurWindow(RadialBlurMain *plugin);
102         ~RadialBlurWindow();
103
104         void create_objects();
105
106
107         RadialBlurSize *x, *y, *steps, *angle;
108         RadialBlurToggle *r, *g, *b, *a;
109         RadialBlurMain *plugin;
110 };
111
112
113
114
115
116
117 class RadialBlurMain : public PluginVClient
118 {
119 public:
120         RadialBlurMain(PluginServer *server);
121         ~RadialBlurMain();
122
123         int process_buffer(VFrame *frame,
124                 int64_t start_position,
125                 double frame_rate);
126         int is_realtime();
127         void save_data(KeyFrame *keyframe);
128         void read_data(KeyFrame *keyframe);
129         void update_gui();
130         int handle_opengl();
131
132         PLUGIN_CLASS_MEMBERS(RadialBlurConfig)
133
134         VFrame *input, *output, *temp;
135         RadialBlurEngine *engine;
136 // Rotate engine only used for OpenGL
137         AffineEngine *rotate;
138 };
139
140 class RadialBlurPackage : public LoadPackage
141 {
142 public:
143         RadialBlurPackage();
144         int y1, y2;
145 };
146
147 class RadialBlurUnit : public LoadClient
148 {
149 public:
150         RadialBlurUnit(RadialBlurEngine *server, RadialBlurMain *plugin);
151         void process_package(LoadPackage *package);
152         RadialBlurEngine *server;
153         RadialBlurMain *plugin;
154 };
155
156 class RadialBlurEngine : public LoadServer
157 {
158 public:
159         RadialBlurEngine(RadialBlurMain *plugin,
160                 int total_clients,
161                 int total_packages);
162         void init_packages();
163         LoadClient* new_client();
164         LoadPackage* new_package();
165         RadialBlurMain *plugin;
166 };
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 REGISTER_PLUGIN(RadialBlurMain)
187
188
189
190 RadialBlurConfig::RadialBlurConfig()
191 {
192         x = 50;
193         y = 50;
194         steps = 10;
195         angle = 33;
196         r = 1;
197         g = 1;
198         b = 1;
199         a = 1;
200 }
201
202 int RadialBlurConfig::equivalent(RadialBlurConfig &that)
203 {
204         return
205                 angle == that.angle &&
206                 x == that.x &&
207                 y == that.y &&
208                 steps == that.steps &&
209                 r == that.r &&
210                 g == that.g &&
211                 b == that.b &&
212                 a == that.a;
213 }
214
215 void RadialBlurConfig::copy_from(RadialBlurConfig &that)
216 {
217         x = that.x;
218         y = that.y;
219         angle = that.angle;
220         steps = that.steps;
221         r = that.r;
222         g = that.g;
223         b = that.b;
224         a = that.a;
225 }
226
227 void RadialBlurConfig::interpolate(RadialBlurConfig &prev,
228         RadialBlurConfig &next,
229         long prev_frame,
230         long next_frame,
231         long current_frame)
232 {
233         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
234         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
235         this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
236         this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
237         this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
238         this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
239         r = prev.r;
240         g = prev.g;
241         b = prev.b;
242         a = prev.a;
243 }
244
245
246
247
248
249
250
251
252
253
254
255
256
257 RadialBlurWindow::RadialBlurWindow(RadialBlurMain *plugin)
258  : PluginClientWindow(plugin,
259         230,
260         340,
261         230,
262         340,
263         0)
264 {
265         this->plugin = plugin;
266 }
267
268 RadialBlurWindow::~RadialBlurWindow()
269 {
270 }
271
272 void RadialBlurWindow::create_objects()
273 {
274         int x = 10, y = 10;
275
276         add_subwindow(new BC_Title(x, y, _("X:")));
277         y += 20;
278         add_subwindow(this->x = new RadialBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
279         y += 30;
280         add_subwindow(new BC_Title(x, y, _("Y:")));
281         y += 20;
282         add_subwindow(this->y = new RadialBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
283         y += 30;
284         add_subwindow(new BC_Title(x, y, _("Angle:")));
285         y += 20;
286         add_subwindow(angle = new RadialBlurSize(plugin, x, y, &plugin->config.angle, 0, 360));
287         y += 30;
288         add_subwindow(new BC_Title(x, y, _("Steps:")));
289         y += 20;
290         add_subwindow(steps = new RadialBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
291         y += 30;
292         add_subwindow(r = new RadialBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
293         y += 30;
294         add_subwindow(g = new RadialBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
295         y += 30;
296         add_subwindow(b = new RadialBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
297         y += 30;
298         add_subwindow(a = new RadialBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
299         y += 30;
300
301         show_window();
302         flush();
303 }
304
305
306
307
308
309
310
311
312
313
314
315 RadialBlurToggle::RadialBlurToggle(RadialBlurMain *plugin,
316         int x,
317         int y,
318         int *output,
319         char *string)
320  : BC_CheckBox(x, y, *output, string)
321 {
322         this->plugin = plugin;
323         this->output = output;
324 }
325
326 int RadialBlurToggle::handle_event()
327 {
328         *output = get_value();
329         plugin->send_configure_change();
330         return 1;
331 }
332
333
334
335
336
337
338
339 RadialBlurSize::RadialBlurSize(RadialBlurMain *plugin,
340         int x,
341         int y,
342         int *output,
343         int min,
344         int max)
345  : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
346 {
347         this->plugin = plugin;
348         this->output = output;
349 }
350 int RadialBlurSize::handle_event()
351 {
352         *output = get_value();
353         plugin->send_configure_change();
354         return 1;
355 }
356
357
358
359
360
361
362
363
364
365
366 RadialBlurMain::RadialBlurMain(PluginServer *server)
367  : PluginVClient(server)
368 {
369
370         engine = 0;
371         temp = 0;
372         rotate = 0;
373 }
374
375 RadialBlurMain::~RadialBlurMain()
376 {
377
378         if(engine) delete engine;
379         if(temp) delete temp;
380         delete rotate;
381 }
382
383 const char* RadialBlurMain::plugin_title() { return N_("Radial Blur"); }
384 int RadialBlurMain::is_realtime() { return 1; }
385
386
387
388 NEW_WINDOW_MACRO(RadialBlurMain, RadialBlurWindow)
389
390 LOAD_CONFIGURATION_MACRO(RadialBlurMain, RadialBlurConfig)
391
392 int RadialBlurMain::process_buffer(VFrame *frame,
393                                                         int64_t start_position,
394                                                         double frame_rate)
395 {
396         load_configuration();
397
398
399         read_frame(frame,
400                 0,
401                 get_source_position(),
402                 get_framerate(),
403 //              0);
404                 get_use_opengl());
405
406         if(get_use_opengl()) return run_opengl();
407
408         if(!engine) engine = new RadialBlurEngine(this,
409                 get_project_smp() + 1,
410                 get_project_smp() + 1);
411
412         this->input = frame;
413         this->output = frame;
414
415
416         if(!temp)
417                 temp = new VFrame(frame->get_w(), frame->get_h(),
418                         frame->get_color_model(), 0);
419         temp->copy_from(frame);
420         this->input = temp;
421
422         engine->process_packages();
423         return 0;
424 }
425
426
427 void RadialBlurMain::update_gui()
428 {
429         if(thread)
430         {
431                 load_configuration();
432                 thread->window->lock_window();
433                 ((RadialBlurWindow*)thread->window)->x->update(config.x);
434                 ((RadialBlurWindow*)thread->window)->y->update(config.y);
435                 ((RadialBlurWindow*)thread->window)->angle->update(config.angle);
436                 ((RadialBlurWindow*)thread->window)->steps->update(config.steps);
437                 ((RadialBlurWindow*)thread->window)->r->update(config.r);
438                 ((RadialBlurWindow*)thread->window)->g->update(config.g);
439                 ((RadialBlurWindow*)thread->window)->b->update(config.b);
440                 ((RadialBlurWindow*)thread->window)->a->update(config.a);
441                 thread->window->unlock_window();
442         }
443 }
444
445
446
447
448 void RadialBlurMain::save_data(KeyFrame *keyframe)
449 {
450         FileXML output;
451
452 // cause data to be stored directly in text
453         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
454         output.tag.set_title("RADIALBLUR");
455
456         output.tag.set_property("X", config.x);
457         output.tag.set_property("Y", config.y);
458         output.tag.set_property("ANGLE", config.angle);
459         output.tag.set_property("STEPS", config.steps);
460         output.tag.set_property("R", config.r);
461         output.tag.set_property("G", config.g);
462         output.tag.set_property("B", config.b);
463         output.tag.set_property("A", config.a);
464         output.append_tag();
465         output.tag.set_title("/RADIALBLUR");
466         output.append_tag();
467         output.append_newline();
468         output.terminate_string();
469 }
470
471 void RadialBlurMain::read_data(KeyFrame *keyframe)
472 {
473         FileXML input;
474
475         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
476
477         int result = 0;
478
479         while(!result)
480         {
481                 result = input.read_tag();
482
483                 if(!result)
484                 {
485                         if(input.tag.title_is("RADIALBLUR"))
486                         {
487                                 config.x = input.tag.get_property("X", config.x);
488                                 config.y = input.tag.get_property("Y", config.y);
489                                 config.angle = input.tag.get_property("ANGLE", config.angle);
490                                 config.steps = input.tag.get_property("STEPS", config.steps);
491                                 config.r = input.tag.get_property("R", config.r);
492                                 config.g = input.tag.get_property("G", config.g);
493                                 config.b = input.tag.get_property("B", config.b);
494                                 config.a = input.tag.get_property("A", config.a);
495                         }
496                 }
497         }
498 }
499
500 int RadialBlurMain::handle_opengl()
501 {
502 #ifdef HAVE_GL
503         get_output()->to_texture();
504         get_output()->enable_opengl();
505         get_output()->init_screen();
506         get_output()->bind_texture(0);
507
508
509         //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
510         glClearColor(0.0, 0.0, 0.0, 0.0);
511         glClear(GL_COLOR_BUFFER_BIT);
512
513 // Draw unselected channels
514         glEnable(GL_BLEND);
515         glBlendFunc(GL_ONE, GL_ONE);
516         glDrawBuffer(GL_BACK);
517         if(!config.r || !config.g || !config.b || !config.a)
518         {
519                 glColor4f(config.r ? 0 : 1,
520                         config.g ? 0 : 1,
521                         config.b ? 0 : 1,
522                         config.a ? 0 : 1);
523                 get_output()->draw_texture();
524         }
525         glAccum(GL_LOAD, 1.0);
526
527
528 // Blur selected channels
529         float fraction = 1.0 / config.steps;
530         for(int i = 0; i < config.steps; i++)
531         {
532                 get_output()->set_opengl_state(VFrame::TEXTURE);
533                 glClear(GL_COLOR_BUFFER_BIT);
534                 glColor4f(config.r ? 1 : 0,
535                         config.g ? 1 : 0,
536                         config.b ? 1 : 0,
537                         config.a ? 1 : 0);
538
539                 float w = get_output()->get_w();
540                 float h = get_output()->get_h();
541
542
543
544                 double current_angle = (double)config.angle *
545                         i /
546                         config.steps -
547                         (double)config.angle / 2;
548
549                 if(!rotate) rotate = new AffineEngine(PluginClient::smp + 1,
550                         PluginClient::smp + 1);
551                 rotate->set_in_pivot((int)(config.x * w / 100),
552                         (int)(config.y * h / 100));
553                 rotate->set_out_pivot((int)(config.x * w / 100),
554                         (int)(config.y * h / 100));
555                 rotate->set_opengl(1);
556                 rotate->rotate(get_output(),
557                         get_output(),
558                         current_angle);
559
560                 glAccum(GL_ACCUM, fraction);
561                 glEnable(GL_TEXTURE_2D);
562                 glColor4f(config.r ? 1 : 0,
563                         config.g ? 1 : 0,
564                         config.b ? 1 : 0,
565                         config.a ? 1 : 0);
566         }
567
568
569         glDisable(GL_BLEND);
570         glReadBuffer(GL_BACK);
571         glDisable(GL_TEXTURE_2D);
572         glAccum(GL_RETURN, 1.0);
573
574         glColor4f(1, 1, 1, 1);
575         get_output()->set_opengl_state(VFrame::SCREEN);
576 #endif
577         return 0;
578 }
579
580
581
582
583
584
585
586
587
588
589
590 RadialBlurPackage::RadialBlurPackage()
591  : LoadPackage()
592 {
593 }
594
595
596 RadialBlurUnit::RadialBlurUnit(RadialBlurEngine *server,
597         RadialBlurMain *plugin)
598  : LoadClient(server)
599 {
600         this->plugin = plugin;
601         this->server = server;
602 }
603
604
605 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
606 { \
607         int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
608         TYPE **in_rows = (TYPE**)plugin->input->get_rows(); \
609         TYPE **out_rows = (TYPE**)plugin->output->get_rows(); \
610         int steps = plugin->config.steps; \
611         double step = (double)plugin->config.angle / 360 * 2 * M_PI / steps; \
612  \
613         for(int i = pkg->y1, out_y = pkg->y1 - center_y; \
614                 i < pkg->y2; \
615                 i++, out_y++) \
616         { \
617                 TYPE *out_row = out_rows[i]; \
618                 TYPE *in_row = in_rows[i]; \
619                 int y_square = out_y * out_y; \
620  \
621                 for(int j = 0, out_x = -center_x; j < w; j++, out_x++) \
622                 { \
623                         TEMP_TYPE accum_r = 0; \
624                         TEMP_TYPE accum_g = 0; \
625                         TEMP_TYPE accum_b = 0; \
626                         TEMP_TYPE accum_a = 0; \
627  \
628 /* Output coordinate to polar */ \
629                         double magnitude = sqrt(y_square + out_x * out_x); \
630                         double angle; \
631                         if(out_y < 0) \
632                                 angle = atan((double)out_x / out_y) + M_PI; \
633                         else \
634                         if(out_y > 0) \
635                                 angle = atan((double)out_x / out_y); \
636                         else \
637                         if(out_x > 0) \
638                                 angle = M_PI / 2; \
639                         else \
640                                 angle = M_PI * 1.5; \
641  \
642 /* Overlay all steps on this pixel*/ \
643                         angle -= (double)plugin->config.angle / 360 * M_PI; \
644                         for(int k = 0; k < steps; k++, angle += step) \
645                         { \
646 /* Polar to input coordinate */ \
647                                 int in_x = (int)(magnitude * sin(angle)) + center_x; \
648                                 int in_y = (int)(magnitude * cos(angle)) + center_y; \
649  \
650 /* Accumulate input coordinate */ \
651                                 if(in_x >= 0 && in_x < w && in_y >= 0 && in_y < h) \
652                                 { \
653                                         accum_r += in_rows[in_y][in_x * COMPONENTS]; \
654                                         if(DO_YUV) \
655                                         { \
656                                                 accum_g += (int)in_rows[in_y][in_x * COMPONENTS + 1]; \
657                                                 accum_b += (int)in_rows[in_y][in_x * COMPONENTS + 2]; \
658                                         } \
659                                         else \
660                                         { \
661                                                 accum_g += in_rows[in_y][in_x * COMPONENTS + 1]; \
662                                                 accum_b += in_rows[in_y][in_x * COMPONENTS + 2]; \
663                                         } \
664                                         if(COMPONENTS == 4) \
665                                                 accum_a += in_rows[in_y][in_x * COMPONENTS + 3]; \
666                                 } \
667                                 else \
668                                 { \
669                                         accum_g += chroma_offset; \
670                                         accum_b += chroma_offset; \
671                                 } \
672                         } \
673  \
674 /* Accumulation to output */ \
675                         if(do_r) \
676                         { \
677                                 *out_row++ = (accum_r * fraction) / 0x10000; \
678                                 in_row++; \
679                         } \
680                         else \
681                         { \
682                                 *out_row++ = *in_row++; \
683                         } \
684  \
685                         if(do_g) \
686                         { \
687                                 if(DO_YUV) \
688                                         *out_row++ = ((accum_g * fraction) / 0x10000); \
689                                 else \
690                                         *out_row++ = (accum_g * fraction) / 0x10000; \
691                                 in_row++; \
692                         } \
693                         else \
694                         { \
695                                 *out_row++ = *in_row++; \
696                         } \
697  \
698                         if(do_b) \
699                         { \
700                                 if(DO_YUV) \
701                                         *out_row++ = (accum_b * fraction) / 0x10000; \
702                                 else \
703                                         *out_row++ = (accum_b * fraction) / 0x10000; \
704                                 in_row++; \
705                         } \
706                         else \
707                         { \
708                                 *out_row++ = *in_row++; \
709                         } \
710  \
711                         if(COMPONENTS == 4) \
712                         { \
713                                 if(do_a) \
714                                 { \
715                                         *out_row++ = (accum_a * fraction) / 0x10000; \
716                                         in_row++; \
717                                 } \
718                                 else \
719                                 { \
720                                         *out_row++ = *in_row++; \
721                                 } \
722                         } \
723                 } \
724         } \
725 }
726
727 void RadialBlurUnit::process_package(LoadPackage *package)
728 {
729         RadialBlurPackage *pkg = (RadialBlurPackage*)package;
730         int h = plugin->output->get_h();
731         int w = plugin->output->get_w();
732         int do_r = plugin->config.r;
733         int do_g = plugin->config.g;
734         int do_b = plugin->config.b;
735         int do_a = plugin->config.a;
736         int fraction = 0x10000 / plugin->config.steps;
737         int center_x = plugin->config.x * w / 100;
738         int center_y = plugin->config.y * h / 100;
739
740         switch(plugin->input->get_color_model())
741         {
742                 case BC_RGB888:
743                         BLEND_LAYER(3, uint8_t, int, 0xff, 0)
744                         break;
745                 case BC_RGBA8888:
746                         BLEND_LAYER(4, uint8_t, int, 0xff, 0)
747                         break;
748                 case BC_RGB_FLOAT:
749                         BLEND_LAYER(3, float, float, 1, 0)
750                         break;
751                 case BC_RGBA_FLOAT:
752                         BLEND_LAYER(4, float, float, 1, 0)
753                         break;
754                 case BC_RGB161616:
755                         BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
756                         break;
757                 case BC_RGBA16161616:
758                         BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
759                         break;
760                 case BC_YUV888:
761                         BLEND_LAYER(3, uint8_t, int, 0xff, 1)
762                         break;
763                 case BC_YUVA8888:
764                         BLEND_LAYER(4, uint8_t, int, 0xff, 1)
765                         break;
766                 case BC_YUV161616:
767                         BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
768                         break;
769                 case BC_YUVA16161616:
770                         BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
771                         break;
772         }
773 }
774
775
776
777
778
779
780 RadialBlurEngine::RadialBlurEngine(RadialBlurMain *plugin,
781         int total_clients,
782         int total_packages)
783  : LoadServer(total_clients, total_packages)
784 // : LoadServer(1, 1)
785 {
786         this->plugin = plugin;
787 }
788
789 void RadialBlurEngine::init_packages()
790 {
791         for(int i = 0; i < get_total_packages(); i++)
792         {
793                 RadialBlurPackage *package = (RadialBlurPackage*)get_package(i);
794                 package->y1 = plugin->output->get_h() * i / get_total_packages();
795                 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
796         }
797 }
798
799 LoadClient* RadialBlurEngine::new_client()
800 {
801         return new RadialBlurUnit(this, plugin);
802 }
803
804 LoadPackage* RadialBlurEngine::new_package()
805 {
806         return new RadialBlurPackage;
807 }
808
809
810
811
812