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 N_("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(input_ptr->get_w(), input_ptr->get_h(),
375 input_ptr->get_color_model(), 0);
376 temp->copy_from(input_ptr);
380 // Generate tables here. The same table is used by many packages to render
381 // each horizontal stripe. Need to cover the entire output range in each
382 // table to avoid green borders
383 float w = input->get_w();
384 float h = input->get_h();
388 float fradius = config.radius * 0.5;
389 float zradius = (float)(zd * fradius / 4 + 1);
390 float center_x = w/2;
391 float center_y = h/2;
394 //float max_w, max_h;
395 float min_x1, min_y1, min_x2, min_y2;
396 float max_x1, max_y1, max_x2, max_y2;
398 //int steps = config.steps ? config.steps : 1;
400 x_offset = (int)(xd * fradius);
401 y_offset = (int)(yd * fradius);
407 min_x1 = center_x - min_w / 2;
408 min_y1 = center_y - min_h / 2;
409 min_x2 = center_x + min_w / 2;
410 min_y2 = center_y + min_h / 2;
417 scale_x_table = new int*[config.steps];
418 scale_y_table = new int*[config.steps];
419 table_entries = config.steps;
421 for(int i = 0; i < config.steps; i++)
423 float fraction = (float)(i - config.steps / 2) / config.steps;
424 float inv_fraction = 1.0 - fraction;
426 int x = (int)(fraction * x_offset);
427 int y = (int)(fraction * y_offset);
428 float out_x1 = min_x1 * fraction + max_x1 * inv_fraction;
429 float out_x2 = min_x2 * fraction + max_x2 * inv_fraction;
430 float out_y1 = min_y1 * fraction + max_y1 * inv_fraction;
431 float out_y2 = min_y2 * fraction + max_y2 * inv_fraction;
432 float out_w = out_x2 - out_x1;
433 float out_h = out_y2 - out_y1;
434 if(out_w < 0) out_w = 0;
435 if(out_h < 0) out_h = 0;
436 float scale_x = (float)w / out_w;
437 float scale_y = (float)h / out_h;
441 scale_y_table[i] = y_table = new int[(int)(h + 1)];
442 scale_x_table[i] = x_table = new int[(int)(w + 1)];
444 for(int j = 0; j < h; j++)
446 y_table[j] = (int)((j - out_y1) * scale_y) + y;
448 for(int j = 0; j < w; j++)
450 x_table[j] = (int)((j - out_x1) * scale_x) + x;
457 BC_CModels::components(input_ptr->get_color_model()) *
458 MAX(sizeof(int), sizeof(float)));
459 engine->process_packages();
464 void MotionBlurMain::update_gui()
468 load_configuration();
469 thread->window->lock_window();
470 ((MotionBlurWindow*)thread->window)->radius->update(config.radius);
471 ((MotionBlurWindow*)thread->window)->steps->update(config.steps);
472 thread->window->unlock_window();
480 void MotionBlurMain::save_data(KeyFrame *keyframe)
484 // cause data to be stored directly in text
485 output.set_shared_output(keyframe->xbuf);
486 output.tag.set_title("MOTIONBLUR");
488 output.tag.set_property("RADIUS", config.radius);
489 output.tag.set_property("STEPS", config.steps);
491 output.tag.set_title("/MOTIONBLUR");
493 output.append_newline();
494 output.terminate_string();
497 void MotionBlurMain::read_data(KeyFrame *keyframe)
501 input.set_shared_input(keyframe->xbuf);
507 result = input.read_tag();
511 if(input.tag.title_is("MOTIONBLUR"))
513 config.radius = input.tag.get_property("RADIUS", config.radius);
514 config.steps = input.tag.get_property("STEPS", config.steps);
525 MotionBlurPackage::MotionBlurPackage()
533 MotionBlurUnit::MotionBlurUnit(MotionBlurEngine *server,
534 MotionBlurMain *plugin)
537 this->plugin = plugin;
538 this->server = server;
542 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
544 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
545 for(int j = pkg->y1; j < pkg->y2; j++) \
547 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
548 int in_y = y_table[j]; \
551 if(in_y >= 0 && in_y < h) \
553 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
554 for(int k = 0; k < w; k++) \
556 int in_x = x_table[k]; \
558 if(in_x >= 0 && in_x < w) \
560 int in_offset = in_x * COMPONENTS; \
561 *out_row++ += in_row[in_offset]; \
564 *out_row++ += in_row[in_offset + 1]; \
565 *out_row++ += in_row[in_offset + 2]; \
569 *out_row++ += in_row[in_offset + 1]; \
570 *out_row++ += in_row[in_offset + 2]; \
572 if(COMPONENTS == 4) \
573 *out_row++ += in_row[in_offset + 3]; \
575 /* Blend nothing */ \
581 *out_row++ += chroma_offset; \
582 *out_row++ += chroma_offset; \
588 if(COMPONENTS == 4) out_row++; \
595 for(int k = 0; k < w; k++) \
598 *out_row++ += chroma_offset; \
599 *out_row++ += chroma_offset; \
600 if(COMPONENTS == 4) out_row++; \
605 /* Copy to output */ \
606 if(i == plugin->config.steps - 1) \
608 for(int j = pkg->y1; j < pkg->y2; j++) \
610 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
611 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
612 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
613 for(int k = 0; k < w; k++) \
615 *out_row++ = (*in_row++ * fraction) / 0x10000; \
620 *out_row++ = (*in_row++ * fraction) / 0x10000; \
623 *out_row++ = (*in_row++ * fraction) / 0x10000; \
628 *out_row++ = (*in_row++ * fraction) / 0x10000; \
630 *out_row++ = (*in_row++ * fraction) / 0x10000; \
634 if(COMPONENTS == 4) \
636 *out_row++ = (*in_row++ * fraction) / 0x10000; \
644 void MotionBlurUnit::process_package(LoadPackage *package)
646 MotionBlurPackage *pkg = (MotionBlurPackage*)package;
647 int h = plugin->output->get_h();
648 int w = plugin->output->get_w();
650 int fraction = 0x10000 / plugin->config.steps;
651 for(int i = 0; i < plugin->config.steps; i++)
653 int *x_table = plugin->scale_x_table[i];
654 int *y_table = plugin->scale_y_table[i];
656 switch(plugin->input->get_color_model())
659 BLEND_LAYER(3, float, float, 1, 0)
662 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
665 BLEND_LAYER(4, float, float, 1, 0)
668 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
671 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
673 case BC_RGBA16161616:
674 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
677 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
680 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
683 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
685 case BC_YUVA16161616:
686 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
697 MotionBlurEngine::MotionBlurEngine(MotionBlurMain *plugin,
700 : LoadServer(total_clients, total_packages)
702 this->plugin = plugin;
705 void MotionBlurEngine::init_packages()
707 for(int i = 0; i < get_total_packages(); i++)
709 MotionBlurPackage *package = (MotionBlurPackage*)get_package(i);
710 package->y1 = plugin->output->get_h() * i / get_total_packages();
711 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
715 LoadClient* MotionBlurEngine::new_client()
717 return new MotionBlurUnit(this, plugin);
720 LoadPackage* MotionBlurEngine::new_package()
722 return new MotionBlurPackage;