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