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
42 typedef struct BiquadCoeffs {
47 typedef struct BiquadContext {
51 typedef struct CrossoverChannel {
52 BiquadContext lp[MAX_BANDS][20];
53 BiquadContext hp[MAX_BANDS][20];
54 BiquadContext ap[MAX_BANDS][MAX_BANDS][20];
57 typedef struct AudioCrossoverContext {
71 BiquadCoeffs lp[MAX_BANDS][20];
72 BiquadCoeffs hp[MAX_BANDS][20];
73 BiquadCoeffs ap[MAX_BANDS][20];
75 CrossoverChannel *xover;
78 AVFrame *frames[MAX_BANDS];
80 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
82 AVFloatDSPContext *fdsp;
83 } AudioCrossoverContext;
85 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
86 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
88 static const AVOption acrossover_options[] = {
89 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
90 { "order", "set order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
91 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
92 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
93 { "6th", "6th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
94 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
95 { "10th", "10th order", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
96 { "12th", "12th order", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
97 { "14th", "14th order", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
98 { "16th", "16th order", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
99 { "18th", "18th order", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
100 { "20th", "20th order", 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
101 { "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
105 AVFILTER_DEFINE_CLASS(acrossover);
107 static av_cold int init(AVFilterContext *ctx)
109 AudioCrossoverContext *s = ctx->priv;
110 char *p, *arg, *saveptr = NULL;
113 s->fdsp = avpriv_float_dsp_alloc(0);
115 return AVERROR(ENOMEM);
117 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
119 return AVERROR(ENOMEM);
122 for (i = 0; i < MAX_SPLITS; i++) {
125 if (!(arg = av_strtok(p, " |", &saveptr)))
130 if (av_sscanf(arg, "%f", &freq) != 1) {
131 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
132 return AVERROR(EINVAL);
135 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
136 return AVERROR(EINVAL);
139 if (i > 0 && freq <= s->splits[i-1]) {
140 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
141 return AVERROR(EINVAL);
149 for (i = 0; i <= s->nb_splits; i++) {
150 AVFilterPad pad = { 0 };
153 pad.type = AVMEDIA_TYPE_AUDIO;
154 name = av_asprintf("out%d", ctx->nb_outputs);
156 return AVERROR(ENOMEM);
159 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
168 static void set_lp(BiquadCoeffs *b, double fc, double q, double sr)
170 double omega = 2. * M_PI * fc / sr;
171 double cosine = cos(omega);
172 double alpha = sin(omega) / (2. * q);
174 double b0 = (1. - cosine) / 2.;
175 double b1 = 1. - cosine;
176 double b2 = (1. - cosine) / 2.;
177 double a0 = 1. + alpha;
178 double a1 = -2. * cosine;
179 double a2 = 1. - alpha;
188 static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
190 double omega = 2. * M_PI * fc / sr;
191 double cosine = cos(omega);
192 double alpha = sin(omega) / (2. * q);
194 double b0 = (1. + cosine) / 2.;
195 double b1 = -1. - cosine;
196 double b2 = (1. + cosine) / 2.;
197 double a0 = 1. + alpha;
198 double a1 = -2. * cosine;
199 double a2 = 1. - alpha;
208 static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
210 double omega = 2. * M_PI * fc / sr;
211 double cosine = cos(omega);
212 double alpha = sin(omega) / (2. * q);
214 double a0 = 1. + alpha;
215 double a1 = -2. * cosine;
216 double a2 = 1. - alpha;
228 static void set_ap1(BiquadCoeffs *b, double fc, double sr)
230 double omega = 2. * M_PI * fc / sr;
239 static void calc_q_factors(int order, double *q)
241 double n = order / 2.;
243 for (int i = 0; i < n / 2; i++)
244 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
247 static int query_formats(AVFilterContext *ctx)
249 AVFilterFormats *formats;
250 AVFilterChannelLayouts *layouts;
251 static const enum AVSampleFormat sample_fmts[] = {
252 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
257 layouts = ff_all_channel_counts();
259 return AVERROR(ENOMEM);
260 ret = ff_set_common_channel_layouts(ctx, layouts);
264 formats = ff_make_format_list(sample_fmts);
266 return AVERROR(ENOMEM);
267 ret = ff_set_common_formats(ctx, formats);
271 formats = ff_all_samplerates();
273 return AVERROR(ENOMEM);
274 return ff_set_common_samplerates(ctx, formats);
277 #define BIQUAD_PROCESS(name, type) \
278 static void biquad_process_## name(const BiquadCoeffs *const c,\
280 type *dst, const type *src, \
283 const type b0 = c->b0; \
284 const type b1 = c->b1; \
285 const type b2 = c->b2; \
286 const type a1 = c->a1; \
287 const type a2 = c->a2; \
291 for (int n = 0; n < nb_samples; n++) { \
292 const type in = src[n]; \
295 out = in * b0 + z1; \
296 z1 = b1 * in + z2 + a1 * out; \
297 z2 = b2 * in + a2 * out; \
305 BIQUAD_PROCESS(fltp, float)
306 BIQUAD_PROCESS(dblp, double)
308 #define XOVER_PROCESS(name, type, one, ff) \
309 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
311 AudioCrossoverContext *s = ctx->priv; \
312 AVFrame *in = s->input_frame; \
313 AVFrame **frames = s->frames; \
314 const int start = (in->channels * jobnr) / nb_jobs; \
315 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
316 const int nb_samples = in->nb_samples; \
318 for (int ch = start; ch < end; ch++) { \
319 const type *src = (const type *)in->extended_data[ch]; \
320 CrossoverChannel *xover = &s->xover[ch]; \
322 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
323 s->level_in, FFALIGN(nb_samples, sizeof(type))); \
326 for (int band = 0; band < ctx->nb_outputs; band++) { \
327 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
328 const type *prv = (const type *)frames[band]->extended_data[ch]; \
329 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
330 const type *hsrc = f == 0 ? prv : dst; \
331 BiquadContext *hp = &xover->hp[band][f]; \
332 BiquadCoeffs *hpc = &s->hp[band][f]; \
334 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
337 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
338 type *dst = (type *)frames[band]->extended_data[ch]; \
339 const type *lsrc = dst; \
340 BiquadContext *lp = &xover->lp[band][f]; \
341 BiquadCoeffs *lpc = &s->lp[band][f]; \
343 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
346 for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \
347 if (s->first_order) { \
348 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
349 type *dst = (type *)frames[band]->extended_data[ch]; \
350 BiquadContext *ap = &xover->ap[band][aband][0]; \
351 BiquadCoeffs *apc = &s->ap[aband][0]; \
353 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
356 for (int f = s->first_order; f < s->ap_filter_count; f++) { \
357 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
358 type *dst = (type *)frames[band]->extended_data[ch]; \
359 BiquadContext *ap = &xover->ap[band][aband][f]; \
360 BiquadCoeffs *apc = &s->ap[aband][f]; \
362 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
367 for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \
369 type *dst = (type *)frames[band]->extended_data[ch]; \
371 for (int n = 0; n < nb_samples; n++) \
380 XOVER_PROCESS(fltp, float, 1.f, f)
381 XOVER_PROCESS(dblp, double, 1.0, d)
383 static int config_input(AVFilterLink *inlink)
385 AVFilterContext *ctx = inlink->dst;
386 AudioCrossoverContext *s = ctx->priv;
387 int sample_rate = inlink->sample_rate;
390 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
392 return AVERROR(ENOMEM);
394 s->order = (s->order_opt + 1) * 2;
395 s->filter_count = s->order / 2;
396 s->first_order = s->filter_count & 1;
397 s->ap_filter_count = s->filter_count / 2 + s->first_order;
398 calc_q_factors(s->order, q);
400 for (int band = 0; band <= s->nb_splits; band++) {
401 if (s->first_order) {
402 set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
403 set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
406 for (int n = s->first_order; n < s->filter_count; n++) {
407 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
409 set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
410 set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
414 set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
416 for (int n = s->first_order; n < s->ap_filter_count; n++) {
417 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
419 set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
423 switch (inlink->format) {
424 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
425 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
431 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
433 AVFilterContext *ctx = inlink->dst;
434 AudioCrossoverContext *s = ctx->priv;
435 AVFrame **frames = s->frames;
438 for (i = 0; i < ctx->nb_outputs; i++) {
439 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
442 ret = AVERROR(ENOMEM);
446 frames[i]->pts = in->pts;
453 ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
454 ff_filter_get_nb_threads(ctx)));
456 for (i = 0; i < ctx->nb_outputs; i++) {
457 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
464 for (i = 0; i < ctx->nb_outputs; i++)
465 av_frame_free(&frames[i]);
467 s->input_frame = NULL;
472 static av_cold void uninit(AVFilterContext *ctx)
474 AudioCrossoverContext *s = ctx->priv;
478 av_freep(&s->splits);
481 for (i = 0; i < ctx->nb_outputs; i++)
482 av_freep(&ctx->output_pads[i].name);
485 static const AVFilterPad inputs[] = {
488 .type = AVMEDIA_TYPE_AUDIO,
489 .filter_frame = filter_frame,
490 .config_props = config_input,
495 AVFilter ff_af_acrossover = {
496 .name = "acrossover",
497 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
498 .priv_size = sizeof(AudioCrossoverContext),
499 .priv_class = &acrossover_class,
502 .query_formats = query_formats,
505 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
506 AVFILTER_FLAG_SLICE_THREADS,