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/internal.h"
31 #include "libavutil/opt.h"
39 #define MAX_BANDS MAX_SPLITS + 1
41 typedef struct BiquadContext {
47 typedef struct CrossoverChannel {
48 BiquadContext lp[MAX_BANDS][20];
49 BiquadContext hp[MAX_BANDS][20];
50 BiquadContext ap[MAX_BANDS][MAX_BANDS][20];
53 typedef struct AudioCrossoverContext {
66 CrossoverChannel *xover;
69 AVFrame *frames[MAX_BANDS];
71 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
72 } AudioCrossoverContext;
74 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
75 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
77 static const AVOption acrossover_options[] = {
78 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
79 { "order", "set order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
80 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
81 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
82 { "6th", "6th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
83 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
84 { "10th", "10th order", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
85 { "12th", "12th order", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
86 { "14th", "14th order", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
87 { "16th", "16th order", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
88 { "18th", "18th order", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
89 { "20th", "20th order", 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
93 AVFILTER_DEFINE_CLASS(acrossover);
95 static av_cold int init(AVFilterContext *ctx)
97 AudioCrossoverContext *s = ctx->priv;
98 char *p, *arg, *saveptr = NULL;
101 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
103 return AVERROR(ENOMEM);
106 for (i = 0; i < MAX_SPLITS; i++) {
109 if (!(arg = av_strtok(p, " |", &saveptr)))
114 if (av_sscanf(arg, "%f", &freq) != 1) {
115 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
116 return AVERROR(EINVAL);
119 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
120 return AVERROR(EINVAL);
123 if (i > 0 && freq <= s->splits[i-1]) {
124 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
125 return AVERROR(EINVAL);
133 for (i = 0; i <= s->nb_splits; i++) {
134 AVFilterPad pad = { 0 };
137 pad.type = AVMEDIA_TYPE_AUDIO;
138 name = av_asprintf("out%d", ctx->nb_outputs);
140 return AVERROR(ENOMEM);
143 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
152 static void set_lp(BiquadContext *b, double fc, double q, double sr)
154 double omega = M_PI * fc / sr;
155 double cosine = cos(omega);
156 double alpha = sin(omega) / (2. * q);
158 double b0 = (1. - cosine) / 2.;
159 double b1 = 1. - cosine;
160 double b2 = (1. - cosine) / 2.;
161 double a0 = 1. + alpha;
162 double a1 = -2. * cosine;
163 double a2 = 1. - alpha;
172 static void set_hp(BiquadContext *b, double fc, double q, double sr)
174 double omega = M_PI * fc / sr;
175 double cosine = cos(omega);
176 double alpha = sin(omega) / (2. * q);
178 double b0 = (1. + cosine) / 2.;
179 double b1 = -1. - cosine;
180 double b2 = (1. + cosine) / 2.;
181 double a0 = 1. + alpha;
182 double a1 = -2. * cosine;
183 double a2 = 1. - alpha;
192 static void set_ap(BiquadContext *b, double fc, double q, double sr)
194 double omega = M_PI * fc / sr;
195 double cosine = cos(omega);
196 double alpha = sin(omega) / (2. * q);
198 double a0 = 1. + alpha;
199 double a1 = -2. * cosine;
200 double a2 = 1. - alpha;
212 static void set_ap1(BiquadContext *b, double fc, double sr)
214 double omega = 0.5 * M_PI * fc / sr + M_PI_4;
215 double cosine = cos(omega);
216 double sine = sin(omega);
218 b->a1 = -cosine / sine;
225 static void calc_q_factors(int order, double *q)
227 double n = order / 2.;
229 for (int i = 0; i < n / 2; i++)
230 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
233 static int query_formats(AVFilterContext *ctx)
235 AVFilterFormats *formats;
236 AVFilterChannelLayouts *layouts;
237 static const enum AVSampleFormat sample_fmts[] = {
238 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
243 layouts = ff_all_channel_counts();
245 return AVERROR(ENOMEM);
246 ret = ff_set_common_channel_layouts(ctx, layouts);
250 formats = ff_make_format_list(sample_fmts);
252 return AVERROR(ENOMEM);
253 ret = ff_set_common_formats(ctx, formats);
257 formats = ff_all_samplerates();
259 return AVERROR(ENOMEM);
260 return ff_set_common_samplerates(ctx, formats);
263 #define BIQUAD_PROCESS(name, type) \
264 static void biquad_process_## name(BiquadContext *b, \
265 type *dst, const type *src, \
268 const type b0 = b->b0; \
269 const type b1 = b->b1; \
270 const type b2 = b->b2; \
271 const type a1 = b->a1; \
272 const type a2 = b->a2; \
276 for (int n = 0; n < nb_samples; n++) { \
277 const type in = src[n]; \
280 out = in * b0 + z1; \
281 z1 = b1 * in + z2 + a1 * out; \
282 z2 = b2 * in + a2 * out; \
290 BIQUAD_PROCESS(fltp, float)
291 BIQUAD_PROCESS(dblp, double)
293 #define XOVER_PROCESS(name, type, one) \
294 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
296 AudioCrossoverContext *s = ctx->priv; \
297 AVFrame *in = s->input_frame; \
298 AVFrame **frames = s->frames; \
299 const int start = (in->channels * jobnr) / nb_jobs; \
300 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
301 const int nb_samples = in->nb_samples; \
303 for (int ch = start; ch < end; ch++) { \
304 CrossoverChannel *xover = &s->xover[ch]; \
306 for (int band = 0; band < ctx->nb_outputs; band++) { \
307 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
308 const type *src = (const type *)in->extended_data[ch]; \
309 const type *prv = (const type *)frames[band]->extended_data[ch]; \
310 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
311 const type *hsrc = (band == 0 && f == 0) ? src : f == 0 ? prv : dst; \
312 BiquadContext *hp = &xover->hp[band][f]; \
314 biquad_process_## name(hp, dst, hsrc, nb_samples); \
317 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
318 const type *src = (const type *)in->extended_data[ch]; \
319 type *dst = (type *)frames[band]->extended_data[ch]; \
320 const type *lsrc = (band == 0 && f == 0) ? src : dst; \
321 BiquadContext *lp = &xover->lp[band][f]; \
323 biquad_process_## name(lp, dst, lsrc, nb_samples); \
326 for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \
327 if (s->first_order) { \
328 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
329 type *dst = (type *)frames[band]->extended_data[ch]; \
330 BiquadContext *ap = &xover->ap[band][aband][0]; \
332 biquad_process_## name(ap, dst, asrc, nb_samples); \
335 for (int f = s->first_order; f < s->ap_filter_count; f++) { \
336 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
337 type *dst = (type *)frames[band]->extended_data[ch]; \
338 BiquadContext *ap = &xover->ap[band][aband][f]; \
340 biquad_process_## name(ap, dst, asrc, nb_samples); \
345 for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \
347 type *dst = (type *)frames[band]->extended_data[ch]; \
349 for (int n = 0; n < nb_samples; n++) \
358 XOVER_PROCESS(fltp, float, 1.f)
359 XOVER_PROCESS(dblp, double, 1.0)
361 static int config_input(AVFilterLink *inlink)
363 AVFilterContext *ctx = inlink->dst;
364 AudioCrossoverContext *s = ctx->priv;
365 int sample_rate = inlink->sample_rate;
368 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
370 return AVERROR(ENOMEM);
372 s->order = (s->order_opt + 1) * 2;
373 s->filter_count = s->order / 2;
374 s->first_order = s->filter_count & 1;
375 s->ap_filter_count = s->filter_count / 2 + s->first_order;
376 calc_q_factors(s->order, q);
378 for (int ch = 0; ch < inlink->channels; ch++) {
379 for (int band = 0; band <= s->nb_splits; band++) {
380 if (s->first_order) {
381 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
382 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
385 for (int n = s->first_order; n < s->filter_count; n++) {
386 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
388 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
389 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);
392 for (int x = 0; x <= s->nb_splits && s->first_order; x++)
393 set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate);
395 for (int n = s->first_order; n < s->ap_filter_count; n++) {
396 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
398 for (int x = 0; x <= s->nb_splits; x++)
399 set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
404 switch (inlink->format) {
405 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
406 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
412 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
414 AVFilterContext *ctx = inlink->dst;
415 AudioCrossoverContext *s = ctx->priv;
416 AVFrame **frames = s->frames;
419 for (i = 0; i < ctx->nb_outputs; i++) {
420 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
423 ret = AVERROR(ENOMEM);
427 frames[i]->pts = in->pts;
434 ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
435 ff_filter_get_nb_threads(ctx)));
437 for (i = 0; i < ctx->nb_outputs; i++) {
438 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
445 for (i = 0; i < ctx->nb_outputs; i++)
446 av_frame_free(&frames[i]);
448 s->input_frame = NULL;
453 static av_cold void uninit(AVFilterContext *ctx)
455 AudioCrossoverContext *s = ctx->priv;
458 av_freep(&s->splits);
461 for (i = 0; i < ctx->nb_outputs; i++)
462 av_freep(&ctx->output_pads[i].name);
465 static const AVFilterPad inputs[] = {
468 .type = AVMEDIA_TYPE_AUDIO,
469 .filter_frame = filter_frame,
470 .config_props = config_input,
475 AVFilter ff_af_acrossover = {
476 .name = "acrossover",
477 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
478 .priv_size = sizeof(AudioCrossoverContext),
479 .priv_class = &acrossover_class,
482 .query_formats = query_formats,
485 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
486 AVFILTER_FLAG_SLICE_THREADS,