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 {
64 CrossoverChannel *xover;
67 AVFrame *frames[MAX_BANDS];
68 } AudioCrossoverContext;
70 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
71 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
73 static const AVOption acrossover_options[] = {
74 { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
75 { "order", "set order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, "m" },
76 { "2nd", "2nd order", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "m" },
77 { "4th", "4th order", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "m" },
78 { "6th", "6th order", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "m" },
79 { "8th", "8th order", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "m" },
80 { "10th", "10th order", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "m" },
81 { "12th", "12th order", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, "m" },
82 { "14th", "14th order", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, "m" },
83 { "16th", "16th order", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, "m" },
84 { "18th", "18th order", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, "m" },
85 { "20th", "20th order", 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, "m" },
89 AVFILTER_DEFINE_CLASS(acrossover);
91 static av_cold int init(AVFilterContext *ctx)
93 AudioCrossoverContext *s = ctx->priv;
94 char *p, *arg, *saveptr = NULL;
97 s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
99 return AVERROR(ENOMEM);
102 for (i = 0; i < MAX_SPLITS; i++) {
105 if (!(arg = av_strtok(p, " |", &saveptr)))
110 if (av_sscanf(arg, "%f", &freq) != 1) {
111 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
112 return AVERROR(EINVAL);
115 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
116 return AVERROR(EINVAL);
119 if (i > 0 && freq <= s->splits[i-1]) {
120 av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
121 return AVERROR(EINVAL);
129 for (i = 0; i <= s->nb_splits; i++) {
130 AVFilterPad pad = { 0 };
133 pad.type = AVMEDIA_TYPE_AUDIO;
134 name = av_asprintf("out%d", ctx->nb_outputs);
136 return AVERROR(ENOMEM);
139 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
148 static void set_lp(BiquadContext *b, double fc, double q, double sr)
150 double omega = M_PI * fc / sr;
151 double cosine = cos(omega);
152 double alpha = sin(omega) / (2. * q);
154 double b0 = (1. - cosine) / 2.;
155 double b1 = 1. - cosine;
156 double b2 = (1. - cosine) / 2.;
157 double a0 = 1. + alpha;
158 double a1 = -2. * cosine;
159 double a2 = 1. - alpha;
168 static void set_hp(BiquadContext *b, double fc, double q, double sr)
170 double omega = M_PI * fc / sr;
171 double cosine = cos(omega);
172 double alpha = sin(omega) / (2. * q);
174 double b0 = (1. + cosine) / 2.;
175 double b1 = -1. - cosine;
176 double b2 = (1. + cosine) / 2.;
177 double a0 = 1. + alpha;
178 double a1 = -2. * cosine;
179 double a2 = 1. - alpha;
188 static void set_ap(BiquadContext *b, double fc, double q, double sr)
190 double omega = M_PI * fc / sr;
191 double cosine = cos(omega);
192 double alpha = sin(omega) / (2. * q);
194 double a0 = 1. + alpha;
195 double a1 = -2. * cosine;
196 double a2 = 1. - alpha;
208 static void calc_q_factors(int order, double *q)
210 double n = order / 2.;
212 for (int i = 0; i < n / 2; i++)
213 q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
216 static int config_input(AVFilterLink *inlink)
218 AVFilterContext *ctx = inlink->dst;
219 AudioCrossoverContext *s = ctx->priv;
220 int sample_rate = inlink->sample_rate;
224 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
226 return AVERROR(ENOMEM);
228 s->order = (s->order_opt + 1) * 2;
229 s->filter_count = s->order / 2;
230 first_order = s->filter_count & 1;
231 calc_q_factors(s->order, q);
233 for (int ch = 0; ch < inlink->channels; ch++) {
234 for (int band = 0; band <= s->nb_splits; band++) {
236 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
237 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
240 for (int n = first_order; n < s->filter_count; n++) {
241 const int idx = s->filter_count / 2 - ((n + first_order) / 2 - first_order) - 1;
243 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
244 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);
246 for (int x = 0; x <= s->nb_splits; x++)
247 set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
255 static int query_formats(AVFilterContext *ctx)
257 AVFilterFormats *formats;
258 AVFilterChannelLayouts *layouts;
259 static const enum AVSampleFormat sample_fmts[] = {
265 layouts = ff_all_channel_counts();
267 return AVERROR(ENOMEM);
268 ret = ff_set_common_channel_layouts(ctx, layouts);
272 formats = ff_make_format_list(sample_fmts);
274 return AVERROR(ENOMEM);
275 ret = ff_set_common_formats(ctx, formats);
279 formats = ff_all_samplerates();
281 return AVERROR(ENOMEM);
282 return ff_set_common_samplerates(ctx, formats);
285 static void biquad_process(BiquadContext *b,
286 double *dst, const double *src,
289 const double b0 = b->b0;
290 const double b1 = b->b1;
291 const double b2 = b->b2;
292 const double a1 = b->a1;
293 const double a2 = b->a2;
297 for (int n = 0; n < nb_samples; n++) {
298 const double in = src[n];
302 z1 = b1 * in + z2 + a1 * out;
303 z2 = b2 * in + a2 * out;
311 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
313 AudioCrossoverContext *s = ctx->priv;
314 AVFrame *in = s->input_frame;
315 AVFrame **frames = s->frames;
316 const int start = (in->channels * jobnr) / nb_jobs;
317 const int end = (in->channels * (jobnr+1)) / nb_jobs;
318 const int nb_samples = in->nb_samples;
320 for (int ch = start; ch < end; ch++) {
321 CrossoverChannel *xover = &s->xover[ch];
323 for (int band = 0; band < ctx->nb_outputs; band++) {
324 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
325 const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch];
326 double *dst = (double *)frames[band + 1]->extended_data[ch];
327 const double *hsrc = f == 0 ? src : dst;
328 BiquadContext *hp = &xover->hp[band][f];
330 biquad_process(hp, dst, hsrc, nb_samples);
333 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {
334 const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch];
335 double *dst = (double *)frames[band]->extended_data[ch];
336 const double *lsrc = f == 0 ? src : dst;
337 BiquadContext *lp = &xover->lp[band][f];
339 biquad_process(lp, dst, lsrc, nb_samples);
342 for (int aband = band + 1; aband < ctx->nb_outputs; aband++) {
343 for (int f = 0; f < s->filter_count / 2; f++) {
344 const double *src = (const double *)frames[band]->extended_data[ch];
345 double *dst = (double *)frames[band]->extended_data[ch];
346 BiquadContext *ap = &xover->ap[band][aband][f * 2 + (s->filter_count & 1)];
348 biquad_process(ap, dst, src, nb_samples);
353 for (int band = 0; band < ctx->nb_outputs && (s->filter_count & 1); band++) {
355 double *dst = (double *)frames[band]->extended_data[ch];
357 for (int n = 0; n < nb_samples; n++)
366 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
368 AVFilterContext *ctx = inlink->dst;
369 AudioCrossoverContext *s = ctx->priv;
370 AVFrame **frames = s->frames;
373 for (i = 0; i < ctx->nb_outputs; i++) {
374 frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
377 ret = AVERROR(ENOMEM);
381 frames[i]->pts = in->pts;
388 ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels,
389 ff_filter_get_nb_threads(ctx)));
391 for (i = 0; i < ctx->nb_outputs; i++) {
392 ret = ff_filter_frame(ctx->outputs[i], frames[i]);
399 for (i = 0; i < ctx->nb_outputs; i++)
400 av_frame_free(&frames[i]);
402 s->input_frame = NULL;
407 static av_cold void uninit(AVFilterContext *ctx)
409 AudioCrossoverContext *s = ctx->priv;
412 av_freep(&s->splits);
415 for (i = 0; i < ctx->nb_outputs; i++)
416 av_freep(&ctx->output_pads[i].name);
419 static const AVFilterPad inputs[] = {
422 .type = AVMEDIA_TYPE_AUDIO,
423 .filter_frame = filter_frame,
424 .config_props = config_input,
429 AVFilter ff_af_acrossover = {
430 .name = "acrossover",
431 .description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
432 .priv_size = sizeof(AudioCrossoverContext),
433 .priv_class = &acrossover_class,
436 .query_formats = query_formats,
439 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
440 AVFILTER_FLAG_SLICE_THREADS,