FFmpeg
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/opt.h"
22 #include "avfilter.h"
23 #include "audio.h"
24 #include "formats.h"
25 #include "hermite.h"
26 
28  const AVClass *class;
29 
30  double threshold;
31  double dfrequency;
32  double dqfactor;
33  double tfrequency;
34  double tqfactor;
35  double ratio;
36  double range;
37  double makeup;
38  double knee;
39  double slew;
40  double attack;
41  double release;
42  double attack_coef;
43  double release_coef;
44  int mode;
45 
48 
50 {
51  AVFilterContext *ctx = inlink->dst;
53 
54  s->state = ff_get_audio_buffer(inlink, 8);
55  if (!s->state)
56  return AVERROR(ENOMEM);
57 
58  return 0;
59 }
60 
61 static double get_svf(double in, double *m, double *a, double *b)
62 {
63  const double v0 = in;
64  const double v3 = v0 - b[1];
65  const double v1 = a[0] * b[0] + a[1] * v3;
66  const double v2 = b[1] + a[1] * b[0] + a[2] * v3;
67 
68  b[0] = 2. * v1 - b[0];
69  b[1] = 2. * v2 - b[1];
70 
71  return m[0] * v0 + m[1] * v1 + m[2] * v2;
72 }
73 
74 static inline double from_dB(double x)
75 {
76  return exp(0.05 * x * M_LN10);
77 }
78 
79 static inline double to_dB(double x)
80 {
81  return 20. * log10(x);
82 }
83 
84 static inline double sqr(double x)
85 {
86  return x * x;
87 }
88 
89 static double get_gain(double in, double srate, double makeup,
90  double aattack, double iratio, double knee, double range,
91  double thresdb, double slewfactor, double *state,
92  double attack_coeff, double release_coeff, double nc)
93 {
94  double width = (6. * knee) + 0.01;
95  double cdb = 0.;
96  double Lgain = 1.;
97  double Lxg, Lxl, Lyg, Lyl, Ly1;
98  double checkwidth = 0.;
99  double slewwidth = 1.8;
100  int attslew = 0;
101 
102  Lyg = 0.;
103  Lxg = to_dB(fabs(in) + DBL_EPSILON);
104 
105  Lyg = Lxg + (iratio - 1.) * sqr(Lxg - thresdb + width * .5) / (2. * width);
106 
107  checkwidth = 2. * fabs(Lxg - thresdb);
108  if (2. * (Lxg - thresdb) < -width) {
109  Lyg = Lxg;
110  } else if (checkwidth <= width) {
111  Lyg = thresdb + (Lxg - thresdb) * iratio;
112  if (checkwidth <= slewwidth) {
113  if (Lyg >= state[2])
114  attslew = 1;
115  }
116  } else if (2. * (Lxg-thresdb) > width) {
117  Lyg = thresdb + (Lxg - thresdb) * iratio;
118  }
119 
120  attack_coeff = attslew ? aattack : attack_coeff;
121 
122  Lxl = Lxg - Lyg;
123 
124  Ly1 = fmaxf(Lxl, release_coeff * state[1] +(1. - release_coeff) * Lxl);
125  Lyl = attack_coeff * state[0] + (1. - attack_coeff) * Ly1;
126 
127  cdb = -Lyl;
128  Lgain = from_dB(nc * fmin(cdb - makeup, range));
129 
130  state[0] = Lyl;
131  state[1] = Ly1;
132  state[2] = Lyg;
133 
134  return Lgain;
135 }
136 
137 typedef struct ThreadData {
138  AVFrame *in, *out;
139 } ThreadData;
140 
141 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
142 {
144  ThreadData *td = arg;
145  AVFrame *in = td->in;
146  AVFrame *out = td->out;
147  const double sample_rate = in->sample_rate;
148  const double makeup = s->makeup;
149  const double iratio = 1. / s->ratio;
150  const double range = s->range;
151  const double dfrequency = fmin(s->dfrequency, sample_rate * 0.5);
152  const double tfrequency = fmin(s->tfrequency, sample_rate * 0.5);
153  const double threshold = log(s->threshold + DBL_EPSILON);
154  const double release = s->release_coef;
155  const double attack = s->attack_coef;
156  const double dqfactor = s->dqfactor;
157  const double tqfactor = s->tqfactor;
158  const double fg = tan(M_PI * tfrequency / sample_rate);
159  const double dg = tan(M_PI * dfrequency / sample_rate);
160  const int start = (in->channels * jobnr) / nb_jobs;
161  const int end = (in->channels * (jobnr+1)) / nb_jobs;
162  const int mode = s->mode;
163  const double knee = s->knee;
164  const double slew = s->slew;
165  const double aattack = exp(-1000. / ((s->attack + 2.0 * (slew - 1.)) * sample_rate));
166  const double nc = mode == 0 ? 1. : -1.;
167  double da[3], dm[3];
168 
169  {
170  double k = 1. / dqfactor;
171 
172  da[0] = 1. / (1. + dg * (dg + k));
173  da[1] = dg * da[0];
174  da[2] = dg * da[1];
175 
176  dm[0] = 0.;
177  dm[1] = 1.;
178  dm[2] = 0.;
179  }
180 
181  for (int ch = start; ch < end; ch++) {
182  const double *src = (const double *)in->extended_data[ch];
183  double *dst = (double *)out->extended_data[ch];
184  double *state = (double *)s->state->extended_data[ch];
185 
186  for (int n = 0; n < out->nb_samples; n++) {
187  double detect, gain, v, listen;
188  double fa[3], fm[3];
189 
190  detect = listen = get_svf(src[n], dm, da, state);
191  detect = fabs(detect);
192 
193  gain = get_gain(detect, sample_rate, makeup,
194  aattack, iratio, knee, range, threshold, slew,
195  &state[4], attack, release, nc);
196 
197  {
198  double k = 1. / (tqfactor * gain);
199 
200  fa[0] = 1. / (1. + fg * (fg + k));
201  fa[1] = fg * fa[0];
202  fa[2] = fg * fa[1];
203 
204  fm[0] = 1.;
205  fm[1] = k * (gain * gain - 1.);
206  fm[2] = 0.;
207  }
208 
209  v = get_svf(src[n], fm, fa, &state[2]);
210  v = mode == -1 ? listen : v;
211  dst[n] = ctx->is_disabled ? src[n] : v;
212  }
213  }
214 
215  return 0;
216 }
217 
218 static double get_coef(double x, double sr)
219 {
220  return exp(-1000. / (x * sr));
221 }
222 
224 {
225  AVFilterContext *ctx = inlink->dst;
226  AVFilterLink *outlink = ctx->outputs[0];
228  ThreadData td;
229  AVFrame *out;
230 
231  if (av_frame_is_writable(in)) {
232  out = in;
233  } else {
234  out = ff_get_audio_buffer(outlink, in->nb_samples);
235  if (!out) {
236  av_frame_free(&in);
237  return AVERROR(ENOMEM);
238  }
240  }
241 
242  s->attack_coef = get_coef(s->attack, in->sample_rate);
243  s->release_coef = get_coef(s->release, in->sample_rate);
244 
245  td.in = in;
246  td.out = out;
249 
250  if (out != in)
251  av_frame_free(&in);
252  return ff_filter_frame(outlink, out);
253 }
254 
256 {
258 
259  av_frame_free(&s->state);
260 }
261 
262 #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
263 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
264 
266  { "threshold", "set detection threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 100, FLAGS },
267  { "dfrequency", "set detection frequency", OFFSET(dfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
268  { "dqfactor", "set detection Q factor", OFFSET(dqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
269  { "tfrequency", "set target frequency", OFFSET(tfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
270  { "tqfactor", "set target Q factor", OFFSET(tqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
271  { "attack", "set attack duration", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 2000, FLAGS },
272  { "release", "set release duration", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=200}, 1, 2000, FLAGS },
273  { "knee", "set knee factor", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 8, FLAGS },
274  { "ratio", "set ratio factor", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 20, FLAGS },
275  { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 30, FLAGS },
276  { "range", "set max gain", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 200, FLAGS },
277  { "slew", "set slew factor", OFFSET(slew), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 200, FLAGS },
278  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, -1, 1, FLAGS, "mode" },
279  { "listen", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "mode" },
280  { "cut", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
281  { "boost", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
282  { NULL }
283 };
284 
285 AVFILTER_DEFINE_CLASS(adynamicequalizer);
286 
287 static const AVFilterPad inputs[] = {
288  {
289  .name = "default",
290  .type = AVMEDIA_TYPE_AUDIO,
291  .filter_frame = filter_frame,
292  .config_props = config_input,
293  },
294 };
295 
296 static const AVFilterPad outputs[] = {
297  {
298  .name = "default",
299  .type = AVMEDIA_TYPE_AUDIO,
300  },
301 };
302 
304  .name = "adynamicequalizer",
305  .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Equalization of input audio."),
306  .priv_size = sizeof(AudioDynamicEqualizerContext),
307  .priv_class = &adynamicequalizer_class,
308  .uninit = uninit,
314  .process_command = ff_filter_process_command,
315 };
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:88
td
#define td
Definition: regdef.h:70
AudioDynamicEqualizerContext::dfrequency
double dfrequency
Definition: af_adynamicequalizer.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AudioDynamicEqualizerContext::range
double range
Definition: af_adynamicequalizer.c:36
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:184
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
ff_af_adynamicequalizer
const AVFilter ff_af_adynamicequalizer
Definition: af_adynamicequalizer.c:303
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AudioDynamicEqualizerContext::makeup
double makeup
Definition: af_adynamicequalizer.c:37
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_adynamicequalizer.c:49
AVOption
AVOption.
Definition: opt.h:247
b
#define b
Definition: input.c:40
FLAGS
#define FLAGS
Definition: af_adynamicequalizer.c:263
float.h
filter_channels
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_adynamicequalizer.c:141
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
AudioDynamicEqualizerContext::mode
int mode
Definition: af_adynamicequalizer.c:44
AudioDynamicEqualizerContext::state
AVFrame * state
Definition: af_adynamicequalizer.c:46
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
get_coef
static double get_coef(double x, double sr)
Definition: af_adynamicequalizer.c:218
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
formats.h
v0
#define v0
Definition: regdef.h:26
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_adynamicequalizer.c:223
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adynamicequalizer.c:255
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
get_svf
static double get_svf(double in, double *m, double *a, double *b)
Definition: af_adynamicequalizer.c:61
AudioDynamicEqualizerContext::release_coef
double release_coef
Definition: af_adynamicequalizer.c:43
av_cold
#define av_cold
Definition: attributes.h:90
adynamicequalizer_options
static const AVOption adynamicequalizer_options[]
Definition: af_adynamicequalizer.c:265
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:628
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
arg
const char * arg
Definition: jacosubdec.c:67
to_dB
static double to_dB(double x)
Definition: af_adynamicequalizer.c:79
sqr
static double sqr(double x)
Definition: af_adynamicequalizer.c:84
AudioDynamicEqualizerContext::tqfactor
double tqfactor
Definition: af_adynamicequalizer.c:34
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(adynamicequalizer)
src
#define src
Definition: vp8dsp.c:255
exp
int8_t exp
Definition: eval.c:72
AudioDynamicEqualizerContext::slew
double slew
Definition: af_adynamicequalizer.c:39
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
state
static struct @320 state
outputs
static const AVFilterPad outputs[]
Definition: af_adynamicequalizer.c:296
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
OFFSET
#define OFFSET(x)
Definition: af_adynamicequalizer.c:262
hermite.h
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:494
fmaxf
float fmaxf(float, float)
fmin
double fmin(double, double)
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:473
AudioDynamicEqualizerContext
Definition: af_adynamicequalizer.c:27
ff_filter_process_command
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:882
AudioDynamicEqualizerContext::knee
double knee
Definition: af_adynamicequalizer.c:38
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
M_PI
#define M_PI
Definition: mathematics.h:52
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:397
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:378
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:803
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AudioDynamicEqualizerContext::ratio
double ratio
Definition: af_adynamicequalizer.c:35
from_dB
static double from_dB(double x)
Definition: af_adynamicequalizer.c:74
AVFilter
Filter definition.
Definition: avfilter.h:165
AudioDynamicEqualizerContext::attack
double attack
Definition: af_adynamicequalizer.c:40
AudioDynamicEqualizerContext::tfrequency
double tfrequency
Definition: af_adynamicequalizer.c:33
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AudioDynamicEqualizerContext::dqfactor
double dqfactor
Definition: af_adynamicequalizer.c:32
AudioDynamicEqualizerContext::release
double release
Definition: af_adynamicequalizer.c:41
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:121
audio.h
M_LN10
#define M_LN10
Definition: mathematics.h:43
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
AudioDynamicEqualizerContext::threshold
double threshold
Definition: af_adynamicequalizer.c:30
get_gain
static double get_gain(double in, double srate, double makeup, double aattack, double iratio, double knee, double range, double thresdb, double slewfactor, double *state, double attack_coeff, double release_coeff, double nc)
Definition: af_adynamicequalizer.c:89
AudioDynamicEqualizerContext::attack_coef
double attack_coef
Definition: af_adynamicequalizer.c:42
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#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:154
inputs
static const AVFilterPad inputs[]
Definition: af_adynamicequalizer.c:287
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:143
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233