2 * This file is part of FFmpeg.
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.
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.
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
23 * Split an audio stream into several bands.
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"
40 #define MAX_BANDS MAX_SPLITS + 1
48 typedef struct BiquadCoeffs {
53 typedef struct AudioCrossoverContext {
66 float splits[MAX_SPLITS];
68 float gains[MAX_BANDS];
70 BiquadCoeffs lp[MAX_BANDS][20];
71 BiquadCoeffs hp[MAX_BANDS][20];
72 BiquadCoeffs ap[MAX_BANDS][20];
77 AVFrame *frames[MAX_BANDS];
79 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
81 AVFloatDSPContext *fdsp;
82 } AudioCrossoverContext;
84 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
85 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
87 static const AVOption acrossover_options[] = {
88 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
89 { "order", "set filter order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
90 { "2nd", "2nd order (12 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
91 { "4th", "4th order (24 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
92 { "6th", "6th order (36 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
93 { "8th", "8th order (48 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
94 { "10th", "10th order (60 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
95 { "12th", "12th order (72 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
96 { "14th", "14th order (84 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
97 { "16th", "16th order (96 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
98 { "18th", "18th order (108 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
99 { "20th", "20th order (120 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
100 { "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
101 { "gain", "set output bands gain", OFFSET(gains_str), AV_OPT_TYPE_STRING, {.str="1.f"}, 0, 0, AF },
105 AVFILTER_DEFINE_CLASS(acrossover);
107 static int parse_gains(AVFilterContext *ctx)
109 AudioCrossoverContext *s = ctx->priv;
110 char *p, *arg, *saveptr = NULL;
115 for (i = 0; i < MAX_BANDS; i++) {
119 if (!(arg = av_strtok(p, " |", &saveptr)))
124 if (av_sscanf(arg, "%f%2s", &gain, c) < 1) {
125 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for gain[%d].\n", i);
126 ret = AVERROR(EINVAL);
130 if (c[0] == 'd' && c[1] == 'B')
131 s->gains[i] = expf(gain * M_LN10 / 20.f);
136 for (; i < MAX_BANDS; i++)
142 static av_cold int init(AVFilterContext *ctx)
144 AudioCrossoverContext *s = ctx->priv;
145 char *p, *arg, *saveptr = NULL;
148 s->fdsp = avpriv_float_dsp_alloc(0);
150 return AVERROR(ENOMEM);
153 for (i = 0; i < MAX_SPLITS; i++) {
156 if (!(arg = av_strtok(p, " |", &saveptr)))
161 if (av_sscanf(arg, "%f", &freq) != 1) {
162 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
163 return AVERROR(EINVAL);
166 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
167 return AVERROR(EINVAL);
170 if (i > 0 && freq <= s->splits[i-1]) {
171 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
172 return AVERROR(EINVAL);
180 ret = parse_gains(ctx);
184 for (i = 0; i <= s->nb_splits; i++) {
185 AVFilterPad pad = { 0 };
188 pad.type = AVMEDIA_TYPE_AUDIO;
189 name = av_asprintf("out%d", ctx->nb_outputs);
191 return AVERROR(ENOMEM);
194 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
203 static void set_lp(BiquadCoeffs *b, double fc, double q, double sr)
205 double omega = 2. * M_PI * fc / sr;
206 double cosine = cos(omega);
207 double alpha = sin(omega) / (2. * q);
209 double b0 = (1. - cosine) / 2.;
210 double b1 = 1. - cosine;
211 double b2 = (1. - cosine) / 2.;
212 double a0 = 1. + alpha;
213 double a1 = -2. * cosine;
214 double a2 = 1. - alpha;
219 b->cd[A1] = -a1 / a0;
220 b->cd[A2] = -a2 / a0;
222 b->cf[B0] = b->cd[B0];
223 b->cf[B1] = b->cd[B1];
224 b->cf[B2] = b->cd[B2];
225 b->cf[A1] = b->cd[A1];
226 b->cf[A2] = b->cd[A2];
229 static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
231 double omega = 2. * M_PI * fc / sr;
232 double cosine = cos(omega);
233 double alpha = sin(omega) / (2. * q);
235 double b0 = (1. + cosine) / 2.;
236 double b1 = -1. - cosine;
237 double b2 = (1. + cosine) / 2.;
238 double a0 = 1. + alpha;
239 double a1 = -2. * cosine;
240 double a2 = 1. - alpha;
245 b->cd[A1] = -a1 / a0;
246 b->cd[A2] = -a2 / a0;
248 b->cf[B0] = b->cd[B0];
249 b->cf[B1] = b->cd[B1];
250 b->cf[B2] = b->cd[B2];
251 b->cf[A1] = b->cd[A1];
252 b->cf[A2] = b->cd[A2];
255 static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
257 double omega = 2. * M_PI * fc / sr;
258 double cosine = cos(omega);
259 double alpha = sin(omega) / (2. * q);
261 double a0 = 1. + alpha;
262 double a1 = -2. * cosine;
263 double a2 = 1. - alpha;
271 b->cd[A1] = -a1 / a0;
272 b->cd[A2] = -a2 / a0;
274 b->cf[B0] = b->cd[B0];
275 b->cf[B1] = b->cd[B1];
276 b->cf[B2] = b->cd[B2];
277 b->cf[A1] = b->cd[A1];
278 b->cf[A2] = b->cd[A2];
281 static void set_ap1(BiquadCoeffs *b, double fc, double sr)
283 double omega = 2. * M_PI * fc / sr;
285 b->cd[A1] = exp(-omega);
287 b->cd[B0] = -b->cd[A1];
291 b->cf[B0] = b->cd[B0];
292 b->cf[B1] = b->cd[B1];
293 b->cf[B2] = b->cd[B2];
294 b->cf[A1] = b->cd[A1];
295 b->cf[A2] = b->cd[A2];
298 static void calc_q_factors(int order, double *q)
300 double n = order / 2.;
302 for (int i = 0; i < n / 2; i++)
303 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
306 static int query_formats(AVFilterContext *ctx)
308 AVFilterFormats *formats;
309 AVFilterChannelLayouts *layouts;
310 static const enum AVSampleFormat sample_fmts[] = {
311 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
316 layouts = ff_all_channel_counts();
318 return AVERROR(ENOMEM);
319 ret = ff_set_common_channel_layouts(ctx, layouts);
323 formats = ff_make_format_list(sample_fmts);
325 return AVERROR(ENOMEM);
326 ret = ff_set_common_formats(ctx, formats);
330 formats = ff_all_samplerates();
332 return AVERROR(ENOMEM);
333 return ff_set_common_samplerates(ctx, formats);
336 #define BIQUAD_PROCESS(name, type) \
337 static void biquad_process_## name(const type *const c, \
339 type *dst, const type *src, \
342 const type b0 = c[B0]; \
343 const type b1 = c[B1]; \
344 const type b2 = c[B2]; \
345 const type a1 = c[A1]; \
346 const type a2 = c[A2]; \
350 for (int n = 0; n + 1 < nb_samples; n++) { \
354 out = in * b0 + z1; \
355 z1 = b1 * in + z2 + a1 * out; \
356 z2 = b2 * in + a2 * out; \
361 out = in * b0 + z1; \
362 z1 = b1 * in + z2 + a1 * out; \
363 z2 = b2 * in + a2 * out; \
367 if (nb_samples & 1) { \
368 const int n = nb_samples - 1; \
369 const type in = src[n]; \
372 out = in * b0 + z1; \
373 z1 = b1 * in + z2 + a1 * out; \
374 z2 = b2 * in + a2 * out; \
382 BIQUAD_PROCESS(fltp, float)
383 BIQUAD_PROCESS(dblp, double)
385 #define XOVER_PROCESS(name, type, one, ff) \
386 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
388 AudioCrossoverContext *s = ctx->priv; \
389 AVFrame *in = s->input_frame; \
390 AVFrame **frames = s->frames; \
391 const int start = (in->channels * jobnr) / nb_jobs; \
392 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
393 const int nb_samples = in->nb_samples; \
394 const int nb_outs = ctx->nb_outputs; \
395 const int first_order = s->first_order; \
397 for (int ch = start; ch < end; ch++) { \
398 const type *src = (const type *)in->extended_data[ch]; \
399 type *xover = (type *)s->xover->extended_data[ch]; \
401 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
402 s->level_in, FFALIGN(nb_samples, sizeof(type))); \
404 for (int band = 0; band < nb_outs; band++) { \
405 for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
406 const type *prv = (const type *)frames[band]->extended_data[ch]; \
407 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
408 const type *hsrc = f == 0 ? prv : dst; \
409 type *hp = xover + nb_outs * 20 + band * 20 + f * 2; \
410 const type *const hpc = (type *)&s->hp[band][f].c ## ff; \
412 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
415 for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
416 type *dst = (type *)frames[band]->extended_data[ch]; \
417 const type *lsrc = dst; \
418 type *lp = xover + band * 20 + f * 2; \
419 const type *const lpc = (type *)&s->lp[band][f].c ## ff; \
421 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
424 for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \
426 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
427 type *dst = (type *)frames[band]->extended_data[ch]; \
428 type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \
429 const type *const apc = (type *)&s->ap[aband][0].c ## ff; \
431 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
434 for (int f = first_order; f < s->ap_filter_count; f++) { \
435 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
436 type *dst = (type *)frames[band]->extended_data[ch]; \
437 type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\
438 const type *const apc = (type *)&s->ap[aband][f].c ## ff; \
440 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
445 for (int band = 0; band < nb_outs; band++) { \
446 const type gain = s->gains[band] * ((band & 1 && first_order) ? -one : one); \
447 type *dst = (type *)frames[band]->extended_data[ch]; \
449 s->fdsp->vector_## ff ##mul_scalar(dst, dst, gain, \
450 FFALIGN(nb_samples, sizeof(type))); \
457 XOVER_PROCESS(fltp, float, 1.f, f)
458 XOVER_PROCESS(dblp, double, 1.0, d)
460 static int config_input(AVFilterLink *inlink)
462 AVFilterContext *ctx = inlink->dst;
463 AudioCrossoverContext *s = ctx->priv;
464 int sample_rate = inlink->sample_rate;
467 s->order = (s->order_opt + 1) * 2;
468 s->filter_count = s->order / 2;
469 s->first_order = s->filter_count & 1;
470 s->ap_filter_count = s->filter_count / 2 + s->first_order;
471 calc_q_factors(s->order, q);
473 for (int band = 0; band <= s->nb_splits; band++) {
474 if (s->first_order) {
475 set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
476 set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
479 for (int n = s->first_order; n < s->filter_count; n++) {
480 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
482 set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
483 set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
487 set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
489 for (int n = s->first_order; n < s->ap_filter_count; n++) {
490 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
492 set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
496 switch (inlink->format) {
497 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
498 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
501 s->xover = ff_get_audio_buffer(inlink, 2 * (ctx->nb_outputs * 10 + ctx->nb_outputs * 10 +
502 ctx->nb_outputs * ctx->nb_outputs * 10));
504 return AVERROR(ENOMEM);
509 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
511 AVFilterContext *ctx = inlink->dst;
512 AudioCrossoverContext *s = ctx->priv;
513 AVFrame **frames = s->frames;
516 for (i = 0; i < ctx->nb_outputs; i++) {
517 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
520 ret = AVERROR(ENOMEM);
524 frames[i]->pts = in->pts;
531 ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
532 ff_filter_get_nb_threads(ctx)));
534 for (i = 0; i < ctx->nb_outputs; i++) {
535 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
542 for (i = 0; i < ctx->nb_outputs; i++)
543 av_frame_free(&frames[i]);
545 s->input_frame = NULL;
550 static av_cold void uninit(AVFilterContext *ctx)
552 AudioCrossoverContext *s = ctx->priv;
556 av_frame_free(&s->xover);
558 for (i = 0; i < ctx->nb_outputs; i++)
559 av_freep(&ctx->output_pads[i].name);
562 static const AVFilterPad inputs[] = {
565 .type = AVMEDIA_TYPE_AUDIO,
566 .filter_frame = filter_frame,
567 .config_props = config_input,
572 const AVFilter ff_af_acrossover = {
573 .name = "acrossover",
574 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
575 .priv_size = sizeof(AudioCrossoverContext),
576 .priv_class = &acrossover_class,
579 .query_formats = query_formats,
582 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
583 AVFILTER_FLAG_SLICE_THREADS,