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"
23 #include "bcsignals.h"
24 #include "chromakey.h"
31 #include "loadbalance.h"
32 #include "playback3d.h"
34 #include "pluginvclient.h"
42 ChromaKeyConfig::ChromaKeyConfig()
47 void ChromaKeyConfig::reset()
59 void ChromaKeyConfig::copy_from(ChromaKeyConfig &src)
64 threshold = src.threshold;
65 use_value = src.use_value;
69 int ChromaKeyConfig::equivalent(ChromaKeyConfig &src)
71 return (EQUIV(red, src.red) &&
72 EQUIV(green, src.green) &&
73 EQUIV(blue, src.blue) &&
74 EQUIV(threshold, src.threshold) &&
75 EQUIV(slope, src.slope) &&
76 use_value == src.use_value);
79 void ChromaKeyConfig::interpolate(ChromaKeyConfig &prev,
80 ChromaKeyConfig &next,
83 int64_t current_frame)
85 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
86 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
88 this->red = prev.red * prev_scale + next.red * next_scale;
89 this->green = prev.green * prev_scale + next.green * next_scale;
90 this->blue = prev.blue * prev_scale + next.blue * next_scale;
91 this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
92 this->slope = prev.slope * prev_scale + next.slope * next_scale;
93 this->use_value = prev.use_value;
96 int ChromaKeyConfig::get_color()
98 int red = (int)(CLIP(this->red, 0, 1) * 0xff);
99 int green = (int)(CLIP(this->green, 0, 1) * 0xff);
100 int blue = (int)(CLIP(this->blue, 0, 1) * 0xff);
101 return (red << 16) | (green << 8) | blue;
110 ChromaKeyWindow::ChromaKeyWindow(ChromaKey *plugin)
111 : PluginClientWindow(plugin,
118 this->plugin = plugin;
122 ChromaKeyWindow::~ChromaKeyWindow()
127 void ChromaKeyWindow::create_objects()
129 int x = 10, y = 10, x1 = 100;
132 add_subwindow(title = new BC_Title(x, y, _("Color:")));
133 x += title->get_w() + 10;
134 add_subwindow(color = new ChromaKeyColor(plugin, this, x, y));
135 x += color->get_w() + 10;
136 add_subwindow(sample = new BC_SubWindow(x, y, 100, 50));
137 y += sample->get_h() + 10;
140 add_subwindow(new BC_Title(x, y, _("Slope:")));
141 add_subwindow(slope = new ChromaKeySlope(plugin, x1, y));
144 add_subwindow(new BC_Title(x, y, _("Threshold:")));
145 add_subwindow(threshold = new ChromaKeyThreshold(plugin, x1, y));
149 add_subwindow(use_value = new ChromaKeyUseValue(plugin, x1, y));
152 add_subwindow(use_colorpicker = new ChromaKeyUseColorPicker(plugin, this, x1, y));
154 y += use_colorpicker->get_h() + 10;
155 add_subwindow(new ChromaKeyReset(plugin, this, x, y));
157 color_thread = new ChromaKeyColorThread(plugin, this);
164 void ChromaKeyWindow::update_sample()
166 sample->set_color(plugin->config.get_color());
171 sample->set_color(BLACK);
172 sample->draw_rectangle(0,
179 void ChromaKeyWindow::done_event(int result)
181 color_thread->close_window();
192 ChromaKeyColor::ChromaKeyColor(ChromaKey *plugin,
193 ChromaKeyWindow *gui,
196 : BC_GenericButton(x,
200 this->plugin = plugin;
203 int ChromaKeyColor::handle_event()
205 gui->color_thread->start_window(
206 plugin->config.get_color(),
214 ChromaKeyThreshold::ChromaKeyThreshold(ChromaKey *plugin, int x, int y)
222 plugin->config.threshold)
224 this->plugin = plugin;
227 int ChromaKeyThreshold::handle_event()
229 plugin->config.threshold = get_value();
230 plugin->send_configure_change();
235 ChromaKeySlope::ChromaKeySlope(ChromaKey *plugin, int x, int y)
243 plugin->config.slope)
245 this->plugin = plugin;
249 int ChromaKeySlope::handle_event()
251 plugin->config.slope = get_value();
252 plugin->send_configure_change();
256 ChromaKeyUseValue::ChromaKeyUseValue(ChromaKey *plugin, int x, int y)
257 : BC_CheckBox(x, y, plugin->config.use_value, _("Use value"))
259 this->plugin = plugin;
261 int ChromaKeyUseValue::handle_event()
263 plugin->config.use_value = get_value();
264 plugin->send_configure_change();
268 ChromaKeyReset::ChromaKeyReset(ChromaKey *plugin, ChromaKeyWindow *gui, int x, int y)
269 : BC_GenericButton(x, y, _("Reset"))
271 this->plugin = plugin;
275 int ChromaKeyReset::handle_event()
277 plugin->config.reset();
279 plugin->send_configure_change();
283 ChromaKeyUseColorPicker::ChromaKeyUseColorPicker(ChromaKey *plugin,
284 ChromaKeyWindow *gui,
287 : BC_GenericButton(x, y, _("Use color picker"))
289 this->plugin = plugin;
293 int ChromaKeyUseColorPicker::handle_event()
295 plugin->config.red = plugin->get_red();
296 plugin->config.green = plugin->get_green();
297 plugin->config.blue = plugin->get_blue();
298 gui->update_sample();
299 plugin->send_configure_change();
306 ChromaKeyColorThread::ChromaKeyColorThread(ChromaKey *plugin, ChromaKeyWindow *gui)
307 : ColorPicker(1, _("Inner color"))
309 this->plugin = plugin;
313 int ChromaKeyColorThread::handle_new_color(int output, int alpha)
315 plugin->config.red = (float)(output & 0xff0000) / 0xff0000;
316 plugin->config.green = (float)(output & 0xff00) / 0xff00;
317 plugin->config.blue = (float)(output & 0xff) / 0xff;
318 gui->update_sample();
319 plugin->send_configure_change();
332 ChromaKeyServer::ChromaKeyServer(ChromaKey *plugin)
333 : LoadServer(plugin->PluginClient::smp + 1, plugin->PluginClient::smp + 1)
335 this->plugin = plugin;
337 void ChromaKeyServer::init_packages()
339 for(int i = 0; i < get_total_packages(); i++)
341 ChromaKeyPackage *pkg = (ChromaKeyPackage*)get_package(i);
342 pkg->y1 = plugin->input->get_h() * i / get_total_packages();
343 pkg->y2 = plugin->input->get_h() * (i + 1) / get_total_packages();
347 LoadClient* ChromaKeyServer::new_client()
349 return new ChromaKeyUnit(plugin, this);
351 LoadPackage* ChromaKeyServer::new_package()
353 return new ChromaKeyPackage;
358 ChromaKeyPackage::ChromaKeyPackage()
363 ChromaKeyUnit::ChromaKeyUnit(ChromaKey *plugin, ChromaKeyServer *server)
366 this->plugin = plugin;
370 void ChromaKeyUnit::process_package(LoadPackage *package)
372 ChromaKeyPackage *pkg = (ChromaKeyPackage*)package;
374 int w = plugin->input->get_w();
377 HSV::rgb_to_hsv(plugin->config.red,
378 plugin->config.green,
383 //float min_hue = h - plugin->config.threshold * 360 / 100;
384 //float max_hue = h + plugin->config.threshold * 360 / 100;
387 #define RGB_TO_VALUE(r, g, b) \
388 ((r) * R_TO_Y + (g) * G_TO_Y + (b) * B_TO_Y)
391 #define OUTER_VARIABLES(plugin) \
392 float value = RGB_TO_VALUE(plugin->config.red, \
393 plugin->config.green, \
394 plugin->config.blue); \
395 float threshold = plugin->config.threshold / 100; \
396 float min_v = value - threshold; \
397 float max_v = value + threshold; \
398 float r_key = plugin->config.red; \
399 float g_key = plugin->config.green; \
400 float b_key = plugin->config.blue; \
401 int y_key, u_key, v_key; \
402 YUV::yuv.rgb_to_yuv_8( \
403 (int)(r_key * 0xff), (int)(g_key * 0xff), (int)(b_key * 0xff), \
404 y_key, u_key, v_key); \
405 float run = plugin->config.slope / 100; \
406 float threshold_run = threshold + run;
408 OUTER_VARIABLES(plugin)
412 #define CHROMAKEY(type, components, max, use_yuv) \
414 for(int i = pkg->y1; i < pkg->y2; i++) \
416 type *row = (type*)plugin->input->get_rows()[i]; \
418 for(int j = 0; j < w; j++) \
422 /* Test for value in range */ \
423 if(plugin->config.use_value) \
425 float current_value; \
428 float r = (float)row[0] / max; \
433 float r = (float)row[0] / max; \
434 float g = (float)row[1] / max; \
435 float b = (float)row[2] / max; \
436 current_value = RGB_TO_VALUE(r, g, b); \
439 /* Full transparency if in range */ \
440 if(current_value >= min_v && current_value < max_v) \
445 /* Phased out if below or above range */ \
446 if(current_value < min_v) \
448 if(min_v - current_value < run) \
449 a = (min_v - current_value) / run; \
452 if(current_value - max_v < run) \
453 a = (current_value - max_v) / run; \
456 /* Use color cube */ \
464 difference = sqrt(SQR(y - y_key) + \
466 SQR(v - v_key)) / max; \
470 float r = (float)row[0] / max; \
471 float g = (float)row[1] / max; \
472 float b = (float)row[2] / max; \
473 difference = sqrt(SQR(r - r_key) + \
477 if(difference < threshold) \
482 if(difference < threshold_run) \
484 a = (difference - threshold) / run; \
489 /* Multiply alpha and put back in frame */ \
490 if(components == 4) \
492 row[3] = MIN((type)(a * max), row[3]); \
497 row[0] = (type)(a * row[0]); \
498 row[1] = (type)(a * (row[1] - (max / 2 + 1)) + max / 2 + 1); \
499 row[2] = (type)(a * (row[2] - (max / 2 + 1)) + max / 2 + 1); \
503 row[0] = (type)(a * row[0]); \
504 row[1] = (type)(a * row[1]); \
505 row[2] = (type)(a * row[2]); \
516 switch(plugin->input->get_color_model())
519 CHROMAKEY(float, 3, 1.0, 0);
522 CHROMAKEY(float, 4, 1.0, 0);
525 CHROMAKEY(unsigned char, 3, 0xff, 0);
528 CHROMAKEY(unsigned char, 4, 0xff, 0);
531 CHROMAKEY(unsigned char, 3, 0xff, 1);
534 CHROMAKEY(unsigned char, 4, 0xff, 1);
537 CHROMAKEY(uint16_t, 3, 0xffff, 1);
539 case BC_YUVA16161616:
540 CHROMAKEY(uint16_t, 4, 0xffff, 1);
550 REGISTER_PLUGIN(ChromaKey)
554 ChromaKey::ChromaKey(PluginServer *server)
555 : PluginVClient(server)
561 ChromaKey::~ChromaKey()
568 int ChromaKey::process_buffer(VFrame *frame,
569 int64_t start_position,
574 load_configuration();
576 this->output = frame;
584 if(EQUIV(config.threshold, 0))
590 if(get_use_opengl()) return run_opengl();
592 if(!engine) engine = new ChromaKeyServer(this);
593 engine->process_packages();
600 const char* ChromaKey::plugin_title() { return _("Chroma key"); }
601 int ChromaKey::is_realtime() { return 1; }
603 NEW_WINDOW_MACRO(ChromaKey, ChromaKeyWindow)
605 LOAD_CONFIGURATION_MACRO(ChromaKey, ChromaKeyConfig)
608 void ChromaKey::save_data(KeyFrame *keyframe)
611 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
612 output.tag.set_title("CHROMAKEY");
613 output.tag.set_property("RED", config.red);
614 output.tag.set_property("GREEN", config.green);
615 output.tag.set_property("BLUE", config.blue);
616 output.tag.set_property("THRESHOLD", config.threshold);
617 output.tag.set_property("SLOPE", config.slope);
618 output.tag.set_property("USE_VALUE", config.use_value);
620 output.tag.set_title("/CHROMAKEY");
622 output.append_newline();
623 output.terminate_string();
626 void ChromaKey::read_data(KeyFrame *keyframe)
630 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
632 while(!input.read_tag())
634 if(input.tag.title_is("CHROMAKEY"))
636 config.red = input.tag.get_property("RED", config.red);
637 config.green = input.tag.get_property("GREEN", config.green);
638 config.blue = input.tag.get_property("BLUE", config.blue);
639 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
640 config.slope = input.tag.get_property("SLOPE", config.slope);
641 config.use_value = input.tag.get_property("USE_VALUE", config.use_value);
648 void ChromaKey::update_gui()
652 load_configuration();
653 thread->window->lock_window();
654 ((ChromaKeyWindow *)(thread->window))->update_gui();
655 thread->window->unlock_window();
659 void ChromaKeyWindow::update_gui()
661 ChromaKeyConfig &config = plugin->config;
662 threshold->update(config.threshold);
663 slope->update(config.slope);
664 use_value->update(config.use_value);
668 int ChromaKey::handle_opengl()
671 OUTER_VARIABLES(this)
675 static const char *uniform_frag =
676 "uniform sampler2D tex;\n"
677 "uniform float min_v;\n"
678 "uniform float max_v;\n"
679 "uniform float run;\n"
680 "uniform float threshold;\n"
681 "uniform float threshold_run;\n"
682 "uniform vec3 key;\n";
684 static const char *get_yuvvalue_frag =
685 "float get_value(vec4 color)\n"
687 " return abs(color.r);\n"
690 static const char *get_rgbvalue_frag =
691 "float get_value(vec4 color)\n"
693 " return dot(color.rgb, vec3(0.29900, 0.58700, 0.11400));\n"
696 static const char *value_frag =
699 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
700 " float value = get_value(color);\n"
701 " float alpha = 1.0;\n"
703 " if(value >= min_v && value < max_v)\n"
706 " if(value < min_v)\n"
708 " if(min_v - value < run)\n"
709 " alpha = (min_v - value) / run;\n"
712 " if(value - max_v < run)\n"
713 " alpha = (value - max_v) / run;\n"
715 " gl_FragColor = vec4(color.rgb, alpha);\n"
718 static const char *cube_frag =
721 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
722 " float difference = length(color.rgb - key);\n"
723 " float alpha = 1.0;\n"
724 " if(difference < threshold)\n"
727 " if(difference < threshold_run)\n"
728 " alpha = (difference - threshold) / run;\n"
729 " gl_FragColor = vec4(color.rgb, min(color.a, alpha));\n"
734 get_output()->to_texture();
735 get_output()->enable_opengl();
736 get_output()->init_screen();
737 const char *shader_stack[] = { 0, 0, 0, 0, 0 };
738 int current_shader = 0;
740 shader_stack[current_shader++] = uniform_frag;
741 switch(get_output()->get_color_model())
747 shader_stack[current_shader++] = get_yuvvalue_frag;
748 shader_stack[current_shader++] = value_frag;
752 shader_stack[current_shader++] = cube_frag;
759 shader_stack[current_shader++] = get_rgbvalue_frag;
760 shader_stack[current_shader++] = value_frag;
764 shader_stack[current_shader++] = cube_frag;
770 unsigned int frag = VFrame::make_shader(0,
776 get_output()->bind_texture(0);
781 glUniform1i(glGetUniformLocation(frag, "tex"), 0);
782 glUniform1f(glGetUniformLocation(frag, "min_v"), min_v);
783 glUniform1f(glGetUniformLocation(frag, "max_v"), max_v);
784 glUniform1f(glGetUniformLocation(frag, "run"), run);
785 glUniform1f(glGetUniformLocation(frag, "threshold"), threshold);
786 glUniform1f(glGetUniformLocation(frag, "threshold_run"), threshold_run);
787 if(get_output()->get_color_model() != BC_YUV888 &&
788 get_output()->get_color_model() != BC_YUVA8888)
789 glUniform3f(glGetUniformLocation(frag, "key"),
790 r_key, g_key, b_key);
792 glUniform3f(glGetUniformLocation(frag, "key"),
793 (float)y_key / 0xff, (float)u_key / 0xff, (float)v_key / 0xff);
798 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
799 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
801 if(BC_CModels::components(get_output()->get_color_model()) == 3)
804 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
805 get_output()->clear_pbuffer();
809 get_output()->draw_texture();
812 get_output()->set_opengl_state(VFrame::SCREEN);
813 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
814 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);