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

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/af_acrossover.c+85−63
@@ -39,21 +39,17 @@ · 38 unmodified lines
3939 #define MAX_SPLITS 16
4040 #define MAX_BANDS MAX_SPLITS + 1
4141
42+#define B0 0
43+#define B1 1
44+#define B2 2
45+#define A1 3
46+#define A2 4
47+
4248 typedef struct BiquadCoeffs {
43− double b0, b1, b2;
44− double a1, a2;
49+ double cd[5];
50+ float cf[5];
4551 } BiquadCoeffs;
4652
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−
5753 typedef struct AudioCrossoverContext {
5854 const AVClass *class;
5955
@@ -72,7 +68,7 @@ typedef struct AudioCrossoverContext { · 12 unmodified lines
7268 BiquadCoeffs hp[MAX_BANDS][20];
7369 BiquadCoeffs ap[MAX_BANDS][20];
7470
75− CrossoverChannel *xover;
71+ AVFrame *xover;
7672
7773 AVFrame *input_frame;
7874 AVFrame *frames[MAX_BANDS];
@@ -178,11 +174,17 @@ static void set_lp(BiquadCoeffs *b, double fc, double q, double sr) · 99 unmodified lines
178174 double a1 = -2. * cosine;
179175 double a2 = 1. - alpha;
180176
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;
177+ b->cd[B0] = b0 / a0;
178+ b->cd[B1] = b1 / a0;
179+ b->cd[B2] = b2 / a0;
180+ b->cd[A1] = -a1 / 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];
186188 }
187189
188190 static 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
198200 double a1 = -2. * cosine;
199201 double a2 = 1. - alpha;
200202
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;
203+ b->cd[B0] = b0 / a0;
204+ b->cd[B1] = b1 / a0;
205+ b->cd[B2] = b2 / a0;
206+ b->cd[A1] = -a1 / 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];
206214 }
207215
208216 static 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
218226 double b1 = a1;
219227 double b2 = a0;
220228
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;
229+ b->cd[B0] = b0 / a0;
230+ b->cd[B1] = b1 / a0;
231+ b->cd[B2] = b2 / a0;
232+ b->cd[A1] = -a1 / 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];
226240 }
227241
228242 static void set_ap1(BiquadCoeffs *b, double fc, double sr)
229243 {
230244 double omega = 2. * M_PI * fc / sr;
231245
232− b->a1 = exp(-omega);
233− b->a2 = 0.;
234− b->b0 = -b->a1;
235− b->b1 = 1.;
236− b->b2 = 0.;
246+ b->cd[A1] = exp(-omega);
247+ b->cd[A2] = 0.;
248+ b->cd[B0] = -b->cd[A1];
249+ b->cd[B1] = 1.;
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];
237257 }
238258
239259 static void calc_q_factors(int order, double *q)
@@ -275,18 +295,18 @@ static int query_formats(AVFilterContext *ctx) · 35 unmodified lines
275295 }
276296
277297 #define BIQUAD_PROCESS(name, type) \
278−static void biquad_process_## name(const BiquadCoeffs *const c,\
279− BiquadContext *b, \
298+static void biquad_process_## name(const type *const c, \
299+ type *b, \
280300 type *dst, const type *src, \
281301 int nb_samples) \
282302 { \
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; \
303+ const type b0 = c[B0]; \
304+ const type b1 = c[B1]; \
305+ const type b2 = c[B2]; \
306+ const type a1 = c[A1]; \
307+ const type a2 = c[A2]; \
308+ type z1 = b[0]; \
309+ type z2 = b[1]; \
290310 \
291311 for (int n = 0; n + 1 < nb_samples; n++) { \
292312 type in = src[n]; \
@@ -316,8 +336,8 @@ static void biquad_process_## name(const BiquadCoeffs *const c,\ · 23 unmodified lines
316336 dst[n] = out; \
317337 } \
318338 \
319− b->z1 = z1; \
320− b->z2 = z2; \
339+ b[0] = z1; \
340+ b[1] = z2; \
321341 }
322342
323343 BIQUAD_PROCESS(fltp, float)
@@ -332,40 +352,41 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i · 8 unmodified lines
332352 const int start = (in->channels * jobnr) / nb_jobs; \
333353 const int end = (in->channels * (jobnr+1)) / nb_jobs; \
334354 const int nb_samples = in->nb_samples; \
355+ const int nb_outs = ctx->nb_outputs; \
335356 \
336357 for (int ch = start; ch < end; ch++) { \
337358 const type *src = (const type *)in->extended_data[ch]; \
338− CrossoverChannel *xover = &s->xover[ch]; \
359+ type *xover = (type *)s->xover->extended_data[ch]; \
339360 \
340361 s->fdsp->vector_## ff ##mul_scalar((type *)frames[0]->extended_data[ch], src, \
341362 s->level_in, FFALIGN(nb_samples, sizeof(type))); \
342363 \
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++) { \
364+ for (int band = 0; band < nb_outs; band++) { \
365+ for (int f = 0; band + 1 < nb_outs && f < s->filter_count; f++) { \
345366 const type *prv = (const type *)frames[band]->extended_data[ch]; \
346367 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
347368 const type *hsrc = f == 0 ? prv : dst; \
348− BiquadContext *hp = &xover->hp[band][f]; \
349− BiquadCoeffs *hpc = &s->hp[band][f]; \
369+ type *hp = xover + nb_outs * 20 + band * 20 + f * 2; \
370+ const type *const hpc = (type *)&s->hp[band][f].c ## ff; \
350371 \
351372 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
352373 } \
353374 \
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++) { \
355376 type *dst = (type *)frames[band]->extended_data[ch]; \
356377 const type *lsrc = dst; \
357− BiquadContext *lp = &xover->lp[band][f]; \
358− BiquadCoeffs *lpc = &s->lp[band][f]; \
378+ type *lp = xover + band * 20 + f * 2; \
379+ const type *const lpc = (type *)&s->lp[band][f].c ## ff; \
359380 \
360381 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
361382 } \
362383 \
363− for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \
384+ for (int aband = band + 1; aband + 1 < nb_outs; aband++) { \
364385 if (s->first_order) { \
365386 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
366387 type *dst = (type *)frames[band]->extended_data[ch]; \
367− BiquadContext *ap = &xover->ap[band][aband][0]; \
368− BiquadCoeffs *apc = &s->ap[aband][0]; \
388+ type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20; \
389+ const type *const apc = (type *)&s->ap[aband][0].c ## ff; \
369390 \
370391 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
371392 } \
@@ -373,15 +394,15 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i · 1 unmodified line
373394 for (int f = s->first_order; f < s->ap_filter_count; f++) { \
374395 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
375396 type *dst = (type *)frames[band]->extended_data[ch]; \
376− BiquadContext *ap = &xover->ap[band][aband][f]; \
377− BiquadCoeffs *apc = &s->ap[aband][f]; \
397+ type *ap = xover + nb_outs * 40 + (aband * nb_outs + band) * 20 + f * 2;\
398+ const type *const apc = (type *)&s->ap[aband][f].c ## ff; \
378399 \
379400 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
380401 } \
381402 } \
382403 } \
383404 \
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++) { \
385406 if (band & 1) { \
386407 type *dst = (type *)frames[band]->extended_data[ch]; \
387408 s->fdsp->vector_## ff ##mul_scalar(dst, dst, -one, \
@@ -403,10 +424,6 @@ static int config_input(AVFilterLink *inlink) · 15 unmodified lines
403424 int sample_rate = inlink->sample_rate;
404425 double q[16];
405426
406− s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
407− if (!s->xover)
408− return AVERROR(ENOMEM);
409−
410427 s->order = (s->order_opt + 1) * 2;
411428 s->filter_count = s->order / 2;
412429 s->first_order = s->filter_count & 1;
@@ -441,6 +458,11 @@ static int config_input(AVFilterLink *inlink) · 28 unmodified lines
441458 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
442459 }
443460
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+
444466 return 0;
445467 }
446468
@@ -492,7 +514,7 @@ static av_cold void uninit(AVFilterContext *ctx) · 45 unmodified lines
492514
493515 av_freep(&s->fdsp);
494516 av_freep(&s->splits);
495− av_freep(&s->xover);
517+ av_frame_free(&s->xover);
496518
497519 for (i = 0; i < ctx->nb_outputs; i++)
498520 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