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
27 #include "bcdisplayinfo.h"
31 #include "linearblur.h"
34 #include "loadbalance.h"
35 #include "pluginvclient.h"
43 REGISTER_PLUGIN(LinearBlurMain)
48 LinearBlurConfig::LinearBlurConfig()
50 reset(RESET_DEFAULT_SETTINGS);
53 void LinearBlurConfig::reset(int clear)
65 case RESET_RADIUS : radius = 0;
67 case RESET_ANGLE : angle = 0;
69 case RESET_STEPS : steps = 1;
71 case RESET_DEFAULT_SETTINGS :
84 int LinearBlurConfig::equivalent(LinearBlurConfig &that)
87 radius == that.radius &&
88 angle == that.angle &&
89 steps == that.steps &&
96 void LinearBlurConfig::copy_from(LinearBlurConfig &that)
107 void LinearBlurConfig::interpolate(LinearBlurConfig &prev,
108 LinearBlurConfig &next,
113 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
114 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
115 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
116 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
117 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
135 LinearBlurWindow::LinearBlurWindow(LinearBlurMain *plugin)
136 : PluginClientWindow(plugin,
143 this->plugin = plugin;
146 LinearBlurWindow::~LinearBlurWindow()
150 void LinearBlurWindow::create_objects()
153 int x1 = 0; int clrBtn_w = 50;
154 int defaultBtn_w = 100;
156 add_subwindow(new BC_Title(x, y, _("Length:")));
158 add_subwindow(radius = new LinearBlurSize(plugin, x, y, &plugin->config.radius, 0, 100));
159 x1 = x + radius->get_w() + 10;
160 add_subwindow(radiusClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_RADIUS));
163 add_subwindow(new BC_Title(x, y, _("Angle:")));
165 add_subwindow(angle = new LinearBlurSize(plugin, x, y, &plugin->config.angle, -180, 180));
166 add_subwindow(angleClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_ANGLE));
169 add_subwindow(new BC_Title(x, y, _("Steps:")));
171 add_subwindow(steps = new LinearBlurSize(plugin, x, y, &plugin->config.steps, 1, 200));
172 add_subwindow(stepsClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_STEPS));
175 add_subwindow(r = new LinearBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
177 add_subwindow(g = new LinearBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
179 add_subwindow(b = new LinearBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
181 add_subwindow(a = new LinearBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
183 add_subwindow(reset = new LinearBlurReset(plugin, this, x, y));
184 add_subwindow(default_settings = new LinearBlurDefaultSettings(plugin, this,
185 (280 - 10 - defaultBtn_w), y, defaultBtn_w));
193 void LinearBlurWindow::update_gui(int clear)
196 case RESET_RADIUS : radius->update(plugin->config.radius);
198 case RESET_ANGLE : angle->update(plugin->config.angle);
200 case RESET_STEPS : steps->update(plugin->config.steps);
203 case RESET_DEFAULT_SETTINGS :
205 radius->update(plugin->config.radius);
206 angle->update(plugin->config.angle);
207 steps->update(plugin->config.steps);
208 r->update(plugin->config.r);
209 g->update(plugin->config.g);
210 b->update(plugin->config.b);
211 a->update(plugin->config.a);
225 LinearBlurToggle::LinearBlurToggle(LinearBlurMain *plugin,
230 : BC_CheckBox(x, y, *output, string)
232 this->plugin = plugin;
233 this->output = output;
236 int LinearBlurToggle::handle_event()
238 *output = get_value();
239 plugin->send_configure_change();
249 LinearBlurSize::LinearBlurSize(LinearBlurMain *plugin,
255 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
257 this->plugin = plugin;
258 this->output = output;
260 int LinearBlurSize::handle_event()
262 *output = get_value();
263 plugin->send_configure_change();
268 LinearBlurReset::LinearBlurReset(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y)
269 : BC_GenericButton(x, y, _("Reset"))
271 this->plugin = plugin;
274 LinearBlurReset::~LinearBlurReset()
277 int LinearBlurReset::handle_event()
279 plugin->config.reset(RESET_ALL);
280 gui->update_gui(RESET_ALL);
281 plugin->send_configure_change();
286 LinearBlurDefaultSettings::LinearBlurDefaultSettings(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int w)
287 : BC_GenericButton(x, y, w, _("Default"))
289 this->plugin = plugin;
292 LinearBlurDefaultSettings::~LinearBlurDefaultSettings()
295 int LinearBlurDefaultSettings::handle_event()
297 plugin->config.reset(RESET_DEFAULT_SETTINGS);
298 gui->update_gui(RESET_DEFAULT_SETTINGS);
299 plugin->send_configure_change();
304 LinearBlurSliderClr::LinearBlurSliderClr(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int w, int clear)
305 : BC_GenericButton(x, y, w, _("⌂"))
307 this->plugin = plugin;
311 LinearBlurSliderClr::~LinearBlurSliderClr()
314 int LinearBlurSliderClr::handle_event()
316 // clear==1 ==> Radius slider
317 // clear==2 ==> Angle slider
318 // clear==3 ==> Steps slider
319 plugin->config.reset(clear);
320 gui->update_gui(clear);
321 plugin->send_configure_change();
332 LinearBlurMain::LinearBlurMain(PluginServer *server)
333 : PluginVClient(server)
341 need_reconfigure = 1;
346 LinearBlurMain::~LinearBlurMain()
349 if(engine) delete engine;
351 if(accum) delete [] accum;
352 if(temp) delete temp;
355 const char* LinearBlurMain::plugin_title() { return N_("Linear Blur"); }
356 int LinearBlurMain::is_realtime() { return 1; }
359 NEW_WINDOW_MACRO(LinearBlurMain, LinearBlurWindow)
360 LOAD_CONFIGURATION_MACRO(LinearBlurMain, LinearBlurConfig)
362 void LinearBlurMain::delete_tables()
366 for(int i = 0; i < table_entries; i++)
367 delete [] scale_x_table[i];
368 delete [] scale_x_table;
373 for(int i = 0; i < table_entries; i++)
374 delete [] scale_y_table[i];
375 delete [] scale_y_table;
377 delete [] layer_table;
384 int LinearBlurMain::process_buffer(VFrame *frame,
385 int64_t start_position,
388 need_reconfigure |= load_configuration();
392 get_source_position(),
396 // Generate tables here. The same table is used by many packages to render
397 // each horizontal stripe. Need to cover the entire output range in each
398 // table to avoid green borders
401 int w = frame->get_w();
402 int h = frame->get_h();
405 int angle = config.angle;
406 int radius = config.radius * MIN(w, h) / 100;
408 while(angle < 0) angle += 360;
429 y_offset = (int)(sin((float)config.angle / 360 * 2 * M_PI) * radius);
430 x_offset = (int)(cos((float)config.angle / 360 * 2 * M_PI) * radius);
436 scale_x_table = new int*[config.steps];
437 scale_y_table = new int*[config.steps];
438 table_entries = config.steps;
439 layer_table = new LinearBlurLayer[table_entries];
441 //printf("LinearBlurMain::process_realtime 1 %d %d %d\n", radius, x_offset, y_offset);
443 for(int i = 0; i < config.steps; i++)
445 float fraction = (float)(i - config.steps / 2) / config.steps;
446 int x = (int)(fraction * x_offset);
447 int y = (int)(fraction * y_offset);
451 scale_y_table[i] = y_table = new int[h];
452 scale_x_table[i] = x_table = new int[w];
454 layer_table[i].x = x;
455 layer_table[i].y = y;
456 for(int j = 0; j < h; j++)
459 CLAMP(y_table[j], 0, h - 1);
461 for(int j = 0; j < w; j++)
464 CLAMP(x_table[j], 0, w - 1);
467 need_reconfigure = 0;
470 if(get_use_opengl()) return run_opengl();
473 if(!engine) engine = new LinearBlurEngine(this,
474 get_project_smp() + 1,
475 get_project_smp() + 1);
476 if(!accum) accum = new unsigned char[frame->get_w() *
478 BC_CModels::components(frame->get_color_model()) *
479 MAX(sizeof(int), sizeof(float))];
482 this->output = frame;
486 temp = new VFrame(frame->get_w(), frame->get_h(),
487 frame->get_color_model(), 0);
488 temp->copy_from(frame);
495 BC_CModels::components(frame->get_color_model()) *
496 MAX(sizeof(int), sizeof(float)));
497 engine->process_packages();
502 void LinearBlurMain::update_gui()
506 load_configuration();
507 ((LinearBlurWindow*)thread->window)->lock_window();
508 ((LinearBlurWindow*)thread->window)->radius->update(config.radius);
509 ((LinearBlurWindow*)thread->window)->angle->update(config.angle);
510 ((LinearBlurWindow*)thread->window)->steps->update(config.steps);
511 ((LinearBlurWindow*)thread->window)->r->update(config.r);
512 ((LinearBlurWindow*)thread->window)->g->update(config.g);
513 ((LinearBlurWindow*)thread->window)->b->update(config.b);
514 ((LinearBlurWindow*)thread->window)->a->update(config.a);
515 ((LinearBlurWindow*)thread->window)->unlock_window();
521 void LinearBlurMain::save_data(KeyFrame *keyframe)
525 // cause data to be stored directly in text
526 output.set_shared_output(keyframe->xbuf);
527 output.tag.set_title("LINEARBLUR");
529 output.tag.set_property("RADIUS", config.radius);
530 output.tag.set_property("ANGLE", config.angle);
531 output.tag.set_property("STEPS", config.steps);
532 output.tag.set_property("R", config.r);
533 output.tag.set_property("G", config.g);
534 output.tag.set_property("B", config.b);
535 output.tag.set_property("A", config.a);
537 output.tag.set_title("/LINEARBLUR");
539 output.append_newline();
540 output.terminate_string();
543 void LinearBlurMain::read_data(KeyFrame *keyframe)
547 input.set_shared_input(keyframe->xbuf);
553 result = input.read_tag();
557 if(input.tag.title_is("LINEARBLUR"))
559 config.radius = input.tag.get_property("RADIUS", config.radius);
560 config.angle = input.tag.get_property("ANGLE", config.angle);
561 config.steps = input.tag.get_property("STEPS", config.steps);
562 config.r = input.tag.get_property("R", config.r);
563 config.g = input.tag.get_property("G", config.g);
564 config.b = input.tag.get_property("B", config.b);
565 config.a = input.tag.get_property("A", config.a);
572 static void draw_box(float x1, float y1, float x2, float y2)
575 glVertex3f(x1, y1, 0.0);
576 glVertex3f(x2, y1, 0.0);
577 glVertex3f(x2, y2, 0.0);
578 glVertex3f(x1, y2, 0.0);
583 int LinearBlurMain::handle_opengl()
586 get_output()->to_texture();
587 get_output()->enable_opengl();
588 get_output()->init_screen();
589 get_output()->bind_texture(0);
591 int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
592 glClearColor(0.0, 0.0, 0.0, 0.0);
593 glClear(GL_COLOR_BUFFER_BIT);
595 // Draw unselected channels
597 glBlendFunc(GL_ONE, GL_ONE);
598 glDrawBuffer(GL_BACK);
599 if(!config.r || !config.g || !config.b || !config.a)
601 glColor4f(config.r ? 0 : 1,
605 get_output()->draw_texture();
607 glAccum(GL_LOAD, 1.0);
609 // Blur selected channels
610 float fraction = 1.0 / config.steps;
611 for(int i = 0; i < config.steps; i++)
613 glClear(GL_COLOR_BUFFER_BIT);
614 glColor4f(config.r ? 1 : 0,
619 int w = get_output()->get_w();
620 int h = get_output()->get_h();
621 get_output()->draw_texture(0,
626 get_output()->get_h() - layer_table[i].y,
627 layer_table[i].x + w,
628 get_output()->get_h() - layer_table[i].y - h,
633 glDisable(GL_TEXTURE_2D);
636 glColor4f(config.r ? 0.0 : 0,
640 float center_x1 = 0.0;
641 float center_x2 = get_output()->get_w();
642 float project_x1 = layer_table[i].x;
643 float project_x2 = layer_table[i].x + get_output()->get_w();
644 float project_y1 = layer_table[i].y;
645 float project_y2 = layer_table[i].y + get_output()->get_h();
648 center_x1 = project_x1;
649 draw_box(0, 0, project_x1, -get_output()->get_h());
651 if(project_x2 < get_output()->get_w())
653 center_x2 = project_x2;
654 draw_box(project_x2, 0, get_output()->get_w(), -get_output()->get_h());
659 -get_output()->get_h(),
661 -get_output()->get_h() + project_y1);
663 if(project_y2 < get_output()->get_h())
666 -get_output()->get_h() + project_y2,
675 glAccum(GL_ACCUM, fraction);
676 glEnable(GL_TEXTURE_2D);
677 glColor4f(config.r ? 1 : 0,
684 glDisable(GL_TEXTURE_2D);
685 glReadBuffer(GL_BACK);
686 glAccum(GL_RETURN, 1.0);
688 glColor4f(1, 1, 1, 1);
689 get_output()->set_opengl_state(VFrame::SCREEN);
699 LinearBlurPackage::LinearBlurPackage()
707 LinearBlurUnit::LinearBlurUnit(LinearBlurEngine *server,
708 LinearBlurMain *plugin)
711 this->plugin = plugin;
712 this->server = server;
716 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
718 for(int j = pkg->y1; j < pkg->y2; j++) \
720 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
721 int in_y = y_table[j]; \
724 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
725 for(int k = 0; k < w; k++) \
727 int in_x = x_table[k]; \
729 int in_offset = in_x * COMPONENTS; \
730 *out_row++ += in_row[in_offset]; \
733 *out_row++ += in_row[in_offset + 1]; \
734 *out_row++ += in_row[in_offset + 2]; \
738 *out_row++ += in_row[in_offset + 1]; \
739 *out_row++ += in_row[in_offset + 2]; \
741 if(COMPONENTS == 4) \
742 *out_row++ += in_row[in_offset + 3]; \
746 /* Copy to output */ \
747 if(i == plugin->config.steps - 1) \
749 for(int j = pkg->y1; j < pkg->y2; j++) \
751 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
752 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
753 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
754 for(int k = 0; k < w; k++) \
758 *out_row++ = (*in_row++ * fraction) / 0x10000; \
763 *out_row++ = *in_backup++; \
771 *out_row++ = (*in_row++ * fraction) / 0x10000; \
776 *out_row++ = *in_backup++; \
782 *out_row++ = (*in_row++ * fraction) / 0x10000; \
787 *out_row++ = *in_backup++; \
795 *out_row++ = (*in_row++ * fraction) / 0x10000; \
800 *out_row++ = *in_backup++; \
806 *out_row++ = (*in_row++ * fraction) / 0x10000; \
811 *out_row++ = *in_backup++; \
816 if(COMPONENTS == 4) \
820 *out_row++ = (*in_row++ * fraction) / 0x10000; \
825 *out_row++ = *in_backup++; \
834 void LinearBlurUnit::process_package(LoadPackage *package)
836 LinearBlurPackage *pkg = (LinearBlurPackage*)package;
837 //int h = plugin->output->get_h();
838 int w = plugin->output->get_w();
840 int fraction = 0x10000 / plugin->config.steps;
841 int do_r = plugin->config.r;
842 int do_g = plugin->config.g;
843 int do_b = plugin->config.b;
844 int do_a = plugin->config.a;
845 for(int i = 0; i < plugin->config.steps; i++)
847 int *x_table = plugin->scale_x_table[i];
848 int *y_table = plugin->scale_y_table[i];
850 switch(plugin->input->get_color_model())
853 BLEND_LAYER(3, float, float, 1, 0)
856 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
859 BLEND_LAYER(4, float, float, 1, 0)
862 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
865 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
867 case BC_RGBA16161616:
868 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
871 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
874 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
877 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
879 case BC_YUVA16161616:
880 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
891 LinearBlurEngine::LinearBlurEngine(LinearBlurMain *plugin,
894 : LoadServer(total_clients, total_packages)
896 this->plugin = plugin;
899 void LinearBlurEngine::init_packages()
901 for(int i = 0; i < get_total_packages(); i++)
903 LinearBlurPackage *package = (LinearBlurPackage*)get_package(i);
904 package->y1 = plugin->output->get_h() * i / get_total_packages();
905 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
909 LoadClient* LinearBlurEngine::new_client()
911 return new LinearBlurUnit(this, plugin);
914 LoadPackage* LinearBlurEngine::new_package()
916 return new LinearBlurPackage;