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];
44 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
47 static int query_formats(AVFilterContext *ctx)
49 AVFilterFormats *formats = NULL;
50 AVFilterChannelLayouts *layouts = NULL;
51 static const enum AVSampleFormat sample_fmts[] = {
58 formats = ff_make_format_list(sample_fmts);
60 return AVERROR(ENOMEM);
61 ret = ff_set_common_formats(ctx, formats);
65 layouts = ff_all_channel_counts();
67 return AVERROR(ENOMEM);
69 ret = ff_set_common_channel_layouts(ctx, layouts);
73 formats = ff_all_samplerates();
74 return ff_set_common_samplerates(ctx, formats);
77 static void calc_q_factors(int n, double *q)
79 for (int i = 0; i < n / 2; i++)
80 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
83 static int get_coeffs(AVFilterContext *ctx)
85 ASuperCutContext *s = ctx->priv;
86 AVFilterLink *inlink = ctx->inputs[0];
87 double w0 = s->cutoff / inlink->sample_rate;
88 double K = tan(M_PI * w0);
91 s->bypass = w0 >= 0.5;
95 s->filter_count = s->order / 2 + (s->order & 1);
96 calc_q_factors(s->order, q);
99 BiquadCoeffs *coeffs = &s->coeffs[0];
100 double omega = 2. * tan(M_PI * w0);
102 coeffs->b0 = omega / (2. + omega);
103 coeffs->b1 = coeffs->b0;
105 coeffs->a1 = -(omega - 2.) / (2. + omega);
109 for (int b = (s->order & 1); b < s->filter_count; b++) {
110 BiquadCoeffs *coeffs = &s->coeffs[b];
111 const int idx = b - (s->order & 1);
112 double norm = 1.0 / (1.0 + K / q[idx] + K * K);
114 coeffs->b0 = K * K * norm;
115 coeffs->b1 = 2.0 * coeffs->b0;
116 coeffs->b2 = coeffs->b0;
117 coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
118 coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
124 typedef struct ThreadData {
128 #define FILTER(name, type) \
129 static int filter_channels_## name(AVFilterContext *ctx, void *arg, \
130 int jobnr, int nb_jobs) \
132 ASuperCutContext *s = ctx->priv; \
133 ThreadData *td = arg; \
134 AVFrame *out = td->out; \
135 AVFrame *in = td->in; \
136 const int start = (in->channels * jobnr) / nb_jobs; \
137 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
139 for (int ch = start; ch < end; ch++) { \
140 const type *src = (const type *)in->extended_data[ch]; \
141 type *dst = (type *)out->extended_data[ch]; \
143 for (int b = 0; b < s->filter_count; b++) { \
144 BiquadCoeffs *coeffs = &s->coeffs[b]; \
145 const type a1 = coeffs->a1; \
146 const type a2 = coeffs->a2; \
147 const type b0 = coeffs->b0; \
148 const type b1 = coeffs->b1; \
149 const type b2 = coeffs->b2; \
150 type *w = ((type *)s->w->extended_data[ch]) + b * 2; \
152 for (int n = 0; n < in->nb_samples; n++) { \
153 type sin = b ? dst[n] : src[n]; \
154 type sout = sin * b0 + w[0]; \
156 w[0] = b1 * sin + w[1] + a1 * sout; \
157 w[1] = b2 * sin + a2 * sout; \
170 static int config_input(AVFilterLink *inlink)
172 AVFilterContext *ctx = inlink->dst;
173 ASuperCutContext *s = ctx->priv;
175 switch (inlink->format) {
176 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
177 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
180 s->w = ff_get_audio_buffer(inlink, 2 * 10);
182 return AVERROR(ENOMEM);
184 return get_coeffs(ctx);
187 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
189 AVFilterContext *ctx = inlink->dst;
190 ASuperCutContext *s = ctx->priv;
191 AVFilterLink *outlink = ctx->outputs[0];
196 return ff_filter_frame(outlink, in);
198 if (av_frame_is_writable(in)) {
201 out = ff_get_audio_buffer(outlink, in->nb_samples);
204 return AVERROR(ENOMEM);
206 av_frame_copy_props(out, in);
209 td.in = in; td.out = out;
210 ctx->internal->execute(ctx, s->filter_channels, &td, NULL, FFMIN(inlink->channels,
211 ff_filter_get_nb_threads(ctx)));
215 return ff_filter_frame(outlink, out);
218 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
219 char *res, int res_len, int flags)
223 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
227 return get_coeffs(ctx);
230 static av_cold void uninit(AVFilterContext *ctx)
232 ASuperCutContext *s = ctx->priv;
234 av_frame_free(&s->w);
237 #define OFFSET(x) offsetof(ASuperCutContext, x)
238 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
240 static const AVOption asupercut_options[] = {
241 { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
242 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
246 AVFILTER_DEFINE_CLASS(asupercut);
248 static const AVFilterPad inputs[] = {
251 .type = AVMEDIA_TYPE_AUDIO,
252 .filter_frame = filter_frame,
253 .config_props = config_input,
258 static const AVFilterPad outputs[] = {
261 .type = AVMEDIA_TYPE_AUDIO,
266 AVFilter ff_af_asupercut = {
268 .description = NULL_IF_CONFIG_SMALL("Cut super frequencies."),
269 .query_formats = query_formats,
270 .priv_size = sizeof(ASuperCutContext),
271 .priv_class = &asupercut_class,
275 .process_command = process_command,
276 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
277 AVFILTER_FLAG_SLICE_THREADS,