r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / plugins / denoisemjpeg / denoisemjpeg.C
blob1c0a6ef304f132e02a61802ac70dd42a13ba64ac
1 #include "clip.h"
2 #include "defaults.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 == 1);
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         accumulation = 0;
443         thread = 0;
444         load_defaults();
448 DenoiseMJPEG::~DenoiseMJPEG()
450         if(thread)
451         {
452                 thread->window->set_done(0);
453                 thread->completion.lock();
454                 delete thread;
455         }
457         save_defaults();
458         delete defaults;
460         if(accumulation) delete [] accumulation;
463 int DenoiseMJPEG::process_realtime(VFrame *input, VFrame *output)
465         load_configuration();
469 int DenoiseMJPEG::is_realtime()
471         return 1;
474 char* DenoiseMJPEG::plugin_title()
476         return _("Denoise video2");
479 VFrame* DenoiseMJPEG::new_picon()
481         return new VFrame(picon_png);
484 SHOW_GUI_MACRO(DenoiseMJPEG, DenoiseMJPEGThread)
486 RAISE_WINDOW_MACRO(DenoiseMJPEG)
488 void DenoiseMJPEG::update_gui()
490         if(thread) 
491         {
492                 load_configuration();
493                 thread->window->lock_window();
494                 thread->window->delay->update(config.delay);
495                 thread->window->threshold1->update(config.threshold);
496                 thread->window->unlock_window();
497         }
502 SET_STRING_MACRO(DenoiseMJPEG);
504 LOAD_CONFIGURATION_MACRO(DenoiseMJPEG, DenoiseMJPEGConfig)
506 int DenoiseMJPEG::load_defaults()
508         char directory[BCTEXTLEN];
509 // set the default directory
510         sprintf(directory, "%sdenoisevideo.rc", BCASTDIR);
512 // load the defaults
513         defaults = new Defaults(directory);
514         defaults->load();
516         config.radius = defaults->get("RADIUS", config.radius);
517         config.threshold = defaults->get("THRESHOLD", config.threshold);
518         config.threshold2 = defaults->get("THRESHOLD2", config.threshold2);
519         config.sharpness = defaults->get("SHARPNESS", config.sharpness);
520         config.lcontrast = defaults->get("LCONTRAST", config.lcontrast);
521         config.ccontrast = defaults->get("CCONTRAST", config.ccontrast);
522         config.deinterlace = defaults->get("DEINTERLACE", config.deinterlace);
523         config.mode = defaults->get("MODE", config.mode);
524         config.delay = defaults->get("DELAY", config.delay);
525         return 0;
528 int DenoiseMJPEG::save_defaults()
530         defaults->update("RADIUS", config.radius);
531         defaults->update("THRESHOLD", config.threshold);
532         defaults->update("THRESHOLD2", config.threshold2);
533         defaults->update("SHARPNESS", config.sharpness);
534         defaults->update("LCONTRAST", config.lcontrast);
535         defaults->update("CCONTRAST", config.ccontrast);
536         defaults->update("DEINTERLACE", config.deinterlace);
537         defaults->update("MODE", config.mode);
538         defaults->update("DELAY", config.delay);
539         defaults->save();
540         return 0;
543 void DenoiseMJPEG::save_data(KeyFrame *keyframe)
545         FileXML output;
547 // cause data to be stored directly in text
548         output.set_shared_string(keyframe->data, MESSAGESIZE);
549         output.tag.set_title("DENOISE_VIDEO2");
550         output.tag.set_property("RADIUS", config.radius);
551         output.tag.set_property("THRESHOLD", config.threshold);
552         output.tag.set_property("THRESHOLD2", config.threshold2);
553         output.tag.set_property("SHARPNESS", config.sharpness);
554         output.tag.set_property("LCONTRAST", config.lcontrast);
555         output.tag.set_property("CCONTRAST", config.ccontrast);
556         output.tag.set_property("DEINTERLACE", config.deinterlace);
557         output.tag.set_property("MODE", config.mode);
558         output.tag.set_property("DELAY", config.delay);
559         output.append_tag();
560         output.terminate_string();
563 void DenoiseMJPEG::read_data(KeyFrame *keyframe)
565         FileXML input;
567         input.set_shared_string(keyframe->data, strlen(keyframe->data));
569         int result = 0;
571         while(!input.read_tag())
572         {
573                 if(input.tag.title_is("DENOISE_VIDEO2"))
574                 {
575                         config.radius = input.tag.get_property("RADIUS", config.radius);
576                         config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
577                         config.threshold2 = input.tag.get_property("THRESHOLD2", config.threshold2);
578                         config.sharpness = input.tag.get_property("SHARPNESS", config.sharpness);
579                         config.lcontrast = input.tag.get_property("LCONTRAST", config.lcontrast);
580                         config.ccontrast = input.tag.get_property("CCONTRAST", config.ccontrast);
581                         config.deinterlace = input.tag.get_property("DEINTERLACE", config.deinterlace);
582                         config.mode = input.tag.get_property("MODE", config.mode);
583                         config.delay = input.tag.get_property("DELAY", config.delay);
584                 }
585         }