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()
53 void LinearBlurConfig::reset()
64 int LinearBlurConfig::equivalent(LinearBlurConfig &that)
67 radius == that.radius &&
68 angle == that.angle &&
69 steps == that.steps &&
76 void LinearBlurConfig::copy_from(LinearBlurConfig &that)
87 void LinearBlurConfig::interpolate(LinearBlurConfig &prev,
88 LinearBlurConfig &next,
93 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
94 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
95 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
96 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
97 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
115 LinearBlurWindow::LinearBlurWindow(LinearBlurMain *plugin)
116 : PluginClientWindow(plugin,
123 this->plugin = plugin;
126 LinearBlurWindow::~LinearBlurWindow()
130 void LinearBlurWindow::create_objects()
134 add_subwindow(new BC_Title(x, y, _("Length:")));
136 add_subwindow(radius = new LinearBlurSize(plugin, x, y, &plugin->config.radius, 0, 100));
138 add_subwindow(new BC_Title(x, y, _("Angle:")));
140 add_subwindow(angle = new LinearBlurSize(plugin, x, y, &plugin->config.angle, -180, 180));
142 add_subwindow(new BC_Title(x, y, _("Steps:")));
144 add_subwindow(steps = new LinearBlurSize(plugin, x, y, &plugin->config.steps, 1, 200));
146 add_subwindow(r = new LinearBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
148 add_subwindow(g = new LinearBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
150 add_subwindow(b = new LinearBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
152 add_subwindow(a = new LinearBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
154 add_subwindow(reset = new LinearBlurReset(plugin, this, x, y));
162 void LinearBlurWindow::update()
164 radius->update(plugin->config.radius);
165 angle->update(plugin->config.angle);
166 steps->update(plugin->config.steps);
167 r->update(plugin->config.r);
168 g->update(plugin->config.g);
169 b->update(plugin->config.b);
170 a->update(plugin->config.a);
182 LinearBlurToggle::LinearBlurToggle(LinearBlurMain *plugin,
187 : BC_CheckBox(x, y, *output, string)
189 this->plugin = plugin;
190 this->output = output;
193 int LinearBlurToggle::handle_event()
195 *output = get_value();
196 plugin->send_configure_change();
206 LinearBlurSize::LinearBlurSize(LinearBlurMain *plugin,
212 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
214 this->plugin = plugin;
215 this->output = output;
217 int LinearBlurSize::handle_event()
219 *output = get_value();
220 plugin->send_configure_change();
225 LinearBlurReset::LinearBlurReset(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y)
226 : BC_GenericButton(x, y, _("Reset"))
228 this->plugin = plugin;
231 LinearBlurReset::~LinearBlurReset()
234 int LinearBlurReset::handle_event()
236 plugin->config.reset();
238 plugin->send_configure_change();
249 LinearBlurMain::LinearBlurMain(PluginServer *server)
250 : PluginVClient(server)
258 need_reconfigure = 1;
263 LinearBlurMain::~LinearBlurMain()
266 if(engine) delete engine;
268 if(accum) delete [] accum;
269 if(temp) delete temp;
272 const char* LinearBlurMain::plugin_title() { return N_("Linear Blur"); }
273 int LinearBlurMain::is_realtime() { return 1; }
276 NEW_WINDOW_MACRO(LinearBlurMain, LinearBlurWindow)
277 LOAD_CONFIGURATION_MACRO(LinearBlurMain, LinearBlurConfig)
279 void LinearBlurMain::delete_tables()
283 for(int i = 0; i < table_entries; i++)
284 delete [] scale_x_table[i];
285 delete [] scale_x_table;
290 for(int i = 0; i < table_entries; i++)
291 delete [] scale_y_table[i];
292 delete [] scale_y_table;
294 delete [] layer_table;
301 int LinearBlurMain::process_buffer(VFrame *frame,
302 int64_t start_position,
305 need_reconfigure |= load_configuration();
309 get_source_position(),
313 // Generate tables here. The same table is used by many packages to render
314 // each horizontal stripe. Need to cover the entire output range in each
315 // table to avoid green borders
318 int w = frame->get_w();
319 int h = frame->get_h();
322 int angle = config.angle;
323 int radius = config.radius * MIN(w, h) / 100;
325 while(angle < 0) angle += 360;
346 y_offset = (int)(sin((float)config.angle / 360 * 2 * M_PI) * radius);
347 x_offset = (int)(cos((float)config.angle / 360 * 2 * M_PI) * radius);
353 scale_x_table = new int*[config.steps];
354 scale_y_table = new int*[config.steps];
355 table_entries = config.steps;
356 layer_table = new LinearBlurLayer[table_entries];
358 //printf("LinearBlurMain::process_realtime 1 %d %d %d\n", radius, x_offset, y_offset);
360 for(int i = 0; i < config.steps; i++)
362 float fraction = (float)(i - config.steps / 2) / config.steps;
363 int x = (int)(fraction * x_offset);
364 int y = (int)(fraction * y_offset);
368 scale_y_table[i] = y_table = new int[h];
369 scale_x_table[i] = x_table = new int[w];
371 layer_table[i].x = x;
372 layer_table[i].y = y;
373 for(int j = 0; j < h; j++)
376 CLAMP(y_table[j], 0, h - 1);
378 for(int j = 0; j < w; j++)
381 CLAMP(x_table[j], 0, w - 1);
384 need_reconfigure = 0;
387 if(get_use_opengl()) return run_opengl();
390 if(!engine) engine = new LinearBlurEngine(this,
391 get_project_smp() + 1,
392 get_project_smp() + 1);
393 if(!accum) accum = new unsigned char[frame->get_w() *
395 BC_CModels::components(frame->get_color_model()) *
396 MAX(sizeof(int), sizeof(float))];
399 this->output = frame;
403 temp = new VFrame(frame->get_w(), frame->get_h(),
404 frame->get_color_model(), 0);
405 temp->copy_from(frame);
412 BC_CModels::components(frame->get_color_model()) *
413 MAX(sizeof(int), sizeof(float)));
414 engine->process_packages();
419 void LinearBlurMain::update_gui()
423 load_configuration();
424 ((LinearBlurWindow*)thread->window)->lock_window();
425 ((LinearBlurWindow*)thread->window)->radius->update(config.radius);
426 ((LinearBlurWindow*)thread->window)->angle->update(config.angle);
427 ((LinearBlurWindow*)thread->window)->steps->update(config.steps);
428 ((LinearBlurWindow*)thread->window)->r->update(config.r);
429 ((LinearBlurWindow*)thread->window)->g->update(config.g);
430 ((LinearBlurWindow*)thread->window)->b->update(config.b);
431 ((LinearBlurWindow*)thread->window)->a->update(config.a);
432 ((LinearBlurWindow*)thread->window)->unlock_window();
438 void LinearBlurMain::save_data(KeyFrame *keyframe)
442 // cause data to be stored directly in text
443 output.set_shared_output(keyframe->xbuf);
444 output.tag.set_title("LINEARBLUR");
446 output.tag.set_property("RADIUS", config.radius);
447 output.tag.set_property("ANGLE", config.angle);
448 output.tag.set_property("STEPS", config.steps);
449 output.tag.set_property("R", config.r);
450 output.tag.set_property("G", config.g);
451 output.tag.set_property("B", config.b);
452 output.tag.set_property("A", config.a);
454 output.tag.set_title("/LINEARBLUR");
456 output.append_newline();
457 output.terminate_string();
460 void LinearBlurMain::read_data(KeyFrame *keyframe)
464 input.set_shared_input(keyframe->xbuf);
470 result = input.read_tag();
474 if(input.tag.title_is("LINEARBLUR"))
476 config.radius = input.tag.get_property("RADIUS", config.radius);
477 config.angle = input.tag.get_property("ANGLE", config.angle);
478 config.steps = input.tag.get_property("STEPS", config.steps);
479 config.r = input.tag.get_property("R", config.r);
480 config.g = input.tag.get_property("G", config.g);
481 config.b = input.tag.get_property("B", config.b);
482 config.a = input.tag.get_property("A", config.a);
489 static void draw_box(float x1, float y1, float x2, float y2)
492 glVertex3f(x1, y1, 0.0);
493 glVertex3f(x2, y1, 0.0);
494 glVertex3f(x2, y2, 0.0);
495 glVertex3f(x1, y2, 0.0);
500 int LinearBlurMain::handle_opengl()
503 get_output()->to_texture();
504 get_output()->enable_opengl();
505 get_output()->init_screen();
506 get_output()->bind_texture(0);
508 int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
509 glClearColor(0.0, 0.0, 0.0, 0.0);
510 glClear(GL_COLOR_BUFFER_BIT);
512 // Draw unselected channels
514 glBlendFunc(GL_ONE, GL_ONE);
515 glDrawBuffer(GL_BACK);
516 if(!config.r || !config.g || !config.b || !config.a)
518 glColor4f(config.r ? 0 : 1,
522 get_output()->draw_texture();
524 glAccum(GL_LOAD, 1.0);
526 // Blur selected channels
527 float fraction = 1.0 / config.steps;
528 for(int i = 0; i < config.steps; i++)
530 glClear(GL_COLOR_BUFFER_BIT);
531 glColor4f(config.r ? 1 : 0,
536 int w = get_output()->get_w();
537 int h = get_output()->get_h();
538 get_output()->draw_texture(0,
543 get_output()->get_h() - layer_table[i].y,
544 layer_table[i].x + w,
545 get_output()->get_h() - layer_table[i].y - h,
550 glDisable(GL_TEXTURE_2D);
553 glColor4f(config.r ? 0.0 : 0,
557 float center_x1 = 0.0;
558 float center_x2 = get_output()->get_w();
559 float project_x1 = layer_table[i].x;
560 float project_x2 = layer_table[i].x + get_output()->get_w();
561 float project_y1 = layer_table[i].y;
562 float project_y2 = layer_table[i].y + get_output()->get_h();
565 center_x1 = project_x1;
566 draw_box(0, 0, project_x1, -get_output()->get_h());
568 if(project_x2 < get_output()->get_w())
570 center_x2 = project_x2;
571 draw_box(project_x2, 0, get_output()->get_w(), -get_output()->get_h());
576 -get_output()->get_h(),
578 -get_output()->get_h() + project_y1);
580 if(project_y2 < get_output()->get_h())
583 -get_output()->get_h() + project_y2,
592 glAccum(GL_ACCUM, fraction);
593 glEnable(GL_TEXTURE_2D);
594 glColor4f(config.r ? 1 : 0,
601 glDisable(GL_TEXTURE_2D);
602 glReadBuffer(GL_BACK);
603 glAccum(GL_RETURN, 1.0);
605 glColor4f(1, 1, 1, 1);
606 get_output()->set_opengl_state(VFrame::SCREEN);
616 LinearBlurPackage::LinearBlurPackage()
624 LinearBlurUnit::LinearBlurUnit(LinearBlurEngine *server,
625 LinearBlurMain *plugin)
628 this->plugin = plugin;
629 this->server = server;
633 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
635 for(int j = pkg->y1; j < pkg->y2; j++) \
637 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
638 int in_y = y_table[j]; \
641 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
642 for(int k = 0; k < w; k++) \
644 int in_x = x_table[k]; \
646 int in_offset = in_x * COMPONENTS; \
647 *out_row++ += in_row[in_offset]; \
650 *out_row++ += in_row[in_offset + 1]; \
651 *out_row++ += in_row[in_offset + 2]; \
655 *out_row++ += in_row[in_offset + 1]; \
656 *out_row++ += in_row[in_offset + 2]; \
658 if(COMPONENTS == 4) \
659 *out_row++ += in_row[in_offset + 3]; \
663 /* Copy to output */ \
664 if(i == plugin->config.steps - 1) \
666 for(int j = pkg->y1; j < pkg->y2; j++) \
668 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
669 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
670 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
671 for(int k = 0; k < w; k++) \
675 *out_row++ = (*in_row++ * fraction) / 0x10000; \
680 *out_row++ = *in_backup++; \
688 *out_row++ = (*in_row++ * fraction) / 0x10000; \
693 *out_row++ = *in_backup++; \
699 *out_row++ = (*in_row++ * fraction) / 0x10000; \
704 *out_row++ = *in_backup++; \
712 *out_row++ = (*in_row++ * fraction) / 0x10000; \
717 *out_row++ = *in_backup++; \
723 *out_row++ = (*in_row++ * fraction) / 0x10000; \
728 *out_row++ = *in_backup++; \
733 if(COMPONENTS == 4) \
737 *out_row++ = (*in_row++ * fraction) / 0x10000; \
742 *out_row++ = *in_backup++; \
751 void LinearBlurUnit::process_package(LoadPackage *package)
753 LinearBlurPackage *pkg = (LinearBlurPackage*)package;
754 //int h = plugin->output->get_h();
755 int w = plugin->output->get_w();
757 int fraction = 0x10000 / plugin->config.steps;
758 int do_r = plugin->config.r;
759 int do_g = plugin->config.g;
760 int do_b = plugin->config.b;
761 int do_a = plugin->config.a;
762 for(int i = 0; i < plugin->config.steps; i++)
764 int *x_table = plugin->scale_x_table[i];
765 int *y_table = plugin->scale_y_table[i];
767 switch(plugin->input->get_color_model())
770 BLEND_LAYER(3, float, float, 1, 0)
773 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
776 BLEND_LAYER(4, float, float, 1, 0)
779 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
782 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
784 case BC_RGBA16161616:
785 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
788 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
791 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
794 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
796 case BC_YUVA16161616:
797 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
808 LinearBlurEngine::LinearBlurEngine(LinearBlurMain *plugin,
811 : LoadServer(total_clients, total_packages)
813 this->plugin = plugin;
816 void LinearBlurEngine::init_packages()
818 for(int i = 0; i < get_total_packages(); i++)
820 LinearBlurPackage *package = (LinearBlurPackage*)get_package(i);
821 package->y1 = plugin->output->get_h() * i / get_total_packages();
822 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
826 LoadClient* LinearBlurEngine::new_client()
828 return new LinearBlurUnit(this, plugin);
831 LoadPackage* LinearBlurEngine::new_package()
833 return new LinearBlurPackage;