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];
70 } AudioCrossoverContext;
72 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
73 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
75 static const AVOption acrossover_options[] = {
76 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
77 { "order", "set order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
78 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
79 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
80 { "6th", "6th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
81 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
82 { "10th", "10th order", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
83 { "12th", "12th order", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
84 { "14th", "14th order", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
85 { "16th", "16th order", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
86 { "18th", "18th order", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
87 { "20th", "20th order", 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
91 AVFILTER_DEFINE_CLASS(acrossover);
93 static av_cold int init(AVFilterContext *ctx)
95 AudioCrossoverContext *s = ctx->priv;
96 char *p, *arg, *saveptr = NULL;
99 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
101 return AVERROR(ENOMEM);
104 for (i = 0; i < MAX_SPLITS; i++) {
107 if (!(arg = av_strtok(p, " |", &saveptr)))
112 if (av_sscanf(arg, "%f", &freq) != 1) {
113 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
114 return AVERROR(EINVAL);
117 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
118 return AVERROR(EINVAL);
121 if (i > 0 && freq <= s->splits[i-1]) {
122 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
123 return AVERROR(EINVAL);
131 for (i = 0; i <= s->nb_splits; i++) {
132 AVFilterPad pad = { 0 };
135 pad.type = AVMEDIA_TYPE_AUDIO;
136 name = av_asprintf("out%d", ctx->nb_outputs);
138 return AVERROR(ENOMEM);
141 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
150 static void set_lp(BiquadContext *b, double fc, double q, double sr)
152 double omega = M_PI * fc / sr;
153 double cosine = cos(omega);
154 double alpha = sin(omega) / (2. * q);
156 double b0 = (1. - cosine) / 2.;
157 double b1 = 1. - cosine;
158 double b2 = (1. - cosine) / 2.;
159 double a0 = 1. + alpha;
160 double a1 = -2. * cosine;
161 double a2 = 1. - alpha;
170 static void set_hp(BiquadContext *b, double fc, double q, double sr)
172 double omega = M_PI * fc / sr;
173 double cosine = cos(omega);
174 double alpha = sin(omega) / (2. * q);
176 double b0 = (1. + cosine) / 2.;
177 double b1 = -1. - cosine;
178 double b2 = (1. + cosine) / 2.;
179 double a0 = 1. + alpha;
180 double a1 = -2. * cosine;
181 double a2 = 1. - alpha;
190 static void set_ap(BiquadContext *b, double fc, double q, double sr)
192 double omega = M_PI * fc / sr;
193 double cosine = cos(omega);
194 double alpha = sin(omega) / (2. * q);
196 double a0 = 1. + alpha;
197 double a1 = -2. * cosine;
198 double a2 = 1. - alpha;
210 static void set_ap1(BiquadContext *b, double fc, double sr)
212 double omega = M_PI * fc / sr;
213 double K = tan(omega);
215 b->a1 = -(1. - K) / (1. + K);
222 static void calc_q_factors(int order, double *q)
224 double n = order / 2.;
226 for (int i = 0; i < n / 2; i++)
227 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
230 static int config_input(AVFilterLink *inlink)
232 AVFilterContext *ctx = inlink->dst;
233 AudioCrossoverContext *s = ctx->priv;
234 int sample_rate = inlink->sample_rate;
237 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
239 return AVERROR(ENOMEM);
241 s->order = (s->order_opt + 1) * 2;
242 s->filter_count = s->order / 2;
243 s->first_order = s->filter_count & 1;
244 s->ap_filter_count = s->filter_count / 2 + s->first_order;
245 calc_q_factors(s->order, q);
247 for (int ch = 0; ch < inlink->channels; ch++) {
248 for (int band = 0; band <= s->nb_splits; band++) {
249 if (s->first_order) {
250 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
251 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
254 for (int n = s->first_order; n < s->filter_count; n++) {
255 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
257 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
258 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);
261 for (int x = 0; x <= s->nb_splits && s->first_order; x++)
262 set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate);
264 for (int n = s->first_order; n < s->ap_filter_count; n++) {
265 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
267 for (int x = 0; x <= s->nb_splits; x++)
268 set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
276 static int query_formats(AVFilterContext *ctx)
278 AVFilterFormats *formats;
279 AVFilterChannelLayouts *layouts;
280 static const enum AVSampleFormat sample_fmts[] = {
286 layouts = ff_all_channel_counts();
288 return AVERROR(ENOMEM);
289 ret = ff_set_common_channel_layouts(ctx, layouts);
293 formats = ff_make_format_list(sample_fmts);
295 return AVERROR(ENOMEM);
296 ret = ff_set_common_formats(ctx, formats);
300 formats = ff_all_samplerates();
302 return AVERROR(ENOMEM);
303 return ff_set_common_samplerates(ctx, formats);
306 static void biquad_process(BiquadContext *b,
307 double *dst, const double *src,
310 const double b0 = b->b0;
311 const double b1 = b->b1;
312 const double b2 = b->b2;
313 const double a1 = b->a1;
314 const double a2 = b->a2;
318 for (int n = 0; n < nb_samples; n++) {
319 const double in = src[n];
323 z1 = b1 * in + z2 + a1 * out;
324 z2 = b2 * in + a2 * out;
332 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
334 AudioCrossoverContext *s = ctx->priv;
335 AVFrame *in = s->input_frame;
336 AVFrame **frames = s->frames;
337 const int start = (in->channels * jobnr) / nb_jobs;
338 const int end = (in->channels * (jobnr+1)) / nb_jobs;
339 const int nb_samples = in->nb_samples;
341 for (int ch = start; ch < end; ch++) {
342 CrossoverChannel *xover = &s->xover[ch];
344 for (int band = 0; band < ctx->nb_outputs; band++) {
345 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
346 const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch];
347 double *dst = (double *)frames[band + 1]->extended_data[ch];
348 const double *hsrc = f == 0 ? src : dst;
349 BiquadContext *hp = &xover->hp[band][f];
351 biquad_process(hp, dst, hsrc, nb_samples);
354 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
355 const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch];
356 double *dst = (double *)frames[band]->extended_data[ch];
357 const double *lsrc = f == 0 ? src : dst;
358 BiquadContext *lp = &xover->lp[band][f];
360 biquad_process(lp, dst, lsrc, nb_samples);
363 for (int aband = band + 1; aband < ctx->nb_outputs; aband++) {
364 if (s->first_order) {
365 const double *src = (const double *)frames[band]->extended_data[ch];
366 double *dst = (double *)frames[band]->extended_data[ch];
367 BiquadContext *ap = &xover->ap[band][aband][0];
369 biquad_process(ap, dst, src, nb_samples);
372 for (int f = s->first_order; f < s->ap_filter_count; f++) {
373 const double *src = (const double *)frames[band]->extended_data[ch];
374 double *dst = (double *)frames[band]->extended_data[ch];
375 BiquadContext *ap = &xover->ap[band][aband][f];
377 biquad_process(ap, dst, src, nb_samples);
382 for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) {
384 double *dst = (double *)frames[band]->extended_data[ch];
386 for (int n = 0; n < nb_samples; n++)
395 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
397 AVFilterContext *ctx = inlink->dst;
398 AudioCrossoverContext *s = ctx->priv;
399 AVFrame **frames = s->frames;
402 for (i = 0; i < ctx->nb_outputs; i++) {
403 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
406 ret = AVERROR(ENOMEM);
410 frames[i]->pts = in->pts;
417 ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels,
418 ff_filter_get_nb_threads(ctx)));
420 for (i = 0; i < ctx->nb_outputs; i++) {
421 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
428 for (i = 0; i < ctx->nb_outputs; i++)
429 av_frame_free(&frames[i]);
431 s->input_frame = NULL;
436 static av_cold void uninit(AVFilterContext *ctx)
438 AudioCrossoverContext *s = ctx->priv;
441 av_freep(&s->splits);
444 for (i = 0; i < ctx->nb_outputs; i++)
445 av_freep(&ctx->output_pads[i].name);
448 static const AVFilterPad inputs[] = {
451 .type = AVMEDIA_TYPE_AUDIO,
452 .filter_frame = filter_frame,
453 .config_props = config_input,
458 AVFilter ff_af_acrossover = {
459 .name = "acrossover",
460 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
461 .priv_size = sizeof(AudioCrossoverContext),
462 .priv_class = &acrossover_class,
465 .query_formats = query_formats,
468 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
469 AVFILTER_FLAG_SLICE_THREADS,