aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/af_aexciter.c
blob: 8e829f992ba90fbc03f07cfa64f19bd3c199d00c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (c) Markus Schmidt and Christian Holschuh
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/opt.h"
#include "avfilter.h"
#include "internal.h"
#include "audio.h"

typedef struct ChannelParams {
    double blend_old, drive_old;
    double rdrive, rbdr, kpa, kpb, kna, knb, ap,
           an, imr, kc, srct, sq, pwrq;
    double prev_med, prev_out;

    double hp[5], lp[5];
    double hw[4][2], lw[2][2];
} ChannelParams;

typedef struct AExciterContext {
    const AVClass *class;

    double level_in;
    double level_out;
    double amount;
    double drive;
    double blend;
    double freq;
    double ceil;
    int listen;

    ChannelParams *cp;
} AExciterContext;

#define OFFSET(x) offsetof(AExciterContext, x)
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM

static const AVOption aexciter_options[] = {
    { "level_in",  "set level in",    OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},           0, 64, A },
    { "level_out", "set level out",   OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},           0, 64, A },
    { "amount", "set amount",         OFFSET(amount),    AV_OPT_TYPE_DOUBLE, {.dbl=1},           0, 64, A },
    { "drive", "set harmonics",       OFFSET(drive),     AV_OPT_TYPE_DOUBLE, {.dbl=8.5},       0.1, 10, A },
    { "blend", "set blend harmonics", OFFSET(blend),     AV_OPT_TYPE_DOUBLE, {.dbl=0},         -10, 10, A },
    { "freq", "set scope",            OFFSET(freq),      AV_OPT_TYPE_DOUBLE, {.dbl=7500},  2000, 12000, A },
    { "ceil", "set ceiling",          OFFSET(ceil),      AV_OPT_TYPE_DOUBLE, {.dbl=9999},  9999, 20000, A },
    { "listen", "enable listen mode", OFFSET(listen),    AV_OPT_TYPE_BOOL,   {.i64=0},        0,     1, A },
    { NULL }
};

AVFILTER_DEFINE_CLASS(aexciter);

static inline double M(double x)
{
    return (fabs(x) > 0.00000001) ? x : 0.0;
}

static inline double D(double x)
{
    x = fabs(x);

    return (x > 0.00000001) ? sqrt(x) : 0.0;
}

static void set_params(ChannelParams *p,
                       double blend, double drive,
                       double srate, double freq,
                       double ceil)
{
    double a0, a1, a2, b0, b1, b2, w0, alpha;

    p->rdrive = 12.0 / drive;
    p->rbdr = p->rdrive / (10.5 - blend) * 780.0 / 33.0;
    p->kpa = D(2.0 * (p->rdrive*p->rdrive) - 1.0) + 1.0;
    p->kpb = (2.0 - p->kpa) / 2.0;
    p->ap = ((p->rdrive*p->rdrive) - p->kpa + 1.0) / 2.0;
    p->kc = p->kpa / D(2.0 * D(2.0 * (p->rdrive*p->rdrive) - 1.0) - 2.0 * p->rdrive*p->rdrive);

    p->srct = (0.1 * srate) / (0.1 * srate + 1.0);
    p->sq = p->kc*p->kc + 1.0;
    p->knb = -1.0 * p->rbdr / D(p->sq);
    p->kna = 2.0 * p->kc * p->rbdr / D(p->sq);
    p->an = p->rbdr*p->rbdr / p->sq;
    p->imr = 2.0 * p->knb + D(2.0 * p->kna + 4.0 * p->an - 1.0);
    p->pwrq = 2.0 / (p->imr + 1.0);

    w0 = 2 * M_PI * freq / srate;
    alpha = sin(w0) / (2. * 0.707);
    a0 =   1 + alpha;
    a1 =  -2 * cos(w0);
    a2 =   1 - alpha;
    b0 =  (1 + cos(w0)) / 2;
    b1 = -(1 + cos(w0));
    b2 =  (1 + cos(w0)) / 2;

    p->hp[0] =-a1 / a0;
    p->hp[1] =-a2 / a0;
    p->hp[2] = b0 / a0;
    p->hp[3] = b1 / a0;
    p->hp[4] = b2 / a0;

    w0 = 2 * M_PI * ceil / srate;
    alpha = sin(w0) / (2. * 0.707);
    a0 =  1 + alpha;
    a1 = -2 * cos(w0);
    a2 =  1 - alpha;
    b0 = (1 - cos(w0)) / 2;
    b1 =  1 - cos(w0);
    b2 = (1 - cos(w0)) / 2;

    p->lp[0] =-a1 / a0;
    p->lp[1] =-a2 / a0;
    p->lp[2] = b0 / a0;
    p->lp[3] = b1 / a0;
    p->lp[4] = b2 / a0;
}

static double bprocess(double in, const double *const c,
                       double *w1, double *w2)
{
    double out = c[2] * in + *w1;

    *w1 = c[3] * in + *w2 + c[0] * out;
    *w2 = c[4] * in + c[1] * out;

    return out;
}

static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
{
    double proc = in, med;

    proc = bprocess(proc, p->hp, &p->hw[0][0], &p->hw[0][1]);
    proc = bprocess(proc, p->hp, &p->hw[1][0], &p->hw[1][1]);

    if (proc >= 0.0) {
        med = (D(p->ap + proc * (p->kpa - proc)) + p->kpb) * p->pwrq;
    } else {
        med = (D(p->an - proc * (p->kna + proc)) + p->knb) * p->pwrq * -1.0;
    }

    proc = p->srct * (med - p->prev_med + p->prev_out);
    p->prev_med = M(med);
    p->prev_out = M(proc);

    proc = bprocess(proc, p->hp, &p->hw[2][0], &p->hw[2][1]);
    proc = bprocess(proc, p->hp, &p->hw[3][0], &p->hw[3][1]);

    if (s->ceil >= 10000.) {
        proc = bprocess(proc, p->lp, &p->lw[0][0], &p->lw[0][1]);
        proc = bprocess(proc, p->lp, &p->lw[1][0], &p->lw[1][1]);
    }

    return proc;
}

static int filter_frame(AVFilterLink *inlink, AVFrame *in)
{
    AVFilterContext *ctx = inlink->dst;
    AExciterContext *s = ctx->priv;
    AVFilterLink *outlink = ctx->outputs[0];
    AVFrame *out;
    const double *src = (const double *)in->data[0];
    const double level_in = s->level_in;
    const double level_out = s->level_out;
    const double amount = s->amount;
    const double listen = 1.0 - s->listen;
    double *dst;

    if (av_frame_is_writable(in)) {
        out = in;
    } else {
        out = ff_get_audio_buffer(inlink, in->nb_samples);
        if (!out) {
            av_frame_free(&in);
            return AVERROR(ENOMEM);
        }
        av_frame_copy_props(out, in);
    }

    dst = (double *)out->data[0];
    for (int n = 0; n < in->nb_samples; n++) {
        for (int c = 0; c < inlink->ch_layout.nb_channels; c++) {
            double sample = src[c] * level_in;

            sample = distortion_process(s, &s->cp[c], sample);
            sample = sample * amount + listen * src[c];

            sample *= level_out;
            if (ctx->is_disabled)
                dst[c] = src[c];
            else
                dst[c] = sample;
        }

        src += inlink->ch_layout.nb_channels;
        dst += inlink->ch_layout.nb_channels;
    }

    if (in != out)
        av_frame_free(&in);

    return ff_filter_frame(outlink, out);
}

static av_cold void uninit(AVFilterContext *ctx)
{
    AExciterContext *s = ctx->priv;

    av_freep(&s->cp);
}

static int config_input(AVFilterLink *inlink)
{
    AVFilterContext *ctx = inlink->dst;
    AExciterContext *s = ctx->priv;

    if (!s->cp)
        s->cp = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->cp));
    if (!s->cp)
        return AVERROR(ENOMEM);

    for (int i = 0; i < inlink->ch_layout.nb_channels; i++)
        set_params(&s->cp[i], s->blend, s->drive, inlink->sample_rate,
                   s->freq, s->ceil);

    return 0;
}

static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
                           char *res, int res_len, int flags)
{
    AVFilterLink *inlink = ctx->inputs[0];
    int ret;

    ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
    if (ret < 0)
        return ret;

    return config_input(inlink);
}

static const AVFilterPad avfilter_af_aexciter_inputs[] = {
    {
        .name         = "default",
        .type         = AVMEDIA_TYPE_AUDIO,
        .config_props = config_input,
        .filter_frame = filter_frame,
    },
};

static const AVFilterPad avfilter_af_aexciter_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_AUDIO,
    },
};

const AVFilter ff_af_aexciter = {
    .name          = "aexciter",
    .description   = NULL_IF_CONFIG_SMALL("Enhance high frequency part of audio."),
    .priv_size     = sizeof(AExciterContext),
    .priv_class    = &aexciter_class,
    .uninit        = uninit,
    FILTER_INPUTS(avfilter_af_aexciter_inputs),
    FILTER_OUTPUTS(avfilter_af_aexciter_outputs),
    FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBL),
    .process_command = process_command,
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
};