]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_acrossover.c
avfilter/af_acrossover: remove unneeded emms_c() calls
[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/float_dsp.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
33
34 #include "audio.h"
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38
39 #define MAX_SPLITS 16
40 #define MAX_BANDS MAX_SPLITS + 1
41
42 typedef struct BiquadCoeffs {
43     double b0, b1, b2;
44     double a1, a2;
45 } BiquadCoeffs;
46
47 typedef struct BiquadContext {
48     double z1, z2;
49 } BiquadContext;
50
51 typedef struct CrossoverChannel {
52     BiquadContext lp[MAX_BANDS][20];
53     BiquadContext hp[MAX_BANDS][20];
54     BiquadContext ap[MAX_BANDS][MAX_BANDS][20];
55 } CrossoverChannel;
56
57 typedef struct AudioCrossoverContext {
58     const AVClass *class;
59
60     char *splits_str;
61     int order_opt;
62     float level_in;
63
64     int order;
65     int filter_count;
66     int first_order;
67     int ap_filter_count;
68     int nb_splits;
69     float *splits;
70
71     BiquadCoeffs lp[MAX_BANDS][20];
72     BiquadCoeffs hp[MAX_BANDS][20];
73     BiquadCoeffs ap[MAX_BANDS][20];
74
75     CrossoverChannel *xover;
76
77     AVFrame *input_frame;
78     AVFrame *frames[MAX_BANDS];
79
80     int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
81
82     AVFloatDSPContext *fdsp;
83 } AudioCrossoverContext;
84
85 #define OFFSET(x) offsetof(AudioCrossoverContext, x)
86 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
87
88 static const AVOption acrossover_options[] = {
89     { "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF },
90     { "order", "set order",             OFFSET(order_opt),  AV_OPT_TYPE_INT,    {.i64=1},     0, 9, AF, "m" },
91     { "2nd",   "2nd order",             0,                  AV_OPT_TYPE_CONST,  {.i64=0},     0, 0, AF, "m" },
92     { "4th",   "4th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=1},     0, 0, AF, "m" },
93     { "6th",   "6th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=2},     0, 0, AF, "m" },
94     { "8th",   "8th order",             0,                  AV_OPT_TYPE_CONST,  {.i64=3},     0, 0, AF, "m" },
95     { "10th",  "10th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=4},     0, 0, AF, "m" },
96     { "12th",  "12th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=5},     0, 0, AF, "m" },
97     { "14th",  "14th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=6},     0, 0, AF, "m" },
98     { "16th",  "16th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=7},     0, 0, AF, "m" },
99     { "18th",  "18th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=8},     0, 0, AF, "m" },
100     { "20th",  "20th order",            0,                  AV_OPT_TYPE_CONST,  {.i64=9},     0, 0, AF, "m" },
101     { "level", "set input gain",        OFFSET(level_in),   AV_OPT_TYPE_FLOAT,  {.dbl=1},     0, 1, AF },
102     { NULL }
103 };
104
105 AVFILTER_DEFINE_CLASS(acrossover);
106
107 static av_cold int init(AVFilterContext *ctx)
108 {
109     AudioCrossoverContext *s = ctx->priv;
110     char *p, *arg, *saveptr = NULL;
111     int i, ret = 0;
112
113     s->fdsp = avpriv_float_dsp_alloc(0);
114     if (!s->fdsp)
115         return AVERROR(ENOMEM);
116
117     s->splits = av_calloc(MAX_SPLITS, sizeof(*s->splits));
118     if (!s->splits)
119         return AVERROR(ENOMEM);
120
121     p = s->splits_str;
122     for (i = 0; i < MAX_SPLITS; i++) {
123         float freq;
124
125         if (!(arg = av_strtok(p, " |", &saveptr)))
126             break;
127
128         p = NULL;
129
130         if (av_sscanf(arg, "%f", &freq) != 1) {
131             av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i);
132             return AVERROR(EINVAL);
133         }
134         if (freq <= 0) {
135             av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq);
136             return AVERROR(EINVAL);
137         }
138
139         if (i > 0 && freq <= s->splits[i-1]) {
140             av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq);
141             return AVERROR(EINVAL);
142         }
143
144         s->splits[i] = freq;
145     }
146
147     s->nb_splits = i;
148
149     for (i = 0; i <= s->nb_splits; i++) {
150         AVFilterPad pad  = { 0 };
151         char *name;
152
153         pad.type = AVMEDIA_TYPE_AUDIO;
154         name = av_asprintf("out%d", ctx->nb_outputs);
155         if (!name)
156             return AVERROR(ENOMEM);
157         pad.name = name;
158
159         if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
160             av_freep(&pad.name);
161             return ret;
162         }
163     }
164
165     return ret;
166 }
167
168 static void set_lp(BiquadCoeffs *b, double fc, double q, double sr)
169 {
170     double omega = 2. * M_PI * fc / sr;
171     double cosine = cos(omega);
172     double alpha = sin(omega) / (2. * q);
173
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;
180
181     b->b0 =  b0 / a0;
182     b->b1 =  b1 / a0;
183     b->b2 =  b2 / a0;
184     b->a1 = -a1 / a0;
185     b->a2 = -a2 / a0;
186 }
187
188 static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
189 {
190     double omega = 2. * M_PI * fc / sr;
191     double cosine = cos(omega);
192     double alpha = sin(omega) / (2. * q);
193
194     double b0 = (1. + cosine) / 2.;
195     double b1 = -1. - cosine;
196     double b2 = (1. + cosine) / 2.;
197     double a0 = 1. + alpha;
198     double a1 = -2. * cosine;
199     double a2 = 1. - alpha;
200
201     b->b0 =  b0 / a0;
202     b->b1 =  b1 / a0;
203     b->b2 =  b2 / a0;
204     b->a1 = -a1 / a0;
205     b->a2 = -a2 / a0;
206 }
207
208 static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
209 {
210     double omega = 2. * M_PI * fc / sr;
211     double cosine = cos(omega);
212     double alpha = sin(omega) / (2. * q);
213
214     double a0 = 1. + alpha;
215     double a1 = -2. * cosine;
216     double a2 = 1. - alpha;
217     double b0 = a2;
218     double b1 = a1;
219     double b2 = a0;
220
221     b->b0 =  b0 / a0;
222     b->b1 =  b1 / a0;
223     b->b2 =  b2 / a0;
224     b->a1 = -a1 / a0;
225     b->a2 = -a2 / a0;
226 }
227
228 static void set_ap1(BiquadCoeffs *b, double fc, double sr)
229 {
230     double omega = 2. * M_PI * fc / sr;
231
232     b->a1 = exp(-omega);
233     b->a2 = 0.;
234     b->b0 = -b->a1;
235     b->b1 = 1.;
236     b->b2 = 0.;
237 }
238
239 static void calc_q_factors(int order, double *q)
240 {
241     double n = order / 2.;
242
243     for (int i = 0; i < n / 2; i++)
244         q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
245 }
246
247 static int query_formats(AVFilterContext *ctx)
248 {
249     AVFilterFormats *formats;
250     AVFilterChannelLayouts *layouts;
251     static const enum AVSampleFormat sample_fmts[] = {
252         AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
253         AV_SAMPLE_FMT_NONE
254     };
255     int ret;
256
257     layouts = ff_all_channel_counts();
258     if (!layouts)
259         return AVERROR(ENOMEM);
260     ret = ff_set_common_channel_layouts(ctx, layouts);
261     if (ret < 0)
262         return ret;
263
264     formats = ff_make_format_list(sample_fmts);
265     if (!formats)
266         return AVERROR(ENOMEM);
267     ret = ff_set_common_formats(ctx, formats);
268     if (ret < 0)
269         return ret;
270
271     formats = ff_all_samplerates();
272     if (!formats)
273         return AVERROR(ENOMEM);
274     return ff_set_common_samplerates(ctx, formats);
275 }
276
277 #define BIQUAD_PROCESS(name, type)                             \
278 static void biquad_process_## name(const BiquadCoeffs *const c,\
279                                    BiquadContext *b,           \
280                                    type *dst, const type *src, \
281                                    int nb_samples)             \
282 {                                                              \
283     const type b0 = c->b0;                                     \
284     const type b1 = c->b1;                                     \
285     const type b2 = c->b2;                                     \
286     const type a1 = c->a1;                                     \
287     const type a2 = c->a2;                                     \
288     type z1 = b->z1;                                           \
289     type z2 = b->z2;                                           \
290                                                                \
291     for (int n = 0; n + 1 < nb_samples; n++) {                 \
292         type in = src[n];                                      \
293         type out;                                              \
294                                                                \
295         out = in * b0 + z1;                                    \
296         z1 = b1 * in + z2 + a1 * out;                          \
297         z2 = b2 * in + a2 * out;                               \
298         dst[n] = out;                                          \
299                                                                \
300         n++;                                                   \
301         in = src[n];                                           \
302         out = in * b0 + z1;                                    \
303         z1 = b1 * in + z2 + a1 * out;                          \
304         z2 = b2 * in + a2 * out;                               \
305         dst[n] = out;                                          \
306     }                                                          \
307                                                                \
308     if (nb_samples & 1) {                                      \
309         const int n = nb_samples - 1;                          \
310         const type in = src[n];                                \
311         type out;                                              \
312                                                                \
313         out = in * b0 + z1;                                    \
314         z1 = b1 * in + z2 + a1 * out;                          \
315         z2 = b2 * in + a2 * out;                               \
316         dst[n] = out;                                          \
317     }                                                          \
318                                                                \
319     b->z1 = z1;                                                \
320     b->z2 = z2;                                                \
321 }
322
323 BIQUAD_PROCESS(fltp, float)
324 BIQUAD_PROCESS(dblp, double)
325
326 #define XOVER_PROCESS(name, type, one, ff)                                                  \
327 static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
328 {                                                                                           \
329     AudioCrossoverContext *s = ctx->priv;                                                   \
330     AVFrame *in = s->input_frame;                                                           \
331     AVFrame **frames = s->frames;                                                           \
332     const int start = (in->channels * jobnr) / nb_jobs;                                     \
333     const int end = (in->channels * (jobnr+1)) / nb_jobs;                                   \
334     const int nb_samples = in->nb_samples;                                                  \
335                                                                                             \
336     for (int ch = start; ch < end; ch++) {                                                  \
337         const type *src = (const type *)in->extended_data[ch];                              \
338         CrossoverChannel *xover = &s->xover[ch];                                            \
339                                                                                             \
340         s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src,       \
341                                     s->level_in, FFALIGN(nb_samples, sizeof(type)));        \
342                                                                                             \
343         for (int band = 0; band < ctx->nb_outputs; band++) {                                \
344             for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {       \
345                 const type *prv = (const type *)frames[band]->extended_data[ch];            \
346                 type *dst = (type *)frames[band + 1]->extended_data[ch];                    \
347                 const type *hsrc = f == 0 ? prv : dst;                                      \
348                 BiquadContext *hp = &xover->hp[band][f];                                    \
349                 BiquadCoeffs *hpc = &s->hp[band][f];                                        \
350                                                                                             \
351                 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples);                     \
352             }                                                                               \
353                                                                                             \
354             for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) {       \
355                 type *dst = (type *)frames[band]->extended_data[ch];                        \
356                 const type *lsrc = dst;                                                     \
357                 BiquadContext *lp = &xover->lp[band][f];                                    \
358                 BiquadCoeffs *lpc = &s->lp[band][f];                                        \
359                                                                                             \
360                 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples);                     \
361             }                                                                               \
362                                                                                             \
363             for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) {              \
364                 if (s->first_order) {                                                       \
365                     const type *asrc = (const type *)frames[band]->extended_data[ch];       \
366                     type *dst = (type *)frames[band]->extended_data[ch];                    \
367                     BiquadContext *ap = &xover->ap[band][aband][0];                         \
368                     BiquadCoeffs *apc = &s->ap[aband][0];                                   \
369                                                                                             \
370                     biquad_process_## name(apc, ap, dst, asrc, nb_samples);                 \
371                 }                                                                           \
372                                                                                             \
373                 for (int f = s->first_order; f < s->ap_filter_count; f++) {                 \
374                     const type *asrc = (const type *)frames[band]->extended_data[ch];       \
375                     type *dst = (type *)frames[band]->extended_data[ch];                    \
376                     BiquadContext *ap = &xover->ap[band][aband][f];                         \
377                     BiquadCoeffs *apc = &s->ap[aband][f];                                   \
378                                                                                             \
379                     biquad_process_## name(apc, ap, dst, asrc, nb_samples);                 \
380                 }                                                                           \
381             }                                                                               \
382         }                                                                                   \
383                                                                                             \
384         for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) {              \
385             if (band & 1) {                                                                 \
386                 type *dst = (type *)frames[band]->extended_data[ch];                        \
387                 s->fdsp->vector_## ff ##mul_scalar(dst, dst, -one,                          \
388                                                    FFALIGN(nb_samples, sizeof(type)));      \
389             }                                                                               \
390         }                                                                                   \
391     }                                                                                       \
392                                                                                             \
393     return 0;                                                                               \
394 }
395
396 XOVER_PROCESS(fltp, float, 1.f, f)
397 XOVER_PROCESS(dblp, double, 1.0, d)
398
399 static int config_input(AVFilterLink *inlink)
400 {
401     AVFilterContext *ctx = inlink->dst;
402     AudioCrossoverContext *s = ctx->priv;
403     int sample_rate = inlink->sample_rate;
404     double q[16];
405
406     s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
407     if (!s->xover)
408         return AVERROR(ENOMEM);
409
410     s->order = (s->order_opt + 1) * 2;
411     s->filter_count = s->order / 2;
412     s->first_order = s->filter_count & 1;
413     s->ap_filter_count = s->filter_count / 2 + s->first_order;
414     calc_q_factors(s->order, q);
415
416     for (int band = 0; band <= s->nb_splits; band++) {
417         if (s->first_order) {
418             set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
419             set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
420         }
421
422         for (int n = s->first_order; n < s->filter_count; n++) {
423             const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
424
425             set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
426             set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
427         }
428
429         if (s->first_order)
430             set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
431
432         for (int n = s->first_order; n < s->ap_filter_count; n++) {
433             const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
434
435             set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
436         }
437     }
438
439     switch (inlink->format) {
440     case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
441     case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
442     }
443
444     return 0;
445 }
446
447 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
448 {
449     AVFilterContext *ctx = inlink->dst;
450     AudioCrossoverContext *s = ctx->priv;
451     AVFrame **frames = s->frames;
452     int i, ret = 0;
453
454     for (i = 0; i < ctx->nb_outputs; i++) {
455         frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples);
456
457         if (!frames[i]) {
458             ret = AVERROR(ENOMEM);
459             break;
460         }
461
462         frames[i]->pts = in->pts;
463     }
464
465     if (ret < 0)
466         goto fail;
467
468     s->input_frame = in;
469     ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels,
470                                                                       ff_filter_get_nb_threads(ctx)));
471
472     for (i = 0; i < ctx->nb_outputs; i++) {
473         ret = ff_filter_frame(ctx->outputs[i], frames[i]);
474         frames[i] = NULL;
475         if (ret < 0)
476             break;
477     }
478
479 fail:
480     for (i = 0; i < ctx->nb_outputs; i++)
481         av_frame_free(&frames[i]);
482     av_frame_free(&in);
483     s->input_frame = NULL;
484
485     return ret;
486 }
487
488 static av_cold void uninit(AVFilterContext *ctx)
489 {
490     AudioCrossoverContext *s = ctx->priv;
491     int i;
492
493     av_freep(&s->fdsp);
494     av_freep(&s->splits);
495     av_freep(&s->xover);
496
497     for (i = 0; i < ctx->nb_outputs; i++)
498         av_freep(&ctx->output_pads[i].name);
499 }
500
501 static const AVFilterPad inputs[] = {
502     {
503         .name         = "default",
504         .type         = AVMEDIA_TYPE_AUDIO,
505         .filter_frame = filter_frame,
506         .config_props = config_input,
507     },
508     { NULL }
509 };
510
511 AVFilter ff_af_acrossover = {
512     .name           = "acrossover",
513     .description    = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."),
514     .priv_size      = sizeof(AudioCrossoverContext),
515     .priv_class     = &acrossover_class,
516     .init           = init,
517     .uninit         = uninit,
518     .query_formats  = query_formats,
519     .inputs         = inputs,
520     .outputs        = NULL,
521     .flags          = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
522                       AVFILTER_FLAG_SLICE_THREADS,
523 };