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.append_newline();
152 output.terminate_string();
155 int DenoiseEffect::load_defaults()
157 char directory[BCTEXTLEN], string[BCTEXTLEN];
158 sprintf(directory, "%sdenoise.rc", BCASTDIR);
159 defaults = new BC_Hash(directory);
162 config.level = defaults->get("LEVEL", config.level);
166 int DenoiseEffect::save_defaults()
168 char string[BCTEXTLEN];
170 defaults->update("LEVEL", config.level);
176 void DenoiseEffect::update_gui()
180 thread->window->lock_window();
181 thread->window->update();
182 thread->window->unlock_window();
188 double DenoiseEffect::dot_product(double *data, double *filter, char filtlen)
194 for(i = 0; i < filtlen; i++) sum += *data-- * *filter++;
198 int DenoiseEffect::convolve_dec_2(double *input_sequence,
202 double *output_sequence)
204 // convolve the input sequence with the filter and decimate by two
205 int i, shortlen, offset;
206 int64_t lengthp4 = length + 4;
207 int64_t lengthm4 = length - 4;
208 int64_t lengthp5 = length + 5;
209 int64_t lengthp8 = length + 8;
211 for(i = 0; (i <= lengthp8) && ((i - filtlen) <= lengthp8); i += 2)
214 *output_sequence++ = dot_product(input_sequence + i, filter, i + 1);
218 offset = i - lengthm4;
219 shortlen = filtlen - offset;
220 *output_sequence++ = dot_product(input_sequence + lengthp4,
221 filter + offset, shortlen);
224 *output_sequence++ = dot_product(input_sequence + i, filter, filtlen);
229 int64_t DenoiseEffect::decompose_branches(double *in_data,
231 WaveletFilters *decomp_filter,
235 // Take input data and filters and form two branches of half the
236 // original length. Length of branches is returned.
237 convolve_dec_2(in_data, length, decomp_filter->h, decomp_filter->length, out_low);
238 convolve_dec_2(in_data, length, decomp_filter->g, decomp_filter->length, out_high);
242 int DenoiseEffect::wavelet_decomposition(double *in_data,
246 for(int i = 0; i < levels; i++)
248 in_length = decompose_branches(in_data,
252 out_data[(2 * i) + 1]);
254 in_data = out_data[2 * i];
259 int DenoiseEffect::tree_copy(double **output,
264 register int i, j, k, l, m;
266 for(i = 0, k = 1; k < levels; i++, k++)
272 for(j = 0; j < length + 5; j++)
275 output[m][j] = input[m][j];
283 for(j = 0; j < length + 5; j++)
285 output[l][j] = input[l][j];
286 output[m][j] = input[m][j];
291 int DenoiseEffect::threshold(int window_size, double gammas, int levels)
294 double threshold, cv, cvb, abs_coeff_r;
295 double *coeff_r, *coeff_l;
298 for(i = 0; i < levels; i++)
300 length = (window_size >> (i + 1)) + 5;
301 threshold = sqrt(2 * log(length) / log(2)) * gammas / sqrt(length);
303 for(j = 0; j < length; j++)
305 coeff_r = &(ex_coeff_r->values[(2 * i) + 1][j]);
306 coeff_l = &(ex_coeff_rn->values[(2 * i) + 1][j]);
309 abs_coeff_r = fabs(*coeff_r);
310 cvb = abs_coeff_r - threshold;
313 if(abs_coeff_r > threshold)
328 double DenoiseEffect::dot_product_even(double *data, double *filter, int filtlen)
334 for(i = 0; i < filtlen; i += 2) sum += *data-- * filter[i];
339 double DenoiseEffect::dot_product_odd(double *data, double *filter, int filtlen)
345 for(i = 1; i < filtlen; i += 2) sum += *data-- * filter[i];
349 int DenoiseEffect::convolve_int_2(double *input_sequence,
354 double *output_sequence)
355 // insert zeros between each element of the input sequence and
356 // convolve with the filter to interpolate the data
359 int endpoint = length + filtlen - 2;
363 // summation with previous convolution
364 // every other dot product interpolates the data
365 for(i = (filtlen / 2) - 1, j = (filtlen / 2); i < endpoint; i++, j++)
367 *output_sequence++ += dot_product_odd(input_sequence + i, filter, filtlen);
368 *output_sequence++ += dot_product_even(input_sequence + j, filter, filtlen);
371 *output_sequence++ += dot_product_odd(input_sequence + i, filter, filtlen);
375 // first convolution of pair
376 // every other dot product interpolates the data
377 for(i = (filtlen / 2) - 1, j = (filtlen / 2); i < endpoint; i++, j++)
379 *output_sequence++ = dot_product_odd(input_sequence + i, filter, filtlen);
380 *output_sequence++ = dot_product_even(input_sequence + j, filter, filtlen);
383 *output_sequence++ = dot_product_odd(input_sequence + i, filter, filtlen);
389 int64_t DenoiseEffect::reconstruct_branches(double *in_low,
392 WaveletFilters *recon_filter,
395 // take input data and filters and form two branches of half the
396 // original length. length of branches is returned
397 convolve_int_2(in_low, in_length, recon_filter->h,
398 recon_filter->length, 0, output);
399 convolve_int_2(in_high, in_length, recon_filter->g,
400 recon_filter->length, 1, output);
401 return in_length * 2;
404 int DenoiseEffect::wavelet_reconstruction(double **in_data,
411 in_length = in_length >> levels;
412 // destination of all but last branch reconstruction is the next
413 // higher intermediate approximation
414 for(i = levels - 1; i > 0; i--)
416 output = in_data[2 * (i - 1)];
417 in_length = reconstruct_branches(in_data[2 * i],
418 in_data[(2 * i) + 1],
424 // destination of the last branch reconstruction is the output data
425 reconstruct_branches(in_data[0],
434 void DenoiseEffect::process_window()
437 for(j = 0; j < iterations; j++)
439 wavelet_decomposition(dsp_in, window_size, ex_coeff_d->values);
441 tree_copy(ex_coeff_r->values, ex_coeff_d->values, window_size, levels);
442 tree_copy(ex_coeff_rn->values, ex_coeff_d->values, window_size, levels);
445 //printf("DenoiseEffect::process_window %f\n", config.level);
446 threshold(window_size, config.level * 10.0, levels);
448 wavelet_reconstruction(ex_coeff_r->values, window_size, dsp_iteration);
449 wavelet_reconstruction(ex_coeff_rn->values, window_size, dsp_in);
451 for(i = 0; i < window_size; i++)
452 dsp_out[i] += dsp_iteration[i];
459 int DenoiseEffect::process_realtime(int64_t size, double *input_ptr, double *output_ptr)
461 load_configuration();
465 int64_t size_factor = (int)(pow(2, levels));
466 dsp_in = new double[window_size * size_factor];
467 dsp_out = new double[window_size * 2];
468 dsp_iteration = new double[window_size * 2];
471 ex_coeff_d = new Tree(window_size, levels);
472 ex_coeff_r = new Tree(window_size, levels);
473 ex_coeff_rn = new Tree(window_size, levels);
474 wave_coeff_d = new WaveletCoeffs(alpha, beta);
475 wave_coeff_r = new WaveletCoeffs(alpha, beta);
476 decomp_filter = new WaveletFilters(wave_coeff_d, DECOMP);
477 recon_filter = new WaveletFilters(wave_coeff_r, RECON);
478 in_scale = 65535 / sqrt(window_size) / iterations;
479 out_scale = output_level / 65535 * sqrt(window_size);
483 // Append input buffer
484 if(input_size + size > input_allocation)
486 double *new_input = new double[input_size + size];
489 memcpy(new_input, input_buffer, sizeof(double) * input_size);
490 delete [] input_buffer;
492 input_buffer = new_input;
493 input_allocation = input_size + size;
495 memcpy(input_buffer + input_size,
497 size * sizeof(double));
501 // Have enough to do some windows
502 while(input_size >= window_size)
505 for(int i = 0; i < window_size; i++)
507 dsp_in[i] = input_buffer[i] * in_scale;
509 bzero(dsp_out, sizeof(double) * window_size);
516 // First window produces garbage
526 // Crossfade into the output buffer
527 int64_t new_allocation = output_size + window_size;
528 if(new_allocation > output_allocation)
530 double *new_output = new double[new_allocation];
534 memcpy(new_output, output_buffer, sizeof(double) * output_size);
535 //printf("CrossfadeFFT::process_fifo 1 %p\n", output_buffer);
536 delete [] output_buffer;
537 //printf("CrossfadeFFT::process_fifo 2\n");
539 output_buffer = new_output;
540 output_allocation = new_allocation;
543 if(output_size >= WINDOW_BORDER)
545 for(int i = 0, j = output_size - WINDOW_BORDER;
549 double src_level = (double)i / WINDOW_BORDER;
550 double dst_level = (double)(WINDOW_BORDER - i) / WINDOW_BORDER;
551 output_buffer[j] = output_buffer[j] * dst_level + out_scale * dsp_out[i] * src_level;
554 for(int i = 0; i < window_size - WINDOW_BORDER; i++)
555 output_buffer[output_size + i] = dsp_out[WINDOW_BORDER + i] * out_scale;
556 output_size += window_size - WINDOW_BORDER;
560 // First buffer has no crossfade
561 memcpy(output_buffer + output_size,
563 sizeof(double) * window_size);
564 output_size += window_size;
568 // Shift input buffer forward
569 for(int i = window_size - WINDOW_BORDER, j = 0;
572 input_buffer[j] = input_buffer[i];
573 input_size -= window_size - WINDOW_BORDER;
577 // Have enough to send to output
578 if(output_size - WINDOW_BORDER >= size)
580 memcpy(output_ptr, output_buffer, sizeof(double) * size);
581 for(int i = size, j = 0; i < output_size; i++, j++)
582 output_buffer[j] = output_buffer[i];
587 //printf("DenoiseEffect::process_realtime 1\n");
588 bzero(output_ptr, sizeof(double) * size);
600 Tree::Tree(int input_length, int levels)
602 this->input_length = input_length;
603 this->levels = levels;
606 // create decomposition tree
607 values = new double*[2 * levels];
609 for (i = 0; i < levels; i++)
617 values[2 * i] = new double[j + 5];
618 values[2 * i + 1] = new double[j + 5];
626 for (i = 2 * levels - 1; i >= 0; i--)
632 WaveletCoeffs::WaveletCoeffs(double alpha, double beta)
635 double tcosa = cos(alpha);
636 double tcosb = cos(beta);
637 double tsina = sin(alpha);
638 double tsinb = sin(beta);
640 // calculate first two wavelet coefficients a = a(-2) and b = a(-1)
641 values[0] = ((1.0 + tcosa + tsina) * (1.0 - tcosb - tsinb)
642 + 2.0 * tsinb * tcosa) / 4.0;
643 values[1] = ((1.0 - tcosa + tsina) * (1.0 + tcosb - tsinb)
644 - 2.0 * tsinb * tcosa) / 4.0;
646 tcosa = cos(alpha - beta);
647 tsina = sin(alpha - beta);
649 // calculate last four wavelet coefficients c = a(0), d = a(1),
650 // e = a(2), and f = a(3)
651 values[2] = (1.0 + tcosa + tsina) / 2.0;
652 values[3] = (1.0 + tcosa - tsina) / 2.0;
653 values[4] = 1 - values[0] - values[2];
654 values[5] = 1 - values[1] - values[3];
656 // zero out very small coefficient values caused by truncation error
657 for (i = 0; i < 6; i++)
659 if (fabs(values[i]) < 1.0e-15) values[i] = 0.0;
663 WaveletCoeffs::~WaveletCoeffs()
668 WaveletFilters::WaveletFilters(WaveletCoeffs *wave_coeffs, wavetype transform)
672 // find the first non-zero wavelet coefficient
674 while(wave_coeffs->values[i] == 0.0) i++;
676 // find the last non-zero wavelet coefficient
678 while(wave_coeffs->values[j] == 0.0) j--;
680 // Form the decomposition filters h~ and g~ or the reconstruction
681 // filters h and g. The division by 2 in the construction
682 // of the decomposition filters is for normalization.
684 for(k = 0; k < length; k++)
686 if (transform == DECOMP)
688 h[k] = wave_coeffs->values[j--] / 2.0;
689 g[k] = (double) (((i++ & 0x01) * 2) - 1) * wave_coeffs->values[i] / 2.0;
693 h[k] = wave_coeffs->values[i++];
694 g[k] = (double) (((j-- & 0x01) * 2) - 1) * wave_coeffs->values[j];
698 // clear out the additional array locations, if any
706 WaveletFilters::~WaveletFilters()
718 DenoiseConfig::DenoiseConfig()
723 void DenoiseConfig::copy_from(DenoiseConfig &that)
728 int DenoiseConfig::equivalent(DenoiseConfig &that)
730 return EQUIV(level, that.level);
733 void DenoiseConfig::interpolate(DenoiseConfig &prev,
737 int64_t current_frame)
739 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
740 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
741 this->level = prev.level * prev_scale + next.level * next_scale;
753 PLUGIN_THREAD_OBJECT(DenoiseEffect, DenoiseThread, DenoiseWindow)
764 DenoiseWindow::DenoiseWindow(DenoiseEffect *plugin, int x, int y)
765 : BC_Window(plugin->gui_string,
776 this->plugin = plugin;
779 void DenoiseWindow::create_objects()
783 add_subwindow(new BC_Title(x, y, _("Level:")));
785 add_subwindow(scale = new DenoiseLevel(plugin, x, y));
790 int DenoiseWindow::close_event()
792 // Set result to 1 to indicate a client side close
797 void DenoiseWindow::update()
799 scale->update(plugin->config.level);
813 DenoiseLevel::DenoiseLevel(DenoiseEffect *plugin, int x, int y)
814 : BC_FPot(x, y, (float)plugin->config.level, 0, 1.0)
816 this->plugin = plugin;
820 int DenoiseLevel::handle_event()
822 plugin->config.level = get_value();
823 plugin->send_configure_change();