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, 345, 100, 345, 100, 0)
305 this->plugin = plugin;
307 void HueWindow::create_objects()
309 int x = 10, y = 10, x1 = 100;
310 add_subwindow(new BC_Title(x, y, _("Hue:")));
311 add_subwindow(hue = new HueSlider(plugin, x1, y, 200));
313 add_subwindow(new BC_Title(x, y, _("Saturation:")));
314 add_subwindow(saturation = new SaturationSlider(plugin, x1, y, 200));
316 add_subwindow(new BC_Title(x, y, _("Value:")));
317 add_subwindow(value = new ValueSlider(plugin, x1, y, 200));
332 HueEngine::HueEngine(HueEffect *plugin, int cpus)
333 : LoadServer(cpus, cpus)
335 this->plugin = plugin;
337 void HueEngine::init_packages()
339 for(int i = 0; i < LoadServer::get_total_packages(); i++)
341 HuePackage *pkg = (HuePackage*)get_package(i);
342 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
343 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
346 LoadClient* HueEngine::new_client()
348 return new HueUnit(plugin, this);
350 LoadPackage* HueEngine::new_package()
352 return new HuePackage;
362 HuePackage::HuePackage()
367 HueUnit::HueUnit(HueEffect *plugin, HueEngine *server)
370 this->plugin = plugin;
379 #define HUESATURATION(type, max, components, use_yuv) \
381 float h_offset = plugin->config.hue; \
382 float s_offset = ((float)plugin->config.saturation - MINSATURATION) / MAXSATURATION; \
383 float v_offset = ((float)plugin->config.value - MINVALUE) / MAXVALUE; \
384 for(int i = pkg->row1; i < pkg->row2; i++) \
386 type* in_row = (type*)plugin->input->get_rows()[i]; \
387 type* out_row = (type*)plugin->output->get_rows()[i]; \
389 for(int j = 0; j < w; j++) \
398 y = (int)in_row[0]; \
399 u = (int)in_row[1]; \
400 v = (int)in_row[2]; \
402 yuv.yuv_to_rgb_16(r_i, g_i, b_i, y, u, v); \
404 yuv.yuv_to_rgb_8(r_i, g_i, b_i, y, u, v); \
405 HSV::rgb_to_hsv((float)r_i / max, \
414 r = (float)in_row[0] / max; \
415 g = (float)in_row[1] / max; \
416 b = (float)in_row[2] / max; \
417 HSV::rgb_to_hsv(r, g, b, h, s, va); \
425 if(h >= 360) h -= 360; \
426 if(h < 0) h += 360; \
427 if(sizeof(type) < 4) \
437 HSV::hsv_to_yuv(y, u, v, h, s, va, max); \
444 HSV::hsv_to_rgb(r, g, b, h, s, va); \
445 if(sizeof(type) < 4) \
450 out_row[0] = (type)CLIP(r, 0, max); \
451 out_row[1] = (type)CLIP(g, 0, max); \
452 out_row[2] = (type)CLIP(b, 0, max); \
456 out_row[0] = (type)r; \
457 out_row[1] = (type)g; \
458 out_row[2] = (type)b; \
462 in_row += components; \
463 out_row += components; \
469 void HueUnit::process_package(LoadPackage *package)
471 HuePackage *pkg = (HuePackage*)package;
472 int w = plugin->input->get_w();
474 switch(plugin->input->get_color_model())
477 HUESATURATION(unsigned char, 0xff, 3, 0)
481 HUESATURATION(float, 1, 3, 0)
485 HUESATURATION(unsigned char, 0xff, 3, 1)
489 HUESATURATION(uint16_t, 0xffff, 3, 0)
493 HUESATURATION(uint16_t, 0xffff, 3, 1)
497 HUESATURATION(float, 1, 4, 0)
501 HUESATURATION(unsigned char, 0xff, 4, 0)
505 HUESATURATION(unsigned char, 0xff, 4, 1)
508 case BC_RGBA16161616:
509 HUESATURATION(uint16_t, 0xffff, 4, 0)
512 case BC_YUVA16161616:
513 HUESATURATION(uint16_t, 0xffff, 4, 1)
522 REGISTER_PLUGIN(HueEffect)
525 HueEffect::HueEffect(PluginServer *server)
526 : PluginVClient(server)
531 HueEffect::~HueEffect()
534 if(engine) delete engine;
537 int HueEffect::process_buffer(VFrame *frame,
538 int64_t start_position,
541 load_configuration();
551 this->output = frame;
552 if(EQUIV(config.hue, 0) && EQUIV(config.saturation, 0) && EQUIV(config.value, 0))
564 if(!engine) engine = new HueEngine(this, PluginClient::smp + 1);
566 engine->process_packages();
571 const char* HueEffect::plugin_title() { return _("Hue saturation"); }
572 int HueEffect::is_realtime() { return 1; }
574 NEW_WINDOW_MACRO(HueEffect, HueWindow)
575 LOAD_CONFIGURATION_MACRO(HueEffect, HueConfig)
578 void HueEffect::save_data(KeyFrame *keyframe)
581 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
582 output.tag.set_title("HUESATURATION");
583 output.tag.set_property("HUE", config.hue);
584 output.tag.set_property("SATURATION", config.saturation);
585 output.tag.set_property("VALUE", config.value);
587 output.tag.set_title("/HUESATURATION");
589 output.append_newline();
590 output.terminate_string();
592 void HueEffect::read_data(KeyFrame *keyframe)
595 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
596 while(!input.read_tag())
598 if(input.tag.title_is("HUESATURATION"))
600 config.hue = input.tag.get_property("HUE", config.hue);
601 config.saturation = input.tag.get_property("SATURATION", config.saturation);
602 config.value = input.tag.get_property("VALUE", config.value);
606 void HueEffect::update_gui()
610 ((HueWindow*)thread->window)->lock_window();
611 load_configuration();
612 ((HueWindow*)thread->window)->hue->update(config.hue);
613 ((HueWindow*)thread->window)->saturation->update(config.saturation);
614 ((HueWindow*)thread->window)->value->update(config.value);
615 ((HueWindow*)thread->window)->unlock_window();
619 int HueEffect::handle_opengl()
622 const char *yuv_saturation_frag =
623 "uniform sampler2D tex;\n"
624 "uniform float s_offset;\n"
625 "uniform float v_offset;\n"
628 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
629 " pixel.r *= v_offset;\n"
630 " pixel.gb -= vec2(0.5, 0.5);\n"
631 " pixel.g *= s_offset;\n"
632 " pixel.b *= s_offset;\n"
633 " pixel.gb += vec2(0.5, 0.5);\n"
634 " gl_FragColor = pixel;\n"
638 const char *yuv_frag =
639 "uniform sampler2D tex;\n"
640 "uniform float h_offset;\n"
641 "uniform float s_offset;\n"
642 "uniform float v_offset;\n"
645 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
646 YUV_TO_RGB_FRAG("pixel")
647 RGB_TO_HSV_FRAG("pixel")
648 " pixel.r += h_offset;\n"
649 " pixel.g *= s_offset;\n"
650 " pixel.b *= v_offset;\n"
651 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
652 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
653 HSV_TO_RGB_FRAG("pixel")
654 RGB_TO_YUV_FRAG("pixel")
655 " gl_FragColor = pixel;\n"
658 const char *rgb_frag =
659 "uniform sampler2D tex;\n"
660 "uniform float h_offset;\n"
661 "uniform float s_offset;\n"
662 "uniform float v_offset;\n"
665 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
666 RGB_TO_HSV_FRAG("pixel")
667 " pixel.r += h_offset;\n"
668 " pixel.g *= s_offset;\n"
669 " pixel.b *= v_offset;\n"
670 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
671 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
672 HSV_TO_RGB_FRAG("pixel")
673 " gl_FragColor = pixel;\n"
677 get_output()->to_texture();
678 get_output()->enable_opengl();
680 unsigned int frag_shader = 0;
681 switch(get_output()->get_color_model())
685 // This is a lousy approximation but good enough for the masker.
686 if(EQUIV(config.hue, 0))
687 frag_shader = VFrame::make_shader(0,
691 frag_shader = VFrame::make_shader(0,
696 frag_shader = VFrame::make_shader(0,
705 glUseProgram(frag_shader);
706 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
707 glUniform1f(glGetUniformLocation(frag_shader, "h_offset"), config.hue);
708 glUniform1f(glGetUniformLocation(frag_shader, "s_offset"),
709 ((float)config.saturation - MINSATURATION) / MAXSATURATION);
710 glUniform1f(glGetUniformLocation(frag_shader, "v_offset"),
711 ((float)config.value - MINVALUE) / MAXVALUE);
714 get_output()->init_screen();
715 get_output()->bind_texture(0);
716 get_output()->draw_texture();
718 get_output()->set_opengl_state(VFrame::SCREEN);