00001 #include "clip.h"
00002 #include "bchash.h"
00003 #include "denoisevideo.h"
00004 #include "filexml.h"
00005 #include "guicast.h"
00006 #include "keyframe.h"
00007 #include "language.h"
00008 #include "picon_png.h"
00009 #include "vframe.h"
00010
00011
00012
00013
00014
00015 #include <stdint.h>
00016 #include <string.h>
00017
00018
00019
00020
00021 REGISTER_PLUGIN(DenoiseVideo)
00022
00023
00024
00025
00026
00027
00028 DenoiseVideoConfig::DenoiseVideoConfig()
00029 {
00030 frames = 2;
00031 threshold = 0.1;
00032 do_r = 1;
00033 do_g = 1;
00034 do_b = 1;
00035 do_a = 1;
00036 }
00037
00038 int DenoiseVideoConfig::equivalent(DenoiseVideoConfig &that)
00039 {
00040 return frames == that.frames &&
00041 EQUIV(threshold, that.threshold) &&
00042 do_r == that.do_r &&
00043 do_g == that.do_g &&
00044 do_b == that.do_b &&
00045 do_a == that.do_a;
00046 }
00047
00048 void DenoiseVideoConfig::copy_from(DenoiseVideoConfig &that)
00049 {
00050 frames = that.frames;
00051 threshold = that.threshold;
00052 do_r = that.do_r;
00053 do_g = that.do_g;
00054 do_b = that.do_b;
00055 do_a = that.do_a;
00056 }
00057
00058 void DenoiseVideoConfig::interpolate(DenoiseVideoConfig &prev,
00059 DenoiseVideoConfig &next,
00060 long prev_frame,
00061 long next_frame,
00062 long current_frame)
00063 {
00064 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
00065 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
00066
00067 this->frames = (int)(prev.frames * prev_scale + next.frames * next_scale);
00068 this->threshold = prev.threshold * prev_scale + next.threshold * next_scale;
00069 do_r = prev.do_r;
00070 do_g = prev.do_g;
00071 do_b = prev.do_b;
00072 do_a = prev.do_a;
00073 }
00074
00075
00076
00077
00078
00079
00080 DenoiseVideoFrames::DenoiseVideoFrames(DenoiseVideo *plugin, int x, int y)
00081 : BC_ISlider(x,
00082 y,
00083 0,
00084 190,
00085 200,
00086 1,
00087 256,
00088 plugin->config.frames)
00089 {
00090 this->plugin = plugin;
00091 }
00092
00093 int DenoiseVideoFrames::handle_event()
00094 {
00095 int result = get_value();
00096 if(result < 1 || result > 256) result = 256;
00097 plugin->config.frames = result;
00098 plugin->send_configure_change();
00099 return 1;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 DenoiseVideoThreshold::DenoiseVideoThreshold(DenoiseVideo *plugin, int x, int y)
00109 : BC_TextBox(x, y, 100, 1, plugin->config.threshold)
00110 {
00111 this->plugin = plugin;
00112 }
00113
00114 int DenoiseVideoThreshold::handle_event()
00115 {
00116 plugin->config.threshold = atof(get_text());
00117 plugin->send_configure_change();
00118 return 1;
00119 }
00120
00121
00122
00123
00124
00125 DenoiseVideoToggle::DenoiseVideoToggle(DenoiseVideo *plugin,
00126 DenoiseVideoWindow *gui,
00127 int x,
00128 int y,
00129 int *output,
00130 char *text)
00131 : BC_CheckBox(x, y, *output, text)
00132 {
00133 this->plugin = plugin;
00134 this->output = output;
00135 }
00136
00137 int DenoiseVideoToggle::handle_event()
00138 {
00139 *output = get_value();
00140 plugin->send_configure_change();
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 DenoiseVideoWindow::DenoiseVideoWindow(DenoiseVideo *plugin, int x, int y)
00153 : BC_Window(plugin->gui_string,
00154 x,
00155 y,
00156 210,
00157 240,
00158 200,
00159 240,
00160 0,
00161 0,
00162 1)
00163 {
00164 this->plugin = plugin;
00165 }
00166
00167
00168 void DenoiseVideoWindow::create_objects()
00169 {
00170 int x = 10, y = 10;
00171 add_subwindow(new BC_Title(x, y, _("Frames to accumulate:")));
00172 y += 20;
00173 add_subwindow(frames = new DenoiseVideoFrames(plugin, x, y));
00174 y += 30;
00175 add_subwindow(new BC_Title(x, y, _("Threshold:")));
00176 y += 20;
00177 add_subwindow(threshold = new DenoiseVideoThreshold(plugin, x, y));
00178 y += 40;
00179 add_subwindow(do_r = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_r, _("Red")));
00180 y += 30;
00181 add_subwindow(do_g = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_g, _("Green")));
00182 y += 30;
00183 add_subwindow(do_b = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_b, _("Blue")));
00184 y += 30;
00185 add_subwindow(do_a = new DenoiseVideoToggle(plugin, this, x, y, &plugin->config.do_a, _("Alpha")));
00186 show_window();
00187 flush();
00188 }
00189
00190 int DenoiseVideoWindow::close_event()
00191 {
00192 set_done(1);
00193 return 1;
00194 }
00195
00196
00197
00198
00199
00200
00201 PLUGIN_THREAD_OBJECT(DenoiseVideo, DenoiseVideoThread, DenoiseVideoWindow)
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 DenoiseVideo::DenoiseVideo(PluginServer *server)
00214 : PluginVClient(server)
00215 {
00216 PLUGIN_CONSTRUCTOR_MACRO
00217 accumulation = 0;
00218 }
00219
00220
00221 DenoiseVideo::~DenoiseVideo()
00222 {
00223 PLUGIN_DESTRUCTOR_MACRO
00224
00225 if(accumulation) delete [] accumulation;
00226 }
00227
00228 int DenoiseVideo::process_realtime(VFrame *input, VFrame *output)
00229 {
00230 load_configuration();
00231
00232 int h = input->get_h();
00233 int w = input->get_w();
00234 int color_model = input->get_color_model();
00235
00236 if(!accumulation)
00237 {
00238 accumulation = new float[w * h * cmodel_components(color_model)];
00239 bzero(accumulation, sizeof(float) * w * h * cmodel_components(color_model));
00240 }
00241
00242 float *accumulation_ptr = accumulation;
00243 float opacity = (float)1.0 / config.frames;
00244 float transparency = 1 - opacity;
00245 float threshold = (float)config.threshold *
00246 cmodel_calculate_max(color_model);
00247 int do_it[4] = { config.do_r, config.do_g, config.do_b, config.do_a };
00248
00249 #define DENOISE_MACRO(type, components, max) \
00250 { \
00251 for(int i = 0; i < h; i++) \
00252 { \
00253 type *output_row = (type*)output->get_rows()[i]; \
00254 type *input_row = (type*)input->get_rows()[i]; \
00255 \
00256 for(int k = 0; k < w * components; k++) \
00257 { \
00258 if(do_it[k % components]) \
00259 { \
00260 float input_pixel = *input_row; \
00261 (*accumulation_ptr) = \
00262 transparency * (*accumulation_ptr) + \
00263 opacity * input_pixel; \
00264 \
00265 if(fabs((*accumulation_ptr) - input_pixel) > threshold) \
00266 { \
00267 (*accumulation_ptr) = input_pixel; \
00268 *output_row = (type)(*accumulation_ptr); \
00269 } \
00270 else \
00271 if(sizeof(type) < 4) \
00272 *output_row = (type)CLIP((*accumulation_ptr), 0, max); \
00273 } \
00274 else \
00275 { \
00276 *output_row = *input_row; \
00277 } \
00278 \
00279 output_row++; \
00280 input_row++; \
00281 accumulation_ptr++; \
00282 } \
00283 } \
00284 }
00285
00286
00287
00288
00289
00290
00291 switch(color_model)
00292 {
00293 case BC_RGB888:
00294 case BC_YUV888:
00295 DENOISE_MACRO(unsigned char, 3, 0xff);
00296 break;
00297
00298 case BC_RGB_FLOAT:
00299 DENOISE_MACRO(float, 3, 1.0);
00300 break;
00301
00302 case BC_RGBA8888:
00303 case BC_YUVA8888:
00304 DENOISE_MACRO(unsigned char, 4, 0xff);
00305 break;
00306
00307 case BC_RGBA_FLOAT:
00308 DENOISE_MACRO(float, 4, 1.0);
00309 break;
00310
00311 case BC_RGB161616:
00312 case BC_YUV161616:
00313 DENOISE_MACRO(uint16_t, 3, 0xffff);
00314 break;
00315
00316 case BC_RGBA16161616:
00317 case BC_YUVA16161616:
00318 DENOISE_MACRO(uint16_t, 4, 0xffff);
00319 break;
00320 }
00321 }
00322
00323
00324 char* DenoiseVideo::plugin_title() { return N_("Denoise video"); }
00325 int DenoiseVideo::is_realtime() { return 1; }
00326
00327
00328 NEW_PICON_MACRO(DenoiseVideo)
00329
00330 SHOW_GUI_MACRO(DenoiseVideo, DenoiseVideoThread)
00331
00332 RAISE_WINDOW_MACRO(DenoiseVideo)
00333
00334 SET_STRING_MACRO(DenoiseVideo);
00335
00336 LOAD_CONFIGURATION_MACRO(DenoiseVideo, DenoiseVideoConfig)
00337
00338 void DenoiseVideo::update_gui()
00339 {
00340 if(thread)
00341 {
00342 load_configuration();
00343 thread->window->lock_window();
00344 thread->window->frames->update(config.frames);
00345 thread->window->threshold->update(config.threshold);
00346 thread->window->unlock_window();
00347 }
00348 }
00349
00350
00351
00352 int DenoiseVideo::load_defaults()
00353 {
00354 char directory[BCTEXTLEN];
00355
00356 sprintf(directory, "%sdenoisevideo.rc", BCASTDIR);
00357
00358
00359 defaults = new BC_Hash(directory);
00360 defaults->load();
00361
00362 config.frames = defaults->get("FRAMES", config.frames);
00363 config.threshold = defaults->get("THRESHOLD", config.threshold);
00364 config.do_r = defaults->get("DO_R", config.do_r);
00365 config.do_g = defaults->get("DO_G", config.do_g);
00366 config.do_b = defaults->get("DO_B", config.do_b);
00367 config.do_a = defaults->get("DO_A", config.do_a);
00368 return 0;
00369 }
00370
00371 int DenoiseVideo::save_defaults()
00372 {
00373 defaults->update("THRESHOLD", config.threshold);
00374 defaults->update("FRAMES", config.frames);
00375 defaults->update("DO_R", config.do_r);
00376 defaults->update("DO_G", config.do_g);
00377 defaults->update("DO_B", config.do_b);
00378 defaults->update("DO_A", config.do_a);
00379 defaults->save();
00380 return 0;
00381 }
00382
00383 void DenoiseVideo::save_data(KeyFrame *keyframe)
00384 {
00385 FileXML output;
00386
00387
00388 output.set_shared_string(keyframe->data, MESSAGESIZE);
00389 output.tag.set_title("DENOISE_VIDEO");
00390 output.tag.set_property("FRAMES", config.frames);
00391 output.tag.set_property("THRESHOLD", config.threshold);
00392 output.tag.set_property("DO_R", config.do_r);
00393 output.tag.set_property("DO_G", config.do_g);
00394 output.tag.set_property("DO_B", config.do_b);
00395 output.tag.set_property("DO_A", config.do_a);
00396 output.append_tag();
00397 output.tag.set_title("/DENOISE_VIDEO");
00398 output.append_tag();
00399 output.terminate_string();
00400 }
00401
00402 void DenoiseVideo::read_data(KeyFrame *keyframe)
00403 {
00404 FileXML input;
00405
00406 input.set_shared_string(keyframe->data, strlen(keyframe->data));
00407
00408 int result = 0;
00409
00410 while(!input.read_tag())
00411 {
00412 if(input.tag.title_is("DENOISE_VIDEO"))
00413 {
00414 config.frames = input.tag.get_property("FRAMES", config.frames);
00415 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
00416 config.do_r = input.tag.get_property("DO_R", config.do_r);
00417 config.do_g = input.tag.get_property("DO_G", config.do_g);
00418 config.do_b = input.tag.get_property("DO_B", config.do_b);
00419 config.do_a = input.tag.get_property("DO_A", config.do_a);
00420 }
00421 }
00422 }
00423
00424
00425
00426