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 
27  const AVClass *class;
28 
29  double threshold;
30  double dfrequency;
31  double dqfactor;
32  double tfrequency;
33  double tqfactor;
34  double ratio;
35  double range;
36  double makeup;
37  double attack;
38  double release;
39  double attack_coef;
40  double release_coef;
41  int mode;
42  int direction;
43  int detection;
44  int tftype;
45  int dftype;
46  int precision;
47  int format;
48 
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];
54 
57 
59 {
61  static const enum AVSampleFormat sample_fmts[3][3] = {
65  };
66  int ret;
67 
69  return ret;
70 
71  if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts[s->precision])) < 0)
72  return ret;
73 
75 }
76 
77 static double get_coef(double x, double sr)
78 {
79  return 1.0 - exp(-1000. / (x * sr));
80 }
81 
82 typedef struct ThreadData {
83  AVFrame *in, *out;
84 } ThreadData;
85 
86 #define DEPTH 32
88 
89 #undef DEPTH
90 #define DEPTH 64
92 
94 {
95  AVFilterContext *ctx = inlink->dst;
97 
98  s->format = inlink->format;
99  s->state = ff_get_audio_buffer(inlink, 16);
100  if (!s->state)
101  return AVERROR(ENOMEM);
102 
103  switch (s->format) {
104  case AV_SAMPLE_FMT_DBLP:
105  s->filter_prepare = filter_prepare_double;
106  s->filter_channels = filter_channels_double;
107  break;
108  case AV_SAMPLE_FMT_FLTP:
109  s->filter_prepare = filter_prepare_float;
110  s->filter_channels = filter_channels_float;
111  break;
112  }
113 
114  return 0;
115 }
116 
118 {
119  AVFilterContext *ctx = inlink->dst;
120  AVFilterLink *outlink = ctx->outputs[0];
122  ThreadData td;
123  AVFrame *out;
124 
125  if (av_frame_is_writable(in)) {
126  out = in;
127  } else {
128  out = ff_get_audio_buffer(outlink, in->nb_samples);
129  if (!out) {
130  av_frame_free(&in);
131  return AVERROR(ENOMEM);
132  }
134  }
135 
136  td.in = in;
137  td.out = out;
138  s->filter_prepare(ctx);
139  ff_filter_execute(ctx, s->filter_channels, &td, NULL,
141 
142  if (out != in)
143  av_frame_free(&in);
144  return ff_filter_frame(outlink, out);
145 }
146 
148 {
150 
151  av_frame_free(&s->state);
152 }
153 
154 #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
155 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
156 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
157 
159  { "threshold", "set detection threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 100, FLAGS },
160  { "dfrequency", "set detection frequency", OFFSET(dfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
161  { "dqfactor", "set detection Q factor", OFFSET(dqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
162  { "tfrequency", "set target frequency", OFFSET(tfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
163  { "tqfactor", "set target Q factor", OFFSET(tqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
164  { "attack", "set attack duration", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 2000, FLAGS },
165  { "release", "set release duration", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=200}, 1, 2000, FLAGS },
166  { "ratio", "set ratio factor", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 30, FLAGS },
167  { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 100, FLAGS },
168  { "range", "set max gain", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 200, FLAGS },
169  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, -1, 1, FLAGS, "mode" },
170  { "listen", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "mode" },
171  { "cut", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
172  { "boost", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
173  { "dftype", "set detection filter type",OFFSET(dftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "dftype" },
174  { "bandpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "dftype" },
175  { "lowpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "dftype" },
176  { "highpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "dftype" },
177  { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "dftype" },
178  { "tftype", "set target filter type", OFFSET(tftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "tftype" },
179  { "bell", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "tftype" },
180  { "lowshelf", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "tftype" },
181  { "highshelf",0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "tftype" },
182  { "direction", "set direction", OFFSET(direction), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "direction" },
183  { "downward", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "direction" },
184  { "upward", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "direction" },
185  { "auto", "set auto threshold", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "auto" },
186  { "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "auto" },
187  { "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "auto" },
188  { "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "auto" },
189  { "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, "precision" },
190  { "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },
191  { "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "precision" },
192  { "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "precision" },
193  { NULL }
194 };
195 
196 AVFILTER_DEFINE_CLASS(adynamicequalizer);
197 
198 static const AVFilterPad inputs[] = {
199  {
200  .name = "default",
201  .type = AVMEDIA_TYPE_AUDIO,
202  .filter_frame = filter_frame,
203  .config_props = config_input,
204  },
205 };
206 
208  .name = "adynamicequalizer",
209  .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Equalization of input audio."),
210  .priv_size = sizeof(AudioDynamicEqualizerContext),
211  .priv_class = &adynamicequalizer_class,
212  .uninit = uninit,
218  .process_command = ff_filter_process_command,
219 };
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:107
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AudioDynamicEqualizerContext::precision
int precision
Definition: af_adynamicequalizer.c:46
td
#define td
Definition: regdef.h:70
AudioDynamicEqualizerContext::dfrequency
double dfrequency
Definition: af_adynamicequalizer.c:30
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::dftype
int dftype
Definition: af_adynamicequalizer.c:45
AudioDynamicEqualizerContext::range
double range
Definition: af_adynamicequalizer.c:35
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:978
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:947
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:207
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AudioDynamicEqualizerContext::makeup
double makeup
Definition: af_adynamicequalizer.c:36
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_adynamicequalizer.c:93
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
FLAGS
#define FLAGS
Definition: af_adynamicequalizer.c:156
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:760
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AudioDynamicEqualizerContext::mode
int mode
Definition: af_adynamicequalizer.c:41
AudioDynamicEqualizerContext::state
AVFrame * state
Definition: af_adynamicequalizer.c:55
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:526
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:153
get_coef
static double get_coef(double x, double sr)
Definition: af_adynamicequalizer.c:77
formats.h
AudioDynamicEqualizerContext::detection
int detection
Definition: af_adynamicequalizer.c:43
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_adynamicequalizer.c:117
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adynamicequalizer.c:147
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
AudioDynamicEqualizerContext::release_coef
double release_coef
Definition: af_adynamicequalizer.c:40
av_cold
#define av_cold
Definition: attributes.h:90
adynamicequalizer_options
static const AVOption adynamicequalizer_options[]
Definition: af_adynamicequalizer.c:158
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:776
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AudioDynamicEqualizerContext::da_double
double da_double[3]
Definition: af_adynamicequalizer.c:52
AudioDynamicEqualizerContext::filter_channels
int(* filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_adynamicequalizer.c:50
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
arg
const char * arg
Definition: jacosubdec.c:67
AudioDynamicEqualizerContext::tqfactor
double tqfactor
Definition: af_adynamicequalizer.c:33
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:736
AudioDynamicEqualizerContext::da_float
float da_float[3]
Definition: af_adynamicequalizer.c:53
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(adynamicequalizer)
ff_audio_default_filterpad
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:32
AudioDynamicEqualizerContext::dm_double
double dm_double[3]
Definition: af_adynamicequalizer.c:52
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:742
exp
int8_t exp
Definition: eval.c:72
AudioDynamicEqualizerContext::filter_prepare
int(* filter_prepare)(AVFilterContext *ctx)
Definition: af_adynamicequalizer.c:49
AudioDynamicEqualizerContext::tftype
int tftype
Definition: af_adynamicequalizer.c:44
adynamicequalizer_template.c
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:106
OFFSET
#define OFFSET(x)
Definition: af_adynamicequalizer.c:154
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AudioDynamicEqualizerContext::format
int format
Definition: af_adynamicequalizer.c:47
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:666
AudioDynamicEqualizerContext
Definition: af_adynamicequalizer.c:26
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2646
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:851
AudioDynamicEqualizerContext::dm_float
float dm_float[3]
Definition: af_adynamicequalizer.c:53
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:786
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AudioDynamicEqualizerContext::ratio
double ratio
Definition: af_adynamicequalizer.c:34
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
AudioDynamicEqualizerContext::attack
double attack
Definition: af_adynamicequalizer.c:37
AF
#define AF
Definition: af_adynamicequalizer.c:155
AudioDynamicEqualizerContext::tfrequency
double tfrequency
Definition: af_adynamicequalizer.c:32
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AudioDynamicEqualizerContext::dqfactor
double dqfactor
Definition: af_adynamicequalizer.c:31
AudioDynamicEqualizerContext::release
double release
Definition: af_adynamicequalizer.c:38
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
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:117
audio.h
AudioDynamicEqualizerContext::threshold
double threshold
Definition: af_adynamicequalizer.c:29
AudioDynamicEqualizerContext::attack_coef
double attack_coef
Definition: af_adynamicequalizer.c:39
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
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:155
inputs
static const AVFilterPad inputs[]
Definition: af_adynamicequalizer.c:198
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:144
int
int
Definition: ffmpeg_filter.c:368
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_adynamicequalizer.c:58
AudioDynamicEqualizerContext::direction
int direction
Definition: af_adynamicequalizer.c:42