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 BiquadContext {
48 typedef struct CrossoverChannel {
49 BiquadContext lp[MAX_BANDS][20];
50 BiquadContext hp[MAX_BANDS][20];
51 BiquadContext ap[MAX_BANDS][MAX_BANDS][20];
54 typedef struct AudioCrossoverContext {
68 CrossoverChannel *xover;
71 AVFrame *frames[MAX_BANDS];
73 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
75 AVFloatDSPContext *fdsp;
76 } AudioCrossoverContext;
78 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
79 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
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 },
98 AVFILTER_DEFINE_CLASS(acrossover);
100 static av_cold int init(AVFilterContext *ctx)
102 AudioCrossoverContext *s = ctx->priv;
103 char *p, *arg, *saveptr = NULL;
106 s->fdsp = avpriv_float_dsp_alloc(0);
108 return AVERROR(ENOMEM);
110 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
112 return AVERROR(ENOMEM);
115 for (i = 0; i < MAX_SPLITS; i++) {
118 if (!(arg = av_strtok(p, " |", &saveptr)))
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);
128 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
129 return AVERROR(EINVAL);
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);
142 for (i = 0; i <= s->nb_splits; i++) {
143 AVFilterPad pad = { 0 };
146 pad.type = AVMEDIA_TYPE_AUDIO;
147 name = av_asprintf("out%d", ctx->nb_outputs);
149 return AVERROR(ENOMEM);
152 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
161 static void set_lp(BiquadContext *b, double fc, double q, double sr)
163 double omega = M_PI * fc / sr;
164 double cosine = cos(omega);
165 double alpha = sin(omega) / (2. * q);
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;
181 static void set_hp(BiquadContext *b, double fc, double q, double sr)
183 double omega = M_PI * fc / sr;
184 double cosine = cos(omega);
185 double alpha = sin(omega) / (2. * q);
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;
201 static void set_ap(BiquadContext *b, double fc, double q, double sr)
203 double omega = M_PI * fc / sr;
204 double cosine = cos(omega);
205 double alpha = sin(omega) / (2. * q);
207 double a0 = 1. + alpha;
208 double a1 = -2. * cosine;
209 double a2 = 1. - alpha;
221 static void set_ap1(BiquadContext *b, double fc, double sr)
223 double omega = M_PI * fc / sr;
232 static void calc_q_factors(int order, double *q)
234 double n = order / 2.;
236 for (int i = 0; i < n / 2; i++)
237 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
240 static int query_formats(AVFilterContext *ctx)
242 AVFilterFormats *formats;
243 AVFilterChannelLayouts *layouts;
244 static const enum AVSampleFormat sample_fmts[] = {
245 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
250 layouts = ff_all_channel_counts();
252 return AVERROR(ENOMEM);
253 ret = ff_set_common_channel_layouts(ctx, layouts);
257 formats = ff_make_format_list(sample_fmts);
259 return AVERROR(ENOMEM);
260 ret = ff_set_common_formats(ctx, formats);
264 formats = ff_all_samplerates();
266 return AVERROR(ENOMEM);
267 return ff_set_common_samplerates(ctx, formats);
270 #define BIQUAD_PROCESS(name, type) \
271 static void biquad_process_## name(BiquadContext *b, \
272 type *dst, const type *src, \
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; \
283 for (int n = 0; n < nb_samples; n++) { \
284 const type in = src[n]; \
287 out = in * b0 + z1; \
288 z1 = b1 * in + z2 + a1 * out; \
289 z2 = b2 * in + a2 * out; \
297 BIQUAD_PROCESS(fltp, float)
298 BIQUAD_PROCESS(dblp, double)
300 #define XOVER_PROCESS(name, type, one, ff) \
301 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
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; \
310 for (int ch = start; ch < end; ch++) { \
311 const type *src = (const type *)in->extended_data[ch]; \
312 CrossoverChannel *xover = &s->xover[ch]; \
314 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
315 s->level_in, nb_samples); \
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]; \
325 biquad_process_## name(hp, dst, hsrc, nb_samples); \
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]; \
333 biquad_process_## name(lp, dst, lsrc, nb_samples); \
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]; \
342 biquad_process_## name(ap, dst, asrc, nb_samples); \
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]; \
350 biquad_process_## name(ap, dst, asrc, nb_samples); \
355 for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \
357 type *dst = (type *)frames[band]->extended_data[ch]; \
359 for (int n = 0; n < nb_samples; n++) \
368 XOVER_PROCESS(fltp, float, 1.f, f)
369 XOVER_PROCESS(dblp, double, 1.0, d)
371 static int config_input(AVFilterLink *inlink)
373 AVFilterContext *ctx = inlink->dst;
374 AudioCrossoverContext *s = ctx->priv;
375 int sample_rate = inlink->sample_rate;
378 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
380 return AVERROR(ENOMEM);
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);
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);
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;
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);
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);
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);
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);
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;
422 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
424 AVFilterContext *ctx = inlink->dst;
425 AudioCrossoverContext *s = ctx->priv;
426 AVFrame **frames = s->frames;
429 for (i = 0; i < ctx->nb_outputs; i++) {
430 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
433 ret = AVERROR(ENOMEM);
437 frames[i]->pts = in->pts;
444 ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
445 ff_filter_get_nb_threads(ctx)));
447 for (i = 0; i < ctx->nb_outputs; i++) {
448 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
455 for (i = 0; i < ctx->nb_outputs; i++)
456 av_frame_free(&frames[i]);
458 s->input_frame = NULL;
463 static av_cold void uninit(AVFilterContext *ctx)
465 AudioCrossoverContext *s = ctx->priv;
469 av_freep(&s->splits);
472 for (i = 0; i < ctx->nb_outputs; i++)
473 av_freep(&ctx->output_pads[i].name);
476 static const AVFilterPad inputs[] = {
479 .type = AVMEDIA_TYPE_AUDIO,
480 .filter_frame = filter_frame,
481 .config_props = config_input,
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,
493 .query_formats = query_formats,
496 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
497 AVFILTER_FLAG_SLICE_THREADS,