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

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/af_afftdn.c+26−9
@@ -71,6 +71,7 @@ typedef struct DeNoiseChannel { · 70 unmodified lines
71 double *band_amt;71 double *band_amt;
72 double *band_excit;72 double *band_excit;
73 double *gain;73 double *gain;
74 double *smoothed_gain;
74 double *prior;75 double *prior;
75 double *prior_band_excit;76 double *prior_band_excit;
76 double *clean_data;77 double *clean_data;
@@ -114,7 +115,7 @@ typedef struct AudioFFTDeNoiseContext { · 37 unmodified lines
114 int output_mode;115 int output_mode;
115 int noise_floor_link;116 int noise_floor_link;
116 float ratio;117 float ratio;
117 float gain_smooth;118 int gain_smooth;
118 float band_multiplier;119 float band_multiplier;
119 float floor_offset;120 float floor_offset;
120 121
@@ -208,8 +209,8 @@ static const AVOption afftdn_options[] = { · 87 unmodified lines
208 { "begin", "start", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_START}, 0, 0, AFR, "sample" },209 { "begin", "start", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_START}, 0, 0, AFR, "sample" },
209 { "stop", "stop", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_STOP}, 0, 0, AFR, "sample" },210 { "stop", "stop", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_STOP}, 0, 0, AFR, "sample" },
210 { "end", "stop", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_STOP}, 0, 0, AFR, "sample" },211 { "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 { "gain_smooth", "set gain smooth radius",OFFSET(gain_smooth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 50, AFR },
212 { "gs", "set gain smooth factor",OFFSET(gain_smooth), AV_OPT_TYPE_FLOAT, {.dbl = 1.00}, 0.0001, 1, AFR },213 { "gs", "set gain smooth radius",OFFSET(gain_smooth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 50, AFR },
213 { NULL }214 { NULL }
214};215};
215 216
@@ -357,6 +358,7 @@ static void process_frame(AVFilterContext *ctx, · 141 unmodified lines
357 double *noisy_data = dnch->noisy_data;358 double *noisy_data = dnch->noisy_data;
358 double *band_excit = dnch->band_excit;359 double *band_excit = dnch->band_excit;
359 double *band_amt = dnch->band_amt;360 double *band_amt = dnch->band_amt;
361 double *smoothed_gain = dnch->smoothed_gain;
360 double *gain = dnch->gain;362 double *gain = dnch->gain;
361 363
362 for (int i = 0; i < s->bin_count; i++) {364 for (int i = 0; i < s->bin_count; i++) {
@@ -424,16 +426,28 @@ static void process_frame(AVFilterContext *ctx, · 61 unmodified lines
424 }426 }
425 }427 }
426 428
427 {429 memcpy(smoothed_gain, gain, s->bin_count * sizeof(*smoothed_gain));
428 const double f = s->gain_smooth;430 if (s->gain_smooth > 0) {
429 const double F = 1. - f;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);
430 440
431 for (int i = 1; i < s->bin_count; i++)441 num += g * d;
432 gain[i] = gain[i-1] * F + f * gain[i];442 den += d;
443 }
444
445 smoothed_gain[i] = num / den;
446 }
433 }447 }
434 448
435 for (int i = 0; i < s->bin_count; i++) {449 for (int i = 0; i < s->bin_count; i++) {
436 const double new_gain = gain[i];450 const double new_gain = smoothed_gain[i];
437 451
438 fft_data[i].re *= new_gain;452 fft_data[i].re *= new_gain;
439 fft_data[i].im *= new_gain;453 fft_data[i].im *= new_gain;
@@ -685,6 +699,7 @@ static int config_input(AVFilterLink *inlink) · 245 unmodified lines
685 dnch->band_amt = av_calloc(s->number_of_bands, sizeof(*dnch->band_amt));699 dnch->band_amt = av_calloc(s->number_of_bands, sizeof(*dnch->band_amt));
686 dnch->band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->band_excit));700 dnch->band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->band_excit));
687 dnch->gain = av_calloc(s->bin_count, sizeof(*dnch->gain));701 dnch->gain = av_calloc(s->bin_count, sizeof(*dnch->gain));
702 dnch->smoothed_gain = av_calloc(s->bin_count, sizeof(*dnch->smoothed_gain));
688 dnch->prior = av_calloc(s->bin_count, sizeof(*dnch->prior));703 dnch->prior = av_calloc(s->bin_count, sizeof(*dnch->prior));
689 dnch->prior_band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->prior_band_excit));704 dnch->prior_band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->prior_band_excit));
690 dnch->clean_data = av_calloc(s->bin_count, sizeof(*dnch->clean_data));705 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
708 !dnch->band_amt ||723 !dnch->band_amt ||
709 !dnch->band_excit ||724 !dnch->band_excit ||
710 !dnch->gain ||725 !dnch->gain ||
726 !dnch->smoothed_gain ||
711 !dnch->prior ||727 !dnch->prior ||
712 !dnch->prior_band_excit ||728 !dnch->prior_band_excit ||
713 !dnch->clean_data ||729 !dnch->clean_data ||
@@ -1161,6 +1177,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 447 unmodified lines
1161 av_freep(&dnch->band_amt);1177 av_freep(&dnch->band_amt);
1162 av_freep(&dnch->band_excit);1178 av_freep(&dnch->band_excit);
1163 av_freep(&dnch->gain);1179 av_freep(&dnch->gain);
1180 av_freep(&dnch->smoothed_gain);
1164 av_freep(&dnch->prior);1181 av_freep(&dnch->prior);
1165 av_freep(&dnch->prior_band_excit);1182 av_freep(&dnch->prior_band_excit);
1166 av_freep(&dnch->clean_data);1183 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