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"
48 static const char* mode_to_text(int mode);
51 static const char* direction_to_text(int direction);
59 static const char* output_to_text(int output_layer);
72 class OverlayMode : public BC_PopupMenu
75 OverlayMode(Overlay *plugin,
78 void create_objects();
83 class OverlayDirection : public BC_PopupMenu
86 OverlayDirection(Overlay *plugin,
89 void create_objects();
94 class OverlayOutput : public BC_PopupMenu
97 OverlayOutput(Overlay *plugin,
100 void create_objects();
106 class OverlayWindow : public PluginClientWindow
109 OverlayWindow(Overlay *plugin);
112 void create_objects();
117 OverlayDirection *direction;
118 OverlayOutput *output;
126 class Overlay : public PluginVClient
129 Overlay(PluginServer *server);
133 PLUGIN_CLASS_MEMBERS(OverlayConfig);
135 int process_buffer(VFrame **frame,
136 int64_t start_position,
139 int is_multichannel();
141 void save_data(KeyFrame *keyframe);
142 void read_data(KeyFrame *keyframe);
146 OverlayFrame *overlayer;
150 // Inclusive layer numbers
166 OverlayConfig::OverlayConfig()
168 mode = TRANSFER_NORMAL;
169 direction = OverlayConfig::BOTTOM_FIRST;
170 output_layer = OverlayConfig::TOP;
173 const char* OverlayConfig::mode_to_text(int mode)
177 case TRANSFER_NORMAL:
181 case TRANSFER_REPLACE:
185 case TRANSFER_ADDITION:
186 return _("Addition");
189 case TRANSFER_SUBTRACT:
190 return _("Subtract");
193 case TRANSFER_MULTIPLY:
194 return _("Multiply");
197 case TRANSFER_DIVIDE:
216 const char* OverlayConfig::direction_to_text(int direction)
220 case OverlayConfig::BOTTOM_FIRST: return _("Bottom first");
221 case OverlayConfig::TOP_FIRST: return _("Top first");
226 const char* OverlayConfig::output_to_text(int output_layer)
230 case OverlayConfig::TOP: return _("Top");
231 case OverlayConfig::BOTTOM: return _("Bottom");
244 OverlayWindow::OverlayWindow(Overlay *plugin)
245 : PluginClientWindow(plugin,
252 this->plugin = plugin;
255 OverlayWindow::~OverlayWindow()
259 void OverlayWindow::create_objects()
264 add_subwindow(title = new BC_Title(x, y, _("Mode:")));
265 add_subwindow(mode = new OverlayMode(plugin,
266 x + title->get_w() + 5,
268 mode->create_objects();
271 add_subwindow(title = new BC_Title(x, y, _("Layer order:")));
272 add_subwindow(direction = new OverlayDirection(plugin,
273 x + title->get_w() + 5,
275 direction->create_objects();
278 add_subwindow(title = new BC_Title(x, y, _("Output layer:")));
279 add_subwindow(output = new OverlayOutput(plugin,
280 x + title->get_w() + 5,
282 output->create_objects();
294 OverlayMode::OverlayMode(Overlay *plugin,
300 OverlayConfig::mode_to_text(plugin->config.mode),
303 this->plugin = plugin;
306 void OverlayMode::create_objects()
308 for(int i = 0; i < TRANSFER_TYPES; i++)
309 add_item(new BC_MenuItem(OverlayConfig::mode_to_text(i)));
312 int OverlayMode::handle_event()
314 char *text = get_text();
316 for(int i = 0; i < TRANSFER_TYPES; i++)
318 if(!strcmp(text, OverlayConfig::mode_to_text(i)))
320 plugin->config.mode = i;
325 plugin->send_configure_change();
330 OverlayDirection::OverlayDirection(Overlay *plugin,
336 OverlayConfig::direction_to_text(plugin->config.direction),
339 this->plugin = plugin;
342 void OverlayDirection::create_objects()
344 add_item(new BC_MenuItem(
345 OverlayConfig::direction_to_text(
346 OverlayConfig::TOP_FIRST)));
347 add_item(new BC_MenuItem(
348 OverlayConfig::direction_to_text(
349 OverlayConfig::BOTTOM_FIRST)));
352 int OverlayDirection::handle_event()
354 char *text = get_text();
357 OverlayConfig::direction_to_text(
358 OverlayConfig::TOP_FIRST)))
359 plugin->config.direction = OverlayConfig::TOP_FIRST;
362 OverlayConfig::direction_to_text(
363 OverlayConfig::BOTTOM_FIRST)))
364 plugin->config.direction = OverlayConfig::BOTTOM_FIRST;
366 plugin->send_configure_change();
371 OverlayOutput::OverlayOutput(Overlay *plugin,
377 OverlayConfig::output_to_text(plugin->config.output_layer),
380 this->plugin = plugin;
383 void OverlayOutput::create_objects()
385 add_item(new BC_MenuItem(
386 OverlayConfig::output_to_text(
387 OverlayConfig::TOP)));
388 add_item(new BC_MenuItem(
389 OverlayConfig::output_to_text(
390 OverlayConfig::BOTTOM)));
393 int OverlayOutput::handle_event()
395 char *text = get_text();
398 OverlayConfig::output_to_text(
399 OverlayConfig::TOP)))
400 plugin->config.output_layer = OverlayConfig::TOP;
403 OverlayConfig::output_to_text(
404 OverlayConfig::BOTTOM)))
405 plugin->config.output_layer = OverlayConfig::BOTTOM;
407 plugin->send_configure_change();
430 REGISTER_PLUGIN(Overlay)
437 Overlay::Overlay(PluginServer *server)
438 : PluginVClient(server)
449 if(overlayer) delete overlayer;
450 if(temp) delete temp;
455 int Overlay::process_buffer(VFrame **frame,
456 int64_t start_position,
459 load_configuration();
462 printf("Overlay::process_buffer mode=%d\n", config.mode);
463 if(!temp) temp = new VFrame(0,
467 frame[0]->get_color_model(),
471 overlayer = new OverlayFrame(get_project_smp() + 1);
476 if(config.direction == OverlayConfig::BOTTOM_FIRST)
478 input_layer1 = get_total_buffers() - 1;
485 input_layer2 = get_total_buffers();
489 if(config.output_layer == OverlayConfig::TOP)
495 output_layer = get_total_buffers() - 1;
500 // Direct copy the first layer
501 output = frame[output_layer];
508 if(get_total_buffers() == 1) return 0;
512 current_layer = input_layer1;
516 for(int i = input_layer1 + step; i != input_layer2; i += step)
524 // Call the opengl handler once for each layer
532 overlayer->overlay(output,
552 int Overlay::handle_opengl()
555 static const char *get_pixels_frag =
556 "uniform sampler2D src_tex;\n"
557 "uniform sampler2D dst_tex;\n"
558 "uniform vec2 dst_tex_dimensions;\n"
559 "uniform vec3 chroma_offset;\n"
562 " vec4 result_color;\n"
563 " vec4 dst_color = texture2D(dst_tex, gl_FragCoord.xy / dst_tex_dimensions);\n"
564 " vec4 src_color = texture2D(src_tex, gl_TexCoord[0].st);\n"
565 " src_color.rgb -= chroma_offset;\n"
566 " dst_color.rgb -= chroma_offset;\n";
568 static const char *put_pixels_frag =
569 " result_color.rgb += chroma_offset;\n"
570 " result_color.rgb = mix(dst_color.rgb, result_color.rgb, src_color.a);\n"
571 " result_color.a = max(src_color.a, dst_color.a);\n"
572 " gl_FragColor = result_color;\n"
575 static const char *blend_add_frag =
576 " result_color.rgb = dst_color.rgb + src_color.rgb;\n";
578 static const char *blend_max_frag =
579 " result_color.r = max(abs(dst_color.r, src_color.r);\n"
580 " result_color.g = max(abs(dst_color.g, src_color.g);\n"
581 " result_color.b = max(abs(dst_color.b, src_color.b);\n";
583 static const char *blend_min_frag =
584 " result_color.r = min(abs(dst_color.r, src_color.r);\n"
585 " result_color.g = min(abs(dst_color.g, src_color.g);\n"
586 " result_color.b = min(abs(dst_color.b, src_color.b);\n";
588 static const char *blend_subtract_frag =
589 " result_color.rgb = dst_color.rgb - src_color.rgb;\n";
592 static const char *blend_multiply_frag =
593 " result_color.rgb = dst_color.rgb * src_color.rgb;\n";
595 static const char *blend_divide_frag =
596 " result_color.rgb = dst_color.rgb / src_color.rgb;\n"
597 " if(src_color.r == 0.0) result_color.r = 1.0;\n"
598 " if(src_color.g == 0.0) result_color.g = 1.0;\n"
599 " if(src_color.b == 0.0) result_color.b = 1.0;\n";
603 VFrame *dst = get_output(output_layer);
605 dst->enable_opengl();
608 const char *shader_stack[] = { 0, 0, 0 };
609 int current_shader = 0;
615 if(config.mode == TRANSFER_REPLACE)
618 src->bind_texture(0);
619 dst->enable_opengl();
627 if(config.mode == TRANSFER_NORMAL)
629 dst->enable_opengl();
632 // Move destination to screen
633 if(dst->get_opengl_state() != VFrame::SCREEN)
636 dst->bind_texture(0);
641 src->bind_texture(0);
642 dst->enable_opengl();
646 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
651 // Read destination back to texture
654 src->enable_opengl();
658 dst->enable_opengl();
660 src->bind_texture(0);
661 dst->bind_texture(1);
664 shader_stack[current_shader++] = get_pixels_frag;
668 case TRANSFER_ADDITION:
669 shader_stack[current_shader++] = blend_add_frag;
671 case TRANSFER_SUBTRACT:
672 shader_stack[current_shader++] = blend_subtract_frag;
674 case TRANSFER_MULTIPLY:
675 shader_stack[current_shader++] = blend_multiply_frag;
677 case TRANSFER_DIVIDE:
678 shader_stack[current_shader++] = blend_divide_frag;
681 shader_stack[current_shader++] = blend_max_frag;
684 shader_stack[current_shader++] = blend_min_frag;
688 shader_stack[current_shader++] = put_pixels_frag;
690 unsigned int shader_id = 0;
691 shader_id = VFrame::make_shader(0,
697 glUseProgram(shader_id);
698 glUniform1i(glGetUniformLocation(shader_id, "src_tex"), 0);
699 glUniform1i(glGetUniformLocation(shader_id, "dst_tex"), 1);
700 if(BC_CModels::is_yuv(dst->get_color_model()))
701 glUniform3f(glGetUniformLocation(shader_id, "chroma_offset"), 0.0, 0.5, 0.5);
703 glUniform3f(glGetUniformLocation(shader_id, "chroma_offset"), 0.0, 0.0, 0.0);
704 glUniform2f(glGetUniformLocation(shader_id, "dst_tex_dimensions"),
705 (float)dst->get_texture_w(),
706 (float)dst->get_texture_h());
714 glActiveTexture(GL_TEXTURE1);
715 glDisable(GL_TEXTURE_2D);
716 glActiveTexture(GL_TEXTURE0);
717 glDisable(GL_TEXTURE_2D);
719 dst->set_opengl_state(VFrame::SCREEN);
725 const char* Overlay::plugin_title() { return _("Overlay"); }
726 int Overlay::is_realtime() { return 1; }
727 int Overlay::is_multichannel() { return 1; }
728 int Overlay::is_synthesis() { return 1; }
732 NEW_WINDOW_MACRO(Overlay, OverlayWindow)
736 int Overlay::load_configuration()
738 KeyFrame *prev_keyframe;
739 prev_keyframe = get_prev_keyframe(get_source_position());
740 read_data(prev_keyframe);
745 void Overlay::save_data(KeyFrame *keyframe)
749 // cause data to be stored directly in text
750 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
751 output.tag.set_title("OVERLAY");
752 output.tag.set_property("MODE", config.mode);
753 output.tag.set_property("DIRECTION", config.direction);
754 output.tag.set_property("OUTPUT_LAYER", config.output_layer);
756 output.tag.set_title("/OVERLAY");
758 output.terminate_string();
761 void Overlay::read_data(KeyFrame *keyframe)
765 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
767 while(!input.read_tag())
769 if(input.tag.title_is("OVERLAY"))
771 config.mode = input.tag.get_property("MODE", config.mode);
772 config.direction = input.tag.get_property("DIRECTION", config.direction);
773 config.output_layer = input.tag.get_property("OUTPUT_LAYER", config.output_layer);
778 void Overlay::update_gui()
782 thread->window->lock_window("Overlay::update_gui");
783 ((OverlayWindow*)thread->window)->mode->set_text(OverlayConfig::mode_to_text(config.mode));
784 ((OverlayWindow*)thread->window)->direction->set_text(OverlayConfig::direction_to_text(config.direction));
785 ((OverlayWindow*)thread->window)->output->set_text(OverlayConfig::output_to_text(config.output_layer));
786 thread->window->unlock_window();