History

avfilter: add aexciter audio filter

Paul B Mahol committed 5 years ago

Commit 579e4e51 parent 129978a6 files · +377 −1Browse files

Unified · Split

6 files
  1. Changelog +1 −0
  2. doc/filters.texi +56 −0
  3. libavfilter/Makefile +1 −0
  4. libavfilter/af_aexciter.c +317 −0
  5. libavfilter/allfilters.c +1 −0
  6. libavfilter/version.h +1 −1
Changelog+1−0
@@ -69,6 +69,7 @@ version <next>: · 68 unmodified lines
69- xbm_pipe demuxer69- xbm_pipe demuxer
70- colorize filter70- colorize filter
71- CRI parser71- CRI parser
72- aexciter audio filter
72 73
73 74
74version 4.3:75version 4.3:
doc/filters.texi+56−0
@@ -1003,6 +1003,62 @@ aeval=val(0)|-val(1) · 1002 unmodified lines
1003@end example1003@end example
1004@end itemize1004@end itemize
1005 1005
1006@section aexciter
1007
1008An exciter is used to produce high sound that is not present in the
1009original signal. This is done by creating harmonic distortions of the
1010signal which are restricted in range and added to the original signal.
1011An Exciter raises the upper end of an audio signal without simply raising
1012the higher frequencies like an equalizer would do to create a more
1013"crisp" or "brilliant" sound.
1014
1015The filter accepts the following options:
1016
1017@table @option
1018@item level_in
1019Set input level prior processing of signal.
1020Allowed range is from 0 to 64.
1021Default value is 1.
1022
1023@item level_out
1024Set output level after processing of signal.
1025Allowed range is from 0 to 64.
1026Default value is 1.
1027
1028@item amount
1029Set the amount of harmonics added to original signal.
1030Allowed range is from 0 to 64.
1031Default value is 1.
1032
1033@item drive
1034Set the amount of newly created harmonics.
1035Allowed range is from 0.1 to 10.
1036Default value is 8.5.
1037
1038@item blend
1039Set the octave of newly created harmonics.
1040Allowed range is from -10 to 10.
1041Default value is 0.
1042
1043@item freq
1044Set the lower frequency limit of producing harmonics in Hz.
1045Allowed range is from 2000 to 12000 Hz.
1046Default is 7500 Hz.
1047
1048@item ceil
1049Set the upper frequency limit of producing harmonics.
1050Allowed range is from 9999 to 20000 Hz.
1051If value is lower than 10000 Hz no limit is applied.
1052
1053@item listen
1054Mute the original signal and output only added harmonics.
1055By default is disabled.
1056@end table
1057
1058@subsection Commands
1059
1060This filter supports the all above options as @ref{commands}.
1061
1006@anchor{afade}1062@anchor{afade}
1007@section afade1063@section afade
1008 1064
libavfilter/Makefile+1−0
@@ -46,6 +46,7 @@ OBJS-$(CONFIG_ADERIVATIVE_FILTER) += af_aderivative.o · 45 unmodified lines
46OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o46OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o
47OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o47OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o
48OBJS-$(CONFIG_AEVAL_FILTER) += aeval.o48OBJS-$(CONFIG_AEVAL_FILTER) += aeval.o
49OBJS-$(CONFIG_AEXCITER_FILTER) += af_aexciter.o
49OBJS-$(CONFIG_AFADE_FILTER) += af_afade.o50OBJS-$(CONFIG_AFADE_FILTER) += af_afade.o
50OBJS-$(CONFIG_AFFTDN_FILTER) += af_afftdn.o51OBJS-$(CONFIG_AFFTDN_FILTER) += af_afftdn.o
51OBJS-$(CONFIG_AFFTFILT_FILTER) += af_afftfilt.o52OBJS-$(CONFIG_AFFTFILT_FILTER) += af_afftfilt.o
libavfilter/af_aexciter.c+317−0
@@ -0,0 +1,317 @@
1/*
2 * Copyright (c) Markus Schmidt and Christian Holschuh
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/opt.h"
22#include "avfilter.h"
23#include "internal.h"
24#include "audio.h"
25
26typedef struct ChannelParams {
27 double blend_old, drive_old;
28 double rdrive, rbdr, kpa, kpb, kna, knb, ap,
29 an, imr, kc, srct, sq, pwrq;
30 double prev_med, prev_out;
31
32 double hp[5], lp[5];
33 double hw[4][2], lw[2][2];
34} ChannelParams;
35
36typedef struct AExciterContext {
37 const AVClass *class;
38
39 double level_in;
40 double level_out;
41 double amount;
42 double drive;
43 double blend;
44 double freq;
45 double ceil;
46 int listen;
47
48 ChannelParams *cp;
49} AExciterContext;
50
51#define OFFSET(x) offsetof(AExciterContext, x)
52#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
53
54static const AVOption aexciter_options[] = {
55 { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
56 { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
57 { "amount", "set amount", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
58 { "drive", "set harmonics", OFFSET(drive), AV_OPT_TYPE_DOUBLE, {.dbl=8.5}, 0.1, 10, A },
59 { "blend", "set blend harmonics", OFFSET(blend), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -10, 10, A },
60 { "freq", "set scope", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl=7500}, 2000, 12000, A },
61 { "ceil", "set ceiling", OFFSET(ceil), AV_OPT_TYPE_DOUBLE, {.dbl=9999}, 9999, 20000, A },
62 { "listen", "enable listen mode", OFFSET(listen), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
63 { NULL }
64};
65
66AVFILTER_DEFINE_CLASS(aexciter);
67
68static inline double M(double x)
69{
70 return (fabs(x) > 0.00000001) ? x : 0.0;
71}
72
73static inline double D(double x)
74{
75 x = fabs(x);
76
77 return (x > 0.00000001) ? sqrt(x) : 0.0;
78}
79
80static void set_params(ChannelParams *p,
81 double blend, double drive,
82 double srate, double freq,
83 double ceil)
84{
85 double a0, a1, a2, b0, b1, b2, w0, alpha;
86
87 p->rdrive = 12.0 / drive;
88 p->rbdr = p->rdrive / (10.5 - blend) * 780.0 / 33.0;
89 p->kpa = D(2.0 * (p->rdrive*p->rdrive) - 1.0) + 1.0;
90 p->kpb = (2.0 - p->kpa) / 2.0;
91 p->ap = ((p->rdrive*p->rdrive) - p->kpa + 1.0) / 2.0;
92 p->kc = p->kpa / D(2.0 * D(2.0 * (p->rdrive*p->rdrive) - 1.0) - 2.0 * p->rdrive*p->rdrive);
93
94 p->srct = (0.1 * srate) / (0.1 * srate + 1.0);
95 p->sq = p->kc*p->kc + 1.0;
96 p->knb = -1.0 * p->rbdr / D(p->sq);
97 p->kna = 2.0 * p->kc * p->rbdr / D(p->sq);
98 p->an = p->rbdr*p->rbdr / p->sq;
99 p->imr = 2.0 * p->knb + D(2.0 * p->kna + 4.0 * p->an - 1.0);
100 p->pwrq = 2.0 / (p->imr + 1.0);
101
102 w0 = 2 * M_PI * freq / srate;
103 alpha = sin(w0) / (2. * 0.707);
104 a0 = 1 + alpha;
105 a1 = -2 * cos(w0);
106 a2 = 1 - alpha;
107 b0 = (1 + cos(w0)) / 2;
108 b1 = -(1 + cos(w0));
109 b2 = (1 + cos(w0)) / 2;
110
111 p->hp[0] =-a1 / a0;
112 p->hp[1] =-a2 / a0;
113 p->hp[2] = b0 / a0;
114 p->hp[3] = b1 / a0;
115 p->hp[4] = b2 / a0;
116
117 w0 = 2 * M_PI * ceil / srate;
118 alpha = sin(w0) / (2. * 0.707);
119 a0 = 1 + alpha;
120 a1 = -2 * cos(w0);
121 a2 = 1 - alpha;
122 b0 = (1 - cos(w0)) / 2;
123 b1 = 1 - cos(w0);
124 b2 = (1 - cos(w0)) / 2;
125
126 p->lp[0] =-a1 / a0;
127 p->lp[1] =-a2 / a0;
128 p->lp[2] = b0 / a0;
129 p->lp[3] = b1 / a0;
130 p->lp[4] = b2 / a0;
131}
132
133static double bprocess(double in, const double *const c,
134 double *w1, double *w2)
135{
136 double out = c[2] * in + *w1;
137
138 *w1 = c[3] * in + *w2 + c[0] * out;
139 *w2 = c[4] * in + c[1] * out;
140
141 return out;
142}
143
144static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
145{
146 double proc = in, med;
147
148 proc = bprocess(proc, p->hp, &p->hw[0][0], &p->hw[0][1]);
149 proc = bprocess(proc, p->hp, &p->hw[1][0], &p->hw[1][1]);
150
151 if (proc >= 0.0) {
152 med = (D(p->ap + proc * (p->kpa - proc)) + p->kpb) * p->pwrq;
153 } else {
154 med = (D(p->an - proc * (p->kna + proc)) + p->knb) * p->pwrq * -1.0;
155 }
156
157 proc = p->srct * (med - p->prev_med + p->prev_out);
158 p->prev_med = M(med);
159 p->prev_out = M(proc);
160
161 proc = bprocess(proc, p->hp, &p->hw[2][0], &p->hw[2][1]);
162 proc = bprocess(proc, p->hp, &p->hw[3][0], &p->hw[3][1]);
163
164 if (s->ceil >= 10000.) {
165 proc = bprocess(proc, p->lp, &p->lw[0][0], &p->lw[0][1]);
166 proc = bprocess(proc, p->lp, &p->lw[1][0], &p->lw[1][1]);
167 }
168
169 return proc;
170}
171
172static int filter_frame(AVFilterLink *inlink, AVFrame *in)
173{
174 AVFilterContext *ctx = inlink->dst;
175 AExciterContext *s = ctx->priv;
176 AVFilterLink *outlink = ctx->outputs[0];
177 AVFrame *out;
178 const double *src = (const double *)in->data[0];
179 const double level_in = s->level_in;
180 const double level_out = s->level_out;
181 const double amount = s->amount;
182 const double listen = 1.0 - s->listen;
183 double *dst;
184
185 if (av_frame_is_writable(in)) {
186 out = in;
187 } else {
188 out = ff_get_audio_buffer(inlink, in->nb_samples);
189 if (!out) {
190 av_frame_free(&in);
191 return AVERROR(ENOMEM);
192 }
193 av_frame_copy_props(out, in);
194 }
195
196 dst = (double *)out->data[0];
197 for (int n = 0; n < in->nb_samples; n++) {
198 for (int c = 0; c < inlink->channels; c++) {
199 double sample = src[c] * level_in;
200
201 sample = distortion_process(s, &s->cp[c], sample);
202 sample = sample * amount + listen * src[c];
203
204 sample *= level_out;
205 if (ctx->is_disabled)
206 dst[c] = src[c];
207 else
208 dst[c] = sample;
209 }
210
211 src += inlink->channels;
212 dst += inlink->channels;
213 }
214
215 if (in != out)
216 av_frame_free(&in);
217
218 return ff_filter_frame(outlink, out);
219}
220
221static int query_formats(AVFilterContext *ctx)
222{
223 AVFilterFormats *formats;
224 AVFilterChannelLayouts *layouts;
225 static const enum AVSampleFormat sample_fmts[] = {
226 AV_SAMPLE_FMT_DBL,
227 AV_SAMPLE_FMT_NONE
228 };
229 int ret;
230
231 layouts = ff_all_channel_counts();
232 if (!layouts)
233 return AVERROR(ENOMEM);
234 ret = ff_set_common_channel_layouts(ctx, layouts);
235 if (ret < 0)
236 return ret;
237
238 formats = ff_make_format_list(sample_fmts);
239 if (!formats)
240 return AVERROR(ENOMEM);
241 ret = ff_set_common_formats(ctx, formats);
242 if (ret < 0)
243 return ret;
244
245 formats = ff_all_samplerates();
246 if (!formats)
247 return AVERROR(ENOMEM);
248 return ff_set_common_samplerates(ctx, formats);
249}
250
251static av_cold void uninit(AVFilterContext *ctx)
252{
253 AExciterContext *s = ctx->priv;
254
255 av_freep(&s->cp);
256}
257
258static int config_input(AVFilterLink *inlink)
259{
260 AVFilterContext *ctx = inlink->dst;
261 AExciterContext *s = ctx->priv;
262
263 if (!s->cp)
264 s->cp = av_calloc(inlink->channels, sizeof(*s->cp));
265 if (!s->cp)
266 return AVERROR(ENOMEM);
267
268 for (int i = 0; i < inlink->channels; i++)
269 set_params(&s->cp[i], s->blend, s->drive, inlink->sample_rate,
270 s->freq, s->ceil);
271
272 return 0;
273}
274
275static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
276 char *res, int res_len, int flags)
277{
278 AVFilterLink *inlink = ctx->inputs[0];
279 int ret;
280
281 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
282 if (ret < 0)
283 return ret;
284
285 return config_input(inlink);
286}
287
288static const AVFilterPad avfilter_af_aexciter_inputs[] = {
289 {
290 .name = "default",
291 .type = AVMEDIA_TYPE_AUDIO,
292 .config_props = config_input,
293 .filter_frame = filter_frame,
294 },
295 { NULL }
296};
297
298static const AVFilterPad avfilter_af_aexciter_outputs[] = {
299 {
300 .name = "default",
301 .type = AVMEDIA_TYPE_AUDIO,
302 },
303 { NULL }
304};
305
306AVFilter ff_af_aexciter = {
307 .name = "aexciter",
308 .description = NULL_IF_CONFIG_SMALL("Enhance high frequency part of audio."),
309 .priv_size = sizeof(AExciterContext),
310 .priv_class = &aexciter_class,
311 .uninit = uninit,
312 .query_formats = query_formats,
313 .inputs = avfilter_af_aexciter_inputs,
314 .outputs = avfilter_af_aexciter_outputs,
315 .process_command = process_command,
316 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
317};
libavfilter/allfilters.c+1−0
@@ -39,6 +39,7 @@ extern AVFilter ff_af_aderivative; · 38 unmodified lines
39extern AVFilter ff_af_aecho;39extern AVFilter ff_af_aecho;
40extern AVFilter ff_af_aemphasis;40extern AVFilter ff_af_aemphasis;
41extern AVFilter ff_af_aeval;41extern AVFilter ff_af_aeval;
42extern AVFilter ff_af_aexciter;
42extern AVFilter ff_af_afade;43extern AVFilter ff_af_afade;
43extern AVFilter ff_af_afftdn;44extern AVFilter ff_af_afftdn;
44extern AVFilter ff_af_afftfilt;45extern AVFilter ff_af_afftfilt;
libavfilter/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
30#include "libavutil/version.h"30#include "libavutil/version.h"
31 31
32#define LIBAVFILTER_VERSION_MAJOR 732#define LIBAVFILTER_VERSION_MAJOR 7
33#define LIBAVFILTER_VERSION_MINOR 10333#define LIBAVFILTER_VERSION_MINOR 104
34#define LIBAVFILTER_VERSION_MICRO 10034#define LIBAVFILTER_VERSION_MICRO 100
35 35
36 36

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