igor ru.po
[goodguy/history.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         190,
109         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         250,
238         300,
239         250,
240         300,
241         0)
242 {
243         this->plugin = plugin;
244 }
245
246
247 void DenoiseVideoWindow::create_objects()
248 {
249         int x = 10, y = 10;
250         BC_Title *title;
251         BC_Bar *bar;
252         add_subwindow(new BC_Title(x, y, _("Frames to accumulate:")));
253         y += 20;
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,
264                 this,
265                 x,
266                 y));
267         y += count_changed->get_h() + 5;
268         add_subwindow(count_same = new DenoiseVideoCountSame(plugin,
269                 this,
270                 x,
271                 y));
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")));
276         y += 30;
277         add_subwindow(do_g = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_g, _("Green")));
278         y += 30;
279         add_subwindow(do_b = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_b, _("Blue")));
280         y += 30;
281         add_subwindow(do_a = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_a, _("Alpha")));
282         show_window();
283         flush();
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300 DenoiseVideo::DenoiseVideo(PluginServer *server)
301  : PluginVClient(server)
302 {
303
304         accumulation = 0;
305 }
306
307
308 DenoiseVideo::~DenoiseVideo()
309 {
310
311
312         if(accumulation) delete [] accumulation;
313 }
314
315 int DenoiseVideo::process_realtime(VFrame *input, VFrame *output)
316 {
317         load_configuration();
318
319         int h = input->get_h();
320         int w = input->get_w();
321         int color_model = input->get_color_model();
322
323         if(!accumulation)
324         {
325                 accumulation = new float[w * h * BC_CModels::components(color_model)];
326                 bzero(accumulation, sizeof(float) * w * h * BC_CModels::components(color_model));
327         }
328
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 };
335
336 #define DENOISE_MACRO(type, components, max) \
337 { \
338         for(int i = 0; i < h; i++) \
339         { \
340                 type *output_row = (type*)output->get_rows()[i]; \
341                 type *input_row = (type*)input->get_rows()[i]; \
342  \
343                 for(int k = 0; k < w * components; k++) \
344                 { \
345                         if( do_it[k % components] ) \
346                         { \
347                                 float input_pixel = *input_row; \
348                                 (*accumulation_ptr) = \
349                                         transparency * (*accumulation_ptr) + \
350                                         opacity * input_pixel; \
351  \
352                                 float difference = fabs((*accumulation_ptr) - input_pixel); \
353                                 if( !config.count_changed ? \
354                                          difference > threshold : difference < threshold) \
355                                 { \
356                                         (*accumulation_ptr) = input_pixel; \
357                                         *output_row = (type)(*accumulation_ptr); \
358                                 } \
359                                 else \
360                                 if(sizeof(type) < 4) \
361                                         *output_row = (type)CLIP((*accumulation_ptr), 0, max); \
362                         } \
363                         else \
364                         { \
365                                 *output_row = *input_row; \
366                         } \
367  \
368                         output_row++; \
369                         input_row++; \
370                         accumulation_ptr++; \
371                 } \
372         } \
373 }
374
375
376
377
378
379
380         switch(color_model)
381         {
382                 case BC_RGB888:
383                 case BC_YUV888:
384                         DENOISE_MACRO(unsigned char, 3, 0xff);
385                         break;
386
387                 case BC_RGB_FLOAT:
388                         DENOISE_MACRO(float, 3, 1.0);
389                         break;
390
391                 case BC_RGBA8888:
392                 case BC_YUVA8888:
393                         DENOISE_MACRO(unsigned char, 4, 0xff);
394                         break;
395
396                 case BC_RGBA_FLOAT:
397                         DENOISE_MACRO(float, 4, 1.0);
398                         break;
399
400                 case BC_RGB161616:
401                 case BC_YUV161616:
402                         DENOISE_MACRO(uint16_t, 3, 0xffff);
403                         break;
404
405                 case BC_RGBA16161616:
406                 case BC_YUVA16161616:
407                         DENOISE_MACRO(uint16_t, 4, 0xffff);
408                         break;
409         }
410         return 0;
411 }
412
413
414 const char* DenoiseVideo::plugin_title() { return _("Denoise video"); }
415 int DenoiseVideo::is_realtime() { return 1; }
416
417
418 NEW_WINDOW_MACRO(DenoiseVideo, DenoiseVideoWindow)
419
420 LOAD_CONFIGURATION_MACRO(DenoiseVideo, DenoiseVideoConfig)
421
422 void DenoiseVideo::update_gui()
423 {
424         if(thread)
425         {
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();
433         }
434 }
435
436
437
438
439 void DenoiseVideo::save_data(KeyFrame *keyframe)
440 {
441         FileXML output;
442
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);
453         output.append_tag();
454         output.tag.set_title("/DENOISE_VIDEO");
455         output.append_tag();
456         output.append_newline();
457         output.terminate_string();
458 }
459
460 void DenoiseVideo::read_data(KeyFrame *keyframe)
461 {
462         FileXML input;
463
464         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
465
466         while(!input.read_tag())
467         {
468                 if(input.tag.title_is("DENOISE_VIDEO"))
469                 {
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);
477                 }
478         }
479 }
480