FFmpeg
Loading...
Searching...
No Matches
af_adynamicequalizer.c
Go to the documentation of this file.
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 "filters.h"
27#include "formats.h"
28
37
46
47typedef struct ChannelContext {
48 double fa_double[3], fm_double[3];
49 double dstate_double[2];
50 double fstate_double[2];
51 double tstate_double[2];
57 double sum_double;
58 float fa_float[3], fm_float[3];
59 float dstate_float[2];
60 float fstate_float[2];
61 float tstate_float[2];
67 float sum_float;
68 void *dqueue;
69 void *queue;
71 int size;
72 int front;
73 int back;
75 int init;
77
79 const AVClass *class;
80
81 double threshold;
83 double dfrequency;
84 double dqfactor;
85 double tfrequency;
86 double tqfactor;
87 double ratio;
88 double range;
89 double makeup;
90 double dattack;
91 double drelease;
96 int mode;
98 int tftype;
99 int dftype;
103
105 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
106
107 double da_double[3], dm_double[3];
108 float da_float[3], dm_float[3];
109
112
114 AVFilterFormatsConfig **cfg_in,
115 AVFilterFormatsConfig **cfg_out)
116{
117 const AudioDynamicEqualizerContext *s = ctx->priv;
118 static const enum AVSampleFormat sample_fmts[3][3] = {
122 };
123 int ret;
124
125 if ((ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out,
126 sample_fmts[s->precision])) < 0)
127 return ret;
128
129 return 0;
130}
131
132static double get_coef(double x, double sr)
133{
134 return 1.0 - exp(-1.0 / (0.001 * x * sr));
135}
136
137typedef struct ThreadData {
138 AVFrame *in, *out;
139} ThreadData;
140
141#define DEPTH 32
143
144#undef DEPTH
145#define DEPTH 64
147
148static int config_input(AVFilterLink *inlink)
149{
150 AVFilterContext *ctx = inlink->dst;
152
153 s->format = inlink->format;
154 s->cc = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->cc));
155 if (!s->cc)
156 return AVERROR(ENOMEM);
157 s->nb_channels = inlink->ch_layout.nb_channels;
158
159 switch (s->format) {
161 s->filter_prepare = filter_prepare_double;
162 s->filter_channels = filter_channels_double;
163 break;
165 s->filter_prepare = filter_prepare_float;
166 s->filter_channels = filter_channels_float;
167 break;
168 }
169
170 for (int ch = 0; ch < s->nb_channels; ch++) {
171 ChannelContext *cc = &s->cc[ch];
172 cc->queue = av_calloc(inlink->sample_rate, sizeof(double));
173 cc->dqueue = av_calloc(inlink->sample_rate, sizeof(double));
174 if (!cc->queue || !cc->dqueue)
175 return AVERROR(ENOMEM);
176 }
177
178 return 0;
179}
180
181static int filter_frame(AVFilterLink *inlink, AVFrame *in)
182{
183 AVFilterContext *ctx = inlink->dst;
184 AVFilterLink *outlink = ctx->outputs[0];
186 ThreadData td;
187 AVFrame *out;
188
189 if (av_frame_is_writable(in)) {
190 out = in;
191 } else {
192 out = ff_get_audio_buffer(outlink, in->nb_samples);
193 if (!out) {
194 av_frame_free(&in);
195 return AVERROR(ENOMEM);
196 }
198 }
199
200 td.in = in;
201 td.out = out;
202 s->filter_prepare(ctx);
203 ff_filter_execute(ctx, s->filter_channels, &td, NULL,
205
206 if (out != in)
207 av_frame_free(&in);
208 return ff_filter_frame(outlink, out);
209}
210
212{
214
215 for (int ch = 0; ch < s->nb_channels; ch++) {
216 ChannelContext *cc = &s->cc[ch];
217 av_freep(&cc->queue);
218 av_freep(&cc->dqueue);
219 }
220 av_freep(&s->cc);
221}
222
223#define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
224#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
225#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
226
228 { "threshold", "set detection threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 100, FLAGS },
229 { "dfrequency", "set detection frequency", OFFSET(dfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
230 { "dqfactor", "set detection Q factor", OFFSET(dqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
231 { "tfrequency", "set target frequency", OFFSET(tfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
232 { "tqfactor", "set target Q factor", OFFSET(tqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
233 { "attack", "set detection attack duration", OFFSET(dattack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, FLAGS },
234 { "release","set detection release duration",OFFSET(drelease), AV_OPT_TYPE_DOUBLE, {.dbl=200}, 0.01, 2000, FLAGS },
235 { "ratio", "set ratio factor", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 30, FLAGS },
236 { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1000, FLAGS },
237 { "range", "set max gain", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 2000, FLAGS },
238 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, LISTEN,NB_FMODES-1,FLAGS, .unit = "mode" },
239 { "listen", 0, 0, AV_OPT_TYPE_CONST, {.i64=LISTEN}, 0, 0, FLAGS, .unit = "mode" },
240 { "cutbelow", 0, 0, AV_OPT_TYPE_CONST, {.i64=CUT_BELOW},0, 0, FLAGS, .unit = "mode" },
241 { "cutabove", 0, 0, AV_OPT_TYPE_CONST, {.i64=CUT_ABOVE},0, 0, FLAGS, .unit = "mode" },
242 { "boostbelow", 0, 0, AV_OPT_TYPE_CONST, {.i64=BOOST_BELOW},0, 0, FLAGS, .unit = "mode" },
243 { "boostabove", 0, 0, AV_OPT_TYPE_CONST, {.i64=BOOST_ABOVE},0, 0, FLAGS, .unit = "mode" },
244 { "dftype", "set detection filter type",OFFSET(dftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, .unit = "dftype" },
245 { "bandpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "dftype" },
246 { "lowpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "dftype" },
247 { "highpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "dftype" },
248 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, .unit = "dftype" },
249 { "tftype", "set target filter type", OFFSET(tftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, .unit = "tftype" },
250 { "bell", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "tftype" },
251 { "lowshelf", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "tftype" },
252 { "highshelf",0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "tftype" },
253 { "auto", "set auto threshold", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=DET_OFF},DET_DISABLED,NB_DMODES-1,FLAGS, .unit = "auto" },
254 { "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_DISABLED}, 0, 0, FLAGS, .unit = "auto" },
255 { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_OFF}, 0, 0, FLAGS, .unit = "auto" },
256 { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_ON}, 0, 0, FLAGS, .unit = "auto" },
257 { "adaptive", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_ADAPTIVE}, 0, 0, FLAGS, .unit = "auto" },
258 { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, .unit = "precision" },
259 { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "precision" },
260 { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "precision" },
261 { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "precision" },
262 { NULL }
263};
264
265AVFILTER_DEFINE_CLASS(adynamicequalizer);
266
267static const AVFilterPad inputs[] = {
268 {
269 .name = "default",
270 .type = AVMEDIA_TYPE_AUDIO,
271 .filter_frame = filter_frame,
272 .config_props = config_input,
273 },
274};
275
277 .p.name = "adynamicequalizer",
278 .p.description = NULL_IF_CONFIG_SMALL("Apply Dynamic Equalization of input audio."),
279 .p.priv_class = &adynamicequalizer_class,
282 .priv_size = sizeof(AudioDynamicEqualizerContext),
283 .uninit = uninit,
287 .process_command = ff_filter_process_command,
288};
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
#define AF
static double get_coef(double x, double sr)
const FFFilter ff_af_adynamicequalizer
@ BOOST_BELOW
@ BOOST_ABOVE
static int config_input(AVFilterLink *inlink)
@ DET_ADAPTIVE
@ DET_DISABLED
static const AVOption adynamicequalizer_options[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static FILE * out
static AVFormatContext * ctx
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int8_t exp
Definition eval.c:76
internal math functions header
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
enum AVColorRange range
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
AVOption.
Definition opt.h:428
int(* filter_prepare)(AVFilterContext *ctx)
int(* filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
Definition swscale.c:71
#define av_freep(p)