]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_acrossover.c
avfilter/af_acrossover: remove unecessary allpass calls
[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 = 0.5 * M_PI * fc / sr + M_PI_4;
215     double cosine = cos(omega);
216     double sine = sin(omega);
217
218     b->a1 = -cosine / sine;
219     b->a2 = 0.;
220     b->b0 = -b->a1;
221     b->b1 = 1.;
222     b->b2 = 0.;
223 }
224
225 static void calc_q_factors(int order, double *q)
226 {
227     double n = order / 2.;
228
229     for (int i = 0; i < n / 2; i++)
230         q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
231 }
232
233 static int query_formats(AVFilterContext *ctx)
234 {
235     AVFilterFormats *formats;
236     AVFilterChannelLayouts *layouts;
237     static const enum AVSampleFormat sample_fmts[] = {
238         AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
239         AV_SAMPLE_FMT_NONE
240     };
241     int ret;
242
243     layouts = ff_all_channel_counts();
244     if (!layouts)
245         return AVERROR(ENOMEM);
246     ret = ff_set_common_channel_layouts(ctx, layouts);
247     if (ret < 0)
248         return ret;
249
250     formats = ff_make_format_list(sample_fmts);
251     if (!formats)
252         return AVERROR(ENOMEM);
253     ret = ff_set_common_formats(ctx, formats);
254     if (ret < 0)
255         return ret;
256
257     formats = ff_all_samplerates();
258     if (!formats)
259         return AVERROR(ENOMEM);
260     return ff_set_common_samplerates(ctx, formats);
261 }
262
263 #define BIQUAD_PROCESS(name, type)                             \
264 static void biquad_process_## name(BiquadContext *b,           \
265                                    type *dst, const type *src, \
266                                    int nb_samples)             \
267 {                                                              \
268     const type b0 = b->b0;                                     \
269     const type b1 = b->b1;                                     \
270     const type b2 = b->b2;                                     \
271     const type a1 = b->a1;                                     \
272     const type a2 = b->a2;                                     \
273     type z1 = b->z1;                                           \
274     type z2 = b->z2;                                           \
275                                                                \
276     for (int n = 0; n < nb_samples; n++) {                     \
277         const type in = src[n];                                \
278         type out;                                              \
279                                                                \
280         out = in * b0 + z1;                                    \
281         z1 = b1 * in + z2 + a1 * out;                          \
282         z2 = b2 * in + a2 * out;                               \
283         dst[n] = out;                                          \
284     }                                                          \
285                                                                \
286     b->z1 = z1;                                                \
287     b->z2 = z2;                                                \
288 }
289
290 BIQUAD_PROCESS(fltp, float)
291 BIQUAD_PROCESS(dblp, double)
292
293 #define XOVER_PROCESS(name, type, one)                         \
294 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
295 {                                                                                           \
296     AudioCrossoverContext *s = ctx->priv;                                                   \
297     AVFrame *in = s->input_frame;                                                           \
298     AVFrame **frames = s->frames;                                                           \
299     const int start = (in->channels * jobnr) / nb_jobs;                                     \
300     const int end = (in->channels * (jobnr+1)) / nb_jobs;                                   \
301     const int nb_samples = in->nb_samples;                                                  \
302                                                                                             \
303     for (int ch = start; ch < end; ch++) {                                                  \
304         CrossoverChannel *xover = &s->xover[ch];                                            \
305                                                                                             \
306         for (int band = 0; band < ctx->nb_outputs; band++) {                                \
307             for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {       \
308                 const type *src = (const type *)in->extended_data[ch];                      \
309                 const type *prv = (const type *)frames[band]->extended_data[ch];            \
310                 type *dst = (type *)frames[band + 1]->extended_data[ch];                    \
311                 const type *hsrc = (band == 0 && f == 0) ? src : f == 0 ? prv : dst;        \
312                 BiquadContext *hp = &xover->hp[band][f];                                    \
313                                                                                             \
314                 biquad_process_## name(hp, dst, hsrc, nb_samples);                          \
315             }                                                                               \
316                                                                                             \
317             for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {       \
318                 const type *src = (const type *)in->extended_data[ch];                      \
319                 type *dst = (type *)frames[band]->extended_data[ch];                        \
320                 const type *lsrc = (band == 0 && f == 0) ? src : dst;                       \
321                 BiquadContext *lp = &xover->lp[band][f];                                    \
322                                                                                             \
323                 biquad_process_## name(lp, dst, lsrc, nb_samples);                          \
324             }                                                                               \
325                                                                                             \
326             for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) {              \
327                 if (s->first_order) {                                                       \
328                     const type *asrc = (const type *)frames[band]->extended_data[ch];       \
329                     type *dst = (type *)frames[band]->extended_data[ch];                    \
330                     BiquadContext *ap = &xover->ap[band][aband][0];                         \
331                                                                                             \
332                     biquad_process_## name(ap, dst, asrc, nb_samples);                      \
333                 }                                                                           \
334                                                                                             \
335                 for (int f = s->first_order; f < s->ap_filter_count; f++) {                 \
336                     const type *asrc = (const type *)frames[band]->extended_data[ch];       \
337                     type *dst = (type *)frames[band]->extended_data[ch];                    \
338                     BiquadContext *ap = &xover->ap[band][aband][f];                         \
339                                                                                             \
340                     biquad_process_## name(ap, dst, asrc, nb_samples);                      \
341                 }                                                                           \
342             }                                                                               \
343         }                                                                                   \
344                                                                                             \
345         for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) {              \
346             if (band & 1) {                                                                 \
347                 type *dst = (type *)frames[band]->extended_data[ch];                        \
348                                                                                             \
349                 for (int n = 0; n < nb_samples; n++)                                        \
350                     dst[n] *= -one;                                                         \
351             }                                                                               \
352         }                                                                                   \
353     }                                                                                       \
354                                                                                             \
355     return 0;                                                                               \
356 }
357
358 XOVER_PROCESS(fltp, float, 1.f)
359 XOVER_PROCESS(dblp, double, 1.0)
360
361 static int config_input(AVFilterLink *inlink)
362 {
363     AVFilterContext *ctx = inlink->dst;
364     AudioCrossoverContext *s = ctx->priv;
365     int sample_rate = inlink->sample_rate;
366     double q[16];
367
368     s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
369     if (!s->xover)
370         return AVERROR(ENOMEM);
371
372     s->order = (s->order_opt + 1) * 2;
373     s->filter_count = s->order / 2;
374     s->first_order = s->filter_count & 1;
375     s->ap_filter_count = s->filter_count / 2 + s->first_order;
376     calc_q_factors(s->order, q);
377
378     for (int ch = 0; ch < inlink->channels; ch++) {
379         for (int band = 0; band <= s->nb_splits; band++) {
380             if (s->first_order) {
381                 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
382                 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
383             }
384
385             for (int n = s->first_order; n < s->filter_count; n++) {
386                 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
387
388                 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
389                 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);
390             }
391
392             for (int x = 0; x <= s->nb_splits && s->first_order; x++)
393                 set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate);
394
395             for (int n = s->first_order; n < s->ap_filter_count; n++) {
396                 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
397
398                 for (int x = 0; x <= s->nb_splits; x++)
399                     set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
400             }
401         }
402     }
403
404     switch (inlink->format) {
405     case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
406     case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
407     }
408
409     return 0;
410 }
411
412 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
413 {
414     AVFilterContext *ctx = inlink->dst;
415     AudioCrossoverContext *s = ctx->priv;
416     AVFrame **frames = s->frames;
417     int i, ret = 0;
418
419     for (i = 0; i < ctx->nb_outputs; i++) {
420         frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
421
422         if (!frames[i]) {
423             ret = AVERROR(ENOMEM);
424             break;
425         }
426
427         frames[i]->pts = in->pts;
428     }
429
430     if (ret < 0)
431         goto fail;
432
433     s->input_frame = in;
434     ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
435                                                                       ff_filter_get_nb_threads(ctx)));
436
437     for (i = 0; i < ctx->nb_outputs; i++) {
438         ret = ff_filter_frame(ctx->outputs[i], frames[i]);
439         frames[i] = NULL;
440         if (ret < 0)
441             break;
442     }
443
444 fail:
445     for (i = 0; i < ctx->nb_outputs; i++)
446         av_frame_free(&frames[i]);
447     av_frame_free(&in);
448     s->input_frame = NULL;
449
450     return ret;
451 }
452
453 static av_cold void uninit(AVFilterContext *ctx)
454 {
455     AudioCrossoverContext *s = ctx->priv;
456     int i;
457
458     av_freep(&s->splits);
459     av_freep(&s->xover);
460
461     for (i = 0; i < ctx->nb_outputs; i++)
462         av_freep(&ctx->output_pads[i].name);
463 }
464
465 static const AVFilterPad inputs[] = {
466     {
467         .name         = "default",
468         .type         = AVMEDIA_TYPE_AUDIO,
469         .filter_frame = filter_frame,
470         .config_props = config_input,
471     },
472     { NULL }
473 };
474
475 AVFilter ff_af_acrossover = {
476     .name           = "acrossover",
477     .description    = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
478     .priv_size      = sizeof(AudioCrossoverContext),
479     .priv_class     = &acrossover_class,
480     .init           = init,
481     .uninit         = uninit,
482     .query_formats  = query_formats,
483     .inputs         = inputs,
484     .outputs        = NULL,
485     .flags          = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
486                       AVFILTER_FLAG_SLICE_THREADS,
487 };