1 #include "bcdisplayinfo.h"
14 #define _(String) gettext(String)
15 #define gettext_noop(String) String
16 #define N_(String) gettext_noop (String)
21 #define WINDOW_BORDER (window_size / 2)
22 #define SGN(x) (x<0 ? -1: 1)
25 REGISTER_PLUGIN(DenoiseEffect)
31 DenoiseEffect::DenoiseEffect(PluginServer *server)
32 : PluginAClient(server)
35 PLUGIN_CONSTRUCTOR_MACRO
38 DenoiseEffect::~DenoiseEffect()
40 PLUGIN_DESTRUCTOR_MACRO
44 NEW_PICON_MACRO(DenoiseEffect)
46 LOAD_CONFIGURATION_MACRO(DenoiseEffect, DenoiseConfig)
48 SHOW_GUI_MACRO(DenoiseEffect, DenoiseThread)
50 RAISE_WINDOW_MACRO(DenoiseEffect)
52 SET_STRING_MACRO(DenoiseEffect)
54 void DenoiseEffect::delete_dsp()
56 if(ex_coeff_d) delete ex_coeff_d;
57 if(ex_coeff_r) delete ex_coeff_r;
58 if(ex_coeff_rn) delete ex_coeff_rn;
59 if(wave_coeff_d) delete wave_coeff_d;
60 if(wave_coeff_r) delete wave_coeff_r;
61 if(decomp_filter) delete decomp_filter;
62 if(recon_filter) delete recon_filter;
63 if(input_buffer) delete [] input_buffer;
64 if(output_buffer) delete [] output_buffer;
65 if(dsp_in) delete [] dsp_in;
66 if(dsp_out) delete [] dsp_out;
67 if(dsp_iteration) delete [] dsp_iteration;
84 void DenoiseEffect::reset()
100 output_allocation = 0;
117 char* DenoiseEffect::plugin_title() { return N_("Denoise"); }
118 int DenoiseEffect::is_realtime() { return 1; }
122 void DenoiseEffect::read_data(KeyFrame *keyframe)
125 input.set_shared_string(keyframe->data, strlen(keyframe->data));
130 result = input.read_tag();
134 if(input.tag.title_is("DENOISE"))
136 config.level = input.tag.get_property("LEVEL", config.level);
142 void DenoiseEffect::save_data(KeyFrame *keyframe)
145 output.set_shared_string(keyframe->data, MESSAGESIZE);
147 output.tag.set_title("DENOISE");
148 output.tag.set_property("LEVEL", config.level);
150 output.tag.set_title("/DENOISE");
152 output.append_newline();
154 output.terminate_string();
157 int DenoiseEffect::load_defaults()
159 char directory[BCTEXTLEN], string[BCTEXTLEN];
160 sprintf(directory, "%sdenoise.rc", BCASTDIR);
161 defaults = new BC_Hash(directory);
164 config.level = defaults->get("LEVEL", config.level);
168 int DenoiseEffect::save_defaults()
170 char string[BCTEXTLEN];
172 defaults->update("LEVEL", config.level);
178 void DenoiseEffect::update_gui()
182 thread->window->lock_window();
183 thread->window->update();
184 thread->window->unlock_window();
190 double DenoiseEffect::dot_product(double *data, double *filter, char filtlen)
196 for(i = 0; i < filtlen; i++) sum += *data-- * *filter++;
200 int DenoiseEffect::convolve_dec_2(double *input_sequence,
204 double *output_sequence)
206 // convolve the input sequence with the filter and decimate by two
207 int i, shortlen, offset;
208 int64_t lengthp4 = length + 4;
209 int64_t lengthm4 = length - 4;
210 int64_t lengthp5 = length + 5;
211 int64_t lengthp8 = length + 8;
213 for(i = 0; (i <= lengthp8) && ((i - filtlen) <= lengthp8); i += 2)
216 *output_sequence++ = dot_product(input_sequence + i, filter, i + 1);
220 offset = i - lengthm4;
221 shortlen = filtlen - offset;
222 *output_sequence++ = dot_product(input_sequence + lengthp4,
223 filter + offset, shortlen);
226 *output_sequence++ = dot_product(input_sequence + i, filter, filtlen);
231 int64_t DenoiseEffect::decompose_branches(double *in_data,
233 WaveletFilters *decomp_filter,
237 // Take input data and filters and form two branches of half the
238 // original length. Length of branches is returned.
239 convolve_dec_2(in_data, length, decomp_filter->h, decomp_filter->length, out_low);
240 convolve_dec_2(in_data, length, decomp_filter->g, decomp_filter->length, out_high);
244 int DenoiseEffect::wavelet_decomposition(double *in_data,
248 for(int i = 0; i < levels; i++)
250 in_length = decompose_branches(in_data,
254 out_data[(2 * i) + 1]);
256 in_data = out_data[2 * i];
261 int DenoiseEffect::tree_copy(double **output,
266 register int i, j, k, l, m;
268 for(i = 0, k = 1; k < levels; i++, k++)
274 for(j = 0; j < length + 5; j++)
277 output[m][j] = input[m][j];
285 for(j = 0; j < length + 5; j++)
287 output[l][j] = input[l][j];
288 output[m][j] = input[m][j];
293 int DenoiseEffect::threshold(int window_size, double gammas, int levels)
296 double threshold, cv, cvb, abs_coeff_r;
297 double *coeff_r, *coeff_l;
300 for(i = 0; i < levels; i++)
302 length = (window_size >> (i + 1)) + 5;
303 threshold = sqrt(2 * log(length) / log(2)) * gammas / sqrt(length);
305 for(j = 0; j < length; j++)
307 coeff_r = &(ex_coeff_r->values[(2 * i) + 1][j]);
308 coeff_l = &(ex_coeff_rn->values[(2 * i) + 1][j]);
311 abs_coeff_r = fabs(*coeff_r);
312 cvb = abs_coeff_r - threshold;
315 if(abs_coeff_r > threshold)
330 double DenoiseEffect::dot_product_even(double *data, double *filter, int filtlen)
336 for(i = 0; i < filtlen; i += 2) sum += *data-- * filter[i];
341 double DenoiseEffect::dot_product_odd(double *data, double *filter, int filtlen)
347 for(i = 1; i < filtlen; i += 2) sum += *data-- * filter[i];
351 int DenoiseEffect::convolve_int_2(double *input_sequence,
356 double *output_sequence)
357 // insert zeros between each element of the input sequence and
358 // convolve with the filter to interpolate the data
361 int endpoint = length + filtlen - 2;
365 // summation with previous convolution
366 // every other dot product interpolates the data
367 for(i = (filtlen / 2) - 1, j = (filtlen / 2); i < endpoint; i++, j++)
369 *output_sequence++ += dot_product_odd(input_sequence + i, filter, filtlen);
370 *output_sequence++ += dot_product_even(input_sequence + j, filter, filtlen);
373 *output_sequence++ += dot_product_odd(input_sequence + i, filter, filtlen);
377 // first convolution of pair
378 // every other dot product interpolates the data
379 for(i = (filtlen / 2) - 1, j = (filtlen / 2); i < endpoint; i++, j++)
381 *output_sequence++ = dot_product_odd(input_sequence + i, filter, filtlen);
382 *output_sequence++ = dot_product_even(input_sequence + j, filter, filtlen);
385 *output_sequence++ = dot_product_odd(input_sequence + i, filter, filtlen);
391 int64_t DenoiseEffect::reconstruct_branches(double *in_low,
394 WaveletFilters *recon_filter,
397 // take input data and filters and form two branches of half the
398 // original length. length of branches is returned
399 convolve_int_2(in_low, in_length, recon_filter->h,
400 recon_filter->length, 0, output);
401 convolve_int_2(in_high, in_length, recon_filter->g,
402 recon_filter->length, 1, output);
403 return in_length * 2;
406 int DenoiseEffect::wavelet_reconstruction(double **in_data,
413 in_length = in_length >> levels;
414 // destination of all but last branch reconstruction is the next
415 // higher intermediate approximation
416 for(i = levels - 1; i > 0; i--)
418 output = in_data[2 * (i - 1)];
419 in_length = reconstruct_branches(in_data[2 * i],
420 in_data[(2 * i) + 1],
426 // destination of the last branch reconstruction is the output data
427 reconstruct_branches(in_data[0],
436 void DenoiseEffect::process_window()
439 for(j = 0; j < iterations; j++)
441 wavelet_decomposition(dsp_in, window_size, ex_coeff_d->values);
443 tree_copy(ex_coeff_r->values, ex_coeff_d->values, window_size, levels);
444 tree_copy(ex_coeff_rn->values, ex_coeff_d->values, window_size, levels);
447 //printf("DenoiseEffect::process_window %f\n", config.level);
448 threshold(window_size, config.level * 10.0, levels);
450 wavelet_reconstruction(ex_coeff_r->values, window_size, dsp_iteration);
451 wavelet_reconstruction(ex_coeff_rn->values, window_size, dsp_in);
453 for(i = 0; i < window_size; i++)
454 dsp_out[i] += dsp_iteration[i];
461 int DenoiseEffect::process_realtime(int64_t size, double *input_ptr, double *output_ptr)
463 load_configuration();
467 int64_t size_factor = (int)(pow(2, levels));
468 dsp_in = new double[window_size * size_factor];
469 dsp_out = new double[window_size * 2];
470 dsp_iteration = new double[window_size * 2];
473 ex_coeff_d = new Tree(window_size, levels);
474 ex_coeff_r = new Tree(window_size, levels);
475 ex_coeff_rn = new Tree(window_size, levels);
476 wave_coeff_d = new WaveletCoeffs(alpha, beta);
477 wave_coeff_r = new WaveletCoeffs(alpha, beta);
478 decomp_filter = new WaveletFilters(wave_coeff_d, DECOMP);
479 recon_filter = new WaveletFilters(wave_coeff_r, RECON);
480 in_scale = 65535 / sqrt(window_size) / iterations;
481 out_scale = output_level / 65535 * sqrt(window_size);
485 // Append input buffer
486 if(input_size + size > input_allocation)
488 double *new_input = new double[input_size + size];
491 memcpy(new_input, input_buffer, sizeof(double) * input_size);
492 delete [] input_buffer;
494 input_buffer = new_input;
495 input_allocation = input_size + size;
497 memcpy(input_buffer + input_size,
499 size * sizeof(double));
503 // Have enough to do some windows
504 while(input_size >= window_size)
507 for(int i = 0; i < window_size; i++)
509 dsp_in[i] = input_buffer[i] * in_scale;
511 bzero(dsp_out, sizeof(double) * window_size);
518 // First window produces garbage
528 // Crossfade into the output buffer
529 int64_t new_allocation = output_size + window_size;
530 if(new_allocation > output_allocation)
532 double *new_output = new double[new_allocation];
536 memcpy(new_output, output_buffer, sizeof(double) * output_size);
537 //printf("CrossfadeFFT::process_fifo 1 %p\n", output_buffer);
538 delete [] output_buffer;
539 //printf("CrossfadeFFT::process_fifo 2\n");
541 output_buffer = new_output;
542 output_allocation = new_allocation;
545 if(output_size >= WINDOW_BORDER)
547 for(int i = 0, j = output_size - WINDOW_BORDER;
551 double src_level = (double)i / WINDOW_BORDER;
552 double dst_level = (double)(WINDOW_BORDER - i) / WINDOW_BORDER;
553 output_buffer[j] = output_buffer[j] * dst_level + out_scale * dsp_out[i] * src_level;
556 for(int i = 0; i < window_size - WINDOW_BORDER; i++)
557 output_buffer[output_size + i] = dsp_out[WINDOW_BORDER + i] * out_scale;
558 output_size += window_size - WINDOW_BORDER;
562 // First buffer has no crossfade
563 memcpy(output_buffer + output_size,
565 sizeof(double) * window_size);
566 output_size += window_size;
570 // Shift input buffer forward
571 for(int i = window_size - WINDOW_BORDER, j = 0;
574 input_buffer[j] = input_buffer[i];
575 input_size -= window_size - WINDOW_BORDER;
579 // Have enough to send to output
580 if(output_size - WINDOW_BORDER >= size)
582 memcpy(output_ptr, output_buffer, sizeof(double) * size);
583 for(int i = size, j = 0; i < output_size; i++, j++)
584 output_buffer[j] = output_buffer[i];
589 //printf("DenoiseEffect::process_realtime 1\n");
590 bzero(output_ptr, sizeof(double) * size);
602 Tree::Tree(int input_length, int levels)
604 this->input_length = input_length;
605 this->levels = levels;
608 // create decomposition tree
609 values = new double*[2 * levels];
611 for (i = 0; i < levels; i++)
619 values[2 * i] = new double[j + 5];
620 values[2 * i + 1] = new double[j + 5];
628 for (i = 2 * levels - 1; i >= 0; i--)
634 WaveletCoeffs::WaveletCoeffs(double alpha, double beta)
637 double tcosa = cos(alpha);
638 double tcosb = cos(beta);
639 double tsina = sin(alpha);
640 double tsinb = sin(beta);
642 // calculate first two wavelet coefficients a = a(-2) and b = a(-1)
643 values[0] = ((1.0 + tcosa + tsina) * (1.0 - tcosb - tsinb)
644 + 2.0 * tsinb * tcosa) / 4.0;
645 values[1] = ((1.0 - tcosa + tsina) * (1.0 + tcosb - tsinb)
646 - 2.0 * tsinb * tcosa) / 4.0;
648 tcosa = cos(alpha - beta);
649 tsina = sin(alpha - beta);
651 // calculate last four wavelet coefficients c = a(0), d = a(1),
652 // e = a(2), and f = a(3)
653 values[2] = (1.0 + tcosa + tsina) / 2.0;
654 values[3] = (1.0 + tcosa - tsina) / 2.0;
655 values[4] = 1 - values[0] - values[2];
656 values[5] = 1 - values[1] - values[3];
658 // zero out very small coefficient values caused by truncation error
659 for (i = 0; i < 6; i++)
661 if (fabs(values[i]) < 1.0e-15) values[i] = 0.0;
665 WaveletCoeffs::~WaveletCoeffs()
670 WaveletFilters::WaveletFilters(WaveletCoeffs *wave_coeffs, wavetype transform)
674 // find the first non-zero wavelet coefficient
676 while(wave_coeffs->values[i] == 0.0) i++;
678 // find the last non-zero wavelet coefficient
680 while(wave_coeffs->values[j] == 0.0) j--;
682 // Form the decomposition filters h~ and g~ or the reconstruction
683 // filters h and g. The division by 2 in the construction
684 // of the decomposition filters is for normalization.
686 for(k = 0; k < length; k++)
688 if (transform == DECOMP)
690 h[k] = wave_coeffs->values[j--] / 2.0;
691 g[k] = (double) (((i++ & 0x01) * 2) - 1) * wave_coeffs->values[i] / 2.0;
695 h[k] = wave_coeffs->values[i++];
696 g[k] = (double) (((j-- & 0x01) * 2) - 1) * wave_coeffs->values[j];
700 // clear out the additional array locations, if any
708 WaveletFilters::~WaveletFilters()
720 DenoiseConfig::DenoiseConfig()
725 void DenoiseConfig::copy_from(DenoiseConfig &that)
730 int DenoiseConfig::equivalent(DenoiseConfig &that)
732 return EQUIV(level, that.level);
735 void DenoiseConfig::interpolate(DenoiseConfig &prev,
739 int64_t current_frame)
741 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
742 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
743 this->level = prev.level * prev_scale + next.level * next_scale;
755 PLUGIN_THREAD_OBJECT(DenoiseEffect, DenoiseThread, DenoiseWindow)
766 DenoiseWindow::DenoiseWindow(DenoiseEffect *plugin, int x, int y)
767 : BC_Window(plugin->gui_string,
778 this->plugin = plugin;
781 void DenoiseWindow::create_objects()
785 add_subwindow(new BC_Title(x, y, _("Level:")));
787 add_subwindow(scale = new DenoiseLevel(plugin, x, y));
792 int DenoiseWindow::close_event()
794 // Set result to 1 to indicate a client side close
799 void DenoiseWindow::update()
801 scale->update(plugin->config.level);
815 DenoiseLevel::DenoiseLevel(DenoiseEffect *plugin, int x, int y)
816 : BC_FPot(x, y, (float)plugin->config.level, 0, 1.0)
818 this->plugin = plugin;
822 int DenoiseLevel::handle_event()
824 plugin->config.level = get_value();
825 plugin->send_configure_change();