]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_acrossover.c
avfilter/af_acrossover: really fix single-pole allpass coefficients
[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 b0, b1, b2;
43     double a1, a2;
44     double z1, z2;
45 } BiquadContext;
46
47 typedef struct CrossoverChannel {
48     BiquadContext lp[MAX_BANDS][20];
49     BiquadContext hp[MAX_BANDS][20];
50     BiquadContext ap[MAX_BANDS][MAX_BANDS][20];
51 } CrossoverChannel;
52
53 typedef struct AudioCrossoverContext {
54     const AVClass *class;
55
56     char *splits_str;
57     int order_opt;
58
59     int order;
60     int filter_count;
61     int first_order;
62     int ap_filter_count;
63     int nb_splits;
64     float *splits;
65
66     CrossoverChannel *xover;
67
68     AVFrame *input_frame;
69     AVFrame *frames[MAX_BANDS];
70
71     int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
72 } AudioCrossoverContext;
73
74 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
75 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
76
77 static const AVOption acrossover_options[] = {
78     { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
79     { "order", "set order",             OFFSET(order_opt),  AV_OPT_TYPE_INT,    {.i64=1},     0, 9, AF, "m" },
80     { "2nd",   "2nd order",             0,                  AV_OPT_TYPE_CONST,  {.i64=0},     0, 0, AF, "m" },
81     { "4th",   "4th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=1},     0, 0, AF, "m" },
82     { "6th",   "6th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=2},     0, 0, AF, "m" },
83     { "8th",   "8th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=3},     0, 0, AF, "m" },
84     { "10th",  "10th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=4},     0, 0, AF, "m" },
85     { "12th",  "12th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=5},     0, 0, AF, "m" },
86     { "14th",  "14th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=6},     0, 0, AF, "m" },
87     { "16th",  "16th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=7},     0, 0, AF, "m" },
88     { "18th",  "18th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=8},     0, 0, AF, "m" },
89     { "20th",  "20th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=9},     0, 0, AF, "m" },
90     { NULL }
91 };
92
93 AVFILTER_DEFINE_CLASS(acrossover);
94
95 static av_cold int init(AVFilterContext *ctx)
96 {
97     AudioCrossoverContext *s = ctx->priv;
98     char *p, *arg, *saveptr = NULL;
99     int i, ret = 0;
100
101     s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
102     if (!s->splits)
103         return AVERROR(ENOMEM);
104
105     p = s->splits_str;
106     for (i = 0; i < MAX_SPLITS; i++) {
107         float freq;
108
109         if (!(arg = av_strtok(p, " |", &saveptr)))
110             break;
111
112         p = NULL;
113
114         if (av_sscanf(arg, "%f", &freq) != 1) {
115             av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
116             return AVERROR(EINVAL);
117         }
118         if (freq <= 0) {
119             av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
120             return AVERROR(EINVAL);
121         }
122
123         if (i > 0 && freq <= s->splits[i-1]) {
124             av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
125             return AVERROR(EINVAL);
126         }
127
128         s->splits[i] = freq;
129     }
130
131     s->nb_splits = i;
132
133     for (i = 0; i <= s->nb_splits; i++) {
134         AVFilterPad pad  = { 0 };
135         char *name;
136
137         pad.type = AVMEDIA_TYPE_AUDIO;
138         name = av_asprintf("out%d", ctx->nb_outputs);
139         if (!name)
140             return AVERROR(ENOMEM);
141         pad.name = name;
142
143         if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
144             av_freep(&pad.name);
145             return ret;
146         }
147     }
148
149     return ret;
150 }
151
152 static void set_lp(BiquadContext *b, double fc, double q, double sr)
153 {
154     double omega = M_PI * fc / sr;
155     double cosine = cos(omega);
156     double alpha = sin(omega) / (2. * q);
157
158     double b0 = (1. - cosine) / 2.;
159     double b1 = 1. - cosine;
160     double b2 = (1. - cosine) / 2.;
161     double a0 = 1. + alpha;
162     double a1 = -2. * cosine;
163     double a2 = 1. - alpha;
164
165     b->b0 =  b0 / a0;
166     b->b1 =  b1 / a0;
167     b->b2 =  b2 / a0;
168     b->a1 = -a1 / a0;
169     b->a2 = -a2 / a0;
170 }
171
172 static void set_hp(BiquadContext *b, double fc, double q, double sr)
173 {
174     double omega = M_PI * fc / sr;
175     double cosine = cos(omega);
176     double alpha = sin(omega) / (2. * q);
177
178     double b0 = (1. + cosine) / 2.;
179     double b1 = -1. - cosine;
180     double b2 = (1. + cosine) / 2.;
181     double a0 = 1. + alpha;
182     double a1 = -2. * cosine;
183     double a2 = 1. - alpha;
184
185     b->b0 =  b0 / a0;
186     b->b1 =  b1 / a0;
187     b->b2 =  b2 / a0;
188     b->a1 = -a1 / a0;
189     b->a2 = -a2 / a0;
190 }
191
192 static void set_ap(BiquadContext *b, double fc, double q, double sr)
193 {
194     double omega = M_PI * fc / sr;
195     double cosine = cos(omega);
196     double alpha = sin(omega) / (2. * q);
197
198     double a0 = 1. + alpha;
199     double a1 = -2. * cosine;
200     double a2 = 1. - alpha;
201     double b0 = a2;
202     double b1 = a1;
203     double b2 = a0;
204
205     b->b0 =  b0 / a0;
206     b->b1 =  b1 / a0;
207     b->b2 =  b2 / a0;
208     b->a1 = -a1 / a0;
209     b->a2 = -a2 / a0;
210 }
211
212 static void set_ap1(BiquadContext *b, double fc, double sr)
213 {
214     double omega = M_PI * fc / sr;
215
216     b->a1 = exp(-omega);
217     b->a2 = 0.;
218     b->b0 = -b->a1;
219     b->b1 = 1.;
220     b->b2 = 0.;
221 }
222
223 static void calc_q_factors(int order, double *q)
224 {
225     double n = order / 2.;
226
227     for (int i = 0; i < n / 2; i++)
228         q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
229 }
230
231 static int query_formats(AVFilterContext *ctx)
232 {
233     AVFilterFormats *formats;
234     AVFilterChannelLayouts *layouts;
235     static const enum AVSampleFormat sample_fmts[] = {
236         AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
237         AV_SAMPLE_FMT_NONE
238     };
239     int ret;
240
241     layouts = ff_all_channel_counts();
242     if (!layouts)
243         return AVERROR(ENOMEM);
244     ret = ff_set_common_channel_layouts(ctx, layouts);
245     if (ret < 0)
246         return ret;
247
248     formats = ff_make_format_list(sample_fmts);
249     if (!formats)
250         return AVERROR(ENOMEM);
251     ret = ff_set_common_formats(ctx, formats);
252     if (ret < 0)
253         return ret;
254
255     formats = ff_all_samplerates();
256     if (!formats)
257         return AVERROR(ENOMEM);
258     return ff_set_common_samplerates(ctx, formats);
259 }
260
261 #define BIQUAD_PROCESS(name, type)                             \
262 static void biquad_process_## name(BiquadContext *b,           \
263                                    type *dst, const type *src, \
264                                    int nb_samples)             \
265 {                                                              \
266     const type b0 = b->b0;                                     \
267     const type b1 = b->b1;                                     \
268     const type b2 = b->b2;                                     \
269     const type a1 = b->a1;                                     \
270     const type a2 = b->a2;                                     \
271     type z1 = b->z1;                                           \
272     type z2 = b->z2;                                           \
273                                                                \
274     for (int n = 0; n < nb_samples; n++) {                     \
275         const type in = src[n];                                \
276         type out;                                              \
277                                                                \
278         out = in * b0 + z1;                                    \
279         z1 = b1 * in + z2 + a1 * out;                          \
280         z2 = b2 * in + a2 * out;                               \
281         dst[n] = out;                                          \
282     }                                                          \
283                                                                \
284     b->z1 = z1;                                                \
285     b->z2 = z2;                                                \
286 }
287
288 BIQUAD_PROCESS(fltp, float)
289 BIQUAD_PROCESS(dblp, double)
290
291 #define XOVER_PROCESS(name, type, one)                         \
292 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
293 {                                                                                           \
294     AudioCrossoverContext *s = ctx->priv;                                                   \
295     AVFrame *in = s->input_frame;                                                           \
296     AVFrame **frames = s->frames;                                                           \
297     const int start = (in->channels * jobnr) / nb_jobs;                                     \
298     const int end = (in->channels * (jobnr+1)) / nb_jobs;                                   \
299     const int nb_samples = in->nb_samples;                                                  \
300                                                                                             \
301     for (int ch = start; ch < end; ch++) {                                                  \
302         CrossoverChannel *xover = &s->xover[ch];                                            \
303                                                                                             \
304         for (int band = 0; band < ctx->nb_outputs; band++) {                                \
305             for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {       \
306                 const type *src = (const type *)in->extended_data[ch];                      \
307                 const type *prv = (const type *)frames[band]->extended_data[ch];            \
308                 type *dst = (type *)frames[band + 1]->extended_data[ch];                    \
309                 const type *hsrc = (band == 0 && f == 0) ? src : f == 0 ? prv : dst;        \
310                 BiquadContext *hp = &xover->hp[band][f];                                    \
311                                                                                             \
312                 biquad_process_## name(hp, dst, hsrc, nb_samples);                          \
313             }                                                                               \
314                                                                                             \
315             for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {       \
316                 const type *src = (const type *)in->extended_data[ch];                      \
317                 type *dst = (type *)frames[band]->extended_data[ch];                        \
318                 const type *lsrc = (band == 0 && f == 0) ? src : dst;                       \
319                 BiquadContext *lp = &xover->lp[band][f];                                    \
320                                                                                             \
321                 biquad_process_## name(lp, dst, lsrc, nb_samples);                          \
322             }                                                                               \
323                                                                                             \
324             for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) {              \
325                 if (s->first_order) {                                                       \
326                     const type *asrc = (const type *)frames[band]->extended_data[ch];       \
327                     type *dst = (type *)frames[band]->extended_data[ch];                    \
328                     BiquadContext *ap = &xover->ap[band][aband][0];                         \
329                                                                                             \
330                     biquad_process_## name(ap, dst, asrc, nb_samples);                      \
331                 }                                                                           \
332                                                                                             \
333                 for (int f = s->first_order; f < s->ap_filter_count; f++) {                 \
334                     const type *asrc = (const type *)frames[band]->extended_data[ch];       \
335                     type *dst = (type *)frames[band]->extended_data[ch];                    \
336                     BiquadContext *ap = &xover->ap[band][aband][f];                         \
337                                                                                             \
338                     biquad_process_## name(ap, dst, asrc, nb_samples);                      \
339                 }                                                                           \
340             }                                                                               \
341         }                                                                                   \
342                                                                                             \
343         for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) {              \
344             if (band & 1) {                                                                 \
345                 type *dst = (type *)frames[band]->extended_data[ch];                        \
346                                                                                             \
347                 for (int n = 0; n < nb_samples; n++)                                        \
348                     dst[n] *= -one;                                                         \
349             }                                                                               \
350         }                                                                                   \
351     }                                                                                       \
352                                                                                             \
353     return 0;                                                                               \
354 }
355
356 XOVER_PROCESS(fltp, float, 1.f)
357 XOVER_PROCESS(dblp, double, 1.0)
358
359 static int config_input(AVFilterLink *inlink)
360 {
361     AVFilterContext *ctx = inlink->dst;
362     AudioCrossoverContext *s = ctx->priv;
363     int sample_rate = inlink->sample_rate;
364     double q[16];
365
366     s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
367     if (!s->xover)
368         return AVERROR(ENOMEM);
369
370     s->order = (s->order_opt + 1) * 2;
371     s->filter_count = s->order / 2;
372     s->first_order = s->filter_count & 1;
373     s->ap_filter_count = s->filter_count / 2 + s->first_order;
374     calc_q_factors(s->order, q);
375
376     for (int ch = 0; ch < inlink->channels; ch++) {
377         for (int band = 0; band <= s->nb_splits; band++) {
378             if (s->first_order) {
379                 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
380                 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
381             }
382
383             for (int n = s->first_order; n < s->filter_count; n++) {
384                 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
385
386                 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
387                 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);
388             }
389
390             for (int x = 0; x <= s->nb_splits && s->first_order; x++)
391                 set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate);
392
393             for (int n = s->first_order; n < s->ap_filter_count; n++) {
394                 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
395
396                 for (int x = 0; x <= s->nb_splits; x++)
397                     set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
398             }
399         }
400     }
401
402     switch (inlink->format) {
403     case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
404     case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
405     }
406
407     return 0;
408 }
409
410 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
411 {
412     AVFilterContext *ctx = inlink->dst;
413     AudioCrossoverContext *s = ctx->priv;
414     AVFrame **frames = s->frames;
415     int i, ret = 0;
416
417     for (i = 0; i < ctx->nb_outputs; i++) {
418         frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
419
420         if (!frames[i]) {
421             ret = AVERROR(ENOMEM);
422             break;
423         }
424
425         frames[i]->pts = in->pts;
426     }
427
428     if (ret < 0)
429         goto fail;
430
431     s->input_frame = in;
432     ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
433                                                                       ff_filter_get_nb_threads(ctx)));
434
435     for (i = 0; i < ctx->nb_outputs; i++) {
436         ret = ff_filter_frame(ctx->outputs[i], frames[i]);
437         frames[i] = NULL;
438         if (ret < 0)
439             break;
440     }
441
442 fail:
443     for (i = 0; i < ctx->nb_outputs; i++)
444         av_frame_free(&frames[i]);
445     av_frame_free(&in);
446     s->input_frame = NULL;
447
448     return ret;
449 }
450
451 static av_cold void uninit(AVFilterContext *ctx)
452 {
453     AudioCrossoverContext *s = ctx->priv;
454     int i;
455
456     av_freep(&s->splits);
457     av_freep(&s->xover);
458
459     for (i = 0; i < ctx->nb_outputs; i++)
460         av_freep(&ctx->output_pads[i].name);
461 }
462
463 static const AVFilterPad inputs[] = {
464     {
465         .name         = "default",
466         .type         = AVMEDIA_TYPE_AUDIO,
467         .filter_frame = filter_frame,
468         .config_props = config_input,
469     },
470     { NULL }
471 };
472
473 AVFilter ff_af_acrossover = {
474     .name           = "acrossover",
475     .description    = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
476     .priv_size      = sizeof(AudioCrossoverContext),
477     .priv_class     = &acrossover_class,
478     .init           = init,
479     .uninit         = uninit,
480     .query_formats  = query_formats,
481     .inputs         = inputs,
482     .outputs        = NULL,
483     .flags          = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
484                       AVFILTER_FLAG_SLICE_THREADS,
485 };