3 #include "denoisevideo.h"
21 REGISTER_PLUGIN(DenoiseVideo)
28 DenoiseVideoConfig::DenoiseVideoConfig()
38 int DenoiseVideoConfig::equivalent(DenoiseVideoConfig &that)
40 return frames == that.frames &&
41 EQUIV(threshold, that.threshold) &&
48 void DenoiseVideoConfig::copy_from(DenoiseVideoConfig &that)
51 threshold = that.threshold;
58 void DenoiseVideoConfig::interpolate(DenoiseVideoConfig &prev,
59 DenoiseVideoConfig &next,
64 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
65 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
67 this->frames = (int)(prev.frames * prev_scale + next.frames * next_scale);
68 this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
80 DenoiseVideoFrames::DenoiseVideoFrames(DenoiseVideo *plugin, int x, int y)
88 plugin->config.frames)
90 this->plugin = plugin;
93 int DenoiseVideoFrames::handle_event()
95 int result = get_value();
96 if(result < 1 || result > 256) result = 256;
97 plugin->config.frames = result;
98 plugin->send_configure_change();
108 DenoiseVideoThreshold::DenoiseVideoThreshold(DenoiseVideo *plugin, int x, int y)
109 : BC_TextBox(x, y, 100, 1, plugin->config.threshold)
111 this->plugin = plugin;
114 int DenoiseVideoThreshold::handle_event()
116 plugin->config.threshold = atof(get_text());
117 plugin->send_configure_change();
125 DenoiseVideoToggle::DenoiseVideoToggle(DenoiseVideo *plugin,
126 DenoiseVideoWindow *gui,
131 : BC_CheckBox(x, y, *output, text)
133 this->plugin = plugin;
134 this->output = output;
137 int DenoiseVideoToggle::handle_event()
139 *output = get_value();
140 plugin->send_configure_change();
152 DenoiseVideoWindow::DenoiseVideoWindow(DenoiseVideo *plugin, int x, int y)
153 : BC_Window(plugin->gui_string,
164 this->plugin = plugin;
168 void DenoiseVideoWindow::create_objects()
171 add_subwindow(new BC_Title(x, y, _("Frames to accumulate:")));
173 add_subwindow(frames = new DenoiseVideoFrames(plugin, x, y));
175 add_subwindow(new BC_Title(x, y, _("Threshold:")));
177 add_subwindow(threshold = new DenoiseVideoThreshold(plugin, x, y));
179 add_subwindow(do_r = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_r, _("Red")));
181 add_subwindow(do_g = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_g, _("Green")));
183 add_subwindow(do_b = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_b, _("Blue")));
185 add_subwindow(do_a = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_a, _("Alpha")));
190 int DenoiseVideoWindow::close_event()
201 PLUGIN_THREAD_OBJECT(DenoiseVideo, DenoiseVideoThread, DenoiseVideoWindow)
213 DenoiseVideo::DenoiseVideo(PluginServer *server)
214 : PluginVClient(server)
216 PLUGIN_CONSTRUCTOR_MACRO
221 DenoiseVideo::~DenoiseVideo()
223 PLUGIN_DESTRUCTOR_MACRO
225 if(accumulation) delete [] accumulation;
228 int DenoiseVideo::process_realtime(VFrame *input, VFrame *output)
230 load_configuration();
232 int h = input->get_h();
233 int w = input->get_w();
234 int color_model = input->get_color_model();
238 accumulation = new float[w * h * cmodel_components(color_model)];
239 bzero(accumulation, sizeof(float) * w * h * cmodel_components(color_model));
242 float *accumulation_ptr = accumulation;
243 float opacity = (float)1.0 / config.frames;
244 float transparency = 1 - opacity;
245 float threshold = (float)config.threshold *
246 cmodel_calculate_max(color_model);
247 int do_it[4] = { config.do_r, config.do_g, config.do_b, config.do_a };
249 #define DENOISE_MACRO(type, components, max) \
251 for(int i = 0; i < h; i++) \
253 type *output_row = (type*)output->get_rows()[i]; \
254 type *input_row = (type*)input->get_rows()[i]; \
256 for(int k = 0; k < w * components; k++) \
258 if(do_it[k % components]) \
260 float input_pixel = *input_row; \
261 (*accumulation_ptr) = \
262 transparency * (*accumulation_ptr) + \
263 opacity * input_pixel; \
265 if(fabs((*accumulation_ptr) - input_pixel) > threshold) \
267 (*accumulation_ptr) = input_pixel; \
268 *output_row = (type)(*accumulation_ptr); \
271 if(sizeof(type) < 4) \
272 *output_row = (type)CLIP((*accumulation_ptr), 0, max); \
276 *output_row = *input_row; \
281 accumulation_ptr++; \
295 DENOISE_MACRO(unsigned char, 3, 0xff);
299 DENOISE_MACRO(float, 3, 1.0);
304 DENOISE_MACRO(unsigned char, 4, 0xff);
308 DENOISE_MACRO(float, 4, 1.0);
313 DENOISE_MACRO(uint16_t, 3, 0xffff);
316 case BC_RGBA16161616:
317 case BC_YUVA16161616:
318 DENOISE_MACRO(uint16_t, 4, 0xffff);
324 char* DenoiseVideo::plugin_title() { return N_("Denoise video"); }
325 int DenoiseVideo::is_realtime() { return 1; }
328 NEW_PICON_MACRO(DenoiseVideo)
330 SHOW_GUI_MACRO(DenoiseVideo, DenoiseVideoThread)
332 RAISE_WINDOW_MACRO(DenoiseVideo)
334 SET_STRING_MACRO(DenoiseVideo);
336 LOAD_CONFIGURATION_MACRO(DenoiseVideo, DenoiseVideoConfig)
338 void DenoiseVideo::update_gui()
342 load_configuration();
343 thread->window->lock_window();
344 thread->window->frames->update(config.frames);
345 thread->window->threshold->update(config.threshold);
346 thread->window->unlock_window();
352 int DenoiseVideo::load_defaults()
354 char directory[BCTEXTLEN];
355 // set the default directory
356 sprintf(directory, "%sdenoisevideo.rc", BCASTDIR);
359 defaults = new BC_Hash(directory);
362 config.frames = defaults->get("FRAMES", config.frames);
363 config.threshold = defaults->get("THRESHOLD", config.threshold);
364 config.do_r = defaults->get("DO_R", config.do_r);
365 config.do_g = defaults->get("DO_G", config.do_g);
366 config.do_b = defaults->get("DO_B", config.do_b);
367 config.do_a = defaults->get("DO_A", config.do_a);
371 int DenoiseVideo::save_defaults()
373 defaults->update("THRESHOLD", config.threshold);
374 defaults->update("FRAMES", config.frames);
375 defaults->update("DO_R", config.do_r);
376 defaults->update("DO_G", config.do_g);
377 defaults->update("DO_B", config.do_b);
378 defaults->update("DO_A", config.do_a);
383 void DenoiseVideo::save_data(KeyFrame *keyframe)
387 // cause data to be stored directly in text
388 output.set_shared_string(keyframe->data, MESSAGESIZE);
389 output.tag.set_title("DENOISE_VIDEO");
390 output.tag.set_property("FRAMES", config.frames);
391 output.tag.set_property("THRESHOLD", config.threshold);
392 output.tag.set_property("DO_R", config.do_r);
393 output.tag.set_property("DO_G", config.do_g);
394 output.tag.set_property("DO_B", config.do_b);
395 output.tag.set_property("DO_A", config.do_a);
397 output.terminate_string();
400 void DenoiseVideo::read_data(KeyFrame *keyframe)
404 input.set_shared_string(keyframe->data, strlen(keyframe->data));
408 while(!input.read_tag())
410 if(input.tag.title_is("DENOISE_VIDEO"))
412 config.frames = input.tag.get_property("FRAMES", config.frames);
413 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
414 config.do_r = input.tag.get_property("DO_R", config.do_r);
415 config.do_g = input.tag.get_property("DO_G", config.do_g);
416 config.do_b = input.tag.get_property("DO_B", config.do_b);
417 config.do_a = input.tag.get_property("DO_A", config.do_a);