r864: Merge 2.1:
[cinelerra_cv.git] / plugins / denoisevideo / denoisevideo.C
blob9f3192d63736873695c0852799f0d12247b957f0
1 #include "clip.h"
2 #include "bchash.h"
3 #include "denoisevideo.h"
4 #include "filexml.h"
5 #include "guicast.h"
6 #include "keyframe.h"
7 #include "language.h"
8 #include "picon_png.h"
9 #include "vframe.h"
15 #include <stdint.h>
16 #include <string.h>
21 REGISTER_PLUGIN(DenoiseVideo)
28 DenoiseVideoConfig::DenoiseVideoConfig()
30         frames = 2;
31         threshold = 0.1;
32         do_r = 1;
33         do_g = 1;
34         do_b = 1;
35         do_a = 1;
38 int DenoiseVideoConfig::equivalent(DenoiseVideoConfig &that)
40         return frames == that.frames && 
41                 EQUIV(threshold, that.threshold) &&
42                 do_r == that.do_r &&
43                 do_g == that.do_g &&
44                 do_b == that.do_b &&
45                 do_a == that.do_a;
48 void DenoiseVideoConfig::copy_from(DenoiseVideoConfig &that)
50         frames = that.frames;
51         threshold = that.threshold;
52         do_r = that.do_r;
53         do_g = that.do_g;
54         do_b = that.do_b;
55         do_a = that.do_a;
58 void DenoiseVideoConfig::interpolate(DenoiseVideoConfig &prev, 
59         DenoiseVideoConfig &next, 
60         long prev_frame, 
61         long next_frame, 
62         long current_frame)
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;
69         do_r = prev.do_r;
70         do_g = prev.do_g;
71         do_b = prev.do_b;
72         do_a = prev.do_a;
80 DenoiseVideoFrames::DenoiseVideoFrames(DenoiseVideo *plugin, int x, int y)
81  : BC_ISlider(x, 
82         y, 
83         0,
84         190, 
85         200, 
86         1, 
87         256, 
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();
99         return 1;
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();
118         return 1;
125 DenoiseVideoToggle::DenoiseVideoToggle(DenoiseVideo *plugin, 
126         DenoiseVideoWindow *gui, 
127         int x, 
128         int y, 
129         int *output,
130         char *text)
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, 
154         x, 
155         y, 
156         210, 
157         240, 
158         200, 
159         240, 
160         0, 
161         0,
162         1)
164         this->plugin = plugin;
168 void DenoiseVideoWindow::create_objects()
170         int x = 10, y = 10;
171         add_subwindow(new BC_Title(x, y, _("Frames to accumulate:")));
172         y += 20;
173         add_subwindow(frames = new DenoiseVideoFrames(plugin, x, y));
174         y += 30;
175         add_subwindow(new BC_Title(x, y, _("Threshold:")));
176         y += 20;
177         add_subwindow(threshold = new DenoiseVideoThreshold(plugin, x, y));
178         y += 40;
179         add_subwindow(do_r = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_r, _("Red")));
180         y += 30;
181         add_subwindow(do_g = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_g, _("Green")));
182         y += 30;
183         add_subwindow(do_b = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_b, _("Blue")));
184         y += 30;
185         add_subwindow(do_a = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_a, _("Alpha")));
186         show_window();
187         flush();
190 int DenoiseVideoWindow::close_event()
192         set_done(1);
193         return 1;
201 PLUGIN_THREAD_OBJECT(DenoiseVideo, DenoiseVideoThread, DenoiseVideoWindow)
213 DenoiseVideo::DenoiseVideo(PluginServer *server)
214  : PluginVClient(server)
216         PLUGIN_CONSTRUCTOR_MACRO
217         accumulation = 0;
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();
236         if(!accumulation)
237         {
238                 accumulation = new float[w * h * cmodel_components(color_model)];
239                 bzero(accumulation, sizeof(float) * w * h * cmodel_components(color_model));
240         }
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) \
250 { \
251         for(int i = 0; i < h; i++) \
252         { \
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++) \
257                 { \
258                         if(do_it[k % components]) \
259                         { \
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) \
266                                 { \
267                                         (*accumulation_ptr) = input_pixel; \
268                                         *output_row = (type)(*accumulation_ptr); \
269                                 } \
270                                 else \
271                                 if(sizeof(type) < 4) \
272                                         *output_row = (type)CLIP((*accumulation_ptr), 0, max); \
273                         } \
274                         else \
275                         { \
276                                 *output_row = *input_row; \
277                         } \
279                         output_row++; \
280                         input_row++; \
281                         accumulation_ptr++; \
282                 } \
283         } \
291         switch(color_model)
292         {
293                 case BC_RGB888:
294                 case BC_YUV888:
295                         DENOISE_MACRO(unsigned char, 3, 0xff);
296                         break;
298                 case BC_RGB_FLOAT:
299                         DENOISE_MACRO(float, 3, 1.0);
300                         break;
302                 case BC_RGBA8888:
303                 case BC_YUVA8888:
304                         DENOISE_MACRO(unsigned char, 4, 0xff);
305                         break;
307                 case BC_RGBA_FLOAT:
308                         DENOISE_MACRO(float, 4, 1.0);
309                         break;
311                 case BC_RGB161616:
312                 case BC_YUV161616:
313                         DENOISE_MACRO(uint16_t, 3, 0xffff);
314                         break;
316                 case BC_RGBA16161616:
317                 case BC_YUVA16161616:
318                         DENOISE_MACRO(uint16_t, 4, 0xffff);
319                         break;
320         }
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()
340         if(thread)
341         {
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();
347         }
352 int DenoiseVideo::load_defaults()
354         char directory[BCTEXTLEN];
355 // set the default directory
356         sprintf(directory, "%sdenoisevideo.rc", BCASTDIR);
358 // load the defaults
359         defaults = new BC_Hash(directory);
360         defaults->load();
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);
368         return 0;
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);
379         defaults->save();
380         return 0;
383 void DenoiseVideo::save_data(KeyFrame *keyframe)
385         FileXML output;
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);
396         output.append_tag();
397         output.terminate_string();
400 void DenoiseVideo::read_data(KeyFrame *keyframe)
402         FileXML input;
404         input.set_shared_string(keyframe->data, strlen(keyframe->data));
406         int result = 0;
408         while(!input.read_tag())
409         {
410                 if(input.tag.title_is("DENOISE_VIDEO"))
411                 {
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);
418                 }
419         }