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
26 #include "bcdisplayinfo.h"
27 #include "bcsignals.h"
33 #include "loadbalance.h"
34 #include "pluginvclient.h"
51 int equivalent(ZoomBlurConfig &that);
52 void copy_from(ZoomBlurConfig &that);
53 void interpolate(ZoomBlurConfig &prev,
71 class ZoomBlurSize : public BC_ISlider
74 ZoomBlurSize(ZoomBlurMain *plugin,
85 class ZoomBlurToggle : public BC_CheckBox
88 ZoomBlurToggle(ZoomBlurMain *plugin,
98 class ZoomBlurWindow : public PluginClientWindow
101 ZoomBlurWindow(ZoomBlurMain *plugin);
104 void create_objects();
106 ZoomBlurSize *x, *y, *radius, *steps;
107 ZoomBlurToggle *r, *g, *b, *a;
108 ZoomBlurMain *plugin;
115 // Output coords for a layer of blurring
116 // Used for OpenGL only
121 float x1, y1, x2, y2;
124 class ZoomBlurMain : public PluginVClient
127 ZoomBlurMain(PluginServer *server);
130 int process_buffer(VFrame *frame,
131 int64_t start_position,
134 void save_data(KeyFrame *keyframe);
135 void read_data(KeyFrame *keyframe);
139 PLUGIN_CLASS_MEMBERS(ZoomBlurConfig)
141 void delete_tables();
142 VFrame *input, *output, *temp;
143 ZoomBlurEngine *engine;
146 ZoomBlurLayer *layer_table;
148 int need_reconfigure;
149 // The accumulation buffer is needed because 8 bits isn't precise enough
150 unsigned char *accum;
153 class ZoomBlurPackage : public LoadPackage
160 class ZoomBlurUnit : public LoadClient
163 ZoomBlurUnit(ZoomBlurEngine *server, ZoomBlurMain *plugin);
164 void process_package(LoadPackage *package);
165 ZoomBlurEngine *server;
166 ZoomBlurMain *plugin;
169 class ZoomBlurEngine : public LoadServer
172 ZoomBlurEngine(ZoomBlurMain *plugin,
175 void init_packages();
176 LoadClient* new_client();
177 LoadPackage* new_package();
178 ZoomBlurMain *plugin;
199 REGISTER_PLUGIN(ZoomBlurMain)
203 ZoomBlurConfig::ZoomBlurConfig()
215 int ZoomBlurConfig::equivalent(ZoomBlurConfig &that)
220 radius == that.radius &&
221 steps == that.steps &&
228 void ZoomBlurConfig::copy_from(ZoomBlurConfig &that)
232 radius = that.radius;
240 void ZoomBlurConfig::interpolate(ZoomBlurConfig &prev,
241 ZoomBlurConfig &next,
246 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
247 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
248 this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
249 this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
250 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
251 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
269 ZoomBlurWindow::ZoomBlurWindow(ZoomBlurMain *plugin)
270 : PluginClientWindow(plugin,
277 this->plugin = plugin;
280 ZoomBlurWindow::~ZoomBlurWindow()
284 void ZoomBlurWindow::create_objects()
288 add_subwindow(new BC_Title(x, y, _("X:")));
290 add_subwindow(this->x = new ZoomBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
292 add_subwindow(new BC_Title(x, y, _("Y:")));
294 add_subwindow(this->y = new ZoomBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
296 add_subwindow(new BC_Title(x, y, _("Radius:")));
298 add_subwindow(radius = new ZoomBlurSize(plugin, x, y, &plugin->config.radius, -100, 100));
300 add_subwindow(new BC_Title(x, y, _("Steps:")));
302 add_subwindow(steps = new ZoomBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
304 add_subwindow(r = new ZoomBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
306 add_subwindow(g = new ZoomBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
308 add_subwindow(b = new ZoomBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
310 add_subwindow(a = new ZoomBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
328 ZoomBlurToggle::ZoomBlurToggle(ZoomBlurMain *plugin,
333 : BC_CheckBox(x, y, *output, string)
335 this->plugin = plugin;
336 this->output = output;
339 int ZoomBlurToggle::handle_event()
341 *output = get_value();
342 plugin->send_configure_change();
352 ZoomBlurSize::ZoomBlurSize(ZoomBlurMain *plugin,
358 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
360 this->plugin = plugin;
361 this->output = output;
363 int ZoomBlurSize::handle_event()
365 *output = get_value();
366 plugin->send_configure_change();
379 ZoomBlurMain::ZoomBlurMain(PluginServer *server)
380 : PluginVClient(server)
389 need_reconfigure = 1;
393 ZoomBlurMain::~ZoomBlurMain()
396 if(engine) delete engine;
398 if(accum) delete [] accum;
399 if(temp) delete temp;
402 const char* ZoomBlurMain::plugin_title() { return _("Zoom Blur"); }
403 int ZoomBlurMain::is_realtime() { return 1; }
407 NEW_WINDOW_MACRO(ZoomBlurMain, ZoomBlurWindow)
409 LOAD_CONFIGURATION_MACRO(ZoomBlurMain, ZoomBlurConfig)
411 void ZoomBlurMain::delete_tables()
415 for(int i = 0; i < table_entries; i++)
416 delete [] scale_x_table[i];
417 delete [] scale_x_table;
422 for(int i = 0; i < table_entries; i++)
423 delete [] scale_y_table[i];
424 delete [] scale_y_table;
427 delete [] layer_table;
434 int ZoomBlurMain::process_buffer(VFrame *frame,
435 int64_t start_position,
438 need_reconfigure |= load_configuration();
444 get_source_position(),
450 // Generate tables here. The same table is used by many packages to render
451 // each horizontal stripe. Need to cover the entire output range in each
452 // table to avoid green borders
456 float w = frame->get_w();
457 float h = frame->get_h();
458 float center_x = (float)config.x / 100 * w;
459 float center_y = (float)config.y / 100 * h;
460 float radius = (float)(100 + config.radius) / 100;
462 //float max_w, max_h;
463 int steps = config.steps ? config.steps : 1;
475 // printf("ZoomBlurMain::process_realtime 1 %d %d\n",
479 center_x = (center_x - w / 2) * (1.0 - radius) + w / 2;
480 center_y = (center_y - h / 2) * (1.0 - radius) + h / 2;
485 min_x1 = center_x - min_w / 2;
486 min_y1 = center_y - min_h / 2;
487 min_x2 = center_x + min_w / 2;
488 min_y2 = center_y + min_h / 2;
495 // printf("ZoomBlurMain::process_realtime 2 w=%f radius=%f center_x=%f\n",
501 // Dimensions of outermost rectangle
504 table_entries = steps;
505 scale_x_table = new int*[steps];
506 scale_y_table = new int*[steps];
507 layer_table = new ZoomBlurLayer[table_entries];
510 for(int i = 0; i < steps; i++)
512 float fraction = (float)i / steps;
513 float inv_fraction = 1.0 - fraction;
514 float out_x1 = min_x1 * fraction + max_x1 * inv_fraction;
515 float out_x2 = min_x2 * fraction + max_x2 * inv_fraction;
516 float out_y1 = min_y1 * fraction + max_y1 * inv_fraction;
517 float out_y2 = min_y2 * fraction + max_y2 * inv_fraction;
518 float out_w = out_x2 - out_x1;
519 float out_h = out_y2 - out_y1;
520 if(out_w < 0) out_w = 0;
521 if(out_h < 0) out_h = 0;
522 float scale_x = (float)w / out_w;
523 float scale_y = (float)h / out_h;
527 scale_y_table[i] = y_table = new int[(int)(h + 1)];
528 scale_x_table[i] = x_table = new int[(int)(w + 1)];
530 layer_table[i].x1 = out_x1;
531 layer_table[i].y1 = out_y1;
532 layer_table[i].x2 = out_x2;
533 layer_table[i].y2 = out_y2;
536 for(int j = 0; j < h; j++)
538 y_table[j] = (int)((j - out_y1) * scale_y);
540 for(int j = 0; j < w; j++)
542 x_table[j] = (int)((j - out_x1) * scale_x);
543 //printf("ZoomBlurMain::process_realtime %d %d\n", j, x_table[j]);
547 need_reconfigure = 0;
551 if(get_use_opengl()) return run_opengl();
557 if(!engine) engine = new ZoomBlurEngine(this,
558 get_project_smp() + 1,
559 get_project_smp() + 1);
560 if(!accum) accum = new unsigned char[frame->get_w() *
562 BC_CModels::components(frame->get_color_model()) *
563 MAX(sizeof(int), sizeof(float))];
566 this->output = frame;
569 if(!temp) temp = new VFrame(0,
573 frame->get_color_model(),
575 temp->copy_from(frame);
581 BC_CModels::components(frame->get_color_model()) *
582 MAX(sizeof(int), sizeof(float)));
583 engine->process_packages();
588 void ZoomBlurMain::update_gui()
592 load_configuration();
593 thread->window->lock_window();
594 ((ZoomBlurWindow*)thread->window)->x->update(config.x);
595 ((ZoomBlurWindow*)thread->window)->y->update(config.y);
596 ((ZoomBlurWindow*)thread->window)->radius->update(config.radius);
597 ((ZoomBlurWindow*)thread->window)->steps->update(config.steps);
598 ((ZoomBlurWindow*)thread->window)->r->update(config.r);
599 ((ZoomBlurWindow*)thread->window)->g->update(config.g);
600 ((ZoomBlurWindow*)thread->window)->b->update(config.b);
601 ((ZoomBlurWindow*)thread->window)->a->update(config.a);
602 thread->window->unlock_window();
610 void ZoomBlurMain::save_data(KeyFrame *keyframe)
614 // cause data to be stored directly in text
615 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
616 output.tag.set_title("ZOOMBLUR");
618 output.tag.set_property("X", config.x);
619 output.tag.set_property("Y", config.y);
620 output.tag.set_property("RADIUS", config.radius);
621 output.tag.set_property("STEPS", config.steps);
622 output.tag.set_property("R", config.r);
623 output.tag.set_property("G", config.g);
624 output.tag.set_property("B", config.b);
625 output.tag.set_property("A", config.a);
627 output.tag.set_title("/ZOOMBLUR");
629 output.append_newline();
630 output.terminate_string();
633 void ZoomBlurMain::read_data(KeyFrame *keyframe)
637 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
643 result = input.read_tag();
647 if(input.tag.title_is("ZOOMBLUR"))
649 config.x = input.tag.get_property("X", config.x);
650 config.y = input.tag.get_property("Y", config.y);
651 config.radius = input.tag.get_property("RADIUS", config.radius);
652 config.steps = input.tag.get_property("STEPS", config.steps);
653 config.r = input.tag.get_property("R", config.r);
654 config.g = input.tag.get_property("G", config.g);
655 config.b = input.tag.get_property("B", config.b);
656 config.a = input.tag.get_property("A", config.a);
663 static void draw_box(float x1, float y1, float x2, float y2)
666 glVertex3f(x1, y1, 0.0);
667 glVertex3f(x2, y1, 0.0);
668 glVertex3f(x2, y2, 0.0);
669 glVertex3f(x1, y2, 0.0);
674 int ZoomBlurMain::handle_opengl()
677 get_output()->to_texture();
678 get_output()->enable_opengl();
679 get_output()->init_screen();
680 get_output()->bind_texture(0);
682 //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
683 glClearColor(0.0, 0.0, 0.0, 0.0);
684 glClear(GL_COLOR_BUFFER_BIT);
686 // Draw unselected channels
688 glBlendFunc(GL_ONE, GL_ONE);
689 glDrawBuffer(GL_BACK);
691 if(!config.r || !config.g || !config.b || !config.a)
693 glColor4f(config.r ? 0 : 1,
697 get_output()->draw_texture();
699 glAccum(GL_LOAD, 1.0);
701 // Blur selected channels
702 float fraction = 1.0 / config.steps;
703 for(int i = 0; i < config.steps; i++)
705 glClear(GL_COLOR_BUFFER_BIT);
706 glColor4f(config.r ? 1 : 0,
711 get_output()->draw_texture(0,
713 get_output()->get_w(),
714 get_output()->get_h(),
716 get_output()->get_h() - layer_table[i].y1,
718 get_output()->get_h() - layer_table[i].y2,
722 glDisable(GL_TEXTURE_2D);
723 if(BC_CModels::is_yuv(get_output()->get_color_model()))
725 glColor4f(config.r ? 0.0 : 0,
729 float center_x1 = 0.0;
730 float center_x2 = get_output()->get_w();
731 if(layer_table[i].x1 > 0)
733 center_x1 = layer_table[i].x1;
734 draw_box(0, 0, layer_table[i].x1, -get_output()->get_h());
736 if(layer_table[i].x2 < get_output()->get_w())
738 center_x2 = layer_table[i].x2;
739 draw_box(layer_table[i].x2, 0, get_output()->get_w(), -get_output()->get_h());
741 if(layer_table[i].y1 > 0)
744 -get_output()->get_h(),
746 -get_output()->get_h() + layer_table[i].y1);
748 if(layer_table[i].y2 < get_output()->get_h())
751 -get_output()->get_h() + layer_table[i].y2,
758 glAccum(GL_ACCUM, fraction);
759 glEnable(GL_TEXTURE_2D);
760 glColor4f(config.r ? 1 : 0,
767 glReadBuffer(GL_BACK);
768 glDisable(GL_TEXTURE_2D);
769 glAccum(GL_RETURN, 1.0);
771 glColor4f(1, 1, 1, 1);
772 get_output()->set_opengl_state(VFrame::SCREEN);
790 ZoomBlurPackage::ZoomBlurPackage()
798 ZoomBlurUnit::ZoomBlurUnit(ZoomBlurEngine *server,
799 ZoomBlurMain *plugin)
802 this->plugin = plugin;
803 this->server = server;
807 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
809 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
810 for(int j = pkg->y1; j < pkg->y2; j++) \
812 TEMP_TYPE *out_row = (TEMP_TYPE*)plugin->accum + COMPONENTS * w * j; \
813 int in_y = y_table[j]; \
816 if(in_y >= 0 && in_y < h) \
818 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
819 for(int k = 0; k < w; k++) \
821 int in_x = x_table[k]; \
823 if(in_x >= 0 && in_x < w) \
825 int in_offset = in_x * COMPONENTS; \
826 *out_row++ += in_row[in_offset]; \
829 *out_row++ += in_row[in_offset + 1]; \
830 *out_row++ += in_row[in_offset + 2]; \
834 *out_row++ += (TEMP_TYPE)in_row[in_offset + 1]; \
835 *out_row++ += (TEMP_TYPE)in_row[in_offset + 2]; \
837 if(COMPONENTS == 4) \
838 *out_row++ += in_row[in_offset + 3]; \
840 /* Blend nothing */ \
846 *out_row++ += chroma_offset; \
847 *out_row++ += chroma_offset; \
853 if(COMPONENTS == 4) out_row++; \
860 for(int k = 0; k < w; k++) \
863 *out_row++ += chroma_offset; \
864 *out_row++ += chroma_offset; \
865 if(COMPONENTS == 4) out_row++; \
870 /* Copy just selected blurred channels to output and combine with original \
871 unblurred channels */ \
872 if(i == plugin->config.steps - 1) \
874 for(int j = pkg->y1; j < pkg->y2; j++) \
876 TEMP_TYPE *in_row = (TEMP_TYPE*)plugin->accum + COMPONENTS * w * j; \
877 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
878 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
879 for(int k = 0; k < w; k++) \
883 *out_row++ = (*in_row++ * fraction) / 0x10000; \
888 *out_row++ = *in_backup++; \
896 *out_row++ = ((*in_row++ * fraction) / 0x10000); \
901 *out_row++ = *in_backup++; \
907 *out_row++ = ((*in_row++ * fraction) / 0x10000); \
912 *out_row++ = *in_backup++; \
920 *out_row++ = (*in_row++ * fraction) / 0x10000; \
925 *out_row++ = *in_backup++; \
931 *out_row++ = (*in_row++ * fraction) / 0x10000; \
936 *out_row++ = *in_backup++; \
941 if(COMPONENTS == 4) \
945 *out_row++ = (*in_row++ * fraction) / 0x10000; \
950 *out_row++ = *in_backup++; \
959 void ZoomBlurUnit::process_package(LoadPackage *package)
961 ZoomBlurPackage *pkg = (ZoomBlurPackage*)package;
962 int h = plugin->output->get_h();
963 int w = plugin->output->get_w();
964 int do_r = plugin->config.r;
965 int do_g = plugin->config.g;
966 int do_b = plugin->config.b;
967 int do_a = plugin->config.a;
969 int fraction = 0x10000 / plugin->config.steps;
970 for(int i = 0; i < plugin->config.steps; i++)
972 int *x_table = plugin->scale_x_table[i];
973 int *y_table = plugin->scale_y_table[i];
975 switch(plugin->input->get_color_model())
978 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
981 BLEND_LAYER(3, float, float, 1, 0)
984 BLEND_LAYER(4, float, float, 1, 0)
987 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
990 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
992 case BC_RGBA16161616:
993 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
996 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
999 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
1002 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
1004 case BC_YUVA16161616:
1005 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
1016 ZoomBlurEngine::ZoomBlurEngine(ZoomBlurMain *plugin,
1019 : LoadServer(total_clients, total_packages)
1021 this->plugin = plugin;
1024 void ZoomBlurEngine::init_packages()
1026 for(int i = 0; i < get_total_packages(); i++)
1028 ZoomBlurPackage *package = (ZoomBlurPackage*)get_package(i);
1029 package->y1 = plugin->output->get_h() * i / get_total_packages();
1030 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
1034 LoadClient* ZoomBlurEngine::new_client()
1036 return new ZoomBlurUnit(this, plugin);
1039 LoadPackage* ZoomBlurEngine::new_package()
1041 return new ZoomBlurPackage;