History

avfilter/af_afftdn: use better approach for gain smoothing

Paul B Mahol committed 4 years ago

Commit fe85afb1 parent eae3b842 files · +30 −12Browse files

Unified · Split

2 files
  1. doc/filters.texi +4 −3
  2. libavfilter/af_afftdn.c +26 −9
doc/filters.texi+4−3
@@ -1392,10 +1392,11 @@ Default value is @code{none}. · 1391 unmodified lines
13921392 @end table
13931393
13941394 @item gain_smooth, gs
1395−Set gain smooth factor, used to lowpass gains applied to each frequency bin.
1395+Set gain smooth spatial radius, used to smooth gains applied to each frequency bin.
13961396 Useful to reduce random music noise artefacts.
1397−Higher values reduce smoothing of gains.
1398−Default value is @code{1.0}.
1397+Higher values increases smoothing of gains.
1398+Allowed range is from @code{0} to @code{50}.
1399+Default value is @code{0}.
13991400 @end table
14001401
14011402 @subsection Commands
libavfilter/af_afftdn.c+26−9
@@ -71,6 +71,7 @@ typedef struct DeNoiseChannel { · 70 unmodified lines
7171 double *band_amt;
7272 double *band_excit;
7373 double *gain;
74+ double *smoothed_gain;
7475 double *prior;
7576 double *prior_band_excit;
7677 double *clean_data;
@@ -114,7 +115,7 @@ typedef struct AudioFFTDeNoiseContext { · 37 unmodified lines
114115 int output_mode;
115116 int noise_floor_link;
116117 float ratio;
117− float gain_smooth;
118+ int gain_smooth;
118119 float band_multiplier;
119120 float floor_offset;
120121
@@ -208,8 +209,8 @@ static const AVOption afftdn_options[] = { · 87 unmodified lines
208209 { "begin", "start", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_START}, 0, 0, AFR, "sample" },
209210 { "stop", "stop", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_STOP}, 0, 0, AFR, "sample" },
210211 { "end", "stop", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_STOP}, 0, 0, AFR, "sample" },
211− { "gain_smooth", "set gain smooth factor",OFFSET(gain_smooth), AV_OPT_TYPE_FLOAT, {.dbl = 1.00}, 0.0001, 1, AFR },
212− { "gs", "set gain smooth factor",OFFSET(gain_smooth), AV_OPT_TYPE_FLOAT, {.dbl = 1.00}, 0.0001, 1, AFR },
212+ { "gain_smooth", "set gain smooth radius",OFFSET(gain_smooth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 50, AFR },
213+ { "gs", "set gain smooth radius",OFFSET(gain_smooth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 50, AFR },
213214 { NULL }
214215 };
215216
@@ -357,6 +358,7 @@ static void process_frame(AVFilterContext *ctx, · 141 unmodified lines
357358 double *noisy_data = dnch->noisy_data;
358359 double *band_excit = dnch->band_excit;
359360 double *band_amt = dnch->band_amt;
361+ double *smoothed_gain = dnch->smoothed_gain;
360362 double *gain = dnch->gain;
361363
362364 for (int i = 0; i < s->bin_count; i++) {
@@ -424,16 +426,28 @@ static void process_frame(AVFilterContext *ctx, · 61 unmodified lines
424426 }
425427 }
426428
427− {
428− const double f = s->gain_smooth;
429− const double F = 1. - f;
429+ memcpy(smoothed_gain, gain, s->bin_count * sizeof(*smoothed_gain));
430+ if (s->gain_smooth > 0) {
431+ const int r = s->gain_smooth;
432+
433+ for (int i = r; i < s->bin_count - r; i++) {
434+ const double gc = gain[i];
435+ double num = 0., den = 0.;
436+
437+ for (int j = -r; j <= r; j++) {
438+ const double g = gain[i + j];
439+ const double d = 1. - fabs(g - gc);
430440
431− for (int i = 1; i < s->bin_count; i++)
432− gain[i] = gain[i-1] * F + f * gain[i];
441+ num += g * d;
442+ den += d;
443+ }
444+
445+ smoothed_gain[i] = num / den;
446+ }
433447 }
434448
435449 for (int i = 0; i < s->bin_count; i++) {
436− const double new_gain = gain[i];
450+ const double new_gain = smoothed_gain[i];
437451
438452 fft_data[i].re *= new_gain;
439453 fft_data[i].im *= new_gain;
@@ -685,6 +699,7 @@ static int config_input(AVFilterLink *inlink) · 245 unmodified lines
685699 dnch->band_amt = av_calloc(s->number_of_bands, sizeof(*dnch->band_amt));
686700 dnch->band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->band_excit));
687701 dnch->gain = av_calloc(s->bin_count, sizeof(*dnch->gain));
702+ dnch->smoothed_gain = av_calloc(s->bin_count, sizeof(*dnch->smoothed_gain));
688703 dnch->prior = av_calloc(s->bin_count, sizeof(*dnch->prior));
689704 dnch->prior_band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->prior_band_excit));
690705 dnch->clean_data = av_calloc(s->bin_count, sizeof(*dnch->clean_data));
@@ -708,6 +723,7 @@ static int config_input(AVFilterLink *inlink) · 17 unmodified lines
708723 !dnch->band_amt ||
709724 !dnch->band_excit ||
710725 !dnch->gain ||
726+ !dnch->smoothed_gain ||
711727 !dnch->prior ||
712728 !dnch->prior_band_excit ||
713729 !dnch->clean_data ||
@@ -1161,6 +1177,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 447 unmodified lines
11611177 av_freep(&dnch->band_amt);
11621178 av_freep(&dnch->band_excit);
11631179 av_freep(&dnch->gain);
1180+ av_freep(&dnch->smoothed_gain);
11641181 av_freep(&dnch->prior);
11651182 av_freep(&dnch->prior_band_excit);
11661183 av_freep(&dnch->clean_data);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t