FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
21 #include "libavutil/opt.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/avassert.h"
24 #include "libavcodec/avfft.h"
25 #include "avfilter.h"
26 #include "internal.h"
27 #include "audio.h"
28 
29 #define RDFT_BITS_MIN 4
30 #define RDFT_BITS_MAX 16
31 
32 enum WindowFunc {
44 };
45 
46 enum Scale {
52 };
53 
54 #define NB_GAIN_ENTRY_MAX 4096
55 typedef struct {
56  double freq;
57  double gain;
58 } GainEntry;
59 
60 typedef struct {
61  int buf_idx;
63 } OverlapIndex;
64 
65 typedef struct {
66  const AVClass *class;
67 
73  int rdft_len;
74 
75  float *analysis_buf;
76  float *dump_buf;
78  float *kernel_buf;
79  float *conv_buf;
81  int fir_len;
83  int64_t next_pts;
85  int remaining;
86 
87  char *gain_cmd;
89  const char *gain;
90  const char *gain_entry;
91  double delay;
92  double accuracy;
93  int wfunc;
94  int fixed;
95  int multi;
97  int scale;
98  char *dumpfile;
99  int dumpscale;
100 
103  GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX];
105 
106 #define OFFSET(x) offsetof(FIREqualizerContext, x)
107 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
108 
109 static const AVOption firequalizer_options[] = {
110  { "gain", "set gain curve", OFFSET(gain), AV_OPT_TYPE_STRING, { .str = "gain_interpolate(f)" }, 0, 0, FLAGS },
111  { "gain_entry", "set gain entry", OFFSET(gain_entry), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
112  { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.0, 1e10, FLAGS },
113  { "accuracy", "set accuracy", OFFSET(accuracy), AV_OPT_TYPE_DOUBLE, { .dbl = 5.0 }, 0.0, 1e10, FLAGS },
114  { "wfunc", "set window function", OFFSET(wfunc), AV_OPT_TYPE_INT, { .i64 = WFUNC_HANN }, 0, NB_WFUNC-1, FLAGS, "wfunc" },
115  { "rectangular", "rectangular window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_RECTANGULAR }, 0, 0, FLAGS, "wfunc" },
116  { "hann", "hann window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HANN }, 0, 0, FLAGS, "wfunc" },
117  { "hamming", "hamming window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_HAMMING }, 0, 0, FLAGS, "wfunc" },
118  { "blackman", "blackman window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BLACKMAN }, 0, 0, FLAGS, "wfunc" },
119  { "nuttall3", "3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL3 }, 0, 0, FLAGS, "wfunc" },
120  { "mnuttall3", "minimum 3-term nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_MNUTTALL3 }, 0, 0, FLAGS, "wfunc" },
121  { "nuttall", "nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_NUTTALL }, 0, 0, FLAGS, "wfunc" },
122  { "bnuttall", "blackman-nuttall window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BNUTTALL }, 0, 0, FLAGS, "wfunc" },
123  { "bharris", "blackman-harris window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_BHARRIS }, 0, 0, FLAGS, "wfunc" },
124  { "tukey", "tukey window", 0, AV_OPT_TYPE_CONST, { .i64 = WFUNC_TUKEY }, 0, 0, FLAGS, "wfunc" },
125  { "fixed", "set fixed frame samples", OFFSET(fixed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
126  { "multi", "set multi channels mode", OFFSET(multi), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
127  { "zero_phase", "set zero phase mode", OFFSET(zero_phase), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
128  { "scale", "set gain scale", OFFSET(scale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" },
129  { "linlin", "linear-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLIN }, 0, 0, FLAGS, "scale" },
130  { "linlog", "linear-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LINLOG }, 0, 0, FLAGS, "scale" },
131  { "loglin", "logarithmic-freq linear-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLIN }, 0, 0, FLAGS, "scale" },
132  { "loglog", "logarithmic-freq logarithmic-gain", 0, AV_OPT_TYPE_CONST, { .i64 = SCALE_LOGLOG }, 0, 0, FLAGS, "scale" },
133  { "dumpfile", "set dump file", OFFSET(dumpfile), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
134  { "dumpscale", "set dump scale", OFFSET(dumpscale), AV_OPT_TYPE_INT, { .i64 = SCALE_LINLOG }, 0, NB_SCALE-1, FLAGS, "scale" },
135  { NULL }
136 };
137 
138 AVFILTER_DEFINE_CLASS(firequalizer);
139 
141 {
144  av_rdft_end(s->rdft);
145  av_rdft_end(s->irdft);
146  s->analysis_rdft = s->analysis_irdft = s->rdft = s->irdft = NULL;
147 
148  av_freep(&s->analysis_buf);
149  av_freep(&s->dump_buf);
151  av_freep(&s->kernel_buf);
152  av_freep(&s->conv_buf);
153  av_freep(&s->conv_idx);
154 }
155 
157 {
158  FIREqualizerContext *s = ctx->priv;
159 
160  common_uninit(s);
161  av_freep(&s->gain_cmd);
163 }
164 
166 {
169  static const enum AVSampleFormat sample_fmts[] = {
172  };
173  int ret;
174 
175  layouts = ff_all_channel_counts();
176  if (!layouts)
177  return AVERROR(ENOMEM);
178  ret = ff_set_common_channel_layouts(ctx, layouts);
179  if (ret < 0)
180  return ret;
181 
182  formats = ff_make_format_list(sample_fmts);
183  if (!formats)
184  return AVERROR(ENOMEM);
185  ret = ff_set_common_formats(ctx, formats);
186  if (ret < 0)
187  return ret;
188 
189  formats = ff_all_samplerates();
190  if (!formats)
191  return AVERROR(ENOMEM);
192  return ff_set_common_samplerates(ctx, formats);
193 }
194 
195 static void fast_convolute(FIREqualizerContext *s, const float *kernel_buf, float *conv_buf,
196  OverlapIndex *idx, float *data, int nsamples)
197 {
198  if (nsamples <= s->nsamples_max) {
199  float *buf = conv_buf + idx->buf_idx * s->rdft_len;
200  float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
201  int center = s->fir_len/2;
202  int k;
203 
204  memset(buf, 0, center * sizeof(*data));
205  memcpy(buf + center, data, nsamples * sizeof(*data));
206  memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) * sizeof(*data));
207  av_rdft_calc(s->rdft, buf);
208 
209  buf[0] *= kernel_buf[0];
210  buf[1] *= kernel_buf[s->rdft_len/2];
211  for (k = 1; k < s->rdft_len/2; k++) {
212  buf[2*k] *= kernel_buf[k];
213  buf[2*k+1] *= kernel_buf[k];
214  }
215 
216  av_rdft_calc(s->irdft, buf);
217  for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
218  buf[k] += obuf[k];
219  memcpy(data, buf, nsamples * sizeof(*data));
220  idx->buf_idx = !idx->buf_idx;
221  idx->overlap_idx = nsamples;
222  } else {
223  while (nsamples > s->nsamples_max * 2) {
224  fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max);
225  data += s->nsamples_max;
226  nsamples -= s->nsamples_max;
227  }
228  fast_convolute(s, kernel_buf, conv_buf, idx, data, nsamples/2);
229  fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2);
230  }
231 }
232 
233 static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch)
234 {
235  FIREqualizerContext *s = ctx->priv;
236  int rate = ctx->inputs[0]->sample_rate;
237  int xlog = s->dumpscale == SCALE_LOGLIN || s->dumpscale == SCALE_LOGLOG;
238  int ylog = s->dumpscale == SCALE_LINLOG || s->dumpscale == SCALE_LOGLOG;
239  int x;
240  int center = s->fir_len / 2;
241  double delay = s->zero_phase ? 0.0 : (double) center / rate;
242  double vx, ya, yb;
243 
244  s->analysis_buf[0] *= s->rdft_len/2;
245  for (x = 1; x <= center; x++) {
246  s->analysis_buf[x] *= s->rdft_len/2;
247  s->analysis_buf[s->analysis_rdft_len - x] *= s->rdft_len/2;
248  }
249 
250  if (ch)
251  fprintf(fp, "\n\n");
252 
253  fprintf(fp, "# time[%d] (time amplitude)\n", ch);
254 
255  for (x = center; x > 0; x--)
256  fprintf(fp, "%15.10f %15.10f\n", delay - (double) x / rate, (double) s->analysis_buf[s->analysis_rdft_len - x]);
257 
258  for (x = 0; x <= center; x++)
259  fprintf(fp, "%15.10f %15.10f\n", delay + (double)x / rate , (double) s->analysis_buf[x]);
260 
262 
263  fprintf(fp, "\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch);
264 
265  for (x = 0; x <= s->analysis_rdft_len/2; x++) {
266  int i = (x == s->analysis_rdft_len/2) ? 1 : 2 * x;
267  vx = (double)x * rate / s->analysis_rdft_len;
268  if (xlog)
269  vx = log2(0.05*vx);
270  ya = s->dump_buf[i];
271  yb = s->analysis_buf[i];
272  if (ylog) {
273  ya = 20.0 * log10(fabs(ya));
274  yb = 20.0 * log10(fabs(yb));
275  }
276  fprintf(fp, "%17.10f %17.10f %17.10f\n", vx, ya, yb);
277  }
278 }
279 
280 static double entry_func(void *p, double freq, double gain)
281 {
282  AVFilterContext *ctx = p;
283  FIREqualizerContext *s = ctx->priv;
284 
285  if (s->nb_gain_entry >= NB_GAIN_ENTRY_MAX) {
286  av_log(ctx, AV_LOG_ERROR, "entry table overflow.\n");
287  s->gain_entry_err = AVERROR(EINVAL);
288  return 0;
289  }
290 
291  if (isnan(freq)) {
292  av_log(ctx, AV_LOG_ERROR, "nan frequency (%g, %g).\n", freq, gain);
293  s->gain_entry_err = AVERROR(EINVAL);
294  return 0;
295  }
296 
297  if (s->nb_gain_entry > 0 && freq <= s->gain_entry_tbl[s->nb_gain_entry - 1].freq) {
298  av_log(ctx, AV_LOG_ERROR, "unsorted frequency (%g, %g).\n", freq, gain);
299  s->gain_entry_err = AVERROR(EINVAL);
300  return 0;
301  }
302 
303  s->gain_entry_tbl[s->nb_gain_entry].freq = freq;
304  s->gain_entry_tbl[s->nb_gain_entry].gain = gain;
305  s->nb_gain_entry++;
306  return 0;
307 }
308 
309 static int gain_entry_compare(const void *key, const void *memb)
310 {
311  const double *freq = key;
312  const GainEntry *entry = memb;
313 
314  if (*freq < entry[0].freq)
315  return -1;
316  if (*freq > entry[1].freq)
317  return 1;
318  return 0;
319 }
320 
321 static double gain_interpolate_func(void *p, double freq)
322 {
323  AVFilterContext *ctx = p;
324  FIREqualizerContext *s = ctx->priv;
325  GainEntry *res;
326  double d0, d1, d;
327 
328  if (isnan(freq))
329  return freq;
330 
331  if (!s->nb_gain_entry)
332  return 0;
333 
334  if (freq <= s->gain_entry_tbl[0].freq)
335  return s->gain_entry_tbl[0].gain;
336 
337  if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq)
338  return s->gain_entry_tbl[s->nb_gain_entry-1].gain;
339 
340  res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare);
341  av_assert0(res);
342 
343  d = res[1].freq - res[0].freq;
344  d0 = freq - res[0].freq;
345  d1 = res[1].freq - freq;
346 
347  if (d0 && d1)
348  return (d0 * res[1].gain + d1 * res[0].gain) / d;
349 
350  if (d0)
351  return res[1].gain;
352 
353  return res[0].gain;
354 }
355 
356 static double cubic_interpolate_func(void *p, double freq)
357 {
358  AVFilterContext *ctx = p;
359  FIREqualizerContext *s = ctx->priv;
360  GainEntry *res;
361  double x, x2, x3;
362  double a, b, c, d;
363  double m0, m1, m2, msum, unit;
364 
365  if (!s->nb_gain_entry)
366  return 0;
367 
368  if (freq <= s->gain_entry_tbl[0].freq)
369  return s->gain_entry_tbl[0].gain;
370 
371  if (freq >= s->gain_entry_tbl[s->nb_gain_entry-1].freq)
372  return s->gain_entry_tbl[s->nb_gain_entry-1].gain;
373 
374  res = bsearch(&freq, &s->gain_entry_tbl, s->nb_gain_entry - 1, sizeof(*res), gain_entry_compare);
375  av_assert0(res);
376 
377  unit = res[1].freq - res[0].freq;
378  m0 = res != s->gain_entry_tbl ?
379  unit * (res[0].gain - res[-1].gain) / (res[0].freq - res[-1].freq) : 0;
380  m1 = res[1].gain - res[0].gain;
381  m2 = res != s->gain_entry_tbl + s->nb_gain_entry - 2 ?
382  unit * (res[2].gain - res[1].gain) / (res[2].freq - res[1].freq) : 0;
383 
384  msum = fabs(m0) + fabs(m1);
385  m0 = msum > 0 ? (fabs(m0) * m1 + fabs(m1) * m0) / msum : 0;
386  msum = fabs(m1) + fabs(m2);
387  m1 = msum > 0 ? (fabs(m1) * m2 + fabs(m2) * m1) / msum : 0;
388 
389  d = res[0].gain;
390  c = m0;
391  b = 3 * res[1].gain - m1 - 2 * c - 3 * d;
392  a = res[1].gain - b - c - d;
393 
394  x = (freq - res[0].freq) / unit;
395  x2 = x * x;
396  x3 = x2 * x;
397 
398  return a * x3 + b * x2 + c * x + d;
399 }
400 
401 static const char *const var_names[] = {
402  "f",
403  "sr",
404  "ch",
405  "chid",
406  "chs",
407  "chlayout",
408  NULL
409 };
410 
411 enum VarOffset {
419 };
420 
421 static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
422 {
423  FIREqualizerContext *s = ctx->priv;
424  AVFilterLink *inlink = ctx->inputs[0];
425  const char *gain_entry_func_names[] = { "entry", NULL };
426  const char *gain_func_names[] = { "gain_interpolate", "cubic_interpolate", NULL };
427  double (*gain_entry_funcs[])(void *, double, double) = { entry_func, NULL };
428  double (*gain_funcs[])(void *, double) = { gain_interpolate_func, cubic_interpolate_func, NULL };
429  double vars[VAR_NB];
430  AVExpr *gain_expr;
431  int ret, k, center, ch;
432  int xlog = s->scale == SCALE_LOGLIN || s->scale == SCALE_LOGLOG;
433  int ylog = s->scale == SCALE_LINLOG || s->scale == SCALE_LOGLOG;
434  FILE *dump_fp = NULL;
435 
436  s->nb_gain_entry = 0;
437  s->gain_entry_err = 0;
438  if (gain_entry) {
439  double result = 0.0;
440  ret = av_expr_parse_and_eval(&result, gain_entry, NULL, NULL, NULL, NULL,
441  gain_entry_func_names, gain_entry_funcs, ctx, 0, ctx);
442  if (ret < 0)
443  return ret;
444  if (s->gain_entry_err < 0)
445  return s->gain_entry_err;
446  }
447 
448  av_log(ctx, AV_LOG_DEBUG, "nb_gain_entry = %d.\n", s->nb_gain_entry);
449 
450  ret = av_expr_parse(&gain_expr, gain, var_names,
451  gain_func_names, gain_funcs, NULL, NULL, 0, ctx);
452  if (ret < 0)
453  return ret;
454 
455  if (s->dumpfile && (!s->dump_buf || !s->analysis_rdft || !(dump_fp = fopen(s->dumpfile, "w"))))
456  av_log(ctx, AV_LOG_WARNING, "dumping failed.\n");
457 
458  vars[VAR_CHS] = inlink->channels;
459  vars[VAR_CHLAYOUT] = inlink->channel_layout;
460  vars[VAR_SR] = inlink->sample_rate;
461  for (ch = 0; ch < inlink->channels; ch++) {
462  float *rdft_buf = s->kernel_tmp_buf + ch * s->rdft_len;
463  double result;
464  vars[VAR_CH] = ch;
466  vars[VAR_F] = 0.0;
467  if (xlog)
468  vars[VAR_F] = log2(0.05 * vars[VAR_F]);
469  result = av_expr_eval(gain_expr, vars, ctx);
470  s->analysis_buf[0] = ylog ? pow(10.0, 0.05 * result) : result;
471 
472  vars[VAR_F] = 0.5 * inlink->sample_rate;
473  if (xlog)
474  vars[VAR_F] = log2(0.05 * vars[VAR_F]);
475  result = av_expr_eval(gain_expr, vars, ctx);
476  s->analysis_buf[1] = ylog ? pow(10.0, 0.05 * result) : result;
477 
478  for (k = 1; k < s->analysis_rdft_len/2; k++) {
479  vars[VAR_F] = k * ((double)inlink->sample_rate /(double)s->analysis_rdft_len);
480  if (xlog)
481  vars[VAR_F] = log2(0.05 * vars[VAR_F]);
482  result = av_expr_eval(gain_expr, vars, ctx);
483  s->analysis_buf[2*k] = ylog ? pow(10.0, 0.05 * result) : result;
484  s->analysis_buf[2*k+1] = 0.0;
485  }
486 
487  if (s->dump_buf)
488  memcpy(s->dump_buf, s->analysis_buf, s->analysis_rdft_len * sizeof(*s->analysis_buf));
489 
491  center = s->fir_len / 2;
492 
493  for (k = 0; k <= center; k++) {
494  double u = k * (M_PI/center);
495  double win;
496  switch (s->wfunc) {
497  case WFUNC_RECTANGULAR:
498  win = 1.0;
499  break;
500  case WFUNC_HANN:
501  win = 0.5 + 0.5 * cos(u);
502  break;
503  case WFUNC_HAMMING:
504  win = 0.53836 + 0.46164 * cos(u);
505  break;
506  case WFUNC_BLACKMAN:
507  win = 0.42 + 0.5 * cos(u) + 0.08 * cos(2*u);
508  break;
509  case WFUNC_NUTTALL3:
510  win = 0.40897 + 0.5 * cos(u) + 0.09103 * cos(2*u);
511  break;
512  case WFUNC_MNUTTALL3:
513  win = 0.4243801 + 0.4973406 * cos(u) + 0.0782793 * cos(2*u);
514  break;
515  case WFUNC_NUTTALL:
516  win = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u);
517  break;
518  case WFUNC_BNUTTALL:
519  win = 0.3635819 + 0.4891775 * cos(u) + 0.1365995 * cos(2*u) + 0.0106411 * cos(3*u);
520  break;
521  case WFUNC_BHARRIS:
522  win = 0.35875 + 0.48829 * cos(u) + 0.14128 * cos(2*u) + 0.01168 * cos(3*u);
523  break;
524  case WFUNC_TUKEY:
525  win = (u <= 0.5 * M_PI) ? 1.0 : (0.5 + 0.5 * cos(2*u - M_PI));
526  break;
527  default:
528  av_assert0(0);
529  }
530  s->analysis_buf[k] *= (2.0/s->analysis_rdft_len) * (2.0/s->rdft_len) * win;
531  if (k)
532  s->analysis_buf[s->analysis_rdft_len - k] = s->analysis_buf[k];
533  }
534 
535  memset(s->analysis_buf + center + 1, 0, (s->analysis_rdft_len - s->fir_len) * sizeof(*s->analysis_buf));
536  memcpy(rdft_buf, s->analysis_buf, s->rdft_len/2 * sizeof(*s->analysis_buf));
537  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));
538  av_rdft_calc(s->rdft, rdft_buf);
539 
540  for (k = 0; k < s->rdft_len; k++) {
541  if (isnan(rdft_buf[k]) || isinf(rdft_buf[k])) {
542  av_log(ctx, AV_LOG_ERROR, "filter kernel contains nan or infinity.\n");
543  av_expr_free(gain_expr);
544  if (dump_fp)
545  fclose(dump_fp);
546  return AVERROR(EINVAL);
547  }
548  }
549 
550  rdft_buf[s->rdft_len-1] = rdft_buf[1];
551  for (k = 0; k < s->rdft_len/2; k++)
552  rdft_buf[k] = rdft_buf[2*k];
553  rdft_buf[s->rdft_len/2] = rdft_buf[s->rdft_len-1];
554 
555  if (dump_fp)
556  dump_fir(ctx, dump_fp, ch);
557 
558  if (!s->multi)
559  break;
560  }
561 
562  memcpy(s->kernel_buf, s->kernel_tmp_buf, (s->multi ? inlink->channels : 1) * s->rdft_len * sizeof(*s->kernel_buf));
563  av_expr_free(gain_expr);
564  if (dump_fp)
565  fclose(dump_fp);
566  return 0;
567 }
568 
569 #define SELECT_GAIN(s) (s->gain_cmd ? s->gain_cmd : s->gain)
570 #define SELECT_GAIN_ENTRY(s) (s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry)
571 
572 static int config_input(AVFilterLink *inlink)
573 {
574  AVFilterContext *ctx = inlink->dst;
575  FIREqualizerContext *s = ctx->priv;
576  int rdft_bits;
577 
578  common_uninit(s);
579 
580  s->next_pts = 0;
581  s->frame_nsamples_max = 0;
582 
583  s->fir_len = FFMAX(2 * (int)(inlink->sample_rate * s->delay) + 1, 3);
584  s->remaining = s->fir_len - 1;
585 
586  for (rdft_bits = RDFT_BITS_MIN; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
587  s->rdft_len = 1 << rdft_bits;
588  s->nsamples_max = s->rdft_len - s->fir_len + 1;
589  if (s->nsamples_max * 2 >= s->fir_len)
590  break;
591  }
592 
593  if (rdft_bits > RDFT_BITS_MAX) {
594  av_log(ctx, AV_LOG_ERROR, "too large delay, please decrease it.\n");
595  return AVERROR(EINVAL);
596  }
597 
598  if (!(s->rdft = av_rdft_init(rdft_bits, DFT_R2C)) || !(s->irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
599  return AVERROR(ENOMEM);
600 
601  for ( ; rdft_bits <= RDFT_BITS_MAX; rdft_bits++) {
602  s->analysis_rdft_len = 1 << rdft_bits;
603  if (inlink->sample_rate <= s->accuracy * s->analysis_rdft_len)
604  break;
605  }
606 
607  if (rdft_bits > RDFT_BITS_MAX) {
608  av_log(ctx, AV_LOG_ERROR, "too small accuracy, please increase it.\n");
609  return AVERROR(EINVAL);
610  }
611 
612  if (!(s->analysis_irdft = av_rdft_init(rdft_bits, IDFT_C2R)))
613  return AVERROR(ENOMEM);
614 
615  if (s->dumpfile) {
616  s->analysis_rdft = av_rdft_init(rdft_bits, DFT_R2C);
617  s->dump_buf = av_malloc_array(s->analysis_rdft_len, sizeof(*s->dump_buf));
618  }
619 
621  s->kernel_tmp_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_tmp_buf));
622  s->kernel_buf = av_malloc_array(s->rdft_len * (s->multi ? inlink->channels : 1), sizeof(*s->kernel_buf));
623  s->conv_buf = av_calloc(2 * s->rdft_len * inlink->channels, sizeof(*s->conv_buf));
624  s->conv_idx = av_calloc(inlink->channels, sizeof(*s->conv_idx));
625  if (!s->analysis_buf || !s->kernel_tmp_buf || !s->kernel_buf || !s->conv_buf || !s->conv_idx)
626  return AVERROR(ENOMEM);
627 
628  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",
629  inlink->sample_rate, inlink->channels, s->analysis_rdft_len, s->rdft_len, s->fir_len, s->nsamples_max);
630 
631  if (s->fixed)
632  inlink->min_samples = inlink->max_samples = inlink->partial_buf_size = s->nsamples_max;
633 
634  return generate_kernel(ctx, SELECT_GAIN(s), SELECT_GAIN_ENTRY(s));
635 }
636 
637 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
638 {
639  AVFilterContext *ctx = inlink->dst;
640  FIREqualizerContext *s = ctx->priv;
641  int ch;
642 
643  for (ch = 0; ch < inlink->channels; ch++) {
644  fast_convolute(s, s->kernel_buf + (s->multi ? ch * s->rdft_len : 0),
645  s->conv_buf + 2 * ch * s->rdft_len, s->conv_idx + ch,
646  (float *) frame->extended_data[ch], frame->nb_samples);
647  }
648 
650  if (frame->pts != AV_NOPTS_VALUE) {
651  s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, av_make_q(1, inlink->sample_rate), inlink->time_base);
652  if (s->zero_phase)
653  frame->pts -= av_rescale_q(s->fir_len/2, av_make_q(1, inlink->sample_rate), inlink->time_base);
654  }
656  return ff_filter_frame(ctx->outputs[0], frame);
657 }
658 
659 static int request_frame(AVFilterLink *outlink)
660 {
661  AVFilterContext *ctx = outlink->src;
662  FIREqualizerContext *s= ctx->priv;
663  int ret;
664 
665  ret = ff_request_frame(ctx->inputs[0]);
666  if (ret == AVERROR_EOF && s->remaining > 0 && s->frame_nsamples_max > 0) {
668 
669  if (!frame)
670  return AVERROR(ENOMEM);
671 
672  av_samples_set_silence(frame->extended_data, 0, frame->nb_samples, outlink->channels, frame->format);
673  frame->pts = s->next_pts;
674  s->remaining -= frame->nb_samples;
675  ret = filter_frame(ctx->inputs[0], frame);
676  }
677 
678  return ret;
679 }
680 
681 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
682  char *res, int res_len, int flags)
683 {
684  FIREqualizerContext *s = ctx->priv;
685  int ret = AVERROR(ENOSYS);
686 
687  if (!strcmp(cmd, "gain")) {
688  char *gain_cmd;
689 
690  if (SELECT_GAIN(s) && !strcmp(SELECT_GAIN(s), args)) {
691  av_log(ctx, AV_LOG_DEBUG, "equal gain, do not rebuild.\n");
692  return 0;
693  }
694 
695  gain_cmd = av_strdup(args);
696  if (!gain_cmd)
697  return AVERROR(ENOMEM);
698 
699  ret = generate_kernel(ctx, gain_cmd, SELECT_GAIN_ENTRY(s));
700  if (ret >= 0) {
701  av_freep(&s->gain_cmd);
702  s->gain_cmd = gain_cmd;
703  } else {
704  av_freep(&gain_cmd);
705  }
706  } else if (!strcmp(cmd, "gain_entry")) {
707  char *gain_entry_cmd;
708 
709  if (SELECT_GAIN_ENTRY(s) && !strcmp(SELECT_GAIN_ENTRY(s), args)) {
710  av_log(ctx, AV_LOG_DEBUG, "equal gain_entry, do not rebuild.\n");
711  return 0;
712  }
713 
714  gain_entry_cmd = av_strdup(args);
715  if (!gain_entry_cmd)
716  return AVERROR(ENOMEM);
717 
718  ret = generate_kernel(ctx, SELECT_GAIN(s), gain_entry_cmd);
719  if (ret >= 0) {
721  s->gain_entry_cmd = gain_entry_cmd;
722  } else {
723  av_freep(&gain_entry_cmd);
724  }
725  }
726 
727  return ret;
728 }
729 
731  {
732  .name = "default",
733  .config_props = config_input,
734  .filter_frame = filter_frame,
735  .type = AVMEDIA_TYPE_AUDIO,
736  .needs_writable = 1,
737  },
738  { NULL }
739 };
740 
742  {
743  .name = "default",
744  .request_frame = request_frame,
745  .type = AVMEDIA_TYPE_AUDIO,
746  },
747  { NULL }
748 };
749 
751  .name = "firequalizer",
752  .description = NULL_IF_CONFIG_SMALL("Finite Impulse Response Equalizer."),
753  .uninit = uninit,
754  .query_formats = query_formats,
755  .process_command = process_command,
756  .priv_size = sizeof(FIREqualizerContext),
757  .inputs = firequalizer_inputs,
758  .outputs = firequalizer_outputs,
759  .priv_class = &firequalizer_class,
760 };
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
const char * s
Definition: avisynth_c.h:768
#define isinf(x)
Definition: libm.h:317
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
RDFTContext * rdft
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static void common_uninit(FIREqualizerContext *s)
Main libavfilter public API header.
static double gain_interpolate_func(void *p, double freq)
const char * b
Definition: vf_curves.c:113
static enum AVSampleFormat formats[]
Definition: avresample.c:163
static int request_frame(AVFilterLink *outlink)
#define SELECT_GAIN(s)
static int config_input(AVFilterLink *inlink)
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:658
#define SELECT_GAIN_ENTRY(s)
#define log2(x)
Definition: libm.h:404
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:260
#define NB_GAIN_ENTRY_MAX
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define RDFT_BITS_MIN
static const AVFilterPad firequalizer_outputs[]
const char * name
Pad name.
Definition: internal.h:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:315
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
VarOffset
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1189
RDFTContext * irdft
#define av_cold
Definition: attributes.h:82
AVOptions.
static double cubic_interpolate_func(void *p, double freq)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
#define RDFT_BITS_MAX
Definition: eval.c:149
static AVFrame * frame
static const char *const var_names[]
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:53
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
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:723
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
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:237
static void fast_convolute(FIREqualizerContext *s, const float *kernel_buf, float *conv_buf, OverlapIndex *idx, float *data, int nsamples)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:64
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVFILTER_DEFINE_CLASS(firequalizer)
void * priv
private data for use by the filter
Definition: avfilter.h:322
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
simple assert() macros that are a bit more flexible than ISO C assert().
Definition: avfft.h:73
#define FFMAX(a, b)
Definition: common.h:94
RDFTContext * analysis_irdft
static av_cold void uninit(AVFilterContext *ctx)
static const AVOption firequalizer_options[]
void av_rdft_calc(RDFTContext *s, FFTSample *data)
static const AVFilterPad firequalizer_inputs[]
WindowFunc
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
#define FLAGS
const char * gain_entry
Definition: avfft.h:72
void av_rdft_end(RDFTContext *s)
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
OverlapIndex * conv_idx
A list of supported channel layouts.
Definition: formats.h:85
static double entry_func(void *p, double freq, double gain)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:248
static const uint8_t vars[2][12]
Definition: camellia.c:179
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
#define OFFSET(x)
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:318
AVFilter ff_af_firequalizer
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
FFT functions.
Scale
#define fp
Definition: regdef.h:44
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
#define isnan(x)
Definition: libm.h:340
GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX]
const char * name
Filter name.
Definition: avfilter.h:148
#define u(width,...)
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch)
static int flags
Definition: cpu.c:47
if(ret< 0)
Definition: vf_mcdeint.c:282
RDFTContext * analysis_rdft
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
static double c[64]
static int query_formats(AVFilterContext *ctx)
static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:713
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
An instance of a filter.
Definition: avfilter.h:307
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
#define M_PI
Definition: mathematics.h:52
#define av_malloc_array(a, b)
static int gain_entry_compare(const void *key, const void *memb)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:369
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:231
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
simple arithmetic expression evaluator