]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_asupercut.c
avfilter/af_asupercut: add order option
[ffmpeg] / libavfilter / af_asupercut.c
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 "libavutil/channel_layout.h"
20 #include "libavutil/ffmath.h"
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "audio.h"
24 #include "formats.h"
25
26 typedef struct BiquadCoeffs {
27     double a1, a2;
28     double b0, b1, b2;
29 } BiquadCoeffs;
30
31 typedef struct ASuperCutContext {
32     const AVClass *class;
33
34     double cutoff;
35     int order;
36
37     int filter_count;
38     int bypass;
39
40     BiquadCoeffs coeffs[10];
41
42     AVFrame *w;
43 } ASuperCutContext;
44
45 static int query_formats(AVFilterContext *ctx)
46 {
47     AVFilterFormats *formats = NULL;
48     AVFilterChannelLayouts *layouts = NULL;
49     static const enum AVSampleFormat sample_fmts[] = {
50         AV_SAMPLE_FMT_DBLP,
51         AV_SAMPLE_FMT_NONE
52     };
53     int ret;
54
55     formats = ff_make_format_list(sample_fmts);
56     if (!formats)
57         return AVERROR(ENOMEM);
58     ret = ff_set_common_formats(ctx, formats);
59     if (ret < 0)
60         return ret;
61
62     layouts = ff_all_channel_counts();
63     if (!layouts)
64         return AVERROR(ENOMEM);
65
66     ret = ff_set_common_channel_layouts(ctx, layouts);
67     if (ret < 0)
68         return ret;
69
70     formats = ff_all_samplerates();
71     return ff_set_common_samplerates(ctx, formats);
72 }
73
74 static void calc_q_factors(int n, double *q)
75 {
76     for (int i = 0; i < n / 2; i++)
77         q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
78 }
79
80 static int get_coeffs(AVFilterContext *ctx)
81 {
82     ASuperCutContext *s = ctx->priv;
83     AVFilterLink *inlink = ctx->inputs[0];
84     double w0 = s->cutoff / inlink->sample_rate;
85     double K = tan(M_PI * w0);
86     double q[10];
87
88     s->bypass = w0 >= 0.5;
89     if (s->bypass)
90         return 0;
91
92     s->filter_count = s->order / 2 + (s->order & 1);
93     calc_q_factors(s->order, q);
94
95     if (s->order & 1) {
96         BiquadCoeffs *coeffs = &s->coeffs[0];
97         double omega = 2. * tan(M_PI * w0);
98
99         coeffs->b0 = omega / (2. + omega);
100         coeffs->b1 = coeffs->b0;
101         coeffs->b2 = 0.;
102         coeffs->a1 = -(omega - 2.) / (2. + omega);
103         coeffs->a2 = 0.;
104     }
105
106     for (int b = (s->order & 1); b < s->filter_count; b++) {
107         BiquadCoeffs *coeffs = &s->coeffs[b];
108         const int idx = b - (s->order & 1);
109         double norm = 1.0 / (1.0 + K / q[idx] + K * K);
110
111         coeffs->b0 = K * K * norm;
112         coeffs->b1 = 2.0 * coeffs->b0;
113         coeffs->b2 = coeffs->b0;
114         coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
115         coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
116     }
117
118     return 0;
119 }
120
121 static int config_input(AVFilterLink *inlink)
122 {
123     AVFilterContext *ctx = inlink->dst;
124     ASuperCutContext *s = ctx->priv;
125
126     s->w = ff_get_audio_buffer(inlink, 2 * 10);
127     if (!s->w)
128         return AVERROR(ENOMEM);
129
130     return get_coeffs(ctx);
131 }
132
133 typedef struct ThreadData {
134     AVFrame *in, *out;
135 } ThreadData;
136
137 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
138 {
139     ASuperCutContext *s = ctx->priv;
140     ThreadData *td = arg;
141     AVFrame *out = td->out;
142     AVFrame *in = td->in;
143     const int start = (in->channels * jobnr) / nb_jobs;
144     const int end = (in->channels * (jobnr+1)) / nb_jobs;
145
146     for (int ch = start; ch < end; ch++) {
147         const double *src = (const double *)in->extended_data[ch];
148         double *dst = (double *)out->extended_data[ch];
149
150         for (int b = 0; b < s->filter_count; b++) {
151             BiquadCoeffs *coeffs = &s->coeffs[b];
152             const double a1 = coeffs->a1;
153             const double a2 = coeffs->a2;
154             const double b0 = coeffs->b0;
155             const double b1 = coeffs->b1;
156             const double b2 = coeffs->b2;
157             double *w = ((double *)s->w->extended_data[ch]) + b * 2;
158
159             for (int n = 0; n < in->nb_samples; n++) {
160                 double sin = b ? dst[n] : src[n];
161                 double sout = sin * b0 + w[0];
162
163                 w[0] = b1 * sin + w[1] + a1 * sout;
164                 w[1] = b2 * sin + a2 * sout;
165
166                 dst[n] = sout;
167             }
168         }
169     }
170
171     return 0;
172 }
173
174 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
175 {
176     AVFilterContext *ctx = inlink->dst;
177     ASuperCutContext *s = ctx->priv;
178     AVFilterLink *outlink = ctx->outputs[0];
179     ThreadData td;
180     AVFrame *out;
181
182     if (s->bypass)
183         return ff_filter_frame(outlink, in);
184
185     if (av_frame_is_writable(in)) {
186         out = in;
187     } else {
188         out = ff_get_audio_buffer(outlink, 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     td.in = in; td.out = out;
197     ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
198                                                             ff_filter_get_nb_threads(ctx)));
199
200     if (out != in)
201         av_frame_free(&in);
202     return ff_filter_frame(outlink, out);
203 }
204
205 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
206                            char *res, int res_len, int flags)
207 {
208     int ret;
209
210     ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
211     if (ret < 0)
212         return ret;
213
214     return get_coeffs(ctx);
215 }
216
217 static av_cold void uninit(AVFilterContext *ctx)
218 {
219     ASuperCutContext *s = ctx->priv;
220
221     av_frame_free(&s->w);
222 }
223
224 #define OFFSET(x) offsetof(ASuperCutContext, x)
225 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
226
227 static const AVOption asupercut_options[] = {
228     { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
229     { "order",  "set filter order",     OFFSET(order),  AV_OPT_TYPE_INT,    {.i64=10},        3,     20, FLAGS },
230     { NULL }
231 };
232
233 AVFILTER_DEFINE_CLASS(asupercut);
234
235 static const AVFilterPad inputs[] = {
236     {
237         .name         = "default",
238         .type         = AVMEDIA_TYPE_AUDIO,
239         .filter_frame = filter_frame,
240         .config_props = config_input,
241     },
242     { NULL }
243 };
244
245 static const AVFilterPad outputs[] = {
246     {
247         .name = "default",
248         .type = AVMEDIA_TYPE_AUDIO,
249     },
250     { NULL }
251 };
252
253 AVFilter ff_af_asupercut = {
254     .name            = "asupercut",
255     .description     = NULL_IF_CONFIG_SMALL("Cut super frequencies."),
256     .query_formats   = query_formats,
257     .priv_size       = sizeof(ASuperCutContext),
258     .priv_class      = &asupercut_class,
259     .uninit          = uninit,
260     .inputs          = inputs,
261     .outputs         = outputs,
262     .process_command = process_command,
263     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
264                        AVFILTER_FLAG_SLICE_THREADS,
265 };