History

avfilter/af_acrossover: move coefficients and state to simple arrays

Paul B Mahol committed 5 years ago

Commit ba6e2a21 parent aa3566a1 file · +85 −63Browse files

Unified · Split

libavfilter/af_acrossover.c+85−63
@@ -39,21 +39,17 @@ · 38 unmodified lines
39#define MAX_SPLITS 1639#define MAX_SPLITS 16
40#define MAX_BANDS MAX_SPLITS + 140#define MAX_BANDS MAX_SPLITS + 1
41 41
42#define B0 0
43#define B1 1
44#define B2 2
45#define A1 3
46#define A2 4
47
42typedef struct BiquadCoeffs {48typedef struct BiquadCoeffs {
43 double b0, b1, b2;49 double cd[5];
44 double a1, a2;50 float cf[5];
45} BiquadCoeffs;51} BiquadCoeffs;
46 52
47typedef struct BiquadContext {
48 double z1, z2;
49} BiquadContext;
50
51typedef 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
57typedef struct AudioCrossoverContext {53typedef struct AudioCrossoverContext {
58 const AVClass *class;54 const AVClass *class;
59 55
@@ -72,7 +68,7 @@ typedef struct AudioCrossoverContext { · 12 unmodified lines
72 BiquadCoeffs hp[MAX_BANDS][20];68 BiquadCoeffs hp[MAX_BANDS][20];
73 BiquadCoeffs ap[MAX_BANDS][20];69 BiquadCoeffs ap[MAX_BANDS][20];
74 70
75 CrossoverChannel *xover;71 AVFrame *xover;
76 72
77 AVFrame *input_frame;73 AVFrame *input_frame;
78 AVFrame *frames[MAX_BANDS];74 AVFrame *frames[MAX_BANDS];
@@ -178,11 +174,17 @@ static void set_lp(BiquadCoeffs *b, double fc, double q, double sr) · 99 unmodified lines
178 double a1 = -2. * cosine;174 double a1 = -2. * cosine;
179 double a2 = 1. - alpha;175 double a2 = 1. - alpha;
180 176
181 b->b0 = b0 / a0;177 b->cd[B0] = b0 / a0;
182 b->b1 = b1 / a0;178 b->cd[B1] = b1 / a0;
183 b->b2 = b2 / a0;179 b->cd[B2] = b2 / a0;
184 b->a1 = -a1 / a0;180 b->cd[A1] = -a1 / a0;
185 b->a2 = -a2 / a0;181 b->cd[A2] = -a2 / a0;
182
183 b->cf[B0] = b->cd[B0];
184 b->cf[B1] = b->cd[B1];
185 b->cf[B2] = b->cd[B2];
186 b->cf[A1] = b->cd[A1];
187 b->cf[A2] = b->cd[A2];
186}188}
187 189
188static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)190static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
@@ -198,11 +200,17 @@ static void set_hp(BiquadCoeffs *b, double fc, double q, double sr) · 9 unmodified lines
198 double a1 = -2. * cosine;200 double a1 = -2. * cosine;
199 double a2 = 1. - alpha;201 double a2 = 1. - alpha;
200 202
201 b->b0 = b0 / a0;203 b->cd[B0] = b0 / a0;
202 b->b1 = b1 / a0;204 b->cd[B1] = b1 / a0;
203 b->b2 = b2 / a0;205 b->cd[B2] = b2 / a0;
204 b->a1 = -a1 / a0;206 b->cd[A1] = -a1 / a0;
205 b->a2 = -a2 / a0;207 b->cd[A2] = -a2 / a0;
208
209 b->cf[B0] = b->cd[B0];
210 b->cf[B1] = b->cd[B1];
211 b->cf[B2] = b->cd[B2];
212 b->cf[A1] = b->cd[A1];
213 b->cf[A2] = b->cd[A2];
206}214}
207 215
208static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)216static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
@@ -218,22 +226,34 @@ static void set_ap(BiquadCoeffs *b, double fc, double q, double sr) · 9 unmodified lines
218 double b1 = a1;226 double b1 = a1;
219 double b2 = a0;227 double b2 = a0;
220 228
221 b->b0 = b0 / a0;229 b->cd[B0] = b0 / a0;
222 b->b1 = b1 / a0;230 b->cd[B1] = b1 / a0;
223 b->b2 = b2 / a0;231 b->cd[B2] = b2 / a0;
224 b->a1 = -a1 / a0;232 b->cd[A1] = -a1 / a0;
225 b->a2 = -a2 / a0;233 b->cd[A2] = -a2 / a0;
234
235 b->cf[B0] = b->cd[B0];
236 b->cf[B1] = b->cd[B1];
237 b->cf[B2] = b->cd[B2];
238 b->cf[A1] = b->cd[A1];
239 b->cf[A2] = b->cd[A2];
226}240}
227 241
228static void set_ap1(BiquadCoeffs *b, double fc, double sr)242static void set_ap1(BiquadCoeffs *b, double fc, double sr)
229{243{
230 double omega = 2. * M_PI * fc / sr;244 double omega = 2. * M_PI * fc / sr;
231 245
232 b->a1 = exp(-omega);246 b->cd[A1] = exp(-omega);
233 b->a2 = 0.;247 b->cd[A2] = 0.;
234 b->b0 = -b->a1;248 b->cd[B0] = -b->cd[A1];
235 b->b1 = 1.;249 b->cd[B1] = 1.;
236 b->b2 = 0.;250 b->cd[B2] = 0.;
251
252 b->cf[B0] = b->cd[B0];
253 b->cf[B1] = b->cd[B1];
254 b->cf[B2] = b->cd[B2];
255 b->cf[A1] = b->cd[A1];
256 b->cf[A2] = b->cd[A2];
237}257}
238 258
239static void calc_q_factors(int order, double *q)259static void calc_q_factors(int order, double *q)
@@ -275,18 +295,18 @@ static int query_formats(AVFilterContext *ctx) · 35 unmodified lines
275}295}
276 296
277#define BIQUAD_PROCESS(name, type) \297#define BIQUAD_PROCESS(name, type) \
278static void biquad_process_## name(const BiquadCoeffs *const c,\298static void biquad_process_## name(const type *const c, \
279 BiquadContext *b, \299 type *b, \
280 type *dst, const type *src, \300 type *dst, const type *src, \
281 int nb_samples) \301 int nb_samples) \
282{ \302{ \
283 const type b0 = c->b0; \303 const type b0 = c[B0]; \
284 const type b1 = c->b1; \304 const type b1 = c[B1]; \
285 const type b2 = c->b2; \305 const type b2 = c[B2]; \
286 const type a1 = c->a1; \306 const type a1 = c[A1]; \
287 const type a2 = c->a2; \307 const type a2 = c[A2]; \
288 type z1 = b->z1; \308 type z1 = b[0]; \
289 type z2 = b->z2; \309 type z2 = b[1]; \
290 \310 \
291 for (int n = 0; n + 1 < nb_samples; n++) { \311 for (int n = 0; n + 1 < nb_samples; n++) { \
292 type in = src[n]; \312 type in = src[n]; \
@@ -316,8 +336,8 @@ static void biquad_process_## name(const BiquadCoeffs *const c,\ · 23 unmodified lines
316 dst[n] = out; \336 dst[n] = out; \
317 } \337 } \
318 \338 \
319 b->z1 = z1; \339 b[0] = z1; \
320 b->z2 = z2; \340 b[1] = z2; \
321}341}
322 342
323BIQUAD_PROCESS(fltp, float)343BIQUAD_PROCESS(fltp, float)
@@ -332,40 +352,41 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i · 8 unmodified lines
332 const int start = (in->channels * jobnr) / nb_jobs; \352 const int start = (in->channels * jobnr) / nb_jobs; \
333 const int end = (in->channels * (jobnr+1)) / nb_jobs; \353 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
334 const int nb_samples = in->nb_samples; \354 const int nb_samples = in->nb_samples; \
355 const int nb_outs = ctx->nb_outputs; \
335 \356 \
336 for (int ch = start; ch < end; ch++) { \357 for (int ch = start; ch < end; ch++) { \
337 const type *src = (const type *)in->extended_data[ch]; \358 const type *src = (const type *)in->extended_data[ch]; \
338 CrossoverChannel *xover = &s->xover[ch]; \359 type *xover = (type *)s->xover->extended_data[ch]; \
339 \360 \
340 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \361 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
341 s->level_in, FFALIGN(nb_samples, sizeof(type))); \362 s->level_in, FFALIGN(nb_samples, sizeof(type))); \
342 \363 \
343 for (int band = 0; band < ctx->nb_outputs; band++) { \364 for (int band = 0; band < nb_outs; band++) { \
344 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \365 for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
345 const type *prv = (const type *)frames[band]->extended_data[ch]; \366 const type *prv = (const type *)frames[band]->extended_data[ch]; \
346 type *dst = (type *)frames[band + 1]->extended_data[ch]; \367 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
347 const type *hsrc = f == 0 ? prv : dst; \368 const type *hsrc = f == 0 ? prv : dst; \
348 BiquadContext *hp = &xover->hp[band][f]; \369 type *hp = xover + nb_outs * 20 + band * 20 + f * 2; \
349 BiquadCoeffs *hpc = &s->hp[band][f]; \370 const type *const hpc = (type *)&s->hp[band][f].c ## ff; \
350 \371 \
351 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \372 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
352 } \373 } \
353 \374 \
354 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \375 for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
355 type *dst = (type *)frames[band]->extended_data[ch]; \376 type *dst = (type *)frames[band]->extended_data[ch]; \
356 const type *lsrc = dst; \377 const type *lsrc = dst; \
357 BiquadContext *lp = &xover->lp[band][f]; \378 type *lp = xover + band * 20 + f * 2; \
358 BiquadCoeffs *lpc = &s->lp[band][f]; \379 const type *const lpc = (type *)&s->lp[band][f].c ## ff; \
359 \380 \
360 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \381 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
361 } \382 } \
362 \383 \
363 for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \384 for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \
364 if (s->first_order) { \385 if (s->first_order) { \
365 const type *asrc = (const type *)frames[band]->extended_data[ch]; \386 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
366 type *dst = (type *)frames[band]->extended_data[ch]; \387 type *dst = (type *)frames[band]->extended_data[ch]; \
367 BiquadContext *ap = &xover->ap[band][aband][0]; \388 type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \
368 BiquadCoeffs *apc = &s->ap[aband][0]; \389 const type *const apc = (type *)&s->ap[aband][0].c ## ff; \
369 \390 \
370 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \391 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
371 } \392 } \
@@ -373,15 +394,15 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i · 1 unmodified line
373 for (int f = s->first_order; f < s->ap_filter_count; f++) { \394 for (int f = s->first_order; f < s->ap_filter_count; f++) { \
374 const type *asrc = (const type *)frames[band]->extended_data[ch]; \395 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
375 type *dst = (type *)frames[band]->extended_data[ch]; \396 type *dst = (type *)frames[band]->extended_data[ch]; \
376 BiquadContext *ap = &xover->ap[band][aband][f]; \397 type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\
377 BiquadCoeffs *apc = &s->ap[aband][f]; \398 const type *const apc = (type *)&s->ap[aband][f].c ## ff; \
378 \399 \
379 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \400 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
380 } \401 } \
381 } \402 } \
382 } \403 } \
383 \404 \
384 for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \405 for (int band = 0; band < nb_outs && s->first_order; band++) { \
385 if (band & 1) { \406 if (band & 1) { \
386 type *dst = (type *)frames[band]->extended_data[ch]; \407 type *dst = (type *)frames[band]->extended_data[ch]; \
387 s->fdsp->vector_## ff ##mul_scalar(dst, dst, -one, \408 s->fdsp->vector_## ff ##mul_scalar(dst, dst, -one, \
@@ -403,10 +424,6 @@ static int config_input(AVFilterLink *inlink) · 15 unmodified lines
403 int sample_rate = inlink->sample_rate;424 int sample_rate = inlink->sample_rate;
404 double q[16];425 double q[16];
405 426
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;427 s->order = (s->order_opt + 1) * 2;
411 s->filter_count = s->order / 2;428 s->filter_count = s->order / 2;
412 s->first_order = s->filter_count & 1;429 s->first_order = s->filter_count & 1;
@@ -441,6 +458,11 @@ static int config_input(AVFilterLink *inlink) · 28 unmodified lines
441 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;458 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
442 }459 }
443 460
461 s->xover = ff_get_audio_buffer(inlink, 2 * (ctx->nb_outputs * 10 + ctx->nb_outputs * 10 +
462 ctx->nb_outputs * ctx->nb_outputs * 10));
463 if (!s->xover)
464 return AVERROR(ENOMEM);
465
444 return 0;466 return 0;
445}467}
446 468
@@ -492,7 +514,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 45 unmodified lines
492 514
493 av_freep(&s->fdsp);515 av_freep(&s->fdsp);
494 av_freep(&s->splits);516 av_freep(&s->splits);
495 av_freep(&s->xover);517 av_frame_free(&s->xover);
496 518
497 for (i = 0; i < ctx->nb_outputs; i++)519 for (i = 0; i < ctx->nb_outputs; i++)
498 av_freep(&ctx->output_pads[i].name);520 av_freep(&ctx->output_pads[i].name);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t