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 x = 10, y = 10, x1 = 100;
133 add_subwindow(title = new BC_Title(x, y, _("Color:")));
134 x += title->get_w() + 10;
135 add_subwindow(color = new ChromaKeyColor(plugin, this, x, y));
136 x += color->get_w() + 10;
137 add_subwindow(sample = new BC_SubWindow(x, y, 100, 50));
138 y += sample->get_h() + 10;
141 add_subwindow(new BC_Title(x, y, _("Slope:")));
142 add_subwindow(slope = new ChromaKeySlope(plugin, x1, y));
145 add_subwindow(new BC_Title(x, y, _("Threshold:")));
146 add_subwindow(threshold = new ChromaKeyThreshold(plugin, x1, y));
150 add_subwindow(use_value = new ChromaKeyUseValue(plugin, x1, y));
153 add_subwindow(use_colorpicker = new ChromaKeyUseColorPicker(plugin, this, x1, y));
155 y += use_colorpicker->get_h() + 10;
156 add_subwindow(new ChromaKeyReset(plugin, this, x, y));
158 color_thread = new ChromaKeyColorThread(plugin, this);
165 void ChromaKeyWindow::update_sample()
167 sample->set_color(plugin->config.get_color());
172 sample->set_color(BLACK);
173 sample->draw_rectangle(0,
180 void ChromaKeyWindow::done_event(int result)
182 color_thread->close_window();
193 ChromaKeyColor::ChromaKeyColor(ChromaKey *plugin,
194 ChromaKeyWindow *gui,
197 : BC_GenericButton(x,
201 this->plugin = plugin;
204 int ChromaKeyColor::handle_event()
206 gui->color_thread->start_window(
207 plugin->config.get_color(),
215 ChromaKeyThreshold::ChromaKeyThreshold(ChromaKey *plugin, int x, int y)
223 plugin->config.threshold)
225 this->plugin = plugin;
228 int ChromaKeyThreshold::handle_event()
230 plugin->config.threshold = get_value();
231 plugin->send_configure_change();
236 ChromaKeySlope::ChromaKeySlope(ChromaKey *plugin, int x, int y)
244 plugin->config.slope)
246 this->plugin = plugin;
250 int ChromaKeySlope::handle_event()
252 plugin->config.slope = get_value();
253 plugin->send_configure_change();
257 ChromaKeyUseValue::ChromaKeyUseValue(ChromaKey *plugin, int x, int y)
258 : BC_CheckBox(x, y, plugin->config.use_value, _("Use value"))
260 this->plugin = plugin;
262 int ChromaKeyUseValue::handle_event()
264 plugin->config.use_value = get_value();
265 plugin->send_configure_change();
269 ChromaKeyReset::ChromaKeyReset(ChromaKey *plugin, ChromaKeyWindow *gui, int x, int y)
270 : BC_GenericButton(x, y, _("Reset"))
272 this->plugin = plugin;
276 int ChromaKeyReset::handle_event()
278 plugin->config.reset();
280 plugin->send_configure_change();
284 ChromaKeyUseColorPicker::ChromaKeyUseColorPicker(ChromaKey *plugin,
285 ChromaKeyWindow *gui,
288 : BC_GenericButton(x, y, _("Use color picker"))
290 this->plugin = plugin;
294 int ChromaKeyUseColorPicker::handle_event()
296 plugin->config.red = plugin->get_red();
297 plugin->config.green = plugin->get_green();
298 plugin->config.blue = plugin->get_blue();
299 gui->update_sample();
300 plugin->send_configure_change();
307 ChromaKeyColorThread::ChromaKeyColorThread(ChromaKey *plugin, ChromaKeyWindow *gui)
308 : ColorPicker(1, _("Inner color"))
310 this->plugin = plugin;
314 int ChromaKeyColorThread::handle_new_color(int output, int alpha)
316 plugin->config.red = (float)(output & 0xff0000) / 0xff0000;
317 plugin->config.green = (float)(output & 0xff00) / 0xff00;
318 plugin->config.blue = (float)(output & 0xff) / 0xff;
319 gui->update_sample();
320 plugin->send_configure_change();
333 ChromaKeyServer::ChromaKeyServer(ChromaKey *plugin)
334 : LoadServer(plugin->PluginClient::smp + 1, plugin->PluginClient::smp + 1)
336 this->plugin = plugin;
338 void ChromaKeyServer::init_packages()
340 for(int i = 0; i < get_total_packages(); i++)
342 ChromaKeyPackage *pkg = (ChromaKeyPackage*)get_package(i);
343 pkg->y1 = plugin->input->get_h() * i / get_total_packages();
344 pkg->y2 = plugin->input->get_h() * (i + 1) / get_total_packages();
348 LoadClient* ChromaKeyServer::new_client()
350 return new ChromaKeyUnit(plugin, this);
352 LoadPackage* ChromaKeyServer::new_package()
354 return new ChromaKeyPackage;
359 ChromaKeyPackage::ChromaKeyPackage()
364 ChromaKeyUnit::ChromaKeyUnit(ChromaKey *plugin, ChromaKeyServer *server)
367 this->plugin = plugin;
371 void ChromaKeyUnit::process_package(LoadPackage *package)
373 ChromaKeyPackage *pkg = (ChromaKeyPackage*)package;
375 int w = plugin->input->get_w();
378 HSV::rgb_to_hsv(plugin->config.red,
379 plugin->config.green,
384 //float min_hue = h - plugin->config.threshold * 360 / 100;
385 //float max_hue = h + plugin->config.threshold * 360 / 100;
388 #define RGB_TO_VALUE(r, g, b) YUV::yuv.rgb_to_y_f((r),(g),(b))
390 #define OUTER_VARIABLES(plugin) \
391 float value = RGB_TO_VALUE(plugin->config.red, \
392 plugin->config.green, \
393 plugin->config.blue); \
394 float threshold = plugin->config.threshold / 100; \
395 float min_v = value - threshold; \
396 float max_v = value + threshold; \
397 float r_key = plugin->config.red; \
398 float g_key = plugin->config.green; \
399 float b_key = plugin->config.blue; \
400 int y_key, u_key, v_key; \
401 YUV::yuv.rgb_to_yuv_8( \
402 (int)(r_key * 0xff), (int)(g_key * 0xff), (int)(b_key * 0xff), \
403 y_key, u_key, v_key); \
404 float run = plugin->config.slope / 100; \
405 float threshold_run = threshold + run;
407 OUTER_VARIABLES(plugin)
411 #define CHROMAKEY(type, components, max, use_yuv) \
413 for(int i = pkg->y1; i < pkg->y2; i++) \
415 type *row = (type*)plugin->input->get_rows()[i]; \
417 for(int j = 0; j < w; j++) \
421 /* Test for value in range */ \
422 if(plugin->config.use_value) \
424 float current_value; \
427 float r = (float)row[0] / max; \
432 float r = (float)row[0] / max; \
433 float g = (float)row[1] / max; \
434 float b = (float)row[2] / max; \
435 current_value = RGB_TO_VALUE(r, g, b); \
438 /* Full transparency if in range */ \
439 if(current_value >= min_v && current_value < max_v) \
444 /* Phased out if below or above range */ \
445 if(current_value < min_v) \
447 if(min_v - current_value < run) \
448 a = (min_v - current_value) / run; \
451 if(current_value - max_v < run) \
452 a = (current_value - max_v) / run; \
455 /* Use color cube */ \
463 difference = sqrt(SQR(y - y_key) + \
465 SQR(v - v_key)) / max; \
469 float r = (float)row[0] / max; \
470 float g = (float)row[1] / max; \
471 float b = (float)row[2] / max; \
472 difference = sqrt(SQR(r - r_key) + \
476 if(difference < threshold) \
481 if(difference < threshold_run) \
483 a = (difference - threshold) / run; \
488 /* Multiply alpha and put back in frame */ \
489 if(components == 4) \
491 row[3] = MIN((type)(a * max), row[3]); \
496 row[0] = (type)(a * row[0]); \
497 row[1] = (type)(a * (row[1] - (max / 2 + 1)) + max / 2 + 1); \
498 row[2] = (type)(a * (row[2] - (max / 2 + 1)) + max / 2 + 1); \
502 row[0] = (type)(a * row[0]); \
503 row[1] = (type)(a * row[1]); \
504 row[2] = (type)(a * row[2]); \
515 switch(plugin->input->get_color_model())
518 CHROMAKEY(float, 3, 1.0, 0);
521 CHROMAKEY(float, 4, 1.0, 0);
524 CHROMAKEY(unsigned char, 3, 0xff, 0);
527 CHROMAKEY(unsigned char, 4, 0xff, 0);
530 CHROMAKEY(unsigned char, 3, 0xff, 1);
533 CHROMAKEY(unsigned char, 4, 0xff, 1);
536 CHROMAKEY(uint16_t, 3, 0xffff, 1);
538 case BC_YUVA16161616:
539 CHROMAKEY(uint16_t, 4, 0xffff, 1);
549 REGISTER_PLUGIN(ChromaKey)
553 ChromaKey::ChromaKey(PluginServer *server)
554 : PluginVClient(server)
560 ChromaKey::~ChromaKey()
567 int ChromaKey::process_buffer(VFrame *frame,
568 int64_t start_position,
573 load_configuration();
575 this->output = frame;
583 if(EQUIV(config.threshold, 0))
589 if(get_use_opengl()) return run_opengl();
591 if(!engine) engine = new ChromaKeyServer(this);
592 engine->process_packages();
599 const char* ChromaKey::plugin_title() { return N_("Chroma key"); }
600 int ChromaKey::is_realtime() { return 1; }
602 NEW_WINDOW_MACRO(ChromaKey, ChromaKeyWindow)
604 LOAD_CONFIGURATION_MACRO(ChromaKey, ChromaKeyConfig)
607 void ChromaKey::save_data(KeyFrame *keyframe)
610 output.set_shared_output(keyframe->xbuf);
611 output.tag.set_title("CHROMAKEY");
612 output.tag.set_property("RED", config.red);
613 output.tag.set_property("GREEN", config.green);
614 output.tag.set_property("BLUE", config.blue);
615 output.tag.set_property("THRESHOLD", config.threshold);
616 output.tag.set_property("SLOPE", config.slope);
617 output.tag.set_property("USE_VALUE", config.use_value);
619 output.tag.set_title("/CHROMAKEY");
621 output.append_newline();
622 output.terminate_string();
625 void ChromaKey::read_data(KeyFrame *keyframe)
629 input.set_shared_input(keyframe->xbuf);
631 while(!input.read_tag())
633 if(input.tag.title_is("CHROMAKEY"))
635 config.red = input.tag.get_property("RED", config.red);
636 config.green = input.tag.get_property("GREEN", config.green);
637 config.blue = input.tag.get_property("BLUE", config.blue);
638 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
639 config.slope = input.tag.get_property("SLOPE", config.slope);
640 config.use_value = input.tag.get_property("USE_VALUE", config.use_value);
647 void ChromaKey::update_gui()
651 load_configuration();
652 thread->window->lock_window();
653 ((ChromaKeyWindow *)(thread->window))->update_gui();
654 thread->window->unlock_window();
658 void ChromaKeyWindow::update_gui()
660 ChromaKeyConfig &config = plugin->config;
661 threshold->update(config.threshold);
662 slope->update(config.slope);
663 use_value->update(config.use_value);
667 int ChromaKey::handle_opengl()
670 OUTER_VARIABLES(this)
674 static const char *uniform_frag =
675 "uniform sampler2D tex;\n"
676 "uniform float min_v;\n"
677 "uniform float max_v;\n"
678 "uniform float run;\n"
679 "uniform float threshold;\n"
680 "uniform float threshold_run;\n"
681 "uniform vec3 key;\n";
683 static const char *get_yuvvalue_frag =
684 "float get_value(vec4 color)\n"
686 " return abs(color.r);\n"
689 static const char *get_rgbvalue_frag =
690 "uniform vec3 rgb_to_y_vector;\n"
691 "uniform float yminf;\n"
692 "float get_value(vec4 color)\n"
694 " return dot(color.rgb, rgb_to_y_vector) + yminf;\n"
697 static const char *value_frag =
700 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
701 " float value = get_value(color);\n"
702 " float alpha = 1.0;\n"
704 " if(value >= min_v && value < max_v)\n"
707 " if(value < min_v)\n"
709 " if(min_v - value < run)\n"
710 " alpha = (min_v - value) / run;\n"
713 " if(value - max_v < run)\n"
714 " alpha = (value - max_v) / run;\n"
716 " gl_FragColor = vec4(color.rgb, alpha);\n"
719 static const char *cube_frag =
722 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
723 " float difference = length(color.rgb - key);\n"
724 " float alpha = 1.0;\n"
725 " if(difference < threshold)\n"
728 " if(difference < threshold_run)\n"
729 " alpha = (difference - threshold) / run;\n"
730 " gl_FragColor = vec4(color.rgb, min(color.a, alpha));\n"
735 get_output()->to_texture();
736 get_output()->enable_opengl();
737 get_output()->init_screen();
739 const char *shader_stack[16];
740 memset(shader_stack,0, sizeof(shader_stack));
741 int current_shader = 0;
742 shader_stack[current_shader++] = uniform_frag;
744 switch(get_output()->get_color_model()) {
747 if( config.use_value ) {
748 shader_stack[current_shader++] = get_yuvvalue_frag;
749 shader_stack[current_shader++] = value_frag;
752 shader_stack[current_shader++] = cube_frag;
757 if(config.use_value) {
758 shader_stack[current_shader++] = get_rgbvalue_frag;
759 shader_stack[current_shader++] = value_frag;
762 shader_stack[current_shader++] = cube_frag;
768 shader_stack[current_shader] = 0;
769 unsigned int shader = VFrame::make_shader(shader_stack);
771 get_output()->bind_texture(0);
772 glUseProgram(shader);
773 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
774 glUniform1f(glGetUniformLocation(shader, "min_v"), min_v);
775 glUniform1f(glGetUniformLocation(shader, "max_v"), max_v);
776 glUniform1f(glGetUniformLocation(shader, "run"), run);
777 glUniform1f(glGetUniformLocation(shader, "threshold"), threshold);
778 glUniform1f(glGetUniformLocation(shader, "threshold_run"), threshold_run);
779 if(get_output()->get_color_model() != BC_YUV888 &&
780 get_output()->get_color_model() != BC_YUVA8888)
781 glUniform3f(glGetUniformLocation(shader, "key"),
782 r_key, g_key, b_key);
784 glUniform3f(glGetUniformLocation(shader, "key"),
785 (float)y_key / 0xff, (float)u_key / 0xff, (float)v_key / 0xff);
787 BC_GL_RGB_TO_Y(shader);
791 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
792 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
794 if(BC_CModels::components(get_output()->get_color_model()) == 3)
797 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
798 get_output()->clear_pbuffer();
802 get_output()->draw_texture();
805 get_output()->set_opengl_state(VFrame::SCREEN);
806 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
807 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);