2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/channel_layout.h"
20 #include "libavutil/ffmath.h"
21 #include "libavutil/opt.h"
26 typedef struct BiquadCoeffs {
31 typedef struct ASuperCutContext {
38 BiquadCoeffs coeffs[5];
43 static int query_formats(AVFilterContext *ctx)
45 AVFilterFormats *formats = NULL;
46 AVFilterChannelLayouts *layouts = NULL;
47 static const enum AVSampleFormat sample_fmts[] = {
53 formats = ff_make_format_list(sample_fmts);
55 return AVERROR(ENOMEM);
56 ret = ff_set_common_formats(ctx, formats);
60 layouts = ff_all_channel_counts();
62 return AVERROR(ENOMEM);
64 ret = ff_set_common_channel_layouts(ctx, layouts);
68 formats = ff_all_samplerates();
69 return ff_set_common_samplerates(ctx, formats);
72 static int get_coeffs(AVFilterContext *ctx)
74 ASuperCutContext *s = ctx->priv;
75 AVFilterLink *inlink = ctx->inputs[0];
76 double w0 = s->cutoff / inlink->sample_rate;
77 double K = tan(M_PI * w0);
80 s->bypass = w0 >= 0.5;
90 for (int b = 0; b < 5; b++) {
91 BiquadCoeffs *coeffs = &s->coeffs[b];
92 double norm = 1.0 / (1.0 + K / q[b] + K * K);
94 coeffs->b0 = K * K * norm;
95 coeffs->b1 = 2.0 * coeffs->b0;
96 coeffs->b2 = coeffs->b0;
97 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
98 coeffs->a2 = -(1.0 - K / q[b] + K * K) * norm;
104 static int config_input(AVFilterLink *inlink)
106 AVFilterContext *ctx = inlink->dst;
107 ASuperCutContext *s = ctx->priv;
109 s->w = ff_get_audio_buffer(inlink, 2 * 5);
111 return AVERROR(ENOMEM);
113 return get_coeffs(ctx);
116 typedef struct ThreadData {
120 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
122 ASuperCutContext *s = ctx->priv;
123 ThreadData *td = arg;
124 AVFrame *out = td->out;
125 AVFrame *in = td->in;
126 const int start = (in->channels * jobnr) / nb_jobs;
127 const int end = (in->channels * (jobnr+1)) / nb_jobs;
129 for (int ch = start; ch < end; ch++) {
130 const double *src = (const double *)in->extended_data[ch];
131 double *dst = (double *)out->extended_data[ch];
133 for (int b = 0; b < 5; b++) {
134 BiquadCoeffs *coeffs = &s->coeffs[b];
135 const double a1 = coeffs->a1;
136 const double a2 = coeffs->a2;
137 const double b0 = coeffs->b0;
138 const double b1 = coeffs->b1;
139 const double b2 = coeffs->b2;
140 double *w = ((double *)s->w->extended_data[ch]) + b * 2;
142 for (int n = 0; n < in->nb_samples; n++) {
143 double sin = b ? dst[n] : src[n];
144 double sout = sin * b0 + w[0];
146 w[0] = b1 * sin + w[1] + a1 * sout;
147 w[1] = b2 * sin + a2 * sout;
157 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
159 AVFilterContext *ctx = inlink->dst;
160 ASuperCutContext *s = ctx->priv;
161 AVFilterLink *outlink = ctx->outputs[0];
166 return ff_filter_frame(outlink, in);
168 if (av_frame_is_writable(in)) {
171 out = ff_get_audio_buffer(outlink, in->nb_samples);
174 return AVERROR(ENOMEM);
176 av_frame_copy_props(out, in);
179 td.in = in; td.out = out;
180 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
181 ff_filter_get_nb_threads(ctx)));
185 return ff_filter_frame(outlink, out);
188 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
189 char *res, int res_len, int flags)
193 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
197 return get_coeffs(ctx);
200 static av_cold void uninit(AVFilterContext *ctx)
202 ASuperCutContext *s = ctx->priv;
204 av_frame_free(&s->w);
207 #define OFFSET(x) offsetof(ASuperCutContext, x)
208 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
210 static const AVOption asupercut_options[] = {
211 { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
215 AVFILTER_DEFINE_CLASS(asupercut);
217 static const AVFilterPad inputs[] = {
220 .type = AVMEDIA_TYPE_AUDIO,
221 .filter_frame = filter_frame,
222 .config_props = config_input,
227 static const AVFilterPad outputs[] = {
230 .type = AVMEDIA_TYPE_AUDIO,
235 AVFilter ff_af_asupercut = {
237 .description = NULL_IF_CONFIG_SMALL("Cut super frequencies."),
238 .query_formats = query_formats,
239 .priv_size = sizeof(ASuperCutContext),
240 .priv_class = &asupercut_class,
244 .process_command = process_command,
245 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
246 AVFILTER_FLAG_SLICE_THREADS,