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];
52 typedef struct AudioCrossoverContext {
62 CrossoverChannel *xover;
65 AVFrame *frames[MAX_BANDS];
66 } AudioCrossoverContext;
68 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
69 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
71 static const AVOption acrossover_options[] = {
72 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
73 { "order", "set order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "m" },
74 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
75 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
76 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
80 AVFILTER_DEFINE_CLASS(acrossover);
82 static av_cold int init(AVFilterContext *ctx)
84 AudioCrossoverContext *s = ctx->priv;
85 char *p, *arg, *saveptr = NULL;
88 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
90 return AVERROR(ENOMEM);
93 for (i = 0; i < MAX_SPLITS; i++) {
96 if (!(arg = av_strtok(p, " |", &saveptr)))
101 av_sscanf(arg, "%f", &freq);
103 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
104 return AVERROR(EINVAL);
107 if (i > 0 && freq <= s->splits[i-1]) {
108 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
109 return AVERROR(EINVAL);
117 for (i = 0; i <= s->nb_splits; i++) {
118 AVFilterPad pad = { 0 };
121 pad.type = AVMEDIA_TYPE_AUDIO;
122 name = av_asprintf("out%d", ctx->nb_outputs);
124 return AVERROR(ENOMEM);
127 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
136 static void set_lp(BiquadContext *b, double fc, double q, double sr)
138 double omega = 2.0 * M_PI * fc / sr;
139 double sn = sin(omega);
140 double cs = cos(omega);
141 double alpha = sn / (2. * q);
142 double inv = 1.0 / (1.0 + alpha);
144 b->a0 = (1. - cs) * 0.5 * inv;
145 b->a1 = (1. - cs) * inv;
147 b->b1 = -2. * cs * inv;
148 b->b2 = (1. - alpha) * inv;
151 static int config_input(AVFilterLink *inlink)
153 AVFilterContext *ctx = inlink->dst;
154 AudioCrossoverContext *s = ctx->priv;
155 int ch, band, sample_rate = inlink->sample_rate;
158 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
160 return AVERROR(ENOMEM);
177 for (ch = 0; ch < inlink->channels; ch++) {
178 for (band = 0; band <= s->nb_splits; band++) {
179 set_lp(&s->xover[ch].lp[band][0], s->splits[band], q, sample_rate);
182 set_lp(&s->xover[ch].lp[band][1], s->splits[band], 1.34, sample_rate);
183 set_lp(&s->xover[ch].lp[band][2], s->splits[band], q, sample_rate);
184 set_lp(&s->xover[ch].lp[band][3], s->splits[band], 1.34, sample_rate);
186 set_lp(&s->xover[ch].lp[band][1], s->splits[band], q, sample_rate);
194 static int query_formats(AVFilterContext *ctx)
196 AVFilterFormats *formats;
197 AVFilterChannelLayouts *layouts;
198 static const enum AVSampleFormat sample_fmts[] = {
204 layouts = ff_all_channel_counts();
206 return AVERROR(ENOMEM);
207 ret = ff_set_common_channel_layouts(ctx, layouts);
211 formats = ff_make_format_list(sample_fmts);
213 return AVERROR(ENOMEM);
214 ret = ff_set_common_formats(ctx, formats);
218 formats = ff_all_samplerates();
220 return AVERROR(ENOMEM);
221 return ff_set_common_samplerates(ctx, formats);
224 static double biquad_process(BiquadContext *b, double in)
226 double out = in * b->a0 + b->i1 * b->a1 + b->i2 * b->a2 - b->o1 * b->b1 - b->o2 * b->b2;
236 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
238 AudioCrossoverContext *s = ctx->priv;
239 AVFrame *in = s->input_frame;
240 AVFrame **frames = s->frames;
241 const int start = (in->channels * jobnr) / nb_jobs;
242 const int end = (in->channels * (jobnr+1)) / nb_jobs;
245 for (int ch = start; ch < end; ch++) {
246 const double *src = (const double *)in->extended_data[ch];
247 CrossoverChannel *xover = &s->xover[ch];
249 for (int i = 0; i < in->nb_samples; i++) {
250 double sample = src[i], lo, hi;
252 for (band = 0; band < ctx->nb_outputs; band++) {
253 double *dst = (double *)frames[band]->extended_data[ch];
256 for (f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
257 BiquadContext *lp = &xover->lp[band][f];
258 lo = biquad_process(lp, lo);
273 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
275 AVFilterContext *ctx = inlink->dst;
276 AudioCrossoverContext *s = ctx->priv;
277 AVFrame **frames = s->frames;
280 for (i = 0; i < ctx->nb_outputs; i++) {
281 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
284 ret = AVERROR(ENOMEM);
288 frames[i]->pts = in->pts;
295 ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels,
296 ff_filter_get_nb_threads(ctx)));
298 for (i = 0; i < ctx->nb_outputs; i++) {
299 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
307 s->input_frame = NULL;
312 static av_cold void uninit(AVFilterContext *ctx)
314 AudioCrossoverContext *s = ctx->priv;
317 av_freep(&s->splits);
320 for (i = 0; i < ctx->nb_outputs; i++)
321 av_freep(&ctx->output_pads[i].name);
324 static const AVFilterPad inputs[] = {
327 .type = AVMEDIA_TYPE_AUDIO,
328 .filter_frame = filter_frame,
329 .config_props = config_input,
334 AVFilter ff_af_acrossover = {
335 .name = "acrossover",
336 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
337 .priv_size = sizeof(AudioCrossoverContext),
338 .priv_class = &acrossover_class,
341 .query_formats = query_formats,
344 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
345 AVFILTER_FLAG_SLICE_THREADS,