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 {
40 BiquadCoeffs coeffs[10];
45 static int query_formats(AVFilterContext *ctx)
47 AVFilterFormats *formats = NULL;
48 AVFilterChannelLayouts *layouts = NULL;
49 static const enum AVSampleFormat sample_fmts[] = {
55 formats = ff_make_format_list(sample_fmts);
57 return AVERROR(ENOMEM);
58 ret = ff_set_common_formats(ctx, formats);
62 layouts = ff_all_channel_counts();
64 return AVERROR(ENOMEM);
66 ret = ff_set_common_channel_layouts(ctx, layouts);
70 formats = ff_all_samplerates();
71 return ff_set_common_samplerates(ctx, formats);
74 static void calc_q_factors(int n, double *q)
76 for (int i = 0; i < n / 2; i++)
77 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
80 static int get_coeffs(AVFilterContext *ctx)
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);
88 s->bypass = w0 >= 0.5;
92 s->filter_count = s->order / 2 + (s->order & 1);
93 calc_q_factors(s->order, q);
96 BiquadCoeffs *coeffs = &s->coeffs[0];
97 double omega = 2. * tan(M_PI * w0);
99 coeffs->b0 = omega / (2. + omega);
100 coeffs->b1 = coeffs->b0;
102 coeffs->a1 = -(omega - 2.) / (2. + omega);
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);
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;
121 static int config_input(AVFilterLink *inlink)
123 AVFilterContext *ctx = inlink->dst;
124 ASuperCutContext *s = ctx->priv;
126 s->w = ff_get_audio_buffer(inlink, 2 * 10);
128 return AVERROR(ENOMEM);
130 return get_coeffs(ctx);
133 typedef struct ThreadData {
137 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
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;
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];
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;
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];
163 w[0] = b1 * sin + w[1] + a1 * sout;
164 w[1] = b2 * sin + a2 * sout;
174 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
176 AVFilterContext *ctx = inlink->dst;
177 ASuperCutContext *s = ctx->priv;
178 AVFilterLink *outlink = ctx->outputs[0];
183 return ff_filter_frame(outlink, in);
185 if (av_frame_is_writable(in)) {
188 out = ff_get_audio_buffer(outlink, in->nb_samples);
191 return AVERROR(ENOMEM);
193 av_frame_copy_props(out, in);
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)));
202 return ff_filter_frame(outlink, out);
205 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
206 char *res, int res_len, int flags)
210 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
214 return get_coeffs(ctx);
217 static av_cold void uninit(AVFilterContext *ctx)
219 ASuperCutContext *s = ctx->priv;
221 av_frame_free(&s->w);
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
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 },
233 AVFILTER_DEFINE_CLASS(asupercut);
235 static const AVFilterPad inputs[] = {
238 .type = AVMEDIA_TYPE_AUDIO,
239 .filter_frame = filter_frame,
240 .config_props = config_input,
245 static const AVFilterPad outputs[] = {
248 .type = AVMEDIA_TYPE_AUDIO,
253 AVFilter ff_af_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,
262 .process_command = process_command,
263 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
264 AVFILTER_FLAG_SLICE_THREADS,