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 av_sscanf(arg, "%f", &freq);
104 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
105 return AVERROR(EINVAL);
108 if (i > 0 && freq <= s->splits[i-1]) {
109 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
110 return AVERROR(EINVAL);
118 for (i = 0; i <= s->nb_splits; i++) {
119 AVFilterPad pad = { 0 };
122 pad.type = AVMEDIA_TYPE_AUDIO;
123 name = av_asprintf("out%d", ctx->nb_outputs);
125 return AVERROR(ENOMEM);
128 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
137 static void set_lp(BiquadContext *b, double fc, double q, double sr)
139 double omega = 2.0 * M_PI * fc / sr;
140 double sn = sin(omega);
141 double cs = cos(omega);
142 double alpha = sn / (2. * q);
143 double inv = 1.0 / (1.0 + alpha);
145 b->a0 = (1. - cs) * 0.5 * inv;
146 b->a1 = (1. - cs) * inv;
148 b->b1 = -2. * cs * inv;
149 b->b2 = (1. - alpha) * inv;
152 static void set_hp(BiquadContext *b, double fc, double q, double sr)
154 double omega = 2 * M_PI * fc / sr;
155 double sn = sin(omega);
156 double cs = cos(omega);
157 double alpha = sn / (2 * q);
158 double inv = 1.0 / (1.0 + alpha);
160 b->a0 = inv * (1. + cs) / 2.;
163 b->b1 = -2. * cs * inv;
164 b->b2 = (1. - alpha) * inv;
167 static int config_input(AVFilterLink *inlink)
169 AVFilterContext *ctx = inlink->dst;
170 AudioCrossoverContext *s = ctx->priv;
171 int ch, band, sample_rate = inlink->sample_rate;
174 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
176 return AVERROR(ENOMEM);
193 for (ch = 0; ch < inlink->channels; ch++) {
194 for (band = 0; band <= s->nb_splits; band++) {
195 set_lp(&s->xover[ch].lp[band][0], s->splits[band], q, sample_rate);
196 set_hp(&s->xover[ch].hp[band][0], s->splits[band], q, sample_rate);
199 set_lp(&s->xover[ch].lp[band][1], s->splits[band], 1.34, sample_rate);
200 set_hp(&s->xover[ch].hp[band][1], s->splits[band], 1.34, sample_rate);
201 set_lp(&s->xover[ch].lp[band][2], s->splits[band], q, sample_rate);
202 set_hp(&s->xover[ch].hp[band][2], s->splits[band], q, sample_rate);
203 set_lp(&s->xover[ch].lp[band][3], s->splits[band], 1.34, sample_rate);
204 set_hp(&s->xover[ch].hp[band][3], s->splits[band], 1.34, sample_rate);
206 set_lp(&s->xover[ch].lp[band][1], s->splits[band], q, sample_rate);
207 set_hp(&s->xover[ch].hp[band][1], s->splits[band], q, sample_rate);
215 static int query_formats(AVFilterContext *ctx)
217 AVFilterFormats *formats;
218 AVFilterChannelLayouts *layouts;
219 static const enum AVSampleFormat sample_fmts[] = {
225 layouts = ff_all_channel_counts();
227 return AVERROR(ENOMEM);
228 ret = ff_set_common_channel_layouts(ctx, layouts);
232 formats = ff_make_format_list(sample_fmts);
234 return AVERROR(ENOMEM);
235 ret = ff_set_common_formats(ctx, formats);
239 formats = ff_all_samplerates();
241 return AVERROR(ENOMEM);
242 return ff_set_common_samplerates(ctx, formats);
245 static double biquad_process(BiquadContext *b, double in)
247 double out = in * b->a0 + b->i1 * b->a1 + b->i2 * b->a2 - b->o1 * b->b1 - b->o2 * b->b2;
257 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
259 AudioCrossoverContext *s = ctx->priv;
260 AVFrame *in = s->input_frame;
261 AVFrame **frames = s->frames;
262 const int start = (in->channels * jobnr) / nb_jobs;
263 const int end = (in->channels * (jobnr+1)) / nb_jobs;
266 for (int ch = start; ch < end; ch++) {
267 const double *src = (const double *)in->extended_data[ch];
268 CrossoverChannel *xover = &s->xover[ch];
270 for (int i = 0; i < in->nb_samples; i++) {
271 double sample = src[i], lo, hi;
273 for (band = 0; band < ctx->nb_outputs; band++) {
274 double *dst = (double *)frames[band]->extended_data[ch];
278 for (f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
279 BiquadContext *lp = &xover->lp[band][f];
280 lo = biquad_process(lp, lo);
283 for (f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
284 BiquadContext *hp = &xover->hp[band][f];
285 hi = biquad_process(hp, hi);
298 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
300 AVFilterContext *ctx = inlink->dst;
301 AudioCrossoverContext *s = ctx->priv;
302 AVFrame **frames = s->frames;
305 for (i = 0; i < ctx->nb_outputs; i++) {
306 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
309 ret = AVERROR(ENOMEM);
313 frames[i]->pts = in->pts;
320 ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels,
321 ff_filter_get_nb_threads(ctx)));
323 for (i = 0; i < ctx->nb_outputs; i++) {
324 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
331 for (i = 0; i < ctx->nb_outputs; i++)
332 av_frame_free(&frames[i]);
334 s->input_frame = NULL;
339 static av_cold void uninit(AVFilterContext *ctx)
341 AudioCrossoverContext *s = ctx->priv;
344 av_freep(&s->splits);
347 for (i = 0; i < ctx->nb_outputs; i++)
348 av_freep(&ctx->output_pads[i].name);
351 static const AVFilterPad inputs[] = {
354 .type = AVMEDIA_TYPE_AUDIO,
355 .filter_frame = filter_frame,
356 .config_props = config_input,
361 AVFilter ff_af_acrossover = {
362 .name = "acrossover",
363 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
364 .priv_size = sizeof(AudioCrossoverContext),
365 .priv_class = &acrossover_class,
368 .query_formats = query_formats,
371 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
372 AVFILTER_FLAG_SLICE_THREADS,