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 + 1 < nb_samples; n++) { \
295 out = in * b0 + z1; \
296 z1 = b1 * in + z2 + a1 * out; \
297 z2 = b2 * in + a2 * out; \
302 out = in * b0 + z1; \
303 z1 = b1 * in + z2 + a1 * out; \
304 z2 = b2 * in + a2 * out; \
308 if (nb_samples & 1) { \
309 const int n = nb_samples - 1; \
310 const type in = src[n]; \
313 out = in * b0 + z1; \
314 z1 = b1 * in + z2 + a1 * out; \
315 z2 = b2 * in + a2 * out; \
323 BIQUAD_PROCESS(fltp, float)
324 BIQUAD_PROCESS(dblp, double)
326 #define XOVER_PROCESS(name, type, one, ff) \
327 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
329 AudioCrossoverContext *s = ctx->priv; \
330 AVFrame *in = s->input_frame; \
331 AVFrame **frames = s->frames; \
332 const int start = (in->channels * jobnr) / nb_jobs; \
333 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
334 const int nb_samples = in->nb_samples; \
336 for (int ch = start; ch < end; ch++) { \
337 const type *src = (const type *)in->extended_data[ch]; \
338 CrossoverChannel *xover = &s->xover[ch]; \
340 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
341 s->level_in, FFALIGN(nb_samples, sizeof(type))); \
343 for (int band = 0; band < ctx->nb_outputs; band++) { \
344 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
345 const type *prv = (const type *)frames[band]->extended_data[ch]; \
346 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
347 const type *hsrc = f == 0 ? prv : dst; \
348 BiquadContext *hp = &xover->hp[band][f]; \
349 BiquadCoeffs *hpc = &s->hp[band][f]; \
351 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
354 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
355 type *dst = (type *)frames[band]->extended_data[ch]; \
356 const type *lsrc = dst; \
357 BiquadContext *lp = &xover->lp[band][f]; \
358 BiquadCoeffs *lpc = &s->lp[band][f]; \
360 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
363 for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \
364 if (s->first_order) { \
365 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
366 type *dst = (type *)frames[band]->extended_data[ch]; \
367 BiquadContext *ap = &xover->ap[band][aband][0]; \
368 BiquadCoeffs *apc = &s->ap[aband][0]; \
370 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
373 for (int f = s->first_order; f < s->ap_filter_count; f++) { \
374 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
375 type *dst = (type *)frames[band]->extended_data[ch]; \
376 BiquadContext *ap = &xover->ap[band][aband][f]; \
377 BiquadCoeffs *apc = &s->ap[aband][f]; \
379 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
384 for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \
386 type *dst = (type *)frames[band]->extended_data[ch]; \
387 s->fdsp->vector_## ff ##mul_scalar(dst, dst, -one, \
388 FFALIGN(nb_samples, sizeof(type))); \
396 XOVER_PROCESS(fltp, float, 1.f, f)
397 XOVER_PROCESS(dblp, double, 1.0, d)
399 static int config_input(AVFilterLink *inlink)
401 AVFilterContext *ctx = inlink->dst;
402 AudioCrossoverContext *s = ctx->priv;
403 int sample_rate = inlink->sample_rate;
406 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
408 return AVERROR(ENOMEM);
410 s->order = (s->order_opt + 1) * 2;
411 s->filter_count = s->order / 2;
412 s->first_order = s->filter_count & 1;
413 s->ap_filter_count = s->filter_count / 2 + s->first_order;
414 calc_q_factors(s->order, q);
416 for (int band = 0; band <= s->nb_splits; band++) {
417 if (s->first_order) {
418 set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
419 set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
422 for (int n = s->first_order; n < s->filter_count; n++) {
423 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
425 set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
426 set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
430 set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
432 for (int n = s->first_order; n < s->ap_filter_count; n++) {
433 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
435 set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
439 switch (inlink->format) {
440 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
441 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
447 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
449 AVFilterContext *ctx = inlink->dst;
450 AudioCrossoverContext *s = ctx->priv;
451 AVFrame **frames = s->frames;
454 for (i = 0; i < ctx->nb_outputs; i++) {
455 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
458 ret = AVERROR(ENOMEM);
462 frames[i]->pts = in->pts;
469 ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
470 ff_filter_get_nb_threads(ctx)));
472 for (i = 0; i < ctx->nb_outputs; i++) {
473 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
480 for (i = 0; i < ctx->nb_outputs; i++)
481 av_frame_free(&frames[i]);
483 s->input_frame = NULL;
488 static av_cold void uninit(AVFilterContext *ctx)
490 AudioCrossoverContext *s = ctx->priv;
494 av_freep(&s->splits);
497 for (i = 0; i < ctx->nb_outputs; i++)
498 av_freep(&ctx->output_pads[i].name);
501 static const AVFilterPad inputs[] = {
504 .type = AVMEDIA_TYPE_AUDIO,
505 .filter_frame = filter_frame,
506 .config_props = config_input,
511 AVFilter ff_af_acrossover = {
512 .name = "acrossover",
513 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
514 .priv_size = sizeof(AudioCrossoverContext),
515 .priv_class = &acrossover_class,
518 .query_formats = query_formats,
521 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
522 AVFILTER_FLAG_SLICE_THREADS,