]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_acrossover.c
Merge commit 'ca44fa5d7fda7e954f3ebfeb5b0d6d1be55fcaa3'
[ffmpeg] / libavfilter / af_acrossover.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 /**
20  * @file
21  * Crossover filter
22  *
23  * Split an audio stream into several bands.
24  */
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/opt.h"
32
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37
38 #define MAX_SPLITS 16
39 #define MAX_BANDS MAX_SPLITS + 1
40
41 typedef struct BiquadContext {
42     double a0, a1, a2;
43     double b1, b2;
44     double i1, i2;
45     double o1, o2;
46 } BiquadContext;
47
48 typedef struct CrossoverChannel {
49     BiquadContext lp[MAX_BANDS][4];
50     BiquadContext hp[MAX_BANDS][4];
51 } CrossoverChannel;
52
53 typedef struct AudioCrossoverContext {
54     const AVClass *class;
55
56     char *splits_str;
57     int order;
58
59     int filter_count;
60     int nb_splits;
61     float *splits;
62
63     CrossoverChannel *xover;
64 } AudioCrossoverContext;
65
66 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
67 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
68
69 static const AVOption acrossover_options[] = {
70     { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
71     { "order", "set order",             OFFSET(order),      AV_OPT_TYPE_INT,    {.i64=1},     0, 2, AF, "m" },
72     { "2nd",   "2nd order",             0,                  AV_OPT_TYPE_CONST,  {.i64=0},     0, 0, AF, "m" },
73     { "4th",   "4th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=1},     0, 0, AF, "m" },
74     { "8th",   "8th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=2},     0, 0, AF, "m" },
75     { NULL }
76 };
77
78 AVFILTER_DEFINE_CLASS(acrossover);
79
80 static av_cold int init(AVFilterContext *ctx)
81 {
82     AudioCrossoverContext *s = ctx->priv;
83     char *p, *arg, *saveptr = NULL;
84     int i, ret = 0;
85
86     s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
87     if (!s->splits)
88         return AVERROR(ENOMEM);
89
90     p = s->splits_str;
91     for (i = 0; i < MAX_SPLITS; i++) {
92         float freq;
93
94         if (!(arg = av_strtok(p, " |", &saveptr)))
95             break;
96
97         p = NULL;
98
99         av_sscanf(arg, "%f", &freq);
100         if (freq <= 0) {
101             av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
102             return AVERROR(EINVAL);
103         }
104
105         if (i > 0 && freq <= s->splits[i-1]) {
106             av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
107             return AVERROR(EINVAL);
108         }
109
110         s->splits[i] = freq;
111     }
112
113     s->nb_splits = i;
114
115     for (i = 0; i <= s->nb_splits; i++) {
116         AVFilterPad pad  = { 0 };
117         char *name;
118
119         pad.type = AVMEDIA_TYPE_AUDIO;
120         name = av_asprintf("out%d", ctx->nb_outputs);
121         if (!name)
122             return AVERROR(ENOMEM);
123         pad.name = name;
124
125         if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
126             av_freep(&pad.name);
127             return ret;
128         }
129     }
130
131     return ret;
132 }
133
134 static void set_lp(BiquadContext *b, float fc, float q, float sr)
135 {
136     double omega = (2.0 * M_PI * fc / sr);
137     double sn = sin(omega);
138     double cs = cos(omega);
139     double alpha = (sn / (2 * q));
140     double inv = (1.0 / (1.0 + alpha));
141
142     b->a2 = b->a0 = (inv * (1.0 - cs) * 0.5);
143     b->a1 = b->a0 + b->a0;
144     b->b1 = -2. * cs * inv;
145     b->b2 = (1. - alpha) * inv;
146 }
147
148 static void set_hp(BiquadContext *b, float fc, float q, float sr)
149 {
150     double omega = 2 * M_PI * fc / sr;
151     double sn = sin(omega);
152     double cs = cos(omega);
153     double alpha = sn / (2 * q);
154     double inv = 1.0 / (1.0 + alpha);
155
156     b->a0 = inv * (1. + cs) / 2.;
157     b->a1 = -2. * b->a0;
158     b->a2 = b->a0;
159     b->b1 = -2. * cs * inv;
160     b->b2 = (1. - alpha) * inv;
161 }
162
163 static int config_input(AVFilterLink *inlink)
164 {
165     AVFilterContext *ctx = inlink->dst;
166     AudioCrossoverContext *s = ctx->priv;
167     int ch, band, sample_rate = inlink->sample_rate;
168     double q;
169
170     s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
171     if (!s->xover)
172         return AVERROR(ENOMEM);
173
174     switch (s->order) {
175     case 0:
176         q = 0.5;
177         s->filter_count = 1;
178         break;
179     case 1:
180         q = M_SQRT1_2;
181         s->filter_count = 2;
182         break;
183     case 2:
184         q = 0.54;
185         s->filter_count = 4;
186         break;
187     }
188
189     for (ch = 0; ch < inlink->channels; ch++) {
190         for (band = 0; band <= s->nb_splits; band++) {
191             set_lp(&s->xover[ch].lp[band][0], s->splits[band], q, sample_rate);
192             set_hp(&s->xover[ch].hp[band][0], s->splits[band], q, sample_rate);
193
194             if (s->order > 1) {
195                 set_lp(&s->xover[ch].lp[band][1], s->splits[band], 1.34, sample_rate);
196                 set_hp(&s->xover[ch].hp[band][1], s->splits[band], 1.34, sample_rate);
197                 set_lp(&s->xover[ch].lp[band][2], s->splits[band],    q, sample_rate);
198                 set_hp(&s->xover[ch].hp[band][2], s->splits[band],    q, sample_rate);
199                 set_lp(&s->xover[ch].lp[band][3], s->splits[band], 1.34, sample_rate);
200                 set_hp(&s->xover[ch].hp[band][3], s->splits[band], 1.34, sample_rate);
201             } else {
202                 set_lp(&s->xover[ch].lp[band][1], s->splits[band], q, sample_rate);
203                 set_hp(&s->xover[ch].hp[band][1], s->splits[band], q, sample_rate);
204             }
205         }
206     }
207
208     return 0;
209 }
210
211 static int query_formats(AVFilterContext *ctx)
212 {
213     AVFilterFormats *formats;
214     AVFilterChannelLayouts *layouts;
215     static const enum AVSampleFormat sample_fmts[] = {
216         AV_SAMPLE_FMT_DBLP,
217         AV_SAMPLE_FMT_NONE
218     };
219     int ret;
220
221     layouts = ff_all_channel_counts();
222     if (!layouts)
223         return AVERROR(ENOMEM);
224     ret = ff_set_common_channel_layouts(ctx, layouts);
225     if (ret < 0)
226         return ret;
227
228     formats = ff_make_format_list(sample_fmts);
229     if (!formats)
230         return AVERROR(ENOMEM);
231     ret = ff_set_common_formats(ctx, formats);
232     if (ret < 0)
233         return ret;
234
235     formats = ff_all_samplerates();
236     if (!formats)
237         return AVERROR(ENOMEM);
238     return ff_set_common_samplerates(ctx, formats);
239 }
240
241 static double biquad_process(BiquadContext *b, double in)
242 {
243     double out = in * b->a0 + b->i1 * b->a1 + b->i2 * b->a2 - b->o1 * b->b1 - b->o2 * b->b2;
244
245     b->i2 = b->i1;
246     b->o2 = b->o1;
247     b->i1 = in;
248     b->o1 = out;
249
250     return out;
251 }
252
253 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
254 {
255     AVFilterContext *ctx = inlink->dst;
256     AudioCrossoverContext *s = ctx->priv;
257     AVFrame *frames[MAX_BANDS] = { NULL };
258     int i, f, ch, band, ret = 0;
259
260     for (i = 0; i < ctx->nb_outputs; i++) {
261         frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
262
263         if (!frames[i]) {
264             ret = AVERROR(ENOMEM);
265             break;
266         }
267
268         frames[i]->pts = in->pts;
269     }
270
271     if (ret < 0)
272         goto fail;
273
274     for (ch = 0; ch < inlink->channels; ch++) {
275         const double *src = (const double *)in->extended_data[ch];
276         CrossoverChannel *xover = &s->xover[ch];
277
278         for (band = 0; band < ctx->nb_outputs; band++) {
279             double *dst = (double *)frames[band]->extended_data[ch];
280
281             for (i = 0; i < in->nb_samples; i++) {
282                 dst[i] = src[i];
283
284                 for (f = 0; f < s->filter_count; f++) {
285                     if (band + 1 < ctx->nb_outputs) {
286                         BiquadContext *lp = &xover->lp[band][f];
287                         dst[i] = biquad_process(lp, dst[i]);
288                     }
289
290                     if (band - 1 >= 0) {
291                         BiquadContext *hp = &xover->hp[band - 1][f];
292                         dst[i] = biquad_process(hp, dst[i]);
293                     }
294                 }
295             }
296         }
297     }
298
299     for (i = 0; i < ctx->nb_outputs; i++) {
300         ret = ff_filter_frame(ctx->outputs[i], frames[i]);
301         if (ret < 0)
302             break;
303     }
304
305 fail:
306     av_frame_free(&in);
307
308     return ret;
309 }
310
311 static av_cold void uninit(AVFilterContext *ctx)
312 {
313     AudioCrossoverContext *s = ctx->priv;
314     int i;
315
316     av_freep(&s->splits);
317
318     for (i = 0; i < ctx->nb_outputs; i++)
319         av_freep(&ctx->output_pads[i].name);
320 }
321
322 static const AVFilterPad inputs[] = {
323     {
324         .name         = "default",
325         .type         = AVMEDIA_TYPE_AUDIO,
326         .filter_frame = filter_frame,
327         .config_props = config_input,
328     },
329     { NULL }
330 };
331
332 AVFilter ff_af_acrossover = {
333     .name           = "acrossover",
334     .description    = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
335     .priv_size      = sizeof(AudioCrossoverContext),
336     .priv_class     = &acrossover_class,
337     .init           = init,
338     .uninit         = uninit,
339     .query_formats  = query_formats,
340     .inputs         = inputs,
341     .outputs        = NULL,
342     .flags          = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
343 };