add BC_SCALE env var for hi def monitors, cleanup theme data
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / denoisevideo / denoisevideo.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "clip.h"
23 #include "bchash.h"
24 #include "denoisevideo.h"
25 #include "filexml.h"
26 #include "guicast.h"
27 #include "keyframe.h"
28 #include "language.h"
29 #include "vframe.h"
30
31
32
33
34
35 #include <stdint.h>
36 #include <string.h>
37
38
39
40
41 REGISTER_PLUGIN(DenoiseVideo)
42
43
44
45
46
47
48 DenoiseVideoConfig::DenoiseVideoConfig()
49 {
50         frames = 2;
51         threshold = 0.1;
52         count_changed = 0;
53         do_r = 1;
54         do_g = 1;
55         do_b = 1;
56         do_a = 1;
57 }
58
59 int DenoiseVideoConfig::equivalent(DenoiseVideoConfig &that)
60 {
61         return frames == that.frames &&
62                 EQUIV(threshold, that.threshold) &&
63                 do_r == that.do_r &&
64                 do_g == that.do_g &&
65                 do_b == that.do_b &&
66                 do_a == that.do_a &&
67                 count_changed == that.count_changed;
68 }
69
70 void DenoiseVideoConfig::copy_from(DenoiseVideoConfig &that)
71 {
72         frames = that.frames;
73         threshold = that.threshold;
74         count_changed = that.count_changed;
75         do_r = that.do_r;
76         do_g = that.do_g;
77         do_b = that.do_b;
78         do_a = that.do_a;
79 }
80
81 void DenoiseVideoConfig::interpolate(DenoiseVideoConfig &prev,
82         DenoiseVideoConfig &next,
83         long prev_frame,
84         long next_frame,
85         long current_frame)
86 {
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);
89
90         this->frames = (int)(prev.frames * prev_scale + next.frames * next_scale);
91         this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
92         do_r = prev.do_r;
93         do_g = prev.do_g;
94         do_b = prev.do_b;
95         do_a = prev.do_a;
96         count_changed = prev.count_changed;
97 }
98
99
100
101
102
103
104 DenoiseVideoFrames::DenoiseVideoFrames(DenoiseVideo *plugin, int x, int y)
105  : BC_ISlider(x,
106         y,
107         0,
108         xS(190),
109         yS(200),
110         1,
111         256,
112         plugin->config.frames)
113 {
114         this->plugin = plugin;
115 }
116
117 int DenoiseVideoFrames::handle_event()
118 {
119         int result = get_value();
120         if(result < 1 || result > 256) result = 256;
121         plugin->config.frames = result;
122         plugin->send_configure_change();
123         return 1;
124 }
125
126
127
128
129
130
131
132 DenoiseVideoThreshold::DenoiseVideoThreshold(DenoiseVideo *plugin,
133         DenoiseVideoWindow *gui,
134         int x,
135         int y)
136  : BC_TumbleTextBox(gui,
137         plugin->config.threshold,
138         (float)0,
139         (float)1,
140         x,
141         y,
142         100)
143 {
144         this->plugin = plugin;
145         set_precision(3);
146         set_increment(0.1);
147 }
148
149 int DenoiseVideoThreshold::handle_event()
150 {
151         plugin->config.threshold = atof(get_text());
152         plugin->send_configure_change();
153         return 1;
154 }
155
156
157
158
159
160 DenoiseVideoToggle::DenoiseVideoToggle(DenoiseVideo *plugin,
161         DenoiseVideoWindow *gui,
162         int x,
163         int y,
164         int *output,
165         char *text)
166  : BC_CheckBox(x, y, *output, text)
167 {
168         this->plugin = plugin;
169         this->output = output;
170 }
171
172 int DenoiseVideoToggle::handle_event()
173 {
174         *output = get_value();
175         plugin->send_configure_change();
176         return 1;
177 }
178
179
180 DenoiseVideoCountChanged::DenoiseVideoCountChanged(DenoiseVideo *plugin,
181         DenoiseVideoWindow *gui,
182         int x,
183         int y)
184  : BC_Radial(x,
185         y,
186         plugin->config.count_changed,
187         _("Average changing pixels"))
188 {
189         this->plugin = plugin;
190         this->gui = gui;
191 }
192
193 int DenoiseVideoCountChanged::handle_event()
194 {
195         plugin->config.count_changed = 1;
196         gui->count_same->update(0);
197         plugin->send_configure_change();
198         return 1;
199 }
200
201
202
203
204
205 DenoiseVideoCountSame::DenoiseVideoCountSame(DenoiseVideo *plugin,
206         DenoiseVideoWindow *gui,
207         int x,
208         int y)
209  : BC_Radial(x,
210         y,
211         !plugin->config.count_changed,
212         _("Average similar pixels"))
213 {
214         this->plugin = plugin;
215         this->gui = gui;
216 }
217
218 int DenoiseVideoCountSame::handle_event()
219 {
220         plugin->config.count_changed = 0;
221         gui->count_changed->update(0);
222         plugin->send_configure_change();
223         return 1;
224 }
225
226
227
228
229
230
231
232
233
234
235 DenoiseVideoWindow::DenoiseVideoWindow(DenoiseVideo *plugin)
236  : PluginClientWindow(plugin,
237         xS(250),
238         yS(300),
239         xS(250),
240         yS(300),
241         0)
242 {
243         this->plugin = plugin;
244 }
245
246
247 void DenoiseVideoWindow::create_objects()
248 {
249         int xs10 = xS(10);
250         int ys5 = yS(5), ys10 = yS(10), ys30 = yS(30);
251         int x = xs10, y = ys10;
252         BC_Title *title;
253         BC_Bar *bar;
254         add_subwindow(new BC_Title(x, y, _("Frames to accumulate:")));
255         y += yS(20);
256         add_subwindow(frames = new DenoiseVideoFrames(plugin, x, y));
257         y += frames->get_h() + ys5;
258         add_subwindow(title = new BC_Title(x, y, _("Threshold:")));
259         y += title->get_h() + ys5;
260         threshold = new DenoiseVideoThreshold(plugin, this, x, y);
261         threshold->create_objects();
262         y += threshold->get_h() + ys5;
263         add_subwindow(bar = new BC_Bar(x, y, get_w() - x * 2));
264         y += bar->get_h() + ys5;
265         add_subwindow(count_changed = new DenoiseVideoCountChanged(plugin,
266                 this,
267                 x,
268                 y));
269         y += count_changed->get_h() + ys5;
270         add_subwindow(count_same = new DenoiseVideoCountSame(plugin,
271                 this,
272                 x,
273                 y));
274         y += count_same->get_h() + ys5;
275         add_subwindow(bar = new BC_Bar(x, y, get_w() - x * 2));
276         y += bar->get_h() + ys5;
277         add_subwindow(do_r = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_r, _("Red")));
278         y += ys30;
279         add_subwindow(do_g = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_g, _("Green")));
280         y += ys30;
281         add_subwindow(do_b = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_b, _("Blue")));
282         y += ys30;
283         add_subwindow(do_a = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_a, _("Alpha")));
284         show_window();
285         flush();
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 DenoiseVideo::DenoiseVideo(PluginServer *server)
303  : PluginVClient(server)
304 {
305
306         accumulation = 0;
307 }
308
309
310 DenoiseVideo::~DenoiseVideo()
311 {
312
313
314         if(accumulation) delete [] accumulation;
315 }
316
317 int DenoiseVideo::process_realtime(VFrame *input, VFrame *output)
318 {
319         load_configuration();
320
321         int h = input->get_h();
322         int w = input->get_w();
323         int color_model = input->get_color_model();
324
325         if(!accumulation)
326         {
327                 accumulation = new float[w * h * BC_CModels::components(color_model)];
328                 bzero(accumulation, sizeof(float) * w * h * BC_CModels::components(color_model));
329         }
330
331         float *accumulation_ptr = accumulation;
332         float opacity = (float)1.0 / config.frames;
333         float transparency = 1 - opacity;
334         float threshold = (float)config.threshold *
335                 BC_CModels::calculate_max(color_model);
336         int do_it[4] = { config.do_r, config.do_g, config.do_b, config.do_a };
337
338 #define DENOISE_MACRO(type, components, max) \
339 { \
340         for(int i = 0; i < h; i++) \
341         { \
342                 type *output_row = (type*)output->get_rows()[i]; \
343                 type *input_row = (type*)input->get_rows()[i]; \
344  \
345                 for(int k = 0; k < w * components; k++) \
346                 { \
347                         if( do_it[k % components] ) \
348                         { \
349                                 float input_pixel = *input_row; \
350                                 (*accumulation_ptr) = \
351                                         transparency * (*accumulation_ptr) + \
352                                         opacity * input_pixel; \
353  \
354                                 float difference = fabs((*accumulation_ptr) - input_pixel); \
355                                 if( !config.count_changed ? \
356                                          difference > threshold : difference < threshold) \
357                                 { \
358                                         (*accumulation_ptr) = input_pixel; \
359                                         *output_row = (type)(*accumulation_ptr); \
360                                 } \
361                                 else \
362                                 if(sizeof(type) < 4) \
363                                         *output_row = (type)CLIP((*accumulation_ptr), 0, max); \
364                         } \
365                         else \
366                         { \
367                                 *output_row = *input_row; \
368                         } \
369  \
370                         output_row++; \
371                         input_row++; \
372                         accumulation_ptr++; \
373                 } \
374         } \
375 }
376
377
378
379
380
381
382         switch(color_model)
383         {
384                 case BC_RGB888:
385                 case BC_YUV888:
386                         DENOISE_MACRO(unsigned char, 3, 0xff);
387                         break;
388
389                 case BC_RGB_FLOAT:
390                         DENOISE_MACRO(float, 3, 1.0);
391                         break;
392
393                 case BC_RGBA8888:
394                 case BC_YUVA8888:
395                         DENOISE_MACRO(unsigned char, 4, 0xff);
396                         break;
397
398                 case BC_RGBA_FLOAT:
399                         DENOISE_MACRO(float, 4, 1.0);
400                         break;
401
402                 case BC_RGB161616:
403                 case BC_YUV161616:
404                         DENOISE_MACRO(uint16_t, 3, 0xffff);
405                         break;
406
407                 case BC_RGBA16161616:
408                 case BC_YUVA16161616:
409                         DENOISE_MACRO(uint16_t, 4, 0xffff);
410                         break;
411         }
412         return 0;
413 }
414
415
416 const char* DenoiseVideo::plugin_title() { return N_("Denoise video"); }
417 int DenoiseVideo::is_realtime() { return 1; }
418
419
420 NEW_WINDOW_MACRO(DenoiseVideo, DenoiseVideoWindow)
421
422 LOAD_CONFIGURATION_MACRO(DenoiseVideo, DenoiseVideoConfig)
423
424 void DenoiseVideo::update_gui()
425 {
426         if(thread)
427         {
428                 load_configuration();
429                 ((DenoiseVideoWindow*)thread->window)->lock_window();
430                 ((DenoiseVideoWindow*)thread->window)->frames->update(config.frames);
431                 ((DenoiseVideoWindow*)thread->window)->threshold->update(config.threshold);
432                 ((DenoiseVideoWindow*)thread->window)->count_changed->update(config.count_changed);
433                 ((DenoiseVideoWindow*)thread->window)->count_same->update(!config.count_changed);
434                 ((DenoiseVideoWindow*)thread->window)->unlock_window();
435         }
436 }
437
438
439
440
441 void DenoiseVideo::save_data(KeyFrame *keyframe)
442 {
443         FileXML output;
444
445 // cause data to be stored directly in text
446         output.set_shared_output(keyframe->xbuf);
447         output.tag.set_title("DENOISE_VIDEO");
448         output.tag.set_property("FRAMES", config.frames);
449         output.tag.set_property("THRESHOLD", config.threshold);
450         output.tag.set_property("DO_R", config.do_r);
451         output.tag.set_property("DO_G", config.do_g);
452         output.tag.set_property("DO_B", config.do_b);
453         output.tag.set_property("DO_A", config.do_a);
454         output.tag.set_property("COUNT_CHANGED", config.count_changed);
455         output.append_tag();
456         output.tag.set_title("/DENOISE_VIDEO");
457         output.append_tag();
458         output.append_newline();
459         output.terminate_string();
460 }
461
462 void DenoiseVideo::read_data(KeyFrame *keyframe)
463 {
464         FileXML input;
465
466         input.set_shared_input(keyframe->xbuf);
467
468         while(!input.read_tag())
469         {
470                 if(input.tag.title_is("DENOISE_VIDEO"))
471                 {
472                         config.frames = input.tag.get_property("FRAMES", config.frames);
473                         config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
474                         config.do_r = input.tag.get_property("DO_R", config.do_r);
475                         config.do_g = input.tag.get_property("DO_G", config.do_g);
476                         config.do_b = input.tag.get_property("DO_B", config.do_b);
477                         config.do_a = input.tag.get_property("DO_A", config.do_a);
478                         config.count_changed = input.tag.get_property("COUNT_CHANGED", config.count_changed);
479                 }
480         }
481 }
482