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 {
67 BiquadCoeffs lp[MAX_BANDS][20];
68 BiquadCoeffs hp[MAX_BANDS][20];
69 BiquadCoeffs ap[MAX_BANDS][20];
74 AVFrame *frames[MAX_BANDS];
76 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
78 AVFloatDSPContext *fdsp;
79 } AudioCrossoverContext;
81 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
82 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
84 static const AVOption acrossover_options[] = {
85 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
86 { "order", "set order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
87 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
88 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
89 { "6th", "6th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
90 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
91 { "10th", "10th order", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
92 { "12th", "12th order", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
93 { "14th", "14th order", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
94 { "16th", "16th order", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
95 { "18th", "18th order", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
96 { "20th", "20th order", 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
97 { "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
101 AVFILTER_DEFINE_CLASS(acrossover);
103 static av_cold int init(AVFilterContext *ctx)
105 AudioCrossoverContext *s = ctx->priv;
106 char *p, *arg, *saveptr = NULL;
109 s->fdsp = avpriv_float_dsp_alloc(0);
111 return AVERROR(ENOMEM);
113 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
115 return AVERROR(ENOMEM);
118 for (i = 0; i < MAX_SPLITS; i++) {
121 if (!(arg = av_strtok(p, " |", &saveptr)))
126 if (av_sscanf(arg, "%f", &freq) != 1) {
127 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
128 return AVERROR(EINVAL);
131 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
132 return AVERROR(EINVAL);
135 if (i > 0 && freq <= s->splits[i-1]) {
136 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
137 return AVERROR(EINVAL);
145 for (i = 0; i <= s->nb_splits; i++) {
146 AVFilterPad pad = { 0 };
149 pad.type = AVMEDIA_TYPE_AUDIO;
150 name = av_asprintf("out%d", ctx->nb_outputs);
152 return AVERROR(ENOMEM);
155 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
164 static void set_lp(BiquadCoeffs *b, double fc, double q, double sr)
166 double omega = 2. * M_PI * fc / sr;
167 double cosine = cos(omega);
168 double alpha = sin(omega) / (2. * q);
170 double b0 = (1. - cosine) / 2.;
171 double b1 = 1. - cosine;
172 double b2 = (1. - cosine) / 2.;
173 double a0 = 1. + alpha;
174 double a1 = -2. * cosine;
175 double a2 = 1. - alpha;
180 b->cd[A1] = -a1 / a0;
181 b->cd[A2] = -a2 / a0;
183 b->cf[B0] = b->cd[B0];
184 b->cf[B1] = b->cd[B1];
185 b->cf[B2] = b->cd[B2];
186 b->cf[A1] = b->cd[A1];
187 b->cf[A2] = b->cd[A2];
190 static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
192 double omega = 2. * M_PI * fc / sr;
193 double cosine = cos(omega);
194 double alpha = sin(omega) / (2. * q);
196 double b0 = (1. + cosine) / 2.;
197 double b1 = -1. - cosine;
198 double b2 = (1. + cosine) / 2.;
199 double a0 = 1. + alpha;
200 double a1 = -2. * cosine;
201 double a2 = 1. - alpha;
206 b->cd[A1] = -a1 / a0;
207 b->cd[A2] = -a2 / a0;
209 b->cf[B0] = b->cd[B0];
210 b->cf[B1] = b->cd[B1];
211 b->cf[B2] = b->cd[B2];
212 b->cf[A1] = b->cd[A1];
213 b->cf[A2] = b->cd[A2];
216 static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
218 double omega = 2. * M_PI * fc / sr;
219 double cosine = cos(omega);
220 double alpha = sin(omega) / (2. * q);
222 double a0 = 1. + alpha;
223 double a1 = -2. * cosine;
224 double a2 = 1. - alpha;
232 b->cd[A1] = -a1 / a0;
233 b->cd[A2] = -a2 / a0;
235 b->cf[B0] = b->cd[B0];
236 b->cf[B1] = b->cd[B1];
237 b->cf[B2] = b->cd[B2];
238 b->cf[A1] = b->cd[A1];
239 b->cf[A2] = b->cd[A2];
242 static void set_ap1(BiquadCoeffs *b, double fc, double sr)
244 double omega = 2. * M_PI * fc / sr;
246 b->cd[A1] = exp(-omega);
248 b->cd[B0] = -b->cd[A1];
252 b->cf[B0] = b->cd[B0];
253 b->cf[B1] = b->cd[B1];
254 b->cf[B2] = b->cd[B2];
255 b->cf[A1] = b->cd[A1];
256 b->cf[A2] = b->cd[A2];
259 static void calc_q_factors(int order, double *q)
261 double n = order / 2.;
263 for (int i = 0; i < n / 2; i++)
264 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
267 static int query_formats(AVFilterContext *ctx)
269 AVFilterFormats *formats;
270 AVFilterChannelLayouts *layouts;
271 static const enum AVSampleFormat sample_fmts[] = {
272 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
277 layouts = ff_all_channel_counts();
279 return AVERROR(ENOMEM);
280 ret = ff_set_common_channel_layouts(ctx, layouts);
284 formats = ff_make_format_list(sample_fmts);
286 return AVERROR(ENOMEM);
287 ret = ff_set_common_formats(ctx, formats);
291 formats = ff_all_samplerates();
293 return AVERROR(ENOMEM);
294 return ff_set_common_samplerates(ctx, formats);
297 #define BIQUAD_PROCESS(name, type) \
298 static void biquad_process_## name(const type *const c, \
300 type *dst, const type *src, \
303 const type b0 = c[B0]; \
304 const type b1 = c[B1]; \
305 const type b2 = c[B2]; \
306 const type a1 = c[A1]; \
307 const type a2 = c[A2]; \
311 for (int n = 0; n + 1 < nb_samples; n++) { \
315 out = in * b0 + z1; \
316 z1 = b1 * in + z2 + a1 * out; \
317 z2 = b2 * in + a2 * out; \
322 out = in * b0 + z1; \
323 z1 = b1 * in + z2 + a1 * out; \
324 z2 = b2 * in + a2 * out; \
328 if (nb_samples & 1) { \
329 const int n = nb_samples - 1; \
330 const type in = src[n]; \
333 out = in * b0 + z1; \
334 z1 = b1 * in + z2 + a1 * out; \
335 z2 = b2 * in + a2 * out; \
343 BIQUAD_PROCESS(fltp, float)
344 BIQUAD_PROCESS(dblp, double)
346 #define XOVER_PROCESS(name, type, one, ff) \
347 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
349 AudioCrossoverContext *s = ctx->priv; \
350 AVFrame *in = s->input_frame; \
351 AVFrame **frames = s->frames; \
352 const int start = (in->channels * jobnr) / nb_jobs; \
353 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
354 const int nb_samples = in->nb_samples; \
355 const int nb_outs = ctx->nb_outputs; \
357 for (int ch = start; ch < end; ch++) { \
358 const type *src = (const type *)in->extended_data[ch]; \
359 type *xover = (type *)s->xover->extended_data[ch]; \
361 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
362 s->level_in, FFALIGN(nb_samples, sizeof(type))); \
364 for (int band = 0; band < nb_outs; band++) { \
365 for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
366 const type *prv = (const type *)frames[band]->extended_data[ch]; \
367 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
368 const type *hsrc = f == 0 ? prv : dst; \
369 type *hp = xover + nb_outs * 20 + band * 20 + f * 2; \
370 const type *const hpc = (type *)&s->hp[band][f].c ## ff; \
372 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
375 for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
376 type *dst = (type *)frames[band]->extended_data[ch]; \
377 const type *lsrc = dst; \
378 type *lp = xover + band * 20 + f * 2; \
379 const type *const lpc = (type *)&s->lp[band][f].c ## ff; \
381 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
384 for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \
385 if (s->first_order) { \
386 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
387 type *dst = (type *)frames[band]->extended_data[ch]; \
388 type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \
389 const type *const apc = (type *)&s->ap[aband][0].c ## ff; \
391 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
394 for (int f = s->first_order; f < s->ap_filter_count; f++) { \
395 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
396 type *dst = (type *)frames[band]->extended_data[ch]; \
397 type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\
398 const type *const apc = (type *)&s->ap[aband][f].c ## ff; \
400 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
405 for (int band = 0; band < nb_outs && s->first_order; band++) { \
407 type *dst = (type *)frames[band]->extended_data[ch]; \
408 s->fdsp->vector_## ff ##mul_scalar(dst, dst, -one, \
409 FFALIGN(nb_samples, sizeof(type))); \
417 XOVER_PROCESS(fltp, float, 1.f, f)
418 XOVER_PROCESS(dblp, double, 1.0, d)
420 static int config_input(AVFilterLink *inlink)
422 AVFilterContext *ctx = inlink->dst;
423 AudioCrossoverContext *s = ctx->priv;
424 int sample_rate = inlink->sample_rate;
427 s->order = (s->order_opt + 1) * 2;
428 s->filter_count = s->order / 2;
429 s->first_order = s->filter_count & 1;
430 s->ap_filter_count = s->filter_count / 2 + s->first_order;
431 calc_q_factors(s->order, q);
433 for (int band = 0; band <= s->nb_splits; band++) {
434 if (s->first_order) {
435 set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
436 set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
439 for (int n = s->first_order; n < s->filter_count; n++) {
440 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
442 set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
443 set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
447 set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
449 for (int n = s->first_order; n < s->ap_filter_count; n++) {
450 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
452 set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
456 switch (inlink->format) {
457 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
458 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
461 s->xover = ff_get_audio_buffer(inlink, 2 * (ctx->nb_outputs * 10 + ctx->nb_outputs * 10 +
462 ctx->nb_outputs * ctx->nb_outputs * 10));
464 return AVERROR(ENOMEM);
469 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
471 AVFilterContext *ctx = inlink->dst;
472 AudioCrossoverContext *s = ctx->priv;
473 AVFrame **frames = s->frames;
476 for (i = 0; i < ctx->nb_outputs; i++) {
477 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
480 ret = AVERROR(ENOMEM);
484 frames[i]->pts = in->pts;
491 ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
492 ff_filter_get_nb_threads(ctx)));
494 for (i = 0; i < ctx->nb_outputs; i++) {
495 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
502 for (i = 0; i < ctx->nb_outputs; i++)
503 av_frame_free(&frames[i]);
505 s->input_frame = NULL;
510 static av_cold void uninit(AVFilterContext *ctx)
512 AudioCrossoverContext *s = ctx->priv;
516 av_freep(&s->splits);
517 av_frame_free(&s->xover);
519 for (i = 0; i < ctx->nb_outputs; i++)
520 av_freep(&ctx->output_pads[i].name);
523 static const AVFilterPad inputs[] = {
526 .type = AVMEDIA_TYPE_AUDIO,
527 .filter_frame = filter_frame,
528 .config_props = config_input,
533 AVFilter ff_af_acrossover = {
534 .name = "acrossover",
535 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
536 .priv_size = sizeof(AudioCrossoverContext),
537 .priv_class = &acrossover_class,
540 .query_formats = query_formats,
543 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
544 AVFILTER_FLAG_SLICE_THREADS,