FFmpeg
af_afftdn.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 The FFmpeg Project
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <float.h>
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/tx.h"
28 #include "avfilter.h"
29 #include "audio.h"
30 #include "filters.h"
31 
32 #define C (M_LN10 * 0.1)
33 #define SOLVE_SIZE (5)
34 #define NB_PROFILE_BANDS (15)
35 
41 };
42 
43 enum OutModes {
48 };
49 
56 };
57 
58 enum NoiseType {
64 };
65 
66 typedef struct DeNoiseChannel {
70  double *amt;
71  double *band_amt;
72  double *band_excit;
73  double *gain;
74  double *smoothed_gain;
75  double *prior;
77  double *clean_data;
78  double *noisy_data;
79  double *out_samples;
80  double *spread_function;
81  double *abs_var;
82  double *rel_var;
83  double *min_abs_var;
84  void *fft_in;
85  void *fft_out;
88 
93 
96  double noise_floor;
100  double max_gain;
101  double max_var;
102  double gain_scale;
104 
105 typedef struct AudioFFTDeNoiseContext {
106  const AVClass *class;
107 
108  int format;
109  size_t sample_size;
111 
113  float noise_floor;
121  float ratio;
125 
126  int channels;
130  float sample_rate;
138 
140 
141  int *bin2band;
142  double *window;
143  double *band_alpha;
144  double *band_beta;
145 
147 
149 
151  double floor;
152  double sample_floor;
153 
161 
162 #define OFFSET(x) offsetof(AudioFFTDeNoiseContext, x)
163 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
164 #define AFR AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
165 
166 static const AVOption afftdn_options[] = {
167  { "noise_reduction", "set the noise reduction",OFFSET(noise_reduction), AV_OPT_TYPE_FLOAT,{.dbl = 12}, .01, 97, AFR },
168  { "nr", "set the noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_FLOAT, {.dbl = 12}, .01, 97, AFR },
169  { "noise_floor", "set the noise floor",OFFSET(noise_floor), AV_OPT_TYPE_FLOAT, {.dbl =-50}, -80,-20, AFR },
170  { "nf", "set the noise floor", OFFSET(noise_floor), AV_OPT_TYPE_FLOAT, {.dbl =-50}, -80,-20, AFR },
171  { "noise_type", "set the noise type", OFFSET(noise_type), AV_OPT_TYPE_INT, {.i64 = WHITE_NOISE}, WHITE_NOISE, NB_NOISE-1, AF, .unit = "type" },
172  { "nt", "set the noise type", OFFSET(noise_type), AV_OPT_TYPE_INT, {.i64 = WHITE_NOISE}, WHITE_NOISE, NB_NOISE-1, AF, .unit = "type" },
173  { "white", "white noise", 0, AV_OPT_TYPE_CONST, {.i64 = WHITE_NOISE}, 0, 0, AF, .unit = "type" },
174  { "w", "white noise", 0, AV_OPT_TYPE_CONST, {.i64 = WHITE_NOISE}, 0, 0, AF, .unit = "type" },
175  { "vinyl", "vinyl noise", 0, AV_OPT_TYPE_CONST, {.i64 = VINYL_NOISE}, 0, 0, AF, .unit = "type" },
176  { "v", "vinyl noise", 0, AV_OPT_TYPE_CONST, {.i64 = VINYL_NOISE}, 0, 0, AF, .unit = "type" },
177  { "shellac", "shellac noise", 0, AV_OPT_TYPE_CONST, {.i64 = SHELLAC_NOISE}, 0, 0, AF, .unit = "type" },
178  { "s", "shellac noise", 0, AV_OPT_TYPE_CONST, {.i64 = SHELLAC_NOISE}, 0, 0, AF, .unit = "type" },
179  { "custom", "custom noise", 0, AV_OPT_TYPE_CONST, {.i64 = CUSTOM_NOISE}, 0, 0, AF, .unit = "type" },
180  { "c", "custom noise", 0, AV_OPT_TYPE_CONST, {.i64 = CUSTOM_NOISE}, 0, 0, AF, .unit = "type" },
181  { "band_noise", "set the custom bands noise", OFFSET(band_noise_str), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, AF },
182  { "bn", "set the custom bands noise", OFFSET(band_noise_str), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, AF },
183  { "residual_floor", "set the residual floor",OFFSET(residual_floor), AV_OPT_TYPE_FLOAT, {.dbl =-38}, -80,-20, AFR },
184  { "rf", "set the residual floor", OFFSET(residual_floor), AV_OPT_TYPE_FLOAT, {.dbl =-38}, -80,-20, AFR },
185  { "track_noise", "track noise", OFFSET(track_noise), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AFR },
186  { "tn", "track noise", OFFSET(track_noise), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AFR },
187  { "track_residual", "track residual", OFFSET(track_residual), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AFR },
188  { "tr", "track residual", OFFSET(track_residual), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AFR },
189  { "output_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64 = OUT_MODE}, 0, NB_MODES-1, AFR, .unit = "mode" },
190  { "om", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64 = OUT_MODE}, 0, NB_MODES-1, AFR, .unit = "mode" },
191  { "input", "input", 0, AV_OPT_TYPE_CONST, {.i64 = IN_MODE}, 0, 0, AFR, .unit = "mode" },
192  { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64 = IN_MODE}, 0, 0, AFR, .unit = "mode" },
193  { "output", "output", 0, AV_OPT_TYPE_CONST, {.i64 = OUT_MODE}, 0, 0, AFR, .unit = "mode" },
194  { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64 = OUT_MODE}, 0, 0, AFR, .unit = "mode" },
195  { "noise", "noise", 0, AV_OPT_TYPE_CONST, {.i64 = NOISE_MODE}, 0, 0, AFR, .unit = "mode" },
196  { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64 = NOISE_MODE}, 0, 0, AFR, .unit = "mode" },
197  { "adaptivity", "set adaptivity factor",OFFSET(ratio), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0, 1, AFR },
198  { "ad", "set adaptivity factor",OFFSET(ratio), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0, 1, AFR },
199  { "floor_offset", "set noise floor offset factor",OFFSET(floor_offset), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, -2, 2, AFR },
200  { "fo", "set noise floor offset factor",OFFSET(floor_offset), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, -2, 2, AFR },
201  { "noise_link", "set the noise floor link",OFFSET(noise_floor_link),AV_OPT_TYPE_INT,{.i64 = MIN_LINK}, 0, NB_LINK-1, AFR, .unit = "link" },
202  { "nl", "set the noise floor link", OFFSET(noise_floor_link),AV_OPT_TYPE_INT,{.i64 = MIN_LINK}, 0, NB_LINK-1, AFR, .unit = "link" },
203  { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = NONE_LINK}, 0, 0, AFR, .unit = "link" },
204  { "min", "min", 0, AV_OPT_TYPE_CONST, {.i64 = MIN_LINK}, 0, 0, AFR, .unit = "link" },
205  { "max", "max", 0, AV_OPT_TYPE_CONST, {.i64 = MAX_LINK}, 0, 0, AFR, .unit = "link" },
206  { "average", "average", 0, AV_OPT_TYPE_CONST, {.i64 = AVERAGE_LINK}, 0, 0, AFR, .unit = "link" },
207  { "band_multiplier", "set band multiplier",OFFSET(band_multiplier), AV_OPT_TYPE_FLOAT,{.dbl = 1.25}, 0.2,5, AF },
208  { "bm", "set band multiplier", OFFSET(band_multiplier), AV_OPT_TYPE_FLOAT,{.dbl = 1.25}, 0.2,5, AF },
209  { "sample_noise", "set sample noise mode",OFFSET(sample_noise_mode),AV_OPT_TYPE_INT,{.i64 = SAMPLE_NONE}, 0, NB_SAMPLEMODES-1, AFR, .unit = "sample" },
210  { "sn", "set sample noise mode",OFFSET(sample_noise_mode),AV_OPT_TYPE_INT,{.i64 = SAMPLE_NONE}, 0, NB_SAMPLEMODES-1, AFR, .unit = "sample" },
211  { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_NONE}, 0, 0, AFR, .unit = "sample" },
212  { "start", "start", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_START}, 0, 0, AFR, .unit = "sample" },
213  { "begin", "start", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_START}, 0, 0, AFR, .unit = "sample" },
214  { "stop", "stop", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_STOP}, 0, 0, AFR, .unit = "sample" },
215  { "end", "stop", 0, AV_OPT_TYPE_CONST, {.i64 = SAMPLE_STOP}, 0, 0, AFR, .unit = "sample" },
216  { "gain_smooth", "set gain smooth radius",OFFSET(gain_smooth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 50, AFR },
217  { "gs", "set gain smooth radius",OFFSET(gain_smooth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 50, AFR },
218  { NULL }
219 };
220 
221 AVFILTER_DEFINE_CLASS(afftdn);
222 
224  int band, double a,
225  double b, double c)
226 {
227  double d1, d2, d3;
228 
229  d1 = a / s->band_centre[band];
230  d1 = 10.0 * log(1.0 + d1 * d1) / M_LN10;
231  d2 = b / s->band_centre[band];
232  d2 = 10.0 * log(1.0 + d2 * d2) / M_LN10;
233  d3 = s->band_centre[band] / c;
234  d3 = 10.0 * log(1.0 + d3 * d3) / M_LN10;
235 
236  return -d1 + d2 - d3;
237 }
238 
239 static void factor(double *array, int size)
240 {
241  for (int i = 0; i < size - 1; i++) {
242  for (int j = i + 1; j < size; j++) {
243  double d = array[j + i * size] / array[i + i * size];
244 
245  array[j + i * size] = d;
246  for (int k = i + 1; k < size; k++) {
247  array[j + k * size] -= d * array[i + k * size];
248  }
249  }
250  }
251 }
252 
253 static void solve(double *matrix, double *vector, int size)
254 {
255  for (int i = 0; i < size - 1; i++) {
256  for (int j = i + 1; j < size; j++) {
257  double d = matrix[j + i * size];
258  vector[j] -= d * vector[i];
259  }
260  }
261 
262  vector[size - 1] /= matrix[size * size - 1];
263 
264  for (int i = size - 2; i >= 0; i--) {
265  double d = vector[i];
266  for (int j = i + 1; j < size; j++)
267  d -= matrix[i + j * size] * vector[j];
268  vector[i] = d / matrix[i + i * size];
269  }
270 }
271 
273  DeNoiseChannel *dnch,
274  int band)
275 {
276  double product, sum, f;
277  int i = 0;
278 
279  if (band < NB_PROFILE_BANDS)
280  return dnch->band_noise[band];
281 
282  for (int j = 0; j < SOLVE_SIZE; j++) {
283  sum = 0.0;
284  for (int k = 0; k < NB_PROFILE_BANDS; k++)
285  sum += s->matrix_b[i++] * dnch->band_noise[k];
286  s->vector_b[j] = sum;
287  }
288 
289  solve(s->matrix_a, s->vector_b, SOLVE_SIZE);
290  f = (0.5 * s->sample_rate) / s->band_centre[NB_PROFILE_BANDS-1];
291  f = 15.0 + log(f / 1.5) / log(1.5);
292  sum = 0.0;
293  product = 1.0;
294  for (int j = 0; j < SOLVE_SIZE; j++) {
295  sum += product * s->vector_b[j];
296  product *= f;
297  }
298 
299  return sum;
300 }
301 
302 static double limit_gain(double a, double b)
303 {
304  if (a > 1.0)
305  return (b * a - 1.0) / (b + a - 2.0);
306  if (a < 1.0)
307  return (b * a - 2.0 * a + 1.0) / (b - a);
308  return 1.0;
309 }
310 
311 static void spectral_flatness(AudioFFTDeNoiseContext *s, const double *const spectral,
312  double floor, int len, double *rnum, double *rden)
313 {
314  double num = 0., den = 0.;
315  int size = 0;
316 
317  for (int n = 0; n < len; n++) {
318  const double v = spectral[n];
319  if (v > floor) {
320  num += log(v);
321  den += v;
322  size++;
323  }
324  }
325 
326  size = FFMAX(size, 1);
327 
328  num /= size;
329  den /= size;
330 
331  num = exp(num);
332 
333  *rnum = num;
334  *rden = den;
335 }
336 
337 static void set_parameters(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int update_var, int update_auto_var);
338 
339 static double floor_offset(const double *S, int size, double mean)
340 {
341  double offset = 0.0;
342 
343  for (int n = 0; n < size; n++) {
344  const double p = S[n] - mean;
345 
346  offset = fmax(offset, fabs(p));
347  }
348 
349  return offset / mean;
350 }
351 
354  double *prior, double *prior_band_excit, int track_noise)
355 {
356  AVFilterLink *outlink = ctx->outputs[0];
357  const double *abs_var = dnch->abs_var;
358  const double ratio = outlink->frame_count_out ? s->ratio : 1.0;
359  const double rratio = 1. - ratio;
360  const int *bin2band = s->bin2band;
361  double *noisy_data = dnch->noisy_data;
362  double *band_excit = dnch->band_excit;
363  double *band_amt = dnch->band_amt;
364  double *smoothed_gain = dnch->smoothed_gain;
365  AVComplexDouble *fft_data_dbl = dnch->fft_out;
366  AVComplexFloat *fft_data_flt = dnch->fft_out;
367  double *gain = dnch->gain;
368 
369  for (int i = 0; i < s->bin_count; i++) {
370  double sqr_new_gain, new_gain, power, mag, mag_abs_var, new_mag_abs_var;
371 
372  switch (s->format) {
373  case AV_SAMPLE_FMT_FLTP:
374  noisy_data[i] = mag = hypot(fft_data_flt[i].re, fft_data_flt[i].im);
375  break;
376  case AV_SAMPLE_FMT_DBLP:
377  noisy_data[i] = mag = hypot(fft_data_dbl[i].re, fft_data_dbl[i].im);
378  break;
379  default:
380  av_assert2(0);
381  }
382 
383  power = mag * mag;
384  mag_abs_var = power / abs_var[i];
385  new_mag_abs_var = ratio * prior[i] + rratio * fmax(mag_abs_var - 1.0, 0.0);
386  new_gain = new_mag_abs_var / (1.0 + new_mag_abs_var);
387  sqr_new_gain = new_gain * new_gain;
388  prior[i] = mag_abs_var * sqr_new_gain;
389  dnch->clean_data[i] = power * sqr_new_gain;
390  gain[i] = new_gain;
391  }
392 
393  if (track_noise) {
394  double flatness, num, den;
395 
396  spectral_flatness(s, noisy_data, s->floor, s->bin_count, &num, &den);
397 
398  flatness = num / den;
399  if (flatness > 0.8) {
400  const double offset = s->floor_offset * floor_offset(noisy_data, s->bin_count, den);
401  const double new_floor = av_clipd(10.0 * log10(den) - 100.0 + offset, -90., -20.);
402 
403  dnch->noise_floor = 0.1 * new_floor + dnch->noise_floor * 0.9;
404  set_parameters(s, dnch, 1, 1);
405  }
406  }
407 
408  for (int i = 0; i < s->number_of_bands; i++) {
409  band_excit[i] = 0.0;
410  band_amt[i] = 0.0;
411  }
412 
413  for (int i = 0; i < s->bin_count; i++)
414  band_excit[bin2band[i]] += dnch->clean_data[i];
415 
416  for (int i = 0; i < s->number_of_bands; i++) {
417  band_excit[i] = fmax(band_excit[i],
418  s->band_alpha[i] * band_excit[i] +
419  s->band_beta[i] * prior_band_excit[i]);
420  prior_band_excit[i] = band_excit[i];
421  }
422 
423  for (int j = 0, i = 0; j < s->number_of_bands; j++) {
424  for (int k = 0; k < s->number_of_bands; k++) {
425  band_amt[j] += dnch->spread_function[i++] * band_excit[k];
426  }
427  }
428 
429  for (int i = 0; i < s->bin_count; i++)
430  dnch->amt[i] = band_amt[bin2band[i]];
431 
432  for (int i = 0; i < s->bin_count; i++) {
433  if (dnch->amt[i] > abs_var[i]) {
434  gain[i] = 1.0;
435  } else if (dnch->amt[i] > dnch->min_abs_var[i]) {
436  const double limit = sqrt(abs_var[i] / dnch->amt[i]);
437 
438  gain[i] = limit_gain(gain[i], limit);
439  } else {
440  gain[i] = limit_gain(gain[i], dnch->max_gain);
441  }
442  }
443 
444  memcpy(smoothed_gain, gain, s->bin_count * sizeof(*smoothed_gain));
445  if (s->gain_smooth > 0) {
446  const int r = s->gain_smooth;
447 
448  for (int i = r; i < s->bin_count - r; i++) {
449  const double gc = gain[i];
450  double num = 0., den = 0.;
451 
452  for (int j = -r; j <= r; j++) {
453  const double g = gain[i + j];
454  const double d = 1. - fabs(g - gc);
455 
456  num += g * d;
457  den += d;
458  }
459 
460  smoothed_gain[i] = num / den;
461  }
462  }
463 
464  switch (s->format) {
465  case AV_SAMPLE_FMT_FLTP:
466  for (int i = 0; i < s->bin_count; i++) {
467  const float new_gain = smoothed_gain[i];
468 
469  fft_data_flt[i].re *= new_gain;
470  fft_data_flt[i].im *= new_gain;
471  }
472  break;
473  case AV_SAMPLE_FMT_DBLP:
474  for (int i = 0; i < s->bin_count; i++) {
475  const double new_gain = smoothed_gain[i];
476 
477  fft_data_dbl[i].re *= new_gain;
478  fft_data_dbl[i].im *= new_gain;
479  }
480  break;
481  }
482 }
483 
484 static double freq2bark(double x)
485 {
486  double d = x / 7500.0;
487 
488  return 13.0 * atan(7.6E-4 * x) + 3.5 * atan(d * d);
489 }
490 
492 {
493  if (band == -1)
494  return lrint(s->band_centre[0] / 1.5);
495 
496  return s->band_centre[band];
497 }
498 
499 static int get_band_edge(AudioFFTDeNoiseContext *s, int band)
500 {
501  int i;
502 
503  if (band == NB_PROFILE_BANDS) {
504  i = lrint(s->band_centre[NB_PROFILE_BANDS - 1] * 1.224745);
505  } else {
506  i = lrint(s->band_centre[band] / 1.224745);
507  }
508 
509  return FFMIN(i, s->sample_rate / 2);
510 }
511 
513  DeNoiseChannel *dnch)
514 {
515  double band_noise, d2, d3, d4, d5;
516  int i = 0, j = 0, k = 0;
517 
518  d5 = 0.0;
519  band_noise = process_get_band_noise(s, dnch, 0);
520  for (int m = j; m < s->bin_count; m++) {
521  if (m == j) {
522  i = j;
523  d5 = band_noise;
524  if (k >= NB_PROFILE_BANDS) {
525  j = s->bin_count;
526  } else {
527  j = s->fft_length * get_band_centre(s, k) / s->sample_rate;
528  }
529  d2 = j - i;
530  band_noise = process_get_band_noise(s, dnch, k);
531  k++;
532  }
533  d3 = (j - m) / d2;
534  d4 = (m - i) / d2;
535  dnch->rel_var[m] = exp((d5 * d3 + band_noise * d4) * C);
536  }
537 
538  for (i = 0; i < NB_PROFILE_BANDS; i++)
539  dnch->noise_band_auto_var[i] = dnch->max_var * exp((process_get_band_noise(s, dnch, i) - 2.0) * C);
540 }
541 
543 {
544  DeNoiseChannel *dnch = &s->dnch[ch];
545  char *custom_noise_str, *p, *arg, *saveptr = NULL;
546  double band_noise[NB_PROFILE_BANDS] = { 0.f };
547  int ret;
548 
549  if (!s->band_noise_str)
550  return;
551 
552  custom_noise_str = p = av_strdup(s->band_noise_str);
553  if (!p)
554  return;
555 
556  for (int i = 0; i < NB_PROFILE_BANDS; i++) {
557  float noise;
558 
559  if (!(arg = av_strtok(p, "| ", &saveptr)))
560  break;
561 
562  p = NULL;
563 
564  ret = av_sscanf(arg, "%f", &noise);
565  if (ret != 1) {
566  av_log(s, AV_LOG_ERROR, "Custom band noise must be float.\n");
567  break;
568  }
569 
570  band_noise[i] = av_clipd(noise, -24., 24.);
571  }
572 
573  av_free(custom_noise_str);
574  memcpy(dnch->band_noise, band_noise, sizeof(band_noise));
575 }
576 
577 static void set_parameters(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int update_var, int update_auto_var)
578 {
579  if (dnch->last_noise_floor != dnch->noise_floor)
580  dnch->last_noise_floor = dnch->noise_floor;
581 
582  if (s->track_residual)
584 
585  dnch->max_var = s->floor * exp((100.0 + dnch->last_noise_floor) * C);
586  if (update_auto_var) {
587  for (int i = 0; i < NB_PROFILE_BANDS; i++)
588  dnch->noise_band_auto_var[i] = dnch->max_var * exp((process_get_band_noise(s, dnch, i) - 2.0) * C);
589  }
590 
591  if (s->track_residual) {
592  if (update_var || dnch->last_residual_floor != dnch->residual_floor) {
593  update_var = 1;
594  dnch->last_residual_floor = dnch->residual_floor;
595  dnch->last_noise_reduction = fmax(dnch->last_noise_floor - dnch->last_residual_floor + 100., 0);
596  dnch->max_gain = exp(dnch->last_noise_reduction * (0.5 * C));
597  }
598  } else if (update_var || dnch->noise_reduction != dnch->last_noise_reduction) {
599  update_var = 1;
601  dnch->last_residual_floor = av_clipd(dnch->last_noise_floor - dnch->last_noise_reduction, -80, -20);
602  dnch->max_gain = exp(dnch->last_noise_reduction * (0.5 * C));
603  }
604 
605  dnch->gain_scale = 1.0 / (dnch->max_gain * dnch->max_gain);
606 
607  if (update_var) {
608  set_band_parameters(s, dnch);
609 
610  for (int i = 0; i < s->bin_count; i++) {
611  dnch->abs_var[i] = fmax(dnch->max_var * dnch->rel_var[i], 1.0);
612  dnch->min_abs_var[i] = dnch->gain_scale * dnch->abs_var[i];
613  }
614  }
615 }
616 
617 static void reduce_mean(double *band_noise)
618 {
619  double mean = 0.f;
620 
621  for (int i = 0; i < NB_PROFILE_BANDS; i++)
622  mean += band_noise[i];
624 
625  for (int i = 0; i < NB_PROFILE_BANDS; i++)
626  band_noise[i] -= mean;
627 }
628 
630 {
631  AVFilterContext *ctx = inlink->dst;
632  AudioFFTDeNoiseContext *s = ctx->priv;
633  double wscale, sar, sum, sdiv;
634  int i, j, k, m, n, ret, tx_type;
635  double dscale = 1.;
636  float fscale = 1.f;
637  void *scale;
638 
639  s->format = inlink->format;
640 
641  switch (s->format) {
642  case AV_SAMPLE_FMT_FLTP:
643  s->sample_size = sizeof(float);
644  s->complex_sample_size = sizeof(AVComplexFloat);
645  tx_type = AV_TX_FLOAT_RDFT;
646  scale = &fscale;
647  break;
648  case AV_SAMPLE_FMT_DBLP:
649  s->sample_size = sizeof(double);
650  s->complex_sample_size = sizeof(AVComplexDouble);
651  tx_type = AV_TX_DOUBLE_RDFT;
652  scale = &dscale;
653  break;
654  }
655 
656  s->dnch = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->dnch));
657  if (!s->dnch)
658  return AVERROR(ENOMEM);
659 
660  s->channels = inlink->ch_layout.nb_channels;
661  s->sample_rate = inlink->sample_rate;
662  s->sample_advance = s->sample_rate / 80;
663  s->window_length = 3 * s->sample_advance;
664  s->fft_length2 = 1 << (32 - ff_clz(s->window_length));
665  s->fft_length = s->fft_length2;
666  s->buffer_length = s->fft_length * 2;
667  s->bin_count = s->fft_length2 / 2 + 1;
668 
669  s->band_centre[0] = 80;
670  for (i = 1; i < NB_PROFILE_BANDS; i++) {
671  s->band_centre[i] = lrint(1.5 * s->band_centre[i - 1] + 5.0);
672  if (s->band_centre[i] < 1000) {
673  s->band_centre[i] = 10 * (s->band_centre[i] / 10);
674  } else if (s->band_centre[i] < 5000) {
675  s->band_centre[i] = 50 * ((s->band_centre[i] + 20) / 50);
676  } else if (s->band_centre[i] < 15000) {
677  s->band_centre[i] = 100 * ((s->band_centre[i] + 45) / 100);
678  } else {
679  s->band_centre[i] = 1000 * ((s->band_centre[i] + 495) / 1000);
680  }
681  }
682 
683  for (j = 0; j < SOLVE_SIZE; j++) {
684  for (k = 0; k < SOLVE_SIZE; k++) {
685  s->matrix_a[j + k * SOLVE_SIZE] = 0.0;
686  for (m = 0; m < NB_PROFILE_BANDS; m++)
687  s->matrix_a[j + k * SOLVE_SIZE] += pow(m, j + k);
688  }
689  }
690 
691  factor(s->matrix_a, SOLVE_SIZE);
692 
693  i = 0;
694  for (j = 0; j < SOLVE_SIZE; j++)
695  for (k = 0; k < NB_PROFILE_BANDS; k++)
696  s->matrix_b[i++] = pow(k, j);
697 
698  i = 0;
699  for (j = 0; j < NB_PROFILE_BANDS; j++)
700  for (k = 0; k < SOLVE_SIZE; k++)
701  s->matrix_c[i++] = pow(j, k);
702 
703  s->window = av_calloc(s->window_length, sizeof(*s->window));
704  s->bin2band = av_calloc(s->bin_count, sizeof(*s->bin2band));
705  if (!s->window || !s->bin2band)
706  return AVERROR(ENOMEM);
707 
708  sdiv = s->band_multiplier;
709  for (i = 0; i < s->bin_count; i++)
710  s->bin2band[i] = lrint(sdiv * freq2bark((0.5 * i * s->sample_rate) / s->fft_length2));
711 
712  s->number_of_bands = s->bin2band[s->bin_count - 1] + 1;
713 
714  s->band_alpha = av_calloc(s->number_of_bands, sizeof(*s->band_alpha));
715  s->band_beta = av_calloc(s->number_of_bands, sizeof(*s->band_beta));
716  if (!s->band_alpha || !s->band_beta)
717  return AVERROR(ENOMEM);
718 
719  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
720  DeNoiseChannel *dnch = &s->dnch[ch];
721 
722  switch (s->noise_type) {
723  case WHITE_NOISE:
724  for (i = 0; i < NB_PROFILE_BANDS; i++)
725  dnch->band_noise[i] = 0.;
726  break;
727  case VINYL_NOISE:
728  for (i = 0; i < NB_PROFILE_BANDS; i++)
729  dnch->band_noise[i] = get_band_noise(s, i, 50.0, 500.5, 2125.0);
730  break;
731  case SHELLAC_NOISE:
732  for (i = 0; i < NB_PROFILE_BANDS; i++)
733  dnch->band_noise[i] = get_band_noise(s, i, 1.0, 500.0, 1.0E10);
734  break;
735  case CUSTOM_NOISE:
736  read_custom_noise(s, ch);
737  break;
738  default:
739  return AVERROR_BUG;
740  }
741 
742  reduce_mean(dnch->band_noise);
743 
744  dnch->amt = av_calloc(s->bin_count, sizeof(*dnch->amt));
745  dnch->band_amt = av_calloc(s->number_of_bands, sizeof(*dnch->band_amt));
746  dnch->band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->band_excit));
747  dnch->gain = av_calloc(s->bin_count, sizeof(*dnch->gain));
748  dnch->smoothed_gain = av_calloc(s->bin_count, sizeof(*dnch->smoothed_gain));
749  dnch->prior = av_calloc(s->bin_count, sizeof(*dnch->prior));
750  dnch->prior_band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->prior_band_excit));
751  dnch->clean_data = av_calloc(s->bin_count, sizeof(*dnch->clean_data));
752  dnch->noisy_data = av_calloc(s->bin_count, sizeof(*dnch->noisy_data));
753  dnch->out_samples = av_calloc(s->buffer_length, sizeof(*dnch->out_samples));
754  dnch->abs_var = av_calloc(s->bin_count, sizeof(*dnch->abs_var));
755  dnch->rel_var = av_calloc(s->bin_count, sizeof(*dnch->rel_var));
756  dnch->min_abs_var = av_calloc(s->bin_count, sizeof(*dnch->min_abs_var));
757  dnch->fft_in = av_calloc(s->fft_length2, s->sample_size);
758  dnch->fft_out = av_calloc(s->fft_length2 + 1, s->complex_sample_size);
759  ret = av_tx_init(&dnch->fft, &dnch->tx_fn, tx_type, 0, s->fft_length2, scale, 0);
760  if (ret < 0)
761  return ret;
762  ret = av_tx_init(&dnch->ifft, &dnch->itx_fn, tx_type, 1, s->fft_length2, scale, 0);
763  if (ret < 0)
764  return ret;
765  dnch->spread_function = av_calloc(s->number_of_bands * s->number_of_bands,
766  sizeof(*dnch->spread_function));
767 
768  if (!dnch->amt ||
769  !dnch->band_amt ||
770  !dnch->band_excit ||
771  !dnch->gain ||
772  !dnch->smoothed_gain ||
773  !dnch->prior ||
774  !dnch->prior_band_excit ||
775  !dnch->clean_data ||
776  !dnch->noisy_data ||
777  !dnch->out_samples ||
778  !dnch->fft_in ||
779  !dnch->fft_out ||
780  !dnch->abs_var ||
781  !dnch->rel_var ||
782  !dnch->min_abs_var ||
783  !dnch->spread_function ||
784  !dnch->fft ||
785  !dnch->ifft)
786  return AVERROR(ENOMEM);
787  }
788 
789  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
790  DeNoiseChannel *dnch = &s->dnch[ch];
791  double *prior_band_excit = dnch->prior_band_excit;
792  double min, max;
793  double p1, p2;
794 
795  p1 = pow(0.1, 2.5 / sdiv);
796  p2 = pow(0.1, 1.0 / sdiv);
797  j = 0;
798  for (m = 0; m < s->number_of_bands; m++) {
799  for (n = 0; n < s->number_of_bands; n++) {
800  if (n < m) {
801  dnch->spread_function[j++] = pow(p2, m - n);
802  } else if (n > m) {
803  dnch->spread_function[j++] = pow(p1, n - m);
804  } else {
805  dnch->spread_function[j++] = 1.0;
806  }
807  }
808  }
809 
810  for (m = 0; m < s->number_of_bands; m++) {
811  dnch->band_excit[m] = 0.0;
812  prior_band_excit[m] = 0.0;
813  }
814 
815  for (m = 0; m < s->bin_count; m++)
816  dnch->band_excit[s->bin2band[m]] += 1.0;
817 
818  j = 0;
819  for (m = 0; m < s->number_of_bands; m++) {
820  for (n = 0; n < s->number_of_bands; n++)
821  prior_band_excit[m] += dnch->spread_function[j++] * dnch->band_excit[n];
822  }
823 
824  min = pow(0.1, 2.5);
825  max = pow(0.1, 1.0);
826  for (int i = 0; i < s->number_of_bands; i++) {
827  if (i < lrint(12.0 * sdiv)) {
828  dnch->band_excit[i] = pow(0.1, 1.45 + 0.1 * i / sdiv);
829  } else {
830  dnch->band_excit[i] = pow(0.1, 2.5 - 0.2 * (i / sdiv - 14.0));
831  }
832  dnch->band_excit[i] = av_clipd(dnch->band_excit[i], min, max);
833  }
834 
835  for (int i = 0; i < s->buffer_length; i++)
836  dnch->out_samples[i] = 0;
837 
838  j = 0;
839  for (int i = 0; i < s->number_of_bands; i++)
840  for (int k = 0; k < s->number_of_bands; k++)
841  dnch->spread_function[j++] *= dnch->band_excit[i] / prior_band_excit[i];
842  }
843 
844  j = 0;
845  sar = s->sample_advance / s->sample_rate;
846  for (int i = 0; i < s->bin_count; i++) {
847  if ((i == s->fft_length2) || (s->bin2band[i] > j)) {
848  double d6 = (i - 1) * s->sample_rate / s->fft_length;
849  double d7 = fmin(0.008 + 2.2 / d6, 0.03);
850  s->band_alpha[j] = exp(-sar / d7);
851  s->band_beta[j] = 1.0 - s->band_alpha[j];
852  j = s->bin2band[i];
853  }
854  }
855 
856  s->winframe = ff_get_audio_buffer(inlink, s->window_length);
857  if (!s->winframe)
858  return AVERROR(ENOMEM);
859 
860  wscale = sqrt(8.0 / (9.0 * s->fft_length));
861  sum = 0.0;
862  for (int i = 0; i < s->window_length; i++) {
863  double d10 = sin(i * M_PI / s->window_length);
864  d10 *= wscale * d10;
865  s->window[i] = d10;
866  sum += d10 * d10;
867  }
868 
869  s->window_weight = 0.5 * sum;
870  s->floor = (1LL << 48) * exp(-23.025558369790467) * s->window_weight;
871  s->sample_floor = s->floor * exp(4.144600506562284);
872 
873  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
874  DeNoiseChannel *dnch = &s->dnch[ch];
875 
876  dnch->noise_reduction = s->noise_reduction;
877  dnch->noise_floor = s->noise_floor;
878  dnch->residual_floor = s->residual_floor;
879 
880  set_parameters(s, dnch, 1, 1);
881  }
882 
883  s->noise_band_edge[0] = FFMIN(s->fft_length2, s->fft_length * get_band_edge(s, 0) / s->sample_rate);
884  i = 0;
885  for (int j = 1; j < NB_PROFILE_BANDS + 1; j++) {
886  s->noise_band_edge[j] = FFMIN(s->fft_length2, s->fft_length * get_band_edge(s, j) / s->sample_rate);
887  if (s->noise_band_edge[j] > lrint(1.1 * s->noise_band_edge[j - 1]))
888  i++;
889  s->noise_band_edge[NB_PROFILE_BANDS + 1] = i;
890  }
891  s->noise_band_count = s->noise_band_edge[NB_PROFILE_BANDS + 1];
892 
893  return 0;
894 }
895 
897 {
898  for (int i = 0; i < NB_PROFILE_BANDS; i++) {
899  dnch->noise_band_norm[i] = 0.0;
900  dnch->noise_band_avr[i] = 0.0;
901  dnch->noise_band_avi[i] = 0.0;
902  dnch->noise_band_var[i] = 0.0;
903  }
904 }
905 
907  DeNoiseChannel *dnch,
908  AVFrame *in, int ch)
909 {
910  double *src_dbl = (double *)in->extended_data[ch];
911  float *src_flt = (float *)in->extended_data[ch];
912  double mag2, var = 0.0, avr = 0.0, avi = 0.0;
913  AVComplexDouble *fft_out_dbl = dnch->fft_out;
914  AVComplexFloat *fft_out_flt = dnch->fft_out;
915  double *fft_in_dbl = dnch->fft_in;
916  float *fft_in_flt = dnch->fft_in;
917  int edge, j, k, n, edgemax;
918 
919  switch (s->format) {
920  case AV_SAMPLE_FMT_FLTP:
921  for (int i = 0; i < s->window_length; i++)
922  fft_in_flt[i] = s->window[i] * src_flt[i] * (1LL << 23);
923 
924  for (int i = s->window_length; i < s->fft_length2; i++)
925  fft_in_flt[i] = 0.f;
926  break;
927  case AV_SAMPLE_FMT_DBLP:
928  for (int i = 0; i < s->window_length; i++)
929  fft_in_dbl[i] = s->window[i] * src_dbl[i] * (1LL << 23);
930 
931  for (int i = s->window_length; i < s->fft_length2; i++)
932  fft_in_dbl[i] = 0.;
933  break;
934  }
935 
936  dnch->tx_fn(dnch->fft, dnch->fft_out, dnch->fft_in, s->sample_size);
937 
938  edge = s->noise_band_edge[0];
939  j = edge;
940  k = 0;
941  n = j;
942  edgemax = fmin(s->fft_length2, s->noise_band_edge[NB_PROFILE_BANDS]);
943  for (int i = j; i <= edgemax; i++) {
944  if ((i == j) && (i < edgemax)) {
945  if (j > edge) {
946  dnch->noise_band_norm[k - 1] += j - edge;
947  dnch->noise_band_avr[k - 1] += avr;
948  dnch->noise_band_avi[k - 1] += avi;
949  dnch->noise_band_var[k - 1] += var;
950  }
951  k++;
952  edge = j;
953  j = s->noise_band_edge[k];
954  if (k == NB_PROFILE_BANDS) {
955  j++;
956  }
957  var = 0.0;
958  avr = 0.0;
959  avi = 0.0;
960  }
961 
962  switch (s->format) {
963  case AV_SAMPLE_FMT_FLTP:
964  avr += fft_out_flt[n].re;
965  avi += fft_out_flt[n].im;
966  mag2 = fft_out_flt[n].re * fft_out_flt[n].re +
967  fft_out_flt[n].im * fft_out_flt[n].im;
968  break;
969  case AV_SAMPLE_FMT_DBLP:
970  avr += fft_out_dbl[n].re;
971  avi += fft_out_dbl[n].im;
972  mag2 = fft_out_dbl[n].re * fft_out_dbl[n].re +
973  fft_out_dbl[n].im * fft_out_dbl[n].im;
974  break;
975  default:
976  av_assert2(0);
977  }
978 
979  mag2 = fmax(mag2, s->sample_floor);
980 
981  var += mag2;
982  n++;
983  }
984 
985  dnch->noise_band_norm[k - 1] += j - edge;
986  dnch->noise_band_avr[k - 1] += avr;
987  dnch->noise_band_avi[k - 1] += avi;
988  dnch->noise_band_var[k - 1] += var;
989 }
990 
992  DeNoiseChannel *dnch,
993  double *sample_noise)
994 {
995  for (int i = 0; i < s->noise_band_count; i++) {
996  dnch->noise_band_avr[i] /= dnch->noise_band_norm[i];
997  dnch->noise_band_avi[i] /= dnch->noise_band_norm[i];
998  dnch->noise_band_var[i] /= dnch->noise_band_norm[i];
999  dnch->noise_band_var[i] -= dnch->noise_band_avr[i] * dnch->noise_band_avr[i] +
1000  dnch->noise_band_avi[i] * dnch->noise_band_avi[i];
1001  dnch->noise_band_auto_var[i] = dnch->noise_band_var[i];
1002  sample_noise[i] = 10.0 * log10(dnch->noise_band_var[i] / s->floor) - 100.0;
1003  }
1004  if (s->noise_band_count < NB_PROFILE_BANDS) {
1005  for (int i = s->noise_band_count; i < NB_PROFILE_BANDS; i++)
1006  sample_noise[i] = sample_noise[i - 1];
1007  }
1008 }
1009 
1011  DeNoiseChannel *dnch,
1012  double *sample_noise)
1013 {
1014  double new_band_noise[NB_PROFILE_BANDS];
1015  double temp[NB_PROFILE_BANDS];
1016  double sum = 0.0;
1017 
1018  for (int m = 0; m < NB_PROFILE_BANDS; m++)
1019  temp[m] = sample_noise[m];
1020 
1021  for (int m = 0, i = 0; m < SOLVE_SIZE; m++) {
1022  sum = 0.0;
1023  for (int n = 0; n < NB_PROFILE_BANDS; n++)
1024  sum += s->matrix_b[i++] * temp[n];
1025  s->vector_b[m] = sum;
1026  }
1027  solve(s->matrix_a, s->vector_b, SOLVE_SIZE);
1028  for (int m = 0, i = 0; m < NB_PROFILE_BANDS; m++) {
1029  sum = 0.0;
1030  for (int n = 0; n < SOLVE_SIZE; n++)
1031  sum += s->matrix_c[i++] * s->vector_b[n];
1032  temp[m] = sum;
1033  }
1034 
1035  reduce_mean(temp);
1036 
1037  av_log(s, AV_LOG_INFO, "bn=");
1038  for (int m = 0; m < NB_PROFILE_BANDS; m++) {
1039  new_band_noise[m] = temp[m];
1040  new_band_noise[m] = av_clipd(new_band_noise[m], -24.0, 24.0);
1041  av_log(s, AV_LOG_INFO, "%f ", new_band_noise[m]);
1042  }
1043  av_log(s, AV_LOG_INFO, "\n");
1044  memcpy(dnch->band_noise, new_band_noise, sizeof(new_band_noise));
1045 }
1046 
1047 static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1048 {
1049  AudioFFTDeNoiseContext *s = ctx->priv;
1050  AVFrame *in = arg;
1051  const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
1052  const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
1053  const int window_length = s->window_length;
1054  const double *window = s->window;
1055 
1056  for (int ch = start; ch < end; ch++) {
1057  DeNoiseChannel *dnch = &s->dnch[ch];
1058  const double *src_dbl = (const double *)in->extended_data[ch];
1059  const float *src_flt = (const float *)in->extended_data[ch];
1060  double *dst = dnch->out_samples;
1061  double *fft_in_dbl = dnch->fft_in;
1062  float *fft_in_flt = dnch->fft_in;
1063 
1064  switch (s->format) {
1065  case AV_SAMPLE_FMT_FLTP:
1066  for (int m = 0; m < window_length; m++)
1067  fft_in_flt[m] = window[m] * src_flt[m] * (1LL << 23);
1068 
1069  for (int m = window_length; m < s->fft_length2; m++)
1070  fft_in_flt[m] = 0.f;
1071  break;
1072  case AV_SAMPLE_FMT_DBLP:
1073  for (int m = 0; m < window_length; m++)
1074  fft_in_dbl[m] = window[m] * src_dbl[m] * (1LL << 23);
1075 
1076  for (int m = window_length; m < s->fft_length2; m++)
1077  fft_in_dbl[m] = 0.;
1078  break;
1079  }
1080 
1081  dnch->tx_fn(dnch->fft, dnch->fft_out, dnch->fft_in, s->sample_size);
1082 
1083  process_frame(ctx, s, dnch,
1084  dnch->prior,
1085  dnch->prior_band_excit,
1086  s->track_noise);
1087 
1088  dnch->itx_fn(dnch->ifft, dnch->fft_in, dnch->fft_out, s->complex_sample_size);
1089 
1090  switch (s->format) {
1091  case AV_SAMPLE_FMT_FLTP:
1092  for (int m = 0; m < window_length; m++)
1093  dst[m] += s->window[m] * fft_in_flt[m] / (1LL << 23);
1094  break;
1095  case AV_SAMPLE_FMT_DBLP:
1096  for (int m = 0; m < window_length; m++)
1097  dst[m] += s->window[m] * fft_in_dbl[m] / (1LL << 23);
1098  break;
1099  }
1100  }
1101 
1102  return 0;
1103 }
1104 
1106 {
1107  AVFilterContext *ctx = inlink->dst;
1108  AVFilterLink *outlink = ctx->outputs[0];
1109  AudioFFTDeNoiseContext *s = ctx->priv;
1110  const int output_mode = ctx->is_disabled ? IN_MODE : s->output_mode;
1111  const int offset = s->window_length - s->sample_advance;
1112  AVFrame *out;
1113 
1114  for (int ch = 0; ch < s->channels; ch++) {
1115  uint8_t *src = (uint8_t *)s->winframe->extended_data[ch];
1116 
1117  memmove(src, src + s->sample_advance * s->sample_size,
1118  offset * s->sample_size);
1119  memcpy(src + offset * s->sample_size, in->extended_data[ch],
1120  in->nb_samples * s->sample_size);
1121  memset(src + s->sample_size * (offset + in->nb_samples), 0,
1122  (s->sample_advance - in->nb_samples) * s->sample_size);
1123  }
1124 
1125  if (s->track_noise) {
1126  double average = 0.0, min = DBL_MAX, max = -DBL_MAX;
1127 
1128  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
1129  DeNoiseChannel *dnch = &s->dnch[ch];
1130 
1131  average += dnch->noise_floor;
1132  max = fmax(max, dnch->noise_floor);
1133  min = fmin(min, dnch->noise_floor);
1134  }
1135 
1136  average /= inlink->ch_layout.nb_channels;
1137 
1138  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
1139  DeNoiseChannel *dnch = &s->dnch[ch];
1140 
1141  switch (s->noise_floor_link) {
1142  case MIN_LINK: dnch->noise_floor = min; break;
1143  case MAX_LINK: dnch->noise_floor = max; break;
1144  case AVERAGE_LINK: dnch->noise_floor = average; break;
1145  case NONE_LINK:
1146  default:
1147  break;
1148  }
1149 
1150  if (dnch->noise_floor != dnch->last_noise_floor)
1151  set_parameters(s, dnch, 1, 0);
1152  }
1153  }
1154 
1155  if (s->sample_noise_mode == SAMPLE_START) {
1156  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
1157  DeNoiseChannel *dnch = &s->dnch[ch];
1158 
1159  init_sample_noise(dnch);
1160  }
1161  s->sample_noise_mode = SAMPLE_NONE;
1162  s->sample_noise = 1;
1163  s->sample_noise_blocks = 0;
1164  }
1165 
1166  if (s->sample_noise) {
1167  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
1168  DeNoiseChannel *dnch = &s->dnch[ch];
1169 
1170  sample_noise_block(s, dnch, s->winframe, ch);
1171  }
1172  s->sample_noise_blocks++;
1173  }
1174 
1175  if (s->sample_noise_mode == SAMPLE_STOP) {
1176  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
1177  DeNoiseChannel *dnch = &s->dnch[ch];
1178  double sample_noise[NB_PROFILE_BANDS];
1179 
1180  if (s->sample_noise_blocks <= 0)
1181  break;
1182  finish_sample_noise(s, dnch, sample_noise);
1183  set_noise_profile(s, dnch, sample_noise);
1184  set_parameters(s, dnch, 1, 1);
1185  }
1186  s->sample_noise = 0;
1187  s->sample_noise_blocks = 0;
1188  s->sample_noise_mode = SAMPLE_NONE;
1189  }
1190 
1191  ff_filter_execute(ctx, filter_channel, s->winframe, NULL,
1193 
1194  if (av_frame_is_writable(in)) {
1195  out = in;
1196  } else {
1197  out = ff_get_audio_buffer(outlink, in->nb_samples);
1198  if (!out) {
1199  av_frame_free(&in);
1200  return AVERROR(ENOMEM);
1201  }
1202 
1203  av_frame_copy_props(out, in);
1204  }
1205 
1206  for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
1207  DeNoiseChannel *dnch = &s->dnch[ch];
1208  double *src = dnch->out_samples;
1209  const double *orig_dbl = (const double *)s->winframe->extended_data[ch];
1210  const float *orig_flt = (const float *)s->winframe->extended_data[ch];
1211  double *dst_dbl = (double *)out->extended_data[ch];
1212  float *dst_flt = (float *)out->extended_data[ch];
1213 
1214  switch (output_mode) {
1215  case IN_MODE:
1216  switch (s->format) {
1217  case AV_SAMPLE_FMT_FLTP:
1218  for (int m = 0; m < out->nb_samples; m++)
1219  dst_flt[m] = orig_flt[m];
1220  break;
1221  case AV_SAMPLE_FMT_DBLP:
1222  for (int m = 0; m < out->nb_samples; m++)
1223  dst_dbl[m] = orig_dbl[m];
1224  break;
1225  }
1226  break;
1227  case OUT_MODE:
1228  switch (s->format) {
1229  case AV_SAMPLE_FMT_FLTP:
1230  for (int m = 0; m < out->nb_samples; m++)
1231  dst_flt[m] = src[m];
1232  break;
1233  case AV_SAMPLE_FMT_DBLP:
1234  for (int m = 0; m < out->nb_samples; m++)
1235  dst_dbl[m] = src[m];
1236  break;
1237  }
1238  break;
1239  case NOISE_MODE:
1240  switch (s->format) {
1241  case AV_SAMPLE_FMT_FLTP:
1242  for (int m = 0; m < out->nb_samples; m++)
1243  dst_flt[m] = orig_flt[m] - src[m];
1244  break;
1245  case AV_SAMPLE_FMT_DBLP:
1246  for (int m = 0; m < out->nb_samples; m++)
1247  dst_dbl[m] = orig_dbl[m] - src[m];
1248  break;
1249  }
1250  break;
1251  default:
1252  if (in != out)
1253  av_frame_free(&in);
1254  av_frame_free(&out);
1255  return AVERROR_BUG;
1256  }
1257 
1258  memmove(src, src + s->sample_advance, (s->window_length - s->sample_advance) * sizeof(*src));
1259  memset(src + (s->window_length - s->sample_advance), 0, s->sample_advance * sizeof(*src));
1260  }
1261 
1262  if (out != in)
1263  av_frame_free(&in);
1264  return ff_filter_frame(outlink, out);
1265 }
1266 
1268 {
1269  AVFilterLink *inlink = ctx->inputs[0];
1270  AVFilterLink *outlink = ctx->outputs[0];
1271  AudioFFTDeNoiseContext *s = ctx->priv;
1272  AVFrame *in = NULL;
1273  int ret;
1274 
1276 
1277  ret = ff_inlink_consume_samples(inlink, s->sample_advance, s->sample_advance, &in);
1278  if (ret < 0)
1279  return ret;
1280  if (ret > 0)
1281  return output_frame(inlink, in);
1282 
1283  if (ff_inlink_queued_samples(inlink) >= s->sample_advance) {
1284  ff_filter_set_ready(ctx, 10);
1285  return 0;
1286  }
1287 
1288  FF_FILTER_FORWARD_STATUS(inlink, outlink);
1289  FF_FILTER_FORWARD_WANTED(outlink, inlink);
1290 
1291  return FFERROR_NOT_READY;
1292 }
1293 
1295 {
1296  AudioFFTDeNoiseContext *s = ctx->priv;
1297 
1298  av_freep(&s->window);
1299  av_freep(&s->bin2band);
1300  av_freep(&s->band_alpha);
1301  av_freep(&s->band_beta);
1302  av_frame_free(&s->winframe);
1303 
1304  if (s->dnch) {
1305  for (int ch = 0; ch < s->channels; ch++) {
1306  DeNoiseChannel *dnch = &s->dnch[ch];
1307  av_freep(&dnch->amt);
1308  av_freep(&dnch->band_amt);
1309  av_freep(&dnch->band_excit);
1310  av_freep(&dnch->gain);
1311  av_freep(&dnch->smoothed_gain);
1312  av_freep(&dnch->prior);
1313  av_freep(&dnch->prior_band_excit);
1314  av_freep(&dnch->clean_data);
1315  av_freep(&dnch->noisy_data);
1316  av_freep(&dnch->out_samples);
1317  av_freep(&dnch->spread_function);
1318  av_freep(&dnch->abs_var);
1319  av_freep(&dnch->rel_var);
1320  av_freep(&dnch->min_abs_var);
1321  av_freep(&dnch->fft_in);
1322  av_freep(&dnch->fft_out);
1323  av_tx_uninit(&dnch->fft);
1324  av_tx_uninit(&dnch->ifft);
1325  }
1326  av_freep(&s->dnch);
1327  }
1328 }
1329 
1330 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
1331  char *res, int res_len, int flags)
1332 {
1333  AudioFFTDeNoiseContext *s = ctx->priv;
1334  int ret = 0;
1335 
1336  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
1337  if (ret < 0)
1338  return ret;
1339 
1340  if (!strcmp(cmd, "sample_noise") || !strcmp(cmd, "sn"))
1341  return 0;
1342 
1343  for (int ch = 0; ch < s->channels; ch++) {
1344  DeNoiseChannel *dnch = &s->dnch[ch];
1345 
1346  dnch->noise_reduction = s->noise_reduction;
1347  dnch->noise_floor = s->noise_floor;
1348  dnch->residual_floor = s->residual_floor;
1349 
1350  set_parameters(s, dnch, 1, 1);
1351  }
1352 
1353  return 0;
1354 }
1355 
1356 static const AVFilterPad inputs[] = {
1357  {
1358  .name = "default",
1359  .type = AVMEDIA_TYPE_AUDIO,
1360  .config_props = config_input,
1361  },
1362 };
1363 
1365  .name = "afftdn",
1366  .description = NULL_IF_CONFIG_SMALL("Denoise audio samples using FFT."),
1367  .priv_size = sizeof(AudioFFTDeNoiseContext),
1368  .priv_class = &afftdn_class,
1369  .activate = activate,
1370  .uninit = uninit,
1374  .process_command = process_command,
1377 };
NB_PROFILE_BANDS
#define NB_PROFILE_BANDS
Definition: af_afftdn.c:34
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:97
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
NB_MODES
@ NB_MODES
Definition: af_afftdn.c:47
DeNoiseChannel::noise_floor
double noise_floor
Definition: af_afftdn.c:96
AudioFFTDeNoiseContext::floor
double floor
Definition: af_afftdn.c:151
DeNoiseChannel::clean_data
double * clean_data
Definition: af_afftdn.c:77
DeNoiseChannel::noise_band_auto_var
double noise_band_auto_var[NB_PROFILE_BANDS]
Definition: af_afftdn.c:68
C
#define C
Definition: af_afftdn.c:32
DeNoiseChannel::ifft
AVTXContext * ifft
Definition: af_afftdn.c:86
r
const char * r
Definition: vf_curves.c:126
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
activate
static int activate(AVFilterContext *ctx)
Definition: af_afftdn.c:1267
inputs
static const AVFilterPad inputs[]
Definition: af_afftdn.c:1356
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
matrix
Definition: vc1dsp.c:42
AVTXContext
Definition: tx_priv.h:235
AudioFFTDeNoiseContext::window_weight
double window_weight
Definition: af_afftdn.c:150
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
ff_clz
#define ff_clz
Definition: intmath.h:143
solve
static void solve(double *matrix, double *vector, int size)
Definition: af_afftdn.c:253
normalize.log
log
Definition: normalize.py:21
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
OutModes
OutModes
Definition: af_aap.c:32
OUT_MODE
@ OUT_MODE
Definition: af_afftdn.c:45
SOLVE_SIZE
#define SOLVE_SIZE
Definition: af_afftdn.c:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
process_frame
static void process_frame(AVFilterContext *ctx, AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *prior, double *prior_band_excit, int track_noise)
Definition: af_afftdn.c:352
sample_noise_block
static void sample_noise_block(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, AVFrame *in, int ch)
Definition: af_afftdn.c:906
AVOption
AVOption.
Definition: opt.h:346
b
#define b
Definition: input.c:41
NONE_LINK
@ NONE_LINK
Definition: af_afftdn.c:51
AVComplexDouble::im
double im
Definition: tx.h:32
float.h
AVComplexFloat
Definition: tx.h:27
max
#define max(a, b)
Definition: cuda_runtime.h:33
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
DeNoiseChannel::max_var
double max_var
Definition: af_afftdn.c:101
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
AudioFFTDeNoiseContext::fft_length2
int fft_length2
Definition: af_afftdn.c:133
AudioFFTDeNoiseContext::vector_b
double vector_b[SOLVE_SIZE]
Definition: af_afftdn.c:157
DeNoiseChannel::band_amt
double * band_amt
Definition: af_afftdn.c:71
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
AudioFFTDeNoiseContext::noise_floor_link
int noise_floor_link
Definition: af_afftdn.c:120
AudioFFTDeNoiseContext::sample_noise_mode
int sample_noise_mode
Definition: af_afftdn.c:129
av_tx_init
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:902
NoiseType
NoiseType
Definition: af_afftdn.c:58
S
#define S(s, c, i)
Definition: flacdsp_template.c:46
DeNoiseChannel::noise_band_norm
double noise_band_norm[NB_PROFILE_BANDS]
Definition: af_afftdn.c:89
factor
static void factor(double *array, int size)
Definition: af_afftdn.c:239
DeNoiseChannel::noise_band_avr
double noise_band_avr[NB_PROFILE_BANDS]
Definition: af_afftdn.c:90
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_afftdn.c:629
AudioFFTDeNoiseContext::residual_floor
float residual_floor
Definition: af_afftdn.c:116
AudioFFTDeNoiseContext::buffer_length
int buffer_length
Definition: af_afftdn.c:131
AVComplexFloat::im
float im
Definition: tx.h:28
window
static SDL_Window * window
Definition: ffplay.c:364
AudioFFTDeNoiseContext::complex_sample_size
size_t complex_sample_size
Definition: af_afftdn.c:110
freq2bark
static double freq2bark(double x)
Definition: af_afftdn.c:484
AudioFFTDeNoiseContext::number_of_bands
int number_of_bands
Definition: af_afftdn.c:137
noise
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise.c:126
DeNoiseChannel::band_noise
double band_noise[NB_PROFILE_BANDS]
Definition: af_afftdn.c:67
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:745
AudioFFTDeNoiseContext::window_length
int window_length
Definition: af_afftdn.c:135
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
avassert.h
lrint
#define lrint
Definition: tablegen.h:53
AudioFFTDeNoiseContext::dnch
DeNoiseChannel * dnch
Definition: af_afftdn.c:146
AudioFFTDeNoiseContext::sample_size
size_t sample_size
Definition: af_afftdn.c:109
DeNoiseChannel::spread_function
double * spread_function
Definition: af_afftdn.c:80
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
OFFSET
#define OFFSET(x)
Definition: af_afftdn.c:162
NoiseLinkType
NoiseLinkType
Definition: af_afftdn.c:50
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:151
float
float
Definition: af_crystalizer.c:121
DeNoiseChannel::last_noise_reduction
double last_noise_reduction
Definition: af_afftdn.c:95
afftdn_options
static const AVOption afftdn_options[]
Definition: af_afftdn.c:166
s
#define s(width, name)
Definition: cbs_vp9.c:198
AudioFFTDeNoiseContext::winframe
AVFrame * winframe
Definition: af_afftdn.c:148
NB_NOISE
@ NB_NOISE
Definition: af_afftdn.c:63
AudioFFTDeNoiseContext::matrix_a
double matrix_a[SOLVE_SIZE *SOLVE_SIZE]
Definition: af_afftdn.c:156
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
g
const char * g
Definition: vf_curves.c:127
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_strtok
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:178
AudioFFTDeNoiseContext
Definition: af_afftdn.c:105
DeNoiseChannel::gain
double * gain
Definition: af_afftdn.c:73
filters.h
DeNoiseChannel::rel_var
double * rel_var
Definition: af_afftdn.c:82
DeNoiseChannel
Definition: af_afftdn.c:66
DeNoiseChannel::min_abs_var
double * min_abs_var
Definition: af_afftdn.c:83
ctx
AVFormatContext * ctx
Definition: movenc.c:48
DeNoiseChannel::noise_band_sample
double noise_band_sample[NB_PROFILE_BANDS]
Definition: af_afftdn.c:69
DeNoiseChannel::fft_out
void * fft_out
Definition: af_afftdn.c:85
AudioFFTDeNoiseContext::sample_noise_blocks
int sample_noise_blocks
Definition: af_afftdn.c:128
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
E
#define E
Definition: avdct.c:32
get_band_edge
static int get_band_edge(AudioFFTDeNoiseContext *s, int band)
Definition: af_afftdn.c:499
arg
const char * arg
Definition: jacosubdec.c:67
AF
#define AF
Definition: af_afftdn.c:163
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:962
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1465
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:679
DeNoiseChannel::last_noise_floor
double last_noise_floor
Definition: af_afftdn.c:97
DeNoiseChannel::max_gain
double max_gain
Definition: af_afftdn.c:100
DeNoiseChannel::residual_floor
double residual_floor
Definition: af_afftdn.c:98
AudioFFTDeNoiseContext::matrix_b
double matrix_b[SOLVE_SIZE *NB_PROFILE_BANDS]
Definition: af_afftdn.c:158
SampleNoiseModes
SampleNoiseModes
Definition: af_afftdn.c:36
AudioFFTDeNoiseContext::ratio
float ratio
Definition: af_afftdn.c:121
filter_channel
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_afftdn.c:1047
AudioFFTDeNoiseContext::noise_type
int noise_type
Definition: af_afftdn.c:114
NB_SAMPLEMODES
@ NB_SAMPLEMODES
Definition: af_afftdn.c:40
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:33
double
double
Definition: af_crystalizer.c:131
set_band_parameters
static void set_band_parameters(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch)
Definition: af_afftdn.c:512
WHITE_NOISE
@ WHITE_NOISE
Definition: af_afftdn.c:59
exp
int8_t exp
Definition: eval.c:74
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
init_sample_noise
static void init_sample_noise(DeNoiseChannel *dnch)
Definition: af_afftdn.c:896
NOISE_MODE
@ NOISE_MODE
Definition: af_afftdn.c:46
read_custom_noise
static void read_custom_noise(AudioFFTDeNoiseContext *s, int ch)
Definition: af_afftdn.c:542
AudioFFTDeNoiseContext::sample_advance
int sample_advance
Definition: af_afftdn.c:136
DeNoiseChannel::last_residual_floor
double last_residual_floor
Definition: af_afftdn.c:99
f
f
Definition: af_crystalizer.c:121
AudioFFTDeNoiseContext::output_mode
int output_mode
Definition: af_afftdn.c:119
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: vvc_intra.c:291
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
NB_LINK
@ NB_LINK
Definition: af_afftdn.c:55
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AudioFFTDeNoiseContext::format
int format
Definition: af_afftdn.c:108
output_frame
static int output_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_afftdn.c:1105
fmin
double fmin(double, double)
hypot
static av_const double hypot(double x, double y)
Definition: libm.h:366
size
int size
Definition: twinvq_data.h:10344
AVComplexFloat::re
float re
Definition: tx.h:28
AudioFFTDeNoiseContext::matrix_c
double matrix_c[SOLVE_SIZE *NB_PROFILE_BANDS]
Definition: af_afftdn.c:159
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:615
AudioFFTDeNoiseContext::track_noise
int track_noise
Definition: af_afftdn.c:117
AudioFFTDeNoiseContext::noise_reduction
float noise_reduction
Definition: af_afftdn.c:112
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:890
AudioFFTDeNoiseContext::window
double * window
Definition: af_afftdn.c:142
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
finish_sample_noise
static void finish_sample_noise(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *sample_noise)
Definition: af_afftdn.c:991
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
VINYL_NOISE
@ VINYL_NOISE
Definition: af_afftdn.c:60
AudioFFTDeNoiseContext::band_alpha
double * band_alpha
Definition: af_afftdn.c:143
limit_gain
static double limit_gain(double a, double b)
Definition: af_afftdn.c:302
MIN_LINK
@ MIN_LINK
Definition: af_afftdn.c:52
AudioFFTDeNoiseContext::channels
int channels
Definition: af_afftdn.c:126
M_PI
#define M_PI
Definition: mathematics.h:67
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:294
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_afftdn.c:1294
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
DeNoiseChannel::out_samples
double * out_samples
Definition: af_afftdn.c:79
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:424
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
DeNoiseChannel::itx_fn
av_tx_fn itx_fn
Definition: af_afftdn.c:87
DeNoiseChannel::fft
AVTXContext * fft
Definition: af_afftdn.c:86
AudioFFTDeNoiseContext::gain_smooth
int gain_smooth
Definition: af_afftdn.c:122
SAMPLE_START
@ SAMPLE_START
Definition: af_afftdn.c:38
DeNoiseChannel::noise_band_avi
double noise_band_avi[NB_PROFILE_BANDS]
Definition: af_afftdn.c:91
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:405
DeNoiseChannel::prior_band_excit
double * prior_band_excit
Definition: af_afftdn.c:76
SAMPLE_NONE
@ SAMPLE_NONE
Definition: af_afftdn.c:37
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:825
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_afftdn.c:1330
SAMPLE_STOP
@ SAMPLE_STOP
Definition: af_afftdn.c:39
DeNoiseChannel::smoothed_gain
double * smoothed_gain
Definition: af_afftdn.c:74
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_TX_DOUBLE_RDFT
@ AV_TX_DOUBLE_RDFT
Definition: tx.h:91
SHELLAC_NOISE
@ SHELLAC_NOISE
Definition: af_afftdn.c:61
len
int len
Definition: vorbis_enc_data.h:426
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1420
AVComplexDouble
Definition: tx.h:31
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AudioFFTDeNoiseContext::track_residual
int track_residual
Definition: af_afftdn.c:118
AudioFFTDeNoiseContext::noise_floor
float noise_floor
Definition: af_afftdn.c:113
limit
static double limit(double x)
Definition: vf_pseudocolor.c:142
AVFilter
Filter definition.
Definition: avfilter.h:166
DeNoiseChannel::amt
double * amt
Definition: af_afftdn.c:70
AVERAGE_LINK
@ AVERAGE_LINK
Definition: af_afftdn.c:54
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:111
ret
ret
Definition: filter_design.txt:187
AVComplexDouble::re
double re
Definition: tx.h:32
MAX_LINK
@ MAX_LINK
Definition: af_afftdn.c:53
get_band_noise
static double get_band_noise(AudioFFTDeNoiseContext *s, int band, double a, double b, double c)
Definition: af_afftdn.c:223
AudioFFTDeNoiseContext::bin2band
int * bin2band
Definition: af_afftdn.c:141
CUSTOM_NOISE
@ CUSTOM_NOISE
Definition: af_afftdn.c:62
IN_MODE
@ IN_MODE
Definition: af_afftdn.c:44
DeNoiseChannel::noisy_data
double * noisy_data
Definition: af_afftdn.c:78
AudioFFTDeNoiseContext::floor_offset
float floor_offset
Definition: af_afftdn.c:124
AV_TX_FLOAT_RDFT
@ AV_TX_FLOAT_RDFT
Real to complex and complex to real DFTs.
Definition: tx.h:90
fmax
double fmax(double, double)
AudioFFTDeNoiseContext::noise_band_count
int noise_band_count
Definition: af_afftdn.c:155
AFR
#define AFR
Definition: af_afftdn.c:164
AudioFFTDeNoiseContext::band_noise_str
char * band_noise_str
Definition: af_afftdn.c:115
DeNoiseChannel::tx_fn
av_tx_fn tx_fn
Definition: af_afftdn.c:87
power
static float power(float r, float g, float b, float max)
Definition: preserve_color.h:45
channel_layout.h
set_parameters
static void set_parameters(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int update_var, int update_auto_var)
Definition: af_afftdn.c:577
process_get_band_noise
static double process_get_band_noise(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int band)
Definition: af_afftdn.c:272
spectral_flatness
static void spectral_flatness(AudioFFTDeNoiseContext *s, const double *const spectral, double floor, int len, double *rnum, double *rden)
Definition: af_afftdn.c:311
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
DeNoiseChannel::band_excit
double * band_excit
Definition: af_afftdn.c:72
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
temp
else temp
Definition: vf_mcdeint.c:263
mean
static float mean(const float *input, int size)
Definition: vf_nnedi.c:862
DeNoiseChannel::noise_reduction
double noise_reduction
Definition: af_afftdn.c:94
AudioFFTDeNoiseContext::band_beta
double * band_beta
Definition: af_afftdn.c:144
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
DeNoiseChannel::noise_band_var
double noise_band_var[NB_PROFILE_BANDS]
Definition: af_afftdn.c:92
AudioFFTDeNoiseContext::band_centre
int band_centre[NB_PROFILE_BANDS]
Definition: af_afftdn.c:139
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(afftdn)
audio.h
M_LN10
#define M_LN10
Definition: mathematics.h:49
DeNoiseChannel::prior
double * prior
Definition: af_afftdn.c:75
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
DeNoiseChannel::fft_in
void * fft_in
Definition: af_afftdn.c:84
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
floor_offset
static double floor_offset(const double *S, int size, double mean)
Definition: af_afftdn.c:339
AudioFFTDeNoiseContext::noise_band_edge
int noise_band_edge[NB_PROFILE_BANDS+2]
Definition: af_afftdn.c:154
d
d
Definition: ffmpeg_filter.c:409
AudioFFTDeNoiseContext::bin_count
int bin_count
Definition: af_afftdn.c:134
reduce_mean
static void reduce_mean(double *band_noise)
Definition: af_afftdn.c:617
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:155
AudioFFTDeNoiseContext::sample_noise
int sample_noise
Definition: af_afftdn.c:127
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AudioFFTDeNoiseContext::fft_length
int fft_length
Definition: af_afftdn.c:132
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AudioFFTDeNoiseContext::sample_floor
double sample_floor
Definition: af_afftdn.c:152
set_noise_profile
static void set_noise_profile(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *sample_noise)
Definition: af_afftdn.c:1010
DeNoiseChannel::abs_var
double * abs_var
Definition: af_afftdn.c:81
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:239
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:134
get_band_centre
static int get_band_centre(AudioFFTDeNoiseContext *s, int band)
Definition: af_afftdn.c:491
AudioFFTDeNoiseContext::sample_rate
float sample_rate
Definition: af_afftdn.c:130
DeNoiseChannel::gain_scale
double gain_scale
Definition: af_afftdn.c:102
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
ff_af_afftdn
const AVFilter ff_af_afftdn
Definition: af_afftdn.c:1364
AudioFFTDeNoiseContext::band_multiplier
float band_multiplier
Definition: af_afftdn.c:123
av_clipd
av_clipd
Definition: af_crystalizer.c:131
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: internal.h:170
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:234
tx.h
min
float min
Definition: vorbis_enc_data.h:429