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"
44 REGISTER_PLUGIN(LinearBlurMain)
49 LinearBlurConfig::LinearBlurConfig()
51 reset(RESET_DEFAULT_SETTINGS);
54 void LinearBlurConfig::reset(int clear)
66 case RESET_RADIUS : radius = 0;
68 case RESET_ANGLE : angle = 0;
70 case RESET_STEPS : steps = 1;
72 case RESET_DEFAULT_SETTINGS :
85 int LinearBlurConfig::equivalent(LinearBlurConfig &that)
88 radius == that.radius &&
89 angle == that.angle &&
90 steps == that.steps &&
97 void LinearBlurConfig::copy_from(LinearBlurConfig &that)
108 void LinearBlurConfig::interpolate(LinearBlurConfig &prev,
109 LinearBlurConfig &next,
114 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
115 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
116 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
117 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
118 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
136 LinearBlurWindow::LinearBlurWindow(LinearBlurMain *plugin)
137 : PluginClientWindow(plugin,
144 this->plugin = plugin;
147 LinearBlurWindow::~LinearBlurWindow()
151 void LinearBlurWindow::create_objects()
153 int xs10 = xS(10), xs50 = xS(50), xs100 = xS(100);
154 int ys10 = yS(10), ys20 = yS(20), ys30 = yS(30), ys40 = yS(40);
155 int x = xs10, y = ys10;
156 int x1 = 0; int clrBtn_w = xs50;
157 int defaultBtn_w = xs100;
159 add_subwindow(new BC_Title(x, y, _("Length:")));
161 add_subwindow(radius = new LinearBlurSize(plugin, x, y, &plugin->config.radius, 0, 100));
162 x1 = x + radius->get_w() + xs10;
163 add_subwindow(radiusClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_RADIUS));
166 add_subwindow(new BC_Title(x, y, _("Angle:")));
168 add_subwindow(angle = new LinearBlurSize(plugin, x, y, &plugin->config.angle, -180, 180));
169 add_subwindow(angleClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_ANGLE));
172 add_subwindow(new BC_Title(x, y, _("Steps:")));
174 add_subwindow(steps = new LinearBlurSize(plugin, x, y, &plugin->config.steps, 1, 200));
175 add_subwindow(stepsClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_STEPS));
178 add_subwindow(r = new LinearBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
180 add_subwindow(g = new LinearBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
182 add_subwindow(b = new LinearBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
184 add_subwindow(a = new LinearBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
186 add_subwindow(reset = new LinearBlurReset(plugin, this, x, y));
187 add_subwindow(default_settings = new LinearBlurDefaultSettings(plugin, this,
188 (xS(280) - xs10 - defaultBtn_w), y, defaultBtn_w));
196 void LinearBlurWindow::update_gui(int clear)
199 case RESET_RADIUS : radius->update(plugin->config.radius);
201 case RESET_ANGLE : angle->update(plugin->config.angle);
203 case RESET_STEPS : steps->update(plugin->config.steps);
206 case RESET_DEFAULT_SETTINGS :
208 radius->update(plugin->config.radius);
209 angle->update(plugin->config.angle);
210 steps->update(plugin->config.steps);
211 r->update(plugin->config.r);
212 g->update(plugin->config.g);
213 b->update(plugin->config.b);
214 a->update(plugin->config.a);
228 LinearBlurToggle::LinearBlurToggle(LinearBlurMain *plugin,
233 : BC_CheckBox(x, y, *output, string)
235 this->plugin = plugin;
236 this->output = output;
239 int LinearBlurToggle::handle_event()
241 *output = get_value();
242 plugin->send_configure_change();
252 LinearBlurSize::LinearBlurSize(LinearBlurMain *plugin,
258 : BC_ISlider(x, y, 0, xS(200), yS(200), min, max, *output)
260 this->plugin = plugin;
261 this->output = output;
263 int LinearBlurSize::handle_event()
265 *output = get_value();
266 plugin->send_configure_change();
271 LinearBlurReset::LinearBlurReset(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y)
272 : BC_GenericButton(x, y, _("Reset"))
274 this->plugin = plugin;
277 LinearBlurReset::~LinearBlurReset()
280 int LinearBlurReset::handle_event()
282 plugin->config.reset(RESET_ALL);
283 gui->update_gui(RESET_ALL);
284 plugin->send_configure_change();
289 LinearBlurDefaultSettings::LinearBlurDefaultSettings(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int w)
290 : BC_GenericButton(x, y, w, _("Default"))
292 this->plugin = plugin;
295 LinearBlurDefaultSettings::~LinearBlurDefaultSettings()
298 int LinearBlurDefaultSettings::handle_event()
300 plugin->config.reset(RESET_DEFAULT_SETTINGS);
301 gui->update_gui(RESET_DEFAULT_SETTINGS);
302 plugin->send_configure_change();
307 LinearBlurSliderClr::LinearBlurSliderClr(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int w, int clear)
308 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
310 this->plugin = plugin;
314 LinearBlurSliderClr::~LinearBlurSliderClr()
317 int LinearBlurSliderClr::handle_event()
319 // clear==1 ==> Radius slider
320 // clear==2 ==> Angle slider
321 // clear==3 ==> Steps slider
322 plugin->config.reset(clear);
323 gui->update_gui(clear);
324 plugin->send_configure_change();
335 LinearBlurMain::LinearBlurMain(PluginServer *server)
336 : PluginVClient(server)
344 need_reconfigure = 1;
349 LinearBlurMain::~LinearBlurMain()
352 if(engine) delete engine;
354 if(accum) delete [] accum;
355 if(temp) delete temp;
358 const char* LinearBlurMain::plugin_title() { return N_("Linear Blur"); }
359 int LinearBlurMain::is_realtime() { return 1; }
362 NEW_WINDOW_MACRO(LinearBlurMain, LinearBlurWindow)
363 LOAD_CONFIGURATION_MACRO(LinearBlurMain, LinearBlurConfig)
365 void LinearBlurMain::delete_tables()
369 for(int i = 0; i < table_entries; i++)
370 delete [] scale_x_table[i];
371 delete [] scale_x_table;
376 for(int i = 0; i < table_entries; i++)
377 delete [] scale_y_table[i];
378 delete [] scale_y_table;
380 delete [] layer_table;
387 int LinearBlurMain::process_buffer(VFrame *frame,
388 int64_t start_position,
391 need_reconfigure |= load_configuration();
395 get_source_position(),
399 // Generate tables here. The same table is used by many packages to render
400 // each horizontal stripe. Need to cover the entire output range in each
401 // table to avoid green borders
404 int w = frame->get_w();
405 int h = frame->get_h();
408 int angle = config.angle;
409 int radius = config.radius * MIN(w, h) / 100;
411 while(angle < 0) angle += 360;
432 y_offset = (int)(sin((float)config.angle / 360 * 2 * M_PI) * radius);
433 x_offset = (int)(cos((float)config.angle / 360 * 2 * M_PI) * radius);
439 scale_x_table = new int*[config.steps];
440 scale_y_table = new int*[config.steps];
441 table_entries = config.steps;
442 layer_table = new LinearBlurLayer[table_entries];
444 //printf("LinearBlurMain::process_realtime 1 %d %d %d\n", radius, x_offset, y_offset);
446 for(int i = 0; i < config.steps; i++)
448 float fraction = (float)(i - config.steps / 2) / config.steps;
449 int x = (int)(fraction * x_offset);
450 int y = (int)(fraction * y_offset);
454 scale_y_table[i] = y_table = new int[h];
455 scale_x_table[i] = x_table = new int[w];
457 layer_table[i].x = x;
458 layer_table[i].y = y;
459 for(int j = 0; j < h; j++)
462 CLAMP(y_table[j], 0, h - 1);
464 for(int j = 0; j < w; j++)
467 CLAMP(x_table[j], 0, w - 1);
470 need_reconfigure = 0;
473 if(get_use_opengl()) return run_opengl();
476 if(!engine) engine = new LinearBlurEngine(this,
477 get_project_smp() + 1,
478 get_project_smp() + 1);
479 if(!accum) accum = new unsigned char[frame->get_w() *
481 BC_CModels::components(frame->get_color_model()) *
482 MAX(sizeof(int), sizeof(float))];
485 this->output = frame;
489 temp = new VFrame(frame->get_w(), frame->get_h(),
490 frame->get_color_model(), 0);
491 temp->copy_from(frame);
498 BC_CModels::components(frame->get_color_model()) *
499 MAX(sizeof(int), sizeof(float)));
500 engine->process_packages();
505 void LinearBlurMain::update_gui()
509 load_configuration();
510 ((LinearBlurWindow*)thread->window)->lock_window();
511 ((LinearBlurWindow*)thread->window)->radius->update(config.radius);
512 ((LinearBlurWindow*)thread->window)->angle->update(config.angle);
513 ((LinearBlurWindow*)thread->window)->steps->update(config.steps);
514 ((LinearBlurWindow*)thread->window)->r->update(config.r);
515 ((LinearBlurWindow*)thread->window)->g->update(config.g);
516 ((LinearBlurWindow*)thread->window)->b->update(config.b);
517 ((LinearBlurWindow*)thread->window)->a->update(config.a);
518 ((LinearBlurWindow*)thread->window)->unlock_window();
524 void LinearBlurMain::save_data(KeyFrame *keyframe)
528 // cause data to be stored directly in text
529 output.set_shared_output(keyframe->xbuf);
530 output.tag.set_title("LINEARBLUR");
532 output.tag.set_property("RADIUS", config.radius);
533 output.tag.set_property("ANGLE", config.angle);
534 output.tag.set_property("STEPS", config.steps);
535 output.tag.set_property("R", config.r);
536 output.tag.set_property("G", config.g);
537 output.tag.set_property("B", config.b);
538 output.tag.set_property("A", config.a);
540 output.tag.set_title("/LINEARBLUR");
542 output.append_newline();
543 output.terminate_string();
546 void LinearBlurMain::read_data(KeyFrame *keyframe)
550 input.set_shared_input(keyframe->xbuf);
556 result = input.read_tag();
560 if(input.tag.title_is("LINEARBLUR"))
562 config.radius = input.tag.get_property("RADIUS", config.radius);
563 config.angle = input.tag.get_property("ANGLE", config.angle);
564 config.steps = input.tag.get_property("STEPS", config.steps);
565 config.r = input.tag.get_property("R", config.r);
566 config.g = input.tag.get_property("G", config.g);
567 config.b = input.tag.get_property("B", config.b);
568 config.a = input.tag.get_property("A", config.a);
575 static void draw_box(float x1, float y1, float x2, float y2)
578 glVertex3f(x1, y1, 0.0);
579 glVertex3f(x2, y1, 0.0);
580 glVertex3f(x2, y2, 0.0);
581 glVertex3f(x1, y2, 0.0);
586 int LinearBlurMain::handle_opengl()
589 get_output()->to_texture();
590 get_output()->enable_opengl();
591 get_output()->init_screen();
592 get_output()->bind_texture(0);
594 int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
595 glClearColor(0.0, 0.0, 0.0, 0.0);
596 glClear(GL_COLOR_BUFFER_BIT);
598 // Draw unselected channels
600 glBlendFunc(GL_ONE, GL_ONE);
601 glDrawBuffer(GL_BACK);
602 if(!config.r || !config.g || !config.b || !config.a)
604 glColor4f(config.r ? 0 : 1,
608 get_output()->draw_texture();
610 glAccum(GL_LOAD, 1.0);
612 // Blur selected channels
613 float fraction = 1.0 / config.steps;
614 for(int i = 0; i < config.steps; i++)
616 glClear(GL_COLOR_BUFFER_BIT);
617 glColor4f(config.r ? 1 : 0,
622 int w = get_output()->get_w();
623 int h = get_output()->get_h();
624 get_output()->draw_texture(0,
629 get_output()->get_h() - layer_table[i].y,
630 layer_table[i].x + w,
631 get_output()->get_h() - layer_table[i].y - h,
636 glDisable(GL_TEXTURE_2D);
639 glColor4f(config.r ? 0.0 : 0,
643 float center_x1 = 0.0;
644 float center_x2 = get_output()->get_w();
645 float project_x1 = layer_table[i].x;
646 float project_x2 = layer_table[i].x + get_output()->get_w();
647 float project_y1 = layer_table[i].y;
648 float project_y2 = layer_table[i].y + get_output()->get_h();
651 center_x1 = project_x1;
652 draw_box(0, 0, project_x1, -get_output()->get_h());
654 if(project_x2 < get_output()->get_w())
656 center_x2 = project_x2;
657 draw_box(project_x2, 0, get_output()->get_w(), -get_output()->get_h());
662 -get_output()->get_h(),
664 -get_output()->get_h() + project_y1);
666 if(project_y2 < get_output()->get_h())
669 -get_output()->get_h() + project_y2,
678 glAccum(GL_ACCUM, fraction);
679 glEnable(GL_TEXTURE_2D);
680 glColor4f(config.r ? 1 : 0,
687 glDisable(GL_TEXTURE_2D);
688 glReadBuffer(GL_BACK);
689 glAccum(GL_RETURN, 1.0);
691 glColor4f(1, 1, 1, 1);
692 get_output()->set_opengl_state(VFrame::SCREEN);
702 LinearBlurPackage::LinearBlurPackage()
710 LinearBlurUnit::LinearBlurUnit(LinearBlurEngine *server,
711 LinearBlurMain *plugin)
714 this->plugin = plugin;
715 this->server = server;
719 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
721 for(int j = pkg->y1; j < pkg->y2; j++) \
723 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
724 int in_y = y_table[j]; \
727 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
728 for(int k = 0; k < w; k++) \
730 int in_x = x_table[k]; \
732 int in_offset = in_x * COMPONENTS; \
733 *out_row++ += in_row[in_offset]; \
736 *out_row++ += in_row[in_offset + 1]; \
737 *out_row++ += in_row[in_offset + 2]; \
741 *out_row++ += in_row[in_offset + 1]; \
742 *out_row++ += in_row[in_offset + 2]; \
744 if(COMPONENTS == 4) \
745 *out_row++ += in_row[in_offset + 3]; \
749 /* Copy to output */ \
750 if(i == plugin->config.steps - 1) \
752 for(int j = pkg->y1; j < pkg->y2; j++) \
754 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
755 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
756 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
757 for(int k = 0; k < w; k++) \
761 *out_row++ = (*in_row++ * fraction) / 0x10000; \
766 *out_row++ = *in_backup++; \
774 *out_row++ = (*in_row++ * fraction) / 0x10000; \
779 *out_row++ = *in_backup++; \
785 *out_row++ = (*in_row++ * fraction) / 0x10000; \
790 *out_row++ = *in_backup++; \
798 *out_row++ = (*in_row++ * fraction) / 0x10000; \
803 *out_row++ = *in_backup++; \
809 *out_row++ = (*in_row++ * fraction) / 0x10000; \
814 *out_row++ = *in_backup++; \
819 if(COMPONENTS == 4) \
823 *out_row++ = (*in_row++ * fraction) / 0x10000; \
828 *out_row++ = *in_backup++; \
837 void LinearBlurUnit::process_package(LoadPackage *package)
839 LinearBlurPackage *pkg = (LinearBlurPackage*)package;
840 //int h = plugin->output->get_h();
841 int w = plugin->output->get_w();
843 int fraction = 0x10000 / plugin->config.steps;
844 int do_r = plugin->config.r;
845 int do_g = plugin->config.g;
846 int do_b = plugin->config.b;
847 int do_a = plugin->config.a;
848 for(int i = 0; i < plugin->config.steps; i++)
850 int *x_table = plugin->scale_x_table[i];
851 int *y_table = plugin->scale_y_table[i];
853 switch(plugin->input->get_color_model())
856 BLEND_LAYER(3, float, float, 1, 0)
859 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
862 BLEND_LAYER(4, float, float, 1, 0)
865 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
868 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
870 case BC_RGBA16161616:
871 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
874 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
877 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
880 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
882 case BC_YUVA16161616:
883 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
894 LinearBlurEngine::LinearBlurEngine(LinearBlurMain *plugin,
897 : LoadServer(total_clients, total_packages)
899 this->plugin = plugin;
902 void LinearBlurEngine::init_packages()
904 for(int i = 0; i < get_total_packages(); i++)
906 LinearBlurPackage *package = (LinearBlurPackage*)get_package(i);
907 package->y1 = plugin->output->get_h() * i / get_total_packages();
908 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
912 LoadClient* LinearBlurEngine::new_client()
914 return new LinearBlurUnit(this, plugin);
917 LoadPackage* LinearBlurEngine::new_package()
919 return new LinearBlurPackage;