Line |
Branch |
Exec |
Source |
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/float_dsp.h" |
30 |
|
|
#include "libavutil/internal.h" |
31 |
|
|
#include "libavutil/mem.h" |
32 |
|
|
#include "libavutil/opt.h" |
33 |
|
|
|
34 |
|
|
#include "audio.h" |
35 |
|
|
#include "avfilter.h" |
36 |
|
|
#include "filters.h" |
37 |
|
|
#include "formats.h" |
38 |
|
|
|
39 |
|
|
#define MAX_SPLITS 16 |
40 |
|
|
#define MAX_BANDS MAX_SPLITS + 1 |
41 |
|
|
|
42 |
|
|
#define B0 0 |
43 |
|
|
#define B1 1 |
44 |
|
|
#define B2 2 |
45 |
|
|
#define A1 3 |
46 |
|
|
#define A2 4 |
47 |
|
|
|
48 |
|
|
typedef struct BiquadCoeffs { |
49 |
|
|
double cd[5]; |
50 |
|
|
float cf[5]; |
51 |
|
|
} BiquadCoeffs; |
52 |
|
|
|
53 |
|
|
typedef struct AudioCrossoverContext { |
54 |
|
|
const AVClass *class; |
55 |
|
|
|
56 |
|
|
char *splits_str; |
57 |
|
|
char *gains_str; |
58 |
|
|
int order_opt; |
59 |
|
|
float level_in; |
60 |
|
|
int precision; |
61 |
|
|
|
62 |
|
|
int order; |
63 |
|
|
int filter_count; |
64 |
|
|
int first_order; |
65 |
|
|
int ap_filter_count; |
66 |
|
|
int nb_splits; |
67 |
|
|
float splits[MAX_SPLITS]; |
68 |
|
|
|
69 |
|
|
float gains[MAX_BANDS]; |
70 |
|
|
|
71 |
|
|
BiquadCoeffs lp[MAX_BANDS][20]; |
72 |
|
|
BiquadCoeffs hp[MAX_BANDS][20]; |
73 |
|
|
BiquadCoeffs ap[MAX_BANDS][20]; |
74 |
|
|
|
75 |
|
|
AVFrame *xover; |
76 |
|
|
|
77 |
|
|
AVFrame *frames[MAX_BANDS]; |
78 |
|
|
|
79 |
|
|
int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
80 |
|
|
|
81 |
|
|
AVFloatDSPContext *fdsp; |
82 |
|
|
} AudioCrossoverContext; |
83 |
|
|
|
84 |
|
|
#define OFFSET(x) offsetof(AudioCrossoverContext, x) |
85 |
|
|
#define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
86 |
|
|
|
87 |
|
|
static const AVOption acrossover_options[] = { |
88 |
|
|
{ "split", "set split frequencies", OFFSET(splits_str), AV_OPT_TYPE_STRING, {.str="500"}, 0, 0, AF }, |
89 |
|
|
{ "order", "set filter order", OFFSET(order_opt), AV_OPT_TYPE_INT, {.i64=1}, 0, 9, AF, .unit = "m" }, |
90 |
|
|
{ "2nd", "2nd order (12 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "m" }, |
91 |
|
|
{ "4th", "4th order (24 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "m" }, |
92 |
|
|
{ "6th", "6th order (36 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "m" }, |
93 |
|
|
{ "8th", "8th order (48 dB/8ve)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, .unit = "m" }, |
94 |
|
|
{ "10th", "10th order (60 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, .unit = "m" }, |
95 |
|
|
{ "12th", "12th order (72 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, AF, .unit = "m" }, |
96 |
|
|
{ "14th", "14th order (84 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, AF, .unit = "m" }, |
97 |
|
|
{ "16th", "16th order (96 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, AF, .unit = "m" }, |
98 |
|
|
{ "18th", "18th order (108 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, AF, .unit = "m" }, |
99 |
|
|
{ "20th", "20th order (120 dB/8ve)",0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, AF, .unit = "m" }, |
100 |
|
|
{ "level", "set input gain", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF }, |
101 |
|
|
{ "gain", "set output bands gain", OFFSET(gains_str), AV_OPT_TYPE_STRING, {.str="1.f"}, 0, 0, AF }, |
102 |
|
|
{ "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, .unit = "precision" }, |
103 |
|
|
{ "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "precision" }, |
104 |
|
|
{ "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "precision" }, |
105 |
|
|
{ "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "precision" }, |
106 |
|
|
{ NULL } |
107 |
|
|
}; |
108 |
|
|
|
109 |
|
|
AVFILTER_DEFINE_CLASS(acrossover); |
110 |
|
|
|
111 |
|
✗ |
static int query_formats(const AVFilterContext *ctx, |
112 |
|
|
AVFilterFormatsConfig **cfg_in, |
113 |
|
|
AVFilterFormatsConfig **cfg_out) |
114 |
|
|
{ |
115 |
|
✗ |
const AudioCrossoverContext *s = ctx->priv; |
116 |
|
|
static const enum AVSampleFormat auto_sample_fmts[] = { |
117 |
|
|
AV_SAMPLE_FMT_FLTP, |
118 |
|
|
AV_SAMPLE_FMT_DBLP, |
119 |
|
|
AV_SAMPLE_FMT_NONE |
120 |
|
|
}; |
121 |
|
✗ |
enum AVSampleFormat sample_fmts[] = { |
122 |
|
|
AV_SAMPLE_FMT_FLTP, |
123 |
|
|
AV_SAMPLE_FMT_NONE |
124 |
|
|
}; |
125 |
|
✗ |
const enum AVSampleFormat *sample_fmts_list = sample_fmts; |
126 |
|
|
int ret; |
127 |
|
|
|
128 |
|
✗ |
switch (s->precision) { |
129 |
|
✗ |
case 0: |
130 |
|
✗ |
sample_fmts_list = auto_sample_fmts; |
131 |
|
✗ |
break; |
132 |
|
✗ |
case 1: |
133 |
|
✗ |
sample_fmts[0] = AV_SAMPLE_FMT_FLTP; |
134 |
|
✗ |
break; |
135 |
|
✗ |
case 2: |
136 |
|
✗ |
sample_fmts[0] = AV_SAMPLE_FMT_DBLP; |
137 |
|
✗ |
break; |
138 |
|
✗ |
default: |
139 |
|
✗ |
break; |
140 |
|
|
} |
141 |
|
✗ |
ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts_list); |
142 |
|
✗ |
if (ret < 0) |
143 |
|
✗ |
return ret; |
144 |
|
|
|
145 |
|
✗ |
return 0; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
✗ |
static int parse_gains(AVFilterContext *ctx) |
149 |
|
|
{ |
150 |
|
✗ |
AudioCrossoverContext *s = ctx->priv; |
151 |
|
✗ |
char *p, *arg, *saveptr = NULL; |
152 |
|
✗ |
int i, ret = 0; |
153 |
|
|
|
154 |
|
✗ |
saveptr = NULL; |
155 |
|
✗ |
p = s->gains_str; |
156 |
|
✗ |
for (i = 0; i < MAX_BANDS; i++) { |
157 |
|
|
float gain; |
158 |
|
✗ |
char c[3] = { 0 }; |
159 |
|
|
|
160 |
|
✗ |
if (!(arg = av_strtok(p, " |", &saveptr))) |
161 |
|
✗ |
break; |
162 |
|
|
|
163 |
|
✗ |
p = NULL; |
164 |
|
|
|
165 |
|
✗ |
if (av_sscanf(arg, "%f%2s", &gain, c) < 1) { |
166 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Invalid syntax for gain[%d].\n", i); |
167 |
|
✗ |
ret = AVERROR(EINVAL); |
168 |
|
✗ |
break; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
✗ |
if (c[0] == 'd' && c[1] == 'B') |
172 |
|
✗ |
s->gains[i] = expf(gain * M_LN10 / 20.f); |
173 |
|
|
else |
174 |
|
✗ |
s->gains[i] = gain; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
✗ |
for (; i < MAX_BANDS; i++) |
178 |
|
✗ |
s->gains[i] = 1.f; |
179 |
|
|
|
180 |
|
✗ |
return ret; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
✗ |
static av_cold int init(AVFilterContext *ctx) |
184 |
|
|
{ |
185 |
|
✗ |
AudioCrossoverContext *s = ctx->priv; |
186 |
|
✗ |
char *p, *arg, *saveptr = NULL; |
187 |
|
✗ |
int i, ret = 0; |
188 |
|
|
|
189 |
|
✗ |
s->fdsp = avpriv_float_dsp_alloc(0); |
190 |
|
✗ |
if (!s->fdsp) |
191 |
|
✗ |
return AVERROR(ENOMEM); |
192 |
|
|
|
193 |
|
✗ |
p = s->splits_str; |
194 |
|
✗ |
for (i = 0; i < MAX_SPLITS; i++) { |
195 |
|
|
float freq; |
196 |
|
|
|
197 |
|
✗ |
if (!(arg = av_strtok(p, " |", &saveptr))) |
198 |
|
✗ |
break; |
199 |
|
|
|
200 |
|
✗ |
p = NULL; |
201 |
|
|
|
202 |
|
✗ |
if (av_sscanf(arg, "%f", &freq) != 1) { |
203 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Invalid syntax for frequency[%d].\n", i); |
204 |
|
✗ |
return AVERROR(EINVAL); |
205 |
|
|
} |
206 |
|
✗ |
if (freq <= 0) { |
207 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Frequency %f must be positive number.\n", freq); |
208 |
|
✗ |
return AVERROR(EINVAL); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
✗ |
if (i > 0 && freq <= s->splits[i-1]) { |
212 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Frequency %f must be in increasing order.\n", freq); |
213 |
|
✗ |
return AVERROR(EINVAL); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
✗ |
s->splits[i] = freq; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
✗ |
s->nb_splits = i; |
220 |
|
|
|
221 |
|
✗ |
ret = parse_gains(ctx); |
222 |
|
✗ |
if (ret < 0) |
223 |
|
✗ |
return ret; |
224 |
|
|
|
225 |
|
✗ |
for (i = 0; i <= s->nb_splits; i++) { |
226 |
|
✗ |
AVFilterPad pad = { 0 }; |
227 |
|
|
char *name; |
228 |
|
|
|
229 |
|
✗ |
pad.type = AVMEDIA_TYPE_AUDIO; |
230 |
|
✗ |
name = av_asprintf("out%d", ctx->nb_outputs); |
231 |
|
✗ |
if (!name) |
232 |
|
✗ |
return AVERROR(ENOMEM); |
233 |
|
✗ |
pad.name = name; |
234 |
|
|
|
235 |
|
✗ |
if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0) |
236 |
|
✗ |
return ret; |
237 |
|
|
} |
238 |
|
|
|
239 |
|
✗ |
return ret; |
240 |
|
|
} |
241 |
|
|
|
242 |
|
✗ |
static void set_lp(BiquadCoeffs *b, double fc, double q, double sr) |
243 |
|
|
{ |
244 |
|
✗ |
double omega = 2. * M_PI * fc / sr; |
245 |
|
✗ |
double cosine = cos(omega); |
246 |
|
✗ |
double alpha = sin(omega) / (2. * q); |
247 |
|
|
|
248 |
|
✗ |
double b0 = (1. - cosine) / 2.; |
249 |
|
✗ |
double b1 = 1. - cosine; |
250 |
|
✗ |
double b2 = (1. - cosine) / 2.; |
251 |
|
✗ |
double a0 = 1. + alpha; |
252 |
|
✗ |
double a1 = -2. * cosine; |
253 |
|
✗ |
double a2 = 1. - alpha; |
254 |
|
|
|
255 |
|
✗ |
b->cd[B0] = b0 / a0; |
256 |
|
✗ |
b->cd[B1] = b1 / a0; |
257 |
|
✗ |
b->cd[B2] = b2 / a0; |
258 |
|
✗ |
b->cd[A1] = -a1 / a0; |
259 |
|
✗ |
b->cd[A2] = -a2 / a0; |
260 |
|
|
|
261 |
|
✗ |
b->cf[B0] = b->cd[B0]; |
262 |
|
✗ |
b->cf[B1] = b->cd[B1]; |
263 |
|
✗ |
b->cf[B2] = b->cd[B2]; |
264 |
|
✗ |
b->cf[A1] = b->cd[A1]; |
265 |
|
✗ |
b->cf[A2] = b->cd[A2]; |
266 |
|
✗ |
} |
267 |
|
|
|
268 |
|
✗ |
static void set_hp(BiquadCoeffs *b, double fc, double q, double sr) |
269 |
|
|
{ |
270 |
|
✗ |
double omega = 2. * M_PI * fc / sr; |
271 |
|
✗ |
double cosine = cos(omega); |
272 |
|
✗ |
double alpha = sin(omega) / (2. * q); |
273 |
|
|
|
274 |
|
✗ |
double b0 = (1. + cosine) / 2.; |
275 |
|
✗ |
double b1 = -1. - cosine; |
276 |
|
✗ |
double b2 = (1. + cosine) / 2.; |
277 |
|
✗ |
double a0 = 1. + alpha; |
278 |
|
✗ |
double a1 = -2. * cosine; |
279 |
|
✗ |
double a2 = 1. - alpha; |
280 |
|
|
|
281 |
|
✗ |
b->cd[B0] = b0 / a0; |
282 |
|
✗ |
b->cd[B1] = b1 / a0; |
283 |
|
✗ |
b->cd[B2] = b2 / a0; |
284 |
|
✗ |
b->cd[A1] = -a1 / a0; |
285 |
|
✗ |
b->cd[A2] = -a2 / a0; |
286 |
|
|
|
287 |
|
✗ |
b->cf[B0] = b->cd[B0]; |
288 |
|
✗ |
b->cf[B1] = b->cd[B1]; |
289 |
|
✗ |
b->cf[B2] = b->cd[B2]; |
290 |
|
✗ |
b->cf[A1] = b->cd[A1]; |
291 |
|
✗ |
b->cf[A2] = b->cd[A2]; |
292 |
|
✗ |
} |
293 |
|
|
|
294 |
|
✗ |
static void set_ap(BiquadCoeffs *b, double fc, double q, double sr) |
295 |
|
|
{ |
296 |
|
✗ |
double omega = 2. * M_PI * fc / sr; |
297 |
|
✗ |
double cosine = cos(omega); |
298 |
|
✗ |
double alpha = sin(omega) / (2. * q); |
299 |
|
|
|
300 |
|
✗ |
double a0 = 1. + alpha; |
301 |
|
✗ |
double a1 = -2. * cosine; |
302 |
|
✗ |
double a2 = 1. - alpha; |
303 |
|
✗ |
double b0 = a2; |
304 |
|
✗ |
double b1 = a1; |
305 |
|
✗ |
double b2 = a0; |
306 |
|
|
|
307 |
|
✗ |
b->cd[B0] = b0 / a0; |
308 |
|
✗ |
b->cd[B1] = b1 / a0; |
309 |
|
✗ |
b->cd[B2] = b2 / a0; |
310 |
|
✗ |
b->cd[A1] = -a1 / a0; |
311 |
|
✗ |
b->cd[A2] = -a2 / a0; |
312 |
|
|
|
313 |
|
✗ |
b->cf[B0] = b->cd[B0]; |
314 |
|
✗ |
b->cf[B1] = b->cd[B1]; |
315 |
|
✗ |
b->cf[B2] = b->cd[B2]; |
316 |
|
✗ |
b->cf[A1] = b->cd[A1]; |
317 |
|
✗ |
b->cf[A2] = b->cd[A2]; |
318 |
|
✗ |
} |
319 |
|
|
|
320 |
|
✗ |
static void set_ap1(BiquadCoeffs *b, double fc, double sr) |
321 |
|
|
{ |
322 |
|
✗ |
double omega = 2. * M_PI * fc / sr; |
323 |
|
|
|
324 |
|
✗ |
b->cd[A1] = exp(-omega); |
325 |
|
✗ |
b->cd[A2] = 0.; |
326 |
|
✗ |
b->cd[B0] = -b->cd[A1]; |
327 |
|
✗ |
b->cd[B1] = 1.; |
328 |
|
✗ |
b->cd[B2] = 0.; |
329 |
|
|
|
330 |
|
✗ |
b->cf[B0] = b->cd[B0]; |
331 |
|
✗ |
b->cf[B1] = b->cd[B1]; |
332 |
|
✗ |
b->cf[B2] = b->cd[B2]; |
333 |
|
✗ |
b->cf[A1] = b->cd[A1]; |
334 |
|
✗ |
b->cf[A2] = b->cd[A2]; |
335 |
|
✗ |
} |
336 |
|
|
|
337 |
|
✗ |
static void calc_q_factors(int order, double *q) |
338 |
|
|
{ |
339 |
|
✗ |
double n = order / 2.; |
340 |
|
|
|
341 |
|
✗ |
for (int i = 0; i < n / 2; i++) |
342 |
|
✗ |
q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n))); |
343 |
|
✗ |
} |
344 |
|
|
|
345 |
|
|
#define BIQUAD_PROCESS(name, type) \ |
346 |
|
|
static void biquad_process_## name(const type *const c, \ |
347 |
|
|
type *b, \ |
348 |
|
|
type *dst, const type *src, \ |
349 |
|
|
int nb_samples) \ |
350 |
|
|
{ \ |
351 |
|
|
const type b0 = c[B0]; \ |
352 |
|
|
const type b1 = c[B1]; \ |
353 |
|
|
const type b2 = c[B2]; \ |
354 |
|
|
const type a1 = c[A1]; \ |
355 |
|
|
const type a2 = c[A2]; \ |
356 |
|
|
type z1 = b[0]; \ |
357 |
|
|
type z2 = b[1]; \ |
358 |
|
|
\ |
359 |
|
|
for (int n = 0; n + 1 < nb_samples; n++) { \ |
360 |
|
|
type in = src[n]; \ |
361 |
|
|
type out; \ |
362 |
|
|
\ |
363 |
|
|
out = in * b0 + z1; \ |
364 |
|
|
z1 = b1 * in + z2 + a1 * out; \ |
365 |
|
|
z2 = b2 * in + a2 * out; \ |
366 |
|
|
dst[n] = out; \ |
367 |
|
|
\ |
368 |
|
|
n++; \ |
369 |
|
|
in = src[n]; \ |
370 |
|
|
out = in * b0 + z1; \ |
371 |
|
|
z1 = b1 * in + z2 + a1 * out; \ |
372 |
|
|
z2 = b2 * in + a2 * out; \ |
373 |
|
|
dst[n] = out; \ |
374 |
|
|
} \ |
375 |
|
|
\ |
376 |
|
|
if (nb_samples & 1) { \ |
377 |
|
|
const int n = nb_samples - 1; \ |
378 |
|
|
const type in = src[n]; \ |
379 |
|
|
type out; \ |
380 |
|
|
\ |
381 |
|
|
out = in * b0 + z1; \ |
382 |
|
|
z1 = b1 * in + z2 + a1 * out; \ |
383 |
|
|
z2 = b2 * in + a2 * out; \ |
384 |
|
|
dst[n] = out; \ |
385 |
|
|
} \ |
386 |
|
|
\ |
387 |
|
|
b[0] = z1; \ |
388 |
|
|
b[1] = z2; \ |
389 |
|
|
} |
390 |
|
|
|
391 |
|
✗ |
BIQUAD_PROCESS(fltp, float) |
392 |
|
✗ |
BIQUAD_PROCESS(dblp, double) |
393 |
|
|
|
394 |
|
|
#define XOVER_PROCESS(name, type, one, ff) \ |
395 |
|
|
static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ |
396 |
|
|
{ \ |
397 |
|
|
AudioCrossoverContext *s = ctx->priv; \ |
398 |
|
|
AVFrame *in = arg; \ |
399 |
|
|
AVFrame **frames = s->frames; \ |
400 |
|
|
const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs; \ |
401 |
|
|
const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs; \ |
402 |
|
|
const int nb_samples = in->nb_samples; \ |
403 |
|
|
const int nb_outs = ctx->nb_outputs; \ |
404 |
|
|
const int first_order = s->first_order; \ |
405 |
|
|
\ |
406 |
|
|
for (int ch = start; ch < end; ch++) { \ |
407 |
|
|
const type *src = (const type *)in->extended_data[ch]; \ |
408 |
|
|
type *xover = (type *)s->xover->extended_data[ch]; \ |
409 |
|
|
\ |
410 |
|
|
s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \ |
411 |
|
|
s->level_in, FFALIGN(nb_samples, sizeof(type))); \ |
412 |
|
|
\ |
413 |
|
|
for (int band = 0; band < nb_outs; band++) { \ |
414 |
|
|
for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \ |
415 |
|
|
const type *prv = (const type *)frames[band]->extended_data[ch]; \ |
416 |
|
|
type *dst = (type *)frames[band + 1]->extended_data[ch]; \ |
417 |
|
|
const type *hsrc = f == 0 ? prv : dst; \ |
418 |
|
|
type *hp = xover + nb_outs * 20 + band * 20 + f * 2; \ |
419 |
|
|
const type *const hpc = (type *)&s->hp[band][f].c ## ff; \ |
420 |
|
|
\ |
421 |
|
|
biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \ |
422 |
|
|
} \ |
423 |
|
|
\ |
424 |
|
|
for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \ |
425 |
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \ |
426 |
|
|
const type *lsrc = dst; \ |
427 |
|
|
type *lp = xover + band * 20 + f * 2; \ |
428 |
|
|
const type *const lpc = (type *)&s->lp[band][f].c ## ff; \ |
429 |
|
|
\ |
430 |
|
|
biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \ |
431 |
|
|
} \ |
432 |
|
|
\ |
433 |
|
|
for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \ |
434 |
|
|
if (first_order) { \ |
435 |
|
|
const type *asrc = (const type *)frames[band]->extended_data[ch]; \ |
436 |
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \ |
437 |
|
|
type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \ |
438 |
|
|
const type *const apc = (type *)&s->ap[aband][0].c ## ff; \ |
439 |
|
|
\ |
440 |
|
|
biquad_process_## name(apc, ap, dst, asrc, nb_samples); \ |
441 |
|
|
} \ |
442 |
|
|
\ |
443 |
|
|
for (int f = first_order; f < s->ap_filter_count; f++) { \ |
444 |
|
|
const type *asrc = (const type *)frames[band]->extended_data[ch]; \ |
445 |
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \ |
446 |
|
|
type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\ |
447 |
|
|
const type *const apc = (type *)&s->ap[aband][f].c ## ff; \ |
448 |
|
|
\ |
449 |
|
|
biquad_process_## name(apc, ap, dst, asrc, nb_samples); \ |
450 |
|
|
} \ |
451 |
|
|
} \ |
452 |
|
|
} \ |
453 |
|
|
\ |
454 |
|
|
for (int band = 0; band < nb_outs; band++) { \ |
455 |
|
|
const type gain = s->gains[band] * ((band & 1 && first_order) ? -one : one); \ |
456 |
|
|
type *dst = (type *)frames[band]->extended_data[ch]; \ |
457 |
|
|
\ |
458 |
|
|
s->fdsp->vector_## ff ##mul_scalar(dst, dst, gain, \ |
459 |
|
|
FFALIGN(nb_samples, sizeof(type))); \ |
460 |
|
|
} \ |
461 |
|
|
} \ |
462 |
|
|
\ |
463 |
|
|
return 0; \ |
464 |
|
|
} |
465 |
|
|
|
466 |
|
✗ |
XOVER_PROCESS(fltp, float, 1.f, f) |
467 |
|
✗ |
XOVER_PROCESS(dblp, double, 1.0, d) |
468 |
|
|
|
469 |
|
✗ |
static int config_input(AVFilterLink *inlink) |
470 |
|
|
{ |
471 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
472 |
|
✗ |
AudioCrossoverContext *s = ctx->priv; |
473 |
|
✗ |
int sample_rate = inlink->sample_rate; |
474 |
|
|
double q[16]; |
475 |
|
|
|
476 |
|
✗ |
s->order = (s->order_opt + 1) * 2; |
477 |
|
✗ |
s->filter_count = s->order / 2; |
478 |
|
✗ |
s->first_order = s->filter_count & 1; |
479 |
|
✗ |
s->ap_filter_count = s->filter_count / 2 + s->first_order; |
480 |
|
✗ |
calc_q_factors(s->order, q); |
481 |
|
|
|
482 |
|
✗ |
for (int band = 0; band <= s->nb_splits; band++) { |
483 |
|
✗ |
if (s->first_order) { |
484 |
|
✗ |
set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate); |
485 |
|
✗ |
set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate); |
486 |
|
|
} |
487 |
|
|
|
488 |
|
✗ |
for (int n = s->first_order; n < s->filter_count; n++) { |
489 |
|
✗ |
const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1; |
490 |
|
|
|
491 |
|
✗ |
set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate); |
492 |
|
✗ |
set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate); |
493 |
|
|
} |
494 |
|
|
|
495 |
|
✗ |
if (s->first_order) |
496 |
|
✗ |
set_ap1(&s->ap[band][0], s->splits[band], sample_rate); |
497 |
|
|
|
498 |
|
✗ |
for (int n = s->first_order; n < s->ap_filter_count; n++) { |
499 |
|
✗ |
const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1); |
500 |
|
|
|
501 |
|
✗ |
set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate); |
502 |
|
|
} |
503 |
|
|
} |
504 |
|
|
|
505 |
|
✗ |
switch (inlink->format) { |
506 |
|
✗ |
case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break; |
507 |
|
✗ |
case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break; |
508 |
|
✗ |
default: return AVERROR_BUG; |
509 |
|
|
} |
510 |
|
|
|
511 |
|
✗ |
s->xover = ff_get_audio_buffer(inlink, 2 * (ctx->nb_outputs * 10 + ctx->nb_outputs * 10 + |
512 |
|
✗ |
ctx->nb_outputs * ctx->nb_outputs * 10)); |
513 |
|
✗ |
if (!s->xover) |
514 |
|
✗ |
return AVERROR(ENOMEM); |
515 |
|
|
|
516 |
|
✗ |
return 0; |
517 |
|
|
} |
518 |
|
|
|
519 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
520 |
|
|
{ |
521 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
522 |
|
✗ |
AudioCrossoverContext *s = ctx->priv; |
523 |
|
✗ |
AVFrame **frames = s->frames; |
524 |
|
✗ |
int ret = 0; |
525 |
|
|
|
526 |
|
✗ |
for (int i = 0; i < ctx->nb_outputs; i++) { |
527 |
|
✗ |
frames[i] = ff_get_audio_buffer(ctx->outputs[i], in->nb_samples); |
528 |
|
✗ |
if (!frames[i]) { |
529 |
|
✗ |
ret = AVERROR(ENOMEM); |
530 |
|
✗ |
break; |
531 |
|
|
} |
532 |
|
|
|
533 |
|
✗ |
frames[i]->pts = in->pts; |
534 |
|
|
} |
535 |
|
|
|
536 |
|
✗ |
if (ret < 0) |
537 |
|
✗ |
goto fail; |
538 |
|
|
|
539 |
|
✗ |
ff_filter_execute(ctx, s->filter_channels, in, NULL, |
540 |
|
✗ |
FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx))); |
541 |
|
|
|
542 |
|
✗ |
for (int i = 0; i < ctx->nb_outputs; i++) { |
543 |
|
✗ |
if (ff_outlink_get_status(ctx->outputs[i])) { |
544 |
|
✗ |
av_frame_free(&frames[i]); |
545 |
|
✗ |
continue; |
546 |
|
|
} |
547 |
|
|
|
548 |
|
✗ |
ret = ff_filter_frame(ctx->outputs[i], frames[i]); |
549 |
|
✗ |
frames[i] = NULL; |
550 |
|
✗ |
if (ret < 0) |
551 |
|
✗ |
break; |
552 |
|
|
} |
553 |
|
|
|
554 |
|
✗ |
fail: |
555 |
|
✗ |
for (int i = 0; i < ctx->nb_outputs; i++) |
556 |
|
✗ |
av_frame_free(&frames[i]); |
557 |
|
|
|
558 |
|
✗ |
return ret; |
559 |
|
|
} |
560 |
|
|
|
561 |
|
✗ |
static int activate(AVFilterContext *ctx) |
562 |
|
|
{ |
563 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
564 |
|
|
int status, ret; |
565 |
|
|
AVFrame *in; |
566 |
|
|
int64_t pts; |
567 |
|
|
|
568 |
|
✗ |
for (int i = 0; i < ctx->nb_outputs; i++) { |
569 |
|
✗ |
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx); |
570 |
|
|
} |
571 |
|
|
|
572 |
|
✗ |
ret = ff_inlink_consume_frame(inlink, &in); |
573 |
|
✗ |
if (ret < 0) |
574 |
|
✗ |
return ret; |
575 |
|
✗ |
if (ret > 0) { |
576 |
|
✗ |
ret = filter_frame(inlink, in); |
577 |
|
✗ |
av_frame_free(&in); |
578 |
|
✗ |
if (ret < 0) |
579 |
|
✗ |
return ret; |
580 |
|
|
} |
581 |
|
|
|
582 |
|
✗ |
if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { |
583 |
|
✗ |
for (int i = 0; i < ctx->nb_outputs; i++) { |
584 |
|
✗ |
if (ff_outlink_get_status(ctx->outputs[i])) |
585 |
|
✗ |
continue; |
586 |
|
✗ |
ff_outlink_set_status(ctx->outputs[i], status, pts); |
587 |
|
|
} |
588 |
|
✗ |
return 0; |
589 |
|
|
} |
590 |
|
|
|
591 |
|
✗ |
for (int i = 0; i < ctx->nb_outputs; i++) { |
592 |
|
✗ |
if (ff_outlink_get_status(ctx->outputs[i])) |
593 |
|
✗ |
continue; |
594 |
|
|
|
595 |
|
✗ |
if (ff_outlink_frame_wanted(ctx->outputs[i])) { |
596 |
|
✗ |
ff_inlink_request_frame(inlink); |
597 |
|
✗ |
return 0; |
598 |
|
|
} |
599 |
|
|
} |
600 |
|
|
|
601 |
|
✗ |
return FFERROR_NOT_READY; |
602 |
|
|
} |
603 |
|
|
|
604 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
605 |
|
|
{ |
606 |
|
✗ |
AudioCrossoverContext *s = ctx->priv; |
607 |
|
|
|
608 |
|
✗ |
av_freep(&s->fdsp); |
609 |
|
✗ |
av_frame_free(&s->xover); |
610 |
|
✗ |
} |
611 |
|
|
|
612 |
|
|
static const AVFilterPad inputs[] = { |
613 |
|
|
{ |
614 |
|
|
.name = "default", |
615 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
616 |
|
|
.config_props = config_input, |
617 |
|
|
}, |
618 |
|
|
}; |
619 |
|
|
|
620 |
|
|
const AVFilter ff_af_acrossover = { |
621 |
|
|
.name = "acrossover", |
622 |
|
|
.description = NULL_IF_CONFIG_SMALL("Split audio into per-bands streams."), |
623 |
|
|
.priv_size = sizeof(AudioCrossoverContext), |
624 |
|
|
.priv_class = &acrossover_class, |
625 |
|
|
.init = init, |
626 |
|
|
.activate = activate, |
627 |
|
|
.uninit = uninit, |
628 |
|
|
FILTER_INPUTS(inputs), |
629 |
|
|
.outputs = NULL, |
630 |
|
|
FILTER_QUERY_FUNC2(query_formats), |
631 |
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS | |
632 |
|
|
AVFILTER_FLAG_SLICE_THREADS, |
633 |
|
|
}; |
634 |
|
|
|