History

avfilter/af_adynamicequalizer: add precision option

Paul B Mahol committed 3 years ago

Commit f247a3d1 parent 41dd50a3 files · +342 −212Browse files

Showing the changes to libavfilter (2 of 3 files). Show all

Unified · Split

2 files
  1. libavfilter/adynamicequalizer_template.c +270 −0
  2. libavfilter/af_adynamicequalizer.c +58 −212
libavfilter/adynamicequalizer_template.c+270−0
@@ -0,0 +1,270 @@
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#undef ftype
20#undef SQRT
21#undef TAN
22#undef ONE
23#undef TWO
24#undef ZERO
25#undef FMAX
26#undef FMIN
27#undef CLIP
28#undef SAMPLE_FORMAT
29#undef FABS
30#if DEPTH == 32
31#define SAMPLE_FORMAT float
32#define SQRT sqrtf
33#define TAN tanf
34#define ONE 1.f
35#define TWO 2.f
36#define ZERO 0.f
37#define FMIN fminf
38#define FMAX fmaxf
39#define CLIP av_clipf
40#define FABS fabsf
41#define ftype float
42#else
43#define SAMPLE_FORMAT double
44#define SQRT sqrt
45#define TAN tan
46#define ONE 1.0
47#define TWO 2.0
48#define ZERO 0.0
49#define FMIN fmin
50#define FMAX fmax
51#define CLIP av_clipd
52#define FABS fabs
53#define ftype double
54#endif
55
56#define fn3(a,b) a##_##b
57#define fn2(a,b) fn3(a,b)
58#define fn(a) fn2(a, SAMPLE_FORMAT)
59
60static ftype fn(get_svf)(ftype in, const ftype *m, const ftype *a, ftype *b)
61{
62 const ftype v0 = in;
63 const ftype v3 = v0 - b[1];
64 const ftype v1 = a[0] * b[0] + a[1] * v3;
65 const ftype v2 = b[1] + a[1] * b[0] + a[2] * v3;
66
67 b[0] = TWO * v1 - b[0];
68 b[1] = TWO * v2 - b[1];
69
70 return m[0] * v0 + m[1] * v1 + m[2] * v2;
71}
72
73static int fn(filter_prepare)(AVFilterContext *ctx)
74{
75 AudioDynamicEqualizerContext *s = ctx->priv;
76 const ftype sample_rate = ctx->inputs[0]->sample_rate;
77 const ftype dfrequency = FMIN(s->dfrequency, sample_rate * 0.5);
78 const ftype dg = TAN(M_PI * dfrequency / sample_rate);
79 const ftype dqfactor = s->dqfactor;
80 const int dftype = s->dftype;
81 ftype *da = fn(s->da);
82 ftype *dm = fn(s->dm);
83 ftype k;
84
85 s->attack_coef = get_coef(s->attack, sample_rate);
86 s->release_coef = get_coef(s->release, sample_rate);
87
88 switch (dftype) {
89 case 0:
90 k = ONE / dqfactor;
91
92 da[0] = ONE / (ONE + dg * (dg + k));
93 da[1] = dg * da[0];
94 da[2] = dg * da[1];
95
96 dm[0] = ZERO;
97 dm[1] = k;
98 dm[2] = ZERO;
99 break;
100 case 1:
101 k = ONE / dqfactor;
102
103 da[0] = ONE / (ONE + dg * (dg + k));
104 da[1] = dg * da[0];
105 da[2] = dg * da[1];
106
107 dm[0] = ZERO;
108 dm[1] = ZERO;
109 dm[2] = ONE;
110 break;
111 case 2:
112 k = ONE / dqfactor;
113
114 da[0] = ONE / (ONE + dg * (dg + k));
115 da[1] = dg * da[0];
116 da[2] = dg * da[1];
117
118 dm[0] = ZERO;
119 dm[1] = -k;
120 dm[2] = -ONE;
121 break;
122 case 3:
123 k = ONE / dqfactor;
124
125 da[0] = ONE / (ONE + dg * (dg + k));
126 da[1] = dg * da[0];
127 da[2] = dg * da[1];
128
129 dm[0] = ZERO;
130 dm[1] = -k;
131 dm[2] = -TWO;
132 break;
133 }
134
135 return 0;
136}
137
138static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
139{
140 AudioDynamicEqualizerContext *s = ctx->priv;
141 ThreadData *td = arg;
142 AVFrame *in = td->in;
143 AVFrame *out = td->out;
144 const ftype sample_rate = in->sample_rate;
145 const ftype makeup = s->makeup;
146 const ftype ratio = s->ratio;
147 const ftype range = s->range;
148 const ftype tfrequency = FMIN(s->tfrequency, sample_rate * 0.5);
149 const ftype release = s->release_coef;
150 const ftype irelease = ONE - release;
151 const ftype attack = s->attack_coef;
152 const ftype iattack = ONE - attack;
153 const ftype tqfactor = s->tqfactor;
154 const ftype itqfactor = ONE / tqfactor;
155 const ftype fg = TAN(M_PI * tfrequency / sample_rate);
156 const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
157 const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
158 const int detection = s->detection;
159 const int direction = s->direction;
160 const int tftype = s->tftype;
161 const int mode = s->mode;
162 const ftype *da = fn(s->da);
163 const ftype *dm = fn(s->dm);
164
165 for (int ch = start; ch < end; ch++) {
166 const ftype *src = (const ftype *)in->extended_data[ch];
167 ftype *dst = (ftype *)out->extended_data[ch];
168 ftype *state = (ftype *)s->state->extended_data[ch];
169 const ftype threshold = detection == 0 ? state[5] : s->threshold;
170
171 if (detection < 0)
172 state[5] = threshold;
173
174 for (int n = 0; n < out->nb_samples; n++) {
175 ftype detect, gain, v, listen;
176 ftype fa[3], fm[3];
177 ftype k, g;
178
179 detect = listen = fn(get_svf)(src[n], dm, da, state);
180 detect = FABS(detect);
181
182 if (detection > 0)
183 state[5] = FMAX(state[5], detect);
184
185 if (direction == 0) {
186 if (detect < threshold) {
187 if (mode == 0)
188 detect = ONE / CLIP(ONE + makeup + (threshold - detect) * ratio, ONE, range);
189 else
190 detect = CLIP(ONE + makeup + (threshold - detect) * ratio, ONE, range);
191 } else {
192 detect = ONE;
193 }
194 } else {
195 if (detect > threshold) {
196 if (mode == 0)
197 detect = ONE / CLIP(ONE + makeup + (detect - threshold) * ratio, ONE, range);
198 else
199 detect = CLIP(ONE + makeup + (detect - threshold) * ratio, ONE, range);
200 } else {
201 detect = ONE;
202 }
203 }
204
205 if (direction == 0) {
206 if (detect > state[4]) {
207 detect = iattack * detect + attack * state[4];
208 } else {
209 detect = irelease * detect + release * state[4];
210 }
211 } else {
212 if (detect < state[4]) {
213 detect = iattack * detect + attack * state[4];
214 } else {
215 detect = irelease * detect + release * state[4];
216 }
217 }
218
219 if (state[4] != detect || n == 0) {
220 state[4] = gain = detect;
221
222 switch (tftype) {
223 case 0:
224 k = ONE / (tqfactor * gain);
225
226 fa[0] = ONE / (ONE + fg * (fg + k));
227 fa[1] = fg * fa[0];
228 fa[2] = fg * fa[1];
229
230 fm[0] = ONE;
231 fm[1] = k * (gain * gain - ONE);
232 fm[2] = ZERO;
233 break;
234 case 1:
235 k = itqfactor;
236 g = fg / SQRT(gain);
237
238 fa[0] = ONE / (ONE + g * (g + k));
239 fa[1] = g * fa[0];
240 fa[2] = g * fa[1];
241
242 fm[0] = ONE;
243 fm[1] = k * (gain - ONE);
244 fm[2] = gain * gain - ONE;
245 break;
246 case 2:
247 k = itqfactor;
248 g = fg / SQRT(gain);
249
250 fa[0] = ONE / (ONE + g * (g + k));
251 fa[1] = g * fa[0];
252 fa[2] = g * fa[1];
253
254 fm[0] = gain * gain;
255 fm[1] = k * (ONE - gain) * gain;
256 fm[2] = ONE - gain * gain;
257 break;
258 }
259 }
260
261 v = fn(get_svf)(src[n], fm, fa, &state[2]);
262 v = mode == -1 ? listen : v;
263 dst[n] = ctx->is_disabled ? src[n] : v;
264 }
265 }
266
267 return 0;
268}
269
270
libavfilter/af_adynamicequalizer.c+58−212
@@ -43,242 +43,82 @@ typedef struct AudioDynamicEqualizerContext { · 42 unmodified lines
43 int detection;43 int detection;
44 int tftype;44 int tftype;
45 int dftype;45 int dftype;
46 int precision;
47 int format;
48
49 int (*filter_prepare)(AVFilterContext *ctx);
50 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
51
52 double da_double[3], dm_double[3];
53 float da_float[3], dm_float[3];
46 54
47 double da[3], dm[3];
48 AVFrame *state;55 AVFrame *state;
49} AudioDynamicEqualizerContext;56} AudioDynamicEqualizerContext;
50 57
51static int config_input(AVFilterLink *inlink)58static int query_formats(AVFilterContext *ctx)
52{59{
53 AVFilterContext *ctx = inlink->dst;
54 AudioDynamicEqualizerContext *s = ctx->priv;60 AudioDynamicEqualizerContext *s = ctx->priv;
61 static const enum AVSampleFormat sample_fmts[3][3] = {
62 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
63 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
64 { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
65 };
66 int ret;
55 67
56 s->state = ff_get_audio_buffer(inlink, 8);68 if ((ret = ff_set_common_all_channel_counts(ctx)) < 0)
57 if (!s->state)69 return ret;
58 return AVERROR(ENOMEM);
59
60 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
61 double *state = (double *)s->state->extended_data[ch];
62 70
63 state[4] = 1.;71 if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts[s->precision])) < 0)
64 }72 return ret;
65 73
66 return 0;74 return ff_set_common_all_samplerates(ctx);
67}75}
68 76
69static double get_svf(double in, const double *m, const double *a, double *b)77static double get_coef(double x, double sr)
70{78{
71 const double v0 = in;79 return exp(-1000. / (x * sr));
72 const double v3 = v0 - b[1];
73 const double v1 = a[0] * b[0] + a[1] * v3;
74 const double v2 = b[1] + a[1] * b[0] + a[2] * v3;
75
76 b[0] = 2. * v1 - b[0];
77 b[1] = 2. * v2 - b[1];
78
79 return m[0] * v0 + m[1] * v1 + m[2] * v2;
80}80}
81 81
82typedef struct ThreadData {82typedef struct ThreadData {
83 AVFrame *in, *out;83 AVFrame *in, *out;
84} ThreadData;84} ThreadData;
85 85
86static double get_coef(double x, double sr)86#define DEPTH 32
87{87#include "adynamicequalizer_template.c"
88 return exp(-1000. / (x * sr));
89}
90 88
91static int filter_prepare(AVFilterContext *ctx)89#undef DEPTH
90#define DEPTH 64
91#include "adynamicequalizer_template.c"
92
93static int config_input(AVFilterLink *inlink)
92{94{
95 AVFilterContext *ctx = inlink->dst;
93 AudioDynamicEqualizerContext *s = ctx->priv;96 AudioDynamicEqualizerContext *s = ctx->priv;
94 const double sample_rate = ctx->inputs[0]->sample_rate;
95 const double dfrequency = fmin(s->dfrequency, sample_rate * 0.5);
96 const double dg = tan(M_PI * dfrequency / sample_rate);
97 const double dqfactor = s->dqfactor;
98 const int dftype = s->dftype;
99 double *da = s->da;
100 double *dm = s->dm;
101 double k;
102
103 s->attack_coef = get_coef(s->attack, sample_rate);
104 s->release_coef = get_coef(s->release, sample_rate);
105
106 switch (dftype) {
107 case 0:
108 k = 1. / dqfactor;
109
110 da[0] = 1. / (1. + dg * (dg + k));
111 da[1] = dg * da[0];
112 da[2] = dg * da[1];
113
114 dm[0] = 0.;
115 dm[1] = k;
116 dm[2] = 0.;
117 break;
118 case 1:
119 k = 1. / dqfactor;
120
121 da[0] = 1. / (1. + dg * (dg + k));
122 da[1] = dg * da[0];
123 da[2] = dg * da[1];
124
125 dm[0] = 0.;
126 dm[1] = 0.;
127 dm[2] = 1.;
128 break;
129 case 2:
130 k = 1. / dqfactor;
131 97
132 da[0] = 1. / (1. + dg * (dg + k));98 s->format = inlink->format;
133 da[1] = dg * da[0];99 s->state = ff_get_audio_buffer(inlink, 8);
134 da[2] = dg * da[1];100 if (!s->state)
135 101 return AVERROR(ENOMEM);
136 dm[0] = 0.;
137 dm[1] = -k;
138 dm[2] = -1.;
139 break;
140 case 3:
141 k = 1. / dqfactor;
142 102
143 da[0] = 1. / (1. + dg * (dg + k));103 switch (s->format) {
144 da[1] = dg * da[0];104 case AV_SAMPLE_FMT_DBLP:
145 da[2] = dg * da[1];105 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
106 double *state = (double *)s->state->extended_data[ch];
146 107
147 dm[0] = 0.;108 state[4] = 1.;
148 dm[1] = -k;109 }
149 dm[2] = -2.;110 s->filter_prepare = filter_prepare_double;
111 s->filter_channels = filter_channels_double;
150 break;112 break;
151 }113 case AV_SAMPLE_FMT_FLTP:
152 114 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
153 return 0;115 float *state = (float *)s->state->extended_data[ch];
154}
155 116
156static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)117 state[4] = 1.;
157{
158 AudioDynamicEqualizerContext *s = ctx->priv;
159 ThreadData *td = arg;
160 AVFrame *in = td->in;
161 AVFrame *out = td->out;
162 const double sample_rate = in->sample_rate;
163 const double makeup = s->makeup;
164 const double ratio = s->ratio;
165 const double range = s->range;
166 const double tfrequency = fmin(s->tfrequency, sample_rate * 0.5);
167 const double release = s->release_coef;
168 const double irelease = 1. - release;
169 const double attack = s->attack_coef;
170 const double iattack = 1. - attack;
171 const double tqfactor = s->tqfactor;
172 const double fg = tan(M_PI * tfrequency / sample_rate);
173 const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
174 const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
175 const int detection = s->detection;
176 const int direction = s->direction;
177 const int tftype = s->tftype;
178 const int mode = s->mode;
179 const double *da = s->da;
180 const double *dm = s->dm;
181
182 for (int ch = start; ch < end; ch++) {
183 const double *src = (const double *)in->extended_data[ch];
184 double *dst = (double *)out->extended_data[ch];
185 double *state = (double *)s->state->extended_data[ch];
186 const double threshold = detection == 0 ? state[5] : s->threshold;
187
188 if (detection < 0)
189 state[5] = threshold;
190
191 for (int n = 0; n < out->nb_samples; n++) {
192 double detect, gain, v, listen;
193 double fa[3], fm[3];
194 double k, g;
195
196 detect = listen = get_svf(src[n], dm, da, state);
197 detect = fabs(detect);
198
199 if (detection > 0)
200 state[5] = fmax(state[5], detect);
201
202 if (direction == 0) {
203 if (detect < threshold) {
204 if (mode == 0)
205 detect = 1. / av_clipd(1. + makeup + (threshold - detect) * ratio, 1., range);
206 else
207 detect = av_clipd(1. + makeup + (threshold - detect) * ratio, 1., range);
208 } else {
209 detect = 1.;
210 }
211 } else {
212 if (detect > threshold) {
213 if (mode == 0)
214 detect = 1. / av_clipd(1. + makeup + (detect - threshold) * ratio, 1., range);
215 else
216 detect = av_clipd(1. + makeup + (detect - threshold) * ratio, 1., range);
217 } else {
218 detect = 1.;
219 }
220 }
221
222 if (direction == 0) {
223 if (detect > state[4]) {
224 detect = iattack * detect + attack * state[4];
225 } else {
226 detect = irelease * detect + release * state[4];
227 }
228 } else {
229 if (detect < state[4]) {
230 detect = iattack * detect + attack * state[4];
231 } else {
232 detect = irelease * detect + release * state[4];
233 }
234 }
235
236 if (state[4] != detect || n == 0) {
237 state[4] = gain = detect;
238
239 switch (tftype) {
240 case 0:
241 k = 1. / (tqfactor * gain);
242
243 fa[0] = 1. / (1. + fg * (fg + k));
244 fa[1] = fg * fa[0];
245 fa[2] = fg * fa[1];
246
247 fm[0] = 1.;
248 fm[1] = k * (gain * gain - 1.);
249 fm[2] = 0.;
250 break;
251 case 1:
252 k = 1. / tqfactor;
253 g = fg / sqrt(gain);
254
255 fa[0] = 1. / (1. + g * (g + k));
256 fa[1] = g * fa[0];
257 fa[2] = g * fa[1];
258
259 fm[0] = 1.;
260 fm[1] = k * (gain - 1.);
261 fm[2] = gain * gain - 1.;
262 break;
263 case 2:
264 k = 1. / tqfactor;
265 g = fg / sqrt(gain);
266
267 fa[0] = 1. / (1. + g * (g + k));
268 fa[1] = g * fa[0];
269 fa[2] = g * fa[1];
270
271 fm[0] = gain * gain;
272 fm[1] = k * (1. - gain) * gain;
273 fm[2] = 1. - gain * gain;
274 break;
275 }
276 }
277
278 v = get_svf(src[n], fm, fa, &state[2]);
279 v = mode == -1 ? listen : v;
280 dst[n] = ctx->is_disabled ? src[n] : v;
281 }118 }
119 s->filter_prepare = filter_prepare_float;
120 s->filter_channels = filter_channels_float;
121 break;
282 }122 }
283 123
284 return 0;124 return 0;
@@ -288,6 +128,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 3 unmodified lines
288{128{
289 AVFilterContext *ctx = inlink->dst;129 AVFilterContext *ctx = inlink->dst;
290 AVFilterLink *outlink = ctx->outputs[0];130 AVFilterLink *outlink = ctx->outputs[0];
131 AudioDynamicEqualizerContext *s = ctx->priv;
291 ThreadData td;132 ThreadData td;
292 AVFrame *out;133 AVFrame *out;
293 134
@@ -304,8 +145,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 10 unmodified lines
304 145
305 td.in = in;146 td.in = in;
306 td.out = out;147 td.out = out;
307 filter_prepare(ctx);148 s->filter_prepare(ctx);
308 ff_filter_execute(ctx, filter_channels, &td, NULL,149 ff_filter_execute(ctx, s->filter_channels, &td, NULL,
309 FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));150 FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
310 151
311 if (out != in)152 if (out != in)
@@ -321,6 +162,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 9 unmodified lines
321}162}
322 163
323#define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)164#define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
165#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
324#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM166#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
325 167
326static const AVOption adynamicequalizer_options[] = {168static const AVOption adynamicequalizer_options[] = {
@@ -354,6 +196,10 @@ static const AVOption adynamicequalizer_options[] = { · 27 unmodified lines
354 { "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "auto" },196 { "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "auto" },
355 { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "auto" },197 { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "auto" },
356 { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "auto" },198 { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "auto" },
199 { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, "precision" },
200 { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
201 { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
202 { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
357 { NULL }203 { NULL }
358};204};
359 205
@@ -383,7 +229,7 @@ const AVFilter ff_af_adynamicequalizer = { · 23 unmodified lines
383 .uninit = uninit,229 .uninit = uninit,
384 FILTER_INPUTS(inputs),230 FILTER_INPUTS(inputs),
385 FILTER_OUTPUTS(outputs),231 FILTER_OUTPUTS(outputs),
386 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),232 FILTER_QUERY_FUNC(query_formats),
387 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |233 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
388 AVFILTER_FLAG_SLICE_THREADS,234 AVFILTER_FLAG_SLICE_THREADS,
389 .process_command = ff_filter_process_command,235 .process_command = ff_filter_process_command,

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