]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_asupercut.c
avfilter/af_acrossover: reduce memory usage
[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
36     int bypass;
37
38     BiquadCoeffs coeffs[5];
39
40     AVFrame *w;
41 } ASuperCutContext;
42
43 static int query_formats(AVFilterContext *ctx)
44 {
45     AVFilterFormats *formats = NULL;
46     AVFilterChannelLayouts *layouts = NULL;
47     static const enum AVSampleFormat sample_fmts[] = {
48         AV_SAMPLE_FMT_DBLP,
49         AV_SAMPLE_FMT_NONE
50     };
51     int ret;
52
53     formats = ff_make_format_list(sample_fmts);
54     if (!formats)
55         return AVERROR(ENOMEM);
56     ret = ff_set_common_formats(ctx, formats);
57     if (ret < 0)
58         return ret;
59
60     layouts = ff_all_channel_counts();
61     if (!layouts)
62         return AVERROR(ENOMEM);
63
64     ret = ff_set_common_channel_layouts(ctx, layouts);
65     if (ret < 0)
66         return ret;
67
68     formats = ff_all_samplerates();
69     return ff_set_common_samplerates(ctx, formats);
70 }
71
72 static int get_coeffs(AVFilterContext *ctx)
73 {
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);
78     double q[5];
79
80     s->bypass = w0 >= 0.5;
81     if (s->bypass)
82         return 0;
83
84     q[0] = 0.50623256;
85     q[1] = 0.56116312;
86     q[2] = 0.70710678;
87     q[3] = 1.10134463;
88     q[4] = 3.19622661;
89
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);
93
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;
99     }
100
101     return 0;
102 }
103
104 static int config_input(AVFilterLink *inlink)
105 {
106     AVFilterContext *ctx = inlink->dst;
107     ASuperCutContext *s = ctx->priv;
108
109     s->w = ff_get_audio_buffer(inlink, 2 * 5);
110     if (!s->w)
111         return AVERROR(ENOMEM);
112
113     return get_coeffs(ctx);
114 }
115
116 typedef struct ThreadData {
117     AVFrame *in, *out;
118 } ThreadData;
119
120 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
121 {
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;
128
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];
132
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;
141
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];
145
146                 w[0] = b1 * sin + w[1] + a1 * sout;
147                 w[1] = b2 * sin + a2 * sout;
148
149                 dst[n] = sout;
150             }
151         }
152     }
153
154     return 0;
155 }
156
157 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
158 {
159     AVFilterContext *ctx = inlink->dst;
160     ASuperCutContext *s = ctx->priv;
161     AVFilterLink *outlink = ctx->outputs[0];
162     ThreadData td;
163     AVFrame *out;
164
165     if (s->bypass)
166         return ff_filter_frame(outlink, in);
167
168     if (av_frame_is_writable(in)) {
169         out = in;
170     } else {
171         out = ff_get_audio_buffer(outlink, in->nb_samples);
172         if (!out) {
173             av_frame_free(&in);
174             return AVERROR(ENOMEM);
175         }
176         av_frame_copy_props(out, in);
177     }
178
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)));
182
183     if (out != in)
184         av_frame_free(&in);
185     return ff_filter_frame(outlink, out);
186 }
187
188 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
189                            char *res, int res_len, int flags)
190 {
191     int ret;
192
193     ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
194     if (ret < 0)
195         return ret;
196
197     return get_coeffs(ctx);
198 }
199
200 static av_cold void uninit(AVFilterContext *ctx)
201 {
202     ASuperCutContext *s = ctx->priv;
203
204     av_frame_free(&s->w);
205 }
206
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
209
210 static const AVOption asupercut_options[] = {
211     { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
212     { NULL }
213 };
214
215 AVFILTER_DEFINE_CLASS(asupercut);
216
217 static const AVFilterPad inputs[] = {
218     {
219         .name         = "default",
220         .type         = AVMEDIA_TYPE_AUDIO,
221         .filter_frame = filter_frame,
222         .config_props = config_input,
223     },
224     { NULL }
225 };
226
227 static const AVFilterPad outputs[] = {
228     {
229         .name = "default",
230         .type = AVMEDIA_TYPE_AUDIO,
231     },
232     { NULL }
233 };
234
235 AVFilter ff_af_asupercut = {
236     .name            = "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,
241     .uninit          = uninit,
242     .inputs          = inputs,
243     .outputs         = outputs,
244     .process_command = process_command,
245     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
246                        AVFILTER_FLAG_SLICE_THREADS,
247 };