FFmpeg
af_firequalizer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Muhammad Faiz <mfcc64@gmail.com>
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 
22 #include "libavutil/opt.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/avassert.h"
25 #include "libavcodec/avfft.h"
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "audio.h"
29 
30 #define RDFT_BITS_MIN 4
31 #define RDFT_BITS_MAX 16
32 
33 enum WindowFunc {
45 };
46 
47 enum Scale {
53 };
54 
55 #define NB_GAIN_ENTRY_MAX 4096
56 typedef struct GainEntry {
57  double freq;
58  double gain;
59 } GainEntry;
60 
61 typedef struct OverlapIndex {
62  int buf_idx;
64 } OverlapIndex;
65 
66 typedef struct FIREqualizerContext {
67  const AVClass *class;
68 
77  int rdft_len;
79 
80  float *analysis_buf;
81  float *dump_buf;
83  float *kernel_buf;
84  float *cepstrum_buf;
85  float *conv_buf;
87  int fir_len;
89  int64_t next_pts;
91  int remaining;
92 
93  char *gain_cmd;
95  const char *gain;
96  const char *gain_entry;
97  double delay;
98  double accuracy;
99  int wfunc;
100  int fixed;
101  int multi;
103  int scale;
104  char *dumpfile;
106  int fft2;
108 
113 
114 #define OFFSET(x) offsetof(FIREqualizerContext, x)
115 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
116 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
117 
118 static const AVOption firequalizer_options[] = {
119  { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, TFLAGS },
120  { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, TFLAGS },
121  { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS },
122  { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS },
123  { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, 0, NB_WFUNC-1, FLAGS, "wfunc" },
124  { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, "wfunc" },
125  { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, "wfunc" },
126  { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, "wfunc" },
127  { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, "wfunc" },
128  { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, "wfunc" },
129  { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, "wfunc" },
130  { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, "wfunc" },
131  { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, "wfunc" },
132  { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, "wfunc" },
133  { "tukey", "tukey window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_TUKEY }, 0, 0, FLAGS, "wfunc" },
134  { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
135  { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
136  { "zero_phase", "set zero phase mode", OFFSET(zero_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
137  { "scale", "set gain scale", OFFSET(scale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" },
138  { "linlin", "linear-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLIN }, 0, 0, FLAGS, "scale" },
139  { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, "scale" },
140  { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, "scale" },
141  { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, "scale" },
142  { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
143  { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" },
144  { "fft2", "set 2-channels fft", OFFSET(fft2), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
145  { "min_phase", "set minimum phase mode", OFFSET(min_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
146  { NULL }
147 };
148 
149 AVFILTER_DEFINE_CLASS(firequalizer);
150 
152 {
153  av_rdft_end(s->analysis_rdft);
154  av_rdft_end(s->analysis_irdft);
155  av_rdft_end(s->rdft);
156  av_rdft_end(s->irdft);
157  av_fft_end(s->fft_ctx);
158  av_rdft_end(s->cepstrum_rdft);
159  av_rdft_end(s->cepstrum_irdft);
160  s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL;
161  s->fft_ctx = NULL;
162  s->cepstrum_rdft = NULL;
163  s->cepstrum_irdft = NULL;
164 
165  av_freep(&s->analysis_buf);
166  av_freep(&s->dump_buf);
167  av_freep(&s->kernel_tmp_buf);
168  av_freep(&s->kernel_buf);
169  av_freep(&s->cepstrum_buf);
170  av_freep(&s->conv_buf);
171  av_freep(&s->conv_idx);
172 }
173 
175 {
176  FIREqualizerContext *s = ctx->priv;
177 
178  common_uninit(s);
179  av_freep(&s->gain_cmd);
180  av_freep(&s->gain_entry_cmd);
181 }
182 
184 {
187  static const enum AVSampleFormat sample_fmts[] = {
190  };
191  int ret;
192 
194  if (!layouts)
195  return AVERROR(ENOMEM);
197  if (ret < 0)
198  return ret;
199 
201  if (!formats)
202  return AVERROR(ENOMEM);
204  if (ret < 0)
205  return ret;
206 
208  if (!formats)
209  return AVERROR(ENOMEM);
211 }
212 
213 static void fast_convolute(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, float *av_restrict conv_buf,
214  OverlapIndex *av_restrict idx, float *av_restrict data, int nsamples)
215 {
216  if (nsamples <= s->nsamples_max) {
217  float *buf = conv_buf + idx->buf_idx * s->rdft_len;
218  float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
219  int center = s->fir_len/2;
220  int k;
221 
222  memset(buf, 0, center * sizeof(*data));
223  memcpy(buf + center, data, nsamples * sizeof(*data));
224  memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*data));
225  av_rdft_calc(s->rdft, buf);
226 
227  buf[0] *= kernel_buf[0];
228  buf[1] *= kernel_buf[s->rdft_len/2];
229  for (k = 1; k < s->rdft_len/2; k++) {
230  buf[2*k] *= kernel_buf[k];
231  buf[2*k+1] *= kernel_buf[k];
232  }
233 
234  av_rdft_calc(s->irdft, buf);
235  for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
236  buf[k] += obuf[k];
237  memcpy(data, buf, nsamples * sizeof(*data));
238  idx->buf_idx = !idx->buf_idx;
239  idx->overlap_idx = nsamples;
240  } else {
241  while (nsamples > s->nsamples_max * 2) {
242  fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max);
243  data += s->nsamples_max;
244  nsamples -= s->nsamples_max;
245  }
246  fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2);
247  fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2);
248  }
249 }
250 
251 static void fast_convolute_nonlinear(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf,
252  float *av_restrict conv_buf, OverlapIndex *av_restrict idx,
253  float *av_restrict data, int nsamples)
254 {
255  if (nsamples <= s->nsamples_max) {
256  float *buf = conv_buf + idx->buf_idx * s->rdft_len;
257  float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
258  int k;
259 
260  memcpy(buf, data, nsamples * sizeof(*data));
261  memset(buf + nsamples, 0, (s->rdft_len - nsamples) * sizeof(*data));
262  av_rdft_calc(s->rdft, buf);
263 
264  buf[0] *= kernel_buf[0];
265  buf[1] *= kernel_buf[1];
266  for (k = 2; k < s->rdft_len; k += 2) {
267  float re, im;
268  re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];
269  im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];
270  buf[k] = re;
271  buf[k+1] = im;
272  }
273 
274  av_rdft_calc(s->irdft, buf);
275  for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
276  buf[k] += obuf[k];
277  memcpy(data, buf, nsamples * sizeof(*data));
278  idx->buf_idx = !idx->buf_idx;
279  idx->overlap_idx = nsamples;
280  } else {
281  while (nsamples > s->nsamples_max * 2) {
282  fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, s->nsamples_max);
283  data += s->nsamples_max;
284  nsamples -= s->nsamples_max;
285  }
286  fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data, nsamples/2);
287  fast_convolute_nonlinear(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2);
288  }
289 }
290 
291 static void fast_convolute2(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, FFTComplex *av_restrict conv_buf,
292  OverlapIndex *av_restrict idx, float *av_restrict data0, float *av_restrict data1, int nsamples)
293 {
294  if (nsamples <= s->nsamples_max) {
295  FFTComplex *buf = conv_buf + idx->buf_idx * s->rdft_len;
296  FFTComplex *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
297  int center = s->fir_len/2;
298  int k;
299  float tmp;
300 
301  memset(buf, 0, center * sizeof(*buf));
302  for (k = 0; k < nsamples; k++) {
303  buf[center+k].re = data0[k];
304  buf[center+k].im = data1[k];
305  }
306  memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*buf));
307  av_fft_permute(s->fft_ctx, buf);
308  av_fft_calc(s->fft_ctx, buf);
309 
310  /* swap re <-> im, do backward fft using forward fft_ctx */
311  /* normalize with 0.5f */
312  tmp = buf[0].re;
313  buf[0].re = 0.5f * kernel_buf[0] * buf[0].im;
314  buf[0].im = 0.5f * kernel_buf[0] * tmp;
315  for (k = 1; k < s->rdft_len/2; k++) {
316  int m = s->rdft_len - k;
317  tmp = buf[k].re;
318  buf[k].re = 0.5f * kernel_buf[k] * buf[k].im;
319  buf[k].im = 0.5f * kernel_buf[k] * tmp;
320  tmp = buf[m].re;
321  buf[m].re = 0.5f * kernel_buf[k] * buf[m].im;
322  buf[m].im = 0.5f * kernel_buf[k] * tmp;
323  }
324  tmp = buf[k].re;
325  buf[k].re = 0.5f * kernel_buf[k] * buf[k].im;
326  buf[k].im = 0.5f * kernel_buf[k] * tmp;
327 
328  av_fft_permute(s->fft_ctx, buf);
329  av_fft_calc(s->fft_ctx, buf);
330 
331  for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) {
332  buf[k].re += obuf[k].re;
333  buf[k].im += obuf[k].im;
334  }
335 
336  /* swapped re <-> im */
337  for (k = 0; k < nsamples; k++) {
338  data0[k] = buf[k].im;
339  data1[k] = buf[k].re;
340  }
341  idx->buf_idx = !idx->buf_idx;
342  idx->overlap_idx = nsamples;
343  } else {
344  while (nsamples > s->nsamples_max * 2) {
345  fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, s->nsamples_max);
346  data0 += s->nsamples_max;
347  data1 += s->nsamples_max;
348  nsamples -= s->nsamples_max;
349  }
350  fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, nsamples/2);
351  fast_convolute2(s, kernel_buf, conv_buf, idx, data0 + nsamples/2, data1 + nsamples/2, nsamples - nsamples/2);
352  }
353 }
354 
355 static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch)
356 {
357  FIREqualizerContext *s = ctx->priv;
358  int rate = ctx->inputs[0]->sample_rate;
359  int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG;
360  int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG;
361  int x;
362  int center = s->fir_len / 2;
363  double delay = s->zero_phase ? 0.0 : (double) center / rate;
364  double vx, ya, yb;
365 
366  if (!s->min_phase) {
367  s->analysis_buf[0] *= s->rdft_len/2;
368  for (x = 1; x <= center; x++) {
369  s->analysis_buf[x] *= s->rdft_len/2;
370  s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2;
371  }
372  } else {
373  for (x = 0; x < s->fir_len; x++)
374  s->analysis_buf[x] *= s->rdft_len/2;
375  }
376 
377  if (ch)
378  fprintf(fp, "\n\n");
379 
380  fprintf(fp, "# time[%d] (time amplitude)\n", ch);
381 
382  if (!s->min_phase) {
383  for (x = center; x > 0; x--)
384  fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]);
385 
386  for (x = 0; x <= center; x++)
387  fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]);
388  } else {
389  for (x = 0; x < s->fir_len; x++)
390  fprintf(fp, "%15.10f %15.10f\n", (double)x / rate, (double) s->analysis_buf[x]);
391  }
392 
393  av_rdft_calc(s->analysis_rdft, s->analysis_buf);
394 
395  fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch);
396 
397  for (x = 0; x <= s->analysis_rdft_len/2; x++) {
398  int i = (x == s->analysis_rdft_len/2) ? 1 : 2 * x;
399  vx = (double)x * rate / s->analysis_rdft_len;
400  if (xlog)
401  vx = log2(0.05*vx);
402  ya = s->dump_buf[i];
403  yb = s->min_phase && (i > 1) ? hypotf(s->analysis_buf[i], s->analysis_buf[i+1]) : s->analysis_buf[i];
404  if (s->min_phase)
405  yb = fabs(yb);
406  if (ylog) {
407  ya = 20.0 * log10(fabs(ya));
408  yb = 20.0 * log10(fabs(yb));
409  }
410  fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb);
411  }
412 }
413 
414 static double entry_func(void *p, double freq, double gain)
415 {
416  AVFilterContext *ctx = p;
417  FIREqualizerContext *s = ctx->priv;
418 
419  if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) {
420  av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n");
421  s->gain_entry_err = AVERROR(EINVAL);
422  return 0;
423  }
424 
425  if (isnan(freq)) {
426  av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain);
427  s->gain_entry_err = AVERROR(EINVAL);
428  return 0;
429  }
430 
431  if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) {
432  av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain);
433  s->gain_entry_err = AVERROR(EINVAL);
434  return 0;
435  }
436 
437  s->gain_entry_tbl[s->nb_gain_entry].freq = freq;
438  s->gain_entry_tbl[s->nb_gain_entry].gain = gain;
439  s->nb_gain_entry++;
440  return 0;
441 }
442 
443 static int gain_entry_compare(const void *key, const void *memb)
444 {
445  const double *freq = key;
446  const GainEntry *entry = memb;
447 
448  if (*freq < entry[0].freq)
449  return -1;
450  if (*freq > entry[1].freq)
451  return 1;
452  return 0;
453 }
454 
455 static double gain_interpolate_func(void *p, double freq)
456 {
457  AVFilterContext *ctx = p;
458  FIREqualizerContext *s = ctx->priv;
459  GainEntry *res;
460  double d0, d1, d;
461 
462  if (isnan(freq))
463  return freq;
464 
465  if (!s->nb_gain_entry)
466  return 0;
467 
468  if (freq <= s->gain_entry_tbl[0].freq)
469  return s->gain_entry_tbl[0].gain;
470 
471  if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq)
472  return s->gain_entry_tbl[s->nb_gain_entry-1].gain;
473 
474  res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare);
475  av_assert0(res);
476 
477  d = res[1].freq - res[0].freq;
478  d0 = freq - res[0].freq;
479  d1 = res[1].freq - freq;
480 
481  if (d0 && d1)
482  return (d0 * res[1].gain + d1 * res[0].gain) / d;
483 
484  if (d0)
485  return res[1].gain;
486 
487  return res[0].gain;
488 }
489 
490 static double cubic_interpolate_func(void *p, double freq)
491 {
492  AVFilterContext *ctx = p;
493  FIREqualizerContext *s = ctx->priv;
494  GainEntry *res;
495  double x, x2, x3;
496  double a, b, c, d;
497  double m0, m1, m2, msum, unit;
498 
499  if (!s->nb_gain_entry)
500  return 0;
501 
502  if (freq <= s->gain_entry_tbl[0].freq)
503  return s->gain_entry_tbl[0].gain;
504 
505  if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq)
506  return s->gain_entry_tbl[s->nb_gain_entry-1].gain;
507 
508  res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare);
509  av_assert0(res);
510 
511  unit = res[1].freq - res[0].freq;
512  m0 = res != s->gain_entry_tbl ?
513  unit * (res[0].gain - res[-1].gain) / (res[0].freq - res[-1].freq) : 0;
514  m1 = res[1].gain - res[0].gain;
515  m2 = res != s->gain_entry_tbl + s->nb_gain_entry - 2 ?
516  unit * (res[2].gain - res[1].gain) / (res[2].freq - res[1].freq) : 0;
517 
518  msum = fabs(m0) + fabs(m1);
519  m0 = msum > 0 ? (fabs(m0) * m1 + fabs(m1) * m0) / msum : 0;
520  msum = fabs(m1) + fabs(m2);
521  m1 = msum > 0 ? (fabs(m1) * m2 + fabs(m2) * m1) / msum : 0;
522 
523  d = res[0].gain;
524  c = m0;
525  b = 3 * res[1].gain - m1 - 2 * c - 3 * d;
526  a = res[1].gain - b - c - d;
527 
528  x = (freq - res[0].freq) / unit;
529  x2 = x * x;
530  x3 = x2 * x;
531 
532  return a * x3 + b * x2 + c * x + d;
533 }
534 
535 static const char *const var_names[] = {
536  "f",
537  "sr",
538  "ch",
539  "chid",
540  "chs",
541  "chlayout",
542  NULL
543 };
544 
545 enum VarOffset {
553 };
554 
555 static void generate_min_phase_kernel(FIREqualizerContext *s, float *rdft_buf)
556 {
557  int k, cepstrum_len = s->cepstrum_len, rdft_len = s->rdft_len;
558  double norm = 2.0 / cepstrum_len;
559  double minval = 1e-7 / rdft_len;
560 
561  memset(s->cepstrum_buf, 0, cepstrum_len * sizeof(*s->cepstrum_buf));
562  memcpy(s->cepstrum_buf, rdft_buf, rdft_len/2 * sizeof(*rdft_buf));
563  memcpy(s->cepstrum_buf + cepstrum_len - rdft_len/2, rdft_buf + rdft_len/2, rdft_len/2 * sizeof(*rdft_buf));
564 
565  av_rdft_calc(s->cepstrum_rdft, s->cepstrum_buf);
566 
567  s->cepstrum_buf[0] = log(FFMAX(s->cepstrum_buf[0], minval));
568  s->cepstrum_buf[1] = log(FFMAX(s->cepstrum_buf[1], minval));
569 
570  for (k = 2; k < cepstrum_len; k += 2) {
571  s->cepstrum_buf[k] = log(FFMAX(s->cepstrum_buf[k], minval));
572  s->cepstrum_buf[k+1] = 0;
573  }
574 
575  av_rdft_calc(s->cepstrum_irdft, s->cepstrum_buf);
576 
577  memset(s->cepstrum_buf + cepstrum_len/2 + 1, 0, (cepstrum_len/2 - 1) * sizeof(*s->cepstrum_buf));
578  for (k = 1; k < cepstrum_len/2; k++)
579  s->cepstrum_buf[k] *= 2;
580 
581  av_rdft_calc(s->cepstrum_rdft, s->cepstrum_buf);
582 
583  s->cepstrum_buf[0] = exp(s->cepstrum_buf[0] * norm) * norm;
584  s->cepstrum_buf[1] = exp(s->cepstrum_buf[1] * norm) * norm;
585  for (k = 2; k < cepstrum_len; k += 2) {
586  double mag = exp(s->cepstrum_buf[k] * norm) * norm;
587  double ph = s->cepstrum_buf[k+1] * norm;
588  s->cepstrum_buf[k] = mag * cos(ph);
589  s->cepstrum_buf[k+1] = mag * sin(ph);
590  }
591 
592  av_rdft_calc(s->cepstrum_irdft, s->cepstrum_buf);
593  memset(rdft_buf, 0, s->rdft_len * sizeof(*rdft_buf));
594  memcpy(rdft_buf, s->cepstrum_buf, s->fir_len * sizeof(*rdft_buf));
595 
596  if (s->dumpfile) {
597  memset(s->analysis_buf, 0, s->analysis_rdft_len * sizeof(*s->analysis_buf));
598  memcpy(s->analysis_buf, s->cepstrum_buf, s->fir_len * sizeof(*s->analysis_buf));
599  }
600 
601 }
602 
603 static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
604 {
605  FIREqualizerContext *s = ctx->priv;
606  AVFilterLink *inlink = ctx->inputs[0];
607  const char *gain_entry_func_names[] = { "entry", NULL };
608  const char *gain_func_names[] = { "gain_interpolate", "cubic_interpolate", NULL };
609  double (*gain_entry_funcs[])(void *, double, double) = { entry_func, NULL };
610  double (*gain_funcs[])(void *, double) = { gain_interpolate_func, cubic_interpolate_func, NULL };
611  double vars[VAR_NB];
612  AVExpr *gain_expr;
613  int ret, k, center, ch;
614  int xlog = s->scale == SCALE_LOGLIN || s->scale == SCALE_LOGLOG;
615  int ylog = s->scale == SCALE_LINLOG || s->scale == SCALE_LOGLOG;
616  FILE *dump_fp = NULL;
617 
618  s->nb_gain_entry = 0;
619  s->gain_entry_err = 0;
620  if (gain_entry) {
621  double result = 0.0;
622  ret = av_expr_parse_and_eval(&result, gain_entry, NULL, NULL, NULL, NULL,
623  gain_entry_func_names, gain_entry_funcs, ctx, 0, ctx);
624  if (ret < 0)
625  return ret;
626  if (s->gain_entry_err < 0)
627  return s->gain_entry_err;
628  }
629 
630  av_log(ctx, AV_LOG_DEBUG, "nb_gain_entry = %d.\n", s->nb_gain_entry);
631 
632  ret = av_expr_parse(&gain_expr, gain, var_names,
633  gain_func_names, gain_funcs, NULL, NULL, 0, ctx);
634  if (ret < 0)
635  return ret;
636 
637  if (s->dumpfile && (!s->dump_buf || !s->analysis_rdft || !(dump_fp = fopen(s->dumpfile, "w"))))
638  av_log(ctx, AV_LOG_WARNING, "dumping failed.\n");
639 
640  vars[VAR_CHS] = inlink->channels;
641  vars[VAR_CHLAYOUT] = inlink->channel_layout;
642  vars[VAR_SR] = inlink->sample_rate;
643  for (ch = 0; ch < inlink->channels; ch++) {
644  float *rdft_buf = s->kernel_tmp_buf + ch * s->rdft_len;
645  double result;
646  vars[VAR_CH] = ch;
647  vars[VAR_CHID] = av_channel_layout_extract_channel(inlink->channel_layout, ch);
648  vars[VAR_F] = 0.0;
649  if (xlog)
650  vars[VAR_F] = log2(0.05 * vars[VAR_F]);
651  result = av_expr_eval(gain_expr, vars, ctx);
652  s->analysis_buf[0] = ylog ? pow(10.0, 0.05 * result) : result;
653 
654  vars[VAR_F] = 0.5 * inlink->sample_rate;
655  if (xlog)
656  vars[VAR_F] = log2(0.05 * vars[VAR_F]);
657  result = av_expr_eval(gain_expr, vars, ctx);
658  s->analysis_buf[1] = ylog ? pow(10.0, 0.05 * result) : result;
659 
660  for (k = 1; k < s->analysis_rdft_len/2; k++) {
661  vars[VAR_F] = k * ((double)inlink->sample_rate /(double)s->analysis_rdft_len);
662  if (xlog)
663  vars[VAR_F] = log2(0.05 * vars[VAR_F]);
664  result = av_expr_eval(gain_expr, vars, ctx);
665  s->analysis_buf[2*k] = ylog ? pow(10.0, 0.05 * result) : s->min_phase ? fabs(result) : result;
666  s->analysis_buf[2*k+1] = 0.0;
667  }
668 
669  if (s->dump_buf)
670  memcpy(s->dump_buf, s->analysis_buf, s->analysis_rdft_len * sizeof(*s->analysis_buf));
671 
672  av_rdft_calc(s->analysis_irdft, s->analysis_buf);
673  center = s->fir_len / 2;
674 
675  for (k = 0; k <= center; k++) {
676  double u = k * (M_PI/center);
677  double win;
678  switch (s->wfunc) {
679  case WFUNC_RECTANGULAR:
680  win = 1.0;
681  break;
682  case WFUNC_HANN:
683  win = 0.5 + 0.5 * cos(u);
684  break;
685  case WFUNC_HAMMING:
686  win = 0.53836 + 0.46164 * cos(u);
687  break;
688  case WFUNC_BLACKMAN:
689  win = 0.42 + 0.5 * cos(u) + 0.08 * cos(2*u);
690  break;
691  case WFUNC_NUTTALL3:
692  win = 0.40897 + 0.5 * cos(u) + 0.09103 * cos(2*u);
693  break;
694  case WFUNC_MNUTTALL3:
695  win = 0.4243801 + 0.4973406 * cos(u) + 0.0782793 * cos(2*u);
696  break;
697  case WFUNC_NUTTALL:
698  win = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u);
699  break;
700  case WFUNC_BNUTTALL:
701  win = 0.3635819 + 0.4891775 * cos(u) + 0.1365995 * cos(2*u) + 0.0106411 * cos(3*u);
702  break;
703  case WFUNC_BHARRIS:
704  win = 0.35875 + 0.48829 * cos(u) + 0.14128 * cos(2*u) + 0.01168 * cos(3*u);
705  break;
706  case WFUNC_TUKEY:
707  win = (u <= 0.5 * M_PI) ? 1.0 : (0.5 + 0.5 * cos(2*u - M_PI));
708  break;
709  default:
710  av_assert0(0);
711  }
712  s->analysis_buf[k] *= (2.0/s->analysis_rdft_len) * (2.0/s->rdft_len) * win;
713  if (k)
714  s->analysis_buf[s->analysis_rdft_len - k] = s->analysis_buf[k];
715  }
716 
717  memset(s->analysis_buf + center + 1, 0, (s->analysis_rdft_len - s->fir_len) * sizeof(*s->analysis_buf));
718  memcpy(rdft_buf, s->analysis_buf, s->rdft_len/2 * sizeof(*s->analysis_buf));
719  memcpy(rdft_buf + s->rdft_len/2, s->analysis_buf + s->analysis_rdft_len - s->rdft_len/2, s->rdft_len/2 * sizeof(*s->analysis_buf));
720  if (s->min_phase)
721  generate_min_phase_kernel(s, rdft_buf);
722  av_rdft_calc(s->rdft, rdft_buf);
723 
724  for (k = 0; k < s->rdft_len; k++) {
725  if (isnan(rdft_buf[k]) || isinf(rdft_buf[k])) {
726  av_log(ctx, AV_LOG_ERROR, "filter kernel contains nan or infinity.\n");
727  av_expr_free(gain_expr);
728  if (dump_fp)
729  fclose(dump_fp);
730  return AVERROR(EINVAL);
731  }
732  }
733 
734  if (!s->min_phase) {
735  rdft_buf[s->rdft_len-1] = rdft_buf[1];
736  for (k = 0; k < s->rdft_len/2; k++)
737  rdft_buf[k] = rdft_buf[2*k];
738  rdft_buf[s->rdft_len/2] = rdft_buf[s->rdft_len-1];
739  }
740 
741  if (dump_fp)
742  dump_fir(ctx, dump_fp, ch);
743 
744  if (!s->multi)
745  break;
746  }
747 
748  memcpy(s->kernel_buf, s->kernel_tmp_buf, (s->multi ? inlink->channels : 1) * s->rdft_len * sizeof(*s->kernel_buf));
749  av_expr_free(gain_expr);
750  if (dump_fp)
751  fclose(dump_fp);
752  return 0;
753 }
754 
755 #define SELECT_GAIN(s) (s->gain_cmd ? s->gain_cmd : s->gain)
756 #define SELECT_GAIN_ENTRY(s) (s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry)
757 
759 {
760  AVFilterContext *ctx = inlink->dst;
761  FIREqualizerContext *s = ctx->priv;
762  int rdft_bits;
763 
764  common_uninit(s);
765 
766  s->next_pts = 0;
767  s->frame_nsamples_max = 0;
768 
769  s->fir_len = FFMAX(2 * (int)(inlink->sample_rate * s->delay) + 1, 3);
770  s->remaining = s->fir_len - 1;
771 
772  for (rdft_bits = RDFT_BITS_MIN; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
773  s->rdft_len = 1 << rdft_bits;
774  s->nsamples_max = s->rdft_len - s->fir_len + 1;
775  if (s->nsamples_max * 2 >= s->fir_len)
776  break;
777  }
778 
779  if (rdft_bits > RDFT_BITS_MAX) {
780  av_log(ctx, AV_LOG_ERROR, "too large delay, please decrease it.\n");
781  return AVERROR(EINVAL);
782  }
783 
784  if (!(s->rdft = av_rdft_init(rdft_bits, DFT_R2C)) || !(s->irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
785  return AVERROR(ENOMEM);
786 
787  if (s->fft2 && !s->multi && inlink->channels > 1 && !(s->fft_ctx = av_fft_init(rdft_bits, 0)))
788  return AVERROR(ENOMEM);
789 
790  if (s->min_phase) {
791  int cepstrum_bits = rdft_bits + 2;
792  if (cepstrum_bits > RDFT_BITS_MAX) {
793  av_log(ctx, AV_LOG_ERROR, "too large delay, please decrease it.\n");
794  return AVERROR(EINVAL);
795  }
796 
797  cepstrum_bits = FFMIN(RDFT_BITS_MAX, cepstrum_bits + 1);
798  s->cepstrum_rdft = av_rdft_init(cepstrum_bits, DFT_R2C);
799  s->cepstrum_irdft = av_rdft_init(cepstrum_bits, IDFT_C2R);
800  if (!s->cepstrum_rdft || !s->cepstrum_irdft)
801  return AVERROR(ENOMEM);
802 
803  s->cepstrum_len = 1 << cepstrum_bits;
804  s->cepstrum_buf = av_malloc_array(s->cepstrum_len, sizeof(*s->cepstrum_buf));
805  if (!s->cepstrum_buf)
806  return AVERROR(ENOMEM);
807  }
808 
809  for ( ; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
810  s->analysis_rdft_len = 1 << rdft_bits;
811  if (inlink->sample_rate <= s->accuracy * s->analysis_rdft_len)
812  break;
813  }
814 
815  if (rdft_bits > RDFT_BITS_MAX) {
816  av_log(ctx, AV_LOG_ERROR, "too small accuracy, please increase it.\n");
817  return AVERROR(EINVAL);
818  }
819 
820  if (!(s->analysis_irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
821  return AVERROR(ENOMEM);
822 
823  if (s->dumpfile) {
824  s->analysis_rdft = av_rdft_init(rdft_bits, DFT_R2C);
825  s->dump_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->dump_buf));
826  }
827 
828  s->analysis_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->analysis_buf));
829  s->kernel_tmp_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_tmp_buf));
830  s->kernel_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_buf));
831  s->conv_buf = av_calloc(2 * s->rdft_len * inlink->channels, sizeof(*s->conv_buf));
832  s->conv_idx = av_calloc(inlink->channels, sizeof(*s->conv_idx));
833  if (!s->analysis_buf || !s->kernel_tmp_buf || !s->kernel_buf || !s->conv_buf || !s->conv_idx)
834  return AVERROR(ENOMEM);
835 
836  av_log(ctx, AV_LOG_DEBUG, "sample_rate = %d, channels = %d, analysis_rdft_len = %d, rdft_len = %d, fir_len = %d, nsamples_max = %d.\n",
837  inlink->sample_rate, inlink->channels, s->analysis_rdft_len, s->rdft_len, s->fir_len, s->nsamples_max);
838 
839  if (s->fixed)
840  inlink->min_samples = inlink->max_samples = inlink->partial_buf_size = s->nsamples_max;
841 
843 }
844 
846 {
847  AVFilterContext *ctx = inlink->dst;
848  FIREqualizerContext *s = ctx->priv;
849  int ch;
850 
851  if (!s->min_phase) {
852  for (ch = 0; ch + 1 < inlink->channels && s->fft_ctx; ch += 2) {
853  fast_convolute2(s, s->kernel_buf, (FFTComplex *)(s->conv_buf + 2 * ch * s->rdft_len),
854  s->conv_idx + ch, (float *) frame->extended_data[ch],
855  (float *) frame->extended_data[ch+1], frame->nb_samples);
856  }
857 
858  for ( ; ch < inlink->channels; ch++) {
859  fast_convolute(s, s->kernel_buf + (s->multi ? ch * s->rdft_len : 0),
860  s->conv_buf + 2 * ch * s->rdft_len, s->conv_idx + ch,
861  (float *) frame->extended_data[ch], frame->nb_samples);
862  }
863  } else {
864  for (ch = 0; ch < inlink->channels; ch++) {
865  fast_convolute_nonlinear(s, s->kernel_buf + (s->multi ? ch * s->rdft_len : 0),
866  s->conv_buf + 2 * ch * s->rdft_len, s->conv_idx + ch,
867  (float *) frame->extended_data[ch], frame->nb_samples);
868  }
869  }
870 
871  s->next_pts = AV_NOPTS_VALUE;
872  if (frame->pts != AV_NOPTS_VALUE) {
873  s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, av_make_q(1, inlink->sample_rate), inlink->time_base);
874  if (s->zero_phase && !s->min_phase)
875  frame->pts -= av_rescale_q(s->fir_len/2, av_make_q(1, inlink->sample_rate), inlink->time_base);
876  }
877  s->frame_nsamples_max = FFMAX(s->frame_nsamples_max, frame->nb_samples);
878  return ff_filter_frame(ctx->outputs[0], frame);
879 }
880 
881 static int request_frame(AVFilterLink *outlink)
882 {
883  AVFilterContext *ctx = outlink->src;
884  FIREqualizerContext *s= ctx->priv;
885  int ret;
886 
887  ret = ff_request_frame(ctx->inputs[0]);
888  if (ret == AVERROR_EOF && s->remaining > 0 && s->frame_nsamples_max > 0) {
889  AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(s->remaining, s->frame_nsamples_max));
890 
891  if (!frame)
892  return AVERROR(ENOMEM);
893 
894  av_samples_set_silence(frame->extended_data, 0, frame->nb_samples, outlink->channels, frame->format);
895  frame->pts = s->next_pts;
896  s->remaining -= frame->nb_samples;
897  ret = filter_frame(ctx->inputs[0], frame);
898  }
899 
900  return ret;
901 }
902 
903 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
904  char *res, int res_len, int flags)
905 {
906  FIREqualizerContext *s = ctx->priv;
907  int ret = AVERROR(ENOSYS);
908 
909  if (!strcmp(cmd, "gain")) {
910  char *gain_cmd;
911 
912  if (SELECT_GAIN(s) && !strcmp(SELECT_GAIN(s), args)) {
913  av_log(ctx, AV_LOG_DEBUG, "equal gain, do not rebuild.\n");
914  return 0;
915  }
916 
917  gain_cmd = av_strdup(args);
918  if (!gain_cmd)
919  return AVERROR(ENOMEM);
920 
921  ret = generate_kernel(ctx, gain_cmd, SELECT_GAIN_ENTRY(s));
922  if (ret >= 0) {
923  av_freep(&s->gain_cmd);
924  s->gain_cmd = gain_cmd;
925  } else {
926  av_freep(&gain_cmd);
927  }
928  } else if (!strcmp(cmd, "gain_entry")) {
929  char *gain_entry_cmd;
930 
931  if (SELECT_GAIN_ENTRY(s) && !strcmp(SELECT_GAIN_ENTRY(s), args)) {
932  av_log(ctx, AV_LOG_DEBUG, "equal gain_entry, do not rebuild.\n");
933  return 0;
934  }
935 
936  gain_entry_cmd = av_strdup(args);
937  if (!gain_entry_cmd)
938  return AVERROR(ENOMEM);
939 
940  ret = generate_kernel(ctx, SELECT_GAIN(s), gain_entry_cmd);
941  if (ret >= 0) {
942  av_freep(&s->gain_entry_cmd);
943  s->gain_entry_cmd = gain_entry_cmd;
944  } else {
945  av_freep(&gain_entry_cmd);
946  }
947  }
948 
949  return ret;
950 }
951 
953  {
954  .name = "default",
955  .config_props = config_input,
956  .filter_frame = filter_frame,
957  .type = AVMEDIA_TYPE_AUDIO,
958  .needs_writable = 1,
959  },
960  { NULL }
961 };
962 
964  {
965  .name = "default",
966  .request_frame = request_frame,
967  .type = AVMEDIA_TYPE_AUDIO,
968  },
969  { NULL }
970 };
971 
973  .name = "firequalizer",
974  .description = NULL_IF_CONFIG_SMALL("Finite Impulse Response Equalizer."),
975  .uninit = uninit,
976  .query_formats = query_formats,
977  .process_command = process_command,
978  .priv_size = sizeof(FIREqualizerContext),
981  .priv_class = &firequalizer_class,
982 };
FIREqualizerContext::cepstrum_len
int cepstrum_len
Definition: af_firequalizer.c:78
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
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
GainEntry::freq
double freq
Definition: af_firequalizer.c:57
FIREqualizerContext::gain_cmd
char * gain_cmd
Definition: af_firequalizer.c:93
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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
generate_kernel
static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
Definition: af_firequalizer.c:603
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
fft2
static void fft2(FFTComplex *z)
Definition: tx_template.c:505
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
gain_interpolate_func
static double gain_interpolate_func(void *p, double freq)
Definition: af_firequalizer.c:455
FIREqualizerContext::analysis_irdft
RDFTContext * analysis_irdft
Definition: af_firequalizer.c:70
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:429
im
float im
Definition: fft.c:82
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_firequalizer.c:845
AVOption
AVOption.
Definition: opt.h:247
b
#define b
Definition: input.c:40
FIREqualizerContext::frame_nsamples_max
int frame_nsamples_max
Definition: af_firequalizer.c:90
data
const char data[16]
Definition: mxf.c:143
OverlapIndex::buf_idx
int buf_idx
Definition: af_firequalizer.c:62
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:395
av_fft_permute
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
Definition: avfft.c:38
FIREqualizerContext::rdft_len
int rdft_len
Definition: af_firequalizer.c:77
VarOffset
VarOffset
Definition: af_firequalizer.c:545
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_firequalizer.c:183
SCALE_LOGLOG
@ SCALE_LOGLOG
Definition: af_firequalizer.c:51
FIREqualizerContext::wfunc
int wfunc
Definition: af_firequalizer.c:99
FIREqualizerContext::cepstrum_irdft
RDFTContext * cepstrum_irdft
Definition: af_firequalizer.c:75
SELECT_GAIN_ENTRY
#define SELECT_GAIN_ENTRY(s)
Definition: af_firequalizer.c:756
FIREqualizerContext::nb_gain_entry
int nb_gain_entry
Definition: af_firequalizer.c:109
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
FIREqualizerContext
Definition: af_firequalizer.c:66
WFUNC_RECTANGULAR
@ WFUNC_RECTANGULAR
Definition: af_firequalizer.c:34
win
static float win(SuperEqualizerContext *s, float n, int N)
Definition: af_superequalizer.c:119
av_expr_parse
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:685
NB_GAIN_ENTRY_MAX
#define NB_GAIN_ENTRY_MAX
Definition: af_firequalizer.c:55
VAR_CHLAYOUT
@ VAR_CHLAYOUT
Definition: af_firequalizer.c:551
WFUNC_BLACKMAN
@ WFUNC_BLACKMAN
Definition: af_firequalizer.c:37
generate_min_phase_kernel
static void generate_min_phase_kernel(FIREqualizerContext *s, float *rdft_buf)
Definition: af_firequalizer.c:555
firequalizer_outputs
static const AVFilterPad firequalizer_outputs[]
Definition: af_firequalizer.c:963
ff_af_firequalizer
const AVFilter ff_af_firequalizer
Definition: af_firequalizer.c:972
firequalizer_inputs
static const AVFilterPad firequalizer_inputs[]
Definition: af_firequalizer.c:952
WindowFunc
WindowFunc
Definition: af_firequalizer.c:33
VAR_SR
@ VAR_SR
Definition: af_firequalizer.c:547
GainEntry::gain
double gain
Definition: af_firequalizer.c:58
RDFT_BITS_MAX
#define RDFT_BITS_MAX
Definition: af_firequalizer.c:31
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: af_firequalizer.c:881
IDFT_C2R
@ IDFT_C2R
Definition: avfft.h:73
FIREqualizerContext::conv_idx
OverlapIndex * conv_idx
Definition: af_firequalizer.c:86
cubic_interpolate_func
static double cubic_interpolate_func(void *p, double freq)
Definition: af_firequalizer.c:490
av_expr_free
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:336
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
SELECT_GAIN
#define SELECT_GAIN(s)
Definition: af_firequalizer.c:755
avassert.h
FIREqualizerContext::scale
int scale
Definition: af_firequalizer.c:103
VAR_CHID
@ VAR_CHID
Definition: af_firequalizer.c:549
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
inputs
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 inputs
Definition: filter_design.txt:243
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:580
WFUNC_NUTTALL
@ WFUNC_NUTTALL
Definition: af_firequalizer.c:40
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_firequalizer.c:174
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
WFUNC_NUTTALL3
@ WFUNC_NUTTALL3
Definition: af_firequalizer.c:38
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
FIREqualizerContext::min_phase
int min_phase
Definition: af_firequalizer.c:107
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:766
WFUNC_BHARRIS
@ WFUNC_BHARRIS
Definition: af_firequalizer.c:42
OverlapIndex::overlap_idx
int overlap_idx
Definition: af_firequalizer.c:63
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
AVExpr
Definition: eval.c:157
key
const char * key
Definition: hwcontext_opencl.c:168
av_rdft_calc
void av_rdft_calc(RDFTContext *s, FFTSample *data)
if
if(ret)
Definition: filter_design.txt:179
SCALE_LINLIN
@ SCALE_LINLIN
Definition: af_firequalizer.c:48
FIREqualizerContext::analysis_rdft_len
int analysis_rdft_len
Definition: af_firequalizer.c:76
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
FIREqualizerContext::gain_entry_err
int gain_entry_err
Definition: af_firequalizer.c:110
vars
static const uint8_t vars[2][12]
Definition: camellia.c:179
FLAGS
#define FLAGS
Definition: af_firequalizer.c:115
WFUNC_HAMMING
@ WFUNC_HAMMING
Definition: af_firequalizer.c:36
isnan
#define isnan(x)
Definition: libm.h:340
FIREqualizerContext::fft_ctx
FFTContext * fft_ctx
Definition: af_firequalizer.c:73
GainEntry
Definition: af_firequalizer.c:56
FIREqualizerContext::zero_phase
int zero_phase
Definition: af_firequalizer.c:102
entry_func
static double entry_func(void *p, double freq, double gain)
Definition: af_firequalizer.c:414
FIREqualizerContext::gain
const char * gain
Definition: af_firequalizer.c:95
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(firequalizer)
isinf
#define isinf(x)
Definition: libm.h:317
DFT_R2C
@ DFT_R2C
Definition: avfft.h:72
avfft.h
OFFSET
#define OFFSET(x)
Definition: af_firequalizer.c:114
fp
#define fp
Definition: regdef.h:44
exp
int8_t exp
Definition: eval.c:72
var_names
static const char *const var_names[]
Definition: af_firequalizer.c:535
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
VAR_CHS
@ VAR_CHS
Definition: af_firequalizer.c:550
FIREqualizerContext::gain_entry_cmd
char * gain_entry_cmd
Definition: af_firequalizer.c:94
eval.h
RDFT_BITS_MIN
#define RDFT_BITS_MIN
Definition: af_firequalizer.c:30
TFLAGS
#define TFLAGS
Definition: af_firequalizer.c:116
av_rdft_init
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
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:116
av_expr_parse_and_eval
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:776
FIREqualizerContext::analysis_buf
float * analysis_buf
Definition: af_firequalizer.c:80
fast_convolute2
static void fast_convolute2(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, FFTComplex *av_restrict conv_buf, OverlapIndex *av_restrict idx, float *av_restrict data0, float *av_restrict data1, int nsamples)
Definition: af_firequalizer.c:291
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
WFUNC_TUKEY
@ WFUNC_TUKEY
Definition: af_firequalizer.c:43
FIREqualizerContext::fir_len
int fir_len
Definition: af_firequalizer.c:87
FFTComplex::im
FFTSample im
Definition: avfft.h:38
FFTComplex::re
FFTSample re
Definition: avfft.h:38
firequalizer_options
static const AVOption firequalizer_options[]
Definition: af_firequalizer.c:118
VAR_NB
@ VAR_NB
Definition: af_firequalizer.c:552
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
FIREqualizerContext::kernel_tmp_buf
float * kernel_tmp_buf
Definition: af_firequalizer.c:82
FIREqualizerContext::cepstrum_rdft
RDFTContext * cepstrum_rdft
Definition: af_firequalizer.c:74
M_PI
#define M_PI
Definition: mathematics.h:52
internal.h
FIREqualizerContext::cepstrum_buf
float * cepstrum_buf
Definition: af_firequalizer.c:84
FIREqualizerContext::remaining
int remaining
Definition: af_firequalizer.c:91
VAR_CH
@ VAR_CH
Definition: af_firequalizer.c:548
WFUNC_HANN
@ WFUNC_HANN
Definition: af_firequalizer.c:35
FFTContext
Definition: fft.h:83
i
int i
Definition: input.c:406
av_channel_layout_extract_channel
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
Definition: channel_layout.c:271
FIREqualizerContext::conv_buf
float * conv_buf
Definition: af_firequalizer.c:85
SCALE_LINLOG
@ SCALE_LINLOG
Definition: af_firequalizer.c:49
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
RDFTContext
Definition: rdft.h:28
FIREqualizerContext::dump_buf
float * dump_buf
Definition: af_firequalizer.c:81
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
FIREqualizerContext::gain_entry_tbl
GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX]
Definition: af_firequalizer.c:111
Scale
Scale
Definition: af_firequalizer.c:47
VAR_F
@ VAR_F
Definition: af_firequalizer.c:546
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_firequalizer.c:903
fast_convolute
static void fast_convolute(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, float *av_restrict conv_buf, OverlapIndex *av_restrict idx, float *av_restrict data, int nsamples)
Definition: af_firequalizer.c:213
FIREqualizerContext::kernel_buf
float * kernel_buf
Definition: af_firequalizer.c:83
log2
#define log2(x)
Definition: libm.h:404
av_samples_set_silence
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:244
FIREqualizerContext::next_pts
int64_t next_pts
Definition: af_firequalizer.c:89
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
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
SCALE_LOGLIN
@ SCALE_LOGLIN
Definition: af_firequalizer.c:50
FIREqualizerContext::accuracy
double accuracy
Definition: af_firequalizer.c:98
av_fft_init
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
Definition: avfft.c:28
NB_WFUNC
@ NB_WFUNC
Definition: af_firequalizer.c:44
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:414
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:251
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
FIREqualizerContext::fixed
int fixed
Definition: af_firequalizer.c:100
FIREqualizerContext::dumpscale
int dumpscale
Definition: af_firequalizer.c:105
FIREqualizerContext::nsamples_max
int nsamples_max
Definition: af_firequalizer.c:88
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
FIREqualizerContext::rdft
RDFTContext * rdft
Definition: af_firequalizer.c:71
common_uninit
static void common_uninit(FIREqualizerContext *s)
Definition: af_firequalizer.c:151
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:259
WFUNC_BNUTTALL
@ WFUNC_BNUTTALL
Definition: af_firequalizer.c:41
audio.h
fast_convolute_nonlinear
static void fast_convolute_nonlinear(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, float *av_restrict conv_buf, OverlapIndex *av_restrict idx, float *av_restrict data, int nsamples)
Definition: af_firequalizer.c:251
gain_entry_compare
static int gain_entry_compare(const void *key, const void *memb)
Definition: af_firequalizer.c:443
FIREqualizerContext::gain_entry
const char * gain_entry
Definition: af_firequalizer.c:96
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FIREqualizerContext::analysis_rdft
RDFTContext * analysis_rdft
Definition: af_firequalizer.c:69
d
d
Definition: ffmpeg_filter.c:156
fixed
#define fixed(width, name, value)
Definition: cbs_av1.c:566
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_rdft_end
void av_rdft_end(RDFTContext *s)
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
FIREqualizerContext::fft2
int fft2
Definition: af_firequalizer.c:106
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:568
NB_SCALE
@ NB_SCALE
Definition: af_firequalizer.c:52
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_firequalizer.c:758
OverlapIndex
Definition: af_firequalizer.c:61
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
FIREqualizerContext::delay
double delay
Definition: af_firequalizer.c:97
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
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
dump_fir
static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch)
Definition: af_firequalizer.c:355
FFTComplex
Definition: avfft.h:37
re
float re
Definition: fft.c:82
FIREqualizerContext::irdft
RDFTContext * irdft
Definition: af_firequalizer.c:72
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:561
FIREqualizerContext::multi
int multi
Definition: af_firequalizer.c:101
WFUNC_MNUTTALL3
@ WFUNC_MNUTTALL3
Definition: af_firequalizer.c:39
FIREqualizerContext::dumpfile
char * dumpfile
Definition: af_firequalizer.c:104