FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_anequalizer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3  * Copyright (c) 2015 Paul B Mahol
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 #include "libavutil/intreadwrite.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/parseutils.h"
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "audio.h"
29 
30 #define FILTER_ORDER 4
31 
32 enum FilterType {
37 };
38 
39 typedef struct FoSection {
40  double a0, a1, a2, a3, a4;
41  double b0, b1, b2, b3, b4;
42 
43  double num[4];
44  double denum[4];
45 } FoSection;
46 
47 typedef struct EqualizatorFilter {
48  int ignore;
49  int channel;
50  int type;
51 
52  double freq;
53  double gain;
54  double width;
55 
58 
59 typedef struct AudioNEqualizerContext {
60  const AVClass *class;
61  char *args;
62  char *colors;
64  int w, h;
65 
66  double mag;
67  int fscale;
73 
74 #define OFFSET(x) offsetof(AudioNEqualizerContext, x)
75 #define A AV_OPT_FLAG_AUDIO_PARAM
76 #define V AV_OPT_FLAG_VIDEO_PARAM
77 #define F AV_OPT_FLAG_FILTERING_PARAM
78 
79 static const AVOption anequalizer_options[] = {
80  { "params", NULL, OFFSET(args), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, A|F },
81  { "curves", "draw frequency response curves", OFFSET(draw_curves), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, V|F },
82  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, V|F },
83  { "mgain", "set max gain", OFFSET(mag), AV_OPT_TYPE_DOUBLE, {.dbl=60}, -900, 900, V|F },
84  { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, V|F, "fscale" },
85  { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, V|F, "fscale" },
86  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, V|F, "fscale" },
87  { "colors", "set channels curves colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, V|F },
88  { NULL }
89 };
90 
91 AVFILTER_DEFINE_CLASS(anequalizer);
92 
94 {
96  char *colors, *color, *saveptr = NULL;
97  int ch, i, n;
98 
99  colors = av_strdup(s->colors);
100  if (!colors)
101  return;
102 
103  memset(out->data[0], 0, s->h * out->linesize[0]);
104 
105  for (ch = 0; ch < inlink->channels; ch++) {
106  uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
107  int prev_v = -1;
108  double f;
109 
110  color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
111  if (color)
112  av_parse_color(fg, color, -1, ctx);
113 
114  for (f = 0; f < s->w; f++) {
115  double zr, zi, zr2, zi2;
116  double Hr, Hi;
117  double Hmag = 1;
118  double w;
119  int v, y, x;
120 
121  w = M_PI * (s->fscale ? pow(s->w - 1, f / s->w) : f) / (s->w - 1);
122  zr = cos(w);
123  zr2 = zr * zr;
124  zi = -sin(w);
125  zi2 = zi * zi;
126 
127  for (n = 0; n < s->nb_filters; n++) {
128  if (s->filters[n].channel != ch ||
129  s->filters[n].ignore)
130  continue;
131 
132  for (i = 0; i < FILTER_ORDER / 2; i++) {
133  FoSection *S = &s->filters[n].section[i];
134 
135  /* H *= (((((S->b4 * z + S->b3) * z + S->b2) * z + S->b1) * z + S->b0) /
136  ((((S->a4 * z + S->a3) * z + S->a2) * z + S->a1) * z + S->a0)); */
137 
138  Hr = S->b4*(1-8*zr2*zi2) + S->b2*(zr2-zi2) + zr*(S->b1+S->b3*(zr2-3*zi2))+ S->b0;
139  Hi = zi*(S->b3*(3*zr2-zi2) + S->b1 + 2*zr*(2*S->b4*(zr2-zi2) + S->b2));
140  Hmag *= hypot(Hr, Hi);
141  Hr = S->a4*(1-8*zr2*zi2) + S->a2*(zr2-zi2) + zr*(S->a1+S->a3*(zr2-3*zi2))+ S->a0;
142  Hi = zi*(S->a3*(3*zr2-zi2) + S->a1 + 2*zr*(2*S->a4*(zr2-zi2) + S->a2));
143  Hmag /= hypot(Hr, Hi);
144  }
145  }
146 
147  v = av_clip((1. + -20 * log10(Hmag) / s->mag) * s->h / 2, 0, s->h - 1);
148  x = lrint(f);
149  if (prev_v == -1)
150  prev_v = v;
151  if (v <= prev_v) {
152  for (y = v; y <= prev_v; y++)
153  AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
154  } else {
155  for (y = prev_v; y <= v; y++)
156  AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
157  }
158 
159  prev_v = v;
160  }
161  }
162 
163  av_free(colors);
164 }
165 
166 static int config_video(AVFilterLink *outlink)
167 {
168  AVFilterContext *ctx = outlink->src;
169  AudioNEqualizerContext *s = ctx->priv;
170  AVFilterLink *inlink = ctx->inputs[0];
171  AVFrame *out;
172 
173  outlink->w = s->w;
174  outlink->h = s->h;
175 
176  av_frame_free(&s->video);
177  s->video = out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
178  if (!out)
179  return AVERROR(ENOMEM);
180  outlink->sample_aspect_ratio = (AVRational){1,1};
181 
182  draw_curves(ctx, inlink, out);
183 
184  return 0;
185 }
186 
188 {
189  AudioNEqualizerContext *s = ctx->priv;
190  AVFilterPad pad, vpad;
191 
192  pad = (AVFilterPad){
193  .name = av_strdup("out0"),
194  .type = AVMEDIA_TYPE_AUDIO,
195  };
196 
197  if (!pad.name)
198  return AVERROR(ENOMEM);
199 
200  if (s->draw_curves) {
201  vpad = (AVFilterPad){
202  .name = av_strdup("out1"),
203  .type = AVMEDIA_TYPE_VIDEO,
204  .config_props = config_video,
205  };
206  if (!vpad.name)
207  return AVERROR(ENOMEM);
208  }
209 
210  ff_insert_outpad(ctx, 0, &pad);
211 
212  if (s->draw_curves)
213  ff_insert_outpad(ctx, 1, &vpad);
214 
215  return 0;
216 }
217 
219 {
220  AVFilterLink *inlink = ctx->inputs[0];
221  AVFilterLink *outlink = ctx->outputs[0];
222  AudioNEqualizerContext *s = ctx->priv;
225  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
226  static const enum AVSampleFormat sample_fmts[] = {
229  };
230  int ret;
231 
232  if (s->draw_curves) {
233  AVFilterLink *videolink = ctx->outputs[1];
234  formats = ff_make_format_list(pix_fmts);
235  if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
236  return ret;
237  }
238 
239  formats = ff_make_format_list(sample_fmts);
240  if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0 ||
241  (ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
242  return ret;
243 
244  layouts = ff_all_channel_counts();
245  if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0 ||
246  (ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts)) < 0)
247  return ret;
248 
249  formats = ff_all_samplerates();
250  if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0 ||
251  (ret = ff_formats_ref(formats, &outlink->in_samplerates)) < 0)
252  return ret;
253 
254  return 0;
255 }
256 
258 {
259  AudioNEqualizerContext *s = ctx->priv;
260 
261  av_freep(&ctx->output_pads[0].name);
262  if (s->draw_curves)
263  av_freep(&ctx->output_pads[1].name);
264  av_frame_free(&s->video);
265  av_freep(&s->filters);
266  s->nb_filters = 0;
267  s->nb_allocated = 0;
268 }
269 
270 static void butterworth_fo_section(FoSection *S, double beta,
271  double si, double g, double g0,
272  double D, double c0)
273 {
274  if (c0 == 1 || c0 == -1) {
275  S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D;
276  S->b1 = 2*c0*(g*g*beta*beta - g0*g0)/D;
277  S->b2 = (g*g*beta*beta - 2*g0*g*beta*si + g0*g0)/D;
278  S->b3 = 0;
279  S->b4 = 0;
280 
281  S->a0 = 1;
282  S->a1 = 2*c0*(beta*beta - 1)/D;
283  S->a2 = (beta*beta - 2*beta*si + 1)/D;
284  S->a3 = 0;
285  S->a4 = 0;
286  } else {
287  S->b0 = (g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/D;
288  S->b1 = -4*c0*(g0*g0 + g*g0*si*beta)/D;
289  S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - g*g*beta*beta)/D;
290  S->b3 = -4*c0*(g0*g0 - g*g0*si*beta)/D;
291  S->b4 = (g*g*beta*beta - 2*g*g0*si*beta + g0*g0)/D;
292 
293  S->a0 = 1;
294  S->a1 = -4*c0*(1 + si*beta)/D;
295  S->a2 = 2*(1 + 2*c0*c0 - beta*beta)/D;
296  S->a3 = -4*c0*(1 - si*beta)/D;
297  S->a4 = (beta*beta - 2*si*beta + 1)/D;
298  }
299 }
300 
302  int N, double w0, double wb,
303  double G, double Gb, double G0)
304 {
305  double g, c0, g0, beta;
306  double epsilon;
307  int r = N % 2;
308  int L = (N - r) / 2;
309  int i;
310 
311  if (G == 0 && G0 == 0) {
312  f->section[0].a0 = 1;
313  f->section[0].b0 = 1;
314  f->section[1].a0 = 1;
315  f->section[1].b0 = 1;
316  return;
317  }
318 
319  G = ff_exp10(G/20);
320  Gb = ff_exp10(Gb/20);
321  G0 = ff_exp10(G0/20);
322 
323  epsilon = sqrt((G * G - Gb * Gb) / (Gb * Gb - G0 * G0));
324  g = pow(G, 1.0 / N);
325  g0 = pow(G0, 1.0 / N);
326  beta = pow(epsilon, -1.0 / N) * tan(wb/2);
327  c0 = cos(w0);
328 
329  for (i = 1; i <= L; i++) {
330  double ui = (2.0 * i - 1) / N;
331  double si = sin(M_PI * ui / 2.0);
332  double Di = beta * beta + 2 * si * beta + 1;
333 
334  butterworth_fo_section(&f->section[i - 1], beta, si, g, g0, Di, c0);
335  }
336 }
337 
338 static void chebyshev1_fo_section(FoSection *S, double a,
339  double c, double tetta_b,
340  double g0, double si, double b,
341  double D, double c0)
342 {
343  if (c0 == 1 || c0 == -1) {
344  S->b0 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) + 2*g0*b*si*tetta_b*tetta_b + g0*g0)/D;
345  S->b1 = 2*c0*(tetta_b*tetta_b*(b*b+g0*g0*c*c) - g0*g0)/D;
346  S->b2 = (tetta_b*tetta_b*(b*b+g0*g0*c*c) - 2*g0*b*si*tetta_b + g0*g0)/D;
347  S->b3 = 0;
348  S->b4 = 0;
349 
350  S->a0 = 1;
351  S->a1 = 2*c0*(tetta_b*tetta_b*(a*a+c*c) - 1)/D;
352  S->a2 = (tetta_b*tetta_b*(a*a+c*c) - 2*a*si*tetta_b + 1)/D;
353  S->a3 = 0;
354  S->a4 = 0;
355  } else {
356  S->b0 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b + 2*g0*b*si*tetta_b + g0*g0)/D;
357  S->b1 = -4*c0*(g0*g0 + g0*b*si*tetta_b)/D;
358  S->b2 = 2*(g0*g0*(1 + 2*c0*c0) - (b*b + g0*g0*c*c)*tetta_b*tetta_b)/D;
359  S->b3 = -4*c0*(g0*g0 - g0*b*si*tetta_b)/D;
360  S->b4 = ((b*b + g0*g0*c*c)*tetta_b*tetta_b - 2*g0*b*si*tetta_b + g0*g0)/D;
361 
362  S->a0 = 1;
363  S->a1 = -4*c0*(1 + a*si*tetta_b)/D;
364  S->a2 = 2*(1 + 2*c0*c0 - (a*a + c*c)*tetta_b*tetta_b)/D;
365  S->a3 = -4*c0*(1 - a*si*tetta_b)/D;
366  S->a4 = ((a*a + c*c)*tetta_b*tetta_b - 2*a*si*tetta_b + 1)/D;
367  }
368 }
369 
371  int N, double w0, double wb,
372  double G, double Gb, double G0)
373 {
374  double a, b, c0, g0, alfa, beta, tetta_b;
375  double epsilon;
376  int r = N % 2;
377  int L = (N - r) / 2;
378  int i;
379 
380  if (G == 0 && G0 == 0) {
381  f->section[0].a0 = 1;
382  f->section[0].b0 = 1;
383  f->section[1].a0 = 1;
384  f->section[1].b0 = 1;
385  return;
386  }
387 
388  G = ff_exp10(G/20);
389  Gb = ff_exp10(Gb/20);
390  G0 = ff_exp10(G0/20);
391 
392  epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
393  g0 = pow(G0,1.0/N);
394  alfa = pow(1.0/epsilon + sqrt(1 + 1/(epsilon*epsilon)), 1.0/N);
395  beta = pow(G/epsilon + Gb * sqrt(1 + 1/(epsilon*epsilon)), 1.0/N);
396  a = 0.5 * (alfa - 1.0/alfa);
397  b = 0.5 * (beta - g0*g0*(1/beta));
398  tetta_b = tan(wb/2);
399  c0 = cos(w0);
400 
401  for (i = 1; i <= L; i++) {
402  double ui = (2.0*i-1.0)/N;
403  double ci = cos(M_PI*ui/2.0);
404  double si = sin(M_PI*ui/2.0);
405  double Di = (a*a + ci*ci)*tetta_b*tetta_b + 2.0*a*si*tetta_b + 1;
406 
407  chebyshev1_fo_section(&f->section[i - 1], a, ci, tetta_b, g0, si, b, Di, c0);
408  }
409 }
410 
411 static void chebyshev2_fo_section(FoSection *S, double a,
412  double c, double tetta_b,
413  double g, double si, double b,
414  double D, double c0)
415 {
416  if (c0 == 1 || c0 == -1) {
417  S->b0 = (g*g*tetta_b*tetta_b + 2*tetta_b*g*b*si + b*b + g*g*c*c)/D;
418  S->b1 = 2*c0*(g*g*tetta_b*tetta_b - b*b - g*g*c*c)/D;
419  S->b2 = (g*g*tetta_b*tetta_b - 2*tetta_b*g*b*si + b*b + g*g*c*c)/D;
420  S->b3 = 0;
421  S->b4 = 0;
422 
423  S->a0 = 1;
424  S->a1 = 2*c0*(tetta_b*tetta_b - a*a - c*c)/D;
425  S->a2 = (tetta_b*tetta_b - 2*tetta_b*a*si + a*a + c*c)/D;
426  S->a3 = 0;
427  S->a4 = 0;
428  } else {
429  S->b0 = (g*g*tetta_b*tetta_b + 2*g*b*si*tetta_b + b*b + g*g*c*c)/D;
430  S->b1 = -4*c0*(b*b + g*g*c*c + g*b*si*tetta_b)/D;
431  S->b2 = 2*((b*b + g*g*c*c)*(1 + 2*c0*c0) - g*g*tetta_b*tetta_b)/D;
432  S->b3 = -4*c0*(b*b + g*g*c*c - g*b*si*tetta_b)/D;
433  S->b4 = (g*g*tetta_b*tetta_b - 2*g*b*si*tetta_b + b*b + g*g*c*c)/D;
434 
435  S->a0 = 1;
436  S->a1 = -4*c0*(a*a + c*c + a*si*tetta_b)/D;
437  S->a2 = 2*((a*a + c*c)*(1 + 2*c0*c0) - tetta_b*tetta_b)/D;
438  S->a3 = -4*c0*(a*a + c*c - a*si*tetta_b)/D;
439  S->a4 = (tetta_b*tetta_b - 2*a*si*tetta_b + a*a + c*c)/D;
440  }
441 }
442 
444  int N, double w0, double wb,
445  double G, double Gb, double G0)
446 {
447  double a, b, c0, tetta_b;
448  double epsilon, g, eu, ew;
449  int r = N % 2;
450  int L = (N - r) / 2;
451  int i;
452 
453  if (G == 0 && G0 == 0) {
454  f->section[0].a0 = 1;
455  f->section[0].b0 = 1;
456  f->section[1].a0 = 1;
457  f->section[1].b0 = 1;
458  return;
459  }
460 
461  G = ff_exp10(G/20);
462  Gb = ff_exp10(Gb/20);
463  G0 = ff_exp10(G0/20);
464 
465  epsilon = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
466  g = pow(G, 1.0 / N);
467  eu = pow(epsilon + sqrt(1 + epsilon*epsilon), 1.0/N);
468  ew = pow(G0*epsilon + Gb*sqrt(1 + epsilon*epsilon), 1.0/N);
469  a = (eu - 1.0/eu)/2.0;
470  b = (ew - g*g/ew)/2.0;
471  tetta_b = tan(wb/2);
472  c0 = cos(w0);
473 
474  for (i = 1; i <= L; i++) {
475  double ui = (2.0 * i - 1.0)/N;
476  double ci = cos(M_PI * ui / 2.0);
477  double si = sin(M_PI * ui / 2.0);
478  double Di = tetta_b*tetta_b + 2*a*si*tetta_b + a*a + ci*ci;
479 
480  chebyshev2_fo_section(&f->section[i - 1], a, ci, tetta_b, g, si, b, Di, c0);
481  }
482 }
483 
484 static double butterworth_compute_bw_gain_db(double gain)
485 {
486  double bw_gain = 0;
487 
488  if (gain <= -6)
489  bw_gain = gain + 3;
490  else if(gain > -6 && gain < 6)
491  bw_gain = gain * 0.5;
492  else if(gain >= 6)
493  bw_gain = gain - 3;
494 
495  return bw_gain;
496 }
497 
498 static double chebyshev1_compute_bw_gain_db(double gain)
499 {
500  double bw_gain = 0;
501 
502  if (gain <= -6)
503  bw_gain = gain + 1;
504  else if(gain > -6 && gain < 6)
505  bw_gain = gain * 0.9;
506  else if(gain >= 6)
507  bw_gain = gain - 1;
508 
509  return bw_gain;
510 }
511 
512 static double chebyshev2_compute_bw_gain_db(double gain)
513 {
514  double bw_gain = 0;
515 
516  if (gain <= -6)
517  bw_gain = -3;
518  else if(gain > -6 && gain < 6)
519  bw_gain = gain * 0.3;
520  else if(gain >= 6)
521  bw_gain = 3;
522 
523  return bw_gain;
524 }
525 
526 static inline double hz_2_rad(double x, double fs)
527 {
528  return 2 * M_PI * x / fs;
529 }
530 
531 static void equalizer(EqualizatorFilter *f, double sample_rate)
532 {
533  double w0 = hz_2_rad(f->freq, sample_rate);
534  double wb = hz_2_rad(f->width, sample_rate);
535  double bw_gain;
536 
537  switch (f->type) {
538  case BUTTERWORTH:
539  bw_gain = butterworth_compute_bw_gain_db(f->gain);
540  butterworth_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
541  break;
542  case CHEBYSHEV1:
543  bw_gain = chebyshev1_compute_bw_gain_db(f->gain);
544  chebyshev1_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
545  break;
546  case CHEBYSHEV2:
547  bw_gain = chebyshev2_compute_bw_gain_db(f->gain);
548  chebyshev2_bp_filter(f, FILTER_ORDER, w0, wb, f->gain, bw_gain, 0);
549  break;
550  }
551 
552 }
553 
555 {
556  equalizer(&s->filters[s->nb_filters], inlink->sample_rate);
557  if (s->nb_filters >= s->nb_allocated) {
559 
560  filters = av_calloc(s->nb_allocated, 2 * sizeof(*s->filters));
561  if (!filters)
562  return AVERROR(ENOMEM);
563  memcpy(filters, s->filters, sizeof(*s->filters) * s->nb_allocated);
564  av_free(s->filters);
565  s->filters = filters;
566  s->nb_allocated *= 2;
567  }
568  s->nb_filters++;
569 
570  return 0;
571 }
572 
573 static int config_input(AVFilterLink *inlink)
574 {
575  AVFilterContext *ctx = inlink->dst;
576  AudioNEqualizerContext *s = ctx->priv;
577  char *args = av_strdup(s->args);
578  char *saveptr = NULL;
579  int ret = 0;
580 
581  if (!args)
582  return AVERROR(ENOMEM);
583 
584  s->nb_allocated = 32 * inlink->channels;
585  s->filters = av_calloc(inlink->channels, 32 * sizeof(*s->filters));
586  if (!s->filters) {
587  s->nb_allocated = 0;
588  av_free(args);
589  return AVERROR(ENOMEM);
590  }
591 
592  while (1) {
593  char *arg = av_strtok(s->nb_filters == 0 ? args : NULL, "|", &saveptr);
594 
595  if (!arg)
596  break;
597 
598  s->filters[s->nb_filters].type = 0;
599  if (sscanf(arg, "c%d f=%lf w=%lf g=%lf t=%d", &s->filters[s->nb_filters].channel,
600  &s->filters[s->nb_filters].freq,
601  &s->filters[s->nb_filters].width,
602  &s->filters[s->nb_filters].gain,
603  &s->filters[s->nb_filters].type) != 5 &&
604  sscanf(arg, "c%d f=%lf w=%lf g=%lf", &s->filters[s->nb_filters].channel,
605  &s->filters[s->nb_filters].freq,
606  &s->filters[s->nb_filters].width,
607  &s->filters[s->nb_filters].gain) != 4 ) {
608  av_free(args);
609  return AVERROR(EINVAL);
610  }
611 
612  if (s->filters[s->nb_filters].freq < 0 ||
613  s->filters[s->nb_filters].freq > inlink->sample_rate / 2.0)
614  s->filters[s->nb_filters].ignore = 1;
615 
616  if (s->filters[s->nb_filters].channel < 0 ||
617  s->filters[s->nb_filters].channel >= inlink->channels)
618  s->filters[s->nb_filters].ignore = 1;
619 
620  s->filters[s->nb_filters].type = av_clip(s->filters[s->nb_filters].type, 0, NB_TYPES - 1);
621  ret = add_filter(s, inlink);
622  if (ret < 0)
623  break;
624  }
625 
626  av_free(args);
627 
628  return ret;
629 }
630 
631 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
632  char *res, int res_len, int flags)
633 {
634  AudioNEqualizerContext *s = ctx->priv;
635  AVFilterLink *inlink = ctx->inputs[0];
636  int ret = AVERROR(ENOSYS);
637 
638  if (!strcmp(cmd, "change")) {
639  double freq, width, gain;
640  int filter;
641 
642  if (sscanf(args, "%d|f=%lf|w=%lf|g=%lf", &filter, &freq, &width, &gain) != 4)
643  return AVERROR(EINVAL);
644 
645  if (filter < 0 || filter >= s->nb_filters)
646  return AVERROR(EINVAL);
647 
648  if (freq < 0 || freq > inlink->sample_rate / 2.0)
649  return AVERROR(EINVAL);
650 
651  s->filters[filter].freq = freq;
652  s->filters[filter].width = width;
653  s->filters[filter].gain = gain;
654  equalizer(&s->filters[filter], inlink->sample_rate);
655  if (s->draw_curves)
656  draw_curves(ctx, inlink, s->video);
657 
658  ret = 0;
659  }
660 
661  return ret;
662 }
663 
664 static inline double section_process(FoSection *S, double in)
665 {
666  double out;
667 
668  out = S->b0 * in;
669  out+= S->b1 * S->num[0] - S->denum[0] * S->a1;
670  out+= S->b2 * S->num[1] - S->denum[1] * S->a2;
671  out+= S->b3 * S->num[2] - S->denum[2] * S->a3;
672  out+= S->b4 * S->num[3] - S->denum[3] * S->a4;
673 
674  S->num[3] = S->num[2];
675  S->num[2] = S->num[1];
676  S->num[1] = S->num[0];
677  S->num[0] = in;
678 
679  S->denum[3] = S->denum[2];
680  S->denum[2] = S->denum[1];
681  S->denum[1] = S->denum[0];
682  S->denum[0] = out;
683 
684  return out;
685 }
686 
687 static double process_sample(FoSection *s1, double in)
688 {
689  double p0 = in, p1;
690  int i;
691 
692  for (i = 0; i < FILTER_ORDER / 2; i++) {
693  p1 = section_process(&s1[i], p0);
694  p0 = p1;
695  }
696 
697  return p1;
698 }
699 
700 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
701 {
702  AVFilterContext *ctx = inlink->dst;
703  AudioNEqualizerContext *s = ctx->priv;
704  AVFilterLink *outlink = ctx->outputs[0];
705  double *bptr;
706  int i, n;
707 
708  for (i = 0; i < s->nb_filters; i++) {
709  EqualizatorFilter *f = &s->filters[i];
710 
711  if (f->gain == 0. || f->ignore)
712  continue;
713 
714  bptr = (double *)buf->extended_data[f->channel];
715  for (n = 0; n < buf->nb_samples; n++) {
716  double sample = bptr[n];
717 
718  sample = process_sample(f->section, sample);
719  bptr[n] = sample;
720  }
721  }
722 
723  if (s->draw_curves) {
724  const int64_t pts = buf->pts +
725  av_rescale_q(buf->nb_samples, (AVRational){ 1, inlink->sample_rate },
726  outlink->time_base);
727  int ret;
728 
729  s->video->pts = pts;
730  ret = ff_filter_frame(ctx->outputs[1], av_frame_clone(s->video));
731  if (ret < 0)
732  return ret;
733  }
734 
735  return ff_filter_frame(outlink, buf);
736 }
737 
738 static const AVFilterPad inputs[] = {
739  {
740  .name = "default",
741  .type = AVMEDIA_TYPE_AUDIO,
742  .config_props = config_input,
743  .filter_frame = filter_frame,
744  .needs_writable = 1,
745  },
746  { NULL }
747 };
748 
750  .name = "anequalizer",
751  .description = NULL_IF_CONFIG_SMALL("Apply high-order audio parametric multi band equalizer."),
752  .priv_size = sizeof(AudioNEqualizerContext),
753  .priv_class = &anequalizer_class,
754  .init = init,
755  .uninit = uninit,
757  .inputs = inputs,
758  .outputs = NULL,
761 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
static void chebyshev2_fo_section(FoSection *S, double a, double c, double tetta_b, double g, double si, double b, double D, double c0)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVOption.
Definition: opt.h:245
static void chebyshev1_bp_filter(EqualizatorFilter *f, int N, double w0, double wb, double G, double Gb, double G0)
AVFormatContext * ctx
Definition: movenc-test.c:48
Main libavfilter public API header.
const char * g
Definition: vf_curves.c:108
static double butterworth_compute_bw_gain_db(double gain)
static int config_video(AVFilterLink *outlink)
const char * b
Definition: vf_curves.c:109
static void butterworth_bp_filter(EqualizatorFilter *f, int N, double w0, double wb, double G, double Gb, double G0)
double, planar
Definition: samplefmt.h:71
static void butterworth_fo_section(FoSection *S, double beta, double si, double g, double g0, double D, double c0)
FoSection section[2]
static enum AVSampleFormat formats[]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
AVFILTER_DEFINE_CLASS(anequalizer)
#define sample
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:312
FilterType
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1163
AVFilterPad * output_pads
array of output pads
Definition: avfilter.h:315
Definition: vf_geq.c:46
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:262
static const AVOption anequalizer_options[]
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:82
#define N
Definition: vf_pp7.c:73
static void chebyshev2_bp_filter(EqualizatorFilter *f, int N, double w0, double wb, double G, double Gb, double G0)
static void chebyshev1_fo_section(FoSection *S, double a, double c, double tetta_b, double g0, double si, double b, double D, double c0)
static double hz_2_rad(double x, double fs)
#define FILTER_ORDER
#define A
static av_cold void uninit(AVFilterContext *ctx)
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:108
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition: parseutils.c:349
A filter pad used for either input or output.
Definition: internal.h:53
#define F
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
static int config_input(AVFilterLink *inlink)
#define S(s, c, i)
#define AVERROR(e)
Definition: error.h:43
static void equalizer(EqualizatorFilter *f, double sample_rate)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:154
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * r
Definition: vf_curves.c:107
void * priv
private data for use by the filter
Definition: avfilter.h:319
const char * arg
Definition: jacosubdec.c:66
#define V
static void draw_curves(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *out)
static int query_formats(AVFilterContext *ctx)
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:95
static av_const double hypot(double x, double y)
Definition: libm.h:366
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
static const AVFilterPad inputs[]
int n
Definition: avisynth_c.h:547
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
#define L(x)
Definition: vp56_arith.h:36
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:461
static double process_sample(FoSection *s1, double in)
A list of supported channel layouts.
Definition: formats.h:85
FILE * out
Definition: movenc-test.c:54
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
sample_rate
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
static double chebyshev2_compute_bw_gain_db(double gain)
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:553
AVFilter ff_af_anequalizer
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:141
rational number numerator/denominator
Definition: rational.h:43
const char * name
Filter name.
Definition: avfilter.h:145
static int add_filter(AudioNEqualizerContext *s, AVFilterLink *inlink)
#define s1
Definition: regdef.h:38
offset must point to two consecutive integers
Definition: opt.h:232
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
static int64_t pts
Global timestamp for the audio frames.
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
static double section_process(FoSection *S, double in)
double denum[4]
static double chebyshev1_compute_bw_gain_db(double gain)
D(D(float, sse)
Definition: rematrix_init.c:28
static double c[64]
static av_cold int init(AVFilterContext *ctx)
#define OFFSET(x)
#define av_free(p)
static const struct PPFilter filters[]
Definition: postprocess.c:137
A list of supported formats for one end of a filter link.
Definition: formats.h:64
#define lrint
Definition: tablegen.h:53
An instance of a filter.
Definition: avfilter.h:304
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: internal.h:306
#define M_PI
Definition: mathematics.h:46
EqualizatorFilter * filters
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
double num[4]
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:225
static int ff_insert_outpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new output pad for the filter.
Definition: internal.h:291
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
for(j=16;j >0;--j)
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
static int width