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