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()
250 int ys10 = yS(10), ys20 = yS(20), ys30 = yS(30);
251 int x = xs10, y = ys10;
253 add_subwindow(new BC_Title(x, y, _("Length:")));
255 add_subwindow(radius = new MotionBlurSize(plugin, x, y, &plugin->config.radius, 0, 100));
257 add_subwindow(new BC_Title(x, y, _("Steps:")));
259 add_subwindow(steps = new MotionBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
269 MotionBlurSize::MotionBlurSize(MotionBlurMain *plugin,
275 : BC_ISlider(x, y, 0, xS(240), yS(240), min, max, *output)
277 this->plugin = plugin;
278 this->output = output;
280 int MotionBlurSize::handle_event()
282 *output = get_value();
283 plugin->send_configure_change();
296 MotionBlurMain::MotionBlurMain(PluginServer *server)
297 : PluginVClient(server)
308 MotionBlurMain::~MotionBlurMain()
311 if(engine) delete engine;
313 if(accum) delete [] accum;
314 if(temp) delete temp;
317 const char* MotionBlurMain::plugin_title() { return N_("Motion Blur"); }
318 int MotionBlurMain::is_realtime() { return 1; }
321 NEW_WINDOW_MACRO(MotionBlurMain, MotionBlurWindow)
323 LOAD_CONFIGURATION_MACRO(MotionBlurMain, MotionBlurConfig)
325 void MotionBlurMain::delete_tables()
329 for(int i = 0; i < table_entries; i++)
330 delete [] scale_x_table[i];
331 delete [] scale_x_table;
336 for(int i = 0; i < table_entries; i++)
337 delete [] scale_y_table[i];
338 delete [] scale_y_table;
345 int MotionBlurMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
347 float xa,ya,za,xb,yb,zb,xd,yd,zd;
348 if (get_source_position() == 0)
349 get_camera(&xa, &ya, &za, get_source_position());
351 get_camera(&xa, &ya, &za, get_source_position()-1);
352 get_camera(&xb, &yb, &zb, get_source_position());
358 //printf("Camera automation deltas: %.2f %.2f %.2f\n", xd, yd, zd);
359 load_configuration();
361 //printf("MotionBlurMain::process_realtime 1 %d\n", config.radius);
362 if(!engine) engine = new MotionBlurEngine(this,
363 get_project_smp() + 1,
364 get_project_smp() + 1);
365 if(!accum) accum = new unsigned char[input_ptr->get_w() *
367 BC_CModels::components(input_ptr->get_color_model()) *
368 MAX(sizeof(int), sizeof(float))];
370 this->input = input_ptr;
371 this->output = output_ptr;
374 if(input_ptr->get_rows()[0] == output_ptr->get_rows()[0])
376 if(!temp) temp = new VFrame(input_ptr->get_w(), input_ptr->get_h(),
377 input_ptr->get_color_model(), 0);
378 temp->copy_from(input_ptr);
382 // Generate tables here. The same table is used by many packages to render
383 // each horizontal stripe. Need to cover the entire output range in each
384 // table to avoid green borders
385 float w = input->get_w();
386 float h = input->get_h();
390 float fradius = config.radius * 0.5;
391 float zradius = (float)(zd * fradius / 4 + 1);
392 float center_x = w/2;
393 float center_y = h/2;
396 //float max_w, max_h;
397 float min_x1, min_y1, min_x2, min_y2;
398 float max_x1, max_y1, max_x2, max_y2;
400 //int steps = config.steps ? config.steps : 1;
402 x_offset = (int)(xd * fradius);
403 y_offset = (int)(yd * fradius);
409 min_x1 = center_x - min_w / 2;
410 min_y1 = center_y - min_h / 2;
411 min_x2 = center_x + min_w / 2;
412 min_y2 = center_y + min_h / 2;
419 scale_x_table = new int*[config.steps];
420 scale_y_table = new int*[config.steps];
421 table_entries = config.steps;
423 for(int i = 0; i < config.steps; i++)
425 float fraction = (float)(i - config.steps / 2) / config.steps;
426 float inv_fraction = 1.0 - fraction;
428 int x = (int)(fraction * x_offset);
429 int y = (int)(fraction * y_offset);
430 float out_x1 = min_x1 * fraction + max_x1 * inv_fraction;
431 float out_x2 = min_x2 * fraction + max_x2 * inv_fraction;
432 float out_y1 = min_y1 * fraction + max_y1 * inv_fraction;
433 float out_y2 = min_y2 * fraction + max_y2 * inv_fraction;
434 float out_w = out_x2 - out_x1;
435 float out_h = out_y2 - out_y1;
436 if(out_w < 0) out_w = 0;
437 if(out_h < 0) out_h = 0;
438 float scale_x = (float)w / out_w;
439 float scale_y = (float)h / out_h;
443 scale_y_table[i] = y_table = new int[(int)(h + 1)];
444 scale_x_table[i] = x_table = new int[(int)(w + 1)];
446 for(int j = 0; j < h; j++)
448 y_table[j] = (int)((j - out_y1) * scale_y) + y;
450 for(int j = 0; j < w; j++)
452 x_table[j] = (int)((j - out_x1) * scale_x) + x;
459 BC_CModels::components(input_ptr->get_color_model()) *
460 MAX(sizeof(int), sizeof(float)));
461 engine->process_packages();
466 void MotionBlurMain::update_gui()
470 load_configuration();
471 thread->window->lock_window();
472 ((MotionBlurWindow*)thread->window)->radius->update(config.radius);
473 ((MotionBlurWindow*)thread->window)->steps->update(config.steps);
474 thread->window->unlock_window();
482 void MotionBlurMain::save_data(KeyFrame *keyframe)
486 // cause data to be stored directly in text
487 output.set_shared_output(keyframe->xbuf);
488 output.tag.set_title("MOTIONBLUR");
490 output.tag.set_property("RADIUS", config.radius);
491 output.tag.set_property("STEPS", config.steps);
493 output.tag.set_title("/MOTIONBLUR");
495 output.append_newline();
496 output.terminate_string();
499 void MotionBlurMain::read_data(KeyFrame *keyframe)
503 input.set_shared_input(keyframe->xbuf);
509 result = input.read_tag();
513 if(input.tag.title_is("MOTIONBLUR"))
515 config.radius = input.tag.get_property("RADIUS", config.radius);
516 config.steps = input.tag.get_property("STEPS", config.steps);
527 MotionBlurPackage::MotionBlurPackage()
535 MotionBlurUnit::MotionBlurUnit(MotionBlurEngine *server,
536 MotionBlurMain *plugin)
539 this->plugin = plugin;
540 this->server = server;
544 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
546 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
547 for(int j = pkg->y1; j < pkg->y2; j++) \
549 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
550 int in_y = y_table[j]; \
553 if(in_y >= 0 && in_y < h) \
555 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
556 for(int k = 0; k < w; k++) \
558 int in_x = x_table[k]; \
560 if(in_x >= 0 && in_x < w) \
562 int in_offset = in_x * COMPONENTS; \
563 *out_row++ += in_row[in_offset]; \
566 *out_row++ += in_row[in_offset + 1]; \
567 *out_row++ += in_row[in_offset + 2]; \
571 *out_row++ += in_row[in_offset + 1]; \
572 *out_row++ += in_row[in_offset + 2]; \
574 if(COMPONENTS == 4) \
575 *out_row++ += in_row[in_offset + 3]; \
577 /* Blend nothing */ \
583 *out_row++ += chroma_offset; \
584 *out_row++ += chroma_offset; \
590 if(COMPONENTS == 4) out_row++; \
597 for(int k = 0; k < w; k++) \
600 *out_row++ += chroma_offset; \
601 *out_row++ += chroma_offset; \
602 if(COMPONENTS == 4) out_row++; \
607 /* Copy to output */ \
608 if(i == plugin->config.steps - 1) \
610 for(int j = pkg->y1; j < pkg->y2; j++) \
612 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
613 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
614 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
615 for(int k = 0; k < w; k++) \
617 *out_row++ = (*in_row++ * fraction) / 0x10000; \
622 *out_row++ = (*in_row++ * fraction) / 0x10000; \
625 *out_row++ = (*in_row++ * fraction) / 0x10000; \
630 *out_row++ = (*in_row++ * fraction) / 0x10000; \
632 *out_row++ = (*in_row++ * fraction) / 0x10000; \
636 if(COMPONENTS == 4) \
638 *out_row++ = (*in_row++ * fraction) / 0x10000; \
646 void MotionBlurUnit::process_package(LoadPackage *package)
648 MotionBlurPackage *pkg = (MotionBlurPackage*)package;
649 int h = plugin->output->get_h();
650 int w = plugin->output->get_w();
652 int fraction = 0x10000 / plugin->config.steps;
653 for(int i = 0; i < plugin->config.steps; i++)
655 int *x_table = plugin->scale_x_table[i];
656 int *y_table = plugin->scale_y_table[i];
658 switch(plugin->input->get_color_model())
661 BLEND_LAYER(3, float, float, 1, 0)
664 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
667 BLEND_LAYER(4, float, float, 1, 0)
670 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
673 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
675 case BC_RGBA16161616:
676 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
679 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
682 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
685 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
687 case BC_YUVA16161616:
688 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
699 MotionBlurEngine::MotionBlurEngine(MotionBlurMain *plugin,
702 : LoadServer(total_clients, total_packages)
704 this->plugin = plugin;
707 void MotionBlurEngine::init_packages()
709 for(int i = 0; i < get_total_packages(); i++)
711 MotionBlurPackage *package = (MotionBlurPackage*)get_package(i);
712 package->y1 = plugin->output->get_h() * i / get_total_packages();
713 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
717 LoadClient* MotionBlurEngine::new_client()
719 return new MotionBlurUnit(this, plugin);
722 LoadPackage* MotionBlurEngine::new_package()
724 return new MotionBlurPackage;