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 {
48 typedef struct CrossoverChannel {
49 BiquadContext lp[MAX_BANDS][4];
50 BiquadContext hp[MAX_BANDS][4];
53 typedef struct AudioCrossoverContext {
63 CrossoverChannel *xover;
66 AVFrame *frames[MAX_BANDS];
67 } AudioCrossoverContext;
69 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
70 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
72 static const AVOption acrossover_options[] = {
73 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
74 { "order", "set order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "m" },
75 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
76 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
77 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
81 AVFILTER_DEFINE_CLASS(acrossover);
83 static av_cold int init(AVFilterContext *ctx)
85 AudioCrossoverContext *s = ctx->priv;
86 char *p, *arg, *saveptr = NULL;
89 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
91 return AVERROR(ENOMEM);
94 for (i = 0; i < MAX_SPLITS; i++) {
97 if (!(arg = av_strtok(p, " |", &saveptr)))
102 if (av_sscanf(arg, "%f", &freq) != 1) {
103 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
104 return AVERROR(EINVAL);
107 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
108 return AVERROR(EINVAL);
111 if (i > 0 && freq <= s->splits[i-1]) {
112 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
113 return AVERROR(EINVAL);
121 for (i = 0; i <= s->nb_splits; i++) {
122 AVFilterPad pad = { 0 };
125 pad.type = AVMEDIA_TYPE_AUDIO;
126 name = av_asprintf("out%d", ctx->nb_outputs);
128 return AVERROR(ENOMEM);
131 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
140 static void set_lp(BiquadContext *b, double fc, double q, double sr)
142 double omega = 2.0 * M_PI * fc / sr;
143 double sn = sin(omega);
144 double cs = cos(omega);
145 double alpha = sn / (2. * q);
146 double inv = 1.0 / (1.0 + alpha);
148 b->a0 = (1. - cs) * 0.5 * inv;
149 b->a1 = (1. - cs) * inv;
151 b->b1 = -2. * cs * inv;
152 b->b2 = (1. - alpha) * inv;
155 static void set_hp(BiquadContext *b, double fc, double q, double sr)
157 double omega = 2 * M_PI * fc / sr;
158 double sn = sin(omega);
159 double cs = cos(omega);
160 double alpha = sn / (2 * q);
161 double inv = 1.0 / (1.0 + alpha);
163 b->a0 = inv * (1. + cs) / 2.;
166 b->b1 = -2. * cs * inv;
167 b->b2 = (1. - alpha) * inv;
170 static int config_input(AVFilterLink *inlink)
172 AVFilterContext *ctx = inlink->dst;
173 AudioCrossoverContext *s = ctx->priv;
174 int ch, band, sample_rate = inlink->sample_rate;
177 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
179 return AVERROR(ENOMEM);
196 for (ch = 0; ch < inlink->channels; ch++) {
197 for (band = 0; band <= s->nb_splits; band++) {
198 set_lp(&s->xover[ch].lp[band][0], s->splits[band], q, sample_rate);
199 set_hp(&s->xover[ch].hp[band][0], s->splits[band], q, sample_rate);
202 set_lp(&s->xover[ch].lp[band][1], s->splits[band], 1.34, sample_rate);
203 set_hp(&s->xover[ch].hp[band][1], s->splits[band], 1.34, sample_rate);
204 set_lp(&s->xover[ch].lp[band][2], s->splits[band], q, sample_rate);
205 set_hp(&s->xover[ch].hp[band][2], s->splits[band], q, sample_rate);
206 set_lp(&s->xover[ch].lp[band][3], s->splits[band], 1.34, sample_rate);
207 set_hp(&s->xover[ch].hp[band][3], s->splits[band], 1.34, sample_rate);
209 set_lp(&s->xover[ch].lp[band][1], s->splits[band], q, sample_rate);
210 set_hp(&s->xover[ch].hp[band][1], s->splits[band], q, sample_rate);
218 static int query_formats(AVFilterContext *ctx)
220 AVFilterFormats *formats;
221 AVFilterChannelLayouts *layouts;
222 static const enum AVSampleFormat sample_fmts[] = {
228 layouts = ff_all_channel_counts();
230 return AVERROR(ENOMEM);
231 ret = ff_set_common_channel_layouts(ctx, layouts);
235 formats = ff_make_format_list(sample_fmts);
237 return AVERROR(ENOMEM);
238 ret = ff_set_common_formats(ctx, formats);
242 formats = ff_all_samplerates();
244 return AVERROR(ENOMEM);
245 return ff_set_common_samplerates(ctx, formats);
248 static double biquad_process(BiquadContext *b, double in)
250 double out = in * b->a0 + b->i1 * b->a1 + b->i2 * b->a2 - b->o1 * b->b1 - b->o2 * b->b2;
260 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
262 AudioCrossoverContext *s = ctx->priv;
263 AVFrame *in = s->input_frame;
264 AVFrame **frames = s->frames;
265 const int start = (in->channels * jobnr) / nb_jobs;
266 const int end = (in->channels * (jobnr+1)) / nb_jobs;
269 for (int ch = start; ch < end; ch++) {
270 const double *src = (const double *)in->extended_data[ch];
271 CrossoverChannel *xover = &s->xover[ch];
273 for (int i = 0; i < in->nb_samples; i++) {
274 double sample = src[i], lo, hi;
276 for (band = 0; band < ctx->nb_outputs; band++) {
277 double *dst = (double *)frames[band]->extended_data[ch];
281 for (f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
282 BiquadContext *lp = &xover->lp[band][f];
283 lo = biquad_process(lp, lo);
286 for (f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
287 BiquadContext *hp = &xover->hp[band][f];
288 hi = biquad_process(hp, hi);
301 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
303 AVFilterContext *ctx = inlink->dst;
304 AudioCrossoverContext *s = ctx->priv;
305 AVFrame **frames = s->frames;
308 for (i = 0; i < ctx->nb_outputs; i++) {
309 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
312 ret = AVERROR(ENOMEM);
316 frames[i]->pts = in->pts;
323 ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels,
324 ff_filter_get_nb_threads(ctx)));
326 for (i = 0; i < ctx->nb_outputs; i++) {
327 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
334 for (i = 0; i < ctx->nb_outputs; i++)
335 av_frame_free(&frames[i]);
337 s->input_frame = NULL;
342 static av_cold void uninit(AVFilterContext *ctx)
344 AudioCrossoverContext *s = ctx->priv;
347 av_freep(&s->splits);
350 for (i = 0; i < ctx->nb_outputs; i++)
351 av_freep(&ctx->output_pads[i].name);
354 static const AVFilterPad inputs[] = {
357 .type = AVMEDIA_TYPE_AUDIO,
358 .filter_frame = filter_frame,
359 .config_props = config_input,
364 AVFilter ff_af_acrossover = {
365 .name = "acrossover",
366 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
367 .priv_size = sizeof(AudioCrossoverContext),
368 .priv_class = &acrossover_class,
371 .query_formats = query_formats,
374 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
375 AVFILTER_FLAG_SLICE_THREADS,