History

avfilter: add asuperpass and asuperstop filter

Paul B Mahol committed 5 years ago

Commit b607f191 parent 628d02a6 files · +185 −5Browse files

Showing the changes to libavfilter (4 of 6 files). Show all

Unified · Split

4 files
  1. libavfilter/Makefile +2 −0
  2. libavfilter/af_asupercut.c +129 −4
  3. libavfilter/allfilters.c +2 −0
  4. libavfilter/version.h +1 −1
libavfilter/Makefile+2−0
@@ -92,6 +92,8 @@ OBJS-$(CONFIG_ASTREAMSELECT_FILTER) += f_streamselect.o framesync.o · 91 unmodified lines
9292 OBJS-$(CONFIG_ASUBBOOST_FILTER) += af_asubboost.o
9393 OBJS-$(CONFIG_ASUBCUT_FILTER) += af_asupercut.o
9494 OBJS-$(CONFIG_ASUPERCUT_FILTER) += af_asupercut.o
95+OBJS-$(CONFIG_ASUPERPASS_FILTER) += af_asupercut.o
96+OBJS-$(CONFIG_ASUPERSTOP_FILTER) += af_asupercut.o
9597 OBJS-$(CONFIG_ATEMPO_FILTER) += af_atempo.o
9698 OBJS-$(CONFIG_ATRIM_FILTER) += trim.o
9799 OBJS-$(CONFIG_AXCORRELATE_FILTER) += af_axcorrelate.o
libavfilter/af_asupercut.c+129−4
@@ -1,4 +1,7 @@
11 /*
2+ * Copyright (c) 2005 Boðaç Topaktaþ
3+ * Copyright (c) 2020 Paul B Mahol
4+ *
25 * This file is part of FFmpeg.
36 *
47 * FFmpeg is free software; you can redistribute it and/or
@@ -33,6 +36,7 @@ typedef struct ASuperCutContext { · 28 unmodified lines
3336
3437 double cutoff;
3538 double level;
39+ double qfactor;
3640 int order;
3741
3842 int filter_count;
@@ -93,10 +97,11 @@ static int get_coeffs(AVFilterContext *ctx) · 54 unmodified lines
9397 if (s->bypass)
9498 return 0;
9599
96− s->filter_count = s->order / 2 + (s->order & 1);
97− calc_q_factors(s->order, q);
98−
99100 if (!strcmp(ctx->filter->name, "asubcut")) {
101+ s->filter_count = s->order / 2 + (s->order & 1);
102+
103+ calc_q_factors(s->order, q);
104+
100105 if (s->order & 1) {
101106 BiquadCoeffs *coeffs = &s->coeffs[0];
102107 double omega = 2. * tan(M_PI * w0);
@@ -119,7 +124,11 @@ static int get_coeffs(AVFilterContext *ctx) · 16 unmodified lines
119124 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
120125 coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
121126 }
122− } else {
127+ } else if (!strcmp(ctx->filter->name, "asupercut")) {
128+ s->filter_count = s->order / 2 + (s->order & 1);
129+
130+ calc_q_factors(s->order, q);
131+
123132 if (s->order & 1) {
124133 BiquadCoeffs *coeffs = &s->coeffs[0];
125134 double omega = 2. * tan(M_PI * w0);
@@ -142,6 +151,74 @@ static int get_coeffs(AVFilterContext *ctx) · 16 unmodified lines
142151 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
143152 coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
144153 }
154+ } else if (!strcmp(ctx->filter->name, "asuperpass")) {
155+ double alpha, beta, gamma, theta;
156+ double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate);
157+ double d_E;
158+
159+ s->filter_count = s->order / 2;
160+ d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0);
161+
162+ for (int b = 0; b < s->filter_count; b += 2) {
163+ double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count));
164+ double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.);
165+ double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.)));
166+ double B = D * (d_E / 2.) / d;
167+ double W = B + sqrt(B * B - 1.);
168+
169+ for (int j = 0; j < 2; j++) {
170+ BiquadCoeffs *coeffs = &s->coeffs[b + j];
171+
172+ if (j == 1)
173+ theta = 2. * atan(tan(theta_0 / 2.) / W);
174+ else
175+ theta = 2. * atan(W * tan(theta_0 / 2.));
176+
177+ beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta)));
178+ gamma = (0.5 + beta) * cos(theta);
179+ alpha = 0.5 * (0.5 - beta) * sqrt(1. + pow((W - (1. / W)) / d, 2.));
180+
181+ coeffs->a1 = 2. * gamma;
182+ coeffs->a2 = -2. * beta;
183+ coeffs->b0 = 2. * alpha;
184+ coeffs->b1 = 0.;
185+ coeffs->b2 = -2. * alpha;
186+ }
187+ }
188+ } else if (!strcmp(ctx->filter->name, "asuperstop")) {
189+ double alpha, beta, gamma, theta;
190+ double theta_0 = 2. * M_PI * (s->cutoff / inlink->sample_rate);
191+ double d_E;
192+
193+ s->filter_count = s->order / 2;
194+ d_E = (2. * tan(theta_0 / (2. * s->qfactor))) / sin(theta_0);
195+
196+ for (int b = 0; b < s->filter_count; b += 2) {
197+ double D = 2. * sin(((b + 1) * M_PI) / (2. * s->filter_count));
198+ double A = (1. + pow((d_E / 2.), 2)) / (D * d_E / 2.);
199+ double d = sqrt((d_E * D) / (A + sqrt(A * A - 1.)));
200+ double B = D * (d_E / 2.) / d;
201+ double W = B + sqrt(B * B - 1.);
202+
203+ for (int j = 0; j < 2; j++) {
204+ BiquadCoeffs *coeffs = &s->coeffs[b + j];
205+
206+ if (j == 1)
207+ theta = 2. * atan(tan(theta_0 / 2.) / W);
208+ else
209+ theta = 2. * atan(W * tan(theta_0 / 2.));
210+
211+ beta = 0.5 * ((1. - (d / 2.) * sin(theta)) / (1. + (d / 2.) * sin(theta)));
212+ gamma = (0.5 + beta) * cos(theta);
213+ alpha = 0.5 * (0.5 + beta) * ((1. - cos(theta)) / (1. - cos(theta_0)));
214+
215+ coeffs->a1 = 2. * gamma;
216+ coeffs->a2 = -2. * beta;
217+ coeffs->b0 = 2. * alpha;
218+ coeffs->b1 = -4. * alpha * cos(theta_0);
219+ coeffs->b2 = 2. * alpha;
220+ }
221+ }
145222 }
146223
147224 return 0;
@@ -327,3 +404,51 @@ AVFilter ff_af_asubcut = { · 179 unmodified lines
327404 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
328405 AVFILTER_FLAG_SLICE_THREADS,
329406 };
407+
408+static const AVOption asuperpass_options[] = {
409+ { "centerf","set center frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 999999, FLAGS },
410+ { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=4}, 4, 20, FLAGS },
411+ { "qfactor","set Q-factor", OFFSET(qfactor),AV_OPT_TYPE_DOUBLE, {.dbl=1.},0.01, 100., FLAGS },
412+ { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 2., FLAGS },
413+ { NULL }
414+};
415+
416+AVFILTER_DEFINE_CLASS(asuperpass);
417+
418+AVFilter ff_af_asuperpass = {
419+ .name = "asuperpass",
420+ .description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-pass filter."),
421+ .query_formats = query_formats,
422+ .priv_size = sizeof(ASuperCutContext),
423+ .priv_class = &asuperpass_class,
424+ .uninit = uninit,
425+ .inputs = inputs,
426+ .outputs = outputs,
427+ .process_command = process_command,
428+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
429+ AVFILTER_FLAG_SLICE_THREADS,
430+};
431+
432+static const AVOption asuperstop_options[] = {
433+ { "centerf","set center frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 999999, FLAGS },
434+ { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=4}, 4, 20, FLAGS },
435+ { "qfactor","set Q-factor", OFFSET(qfactor),AV_OPT_TYPE_DOUBLE, {.dbl=1.},0.01, 100., FLAGS },
436+ { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 2., FLAGS },
437+ { NULL }
438+};
439+
440+AVFILTER_DEFINE_CLASS(asuperstop);
441+
442+AVFilter ff_af_asuperstop = {
443+ .name = "asuperstop",
444+ .description = NULL_IF_CONFIG_SMALL("Apply high order Butterworth band-stop filter."),
445+ .query_formats = query_formats,
446+ .priv_size = sizeof(ASuperCutContext),
447+ .priv_class = &asuperstop_class,
448+ .uninit = uninit,
449+ .inputs = inputs,
450+ .outputs = outputs,
451+ .process_command = process_command,
452+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
453+ AVFILTER_FLAG_SLICE_THREADS,
454+};
libavfilter/allfilters.c+2−0
@@ -85,6 +85,8 @@ extern AVFilter ff_af_astreamselect; · 84 unmodified lines
8585 extern AVFilter ff_af_asubboost;
8686 extern AVFilter ff_af_asubcut;
8787 extern AVFilter ff_af_asupercut;
88+extern AVFilter ff_af_asuperpass;
89+extern AVFilter ff_af_asuperstop;
8890 extern AVFilter ff_af_atempo;
8991 extern AVFilter ff_af_atrim;
9092 extern AVFilter ff_af_axcorrelate;
libavfilter/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
3030 #include "libavutil/version.h"
3131
3232 #define LIBAVFILTER_VERSION_MAJOR 7
33−#define LIBAVFILTER_VERSION_MINOR 92
33+#define LIBAVFILTER_VERSION_MINOR 93
3434 #define LIBAVFILTER_VERSION_MICRO 100
3535
3636

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t