FFmpeg
af_biquads.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  * Copyright (c) 2006-2008 Rob Sykes <robs@users.sourceforge.net>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /*
23  * 2-pole filters designed by Robert Bristow-Johnson <rbj@audioimagination.com>
24  * see http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
25  *
26  * 1-pole filters based on code (c) 2000 Chris Bagwell <cbagwell@sprynet.com>
27  * Algorithms: Recursive single pole low/high pass filter
28  * Reference: The Scientist and Engineer's Guide to Digital Signal Processing
29  *
30  * low-pass: output[N] = input[N] * A + output[N-1] * B
31  * X = exp(-2.0 * pi * Fc)
32  * A = 1 - X
33  * B = X
34  * Fc = cutoff freq / sample rate
35  *
36  * Mimics an RC low-pass filter:
37  *
38  * ---/\/\/\/\----------->
39  * |
40  * --- C
41  * ---
42  * |
43  * |
44  * V
45  *
46  * high-pass: output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
47  * X = exp(-2.0 * pi * Fc)
48  * A0 = (1 + X) / 2
49  * A1 = -(1 + X) / 2
50  * B1 = X
51  * Fc = cutoff freq / sample rate
52  *
53  * Mimics an RC high-pass filter:
54  *
55  * || C
56  * ----||--------->
57  * || |
58  * <
59  * > R
60  * <
61  * |
62  * V
63  */
64 
65 #include "config_components.h"
66 
67 #include "libavutil/avassert.h"
69 #include "libavutil/ffmath.h"
70 #include "libavutil/mem.h"
71 #include "libavutil/opt.h"
72 #include "audio.h"
73 #include "avfilter.h"
74 #include "filters.h"
75 #include "formats.h"
76 #include "internal.h"
77 
78 enum FilterType {
91 };
92 
93 enum WidthType {
101 };
102 
104  DI,
112 };
113 
114 typedef struct BiquadsContext {
115  const AVClass *class;
116 
119  int poles;
120  int csg;
124 
125  int bypass;
126 
127  double gain;
128  double frequency;
129  double width;
130  double mix;
134  int order;
135 
136  double a_double[3];
137  double b_double[3];
138 
139  float a_float[3];
140  float b_float[3];
141 
142  double oa[3];
143  double ob[3];
144 
146 
147  int *clip;
150 
151  int64_t pts;
153 
154  void (*filter)(struct BiquadsContext *s, const void *ibuf, void *obuf, int len,
155  void *cache, int *clip, int disabled);
157 
159 {
160  BiquadsContext *s = ctx->priv;
161  static const enum AVSampleFormat auto_sample_fmts[] = {
167  };
168  enum AVSampleFormat sample_fmts[] = {
171  };
172  const enum AVSampleFormat *sample_fmts_list = sample_fmts;
174  if (ret < 0)
175  return ret;
176 
177  switch (s->precision) {
178  case 0:
180  break;
181  case 1:
183  break;
184  case 2:
186  break;
187  case 3:
189  break;
190  default:
191  sample_fmts_list = auto_sample_fmts;
192  break;
193  }
194  ret = ff_set_common_formats_from_list(ctx, sample_fmts_list);
195  if (ret < 0)
196  return ret;
197 
199 }
200 
201 #define BIQUAD_FILTER(name, type, ftype, min, max, need_clipping) \
202 static void biquad_## name (BiquadsContext *s, \
203  const void *input, void *output, int len, \
204  void *cache, int *clippings, int disabled) \
205 { \
206  const type *ibuf = input; \
207  type *obuf = output; \
208  ftype *fcache = cache; \
209  ftype i1 = fcache[0], i2 = fcache[1], o1 = fcache[2], o2 = fcache[3]; \
210  ftype *a = s->a_##ftype; \
211  ftype *b = s->b_##ftype; \
212  ftype a1 = -a[1]; \
213  ftype a2 = -a[2]; \
214  ftype b0 = b[0]; \
215  ftype b1 = b[1]; \
216  ftype b2 = b[2]; \
217  ftype wet = s->mix; \
218  ftype dry = 1. - wet; \
219  ftype out; \
220  int i; \
221  \
222  for (i = 0; i+1 < len; i++) { \
223  o2 = i2 * b2 + i1 * b1 + ibuf[i] * b0 + o2 * a2 + o1 * a1; \
224  i2 = ibuf[i]; \
225  out = o2 * wet + i2 * dry; \
226  if (disabled) { \
227  obuf[i] = i2; \
228  } else if (need_clipping && out < min) { \
229  (*clippings)++; \
230  obuf[i] = min; \
231  } else if (need_clipping && out > max) { \
232  (*clippings)++; \
233  obuf[i] = max; \
234  } else { \
235  obuf[i] = out; \
236  } \
237  i++; \
238  o1 = i1 * b2 + i2 * b1 + ibuf[i] * b0 + o1 * a2 + o2 * a1; \
239  i1 = ibuf[i]; \
240  out = o1 * wet + i1 * dry; \
241  if (disabled) { \
242  obuf[i] = i1; \
243  } else if (need_clipping && out < min) { \
244  (*clippings)++; \
245  obuf[i] = min; \
246  } else if (need_clipping && out > max) { \
247  (*clippings)++; \
248  obuf[i] = max; \
249  } else { \
250  obuf[i] = out; \
251  } \
252  } \
253  if (i < len) { \
254  ftype o0 = ibuf[i] * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
255  i2 = i1; \
256  i1 = ibuf[i]; \
257  o2 = o1; \
258  o1 = o0; \
259  out = o0 * wet + i1 * dry; \
260  if (disabled) { \
261  obuf[i] = i1; \
262  } else if (need_clipping && out < min) { \
263  (*clippings)++; \
264  obuf[i] = min; \
265  } else if (need_clipping && out > max) { \
266  (*clippings)++; \
267  obuf[i] = max; \
268  } else { \
269  obuf[i] = out; \
270  } \
271  } \
272  fcache[0] = i1; \
273  fcache[1] = i2; \
274  fcache[2] = o1; \
275  fcache[3] = o2; \
276 }
277 
278 BIQUAD_FILTER(s16, int16_t, float, INT16_MIN, INT16_MAX, 1)
279 BIQUAD_FILTER(s32, int32_t, double, INT32_MIN, INT32_MAX, 1)
280 BIQUAD_FILTER(flt, float, float, -1.f, 1.f, 0)
281 BIQUAD_FILTER(dbl, double, double, -1., 1., 0)
282 
283 #define BIQUAD_DII_FILTER(name, type, ftype, min, max, need_clipping) \
284 static void biquad_dii_## name (BiquadsContext *s, \
285  const void *input, void *output, int len, \
286  void *cache, int *clippings, int disabled) \
287 { \
288  const type *ibuf = input; \
289  type *obuf = output; \
290  ftype *fcache = cache; \
291  ftype *a = s->a_##ftype; \
292  ftype *b = s->b_##ftype; \
293  ftype a1 = -a[1]; \
294  ftype a2 = -a[2]; \
295  ftype b0 = b[0]; \
296  ftype b1 = b[1]; \
297  ftype b2 = b[2]; \
298  ftype w1 = fcache[0]; \
299  ftype w2 = fcache[1]; \
300  ftype wet = s->mix; \
301  ftype dry = 1. - wet; \
302  ftype in, out, w0; \
303  \
304  for (int i = 0; i < len; i++) { \
305  in = ibuf[i]; \
306  w0 = in + a1 * w1 + a2 * w2; \
307  out = b0 * w0 + b1 * w1 + b2 * w2; \
308  w2 = w1; \
309  w1 = w0; \
310  out = out * wet + in * dry; \
311  if (disabled) { \
312  obuf[i] = in; \
313  } else if (need_clipping && out < min) { \
314  (*clippings)++; \
315  obuf[i] = min; \
316  } else if (need_clipping && out > max) { \
317  (*clippings)++; \
318  obuf[i] = max; \
319  } else { \
320  obuf[i] = out; \
321  } \
322  } \
323  fcache[0] = w1; \
324  fcache[1] = w2; \
325 }
326 
327 BIQUAD_DII_FILTER(s16, int16_t, float, INT16_MIN, INT16_MAX, 1)
328 BIQUAD_DII_FILTER(s32, int32_t, double, INT32_MIN, INT32_MAX, 1)
329 BIQUAD_DII_FILTER(flt, float, float, -1.f, 1.f, 0)
330 BIQUAD_DII_FILTER(dbl, double, double, -1., 1., 0)
331 
332 #define BIQUAD_TDI_FILTER(name, type, ftype, min, max, need_clipping) \
333 static void biquad_tdi_## name (BiquadsContext *s, \
334  const void *input, void *output, int len, \
335  void *cache, int *clippings, int disabled) \
336 { \
337  const type *ibuf = input; \
338  type *obuf = output; \
339  ftype *fcache = cache; \
340  ftype *a = s->a_##ftype; \
341  ftype *b = s->b_##ftype; \
342  ftype a1 = -a[1]; \
343  ftype a2 = -a[2]; \
344  ftype b0 = b[0]; \
345  ftype b1 = b[1]; \
346  ftype b2 = b[2]; \
347  ftype s1 = fcache[0]; \
348  ftype s2 = fcache[1]; \
349  ftype s3 = fcache[2]; \
350  ftype s4 = fcache[3]; \
351  ftype wet = s->mix; \
352  ftype dry = 1. - wet; \
353  ftype in, out; \
354  \
355  for (int i = 0; i < len; i++) { \
356  ftype t1, t2, t3, t4; \
357  in = ibuf[i] + s1; \
358  t1 = in * a1 + s2; \
359  t2 = in * a2; \
360  t3 = in * b1 + s4; \
361  t4 = in * b2; \
362  out = b0 * in + s3; \
363  out = out * wet + in * dry; \
364  s1 = t1; s2 = t2; s3 = t3; s4 = t4; \
365  if (disabled) { \
366  obuf[i] = in; \
367  } else if (need_clipping && out < min) { \
368  (*clippings)++; \
369  obuf[i] = min; \
370  } else if (need_clipping && out > max) { \
371  (*clippings)++; \
372  obuf[i] = max; \
373  } else { \
374  obuf[i] = out; \
375  } \
376  } \
377  \
378  fcache[0] = s1; \
379  fcache[1] = s2; \
380  fcache[2] = s3; \
381  fcache[3] = s4; \
382 }
383 
384 BIQUAD_TDI_FILTER(s16, int16_t, float, INT16_MIN, INT16_MAX, 1)
385 BIQUAD_TDI_FILTER(s32, int32_t, double, INT32_MIN, INT32_MAX, 1)
386 BIQUAD_TDI_FILTER(flt, float, float, -1.f, 1.f, 0)
387 BIQUAD_TDI_FILTER(dbl, double, double, -1., 1., 0)
388 
389 #define BIQUAD_TDII_FILTER(name, type, ftype, min, max, need_clipping) \
390 static void biquad_tdii_## name (BiquadsContext *s, \
391  const void *input, void *output, int len, \
392  void *cache, int *clippings, int disabled) \
393 { \
394  const type *ibuf = input; \
395  type *obuf = output; \
396  ftype *fcache = cache; \
397  ftype *a = s->a_##ftype; \
398  ftype *b = s->b_##ftype; \
399  ftype a1 = -a[1]; \
400  ftype a2 = -a[2]; \
401  ftype b0 = b[0]; \
402  ftype b1 = b[1]; \
403  ftype b2 = b[2]; \
404  ftype w1 = fcache[0]; \
405  ftype w2 = fcache[1]; \
406  ftype wet = s->mix; \
407  ftype dry = 1. - wet; \
408  ftype in, out; \
409  \
410  for (int i = 0; i < len; i++) { \
411  in = ibuf[i]; \
412  out = b0 * in + w1; \
413  w1 = b1 * in + w2 + a1 * out; \
414  w2 = b2 * in + a2 * out; \
415  out = out * wet + in * dry; \
416  if (disabled) { \
417  obuf[i] = in; \
418  } else if (need_clipping && out < min) { \
419  (*clippings)++; \
420  obuf[i] = min; \
421  } else if (need_clipping && out > max) { \
422  (*clippings)++; \
423  obuf[i] = max; \
424  } else { \
425  obuf[i] = out; \
426  } \
427  } \
428  fcache[0] = w1; \
429  fcache[1] = w2; \
430 }
431 
432 BIQUAD_TDII_FILTER(s16, int16_t, float, INT16_MIN, INT16_MAX, 1)
433 BIQUAD_TDII_FILTER(s32, int32_t, double, INT32_MIN, INT32_MAX, 1)
434 BIQUAD_TDII_FILTER(flt, float, float, -1.f, 1.f, 0)
435 BIQUAD_TDII_FILTER(dbl, double, double, -1., 1., 0)
436 
437 #define BIQUAD_LATT_FILTER(name, type, ftype, min, max, need_clipping) \
438 static void biquad_latt_## name (BiquadsContext *s, \
439  const void *input, void *output, int len, \
440  void *cache, int *clippings, int disabled) \
441 { \
442  const type *ibuf = input; \
443  type *obuf = output; \
444  ftype *fcache = cache; \
445  ftype *a = s->a_##ftype; \
446  ftype *b = s->b_##ftype; \
447  ftype k0 = a[1]; \
448  ftype k1 = a[2]; \
449  ftype v0 = b[0]; \
450  ftype v1 = b[1]; \
451  ftype v2 = b[2]; \
452  ftype s0 = fcache[0]; \
453  ftype s1 = fcache[1]; \
454  ftype wet = s->mix; \
455  ftype dry = 1. - wet; \
456  ftype in, out; \
457  ftype t0, t1; \
458  \
459  for (int i = 0; i < len; i++) { \
460  out = 0.; \
461  in = ibuf[i]; \
462  t0 = in - k1 * s0; \
463  t1 = t0 * k1 + s0; \
464  out += t1 * v2; \
465  \
466  t0 = t0 - k0 * s1; \
467  t1 = t0 * k0 + s1; \
468  out += t1 * v1; \
469  \
470  out += t0 * v0; \
471  s0 = t1; \
472  s1 = t0; \
473  \
474  out = out * wet + in * dry; \
475  if (disabled) { \
476  obuf[i] = in; \
477  } else if (need_clipping && out < min) { \
478  (*clippings)++; \
479  obuf[i] = min; \
480  } else if (need_clipping && out > max) { \
481  (*clippings)++; \
482  obuf[i] = max; \
483  } else { \
484  obuf[i] = out; \
485  } \
486  } \
487  fcache[0] = s0; \
488  fcache[1] = s1; \
489 }
490 
491 BIQUAD_LATT_FILTER(s16, int16_t, float, INT16_MIN, INT16_MAX, 1)
492 BIQUAD_LATT_FILTER(s32, int32_t, double, INT32_MIN, INT32_MAX, 1)
493 BIQUAD_LATT_FILTER(flt, float, float, -1.f, 1.f, 0)
494 BIQUAD_LATT_FILTER(dbl, double, double, -1., 1., 0)
495 
496 #define BIQUAD_SVF_FILTER(name, type, ftype, min, max, need_clipping) \
497 static void biquad_svf_## name (BiquadsContext *s, \
498  const void *input, void *output, int len, \
499  void *cache, int *clippings, int disabled) \
500 { \
501  const type *ibuf = input; \
502  type *obuf = output; \
503  ftype *fcache = cache; \
504  ftype *a = s->a_##ftype; \
505  ftype *b = s->b_##ftype; \
506  ftype a1 = a[1]; \
507  ftype a2 = a[2]; \
508  ftype b0 = b[0]; \
509  ftype b1 = b[1]; \
510  ftype b2 = b[2]; \
511  ftype s0 = fcache[0]; \
512  ftype s1 = fcache[1]; \
513  ftype wet = s->mix; \
514  ftype dry = 1. - wet; \
515  ftype in, out; \
516  ftype t0, t1; \
517  \
518  for (int i = 0; i < len; i++) { \
519  in = ibuf[i]; \
520  out = b2 * in + s0; \
521  t0 = b0 * in + a1 * s0 + s1; \
522  t1 = b1 * in + a2 * s0; \
523  s0 = t0; \
524  s1 = t1; \
525  \
526  out = out * wet + in * dry; \
527  if (disabled) { \
528  obuf[i] = in; \
529  } else if (need_clipping && out < min) { \
530  (*clippings)++; \
531  obuf[i] = min; \
532  } else if (need_clipping && out > max) { \
533  (*clippings)++; \
534  obuf[i] = max; \
535  } else { \
536  obuf[i] = out; \
537  } \
538  } \
539  fcache[0] = s0; \
540  fcache[1] = s1; \
541 }
542 
543 BIQUAD_SVF_FILTER(s16, int16_t, float, INT16_MIN, INT16_MAX, 1)
544 BIQUAD_SVF_FILTER(s32, int32_t, double, INT32_MIN, INT32_MAX, 1)
545 BIQUAD_SVF_FILTER(flt, float, float, -1.f, 1.f, 0)
546 BIQUAD_SVF_FILTER(dbl, double, double, -1., 1., 0)
547 
548 #define BIQUAD_ZDF_FILTER(name, type, ftype, min, max, need_clipping, two) \
549 static void biquad_zdf_## name (BiquadsContext *s, \
550  const void *input, void *output, int len, \
551  void *cache, int *clippings, int disabled) \
552 { \
553  const type *ibuf = input; \
554  type *obuf = output; \
555  ftype *fcache = cache; \
556  ftype *a = s->a_##ftype; \
557  ftype *b = s->b_##ftype; \
558  ftype m0 = b[0]; \
559  ftype m1 = b[1]; \
560  ftype m2 = b[2]; \
561  ftype a0 = a[0]; \
562  ftype a1 = a[1]; \
563  ftype a2 = a[2]; \
564  ftype b0 = fcache[0]; \
565  ftype b1 = fcache[1]; \
566  ftype wet = s->mix; \
567  ftype dry = 1. - wet; \
568  ftype out; \
569  \
570  for (int i = 0; i < len; i++) { \
571  const ftype in = ibuf[i]; \
572  const ftype v0 = in; \
573  const ftype v3 = v0 - b1; \
574  const ftype v1 = a0 * b0 + a1 * v3; \
575  const ftype v2 = b1 + a1 * b0 + a2 * v3; \
576  \
577  b0 = two * v1 - b0; \
578  b1 = two * v2 - b1; \
579  \
580  out = m0 * v0 + m1 * v1 + m2 * v2; \
581  out = out * wet + in * dry; \
582  if (disabled) { \
583  obuf[i] = in; \
584  } else if (need_clipping && out < min) { \
585  (*clippings)++; \
586  obuf[i] = min; \
587  } else if (need_clipping && out > max) { \
588  (*clippings)++; \
589  obuf[i] = max; \
590  } else { \
591  obuf[i] = out; \
592  } \
593  } \
594  fcache[0] = b0; \
595  fcache[1] = b1; \
596 }
597 
598 BIQUAD_ZDF_FILTER(s16, int16_t, float, INT16_MIN, INT16_MAX, 1, 2.f)
599 BIQUAD_ZDF_FILTER(s32, int32_t, double, INT32_MIN, INT32_MAX, 1, 2.0)
600 BIQUAD_ZDF_FILTER(flt, float, float, -1.f, 1.f, 0, 2.f)
601 BIQUAD_ZDF_FILTER(dbl, double, double, -1., 1., 0, 2.0)
602 
604 {
605  double k0, k1, v0, v1, v2;
606 
607  k1 = s->a_double[2];
608  k0 = s->a_double[1] / (1. + k1);
609  v2 = s->b_double[2];
610  v1 = s->b_double[1] - v2 * s->a_double[1];
611  v0 = s->b_double[0] - v1 * k0 - v2 * k1;
612 
613  s->a_double[1] = k0;
614  s->a_double[2] = k1;
615  s->b_double[0] = v0;
616  s->b_double[1] = v1;
617  s->b_double[2] = v2;
618 }
619 
621 {
622  double a[2];
623  double b[3];
624 
625  a[0] = -s->a_double[1];
626  a[1] = -s->a_double[2];
627  b[0] = s->b_double[1] - s->a_double[1] * s->b_double[0];
628  b[1] = s->b_double[2] - s->a_double[2] * s->b_double[0];
629  b[2] = s->b_double[0];
630 
631  s->a_double[1] = a[0];
632  s->a_double[2] = a[1];
633  s->b_double[0] = b[0];
634  s->b_double[1] = b[1];
635  s->b_double[2] = b[2];
636 }
637 
638 static double convert_width2qfactor(double width,
639  double frequency,
640  double gain,
641  double sample_rate,
642  int width_type)
643 {
644  double w0 = 2. * M_PI * frequency / sample_rate;
645  double A = ff_exp10(gain / 40.);
646  double ret;
647 
648  switch (width_type) {
649  case NONE:
650  case QFACTOR:
651  ret = width;
652  break;
653  case HERTZ:
654  ret = frequency / width;
655  break;
656  case KHERTZ:
657  ret = frequency / (width * 1000.);
658  break;
659  case OCTAVE:
660  ret = 1. / (2. * sinh(log(2.) / 2. * width * w0 / sin(w0)));
661  break;
662  case SLOPE:
663  ret = 1. / sqrt((A + 1. / A) * (1. / width - 1.) + 2.);
664  break;
665  default:
666  av_assert0(0);
667  break;
668  }
669 
670  return ret;
671 }
672 
674 {
675  double Q = convert_width2qfactor(s->width, s->frequency, s->gain, sample_rate, s->width_type);
676  double g, k, A;
677  double a[3];
678  double m[3];
679 
680  switch (s->filter_type) {
681  case biquad:
682  a[0] = s->oa[0];
683  a[1] = s->oa[1];
684  a[2] = s->oa[2];
685  m[0] = s->ob[0];
686  m[1] = s->ob[1];
687  m[2] = s->ob[2];
688  break;
689  case equalizer:
690  A = ff_exp10(s->gain / 40.);
691  g = tan(M_PI * s->frequency / sample_rate);
692  k = 1. / (Q * A);
693  a[0] = 1. / (1. + g * (g + k));
694  a[1] = g * a[0];
695  a[2] = g * a[1];
696  m[0] = 1.;
697  m[1] = k * (A * A - 1.);
698  m[2] = 0.;
699  break;
700  case bass:
701  case lowshelf:
702  A = ff_exp10(s->gain / 40.);
703  g = tan(M_PI * s->frequency / sample_rate) / sqrt(A);
704  k = 1. / Q;
705  a[0] = 1. / (1. + g * (g + k));
706  a[1] = g * a[0];
707  a[2] = g * a[1];
708  m[0] = 1.;
709  m[1] = k * (A - 1.);
710  m[2] = A * A - 1.;
711  break;
712  case tiltshelf:
713  A = ff_exp10(s->gain / 20.);
714  g = tan(M_PI * s->frequency / sample_rate) / sqrt(A);
715  k = 1. / Q;
716  a[0] = 1. / (1. + g * (g + k));
717  a[1] = g * a[0];
718  a[2] = g * a[1];
719  m[0] = 1./ A;
720  m[1] = k * (A - 1.) / A;
721  m[2] = (A * A - 1.) / A;
722  break;
723  case treble:
724  case highshelf:
725  A = ff_exp10(s->gain / 40.);
726  g = tan(M_PI * s->frequency / sample_rate) * sqrt(A);
727  k = 1. / Q;
728  a[0] = 1. / (1. + g * (g + k));
729  a[1] = g * a[0];
730  a[2] = g * a[1];
731  m[0] = A * A;
732  m[1] = k * (1. - A) * A;
733  m[2] = 1. - A * A;
734  break;
735  case bandpass:
736  g = tan(M_PI * s->frequency / sample_rate);
737  k = 1. / Q;
738  a[0] = 1. / (1. + g * (g + k));
739  a[1] = g * a[0];
740  a[2] = g * a[1];
741  m[0] = 0.;
742  m[1] = s->csg ? 1. : k;
743  m[2] = 0.;
744  break;
745  case bandreject:
746  g = tan(M_PI * s->frequency / sample_rate);
747  k = 1. / Q;
748  a[0] = 1. / (1. + g * (g + k));
749  a[1] = g * a[0];
750  a[2] = g * a[1];
751  m[0] = 1.;
752  m[1] = -k;
753  m[2] = 0.;
754  break;
755  case lowpass:
756  g = tan(M_PI * s->frequency / sample_rate);
757  k = 1. / Q;
758  a[0] = 1. / (1. + g * (g + k));
759  a[1] = g * a[0];
760  a[2] = g * a[1];
761  m[0] = 0.;
762  m[1] = 0.;
763  m[2] = 1.;
764  break;
765  case highpass:
766  g = tan(M_PI * s->frequency / sample_rate);
767  k = 1. / Q;
768  a[0] = 1. / (1. + g * (g + k));
769  a[1] = g * a[0];
770  a[2] = g * a[1];
771  m[0] = 1.;
772  m[1] = -k;
773  m[2] = -1.;
774  break;
775  case allpass:
776  g = tan(M_PI * s->frequency / sample_rate);
777  k = 1. / Q;
778  a[0] = 1. / (1. + g * (g + k));
779  a[1] = g * a[0];
780  a[2] = g * a[1];
781  m[0] = 1.;
782  m[1] = -2. * k;
783  m[2] = 0.;
784  break;
785  default:
786  av_assert0(0);
787  }
788 
789  s->a_double[0] = a[0];
790  s->a_double[1] = a[1];
791  s->a_double[2] = a[2];
792  s->b_double[0] = m[0];
793  s->b_double[1] = m[1];
794  s->b_double[2] = m[2];
795 }
796 
797 static int config_filter(AVFilterLink *outlink, int reset)
798 {
799  AVFilterContext *ctx = outlink->src;
800  BiquadsContext *s = ctx->priv;
801  AVFilterLink *inlink = ctx->inputs[0];
802  double gain = s->gain * ((s->filter_type == tiltshelf) + 1.);
803  double A = ff_exp10(gain / 40);
804  double w0 = 2 * M_PI * s->frequency / inlink->sample_rate;
805  double K = tan(w0 / 2.);
806  double alpha, beta;
807 
808  s->bypass = (((w0 > M_PI || w0 <= 0.) && reset) || (s->width <= 0.)) && (s->filter_type != biquad);
809  if (s->bypass) {
810  av_log(ctx, AV_LOG_WARNING, "Invalid frequency and/or width!\n");
811  return 0;
812  }
813 
814  if ((w0 > M_PI || w0 <= 0.) && (s->filter_type != biquad))
815  return AVERROR(EINVAL);
816 
817  switch (s->width_type) {
818  case NONE:
819  alpha = 0.0;
820  break;
821  case HERTZ:
822  alpha = sin(w0) / (2 * s->frequency / s->width);
823  break;
824  case KHERTZ:
825  alpha = sin(w0) / (2 * s->frequency / (s->width * 1000));
826  break;
827  case OCTAVE:
828  alpha = sin(w0) * sinh(log(2.) / 2 * s->width * w0 / sin(w0));
829  break;
830  case QFACTOR:
831  alpha = sin(w0) / (2 * s->width);
832  break;
833  case SLOPE:
834  alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->width - 1) + 2);
835  break;
836  default:
837  av_assert0(0);
838  }
839 
840  beta = 2 * sqrt(A);
841 
842  switch (s->filter_type) {
843  case biquad:
844  s->a_double[0] = s->oa[0];
845  s->a_double[1] = s->oa[1];
846  s->a_double[2] = s->oa[2];
847  s->b_double[0] = s->ob[0];
848  s->b_double[1] = s->ob[1];
849  s->b_double[2] = s->ob[2];
850  break;
851  case equalizer:
852  s->a_double[0] = 1 + alpha / A;
853  s->a_double[1] = -2 * cos(w0);
854  s->a_double[2] = 1 - alpha / A;
855  s->b_double[0] = 1 + alpha * A;
856  s->b_double[1] = -2 * cos(w0);
857  s->b_double[2] = 1 - alpha * A;
858  break;
859  case bass:
860  beta = sqrt((A * A + 1) - (A - 1) * (A - 1));
861  case tiltshelf:
862  case lowshelf:
863  if (s->poles == 1) {
864  double A = ff_exp10(gain / 20);
865  double ro = -sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
866  double n = (A + 1) / (A - 1);
867  double alpha1 = A == 1. ? 0. : n - FFSIGN(n) * sqrt(n * n - 1);
868  double beta0 = ((1 + A) + (1 - A) * alpha1) * 0.5;
869  double beta1 = ((1 - A) + (1 + A) * alpha1) * 0.5;
870 
871  s->a_double[0] = 1 + ro * alpha1;
872  s->a_double[1] = -ro - alpha1;
873  s->a_double[2] = 0;
874  s->b_double[0] = beta0 + ro * beta1;
875  s->b_double[1] = -beta1 - ro * beta0;
876  s->b_double[2] = 0;
877  } else {
878  s->a_double[0] = (A + 1) + (A - 1) * cos(w0) + beta * alpha;
879  s->a_double[1] = -2 * ((A - 1) + (A + 1) * cos(w0));
880  s->a_double[2] = (A + 1) + (A - 1) * cos(w0) - beta * alpha;
881  s->b_double[0] = A * ((A + 1) - (A - 1) * cos(w0) + beta * alpha);
882  s->b_double[1] = 2 * A * ((A - 1) - (A + 1) * cos(w0));
883  s->b_double[2] = A * ((A + 1) - (A - 1) * cos(w0) - beta * alpha);
884  }
885  break;
886  case treble:
887  beta = sqrt((A * A + 1) - (A - 1) * (A - 1));
888  case highshelf:
889  if (s->poles == 1) {
890  double A = ff_exp10(gain / 20);
891  double ro = sin(w0 / 2. - M_PI_4) / sin(w0 / 2. + M_PI_4);
892  double n = (A + 1) / (A - 1);
893  double alpha1 = A == 1. ? 0. : n - FFSIGN(n) * sqrt(n * n - 1);
894  double beta0 = ((1 + A) + (1 - A) * alpha1) * 0.5;
895  double beta1 = ((1 - A) + (1 + A) * alpha1) * 0.5;
896 
897  s->a_double[0] = 1 + ro * alpha1;
898  s->a_double[1] = ro + alpha1;
899  s->a_double[2] = 0;
900  s->b_double[0] = beta0 + ro * beta1;
901  s->b_double[1] = beta1 + ro * beta0;
902  s->b_double[2] = 0;
903  } else {
904  s->a_double[0] = (A + 1) - (A - 1) * cos(w0) + beta * alpha;
905  s->a_double[1] = 2 * ((A - 1) - (A + 1) * cos(w0));
906  s->a_double[2] = (A + 1) - (A - 1) * cos(w0) - beta * alpha;
907  s->b_double[0] = A * ((A + 1) + (A - 1) * cos(w0) + beta * alpha);
908  s->b_double[1] =-2 * A * ((A - 1) + (A + 1) * cos(w0));
909  s->b_double[2] = A * ((A + 1) + (A - 1) * cos(w0) - beta * alpha);
910  }
911  break;
912  case bandpass:
913  if (s->csg) {
914  s->a_double[0] = 1 + alpha;
915  s->a_double[1] = -2 * cos(w0);
916  s->a_double[2] = 1 - alpha;
917  s->b_double[0] = sin(w0) / 2;
918  s->b_double[1] = 0;
919  s->b_double[2] = -sin(w0) / 2;
920  } else {
921  s->a_double[0] = 1 + alpha;
922  s->a_double[1] = -2 * cos(w0);
923  s->a_double[2] = 1 - alpha;
924  s->b_double[0] = alpha;
925  s->b_double[1] = 0;
926  s->b_double[2] = -alpha;
927  }
928  break;
929  case bandreject:
930  s->a_double[0] = 1 + alpha;
931  s->a_double[1] = -2 * cos(w0);
932  s->a_double[2] = 1 - alpha;
933  s->b_double[0] = 1;
934  s->b_double[1] = -2 * cos(w0);
935  s->b_double[2] = 1;
936  break;
937  case lowpass:
938  if (s->poles == 1) {
939  s->a_double[0] = 1;
940  s->a_double[1] = -exp(-w0);
941  s->a_double[2] = 0;
942  s->b_double[0] = 1 + s->a_double[1];
943  s->b_double[1] = 0;
944  s->b_double[2] = 0;
945  } else {
946  s->a_double[0] = 1 + alpha;
947  s->a_double[1] = -2 * cos(w0);
948  s->a_double[2] = 1 - alpha;
949  s->b_double[0] = (1 - cos(w0)) / 2;
950  s->b_double[1] = 1 - cos(w0);
951  s->b_double[2] = (1 - cos(w0)) / 2;
952  }
953  break;
954  case highpass:
955  if (s->poles == 1) {
956  s->a_double[0] = 1;
957  s->a_double[1] = -exp(-w0);
958  s->a_double[2] = 0;
959  s->b_double[0] = (1 - s->a_double[1]) / 2;
960  s->b_double[1] = -s->b_double[0];
961  s->b_double[2] = 0;
962  } else {
963  s->a_double[0] = 1 + alpha;
964  s->a_double[1] = -2 * cos(w0);
965  s->a_double[2] = 1 - alpha;
966  s->b_double[0] = (1 + cos(w0)) / 2;
967  s->b_double[1] = -(1 + cos(w0));
968  s->b_double[2] = (1 + cos(w0)) / 2;
969  }
970  break;
971  case allpass:
972  switch (s->order) {
973  case 1:
974  s->a_double[0] = 1.;
975  s->a_double[1] = -(1. - K) / (1. + K);
976  s->a_double[2] = 0.;
977  s->b_double[0] = s->a_double[1];
978  s->b_double[1] = s->a_double[0];
979  s->b_double[2] = 0.;
980  break;
981  case 2:
982  s->a_double[0] = 1 + alpha;
983  s->a_double[1] = -2 * cos(w0);
984  s->a_double[2] = 1 - alpha;
985  s->b_double[0] = 1 - alpha;
986  s->b_double[1] = -2 * cos(w0);
987  s->b_double[2] = 1 + alpha;
988  break;
989  }
990  break;
991  default:
992  av_assert0(0);
993  }
994 
995  av_log(ctx, AV_LOG_VERBOSE, "a=%f %f %f:b=%f %f %f\n",
996  s->a_double[0], s->a_double[1], s->a_double[2],
997  s->b_double[0], s->b_double[1], s->b_double[2]);
998 
999  s->a_double[1] /= s->a_double[0];
1000  s->a_double[2] /= s->a_double[0];
1001  s->b_double[0] /= s->a_double[0];
1002  s->b_double[1] /= s->a_double[0];
1003  s->b_double[2] /= s->a_double[0];
1004  s->a_double[0] /= s->a_double[0];
1005 
1006  if (s->normalize && fabs(s->b_double[0] + s->b_double[1] + s->b_double[2]) > 1e-6) {
1007  double factor = (s->a_double[0] + s->a_double[1] + s->a_double[2]) /
1008  (s->b_double[0] + s->b_double[1] + s->b_double[2]);
1009 
1010  s->b_double[0] *= factor;
1011  s->b_double[1] *= factor;
1012  s->b_double[2] *= factor;
1013  }
1014 
1015  switch (s->filter_type) {
1016  case tiltshelf:
1017  s->b_double[0] /= A;
1018  s->b_double[1] /= A;
1019  s->b_double[2] /= A;
1020  break;
1021  }
1022 
1023  if (!s->cache[0])
1024  s->cache[0] = ff_get_audio_buffer(outlink, 4 * sizeof(double));
1025  if (!s->clip)
1026  s->clip = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->clip));
1027  if (!s->cache[0] || !s->clip)
1028  return AVERROR(ENOMEM);
1029  if (reset) {
1030  av_samples_set_silence(s->cache[0]->extended_data, 0, s->cache[0]->nb_samples,
1031  s->cache[0]->ch_layout.nb_channels, s->cache[0]->format);
1032  }
1033 
1034  if (reset && s->block_samples > 0) {
1035  if (!s->cache[1])
1036  s->cache[1] = ff_get_audio_buffer(outlink, 4 * sizeof(double));
1037  if (!s->cache[1])
1038  return AVERROR(ENOMEM);
1039  av_samples_set_silence(s->cache[1]->extended_data, 0, s->cache[1]->nb_samples,
1040  s->cache[1]->ch_layout.nb_channels, s->cache[1]->format);
1041  for (int i = 0; i < 3; i++) {
1042  if (!s->block[i])
1043  s->block[i] = ff_get_audio_buffer(outlink, s->block_samples * 2);
1044  if (!s->block[i])
1045  return AVERROR(ENOMEM);
1046  av_samples_set_silence(s->block[i]->extended_data, 0, s->block_samples * 2,
1047  s->block[i]->ch_layout.nb_channels, s->block[i]->format);
1048  }
1049  }
1050 
1051  switch (s->transform_type) {
1052  case DI:
1053  switch (inlink->format) {
1054  case AV_SAMPLE_FMT_S16P:
1055  s->filter = biquad_s16;
1056  break;
1057  case AV_SAMPLE_FMT_S32P:
1058  s->filter = biquad_s32;
1059  break;
1060  case AV_SAMPLE_FMT_FLTP:
1061  s->filter = biquad_flt;
1062  break;
1063  case AV_SAMPLE_FMT_DBLP:
1064  s->filter = biquad_dbl;
1065  break;
1066  default: av_assert0(0);
1067  }
1068  break;
1069  case DII:
1070  switch (inlink->format) {
1071  case AV_SAMPLE_FMT_S16P:
1072  s->filter = biquad_dii_s16;
1073  break;
1074  case AV_SAMPLE_FMT_S32P:
1075  s->filter = biquad_dii_s32;
1076  break;
1077  case AV_SAMPLE_FMT_FLTP:
1078  s->filter = biquad_dii_flt;
1079  break;
1080  case AV_SAMPLE_FMT_DBLP:
1081  s->filter = biquad_dii_dbl;
1082  break;
1083  default: av_assert0(0);
1084  }
1085  break;
1086  case TDI:
1087  switch (inlink->format) {
1088  case AV_SAMPLE_FMT_S16P:
1089  s->filter = biquad_tdi_s16;
1090  break;
1091  case AV_SAMPLE_FMT_S32P:
1092  s->filter = biquad_tdi_s32;
1093  break;
1094  case AV_SAMPLE_FMT_FLTP:
1095  s->filter = biquad_tdi_flt;
1096  break;
1097  case AV_SAMPLE_FMT_DBLP:
1098  s->filter = biquad_tdi_dbl;
1099  break;
1100  default: av_assert0(0);
1101  }
1102  break;
1103  case TDII:
1104  switch (inlink->format) {
1105  case AV_SAMPLE_FMT_S16P:
1106  s->filter = biquad_tdii_s16;
1107  break;
1108  case AV_SAMPLE_FMT_S32P:
1109  s->filter = biquad_tdii_s32;
1110  break;
1111  case AV_SAMPLE_FMT_FLTP:
1112  s->filter = biquad_tdii_flt;
1113  break;
1114  case AV_SAMPLE_FMT_DBLP:
1115  s->filter = biquad_tdii_dbl;
1116  break;
1117  default: av_assert0(0);
1118  }
1119  break;
1120  case LATT:
1121  switch (inlink->format) {
1122  case AV_SAMPLE_FMT_S16P:
1123  s->filter = biquad_latt_s16;
1124  break;
1125  case AV_SAMPLE_FMT_S32P:
1126  s->filter = biquad_latt_s32;
1127  break;
1128  case AV_SAMPLE_FMT_FLTP:
1129  s->filter = biquad_latt_flt;
1130  break;
1131  case AV_SAMPLE_FMT_DBLP:
1132  s->filter = biquad_latt_dbl;
1133  break;
1134  default: av_assert0(0);
1135  }
1136  break;
1137  case SVF:
1138  switch (inlink->format) {
1139  case AV_SAMPLE_FMT_S16P:
1140  s->filter = biquad_svf_s16;
1141  break;
1142  case AV_SAMPLE_FMT_S32P:
1143  s->filter = biquad_svf_s32;
1144  break;
1145  case AV_SAMPLE_FMT_FLTP:
1146  s->filter = biquad_svf_flt;
1147  break;
1148  case AV_SAMPLE_FMT_DBLP:
1149  s->filter = biquad_svf_dbl;
1150  break;
1151  default: av_assert0(0);
1152  }
1153  break;
1154  case ZDF:
1155  switch (inlink->format) {
1156  case AV_SAMPLE_FMT_S16P:
1157  s->filter = biquad_zdf_s16;
1158  break;
1159  case AV_SAMPLE_FMT_S32P:
1160  s->filter = biquad_zdf_s32;
1161  break;
1162  case AV_SAMPLE_FMT_FLTP:
1163  s->filter = biquad_zdf_flt;
1164  break;
1165  case AV_SAMPLE_FMT_DBLP:
1166  s->filter = biquad_zdf_dbl;
1167  break;
1168  default: av_assert0(0);
1169  }
1170  break;
1171  default:
1172  av_assert0(0);
1173  }
1174 
1175  s->block_align = av_get_bytes_per_sample(inlink->format);
1176 
1177  if (s->transform_type == LATT)
1179  else if (s->transform_type == SVF)
1180  convert_dir2svf(s);
1181  else if (s->transform_type == ZDF)
1182  convert_dir2zdf(s, inlink->sample_rate);
1183 
1184  s->a_float[0] = s->a_double[0];
1185  s->a_float[1] = s->a_double[1];
1186  s->a_float[2] = s->a_double[2];
1187  s->b_float[0] = s->b_double[0];
1188  s->b_float[1] = s->b_double[1];
1189  s->b_float[2] = s->b_double[2];
1190 
1191  return 0;
1192 }
1193 
1194 static int config_output(AVFilterLink *outlink)
1195 {
1196  return config_filter(outlink, 1);
1197 }
1198 
1199 typedef struct ThreadData {
1200  AVFrame *in, *out;
1201  int eof;
1202 } ThreadData;
1203 
1204 static void reverse_samples(AVFrame *out, AVFrame *in, int p,
1205  int oo, int io, int nb_samples)
1206 {
1207  switch (out->format) {
1208  case AV_SAMPLE_FMT_S16P: {
1209  const int16_t *src = ((const int16_t *)in->extended_data[p]) + io;
1210  int16_t *dst = ((int16_t *)out->extended_data[p]) + oo;
1211  for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1212  dst[i] = src[j];
1213  }
1214  break;
1215  case AV_SAMPLE_FMT_S32P: {
1216  const int32_t *src = ((const int32_t *)in->extended_data[p]) + io;
1217  int32_t *dst = ((int32_t *)out->extended_data[p]) + oo;
1218  for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1219  dst[i] = src[j];
1220  }
1221  break;
1222  case AV_SAMPLE_FMT_FLTP: {
1223  const float *src = ((const float *)in->extended_data[p]) + io;
1224  float *dst = ((float *)out->extended_data[p]) + oo;
1225  for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1226  dst[i] = src[j];
1227  }
1228  break;
1229  case AV_SAMPLE_FMT_DBLP: {
1230  const double *src = ((const double *)in->extended_data[p]) + io;
1231  double *dst = ((double *)out->extended_data[p]) + oo;
1232  for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--)
1233  dst[i] = src[j];
1234  }
1235  break;
1236  }
1237 }
1238 
1239 static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1240 {
1241  AVFilterLink *inlink = ctx->inputs[0];
1242  ThreadData *td = arg;
1243  AVFrame *buf = td->in;
1244  AVFrame *out_buf = td->out;
1245  BiquadsContext *s = ctx->priv;
1246  const int start = (buf->ch_layout.nb_channels * jobnr) / nb_jobs;
1247  const int end = (buf->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
1248  int ch;
1249 
1250  for (ch = start; ch < end; ch++) {
1252 
1253  if (av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0) {
1254  if (buf != out_buf)
1255  memcpy(out_buf->extended_data[ch], buf->extended_data[ch],
1256  buf->nb_samples * s->block_align);
1257  continue;
1258  }
1259 
1260  if (!s->block_samples) {
1261  s->filter(s, buf->extended_data[ch], out_buf->extended_data[ch], buf->nb_samples,
1262  s->cache[0]->extended_data[ch], s->clip+ch, ctx->is_disabled);
1263  } else if (td->eof) {
1264  memcpy(out_buf->extended_data[ch], s->block[1]->extended_data[ch] + s->block_align * s->block_samples,
1265  s->nb_samples * s->block_align);
1266  } else {
1267  memcpy(s->block[0]->extended_data[ch] + s->block_align * s->block_samples, buf->extended_data[ch],
1268  buf->nb_samples * s->block_align);
1269  memset(s->block[0]->extended_data[ch] + s->block_align * (s->block_samples + buf->nb_samples),
1270  0, (s->block_samples - buf->nb_samples) * s->block_align);
1271  s->filter(s, s->block[0]->extended_data[ch], s->block[1]->extended_data[ch], s->block_samples,
1272  s->cache[0]->extended_data[ch], s->clip+ch, ctx->is_disabled);
1273  av_samples_copy(s->cache[1]->extended_data, s->cache[0]->extended_data, 0, 0,
1274  s->cache[0]->nb_samples, s->cache[0]->ch_layout.nb_channels,
1275  s->cache[0]->format);
1276  s->filter(s, s->block[0]->extended_data[ch] + s->block_samples * s->block_align,
1277  s->block[1]->extended_data[ch] + s->block_samples * s->block_align,
1278  s->block_samples, s->cache[1]->extended_data[ch], s->clip+ch,
1279  ctx->is_disabled);
1280  reverse_samples(s->block[2], s->block[1], ch, 0, 0, 2 * s->block_samples);
1281  av_samples_set_silence(s->cache[1]->extended_data, 0, s->cache[1]->nb_samples,
1282  s->cache[1]->ch_layout.nb_channels, s->cache[1]->format);
1283  s->filter(s, s->block[2]->extended_data[ch], s->block[2]->extended_data[ch], 2 * s->block_samples,
1284  s->cache[1]->extended_data[ch], s->clip+ch, ctx->is_disabled);
1285  reverse_samples(s->block[1], s->block[2], ch, 0, 0, 2 * s->block_samples);
1286  memcpy(out_buf->extended_data[ch], s->block[1]->extended_data[ch],
1287  s->block_samples * s->block_align);
1288  memmove(s->block[0]->extended_data[ch], s->block[0]->extended_data[ch] + s->block_align * s->block_samples,
1289  s->block_samples * s->block_align);
1290  }
1291  }
1292 
1293  return 0;
1294 }
1295 
1296 static int filter_frame(AVFilterLink *inlink, AVFrame *buf, int eof)
1297 {
1298  AVFilterContext *ctx = inlink->dst;
1299  BiquadsContext *s = ctx->priv;
1300  AVFilterLink *outlink = ctx->outputs[0];
1301  AVFrame *out_buf;
1302  ThreadData td;
1303  int ch, ret, drop = 0;
1304 
1305  if (s->bypass)
1306  return ff_filter_frame(outlink, buf);
1307 
1308  ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout);
1309  if (ret < 0) {
1310  av_frame_free(&buf);
1311  return ret;
1312  }
1313  if (strcmp(s->ch_layout_str, "all"))
1314  av_channel_layout_from_string(&s->ch_layout,
1315  s->ch_layout_str);
1316 
1317  if (av_frame_is_writable(buf) && s->block_samples == 0) {
1318  out_buf = buf;
1319  } else {
1320  out_buf = ff_get_audio_buffer(outlink, s->block_samples > 0 ? s->block_samples : buf->nb_samples);
1321  if (!out_buf) {
1322  av_frame_free(&buf);
1323  return AVERROR(ENOMEM);
1324  }
1325  av_frame_copy_props(out_buf, buf);
1326  }
1327 
1328  if (s->block_samples > 0 && s->pts == AV_NOPTS_VALUE)
1329  drop = 1;
1330  td.in = buf;
1331  td.out = out_buf;
1332  td.eof = eof;
1335 
1336  for (ch = 0; ch < outlink->ch_layout.nb_channels; ch++) {
1337  if (s->clip[ch] > 0)
1338  av_log(ctx, AV_LOG_WARNING, "Channel %d clipping %d times. Please reduce gain.\n",
1339  ch, s->clip[ch]);
1340  s->clip[ch] = 0;
1341  }
1342 
1343  if (s->block_samples > 0) {
1344  int nb_samples = buf->nb_samples;
1345  int64_t pts = buf->pts;
1346 
1347  out_buf->pts = s->pts;
1348  out_buf->nb_samples = s->nb_samples;
1349  s->pts = pts;
1350  s->nb_samples = nb_samples;
1351  }
1352 
1353  if (buf != out_buf)
1354  av_frame_free(&buf);
1355 
1356  if (!drop)
1357  return ff_filter_frame(outlink, out_buf);
1358  else {
1359  av_frame_free(&out_buf);
1360  ff_filter_set_ready(ctx, 10);
1361  return 0;
1362  }
1363 }
1364 
1366 {
1367  AVFilterLink *inlink = ctx->inputs[0];
1368  AVFilterLink *outlink = ctx->outputs[0];
1369  BiquadsContext *s = ctx->priv;
1370  AVFrame *in = NULL;
1371  int64_t pts;
1372  int status;
1373  int ret;
1374 
1376 
1377  if (s->block_samples > 0) {
1378  ret = ff_inlink_consume_samples(inlink, s->block_samples, s->block_samples, &in);
1379  } else {
1381  }
1382  if (ret < 0)
1383  return ret;
1384  if (ret > 0)
1385  return filter_frame(inlink, in, 0);
1386 
1387  if (s->block_samples > 0 && ff_inlink_queued_samples(inlink) >= s->block_samples) {
1388  ff_filter_set_ready(ctx, 10);
1389  return 0;
1390  }
1391 
1393  if (s->block_samples > 0) {
1394  AVFrame *in = ff_get_audio_buffer(outlink, s->block_samples);
1395  if (!in)
1396  return AVERROR(ENOMEM);
1397 
1398  ret = filter_frame(inlink, in, 1);
1399  }
1400 
1401  ff_outlink_set_status(outlink, status, pts);
1402 
1403  return ret;
1404  }
1405 
1406  FF_FILTER_FORWARD_WANTED(outlink, inlink);
1407 
1408  return FFERROR_NOT_READY;
1409 }
1410 
1411 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
1412  char *res, int res_len, int flags)
1413 {
1414  AVFilterLink *outlink = ctx->outputs[0];
1415  int ret;
1416 
1417  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
1418  if (ret < 0)
1419  return ret;
1420 
1421  return config_filter(outlink, 0);
1422 }
1423 
1425 {
1426  BiquadsContext *s = ctx->priv;
1427 
1428  for (int i = 0; i < 3; i++)
1429  av_frame_free(&s->block[i]);
1430  av_frame_free(&s->cache[0]);
1431  av_frame_free(&s->cache[1]);
1432  av_freep(&s->clip);
1433  av_channel_layout_uninit(&s->ch_layout);
1434 }
1435 
1436 static const AVFilterPad outputs[] = {
1437  {
1438  .name = "default",
1439  .type = AVMEDIA_TYPE_AUDIO,
1440  .config_props = config_output,
1441  },
1442 };
1443 
1444 #define OFFSET(x) offsetof(BiquadsContext, x)
1445 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
1446 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1447 
1448 #define DEFINE_BIQUAD_FILTER_2(name_, description_, priv_class_) \
1449 static av_cold int name_##_init(AVFilterContext *ctx) \
1450 { \
1451  BiquadsContext *s = ctx->priv; \
1452  s->filter_type = name_; \
1453  s->pts = AV_NOPTS_VALUE; \
1454  return 0; \
1455 } \
1456  \
1457 const AVFilter ff_af_##name_ = { \
1458  .name = #name_, \
1459  .description = NULL_IF_CONFIG_SMALL(description_), \
1460  .priv_class = &priv_class_##_class, \
1461  .priv_size = sizeof(BiquadsContext), \
1462  .init = name_##_init, \
1463  .activate = activate, \
1464  .uninit = uninit, \
1465  FILTER_INPUTS(ff_audio_default_filterpad), \
1466  FILTER_OUTPUTS(outputs), \
1467  FILTER_QUERY_FUNC(query_formats), \
1468  .process_command = process_command, \
1469  .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, \
1470 }
1471 
1472 #define DEFINE_BIQUAD_FILTER(name, description) \
1473  AVFILTER_DEFINE_CLASS(name); \
1474  DEFINE_BIQUAD_FILTER_2(name, description, name)
1475 
1476 #define WIDTH_OPTION(x) \
1477  {"width", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 99999, FLAGS}, \
1478  {"w", "set width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 99999, FLAGS}
1479 
1480 #define WIDTH_TYPE_OPTION(x) \
1481  {"width_type", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=x}, HERTZ, NB_WTYPE-1, FLAGS, .unit = "width_type"}, \
1482  {"t", "set filter-width type", OFFSET(width_type), AV_OPT_TYPE_INT, {.i64=x}, HERTZ, NB_WTYPE-1, FLAGS, .unit = "width_type"}, \
1483  {"h", "Hz", 0, AV_OPT_TYPE_CONST, {.i64=HERTZ}, 0, 0, FLAGS, .unit = "width_type"}, \
1484  {"q", "Q-Factor", 0, AV_OPT_TYPE_CONST, {.i64=QFACTOR}, 0, 0, FLAGS, .unit = "width_type"}, \
1485  {"o", "octave", 0, AV_OPT_TYPE_CONST, {.i64=OCTAVE}, 0, 0, FLAGS, .unit = "width_type"}, \
1486  {"s", "slope", 0, AV_OPT_TYPE_CONST, {.i64=SLOPE}, 0, 0, FLAGS, .unit = "width_type"}, \
1487  {"k", "kHz", 0, AV_OPT_TYPE_CONST, {.i64=KHERTZ}, 0, 0, FLAGS, .unit = "width_type"}
1488 
1489 #define MIX_CHANNELS_NORMALIZE_OPTION(x, y, z) \
1490  {"mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 1, FLAGS}, \
1491  {"m", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=x}, 0, 1, FLAGS}, \
1492  {"channels", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str=y}, 0, 0, FLAGS}, \
1493  {"c", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str=y}, 0, 0, FLAGS}, \
1494  {"normalize", "normalize coefficients", OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=z}, 0, 1, FLAGS}, \
1495  {"n", "normalize coefficients", OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=z}, 0, 1, FLAGS}
1496 
1497 #define TRANSFORM_OPTION(x) \
1498  {"transform", "set transform type", OFFSET(transform_type), AV_OPT_TYPE_INT, {.i64=x}, 0, NB_TTYPE-1, AF, .unit = "transform_type"}, \
1499  {"a", "set transform type", OFFSET(transform_type), AV_OPT_TYPE_INT, {.i64=x}, 0, NB_TTYPE-1, AF, .unit = "transform_type"}, \
1500  {"di", "direct form I", 0, AV_OPT_TYPE_CONST, {.i64=DI}, 0, 0, AF, .unit = "transform_type"}, \
1501  {"dii", "direct form II", 0, AV_OPT_TYPE_CONST, {.i64=DII}, 0, 0, AF, .unit = "transform_type"}, \
1502  {"tdi", "transposed direct form I", 0, AV_OPT_TYPE_CONST, {.i64=TDI}, 0, 0, AF, .unit = "transform_type"}, \
1503  {"tdii", "transposed direct form II", 0, AV_OPT_TYPE_CONST, {.i64=TDII}, 0, 0, AF, .unit = "transform_type"}, \
1504  {"latt", "lattice-ladder form", 0, AV_OPT_TYPE_CONST, {.i64=LATT}, 0, 0, AF, .unit = "transform_type"}, \
1505  {"svf", "state variable filter form", 0, AV_OPT_TYPE_CONST, {.i64=SVF}, 0, 0, AF, .unit = "transform_type"}, \
1506  {"zdf", "zero-delay filter form", 0, AV_OPT_TYPE_CONST, {.i64=ZDF}, 0, 0, AF, .unit = "transform_type"}
1507 
1508 #define PRECISION_OPTION(x) \
1509  {"precision", "set filtering precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=x}, -1, 3, AF, .unit = "precision"}, \
1510  {"r", "set filtering precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=x}, -1, 3, AF, .unit = "precision"}, \
1511  {"auto", "automatic", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, AF, .unit = "precision"}, \
1512  {"s16", "signed 16-bit", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "precision"}, \
1513  {"s32", "signed 32-bit", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "precision"}, \
1514  {"f32", "floating-point single", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "precision"}, \
1515  {"f64", "floating-point double", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, .unit = "precision"}
1516 
1517 #define BLOCKSIZE_OPTION(x) \
1518  {"blocksize", "set the block size", OFFSET(block_samples), AV_OPT_TYPE_INT, {.i64=x}, 0, 32768, AF}, \
1519  {"b", "set the block size", OFFSET(block_samples), AV_OPT_TYPE_INT, {.i64=x}, 0, 32768, AF}
1520 
1521 #if CONFIG_EQUALIZER_FILTER
1522 static const AVOption equalizer_options[] = {
1523  {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
1524  {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 999999, FLAGS},
1526  WIDTH_OPTION(1.0),
1527  {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1528  {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1529  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1531  PRECISION_OPTION(-1),
1532  BLOCKSIZE_OPTION(0),
1533  {NULL}
1534 };
1535 
1536 DEFINE_BIQUAD_FILTER(equalizer, "Apply two-pole peaking equalization (EQ) filter.");
1537 #endif /* CONFIG_EQUALIZER_FILTER */
1538 #if CONFIG_BASS_FILTER || CONFIG_LOWSHELF_FILTER
1539 static const AVOption bass_lowshelf_options[] = {
1540  {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
1541  {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 0, 999999, FLAGS},
1543  WIDTH_OPTION(0.5),
1544  {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1545  {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1546  {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1547  {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1548  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1550  PRECISION_OPTION(-1),
1551  BLOCKSIZE_OPTION(0),
1552  {NULL}
1553 };
1554 
1555 AVFILTER_DEFINE_CLASS_EXT(bass_lowshelf, "bass/lowshelf", bass_lowshelf_options);
1556 #if CONFIG_BASS_FILTER
1557 DEFINE_BIQUAD_FILTER_2(bass, "Boost or cut lower frequencies.", bass_lowshelf);
1558 #endif /* CONFIG_BASS_FILTER */
1559 
1560 #if CONFIG_LOWSHELF_FILTER
1561 DEFINE_BIQUAD_FILTER_2(lowshelf, "Apply a low shelf filter.", bass_lowshelf);
1562 #endif /* CONFIG_LOWSHELF_FILTER */
1563 #endif /* CONFIG_BASS_FILTER || CONFIG LOWSHELF_FILTER */
1564 #if CONFIG_TREBLE_FILTER || CONFIG_HIGHSHELF_FILTER || CONFIG_TILTSHELF_FILTER
1565 static const AVOption treble_highshelf_options[] = {
1566  {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1567  {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1569  WIDTH_OPTION(0.5),
1570  {"gain", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1571  {"g", "set gain", OFFSET(gain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, FLAGS},
1572  {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1573  {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1574  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1576  PRECISION_OPTION(-1),
1577  BLOCKSIZE_OPTION(0),
1578  {NULL}
1579 };
1580 
1581 AVFILTER_DEFINE_CLASS_EXT(treble_highshelf, "treble/high/tiltshelf",
1582  treble_highshelf_options);
1583 
1584 #if CONFIG_TREBLE_FILTER
1585 DEFINE_BIQUAD_FILTER_2(treble, "Boost or cut upper frequencies.", treble_highshelf);
1586 #endif /* CONFIG_TREBLE_FILTER */
1587 
1588 #if CONFIG_HIGHSHELF_FILTER
1589 DEFINE_BIQUAD_FILTER_2(highshelf, "Apply a high shelf filter.", treble_highshelf);
1590 #endif /* CONFIG_HIGHSHELF_FILTER */
1591 
1592 #if CONFIG_TILTSHELF_FILTER
1593 DEFINE_BIQUAD_FILTER_2(tiltshelf, "Apply a tilt shelf filter.", treble_highshelf);
1594 #endif
1595 #endif /* CONFIG_TREBLE_FILTER || CONFIG_HIGHSHELF_FILTER || CONFIG_TILTSHELF_FILTER */
1596 
1597 #if CONFIG_BANDPASS_FILTER
1598 static const AVOption bandpass_options[] = {
1599  {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1600  {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1602  WIDTH_OPTION(0.5),
1603  {"csg", "use constant skirt gain", OFFSET(csg), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
1604  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1606  PRECISION_OPTION(-1),
1607  BLOCKSIZE_OPTION(0),
1608  {NULL}
1609 };
1610 
1611 DEFINE_BIQUAD_FILTER(bandpass, "Apply a two-pole Butterworth band-pass filter.");
1612 #endif /* CONFIG_BANDPASS_FILTER */
1613 #if CONFIG_BANDREJECT_FILTER
1614 static const AVOption bandreject_options[] = {
1615  {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1616  {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1618  WIDTH_OPTION(0.5),
1619  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1621  PRECISION_OPTION(-1),
1622  BLOCKSIZE_OPTION(0),
1623  {NULL}
1624 };
1625 
1626 DEFINE_BIQUAD_FILTER(bandreject, "Apply a two-pole Butterworth band-reject filter.");
1627 #endif /* CONFIG_BANDREJECT_FILTER */
1628 #if CONFIG_LOWPASS_FILTER
1629 static const AVOption lowpass_options[] = {
1630  {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
1631  {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=500}, 0, 999999, FLAGS},
1633  WIDTH_OPTION(0.707),
1634  {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1635  {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1636  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1638  PRECISION_OPTION(-1),
1639  BLOCKSIZE_OPTION(0),
1640  {NULL}
1641 };
1642 
1643 DEFINE_BIQUAD_FILTER(lowpass, "Apply a low-pass filter with 3dB point frequency.");
1644 #endif /* CONFIG_LOWPASS_FILTER */
1645 #if CONFIG_HIGHPASS_FILTER
1646 static const AVOption highpass_options[] = {
1647  {"frequency", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1648  {"f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1650  WIDTH_OPTION(0.707),
1651  {"poles", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1652  {"p", "set number of poles", OFFSET(poles), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AF},
1653  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1655  PRECISION_OPTION(-1),
1656  BLOCKSIZE_OPTION(0),
1657  {NULL}
1658 };
1659 
1660 DEFINE_BIQUAD_FILTER(highpass, "Apply a high-pass filter with 3dB point frequency.");
1661 #endif /* CONFIG_HIGHPASS_FILTER */
1662 #if CONFIG_ALLPASS_FILTER
1663 static const AVOption allpass_options[] = {
1664  {"frequency", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1665  {"f", "set central frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=3000}, 0, 999999, FLAGS},
1667  WIDTH_OPTION(0.707),
1668  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1669  {"order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
1670  {"o", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, FLAGS},
1672  PRECISION_OPTION(-1),
1673  {NULL}
1674 };
1675 
1676 DEFINE_BIQUAD_FILTER(allpass, "Apply a two-pole all-pass filter.");
1677 #endif /* CONFIG_ALLPASS_FILTER */
1678 #if CONFIG_BIQUAD_FILTER
1679 static const AVOption biquad_options[] = {
1680  {"a0", NULL, OFFSET(oa[0]), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT32_MIN, INT32_MAX, FLAGS},
1681  {"a1", NULL, OFFSET(oa[1]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1682  {"a2", NULL, OFFSET(oa[2]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1683  {"b0", NULL, OFFSET(ob[0]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1684  {"b1", NULL, OFFSET(ob[1]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1685  {"b2", NULL, OFFSET(ob[2]), AV_OPT_TYPE_DOUBLE, {.dbl=0}, INT32_MIN, INT32_MAX, FLAGS},
1686  MIX_CHANNELS_NORMALIZE_OPTION(1, "all", 0),
1688  PRECISION_OPTION(-1),
1689  BLOCKSIZE_OPTION(0),
1690  {NULL}
1691 };
1692 
1693 DEFINE_BIQUAD_FILTER(biquad, "Apply a biquad IIR filter with the given coefficients.");
1694 #endif /* CONFIG_BIQUAD_FILTER */
A
#define A(x)
Definition: vpx_arith.h:28
BLOCKSIZE_OPTION
#define BLOCKSIZE_OPTION(x)
Definition: af_biquads.c:1517
av_samples_copy
int av_samples_copy(uint8_t *const *dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition: samplefmt.c:222
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:97
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
td
#define td
Definition: regdef.h:70
ff_exp10
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
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
BiquadsContext::normalize
int normalize
Definition: af_biquads.c:133
out
FILE * out
Definition: movenc.c:55
DII
@ DII
Definition: af_biquads.c:105
BiquadsContext
Definition: af_biquads.c:114
BiquadsContext::block_align
int block_align
Definition: af_biquads.c:149
BiquadsContext::width_type
int width_type
Definition: af_biquads.c:118
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
BIQUAD_LATT_FILTER
#define BIQUAD_LATT_FILTER(name, type, ftype, min, max, need_clipping)
Definition: af_biquads.c:437
NB_TTYPE
@ NB_TTYPE
Definition: af_biquads.c:111
DEFINE_BIQUAD_FILTER
#define DEFINE_BIQUAD_FILTER(name, description)
Definition: af_biquads.c:1472
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
SLOPE
@ SLOPE
Definition: af_biquads.c:98
normalize.log
log
Definition: normalize.py:21
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
reverse_samples
static void reverse_samples(AVFrame *out, AVFrame *in, int p, int oo, int io, int nb_samples)
Definition: af_biquads.c:1204
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:486
av_channel_layout_channel_from_index
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
Definition: channel_layout.c:664
av_samples_set_silence
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:246
AVOption
AVOption.
Definition: opt.h:357
b
#define b
Definition: input.c:41
BiquadsContext::csg
int csg
Definition: af_biquads.c:120
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:822
bandreject
@ bandreject
Definition: af_biquads.c:84
BiquadsContext::block_samples
int block_samples
Definition: af_biquads.c:123
AVFILTER_DEFINE_CLASS_EXT
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition: internal.h:315
BiquadsContext::a_float
float a_float[3]
Definition: af_biquads.c:139
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:527
BiquadsContext::block
AVFrame * block[3]
Definition: af_biquads.c:145
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
sample_rate
sample_rate
Definition: ffmpeg_filter.c:424
formats.h
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1442
convert_dir2latt
static void convert_dir2latt(BiquadsContext *s)
Definition: af_biquads.c:603
v0
#define v0
Definition: regdef.h:26
BiquadsContext::b_float
float b_float[3]
Definition: af_biquads.c:140
outputs
static const AVFilterPad outputs[]
Definition: af_biquads.c:1436
FFSIGN
#define FFSIGN(a)
Definition: common.h:74
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:775
BiquadsContext::transform_type
int transform_type
Definition: af_biquads.c:121
pts
static int64_t pts
Definition: transcode_aac.c:644
BiquadsContext::ch_layout
AVChannelLayout ch_layout
Definition: af_biquads.c:132
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
BiquadsContext::bypass
int bypass
Definition: af_biquads.c:125
ZDF
@ ZDF
Definition: af_biquads.c:110
FilterType
FilterType
Definition: af_adenorm.c:26
avassert.h
BiquadsContext::ob
double ob[3]
Definition: af_biquads.c:143
activate
static int activate(AVFilterContext *ctx)
Definition: af_biquads.c:1365
av_cold
#define av_cold
Definition: attributes.h:90
convert_dir2zdf
static void convert_dir2zdf(BiquadsContext *s, int sample_rate)
Definition: af_biquads.c:673
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:198
BIQUAD_ZDF_FILTER
#define BIQUAD_ZDF_FILTER(name, type, ftype, min, max, need_clipping, two)
Definition: af_biquads.c:548
TransformType
TransformType
Definition: webp.c:113
BiquadsContext::ch_layout_str
char * ch_layout_str
Definition: af_biquads.c:131
BIQUAD_TDI_FILTER
#define BIQUAD_TDI_FILTER(name, type, ftype, min, max, need_clipping)
Definition: af_biquads.c:332
BIQUAD_TDII_FILTER
#define BIQUAD_TDII_FILTER(name, type, ftype, min, max, need_clipping)
Definition: af_biquads.c:389
convert_dir2svf
static void convert_dir2svf(BiquadsContext *s)
Definition: af_biquads.c:620
g
const char * g
Definition: vf_curves.c:128
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:247
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:874
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
highpass
@ highpass
Definition: af_biquads.c:86
filters.h
BiquadsContext::precision
int precision
Definition: af_biquads.c:122
NB_WTYPE
@ NB_WTYPE
Definition: af_biquads.c:100
ctx
AVFormatContext * ctx
Definition: movenc.c:49
BiquadsContext::width
double width
Definition: af_biquads.c:129
BiquadsContext::poles
int poles
Definition: af_biquads.c:119
BiquadsContext::a_double
double a_double[3]
Definition: af_biquads.c:136
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1462
NULL
#define NULL
Definition: coverity.c:32
biquad
@ biquad
Definition: af_biquads.c:79
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:709
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *buf, int eof)
Definition: af_biquads.c:1296
allpass
@ allpass
Definition: af_biquads.c:85
WIDTH_OPTION
#define WIDTH_OPTION(x)
Definition: af_biquads.c:1476
BIQUAD_FILTER
#define BIQUAD_FILTER(name, type, ftype, min, max, need_clipping)
Definition: af_biquads.c:201
MIX_CHANNELS_NORMALIZE_OPTION
#define MIX_CHANNELS_NORMALIZE_OPTION(x, y, z)
Definition: af_biquads.c:1489
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:804
exp
int8_t exp
Definition: eval.c:73
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1389
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_biquads.c:1411
BiquadsContext::order
int order
Definition: af_biquads.c:134
KHERTZ
@ KHERTZ
Definition: af_biquads.c:99
lowpass
@ lowpass
Definition: af_biquads.c:87
AF
#define AF
Definition: af_biquads.c:1446
f
f
Definition: af_crystalizer.c:121
filter_channel
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_biquads.c:1239
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
NONE
@ NONE
Definition: af_biquads.c:94
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
highshelf
@ highshelf
Definition: af_biquads.c:89
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:645
bass
@ bass
Definition: af_biquads.c:81
BiquadsContext::cache
AVFrame * cache[2]
Definition: af_biquads.c:148
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:887
BIQUAD_SVF_FILTER
#define BIQUAD_SVF_FILTER(name, type, ftype, min, max, need_clipping)
Definition: af_biquads.c:496
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_biquads.c:1194
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
BiquadsContext::nb_samples
int nb_samples
Definition: af_biquads.c:152
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
M_PI
#define M_PI
Definition: mathematics.h:67
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
LATT
@ LATT
Definition: af_biquads.c:108
internal.h
BiquadsContext::mix
double mix
Definition: af_biquads.c:130
QFACTOR
@ QFACTOR
Definition: af_biquads.c:97
AVChannel
AVChannel
Definition: channel_layout.h:47
av_channel_layout_from_string
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
Definition: channel_layout.c:303
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:454
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
BiquadsContext::oa
double oa[3]
Definition: af_biquads.c:142
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:435
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:827
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
M_PI_4
#define M_PI_4
Definition: mathematics.h:79
ThreadData
Used for passing data between threads.
Definition: dsddec.c:71
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
TDII
@ TDII
Definition: af_biquads.c:107
config_filter
static int config_filter(AVFilterLink *outlink, int reset)
Definition: af_biquads.c:797
tiltshelf
@ tiltshelf
Definition: af_biquads.c:90
len
int len
Definition: vorbis_enc_data.h:426
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1417
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
ThreadData::eof
int eof
Definition: af_biquads.c:1201
PRECISION_OPTION
#define PRECISION_OPTION(x)
Definition: af_biquads.c:1508
TRANSFORM_OPTION
#define TRANSFORM_OPTION(x)
Definition: af_biquads.c:1497
treble
@ treble
Definition: af_biquads.c:82
OFFSET
#define OFFSET(x)
Definition: af_biquads.c:1444
ret
ret
Definition: filter_design.txt:187
BIQUAD_DII_FILTER
#define BIQUAD_DII_FILTER(name, type, ftype, min, max, need_clipping)
Definition: af_biquads.c:283
WidthType
WidthType
Definition: af_biquads.c:93
BiquadsContext::filter
void(* filter)(struct BiquadsContext *s, const void *ibuf, void *obuf, int len, void *cache, int *clip, int disabled)
Definition: af_biquads.c:154
BiquadsContext::filter_type
enum FilterType filter_type
Definition: af_biquads.c:117
status
ov_status_e status
Definition: dnn_backend_openvino.c:101
channel_layout.h
HERTZ
@ HERTZ
Definition: af_biquads.c:95
av_channel_layout_index_from_channel
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
Definition: channel_layout.c:704
BiquadsContext::gain
double gain
Definition: af_biquads.c:127
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:245
avfilter.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:433
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_biquads.c:158
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
DEFINE_BIQUAD_FILTER_2
#define DEFINE_BIQUAD_FILTER_2(name_, description_, priv_class_)
Definition: af_biquads.c:1448
WIDTH_TYPE_OPTION
#define WIDTH_TYPE_OPTION(x)
Definition: af_biquads.c:1480
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_biquads.c:1424
ffmath.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
factor
static const int factor[16]
Definition: vf_pp7.c:79
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:440
FLAGS
#define FLAGS
Definition: af_biquads.c:1445
DI
@ DI
Definition: af_biquads.c:104
mem.h
audio.h
TDI
@ TDI
Definition: af_biquads.c:106
SVF
@ SVF
Definition: af_biquads.c:109
lowshelf
@ lowshelf
Definition: af_biquads.c:88
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:261
equalizer
@ equalizer
Definition: af_biquads.c:80
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
convert_width2qfactor
static double convert_width2qfactor(double width, double frequency, double gain, double sample_rate, int width_type)
Definition: af_biquads.c:638
K
#define K
Definition: palette.c:25
OCTAVE
@ OCTAVE
Definition: af_biquads.c:96
int32_t
int32_t
Definition: audioconvert.c:56
Q
#define Q(x)
Definition: filter_template.c:433
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
BiquadsContext::clip
int * clip
Definition: af_biquads.c:147
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:134
BiquadsContext::b_double
double b_double[3]
Definition: af_biquads.c:137
BiquadsContext::frequency
double frequency
Definition: af_biquads.c:128
BiquadsContext::pts
int64_t pts
Definition: af_biquads.c:151
channel
channel
Definition: ebur128.h:39
bandpass
@ bandpass
Definition: af_biquads.c:83
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:235