]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_asupercut.c
avfilter/af_asupercut: add float sample format support
[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
44     int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
45 } ASuperCutContext;
46
47 static int query_formats(AVFilterContext *ctx)
48 {
49     AVFilterFormats *formats = NULL;
50     AVFilterChannelLayouts *layouts = NULL;
51     static const enum AVSampleFormat sample_fmts[] = {
52         AV_SAMPLE_FMT_FLTP,
53         AV_SAMPLE_FMT_DBLP,
54         AV_SAMPLE_FMT_NONE
55     };
56     int ret;
57
58     formats = ff_make_format_list(sample_fmts);
59     if (!formats)
60         return AVERROR(ENOMEM);
61     ret = ff_set_common_formats(ctx, formats);
62     if (ret < 0)
63         return ret;
64
65     layouts = ff_all_channel_counts();
66     if (!layouts)
67         return AVERROR(ENOMEM);
68
69     ret = ff_set_common_channel_layouts(ctx, layouts);
70     if (ret < 0)
71         return ret;
72
73     formats = ff_all_samplerates();
74     return ff_set_common_samplerates(ctx, formats);
75 }
76
77 static void calc_q_factors(int n, double *q)
78 {
79     for (int i = 0; i < n / 2; i++)
80         q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
81 }
82
83 static int get_coeffs(AVFilterContext *ctx)
84 {
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);
89     double q[10];
90
91     s->bypass = w0 >= 0.5;
92     if (s->bypass)
93         return 0;
94
95     s->filter_count = s->order / 2 + (s->order & 1);
96     calc_q_factors(s->order, q);
97
98     if (s->order & 1) {
99         BiquadCoeffs *coeffs = &s->coeffs[0];
100         double omega = 2. * tan(M_PI * w0);
101
102         coeffs->b0 = omega / (2. + omega);
103         coeffs->b1 = coeffs->b0;
104         coeffs->b2 = 0.;
105         coeffs->a1 = -(omega - 2.) / (2. + omega);
106         coeffs->a2 = 0.;
107     }
108
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);
113
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;
119     }
120
121     return 0;
122 }
123
124 typedef struct ThreadData {
125     AVFrame *in, *out;
126 } ThreadData;
127
128 #define FILTER(name, type)                                          \
129 static int filter_channels_## name(AVFilterContext *ctx, void *arg, \
130                                    int jobnr, int nb_jobs)          \
131 {                                                                   \
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;           \
138                                                                     \
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];                 \
142                                                                     \
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;    \
151                                                                     \
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];                        \
155                                                                     \
156                 w[0] = b1 * sin + w[1] + a1 * sout;                 \
157                 w[1] = b2 * sin + a2 * sout;                        \
158                                                                     \
159                 dst[n] = sout;                                      \
160             }                                                       \
161         }                                                           \
162     }                                                               \
163                                                                     \
164     return 0;                                                       \
165 }
166
167 FILTER(fltp, float)
168 FILTER(dblp, double)
169
170 static int config_input(AVFilterLink *inlink)
171 {
172     AVFilterContext *ctx = inlink->dst;
173     ASuperCutContext *s = ctx->priv;
174
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;
178     }
179
180     s->w = ff_get_audio_buffer(inlink, 2 * 10);
181     if (!s->w)
182         return AVERROR(ENOMEM);
183
184     return get_coeffs(ctx);
185 }
186
187 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
188 {
189     AVFilterContext *ctx = inlink->dst;
190     ASuperCutContext *s = ctx->priv;
191     AVFilterLink *outlink = ctx->outputs[0];
192     ThreadData td;
193     AVFrame *out;
194
195     if (s->bypass)
196         return ff_filter_frame(outlink, in);
197
198     if (av_frame_is_writable(in)) {
199         out = in;
200     } else {
201         out = ff_get_audio_buffer(outlink, in->nb_samples);
202         if (!out) {
203             av_frame_free(&in);
204             return AVERROR(ENOMEM);
205         }
206         av_frame_copy_props(out, in);
207     }
208
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)));
212
213     if (out != in)
214         av_frame_free(&in);
215     return ff_filter_frame(outlink, out);
216 }
217
218 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
219                            char *res, int res_len, int flags)
220 {
221     int ret;
222
223     ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
224     if (ret < 0)
225         return ret;
226
227     return get_coeffs(ctx);
228 }
229
230 static av_cold void uninit(AVFilterContext *ctx)
231 {
232     ASuperCutContext *s = ctx->priv;
233
234     av_frame_free(&s->w);
235 }
236
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
239
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 },
243     { NULL }
244 };
245
246 AVFILTER_DEFINE_CLASS(asupercut);
247
248 static const AVFilterPad inputs[] = {
249     {
250         .name         = "default",
251         .type         = AVMEDIA_TYPE_AUDIO,
252         .filter_frame = filter_frame,
253         .config_props = config_input,
254     },
255     { NULL }
256 };
257
258 static const AVFilterPad outputs[] = {
259     {
260         .name = "default",
261         .type = AVMEDIA_TYPE_AUDIO,
262     },
263     { NULL }
264 };
265
266 AVFilter ff_af_asupercut = {
267     .name            = "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,
272     .uninit          = uninit,
273     .inputs          = inputs,
274     .outputs         = outputs,
275     .process_command = process_command,
276     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
277                        AVFILTER_FLAG_SLICE_THREADS,
278 };