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
24 #include "denoisevideo.h"
41 REGISTER_PLUGIN(DenoiseVideo)
48 DenoiseVideoConfig::DenoiseVideoConfig()
59 int DenoiseVideoConfig::equivalent(DenoiseVideoConfig &that)
61 return frames == that.frames &&
62 EQUIV(threshold, that.threshold) &&
67 count_changed == that.count_changed;
70 void DenoiseVideoConfig::copy_from(DenoiseVideoConfig &that)
73 threshold = that.threshold;
74 count_changed = that.count_changed;
81 void DenoiseVideoConfig::interpolate(DenoiseVideoConfig &prev,
82 DenoiseVideoConfig &next,
87 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
88 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
90 this->frames = (int)(prev.frames * prev_scale + next.frames * next_scale);
91 this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
96 count_changed = prev.count_changed;
104 DenoiseVideoFrames::DenoiseVideoFrames(DenoiseVideo *plugin, int x, int y)
112 plugin->config.frames)
114 this->plugin = plugin;
117 int DenoiseVideoFrames::handle_event()
119 int result = get_value();
120 if(result < 1 || result > 256) result = 256;
121 plugin->config.frames = result;
122 plugin->send_configure_change();
132 DenoiseVideoThreshold::DenoiseVideoThreshold(DenoiseVideo *plugin,
133 DenoiseVideoWindow *gui,
136 : BC_TumbleTextBox(gui,
137 plugin->config.threshold,
144 this->plugin = plugin;
149 int DenoiseVideoThreshold::handle_event()
151 plugin->config.threshold = atof(get_text());
152 plugin->send_configure_change();
160 DenoiseVideoToggle::DenoiseVideoToggle(DenoiseVideo *plugin,
161 DenoiseVideoWindow *gui,
166 : BC_CheckBox(x, y, *output, text)
168 this->plugin = plugin;
169 this->output = output;
172 int DenoiseVideoToggle::handle_event()
174 *output = get_value();
175 plugin->send_configure_change();
180 DenoiseVideoCountChanged::DenoiseVideoCountChanged(DenoiseVideo *plugin,
181 DenoiseVideoWindow *gui,
186 plugin->config.count_changed,
187 _("Average changing pixels"))
189 this->plugin = plugin;
193 int DenoiseVideoCountChanged::handle_event()
195 plugin->config.count_changed = 1;
196 gui->count_same->update(0);
197 plugin->send_configure_change();
205 DenoiseVideoCountSame::DenoiseVideoCountSame(DenoiseVideo *plugin,
206 DenoiseVideoWindow *gui,
211 !plugin->config.count_changed,
212 _("Average similar pixels"))
214 this->plugin = plugin;
218 int DenoiseVideoCountSame::handle_event()
220 plugin->config.count_changed = 0;
221 gui->count_changed->update(0);
222 plugin->send_configure_change();
235 DenoiseVideoWindow::DenoiseVideoWindow(DenoiseVideo *plugin)
236 : PluginClientWindow(plugin,
243 this->plugin = plugin;
247 void DenoiseVideoWindow::create_objects()
252 add_subwindow(new BC_Title(x, y, _("Frames to accumulate:")));
254 add_subwindow(frames = new DenoiseVideoFrames(plugin, x, y));
255 y += frames->get_h() + 5;
256 add_subwindow(title = new BC_Title(x, y, _("Threshold:")));
257 y += title->get_h() + 5;
258 threshold = new DenoiseVideoThreshold(plugin, this, x, y);
259 threshold->create_objects();
260 y += threshold->get_h() + 5;
261 add_subwindow(bar = new BC_Bar(x, y, get_w() - x * 2));
262 y += bar->get_h() + 5;
263 add_subwindow(count_changed = new DenoiseVideoCountChanged(plugin,
267 y += count_changed->get_h() + 5;
268 add_subwindow(count_same = new DenoiseVideoCountSame(plugin,
272 y += count_same->get_h() + 5;
273 add_subwindow(bar = new BC_Bar(x, y, get_w() - x * 2));
274 y += bar->get_h() + 5;
275 add_subwindow(do_r = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_r, _("Red")));
277 add_subwindow(do_g = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_g, _("Green")));
279 add_subwindow(do_b = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_b, _("Blue")));
281 add_subwindow(do_a = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_a, _("Alpha")));
300 DenoiseVideo::DenoiseVideo(PluginServer *server)
301 : PluginVClient(server)
308 DenoiseVideo::~DenoiseVideo()
312 if(accumulation) delete [] accumulation;
315 int DenoiseVideo::process_realtime(VFrame *input, VFrame *output)
317 load_configuration();
319 int h = input->get_h();
320 int w = input->get_w();
321 int color_model = input->get_color_model();
325 accumulation = new float[w * h * BC_CModels::components(color_model)];
326 bzero(accumulation, sizeof(float) * w * h * BC_CModels::components(color_model));
329 float *accumulation_ptr = accumulation;
330 float opacity = (float)1.0 / config.frames;
331 float transparency = 1 - opacity;
332 float threshold = (float)config.threshold *
333 BC_CModels::calculate_max(color_model);
334 int do_it[4] = { config.do_r, config.do_g, config.do_b, config.do_a };
336 #define DENOISE_MACRO(type, components, max) \
338 for(int i = 0; i < h; i++) \
340 type *output_row = (type*)output->get_rows()[i]; \
341 type *input_row = (type*)input->get_rows()[i]; \
343 for(int k = 0; k < w * components; k++) \
345 if( do_it[k % components] ) \
347 float input_pixel = *input_row; \
348 (*accumulation_ptr) = \
349 transparency * (*accumulation_ptr) + \
350 opacity * input_pixel; \
352 float difference = fabs((*accumulation_ptr) - input_pixel); \
353 if( !config.count_changed ? \
354 difference > threshold : difference < threshold) \
356 (*accumulation_ptr) = input_pixel; \
357 *output_row = (type)(*accumulation_ptr); \
360 if(sizeof(type) < 4) \
361 *output_row = (type)CLIP((*accumulation_ptr), 0, max); \
365 *output_row = *input_row; \
370 accumulation_ptr++; \
384 DENOISE_MACRO(unsigned char, 3, 0xff);
388 DENOISE_MACRO(float, 3, 1.0);
393 DENOISE_MACRO(unsigned char, 4, 0xff);
397 DENOISE_MACRO(float, 4, 1.0);
402 DENOISE_MACRO(uint16_t, 3, 0xffff);
405 case BC_RGBA16161616:
406 case BC_YUVA16161616:
407 DENOISE_MACRO(uint16_t, 4, 0xffff);
414 const char* DenoiseVideo::plugin_title() { return _("Denoise video"); }
415 int DenoiseVideo::is_realtime() { return 1; }
418 NEW_WINDOW_MACRO(DenoiseVideo, DenoiseVideoWindow)
420 LOAD_CONFIGURATION_MACRO(DenoiseVideo, DenoiseVideoConfig)
422 void DenoiseVideo::update_gui()
426 load_configuration();
427 ((DenoiseVideoWindow*)thread->window)->lock_window();
428 ((DenoiseVideoWindow*)thread->window)->frames->update(config.frames);
429 ((DenoiseVideoWindow*)thread->window)->threshold->update(config.threshold);
430 ((DenoiseVideoWindow*)thread->window)->count_changed->update(config.count_changed);
431 ((DenoiseVideoWindow*)thread->window)->count_same->update(!config.count_changed);
432 ((DenoiseVideoWindow*)thread->window)->unlock_window();
439 void DenoiseVideo::save_data(KeyFrame *keyframe)
443 // cause data to be stored directly in text
444 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
445 output.tag.set_title("DENOISE_VIDEO");
446 output.tag.set_property("FRAMES", config.frames);
447 output.tag.set_property("THRESHOLD", config.threshold);
448 output.tag.set_property("DO_R", config.do_r);
449 output.tag.set_property("DO_G", config.do_g);
450 output.tag.set_property("DO_B", config.do_b);
451 output.tag.set_property("DO_A", config.do_a);
452 output.tag.set_property("COUNT_CHANGED", config.count_changed);
454 output.tag.set_title("/DENOISE_VIDEO");
456 output.append_newline();
457 output.terminate_string();
460 void DenoiseVideo::read_data(KeyFrame *keyframe)
464 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
466 while(!input.read_tag())
468 if(input.tag.title_is("DENOISE_VIDEO"))
470 config.frames = input.tag.get_property("FRAMES", config.frames);
471 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
472 config.do_r = input.tag.get_property("DO_R", config.do_r);
473 config.do_g = input.tag.get_property("DO_G", config.do_g);
474 config.do_b = input.tag.get_property("DO_B", config.do_b);
475 config.do_a = input.tag.get_property("DO_A", config.do_a);
476 config.count_changed = input.tag.get_property("COUNT_CHANGED", config.count_changed);