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
26 #include "bcdisplayinfo.h"
32 #include "loadbalance.h"
34 #include "pluginvclient.h"
35 #include "playback3d.h"
50 void copy_from(DiffKeyConfig &src);
51 int equivalent(DiffKeyConfig &src);
52 void interpolate(DiffKeyConfig &prev,
56 int64_t current_frame);
64 class DiffKeyThreshold : public BC_FSlider
67 DiffKeyThreshold(DiffKey *plugin, int x, int y);
72 class DiffKeySlope : public BC_FSlider
75 DiffKeySlope(DiffKey *plugin, int x, int y);
80 class DiffKeyDoValue : public BC_CheckBox
83 DiffKeyDoValue(DiffKey *plugin, int x, int y);
90 class DiffKeyGUI : public PluginClientWindow
93 DiffKeyGUI(DiffKey *plugin);
97 void create_objects();
100 DiffKeyThreshold *threshold;
102 DiffKeyDoValue *do_value;
109 class DiffKeyEngine : public LoadServer
112 DiffKeyEngine(DiffKey *plugin);
113 void init_packages();
114 LoadClient* new_client();
115 LoadPackage* new_package();
120 class DiffKeyClient : public LoadClient
123 DiffKeyClient(DiffKeyEngine *engine);
126 void process_package(LoadPackage *pkg);
127 DiffKeyEngine *engine;
130 class DiffKeyPackage : public LoadPackage
140 class DiffKey : public PluginVClient
143 DiffKey(PluginServer *server);
146 int process_buffer(VFrame **frame,
147 int64_t start_position,
150 int is_multichannel();
151 void save_data(KeyFrame *keyframe);
152 void read_data(KeyFrame *keyframe);
158 PLUGIN_CLASS_MEMBERS(DiffKeyConfig)
160 DiffKeyEngine *engine;
162 VFrame *bottom_frame;
172 REGISTER_PLUGIN(DiffKey)
175 DiffKeyConfig::DiffKeyConfig()
182 void DiffKeyConfig::copy_from(DiffKeyConfig &src)
184 this->threshold = src.threshold;
185 this->slope = src.slope;
186 this->do_value = src.do_value;
190 int DiffKeyConfig::equivalent(DiffKeyConfig &src)
192 return EQUIV(threshold, src.threshold) &&
193 EQUIV(slope, src.slope) &&
194 do_value == src.do_value;
197 void DiffKeyConfig::interpolate(DiffKeyConfig &prev,
201 int64_t current_frame)
203 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
204 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
206 this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
207 this->slope = prev.slope * prev_scale + next.slope * next_scale;
208 this->do_value = prev.do_value;
220 DiffKeyThreshold::DiffKeyThreshold(DiffKey *plugin, int x, int y)
221 : BC_FSlider(x, y, 0, 200, 200, 0, 100, plugin->config.threshold)
223 this->plugin = plugin;
226 int DiffKeyThreshold::handle_event()
228 plugin->config.threshold = get_value();
229 plugin->send_configure_change();
240 DiffKeySlope::DiffKeySlope(DiffKey *plugin, int x, int y)
241 : BC_FSlider(x, y, 0, 200, 200, 0, 100, plugin->config.slope)
243 this->plugin = plugin;
246 int DiffKeySlope::handle_event()
248 plugin->config.slope = get_value();
249 plugin->send_configure_change();
255 DiffKeyDoValue::DiffKeyDoValue(DiffKey *plugin, int x, int y)
256 : BC_CheckBox(x, y, plugin->config.do_value, _("Use Value"))
258 this->plugin = plugin;
261 int DiffKeyDoValue::handle_event()
263 plugin->config.do_value = get_value();
264 plugin->send_configure_change();
274 DiffKeyGUI::DiffKeyGUI(DiffKey *plugin)
275 : PluginClientWindow(plugin,
282 this->plugin = plugin;
285 DiffKeyGUI::~DiffKeyGUI()
290 void DiffKeyGUI::create_objects()
294 add_subwindow(title = new BC_Title(x, y, _("Threshold:")));
295 x += title->get_w() + 10;
296 add_subwindow(threshold = new DiffKeyThreshold(plugin, x, y));
298 y += threshold->get_h() + 10;
299 add_subwindow(title = new BC_Title(x, y, _("Slope:")));
300 x += title->get_w() + 10;
301 add_subwindow(slope = new DiffKeySlope(plugin, x, y));
303 y += slope->get_h() + 10;
304 add_subwindow(do_value = new DiffKeyDoValue(plugin, x, y));
314 DiffKey::DiffKey(PluginServer *server)
315 : PluginVClient(server)
327 NEW_WINDOW_MACRO(DiffKey, DiffKeyGUI)
328 LOAD_CONFIGURATION_MACRO(DiffKey, DiffKeyConfig)
330 const char* DiffKey::plugin_title() { return N_("Difference key"); }
331 int DiffKey::is_realtime() { return 1; }
332 int DiffKey::is_multichannel() { return 1; }
335 void DiffKey::save_data(KeyFrame *keyframe)
338 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
339 output.tag.set_title("DIFFKEY");
340 output.tag.set_property("THRESHOLD", config.threshold);
341 output.tag.set_property("SLOPE", config.slope);
342 output.tag.set_property("DO_VALUE", config.do_value);
344 output.tag.set_title("/DIFFKEY");
346 output.append_newline();
347 output.terminate_string();
350 void DiffKey::read_data(KeyFrame *keyframe)
354 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
356 while(!input.read_tag())
358 if(input.tag.title_is("DIFFKEY"))
360 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
361 config.slope = input.tag.get_property("SLOPE", config.slope);
362 config.do_value = input.tag.get_property("DO_VALUE", config.do_value);
367 void DiffKey::update_gui()
371 if(load_configuration())
373 thread->window->lock_window("DiffKey::update_gui");
374 ((DiffKeyGUI*)thread->window)->threshold->update(config.threshold);
375 ((DiffKeyGUI*)thread->window)->slope->update(config.slope);
376 ((DiffKeyGUI*)thread->window)->do_value->update(config.do_value);
377 thread->window->unlock_window();
382 int DiffKey::process_buffer(VFrame **frame,
383 int64_t start_position,
386 load_configuration();
388 // Don't process if only 1 layer.
389 if(get_total_buffers() < 2)
399 // Read frames from 2 layers
411 top_frame = frame[0];
412 bottom_frame = frame[1];
419 engine = new DiffKeyEngine(this);
422 engine->process_packages();
427 #define DIFFKEY_VARS(plugin) \
428 float threshold = plugin->config.threshold / 100; \
429 float pad = plugin->config.slope / 100; \
430 float threshold_pad = threshold + pad; \
433 int DiffKey::handle_opengl()
436 static const char *diffkey_head =
437 "uniform sampler2D tex_bg;\n"
438 "uniform sampler2D tex_fg;\n"
439 "uniform float threshold;\n"
440 "uniform float pad;\n"
441 "uniform float threshold_pad;\n"
444 " vec4 foreground = texture2D(tex_fg, gl_TexCoord[0].st);\n"
445 " vec4 background = texture2D(tex_bg, gl_TexCoord[0].st);\n";
447 static const char *colorcube =
448 " float difference = length(foreground.rgb - background.rgb);\n";
450 static const char *yuv_value =
451 " float difference = abs(foreground.r - background.r);\n";
453 static const char *rgb_value =
454 " float difference = abs("
455 " dot(foreground.rgb, rgb_to_y_vector) - "
456 " dot(background.rgb, rgb_to_y_vector));\n";
458 static const char *diffkey_tail =
460 " if(difference < threshold)\n"
463 " if(difference < threshold_pad)\n"
464 " result.a = (difference - threshold) / pad;\n"
467 " result.rgb = foreground.rgb;\n"
468 " gl_FragColor = result;\n"
471 top_frame->to_texture();
472 bottom_frame->to_texture();
474 top_frame->enable_opengl();
475 top_frame->init_screen();
477 const char *shader_stack[16];
478 memset(shader_stack,0, sizeof(shader_stack));
479 int current_shader = 0;
481 int need_rgb_to_y = 0;
482 const char *shader_frag = !config.do_value ? colorcube :
483 BC_CModels::is_yuv(top_frame->get_color_model()) ?
484 yuv_value : (need_rgb_to_y = 1, rgb_value);
486 shader_stack[current_shader++] = bc_gl_rgb_to_y;
487 shader_stack[current_shader++] = diffkey_head;
488 shader_stack[current_shader++] = shader_frag;
489 shader_stack[current_shader++] = diffkey_tail;
491 shader_stack[current_shader] = 0;
492 unsigned int shader = VFrame::make_shader(shader_stack);
495 bottom_frame->bind_texture(1);
496 top_frame->bind_texture(0);
499 glUseProgram(shader);
500 glUniform1i(glGetUniformLocation(shader, "tex_fg"), 0);
501 glUniform1i(glGetUniformLocation(shader, "tex_bg"), 1);
502 glUniform1f(glGetUniformLocation(shader, "threshold"), threshold);
503 glUniform1f(glGetUniformLocation(shader, "pad"), pad);
504 glUniform1f(glGetUniformLocation(shader, "threshold_pad"), threshold_pad);
506 BC_GL_MATRIX(shader, rgb_to_y_vector);
509 if(BC_CModels::components(get_output()->get_color_model()) == 3)
512 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
513 top_frame->clear_pbuffer();
516 top_frame->draw_texture();
518 top_frame->set_opengl_state(VFrame::SCREEN);
520 // Fastest way to discard output
521 bottom_frame->set_opengl_state(VFrame::UNKNOWN);
524 top_frame->screen_to_ram();
532 DiffKeyEngine::DiffKeyEngine(DiffKey *plugin)
533 : LoadServer(plugin->get_project_smp() + 1, plugin->get_project_smp() + 1)
535 this->plugin = plugin;
538 void DiffKeyEngine::init_packages()
540 int increment = plugin->top_frame->get_h() / get_total_packages() + 1;
542 for(int i = 0; i < get_total_packages(); i++)
544 DiffKeyPackage *pkg = (DiffKeyPackage*)get_package(i);
546 pkg->row2 = MIN(y + increment, plugin->top_frame->get_h());
551 LoadClient* DiffKeyEngine::new_client()
553 return new DiffKeyClient(this);
556 LoadPackage* DiffKeyEngine::new_package()
558 return new DiffKeyPackage;
571 DiffKeyClient::DiffKeyClient(DiffKeyEngine *engine)
574 this->engine = engine;
577 DiffKeyClient::~DiffKeyClient()
581 void DiffKeyClient::process_package(LoadPackage *ptr)
583 DiffKeyPackage *pkg = (DiffKeyPackage*)ptr;
584 DiffKey *plugin = engine->plugin;
585 int w = plugin->top_frame->get_w();
587 #define RGB_TO_VALUE(r, g, b) YUV::yuv.rgb_to_y_f((r),(g),(b))
590 #define DIFFKEY_MACRO(type, components, max, chroma_offset) \
593 for(int i = pkg->row1; i < pkg->row2; i++) \
595 type *top_row = (type*)plugin->top_frame->get_rows()[i]; \
596 type *bottom_row = (type*)plugin->bottom_frame->get_rows()[i]; \
598 for(int j = 0; j < w; j++) \
602 /* Test for value in range */ \
603 if(plugin->config.do_value) \
606 float bottom_value; \
608 /* Convert pixel data into floating point value */ \
611 float top_r = (float)top_row[0] / max; \
612 float bottom_r = (float)bottom_row[0] / max; \
614 bottom_value = bottom_r; \
618 float top_r = (float)top_row[0] / max; \
619 float top_g = (float)top_row[1] / max; \
620 float top_b = (float)top_row[2] / max; \
621 top_g -= (float)chroma_offset / max; \
622 top_b -= (float)chroma_offset / max; \
624 float bottom_r = (float)bottom_row[0] / max; \
625 float bottom_g = (float)bottom_row[1] / max; \
626 float bottom_b = (float)bottom_row[2] / max; \
627 bottom_g -= (float)chroma_offset / max; \
628 bottom_b -= (float)chroma_offset / max; \
630 top_value = RGB_TO_VALUE(top_r, top_g, top_b); \
631 bottom_value = RGB_TO_VALUE(bottom_r, bottom_g, bottom_b); \
634 float min_v = bottom_value - threshold; \
635 float max_v = bottom_value + threshold; \
637 /* Full transparency if in range */ \
638 if(top_value >= min_v && top_value < max_v) \
643 /* Phased out if below or above range */ \
644 if(top_value < min_v) \
646 if(min_v - top_value < pad) \
647 a = (min_v - top_value) / pad; \
650 if(top_value - max_v < pad) \
651 a = (top_value - max_v) / pad; \
654 /* Use color cube */ \
656 float top_r = (float)top_row[0] / max; \
657 float top_g = (float)top_row[1] / max; \
658 float top_b = (float)top_row[2] / max; \
659 top_g -= (float)chroma_offset / max; \
660 top_b -= (float)chroma_offset / max; \
662 float bottom_r = (float)bottom_row[0] / max; \
663 float bottom_g = (float)bottom_row[1] / max; \
664 float bottom_b = (float)bottom_row[2] / max; \
665 bottom_g -= (float)chroma_offset / max; \
666 bottom_b -= (float)chroma_offset / max; \
669 float difference = sqrt(SQR(top_r - bottom_r) + \
670 SQR(top_g - bottom_g) + \
671 SQR(top_b - bottom_b)); \
673 if(difference < threshold) \
678 if(difference < threshold_pad) \
680 a = (difference - threshold) / pad; \
684 /* multiply alpha */ \
685 if(components == 4) \
687 top_row[3] = MIN((type)(a * max), top_row[3]); \
691 top_row[0] = (type)(a * top_row[0]); \
692 top_row[1] = (type)(a * (top_row[1] - chroma_offset) + chroma_offset); \
693 top_row[2] = (type)(a * (top_row[2] - chroma_offset) + chroma_offset); \
696 top_row += components; \
697 bottom_row += components; \
704 switch(plugin->top_frame->get_color_model())
707 DIFFKEY_MACRO(float, 3, 1.0, 0);
710 DIFFKEY_MACRO(float, 4, 1.0, 0);
713 DIFFKEY_MACRO(unsigned char, 3, 0xff, 0);
716 DIFFKEY_MACRO(unsigned char, 4, 0xff, 0);
719 DIFFKEY_MACRO(unsigned char, 3, 0xff, 0x80);
722 DIFFKEY_MACRO(unsigned char, 4, 0xff, 0x80);
733 DiffKeyPackage::DiffKeyPackage()