add BC_SCALE env var for hi def monitors, cleanup theme data
[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         xS(300),
188         yS(160),
189         xS(300),
190         yS(160),
191         0)
192 {
193         this->plugin = plugin;
194 }
195
196 OverlayWindow::~OverlayWindow()
197 {
198 }
199
200 void OverlayWindow::create_objects()
201 {
202         int xs5 = xS(5), xs10 = xS(10);
203         int ys10 = yS(10), ys30 = yS(30);
204         int x = xs10, y = ys10;
205
206         BC_Title *title;
207         add_subwindow(title = new BC_Title(x, y, _("Mode:")));
208         add_subwindow(mode = new OverlayMode(plugin,
209                 x + title->get_w() + xs5,
210                 y));
211         mode->create_objects();
212
213         y += ys30;
214         add_subwindow(title = new BC_Title(x, y, _("Layer order:")));
215         add_subwindow(direction = new OverlayDirection(plugin,
216                 x + title->get_w() + xs5,
217                 y));
218         direction->create_objects();
219
220         y += ys30;
221         add_subwindow(title = new BC_Title(x, y, _("Output layer:")));
222         add_subwindow(output = new OverlayOutput(plugin,
223                 x + title->get_w() + xs5,
224                 y));
225         output->create_objects();
226
227         show_window();
228         flush();
229 }
230
231
232
233
234
235
236
237 OverlayMode::OverlayMode(Overlay *plugin, int x, int y)
238  : BC_PopupMenu(x, y, xS(150),
239         OverlayConfig::mode_to_text(plugin->config.mode), 1)
240 {
241         this->plugin = plugin;
242 }
243
244 void OverlayMode::create_objects()
245 {
246         for(int i = 0; i < TRANSFER_TYPES; i++)
247                 add_item(new BC_MenuItem(OverlayConfig::mode_to_text(i)));
248 }
249
250 int OverlayMode::handle_event()
251 {
252         char *text = get_text();
253
254         for(int i = 0; i < TRANSFER_TYPES; i++)
255         {
256                 if(!strcmp(text, OverlayConfig::mode_to_text(i)))
257                 {
258                         plugin->config.mode = i;
259                         break;
260                 }
261         }
262
263         plugin->send_configure_change();
264         return 1;
265 }
266
267
268 OverlayDirection::OverlayDirection(Overlay *plugin,
269         int x,
270         int y)
271  : BC_PopupMenu(x, y, xS(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, y, xS(100),
311         OverlayConfig::output_to_text(plugin->config.output_layer),
312         1)
313 {
314         this->plugin = plugin;
315 }
316
317 void OverlayOutput::create_objects()
318 {
319         add_item(new BC_MenuItem(
320                 OverlayConfig::output_to_text(
321                         OverlayConfig::TOP)));
322         add_item(new BC_MenuItem(
323                 OverlayConfig::output_to_text(
324                         OverlayConfig::BOTTOM)));
325 }
326
327 int OverlayOutput::handle_event()
328 {
329         char *text = get_text();
330
331         if(!strcmp(text,
332                 OverlayConfig::output_to_text(
333                         OverlayConfig::TOP)))
334                 plugin->config.output_layer = OverlayConfig::TOP;
335         else
336         if(!strcmp(text,
337                 OverlayConfig::output_to_text(
338                         OverlayConfig::BOTTOM)))
339                 plugin->config.output_layer = OverlayConfig::BOTTOM;
340
341         plugin->send_configure_change();
342         return 1;
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 REGISTER_PLUGIN(Overlay)
365
366
367
368
369
370
371 Overlay::Overlay(PluginServer *server)
372  : PluginVClient(server)
373 {
374
375         overlayer = 0;
376         temp = 0;
377 }
378
379
380 Overlay::~Overlay()
381 {
382
383         if(overlayer) delete overlayer;
384         if(temp) delete temp;
385 }
386
387
388
389 int Overlay::process_buffer(VFrame **frame,
390         int64_t start_position,
391         double frame_rate)
392 {
393         load_configuration();
394
395         EDLSession* session = get_edl()->session;
396         int interpolation_type = session ? session->interpolation_type : NEAREST_NEIGHBOR;
397
398         int step = config.direction == OverlayConfig::BOTTOM_FIRST ?  -1 : 1;
399         int layers = get_total_buffers();
400         input_layer = config.direction == OverlayConfig::BOTTOM_FIRST ? layers-1 : 0;
401         output_layer = config.output_layer == OverlayConfig::TOP ?  0 : layers-1;
402         VFrame *output = frame[output_layer];
403
404         current_layer = input_layer;
405         read_frame(output, current_layer,  // Direct copy the first layer
406                          start_position, frame_rate, get_use_opengl());
407
408         if( --layers > 0 ) {    // need 2 layers to do overlay
409                 if( !temp )
410                         temp = new VFrame(frame[0]->get_w(), frame[0]->get_h(),
411                                         frame[0]->get_color_model(), 0);
412
413                 while( --layers >= 0 ) {
414                         current_layer += step;
415                         read_frame(temp, current_layer,
416                                  start_position, frame_rate, get_use_opengl());
417
418                         if(get_use_opengl()) {
419                                 run_opengl();
420                                 continue;
421                         }
422
423                         if(!overlayer)
424                                 overlayer = new OverlayFrame(get_project_smp() + 1);
425
426                         overlayer->overlay(output, temp,
427                                 0, 0, output->get_w(), output->get_h(),
428                                 0, 0, output->get_w(), output->get_h(),
429                                 1, config.mode, interpolation_type);
430                 }
431         }
432
433         return 0;
434 }
435
436 int Overlay::handle_opengl()
437 {
438 #ifdef HAVE_GL
439         static const char *get_pixels_frag =
440                 "uniform sampler2D src_tex;\n"
441                 "uniform sampler2D dst_tex;\n"
442                 "uniform vec2 dst_tex_dimensions;\n"
443                 "uniform vec3 chroma_offset;\n"
444                 "void main()\n"
445                 "{\n"
446                 "       vec4 dst_color = texture2D(dst_tex, gl_FragCoord.xy / dst_tex_dimensions);\n"
447                 "       vec4 src_color = texture2D(src_tex, gl_TexCoord[0].st);\n"
448                 "       src_color.rgb -= chroma_offset;\n"
449                 "       dst_color.rgb -= chroma_offset;\n";
450
451         static const char *put_pixels_frag =
452                 "       result.rgb += chroma_offset;\n"
453                 "       gl_FragColor = result;\n"
454                 "}\n";
455
456 #define QQ(q)#q
457 #define SS(s)QQ(s)
458
459 #define GL_STD_FRAG(FN) static const char *blend_##FN##_frag = \
460         "       vec4 result;\n" \
461         "       result.rgb = " SS(COLOR_##FN(1.0, src_color.rgb, src_color.a, dst_color.rgb, dst_color.a)) ";\n" \
462         "       result.a = " SS(ALPHA_##FN(1.0, src_color.a, dst_color.a))";\n" \
463
464 #define GL_VEC_FRAG(FN) static const char *blend_##FN##_frag = \
465         "       vec4 result;\n" \
466         "       result.r = " SS(COLOR_##FN(1.0, src_color.r, src_color.a, dst_color.r, dst_color.a)) ";\n" \
467         "       result.g = " SS(COLOR_##FN(1.0, src_color.g, src_color.a, dst_color.g, dst_color.a)) ";\n" \
468         "       result.b = " SS(COLOR_##FN(1.0, src_color.b, src_color.a, dst_color.b, dst_color.a)) ";\n" \
469         "       result.a = " SS(ALPHA_##FN(1.0, src_color.a, dst_color.a)) ";\n" \
470         "       result = clamp(result, 0.0, 1.0);\n" \
471
472 #undef mabs
473 #define mabs abs
474 #undef mmin
475 #define mmin min
476 #undef mmax
477 #define mmax max
478
479 #undef ZERO
480 #define ZERO 0.0
481 #undef ONE
482 #define ONE 1.0
483 #undef TWO
484 #define TWO 2.0
485
486 static const char *blend_REPLACE_frag =
487         "       vec4 result = src_color;\n";
488
489 GL_VEC_FRAG(NORMAL);
490 GL_VEC_FRAG(ADDITION);
491 GL_VEC_FRAG(SUBTRACT);
492 GL_STD_FRAG(MULTIPLY);
493 GL_VEC_FRAG(DIVIDE);
494 GL_VEC_FRAG(MAX);
495 GL_VEC_FRAG(MIN);
496 GL_VEC_FRAG(DARKEN);
497 GL_VEC_FRAG(LIGHTEN);
498 GL_STD_FRAG(DST);
499 GL_STD_FRAG(DST_ATOP);
500 GL_STD_FRAG(DST_IN);
501 GL_STD_FRAG(DST_OUT);
502 GL_STD_FRAG(DST_OVER);
503 GL_STD_FRAG(SRC);
504 GL_STD_FRAG(SRC_ATOP);
505 GL_STD_FRAG(SRC_IN);
506 GL_STD_FRAG(SRC_OUT);
507 GL_STD_FRAG(SRC_OVER);
508 GL_STD_FRAG(AND);
509 GL_STD_FRAG(OR);
510 GL_STD_FRAG(XOR);
511 GL_VEC_FRAG(OVERLAY);
512 GL_STD_FRAG(SCREEN);
513 GL_VEC_FRAG(BURN);
514 GL_VEC_FRAG(DODGE);
515 GL_VEC_FRAG(HARDLIGHT);
516 GL_VEC_FRAG(SOFTLIGHT);
517 GL_VEC_FRAG(DIFFERENCE);
518
519 static const char * const overlay_shaders[TRANSFER_TYPES] = {
520         blend_NORMAL_frag,      // TRANSFER_NORMAL
521         blend_ADDITION_frag,    // TRANSFER_ADDITION
522         blend_SUBTRACT_frag,    // TRANSFER_SUBTRACT
523         blend_MULTIPLY_frag,    // TRANSFER_MULTIPLY
524         blend_DIVIDE_frag,      // TRANSFER_DIVIDE
525         blend_REPLACE_frag,     // TRANSFER_REPLACE
526         blend_MAX_frag,         // TRANSFER_MAX
527         blend_MIN_frag,         // TRANSFER_MIN
528         blend_DARKEN_frag,      // TRANSFER_DARKEN
529         blend_LIGHTEN_frag,     // TRANSFER_LIGHTEN
530         blend_DST_frag,         // TRANSFER_DST
531         blend_DST_ATOP_frag,    // TRANSFER_DST_ATOP
532         blend_DST_IN_frag,      // TRANSFER_DST_IN
533         blend_DST_OUT_frag,     // TRANSFER_DST_OUT
534         blend_DST_OVER_frag,    // TRANSFER_DST_OVER
535         blend_SRC_frag,         // TRANSFER_SRC
536         blend_SRC_ATOP_frag,    // TRANSFER_SRC_ATOP
537         blend_SRC_IN_frag,      // TRANSFER_SRC_IN
538         blend_SRC_OUT_frag,     // TRANSFER_SRC_OUT
539         blend_SRC_OVER_frag,    // TRANSFER_SRC_OVER
540         blend_AND_frag,         // TRANSFER_AND
541         blend_OR_frag,          // TRANSFER_OR
542         blend_XOR_frag,         // TRANSFER_XOR
543         blend_OVERLAY_frag,     // TRANSFER_OVERLAY
544         blend_SCREEN_frag,      // TRANSFER_SCREEN
545         blend_BURN_frag,        // TRANSFER_BURN
546         blend_DODGE_frag,       // TRANSFER_DODGE
547         blend_HARDLIGHT_frag,   // TRANSFER_HARDLIGHT
548         blend_SOFTLIGHT_frag,   // TRANSFER_SOFTLIGHT
549         blend_DIFFERENCE_frag,  // TRANSFER_DIFFERENCE
550 };
551
552         glDisable(GL_BLEND);
553         VFrame *dst = get_output(output_layer);
554         VFrame *src = temp;
555
556         switch( config.mode ) {
557         case TRANSFER_REPLACE:
558         case TRANSFER_SRC:
559 // Direct copy layer
560                 src->to_texture();
561                 dst->enable_opengl();
562                 dst->init_screen();
563                 src->draw_texture();
564                 break;
565         default:
566                 src->to_texture();
567                 dst->to_texture();
568                 dst->enable_opengl();
569                 dst->init_screen();
570                 src->bind_texture(0);
571                 dst->bind_texture(1);
572
573                 const char *shader_stack[16];
574                 memset(shader_stack,0, sizeof(shader_stack));
575                 int current_shader = 0;
576
577                 shader_stack[current_shader++] = get_pixels_frag;
578                 shader_stack[current_shader++] = overlay_shaders[config.mode];
579                 shader_stack[current_shader++] = put_pixels_frag;
580                 shader_stack[current_shader] = 0;
581                 unsigned int shader = VFrame::make_shader(shader_stack);
582                 if( shader > 0 ) {
583                         glUseProgram(shader);
584                         glUniform1i(glGetUniformLocation(shader, "src_tex"), 0);
585                         glUniform1i(glGetUniformLocation(shader, "dst_tex"), 1);
586                         glUniform2f(glGetUniformLocation(shader, "dst_tex_dimensions"),
587                                         (float)dst->get_texture_w(), (float)dst->get_texture_h());
588                         float chroma_offset = BC_CModels::is_yuv(dst->get_color_model()) ? 0.5 : 0.0;
589                         glUniform3f(glGetUniformLocation(shader, "chroma_offset"),
590                                         0.0, chroma_offset, chroma_offset);
591                 }
592                 src->draw_texture();
593                 glUseProgram(0);
594
595                 glActiveTexture(GL_TEXTURE1);
596                 glDisable(GL_TEXTURE_2D);
597                 break;
598         }
599
600         glActiveTexture(GL_TEXTURE0);
601         glDisable(GL_TEXTURE_2D);
602         glDisable(GL_BLEND);
603
604 // get the data before something else uses the screen
605         dst->screen_to_ram();
606 #endif
607         return 0;
608 }
609
610
611 const char* Overlay::plugin_title() { return N_("Overlay"); }
612 int Overlay::is_realtime() { return 1; }
613 int Overlay::is_multichannel() { return 1; }
614 int Overlay::is_synthesis() { return 1; }
615
616
617
618 NEW_WINDOW_MACRO(Overlay, OverlayWindow)
619
620
621
622 int Overlay::load_configuration()
623 {
624         KeyFrame *prev_keyframe;
625         prev_keyframe = get_prev_keyframe(get_source_position());
626         read_data(prev_keyframe);
627         return 0;
628 }
629
630
631 void Overlay::save_data(KeyFrame *keyframe)
632 {
633         FileXML output;
634
635 // cause data to be stored directly in text
636         output.set_shared_output(keyframe->xbuf);
637         output.tag.set_title("OVERLAY");
638         output.tag.set_property("MODE", config.mode);
639         output.tag.set_property("DIRECTION", config.direction);
640         output.tag.set_property("OUTPUT_LAYER", config.output_layer);
641         output.append_tag();
642         output.tag.set_title("/OVERLAY");
643         output.append_tag();
644         output.terminate_string();
645 }
646
647 void Overlay::read_data(KeyFrame *keyframe)
648 {
649         FileXML input;
650
651         input.set_shared_input(keyframe->xbuf);
652
653         while(!input.read_tag())
654         {
655                 if(input.tag.title_is("OVERLAY"))
656                 {
657                         config.mode = input.tag.get_property("MODE", config.mode);
658                         config.direction = input.tag.get_property("DIRECTION", config.direction);
659                         config.output_layer = input.tag.get_property("OUTPUT_LAYER", config.output_layer);
660                 }
661         }
662 }
663
664 void Overlay::update_gui()
665 {
666         if(thread)
667         {
668                 thread->window->lock_window("Overlay::update_gui");
669                 ((OverlayWindow*)thread->window)->mode->set_text(OverlayConfig::mode_to_text(config.mode));
670                 ((OverlayWindow*)thread->window)->direction->set_text(OverlayConfig::direction_to_text(config.direction));
671                 ((OverlayWindow*)thread->window)->output->set_text(OverlayConfig::output_to_text(config.output_layer));
672                 thread->window->unlock_window();
673         }
674 }
675
676
677
678
679