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