FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_adynamicequalizer.c
Date: 2024-04-17 23:49:55
Exec Total Coverage
Lines: 0 60 0.0%
Functions: 0 5 0.0%
Branches: 0 25 0.0%

Line Branch Exec Source
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 #include <float.h>
20
21 #include "libavutil/ffmath.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "audio.h"
26 #include "formats.h"
27
28 enum DetectionModes {
29 DET_UNSET = 0,
30 DET_DISABLED,
31 DET_OFF,
32 DET_ON,
33 DET_ADAPTIVE,
34 NB_DMODES,
35 };
36
37 enum FilterModes {
38 LISTEN = -1,
39 CUT_BELOW,
40 CUT_ABOVE,
41 BOOST_BELOW,
42 BOOST_ABOVE,
43 NB_FMODES,
44 };
45
46 typedef struct ChannelContext {
47 double fa_double[3], fm_double[3];
48 double dstate_double[2];
49 double fstate_double[2];
50 double tstate_double[2];
51 double lin_gain_double;
52 double detect_double;
53 double threshold_log_double;
54 double new_threshold_log_double;
55 double log_sum_double;
56 double sum_double;
57 float fa_float[3], fm_float[3];
58 float dstate_float[2];
59 float fstate_float[2];
60 float tstate_float[2];
61 float lin_gain_float;
62 float detect_float;
63 float threshold_log_float;
64 float new_threshold_log_float;
65 float log_sum_float;
66 float sum_float;
67 void *dqueue;
68 void *queue;
69 int position;
70 int size;
71 int front;
72 int back;
73 int detection;
74 int init;
75 } ChannelContext;
76
77 typedef struct AudioDynamicEqualizerContext {
78 const AVClass *class;
79
80 double threshold;
81 double threshold_log;
82 double dfrequency;
83 double dqfactor;
84 double tfrequency;
85 double tqfactor;
86 double ratio;
87 double range;
88 double makeup;
89 double dattack;
90 double drelease;
91 double dattack_coef;
92 double drelease_coef;
93 double gattack_coef;
94 double grelease_coef;
95 int mode;
96 int detection;
97 int tftype;
98 int dftype;
99 int precision;
100 int format;
101 int nb_channels;
102
103 int (*filter_prepare)(AVFilterContext *ctx);
104 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
105
106 double da_double[3], dm_double[3];
107 float da_float[3], dm_float[3];
108
109 ChannelContext *cc;
110 } AudioDynamicEqualizerContext;
111
112 static int query_formats(AVFilterContext *ctx)
113 {
114 AudioDynamicEqualizerContext *s = ctx->priv;
115 static const enum AVSampleFormat sample_fmts[3][3] = {
116 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
117 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
118 { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
119 };
120 int ret;
121
122 if ((ret = ff_set_common_all_channel_counts(ctx)) < 0)
123 return ret;
124
125 if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts[s->precision])) < 0)
126 return ret;
127
128 return ff_set_common_all_samplerates(ctx);
129 }
130
131 static double get_coef(double x, double sr)
132 {
133 return 1.0 - exp(-1.0 / (0.001 * x * sr));
134 }
135
136 typedef struct ThreadData {
137 AVFrame *in, *out;
138 } ThreadData;
139
140 #define DEPTH 32
141 #include "adynamicequalizer_template.c"
142
143 #undef DEPTH
144 #define DEPTH 64
145 #include "adynamicequalizer_template.c"
146
147 static int config_input(AVFilterLink *inlink)
148 {
149 AVFilterContext *ctx = inlink->dst;
150 AudioDynamicEqualizerContext *s = ctx->priv;
151
152 s->format = inlink->format;
153 s->cc = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->cc));
154 if (!s->cc)
155 return AVERROR(ENOMEM);
156 s->nb_channels = inlink->ch_layout.nb_channels;
157
158 switch (s->format) {
159 case AV_SAMPLE_FMT_DBLP:
160 s->filter_prepare = filter_prepare_double;
161 s->filter_channels = filter_channels_double;
162 break;
163 case AV_SAMPLE_FMT_FLTP:
164 s->filter_prepare = filter_prepare_float;
165 s->filter_channels = filter_channels_float;
166 break;
167 }
168
169 for (int ch = 0; ch < s->nb_channels; ch++) {
170 ChannelContext *cc = &s->cc[ch];
171 cc->queue = av_calloc(inlink->sample_rate, sizeof(double));
172 cc->dqueue = av_calloc(inlink->sample_rate, sizeof(double));
173 if (!cc->queue || !cc->dqueue)
174 return AVERROR(ENOMEM);
175 }
176
177 return 0;
178 }
179
180 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
181 {
182 AVFilterContext *ctx = inlink->dst;
183 AVFilterLink *outlink = ctx->outputs[0];
184 AudioDynamicEqualizerContext *s = ctx->priv;
185 ThreadData td;
186 AVFrame *out;
187
188 if (av_frame_is_writable(in)) {
189 out = in;
190 } else {
191 out = ff_get_audio_buffer(outlink, in->nb_samples);
192 if (!out) {
193 av_frame_free(&in);
194 return AVERROR(ENOMEM);
195 }
196 av_frame_copy_props(out, in);
197 }
198
199 td.in = in;
200 td.out = out;
201 s->filter_prepare(ctx);
202 ff_filter_execute(ctx, s->filter_channels, &td, NULL,
203 FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
204
205 if (out != in)
206 av_frame_free(&in);
207 return ff_filter_frame(outlink, out);
208 }
209
210 static av_cold void uninit(AVFilterContext *ctx)
211 {
212 AudioDynamicEqualizerContext *s = ctx->priv;
213
214 for (int ch = 0; ch < s->nb_channels; ch++) {
215 ChannelContext *cc = &s->cc[ch];
216 av_freep(&cc->queue);
217 av_freep(&cc->dqueue);
218 }
219 av_freep(&s->cc);
220 }
221
222 #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
223 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
224 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
225
226 static const AVOption adynamicequalizer_options[] = {
227 { "threshold", "set detection threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 100, FLAGS },
228 { "dfrequency", "set detection frequency", OFFSET(dfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
229 { "dqfactor", "set detection Q factor", OFFSET(dqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
230 { "tfrequency", "set target frequency", OFFSET(tfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
231 { "tqfactor", "set target Q factor", OFFSET(tqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
232 { "attack", "set detection attack duration", OFFSET(dattack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, FLAGS },
233 { "release","set detection release duration",OFFSET(drelease), AV_OPT_TYPE_DOUBLE, {.dbl=200}, 0.01, 2000, FLAGS },
234 { "ratio", "set ratio factor", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 30, FLAGS },
235 { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1000, FLAGS },
236 { "range", "set max gain", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 2000, FLAGS },
237 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, LISTEN,NB_FMODES-1,FLAGS, .unit = "mode" },
238 { "listen", 0, 0, AV_OPT_TYPE_CONST, {.i64=LISTEN}, 0, 0, FLAGS, .unit = "mode" },
239 { "cutbelow", 0, 0, AV_OPT_TYPE_CONST, {.i64=CUT_BELOW},0, 0, FLAGS, .unit = "mode" },
240 { "cutabove", 0, 0, AV_OPT_TYPE_CONST, {.i64=CUT_ABOVE},0, 0, FLAGS, .unit = "mode" },
241 { "boostbelow", 0, 0, AV_OPT_TYPE_CONST, {.i64=BOOST_BELOW},0, 0, FLAGS, .unit = "mode" },
242 { "boostabove", 0, 0, AV_OPT_TYPE_CONST, {.i64=BOOST_ABOVE},0, 0, FLAGS, .unit = "mode" },
243 { "dftype", "set detection filter type",OFFSET(dftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, .unit = "dftype" },
244 { "bandpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "dftype" },
245 { "lowpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "dftype" },
246 { "highpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "dftype" },
247 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, .unit = "dftype" },
248 { "tftype", "set target filter type", OFFSET(tftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, .unit = "tftype" },
249 { "bell", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "tftype" },
250 { "lowshelf", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "tftype" },
251 { "highshelf",0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "tftype" },
252 { "auto", "set auto threshold", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=DET_OFF},DET_DISABLED,NB_DMODES-1,FLAGS, .unit = "auto" },
253 { "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_DISABLED}, 0, 0, FLAGS, .unit = "auto" },
254 { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_OFF}, 0, 0, FLAGS, .unit = "auto" },
255 { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_ON}, 0, 0, FLAGS, .unit = "auto" },
256 { "adaptive", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_ADAPTIVE}, 0, 0, FLAGS, .unit = "auto" },
257 { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, .unit = "precision" },
258 { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "precision" },
259 { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "precision" },
260 { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "precision" },
261 { NULL }
262 };
263
264 AVFILTER_DEFINE_CLASS(adynamicequalizer);
265
266 static const AVFilterPad inputs[] = {
267 {
268 .name = "default",
269 .type = AVMEDIA_TYPE_AUDIO,
270 .filter_frame = filter_frame,
271 .config_props = config_input,
272 },
273 };
274
275 const AVFilter ff_af_adynamicequalizer = {
276 .name = "adynamicequalizer",
277 .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Equalization of input audio."),
278 .priv_size = sizeof(AudioDynamicEqualizerContext),
279 .priv_class = &adynamicequalizer_class,
280 .uninit = uninit,
281 FILTER_INPUTS(inputs),
282 FILTER_OUTPUTS(ff_audio_default_filterpad),
283 FILTER_QUERY_FUNC(query_formats),
284 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
285 AVFILTER_FLAG_SLICE_THREADS,
286 .process_command = ff_filter_process_command,
287 };
288