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 N_("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(frame->get_w(), frame->get_h(),
570 frame->get_color_model(), 0);
571 temp->copy_from(frame);
577 BC_CModels::components(frame->get_color_model()) *
578 MAX(sizeof(int), sizeof(float)));
579 engine->process_packages();
584 void ZoomBlurMain::update_gui()
588 load_configuration();
589 thread->window->lock_window();
590 ((ZoomBlurWindow*)thread->window)->x->update(config.x);
591 ((ZoomBlurWindow*)thread->window)->y->update(config.y);
592 ((ZoomBlurWindow*)thread->window)->radius->update(config.radius);
593 ((ZoomBlurWindow*)thread->window)->steps->update(config.steps);
594 ((ZoomBlurWindow*)thread->window)->r->update(config.r);
595 ((ZoomBlurWindow*)thread->window)->g->update(config.g);
596 ((ZoomBlurWindow*)thread->window)->b->update(config.b);
597 ((ZoomBlurWindow*)thread->window)->a->update(config.a);
598 thread->window->unlock_window();
606 void ZoomBlurMain::save_data(KeyFrame *keyframe)
610 // cause data to be stored directly in text
611 output.set_shared_output(keyframe->xbuf);
612 output.tag.set_title("ZOOMBLUR");
614 output.tag.set_property("X", config.x);
615 output.tag.set_property("Y", config.y);
616 output.tag.set_property("RADIUS", config.radius);
617 output.tag.set_property("STEPS", config.steps);
618 output.tag.set_property("R", config.r);
619 output.tag.set_property("G", config.g);
620 output.tag.set_property("B", config.b);
621 output.tag.set_property("A", config.a);
623 output.tag.set_title("/ZOOMBLUR");
625 output.append_newline();
626 output.terminate_string();
629 void ZoomBlurMain::read_data(KeyFrame *keyframe)
633 input.set_shared_input(keyframe->xbuf);
639 result = input.read_tag();
643 if(input.tag.title_is("ZOOMBLUR"))
645 config.x = input.tag.get_property("X", config.x);
646 config.y = input.tag.get_property("Y", config.y);
647 config.radius = input.tag.get_property("RADIUS", config.radius);
648 config.steps = input.tag.get_property("STEPS", config.steps);
649 config.r = input.tag.get_property("R", config.r);
650 config.g = input.tag.get_property("G", config.g);
651 config.b = input.tag.get_property("B", config.b);
652 config.a = input.tag.get_property("A", config.a);
659 static void draw_box(float x1, float y1, float x2, float y2)
662 glVertex3f(x1, y1, 0.0);
663 glVertex3f(x2, y1, 0.0);
664 glVertex3f(x2, y2, 0.0);
665 glVertex3f(x1, y2, 0.0);
670 int ZoomBlurMain::handle_opengl()
673 get_output()->to_texture();
674 get_output()->enable_opengl();
675 get_output()->init_screen();
676 get_output()->bind_texture(0);
678 //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
679 glClearColor(0.0, 0.0, 0.0, 0.0);
680 glClear(GL_COLOR_BUFFER_BIT);
682 // Draw unselected channels
684 glBlendFunc(GL_ONE, GL_ONE);
685 glDrawBuffer(GL_BACK);
687 if(!config.r || !config.g || !config.b || !config.a)
689 glColor4f(config.r ? 0 : 1,
693 get_output()->draw_texture();
695 glAccum(GL_LOAD, 1.0);
697 // Blur selected channels
698 float fraction = 1.0 / config.steps;
699 for(int i = 0; i < config.steps; i++)
701 glClear(GL_COLOR_BUFFER_BIT);
702 glColor4f(config.r ? 1 : 0,
707 get_output()->draw_texture(0,
709 get_output()->get_w(),
710 get_output()->get_h(),
712 get_output()->get_h() - layer_table[i].y1,
714 get_output()->get_h() - layer_table[i].y2,
718 glDisable(GL_TEXTURE_2D);
719 if(BC_CModels::is_yuv(get_output()->get_color_model()))
721 glColor4f(config.r ? 0.0 : 0,
725 float center_x1 = 0.0;
726 float center_x2 = get_output()->get_w();
727 if(layer_table[i].x1 > 0)
729 center_x1 = layer_table[i].x1;
730 draw_box(0, 0, layer_table[i].x1, -get_output()->get_h());
732 if(layer_table[i].x2 < get_output()->get_w())
734 center_x2 = layer_table[i].x2;
735 draw_box(layer_table[i].x2, 0, get_output()->get_w(), -get_output()->get_h());
737 if(layer_table[i].y1 > 0)
740 -get_output()->get_h(),
742 -get_output()->get_h() + layer_table[i].y1);
744 if(layer_table[i].y2 < get_output()->get_h())
747 -get_output()->get_h() + layer_table[i].y2,
754 glAccum(GL_ACCUM, fraction);
755 glEnable(GL_TEXTURE_2D);
756 glColor4f(config.r ? 1 : 0,
763 glReadBuffer(GL_BACK);
764 glDisable(GL_TEXTURE_2D);
765 glAccum(GL_RETURN, 1.0);
767 glColor4f(1, 1, 1, 1);
768 get_output()->set_opengl_state(VFrame::SCREEN);
786 ZoomBlurPackage::ZoomBlurPackage()
794 ZoomBlurUnit::ZoomBlurUnit(ZoomBlurEngine *server,
795 ZoomBlurMain *plugin)
798 this->plugin = plugin;
799 this->server = server;
803 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
805 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
806 for(int j = pkg->y1; j < pkg->y2; j++) \
808 TEMP_TYPE *out_row = (TEMP_TYPE*)plugin->accum + COMPONENTS * w * j; \
809 int in_y = y_table[j]; \
812 if(in_y >= 0 && in_y < h) \
814 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
815 for(int k = 0; k < w; k++) \
817 int in_x = x_table[k]; \
819 if(in_x >= 0 && in_x < w) \
821 int in_offset = in_x * COMPONENTS; \
822 *out_row++ += in_row[in_offset]; \
825 *out_row++ += in_row[in_offset + 1]; \
826 *out_row++ += in_row[in_offset + 2]; \
830 *out_row++ += (TEMP_TYPE)in_row[in_offset + 1]; \
831 *out_row++ += (TEMP_TYPE)in_row[in_offset + 2]; \
833 if(COMPONENTS == 4) \
834 *out_row++ += in_row[in_offset + 3]; \
836 /* Blend nothing */ \
842 *out_row++ += chroma_offset; \
843 *out_row++ += chroma_offset; \
849 if(COMPONENTS == 4) out_row++; \
856 for(int k = 0; k < w; k++) \
859 *out_row++ += chroma_offset; \
860 *out_row++ += chroma_offset; \
861 if(COMPONENTS == 4) out_row++; \
866 /* Copy just selected blurred channels to output and combine with original \
867 unblurred channels */ \
868 if(i == plugin->config.steps - 1) \
870 for(int j = pkg->y1; j < pkg->y2; j++) \
872 TEMP_TYPE *in_row = (TEMP_TYPE*)plugin->accum + COMPONENTS * w * j; \
873 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
874 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
875 for(int k = 0; k < w; k++) \
879 *out_row++ = (*in_row++ * fraction) / 0x10000; \
884 *out_row++ = *in_backup++; \
892 *out_row++ = ((*in_row++ * fraction) / 0x10000); \
897 *out_row++ = *in_backup++; \
903 *out_row++ = ((*in_row++ * fraction) / 0x10000); \
908 *out_row++ = *in_backup++; \
916 *out_row++ = (*in_row++ * fraction) / 0x10000; \
921 *out_row++ = *in_backup++; \
927 *out_row++ = (*in_row++ * fraction) / 0x10000; \
932 *out_row++ = *in_backup++; \
937 if(COMPONENTS == 4) \
941 *out_row++ = (*in_row++ * fraction) / 0x10000; \
946 *out_row++ = *in_backup++; \
955 void ZoomBlurUnit::process_package(LoadPackage *package)
957 ZoomBlurPackage *pkg = (ZoomBlurPackage*)package;
958 int h = plugin->output->get_h();
959 int w = plugin->output->get_w();
960 int do_r = plugin->config.r;
961 int do_g = plugin->config.g;
962 int do_b = plugin->config.b;
963 int do_a = plugin->config.a;
965 int fraction = 0x10000 / plugin->config.steps;
966 for(int i = 0; i < plugin->config.steps; i++)
968 int *x_table = plugin->scale_x_table[i];
969 int *y_table = plugin->scale_y_table[i];
971 switch(plugin->input->get_color_model())
974 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
977 BLEND_LAYER(3, float, float, 1, 0)
980 BLEND_LAYER(4, float, float, 1, 0)
983 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
986 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
988 case BC_RGBA16161616:
989 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
992 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
995 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
998 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
1000 case BC_YUVA16161616:
1001 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
1012 ZoomBlurEngine::ZoomBlurEngine(ZoomBlurMain *plugin,
1015 : LoadServer(total_clients, total_packages)
1017 this->plugin = plugin;
1020 void ZoomBlurEngine::init_packages()
1022 for(int i = 0; i < get_total_packages(); i++)
1024 ZoomBlurPackage *package = (ZoomBlurPackage*)get_package(i);
1025 package->y1 = plugin->output->get_h() * i / get_total_packages();
1026 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
1030 LoadClient* ZoomBlurEngine::new_client()
1032 return new ZoomBlurUnit(this, plugin);
1035 LoadPackage* ZoomBlurEngine::new_package()
1037 return new ZoomBlurPackage;