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/internal.h"
30 #include "libavutil/opt.h"
38 #define MAX_BANDS MAX_SPLITS + 1
40 typedef struct BiquadContext {
47 typedef struct CrossoverChannel {
48 BiquadContext lp[MAX_BANDS][4];
49 BiquadContext hp[MAX_BANDS][4];
52 typedef struct AudioCrossoverContext {
62 CrossoverChannel *xover;
63 } AudioCrossoverContext;
65 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
66 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
68 static const AVOption acrossover_options[] = {
69 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
70 { "order", "set order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "m" },
71 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
72 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
73 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
77 AVFILTER_DEFINE_CLASS(acrossover);
79 static av_cold int init(AVFilterContext *ctx)
81 AudioCrossoverContext *s = ctx->priv;
82 char *p, *arg, *saveptr = NULL;
85 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
87 return AVERROR(ENOMEM);
90 for (i = 0; i < MAX_SPLITS; i++) {
93 if (!(arg = av_strtok(p, " |", &saveptr)))
98 ret = sscanf(arg, "%f", &freq);
101 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
102 return AVERROR(EINVAL);
105 if (i > 0 && freq <= s->splits[i-1]) {
106 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
107 return AVERROR(EINVAL);
115 for (i = 0; i <= s->nb_splits; i++) {
116 AVFilterPad pad = { 0 };
119 pad.type = AVMEDIA_TYPE_AUDIO;
120 name = av_asprintf("out%d", ctx->nb_outputs);
122 return AVERROR(ENOMEM);
125 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
134 static void set_lp(BiquadContext *b, float fc, float q, float sr)
136 double omega = (2.0 * M_PI * fc / sr);
137 double sn = sin(omega);
138 double cs = cos(omega);
139 double alpha = (sn / (2 * q));
140 double inv = (1.0 / (1.0 + alpha));
142 b->a2 = b->a0 = (inv * (1.0 - cs) * 0.5);
143 b->a1 = b->a0 + b->a0;
144 b->b1 = -2. * cs * inv;
145 b->b2 = (1. - alpha) * inv;
148 static void set_hp(BiquadContext *b, float fc, float q, float sr)
150 double omega = 2 * M_PI * fc / sr;
151 double sn = sin(omega);
152 double cs = cos(omega);
153 double alpha = sn / (2 * q);
154 double inv = 1.0 / (1.0 + alpha);
156 b->a0 = inv * (1. + cs) / 2.;
159 b->b1 = -2. * cs * inv;
160 b->b2 = (1. - alpha) * inv;
163 static int config_input(AVFilterLink *inlink)
165 AVFilterContext *ctx = inlink->dst;
166 AudioCrossoverContext *s = ctx->priv;
167 int ch, band, sample_rate = inlink->sample_rate;
170 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
172 return AVERROR(ENOMEM);
189 for (ch = 0; ch < inlink->channels; ch++) {
190 for (band = 0; band <= s->nb_splits; band++) {
191 set_lp(&s->xover[ch].lp[band][0], s->splits[band], q, sample_rate);
192 set_hp(&s->xover[ch].hp[band][0], s->splits[band], q, sample_rate);
195 set_lp(&s->xover[ch].lp[band][1], s->splits[band], 1.34, sample_rate);
196 set_hp(&s->xover[ch].hp[band][1], s->splits[band], 1.34, sample_rate);
197 set_lp(&s->xover[ch].lp[band][2], s->splits[band], q, sample_rate);
198 set_hp(&s->xover[ch].hp[band][2], s->splits[band], q, sample_rate);
199 set_lp(&s->xover[ch].lp[band][3], s->splits[band], 1.34, sample_rate);
200 set_hp(&s->xover[ch].hp[band][3], s->splits[band], 1.34, sample_rate);
202 set_lp(&s->xover[ch].lp[band][1], s->splits[band], q, sample_rate);
203 set_hp(&s->xover[ch].hp[band][1], s->splits[band], q, sample_rate);
211 static int query_formats(AVFilterContext *ctx)
213 AVFilterFormats *formats;
214 AVFilterChannelLayouts *layouts;
215 static const enum AVSampleFormat sample_fmts[] = {
221 layouts = ff_all_channel_counts();
223 return AVERROR(ENOMEM);
224 ret = ff_set_common_channel_layouts(ctx, layouts);
228 formats = ff_make_format_list(sample_fmts);
230 return AVERROR(ENOMEM);
231 ret = ff_set_common_formats(ctx, formats);
235 formats = ff_all_samplerates();
237 return AVERROR(ENOMEM);
238 return ff_set_common_samplerates(ctx, formats);
241 static double biquad_process(BiquadContext *b, double in)
243 double out = in * b->a0 + b->i1 * b->a1 + b->i2 * b->a2 - b->o1 * b->b1 - b->o2 * b->b2;
253 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
255 AVFilterContext *ctx = inlink->dst;
256 AudioCrossoverContext *s = ctx->priv;
257 AVFrame *frames[MAX_BANDS] = { NULL };
258 int i, f, ch, band, ret = 0;
260 for (i = 0; i < ctx->nb_outputs; i++) {
261 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
264 ret = AVERROR(ENOMEM);
268 frames[i]->pts = in->pts;
274 for (ch = 0; ch < inlink->channels; ch++) {
275 const double *src = (const double *)in->extended_data[ch];
276 CrossoverChannel *xover = &s->xover[ch];
278 for (band = 0; band < ctx->nb_outputs; band++) {
279 double *dst = (double *)frames[band]->extended_data[ch];
281 for (i = 0; i < in->nb_samples; i++) {
284 for (f = 0; f < s->filter_count; f++) {
285 if (band + 1 < ctx->nb_outputs) {
286 BiquadContext *lp = &xover->lp[band][f];
287 dst[i] = biquad_process(lp, dst[i]);
291 BiquadContext *hp = &xover->hp[band - 1][f];
292 dst[i] = biquad_process(hp, dst[i]);
299 for (i = 0; i < ctx->nb_outputs; i++) {
300 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
311 static av_cold void uninit(AVFilterContext *ctx)
313 AudioCrossoverContext *s = ctx->priv;
316 av_freep(&s->splits);
318 for (i = 0; i < ctx->nb_outputs; i++)
319 av_freep(&ctx->output_pads[i].name);
322 static const AVFilterPad inputs[] = {
325 .type = AVMEDIA_TYPE_AUDIO,
326 .filter_frame = filter_frame,
327 .config_props = config_input,
332 AVFilter ff_af_acrossover = {
333 .name = "acrossover",
334 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
335 .priv_size = sizeof(AudioCrossoverContext),
336 .priv_class = &acrossover_class,
339 .query_formats = query_formats,
342 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,