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/audio_fifo.h"
24 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 #include "libavcodec/avfft.h"
28 #include "avfilter.h"
29 #include "audio.h"
30 #include "formats.h"
31 #include "filters.h"
32 
33 #define C (M_LN10 * 0.1)
34 #define RATIO 0.98
35 #define RRATIO (1.0 - RATIO)
36 
37 enum OutModes {
42 };
43 
44 enum NoiseType {
50 };
51 
52 typedef struct DeNoiseChannel {
53  int band_noise[15];
54  double noise_band_auto_var[15];
55  double noise_band_sample[15];
56  double *amt;
57  double *band_amt;
58  double *band_excit;
59  double *gain;
60  double *prior;
62  double *clean_data;
63  double *noisy_data;
64  double *out_samples;
65  double *spread_function;
66  double *abs_var;
67  double *rel_var;
68  double *min_abs_var;
71 
72  double noise_band_norm[15];
73  double noise_band_avr[15];
74  double noise_band_avi[15];
75  double noise_band_var[15];
76 
77  double sfm_threshold;
78  double sfm_alpha;
79  double sfm_results[3];
80  int sfm_fail_flags[512];
83 
84 typedef struct AudioFFTDeNoiseContext {
85  const AVClass *class;
86 
88  float noise_floor;
95 
100  int64_t block_count;
101 
102  int64_t pts;
103  int channels;
107  float sample_rate;
115 
116  int band_centre[15];
117 
118  int *bin2band;
119  double *window;
120  double *band_alpha;
121  double *band_beta;
122 
124 
125  double max_gain;
126  double max_var;
127  double gain_scale;
129  double floor;
130  double sample_floor;
131  double auto_floor;
132 
135  double matrix_a[25];
136  double vector_b[5];
137  double matrix_b[75];
138  double matrix_c[75];
139 
142 
143 #define OFFSET(x) offsetof(AudioFFTDeNoiseContext, x)
144 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
145 #define AFR AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
146 
147 static const AVOption afftdn_options[] = {
148  { "nr", "set the noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_FLOAT, {.dbl = 12}, .01, 97, AFR },
149  { "nf", "set the noise floor", OFFSET(noise_floor), AV_OPT_TYPE_FLOAT, {.dbl =-50}, -80,-20, AFR },
150  { "nt", "set the noise type", OFFSET(noise_type), AV_OPT_TYPE_INT, {.i64 = WHITE_NOISE}, WHITE_NOISE, NB_NOISE-1, AF, "type" },
151  { "w", "white noise", 0, AV_OPT_TYPE_CONST, {.i64 = WHITE_NOISE}, 0, 0, AF, "type" },
152  { "v", "vinyl noise", 0, AV_OPT_TYPE_CONST, {.i64 = VINYL_NOISE}, 0, 0, AF, "type" },
153  { "s", "shellac noise", 0, AV_OPT_TYPE_CONST, {.i64 = SHELLAC_NOISE}, 0, 0, AF, "type" },
154  { "c", "custom noise", 0, AV_OPT_TYPE_CONST, {.i64 = CUSTOM_NOISE}, 0, 0, AF, "type" },
155  { "bn", "set the custom bands noise", OFFSET(band_noise_str), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, AF },
156  { "rf", "set the residual floor", OFFSET(residual_floor), AV_OPT_TYPE_FLOAT, {.dbl =-38}, -80,-20, AFR },
157  { "tn", "track noise", OFFSET(track_noise), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AFR },
158  { "tr", "track residual", OFFSET(track_residual), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AFR },
159  { "om", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64 = OUT_MODE}, 0, NB_MODES-1, AFR, "mode" },
160  { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64 = IN_MODE}, 0, 0, AFR, "mode" },
161  { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64 = OUT_MODE}, 0, 0, AFR, "mode" },
162  { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64 = NOISE_MODE}, 0, 0, AFR, "mode" },
163  { NULL }
164 };
165 
166 AVFILTER_DEFINE_CLASS(afftdn);
167 
169  int band, double a,
170  double b, double c)
171 {
172  double d1, d2, d3;
173 
174  d1 = a / s->band_centre[band];
175  d1 = 10.0 * log(1.0 + d1 * d1) / M_LN10;
176  d2 = b / s->band_centre[band];
177  d2 = 10.0 * log(1.0 + d2 * d2) / M_LN10;
178  d3 = s->band_centre[band] / c;
179  d3 = 10.0 * log(1.0 + d3 * d3) / M_LN10;
180 
181  return lrint(-d1 + d2 - d3);
182 }
183 
184 static void factor(double *array, int size)
185 {
186  for (int i = 0; i < size - 1; i++) {
187  for (int j = i + 1; j < size; j++) {
188  double d = array[j + i * size] / array[i + i * size];
189 
190  array[j + i * size] = d;
191  for (int k = i + 1; k < size; k++) {
192  array[j + k * size] -= d * array[i + k * size];
193  }
194  }
195  }
196 }
197 
198 static void solve(double *matrix, double *vector, int size)
199 {
200  for (int i = 0; i < size - 1; i++) {
201  for (int j = i + 1; j < size; j++) {
202  double d = matrix[j + i * size];
203  vector[j] -= d * vector[i];
204  }
205  }
206 
207  vector[size - 1] /= matrix[size * size - 1];
208 
209  for (int i = size - 2; i >= 0; i--) {
210  double d = vector[i];
211  for (int j = i + 1; j < size; j++)
212  d -= matrix[i + j * size] * vector[j];
213  vector[i] = d / matrix[i + i * size];
214  }
215 }
216 
218  DeNoiseChannel *dnch,
219  int band)
220 {
221  double product, sum, f;
222  int i = 0;
223 
224  if (band < 15)
225  return dnch->band_noise[band];
226 
227  for (int j = 0; j < 5; j++) {
228  sum = 0.0;
229  for (int k = 0; k < 15; k++)
230  sum += s->matrix_b[i++] * dnch->band_noise[k];
231  s->vector_b[j] = sum;
232  }
233 
234  solve(s->matrix_a, s->vector_b, 5);
235  f = (0.5 * s->sample_rate) / s->band_centre[14];
236  f = 15.0 + log(f / 1.5) / log(1.5);
237  sum = 0.0;
238  product = 1.0;
239  for (int j = 0; j < 5; j++) {
240  sum += product * s->vector_b[j];
241  product *= f;
242  }
243 
244  return lrint(sum);
245 }
246 
248  DeNoiseChannel *dnch,
249  int start, int end)
250 {
251  double d1 = 0.0, d2 = 1.0;
252  int i = 0, j = 0;
253 
254  for (int k = start; k < end; k++) {
255  if (dnch->noisy_data[k] > s->sample_floor) {
256  j++;
257  d1 += dnch->noisy_data[k];
258  d2 *= dnch->noisy_data[k];
259  if (d2 > 1.0E100) {
260  d2 *= 1.0E-100;
261  i++;
262  } else if (d2 < 1.0E-100) {
263  d2 *= 1.0E100;
264  i--;
265  }
266  }
267  }
268  if (j > 1) {
269  d1 /= j;
270  dnch->sfm_results[0] = d1;
271  d2 = log(d2) + 230.2585 * i;
272  d2 /= j;
273  d1 = log(d1);
274  dnch->sfm_results[1] = d1;
275  dnch->sfm_results[2] = d1 - d2;
276  } else {
277  dnch->sfm_results[0] = s->auto_floor;
278  dnch->sfm_results[1] = dnch->sfm_threshold;
279  dnch->sfm_results[2] = dnch->sfm_threshold;
280  }
281 }
282 
283 static double limit_gain(double a, double b)
284 {
285  if (a > 1.0)
286  return (b * a - 1.0) / (b + a - 2.0);
287  if (a < 1.0)
288  return (b * a - 2.0 * a + 1.0) / (b - a);
289  return 1.0;
290 }
291 
293  FFTComplex *fft_data,
294  double *prior, double *prior_band_excit, int track_noise)
295 {
296  double d1, d2, d3, gain;
297  int n, i1;
298 
299  d1 = fft_data[0].re * fft_data[0].re;
300  dnch->noisy_data[0] = d1;
301  d2 = d1 / dnch->abs_var[0];
302  d3 = RATIO * prior[0] + RRATIO * fmax(d2 - 1.0, 0.0);
303  gain = d3 / (1.0 + d3);
304  gain *= (gain + M_PI_4 / fmax(d2, 1.0E-6));
305  prior[0] = (d2 * gain);
306  dnch->clean_data[0] = (d1 * gain);
307  gain = sqrt(gain);
308  dnch->gain[0] = gain;
309  n = 0;
310  for (int i = 1; i < s->fft_length2; i++) {
311  d1 = fft_data[i].re * fft_data[i].re + fft_data[i].im * fft_data[i].im;
312  if (d1 > s->sample_floor)
313  n = i;
314 
315  dnch->noisy_data[i] = d1;
316  d2 = d1 / dnch->abs_var[i];
317  d3 = RATIO * prior[i] + RRATIO * fmax(d2 - 1.0, 0.0);
318  gain = d3 / (1.0 + d3);
319  gain *= (gain + M_PI_4 / fmax(d2, 1.0E-6));
320  prior[i] = d2 * gain;
321  dnch->clean_data[i] = d1 * gain;
322  gain = sqrt(gain);
323  dnch->gain[i] = gain;
324  }
325  d1 = fft_data[0].im * fft_data[0].im;
326  if (d1 > s->sample_floor)
327  n = s->fft_length2;
328 
329  dnch->noisy_data[s->fft_length2] = d1;
330  d2 = d1 / dnch->abs_var[s->fft_length2];
331  d3 = RATIO * prior[s->fft_length2] + RRATIO * fmax(d2 - 1.0, 0.0);
332  gain = d3 / (1.0 + d3);
333  gain *= gain + M_PI_4 / fmax(d2, 1.0E-6);
334  prior[s->fft_length2] = d2 * gain;
335  dnch->clean_data[s->fft_length2] = d1 * gain;
336  gain = sqrt(gain);
337  dnch->gain[s->fft_length2] = gain;
338  if (n > s->fft_length2 - 2) {
339  n = s->bin_count;
340  i1 = s->noise_band_count;
341  } else {
342  i1 = 0;
343  for (int i = 0; i <= s->noise_band_count; i++) {
344  if (n > 1.1 * s->noise_band_edge[i]) {
345  i1 = i;
346  }
347  }
348  }
349 
350  if (track_noise && (i1 > s->noise_band_count / 2)) {
351  int j = FFMIN(n, s->noise_band_edge[i1]);
352  int m = 3, k;
353 
354  for (k = i1 - 1; k >= 0; k--) {
355  int i = s->noise_band_edge[k];
356  calculate_sfm(s, dnch, i, j);
357  dnch->noise_band_sample[k] = dnch->sfm_results[0];
358  if (dnch->sfm_results[2] + 0.013 * m * fmax(0.0, dnch->sfm_results[1] - 20.53) >= dnch->sfm_threshold) {
359  break;
360  }
361  j = i;
362  m++;
363  }
364 
365  if (k < i1 - 1) {
366  double sum = 0.0, min, max;
367  int i;
368 
369  for (i = i1 - 1; i > k; i--) {
370  min = log(dnch->noise_band_sample[i] / dnch->noise_band_auto_var[i]);
371  sum += min;
372  }
373 
374  i = i1 - k - 1;
375  if (i < 5) {
376  min = 3.0E-4 * i * i;
377  } else {
378  min = 3.0E-4 * (8 * i - 16);
379  }
380  if (i < 3) {
381  max = 2.0E-4 * i * i;
382  } else {
383  max = 2.0E-4 * (4 * i - 4);
384  }
385 
386  if (s->track_residual) {
387  if (s->last_noise_floor > s->last_residual_floor + 9) {
388  min *= 0.5;
389  max *= 0.75;
390  } else if (s->last_noise_floor > s->last_residual_floor + 6) {
391  min *= 0.4;
392  max *= 1.0;
393  } else if (s->last_noise_floor > s->last_residual_floor + 4) {
394  min *= 0.3;
395  max *= 1.3;
396  } else if (s->last_noise_floor > s->last_residual_floor + 2) {
397  min *= 0.2;
398  max *= 1.6;
399  } else if (s->last_noise_floor > s->last_residual_floor) {
400  min *= 0.1;
401  max *= 2.0;
402  } else {
403  min = 0.0;
404  max *= 2.5;
405  }
406  }
407 
408  sum = av_clipd(sum, -min, max);
409  sum = exp(sum);
410  for (int i = 0; i < 15; i++)
411  dnch->noise_band_auto_var[i] *= sum;
412  } else if (dnch->sfm_results[2] >= dnch->sfm_threshold) {
413  dnch->sfm_fail_flags[s->block_count & 0x1FF] = 1;
414  dnch->sfm_fail_total += 1;
415  }
416  }
417 
418  for (int i = 0; i < s->number_of_bands; i++) {
419  dnch->band_excit[i] = 0.0;
420  dnch->band_amt[i] = 0.0;
421  }
422 
423  for (int i = 0; i < s->bin_count; i++) {
424  dnch->band_excit[s->bin2band[i]] += dnch->clean_data[i];
425  }
426 
427  for (int i = 0; i < s->number_of_bands; i++) {
428  dnch->band_excit[i] = fmax(dnch->band_excit[i],
429  s->band_alpha[i] * dnch->band_excit[i] +
430  s->band_beta[i] * prior_band_excit[i]);
431  prior_band_excit[i] = dnch->band_excit[i];
432  }
433 
434  for (int j = 0, i = 0; j < s->number_of_bands; j++) {
435  for (int k = 0; k < s->number_of_bands; k++) {
436  dnch->band_amt[j] += dnch->spread_function[i++] * dnch->band_excit[k];
437  }
438  }
439 
440  for (int i = 0; i < s->bin_count; i++)
441  dnch->amt[i] = dnch->band_amt[s->bin2band[i]];
442 
443  if (dnch->amt[0] > dnch->abs_var[0]) {
444  dnch->gain[0] = 1.0;
445  } else if (dnch->amt[0] > dnch->min_abs_var[0]) {
446  double limit = sqrt(dnch->abs_var[0] / dnch->amt[0]);
447  dnch->gain[0] = limit_gain(dnch->gain[0], limit);
448  } else {
449  dnch->gain[0] = limit_gain(dnch->gain[0], s->max_gain);
450  }
451  if (dnch->amt[s->fft_length2] > dnch->abs_var[s->fft_length2]) {
452  dnch->gain[s->fft_length2] = 1.0;
453  } else if (dnch->amt[s->fft_length2] > dnch->min_abs_var[s->fft_length2]) {
454  double limit = sqrt(dnch->abs_var[s->fft_length2] / dnch->amt[s->fft_length2]);
455  dnch->gain[s->fft_length2] = limit_gain(dnch->gain[s->fft_length2], limit);
456  } else {
457  dnch->gain[s->fft_length2] = limit_gain(dnch->gain[s->fft_length2], s->max_gain);
458  }
459 
460  for (int i = 1; i < s->fft_length2; i++) {
461  if (dnch->amt[i] > dnch->abs_var[i]) {
462  dnch->gain[i] = 1.0;
463  } else if (dnch->amt[i] > dnch->min_abs_var[i]) {
464  double limit = sqrt(dnch->abs_var[i] / dnch->amt[i]);
465  dnch->gain[i] = limit_gain(dnch->gain[i], limit);
466  } else {
467  dnch->gain[i] = limit_gain(dnch->gain[i], s->max_gain);
468  }
469  }
470 
471  gain = dnch->gain[0];
472  dnch->clean_data[0] = (gain * gain * dnch->noisy_data[0]);
473  fft_data[0].re *= gain;
474  gain = dnch->gain[s->fft_length2];
475  dnch->clean_data[s->fft_length2] = (gain * gain * dnch->noisy_data[s->fft_length2]);
476  fft_data[0].im *= gain;
477  for (int i = 1; i < s->fft_length2; i++) {
478  gain = dnch->gain[i];
479  dnch->clean_data[i] = (gain * gain * dnch->noisy_data[i]);
480  fft_data[i].re *= gain;
481  fft_data[i].im *= gain;
482  }
483 }
484 
485 static double freq2bark(double x)
486 {
487  double d = x / 7500.0;
488 
489  return 13.0 * atan(7.6E-4 * x) + 3.5 * atan(d * d);
490 }
491 
493 {
494  if (band == -1)
495  return lrint(s->band_centre[0] / 1.5);
496 
497  return s->band_centre[band];
498 }
499 
500 static int get_band_edge(AudioFFTDeNoiseContext *s, int band)
501 {
502  int i;
503 
504  if (band == 15) {
505  i = lrint(s->band_centre[14] * 1.224745);
506  } else {
507  i = lrint(s->band_centre[band] / 1.224745);
508  }
509 
510  return FFMIN(i, s->sample_rate / 2);
511 }
512 
514  DeNoiseChannel *dnch)
515 {
516  double band_noise, d2, d3, d4, d5;
517  int i = 0, j = 0, k = 0;
518 
519  d5 = 0.0;
520  band_noise = process_get_band_noise(s, dnch, 0);
521  for (int m = j; m <= s->fft_length2; m++) {
522  if (m == j) {
523  i = j;
524  d5 = band_noise;
525  if (k == 15) {
526  j = s->bin_count;
527  } else {
528  j = s->fft_length * get_band_centre(s, k) / s->sample_rate;
529  }
530  d2 = j - i;
531  band_noise = process_get_band_noise(s, dnch, k);
532  k++;
533  }
534  d3 = (j - m) / d2;
535  d4 = (m - i) / d2;
536  dnch->rel_var[m] = exp((d5 * d3 + band_noise * d4) * C);
537  }
538  dnch->rel_var[s->fft_length2] = exp(band_noise * C);
539 
540  for (i = 0; i < 15; i++)
541  dnch->noise_band_auto_var[i] = s->max_var * exp((process_get_band_noise(s, dnch, i) - 2.0) * C);
542 
543  for (i = 0; i <= s->fft_length2; i++) {
544  dnch->abs_var[i] = fmax(s->max_var * dnch->rel_var[i], 1.0);
545  dnch->min_abs_var[i] = s->gain_scale * dnch->abs_var[i];
546  }
547 }
548 
550 {
551  DeNoiseChannel *dnch = &s->dnch[ch];
552  char *p, *arg, *saveptr = NULL;
553  int i, ret, band_noise[15] = { 0 };
554 
555  if (!s->band_noise_str)
556  return;
557 
558  p = av_strdup(s->band_noise_str);
559  if (!p)
560  return;
561 
562  for (i = 0; i < 15; i++) {
563  if (!(arg = av_strtok(p, "| ", &saveptr)))
564  break;
565 
566  p = NULL;
567 
568  ret = av_sscanf(arg, "%d", &band_noise[i]);
569  if (ret != 1) {
570  av_log(s, AV_LOG_ERROR, "Custom band noise must be integer.\n");
571  break;
572  }
573 
574  band_noise[i] = av_clip(band_noise[i], -24, 24);
575  }
576 
577  av_free(p);
578  memcpy(dnch->band_noise, band_noise, sizeof(band_noise));
579 }
580 
582 {
583  if (s->last_noise_floor != s->noise_floor)
584  s->last_noise_floor = s->noise_floor;
585 
586  if (s->track_residual)
587  s->last_noise_floor = fmaxf(s->last_noise_floor, s->residual_floor);
588 
589  s->max_var = s->floor * exp((100.0 + s->last_noise_floor) * C);
590 
591  if (s->track_residual) {
592  s->last_residual_floor = s->residual_floor;
593  s->last_noise_reduction = fmax(s->last_noise_floor - s->last_residual_floor, 0);
594  s->max_gain = exp(s->last_noise_reduction * (0.5 * C));
595  } else if (s->noise_reduction != s->last_noise_reduction) {
596  s->last_noise_reduction = s->noise_reduction;
597  s->last_residual_floor = av_clipf(s->last_noise_floor - s->last_noise_reduction, -80, -20);
598  s->max_gain = exp(s->last_noise_reduction * (0.5 * C));
599  }
600 
601  s->gain_scale = 1.0 / (s->max_gain * s->max_gain);
602 
603  for (int ch = 0; ch < s->channels; ch++) {
604  DeNoiseChannel *dnch = &s->dnch[ch];
605 
606  set_band_parameters(s, dnch);
607  }
608 }
609 
611 {
612  AVFilterContext *ctx = inlink->dst;
613  AudioFFTDeNoiseContext *s = ctx->priv;
614  double wscale, sar, sum, sdiv;
615  int i, j, k, m, n;
616 
617  s->dnch = av_calloc(inlink->channels, sizeof(*s->dnch));
618  if (!s->dnch)
619  return AVERROR(ENOMEM);
620 
621  s->pts = AV_NOPTS_VALUE;
622  s->channels = inlink->channels;
623  s->sample_rate = inlink->sample_rate;
624  s->sample_advance = s->sample_rate / 80;
625  s->window_length = 3 * s->sample_advance;
626  s->fft_length2 = 1 << (32 - ff_clz(s->window_length));
627  s->fft_length = s->fft_length2 * 2;
628  s->buffer_length = s->fft_length * 2;
629  s->bin_count = s->fft_length2 + 1;
630 
631  s->band_centre[0] = 80;
632  for (i = 1; i < 15; i++) {
633  s->band_centre[i] = lrint(1.5 * s->band_centre[i - 1] + 5.0);
634  if (s->band_centre[i] < 1000) {
635  s->band_centre[i] = 10 * (s->band_centre[i] / 10);
636  } else if (s->band_centre[i] < 5000) {
637  s->band_centre[i] = 50 * ((s->band_centre[i] + 20) / 50);
638  } else if (s->band_centre[i] < 15000) {
639  s->band_centre[i] = 100 * ((s->band_centre[i] + 45) / 100);
640  } else {
641  s->band_centre[i] = 1000 * ((s->band_centre[i] + 495) / 1000);
642  }
643  }
644 
645  for (j = 0; j < 5; j++) {
646  for (k = 0; k < 5; k++) {
647  s->matrix_a[j + k * 5] = 0.0;
648  for (m = 0; m < 15; m++)
649  s->matrix_a[j + k * 5] += pow(m, j + k);
650  }
651  }
652 
653  factor(s->matrix_a, 5);
654 
655  i = 0;
656  for (j = 0; j < 5; j++)
657  for (k = 0; k < 15; k++)
658  s->matrix_b[i++] = pow(k, j);
659 
660  i = 0;
661  for (j = 0; j < 15; j++)
662  for (k = 0; k < 5; k++)
663  s->matrix_c[i++] = pow(j, k);
664 
665  s->window = av_calloc(s->window_length, sizeof(*s->window));
666  s->bin2band = av_calloc(s->bin_count, sizeof(*s->bin2band));
667  if (!s->window || !s->bin2band)
668  return AVERROR(ENOMEM);
669 
670  sdiv = s->sample_rate / 17640.0;
671  for (i = 0; i <= s->fft_length2; i++)
672  s->bin2band[i] = lrint(sdiv * freq2bark((0.5 * i * s->sample_rate) / s->fft_length2));
673 
674  s->number_of_bands = s->bin2band[s->fft_length2] + 1;
675 
676  s->band_alpha = av_calloc(s->number_of_bands, sizeof(*s->band_alpha));
677  s->band_beta = av_calloc(s->number_of_bands, sizeof(*s->band_beta));
678  if (!s->band_alpha || !s->band_beta)
679  return AVERROR(ENOMEM);
680 
681  for (int ch = 0; ch < inlink->channels; ch++) {
682  DeNoiseChannel *dnch = &s->dnch[ch];
683 
684  switch (s->noise_type) {
685  case WHITE_NOISE:
686  for (i = 0; i < 15; i++)
687  dnch->band_noise[i] = 0;
688  break;
689  case VINYL_NOISE:
690  for (i = 0; i < 15; i++)
691  dnch->band_noise[i] = get_band_noise(s, i, 50.0, 500.5, 2125.0) + FFMAX(i - 7, 0);
692  break;
693  case SHELLAC_NOISE:
694  for (i = 0; i < 15; i++)
695  dnch->band_noise[i] = get_band_noise(s, i, 1.0, 500.0, 1.0E10) + FFMAX(i - 12, -5);
696  break;
697  case CUSTOM_NOISE:
698  read_custom_noise(s, ch);
699  break;
700  default:
701  return AVERROR_BUG;
702  }
703 
704 
705  dnch->sfm_threshold = 0.8;
706  dnch->sfm_alpha = 0.05;
707  for (i = 0; i < 512; i++)
708  dnch->sfm_fail_flags[i] = 0;
709 
710  dnch->sfm_fail_total = 0;
711  j = FFMAX((int)(10.0 * (1.3 - dnch->sfm_threshold)), 1);
712 
713  for (i = 0; i < 512; i += j) {
714  dnch->sfm_fail_flags[i] = 1;
715  dnch->sfm_fail_total += 1;
716  }
717 
718  dnch->amt = av_calloc(s->bin_count, sizeof(*dnch->amt));
719  dnch->band_amt = av_calloc(s->number_of_bands, sizeof(*dnch->band_amt));
720  dnch->band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->band_excit));
721  dnch->gain = av_calloc(s->bin_count, sizeof(*dnch->gain));
722  dnch->prior = av_calloc(s->bin_count, sizeof(*dnch->prior));
723  dnch->prior_band_excit = av_calloc(s->number_of_bands, sizeof(*dnch->prior_band_excit));
724  dnch->clean_data = av_calloc(s->bin_count, sizeof(*dnch->clean_data));
725  dnch->noisy_data = av_calloc(s->bin_count, sizeof(*dnch->noisy_data));
726  dnch->out_samples = av_calloc(s->buffer_length, sizeof(*dnch->out_samples));
727  dnch->abs_var = av_calloc(s->bin_count, sizeof(*dnch->abs_var));
728  dnch->rel_var = av_calloc(s->bin_count, sizeof(*dnch->rel_var));
729  dnch->min_abs_var = av_calloc(s->bin_count, sizeof(*dnch->min_abs_var));
730  dnch->fft_data = av_calloc(s->fft_length2 + 1, sizeof(*dnch->fft_data));
731  dnch->fft = av_fft_init(av_log2(s->fft_length2), 0);
732  dnch->ifft = av_fft_init(av_log2(s->fft_length2), 1);
733  dnch->spread_function = av_calloc(s->number_of_bands * s->number_of_bands,
734  sizeof(*dnch->spread_function));
735 
736  if (!dnch->amt ||
737  !dnch->band_amt ||
738  !dnch->band_excit ||
739  !dnch->gain ||
740  !dnch->prior ||
741  !dnch->prior_band_excit ||
742  !dnch->clean_data ||
743  !dnch->noisy_data ||
744  !dnch->out_samples ||
745  !dnch->fft_data ||
746  !dnch->abs_var ||
747  !dnch->rel_var ||
748  !dnch->min_abs_var ||
749  !dnch->spread_function ||
750  !dnch->fft ||
751  !dnch->ifft)
752  return AVERROR(ENOMEM);
753  }
754 
755  for (int ch = 0; ch < inlink->channels; ch++) {
756  DeNoiseChannel *dnch = &s->dnch[ch];
757  double *prior_band_excit = dnch->prior_band_excit;
758  double *prior = dnch->prior;
759  double min, max;
760  double p1, p2;
761 
762  p1 = pow(0.1, 2.5 / sdiv);
763  p2 = pow(0.1, 1.0 / sdiv);
764  j = 0;
765  for (m = 0; m < s->number_of_bands; m++) {
766  for (n = 0; n < s->number_of_bands; n++) {
767  if (n < m) {
768  dnch->spread_function[j++] = pow(p2, m - n);
769  } else if (n > m) {
770  dnch->spread_function[j++] = pow(p1, n - m);
771  } else {
772  dnch->spread_function[j++] = 1.0;
773  }
774  }
775  }
776 
777  for (m = 0; m < s->number_of_bands; m++) {
778  dnch->band_excit[m] = 0.0;
779  prior_band_excit[m] = 0.0;
780  }
781 
782  for (m = 0; m <= s->fft_length2; m++)
783  dnch->band_excit[s->bin2band[m]] += 1.0;
784 
785  j = 0;
786  for (m = 0; m < s->number_of_bands; m++) {
787  for (n = 0; n < s->number_of_bands; n++)
788  prior_band_excit[m] += dnch->spread_function[j++] * dnch->band_excit[n];
789  }
790 
791  min = pow(0.1, 2.5);
792  max = pow(0.1, 1.0);
793  for (int i = 0; i < s->number_of_bands; i++) {
794  if (i < lrint(12.0 * sdiv)) {
795  dnch->band_excit[i] = pow(0.1, 1.45 + 0.1 * i / sdiv);
796  } else {
797  dnch->band_excit[i] = pow(0.1, 2.5 - 0.2 * (i / sdiv - 14.0));
798  }
799  dnch->band_excit[i] = av_clipd(dnch->band_excit[i], min, max);
800  }
801 
802  for (int i = 0; i <= s->fft_length2; i++)
803  prior[i] = RRATIO;
804  for (int i = 0; i < s->buffer_length; i++)
805  dnch->out_samples[i] = 0;
806 
807  j = 0;
808  for (int i = 0; i < s->number_of_bands; i++)
809  for (int k = 0; k < s->number_of_bands; k++)
810  dnch->spread_function[j++] *= dnch->band_excit[i] / prior_band_excit[i];
811  }
812 
813  j = 0;
814  sar = s->sample_advance / s->sample_rate;
815  for (int i = 0; i <= s->fft_length2; i++) {
816  if ((i == s->fft_length2) || (s->bin2band[i] > j)) {
817  double d6 = (i - 1) * s->sample_rate / s->fft_length;
818  double d7 = fmin(0.008 + 2.2 / d6, 0.03);
819  s->band_alpha[j] = exp(-sar / d7);
820  s->band_beta[j] = 1.0 - s->band_alpha[j];
821  j = s->bin2band[i];
822  }
823  }
824 
825  wscale = sqrt(16.0 / (9.0 * s->fft_length));
826  sum = 0.0;
827  for (int i = 0; i < s->window_length; i++) {
828  double d10 = sin(i * M_PI / s->window_length);
829  d10 *= wscale * d10;
830  s->window[i] = d10;
831  sum += d10 * d10;
832  }
833 
834  s->window_weight = 0.5 * sum;
835  s->floor = (1LL << 48) * exp(-23.025558369790467) * s->window_weight;
836  s->sample_floor = s->floor * exp(4.144600506562284);
837  s->auto_floor = s->floor * exp(6.907667510937141);
838 
839  set_parameters(s);
840 
841  s->noise_band_edge[0] = FFMIN(s->fft_length2, s->fft_length * get_band_edge(s, 0) / s->sample_rate);
842  i = 0;
843  for (int j = 1; j < 16; j++) {
844  s->noise_band_edge[j] = FFMIN(s->fft_length2, s->fft_length * get_band_edge(s, j) / s->sample_rate);
845  if (s->noise_band_edge[j] > lrint(1.1 * s->noise_band_edge[j - 1]))
846  i++;
847  s->noise_band_edge[16] = i;
848  }
849  s->noise_band_count = s->noise_band_edge[16];
850 
851  s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->fft_length);
852  if (!s->fifo)
853  return AVERROR(ENOMEM);
854 
855  return 0;
856 }
857 
858 static void preprocess(FFTComplex *in, int len)
859 {
860  double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
861  int n, i, k;
862 
863  d5 = 2.0 * M_PI / len;
864  d8 = sin(0.5 * d5);
865  d8 = -2.0 * d8 * d8;
866  d7 = sin(d5);
867  d9 = 1.0 + d8;
868  d6 = d7;
869  n = len / 2;
870 
871  for (i = 1; i < len / 4; i++) {
872  k = n - i;
873  d2 = 0.5 * (in[i].re + in[k].re);
874  d1 = 0.5 * (in[i].im - in[k].im);
875  d4 = 0.5 * (in[i].im + in[k].im);
876  d3 = 0.5 * (in[k].re - in[i].re);
877  in[i].re = d2 + d9 * d4 + d6 * d3;
878  in[i].im = d1 + d9 * d3 - d6 * d4;
879  in[k].re = d2 - d9 * d4 - d6 * d3;
880  in[k].im = -d1 + d9 * d3 - d6 * d4;
881  d10 = d9;
882  d9 += d9 * d8 - d6 * d7;
883  d6 += d6 * d8 + d10 * d7;
884  }
885 
886  d2 = in[0].re;
887  in[0].re = d2 + in[0].im;
888  in[0].im = d2 - in[0].im;
889 }
890 
891 static void postprocess(FFTComplex *in, int len)
892 {
893  double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
894  int n, i, k;
895 
896  d5 = 2.0 * M_PI / len;
897  d8 = sin(0.5 * d5);
898  d8 = -2.0 * d8 * d8;
899  d7 = sin(d5);
900  d9 = 1.0 + d8;
901  d6 = d7;
902  n = len / 2;
903  for (i = 1; i < len / 4; i++) {
904  k = n - i;
905  d2 = 0.5 * (in[i].re + in[k].re);
906  d1 = 0.5 * (in[i].im - in[k].im);
907  d4 = 0.5 * (in[i].re - in[k].re);
908  d3 = 0.5 * (in[i].im + in[k].im);
909  in[i].re = d2 - d9 * d3 - d6 * d4;
910  in[i].im = d1 + d9 * d4 - d6 * d3;
911  in[k].re = d2 + d9 * d3 + d6 * d4;
912  in[k].im = -d1 + d9 * d4 - d6 * d3;
913  d10 = d9;
914  d9 += d9 * d8 - d6 * d7;
915  d6 += d6 * d8 + d10 * d7;
916  }
917  d2 = in[0].re;
918  in[0].re = 0.5 * (d2 + in[0].im);
919  in[0].im = 0.5 * (d2 - in[0].im);
920 }
921 
923 {
924  for (int i = 0; i < 15; i++) {
925  dnch->noise_band_norm[i] = 0.0;
926  dnch->noise_band_avr[i] = 0.0;
927  dnch->noise_band_avi[i] = 0.0;
928  dnch->noise_band_var[i] = 0.0;
929  }
930 }
931 
933  DeNoiseChannel *dnch,
934  AVFrame *in, int ch)
935 {
936  float *src = (float *)in->extended_data[ch];
937  double mag2, var = 0.0, avr = 0.0, avi = 0.0;
938  int edge, j, k, n, edgemax;
939 
940  for (int i = 0; i < s->window_length; i++) {
941  dnch->fft_data[i].re = s->window[i] * src[i] * (1LL << 24);
942  dnch->fft_data[i].im = 0.0;
943  }
944 
945  for (int i = s->window_length; i < s->fft_length2; i++) {
946  dnch->fft_data[i].re = 0.0;
947  dnch->fft_data[i].im = 0.0;
948  }
949 
950  av_fft_permute(dnch->fft, dnch->fft_data);
951  av_fft_calc(dnch->fft, dnch->fft_data);
952 
953  preprocess(dnch->fft_data, s->fft_length);
954 
955  edge = s->noise_band_edge[0];
956  j = edge;
957  k = 0;
958  n = j;
959  edgemax = fmin(s->fft_length2, s->noise_band_edge[15]);
960  dnch->fft_data[s->fft_length2].re = dnch->fft_data[0].im;
961  dnch->fft_data[0].im = 0.0;
962  dnch->fft_data[s->fft_length2].im = 0.0;
963 
964  for (int i = j; i <= edgemax; i++) {
965  if ((i == j) && (i < edgemax)) {
966  if (j > edge) {
967  dnch->noise_band_norm[k - 1] += j - edge;
968  dnch->noise_band_avr[k - 1] += avr;
969  dnch->noise_band_avi[k - 1] += avi;
970  dnch->noise_band_var[k - 1] += var;
971  }
972  k++;
973  edge = j;
974  j = s->noise_band_edge[k];
975  if (k == 15) {
976  j++;
977  }
978  var = 0.0;
979  avr = 0.0;
980  avi = 0.0;
981  }
982  avr += dnch->fft_data[n].re;
983  avi += dnch->fft_data[n].im;
984  mag2 = dnch->fft_data[n].re * dnch->fft_data[n].re +
985  dnch->fft_data[n].im * dnch->fft_data[n].im;
986 
987  mag2 = fmax(mag2, s->sample_floor);
988 
989  dnch->noisy_data[i] = mag2;
990  var += mag2;
991  n++;
992  }
993 
994  dnch->noise_band_norm[k - 1] += j - edge;
995  dnch->noise_band_avr[k - 1] += avr;
996  dnch->noise_band_avi[k - 1] += avi;
997  dnch->noise_band_var[k - 1] += var;
998 }
999 
1001  DeNoiseChannel *dnch,
1002  double *sample_noise)
1003 {
1004  for (int i = 0; i < s->noise_band_count; i++) {
1005  dnch->noise_band_avr[i] /= dnch->noise_band_norm[i];
1006  dnch->noise_band_avi[i] /= dnch->noise_band_norm[i];
1007  dnch->noise_band_var[i] /= dnch->noise_band_norm[i];
1008  dnch->noise_band_var[i] -= dnch->noise_band_avr[i] * dnch->noise_band_avr[i] +
1009  dnch->noise_band_avi[i] * dnch->noise_band_avi[i];
1010  dnch->noise_band_auto_var[i] = dnch->noise_band_var[i];
1011  sample_noise[i] = (1.0 / C) * log(dnch->noise_band_var[i] / s->floor) - 100.0;
1012  }
1013  if (s->noise_band_count < 15) {
1014  for (int i = s->noise_band_count; i < 15; i++)
1015  sample_noise[i] = sample_noise[i - 1];
1016  }
1017 }
1018 
1020  DeNoiseChannel *dnch,
1021  double *sample_noise,
1022  int new_profile)
1023 {
1024  int new_band_noise[15];
1025  double temp[15];
1026  double sum = 0.0, d1;
1027  float new_noise_floor;
1028  int i, n;
1029 
1030  for (int m = 0; m < 15; m++)
1031  temp[m] = sample_noise[m];
1032 
1033  if (new_profile) {
1034  i = 0;
1035  for (int m = 0; m < 5; m++) {
1036  sum = 0.0;
1037  for (n = 0; n < 15; n++)
1038  sum += s->matrix_b[i++] * temp[n];
1039  s->vector_b[m] = sum;
1040  }
1041  solve(s->matrix_a, s->vector_b, 5);
1042  i = 0;
1043  for (int m = 0; m < 15; m++) {
1044  sum = 0.0;
1045  for (n = 0; n < 5; n++)
1046  sum += s->matrix_c[i++] * s->vector_b[n];
1047  temp[m] = sum;
1048  }
1049  }
1050 
1051  sum = 0.0;
1052  for (int m = 0; m < 15; m++)
1053  sum += temp[m];
1054 
1055  d1 = (int)(sum / 15.0 - 0.5);
1056  if (!new_profile)
1057  i = lrint(temp[7] - d1);
1058 
1059  for (d1 -= dnch->band_noise[7] - i; d1 > -20.0; d1 -= 1.0)
1060  ;
1061 
1062  for (int m = 0; m < 15; m++)
1063  temp[m] -= d1;
1064 
1065  new_noise_floor = d1 + 2.5;
1066 
1067  if (new_profile) {
1068  av_log(s, AV_LOG_INFO, "bn=");
1069  for (int m = 0; m < 15; m++) {
1070  new_band_noise[m] = lrint(temp[m]);
1071  new_band_noise[m] = av_clip(new_band_noise[m], -24, 24);
1072  av_log(s, AV_LOG_INFO, "%d ", new_band_noise[m]);
1073  }
1074  av_log(s, AV_LOG_INFO, "\n");
1075  memcpy(dnch->band_noise, new_band_noise, sizeof(new_band_noise));
1076  }
1077 
1078  if (s->track_noise)
1079  s->noise_floor = new_noise_floor;
1080 }
1081 
1082 typedef struct ThreadData {
1083  AVFrame *in;
1084 } ThreadData;
1085 
1086 static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1087 {
1088  AudioFFTDeNoiseContext *s = ctx->priv;
1089  ThreadData *td = arg;
1090  AVFrame *in = td->in;
1091  const int start = (in->channels * jobnr) / nb_jobs;
1092  const int end = (in->channels * (jobnr+1)) / nb_jobs;
1093 
1094  for (int ch = start; ch < end; ch++) {
1095  DeNoiseChannel *dnch = &s->dnch[ch];
1096  const float *src = (const float *)in->extended_data[ch];
1097  double *dst = dnch->out_samples;
1098 
1099  if (s->track_noise) {
1100  int i = s->block_count & 0x1FF;
1101 
1102  if (dnch->sfm_fail_flags[i])
1103  dnch->sfm_fail_total--;
1104  dnch->sfm_fail_flags[i] = 0;
1105  dnch->sfm_threshold *= 1.0 - dnch->sfm_alpha;
1106  dnch->sfm_threshold += dnch->sfm_alpha * (0.5 + (1.0 / 640) * dnch->sfm_fail_total);
1107  }
1108 
1109  for (int m = 0; m < s->window_length; m++) {
1110  dnch->fft_data[m].re = s->window[m] * src[m] * (1LL << 24);
1111  dnch->fft_data[m].im = 0;
1112  }
1113 
1114  for (int m = s->window_length; m < s->fft_length2; m++) {
1115  dnch->fft_data[m].re = 0;
1116  dnch->fft_data[m].im = 0;
1117  }
1118 
1119  av_fft_permute(dnch->fft, dnch->fft_data);
1120  av_fft_calc(dnch->fft, dnch->fft_data);
1121 
1122  preprocess(dnch->fft_data, s->fft_length);
1123  process_frame(s, dnch, dnch->fft_data,
1124  dnch->prior,
1125  dnch->prior_band_excit,
1126  s->track_noise);
1127  postprocess(dnch->fft_data, s->fft_length);
1128 
1129  av_fft_permute(dnch->ifft, dnch->fft_data);
1130  av_fft_calc(dnch->ifft, dnch->fft_data);
1131 
1132  for (int m = 0; m < s->window_length; m++)
1133  dst[m] += s->window[m] * dnch->fft_data[m].re / (1LL << 24);
1134  }
1135 
1136  return 0;
1137 }
1138 
1140  DeNoiseChannel *dnch,
1141  double *levels)
1142 {
1143  if (s->noise_band_count > 0) {
1144  for (int i = 0; i < s->noise_band_count; i++) {
1145  levels[i] = (1.0 / C) * log(dnch->noise_band_auto_var[i] / s->floor) - 100.0;
1146  }
1147  if (s->noise_band_count < 15) {
1148  for (int i = s->noise_band_count; i < 15; i++)
1149  levels[i] = levels[i - 1];
1150  }
1151  } else {
1152  for (int i = 0; i < 15; i++) {
1153  levels[i] = -100.0;
1154  }
1155  }
1156 }
1157 
1159 {
1160  AVFilterContext *ctx = inlink->dst;
1161  AVFilterLink *outlink = ctx->outputs[0];
1162  AudioFFTDeNoiseContext *s = ctx->priv;
1163  AVFrame *out = NULL, *in = NULL;
1164  ThreadData td;
1165  int ret = 0;
1166 
1167  in = ff_get_audio_buffer(outlink, s->window_length);
1168  if (!in)
1169  return AVERROR(ENOMEM);
1170 
1171  ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, s->window_length);
1172  if (ret < 0)
1173  goto end;
1174 
1175  if (s->track_noise) {
1176  for (int ch = 0; ch < inlink->channels; ch++) {
1177  DeNoiseChannel *dnch = &s->dnch[ch];
1178  double levels[15];
1179 
1180  get_auto_noise_levels(s, dnch, levels);
1181  set_noise_profile(s, dnch, levels, 0);
1182  }
1183 
1184  if (s->noise_floor != s->last_noise_floor)
1185  set_parameters(s);
1186  }
1187 
1188  if (s->sample_noise_start) {
1189  for (int ch = 0; ch < inlink->channels; ch++) {
1190  DeNoiseChannel *dnch = &s->dnch[ch];
1191 
1192  init_sample_noise(dnch);
1193  }
1194  s->sample_noise_start = 0;
1195  s->sample_noise = 1;
1196  }
1197 
1198  if (s->sample_noise) {
1199  for (int ch = 0; ch < inlink->channels; ch++) {
1200  DeNoiseChannel *dnch = &s->dnch[ch];
1201 
1202  sample_noise_block(s, dnch, in, ch);
1203  }
1204  }
1205 
1206  if (s->sample_noise_end) {
1207  for (int ch = 0; ch < inlink->channels; ch++) {
1208  DeNoiseChannel *dnch = &s->dnch[ch];
1209  double sample_noise[15];
1210 
1211  finish_sample_noise(s, dnch, sample_noise);
1212  set_noise_profile(s, dnch, sample_noise, 1);
1213  set_band_parameters(s, dnch);
1214  }
1215  s->sample_noise = 0;
1216  s->sample_noise_end = 0;
1217  }
1218 
1219  s->block_count++;
1220  td.in = in;
1221  ctx->internal->execute(ctx, filter_channel, &td, NULL,
1223 
1224  out = ff_get_audio_buffer(outlink, s->sample_advance);
1225  if (!out) {
1226  ret = AVERROR(ENOMEM);
1227  goto end;
1228  }
1229 
1230  for (int ch = 0; ch < inlink->channels; ch++) {
1231  DeNoiseChannel *dnch = &s->dnch[ch];
1232  double *src = dnch->out_samples;
1233  float *orig = (float *)in->extended_data[ch];
1234  float *dst = (float *)out->extended_data[ch];
1235 
1236  switch (s->output_mode) {
1237  case IN_MODE:
1238  for (int m = 0; m < s->sample_advance; m++)
1239  dst[m] = orig[m];
1240  break;
1241  case OUT_MODE:
1242  for (int m = 0; m < s->sample_advance; m++)
1243  dst[m] = src[m];
1244  break;
1245  case NOISE_MODE:
1246  for (int m = 0; m < s->sample_advance; m++)
1247  dst[m] = orig[m] - src[m];
1248  break;
1249  default:
1250  av_frame_free(&out);
1251  ret = AVERROR_BUG;
1252  goto end;
1253  }
1254  memmove(src, src + s->sample_advance, (s->window_length - s->sample_advance) * sizeof(*src));
1255  memset(src + (s->window_length - s->sample_advance), 0, s->sample_advance * sizeof(*src));
1256  }
1257 
1258  av_audio_fifo_drain(s->fifo, s->sample_advance);
1259 
1260  out->pts = s->pts;
1261  ret = ff_filter_frame(outlink, out);
1262  if (ret < 0)
1263  goto end;
1264  s->pts += av_rescale_q(s->sample_advance, (AVRational){1, outlink->sample_rate}, outlink->time_base);
1265 end:
1266  av_frame_free(&in);
1267 
1268  return ret;
1269 }
1270 
1272 {
1273  AVFilterLink *inlink = ctx->inputs[0];
1274  AVFilterLink *outlink = ctx->outputs[0];
1275  AudioFFTDeNoiseContext *s = ctx->priv;
1276  AVFrame *frame = NULL;
1277  int ret;
1278 
1280 
1282  if (ret < 0)
1283  return ret;
1284 
1285  if (ret > 0) {
1286  if (s->pts == AV_NOPTS_VALUE)
1287  s->pts = frame->pts;
1288 
1289  ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
1290  av_frame_free(&frame);
1291  if (ret < 0)
1292  return ret;
1293  }
1294 
1295  if (av_audio_fifo_size(s->fifo) >= s->window_length)
1296  return output_frame(inlink);
1297 
1298  FF_FILTER_FORWARD_STATUS(inlink, outlink);
1299  if (ff_outlink_frame_wanted(outlink) &&
1300  av_audio_fifo_size(s->fifo) < s->window_length) {
1302  return 0;
1303  }
1304 
1305  return FFERROR_NOT_READY;
1306 }
1307 
1309 {
1310  AudioFFTDeNoiseContext *s = ctx->priv;
1311 
1312  av_freep(&s->window);
1313  av_freep(&s->bin2band);
1314  av_freep(&s->band_alpha);
1315  av_freep(&s->band_beta);
1316 
1317  if (s->dnch) {
1318  for (int ch = 0; ch < s->channels; ch++) {
1319  DeNoiseChannel *dnch = &s->dnch[ch];
1320  av_freep(&dnch->amt);
1321  av_freep(&dnch->band_amt);
1322  av_freep(&dnch->band_excit);
1323  av_freep(&dnch->gain);
1324  av_freep(&dnch->prior);
1325  av_freep(&dnch->prior_band_excit);
1326  av_freep(&dnch->clean_data);
1327  av_freep(&dnch->noisy_data);
1328  av_freep(&dnch->out_samples);
1329  av_freep(&dnch->spread_function);
1330  av_freep(&dnch->abs_var);
1331  av_freep(&dnch->rel_var);
1332  av_freep(&dnch->min_abs_var);
1333  av_freep(&dnch->fft_data);
1334  av_fft_end(dnch->fft);
1335  dnch->fft = NULL;
1336  av_fft_end(dnch->ifft);
1337  dnch->ifft = NULL;
1338  }
1339  av_freep(&s->dnch);
1340  }
1341 
1342  av_audio_fifo_free(s->fifo);
1343 }
1344 
1346 {
1349  static const enum AVSampleFormat sample_fmts[] = {
1352  };
1353  int ret;
1354 
1356  if (!formats)
1357  return AVERROR(ENOMEM);
1359  if (ret < 0)
1360  return ret;
1361 
1363  if (!layouts)
1364  return AVERROR(ENOMEM);
1365 
1367  if (ret < 0)
1368  return ret;
1369 
1372 }
1373 
1374 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
1375  char *res, int res_len, int flags)
1376 {
1377  AudioFFTDeNoiseContext *s = ctx->priv;
1378  int need_reset = 0;
1379  int ret = 0;
1380 
1381  if (!strcmp(cmd, "sample_noise") ||
1382  !strcmp(cmd, "sn")) {
1383  if (!strcmp(args, "start")) {
1384  s->sample_noise_start = 1;
1385  s->sample_noise_end = 0;
1386  } else if (!strcmp(args, "end") ||
1387  !strcmp(args, "stop")) {
1388  s->sample_noise_start = 0;
1389  s->sample_noise_end = 1;
1390  }
1391  } else {
1392  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
1393  if (ret < 0)
1394  return ret;
1395  need_reset = 1;
1396  }
1397 
1398  if (need_reset)
1399  set_parameters(s);
1400 
1401  return 0;
1402 }
1403 
1404 static const AVFilterPad inputs[] = {
1405  {
1406  .name = "default",
1407  .type = AVMEDIA_TYPE_AUDIO,
1408  .config_props = config_input,
1409  },
1410  { NULL }
1411 };
1412 
1413 static const AVFilterPad outputs[] = {
1414  {
1415  .name = "default",
1416  .type = AVMEDIA_TYPE_AUDIO,
1417  },
1418  { NULL }
1419 };
1420 
1422  .name = "afftdn",
1423  .description = NULL_IF_CONFIG_SMALL("Denoise audio samples using FFT."),
1424  .query_formats = query_formats,
1425  .priv_size = sizeof(AudioFFTDeNoiseContext),
1426  .priv_class = &afftdn_class,
1427  .activate = activate,
1428  .uninit = uninit,
1429  .inputs = inputs,
1430  .outputs = outputs,
1434 };
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
formats
formats
Definition: signature.h:48
av_fft_end
av_cold void av_fft_end(FFTContext *s)
Definition: avfft.c:48
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:86
set_noise_profile
static void set_noise_profile(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *sample_noise, int new_profile)
Definition: af_afftdn.c:1019
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:86
td
#define td
Definition: regdef.h:70
NB_MODES
@ NB_MODES
Definition: af_afftdn.c:41
AudioFFTDeNoiseContext::floor
double floor
Definition: af_afftdn.c:129
av_clip
#define av_clip
Definition: common.h:122
DeNoiseChannel::clean_data
double * clean_data
Definition: af_afftdn.c:62
C
#define C
Definition: af_afftdn.c:33
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
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
DeNoiseChannel::noise_band_avr
double noise_band_avr[15]
Definition: af_afftdn.c:73
AudioFFTDeNoiseContext::vector_b
double vector_b[5]
Definition: af_afftdn.c:136
out
FILE * out
Definition: movenc.c:54
RRATIO
#define RRATIO
Definition: af_afftdn.c:35
calculate_sfm
static void calculate_sfm(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int start, int end)
Definition: af_afftdn.c:247
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
activate
static int activate(AVFilterContext *ctx)
Definition: af_afftdn.c:1271
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
inputs
static const AVFilterPad inputs[]
Definition: af_afftdn.c:1404
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
AudioFFTDeNoiseContext::band_centre
int band_centre[15]
Definition: af_afftdn.c:116
AudioFFTDeNoiseContext::window_weight
double window_weight
Definition: af_afftdn.c:128
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:142
solve
static void solve(double *matrix, double *vector, int size)
Definition: af_afftdn.c:198
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:436
AudioConvert::channels
int channels
Definition: audio_convert.c:54
OUT_MODE
@ OUT_MODE
Definition: af_afftdn.c:39
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
sample_noise_block
static void sample_noise_block(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, AVFrame *in, int ch)
Definition: af_afftdn.c:932
AVOption
AVOption.
Definition: opt.h:248
b
#define b
Definition: input.c:41
av_fft_permute
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
Definition: avfft.c:38
float.h
max
#define max(a, b)
Definition: cuda_runtime.h:33
AudioFFTDeNoiseContext::last_noise_balance
float last_noise_balance
Definition: af_afftdn.c:99
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
AudioFFTDeNoiseContext::matrix_b
double matrix_b[75]
Definition: af_afftdn.c:137
AudioFFTDeNoiseContext::fft_length2
int fft_length2
Definition: af_afftdn.c:110
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
DeNoiseChannel::band_amt
double * band_amt
Definition: af_afftdn.c:57
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::fifo
AVAudioFifo * fifo
Definition: af_afftdn.c:140
DeNoiseChannel::fft_data
FFTComplex * fft_data
Definition: af_afftdn.c:69
AudioFFTDeNoiseContext::block_count
int64_t block_count
Definition: af_afftdn.c:100
AudioFFTDeNoiseContext::max_var
double max_var
Definition: af_afftdn.c:126
NoiseType
NoiseType
Definition: af_afftdn.c:44
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
postprocess
static void postprocess(FFTComplex *in, int len)
Definition: af_afftdn.c:891
formats.h
AudioFFTDeNoiseContext::gain_scale
double gain_scale
Definition: af_afftdn.c:127
factor
static void factor(double *array, int size)
Definition: af_afftdn.c:184
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_afftdn.c:610
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1494
AudioFFTDeNoiseContext::residual_floor
float residual_floor
Definition: af_afftdn.c:91
AudioFFTDeNoiseContext::buffer_length
int buffer_length
Definition: af_afftdn.c:108
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
DeNoiseChannel::sfm_results
double sfm_results[3]
Definition: af_afftdn.c:79
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
freq2bark
static double freq2bark(double x)
Definition: af_afftdn.c:485
AudioFFTDeNoiseContext::matrix_a
double matrix_a[25]
Definition: af_afftdn.c:135
AudioFFTDeNoiseContext::sample_noise_end
int sample_noise_end
Definition: af_afftdn.c:106
AudioFFTDeNoiseContext::number_of_bands
int number_of_bands
Definition: af_afftdn.c:114
AudioFFTDeNoiseContext::pts
int64_t pts
Definition: af_afftdn.c:102
AudioFFTDeNoiseContext::window_length
int window_length
Definition: af_afftdn.c:112
AudioFFTDeNoiseContext::sample_noise_start
int sample_noise_start
Definition: af_afftdn.c:105
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
lrint
#define lrint
Definition: tablegen.h:53
AudioFFTDeNoiseContext::dnch
DeNoiseChannel * dnch
Definition: af_afftdn.c:123
DeNoiseChannel::spread_function
double * spread_function
Definition: af_afftdn.c:65
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
av_cold
#define av_cold
Definition: attributes.h:90
DeNoiseChannel::sfm_threshold
double sfm_threshold
Definition: af_afftdn.c:77
OFFSET
#define OFFSET(x)
Definition: af_afftdn.c:143
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1620
afftdn_options
static const AVOption afftdn_options[]
Definition: af_afftdn.c:147
s
#define s(width, name)
Definition: cbs_vp9.c:257
NB_NOISE
@ NB_NOISE
Definition: af_afftdn.c:49
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
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:186
AudioFFTDeNoiseContext
Definition: af_afftdn.c:84
DeNoiseChannel::gain
double * gain
Definition: af_afftdn.c:59
filters.h
DeNoiseChannel::rel_var
double * rel_var
Definition: af_afftdn.c:67
DeNoiseChannel
Definition: af_afftdn.c:52
DeNoiseChannel::min_abs_var
double * min_abs_var
Definition: af_afftdn.c:68
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
output_frame
static int output_frame(AVFilterLink *inlink)
Definition: af_afftdn.c:1158
AudioFFTDeNoiseContext::last_noise_reduction
float last_noise_reduction
Definition: af_afftdn.c:98
f
#define f(width, name)
Definition: cbs_vp9.c:255
E
#define E
Definition: avdct.c:32
get_band_edge
static int get_band_edge(AudioFFTDeNoiseContext *s, int band)
Definition: af_afftdn.c:500
arg
const char * arg
Definition: jacosubdec.c:66
if
if(ret)
Definition: filter_design.txt:179
preprocess
static void preprocess(FFTComplex *in, int len)
Definition: af_afftdn.c:858
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_afftdn.c:1345
AF
#define AF
Definition: af_afftdn.c:144
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:67
DeNoiseChannel::noise_band_auto_var
double noise_band_auto_var[15]
Definition: af_afftdn.c:54
NULL
#define NULL
Definition: coverity.c:32
DeNoiseChannel::sfm_fail_flags
int sfm_fail_flags[512]
Definition: af_afftdn.c:80
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_audio_fifo_alloc
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
get_auto_noise_levels
static void get_auto_noise_levels(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *levels)
Definition: af_afftdn.c:1139
av_clipf
#define av_clipf
Definition: common.h:170
filter_channel
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_afftdn.c:1086
src
#define src
Definition: vp8dsp.c:255
AudioFFTDeNoiseContext::noise_type
int noise_type
Definition: af_afftdn.c:89
DeNoiseChannel::sfm_alpha
double sfm_alpha
Definition: af_afftdn.c:78
avfft.h
set_band_parameters
static void set_band_parameters(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch)
Definition: af_afftdn.c:513
WHITE_NOISE
@ WHITE_NOISE
Definition: af_afftdn.c:45
exp
int8_t exp
Definition: eval.c:72
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
DeNoiseChannel::fft
FFTContext * fft
Definition: af_afftdn.c:70
init_sample_noise
static void init_sample_noise(DeNoiseChannel *dnch)
Definition: af_afftdn.c:922
NOISE_MODE
@ NOISE_MODE
Definition: af_afftdn.c:40
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
read_custom_noise
static void read_custom_noise(AudioFFTDeNoiseContext *s, int ch)
Definition: af_afftdn.c:549
AudioFFTDeNoiseContext::sample_advance
int sample_advance
Definition: af_afftdn.c:113
OutModes
OutModes
Definition: af_afftdn.c:37
AudioFFTDeNoiseContext::output_mode
int output_mode
Definition: af_afftdn.c:94
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:117
outputs
static const AVFilterPad outputs[]
Definition: af_afftdn.c:1413
av_clipd
#define av_clipd
Definition: common.h:173
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
fmaxf
float fmaxf(float, float)
fmin
double fmin(double, double)
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
size
int size
Definition: twinvq_data.h:10344
DeNoiseChannel::noise_band_norm
double noise_band_norm[15]
Definition: af_afftdn.c:72
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FFTComplex::im
FFTSample im
Definition: avfft.h:38
AudioFFTDeNoiseContext::track_noise
int track_noise
Definition: af_afftdn.c:92
AudioFFTDeNoiseContext::noise_reduction
float noise_reduction
Definition: af_afftdn.c:87
FFTComplex::re
FFTSample re
Definition: avfft.h:38
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:882
AudioFFTDeNoiseContext::window
double * window
Definition: af_afftdn.c:119
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
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:1000
VINYL_NOISE
@ VINYL_NOISE
Definition: af_afftdn.c:46
AudioFFTDeNoiseContext::band_alpha
double * band_alpha
Definition: af_afftdn.c:120
limit_gain
static double limit_gain(double a, double b)
Definition: af_afftdn.c:283
av_audio_fifo_size
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
AudioFFTDeNoiseContext::channels
int channels
Definition: af_afftdn.c:103
M_PI
#define M_PI
Definition: mathematics.h:52
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_afftdn.c:1308
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:126
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
get_band_noise
static int get_band_noise(AudioFFTDeNoiseContext *s, int band, double a, double b, double c)
Definition: af_afftdn.c:168
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
DeNoiseChannel::out_samples
double * out_samples
Definition: af_afftdn.c:64
FFTContext
Definition: fft.h:83
i
int i
Definition: input.c:407
DeNoiseChannel::prior_band_excit
double * prior_band_excit
Definition: af_afftdn.c:61
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
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:1374
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
SHELLAC_NOISE
@ SHELLAC_NOISE
Definition: af_afftdn.c:47
audio_fifo.h
len
int len
Definition: vorbis_enc_data.h:452
DeNoiseChannel::noise_band_var
double noise_band_var[15]
Definition: af_afftdn.c:75
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
DeNoiseChannel::noise_band_sample
double noise_band_sample[15]
Definition: af_afftdn.c:55
AudioFFTDeNoiseContext::track_residual
int track_residual
Definition: af_afftdn.c:93
AudioFFTDeNoiseContext::noise_floor
float noise_floor
Definition: af_afftdn.c:88
AVFilter
Filter definition.
Definition: avfilter.h:145
ff_af_afftdn
AVFilter ff_af_afftdn
Definition: af_afftdn.c:1421
AudioFFTDeNoiseContext::auto_floor
double auto_floor
Definition: af_afftdn.c:131
DeNoiseChannel::amt
double * amt
Definition: af_afftdn.c:56
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106
ret
ret
Definition: filter_design.txt:187
RATIO
#define RATIO
Definition: af_afftdn.c:34
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AudioFFTDeNoiseContext::bin2band
int * bin2band
Definition: af_afftdn.c:118
CUSTOM_NOISE
@ CUSTOM_NOISE
Definition: af_afftdn.c:48
IN_MODE
@ IN_MODE
Definition: af_afftdn.c:38
DeNoiseChannel::sfm_fail_total
int sfm_fail_total
Definition: af_afftdn.c:81
DeNoiseChannel::noisy_data
double * noisy_data
Definition: af_afftdn.c:63
fmax
double fmax(double, double)
AudioFFTDeNoiseContext::noise_band_count
int noise_band_count
Definition: af_afftdn.c:134
av_fft_init
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
Definition: avfft.c:28
AudioFFTDeNoiseContext::last_noise_floor
float last_noise_floor
Definition: af_afftdn.c:97
DeNoiseChannel::ifft
FFTContext * ifft
Definition: af_afftdn.c:70
AFR
#define AFR
Definition: af_afftdn.c:145
AudioFFTDeNoiseContext::band_noise_str
char * band_noise_str
Definition: af_afftdn.c:90
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:421
DeNoiseChannel::band_noise
int band_noise[15]
Definition: af_afftdn.c:53
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AudioFFTDeNoiseContext::matrix_c
double matrix_c[75]
Definition: af_afftdn.c:138
process_frame
static void process_frame(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, FFTComplex *fft_data, double *prior, double *prior_band_excit, int track_noise)
Definition: af_afftdn.c:292
DeNoiseChannel::band_excit
double * band_excit
Definition: af_afftdn.c:58
temp
else temp
Definition: vf_mcdeint.c:259
set_parameters
static void set_parameters(AudioFFTDeNoiseContext *s)
Definition: af_afftdn.c:581
AudioFFTDeNoiseContext::band_beta
double * band_beta
Definition: af_afftdn.c:121
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
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:253
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(afftdn)
audio.h
M_LN10
#define M_LN10
Definition: mathematics.h:43
ThreadData::in
AVFrame * in
Definition: af_adenorm.c:223
AudioFFTDeNoiseContext::max_gain
double max_gain
Definition: af_afftdn.c:125
DeNoiseChannel::prior
double * prior
Definition: af_afftdn.c:60
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AudioFFTDeNoiseContext::bin_count
int bin_count
Definition: af_afftdn.c:111
AudioFFTDeNoiseContext::sample_noise
int sample_noise
Definition: af_afftdn.c:104
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
AudioFFTDeNoiseContext::fft_length
int fft_length
Definition: af_afftdn.c:109
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AudioFFTDeNoiseContext::sample_floor
double sample_floor
Definition: af_afftdn.c:130
AudioFFTDeNoiseContext::last_residual_floor
float last_residual_floor
Definition: af_afftdn.c:96
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:575
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
DeNoiseChannel::abs_var
double * abs_var
Definition: af_afftdn.c:66
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
AudioFFTDeNoiseContext::noise_band_edge
int noise_band_edge[17]
Definition: af_afftdn.c:133
get_band_centre
static int get_band_centre(AudioFFTDeNoiseContext *s, int band)
Definition: af_afftdn.c:492
DeNoiseChannel::noise_band_avi
double noise_band_avi[15]
Definition: af_afftdn.c:74
AudioFFTDeNoiseContext::sample_rate
float sample_rate
Definition: af_afftdn.c:107
int
int
Definition: ffmpeg_filter.c:170
av_audio_fifo_peek
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
av_fft_calc
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
Definition: avfft.c:43
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
FFTComplex
Definition: avfft.h:37
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:568
process_get_band_noise
static int process_get_band_noise(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int band)
Definition: af_afftdn.c:217
min
float min
Definition: vorbis_enc_data.h:456