Condense the xml-cleanup into a short feature-branch
[cinelerra_cv/mob.git] / plugins / denoisemjpeg / denoisemjpeg.C
blobd0470129c1775e059d9a877a07ecd7f84b8ea453
1 #include "clip.h"
2 #include "bchash.h"
3 #include "denoisemjpeg.h"
4 #include "filexml.h"
5 #include "guicast.h"
6 #include "keyframe.h"
7 #include "picon_png.h"
8 #include "vframe.h"
11 #include <libintl.h>
12 #define _(String) gettext(String)
13 #define gettext_noop(String) String
14 #define N_(String) gettext_noop (String)
19 #include <stdint.h>
20 #include <string.h>
24 REGISTER_PLUGIN(DenoiseMJPEG)
31 DenoiseMJPEGConfig::DenoiseMJPEGConfig()
33         radius = 8;
34         threshold = 5;
35         threshold2 = 4;
36         sharpness = 125;
37         lcontrast = 100;
38         ccontrast = 100;
39         deinterlace = 0;
40         mode = 0;
41         delay = 3;
44 int DenoiseMJPEGConfig::equivalent(DenoiseMJPEGConfig &that)
46         return 
47                 that.radius == radius && 
48                 that.threshold == threshold && 
49                 that.threshold2 == threshold2 && 
50                 that.sharpness == sharpness && 
51                 that.lcontrast == lcontrast && 
52                 that.ccontrast == ccontrast && 
53                 that.deinterlace == deinterlace && 
54                 that.mode == mode && 
55                 that.delay == delay;
58 void DenoiseMJPEGConfig::copy_from(DenoiseMJPEGConfig &that)
60         radius = that.radius;
61         threshold = that.threshold;
62         threshold2 = that.threshold2;
63         sharpness = that.sharpness;
64         lcontrast = that.lcontrast;
65         ccontrast = that.ccontrast;
66         deinterlace = that.deinterlace;
67         mode = that.mode;
68         delay = that.delay;
71 void DenoiseMJPEGConfig::interpolate(DenoiseMJPEGConfig &prev, 
72         DenoiseMJPEGConfig &next, 
73         long prev_frame, 
74         long next_frame, 
75         long current_frame)
77         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
78         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
80         this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale);
81         this->threshold = (int)(prev.threshold * prev_scale + next.threshold * next_scale);
82         this->threshold2 = (int)(prev.threshold2 * prev_scale + next.threshold2 * next_scale);
83         this->sharpness = (int)(prev.sharpness * prev_scale + next.sharpness * next_scale);
84         this->lcontrast = (int)(prev.lcontrast * prev_scale + next.lcontrast * next_scale);
85         this->ccontrast = (int)(prev.ccontrast * prev_scale + next.ccontrast * next_scale);
86         this->deinterlace = prev.deinterlace;
87         this->mode = prev.mode;
88         this->delay = (int)(prev.delay * prev_scale + next.delay * next_scale);
96 DenoiseMJPEGRadius::DenoiseMJPEGRadius(DenoiseMJPEG *plugin, int x, int y)
97  : BC_IPot(x, 
98         y, 
99         plugin->config.radius,
100         8,
101         24)
103         this->plugin = plugin;
106 int DenoiseMJPEGRadius::handle_event()
108         int result = get_value();
109         plugin->config.radius = result;
110         plugin->send_configure_change();
111         return 1;
119 DenoiseMJPEGThresh::DenoiseMJPEGThresh(DenoiseMJPEG *plugin, int x, int y)
120  : BC_IPot(x, 
121         y, 
122         plugin->config.threshold,
123         0,
124         255)
126         this->plugin = plugin;
129 int DenoiseMJPEGThresh::handle_event()
131         int result = get_value();
132         plugin->config.threshold = result;
133         plugin->send_configure_change();
134         return 1;
142 DenoiseMJPEGThresh2::DenoiseMJPEGThresh2(DenoiseMJPEG *plugin, int x, int y)
143  : BC_IPot(x, 
144         y, 
145         plugin->config.threshold2,
146         0,
147         255)
149         this->plugin = plugin;
152 int DenoiseMJPEGThresh2::handle_event()
154         int result = get_value();
155         plugin->config.threshold2 = result;
156         plugin->send_configure_change();
157         return 1;
165 DenoiseMJPEGSharp::DenoiseMJPEGSharp(DenoiseMJPEG *plugin, int x, int y)
166  : BC_IPot(x, 
167         y, 
168         plugin->config.sharpness,
169         0,
170         255)
172         this->plugin = plugin;
175 int DenoiseMJPEGSharp::handle_event()
177         int result = get_value();
178         plugin->config.sharpness = result;
179         plugin->send_configure_change();
180         return 1;
188 DenoiseMJPEGLContrast::DenoiseMJPEGLContrast(DenoiseMJPEG *plugin, int x, int y)
189  : BC_IPot(x, 
190         y, 
191         plugin->config.lcontrast,
192         0,
193         255)
195         this->plugin = plugin;
198 int DenoiseMJPEGLContrast::handle_event()
200         int result = get_value();
201         plugin->config.lcontrast = result;
202         plugin->send_configure_change();
203         return 1;
211 DenoiseMJPEGCContrast::DenoiseMJPEGCContrast(DenoiseMJPEG *plugin, int x, int y)
212  : BC_IPot(x, 
213         y, 
214         plugin->config.ccontrast,
215         0,
216         255)
218         this->plugin = plugin;
221 int DenoiseMJPEGCContrast::handle_event()
223         int result = get_value();
224         plugin->config.ccontrast = result;
225         plugin->send_configure_change();
226         return 1;
234 DenoiseMJPEGDeinterlace::DenoiseMJPEGDeinterlace(DenoiseMJPEG *plugin, int x, int y)
235  : BC_CheckBox(x, 
236         y, 
237         plugin->config.deinterlace,
238         _("Deinterlace"))
240         this->plugin = plugin;
243 int DenoiseMJPEGDeinterlace::handle_event()
245         int result = get_value();
246         plugin->config.deinterlace = result;
247         plugin->send_configure_change();
248         return 1;
256 DenoiseMJPEGModeProgressive::DenoiseMJPEGModeProgressive(DenoiseMJPEG *plugin, DenoiseMJPEGWindow *gui, int x, int y)
257  : BC_Radial(x, 
258         y, 
259         plugin->config.mode == 0,
260         _("Progressive"))
262         this->plugin = plugin;
263         this->gui = gui;
266 int DenoiseMJPEGModeProgressive::handle_event()
268         int result = get_value();
269         if(result) gui->update_mode(0);
270         plugin->send_configure_change();
271         return 1;
275 DenoiseMJPEGModeInterlaced::DenoiseMJPEGModeInterlaced(DenoiseMJPEG *plugin, DenoiseMJPEGWindow *gui, int x, int y)
276  : BC_Radial(x, 
277         y, 
278         plugin->config.mode == 1,
279         _("Interlaced"))
281         this->plugin = plugin;
282         this->gui = gui;
285 int DenoiseMJPEGModeInterlaced::handle_event()
287         int result = get_value();
288         if(result) gui->update_mode(1);
289         plugin->send_configure_change();
290         return 1;
294 DenoiseMJPEGModeFast::DenoiseMJPEGModeFast(DenoiseMJPEG *plugin, DenoiseMJPEGWindow *gui, int x, int y)
295  : BC_Radial(x, 
296         y, 
297         plugin->config.mode == 2,
298         _("Fast"))
300         this->plugin = plugin;
301         this->gui = gui;
304 int DenoiseMJPEGModeFast::handle_event()
306         int result = get_value();
307         if(result) gui->update_mode(2);
308         plugin->send_configure_change();
309         return 1;
317 DenoiseMJPEGDelay::DenoiseMJPEGDelay(DenoiseMJPEG *plugin, int x, int y)
318  : BC_IPot(x, 
319         y, 
320         plugin->config.delay,
321         1,
322         8)
324         this->plugin = plugin;
327 int DenoiseMJPEGDelay::handle_event()
329         int result = get_value();
330         plugin->config.delay = result;
331         plugin->send_configure_change();
332         return 1;
345 DenoiseMJPEGWindow::DenoiseMJPEGWindow(DenoiseMJPEG *plugin, int x, int y)
346  : BC_Window(plugin->gui_string, 
347         x, 
348         y, 
349         250, 
350         350, 
351         250, 
352         350, 
353         0, 
354         0,
355         1)
357         this->plugin = plugin;
361 void DenoiseMJPEGWindow::create_objects()
363         int x1 = 10, y1 = 20, x2 = 140, x3 = 180, y2 = 10, margin = 30, margin2 = 25;
364         add_subwindow(new BC_Title(x1, y1, _("Search radius:")));
365         add_subwindow(radius = new DenoiseMJPEGRadius(plugin, x2, y2));
366         y1 += margin;
367         y2 += margin;
368         add_subwindow(new BC_Title(x1, y1, _("Pass 1 threshold:")));
369         add_subwindow(threshold1 = new DenoiseMJPEGThresh(plugin, x3, y2));
370         y1 += margin;
371         y2 += margin;
372         add_subwindow(new BC_Title(x1, y1, _("Pass 2 threshold:")));
373         add_subwindow(threshold2 = new DenoiseMJPEGThresh2(plugin, x2, y2));
374         y1 += margin;
375         y2 += margin;
376         add_subwindow(new BC_Title(x1, y1, _("Sharpness:")));
377         add_subwindow(sharpness = new DenoiseMJPEGSharp(plugin, x3, y2));
378         y1 += margin;
379         y2 += margin;
380         add_subwindow(new BC_Title(x1, y1, _("Luma contrast:")));
381         add_subwindow(lcontrast = new DenoiseMJPEGLContrast(plugin, x2, y2));
382         y1 += margin;
383         y2 += margin;
384         add_subwindow(new BC_Title(x1, y1, _("Chroma contrast:")));
385         add_subwindow(ccontrast = new DenoiseMJPEGCContrast(plugin, x3, y2));
386         y1 += margin;
387         y2 += margin;
388         add_subwindow(new BC_Title(x1, y1, _("Delay frames:")));
389         add_subwindow(delay = new DenoiseMJPEGDelay(plugin, x2, y2));
390         y1 += margin;
391         y2 += margin;
392         add_subwindow(new BC_Title(x1, y1, _("Mode:")));
393         add_subwindow(interlaced = new DenoiseMJPEGModeInterlaced(plugin, this, x2, y1));
394         y1 += margin2;
395         y2 += margin2;
396         add_subwindow(progressive = new DenoiseMJPEGModeProgressive(plugin, this, x2, y1));
397         y1 += margin2;
398         y2 += margin2;
399         add_subwindow(fast = new DenoiseMJPEGModeFast(plugin, this, x2, y1));
400         y1 += margin;
401         y2 += margin;
402         add_subwindow(deinterlace = new DenoiseMJPEGDeinterlace(plugin, x1, y1));
404         show_window();
405         flush();
408 void DenoiseMJPEGWindow::update_mode(int value)
410         plugin->config.mode = value;
411         progressive->update(value == 0);
412         interlaced->update(value == 1);
413         fast->update(value == 2);
416 int DenoiseMJPEGWindow::close_event()
418         set_done(1);
419         return 1;
427 PLUGIN_THREAD_OBJECT(DenoiseMJPEG, DenoiseMJPEGThread, DenoiseMJPEGWindow)
439 DenoiseMJPEG::DenoiseMJPEG(PluginServer *server)
440  : PluginVClient(server)
442         PLUGIN_CONSTRUCTOR_MACRO
443         accumulation = 0;
447 DenoiseMJPEG::~DenoiseMJPEG()
449         PLUGIN_DESTRUCTOR_MACRO
451         if(accumulation) delete [] accumulation;
454 int DenoiseMJPEG::process_realtime(VFrame *input, VFrame *output)
456         load_configuration();
460 char* DenoiseMJPEG::plugin_title() { return N_("Denoise video2"); }
461 int DenoiseMJPEG::is_realtime() { return 1; }
463 VFrame* DenoiseMJPEG::new_picon()
465         return new VFrame(picon_png);
468 SHOW_GUI_MACRO(DenoiseMJPEG, DenoiseMJPEGThread)
470 RAISE_WINDOW_MACRO(DenoiseMJPEG)
472 void DenoiseMJPEG::update_gui()
474         if(thread) 
475         {
476                 load_configuration();
477                 thread->window->lock_window();
478                 thread->window->delay->update(config.delay);
479                 thread->window->threshold1->update(config.threshold);
480                 thread->window->unlock_window();
481         }
486 SET_STRING_MACRO(DenoiseMJPEG);
488 LOAD_CONFIGURATION_MACRO(DenoiseMJPEG, DenoiseMJPEGConfig)
490 int DenoiseMJPEG::load_defaults()
492         char directory[BCTEXTLEN];
493 // set the default directory
494         sprintf(directory, "%sdenoisevideo.rc", BCASTDIR);
496 // load the defaults
497         defaults = new BC_Hash(directory);
498         defaults->load();
500         config.radius = defaults->get("RADIUS", config.radius);
501         config.threshold = defaults->get("THRESHOLD", config.threshold);
502         config.threshold2 = defaults->get("THRESHOLD2", config.threshold2);
503         config.sharpness = defaults->get("SHARPNESS", config.sharpness);
504         config.lcontrast = defaults->get("LCONTRAST", config.lcontrast);
505         config.ccontrast = defaults->get("CCONTRAST", config.ccontrast);
506         config.deinterlace = defaults->get("DEINTERLACE", config.deinterlace);
507         config.mode = defaults->get("MODE", config.mode);
508         config.delay = defaults->get("DELAY", config.delay);
509         return 0;
512 int DenoiseMJPEG::save_defaults()
514         defaults->update("RADIUS", config.radius);
515         defaults->update("THRESHOLD", config.threshold);
516         defaults->update("THRESHOLD2", config.threshold2);
517         defaults->update("SHARPNESS", config.sharpness);
518         defaults->update("LCONTRAST", config.lcontrast);
519         defaults->update("CCONTRAST", config.ccontrast);
520         defaults->update("DEINTERLACE", config.deinterlace);
521         defaults->update("MODE", config.mode);
522         defaults->update("DELAY", config.delay);
523         defaults->save();
524         return 0;
527 void DenoiseMJPEG::save_data(KeyFrame *keyframe)
529         FileXML output;
531 // cause data to be stored directly in text
532         output.set_shared_string(keyframe->data, MESSAGESIZE);
533         output.tag.set_title("DENOISE_VIDEO2");
534         output.tag.set_property("RADIUS", config.radius);
535         output.tag.set_property("THRESHOLD", config.threshold);
536         output.tag.set_property("THRESHOLD2", config.threshold2);
537         output.tag.set_property("SHARPNESS", config.sharpness);
538         output.tag.set_property("LCONTRAST", config.lcontrast);
539         output.tag.set_property("CCONTRAST", config.ccontrast);
540         output.tag.set_property("DEINTERLACE", config.deinterlace);
541         output.tag.set_property("MODE", config.mode);
542         output.tag.set_property("DELAY", config.delay);
543         output.append_tag();
544         output.tag.set_title("/DENOISE_VIDEO2");
545         output.append_tag();
546         output.terminate_string();
549 void DenoiseMJPEG::read_data(KeyFrame *keyframe)
551         FileXML input;
553         input.set_shared_string(keyframe->data, strlen(keyframe->data));
555         int result = 0;
557         while(!input.read_tag())
558         {
559                 if(input.tag.title_is("DENOISE_VIDEO2"))
560                 {
561                         config.radius = input.tag.get_property("RADIUS", config.radius);
562                         config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
563                         config.threshold2 = input.tag.get_property("THRESHOLD2", config.threshold2);
564                         config.sharpness = input.tag.get_property("SHARPNESS", config.sharpness);
565                         config.lcontrast = input.tag.get_property("LCONTRAST", config.lcontrast);
566                         config.ccontrast = input.tag.get_property("CCONTRAST", config.ccontrast);
567                         config.deinterlace = input.tag.get_property("DEINTERLACE", config.deinterlace);
568                         config.mode = input.tag.get_property("MODE", config.mode);
569                         config.delay = input.tag.get_property("DELAY", config.delay);
570                 }
571         }