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()
154 int x1 = 0; int clrBtn_w = 50;
155 int defaultBtn_w = 100;
157 add_subwindow(new BC_Title(x, y, _("Length:")));
159 add_subwindow(radius = new LinearBlurSize(plugin, x, y, &plugin->config.radius, 0, 100));
160 x1 = x + radius->get_w() + 10;
161 add_subwindow(radiusClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_RADIUS));
164 add_subwindow(new BC_Title(x, y, _("Angle:")));
166 add_subwindow(angle = new LinearBlurSize(plugin, x, y, &plugin->config.angle, -180, 180));
167 add_subwindow(angleClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_ANGLE));
170 add_subwindow(new BC_Title(x, y, _("Steps:")));
172 add_subwindow(steps = new LinearBlurSize(plugin, x, y, &plugin->config.steps, 1, 200));
173 add_subwindow(stepsClr = new LinearBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_STEPS));
176 add_subwindow(r = new LinearBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
178 add_subwindow(g = new LinearBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
180 add_subwindow(b = new LinearBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
182 add_subwindow(a = new LinearBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
184 add_subwindow(reset = new LinearBlurReset(plugin, this, x, y));
185 add_subwindow(default_settings = new LinearBlurDefaultSettings(plugin, this,
186 (280 - 10 - defaultBtn_w), y, defaultBtn_w));
194 void LinearBlurWindow::update_gui(int clear)
197 case RESET_RADIUS : radius->update(plugin->config.radius);
199 case RESET_ANGLE : angle->update(plugin->config.angle);
201 case RESET_STEPS : steps->update(plugin->config.steps);
204 case RESET_DEFAULT_SETTINGS :
206 radius->update(plugin->config.radius);
207 angle->update(plugin->config.angle);
208 steps->update(plugin->config.steps);
209 r->update(plugin->config.r);
210 g->update(plugin->config.g);
211 b->update(plugin->config.b);
212 a->update(plugin->config.a);
226 LinearBlurToggle::LinearBlurToggle(LinearBlurMain *plugin,
231 : BC_CheckBox(x, y, *output, string)
233 this->plugin = plugin;
234 this->output = output;
237 int LinearBlurToggle::handle_event()
239 *output = get_value();
240 plugin->send_configure_change();
250 LinearBlurSize::LinearBlurSize(LinearBlurMain *plugin,
256 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
258 this->plugin = plugin;
259 this->output = output;
261 int LinearBlurSize::handle_event()
263 *output = get_value();
264 plugin->send_configure_change();
269 LinearBlurReset::LinearBlurReset(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y)
270 : BC_GenericButton(x, y, _("Reset"))
272 this->plugin = plugin;
275 LinearBlurReset::~LinearBlurReset()
278 int LinearBlurReset::handle_event()
280 plugin->config.reset(RESET_ALL);
281 gui->update_gui(RESET_ALL);
282 plugin->send_configure_change();
287 LinearBlurDefaultSettings::LinearBlurDefaultSettings(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int w)
288 : BC_GenericButton(x, y, w, _("Default"))
290 this->plugin = plugin;
293 LinearBlurDefaultSettings::~LinearBlurDefaultSettings()
296 int LinearBlurDefaultSettings::handle_event()
298 plugin->config.reset(RESET_DEFAULT_SETTINGS);
299 gui->update_gui(RESET_DEFAULT_SETTINGS);
300 plugin->send_configure_change();
305 LinearBlurSliderClr::LinearBlurSliderClr(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int w, int clear)
306 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
308 this->plugin = plugin;
312 LinearBlurSliderClr::~LinearBlurSliderClr()
315 int LinearBlurSliderClr::handle_event()
317 // clear==1 ==> Radius slider
318 // clear==2 ==> Angle slider
319 // clear==3 ==> Steps slider
320 plugin->config.reset(clear);
321 gui->update_gui(clear);
322 plugin->send_configure_change();
333 LinearBlurMain::LinearBlurMain(PluginServer *server)
334 : PluginVClient(server)
342 need_reconfigure = 1;
347 LinearBlurMain::~LinearBlurMain()
350 if(engine) delete engine;
352 if(accum) delete [] accum;
353 if(temp) delete temp;
356 const char* LinearBlurMain::plugin_title() { return N_("Linear Blur"); }
357 int LinearBlurMain::is_realtime() { return 1; }
360 NEW_WINDOW_MACRO(LinearBlurMain, LinearBlurWindow)
361 LOAD_CONFIGURATION_MACRO(LinearBlurMain, LinearBlurConfig)
363 void LinearBlurMain::delete_tables()
367 for(int i = 0; i < table_entries; i++)
368 delete [] scale_x_table[i];
369 delete [] scale_x_table;
374 for(int i = 0; i < table_entries; i++)
375 delete [] scale_y_table[i];
376 delete [] scale_y_table;
378 delete [] layer_table;
385 int LinearBlurMain::process_buffer(VFrame *frame,
386 int64_t start_position,
389 need_reconfigure |= load_configuration();
393 get_source_position(),
397 // Generate tables here. The same table is used by many packages to render
398 // each horizontal stripe. Need to cover the entire output range in each
399 // table to avoid green borders
402 int w = frame->get_w();
403 int h = frame->get_h();
406 int angle = config.angle;
407 int radius = config.radius * MIN(w, h) / 100;
409 while(angle < 0) angle += 360;
430 y_offset = (int)(sin((float)config.angle / 360 * 2 * M_PI) * radius);
431 x_offset = (int)(cos((float)config.angle / 360 * 2 * M_PI) * radius);
437 scale_x_table = new int*[config.steps];
438 scale_y_table = new int*[config.steps];
439 table_entries = config.steps;
440 layer_table = new LinearBlurLayer[table_entries];
442 //printf("LinearBlurMain::process_realtime 1 %d %d %d\n", radius, x_offset, y_offset);
444 for(int i = 0; i < config.steps; i++)
446 float fraction = (float)(i - config.steps / 2) / config.steps;
447 int x = (int)(fraction * x_offset);
448 int y = (int)(fraction * y_offset);
452 scale_y_table[i] = y_table = new int[h];
453 scale_x_table[i] = x_table = new int[w];
455 layer_table[i].x = x;
456 layer_table[i].y = y;
457 for(int j = 0; j < h; j++)
460 CLAMP(y_table[j], 0, h - 1);
462 for(int j = 0; j < w; j++)
465 CLAMP(x_table[j], 0, w - 1);
468 need_reconfigure = 0;
471 if(get_use_opengl()) return run_opengl();
474 if(!engine) engine = new LinearBlurEngine(this,
475 get_project_smp() + 1,
476 get_project_smp() + 1);
477 if(!accum) accum = new unsigned char[frame->get_w() *
479 BC_CModels::components(frame->get_color_model()) *
480 MAX(sizeof(int), sizeof(float))];
483 this->output = frame;
487 temp = new VFrame(frame->get_w(), frame->get_h(),
488 frame->get_color_model(), 0);
489 temp->copy_from(frame);
496 BC_CModels::components(frame->get_color_model()) *
497 MAX(sizeof(int), sizeof(float)));
498 engine->process_packages();
503 void LinearBlurMain::update_gui()
507 load_configuration();
508 ((LinearBlurWindow*)thread->window)->lock_window();
509 ((LinearBlurWindow*)thread->window)->radius->update(config.radius);
510 ((LinearBlurWindow*)thread->window)->angle->update(config.angle);
511 ((LinearBlurWindow*)thread->window)->steps->update(config.steps);
512 ((LinearBlurWindow*)thread->window)->r->update(config.r);
513 ((LinearBlurWindow*)thread->window)->g->update(config.g);
514 ((LinearBlurWindow*)thread->window)->b->update(config.b);
515 ((LinearBlurWindow*)thread->window)->a->update(config.a);
516 ((LinearBlurWindow*)thread->window)->unlock_window();
522 void LinearBlurMain::save_data(KeyFrame *keyframe)
526 // cause data to be stored directly in text
527 output.set_shared_output(keyframe->xbuf);
528 output.tag.set_title("LINEARBLUR");
530 output.tag.set_property("RADIUS", config.radius);
531 output.tag.set_property("ANGLE", config.angle);
532 output.tag.set_property("STEPS", config.steps);
533 output.tag.set_property("R", config.r);
534 output.tag.set_property("G", config.g);
535 output.tag.set_property("B", config.b);
536 output.tag.set_property("A", config.a);
538 output.tag.set_title("/LINEARBLUR");
540 output.append_newline();
541 output.terminate_string();
544 void LinearBlurMain::read_data(KeyFrame *keyframe)
548 input.set_shared_input(keyframe->xbuf);
554 result = input.read_tag();
558 if(input.tag.title_is("LINEARBLUR"))
560 config.radius = input.tag.get_property("RADIUS", config.radius);
561 config.angle = input.tag.get_property("ANGLE", config.angle);
562 config.steps = input.tag.get_property("STEPS", config.steps);
563 config.r = input.tag.get_property("R", config.r);
564 config.g = input.tag.get_property("G", config.g);
565 config.b = input.tag.get_property("B", config.b);
566 config.a = input.tag.get_property("A", config.a);
573 static void draw_box(float x1, float y1, float x2, float y2)
576 glVertex3f(x1, y1, 0.0);
577 glVertex3f(x2, y1, 0.0);
578 glVertex3f(x2, y2, 0.0);
579 glVertex3f(x1, y2, 0.0);
584 int LinearBlurMain::handle_opengl()
587 get_output()->to_texture();
588 get_output()->enable_opengl();
589 get_output()->init_screen();
590 get_output()->bind_texture(0);
592 int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
593 glClearColor(0.0, 0.0, 0.0, 0.0);
594 glClear(GL_COLOR_BUFFER_BIT);
596 // Draw unselected channels
598 glBlendFunc(GL_ONE, GL_ONE);
599 glDrawBuffer(GL_BACK);
600 if(!config.r || !config.g || !config.b || !config.a)
602 glColor4f(config.r ? 0 : 1,
606 get_output()->draw_texture();
608 glAccum(GL_LOAD, 1.0);
610 // Blur selected channels
611 float fraction = 1.0 / config.steps;
612 for(int i = 0; i < config.steps; i++)
614 glClear(GL_COLOR_BUFFER_BIT);
615 glColor4f(config.r ? 1 : 0,
620 int w = get_output()->get_w();
621 int h = get_output()->get_h();
622 get_output()->draw_texture(0,
627 get_output()->get_h() - layer_table[i].y,
628 layer_table[i].x + w,
629 get_output()->get_h() - layer_table[i].y - h,
634 glDisable(GL_TEXTURE_2D);
637 glColor4f(config.r ? 0.0 : 0,
641 float center_x1 = 0.0;
642 float center_x2 = get_output()->get_w();
643 float project_x1 = layer_table[i].x;
644 float project_x2 = layer_table[i].x + get_output()->get_w();
645 float project_y1 = layer_table[i].y;
646 float project_y2 = layer_table[i].y + get_output()->get_h();
649 center_x1 = project_x1;
650 draw_box(0, 0, project_x1, -get_output()->get_h());
652 if(project_x2 < get_output()->get_w())
654 center_x2 = project_x2;
655 draw_box(project_x2, 0, get_output()->get_w(), -get_output()->get_h());
660 -get_output()->get_h(),
662 -get_output()->get_h() + project_y1);
664 if(project_y2 < get_output()->get_h())
667 -get_output()->get_h() + project_y2,
676 glAccum(GL_ACCUM, fraction);
677 glEnable(GL_TEXTURE_2D);
678 glColor4f(config.r ? 1 : 0,
685 glDisable(GL_TEXTURE_2D);
686 glReadBuffer(GL_BACK);
687 glAccum(GL_RETURN, 1.0);
689 glColor4f(1, 1, 1, 1);
690 get_output()->set_opengl_state(VFrame::SCREEN);
700 LinearBlurPackage::LinearBlurPackage()
708 LinearBlurUnit::LinearBlurUnit(LinearBlurEngine *server,
709 LinearBlurMain *plugin)
712 this->plugin = plugin;
713 this->server = server;
717 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
719 for(int j = pkg->y1; j < pkg->y2; j++) \
721 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
722 int in_y = y_table[j]; \
725 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
726 for(int k = 0; k < w; k++) \
728 int in_x = x_table[k]; \
730 int in_offset = in_x * COMPONENTS; \
731 *out_row++ += in_row[in_offset]; \
734 *out_row++ += in_row[in_offset + 1]; \
735 *out_row++ += in_row[in_offset + 2]; \
739 *out_row++ += in_row[in_offset + 1]; \
740 *out_row++ += in_row[in_offset + 2]; \
742 if(COMPONENTS == 4) \
743 *out_row++ += in_row[in_offset + 3]; \
747 /* Copy to output */ \
748 if(i == plugin->config.steps - 1) \
750 for(int j = pkg->y1; j < pkg->y2; j++) \
752 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
753 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
754 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
755 for(int k = 0; k < w; k++) \
759 *out_row++ = (*in_row++ * fraction) / 0x10000; \
764 *out_row++ = *in_backup++; \
772 *out_row++ = (*in_row++ * fraction) / 0x10000; \
777 *out_row++ = *in_backup++; \
783 *out_row++ = (*in_row++ * fraction) / 0x10000; \
788 *out_row++ = *in_backup++; \
796 *out_row++ = (*in_row++ * fraction) / 0x10000; \
801 *out_row++ = *in_backup++; \
807 *out_row++ = (*in_row++ * fraction) / 0x10000; \
812 *out_row++ = *in_backup++; \
817 if(COMPONENTS == 4) \
821 *out_row++ = (*in_row++ * fraction) / 0x10000; \
826 *out_row++ = *in_backup++; \
835 void LinearBlurUnit::process_package(LoadPackage *package)
837 LinearBlurPackage *pkg = (LinearBlurPackage*)package;
838 //int h = plugin->output->get_h();
839 int w = plugin->output->get_w();
841 int fraction = 0x10000 / plugin->config.steps;
842 int do_r = plugin->config.r;
843 int do_g = plugin->config.g;
844 int do_b = plugin->config.b;
845 int do_a = plugin->config.a;
846 for(int i = 0; i < plugin->config.steps; i++)
848 int *x_table = plugin->scale_x_table[i];
849 int *y_table = plugin->scale_y_table[i];
851 switch(plugin->input->get_color_model())
854 BLEND_LAYER(3, float, float, 1, 0)
857 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
860 BLEND_LAYER(4, float, float, 1, 0)
863 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
866 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
868 case BC_RGBA16161616:
869 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
872 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
875 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
878 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
880 case BC_YUVA16161616:
881 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
892 LinearBlurEngine::LinearBlurEngine(LinearBlurMain *plugin,
895 : LoadServer(total_clients, total_packages)
897 this->plugin = plugin;
900 void LinearBlurEngine::init_packages()
902 for(int i = 0; i < get_total_packages(); i++)
904 LinearBlurPackage *package = (LinearBlurPackage*)get_package(i);
905 package->y1 = plugin->output->get_h() * i / get_total_packages();
906 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
910 LoadClient* LinearBlurEngine::new_client()
912 return new LinearBlurUnit(this, plugin);
915 LoadPackage* LinearBlurEngine::new_package()
917 return new LinearBlurPackage;