5 #include "bcdisplayinfo.h"
11 #include "loadbalance.h"
12 #include "picon_png.h"
13 #include "pluginvclient.h"
18 class MotionBlurEngine;
24 class MotionBlurConfig
29 int equivalent(MotionBlurConfig &that);
30 void copy_from(MotionBlurConfig &that);
31 void interpolate(MotionBlurConfig &prev,
32 MotionBlurConfig &next,
47 class MotionBlurSize : public BC_ISlider
50 MotionBlurSize(MotionBlurMain *plugin,
57 MotionBlurMain *plugin;
62 class MotionBlurWindow : public BC_Window
65 MotionBlurWindow(MotionBlurMain *plugin, int x, int y);
71 MotionBlurSize *steps, *radius;
72 MotionBlurMain *plugin;
77 PLUGIN_THREAD_HEADER(MotionBlurMain, MotionBlurThread, MotionBlurWindow)
80 class MotionBlurMain : public PluginVClient
83 MotionBlurMain(PluginServer *server);
86 int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
90 void save_data(KeyFrame *keyframe);
91 void read_data(KeyFrame *keyframe);
94 PLUGIN_CLASS_MEMBERS(MotionBlurConfig, MotionBlurThread)
97 VFrame *input, *output, *temp;
98 MotionBlurEngine *engine;
102 unsigned char *accum;
105 class MotionBlurPackage : public LoadPackage
112 class MotionBlurUnit : public LoadClient
115 MotionBlurUnit(MotionBlurEngine *server, MotionBlurMain *plugin);
116 void process_package(LoadPackage *package);
117 MotionBlurEngine *server;
118 MotionBlurMain *plugin;
121 class MotionBlurEngine : public LoadServer
124 MotionBlurEngine(MotionBlurMain *plugin,
127 void init_packages();
128 LoadClient* new_client();
129 LoadPackage* new_package();
130 MotionBlurMain *plugin;
151 REGISTER_PLUGIN(MotionBlurMain)
155 MotionBlurConfig::MotionBlurConfig()
165 int MotionBlurConfig::equivalent(MotionBlurConfig &that)
168 radius == that.radius &&
169 steps == that.steps &&
176 void MotionBlurConfig::copy_from(MotionBlurConfig &that)
178 radius = that.radius;
186 void MotionBlurConfig::interpolate(MotionBlurConfig &prev,
187 MotionBlurConfig &next,
192 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
193 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
194 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
195 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
210 PLUGIN_THREAD_OBJECT(MotionBlurMain, MotionBlurThread, MotionBlurWindow)
214 MotionBlurWindow::MotionBlurWindow(MotionBlurMain *plugin, int x, int y)
215 : BC_Window(plugin->gui_string,
225 this->plugin = plugin;
228 MotionBlurWindow::~MotionBlurWindow()
232 int MotionBlurWindow::create_objects()
236 add_subwindow(new BC_Title(x, y, _("Length:")));
238 add_subwindow(radius = new MotionBlurSize(plugin, x, y, &plugin->config.radius, 0, 100));
240 add_subwindow(new BC_Title(x, y, _("Steps:")));
242 add_subwindow(steps = new MotionBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
249 WINDOW_CLOSE_EVENT(MotionBlurWindow)
253 MotionBlurSize::MotionBlurSize(MotionBlurMain *plugin,
259 : BC_ISlider(x, y, 0, 240, 240, min, max, *output)
261 this->plugin = plugin;
262 this->output = output;
264 int MotionBlurSize::handle_event()
266 *output = get_value();
267 plugin->send_configure_change();
280 MotionBlurMain::MotionBlurMain(PluginServer *server)
281 : PluginVClient(server)
283 PLUGIN_CONSTRUCTOR_MACRO
292 MotionBlurMain::~MotionBlurMain()
294 PLUGIN_DESTRUCTOR_MACRO
295 if(engine) delete engine;
297 if(accum) delete [] accum;
298 if(temp) delete temp;
301 char* MotionBlurMain::plugin_title() { return N_("Motion Blur"); }
302 int MotionBlurMain::is_realtime() { return 1; }
305 NEW_PICON_MACRO(MotionBlurMain)
307 SHOW_GUI_MACRO(MotionBlurMain, MotionBlurThread)
309 SET_STRING_MACRO(MotionBlurMain)
311 RAISE_WINDOW_MACRO(MotionBlurMain)
313 LOAD_CONFIGURATION_MACRO(MotionBlurMain, MotionBlurConfig)
315 void MotionBlurMain::delete_tables()
319 for(int i = 0; i < table_entries; i++)
320 delete [] scale_x_table[i];
321 delete [] scale_x_table;
326 for(int i = 0; i < table_entries; i++)
327 delete [] scale_y_table[i];
328 delete [] scale_y_table;
335 int MotionBlurMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
337 float xa,ya,za,xb,yb,zb,xd,yd,zd;
338 if (get_source_position() == 0)
339 get_camera(&xa, &ya, &za, get_source_position());
341 get_camera(&xa, &ya, &za, get_source_position()-1);
342 get_camera(&xb, &yb, &zb, get_source_position());
348 //printf("Camera automation deltas: %.2f %.2f %.2f\n", xd, yd, zd);
349 load_configuration();
351 //printf("MotionBlurMain::process_realtime 1 %d\n", config.radius);
352 if(!engine) engine = new MotionBlurEngine(this,
353 get_project_smp() + 1,
354 get_project_smp() + 1);
355 if(!accum) accum = new unsigned char[input_ptr->get_w() *
357 cmodel_components(input_ptr->get_color_model()) *
358 MAX(sizeof(int), sizeof(float))];
360 this->input = input_ptr;
361 this->output = output_ptr;
364 if(input_ptr->get_rows()[0] == output_ptr->get_rows()[0])
366 if(!temp) temp = new VFrame(0,
369 input_ptr->get_color_model());
370 temp->copy_from(input_ptr);
374 // Generate tables here. The same table is used by many packages to render
375 // each horizontal stripe. Need to cover the entire output range in each
376 // table to avoid green borders
377 float w = input->get_w();
378 float h = input->get_h();
382 float zradius = (float)(zd * config.radius / 4 + 1);
383 float center_x = w/2;
384 float center_y = h/2;
388 float min_x1, min_y1, min_x2, min_y2;
389 float max_x1, max_y1, max_x2, max_y2;
391 int steps = config.steps ? config.steps : 1;
393 x_offset = (int)(xd * config.radius);
394 y_offset = (int)(yd * config.radius);
400 min_x1 = center_x - min_w / 2;
401 min_y1 = center_y - min_h / 2;
402 min_x2 = center_x + min_w / 2;
403 min_y2 = center_y + min_h / 2;
410 scale_x_table = new int*[config.steps];
411 scale_y_table = new int*[config.steps];
412 table_entries = config.steps;
414 for(int i = 0; i < config.steps; i++)
416 float fraction = (float)(i - config.steps / 2) / config.steps;
417 float inv_fraction = 1.0 - fraction;
419 int x = (int)(fraction * x_offset);
420 int y = (int)(fraction * y_offset);
421 float out_x1 = min_x1 * fraction + max_x1 * inv_fraction;
422 float out_x2 = min_x2 * fraction + max_x2 * inv_fraction;
423 float out_y1 = min_y1 * fraction + max_y1 * inv_fraction;
424 float out_y2 = min_y2 * fraction + max_y2 * inv_fraction;
425 float out_w = out_x2 - out_x1;
426 float out_h = out_y2 - out_y1;
427 if(out_w < 0) out_w = 0;
428 if(out_h < 0) out_h = 0;
429 float scale_x = (float)w / out_w;
430 float scale_y = (float)h / out_h;
434 scale_y_table[i] = y_table = new int[(int)(h + 1)];
435 scale_x_table[i] = x_table = new int[(int)(w + 1)];
437 for(int j = 0; j < h; j++)
439 y_table[j] = (int)((j - out_y1) * scale_y) + y;
441 for(int j = 0; j < w; j++)
443 x_table[j] = (int)((j - out_x1) * scale_x) + x;
450 cmodel_components(input_ptr->get_color_model()) *
451 MAX(sizeof(int), sizeof(float)));
452 engine->process_packages();
457 void MotionBlurMain::update_gui()
461 load_configuration();
462 thread->window->lock_window();
463 thread->window->radius->update(config.radius);
464 thread->window->steps->update(config.steps);
465 thread->window->unlock_window();
470 int MotionBlurMain::load_defaults()
472 char directory[1024], string[1024];
473 // set the default directory
474 sprintf(directory, "%smotionblur.rc", BCASTDIR);
477 defaults = new Defaults(directory);
480 config.radius = defaults->get("RADIUS", config.radius);
481 config.steps = defaults->get("STEPS", config.steps);
486 int MotionBlurMain::save_defaults()
488 defaults->update("RADIUS", config.radius);
489 defaults->update("STEPS", config.steps);
496 void MotionBlurMain::save_data(KeyFrame *keyframe)
500 // cause data to be stored directly in text
501 output.set_shared_string(keyframe->data, MESSAGESIZE);
502 output.tag.set_title("MOTIONBLUR");
504 output.tag.set_property("RADIUS", config.radius);
505 output.tag.set_property("STEPS", config.steps);
507 output.terminate_string();
510 void MotionBlurMain::read_data(KeyFrame *keyframe)
514 input.set_shared_string(keyframe->data, strlen(keyframe->data));
520 result = input.read_tag();
524 if(input.tag.title_is("MOTIONBLUR"))
526 config.radius = input.tag.get_property("RADIUS", config.radius);
527 config.steps = input.tag.get_property("STEPS", config.steps);
538 MotionBlurPackage::MotionBlurPackage()
546 MotionBlurUnit::MotionBlurUnit(MotionBlurEngine *server,
547 MotionBlurMain *plugin)
550 this->plugin = plugin;
551 this->server = server;
555 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
557 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
558 for(int j = pkg->y1; j < pkg->y2; j++) \
560 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
561 int in_y = y_table[j]; \
564 if(in_y >= 0 && in_y < h) \
566 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
567 for(int k = 0; k < w; k++) \
569 int in_x = x_table[k]; \
571 if(in_x >= 0 && in_x < w) \
573 int in_offset = in_x * COMPONENTS; \
574 *out_row++ += in_row[in_offset]; \
577 *out_row++ += in_row[in_offset + 1]; \
578 *out_row++ += in_row[in_offset + 2]; \
582 *out_row++ += in_row[in_offset + 1]; \
583 *out_row++ += in_row[in_offset + 2]; \
585 if(COMPONENTS == 4) \
586 *out_row++ += in_row[in_offset + 3]; \
588 /* Blend nothing */ \
594 *out_row++ += chroma_offset; \
595 *out_row++ += chroma_offset; \
601 if(COMPONENTS == 4) out_row++; \
608 for(int k = 0; k < w; k++) \
611 *out_row++ += chroma_offset; \
612 *out_row++ += chroma_offset; \
613 if(COMPONENTS == 4) out_row++; \
618 /* Copy to output */ \
619 if(i == plugin->config.steps - 1) \
621 for(int j = pkg->y1; j < pkg->y2; j++) \
623 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
624 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
625 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
626 for(int k = 0; k < w; k++) \
628 *out_row++ = (*in_row++ * fraction) / 0x10000; \
633 *out_row++ = (*in_row++ * fraction) / 0x10000; \
636 *out_row++ = (*in_row++ * fraction) / 0x10000; \
641 *out_row++ = (*in_row++ * fraction) / 0x10000; \
643 *out_row++ = (*in_row++ * fraction) / 0x10000; \
647 if(COMPONENTS == 4) \
649 *out_row++ = (*in_row++ * fraction) / 0x10000; \
657 void MotionBlurUnit::process_package(LoadPackage *package)
659 MotionBlurPackage *pkg = (MotionBlurPackage*)package;
660 int h = plugin->output->get_h();
661 int w = plugin->output->get_w();
663 int fraction = 0x10000 / plugin->config.steps;
664 for(int i = 0; i < plugin->config.steps; i++)
666 int *x_table = plugin->scale_x_table[i];
667 int *y_table = plugin->scale_y_table[i];
669 switch(plugin->input->get_color_model())
672 BLEND_LAYER(3, float, float, 1, 0)
675 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
678 BLEND_LAYER(4, float, float, 1, 0)
681 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
684 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
686 case BC_RGBA16161616:
687 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
690 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
693 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
696 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
698 case BC_YUVA16161616:
699 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
710 MotionBlurEngine::MotionBlurEngine(MotionBlurMain *plugin,
713 : LoadServer(total_clients, total_packages)
715 this->plugin = plugin;
718 void MotionBlurEngine::init_packages()
720 for(int i = 0; i < total_packages; i++)
722 MotionBlurPackage *package = (MotionBlurPackage*)packages[i];
723 package->y1 = plugin->output->get_h() * i / total_packages;
724 package->y2 = plugin->output->get_h() * (i + 1) / total_packages;
728 LoadClient* MotionBlurEngine::new_client()
730 return new MotionBlurUnit(this, plugin);
733 LoadPackage* MotionBlurEngine::new_package()
735 return new MotionBlurPackage;