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"
28 #include "loadbalance.h"
30 #include "playback3d.h"
31 #include "pluginvclient.h"
42 #define MINSATURATION -100
43 #define MAXSATURATION 100
57 void copy_from(HueConfig &src);
58 int equivalent(HueConfig &src);
59 void interpolate(HueConfig &prev,
64 float hue, saturation, value;
67 class HueSlider : public BC_FSlider
70 HueSlider(HueEffect *plugin, int x, int y, int w);
75 class SaturationSlider : public BC_FSlider
78 SaturationSlider(HueEffect *plugin, int x, int y, int w);
82 char string[BCTEXTLEN];
85 class ValueSlider : public BC_FSlider
88 ValueSlider(HueEffect *plugin, int x, int y, int w);
92 char string[BCTEXTLEN];
95 class HueWindow : public PluginClientWindow
98 HueWindow(HueEffect *plugin);
99 void create_objects();
102 SaturationSlider *saturation;
108 class HueEngine : public LoadServer
111 HueEngine(HueEffect *plugin, int cpus);
112 void init_packages();
113 LoadClient* new_client();
114 LoadPackage* new_package();
118 class HuePackage : public LoadPackage
125 class HueUnit : public LoadClient
128 HueUnit(HueEffect *plugin, HueEngine *server);
129 void process_package(LoadPackage *package);
134 class HueEffect : public PluginVClient
137 HueEffect(PluginServer *server);
141 PLUGIN_CLASS_MEMBERS(HueConfig);
142 int process_buffer(VFrame *frame,
143 int64_t start_position,
146 void save_data(KeyFrame *keyframe);
147 void read_data(KeyFrame *keyframe);
151 VFrame *input, *output;
174 HueConfig::HueConfig()
176 hue = saturation = value = 0;
179 void HueConfig::copy_from(HueConfig &src)
182 saturation = src.saturation;
185 int HueConfig::equivalent(HueConfig &src)
187 return EQUIV(hue, src.hue) &&
188 EQUIV(saturation, src.saturation) &&
189 EQUIV(value, src.value);
191 void HueConfig::interpolate(HueConfig &prev,
197 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
198 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
200 this->hue = prev.hue * prev_scale + next.hue * next_scale;
201 this->saturation = prev.saturation * prev_scale + next.saturation * next_scale;
202 this->value = prev.value * prev_scale + next.value * next_scale;
212 HueSlider::HueSlider(HueEffect *plugin, int x, int y, int w)
222 this->plugin = plugin;
224 int HueSlider::handle_event()
226 plugin->config.hue = get_value();
227 plugin->send_configure_change();
237 SaturationSlider::SaturationSlider(HueEffect *plugin, int x, int y, int w)
243 (float)MINSATURATION,
244 (float)MAXSATURATION,
245 plugin->config.saturation)
247 this->plugin = plugin;
249 int SaturationSlider::handle_event()
251 plugin->config.saturation = get_value();
252 plugin->send_configure_change();
256 char* SaturationSlider::get_caption()
258 float fraction = ((float)plugin->config.saturation - MINSATURATION) /
260 sprintf(string, "%0.4f", fraction);
270 ValueSlider::ValueSlider(HueEffect *plugin, int x, int y, int w)
278 plugin->config.value)
280 this->plugin = plugin;
282 int ValueSlider::handle_event()
284 plugin->config.value = get_value();
285 plugin->send_configure_change();
289 char* ValueSlider::get_caption()
291 float fraction = ((float)plugin->config.value - MINVALUE) / MAXVALUE;
292 sprintf(string, "%0.4f", fraction);
302 HueWindow::HueWindow(HueEffect *plugin)
303 : PluginClientWindow(plugin,
304 345, 100, 345, 100, 0)
306 this->plugin = plugin;
308 void HueWindow::create_objects()
310 int x = 10, y = 10, x1 = 100;
311 add_subwindow(new BC_Title(x, y, _("Hue:")));
312 add_subwindow(hue = new HueSlider(plugin, x1, y, 200));
314 add_subwindow(new BC_Title(x, y, _("Saturation:")));
315 add_subwindow(saturation = new SaturationSlider(plugin, x1, y, 200));
317 add_subwindow(new BC_Title(x, y, _("Value:")));
318 add_subwindow(value = new ValueSlider(plugin, x1, y, 200));
333 HueEngine::HueEngine(HueEffect *plugin, int cpus)
334 : LoadServer(cpus, cpus)
336 this->plugin = plugin;
338 void HueEngine::init_packages()
340 for(int i = 0; i < LoadServer::get_total_packages(); i++)
342 HuePackage *pkg = (HuePackage*)get_package(i);
343 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
344 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
347 LoadClient* HueEngine::new_client()
349 return new HueUnit(plugin, this);
351 LoadPackage* HueEngine::new_package()
353 return new HuePackage;
363 HuePackage::HuePackage()
368 HueUnit::HueUnit(HueEffect *plugin, HueEngine *server)
371 this->plugin = plugin;
380 #define HUESATURATION(type, max, components, use_yuv) \
382 float h_offset = plugin->config.hue; \
383 float s_offset = ((float)plugin->config.saturation - MINSATURATION) / MAXSATURATION; \
384 float v_offset = ((float)plugin->config.value - MINVALUE) / MAXVALUE; \
385 for(int i = pkg->row1; i < pkg->row2; i++) \
387 type* in_row = (type*)plugin->input->get_rows()[i]; \
388 type* out_row = (type*)plugin->output->get_rows()[i]; \
390 for(int j = 0; j < w; j++) \
399 y = (int)in_row[0]; \
400 u = (int)in_row[1]; \
401 v = (int)in_row[2]; \
403 yuv.yuv_to_rgb_16(r_i, g_i, b_i, y, u, v); \
405 yuv.yuv_to_rgb_8(r_i, g_i, b_i, y, u, v); \
406 HSV::rgb_to_hsv((float)r_i / max, \
415 r = (float)in_row[0] / max; \
416 g = (float)in_row[1] / max; \
417 b = (float)in_row[2] / max; \
418 HSV::rgb_to_hsv(r, g, b, h, s, va); \
426 if(h >= 360) h -= 360; \
427 if(h < 0) h += 360; \
428 if(sizeof(type) < 4) \
438 HSV::hsv_to_yuv(y, u, v, h, s, va, max); \
445 HSV::hsv_to_rgb(r, g, b, h, s, va); \
446 if(sizeof(type) < 4) \
451 out_row[0] = (type)CLIP(r, 0, max); \
452 out_row[1] = (type)CLIP(g, 0, max); \
453 out_row[2] = (type)CLIP(b, 0, max); \
457 out_row[0] = (type)r; \
458 out_row[1] = (type)g; \
459 out_row[2] = (type)b; \
463 in_row += components; \
464 out_row += components; \
470 void HueUnit::process_package(LoadPackage *package)
472 HuePackage *pkg = (HuePackage*)package;
473 int w = plugin->input->get_w();
475 switch(plugin->input->get_color_model())
478 HUESATURATION(unsigned char, 0xff, 3, 0)
482 HUESATURATION(float, 1, 3, 0)
486 HUESATURATION(unsigned char, 0xff, 3, 1)
490 HUESATURATION(uint16_t, 0xffff, 3, 0)
494 HUESATURATION(uint16_t, 0xffff, 3, 1)
498 HUESATURATION(float, 1, 4, 0)
502 HUESATURATION(unsigned char, 0xff, 4, 0)
506 HUESATURATION(unsigned char, 0xff, 4, 1)
509 case BC_RGBA16161616:
510 HUESATURATION(uint16_t, 0xffff, 4, 0)
513 case BC_YUVA16161616:
514 HUESATURATION(uint16_t, 0xffff, 4, 1)
523 REGISTER_PLUGIN(HueEffect)
526 HueEffect::HueEffect(PluginServer *server)
527 : PluginVClient(server)
532 HueEffect::~HueEffect()
535 if(engine) delete engine;
538 int HueEffect::process_buffer(VFrame *frame,
539 int64_t start_position,
542 load_configuration();
552 this->output = frame;
553 if(EQUIV(config.hue, 0) && EQUIV(config.saturation, 0) && EQUIV(config.value, 0))
565 if(!engine) engine = new HueEngine(this, PluginClient::smp + 1);
567 engine->process_packages();
572 const char* HueEffect::plugin_title() { return _("Hue saturation"); }
573 int HueEffect::is_realtime() { return 1; }
575 NEW_WINDOW_MACRO(HueEffect, HueWindow)
576 LOAD_CONFIGURATION_MACRO(HueEffect, HueConfig)
579 void HueEffect::save_data(KeyFrame *keyframe)
582 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
583 output.tag.set_title("HUESATURATION");
584 output.tag.set_property("HUE", config.hue);
585 output.tag.set_property("SATURATION", config.saturation);
586 output.tag.set_property("VALUE", config.value);
588 output.tag.set_title("/HUESATURATION");
590 output.append_newline();
591 output.terminate_string();
593 void HueEffect::read_data(KeyFrame *keyframe)
596 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
597 while(!input.read_tag())
599 if(input.tag.title_is("HUESATURATION"))
601 config.hue = input.tag.get_property("HUE", config.hue);
602 config.saturation = input.tag.get_property("SATURATION", config.saturation);
603 config.value = input.tag.get_property("VALUE", config.value);
607 void HueEffect::update_gui()
611 ((HueWindow*)thread->window)->lock_window();
612 load_configuration();
613 ((HueWindow*)thread->window)->hue->update(config.hue);
614 ((HueWindow*)thread->window)->saturation->update(config.saturation);
615 ((HueWindow*)thread->window)->value->update(config.value);
616 ((HueWindow*)thread->window)->unlock_window();
620 int HueEffect::handle_opengl()
623 const char *yuv_saturation_frag =
624 "uniform sampler2D tex;\n"
625 "uniform float s_offset;\n"
626 "uniform float v_offset;\n"
629 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
630 " pixel.r *= v_offset;\n"
631 " pixel.gb -= vec2(0.5, 0.5);\n"
632 " pixel.g *= s_offset;\n"
633 " pixel.b *= s_offset;\n"
634 " pixel.gb += vec2(0.5, 0.5);\n"
635 " gl_FragColor = pixel;\n"
639 const char *yuv_frag =
640 "uniform sampler2D tex;\n"
641 "uniform float h_offset;\n"
642 "uniform float s_offset;\n"
643 "uniform float v_offset;\n"
646 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
647 YUV_TO_RGB_FRAG("pixel")
648 RGB_TO_HSV_FRAG("pixel")
649 " pixel.r += h_offset;\n"
650 " pixel.g *= s_offset;\n"
651 " pixel.b *= v_offset;\n"
652 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
653 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
654 HSV_TO_RGB_FRAG("pixel")
655 RGB_TO_YUV_FRAG("pixel")
656 " gl_FragColor = pixel;\n"
659 const char *rgb_frag =
660 "uniform sampler2D tex;\n"
661 "uniform float h_offset;\n"
662 "uniform float s_offset;\n"
663 "uniform float v_offset;\n"
666 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
667 RGB_TO_HSV_FRAG("pixel")
668 " pixel.r += h_offset;\n"
669 " pixel.g *= s_offset;\n"
670 " pixel.b *= v_offset;\n"
671 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
672 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
673 HSV_TO_RGB_FRAG("pixel")
674 " gl_FragColor = pixel;\n"
678 get_output()->to_texture();
679 get_output()->enable_opengl();
681 unsigned int frag_shader = 0;
682 switch(get_output()->get_color_model())
686 // This is a lousy approximation but good enough for the masker.
687 if(EQUIV(config.hue, 0))
688 frag_shader = VFrame::make_shader(0,
692 frag_shader = VFrame::make_shader(0,
697 frag_shader = VFrame::make_shader(0,
706 glUseProgram(frag_shader);
707 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
708 glUniform1f(glGetUniformLocation(frag_shader, "h_offset"), config.hue);
709 glUniform1f(glGetUniformLocation(frag_shader, "s_offset"),
710 ((float)config.saturation - MINSATURATION) / MAXSATURATION);
711 glUniform1f(glGetUniformLocation(frag_shader, "v_offset"),
712 ((float)config.value - MINVALUE) / MAXVALUE);
715 get_output()->init_screen();
716 get_output()->bind_texture(0);
717 get_output()->draw_texture();
719 get_output()->set_opengl_state(VFrame::SCREEN);