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"
44 ChromaKeyConfig::ChromaKeyConfig()
55 void ChromaKeyConfig::copy_from(ChromaKeyConfig &src)
60 threshold = src.threshold;
61 use_value = src.use_value;
65 int ChromaKeyConfig::equivalent(ChromaKeyConfig &src)
67 return (EQUIV(red, src.red) &&
68 EQUIV(green, src.green) &&
69 EQUIV(blue, src.blue) &&
70 EQUIV(threshold, src.threshold) &&
71 EQUIV(slope, src.slope) &&
72 use_value == src.use_value);
75 void ChromaKeyConfig::interpolate(ChromaKeyConfig &prev,
76 ChromaKeyConfig &next,
79 int64_t current_frame)
81 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
82 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
84 this->red = prev.red * prev_scale + next.red * next_scale;
85 this->green = prev.green * prev_scale + next.green * next_scale;
86 this->blue = prev.blue * prev_scale + next.blue * next_scale;
87 this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
88 this->slope = prev.slope * prev_scale + next.slope * next_scale;
89 this->use_value = prev.use_value;
92 int ChromaKeyConfig::get_color()
94 int red = (int)(CLIP(this->red, 0, 1) * 0xff);
95 int green = (int)(CLIP(this->green, 0, 1) * 0xff);
96 int blue = (int)(CLIP(this->blue, 0, 1) * 0xff);
97 return (red << 16) | (green << 8) | blue;
106 ChromaKeyWindow::ChromaKeyWindow(ChromaKey *plugin)
107 : PluginClientWindow(plugin,
114 this->plugin = plugin;
118 ChromaKeyWindow::~ChromaKeyWindow()
123 void ChromaKeyWindow::create_objects()
125 int x = 10, y = 10, x1 = 100;
128 add_subwindow(title = new BC_Title(x, y, _("Color:")));
129 x += title->get_w() + 10;
130 add_subwindow(color = new ChromaKeyColor(plugin, this, x, y));
131 x += color->get_w() + 10;
132 add_subwindow(sample = new BC_SubWindow(x, y, 100, 50));
133 y += sample->get_h() + 10;
136 add_subwindow(new BC_Title(x, y, _("Slope:")));
137 add_subwindow(slope = new ChromaKeySlope(plugin, x1, y));
140 add_subwindow(new BC_Title(x, y, _("Threshold:")));
141 add_subwindow(threshold = new ChromaKeyThreshold(plugin, x1, y));
145 add_subwindow(use_value = new ChromaKeyUseValue(plugin, x1, y));
148 add_subwindow(use_colorpicker = new ChromaKeyUseColorPicker(plugin, this, x1, y));
150 color_thread = new ChromaKeyColorThread(plugin, this);
157 void ChromaKeyWindow::update_sample()
159 sample->set_color(plugin->config.get_color());
164 sample->set_color(BLACK);
165 sample->draw_rectangle(0,
180 ChromaKeyColor::ChromaKeyColor(ChromaKey *plugin,
181 ChromaKeyWindow *gui,
184 : BC_GenericButton(x,
188 this->plugin = plugin;
191 int ChromaKeyColor::handle_event()
193 gui->color_thread->start_window(
194 plugin->config.get_color(),
202 ChromaKeyThreshold::ChromaKeyThreshold(ChromaKey *plugin, int x, int y)
210 plugin->config.threshold)
212 this->plugin = plugin;
215 int ChromaKeyThreshold::handle_event()
217 plugin->config.threshold = get_value();
218 plugin->send_configure_change();
223 ChromaKeySlope::ChromaKeySlope(ChromaKey *plugin, int x, int y)
231 plugin->config.slope)
233 this->plugin = plugin;
237 int ChromaKeySlope::handle_event()
239 plugin->config.slope = get_value();
240 plugin->send_configure_change();
245 ChromaKeyUseValue::ChromaKeyUseValue(ChromaKey *plugin, int x, int y)
246 : BC_CheckBox(x, y, plugin->config.use_value, _("Use value"))
248 this->plugin = plugin;
250 int ChromaKeyUseValue::handle_event()
252 plugin->config.use_value = get_value();
253 plugin->send_configure_change();
258 ChromaKeyUseColorPicker::ChromaKeyUseColorPicker(ChromaKey *plugin,
259 ChromaKeyWindow *gui,
262 : BC_GenericButton(x, y, _("Use color picker"))
264 this->plugin = plugin;
268 int ChromaKeyUseColorPicker::handle_event()
270 plugin->config.red = plugin->get_red();
271 plugin->config.green = plugin->get_green();
272 plugin->config.blue = plugin->get_blue();
273 gui->update_sample();
274 plugin->send_configure_change();
281 ChromaKeyColorThread::ChromaKeyColorThread(ChromaKey *plugin, ChromaKeyWindow *gui)
282 : ColorThread(1, _("Inner color"))
284 this->plugin = plugin;
288 int ChromaKeyColorThread::handle_new_color(int output, int alpha)
290 plugin->config.red = (float)(output & 0xff0000) / 0xff0000;
291 plugin->config.green = (float)(output & 0xff00) / 0xff00;
292 plugin->config.blue = (float)(output & 0xff) / 0xff;
293 gui->update_sample();
294 plugin->send_configure_change();
307 ChromaKeyServer::ChromaKeyServer(ChromaKey *plugin)
308 : LoadServer(plugin->PluginClient::smp + 1, plugin->PluginClient::smp + 1)
310 this->plugin = plugin;
312 void ChromaKeyServer::init_packages()
314 for(int i = 0; i < get_total_packages(); i++)
316 ChromaKeyPackage *pkg = (ChromaKeyPackage*)get_package(i);
317 pkg->y1 = plugin->input->get_h() * i / get_total_packages();
318 pkg->y2 = plugin->input->get_h() * (i + 1) / get_total_packages();
322 LoadClient* ChromaKeyServer::new_client()
324 return new ChromaKeyUnit(plugin, this);
326 LoadPackage* ChromaKeyServer::new_package()
328 return new ChromaKeyPackage;
333 ChromaKeyPackage::ChromaKeyPackage()
338 ChromaKeyUnit::ChromaKeyUnit(ChromaKey *plugin, ChromaKeyServer *server)
341 this->plugin = plugin;
345 void ChromaKeyUnit::process_package(LoadPackage *package)
347 ChromaKeyPackage *pkg = (ChromaKeyPackage*)package;
349 int w = plugin->input->get_w();
352 HSV::rgb_to_hsv(plugin->config.red,
353 plugin->config.green,
358 //float min_hue = h - plugin->config.threshold * 360 / 100;
359 //float max_hue = h + plugin->config.threshold * 360 / 100;
362 #define RGB_TO_VALUE(r, g, b) \
363 ((r) * R_TO_Y + (g) * G_TO_Y + (b) * B_TO_Y)
366 #define OUTER_VARIABLES(plugin) \
368 float value = RGB_TO_VALUE(plugin->config.red, \
369 plugin->config.green, \
370 plugin->config.blue); \
371 float threshold = plugin->config.threshold / 100; \
372 float min_v = value - threshold; \
373 float max_v = value + threshold; \
374 float r_key = plugin->config.red; \
375 float g_key = plugin->config.green; \
376 float b_key = plugin->config.blue; \
377 int y_key, u_key, v_key; \
378 yuv.rgb_to_yuv_8((int)(r_key * 0xff), (int)(g_key * 0xff), (int)(b_key * 0xff), y_key, u_key, v_key); \
379 float run = plugin->config.slope / 100; \
380 float threshold_run = threshold + run;
382 OUTER_VARIABLES(plugin)
386 #define CHROMAKEY(type, components, max, use_yuv) \
388 for(int i = pkg->y1; i < pkg->y2; i++) \
390 type *row = (type*)plugin->input->get_rows()[i]; \
392 for(int j = 0; j < w; j++) \
396 /* Test for value in range */ \
397 if(plugin->config.use_value) \
399 float current_value; \
402 float r = (float)row[0] / max; \
407 float r = (float)row[0] / max; \
408 float g = (float)row[1] / max; \
409 float b = (float)row[2] / max; \
410 current_value = RGB_TO_VALUE(r, g, b); \
413 /* Full transparency if in range */ \
414 if(current_value >= min_v && current_value < max_v) \
419 /* Phased out if below or above range */ \
420 if(current_value < min_v) \
422 if(min_v - current_value < run) \
423 a = (min_v - current_value) / run; \
426 if(current_value - max_v < run) \
427 a = (current_value - max_v) / run; \
430 /* Use color cube */ \
438 difference = sqrt(SQR(y - y_key) + \
440 SQR(v - v_key)) / max; \
444 float r = (float)row[0] / max; \
445 float g = (float)row[1] / max; \
446 float b = (float)row[2] / max; \
447 difference = sqrt(SQR(r - r_key) + \
451 if(difference < threshold) \
456 if(difference < threshold_run) \
458 a = (difference - threshold) / run; \
463 /* Multiply alpha and put back in frame */ \
464 if(components == 4) \
466 row[3] = MIN((type)(a * max), row[3]); \
471 row[0] = (type)(a * row[0]); \
472 row[1] = (type)(a * (row[1] - (max / 2 + 1)) + max / 2 + 1); \
473 row[2] = (type)(a * (row[2] - (max / 2 + 1)) + max / 2 + 1); \
477 row[0] = (type)(a * row[0]); \
478 row[1] = (type)(a * row[1]); \
479 row[2] = (type)(a * row[2]); \
490 switch(plugin->input->get_color_model())
493 CHROMAKEY(float, 3, 1.0, 0);
496 CHROMAKEY(float, 4, 1.0, 0);
499 CHROMAKEY(unsigned char, 3, 0xff, 0);
502 CHROMAKEY(unsigned char, 4, 0xff, 0);
505 CHROMAKEY(unsigned char, 3, 0xff, 1);
508 CHROMAKEY(unsigned char, 4, 0xff, 1);
511 CHROMAKEY(uint16_t, 3, 0xffff, 1);
513 case BC_YUVA16161616:
514 CHROMAKEY(uint16_t, 4, 0xffff, 1);
524 REGISTER_PLUGIN(ChromaKey)
528 ChromaKey::ChromaKey(PluginServer *server)
529 : PluginVClient(server)
535 ChromaKey::~ChromaKey()
542 int ChromaKey::process_buffer(VFrame *frame,
543 int64_t start_position,
548 load_configuration();
550 this->output = frame;
558 if(EQUIV(config.threshold, 0))
564 if(get_use_opengl()) return run_opengl();
566 if(!engine) engine = new ChromaKeyServer(this);
567 engine->process_packages();
574 const char* ChromaKey::plugin_title() { return _("Chroma key"); }
575 int ChromaKey::is_realtime() { return 1; }
577 NEW_WINDOW_MACRO(ChromaKey, ChromaKeyWindow)
579 LOAD_CONFIGURATION_MACRO(ChromaKey, ChromaKeyConfig)
582 void ChromaKey::save_data(KeyFrame *keyframe)
585 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
586 output.tag.set_title("CHROMAKEY");
587 output.tag.set_property("RED", config.red);
588 output.tag.set_property("GREEN", config.green);
589 output.tag.set_property("BLUE", config.blue);
590 output.tag.set_property("THRESHOLD", config.threshold);
591 output.tag.set_property("SLOPE", config.slope);
592 output.tag.set_property("USE_VALUE", config.use_value);
594 output.tag.set_title("/CHROMAKEY");
596 output.append_newline();
597 output.terminate_string();
600 void ChromaKey::read_data(KeyFrame *keyframe)
604 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
606 while(!input.read_tag())
608 if(input.tag.title_is("CHROMAKEY"))
610 config.red = input.tag.get_property("RED", config.red);
611 config.green = input.tag.get_property("GREEN", config.green);
612 config.blue = input.tag.get_property("BLUE", config.blue);
613 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
614 config.slope = input.tag.get_property("SLOPE", config.slope);
615 config.use_value = input.tag.get_property("USE_VALUE", config.use_value);
622 void ChromaKey::update_gui()
626 load_configuration();
627 thread->window->lock_window();
628 ((ChromaKeyWindow*)thread->window)->threshold->update(config.threshold);
629 ((ChromaKeyWindow*)thread->window)->slope->update(config.slope);
630 ((ChromaKeyWindow*)thread->window)->use_value->update(config.use_value);
631 ((ChromaKeyWindow*)thread->window)->update_sample();
633 thread->window->unlock_window();
637 int ChromaKey::handle_opengl()
640 OUTER_VARIABLES(this)
644 static const char *uniform_frag =
645 "uniform sampler2D tex;\n"
646 "uniform float min_v;\n"
647 "uniform float max_v;\n"
648 "uniform float run;\n"
649 "uniform float threshold;\n"
650 "uniform float threshold_run;\n"
651 "uniform vec3 key;\n";
653 static const char *get_yuvvalue_frag =
654 "float get_value(vec4 color)\n"
656 " return abs(color.r);\n"
659 static const char *get_rgbvalue_frag =
660 "float get_value(vec4 color)\n"
662 " return dot(color.rgb, vec3(0.29900, 0.58700, 0.11400));\n"
665 static const char *value_frag =
668 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
669 " float value = get_value(color);\n"
670 " float alpha = 1.0;\n"
672 " if(value >= min_v && value < max_v)\n"
675 " if(value < min_v)\n"
677 " if(min_v - value < run)\n"
678 " alpha = (min_v - value) / run;\n"
681 " if(value - max_v < run)\n"
682 " alpha = (value - max_v) / run;\n"
684 " gl_FragColor = vec4(color.rgb, alpha);\n"
687 static const char *cube_frag =
690 " vec4 color = texture2D(tex, gl_TexCoord[0].st);\n"
691 " float difference = length(color.rgb - key);\n"
692 " float alpha = 1.0;\n"
693 " if(difference < threshold)\n"
696 " if(difference < threshold_run)\n"
697 " alpha = (difference - threshold) / run;\n"
698 " gl_FragColor = vec4(color.rgb, min(color.a, alpha));\n"
703 get_output()->to_texture();
704 get_output()->enable_opengl();
705 get_output()->init_screen();
706 const char *shader_stack[] = { 0, 0, 0, 0, 0 };
707 int current_shader = 0;
709 shader_stack[current_shader++] = uniform_frag;
710 switch(get_output()->get_color_model())
716 shader_stack[current_shader++] = get_yuvvalue_frag;
717 shader_stack[current_shader++] = value_frag;
721 shader_stack[current_shader++] = cube_frag;
728 shader_stack[current_shader++] = get_rgbvalue_frag;
729 shader_stack[current_shader++] = value_frag;
733 shader_stack[current_shader++] = cube_frag;
739 unsigned int frag = VFrame::make_shader(0,
745 get_output()->bind_texture(0);
750 glUniform1i(glGetUniformLocation(frag, "tex"), 0);
751 glUniform1f(glGetUniformLocation(frag, "min_v"), min_v);
752 glUniform1f(glGetUniformLocation(frag, "max_v"), max_v);
753 glUniform1f(glGetUniformLocation(frag, "run"), run);
754 glUniform1f(glGetUniformLocation(frag, "threshold"), threshold);
755 glUniform1f(glGetUniformLocation(frag, "threshold_run"), threshold_run);
756 if(get_output()->get_color_model() != BC_YUV888 &&
757 get_output()->get_color_model() != BC_YUVA8888)
758 glUniform3f(glGetUniformLocation(frag, "key"),
759 r_key, g_key, b_key);
761 glUniform3f(glGetUniformLocation(frag, "key"),
762 (float)y_key / 0xff, (float)u_key / 0xff, (float)v_key / 0xff);
767 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
768 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
770 if(BC_CModels::components(get_output()->get_color_model()) == 3)
773 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
774 get_output()->clear_pbuffer();
778 get_output()->draw_texture();
781 get_output()->set_opengl_state(VFrame::SCREEN);
782 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
783 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);