render effect segv, drag chkbox track coords, check mask active,
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / overlay / overlay.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 "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "filexml.h"
28 #include "guicast.h"
29 #include "keyframe.h"
30 #include "language.h"
31 #include "overlayframe.h"
32 #include "pluginvclient.h"
33 #include "vpatchgui.h"
34 #include "vframe.h"
35
36 #include <string.h>
37 #include <stdint.h>
38
39
40 class Overlay;
41 class OverlayWindow;
42
43
44 class OverlayConfig
45 {
46 public:
47         OverlayConfig();
48
49         static const char* mode_to_text(int mode);
50         int mode;
51
52         static const char* direction_to_text(int direction);
53         int direction;
54         enum
55         {
56                 BOTTOM_FIRST,
57                 TOP_FIRST
58         };
59
60         static const char* output_to_text(int output_layer);
61         int output_layer;
62         enum
63         {
64                 TOP,
65                 BOTTOM
66         };
67 };
68
69 class OverlayMode : public BC_PopupMenu
70 {
71 public:
72         OverlayMode(Overlay *plugin,
73                 int x,
74                 int y);
75         void create_objects();
76         int handle_event();
77         Overlay *plugin;
78 };
79
80 class OverlayDirection : public BC_PopupMenu
81 {
82 public:
83         OverlayDirection(Overlay *plugin,
84                 int x,
85                 int y);
86         void create_objects();
87         int handle_event();
88         Overlay *plugin;
89 };
90
91 class OverlayOutput : public BC_PopupMenu
92 {
93 public:
94         OverlayOutput(Overlay *plugin,
95                 int x,
96                 int y);
97         void create_objects();
98         int handle_event();
99         Overlay *plugin;
100 };
101
102
103 class OverlayWindow : public PluginClientWindow
104 {
105 public:
106         OverlayWindow(Overlay *plugin);
107         ~OverlayWindow();
108
109         void create_objects();
110
111
112         Overlay *plugin;
113         OverlayMode *mode;
114         OverlayDirection *direction;
115         OverlayOutput *output;
116 };
117
118 class Overlay : public PluginVClient
119 {
120 public:
121         Overlay(PluginServer *server);
122         ~Overlay();
123
124
125         PLUGIN_CLASS_MEMBERS(OverlayConfig);
126
127         int process_buffer(VFrame **frame,
128                 int64_t start_position,
129                 double frame_rate);
130         int is_realtime();
131         int is_multichannel();
132         int is_synthesis();
133         void save_data(KeyFrame *keyframe);
134         void read_data(KeyFrame *keyframe);
135         void update_gui();
136         int handle_opengl();
137
138         OverlayFrame *overlayer;
139         VFrame *temp;
140         int current_layer;
141         int output_layer;
142         int input_layer;
143 };
144
145 OverlayConfig::OverlayConfig()
146 {
147         mode = TRANSFER_NORMAL;
148         direction = OverlayConfig::BOTTOM_FIRST;
149         output_layer = OverlayConfig::TOP;
150 }
151
152 const char* OverlayConfig::mode_to_text(int mode)
153 {
154         return VModePatch::mode_to_text(mode);
155 }
156
157 const char* OverlayConfig::direction_to_text(int direction)
158 {
159         switch(direction)
160         {
161                 case OverlayConfig::BOTTOM_FIRST: return _("Bottom first");
162                 case OverlayConfig::TOP_FIRST:    return _("Top first");
163         }
164         return "";
165 }
166
167 const char* OverlayConfig::output_to_text(int output_layer)
168 {
169         switch(output_layer)
170         {
171                 case OverlayConfig::TOP:    return _("Top");
172                 case OverlayConfig::BOTTOM: return _("Bottom");
173         }
174         return "";
175 }
176
177
178
179
180
181
182
183
184
185 OverlayWindow::OverlayWindow(Overlay *plugin)
186  : PluginClientWindow(plugin,
187         300,
188         160,
189         300,
190         160,
191         0)
192 {
193         this->plugin = plugin;
194 }
195
196 OverlayWindow::~OverlayWindow()
197 {
198 }
199
200 void OverlayWindow::create_objects()
201 {
202         int x = 10, y = 10;
203
204         BC_Title *title;
205         add_subwindow(title = new BC_Title(x, y, _("Mode:")));
206         add_subwindow(mode = new OverlayMode(plugin,
207                 x + title->get_w() + 5,
208                 y));
209         mode->create_objects();
210
211         y += 30;
212         add_subwindow(title = new BC_Title(x, y, _("Layer order:")));
213         add_subwindow(direction = new OverlayDirection(plugin,
214                 x + title->get_w() + 5,
215                 y));
216         direction->create_objects();
217
218         y += 30;
219         add_subwindow(title = new BC_Title(x, y, _("Output layer:")));
220         add_subwindow(output = new OverlayOutput(plugin,
221                 x + title->get_w() + 5,
222                 y));
223         output->create_objects();
224
225         show_window();
226         flush();
227 }
228
229
230
231
232
233
234
235 OverlayMode::OverlayMode(Overlay *plugin, int x, int y)
236  : BC_PopupMenu(x, y, 150,
237         OverlayConfig::mode_to_text(plugin->config.mode), 1)
238 {
239         this->plugin = plugin;
240 }
241
242 void OverlayMode::create_objects()
243 {
244         for(int i = 0; i < TRANSFER_TYPES; i++)
245                 add_item(new BC_MenuItem(OverlayConfig::mode_to_text(i)));
246 }
247
248 int OverlayMode::handle_event()
249 {
250         char *text = get_text();
251
252         for(int i = 0; i < TRANSFER_TYPES; i++)
253         {
254                 if(!strcmp(text, OverlayConfig::mode_to_text(i)))
255                 {
256                         plugin->config.mode = i;
257                         break;
258                 }
259         }
260
261         plugin->send_configure_change();
262         return 1;
263 }
264
265
266 OverlayDirection::OverlayDirection(Overlay *plugin,
267         int x,
268         int y)
269  : BC_PopupMenu(x,
270         y,
271         150,
272         OverlayConfig::direction_to_text(plugin->config.direction),
273         1)
274 {
275         this->plugin = plugin;
276 }
277
278 void OverlayDirection::create_objects()
279 {
280         add_item(new BC_MenuItem(
281                 OverlayConfig::direction_to_text(
282                         OverlayConfig::TOP_FIRST)));
283         add_item(new BC_MenuItem(
284                 OverlayConfig::direction_to_text(
285                         OverlayConfig::BOTTOM_FIRST)));
286 }
287
288 int OverlayDirection::handle_event()
289 {
290         char *text = get_text();
291
292         if(!strcmp(text,
293                 OverlayConfig::direction_to_text(
294                         OverlayConfig::TOP_FIRST)))
295                 plugin->config.direction = OverlayConfig::TOP_FIRST;
296         else
297         if(!strcmp(text,
298                 OverlayConfig::direction_to_text(
299                         OverlayConfig::BOTTOM_FIRST)))
300                 plugin->config.direction = OverlayConfig::BOTTOM_FIRST;
301
302         plugin->send_configure_change();
303         return 1;
304 }
305
306
307 OverlayOutput::OverlayOutput(Overlay *plugin,
308         int x,
309         int y)
310  : BC_PopupMenu(x,
311         y,
312         100,
313         OverlayConfig::output_to_text(plugin->config.output_layer),
314         1)
315 {
316         this->plugin = plugin;
317 }
318
319 void OverlayOutput::create_objects()
320 {
321         add_item(new BC_MenuItem(
322                 OverlayConfig::output_to_text(
323                         OverlayConfig::TOP)));
324         add_item(new BC_MenuItem(
325                 OverlayConfig::output_to_text(
326                         OverlayConfig::BOTTOM)));
327 }
328
329 int OverlayOutput::handle_event()
330 {
331         char *text = get_text();
332
333         if(!strcmp(text,
334                 OverlayConfig::output_to_text(
335                         OverlayConfig::TOP)))
336                 plugin->config.output_layer = OverlayConfig::TOP;
337         else
338         if(!strcmp(text,
339                 OverlayConfig::output_to_text(
340                         OverlayConfig::BOTTOM)))
341                 plugin->config.output_layer = OverlayConfig::BOTTOM;
342
343         plugin->send_configure_change();
344         return 1;
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 REGISTER_PLUGIN(Overlay)
367
368
369
370
371
372
373 Overlay::Overlay(PluginServer *server)
374  : PluginVClient(server)
375 {
376
377         overlayer = 0;
378         temp = 0;
379 }
380
381
382 Overlay::~Overlay()
383 {
384
385         if(overlayer) delete overlayer;
386         if(temp) delete temp;
387 }
388
389
390
391 int Overlay::process_buffer(VFrame **frame,
392         int64_t start_position,
393         double frame_rate)
394 {
395         load_configuration();
396
397         EDLSession* session = get_edl()->session;
398         int interpolation_type = session ? session->interpolation_type : NEAREST_NEIGHBOR;
399
400         int step = config.direction == OverlayConfig::BOTTOM_FIRST ?  -1 : 1;
401         int layers = get_total_buffers();
402         input_layer = config.direction == OverlayConfig::BOTTOM_FIRST ? layers-1 : 0;
403         output_layer = config.output_layer == OverlayConfig::TOP ?  0 : layers-1;
404         VFrame *output = frame[output_layer];
405
406         current_layer = input_layer;
407         read_frame(output, current_layer,  // Direct copy the first layer
408                          start_position, frame_rate, get_use_opengl());
409
410         if( --layers > 0 ) {    // need 2 layers to do overlay
411                 if( !temp )
412                         temp = new VFrame(frame[0]->get_w(), frame[0]->get_h(),
413                                         frame[0]->get_color_model(), 0);
414
415                 while( --layers >= 0 ) {
416                         current_layer += step;
417                         read_frame(temp, current_layer,
418                                  start_position, frame_rate, get_use_opengl());
419
420                         if(get_use_opengl()) {
421                                 run_opengl();
422                                 continue;
423                         }
424
425                         if(!overlayer)
426                                 overlayer = new OverlayFrame(get_project_smp() + 1);
427
428                         overlayer->overlay(output, temp,
429                                 0, 0, output->get_w(), output->get_h(),
430                                 0, 0, output->get_w(), output->get_h(),
431                                 1, config.mode, interpolation_type);
432                 }
433         }
434
435         return 0;
436 }
437
438 int Overlay::handle_opengl()
439 {
440 #ifdef HAVE_GL
441         static const char *get_pixels_frag =
442                 "uniform sampler2D src_tex;\n"
443                 "uniform sampler2D dst_tex;\n"
444                 "uniform vec2 dst_tex_dimensions;\n"
445                 "uniform vec3 chroma_offset;\n"
446                 "void main()\n"
447                 "{\n"
448                 "       vec4 dst_color = texture2D(dst_tex, gl_FragCoord.xy / dst_tex_dimensions);\n"
449                 "       vec4 src_color = texture2D(src_tex, gl_TexCoord[0].st);\n"
450                 "       src_color.rgb -= chroma_offset;\n"
451                 "       dst_color.rgb -= chroma_offset;\n";
452
453         static const char *put_pixels_frag =
454                 "       result.rgb += chroma_offset;\n"
455                 "       gl_FragColor = result;\n"
456                 "}\n";
457
458 #define QQ(q)#q
459 #define SS(s)QQ(s)
460
461 #define GL_STD_FRAG(FN) static const char *blend_##FN##_frag = \
462         "       vec4 result;\n" \
463         "       result.rgb = " SS(COLOR_##FN(1.0, src_color.rgb, src_color.a, dst_color.rgb, dst_color.a)) ";\n" \
464         "       result.a = " SS(ALPHA_##FN(1.0, src_color.a, dst_color.a))";\n" \
465
466 #define GL_VEC_FRAG(FN) static const char *blend_##FN##_frag = \
467         "       vec4 result;\n" \
468         "       result.r = " SS(COLOR_##FN(1.0, src_color.r, src_color.a, dst_color.r, dst_color.a)) ";\n" \
469         "       result.g = " SS(COLOR_##FN(1.0, src_color.g, src_color.a, dst_color.g, dst_color.a)) ";\n" \
470         "       result.b = " SS(COLOR_##FN(1.0, src_color.b, src_color.a, dst_color.b, dst_color.a)) ";\n" \
471         "       result.a = " SS(ALPHA_##FN(1.0, src_color.a, dst_color.a)) ";\n" \
472         "       result = clamp(result, 0.0, 1.0);\n" \
473
474 #undef mabs
475 #define mabs abs
476 #undef mmin
477 #define mmin min
478 #undef mmax
479 #define mmax max
480
481 #undef ZERO
482 #define ZERO 0.0
483 #undef ONE
484 #define ONE 1.0
485 #undef TWO
486 #define TWO 2.0
487
488 static const char *blend_NORMAL_frag =
489         "       vec4 result = mix(src_color, src_color, src_color.a);\n";
490
491 static const char *blend_ADDITION_frag =
492         "       vec4 result = dst_color + src_color;\n"
493         "       result = clamp(result, 0.0, 1.0);\n";
494
495 static const char *blend_SUBTRACT_frag =
496         "       vec4 result = dst_color - src_color;\n"
497         "       result = clamp(result, 0.0, 1.0);\n";
498
499 static const char *blend_REPLACE_frag =
500         "       vec4 result = src_color;\n";
501
502 GL_STD_FRAG(MULTIPLY);
503 GL_VEC_FRAG(DIVIDE);
504 GL_VEC_FRAG(MAX);
505 GL_VEC_FRAG(MIN);
506 GL_VEC_FRAG(DARKEN);
507 GL_VEC_FRAG(LIGHTEN);
508 GL_STD_FRAG(DST);
509 GL_STD_FRAG(DST_ATOP);
510 GL_STD_FRAG(DST_IN);
511 GL_STD_FRAG(DST_OUT);
512 GL_STD_FRAG(DST_OVER);
513 GL_STD_FRAG(SRC);
514 GL_STD_FRAG(SRC_ATOP);
515 GL_STD_FRAG(SRC_IN);
516 GL_STD_FRAG(SRC_OUT);
517 GL_STD_FRAG(SRC_OVER);
518 GL_STD_FRAG(AND);
519 GL_STD_FRAG(OR);
520 GL_STD_FRAG(XOR);
521 GL_VEC_FRAG(OVERLAY);
522 GL_STD_FRAG(SCREEN);
523 GL_VEC_FRAG(BURN);
524 GL_VEC_FRAG(DODGE);
525 GL_VEC_FRAG(HARDLIGHT);
526 GL_VEC_FRAG(SOFTLIGHT);
527 GL_VEC_FRAG(DIFFERENCE);
528
529 static const char * const overlay_shaders[TRANSFER_TYPES] = {
530         blend_NORMAL_frag,      // TRANSFER_NORMAL
531         blend_ADDITION_frag,    // TRANSFER_ADDITION
532         blend_SUBTRACT_frag,    // TRANSFER_SUBTRACT
533         blend_MULTIPLY_frag,    // TRANSFER_MULTIPLY
534         blend_DIVIDE_frag,      // TRANSFER_DIVIDE
535         blend_REPLACE_frag,     // TRANSFER_REPLACE
536         blend_MAX_frag,         // TRANSFER_MAX
537         blend_MIN_frag,         // TRANSFER_MIN
538         blend_DARKEN_frag,      // TRANSFER_DARKEN
539         blend_LIGHTEN_frag,     // TRANSFER_LIGHTEN
540         blend_DST_frag,         // TRANSFER_DST
541         blend_DST_ATOP_frag,    // TRANSFER_DST_ATOP
542         blend_DST_IN_frag,      // TRANSFER_DST_IN
543         blend_DST_OUT_frag,     // TRANSFER_DST_OUT
544         blend_DST_OVER_frag,    // TRANSFER_DST_OVER
545         blend_SRC_frag,         // TRANSFER_SRC
546         blend_SRC_ATOP_frag,    // TRANSFER_SRC_ATOP
547         blend_SRC_IN_frag,      // TRANSFER_SRC_IN
548         blend_SRC_OUT_frag,     // TRANSFER_SRC_OUT
549         blend_SRC_OVER_frag,    // TRANSFER_SRC_OVER
550         blend_AND_frag,         // TRANSFER_AND
551         blend_OR_frag,          // TRANSFER_OR
552         blend_XOR_frag,         // TRANSFER_XOR
553         blend_OVERLAY_frag,     // TRANSFER_OVERLAY
554         blend_SCREEN_frag,      // TRANSFER_SCREEN
555         blend_BURN_frag,        // TRANSFER_BURN
556         blend_DODGE_frag,       // TRANSFER_DODGE
557         blend_HARDLIGHT_frag,   // TRANSFER_HARDLIGHT
558         blend_SOFTLIGHT_frag,   // TRANSFER_SOFTLIGHT
559         blend_DIFFERENCE_frag,  // TRANSFER_DIFFERENCE
560 };
561
562         glDisable(GL_BLEND);
563         VFrame *dst = get_output(output_layer);
564         VFrame *src = temp;
565
566         switch( config.mode ) {
567         case TRANSFER_REPLACE:
568         case TRANSFER_SRC:
569 // Direct copy layer
570                 src->to_texture();
571                 dst->enable_opengl();
572                 dst->init_screen();
573                 src->draw_texture();
574                 break;
575         case TRANSFER_NORMAL:
576 // Move destination to screen
577                 if( dst->get_opengl_state() != VFrame::SCREEN ) {
578                         dst->to_texture();
579                         dst->enable_opengl();
580                         dst->init_screen();
581                         dst->draw_texture();
582                 }
583                 src->to_texture();
584                 dst->enable_opengl();
585                 dst->init_screen();
586                 glEnable(GL_BLEND);
587                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
588                 src->draw_texture();
589                 break;
590         default:
591                 src->to_texture();
592                 dst->to_texture();
593                 dst->enable_opengl();
594                 dst->init_screen();
595                 src->bind_texture(0);
596                 dst->bind_texture(1);
597
598                 const char *shader_stack[16];
599                 memset(shader_stack,0, sizeof(shader_stack));
600                 int current_shader = 0;
601
602                 shader_stack[current_shader++] = get_pixels_frag;
603                 shader_stack[current_shader++] = overlay_shaders[config.mode];
604                 shader_stack[current_shader++] = put_pixels_frag;
605                 shader_stack[current_shader] = 0;
606                 unsigned int shader = VFrame::make_shader(shader_stack);
607                 if( shader > 0 ) {
608                         glUseProgram(shader);
609                         glUniform1i(glGetUniformLocation(shader, "src_tex"), 0);
610                         glUniform1i(glGetUniformLocation(shader, "dst_tex"), 1);
611                         glUniform2f(glGetUniformLocation(shader, "dst_tex_dimensions"),
612                                         (float)dst->get_texture_w(), (float)dst->get_texture_h());
613                         float chroma_offset = BC_CModels::is_yuv(dst->get_color_model()) ? 0.5 : 0.0;
614                         glUniform3f(glGetUniformLocation(shader, "chroma_offset"),
615                                         0.0, chroma_offset, chroma_offset);
616                 }
617                 src->draw_texture();
618                 glUseProgram(0);
619
620                 glActiveTexture(GL_TEXTURE1);
621                 glDisable(GL_TEXTURE_2D);
622                 break;
623         }
624
625         glActiveTexture(GL_TEXTURE0);
626         glDisable(GL_TEXTURE_2D);
627         glDisable(GL_BLEND);
628
629 // get the data before something else uses the screen
630         dst->screen_to_ram();
631 #endif
632         return 0;
633 }
634
635
636 const char* Overlay::plugin_title() { return N_("Overlay"); }
637 int Overlay::is_realtime() { return 1; }
638 int Overlay::is_multichannel() { return 1; }
639 int Overlay::is_synthesis() { return 1; }
640
641
642
643 NEW_WINDOW_MACRO(Overlay, OverlayWindow)
644
645
646
647 int Overlay::load_configuration()
648 {
649         KeyFrame *prev_keyframe;
650         prev_keyframe = get_prev_keyframe(get_source_position());
651         read_data(prev_keyframe);
652         return 0;
653 }
654
655
656 void Overlay::save_data(KeyFrame *keyframe)
657 {
658         FileXML output;
659
660 // cause data to be stored directly in text
661         output.set_shared_output(keyframe->xbuf);
662         output.tag.set_title("OVERLAY");
663         output.tag.set_property("MODE", config.mode);
664         output.tag.set_property("DIRECTION", config.direction);
665         output.tag.set_property("OUTPUT_LAYER", config.output_layer);
666         output.append_tag();
667         output.tag.set_title("/OVERLAY");
668         output.append_tag();
669         output.terminate_string();
670 }
671
672 void Overlay::read_data(KeyFrame *keyframe)
673 {
674         FileXML input;
675
676         input.set_shared_input(keyframe->xbuf);
677
678         while(!input.read_tag())
679         {
680                 if(input.tag.title_is("OVERLAY"))
681                 {
682                         config.mode = input.tag.get_property("MODE", config.mode);
683                         config.direction = input.tag.get_property("DIRECTION", config.direction);
684                         config.output_layer = input.tag.get_property("OUTPUT_LAYER", config.output_layer);
685                 }
686         }
687 }
688
689 void Overlay::update_gui()
690 {
691         if(thread)
692         {
693                 thread->window->lock_window("Overlay::update_gui");
694                 ((OverlayWindow*)thread->window)->mode->set_text(OverlayConfig::mode_to_text(config.mode));
695                 ((OverlayWindow*)thread->window)->direction->set_text(OverlayConfig::direction_to_text(config.direction));
696                 ((OverlayWindow*)thread->window)->output->set_text(OverlayConfig::output_to_text(config.output_layer));
697                 thread->window->unlock_window();
698         }
699 }
700
701
702
703
704