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"
32 #include "loadbalance.h"
33 #include "pluginvclient.h"
38 class MotionBlurEngine;
44 class MotionBlurConfig
49 int equivalent(MotionBlurConfig &that);
50 void copy_from(MotionBlurConfig &that);
51 void interpolate(MotionBlurConfig &prev,
52 MotionBlurConfig &next,
67 class MotionBlurSize : public BC_ISlider
70 MotionBlurSize(MotionBlurMain *plugin,
77 MotionBlurMain *plugin;
82 class MotionBlurWindow : public PluginClientWindow
85 MotionBlurWindow(MotionBlurMain *plugin);
88 void create_objects();
91 MotionBlurSize *steps, *radius;
92 MotionBlurMain *plugin;
100 class MotionBlurMain : public PluginVClient
103 MotionBlurMain(PluginServer *server);
106 int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
108 void save_data(KeyFrame *keyframe);
109 void read_data(KeyFrame *keyframe);
112 PLUGIN_CLASS_MEMBERS(MotionBlurConfig)
114 void delete_tables();
115 VFrame *input, *output, *temp;
116 MotionBlurEngine *engine;
120 unsigned char *accum;
123 class MotionBlurPackage : public LoadPackage
130 class MotionBlurUnit : public LoadClient
133 MotionBlurUnit(MotionBlurEngine *server, MotionBlurMain *plugin);
134 void process_package(LoadPackage *package);
135 MotionBlurEngine *server;
136 MotionBlurMain *plugin;
139 class MotionBlurEngine : public LoadServer
142 MotionBlurEngine(MotionBlurMain *plugin,
145 void init_packages();
146 LoadClient* new_client();
147 LoadPackage* new_package();
148 MotionBlurMain *plugin;
169 REGISTER_PLUGIN(MotionBlurMain)
173 MotionBlurConfig::MotionBlurConfig()
183 int MotionBlurConfig::equivalent(MotionBlurConfig &that)
186 radius == that.radius &&
187 steps == that.steps &&
194 void MotionBlurConfig::copy_from(MotionBlurConfig &that)
196 radius = that.radius;
204 void MotionBlurConfig::interpolate(MotionBlurConfig &prev,
205 MotionBlurConfig &next,
210 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
211 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
212 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
213 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
232 MotionBlurWindow::MotionBlurWindow(MotionBlurMain *plugin)
233 : PluginClientWindow(plugin,
240 this->plugin = plugin;
243 MotionBlurWindow::~MotionBlurWindow()
247 void MotionBlurWindow::create_objects()
251 add_subwindow(new BC_Title(x, y, _("Length:")));
253 add_subwindow(radius = new MotionBlurSize(plugin, x, y, &plugin->config.radius, 0, 100));
255 add_subwindow(new BC_Title(x, y, _("Steps:")));
257 add_subwindow(steps = new MotionBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
267 MotionBlurSize::MotionBlurSize(MotionBlurMain *plugin,
273 : BC_ISlider(x, y, 0, 240, 240, min, max, *output)
275 this->plugin = plugin;
276 this->output = output;
278 int MotionBlurSize::handle_event()
280 *output = get_value();
281 plugin->send_configure_change();
294 MotionBlurMain::MotionBlurMain(PluginServer *server)
295 : PluginVClient(server)
306 MotionBlurMain::~MotionBlurMain()
309 if(engine) delete engine;
311 if(accum) delete [] accum;
312 if(temp) delete temp;
315 const char* MotionBlurMain::plugin_title() { return _("Motion Blur"); }
316 int MotionBlurMain::is_realtime() { return 1; }
319 NEW_WINDOW_MACRO(MotionBlurMain, MotionBlurWindow)
321 LOAD_CONFIGURATION_MACRO(MotionBlurMain, MotionBlurConfig)
323 void MotionBlurMain::delete_tables()
327 for(int i = 0; i < table_entries; i++)
328 delete [] scale_x_table[i];
329 delete [] scale_x_table;
334 for(int i = 0; i < table_entries; i++)
335 delete [] scale_y_table[i];
336 delete [] scale_y_table;
343 int MotionBlurMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
345 float xa,ya,za,xb,yb,zb,xd,yd,zd;
346 if (get_source_position() == 0)
347 get_camera(&xa, &ya, &za, get_source_position());
349 get_camera(&xa, &ya, &za, get_source_position()-1);
350 get_camera(&xb, &yb, &zb, get_source_position());
356 //printf("Camera automation deltas: %.2f %.2f %.2f\n", xd, yd, zd);
357 load_configuration();
359 //printf("MotionBlurMain::process_realtime 1 %d\n", config.radius);
360 if(!engine) engine = new MotionBlurEngine(this,
361 get_project_smp() + 1,
362 get_project_smp() + 1);
363 if(!accum) accum = new unsigned char[input_ptr->get_w() *
365 BC_CModels::components(input_ptr->get_color_model()) *
366 MAX(sizeof(int), sizeof(float))];
368 this->input = input_ptr;
369 this->output = output_ptr;
372 if(input_ptr->get_rows()[0] == output_ptr->get_rows()[0])
374 if(!temp) temp = new VFrame(0,
378 input_ptr->get_color_model(),
380 temp->copy_from(input_ptr);
384 // Generate tables here. The same table is used by many packages to render
385 // each horizontal stripe. Need to cover the entire output range in each
386 // table to avoid green borders
387 float w = input->get_w();
388 float h = input->get_h();
392 float fradius = config.radius * 0.5;
393 float zradius = (float)(zd * fradius / 4 + 1);
394 float center_x = w/2;
395 float center_y = h/2;
398 //float max_w, max_h;
399 float min_x1, min_y1, min_x2, min_y2;
400 float max_x1, max_y1, max_x2, max_y2;
402 //int steps = config.steps ? config.steps : 1;
404 x_offset = (int)(xd * fradius);
405 y_offset = (int)(yd * fradius);
411 min_x1 = center_x - min_w / 2;
412 min_y1 = center_y - min_h / 2;
413 min_x2 = center_x + min_w / 2;
414 min_y2 = center_y + min_h / 2;
421 scale_x_table = new int*[config.steps];
422 scale_y_table = new int*[config.steps];
423 table_entries = config.steps;
425 for(int i = 0; i < config.steps; i++)
427 float fraction = (float)(i - config.steps / 2) / config.steps;
428 float inv_fraction = 1.0 - fraction;
430 int x = (int)(fraction * x_offset);
431 int y = (int)(fraction * y_offset);
432 float out_x1 = min_x1 * fraction + max_x1 * inv_fraction;
433 float out_x2 = min_x2 * fraction + max_x2 * inv_fraction;
434 float out_y1 = min_y1 * fraction + max_y1 * inv_fraction;
435 float out_y2 = min_y2 * fraction + max_y2 * inv_fraction;
436 float out_w = out_x2 - out_x1;
437 float out_h = out_y2 - out_y1;
438 if(out_w < 0) out_w = 0;
439 if(out_h < 0) out_h = 0;
440 float scale_x = (float)w / out_w;
441 float scale_y = (float)h / out_h;
445 scale_y_table[i] = y_table = new int[(int)(h + 1)];
446 scale_x_table[i] = x_table = new int[(int)(w + 1)];
448 for(int j = 0; j < h; j++)
450 y_table[j] = (int)((j - out_y1) * scale_y) + y;
452 for(int j = 0; j < w; j++)
454 x_table[j] = (int)((j - out_x1) * scale_x) + x;
461 BC_CModels::components(input_ptr->get_color_model()) *
462 MAX(sizeof(int), sizeof(float)));
463 engine->process_packages();
468 void MotionBlurMain::update_gui()
472 load_configuration();
473 thread->window->lock_window();
474 ((MotionBlurWindow*)thread->window)->radius->update(config.radius);
475 ((MotionBlurWindow*)thread->window)->steps->update(config.steps);
476 thread->window->unlock_window();
484 void MotionBlurMain::save_data(KeyFrame *keyframe)
488 // cause data to be stored directly in text
489 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
490 output.tag.set_title("MOTIONBLUR");
492 output.tag.set_property("RADIUS", config.radius);
493 output.tag.set_property("STEPS", config.steps);
495 output.tag.set_title("/MOTIONBLUR");
497 output.append_newline();
498 output.terminate_string();
501 void MotionBlurMain::read_data(KeyFrame *keyframe)
505 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
511 result = input.read_tag();
515 if(input.tag.title_is("MOTIONBLUR"))
517 config.radius = input.tag.get_property("RADIUS", config.radius);
518 config.steps = input.tag.get_property("STEPS", config.steps);
529 MotionBlurPackage::MotionBlurPackage()
537 MotionBlurUnit::MotionBlurUnit(MotionBlurEngine *server,
538 MotionBlurMain *plugin)
541 this->plugin = plugin;
542 this->server = server;
546 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
548 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
549 for(int j = pkg->y1; j < pkg->y2; j++) \
551 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
552 int in_y = y_table[j]; \
555 if(in_y >= 0 && in_y < h) \
557 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
558 for(int k = 0; k < w; k++) \
560 int in_x = x_table[k]; \
562 if(in_x >= 0 && in_x < w) \
564 int in_offset = in_x * COMPONENTS; \
565 *out_row++ += in_row[in_offset]; \
568 *out_row++ += in_row[in_offset + 1]; \
569 *out_row++ += in_row[in_offset + 2]; \
573 *out_row++ += in_row[in_offset + 1]; \
574 *out_row++ += in_row[in_offset + 2]; \
576 if(COMPONENTS == 4) \
577 *out_row++ += in_row[in_offset + 3]; \
579 /* Blend nothing */ \
585 *out_row++ += chroma_offset; \
586 *out_row++ += chroma_offset; \
592 if(COMPONENTS == 4) out_row++; \
599 for(int k = 0; k < w; k++) \
602 *out_row++ += chroma_offset; \
603 *out_row++ += chroma_offset; \
604 if(COMPONENTS == 4) out_row++; \
609 /* Copy to output */ \
610 if(i == plugin->config.steps - 1) \
612 for(int j = pkg->y1; j < pkg->y2; j++) \
614 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
615 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
616 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
617 for(int k = 0; k < w; k++) \
619 *out_row++ = (*in_row++ * fraction) / 0x10000; \
624 *out_row++ = (*in_row++ * fraction) / 0x10000; \
627 *out_row++ = (*in_row++ * fraction) / 0x10000; \
632 *out_row++ = (*in_row++ * fraction) / 0x10000; \
634 *out_row++ = (*in_row++ * fraction) / 0x10000; \
638 if(COMPONENTS == 4) \
640 *out_row++ = (*in_row++ * fraction) / 0x10000; \
648 void MotionBlurUnit::process_package(LoadPackage *package)
650 MotionBlurPackage *pkg = (MotionBlurPackage*)package;
651 int h = plugin->output->get_h();
652 int w = plugin->output->get_w();
654 int fraction = 0x10000 / plugin->config.steps;
655 for(int i = 0; i < plugin->config.steps; i++)
657 int *x_table = plugin->scale_x_table[i];
658 int *y_table = plugin->scale_y_table[i];
660 switch(plugin->input->get_color_model())
663 BLEND_LAYER(3, float, float, 1, 0)
666 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
669 BLEND_LAYER(4, float, float, 1, 0)
672 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
675 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
677 case BC_RGBA16161616:
678 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
681 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
684 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
687 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
689 case BC_YUVA16161616:
690 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
701 MotionBlurEngine::MotionBlurEngine(MotionBlurMain *plugin,
704 : LoadServer(total_clients, total_packages)
706 this->plugin = plugin;
709 void MotionBlurEngine::init_packages()
711 for(int i = 0; i < get_total_packages(); i++)
713 MotionBlurPackage *package = (MotionBlurPackage*)get_package(i);
714 package->y1 = plugin->output->get_h() * i / get_total_packages();
715 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
719 LoadClient* MotionBlurEngine::new_client()
721 return new MotionBlurUnit(this, plugin);
724 LoadPackage* MotionBlurEngine::new_package()
726 return new MotionBlurPackage;