4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcdisplayinfo.h"
29 #include "overlayframe.h"
30 #include "pluginvclient.h"
46 static const char* mode_to_text(int mode);
49 static const char* direction_to_text(int direction);
57 static const char* output_to_text(int output_layer);
66 class OverlayMode : public BC_PopupMenu
69 OverlayMode(Overlay *plugin,
72 void create_objects();
77 class OverlayDirection : public BC_PopupMenu
80 OverlayDirection(Overlay *plugin,
83 void create_objects();
88 class OverlayOutput : public BC_PopupMenu
91 OverlayOutput(Overlay *plugin,
94 void create_objects();
100 class OverlayWindow : public PluginClientWindow
103 OverlayWindow(Overlay *plugin);
106 void create_objects();
111 OverlayDirection *direction;
112 OverlayOutput *output;
115 class Overlay : public PluginVClient
118 Overlay(PluginServer *server);
122 PLUGIN_CLASS_MEMBERS(OverlayConfig);
124 int process_buffer(VFrame **frame,
125 int64_t start_position,
128 int is_multichannel();
130 void save_data(KeyFrame *keyframe);
131 void read_data(KeyFrame *keyframe);
135 OverlayFrame *overlayer;
139 // Inclusive layer numbers
144 OverlayConfig::OverlayConfig()
146 mode = TRANSFER_NORMAL;
147 direction = OverlayConfig::BOTTOM_FIRST;
148 output_layer = OverlayConfig::TOP;
151 const char* OverlayConfig::mode_to_text(int mode)
154 case TRANSFER_NORMAL: return _("Normal");
155 case TRANSFER_ADDITION: return _("Addition");
156 case TRANSFER_SUBTRACT: return _("Subtract");
157 case TRANSFER_MULTIPLY: return _("Multiply");
158 case TRANSFER_DIVIDE: return _("Divide");
159 case TRANSFER_REPLACE: return _("Replace");
160 case TRANSFER_MAX: return _("Max");
161 case TRANSFER_MIN: return _("Min");
162 case TRANSFER_AVERAGE: return _("Average");
163 case TRANSFER_DARKEN: return _("Darken");
164 case TRANSFER_LIGHTEN: return _("Lighten");
165 case TRANSFER_DST: return _("Dst");
166 case TRANSFER_DST_ATOP: return _("DstAtop");
167 case TRANSFER_DST_IN: return _("DstIn");
168 case TRANSFER_DST_OUT: return _("DstOut");
169 case TRANSFER_DST_OVER: return _("DstOver");
170 case TRANSFER_SRC: return _("Src");
171 case TRANSFER_SRC_ATOP: return _("SrcAtop");
172 case TRANSFER_SRC_IN: return _("SrcIn");
173 case TRANSFER_SRC_OUT: return _("SrcOut");
174 case TRANSFER_SRC_OVER: return _("SrcOver");
175 case TRANSFER_OR: return _("Or");
176 case TRANSFER_XOR: return _("Xor");
182 const char* OverlayConfig::direction_to_text(int direction)
186 case OverlayConfig::BOTTOM_FIRST: return _("Bottom first");
187 case OverlayConfig::TOP_FIRST: return _("Top first");
192 const char* OverlayConfig::output_to_text(int output_layer)
196 case OverlayConfig::TOP: return _("Top");
197 case OverlayConfig::BOTTOM: return _("Bottom");
210 OverlayWindow::OverlayWindow(Overlay *plugin)
211 : PluginClientWindow(plugin,
218 this->plugin = plugin;
221 OverlayWindow::~OverlayWindow()
225 void OverlayWindow::create_objects()
230 add_subwindow(title = new BC_Title(x, y, _("Mode:")));
231 add_subwindow(mode = new OverlayMode(plugin,
232 x + title->get_w() + 5,
234 mode->create_objects();
237 add_subwindow(title = new BC_Title(x, y, _("Layer order:")));
238 add_subwindow(direction = new OverlayDirection(plugin,
239 x + title->get_w() + 5,
241 direction->create_objects();
244 add_subwindow(title = new BC_Title(x, y, _("Output layer:")));
245 add_subwindow(output = new OverlayOutput(plugin,
246 x + title->get_w() + 5,
248 output->create_objects();
260 OverlayMode::OverlayMode(Overlay *plugin, int x, int y)
261 : BC_PopupMenu(x, y, 150,
262 OverlayConfig::mode_to_text(plugin->config.mode), 1)
264 this->plugin = plugin;
267 void OverlayMode::create_objects()
269 for(int i = 0; i < TRANSFER_TYPES; i++)
270 add_item(new BC_MenuItem(OverlayConfig::mode_to_text(i)));
273 int OverlayMode::handle_event()
275 char *text = get_text();
277 for(int i = 0; i < TRANSFER_TYPES; i++)
279 if(!strcmp(text, OverlayConfig::mode_to_text(i)))
281 plugin->config.mode = i;
286 plugin->send_configure_change();
291 OverlayDirection::OverlayDirection(Overlay *plugin,
297 OverlayConfig::direction_to_text(plugin->config.direction),
300 this->plugin = plugin;
303 void OverlayDirection::create_objects()
305 add_item(new BC_MenuItem(
306 OverlayConfig::direction_to_text(
307 OverlayConfig::TOP_FIRST)));
308 add_item(new BC_MenuItem(
309 OverlayConfig::direction_to_text(
310 OverlayConfig::BOTTOM_FIRST)));
313 int OverlayDirection::handle_event()
315 char *text = get_text();
318 OverlayConfig::direction_to_text(
319 OverlayConfig::TOP_FIRST)))
320 plugin->config.direction = OverlayConfig::TOP_FIRST;
323 OverlayConfig::direction_to_text(
324 OverlayConfig::BOTTOM_FIRST)))
325 plugin->config.direction = OverlayConfig::BOTTOM_FIRST;
327 plugin->send_configure_change();
332 OverlayOutput::OverlayOutput(Overlay *plugin,
338 OverlayConfig::output_to_text(plugin->config.output_layer),
341 this->plugin = plugin;
344 void OverlayOutput::create_objects()
346 add_item(new BC_MenuItem(
347 OverlayConfig::output_to_text(
348 OverlayConfig::TOP)));
349 add_item(new BC_MenuItem(
350 OverlayConfig::output_to_text(
351 OverlayConfig::BOTTOM)));
354 int OverlayOutput::handle_event()
356 char *text = get_text();
359 OverlayConfig::output_to_text(
360 OverlayConfig::TOP)))
361 plugin->config.output_layer = OverlayConfig::TOP;
364 OverlayConfig::output_to_text(
365 OverlayConfig::BOTTOM)))
366 plugin->config.output_layer = OverlayConfig::BOTTOM;
368 plugin->send_configure_change();
391 REGISTER_PLUGIN(Overlay)
398 Overlay::Overlay(PluginServer *server)
399 : PluginVClient(server)
410 if(overlayer) delete overlayer;
411 if(temp) delete temp;
416 int Overlay::process_buffer(VFrame **frame,
417 int64_t start_position,
420 load_configuration();
423 if(!temp) temp = new VFrame(0,
427 frame[0]->get_color_model(),
431 overlayer = new OverlayFrame(get_project_smp() + 1);
436 if(config.direction == OverlayConfig::BOTTOM_FIRST)
438 input_layer1 = get_total_buffers() - 1;
445 input_layer2 = get_total_buffers();
449 if(config.output_layer == OverlayConfig::TOP)
455 output_layer = get_total_buffers() - 1;
460 // Direct copy the first layer
461 output = frame[output_layer];
468 if(get_total_buffers() == 1) return 0;
472 current_layer = input_layer1;
476 for(int i = input_layer1 + step; i != input_layer2; i += step)
484 // Call the opengl handler once for each layer
492 overlayer->overlay(output,
512 int Overlay::handle_opengl()
515 static const char *get_pixels_frag =
516 "uniform sampler2D src_tex;\n"
517 "uniform sampler2D dst_tex;\n"
518 "uniform vec2 dst_tex_dimensions;\n"
519 "uniform vec3 chroma_offset;\n"
522 " vec4 result_color;\n"
523 " vec4 dst_color = texture2D(dst_tex, gl_FragCoord.xy / dst_tex_dimensions);\n"
524 " vec4 src_color = texture2D(src_tex, gl_TexCoord[0].st);\n"
525 " src_color.rgb -= chroma_offset;\n"
526 " dst_color.rgb -= chroma_offset;\n";
528 static const char *put_pixels_frag =
529 " result_color.rgb += chroma_offset;\n"
530 " result_color.rgb = mix(dst_color.rgb, result_color.rgb, src_color.a);\n"
531 " result_color.a = max(src_color.a, dst_color.a);\n"
532 " gl_FragColor = result_color;\n"
535 static const char *blend_add_frag =
536 " result_color.rgb = dst_color.rgb + src_color.rgb;\n";
538 static const char *blend_max_frag =
539 " result_color.r = max(abs(dst_color.r, src_color.r);\n"
540 " result_color.g = max(abs(dst_color.g, src_color.g);\n"
541 " result_color.b = max(abs(dst_color.b, src_color.b);\n";
543 static const char *blend_min_frag =
544 " result_color.r = min(abs(dst_color.r, src_color.r);\n"
545 " result_color.g = min(abs(dst_color.g, src_color.g);\n"
546 " result_color.b = min(abs(dst_color.b, src_color.b);\n";
548 static const char *blend_subtract_frag =
549 " result_color.rgb = dst_color.rgb - src_color.rgb;\n";
552 static const char *blend_multiply_frag =
553 " result_color.rgb = dst_color.rgb * src_color.rgb;\n";
555 static const char *blend_divide_frag =
556 " result_color.rgb = dst_color.rgb / src_color.rgb;\n"
557 " if(src_color.r == 0.0) result_color.r = 1.0;\n"
558 " if(src_color.g == 0.0) result_color.g = 1.0;\n"
559 " if(src_color.b == 0.0) result_color.b = 1.0;\n";
563 VFrame *dst = get_output(output_layer);
565 dst->enable_opengl();
568 const char *shader_stack[] = { 0, 0, 0 };
569 int current_shader = 0;
575 if(config.mode == TRANSFER_REPLACE)
578 src->bind_texture(0);
579 dst->enable_opengl();
587 if(config.mode == TRANSFER_NORMAL)
589 dst->enable_opengl();
592 // Move destination to screen
593 if(dst->get_opengl_state() != VFrame::SCREEN)
596 dst->bind_texture(0);
601 src->bind_texture(0);
602 dst->enable_opengl();
606 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
611 // Read destination back to texture
614 src->enable_opengl();
618 dst->enable_opengl();
620 src->bind_texture(0);
621 dst->bind_texture(1);
624 shader_stack[current_shader++] = get_pixels_frag;
628 case TRANSFER_ADDITION:
629 shader_stack[current_shader++] = blend_add_frag;
631 case TRANSFER_SUBTRACT:
632 shader_stack[current_shader++] = blend_subtract_frag;
634 case TRANSFER_MULTIPLY:
635 shader_stack[current_shader++] = blend_multiply_frag;
637 case TRANSFER_DIVIDE:
638 shader_stack[current_shader++] = blend_divide_frag;
641 shader_stack[current_shader++] = blend_max_frag;
644 shader_stack[current_shader++] = blend_min_frag;
648 shader_stack[current_shader++] = put_pixels_frag;
650 unsigned int shader_id = 0;
651 shader_id = VFrame::make_shader(0,
657 glUseProgram(shader_id);
658 glUniform1i(glGetUniformLocation(shader_id, "src_tex"), 0);
659 glUniform1i(glGetUniformLocation(shader_id, "dst_tex"), 1);
660 if(BC_CModels::is_yuv(dst->get_color_model()))
661 glUniform3f(glGetUniformLocation(shader_id, "chroma_offset"), 0.0, 0.5, 0.5);
663 glUniform3f(glGetUniformLocation(shader_id, "chroma_offset"), 0.0, 0.0, 0.0);
664 glUniform2f(glGetUniformLocation(shader_id, "dst_tex_dimensions"),
665 (float)dst->get_texture_w(),
666 (float)dst->get_texture_h());
674 glActiveTexture(GL_TEXTURE1);
675 glDisable(GL_TEXTURE_2D);
676 glActiveTexture(GL_TEXTURE0);
677 glDisable(GL_TEXTURE_2D);
679 dst->set_opengl_state(VFrame::SCREEN);
685 const char* Overlay::plugin_title() { return _("Overlay"); }
686 int Overlay::is_realtime() { return 1; }
687 int Overlay::is_multichannel() { return 1; }
688 int Overlay::is_synthesis() { return 1; }
692 NEW_WINDOW_MACRO(Overlay, OverlayWindow)
696 int Overlay::load_configuration()
698 KeyFrame *prev_keyframe;
699 prev_keyframe = get_prev_keyframe(get_source_position());
700 read_data(prev_keyframe);
705 void Overlay::save_data(KeyFrame *keyframe)
709 // cause data to be stored directly in text
710 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
711 output.tag.set_title("OVERLAY");
712 output.tag.set_property("MODE", config.mode);
713 output.tag.set_property("DIRECTION", config.direction);
714 output.tag.set_property("OUTPUT_LAYER", config.output_layer);
716 output.tag.set_title("/OVERLAY");
718 output.terminate_string();
721 void Overlay::read_data(KeyFrame *keyframe)
725 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
727 while(!input.read_tag())
729 if(input.tag.title_is("OVERLAY"))
731 config.mode = input.tag.get_property("MODE", config.mode);
732 config.direction = input.tag.get_property("DIRECTION", config.direction);
733 config.output_layer = input.tag.get_property("OUTPUT_LAYER", config.output_layer);
738 void Overlay::update_gui()
742 thread->window->lock_window("Overlay::update_gui");
743 ((OverlayWindow*)thread->window)->mode->set_text(OverlayConfig::mode_to_text(config.mode));
744 ((OverlayWindow*)thread->window)->direction->set_text(OverlayConfig::direction_to_text(config.direction));
745 ((OverlayWindow*)thread->window)->output->set_text(OverlayConfig::output_to_text(config.output_layer));
746 thread->window->unlock_window();