5 #include "bcdisplayinfo.h"
12 #include "loadbalance.h"
13 #include "picon_png.h"
14 #include "pluginvclient.h"
31 int equivalent(ZoomBlurConfig &that);
32 void copy_from(ZoomBlurConfig &that);
33 void interpolate(ZoomBlurConfig &prev,
51 class ZoomBlurSize : public BC_ISlider
54 ZoomBlurSize(ZoomBlurMain *plugin,
65 class ZoomBlurToggle : public BC_CheckBox
68 ZoomBlurToggle(ZoomBlurMain *plugin,
78 class ZoomBlurWindow : public BC_Window
81 ZoomBlurWindow(ZoomBlurMain *plugin, int x, int y);
87 ZoomBlurSize *x, *y, *radius, *steps;
88 ZoomBlurToggle *r, *g, *b, *a;
94 PLUGIN_THREAD_HEADER(ZoomBlurMain, ZoomBlurThread, ZoomBlurWindow)
97 // Output coords for a layer of blurring
98 // Used for OpenGL only
103 float x1, y1, x2, y2;
106 class ZoomBlurMain : public PluginVClient
109 ZoomBlurMain(PluginServer *server);
112 int process_buffer(VFrame *frame,
113 int64_t start_position,
118 void save_data(KeyFrame *keyframe);
119 void read_data(KeyFrame *keyframe);
123 PLUGIN_CLASS_MEMBERS(ZoomBlurConfig, ZoomBlurThread)
125 void delete_tables();
126 VFrame *input, *output, *temp;
127 ZoomBlurEngine *engine;
130 ZoomBlurLayer *layer_table;
132 int need_reconfigure;
133 // The accumulation buffer is needed because 8 bits isn't precise enough
134 unsigned char *accum;
137 class ZoomBlurPackage : public LoadPackage
144 class ZoomBlurUnit : public LoadClient
147 ZoomBlurUnit(ZoomBlurEngine *server, ZoomBlurMain *plugin);
148 void process_package(LoadPackage *package);
149 ZoomBlurEngine *server;
150 ZoomBlurMain *plugin;
153 class ZoomBlurEngine : public LoadServer
156 ZoomBlurEngine(ZoomBlurMain *plugin,
159 void init_packages();
160 LoadClient* new_client();
161 LoadPackage* new_package();
162 ZoomBlurMain *plugin;
183 REGISTER_PLUGIN(ZoomBlurMain)
187 ZoomBlurConfig::ZoomBlurConfig()
199 int ZoomBlurConfig::equivalent(ZoomBlurConfig &that)
204 radius == that.radius &&
205 steps == that.steps &&
212 void ZoomBlurConfig::copy_from(ZoomBlurConfig &that)
216 radius = that.radius;
224 void ZoomBlurConfig::interpolate(ZoomBlurConfig &prev,
225 ZoomBlurConfig &next,
230 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
231 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
232 this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
233 this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
234 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
235 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
250 PLUGIN_THREAD_OBJECT(ZoomBlurMain, ZoomBlurThread, ZoomBlurWindow)
254 ZoomBlurWindow::ZoomBlurWindow(ZoomBlurMain *plugin, int x, int y)
255 : BC_Window(plugin->gui_string,
265 this->plugin = plugin;
268 ZoomBlurWindow::~ZoomBlurWindow()
272 int ZoomBlurWindow::create_objects()
276 add_subwindow(new BC_Title(x, y, _("X:")));
278 add_subwindow(this->x = new ZoomBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
280 add_subwindow(new BC_Title(x, y, _("Y:")));
282 add_subwindow(this->y = new ZoomBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
284 add_subwindow(new BC_Title(x, y, _("Radius:")));
286 add_subwindow(radius = new ZoomBlurSize(plugin, x, y, &plugin->config.radius, -100, 100));
288 add_subwindow(new BC_Title(x, y, _("Steps:")));
290 add_subwindow(steps = new ZoomBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
292 add_subwindow(r = new ZoomBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
294 add_subwindow(g = new ZoomBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
296 add_subwindow(b = new ZoomBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
298 add_subwindow(a = new ZoomBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
306 int ZoomBlurWindow::close_event()
308 // Set result to 1 to indicate a plugin side close
322 ZoomBlurToggle::ZoomBlurToggle(ZoomBlurMain *plugin,
327 : BC_CheckBox(x, y, *output, string)
329 this->plugin = plugin;
330 this->output = output;
333 int ZoomBlurToggle::handle_event()
335 *output = get_value();
336 plugin->send_configure_change();
346 ZoomBlurSize::ZoomBlurSize(ZoomBlurMain *plugin,
352 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
354 this->plugin = plugin;
355 this->output = output;
357 int ZoomBlurSize::handle_event()
359 *output = get_value();
360 plugin->send_configure_change();
373 ZoomBlurMain::ZoomBlurMain(PluginServer *server)
374 : PluginVClient(server)
376 PLUGIN_CONSTRUCTOR_MACRO
383 need_reconfigure = 1;
387 ZoomBlurMain::~ZoomBlurMain()
389 PLUGIN_DESTRUCTOR_MACRO
390 if(engine) delete engine;
392 if(accum) delete [] accum;
393 if(temp) delete temp;
396 char* ZoomBlurMain::plugin_title() { return N_("Zoom Blur"); }
397 int ZoomBlurMain::is_realtime() { return 1; }
400 NEW_PICON_MACRO(ZoomBlurMain)
402 SHOW_GUI_MACRO(ZoomBlurMain, ZoomBlurThread)
404 SET_STRING_MACRO(ZoomBlurMain)
406 RAISE_WINDOW_MACRO(ZoomBlurMain)
408 LOAD_CONFIGURATION_MACRO(ZoomBlurMain, ZoomBlurConfig)
410 void ZoomBlurMain::delete_tables()
414 for(int i = 0; i < table_entries; i++)
415 delete [] scale_x_table[i];
416 delete [] scale_x_table;
421 for(int i = 0; i < table_entries; i++)
422 delete [] scale_y_table[i];
423 delete [] scale_y_table;
426 delete [] layer_table;
433 int ZoomBlurMain::process_buffer(VFrame *frame,
434 int64_t start_position,
437 need_reconfigure |= load_configuration();
443 get_source_position(),
449 // Generate tables here. The same table is used by many packages to render
450 // each horizontal stripe. Need to cover the entire output range in each
451 // table to avoid green borders
455 float w = frame->get_w();
456 float h = frame->get_h();
457 float center_x = (float)config.x / 100 * w;
458 float center_y = (float)config.y / 100 * h;
459 float radius = (float)(100 + config.radius) / 100;
462 int steps = config.steps ? config.steps : 1;
474 // printf("ZoomBlurMain::process_realtime 1 %d %d\n",
478 center_x = (center_x - w / 2) * (1.0 - radius) + w / 2;
479 center_y = (center_y - h / 2) * (1.0 - radius) + h / 2;
484 min_x1 = center_x - min_w / 2;
485 min_y1 = center_y - min_h / 2;
486 min_x2 = center_x + min_w / 2;
487 min_y2 = center_y + min_h / 2;
494 // printf("ZoomBlurMain::process_realtime 2 w=%f radius=%f center_x=%f\n",
500 // Dimensions of outermost rectangle
503 table_entries = steps;
504 scale_x_table = new int*[steps];
505 scale_y_table = new int*[steps];
506 layer_table = new ZoomBlurLayer[table_entries];
509 for(int i = 0; i < steps; i++)
511 float fraction = (float)i / steps;
512 float inv_fraction = 1.0 - fraction;
513 float out_x1 = min_x1 * fraction + max_x1 * inv_fraction;
514 float out_x2 = min_x2 * fraction + max_x2 * inv_fraction;
515 float out_y1 = min_y1 * fraction + max_y1 * inv_fraction;
516 float out_y2 = min_y2 * fraction + max_y2 * inv_fraction;
517 float out_w = out_x2 - out_x1;
518 float out_h = out_y2 - out_y1;
519 if(out_w < 0) out_w = 0;
520 if(out_h < 0) out_h = 0;
521 float scale_x = (float)w / out_w;
522 float scale_y = (float)h / out_h;
526 scale_y_table[i] = y_table = new int[(int)(h + 1)];
527 scale_x_table[i] = x_table = new int[(int)(w + 1)];
529 layer_table[i].x1 = out_x1;
530 layer_table[i].y1 = out_y1;
531 layer_table[i].x2 = out_x2;
532 layer_table[i].y2 = out_y2;
535 for(int j = 0; j < h; j++)
537 y_table[j] = (int)((j - out_y1) * scale_y);
539 for(int j = 0; j < w; j++)
541 x_table[j] = (int)((j - out_x1) * scale_x);
542 //printf("ZoomBlurMain::process_realtime %d %d\n", j, x_table[j]);
546 need_reconfigure = 0;
550 if(get_use_opengl()) return run_opengl();
556 if(!engine) engine = new ZoomBlurEngine(this,
557 get_project_smp() + 1,
558 get_project_smp() + 1);
559 if(!accum) accum = new unsigned char[frame->get_w() *
561 cmodel_components(frame->get_color_model()) *
562 MAX(sizeof(int), sizeof(float))];
565 this->output = frame;
568 if(!temp) temp = new VFrame(0,
571 frame->get_color_model());
572 temp->copy_from(frame);
578 cmodel_components(frame->get_color_model()) *
579 MAX(sizeof(int), sizeof(float)));
580 engine->process_packages();
585 void ZoomBlurMain::update_gui()
589 load_configuration();
590 thread->window->lock_window();
591 thread->window->x->update(config.x);
592 thread->window->y->update(config.y);
593 thread->window->radius->update(config.radius);
594 thread->window->steps->update(config.steps);
595 thread->window->r->update(config.r);
596 thread->window->g->update(config.g);
597 thread->window->b->update(config.b);
598 thread->window->a->update(config.a);
599 thread->window->unlock_window();
604 int ZoomBlurMain::load_defaults()
606 char directory[1024], string[1024];
607 // set the default directory
608 sprintf(directory, "%szoomblur.rc", BCASTDIR);
611 defaults = new BC_Hash(directory);
614 config.x = defaults->get("X", config.x);
615 config.y = defaults->get("Y", config.y);
616 config.radius = defaults->get("RADIUS", config.radius);
617 config.steps = defaults->get("STEPS", config.steps);
618 config.r = defaults->get("R", config.r);
619 config.g = defaults->get("G", config.g);
620 config.b = defaults->get("B", config.b);
621 config.a = defaults->get("A", config.a);
626 int ZoomBlurMain::save_defaults()
628 defaults->update("X", config.x);
629 defaults->update("Y", config.y);
630 defaults->update("RADIUS", config.radius);
631 defaults->update("STEPS", config.steps);
632 defaults->update("R", config.r);
633 defaults->update("G", config.g);
634 defaults->update("B", config.b);
635 defaults->update("A", config.a);
642 void ZoomBlurMain::save_data(KeyFrame *keyframe)
646 // cause data to be stored directly in text
647 output.set_shared_string(keyframe->data, MESSAGESIZE);
648 output.tag.set_title("ZOOMBLUR");
650 output.tag.set_property("X", config.x);
651 output.tag.set_property("Y", config.y);
652 output.tag.set_property("RADIUS", config.radius);
653 output.tag.set_property("STEPS", config.steps);
654 output.tag.set_property("R", config.r);
655 output.tag.set_property("G", config.g);
656 output.tag.set_property("B", config.b);
657 output.tag.set_property("A", config.a);
659 output.terminate_string();
662 void ZoomBlurMain::read_data(KeyFrame *keyframe)
666 input.set_shared_string(keyframe->data, strlen(keyframe->data));
672 result = input.read_tag();
676 if(input.tag.title_is("ZOOMBLUR"))
678 config.x = input.tag.get_property("X", config.x);
679 config.y = input.tag.get_property("Y", config.y);
680 config.radius = input.tag.get_property("RADIUS", config.radius);
681 config.steps = input.tag.get_property("STEPS", config.steps);
682 config.r = input.tag.get_property("R", config.r);
683 config.g = input.tag.get_property("G", config.g);
684 config.b = input.tag.get_property("B", config.b);
685 config.a = input.tag.get_property("A", config.a);
692 static void draw_box(float x1, float y1, float x2, float y2)
695 glVertex3f(x1, y1, 0.0);
696 glVertex3f(x2, y1, 0.0);
697 glVertex3f(x2, y2, 0.0);
698 glVertex3f(x1, y2, 0.0);
703 int ZoomBlurMain::handle_opengl()
706 get_output()->to_texture();
707 get_output()->enable_opengl();
708 get_output()->init_screen();
709 get_output()->bind_texture(0);
711 int is_yuv = cmodel_is_yuv(get_output()->get_color_model());
712 glClearColor(0.0, 0.0, 0.0, 0.0);
713 glClear(GL_COLOR_BUFFER_BIT);
715 // Draw unselected channels
717 glBlendFunc(GL_ONE, GL_ONE);
718 glDrawBuffer(GL_BACK);
720 if(!config.r || !config.g || !config.b || !config.a)
722 glColor4f(config.r ? 0 : 1,
726 get_output()->draw_texture();
728 glAccum(GL_LOAD, 1.0);
730 // Blur selected channels
731 float fraction = 1.0 / config.steps;
732 for(int i = 0; i < config.steps; i++)
734 glClear(GL_COLOR_BUFFER_BIT);
735 glColor4f(config.r ? 1 : 0,
740 get_output()->draw_texture(0,
742 get_output()->get_w(),
743 get_output()->get_h(),
745 get_output()->get_h() - layer_table[i].y1,
747 get_output()->get_h() - layer_table[i].y2,
751 glDisable(GL_TEXTURE_2D);
752 if(cmodel_is_yuv(get_output()->get_color_model()))
754 glColor4f(config.r ? 0.0 : 0,
758 float center_x1 = 0.0;
759 float center_x2 = get_output()->get_w();
760 if(layer_table[i].x1 > 0)
762 center_x1 = layer_table[i].x1;
763 draw_box(0, 0, layer_table[i].x1, -get_output()->get_h());
765 if(layer_table[i].x2 < get_output()->get_w())
767 center_x2 = layer_table[i].x2;
768 draw_box(layer_table[i].x2, 0, get_output()->get_w(), -get_output()->get_h());
770 if(layer_table[i].y1 > 0)
773 -get_output()->get_h(),
775 -get_output()->get_h() + layer_table[i].y1);
777 if(layer_table[i].y2 < get_output()->get_h())
780 -get_output()->get_h() + layer_table[i].y2,
787 glAccum(GL_ACCUM, fraction);
788 glEnable(GL_TEXTURE_2D);
789 glColor4f(config.r ? 1 : 0,
796 glReadBuffer(GL_BACK);
797 glDisable(GL_TEXTURE_2D);
798 glAccum(GL_RETURN, 1.0);
800 glColor4f(1, 1, 1, 1);
801 get_output()->set_opengl_state(VFrame::SCREEN);
818 ZoomBlurPackage::ZoomBlurPackage()
826 ZoomBlurUnit::ZoomBlurUnit(ZoomBlurEngine *server,
827 ZoomBlurMain *plugin)
830 this->plugin = plugin;
831 this->server = server;
835 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
837 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
838 for(int j = pkg->y1; j < pkg->y2; j++) \
840 TEMP_TYPE *out_row = (TEMP_TYPE*)plugin->accum + COMPONENTS * w * j; \
841 int in_y = y_table[j]; \
844 if(in_y >= 0 && in_y < h) \
846 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
847 for(int k = 0; k < w; k++) \
849 int in_x = x_table[k]; \
851 if(in_x >= 0 && in_x < w) \
853 int in_offset = in_x * COMPONENTS; \
854 *out_row++ += in_row[in_offset]; \
857 *out_row++ += in_row[in_offset + 1]; \
858 *out_row++ += in_row[in_offset + 2]; \
862 *out_row++ += (TEMP_TYPE)in_row[in_offset + 1]; \
863 *out_row++ += (TEMP_TYPE)in_row[in_offset + 2]; \
865 if(COMPONENTS == 4) \
866 *out_row++ += in_row[in_offset + 3]; \
868 /* Blend nothing */ \
874 *out_row++ += chroma_offset; \
875 *out_row++ += chroma_offset; \
881 if(COMPONENTS == 4) out_row++; \
888 for(int k = 0; k < w; k++) \
891 *out_row++ += chroma_offset; \
892 *out_row++ += chroma_offset; \
893 if(COMPONENTS == 4) out_row++; \
898 /* Copy just selected blurred channels to output and combine with original \
899 unblurred channels */ \
900 if(i == plugin->config.steps - 1) \
902 for(int j = pkg->y1; j < pkg->y2; j++) \
904 TEMP_TYPE *in_row = (TEMP_TYPE*)plugin->accum + COMPONENTS * w * j; \
905 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
906 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
907 for(int k = 0; k < w; k++) \
911 *out_row++ = (*in_row++ * fraction) / 0x10000; \
916 *out_row++ = *in_backup++; \
924 *out_row++ = ((*in_row++ * fraction) / 0x10000); \
929 *out_row++ = *in_backup++; \
935 *out_row++ = ((*in_row++ * fraction) / 0x10000); \
940 *out_row++ = *in_backup++; \
948 *out_row++ = (*in_row++ * fraction) / 0x10000; \
953 *out_row++ = *in_backup++; \
959 *out_row++ = (*in_row++ * fraction) / 0x10000; \
964 *out_row++ = *in_backup++; \
969 if(COMPONENTS == 4) \
973 *out_row++ = (*in_row++ * fraction) / 0x10000; \
978 *out_row++ = *in_backup++; \
987 void ZoomBlurUnit::process_package(LoadPackage *package)
989 ZoomBlurPackage *pkg = (ZoomBlurPackage*)package;
990 int h = plugin->output->get_h();
991 int w = plugin->output->get_w();
992 int do_r = plugin->config.r;
993 int do_g = plugin->config.g;
994 int do_b = plugin->config.b;
995 int do_a = plugin->config.a;
997 int fraction = 0x10000 / plugin->config.steps;
998 for(int i = 0; i < plugin->config.steps; i++)
1000 int *x_table = plugin->scale_x_table[i];
1001 int *y_table = plugin->scale_y_table[i];
1003 switch(plugin->input->get_color_model())
1006 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
1009 BLEND_LAYER(3, float, float, 1, 0)
1012 BLEND_LAYER(4, float, float, 1, 0)
1015 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
1018 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
1020 case BC_RGBA16161616:
1021 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
1024 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
1027 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
1030 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
1032 case BC_YUVA16161616:
1033 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
1044 ZoomBlurEngine::ZoomBlurEngine(ZoomBlurMain *plugin,
1047 : LoadServer(total_clients, total_packages)
1049 this->plugin = plugin;
1052 void ZoomBlurEngine::init_packages()
1054 for(int i = 0; i < get_total_packages(); i++)
1056 ZoomBlurPackage *package = (ZoomBlurPackage*)get_package(i);
1057 package->y1 = plugin->output->get_h() * i / get_total_packages();
1058 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
1062 LoadClient* ZoomBlurEngine::new_client()
1064 return new ZoomBlurUnit(this, plugin);
1067 LoadPackage* ZoomBlurEngine::new_package()
1069 return new ZoomBlurPackage;