]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_acrossover.c
avfilter/af_acrossover: add slice threading support
[ffmpeg] / libavfilter / af_acrossover.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /**
20  * @file
21  * Crossover filter
22  *
23  * Split an audio stream into several bands.
24  */
25
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"
32
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37
38 #define MAX_SPLITS 16
39 #define MAX_BANDS MAX_SPLITS + 1
40
41 typedef struct BiquadContext {
42     double a0, a1, a2;
43     double b1, b2;
44     double i1, i2;
45     double o1, o2;
46 } BiquadContext;
47
48 typedef struct CrossoverChannel {
49     BiquadContext lp[MAX_BANDS][4];
50 } CrossoverChannel;
51
52 typedef struct AudioCrossoverContext {
53     const AVClass *class;
54
55     char *splits_str;
56     int order;
57
58     int filter_count;
59     int nb_splits;
60     float *splits;
61
62     CrossoverChannel *xover;
63
64     AVFrame *input_frame;
65     AVFrame *frames[MAX_BANDS];
66 } AudioCrossoverContext;
67
68 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
69 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
70
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" },
77     { NULL }
78 };
79
80 AVFILTER_DEFINE_CLASS(acrossover);
81
82 static av_cold int init(AVFilterContext *ctx)
83 {
84     AudioCrossoverContext *s = ctx->priv;
85     char *p, *arg, *saveptr = NULL;
86     int i, ret = 0;
87
88     s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
89     if (!s->splits)
90         return AVERROR(ENOMEM);
91
92     p = s->splits_str;
93     for (i = 0; i < MAX_SPLITS; i++) {
94         float freq;
95
96         if (!(arg = av_strtok(p, " |", &saveptr)))
97             break;
98
99         p = NULL;
100
101         av_sscanf(arg, "%f", &freq);
102         if (freq <= 0) {
103             av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
104             return AVERROR(EINVAL);
105         }
106
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);
110         }
111
112         s->splits[i] = freq;
113     }
114
115     s->nb_splits = i;
116
117     for (i = 0; i <= s->nb_splits; i++) {
118         AVFilterPad pad  = { 0 };
119         char *name;
120
121         pad.type = AVMEDIA_TYPE_AUDIO;
122         name = av_asprintf("out%d", ctx->nb_outputs);
123         if (!name)
124             return AVERROR(ENOMEM);
125         pad.name = name;
126
127         if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
128             av_freep(&pad.name);
129             return ret;
130         }
131     }
132
133     return ret;
134 }
135
136 static void set_lp(BiquadContext *b, double fc, double q, double sr)
137 {
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);
143
144     b->a0 = (1. - cs) * 0.5 * inv;
145     b->a1 = (1. - cs) * inv;
146     b->a2 = b->a0;
147     b->b1 = -2. * cs * inv;
148     b->b2 = (1. - alpha) * inv;
149 }
150
151 static int config_input(AVFilterLink *inlink)
152 {
153     AVFilterContext *ctx = inlink->dst;
154     AudioCrossoverContext *s = ctx->priv;
155     int ch, band, sample_rate = inlink->sample_rate;
156     double q;
157
158     s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
159     if (!s->xover)
160         return AVERROR(ENOMEM);
161
162     switch (s->order) {
163     case 0:
164         q = 0.5;
165         s->filter_count = 1;
166         break;
167     case 1:
168         q = M_SQRT1_2;
169         s->filter_count = 2;
170         break;
171     case 2:
172         q = 0.54;
173         s->filter_count = 4;
174         break;
175     }
176
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);
180
181             if (s->order > 1) {
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);
185             } else {
186                 set_lp(&s->xover[ch].lp[band][1], s->splits[band], q, sample_rate);
187             }
188         }
189     }
190
191     return 0;
192 }
193
194 static int query_formats(AVFilterContext *ctx)
195 {
196     AVFilterFormats *formats;
197     AVFilterChannelLayouts *layouts;
198     static const enum AVSampleFormat sample_fmts[] = {
199         AV_SAMPLE_FMT_DBLP,
200         AV_SAMPLE_FMT_NONE
201     };
202     int ret;
203
204     layouts = ff_all_channel_counts();
205     if (!layouts)
206         return AVERROR(ENOMEM);
207     ret = ff_set_common_channel_layouts(ctx, layouts);
208     if (ret < 0)
209         return ret;
210
211     formats = ff_make_format_list(sample_fmts);
212     if (!formats)
213         return AVERROR(ENOMEM);
214     ret = ff_set_common_formats(ctx, formats);
215     if (ret < 0)
216         return ret;
217
218     formats = ff_all_samplerates();
219     if (!formats)
220         return AVERROR(ENOMEM);
221     return ff_set_common_samplerates(ctx, formats);
222 }
223
224 static double biquad_process(BiquadContext *b, double in)
225 {
226     double out = in * b->a0 + b->i1 * b->a1 + b->i2 * b->a2 - b->o1 * b->b1 - b->o2 * b->b2;
227
228     b->i2 = b->i1;
229     b->o2 = b->o1;
230     b->i1 = in;
231     b->o1 = out;
232
233     return out;
234 }
235
236 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
237 {
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;
243     int f, band;
244
245     for (int ch = start; ch < end; ch++) {
246         const double *src = (const double *)in->extended_data[ch];
247         CrossoverChannel *xover = &s->xover[ch];
248
249         for (int i = 0; i < in->nb_samples; i++) {
250             double sample = src[i], lo, hi;
251
252             for (band = 0; band < ctx->nb_outputs; band++) {
253                 double *dst = (double *)frames[band]->extended_data[ch];
254
255                 lo = sample;
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);
259                 }
260
261                 hi = sample - lo;
262
263                 dst[i] = lo;
264
265                 sample = hi;
266             }
267         }
268     }
269
270     return 0;
271 }
272
273 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
274 {
275     AVFilterContext *ctx = inlink->dst;
276     AudioCrossoverContext *s = ctx->priv;
277     AVFrame **frames = s->frames;
278     int i, ret = 0;
279
280     for (i = 0; i < ctx->nb_outputs; i++) {
281         frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
282
283         if (!frames[i]) {
284             ret = AVERROR(ENOMEM);
285             break;
286         }
287
288         frames[i]->pts = in->pts;
289     }
290
291     if (ret < 0)
292         goto fail;
293
294     s->input_frame = in;
295     ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels,
296                                                                    ff_filter_get_nb_threads(ctx)));
297
298     for (i = 0; i < ctx->nb_outputs; i++) {
299         ret = ff_filter_frame(ctx->outputs[i], frames[i]);
300         frames[i] = NULL;
301         if (ret < 0)
302             break;
303     }
304
305 fail:
306     av_frame_free(&in);
307     s->input_frame = NULL;
308
309     return ret;
310 }
311
312 static av_cold void uninit(AVFilterContext *ctx)
313 {
314     AudioCrossoverContext *s = ctx->priv;
315     int i;
316
317     av_freep(&s->splits);
318     av_freep(&s->xover);
319
320     for (i = 0; i < ctx->nb_outputs; i++)
321         av_freep(&ctx->output_pads[i].name);
322 }
323
324 static const AVFilterPad inputs[] = {
325     {
326         .name         = "default",
327         .type         = AVMEDIA_TYPE_AUDIO,
328         .filter_frame = filter_frame,
329         .config_props = config_input,
330     },
331     { NULL }
332 };
333
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,
339     .init           = init,
340     .uninit         = uninit,
341     .query_formats  = query_formats,
342     .inputs         = inputs,
343     .outputs        = NULL,
344     .flags          = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
345                       AVFILTER_FLAG_SLICE_THREADS,
346 };