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
23 #include "bcdisplayinfo.h"
24 #include "bcsignals.h"
25 #include "chromakey.h"
32 #include "loadbalance.h"
33 #include "playback3d.h"
35 #include "pluginvclient.h"
43 ChromaKeyConfig::ChromaKeyConfig()
48 void ChromaKeyConfig::reset()
60 void ChromaKeyConfig::copy_from(ChromaKeyConfig &src)
65 threshold = src.threshold;
66 use_value = src.use_value;
70 int ChromaKeyConfig::equivalent(ChromaKeyConfig &src)
72 return (EQUIV(red, src.red) &&
73 EQUIV(green, src.green) &&
74 EQUIV(blue, src.blue) &&
75 EQUIV(threshold, src.threshold) &&
76 EQUIV(slope, src.slope) &&
77 use_value == src.use_value);
80 void ChromaKeyConfig::interpolate(ChromaKeyConfig &prev,
81 ChromaKeyConfig &next,
84 int64_t current_frame)
86 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
87 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
89 this->red = prev.red * prev_scale + next.red * next_scale;
90 this->green = prev.green * prev_scale + next.green * next_scale;
91 this->blue = prev.blue * prev_scale + next.blue * next_scale;
92 this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
93 this->slope = prev.slope * prev_scale + next.slope * next_scale;
94 this->use_value = prev.use_value;
97 int ChromaKeyConfig::get_color()
99 int red = (int)(CLIP(this->red, 0, 1) * 0xff);
100 int green = (int)(CLIP(this->green, 0, 1) * 0xff);
101 int blue = (int)(CLIP(this->blue, 0, 1) * 0xff);
102 return (red << 16) | (green << 8) | blue;
111 ChromaKeyWindow::ChromaKeyWindow(ChromaKey *plugin)
112 : PluginClientWindow(plugin,
119 this->plugin = plugin;
123 ChromaKeyWindow::~ChromaKeyWindow()
128 void ChromaKeyWindow::create_objects()
130 int xs10 = xS(10), xs100 = xS(100);
131 int ys10 = yS(10), ys30 = yS(30), ys50 = yS(50);
132 int x = xs10, y = ys10, x1 = xS(100);
135 add_subwindow(title = new BC_Title(x, y, _("Color:")));
136 x += title->get_w() + xs10;
137 add_subwindow(color = new ChromaKeyColor(plugin, this, x, y));
138 x += color->get_w() + xs10;
139 add_subwindow(sample = new BC_SubWindow(x, y, xs100, ys50));
140 y += sample->get_h() + xs10;
143 add_subwindow(new BC_Title(x, y, _("Slope:")));
144 add_subwindow(slope = new ChromaKeySlope(plugin, x1, y));
147 add_subwindow(new BC_Title(x, y, _("Threshold:")));
148 add_subwindow(threshold = new ChromaKeyThreshold(plugin, x1, y));
152 add_subwindow(use_value = new ChromaKeyUseValue(plugin, x1, y));
155 add_subwindow(use_colorpicker = new ChromaKeyUseColorPicker(plugin, this, x1, y));
157 y += use_colorpicker->get_h() + xs10;
158 add_subwindow(new ChromaKeyReset(plugin, this, x, y));
160 color_thread = new ChromaKeyColorThread(plugin, this);
167 void ChromaKeyWindow::update_sample()
169 sample->set_color(plugin->config.get_color());
174 sample->set_color(BLACK);
175 sample->draw_rectangle(0,
182 void ChromaKeyWindow::done_event(int result)
184 color_thread->close_window();
195 ChromaKeyColor::ChromaKeyColor(ChromaKey *plugin,
196 ChromaKeyWindow *gui,
199 : BC_GenericButton(x,
203 this->plugin = plugin;
206 int ChromaKeyColor::handle_event()
208 gui->color_thread->start_window(
209 plugin->config.get_color(),
217 ChromaKeyThreshold::ChromaKeyThreshold(ChromaKey *plugin, int x, int y)
225 plugin->config.threshold)
227 this->plugin = plugin;
230 int ChromaKeyThreshold::handle_event()
232 plugin->config.threshold = get_value();
233 plugin->send_configure_change();
238 ChromaKeySlope::ChromaKeySlope(ChromaKey *plugin, int x, int y)
246 plugin->config.slope)
248 this->plugin = plugin;
252 int ChromaKeySlope::handle_event()
254 plugin->config.slope = get_value();
255 plugin->send_configure_change();
259 ChromaKeyUseValue::ChromaKeyUseValue(ChromaKey *plugin, int x, int y)
260 : BC_CheckBox(x, y, plugin->config.use_value, _("Use value"))
262 this->plugin = plugin;
264 int ChromaKeyUseValue::handle_event()
266 plugin->config.use_value = get_value();
267 plugin->send_configure_change();
271 ChromaKeyReset::ChromaKeyReset(ChromaKey *plugin, ChromaKeyWindow *gui, int x, int y)
272 : BC_GenericButton(x, y, _("Reset"))
274 this->plugin = plugin;
278 int ChromaKeyReset::handle_event()
280 plugin->config.reset();
282 plugin->send_configure_change();
286 ChromaKeyUseColorPicker::ChromaKeyUseColorPicker(ChromaKey *plugin,
287 ChromaKeyWindow *gui,
290 : BC_GenericButton(x, y, _("Use color picker"))
292 this->plugin = plugin;
296 int ChromaKeyUseColorPicker::handle_event()
298 plugin->config.red = plugin->get_red();
299 plugin->config.green = plugin->get_green();
300 plugin->config.blue = plugin->get_blue();
301 gui->update_sample();
302 plugin->send_configure_change();
309 ChromaKeyColorThread::ChromaKeyColorThread(ChromaKey *plugin, ChromaKeyWindow *gui)
310 : ColorPicker(1, _("Inner color"))
312 this->plugin = plugin;
316 int ChromaKeyColorThread::handle_new_color(int output, int alpha)
318 plugin->config.red = (float)(output & 0xff0000) / 0xff0000;
319 plugin->config.green = (float)(output & 0xff00) / 0xff00;
320 plugin->config.blue = (float)(output & 0xff) / 0xff;
321 gui->update_sample();
322 plugin->send_configure_change();
335 ChromaKeyServer::ChromaKeyServer(ChromaKey *plugin)
336 : LoadServer(plugin->PluginClient::smp + 1, plugin->PluginClient::smp + 1)
338 this->plugin = plugin;
340 void ChromaKeyServer::init_packages()
342 for(int i = 0; i < get_total_packages(); i++)
344 ChromaKeyPackage *pkg = (ChromaKeyPackage*)get_package(i);
345 pkg->y1 = plugin->input->get_h() * i / get_total_packages();
346 pkg->y2 = plugin->input->get_h() * (i + 1) / get_total_packages();
350 LoadClient* ChromaKeyServer::new_client()
352 return new ChromaKeyUnit(plugin, this);
354 LoadPackage* ChromaKeyServer::new_package()
356 return new ChromaKeyPackage;
361 ChromaKeyPackage::ChromaKeyPackage()
366 ChromaKeyUnit::ChromaKeyUnit(ChromaKey *plugin, ChromaKeyServer *server)
369 this->plugin = plugin;
373 void ChromaKeyUnit::process_package(LoadPackage *package)
375 ChromaKeyPackage *pkg = (ChromaKeyPackage*)package;
377 int w = plugin->input->get_w();
380 HSV::rgb_to_hsv(plugin->config.red,
381 plugin->config.green,
386 //float min_hue = h - plugin->config.threshold * 360 / 100;
387 //float max_hue = h + plugin->config.threshold * 360 / 100;
390 #define RGB_TO_VALUE(r, g, b) YUV::yuv.rgb_to_y_f((r),(g),(b))
392 #define OUTER_VARIABLES(plugin) \
393 float value = RGB_TO_VALUE(plugin->config.red, \
394 plugin->config.green, \
395 plugin->config.blue); \
396 float threshold = plugin->config.threshold / 100; \
397 float min_v = value - threshold; \
398 float max_v = value + threshold; \
399 float r_key = plugin->config.red; \
400 float g_key = plugin->config.green; \
401 float b_key = plugin->config.blue; \
402 int y_key, u_key, v_key; \
403 YUV::yuv.rgb_to_yuv_8( \
404 (int)(r_key * 0xff), (int)(g_key * 0xff), (int)(b_key * 0xff), \
405 y_key, u_key, v_key); \
406 float run = plugin->config.slope / 100; \
407 float threshold_run = threshold + run;
409 OUTER_VARIABLES(plugin)
413 #define CHROMAKEY(type, components, max, use_yuv) \
415 for(int i = pkg->y1; i < pkg->y2; i++) \
417 type *row = (type*)plugin->input->get_rows()[i]; \
419 for(int j = 0; j < w; j++) \
423 /* Test for value in range */ \
424 if(plugin->config.use_value) \
426 float current_value; \
429 float r = (float)row[0] / max; \
434 float r = (float)row[0] / max; \
435 float g = (float)row[1] / max; \
436 float b = (float)row[2] / max; \
437 current_value = RGB_TO_VALUE(r, g, b); \
440 /* Full transparency if in range */ \
441 if(current_value >= min_v && current_value < max_v) \
446 /* Phased out if below or above range */ \
447 if(current_value < min_v) \
449 if(min_v - current_value < run) \
450 a = (min_v - current_value) / run; \
453 if(current_value - max_v < run) \
454 a = (current_value - max_v) / run; \
457 /* Use color cube */ \
465 difference = sqrt(SQR(y - y_key) + \
467 SQR(v - v_key)) / max; \
471 float r = (float)row[0] / max; \
472 float g = (float)row[1] / max; \
473 float b = (float)row[2] / max; \
474 difference = sqrt(SQR(r - r_key) + \
478 if(difference < threshold) \
483 if(difference < threshold_run) \
485 a = (difference - threshold) / run; \
490 /* Multiply alpha and put back in frame */ \
491 if(components == 4) \
493 row[3] = MIN((type)(a * max), row[3]); \
498 row[0] = (type)(a * row[0]); \
499 row[1] = (type)(a * (row[1] - (max / 2 + 1)) + max / 2 + 1); \
500 row[2] = (type)(a * (row[2] - (max / 2 + 1)) + max / 2 + 1); \
504 row[0] = (type)(a * row[0]); \
505 row[1] = (type)(a * row[1]); \
506 row[2] = (type)(a * row[2]); \
517 switch(plugin->input->get_color_model())
520 CHROMAKEY(float, 3, 1.0, 0);
523 CHROMAKEY(float, 4, 1.0, 0);
526 CHROMAKEY(unsigned char, 3, 0xff, 0);
529 CHROMAKEY(unsigned char, 4, 0xff, 0);
532 CHROMAKEY(unsigned char, 3, 0xff, 1);
535 CHROMAKEY(unsigned char, 4, 0xff, 1);
538 CHROMAKEY(uint16_t, 3, 0xffff, 1);
540 case BC_YUVA16161616:
541 CHROMAKEY(uint16_t, 4, 0xffff, 1);
551 REGISTER_PLUGIN(ChromaKey)
555 ChromaKey::ChromaKey(PluginServer *server)
556 : PluginVClient(server)
562 ChromaKey::~ChromaKey()
569 int ChromaKey::process_buffer(VFrame *frame,
570 int64_t start_position,
575 load_configuration();
577 this->output = frame;
585 if(EQUIV(config.threshold, 0))
591 if(get_use_opengl()) return run_opengl();
593 if(!engine) engine = new ChromaKeyServer(this);
594 engine->process_packages();
601 const char* ChromaKey::plugin_title() { return N_("Chroma key"); }
602 int ChromaKey::is_realtime() { return 1; }
604 NEW_WINDOW_MACRO(ChromaKey, ChromaKeyWindow)
606 LOAD_CONFIGURATION_MACRO(ChromaKey, ChromaKeyConfig)
609 void ChromaKey::save_data(KeyFrame *keyframe)
612 output.set_shared_output(keyframe->xbuf);
613 output.tag.set_title("CHROMAKEY");
614 output.tag.set_property("RED", config.red);
615 output.tag.set_property("GREEN", config.green);
616 output.tag.set_property("BLUE", config.blue);
617 output.tag.set_property("THRESHOLD", config.threshold);
618 output.tag.set_property("SLOPE", config.slope);
619 output.tag.set_property("USE_VALUE", config.use_value);
621 output.tag.set_title("/CHROMAKEY");
623 output.append_newline();
624 output.terminate_string();
627 void ChromaKey::read_data(KeyFrame *keyframe)
631 input.set_shared_input(keyframe->xbuf);
633 while(!input.read_tag())
635 if(input.tag.title_is("CHROMAKEY"))
637 config.red = input.tag.get_property("RED", config.red);
638 config.green = input.tag.get_property("GREEN", config.green);
639 config.blue = input.tag.get_property("BLUE", config.blue);
640 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
641 config.slope = input.tag.get_property("SLOPE", config.slope);
642 config.use_value = input.tag.get_property("USE_VALUE", config.use_value);
649 void ChromaKey::update_gui()
653 load_configuration();
654 thread->window->lock_window();
655 ((ChromaKeyWindow *)(thread->window))->update_gui();
656 thread->window->unlock_window();
660 void ChromaKeyWindow::update_gui()
662 ChromaKeyConfig &config = plugin->config;
663 threshold->update(config.threshold);
664 slope->update(config.slope);
665 use_value->update(config.use_value);
669 int ChromaKey::handle_opengl()
672 OUTER_VARIABLES(this)
676 static const char *uniform_frag =
677 "uniform sampler2D tex;\n"
678 "uniform float min_v;\n"
679 "uniform float max_v;\n"
680 "uniform float run;\n"
681 "uniform float threshold;\n"
682 "uniform float threshold_run;\n"
683 "uniform vec3 key;\n";
685 static const char *get_yuvvalue_frag =
686 "float get_value(vec4 color)\n"
688 " return abs(color.r);\n"
691 static const char *get_rgbvalue_frag =
692 "uniform vec3 rgb_to_y_vector;\n"
693 "uniform float yminf;\n"
694 "float get_value(vec4 color)\n"
696 " return dot(color.rgb, rgb_to_y_vector) + yminf;\n"
699 static const char *value_frag =
702 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
703 " float value = get_value(color);\n"
704 " float alpha = 1.0;\n"
706 " if(value >= min_v && value < max_v)\n"
709 " if(value < min_v)\n"
711 " if(min_v - value < run)\n"
712 " alpha = (min_v - value) / run;\n"
715 " if(value - max_v < run)\n"
716 " alpha = (value - max_v) / run;\n"
718 " gl_FragColor = vec4(color.rgb, alpha);\n"
721 static const char *cube_frag =
724 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
725 " float difference = length(color.rgb - key);\n"
726 " float alpha = 1.0;\n"
727 " if(difference < threshold)\n"
730 " if(difference < threshold_run)\n"
731 " alpha = (difference - threshold) / run;\n"
732 " gl_FragColor = vec4(color.rgb, min(color.a, alpha));\n"
737 get_output()->to_texture();
738 get_output()->enable_opengl();
739 get_output()->init_screen();
741 const char *shader_stack[16];
742 memset(shader_stack,0, sizeof(shader_stack));
743 int current_shader = 0;
744 shader_stack[current_shader++] = uniform_frag;
746 switch(get_output()->get_color_model()) {
749 if( config.use_value ) {
750 shader_stack[current_shader++] = get_yuvvalue_frag;
751 shader_stack[current_shader++] = value_frag;
754 shader_stack[current_shader++] = cube_frag;
759 if(config.use_value) {
760 shader_stack[current_shader++] = get_rgbvalue_frag;
761 shader_stack[current_shader++] = value_frag;
764 shader_stack[current_shader++] = cube_frag;
770 shader_stack[current_shader] = 0;
771 unsigned int shader = VFrame::make_shader(shader_stack);
773 get_output()->bind_texture(0);
774 glUseProgram(shader);
775 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
776 glUniform1f(glGetUniformLocation(shader, "min_v"), min_v);
777 glUniform1f(glGetUniformLocation(shader, "max_v"), max_v);
778 glUniform1f(glGetUniformLocation(shader, "run"), run);
779 glUniform1f(glGetUniformLocation(shader, "threshold"), threshold);
780 glUniform1f(glGetUniformLocation(shader, "threshold_run"), threshold_run);
781 if(get_output()->get_color_model() != BC_YUV888 &&
782 get_output()->get_color_model() != BC_YUVA8888)
783 glUniform3f(glGetUniformLocation(shader, "key"),
784 r_key, g_key, b_key);
786 glUniform3f(glGetUniformLocation(shader, "key"),
787 (float)y_key / 0xff, (float)u_key / 0xff, (float)v_key / 0xff);
789 BC_GL_RGB_TO_Y(shader);
793 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
794 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
796 if(BC_CModels::components(get_output()->get_color_model()) == 3)
799 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
800 get_output()->clear_pbuffer();
804 get_output()->draw_texture();
807 get_output()->set_opengl_state(VFrame::SCREEN);
808 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
809 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);